blob: 0bad84ebe9aa313abc4425337f1d2e6a054983ab [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>
Paul Bollecc502bb2012-06-03 18:39:13 +020020#include "common.h"
21#include "pipe.h"
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +090022
Kuninori Morimotod3af90a2011-06-06 14:18:44 +090023#define usbhsf_get_cfifo(p) (&((p)->fifo_info.cfifo))
Shimoda, Yoshihiro5ea43992012-01-05 15:37:22 +090024#define usbhsf_is_cfifo(p, f) (usbhsf_get_cfifo(p) == f)
Kuninori Morimotod3af90a2011-06-06 14:18:44 +090025
Kuninori Morimotod77e3f42011-06-06 14:18:50 +090026#define usbhsf_fifo_is_busy(f) ((f)->pipe) /* see usbhs_pipe_select_fifo */
27
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +090028/*
Kuninori Morimoto233f5192011-07-07 00:23:24 -070029 * packet initialize
30 */
31void usbhs_pkt_init(struct usbhs_pkt *pkt)
32{
Kuninori Morimoto233f5192011-07-07 00:23:24 -070033 INIT_LIST_HEAD(&pkt->node);
34}
35
36/*
37 * packet control function
Kuninori Morimoto4bd04812011-06-06 14:18:07 +090038 */
Kuninori Morimoto97664a22011-06-06 14:18:38 +090039static int usbhsf_null_handle(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotodad67392011-06-06 14:18:28 +090040{
41 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
42 struct device *dev = usbhs_priv_to_dev(priv);
43
44 dev_err(dev, "null handler\n");
45
46 return -EINVAL;
47}
48
49static struct usbhs_pkt_handle usbhsf_null_handler = {
50 .prepare = usbhsf_null_handle,
51 .try_run = usbhsf_null_handle,
52};
53
Kuninori Morimoto659d4952011-06-06 14:18:23 +090054void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt,
Kuninori Morimotob3318722011-10-10 22:04:41 -070055 void (*done)(struct usbhs_priv *priv,
56 struct usbhs_pkt *pkt),
Kuninori Morimoto3edeee32011-12-08 18:28:54 -080057 void *buf, int len, int zero, int sequence)
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090058{
Kuninori Morimotodad67392011-06-06 14:18:28 +090059 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
60 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto97664a22011-06-06 14:18:38 +090061 unsigned long flags;
62
Kuninori Morimotob3318722011-10-10 22:04:41 -070063 if (!done) {
64 dev_err(dev, "no done function\n");
65 return;
66 }
67
Kuninori Morimotoa2c76b82011-10-18 20:05:50 -070068 /******************** spin lock ********************/
69 usbhs_lock(priv, flags);
70
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -070071 if (!pipe->handler) {
Kuninori Morimotodad67392011-06-06 14:18:28 +090072 dev_err(dev, "no handler function\n");
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -070073 pipe->handler = &usbhsf_null_handler;
Kuninori Morimotodad67392011-06-06 14:18:28 +090074 }
75
Guennadi Liakhovetskid5261282012-02-14 11:37:17 +010076 list_move_tail(&pkt->node, &pipe->list);
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090077
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -070078 /*
79 * each pkt must hold own handler.
80 * because handler might be changed by its situation.
81 * dma handler -> pio handler.
82 */
Kuninori Morimoto659d4952011-06-06 14:18:23 +090083 pkt->pipe = pipe;
84 pkt->buf = buf;
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -070085 pkt->handler = pipe->handler;
Kuninori Morimoto659d4952011-06-06 14:18:23 +090086 pkt->length = len;
87 pkt->zero = zero;
88 pkt->actual = 0;
Kuninori Morimotob3318722011-10-10 22:04:41 -070089 pkt->done = done;
Kuninori Morimoto3edeee32011-12-08 18:28:54 -080090 pkt->sequence = sequence;
Kuninori Morimoto97664a22011-06-06 14:18:38 +090091
92 usbhs_unlock(priv, flags);
93 /******************** spin unlock ******************/
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090094}
95
Kuninori Morimoto97664a22011-06-06 14:18:38 +090096static void __usbhsf_pkt_del(struct usbhs_pkt *pkt)
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090097{
98 list_del_init(&pkt->node);
99}
100
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900101static struct usbhs_pkt *__usbhsf_pkt_get(struct usbhs_pipe *pipe)
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900102{
103 if (list_empty(&pipe->list))
104 return NULL;
105
Guennadi Liakhovetskid5261282012-02-14 11:37:17 +0100106 return list_first_entry(&pipe->list, struct usbhs_pkt, node);
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900107}
108
Yoshihiro Shimoda2743e7f2014-08-22 20:14:28 +0900109static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
110 struct usbhs_fifo *fifo);
111static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
112 struct usbhs_fifo *fifo);
113static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
114 struct usbhs_pkt *pkt);
115#define usbhsf_dma_map(p) __usbhsf_dma_map_ctrl(p, 1)
116#define usbhsf_dma_unmap(p) __usbhsf_dma_map_ctrl(p, 0)
117static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900118struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt)
119{
120 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Yoshihiro Shimoda2743e7f2014-08-22 20:14:28 +0900121 struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900122 unsigned long flags;
123
124 /******************** spin lock ********************/
125 usbhs_lock(priv, flags);
126
Yoshihiro Shimoda2743e7f2014-08-22 20:14:28 +0900127 usbhs_pipe_disable(pipe);
128
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900129 if (!pkt)
130 pkt = __usbhsf_pkt_get(pipe);
131
Yoshihiro Shimoda2743e7f2014-08-22 20:14:28 +0900132 if (pkt) {
133 struct dma_chan *chan = NULL;
134
135 if (fifo)
136 chan = usbhsf_dma_chan_get(fifo, pkt);
137 if (chan) {
138 dmaengine_terminate_all(chan);
139 usbhsf_fifo_clear(pipe, fifo);
140 usbhsf_dma_unmap(pkt);
141 }
142
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900143 __usbhsf_pkt_del(pkt);
Yoshihiro Shimoda2743e7f2014-08-22 20:14:28 +0900144 }
145
146 if (fifo)
147 usbhsf_fifo_unselect(pipe, fifo);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900148
149 usbhs_unlock(priv, flags);
150 /******************** spin unlock ******************/
151
152 return pkt;
153}
154
Kuninori Morimoto51b8a022011-10-10 21:58:45 -0700155enum {
156 USBHSF_PKT_PREPARE,
157 USBHSF_PKT_TRY_RUN,
158 USBHSF_PKT_DMA_DONE,
159};
160
161static int usbhsf_pkt_handler(struct usbhs_pipe *pipe, int type)
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900162{
163 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900164 struct usbhs_pkt *pkt;
165 struct device *dev = usbhs_priv_to_dev(priv);
166 int (*func)(struct usbhs_pkt *pkt, int *is_done);
167 unsigned long flags;
168 int ret = 0;
169 int is_done = 0;
170
171 /******************** spin lock ********************/
172 usbhs_lock(priv, flags);
173
174 pkt = __usbhsf_pkt_get(pipe);
175 if (!pkt)
176 goto __usbhs_pkt_handler_end;
177
178 switch (type) {
179 case USBHSF_PKT_PREPARE:
180 func = pkt->handler->prepare;
181 break;
182 case USBHSF_PKT_TRY_RUN:
183 func = pkt->handler->try_run;
184 break;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900185 case USBHSF_PKT_DMA_DONE:
186 func = pkt->handler->dma_done;
187 break;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900188 default:
Masanari Iida984e8332012-11-01 00:03:51 +0900189 dev_err(dev, "unknown pkt handler\n");
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900190 goto __usbhs_pkt_handler_end;
191 }
192
193 ret = func(pkt, &is_done);
194
195 if (is_done)
196 __usbhsf_pkt_del(pkt);
197
198__usbhs_pkt_handler_end:
199 usbhs_unlock(priv, flags);
200 /******************** spin unlock ******************/
201
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900202 if (is_done) {
Kuninori Morimotob3318722011-10-10 22:04:41 -0700203 pkt->done(priv, pkt);
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900204 usbhs_pkt_start(pipe);
205 }
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900206
207 return ret;
208}
209
Kuninori Morimoto51b8a022011-10-10 21:58:45 -0700210void usbhs_pkt_start(struct usbhs_pipe *pipe)
211{
212 usbhsf_pkt_handler(pipe, USBHSF_PKT_PREPARE);
213}
214
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900215/*
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900216 * irq enable/disable function
217 */
Kuninori Morimoto3192fcb2012-10-29 00:45:07 -0700218#define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_bempsts, e)
219#define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_brdysts, e)
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900220#define usbhsf_irq_callback_ctrl(pipe, status, enable) \
221 ({ \
222 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
223 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
224 u16 status = (1 << usbhs_pipe_number(pipe)); \
225 if (!mod) \
226 return; \
227 if (enable) \
Kuninori Morimoto3192fcb2012-10-29 00:45:07 -0700228 mod->status |= status; \
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900229 else \
Kuninori Morimoto3192fcb2012-10-29 00:45:07 -0700230 mod->status &= ~status; \
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900231 usbhs_irq_callback_update(priv, mod); \
232 })
233
234static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
235{
236 /*
237 * And DCP pipe can NOT use "ready interrupt" for "send"
238 * it should use "empty" interrupt.
239 * see
240 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
241 *
242 * on the other hand, normal pipe can use "ready interrupt" for "send"
243 * even though it is single/double buffer
244 */
245 if (usbhs_pipe_is_dcp(pipe))
246 usbhsf_irq_empty_ctrl(pipe, enable);
247 else
248 usbhsf_irq_ready_ctrl(pipe, enable);
249}
250
251static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
252{
253 usbhsf_irq_ready_ctrl(pipe, enable);
254}
255
256/*
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900257 * FIFO ctrl
258 */
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900259static void usbhsf_send_terminator(struct usbhs_pipe *pipe,
260 struct usbhs_fifo *fifo)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900261{
262 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
263
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900264 usbhs_bset(priv, fifo->ctr, BVAL, BVAL);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900265}
266
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900267static int usbhsf_fifo_barrier(struct usbhs_priv *priv,
268 struct usbhs_fifo *fifo)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900269{
270 int timeout = 1024;
271
272 do {
273 /* The FIFO port is accessible */
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900274 if (usbhs_read(priv, fifo->ctr) & FRDY)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900275 return 0;
276
277 udelay(10);
278 } while (timeout--);
279
280 return -EBUSY;
281}
282
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900283static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
284 struct usbhs_fifo *fifo)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900285{
286 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
287
288 if (!usbhs_pipe_is_dcp(pipe))
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900289 usbhsf_fifo_barrier(priv, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900290
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900291 usbhs_write(priv, fifo->ctr, BCLR);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900292}
293
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900294static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv,
295 struct usbhs_fifo *fifo)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900296{
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900297 return usbhs_read(priv, fifo->ctr) & DTLN_MASK;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900298}
299
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900300static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
301 struct usbhs_fifo *fifo)
302{
303 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
304
305 usbhs_pipe_select_fifo(pipe, NULL);
306 usbhs_write(priv, fifo->sel, 0);
307}
308
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900309static int usbhsf_fifo_select(struct usbhs_pipe *pipe,
310 struct usbhs_fifo *fifo,
311 int write)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900312{
313 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
314 struct device *dev = usbhs_priv_to_dev(priv);
315 int timeout = 1024;
316 u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
317 u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
318
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900319 if (usbhs_pipe_is_busy(pipe) ||
320 usbhsf_fifo_is_busy(fifo))
321 return -EBUSY;
322
Kuninori Morimoto92352072011-10-10 22:02:57 -0700323 if (usbhs_pipe_is_dcp(pipe)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900324 base |= (1 == write) << 5; /* ISEL */
325
Kuninori Morimoto92352072011-10-10 22:02:57 -0700326 if (usbhs_mod_is_host(priv))
327 usbhs_dcp_dir_for_host(pipe, write);
328 }
329
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900330 /* "base" will be used below */
Shimoda, Yoshihiro5ea43992012-01-05 15:37:22 +0900331 if (usbhs_get_dparam(priv, has_sudmac) && !usbhsf_is_cfifo(priv, fifo))
332 usbhs_write(priv, fifo->sel, base);
333 else
334 usbhs_write(priv, fifo->sel, base | MBW_32);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900335
336 /* check ISEL and CURPIPE value */
337 while (timeout--) {
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900338 if (base == (mask & usbhs_read(priv, fifo->sel))) {
339 usbhs_pipe_select_fifo(pipe, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900340 return 0;
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900341 }
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900342 udelay(10);
343 }
344
345 dev_err(dev, "fifo select error\n");
346
347 return -EIO;
348}
349
350/*
Kuninori Morimoto9e74d602011-10-10 22:07:08 -0700351 * DCP status stage
352 */
353static int usbhs_dcp_dir_switch_to_write(struct usbhs_pkt *pkt, int *is_done)
354{
355 struct usbhs_pipe *pipe = pkt->pipe;
356 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
357 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
358 struct device *dev = usbhs_priv_to_dev(priv);
359 int ret;
360
361 usbhs_pipe_disable(pipe);
362
363 ret = usbhsf_fifo_select(pipe, fifo, 1);
364 if (ret < 0) {
365 dev_err(dev, "%s() faile\n", __func__);
366 return ret;
367 }
368
369 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
370
371 usbhsf_fifo_clear(pipe, fifo);
372 usbhsf_send_terminator(pipe, fifo);
373
374 usbhsf_fifo_unselect(pipe, fifo);
375
376 usbhsf_tx_irq_ctrl(pipe, 1);
377 usbhs_pipe_enable(pipe);
378
379 return ret;
380}
381
382static int usbhs_dcp_dir_switch_to_read(struct usbhs_pkt *pkt, int *is_done)
383{
384 struct usbhs_pipe *pipe = pkt->pipe;
385 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
386 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
387 struct device *dev = usbhs_priv_to_dev(priv);
388 int ret;
389
390 usbhs_pipe_disable(pipe);
391
392 ret = usbhsf_fifo_select(pipe, fifo, 0);
393 if (ret < 0) {
394 dev_err(dev, "%s() fail\n", __func__);
395 return ret;
396 }
397
398 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
399 usbhsf_fifo_clear(pipe, fifo);
400
401 usbhsf_fifo_unselect(pipe, fifo);
402
403 usbhsf_rx_irq_ctrl(pipe, 1);
404 usbhs_pipe_enable(pipe);
405
406 return ret;
407
408}
409
410static int usbhs_dcp_dir_switch_done(struct usbhs_pkt *pkt, int *is_done)
411{
412 struct usbhs_pipe *pipe = pkt->pipe;
413
414 if (pkt->handler == &usbhs_dcp_status_stage_in_handler)
415 usbhsf_tx_irq_ctrl(pipe, 0);
416 else
417 usbhsf_rx_irq_ctrl(pipe, 0);
418
419 pkt->actual = pkt->length;
420 *is_done = 1;
421
422 return 0;
423}
424
425struct usbhs_pkt_handle usbhs_dcp_status_stage_in_handler = {
426 .prepare = usbhs_dcp_dir_switch_to_write,
427 .try_run = usbhs_dcp_dir_switch_done,
428};
429
430struct usbhs_pkt_handle usbhs_dcp_status_stage_out_handler = {
431 .prepare = usbhs_dcp_dir_switch_to_read,
432 .try_run = usbhs_dcp_dir_switch_done,
433};
434
435/*
436 * DCP data stage (push)
437 */
438static int usbhsf_dcp_data_stage_try_push(struct usbhs_pkt *pkt, int *is_done)
439{
440 struct usbhs_pipe *pipe = pkt->pipe;
441
442 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
443
444 /*
445 * change handler to PIO push
446 */
447 pkt->handler = &usbhs_fifo_pio_push_handler;
448
449 return pkt->handler->prepare(pkt, is_done);
450}
451
452struct usbhs_pkt_handle usbhs_dcp_data_stage_out_handler = {
453 .prepare = usbhsf_dcp_data_stage_try_push,
454};
455
456/*
457 * DCP data stage (pop)
458 */
459static int usbhsf_dcp_data_stage_prepare_pop(struct usbhs_pkt *pkt,
460 int *is_done)
461{
462 struct usbhs_pipe *pipe = pkt->pipe;
463 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
464 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
465
466 if (usbhs_pipe_is_busy(pipe))
467 return 0;
468
469 /*
470 * prepare pop for DCP should
471 * - change DCP direction,
472 * - clear fifo
473 * - DATA1
474 */
475 usbhs_pipe_disable(pipe);
476
477 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
478
479 usbhsf_fifo_select(pipe, fifo, 0);
480 usbhsf_fifo_clear(pipe, fifo);
481 usbhsf_fifo_unselect(pipe, fifo);
482
483 /*
484 * change handler to PIO pop
485 */
486 pkt->handler = &usbhs_fifo_pio_pop_handler;
487
488 return pkt->handler->prepare(pkt, is_done);
489}
490
491struct usbhs_pkt_handle usbhs_dcp_data_stage_in_handler = {
492 .prepare = usbhsf_dcp_data_stage_prepare_pop,
493};
494
495/*
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700496 * PIO push handler
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900497 */
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900498static int usbhsf_pio_try_push(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900499{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900500 struct usbhs_pipe *pipe = pkt->pipe;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900501 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900502 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900503 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
504 void __iomem *addr = priv->base + fifo->port;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900505 u8 *buf;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900506 int maxp = usbhs_pipe_get_maxpacket(pipe);
507 int total_len;
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900508 int i, ret, len;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900509 int is_short;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900510
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800511 usbhs_pipe_data_sequence(pipe, pkt->sequence);
512 pkt->sequence = -1; /* -1 sequence will be ignored */
513
Kuninori Morimoto1c90ee02012-11-06 16:15:09 -0800514 usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
515
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900516 ret = usbhsf_fifo_select(pipe, fifo, 1);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900517 if (ret < 0)
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900518 return 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900519
Kuninori Morimotodad67392011-06-06 14:18:28 +0900520 ret = usbhs_pipe_is_accessible(pipe);
Kuninori Morimoto4ef85e02011-07-03 17:42:47 -0700521 if (ret < 0) {
522 /* inaccessible pipe is not an error */
523 ret = 0;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900524 goto usbhs_fifo_write_busy;
Kuninori Morimoto4ef85e02011-07-03 17:42:47 -0700525 }
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900526
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900527 ret = usbhsf_fifo_barrier(priv, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900528 if (ret < 0)
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900529 goto usbhs_fifo_write_busy;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900530
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900531 buf = pkt->buf + pkt->actual;
532 len = pkt->length - pkt->actual;
533 len = min(len, maxp);
534 total_len = len;
535 is_short = total_len < maxp;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900536
537 /*
538 * FIXME
539 *
540 * 32-bit access only
541 */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900542 if (len >= 4 && !((unsigned long)buf & 0x03)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900543 iowrite32_rep(addr, buf, len / 4);
544 len %= 4;
545 buf += total_len - len;
546 }
547
548 /* the rest operation */
549 for (i = 0; i < len; i++)
550 iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
551
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900552 /*
553 * variable update
554 */
555 pkt->actual += total_len;
556
557 if (pkt->actual < pkt->length)
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900558 *is_done = 0; /* there are remainder data */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900559 else if (is_short)
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900560 *is_done = 1; /* short packet */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900561 else
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900562 *is_done = !pkt->zero; /* send zero packet ? */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900563
564 /*
565 * pipe/irq handling
566 */
567 if (is_short)
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900568 usbhsf_send_terminator(pipe, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900569
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900570 usbhsf_tx_irq_ctrl(pipe, !*is_done);
Yoshihiro Shimoda8355b2b2014-08-22 20:13:50 +0900571 usbhs_pipe_running(pipe, !*is_done);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900572 usbhs_pipe_enable(pipe);
573
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900574 dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
575 usbhs_pipe_number(pipe),
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900576 pkt->length, pkt->actual, *is_done, pkt->zero);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900577
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900578 usbhsf_fifo_unselect(pipe, fifo);
579
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900580 return 0;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900581
582usbhs_fifo_write_busy:
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900583 usbhsf_fifo_unselect(pipe, fifo);
584
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900585 /*
586 * pipe is busy.
587 * retry in interrupt
588 */
589 usbhsf_tx_irq_ctrl(pipe, 1);
Yoshihiro Shimoda8355b2b2014-08-22 20:13:50 +0900590 usbhs_pipe_running(pipe, 1);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900591
592 return ret;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900593}
594
Yoshihiro Shimoda8355b2b2014-08-22 20:13:50 +0900595static int usbhsf_pio_prepare_push(struct usbhs_pkt *pkt, int *is_done)
596{
597 if (usbhs_pipe_is_running(pkt->pipe))
598 return 0;
599
600 return usbhsf_pio_try_push(pkt, is_done);
601}
602
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900603struct usbhs_pkt_handle usbhs_fifo_pio_push_handler = {
Yoshihiro Shimoda8355b2b2014-08-22 20:13:50 +0900604 .prepare = usbhsf_pio_prepare_push,
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900605 .try_run = usbhsf_pio_try_push,
Kuninori Morimotodad67392011-06-06 14:18:28 +0900606};
607
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700608/*
609 * PIO pop handler
610 */
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900611static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900612{
Kuninori Morimotodad67392011-06-06 14:18:28 +0900613 struct usbhs_pipe *pipe = pkt->pipe;
Kazuya Mizuguchie73d42f2015-05-26 20:13:42 +0900614 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
615 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900616
617 if (usbhs_pipe_is_busy(pipe))
618 return 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900619
Yoshihiro Shimoda8355b2b2014-08-22 20:13:50 +0900620 if (usbhs_pipe_is_running(pipe))
621 return 0;
622
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900623 /*
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900624 * pipe enable to prepare packet receive
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900625 */
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800626 usbhs_pipe_data_sequence(pipe, pkt->sequence);
627 pkt->sequence = -1; /* -1 sequence will be ignored */
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900628
Kazuya Mizuguchie73d42f2015-05-26 20:13:42 +0900629 if (usbhs_pipe_is_dcp(pipe))
630 usbhsf_fifo_clear(pipe, fifo);
631
Kuninori Morimoto1c90ee02012-11-06 16:15:09 -0800632 usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900633 usbhs_pipe_enable(pipe);
Yoshihiro Shimoda8355b2b2014-08-22 20:13:50 +0900634 usbhs_pipe_running(pipe, 1);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900635 usbhsf_rx_irq_ctrl(pipe, 1);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900636
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900637 return 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900638}
639
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900640static int usbhsf_pio_try_pop(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900641{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900642 struct usbhs_pipe *pipe = pkt->pipe;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900643 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900644 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900645 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
646 void __iomem *addr = priv->base + fifo->port;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900647 u8 *buf;
648 u32 data = 0;
649 int maxp = usbhs_pipe_get_maxpacket(pipe);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900650 int rcv_len, len;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900651 int i, ret;
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900652 int total_len = 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900653
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900654 ret = usbhsf_fifo_select(pipe, fifo, 0);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900655 if (ret < 0)
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900656 return 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900657
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900658 ret = usbhsf_fifo_barrier(priv, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900659 if (ret < 0)
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900660 goto usbhs_fifo_read_busy;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900661
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900662 rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900663
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900664 buf = pkt->buf + pkt->actual;
665 len = pkt->length - pkt->actual;
666 len = min(len, rcv_len);
667 total_len = len;
668
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900669 /*
Kuninori Morimoto6ff5d092011-10-10 22:05:51 -0700670 * update actual length first here to decide disable pipe.
671 * if this pipe keeps BUF status and all data were popped,
672 * then, next interrupt/token will be issued again
673 */
674 pkt->actual += total_len;
675
676 if ((pkt->actual == pkt->length) || /* receive all data */
677 (total_len < maxp)) { /* short packet */
678 *is_done = 1;
679 usbhsf_rx_irq_ctrl(pipe, 0);
Yoshihiro Shimoda8355b2b2014-08-22 20:13:50 +0900680 usbhs_pipe_running(pipe, 0);
Kuninori Morimoto6ff5d092011-10-10 22:05:51 -0700681 usbhs_pipe_disable(pipe); /* disable pipe first */
682 }
683
684 /*
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900685 * Buffer clear if Zero-Length packet
686 *
687 * see
688 * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
689 */
690 if (0 == rcv_len) {
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800691 pkt->zero = 1;
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900692 usbhsf_fifo_clear(pipe, fifo);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900693 goto usbhs_fifo_read_end;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900694 }
695
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900696 /*
697 * FIXME
698 *
699 * 32-bit access only
700 */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900701 if (len >= 4 && !((unsigned long)buf & 0x03)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900702 ioread32_rep(addr, buf, len / 4);
703 len %= 4;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900704 buf += total_len - len;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900705 }
706
707 /* the rest operation */
708 for (i = 0; i < len; i++) {
709 if (!(i & 0x03))
710 data = ioread32(addr);
711
712 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
713 }
714
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900715usbhs_fifo_read_end:
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900716 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
717 usbhs_pipe_number(pipe),
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900718 pkt->length, pkt->actual, *is_done, pkt->zero);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900719
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900720usbhs_fifo_read_busy:
721 usbhsf_fifo_unselect(pipe, fifo);
722
723 return ret;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900724}
Kuninori Morimotodad67392011-06-06 14:18:28 +0900725
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900726struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler = {
Kuninori Morimotodad67392011-06-06 14:18:28 +0900727 .prepare = usbhsf_prepare_pop,
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900728 .try_run = usbhsf_pio_try_pop,
Kuninori Morimotodad67392011-06-06 14:18:28 +0900729};
730
731/*
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700732 * DCP ctrol statge handler
Kuninori Morimotodad67392011-06-06 14:18:28 +0900733 */
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900734static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotodad67392011-06-06 14:18:28 +0900735{
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900736 usbhs_dcp_control_transfer_done(pkt->pipe);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900737
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900738 *is_done = 1;
Kuninori Morimotodad67392011-06-06 14:18:28 +0900739
740 return 0;
741}
742
743struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
744 .prepare = usbhsf_ctrl_stage_end,
745 .try_run = usbhsf_ctrl_stage_end,
746};
747
748/*
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900749 * DMA fifo functions
750 */
751static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
752 struct usbhs_pkt *pkt)
753{
754 if (&usbhs_fifo_dma_push_handler == pkt->handler)
755 return fifo->tx_chan;
756
757 if (&usbhs_fifo_dma_pop_handler == pkt->handler)
758 return fifo->rx_chan;
759
760 return NULL;
761}
762
763static struct usbhs_fifo *usbhsf_get_dma_fifo(struct usbhs_priv *priv,
764 struct usbhs_pkt *pkt)
765{
766 struct usbhs_fifo *fifo;
Yoshihiro Shimoda3a2634a2014-11-10 20:02:45 +0900767 int i;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900768
Yoshihiro Shimoda3a2634a2014-11-10 20:02:45 +0900769 usbhs_for_each_dfifo(priv, fifo, i) {
770 if (usbhsf_dma_chan_get(fifo, pkt) &&
771 !usbhsf_fifo_is_busy(fifo))
772 return fifo;
773 }
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900774
775 return NULL;
776}
777
778#define usbhsf_dma_start(p, f) __usbhsf_dma_ctrl(p, f, DREQE)
779#define usbhsf_dma_stop(p, f) __usbhsf_dma_ctrl(p, f, 0)
780static void __usbhsf_dma_ctrl(struct usbhs_pipe *pipe,
781 struct usbhs_fifo *fifo,
782 u16 dreqe)
783{
784 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
785
786 usbhs_bset(priv, fifo->sel, DREQE, dreqe);
787}
788
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900789static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
790{
791 struct usbhs_pipe *pipe = pkt->pipe;
792 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
793 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
794
795 return info->dma_map_ctrl(pkt, map);
796}
797
798static void usbhsf_dma_complete(void *arg);
Guennadi Liakhovetski6e4b74e2012-02-14 11:37:21 +0100799static void xfer_work(struct work_struct *work)
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900800{
Guennadi Liakhovetski6e4b74e2012-02-14 11:37:21 +0100801 struct usbhs_pkt *pkt = container_of(work, struct usbhs_pkt, work);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900802 struct usbhs_pipe *pipe = pkt->pipe;
803 struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
804 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900805 struct dma_async_tx_descriptor *desc;
806 struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
807 struct device *dev = usbhs_priv_to_dev(priv);
Vinod Koul55ba4e52011-10-14 21:57:58 +0530808 enum dma_transfer_direction dir;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900809
Vinod Koul55ba4e52011-10-14 21:57:58 +0530810 dir = usbhs_pipe_is_dir_in(pipe) ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900811
Kuninori Morimoto2f0de9d2012-07-08 23:10:28 -0700812 desc = dmaengine_prep_slave_single(chan, pkt->dma + pkt->actual,
813 pkt->trans, dir,
Alexandre Bounine16052822012-03-08 16:11:18 -0500814 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900815 if (!desc)
816 return;
817
818 desc->callback = usbhsf_dma_complete;
819 desc->callback_param = pipe;
820
Yoshihiro Shimodaab330cf2015-03-12 15:35:20 +0900821 pkt->cookie = dmaengine_submit(desc);
822 if (pkt->cookie < 0) {
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900823 dev_err(dev, "Failed to submit dma descriptor\n");
824 return;
825 }
826
827 dev_dbg(dev, " %s %d (%d/ %d)\n",
828 fifo->name, usbhs_pipe_number(pipe), pkt->length, pkt->zero);
829
Yoshihiro Shimoda8355b2b2014-08-22 20:13:50 +0900830 usbhs_pipe_running(pipe, 1);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900831 usbhsf_dma_start(pipe, fifo);
Yoshihiro Shimoda9b53d9a2015-03-12 15:35:19 +0900832 usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->trans);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900833 dma_async_issue_pending(chan);
Yoshihiro Shimoda9b53d9a2015-03-12 15:35:19 +0900834 usbhs_pipe_enable(pipe);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900835}
836
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700837/*
838 * DMA push handler
839 */
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900840static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done)
841{
842 struct usbhs_pipe *pipe = pkt->pipe;
843 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
844 struct usbhs_fifo *fifo;
845 int len = pkt->length - pkt->actual;
846 int ret;
Yoshihiro Shimodaab330cf2015-03-12 15:35:20 +0900847 uintptr_t align_mask;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900848
849 if (usbhs_pipe_is_busy(pipe))
850 return 0;
851
852 /* use PIO if packet is less than pio_dma_border or pipe is DCP */
853 if ((len < usbhs_get_dparam(priv, pio_dma_border)) ||
854 usbhs_pipe_is_dcp(pipe))
855 goto usbhsf_pio_prepare_push;
856
Yoshihiro Shimodaab330cf2015-03-12 15:35:20 +0900857 /* check data length if this driver don't use USB-DMAC */
858 if (!usbhs_get_dparam(priv, has_usb_dmac) && len & 0x7)
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900859 goto usbhsf_pio_prepare_push;
860
Yoshihiro Shimodaab330cf2015-03-12 15:35:20 +0900861 /* check buffer alignment */
862 align_mask = usbhs_get_dparam(priv, has_usb_dmac) ?
863 USBHS_USB_DMAC_XFER_SIZE - 1 : 0x7;
864 if ((uintptr_t)(pkt->buf + pkt->actual) & align_mask)
Kuninori Morimoto9a12d092011-07-03 17:42:19 -0700865 goto usbhsf_pio_prepare_push;
866
Yoshihiro Shimoda8355b2b2014-08-22 20:13:50 +0900867 /* return at this time if the pipe is running */
868 if (usbhs_pipe_is_running(pipe))
869 return 0;
870
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900871 /* get enable DMA fifo */
872 fifo = usbhsf_get_dma_fifo(priv, pkt);
873 if (!fifo)
874 goto usbhsf_pio_prepare_push;
875
876 if (usbhsf_dma_map(pkt) < 0)
877 goto usbhsf_pio_prepare_push;
878
879 ret = usbhsf_fifo_select(pipe, fifo, 0);
880 if (ret < 0)
881 goto usbhsf_pio_prepare_push_unmap;
882
883 pkt->trans = len;
884
Guennadi Liakhovetski6e4b74e2012-02-14 11:37:21 +0100885 INIT_WORK(&pkt->work, xfer_work);
886 schedule_work(&pkt->work);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900887
888 return 0;
889
890usbhsf_pio_prepare_push_unmap:
891 usbhsf_dma_unmap(pkt);
892usbhsf_pio_prepare_push:
893 /*
894 * change handler to PIO
895 */
896 pkt->handler = &usbhs_fifo_pio_push_handler;
897
898 return pkt->handler->prepare(pkt, is_done);
899}
900
901static int usbhsf_dma_push_done(struct usbhs_pkt *pkt, int *is_done)
902{
903 struct usbhs_pipe *pipe = pkt->pipe;
Yoshihiro Shimodac0ed8b22014-08-22 20:14:10 +0900904 int is_short = pkt->trans % usbhs_pipe_get_maxpacket(pipe);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900905
Yoshihiro Shimodac0ed8b22014-08-22 20:14:10 +0900906 pkt->actual += pkt->trans;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900907
Yoshihiro Shimodac0ed8b22014-08-22 20:14:10 +0900908 if (pkt->actual < pkt->length)
909 *is_done = 0; /* there are remainder data */
910 else if (is_short)
911 *is_done = 1; /* short packet */
912 else
913 *is_done = !pkt->zero; /* send zero packet? */
914
Yoshihiro Shimoda8355b2b2014-08-22 20:13:50 +0900915 usbhs_pipe_running(pipe, !*is_done);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900916
917 usbhsf_dma_stop(pipe, pipe->fifo);
918 usbhsf_dma_unmap(pkt);
919 usbhsf_fifo_unselect(pipe, pipe->fifo);
920
Yoshihiro Shimodac0ed8b22014-08-22 20:14:10 +0900921 if (!*is_done) {
922 /* change handler to PIO */
923 pkt->handler = &usbhs_fifo_pio_push_handler;
924 return pkt->handler->try_run(pkt, is_done);
925 }
926
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900927 return 0;
928}
929
930struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = {
931 .prepare = usbhsf_dma_prepare_push,
932 .dma_done = usbhsf_dma_push_done,
933};
934
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700935/*
936 * DMA pop handler
937 */
Yoshihiro Shimodaab330cf2015-03-12 15:35:20 +0900938
939static int usbhsf_dma_prepare_pop_with_rx_irq(struct usbhs_pkt *pkt,
940 int *is_done)
941{
942 return usbhsf_prepare_pop(pkt, is_done);
943}
944
945static int usbhsf_dma_prepare_pop_with_usb_dmac(struct usbhs_pkt *pkt,
946 int *is_done)
947{
948 struct usbhs_pipe *pipe = pkt->pipe;
949 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
950 struct usbhs_fifo *fifo;
951 int ret;
952
953 if (usbhs_pipe_is_busy(pipe))
954 return 0;
955
956 /* use PIO if packet is less than pio_dma_border or pipe is DCP */
957 if ((pkt->length < usbhs_get_dparam(priv, pio_dma_border)) ||
958 usbhs_pipe_is_dcp(pipe))
959 goto usbhsf_pio_prepare_pop;
960
961 fifo = usbhsf_get_dma_fifo(priv, pkt);
962 if (!fifo)
963 goto usbhsf_pio_prepare_pop;
964
965 if ((uintptr_t)pkt->buf & (USBHS_USB_DMAC_XFER_SIZE - 1))
966 goto usbhsf_pio_prepare_pop;
967
968 usbhs_pipe_config_change_bfre(pipe, 1);
969
970 ret = usbhsf_fifo_select(pipe, fifo, 0);
971 if (ret < 0)
972 goto usbhsf_pio_prepare_pop;
973
974 if (usbhsf_dma_map(pkt) < 0)
975 goto usbhsf_pio_prepare_pop_unselect;
976
977 /* DMA */
978
979 /*
980 * usbhs_fifo_dma_pop_handler :: prepare
981 * enabled irq to come here.
982 * but it is no longer needed for DMA. disable it.
983 */
984 usbhsf_rx_irq_ctrl(pipe, 0);
985
986 pkt->trans = pkt->length;
987
988 INIT_WORK(&pkt->work, xfer_work);
989 schedule_work(&pkt->work);
990
991 return 0;
992
993usbhsf_pio_prepare_pop_unselect:
994 usbhsf_fifo_unselect(pipe, fifo);
995usbhsf_pio_prepare_pop:
996
997 /*
998 * change handler to PIO
999 */
1000 pkt->handler = &usbhs_fifo_pio_pop_handler;
1001 usbhs_pipe_config_change_bfre(pipe, 0);
1002
1003 return pkt->handler->prepare(pkt, is_done);
1004}
1005
1006static int usbhsf_dma_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
1007{
1008 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1009
1010 if (usbhs_get_dparam(priv, has_usb_dmac))
1011 return usbhsf_dma_prepare_pop_with_usb_dmac(pkt, is_done);
1012 else
1013 return usbhsf_dma_prepare_pop_with_rx_irq(pkt, is_done);
1014}
1015
1016static int usbhsf_dma_try_pop_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001017{
1018 struct usbhs_pipe *pipe = pkt->pipe;
1019 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1020 struct usbhs_fifo *fifo;
1021 int len, ret;
1022
1023 if (usbhs_pipe_is_busy(pipe))
1024 return 0;
1025
1026 if (usbhs_pipe_is_dcp(pipe))
1027 goto usbhsf_pio_prepare_pop;
1028
1029 /* get enable DMA fifo */
1030 fifo = usbhsf_get_dma_fifo(priv, pkt);
1031 if (!fifo)
1032 goto usbhsf_pio_prepare_pop;
1033
Kuninori Morimotoc9ae0c92011-10-26 01:21:07 -07001034 if ((uintptr_t)(pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
Kuninori Morimoto9a12d092011-07-03 17:42:19 -07001035 goto usbhsf_pio_prepare_pop;
1036
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001037 ret = usbhsf_fifo_select(pipe, fifo, 0);
1038 if (ret < 0)
1039 goto usbhsf_pio_prepare_pop;
1040
1041 /* use PIO if packet is less than pio_dma_border */
1042 len = usbhsf_fifo_rcv_len(priv, fifo);
1043 len = min(pkt->length - pkt->actual, len);
Kuninori Morimoto77975ee2012-08-22 18:01:13 -07001044 if (len & 0x7) /* 8byte alignment */
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001045 goto usbhsf_pio_prepare_pop_unselect;
1046
1047 if (len < usbhs_get_dparam(priv, pio_dma_border))
1048 goto usbhsf_pio_prepare_pop_unselect;
1049
1050 ret = usbhsf_fifo_barrier(priv, fifo);
1051 if (ret < 0)
1052 goto usbhsf_pio_prepare_pop_unselect;
1053
1054 if (usbhsf_dma_map(pkt) < 0)
1055 goto usbhsf_pio_prepare_pop_unselect;
1056
1057 /* DMA */
1058
1059 /*
1060 * usbhs_fifo_dma_pop_handler :: prepare
1061 * enabled irq to come here.
1062 * but it is no longer needed for DMA. disable it.
1063 */
1064 usbhsf_rx_irq_ctrl(pipe, 0);
1065
1066 pkt->trans = len;
1067
Guennadi Liakhovetski6e4b74e2012-02-14 11:37:21 +01001068 INIT_WORK(&pkt->work, xfer_work);
1069 schedule_work(&pkt->work);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001070
1071 return 0;
1072
1073usbhsf_pio_prepare_pop_unselect:
1074 usbhsf_fifo_unselect(pipe, fifo);
1075usbhsf_pio_prepare_pop:
1076
1077 /*
1078 * change handler to PIO
1079 */
1080 pkt->handler = &usbhs_fifo_pio_pop_handler;
1081
1082 return pkt->handler->try_run(pkt, is_done);
1083}
1084
Yoshihiro Shimodaab330cf2015-03-12 15:35:20 +09001085static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done)
1086{
1087 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1088
1089 BUG_ON(usbhs_get_dparam(priv, has_usb_dmac));
1090
1091 return usbhsf_dma_try_pop_with_rx_irq(pkt, is_done);
1092}
1093
1094static int usbhsf_dma_pop_done_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001095{
1096 struct usbhs_pipe *pipe = pkt->pipe;
1097 int maxp = usbhs_pipe_get_maxpacket(pipe);
1098
1099 usbhsf_dma_stop(pipe, pipe->fifo);
1100 usbhsf_dma_unmap(pkt);
1101 usbhsf_fifo_unselect(pipe, pipe->fifo);
1102
1103 pkt->actual += pkt->trans;
1104
1105 if ((pkt->actual == pkt->length) || /* receive all data */
1106 (pkt->trans < maxp)) { /* short packet */
1107 *is_done = 1;
Yoshihiro Shimoda8355b2b2014-08-22 20:13:50 +09001108 usbhs_pipe_running(pipe, 0);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001109 } else {
1110 /* re-enable */
Yoshihiro Shimoda8355b2b2014-08-22 20:13:50 +09001111 usbhs_pipe_running(pipe, 0);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001112 usbhsf_prepare_pop(pkt, is_done);
1113 }
1114
1115 return 0;
1116}
1117
Yoshihiro Shimodaab330cf2015-03-12 15:35:20 +09001118static size_t usbhs_dma_calc_received_size(struct usbhs_pkt *pkt,
1119 struct dma_chan *chan, int dtln)
1120{
1121 struct usbhs_pipe *pipe = pkt->pipe;
1122 struct dma_tx_state state;
1123 size_t received_size;
1124 int maxp = usbhs_pipe_get_maxpacket(pipe);
1125
1126 dmaengine_tx_status(chan, pkt->cookie, &state);
1127 received_size = pkt->length - state.residue;
1128
1129 if (dtln) {
1130 received_size -= USBHS_USB_DMAC_XFER_SIZE;
1131 received_size &= ~(maxp - 1);
1132 received_size += dtln;
1133 }
1134
1135 return received_size;
1136}
1137
1138static int usbhsf_dma_pop_done_with_usb_dmac(struct usbhs_pkt *pkt,
1139 int *is_done)
1140{
1141 struct usbhs_pipe *pipe = pkt->pipe;
1142 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1143 struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
1144 struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
1145 int rcv_len;
1146
1147 /*
1148 * Since the driver disables rx_irq in DMA mode, the interrupt handler
1149 * cannot the BRDYSTS. So, the function clears it here because the
1150 * driver may use PIO mode next time.
1151 */
1152 usbhs_xxxsts_clear(priv, BRDYSTS, usbhs_pipe_number(pipe));
1153
1154 rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
1155 usbhsf_fifo_clear(pipe, fifo);
1156 pkt->actual = usbhs_dma_calc_received_size(pkt, chan, rcv_len);
1157
1158 usbhsf_dma_stop(pipe, fifo);
1159 usbhsf_dma_unmap(pkt);
1160 usbhsf_fifo_unselect(pipe, pipe->fifo);
1161
1162 /* The driver can assume the rx transaction is always "done" */
1163 *is_done = 1;
1164
1165 return 0;
1166}
1167
1168static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done)
1169{
1170 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1171
1172 if (usbhs_get_dparam(priv, has_usb_dmac))
1173 return usbhsf_dma_pop_done_with_usb_dmac(pkt, is_done);
1174 else
1175 return usbhsf_dma_pop_done_with_rx_irq(pkt, is_done);
1176}
1177
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001178struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = {
Yoshihiro Shimodaab330cf2015-03-12 15:35:20 +09001179 .prepare = usbhsf_dma_prepare_pop,
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001180 .try_run = usbhsf_dma_try_pop,
1181 .dma_done = usbhsf_dma_pop_done
1182};
1183
1184/*
1185 * DMA setting
1186 */
1187static bool usbhsf_dma_filter(struct dma_chan *chan, void *param)
1188{
1189 struct sh_dmae_slave *slave = param;
1190
1191 /*
1192 * FIXME
1193 *
1194 * usbhs doesn't recognize id = 0 as valid DMA
1195 */
Guennadi Liakhovetskif19b7e02012-05-09 17:09:19 +02001196 if (0 == slave->shdma_slave.slave_id)
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001197 return false;
1198
1199 chan->private = slave;
1200
1201 return true;
1202}
1203
1204static void usbhsf_dma_quit(struct usbhs_priv *priv, struct usbhs_fifo *fifo)
1205{
1206 if (fifo->tx_chan)
1207 dma_release_channel(fifo->tx_chan);
1208 if (fifo->rx_chan)
1209 dma_release_channel(fifo->rx_chan);
1210
1211 fifo->tx_chan = NULL;
1212 fifo->rx_chan = NULL;
1213}
1214
Yoshihiro Shimoda6e3f53a2015-01-19 12:53:16 +09001215static void usbhsf_dma_init_pdev(struct usbhs_fifo *fifo)
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001216{
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001217 dma_cap_mask_t mask;
1218
1219 dma_cap_zero(mask);
1220 dma_cap_set(DMA_SLAVE, mask);
1221 fifo->tx_chan = dma_request_channel(mask, usbhsf_dma_filter,
1222 &fifo->tx_slave);
1223
1224 dma_cap_zero(mask);
1225 dma_cap_set(DMA_SLAVE, mask);
1226 fifo->rx_chan = dma_request_channel(mask, usbhsf_dma_filter,
1227 &fifo->rx_slave);
Yoshihiro Shimoda6e3f53a2015-01-19 12:53:16 +09001228}
1229
Yoshihiro Shimoda7a96b782015-03-12 15:35:18 +09001230static void usbhsf_dma_init_dt(struct device *dev, struct usbhs_fifo *fifo,
1231 int channel)
Yoshihiro Shimodaabd2dbf2015-01-19 12:53:17 +09001232{
Yoshihiro Shimoda7a96b782015-03-12 15:35:18 +09001233 char name[16];
1234
Yoshihiro Shimoda7cc99f12015-04-08 19:42:24 +09001235 /*
1236 * To avoid complex handing for DnFIFOs, the driver uses each
1237 * DnFIFO as TX or RX direction (not bi-direction).
1238 * So, the driver uses odd channels for TX, even channels for RX.
1239 */
1240 snprintf(name, sizeof(name), "ch%d", channel);
1241 if (channel & 1) {
1242 fifo->tx_chan = dma_request_slave_channel_reason(dev, name);
1243 if (IS_ERR(fifo->tx_chan))
1244 fifo->tx_chan = NULL;
1245 } else {
1246 fifo->rx_chan = dma_request_slave_channel_reason(dev, name);
1247 if (IS_ERR(fifo->rx_chan))
1248 fifo->rx_chan = NULL;
1249 }
Yoshihiro Shimodaabd2dbf2015-01-19 12:53:17 +09001250}
1251
Yoshihiro Shimoda7a96b782015-03-12 15:35:18 +09001252static void usbhsf_dma_init(struct usbhs_priv *priv, struct usbhs_fifo *fifo,
1253 int channel)
Yoshihiro Shimoda6e3f53a2015-01-19 12:53:16 +09001254{
1255 struct device *dev = usbhs_priv_to_dev(priv);
1256
Yoshihiro Shimodaabd2dbf2015-01-19 12:53:17 +09001257 if (dev->of_node)
Yoshihiro Shimoda7a96b782015-03-12 15:35:18 +09001258 usbhsf_dma_init_dt(dev, fifo, channel);
Yoshihiro Shimodaabd2dbf2015-01-19 12:53:17 +09001259 else
1260 usbhsf_dma_init_pdev(fifo);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001261
1262 if (fifo->tx_chan || fifo->rx_chan)
Kuninori Morimoto4ce68802011-06-21 09:33:43 +09001263 dev_dbg(dev, "enable DMAEngine (%s%s%s)\n",
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001264 fifo->name,
1265 fifo->tx_chan ? "[TX]" : " ",
1266 fifo->rx_chan ? "[RX]" : " ");
1267}
1268
1269/*
Kuninori Morimotodad67392011-06-06 14:18:28 +09001270 * irq functions
1271 */
1272static int usbhsf_irq_empty(struct usbhs_priv *priv,
1273 struct usbhs_irq_state *irq_state)
1274{
1275 struct usbhs_pipe *pipe;
Kuninori Morimotodad67392011-06-06 14:18:28 +09001276 struct device *dev = usbhs_priv_to_dev(priv);
1277 int i, ret;
1278
1279 if (!irq_state->bempsts) {
1280 dev_err(dev, "debug %s !!\n", __func__);
1281 return -EIO;
1282 }
1283
1284 dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
1285
1286 /*
1287 * search interrupted "pipe"
1288 * not "uep".
1289 */
1290 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
1291 if (!(irq_state->bempsts & (1 << i)))
1292 continue;
1293
Kuninori Morimoto51b8a022011-10-10 21:58:45 -07001294 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
Kuninori Morimotodad67392011-06-06 14:18:28 +09001295 if (ret < 0)
1296 dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
1297 }
1298
1299 return 0;
1300}
1301
1302static int usbhsf_irq_ready(struct usbhs_priv *priv,
1303 struct usbhs_irq_state *irq_state)
1304{
1305 struct usbhs_pipe *pipe;
Kuninori Morimotodad67392011-06-06 14:18:28 +09001306 struct device *dev = usbhs_priv_to_dev(priv);
1307 int i, ret;
1308
1309 if (!irq_state->brdysts) {
1310 dev_err(dev, "debug %s !!\n", __func__);
1311 return -EIO;
1312 }
1313
1314 dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
1315
1316 /*
1317 * search interrupted "pipe"
1318 * not "uep".
1319 */
1320 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
1321 if (!(irq_state->brdysts & (1 << i)))
1322 continue;
1323
Kuninori Morimoto51b8a022011-10-10 21:58:45 -07001324 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
Kuninori Morimotodad67392011-06-06 14:18:28 +09001325 if (ret < 0)
1326 dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
1327 }
1328
1329 return 0;
1330}
1331
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001332static void usbhsf_dma_complete(void *arg)
1333{
1334 struct usbhs_pipe *pipe = arg;
1335 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1336 struct device *dev = usbhs_priv_to_dev(priv);
1337 int ret;
1338
Kuninori Morimoto51b8a022011-10-10 21:58:45 -07001339 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_DMA_DONE);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001340 if (ret < 0)
1341 dev_err(dev, "dma_complete run_error %d : %d\n",
1342 usbhs_pipe_number(pipe), ret);
1343}
1344
Yoshihiro Shimodacdeb7942014-11-04 10:05:45 +09001345void usbhs_fifo_clear_dcp(struct usbhs_pipe *pipe)
1346{
1347 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1348 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
1349
1350 /* clear DCP FIFO of transmission */
1351 if (usbhsf_fifo_select(pipe, fifo, 1) < 0)
1352 return;
1353 usbhsf_fifo_clear(pipe, fifo);
1354 usbhsf_fifo_unselect(pipe, fifo);
1355
1356 /* clear DCP FIFO of reception */
1357 if (usbhsf_fifo_select(pipe, fifo, 0) < 0)
1358 return;
1359 usbhsf_fifo_clear(pipe, fifo);
1360 usbhsf_fifo_unselect(pipe, fifo);
1361}
1362
Kuninori Morimotodad67392011-06-06 14:18:28 +09001363/*
1364 * fifo init
1365 */
1366void usbhs_fifo_init(struct usbhs_priv *priv)
1367{
1368 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
Kuninori Morimotod77e3f42011-06-06 14:18:50 +09001369 struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
Yoshihiro Shimoda3a2634a2014-11-10 20:02:45 +09001370 struct usbhs_fifo *dfifo;
1371 int i;
Kuninori Morimotodad67392011-06-06 14:18:28 +09001372
1373 mod->irq_empty = usbhsf_irq_empty;
1374 mod->irq_ready = usbhsf_irq_ready;
1375 mod->irq_bempsts = 0;
1376 mod->irq_brdysts = 0;
Kuninori Morimotod77e3f42011-06-06 14:18:50 +09001377
1378 cfifo->pipe = NULL;
Yoshihiro Shimoda3a2634a2014-11-10 20:02:45 +09001379 usbhs_for_each_dfifo(priv, dfifo, i)
1380 dfifo->pipe = NULL;
Kuninori Morimotodad67392011-06-06 14:18:28 +09001381}
1382
1383void usbhs_fifo_quit(struct usbhs_priv *priv)
1384{
1385 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1386
1387 mod->irq_empty = NULL;
1388 mod->irq_ready = NULL;
1389 mod->irq_bempsts = 0;
1390 mod->irq_brdysts = 0;
1391}
Kuninori Morimotod3af90a2011-06-06 14:18:44 +09001392
Yoshihiro Shimoda53e734b2014-11-10 20:02:46 +09001393#define __USBHS_DFIFO_INIT(priv, fifo, channel, fifo_port) \
Yoshihiro Shimoda3a2634a2014-11-10 20:02:45 +09001394do { \
1395 fifo = usbhsf_get_dnfifo(priv, channel); \
1396 fifo->name = "D"#channel"FIFO"; \
Yoshihiro Shimoda53e734b2014-11-10 20:02:46 +09001397 fifo->port = fifo_port; \
Yoshihiro Shimoda3a2634a2014-11-10 20:02:45 +09001398 fifo->sel = D##channel##FIFOSEL; \
1399 fifo->ctr = D##channel##FIFOCTR; \
1400 fifo->tx_slave.shdma_slave.slave_id = \
1401 usbhs_get_dparam(priv, d##channel##_tx_id); \
1402 fifo->rx_slave.shdma_slave.slave_id = \
1403 usbhs_get_dparam(priv, d##channel##_rx_id); \
Yoshihiro Shimoda7a96b782015-03-12 15:35:18 +09001404 usbhsf_dma_init(priv, fifo, channel); \
Yoshihiro Shimoda3a2634a2014-11-10 20:02:45 +09001405} while (0)
1406
Yoshihiro Shimoda53e734b2014-11-10 20:02:46 +09001407#define USBHS_DFIFO_INIT(priv, fifo, channel) \
1408 __USBHS_DFIFO_INIT(priv, fifo, channel, D##channel##FIFO)
1409#define USBHS_DFIFO_INIT_NO_PORT(priv, fifo, channel) \
1410 __USBHS_DFIFO_INIT(priv, fifo, channel, 0)
1411
Kuninori Morimotod3af90a2011-06-06 14:18:44 +09001412int usbhs_fifo_probe(struct usbhs_priv *priv)
1413{
1414 struct usbhs_fifo *fifo;
1415
1416 /* CFIFO */
1417 fifo = usbhsf_get_cfifo(priv);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001418 fifo->name = "CFIFO";
Kuninori Morimotod3af90a2011-06-06 14:18:44 +09001419 fifo->port = CFIFO;
1420 fifo->sel = CFIFOSEL;
1421 fifo->ctr = CFIFOCTR;
1422
Yoshihiro Shimoda3a2634a2014-11-10 20:02:45 +09001423 /* DFIFO */
1424 USBHS_DFIFO_INIT(priv, fifo, 0);
1425 USBHS_DFIFO_INIT(priv, fifo, 1);
Yoshihiro Shimodad3cf6a42014-11-10 20:02:47 +09001426 USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 2);
1427 USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 3);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +09001428
Kuninori Morimotod3af90a2011-06-06 14:18:44 +09001429 return 0;
1430}
1431
1432void usbhs_fifo_remove(struct usbhs_priv *priv)
1433{
Yoshihiro Shimoda3a2634a2014-11-10 20:02:45 +09001434 struct usbhs_fifo *fifo;
1435 int i;
1436
1437 usbhs_for_each_dfifo(priv, fifo, i)
1438 usbhsf_dma_quit(priv, fifo);
Kuninori Morimotod3af90a2011-06-06 14:18:44 +09001439}