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, ISP1301_I2C_ADDR + 1, 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) {
1023 /* Re-init device controller and EP0 */
1025 udc->gadget.speed = USB_SPEED_FULL;
1027 for (i = 1; i < NUM_ENDPOINTS; i++) {
1028 struct lpc32xx_ep *ep = &udc->ep[i];
1029 ep->req_pending = 0;
1033 /* Send a ZLP on EP0 */
1034 static void udc_ep0_send_zlp(struct lpc32xx_udc *udc) {
1035 udc_write_hwep(udc, EP_IN, NULL, 0);
1038 /* Get current frame number */
1039 static u16 udc_get_current_frame(struct lpc32xx_udc *udc) {
1042 udc_protocol_cmd_w(udc, CMD_RD_FRAME);
1043 flo = (u16) udc_protocol_cmd_r(udc, DAT_RD_FRAME);
1044 fhi = (u16) udc_protocol_cmd_r(udc, DAT_RD_FRAME);
1046 return (fhi << 8) | flo;
1049 /* Set the device as configured - enables all endpoints */
1050 static inline void udc_set_device_configured(struct lpc32xx_udc *udc) {
1051 udc_protocol_cmd_data_w(udc, CMD_CFG_DEV, DAT_WR_BYTE(CONF_DVICE));
1054 /* Set the device as unconfigured - disables all endpoints */
1055 static inline void udc_set_device_unconfigured(struct lpc32xx_udc *udc) {
1056 udc_protocol_cmd_data_w(udc, CMD_CFG_DEV, DAT_WR_BYTE(0));
1059 /* reinit == restore inital software state */
1060 static void udc_reinit(struct lpc32xx_udc *udc)
1064 INIT_LIST_HEAD(&udc->gadget.ep_list);
1065 INIT_LIST_HEAD(&udc->gadget.ep0->ep_list);
1067 for (i = 0; i < NUM_ENDPOINTS; i++) {
1068 struct lpc32xx_ep *ep = &udc->ep[i];
1071 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
1074 ep->ep.maxpacket = ep->maxpacket;
1075 INIT_LIST_HEAD(&ep->queue);
1076 ep->req_pending = 0;
1079 udc->ep0state = WAIT_FOR_SETUP;
1082 static void done(struct lpc32xx_ep *ep, struct lpc32xx_request *req, int status)
1084 struct lpc32xx_udc *udc = ep->udc;
1086 list_del_init(&req->queue);
1087 if (req->req.status == -EINPROGRESS) {
1088 req->req.status = status;
1091 status = req->req.status;
1094 #if defined(UDC_ENABLE_DMA)
1096 enum dma_data_direction direction;
1099 direction = DMA_TO_DEVICE;
1101 direction = DMA_FROM_DEVICE;
1104 dma_unmap_single(ep->udc->gadget.dev.parent,
1105 req->req.dma, req->req.length, direction);
1110 dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
1111 req->req.dma, req->req.length, direction);
1115 udc_dd_free(udc, req->dd_desc_ptr);
1119 if (status && status != -ESHUTDOWN) {
1120 ep_dbg(ep, "%s done %p, status %d\n", ep->ep.name, req, status);
1123 spin_unlock(&udc->lock);
1124 req->req.complete(&ep->ep, &req->req);
1125 spin_lock(&udc->lock);
1129 static void nuke(struct lpc32xx_ep *ep, int status)
1131 struct lpc32xx_request *req;
1133 while (!list_empty(&ep->queue)) {
1134 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
1135 done(ep, req, status);
1139 if (status == -ESHUTDOWN) {
1140 uda_disable_hwepint(ep->udc, ep->hwep_num);
1141 udc_disable_hwep(ep->udc, ep->hwep_num);
1146 static void stop_activity(struct lpc32xx_udc *udc)
1148 struct usb_gadget_driver *driver = udc->driver;
1151 if (udc->gadget.speed == USB_SPEED_UNKNOWN)
1154 udc->gadget.speed = USB_SPEED_UNKNOWN;
1157 for (i = 0; i < NUM_ENDPOINTS; i++) {
1158 struct lpc32xx_ep *ep = &udc->ep[i];
1159 nuke(ep, -ESHUTDOWN);
1162 driver->disconnect(&udc->gadget);
1164 * Wait for all the endpoints to disable,
1165 * before disabling clocks. Don't wait if
1166 * endpoints are not enabled.
1168 if(atomic_read(&udc->enabled_ep_cnt))
1169 wait_event_interruptible(udc->ep_disable_wait_queue,
1170 (atomic_read(&udc->enabled_ep_cnt) == 0));
1173 isp1301_pullup_enable(0);
1179 * Activate or kill host pullup
1181 static void pullup(struct lpc32xx_udc *udc, int is_on)
1186 if (!udc->enabled || !udc->vbus)
1190 udc_clk_set(udc, 1);
1191 isp1301_pullup_enable(1);
1194 isp1301_pullup_enable(0);
1195 udc_clk_set(udc, 0);
1199 static int lpc32xx_ep_disable (struct usb_ep * _ep)
1201 struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
1202 struct lpc32xx_udc *udc = ep->udc;
1203 unsigned long flags;
1205 if ((ep->hwep_num_base == 0) || (ep->hwep_num == 0)) {
1209 local_irq_save(flags);
1211 nuke(ep, -ESHUTDOWN);
1213 /* restore the endpoint's pristine config */
1216 /* Clear all DMA statuses for this EP */
1217 udc_ep_dma_disable(udc, ep->hwep_num);
1218 __raw_writel((1 << ep->hwep_num), USBD_EOTINTCLR(udc->udp_baseaddr));
1219 __raw_writel((1 << ep->hwep_num), USBD_NDDRTINTCLR(udc->udp_baseaddr));
1220 __raw_writel((1 << ep->hwep_num), USBD_SYSERRTINTCLR(udc->udp_baseaddr));
1221 __raw_writel((1 << ep->hwep_num), USBD_DMARCLR(udc->udp_baseaddr));
1223 /* Remove the DD pointer in the UDCA */
1224 udc->udca_v_base[ep->hwep_num] = 0;
1227 /* Disable and reset endpoint and interrupt */
1228 uda_clear_hwepint(udc, ep->hwep_num);
1229 udc_unrealize_hwep(udc, ep->hwep_num);
1233 local_irq_restore(flags);
1235 atomic_dec(&udc->enabled_ep_cnt);
1236 wake_up(&udc->ep_disable_wait_queue);
1241 static int lpc32xx_ep_enable(struct usb_ep *_ep,
1242 const struct usb_endpoint_descriptor *desc)
1244 struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
1245 struct lpc32xx_udc *udc = ep->udc;
1248 unsigned long flags;
1250 /* Verify EP data */
1251 if ((!_ep) || (!ep) || (!desc) || (ep->desc) ||
1252 (desc->bDescriptorType != USB_DT_ENDPOINT) ||
1253 ((maxpacket = le16_to_cpu(desc->wMaxPacketSize)) == 0) ||
1254 (maxpacket > ep->maxpacket)) {
1255 dev_dbg(udc->dev, "bad ep or descriptor\n");
1259 /* Don't touch EP0 */
1260 if (ep->hwep_num_base == 0) {
1261 dev_dbg(udc->dev, "Can't re-enable EP0!!!\n");
1265 /* Is driver ready? */
1266 if ((!udc->driver) || (udc->gadget.speed == USB_SPEED_UNKNOWN)) {
1267 dev_dbg(udc->dev, "bogus device state\n");
1271 tmp = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
1273 case USB_ENDPOINT_XFER_CONTROL:
1276 case USB_ENDPOINT_XFER_INT:
1277 if (maxpacket > ep->maxpacket) {
1278 dev_dbg(udc->dev, "Bad INT endpoint maxpacket %d\n", maxpacket);
1283 case USB_ENDPOINT_XFER_BULK:
1284 switch (maxpacket) {
1292 dev_dbg(udc->dev, "Bad BULK endpoint maxpacket %d\n", maxpacket);
1297 case USB_ENDPOINT_XFER_ISOC:
1301 local_irq_save(flags);
1303 /* Initialize endpoint to match the selected descriptor */
1304 ep->is_in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
1306 ep->ep.maxpacket = maxpacket;
1308 /* Map hardware endpoint from base and direction */
1310 /* IN endpoints are offset 1 from the OUT endpoint */
1311 ep->hwep_num = ep->hwep_num_base + EP_IN;
1314 ep->hwep_num = ep->hwep_num_base;
1317 ep_dbg(ep, "EP enabled: %s, HW:%d, MP:%d IN:%d\n", ep->ep.name, ep->hwep_num,
1318 maxpacket, (ep->is_in == 1));
1320 /* Realize the endpoint, interrupt is enabled later when
1321 buffers are queued, IN EPs will NAK until buffers are ready */
1322 udc_realize_hwep(udc, ep->hwep_num, ep->ep.maxpacket);
1323 udc_clr_buffer_hwep(udc, ep->hwep_num);
1324 uda_disable_hwepint(udc, ep->hwep_num);
1325 udc_clrstall_hwep(udc, ep->hwep_num);
1327 /* Clear all DMA statuses for this EP */
1328 udc_ep_dma_disable(udc, ep->hwep_num);
1329 __raw_writel((1 << ep->hwep_num), USBD_EOTINTCLR(udc->udp_baseaddr));
1330 __raw_writel((1 << ep->hwep_num), USBD_NDDRTINTCLR(udc->udp_baseaddr));
1331 __raw_writel((1 << ep->hwep_num), USBD_SYSERRTINTCLR(udc->udp_baseaddr));
1332 __raw_writel((1 << ep->hwep_num), USBD_DMARCLR(udc->udp_baseaddr));
1334 #if defined(UDC_ENABLE_DMA)
1338 local_irq_restore(flags);
1340 atomic_inc(&udc->enabled_ep_cnt);
1344 /* Allocate a USB request list */
1345 static struct usb_request *lpc32xx_ep_alloc_request(
1346 struct usb_ep *_ep, gfp_t gfp_flags)
1348 struct lpc32xx_request *req;
1350 req = kzalloc(sizeof (struct lpc32xx_request), gfp_flags);
1354 INIT_LIST_HEAD(&req->queue);
1358 /* De-allocate a USB request list */
1359 static void lpc32xx_ep_free_request(struct usb_ep *_ep,
1360 struct usb_request *_req)
1362 struct lpc32xx_request *req;
1364 req = container_of(_req, struct lpc32xx_request, req);
1365 BUG_ON(!list_empty(&req->queue));
1369 static int lpc32xx_ep_queue(struct usb_ep *_ep,
1370 struct usb_request *_req, gfp_t gfp_flags)
1372 struct lpc32xx_request *req;
1373 struct lpc32xx_ep *ep;
1374 struct lpc32xx_udc *udc;
1375 unsigned long flags;
1378 req = container_of(_req, struct lpc32xx_request, req);
1379 ep = container_of(_ep, struct lpc32xx_ep, ep);
1381 if (!_req || !_req->complete || !_req->buf || !list_empty(&req->queue)) {
1387 if (!_ep || (!ep->desc && ep->hwep_num_base != 0)) {
1388 dev_dbg(udc->dev, "invalid ep\n");
1393 if ((!udc) || (!udc->driver) || (udc->gadget.speed == USB_SPEED_UNKNOWN)) {
1394 dev_dbg(udc->dev, "invalid device\n");
1398 #if defined(UDC_ENABLE_DMA)
1400 enum dma_data_direction direction;
1401 struct lpc32xx_usbd_dd_gad *dd;
1403 /* Map DMA pointer */
1405 direction = DMA_TO_DEVICE;
1407 direction = DMA_FROM_DEVICE;
1409 if (req->req.dma == 0) {
1410 req->req.dma = dma_map_single(
1411 ep->udc->gadget.dev.parent,
1412 req->req.buf, req->req.length, direction);
1416 dma_sync_single_for_device(
1417 ep->udc->gadget.dev.parent,
1418 req->req.dma, req->req.length, direction);
1422 /* For the request, build a list of DDs */
1423 dd = udc_dd_alloc(udc);
1425 /* Error allocating DD */
1428 req->dd_desc_ptr = dd;
1430 /* Setup the DMA descriptor */
1431 dd->dd_next_phy = dd->dd_next_v = NULL;
1432 dd->dd_buffer_addr = (u32) req->req.dma;
1435 /* Special handling for ISO EPs */
1436 if (ep->eptype == EP_ISO_TYPE) {
1437 dd->dd_setup = DD_SETUP_ISO_EP |
1438 DD_SETUP_PACKETLEN(ep->ep.maxpacket) |
1439 DD_SETUP_DMALENBYTES(req->req.length);
1440 dd->dd_iso_ps_mem_addr = (u32 *) ((u32) dd->this_dma + 20);
1441 dd->iso_status[0] = req->req.length;
1444 dd->dd_setup = DD_SETUP_PACKETLEN(ep->ep.maxpacket) |
1445 DD_SETUP_DMALENBYTES(req->req.length);
1450 ep_dbg(ep, "%s queue req %p len %d buf %p (in=%d) z=%d\n", _ep->name, _req, _req->length,
1451 _req->buf, ep->is_in, _req->zero);
1453 spin_lock_irqsave(&udc->lock, flags);
1455 _req->status = -EINPROGRESS;
1457 req->send_zlp = _req->zero;
1459 /* Kickstart empty queues */
1460 if (list_empty(&ep->queue)) {
1461 list_add_tail(&req->queue, &ep->queue);
1463 if (ep->hwep_num_base == 0) {
1464 /* Handle expected data direction */
1466 /* IN packet to host */
1467 udc->ep0state = DATA_IN;
1468 status = udc_ep0_in_req(udc);
1471 /* OUT packet from host */
1472 udc->ep0state = DATA_OUT;
1473 status = udc_ep0_out_req(udc);
1476 else if (ep->is_in) {
1477 /* IN packet to host and kick off transfer */
1478 if (!ep->req_pending) {
1479 #if defined(UDC_ENABLE_DMA)
1480 udc_ep_in_req_dma(udc, ep);
1482 uda_enable_hwepint(udc, ep->hwep_num);
1483 udc_ep_in_req(udc, ep);
1488 /* OUT packet from host and kick off list */
1489 if (!ep->req_pending) {
1490 #if defined(UDC_ENABLE_DMA)
1491 udc_ep_out_req_dma(udc, ep);
1493 uda_enable_hwepint(udc, ep->hwep_num);
1494 udc_ep_out_req(udc, ep);
1500 list_add_tail(&req->queue, &ep->queue);
1502 spin_unlock_irqrestore(&udc->lock, flags);
1504 return (status < 0) ? status : 0;
1507 static int lpc32xx_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1509 struct lpc32xx_ep *ep;
1510 struct lpc32xx_request *req;
1511 unsigned long flags;
1513 ep = container_of(_ep, struct lpc32xx_ep, ep);
1514 if (!_ep || ep->hwep_num_base == 0)
1517 spin_lock_irqsave(&ep->udc->lock, flags);
1519 /* make sure it's actually queued on this endpoint */
1520 list_for_each_entry (req, &ep->queue, queue) {
1521 if (&req->req == _req)
1524 if (&req->req != _req) {
1525 spin_unlock_irqrestore(&ep->udc->lock, flags);
1529 done(ep, req, -ECONNRESET);
1530 spin_unlock_irqrestore(&ep->udc->lock, flags);
1535 static int lpc32xx_ep_set_halt(struct usb_ep *_ep, int value)
1537 struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
1538 struct lpc32xx_udc *udc = ep->udc;
1539 unsigned long flags;
1541 if ((!ep) || (ep->desc == NULL) || (ep->hwep_num <= 1))
1544 /* Don't halt an IN EP */
1548 spin_lock_irqsave(&udc->lock, flags);
1552 udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(ep->hwep_num),
1553 DAT_WR_BYTE(EP_STAT_ST));
1557 udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(ep->hwep_num),
1561 spin_unlock_irqrestore(&udc->lock, flags);
1566 static const struct usb_ep_ops lpc32xx_ep_ops = {
1567 .enable = lpc32xx_ep_enable,
1568 .disable = lpc32xx_ep_disable,
1569 .alloc_request = lpc32xx_ep_alloc_request,
1570 .free_request = lpc32xx_ep_free_request,
1571 .queue = lpc32xx_ep_queue,
1572 .dequeue = lpc32xx_ep_dequeue,
1573 .set_halt = lpc32xx_ep_set_halt,
1576 #if defined(UDC_ENABLE_DMA)
1577 /* Setup up a IN request for DMA transfer - this consists of determining the
1578 list of DMA addresses for the transfer, allocating DMA Descriptors,
1579 installing the DD into the UDCA, and then enabling the DMA for that EP */
1580 static int udc_ep_in_req_dma(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
1582 struct lpc32xx_request *req;
1583 u32 hwep = ep->hwep_num;
1585 ep->req_pending = 1;
1587 /* There will always be a request waiting here */
1588 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
1590 /* Place the DD Descriptor into the UDCA */
1591 udc->udca_v_base[hwep] = (u32) req->dd_desc_ptr->this_dma;
1593 /* Enable DMA and interrupt for the HW EP */
1594 udc_ep_dma_enable(udc, hwep);
1599 /* Setup up a OUT request for DMA transfer - this consists of determining the
1600 list of DMA addresses for the transfer, allocating DMA Descriptors,
1601 installing the DD into the UDCA, and then enabling the DMA for that EP */
1602 static int udc_ep_out_req_dma(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
1604 struct lpc32xx_request *req;
1605 u32 hwep = ep->hwep_num;
1607 ep->req_pending = 1;
1609 /* There will always be a request waiting here */
1610 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
1612 /* Place the DD Descriptor into the UDCA */
1613 udc->udca_v_base[hwep] = (u32) req->dd_desc_ptr->this_dma;
1615 /* Enable DMA and interrupt for the HW EP */
1616 udc_ep_dma_enable(udc, hwep);
1621 /* This function is only called for IN ZLP transfer completions */
1622 void udc_handle_eps(struct lpc32xx_udc *udc, u32 epints) {
1623 int hwep = -1, i = 0;
1625 struct lpc32xx_ep *ep;
1626 struct lpc32xx_request *req;
1627 const int ineps[] = {2, 5, 8, 11, 15, -1};
1629 /* Find the IN EP that generated the interrupt */
1630 while (ineps[i] != 0) {
1631 if (epints & (1 << ineps[i]))
1636 uda_disable_hwepint(udc, hwep);
1641 epstatus = udc_clearep_getsts(udc, hwep);
1643 ep = &udc->ep[hwep];
1644 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
1647 /* Start another request if ready */
1648 if(!list_empty((&ep->queue))) {
1650 udc_ep_in_req_dma(udc, ep);
1653 udc_ep_out_req_dma(udc, ep);
1657 ep->req_pending = 0;
1660 /* Send a ZLP on a non-0 IN EP */
1661 void udc_send_in_zlp(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep,
1662 struct lpc32xx_usbd_dd_gad *dd) {
1663 /* Set up EP interrupt status */
1664 uda_enable_hwepint(udc, ep->hwep_num);
1665 udc_clearep_getsts(udc, ep->hwep_num);
1668 udc_write_hwep(udc, ep->hwep_num, NULL, 0);
1671 /* DMA end of transfer completion */
1672 static void udc_handle_dma_ep(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep) {
1674 struct lpc32xx_request *req;
1675 struct lpc32xx_usbd_dd_gad *dd;
1677 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1680 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
1682 ep_err(ep, "DMA interrupt on no req!\n");
1685 dd = req->dd_desc_ptr;
1687 /* Wait for end of descriptor to retire */
1688 while (!(dd->dd_status & DD_STATUS_DD_RETIRED));
1691 udc_ep_dma_disable(udc, ep->hwep_num);
1692 __raw_writel((1 << ep->hwep_num), USBD_EOTINTCLR(udc->udp_baseaddr));
1693 __raw_writel((1 << ep->hwep_num), USBD_NDDRTINTCLR(udc->udp_baseaddr));
1696 if (__raw_readl(USBD_SYSERRTINTST(udc->udp_baseaddr)) & (1 << ep->hwep_num)) {
1697 __raw_writel((1 << ep->hwep_num), USBD_SYSERRTINTCLR(udc->udp_baseaddr));
1698 ep_err(ep, "AHB critical error!\n");
1699 ep->req_pending = 0;
1701 /* The error could of occurred on a packet of a multipacket transfer,
1702 so recovering the transfer is not possible. Close the request with
1704 done(ep, req, -ECONNABORTED);
1709 /* Handle the current DD's status */
1710 status = dd->dd_status;
1711 switch (status & DD_STATUS_STS_MASK) {
1712 case DD_STATUS_STS_NS:
1713 /* DD not serviced? This shouldn't happen! */
1714 ep->req_pending = 0;
1715 ep_err(ep, "DMA critical EP error: DD not serviced (0x%x)!\n", status);
1716 done(ep, req, -ECONNABORTED);
1719 case DD_STATUS_STS_BS:
1720 /* Interrupt only fires on EOT - This shouldn't happen! */
1721 ep->req_pending = 0;
1722 ep_err(ep, "DMA critical EP error: EOT prior to service completion (0x%x)!\n", status);
1723 done(ep, req, -ECONNABORTED);
1726 case DD_STATUS_STS_NC:
1727 case DD_STATUS_STS_DUR: /* Really just a short packet, not an underrun */
1728 /* This is a good status and what we expect */
1732 /* Data overrun, system error, or unknown */
1733 ep->req_pending = 0;
1734 ep_err(ep, "DMA critical EP error: System error (0x%x)!\n", status);
1735 done(ep, req, -ECONNABORTED);
1739 /* Save transferred data size */
1740 req->req.actual += DD_STATUS_CURDMACNT(status);
1742 /* Work around the wierd underrun packet issue */
1743 if ((!ep->is_in) && (!(req->req.actual % ep->maxpacket)) &&
1744 ((req->req.length - req->req.actual) > 0)) {
1745 ep_dbg(ep, "Short packet in unexpected situation!\n");
1747 // WTF is this? An underrun packet on OUT with no ZLP! Hardware issue? */
1748 dd->dd_next_phy = dd->dd_next_v = NULL;
1749 dd->dd_buffer_addr = (u32) req->req.dma;
1750 dd->dd_buffer_addr += req->req.actual;
1753 /* Special handling for ISO EPs */
1754 dd->dd_setup = DD_SETUP_PACKETLEN(ep->ep.maxpacket) |
1755 DD_SETUP_DMALENBYTES(req->req.length - req->req.actual);
1757 /* Do the remainder of the req */
1758 udc_ep_out_req_dma(udc, ep);
1763 /* ISO endpoints are handled differently */
1764 if (ep->eptype == EP_ISO_TYPE) {
1766 req->req.actual = dd->iso_status[0] & 0xFFFF;
1769 /* For an Bulk IN EP, the DMA engine will only send data as specified in the
1770 descriptor. If the total transfer size is a multiple of the max packet
1771 size, then the transfer was completed, but no ZLP was sent. The ZLP needs
1772 to be sent using the FIFO mechanism to terminate this transfer */
1773 if (req->send_zlp) {
1774 udc_send_in_zlp(udc, ep, dd);
1780 /* Transfer request is complete */
1783 udc_clearep_getsts(udc, ep->hwep_num);
1784 /* Start another request if ready */
1785 if(!list_empty((&ep->queue))) {
1787 udc_ep_in_req_dma(udc, ep);
1790 udc_ep_out_req_dma(udc, ep);
1794 ep->req_pending = 0;
1798 /* This function was called if a new request is ready to be placed into the SEND FIFO
1799 for transfer to the host, or when a previous transfer to the host has completed. */
1800 static int udc_ep_in_req(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep) {
1801 struct lpc32xx_request *req;
1802 u32 ts, epstatus, bufst;
1805 /* Select and clear EP interrupt */
1806 epstatus = udc_clearep_getsts(udc, ep->hwep_num);
1808 if (epstatus & EP_SEL_ST) {
1814 if (epstatus & EP_SEL_EPN) {
1815 /* NAK'd on other side */
1819 bufst = (epstatus & EP_SEL_F);
1821 /* Are any requests available? */
1822 if (list_empty(&ep->queue)) {
1824 /* No reqs and the hardware is idle, disable IRQ */
1825 uda_disable_hwepint(udc, ep->hwep_num);
1831 /* If both buffers are currently full, just exit for now */
1835 /* A buffer is available in the hardware, so we can fill it */
1836 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
1838 /* Limit packet size to the size of the EP */
1839 ts = req->req.length - req->req.actual;
1840 if (ts > ep->ep.maxpacket)
1841 ts = ep->ep.maxpacket;
1843 /* Write data to the EP0 FIFO and start transfer */
1844 ep_dbg(ep, "SEND %s 0x%x(%d)\n", ep->ep.name, (u32)(req->req.buf + req->req.actual), ts);
1845 udc_write_hwep(udc, ep->hwep_num, (req->req.buf + req->req.actual), ts);
1847 /* Increment data pointer */
1848 req->req.actual += ts;
1850 if (ts < ep->ep.maxpacket)
1852 else if ((req->req.actual != req->req.length) || (req->send_zlp)) {
1860 /* Transfer request is complete */
1865 /* Stay in data transfer state */
1869 static int udc_ep_out_req(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep) {
1870 struct lpc32xx_request *req;
1871 u32 tr, bufferspace, epstatus;
1873 /* Clear EP interrupt */
1874 epstatus = udc_clearep_getsts(udc, ep->hwep_num);
1876 if (epstatus & EP_SEL_ST) {
1881 if (epstatus & EP_SEL_EPN) {
1886 /* Are any requests available? */
1887 if (list_empty(&ep->queue)) {
1888 uda_disable_hwepint(udc, ep->hwep_num);
1893 if (epstatus & EP_SEL_F) {
1894 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
1895 if (req->req.length == 0) {
1896 ep_dbg(ep, "%s OUT zero buffer length!\n", ep->ep.name);
1900 /* Limit transfer size to size of endpoint */
1901 bufferspace = req->req.length - req->req.actual;
1902 if (bufferspace > ep->ep.maxpacket)
1903 bufferspace = ep->ep.maxpacket;
1905 /* Copy data to buffer from FIFO */
1906 prefetchw(req->req.buf + req->req.actual);
1907 tr = udc_read_hwep(udc, ep->hwep_num,
1908 (req->req.buf + req->req.actual), bufferspace);
1910 ep_dbg(ep, "RECV %s 0x%x(%d) %d %d\n", ep->ep.name, (u32)(req->req.buf + req->req.actual), tr,
1911 req->req.actual, req->req.length);
1912 req->req.actual += tr;
1914 if ((tr < ep->ep.maxpacket) || (req->req.actual == req->req.length)) {
1915 /* This is the last packet */
1925 static void udc_handle_ep(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep) {
1929 uda_disable_hwepint(udc, ep->hwep_num);
1935 /* Handle IN request */
1936 udc_ep_in_req(udc, ep);
1939 /* Handle OUT request */
1940 udc_ep_out_req(udc, ep);
1947 * Endpoint 0 functions
1950 static void udc_handle_dev(struct lpc32xx_udc *udc) {
1953 udc_protocol_cmd_w(udc, CMD_GET_DEV_STAT);
1954 tmp = udc_protocol_cmd_r(udc, DAT_GET_DEV_STAT);
1956 if (tmp & DEV_RST) {
1959 else if (tmp & DEV_CON_CH) {
1960 uda_power_event(udc, (tmp & DEV_CON));
1962 else if (tmp & DEV_SUS_CH) {
1963 if (tmp & DEV_SUS) {
1964 if (udc->vbus == 0) {
1966 } else if ((udc->gadget.speed !=
1967 USB_SPEED_UNKNOWN) && udc->driver &&
1968 udc->driver->suspend) {
1969 udc->driver->suspend(&udc->gadget);
1970 uda_resm_susp_event(udc, 1);
1974 if ((udc->gadget.speed != USB_SPEED_UNKNOWN) &&
1975 udc->driver && udc->driver->resume &&
1977 udc->driver->resume(&udc->gadget);
1978 uda_resm_susp_event(udc, 0);
1984 /* IN endpoint 0 transfer */
1985 static int udc_ep0_in_req(struct lpc32xx_udc *udc) {
1986 struct lpc32xx_request *req;
1987 struct lpc32xx_ep *ep0 = &udc->ep [0];
1990 if (list_empty(&ep0->queue))
1992 /* Nothing to send */
1996 req = list_entry(ep0->queue.next,
1997 struct lpc32xx_request, queue);
2000 tsend = ts = req->req.length - req->req.actual;
2003 udc_ep0_send_zlp(udc);
2007 else if (ts > ep0->ep.maxpacket) {
2008 /* Just send what we can */
2009 ts = ep0->ep.maxpacket;
2012 /* Write data to the EP0 FIFO and start transfer */
2013 udc_write_hwep(udc, EP_IN, (req->req.buf + req->req.actual), ts);
2015 /* Increment data pointer */
2016 req->req.actual += ts;
2018 if (tsend >= ep0->ep.maxpacket) {
2019 /* Stay in data transfer state */
2023 /* Transfer request is complete */
2024 udc->ep0state = WAIT_FOR_SETUP;
2029 /* OUT endpoint 0 transfer */
2030 static int udc_ep0_out_req(struct lpc32xx_udc *udc) {
2031 struct lpc32xx_request *req;
2032 struct lpc32xx_ep *ep0 = &udc->ep[0];
2033 u32 tr, bufferspace;
2035 if (list_empty(&ep0->queue)) {
2039 req = list_entry(ep0->queue.next, struct lpc32xx_request, queue);
2043 if (req->req.length == 0) {
2044 /* Just dequeue request */
2046 udc->ep0state = WAIT_FOR_SETUP;
2050 /* Get data from FIFO */
2051 bufferspace = req->req.length - req->req.actual;
2052 if (bufferspace > ep0->ep.maxpacket) {
2053 bufferspace = ep0->ep.maxpacket;
2056 /* Copy data to buffer */
2057 prefetchw(req->req.buf + req->req.actual);
2058 tr = udc_read_hwep(udc, EP_OUT,
2059 (req->req.buf + req->req.actual), bufferspace);
2060 req->req.actual += bufferspace;
2062 if (tr < ep0->ep.maxpacket) {
2063 /* This is the last packet */
2065 udc->ep0state = WAIT_FOR_SETUP;
2073 static int udc_get_status(struct lpc32xx_udc *udc, u16 reqtype, u16 wIndex) {
2074 struct lpc32xx_ep *ep;
2075 u32 ep0buff = 0, tmp;
2078 case USB_RECIP_INTERFACE:
2082 case USB_RECIP_DEVICE:
2083 ep0buff = (udc->selfpowered << USB_DEVICE_SELF_POWERED);
2084 if (udc->dev_status & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
2085 ep0buff |= (1 << USB_DEVICE_REMOTE_WAKEUP);
2089 case USB_RECIP_ENDPOINT:
2090 tmp = wIndex & USB_ENDPOINT_NUMBER_MASK;
2092 if ((tmp == 0) || (tmp >= NUM_ENDPOINTS) || (tmp && !ep->desc)) {
2096 if (wIndex & USB_DIR_IN) {
2098 /* Somethings wrong */
2101 } else if (ep->is_in)
2102 /* Not an IN endpoint */
2105 /* Get status of the endpoint */
2106 udc_protocol_cmd_w(udc, CMD_SEL_EP(ep->hwep_num));
2107 tmp = udc_protocol_cmd_r(udc, DAT_SEL_EP(ep->hwep_num));
2109 if (tmp & EP_SEL_ST) {
2110 ep0buff = (1 << USB_ENDPOINT_HALT);
2122 udc_write_hwep(udc, EP_IN, &ep0buff, 2);
2127 static void udc_handle_ep0_setup(struct lpc32xx_udc *udc) {
2128 struct lpc32xx_ep *ep, *ep0 = &udc->ep[0];
2129 struct usb_ctrlrequest ctrlpkt;
2131 u16 wIndex, wValue, wLength, reqtype, req, tmp;
2133 /* Nuke previous transfers */
2136 /* Get setup packet */
2137 bytes = udc_read_hwep(udc, EP_OUT, (u32 *) &ctrlpkt, 8);
2139 ep_dbg(ep0, "Incorrectly sized setup packet (s/b 8, is %d!\n", bytes);
2143 /* Native endianness */
2144 wIndex = le16_to_cpu(ctrlpkt.wIndex);
2145 wValue = le16_to_cpu(ctrlpkt.wValue);
2146 wLength = le16_to_cpu(ctrlpkt.wLength);
2147 reqtype = le16_to_cpu(ctrlpkt.bRequestType);
2149 /* Set direction of EP0 */
2150 if (likely(reqtype & USB_DIR_IN)) {
2156 /* Handle SETUP packet */
2157 req = le16_to_cpu(ctrlpkt.bRequest);
2159 case USB_REQ_CLEAR_FEATURE:
2160 case USB_REQ_SET_FEATURE:
2162 case (USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2163 if (wValue != USB_DEVICE_REMOTE_WAKEUP) {
2164 /* Nothing else handled */
2168 /* Tell board about event */
2169 if (req == USB_REQ_CLEAR_FEATURE)
2170 udc->dev_status &= ~(1 << USB_DEVICE_REMOTE_WAKEUP);
2172 udc->dev_status |= (1 << USB_DEVICE_REMOTE_WAKEUP);
2173 uda_remwkp_cgh(udc);
2176 case (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2177 tmp = wIndex & USB_ENDPOINT_NUMBER_MASK;
2178 if ((wValue != USB_ENDPOINT_HALT) || (tmp >= NUM_ENDPOINTS))
2181 /* Find hardware endpoint from logical endpoint */
2187 if (req == USB_REQ_CLEAR_FEATURE)
2188 udc_stall_hwep(udc, tmp);
2190 udc_clrstall_hwep(udc, tmp);
2199 case USB_REQ_SET_ADDRESS:
2200 if (reqtype == (USB_TYPE_STANDARD | USB_RECIP_DEVICE)) {
2201 udc_set_address(udc, wValue);
2206 case USB_REQ_GET_STATUS:
2207 udc_get_status(udc, reqtype, wIndex);
2211 /* Let GadgetFs handle the descriptor instead */
2215 if (likely(udc->driver)) {
2216 /* device-2-host (IN) or no data setup command, process immediately */
2217 spin_unlock(&udc->lock);
2218 i = udc->driver->setup(&udc->gadget, &ctrlpkt);
2219 spin_lock(&udc->lock);
2220 if (req == USB_REQ_SET_CONFIGURATION) {
2221 /* Configuration is set after endpoints are realized */
2223 /* Set configuration */
2224 udc_set_device_configured(udc);
2226 /* NAK EP interrupts do not need to be enabled for this
2227 driver, but if you really want them for statistic
2228 purposes, uncomment the following lines */
2229 udc_protocol_cmd_data_w(udc, CMD_SET_MODE, DAT_WR_BYTE(AP_CLK |
2230 #if defined(UDC_ENABLE_DMA)
2231 INAK_BI | INAK_II));
2233 INAK_BO | INAK_BI | INAK_IO | INAK_II));
2237 /* Clear configuration */
2238 udc_set_device_unconfigured(udc);
2240 /* Disable NAK interrupts */
2241 udc_protocol_cmd_data_w(udc, CMD_SET_MODE, DAT_WR_BYTE(AP_CLK));
2246 /* setup processing failed, force stall */
2247 dev_err(udc->dev, "req %02x.%02x protocol STALL; stat %d\n",
2249 udc->ep0state = WAIT_FOR_SETUP;
2255 /* ZLP IN packet on on data phase */
2256 udc_ep0_send_zlp(udc);
2262 udc_stall_hwep(udc, EP_IN);
2266 udc_ep0_send_zlp(udc);
2270 /* IN endpoint 0 transfer */
2271 static void udc_handle_ep0_in(struct lpc32xx_udc *udc) {
2272 struct lpc32xx_ep *ep0 = &udc->ep [0];
2275 /* Clear EP interrupt */
2276 epstatus = udc_clearep_getsts(udc, EP_IN);
2278 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2282 /* Stalled? Clear stall and reset buffers */
2283 if (epstatus & EP_SEL_ST) {
2284 udc_clrstall_hwep(udc, EP_IN);
2285 nuke(ep0, -ECONNABORTED);
2286 udc->ep0state = WAIT_FOR_SETUP;
2290 /* Is a buffer available? */
2291 if (!(epstatus & EP_SEL_F)) {
2292 /* Handle based on current state */
2293 if (udc->ep0state == DATA_IN) {
2294 udc_ep0_in_req(udc);
2297 /* Unknown state for EP0 oe end of DATA IN phase */
2298 nuke(ep0, -ECONNABORTED);
2299 udc->ep0state = WAIT_FOR_SETUP;
2304 /* OUT endpoint 0 transfer */
2305 static void udc_handle_ep0_out(struct lpc32xx_udc *udc) {
2306 struct lpc32xx_ep *ep0 = &udc->ep[0];
2309 /* Clear EP interrupt */
2310 epstatus = udc_clearep_getsts(udc, EP_OUT);
2312 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2317 if (epstatus & EP_SEL_ST) {
2318 udc_clrstall_hwep(udc, EP_OUT);
2319 nuke(ep0, -ECONNABORTED);
2320 udc->ep0state = WAIT_FOR_SETUP;
2324 /* A NAK may occur if a packet coudn't be received yet */
2325 if (epstatus & EP_SEL_EPN) {
2328 /* Setup packet incoming? */
2329 if (epstatus & EP_SEL_STP) {
2331 udc->ep0state = WAIT_FOR_SETUP;
2334 /* Data available? */
2335 if (epstatus & EP_SEL_F) {
2336 /* Handle based on current state */
2337 switch (udc->ep0state) {
2338 case WAIT_FOR_SETUP:
2339 udc_handle_ep0_setup(udc);
2343 udc_ep0_out_req(udc);
2347 /* Unknown state for EP0 */
2348 nuke(ep0, -ECONNABORTED);
2349 udc->ep0state = WAIT_FOR_SETUP;
2354 static int lpc32xx_get_frame(struct usb_gadget *gadget)
2356 struct lpc32xx_udc *udc = to_udc(gadget);
2358 if (!to_udc(gadget)->clocked)
2361 return (int) udc_get_current_frame(udc);
2364 static int lpc32xx_wakeup(struct usb_gadget *gadget)
2369 static int lpc32xx_set_selfpowered(struct usb_gadget *gadget, int is_on)
2371 struct lpc32xx_udc *udc = to_udc(gadget);
2373 /* Always self-powered */
2374 udc->selfpowered = (is_on != 0);
2380 /* vbus is here! turn everything on that's ready */
2381 static int lpc32xx_vbus_session(struct usb_gadget *gadget, int is_active)
2383 struct lpc32xx_udc *udc = to_udc(gadget);
2385 /* Doesn't need lock */
2387 pullup(udc, is_active);
2394 static int lpc32xx_pullup(struct usb_gadget *gadget, int is_on)
2396 struct lpc32xx_udc *udc = to_udc(gadget);
2398 /* Doesn't need lock */
2404 static const struct usb_gadget_ops lpc32xx_udc_ops = {
2405 .get_frame = lpc32xx_get_frame,
2406 .wakeup = lpc32xx_wakeup,
2407 .set_selfpowered = lpc32xx_set_selfpowered,
2408 .vbus_session = lpc32xx_vbus_session,
2409 .pullup = lpc32xx_pullup,
2412 static void nop_release(struct device *dev)
2414 /* nothing to free */
2417 static struct lpc32xx_udc controller = {
2419 .ops = &lpc32xx_udc_ops,
2420 .ep0 = &controller.ep[0].ep,
2421 .name = driver_name,
2423 .init_name = "gadget",
2424 .release = nop_release,
2430 .ops = &lpc32xx_ep_ops,
2435 .hwep_num = 0, /* Can be 0 or 1, has special handling */
2437 .eptype = EP_CTL_TYPE,
2442 .ops = &lpc32xx_ep_ops,
2447 .hwep_num = 0, /* 2 or 3, will be set later */
2449 .eptype = EP_INT_TYPE,
2454 .ops = &lpc32xx_ep_ops,
2459 .hwep_num = 0, /* 4 or 5, will be set later */
2462 .eptype = EP_BLK_TYPE,
2467 .ops = &lpc32xx_ep_ops,
2472 .hwep_num = 0, /* 6 or 7, will be set later */
2475 .eptype = EP_ISO_TYPE,
2480 .ops = &lpc32xx_ep_ops,
2485 .hwep_num = 0, /* 8 or 9, will be set later */
2487 .eptype = EP_INT_TYPE,
2492 .ops = &lpc32xx_ep_ops,
2496 .hwep_num_base = 10,
2497 .hwep_num = 0, /* 10 or 11, will be set later */
2500 .eptype = EP_BLK_TYPE,
2505 .ops = &lpc32xx_ep_ops,
2509 .hwep_num_base = 12,
2510 .hwep_num = 0, /* 12 or 13, will be set later */
2513 .eptype = EP_ISO_TYPE,
2518 .ops = &lpc32xx_ep_ops,
2522 .hwep_num_base = 14,
2525 .eptype = EP_INT_TYPE,
2530 .ops = &lpc32xx_ep_ops,
2534 .hwep_num_base = 16,
2538 .eptype = EP_BLK_TYPE,
2543 .ops = &lpc32xx_ep_ops,
2547 .hwep_num_base = 18,
2551 .eptype = EP_ISO_TYPE,
2556 .ops = &lpc32xx_ep_ops,
2560 .hwep_num_base = 20,
2563 .eptype = EP_INT_TYPE,
2567 .name = "ep11-bulk",
2568 .ops = &lpc32xx_ep_ops,
2572 .hwep_num_base = 22,
2576 .eptype = EP_BLK_TYPE,
2581 .ops = &lpc32xx_ep_ops,
2585 .hwep_num_base = 24,
2589 .eptype = EP_ISO_TYPE,
2594 .ops = &lpc32xx_ep_ops,
2598 .hwep_num_base = 26,
2601 .eptype = EP_INT_TYPE,
2605 .name = "ep14-bulk",
2606 .ops = &lpc32xx_ep_ops,
2610 .hwep_num_base = 28,
2614 .eptype = EP_BLK_TYPE,
2618 .name = "ep15-bulk",
2619 .ops = &lpc32xx_ep_ops,
2623 .hwep_num_base = 30,
2627 .eptype = EP_BLK_TYPE,
2631 /* ISO and status interrupts */
2632 static irqreturn_t lpc32xx_usb_lp_irq(int irq, void *_udc) {
2634 struct lpc32xx_udc *udc = _udc;
2636 spin_lock(&udc->lock);
2638 /* Read the device status register */
2639 devstat = __raw_readl(USBD_DEVINTST(udc->udp_baseaddr));
2640 devstat &= ~USBD_EP_FAST;
2641 __raw_writel(devstat, USBD_DEVINTCLR(udc->udp_baseaddr));
2642 devstat = devstat & udc->enabled_devints;
2644 /* Device specific handling needed? */
2645 if (devstat & USBD_DEV_STAT) {
2646 udc_handle_dev(udc);
2649 /* Start of frame? */
2650 if (devstat & FRAME_INT) {
2651 /* The frame interrupt isn't really needed for ISO support,
2652 as the driver will queue the necessary packets */
2653 dev_dbg(udc->dev, "Device frame interrupt not supported\n");
2657 if (devstat & ERR_INT) {
2658 /* All types of errors, from cable removal during transfer to
2659 misc protocol and bit errors. These are mostly for just info,
2660 as the USB hardware will work around these */
2661 udc_protocol_cmd_w(udc, CMD_RD_ERR_STAT);
2662 tmp = udc_protocol_cmd_r(udc, DAT_RD_ERR_STAT);
2663 dev_err(udc->dev, "Device error (0x%x)!\n", tmp);
2666 spin_unlock(&udc->lock);
2671 static irqreturn_t lpc32xx_usb_hp_irq(int irq, void *_udc)
2674 struct lpc32xx_udc *udc = _udc;
2676 spin_lock(&udc->lock);
2678 /* Read the device status register */
2679 tmp = __raw_readl(USBD_DEVINTST(udc->udp_baseaddr));
2680 __raw_writel(USBD_EP_FAST, USBD_DEVINTCLR(udc->udp_baseaddr));
2683 tmp = __raw_readl(USBD_EPINTST(udc->udp_baseaddr));
2685 /* Special handling for EP0 */
2686 if (tmp & (EP_MASK_SEL(0, EP_OUT) | EP_MASK_SEL(0, EP_IN))) {
2688 if (tmp & (EP_MASK_SEL(0, EP_IN)))
2689 udc_handle_ep0_in(udc);
2691 /* Handle EP0 OUT */
2692 if (tmp & (EP_MASK_SEL(0, EP_OUT)))
2693 udc_handle_ep0_out(udc);
2697 if (tmp & ~(EP_MASK_SEL(0, EP_OUT) | EP_MASK_SEL(0, EP_IN))) {
2698 #if defined(UDC_ENABLE_DMA)
2699 udc_handle_eps(udc, tmp);
2704 /* Handle other EP interrupts */
2705 for (i = 1; i < NUM_ENDPOINTS; i++) {
2706 if (tmp & (1 << udc->ep [i].hwep_num))
2707 udc_handle_ep(udc, &udc->ep[i]);
2712 spin_unlock(&udc->lock);
2716 static irqreturn_t lpc32xx_usb_devdma_irq(int irq, void *_udc)
2718 struct lpc32xx_udc *udc = _udc;
2720 #if defined(UDC_ENABLE_DMA)
2724 spin_lock(&udc->lock);
2726 /* Handle EP DMA EOT interrupts */
2727 tmp = __raw_readl(USBD_EOTINTST(udc->udp_baseaddr)) |
2728 __raw_readl(USBD_NDDRTINTST(udc->udp_baseaddr)) |
2729 __raw_readl(USBD_SYSERRTINTST(udc->udp_baseaddr));
2730 for (i = 1; i < NUM_ENDPOINTS; i++) {
2731 if (tmp & (1 << udc->ep [i].hwep_num))
2732 udc_handle_dma_ep(udc, &udc->ep[i]);
2735 spin_unlock(&udc->lock);
2743 * VBUS detection, pullup handler, and Gadget cable state notification
2746 static int vbus_handler_thread(void *udc_)
2748 struct lpc32xx_udc *udc = udc_;
2752 while (!kthread_should_stop()) {
2753 if (udc->enabled != 0) {
2754 /* Get the VBUS status from the transceiver */
2755 value = i2c_read(ISP1301_I2C_OTG_CONTROL_2);
2757 /* VBUS on or off? */
2758 if (value & OTG_B_SESS_VLD) {
2761 /* Enable USB clocks */
2762 udc_clk_set(udc, 1);
2764 /* Setup the UDC and ep0 */
2768 /* Will force disconnect */
2773 if (udc->last_vbus != udc->vbus) {
2774 lpc32xx_vbus_session(&udc->gadget, udc->vbus);
2775 udc->last_vbus = udc->vbus;
2779 /* sleep if nothing to send */
2780 set_current_state(TASK_INTERRUPTIBLE);
2784 udc->thread_task = NULL;
2789 static irqreturn_t lpc32xx_usb_vbus_irq(int irq, void *_udc)
2791 struct lpc32xx_udc *udc = _udc;
2793 /* Discharge VBUS real quick */
2794 i2c_write(OTG1_VBUS_DISCHRG, ISP1301_I2C_OTG_CONTROL_1);
2796 /* Give VBUS some time (100mS) to discharge */
2799 /* Disable VBUS discharge resistor */
2800 i2c_write(OTG1_VBUS_DISCHRG,
2801 (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR));
2803 /* Clear interrupt */
2804 i2c_write(~0, ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR);
2806 /* Kick off the VBUS handler thread */
2807 udc->thread_wakeup_needed = 1;
2808 wake_up_process(udc->thread_task);
2813 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
2815 struct lpc32xx_udc *udc = &controller;
2818 if (!driver || driver->speed < USB_SPEED_FULL ||
2819 !driver->bind || !driver->setup) {
2820 dev_err(udc->dev, "bad parameter.\n");
2825 dev_err(udc->dev, "UDC already has a gadget driver\n");
2829 udc->driver = driver;
2830 udc->gadget.dev.driver = &driver->driver;
2832 udc->selfpowered = 1;
2835 retval = driver->bind(&udc->gadget);
2837 dev_err(udc->dev, "driver->bind() returned %d\n", retval);
2839 udc->selfpowered = 0;
2841 udc->gadget.dev.driver = NULL;
2845 dev_dbg(udc->dev, "bound to %s\n", driver->driver.name);
2847 /* Force VBUS process once to check for cable insertion */
2848 udc->last_vbus = udc->vbus = 0;
2849 wake_up_process(udc->thread_task);
2850 enable_irq(udc->udp_irq[IRQ_USB_ATX]);
2854 EXPORT_SYMBOL (usb_gadget_register_driver);
2856 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
2858 struct lpc32xx_udc *udc = &controller;
2860 if (!driver || driver != udc->driver || !driver->unbind)
2863 disable_irq(udc->udp_irq[IRQ_USB_ATX]);
2864 local_irq_disable();
2869 driver->unbind(&udc->gadget);
2870 udc->gadget.dev.driver = NULL;
2873 dev_dbg(udc->dev, "unbound from %s\n", driver->driver.name);
2876 EXPORT_SYMBOL (usb_gadget_unregister_driver);
2878 /*-------------------------------------------------------------------------*/
2880 static void lpc32xx_udc_shutdown(struct platform_device *dev)
2882 /* Force disconnect on reboot */
2883 struct lpc32xx_udc *udc = &controller;
2888 static int __init lpc32xx_udc_probe(struct platform_device *pdev)
2890 struct device *dev = &pdev->dev;
2891 struct lpc32xx_udc *udc = &controller;
2893 struct resource *res;
2894 dma_addr_t dma_handle;
2895 struct i2c_adapter *i2c_adap;
2896 struct i2c_board_info i2c_info;
2898 /* init software state */
2899 udc->gadget.dev.parent = dev;
2901 udc->dev = &pdev->dev;
2904 if (!dev->platform_data) {
2905 dev_err(udc->dev, "missing platform_data\n");
2909 udc->board = (struct lpc32xx_usbd_cfg *) dev->platform_data;
2912 * Resources are mapped as follows:
2913 * [0] = IORESOURCE_MEM, base address and size of USB space
2914 * [1] = IORESOURCE_IRQ, USB device low priority interrupt number
2915 * [2] = IORESOURCE_IRQ, USB device high priority interrupt number
2916 * [3] = IORESOURCE_IRQ, USB device interrupt number
2917 * [4] = IORESOURCE_IRQ, USB transciever interrupt number
2919 if (pdev->num_resources != 5) {
2920 dev_err(udc->dev, "invalid num_resources\n");
2924 if (pdev->resource[0].flags != IORESOURCE_MEM) {
2925 dev_err(udc->dev, "invalid resource type\n");
2929 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2933 spin_lock_init(&udc->lock);
2936 for (i = 0; i < 4; i++) {
2937 if (pdev->resource[i + 1].flags != IORESOURCE_IRQ) {
2938 dev_err(udc->dev, "invalid resource type\n");
2941 udc->udp_irq[i] = platform_get_irq(pdev, i);
2944 udc->io_p_start = res->start;
2945 udc->io_p_size = res->end - res->start + 1;
2946 if (!request_mem_region(udc->io_p_start, udc->io_p_size, driver_name)) {
2947 dev_err(udc->dev, "someone's using UDC memory\n");
2951 /* Enable AHB slave USB clock, needed for further USB clock control */
2952 __raw_writel(USB_SLAVE_HCLK_EN | (1 << 19), USB_CTRL);
2954 /* Get required clocks */
2955 udc->usb_pll_clk = clk_get(&pdev->dev, "ck_pll5");
2956 if (IS_ERR(udc->usb_pll_clk)) {
2957 dev_err(udc->dev, "failed to acquire USB PLL");
2958 retval = PTR_ERR(udc->usb_pll_clk);
2961 udc->usb_slv_clk = clk_get(&pdev->dev, "ck_usbd");
2962 if (IS_ERR(udc->usb_slv_clk)) {
2963 dev_err(udc->dev, "failed to acquire USB device clock");
2964 retval = PTR_ERR(udc->usb_slv_clk);
2965 goto usb_clk_get_fail;
2968 /* Setup PLL clock to 48MHz */
2969 retval = clk_enable(udc->usb_pll_clk);
2971 dev_err(udc->dev, "failed to start USB PLL");
2972 goto pll_enable_fail;
2975 retval = clk_set_rate(udc->usb_pll_clk, 48000);
2977 dev_err(udc->dev, "failed to set USB clock rate");
2981 __raw_writel(__raw_readl(USB_CTRL) | USB_DEV_NEED_CLK_EN, USB_CTRL);
2983 /* Enable USB device clock */
2984 retval = clk_enable(udc->usb_slv_clk);
2986 dev_err(udc->dev, "failed to start USB device clock");
2987 goto usb_clk_enable_fail;
2990 /* Set to enable all needed USB OTG clocks */
2991 __raw_writel(USB_CLOCK_MASK, USB_OTG_CLK_CTRL);
2993 /* This is unsafe */
2994 while ((__raw_readl(USB_OTG_CLK_STAT) & USB_CLOCK_MASK) !=
2997 /* All clocks are now on */
3000 retval = i2c_add_driver(&isp1301_driver);
3002 dev_err(udc->dev, "Failed to add ISP1301 driver \n");
3005 i2c_adap = i2c_get_adapter(2);
3006 memset(&i2c_info, 0, sizeof(struct i2c_board_info));
3007 isp1301_i2c_client = i2c_new_probed_device(i2c_adap, &i2c_info,
3009 i2c_put_adapter(i2c_adap);
3010 if (!isp1301_i2c_client) {
3011 dev_err(udc->dev,"failed to connect I2C to ISP1301 USB Transceiver");
3012 goto i2c_probe_fail;
3015 dev_info(udc->dev, "I2C device at address 0x%x", isp1301_i2c_client->addr);
3017 isp1301_udc_configure(udc);
3018 /* Map register space */
3019 udc->udp_baseaddr = ioremap(udc->io_p_start, udc->io_p_size);
3020 if (!udc->udp_baseaddr) {
3022 dev_err(udc->dev, "IO map failure");
3026 /* Allocate memory for the UDCA */
3027 udc->udca_v_base = dma_alloc_coherent(&pdev->dev, UDCA_BUFF_SIZE,
3028 &dma_handle, (GFP_KERNEL | GFP_DMA));
3029 if (!udc->udca_v_base)
3031 dev_err(udc->dev, "error getting UDCA region");
3033 goto dma_alloc_fail;
3035 udc->udca_p_base = (void *) dma_handle;
3036 dev_dbg(udc->dev, "DMA buffer(0x%x bytes), P:0x%08x, V:0x%08x",
3037 UDCA_BUFF_SIZE, (u32) udc->udca_p_base, (u32) udc->udca_v_base);
3039 /* Setup the DD DMA memory pool */
3040 udc->dd_cache = dma_pool_create ("udc_dd", udc->dev,
3041 sizeof (struct lpc32xx_usbd_dd_gad), sizeof (u32), 0);
3042 if (!udc->dd_cache) {
3043 dev_err(udc->dev, "error getting DD DMA region");
3045 goto dma2_alloc_fail;
3048 /* Clear USB peripheral and initialize gadget endpoints */
3052 retval = device_register(&udc->gadget.dev);
3054 dev_err(udc->dev, "Device registration failure");
3055 goto dev_register_fail;
3058 /* Request IRQs - low and high priority USB device IRQs are routed to the
3059 same handler, while the DMA interrupt is routed elsewhere */
3060 retval = request_irq(udc->udp_irq[IRQ_USB_LP], lpc32xx_usb_lp_irq,
3063 dev_err(udc->dev, "LP request irq %d failed", udc->udp_irq[IRQ_USB_LP]);
3066 retval = request_irq(udc->udp_irq[IRQ_USB_HP], lpc32xx_usb_hp_irq,
3069 dev_err(udc->dev, "HP request irq %d failed", udc->udp_irq[IRQ_USB_HP]);
3073 retval = request_irq(udc->udp_irq[IRQ_USB_DEVDMA], lpc32xx_usb_devdma_irq,
3076 dev_err(udc->dev, "DEV request irq %d failed", udc->udp_irq[IRQ_USB_DEVDMA]);
3080 /* Create VBUS handler thread */
3081 udc->thread_wakeup_needed = 0;
3082 udc->thread_task = kthread_create(vbus_handler_thread, udc,
3083 "vbus_handler_thread");
3084 if (IS_ERR(udc->thread_task)) {
3085 retval = PTR_ERR(udc->thread_task);
3086 dev_err(udc->dev, "VBUS handler thread failures");
3087 goto vbus_thread_fail;
3090 /* The transceiver interrupt is used for VBUS detection and will
3091 kick off the VBUS handler thread */
3092 retval = request_threaded_irq(udc->udp_irq[IRQ_USB_ATX], NULL, lpc32xx_usb_vbus_irq,
3093 IRQF_ONESHOT, "udc_otg", udc);
3095 dev_err(udc->dev, "VBUS request irq %d failed\n", udc->udp_irq[IRQ_USB_ATX]);
3099 /* Initialize wait queue */
3100 init_waitqueue_head(&udc->ep_disable_wait_queue);
3101 atomic_set(&udc->enabled_ep_cnt,0);
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);
3134 i2c_unregister_device(isp1301_i2c_client);
3135 isp1301_i2c_client = NULL;
3137 i2c_del_driver(&isp1301_driver);
3139 clk_disable(udc->usb_slv_clk);
3140 usb_clk_enable_fail:
3142 clk_disable(udc->usb_pll_clk);
3144 clk_put(udc->usb_slv_clk);
3146 clk_put(udc->usb_pll_clk);
3148 release_mem_region(udc->io_p_start, udc->io_p_size);
3149 dev_err(udc->dev, "%s probe failed, %d\n", driver_name, retval);
3154 static int __exit lpc32xx_udc_remove(struct platform_device *pdev)
3156 struct lpc32xx_udc *udc = platform_get_drvdata(pdev);
3161 udc_clk_set(udc, 1);
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_unregister_device(isp1301_i2c_client);
3186 isp1301_i2c_client = NULL;
3187 i2c_del_driver(&isp1301_driver);
3188 release_mem_region(udc->io_p_start, udc->io_p_size);
3194 static int lpc32xx_udc_suspend(struct platform_device *pdev, pm_message_t mesg)
3196 struct lpc32xx_udc *udc = platform_get_drvdata(pdev);
3199 /* Power down ISP */
3200 isp1301_set_powerstate(0);
3202 /* Disable clocking */
3203 udc_clk_set(udc, 0);
3205 /* Keep clock flag on, so we know to re-enable clocks
3209 /* Kill OTG and I2C clocks */
3210 __raw_writel(0, USB_OTG_CLK_CTRL);
3211 while ((__raw_readl(USB_OTG_CLK_STAT) & OTGOFF_CLK_MASK) !=
3214 /* Kill global USB clock */
3215 clk_disable(udc->usb_slv_clk);