usb: renesas_usbhs: add usbhsf_fifo
[zen-stable.git] / drivers / usb / renesas_usbhs / fifo.c
blob53e2b35dd3259c3af7fb14f77bc2c86c867f0373
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))
25 * packet info function
27 static int usbhsf_null_handle(struct usbhs_pkt *pkt, int *is_done)
29 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
30 struct device *dev = usbhs_priv_to_dev(priv);
32 dev_err(dev, "null handler\n");
34 return -EINVAL;
37 static struct usbhs_pkt_handle usbhsf_null_handler = {
38 .prepare = usbhsf_null_handle,
39 .try_run = usbhsf_null_handle,
42 void usbhs_pkt_init(struct usbhs_pkt *pkt)
44 INIT_LIST_HEAD(&pkt->node);
47 void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt,
48 struct usbhs_pkt_handle *handler,
49 void *buf, int len, int zero)
51 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
52 struct device *dev = usbhs_priv_to_dev(priv);
53 unsigned long flags;
55 /******************** spin lock ********************/
56 usbhs_lock(priv, flags);
58 if (!handler) {
59 dev_err(dev, "no handler function\n");
60 handler = &usbhsf_null_handler;
63 list_del_init(&pkt->node);
64 list_add_tail(&pkt->node, &pipe->list);
66 pkt->pipe = pipe;
67 pkt->buf = buf;
68 pkt->handler = handler;
69 pkt->length = len;
70 pkt->zero = zero;
71 pkt->actual = 0;
73 usbhs_unlock(priv, flags);
74 /******************** spin unlock ******************/
77 static void __usbhsf_pkt_del(struct usbhs_pkt *pkt)
79 list_del_init(&pkt->node);
82 static struct usbhs_pkt *__usbhsf_pkt_get(struct usbhs_pipe *pipe)
84 if (list_empty(&pipe->list))
85 return NULL;
87 return list_entry(pipe->list.next, struct usbhs_pkt, node);
90 struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt)
92 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
93 unsigned long flags;
95 /******************** spin lock ********************/
96 usbhs_lock(priv, flags);
98 if (!pkt)
99 pkt = __usbhsf_pkt_get(pipe);
101 if (pkt)
102 __usbhsf_pkt_del(pkt);
104 usbhs_unlock(priv, flags);
105 /******************** spin unlock ******************/
107 return pkt;
110 int __usbhs_pkt_handler(struct usbhs_pipe *pipe, int type)
112 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
113 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
114 struct usbhs_pkt *pkt;
115 struct device *dev = usbhs_priv_to_dev(priv);
116 int (*func)(struct usbhs_pkt *pkt, int *is_done);
117 unsigned long flags;
118 int ret = 0;
119 int is_done = 0;
121 /******************** spin lock ********************/
122 usbhs_lock(priv, flags);
124 pkt = __usbhsf_pkt_get(pipe);
125 if (!pkt)
126 goto __usbhs_pkt_handler_end;
128 switch (type) {
129 case USBHSF_PKT_PREPARE:
130 func = pkt->handler->prepare;
131 break;
132 case USBHSF_PKT_TRY_RUN:
133 func = pkt->handler->try_run;
134 break;
135 default:
136 dev_err(dev, "unknown pkt hander\n");
137 goto __usbhs_pkt_handler_end;
140 ret = func(pkt, &is_done);
142 if (is_done)
143 __usbhsf_pkt_del(pkt);
145 __usbhs_pkt_handler_end:
146 usbhs_unlock(priv, flags);
147 /******************** spin unlock ******************/
149 if (is_done)
150 info->done(pkt);
152 return ret;
156 * irq enable/disable function
158 #define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, bempsts, e)
159 #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, brdysts, e)
160 #define usbhsf_irq_callback_ctrl(pipe, status, enable) \
161 ({ \
162 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
163 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
164 u16 status = (1 << usbhs_pipe_number(pipe)); \
165 if (!mod) \
166 return; \
167 if (enable) \
168 mod->irq_##status |= status; \
169 else \
170 mod->irq_##status &= ~status; \
171 usbhs_irq_callback_update(priv, mod); \
174 static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
177 * And DCP pipe can NOT use "ready interrupt" for "send"
178 * it should use "empty" interrupt.
179 * see
180 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
182 * on the other hand, normal pipe can use "ready interrupt" for "send"
183 * even though it is single/double buffer
185 if (usbhs_pipe_is_dcp(pipe))
186 usbhsf_irq_empty_ctrl(pipe, enable);
187 else
188 usbhsf_irq_ready_ctrl(pipe, enable);
191 static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
193 usbhsf_irq_ready_ctrl(pipe, enable);
197 * FIFO ctrl
199 static void usbhsf_send_terminator(struct usbhs_pipe *pipe,
200 struct usbhs_fifo *fifo)
202 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
204 usbhs_bset(priv, fifo->ctr, BVAL, BVAL);
207 static int usbhsf_fifo_barrier(struct usbhs_priv *priv,
208 struct usbhs_fifo *fifo)
210 int timeout = 1024;
212 do {
213 /* The FIFO port is accessible */
214 if (usbhs_read(priv, fifo->ctr) & FRDY)
215 return 0;
217 udelay(10);
218 } while (timeout--);
220 return -EBUSY;
223 static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
224 struct usbhs_fifo *fifo)
226 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
228 if (!usbhs_pipe_is_dcp(pipe))
229 usbhsf_fifo_barrier(priv, fifo);
231 usbhs_write(priv, fifo->ctr, BCLR);
234 static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv,
235 struct usbhs_fifo *fifo)
237 return usbhs_read(priv, fifo->ctr) & DTLN_MASK;
240 static int usbhsf_fifo_select(struct usbhs_pipe *pipe,
241 struct usbhs_fifo *fifo,
242 int write)
244 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
245 struct device *dev = usbhs_priv_to_dev(priv);
246 int timeout = 1024;
247 u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
248 u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
250 if (usbhs_pipe_is_dcp(pipe))
251 base |= (1 == write) << 5; /* ISEL */
253 /* "base" will be used below */
254 usbhs_write(priv, fifo->sel, base | MBW_32);
256 /* check ISEL and CURPIPE value */
257 while (timeout--) {
258 if (base == (mask & usbhs_read(priv, fifo->sel)))
259 return 0;
260 udelay(10);
263 dev_err(dev, "fifo select error\n");
265 return -EIO;
269 * PIO fifo functions
271 static int usbhsf_try_push(struct usbhs_pkt *pkt, int *is_done)
273 struct usbhs_pipe *pipe = pkt->pipe;
274 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
275 struct device *dev = usbhs_priv_to_dev(priv);
276 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
277 void __iomem *addr = priv->base + fifo->port;
278 u8 *buf;
279 int maxp = usbhs_pipe_get_maxpacket(pipe);
280 int total_len;
281 int i, ret, len;
282 int is_short;
284 ret = usbhsf_fifo_select(pipe, fifo, 1);
285 if (ret < 0)
286 goto usbhs_fifo_write_busy;
288 ret = usbhs_pipe_is_accessible(pipe);
289 if (ret < 0)
290 goto usbhs_fifo_write_busy;
292 ret = usbhsf_fifo_barrier(priv, fifo);
293 if (ret < 0)
294 goto usbhs_fifo_write_busy;
296 buf = pkt->buf + pkt->actual;
297 len = pkt->length - pkt->actual;
298 len = min(len, maxp);
299 total_len = len;
300 is_short = total_len < maxp;
303 * FIXME
305 * 32-bit access only
307 if (len >= 4 && !((unsigned long)buf & 0x03)) {
308 iowrite32_rep(addr, buf, len / 4);
309 len %= 4;
310 buf += total_len - len;
313 /* the rest operation */
314 for (i = 0; i < len; i++)
315 iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
318 * variable update
320 pkt->actual += total_len;
322 if (pkt->actual < pkt->length)
323 *is_done = 0; /* there are remainder data */
324 else if (is_short)
325 *is_done = 1; /* short packet */
326 else
327 *is_done = !pkt->zero; /* send zero packet ? */
330 * pipe/irq handling
332 if (is_short)
333 usbhsf_send_terminator(pipe, fifo);
335 usbhsf_tx_irq_ctrl(pipe, !*is_done);
336 usbhs_pipe_enable(pipe);
338 dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
339 usbhs_pipe_number(pipe),
340 pkt->length, pkt->actual, *is_done, pkt->zero);
343 * Transmission end
345 if (*is_done) {
346 if (usbhs_pipe_is_dcp(pipe))
347 usbhs_dcp_control_transfer_done(pipe);
350 return 0;
352 usbhs_fifo_write_busy:
354 * pipe is busy.
355 * retry in interrupt
357 usbhsf_tx_irq_ctrl(pipe, 1);
359 return ret;
362 struct usbhs_pkt_handle usbhs_fifo_push_handler = {
363 .prepare = usbhsf_try_push,
364 .try_run = usbhsf_try_push,
367 static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
369 struct usbhs_pipe *pipe = pkt->pipe;
370 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
371 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
372 int ret;
375 * select pipe and enable it to prepare packet receive
377 ret = usbhsf_fifo_select(pipe, fifo, 0);
378 if (ret < 0)
379 return ret;
381 usbhs_pipe_enable(pipe);
382 usbhsf_rx_irq_ctrl(pipe, 1);
384 return 0;
387 static int usbhsf_try_pop(struct usbhs_pkt *pkt, int *is_done)
389 struct usbhs_pipe *pipe = pkt->pipe;
390 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
391 struct device *dev = usbhs_priv_to_dev(priv);
392 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
393 void __iomem *addr = priv->base + fifo->port;
394 u8 *buf;
395 u32 data = 0;
396 int maxp = usbhs_pipe_get_maxpacket(pipe);
397 int rcv_len, len;
398 int i, ret;
399 int total_len = 0;
401 ret = usbhsf_fifo_select(pipe, fifo, 0);
402 if (ret < 0)
403 return ret;
405 ret = usbhsf_fifo_barrier(priv, fifo);
406 if (ret < 0)
407 return ret;
409 rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
411 buf = pkt->buf + pkt->actual;
412 len = pkt->length - pkt->actual;
413 len = min(len, rcv_len);
414 total_len = len;
417 * Buffer clear if Zero-Length packet
419 * see
420 * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
422 if (0 == rcv_len) {
423 usbhsf_fifo_clear(pipe, fifo);
424 goto usbhs_fifo_read_end;
428 * FIXME
430 * 32-bit access only
432 if (len >= 4 && !((unsigned long)buf & 0x03)) {
433 ioread32_rep(addr, buf, len / 4);
434 len %= 4;
435 buf += total_len - len;
438 /* the rest operation */
439 for (i = 0; i < len; i++) {
440 if (!(i & 0x03))
441 data = ioread32(addr);
443 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
446 pkt->actual += total_len;
448 usbhs_fifo_read_end:
449 if ((pkt->actual == pkt->length) || /* receive all data */
450 (total_len < maxp)) { /* short packet */
451 *is_done = 1;
452 usbhsf_rx_irq_ctrl(pipe, 0);
453 usbhs_pipe_disable(pipe);
456 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
457 usbhs_pipe_number(pipe),
458 pkt->length, pkt->actual, *is_done, pkt->zero);
460 return 0;
463 struct usbhs_pkt_handle usbhs_fifo_pop_handler = {
464 .prepare = usbhsf_prepare_pop,
465 .try_run = usbhsf_try_pop,
469 * handler function
471 static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
473 usbhs_dcp_control_transfer_done(pkt->pipe);
475 *is_done = 1;
477 return 0;
480 struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
481 .prepare = usbhsf_ctrl_stage_end,
482 .try_run = usbhsf_ctrl_stage_end,
486 * irq functions
488 static int usbhsf_irq_empty(struct usbhs_priv *priv,
489 struct usbhs_irq_state *irq_state)
491 struct usbhs_pipe *pipe;
492 struct device *dev = usbhs_priv_to_dev(priv);
493 int i, ret;
495 if (!irq_state->bempsts) {
496 dev_err(dev, "debug %s !!\n", __func__);
497 return -EIO;
500 dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
503 * search interrupted "pipe"
504 * not "uep".
506 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
507 if (!(irq_state->bempsts & (1 << i)))
508 continue;
510 ret = usbhs_pkt_run(pipe);
511 if (ret < 0)
512 dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
515 return 0;
518 static int usbhsf_irq_ready(struct usbhs_priv *priv,
519 struct usbhs_irq_state *irq_state)
521 struct usbhs_pipe *pipe;
522 struct device *dev = usbhs_priv_to_dev(priv);
523 int i, ret;
525 if (!irq_state->brdysts) {
526 dev_err(dev, "debug %s !!\n", __func__);
527 return -EIO;
530 dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
533 * search interrupted "pipe"
534 * not "uep".
536 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
537 if (!(irq_state->brdysts & (1 << i)))
538 continue;
540 ret = usbhs_pkt_run(pipe);
541 if (ret < 0)
542 dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
545 return 0;
549 * fifo init
551 void usbhs_fifo_init(struct usbhs_priv *priv)
553 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
555 mod->irq_empty = usbhsf_irq_empty;
556 mod->irq_ready = usbhsf_irq_ready;
557 mod->irq_bempsts = 0;
558 mod->irq_brdysts = 0;
561 void usbhs_fifo_quit(struct usbhs_priv *priv)
563 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
565 mod->irq_empty = NULL;
566 mod->irq_ready = NULL;
567 mod->irq_bempsts = 0;
568 mod->irq_brdysts = 0;
571 int usbhs_fifo_probe(struct usbhs_priv *priv)
573 struct usbhs_fifo *fifo;
575 /* CFIFO */
576 fifo = usbhsf_get_cfifo(priv);
577 fifo->port = CFIFO;
578 fifo->sel = CFIFOSEL;
579 fifo->ctr = CFIFOCTR;
581 return 0;
584 void usbhs_fifo_remove(struct usbhs_priv *priv)