2 * drivers/usb/gadget/lpc32xx_udc.c
4 * Author: Kevin Wells <kevin.wells@nxp.com>
6 * Copyright (C) 2009 NXP Semiconductors
7 * Copyright (C) 2006 Mike James , Philips Semiconductors
9 * Note: This driver is based on original work done by Mike James for
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 * ISO functionality is untested. It probably works, but may not be reliable.
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/platform_device.h>
36 #include <linux/delay.h>
37 #include <linux/ioport.h>
38 #include <linux/slab.h>
39 #include <linux/errno.h>
40 #include <linux/init.h>
41 #include <linux/list.h>
42 #include <linux/interrupt.h>
43 #include <linux/proc_fs.h>
44 #include <linux/clk.h>
45 #include <linux/usb/ch9.h>
46 #include <linux/usb/gadget.h>
47 #include <linux/i2c.h>
48 #include <linux/kthread.h>
49 #include <linux/freezer.h>
50 #include <linux/dma-mapping.h>
51 #include <linux/dmapool.h>
53 #include <asm/byteorder.h>
54 #include <mach/hardware.h>
57 #include <asm/system.h>
59 #include <mach/platform.h>
60 #include <mach/irqs.h>
61 #include <mach/usbd.h>
62 #include <mach/board.h>
63 #include "lpc32xx_udc.h"
64 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
65 #include <linux/seq_file.h>
68 #define UDCA_BUFF_SIZE (128)
70 #define USB_CTRL IO_ADDRESS(LPC32XX_CLK_PM_BASE + 0x64)
71 #define USB_CLOCK_MASK (AHB_M_CLOCK_ON| OTG_CLOCK_ON | DEV_CLOCK_ON | I2C_CLOCK_ON)
73 /* USB_CTRL bit defines */
74 #define USB_SLAVE_HCLK_EN (1 << 24)
75 #define USB_HOST_NEED_CLK_EN (1 << 21)
76 #define USB_DEV_NEED_CLK_EN (1 << 22)
78 #define USB_OTG_CLK_CTRL IO_ADDRESS(LPC32XX_USB_BASE + 0xFF4)
79 #define USB_OTG_CLK_STAT IO_ADDRESS(LPC32XX_USB_BASE + 0xFF8)
81 /* USB_OTG_CLK_CTRL bit defines */
82 #define AHB_M_CLOCK_ON (1 << 4)
83 #define OTG_CLOCK_ON (1 << 3)
84 #define I2C_CLOCK_ON (1 << 2)
85 #define DEV_CLOCK_ON (1 << 1)
86 #define HOST_CLOCK_ON (1 << 0)
88 #define USB_OTG_STAT_CONTROL IO_ADDRESS(LPC32XX_USB_BASE + 0x110)
90 /* USB_OTG_STAT_CONTROL bit defines */
91 #define TRANSPARENT_I2C_EN (1 << 7)
92 #define HOST_EN (1 << 0)
94 /* ISP1301 USB transceiver I2C registers */
95 #define ISP1301_MODE_CONTROL_1 0x04 /* u8 read, set, +1 clear */
97 #define MC1_SPEED_REG (1 << 0)
98 #define MC1_SUSPEND_REG (1 << 1)
99 #define MC1_DAT_SE0 (1 << 2)
100 #define MC1_TRANSPARENT (1 << 3)
101 #define MC1_BDIS_ACON_EN (1 << 4)
102 #define MC1_OE_INT_EN (1 << 5)
103 #define MC1_UART_EN (1 << 6)
104 #define MC1_MASK 0x7f
106 #define ISP1301_MODE_CONTROL_2 0x12 /* u8 read, set, +1 clear */
108 #define MC2_GLOBAL_PWR_DN (1 << 0)
109 #define MC2_SPD_SUSP_CTRL (1 << 1)
110 #define MC2_BI_DI (1 << 2)
111 #define MC2_TRANSP_BDIR0 (1 << 3)
112 #define MC2_TRANSP_BDIR1 (1 << 4)
113 #define MC2_AUDIO_EN (1 << 5)
114 #define MC2_PSW_EN (1 << 6)
115 #define MC2_EN2V7 (1 << 7)
117 #define ISP1301_OTG_CONTROL_1 0x06 /* u8 read, set, +1 clear */
118 #define OTG1_DP_PULLUP (1 << 0)
119 #define OTG1_DM_PULLUP (1 << 1)
120 #define OTG1_DP_PULLDOWN (1 << 2)
121 #define OTG1_DM_PULLDOWN (1 << 3)
122 #define OTG1_ID_PULLDOWN (1 << 4)
123 #define OTG1_VBUS_DRV (1 << 5)
124 #define OTG1_VBUS_DISCHRG (1 << 6)
125 #define OTG1_VBUS_CHRG (1 << 7)
126 #define ISP1301_OTG_STATUS 0x10 /* u8 readonly */
127 #define OTG_B_SESS_END (1 << 6)
128 #define OTG_B_SESS_VLD (1 << 7)
130 #define INT_CR_INT (1 << 7)
131 #define INT_BDIS_ACON (1 << 6)
132 #define INT_ID_FLOAT (1 << 5)
133 #define INT_DM_HI (1 << 4)
134 #define INT_ID_GND (1 << 3)
135 #define INT_DP_HI (1 << 2)
136 #define INT_SESS_VLD (1 << 1)
137 #define INT_VBUS_VLD (1 << 0)
139 #define ISP1301_I2C_ADDR 0x2C
141 #define ISP1301_I2C_MODE_CONTROL_1 0x4
142 #define ISP1301_I2C_MODE_CONTROL_2 0x12
143 #define ISP1301_I2C_OTG_CONTROL_1 0x6
144 #define ISP1301_I2C_OTG_CONTROL_2 0x10
145 #define ISP1301_I2C_INTERRUPT_SOURCE 0x8
146 #define ISP1301_I2C_INTERRUPT_LATCH 0xA
147 #define ISP1301_I2C_INTERRUPT_FALLING 0xC
148 #define ISP1301_I2C_INTERRUPT_RISING 0xE
149 #define ISP1301_I2C_REG_CLEAR_ADDR 1
151 #define DRIVER_VERSION "$Revision: 1.01 $"
152 static const char driver_name [] = "lpc32xx_udc";
154 static void udc_set_address(struct lpc32xx_udc *udc, u32 addr);
155 #if defined(UDC_ENABLE_DMA)
156 static int udc_ep_in_req_dma(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep);
157 static int udc_ep_out_req_dma(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep);
159 static int udc_ep_in_req(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep);
160 static int udc_ep_out_req(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep);
162 static int udc_ep0_in_req(struct lpc32xx_udc *udc);
163 static int udc_ep0_out_req(struct lpc32xx_udc *udc);
167 * proc interface support
170 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
171 static char *epnames[] = {"INT", "ISO", "BULK", "CTRL"};
172 static const char debug_filename[] = "driver/udc";
174 static void proc_ep_show(struct seq_file *s, struct lpc32xx_ep *ep)
176 struct lpc32xx_request *req;
179 local_irq_save(flags);
182 seq_printf(s, "%12s, maxpacket %4d %3s",
183 ep->ep.name, ep->ep.maxpacket,
184 ep->is_in ? "in" : "out");
185 seq_printf(s, " type %4s", epnames[ep->eptype]);
186 seq_printf(s, " ints: %12d", ep->totalints);
187 seq_printf(s, " stalls: %12d", ep->totalstalls);
188 seq_printf(s, " NAKs: %12d\n", ep->totalnaks);
190 if (list_empty (&ep->queue))
191 seq_printf(s, "\t(queue empty)\n");
193 list_for_each_entry (req, &ep->queue, queue) {
194 u32 length = req->req.actual;
196 seq_printf(s, "\treq %p len %d/%d buf %p\n",
198 req->req.length, req->req.buf);
202 local_irq_restore(flags);
205 static int proc_udc_show(struct seq_file *s, void *unused)
207 struct lpc32xx_udc *udc = s->private;
208 struct lpc32xx_ep *ep;
210 seq_printf(s, "%s: version %s\n", driver_name, DRIVER_VERSION);
212 seq_printf(s, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
213 udc->vbus ? "present" : "off",
215 ? (udc->vbus ? "active" : "enabled")
217 udc->selfpowered ? "self" : "VBUS",
218 udc->suspended ? ", suspended" : "",
219 udc->driver ? udc->driver->driver.name : "(none)");
221 if (udc->enabled && udc->vbus) {
222 proc_ep_show(s, &udc->ep[0]);
223 list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) {
233 static int proc_udc_open(struct inode *inode, struct file *file)
235 return single_open(file, proc_udc_show, PDE(inode)->data);
238 static const struct file_operations proc_ops = {
239 .owner = THIS_MODULE,
240 .open = proc_udc_open,
243 .release = single_release,
246 static void create_debug_file(struct lpc32xx_udc *udc)
248 udc->pde = proc_create_data(debug_filename, 0, NULL, &proc_ops, udc);
251 static void remove_debug_file(struct lpc32xx_udc *udc)
254 remove_proc_entry(debug_filename, NULL);
258 static inline void create_debug_file(struct lpc32xx_udc *udc) {}
259 static inline void remove_debug_file(struct lpc32xx_udc *udc) {}
264 * ISP1301 transceiver support functions
267 struct i2c_driver isp1301_driver;
268 struct i2c_client *isp1301_i2c_client;
269 static int isp1301_probe(struct i2c_client *client, const struct i2c_device_id *i2c_id);
270 static int isp1301_remove(struct i2c_client *client);
272 static const unsigned short normal_i2c[] =
273 { ISP1301_I2C_ADDR, I2C_CLIENT_END };
275 static const struct i2c_device_id isp1301_id[] = {
276 { "isp1301_pnx", 0 },
280 struct i2c_driver isp1301_driver = {
282 .name = "isp1301_pnx",
284 .probe = isp1301_probe,
285 .remove = isp1301_remove,
286 .id_table = isp1301_id,
289 static int isp1301_probe(struct i2c_client *client, const struct i2c_device_id *i2c_id)
294 static int isp1301_remove(struct i2c_client *client)
299 static void i2c_write(u8 buf, u8 subaddr)
303 tmpbuf[0] = subaddr; /*register number */
304 tmpbuf[1] = buf; /*register data */
305 i2c_master_send(isp1301_i2c_client, &tmpbuf[0], 2);
308 static u16 i2c_read(u8 subaddr)
312 i2c_master_send(isp1301_i2c_client, &subaddr, 1);
313 i2c_master_recv(isp1301_i2c_client, (u8 *) &data, 1);
318 static u16 i2c_read16(u8 subaddr)
322 i2c_master_send(isp1301_i2c_client, &subaddr, 1);
323 i2c_master_recv(isp1301_i2c_client, (u8 *) &data, 2);
328 /* Primary initializion sequence for the ISP1301 transceiver */
329 static void isp1301_udc_configure(struct lpc32xx_udc *udc)
331 /* LPC32XX only supports DAT_SE0 USB mode */
332 /* This sequence is important */
334 /* Disable transparent UART mode first */
335 i2c_write(MC1_UART_EN, (ISP1301_I2C_MODE_CONTROL_1 |
336 ISP1301_I2C_REG_CLEAR_ADDR));
338 /* Set full speed and SE0 mode */
339 i2c_write(~0, (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR));
340 i2c_write((MC1_SPEED_REG | MC1_DAT_SE0), ISP1301_I2C_MODE_CONTROL_1);
342 /* The PSW_OE enable bit state is reversed in the ISP1301 User's guide! */
343 i2c_write(~0, (ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR));
344 i2c_write((MC2_BI_DI | MC2_SPD_SUSP_CTRL), ISP1301_I2C_MODE_CONTROL_2);
346 /* Driver VBUS_DRV high or low depending on board setup */
347 if (udc->board->vbus_drv_pol != 0) {
348 i2c_write(OTG1_VBUS_DRV, ISP1301_I2C_OTG_CONTROL_1);
351 i2c_write(OTG1_VBUS_DRV, (ISP1301_I2C_OTG_CONTROL_1 |
352 ISP1301_I2C_REG_CLEAR_ADDR));
355 /* Bi-derctional mode with suspend control */
356 /* Enable both pulldowns for now - the pullup will be enable when VBUS is detected */
357 i2c_write(~0, (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR));
358 i2c_write((0 | OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN),
359 ISP1301_I2C_OTG_CONTROL_1);
361 /* Discharge VBUS (just in case) */
362 i2c_write(OTG1_VBUS_DISCHRG, ISP1301_I2C_OTG_CONTROL_1);
364 i2c_write(OTG1_VBUS_DISCHRG,
365 (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR));
367 /* Clear and enable VBUS high edge interrupt */
368 i2c_write(~0, ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR);
369 i2c_write(~0, ISP1301_I2C_INTERRUPT_FALLING | ISP1301_I2C_REG_CLEAR_ADDR);
370 i2c_write(INT_VBUS_VLD, ISP1301_I2C_INTERRUPT_FALLING);
371 i2c_write(~0, ISP1301_I2C_INTERRUPT_RISING | ISP1301_I2C_REG_CLEAR_ADDR);
372 i2c_write(INT_VBUS_VLD, ISP1301_I2C_INTERRUPT_RISING);
374 /* Enable usb_need_clk clock after transceiver is initialized */
375 __raw_writel((__raw_readl(USB_CTRL) | (1 << 22)), USB_CTRL);
377 dev_dbg(udc->dev, "ISP1301 Vendor ID : 0x%04x\n", i2c_read16(0x00));
378 dev_dbg(udc->dev, "ISP1301 Product ID : 0x%04x\n", i2c_read16(0x02));
379 dev_dbg(udc->dev, "ISP1301 Version ID : 0x%04x\n", i2c_read16(0x14));
382 /* Enables or disables the USB device pullup via the ISP1301 transceiver */
383 static void isp1301_pullup_enable(int en_pullup)
386 /* Enable pullup for bus signalling */
387 i2c_write(OTG1_DP_PULLUP, ISP1301_I2C_OTG_CONTROL_1);
390 /* Enable pullup for bus signalling */
391 i2c_write(OTG1_DP_PULLUP,
392 (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR));
397 /* Powers up or down the ISP1301 transceiver */
398 static void isp1301_set_powerstate(int enable) {
400 /* Power up ISP1301 - this ISP1301 will automatically wakeup
401 when VBUS is detected */
402 i2c_write(MC2_GLOBAL_PWR_DN,
403 (ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR));
406 /* Power down ISP1301 */
407 i2c_write(MC2_GLOBAL_PWR_DN, ISP1301_I2C_MODE_CONTROL_2);
414 * USB protocol engine command/data read/write helper functions
417 /* Issues a single command to the USB device state machine */
418 static void udc_protocol_cmd_w(struct lpc32xx_udc *udc, u32 cmd) {
422 /* EP may lock on CLRI if this read isn't done */
423 volatile u32 tmp = __raw_readl(USBD_DEVINTST(udc->udp_baseaddr));
427 __raw_writel(USBD_CCEMPTY, USBD_DEVINTCLR(udc->udp_baseaddr));
429 /* Write command code */
430 __raw_writel(cmd, USBD_CMDCODE(udc->udp_baseaddr));
432 while (((__raw_readl(USBD_DEVINTST(udc->udp_baseaddr)) &
433 USBD_CCEMPTY) == 0) && (to > 0)) {
437 if (to > 0) pass = 1;
441 /* Issues 2 commands (or command and data) to the USB device state machine */
442 static inline void udc_protocol_cmd_data_w(struct lpc32xx_udc *udc, u32 cmd, u32 data) {
443 udc_protocol_cmd_w(udc, cmd);
444 udc_protocol_cmd_w(udc, data);
447 /* Issues a single command to the USB device state machine and reads
449 static u32 udc_protocol_cmd_r(struct lpc32xx_udc *udc, u32 cmd) {
450 /* Write a command and read data from the protocol engine */
453 __raw_writel((USBD_CDFULL | USBD_CCEMPTY),
454 USBD_DEVINTCLR(udc->udp_baseaddr));
456 /* Write command code */
457 udc_protocol_cmd_w(udc, cmd);
458 while ((__raw_readl(USBD_DEVINTST(udc->udp_baseaddr)) &
461 tmp = __raw_readl(USBD_CMDDATA(udc->udp_baseaddr));
468 * USB device interrupt mask support functions
471 /* Enable one or more USB device interrupts */
472 static inline void uda_enable_devint(struct lpc32xx_udc *udc, u32 devmask) {
473 udc->enabled_devints |= devmask;
474 __raw_writel(udc->enabled_devints, USBD_DEVINTEN(udc->udp_baseaddr));
477 /* Disable one or more USB device interrupts */
478 static inline void uda_disable_devint(struct lpc32xx_udc *udc, u32 mask) {
479 udc->enabled_devints &= ~mask;
480 __raw_writel(udc->enabled_devints, USBD_DEVINTEN(udc->udp_baseaddr));
483 /* Clear one or more USB device interrupts */
484 static inline void uda_clear_devint(struct lpc32xx_udc *udc, u32 mask) {
485 __raw_writel(mask, USBD_DEVINTCLR(udc->udp_baseaddr));
490 * Endpoint interrupt disable/enable functions
493 /* Enable one or more USB endpoint interrupts */
494 static void uda_enable_hwepint(struct lpc32xx_udc *udc, u32 hwep) {
495 udc->enabled_hwepints |= (1 << hwep);
496 __raw_writel(udc->enabled_hwepints, USBD_EPINTEN(udc->udp_baseaddr));
499 /* Disable one or more USB endpoint interrupts */
500 static void uda_disable_hwepint(struct lpc32xx_udc *udc, u32 hwep) {
501 udc->enabled_hwepints &= ~(1 << hwep);
502 __raw_writel(udc->enabled_hwepints, USBD_EPINTEN(udc->udp_baseaddr));
505 /* Clear one or more USB endpoint interrupts */
506 static inline void uda_clear_hwepint(struct lpc32xx_udc *udc, u32 hwep) {
507 __raw_writel((1 << hwep), USBD_EPINTCLR(udc->udp_baseaddr));
510 /* Enable DMA for the HW channel */
511 static inline void udc_ep_dma_enable(struct lpc32xx_udc *udc, u32 hwep) {
512 __raw_writel((1 << hwep), USBD_EPDMAEN(udc->udp_baseaddr));
515 /* Disable DMA for the HW channel */
516 static inline void udc_ep_dma_disable(struct lpc32xx_udc *udc, u32 hwep) {
517 __raw_writel((1 << hwep), USBD_EPDMADIS(udc->udp_baseaddr));
522 * Endpoint realize/unrealize functions
525 /* Before an endpoint can be used, it needs to be realized
526 in the USB protocol engine - this realizes the endpoint.
527 The interrupt (FIFO or DMA) is not enabled with this function */
528 static void udc_realize_hwep(struct lpc32xx_udc *udc, u32 hwep,
530 __raw_writel(USBD_EP_RLZED, USBD_DEVINTCLR(udc->udp_baseaddr));
531 __raw_writel(hwep, USBD_EPIND(udc->udp_baseaddr));
532 udc->realized_eps |= (1 << hwep);
533 __raw_writel(udc->realized_eps, USBD_REEP(udc->udp_baseaddr));
534 __raw_writel(maxpacket, USBD_EPMAXPSIZE(udc->udp_baseaddr));
536 /* Wait until endpoint is realized in hardware */
537 while (!(__raw_readl(USBD_DEVINTST(udc->udp_baseaddr)) & USBD_EP_RLZED));
538 __raw_writel(USBD_EP_RLZED, USBD_DEVINTCLR(udc->udp_baseaddr));
541 /* Unrealize an EP */
542 static void udc_unrealize_hwep(struct lpc32xx_udc *udc, u32 hwep) {
543 udc->realized_eps &= ~(1 << hwep);
544 __raw_writel(udc->realized_eps, USBD_REEP(udc->udp_baseaddr));
549 * Endpoint support functions
552 /* Select and clear endpoint interrupt */
553 static u32 udc_selep_clrint(struct lpc32xx_udc *udc, u32 hwep) {
554 udc_protocol_cmd_w(udc, CMD_SEL_EP_CLRI(hwep));
555 return udc_protocol_cmd_r(udc, DAT_SEL_EP_CLRI(hwep));
558 /* Disables the endpoint in the USB protocol engine */
559 static void udc_disable_hwep(struct lpc32xx_udc *udc, u32 hwep) {
560 udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(hwep),
561 DAT_WR_BYTE(EP_STAT_DA));
564 /* Stalls the endpoint - endpoint will return STALL */
565 static void udc_stall_hwep(struct lpc32xx_udc *udc, u32 hwep) {
566 udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(hwep),
567 DAT_WR_BYTE(EP_STAT_ST));
570 /* Clear stall or reset endpoint */
571 static void udc_clrstall_hwep(struct lpc32xx_udc *udc, u32 hwep) {
572 udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(hwep),
576 /* Select an endpoint for endpoint status, clear, validate */
577 static void udc_select_hwep(struct lpc32xx_udc *udc, u32 hwep) {
578 udc_protocol_cmd_w(udc, CMD_SEL_EP(hwep));
583 * Endpoint buffer management functions
586 /* Clear the current endpoint's buffer */
587 static void udc_clr_buffer_hwep(struct lpc32xx_udc *udc, u32 hwep) {
588 udc_select_hwep(udc, hwep);
589 udc_protocol_cmd_w(udc, CMD_CLR_BUF);
592 /* Validate the current endpoint's buffer */
593 static void udc_val_buffer_hwep(struct lpc32xx_udc *udc, u32 hwep) {
594 udc_select_hwep(udc, hwep);
595 udc_protocol_cmd_w(udc, CMD_VALID_BUF);
598 static inline u32 udc_clearep_getsts(struct lpc32xx_udc *udc, u32 hwep) {
599 /* Clear EP interrupt */
600 uda_clear_hwepint(udc, hwep);
601 return udc_selep_clrint(udc, hwep);
604 #if defined(UDC_ENABLE_DMA)
610 /* Allocate a DMA Descriptor */
611 static struct lpc32xx_usbd_dd_gad *udc_dd_alloc(struct lpc32xx_udc *udc) {
613 struct lpc32xx_usbd_dd_gad *dd;
615 dd = (struct lpc32xx_usbd_dd_gad *) dma_pool_alloc(
616 udc->dd_cache, (GFP_KERNEL | GFP_DMA), &dma);
624 /* Free a DMA Descriptor */
625 static void udc_dd_free(struct lpc32xx_udc *udc, struct lpc32xx_usbd_dd_gad *dd)
627 dma_pool_free(udc->dd_cache, dd, dd->this_dma);
633 * USB setup and shutdown functions
636 /* Enables or disables most of the USB system clocks when low power mode is
637 needed. Clocks are typically started on a connection event, and disabled
638 when a cable is disconnected */
639 #define OTGOFF_CLK_MASK (AHB_M_CLOCK_ON | I2C_CLOCK_ON)
640 static void udc_clk_set(struct lpc32xx_udc *udc, int enable)
649 clk_enable(udc->usb_pll_clk);
651 /* Enable the USb device clock */
652 __raw_writel(__raw_readl(USB_CTRL) | USB_DEV_NEED_CLK_EN, USB_CTRL);
654 /* Set to enable all needed USB OTG clocks */
655 __raw_writel(USB_CLOCK_MASK, USB_OTG_CLK_CTRL);
657 while ((__raw_readl(USB_OTG_CLK_STAT) & USB_CLOCK_MASK) !=
665 udc->gadget.speed = USB_SPEED_UNKNOWN;
667 /* Never disable the USB_HCLK during normal operation */
670 clk_disable(udc->usb_pll_clk);
672 /* Enable the USb device clock */
673 __raw_writel(__raw_readl(USB_CTRL) & ~USB_DEV_NEED_CLK_EN, USB_CTRL);
675 /* Set to enable all needed USB OTG clocks */
676 __raw_writel(OTGOFF_CLK_MASK, USB_OTG_CLK_CTRL);
678 while ((__raw_readl(USB_OTG_CLK_STAT) & OTGOFF_CLK_MASK) !=
683 static void udc_disable(struct lpc32xx_udc *udc) {
687 udc_protocol_cmd_data_w(udc, CMD_CFG_DEV, DAT_WR_BYTE(0));
688 udc_protocol_cmd_data_w(udc, CMD_SET_DEV_STAT, DAT_WR_BYTE(0));
690 /* Disable all device interrupts (including EP0) */
691 uda_disable_devint(udc, 0x3FF);
693 /* Disable and reset all endpoint interrupts */
694 for (i = 0; i < 32; i++) {
695 uda_disable_hwepint(udc, i);
696 uda_clear_hwepint(udc, i);
697 udc_disable_hwep(udc, i);
698 udc_unrealize_hwep(udc, i);
699 udc->udca_v_base [i] = 0;
701 /* Disable and clear all interrupts and DMA */
702 udc_ep_dma_disable(udc, i);
703 __raw_writel((1 << i), USBD_EOTINTCLR(udc->udp_baseaddr));
704 __raw_writel((1 << i), USBD_NDDRTINTCLR(udc->udp_baseaddr));
705 __raw_writel((1 << i), USBD_SYSERRTINTCLR(udc->udp_baseaddr));
706 __raw_writel((1 << i), USBD_DMARCLR(udc->udp_baseaddr));
709 /* Disable DMA interrupts */
710 __raw_writel(0, USBD_DMAINTEN(udc->udp_baseaddr));
712 __raw_writel(0, USBD_UDCAH(udc->udp_baseaddr));
715 static void udc_enable(struct lpc32xx_udc *udc)
718 struct lpc32xx_ep *ep = &udc->ep[0];
720 udc->gadget.speed = USB_SPEED_UNKNOWN;
722 /* Start with known state */
726 udc_protocol_cmd_data_w(udc, CMD_SET_DEV_STAT, DAT_WR_BYTE(DEV_CON));
728 /* EP interrupts on high priority, FRAME interrupt on low priority */
729 __raw_writel(USBD_EP_FAST, USBD_DEVINTPRI(udc->udp_baseaddr));
730 __raw_writel(0xFFFF, USBD_EPINTPRI(udc->udp_baseaddr));
732 /* Clear any pending device interrupts */
733 __raw_writel(0x3FF, USBD_DEVINTCLR(udc->udp_baseaddr));
735 /* Setup UDCA - not yet used (DMA) */
736 __raw_writel((u32) udc->udca_p_base, USBD_UDCAH(udc->udp_baseaddr));
738 /* Only enable EP0 in and out for now, EP0 only works in FIFO mode */
739 for (i = 0; i <= 1; i++) {
740 udc_realize_hwep(udc, i, ep->ep.maxpacket);
741 uda_enable_hwepint(udc, i);
742 udc_select_hwep(udc, i);
743 udc_clrstall_hwep(udc, i);
744 udc_clr_buffer_hwep(udc, i);
747 /* Device interrupt setup */
748 uda_clear_devint(udc, (USBD_ERR_INT | USBD_DEV_STAT | USBD_EP_SLOW |
750 uda_enable_devint(udc, (USBD_ERR_INT | USBD_DEV_STAT | USBD_EP_SLOW |
753 /* Set device address to 0 - called twice to force a latch in the USB
754 engine without the need of a setup packet status closure */
755 udc_set_address(udc, 0);
756 udc_set_address(udc, 0);
758 #if defined(UDC_ENABLE_DMA)
759 /* Enable master DMA interrupts */
760 __raw_writel((USBD_SYS_ERR_INT | USBD_EOT_INT), USBD_DMAINTEN(udc->udp_baseaddr));
768 * USB device board specific events handled via callbacks
771 /* Connection change event - notify board function of change */
772 static void uda_power_event(struct lpc32xx_udc *udc, u32 conn) {
773 /* Just notify of a connection change event (optional) */
774 if (udc->board->conn_chgb != NULL) {
775 udc->board->conn_chgb(conn);
779 /* Suspend/resume event - notify board function of change */
780 static void uda_resm_susp_event(struct lpc32xx_udc *udc, u32 conn) {
781 /* Just notify of a Suspend/resume change event (optional) */
782 if (udc->board->susp_chgb != NULL) {
783 udc->board->susp_chgb(conn);
792 /* Remote wakeup enable/disable - notify board function of change */
793 static void uda_remwkp_cgh(struct lpc32xx_udc *udc) {
794 if (udc->board->rmwk_chgb != NULL) {
795 udc->board->rmwk_chgb(udc->dev_status &
796 (1 << USB_DEVICE_REMOTE_WAKEUP));
800 /* Reads data from FIFO, adjusts for alignment and data size */
801 static void udc_pop_fifo(struct lpc32xx_udc *udc, u8 *data, u32 bytes) {
804 u32 *p32, tmp, cbytes;
806 /* Use optimal data transfer method based on source address and size */
807 switch (((u32) data) & 0x3) {
808 case 0: /* 32-bit aligned */
810 cbytes = (bytes & ~0x3);
812 /* Copy 32-bit aligned data first */
813 for (n = 0; n < cbytes; n += 4)
814 *p32++ = __raw_readl(USBD_RXDATA(udc->udp_baseaddr));
816 /* Handle any remaining bytes */
819 tmp = __raw_readl(USBD_RXDATA(udc->udp_baseaddr));
820 for (n = 0; n < bl; n++)
821 data[cbytes + n] = ((tmp >> (n * 8)) & 0xFF);
826 case 1: /* 8-bit aligned */
828 /* Each byte has to be handled independently */
829 for (n = 0; n < bytes; n += 4) {
830 tmp = __raw_readl(USBD_RXDATA(udc->udp_baseaddr));
836 for (i = 0; i < bl; i++)
837 data[n + i] = (u8) ((tmp >> (n * 8)) & 0xFF);
841 case 2: /* 16-bit aligned */
843 cbytes = (bytes & ~0x3);
845 /* Copy 32-bit sized objects first with 16-bit alignment */
846 for (n = 0; n < cbytes; n += 4) {
847 tmp = __raw_readl(USBD_RXDATA(udc->udp_baseaddr));
848 *p16++ = (u16) (tmp & 0xFFFF);
849 *p16++ = (u16) ((tmp >> 16) & 0xFFFF);
852 /* Handle any remaining bytes */
855 tmp = __raw_readl(USBD_RXDATA(udc->udp_baseaddr));
856 for (n = 0; n < bl; n++)
857 data[cbytes + n] = ((tmp >> (n * 8)) & 0xFF);
863 /* Read data from the FIFO for an endpoint. This function is for endpoints (such
864 as EP0) that don't use DMA. This function should only be called if a packet
865 is known to be ready to read for the endpopint. Note that the endpoint must
866 be selected in the protocol engine prior to this call. */
867 static u32 udc_read_hwep(struct lpc32xx_udc *udc, u32 hwep, u32 *data,
870 u32 tmp, hwrep = ((hwep & 0x1E) << 1) | CTRL_RD_EN;
872 /* Setup read of endpoint */
873 __raw_writel(hwrep, USBD_CTRL(udc->udp_baseaddr));
874 __raw_writel(hwrep, USBD_CTRL(udc->udp_baseaddr));
876 while (__raw_readl(USBD_CTRL(udc->udp_baseaddr)) != hwrep) {
877 __raw_writel(hwrep, USBD_CTRL(udc->udp_baseaddr));
880 /* Wait until packet is ready */
882 while ((tmpv & PKT_RDY) == 0) {
883 tmpv = __raw_readl(USBD_RXPLEN(udc->udp_baseaddr));
887 tmp = tmpv & PKT_LNGTH_MASK;
892 if ((tmp > 0) && (data != NULL)) {
893 udc_pop_fifo(udc, (u8 *) data, tmp);
896 __raw_writel(((hwep & 0x1E) << 1), USBD_CTRL(udc->udp_baseaddr));
897 __raw_writel(((hwep & 0x1E) << 1), USBD_CTRL(udc->udp_baseaddr));
899 /* Clear the buffer */
900 udc_clr_buffer_hwep(udc, hwep);
905 /* Stuffs data into the FIFO, adjusts for alignment and data size */
906 static void udc_stuff_fifo(struct lpc32xx_udc *udc, u8 *data, u32 bytes) {
909 u32 *p32, tmp, cbytes;
911 /* Use optimal data transfer method based on source address and size */
912 switch (((u32) data) & 0x3) {
913 case 0: /* 32-bit aligned */
915 cbytes = (bytes & ~0x3);
917 /* Copy 32-bit aligned data first */
918 for (n = 0; n < cbytes; n += 4)
919 __raw_writel(*p32++, USBD_TXDATA(udc->udp_baseaddr));
921 /* Handle any remaining bytes */
925 for (n = 0; n < bl; n++)
926 tmp |= (u32) (data[cbytes + n] << (n * 8));
928 __raw_writel(tmp, USBD_TXDATA(udc->udp_baseaddr));
932 case 1: /* 8-bit aligned */
934 /* Each byte has to be handled independently */
935 for (n = 0; n < bytes; n += 4) {
941 for (i = 0; i < bl; i++)
942 tmp |= (u32) (data[n + i] << (i * 8));
944 __raw_writel(tmp, USBD_TXDATA(udc->udp_baseaddr));
948 case 2: /* 16-bit aligned */
950 cbytes = (bytes & ~0x3);
952 /* Copy 32-bit aligned data first */
953 for (n = 0; n < cbytes; n += 4) {
954 tmp = (u32) (*p16++ & 0xFFFF);
955 tmp |= (u32) ((*p16++ & 0xFFFF) << 16);
956 __raw_writel(tmp, USBD_TXDATA(udc->udp_baseaddr));
959 /* Handle any remaining bytes */
963 for (n = 0; n < bl; n++)
964 tmp |= (u32) (data[cbytes + n] << (n * 8));
966 __raw_writel(tmp, USBD_TXDATA(udc->udp_baseaddr));
972 /* Write data to the FIFO for an endpoint. This function is for endpoints (such
973 as EP0) that don't use DMA. Note that the endpoint must be selected in the
974 protocol engine prior to this call. */
975 static void udc_write_hwep(struct lpc32xx_udc *udc, u32 hwep,
976 u32 *data, u32 bytes) {
977 u32 hwwep = ((hwep & 0x1E) << 1) | CTRL_WR_EN;
979 if ((bytes > 0) && (data == NULL)) {
983 /* Setup write of endpoint */
984 __raw_writel(hwwep, USBD_CTRL(udc->udp_baseaddr));
985 __raw_writel(hwwep, USBD_CTRL(udc->udp_baseaddr));
986 while (__raw_readl(USBD_CTRL(udc->udp_baseaddr)) != hwwep) {
987 __raw_writel(hwwep, USBD_CTRL(udc->udp_baseaddr));
990 __raw_writel(bytes, USBD_TXPLEN(udc->udp_baseaddr));
992 /* Need at least 1 byte to rgigger TX, may not be needed */
994 __raw_writel(0, USBD_TXDATA(udc->udp_baseaddr));
997 udc_stuff_fifo(udc, (u8 *) data, bytes);
1000 __raw_writel(((hwep & 0x1E) << 1), USBD_CTRL(udc->udp_baseaddr));
1001 __raw_writel(((hwep & 0x1E) << 1), USBD_CTRL(udc->udp_baseaddr));
1003 udc_val_buffer_hwep(udc, hwep);
1008 * USB protocol high level support functions
1011 /* Set/reset USB device address */
1012 static void udc_set_address(struct lpc32xx_udc *udc, u32 addr) {
1013 /* Address will be latched at the end of the status phase, or
1014 latched immediately if function is called twice */
1015 udc_protocol_cmd_data_w(udc, CMD_SET_ADDR,
1016 DAT_WR_BYTE(DEV_EN | addr));
1019 /* USB device reset - resets USB to a default state with just EP0
1021 static void uda_usb_reset(struct lpc32xx_udc *udc) {
1022 /* Re-init device controller and EP0 */
1024 udc->gadget.speed = USB_SPEED_FULL;
1027 /* Send a ZLP on EP0 */
1028 static void udc_ep0_send_zlp(struct lpc32xx_udc *udc) {
1029 udc_write_hwep(udc, EP_IN, NULL, 0);
1032 /* Get current frame number */
1033 static u16 udc_get_current_frame(struct lpc32xx_udc *udc) {
1036 udc_protocol_cmd_w(udc, CMD_RD_FRAME);
1037 flo = (u16) udc_protocol_cmd_r(udc, DAT_RD_FRAME);
1038 fhi = (u16) udc_protocol_cmd_r(udc, DAT_RD_FRAME);
1040 return (fhi << 8) | flo;
1043 /* Set the device as configured - enables all endpoints */
1044 static inline void udc_set_device_configured(struct lpc32xx_udc *udc) {
1045 udc_protocol_cmd_data_w(udc, CMD_CFG_DEV, DAT_WR_BYTE(CONF_DVICE));
1048 /* Set the device as unconfigured - disables all endpoints */
1049 static inline void udc_set_device_unconfigured(struct lpc32xx_udc *udc) {
1050 udc_protocol_cmd_data_w(udc, CMD_CFG_DEV, DAT_WR_BYTE(0));
1053 /* reinit == restore inital software state */
1054 static void udc_reinit(struct lpc32xx_udc *udc)
1058 INIT_LIST_HEAD(&udc->gadget.ep_list);
1059 INIT_LIST_HEAD(&udc->gadget.ep0->ep_list);
1061 for (i = 0; i < NUM_ENDPOINTS; i++) {
1062 struct lpc32xx_ep *ep = &udc->ep[i];
1065 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
1068 ep->ep.maxpacket = ep->maxpacket;
1069 INIT_LIST_HEAD(&ep->queue);
1070 ep->req_pending = 0;
1073 udc->ep0state = WAIT_FOR_SETUP;
1076 static void done(struct lpc32xx_ep *ep, struct lpc32xx_request *req, int status)
1078 struct lpc32xx_udc *udc = ep->udc;
1080 list_del_init(&req->queue);
1081 if (req->req.status == -EINPROGRESS) {
1082 req->req.status = status;
1085 status = req->req.status;
1088 #if defined(UDC_ENABLE_DMA)
1090 enum dma_data_direction direction;
1093 direction = DMA_TO_DEVICE;
1095 direction = DMA_FROM_DEVICE;
1098 dma_unmap_single(ep->udc->gadget.dev.parent,
1099 req->req.dma, req->req.length, direction);
1104 dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
1105 req->req.dma, req->req.length, direction);
1109 udc_dd_free(udc, req->dd_desc_ptr);
1113 if (status && status != -ESHUTDOWN) {
1114 ep_dbg(ep, "%s done %p, status %d\n", ep->ep.name, req, status);
1117 spin_unlock(&udc->lock);
1118 req->req.complete(&ep->ep, &req->req);
1119 spin_lock(&udc->lock);
1123 static void nuke(struct lpc32xx_ep *ep, int status)
1125 struct lpc32xx_request *req;
1127 while (!list_empty(&ep->queue)) {
1128 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
1129 done(ep, req, status);
1133 if (status == -ESHUTDOWN) {
1134 uda_disable_hwepint(ep->udc, ep->hwep_num);
1135 udc_disable_hwep(ep->udc, ep->hwep_num);
1140 static void stop_activity(struct lpc32xx_udc *udc)
1142 struct usb_gadget_driver *driver = udc->driver;
1145 if (udc->gadget.speed == USB_SPEED_UNKNOWN)
1148 udc->gadget.speed = USB_SPEED_UNKNOWN;
1151 for (i = 0; i < NUM_ENDPOINTS; i++) {
1152 struct lpc32xx_ep *ep = &udc->ep[i];
1153 nuke(ep, -ESHUTDOWN);
1156 driver->disconnect(&udc->gadget);
1163 * Activate or kill host pullup
1165 static void pullup(struct lpc32xx_udc *udc, int is_on)
1170 if (!udc->enabled || !udc->vbus)
1174 udc_clk_set(udc, 1);
1175 isp1301_pullup_enable(1);
1178 isp1301_pullup_enable(0);
1179 udc_clk_set(udc, 0);
1183 static int lpc32xx_ep_disable (struct usb_ep * _ep)
1185 struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
1186 struct lpc32xx_udc *udc = ep->udc;
1187 unsigned long flags;
1189 if ((ep->hwep_num_base == 0) || (ep->hwep_num == 0)) {
1193 local_irq_save(flags);
1195 nuke(ep, -ESHUTDOWN);
1197 /* restore the endpoint's pristine config */
1200 /* Clear all DMA statuses for this EP */
1201 udc_ep_dma_disable(udc, ep->hwep_num);
1202 __raw_writel((1 << ep->hwep_num), USBD_EOTINTCLR(udc->udp_baseaddr));
1203 __raw_writel((1 << ep->hwep_num), USBD_NDDRTINTCLR(udc->udp_baseaddr));
1204 __raw_writel((1 << ep->hwep_num), USBD_SYSERRTINTCLR(udc->udp_baseaddr));
1205 __raw_writel((1 << ep->hwep_num), USBD_DMARCLR(udc->udp_baseaddr));
1207 /* Remove the DD pointer in the UDCA */
1208 udc->udca_v_base[ep->hwep_num] = 0;
1211 /* Disable and reset endpoint and interrupt */
1212 uda_clear_hwepint(udc, ep->hwep_num);
1213 udc_unrealize_hwep(udc, ep->hwep_num);
1217 local_irq_restore(flags);
1222 static int lpc32xx_ep_enable(struct usb_ep *_ep,
1223 const struct usb_endpoint_descriptor *desc)
1225 struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
1226 struct lpc32xx_udc *udc = ep->udc;
1229 unsigned long flags;
1231 /* Verify EP data */
1232 if ((!_ep) || (!ep) || (!desc) || (ep->desc) ||
1233 (desc->bDescriptorType != USB_DT_ENDPOINT) ||
1234 ((maxpacket = le16_to_cpu(desc->wMaxPacketSize)) == 0) ||
1235 (maxpacket > ep->maxpacket)) {
1236 dev_dbg(udc->dev, "bad ep or descriptor\n");
1240 /* Don't touch EP0 */
1241 if (ep->hwep_num_base == 0) {
1242 dev_dbg(udc->dev, "Can't re-enable EP0!!!\n");
1246 /* Is driver ready? */
1247 if ((!udc->driver) || (udc->gadget.speed == USB_SPEED_UNKNOWN)) {
1248 dev_dbg(udc->dev, "bogus device state\n");
1252 tmp = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
1254 case USB_ENDPOINT_XFER_CONTROL:
1257 case USB_ENDPOINT_XFER_INT:
1258 if (maxpacket > ep->maxpacket) {
1259 dev_dbg(udc->dev, "Bad INT endpoint maxpacket %d\n", maxpacket);
1264 case USB_ENDPOINT_XFER_BULK:
1265 switch (maxpacket) {
1273 dev_dbg(udc->dev, "Bad BULK endpoint maxpacket %d\n", maxpacket);
1278 case USB_ENDPOINT_XFER_ISOC:
1282 local_irq_save(flags);
1284 /* Initialize endpoint to match the selected descriptor */
1285 ep->is_in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
1287 ep->ep.maxpacket = maxpacket;
1289 /* Map hardware endpoint from base and direction */
1291 /* IN endpoints are offset 1 from the OUT endpoint */
1292 ep->hwep_num = ep->hwep_num_base + EP_IN;
1295 ep->hwep_num = ep->hwep_num_base;
1298 ep_dbg(ep, "EP enabled: %s, HW:%d, MP:%d IN:%d\n", ep->ep.name, ep->hwep_num,
1299 maxpacket, (ep->is_in == 1));
1301 /* Realize the endpoint, interrupt is enabled later when
1302 buffers are queued, IN EPs will NAK until buffers are ready */
1303 udc_realize_hwep(udc, ep->hwep_num, ep->ep.maxpacket);
1304 udc_clr_buffer_hwep(udc, ep->hwep_num);
1305 uda_disable_hwepint(udc, ep->hwep_num);
1306 udc_clrstall_hwep(udc, ep->hwep_num);
1308 /* Clear all DMA statuses for this EP */
1309 udc_ep_dma_disable(udc, ep->hwep_num);
1310 __raw_writel((1 << ep->hwep_num), USBD_EOTINTCLR(udc->udp_baseaddr));
1311 __raw_writel((1 << ep->hwep_num), USBD_NDDRTINTCLR(udc->udp_baseaddr));
1312 __raw_writel((1 << ep->hwep_num), USBD_SYSERRTINTCLR(udc->udp_baseaddr));
1313 __raw_writel((1 << ep->hwep_num), USBD_DMARCLR(udc->udp_baseaddr));
1315 #if defined(UDC_ENABLE_DMA)
1319 local_irq_restore(flags);
1324 /* Allocate a USB request list */
1325 static struct usb_request *lpc32xx_ep_alloc_request(
1326 struct usb_ep *_ep, gfp_t gfp_flags)
1328 struct lpc32xx_request *req;
1330 req = kzalloc(sizeof (struct lpc32xx_request), gfp_flags);
1334 INIT_LIST_HEAD(&req->queue);
1338 /* De-allocate a USB request list */
1339 static void lpc32xx_ep_free_request(struct usb_ep *_ep,
1340 struct usb_request *_req)
1342 struct lpc32xx_request *req;
1344 req = container_of(_req, struct lpc32xx_request, req);
1345 BUG_ON(!list_empty(&req->queue));
1349 static int lpc32xx_ep_queue(struct usb_ep *_ep,
1350 struct usb_request *_req, gfp_t gfp_flags)
1352 struct lpc32xx_request *req;
1353 struct lpc32xx_ep *ep;
1354 struct lpc32xx_udc *udc;
1355 unsigned long flags;
1358 req = container_of(_req, struct lpc32xx_request, req);
1359 ep = container_of(_ep, struct lpc32xx_ep, ep);
1361 if (!_req || !_req->complete || !_req->buf || !list_empty(&req->queue)) {
1367 if (!_ep || (!ep->desc && ep->hwep_num_base != 0)) {
1368 dev_dbg(udc->dev, "invalid ep\n");
1373 if ((!udc) || (!udc->driver) || (udc->gadget.speed == USB_SPEED_UNKNOWN)) {
1374 dev_dbg(udc->dev, "invalid device\n");
1378 #if defined(UDC_ENABLE_DMA)
1380 enum dma_data_direction direction;
1381 struct lpc32xx_usbd_dd_gad *dd;
1383 /* Map DMA pointer */
1385 direction = DMA_TO_DEVICE;
1387 direction = DMA_FROM_DEVICE;
1389 if (req->req.dma == 0) {
1390 req->req.dma = dma_map_single(
1391 ep->udc->gadget.dev.parent,
1392 req->req.buf, req->req.length, direction);
1396 dma_sync_single_for_device(
1397 ep->udc->gadget.dev.parent,
1398 req->req.dma, req->req.length, direction);
1402 /* For the request, build a list of DDs */
1403 dd = udc_dd_alloc(udc);
1405 /* Error allocating DD */
1408 req->dd_desc_ptr = dd;
1410 /* Setup the DMA descriptor */
1411 dd->dd_next_phy = dd->dd_next_v = NULL;
1412 dd->dd_buffer_addr = (u32) req->req.dma;
1415 /* Special handling for ISO EPs */
1416 if (ep->eptype == EP_ISO_TYPE) {
1417 dd->dd_setup = DD_SETUP_ISO_EP |
1418 DD_SETUP_PACKETLEN(ep->ep.maxpacket) |
1419 DD_SETUP_DMALENBYTES(req->req.length);
1420 dd->dd_iso_ps_mem_addr = (u32 *) ((u32) dd->this_dma + 20);
1421 dd->iso_status[0] = req->req.length;
1424 dd->dd_setup = DD_SETUP_PACKETLEN(ep->ep.maxpacket) |
1425 DD_SETUP_DMALENBYTES(req->req.length);
1430 ep_dbg(ep, "%s queue req %p len %d buf %p (in=%d) z=%d\n", _ep->name, _req, _req->length,
1431 _req->buf, ep->is_in, _req->zero);
1433 spin_lock_irqsave(&udc->lock, flags);
1435 _req->status = -EINPROGRESS;
1437 req->send_zlp = _req->zero;
1439 /* Kickstart empty queues */
1440 if (list_empty(&ep->queue)) {
1441 list_add_tail(&req->queue, &ep->queue);
1443 if (ep->hwep_num_base == 0) {
1444 /* Handle expected data direction */
1446 /* IN packet to host */
1447 udc->ep0state = DATA_IN;
1448 status = udc_ep0_in_req(udc);
1451 /* OUT packet from host */
1452 udc->ep0state = DATA_OUT;
1453 status = udc_ep0_out_req(udc);
1456 else if (ep->is_in) {
1457 /* IN packet to host and kick off transfer */
1458 if (!ep->req_pending) {
1459 #if defined(UDC_ENABLE_DMA)
1460 udc_ep_in_req_dma(udc, ep);
1462 uda_enable_hwepint(udc, ep->hwep_num);
1463 udc_ep_in_req(udc, ep);
1468 /* OUT packet from host and kick off list */
1469 if (!ep->req_pending) {
1470 #if defined(UDC_ENABLE_DMA)
1471 udc_ep_out_req_dma(udc, ep);
1473 uda_enable_hwepint(udc, ep->hwep_num);
1474 udc_ep_out_req(udc, ep);
1480 list_add_tail(&req->queue, &ep->queue);
1482 spin_unlock_irqrestore(&udc->lock, flags);
1484 return (status < 0) ? status : 0;
1487 static int lpc32xx_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1489 struct lpc32xx_ep *ep;
1490 struct lpc32xx_request *req;
1491 unsigned long flags;
1493 ep = container_of(_ep, struct lpc32xx_ep, ep);
1494 if (!_ep || ep->hwep_num_base == 0)
1497 spin_lock_irqsave(&ep->udc->lock, flags);
1499 /* make sure it's actually queued on this endpoint */
1500 list_for_each_entry (req, &ep->queue, queue) {
1501 if (&req->req == _req)
1504 if (&req->req != _req) {
1505 spin_unlock_irqrestore(&ep->udc->lock, flags);
1509 done(ep, req, -ECONNRESET);
1510 spin_unlock_irqrestore(&ep->udc->lock, flags);
1515 static int lpc32xx_ep_set_halt(struct usb_ep *_ep, int value)
1517 struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
1518 struct lpc32xx_udc *udc = ep->udc;
1519 unsigned long flags;
1521 if ((!ep) || (ep->desc == NULL) || (ep->hwep_num <= 1))
1524 /* Don't halt an IN EP */
1528 spin_lock_irqsave(&udc->lock, flags);
1532 udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(ep->hwep_num),
1533 DAT_WR_BYTE(EP_STAT_ST));
1537 udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(ep->hwep_num),
1541 spin_unlock_irqrestore(&udc->lock, flags);
1546 static const struct usb_ep_ops lpc32xx_ep_ops = {
1547 .enable = lpc32xx_ep_enable,
1548 .disable = lpc32xx_ep_disable,
1549 .alloc_request = lpc32xx_ep_alloc_request,
1550 .free_request = lpc32xx_ep_free_request,
1551 .queue = lpc32xx_ep_queue,
1552 .dequeue = lpc32xx_ep_dequeue,
1553 .set_halt = lpc32xx_ep_set_halt,
1556 #if defined(UDC_ENABLE_DMA)
1557 /* Setup up a IN request for DMA transfer - this consists of determining the
1558 list of DMA addresses for the transfer, allocating DMA Descriptors,
1559 installing the DD into the UDCA, and then enabling the DMA for that EP */
1560 static int udc_ep_in_req_dma(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
1562 struct lpc32xx_request *req;
1563 u32 hwep = ep->hwep_num;
1566 ep->req_pending = 1;
1567 epstatus = udc_clearep_getsts(udc, ep->hwep_num);
1569 /* There will always be a request waiting here */
1570 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
1572 /* Place the DD Descriptor into the UDCA */
1573 udc->udca_v_base[hwep] = (u32) req->dd_desc_ptr->this_dma;
1575 /* Enable DMA and interrupt for the HW EP */
1576 udc_ep_dma_enable(udc, hwep);
1581 /* Setup up a OUT request for DMA transfer - this consists of determining the
1582 list of DMA addresses for the transfer, allocating DMA Descriptors,
1583 installing the DD into the UDCA, and then enabling the DMA for that EP */
1584 static int udc_ep_out_req_dma(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
1586 struct lpc32xx_request *req;
1587 u32 hwep = ep->hwep_num;
1589 ep->req_pending = 1;
1591 /* There will always be a request waiting here */
1592 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
1594 /* Place the DD Descriptor into the UDCA */
1595 udc->udca_v_base[hwep] = (u32) req->dd_desc_ptr->this_dma;
1597 /* Enable DMA and interrupt for the HW EP */
1598 udc_ep_dma_enable(udc, hwep);
1603 /* This function is only called for IN ZLP transfer completions */
1604 void udc_handle_eps(struct lpc32xx_udc *udc, u32 epints) {
1605 int hwep = -1, i = 0;
1607 struct lpc32xx_ep *ep;
1608 struct lpc32xx_request *req;
1609 const int ineps[] = {2, 5, 8, 11, 15, -1};
1611 /* Find the IN EP that generated the interrupt */
1612 while (ineps[i] != 0) {
1613 if (epints & (1 << ineps[i]))
1618 uda_disable_hwepint(udc, hwep);
1623 epstatus = udc_clearep_getsts(udc, hwep);
1625 ep = &udc->ep[hwep];
1626 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
1629 /* Start another request if ready */
1630 if (!list_entry(ep->queue.next, struct lpc32xx_request, queue)) {
1632 udc_ep_in_req_dma(udc, ep);
1634 udc_ep_out_req_dma(udc, ep);
1637 ep->req_pending = 0;
1640 /* Send a ZLP on a non-0 IN EP */
1641 void udc_send_in_zlp(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep,
1642 struct lpc32xx_usbd_dd_gad *dd) {
1643 /* Set up EP interrupt status */
1644 uda_enable_hwepint(udc, ep->hwep_num);
1645 udc_clearep_getsts(udc, ep->hwep_num);
1648 udc_write_hwep(udc, ep->hwep_num, NULL, 0);
1651 /* DMA end of transfer completion */
1652 static void udc_handle_dma_ep(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep) {
1653 u32 status, epstatus;
1654 struct lpc32xx_request *req;
1655 struct lpc32xx_usbd_dd_gad *dd;
1657 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1661 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
1663 ep_err(ep, "DMA interrupt on no req!\n");
1666 dd = req->dd_desc_ptr;
1668 /* Wait for end of descriptor to retire */
1669 while (!(dd->dd_status & DD_STATUS_DD_RETIRED));
1672 udc_ep_dma_disable(udc, ep->hwep_num);
1673 __raw_writel((1 << ep->hwep_num), USBD_EOTINTCLR(udc->udp_baseaddr));
1674 __raw_writel((1 << ep->hwep_num), USBD_NDDRTINTCLR(udc->udp_baseaddr));
1677 if (__raw_readl(USBD_SYSERRTINTST(udc->udp_baseaddr)) & (1 << ep->hwep_num)) {
1678 __raw_writel((1 << ep->hwep_num), USBD_SYSERRTINTCLR(udc->udp_baseaddr));
1679 ep_err(ep, "AHB critical error!\n");
1680 ep->req_pending = 0;
1682 /* The error could of occurred on a packet of a multipacket transfer,
1683 so recovering the transfer is not possible. Close the request with
1685 done(ep, req, -ECONNABORTED);
1690 /* Handle the current DD's status */
1691 status = dd->dd_status;
1692 switch (status & DD_STATUS_STS_MASK) {
1693 case DD_STATUS_STS_NS:
1694 /* DD not serviced? This shouldn't happen! */
1695 ep->req_pending = 0;
1696 ep_err(ep, "DMA critical EP error: DD not serviced (0x%x)!\n", status);
1697 done(ep, req, -ECONNABORTED);
1700 case DD_STATUS_STS_BS:
1701 /* Interrupt only fires on EOT - This shouldn't happen! */
1702 ep->req_pending = 0;
1703 ep_err(ep, "DMA critical EP error: EOT prior to service completion (0x%x)!\n", status);
1704 done(ep, req, -ECONNABORTED);
1707 case DD_STATUS_STS_NC:
1708 case DD_STATUS_STS_DUR: /* Really just a short packet, not an underrun */
1709 /* This is a good status and what we expect */
1713 /* Data overrun, system error, or unknown */
1714 ep->req_pending = 0;
1715 ep_err(ep, "DMA critical EP error: System error (0x%x)!\n", status);
1716 done(ep, req, -ECONNABORTED);
1720 /* Save transferred data size */
1721 req->req.actual += DD_STATUS_CURDMACNT(status);
1723 /* Work around the wierd underrun packet issue */
1724 if ((!ep->is_in) && (!(req->req.actual % ep->maxpacket)) &&
1725 ((req->req.length - req->req.actual) > 0)) {
1726 ep_dbg(ep, "Short packet in unexpected situation!\n");
1728 // WTF is this? An underrun packet on OUT with no ZLP! Hardware issue? */
1729 dd->dd_next_phy = dd->dd_next_v = NULL;
1730 dd->dd_buffer_addr = (u32) req->req.dma;
1731 dd->dd_buffer_addr += req->req.actual;
1734 /* Special handling for ISO EPs */
1735 dd->dd_setup = DD_SETUP_PACKETLEN(ep->ep.maxpacket) |
1736 DD_SETUP_DMALENBYTES(req->req.length - req->req.actual);
1738 /* Do the remainder of the req */
1739 udc_ep_out_req_dma(udc, ep);
1744 /* ISO endpoints are handled differently */
1745 if (ep->eptype == EP_ISO_TYPE) {
1747 req->req.actual = dd->iso_status[0] & 0xFFFF;
1750 /* For an Bulk IN EP, the DMA engine will only send data as specified in the
1751 descriptor. If the total transfer size is a multiple of the max packet
1752 size, then the transfer was completed, but no ZLP was sent. The ZLP needs
1753 to be sent using the FIFO mechanism to terminate this transfer */
1754 if (req->send_zlp) {
1755 udc_send_in_zlp(udc, ep, dd);
1761 /* Transfer request is complete */
1764 epstatus = udc_clearep_getsts(udc, ep->hwep_num);
1765 if(!list_empty((&ep->queue))) {
1767 udc_ep_in_req_dma(udc, ep);
1769 udc_ep_out_req_dma(udc, ep);
1772 ep->req_pending = 0;
1774 bufst = epstatus & EP_SEL_F;
1777 if(list_empty((&ep->queue))) {
1782 ep->req_pending = 0;
1787 if(!list_empty((&ep->queue))) {
1789 udc_ep_in_req_dma(udc, ep);
1791 udc_ep_out_req_dma(udc, ep);
1797 /* This function was called if a new request is ready to be placed into the SEND FIFO
1798 for transfer to the host, or when a previous transfer to the host has completed. */
1799 static int udc_ep_in_req(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep) {
1800 struct lpc32xx_request *req;
1801 u32 ts, epstatus, bufst;
1804 /* Select and clear EP interrupt */
1805 epstatus = udc_clearep_getsts(udc, ep->hwep_num);
1807 if (epstatus & EP_SEL_ST) {
1813 if (epstatus & EP_SEL_EPN) {
1814 /* NAK'd on other side */
1818 bufst = (epstatus & EP_SEL_F);
1820 /* Are any requests available? */
1821 if (list_empty(&ep->queue)) {
1823 /* No reqs and the hardware is idle, disable IRQ */
1824 uda_disable_hwepint(udc, ep->hwep_num);
1830 /* If both buffers are currently full, just exit for now */
1834 /* A buffer is available in the hardware, so we can fill it */
1835 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
1837 /* Limit packet size to the size of the EP */
1838 ts = req->req.length - req->req.actual;
1839 if (ts > ep->ep.maxpacket)
1840 ts = ep->ep.maxpacket;
1842 /* Write data to the EP0 FIFO and start transfer */
1843 ep_dbg(ep, "SEND %s 0x%x(%d)\n", ep->ep.name, (u32)(req->req.buf + req->req.actual), ts);
1844 udc_write_hwep(udc, ep->hwep_num, (req->req.buf + req->req.actual), ts);
1846 /* Increment data pointer */
1847 req->req.actual += ts;
1849 if (ts < ep->ep.maxpacket)
1851 else if ((req->req.actual != req->req.length) || (req->send_zlp)) {
1859 /* Transfer request is complete */
1864 /* Stay in data transfer state */
1868 static int udc_ep_out_req(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep) {
1869 struct lpc32xx_request *req;
1870 u32 tr, bufferspace, epstatus;
1872 /* Clear EP interrupt */
1873 epstatus = udc_clearep_getsts(udc, ep->hwep_num);
1875 if (epstatus & EP_SEL_ST) {
1880 if (epstatus & EP_SEL_EPN) {
1885 /* Are any requests available? */
1886 if (list_empty(&ep->queue)) {
1887 uda_disable_hwepint(udc, ep->hwep_num);
1892 if (epstatus & EP_SEL_F) {
1893 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
1894 if (req->req.length == 0) {
1895 ep_dbg(ep, "%s OUT zero buffer length!\n", ep->ep.name);
1899 /* Limit transfer size to size of endpoint */
1900 bufferspace = req->req.length - req->req.actual;
1901 if (bufferspace > ep->ep.maxpacket)
1902 bufferspace = ep->ep.maxpacket;
1904 /* Copy data to buffer from FIFO */
1905 prefetchw(req->req.buf + req->req.actual);
1906 tr = udc_read_hwep(udc, ep->hwep_num,
1907 (req->req.buf + req->req.actual), bufferspace);
1909 ep_dbg(ep, "RECV %s 0x%x(%d) %d %d\n", ep->ep.name, (u32)(req->req.buf + req->req.actual), tr,
1910 req->req.actual, req->req.length);
1911 req->req.actual += tr;
1913 if ((tr < ep->ep.maxpacket) || (req->req.actual == req->req.length)) {
1914 /* This is the last packet */
1924 static void udc_handle_ep(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep) {
1928 uda_disable_hwepint(udc, ep->hwep_num);
1934 /* Handle IN request */
1935 udc_ep_in_req(udc, ep);
1938 /* Handle OUT request */
1939 udc_ep_out_req(udc, ep);
1946 * Endpoint 0 functions
1949 static void udc_handle_dev(struct lpc32xx_udc *udc) {
1952 udc_protocol_cmd_w(udc, CMD_GET_DEV_STAT);
1953 tmp = udc_protocol_cmd_r(udc, DAT_GET_DEV_STAT);
1955 if (tmp & DEV_RST) {
1958 else if (tmp & DEV_CON_CH) {
1959 uda_power_event(udc, (tmp & DEV_CON));
1961 else if (tmp & DEV_SUS_CH) {
1962 if (tmp & DEV_SUS) {
1963 if (udc->vbus == 0) {
1965 } else if ((udc->gadget.speed !=
1966 USB_SPEED_UNKNOWN) && udc->driver &&
1967 udc->driver->suspend) {
1968 udc->driver->suspend(&udc->gadget);
1969 uda_resm_susp_event(udc, 1);
1973 if ((udc->gadget.speed != USB_SPEED_UNKNOWN) &&
1974 udc->driver && udc->driver->resume &&
1976 udc->driver->resume(&udc->gadget);
1977 uda_resm_susp_event(udc, 0);
1983 /* IN endpoint 0 transfer */
1984 static int udc_ep0_in_req(struct lpc32xx_udc *udc) {
1985 struct lpc32xx_request *req;
1986 struct lpc32xx_ep *ep0 = &udc->ep [0];
1989 if (list_empty(&ep0->queue))
1991 /* Nothing to send */
1995 req = list_entry(ep0->queue.next,
1996 struct lpc32xx_request, queue);
1999 tsend = ts = req->req.length - req->req.actual;
2002 udc_ep0_send_zlp(udc);
2006 else if (ts > ep0->ep.maxpacket) {
2007 /* Just send what we can */
2008 ts = ep0->ep.maxpacket;
2011 /* Write data to the EP0 FIFO and start transfer */
2012 udc_write_hwep(udc, EP_IN, (req->req.buf + req->req.actual), ts);
2014 /* Increment data pointer */
2015 req->req.actual += ts;
2017 if (tsend >= ep0->ep.maxpacket) {
2018 /* Stay in data transfer state */
2022 /* Transfer request is complete */
2023 udc->ep0state = WAIT_FOR_SETUP;
2028 /* OUT endpoint 0 transfer */
2029 static int udc_ep0_out_req(struct lpc32xx_udc *udc) {
2030 struct lpc32xx_request *req;
2031 struct lpc32xx_ep *ep0 = &udc->ep[0];
2032 u32 tr, bufferspace;
2034 if (list_empty(&ep0->queue)) {
2038 req = list_entry(ep0->queue.next, struct lpc32xx_request, queue);
2042 if (req->req.length == 0) {
2043 /* Just dequeue request */
2045 udc->ep0state = WAIT_FOR_SETUP;
2049 /* Get data from FIFO */
2050 bufferspace = req->req.length - req->req.actual;
2051 if (bufferspace > ep0->ep.maxpacket) {
2052 bufferspace = ep0->ep.maxpacket;
2055 /* Copy data to buffer */
2056 prefetchw(req->req.buf + req->req.actual);
2057 tr = udc_read_hwep(udc, EP_OUT,
2058 (req->req.buf + req->req.actual), bufferspace);
2059 req->req.actual += bufferspace;
2061 if (tr < ep0->ep.maxpacket) {
2062 /* This is the last packet */
2064 udc->ep0state = WAIT_FOR_SETUP;
2072 static int udc_get_status(struct lpc32xx_udc *udc, u16 reqtype, u16 wIndex) {
2073 struct lpc32xx_ep *ep;
2074 u32 ep0buff = 0, tmp;
2077 case USB_RECIP_INTERFACE:
2081 case USB_RECIP_DEVICE:
2082 ep0buff = (udc->selfpowered << USB_DEVICE_SELF_POWERED);
2083 if (udc->dev_status & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
2084 ep0buff |= (1 << USB_DEVICE_REMOTE_WAKEUP);
2088 case USB_RECIP_ENDPOINT:
2089 tmp = wIndex & USB_ENDPOINT_NUMBER_MASK;
2091 if ((tmp == 0) || (tmp >= NUM_ENDPOINTS) || (tmp && !ep->desc)) {
2095 if (wIndex & USB_DIR_IN) {
2097 /* Somethings wrong */
2100 } else if (ep->is_in)
2101 /* Not an IN endpoint */
2104 /* Get status of the endpoint */
2105 udc_protocol_cmd_w(udc, CMD_SEL_EP(ep->hwep_num));
2106 tmp = udc_protocol_cmd_r(udc, DAT_SEL_EP(ep->hwep_num));
2108 if (tmp & EP_SEL_ST) {
2109 ep0buff = (1 << USB_ENDPOINT_HALT);
2121 udc_write_hwep(udc, EP_IN, &ep0buff, 2);
2126 static void udc_handle_ep0_setup(struct lpc32xx_udc *udc) {
2127 struct lpc32xx_ep *ep, *ep0 = &udc->ep[0];
2128 struct usb_ctrlrequest ctrlpkt;
2130 u16 wIndex, wValue, wLength, reqtype, req, tmp;
2132 /* Nuke previous transfers */
2135 /* Get setup packet */
2136 bytes = udc_read_hwep(udc, EP_OUT, (u32 *) &ctrlpkt, 8);
2138 ep_dbg(ep0, "Incorrectly sized setup packet (s/b 8, is %d!\n", bytes);
2142 /* Native endianness */
2143 wIndex = le16_to_cpu(ctrlpkt.wIndex);
2144 wValue = le16_to_cpu(ctrlpkt.wValue);
2145 wLength = le16_to_cpu(ctrlpkt.wLength);
2146 reqtype = le16_to_cpu(ctrlpkt.bRequestType);
2148 /* Set direction of EP0 */
2149 if (likely(reqtype & USB_DIR_IN)) {
2155 /* Handle SETUP packet */
2156 req = le16_to_cpu(ctrlpkt.bRequest);
2158 case USB_REQ_CLEAR_FEATURE:
2159 case USB_REQ_SET_FEATURE:
2161 case (USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2162 if (wValue != USB_DEVICE_REMOTE_WAKEUP) {
2163 /* Nothing else handled */
2167 /* Tell board about event */
2168 if (req == USB_REQ_CLEAR_FEATURE)
2169 udc->dev_status &= ~(1 << USB_DEVICE_REMOTE_WAKEUP);
2171 udc->dev_status |= (1 << USB_DEVICE_REMOTE_WAKEUP);
2172 uda_remwkp_cgh(udc);
2175 case (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2176 tmp = wIndex & USB_ENDPOINT_NUMBER_MASK;
2177 if ((wValue != USB_ENDPOINT_HALT) || (tmp >= NUM_ENDPOINTS))
2180 /* Find hardware endpoint from logical endpoint */
2186 if (req == USB_REQ_CLEAR_FEATURE)
2187 udc_stall_hwep(udc, tmp);
2189 udc_clrstall_hwep(udc, tmp);
2198 case USB_REQ_SET_ADDRESS:
2199 if (reqtype == (USB_TYPE_STANDARD | USB_RECIP_DEVICE)) {
2200 udc_set_address(udc, wValue);
2205 case USB_REQ_GET_STATUS:
2206 udc_get_status(udc, reqtype, wIndex);
2210 /* Let GadgetFs handle the descriptor instead */
2214 if (likely(udc->driver)) {
2215 /* device-2-host (IN) or no data setup command, process immediately */
2216 spin_unlock(&udc->lock);
2217 i = udc->driver->setup(&udc->gadget, &ctrlpkt);
2218 spin_lock(&udc->lock);
2219 if (req == USB_REQ_SET_CONFIGURATION) {
2220 /* Configuration is set after endpoints are realized */
2222 /* Set configuration */
2223 udc_set_device_configured(udc);
2225 /* NAK EP interrupts do not need to be enabled for this
2226 driver, but if you really want them for statistic
2227 purposes, uncomment the following lines */
2228 udc_protocol_cmd_data_w(udc, CMD_SET_MODE, DAT_WR_BYTE(AP_CLK |
2229 #if defined(UDC_ENABLE_DMA)
2230 INAK_BI | INAK_II));
2232 INAK_BO | INAK_BI | INAK_IO | INAK_II));
2236 /* Clear configuration */
2237 udc_set_device_unconfigured(udc);
2239 /* Disable NAK interrupts */
2240 udc_protocol_cmd_data_w(udc, CMD_SET_MODE, DAT_WR_BYTE(AP_CLK));
2245 /* setup processing failed, force stall */
2246 dev_err(udc->dev, "req %02x.%02x protocol STALL; stat %d\n",
2248 udc->ep0state = WAIT_FOR_SETUP;
2254 /* ZLP IN packet on on data phase */
2255 udc_ep0_send_zlp(udc);
2261 udc_stall_hwep(udc, EP_IN);
2265 udc_ep0_send_zlp(udc);
2269 /* IN endpoint 0 transfer */
2270 static void udc_handle_ep0_in(struct lpc32xx_udc *udc) {
2271 struct lpc32xx_ep *ep0 = &udc->ep [0];
2274 /* Clear EP interrupt */
2275 epstatus = udc_clearep_getsts(udc, EP_IN);
2277 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2281 /* Stalled? Clear stall and reset buffers */
2282 if (epstatus & EP_SEL_ST) {
2283 udc_clrstall_hwep(udc, EP_IN);
2284 nuke(ep0, -ECONNABORTED);
2285 udc->ep0state = WAIT_FOR_SETUP;
2289 /* Is a buffer available? */
2290 if (!(epstatus & EP_SEL_F)) {
2291 /* Handle based on current state */
2292 if (udc->ep0state == DATA_IN) {
2293 udc_ep0_in_req(udc);
2296 /* Unknown state for EP0 oe end of DATA IN phase */
2297 nuke(ep0, -ECONNABORTED);
2298 udc->ep0state = WAIT_FOR_SETUP;
2303 /* OUT endpoint 0 transfer */
2304 static void udc_handle_ep0_out(struct lpc32xx_udc *udc) {
2305 struct lpc32xx_ep *ep0 = &udc->ep[0];
2308 /* Clear EP interrupt */
2309 epstatus = udc_clearep_getsts(udc, EP_OUT);
2311 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2316 if (epstatus & EP_SEL_ST) {
2317 udc_clrstall_hwep(udc, EP_OUT);
2318 nuke(ep0, -ECONNABORTED);
2319 udc->ep0state = WAIT_FOR_SETUP;
2323 /* A NAK may occur if a packet coudn't be received yet */
2324 if (epstatus & EP_SEL_EPN) {
2327 /* Setup packet incoming? */
2328 if (epstatus & EP_SEL_STP) {
2330 udc->ep0state = WAIT_FOR_SETUP;
2333 /* Data available? */
2334 if (epstatus & EP_SEL_F) {
2335 /* Handle based on current state */
2336 switch (udc->ep0state) {
2337 case WAIT_FOR_SETUP:
2338 udc_handle_ep0_setup(udc);
2342 udc_ep0_out_req(udc);
2346 /* Unknown state for EP0 */
2347 nuke(ep0, -ECONNABORTED);
2348 udc->ep0state = WAIT_FOR_SETUP;
2353 static int lpc32xx_get_frame(struct usb_gadget *gadget)
2355 struct lpc32xx_udc *udc = to_udc(gadget);
2357 if (!to_udc(gadget)->clocked)
2360 return (int) udc_get_current_frame(udc);
2363 static int lpc32xx_wakeup(struct usb_gadget *gadget)
2368 static int lpc32xx_set_selfpowered(struct usb_gadget *gadget, int is_on)
2370 struct lpc32xx_udc *udc = to_udc(gadget);
2372 /* Always self-powered */
2373 udc->selfpowered = (is_on != 0);
2379 /* vbus is here! turn everything on that's ready */
2380 static int lpc32xx_vbus_session(struct usb_gadget *gadget, int is_active)
2382 struct lpc32xx_udc *udc = to_udc(gadget);
2384 /* Doesn't need lock */
2386 pullup(udc, is_active);
2393 static int lpc32xx_pullup(struct usb_gadget *gadget, int is_on)
2395 struct lpc32xx_udc *udc = to_udc(gadget);
2397 /* Doesn't need lock */
2403 static const struct usb_gadget_ops lpc32xx_udc_ops = {
2404 .get_frame = lpc32xx_get_frame,
2405 .wakeup = lpc32xx_wakeup,
2406 .set_selfpowered = lpc32xx_set_selfpowered,
2407 .vbus_session = lpc32xx_vbus_session,
2408 .pullup = lpc32xx_pullup,
2411 static void nop_release(struct device *dev)
2413 /* nothing to free */
2416 static struct lpc32xx_udc controller = {
2418 .ops = &lpc32xx_udc_ops,
2419 .ep0 = &controller.ep[0].ep,
2420 .name = driver_name,
2422 .init_name = "gadget",
2423 .release = nop_release,
2429 .ops = &lpc32xx_ep_ops,
2434 .hwep_num = 0, /* Can be 0 or 1, has special handling */
2436 .eptype = EP_CTL_TYPE,
2441 .ops = &lpc32xx_ep_ops,
2446 .hwep_num = 0, /* 2 or 3, will be set later */
2448 .eptype = EP_INT_TYPE,
2453 .ops = &lpc32xx_ep_ops,
2458 .hwep_num = 0, /* 4 or 5, will be set later */
2461 .eptype = EP_BLK_TYPE,
2466 .ops = &lpc32xx_ep_ops,
2471 .hwep_num = 0, /* 6 or 7, will be set later */
2474 .eptype = EP_ISO_TYPE,
2479 .ops = &lpc32xx_ep_ops,
2484 .hwep_num = 0, /* 8 or 9, will be set later */
2486 .eptype = EP_INT_TYPE,
2491 .ops = &lpc32xx_ep_ops,
2495 .hwep_num_base = 10,
2496 .hwep_num = 0, /* 10 or 11, will be set later */
2499 .eptype = EP_BLK_TYPE,
2504 .ops = &lpc32xx_ep_ops,
2508 .hwep_num_base = 12,
2509 .hwep_num = 0, /* 12 or 13, will be set later */
2512 .eptype = EP_ISO_TYPE,
2517 .ops = &lpc32xx_ep_ops,
2521 .hwep_num_base = 14,
2524 .eptype = EP_INT_TYPE,
2529 .ops = &lpc32xx_ep_ops,
2533 .hwep_num_base = 16,
2537 .eptype = EP_BLK_TYPE,
2542 .ops = &lpc32xx_ep_ops,
2546 .hwep_num_base = 18,
2550 .eptype = EP_ISO_TYPE,
2555 .ops = &lpc32xx_ep_ops,
2559 .hwep_num_base = 20,
2562 .eptype = EP_INT_TYPE,
2566 .name = "ep11-bulk",
2567 .ops = &lpc32xx_ep_ops,
2571 .hwep_num_base = 22,
2575 .eptype = EP_BLK_TYPE,
2580 .ops = &lpc32xx_ep_ops,
2584 .hwep_num_base = 24,
2588 .eptype = EP_ISO_TYPE,
2593 .ops = &lpc32xx_ep_ops,
2597 .hwep_num_base = 26,
2600 .eptype = EP_INT_TYPE,
2604 .name = "ep14-bulk",
2605 .ops = &lpc32xx_ep_ops,
2609 .hwep_num_base = 28,
2613 .eptype = EP_BLK_TYPE,
2617 .name = "ep15-bulk",
2618 .ops = &lpc32xx_ep_ops,
2622 .hwep_num_base = 30,
2626 .eptype = EP_BLK_TYPE,
2630 /* ISO and status interrupts */
2631 static irqreturn_t lpc32xx_usb_lp_irq(int irq, void *_udc) {
2633 struct lpc32xx_udc *udc = _udc;
2635 spin_lock(&udc->lock);
2637 /* Read the device status register */
2638 devstat = __raw_readl(USBD_DEVINTST(udc->udp_baseaddr));
2639 devstat &= ~USBD_EP_FAST;
2640 __raw_writel(devstat, USBD_DEVINTCLR(udc->udp_baseaddr));
2641 devstat = devstat & udc->enabled_devints;
2643 /* Device specific handling needed? */
2644 if (devstat & USBD_DEV_STAT) {
2645 udc_handle_dev(udc);
2648 /* Start of frame? */
2649 if (devstat & FRAME_INT) {
2650 /* The frame interrupt isn't really needed for ISO support,
2651 as the driver will queue the necessary packets */
2652 dev_dbg(udc->dev, "Device frame interrupt not supported\n");
2656 if (devstat & ERR_INT) {
2657 /* All types of errors, from cable removal during transfer to
2658 misc protocol and bit errors. These are mostly for just info,
2659 as the USB hardware will work around these */
2660 udc_protocol_cmd_w(udc, CMD_RD_ERR_STAT);
2661 tmp = udc_protocol_cmd_r(udc, DAT_RD_ERR_STAT);
2662 dev_err(udc->dev, "Device error (0x%x)!\n", tmp);
2665 spin_unlock(&udc->lock);
2670 static irqreturn_t lpc32xx_usb_hp_irq(int irq, void *_udc)
2673 struct lpc32xx_udc *udc = _udc;
2675 spin_lock(&udc->lock);
2677 /* Read the device status register */
2678 tmp = __raw_readl(USBD_DEVINTST(udc->udp_baseaddr));
2679 __raw_writel(USBD_EP_FAST, USBD_DEVINTCLR(udc->udp_baseaddr));
2682 tmp = __raw_readl(USBD_EPINTST(udc->udp_baseaddr));
2684 /* Special handling for EP0 */
2685 if (tmp & (EP_MASK_SEL(0, EP_OUT) | EP_MASK_SEL(0, EP_IN))) {
2687 if (tmp & (EP_MASK_SEL(0, EP_IN)))
2688 udc_handle_ep0_in(udc);
2690 /* Handle EP0 OUT */
2691 if (tmp & (EP_MASK_SEL(0, EP_OUT)))
2692 udc_handle_ep0_out(udc);
2696 if (tmp & ~(EP_MASK_SEL(0, EP_OUT) | EP_MASK_SEL(0, EP_IN))) {
2697 #if defined(UDC_ENABLE_DMA)
2698 udc_handle_eps(udc, tmp);
2703 /* Handle other EP interrupts */
2704 for (i = 1; i < NUM_ENDPOINTS; i++) {
2705 if (tmp & (1 << udc->ep [i].hwep_num))
2706 udc_handle_ep(udc, &udc->ep[i]);
2711 spin_unlock(&udc->lock);
2715 static irqreturn_t lpc32xx_usb_devdma_irq(int irq, void *_udc)
2717 struct lpc32xx_udc *udc = _udc;
2719 #if defined(UDC_ENABLE_DMA)
2723 spin_lock(&udc->lock);
2725 /* Handle EP DMA EOT interrupts */
2726 tmp = __raw_readl(USBD_EOTINTST(udc->udp_baseaddr)) |
2727 __raw_readl(USBD_NDDRTINTST(udc->udp_baseaddr)) |
2728 __raw_readl(USBD_SYSERRTINTST(udc->udp_baseaddr));
2729 for (i = 1; i < NUM_ENDPOINTS; i++) {
2730 if (tmp & (1 << udc->ep [i].hwep_num))
2731 udc_handle_dma_ep(udc, &udc->ep[i]);
2734 spin_unlock(&udc->lock);
2742 * VBUS detection, pullup handler, and Gadget cable state notification
2745 static int vbus_handler_thread(void *udc_)
2747 struct lpc32xx_udc *udc = udc_;
2751 while (!kthread_should_stop()) {
2752 /* Get the interrupt from the transceiver */
2753 value = i2c_read(ISP1301_I2C_INTERRUPT_LATCH);
2755 /* Discharge VBUS real quick */
2756 i2c_write(OTG1_VBUS_DISCHRG, ISP1301_I2C_OTG_CONTROL_1);
2758 /* Give VBUS some time (200mS) to discharge */
2759 set_current_state(TASK_UNINTERRUPTIBLE);
2760 schedule_timeout(200 / (1000 / HZ));
2761 set_current_state(TASK_INTERRUPTIBLE);
2763 /* Disable VBUS discharge resistor */
2764 i2c_write(OTG1_VBUS_DISCHRG,
2765 (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR));
2767 if (udc->enabled != 0) {
2768 /* Get the VBUS status from the transceiver */
2769 value = i2c_read(ISP1301_I2C_OTG_CONTROL_2);
2771 /* VBUS on or off? */
2772 if (value & OTG_B_SESS_VLD) {
2775 /* Enable USB clocks */
2776 udc_clk_set(udc, 1);
2778 /* Setup the UDC and ep0 */
2782 /* Will force disconnect */
2787 if (udc->last_vbus != udc->vbus) {
2788 lpc32xx_vbus_session(&udc->gadget, udc->vbus);
2789 udc->last_vbus = udc->vbus;
2793 /* Clear interrupt */
2794 i2c_write(~0, ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR);
2796 /* sleep if nothing to send */
2797 set_current_state(TASK_INTERRUPTIBLE);
2801 udc->thread_task = NULL;
2806 static irqreturn_t lpc32xx_usb_vbus_irq(int irq, void *_udc)
2808 struct lpc32xx_udc *udc = _udc;
2810 /* Kick off the VBUS handler thread */
2811 udc->thread_wakeup_needed = 1;
2812 wake_up_process(udc->thread_task);
2817 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
2819 struct lpc32xx_udc *udc = &controller;
2822 if (!driver || driver->speed < USB_SPEED_FULL ||
2823 !driver->bind || !driver->setup) {
2824 dev_err(udc->dev, "bad parameter.\n");
2829 dev_err(udc->dev, "UDC already has a gadget driver\n");
2833 udc->driver = driver;
2834 udc->gadget.dev.driver = &driver->driver;
2836 udc->selfpowered = 1;
2839 retval = driver->bind(&udc->gadget);
2841 dev_err(udc->dev, "driver->bind() returned %d\n", retval);
2843 udc->selfpowered = 0;
2845 udc->gadget.dev.driver = NULL;
2849 dev_dbg(udc->dev, "bound to %s\n", driver->driver.name);
2851 /* Force VBUS process once to check for cable insertion */
2852 udc->last_vbus = udc->vbus = 0;
2853 wake_up_process(udc->thread_task);
2854 enable_irq(udc->udp_irq[IRQ_USB_ATX]);
2858 EXPORT_SYMBOL (usb_gadget_register_driver);
2860 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
2862 struct lpc32xx_udc *udc = &controller;
2864 if (!driver || driver != udc->driver || !driver->unbind)
2867 disable_irq(udc->udp_irq[IRQ_USB_ATX]);
2868 local_irq_disable();
2873 driver->unbind(&udc->gadget);
2874 udc->gadget.dev.driver = NULL;
2877 dev_dbg(udc->dev, "unbound from %s\n", driver->driver.name);
2880 EXPORT_SYMBOL (usb_gadget_unregister_driver);
2882 /*-------------------------------------------------------------------------*/
2884 static void lpc32xx_udc_shutdown(struct platform_device *dev)
2886 /* Force disconnect on reboot */
2887 struct lpc32xx_udc *udc = &controller;
2892 static int __init lpc32xx_udc_probe(struct platform_device *pdev)
2894 struct device *dev = &pdev->dev;
2895 struct lpc32xx_udc *udc = &controller;
2897 struct resource *res;
2898 dma_addr_t dma_handle;
2899 struct i2c_adapter *i2c_adap;
2900 struct i2c_board_info i2c_info;
2902 /* init software state */
2903 udc->gadget.dev.parent = dev;
2905 udc->dev = &pdev->dev;
2908 if (!dev->platform_data) {
2909 dev_err(udc->dev, "missing platform_data\n");
2913 udc->board = (struct lpc32xx_usbd_cfg *) dev->platform_data;
2916 * Resources are mapped as follows:
2917 * [0] = IORESOURCE_MEM, base address and size of USB space
2918 * [1] = IORESOURCE_IRQ, USB device low priority interrupt number
2919 * [2] = IORESOURCE_IRQ, USB device high priority interrupt number
2920 * [3] = IORESOURCE_IRQ, USB device interrupt number
2921 * [4] = IORESOURCE_IRQ, USB transciever interrupt number
2923 if (pdev->num_resources != 5) {
2924 dev_err(udc->dev, "invalid num_resources\n");
2928 if (pdev->resource[0].flags != IORESOURCE_MEM) {
2929 dev_err(udc->dev, "invalid resource type\n");
2933 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2937 spin_lock_init(&udc->lock);
2940 for (i = 0; i < 4; i++) {
2941 if (pdev->resource[i + 1].flags != IORESOURCE_IRQ) {
2942 dev_err(udc->dev, "invalid resource type\n");
2945 udc->udp_irq[i] = platform_get_irq(pdev, i);
2948 udc->io_p_start = res->start;
2949 udc->io_p_size = res->end - res->start + 1;
2950 if (!request_mem_region(udc->io_p_start, udc->io_p_size, driver_name)) {
2951 dev_err(udc->dev, "someone's using UDC memory\n");
2955 /* Enable AHB slave USB clock, needed for further USB clock control */
2956 __raw_writel(USB_SLAVE_HCLK_EN | (1 << 19), USB_CTRL);
2958 /* Get required clocks */
2959 udc->usb_pll_clk = clk_get(&pdev->dev, "ck_pll5");
2960 if (IS_ERR(udc->usb_pll_clk)) {
2961 dev_err(udc->dev, "failed to acquire USB PLL");
2962 retval = PTR_ERR(udc->usb_pll_clk);
2965 udc->usb_slv_clk = clk_get(&pdev->dev, "ck_usbd");
2966 if (IS_ERR(udc->usb_slv_clk)) {
2967 dev_err(udc->dev, "failed to acquire USB device clock");
2968 retval = PTR_ERR(udc->usb_slv_clk);
2969 goto usb_clk_get_fail;
2972 /* Setup PLL clock to 48MHz */
2973 retval = clk_enable(udc->usb_pll_clk);
2975 dev_err(udc->dev, "failed to start USB PLL");
2976 goto pll_enable_fail;
2979 retval = clk_set_rate(udc->usb_pll_clk, 48000);
2981 dev_err(udc->dev, "failed to set USB clock rate");
2985 __raw_writel(__raw_readl(USB_CTRL) | USB_DEV_NEED_CLK_EN, USB_CTRL);
2987 /* Enable USB device clock */
2988 retval = clk_enable(udc->usb_slv_clk);
2990 dev_err(udc->dev, "failed to start USB device clock");
2991 goto usb_clk_enable_fail;
2994 /* Set to enable all needed USB OTG clocks */
2995 __raw_writel(USB_CLOCK_MASK, USB_OTG_CLK_CTRL);
2997 /* This is unsafe */
2998 while ((__raw_readl(USB_OTG_CLK_STAT) & USB_CLOCK_MASK) !=
3001 /* All clocks are now on */
3004 retval = i2c_add_driver(&isp1301_driver);
3006 dev_err(udc->dev, "Failed to add ISP1301 driver \n");
3009 i2c_adap = i2c_get_adapter(2);
3010 memset(&i2c_info, 0, sizeof(struct i2c_board_info));
3011 isp1301_i2c_client = i2c_new_probed_device(i2c_adap, &i2c_info,
3013 i2c_put_adapter(i2c_adap);
3014 if (!isp1301_i2c_client) {
3015 dev_err(udc->dev,"failed to connect I2C to ISP1301 USB Transceiver");
3016 goto i2c_probe_fail;
3019 dev_info(udc->dev, "I2C device at address 0x%x", isp1301_i2c_client->addr);
3021 isp1301_udc_configure(udc);
3022 /* Map register space */
3023 udc->udp_baseaddr = ioremap(udc->io_p_start, udc->io_p_size);
3024 if (!udc->udp_baseaddr) {
3026 dev_err(udc->dev, "IO map failure");
3030 /* Allocate memory for the UDCA */
3031 udc->udca_v_base = dma_alloc_coherent(&pdev->dev, UDCA_BUFF_SIZE,
3032 &dma_handle, (GFP_KERNEL | GFP_DMA));
3033 if (!udc->udca_v_base)
3035 dev_err(udc->dev, "error getting UDCA region");
3037 goto dma_alloc_fail;
3039 udc->udca_p_base = (void *) dma_handle;
3040 dev_dbg(udc->dev, "DMA buffer(0x%x bytes), P:0x%08x, V:0x%08x",
3041 UDCA_BUFF_SIZE, (u32) udc->udca_p_base, (u32) udc->udca_v_base);
3043 /* Setup the DD DMA memory pool */
3044 udc->dd_cache = dma_pool_create ("udc_dd", udc->dev,
3045 sizeof (struct lpc32xx_usbd_dd_gad), sizeof (u32), 0);
3046 if (!udc->dd_cache) {
3047 dev_err(udc->dev, "error getting DD DMA region");
3049 goto dma2_alloc_fail;
3052 /* Clear USB peripheral and initialize gadget endpoints */
3056 retval = device_register(&udc->gadget.dev);
3058 dev_err(udc->dev, "Device registration failure");
3059 goto dev_register_fail;
3062 /* Request IRQs - low and high priority USB device IRQs are routed to the
3063 same handler, while the DMA interrupt is routed elsewhere */
3064 retval = request_irq(udc->udp_irq[IRQ_USB_LP], lpc32xx_usb_lp_irq,
3067 dev_err(udc->dev, "LP request irq %d failed", udc->udp_irq[IRQ_USB_LP]);
3070 retval = request_irq(udc->udp_irq[IRQ_USB_HP], lpc32xx_usb_hp_irq,
3073 dev_err(udc->dev, "HP request irq %d failed", udc->udp_irq[IRQ_USB_HP]);
3077 retval = request_irq(udc->udp_irq[IRQ_USB_DEVDMA], lpc32xx_usb_devdma_irq,
3080 dev_err(udc->dev, "DEV request irq %d failed", udc->udp_irq[IRQ_USB_DEVDMA]);
3084 /* Create VBUS handler thread */
3085 udc->thread_wakeup_needed = 0;
3086 udc->thread_task = kthread_create(vbus_handler_thread, udc,
3087 "vbus_handler_thread");
3088 if (IS_ERR(udc->thread_task)) {
3089 retval = PTR_ERR(udc->thread_task);
3090 dev_err(udc->dev, "VBUS handler thread failures");
3091 goto vbus_thread_fail;
3094 /* The transceiver interrupt is used for VBUS detection and will
3095 kick off the VBUS handler thread */
3096 retval = request_irq(udc->udp_irq[IRQ_USB_ATX], lpc32xx_usb_vbus_irq,
3097 IRQF_ONESHOT, "udc_otg", udc);
3099 dev_err(udc->dev, "VBUS request irq %d failed\n", udc->udp_irq[IRQ_USB_ATX]);
3103 /* Keep VBUS IRQ disabled until GadgetFS starts up */
3104 disable_irq(udc->udp_irq[IRQ_USB_ATX]);
3106 dev_set_drvdata(dev, udc);
3107 device_init_wakeup(dev, 1);
3108 create_debug_file(udc);
3110 /* Disable clocks for now */
3111 udc_clk_set(udc, 0);
3113 dev_info(udc->dev, "%s version %s\n", driver_name, DRIVER_VERSION);
3117 kthread_stop(udc->thread_task);
3119 free_irq(udc->udp_irq[IRQ_USB_DEVDMA], udc);
3121 free_irq(udc->udp_irq[IRQ_USB_HP], udc);
3123 free_irq(udc->udp_irq[IRQ_USB_LP], udc);
3125 device_unregister(&udc->gadget.dev);
3127 dma_pool_destroy(udc->dd_cache);
3129 dma_free_coherent(&pdev->dev, UDCA_BUFF_SIZE,
3130 udc->udca_v_base, (dma_addr_t) udc->udca_p_base);
3132 iounmap(udc->udp_baseaddr);
3135 i2c_del_driver(&isp1301_driver);
3137 clk_disable(udc->usb_slv_clk);
3138 usb_clk_enable_fail:
3140 clk_disable(udc->usb_pll_clk);
3142 clk_put(udc->usb_slv_clk);
3144 clk_put(udc->usb_pll_clk);
3146 release_mem_region(udc->io_p_start, udc->io_p_size);
3147 dev_err(udc->dev, "%s probe failed, %d\n", driver_name, retval);
3152 static int __exit lpc32xx_udc_remove(struct platform_device *pdev)
3154 struct lpc32xx_udc *udc = platform_get_drvdata(pdev);
3159 udc_clk_set(udc, 1);
3163 if (udc->irq_asrtd == 1)
3164 disable_irq(udc->udp_irq[IRQ_USB_ATX]);
3165 free_irq(udc->udp_irq[IRQ_USB_ATX], udc);
3167 device_init_wakeup(&pdev->dev, 0);
3168 remove_debug_file(udc);
3170 dma_pool_destroy(udc->dd_cache);
3171 dma_free_coherent(&pdev->dev, UDCA_BUFF_SIZE,
3172 udc->udca_v_base, (dma_addr_t) udc->udca_p_base);
3173 kthread_stop(udc->thread_task);
3174 free_irq(udc->udp_irq[IRQ_USB_DEVDMA], udc);
3175 free_irq(udc->udp_irq[IRQ_USB_HP], udc);
3176 free_irq(udc->udp_irq[IRQ_USB_LP], udc);
3178 device_unregister(&udc->gadget.dev);
3180 clk_disable(udc->usb_slv_clk);
3181 clk_put(udc->usb_slv_clk);
3182 clk_disable(udc->usb_pll_clk);
3183 clk_put(udc->usb_pll_clk);
3184 iounmap(udc->udp_baseaddr);
3185 i2c_del_driver(&isp1301_driver);
3186 release_mem_region(udc->io_p_start, udc->io_p_size);
3192 static int lpc32xx_udc_suspend(struct platform_device *pdev, pm_message_t mesg)
3194 struct lpc32xx_udc *udc = platform_get_drvdata(pdev);
3197 /* Power down ISP */
3198 isp1301_set_powerstate(0);
3200 /* Disable clocking */
3201 udc_clk_set(udc, 0);
3203 /* Keep clock flag on, so we know to re-enable clocks
3207 /* Kill OTG and I2C clocks */
3208 __raw_writel(0, USB_OTG_CLK_CTRL);
3209 while ((__raw_readl(USB_OTG_CLK_STAT) & OTGOFF_CLK_MASK) !=
3212 /* Kill global USB clock */
3213 clk_disable(udc->usb_slv_clk);
3219 static int lpc32xx_udc_resume(struct platform_device *pdev)
3221 struct lpc32xx_udc *udc = platform_get_drvdata(pdev);
3224 /* Enable global USB clock */
3225 clk_enable(udc->usb_slv_clk);
3227 /* Enable clocking */
3228 udc_clk_set(udc, 1);
3230 /* ISP back to normal power mode */
3231 isp1301_set_powerstate(1);
3237 #define lpc32xx_udc_suspend NULL
3238 #define lpc32xx_udc_resume NULL
3241 static struct platform_driver lpc32xx_udc_driver = {
3242 .probe = lpc32xx_udc_probe,
3243 .remove = __exit_p(lpc32xx_udc_remove),
3244 .shutdown = lpc32xx_udc_shutdown,
3245 .suspend = lpc32xx_udc_suspend,
3246 .resume = lpc32xx_udc_resume,
3248 .name = (char *) driver_name,
3249 .owner = THIS_MODULE,
3253 static int __init udc_init_module(void)
3255 return platform_driver_register(&lpc32xx_udc_driver);
3257 module_init(udc_init_module);
3259 static void __exit udc_exit_module(void)
3261 platform_driver_unregister(&lpc32xx_udc_driver);
3263 module_exit(udc_exit_module);
3265 MODULE_DESCRIPTION("LPC32XX udc driver");
3266 MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com");
3267 MODULE_LICENSE("GPL");
3268 MODULE_ALIAS("platform:lpc32xx_udc");