2 * USB Gadget driver for LPC32xx
5 * Kevin Wells <kevin.wells@nxp.com>
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
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/clk.h>
32 #include <linux/delay.h>
33 #include <linux/dma-mapping.h>
34 #include <linux/dmapool.h>
35 #include <linux/i2c.h>
36 #include <linux/interrupt.h>
37 #include <linux/module.h>
39 #include <linux/platform_device.h>
40 #include <linux/proc_fs.h>
41 #include <linux/slab.h>
42 #include <linux/usb/ch9.h>
43 #include <linux/usb/gadget.h>
44 #include <linux/usb/isp1301.h>
46 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
47 #include <linux/debugfs.h>
48 #include <linux/seq_file.h>
51 #include <mach/hardware.h>
54 * USB device configuration structure
56 typedef void (*usc_chg_event)(int);
57 struct lpc32xx_usbd_cfg {
58 int vbus_drv_pol; /* 0=active low drive for VBUS via ISP1301 */
59 usc_chg_event conn_chgb; /* Connection change event (optional) */
60 usc_chg_event susp_chgb; /* Suspend/resume event (optional) */
61 usc_chg_event rmwk_chgb; /* Enable/disable remote wakeup */
65 * controller driver data structures
68 /* 16 endpoints (not to be confused with 32 hardware endpoints) */
69 #define NUM_ENDPOINTS 16
72 * IRQ indices make reading the code a little easier
76 #define IRQ_USB_DEVDMA 2
79 #define EP_OUT 0 /* RX (from host) */
80 #define EP_IN 1 /* TX (to host) */
82 /* Returns the interrupt mask for the selected hardware endpoint */
83 #define EP_MASK_SEL(ep, dir) (1 << (((ep) * 2) + dir))
91 #define WAIT_FOR_SETUP 0 /* Wait for setup packet */
92 #define DATA_IN 1 /* Expect dev->host transfer */
93 #define DATA_OUT 2 /* Expect host->dev transfer */
95 /* DD (DMA Descriptor) structure, requires word alignment, this is already
96 * defined in the LPC32XX USB device header file, but this version is slightly
97 * modified to tag some work data with each DMA descriptor. */
98 struct lpc32xx_usbd_dd_gad {
103 u32 dd_iso_ps_mem_addr;
105 u32 iso_status[6]; /* 5 spare */
110 * Logical endpoint structure
114 struct list_head queue;
115 struct lpc32xx_udc *udc;
117 u32 hwep_num_base; /* Physical hardware EP */
118 u32 hwep_num; /* Maps to hardware endpoint */
132 * Common UDC structure
135 struct usb_gadget gadget;
136 struct usb_gadget_driver *driver;
137 struct platform_device *pdev;
141 struct i2c_client *isp1301_i2c_client;
143 /* Board and device specific */
144 struct lpc32xx_usbd_cfg *board;
147 void __iomem *udp_baseaddr;
149 struct clk *usb_slv_clk;
154 struct dma_pool *dd_cache;
156 /* Common EP and control data */
158 u32 enabled_hwepints;
162 /* VBUS detection, pullup, and power flags */
168 /* Work queues related to I2C support */
169 struct work_struct pullup_job;
170 struct work_struct vbus_job;
171 struct work_struct power_job;
173 /* USB device peripheral - various */
174 struct lpc32xx_ep ep[NUM_ENDPOINTS];
179 atomic_t enabled_ep_cnt;
180 wait_queue_head_t ep_disable_wait_queue;
186 struct lpc32xx_request {
187 struct usb_request req;
188 struct list_head queue;
189 struct lpc32xx_usbd_dd_gad *dd_desc_ptr;
194 static inline struct lpc32xx_udc *to_udc(struct usb_gadget *g)
196 return container_of(g, struct lpc32xx_udc, gadget);
199 #define ep_dbg(epp, fmt, arg...) \
200 dev_dbg(epp->udc->dev, "%s: " fmt, __func__, ## arg)
201 #define ep_err(epp, fmt, arg...) \
202 dev_err(epp->udc->dev, "%s: " fmt, __func__, ## arg)
203 #define ep_info(epp, fmt, arg...) \
204 dev_info(epp->udc->dev, "%s: " fmt, __func__, ## arg)
205 #define ep_warn(epp, fmt, arg...) \
206 dev_warn(epp->udc->dev, "%s:" fmt, __func__, ## arg)
208 #define UDCA_BUFF_SIZE (128)
210 /**********************************************************************
211 * USB device controller register offsets
212 **********************************************************************/
214 #define USBD_DEVINTST(x) ((x) + 0x200)
215 #define USBD_DEVINTEN(x) ((x) + 0x204)
216 #define USBD_DEVINTCLR(x) ((x) + 0x208)
217 #define USBD_DEVINTSET(x) ((x) + 0x20C)
218 #define USBD_CMDCODE(x) ((x) + 0x210)
219 #define USBD_CMDDATA(x) ((x) + 0x214)
220 #define USBD_RXDATA(x) ((x) + 0x218)
221 #define USBD_TXDATA(x) ((x) + 0x21C)
222 #define USBD_RXPLEN(x) ((x) + 0x220)
223 #define USBD_TXPLEN(x) ((x) + 0x224)
224 #define USBD_CTRL(x) ((x) + 0x228)
225 #define USBD_DEVINTPRI(x) ((x) + 0x22C)
226 #define USBD_EPINTST(x) ((x) + 0x230)
227 #define USBD_EPINTEN(x) ((x) + 0x234)
228 #define USBD_EPINTCLR(x) ((x) + 0x238)
229 #define USBD_EPINTSET(x) ((x) + 0x23C)
230 #define USBD_EPINTPRI(x) ((x) + 0x240)
231 #define USBD_REEP(x) ((x) + 0x244)
232 #define USBD_EPIND(x) ((x) + 0x248)
233 #define USBD_EPMAXPSIZE(x) ((x) + 0x24C)
234 /* DMA support registers only below */
235 /* Set, clear, or get enabled state of the DMA request status. If
236 * enabled, an IN or OUT token will start a DMA transfer for the EP */
237 #define USBD_DMARST(x) ((x) + 0x250)
238 #define USBD_DMARCLR(x) ((x) + 0x254)
239 #define USBD_DMARSET(x) ((x) + 0x258)
240 /* DMA UDCA head pointer */
241 #define USBD_UDCAH(x) ((x) + 0x280)
242 /* EP DMA status, enable, and disable. This is used to specifically
243 * enabled or disable DMA for a specific EP */
244 #define USBD_EPDMAST(x) ((x) + 0x284)
245 #define USBD_EPDMAEN(x) ((x) + 0x288)
246 #define USBD_EPDMADIS(x) ((x) + 0x28C)
247 /* DMA master interrupts enable and pending interrupts */
248 #define USBD_DMAINTST(x) ((x) + 0x290)
249 #define USBD_DMAINTEN(x) ((x) + 0x294)
250 /* DMA end of transfer interrupt enable, disable, status */
251 #define USBD_EOTINTST(x) ((x) + 0x2A0)
252 #define USBD_EOTINTCLR(x) ((x) + 0x2A4)
253 #define USBD_EOTINTSET(x) ((x) + 0x2A8)
254 /* New DD request interrupt enable, disable, status */
255 #define USBD_NDDRTINTST(x) ((x) + 0x2AC)
256 #define USBD_NDDRTINTCLR(x) ((x) + 0x2B0)
257 #define USBD_NDDRTINTSET(x) ((x) + 0x2B4)
258 /* DMA error interrupt enable, disable, status */
259 #define USBD_SYSERRTINTST(x) ((x) + 0x2B8)
260 #define USBD_SYSERRTINTCLR(x) ((x) + 0x2BC)
261 #define USBD_SYSERRTINTSET(x) ((x) + 0x2C0)
263 /**********************************************************************
264 * USBD_DEVINTST/USBD_DEVINTEN/USBD_DEVINTCLR/USBD_DEVINTSET/
265 * USBD_DEVINTPRI register definitions
266 **********************************************************************/
267 #define USBD_ERR_INT (1 << 9)
268 #define USBD_EP_RLZED (1 << 8)
269 #define USBD_TXENDPKT (1 << 7)
270 #define USBD_RXENDPKT (1 << 6)
271 #define USBD_CDFULL (1 << 5)
272 #define USBD_CCEMPTY (1 << 4)
273 #define USBD_DEV_STAT (1 << 3)
274 #define USBD_EP_SLOW (1 << 2)
275 #define USBD_EP_FAST (1 << 1)
276 #define USBD_FRAME (1 << 0)
278 /**********************************************************************
279 * USBD_EPINTST/USBD_EPINTEN/USBD_EPINTCLR/USBD_EPINTSET/
280 * USBD_EPINTPRI register definitions
281 **********************************************************************/
282 /* End point selection macro (RX) */
283 #define USBD_RX_EP_SEL(e) (1 << ((e) << 1))
285 /* End point selection macro (TX) */
286 #define USBD_TX_EP_SEL(e) (1 << (((e) << 1) + 1))
288 /**********************************************************************
289 * USBD_REEP/USBD_DMARST/USBD_DMARCLR/USBD_DMARSET/USBD_EPDMAST/
290 * USBD_EPDMAEN/USBD_EPDMADIS/
291 * USBD_NDDRTINTST/USBD_NDDRTINTCLR/USBD_NDDRTINTSET/
292 * USBD_EOTINTST/USBD_EOTINTCLR/USBD_EOTINTSET/
293 * USBD_SYSERRTINTST/USBD_SYSERRTINTCLR/USBD_SYSERRTINTSET
294 * register definitions
295 **********************************************************************/
296 /* Endpoint selection macro */
297 #define USBD_EP_SEL(e) (1 << (e))
299 /**********************************************************************
300 * SBD_DMAINTST/USBD_DMAINTEN
301 **********************************************************************/
302 #define USBD_SYS_ERR_INT (1 << 2)
303 #define USBD_NEW_DD_INT (1 << 1)
304 #define USBD_EOT_INT (1 << 0)
306 /**********************************************************************
307 * USBD_RXPLEN register definitions
308 **********************************************************************/
309 #define USBD_PKT_RDY (1 << 11)
310 #define USBD_DV (1 << 10)
311 #define USBD_PK_LEN_MASK 0x3FF
313 /**********************************************************************
314 * USBD_CTRL register definitions
315 **********************************************************************/
316 #define USBD_LOG_ENDPOINT(e) ((e) << 2)
317 #define USBD_WR_EN (1 << 1)
318 #define USBD_RD_EN (1 << 0)
320 /**********************************************************************
321 * USBD_CMDCODE register definitions
322 **********************************************************************/
323 #define USBD_CMD_CODE(c) ((c) << 16)
324 #define USBD_CMD_PHASE(p) ((p) << 8)
326 /**********************************************************************
327 * USBD_DMARST/USBD_DMARCLR/USBD_DMARSET register definitions
328 **********************************************************************/
329 #define USBD_DMAEP(e) (1 << (e))
331 /* DD (DMA Descriptor) structure, requires word alignment */
332 struct lpc32xx_usbd_dd {
337 u32 dd_iso_ps_mem_addr;
340 /* dd_setup bit defines */
341 #define DD_SETUP_ATLE_DMA_MODE 0x01
342 #define DD_SETUP_NEXT_DD_VALID 0x04
343 #define DD_SETUP_ISO_EP 0x10
344 #define DD_SETUP_PACKETLEN(n) (((n) & 0x7FF) << 5)
345 #define DD_SETUP_DMALENBYTES(n) (((n) & 0xFFFF) << 16)
347 /* dd_status bit defines */
348 #define DD_STATUS_DD_RETIRED 0x01
349 #define DD_STATUS_STS_MASK 0x1E
350 #define DD_STATUS_STS_NS 0x00 /* Not serviced */
351 #define DD_STATUS_STS_BS 0x02 /* Being serviced */
352 #define DD_STATUS_STS_NC 0x04 /* Normal completion */
353 #define DD_STATUS_STS_DUR 0x06 /* Data underrun (short packet) */
354 #define DD_STATUS_STS_DOR 0x08 /* Data overrun */
355 #define DD_STATUS_STS_SE 0x12 /* System error */
356 #define DD_STATUS_PKT_VAL 0x20 /* Packet valid */
357 #define DD_STATUS_LSB_EX 0x40 /* LS byte extracted (ATLE) */
358 #define DD_STATUS_MSB_EX 0x80 /* MS byte extracted (ATLE) */
359 #define DD_STATUS_MLEN(n) (((n) >> 8) & 0x3F)
360 #define DD_STATUS_CURDMACNT(n) (((n) >> 16) & 0xFFFF)
364 * Protocol engine bits below
367 /* Device Interrupt Bit Definitions */
368 #define FRAME_INT 0x00000001
369 #define EP_FAST_INT 0x00000002
370 #define EP_SLOW_INT 0x00000004
371 #define DEV_STAT_INT 0x00000008
372 #define CCEMTY_INT 0x00000010
373 #define CDFULL_INT 0x00000020
374 #define RxENDPKT_INT 0x00000040
375 #define TxENDPKT_INT 0x00000080
376 #define EP_RLZED_INT 0x00000100
377 #define ERR_INT 0x00000200
379 /* Rx & Tx Packet Length Definitions */
380 #define PKT_LNGTH_MASK 0x000003FF
381 #define PKT_DV 0x00000400
382 #define PKT_RDY 0x00000800
384 /* USB Control Definitions */
385 #define CTRL_RD_EN 0x00000001
386 #define CTRL_WR_EN 0x00000002
389 #define CMD_SET_ADDR 0x00D00500
390 #define CMD_CFG_DEV 0x00D80500
391 #define CMD_SET_MODE 0x00F30500
392 #define CMD_RD_FRAME 0x00F50500
393 #define DAT_RD_FRAME 0x00F50200
394 #define CMD_RD_TEST 0x00FD0500
395 #define DAT_RD_TEST 0x00FD0200
396 #define CMD_SET_DEV_STAT 0x00FE0500
397 #define CMD_GET_DEV_STAT 0x00FE0500
398 #define DAT_GET_DEV_STAT 0x00FE0200
399 #define CMD_GET_ERR_CODE 0x00FF0500
400 #define DAT_GET_ERR_CODE 0x00FF0200
401 #define CMD_RD_ERR_STAT 0x00FB0500
402 #define DAT_RD_ERR_STAT 0x00FB0200
403 #define DAT_WR_BYTE(x) (0x00000100 | ((x) << 16))
404 #define CMD_SEL_EP(x) (0x00000500 | ((x) << 16))
405 #define DAT_SEL_EP(x) (0x00000200 | ((x) << 16))
406 #define CMD_SEL_EP_CLRI(x) (0x00400500 | ((x) << 16))
407 #define DAT_SEL_EP_CLRI(x) (0x00400200 | ((x) << 16))
408 #define CMD_SET_EP_STAT(x) (0x00400500 | ((x) << 16))
409 #define CMD_CLR_BUF 0x00F20500
410 #define DAT_CLR_BUF 0x00F20200
411 #define CMD_VALID_BUF 0x00FA0500
413 /* Device Address Register Definitions */
414 #define DEV_ADDR_MASK 0x7F
417 /* Device Configure Register Definitions */
418 #define CONF_DVICE 0x01
420 /* Device Mode Register Definitions */
429 /* Device Status Register Definitions */
431 #define DEV_CON_CH 0x02
433 #define DEV_SUS_CH 0x08
436 /* Error Code Register Definitions */
437 #define ERR_EC_MASK 0x0F
440 /* Error Status Register Definitions */
442 #define ERR_UEPKT 0x02
443 #define ERR_DCRC 0x04
444 #define ERR_TIMOUT 0x08
446 #define ERR_B_OVRN 0x20
447 #define ERR_BTSTF 0x40
450 /* Endpoint Select Register Definitions */
451 #define EP_SEL_F 0x01
452 #define EP_SEL_ST 0x02
453 #define EP_SEL_STP 0x04
454 #define EP_SEL_PO 0x08
455 #define EP_SEL_EPN 0x10
456 #define EP_SEL_B_1_FULL 0x20
457 #define EP_SEL_B_2_FULL 0x40
459 /* Endpoint Status Register Definitions */
460 #define EP_STAT_ST 0x01
461 #define EP_STAT_DA 0x20
462 #define EP_STAT_RF_MO 0x40
463 #define EP_STAT_CND_ST 0x80
465 /* Clear Buffer Register Definitions */
466 #define CLR_BUF_PO 0x01
468 /* DMA Interrupt Bit Definitions */
470 #define NDD_REQ_INT 0x02
471 #define SYS_ERR_INT 0x04
473 #define DRIVER_VERSION "1.03"
474 static const char driver_name[] = "lpc32xx_udc";
478 * proc interface support
481 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
482 static char *epnames[] = {"INT", "ISO", "BULK", "CTRL"};
483 static const char debug_filename[] = "driver/udc";
485 static void proc_ep_show(struct seq_file *s, struct lpc32xx_ep *ep)
487 struct lpc32xx_request *req;
490 seq_printf(s, "%12s, maxpacket %4d %3s",
491 ep->ep.name, ep->ep.maxpacket,
492 ep->is_in ? "in" : "out");
493 seq_printf(s, " type %4s", epnames[ep->eptype]);
494 seq_printf(s, " ints: %12d", ep->totalints);
496 if (list_empty(&ep->queue))
497 seq_printf(s, "\t(queue empty)\n");
499 list_for_each_entry(req, &ep->queue, queue) {
500 u32 length = req->req.actual;
502 seq_printf(s, "\treq %p len %d/%d buf %p\n",
504 req->req.length, req->req.buf);
509 static int proc_udc_show(struct seq_file *s, void *unused)
511 struct lpc32xx_udc *udc = s->private;
512 struct lpc32xx_ep *ep;
515 seq_printf(s, "%s: version %s\n", driver_name, DRIVER_VERSION);
517 spin_lock_irqsave(&udc->lock, flags);
519 seq_printf(s, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
520 udc->vbus ? "present" : "off",
521 udc->enabled ? (udc->vbus ? "active" : "enabled") :
523 udc->gadget.is_selfpowered ? "self" : "VBUS",
524 udc->suspended ? ", suspended" : "",
525 udc->driver ? udc->driver->driver.name : "(none)");
527 if (udc->enabled && udc->vbus) {
528 proc_ep_show(s, &udc->ep[0]);
529 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list)
533 spin_unlock_irqrestore(&udc->lock, flags);
538 static int proc_udc_open(struct inode *inode, struct file *file)
540 return single_open(file, proc_udc_show, PDE_DATA(inode));
543 static const struct file_operations proc_ops = {
544 .owner = THIS_MODULE,
545 .open = proc_udc_open,
548 .release = single_release,
551 static void create_debug_file(struct lpc32xx_udc *udc)
553 udc->pde = debugfs_create_file(debug_filename, 0, NULL, udc, &proc_ops);
556 static void remove_debug_file(struct lpc32xx_udc *udc)
558 debugfs_remove(udc->pde);
562 static inline void create_debug_file(struct lpc32xx_udc *udc) {}
563 static inline void remove_debug_file(struct lpc32xx_udc *udc) {}
566 /* Primary initialization sequence for the ISP1301 transceiver */
567 static void isp1301_udc_configure(struct lpc32xx_udc *udc)
569 /* LPC32XX only supports DAT_SE0 USB mode */
570 /* This sequence is important */
572 /* Disable transparent UART mode first */
573 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
574 (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
577 /* Set full speed and SE0 mode */
578 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
579 (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), ~0);
580 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
581 ISP1301_I2C_MODE_CONTROL_1, (MC1_SPEED_REG | MC1_DAT_SE0));
584 * The PSW_OE enable bit state is reversed in the ISP1301 User's Guide
586 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
587 (ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR), ~0);
588 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
589 ISP1301_I2C_MODE_CONTROL_2, (MC2_BI_DI | MC2_SPD_SUSP_CTRL));
591 /* Driver VBUS_DRV high or low depending on board setup */
592 if (udc->board->vbus_drv_pol != 0)
593 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
594 ISP1301_I2C_OTG_CONTROL_1, OTG1_VBUS_DRV);
596 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
597 ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
600 /* Bi-directional mode with suspend control
601 * Enable both pulldowns for now - the pullup will be enable when VBUS
603 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
604 (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), ~0);
605 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
606 ISP1301_I2C_OTG_CONTROL_1,
607 (0 | OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
609 /* Discharge VBUS (just in case) */
610 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
611 ISP1301_I2C_OTG_CONTROL_1, OTG1_VBUS_DISCHRG);
613 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
614 (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
617 /* Clear and enable VBUS high edge interrupt */
618 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
619 ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
620 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
621 ISP1301_I2C_INTERRUPT_FALLING | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
622 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
623 ISP1301_I2C_INTERRUPT_FALLING, INT_VBUS_VLD);
624 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
625 ISP1301_I2C_INTERRUPT_RISING | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
626 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
627 ISP1301_I2C_INTERRUPT_RISING, INT_VBUS_VLD);
629 dev_info(udc->dev, "ISP1301 Vendor ID : 0x%04x\n",
630 i2c_smbus_read_word_data(udc->isp1301_i2c_client, 0x00));
631 dev_info(udc->dev, "ISP1301 Product ID : 0x%04x\n",
632 i2c_smbus_read_word_data(udc->isp1301_i2c_client, 0x02));
633 dev_info(udc->dev, "ISP1301 Version ID : 0x%04x\n",
634 i2c_smbus_read_word_data(udc->isp1301_i2c_client, 0x14));
637 /* Enables or disables the USB device pullup via the ISP1301 transceiver */
638 static void isp1301_pullup_set(struct lpc32xx_udc *udc)
641 /* Enable pullup for bus signalling */
642 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
643 ISP1301_I2C_OTG_CONTROL_1, OTG1_DP_PULLUP);
645 /* Enable pullup for bus signalling */
646 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
647 ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
651 static void pullup_work(struct work_struct *work)
653 struct lpc32xx_udc *udc =
654 container_of(work, struct lpc32xx_udc, pullup_job);
656 isp1301_pullup_set(udc);
659 static void isp1301_pullup_enable(struct lpc32xx_udc *udc, int en_pullup,
662 if (en_pullup == udc->pullup)
665 udc->pullup = en_pullup;
667 isp1301_pullup_set(udc);
669 /* defer slow i2c pull up setting */
670 schedule_work(&udc->pullup_job);
674 /* Powers up or down the ISP1301 transceiver */
675 static void isp1301_set_powerstate(struct lpc32xx_udc *udc, int enable)
678 /* Power up ISP1301 - this ISP1301 will automatically wakeup
679 when VBUS is detected */
680 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
681 ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR,
684 /* Power down ISP1301 */
685 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
686 ISP1301_I2C_MODE_CONTROL_2, MC2_GLOBAL_PWR_DN);
689 static void power_work(struct work_struct *work)
691 struct lpc32xx_udc *udc =
692 container_of(work, struct lpc32xx_udc, power_job);
694 isp1301_set_powerstate(udc, udc->poweron);
700 * USB protocol engine command/data read/write helper functions
703 /* Issues a single command to the USB device state machine */
704 static void udc_protocol_cmd_w(struct lpc32xx_udc *udc, u32 cmd)
709 /* EP may lock on CLRI if this read isn't done */
710 u32 tmp = readl(USBD_DEVINTST(udc->udp_baseaddr));
714 writel(USBD_CCEMPTY, USBD_DEVINTCLR(udc->udp_baseaddr));
716 /* Write command code */
717 writel(cmd, USBD_CMDCODE(udc->udp_baseaddr));
719 while (((readl(USBD_DEVINTST(udc->udp_baseaddr)) &
720 USBD_CCEMPTY) == 0) && (to > 0)) {
731 /* Issues 2 commands (or command and data) to the USB device state machine */
732 static inline void udc_protocol_cmd_data_w(struct lpc32xx_udc *udc, u32 cmd,
735 udc_protocol_cmd_w(udc, cmd);
736 udc_protocol_cmd_w(udc, data);
739 /* Issues a single command to the USB device state machine and reads
741 static u32 udc_protocol_cmd_r(struct lpc32xx_udc *udc, u32 cmd)
746 /* Write a command and read data from the protocol engine */
747 writel((USBD_CDFULL | USBD_CCEMPTY),
748 USBD_DEVINTCLR(udc->udp_baseaddr));
750 /* Write command code */
751 udc_protocol_cmd_w(udc, cmd);
753 tmp = readl(USBD_DEVINTST(udc->udp_baseaddr));
754 while ((!(readl(USBD_DEVINTST(udc->udp_baseaddr)) & USBD_CDFULL))
759 "Protocol engine didn't receive response (CDFULL)\n");
761 return readl(USBD_CMDDATA(udc->udp_baseaddr));
766 * USB device interrupt mask support functions
769 /* Enable one or more USB device interrupts */
770 static inline void uda_enable_devint(struct lpc32xx_udc *udc, u32 devmask)
772 udc->enabled_devints |= devmask;
773 writel(udc->enabled_devints, USBD_DEVINTEN(udc->udp_baseaddr));
776 /* Disable one or more USB device interrupts */
777 static inline void uda_disable_devint(struct lpc32xx_udc *udc, u32 mask)
779 udc->enabled_devints &= ~mask;
780 writel(udc->enabled_devints, USBD_DEVINTEN(udc->udp_baseaddr));
783 /* Clear one or more USB device interrupts */
784 static inline void uda_clear_devint(struct lpc32xx_udc *udc, u32 mask)
786 writel(mask, USBD_DEVINTCLR(udc->udp_baseaddr));
791 * Endpoint interrupt disable/enable functions
794 /* Enable one or more USB endpoint interrupts */
795 static void uda_enable_hwepint(struct lpc32xx_udc *udc, u32 hwep)
797 udc->enabled_hwepints |= (1 << hwep);
798 writel(udc->enabled_hwepints, USBD_EPINTEN(udc->udp_baseaddr));
801 /* Disable one or more USB endpoint interrupts */
802 static void uda_disable_hwepint(struct lpc32xx_udc *udc, u32 hwep)
804 udc->enabled_hwepints &= ~(1 << hwep);
805 writel(udc->enabled_hwepints, USBD_EPINTEN(udc->udp_baseaddr));
808 /* Clear one or more USB endpoint interrupts */
809 static inline void uda_clear_hwepint(struct lpc32xx_udc *udc, u32 hwep)
811 writel((1 << hwep), USBD_EPINTCLR(udc->udp_baseaddr));
814 /* Enable DMA for the HW channel */
815 static inline void udc_ep_dma_enable(struct lpc32xx_udc *udc, u32 hwep)
817 writel((1 << hwep), USBD_EPDMAEN(udc->udp_baseaddr));
820 /* Disable DMA for the HW channel */
821 static inline void udc_ep_dma_disable(struct lpc32xx_udc *udc, u32 hwep)
823 writel((1 << hwep), USBD_EPDMADIS(udc->udp_baseaddr));
828 * Endpoint realize/unrealize functions
831 /* Before an endpoint can be used, it needs to be realized
832 * in the USB protocol engine - this realizes the endpoint.
833 * The interrupt (FIFO or DMA) is not enabled with this function */
834 static void udc_realize_hwep(struct lpc32xx_udc *udc, u32 hwep,
839 writel(USBD_EP_RLZED, USBD_DEVINTCLR(udc->udp_baseaddr));
840 writel(hwep, USBD_EPIND(udc->udp_baseaddr));
841 udc->realized_eps |= (1 << hwep);
842 writel(udc->realized_eps, USBD_REEP(udc->udp_baseaddr));
843 writel(maxpacket, USBD_EPMAXPSIZE(udc->udp_baseaddr));
845 /* Wait until endpoint is realized in hardware */
846 while ((!(readl(USBD_DEVINTST(udc->udp_baseaddr)) &
847 USBD_EP_RLZED)) && (to > 0))
850 dev_dbg(udc->dev, "EP not correctly realized in hardware\n");
852 writel(USBD_EP_RLZED, USBD_DEVINTCLR(udc->udp_baseaddr));
855 /* Unrealize an EP */
856 static void udc_unrealize_hwep(struct lpc32xx_udc *udc, u32 hwep)
858 udc->realized_eps &= ~(1 << hwep);
859 writel(udc->realized_eps, USBD_REEP(udc->udp_baseaddr));
864 * Endpoint support functions
867 /* Select and clear endpoint interrupt */
868 static u32 udc_selep_clrint(struct lpc32xx_udc *udc, u32 hwep)
870 udc_protocol_cmd_w(udc, CMD_SEL_EP_CLRI(hwep));
871 return udc_protocol_cmd_r(udc, DAT_SEL_EP_CLRI(hwep));
874 /* Disables the endpoint in the USB protocol engine */
875 static void udc_disable_hwep(struct lpc32xx_udc *udc, u32 hwep)
877 udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(hwep),
878 DAT_WR_BYTE(EP_STAT_DA));
881 /* Stalls the endpoint - endpoint will return STALL */
882 static void udc_stall_hwep(struct lpc32xx_udc *udc, u32 hwep)
884 udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(hwep),
885 DAT_WR_BYTE(EP_STAT_ST));
888 /* Clear stall or reset endpoint */
889 static void udc_clrstall_hwep(struct lpc32xx_udc *udc, u32 hwep)
891 udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(hwep),
895 /* Select an endpoint for endpoint status, clear, validate */
896 static void udc_select_hwep(struct lpc32xx_udc *udc, u32 hwep)
898 udc_protocol_cmd_w(udc, CMD_SEL_EP(hwep));
903 * Endpoint buffer management functions
906 /* Clear the current endpoint's buffer */
907 static void udc_clr_buffer_hwep(struct lpc32xx_udc *udc, u32 hwep)
909 udc_select_hwep(udc, hwep);
910 udc_protocol_cmd_w(udc, CMD_CLR_BUF);
913 /* Validate the current endpoint's buffer */
914 static void udc_val_buffer_hwep(struct lpc32xx_udc *udc, u32 hwep)
916 udc_select_hwep(udc, hwep);
917 udc_protocol_cmd_w(udc, CMD_VALID_BUF);
920 static inline u32 udc_clearep_getsts(struct lpc32xx_udc *udc, u32 hwep)
922 /* Clear EP interrupt */
923 uda_clear_hwepint(udc, hwep);
924 return udc_selep_clrint(udc, hwep);
932 /* Allocate a DMA Descriptor */
933 static struct lpc32xx_usbd_dd_gad *udc_dd_alloc(struct lpc32xx_udc *udc)
936 struct lpc32xx_usbd_dd_gad *dd;
938 dd = (struct lpc32xx_usbd_dd_gad *) dma_pool_alloc(
939 udc->dd_cache, (GFP_KERNEL | GFP_DMA), &dma);
946 /* Free a DMA Descriptor */
947 static void udc_dd_free(struct lpc32xx_udc *udc, struct lpc32xx_usbd_dd_gad *dd)
949 dma_pool_free(udc->dd_cache, dd, dd->this_dma);
954 * USB setup and shutdown functions
957 /* Enables or disables most of the USB system clocks when low power mode is
958 * needed. Clocks are typically started on a connection event, and disabled
959 * when a cable is disconnected */
960 static void udc_clk_set(struct lpc32xx_udc *udc, int enable)
967 clk_prepare_enable(udc->usb_slv_clk);
973 clk_disable_unprepare(udc->usb_slv_clk);
977 /* Set/reset USB device address */
978 static void udc_set_address(struct lpc32xx_udc *udc, u32 addr)
980 /* Address will be latched at the end of the status phase, or
981 latched immediately if function is called twice */
982 udc_protocol_cmd_data_w(udc, CMD_SET_ADDR,
983 DAT_WR_BYTE(DEV_EN | addr));
986 /* Setup up a IN request for DMA transfer - this consists of determining the
987 * list of DMA addresses for the transfer, allocating DMA Descriptors,
988 * installing the DD into the UDCA, and then enabling the DMA for that EP */
989 static int udc_ep_in_req_dma(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
991 struct lpc32xx_request *req;
992 u32 hwep = ep->hwep_num;
996 /* There will always be a request waiting here */
997 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
999 /* Place the DD Descriptor into the UDCA */
1000 udc->udca_v_base[hwep] = req->dd_desc_ptr->this_dma;
1002 /* Enable DMA and interrupt for the HW EP */
1003 udc_ep_dma_enable(udc, hwep);
1005 /* Clear ZLP if last packet is not of MAXP size */
1006 if (req->req.length % ep->ep.maxpacket)
1012 /* Setup up a OUT request for DMA transfer - this consists of determining the
1013 * list of DMA addresses for the transfer, allocating DMA Descriptors,
1014 * installing the DD into the UDCA, and then enabling the DMA for that EP */
1015 static int udc_ep_out_req_dma(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
1017 struct lpc32xx_request *req;
1018 u32 hwep = ep->hwep_num;
1020 ep->req_pending = 1;
1022 /* There will always be a request waiting here */
1023 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
1025 /* Place the DD Descriptor into the UDCA */
1026 udc->udca_v_base[hwep] = req->dd_desc_ptr->this_dma;
1028 /* Enable DMA and interrupt for the HW EP */
1029 udc_ep_dma_enable(udc, hwep);
1033 static void udc_disable(struct lpc32xx_udc *udc)
1037 /* Disable device */
1038 udc_protocol_cmd_data_w(udc, CMD_CFG_DEV, DAT_WR_BYTE(0));
1039 udc_protocol_cmd_data_w(udc, CMD_SET_DEV_STAT, DAT_WR_BYTE(0));
1041 /* Disable all device interrupts (including EP0) */
1042 uda_disable_devint(udc, 0x3FF);
1044 /* Disable and reset all endpoint interrupts */
1045 for (i = 0; i < 32; i++) {
1046 uda_disable_hwepint(udc, i);
1047 uda_clear_hwepint(udc, i);
1048 udc_disable_hwep(udc, i);
1049 udc_unrealize_hwep(udc, i);
1050 udc->udca_v_base[i] = 0;
1052 /* Disable and clear all interrupts and DMA */
1053 udc_ep_dma_disable(udc, i);
1054 writel((1 << i), USBD_EOTINTCLR(udc->udp_baseaddr));
1055 writel((1 << i), USBD_NDDRTINTCLR(udc->udp_baseaddr));
1056 writel((1 << i), USBD_SYSERRTINTCLR(udc->udp_baseaddr));
1057 writel((1 << i), USBD_DMARCLR(udc->udp_baseaddr));
1060 /* Disable DMA interrupts */
1061 writel(0, USBD_DMAINTEN(udc->udp_baseaddr));
1063 writel(0, USBD_UDCAH(udc->udp_baseaddr));
1066 static void udc_enable(struct lpc32xx_udc *udc)
1069 struct lpc32xx_ep *ep = &udc->ep[0];
1071 /* Start with known state */
1075 udc_protocol_cmd_data_w(udc, CMD_SET_DEV_STAT, DAT_WR_BYTE(DEV_CON));
1077 /* EP interrupts on high priority, FRAME interrupt on low priority */
1078 writel(USBD_EP_FAST, USBD_DEVINTPRI(udc->udp_baseaddr));
1079 writel(0xFFFF, USBD_EPINTPRI(udc->udp_baseaddr));
1081 /* Clear any pending device interrupts */
1082 writel(0x3FF, USBD_DEVINTCLR(udc->udp_baseaddr));
1084 /* Setup UDCA - not yet used (DMA) */
1085 writel(udc->udca_p_base, USBD_UDCAH(udc->udp_baseaddr));
1087 /* Only enable EP0 in and out for now, EP0 only works in FIFO mode */
1088 for (i = 0; i <= 1; i++) {
1089 udc_realize_hwep(udc, i, ep->ep.maxpacket);
1090 uda_enable_hwepint(udc, i);
1091 udc_select_hwep(udc, i);
1092 udc_clrstall_hwep(udc, i);
1093 udc_clr_buffer_hwep(udc, i);
1096 /* Device interrupt setup */
1097 uda_clear_devint(udc, (USBD_ERR_INT | USBD_DEV_STAT | USBD_EP_SLOW |
1099 uda_enable_devint(udc, (USBD_ERR_INT | USBD_DEV_STAT | USBD_EP_SLOW |
1102 /* Set device address to 0 - called twice to force a latch in the USB
1103 engine without the need of a setup packet status closure */
1104 udc_set_address(udc, 0);
1105 udc_set_address(udc, 0);
1107 /* Enable master DMA interrupts */
1108 writel((USBD_SYS_ERR_INT | USBD_EOT_INT),
1109 USBD_DMAINTEN(udc->udp_baseaddr));
1111 udc->dev_status = 0;
1116 * USB device board specific events handled via callbacks
1119 /* Connection change event - notify board function of change */
1120 static void uda_power_event(struct lpc32xx_udc *udc, u32 conn)
1122 /* Just notify of a connection change event (optional) */
1123 if (udc->board->conn_chgb != NULL)
1124 udc->board->conn_chgb(conn);
1127 /* Suspend/resume event - notify board function of change */
1128 static void uda_resm_susp_event(struct lpc32xx_udc *udc, u32 conn)
1130 /* Just notify of a Suspend/resume change event (optional) */
1131 if (udc->board->susp_chgb != NULL)
1132 udc->board->susp_chgb(conn);
1140 /* Remote wakeup enable/disable - notify board function of change */
1141 static void uda_remwkp_cgh(struct lpc32xx_udc *udc)
1143 if (udc->board->rmwk_chgb != NULL)
1144 udc->board->rmwk_chgb(udc->dev_status &
1145 (1 << USB_DEVICE_REMOTE_WAKEUP));
1148 /* Reads data from FIFO, adjusts for alignment and data size */
1149 static void udc_pop_fifo(struct lpc32xx_udc *udc, u8 *data, u32 bytes)
1153 u32 *p32, tmp, cbytes;
1155 /* Use optimal data transfer method based on source address and size */
1156 switch (((u32) data) & 0x3) {
1157 case 0: /* 32-bit aligned */
1159 cbytes = (bytes & ~0x3);
1161 /* Copy 32-bit aligned data first */
1162 for (n = 0; n < cbytes; n += 4)
1163 *p32++ = readl(USBD_RXDATA(udc->udp_baseaddr));
1165 /* Handle any remaining bytes */
1166 bl = bytes - cbytes;
1168 tmp = readl(USBD_RXDATA(udc->udp_baseaddr));
1169 for (n = 0; n < bl; n++)
1170 data[cbytes + n] = ((tmp >> (n * 8)) & 0xFF);
1175 case 1: /* 8-bit aligned */
1177 /* Each byte has to be handled independently */
1178 for (n = 0; n < bytes; n += 4) {
1179 tmp = readl(USBD_RXDATA(udc->udp_baseaddr));
1185 for (i = 0; i < bl; i++)
1186 data[n + i] = (u8) ((tmp >> (n * 8)) & 0xFF);
1190 case 2: /* 16-bit aligned */
1192 cbytes = (bytes & ~0x3);
1194 /* Copy 32-bit sized objects first with 16-bit alignment */
1195 for (n = 0; n < cbytes; n += 4) {
1196 tmp = readl(USBD_RXDATA(udc->udp_baseaddr));
1197 *p16++ = (u16)(tmp & 0xFFFF);
1198 *p16++ = (u16)((tmp >> 16) & 0xFFFF);
1201 /* Handle any remaining bytes */
1202 bl = bytes - cbytes;
1204 tmp = readl(USBD_RXDATA(udc->udp_baseaddr));
1205 for (n = 0; n < bl; n++)
1206 data[cbytes + n] = ((tmp >> (n * 8)) & 0xFF);
1212 /* Read data from the FIFO for an endpoint. This function is for endpoints (such
1213 * as EP0) that don't use DMA. This function should only be called if a packet
1214 * is known to be ready to read for the endpoint. Note that the endpoint must
1215 * be selected in the protocol engine prior to this call. */
1216 static u32 udc_read_hwep(struct lpc32xx_udc *udc, u32 hwep, u32 *data,
1221 u32 tmp, hwrep = ((hwep & 0x1E) << 1) | CTRL_RD_EN;
1223 /* Setup read of endpoint */
1224 writel(hwrep, USBD_CTRL(udc->udp_baseaddr));
1226 /* Wait until packet is ready */
1227 while ((((tmpv = readl(USBD_RXPLEN(udc->udp_baseaddr))) &
1228 PKT_RDY) == 0) && (to > 0))
1231 dev_dbg(udc->dev, "No packet ready on FIFO EP read\n");
1233 /* Mask out count */
1234 tmp = tmpv & PKT_LNGTH_MASK;
1238 if ((tmp > 0) && (data != NULL))
1239 udc_pop_fifo(udc, (u8 *) data, tmp);
1241 writel(((hwep & 0x1E) << 1), USBD_CTRL(udc->udp_baseaddr));
1243 /* Clear the buffer */
1244 udc_clr_buffer_hwep(udc, hwep);
1249 /* Stuffs data into the FIFO, adjusts for alignment and data size */
1250 static void udc_stuff_fifo(struct lpc32xx_udc *udc, u8 *data, u32 bytes)
1254 u32 *p32, tmp, cbytes;
1256 /* Use optimal data transfer method based on source address and size */
1257 switch (((u32) data) & 0x3) {
1258 case 0: /* 32-bit aligned */
1260 cbytes = (bytes & ~0x3);
1262 /* Copy 32-bit aligned data first */
1263 for (n = 0; n < cbytes; n += 4)
1264 writel(*p32++, USBD_TXDATA(udc->udp_baseaddr));
1266 /* Handle any remaining bytes */
1267 bl = bytes - cbytes;
1270 for (n = 0; n < bl; n++)
1271 tmp |= data[cbytes + n] << (n * 8);
1273 writel(tmp, USBD_TXDATA(udc->udp_baseaddr));
1277 case 1: /* 8-bit aligned */
1279 /* Each byte has to be handled independently */
1280 for (n = 0; n < bytes; n += 4) {
1286 for (i = 0; i < bl; i++)
1287 tmp |= data[n + i] << (i * 8);
1289 writel(tmp, USBD_TXDATA(udc->udp_baseaddr));
1293 case 2: /* 16-bit aligned */
1295 cbytes = (bytes & ~0x3);
1297 /* Copy 32-bit aligned data first */
1298 for (n = 0; n < cbytes; n += 4) {
1299 tmp = *p16++ & 0xFFFF;
1300 tmp |= (*p16++ & 0xFFFF) << 16;
1301 writel(tmp, USBD_TXDATA(udc->udp_baseaddr));
1304 /* Handle any remaining bytes */
1305 bl = bytes - cbytes;
1308 for (n = 0; n < bl; n++)
1309 tmp |= data[cbytes + n] << (n * 8);
1311 writel(tmp, USBD_TXDATA(udc->udp_baseaddr));
1317 /* Write data to the FIFO for an endpoint. This function is for endpoints (such
1318 * as EP0) that don't use DMA. Note that the endpoint must be selected in the
1319 * protocol engine prior to this call. */
1320 static void udc_write_hwep(struct lpc32xx_udc *udc, u32 hwep, u32 *data,
1323 u32 hwwep = ((hwep & 0x1E) << 1) | CTRL_WR_EN;
1325 if ((bytes > 0) && (data == NULL))
1328 /* Setup write of endpoint */
1329 writel(hwwep, USBD_CTRL(udc->udp_baseaddr));
1331 writel(bytes, USBD_TXPLEN(udc->udp_baseaddr));
1333 /* Need at least 1 byte to trigger TX */
1335 writel(0, USBD_TXDATA(udc->udp_baseaddr));
1337 udc_stuff_fifo(udc, (u8 *) data, bytes);
1339 writel(((hwep & 0x1E) << 1), USBD_CTRL(udc->udp_baseaddr));
1341 udc_val_buffer_hwep(udc, hwep);
1344 /* USB device reset - resets USB to a default state with just EP0
1346 static void uda_usb_reset(struct lpc32xx_udc *udc)
1349 /* Re-init device controller and EP0 */
1351 udc->gadget.speed = USB_SPEED_FULL;
1353 for (i = 1; i < NUM_ENDPOINTS; i++) {
1354 struct lpc32xx_ep *ep = &udc->ep[i];
1355 ep->req_pending = 0;
1359 /* Send a ZLP on EP0 */
1360 static void udc_ep0_send_zlp(struct lpc32xx_udc *udc)
1362 udc_write_hwep(udc, EP_IN, NULL, 0);
1365 /* Get current frame number */
1366 static u16 udc_get_current_frame(struct lpc32xx_udc *udc)
1370 udc_protocol_cmd_w(udc, CMD_RD_FRAME);
1371 flo = (u16) udc_protocol_cmd_r(udc, DAT_RD_FRAME);
1372 fhi = (u16) udc_protocol_cmd_r(udc, DAT_RD_FRAME);
1374 return (fhi << 8) | flo;
1377 /* Set the device as configured - enables all endpoints */
1378 static inline void udc_set_device_configured(struct lpc32xx_udc *udc)
1380 udc_protocol_cmd_data_w(udc, CMD_CFG_DEV, DAT_WR_BYTE(CONF_DVICE));
1383 /* Set the device as unconfigured - disables all endpoints */
1384 static inline void udc_set_device_unconfigured(struct lpc32xx_udc *udc)
1386 udc_protocol_cmd_data_w(udc, CMD_CFG_DEV, DAT_WR_BYTE(0));
1389 /* reinit == restore initial software state */
1390 static void udc_reinit(struct lpc32xx_udc *udc)
1394 INIT_LIST_HEAD(&udc->gadget.ep_list);
1395 INIT_LIST_HEAD(&udc->gadget.ep0->ep_list);
1397 for (i = 0; i < NUM_ENDPOINTS; i++) {
1398 struct lpc32xx_ep *ep = &udc->ep[i];
1401 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
1402 usb_ep_set_maxpacket_limit(&ep->ep, ep->maxpacket);
1403 INIT_LIST_HEAD(&ep->queue);
1404 ep->req_pending = 0;
1407 udc->ep0state = WAIT_FOR_SETUP;
1410 /* Must be called with lock */
1411 static void done(struct lpc32xx_ep *ep, struct lpc32xx_request *req, int status)
1413 struct lpc32xx_udc *udc = ep->udc;
1415 list_del_init(&req->queue);
1416 if (req->req.status == -EINPROGRESS)
1417 req->req.status = status;
1419 status = req->req.status;
1422 usb_gadget_unmap_request(&udc->gadget, &req->req, ep->is_in);
1425 udc_dd_free(udc, req->dd_desc_ptr);
1428 if (status && status != -ESHUTDOWN)
1429 ep_dbg(ep, "%s done %p, status %d\n", ep->ep.name, req, status);
1431 ep->req_pending = 0;
1432 spin_unlock(&udc->lock);
1433 usb_gadget_giveback_request(&ep->ep, &req->req);
1434 spin_lock(&udc->lock);
1437 /* Must be called with lock */
1438 static void nuke(struct lpc32xx_ep *ep, int status)
1440 struct lpc32xx_request *req;
1442 while (!list_empty(&ep->queue)) {
1443 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
1444 done(ep, req, status);
1447 if (status == -ESHUTDOWN) {
1448 uda_disable_hwepint(ep->udc, ep->hwep_num);
1449 udc_disable_hwep(ep->udc, ep->hwep_num);
1453 /* IN endpoint 0 transfer */
1454 static int udc_ep0_in_req(struct lpc32xx_udc *udc)
1456 struct lpc32xx_request *req;
1457 struct lpc32xx_ep *ep0 = &udc->ep[0];
1460 if (list_empty(&ep0->queue))
1461 /* Nothing to send */
1464 req = list_entry(ep0->queue.next, struct lpc32xx_request,
1467 tsend = ts = req->req.length - req->req.actual;
1470 udc_ep0_send_zlp(udc);
1473 } else if (ts > ep0->ep.maxpacket)
1474 ts = ep0->ep.maxpacket; /* Just send what we can */
1476 /* Write data to the EP0 FIFO and start transfer */
1477 udc_write_hwep(udc, EP_IN, (req->req.buf + req->req.actual), ts);
1479 /* Increment data pointer */
1480 req->req.actual += ts;
1482 if (tsend >= ep0->ep.maxpacket)
1483 return 0; /* Stay in data transfer state */
1485 /* Transfer request is complete */
1486 udc->ep0state = WAIT_FOR_SETUP;
1491 /* OUT endpoint 0 transfer */
1492 static int udc_ep0_out_req(struct lpc32xx_udc *udc)
1494 struct lpc32xx_request *req;
1495 struct lpc32xx_ep *ep0 = &udc->ep[0];
1496 u32 tr, bufferspace;
1498 if (list_empty(&ep0->queue))
1501 req = list_entry(ep0->queue.next, struct lpc32xx_request,
1505 if (req->req.length == 0) {
1506 /* Just dequeue request */
1508 udc->ep0state = WAIT_FOR_SETUP;
1512 /* Get data from FIFO */
1513 bufferspace = req->req.length - req->req.actual;
1514 if (bufferspace > ep0->ep.maxpacket)
1515 bufferspace = ep0->ep.maxpacket;
1517 /* Copy data to buffer */
1518 prefetchw(req->req.buf + req->req.actual);
1519 tr = udc_read_hwep(udc, EP_OUT, req->req.buf + req->req.actual,
1521 req->req.actual += bufferspace;
1523 if (tr < ep0->ep.maxpacket) {
1524 /* This is the last packet */
1526 udc->ep0state = WAIT_FOR_SETUP;
1534 /* Must be called with lock */
1535 static void stop_activity(struct lpc32xx_udc *udc)
1537 struct usb_gadget_driver *driver = udc->driver;
1540 if (udc->gadget.speed == USB_SPEED_UNKNOWN)
1543 udc->gadget.speed = USB_SPEED_UNKNOWN;
1546 for (i = 0; i < NUM_ENDPOINTS; i++) {
1547 struct lpc32xx_ep *ep = &udc->ep[i];
1548 nuke(ep, -ESHUTDOWN);
1551 spin_unlock(&udc->lock);
1552 driver->disconnect(&udc->gadget);
1553 spin_lock(&udc->lock);
1556 isp1301_pullup_enable(udc, 0, 0);
1562 * Activate or kill host pullup
1563 * Can be called with or without lock
1565 static void pullup(struct lpc32xx_udc *udc, int is_on)
1570 if (!udc->enabled || !udc->vbus)
1573 if (is_on != udc->pullup)
1574 isp1301_pullup_enable(udc, is_on, 0);
1577 /* Must be called without lock */
1578 static int lpc32xx_ep_disable(struct usb_ep *_ep)
1580 struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
1581 struct lpc32xx_udc *udc = ep->udc;
1582 unsigned long flags;
1584 if ((ep->hwep_num_base == 0) || (ep->hwep_num == 0))
1586 spin_lock_irqsave(&udc->lock, flags);
1588 nuke(ep, -ESHUTDOWN);
1590 /* Clear all DMA statuses for this EP */
1591 udc_ep_dma_disable(udc, ep->hwep_num);
1592 writel(1 << ep->hwep_num, USBD_EOTINTCLR(udc->udp_baseaddr));
1593 writel(1 << ep->hwep_num, USBD_NDDRTINTCLR(udc->udp_baseaddr));
1594 writel(1 << ep->hwep_num, USBD_SYSERRTINTCLR(udc->udp_baseaddr));
1595 writel(1 << ep->hwep_num, USBD_DMARCLR(udc->udp_baseaddr));
1597 /* Remove the DD pointer in the UDCA */
1598 udc->udca_v_base[ep->hwep_num] = 0;
1600 /* Disable and reset endpoint and interrupt */
1601 uda_clear_hwepint(udc, ep->hwep_num);
1602 udc_unrealize_hwep(udc, ep->hwep_num);
1606 spin_unlock_irqrestore(&udc->lock, flags);
1608 atomic_dec(&udc->enabled_ep_cnt);
1609 wake_up(&udc->ep_disable_wait_queue);
1614 /* Must be called without lock */
1615 static int lpc32xx_ep_enable(struct usb_ep *_ep,
1616 const struct usb_endpoint_descriptor *desc)
1618 struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
1619 struct lpc32xx_udc *udc = ep->udc;
1622 unsigned long flags;
1624 /* Verify EP data */
1625 if ((!_ep) || (!ep) || (!desc) ||
1626 (desc->bDescriptorType != USB_DT_ENDPOINT)) {
1627 dev_dbg(udc->dev, "bad ep or descriptor\n");
1630 maxpacket = usb_endpoint_maxp(desc);
1631 if ((maxpacket == 0) || (maxpacket > ep->maxpacket)) {
1632 dev_dbg(udc->dev, "bad ep descriptor's packet size\n");
1636 /* Don't touch EP0 */
1637 if (ep->hwep_num_base == 0) {
1638 dev_dbg(udc->dev, "Can't re-enable EP0!!!\n");
1642 /* Is driver ready? */
1643 if ((!udc->driver) || (udc->gadget.speed == USB_SPEED_UNKNOWN)) {
1644 dev_dbg(udc->dev, "bogus device state\n");
1648 tmp = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
1650 case USB_ENDPOINT_XFER_CONTROL:
1653 case USB_ENDPOINT_XFER_INT:
1654 if (maxpacket > ep->maxpacket) {
1656 "Bad INT endpoint maxpacket %d\n", maxpacket);
1661 case USB_ENDPOINT_XFER_BULK:
1662 switch (maxpacket) {
1671 "Bad BULK endpoint maxpacket %d\n", maxpacket);
1676 case USB_ENDPOINT_XFER_ISOC:
1679 spin_lock_irqsave(&udc->lock, flags);
1681 /* Initialize endpoint to match the selected descriptor */
1682 ep->is_in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
1683 ep->ep.maxpacket = maxpacket;
1685 /* Map hardware endpoint from base and direction */
1687 /* IN endpoints are offset 1 from the OUT endpoint */
1688 ep->hwep_num = ep->hwep_num_base + EP_IN;
1690 ep->hwep_num = ep->hwep_num_base;
1692 ep_dbg(ep, "EP enabled: %s, HW:%d, MP:%d IN:%d\n", ep->ep.name,
1693 ep->hwep_num, maxpacket, (ep->is_in == 1));
1695 /* Realize the endpoint, interrupt is enabled later when
1696 * buffers are queued, IN EPs will NAK until buffers are ready */
1697 udc_realize_hwep(udc, ep->hwep_num, ep->ep.maxpacket);
1698 udc_clr_buffer_hwep(udc, ep->hwep_num);
1699 uda_disable_hwepint(udc, ep->hwep_num);
1700 udc_clrstall_hwep(udc, ep->hwep_num);
1702 /* Clear all DMA statuses for this EP */
1703 udc_ep_dma_disable(udc, ep->hwep_num);
1704 writel(1 << ep->hwep_num, USBD_EOTINTCLR(udc->udp_baseaddr));
1705 writel(1 << ep->hwep_num, USBD_NDDRTINTCLR(udc->udp_baseaddr));
1706 writel(1 << ep->hwep_num, USBD_SYSERRTINTCLR(udc->udp_baseaddr));
1707 writel(1 << ep->hwep_num, USBD_DMARCLR(udc->udp_baseaddr));
1709 spin_unlock_irqrestore(&udc->lock, flags);
1711 atomic_inc(&udc->enabled_ep_cnt);
1716 * Allocate a USB request list
1717 * Can be called with or without lock
1719 static struct usb_request *lpc32xx_ep_alloc_request(struct usb_ep *_ep,
1722 struct lpc32xx_request *req;
1724 req = kzalloc(sizeof(struct lpc32xx_request), gfp_flags);
1728 INIT_LIST_HEAD(&req->queue);
1733 * De-allocate a USB request list
1734 * Can be called with or without lock
1736 static void lpc32xx_ep_free_request(struct usb_ep *_ep,
1737 struct usb_request *_req)
1739 struct lpc32xx_request *req;
1741 req = container_of(_req, struct lpc32xx_request, req);
1742 BUG_ON(!list_empty(&req->queue));
1746 /* Must be called without lock */
1747 static int lpc32xx_ep_queue(struct usb_ep *_ep,
1748 struct usb_request *_req, gfp_t gfp_flags)
1750 struct lpc32xx_request *req;
1751 struct lpc32xx_ep *ep;
1752 struct lpc32xx_udc *udc;
1753 unsigned long flags;
1756 req = container_of(_req, struct lpc32xx_request, req);
1757 ep = container_of(_ep, struct lpc32xx_ep, ep);
1759 if (!_ep || !_req || !_req->complete || !_req->buf ||
1760 !list_empty(&req->queue))
1765 if (udc->gadget.speed == USB_SPEED_UNKNOWN)
1769 struct lpc32xx_usbd_dd_gad *dd;
1771 status = usb_gadget_map_request(&udc->gadget, _req, ep->is_in);
1775 /* For the request, build a list of DDs */
1776 dd = udc_dd_alloc(udc);
1778 /* Error allocating DD */
1781 req->dd_desc_ptr = dd;
1783 /* Setup the DMA descriptor */
1784 dd->dd_next_phy = dd->dd_next_v = 0;
1785 dd->dd_buffer_addr = req->req.dma;
1788 /* Special handling for ISO EPs */
1789 if (ep->eptype == EP_ISO_TYPE) {
1790 dd->dd_setup = DD_SETUP_ISO_EP |
1791 DD_SETUP_PACKETLEN(0) |
1792 DD_SETUP_DMALENBYTES(1);
1793 dd->dd_iso_ps_mem_addr = dd->this_dma + 24;
1795 dd->iso_status[0] = req->req.length;
1797 dd->iso_status[0] = 0;
1799 dd->dd_setup = DD_SETUP_PACKETLEN(ep->ep.maxpacket) |
1800 DD_SETUP_DMALENBYTES(req->req.length);
1803 ep_dbg(ep, "%s queue req %p len %d buf %p (in=%d) z=%d\n", _ep->name,
1804 _req, _req->length, _req->buf, ep->is_in, _req->zero);
1806 spin_lock_irqsave(&udc->lock, flags);
1808 _req->status = -EINPROGRESS;
1810 req->send_zlp = _req->zero;
1812 /* Kickstart empty queues */
1813 if (list_empty(&ep->queue)) {
1814 list_add_tail(&req->queue, &ep->queue);
1816 if (ep->hwep_num_base == 0) {
1817 /* Handle expected data direction */
1819 /* IN packet to host */
1820 udc->ep0state = DATA_IN;
1821 status = udc_ep0_in_req(udc);
1823 /* OUT packet from host */
1824 udc->ep0state = DATA_OUT;
1825 status = udc_ep0_out_req(udc);
1827 } else if (ep->is_in) {
1828 /* IN packet to host and kick off transfer */
1829 if (!ep->req_pending)
1830 udc_ep_in_req_dma(udc, ep);
1832 /* OUT packet from host and kick off list */
1833 if (!ep->req_pending)
1834 udc_ep_out_req_dma(udc, ep);
1836 list_add_tail(&req->queue, &ep->queue);
1838 spin_unlock_irqrestore(&udc->lock, flags);
1840 return (status < 0) ? status : 0;
1843 /* Must be called without lock */
1844 static int lpc32xx_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1846 struct lpc32xx_ep *ep;
1847 struct lpc32xx_request *req;
1848 unsigned long flags;
1850 ep = container_of(_ep, struct lpc32xx_ep, ep);
1851 if (!_ep || ep->hwep_num_base == 0)
1854 spin_lock_irqsave(&ep->udc->lock, flags);
1856 /* make sure it's actually queued on this endpoint */
1857 list_for_each_entry(req, &ep->queue, queue) {
1858 if (&req->req == _req)
1861 if (&req->req != _req) {
1862 spin_unlock_irqrestore(&ep->udc->lock, flags);
1866 done(ep, req, -ECONNRESET);
1868 spin_unlock_irqrestore(&ep->udc->lock, flags);
1873 /* Must be called without lock */
1874 static int lpc32xx_ep_set_halt(struct usb_ep *_ep, int value)
1876 struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
1877 struct lpc32xx_udc *udc = ep->udc;
1878 unsigned long flags;
1880 if ((!ep) || (ep->hwep_num <= 1))
1883 /* Don't halt an IN EP */
1887 spin_lock_irqsave(&udc->lock, flags);
1891 udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(ep->hwep_num),
1892 DAT_WR_BYTE(EP_STAT_ST));
1896 udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(ep->hwep_num),
1900 spin_unlock_irqrestore(&udc->lock, flags);
1905 /* set the halt feature and ignores clear requests */
1906 static int lpc32xx_ep_set_wedge(struct usb_ep *_ep)
1908 struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
1910 if (!_ep || !ep->udc)
1915 return usb_ep_set_halt(_ep);
1918 static const struct usb_ep_ops lpc32xx_ep_ops = {
1919 .enable = lpc32xx_ep_enable,
1920 .disable = lpc32xx_ep_disable,
1921 .alloc_request = lpc32xx_ep_alloc_request,
1922 .free_request = lpc32xx_ep_free_request,
1923 .queue = lpc32xx_ep_queue,
1924 .dequeue = lpc32xx_ep_dequeue,
1925 .set_halt = lpc32xx_ep_set_halt,
1926 .set_wedge = lpc32xx_ep_set_wedge,
1929 /* Send a ZLP on a non-0 IN EP */
1930 void udc_send_in_zlp(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
1932 /* Clear EP status */
1933 udc_clearep_getsts(udc, ep->hwep_num);
1935 /* Send ZLP via FIFO mechanism */
1936 udc_write_hwep(udc, ep->hwep_num, NULL, 0);
1940 * Handle EP completion for ZLP
1941 * This function will only be called when a delayed ZLP needs to be sent out
1942 * after a DMA transfer has filled both buffers.
1944 void udc_handle_eps(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
1947 struct lpc32xx_request *req;
1949 if (ep->hwep_num <= 0)
1952 uda_clear_hwepint(udc, ep->hwep_num);
1954 /* If this interrupt isn't enabled, return now */
1955 if (!(udc->enabled_hwepints & (1 << ep->hwep_num)))
1958 /* Get endpoint status */
1959 epstatus = udc_clearep_getsts(udc, ep->hwep_num);
1962 * This should never happen, but protect against writing to the
1965 if (epstatus & EP_SEL_F)
1969 udc_send_in_zlp(udc, ep);
1970 uda_disable_hwepint(udc, ep->hwep_num);
1974 /* If there isn't a request waiting, something went wrong */
1975 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
1979 /* Start another request if ready */
1980 if (!list_empty(&ep->queue)) {
1982 udc_ep_in_req_dma(udc, ep);
1984 udc_ep_out_req_dma(udc, ep);
1986 ep->req_pending = 0;
1991 /* DMA end of transfer completion */
1992 static void udc_handle_dma_ep(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
1994 u32 status, epstatus;
1995 struct lpc32xx_request *req;
1996 struct lpc32xx_usbd_dd_gad *dd;
1998 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2002 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
2004 ep_err(ep, "DMA interrupt on no req!\n");
2007 dd = req->dd_desc_ptr;
2009 /* DMA descriptor should always be retired for this call */
2010 if (!(dd->dd_status & DD_STATUS_DD_RETIRED))
2011 ep_warn(ep, "DMA descriptor did not retire\n");
2014 udc_ep_dma_disable(udc, ep->hwep_num);
2015 writel((1 << ep->hwep_num), USBD_EOTINTCLR(udc->udp_baseaddr));
2016 writel((1 << ep->hwep_num), USBD_NDDRTINTCLR(udc->udp_baseaddr));
2019 if (readl(USBD_SYSERRTINTST(udc->udp_baseaddr)) &
2020 (1 << ep->hwep_num)) {
2021 writel((1 << ep->hwep_num),
2022 USBD_SYSERRTINTCLR(udc->udp_baseaddr));
2023 ep_err(ep, "AHB critical error!\n");
2024 ep->req_pending = 0;
2026 /* The error could have occurred on a packet of a multipacket
2027 * transfer, so recovering the transfer is not possible. Close
2028 * the request with an error */
2029 done(ep, req, -ECONNABORTED);
2033 /* Handle the current DD's status */
2034 status = dd->dd_status;
2035 switch (status & DD_STATUS_STS_MASK) {
2036 case DD_STATUS_STS_NS:
2037 /* DD not serviced? This shouldn't happen! */
2038 ep->req_pending = 0;
2039 ep_err(ep, "DMA critical EP error: DD not serviced (0x%x)!\n",
2042 done(ep, req, -ECONNABORTED);
2045 case DD_STATUS_STS_BS:
2046 /* Interrupt only fires on EOT - This shouldn't happen! */
2047 ep->req_pending = 0;
2048 ep_err(ep, "DMA critical EP error: EOT prior to service completion (0x%x)!\n",
2050 done(ep, req, -ECONNABORTED);
2053 case DD_STATUS_STS_NC:
2054 case DD_STATUS_STS_DUR:
2055 /* Really just a short packet, not an underrun */
2056 /* This is a good status and what we expect */
2060 /* Data overrun, system error, or unknown */
2061 ep->req_pending = 0;
2062 ep_err(ep, "DMA critical EP error: System error (0x%x)!\n",
2064 done(ep, req, -ECONNABORTED);
2068 /* ISO endpoints are handled differently */
2069 if (ep->eptype == EP_ISO_TYPE) {
2071 req->req.actual = req->req.length;
2073 req->req.actual = dd->iso_status[0] & 0xFFFF;
2075 req->req.actual += DD_STATUS_CURDMACNT(status);
2077 /* Send a ZLP if necessary. This will be done for non-int
2078 * packets which have a size that is a divisor of MAXP */
2079 if (req->send_zlp) {
2081 * If at least 1 buffer is available, send the ZLP now.
2082 * Otherwise, the ZLP send needs to be deferred until a
2083 * buffer is available.
2085 if (udc_clearep_getsts(udc, ep->hwep_num) & EP_SEL_F) {
2086 udc_clearep_getsts(udc, ep->hwep_num);
2087 uda_enable_hwepint(udc, ep->hwep_num);
2088 epstatus = udc_clearep_getsts(udc, ep->hwep_num);
2090 /* Let the EP interrupt handle the ZLP */
2093 udc_send_in_zlp(udc, ep);
2096 /* Transfer request is complete */
2099 /* Start another request if ready */
2100 udc_clearep_getsts(udc, ep->hwep_num);
2101 if (!list_empty((&ep->queue))) {
2103 udc_ep_in_req_dma(udc, ep);
2105 udc_ep_out_req_dma(udc, ep);
2107 ep->req_pending = 0;
2113 * Endpoint 0 functions
2116 static void udc_handle_dev(struct lpc32xx_udc *udc)
2120 udc_protocol_cmd_w(udc, CMD_GET_DEV_STAT);
2121 tmp = udc_protocol_cmd_r(udc, DAT_GET_DEV_STAT);
2125 else if (tmp & DEV_CON_CH)
2126 uda_power_event(udc, (tmp & DEV_CON));
2127 else if (tmp & DEV_SUS_CH) {
2128 if (tmp & DEV_SUS) {
2131 else if ((udc->gadget.speed != USB_SPEED_UNKNOWN) &&
2133 /* Power down transceiver */
2135 schedule_work(&udc->pullup_job);
2136 uda_resm_susp_event(udc, 1);
2138 } else if ((udc->gadget.speed != USB_SPEED_UNKNOWN) &&
2139 udc->driver && udc->vbus) {
2140 uda_resm_susp_event(udc, 0);
2141 /* Power up transceiver */
2143 schedule_work(&udc->pullup_job);
2148 static int udc_get_status(struct lpc32xx_udc *udc, u16 reqtype, u16 wIndex)
2150 struct lpc32xx_ep *ep;
2151 u32 ep0buff = 0, tmp;
2153 switch (reqtype & USB_RECIP_MASK) {
2154 case USB_RECIP_INTERFACE:
2155 break; /* Not supported */
2157 case USB_RECIP_DEVICE:
2158 ep0buff = udc->gadget.is_selfpowered;
2159 if (udc->dev_status & (1 << USB_DEVICE_REMOTE_WAKEUP))
2160 ep0buff |= (1 << USB_DEVICE_REMOTE_WAKEUP);
2163 case USB_RECIP_ENDPOINT:
2164 tmp = wIndex & USB_ENDPOINT_NUMBER_MASK;
2166 if ((tmp == 0) || (tmp >= NUM_ENDPOINTS))
2169 if (wIndex & USB_DIR_IN) {
2171 return -EOPNOTSUPP; /* Something's wrong */
2172 } else if (ep->is_in)
2173 return -EOPNOTSUPP; /* Not an IN endpoint */
2175 /* Get status of the endpoint */
2176 udc_protocol_cmd_w(udc, CMD_SEL_EP(ep->hwep_num));
2177 tmp = udc_protocol_cmd_r(udc, DAT_SEL_EP(ep->hwep_num));
2179 if (tmp & EP_SEL_ST)
2180 ep0buff = (1 << USB_ENDPOINT_HALT);
2190 udc_write_hwep(udc, EP_IN, &ep0buff, 2);
2195 static void udc_handle_ep0_setup(struct lpc32xx_udc *udc)
2197 struct lpc32xx_ep *ep, *ep0 = &udc->ep[0];
2198 struct usb_ctrlrequest ctrlpkt;
2200 u16 wIndex, wValue, wLength, reqtype, req, tmp;
2202 /* Nuke previous transfers */
2205 /* Get setup packet */
2206 bytes = udc_read_hwep(udc, EP_OUT, (u32 *) &ctrlpkt, 8);
2208 ep_warn(ep0, "Incorrectly sized setup packet (s/b 8, is %d)!\n",
2213 /* Native endianness */
2214 wIndex = le16_to_cpu(ctrlpkt.wIndex);
2215 wValue = le16_to_cpu(ctrlpkt.wValue);
2216 wLength = le16_to_cpu(ctrlpkt.wLength);
2217 reqtype = le16_to_cpu(ctrlpkt.bRequestType);
2219 /* Set direction of EP0 */
2220 if (likely(reqtype & USB_DIR_IN))
2225 /* Handle SETUP packet */
2226 req = le16_to_cpu(ctrlpkt.bRequest);
2228 case USB_REQ_CLEAR_FEATURE:
2229 case USB_REQ_SET_FEATURE:
2231 case (USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2232 if (wValue != USB_DEVICE_REMOTE_WAKEUP)
2233 goto stall; /* Nothing else handled */
2235 /* Tell board about event */
2236 if (req == USB_REQ_CLEAR_FEATURE)
2238 ~(1 << USB_DEVICE_REMOTE_WAKEUP);
2241 (1 << USB_DEVICE_REMOTE_WAKEUP);
2242 uda_remwkp_cgh(udc);
2245 case (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2246 tmp = wIndex & USB_ENDPOINT_NUMBER_MASK;
2247 if ((wValue != USB_ENDPOINT_HALT) ||
2248 (tmp >= NUM_ENDPOINTS))
2251 /* Find hardware endpoint from logical endpoint */
2257 if (req == USB_REQ_SET_FEATURE)
2258 udc_stall_hwep(udc, tmp);
2259 else if (!ep->wedge)
2260 udc_clrstall_hwep(udc, tmp);
2269 case USB_REQ_SET_ADDRESS:
2270 if (reqtype == (USB_TYPE_STANDARD | USB_RECIP_DEVICE)) {
2271 udc_set_address(udc, wValue);
2276 case USB_REQ_GET_STATUS:
2277 udc_get_status(udc, reqtype, wIndex);
2281 break; /* Let GadgetFS handle the descriptor instead */
2284 if (likely(udc->driver)) {
2285 /* device-2-host (IN) or no data setup command, process
2287 spin_unlock(&udc->lock);
2288 i = udc->driver->setup(&udc->gadget, &ctrlpkt);
2290 spin_lock(&udc->lock);
2291 if (req == USB_REQ_SET_CONFIGURATION) {
2292 /* Configuration is set after endpoints are realized */
2294 /* Set configuration */
2295 udc_set_device_configured(udc);
2297 udc_protocol_cmd_data_w(udc, CMD_SET_MODE,
2298 DAT_WR_BYTE(AP_CLK |
2299 INAK_BI | INAK_II));
2301 /* Clear configuration */
2302 udc_set_device_unconfigured(udc);
2304 /* Disable NAK interrupts */
2305 udc_protocol_cmd_data_w(udc, CMD_SET_MODE,
2306 DAT_WR_BYTE(AP_CLK));
2311 /* setup processing failed, force stall */
2313 "req %02x.%02x protocol STALL; stat %d\n",
2315 udc->ep0state = WAIT_FOR_SETUP;
2321 udc_ep0_send_zlp(udc); /* ZLP IN packet on data phase */
2326 udc_stall_hwep(udc, EP_IN);
2330 udc_ep0_send_zlp(udc);
2334 /* IN endpoint 0 transfer */
2335 static void udc_handle_ep0_in(struct lpc32xx_udc *udc)
2337 struct lpc32xx_ep *ep0 = &udc->ep[0];
2340 /* Clear EP interrupt */
2341 epstatus = udc_clearep_getsts(udc, EP_IN);
2343 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2347 /* Stalled? Clear stall and reset buffers */
2348 if (epstatus & EP_SEL_ST) {
2349 udc_clrstall_hwep(udc, EP_IN);
2350 nuke(ep0, -ECONNABORTED);
2351 udc->ep0state = WAIT_FOR_SETUP;
2355 /* Is a buffer available? */
2356 if (!(epstatus & EP_SEL_F)) {
2357 /* Handle based on current state */
2358 if (udc->ep0state == DATA_IN)
2359 udc_ep0_in_req(udc);
2361 /* Unknown state for EP0 oe end of DATA IN phase */
2362 nuke(ep0, -ECONNABORTED);
2363 udc->ep0state = WAIT_FOR_SETUP;
2368 /* OUT endpoint 0 transfer */
2369 static void udc_handle_ep0_out(struct lpc32xx_udc *udc)
2371 struct lpc32xx_ep *ep0 = &udc->ep[0];
2374 /* Clear EP interrupt */
2375 epstatus = udc_clearep_getsts(udc, EP_OUT);
2378 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2383 if (epstatus & EP_SEL_ST) {
2384 udc_clrstall_hwep(udc, EP_OUT);
2385 nuke(ep0, -ECONNABORTED);
2386 udc->ep0state = WAIT_FOR_SETUP;
2390 /* A NAK may occur if a packet couldn't be received yet */
2391 if (epstatus & EP_SEL_EPN)
2393 /* Setup packet incoming? */
2394 if (epstatus & EP_SEL_STP) {
2396 udc->ep0state = WAIT_FOR_SETUP;
2399 /* Data available? */
2400 if (epstatus & EP_SEL_F)
2401 /* Handle based on current state */
2402 switch (udc->ep0state) {
2403 case WAIT_FOR_SETUP:
2404 udc_handle_ep0_setup(udc);
2408 udc_ep0_out_req(udc);
2412 /* Unknown state for EP0 */
2413 nuke(ep0, -ECONNABORTED);
2414 udc->ep0state = WAIT_FOR_SETUP;
2418 /* Must be called without lock */
2419 static int lpc32xx_get_frame(struct usb_gadget *gadget)
2422 unsigned long flags;
2423 struct lpc32xx_udc *udc = to_udc(gadget);
2428 spin_lock_irqsave(&udc->lock, flags);
2430 frame = (int) udc_get_current_frame(udc);
2432 spin_unlock_irqrestore(&udc->lock, flags);
2437 static int lpc32xx_wakeup(struct usb_gadget *gadget)
2442 static int lpc32xx_set_selfpowered(struct usb_gadget *gadget, int is_on)
2444 gadget->is_selfpowered = (is_on != 0);
2450 * vbus is here! turn everything on that's ready
2451 * Must be called without lock
2453 static int lpc32xx_vbus_session(struct usb_gadget *gadget, int is_active)
2455 unsigned long flags;
2456 struct lpc32xx_udc *udc = to_udc(gadget);
2458 spin_lock_irqsave(&udc->lock, flags);
2460 /* Doesn't need lock */
2462 udc_clk_set(udc, 1);
2464 pullup(udc, is_active);
2469 spin_unlock_irqrestore(&udc->lock, flags);
2471 * Wait for all the endpoints to disable,
2472 * before disabling clocks. Don't wait if
2473 * endpoints are not enabled.
2475 if (atomic_read(&udc->enabled_ep_cnt))
2476 wait_event_interruptible(udc->ep_disable_wait_queue,
2477 (atomic_read(&udc->enabled_ep_cnt) == 0));
2479 spin_lock_irqsave(&udc->lock, flags);
2481 udc_clk_set(udc, 0);
2484 spin_unlock_irqrestore(&udc->lock, flags);
2489 /* Can be called with or without lock */
2490 static int lpc32xx_pullup(struct usb_gadget *gadget, int is_on)
2492 struct lpc32xx_udc *udc = to_udc(gadget);
2494 /* Doesn't need lock */
2500 static int lpc32xx_start(struct usb_gadget *, struct usb_gadget_driver *);
2501 static int lpc32xx_stop(struct usb_gadget *);
2503 static const struct usb_gadget_ops lpc32xx_udc_ops = {
2504 .get_frame = lpc32xx_get_frame,
2505 .wakeup = lpc32xx_wakeup,
2506 .set_selfpowered = lpc32xx_set_selfpowered,
2507 .vbus_session = lpc32xx_vbus_session,
2508 .pullup = lpc32xx_pullup,
2509 .udc_start = lpc32xx_start,
2510 .udc_stop = lpc32xx_stop,
2513 static void nop_release(struct device *dev)
2515 /* nothing to free */
2518 static const struct lpc32xx_udc controller_template = {
2520 .ops = &lpc32xx_udc_ops,
2521 .name = driver_name,
2523 .init_name = "gadget",
2524 .release = nop_release,
2530 .ops = &lpc32xx_ep_ops,
2531 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL,
2532 USB_EP_CAPS_DIR_ALL),
2536 .hwep_num = 0, /* Can be 0 or 1, has special handling */
2538 .eptype = EP_CTL_TYPE,
2543 .ops = &lpc32xx_ep_ops,
2544 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_INT,
2545 USB_EP_CAPS_DIR_ALL),
2549 .hwep_num = 0, /* 2 or 3, will be set later */
2551 .eptype = EP_INT_TYPE,
2556 .ops = &lpc32xx_ep_ops,
2557 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
2558 USB_EP_CAPS_DIR_ALL),
2562 .hwep_num = 0, /* 4 or 5, will be set later */
2564 .eptype = EP_BLK_TYPE,
2569 .ops = &lpc32xx_ep_ops,
2570 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
2571 USB_EP_CAPS_DIR_ALL),
2575 .hwep_num = 0, /* 6 or 7, will be set later */
2577 .eptype = EP_ISO_TYPE,
2582 .ops = &lpc32xx_ep_ops,
2583 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_INT,
2584 USB_EP_CAPS_DIR_ALL),
2588 .hwep_num = 0, /* 8 or 9, will be set later */
2590 .eptype = EP_INT_TYPE,
2595 .ops = &lpc32xx_ep_ops,
2596 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
2597 USB_EP_CAPS_DIR_ALL),
2600 .hwep_num_base = 10,
2601 .hwep_num = 0, /* 10 or 11, will be set later */
2603 .eptype = EP_BLK_TYPE,
2608 .ops = &lpc32xx_ep_ops,
2609 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
2610 USB_EP_CAPS_DIR_ALL),
2613 .hwep_num_base = 12,
2614 .hwep_num = 0, /* 12 or 13, will be set later */
2616 .eptype = EP_ISO_TYPE,
2621 .ops = &lpc32xx_ep_ops,
2622 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_INT,
2623 USB_EP_CAPS_DIR_ALL),
2626 .hwep_num_base = 14,
2629 .eptype = EP_INT_TYPE,
2634 .ops = &lpc32xx_ep_ops,
2635 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
2636 USB_EP_CAPS_DIR_ALL),
2639 .hwep_num_base = 16,
2642 .eptype = EP_BLK_TYPE,
2647 .ops = &lpc32xx_ep_ops,
2648 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
2649 USB_EP_CAPS_DIR_ALL),
2652 .hwep_num_base = 18,
2655 .eptype = EP_ISO_TYPE,
2660 .ops = &lpc32xx_ep_ops,
2661 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_INT,
2662 USB_EP_CAPS_DIR_ALL),
2665 .hwep_num_base = 20,
2668 .eptype = EP_INT_TYPE,
2672 .name = "ep11-bulk",
2673 .ops = &lpc32xx_ep_ops,
2674 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
2675 USB_EP_CAPS_DIR_ALL),
2678 .hwep_num_base = 22,
2681 .eptype = EP_BLK_TYPE,
2686 .ops = &lpc32xx_ep_ops,
2687 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
2688 USB_EP_CAPS_DIR_ALL),
2691 .hwep_num_base = 24,
2694 .eptype = EP_ISO_TYPE,
2699 .ops = &lpc32xx_ep_ops,
2700 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_INT,
2701 USB_EP_CAPS_DIR_ALL),
2704 .hwep_num_base = 26,
2707 .eptype = EP_INT_TYPE,
2711 .name = "ep14-bulk",
2712 .ops = &lpc32xx_ep_ops,
2713 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
2714 USB_EP_CAPS_DIR_ALL),
2717 .hwep_num_base = 28,
2720 .eptype = EP_BLK_TYPE,
2724 .name = "ep15-bulk",
2725 .ops = &lpc32xx_ep_ops,
2726 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
2727 USB_EP_CAPS_DIR_ALL),
2730 .hwep_num_base = 30,
2733 .eptype = EP_BLK_TYPE,
2737 /* ISO and status interrupts */
2738 static irqreturn_t lpc32xx_usb_lp_irq(int irq, void *_udc)
2741 struct lpc32xx_udc *udc = _udc;
2743 spin_lock(&udc->lock);
2745 /* Read the device status register */
2746 devstat = readl(USBD_DEVINTST(udc->udp_baseaddr));
2748 devstat &= ~USBD_EP_FAST;
2749 writel(devstat, USBD_DEVINTCLR(udc->udp_baseaddr));
2750 devstat = devstat & udc->enabled_devints;
2752 /* Device specific handling needed? */
2753 if (devstat & USBD_DEV_STAT)
2754 udc_handle_dev(udc);
2756 /* Start of frame? (devstat & FRAME_INT):
2757 * The frame interrupt isn't really needed for ISO support,
2758 * as the driver will queue the necessary packets */
2761 if (devstat & ERR_INT) {
2762 /* All types of errors, from cable removal during transfer to
2763 * misc protocol and bit errors. These are mostly for just info,
2764 * as the USB hardware will work around these. If these errors
2765 * happen alot, something is wrong. */
2766 udc_protocol_cmd_w(udc, CMD_RD_ERR_STAT);
2767 tmp = udc_protocol_cmd_r(udc, DAT_RD_ERR_STAT);
2768 dev_dbg(udc->dev, "Device error (0x%x)!\n", tmp);
2771 spin_unlock(&udc->lock);
2777 static irqreturn_t lpc32xx_usb_hp_irq(int irq, void *_udc)
2780 struct lpc32xx_udc *udc = _udc;
2782 spin_lock(&udc->lock);
2784 /* Read the device status register */
2785 writel(USBD_EP_FAST, USBD_DEVINTCLR(udc->udp_baseaddr));
2788 tmp = readl(USBD_EPINTST(udc->udp_baseaddr));
2790 /* Special handling for EP0 */
2791 if (tmp & (EP_MASK_SEL(0, EP_OUT) | EP_MASK_SEL(0, EP_IN))) {
2793 if (tmp & (EP_MASK_SEL(0, EP_IN)))
2794 udc_handle_ep0_in(udc);
2796 /* Handle EP0 OUT */
2797 if (tmp & (EP_MASK_SEL(0, EP_OUT)))
2798 udc_handle_ep0_out(udc);
2802 if (tmp & ~(EP_MASK_SEL(0, EP_OUT) | EP_MASK_SEL(0, EP_IN))) {
2805 /* Handle other EP interrupts */
2806 for (i = 1; i < NUM_ENDPOINTS; i++) {
2807 if (tmp & (1 << udc->ep[i].hwep_num))
2808 udc_handle_eps(udc, &udc->ep[i]);
2812 spin_unlock(&udc->lock);
2817 static irqreturn_t lpc32xx_usb_devdma_irq(int irq, void *_udc)
2819 struct lpc32xx_udc *udc = _udc;
2824 spin_lock(&udc->lock);
2826 /* Handle EP DMA EOT interrupts */
2827 tmp = readl(USBD_EOTINTST(udc->udp_baseaddr)) |
2828 (readl(USBD_EPDMAST(udc->udp_baseaddr)) &
2829 readl(USBD_NDDRTINTST(udc->udp_baseaddr))) |
2830 readl(USBD_SYSERRTINTST(udc->udp_baseaddr));
2831 for (i = 1; i < NUM_ENDPOINTS; i++) {
2832 if (tmp & (1 << udc->ep[i].hwep_num))
2833 udc_handle_dma_ep(udc, &udc->ep[i]);
2836 spin_unlock(&udc->lock);
2843 * VBUS detection, pullup handler, and Gadget cable state notification
2846 static void vbus_work(struct work_struct *work)
2849 struct lpc32xx_udc *udc = container_of(work, struct lpc32xx_udc,
2852 if (udc->enabled != 0) {
2853 /* Discharge VBUS real quick */
2854 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
2855 ISP1301_I2C_OTG_CONTROL_1, OTG1_VBUS_DISCHRG);
2857 /* Give VBUS some time (100mS) to discharge */
2860 /* Disable VBUS discharge resistor */
2861 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
2862 ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
2865 /* Clear interrupt */
2866 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
2867 ISP1301_I2C_INTERRUPT_LATCH |
2868 ISP1301_I2C_REG_CLEAR_ADDR, ~0);
2870 /* Get the VBUS status from the transceiver */
2871 value = i2c_smbus_read_byte_data(udc->isp1301_i2c_client,
2872 ISP1301_I2C_INTERRUPT_SOURCE);
2874 /* VBUS on or off? */
2875 if (value & INT_SESS_VLD)
2881 if (udc->last_vbus != udc->vbus) {
2882 udc->last_vbus = udc->vbus;
2883 lpc32xx_vbus_session(&udc->gadget, udc->vbus);
2887 /* Re-enable after completion */
2888 enable_irq(udc->udp_irq[IRQ_USB_ATX]);
2891 static irqreturn_t lpc32xx_usb_vbus_irq(int irq, void *_udc)
2893 struct lpc32xx_udc *udc = _udc;
2895 /* Defer handling of VBUS IRQ to work queue */
2896 disable_irq_nosync(udc->udp_irq[IRQ_USB_ATX]);
2897 schedule_work(&udc->vbus_job);
2902 static int lpc32xx_start(struct usb_gadget *gadget,
2903 struct usb_gadget_driver *driver)
2905 struct lpc32xx_udc *udc = to_udc(gadget);
2908 if (!driver || driver->max_speed < USB_SPEED_FULL || !driver->setup) {
2909 dev_err(udc->dev, "bad parameter.\n");
2914 dev_err(udc->dev, "UDC already has a gadget driver\n");
2918 udc->driver = driver;
2919 udc->gadget.dev.of_node = udc->dev->of_node;
2921 udc->gadget.is_selfpowered = 1;
2924 /* Force VBUS process once to check for cable insertion */
2925 udc->last_vbus = udc->vbus = 0;
2926 schedule_work(&udc->vbus_job);
2928 /* Do not re-enable ATX IRQ (3) */
2929 for (i = IRQ_USB_LP; i < IRQ_USB_ATX; i++)
2930 enable_irq(udc->udp_irq[i]);
2935 static int lpc32xx_stop(struct usb_gadget *gadget)
2938 struct lpc32xx_udc *udc = to_udc(gadget);
2940 for (i = IRQ_USB_LP; i <= IRQ_USB_ATX; i++)
2941 disable_irq(udc->udp_irq[i]);
2944 spin_lock(&udc->lock);
2946 spin_unlock(&udc->lock);
2949 * Wait for all the endpoints to disable,
2950 * before disabling clocks. Don't wait if
2951 * endpoints are not enabled.
2953 if (atomic_read(&udc->enabled_ep_cnt))
2954 wait_event_interruptible(udc->ep_disable_wait_queue,
2955 (atomic_read(&udc->enabled_ep_cnt) == 0));
2957 spin_lock(&udc->lock);
2958 udc_clk_set(udc, 0);
2959 spin_unlock(&udc->lock);
2968 static void lpc32xx_udc_shutdown(struct platform_device *dev)
2970 /* Force disconnect on reboot */
2971 struct lpc32xx_udc *udc = platform_get_drvdata(dev);
2977 * Callbacks to be overridden by options passed via OF (TODO)
2980 static void lpc32xx_usbd_conn_chg(int conn)
2982 /* Do nothing, it might be nice to enable an LED
2983 * based on conn state being !0 */
2986 static void lpc32xx_usbd_susp_chg(int susp)
2988 /* Device suspend if susp != 0 */
2991 static void lpc32xx_rmwkup_chg(int remote_wakup_enable)
2993 /* Enable or disable USB remote wakeup */
2996 struct lpc32xx_usbd_cfg lpc32xx_usbddata = {
2998 .conn_chgb = &lpc32xx_usbd_conn_chg,
2999 .susp_chgb = &lpc32xx_usbd_susp_chg,
3000 .rmwk_chgb = &lpc32xx_rmwkup_chg,
3004 static u64 lpc32xx_usbd_dmamask = ~(u32) 0x7F;
3006 static int lpc32xx_udc_probe(struct platform_device *pdev)
3008 struct device *dev = &pdev->dev;
3009 struct lpc32xx_udc *udc;
3011 struct resource *res;
3012 dma_addr_t dma_handle;
3013 struct device_node *isp1301_node;
3015 udc = kmemdup(&controller_template, sizeof(*udc), GFP_KERNEL);
3019 for (i = 0; i <= 15; i++)
3020 udc->ep[i].udc = udc;
3021 udc->gadget.ep0 = &udc->ep[0].ep;
3023 /* init software state */
3024 udc->gadget.dev.parent = dev;
3026 udc->dev = &pdev->dev;
3029 if (pdev->dev.of_node) {
3030 isp1301_node = of_parse_phandle(pdev->dev.of_node,
3033 isp1301_node = NULL;
3036 udc->isp1301_i2c_client = isp1301_get_client(isp1301_node);
3037 if (!udc->isp1301_i2c_client) {
3038 retval = -EPROBE_DEFER;
3042 dev_info(udc->dev, "ISP1301 I2C device at address 0x%x\n",
3043 udc->isp1301_i2c_client->addr);
3045 pdev->dev.dma_mask = &lpc32xx_usbd_dmamask;
3046 retval = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
3050 udc->board = &lpc32xx_usbddata;
3053 * Resources are mapped as follows:
3054 * IORESOURCE_MEM, base address and size of USB space
3055 * IORESOURCE_IRQ, USB device low priority interrupt number
3056 * IORESOURCE_IRQ, USB device high priority interrupt number
3057 * IORESOURCE_IRQ, USB device interrupt number
3058 * IORESOURCE_IRQ, USB transceiver interrupt number
3060 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3066 spin_lock_init(&udc->lock);
3069 for (i = 0; i < 4; i++) {
3070 udc->udp_irq[i] = platform_get_irq(pdev, i);
3071 if (udc->udp_irq[i] < 0) {
3073 "irq resource %d not available!\n", i);
3074 retval = udc->udp_irq[i];
3079 udc->io_p_start = res->start;
3080 udc->io_p_size = resource_size(res);
3081 if (!request_mem_region(udc->io_p_start, udc->io_p_size, driver_name)) {
3082 dev_err(udc->dev, "someone's using UDC memory\n");
3084 goto request_mem_region_fail;
3087 udc->udp_baseaddr = ioremap(udc->io_p_start, udc->io_p_size);
3088 if (!udc->udp_baseaddr) {
3090 dev_err(udc->dev, "IO map failure\n");
3094 /* Get USB device clock */
3095 udc->usb_slv_clk = clk_get(&pdev->dev, NULL);
3096 if (IS_ERR(udc->usb_slv_clk)) {
3097 dev_err(udc->dev, "failed to acquire USB device clock\n");
3098 retval = PTR_ERR(udc->usb_slv_clk);
3099 goto usb_clk_get_fail;
3102 /* Enable USB device clock */
3103 retval = clk_prepare_enable(udc->usb_slv_clk);
3105 dev_err(udc->dev, "failed to start USB device clock\n");
3106 goto usb_clk_enable_fail;
3109 /* Setup deferred workqueue data */
3110 udc->poweron = udc->pullup = 0;
3111 INIT_WORK(&udc->pullup_job, pullup_work);
3112 INIT_WORK(&udc->vbus_job, vbus_work);
3114 INIT_WORK(&udc->power_job, power_work);
3117 /* All clocks are now on */
3120 isp1301_udc_configure(udc);
3121 /* Allocate memory for the UDCA */
3122 udc->udca_v_base = dma_alloc_coherent(&pdev->dev, UDCA_BUFF_SIZE,
3124 (GFP_KERNEL | GFP_DMA));
3125 if (!udc->udca_v_base) {
3126 dev_err(udc->dev, "error getting UDCA region\n");
3130 udc->udca_p_base = dma_handle;
3131 dev_dbg(udc->dev, "DMA buffer(0x%x bytes), P:0x%08x, V:0x%p\n",
3132 UDCA_BUFF_SIZE, udc->udca_p_base, udc->udca_v_base);
3134 /* Setup the DD DMA memory pool */
3135 udc->dd_cache = dma_pool_create("udc_dd", udc->dev,
3136 sizeof(struct lpc32xx_usbd_dd_gad),
3138 if (!udc->dd_cache) {
3139 dev_err(udc->dev, "error getting DD DMA region\n");
3141 goto dma_alloc_fail;
3144 /* Clear USB peripheral and initialize gadget endpoints */
3148 /* Request IRQs - low and high priority USB device IRQs are routed to
3149 * the same handler, while the DMA interrupt is routed elsewhere */
3150 retval = request_irq(udc->udp_irq[IRQ_USB_LP], lpc32xx_usb_lp_irq,
3153 dev_err(udc->dev, "LP request irq %d failed\n",
3154 udc->udp_irq[IRQ_USB_LP]);
3157 retval = request_irq(udc->udp_irq[IRQ_USB_HP], lpc32xx_usb_hp_irq,
3160 dev_err(udc->dev, "HP request irq %d failed\n",
3161 udc->udp_irq[IRQ_USB_HP]);
3165 retval = request_irq(udc->udp_irq[IRQ_USB_DEVDMA],
3166 lpc32xx_usb_devdma_irq, 0, "udc_dma", udc);
3168 dev_err(udc->dev, "DEV request irq %d failed\n",
3169 udc->udp_irq[IRQ_USB_DEVDMA]);
3173 /* The transceiver interrupt is used for VBUS detection and will
3174 kick off the VBUS handler function */
3175 retval = request_irq(udc->udp_irq[IRQ_USB_ATX], lpc32xx_usb_vbus_irq,
3178 dev_err(udc->dev, "VBUS request irq %d failed\n",
3179 udc->udp_irq[IRQ_USB_ATX]);
3183 /* Initialize wait queue */
3184 init_waitqueue_head(&udc->ep_disable_wait_queue);
3185 atomic_set(&udc->enabled_ep_cnt, 0);
3187 /* Keep all IRQs disabled until GadgetFS starts up */
3188 for (i = IRQ_USB_LP; i <= IRQ_USB_ATX; i++)
3189 disable_irq(udc->udp_irq[i]);
3191 retval = usb_add_gadget_udc(dev, &udc->gadget);
3193 goto add_gadget_fail;
3195 dev_set_drvdata(dev, udc);
3196 device_init_wakeup(dev, 1);
3197 create_debug_file(udc);
3199 /* Disable clocks for now */
3200 udc_clk_set(udc, 0);
3202 dev_info(udc->dev, "%s version %s\n", driver_name, DRIVER_VERSION);
3206 free_irq(udc->udp_irq[IRQ_USB_ATX], udc);
3208 free_irq(udc->udp_irq[IRQ_USB_DEVDMA], udc);
3210 free_irq(udc->udp_irq[IRQ_USB_HP], udc);
3212 free_irq(udc->udp_irq[IRQ_USB_LP], udc);
3214 dma_pool_destroy(udc->dd_cache);
3216 dma_free_coherent(&pdev->dev, UDCA_BUFF_SIZE,
3217 udc->udca_v_base, udc->udca_p_base);
3219 clk_disable_unprepare(udc->usb_slv_clk);
3220 usb_clk_enable_fail:
3221 clk_put(udc->usb_slv_clk);
3223 iounmap(udc->udp_baseaddr);
3225 release_mem_region(udc->io_p_start, udc->io_p_size);
3226 dev_err(udc->dev, "%s probe failed, %d\n", driver_name, retval);
3227 request_mem_region_fail:
3235 static int lpc32xx_udc_remove(struct platform_device *pdev)
3237 struct lpc32xx_udc *udc = platform_get_drvdata(pdev);
3239 usb_del_gadget_udc(&udc->gadget);
3243 udc_clk_set(udc, 1);
3247 free_irq(udc->udp_irq[IRQ_USB_ATX], udc);
3249 device_init_wakeup(&pdev->dev, 0);
3250 remove_debug_file(udc);
3252 dma_pool_destroy(udc->dd_cache);
3253 dma_free_coherent(&pdev->dev, UDCA_BUFF_SIZE,
3254 udc->udca_v_base, udc->udca_p_base);
3255 free_irq(udc->udp_irq[IRQ_USB_DEVDMA], udc);
3256 free_irq(udc->udp_irq[IRQ_USB_HP], udc);
3257 free_irq(udc->udp_irq[IRQ_USB_LP], udc);
3259 clk_disable_unprepare(udc->usb_slv_clk);
3260 clk_put(udc->usb_slv_clk);
3262 iounmap(udc->udp_baseaddr);
3263 release_mem_region(udc->io_p_start, udc->io_p_size);
3270 static int lpc32xx_udc_suspend(struct platform_device *pdev, pm_message_t mesg)
3272 struct lpc32xx_udc *udc = platform_get_drvdata(pdev);
3275 /* Power down ISP */
3277 isp1301_set_powerstate(udc, 0);
3279 /* Disable clocking */
3280 udc_clk_set(udc, 0);
3282 /* Keep clock flag on, so we know to re-enable clocks
3286 /* Kill global USB clock */
3287 clk_disable_unprepare(udc->usb_slv_clk);
3293 static int lpc32xx_udc_resume(struct platform_device *pdev)
3295 struct lpc32xx_udc *udc = platform_get_drvdata(pdev);
3298 /* Enable global USB clock */
3299 clk_prepare_enable(udc->usb_slv_clk);
3301 /* Enable clocking */
3302 udc_clk_set(udc, 1);
3304 /* ISP back to normal power mode */
3306 isp1301_set_powerstate(udc, 1);
3312 #define lpc32xx_udc_suspend NULL
3313 #define lpc32xx_udc_resume NULL
3317 static const struct of_device_id lpc32xx_udc_of_match[] = {
3318 { .compatible = "nxp,lpc3220-udc", },
3321 MODULE_DEVICE_TABLE(of, lpc32xx_udc_of_match);
3324 static struct platform_driver lpc32xx_udc_driver = {
3325 .remove = lpc32xx_udc_remove,
3326 .shutdown = lpc32xx_udc_shutdown,
3327 .suspend = lpc32xx_udc_suspend,
3328 .resume = lpc32xx_udc_resume,
3330 .name = (char *) driver_name,
3331 .of_match_table = of_match_ptr(lpc32xx_udc_of_match),
3335 module_platform_driver_probe(lpc32xx_udc_driver, lpc32xx_udc_probe);
3337 MODULE_DESCRIPTION("LPC32XX udc driver");
3338 MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>");
3339 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
3340 MODULE_LICENSE("GPL");
3341 MODULE_ALIAS("platform:lpc32xx_udc");