blob: 53e2b35dd3259c3af7fb14f77bc2c86c867f0373 [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>
19#include "./common.h"
20#include "./pipe.h"
21
Kuninori Morimotod3af90a2011-06-06 14:18:44 +090022#define usbhsf_get_cfifo(p) (&((p)->fifo_info.cfifo))
23
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +090024/*
Kuninori Morimoto4bd04812011-06-06 14:18:07 +090025 * packet info function
26 */
Kuninori Morimoto97664a22011-06-06 14:18:38 +090027static int usbhsf_null_handle(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotodad67392011-06-06 14:18:28 +090028{
29 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
30 struct device *dev = usbhs_priv_to_dev(priv);
31
32 dev_err(dev, "null handler\n");
33
34 return -EINVAL;
35}
36
37static struct usbhs_pkt_handle usbhsf_null_handler = {
38 .prepare = usbhsf_null_handle,
39 .try_run = usbhsf_null_handle,
40};
41
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090042void usbhs_pkt_init(struct usbhs_pkt *pkt)
Kuninori Morimoto4bd04812011-06-06 14:18:07 +090043{
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090044 INIT_LIST_HEAD(&pkt->node);
45}
46
Kuninori Morimoto659d4952011-06-06 14:18:23 +090047void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt,
Kuninori Morimotodad67392011-06-06 14:18:28 +090048 struct usbhs_pkt_handle *handler,
Kuninori Morimoto659d4952011-06-06 14:18:23 +090049 void *buf, int len, int zero)
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090050{
Kuninori Morimotodad67392011-06-06 14:18:28 +090051 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
52 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto97664a22011-06-06 14:18:38 +090053 unsigned long flags;
54
55 /******************** spin lock ********************/
56 usbhs_lock(priv, flags);
Kuninori Morimotodad67392011-06-06 14:18:28 +090057
58 if (!handler) {
59 dev_err(dev, "no handler function\n");
60 handler = &usbhsf_null_handler;
61 }
62
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090063 list_del_init(&pkt->node);
64 list_add_tail(&pkt->node, &pipe->list);
65
Kuninori Morimoto659d4952011-06-06 14:18:23 +090066 pkt->pipe = pipe;
67 pkt->buf = buf;
Kuninori Morimotodad67392011-06-06 14:18:28 +090068 pkt->handler = handler;
Kuninori Morimoto659d4952011-06-06 14:18:23 +090069 pkt->length = len;
70 pkt->zero = zero;
71 pkt->actual = 0;
Kuninori Morimoto97664a22011-06-06 14:18:38 +090072
73 usbhs_unlock(priv, flags);
74 /******************** spin unlock ******************/
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090075}
76
Kuninori Morimoto97664a22011-06-06 14:18:38 +090077static void __usbhsf_pkt_del(struct usbhs_pkt *pkt)
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090078{
79 list_del_init(&pkt->node);
80}
81
Kuninori Morimoto97664a22011-06-06 14:18:38 +090082static struct usbhs_pkt *__usbhsf_pkt_get(struct usbhs_pipe *pipe)
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090083{
84 if (list_empty(&pipe->list))
85 return NULL;
86
87 return list_entry(pipe->list.next, struct usbhs_pkt, node);
88}
89
Kuninori Morimoto97664a22011-06-06 14:18:38 +090090struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt)
91{
92 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
93 unsigned long flags;
94
95 /******************** spin lock ********************/
96 usbhs_lock(priv, flags);
97
98 if (!pkt)
99 pkt = __usbhsf_pkt_get(pipe);
100
101 if (pkt)
102 __usbhsf_pkt_del(pkt);
103
104 usbhs_unlock(priv, flags);
105 /******************** spin unlock ******************/
106
107 return pkt;
108}
109
110int __usbhs_pkt_handler(struct usbhs_pipe *pipe, int type)
111{
112 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
113 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
114 struct usbhs_pkt *pkt;
115 struct device *dev = usbhs_priv_to_dev(priv);
116 int (*func)(struct usbhs_pkt *pkt, int *is_done);
117 unsigned long flags;
118 int ret = 0;
119 int is_done = 0;
120
121 /******************** spin lock ********************/
122 usbhs_lock(priv, flags);
123
124 pkt = __usbhsf_pkt_get(pipe);
125 if (!pkt)
126 goto __usbhs_pkt_handler_end;
127
128 switch (type) {
129 case USBHSF_PKT_PREPARE:
130 func = pkt->handler->prepare;
131 break;
132 case USBHSF_PKT_TRY_RUN:
133 func = pkt->handler->try_run;
134 break;
135 default:
136 dev_err(dev, "unknown pkt hander\n");
137 goto __usbhs_pkt_handler_end;
138 }
139
140 ret = func(pkt, &is_done);
141
142 if (is_done)
143 __usbhsf_pkt_del(pkt);
144
145__usbhs_pkt_handler_end:
146 usbhs_unlock(priv, flags);
147 /******************** spin unlock ******************/
148
149 if (is_done)
150 info->done(pkt);
151
152 return ret;
153}
154
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900155/*
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900156 * irq enable/disable function
157 */
158#define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, bempsts, e)
159#define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, brdysts, e)
160#define usbhsf_irq_callback_ctrl(pipe, status, enable) \
161 ({ \
162 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
163 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
164 u16 status = (1 << usbhs_pipe_number(pipe)); \
165 if (!mod) \
166 return; \
167 if (enable) \
168 mod->irq_##status |= status; \
169 else \
170 mod->irq_##status &= ~status; \
171 usbhs_irq_callback_update(priv, mod); \
172 })
173
174static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
175{
176 /*
177 * And DCP pipe can NOT use "ready interrupt" for "send"
178 * it should use "empty" interrupt.
179 * see
180 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
181 *
182 * on the other hand, normal pipe can use "ready interrupt" for "send"
183 * even though it is single/double buffer
184 */
185 if (usbhs_pipe_is_dcp(pipe))
186 usbhsf_irq_empty_ctrl(pipe, enable);
187 else
188 usbhsf_irq_ready_ctrl(pipe, enable);
189}
190
191static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
192{
193 usbhsf_irq_ready_ctrl(pipe, enable);
194}
195
196/*
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900197 * FIFO ctrl
198 */
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900199static void usbhsf_send_terminator(struct usbhs_pipe *pipe,
200 struct usbhs_fifo *fifo)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900201{
202 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
203
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900204 usbhs_bset(priv, fifo->ctr, BVAL, BVAL);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900205}
206
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900207static int usbhsf_fifo_barrier(struct usbhs_priv *priv,
208 struct usbhs_fifo *fifo)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900209{
210 int timeout = 1024;
211
212 do {
213 /* The FIFO port is accessible */
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900214 if (usbhs_read(priv, fifo->ctr) & FRDY)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900215 return 0;
216
217 udelay(10);
218 } while (timeout--);
219
220 return -EBUSY;
221}
222
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900223static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
224 struct usbhs_fifo *fifo)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900225{
226 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
227
228 if (!usbhs_pipe_is_dcp(pipe))
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900229 usbhsf_fifo_barrier(priv, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900230
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900231 usbhs_write(priv, fifo->ctr, BCLR);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900232}
233
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900234static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv,
235 struct usbhs_fifo *fifo)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900236{
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900237 return usbhs_read(priv, fifo->ctr) & DTLN_MASK;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900238}
239
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900240static int usbhsf_fifo_select(struct usbhs_pipe *pipe,
241 struct usbhs_fifo *fifo,
242 int write)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900243{
244 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
245 struct device *dev = usbhs_priv_to_dev(priv);
246 int timeout = 1024;
247 u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
248 u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
249
250 if (usbhs_pipe_is_dcp(pipe))
251 base |= (1 == write) << 5; /* ISEL */
252
253 /* "base" will be used below */
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900254 usbhs_write(priv, fifo->sel, base | MBW_32);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900255
256 /* check ISEL and CURPIPE value */
257 while (timeout--) {
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900258 if (base == (mask & usbhs_read(priv, fifo->sel)))
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900259 return 0;
260 udelay(10);
261 }
262
263 dev_err(dev, "fifo select error\n");
264
265 return -EIO;
266}
267
268/*
269 * PIO fifo functions
270 */
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900271static int usbhsf_try_push(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900272{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900273 struct usbhs_pipe *pipe = pkt->pipe;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900274 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900275 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900276 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
277 void __iomem *addr = priv->base + fifo->port;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900278 u8 *buf;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900279 int maxp = usbhs_pipe_get_maxpacket(pipe);
280 int total_len;
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900281 int i, ret, len;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900282 int is_short;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900283
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900284 ret = usbhsf_fifo_select(pipe, fifo, 1);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900285 if (ret < 0)
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900286 goto usbhs_fifo_write_busy;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900287
Kuninori Morimotodad67392011-06-06 14:18:28 +0900288 ret = usbhs_pipe_is_accessible(pipe);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900289 if (ret < 0)
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900290 goto usbhs_fifo_write_busy;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900291
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900292 ret = usbhsf_fifo_barrier(priv, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900293 if (ret < 0)
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900294 goto usbhs_fifo_write_busy;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900295
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900296 buf = pkt->buf + pkt->actual;
297 len = pkt->length - pkt->actual;
298 len = min(len, maxp);
299 total_len = len;
300 is_short = total_len < maxp;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900301
302 /*
303 * FIXME
304 *
305 * 32-bit access only
306 */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900307 if (len >= 4 && !((unsigned long)buf & 0x03)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900308 iowrite32_rep(addr, buf, len / 4);
309 len %= 4;
310 buf += total_len - len;
311 }
312
313 /* the rest operation */
314 for (i = 0; i < len; i++)
315 iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
316
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900317 /*
318 * variable update
319 */
320 pkt->actual += total_len;
321
322 if (pkt->actual < pkt->length)
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900323 *is_done = 0; /* there are remainder data */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900324 else if (is_short)
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900325 *is_done = 1; /* short packet */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900326 else
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900327 *is_done = !pkt->zero; /* send zero packet ? */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900328
329 /*
330 * pipe/irq handling
331 */
332 if (is_short)
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900333 usbhsf_send_terminator(pipe, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900334
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900335 usbhsf_tx_irq_ctrl(pipe, !*is_done);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900336 usbhs_pipe_enable(pipe);
337
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900338 dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
339 usbhs_pipe_number(pipe),
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900340 pkt->length, pkt->actual, *is_done, pkt->zero);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900341
342 /*
343 * Transmission end
344 */
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900345 if (*is_done) {
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900346 if (usbhs_pipe_is_dcp(pipe))
347 usbhs_dcp_control_transfer_done(pipe);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900348 }
349
350 return 0;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900351
352usbhs_fifo_write_busy:
353 /*
354 * pipe is busy.
355 * retry in interrupt
356 */
357 usbhsf_tx_irq_ctrl(pipe, 1);
358
359 return ret;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900360}
361
Kuninori Morimotodad67392011-06-06 14:18:28 +0900362struct usbhs_pkt_handle usbhs_fifo_push_handler = {
363 .prepare = usbhsf_try_push,
364 .try_run = usbhsf_try_push,
365};
366
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900367static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900368{
Kuninori Morimotodad67392011-06-06 14:18:28 +0900369 struct usbhs_pipe *pipe = pkt->pipe;
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900370 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
371 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900372 int ret;
373
374 /*
375 * select pipe and enable it to prepare packet receive
376 */
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900377 ret = usbhsf_fifo_select(pipe, fifo, 0);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900378 if (ret < 0)
379 return ret;
380
381 usbhs_pipe_enable(pipe);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900382 usbhsf_rx_irq_ctrl(pipe, 1);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900383
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900384 return 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900385}
386
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900387static int usbhsf_try_pop(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900388{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900389 struct usbhs_pipe *pipe = pkt->pipe;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900390 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900391 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900392 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
393 void __iomem *addr = priv->base + fifo->port;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900394 u8 *buf;
395 u32 data = 0;
396 int maxp = usbhs_pipe_get_maxpacket(pipe);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900397 int rcv_len, len;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900398 int i, ret;
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900399 int total_len = 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900400
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900401 ret = usbhsf_fifo_select(pipe, fifo, 0);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900402 if (ret < 0)
403 return ret;
404
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900405 ret = usbhsf_fifo_barrier(priv, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900406 if (ret < 0)
407 return ret;
408
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900409 rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900410
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900411 buf = pkt->buf + pkt->actual;
412 len = pkt->length - pkt->actual;
413 len = min(len, rcv_len);
414 total_len = len;
415
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900416 /*
417 * Buffer clear if Zero-Length packet
418 *
419 * see
420 * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
421 */
422 if (0 == rcv_len) {
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900423 usbhsf_fifo_clear(pipe, fifo);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900424 goto usbhs_fifo_read_end;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900425 }
426
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900427 /*
428 * FIXME
429 *
430 * 32-bit access only
431 */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900432 if (len >= 4 && !((unsigned long)buf & 0x03)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900433 ioread32_rep(addr, buf, len / 4);
434 len %= 4;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900435 buf += total_len - len;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900436 }
437
438 /* the rest operation */
439 for (i = 0; i < len; i++) {
440 if (!(i & 0x03))
441 data = ioread32(addr);
442
443 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
444 }
445
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900446 pkt->actual += total_len;
447
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900448usbhs_fifo_read_end:
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900449 if ((pkt->actual == pkt->length) || /* receive all data */
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900450 (total_len < maxp)) { /* short packet */
451 *is_done = 1;
452 usbhsf_rx_irq_ctrl(pipe, 0);
453 usbhs_pipe_disable(pipe);
454 }
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900455
456 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
457 usbhs_pipe_number(pipe),
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900458 pkt->length, pkt->actual, *is_done, pkt->zero);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900459
460 return 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900461}
Kuninori Morimotodad67392011-06-06 14:18:28 +0900462
463struct usbhs_pkt_handle usbhs_fifo_pop_handler = {
464 .prepare = usbhsf_prepare_pop,
465 .try_run = usbhsf_try_pop,
466};
467
468/*
469 * handler function
470 */
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900471static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotodad67392011-06-06 14:18:28 +0900472{
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900473 usbhs_dcp_control_transfer_done(pkt->pipe);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900474
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900475 *is_done = 1;
Kuninori Morimotodad67392011-06-06 14:18:28 +0900476
477 return 0;
478}
479
480struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
481 .prepare = usbhsf_ctrl_stage_end,
482 .try_run = usbhsf_ctrl_stage_end,
483};
484
485/*
486 * irq functions
487 */
488static int usbhsf_irq_empty(struct usbhs_priv *priv,
489 struct usbhs_irq_state *irq_state)
490{
491 struct usbhs_pipe *pipe;
Kuninori Morimotodad67392011-06-06 14:18:28 +0900492 struct device *dev = usbhs_priv_to_dev(priv);
493 int i, ret;
494
495 if (!irq_state->bempsts) {
496 dev_err(dev, "debug %s !!\n", __func__);
497 return -EIO;
498 }
499
500 dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
501
502 /*
503 * search interrupted "pipe"
504 * not "uep".
505 */
506 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
507 if (!(irq_state->bempsts & (1 << i)))
508 continue;
509
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900510 ret = usbhs_pkt_run(pipe);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900511 if (ret < 0)
512 dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
513 }
514
515 return 0;
516}
517
518static int usbhsf_irq_ready(struct usbhs_priv *priv,
519 struct usbhs_irq_state *irq_state)
520{
521 struct usbhs_pipe *pipe;
Kuninori Morimotodad67392011-06-06 14:18:28 +0900522 struct device *dev = usbhs_priv_to_dev(priv);
523 int i, ret;
524
525 if (!irq_state->brdysts) {
526 dev_err(dev, "debug %s !!\n", __func__);
527 return -EIO;
528 }
529
530 dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
531
532 /*
533 * search interrupted "pipe"
534 * not "uep".
535 */
536 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
537 if (!(irq_state->brdysts & (1 << i)))
538 continue;
539
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900540 ret = usbhs_pkt_run(pipe);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900541 if (ret < 0)
542 dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
543 }
544
545 return 0;
546}
547
548/*
549 * fifo init
550 */
551void usbhs_fifo_init(struct usbhs_priv *priv)
552{
553 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
554
555 mod->irq_empty = usbhsf_irq_empty;
556 mod->irq_ready = usbhsf_irq_ready;
557 mod->irq_bempsts = 0;
558 mod->irq_brdysts = 0;
559}
560
561void usbhs_fifo_quit(struct usbhs_priv *priv)
562{
563 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
564
565 mod->irq_empty = NULL;
566 mod->irq_ready = NULL;
567 mod->irq_bempsts = 0;
568 mod->irq_brdysts = 0;
569}
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900570
571int usbhs_fifo_probe(struct usbhs_priv *priv)
572{
573 struct usbhs_fifo *fifo;
574
575 /* CFIFO */
576 fifo = usbhsf_get_cfifo(priv);
577 fifo->port = CFIFO;
578 fifo->sel = CFIFOSEL;
579 fifo->ctr = CFIFOCTR;
580
581 return 0;
582}
583
584void usbhs_fifo_remove(struct usbhs_priv *priv)
585{
586}