blob: 03a9cc529c820b3f6ec26c8ef1187c0fa1bbd70d [file] [log] [blame]
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +09001/*
2 * Renesas USB driver
3 *
4 * Copyright (C) 2011 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
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.
11 *
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
15 *
16 */
17#include <linux/delay.h>
18#include <linux/io.h>
Rafael J. Wysocki9c646cf2011-07-26 20:51:01 +020019#include <linux/scatterlist.h>
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +090020#include "./common.h"
21#include "./pipe.h"
22
Kuninori Morimotod3af90a2011-06-06 14:18:44 +090023#define usbhsf_get_cfifo(p) (&((p)->fifo_info.cfifo))
Kuninori Morimotoe73a9892011-06-06 14:19:03 +090024#define usbhsf_get_d0fifo(p) (&((p)->fifo_info.d0fifo))
25#define usbhsf_get_d1fifo(p) (&((p)->fifo_info.d1fifo))
Shimoda, Yoshihiro5ea43992012-01-05 15:37:22 +090026#define usbhsf_is_cfifo(p, f) (usbhsf_get_cfifo(p) == f)
Kuninori Morimotod3af90a2011-06-06 14:18:44 +090027
Kuninori Morimotod77e3f42011-06-06 14:18:50 +090028#define usbhsf_fifo_is_busy(f) ((f)->pipe) /* see usbhs_pipe_select_fifo */
29
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +090030/*
Kuninori Morimoto233f5192011-07-07 00:23:24 -070031 * packet initialize
32 */
33void usbhs_pkt_init(struct usbhs_pkt *pkt)
34{
35 pkt->dma = DMA_ADDR_INVALID;
36 INIT_LIST_HEAD(&pkt->node);
37}
38
39/*
40 * packet control function
Kuninori Morimoto4bd04812011-06-06 14:18:07 +090041 */
Kuninori Morimoto97664a22011-06-06 14:18:38 +090042static int usbhsf_null_handle(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotodad67392011-06-06 14:18:28 +090043{
44 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
45 struct device *dev = usbhs_priv_to_dev(priv);
46
47 dev_err(dev, "null handler\n");
48
49 return -EINVAL;
50}
51
52static struct usbhs_pkt_handle usbhsf_null_handler = {
53 .prepare = usbhsf_null_handle,
54 .try_run = usbhsf_null_handle,
55};
56
Kuninori Morimoto659d4952011-06-06 14:18:23 +090057void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt,
Kuninori Morimotob3318722011-10-10 22:04:41 -070058 void (*done)(struct usbhs_priv *priv,
59 struct usbhs_pkt *pkt),
Kuninori Morimoto3edeee32011-12-08 18:28:54 -080060 void *buf, int len, int zero, int sequence)
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090061{
Kuninori Morimotodad67392011-06-06 14:18:28 +090062 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
63 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto97664a22011-06-06 14:18:38 +090064 unsigned long flags;
65
Kuninori Morimotob3318722011-10-10 22:04:41 -070066 if (!done) {
67 dev_err(dev, "no done function\n");
68 return;
69 }
70
Kuninori Morimotoa2c76b82011-10-18 20:05:50 -070071 /******************** spin lock ********************/
72 usbhs_lock(priv, flags);
73
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -070074 if (!pipe->handler) {
Kuninori Morimotodad67392011-06-06 14:18:28 +090075 dev_err(dev, "no handler function\n");
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -070076 pipe->handler = &usbhsf_null_handler;
Kuninori Morimotodad67392011-06-06 14:18:28 +090077 }
78
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090079 list_del_init(&pkt->node);
80 list_add_tail(&pkt->node, &pipe->list);
81
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -070082 /*
83 * each pkt must hold own handler.
84 * because handler might be changed by its situation.
85 * dma handler -> pio handler.
86 */
Kuninori Morimoto659d4952011-06-06 14:18:23 +090087 pkt->pipe = pipe;
88 pkt->buf = buf;
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -070089 pkt->handler = pipe->handler;
Kuninori Morimoto659d4952011-06-06 14:18:23 +090090 pkt->length = len;
91 pkt->zero = zero;
92 pkt->actual = 0;
Kuninori Morimotob3318722011-10-10 22:04:41 -070093 pkt->done = done;
Kuninori Morimoto3edeee32011-12-08 18:28:54 -080094 pkt->sequence = sequence;
Kuninori Morimoto97664a22011-06-06 14:18:38 +090095
96 usbhs_unlock(priv, flags);
97 /******************** spin unlock ******************/
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090098}
99
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900100static void __usbhsf_pkt_del(struct usbhs_pkt *pkt)
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900101{
102 list_del_init(&pkt->node);
103}
104
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900105static struct usbhs_pkt *__usbhsf_pkt_get(struct usbhs_pipe *pipe)
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900106{
107 if (list_empty(&pipe->list))
108 return NULL;
109
110 return list_entry(pipe->list.next, struct usbhs_pkt, node);
111}
112
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900113struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt)
114{
115 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
116 unsigned long flags;
117
118 /******************** spin lock ********************/
119 usbhs_lock(priv, flags);
120
121 if (!pkt)
122 pkt = __usbhsf_pkt_get(pipe);
123
124 if (pkt)
125 __usbhsf_pkt_del(pkt);
126
127 usbhs_unlock(priv, flags);
128 /******************** spin unlock ******************/
129
130 return pkt;
131}
132
Kuninori Morimoto51b8a022011-10-10 21:58:45 -0700133enum {
134 USBHSF_PKT_PREPARE,
135 USBHSF_PKT_TRY_RUN,
136 USBHSF_PKT_DMA_DONE,
137};
138
139static int usbhsf_pkt_handler(struct usbhs_pipe *pipe, int type)
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900140{
141 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900142 struct usbhs_pkt *pkt;
143 struct device *dev = usbhs_priv_to_dev(priv);
144 int (*func)(struct usbhs_pkt *pkt, int *is_done);
145 unsigned long flags;
146 int ret = 0;
147 int is_done = 0;
148
149 /******************** spin lock ********************/
150 usbhs_lock(priv, flags);
151
152 pkt = __usbhsf_pkt_get(pipe);
153 if (!pkt)
154 goto __usbhs_pkt_handler_end;
155
156 switch (type) {
157 case USBHSF_PKT_PREPARE:
158 func = pkt->handler->prepare;
159 break;
160 case USBHSF_PKT_TRY_RUN:
161 func = pkt->handler->try_run;
162 break;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900163 case USBHSF_PKT_DMA_DONE:
164 func = pkt->handler->dma_done;
165 break;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900166 default:
167 dev_err(dev, "unknown pkt hander\n");
168 goto __usbhs_pkt_handler_end;
169 }
170
171 ret = func(pkt, &is_done);
172
173 if (is_done)
174 __usbhsf_pkt_del(pkt);
175
176__usbhs_pkt_handler_end:
177 usbhs_unlock(priv, flags);
178 /******************** spin unlock ******************/
179
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900180 if (is_done) {
Kuninori Morimotob3318722011-10-10 22:04:41 -0700181 pkt->done(priv, pkt);
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900182 usbhs_pkt_start(pipe);
183 }
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900184
185 return ret;
186}
187
Kuninori Morimoto51b8a022011-10-10 21:58:45 -0700188void usbhs_pkt_start(struct usbhs_pipe *pipe)
189{
190 usbhsf_pkt_handler(pipe, USBHSF_PKT_PREPARE);
191}
192
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900193/*
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900194 * irq enable/disable function
195 */
196#define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, bempsts, e)
197#define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, brdysts, e)
198#define usbhsf_irq_callback_ctrl(pipe, status, enable) \
199 ({ \
200 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
201 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
202 u16 status = (1 << usbhs_pipe_number(pipe)); \
203 if (!mod) \
204 return; \
205 if (enable) \
206 mod->irq_##status |= status; \
207 else \
208 mod->irq_##status &= ~status; \
209 usbhs_irq_callback_update(priv, mod); \
210 })
211
212static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
213{
214 /*
215 * And DCP pipe can NOT use "ready interrupt" for "send"
216 * it should use "empty" interrupt.
217 * see
218 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
219 *
220 * on the other hand, normal pipe can use "ready interrupt" for "send"
221 * even though it is single/double buffer
222 */
223 if (usbhs_pipe_is_dcp(pipe))
224 usbhsf_irq_empty_ctrl(pipe, enable);
225 else
226 usbhsf_irq_ready_ctrl(pipe, enable);
227}
228
229static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
230{
231 usbhsf_irq_ready_ctrl(pipe, enable);
232}
233
234/*
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900235 * FIFO ctrl
236 */
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900237static void usbhsf_send_terminator(struct usbhs_pipe *pipe,
238 struct usbhs_fifo *fifo)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900239{
240 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
241
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900242 usbhs_bset(priv, fifo->ctr, BVAL, BVAL);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900243}
244
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900245static int usbhsf_fifo_barrier(struct usbhs_priv *priv,
246 struct usbhs_fifo *fifo)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900247{
248 int timeout = 1024;
249
250 do {
251 /* The FIFO port is accessible */
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900252 if (usbhs_read(priv, fifo->ctr) & FRDY)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900253 return 0;
254
255 udelay(10);
256 } while (timeout--);
257
258 return -EBUSY;
259}
260
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900261static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
262 struct usbhs_fifo *fifo)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900263{
264 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
265
266 if (!usbhs_pipe_is_dcp(pipe))
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900267 usbhsf_fifo_barrier(priv, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900268
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900269 usbhs_write(priv, fifo->ctr, BCLR);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900270}
271
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900272static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv,
273 struct usbhs_fifo *fifo)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900274{
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900275 return usbhs_read(priv, fifo->ctr) & DTLN_MASK;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900276}
277
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900278static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
279 struct usbhs_fifo *fifo)
280{
281 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
282
283 usbhs_pipe_select_fifo(pipe, NULL);
284 usbhs_write(priv, fifo->sel, 0);
285}
286
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900287static int usbhsf_fifo_select(struct usbhs_pipe *pipe,
288 struct usbhs_fifo *fifo,
289 int write)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900290{
291 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
292 struct device *dev = usbhs_priv_to_dev(priv);
293 int timeout = 1024;
294 u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
295 u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
296
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900297 if (usbhs_pipe_is_busy(pipe) ||
298 usbhsf_fifo_is_busy(fifo))
299 return -EBUSY;
300
Kuninori Morimoto92352072011-10-10 22:02:57 -0700301 if (usbhs_pipe_is_dcp(pipe)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900302 base |= (1 == write) << 5; /* ISEL */
303
Kuninori Morimoto92352072011-10-10 22:02:57 -0700304 if (usbhs_mod_is_host(priv))
305 usbhs_dcp_dir_for_host(pipe, write);
306 }
307
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900308 /* "base" will be used below */
Shimoda, Yoshihiro5ea43992012-01-05 15:37:22 +0900309 if (usbhs_get_dparam(priv, has_sudmac) && !usbhsf_is_cfifo(priv, fifo))
310 usbhs_write(priv, fifo->sel, base);
311 else
312 usbhs_write(priv, fifo->sel, base | MBW_32);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900313
314 /* check ISEL and CURPIPE value */
315 while (timeout--) {
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900316 if (base == (mask & usbhs_read(priv, fifo->sel))) {
317 usbhs_pipe_select_fifo(pipe, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900318 return 0;
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900319 }
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900320 udelay(10);
321 }
322
323 dev_err(dev, "fifo select error\n");
324
325 return -EIO;
326}
327
328/*
Kuninori Morimoto9e74d602011-10-10 22:07:08 -0700329 * DCP status stage
330 */
331static int usbhs_dcp_dir_switch_to_write(struct usbhs_pkt *pkt, int *is_done)
332{
333 struct usbhs_pipe *pipe = pkt->pipe;
334 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
335 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
336 struct device *dev = usbhs_priv_to_dev(priv);
337 int ret;
338
339 usbhs_pipe_disable(pipe);
340
341 ret = usbhsf_fifo_select(pipe, fifo, 1);
342 if (ret < 0) {
343 dev_err(dev, "%s() faile\n", __func__);
344 return ret;
345 }
346
347 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
348
349 usbhsf_fifo_clear(pipe, fifo);
350 usbhsf_send_terminator(pipe, fifo);
351
352 usbhsf_fifo_unselect(pipe, fifo);
353
354 usbhsf_tx_irq_ctrl(pipe, 1);
355 usbhs_pipe_enable(pipe);
356
357 return ret;
358}
359
360static int usbhs_dcp_dir_switch_to_read(struct usbhs_pkt *pkt, int *is_done)
361{
362 struct usbhs_pipe *pipe = pkt->pipe;
363 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
364 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
365 struct device *dev = usbhs_priv_to_dev(priv);
366 int ret;
367
368 usbhs_pipe_disable(pipe);
369
370 ret = usbhsf_fifo_select(pipe, fifo, 0);
371 if (ret < 0) {
372 dev_err(dev, "%s() fail\n", __func__);
373 return ret;
374 }
375
376 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
377 usbhsf_fifo_clear(pipe, fifo);
378
379 usbhsf_fifo_unselect(pipe, fifo);
380
381 usbhsf_rx_irq_ctrl(pipe, 1);
382 usbhs_pipe_enable(pipe);
383
384 return ret;
385
386}
387
388static int usbhs_dcp_dir_switch_done(struct usbhs_pkt *pkt, int *is_done)
389{
390 struct usbhs_pipe *pipe = pkt->pipe;
391
392 if (pkt->handler == &usbhs_dcp_status_stage_in_handler)
393 usbhsf_tx_irq_ctrl(pipe, 0);
394 else
395 usbhsf_rx_irq_ctrl(pipe, 0);
396
397 pkt->actual = pkt->length;
398 *is_done = 1;
399
400 return 0;
401}
402
403struct usbhs_pkt_handle usbhs_dcp_status_stage_in_handler = {
404 .prepare = usbhs_dcp_dir_switch_to_write,
405 .try_run = usbhs_dcp_dir_switch_done,
406};
407
408struct usbhs_pkt_handle usbhs_dcp_status_stage_out_handler = {
409 .prepare = usbhs_dcp_dir_switch_to_read,
410 .try_run = usbhs_dcp_dir_switch_done,
411};
412
413/*
414 * DCP data stage (push)
415 */
416static int usbhsf_dcp_data_stage_try_push(struct usbhs_pkt *pkt, int *is_done)
417{
418 struct usbhs_pipe *pipe = pkt->pipe;
419
420 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
421
422 /*
423 * change handler to PIO push
424 */
425 pkt->handler = &usbhs_fifo_pio_push_handler;
426
427 return pkt->handler->prepare(pkt, is_done);
428}
429
430struct usbhs_pkt_handle usbhs_dcp_data_stage_out_handler = {
431 .prepare = usbhsf_dcp_data_stage_try_push,
432};
433
434/*
435 * DCP data stage (pop)
436 */
437static int usbhsf_dcp_data_stage_prepare_pop(struct usbhs_pkt *pkt,
438 int *is_done)
439{
440 struct usbhs_pipe *pipe = pkt->pipe;
441 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
442 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
443
444 if (usbhs_pipe_is_busy(pipe))
445 return 0;
446
447 /*
448 * prepare pop for DCP should
449 * - change DCP direction,
450 * - clear fifo
451 * - DATA1
452 */
453 usbhs_pipe_disable(pipe);
454
455 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
456
457 usbhsf_fifo_select(pipe, fifo, 0);
458 usbhsf_fifo_clear(pipe, fifo);
459 usbhsf_fifo_unselect(pipe, fifo);
460
461 /*
462 * change handler to PIO pop
463 */
464 pkt->handler = &usbhs_fifo_pio_pop_handler;
465
466 return pkt->handler->prepare(pkt, is_done);
467}
468
469struct usbhs_pkt_handle usbhs_dcp_data_stage_in_handler = {
470 .prepare = usbhsf_dcp_data_stage_prepare_pop,
471};
472
473/*
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700474 * PIO push handler
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900475 */
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900476static int usbhsf_pio_try_push(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900477{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900478 struct usbhs_pipe *pipe = pkt->pipe;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900479 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900480 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900481 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
482 void __iomem *addr = priv->base + fifo->port;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900483 u8 *buf;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900484 int maxp = usbhs_pipe_get_maxpacket(pipe);
485 int total_len;
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900486 int i, ret, len;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900487 int is_short;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900488
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800489 usbhs_pipe_data_sequence(pipe, pkt->sequence);
490 pkt->sequence = -1; /* -1 sequence will be ignored */
491
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900492 ret = usbhsf_fifo_select(pipe, fifo, 1);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900493 if (ret < 0)
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900494 return 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900495
Kuninori Morimotodad67392011-06-06 14:18:28 +0900496 ret = usbhs_pipe_is_accessible(pipe);
Kuninori Morimoto4ef85e02011-07-03 17:42:47 -0700497 if (ret < 0) {
498 /* inaccessible pipe is not an error */
499 ret = 0;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900500 goto usbhs_fifo_write_busy;
Kuninori Morimoto4ef85e02011-07-03 17:42:47 -0700501 }
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900502
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900503 ret = usbhsf_fifo_barrier(priv, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900504 if (ret < 0)
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900505 goto usbhs_fifo_write_busy;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900506
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900507 buf = pkt->buf + pkt->actual;
508 len = pkt->length - pkt->actual;
509 len = min(len, maxp);
510 total_len = len;
511 is_short = total_len < maxp;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900512
513 /*
514 * FIXME
515 *
516 * 32-bit access only
517 */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900518 if (len >= 4 && !((unsigned long)buf & 0x03)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900519 iowrite32_rep(addr, buf, len / 4);
520 len %= 4;
521 buf += total_len - len;
522 }
523
524 /* the rest operation */
525 for (i = 0; i < len; i++)
526 iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
527
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900528 /*
529 * variable update
530 */
531 pkt->actual += total_len;
532
533 if (pkt->actual < pkt->length)
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900534 *is_done = 0; /* there are remainder data */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900535 else if (is_short)
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900536 *is_done = 1; /* short packet */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900537 else
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900538 *is_done = !pkt->zero; /* send zero packet ? */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900539
540 /*
541 * pipe/irq handling
542 */
543 if (is_short)
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900544 usbhsf_send_terminator(pipe, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900545
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900546 usbhsf_tx_irq_ctrl(pipe, !*is_done);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900547 usbhs_pipe_enable(pipe);
548
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900549 dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
550 usbhs_pipe_number(pipe),
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900551 pkt->length, pkt->actual, *is_done, pkt->zero);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900552
553 /*
554 * Transmission end
555 */
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900556 if (*is_done) {
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900557 if (usbhs_pipe_is_dcp(pipe))
558 usbhs_dcp_control_transfer_done(pipe);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900559 }
560
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900561 usbhsf_fifo_unselect(pipe, fifo);
562
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900563 return 0;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900564
565usbhs_fifo_write_busy:
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900566 usbhsf_fifo_unselect(pipe, fifo);
567
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900568 /*
569 * pipe is busy.
570 * retry in interrupt
571 */
572 usbhsf_tx_irq_ctrl(pipe, 1);
573
574 return ret;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900575}
576
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900577struct usbhs_pkt_handle usbhs_fifo_pio_push_handler = {
578 .prepare = usbhsf_pio_try_push,
579 .try_run = usbhsf_pio_try_push,
Kuninori Morimotodad67392011-06-06 14:18:28 +0900580};
581
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700582/*
583 * PIO pop handler
584 */
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900585static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900586{
Kuninori Morimotodad67392011-06-06 14:18:28 +0900587 struct usbhs_pipe *pipe = pkt->pipe;
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900588
589 if (usbhs_pipe_is_busy(pipe))
590 return 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900591
592 /*
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900593 * pipe enable to prepare packet receive
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900594 */
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800595 usbhs_pipe_data_sequence(pipe, pkt->sequence);
596 pkt->sequence = -1; /* -1 sequence will be ignored */
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900597
598 usbhs_pipe_enable(pipe);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900599 usbhsf_rx_irq_ctrl(pipe, 1);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900600
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900601 return 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900602}
603
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900604static int usbhsf_pio_try_pop(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900605{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900606 struct usbhs_pipe *pipe = pkt->pipe;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900607 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900608 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900609 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
610 void __iomem *addr = priv->base + fifo->port;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900611 u8 *buf;
612 u32 data = 0;
613 int maxp = usbhs_pipe_get_maxpacket(pipe);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900614 int rcv_len, len;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900615 int i, ret;
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900616 int total_len = 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900617
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900618 ret = usbhsf_fifo_select(pipe, fifo, 0);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900619 if (ret < 0)
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900620 return 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900621
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900622 ret = usbhsf_fifo_barrier(priv, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900623 if (ret < 0)
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900624 goto usbhs_fifo_read_busy;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900625
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900626 rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900627
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900628 buf = pkt->buf + pkt->actual;
629 len = pkt->length - pkt->actual;
630 len = min(len, rcv_len);
631 total_len = len;
632
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900633 /*
Kuninori Morimoto6ff5d092011-10-10 22:05:51 -0700634 * update actual length first here to decide disable pipe.
635 * if this pipe keeps BUF status and all data were popped,
636 * then, next interrupt/token will be issued again
637 */
638 pkt->actual += total_len;
639
640 if ((pkt->actual == pkt->length) || /* receive all data */
641 (total_len < maxp)) { /* short packet */
642 *is_done = 1;
643 usbhsf_rx_irq_ctrl(pipe, 0);
644 usbhs_pipe_disable(pipe); /* disable pipe first */
645 }
646
647 /*
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900648 * Buffer clear if Zero-Length packet
649 *
650 * see
651 * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
652 */
653 if (0 == rcv_len) {
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800654 pkt->zero = 1;
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900655 usbhsf_fifo_clear(pipe, fifo);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900656 goto usbhs_fifo_read_end;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900657 }
658
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900659 /*
660 * FIXME
661 *
662 * 32-bit access only
663 */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900664 if (len >= 4 && !((unsigned long)buf & 0x03)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900665 ioread32_rep(addr, buf, len / 4);
666 len %= 4;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900667 buf += total_len - len;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900668 }
669
670 /* the rest operation */
671 for (i = 0; i < len; i++) {
672 if (!(i & 0x03))
673 data = ioread32(addr);
674
675 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
676 }
677
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900678usbhs_fifo_read_end:
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900679 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
680 usbhs_pipe_number(pipe),
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900681 pkt->length, pkt->actual, *is_done, pkt->zero);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900682
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900683usbhs_fifo_read_busy:
684 usbhsf_fifo_unselect(pipe, fifo);
685
686 return ret;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900687}
Kuninori Morimotodad67392011-06-06 14:18:28 +0900688
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900689struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler = {
Kuninori Morimotodad67392011-06-06 14:18:28 +0900690 .prepare = usbhsf_prepare_pop,
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900691 .try_run = usbhsf_pio_try_pop,
Kuninori Morimotodad67392011-06-06 14:18:28 +0900692};
693
694/*
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700695 * DCP ctrol statge handler
Kuninori Morimotodad67392011-06-06 14:18:28 +0900696 */
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900697static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotodad67392011-06-06 14:18:28 +0900698{
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900699 usbhs_dcp_control_transfer_done(pkt->pipe);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900700
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900701 *is_done = 1;
Kuninori Morimotodad67392011-06-06 14:18:28 +0900702
703 return 0;
704}
705
706struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
707 .prepare = usbhsf_ctrl_stage_end,
708 .try_run = usbhsf_ctrl_stage_end,
709};
710
711/*
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900712 * DMA fifo functions
713 */
714static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
715 struct usbhs_pkt *pkt)
716{
717 if (&usbhs_fifo_dma_push_handler == pkt->handler)
718 return fifo->tx_chan;
719
720 if (&usbhs_fifo_dma_pop_handler == pkt->handler)
721 return fifo->rx_chan;
722
723 return NULL;
724}
725
726static struct usbhs_fifo *usbhsf_get_dma_fifo(struct usbhs_priv *priv,
727 struct usbhs_pkt *pkt)
728{
729 struct usbhs_fifo *fifo;
730
731 /* DMA :: D0FIFO */
732 fifo = usbhsf_get_d0fifo(priv);
733 if (usbhsf_dma_chan_get(fifo, pkt) &&
734 !usbhsf_fifo_is_busy(fifo))
735 return fifo;
736
737 /* DMA :: D1FIFO */
738 fifo = usbhsf_get_d1fifo(priv);
739 if (usbhsf_dma_chan_get(fifo, pkt) &&
740 !usbhsf_fifo_is_busy(fifo))
741 return fifo;
742
743 return NULL;
744}
745
746#define usbhsf_dma_start(p, f) __usbhsf_dma_ctrl(p, f, DREQE)
747#define usbhsf_dma_stop(p, f) __usbhsf_dma_ctrl(p, f, 0)
748static void __usbhsf_dma_ctrl(struct usbhs_pipe *pipe,
749 struct usbhs_fifo *fifo,
750 u16 dreqe)
751{
752 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
753
754 usbhs_bset(priv, fifo->sel, DREQE, dreqe);
755}
756
757#define usbhsf_dma_map(p) __usbhsf_dma_map_ctrl(p, 1)
758#define usbhsf_dma_unmap(p) __usbhsf_dma_map_ctrl(p, 0)
759static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
760{
761 struct usbhs_pipe *pipe = pkt->pipe;
762 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
763 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
764
765 return info->dma_map_ctrl(pkt, map);
766}
767
768static void usbhsf_dma_complete(void *arg);
769static void usbhsf_dma_prepare_tasklet(unsigned long data)
770{
771 struct usbhs_pkt *pkt = (struct usbhs_pkt *)data;
772 struct usbhs_pipe *pipe = pkt->pipe;
773 struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
774 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
775 struct scatterlist sg;
776 struct dma_async_tx_descriptor *desc;
777 struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
778 struct device *dev = usbhs_priv_to_dev(priv);
Vinod Koul55ba4e52011-10-14 21:57:58 +0530779 enum dma_transfer_direction dir;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900780 dma_cookie_t cookie;
781
Vinod Koul55ba4e52011-10-14 21:57:58 +0530782 dir = usbhs_pipe_is_dir_in(pipe) ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900783
784 sg_init_table(&sg, 1);
785 sg_set_page(&sg, virt_to_page(pkt->dma),
786 pkt->length, offset_in_page(pkt->dma));
787 sg_dma_address(&sg) = pkt->dma + pkt->actual;
788 sg_dma_len(&sg) = pkt->trans;
789
790 desc = chan->device->device_prep_slave_sg(chan, &sg, 1, dir,
791 DMA_PREP_INTERRUPT |
792 DMA_CTRL_ACK);
793 if (!desc)
794 return;
795
796 desc->callback = usbhsf_dma_complete;
797 desc->callback_param = pipe;
798
799 cookie = desc->tx_submit(desc);
800 if (cookie < 0) {
801 dev_err(dev, "Failed to submit dma descriptor\n");
802 return;
803 }
804
805 dev_dbg(dev, " %s %d (%d/ %d)\n",
806 fifo->name, usbhs_pipe_number(pipe), pkt->length, pkt->zero);
807
808 usbhsf_dma_start(pipe, fifo);
809 dma_async_issue_pending(chan);
810}
811
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700812/*
813 * DMA push handler
814 */
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900815static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done)
816{
817 struct usbhs_pipe *pipe = pkt->pipe;
818 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
819 struct usbhs_fifo *fifo;
820 int len = pkt->length - pkt->actual;
821 int ret;
822
823 if (usbhs_pipe_is_busy(pipe))
824 return 0;
825
826 /* use PIO if packet is less than pio_dma_border or pipe is DCP */
827 if ((len < usbhs_get_dparam(priv, pio_dma_border)) ||
828 usbhs_pipe_is_dcp(pipe))
829 goto usbhsf_pio_prepare_push;
830
831 if (len % 4) /* 32bit alignment */
832 goto usbhsf_pio_prepare_push;
833
Kuninori Morimotoc9ae0c92011-10-26 01:21:07 -0700834 if ((uintptr_t)(pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
Kuninori Morimoto9a12d092011-07-03 17:42:19 -0700835 goto usbhsf_pio_prepare_push;
836
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900837 /* get enable DMA fifo */
838 fifo = usbhsf_get_dma_fifo(priv, pkt);
839 if (!fifo)
840 goto usbhsf_pio_prepare_push;
841
842 if (usbhsf_dma_map(pkt) < 0)
843 goto usbhsf_pio_prepare_push;
844
845 ret = usbhsf_fifo_select(pipe, fifo, 0);
846 if (ret < 0)
847 goto usbhsf_pio_prepare_push_unmap;
848
849 pkt->trans = len;
850
851 tasklet_init(&fifo->tasklet,
852 usbhsf_dma_prepare_tasklet,
853 (unsigned long)pkt);
854
855 tasklet_schedule(&fifo->tasklet);
856
857 return 0;
858
859usbhsf_pio_prepare_push_unmap:
860 usbhsf_dma_unmap(pkt);
861usbhsf_pio_prepare_push:
862 /*
863 * change handler to PIO
864 */
865 pkt->handler = &usbhs_fifo_pio_push_handler;
866
867 return pkt->handler->prepare(pkt, is_done);
868}
869
870static int usbhsf_dma_push_done(struct usbhs_pkt *pkt, int *is_done)
871{
872 struct usbhs_pipe *pipe = pkt->pipe;
873
874 pkt->actual = pkt->trans;
875
876 *is_done = !pkt->zero; /* send zero packet ? */
877
878 usbhsf_dma_stop(pipe, pipe->fifo);
879 usbhsf_dma_unmap(pkt);
880 usbhsf_fifo_unselect(pipe, pipe->fifo);
881
882 return 0;
883}
884
885struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = {
886 .prepare = usbhsf_dma_prepare_push,
887 .dma_done = usbhsf_dma_push_done,
888};
889
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700890/*
891 * DMA pop handler
892 */
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900893static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done)
894{
895 struct usbhs_pipe *pipe = pkt->pipe;
896 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
897 struct usbhs_fifo *fifo;
898 int len, ret;
899
900 if (usbhs_pipe_is_busy(pipe))
901 return 0;
902
903 if (usbhs_pipe_is_dcp(pipe))
904 goto usbhsf_pio_prepare_pop;
905
906 /* get enable DMA fifo */
907 fifo = usbhsf_get_dma_fifo(priv, pkt);
908 if (!fifo)
909 goto usbhsf_pio_prepare_pop;
910
Kuninori Morimotoc9ae0c92011-10-26 01:21:07 -0700911 if ((uintptr_t)(pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
Kuninori Morimoto9a12d092011-07-03 17:42:19 -0700912 goto usbhsf_pio_prepare_pop;
913
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900914 ret = usbhsf_fifo_select(pipe, fifo, 0);
915 if (ret < 0)
916 goto usbhsf_pio_prepare_pop;
917
918 /* use PIO if packet is less than pio_dma_border */
919 len = usbhsf_fifo_rcv_len(priv, fifo);
920 len = min(pkt->length - pkt->actual, len);
921 if (len % 4) /* 32bit alignment */
922 goto usbhsf_pio_prepare_pop_unselect;
923
924 if (len < usbhs_get_dparam(priv, pio_dma_border))
925 goto usbhsf_pio_prepare_pop_unselect;
926
927 ret = usbhsf_fifo_barrier(priv, fifo);
928 if (ret < 0)
929 goto usbhsf_pio_prepare_pop_unselect;
930
931 if (usbhsf_dma_map(pkt) < 0)
932 goto usbhsf_pio_prepare_pop_unselect;
933
934 /* DMA */
935
936 /*
937 * usbhs_fifo_dma_pop_handler :: prepare
938 * enabled irq to come here.
939 * but it is no longer needed for DMA. disable it.
940 */
941 usbhsf_rx_irq_ctrl(pipe, 0);
942
943 pkt->trans = len;
944
945 tasklet_init(&fifo->tasklet,
946 usbhsf_dma_prepare_tasklet,
947 (unsigned long)pkt);
948
949 tasklet_schedule(&fifo->tasklet);
950
951 return 0;
952
953usbhsf_pio_prepare_pop_unselect:
954 usbhsf_fifo_unselect(pipe, fifo);
955usbhsf_pio_prepare_pop:
956
957 /*
958 * change handler to PIO
959 */
960 pkt->handler = &usbhs_fifo_pio_pop_handler;
961
962 return pkt->handler->try_run(pkt, is_done);
963}
964
965static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done)
966{
967 struct usbhs_pipe *pipe = pkt->pipe;
968 int maxp = usbhs_pipe_get_maxpacket(pipe);
969
970 usbhsf_dma_stop(pipe, pipe->fifo);
971 usbhsf_dma_unmap(pkt);
972 usbhsf_fifo_unselect(pipe, pipe->fifo);
973
974 pkt->actual += pkt->trans;
975
976 if ((pkt->actual == pkt->length) || /* receive all data */
977 (pkt->trans < maxp)) { /* short packet */
978 *is_done = 1;
979 } else {
980 /* re-enable */
981 usbhsf_prepare_pop(pkt, is_done);
982 }
983
984 return 0;
985}
986
987struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = {
988 .prepare = usbhsf_prepare_pop,
989 .try_run = usbhsf_dma_try_pop,
990 .dma_done = usbhsf_dma_pop_done
991};
992
993/*
994 * DMA setting
995 */
996static bool usbhsf_dma_filter(struct dma_chan *chan, void *param)
997{
998 struct sh_dmae_slave *slave = param;
999
1000 /*
1001 * FIXME
1002 *
1003 * usbhs doesn't recognize id = 0 as valid DMA
1004 */
1005 if (0 == slave->slave_id)
1006 return false;
1007
1008 chan->private = slave;
1009
1010 return true;
1011}
1012
1013static void usbhsf_dma_quit(struct usbhs_priv *priv, struct usbhs_fifo *fifo)
1014{
1015 if (fifo->tx_chan)
1016 dma_release_channel(fifo->tx_chan);
1017 if (fifo->rx_chan)
1018 dma_release_channel(fifo->rx_chan);
1019
1020 fifo->tx_chan = NULL;
1021 fifo->rx_chan = NULL;
1022}
1023
1024static void usbhsf_dma_init(struct usbhs_priv *priv,
1025 struct usbhs_fifo *fifo)
1026{
1027 struct device *dev = usbhs_priv_to_dev(priv);
1028 dma_cap_mask_t mask;
1029
1030 dma_cap_zero(mask);
1031 dma_cap_set(DMA_SLAVE, mask);
1032 fifo->tx_chan = dma_request_channel(mask, usbhsf_dma_filter,
1033 &fifo->tx_slave);
1034
1035 dma_cap_zero(mask);
1036 dma_cap_set(DMA_SLAVE, mask);
1037 fifo->rx_chan = dma_request_channel(mask, usbhsf_dma_filter,
1038 &fifo->rx_slave);
1039
1040 if (fifo->tx_chan || fifo->rx_chan)
Kuninori Morimoto4ce68802011-06-21 09:33:43 +09001041 dev_dbg(dev, "enable DMAEngine (%s%s%s)\n",
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001042 fifo->name,
1043 fifo->tx_chan ? "[TX]" : " ",
1044 fifo->rx_chan ? "[RX]" : " ");
1045}
1046
1047/*
Kuninori Morimotodad67392011-06-06 14:18:28 +09001048 * irq functions
1049 */
1050static int usbhsf_irq_empty(struct usbhs_priv *priv,
1051 struct usbhs_irq_state *irq_state)
1052{
1053 struct usbhs_pipe *pipe;
Kuninori Morimotodad67392011-06-06 14:18:28 +09001054 struct device *dev = usbhs_priv_to_dev(priv);
1055 int i, ret;
1056
1057 if (!irq_state->bempsts) {
1058 dev_err(dev, "debug %s !!\n", __func__);
1059 return -EIO;
1060 }
1061
1062 dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
1063
1064 /*
1065 * search interrupted "pipe"
1066 * not "uep".
1067 */
1068 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
1069 if (!(irq_state->bempsts & (1 << i)))
1070 continue;
1071
Kuninori Morimoto51b8a022011-10-10 21:58:45 -07001072 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
Kuninori Morimotodad67392011-06-06 14:18:28 +09001073 if (ret < 0)
1074 dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
1075 }
1076
1077 return 0;
1078}
1079
1080static int usbhsf_irq_ready(struct usbhs_priv *priv,
1081 struct usbhs_irq_state *irq_state)
1082{
1083 struct usbhs_pipe *pipe;
Kuninori Morimotodad67392011-06-06 14:18:28 +09001084 struct device *dev = usbhs_priv_to_dev(priv);
1085 int i, ret;
1086
1087 if (!irq_state->brdysts) {
1088 dev_err(dev, "debug %s !!\n", __func__);
1089 return -EIO;
1090 }
1091
1092 dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
1093
1094 /*
1095 * search interrupted "pipe"
1096 * not "uep".
1097 */
1098 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
1099 if (!(irq_state->brdysts & (1 << i)))
1100 continue;
1101
Kuninori Morimoto51b8a022011-10-10 21:58:45 -07001102 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
Kuninori Morimotodad67392011-06-06 14:18:28 +09001103 if (ret < 0)
1104 dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
1105 }
1106
1107 return 0;
1108}
1109
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001110static void usbhsf_dma_complete(void *arg)
1111{
1112 struct usbhs_pipe *pipe = arg;
1113 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1114 struct device *dev = usbhs_priv_to_dev(priv);
1115 int ret;
1116
Kuninori Morimoto51b8a022011-10-10 21:58:45 -07001117 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_DMA_DONE);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001118 if (ret < 0)
1119 dev_err(dev, "dma_complete run_error %d : %d\n",
1120 usbhs_pipe_number(pipe), ret);
1121}
1122
Kuninori Morimotodad67392011-06-06 14:18:28 +09001123/*
1124 * fifo init
1125 */
1126void usbhs_fifo_init(struct usbhs_priv *priv)
1127{
1128 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
Kuninori Morimotod77e3f42011-06-06 14:18:50 +09001129 struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001130 struct usbhs_fifo *d0fifo = usbhsf_get_d0fifo(priv);
1131 struct usbhs_fifo *d1fifo = usbhsf_get_d1fifo(priv);
Kuninori Morimotodad67392011-06-06 14:18:28 +09001132
1133 mod->irq_empty = usbhsf_irq_empty;
1134 mod->irq_ready = usbhsf_irq_ready;
1135 mod->irq_bempsts = 0;
1136 mod->irq_brdysts = 0;
Kuninori Morimotod77e3f42011-06-06 14:18:50 +09001137
1138 cfifo->pipe = NULL;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001139 cfifo->tx_chan = NULL;
1140 cfifo->rx_chan = NULL;
1141
1142 d0fifo->pipe = NULL;
1143 d0fifo->tx_chan = NULL;
1144 d0fifo->rx_chan = NULL;
1145
1146 d1fifo->pipe = NULL;
1147 d1fifo->tx_chan = NULL;
1148 d1fifo->rx_chan = NULL;
1149
1150 usbhsf_dma_init(priv, usbhsf_get_d0fifo(priv));
1151 usbhsf_dma_init(priv, usbhsf_get_d1fifo(priv));
Kuninori Morimotodad67392011-06-06 14:18:28 +09001152}
1153
1154void usbhs_fifo_quit(struct usbhs_priv *priv)
1155{
1156 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1157
1158 mod->irq_empty = NULL;
1159 mod->irq_ready = NULL;
1160 mod->irq_bempsts = 0;
1161 mod->irq_brdysts = 0;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001162
1163 usbhsf_dma_quit(priv, usbhsf_get_d0fifo(priv));
1164 usbhsf_dma_quit(priv, usbhsf_get_d1fifo(priv));
Kuninori Morimotodad67392011-06-06 14:18:28 +09001165}
Kuninori Morimotod3af90a2011-06-06 14:18:44 +09001166
1167int usbhs_fifo_probe(struct usbhs_priv *priv)
1168{
1169 struct usbhs_fifo *fifo;
1170
1171 /* CFIFO */
1172 fifo = usbhsf_get_cfifo(priv);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001173 fifo->name = "CFIFO";
Kuninori Morimotod3af90a2011-06-06 14:18:44 +09001174 fifo->port = CFIFO;
1175 fifo->sel = CFIFOSEL;
1176 fifo->ctr = CFIFOCTR;
1177
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001178 /* D0FIFO */
1179 fifo = usbhsf_get_d0fifo(priv);
1180 fifo->name = "D0FIFO";
1181 fifo->port = D0FIFO;
1182 fifo->sel = D0FIFOSEL;
1183 fifo->ctr = D0FIFOCTR;
1184 fifo->tx_slave.slave_id = usbhs_get_dparam(priv, d0_tx_id);
1185 fifo->rx_slave.slave_id = usbhs_get_dparam(priv, d0_rx_id);
1186
1187 /* D1FIFO */
1188 fifo = usbhsf_get_d1fifo(priv);
1189 fifo->name = "D1FIFO";
1190 fifo->port = D1FIFO;
1191 fifo->sel = D1FIFOSEL;
1192 fifo->ctr = D1FIFOCTR;
1193 fifo->tx_slave.slave_id = usbhs_get_dparam(priv, d1_tx_id);
1194 fifo->rx_slave.slave_id = usbhs_get_dparam(priv, d1_rx_id);
1195
Kuninori Morimotod3af90a2011-06-06 14:18:44 +09001196 return 0;
1197}
1198
1199void usbhs_fifo_remove(struct usbhs_priv *priv)
1200{
1201}