blob: 14baaad20b79b2ae7cc7b0d62720c7e69d5d39bc [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 Morimoto0432eed2011-06-06 14:18:54 +090077
78 usbhs_pkt_start(pipe);
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090079}
80
Kuninori Morimoto97664a22011-06-06 14:18:38 +090081static void __usbhsf_pkt_del(struct usbhs_pkt *pkt)
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090082{
83 list_del_init(&pkt->node);
84}
85
Kuninori Morimoto97664a22011-06-06 14:18:38 +090086static struct usbhs_pkt *__usbhsf_pkt_get(struct usbhs_pipe *pipe)
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +090087{
88 if (list_empty(&pipe->list))
89 return NULL;
90
91 return list_entry(pipe->list.next, struct usbhs_pkt, node);
92}
93
Kuninori Morimoto97664a22011-06-06 14:18:38 +090094struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt)
95{
96 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
97 unsigned long flags;
98
99 /******************** spin lock ********************/
100 usbhs_lock(priv, flags);
101
102 if (!pkt)
103 pkt = __usbhsf_pkt_get(pipe);
104
105 if (pkt)
106 __usbhsf_pkt_del(pkt);
107
108 usbhs_unlock(priv, flags);
109 /******************** spin unlock ******************/
110
111 return pkt;
112}
113
114int __usbhs_pkt_handler(struct usbhs_pipe *pipe, int type)
115{
116 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
117 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
118 struct usbhs_pkt *pkt;
119 struct device *dev = usbhs_priv_to_dev(priv);
120 int (*func)(struct usbhs_pkt *pkt, int *is_done);
121 unsigned long flags;
122 int ret = 0;
123 int is_done = 0;
124
125 /******************** spin lock ********************/
126 usbhs_lock(priv, flags);
127
128 pkt = __usbhsf_pkt_get(pipe);
129 if (!pkt)
130 goto __usbhs_pkt_handler_end;
131
132 switch (type) {
133 case USBHSF_PKT_PREPARE:
134 func = pkt->handler->prepare;
135 break;
136 case USBHSF_PKT_TRY_RUN:
137 func = pkt->handler->try_run;
138 break;
139 default:
140 dev_err(dev, "unknown pkt hander\n");
141 goto __usbhs_pkt_handler_end;
142 }
143
144 ret = func(pkt, &is_done);
145
146 if (is_done)
147 __usbhsf_pkt_del(pkt);
148
149__usbhs_pkt_handler_end:
150 usbhs_unlock(priv, flags);
151 /******************** spin unlock ******************/
152
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900153 if (is_done) {
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900154 info->done(pkt);
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900155 usbhs_pkt_start(pipe);
156 }
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900157
158 return ret;
159}
160
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900161/*
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900162 * irq enable/disable function
163 */
164#define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, bempsts, e)
165#define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, brdysts, e)
166#define usbhsf_irq_callback_ctrl(pipe, status, enable) \
167 ({ \
168 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
169 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
170 u16 status = (1 << usbhs_pipe_number(pipe)); \
171 if (!mod) \
172 return; \
173 if (enable) \
174 mod->irq_##status |= status; \
175 else \
176 mod->irq_##status &= ~status; \
177 usbhs_irq_callback_update(priv, mod); \
178 })
179
180static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
181{
182 /*
183 * And DCP pipe can NOT use "ready interrupt" for "send"
184 * it should use "empty" interrupt.
185 * see
186 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
187 *
188 * on the other hand, normal pipe can use "ready interrupt" for "send"
189 * even though it is single/double buffer
190 */
191 if (usbhs_pipe_is_dcp(pipe))
192 usbhsf_irq_empty_ctrl(pipe, enable);
193 else
194 usbhsf_irq_ready_ctrl(pipe, enable);
195}
196
197static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
198{
199 usbhsf_irq_ready_ctrl(pipe, enable);
200}
201
202/*
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900203 * FIFO ctrl
204 */
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900205static void usbhsf_send_terminator(struct usbhs_pipe *pipe,
206 struct usbhs_fifo *fifo)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900207{
208 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
209
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900210 usbhs_bset(priv, fifo->ctr, BVAL, BVAL);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900211}
212
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900213static int usbhsf_fifo_barrier(struct usbhs_priv *priv,
214 struct usbhs_fifo *fifo)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900215{
216 int timeout = 1024;
217
218 do {
219 /* The FIFO port is accessible */
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900220 if (usbhs_read(priv, fifo->ctr) & FRDY)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900221 return 0;
222
223 udelay(10);
224 } while (timeout--);
225
226 return -EBUSY;
227}
228
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900229static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
230 struct usbhs_fifo *fifo)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900231{
232 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
233
234 if (!usbhs_pipe_is_dcp(pipe))
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900235 usbhsf_fifo_barrier(priv, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900236
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900237 usbhs_write(priv, fifo->ctr, BCLR);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900238}
239
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900240static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv,
241 struct usbhs_fifo *fifo)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900242{
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900243 return usbhs_read(priv, fifo->ctr) & DTLN_MASK;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900244}
245
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900246static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
247 struct usbhs_fifo *fifo)
248{
249 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
250
251 usbhs_pipe_select_fifo(pipe, NULL);
252 usbhs_write(priv, fifo->sel, 0);
253}
254
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900255static int usbhsf_fifo_select(struct usbhs_pipe *pipe,
256 struct usbhs_fifo *fifo,
257 int write)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900258{
259 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
260 struct device *dev = usbhs_priv_to_dev(priv);
261 int timeout = 1024;
262 u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
263 u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
264
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900265 if (usbhs_pipe_is_busy(pipe) ||
266 usbhsf_fifo_is_busy(fifo))
267 return -EBUSY;
268
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900269 if (usbhs_pipe_is_dcp(pipe))
270 base |= (1 == write) << 5; /* ISEL */
271
272 /* "base" will be used below */
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900273 usbhs_write(priv, fifo->sel, base | MBW_32);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900274
275 /* check ISEL and CURPIPE value */
276 while (timeout--) {
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900277 if (base == (mask & usbhs_read(priv, fifo->sel))) {
278 usbhs_pipe_select_fifo(pipe, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900279 return 0;
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900280 }
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900281 udelay(10);
282 }
283
284 dev_err(dev, "fifo select error\n");
285
286 return -EIO;
287}
288
289/*
290 * PIO fifo functions
291 */
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900292static int usbhsf_pio_try_push(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900293{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900294 struct usbhs_pipe *pipe = pkt->pipe;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900295 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900296 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900297 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
298 void __iomem *addr = priv->base + fifo->port;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900299 u8 *buf;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900300 int maxp = usbhs_pipe_get_maxpacket(pipe);
301 int total_len;
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900302 int i, ret, len;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900303 int is_short;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900304
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900305 ret = usbhsf_fifo_select(pipe, fifo, 1);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900306 if (ret < 0)
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900307 return 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900308
Kuninori Morimotodad67392011-06-06 14:18:28 +0900309 ret = usbhs_pipe_is_accessible(pipe);
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 Morimotod3af90a2011-06-06 14:18:44 +0900313 ret = usbhsf_fifo_barrier(priv, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900314 if (ret < 0)
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900315 goto usbhs_fifo_write_busy;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900316
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900317 buf = pkt->buf + pkt->actual;
318 len = pkt->length - pkt->actual;
319 len = min(len, maxp);
320 total_len = len;
321 is_short = total_len < maxp;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900322
323 /*
324 * FIXME
325 *
326 * 32-bit access only
327 */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900328 if (len >= 4 && !((unsigned long)buf & 0x03)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900329 iowrite32_rep(addr, buf, len / 4);
330 len %= 4;
331 buf += total_len - len;
332 }
333
334 /* the rest operation */
335 for (i = 0; i < len; i++)
336 iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
337
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900338 /*
339 * variable update
340 */
341 pkt->actual += total_len;
342
343 if (pkt->actual < pkt->length)
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900344 *is_done = 0; /* there are remainder data */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900345 else if (is_short)
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900346 *is_done = 1; /* short packet */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900347 else
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900348 *is_done = !pkt->zero; /* send zero packet ? */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900349
350 /*
351 * pipe/irq handling
352 */
353 if (is_short)
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900354 usbhsf_send_terminator(pipe, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900355
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900356 usbhsf_tx_irq_ctrl(pipe, !*is_done);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900357 usbhs_pipe_enable(pipe);
358
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900359 dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
360 usbhs_pipe_number(pipe),
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900361 pkt->length, pkt->actual, *is_done, pkt->zero);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900362
363 /*
364 * Transmission end
365 */
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900366 if (*is_done) {
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900367 if (usbhs_pipe_is_dcp(pipe))
368 usbhs_dcp_control_transfer_done(pipe);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900369 }
370
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900371 usbhsf_fifo_unselect(pipe, fifo);
372
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900373 return 0;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900374
375usbhs_fifo_write_busy:
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900376 usbhsf_fifo_unselect(pipe, fifo);
377
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900378 /*
379 * pipe is busy.
380 * retry in interrupt
381 */
382 usbhsf_tx_irq_ctrl(pipe, 1);
383
384 return ret;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900385}
386
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900387struct usbhs_pkt_handle usbhs_fifo_pio_push_handler = {
388 .prepare = usbhsf_pio_try_push,
389 .try_run = usbhsf_pio_try_push,
Kuninori Morimotodad67392011-06-06 14:18:28 +0900390};
391
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900392static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900393{
Kuninori Morimotodad67392011-06-06 14:18:28 +0900394 struct usbhs_pipe *pipe = pkt->pipe;
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900395
396 if (usbhs_pipe_is_busy(pipe))
397 return 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900398
399 /*
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900400 * pipe enable to prepare packet receive
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900401 */
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900402
403 usbhs_pipe_enable(pipe);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900404 usbhsf_rx_irq_ctrl(pipe, 1);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900405
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900406 return 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900407}
408
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900409static int usbhsf_pio_try_pop(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900410{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900411 struct usbhs_pipe *pipe = pkt->pipe;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900412 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900413 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900414 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
415 void __iomem *addr = priv->base + fifo->port;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900416 u8 *buf;
417 u32 data = 0;
418 int maxp = usbhs_pipe_get_maxpacket(pipe);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900419 int rcv_len, len;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900420 int i, ret;
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900421 int total_len = 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900422
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900423 ret = usbhsf_fifo_select(pipe, fifo, 0);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900424 if (ret < 0)
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900425 return 0;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900426
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900427 ret = usbhsf_fifo_barrier(priv, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900428 if (ret < 0)
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900429 goto usbhs_fifo_read_busy;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900430
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900431 rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900432
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900433 buf = pkt->buf + pkt->actual;
434 len = pkt->length - pkt->actual;
435 len = min(len, rcv_len);
436 total_len = len;
437
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900438 /*
439 * Buffer clear if Zero-Length packet
440 *
441 * see
442 * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
443 */
444 if (0 == rcv_len) {
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900445 usbhsf_fifo_clear(pipe, fifo);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900446 goto usbhs_fifo_read_end;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900447 }
448
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900449 /*
450 * FIXME
451 *
452 * 32-bit access only
453 */
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900454 if (len >= 4 && !((unsigned long)buf & 0x03)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900455 ioread32_rep(addr, buf, len / 4);
456 len %= 4;
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900457 buf += total_len - len;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900458 }
459
460 /* the rest operation */
461 for (i = 0; i < len; i++) {
462 if (!(i & 0x03))
463 data = ioread32(addr);
464
465 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
466 }
467
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900468 pkt->actual += total_len;
469
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900470usbhs_fifo_read_end:
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900471 if ((pkt->actual == pkt->length) || /* receive all data */
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900472 (total_len < maxp)) { /* short packet */
473 *is_done = 1;
474 usbhsf_rx_irq_ctrl(pipe, 0);
475 usbhs_pipe_disable(pipe);
476 }
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900477
478 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
479 usbhs_pipe_number(pipe),
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900480 pkt->length, pkt->actual, *is_done, pkt->zero);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900481
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900482usbhs_fifo_read_busy:
483 usbhsf_fifo_unselect(pipe, fifo);
484
485 return ret;
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900486}
Kuninori Morimotodad67392011-06-06 14:18:28 +0900487
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900488struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler = {
Kuninori Morimotodad67392011-06-06 14:18:28 +0900489 .prepare = usbhsf_prepare_pop,
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900490 .try_run = usbhsf_pio_try_pop,
Kuninori Morimotodad67392011-06-06 14:18:28 +0900491};
492
493/*
494 * handler function
495 */
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900496static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
Kuninori Morimotodad67392011-06-06 14:18:28 +0900497{
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900498 usbhs_dcp_control_transfer_done(pkt->pipe);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900499
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900500 *is_done = 1;
Kuninori Morimotodad67392011-06-06 14:18:28 +0900501
502 return 0;
503}
504
505struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
506 .prepare = usbhsf_ctrl_stage_end,
507 .try_run = usbhsf_ctrl_stage_end,
508};
509
510/*
511 * irq functions
512 */
513static int usbhsf_irq_empty(struct usbhs_priv *priv,
514 struct usbhs_irq_state *irq_state)
515{
516 struct usbhs_pipe *pipe;
Kuninori Morimotodad67392011-06-06 14:18:28 +0900517 struct device *dev = usbhs_priv_to_dev(priv);
518 int i, ret;
519
520 if (!irq_state->bempsts) {
521 dev_err(dev, "debug %s !!\n", __func__);
522 return -EIO;
523 }
524
525 dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
526
527 /*
528 * search interrupted "pipe"
529 * not "uep".
530 */
531 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
532 if (!(irq_state->bempsts & (1 << i)))
533 continue;
534
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900535 ret = usbhs_pkt_run(pipe);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900536 if (ret < 0)
537 dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
538 }
539
540 return 0;
541}
542
543static int usbhsf_irq_ready(struct usbhs_priv *priv,
544 struct usbhs_irq_state *irq_state)
545{
546 struct usbhs_pipe *pipe;
Kuninori Morimotodad67392011-06-06 14:18:28 +0900547 struct device *dev = usbhs_priv_to_dev(priv);
548 int i, ret;
549
550 if (!irq_state->brdysts) {
551 dev_err(dev, "debug %s !!\n", __func__);
552 return -EIO;
553 }
554
555 dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
556
557 /*
558 * search interrupted "pipe"
559 * not "uep".
560 */
561 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
562 if (!(irq_state->brdysts & (1 << i)))
563 continue;
564
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900565 ret = usbhs_pkt_run(pipe);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900566 if (ret < 0)
567 dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
568 }
569
570 return 0;
571}
572
573/*
574 * fifo init
575 */
576void usbhs_fifo_init(struct usbhs_priv *priv)
577{
578 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900579 struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900580
581 mod->irq_empty = usbhsf_irq_empty;
582 mod->irq_ready = usbhsf_irq_ready;
583 mod->irq_bempsts = 0;
584 mod->irq_brdysts = 0;
Kuninori Morimotod77e3f42011-06-06 14:18:50 +0900585
586 cfifo->pipe = NULL;
Kuninori Morimotodad67392011-06-06 14:18:28 +0900587}
588
589void usbhs_fifo_quit(struct usbhs_priv *priv)
590{
591 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
592
593 mod->irq_empty = NULL;
594 mod->irq_ready = NULL;
595 mod->irq_bempsts = 0;
596 mod->irq_brdysts = 0;
597}
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900598
599int usbhs_fifo_probe(struct usbhs_priv *priv)
600{
601 struct usbhs_fifo *fifo;
602
603 /* CFIFO */
604 fifo = usbhsf_get_cfifo(priv);
605 fifo->port = CFIFO;
606 fifo->sel = CFIFOSEL;
607 fifo->ctr = CFIFOCTR;
608
609 return 0;
610}
611
612void usbhs_fifo_remove(struct usbhs_priv *priv)
613{
614}