mm: Initialize error in shmem_file_aio_read()
[linux/fpc-iii.git] / drivers / usb / gadget / lpc32xx_udc.c
blobe471580a2a3bee941ca5b4a251c618215b242105
1 /*
2 * USB Gadget driver for LPC32xx
4 * Authors:
5 * Kevin Wells <kevin.wells@nxp.com>
6 * Mike James
7 * Roland Stigge <stigge@antcom.de>
9 * Copyright (C) 2006 Philips Semiconductors
10 * Copyright (C) 2009 NXP Semiconductors
11 * Copyright (C) 2012 Roland Stigge
13 * Note: This driver is based on original work done by Mike James for
14 * the LPC3180.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/platform_device.h>
34 #include <linux/delay.h>
35 #include <linux/ioport.h>
36 #include <linux/slab.h>
37 #include <linux/errno.h>
38 #include <linux/init.h>
39 #include <linux/list.h>
40 #include <linux/interrupt.h>
41 #include <linux/proc_fs.h>
42 #include <linux/clk.h>
43 #include <linux/usb/ch9.h>
44 #include <linux/usb/gadget.h>
45 #include <linux/i2c.h>
46 #include <linux/kthread.h>
47 #include <linux/freezer.h>
48 #include <linux/dma-mapping.h>
49 #include <linux/dmapool.h>
50 #include <linux/workqueue.h>
51 #include <linux/of.h>
52 #include <linux/usb/isp1301.h>
54 #include <asm/byteorder.h>
55 #include <mach/hardware.h>
56 #include <linux/io.h>
57 #include <asm/irq.h>
59 #include <mach/platform.h>
60 #include <mach/irqs.h>
61 #include <mach/board.h>
62 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
63 #include <linux/debugfs.h>
64 #include <linux/seq_file.h>
65 #endif
68 * USB device configuration structure
70 typedef void (*usc_chg_event)(int);
71 struct lpc32xx_usbd_cfg {
72 int vbus_drv_pol; /* 0=active low drive for VBUS via ISP1301 */
73 usc_chg_event conn_chgb; /* Connection change event (optional) */
74 usc_chg_event susp_chgb; /* Suspend/resume event (optional) */
75 usc_chg_event rmwk_chgb; /* Enable/disable remote wakeup */
79 * controller driver data structures
82 /* 16 endpoints (not to be confused with 32 hardware endpoints) */
83 #define NUM_ENDPOINTS 16
86 * IRQ indices make reading the code a little easier
88 #define IRQ_USB_LP 0
89 #define IRQ_USB_HP 1
90 #define IRQ_USB_DEVDMA 2
91 #define IRQ_USB_ATX 3
93 #define EP_OUT 0 /* RX (from host) */
94 #define EP_IN 1 /* TX (to host) */
96 /* Returns the interrupt mask for the selected hardware endpoint */
97 #define EP_MASK_SEL(ep, dir) (1 << (((ep) * 2) + dir))
99 #define EP_INT_TYPE 0
100 #define EP_ISO_TYPE 1
101 #define EP_BLK_TYPE 2
102 #define EP_CTL_TYPE 3
104 /* EP0 states */
105 #define WAIT_FOR_SETUP 0 /* Wait for setup packet */
106 #define DATA_IN 1 /* Expect dev->host transfer */
107 #define DATA_OUT 2 /* Expect host->dev transfer */
109 /* DD (DMA Descriptor) structure, requires word alignment, this is already
110 * defined in the LPC32XX USB device header file, but this version is slightly
111 * modified to tag some work data with each DMA descriptor. */
112 struct lpc32xx_usbd_dd_gad {
113 u32 dd_next_phy;
114 u32 dd_setup;
115 u32 dd_buffer_addr;
116 u32 dd_status;
117 u32 dd_iso_ps_mem_addr;
118 u32 this_dma;
119 u32 iso_status[6]; /* 5 spare */
120 u32 dd_next_v;
124 * Logical endpoint structure
126 struct lpc32xx_ep {
127 struct usb_ep ep;
128 struct list_head queue;
129 struct lpc32xx_udc *udc;
131 u32 hwep_num_base; /* Physical hardware EP */
132 u32 hwep_num; /* Maps to hardware endpoint */
133 u32 maxpacket;
134 u32 lep;
136 bool is_in;
137 bool req_pending;
138 u32 eptype;
140 u32 totalints;
142 bool wedge;
146 * Common UDC structure
148 struct lpc32xx_udc {
149 struct usb_gadget gadget;
150 struct usb_gadget_driver *driver;
151 struct platform_device *pdev;
152 struct device *dev;
153 struct dentry *pde;
154 spinlock_t lock;
155 struct i2c_client *isp1301_i2c_client;
157 /* Board and device specific */
158 struct lpc32xx_usbd_cfg *board;
159 u32 io_p_start;
160 u32 io_p_size;
161 void __iomem *udp_baseaddr;
162 int udp_irq[4];
163 struct clk *usb_pll_clk;
164 struct clk *usb_slv_clk;
165 struct clk *usb_otg_clk;
167 /* DMA support */
168 u32 *udca_v_base;
169 u32 udca_p_base;
170 struct dma_pool *dd_cache;
172 /* Common EP and control data */
173 u32 enabled_devints;
174 u32 enabled_hwepints;
175 u32 dev_status;
176 u32 realized_eps;
178 /* VBUS detection, pullup, and power flags */
179 u8 vbus;
180 u8 last_vbus;
181 int pullup;
182 int poweron;
184 /* Work queues related to I2C support */
185 struct work_struct pullup_job;
186 struct work_struct vbus_job;
187 struct work_struct power_job;
189 /* USB device peripheral - various */
190 struct lpc32xx_ep ep[NUM_ENDPOINTS];
191 bool enabled;
192 bool clocked;
193 bool suspended;
194 bool selfpowered;
195 int ep0state;
196 atomic_t enabled_ep_cnt;
197 wait_queue_head_t ep_disable_wait_queue;
201 * Endpoint request
203 struct lpc32xx_request {
204 struct usb_request req;
205 struct list_head queue;
206 struct lpc32xx_usbd_dd_gad *dd_desc_ptr;
207 bool mapped;
208 bool send_zlp;
211 static inline struct lpc32xx_udc *to_udc(struct usb_gadget *g)
213 return container_of(g, struct lpc32xx_udc, gadget);
216 #define ep_dbg(epp, fmt, arg...) \
217 dev_dbg(epp->udc->dev, "%s: " fmt, __func__, ## arg)
218 #define ep_err(epp, fmt, arg...) \
219 dev_err(epp->udc->dev, "%s: " fmt, __func__, ## arg)
220 #define ep_info(epp, fmt, arg...) \
221 dev_info(epp->udc->dev, "%s: " fmt, __func__, ## arg)
222 #define ep_warn(epp, fmt, arg...) \
223 dev_warn(epp->udc->dev, "%s:" fmt, __func__, ## arg)
225 #define UDCA_BUFF_SIZE (128)
227 /* TODO: When the clock framework is introduced in LPC32xx, IO_ADDRESS will
228 * be replaced with an inremap()ed pointer
229 * */
230 #define USB_CTRL IO_ADDRESS(LPC32XX_CLK_PM_BASE + 0x64)
232 /* USB_CTRL bit defines */
233 #define USB_SLAVE_HCLK_EN (1 << 24)
234 #define USB_HOST_NEED_CLK_EN (1 << 21)
235 #define USB_DEV_NEED_CLK_EN (1 << 22)
237 /**********************************************************************
238 * USB device controller register offsets
239 **********************************************************************/
241 #define USBD_DEVINTST(x) ((x) + 0x200)
242 #define USBD_DEVINTEN(x) ((x) + 0x204)
243 #define USBD_DEVINTCLR(x) ((x) + 0x208)
244 #define USBD_DEVINTSET(x) ((x) + 0x20C)
245 #define USBD_CMDCODE(x) ((x) + 0x210)
246 #define USBD_CMDDATA(x) ((x) + 0x214)
247 #define USBD_RXDATA(x) ((x) + 0x218)
248 #define USBD_TXDATA(x) ((x) + 0x21C)
249 #define USBD_RXPLEN(x) ((x) + 0x220)
250 #define USBD_TXPLEN(x) ((x) + 0x224)
251 #define USBD_CTRL(x) ((x) + 0x228)
252 #define USBD_DEVINTPRI(x) ((x) + 0x22C)
253 #define USBD_EPINTST(x) ((x) + 0x230)
254 #define USBD_EPINTEN(x) ((x) + 0x234)
255 #define USBD_EPINTCLR(x) ((x) + 0x238)
256 #define USBD_EPINTSET(x) ((x) + 0x23C)
257 #define USBD_EPINTPRI(x) ((x) + 0x240)
258 #define USBD_REEP(x) ((x) + 0x244)
259 #define USBD_EPIND(x) ((x) + 0x248)
260 #define USBD_EPMAXPSIZE(x) ((x) + 0x24C)
261 /* DMA support registers only below */
262 /* Set, clear, or get enabled state of the DMA request status. If
263 * enabled, an IN or OUT token will start a DMA transfer for the EP */
264 #define USBD_DMARST(x) ((x) + 0x250)
265 #define USBD_DMARCLR(x) ((x) + 0x254)
266 #define USBD_DMARSET(x) ((x) + 0x258)
267 /* DMA UDCA head pointer */
268 #define USBD_UDCAH(x) ((x) + 0x280)
269 /* EP DMA status, enable, and disable. This is used to specifically
270 * enabled or disable DMA for a specific EP */
271 #define USBD_EPDMAST(x) ((x) + 0x284)
272 #define USBD_EPDMAEN(x) ((x) + 0x288)
273 #define USBD_EPDMADIS(x) ((x) + 0x28C)
274 /* DMA master interrupts enable and pending interrupts */
275 #define USBD_DMAINTST(x) ((x) + 0x290)
276 #define USBD_DMAINTEN(x) ((x) + 0x294)
277 /* DMA end of transfer interrupt enable, disable, status */
278 #define USBD_EOTINTST(x) ((x) + 0x2A0)
279 #define USBD_EOTINTCLR(x) ((x) + 0x2A4)
280 #define USBD_EOTINTSET(x) ((x) + 0x2A8)
281 /* New DD request interrupt enable, disable, status */
282 #define USBD_NDDRTINTST(x) ((x) + 0x2AC)
283 #define USBD_NDDRTINTCLR(x) ((x) + 0x2B0)
284 #define USBD_NDDRTINTSET(x) ((x) + 0x2B4)
285 /* DMA error interrupt enable, disable, status */
286 #define USBD_SYSERRTINTST(x) ((x) + 0x2B8)
287 #define USBD_SYSERRTINTCLR(x) ((x) + 0x2BC)
288 #define USBD_SYSERRTINTSET(x) ((x) + 0x2C0)
290 /**********************************************************************
291 * USBD_DEVINTST/USBD_DEVINTEN/USBD_DEVINTCLR/USBD_DEVINTSET/
292 * USBD_DEVINTPRI register definitions
293 **********************************************************************/
294 #define USBD_ERR_INT (1 << 9)
295 #define USBD_EP_RLZED (1 << 8)
296 #define USBD_TXENDPKT (1 << 7)
297 #define USBD_RXENDPKT (1 << 6)
298 #define USBD_CDFULL (1 << 5)
299 #define USBD_CCEMPTY (1 << 4)
300 #define USBD_DEV_STAT (1 << 3)
301 #define USBD_EP_SLOW (1 << 2)
302 #define USBD_EP_FAST (1 << 1)
303 #define USBD_FRAME (1 << 0)
305 /**********************************************************************
306 * USBD_EPINTST/USBD_EPINTEN/USBD_EPINTCLR/USBD_EPINTSET/
307 * USBD_EPINTPRI register definitions
308 **********************************************************************/
309 /* End point selection macro (RX) */
310 #define USBD_RX_EP_SEL(e) (1 << ((e) << 1))
312 /* End point selection macro (TX) */
313 #define USBD_TX_EP_SEL(e) (1 << (((e) << 1) + 1))
315 /**********************************************************************
316 * USBD_REEP/USBD_DMARST/USBD_DMARCLR/USBD_DMARSET/USBD_EPDMAST/
317 * USBD_EPDMAEN/USBD_EPDMADIS/
318 * USBD_NDDRTINTST/USBD_NDDRTINTCLR/USBD_NDDRTINTSET/
319 * USBD_EOTINTST/USBD_EOTINTCLR/USBD_EOTINTSET/
320 * USBD_SYSERRTINTST/USBD_SYSERRTINTCLR/USBD_SYSERRTINTSET
321 * register definitions
322 **********************************************************************/
323 /* Endpoint selection macro */
324 #define USBD_EP_SEL(e) (1 << (e))
326 /**********************************************************************
327 * SBD_DMAINTST/USBD_DMAINTEN
328 **********************************************************************/
329 #define USBD_SYS_ERR_INT (1 << 2)
330 #define USBD_NEW_DD_INT (1 << 1)
331 #define USBD_EOT_INT (1 << 0)
333 /**********************************************************************
334 * USBD_RXPLEN register definitions
335 **********************************************************************/
336 #define USBD_PKT_RDY (1 << 11)
337 #define USBD_DV (1 << 10)
338 #define USBD_PK_LEN_MASK 0x3FF
340 /**********************************************************************
341 * USBD_CTRL register definitions
342 **********************************************************************/
343 #define USBD_LOG_ENDPOINT(e) ((e) << 2)
344 #define USBD_WR_EN (1 << 1)
345 #define USBD_RD_EN (1 << 0)
347 /**********************************************************************
348 * USBD_CMDCODE register definitions
349 **********************************************************************/
350 #define USBD_CMD_CODE(c) ((c) << 16)
351 #define USBD_CMD_PHASE(p) ((p) << 8)
353 /**********************************************************************
354 * USBD_DMARST/USBD_DMARCLR/USBD_DMARSET register definitions
355 **********************************************************************/
356 #define USBD_DMAEP(e) (1 << (e))
358 /* DD (DMA Descriptor) structure, requires word alignment */
359 struct lpc32xx_usbd_dd {
360 u32 *dd_next;
361 u32 dd_setup;
362 u32 dd_buffer_addr;
363 u32 dd_status;
364 u32 dd_iso_ps_mem_addr;
367 /* dd_setup bit defines */
368 #define DD_SETUP_ATLE_DMA_MODE 0x01
369 #define DD_SETUP_NEXT_DD_VALID 0x04
370 #define DD_SETUP_ISO_EP 0x10
371 #define DD_SETUP_PACKETLEN(n) (((n) & 0x7FF) << 5)
372 #define DD_SETUP_DMALENBYTES(n) (((n) & 0xFFFF) << 16)
374 /* dd_status bit defines */
375 #define DD_STATUS_DD_RETIRED 0x01
376 #define DD_STATUS_STS_MASK 0x1E
377 #define DD_STATUS_STS_NS 0x00 /* Not serviced */
378 #define DD_STATUS_STS_BS 0x02 /* Being serviced */
379 #define DD_STATUS_STS_NC 0x04 /* Normal completion */
380 #define DD_STATUS_STS_DUR 0x06 /* Data underrun (short packet) */
381 #define DD_STATUS_STS_DOR 0x08 /* Data overrun */
382 #define DD_STATUS_STS_SE 0x12 /* System error */
383 #define DD_STATUS_PKT_VAL 0x20 /* Packet valid */
384 #define DD_STATUS_LSB_EX 0x40 /* LS byte extracted (ATLE) */
385 #define DD_STATUS_MSB_EX 0x80 /* MS byte extracted (ATLE) */
386 #define DD_STATUS_MLEN(n) (((n) >> 8) & 0x3F)
387 #define DD_STATUS_CURDMACNT(n) (((n) >> 16) & 0xFFFF)
391 * Protocol engine bits below
394 /* Device Interrupt Bit Definitions */
395 #define FRAME_INT 0x00000001
396 #define EP_FAST_INT 0x00000002
397 #define EP_SLOW_INT 0x00000004
398 #define DEV_STAT_INT 0x00000008
399 #define CCEMTY_INT 0x00000010
400 #define CDFULL_INT 0x00000020
401 #define RxENDPKT_INT 0x00000040
402 #define TxENDPKT_INT 0x00000080
403 #define EP_RLZED_INT 0x00000100
404 #define ERR_INT 0x00000200
406 /* Rx & Tx Packet Length Definitions */
407 #define PKT_LNGTH_MASK 0x000003FF
408 #define PKT_DV 0x00000400
409 #define PKT_RDY 0x00000800
411 /* USB Control Definitions */
412 #define CTRL_RD_EN 0x00000001
413 #define CTRL_WR_EN 0x00000002
415 /* Command Codes */
416 #define CMD_SET_ADDR 0x00D00500
417 #define CMD_CFG_DEV 0x00D80500
418 #define CMD_SET_MODE 0x00F30500
419 #define CMD_RD_FRAME 0x00F50500
420 #define DAT_RD_FRAME 0x00F50200
421 #define CMD_RD_TEST 0x00FD0500
422 #define DAT_RD_TEST 0x00FD0200
423 #define CMD_SET_DEV_STAT 0x00FE0500
424 #define CMD_GET_DEV_STAT 0x00FE0500
425 #define DAT_GET_DEV_STAT 0x00FE0200
426 #define CMD_GET_ERR_CODE 0x00FF0500
427 #define DAT_GET_ERR_CODE 0x00FF0200
428 #define CMD_RD_ERR_STAT 0x00FB0500
429 #define DAT_RD_ERR_STAT 0x00FB0200
430 #define DAT_WR_BYTE(x) (0x00000100 | ((x) << 16))
431 #define CMD_SEL_EP(x) (0x00000500 | ((x) << 16))
432 #define DAT_SEL_EP(x) (0x00000200 | ((x) << 16))
433 #define CMD_SEL_EP_CLRI(x) (0x00400500 | ((x) << 16))
434 #define DAT_SEL_EP_CLRI(x) (0x00400200 | ((x) << 16))
435 #define CMD_SET_EP_STAT(x) (0x00400500 | ((x) << 16))
436 #define CMD_CLR_BUF 0x00F20500
437 #define DAT_CLR_BUF 0x00F20200
438 #define CMD_VALID_BUF 0x00FA0500
440 /* Device Address Register Definitions */
441 #define DEV_ADDR_MASK 0x7F
442 #define DEV_EN 0x80
444 /* Device Configure Register Definitions */
445 #define CONF_DVICE 0x01
447 /* Device Mode Register Definitions */
448 #define AP_CLK 0x01
449 #define INAK_CI 0x02
450 #define INAK_CO 0x04
451 #define INAK_II 0x08
452 #define INAK_IO 0x10
453 #define INAK_BI 0x20
454 #define INAK_BO 0x40
456 /* Device Status Register Definitions */
457 #define DEV_CON 0x01
458 #define DEV_CON_CH 0x02
459 #define DEV_SUS 0x04
460 #define DEV_SUS_CH 0x08
461 #define DEV_RST 0x10
463 /* Error Code Register Definitions */
464 #define ERR_EC_MASK 0x0F
465 #define ERR_EA 0x10
467 /* Error Status Register Definitions */
468 #define ERR_PID 0x01
469 #define ERR_UEPKT 0x02
470 #define ERR_DCRC 0x04
471 #define ERR_TIMOUT 0x08
472 #define ERR_EOP 0x10
473 #define ERR_B_OVRN 0x20
474 #define ERR_BTSTF 0x40
475 #define ERR_TGL 0x80
477 /* Endpoint Select Register Definitions */
478 #define EP_SEL_F 0x01
479 #define EP_SEL_ST 0x02
480 #define EP_SEL_STP 0x04
481 #define EP_SEL_PO 0x08
482 #define EP_SEL_EPN 0x10
483 #define EP_SEL_B_1_FULL 0x20
484 #define EP_SEL_B_2_FULL 0x40
486 /* Endpoint Status Register Definitions */
487 #define EP_STAT_ST 0x01
488 #define EP_STAT_DA 0x20
489 #define EP_STAT_RF_MO 0x40
490 #define EP_STAT_CND_ST 0x80
492 /* Clear Buffer Register Definitions */
493 #define CLR_BUF_PO 0x01
495 /* DMA Interrupt Bit Definitions */
496 #define EOT_INT 0x01
497 #define NDD_REQ_INT 0x02
498 #define SYS_ERR_INT 0x04
500 #define DRIVER_VERSION "1.03"
501 static const char driver_name[] = "lpc32xx_udc";
505 * proc interface support
508 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
509 static char *epnames[] = {"INT", "ISO", "BULK", "CTRL"};
510 static const char debug_filename[] = "driver/udc";
512 static void proc_ep_show(struct seq_file *s, struct lpc32xx_ep *ep)
514 struct lpc32xx_request *req;
516 seq_printf(s, "\n");
517 seq_printf(s, "%12s, maxpacket %4d %3s",
518 ep->ep.name, ep->ep.maxpacket,
519 ep->is_in ? "in" : "out");
520 seq_printf(s, " type %4s", epnames[ep->eptype]);
521 seq_printf(s, " ints: %12d", ep->totalints);
523 if (list_empty(&ep->queue))
524 seq_printf(s, "\t(queue empty)\n");
525 else {
526 list_for_each_entry(req, &ep->queue, queue) {
527 u32 length = req->req.actual;
529 seq_printf(s, "\treq %p len %d/%d buf %p\n",
530 &req->req, length,
531 req->req.length, req->req.buf);
536 static int proc_udc_show(struct seq_file *s, void *unused)
538 struct lpc32xx_udc *udc = s->private;
539 struct lpc32xx_ep *ep;
540 unsigned long flags;
542 seq_printf(s, "%s: version %s\n", driver_name, DRIVER_VERSION);
544 spin_lock_irqsave(&udc->lock, flags);
546 seq_printf(s, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
547 udc->vbus ? "present" : "off",
548 udc->enabled ? (udc->vbus ? "active" : "enabled") :
549 "disabled",
550 udc->selfpowered ? "self" : "VBUS",
551 udc->suspended ? ", suspended" : "",
552 udc->driver ? udc->driver->driver.name : "(none)");
554 if (udc->enabled && udc->vbus) {
555 proc_ep_show(s, &udc->ep[0]);
556 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list)
557 proc_ep_show(s, ep);
560 spin_unlock_irqrestore(&udc->lock, flags);
562 return 0;
565 static int proc_udc_open(struct inode *inode, struct file *file)
567 return single_open(file, proc_udc_show, PDE_DATA(inode));
570 static const struct file_operations proc_ops = {
571 .owner = THIS_MODULE,
572 .open = proc_udc_open,
573 .read = seq_read,
574 .llseek = seq_lseek,
575 .release = single_release,
578 static void create_debug_file(struct lpc32xx_udc *udc)
580 udc->pde = debugfs_create_file(debug_filename, 0, NULL, udc, &proc_ops);
583 static void remove_debug_file(struct lpc32xx_udc *udc)
585 if (udc->pde)
586 debugfs_remove(udc->pde);
589 #else
590 static inline void create_debug_file(struct lpc32xx_udc *udc) {}
591 static inline void remove_debug_file(struct lpc32xx_udc *udc) {}
592 #endif
594 /* Primary initialization sequence for the ISP1301 transceiver */
595 static void isp1301_udc_configure(struct lpc32xx_udc *udc)
597 /* LPC32XX only supports DAT_SE0 USB mode */
598 /* This sequence is important */
600 /* Disable transparent UART mode first */
601 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
602 (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
603 MC1_UART_EN);
605 /* Set full speed and SE0 mode */
606 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
607 (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), ~0);
608 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
609 ISP1301_I2C_MODE_CONTROL_1, (MC1_SPEED_REG | MC1_DAT_SE0));
612 * The PSW_OE enable bit state is reversed in the ISP1301 User's Guide
614 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
615 (ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR), ~0);
616 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
617 ISP1301_I2C_MODE_CONTROL_2, (MC2_BI_DI | MC2_SPD_SUSP_CTRL));
619 /* Driver VBUS_DRV high or low depending on board setup */
620 if (udc->board->vbus_drv_pol != 0)
621 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
622 ISP1301_I2C_OTG_CONTROL_1, OTG1_VBUS_DRV);
623 else
624 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
625 ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
626 OTG1_VBUS_DRV);
628 /* Bi-directional mode with suspend control
629 * Enable both pulldowns for now - the pullup will be enable when VBUS
630 * is detected */
631 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
632 (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), ~0);
633 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
634 ISP1301_I2C_OTG_CONTROL_1,
635 (0 | OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
637 /* Discharge VBUS (just in case) */
638 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
639 ISP1301_I2C_OTG_CONTROL_1, OTG1_VBUS_DISCHRG);
640 msleep(1);
641 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
642 (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
643 OTG1_VBUS_DISCHRG);
645 /* Clear and enable VBUS high edge interrupt */
646 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
647 ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
648 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
649 ISP1301_I2C_INTERRUPT_FALLING | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
650 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
651 ISP1301_I2C_INTERRUPT_FALLING, INT_VBUS_VLD);
652 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
653 ISP1301_I2C_INTERRUPT_RISING | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
654 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
655 ISP1301_I2C_INTERRUPT_RISING, INT_VBUS_VLD);
657 /* Enable usb_need_clk clock after transceiver is initialized */
658 writel((readl(USB_CTRL) | USB_DEV_NEED_CLK_EN), USB_CTRL);
660 dev_info(udc->dev, "ISP1301 Vendor ID : 0x%04x\n",
661 i2c_smbus_read_word_data(udc->isp1301_i2c_client, 0x00));
662 dev_info(udc->dev, "ISP1301 Product ID : 0x%04x\n",
663 i2c_smbus_read_word_data(udc->isp1301_i2c_client, 0x02));
664 dev_info(udc->dev, "ISP1301 Version ID : 0x%04x\n",
665 i2c_smbus_read_word_data(udc->isp1301_i2c_client, 0x14));
668 /* Enables or disables the USB device pullup via the ISP1301 transceiver */
669 static void isp1301_pullup_set(struct lpc32xx_udc *udc)
671 if (udc->pullup)
672 /* Enable pullup for bus signalling */
673 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
674 ISP1301_I2C_OTG_CONTROL_1, OTG1_DP_PULLUP);
675 else
676 /* Enable pullup for bus signalling */
677 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
678 ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
679 OTG1_DP_PULLUP);
682 static void pullup_work(struct work_struct *work)
684 struct lpc32xx_udc *udc =
685 container_of(work, struct lpc32xx_udc, pullup_job);
687 isp1301_pullup_set(udc);
690 static void isp1301_pullup_enable(struct lpc32xx_udc *udc, int en_pullup,
691 int block)
693 if (en_pullup == udc->pullup)
694 return;
696 udc->pullup = en_pullup;
697 if (block)
698 isp1301_pullup_set(udc);
699 else
700 /* defer slow i2c pull up setting */
701 schedule_work(&udc->pullup_job);
704 #ifdef CONFIG_PM
705 /* Powers up or down the ISP1301 transceiver */
706 static void isp1301_set_powerstate(struct lpc32xx_udc *udc, int enable)
708 if (enable != 0)
709 /* Power up ISP1301 - this ISP1301 will automatically wakeup
710 when VBUS is detected */
711 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
712 ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR,
713 MC2_GLOBAL_PWR_DN);
714 else
715 /* Power down ISP1301 */
716 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
717 ISP1301_I2C_MODE_CONTROL_2, MC2_GLOBAL_PWR_DN);
720 static void power_work(struct work_struct *work)
722 struct lpc32xx_udc *udc =
723 container_of(work, struct lpc32xx_udc, power_job);
725 isp1301_set_powerstate(udc, udc->poweron);
727 #endif
731 * USB protocol engine command/data read/write helper functions
734 /* Issues a single command to the USB device state machine */
735 static void udc_protocol_cmd_w(struct lpc32xx_udc *udc, u32 cmd)
737 u32 pass = 0;
738 int to;
740 /* EP may lock on CLRI if this read isn't done */
741 u32 tmp = readl(USBD_DEVINTST(udc->udp_baseaddr));
742 (void) tmp;
744 while (pass == 0) {
745 writel(USBD_CCEMPTY, USBD_DEVINTCLR(udc->udp_baseaddr));
747 /* Write command code */
748 writel(cmd, USBD_CMDCODE(udc->udp_baseaddr));
749 to = 10000;
750 while (((readl(USBD_DEVINTST(udc->udp_baseaddr)) &
751 USBD_CCEMPTY) == 0) && (to > 0)) {
752 to--;
755 if (to > 0)
756 pass = 1;
758 cpu_relax();
762 /* Issues 2 commands (or command and data) to the USB device state machine */
763 static inline void udc_protocol_cmd_data_w(struct lpc32xx_udc *udc, u32 cmd,
764 u32 data)
766 udc_protocol_cmd_w(udc, cmd);
767 udc_protocol_cmd_w(udc, data);
770 /* Issues a single command to the USB device state machine and reads
771 * response data */
772 static u32 udc_protocol_cmd_r(struct lpc32xx_udc *udc, u32 cmd)
774 u32 tmp;
775 int to = 1000;
777 /* Write a command and read data from the protocol engine */
778 writel((USBD_CDFULL | USBD_CCEMPTY),
779 USBD_DEVINTCLR(udc->udp_baseaddr));
781 /* Write command code */
782 udc_protocol_cmd_w(udc, cmd);
784 tmp = readl(USBD_DEVINTST(udc->udp_baseaddr));
785 while ((!(readl(USBD_DEVINTST(udc->udp_baseaddr)) & USBD_CDFULL))
786 && (to > 0))
787 to--;
788 if (!to)
789 dev_dbg(udc->dev,
790 "Protocol engine didn't receive response (CDFULL)\n");
792 return readl(USBD_CMDDATA(udc->udp_baseaddr));
797 * USB device interrupt mask support functions
800 /* Enable one or more USB device interrupts */
801 static inline void uda_enable_devint(struct lpc32xx_udc *udc, u32 devmask)
803 udc->enabled_devints |= devmask;
804 writel(udc->enabled_devints, USBD_DEVINTEN(udc->udp_baseaddr));
807 /* Disable one or more USB device interrupts */
808 static inline void uda_disable_devint(struct lpc32xx_udc *udc, u32 mask)
810 udc->enabled_devints &= ~mask;
811 writel(udc->enabled_devints, USBD_DEVINTEN(udc->udp_baseaddr));
814 /* Clear one or more USB device interrupts */
815 static inline void uda_clear_devint(struct lpc32xx_udc *udc, u32 mask)
817 writel(mask, USBD_DEVINTCLR(udc->udp_baseaddr));
822 * Endpoint interrupt disable/enable functions
825 /* Enable one or more USB endpoint interrupts */
826 static void uda_enable_hwepint(struct lpc32xx_udc *udc, u32 hwep)
828 udc->enabled_hwepints |= (1 << hwep);
829 writel(udc->enabled_hwepints, USBD_EPINTEN(udc->udp_baseaddr));
832 /* Disable one or more USB endpoint interrupts */
833 static void uda_disable_hwepint(struct lpc32xx_udc *udc, u32 hwep)
835 udc->enabled_hwepints &= ~(1 << hwep);
836 writel(udc->enabled_hwepints, USBD_EPINTEN(udc->udp_baseaddr));
839 /* Clear one or more USB endpoint interrupts */
840 static inline void uda_clear_hwepint(struct lpc32xx_udc *udc, u32 hwep)
842 writel((1 << hwep), USBD_EPINTCLR(udc->udp_baseaddr));
845 /* Enable DMA for the HW channel */
846 static inline void udc_ep_dma_enable(struct lpc32xx_udc *udc, u32 hwep)
848 writel((1 << hwep), USBD_EPDMAEN(udc->udp_baseaddr));
851 /* Disable DMA for the HW channel */
852 static inline void udc_ep_dma_disable(struct lpc32xx_udc *udc, u32 hwep)
854 writel((1 << hwep), USBD_EPDMADIS(udc->udp_baseaddr));
859 * Endpoint realize/unrealize functions
862 /* Before an endpoint can be used, it needs to be realized
863 * in the USB protocol engine - this realizes the endpoint.
864 * The interrupt (FIFO or DMA) is not enabled with this function */
865 static void udc_realize_hwep(struct lpc32xx_udc *udc, u32 hwep,
866 u32 maxpacket)
868 int to = 1000;
870 writel(USBD_EP_RLZED, USBD_DEVINTCLR(udc->udp_baseaddr));
871 writel(hwep, USBD_EPIND(udc->udp_baseaddr));
872 udc->realized_eps |= (1 << hwep);
873 writel(udc->realized_eps, USBD_REEP(udc->udp_baseaddr));
874 writel(maxpacket, USBD_EPMAXPSIZE(udc->udp_baseaddr));
876 /* Wait until endpoint is realized in hardware */
877 while ((!(readl(USBD_DEVINTST(udc->udp_baseaddr)) &
878 USBD_EP_RLZED)) && (to > 0))
879 to--;
880 if (!to)
881 dev_dbg(udc->dev, "EP not correctly realized in hardware\n");
883 writel(USBD_EP_RLZED, USBD_DEVINTCLR(udc->udp_baseaddr));
886 /* Unrealize an EP */
887 static void udc_unrealize_hwep(struct lpc32xx_udc *udc, u32 hwep)
889 udc->realized_eps &= ~(1 << hwep);
890 writel(udc->realized_eps, USBD_REEP(udc->udp_baseaddr));
895 * Endpoint support functions
898 /* Select and clear endpoint interrupt */
899 static u32 udc_selep_clrint(struct lpc32xx_udc *udc, u32 hwep)
901 udc_protocol_cmd_w(udc, CMD_SEL_EP_CLRI(hwep));
902 return udc_protocol_cmd_r(udc, DAT_SEL_EP_CLRI(hwep));
905 /* Disables the endpoint in the USB protocol engine */
906 static void udc_disable_hwep(struct lpc32xx_udc *udc, u32 hwep)
908 udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(hwep),
909 DAT_WR_BYTE(EP_STAT_DA));
912 /* Stalls the endpoint - endpoint will return STALL */
913 static void udc_stall_hwep(struct lpc32xx_udc *udc, u32 hwep)
915 udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(hwep),
916 DAT_WR_BYTE(EP_STAT_ST));
919 /* Clear stall or reset endpoint */
920 static void udc_clrstall_hwep(struct lpc32xx_udc *udc, u32 hwep)
922 udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(hwep),
923 DAT_WR_BYTE(0));
926 /* Select an endpoint for endpoint status, clear, validate */
927 static void udc_select_hwep(struct lpc32xx_udc *udc, u32 hwep)
929 udc_protocol_cmd_w(udc, CMD_SEL_EP(hwep));
934 * Endpoint buffer management functions
937 /* Clear the current endpoint's buffer */
938 static void udc_clr_buffer_hwep(struct lpc32xx_udc *udc, u32 hwep)
940 udc_select_hwep(udc, hwep);
941 udc_protocol_cmd_w(udc, CMD_CLR_BUF);
944 /* Validate the current endpoint's buffer */
945 static void udc_val_buffer_hwep(struct lpc32xx_udc *udc, u32 hwep)
947 udc_select_hwep(udc, hwep);
948 udc_protocol_cmd_w(udc, CMD_VALID_BUF);
951 static inline u32 udc_clearep_getsts(struct lpc32xx_udc *udc, u32 hwep)
953 /* Clear EP interrupt */
954 uda_clear_hwepint(udc, hwep);
955 return udc_selep_clrint(udc, hwep);
960 * USB EP DMA support
963 /* Allocate a DMA Descriptor */
964 static struct lpc32xx_usbd_dd_gad *udc_dd_alloc(struct lpc32xx_udc *udc)
966 dma_addr_t dma;
967 struct lpc32xx_usbd_dd_gad *dd;
969 dd = (struct lpc32xx_usbd_dd_gad *) dma_pool_alloc(
970 udc->dd_cache, (GFP_KERNEL | GFP_DMA), &dma);
971 if (dd)
972 dd->this_dma = dma;
974 return dd;
977 /* Free a DMA Descriptor */
978 static void udc_dd_free(struct lpc32xx_udc *udc, struct lpc32xx_usbd_dd_gad *dd)
980 dma_pool_free(udc->dd_cache, dd, dd->this_dma);
985 * USB setup and shutdown functions
988 /* Enables or disables most of the USB system clocks when low power mode is
989 * needed. Clocks are typically started on a connection event, and disabled
990 * when a cable is disconnected */
991 static void udc_clk_set(struct lpc32xx_udc *udc, int enable)
993 if (enable != 0) {
994 if (udc->clocked)
995 return;
997 udc->clocked = 1;
999 /* 48MHz PLL up */
1000 clk_enable(udc->usb_pll_clk);
1002 /* Enable the USB device clock */
1003 writel(readl(USB_CTRL) | USB_DEV_NEED_CLK_EN,
1004 USB_CTRL);
1006 clk_enable(udc->usb_otg_clk);
1007 } else {
1008 if (!udc->clocked)
1009 return;
1011 udc->clocked = 0;
1013 /* Never disable the USB_HCLK during normal operation */
1015 /* 48MHz PLL dpwn */
1016 clk_disable(udc->usb_pll_clk);
1018 /* Disable the USB device clock */
1019 writel(readl(USB_CTRL) & ~USB_DEV_NEED_CLK_EN,
1020 USB_CTRL);
1022 clk_disable(udc->usb_otg_clk);
1026 /* Set/reset USB device address */
1027 static void udc_set_address(struct lpc32xx_udc *udc, u32 addr)
1029 /* Address will be latched at the end of the status phase, or
1030 latched immediately if function is called twice */
1031 udc_protocol_cmd_data_w(udc, CMD_SET_ADDR,
1032 DAT_WR_BYTE(DEV_EN | addr));
1035 /* Setup up a IN request for DMA transfer - this consists of determining the
1036 * list of DMA addresses for the transfer, allocating DMA Descriptors,
1037 * installing the DD into the UDCA, and then enabling the DMA for that EP */
1038 static int udc_ep_in_req_dma(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
1040 struct lpc32xx_request *req;
1041 u32 hwep = ep->hwep_num;
1043 ep->req_pending = 1;
1045 /* There will always be a request waiting here */
1046 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
1048 /* Place the DD Descriptor into the UDCA */
1049 udc->udca_v_base[hwep] = req->dd_desc_ptr->this_dma;
1051 /* Enable DMA and interrupt for the HW EP */
1052 udc_ep_dma_enable(udc, hwep);
1054 /* Clear ZLP if last packet is not of MAXP size */
1055 if (req->req.length % ep->ep.maxpacket)
1056 req->send_zlp = 0;
1058 return 0;
1061 /* Setup up a OUT request for DMA transfer - this consists of determining the
1062 * list of DMA addresses for the transfer, allocating DMA Descriptors,
1063 * installing the DD into the UDCA, and then enabling the DMA for that EP */
1064 static int udc_ep_out_req_dma(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
1066 struct lpc32xx_request *req;
1067 u32 hwep = ep->hwep_num;
1069 ep->req_pending = 1;
1071 /* There will always be a request waiting here */
1072 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
1074 /* Place the DD Descriptor into the UDCA */
1075 udc->udca_v_base[hwep] = req->dd_desc_ptr->this_dma;
1077 /* Enable DMA and interrupt for the HW EP */
1078 udc_ep_dma_enable(udc, hwep);
1079 return 0;
1082 static void udc_disable(struct lpc32xx_udc *udc)
1084 u32 i;
1086 /* Disable device */
1087 udc_protocol_cmd_data_w(udc, CMD_CFG_DEV, DAT_WR_BYTE(0));
1088 udc_protocol_cmd_data_w(udc, CMD_SET_DEV_STAT, DAT_WR_BYTE(0));
1090 /* Disable all device interrupts (including EP0) */
1091 uda_disable_devint(udc, 0x3FF);
1093 /* Disable and reset all endpoint interrupts */
1094 for (i = 0; i < 32; i++) {
1095 uda_disable_hwepint(udc, i);
1096 uda_clear_hwepint(udc, i);
1097 udc_disable_hwep(udc, i);
1098 udc_unrealize_hwep(udc, i);
1099 udc->udca_v_base[i] = 0;
1101 /* Disable and clear all interrupts and DMA */
1102 udc_ep_dma_disable(udc, i);
1103 writel((1 << i), USBD_EOTINTCLR(udc->udp_baseaddr));
1104 writel((1 << i), USBD_NDDRTINTCLR(udc->udp_baseaddr));
1105 writel((1 << i), USBD_SYSERRTINTCLR(udc->udp_baseaddr));
1106 writel((1 << i), USBD_DMARCLR(udc->udp_baseaddr));
1109 /* Disable DMA interrupts */
1110 writel(0, USBD_DMAINTEN(udc->udp_baseaddr));
1112 writel(0, USBD_UDCAH(udc->udp_baseaddr));
1115 static void udc_enable(struct lpc32xx_udc *udc)
1117 u32 i;
1118 struct lpc32xx_ep *ep = &udc->ep[0];
1120 /* Start with known state */
1121 udc_disable(udc);
1123 /* Enable device */
1124 udc_protocol_cmd_data_w(udc, CMD_SET_DEV_STAT, DAT_WR_BYTE(DEV_CON));
1126 /* EP interrupts on high priority, FRAME interrupt on low priority */
1127 writel(USBD_EP_FAST, USBD_DEVINTPRI(udc->udp_baseaddr));
1128 writel(0xFFFF, USBD_EPINTPRI(udc->udp_baseaddr));
1130 /* Clear any pending device interrupts */
1131 writel(0x3FF, USBD_DEVINTCLR(udc->udp_baseaddr));
1133 /* Setup UDCA - not yet used (DMA) */
1134 writel(udc->udca_p_base, USBD_UDCAH(udc->udp_baseaddr));
1136 /* Only enable EP0 in and out for now, EP0 only works in FIFO mode */
1137 for (i = 0; i <= 1; i++) {
1138 udc_realize_hwep(udc, i, ep->ep.maxpacket);
1139 uda_enable_hwepint(udc, i);
1140 udc_select_hwep(udc, i);
1141 udc_clrstall_hwep(udc, i);
1142 udc_clr_buffer_hwep(udc, i);
1145 /* Device interrupt setup */
1146 uda_clear_devint(udc, (USBD_ERR_INT | USBD_DEV_STAT | USBD_EP_SLOW |
1147 USBD_EP_FAST));
1148 uda_enable_devint(udc, (USBD_ERR_INT | USBD_DEV_STAT | USBD_EP_SLOW |
1149 USBD_EP_FAST));
1151 /* Set device address to 0 - called twice to force a latch in the USB
1152 engine without the need of a setup packet status closure */
1153 udc_set_address(udc, 0);
1154 udc_set_address(udc, 0);
1156 /* Enable master DMA interrupts */
1157 writel((USBD_SYS_ERR_INT | USBD_EOT_INT),
1158 USBD_DMAINTEN(udc->udp_baseaddr));
1160 udc->dev_status = 0;
1165 * USB device board specific events handled via callbacks
1168 /* Connection change event - notify board function of change */
1169 static void uda_power_event(struct lpc32xx_udc *udc, u32 conn)
1171 /* Just notify of a connection change event (optional) */
1172 if (udc->board->conn_chgb != NULL)
1173 udc->board->conn_chgb(conn);
1176 /* Suspend/resume event - notify board function of change */
1177 static void uda_resm_susp_event(struct lpc32xx_udc *udc, u32 conn)
1179 /* Just notify of a Suspend/resume change event (optional) */
1180 if (udc->board->susp_chgb != NULL)
1181 udc->board->susp_chgb(conn);
1183 if (conn)
1184 udc->suspended = 0;
1185 else
1186 udc->suspended = 1;
1189 /* Remote wakeup enable/disable - notify board function of change */
1190 static void uda_remwkp_cgh(struct lpc32xx_udc *udc)
1192 if (udc->board->rmwk_chgb != NULL)
1193 udc->board->rmwk_chgb(udc->dev_status &
1194 (1 << USB_DEVICE_REMOTE_WAKEUP));
1197 /* Reads data from FIFO, adjusts for alignment and data size */
1198 static void udc_pop_fifo(struct lpc32xx_udc *udc, u8 *data, u32 bytes)
1200 int n, i, bl;
1201 u16 *p16;
1202 u32 *p32, tmp, cbytes;
1204 /* Use optimal data transfer method based on source address and size */
1205 switch (((u32) data) & 0x3) {
1206 case 0: /* 32-bit aligned */
1207 p32 = (u32 *) data;
1208 cbytes = (bytes & ~0x3);
1210 /* Copy 32-bit aligned data first */
1211 for (n = 0; n < cbytes; n += 4)
1212 *p32++ = readl(USBD_RXDATA(udc->udp_baseaddr));
1214 /* Handle any remaining bytes */
1215 bl = bytes - cbytes;
1216 if (bl) {
1217 tmp = readl(USBD_RXDATA(udc->udp_baseaddr));
1218 for (n = 0; n < bl; n++)
1219 data[cbytes + n] = ((tmp >> (n * 8)) & 0xFF);
1222 break;
1224 case 1: /* 8-bit aligned */
1225 case 3:
1226 /* Each byte has to be handled independently */
1227 for (n = 0; n < bytes; n += 4) {
1228 tmp = readl(USBD_RXDATA(udc->udp_baseaddr));
1230 bl = bytes - n;
1231 if (bl > 3)
1232 bl = 3;
1234 for (i = 0; i < bl; i++)
1235 data[n + i] = (u8) ((tmp >> (n * 8)) & 0xFF);
1237 break;
1239 case 2: /* 16-bit aligned */
1240 p16 = (u16 *) data;
1241 cbytes = (bytes & ~0x3);
1243 /* Copy 32-bit sized objects first with 16-bit alignment */
1244 for (n = 0; n < cbytes; n += 4) {
1245 tmp = readl(USBD_RXDATA(udc->udp_baseaddr));
1246 *p16++ = (u16)(tmp & 0xFFFF);
1247 *p16++ = (u16)((tmp >> 16) & 0xFFFF);
1250 /* Handle any remaining bytes */
1251 bl = bytes - cbytes;
1252 if (bl) {
1253 tmp = readl(USBD_RXDATA(udc->udp_baseaddr));
1254 for (n = 0; n < bl; n++)
1255 data[cbytes + n] = ((tmp >> (n * 8)) & 0xFF);
1257 break;
1261 /* Read data from the FIFO for an endpoint. This function is for endpoints (such
1262 * as EP0) that don't use DMA. This function should only be called if a packet
1263 * is known to be ready to read for the endpoint. Note that the endpoint must
1264 * be selected in the protocol engine prior to this call. */
1265 static u32 udc_read_hwep(struct lpc32xx_udc *udc, u32 hwep, u32 *data,
1266 u32 bytes)
1268 u32 tmpv;
1269 int to = 1000;
1270 u32 tmp, hwrep = ((hwep & 0x1E) << 1) | CTRL_RD_EN;
1272 /* Setup read of endpoint */
1273 writel(hwrep, USBD_CTRL(udc->udp_baseaddr));
1275 /* Wait until packet is ready */
1276 while ((((tmpv = readl(USBD_RXPLEN(udc->udp_baseaddr))) &
1277 PKT_RDY) == 0) && (to > 0))
1278 to--;
1279 if (!to)
1280 dev_dbg(udc->dev, "No packet ready on FIFO EP read\n");
1282 /* Mask out count */
1283 tmp = tmpv & PKT_LNGTH_MASK;
1284 if (bytes < tmp)
1285 tmp = bytes;
1287 if ((tmp > 0) && (data != NULL))
1288 udc_pop_fifo(udc, (u8 *) data, tmp);
1290 writel(((hwep & 0x1E) << 1), USBD_CTRL(udc->udp_baseaddr));
1292 /* Clear the buffer */
1293 udc_clr_buffer_hwep(udc, hwep);
1295 return tmp;
1298 /* Stuffs data into the FIFO, adjusts for alignment and data size */
1299 static void udc_stuff_fifo(struct lpc32xx_udc *udc, u8 *data, u32 bytes)
1301 int n, i, bl;
1302 u16 *p16;
1303 u32 *p32, tmp, cbytes;
1305 /* Use optimal data transfer method based on source address and size */
1306 switch (((u32) data) & 0x3) {
1307 case 0: /* 32-bit aligned */
1308 p32 = (u32 *) data;
1309 cbytes = (bytes & ~0x3);
1311 /* Copy 32-bit aligned data first */
1312 for (n = 0; n < cbytes; n += 4)
1313 writel(*p32++, USBD_TXDATA(udc->udp_baseaddr));
1315 /* Handle any remaining bytes */
1316 bl = bytes - cbytes;
1317 if (bl) {
1318 tmp = 0;
1319 for (n = 0; n < bl; n++)
1320 tmp |= data[cbytes + n] << (n * 8);
1322 writel(tmp, USBD_TXDATA(udc->udp_baseaddr));
1324 break;
1326 case 1: /* 8-bit aligned */
1327 case 3:
1328 /* Each byte has to be handled independently */
1329 for (n = 0; n < bytes; n += 4) {
1330 bl = bytes - n;
1331 if (bl > 4)
1332 bl = 4;
1334 tmp = 0;
1335 for (i = 0; i < bl; i++)
1336 tmp |= data[n + i] << (i * 8);
1338 writel(tmp, USBD_TXDATA(udc->udp_baseaddr));
1340 break;
1342 case 2: /* 16-bit aligned */
1343 p16 = (u16 *) data;
1344 cbytes = (bytes & ~0x3);
1346 /* Copy 32-bit aligned data first */
1347 for (n = 0; n < cbytes; n += 4) {
1348 tmp = *p16++ & 0xFFFF;
1349 tmp |= (*p16++ & 0xFFFF) << 16;
1350 writel(tmp, USBD_TXDATA(udc->udp_baseaddr));
1353 /* Handle any remaining bytes */
1354 bl = bytes - cbytes;
1355 if (bl) {
1356 tmp = 0;
1357 for (n = 0; n < bl; n++)
1358 tmp |= data[cbytes + n] << (n * 8);
1360 writel(tmp, USBD_TXDATA(udc->udp_baseaddr));
1362 break;
1366 /* Write data to the FIFO for an endpoint. This function is for endpoints (such
1367 * as EP0) that don't use DMA. Note that the endpoint must be selected in the
1368 * protocol engine prior to this call. */
1369 static void udc_write_hwep(struct lpc32xx_udc *udc, u32 hwep, u32 *data,
1370 u32 bytes)
1372 u32 hwwep = ((hwep & 0x1E) << 1) | CTRL_WR_EN;
1374 if ((bytes > 0) && (data == NULL))
1375 return;
1377 /* Setup write of endpoint */
1378 writel(hwwep, USBD_CTRL(udc->udp_baseaddr));
1380 writel(bytes, USBD_TXPLEN(udc->udp_baseaddr));
1382 /* Need at least 1 byte to trigger TX */
1383 if (bytes == 0)
1384 writel(0, USBD_TXDATA(udc->udp_baseaddr));
1385 else
1386 udc_stuff_fifo(udc, (u8 *) data, bytes);
1388 writel(((hwep & 0x1E) << 1), USBD_CTRL(udc->udp_baseaddr));
1390 udc_val_buffer_hwep(udc, hwep);
1393 /* USB device reset - resets USB to a default state with just EP0
1394 enabled */
1395 static void uda_usb_reset(struct lpc32xx_udc *udc)
1397 u32 i = 0;
1398 /* Re-init device controller and EP0 */
1399 udc_enable(udc);
1400 udc->gadget.speed = USB_SPEED_FULL;
1402 for (i = 1; i < NUM_ENDPOINTS; i++) {
1403 struct lpc32xx_ep *ep = &udc->ep[i];
1404 ep->req_pending = 0;
1408 /* Send a ZLP on EP0 */
1409 static void udc_ep0_send_zlp(struct lpc32xx_udc *udc)
1411 udc_write_hwep(udc, EP_IN, NULL, 0);
1414 /* Get current frame number */
1415 static u16 udc_get_current_frame(struct lpc32xx_udc *udc)
1417 u16 flo, fhi;
1419 udc_protocol_cmd_w(udc, CMD_RD_FRAME);
1420 flo = (u16) udc_protocol_cmd_r(udc, DAT_RD_FRAME);
1421 fhi = (u16) udc_protocol_cmd_r(udc, DAT_RD_FRAME);
1423 return (fhi << 8) | flo;
1426 /* Set the device as configured - enables all endpoints */
1427 static inline void udc_set_device_configured(struct lpc32xx_udc *udc)
1429 udc_protocol_cmd_data_w(udc, CMD_CFG_DEV, DAT_WR_BYTE(CONF_DVICE));
1432 /* Set the device as unconfigured - disables all endpoints */
1433 static inline void udc_set_device_unconfigured(struct lpc32xx_udc *udc)
1435 udc_protocol_cmd_data_w(udc, CMD_CFG_DEV, DAT_WR_BYTE(0));
1438 /* reinit == restore initial software state */
1439 static void udc_reinit(struct lpc32xx_udc *udc)
1441 u32 i;
1443 INIT_LIST_HEAD(&udc->gadget.ep_list);
1444 INIT_LIST_HEAD(&udc->gadget.ep0->ep_list);
1446 for (i = 0; i < NUM_ENDPOINTS; i++) {
1447 struct lpc32xx_ep *ep = &udc->ep[i];
1449 if (i != 0)
1450 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
1451 usb_ep_set_maxpacket_limit(&ep->ep, ep->maxpacket);
1452 INIT_LIST_HEAD(&ep->queue);
1453 ep->req_pending = 0;
1456 udc->ep0state = WAIT_FOR_SETUP;
1459 /* Must be called with lock */
1460 static void done(struct lpc32xx_ep *ep, struct lpc32xx_request *req, int status)
1462 struct lpc32xx_udc *udc = ep->udc;
1464 list_del_init(&req->queue);
1465 if (req->req.status == -EINPROGRESS)
1466 req->req.status = status;
1467 else
1468 status = req->req.status;
1470 if (ep->lep) {
1471 usb_gadget_unmap_request(&udc->gadget, &req->req, ep->is_in);
1473 /* Free DDs */
1474 udc_dd_free(udc, req->dd_desc_ptr);
1477 if (status && status != -ESHUTDOWN)
1478 ep_dbg(ep, "%s done %p, status %d\n", ep->ep.name, req, status);
1480 ep->req_pending = 0;
1481 spin_unlock(&udc->lock);
1482 req->req.complete(&ep->ep, &req->req);
1483 spin_lock(&udc->lock);
1486 /* Must be called with lock */
1487 static void nuke(struct lpc32xx_ep *ep, int status)
1489 struct lpc32xx_request *req;
1491 while (!list_empty(&ep->queue)) {
1492 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
1493 done(ep, req, status);
1496 if (status == -ESHUTDOWN) {
1497 uda_disable_hwepint(ep->udc, ep->hwep_num);
1498 udc_disable_hwep(ep->udc, ep->hwep_num);
1502 /* IN endpoint 0 transfer */
1503 static int udc_ep0_in_req(struct lpc32xx_udc *udc)
1505 struct lpc32xx_request *req;
1506 struct lpc32xx_ep *ep0 = &udc->ep[0];
1507 u32 tsend, ts = 0;
1509 if (list_empty(&ep0->queue))
1510 /* Nothing to send */
1511 return 0;
1512 else
1513 req = list_entry(ep0->queue.next, struct lpc32xx_request,
1514 queue);
1516 tsend = ts = req->req.length - req->req.actual;
1517 if (ts == 0) {
1518 /* Send a ZLP */
1519 udc_ep0_send_zlp(udc);
1520 done(ep0, req, 0);
1521 return 1;
1522 } else if (ts > ep0->ep.maxpacket)
1523 ts = ep0->ep.maxpacket; /* Just send what we can */
1525 /* Write data to the EP0 FIFO and start transfer */
1526 udc_write_hwep(udc, EP_IN, (req->req.buf + req->req.actual), ts);
1528 /* Increment data pointer */
1529 req->req.actual += ts;
1531 if (tsend >= ep0->ep.maxpacket)
1532 return 0; /* Stay in data transfer state */
1534 /* Transfer request is complete */
1535 udc->ep0state = WAIT_FOR_SETUP;
1536 done(ep0, req, 0);
1537 return 1;
1540 /* OUT endpoint 0 transfer */
1541 static int udc_ep0_out_req(struct lpc32xx_udc *udc)
1543 struct lpc32xx_request *req;
1544 struct lpc32xx_ep *ep0 = &udc->ep[0];
1545 u32 tr, bufferspace;
1547 if (list_empty(&ep0->queue))
1548 return 0;
1549 else
1550 req = list_entry(ep0->queue.next, struct lpc32xx_request,
1551 queue);
1553 if (req) {
1554 if (req->req.length == 0) {
1555 /* Just dequeue request */
1556 done(ep0, req, 0);
1557 udc->ep0state = WAIT_FOR_SETUP;
1558 return 1;
1561 /* Get data from FIFO */
1562 bufferspace = req->req.length - req->req.actual;
1563 if (bufferspace > ep0->ep.maxpacket)
1564 bufferspace = ep0->ep.maxpacket;
1566 /* Copy data to buffer */
1567 prefetchw(req->req.buf + req->req.actual);
1568 tr = udc_read_hwep(udc, EP_OUT, req->req.buf + req->req.actual,
1569 bufferspace);
1570 req->req.actual += bufferspace;
1572 if (tr < ep0->ep.maxpacket) {
1573 /* This is the last packet */
1574 done(ep0, req, 0);
1575 udc->ep0state = WAIT_FOR_SETUP;
1576 return 1;
1580 return 0;
1583 /* Must be called with lock */
1584 static void stop_activity(struct lpc32xx_udc *udc)
1586 struct usb_gadget_driver *driver = udc->driver;
1587 int i;
1589 if (udc->gadget.speed == USB_SPEED_UNKNOWN)
1590 driver = NULL;
1592 udc->gadget.speed = USB_SPEED_UNKNOWN;
1593 udc->suspended = 0;
1595 for (i = 0; i < NUM_ENDPOINTS; i++) {
1596 struct lpc32xx_ep *ep = &udc->ep[i];
1597 nuke(ep, -ESHUTDOWN);
1599 if (driver) {
1600 spin_unlock(&udc->lock);
1601 driver->disconnect(&udc->gadget);
1602 spin_lock(&udc->lock);
1605 isp1301_pullup_enable(udc, 0, 0);
1606 udc_disable(udc);
1607 udc_reinit(udc);
1611 * Activate or kill host pullup
1612 * Can be called with or without lock
1614 static void pullup(struct lpc32xx_udc *udc, int is_on)
1616 if (!udc->clocked)
1617 return;
1619 if (!udc->enabled || !udc->vbus)
1620 is_on = 0;
1622 if (is_on != udc->pullup)
1623 isp1301_pullup_enable(udc, is_on, 0);
1626 /* Must be called without lock */
1627 static int lpc32xx_ep_disable(struct usb_ep *_ep)
1629 struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
1630 struct lpc32xx_udc *udc = ep->udc;
1631 unsigned long flags;
1633 if ((ep->hwep_num_base == 0) || (ep->hwep_num == 0))
1634 return -EINVAL;
1635 spin_lock_irqsave(&udc->lock, flags);
1637 nuke(ep, -ESHUTDOWN);
1639 /* Clear all DMA statuses for this EP */
1640 udc_ep_dma_disable(udc, ep->hwep_num);
1641 writel(1 << ep->hwep_num, USBD_EOTINTCLR(udc->udp_baseaddr));
1642 writel(1 << ep->hwep_num, USBD_NDDRTINTCLR(udc->udp_baseaddr));
1643 writel(1 << ep->hwep_num, USBD_SYSERRTINTCLR(udc->udp_baseaddr));
1644 writel(1 << ep->hwep_num, USBD_DMARCLR(udc->udp_baseaddr));
1646 /* Remove the DD pointer in the UDCA */
1647 udc->udca_v_base[ep->hwep_num] = 0;
1649 /* Disable and reset endpoint and interrupt */
1650 uda_clear_hwepint(udc, ep->hwep_num);
1651 udc_unrealize_hwep(udc, ep->hwep_num);
1653 ep->hwep_num = 0;
1655 spin_unlock_irqrestore(&udc->lock, flags);
1657 atomic_dec(&udc->enabled_ep_cnt);
1658 wake_up(&udc->ep_disable_wait_queue);
1660 return 0;
1663 /* Must be called without lock */
1664 static int lpc32xx_ep_enable(struct usb_ep *_ep,
1665 const struct usb_endpoint_descriptor *desc)
1667 struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
1668 struct lpc32xx_udc *udc = ep->udc;
1669 u16 maxpacket;
1670 u32 tmp;
1671 unsigned long flags;
1673 /* Verify EP data */
1674 if ((!_ep) || (!ep) || (!desc) ||
1675 (desc->bDescriptorType != USB_DT_ENDPOINT)) {
1676 dev_dbg(udc->dev, "bad ep or descriptor\n");
1677 return -EINVAL;
1679 maxpacket = usb_endpoint_maxp(desc);
1680 if ((maxpacket == 0) || (maxpacket > ep->maxpacket)) {
1681 dev_dbg(udc->dev, "bad ep descriptor's packet size\n");
1682 return -EINVAL;
1685 /* Don't touch EP0 */
1686 if (ep->hwep_num_base == 0) {
1687 dev_dbg(udc->dev, "Can't re-enable EP0!!!\n");
1688 return -EINVAL;
1691 /* Is driver ready? */
1692 if ((!udc->driver) || (udc->gadget.speed == USB_SPEED_UNKNOWN)) {
1693 dev_dbg(udc->dev, "bogus device state\n");
1694 return -ESHUTDOWN;
1697 tmp = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
1698 switch (tmp) {
1699 case USB_ENDPOINT_XFER_CONTROL:
1700 return -EINVAL;
1702 case USB_ENDPOINT_XFER_INT:
1703 if (maxpacket > ep->maxpacket) {
1704 dev_dbg(udc->dev,
1705 "Bad INT endpoint maxpacket %d\n", maxpacket);
1706 return -EINVAL;
1708 break;
1710 case USB_ENDPOINT_XFER_BULK:
1711 switch (maxpacket) {
1712 case 8:
1713 case 16:
1714 case 32:
1715 case 64:
1716 break;
1718 default:
1719 dev_dbg(udc->dev,
1720 "Bad BULK endpoint maxpacket %d\n", maxpacket);
1721 return -EINVAL;
1723 break;
1725 case USB_ENDPOINT_XFER_ISOC:
1726 break;
1728 spin_lock_irqsave(&udc->lock, flags);
1730 /* Initialize endpoint to match the selected descriptor */
1731 ep->is_in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
1732 ep->ep.maxpacket = maxpacket;
1734 /* Map hardware endpoint from base and direction */
1735 if (ep->is_in)
1736 /* IN endpoints are offset 1 from the OUT endpoint */
1737 ep->hwep_num = ep->hwep_num_base + EP_IN;
1738 else
1739 ep->hwep_num = ep->hwep_num_base;
1741 ep_dbg(ep, "EP enabled: %s, HW:%d, MP:%d IN:%d\n", ep->ep.name,
1742 ep->hwep_num, maxpacket, (ep->is_in == 1));
1744 /* Realize the endpoint, interrupt is enabled later when
1745 * buffers are queued, IN EPs will NAK until buffers are ready */
1746 udc_realize_hwep(udc, ep->hwep_num, ep->ep.maxpacket);
1747 udc_clr_buffer_hwep(udc, ep->hwep_num);
1748 uda_disable_hwepint(udc, ep->hwep_num);
1749 udc_clrstall_hwep(udc, ep->hwep_num);
1751 /* Clear all DMA statuses for this EP */
1752 udc_ep_dma_disable(udc, ep->hwep_num);
1753 writel(1 << ep->hwep_num, USBD_EOTINTCLR(udc->udp_baseaddr));
1754 writel(1 << ep->hwep_num, USBD_NDDRTINTCLR(udc->udp_baseaddr));
1755 writel(1 << ep->hwep_num, USBD_SYSERRTINTCLR(udc->udp_baseaddr));
1756 writel(1 << ep->hwep_num, USBD_DMARCLR(udc->udp_baseaddr));
1758 spin_unlock_irqrestore(&udc->lock, flags);
1760 atomic_inc(&udc->enabled_ep_cnt);
1761 return 0;
1765 * Allocate a USB request list
1766 * Can be called with or without lock
1768 static struct usb_request *lpc32xx_ep_alloc_request(struct usb_ep *_ep,
1769 gfp_t gfp_flags)
1771 struct lpc32xx_request *req;
1773 req = kzalloc(sizeof(struct lpc32xx_request), gfp_flags);
1774 if (!req)
1775 return NULL;
1777 INIT_LIST_HEAD(&req->queue);
1778 return &req->req;
1782 * De-allocate a USB request list
1783 * Can be called with or without lock
1785 static void lpc32xx_ep_free_request(struct usb_ep *_ep,
1786 struct usb_request *_req)
1788 struct lpc32xx_request *req;
1790 req = container_of(_req, struct lpc32xx_request, req);
1791 BUG_ON(!list_empty(&req->queue));
1792 kfree(req);
1795 /* Must be called without lock */
1796 static int lpc32xx_ep_queue(struct usb_ep *_ep,
1797 struct usb_request *_req, gfp_t gfp_flags)
1799 struct lpc32xx_request *req;
1800 struct lpc32xx_ep *ep;
1801 struct lpc32xx_udc *udc;
1802 unsigned long flags;
1803 int status = 0;
1805 req = container_of(_req, struct lpc32xx_request, req);
1806 ep = container_of(_ep, struct lpc32xx_ep, ep);
1808 if (!_req || !_req->complete || !_req->buf ||
1809 !list_empty(&req->queue))
1810 return -EINVAL;
1812 udc = ep->udc;
1814 if (!_ep) {
1815 dev_dbg(udc->dev, "invalid ep\n");
1816 return -EINVAL;
1820 if ((!udc) || (!udc->driver) ||
1821 (udc->gadget.speed == USB_SPEED_UNKNOWN)) {
1822 dev_dbg(udc->dev, "invalid device\n");
1823 return -EINVAL;
1826 if (ep->lep) {
1827 struct lpc32xx_usbd_dd_gad *dd;
1829 status = usb_gadget_map_request(&udc->gadget, _req, ep->is_in);
1830 if (status)
1831 return status;
1833 /* For the request, build a list of DDs */
1834 dd = udc_dd_alloc(udc);
1835 if (!dd) {
1836 /* Error allocating DD */
1837 return -ENOMEM;
1839 req->dd_desc_ptr = dd;
1841 /* Setup the DMA descriptor */
1842 dd->dd_next_phy = dd->dd_next_v = 0;
1843 dd->dd_buffer_addr = req->req.dma;
1844 dd->dd_status = 0;
1846 /* Special handling for ISO EPs */
1847 if (ep->eptype == EP_ISO_TYPE) {
1848 dd->dd_setup = DD_SETUP_ISO_EP |
1849 DD_SETUP_PACKETLEN(0) |
1850 DD_SETUP_DMALENBYTES(1);
1851 dd->dd_iso_ps_mem_addr = dd->this_dma + 24;
1852 if (ep->is_in)
1853 dd->iso_status[0] = req->req.length;
1854 else
1855 dd->iso_status[0] = 0;
1856 } else
1857 dd->dd_setup = DD_SETUP_PACKETLEN(ep->ep.maxpacket) |
1858 DD_SETUP_DMALENBYTES(req->req.length);
1861 ep_dbg(ep, "%s queue req %p len %d buf %p (in=%d) z=%d\n", _ep->name,
1862 _req, _req->length, _req->buf, ep->is_in, _req->zero);
1864 spin_lock_irqsave(&udc->lock, flags);
1866 _req->status = -EINPROGRESS;
1867 _req->actual = 0;
1868 req->send_zlp = _req->zero;
1870 /* Kickstart empty queues */
1871 if (list_empty(&ep->queue)) {
1872 list_add_tail(&req->queue, &ep->queue);
1874 if (ep->hwep_num_base == 0) {
1875 /* Handle expected data direction */
1876 if (ep->is_in) {
1877 /* IN packet to host */
1878 udc->ep0state = DATA_IN;
1879 status = udc_ep0_in_req(udc);
1880 } else {
1881 /* OUT packet from host */
1882 udc->ep0state = DATA_OUT;
1883 status = udc_ep0_out_req(udc);
1885 } else if (ep->is_in) {
1886 /* IN packet to host and kick off transfer */
1887 if (!ep->req_pending)
1888 udc_ep_in_req_dma(udc, ep);
1889 } else
1890 /* OUT packet from host and kick off list */
1891 if (!ep->req_pending)
1892 udc_ep_out_req_dma(udc, ep);
1893 } else
1894 list_add_tail(&req->queue, &ep->queue);
1896 spin_unlock_irqrestore(&udc->lock, flags);
1898 return (status < 0) ? status : 0;
1901 /* Must be called without lock */
1902 static int lpc32xx_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1904 struct lpc32xx_ep *ep;
1905 struct lpc32xx_request *req;
1906 unsigned long flags;
1908 ep = container_of(_ep, struct lpc32xx_ep, ep);
1909 if (!_ep || ep->hwep_num_base == 0)
1910 return -EINVAL;
1912 spin_lock_irqsave(&ep->udc->lock, flags);
1914 /* make sure it's actually queued on this endpoint */
1915 list_for_each_entry(req, &ep->queue, queue) {
1916 if (&req->req == _req)
1917 break;
1919 if (&req->req != _req) {
1920 spin_unlock_irqrestore(&ep->udc->lock, flags);
1921 return -EINVAL;
1924 done(ep, req, -ECONNRESET);
1926 spin_unlock_irqrestore(&ep->udc->lock, flags);
1928 return 0;
1931 /* Must be called without lock */
1932 static int lpc32xx_ep_set_halt(struct usb_ep *_ep, int value)
1934 struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
1935 struct lpc32xx_udc *udc = ep->udc;
1936 unsigned long flags;
1938 if ((!ep) || (ep->hwep_num <= 1))
1939 return -EINVAL;
1941 /* Don't halt an IN EP */
1942 if (ep->is_in)
1943 return -EAGAIN;
1945 spin_lock_irqsave(&udc->lock, flags);
1947 if (value == 1) {
1948 /* stall */
1949 udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(ep->hwep_num),
1950 DAT_WR_BYTE(EP_STAT_ST));
1951 } else {
1952 /* End stall */
1953 ep->wedge = 0;
1954 udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(ep->hwep_num),
1955 DAT_WR_BYTE(0));
1958 spin_unlock_irqrestore(&udc->lock, flags);
1960 return 0;
1963 /* set the halt feature and ignores clear requests */
1964 static int lpc32xx_ep_set_wedge(struct usb_ep *_ep)
1966 struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
1968 if (!_ep || !ep->udc)
1969 return -EINVAL;
1971 ep->wedge = 1;
1973 return usb_ep_set_halt(_ep);
1976 static const struct usb_ep_ops lpc32xx_ep_ops = {
1977 .enable = lpc32xx_ep_enable,
1978 .disable = lpc32xx_ep_disable,
1979 .alloc_request = lpc32xx_ep_alloc_request,
1980 .free_request = lpc32xx_ep_free_request,
1981 .queue = lpc32xx_ep_queue,
1982 .dequeue = lpc32xx_ep_dequeue,
1983 .set_halt = lpc32xx_ep_set_halt,
1984 .set_wedge = lpc32xx_ep_set_wedge,
1987 /* Send a ZLP on a non-0 IN EP */
1988 void udc_send_in_zlp(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
1990 /* Clear EP status */
1991 udc_clearep_getsts(udc, ep->hwep_num);
1993 /* Send ZLP via FIFO mechanism */
1994 udc_write_hwep(udc, ep->hwep_num, NULL, 0);
1998 * Handle EP completion for ZLP
1999 * This function will only be called when a delayed ZLP needs to be sent out
2000 * after a DMA transfer has filled both buffers.
2002 void udc_handle_eps(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
2004 u32 epstatus;
2005 struct lpc32xx_request *req;
2007 if (ep->hwep_num <= 0)
2008 return;
2010 uda_clear_hwepint(udc, ep->hwep_num);
2012 /* If this interrupt isn't enabled, return now */
2013 if (!(udc->enabled_hwepints & (1 << ep->hwep_num)))
2014 return;
2016 /* Get endpoint status */
2017 epstatus = udc_clearep_getsts(udc, ep->hwep_num);
2020 * This should never happen, but protect against writing to the
2021 * buffer when full.
2023 if (epstatus & EP_SEL_F)
2024 return;
2026 if (ep->is_in) {
2027 udc_send_in_zlp(udc, ep);
2028 uda_disable_hwepint(udc, ep->hwep_num);
2029 } else
2030 return;
2032 /* If there isn't a request waiting, something went wrong */
2033 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
2034 if (req) {
2035 done(ep, req, 0);
2037 /* Start another request if ready */
2038 if (!list_empty(&ep->queue)) {
2039 if (ep->is_in)
2040 udc_ep_in_req_dma(udc, ep);
2041 else
2042 udc_ep_out_req_dma(udc, ep);
2043 } else
2044 ep->req_pending = 0;
2049 /* DMA end of transfer completion */
2050 static void udc_handle_dma_ep(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
2052 u32 status, epstatus;
2053 struct lpc32xx_request *req;
2054 struct lpc32xx_usbd_dd_gad *dd;
2056 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2057 ep->totalints++;
2058 #endif
2060 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
2061 if (!req) {
2062 ep_err(ep, "DMA interrupt on no req!\n");
2063 return;
2065 dd = req->dd_desc_ptr;
2067 /* DMA descriptor should always be retired for this call */
2068 if (!(dd->dd_status & DD_STATUS_DD_RETIRED))
2069 ep_warn(ep, "DMA descriptor did not retire\n");
2071 /* Disable DMA */
2072 udc_ep_dma_disable(udc, ep->hwep_num);
2073 writel((1 << ep->hwep_num), USBD_EOTINTCLR(udc->udp_baseaddr));
2074 writel((1 << ep->hwep_num), USBD_NDDRTINTCLR(udc->udp_baseaddr));
2076 /* System error? */
2077 if (readl(USBD_SYSERRTINTST(udc->udp_baseaddr)) &
2078 (1 << ep->hwep_num)) {
2079 writel((1 << ep->hwep_num),
2080 USBD_SYSERRTINTCLR(udc->udp_baseaddr));
2081 ep_err(ep, "AHB critical error!\n");
2082 ep->req_pending = 0;
2084 /* The error could have occurred on a packet of a multipacket
2085 * transfer, so recovering the transfer is not possible. Close
2086 * the request with an error */
2087 done(ep, req, -ECONNABORTED);
2088 return;
2091 /* Handle the current DD's status */
2092 status = dd->dd_status;
2093 switch (status & DD_STATUS_STS_MASK) {
2094 case DD_STATUS_STS_NS:
2095 /* DD not serviced? This shouldn't happen! */
2096 ep->req_pending = 0;
2097 ep_err(ep, "DMA critical EP error: DD not serviced (0x%x)!\n",
2098 status);
2100 done(ep, req, -ECONNABORTED);
2101 return;
2103 case DD_STATUS_STS_BS:
2104 /* Interrupt only fires on EOT - This shouldn't happen! */
2105 ep->req_pending = 0;
2106 ep_err(ep, "DMA critical EP error: EOT prior to service completion (0x%x)!\n",
2107 status);
2108 done(ep, req, -ECONNABORTED);
2109 return;
2111 case DD_STATUS_STS_NC:
2112 case DD_STATUS_STS_DUR:
2113 /* Really just a short packet, not an underrun */
2114 /* This is a good status and what we expect */
2115 break;
2117 default:
2118 /* Data overrun, system error, or unknown */
2119 ep->req_pending = 0;
2120 ep_err(ep, "DMA critical EP error: System error (0x%x)!\n",
2121 status);
2122 done(ep, req, -ECONNABORTED);
2123 return;
2126 /* ISO endpoints are handled differently */
2127 if (ep->eptype == EP_ISO_TYPE) {
2128 if (ep->is_in)
2129 req->req.actual = req->req.length;
2130 else
2131 req->req.actual = dd->iso_status[0] & 0xFFFF;
2132 } else
2133 req->req.actual += DD_STATUS_CURDMACNT(status);
2135 /* Send a ZLP if necessary. This will be done for non-int
2136 * packets which have a size that is a divisor of MAXP */
2137 if (req->send_zlp) {
2139 * If at least 1 buffer is available, send the ZLP now.
2140 * Otherwise, the ZLP send needs to be deferred until a
2141 * buffer is available.
2143 if (udc_clearep_getsts(udc, ep->hwep_num) & EP_SEL_F) {
2144 udc_clearep_getsts(udc, ep->hwep_num);
2145 uda_enable_hwepint(udc, ep->hwep_num);
2146 epstatus = udc_clearep_getsts(udc, ep->hwep_num);
2148 /* Let the EP interrupt handle the ZLP */
2149 return;
2150 } else
2151 udc_send_in_zlp(udc, ep);
2154 /* Transfer request is complete */
2155 done(ep, req, 0);
2157 /* Start another request if ready */
2158 udc_clearep_getsts(udc, ep->hwep_num);
2159 if (!list_empty((&ep->queue))) {
2160 if (ep->is_in)
2161 udc_ep_in_req_dma(udc, ep);
2162 else
2163 udc_ep_out_req_dma(udc, ep);
2164 } else
2165 ep->req_pending = 0;
2171 * Endpoint 0 functions
2174 static void udc_handle_dev(struct lpc32xx_udc *udc)
2176 u32 tmp;
2178 udc_protocol_cmd_w(udc, CMD_GET_DEV_STAT);
2179 tmp = udc_protocol_cmd_r(udc, DAT_GET_DEV_STAT);
2181 if (tmp & DEV_RST)
2182 uda_usb_reset(udc);
2183 else if (tmp & DEV_CON_CH)
2184 uda_power_event(udc, (tmp & DEV_CON));
2185 else if (tmp & DEV_SUS_CH) {
2186 if (tmp & DEV_SUS) {
2187 if (udc->vbus == 0)
2188 stop_activity(udc);
2189 else if ((udc->gadget.speed != USB_SPEED_UNKNOWN) &&
2190 udc->driver) {
2191 /* Power down transceiver */
2192 udc->poweron = 0;
2193 schedule_work(&udc->pullup_job);
2194 uda_resm_susp_event(udc, 1);
2196 } else if ((udc->gadget.speed != USB_SPEED_UNKNOWN) &&
2197 udc->driver && udc->vbus) {
2198 uda_resm_susp_event(udc, 0);
2199 /* Power up transceiver */
2200 udc->poweron = 1;
2201 schedule_work(&udc->pullup_job);
2206 static int udc_get_status(struct lpc32xx_udc *udc, u16 reqtype, u16 wIndex)
2208 struct lpc32xx_ep *ep;
2209 u32 ep0buff = 0, tmp;
2211 switch (reqtype & USB_RECIP_MASK) {
2212 case USB_RECIP_INTERFACE:
2213 break; /* Not supported */
2215 case USB_RECIP_DEVICE:
2216 ep0buff = (udc->selfpowered << USB_DEVICE_SELF_POWERED);
2217 if (udc->dev_status & (1 << USB_DEVICE_REMOTE_WAKEUP))
2218 ep0buff |= (1 << USB_DEVICE_REMOTE_WAKEUP);
2219 break;
2221 case USB_RECIP_ENDPOINT:
2222 tmp = wIndex & USB_ENDPOINT_NUMBER_MASK;
2223 ep = &udc->ep[tmp];
2224 if ((tmp == 0) || (tmp >= NUM_ENDPOINTS))
2225 return -EOPNOTSUPP;
2227 if (wIndex & USB_DIR_IN) {
2228 if (!ep->is_in)
2229 return -EOPNOTSUPP; /* Something's wrong */
2230 } else if (ep->is_in)
2231 return -EOPNOTSUPP; /* Not an IN endpoint */
2233 /* Get status of the endpoint */
2234 udc_protocol_cmd_w(udc, CMD_SEL_EP(ep->hwep_num));
2235 tmp = udc_protocol_cmd_r(udc, DAT_SEL_EP(ep->hwep_num));
2237 if (tmp & EP_SEL_ST)
2238 ep0buff = (1 << USB_ENDPOINT_HALT);
2239 else
2240 ep0buff = 0;
2241 break;
2243 default:
2244 break;
2247 /* Return data */
2248 udc_write_hwep(udc, EP_IN, &ep0buff, 2);
2250 return 0;
2253 static void udc_handle_ep0_setup(struct lpc32xx_udc *udc)
2255 struct lpc32xx_ep *ep, *ep0 = &udc->ep[0];
2256 struct usb_ctrlrequest ctrlpkt;
2257 int i, bytes;
2258 u16 wIndex, wValue, wLength, reqtype, req, tmp;
2260 /* Nuke previous transfers */
2261 nuke(ep0, -EPROTO);
2263 /* Get setup packet */
2264 bytes = udc_read_hwep(udc, EP_OUT, (u32 *) &ctrlpkt, 8);
2265 if (bytes != 8) {
2266 ep_warn(ep0, "Incorrectly sized setup packet (s/b 8, is %d)!\n",
2267 bytes);
2268 return;
2271 /* Native endianness */
2272 wIndex = le16_to_cpu(ctrlpkt.wIndex);
2273 wValue = le16_to_cpu(ctrlpkt.wValue);
2274 wLength = le16_to_cpu(ctrlpkt.wLength);
2275 reqtype = le16_to_cpu(ctrlpkt.bRequestType);
2277 /* Set direction of EP0 */
2278 if (likely(reqtype & USB_DIR_IN))
2279 ep0->is_in = 1;
2280 else
2281 ep0->is_in = 0;
2283 /* Handle SETUP packet */
2284 req = le16_to_cpu(ctrlpkt.bRequest);
2285 switch (req) {
2286 case USB_REQ_CLEAR_FEATURE:
2287 case USB_REQ_SET_FEATURE:
2288 switch (reqtype) {
2289 case (USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2290 if (wValue != USB_DEVICE_REMOTE_WAKEUP)
2291 goto stall; /* Nothing else handled */
2293 /* Tell board about event */
2294 if (req == USB_REQ_CLEAR_FEATURE)
2295 udc->dev_status &=
2296 ~(1 << USB_DEVICE_REMOTE_WAKEUP);
2297 else
2298 udc->dev_status |=
2299 (1 << USB_DEVICE_REMOTE_WAKEUP);
2300 uda_remwkp_cgh(udc);
2301 goto zlp_send;
2303 case (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2304 tmp = wIndex & USB_ENDPOINT_NUMBER_MASK;
2305 if ((wValue != USB_ENDPOINT_HALT) ||
2306 (tmp >= NUM_ENDPOINTS))
2307 break;
2309 /* Find hardware endpoint from logical endpoint */
2310 ep = &udc->ep[tmp];
2311 tmp = ep->hwep_num;
2312 if (tmp == 0)
2313 break;
2315 if (req == USB_REQ_SET_FEATURE)
2316 udc_stall_hwep(udc, tmp);
2317 else if (!ep->wedge)
2318 udc_clrstall_hwep(udc, tmp);
2320 goto zlp_send;
2322 default:
2323 break;
2327 case USB_REQ_SET_ADDRESS:
2328 if (reqtype == (USB_TYPE_STANDARD | USB_RECIP_DEVICE)) {
2329 udc_set_address(udc, wValue);
2330 goto zlp_send;
2332 break;
2334 case USB_REQ_GET_STATUS:
2335 udc_get_status(udc, reqtype, wIndex);
2336 return;
2338 default:
2339 break; /* Let GadgetFS handle the descriptor instead */
2342 if (likely(udc->driver)) {
2343 /* device-2-host (IN) or no data setup command, process
2344 * immediately */
2345 spin_unlock(&udc->lock);
2346 i = udc->driver->setup(&udc->gadget, &ctrlpkt);
2348 spin_lock(&udc->lock);
2349 if (req == USB_REQ_SET_CONFIGURATION) {
2350 /* Configuration is set after endpoints are realized */
2351 if (wValue) {
2352 /* Set configuration */
2353 udc_set_device_configured(udc);
2355 udc_protocol_cmd_data_w(udc, CMD_SET_MODE,
2356 DAT_WR_BYTE(AP_CLK |
2357 INAK_BI | INAK_II));
2358 } else {
2359 /* Clear configuration */
2360 udc_set_device_unconfigured(udc);
2362 /* Disable NAK interrupts */
2363 udc_protocol_cmd_data_w(udc, CMD_SET_MODE,
2364 DAT_WR_BYTE(AP_CLK));
2368 if (i < 0) {
2369 /* setup processing failed, force stall */
2370 dev_dbg(udc->dev,
2371 "req %02x.%02x protocol STALL; stat %d\n",
2372 reqtype, req, i);
2373 udc->ep0state = WAIT_FOR_SETUP;
2374 goto stall;
2378 if (!ep0->is_in)
2379 udc_ep0_send_zlp(udc); /* ZLP IN packet on data phase */
2381 return;
2383 stall:
2384 udc_stall_hwep(udc, EP_IN);
2385 return;
2387 zlp_send:
2388 udc_ep0_send_zlp(udc);
2389 return;
2392 /* IN endpoint 0 transfer */
2393 static void udc_handle_ep0_in(struct lpc32xx_udc *udc)
2395 struct lpc32xx_ep *ep0 = &udc->ep[0];
2396 u32 epstatus;
2398 /* Clear EP interrupt */
2399 epstatus = udc_clearep_getsts(udc, EP_IN);
2401 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2402 ep0->totalints++;
2403 #endif
2405 /* Stalled? Clear stall and reset buffers */
2406 if (epstatus & EP_SEL_ST) {
2407 udc_clrstall_hwep(udc, EP_IN);
2408 nuke(ep0, -ECONNABORTED);
2409 udc->ep0state = WAIT_FOR_SETUP;
2410 return;
2413 /* Is a buffer available? */
2414 if (!(epstatus & EP_SEL_F)) {
2415 /* Handle based on current state */
2416 if (udc->ep0state == DATA_IN)
2417 udc_ep0_in_req(udc);
2418 else {
2419 /* Unknown state for EP0 oe end of DATA IN phase */
2420 nuke(ep0, -ECONNABORTED);
2421 udc->ep0state = WAIT_FOR_SETUP;
2426 /* OUT endpoint 0 transfer */
2427 static void udc_handle_ep0_out(struct lpc32xx_udc *udc)
2429 struct lpc32xx_ep *ep0 = &udc->ep[0];
2430 u32 epstatus;
2432 /* Clear EP interrupt */
2433 epstatus = udc_clearep_getsts(udc, EP_OUT);
2436 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2437 ep0->totalints++;
2438 #endif
2440 /* Stalled? */
2441 if (epstatus & EP_SEL_ST) {
2442 udc_clrstall_hwep(udc, EP_OUT);
2443 nuke(ep0, -ECONNABORTED);
2444 udc->ep0state = WAIT_FOR_SETUP;
2445 return;
2448 /* A NAK may occur if a packet couldn't be received yet */
2449 if (epstatus & EP_SEL_EPN)
2450 return;
2451 /* Setup packet incoming? */
2452 if (epstatus & EP_SEL_STP) {
2453 nuke(ep0, 0);
2454 udc->ep0state = WAIT_FOR_SETUP;
2457 /* Data available? */
2458 if (epstatus & EP_SEL_F)
2459 /* Handle based on current state */
2460 switch (udc->ep0state) {
2461 case WAIT_FOR_SETUP:
2462 udc_handle_ep0_setup(udc);
2463 break;
2465 case DATA_OUT:
2466 udc_ep0_out_req(udc);
2467 break;
2469 default:
2470 /* Unknown state for EP0 */
2471 nuke(ep0, -ECONNABORTED);
2472 udc->ep0state = WAIT_FOR_SETUP;
2476 /* Must be called without lock */
2477 static int lpc32xx_get_frame(struct usb_gadget *gadget)
2479 int frame;
2480 unsigned long flags;
2481 struct lpc32xx_udc *udc = to_udc(gadget);
2483 if (!udc->clocked)
2484 return -EINVAL;
2486 spin_lock_irqsave(&udc->lock, flags);
2488 frame = (int) udc_get_current_frame(udc);
2490 spin_unlock_irqrestore(&udc->lock, flags);
2492 return frame;
2495 static int lpc32xx_wakeup(struct usb_gadget *gadget)
2497 return -ENOTSUPP;
2500 static int lpc32xx_set_selfpowered(struct usb_gadget *gadget, int is_on)
2502 struct lpc32xx_udc *udc = to_udc(gadget);
2504 /* Always self-powered */
2505 udc->selfpowered = (is_on != 0);
2507 return 0;
2511 * vbus is here! turn everything on that's ready
2512 * Must be called without lock
2514 static int lpc32xx_vbus_session(struct usb_gadget *gadget, int is_active)
2516 unsigned long flags;
2517 struct lpc32xx_udc *udc = to_udc(gadget);
2519 spin_lock_irqsave(&udc->lock, flags);
2521 /* Doesn't need lock */
2522 if (udc->driver) {
2523 udc_clk_set(udc, 1);
2524 udc_enable(udc);
2525 pullup(udc, is_active);
2526 } else {
2527 stop_activity(udc);
2528 pullup(udc, 0);
2530 spin_unlock_irqrestore(&udc->lock, flags);
2532 * Wait for all the endpoints to disable,
2533 * before disabling clocks. Don't wait if
2534 * endpoints are not enabled.
2536 if (atomic_read(&udc->enabled_ep_cnt))
2537 wait_event_interruptible(udc->ep_disable_wait_queue,
2538 (atomic_read(&udc->enabled_ep_cnt) == 0));
2540 spin_lock_irqsave(&udc->lock, flags);
2542 udc_clk_set(udc, 0);
2545 spin_unlock_irqrestore(&udc->lock, flags);
2547 return 0;
2550 /* Can be called with or without lock */
2551 static int lpc32xx_pullup(struct usb_gadget *gadget, int is_on)
2553 struct lpc32xx_udc *udc = to_udc(gadget);
2555 /* Doesn't need lock */
2556 pullup(udc, is_on);
2558 return 0;
2561 static int lpc32xx_start(struct usb_gadget *, struct usb_gadget_driver *);
2562 static int lpc32xx_stop(struct usb_gadget *, struct usb_gadget_driver *);
2564 static const struct usb_gadget_ops lpc32xx_udc_ops = {
2565 .get_frame = lpc32xx_get_frame,
2566 .wakeup = lpc32xx_wakeup,
2567 .set_selfpowered = lpc32xx_set_selfpowered,
2568 .vbus_session = lpc32xx_vbus_session,
2569 .pullup = lpc32xx_pullup,
2570 .udc_start = lpc32xx_start,
2571 .udc_stop = lpc32xx_stop,
2574 static void nop_release(struct device *dev)
2576 /* nothing to free */
2579 static const struct lpc32xx_udc controller_template = {
2580 .gadget = {
2581 .ops = &lpc32xx_udc_ops,
2582 .name = driver_name,
2583 .dev = {
2584 .init_name = "gadget",
2585 .release = nop_release,
2588 .ep[0] = {
2589 .ep = {
2590 .name = "ep0",
2591 .ops = &lpc32xx_ep_ops,
2593 .maxpacket = 64,
2594 .hwep_num_base = 0,
2595 .hwep_num = 0, /* Can be 0 or 1, has special handling */
2596 .lep = 0,
2597 .eptype = EP_CTL_TYPE,
2599 .ep[1] = {
2600 .ep = {
2601 .name = "ep1-int",
2602 .ops = &lpc32xx_ep_ops,
2604 .maxpacket = 64,
2605 .hwep_num_base = 2,
2606 .hwep_num = 0, /* 2 or 3, will be set later */
2607 .lep = 1,
2608 .eptype = EP_INT_TYPE,
2610 .ep[2] = {
2611 .ep = {
2612 .name = "ep2-bulk",
2613 .ops = &lpc32xx_ep_ops,
2615 .maxpacket = 64,
2616 .hwep_num_base = 4,
2617 .hwep_num = 0, /* 4 or 5, will be set later */
2618 .lep = 2,
2619 .eptype = EP_BLK_TYPE,
2621 .ep[3] = {
2622 .ep = {
2623 .name = "ep3-iso",
2624 .ops = &lpc32xx_ep_ops,
2626 .maxpacket = 1023,
2627 .hwep_num_base = 6,
2628 .hwep_num = 0, /* 6 or 7, will be set later */
2629 .lep = 3,
2630 .eptype = EP_ISO_TYPE,
2632 .ep[4] = {
2633 .ep = {
2634 .name = "ep4-int",
2635 .ops = &lpc32xx_ep_ops,
2637 .maxpacket = 64,
2638 .hwep_num_base = 8,
2639 .hwep_num = 0, /* 8 or 9, will be set later */
2640 .lep = 4,
2641 .eptype = EP_INT_TYPE,
2643 .ep[5] = {
2644 .ep = {
2645 .name = "ep5-bulk",
2646 .ops = &lpc32xx_ep_ops,
2648 .maxpacket = 64,
2649 .hwep_num_base = 10,
2650 .hwep_num = 0, /* 10 or 11, will be set later */
2651 .lep = 5,
2652 .eptype = EP_BLK_TYPE,
2654 .ep[6] = {
2655 .ep = {
2656 .name = "ep6-iso",
2657 .ops = &lpc32xx_ep_ops,
2659 .maxpacket = 1023,
2660 .hwep_num_base = 12,
2661 .hwep_num = 0, /* 12 or 13, will be set later */
2662 .lep = 6,
2663 .eptype = EP_ISO_TYPE,
2665 .ep[7] = {
2666 .ep = {
2667 .name = "ep7-int",
2668 .ops = &lpc32xx_ep_ops,
2670 .maxpacket = 64,
2671 .hwep_num_base = 14,
2672 .hwep_num = 0,
2673 .lep = 7,
2674 .eptype = EP_INT_TYPE,
2676 .ep[8] = {
2677 .ep = {
2678 .name = "ep8-bulk",
2679 .ops = &lpc32xx_ep_ops,
2681 .maxpacket = 64,
2682 .hwep_num_base = 16,
2683 .hwep_num = 0,
2684 .lep = 8,
2685 .eptype = EP_BLK_TYPE,
2687 .ep[9] = {
2688 .ep = {
2689 .name = "ep9-iso",
2690 .ops = &lpc32xx_ep_ops,
2692 .maxpacket = 1023,
2693 .hwep_num_base = 18,
2694 .hwep_num = 0,
2695 .lep = 9,
2696 .eptype = EP_ISO_TYPE,
2698 .ep[10] = {
2699 .ep = {
2700 .name = "ep10-int",
2701 .ops = &lpc32xx_ep_ops,
2703 .maxpacket = 64,
2704 .hwep_num_base = 20,
2705 .hwep_num = 0,
2706 .lep = 10,
2707 .eptype = EP_INT_TYPE,
2709 .ep[11] = {
2710 .ep = {
2711 .name = "ep11-bulk",
2712 .ops = &lpc32xx_ep_ops,
2714 .maxpacket = 64,
2715 .hwep_num_base = 22,
2716 .hwep_num = 0,
2717 .lep = 11,
2718 .eptype = EP_BLK_TYPE,
2720 .ep[12] = {
2721 .ep = {
2722 .name = "ep12-iso",
2723 .ops = &lpc32xx_ep_ops,
2725 .maxpacket = 1023,
2726 .hwep_num_base = 24,
2727 .hwep_num = 0,
2728 .lep = 12,
2729 .eptype = EP_ISO_TYPE,
2731 .ep[13] = {
2732 .ep = {
2733 .name = "ep13-int",
2734 .ops = &lpc32xx_ep_ops,
2736 .maxpacket = 64,
2737 .hwep_num_base = 26,
2738 .hwep_num = 0,
2739 .lep = 13,
2740 .eptype = EP_INT_TYPE,
2742 .ep[14] = {
2743 .ep = {
2744 .name = "ep14-bulk",
2745 .ops = &lpc32xx_ep_ops,
2747 .maxpacket = 64,
2748 .hwep_num_base = 28,
2749 .hwep_num = 0,
2750 .lep = 14,
2751 .eptype = EP_BLK_TYPE,
2753 .ep[15] = {
2754 .ep = {
2755 .name = "ep15-bulk",
2756 .ops = &lpc32xx_ep_ops,
2758 .maxpacket = 1023,
2759 .hwep_num_base = 30,
2760 .hwep_num = 0,
2761 .lep = 15,
2762 .eptype = EP_BLK_TYPE,
2766 /* ISO and status interrupts */
2767 static irqreturn_t lpc32xx_usb_lp_irq(int irq, void *_udc)
2769 u32 tmp, devstat;
2770 struct lpc32xx_udc *udc = _udc;
2772 spin_lock(&udc->lock);
2774 /* Read the device status register */
2775 devstat = readl(USBD_DEVINTST(udc->udp_baseaddr));
2777 devstat &= ~USBD_EP_FAST;
2778 writel(devstat, USBD_DEVINTCLR(udc->udp_baseaddr));
2779 devstat = devstat & udc->enabled_devints;
2781 /* Device specific handling needed? */
2782 if (devstat & USBD_DEV_STAT)
2783 udc_handle_dev(udc);
2785 /* Start of frame? (devstat & FRAME_INT):
2786 * The frame interrupt isn't really needed for ISO support,
2787 * as the driver will queue the necessary packets */
2789 /* Error? */
2790 if (devstat & ERR_INT) {
2791 /* All types of errors, from cable removal during transfer to
2792 * misc protocol and bit errors. These are mostly for just info,
2793 * as the USB hardware will work around these. If these errors
2794 * happen alot, something is wrong. */
2795 udc_protocol_cmd_w(udc, CMD_RD_ERR_STAT);
2796 tmp = udc_protocol_cmd_r(udc, DAT_RD_ERR_STAT);
2797 dev_dbg(udc->dev, "Device error (0x%x)!\n", tmp);
2800 spin_unlock(&udc->lock);
2802 return IRQ_HANDLED;
2805 /* EP interrupts */
2806 static irqreturn_t lpc32xx_usb_hp_irq(int irq, void *_udc)
2808 u32 tmp;
2809 struct lpc32xx_udc *udc = _udc;
2811 spin_lock(&udc->lock);
2813 /* Read the device status register */
2814 writel(USBD_EP_FAST, USBD_DEVINTCLR(udc->udp_baseaddr));
2816 /* Endpoints */
2817 tmp = readl(USBD_EPINTST(udc->udp_baseaddr));
2819 /* Special handling for EP0 */
2820 if (tmp & (EP_MASK_SEL(0, EP_OUT) | EP_MASK_SEL(0, EP_IN))) {
2821 /* Handle EP0 IN */
2822 if (tmp & (EP_MASK_SEL(0, EP_IN)))
2823 udc_handle_ep0_in(udc);
2825 /* Handle EP0 OUT */
2826 if (tmp & (EP_MASK_SEL(0, EP_OUT)))
2827 udc_handle_ep0_out(udc);
2830 /* All other EPs */
2831 if (tmp & ~(EP_MASK_SEL(0, EP_OUT) | EP_MASK_SEL(0, EP_IN))) {
2832 int i;
2834 /* Handle other EP interrupts */
2835 for (i = 1; i < NUM_ENDPOINTS; i++) {
2836 if (tmp & (1 << udc->ep[i].hwep_num))
2837 udc_handle_eps(udc, &udc->ep[i]);
2841 spin_unlock(&udc->lock);
2843 return IRQ_HANDLED;
2846 static irqreturn_t lpc32xx_usb_devdma_irq(int irq, void *_udc)
2848 struct lpc32xx_udc *udc = _udc;
2850 int i;
2851 u32 tmp;
2853 spin_lock(&udc->lock);
2855 /* Handle EP DMA EOT interrupts */
2856 tmp = readl(USBD_EOTINTST(udc->udp_baseaddr)) |
2857 (readl(USBD_EPDMAST(udc->udp_baseaddr)) &
2858 readl(USBD_NDDRTINTST(udc->udp_baseaddr))) |
2859 readl(USBD_SYSERRTINTST(udc->udp_baseaddr));
2860 for (i = 1; i < NUM_ENDPOINTS; i++) {
2861 if (tmp & (1 << udc->ep[i].hwep_num))
2862 udc_handle_dma_ep(udc, &udc->ep[i]);
2865 spin_unlock(&udc->lock);
2867 return IRQ_HANDLED;
2872 * VBUS detection, pullup handler, and Gadget cable state notification
2875 static void vbus_work(struct work_struct *work)
2877 u8 value;
2878 struct lpc32xx_udc *udc = container_of(work, struct lpc32xx_udc,
2879 vbus_job);
2881 if (udc->enabled != 0) {
2882 /* Discharge VBUS real quick */
2883 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
2884 ISP1301_I2C_OTG_CONTROL_1, OTG1_VBUS_DISCHRG);
2886 /* Give VBUS some time (100mS) to discharge */
2887 msleep(100);
2889 /* Disable VBUS discharge resistor */
2890 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
2891 ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
2892 OTG1_VBUS_DISCHRG);
2894 /* Clear interrupt */
2895 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
2896 ISP1301_I2C_INTERRUPT_LATCH |
2897 ISP1301_I2C_REG_CLEAR_ADDR, ~0);
2899 /* Get the VBUS status from the transceiver */
2900 value = i2c_smbus_read_byte_data(udc->isp1301_i2c_client,
2901 ISP1301_I2C_INTERRUPT_SOURCE);
2903 /* VBUS on or off? */
2904 if (value & INT_SESS_VLD)
2905 udc->vbus = 1;
2906 else
2907 udc->vbus = 0;
2909 /* VBUS changed? */
2910 if (udc->last_vbus != udc->vbus) {
2911 udc->last_vbus = udc->vbus;
2912 lpc32xx_vbus_session(&udc->gadget, udc->vbus);
2916 /* Re-enable after completion */
2917 enable_irq(udc->udp_irq[IRQ_USB_ATX]);
2920 static irqreturn_t lpc32xx_usb_vbus_irq(int irq, void *_udc)
2922 struct lpc32xx_udc *udc = _udc;
2924 /* Defer handling of VBUS IRQ to work queue */
2925 disable_irq_nosync(udc->udp_irq[IRQ_USB_ATX]);
2926 schedule_work(&udc->vbus_job);
2928 return IRQ_HANDLED;
2931 static int lpc32xx_start(struct usb_gadget *gadget,
2932 struct usb_gadget_driver *driver)
2934 struct lpc32xx_udc *udc = to_udc(gadget);
2935 int i;
2937 if (!driver || driver->max_speed < USB_SPEED_FULL || !driver->setup) {
2938 dev_err(udc->dev, "bad parameter.\n");
2939 return -EINVAL;
2942 if (udc->driver) {
2943 dev_err(udc->dev, "UDC already has a gadget driver\n");
2944 return -EBUSY;
2947 udc->driver = driver;
2948 udc->gadget.dev.of_node = udc->dev->of_node;
2949 udc->enabled = 1;
2950 udc->selfpowered = 1;
2951 udc->vbus = 0;
2953 /* Force VBUS process once to check for cable insertion */
2954 udc->last_vbus = udc->vbus = 0;
2955 schedule_work(&udc->vbus_job);
2957 /* Do not re-enable ATX IRQ (3) */
2958 for (i = IRQ_USB_LP; i < IRQ_USB_ATX; i++)
2959 enable_irq(udc->udp_irq[i]);
2961 return 0;
2964 static int lpc32xx_stop(struct usb_gadget *gadget,
2965 struct usb_gadget_driver *driver)
2967 int i;
2968 struct lpc32xx_udc *udc = to_udc(gadget);
2970 if (!driver || driver != udc->driver)
2971 return -EINVAL;
2973 for (i = IRQ_USB_LP; i <= IRQ_USB_ATX; i++)
2974 disable_irq(udc->udp_irq[i]);
2976 if (udc->clocked) {
2977 spin_lock(&udc->lock);
2978 stop_activity(udc);
2979 spin_unlock(&udc->lock);
2982 * Wait for all the endpoints to disable,
2983 * before disabling clocks. Don't wait if
2984 * endpoints are not enabled.
2986 if (atomic_read(&udc->enabled_ep_cnt))
2987 wait_event_interruptible(udc->ep_disable_wait_queue,
2988 (atomic_read(&udc->enabled_ep_cnt) == 0));
2990 spin_lock(&udc->lock);
2991 udc_clk_set(udc, 0);
2992 spin_unlock(&udc->lock);
2995 udc->enabled = 0;
2996 udc->driver = NULL;
2998 return 0;
3001 static void lpc32xx_udc_shutdown(struct platform_device *dev)
3003 /* Force disconnect on reboot */
3004 struct lpc32xx_udc *udc = platform_get_drvdata(dev);
3006 pullup(udc, 0);
3010 * Callbacks to be overridden by options passed via OF (TODO)
3013 static void lpc32xx_usbd_conn_chg(int conn)
3015 /* Do nothing, it might be nice to enable an LED
3016 * based on conn state being !0 */
3019 static void lpc32xx_usbd_susp_chg(int susp)
3021 /* Device suspend if susp != 0 */
3024 static void lpc32xx_rmwkup_chg(int remote_wakup_enable)
3026 /* Enable or disable USB remote wakeup */
3029 struct lpc32xx_usbd_cfg lpc32xx_usbddata = {
3030 .vbus_drv_pol = 0,
3031 .conn_chgb = &lpc32xx_usbd_conn_chg,
3032 .susp_chgb = &lpc32xx_usbd_susp_chg,
3033 .rmwk_chgb = &lpc32xx_rmwkup_chg,
3037 static u64 lpc32xx_usbd_dmamask = ~(u32) 0x7F;
3039 static int __init lpc32xx_udc_probe(struct platform_device *pdev)
3041 struct device *dev = &pdev->dev;
3042 struct lpc32xx_udc *udc;
3043 int retval, i;
3044 struct resource *res;
3045 dma_addr_t dma_handle;
3046 struct device_node *isp1301_node;
3048 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
3049 if (!udc)
3050 return -ENOMEM;
3052 memcpy(udc, &controller_template, sizeof(*udc));
3053 for (i = 0; i <= 15; i++)
3054 udc->ep[i].udc = udc;
3055 udc->gadget.ep0 = &udc->ep[0].ep;
3057 /* init software state */
3058 udc->gadget.dev.parent = dev;
3059 udc->pdev = pdev;
3060 udc->dev = &pdev->dev;
3061 udc->enabled = 0;
3063 if (pdev->dev.of_node) {
3064 isp1301_node = of_parse_phandle(pdev->dev.of_node,
3065 "transceiver", 0);
3066 } else {
3067 isp1301_node = NULL;
3070 udc->isp1301_i2c_client = isp1301_get_client(isp1301_node);
3071 if (!udc->isp1301_i2c_client) {
3072 retval = -EPROBE_DEFER;
3073 goto phy_fail;
3076 dev_info(udc->dev, "ISP1301 I2C device at address 0x%x\n",
3077 udc->isp1301_i2c_client->addr);
3079 pdev->dev.dma_mask = &lpc32xx_usbd_dmamask;
3080 retval = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
3081 if (retval)
3082 goto resource_fail;
3084 udc->board = &lpc32xx_usbddata;
3087 * Resources are mapped as follows:
3088 * IORESOURCE_MEM, base address and size of USB space
3089 * IORESOURCE_IRQ, USB device low priority interrupt number
3090 * IORESOURCE_IRQ, USB device high priority interrupt number
3091 * IORESOURCE_IRQ, USB device interrupt number
3092 * IORESOURCE_IRQ, USB transceiver interrupt number
3094 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3095 if (!res) {
3096 retval = -ENXIO;
3097 goto resource_fail;
3100 spin_lock_init(&udc->lock);
3102 /* Get IRQs */
3103 for (i = 0; i < 4; i++) {
3104 udc->udp_irq[i] = platform_get_irq(pdev, i);
3105 if (udc->udp_irq[i] < 0) {
3106 dev_err(udc->dev,
3107 "irq resource %d not available!\n", i);
3108 retval = udc->udp_irq[i];
3109 goto irq_fail;
3113 udc->io_p_start = res->start;
3114 udc->io_p_size = resource_size(res);
3115 if (!request_mem_region(udc->io_p_start, udc->io_p_size, driver_name)) {
3116 dev_err(udc->dev, "someone's using UDC memory\n");
3117 retval = -EBUSY;
3118 goto request_mem_region_fail;
3121 udc->udp_baseaddr = ioremap(udc->io_p_start, udc->io_p_size);
3122 if (!udc->udp_baseaddr) {
3123 retval = -ENOMEM;
3124 dev_err(udc->dev, "IO map failure\n");
3125 goto io_map_fail;
3128 /* Enable AHB slave USB clock, needed for further USB clock control */
3129 writel(USB_SLAVE_HCLK_EN | (1 << 19), USB_CTRL);
3131 /* Get required clocks */
3132 udc->usb_pll_clk = clk_get(&pdev->dev, "ck_pll5");
3133 if (IS_ERR(udc->usb_pll_clk)) {
3134 dev_err(udc->dev, "failed to acquire USB PLL\n");
3135 retval = PTR_ERR(udc->usb_pll_clk);
3136 goto pll_get_fail;
3138 udc->usb_slv_clk = clk_get(&pdev->dev, "ck_usbd");
3139 if (IS_ERR(udc->usb_slv_clk)) {
3140 dev_err(udc->dev, "failed to acquire USB device clock\n");
3141 retval = PTR_ERR(udc->usb_slv_clk);
3142 goto usb_clk_get_fail;
3144 udc->usb_otg_clk = clk_get(&pdev->dev, "ck_usb_otg");
3145 if (IS_ERR(udc->usb_otg_clk)) {
3146 dev_err(udc->dev, "failed to acquire USB otg clock\n");
3147 retval = PTR_ERR(udc->usb_otg_clk);
3148 goto usb_otg_clk_get_fail;
3151 /* Setup PLL clock to 48MHz */
3152 retval = clk_enable(udc->usb_pll_clk);
3153 if (retval < 0) {
3154 dev_err(udc->dev, "failed to start USB PLL\n");
3155 goto pll_enable_fail;
3158 retval = clk_set_rate(udc->usb_pll_clk, 48000);
3159 if (retval < 0) {
3160 dev_err(udc->dev, "failed to set USB clock rate\n");
3161 goto pll_set_fail;
3164 writel(readl(USB_CTRL) | USB_DEV_NEED_CLK_EN, USB_CTRL);
3166 /* Enable USB device clock */
3167 retval = clk_enable(udc->usb_slv_clk);
3168 if (retval < 0) {
3169 dev_err(udc->dev, "failed to start USB device clock\n");
3170 goto usb_clk_enable_fail;
3173 /* Enable USB OTG clock */
3174 retval = clk_enable(udc->usb_otg_clk);
3175 if (retval < 0) {
3176 dev_err(udc->dev, "failed to start USB otg clock\n");
3177 goto usb_otg_clk_enable_fail;
3180 /* Setup deferred workqueue data */
3181 udc->poweron = udc->pullup = 0;
3182 INIT_WORK(&udc->pullup_job, pullup_work);
3183 INIT_WORK(&udc->vbus_job, vbus_work);
3184 #ifdef CONFIG_PM
3185 INIT_WORK(&udc->power_job, power_work);
3186 #endif
3188 /* All clocks are now on */
3189 udc->clocked = 1;
3191 isp1301_udc_configure(udc);
3192 /* Allocate memory for the UDCA */
3193 udc->udca_v_base = dma_alloc_coherent(&pdev->dev, UDCA_BUFF_SIZE,
3194 &dma_handle,
3195 (GFP_KERNEL | GFP_DMA));
3196 if (!udc->udca_v_base) {
3197 dev_err(udc->dev, "error getting UDCA region\n");
3198 retval = -ENOMEM;
3199 goto i2c_fail;
3201 udc->udca_p_base = dma_handle;
3202 dev_dbg(udc->dev, "DMA buffer(0x%x bytes), P:0x%08x, V:0x%p\n",
3203 UDCA_BUFF_SIZE, udc->udca_p_base, udc->udca_v_base);
3205 /* Setup the DD DMA memory pool */
3206 udc->dd_cache = dma_pool_create("udc_dd", udc->dev,
3207 sizeof(struct lpc32xx_usbd_dd_gad),
3208 sizeof(u32), 0);
3209 if (!udc->dd_cache) {
3210 dev_err(udc->dev, "error getting DD DMA region\n");
3211 retval = -ENOMEM;
3212 goto dma_alloc_fail;
3215 /* Clear USB peripheral and initialize gadget endpoints */
3216 udc_disable(udc);
3217 udc_reinit(udc);
3219 /* Request IRQs - low and high priority USB device IRQs are routed to
3220 * the same handler, while the DMA interrupt is routed elsewhere */
3221 retval = request_irq(udc->udp_irq[IRQ_USB_LP], lpc32xx_usb_lp_irq,
3222 0, "udc_lp", udc);
3223 if (retval < 0) {
3224 dev_err(udc->dev, "LP request irq %d failed\n",
3225 udc->udp_irq[IRQ_USB_LP]);
3226 goto irq_lp_fail;
3228 retval = request_irq(udc->udp_irq[IRQ_USB_HP], lpc32xx_usb_hp_irq,
3229 0, "udc_hp", udc);
3230 if (retval < 0) {
3231 dev_err(udc->dev, "HP request irq %d failed\n",
3232 udc->udp_irq[IRQ_USB_HP]);
3233 goto irq_hp_fail;
3236 retval = request_irq(udc->udp_irq[IRQ_USB_DEVDMA],
3237 lpc32xx_usb_devdma_irq, 0, "udc_dma", udc);
3238 if (retval < 0) {
3239 dev_err(udc->dev, "DEV request irq %d failed\n",
3240 udc->udp_irq[IRQ_USB_DEVDMA]);
3241 goto irq_dev_fail;
3244 /* The transceiver interrupt is used for VBUS detection and will
3245 kick off the VBUS handler function */
3246 retval = request_irq(udc->udp_irq[IRQ_USB_ATX], lpc32xx_usb_vbus_irq,
3247 0, "udc_otg", udc);
3248 if (retval < 0) {
3249 dev_err(udc->dev, "VBUS request irq %d failed\n",
3250 udc->udp_irq[IRQ_USB_ATX]);
3251 goto irq_xcvr_fail;
3254 /* Initialize wait queue */
3255 init_waitqueue_head(&udc->ep_disable_wait_queue);
3256 atomic_set(&udc->enabled_ep_cnt, 0);
3258 /* Keep all IRQs disabled until GadgetFS starts up */
3259 for (i = IRQ_USB_LP; i <= IRQ_USB_ATX; i++)
3260 disable_irq(udc->udp_irq[i]);
3262 retval = usb_add_gadget_udc(dev, &udc->gadget);
3263 if (retval < 0)
3264 goto add_gadget_fail;
3266 dev_set_drvdata(dev, udc);
3267 device_init_wakeup(dev, 1);
3268 create_debug_file(udc);
3270 /* Disable clocks for now */
3271 udc_clk_set(udc, 0);
3273 dev_info(udc->dev, "%s version %s\n", driver_name, DRIVER_VERSION);
3274 return 0;
3276 add_gadget_fail:
3277 free_irq(udc->udp_irq[IRQ_USB_ATX], udc);
3278 irq_xcvr_fail:
3279 free_irq(udc->udp_irq[IRQ_USB_DEVDMA], udc);
3280 irq_dev_fail:
3281 free_irq(udc->udp_irq[IRQ_USB_HP], udc);
3282 irq_hp_fail:
3283 free_irq(udc->udp_irq[IRQ_USB_LP], udc);
3284 irq_lp_fail:
3285 dma_pool_destroy(udc->dd_cache);
3286 dma_alloc_fail:
3287 dma_free_coherent(&pdev->dev, UDCA_BUFF_SIZE,
3288 udc->udca_v_base, udc->udca_p_base);
3289 i2c_fail:
3290 clk_disable(udc->usb_otg_clk);
3291 usb_otg_clk_enable_fail:
3292 clk_disable(udc->usb_slv_clk);
3293 usb_clk_enable_fail:
3294 pll_set_fail:
3295 clk_disable(udc->usb_pll_clk);
3296 pll_enable_fail:
3297 clk_put(udc->usb_otg_clk);
3298 usb_otg_clk_get_fail:
3299 clk_put(udc->usb_slv_clk);
3300 usb_clk_get_fail:
3301 clk_put(udc->usb_pll_clk);
3302 pll_get_fail:
3303 iounmap(udc->udp_baseaddr);
3304 io_map_fail:
3305 release_mem_region(udc->io_p_start, udc->io_p_size);
3306 dev_err(udc->dev, "%s probe failed, %d\n", driver_name, retval);
3307 request_mem_region_fail:
3308 irq_fail:
3309 resource_fail:
3310 phy_fail:
3311 kfree(udc);
3312 return retval;
3315 static int lpc32xx_udc_remove(struct platform_device *pdev)
3317 struct lpc32xx_udc *udc = platform_get_drvdata(pdev);
3319 usb_del_gadget_udc(&udc->gadget);
3320 if (udc->driver)
3321 return -EBUSY;
3323 udc_clk_set(udc, 1);
3324 udc_disable(udc);
3325 pullup(udc, 0);
3327 free_irq(udc->udp_irq[IRQ_USB_ATX], udc);
3329 device_init_wakeup(&pdev->dev, 0);
3330 remove_debug_file(udc);
3332 dma_pool_destroy(udc->dd_cache);
3333 dma_free_coherent(&pdev->dev, UDCA_BUFF_SIZE,
3334 udc->udca_v_base, udc->udca_p_base);
3335 free_irq(udc->udp_irq[IRQ_USB_DEVDMA], udc);
3336 free_irq(udc->udp_irq[IRQ_USB_HP], udc);
3337 free_irq(udc->udp_irq[IRQ_USB_LP], udc);
3339 clk_disable(udc->usb_otg_clk);
3340 clk_put(udc->usb_otg_clk);
3341 clk_disable(udc->usb_slv_clk);
3342 clk_put(udc->usb_slv_clk);
3343 clk_disable(udc->usb_pll_clk);
3344 clk_put(udc->usb_pll_clk);
3345 iounmap(udc->udp_baseaddr);
3346 release_mem_region(udc->io_p_start, udc->io_p_size);
3347 kfree(udc);
3349 return 0;
3352 #ifdef CONFIG_PM
3353 static int lpc32xx_udc_suspend(struct platform_device *pdev, pm_message_t mesg)
3355 struct lpc32xx_udc *udc = platform_get_drvdata(pdev);
3357 if (udc->clocked) {
3358 /* Power down ISP */
3359 udc->poweron = 0;
3360 isp1301_set_powerstate(udc, 0);
3362 /* Disable clocking */
3363 udc_clk_set(udc, 0);
3365 /* Keep clock flag on, so we know to re-enable clocks
3366 on resume */
3367 udc->clocked = 1;
3369 /* Kill global USB clock */
3370 clk_disable(udc->usb_slv_clk);
3373 return 0;
3376 static int lpc32xx_udc_resume(struct platform_device *pdev)
3378 struct lpc32xx_udc *udc = platform_get_drvdata(pdev);
3380 if (udc->clocked) {
3381 /* Enable global USB clock */
3382 clk_enable(udc->usb_slv_clk);
3384 /* Enable clocking */
3385 udc_clk_set(udc, 1);
3387 /* ISP back to normal power mode */
3388 udc->poweron = 1;
3389 isp1301_set_powerstate(udc, 1);
3392 return 0;
3394 #else
3395 #define lpc32xx_udc_suspend NULL
3396 #define lpc32xx_udc_resume NULL
3397 #endif
3399 #ifdef CONFIG_OF
3400 static struct of_device_id lpc32xx_udc_of_match[] = {
3401 { .compatible = "nxp,lpc3220-udc", },
3402 { },
3404 MODULE_DEVICE_TABLE(of, lpc32xx_udc_of_match);
3405 #endif
3407 static struct platform_driver lpc32xx_udc_driver = {
3408 .remove = lpc32xx_udc_remove,
3409 .shutdown = lpc32xx_udc_shutdown,
3410 .suspend = lpc32xx_udc_suspend,
3411 .resume = lpc32xx_udc_resume,
3412 .driver = {
3413 .name = (char *) driver_name,
3414 .owner = THIS_MODULE,
3415 .of_match_table = of_match_ptr(lpc32xx_udc_of_match),
3419 module_platform_driver_probe(lpc32xx_udc_driver, lpc32xx_udc_probe);
3421 MODULE_DESCRIPTION("LPC32XX udc driver");
3422 MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>");
3423 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
3424 MODULE_LICENSE("GPL");
3425 MODULE_ALIAS("platform:lpc32xx_udc");