blob: 8852423313a325b212eb4a6e20f1c0f1da0a5994 [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 Morimotod77e3f42011-06-06 14:18:50 +090024#define usbhsf_fifo_is_busy(f) ((f)->pipe) /* see usbhs_pipe_select_fifo */
25
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +090026/*
Kuninori Morimoto4bd04812011-06-06 14:18:07 +090027 * packet info function
28 */
Kuninori Morimoto97664a22011-06-06 14:18:38 +090029static int usbhsf_null_handle(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotodad67392011-06-06 14:18:28 +090030{
31 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
32 struct device *dev = usbhs_priv_to_dev(priv);
33
34 dev_err(dev, "null handler\n");
35
36 return -EINVAL;
37}
38
39static struct usbhs_pkt_handle usbhsf_null_handler = {
40 .prepare = usbhsf_null_handle,
41 .try_run = usbhsf_null_handle,
42};
43
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090044void usbhs_pkt_init(struct usbhs_pkt *pkt)
Kuninori Morimoto4bd04812011-06-06 14:18:07 +090045{
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090046 INIT_LIST_HEAD(&pkt->node);
47}
48
Kuninori Morimoto659d4952011-06-06 14:18:23 +090049void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt,
Kuninori Morimotodad67392011-06-06 14:18:28 +090050 struct usbhs_pkt_handle *handler,
Kuninori Morimoto659d4952011-06-06 14:18:23 +090051 void *buf, int len, int zero)
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090052{
Kuninori Morimotodad67392011-06-06 14:18:28 +090053 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
54 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto97664a22011-06-06 14:18:38 +090055 unsigned long flags;
56
57 /******************** spin lock ********************/
58 usbhs_lock(priv, flags);
Kuninori Morimotodad67392011-06-06 14:18:28 +090059
60 if (!handler) {
61 dev_err(dev, "no handler function\n");
62 handler = &usbhsf_null_handler;
63 }
64
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090065 list_del_init(&pkt->node);
66 list_add_tail(&pkt->node, &pipe->list);
67
Kuninori Morimoto659d4952011-06-06 14:18:23 +090068 pkt->pipe = pipe;
69 pkt->buf = buf;
Kuninori Morimotodad67392011-06-06 14:18:28 +090070 pkt->handler = handler;
Kuninori Morimoto659d4952011-06-06 14:18:23 +090071 pkt->length = len;
72 pkt->zero = zero;
73 pkt->actual = 0;
Kuninori Morimoto97664a22011-06-06 14:18:38 +090074
75 usbhs_unlock(priv, flags);
76 /******************** spin unlock ******************/
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090077}
78
Kuninori Morimoto97664a22011-06-06 14:18:38 +090079static void __usbhsf_pkt_del(struct usbhs_pkt *pkt)
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090080{
81 list_del_init(&pkt->node);
82}
83
Kuninori Morimoto97664a22011-06-06 14:18:38 +090084static struct usbhs_pkt *__usbhsf_pkt_get(struct usbhs_pipe *pipe)
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090085{
86 if (list_empty(&pipe->list))
87 return NULL;
88
89 return list_entry(pipe->list.next, struct usbhs_pkt, node);
90}
91
Kuninori Morimoto97664a22011-06-06 14:18:38 +090092struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt)
93{
94 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
95 unsigned long flags;
96
97 /******************** spin lock ********************/
98 usbhs_lock(priv, flags);
99
100 if (!pkt)
101 pkt = __usbhsf_pkt_get(pipe);
102
103 if (pkt)
104 __usbhsf_pkt_del(pkt);
105
106 usbhs_unlock(priv, flags);
107 /******************** spin unlock ******************/
108
109 return pkt;
110}
111
112int __usbhs_pkt_handler(struct usbhs_pipe *pipe, int type)
113{
114 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
115 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
116 struct usbhs_pkt *pkt;
117 struct device *dev = usbhs_priv_to_dev(priv);
118 int (*func)(struct usbhs_pkt *pkt, int *is_done);
119 unsigned long flags;
120 int ret = 0;
121 int is_done = 0;
122
123 /******************** spin lock ********************/
124 usbhs_lock(priv, flags);
125
126 pkt = __usbhsf_pkt_get(pipe);
127 if (!pkt)
128 goto __usbhs_pkt_handler_end;
129
130 switch (type) {
131 case USBHSF_PKT_PREPARE:
132 func = pkt->handler->prepare;
133 break;
134 case USBHSF_PKT_TRY_RUN:
135 func = pkt->handler->try_run;
136 break;
137 default:
138 dev_err(dev, "unknown pkt hander\n");
139 goto __usbhs_pkt_handler_end;
140 }
141
142 ret = func(pkt, &is_done);
143
144 if (is_done)
145 __usbhsf_pkt_del(pkt);
146
147__usbhs_pkt_handler_end:
148 usbhs_unlock(priv, flags);
149 /******************** spin unlock ******************/
150
151 if (is_done)
152 info->done(pkt);
153
154 return ret;
155}
156
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900157/*
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900158 * irq enable/disable function
159 */
160#define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, bempsts, e)
161#define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, brdysts, e)
162#define usbhsf_irq_callback_ctrl(pipe, status, enable) \
163 ({ \
164 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
165 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
166 u16 status = (1 << usbhs_pipe_number(pipe)); \
167 if (!mod) \
168 return; \
169 if (enable) \
170 mod->irq_##status |= status; \
171 else \
172 mod->irq_##status &= ~status; \
173 usbhs_irq_callback_update(priv, mod); \
174 })
175
176static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
177{
178 /*
179 * And DCP pipe can NOT use "ready interrupt" for "send"
180 * it should use "empty" interrupt.
181 * see
182 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
183 *
184 * on the other hand, normal pipe can use "ready interrupt" for "send"
185 * even though it is single/double buffer
186 */
187 if (usbhs_pipe_is_dcp(pipe))
188 usbhsf_irq_empty_ctrl(pipe, enable);
189 else
190 usbhsf_irq_ready_ctrl(pipe, enable);
191}
192
193static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
194{
195 usbhsf_irq_ready_ctrl(pipe, enable);
196}
197
198/*
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900199 * FIFO ctrl
200 */
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900201static void usbhsf_send_terminator(struct usbhs_pipe *pipe,
202 struct usbhs_fifo *fifo)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900203{
204 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
205
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900206 usbhs_bset(priv, fifo->ctr, BVAL, BVAL);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900207}
208
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900209static int usbhsf_fifo_barrier(struct usbhs_priv *priv,
210 struct usbhs_fifo *fifo)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900211{
212 int timeout = 1024;
213
214 do {
215 /* The FIFO port is accessible */
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900216 if (usbhs_read(priv, fifo->ctr) & FRDY)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900217 return 0;
218
219 udelay(10);
220 } while (timeout--);
221
222 return -EBUSY;
223}
224
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900225static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
226 struct usbhs_fifo *fifo)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900227{
228 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
229
230 if (!usbhs_pipe_is_dcp(pipe))
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900231 usbhsf_fifo_barrier(priv, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900232
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900233 usbhs_write(priv, fifo->ctr, BCLR);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900234}
235
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900236static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv,
237 struct usbhs_fifo *fifo)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900238{
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900239 return usbhs_read(priv, fifo->ctr) & DTLN_MASK;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900240}
241
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900242static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
243 struct usbhs_fifo *fifo)
244{
245 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
246
247 usbhs_pipe_select_fifo(pipe, NULL);
248 usbhs_write(priv, fifo->sel, 0);
249}
250
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900251static int usbhsf_fifo_select(struct usbhs_pipe *pipe,
252 struct usbhs_fifo *fifo,
253 int write)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900254{
255 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
256 struct device *dev = usbhs_priv_to_dev(priv);
257 int timeout = 1024;
258 u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
259 u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
260
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900261 if (usbhs_pipe_is_busy(pipe) ||
262 usbhsf_fifo_is_busy(fifo))
263 return -EBUSY;
264
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900265 if (usbhs_pipe_is_dcp(pipe))
266 base |= (1 == write) << 5; /* ISEL */
267
268 /* "base" will be used below */
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900269 usbhs_write(priv, fifo->sel, base | MBW_32);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900270
271 /* check ISEL and CURPIPE value */
272 while (timeout--) {
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900273 if (base == (mask & usbhs_read(priv, fifo->sel))) {
274 usbhs_pipe_select_fifo(pipe, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900275 return 0;
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900276 }
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900277 udelay(10);
278 }
279
280 dev_err(dev, "fifo select error\n");
281
282 return -EIO;
283}
284
285/*
286 * PIO fifo functions
287 */
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900288static int usbhsf_try_push(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900289{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900290 struct usbhs_pipe *pipe = pkt->pipe;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900291 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900292 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900293 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
294 void __iomem *addr = priv->base + fifo->port;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900295 u8 *buf;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900296 int maxp = usbhs_pipe_get_maxpacket(pipe);
297 int total_len;
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900298 int i, ret, len;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900299 int is_short;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900300
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900301 ret = usbhsf_fifo_select(pipe, fifo, 1);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900302 if (ret < 0)
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900303 return 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900304
Kuninori Morimotodad67392011-06-06 14:18:28 +0900305 ret = usbhs_pipe_is_accessible(pipe);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900306 if (ret < 0)
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900307 goto usbhs_fifo_write_busy;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900308
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900309 ret = usbhsf_fifo_barrier(priv, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900310 if (ret < 0)
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900311 goto usbhs_fifo_write_busy;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900312
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900313 buf = pkt->buf + pkt->actual;
314 len = pkt->length - pkt->actual;
315 len = min(len, maxp);
316 total_len = len;
317 is_short = total_len < maxp;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900318
319 /*
320 * FIXME
321 *
322 * 32-bit access only
323 */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900324 if (len >= 4 && !((unsigned long)buf & 0x03)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900325 iowrite32_rep(addr, buf, len / 4);
326 len %= 4;
327 buf += total_len - len;
328 }
329
330 /* the rest operation */
331 for (i = 0; i < len; i++)
332 iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
333
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900334 /*
335 * variable update
336 */
337 pkt->actual += total_len;
338
339 if (pkt->actual < pkt->length)
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900340 *is_done = 0; /* there are remainder data */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900341 else if (is_short)
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900342 *is_done = 1; /* short packet */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900343 else
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900344 *is_done = !pkt->zero; /* send zero packet ? */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900345
346 /*
347 * pipe/irq handling
348 */
349 if (is_short)
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900350 usbhsf_send_terminator(pipe, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900351
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900352 usbhsf_tx_irq_ctrl(pipe, !*is_done);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900353 usbhs_pipe_enable(pipe);
354
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900355 dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
356 usbhs_pipe_number(pipe),
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900357 pkt->length, pkt->actual, *is_done, pkt->zero);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900358
359 /*
360 * Transmission end
361 */
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900362 if (*is_done) {
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900363 if (usbhs_pipe_is_dcp(pipe))
364 usbhs_dcp_control_transfer_done(pipe);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900365 }
366
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900367 usbhsf_fifo_unselect(pipe, fifo);
368
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900369 return 0;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900370
371usbhs_fifo_write_busy:
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900372 usbhsf_fifo_unselect(pipe, fifo);
373
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900374 /*
375 * pipe is busy.
376 * retry in interrupt
377 */
378 usbhsf_tx_irq_ctrl(pipe, 1);
379
380 return ret;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900381}
382
Kuninori Morimotodad67392011-06-06 14:18:28 +0900383struct usbhs_pkt_handle usbhs_fifo_push_handler = {
384 .prepare = usbhsf_try_push,
385 .try_run = usbhsf_try_push,
386};
387
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900388static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900389{
Kuninori Morimotodad67392011-06-06 14:18:28 +0900390 struct usbhs_pipe *pipe = pkt->pipe;
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900391
392 if (usbhs_pipe_is_busy(pipe))
393 return 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900394
395 /*
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900396 * pipe enable to prepare packet receive
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900397 */
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900398
399 usbhs_pipe_enable(pipe);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900400 usbhsf_rx_irq_ctrl(pipe, 1);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900401
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900402 return 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900403}
404
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900405static int usbhsf_try_pop(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900406{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900407 struct usbhs_pipe *pipe = pkt->pipe;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900408 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900409 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900410 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
411 void __iomem *addr = priv->base + fifo->port;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900412 u8 *buf;
413 u32 data = 0;
414 int maxp = usbhs_pipe_get_maxpacket(pipe);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900415 int rcv_len, len;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900416 int i, ret;
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900417 int total_len = 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900418
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900419 ret = usbhsf_fifo_select(pipe, fifo, 0);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900420 if (ret < 0)
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900421 return 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900422
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900423 ret = usbhsf_fifo_barrier(priv, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900424 if (ret < 0)
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900425 goto usbhs_fifo_read_busy;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900426
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900427 rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900428
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900429 buf = pkt->buf + pkt->actual;
430 len = pkt->length - pkt->actual;
431 len = min(len, rcv_len);
432 total_len = len;
433
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900434 /*
435 * Buffer clear if Zero-Length packet
436 *
437 * see
438 * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
439 */
440 if (0 == rcv_len) {
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900441 usbhsf_fifo_clear(pipe, fifo);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900442 goto usbhs_fifo_read_end;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900443 }
444
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900445 /*
446 * FIXME
447 *
448 * 32-bit access only
449 */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900450 if (len >= 4 && !((unsigned long)buf & 0x03)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900451 ioread32_rep(addr, buf, len / 4);
452 len %= 4;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900453 buf += total_len - len;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900454 }
455
456 /* the rest operation */
457 for (i = 0; i < len; i++) {
458 if (!(i & 0x03))
459 data = ioread32(addr);
460
461 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
462 }
463
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900464 pkt->actual += total_len;
465
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900466usbhs_fifo_read_end:
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900467 if ((pkt->actual == pkt->length) || /* receive all data */
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900468 (total_len < maxp)) { /* short packet */
469 *is_done = 1;
470 usbhsf_rx_irq_ctrl(pipe, 0);
471 usbhs_pipe_disable(pipe);
472 }
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900473
474 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
475 usbhs_pipe_number(pipe),
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900476 pkt->length, pkt->actual, *is_done, pkt->zero);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900477
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900478usbhs_fifo_read_busy:
479 usbhsf_fifo_unselect(pipe, fifo);
480
481 return ret;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900482}
Kuninori Morimotodad67392011-06-06 14:18:28 +0900483
484struct usbhs_pkt_handle usbhs_fifo_pop_handler = {
485 .prepare = usbhsf_prepare_pop,
486 .try_run = usbhsf_try_pop,
487};
488
489/*
490 * handler function
491 */
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900492static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotodad67392011-06-06 14:18:28 +0900493{
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900494 usbhs_dcp_control_transfer_done(pkt->pipe);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900495
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900496 *is_done = 1;
Kuninori Morimotodad67392011-06-06 14:18:28 +0900497
498 return 0;
499}
500
501struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
502 .prepare = usbhsf_ctrl_stage_end,
503 .try_run = usbhsf_ctrl_stage_end,
504};
505
506/*
507 * irq functions
508 */
509static int usbhsf_irq_empty(struct usbhs_priv *priv,
510 struct usbhs_irq_state *irq_state)
511{
512 struct usbhs_pipe *pipe;
Kuninori Morimotodad67392011-06-06 14:18:28 +0900513 struct device *dev = usbhs_priv_to_dev(priv);
514 int i, ret;
515
516 if (!irq_state->bempsts) {
517 dev_err(dev, "debug %s !!\n", __func__);
518 return -EIO;
519 }
520
521 dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
522
523 /*
524 * search interrupted "pipe"
525 * not "uep".
526 */
527 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
528 if (!(irq_state->bempsts & (1 << i)))
529 continue;
530
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900531 ret = usbhs_pkt_run(pipe);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900532 if (ret < 0)
533 dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
534 }
535
536 return 0;
537}
538
539static int usbhsf_irq_ready(struct usbhs_priv *priv,
540 struct usbhs_irq_state *irq_state)
541{
542 struct usbhs_pipe *pipe;
Kuninori Morimotodad67392011-06-06 14:18:28 +0900543 struct device *dev = usbhs_priv_to_dev(priv);
544 int i, ret;
545
546 if (!irq_state->brdysts) {
547 dev_err(dev, "debug %s !!\n", __func__);
548 return -EIO;
549 }
550
551 dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
552
553 /*
554 * search interrupted "pipe"
555 * not "uep".
556 */
557 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
558 if (!(irq_state->brdysts & (1 << i)))
559 continue;
560
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900561 ret = usbhs_pkt_run(pipe);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900562 if (ret < 0)
563 dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
564 }
565
566 return 0;
567}
568
569/*
570 * fifo init
571 */
572void usbhs_fifo_init(struct usbhs_priv *priv)
573{
574 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900575 struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900576
577 mod->irq_empty = usbhsf_irq_empty;
578 mod->irq_ready = usbhsf_irq_ready;
579 mod->irq_bempsts = 0;
580 mod->irq_brdysts = 0;
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900581
582 cfifo->pipe = NULL;
Kuninori Morimotodad67392011-06-06 14:18:28 +0900583}
584
585void usbhs_fifo_quit(struct usbhs_priv *priv)
586{
587 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
588
589 mod->irq_empty = NULL;
590 mod->irq_ready = NULL;
591 mod->irq_bempsts = 0;
592 mod->irq_brdysts = 0;
593}
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900594
595int usbhs_fifo_probe(struct usbhs_priv *priv)
596{
597 struct usbhs_fifo *fifo;
598
599 /* CFIFO */
600 fifo = usbhsf_get_cfifo(priv);
601 fifo->port = CFIFO;
602 fifo->sel = CFIFOSEL;
603 fifo->ctr = CFIFOCTR;
604
605 return 0;
606}
607
608void usbhs_fifo_remove(struct usbhs_priv *priv)
609{
610}