Realtek cr: Remove unused Macros
[zen-stable.git] / drivers / usb / renesas_usbhs / fifo.c
blob021695de1e9eae6bd0336228381bb10a9666c94b
1 /*
2 * Renesas USB driver
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>
18 #include <linux/io.h>
19 #include "./common.h"
20 #include "./pipe.h"
22 #define usbhsf_get_cfifo(p) (&((p)->fifo_info.cfifo))
23 #define usbhsf_get_d0fifo(p) (&((p)->fifo_info.d0fifo))
24 #define usbhsf_get_d1fifo(p) (&((p)->fifo_info.d1fifo))
26 #define usbhsf_fifo_is_busy(f) ((f)->pipe) /* see usbhs_pipe_select_fifo */
29 * packet info function
31 static int usbhsf_null_handle(struct usbhs_pkt *pkt, int *is_done)
33 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
34 struct device *dev = usbhs_priv_to_dev(priv);
36 dev_err(dev, "null handler\n");
38 return -EINVAL;
41 static struct usbhs_pkt_handle usbhsf_null_handler = {
42 .prepare = usbhsf_null_handle,
43 .try_run = usbhsf_null_handle,
46 void usbhs_pkt_init(struct usbhs_pkt *pkt)
48 pkt->dma = DMA_ADDR_INVALID;
49 INIT_LIST_HEAD(&pkt->node);
52 void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt,
53 struct usbhs_pkt_handle *handler,
54 void *buf, int len, int zero)
56 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
57 struct device *dev = usbhs_priv_to_dev(priv);
58 unsigned long flags;
60 /******************** spin lock ********************/
61 usbhs_lock(priv, flags);
63 if (!handler) {
64 dev_err(dev, "no handler function\n");
65 handler = &usbhsf_null_handler;
68 list_del_init(&pkt->node);
69 list_add_tail(&pkt->node, &pipe->list);
71 pkt->pipe = pipe;
72 pkt->buf = buf;
73 pkt->handler = handler;
74 pkt->length = len;
75 pkt->zero = zero;
76 pkt->actual = 0;
78 usbhs_unlock(priv, flags);
79 /******************** spin unlock ******************/
81 usbhs_pkt_start(pipe);
84 static void __usbhsf_pkt_del(struct usbhs_pkt *pkt)
86 list_del_init(&pkt->node);
89 static struct usbhs_pkt *__usbhsf_pkt_get(struct usbhs_pipe *pipe)
91 if (list_empty(&pipe->list))
92 return NULL;
94 return list_entry(pipe->list.next, struct usbhs_pkt, node);
97 struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt)
99 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
100 unsigned long flags;
102 /******************** spin lock ********************/
103 usbhs_lock(priv, flags);
105 if (!pkt)
106 pkt = __usbhsf_pkt_get(pipe);
108 if (pkt)
109 __usbhsf_pkt_del(pkt);
111 usbhs_unlock(priv, flags);
112 /******************** spin unlock ******************/
114 return pkt;
117 int __usbhs_pkt_handler(struct usbhs_pipe *pipe, int type)
119 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
120 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
121 struct usbhs_pkt *pkt;
122 struct device *dev = usbhs_priv_to_dev(priv);
123 int (*func)(struct usbhs_pkt *pkt, int *is_done);
124 unsigned long flags;
125 int ret = 0;
126 int is_done = 0;
128 /******************** spin lock ********************/
129 usbhs_lock(priv, flags);
131 pkt = __usbhsf_pkt_get(pipe);
132 if (!pkt)
133 goto __usbhs_pkt_handler_end;
135 switch (type) {
136 case USBHSF_PKT_PREPARE:
137 func = pkt->handler->prepare;
138 break;
139 case USBHSF_PKT_TRY_RUN:
140 func = pkt->handler->try_run;
141 break;
142 case USBHSF_PKT_DMA_DONE:
143 func = pkt->handler->dma_done;
144 break;
145 default:
146 dev_err(dev, "unknown pkt hander\n");
147 goto __usbhs_pkt_handler_end;
150 ret = func(pkt, &is_done);
152 if (is_done)
153 __usbhsf_pkt_del(pkt);
155 __usbhs_pkt_handler_end:
156 usbhs_unlock(priv, flags);
157 /******************** spin unlock ******************/
159 if (is_done) {
160 info->done(pkt);
161 usbhs_pkt_start(pipe);
164 return ret;
168 * irq enable/disable function
170 #define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, bempsts, e)
171 #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, brdysts, e)
172 #define usbhsf_irq_callback_ctrl(pipe, status, enable) \
173 ({ \
174 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
175 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
176 u16 status = (1 << usbhs_pipe_number(pipe)); \
177 if (!mod) \
178 return; \
179 if (enable) \
180 mod->irq_##status |= status; \
181 else \
182 mod->irq_##status &= ~status; \
183 usbhs_irq_callback_update(priv, mod); \
186 static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
189 * And DCP pipe can NOT use "ready interrupt" for "send"
190 * it should use "empty" interrupt.
191 * see
192 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
194 * on the other hand, normal pipe can use "ready interrupt" for "send"
195 * even though it is single/double buffer
197 if (usbhs_pipe_is_dcp(pipe))
198 usbhsf_irq_empty_ctrl(pipe, enable);
199 else
200 usbhsf_irq_ready_ctrl(pipe, enable);
203 static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
205 usbhsf_irq_ready_ctrl(pipe, enable);
209 * FIFO ctrl
211 static void usbhsf_send_terminator(struct usbhs_pipe *pipe,
212 struct usbhs_fifo *fifo)
214 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
216 usbhs_bset(priv, fifo->ctr, BVAL, BVAL);
219 static int usbhsf_fifo_barrier(struct usbhs_priv *priv,
220 struct usbhs_fifo *fifo)
222 int timeout = 1024;
224 do {
225 /* The FIFO port is accessible */
226 if (usbhs_read(priv, fifo->ctr) & FRDY)
227 return 0;
229 udelay(10);
230 } while (timeout--);
232 return -EBUSY;
235 static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
236 struct usbhs_fifo *fifo)
238 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
240 if (!usbhs_pipe_is_dcp(pipe))
241 usbhsf_fifo_barrier(priv, fifo);
243 usbhs_write(priv, fifo->ctr, BCLR);
246 static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv,
247 struct usbhs_fifo *fifo)
249 return usbhs_read(priv, fifo->ctr) & DTLN_MASK;
252 static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
253 struct usbhs_fifo *fifo)
255 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
257 usbhs_pipe_select_fifo(pipe, NULL);
258 usbhs_write(priv, fifo->sel, 0);
261 static int usbhsf_fifo_select(struct usbhs_pipe *pipe,
262 struct usbhs_fifo *fifo,
263 int write)
265 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
266 struct device *dev = usbhs_priv_to_dev(priv);
267 int timeout = 1024;
268 u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
269 u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
271 if (usbhs_pipe_is_busy(pipe) ||
272 usbhsf_fifo_is_busy(fifo))
273 return -EBUSY;
275 if (usbhs_pipe_is_dcp(pipe))
276 base |= (1 == write) << 5; /* ISEL */
278 /* "base" will be used below */
279 usbhs_write(priv, fifo->sel, base | MBW_32);
281 /* check ISEL and CURPIPE value */
282 while (timeout--) {
283 if (base == (mask & usbhs_read(priv, fifo->sel))) {
284 usbhs_pipe_select_fifo(pipe, fifo);
285 return 0;
287 udelay(10);
290 dev_err(dev, "fifo select error\n");
292 return -EIO;
296 * PIO fifo functions
298 static int usbhsf_pio_try_push(struct usbhs_pkt *pkt, int *is_done)
300 struct usbhs_pipe *pipe = pkt->pipe;
301 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
302 struct device *dev = usbhs_priv_to_dev(priv);
303 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
304 void __iomem *addr = priv->base + fifo->port;
305 u8 *buf;
306 int maxp = usbhs_pipe_get_maxpacket(pipe);
307 int total_len;
308 int i, ret, len;
309 int is_short;
311 ret = usbhsf_fifo_select(pipe, fifo, 1);
312 if (ret < 0)
313 return 0;
315 ret = usbhs_pipe_is_accessible(pipe);
316 if (ret < 0)
317 goto usbhs_fifo_write_busy;
319 ret = usbhsf_fifo_barrier(priv, fifo);
320 if (ret < 0)
321 goto usbhs_fifo_write_busy;
323 buf = pkt->buf + pkt->actual;
324 len = pkt->length - pkt->actual;
325 len = min(len, maxp);
326 total_len = len;
327 is_short = total_len < maxp;
330 * FIXME
332 * 32-bit access only
334 if (len >= 4 && !((unsigned long)buf & 0x03)) {
335 iowrite32_rep(addr, buf, len / 4);
336 len %= 4;
337 buf += total_len - len;
340 /* the rest operation */
341 for (i = 0; i < len; i++)
342 iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
345 * variable update
347 pkt->actual += total_len;
349 if (pkt->actual < pkt->length)
350 *is_done = 0; /* there are remainder data */
351 else if (is_short)
352 *is_done = 1; /* short packet */
353 else
354 *is_done = !pkt->zero; /* send zero packet ? */
357 * pipe/irq handling
359 if (is_short)
360 usbhsf_send_terminator(pipe, fifo);
362 usbhsf_tx_irq_ctrl(pipe, !*is_done);
363 usbhs_pipe_enable(pipe);
365 dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
366 usbhs_pipe_number(pipe),
367 pkt->length, pkt->actual, *is_done, pkt->zero);
370 * Transmission end
372 if (*is_done) {
373 if (usbhs_pipe_is_dcp(pipe))
374 usbhs_dcp_control_transfer_done(pipe);
377 usbhsf_fifo_unselect(pipe, fifo);
379 return 0;
381 usbhs_fifo_write_busy:
382 usbhsf_fifo_unselect(pipe, fifo);
385 * pipe is busy.
386 * retry in interrupt
388 usbhsf_tx_irq_ctrl(pipe, 1);
390 return ret;
393 struct usbhs_pkt_handle usbhs_fifo_pio_push_handler = {
394 .prepare = usbhsf_pio_try_push,
395 .try_run = usbhsf_pio_try_push,
398 static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
400 struct usbhs_pipe *pipe = pkt->pipe;
402 if (usbhs_pipe_is_busy(pipe))
403 return 0;
406 * pipe enable to prepare packet receive
409 usbhs_pipe_enable(pipe);
410 usbhsf_rx_irq_ctrl(pipe, 1);
412 return 0;
415 static int usbhsf_pio_try_pop(struct usbhs_pkt *pkt, int *is_done)
417 struct usbhs_pipe *pipe = pkt->pipe;
418 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
419 struct device *dev = usbhs_priv_to_dev(priv);
420 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
421 void __iomem *addr = priv->base + fifo->port;
422 u8 *buf;
423 u32 data = 0;
424 int maxp = usbhs_pipe_get_maxpacket(pipe);
425 int rcv_len, len;
426 int i, ret;
427 int total_len = 0;
429 ret = usbhsf_fifo_select(pipe, fifo, 0);
430 if (ret < 0)
431 return 0;
433 ret = usbhsf_fifo_barrier(priv, fifo);
434 if (ret < 0)
435 goto usbhs_fifo_read_busy;
437 rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
439 buf = pkt->buf + pkt->actual;
440 len = pkt->length - pkt->actual;
441 len = min(len, rcv_len);
442 total_len = len;
445 * Buffer clear if Zero-Length packet
447 * see
448 * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
450 if (0 == rcv_len) {
451 usbhsf_fifo_clear(pipe, fifo);
452 goto usbhs_fifo_read_end;
456 * FIXME
458 * 32-bit access only
460 if (len >= 4 && !((unsigned long)buf & 0x03)) {
461 ioread32_rep(addr, buf, len / 4);
462 len %= 4;
463 buf += total_len - len;
466 /* the rest operation */
467 for (i = 0; i < len; i++) {
468 if (!(i & 0x03))
469 data = ioread32(addr);
471 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
474 pkt->actual += total_len;
476 usbhs_fifo_read_end:
477 if ((pkt->actual == pkt->length) || /* receive all data */
478 (total_len < maxp)) { /* short packet */
479 *is_done = 1;
480 usbhsf_rx_irq_ctrl(pipe, 0);
481 usbhs_pipe_disable(pipe);
484 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
485 usbhs_pipe_number(pipe),
486 pkt->length, pkt->actual, *is_done, pkt->zero);
488 usbhs_fifo_read_busy:
489 usbhsf_fifo_unselect(pipe, fifo);
491 return ret;
494 struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler = {
495 .prepare = usbhsf_prepare_pop,
496 .try_run = usbhsf_pio_try_pop,
500 * handler function
502 static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
504 usbhs_dcp_control_transfer_done(pkt->pipe);
506 *is_done = 1;
508 return 0;
511 struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
512 .prepare = usbhsf_ctrl_stage_end,
513 .try_run = usbhsf_ctrl_stage_end,
517 * DMA fifo functions
519 static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
520 struct usbhs_pkt *pkt)
522 if (&usbhs_fifo_dma_push_handler == pkt->handler)
523 return fifo->tx_chan;
525 if (&usbhs_fifo_dma_pop_handler == pkt->handler)
526 return fifo->rx_chan;
528 return NULL;
531 static struct usbhs_fifo *usbhsf_get_dma_fifo(struct usbhs_priv *priv,
532 struct usbhs_pkt *pkt)
534 struct usbhs_fifo *fifo;
536 /* DMA :: D0FIFO */
537 fifo = usbhsf_get_d0fifo(priv);
538 if (usbhsf_dma_chan_get(fifo, pkt) &&
539 !usbhsf_fifo_is_busy(fifo))
540 return fifo;
542 /* DMA :: D1FIFO */
543 fifo = usbhsf_get_d1fifo(priv);
544 if (usbhsf_dma_chan_get(fifo, pkt) &&
545 !usbhsf_fifo_is_busy(fifo))
546 return fifo;
548 return NULL;
551 #define usbhsf_dma_start(p, f) __usbhsf_dma_ctrl(p, f, DREQE)
552 #define usbhsf_dma_stop(p, f) __usbhsf_dma_ctrl(p, f, 0)
553 static void __usbhsf_dma_ctrl(struct usbhs_pipe *pipe,
554 struct usbhs_fifo *fifo,
555 u16 dreqe)
557 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
559 usbhs_bset(priv, fifo->sel, DREQE, dreqe);
562 #define usbhsf_dma_map(p) __usbhsf_dma_map_ctrl(p, 1)
563 #define usbhsf_dma_unmap(p) __usbhsf_dma_map_ctrl(p, 0)
564 static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
566 struct usbhs_pipe *pipe = pkt->pipe;
567 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
568 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
570 return info->dma_map_ctrl(pkt, map);
573 static void usbhsf_dma_complete(void *arg);
574 static void usbhsf_dma_prepare_tasklet(unsigned long data)
576 struct usbhs_pkt *pkt = (struct usbhs_pkt *)data;
577 struct usbhs_pipe *pipe = pkt->pipe;
578 struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
579 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
580 struct scatterlist sg;
581 struct dma_async_tx_descriptor *desc;
582 struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
583 struct device *dev = usbhs_priv_to_dev(priv);
584 enum dma_data_direction dir;
585 dma_cookie_t cookie;
587 dir = usbhs_pipe_is_dir_in(pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
589 sg_init_table(&sg, 1);
590 sg_set_page(&sg, virt_to_page(pkt->dma),
591 pkt->length, offset_in_page(pkt->dma));
592 sg_dma_address(&sg) = pkt->dma + pkt->actual;
593 sg_dma_len(&sg) = pkt->trans;
595 desc = chan->device->device_prep_slave_sg(chan, &sg, 1, dir,
596 DMA_PREP_INTERRUPT |
597 DMA_CTRL_ACK);
598 if (!desc)
599 return;
601 desc->callback = usbhsf_dma_complete;
602 desc->callback_param = pipe;
604 cookie = desc->tx_submit(desc);
605 if (cookie < 0) {
606 dev_err(dev, "Failed to submit dma descriptor\n");
607 return;
610 dev_dbg(dev, " %s %d (%d/ %d)\n",
611 fifo->name, usbhs_pipe_number(pipe), pkt->length, pkt->zero);
613 usbhsf_dma_start(pipe, fifo);
614 dma_async_issue_pending(chan);
617 static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done)
619 struct usbhs_pipe *pipe = pkt->pipe;
620 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
621 struct usbhs_fifo *fifo;
622 int len = pkt->length - pkt->actual;
623 int ret;
625 if (usbhs_pipe_is_busy(pipe))
626 return 0;
628 /* use PIO if packet is less than pio_dma_border or pipe is DCP */
629 if ((len < usbhs_get_dparam(priv, pio_dma_border)) ||
630 usbhs_pipe_is_dcp(pipe))
631 goto usbhsf_pio_prepare_push;
633 if (len % 4) /* 32bit alignment */
634 goto usbhsf_pio_prepare_push;
636 /* get enable DMA fifo */
637 fifo = usbhsf_get_dma_fifo(priv, pkt);
638 if (!fifo)
639 goto usbhsf_pio_prepare_push;
641 if (usbhsf_dma_map(pkt) < 0)
642 goto usbhsf_pio_prepare_push;
644 ret = usbhsf_fifo_select(pipe, fifo, 0);
645 if (ret < 0)
646 goto usbhsf_pio_prepare_push_unmap;
648 pkt->trans = len;
650 tasklet_init(&fifo->tasklet,
651 usbhsf_dma_prepare_tasklet,
652 (unsigned long)pkt);
654 tasklet_schedule(&fifo->tasklet);
656 return 0;
658 usbhsf_pio_prepare_push_unmap:
659 usbhsf_dma_unmap(pkt);
660 usbhsf_pio_prepare_push:
662 * change handler to PIO
664 pkt->handler = &usbhs_fifo_pio_push_handler;
666 return pkt->handler->prepare(pkt, is_done);
669 static int usbhsf_dma_push_done(struct usbhs_pkt *pkt, int *is_done)
671 struct usbhs_pipe *pipe = pkt->pipe;
673 pkt->actual = pkt->trans;
675 *is_done = !pkt->zero; /* send zero packet ? */
677 usbhsf_dma_stop(pipe, pipe->fifo);
678 usbhsf_dma_unmap(pkt);
679 usbhsf_fifo_unselect(pipe, pipe->fifo);
681 return 0;
684 struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = {
685 .prepare = usbhsf_dma_prepare_push,
686 .dma_done = usbhsf_dma_push_done,
689 static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done)
691 struct usbhs_pipe *pipe = pkt->pipe;
692 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
693 struct usbhs_fifo *fifo;
694 int len, ret;
696 if (usbhs_pipe_is_busy(pipe))
697 return 0;
699 if (usbhs_pipe_is_dcp(pipe))
700 goto usbhsf_pio_prepare_pop;
702 /* get enable DMA fifo */
703 fifo = usbhsf_get_dma_fifo(priv, pkt);
704 if (!fifo)
705 goto usbhsf_pio_prepare_pop;
707 ret = usbhsf_fifo_select(pipe, fifo, 0);
708 if (ret < 0)
709 goto usbhsf_pio_prepare_pop;
711 /* use PIO if packet is less than pio_dma_border */
712 len = usbhsf_fifo_rcv_len(priv, fifo);
713 len = min(pkt->length - pkt->actual, len);
714 if (len % 4) /* 32bit alignment */
715 goto usbhsf_pio_prepare_pop_unselect;
717 if (len < usbhs_get_dparam(priv, pio_dma_border))
718 goto usbhsf_pio_prepare_pop_unselect;
720 ret = usbhsf_fifo_barrier(priv, fifo);
721 if (ret < 0)
722 goto usbhsf_pio_prepare_pop_unselect;
724 if (usbhsf_dma_map(pkt) < 0)
725 goto usbhsf_pio_prepare_pop_unselect;
727 /* DMA */
730 * usbhs_fifo_dma_pop_handler :: prepare
731 * enabled irq to come here.
732 * but it is no longer needed for DMA. disable it.
734 usbhsf_rx_irq_ctrl(pipe, 0);
736 pkt->trans = len;
738 tasklet_init(&fifo->tasklet,
739 usbhsf_dma_prepare_tasklet,
740 (unsigned long)pkt);
742 tasklet_schedule(&fifo->tasklet);
744 return 0;
746 usbhsf_pio_prepare_pop_unselect:
747 usbhsf_fifo_unselect(pipe, fifo);
748 usbhsf_pio_prepare_pop:
751 * change handler to PIO
753 pkt->handler = &usbhs_fifo_pio_pop_handler;
755 return pkt->handler->try_run(pkt, is_done);
758 static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done)
760 struct usbhs_pipe *pipe = pkt->pipe;
761 int maxp = usbhs_pipe_get_maxpacket(pipe);
763 usbhsf_dma_stop(pipe, pipe->fifo);
764 usbhsf_dma_unmap(pkt);
765 usbhsf_fifo_unselect(pipe, pipe->fifo);
767 pkt->actual += pkt->trans;
769 if ((pkt->actual == pkt->length) || /* receive all data */
770 (pkt->trans < maxp)) { /* short packet */
771 *is_done = 1;
772 } else {
773 /* re-enable */
774 usbhsf_prepare_pop(pkt, is_done);
777 return 0;
780 struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = {
781 .prepare = usbhsf_prepare_pop,
782 .try_run = usbhsf_dma_try_pop,
783 .dma_done = usbhsf_dma_pop_done
787 * DMA setting
789 static bool usbhsf_dma_filter(struct dma_chan *chan, void *param)
791 struct sh_dmae_slave *slave = param;
794 * FIXME
796 * usbhs doesn't recognize id = 0 as valid DMA
798 if (0 == slave->slave_id)
799 return false;
801 chan->private = slave;
803 return true;
806 static void usbhsf_dma_quit(struct usbhs_priv *priv, struct usbhs_fifo *fifo)
808 if (fifo->tx_chan)
809 dma_release_channel(fifo->tx_chan);
810 if (fifo->rx_chan)
811 dma_release_channel(fifo->rx_chan);
813 fifo->tx_chan = NULL;
814 fifo->rx_chan = NULL;
817 static void usbhsf_dma_init(struct usbhs_priv *priv,
818 struct usbhs_fifo *fifo)
820 struct device *dev = usbhs_priv_to_dev(priv);
821 dma_cap_mask_t mask;
823 dma_cap_zero(mask);
824 dma_cap_set(DMA_SLAVE, mask);
825 fifo->tx_chan = dma_request_channel(mask, usbhsf_dma_filter,
826 &fifo->tx_slave);
828 dma_cap_zero(mask);
829 dma_cap_set(DMA_SLAVE, mask);
830 fifo->rx_chan = dma_request_channel(mask, usbhsf_dma_filter,
831 &fifo->rx_slave);
833 if (fifo->tx_chan || fifo->rx_chan)
834 dev_dbg(dev, "enable DMAEngine (%s%s%s)\n",
835 fifo->name,
836 fifo->tx_chan ? "[TX]" : " ",
837 fifo->rx_chan ? "[RX]" : " ");
841 * irq functions
843 static int usbhsf_irq_empty(struct usbhs_priv *priv,
844 struct usbhs_irq_state *irq_state)
846 struct usbhs_pipe *pipe;
847 struct device *dev = usbhs_priv_to_dev(priv);
848 int i, ret;
850 if (!irq_state->bempsts) {
851 dev_err(dev, "debug %s !!\n", __func__);
852 return -EIO;
855 dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
858 * search interrupted "pipe"
859 * not "uep".
861 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
862 if (!(irq_state->bempsts & (1 << i)))
863 continue;
865 ret = usbhs_pkt_run(pipe);
866 if (ret < 0)
867 dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
870 return 0;
873 static int usbhsf_irq_ready(struct usbhs_priv *priv,
874 struct usbhs_irq_state *irq_state)
876 struct usbhs_pipe *pipe;
877 struct device *dev = usbhs_priv_to_dev(priv);
878 int i, ret;
880 if (!irq_state->brdysts) {
881 dev_err(dev, "debug %s !!\n", __func__);
882 return -EIO;
885 dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
888 * search interrupted "pipe"
889 * not "uep".
891 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
892 if (!(irq_state->brdysts & (1 << i)))
893 continue;
895 ret = usbhs_pkt_run(pipe);
896 if (ret < 0)
897 dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
900 return 0;
903 static void usbhsf_dma_complete(void *arg)
905 struct usbhs_pipe *pipe = arg;
906 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
907 struct device *dev = usbhs_priv_to_dev(priv);
908 int ret;
910 ret = usbhs_pkt_dmadone(pipe);
911 if (ret < 0)
912 dev_err(dev, "dma_complete run_error %d : %d\n",
913 usbhs_pipe_number(pipe), ret);
917 * fifo init
919 void usbhs_fifo_init(struct usbhs_priv *priv)
921 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
922 struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
923 struct usbhs_fifo *d0fifo = usbhsf_get_d0fifo(priv);
924 struct usbhs_fifo *d1fifo = usbhsf_get_d1fifo(priv);
926 mod->irq_empty = usbhsf_irq_empty;
927 mod->irq_ready = usbhsf_irq_ready;
928 mod->irq_bempsts = 0;
929 mod->irq_brdysts = 0;
931 cfifo->pipe = NULL;
932 cfifo->tx_chan = NULL;
933 cfifo->rx_chan = NULL;
935 d0fifo->pipe = NULL;
936 d0fifo->tx_chan = NULL;
937 d0fifo->rx_chan = NULL;
939 d1fifo->pipe = NULL;
940 d1fifo->tx_chan = NULL;
941 d1fifo->rx_chan = NULL;
943 usbhsf_dma_init(priv, usbhsf_get_d0fifo(priv));
944 usbhsf_dma_init(priv, usbhsf_get_d1fifo(priv));
947 void usbhs_fifo_quit(struct usbhs_priv *priv)
949 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
951 mod->irq_empty = NULL;
952 mod->irq_ready = NULL;
953 mod->irq_bempsts = 0;
954 mod->irq_brdysts = 0;
956 usbhsf_dma_quit(priv, usbhsf_get_d0fifo(priv));
957 usbhsf_dma_quit(priv, usbhsf_get_d1fifo(priv));
960 int usbhs_fifo_probe(struct usbhs_priv *priv)
962 struct usbhs_fifo *fifo;
964 /* CFIFO */
965 fifo = usbhsf_get_cfifo(priv);
966 fifo->name = "CFIFO";
967 fifo->port = CFIFO;
968 fifo->sel = CFIFOSEL;
969 fifo->ctr = CFIFOCTR;
971 /* D0FIFO */
972 fifo = usbhsf_get_d0fifo(priv);
973 fifo->name = "D0FIFO";
974 fifo->port = D0FIFO;
975 fifo->sel = D0FIFOSEL;
976 fifo->ctr = D0FIFOCTR;
977 fifo->tx_slave.slave_id = usbhs_get_dparam(priv, d0_tx_id);
978 fifo->rx_slave.slave_id = usbhs_get_dparam(priv, d0_rx_id);
980 /* D1FIFO */
981 fifo = usbhsf_get_d1fifo(priv);
982 fifo->name = "D1FIFO";
983 fifo->port = D1FIFO;
984 fifo->sel = D1FIFOSEL;
985 fifo->ctr = D1FIFOCTR;
986 fifo->tx_slave.slave_id = usbhs_get_dparam(priv, d1_tx_id);
987 fifo->rx_slave.slave_id = usbhs_get_dparam(priv, d1_rx_id);
989 return 0;
992 void usbhs_fifo_remove(struct usbhs_priv *priv)