4 * Copyright (C) 2011 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 #include <linux/delay.h>
23 * packet info function
25 static int usbhsf_null_handle(struct usbhs_pkt *pkt)
27 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
28 struct device *dev = usbhs_priv_to_dev(priv);
30 dev_err(dev, "null handler\n");
35 static struct usbhs_pkt_handle usbhsf_null_handler = {
36 .prepare = usbhsf_null_handle,
37 .try_run = usbhsf_null_handle,
40 void usbhs_pkt_init(struct usbhs_pkt *pkt)
42 INIT_LIST_HEAD(&pkt->node);
45 void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt,
46 struct usbhs_pkt_handle *handler,
47 void *buf, int len, int zero)
49 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
50 struct device *dev = usbhs_priv_to_dev(priv);
53 dev_err(dev, "no handler function\n");
54 handler = &usbhsf_null_handler;
57 list_del_init(&pkt->node);
58 list_add_tail(&pkt->node, &pipe->list);
62 pkt->handler = handler;
68 void usbhs_pkt_pop(struct usbhs_pkt *pkt)
70 list_del_init(&pkt->node);
73 struct usbhs_pkt *usbhs_pkt_get(struct usbhs_pipe *pipe)
75 if (list_empty(&pipe->list))
78 return list_entry(pipe->list.next, struct usbhs_pkt, node);
82 * irq enable/disable function
84 #define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, bempsts, e)
85 #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, brdysts, e)
86 #define usbhsf_irq_callback_ctrl(pipe, status, enable) \
88 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
89 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
90 u16 status = (1 << usbhs_pipe_number(pipe)); \
94 mod->irq_##status |= status; \
96 mod->irq_##status &= ~status; \
97 usbhs_irq_callback_update(priv, mod); \
100 static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
103 * And DCP pipe can NOT use "ready interrupt" for "send"
104 * it should use "empty" interrupt.
106 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
108 * on the other hand, normal pipe can use "ready interrupt" for "send"
109 * even though it is single/double buffer
111 if (usbhs_pipe_is_dcp(pipe))
112 usbhsf_irq_empty_ctrl(pipe, enable);
114 usbhsf_irq_ready_ctrl(pipe, enable);
117 static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
119 usbhsf_irq_ready_ctrl(pipe, enable);
125 static void usbhsf_send_terminator(struct usbhs_pipe *pipe)
127 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
129 usbhs_bset(priv, CFIFOCTR, BVAL, BVAL);
132 static int usbhsf_fifo_barrier(struct usbhs_priv *priv)
137 /* The FIFO port is accessible */
138 if (usbhs_read(priv, CFIFOCTR) & FRDY)
147 static void usbhsf_fifo_clear(struct usbhs_pipe *pipe)
149 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
151 if (!usbhs_pipe_is_dcp(pipe))
152 usbhsf_fifo_barrier(priv);
154 usbhs_write(priv, CFIFOCTR, BCLR);
157 static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv)
159 return usbhs_read(priv, CFIFOCTR) & DTLN_MASK;
162 static int usbhsf_fifo_select(struct usbhs_pipe *pipe, int write)
164 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
165 struct device *dev = usbhs_priv_to_dev(priv);
167 u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
168 u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
170 if (usbhs_pipe_is_dcp(pipe))
171 base |= (1 == write) << 5; /* ISEL */
173 /* "base" will be used below */
174 usbhs_write(priv, CFIFOSEL, base | MBW_32);
176 /* check ISEL and CURPIPE value */
178 if (base == (mask & usbhs_read(priv, CFIFOSEL)))
183 dev_err(dev, "fifo select error\n");
191 static int usbhsf_try_push(struct usbhs_pkt *pkt)
193 struct usbhs_pipe *pipe = pkt->pipe;
194 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
195 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
196 struct device *dev = usbhs_priv_to_dev(priv);
197 void __iomem *addr = priv->base + CFIFO;
199 int maxp = usbhs_pipe_get_maxpacket(pipe);
202 int is_short, is_done;
204 ret = usbhsf_fifo_select(pipe, 1);
206 goto usbhs_fifo_write_busy;
208 ret = usbhs_pipe_is_accessible(pipe);
210 goto usbhs_fifo_write_busy;
212 ret = usbhsf_fifo_barrier(priv);
214 goto usbhs_fifo_write_busy;
216 buf = pkt->buf + pkt->actual;
217 len = pkt->length - pkt->actual;
218 len = min(len, maxp);
220 is_short = total_len < maxp;
227 if (len >= 4 && !((unsigned long)buf & 0x03)) {
228 iowrite32_rep(addr, buf, len / 4);
230 buf += total_len - len;
233 /* the rest operation */
234 for (i = 0; i < len; i++)
235 iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
240 pkt->actual += total_len;
242 if (pkt->actual < pkt->length)
243 is_done = 0; /* there are remainder data */
245 is_done = 1; /* short packet */
247 is_done = !pkt->zero; /* send zero packet ? */
253 usbhsf_send_terminator(pipe);
255 usbhsf_tx_irq_ctrl(pipe, !is_done);
256 usbhs_pipe_enable(pipe);
258 dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
259 usbhs_pipe_number(pipe),
260 pkt->length, pkt->actual, is_done, pkt->zero);
266 if (usbhs_pipe_is_dcp(pipe))
267 usbhs_dcp_control_transfer_done(pipe);
274 usbhs_fifo_write_busy:
279 usbhsf_tx_irq_ctrl(pipe, 1);
284 struct usbhs_pkt_handle usbhs_fifo_push_handler = {
285 .prepare = usbhsf_try_push,
286 .try_run = usbhsf_try_push,
289 static int usbhsf_prepare_pop(struct usbhs_pkt *pkt)
291 struct usbhs_pipe *pipe = pkt->pipe;
295 * select pipe and enable it to prepare packet receive
297 ret = usbhsf_fifo_select(pipe, 0);
301 usbhs_pipe_enable(pipe);
302 usbhsf_rx_irq_ctrl(pipe, 1);
307 static int usbhsf_try_pop(struct usbhs_pkt *pkt)
309 struct usbhs_pipe *pipe = pkt->pipe;
310 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
311 struct device *dev = usbhs_priv_to_dev(priv);
312 void __iomem *addr = priv->base + CFIFO;
315 int maxp = usbhs_pipe_get_maxpacket(pipe);
321 ret = usbhsf_fifo_select(pipe, 0);
325 ret = usbhsf_fifo_barrier(priv);
329 rcv_len = usbhsf_fifo_rcv_len(priv);
331 buf = pkt->buf + pkt->actual;
332 len = pkt->length - pkt->actual;
333 len = min(len, rcv_len);
337 * Buffer clear if Zero-Length packet
340 * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
343 usbhsf_fifo_clear(pipe);
344 goto usbhs_fifo_read_end;
352 if (len >= 4 && !((unsigned long)buf & 0x03)) {
353 ioread32_rep(addr, buf, len / 4);
355 buf += total_len - len;
358 /* the rest operation */
359 for (i = 0; i < len; i++) {
361 data = ioread32(addr);
363 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
366 pkt->actual += total_len;
369 if ((pkt->actual == pkt->length) || /* receive all data */
370 (total_len < maxp)) /* short packet */
373 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
374 usbhs_pipe_number(pipe),
375 pkt->length, pkt->actual, is_done, pkt->zero);
378 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
380 usbhsf_rx_irq_ctrl(pipe, 0);
381 usbhs_pipe_disable(pipe);
389 struct usbhs_pkt_handle usbhs_fifo_pop_handler = {
390 .prepare = usbhsf_prepare_pop,
391 .try_run = usbhsf_try_pop,
397 static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt)
399 struct usbhs_pipe *pipe = pkt->pipe;
400 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
401 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
403 usbhs_dcp_control_transfer_done(pipe);
410 struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
411 .prepare = usbhsf_ctrl_stage_end,
412 .try_run = usbhsf_ctrl_stage_end,
418 static int usbhsf_irq_empty(struct usbhs_priv *priv,
419 struct usbhs_irq_state *irq_state)
421 struct usbhs_pipe *pipe;
422 struct usbhs_pkt *pkt;
423 struct device *dev = usbhs_priv_to_dev(priv);
426 if (!irq_state->bempsts) {
427 dev_err(dev, "debug %s !!\n", __func__);
431 dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
434 * search interrupted "pipe"
437 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
438 if (!(irq_state->bempsts & (1 << i)))
441 pkt = usbhs_pkt_get(pipe);
442 ret = usbhs_pkt_run(pkt);
444 dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
450 static int usbhsf_irq_ready(struct usbhs_priv *priv,
451 struct usbhs_irq_state *irq_state)
453 struct usbhs_pipe *pipe;
454 struct usbhs_pkt *pkt;
455 struct device *dev = usbhs_priv_to_dev(priv);
458 if (!irq_state->brdysts) {
459 dev_err(dev, "debug %s !!\n", __func__);
463 dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
466 * search interrupted "pipe"
469 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
470 if (!(irq_state->brdysts & (1 << i)))
473 pkt = usbhs_pkt_get(pipe);
474 ret = usbhs_pkt_run(pkt);
476 dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
485 void usbhs_fifo_init(struct usbhs_priv *priv)
487 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
489 mod->irq_empty = usbhsf_irq_empty;
490 mod->irq_ready = usbhsf_irq_ready;
491 mod->irq_bempsts = 0;
492 mod->irq_brdysts = 0;
495 void usbhs_fifo_quit(struct usbhs_priv *priv)
497 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
499 mod->irq_empty = NULL;
500 mod->irq_ready = NULL;
501 mod->irq_bempsts = 0;
502 mod->irq_brdysts = 0;