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