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