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/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>
52 #include <linux/usb/isp1301.h>
54 #include <asm/byteorder.h>
55 #include <mach/hardware.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>
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
90 #define IRQ_USB_DEVDMA 2
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))
100 #define EP_ISO_TYPE 1
101 #define EP_BLK_TYPE 2
102 #define EP_CTL_TYPE 3
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 {
117 u32 dd_iso_ps_mem_addr;
119 u32 iso_status[6]; /* 5 spare */
124 * Logical endpoint structure
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 */
146 * Common UDC structure
149 struct usb_gadget gadget;
150 struct usb_gadget_driver *driver;
151 struct platform_device *pdev;
155 struct i2c_client *isp1301_i2c_client;
157 /* Board and device specific */
158 struct lpc32xx_usbd_cfg *board;
161 void __iomem *udp_baseaddr;
163 struct clk *usb_pll_clk;
164 struct clk *usb_slv_clk;
165 struct clk *usb_otg_clk;
170 struct dma_pool *dd_cache;
172 /* Common EP and control data */
174 u32 enabled_hwepints;
178 /* VBUS detection, pullup, and power flags */
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];
195 atomic_t enabled_ep_cnt;
196 wait_queue_head_t ep_disable_wait_queue;
202 struct lpc32xx_request {
203 struct usb_request req;
204 struct list_head queue;
205 struct lpc32xx_usbd_dd_gad *dd_desc_ptr;
210 static inline struct lpc32xx_udc *to_udc(struct usb_gadget *g)
212 return container_of(g, struct lpc32xx_udc, gadget);
215 #define ep_dbg(epp, fmt, arg...) \
216 dev_dbg(epp->udc->dev, "%s: " fmt, __func__, ## arg)
217 #define ep_err(epp, fmt, arg...) \
218 dev_err(epp->udc->dev, "%s: " fmt, __func__, ## arg)
219 #define ep_info(epp, fmt, arg...) \
220 dev_info(epp->udc->dev, "%s: " fmt, __func__, ## arg)
221 #define ep_warn(epp, fmt, arg...) \
222 dev_warn(epp->udc->dev, "%s:" fmt, __func__, ## arg)
224 #define UDCA_BUFF_SIZE (128)
226 /* TODO: When the clock framework is introduced in LPC32xx, IO_ADDRESS will
227 * be replaced with an inremap()ed pointer
229 #define USB_CTRL IO_ADDRESS(LPC32XX_CLK_PM_BASE + 0x64)
231 /* USB_CTRL bit defines */
232 #define USB_SLAVE_HCLK_EN (1 << 24)
233 #define USB_HOST_NEED_CLK_EN (1 << 21)
234 #define USB_DEV_NEED_CLK_EN (1 << 22)
236 /**********************************************************************
237 * USB device controller register offsets
238 **********************************************************************/
240 #define USBD_DEVINTST(x) ((x) + 0x200)
241 #define USBD_DEVINTEN(x) ((x) + 0x204)
242 #define USBD_DEVINTCLR(x) ((x) + 0x208)
243 #define USBD_DEVINTSET(x) ((x) + 0x20C)
244 #define USBD_CMDCODE(x) ((x) + 0x210)
245 #define USBD_CMDDATA(x) ((x) + 0x214)
246 #define USBD_RXDATA(x) ((x) + 0x218)
247 #define USBD_TXDATA(x) ((x) + 0x21C)
248 #define USBD_RXPLEN(x) ((x) + 0x220)
249 #define USBD_TXPLEN(x) ((x) + 0x224)
250 #define USBD_CTRL(x) ((x) + 0x228)
251 #define USBD_DEVINTPRI(x) ((x) + 0x22C)
252 #define USBD_EPINTST(x) ((x) + 0x230)
253 #define USBD_EPINTEN(x) ((x) + 0x234)
254 #define USBD_EPINTCLR(x) ((x) + 0x238)
255 #define USBD_EPINTSET(x) ((x) + 0x23C)
256 #define USBD_EPINTPRI(x) ((x) + 0x240)
257 #define USBD_REEP(x) ((x) + 0x244)
258 #define USBD_EPIND(x) ((x) + 0x248)
259 #define USBD_EPMAXPSIZE(x) ((x) + 0x24C)
260 /* DMA support registers only below */
261 /* Set, clear, or get enabled state of the DMA request status. If
262 * enabled, an IN or OUT token will start a DMA transfer for the EP */
263 #define USBD_DMARST(x) ((x) + 0x250)
264 #define USBD_DMARCLR(x) ((x) + 0x254)
265 #define USBD_DMARSET(x) ((x) + 0x258)
266 /* DMA UDCA head pointer */
267 #define USBD_UDCAH(x) ((x) + 0x280)
268 /* EP DMA status, enable, and disable. This is used to specifically
269 * enabled or disable DMA for a specific EP */
270 #define USBD_EPDMAST(x) ((x) + 0x284)
271 #define USBD_EPDMAEN(x) ((x) + 0x288)
272 #define USBD_EPDMADIS(x) ((x) + 0x28C)
273 /* DMA master interrupts enable and pending interrupts */
274 #define USBD_DMAINTST(x) ((x) + 0x290)
275 #define USBD_DMAINTEN(x) ((x) + 0x294)
276 /* DMA end of transfer interrupt enable, disable, status */
277 #define USBD_EOTINTST(x) ((x) + 0x2A0)
278 #define USBD_EOTINTCLR(x) ((x) + 0x2A4)
279 #define USBD_EOTINTSET(x) ((x) + 0x2A8)
280 /* New DD request interrupt enable, disable, status */
281 #define USBD_NDDRTINTST(x) ((x) + 0x2AC)
282 #define USBD_NDDRTINTCLR(x) ((x) + 0x2B0)
283 #define USBD_NDDRTINTSET(x) ((x) + 0x2B4)
284 /* DMA error interrupt enable, disable, status */
285 #define USBD_SYSERRTINTST(x) ((x) + 0x2B8)
286 #define USBD_SYSERRTINTCLR(x) ((x) + 0x2BC)
287 #define USBD_SYSERRTINTSET(x) ((x) + 0x2C0)
289 /**********************************************************************
290 * USBD_DEVINTST/USBD_DEVINTEN/USBD_DEVINTCLR/USBD_DEVINTSET/
291 * USBD_DEVINTPRI register definitions
292 **********************************************************************/
293 #define USBD_ERR_INT (1 << 9)
294 #define USBD_EP_RLZED (1 << 8)
295 #define USBD_TXENDPKT (1 << 7)
296 #define USBD_RXENDPKT (1 << 6)
297 #define USBD_CDFULL (1 << 5)
298 #define USBD_CCEMPTY (1 << 4)
299 #define USBD_DEV_STAT (1 << 3)
300 #define USBD_EP_SLOW (1 << 2)
301 #define USBD_EP_FAST (1 << 1)
302 #define USBD_FRAME (1 << 0)
304 /**********************************************************************
305 * USBD_EPINTST/USBD_EPINTEN/USBD_EPINTCLR/USBD_EPINTSET/
306 * USBD_EPINTPRI register definitions
307 **********************************************************************/
308 /* End point selection macro (RX) */
309 #define USBD_RX_EP_SEL(e) (1 << ((e) << 1))
311 /* End point selection macro (TX) */
312 #define USBD_TX_EP_SEL(e) (1 << (((e) << 1) + 1))
314 /**********************************************************************
315 * USBD_REEP/USBD_DMARST/USBD_DMARCLR/USBD_DMARSET/USBD_EPDMAST/
316 * USBD_EPDMAEN/USBD_EPDMADIS/
317 * USBD_NDDRTINTST/USBD_NDDRTINTCLR/USBD_NDDRTINTSET/
318 * USBD_EOTINTST/USBD_EOTINTCLR/USBD_EOTINTSET/
319 * USBD_SYSERRTINTST/USBD_SYSERRTINTCLR/USBD_SYSERRTINTSET
320 * register definitions
321 **********************************************************************/
322 /* Endpoint selection macro */
323 #define USBD_EP_SEL(e) (1 << (e))
325 /**********************************************************************
326 * SBD_DMAINTST/USBD_DMAINTEN
327 **********************************************************************/
328 #define USBD_SYS_ERR_INT (1 << 2)
329 #define USBD_NEW_DD_INT (1 << 1)
330 #define USBD_EOT_INT (1 << 0)
332 /**********************************************************************
333 * USBD_RXPLEN register definitions
334 **********************************************************************/
335 #define USBD_PKT_RDY (1 << 11)
336 #define USBD_DV (1 << 10)
337 #define USBD_PK_LEN_MASK 0x3FF
339 /**********************************************************************
340 * USBD_CTRL register definitions
341 **********************************************************************/
342 #define USBD_LOG_ENDPOINT(e) ((e) << 2)
343 #define USBD_WR_EN (1 << 1)
344 #define USBD_RD_EN (1 << 0)
346 /**********************************************************************
347 * USBD_CMDCODE register definitions
348 **********************************************************************/
349 #define USBD_CMD_CODE(c) ((c) << 16)
350 #define USBD_CMD_PHASE(p) ((p) << 8)
352 /**********************************************************************
353 * USBD_DMARST/USBD_DMARCLR/USBD_DMARSET register definitions
354 **********************************************************************/
355 #define USBD_DMAEP(e) (1 << (e))
357 /* DD (DMA Descriptor) structure, requires word alignment */
358 struct lpc32xx_usbd_dd {
363 u32 dd_iso_ps_mem_addr;
366 /* dd_setup bit defines */
367 #define DD_SETUP_ATLE_DMA_MODE 0x01
368 #define DD_SETUP_NEXT_DD_VALID 0x04
369 #define DD_SETUP_ISO_EP 0x10
370 #define DD_SETUP_PACKETLEN(n) (((n) & 0x7FF) << 5)
371 #define DD_SETUP_DMALENBYTES(n) (((n) & 0xFFFF) << 16)
373 /* dd_status bit defines */
374 #define DD_STATUS_DD_RETIRED 0x01
375 #define DD_STATUS_STS_MASK 0x1E
376 #define DD_STATUS_STS_NS 0x00 /* Not serviced */
377 #define DD_STATUS_STS_BS 0x02 /* Being serviced */
378 #define DD_STATUS_STS_NC 0x04 /* Normal completion */
379 #define DD_STATUS_STS_DUR 0x06 /* Data underrun (short packet) */
380 #define DD_STATUS_STS_DOR 0x08 /* Data overrun */
381 #define DD_STATUS_STS_SE 0x12 /* System error */
382 #define DD_STATUS_PKT_VAL 0x20 /* Packet valid */
383 #define DD_STATUS_LSB_EX 0x40 /* LS byte extracted (ATLE) */
384 #define DD_STATUS_MSB_EX 0x80 /* MS byte extracted (ATLE) */
385 #define DD_STATUS_MLEN(n) (((n) >> 8) & 0x3F)
386 #define DD_STATUS_CURDMACNT(n) (((n) >> 16) & 0xFFFF)
390 * Protocol engine bits below
393 /* Device Interrupt Bit Definitions */
394 #define FRAME_INT 0x00000001
395 #define EP_FAST_INT 0x00000002
396 #define EP_SLOW_INT 0x00000004
397 #define DEV_STAT_INT 0x00000008
398 #define CCEMTY_INT 0x00000010
399 #define CDFULL_INT 0x00000020
400 #define RxENDPKT_INT 0x00000040
401 #define TxENDPKT_INT 0x00000080
402 #define EP_RLZED_INT 0x00000100
403 #define ERR_INT 0x00000200
405 /* Rx & Tx Packet Length Definitions */
406 #define PKT_LNGTH_MASK 0x000003FF
407 #define PKT_DV 0x00000400
408 #define PKT_RDY 0x00000800
410 /* USB Control Definitions */
411 #define CTRL_RD_EN 0x00000001
412 #define CTRL_WR_EN 0x00000002
415 #define CMD_SET_ADDR 0x00D00500
416 #define CMD_CFG_DEV 0x00D80500
417 #define CMD_SET_MODE 0x00F30500
418 #define CMD_RD_FRAME 0x00F50500
419 #define DAT_RD_FRAME 0x00F50200
420 #define CMD_RD_TEST 0x00FD0500
421 #define DAT_RD_TEST 0x00FD0200
422 #define CMD_SET_DEV_STAT 0x00FE0500
423 #define CMD_GET_DEV_STAT 0x00FE0500
424 #define DAT_GET_DEV_STAT 0x00FE0200
425 #define CMD_GET_ERR_CODE 0x00FF0500
426 #define DAT_GET_ERR_CODE 0x00FF0200
427 #define CMD_RD_ERR_STAT 0x00FB0500
428 #define DAT_RD_ERR_STAT 0x00FB0200
429 #define DAT_WR_BYTE(x) (0x00000100 | ((x) << 16))
430 #define CMD_SEL_EP(x) (0x00000500 | ((x) << 16))
431 #define DAT_SEL_EP(x) (0x00000200 | ((x) << 16))
432 #define CMD_SEL_EP_CLRI(x) (0x00400500 | ((x) << 16))
433 #define DAT_SEL_EP_CLRI(x) (0x00400200 | ((x) << 16))
434 #define CMD_SET_EP_STAT(x) (0x00400500 | ((x) << 16))
435 #define CMD_CLR_BUF 0x00F20500
436 #define DAT_CLR_BUF 0x00F20200
437 #define CMD_VALID_BUF 0x00FA0500
439 /* Device Address Register Definitions */
440 #define DEV_ADDR_MASK 0x7F
443 /* Device Configure Register Definitions */
444 #define CONF_DVICE 0x01
446 /* Device Mode Register Definitions */
455 /* Device Status Register Definitions */
457 #define DEV_CON_CH 0x02
459 #define DEV_SUS_CH 0x08
462 /* Error Code Register Definitions */
463 #define ERR_EC_MASK 0x0F
466 /* Error Status Register Definitions */
468 #define ERR_UEPKT 0x02
469 #define ERR_DCRC 0x04
470 #define ERR_TIMOUT 0x08
472 #define ERR_B_OVRN 0x20
473 #define ERR_BTSTF 0x40
476 /* Endpoint Select Register Definitions */
477 #define EP_SEL_F 0x01
478 #define EP_SEL_ST 0x02
479 #define EP_SEL_STP 0x04
480 #define EP_SEL_PO 0x08
481 #define EP_SEL_EPN 0x10
482 #define EP_SEL_B_1_FULL 0x20
483 #define EP_SEL_B_2_FULL 0x40
485 /* Endpoint Status Register Definitions */
486 #define EP_STAT_ST 0x01
487 #define EP_STAT_DA 0x20
488 #define EP_STAT_RF_MO 0x40
489 #define EP_STAT_CND_ST 0x80
491 /* Clear Buffer Register Definitions */
492 #define CLR_BUF_PO 0x01
494 /* DMA Interrupt Bit Definitions */
496 #define NDD_REQ_INT 0x02
497 #define SYS_ERR_INT 0x04
499 #define DRIVER_VERSION "1.03"
500 static const char driver_name[] = "lpc32xx_udc";
504 * proc interface support
507 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
508 static char *epnames[] = {"INT", "ISO", "BULK", "CTRL"};
509 static const char debug_filename[] = "driver/udc";
511 static void proc_ep_show(struct seq_file *s, struct lpc32xx_ep *ep)
513 struct lpc32xx_request *req;
516 seq_printf(s, "%12s, maxpacket %4d %3s",
517 ep->ep.name, ep->ep.maxpacket,
518 ep->is_in ? "in" : "out");
519 seq_printf(s, " type %4s", epnames[ep->eptype]);
520 seq_printf(s, " ints: %12d", ep->totalints);
522 if (list_empty(&ep->queue))
523 seq_printf(s, "\t(queue empty)\n");
525 list_for_each_entry(req, &ep->queue, queue) {
526 u32 length = req->req.actual;
528 seq_printf(s, "\treq %p len %d/%d buf %p\n",
530 req->req.length, req->req.buf);
535 static int proc_udc_show(struct seq_file *s, void *unused)
537 struct lpc32xx_udc *udc = s->private;
538 struct lpc32xx_ep *ep;
541 seq_printf(s, "%s: version %s\n", driver_name, DRIVER_VERSION);
543 spin_lock_irqsave(&udc->lock, flags);
545 seq_printf(s, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
546 udc->vbus ? "present" : "off",
547 udc->enabled ? (udc->vbus ? "active" : "enabled") :
549 udc->gadget.is_selfpowered ? "self" : "VBUS",
550 udc->suspended ? ", suspended" : "",
551 udc->driver ? udc->driver->driver.name : "(none)");
553 if (udc->enabled && udc->vbus) {
554 proc_ep_show(s, &udc->ep[0]);
555 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list)
559 spin_unlock_irqrestore(&udc->lock, flags);
564 static int proc_udc_open(struct inode *inode, struct file *file)
566 return single_open(file, proc_udc_show, PDE_DATA(inode));
569 static const struct file_operations proc_ops = {
570 .owner = THIS_MODULE,
571 .open = proc_udc_open,
574 .release = single_release,
577 static void create_debug_file(struct lpc32xx_udc *udc)
579 udc->pde = debugfs_create_file(debug_filename, 0, NULL, udc, &proc_ops);
582 static void remove_debug_file(struct lpc32xx_udc *udc)
584 debugfs_remove(udc->pde);
588 static inline void create_debug_file(struct lpc32xx_udc *udc) {}
589 static inline void remove_debug_file(struct lpc32xx_udc *udc) {}
592 /* Primary initialization sequence for the ISP1301 transceiver */
593 static void isp1301_udc_configure(struct lpc32xx_udc *udc)
595 /* LPC32XX only supports DAT_SE0 USB mode */
596 /* This sequence is important */
598 /* Disable transparent UART mode first */
599 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
600 (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
603 /* Set full speed and SE0 mode */
604 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
605 (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), ~0);
606 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
607 ISP1301_I2C_MODE_CONTROL_1, (MC1_SPEED_REG | MC1_DAT_SE0));
610 * The PSW_OE enable bit state is reversed in the ISP1301 User's Guide
612 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
613 (ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR), ~0);
614 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
615 ISP1301_I2C_MODE_CONTROL_2, (MC2_BI_DI | MC2_SPD_SUSP_CTRL));
617 /* Driver VBUS_DRV high or low depending on board setup */
618 if (udc->board->vbus_drv_pol != 0)
619 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
620 ISP1301_I2C_OTG_CONTROL_1, OTG1_VBUS_DRV);
622 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
623 ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
626 /* Bi-directional mode with suspend control
627 * Enable both pulldowns for now - the pullup will be enable when VBUS
629 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
630 (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), ~0);
631 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
632 ISP1301_I2C_OTG_CONTROL_1,
633 (0 | OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
635 /* Discharge VBUS (just in case) */
636 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
637 ISP1301_I2C_OTG_CONTROL_1, OTG1_VBUS_DISCHRG);
639 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
640 (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
643 /* Clear and enable VBUS high edge interrupt */
644 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
645 ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
646 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
647 ISP1301_I2C_INTERRUPT_FALLING | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
648 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
649 ISP1301_I2C_INTERRUPT_FALLING, INT_VBUS_VLD);
650 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
651 ISP1301_I2C_INTERRUPT_RISING | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
652 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
653 ISP1301_I2C_INTERRUPT_RISING, INT_VBUS_VLD);
655 /* Enable usb_need_clk clock after transceiver is initialized */
656 writel((readl(USB_CTRL) | USB_DEV_NEED_CLK_EN), USB_CTRL);
658 dev_info(udc->dev, "ISP1301 Vendor ID : 0x%04x\n",
659 i2c_smbus_read_word_data(udc->isp1301_i2c_client, 0x00));
660 dev_info(udc->dev, "ISP1301 Product ID : 0x%04x\n",
661 i2c_smbus_read_word_data(udc->isp1301_i2c_client, 0x02));
662 dev_info(udc->dev, "ISP1301 Version ID : 0x%04x\n",
663 i2c_smbus_read_word_data(udc->isp1301_i2c_client, 0x14));
666 /* Enables or disables the USB device pullup via the ISP1301 transceiver */
667 static void isp1301_pullup_set(struct lpc32xx_udc *udc)
670 /* Enable pullup for bus signalling */
671 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
672 ISP1301_I2C_OTG_CONTROL_1, OTG1_DP_PULLUP);
674 /* Enable pullup for bus signalling */
675 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
676 ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
680 static void pullup_work(struct work_struct *work)
682 struct lpc32xx_udc *udc =
683 container_of(work, struct lpc32xx_udc, pullup_job);
685 isp1301_pullup_set(udc);
688 static void isp1301_pullup_enable(struct lpc32xx_udc *udc, int en_pullup,
691 if (en_pullup == udc->pullup)
694 udc->pullup = en_pullup;
696 isp1301_pullup_set(udc);
698 /* defer slow i2c pull up setting */
699 schedule_work(&udc->pullup_job);
703 /* Powers up or down the ISP1301 transceiver */
704 static void isp1301_set_powerstate(struct lpc32xx_udc *udc, int enable)
707 /* Power up ISP1301 - this ISP1301 will automatically wakeup
708 when VBUS is detected */
709 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
710 ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR,
713 /* Power down ISP1301 */
714 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
715 ISP1301_I2C_MODE_CONTROL_2, MC2_GLOBAL_PWR_DN);
718 static void power_work(struct work_struct *work)
720 struct lpc32xx_udc *udc =
721 container_of(work, struct lpc32xx_udc, power_job);
723 isp1301_set_powerstate(udc, udc->poweron);
729 * USB protocol engine command/data read/write helper functions
732 /* Issues a single command to the USB device state machine */
733 static void udc_protocol_cmd_w(struct lpc32xx_udc *udc, u32 cmd)
738 /* EP may lock on CLRI if this read isn't done */
739 u32 tmp = readl(USBD_DEVINTST(udc->udp_baseaddr));
743 writel(USBD_CCEMPTY, USBD_DEVINTCLR(udc->udp_baseaddr));
745 /* Write command code */
746 writel(cmd, USBD_CMDCODE(udc->udp_baseaddr));
748 while (((readl(USBD_DEVINTST(udc->udp_baseaddr)) &
749 USBD_CCEMPTY) == 0) && (to > 0)) {
760 /* Issues 2 commands (or command and data) to the USB device state machine */
761 static inline void udc_protocol_cmd_data_w(struct lpc32xx_udc *udc, u32 cmd,
764 udc_protocol_cmd_w(udc, cmd);
765 udc_protocol_cmd_w(udc, data);
768 /* Issues a single command to the USB device state machine and reads
770 static u32 udc_protocol_cmd_r(struct lpc32xx_udc *udc, u32 cmd)
775 /* Write a command and read data from the protocol engine */
776 writel((USBD_CDFULL | USBD_CCEMPTY),
777 USBD_DEVINTCLR(udc->udp_baseaddr));
779 /* Write command code */
780 udc_protocol_cmd_w(udc, cmd);
782 tmp = readl(USBD_DEVINTST(udc->udp_baseaddr));
783 while ((!(readl(USBD_DEVINTST(udc->udp_baseaddr)) & USBD_CDFULL))
788 "Protocol engine didn't receive response (CDFULL)\n");
790 return readl(USBD_CMDDATA(udc->udp_baseaddr));
795 * USB device interrupt mask support functions
798 /* Enable one or more USB device interrupts */
799 static inline void uda_enable_devint(struct lpc32xx_udc *udc, u32 devmask)
801 udc->enabled_devints |= devmask;
802 writel(udc->enabled_devints, USBD_DEVINTEN(udc->udp_baseaddr));
805 /* Disable one or more USB device interrupts */
806 static inline void uda_disable_devint(struct lpc32xx_udc *udc, u32 mask)
808 udc->enabled_devints &= ~mask;
809 writel(udc->enabled_devints, USBD_DEVINTEN(udc->udp_baseaddr));
812 /* Clear one or more USB device interrupts */
813 static inline void uda_clear_devint(struct lpc32xx_udc *udc, u32 mask)
815 writel(mask, USBD_DEVINTCLR(udc->udp_baseaddr));
820 * Endpoint interrupt disable/enable functions
823 /* Enable one or more USB endpoint interrupts */
824 static void uda_enable_hwepint(struct lpc32xx_udc *udc, u32 hwep)
826 udc->enabled_hwepints |= (1 << hwep);
827 writel(udc->enabled_hwepints, USBD_EPINTEN(udc->udp_baseaddr));
830 /* Disable one or more USB endpoint interrupts */
831 static void uda_disable_hwepint(struct lpc32xx_udc *udc, u32 hwep)
833 udc->enabled_hwepints &= ~(1 << hwep);
834 writel(udc->enabled_hwepints, USBD_EPINTEN(udc->udp_baseaddr));
837 /* Clear one or more USB endpoint interrupts */
838 static inline void uda_clear_hwepint(struct lpc32xx_udc *udc, u32 hwep)
840 writel((1 << hwep), USBD_EPINTCLR(udc->udp_baseaddr));
843 /* Enable DMA for the HW channel */
844 static inline void udc_ep_dma_enable(struct lpc32xx_udc *udc, u32 hwep)
846 writel((1 << hwep), USBD_EPDMAEN(udc->udp_baseaddr));
849 /* Disable DMA for the HW channel */
850 static inline void udc_ep_dma_disable(struct lpc32xx_udc *udc, u32 hwep)
852 writel((1 << hwep), USBD_EPDMADIS(udc->udp_baseaddr));
857 * Endpoint realize/unrealize functions
860 /* Before an endpoint can be used, it needs to be realized
861 * in the USB protocol engine - this realizes the endpoint.
862 * The interrupt (FIFO or DMA) is not enabled with this function */
863 static void udc_realize_hwep(struct lpc32xx_udc *udc, u32 hwep,
868 writel(USBD_EP_RLZED, USBD_DEVINTCLR(udc->udp_baseaddr));
869 writel(hwep, USBD_EPIND(udc->udp_baseaddr));
870 udc->realized_eps |= (1 << hwep);
871 writel(udc->realized_eps, USBD_REEP(udc->udp_baseaddr));
872 writel(maxpacket, USBD_EPMAXPSIZE(udc->udp_baseaddr));
874 /* Wait until endpoint is realized in hardware */
875 while ((!(readl(USBD_DEVINTST(udc->udp_baseaddr)) &
876 USBD_EP_RLZED)) && (to > 0))
879 dev_dbg(udc->dev, "EP not correctly realized in hardware\n");
881 writel(USBD_EP_RLZED, USBD_DEVINTCLR(udc->udp_baseaddr));
884 /* Unrealize an EP */
885 static void udc_unrealize_hwep(struct lpc32xx_udc *udc, u32 hwep)
887 udc->realized_eps &= ~(1 << hwep);
888 writel(udc->realized_eps, USBD_REEP(udc->udp_baseaddr));
893 * Endpoint support functions
896 /* Select and clear endpoint interrupt */
897 static u32 udc_selep_clrint(struct lpc32xx_udc *udc, u32 hwep)
899 udc_protocol_cmd_w(udc, CMD_SEL_EP_CLRI(hwep));
900 return udc_protocol_cmd_r(udc, DAT_SEL_EP_CLRI(hwep));
903 /* Disables the endpoint in the USB protocol engine */
904 static void udc_disable_hwep(struct lpc32xx_udc *udc, u32 hwep)
906 udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(hwep),
907 DAT_WR_BYTE(EP_STAT_DA));
910 /* Stalls the endpoint - endpoint will return STALL */
911 static void udc_stall_hwep(struct lpc32xx_udc *udc, u32 hwep)
913 udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(hwep),
914 DAT_WR_BYTE(EP_STAT_ST));
917 /* Clear stall or reset endpoint */
918 static void udc_clrstall_hwep(struct lpc32xx_udc *udc, u32 hwep)
920 udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(hwep),
924 /* Select an endpoint for endpoint status, clear, validate */
925 static void udc_select_hwep(struct lpc32xx_udc *udc, u32 hwep)
927 udc_protocol_cmd_w(udc, CMD_SEL_EP(hwep));
932 * Endpoint buffer management functions
935 /* Clear the current endpoint's buffer */
936 static void udc_clr_buffer_hwep(struct lpc32xx_udc *udc, u32 hwep)
938 udc_select_hwep(udc, hwep);
939 udc_protocol_cmd_w(udc, CMD_CLR_BUF);
942 /* Validate the current endpoint's buffer */
943 static void udc_val_buffer_hwep(struct lpc32xx_udc *udc, u32 hwep)
945 udc_select_hwep(udc, hwep);
946 udc_protocol_cmd_w(udc, CMD_VALID_BUF);
949 static inline u32 udc_clearep_getsts(struct lpc32xx_udc *udc, u32 hwep)
951 /* Clear EP interrupt */
952 uda_clear_hwepint(udc, hwep);
953 return udc_selep_clrint(udc, hwep);
961 /* Allocate a DMA Descriptor */
962 static struct lpc32xx_usbd_dd_gad *udc_dd_alloc(struct lpc32xx_udc *udc)
965 struct lpc32xx_usbd_dd_gad *dd;
967 dd = dma_pool_alloc(udc->dd_cache, GFP_ATOMIC | GFP_DMA, &dma);
974 /* Free a DMA Descriptor */
975 static void udc_dd_free(struct lpc32xx_udc *udc, struct lpc32xx_usbd_dd_gad *dd)
977 dma_pool_free(udc->dd_cache, dd, dd->this_dma);
982 * USB setup and shutdown functions
985 /* Enables or disables most of the USB system clocks when low power mode is
986 * needed. Clocks are typically started on a connection event, and disabled
987 * when a cable is disconnected */
988 static void udc_clk_set(struct lpc32xx_udc *udc, int enable)
997 clk_enable(udc->usb_pll_clk);
999 /* Enable the USB device clock */
1000 writel(readl(USB_CTRL) | USB_DEV_NEED_CLK_EN,
1003 clk_enable(udc->usb_otg_clk);
1010 /* Never disable the USB_HCLK during normal operation */
1012 /* 48MHz PLL dpwn */
1013 clk_disable(udc->usb_pll_clk);
1015 /* Disable the USB device clock */
1016 writel(readl(USB_CTRL) & ~USB_DEV_NEED_CLK_EN,
1019 clk_disable(udc->usb_otg_clk);
1023 /* Set/reset USB device address */
1024 static void udc_set_address(struct lpc32xx_udc *udc, u32 addr)
1026 /* Address will be latched at the end of the status phase, or
1027 latched immediately if function is called twice */
1028 udc_protocol_cmd_data_w(udc, CMD_SET_ADDR,
1029 DAT_WR_BYTE(DEV_EN | addr));
1032 /* Setup up a IN request for DMA transfer - this consists of determining the
1033 * list of DMA addresses for the transfer, allocating DMA Descriptors,
1034 * installing the DD into the UDCA, and then enabling the DMA for that EP */
1035 static int udc_ep_in_req_dma(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
1037 struct lpc32xx_request *req;
1038 u32 hwep = ep->hwep_num;
1040 ep->req_pending = 1;
1042 /* There will always be a request waiting here */
1043 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
1045 /* Place the DD Descriptor into the UDCA */
1046 udc->udca_v_base[hwep] = req->dd_desc_ptr->this_dma;
1048 /* Enable DMA and interrupt for the HW EP */
1049 udc_ep_dma_enable(udc, hwep);
1051 /* Clear ZLP if last packet is not of MAXP size */
1052 if (req->req.length % ep->ep.maxpacket)
1058 /* Setup up a OUT request for DMA transfer - this consists of determining the
1059 * list of DMA addresses for the transfer, allocating DMA Descriptors,
1060 * installing the DD into the UDCA, and then enabling the DMA for that EP */
1061 static int udc_ep_out_req_dma(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
1063 struct lpc32xx_request *req;
1064 u32 hwep = ep->hwep_num;
1066 ep->req_pending = 1;
1068 /* There will always be a request waiting here */
1069 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
1071 /* Place the DD Descriptor into the UDCA */
1072 udc->udca_v_base[hwep] = req->dd_desc_ptr->this_dma;
1074 /* Enable DMA and interrupt for the HW EP */
1075 udc_ep_dma_enable(udc, hwep);
1079 static void udc_disable(struct lpc32xx_udc *udc)
1083 /* Disable device */
1084 udc_protocol_cmd_data_w(udc, CMD_CFG_DEV, DAT_WR_BYTE(0));
1085 udc_protocol_cmd_data_w(udc, CMD_SET_DEV_STAT, DAT_WR_BYTE(0));
1087 /* Disable all device interrupts (including EP0) */
1088 uda_disable_devint(udc, 0x3FF);
1090 /* Disable and reset all endpoint interrupts */
1091 for (i = 0; i < 32; i++) {
1092 uda_disable_hwepint(udc, i);
1093 uda_clear_hwepint(udc, i);
1094 udc_disable_hwep(udc, i);
1095 udc_unrealize_hwep(udc, i);
1096 udc->udca_v_base[i] = 0;
1098 /* Disable and clear all interrupts and DMA */
1099 udc_ep_dma_disable(udc, i);
1100 writel((1 << i), USBD_EOTINTCLR(udc->udp_baseaddr));
1101 writel((1 << i), USBD_NDDRTINTCLR(udc->udp_baseaddr));
1102 writel((1 << i), USBD_SYSERRTINTCLR(udc->udp_baseaddr));
1103 writel((1 << i), USBD_DMARCLR(udc->udp_baseaddr));
1106 /* Disable DMA interrupts */
1107 writel(0, USBD_DMAINTEN(udc->udp_baseaddr));
1109 writel(0, USBD_UDCAH(udc->udp_baseaddr));
1112 static void udc_enable(struct lpc32xx_udc *udc)
1115 struct lpc32xx_ep *ep = &udc->ep[0];
1117 /* Start with known state */
1121 udc_protocol_cmd_data_w(udc, CMD_SET_DEV_STAT, DAT_WR_BYTE(DEV_CON));
1123 /* EP interrupts on high priority, FRAME interrupt on low priority */
1124 writel(USBD_EP_FAST, USBD_DEVINTPRI(udc->udp_baseaddr));
1125 writel(0xFFFF, USBD_EPINTPRI(udc->udp_baseaddr));
1127 /* Clear any pending device interrupts */
1128 writel(0x3FF, USBD_DEVINTCLR(udc->udp_baseaddr));
1130 /* Setup UDCA - not yet used (DMA) */
1131 writel(udc->udca_p_base, USBD_UDCAH(udc->udp_baseaddr));
1133 /* Only enable EP0 in and out for now, EP0 only works in FIFO mode */
1134 for (i = 0; i <= 1; i++) {
1135 udc_realize_hwep(udc, i, ep->ep.maxpacket);
1136 uda_enable_hwepint(udc, i);
1137 udc_select_hwep(udc, i);
1138 udc_clrstall_hwep(udc, i);
1139 udc_clr_buffer_hwep(udc, i);
1142 /* Device interrupt setup */
1143 uda_clear_devint(udc, (USBD_ERR_INT | USBD_DEV_STAT | USBD_EP_SLOW |
1145 uda_enable_devint(udc, (USBD_ERR_INT | USBD_DEV_STAT | USBD_EP_SLOW |
1148 /* Set device address to 0 - called twice to force a latch in the USB
1149 engine without the need of a setup packet status closure */
1150 udc_set_address(udc, 0);
1151 udc_set_address(udc, 0);
1153 /* Enable master DMA interrupts */
1154 writel((USBD_SYS_ERR_INT | USBD_EOT_INT),
1155 USBD_DMAINTEN(udc->udp_baseaddr));
1157 udc->dev_status = 0;
1162 * USB device board specific events handled via callbacks
1165 /* Connection change event - notify board function of change */
1166 static void uda_power_event(struct lpc32xx_udc *udc, u32 conn)
1168 /* Just notify of a connection change event (optional) */
1169 if (udc->board->conn_chgb != NULL)
1170 udc->board->conn_chgb(conn);
1173 /* Suspend/resume event - notify board function of change */
1174 static void uda_resm_susp_event(struct lpc32xx_udc *udc, u32 conn)
1176 /* Just notify of a Suspend/resume change event (optional) */
1177 if (udc->board->susp_chgb != NULL)
1178 udc->board->susp_chgb(conn);
1186 /* Remote wakeup enable/disable - notify board function of change */
1187 static void uda_remwkp_cgh(struct lpc32xx_udc *udc)
1189 if (udc->board->rmwk_chgb != NULL)
1190 udc->board->rmwk_chgb(udc->dev_status &
1191 (1 << USB_DEVICE_REMOTE_WAKEUP));
1194 /* Reads data from FIFO, adjusts for alignment and data size */
1195 static void udc_pop_fifo(struct lpc32xx_udc *udc, u8 *data, u32 bytes)
1199 u32 *p32, tmp, cbytes;
1201 /* Use optimal data transfer method based on source address and size */
1202 switch (((u32) data) & 0x3) {
1203 case 0: /* 32-bit aligned */
1205 cbytes = (bytes & ~0x3);
1207 /* Copy 32-bit aligned data first */
1208 for (n = 0; n < cbytes; n += 4)
1209 *p32++ = readl(USBD_RXDATA(udc->udp_baseaddr));
1211 /* Handle any remaining bytes */
1212 bl = bytes - cbytes;
1214 tmp = readl(USBD_RXDATA(udc->udp_baseaddr));
1215 for (n = 0; n < bl; n++)
1216 data[cbytes + n] = ((tmp >> (n * 8)) & 0xFF);
1221 case 1: /* 8-bit aligned */
1223 /* Each byte has to be handled independently */
1224 for (n = 0; n < bytes; n += 4) {
1225 tmp = readl(USBD_RXDATA(udc->udp_baseaddr));
1231 for (i = 0; i < bl; i++)
1232 data[n + i] = (u8) ((tmp >> (i * 8)) & 0xFF);
1236 case 2: /* 16-bit aligned */
1238 cbytes = (bytes & ~0x3);
1240 /* Copy 32-bit sized objects first with 16-bit alignment */
1241 for (n = 0; n < cbytes; n += 4) {
1242 tmp = readl(USBD_RXDATA(udc->udp_baseaddr));
1243 *p16++ = (u16)(tmp & 0xFFFF);
1244 *p16++ = (u16)((tmp >> 16) & 0xFFFF);
1247 /* Handle any remaining bytes */
1248 bl = bytes - cbytes;
1250 tmp = readl(USBD_RXDATA(udc->udp_baseaddr));
1251 for (n = 0; n < bl; n++)
1252 data[cbytes + n] = ((tmp >> (n * 8)) & 0xFF);
1258 /* Read data from the FIFO for an endpoint. This function is for endpoints (such
1259 * as EP0) that don't use DMA. This function should only be called if a packet
1260 * is known to be ready to read for the endpoint. Note that the endpoint must
1261 * be selected in the protocol engine prior to this call. */
1262 static u32 udc_read_hwep(struct lpc32xx_udc *udc, u32 hwep, u32 *data,
1267 u32 tmp, hwrep = ((hwep & 0x1E) << 1) | CTRL_RD_EN;
1269 /* Setup read of endpoint */
1270 writel(hwrep, USBD_CTRL(udc->udp_baseaddr));
1272 /* Wait until packet is ready */
1273 while ((((tmpv = readl(USBD_RXPLEN(udc->udp_baseaddr))) &
1274 PKT_RDY) == 0) && (to > 0))
1277 dev_dbg(udc->dev, "No packet ready on FIFO EP read\n");
1279 /* Mask out count */
1280 tmp = tmpv & PKT_LNGTH_MASK;
1284 if ((tmp > 0) && (data != NULL))
1285 udc_pop_fifo(udc, (u8 *) data, tmp);
1287 writel(((hwep & 0x1E) << 1), USBD_CTRL(udc->udp_baseaddr));
1289 /* Clear the buffer */
1290 udc_clr_buffer_hwep(udc, hwep);
1295 /* Stuffs data into the FIFO, adjusts for alignment and data size */
1296 static void udc_stuff_fifo(struct lpc32xx_udc *udc, u8 *data, u32 bytes)
1300 u32 *p32, tmp, cbytes;
1302 /* Use optimal data transfer method based on source address and size */
1303 switch (((u32) data) & 0x3) {
1304 case 0: /* 32-bit aligned */
1306 cbytes = (bytes & ~0x3);
1308 /* Copy 32-bit aligned data first */
1309 for (n = 0; n < cbytes; n += 4)
1310 writel(*p32++, USBD_TXDATA(udc->udp_baseaddr));
1312 /* Handle any remaining bytes */
1313 bl = bytes - cbytes;
1316 for (n = 0; n < bl; n++)
1317 tmp |= data[cbytes + n] << (n * 8);
1319 writel(tmp, USBD_TXDATA(udc->udp_baseaddr));
1323 case 1: /* 8-bit aligned */
1325 /* Each byte has to be handled independently */
1326 for (n = 0; n < bytes; n += 4) {
1332 for (i = 0; i < bl; i++)
1333 tmp |= data[n + i] << (i * 8);
1335 writel(tmp, USBD_TXDATA(udc->udp_baseaddr));
1339 case 2: /* 16-bit aligned */
1341 cbytes = (bytes & ~0x3);
1343 /* Copy 32-bit aligned data first */
1344 for (n = 0; n < cbytes; n += 4) {
1345 tmp = *p16++ & 0xFFFF;
1346 tmp |= (*p16++ & 0xFFFF) << 16;
1347 writel(tmp, USBD_TXDATA(udc->udp_baseaddr));
1350 /* Handle any remaining bytes */
1351 bl = bytes - cbytes;
1354 for (n = 0; n < bl; n++)
1355 tmp |= data[cbytes + n] << (n * 8);
1357 writel(tmp, USBD_TXDATA(udc->udp_baseaddr));
1363 /* Write data to the FIFO for an endpoint. This function is for endpoints (such
1364 * as EP0) that don't use DMA. Note that the endpoint must be selected in the
1365 * protocol engine prior to this call. */
1366 static void udc_write_hwep(struct lpc32xx_udc *udc, u32 hwep, u32 *data,
1369 u32 hwwep = ((hwep & 0x1E) << 1) | CTRL_WR_EN;
1371 if ((bytes > 0) && (data == NULL))
1374 /* Setup write of endpoint */
1375 writel(hwwep, USBD_CTRL(udc->udp_baseaddr));
1377 writel(bytes, USBD_TXPLEN(udc->udp_baseaddr));
1379 /* Need at least 1 byte to trigger TX */
1381 writel(0, USBD_TXDATA(udc->udp_baseaddr));
1383 udc_stuff_fifo(udc, (u8 *) data, bytes);
1385 writel(((hwep & 0x1E) << 1), USBD_CTRL(udc->udp_baseaddr));
1387 udc_val_buffer_hwep(udc, hwep);
1390 /* USB device reset - resets USB to a default state with just EP0
1392 static void uda_usb_reset(struct lpc32xx_udc *udc)
1395 /* Re-init device controller and EP0 */
1397 udc->gadget.speed = USB_SPEED_FULL;
1399 for (i = 1; i < NUM_ENDPOINTS; i++) {
1400 struct lpc32xx_ep *ep = &udc->ep[i];
1401 ep->req_pending = 0;
1405 /* Send a ZLP on EP0 */
1406 static void udc_ep0_send_zlp(struct lpc32xx_udc *udc)
1408 udc_write_hwep(udc, EP_IN, NULL, 0);
1411 /* Get current frame number */
1412 static u16 udc_get_current_frame(struct lpc32xx_udc *udc)
1416 udc_protocol_cmd_w(udc, CMD_RD_FRAME);
1417 flo = (u16) udc_protocol_cmd_r(udc, DAT_RD_FRAME);
1418 fhi = (u16) udc_protocol_cmd_r(udc, DAT_RD_FRAME);
1420 return (fhi << 8) | flo;
1423 /* Set the device as configured - enables all endpoints */
1424 static inline void udc_set_device_configured(struct lpc32xx_udc *udc)
1426 udc_protocol_cmd_data_w(udc, CMD_CFG_DEV, DAT_WR_BYTE(CONF_DVICE));
1429 /* Set the device as unconfigured - disables all endpoints */
1430 static inline void udc_set_device_unconfigured(struct lpc32xx_udc *udc)
1432 udc_protocol_cmd_data_w(udc, CMD_CFG_DEV, DAT_WR_BYTE(0));
1435 /* reinit == restore initial software state */
1436 static void udc_reinit(struct lpc32xx_udc *udc)
1440 INIT_LIST_HEAD(&udc->gadget.ep_list);
1441 INIT_LIST_HEAD(&udc->gadget.ep0->ep_list);
1443 for (i = 0; i < NUM_ENDPOINTS; i++) {
1444 struct lpc32xx_ep *ep = &udc->ep[i];
1447 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
1448 usb_ep_set_maxpacket_limit(&ep->ep, ep->maxpacket);
1449 INIT_LIST_HEAD(&ep->queue);
1450 ep->req_pending = 0;
1453 udc->ep0state = WAIT_FOR_SETUP;
1456 /* Must be called with lock */
1457 static void done(struct lpc32xx_ep *ep, struct lpc32xx_request *req, int status)
1459 struct lpc32xx_udc *udc = ep->udc;
1461 list_del_init(&req->queue);
1462 if (req->req.status == -EINPROGRESS)
1463 req->req.status = status;
1465 status = req->req.status;
1468 usb_gadget_unmap_request(&udc->gadget, &req->req, ep->is_in);
1471 udc_dd_free(udc, req->dd_desc_ptr);
1474 if (status && status != -ESHUTDOWN)
1475 ep_dbg(ep, "%s done %p, status %d\n", ep->ep.name, req, status);
1477 ep->req_pending = 0;
1478 spin_unlock(&udc->lock);
1479 usb_gadget_giveback_request(&ep->ep, &req->req);
1480 spin_lock(&udc->lock);
1483 /* Must be called with lock */
1484 static void nuke(struct lpc32xx_ep *ep, int status)
1486 struct lpc32xx_request *req;
1488 while (!list_empty(&ep->queue)) {
1489 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
1490 done(ep, req, status);
1493 if (status == -ESHUTDOWN) {
1494 uda_disable_hwepint(ep->udc, ep->hwep_num);
1495 udc_disable_hwep(ep->udc, ep->hwep_num);
1499 /* IN endpoint 0 transfer */
1500 static int udc_ep0_in_req(struct lpc32xx_udc *udc)
1502 struct lpc32xx_request *req;
1503 struct lpc32xx_ep *ep0 = &udc->ep[0];
1506 if (list_empty(&ep0->queue))
1507 /* Nothing to send */
1510 req = list_entry(ep0->queue.next, struct lpc32xx_request,
1513 tsend = ts = req->req.length - req->req.actual;
1516 udc_ep0_send_zlp(udc);
1519 } else if (ts > ep0->ep.maxpacket)
1520 ts = ep0->ep.maxpacket; /* Just send what we can */
1522 /* Write data to the EP0 FIFO and start transfer */
1523 udc_write_hwep(udc, EP_IN, (req->req.buf + req->req.actual), ts);
1525 /* Increment data pointer */
1526 req->req.actual += ts;
1528 if (tsend >= ep0->ep.maxpacket)
1529 return 0; /* Stay in data transfer state */
1531 /* Transfer request is complete */
1532 udc->ep0state = WAIT_FOR_SETUP;
1537 /* OUT endpoint 0 transfer */
1538 static int udc_ep0_out_req(struct lpc32xx_udc *udc)
1540 struct lpc32xx_request *req;
1541 struct lpc32xx_ep *ep0 = &udc->ep[0];
1542 u32 tr, bufferspace;
1544 if (list_empty(&ep0->queue))
1547 req = list_entry(ep0->queue.next, struct lpc32xx_request,
1551 if (req->req.length == 0) {
1552 /* Just dequeue request */
1554 udc->ep0state = WAIT_FOR_SETUP;
1558 /* Get data from FIFO */
1559 bufferspace = req->req.length - req->req.actual;
1560 if (bufferspace > ep0->ep.maxpacket)
1561 bufferspace = ep0->ep.maxpacket;
1563 /* Copy data to buffer */
1564 prefetchw(req->req.buf + req->req.actual);
1565 tr = udc_read_hwep(udc, EP_OUT, req->req.buf + req->req.actual,
1567 req->req.actual += bufferspace;
1569 if (tr < ep0->ep.maxpacket) {
1570 /* This is the last packet */
1572 udc->ep0state = WAIT_FOR_SETUP;
1580 /* Must be called with lock */
1581 static void stop_activity(struct lpc32xx_udc *udc)
1583 struct usb_gadget_driver *driver = udc->driver;
1586 if (udc->gadget.speed == USB_SPEED_UNKNOWN)
1589 udc->gadget.speed = USB_SPEED_UNKNOWN;
1592 for (i = 0; i < NUM_ENDPOINTS; i++) {
1593 struct lpc32xx_ep *ep = &udc->ep[i];
1594 nuke(ep, -ESHUTDOWN);
1597 spin_unlock(&udc->lock);
1598 driver->disconnect(&udc->gadget);
1599 spin_lock(&udc->lock);
1602 isp1301_pullup_enable(udc, 0, 0);
1608 * Activate or kill host pullup
1609 * Can be called with or without lock
1611 static void pullup(struct lpc32xx_udc *udc, int is_on)
1616 if (!udc->enabled || !udc->vbus)
1619 if (is_on != udc->pullup)
1620 isp1301_pullup_enable(udc, is_on, 0);
1623 /* Must be called without lock */
1624 static int lpc32xx_ep_disable(struct usb_ep *_ep)
1626 struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
1627 struct lpc32xx_udc *udc = ep->udc;
1628 unsigned long flags;
1630 if ((ep->hwep_num_base == 0) || (ep->hwep_num == 0))
1632 spin_lock_irqsave(&udc->lock, flags);
1634 nuke(ep, -ESHUTDOWN);
1636 /* Clear all DMA statuses for this EP */
1637 udc_ep_dma_disable(udc, ep->hwep_num);
1638 writel(1 << ep->hwep_num, USBD_EOTINTCLR(udc->udp_baseaddr));
1639 writel(1 << ep->hwep_num, USBD_NDDRTINTCLR(udc->udp_baseaddr));
1640 writel(1 << ep->hwep_num, USBD_SYSERRTINTCLR(udc->udp_baseaddr));
1641 writel(1 << ep->hwep_num, USBD_DMARCLR(udc->udp_baseaddr));
1643 /* Remove the DD pointer in the UDCA */
1644 udc->udca_v_base[ep->hwep_num] = 0;
1646 /* Disable and reset endpoint and interrupt */
1647 uda_clear_hwepint(udc, ep->hwep_num);
1648 udc_unrealize_hwep(udc, ep->hwep_num);
1652 spin_unlock_irqrestore(&udc->lock, flags);
1654 atomic_dec(&udc->enabled_ep_cnt);
1655 wake_up(&udc->ep_disable_wait_queue);
1660 /* Must be called without lock */
1661 static int lpc32xx_ep_enable(struct usb_ep *_ep,
1662 const struct usb_endpoint_descriptor *desc)
1664 struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
1665 struct lpc32xx_udc *udc;
1668 unsigned long flags;
1670 /* Verify EP data */
1671 if ((!_ep) || (!ep) || (!desc) ||
1672 (desc->bDescriptorType != USB_DT_ENDPOINT))
1676 maxpacket = usb_endpoint_maxp(desc);
1677 if ((maxpacket == 0) || (maxpacket > ep->maxpacket)) {
1678 dev_dbg(udc->dev, "bad ep descriptor's packet size\n");
1682 /* Don't touch EP0 */
1683 if (ep->hwep_num_base == 0) {
1684 dev_dbg(udc->dev, "Can't re-enable EP0!!!\n");
1688 /* Is driver ready? */
1689 if ((!udc->driver) || (udc->gadget.speed == USB_SPEED_UNKNOWN)) {
1690 dev_dbg(udc->dev, "bogus device state\n");
1694 tmp = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
1696 case USB_ENDPOINT_XFER_CONTROL:
1699 case USB_ENDPOINT_XFER_INT:
1700 if (maxpacket > ep->maxpacket) {
1702 "Bad INT endpoint maxpacket %d\n", maxpacket);
1707 case USB_ENDPOINT_XFER_BULK:
1708 switch (maxpacket) {
1717 "Bad BULK endpoint maxpacket %d\n", maxpacket);
1722 case USB_ENDPOINT_XFER_ISOC:
1725 spin_lock_irqsave(&udc->lock, flags);
1727 /* Initialize endpoint to match the selected descriptor */
1728 ep->is_in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
1729 ep->ep.maxpacket = maxpacket;
1731 /* Map hardware endpoint from base and direction */
1733 /* IN endpoints are offset 1 from the OUT endpoint */
1734 ep->hwep_num = ep->hwep_num_base + EP_IN;
1736 ep->hwep_num = ep->hwep_num_base;
1738 ep_dbg(ep, "EP enabled: %s, HW:%d, MP:%d IN:%d\n", ep->ep.name,
1739 ep->hwep_num, maxpacket, (ep->is_in == 1));
1741 /* Realize the endpoint, interrupt is enabled later when
1742 * buffers are queued, IN EPs will NAK until buffers are ready */
1743 udc_realize_hwep(udc, ep->hwep_num, ep->ep.maxpacket);
1744 udc_clr_buffer_hwep(udc, ep->hwep_num);
1745 uda_disable_hwepint(udc, ep->hwep_num);
1746 udc_clrstall_hwep(udc, ep->hwep_num);
1748 /* Clear all DMA statuses for this EP */
1749 udc_ep_dma_disable(udc, ep->hwep_num);
1750 writel(1 << ep->hwep_num, USBD_EOTINTCLR(udc->udp_baseaddr));
1751 writel(1 << ep->hwep_num, USBD_NDDRTINTCLR(udc->udp_baseaddr));
1752 writel(1 << ep->hwep_num, USBD_SYSERRTINTCLR(udc->udp_baseaddr));
1753 writel(1 << ep->hwep_num, USBD_DMARCLR(udc->udp_baseaddr));
1755 spin_unlock_irqrestore(&udc->lock, flags);
1757 atomic_inc(&udc->enabled_ep_cnt);
1762 * Allocate a USB request list
1763 * Can be called with or without lock
1765 static struct usb_request *lpc32xx_ep_alloc_request(struct usb_ep *_ep,
1768 struct lpc32xx_request *req;
1770 req = kzalloc(sizeof(struct lpc32xx_request), gfp_flags);
1774 INIT_LIST_HEAD(&req->queue);
1779 * De-allocate a USB request list
1780 * Can be called with or without lock
1782 static void lpc32xx_ep_free_request(struct usb_ep *_ep,
1783 struct usb_request *_req)
1785 struct lpc32xx_request *req;
1787 req = container_of(_req, struct lpc32xx_request, req);
1788 BUG_ON(!list_empty(&req->queue));
1792 /* Must be called without lock */
1793 static int lpc32xx_ep_queue(struct usb_ep *_ep,
1794 struct usb_request *_req, gfp_t gfp_flags)
1796 struct lpc32xx_request *req;
1797 struct lpc32xx_ep *ep;
1798 struct lpc32xx_udc *udc;
1799 unsigned long flags;
1802 req = container_of(_req, struct lpc32xx_request, req);
1803 ep = container_of(_ep, struct lpc32xx_ep, ep);
1805 if (!_ep || !_req || !_req->complete || !_req->buf ||
1806 !list_empty(&req->queue))
1811 if (udc->gadget.speed == USB_SPEED_UNKNOWN)
1815 struct lpc32xx_usbd_dd_gad *dd;
1817 status = usb_gadget_map_request(&udc->gadget, _req, ep->is_in);
1821 /* For the request, build a list of DDs */
1822 dd = udc_dd_alloc(udc);
1824 /* Error allocating DD */
1827 req->dd_desc_ptr = dd;
1829 /* Setup the DMA descriptor */
1830 dd->dd_next_phy = dd->dd_next_v = 0;
1831 dd->dd_buffer_addr = req->req.dma;
1834 /* Special handling for ISO EPs */
1835 if (ep->eptype == EP_ISO_TYPE) {
1836 dd->dd_setup = DD_SETUP_ISO_EP |
1837 DD_SETUP_PACKETLEN(0) |
1838 DD_SETUP_DMALENBYTES(1);
1839 dd->dd_iso_ps_mem_addr = dd->this_dma + 24;
1841 dd->iso_status[0] = req->req.length;
1843 dd->iso_status[0] = 0;
1845 dd->dd_setup = DD_SETUP_PACKETLEN(ep->ep.maxpacket) |
1846 DD_SETUP_DMALENBYTES(req->req.length);
1849 ep_dbg(ep, "%s queue req %p len %d buf %p (in=%d) z=%d\n", _ep->name,
1850 _req, _req->length, _req->buf, ep->is_in, _req->zero);
1852 spin_lock_irqsave(&udc->lock, flags);
1854 _req->status = -EINPROGRESS;
1856 req->send_zlp = _req->zero;
1858 /* Kickstart empty queues */
1859 if (list_empty(&ep->queue)) {
1860 list_add_tail(&req->queue, &ep->queue);
1862 if (ep->hwep_num_base == 0) {
1863 /* Handle expected data direction */
1865 /* IN packet to host */
1866 udc->ep0state = DATA_IN;
1867 status = udc_ep0_in_req(udc);
1869 /* OUT packet from host */
1870 udc->ep0state = DATA_OUT;
1871 status = udc_ep0_out_req(udc);
1873 } else if (ep->is_in) {
1874 /* IN packet to host and kick off transfer */
1875 if (!ep->req_pending)
1876 udc_ep_in_req_dma(udc, ep);
1878 /* OUT packet from host and kick off list */
1879 if (!ep->req_pending)
1880 udc_ep_out_req_dma(udc, ep);
1882 list_add_tail(&req->queue, &ep->queue);
1884 spin_unlock_irqrestore(&udc->lock, flags);
1886 return (status < 0) ? status : 0;
1889 /* Must be called without lock */
1890 static int lpc32xx_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1892 struct lpc32xx_ep *ep;
1893 struct lpc32xx_request *req;
1894 unsigned long flags;
1896 ep = container_of(_ep, struct lpc32xx_ep, ep);
1897 if (!_ep || ep->hwep_num_base == 0)
1900 spin_lock_irqsave(&ep->udc->lock, flags);
1902 /* make sure it's actually queued on this endpoint */
1903 list_for_each_entry(req, &ep->queue, queue) {
1904 if (&req->req == _req)
1907 if (&req->req != _req) {
1908 spin_unlock_irqrestore(&ep->udc->lock, flags);
1912 done(ep, req, -ECONNRESET);
1914 spin_unlock_irqrestore(&ep->udc->lock, flags);
1919 /* Must be called without lock */
1920 static int lpc32xx_ep_set_halt(struct usb_ep *_ep, int value)
1922 struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
1923 struct lpc32xx_udc *udc;
1924 unsigned long flags;
1926 if ((!ep) || (ep->hwep_num <= 1))
1929 /* Don't halt an IN EP */
1934 spin_lock_irqsave(&udc->lock, flags);
1938 udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(ep->hwep_num),
1939 DAT_WR_BYTE(EP_STAT_ST));
1943 udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(ep->hwep_num),
1947 spin_unlock_irqrestore(&udc->lock, flags);
1952 /* set the halt feature and ignores clear requests */
1953 static int lpc32xx_ep_set_wedge(struct usb_ep *_ep)
1955 struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
1957 if (!_ep || !ep->udc)
1962 return usb_ep_set_halt(_ep);
1965 static const struct usb_ep_ops lpc32xx_ep_ops = {
1966 .enable = lpc32xx_ep_enable,
1967 .disable = lpc32xx_ep_disable,
1968 .alloc_request = lpc32xx_ep_alloc_request,
1969 .free_request = lpc32xx_ep_free_request,
1970 .queue = lpc32xx_ep_queue,
1971 .dequeue = lpc32xx_ep_dequeue,
1972 .set_halt = lpc32xx_ep_set_halt,
1973 .set_wedge = lpc32xx_ep_set_wedge,
1976 /* Send a ZLP on a non-0 IN EP */
1977 void udc_send_in_zlp(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
1979 /* Clear EP status */
1980 udc_clearep_getsts(udc, ep->hwep_num);
1982 /* Send ZLP via FIFO mechanism */
1983 udc_write_hwep(udc, ep->hwep_num, NULL, 0);
1987 * Handle EP completion for ZLP
1988 * This function will only be called when a delayed ZLP needs to be sent out
1989 * after a DMA transfer has filled both buffers.
1991 void udc_handle_eps(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
1994 struct lpc32xx_request *req;
1996 if (ep->hwep_num <= 0)
1999 uda_clear_hwepint(udc, ep->hwep_num);
2001 /* If this interrupt isn't enabled, return now */
2002 if (!(udc->enabled_hwepints & (1 << ep->hwep_num)))
2005 /* Get endpoint status */
2006 epstatus = udc_clearep_getsts(udc, ep->hwep_num);
2009 * This should never happen, but protect against writing to the
2012 if (epstatus & EP_SEL_F)
2016 udc_send_in_zlp(udc, ep);
2017 uda_disable_hwepint(udc, ep->hwep_num);
2021 /* If there isn't a request waiting, something went wrong */
2022 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
2026 /* Start another request if ready */
2027 if (!list_empty(&ep->queue)) {
2029 udc_ep_in_req_dma(udc, ep);
2031 udc_ep_out_req_dma(udc, ep);
2033 ep->req_pending = 0;
2038 /* DMA end of transfer completion */
2039 static void udc_handle_dma_ep(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
2041 u32 status, epstatus;
2042 struct lpc32xx_request *req;
2043 struct lpc32xx_usbd_dd_gad *dd;
2045 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2049 req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
2051 ep_err(ep, "DMA interrupt on no req!\n");
2054 dd = req->dd_desc_ptr;
2056 /* DMA descriptor should always be retired for this call */
2057 if (!(dd->dd_status & DD_STATUS_DD_RETIRED))
2058 ep_warn(ep, "DMA descriptor did not retire\n");
2061 udc_ep_dma_disable(udc, ep->hwep_num);
2062 writel((1 << ep->hwep_num), USBD_EOTINTCLR(udc->udp_baseaddr));
2063 writel((1 << ep->hwep_num), USBD_NDDRTINTCLR(udc->udp_baseaddr));
2066 if (readl(USBD_SYSERRTINTST(udc->udp_baseaddr)) &
2067 (1 << ep->hwep_num)) {
2068 writel((1 << ep->hwep_num),
2069 USBD_SYSERRTINTCLR(udc->udp_baseaddr));
2070 ep_err(ep, "AHB critical error!\n");
2071 ep->req_pending = 0;
2073 /* The error could have occurred on a packet of a multipacket
2074 * transfer, so recovering the transfer is not possible. Close
2075 * the request with an error */
2076 done(ep, req, -ECONNABORTED);
2080 /* Handle the current DD's status */
2081 status = dd->dd_status;
2082 switch (status & DD_STATUS_STS_MASK) {
2083 case DD_STATUS_STS_NS:
2084 /* DD not serviced? This shouldn't happen! */
2085 ep->req_pending = 0;
2086 ep_err(ep, "DMA critical EP error: DD not serviced (0x%x)!\n",
2089 done(ep, req, -ECONNABORTED);
2092 case DD_STATUS_STS_BS:
2093 /* Interrupt only fires on EOT - This shouldn't happen! */
2094 ep->req_pending = 0;
2095 ep_err(ep, "DMA critical EP error: EOT prior to service completion (0x%x)!\n",
2097 done(ep, req, -ECONNABORTED);
2100 case DD_STATUS_STS_NC:
2101 case DD_STATUS_STS_DUR:
2102 /* Really just a short packet, not an underrun */
2103 /* This is a good status and what we expect */
2107 /* Data overrun, system error, or unknown */
2108 ep->req_pending = 0;
2109 ep_err(ep, "DMA critical EP error: System error (0x%x)!\n",
2111 done(ep, req, -ECONNABORTED);
2115 /* ISO endpoints are handled differently */
2116 if (ep->eptype == EP_ISO_TYPE) {
2118 req->req.actual = req->req.length;
2120 req->req.actual = dd->iso_status[0] & 0xFFFF;
2122 req->req.actual += DD_STATUS_CURDMACNT(status);
2124 /* Send a ZLP if necessary. This will be done for non-int
2125 * packets which have a size that is a divisor of MAXP */
2126 if (req->send_zlp) {
2128 * If at least 1 buffer is available, send the ZLP now.
2129 * Otherwise, the ZLP send needs to be deferred until a
2130 * buffer is available.
2132 if (udc_clearep_getsts(udc, ep->hwep_num) & EP_SEL_F) {
2133 udc_clearep_getsts(udc, ep->hwep_num);
2134 uda_enable_hwepint(udc, ep->hwep_num);
2135 epstatus = udc_clearep_getsts(udc, ep->hwep_num);
2137 /* Let the EP interrupt handle the ZLP */
2140 udc_send_in_zlp(udc, ep);
2143 /* Transfer request is complete */
2146 /* Start another request if ready */
2147 udc_clearep_getsts(udc, ep->hwep_num);
2148 if (!list_empty((&ep->queue))) {
2150 udc_ep_in_req_dma(udc, ep);
2152 udc_ep_out_req_dma(udc, ep);
2154 ep->req_pending = 0;
2160 * Endpoint 0 functions
2163 static void udc_handle_dev(struct lpc32xx_udc *udc)
2167 udc_protocol_cmd_w(udc, CMD_GET_DEV_STAT);
2168 tmp = udc_protocol_cmd_r(udc, DAT_GET_DEV_STAT);
2172 else if (tmp & DEV_CON_CH)
2173 uda_power_event(udc, (tmp & DEV_CON));
2174 else if (tmp & DEV_SUS_CH) {
2175 if (tmp & DEV_SUS) {
2178 else if ((udc->gadget.speed != USB_SPEED_UNKNOWN) &&
2180 /* Power down transceiver */
2182 schedule_work(&udc->pullup_job);
2183 uda_resm_susp_event(udc, 1);
2185 } else if ((udc->gadget.speed != USB_SPEED_UNKNOWN) &&
2186 udc->driver && udc->vbus) {
2187 uda_resm_susp_event(udc, 0);
2188 /* Power up transceiver */
2190 schedule_work(&udc->pullup_job);
2195 static int udc_get_status(struct lpc32xx_udc *udc, u16 reqtype, u16 wIndex)
2197 struct lpc32xx_ep *ep;
2198 u32 ep0buff = 0, tmp;
2200 switch (reqtype & USB_RECIP_MASK) {
2201 case USB_RECIP_INTERFACE:
2202 break; /* Not supported */
2204 case USB_RECIP_DEVICE:
2205 ep0buff = udc->gadget.is_selfpowered;
2206 if (udc->dev_status & (1 << USB_DEVICE_REMOTE_WAKEUP))
2207 ep0buff |= (1 << USB_DEVICE_REMOTE_WAKEUP);
2210 case USB_RECIP_ENDPOINT:
2211 tmp = wIndex & USB_ENDPOINT_NUMBER_MASK;
2213 if ((tmp == 0) || (tmp >= NUM_ENDPOINTS))
2216 if (wIndex & USB_DIR_IN) {
2218 return -EOPNOTSUPP; /* Something's wrong */
2219 } else if (ep->is_in)
2220 return -EOPNOTSUPP; /* Not an IN endpoint */
2222 /* Get status of the endpoint */
2223 udc_protocol_cmd_w(udc, CMD_SEL_EP(ep->hwep_num));
2224 tmp = udc_protocol_cmd_r(udc, DAT_SEL_EP(ep->hwep_num));
2226 if (tmp & EP_SEL_ST)
2227 ep0buff = (1 << USB_ENDPOINT_HALT);
2237 udc_write_hwep(udc, EP_IN, &ep0buff, 2);
2242 static void udc_handle_ep0_setup(struct lpc32xx_udc *udc)
2244 struct lpc32xx_ep *ep, *ep0 = &udc->ep[0];
2245 struct usb_ctrlrequest ctrlpkt;
2247 u16 wIndex, wValue, wLength, reqtype, req, tmp;
2249 /* Nuke previous transfers */
2252 /* Get setup packet */
2253 bytes = udc_read_hwep(udc, EP_OUT, (u32 *) &ctrlpkt, 8);
2255 ep_warn(ep0, "Incorrectly sized setup packet (s/b 8, is %d)!\n",
2260 /* Native endianness */
2261 wIndex = le16_to_cpu(ctrlpkt.wIndex);
2262 wValue = le16_to_cpu(ctrlpkt.wValue);
2263 wLength = le16_to_cpu(ctrlpkt.wLength);
2264 reqtype = le16_to_cpu(ctrlpkt.bRequestType);
2266 /* Set direction of EP0 */
2267 if (likely(reqtype & USB_DIR_IN))
2272 /* Handle SETUP packet */
2273 req = le16_to_cpu(ctrlpkt.bRequest);
2275 case USB_REQ_CLEAR_FEATURE:
2276 case USB_REQ_SET_FEATURE:
2278 case (USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2279 if (wValue != USB_DEVICE_REMOTE_WAKEUP)
2280 goto stall; /* Nothing else handled */
2282 /* Tell board about event */
2283 if (req == USB_REQ_CLEAR_FEATURE)
2285 ~(1 << USB_DEVICE_REMOTE_WAKEUP);
2288 (1 << USB_DEVICE_REMOTE_WAKEUP);
2289 uda_remwkp_cgh(udc);
2292 case (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2293 tmp = wIndex & USB_ENDPOINT_NUMBER_MASK;
2294 if ((wValue != USB_ENDPOINT_HALT) ||
2295 (tmp >= NUM_ENDPOINTS))
2298 /* Find hardware endpoint from logical endpoint */
2304 if (req == USB_REQ_SET_FEATURE)
2305 udc_stall_hwep(udc, tmp);
2306 else if (!ep->wedge)
2307 udc_clrstall_hwep(udc, tmp);
2316 case USB_REQ_SET_ADDRESS:
2317 if (reqtype == (USB_TYPE_STANDARD | USB_RECIP_DEVICE)) {
2318 udc_set_address(udc, wValue);
2323 case USB_REQ_GET_STATUS:
2324 udc_get_status(udc, reqtype, wIndex);
2328 break; /* Let GadgetFS handle the descriptor instead */
2331 if (likely(udc->driver)) {
2332 /* device-2-host (IN) or no data setup command, process
2334 spin_unlock(&udc->lock);
2335 i = udc->driver->setup(&udc->gadget, &ctrlpkt);
2337 spin_lock(&udc->lock);
2338 if (req == USB_REQ_SET_CONFIGURATION) {
2339 /* Configuration is set after endpoints are realized */
2341 /* Set configuration */
2342 udc_set_device_configured(udc);
2344 udc_protocol_cmd_data_w(udc, CMD_SET_MODE,
2345 DAT_WR_BYTE(AP_CLK |
2346 INAK_BI | INAK_II));
2348 /* Clear configuration */
2349 udc_set_device_unconfigured(udc);
2351 /* Disable NAK interrupts */
2352 udc_protocol_cmd_data_w(udc, CMD_SET_MODE,
2353 DAT_WR_BYTE(AP_CLK));
2358 /* setup processing failed, force stall */
2360 "req %02x.%02x protocol STALL; stat %d\n",
2362 udc->ep0state = WAIT_FOR_SETUP;
2368 udc_ep0_send_zlp(udc); /* ZLP IN packet on data phase */
2373 udc_stall_hwep(udc, EP_IN);
2377 udc_ep0_send_zlp(udc);
2381 /* IN endpoint 0 transfer */
2382 static void udc_handle_ep0_in(struct lpc32xx_udc *udc)
2384 struct lpc32xx_ep *ep0 = &udc->ep[0];
2387 /* Clear EP interrupt */
2388 epstatus = udc_clearep_getsts(udc, EP_IN);
2390 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2394 /* Stalled? Clear stall and reset buffers */
2395 if (epstatus & EP_SEL_ST) {
2396 udc_clrstall_hwep(udc, EP_IN);
2397 nuke(ep0, -ECONNABORTED);
2398 udc->ep0state = WAIT_FOR_SETUP;
2402 /* Is a buffer available? */
2403 if (!(epstatus & EP_SEL_F)) {
2404 /* Handle based on current state */
2405 if (udc->ep0state == DATA_IN)
2406 udc_ep0_in_req(udc);
2408 /* Unknown state for EP0 oe end of DATA IN phase */
2409 nuke(ep0, -ECONNABORTED);
2410 udc->ep0state = WAIT_FOR_SETUP;
2415 /* OUT endpoint 0 transfer */
2416 static void udc_handle_ep0_out(struct lpc32xx_udc *udc)
2418 struct lpc32xx_ep *ep0 = &udc->ep[0];
2421 /* Clear EP interrupt */
2422 epstatus = udc_clearep_getsts(udc, EP_OUT);
2425 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2430 if (epstatus & EP_SEL_ST) {
2431 udc_clrstall_hwep(udc, EP_OUT);
2432 nuke(ep0, -ECONNABORTED);
2433 udc->ep0state = WAIT_FOR_SETUP;
2437 /* A NAK may occur if a packet couldn't be received yet */
2438 if (epstatus & EP_SEL_EPN)
2440 /* Setup packet incoming? */
2441 if (epstatus & EP_SEL_STP) {
2443 udc->ep0state = WAIT_FOR_SETUP;
2446 /* Data available? */
2447 if (epstatus & EP_SEL_F)
2448 /* Handle based on current state */
2449 switch (udc->ep0state) {
2450 case WAIT_FOR_SETUP:
2451 udc_handle_ep0_setup(udc);
2455 udc_ep0_out_req(udc);
2459 /* Unknown state for EP0 */
2460 nuke(ep0, -ECONNABORTED);
2461 udc->ep0state = WAIT_FOR_SETUP;
2465 /* Must be called without lock */
2466 static int lpc32xx_get_frame(struct usb_gadget *gadget)
2469 unsigned long flags;
2470 struct lpc32xx_udc *udc = to_udc(gadget);
2475 spin_lock_irqsave(&udc->lock, flags);
2477 frame = (int) udc_get_current_frame(udc);
2479 spin_unlock_irqrestore(&udc->lock, flags);
2484 static int lpc32xx_wakeup(struct usb_gadget *gadget)
2489 static int lpc32xx_set_selfpowered(struct usb_gadget *gadget, int is_on)
2491 gadget->is_selfpowered = (is_on != 0);
2497 * vbus is here! turn everything on that's ready
2498 * Must be called without lock
2500 static int lpc32xx_vbus_session(struct usb_gadget *gadget, int is_active)
2502 unsigned long flags;
2503 struct lpc32xx_udc *udc = to_udc(gadget);
2505 spin_lock_irqsave(&udc->lock, flags);
2507 /* Doesn't need lock */
2509 udc_clk_set(udc, 1);
2511 pullup(udc, is_active);
2516 spin_unlock_irqrestore(&udc->lock, flags);
2518 * Wait for all the endpoints to disable,
2519 * before disabling clocks. Don't wait if
2520 * endpoints are not enabled.
2522 if (atomic_read(&udc->enabled_ep_cnt))
2523 wait_event_interruptible(udc->ep_disable_wait_queue,
2524 (atomic_read(&udc->enabled_ep_cnt) == 0));
2526 spin_lock_irqsave(&udc->lock, flags);
2528 udc_clk_set(udc, 0);
2531 spin_unlock_irqrestore(&udc->lock, flags);
2536 /* Can be called with or without lock */
2537 static int lpc32xx_pullup(struct usb_gadget *gadget, int is_on)
2539 struct lpc32xx_udc *udc = to_udc(gadget);
2541 /* Doesn't need lock */
2547 static int lpc32xx_start(struct usb_gadget *, struct usb_gadget_driver *);
2548 static int lpc32xx_stop(struct usb_gadget *);
2550 static const struct usb_gadget_ops lpc32xx_udc_ops = {
2551 .get_frame = lpc32xx_get_frame,
2552 .wakeup = lpc32xx_wakeup,
2553 .set_selfpowered = lpc32xx_set_selfpowered,
2554 .vbus_session = lpc32xx_vbus_session,
2555 .pullup = lpc32xx_pullup,
2556 .udc_start = lpc32xx_start,
2557 .udc_stop = lpc32xx_stop,
2560 static void nop_release(struct device *dev)
2562 /* nothing to free */
2565 static const struct lpc32xx_udc controller_template = {
2567 .ops = &lpc32xx_udc_ops,
2568 .name = driver_name,
2570 .init_name = "gadget",
2571 .release = nop_release,
2577 .ops = &lpc32xx_ep_ops,
2578 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL,
2579 USB_EP_CAPS_DIR_ALL),
2583 .hwep_num = 0, /* Can be 0 or 1, has special handling */
2585 .eptype = EP_CTL_TYPE,
2590 .ops = &lpc32xx_ep_ops,
2591 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_INT,
2592 USB_EP_CAPS_DIR_ALL),
2596 .hwep_num = 0, /* 2 or 3, will be set later */
2598 .eptype = EP_INT_TYPE,
2603 .ops = &lpc32xx_ep_ops,
2604 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
2605 USB_EP_CAPS_DIR_ALL),
2609 .hwep_num = 0, /* 4 or 5, will be set later */
2611 .eptype = EP_BLK_TYPE,
2616 .ops = &lpc32xx_ep_ops,
2617 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
2618 USB_EP_CAPS_DIR_ALL),
2622 .hwep_num = 0, /* 6 or 7, will be set later */
2624 .eptype = EP_ISO_TYPE,
2629 .ops = &lpc32xx_ep_ops,
2630 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_INT,
2631 USB_EP_CAPS_DIR_ALL),
2635 .hwep_num = 0, /* 8 or 9, will be set later */
2637 .eptype = EP_INT_TYPE,
2642 .ops = &lpc32xx_ep_ops,
2643 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
2644 USB_EP_CAPS_DIR_ALL),
2647 .hwep_num_base = 10,
2648 .hwep_num = 0, /* 10 or 11, will be set later */
2650 .eptype = EP_BLK_TYPE,
2655 .ops = &lpc32xx_ep_ops,
2656 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
2657 USB_EP_CAPS_DIR_ALL),
2660 .hwep_num_base = 12,
2661 .hwep_num = 0, /* 12 or 13, will be set later */
2663 .eptype = EP_ISO_TYPE,
2668 .ops = &lpc32xx_ep_ops,
2669 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_INT,
2670 USB_EP_CAPS_DIR_ALL),
2673 .hwep_num_base = 14,
2676 .eptype = EP_INT_TYPE,
2681 .ops = &lpc32xx_ep_ops,
2682 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
2683 USB_EP_CAPS_DIR_ALL),
2686 .hwep_num_base = 16,
2689 .eptype = EP_BLK_TYPE,
2694 .ops = &lpc32xx_ep_ops,
2695 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
2696 USB_EP_CAPS_DIR_ALL),
2699 .hwep_num_base = 18,
2702 .eptype = EP_ISO_TYPE,
2707 .ops = &lpc32xx_ep_ops,
2708 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_INT,
2709 USB_EP_CAPS_DIR_ALL),
2712 .hwep_num_base = 20,
2715 .eptype = EP_INT_TYPE,
2719 .name = "ep11-bulk",
2720 .ops = &lpc32xx_ep_ops,
2721 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
2722 USB_EP_CAPS_DIR_ALL),
2725 .hwep_num_base = 22,
2728 .eptype = EP_BLK_TYPE,
2733 .ops = &lpc32xx_ep_ops,
2734 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
2735 USB_EP_CAPS_DIR_ALL),
2738 .hwep_num_base = 24,
2741 .eptype = EP_ISO_TYPE,
2746 .ops = &lpc32xx_ep_ops,
2747 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_INT,
2748 USB_EP_CAPS_DIR_ALL),
2751 .hwep_num_base = 26,
2754 .eptype = EP_INT_TYPE,
2758 .name = "ep14-bulk",
2759 .ops = &lpc32xx_ep_ops,
2760 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
2761 USB_EP_CAPS_DIR_ALL),
2764 .hwep_num_base = 28,
2767 .eptype = EP_BLK_TYPE,
2771 .name = "ep15-bulk",
2772 .ops = &lpc32xx_ep_ops,
2773 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
2774 USB_EP_CAPS_DIR_ALL),
2777 .hwep_num_base = 30,
2780 .eptype = EP_BLK_TYPE,
2784 /* ISO and status interrupts */
2785 static irqreturn_t lpc32xx_usb_lp_irq(int irq, void *_udc)
2788 struct lpc32xx_udc *udc = _udc;
2790 spin_lock(&udc->lock);
2792 /* Read the device status register */
2793 devstat = readl(USBD_DEVINTST(udc->udp_baseaddr));
2795 devstat &= ~USBD_EP_FAST;
2796 writel(devstat, USBD_DEVINTCLR(udc->udp_baseaddr));
2797 devstat = devstat & udc->enabled_devints;
2799 /* Device specific handling needed? */
2800 if (devstat & USBD_DEV_STAT)
2801 udc_handle_dev(udc);
2803 /* Start of frame? (devstat & FRAME_INT):
2804 * The frame interrupt isn't really needed for ISO support,
2805 * as the driver will queue the necessary packets */
2808 if (devstat & ERR_INT) {
2809 /* All types of errors, from cable removal during transfer to
2810 * misc protocol and bit errors. These are mostly for just info,
2811 * as the USB hardware will work around these. If these errors
2812 * happen alot, something is wrong. */
2813 udc_protocol_cmd_w(udc, CMD_RD_ERR_STAT);
2814 tmp = udc_protocol_cmd_r(udc, DAT_RD_ERR_STAT);
2815 dev_dbg(udc->dev, "Device error (0x%x)!\n", tmp);
2818 spin_unlock(&udc->lock);
2824 static irqreturn_t lpc32xx_usb_hp_irq(int irq, void *_udc)
2827 struct lpc32xx_udc *udc = _udc;
2829 spin_lock(&udc->lock);
2831 /* Read the device status register */
2832 writel(USBD_EP_FAST, USBD_DEVINTCLR(udc->udp_baseaddr));
2835 tmp = readl(USBD_EPINTST(udc->udp_baseaddr));
2837 /* Special handling for EP0 */
2838 if (tmp & (EP_MASK_SEL(0, EP_OUT) | EP_MASK_SEL(0, EP_IN))) {
2840 if (tmp & (EP_MASK_SEL(0, EP_IN)))
2841 udc_handle_ep0_in(udc);
2843 /* Handle EP0 OUT */
2844 if (tmp & (EP_MASK_SEL(0, EP_OUT)))
2845 udc_handle_ep0_out(udc);
2849 if (tmp & ~(EP_MASK_SEL(0, EP_OUT) | EP_MASK_SEL(0, EP_IN))) {
2852 /* Handle other EP interrupts */
2853 for (i = 1; i < NUM_ENDPOINTS; i++) {
2854 if (tmp & (1 << udc->ep[i].hwep_num))
2855 udc_handle_eps(udc, &udc->ep[i]);
2859 spin_unlock(&udc->lock);
2864 static irqreturn_t lpc32xx_usb_devdma_irq(int irq, void *_udc)
2866 struct lpc32xx_udc *udc = _udc;
2871 spin_lock(&udc->lock);
2873 /* Handle EP DMA EOT interrupts */
2874 tmp = readl(USBD_EOTINTST(udc->udp_baseaddr)) |
2875 (readl(USBD_EPDMAST(udc->udp_baseaddr)) &
2876 readl(USBD_NDDRTINTST(udc->udp_baseaddr))) |
2877 readl(USBD_SYSERRTINTST(udc->udp_baseaddr));
2878 for (i = 1; i < NUM_ENDPOINTS; i++) {
2879 if (tmp & (1 << udc->ep[i].hwep_num))
2880 udc_handle_dma_ep(udc, &udc->ep[i]);
2883 spin_unlock(&udc->lock);
2890 * VBUS detection, pullup handler, and Gadget cable state notification
2893 static void vbus_work(struct work_struct *work)
2896 struct lpc32xx_udc *udc = container_of(work, struct lpc32xx_udc,
2899 if (udc->enabled != 0) {
2900 /* Discharge VBUS real quick */
2901 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
2902 ISP1301_I2C_OTG_CONTROL_1, OTG1_VBUS_DISCHRG);
2904 /* Give VBUS some time (100mS) to discharge */
2907 /* Disable VBUS discharge resistor */
2908 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
2909 ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
2912 /* Clear interrupt */
2913 i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
2914 ISP1301_I2C_INTERRUPT_LATCH |
2915 ISP1301_I2C_REG_CLEAR_ADDR, ~0);
2917 /* Get the VBUS status from the transceiver */
2918 value = i2c_smbus_read_byte_data(udc->isp1301_i2c_client,
2919 ISP1301_I2C_INTERRUPT_SOURCE);
2921 /* VBUS on or off? */
2922 if (value & INT_SESS_VLD)
2928 if (udc->last_vbus != udc->vbus) {
2929 udc->last_vbus = udc->vbus;
2930 lpc32xx_vbus_session(&udc->gadget, udc->vbus);
2934 /* Re-enable after completion */
2935 enable_irq(udc->udp_irq[IRQ_USB_ATX]);
2938 static irqreturn_t lpc32xx_usb_vbus_irq(int irq, void *_udc)
2940 struct lpc32xx_udc *udc = _udc;
2942 /* Defer handling of VBUS IRQ to work queue */
2943 disable_irq_nosync(udc->udp_irq[IRQ_USB_ATX]);
2944 schedule_work(&udc->vbus_job);
2949 static int lpc32xx_start(struct usb_gadget *gadget,
2950 struct usb_gadget_driver *driver)
2952 struct lpc32xx_udc *udc = to_udc(gadget);
2955 if (!driver || driver->max_speed < USB_SPEED_FULL || !driver->setup) {
2956 dev_err(udc->dev, "bad parameter.\n");
2961 dev_err(udc->dev, "UDC already has a gadget driver\n");
2965 udc->driver = driver;
2966 udc->gadget.dev.of_node = udc->dev->of_node;
2968 udc->gadget.is_selfpowered = 1;
2971 /* Force VBUS process once to check for cable insertion */
2972 udc->last_vbus = udc->vbus = 0;
2973 schedule_work(&udc->vbus_job);
2975 /* Do not re-enable ATX IRQ (3) */
2976 for (i = IRQ_USB_LP; i < IRQ_USB_ATX; i++)
2977 enable_irq(udc->udp_irq[i]);
2982 static int lpc32xx_stop(struct usb_gadget *gadget)
2985 struct lpc32xx_udc *udc = to_udc(gadget);
2987 for (i = IRQ_USB_LP; i <= IRQ_USB_ATX; i++)
2988 disable_irq(udc->udp_irq[i]);
2991 spin_lock(&udc->lock);
2993 spin_unlock(&udc->lock);
2996 * Wait for all the endpoints to disable,
2997 * before disabling clocks. Don't wait if
2998 * endpoints are not enabled.
3000 if (atomic_read(&udc->enabled_ep_cnt))
3001 wait_event_interruptible(udc->ep_disable_wait_queue,
3002 (atomic_read(&udc->enabled_ep_cnt) == 0));
3004 spin_lock(&udc->lock);
3005 udc_clk_set(udc, 0);
3006 spin_unlock(&udc->lock);
3015 static void lpc32xx_udc_shutdown(struct platform_device *dev)
3017 /* Force disconnect on reboot */
3018 struct lpc32xx_udc *udc = platform_get_drvdata(dev);
3024 * Callbacks to be overridden by options passed via OF (TODO)
3027 static void lpc32xx_usbd_conn_chg(int conn)
3029 /* Do nothing, it might be nice to enable an LED
3030 * based on conn state being !0 */
3033 static void lpc32xx_usbd_susp_chg(int susp)
3035 /* Device suspend if susp != 0 */
3038 static void lpc32xx_rmwkup_chg(int remote_wakup_enable)
3040 /* Enable or disable USB remote wakeup */
3043 struct lpc32xx_usbd_cfg lpc32xx_usbddata = {
3045 .conn_chgb = &lpc32xx_usbd_conn_chg,
3046 .susp_chgb = &lpc32xx_usbd_susp_chg,
3047 .rmwk_chgb = &lpc32xx_rmwkup_chg,
3051 static u64 lpc32xx_usbd_dmamask = ~(u32) 0x7F;
3053 static int lpc32xx_udc_probe(struct platform_device *pdev)
3055 struct device *dev = &pdev->dev;
3056 struct lpc32xx_udc *udc;
3058 struct resource *res;
3059 dma_addr_t dma_handle;
3060 struct device_node *isp1301_node;
3062 udc = kmemdup(&controller_template, sizeof(*udc), GFP_KERNEL);
3066 for (i = 0; i <= 15; i++)
3067 udc->ep[i].udc = udc;
3068 udc->gadget.ep0 = &udc->ep[0].ep;
3070 /* init software state */
3071 udc->gadget.dev.parent = dev;
3073 udc->dev = &pdev->dev;
3076 if (pdev->dev.of_node) {
3077 isp1301_node = of_parse_phandle(pdev->dev.of_node,
3080 isp1301_node = NULL;
3083 udc->isp1301_i2c_client = isp1301_get_client(isp1301_node);
3084 if (!udc->isp1301_i2c_client) {
3085 retval = -EPROBE_DEFER;
3089 dev_info(udc->dev, "ISP1301 I2C device at address 0x%x\n",
3090 udc->isp1301_i2c_client->addr);
3092 pdev->dev.dma_mask = &lpc32xx_usbd_dmamask;
3093 retval = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
3097 udc->board = &lpc32xx_usbddata;
3100 * Resources are mapped as follows:
3101 * IORESOURCE_MEM, base address and size of USB space
3102 * IORESOURCE_IRQ, USB device low priority interrupt number
3103 * IORESOURCE_IRQ, USB device high priority interrupt number
3104 * IORESOURCE_IRQ, USB device interrupt number
3105 * IORESOURCE_IRQ, USB transceiver interrupt number
3107 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3113 spin_lock_init(&udc->lock);
3116 for (i = 0; i < 4; i++) {
3117 udc->udp_irq[i] = platform_get_irq(pdev, i);
3118 if (udc->udp_irq[i] < 0) {
3120 "irq resource %d not available!\n", i);
3121 retval = udc->udp_irq[i];
3126 udc->io_p_start = res->start;
3127 udc->io_p_size = resource_size(res);
3128 if (!request_mem_region(udc->io_p_start, udc->io_p_size, driver_name)) {
3129 dev_err(udc->dev, "someone's using UDC memory\n");
3131 goto request_mem_region_fail;
3134 udc->udp_baseaddr = ioremap(udc->io_p_start, udc->io_p_size);
3135 if (!udc->udp_baseaddr) {
3137 dev_err(udc->dev, "IO map failure\n");
3141 /* Enable AHB slave USB clock, needed for further USB clock control */
3142 writel(USB_SLAVE_HCLK_EN | (1 << 19), USB_CTRL);
3144 /* Get required clocks */
3145 udc->usb_pll_clk = clk_get(&pdev->dev, "ck_pll5");
3146 if (IS_ERR(udc->usb_pll_clk)) {
3147 dev_err(udc->dev, "failed to acquire USB PLL\n");
3148 retval = PTR_ERR(udc->usb_pll_clk);
3151 udc->usb_slv_clk = clk_get(&pdev->dev, "ck_usbd");
3152 if (IS_ERR(udc->usb_slv_clk)) {
3153 dev_err(udc->dev, "failed to acquire USB device clock\n");
3154 retval = PTR_ERR(udc->usb_slv_clk);
3155 goto usb_clk_get_fail;
3157 udc->usb_otg_clk = clk_get(&pdev->dev, "ck_usb_otg");
3158 if (IS_ERR(udc->usb_otg_clk)) {
3159 dev_err(udc->dev, "failed to acquire USB otg clock\n");
3160 retval = PTR_ERR(udc->usb_otg_clk);
3161 goto usb_otg_clk_get_fail;
3164 /* Setup PLL clock to 48MHz */
3165 retval = clk_enable(udc->usb_pll_clk);
3167 dev_err(udc->dev, "failed to start USB PLL\n");
3168 goto pll_enable_fail;
3171 retval = clk_set_rate(udc->usb_pll_clk, 48000);
3173 dev_err(udc->dev, "failed to set USB clock rate\n");
3177 writel(readl(USB_CTRL) | USB_DEV_NEED_CLK_EN, USB_CTRL);
3179 /* Enable USB device clock */
3180 retval = clk_enable(udc->usb_slv_clk);
3182 dev_err(udc->dev, "failed to start USB device clock\n");
3183 goto usb_clk_enable_fail;
3186 /* Enable USB OTG clock */
3187 retval = clk_enable(udc->usb_otg_clk);
3189 dev_err(udc->dev, "failed to start USB otg clock\n");
3190 goto usb_otg_clk_enable_fail;
3193 /* Setup deferred workqueue data */
3194 udc->poweron = udc->pullup = 0;
3195 INIT_WORK(&udc->pullup_job, pullup_work);
3196 INIT_WORK(&udc->vbus_job, vbus_work);
3198 INIT_WORK(&udc->power_job, power_work);
3201 /* All clocks are now on */
3204 isp1301_udc_configure(udc);
3205 /* Allocate memory for the UDCA */
3206 udc->udca_v_base = dma_alloc_coherent(&pdev->dev, UDCA_BUFF_SIZE,
3208 (GFP_KERNEL | GFP_DMA));
3209 if (!udc->udca_v_base) {
3210 dev_err(udc->dev, "error getting UDCA region\n");
3214 udc->udca_p_base = dma_handle;
3215 dev_dbg(udc->dev, "DMA buffer(0x%x bytes), P:0x%08x, V:0x%p\n",
3216 UDCA_BUFF_SIZE, udc->udca_p_base, udc->udca_v_base);
3218 /* Setup the DD DMA memory pool */
3219 udc->dd_cache = dma_pool_create("udc_dd", udc->dev,
3220 sizeof(struct lpc32xx_usbd_dd_gad),
3222 if (!udc->dd_cache) {
3223 dev_err(udc->dev, "error getting DD DMA region\n");
3225 goto dma_alloc_fail;
3228 /* Clear USB peripheral and initialize gadget endpoints */
3232 /* Request IRQs - low and high priority USB device IRQs are routed to
3233 * the same handler, while the DMA interrupt is routed elsewhere */
3234 retval = request_irq(udc->udp_irq[IRQ_USB_LP], lpc32xx_usb_lp_irq,
3237 dev_err(udc->dev, "LP request irq %d failed\n",
3238 udc->udp_irq[IRQ_USB_LP]);
3241 retval = request_irq(udc->udp_irq[IRQ_USB_HP], lpc32xx_usb_hp_irq,
3244 dev_err(udc->dev, "HP request irq %d failed\n",
3245 udc->udp_irq[IRQ_USB_HP]);
3249 retval = request_irq(udc->udp_irq[IRQ_USB_DEVDMA],
3250 lpc32xx_usb_devdma_irq, 0, "udc_dma", udc);
3252 dev_err(udc->dev, "DEV request irq %d failed\n",
3253 udc->udp_irq[IRQ_USB_DEVDMA]);
3257 /* The transceiver interrupt is used for VBUS detection and will
3258 kick off the VBUS handler function */
3259 retval = request_irq(udc->udp_irq[IRQ_USB_ATX], lpc32xx_usb_vbus_irq,
3262 dev_err(udc->dev, "VBUS request irq %d failed\n",
3263 udc->udp_irq[IRQ_USB_ATX]);
3267 /* Initialize wait queue */
3268 init_waitqueue_head(&udc->ep_disable_wait_queue);
3269 atomic_set(&udc->enabled_ep_cnt, 0);
3271 /* Keep all IRQs disabled until GadgetFS starts up */
3272 for (i = IRQ_USB_LP; i <= IRQ_USB_ATX; i++)
3273 disable_irq(udc->udp_irq[i]);
3275 retval = usb_add_gadget_udc(dev, &udc->gadget);
3277 goto add_gadget_fail;
3279 dev_set_drvdata(dev, udc);
3280 device_init_wakeup(dev, 1);
3281 create_debug_file(udc);
3283 /* Disable clocks for now */
3284 udc_clk_set(udc, 0);
3286 dev_info(udc->dev, "%s version %s\n", driver_name, DRIVER_VERSION);
3290 free_irq(udc->udp_irq[IRQ_USB_ATX], udc);
3292 free_irq(udc->udp_irq[IRQ_USB_DEVDMA], udc);
3294 free_irq(udc->udp_irq[IRQ_USB_HP], udc);
3296 free_irq(udc->udp_irq[IRQ_USB_LP], udc);
3298 dma_pool_destroy(udc->dd_cache);
3300 dma_free_coherent(&pdev->dev, UDCA_BUFF_SIZE,
3301 udc->udca_v_base, udc->udca_p_base);
3303 clk_disable(udc->usb_otg_clk);
3304 usb_otg_clk_enable_fail:
3305 clk_disable(udc->usb_slv_clk);
3306 usb_clk_enable_fail:
3308 clk_disable(udc->usb_pll_clk);
3310 clk_put(udc->usb_otg_clk);
3311 usb_otg_clk_get_fail:
3312 clk_put(udc->usb_slv_clk);
3314 clk_put(udc->usb_pll_clk);
3316 iounmap(udc->udp_baseaddr);
3318 release_mem_region(udc->io_p_start, udc->io_p_size);
3319 dev_err(udc->dev, "%s probe failed, %d\n", driver_name, retval);
3320 request_mem_region_fail:
3328 static int lpc32xx_udc_remove(struct platform_device *pdev)
3330 struct lpc32xx_udc *udc = platform_get_drvdata(pdev);
3332 usb_del_gadget_udc(&udc->gadget);
3336 udc_clk_set(udc, 1);
3340 free_irq(udc->udp_irq[IRQ_USB_ATX], udc);
3342 device_init_wakeup(&pdev->dev, 0);
3343 remove_debug_file(udc);
3345 dma_pool_destroy(udc->dd_cache);
3346 dma_free_coherent(&pdev->dev, UDCA_BUFF_SIZE,
3347 udc->udca_v_base, udc->udca_p_base);
3348 free_irq(udc->udp_irq[IRQ_USB_DEVDMA], udc);
3349 free_irq(udc->udp_irq[IRQ_USB_HP], udc);
3350 free_irq(udc->udp_irq[IRQ_USB_LP], udc);
3352 clk_disable(udc->usb_otg_clk);
3353 clk_put(udc->usb_otg_clk);
3354 clk_disable(udc->usb_slv_clk);
3355 clk_put(udc->usb_slv_clk);
3356 clk_disable(udc->usb_pll_clk);
3357 clk_put(udc->usb_pll_clk);
3358 iounmap(udc->udp_baseaddr);
3359 release_mem_region(udc->io_p_start, udc->io_p_size);
3366 static int lpc32xx_udc_suspend(struct platform_device *pdev, pm_message_t mesg)
3368 struct lpc32xx_udc *udc = platform_get_drvdata(pdev);
3371 /* Power down ISP */
3373 isp1301_set_powerstate(udc, 0);
3375 /* Disable clocking */
3376 udc_clk_set(udc, 0);
3378 /* Keep clock flag on, so we know to re-enable clocks
3382 /* Kill global USB clock */
3383 clk_disable(udc->usb_slv_clk);
3389 static int lpc32xx_udc_resume(struct platform_device *pdev)
3391 struct lpc32xx_udc *udc = platform_get_drvdata(pdev);
3394 /* Enable global USB clock */
3395 clk_enable(udc->usb_slv_clk);
3397 /* Enable clocking */
3398 udc_clk_set(udc, 1);
3400 /* ISP back to normal power mode */
3402 isp1301_set_powerstate(udc, 1);
3408 #define lpc32xx_udc_suspend NULL
3409 #define lpc32xx_udc_resume NULL
3413 static const struct of_device_id lpc32xx_udc_of_match[] = {
3414 { .compatible = "nxp,lpc3220-udc", },
3417 MODULE_DEVICE_TABLE(of, lpc32xx_udc_of_match);
3420 static struct platform_driver lpc32xx_udc_driver = {
3421 .remove = lpc32xx_udc_remove,
3422 .shutdown = lpc32xx_udc_shutdown,
3423 .suspend = lpc32xx_udc_suspend,
3424 .resume = lpc32xx_udc_resume,
3426 .name = (char *) driver_name,
3427 .of_match_table = of_match_ptr(lpc32xx_udc_of_match),
3431 module_platform_driver_probe(lpc32xx_udc_driver, lpc32xx_udc_probe);
3433 MODULE_DESCRIPTION("LPC32XX udc driver");
3434 MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>");
3435 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
3436 MODULE_LICENSE("GPL");
3437 MODULE_ALIAS("platform:lpc32xx_udc");