Commit | Line | Data |
---|---|---|
2a4f136f DA |
1 | /* |
2 | * driver/usb/gadget/imx_udc.c | |
3 | * | |
593bef6c | 4 | * Copyright (C) 2005 Mike Lee <eemike@gmail.com> |
2a4f136f DA |
5 | * Copyright (C) 2008 Darius Augulis <augulis.darius@gmail.com> |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | */ | |
17 | ||
18 | #include <linux/init.h> | |
19 | #include <linux/kernel.h> | |
20 | #include <linux/platform_device.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/errno.h> | |
23 | #include <linux/list.h> | |
24 | #include <linux/interrupt.h> | |
25 | #include <linux/io.h> | |
26 | #include <linux/irq.h> | |
27 | #include <linux/device.h> | |
28 | #include <linux/dma-mapping.h> | |
29 | #include <linux/clk.h> | |
30 | #include <linux/delay.h> | |
b633d28e | 31 | #include <linux/timer.h> |
5a0e3ad6 | 32 | #include <linux/slab.h> |
2a4f136f DA |
33 | |
34 | #include <linux/usb/ch9.h> | |
35 | #include <linux/usb/gadget.h> | |
36 | ||
37 | #include <mach/usb.h> | |
38 | #include <mach/hardware.h> | |
39 | ||
40 | #include "imx_udc.h" | |
41 | ||
42 | static const char driver_name[] = "imx_udc"; | |
43 | static const char ep0name[] = "ep0"; | |
44 | ||
45 | void ep0_chg_stat(const char *label, struct imx_udc_struct *imx_usb, | |
46 | enum ep0_state stat); | |
47 | ||
48 | /******************************************************************************* | |
49 | * IMX UDC hardware related functions | |
50 | ******************************************************************************* | |
51 | */ | |
52 | ||
53 | void imx_udc_enable(struct imx_udc_struct *imx_usb) | |
54 | { | |
55 | int temp = __raw_readl(imx_usb->base + USB_CTRL); | |
593bef6c DA |
56 | __raw_writel(temp | CTRL_FE_ENA | CTRL_AFE_ENA, |
57 | imx_usb->base + USB_CTRL); | |
2a4f136f DA |
58 | imx_usb->gadget.speed = USB_SPEED_FULL; |
59 | } | |
60 | ||
61 | void imx_udc_disable(struct imx_udc_struct *imx_usb) | |
62 | { | |
63 | int temp = __raw_readl(imx_usb->base + USB_CTRL); | |
64 | ||
65 | __raw_writel(temp & ~(CTRL_FE_ENA | CTRL_AFE_ENA), | |
66 | imx_usb->base + USB_CTRL); | |
67 | ||
68 | ep0_chg_stat(__func__, imx_usb, EP0_IDLE); | |
69 | imx_usb->gadget.speed = USB_SPEED_UNKNOWN; | |
70 | } | |
71 | ||
72 | void imx_udc_reset(struct imx_udc_struct *imx_usb) | |
73 | { | |
74 | int temp = __raw_readl(imx_usb->base + USB_ENAB); | |
75 | ||
76 | /* set RST bit */ | |
77 | __raw_writel(temp | ENAB_RST, imx_usb->base + USB_ENAB); | |
78 | ||
79 | /* wait RST bit to clear */ | |
80 | do {} while (__raw_readl(imx_usb->base + USB_ENAB) & ENAB_RST); | |
81 | ||
82 | /* wait CFG bit to assert */ | |
83 | do {} while (!(__raw_readl(imx_usb->base + USB_DADR) & DADR_CFG)); | |
84 | ||
85 | /* udc module is now ready */ | |
86 | } | |
87 | ||
88 | void imx_udc_config(struct imx_udc_struct *imx_usb) | |
89 | { | |
90 | u8 ep_conf[5]; | |
91 | u8 i, j, cfg; | |
92 | struct imx_ep_struct *imx_ep; | |
93 | ||
94 | /* wait CFG bit to assert */ | |
95 | do {} while (!(__raw_readl(imx_usb->base + USB_DADR) & DADR_CFG)); | |
96 | ||
97 | /* Download the endpoint buffer for endpoint 0. */ | |
98 | for (j = 0; j < 5; j++) { | |
99 | i = (j == 2 ? imx_usb->imx_ep[0].fifosize : 0x00); | |
100 | __raw_writeb(i, imx_usb->base + USB_DDAT); | |
101 | do {} while (__raw_readl(imx_usb->base + USB_DADR) & DADR_BSY); | |
102 | } | |
103 | ||
104 | /* Download the endpoint buffers for endpoints 1-5. | |
105 | * We specify two configurations, one interface | |
106 | */ | |
107 | for (cfg = 1; cfg < 3; cfg++) { | |
108 | for (i = 1; i < IMX_USB_NB_EP; i++) { | |
109 | imx_ep = &imx_usb->imx_ep[i]; | |
110 | /* EP no | Config no */ | |
111 | ep_conf[0] = (i << 4) | (cfg << 2); | |
112 | /* Type | Direction */ | |
113 | ep_conf[1] = (imx_ep->bmAttributes << 3) | | |
114 | (EP_DIR(imx_ep) << 2); | |
115 | /* Max packet size */ | |
116 | ep_conf[2] = imx_ep->fifosize; | |
117 | /* TRXTYP */ | |
118 | ep_conf[3] = 0xC0; | |
119 | /* FIFO no */ | |
120 | ep_conf[4] = i; | |
121 | ||
122 | D_INI(imx_usb->dev, | |
123 | "<%s> ep%d_conf[%d]:" | |
124 | "[%02x-%02x-%02x-%02x-%02x]\n", | |
125 | __func__, i, cfg, | |
126 | ep_conf[0], ep_conf[1], ep_conf[2], | |
127 | ep_conf[3], ep_conf[4]); | |
128 | ||
129 | for (j = 0; j < 5; j++) { | |
130 | __raw_writeb(ep_conf[j], | |
131 | imx_usb->base + USB_DDAT); | |
593bef6c DA |
132 | do {} while (__raw_readl(imx_usb->base |
133 | + USB_DADR) | |
2a4f136f DA |
134 | & DADR_BSY); |
135 | } | |
136 | } | |
137 | } | |
138 | ||
139 | /* wait CFG bit to clear */ | |
140 | do {} while (__raw_readl(imx_usb->base + USB_DADR) & DADR_CFG); | |
141 | } | |
142 | ||
143 | void imx_udc_init_irq(struct imx_udc_struct *imx_usb) | |
144 | { | |
145 | int i; | |
146 | ||
147 | /* Mask and clear all irqs */ | |
148 | __raw_writel(0xFFFFFFFF, imx_usb->base + USB_MASK); | |
149 | __raw_writel(0xFFFFFFFF, imx_usb->base + USB_INTR); | |
150 | for (i = 0; i < IMX_USB_NB_EP; i++) { | |
151 | __raw_writel(0x1FF, imx_usb->base + USB_EP_MASK(i)); | |
152 | __raw_writel(0x1FF, imx_usb->base + USB_EP_INTR(i)); | |
153 | } | |
154 | ||
155 | /* Enable USB irqs */ | |
156 | __raw_writel(INTR_MSOF | INTR_FRAME_MATCH, imx_usb->base + USB_MASK); | |
157 | ||
158 | /* Enable EP0 irqs */ | |
159 | __raw_writel(0x1FF & ~(EPINTR_DEVREQ | EPINTR_MDEVREQ | EPINTR_EOT | |
160 | | EPINTR_EOF | EPINTR_FIFO_EMPTY | EPINTR_FIFO_FULL), | |
161 | imx_usb->base + USB_EP_MASK(0)); | |
162 | } | |
163 | ||
164 | void imx_udc_init_ep(struct imx_udc_struct *imx_usb) | |
165 | { | |
166 | int i, max, temp; | |
167 | struct imx_ep_struct *imx_ep; | |
168 | for (i = 0; i < IMX_USB_NB_EP; i++) { | |
169 | imx_ep = &imx_usb->imx_ep[i]; | |
170 | switch (imx_ep->fifosize) { | |
171 | case 8: | |
172 | max = 0; | |
173 | break; | |
174 | case 16: | |
175 | max = 1; | |
176 | break; | |
177 | case 32: | |
178 | max = 2; | |
179 | break; | |
180 | case 64: | |
181 | max = 3; | |
182 | break; | |
183 | default: | |
184 | max = 1; | |
185 | break; | |
186 | } | |
187 | temp = (EP_DIR(imx_ep) << 7) | (max << 5) | |
188 | | (imx_ep->bmAttributes << 3); | |
189 | __raw_writel(temp, imx_usb->base + USB_EP_STAT(i)); | |
593bef6c DA |
190 | __raw_writel(temp | EPSTAT_FLUSH, |
191 | imx_usb->base + USB_EP_STAT(i)); | |
2a4f136f DA |
192 | D_INI(imx_usb->dev, "<%s> ep%d_stat %08x\n", __func__, i, |
193 | __raw_readl(imx_usb->base + USB_EP_STAT(i))); | |
194 | } | |
195 | } | |
196 | ||
197 | void imx_udc_init_fifo(struct imx_udc_struct *imx_usb) | |
198 | { | |
199 | int i, temp; | |
200 | struct imx_ep_struct *imx_ep; | |
201 | for (i = 0; i < IMX_USB_NB_EP; i++) { | |
202 | imx_ep = &imx_usb->imx_ep[i]; | |
203 | ||
204 | /* Fifo control */ | |
205 | temp = EP_DIR(imx_ep) ? 0x0B000000 : 0x0F000000; | |
206 | __raw_writel(temp, imx_usb->base + USB_EP_FCTRL(i)); | |
207 | D_INI(imx_usb->dev, "<%s> ep%d_fctrl %08x\n", __func__, i, | |
208 | __raw_readl(imx_usb->base + USB_EP_FCTRL(i))); | |
209 | ||
210 | /* Fifo alarm */ | |
211 | temp = (i ? imx_ep->fifosize / 2 : 0); | |
212 | __raw_writel(temp, imx_usb->base + USB_EP_FALRM(i)); | |
213 | D_INI(imx_usb->dev, "<%s> ep%d_falrm %08x\n", __func__, i, | |
214 | __raw_readl(imx_usb->base + USB_EP_FALRM(i))); | |
215 | } | |
216 | } | |
217 | ||
218 | static void imx_udc_init(struct imx_udc_struct *imx_usb) | |
219 | { | |
220 | /* Reset UDC */ | |
221 | imx_udc_reset(imx_usb); | |
222 | ||
223 | /* Download config to enpoint buffer */ | |
224 | imx_udc_config(imx_usb); | |
225 | ||
226 | /* Setup interrups */ | |
227 | imx_udc_init_irq(imx_usb); | |
228 | ||
229 | /* Setup endpoints */ | |
230 | imx_udc_init_ep(imx_usb); | |
231 | ||
232 | /* Setup fifos */ | |
233 | imx_udc_init_fifo(imx_usb); | |
234 | } | |
235 | ||
236 | void imx_ep_irq_enable(struct imx_ep_struct *imx_ep) | |
237 | { | |
238 | ||
239 | int i = EP_NO(imx_ep); | |
240 | ||
241 | __raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_MASK(i)); | |
242 | __raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_INTR(i)); | |
243 | __raw_writel(0x1FF & ~(EPINTR_EOT | EPINTR_EOF), | |
244 | imx_ep->imx_usb->base + USB_EP_MASK(i)); | |
245 | } | |
246 | ||
247 | void imx_ep_irq_disable(struct imx_ep_struct *imx_ep) | |
248 | { | |
249 | ||
250 | int i = EP_NO(imx_ep); | |
251 | ||
252 | __raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_MASK(i)); | |
253 | __raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_INTR(i)); | |
254 | } | |
255 | ||
256 | int imx_ep_empty(struct imx_ep_struct *imx_ep) | |
257 | { | |
258 | struct imx_udc_struct *imx_usb = imx_ep->imx_usb; | |
259 | ||
260 | return __raw_readl(imx_usb->base + USB_EP_FSTAT(EP_NO(imx_ep))) | |
261 | & FSTAT_EMPTY; | |
262 | } | |
263 | ||
264 | unsigned imx_fifo_bcount(struct imx_ep_struct *imx_ep) | |
265 | { | |
266 | struct imx_udc_struct *imx_usb = imx_ep->imx_usb; | |
267 | ||
268 | return (__raw_readl(imx_usb->base + USB_EP_STAT(EP_NO(imx_ep))) | |
269 | & EPSTAT_BCOUNT) >> 16; | |
270 | } | |
271 | ||
272 | void imx_flush(struct imx_ep_struct *imx_ep) | |
273 | { | |
274 | struct imx_udc_struct *imx_usb = imx_ep->imx_usb; | |
275 | ||
276 | int temp = __raw_readl(imx_usb->base + USB_EP_STAT(EP_NO(imx_ep))); | |
277 | __raw_writel(temp | EPSTAT_FLUSH, | |
278 | imx_usb->base + USB_EP_STAT(EP_NO(imx_ep))); | |
279 | } | |
280 | ||
281 | void imx_ep_stall(struct imx_ep_struct *imx_ep) | |
282 | { | |
283 | struct imx_udc_struct *imx_usb = imx_ep->imx_usb; | |
284 | int temp, i; | |
285 | ||
593bef6c DA |
286 | D_ERR(imx_usb->dev, |
287 | "<%s> Forced stall on %s\n", __func__, imx_ep->ep.name); | |
2a4f136f DA |
288 | |
289 | imx_flush(imx_ep); | |
290 | ||
291 | /* Special care for ep0 */ | |
8f182e5d | 292 | if (!EP_NO(imx_ep)) { |
2a4f136f | 293 | temp = __raw_readl(imx_usb->base + USB_CTRL); |
593bef6c DA |
294 | __raw_writel(temp | CTRL_CMDOVER | CTRL_CMDERROR, |
295 | imx_usb->base + USB_CTRL); | |
296 | do { } while (__raw_readl(imx_usb->base + USB_CTRL) | |
297 | & CTRL_CMDOVER); | |
2a4f136f DA |
298 | temp = __raw_readl(imx_usb->base + USB_CTRL); |
299 | __raw_writel(temp & ~CTRL_CMDERROR, imx_usb->base + USB_CTRL); | |
300 | } | |
301 | else { | |
302 | temp = __raw_readl(imx_usb->base + USB_EP_STAT(EP_NO(imx_ep))); | |
303 | __raw_writel(temp | EPSTAT_STALL, | |
304 | imx_usb->base + USB_EP_STAT(EP_NO(imx_ep))); | |
305 | ||
306 | for (i = 0; i < 100; i ++) { | |
593bef6c DA |
307 | temp = __raw_readl(imx_usb->base |
308 | + USB_EP_STAT(EP_NO(imx_ep))); | |
0df24792 | 309 | if (!(temp & EPSTAT_STALL)) |
2a4f136f DA |
310 | break; |
311 | udelay(20); | |
312 | } | |
8f182e5d | 313 | if (i == 100) |
2a4f136f DA |
314 | D_ERR(imx_usb->dev, "<%s> Non finished stall on %s\n", |
315 | __func__, imx_ep->ep.name); | |
316 | } | |
317 | } | |
318 | ||
319 | static int imx_udc_get_frame(struct usb_gadget *_gadget) | |
320 | { | |
321 | struct imx_udc_struct *imx_usb = container_of(_gadget, | |
322 | struct imx_udc_struct, gadget); | |
323 | ||
324 | return __raw_readl(imx_usb->base + USB_FRAME) & 0x7FF; | |
325 | } | |
326 | ||
327 | static int imx_udc_wakeup(struct usb_gadget *_gadget) | |
328 | { | |
329 | return 0; | |
330 | } | |
331 | ||
332 | /******************************************************************************* | |
333 | * USB request control functions | |
334 | ******************************************************************************* | |
335 | */ | |
336 | ||
593bef6c DA |
337 | static void ep_add_request(struct imx_ep_struct *imx_ep, |
338 | struct imx_request *req) | |
2a4f136f DA |
339 | { |
340 | if (unlikely(!req)) | |
341 | return; | |
342 | ||
343 | req->in_use = 1; | |
344 | list_add_tail(&req->queue, &imx_ep->queue); | |
345 | } | |
346 | ||
593bef6c DA |
347 | static void ep_del_request(struct imx_ep_struct *imx_ep, |
348 | struct imx_request *req) | |
2a4f136f DA |
349 | { |
350 | if (unlikely(!req)) | |
351 | return; | |
352 | ||
353 | list_del_init(&req->queue); | |
354 | req->in_use = 0; | |
355 | } | |
356 | ||
593bef6c DA |
357 | static void done(struct imx_ep_struct *imx_ep, |
358 | struct imx_request *req, int status) | |
2a4f136f DA |
359 | { |
360 | ep_del_request(imx_ep, req); | |
361 | ||
362 | if (likely(req->req.status == -EINPROGRESS)) | |
363 | req->req.status = status; | |
364 | else | |
365 | status = req->req.status; | |
366 | ||
367 | if (status && status != -ESHUTDOWN) | |
368 | D_ERR(imx_ep->imx_usb->dev, | |
369 | "<%s> complete %s req %p stat %d len %u/%u\n", __func__, | |
370 | imx_ep->ep.name, &req->req, status, | |
371 | req->req.actual, req->req.length); | |
372 | ||
373 | req->req.complete(&imx_ep->ep, &req->req); | |
374 | } | |
375 | ||
376 | static void nuke(struct imx_ep_struct *imx_ep, int status) | |
377 | { | |
378 | struct imx_request *req; | |
379 | ||
380 | while (!list_empty(&imx_ep->queue)) { | |
381 | req = list_entry(imx_ep->queue.next, struct imx_request, queue); | |
382 | done(imx_ep, req, status); | |
383 | } | |
384 | } | |
385 | ||
386 | /******************************************************************************* | |
387 | * Data tansfer over USB functions | |
388 | ******************************************************************************* | |
389 | */ | |
390 | static int read_packet(struct imx_ep_struct *imx_ep, struct imx_request *req) | |
391 | { | |
392 | u8 *buf; | |
393 | int bytes_ep, bufferspace, count, i; | |
394 | ||
395 | bytes_ep = imx_fifo_bcount(imx_ep); | |
396 | bufferspace = req->req.length - req->req.actual; | |
397 | ||
398 | buf = req->req.buf + req->req.actual; | |
399 | prefetchw(buf); | |
400 | ||
401 | if (unlikely(imx_ep_empty(imx_ep))) | |
402 | count = 0; /* zlp */ | |
403 | else | |
404 | count = min(bytes_ep, bufferspace); | |
405 | ||
406 | for (i = count; i > 0; i--) | |
407 | *buf++ = __raw_readb(imx_ep->imx_usb->base | |
408 | + USB_EP_FDAT0(EP_NO(imx_ep))); | |
409 | req->req.actual += count; | |
410 | ||
411 | return count; | |
412 | } | |
413 | ||
414 | static int write_packet(struct imx_ep_struct *imx_ep, struct imx_request *req) | |
415 | { | |
416 | u8 *buf; | |
417 | int length, count, temp; | |
418 | ||
680cc645 DG |
419 | if (unlikely(__raw_readl(imx_ep->imx_usb->base + |
420 | USB_EP_STAT(EP_NO(imx_ep))) & EPSTAT_ZLPS)) { | |
421 | D_TRX(imx_ep->imx_usb->dev, "<%s> zlp still queued in EP %s\n", | |
422 | __func__, imx_ep->ep.name); | |
423 | return -1; | |
424 | } | |
425 | ||
2a4f136f DA |
426 | buf = req->req.buf + req->req.actual; |
427 | prefetch(buf); | |
428 | ||
429 | length = min(req->req.length - req->req.actual, (u32)imx_ep->fifosize); | |
430 | ||
431 | if (imx_fifo_bcount(imx_ep) + length > imx_ep->fifosize) { | |
432 | D_TRX(imx_ep->imx_usb->dev, "<%s> packet overfill %s fifo\n", | |
433 | __func__, imx_ep->ep.name); | |
434 | return -1; | |
435 | } | |
436 | ||
437 | req->req.actual += length; | |
438 | count = length; | |
439 | ||
440 | if (!count && req->req.zero) { /* zlp */ | |
441 | temp = __raw_readl(imx_ep->imx_usb->base | |
442 | + USB_EP_STAT(EP_NO(imx_ep))); | |
443 | __raw_writel(temp | EPSTAT_ZLPS, imx_ep->imx_usb->base | |
444 | + USB_EP_STAT(EP_NO(imx_ep))); | |
445 | D_TRX(imx_ep->imx_usb->dev, "<%s> zero packet\n", __func__); | |
446 | return 0; | |
447 | } | |
448 | ||
449 | while (count--) { | |
450 | if (count == 0) { /* last byte */ | |
451 | temp = __raw_readl(imx_ep->imx_usb->base | |
452 | + USB_EP_FCTRL(EP_NO(imx_ep))); | |
453 | __raw_writel(temp | FCTRL_WFR, imx_ep->imx_usb->base | |
454 | + USB_EP_FCTRL(EP_NO(imx_ep))); | |
455 | } | |
456 | __raw_writeb(*buf++, | |
457 | imx_ep->imx_usb->base + USB_EP_FDAT0(EP_NO(imx_ep))); | |
458 | } | |
459 | ||
460 | return length; | |
461 | } | |
462 | ||
463 | static int read_fifo(struct imx_ep_struct *imx_ep, struct imx_request *req) | |
464 | { | |
465 | int bytes = 0, | |
466 | count, | |
467 | completed = 0; | |
468 | ||
469 | while (__raw_readl(imx_ep->imx_usb->base + USB_EP_FSTAT(EP_NO(imx_ep))) | |
470 | & FSTAT_FR) { | |
471 | count = read_packet(imx_ep, req); | |
472 | bytes += count; | |
473 | ||
474 | completed = (count != imx_ep->fifosize); | |
475 | if (completed || req->req.actual == req->req.length) { | |
476 | completed = 1; | |
477 | break; | |
478 | } | |
479 | } | |
480 | ||
481 | if (completed || !req->req.length) { | |
482 | done(imx_ep, req, 0); | |
483 | D_REQ(imx_ep->imx_usb->dev, "<%s> %s req<%p> %s\n", | |
484 | __func__, imx_ep->ep.name, req, | |
485 | completed ? "completed" : "not completed"); | |
486 | if (!EP_NO(imx_ep)) | |
487 | ep0_chg_stat(__func__, imx_ep->imx_usb, EP0_IDLE); | |
488 | } | |
489 | ||
490 | D_TRX(imx_ep->imx_usb->dev, "<%s> bytes read: %d\n", __func__, bytes); | |
491 | ||
492 | return completed; | |
493 | } | |
494 | ||
495 | static int write_fifo(struct imx_ep_struct *imx_ep, struct imx_request *req) | |
496 | { | |
497 | int bytes = 0, | |
498 | count, | |
499 | completed = 0; | |
500 | ||
501 | while (!completed) { | |
502 | count = write_packet(imx_ep, req); | |
503 | if (count < 0) | |
504 | break; /* busy */ | |
505 | bytes += count; | |
506 | ||
507 | /* last packet "must be" short (or a zlp) */ | |
508 | completed = (count != imx_ep->fifosize); | |
509 | ||
510 | if (unlikely(completed)) { | |
511 | done(imx_ep, req, 0); | |
512 | D_REQ(imx_ep->imx_usb->dev, "<%s> %s req<%p> %s\n", | |
513 | __func__, imx_ep->ep.name, req, | |
514 | completed ? "completed" : "not completed"); | |
515 | if (!EP_NO(imx_ep)) | |
593bef6c DA |
516 | ep0_chg_stat(__func__, |
517 | imx_ep->imx_usb, EP0_IDLE); | |
2a4f136f DA |
518 | } |
519 | } | |
520 | ||
521 | D_TRX(imx_ep->imx_usb->dev, "<%s> bytes sent: %d\n", __func__, bytes); | |
522 | ||
523 | return completed; | |
524 | } | |
525 | ||
526 | /******************************************************************************* | |
527 | * Endpoint handlers | |
528 | ******************************************************************************* | |
529 | */ | |
530 | static int handle_ep(struct imx_ep_struct *imx_ep) | |
531 | { | |
532 | struct imx_request *req; | |
533 | int completed = 0; | |
534 | ||
535 | do { | |
536 | if (!list_empty(&imx_ep->queue)) | |
537 | req = list_entry(imx_ep->queue.next, | |
538 | struct imx_request, queue); | |
539 | else { | |
540 | D_REQ(imx_ep->imx_usb->dev, "<%s> no request on %s\n", | |
541 | __func__, imx_ep->ep.name); | |
542 | return 0; | |
543 | } | |
544 | ||
545 | if (EP_DIR(imx_ep)) /* to host */ | |
546 | completed = write_fifo(imx_ep, req); | |
547 | else /* to device */ | |
548 | completed = read_fifo(imx_ep, req); | |
549 | ||
550 | dump_ep_stat(__func__, imx_ep); | |
551 | ||
552 | } while (completed); | |
553 | ||
554 | return 0; | |
555 | } | |
556 | ||
557 | static int handle_ep0(struct imx_ep_struct *imx_ep) | |
558 | { | |
559 | struct imx_request *req = NULL; | |
560 | int ret = 0; | |
561 | ||
8f182e5d | 562 | if (!list_empty(&imx_ep->queue)) { |
2a4f136f DA |
563 | req = list_entry(imx_ep->queue.next, struct imx_request, queue); |
564 | ||
2a4f136f DA |
565 | switch (imx_ep->imx_usb->ep0state) { |
566 | ||
567 | case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR */ | |
568 | write_fifo(imx_ep, req); | |
569 | break; | |
570 | case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR */ | |
571 | read_fifo(imx_ep, req); | |
572 | break; | |
573 | default: | |
574 | D_EP0(imx_ep->imx_usb->dev, | |
575 | "<%s> ep0 i/o, odd state %d\n", | |
576 | __func__, imx_ep->imx_usb->ep0state); | |
577 | ep_del_request(imx_ep, req); | |
578 | ret = -EL2HLT; | |
579 | break; | |
580 | } | |
581 | } | |
582 | ||
8f182e5d DA |
583 | else |
584 | D_ERR(imx_ep->imx_usb->dev, "<%s> no request on %s\n", | |
585 | __func__, imx_ep->ep.name); | |
586 | ||
2a4f136f DA |
587 | return ret; |
588 | } | |
589 | ||
590 | static void handle_ep0_devreq(struct imx_udc_struct *imx_usb) | |
591 | { | |
592 | struct imx_ep_struct *imx_ep = &imx_usb->imx_ep[0]; | |
593 | union { | |
594 | struct usb_ctrlrequest r; | |
595 | u8 raw[8]; | |
596 | u32 word[2]; | |
597 | } u; | |
598 | int temp, i; | |
599 | ||
600 | nuke(imx_ep, -EPROTO); | |
601 | ||
602 | /* read SETUP packet */ | |
603 | for (i = 0; i < 2; i++) { | |
604 | if (imx_ep_empty(imx_ep)) { | |
605 | D_ERR(imx_usb->dev, | |
606 | "<%s> no setup packet received\n", __func__); | |
607 | goto stall; | |
608 | } | |
593bef6c DA |
609 | u.word[i] = __raw_readl(imx_usb->base |
610 | + USB_EP_FDAT(EP_NO(imx_ep))); | |
2a4f136f DA |
611 | } |
612 | ||
613 | temp = imx_ep_empty(imx_ep); | |
614 | while (!imx_ep_empty(imx_ep)) { | |
615 | i = __raw_readl(imx_usb->base + USB_EP_FDAT(EP_NO(imx_ep))); | |
616 | D_ERR(imx_usb->dev, | |
617 | "<%s> wrong to have extra bytes for setup : 0x%08x\n", | |
618 | __func__, i); | |
619 | } | |
620 | if (!temp) | |
621 | goto stall; | |
622 | ||
623 | le16_to_cpus(&u.r.wValue); | |
624 | le16_to_cpus(&u.r.wIndex); | |
625 | le16_to_cpus(&u.r.wLength); | |
626 | ||
627 | D_REQ(imx_usb->dev, "<%s> SETUP %02x.%02x v%04x i%04x l%04x\n", | |
628 | __func__, u.r.bRequestType, u.r.bRequest, | |
629 | u.r.wValue, u.r.wIndex, u.r.wLength); | |
630 | ||
631 | if (imx_usb->set_config) { | |
632 | /* NACK the host by using CMDOVER */ | |
633 | temp = __raw_readl(imx_usb->base + USB_CTRL); | |
634 | __raw_writel(temp | CTRL_CMDOVER, imx_usb->base + USB_CTRL); | |
635 | ||
636 | D_ERR(imx_usb->dev, | |
637 | "<%s> set config req is pending, NACK the host\n", | |
638 | __func__); | |
639 | return; | |
640 | } | |
641 | ||
642 | if (u.r.bRequestType & USB_DIR_IN) | |
643 | ep0_chg_stat(__func__, imx_usb, EP0_IN_DATA_PHASE); | |
644 | else | |
645 | ep0_chg_stat(__func__, imx_usb, EP0_OUT_DATA_PHASE); | |
646 | ||
647 | i = imx_usb->driver->setup(&imx_usb->gadget, &u.r); | |
648 | if (i < 0) { | |
649 | D_ERR(imx_usb->dev, "<%s> device setup error %d\n", | |
650 | __func__, i); | |
651 | goto stall; | |
652 | } | |
653 | ||
654 | return; | |
655 | stall: | |
656 | D_ERR(imx_usb->dev, "<%s> protocol STALL\n", __func__); | |
657 | imx_ep_stall(imx_ep); | |
658 | ep0_chg_stat(__func__, imx_usb, EP0_STALL); | |
659 | return; | |
660 | } | |
661 | ||
662 | /******************************************************************************* | |
663 | * USB gadget callback functions | |
664 | ******************************************************************************* | |
665 | */ | |
666 | ||
667 | static int imx_ep_enable(struct usb_ep *usb_ep, | |
668 | const struct usb_endpoint_descriptor *desc) | |
669 | { | |
670 | struct imx_ep_struct *imx_ep = container_of(usb_ep, | |
671 | struct imx_ep_struct, ep); | |
672 | struct imx_udc_struct *imx_usb = imx_ep->imx_usb; | |
673 | unsigned long flags; | |
674 | ||
675 | if (!usb_ep | |
676 | || !desc | |
677 | || !EP_NO(imx_ep) | |
678 | || desc->bDescriptorType != USB_DT_ENDPOINT | |
679 | || imx_ep->bEndpointAddress != desc->bEndpointAddress) { | |
680 | D_ERR(imx_usb->dev, | |
681 | "<%s> bad ep or descriptor\n", __func__); | |
682 | return -EINVAL; | |
683 | } | |
684 | ||
685 | if (imx_ep->bmAttributes != desc->bmAttributes) { | |
686 | D_ERR(imx_usb->dev, | |
687 | "<%s> %s type mismatch\n", __func__, usb_ep->name); | |
688 | return -EINVAL; | |
689 | } | |
690 | ||
691 | if (imx_ep->fifosize < le16_to_cpu(desc->wMaxPacketSize)) { | |
692 | D_ERR(imx_usb->dev, | |
693 | "<%s> bad %s maxpacket\n", __func__, usb_ep->name); | |
694 | return -ERANGE; | |
695 | } | |
696 | ||
697 | if (!imx_usb->driver || imx_usb->gadget.speed == USB_SPEED_UNKNOWN) { | |
698 | D_ERR(imx_usb->dev, "<%s> bogus device state\n", __func__); | |
699 | return -ESHUTDOWN; | |
700 | } | |
701 | ||
702 | local_irq_save(flags); | |
703 | ||
704 | imx_ep->stopped = 0; | |
705 | imx_flush(imx_ep); | |
706 | imx_ep_irq_enable(imx_ep); | |
707 | ||
708 | local_irq_restore(flags); | |
709 | ||
710 | D_EPX(imx_usb->dev, "<%s> ENABLED %s\n", __func__, usb_ep->name); | |
711 | return 0; | |
712 | } | |
713 | ||
714 | static int imx_ep_disable(struct usb_ep *usb_ep) | |
715 | { | |
716 | struct imx_ep_struct *imx_ep = container_of(usb_ep, | |
717 | struct imx_ep_struct, ep); | |
718 | unsigned long flags; | |
719 | ||
720 | if (!usb_ep || !EP_NO(imx_ep) || !list_empty(&imx_ep->queue)) { | |
721 | D_ERR(imx_ep->imx_usb->dev, "<%s> %s can not be disabled\n", | |
722 | __func__, usb_ep ? imx_ep->ep.name : NULL); | |
723 | return -EINVAL; | |
724 | } | |
725 | ||
726 | local_irq_save(flags); | |
727 | ||
728 | imx_ep->stopped = 1; | |
729 | nuke(imx_ep, -ESHUTDOWN); | |
730 | imx_flush(imx_ep); | |
731 | imx_ep_irq_disable(imx_ep); | |
732 | ||
733 | local_irq_restore(flags); | |
734 | ||
735 | D_EPX(imx_ep->imx_usb->dev, | |
736 | "<%s> DISABLED %s\n", __func__, usb_ep->name); | |
737 | return 0; | |
738 | } | |
739 | ||
740 | static struct usb_request *imx_ep_alloc_request | |
741 | (struct usb_ep *usb_ep, gfp_t gfp_flags) | |
742 | { | |
743 | struct imx_request *req; | |
744 | ||
1e0abb7e DM |
745 | if (!usb_ep) |
746 | return NULL; | |
747 | ||
2a4f136f | 748 | req = kzalloc(sizeof *req, gfp_flags); |
1e0abb7e DM |
749 | if (!req) |
750 | return NULL; | |
2a4f136f DA |
751 | |
752 | INIT_LIST_HEAD(&req->queue); | |
753 | req->in_use = 0; | |
754 | ||
755 | return &req->req; | |
756 | } | |
757 | ||
758 | static void imx_ep_free_request | |
759 | (struct usb_ep *usb_ep, struct usb_request *usb_req) | |
760 | { | |
761 | struct imx_request *req; | |
762 | ||
763 | req = container_of(usb_req, struct imx_request, req); | |
764 | WARN_ON(!list_empty(&req->queue)); | |
765 | kfree(req); | |
766 | } | |
767 | ||
768 | static int imx_ep_queue | |
769 | (struct usb_ep *usb_ep, struct usb_request *usb_req, gfp_t gfp_flags) | |
770 | { | |
771 | struct imx_ep_struct *imx_ep; | |
772 | struct imx_udc_struct *imx_usb; | |
773 | struct imx_request *req; | |
774 | unsigned long flags; | |
775 | int ret = 0; | |
776 | ||
777 | imx_ep = container_of(usb_ep, struct imx_ep_struct, ep); | |
778 | imx_usb = imx_ep->imx_usb; | |
779 | req = container_of(usb_req, struct imx_request, req); | |
780 | ||
781 | /* | |
782 | Special care on IMX udc. | |
783 | Ignore enqueue when after set configuration from the | |
784 | host. This assume all gadget drivers reply set | |
785 | configuration with the next ep0 req enqueue. | |
786 | */ | |
787 | if (imx_usb->set_config && !EP_NO(imx_ep)) { | |
788 | imx_usb->set_config = 0; | |
8f182e5d | 789 | D_ERR(imx_usb->dev, |
2a4f136f DA |
790 | "<%s> gadget reply set config\n", __func__); |
791 | return 0; | |
792 | } | |
793 | ||
794 | if (unlikely(!usb_req || !req || !usb_req->complete || !usb_req->buf)) { | |
795 | D_ERR(imx_usb->dev, "<%s> bad params\n", __func__); | |
796 | return -EINVAL; | |
797 | } | |
798 | ||
799 | if (unlikely(!usb_ep || !imx_ep)) { | |
800 | D_ERR(imx_usb->dev, "<%s> bad ep\n", __func__); | |
801 | return -EINVAL; | |
802 | } | |
803 | ||
804 | if (!imx_usb->driver || imx_usb->gadget.speed == USB_SPEED_UNKNOWN) { | |
805 | D_ERR(imx_usb->dev, "<%s> bogus device state\n", __func__); | |
806 | return -ESHUTDOWN; | |
807 | } | |
808 | ||
2a4f136f DA |
809 | /* Debug */ |
810 | D_REQ(imx_usb->dev, "<%s> ep%d %s request for [%d] bytes\n", | |
811 | __func__, EP_NO(imx_ep), | |
593bef6c DA |
812 | ((!EP_NO(imx_ep) && imx_ep->imx_usb->ep0state |
813 | == EP0_IN_DATA_PHASE) | |
814 | || (EP_NO(imx_ep) && EP_DIR(imx_ep))) | |
815 | ? "IN" : "OUT", usb_req->length); | |
2a4f136f DA |
816 | dump_req(__func__, imx_ep, usb_req); |
817 | ||
818 | if (imx_ep->stopped) { | |
819 | usb_req->status = -ESHUTDOWN; | |
8f182e5d | 820 | return -ESHUTDOWN; |
2a4f136f DA |
821 | } |
822 | ||
823 | if (req->in_use) { | |
824 | D_ERR(imx_usb->dev, | |
825 | "<%s> refusing to queue req %p (already queued)\n", | |
826 | __func__, req); | |
8f182e5d | 827 | return 0; |
2a4f136f DA |
828 | } |
829 | ||
8f182e5d DA |
830 | local_irq_save(flags); |
831 | ||
2a4f136f DA |
832 | usb_req->status = -EINPROGRESS; |
833 | usb_req->actual = 0; | |
834 | ||
835 | ep_add_request(imx_ep, req); | |
836 | ||
837 | if (!EP_NO(imx_ep)) | |
838 | ret = handle_ep0(imx_ep); | |
839 | else | |
840 | ret = handle_ep(imx_ep); | |
8f182e5d | 841 | |
2a4f136f DA |
842 | local_irq_restore(flags); |
843 | return ret; | |
844 | } | |
845 | ||
846 | static int imx_ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req) | |
847 | { | |
848 | ||
849 | struct imx_ep_struct *imx_ep = container_of | |
850 | (usb_ep, struct imx_ep_struct, ep); | |
851 | struct imx_request *req; | |
852 | unsigned long flags; | |
853 | ||
854 | if (unlikely(!usb_ep || !EP_NO(imx_ep))) { | |
855 | D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__); | |
856 | return -EINVAL; | |
857 | } | |
858 | ||
859 | local_irq_save(flags); | |
860 | ||
861 | /* make sure it's actually queued on this endpoint */ | |
862 | list_for_each_entry(req, &imx_ep->queue, queue) { | |
863 | if (&req->req == usb_req) | |
864 | break; | |
865 | } | |
866 | if (&req->req != usb_req) { | |
867 | local_irq_restore(flags); | |
868 | return -EINVAL; | |
869 | } | |
870 | ||
871 | done(imx_ep, req, -ECONNRESET); | |
872 | ||
873 | local_irq_restore(flags); | |
874 | return 0; | |
875 | } | |
876 | ||
877 | static int imx_ep_set_halt(struct usb_ep *usb_ep, int value) | |
878 | { | |
879 | struct imx_ep_struct *imx_ep = container_of | |
880 | (usb_ep, struct imx_ep_struct, ep); | |
881 | unsigned long flags; | |
882 | ||
883 | if (unlikely(!usb_ep || !EP_NO(imx_ep))) { | |
884 | D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__); | |
885 | return -EINVAL; | |
886 | } | |
887 | ||
888 | local_irq_save(flags); | |
889 | ||
890 | if ((imx_ep->bEndpointAddress & USB_DIR_IN) | |
891 | && !list_empty(&imx_ep->queue)) { | |
892 | local_irq_restore(flags); | |
893 | return -EAGAIN; | |
894 | } | |
895 | ||
896 | imx_ep_stall(imx_ep); | |
897 | ||
898 | local_irq_restore(flags); | |
899 | ||
900 | D_EPX(imx_ep->imx_usb->dev, "<%s> %s halt\n", __func__, usb_ep->name); | |
901 | return 0; | |
902 | } | |
903 | ||
904 | static int imx_ep_fifo_status(struct usb_ep *usb_ep) | |
905 | { | |
906 | struct imx_ep_struct *imx_ep = container_of | |
907 | (usb_ep, struct imx_ep_struct, ep); | |
908 | ||
909 | if (!usb_ep) { | |
910 | D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__); | |
911 | return -ENODEV; | |
912 | } | |
913 | ||
914 | if (imx_ep->imx_usb->gadget.speed == USB_SPEED_UNKNOWN) | |
915 | return 0; | |
916 | else | |
917 | return imx_fifo_bcount(imx_ep); | |
918 | } | |
919 | ||
920 | static void imx_ep_fifo_flush(struct usb_ep *usb_ep) | |
921 | { | |
922 | struct imx_ep_struct *imx_ep = container_of | |
923 | (usb_ep, struct imx_ep_struct, ep); | |
924 | unsigned long flags; | |
925 | ||
926 | local_irq_save(flags); | |
927 | ||
928 | if (!usb_ep || !EP_NO(imx_ep) || !list_empty(&imx_ep->queue)) { | |
929 | D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__); | |
930 | local_irq_restore(flags); | |
931 | return; | |
932 | } | |
933 | ||
934 | /* toggle and halt bits stay unchanged */ | |
935 | imx_flush(imx_ep); | |
936 | ||
937 | local_irq_restore(flags); | |
938 | } | |
939 | ||
940 | static struct usb_ep_ops imx_ep_ops = { | |
941 | .enable = imx_ep_enable, | |
942 | .disable = imx_ep_disable, | |
943 | ||
944 | .alloc_request = imx_ep_alloc_request, | |
945 | .free_request = imx_ep_free_request, | |
946 | ||
947 | .queue = imx_ep_queue, | |
948 | .dequeue = imx_ep_dequeue, | |
949 | ||
950 | .set_halt = imx_ep_set_halt, | |
951 | .fifo_status = imx_ep_fifo_status, | |
952 | .fifo_flush = imx_ep_fifo_flush, | |
953 | }; | |
954 | ||
955 | /******************************************************************************* | |
956 | * USB endpoint control functions | |
957 | ******************************************************************************* | |
958 | */ | |
959 | ||
960 | void ep0_chg_stat(const char *label, | |
961 | struct imx_udc_struct *imx_usb, enum ep0_state stat) | |
962 | { | |
963 | D_EP0(imx_usb->dev, "<%s> from %15s to %15s\n", | |
964 | label, state_name[imx_usb->ep0state], state_name[stat]); | |
965 | ||
966 | if (imx_usb->ep0state == stat) | |
967 | return; | |
968 | ||
969 | imx_usb->ep0state = stat; | |
970 | } | |
971 | ||
972 | static void usb_init_data(struct imx_udc_struct *imx_usb) | |
973 | { | |
974 | struct imx_ep_struct *imx_ep; | |
975 | u8 i; | |
976 | ||
977 | /* device/ep0 records init */ | |
978 | INIT_LIST_HEAD(&imx_usb->gadget.ep_list); | |
979 | INIT_LIST_HEAD(&imx_usb->gadget.ep0->ep_list); | |
980 | ep0_chg_stat(__func__, imx_usb, EP0_IDLE); | |
981 | ||
982 | /* basic endpoint records init */ | |
983 | for (i = 0; i < IMX_USB_NB_EP; i++) { | |
984 | imx_ep = &imx_usb->imx_ep[i]; | |
985 | ||
986 | if (i) { | |
987 | list_add_tail(&imx_ep->ep.ep_list, | |
988 | &imx_usb->gadget.ep_list); | |
989 | imx_ep->stopped = 1; | |
990 | } else | |
991 | imx_ep->stopped = 0; | |
992 | ||
993 | INIT_LIST_HEAD(&imx_ep->queue); | |
994 | } | |
995 | } | |
996 | ||
997 | static void udc_stop_activity(struct imx_udc_struct *imx_usb, | |
998 | struct usb_gadget_driver *driver) | |
999 | { | |
1000 | struct imx_ep_struct *imx_ep; | |
1001 | int i; | |
1002 | ||
1003 | if (imx_usb->gadget.speed == USB_SPEED_UNKNOWN) | |
1004 | driver = NULL; | |
1005 | ||
1006 | /* prevent new request submissions, kill any outstanding requests */ | |
1007 | for (i = 1; i < IMX_USB_NB_EP; i++) { | |
1008 | imx_ep = &imx_usb->imx_ep[i]; | |
1009 | imx_flush(imx_ep); | |
1010 | imx_ep->stopped = 1; | |
1011 | imx_ep_irq_disable(imx_ep); | |
1012 | nuke(imx_ep, -ESHUTDOWN); | |
1013 | } | |
1014 | ||
1015 | imx_usb->cfg = 0; | |
1016 | imx_usb->intf = 0; | |
1017 | imx_usb->alt = 0; | |
1018 | ||
1019 | if (driver) | |
1020 | driver->disconnect(&imx_usb->gadget); | |
1021 | } | |
1022 | ||
1023 | /******************************************************************************* | |
1024 | * Interrupt handlers | |
1025 | ******************************************************************************* | |
1026 | */ | |
1027 | ||
b633d28e DA |
1028 | /* |
1029 | * Called when timer expires. | |
1030 | * Timer is started when CFG_CHG is received. | |
1031 | */ | |
1032 | static void handle_config(unsigned long data) | |
2a4f136f | 1033 | { |
b633d28e | 1034 | struct imx_udc_struct *imx_usb = (void *)data; |
2a4f136f DA |
1035 | struct usb_ctrlrequest u; |
1036 | int temp, cfg, intf, alt; | |
2a4f136f | 1037 | |
b633d28e | 1038 | local_irq_disable(); |
2a4f136f | 1039 | |
b633d28e DA |
1040 | temp = __raw_readl(imx_usb->base + USB_STAT); |
1041 | cfg = (temp & STAT_CFG) >> 5; | |
1042 | intf = (temp & STAT_INTF) >> 3; | |
1043 | alt = temp & STAT_ALTSET; | |
2a4f136f | 1044 | |
b633d28e DA |
1045 | D_REQ(imx_usb->dev, |
1046 | "<%s> orig config C=%d, I=%d, A=%d / " | |
1047 | "req config C=%d, I=%d, A=%d\n", | |
1048 | __func__, imx_usb->cfg, imx_usb->intf, imx_usb->alt, | |
1049 | cfg, intf, alt); | |
2a4f136f | 1050 | |
b633d28e | 1051 | if (cfg == 1 || cfg == 2) { |
2a4f136f | 1052 | |
2a4f136f | 1053 | if (imx_usb->cfg != cfg) { |
2a4f136f DA |
1054 | u.bRequest = USB_REQ_SET_CONFIGURATION; |
1055 | u.bRequestType = USB_DIR_OUT | | |
1056 | USB_TYPE_STANDARD | | |
1057 | USB_RECIP_DEVICE; | |
1058 | u.wValue = cfg; | |
1059 | u.wIndex = 0; | |
1060 | u.wLength = 0; | |
1061 | imx_usb->cfg = cfg; | |
2a4f136f | 1062 | imx_usb->driver->setup(&imx_usb->gadget, &u); |
2a4f136f DA |
1063 | |
1064 | } | |
1065 | if (imx_usb->intf != intf || imx_usb->alt != alt) { | |
2a4f136f DA |
1066 | u.bRequest = USB_REQ_SET_INTERFACE; |
1067 | u.bRequestType = USB_DIR_OUT | | |
1068 | USB_TYPE_STANDARD | | |
1069 | USB_RECIP_INTERFACE; | |
1070 | u.wValue = alt; | |
1071 | u.wIndex = intf; | |
1072 | u.wLength = 0; | |
1073 | imx_usb->intf = intf; | |
1074 | imx_usb->alt = alt; | |
2a4f136f | 1075 | imx_usb->driver->setup(&imx_usb->gadget, &u); |
2a4f136f DA |
1076 | } |
1077 | } | |
1078 | ||
b633d28e DA |
1079 | imx_usb->set_config = 0; |
1080 | ||
1081 | local_irq_enable(); | |
1082 | } | |
1083 | ||
1084 | static irqreturn_t imx_udc_irq(int irq, void *dev) | |
1085 | { | |
1086 | struct imx_udc_struct *imx_usb = dev; | |
1087 | int intr = __raw_readl(imx_usb->base + USB_INTR); | |
1088 | int temp; | |
1089 | ||
1090 | if (intr & (INTR_WAKEUP | INTR_SUSPEND | INTR_RESUME | INTR_RESET_START | |
1091 | | INTR_RESET_STOP | INTR_CFG_CHG)) { | |
1092 | dump_intr(__func__, intr, imx_usb->dev); | |
1093 | dump_usb_stat(__func__, imx_usb); | |
1094 | } | |
1095 | ||
1096 | if (!imx_usb->driver) | |
1097 | goto end_irq; | |
1098 | ||
2a4f136f | 1099 | if (intr & INTR_SOF) { |
8f182e5d DA |
1100 | /* Copy from Freescale BSP. |
1101 | We must enable SOF intr and set CMDOVER. | |
1102 | Datasheet don't specifiy this action, but it | |
1103 | is done in Freescale BSP, so just copy it. | |
1104 | */ | |
2a4f136f DA |
1105 | if (imx_usb->ep0state == EP0_IDLE) { |
1106 | temp = __raw_readl(imx_usb->base + USB_CTRL); | |
593bef6c DA |
1107 | __raw_writel(temp | CTRL_CMDOVER, |
1108 | imx_usb->base + USB_CTRL); | |
2a4f136f DA |
1109 | } |
1110 | } | |
1111 | ||
b633d28e DA |
1112 | if (intr & INTR_CFG_CHG) { |
1113 | /* A workaround of serious IMX UDC bug. | |
1114 | Handling of CFG_CHG should be delayed for some time, because | |
1115 | IMX does not NACK the host when CFG_CHG interrupt is pending. | |
1116 | There is no time to handle current CFG_CHG | |
1117 | if next CFG_CHG or SETUP packed is send immediately. | |
1118 | We have to clear CFG_CHG, start the timer and | |
1119 | NACK the host by setting CTRL_CMDOVER | |
1120 | if it sends any SETUP packet. | |
1121 | When timer expires, handler is called to handle configuration | |
1122 | changes. While CFG_CHG is not handled (set_config=1), | |
1123 | we must NACK the host to every SETUP packed. | |
1124 | This delay prevents from going out of sync with host. | |
1125 | */ | |
1126 | __raw_writel(INTR_CFG_CHG, imx_usb->base + USB_INTR); | |
1127 | imx_usb->set_config = 1; | |
1128 | mod_timer(&imx_usb->timer, jiffies + 5); | |
1129 | goto end_irq; | |
1130 | } | |
1131 | ||
1132 | if (intr & INTR_WAKEUP) { | |
1133 | if (imx_usb->gadget.speed == USB_SPEED_UNKNOWN | |
1134 | && imx_usb->driver && imx_usb->driver->resume) | |
1135 | imx_usb->driver->resume(&imx_usb->gadget); | |
1136 | imx_usb->set_config = 0; | |
1137 | del_timer(&imx_usb->timer); | |
1138 | imx_usb->gadget.speed = USB_SPEED_FULL; | |
1139 | } | |
1140 | ||
1141 | if (intr & INTR_SUSPEND) { | |
1142 | if (imx_usb->gadget.speed != USB_SPEED_UNKNOWN | |
1143 | && imx_usb->driver && imx_usb->driver->suspend) | |
1144 | imx_usb->driver->suspend(&imx_usb->gadget); | |
1145 | imx_usb->set_config = 0; | |
1146 | del_timer(&imx_usb->timer); | |
1147 | imx_usb->gadget.speed = USB_SPEED_UNKNOWN; | |
1148 | } | |
1149 | ||
1150 | if (intr & INTR_RESET_START) { | |
1151 | __raw_writel(intr, imx_usb->base + USB_INTR); | |
1152 | udc_stop_activity(imx_usb, imx_usb->driver); | |
1153 | imx_usb->set_config = 0; | |
1154 | del_timer(&imx_usb->timer); | |
1155 | imx_usb->gadget.speed = USB_SPEED_UNKNOWN; | |
1156 | } | |
1157 | ||
1158 | if (intr & INTR_RESET_STOP) | |
1159 | imx_usb->gadget.speed = USB_SPEED_FULL; | |
1160 | ||
2a4f136f DA |
1161 | end_irq: |
1162 | __raw_writel(intr, imx_usb->base + USB_INTR); | |
1163 | return IRQ_HANDLED; | |
1164 | } | |
1165 | ||
1166 | static irqreturn_t imx_udc_ctrl_irq(int irq, void *dev) | |
1167 | { | |
1168 | struct imx_udc_struct *imx_usb = dev; | |
d24921a3 | 1169 | struct imx_ep_struct *imx_ep = &imx_usb->imx_ep[0]; |
2a4f136f DA |
1170 | int intr = __raw_readl(imx_usb->base + USB_EP_INTR(0)); |
1171 | ||
1172 | dump_ep_intr(__func__, 0, intr, imx_usb->dev); | |
1173 | ||
1174 | if (!imx_usb->driver) { | |
1175 | __raw_writel(intr, imx_usb->base + USB_EP_INTR(0)); | |
1176 | return IRQ_HANDLED; | |
1177 | } | |
1178 | ||
d24921a3 | 1179 | /* DEVREQ has highest priority */ |
2a4f136f DA |
1180 | if (intr & (EPINTR_DEVREQ | EPINTR_MDEVREQ)) |
1181 | handle_ep0_devreq(imx_usb); | |
1182 | /* Seem i.MX is missing EOF interrupt sometimes. | |
d24921a3 DA |
1183 | * Therefore we don't monitor EOF. |
1184 | * We call handle_ep0() only if a request is queued for ep0. | |
2a4f136f | 1185 | */ |
d24921a3 DA |
1186 | else if (!list_empty(&imx_ep->queue)) |
1187 | handle_ep0(imx_ep); | |
2a4f136f DA |
1188 | |
1189 | __raw_writel(intr, imx_usb->base + USB_EP_INTR(0)); | |
1190 | ||
1191 | return IRQ_HANDLED; | |
1192 | } | |
1193 | ||
1194 | static irqreturn_t imx_udc_bulk_irq(int irq, void *dev) | |
1195 | { | |
1196 | struct imx_udc_struct *imx_usb = dev; | |
1197 | struct imx_ep_struct *imx_ep = &imx_usb->imx_ep[irq - USBD_INT0]; | |
1198 | int intr = __raw_readl(imx_usb->base + USB_EP_INTR(EP_NO(imx_ep))); | |
1199 | ||
1200 | dump_ep_intr(__func__, irq - USBD_INT0, intr, imx_usb->dev); | |
1201 | ||
1202 | if (!imx_usb->driver) { | |
1203 | __raw_writel(intr, imx_usb->base + USB_EP_INTR(EP_NO(imx_ep))); | |
1204 | return IRQ_HANDLED; | |
1205 | } | |
1206 | ||
1207 | handle_ep(imx_ep); | |
1208 | ||
1209 | __raw_writel(intr, imx_usb->base + USB_EP_INTR(EP_NO(imx_ep))); | |
1210 | ||
1211 | return IRQ_HANDLED; | |
1212 | } | |
1213 | ||
1214 | irq_handler_t intr_handler(int i) | |
1215 | { | |
1216 | switch (i) { | |
1217 | case 0: | |
1218 | return imx_udc_ctrl_irq; | |
1219 | case 1: | |
1220 | case 2: | |
1221 | case 3: | |
1222 | case 4: | |
1223 | case 5: | |
1224 | return imx_udc_bulk_irq; | |
1225 | default: | |
1226 | return imx_udc_irq; | |
1227 | } | |
1228 | } | |
1229 | ||
1230 | /******************************************************************************* | |
1231 | * Static defined IMX UDC structure | |
1232 | ******************************************************************************* | |
1233 | */ | |
1234 | ||
1235 | static const struct usb_gadget_ops imx_udc_ops = { | |
1236 | .get_frame = imx_udc_get_frame, | |
1237 | .wakeup = imx_udc_wakeup, | |
1238 | }; | |
1239 | ||
1240 | static struct imx_udc_struct controller = { | |
1241 | .gadget = { | |
1242 | .ops = &imx_udc_ops, | |
1243 | .ep0 = &controller.imx_ep[0].ep, | |
1244 | .name = driver_name, | |
1245 | .dev = { | |
5df58524 KS |
1246 | .init_name = "gadget", |
1247 | }, | |
2a4f136f DA |
1248 | }, |
1249 | ||
1250 | .imx_ep[0] = { | |
1251 | .ep = { | |
1252 | .name = ep0name, | |
1253 | .ops = &imx_ep_ops, | |
1254 | .maxpacket = 32, | |
1255 | }, | |
1256 | .imx_usb = &controller, | |
1257 | .fifosize = 32, | |
1258 | .bEndpointAddress = 0, | |
1259 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, | |
1260 | }, | |
1261 | .imx_ep[1] = { | |
1262 | .ep = { | |
1263 | .name = "ep1in-bulk", | |
1264 | .ops = &imx_ep_ops, | |
1265 | .maxpacket = 64, | |
1266 | }, | |
1267 | .imx_usb = &controller, | |
1268 | .fifosize = 64, | |
1269 | .bEndpointAddress = USB_DIR_IN | 1, | |
1270 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
1271 | }, | |
1272 | .imx_ep[2] = { | |
1273 | .ep = { | |
1274 | .name = "ep2out-bulk", | |
1275 | .ops = &imx_ep_ops, | |
1276 | .maxpacket = 64, | |
1277 | }, | |
1278 | .imx_usb = &controller, | |
1279 | .fifosize = 64, | |
1280 | .bEndpointAddress = USB_DIR_OUT | 2, | |
1281 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
1282 | }, | |
1283 | .imx_ep[3] = { | |
1284 | .ep = { | |
1285 | .name = "ep3out-bulk", | |
1286 | .ops = &imx_ep_ops, | |
1287 | .maxpacket = 32, | |
1288 | }, | |
1289 | .imx_usb = &controller, | |
1290 | .fifosize = 32, | |
1291 | .bEndpointAddress = USB_DIR_OUT | 3, | |
1292 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
1293 | }, | |
1294 | .imx_ep[4] = { | |
1295 | .ep = { | |
1296 | .name = "ep4in-int", | |
1297 | .ops = &imx_ep_ops, | |
1298 | .maxpacket = 32, | |
1299 | }, | |
1300 | .imx_usb = &controller, | |
1301 | .fifosize = 32, | |
1302 | .bEndpointAddress = USB_DIR_IN | 4, | |
1303 | .bmAttributes = USB_ENDPOINT_XFER_INT, | |
1304 | }, | |
1305 | .imx_ep[5] = { | |
1306 | .ep = { | |
1307 | .name = "ep5out-int", | |
1308 | .ops = &imx_ep_ops, | |
1309 | .maxpacket = 32, | |
1310 | }, | |
1311 | .imx_usb = &controller, | |
1312 | .fifosize = 32, | |
1313 | .bEndpointAddress = USB_DIR_OUT | 5, | |
1314 | .bmAttributes = USB_ENDPOINT_XFER_INT, | |
1315 | }, | |
1316 | }; | |
1317 | ||
1318 | /******************************************************************************* | |
1319 | * USB gadged driver functions | |
1320 | ******************************************************************************* | |
1321 | */ | |
1322 | int usb_gadget_register_driver(struct usb_gadget_driver *driver) | |
1323 | { | |
1324 | struct imx_udc_struct *imx_usb = &controller; | |
1325 | int retval; | |
1326 | ||
1327 | if (!driver | |
1328 | || driver->speed < USB_SPEED_FULL | |
1329 | || !driver->bind | |
1330 | || !driver->disconnect | |
1331 | || !driver->setup) | |
1332 | return -EINVAL; | |
1333 | if (!imx_usb) | |
1334 | return -ENODEV; | |
1335 | if (imx_usb->driver) | |
1336 | return -EBUSY; | |
1337 | ||
1338 | /* first hook up the driver ... */ | |
1339 | imx_usb->driver = driver; | |
1340 | imx_usb->gadget.dev.driver = &driver->driver; | |
1341 | ||
1342 | retval = device_add(&imx_usb->gadget.dev); | |
1343 | if (retval) | |
1344 | goto fail; | |
1345 | retval = driver->bind(&imx_usb->gadget); | |
1346 | if (retval) { | |
1347 | D_ERR(imx_usb->dev, "<%s> bind to driver %s --> error %d\n", | |
1348 | __func__, driver->driver.name, retval); | |
1349 | device_del(&imx_usb->gadget.dev); | |
1350 | ||
1351 | goto fail; | |
1352 | } | |
1353 | ||
1354 | D_INI(imx_usb->dev, "<%s> registered gadget driver '%s'\n", | |
1355 | __func__, driver->driver.name); | |
1356 | ||
1357 | imx_udc_enable(imx_usb); | |
1358 | ||
1359 | return 0; | |
1360 | fail: | |
1361 | imx_usb->driver = NULL; | |
1362 | imx_usb->gadget.dev.driver = NULL; | |
1363 | return retval; | |
1364 | } | |
1365 | EXPORT_SYMBOL(usb_gadget_register_driver); | |
1366 | ||
1367 | int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) | |
1368 | { | |
1369 | struct imx_udc_struct *imx_usb = &controller; | |
1370 | ||
1371 | if (!imx_usb) | |
1372 | return -ENODEV; | |
1373 | if (!driver || driver != imx_usb->driver || !driver->unbind) | |
1374 | return -EINVAL; | |
1375 | ||
1376 | udc_stop_activity(imx_usb, driver); | |
1377 | imx_udc_disable(imx_usb); | |
b633d28e | 1378 | del_timer(&imx_usb->timer); |
2a4f136f DA |
1379 | |
1380 | driver->unbind(&imx_usb->gadget); | |
1381 | imx_usb->gadget.dev.driver = NULL; | |
1382 | imx_usb->driver = NULL; | |
1383 | ||
1384 | device_del(&imx_usb->gadget.dev); | |
1385 | ||
1386 | D_INI(imx_usb->dev, "<%s> unregistered gadget driver '%s'\n", | |
1387 | __func__, driver->driver.name); | |
1388 | ||
1389 | return 0; | |
1390 | } | |
1391 | EXPORT_SYMBOL(usb_gadget_unregister_driver); | |
1392 | ||
1393 | /******************************************************************************* | |
1394 | * Module functions | |
1395 | ******************************************************************************* | |
1396 | */ | |
1397 | ||
1398 | static int __init imx_udc_probe(struct platform_device *pdev) | |
1399 | { | |
1400 | struct imx_udc_struct *imx_usb = &controller; | |
1401 | struct resource *res; | |
1402 | struct imxusb_platform_data *pdata; | |
1403 | struct clk *clk; | |
1404 | void __iomem *base; | |
1405 | int ret = 0; | |
d86a83f4 TK |
1406 | int i; |
1407 | resource_size_t res_size; | |
2a4f136f DA |
1408 | |
1409 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
1410 | if (!res) { | |
1411 | dev_err(&pdev->dev, "can't get device resources\n"); | |
1412 | return -ENODEV; | |
1413 | } | |
1414 | ||
1415 | pdata = pdev->dev.platform_data; | |
1416 | if (!pdata) { | |
1417 | dev_err(&pdev->dev, "driver needs platform data\n"); | |
1418 | return -ENODEV; | |
1419 | } | |
1420 | ||
d86a83f4 | 1421 | res_size = resource_size(res); |
2a4f136f DA |
1422 | if (!request_mem_region(res->start, res_size, res->name)) { |
1423 | dev_err(&pdev->dev, "can't allocate %d bytes at %d address\n", | |
1424 | res_size, res->start); | |
1425 | return -ENOMEM; | |
1426 | } | |
1427 | ||
1428 | if (pdata->init) { | |
1429 | ret = pdata->init(&pdev->dev); | |
1430 | if (ret) | |
1431 | goto fail0; | |
1432 | } | |
1433 | ||
1434 | base = ioremap(res->start, res_size); | |
1435 | if (!base) { | |
1436 | dev_err(&pdev->dev, "ioremap failed\n"); | |
1437 | ret = -EIO; | |
1438 | goto fail1; | |
1439 | } | |
1440 | ||
1441 | clk = clk_get(NULL, "usbd_clk"); | |
1442 | if (IS_ERR(clk)) { | |
1443 | ret = PTR_ERR(clk); | |
1444 | dev_err(&pdev->dev, "can't get USB clock\n"); | |
1445 | goto fail2; | |
1446 | } | |
1447 | clk_enable(clk); | |
1448 | ||
1449 | if (clk_get_rate(clk) != 48000000) { | |
1450 | D_INI(&pdev->dev, | |
1451 | "Bad USB clock (%d Hz), changing to 48000000 Hz\n", | |
1452 | (int)clk_get_rate(clk)); | |
1453 | if (clk_set_rate(clk, 48000000)) { | |
1454 | dev_err(&pdev->dev, | |
1455 | "Unable to set correct USB clock (48MHz)\n"); | |
1456 | ret = -EIO; | |
1457 | goto fail3; | |
1458 | } | |
1459 | } | |
1460 | ||
1461 | for (i = 0; i < IMX_USB_NB_EP + 1; i++) { | |
1462 | imx_usb->usbd_int[i] = platform_get_irq(pdev, i); | |
1463 | if (imx_usb->usbd_int[i] < 0) { | |
1464 | dev_err(&pdev->dev, "can't get irq number\n"); | |
1465 | ret = -ENODEV; | |
1466 | goto fail3; | |
1467 | } | |
1468 | } | |
1469 | ||
1470 | for (i = 0; i < IMX_USB_NB_EP + 1; i++) { | |
1471 | ret = request_irq(imx_usb->usbd_int[i], intr_handler(i), | |
1472 | IRQF_DISABLED, driver_name, imx_usb); | |
1473 | if (ret) { | |
1474 | dev_err(&pdev->dev, "can't get irq %i, err %d\n", | |
1475 | imx_usb->usbd_int[i], ret); | |
1476 | for (--i; i >= 0; i--) | |
1477 | free_irq(imx_usb->usbd_int[i], imx_usb); | |
1478 | goto fail3; | |
1479 | } | |
1480 | } | |
1481 | ||
1482 | imx_usb->res = res; | |
1483 | imx_usb->base = base; | |
1484 | imx_usb->clk = clk; | |
1485 | imx_usb->dev = &pdev->dev; | |
1486 | ||
1487 | device_initialize(&imx_usb->gadget.dev); | |
1488 | ||
1489 | imx_usb->gadget.dev.parent = &pdev->dev; | |
1490 | imx_usb->gadget.dev.dma_mask = pdev->dev.dma_mask; | |
1491 | ||
1492 | platform_set_drvdata(pdev, imx_usb); | |
1493 | ||
1494 | usb_init_data(imx_usb); | |
1495 | imx_udc_init(imx_usb); | |
1496 | ||
b633d28e DA |
1497 | init_timer(&imx_usb->timer); |
1498 | imx_usb->timer.function = handle_config; | |
1499 | imx_usb->timer.data = (unsigned long)imx_usb; | |
1500 | ||
2a4f136f DA |
1501 | return 0; |
1502 | ||
1503 | fail3: | |
1504 | clk_put(clk); | |
1505 | clk_disable(clk); | |
1506 | fail2: | |
1507 | iounmap(base); | |
1508 | fail1: | |
1509 | if (pdata->exit) | |
1510 | pdata->exit(&pdev->dev); | |
1511 | fail0: | |
1512 | release_mem_region(res->start, res_size); | |
1513 | return ret; | |
1514 | } | |
1515 | ||
1516 | static int __exit imx_udc_remove(struct platform_device *pdev) | |
1517 | { | |
1518 | struct imx_udc_struct *imx_usb = platform_get_drvdata(pdev); | |
1519 | struct imxusb_platform_data *pdata = pdev->dev.platform_data; | |
1520 | int i; | |
1521 | ||
1522 | imx_udc_disable(imx_usb); | |
b633d28e | 1523 | del_timer(&imx_usb->timer); |
2a4f136f DA |
1524 | |
1525 | for (i = 0; i < IMX_USB_NB_EP + 1; i++) | |
1526 | free_irq(imx_usb->usbd_int[i], imx_usb); | |
1527 | ||
1528 | clk_put(imx_usb->clk); | |
1529 | clk_disable(imx_usb->clk); | |
1530 | iounmap(imx_usb->base); | |
1531 | ||
d86a83f4 | 1532 | release_mem_region(imx_usb->res->start, resource_size(imx_usb->res)); |
2a4f136f DA |
1533 | |
1534 | if (pdata->exit) | |
1535 | pdata->exit(&pdev->dev); | |
1536 | ||
1537 | platform_set_drvdata(pdev, NULL); | |
1538 | ||
1539 | return 0; | |
1540 | } | |
1541 | ||
1542 | /*----------------------------------------------------------------------------*/ | |
1543 | ||
1544 | #ifdef CONFIG_PM | |
1545 | #define imx_udc_suspend NULL | |
1546 | #define imx_udc_resume NULL | |
1547 | #else | |
1548 | #define imx_udc_suspend NULL | |
1549 | #define imx_udc_resume NULL | |
1550 | #endif | |
1551 | ||
1552 | /*----------------------------------------------------------------------------*/ | |
1553 | ||
1554 | static struct platform_driver udc_driver = { | |
1555 | .driver = { | |
1556 | .name = driver_name, | |
1557 | .owner = THIS_MODULE, | |
1558 | }, | |
1559 | .remove = __exit_p(imx_udc_remove), | |
1560 | .suspend = imx_udc_suspend, | |
1561 | .resume = imx_udc_resume, | |
1562 | }; | |
1563 | ||
1564 | static int __init udc_init(void) | |
1565 | { | |
1566 | return platform_driver_probe(&udc_driver, imx_udc_probe); | |
1567 | } | |
1568 | module_init(udc_init); | |
1569 | ||
1570 | static void __exit udc_exit(void) | |
1571 | { | |
1572 | platform_driver_unregister(&udc_driver); | |
1573 | } | |
1574 | module_exit(udc_exit); | |
1575 | ||
1576 | MODULE_DESCRIPTION("IMX USB Device Controller driver"); | |
1577 | MODULE_AUTHOR("Darius Augulis <augulis.darius@gmail.com>"); | |
1578 | MODULE_LICENSE("GPL"); | |
1579 | MODULE_ALIAS("platform:imx_udc"); |