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>
58 #include <asm/system.h>
60 #include <mach/platform.h>
61 #include <mach/irqs.h>
62 #include <mach/board.h>
63 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
64 #include <linux/debugfs.h>
65 #include <linux/seq_file.h>
69 * USB device configuration structure
71 typedef void (*usc_chg_event
)(int);
72 struct lpc32xx_usbd_cfg
{
73 int vbus_drv_pol
; /* 0=active low drive for VBUS via ISP1301 */
74 usc_chg_event conn_chgb
; /* Connection change event (optional) */
75 usc_chg_event susp_chgb
; /* Suspend/resume event (optional) */
76 usc_chg_event rmwk_chgb
; /* Enable/disable remote wakeup */
80 * controller driver data structures
83 /* 16 endpoints (not to be confused with 32 hardware endpoints) */
84 #define NUM_ENDPOINTS 16
87 * IRQ indices make reading the code a little easier
91 #define IRQ_USB_DEVDMA 2
94 #define EP_OUT 0 /* RX (from host) */
95 #define EP_IN 1 /* TX (to host) */
97 /* Returns the interrupt mask for the selected hardware endpoint */
98 #define EP_MASK_SEL(ep, dir) (1 << (((ep) * 2) + dir))
100 #define EP_INT_TYPE 0
101 #define EP_ISO_TYPE 1
102 #define EP_BLK_TYPE 2
103 #define EP_CTL_TYPE 3
106 #define WAIT_FOR_SETUP 0 /* Wait for setup packet */
107 #define DATA_IN 1 /* Expect dev->host transfer */
108 #define DATA_OUT 2 /* Expect host->dev transfer */
110 /* DD (DMA Descriptor) structure, requires word alignment, this is already
111 * defined in the LPC32XX USB device header file, but this version is slightly
112 * modified to tag some work data with each DMA descriptor. */
113 struct lpc32xx_usbd_dd_gad
{
118 u32 dd_iso_ps_mem_addr
;
120 u32 iso_status
[6]; /* 5 spare */
125 * Logical endpoint structure
129 struct list_head queue
;
130 struct lpc32xx_udc
*udc
;
132 u32 hwep_num_base
; /* Physical hardware EP */
133 u32 hwep_num
; /* Maps to hardware endpoint */
145 const struct usb_endpoint_descriptor
*desc
;
149 * Common UDC structure
152 struct usb_gadget gadget
;
153 struct usb_gadget_driver
*driver
;
154 struct platform_device
*pdev
;
158 struct i2c_client
*isp1301_i2c_client
;
160 /* Board and device specific */
161 struct lpc32xx_usbd_cfg
*board
;
164 void __iomem
*udp_baseaddr
;
166 struct clk
*usb_pll_clk
;
167 struct clk
*usb_slv_clk
;
168 struct clk
*usb_otg_clk
;
173 struct dma_pool
*dd_cache
;
175 /* Common EP and control data */
177 u32 enabled_hwepints
;
181 /* VBUS detection, pullup, and power flags */
187 /* Work queues related to I2C support */
188 struct work_struct pullup_job
;
189 struct work_struct vbus_job
;
190 struct work_struct power_job
;
192 /* USB device peripheral - various */
193 struct lpc32xx_ep ep
[NUM_ENDPOINTS
];
199 atomic_t enabled_ep_cnt
;
200 wait_queue_head_t ep_disable_wait_queue
;
206 struct lpc32xx_request
{
207 struct usb_request req
;
208 struct list_head queue
;
209 struct lpc32xx_usbd_dd_gad
*dd_desc_ptr
;
214 static inline struct lpc32xx_udc
*to_udc(struct usb_gadget
*g
)
216 return container_of(g
, struct lpc32xx_udc
, gadget
);
219 #define ep_dbg(epp, fmt, arg...) \
220 dev_dbg(epp->udc->dev, "%s: " fmt, __func__, ## arg)
221 #define ep_err(epp, fmt, arg...) \
222 dev_err(epp->udc->dev, "%s: " fmt, __func__, ## arg)
223 #define ep_info(epp, fmt, arg...) \
224 dev_info(epp->udc->dev, "%s: " fmt, __func__, ## arg)
225 #define ep_warn(epp, fmt, arg...) \
226 dev_warn(epp->udc->dev, "%s:" fmt, __func__, ## arg)
228 #define UDCA_BUFF_SIZE (128)
230 /* TODO: When the clock framework is introduced in LPC32xx, IO_ADDRESS will
231 * be replaced with an inremap()ed pointer
233 #define USB_CTRL IO_ADDRESS(LPC32XX_CLK_PM_BASE + 0x64)
235 /* USB_CTRL bit defines */
236 #define USB_SLAVE_HCLK_EN (1 << 24)
237 #define USB_HOST_NEED_CLK_EN (1 << 21)
238 #define USB_DEV_NEED_CLK_EN (1 << 22)
240 /**********************************************************************
241 * USB device controller register offsets
242 **********************************************************************/
244 #define USBD_DEVINTST(x) ((x) + 0x200)
245 #define USBD_DEVINTEN(x) ((x) + 0x204)
246 #define USBD_DEVINTCLR(x) ((x) + 0x208)
247 #define USBD_DEVINTSET(x) ((x) + 0x20C)
248 #define USBD_CMDCODE(x) ((x) + 0x210)
249 #define USBD_CMDDATA(x) ((x) + 0x214)
250 #define USBD_RXDATA(x) ((x) + 0x218)
251 #define USBD_TXDATA(x) ((x) + 0x21C)
252 #define USBD_RXPLEN(x) ((x) + 0x220)
253 #define USBD_TXPLEN(x) ((x) + 0x224)
254 #define USBD_CTRL(x) ((x) + 0x228)
255 #define USBD_DEVINTPRI(x) ((x) + 0x22C)
256 #define USBD_EPINTST(x) ((x) + 0x230)
257 #define USBD_EPINTEN(x) ((x) + 0x234)
258 #define USBD_EPINTCLR(x) ((x) + 0x238)
259 #define USBD_EPINTSET(x) ((x) + 0x23C)
260 #define USBD_EPINTPRI(x) ((x) + 0x240)
261 #define USBD_REEP(x) ((x) + 0x244)
262 #define USBD_EPIND(x) ((x) + 0x248)
263 #define USBD_EPMAXPSIZE(x) ((x) + 0x24C)
264 /* DMA support registers only below */
265 /* Set, clear, or get enabled state of the DMA request status. If
266 * enabled, an IN or OUT token will start a DMA transfer for the EP */
267 #define USBD_DMARST(x) ((x) + 0x250)
268 #define USBD_DMARCLR(x) ((x) + 0x254)
269 #define USBD_DMARSET(x) ((x) + 0x258)
270 /* DMA UDCA head pointer */
271 #define USBD_UDCAH(x) ((x) + 0x280)
272 /* EP DMA status, enable, and disable. This is used to specifically
273 * enabled or disable DMA for a specific EP */
274 #define USBD_EPDMAST(x) ((x) + 0x284)
275 #define USBD_EPDMAEN(x) ((x) + 0x288)
276 #define USBD_EPDMADIS(x) ((x) + 0x28C)
277 /* DMA master interrupts enable and pending interrupts */
278 #define USBD_DMAINTST(x) ((x) + 0x290)
279 #define USBD_DMAINTEN(x) ((x) + 0x294)
280 /* DMA end of transfer interrupt enable, disable, status */
281 #define USBD_EOTINTST(x) ((x) + 0x2A0)
282 #define USBD_EOTINTCLR(x) ((x) + 0x2A4)
283 #define USBD_EOTINTSET(x) ((x) + 0x2A8)
284 /* New DD request interrupt enable, disable, status */
285 #define USBD_NDDRTINTST(x) ((x) + 0x2AC)
286 #define USBD_NDDRTINTCLR(x) ((x) + 0x2B0)
287 #define USBD_NDDRTINTSET(x) ((x) + 0x2B4)
288 /* DMA error interrupt enable, disable, status */
289 #define USBD_SYSERRTINTST(x) ((x) + 0x2B8)
290 #define USBD_SYSERRTINTCLR(x) ((x) + 0x2BC)
291 #define USBD_SYSERRTINTSET(x) ((x) + 0x2C0)
293 /**********************************************************************
294 * USBD_DEVINTST/USBD_DEVINTEN/USBD_DEVINTCLR/USBD_DEVINTSET/
295 * USBD_DEVINTPRI register definitions
296 **********************************************************************/
297 #define USBD_ERR_INT (1 << 9)
298 #define USBD_EP_RLZED (1 << 8)
299 #define USBD_TXENDPKT (1 << 7)
300 #define USBD_RXENDPKT (1 << 6)
301 #define USBD_CDFULL (1 << 5)
302 #define USBD_CCEMPTY (1 << 4)
303 #define USBD_DEV_STAT (1 << 3)
304 #define USBD_EP_SLOW (1 << 2)
305 #define USBD_EP_FAST (1 << 1)
306 #define USBD_FRAME (1 << 0)
308 /**********************************************************************
309 * USBD_EPINTST/USBD_EPINTEN/USBD_EPINTCLR/USBD_EPINTSET/
310 * USBD_EPINTPRI register definitions
311 **********************************************************************/
312 /* End point selection macro (RX) */
313 #define USBD_RX_EP_SEL(e) (1 << ((e) << 1))
315 /* End point selection macro (TX) */
316 #define USBD_TX_EP_SEL(e) (1 << (((e) << 1) + 1))
318 /**********************************************************************
319 * USBD_REEP/USBD_DMARST/USBD_DMARCLR/USBD_DMARSET/USBD_EPDMAST/
320 * USBD_EPDMAEN/USBD_EPDMADIS/
321 * USBD_NDDRTINTST/USBD_NDDRTINTCLR/USBD_NDDRTINTSET/
322 * USBD_EOTINTST/USBD_EOTINTCLR/USBD_EOTINTSET/
323 * USBD_SYSERRTINTST/USBD_SYSERRTINTCLR/USBD_SYSERRTINTSET
324 * register definitions
325 **********************************************************************/
326 /* Endpoint selection macro */
327 #define USBD_EP_SEL(e) (1 << (e))
329 /**********************************************************************
330 * SBD_DMAINTST/USBD_DMAINTEN
331 **********************************************************************/
332 #define USBD_SYS_ERR_INT (1 << 2)
333 #define USBD_NEW_DD_INT (1 << 1)
334 #define USBD_EOT_INT (1 << 0)
336 /**********************************************************************
337 * USBD_RXPLEN register definitions
338 **********************************************************************/
339 #define USBD_PKT_RDY (1 << 11)
340 #define USBD_DV (1 << 10)
341 #define USBD_PK_LEN_MASK 0x3FF
343 /**********************************************************************
344 * USBD_CTRL register definitions
345 **********************************************************************/
346 #define USBD_LOG_ENDPOINT(e) ((e) << 2)
347 #define USBD_WR_EN (1 << 1)
348 #define USBD_RD_EN (1 << 0)
350 /**********************************************************************
351 * USBD_CMDCODE register definitions
352 **********************************************************************/
353 #define USBD_CMD_CODE(c) ((c) << 16)
354 #define USBD_CMD_PHASE(p) ((p) << 8)
356 /**********************************************************************
357 * USBD_DMARST/USBD_DMARCLR/USBD_DMARSET register definitions
358 **********************************************************************/
359 #define USBD_DMAEP(e) (1 << (e))
361 /* DD (DMA Descriptor) structure, requires word alignment */
362 struct lpc32xx_usbd_dd
{
367 u32 dd_iso_ps_mem_addr
;
370 /* dd_setup bit defines */
371 #define DD_SETUP_ATLE_DMA_MODE 0x01
372 #define DD_SETUP_NEXT_DD_VALID 0x04
373 #define DD_SETUP_ISO_EP 0x10
374 #define DD_SETUP_PACKETLEN(n) (((n) & 0x7FF) << 5)
375 #define DD_SETUP_DMALENBYTES(n) (((n) & 0xFFFF) << 16)
377 /* dd_status bit defines */
378 #define DD_STATUS_DD_RETIRED 0x01
379 #define DD_STATUS_STS_MASK 0x1E
380 #define DD_STATUS_STS_NS 0x00 /* Not serviced */
381 #define DD_STATUS_STS_BS 0x02 /* Being serviced */
382 #define DD_STATUS_STS_NC 0x04 /* Normal completion */
383 #define DD_STATUS_STS_DUR 0x06 /* Data underrun (short packet) */
384 #define DD_STATUS_STS_DOR 0x08 /* Data overrun */
385 #define DD_STATUS_STS_SE 0x12 /* System error */
386 #define DD_STATUS_PKT_VAL 0x20 /* Packet valid */
387 #define DD_STATUS_LSB_EX 0x40 /* LS byte extracted (ATLE) */
388 #define DD_STATUS_MSB_EX 0x80 /* MS byte extracted (ATLE) */
389 #define DD_STATUS_MLEN(n) (((n) >> 8) & 0x3F)
390 #define DD_STATUS_CURDMACNT(n) (((n) >> 16) & 0xFFFF)
394 * Protocol engine bits below
397 /* Device Interrupt Bit Definitions */
398 #define FRAME_INT 0x00000001
399 #define EP_FAST_INT 0x00000002
400 #define EP_SLOW_INT 0x00000004
401 #define DEV_STAT_INT 0x00000008
402 #define CCEMTY_INT 0x00000010
403 #define CDFULL_INT 0x00000020
404 #define RxENDPKT_INT 0x00000040
405 #define TxENDPKT_INT 0x00000080
406 #define EP_RLZED_INT 0x00000100
407 #define ERR_INT 0x00000200
409 /* Rx & Tx Packet Length Definitions */
410 #define PKT_LNGTH_MASK 0x000003FF
411 #define PKT_DV 0x00000400
412 #define PKT_RDY 0x00000800
414 /* USB Control Definitions */
415 #define CTRL_RD_EN 0x00000001
416 #define CTRL_WR_EN 0x00000002
419 #define CMD_SET_ADDR 0x00D00500
420 #define CMD_CFG_DEV 0x00D80500
421 #define CMD_SET_MODE 0x00F30500
422 #define CMD_RD_FRAME 0x00F50500
423 #define DAT_RD_FRAME 0x00F50200
424 #define CMD_RD_TEST 0x00FD0500
425 #define DAT_RD_TEST 0x00FD0200
426 #define CMD_SET_DEV_STAT 0x00FE0500
427 #define CMD_GET_DEV_STAT 0x00FE0500
428 #define DAT_GET_DEV_STAT 0x00FE0200
429 #define CMD_GET_ERR_CODE 0x00FF0500
430 #define DAT_GET_ERR_CODE 0x00FF0200
431 #define CMD_RD_ERR_STAT 0x00FB0500
432 #define DAT_RD_ERR_STAT 0x00FB0200
433 #define DAT_WR_BYTE(x) (0x00000100 | ((x) << 16))
434 #define CMD_SEL_EP(x) (0x00000500 | ((x) << 16))
435 #define DAT_SEL_EP(x) (0x00000200 | ((x) << 16))
436 #define CMD_SEL_EP_CLRI(x) (0x00400500 | ((x) << 16))
437 #define DAT_SEL_EP_CLRI(x) (0x00400200 | ((x) << 16))
438 #define CMD_SET_EP_STAT(x) (0x00400500 | ((x) << 16))
439 #define CMD_CLR_BUF 0x00F20500
440 #define DAT_CLR_BUF 0x00F20200
441 #define CMD_VALID_BUF 0x00FA0500
443 /* Device Address Register Definitions */
444 #define DEV_ADDR_MASK 0x7F
447 /* Device Configure Register Definitions */
448 #define CONF_DVICE 0x01
450 /* Device Mode Register Definitions */
459 /* Device Status Register Definitions */
461 #define DEV_CON_CH 0x02
463 #define DEV_SUS_CH 0x08
466 /* Error Code Register Definitions */
467 #define ERR_EC_MASK 0x0F
470 /* Error Status Register Definitions */
472 #define ERR_UEPKT 0x02
473 #define ERR_DCRC 0x04
474 #define ERR_TIMOUT 0x08
476 #define ERR_B_OVRN 0x20
477 #define ERR_BTSTF 0x40
480 /* Endpoint Select Register Definitions */
481 #define EP_SEL_F 0x01
482 #define EP_SEL_ST 0x02
483 #define EP_SEL_STP 0x04
484 #define EP_SEL_PO 0x08
485 #define EP_SEL_EPN 0x10
486 #define EP_SEL_B_1_FULL 0x20
487 #define EP_SEL_B_2_FULL 0x40
489 /* Endpoint Status Register Definitions */
490 #define EP_STAT_ST 0x01
491 #define EP_STAT_DA 0x20
492 #define EP_STAT_RF_MO 0x40
493 #define EP_STAT_CND_ST 0x80
495 /* Clear Buffer Register Definitions */
496 #define CLR_BUF_PO 0x01
498 /* DMA Interrupt Bit Definitions */
500 #define NDD_REQ_INT 0x02
501 #define SYS_ERR_INT 0x04
503 #define DRIVER_VERSION "1.03"
504 static const char driver_name
[] = "lpc32xx_udc";
508 * proc interface support
511 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
512 static char *epnames
[] = {"INT", "ISO", "BULK", "CTRL"};
513 static const char debug_filename
[] = "driver/udc";
515 static void proc_ep_show(struct seq_file
*s
, struct lpc32xx_ep
*ep
)
517 struct lpc32xx_request
*req
;
520 seq_printf(s
, "%12s, maxpacket %4d %3s",
521 ep
->ep
.name
, ep
->ep
.maxpacket
,
522 ep
->is_in
? "in" : "out");
523 seq_printf(s
, " type %4s", epnames
[ep
->eptype
]);
524 seq_printf(s
, " ints: %12d", ep
->totalints
);
526 if (list_empty(&ep
->queue
))
527 seq_printf(s
, "\t(queue empty)\n");
529 list_for_each_entry(req
, &ep
->queue
, queue
) {
530 u32 length
= req
->req
.actual
;
532 seq_printf(s
, "\treq %p len %d/%d buf %p\n",
534 req
->req
.length
, req
->req
.buf
);
539 static int proc_udc_show(struct seq_file
*s
, void *unused
)
541 struct lpc32xx_udc
*udc
= s
->private;
542 struct lpc32xx_ep
*ep
;
545 seq_printf(s
, "%s: version %s\n", driver_name
, DRIVER_VERSION
);
547 spin_lock_irqsave(&udc
->lock
, flags
);
549 seq_printf(s
, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
550 udc
->vbus
? "present" : "off",
551 udc
->enabled
? (udc
->vbus
? "active" : "enabled") :
553 udc
->selfpowered
? "self" : "VBUS",
554 udc
->suspended
? ", suspended" : "",
555 udc
->driver
? udc
->driver
->driver
.name
: "(none)");
557 if (udc
->enabled
&& udc
->vbus
) {
558 proc_ep_show(s
, &udc
->ep
[0]);
559 list_for_each_entry(ep
, &udc
->gadget
.ep_list
, ep
.ep_list
) {
565 spin_unlock_irqrestore(&udc
->lock
, flags
);
570 static int proc_udc_open(struct inode
*inode
, struct file
*file
)
572 return single_open(file
, proc_udc_show
, PDE(inode
)->data
);
575 static const struct file_operations proc_ops
= {
576 .owner
= THIS_MODULE
,
577 .open
= proc_udc_open
,
580 .release
= single_release
,
583 static void create_debug_file(struct lpc32xx_udc
*udc
)
585 udc
->pde
= debugfs_create_file(debug_filename
, 0, NULL
, udc
, &proc_ops
);
588 static void remove_debug_file(struct lpc32xx_udc
*udc
)
591 debugfs_remove(udc
->pde
);
595 static inline void create_debug_file(struct lpc32xx_udc
*udc
) {}
596 static inline void remove_debug_file(struct lpc32xx_udc
*udc
) {}
599 /* Primary initialization sequence for the ISP1301 transceiver */
600 static void isp1301_udc_configure(struct lpc32xx_udc
*udc
)
602 /* LPC32XX only supports DAT_SE0 USB mode */
603 /* This sequence is important */
605 /* Disable transparent UART mode first */
606 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
607 (ISP1301_I2C_MODE_CONTROL_1
| ISP1301_I2C_REG_CLEAR_ADDR
),
610 /* Set full speed and SE0 mode */
611 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
612 (ISP1301_I2C_MODE_CONTROL_1
| ISP1301_I2C_REG_CLEAR_ADDR
), ~0);
613 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
614 ISP1301_I2C_MODE_CONTROL_1
, (MC1_SPEED_REG
| MC1_DAT_SE0
));
617 * The PSW_OE enable bit state is reversed in the ISP1301 User's Guide
619 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
620 (ISP1301_I2C_MODE_CONTROL_2
| ISP1301_I2C_REG_CLEAR_ADDR
), ~0);
621 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
622 ISP1301_I2C_MODE_CONTROL_2
, (MC2_BI_DI
| MC2_SPD_SUSP_CTRL
));
624 /* Driver VBUS_DRV high or low depending on board setup */
625 if (udc
->board
->vbus_drv_pol
!= 0)
626 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
627 ISP1301_I2C_OTG_CONTROL_1
, OTG1_VBUS_DRV
);
629 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
630 ISP1301_I2C_OTG_CONTROL_1
| ISP1301_I2C_REG_CLEAR_ADDR
,
633 /* Bi-directional mode with suspend control
634 * Enable both pulldowns for now - the pullup will be enable when VBUS
636 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
637 (ISP1301_I2C_OTG_CONTROL_1
| ISP1301_I2C_REG_CLEAR_ADDR
), ~0);
638 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
639 ISP1301_I2C_OTG_CONTROL_1
,
640 (0 | OTG1_DM_PULLDOWN
| OTG1_DP_PULLDOWN
));
642 /* Discharge VBUS (just in case) */
643 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
644 ISP1301_I2C_OTG_CONTROL_1
, OTG1_VBUS_DISCHRG
);
646 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
647 (ISP1301_I2C_OTG_CONTROL_1
| ISP1301_I2C_REG_CLEAR_ADDR
),
650 /* Clear and enable VBUS high edge interrupt */
651 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
652 ISP1301_I2C_INTERRUPT_LATCH
| ISP1301_I2C_REG_CLEAR_ADDR
, ~0);
653 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
654 ISP1301_I2C_INTERRUPT_FALLING
| ISP1301_I2C_REG_CLEAR_ADDR
, ~0);
655 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
656 ISP1301_I2C_INTERRUPT_FALLING
, INT_VBUS_VLD
);
657 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
658 ISP1301_I2C_INTERRUPT_RISING
| ISP1301_I2C_REG_CLEAR_ADDR
, ~0);
659 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
660 ISP1301_I2C_INTERRUPT_RISING
, INT_VBUS_VLD
);
662 /* Enable usb_need_clk clock after transceiver is initialized */
663 writel((readl(USB_CTRL
) | USB_DEV_NEED_CLK_EN
), USB_CTRL
);
665 dev_info(udc
->dev
, "ISP1301 Vendor ID : 0x%04x\n",
666 i2c_smbus_read_word_data(udc
->isp1301_i2c_client
, 0x00));
667 dev_info(udc
->dev
, "ISP1301 Product ID : 0x%04x\n",
668 i2c_smbus_read_word_data(udc
->isp1301_i2c_client
, 0x02));
669 dev_info(udc
->dev
, "ISP1301 Version ID : 0x%04x\n",
670 i2c_smbus_read_word_data(udc
->isp1301_i2c_client
, 0x14));
673 /* Enables or disables the USB device pullup via the ISP1301 transceiver */
674 static void isp1301_pullup_set(struct lpc32xx_udc
*udc
)
677 /* Enable pullup for bus signalling */
678 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
679 ISP1301_I2C_OTG_CONTROL_1
, OTG1_DP_PULLUP
);
681 /* Enable pullup for bus signalling */
682 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
683 ISP1301_I2C_OTG_CONTROL_1
| ISP1301_I2C_REG_CLEAR_ADDR
,
687 static void pullup_work(struct work_struct
*work
)
689 struct lpc32xx_udc
*udc
=
690 container_of(work
, struct lpc32xx_udc
, pullup_job
);
692 isp1301_pullup_set(udc
);
695 static void isp1301_pullup_enable(struct lpc32xx_udc
*udc
, int en_pullup
,
698 if (en_pullup
== udc
->pullup
)
701 udc
->pullup
= en_pullup
;
703 isp1301_pullup_set(udc
);
705 /* defer slow i2c pull up setting */
706 schedule_work(&udc
->pullup_job
);
710 /* Powers up or down the ISP1301 transceiver */
711 static void isp1301_set_powerstate(struct lpc32xx_udc
*udc
, int enable
)
714 /* Power up ISP1301 - this ISP1301 will automatically wakeup
715 when VBUS is detected */
716 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
717 ISP1301_I2C_MODE_CONTROL_2
| ISP1301_I2C_REG_CLEAR_ADDR
,
720 /* Power down ISP1301 */
721 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
722 ISP1301_I2C_MODE_CONTROL_2
, MC2_GLOBAL_PWR_DN
);
725 static void power_work(struct work_struct
*work
)
727 struct lpc32xx_udc
*udc
=
728 container_of(work
, struct lpc32xx_udc
, power_job
);
730 isp1301_set_powerstate(udc
, udc
->poweron
);
736 * USB protocol engine command/data read/write helper functions
739 /* Issues a single command to the USB device state machine */
740 static void udc_protocol_cmd_w(struct lpc32xx_udc
*udc
, u32 cmd
)
745 /* EP may lock on CLRI if this read isn't done */
746 u32 tmp
= readl(USBD_DEVINTST(udc
->udp_baseaddr
));
750 writel(USBD_CCEMPTY
, USBD_DEVINTCLR(udc
->udp_baseaddr
));
752 /* Write command code */
753 writel(cmd
, USBD_CMDCODE(udc
->udp_baseaddr
));
755 while (((readl(USBD_DEVINTST(udc
->udp_baseaddr
)) &
756 USBD_CCEMPTY
) == 0) && (to
> 0)) {
767 /* Issues 2 commands (or command and data) to the USB device state machine */
768 static inline void udc_protocol_cmd_data_w(struct lpc32xx_udc
*udc
, u32 cmd
,
771 udc_protocol_cmd_w(udc
, cmd
);
772 udc_protocol_cmd_w(udc
, data
);
775 /* Issues a single command to the USB device state machine and reads
777 static u32
udc_protocol_cmd_r(struct lpc32xx_udc
*udc
, u32 cmd
)
782 /* Write a command and read data from the protocol engine */
783 writel((USBD_CDFULL
| USBD_CCEMPTY
),
784 USBD_DEVINTCLR(udc
->udp_baseaddr
));
786 /* Write command code */
787 udc_protocol_cmd_w(udc
, cmd
);
789 tmp
= readl(USBD_DEVINTST(udc
->udp_baseaddr
));
790 while ((!(readl(USBD_DEVINTST(udc
->udp_baseaddr
)) & USBD_CDFULL
))
795 "Protocol engine didn't receive response (CDFULL)\n");
797 return readl(USBD_CMDDATA(udc
->udp_baseaddr
));
802 * USB device interrupt mask support functions
805 /* Enable one or more USB device interrupts */
806 static inline void uda_enable_devint(struct lpc32xx_udc
*udc
, u32 devmask
)
808 udc
->enabled_devints
|= devmask
;
809 writel(udc
->enabled_devints
, USBD_DEVINTEN(udc
->udp_baseaddr
));
812 /* Disable one or more USB device interrupts */
813 static inline void uda_disable_devint(struct lpc32xx_udc
*udc
, u32 mask
)
815 udc
->enabled_devints
&= ~mask
;
816 writel(udc
->enabled_devints
, USBD_DEVINTEN(udc
->udp_baseaddr
));
819 /* Clear one or more USB device interrupts */
820 static inline void uda_clear_devint(struct lpc32xx_udc
*udc
, u32 mask
)
822 writel(mask
, USBD_DEVINTCLR(udc
->udp_baseaddr
));
827 * Endpoint interrupt disable/enable functions
830 /* Enable one or more USB endpoint interrupts */
831 static void uda_enable_hwepint(struct lpc32xx_udc
*udc
, u32 hwep
)
833 udc
->enabled_hwepints
|= (1 << hwep
);
834 writel(udc
->enabled_hwepints
, USBD_EPINTEN(udc
->udp_baseaddr
));
837 /* Disable one or more USB endpoint interrupts */
838 static void uda_disable_hwepint(struct lpc32xx_udc
*udc
, u32 hwep
)
840 udc
->enabled_hwepints
&= ~(1 << hwep
);
841 writel(udc
->enabled_hwepints
, USBD_EPINTEN(udc
->udp_baseaddr
));
844 /* Clear one or more USB endpoint interrupts */
845 static inline void uda_clear_hwepint(struct lpc32xx_udc
*udc
, u32 hwep
)
847 writel((1 << hwep
), USBD_EPINTCLR(udc
->udp_baseaddr
));
850 /* Enable DMA for the HW channel */
851 static inline void udc_ep_dma_enable(struct lpc32xx_udc
*udc
, u32 hwep
)
853 writel((1 << hwep
), USBD_EPDMAEN(udc
->udp_baseaddr
));
856 /* Disable DMA for the HW channel */
857 static inline void udc_ep_dma_disable(struct lpc32xx_udc
*udc
, u32 hwep
)
859 writel((1 << hwep
), USBD_EPDMADIS(udc
->udp_baseaddr
));
864 * Endpoint realize/unrealize functions
867 /* Before an endpoint can be used, it needs to be realized
868 * in the USB protocol engine - this realizes the endpoint.
869 * The interrupt (FIFO or DMA) is not enabled with this function */
870 static void udc_realize_hwep(struct lpc32xx_udc
*udc
, u32 hwep
,
875 writel(USBD_EP_RLZED
, USBD_DEVINTCLR(udc
->udp_baseaddr
));
876 writel(hwep
, USBD_EPIND(udc
->udp_baseaddr
));
877 udc
->realized_eps
|= (1 << hwep
);
878 writel(udc
->realized_eps
, USBD_REEP(udc
->udp_baseaddr
));
879 writel(maxpacket
, USBD_EPMAXPSIZE(udc
->udp_baseaddr
));
881 /* Wait until endpoint is realized in hardware */
882 while ((!(readl(USBD_DEVINTST(udc
->udp_baseaddr
)) &
883 USBD_EP_RLZED
)) && (to
> 0))
886 dev_dbg(udc
->dev
, "EP not correctly realized in hardware\n");
888 writel(USBD_EP_RLZED
, USBD_DEVINTCLR(udc
->udp_baseaddr
));
891 /* Unrealize an EP */
892 static void udc_unrealize_hwep(struct lpc32xx_udc
*udc
, u32 hwep
)
894 udc
->realized_eps
&= ~(1 << hwep
);
895 writel(udc
->realized_eps
, USBD_REEP(udc
->udp_baseaddr
));
900 * Endpoint support functions
903 /* Select and clear endpoint interrupt */
904 static u32
udc_selep_clrint(struct lpc32xx_udc
*udc
, u32 hwep
)
906 udc_protocol_cmd_w(udc
, CMD_SEL_EP_CLRI(hwep
));
907 return udc_protocol_cmd_r(udc
, DAT_SEL_EP_CLRI(hwep
));
910 /* Disables the endpoint in the USB protocol engine */
911 static void udc_disable_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_DA
));
917 /* Stalls the endpoint - endpoint will return STALL */
918 static void udc_stall_hwep(struct lpc32xx_udc
*udc
, u32 hwep
)
920 udc_protocol_cmd_data_w(udc
, CMD_SET_EP_STAT(hwep
),
921 DAT_WR_BYTE(EP_STAT_ST
));
924 /* Clear stall or reset endpoint */
925 static void udc_clrstall_hwep(struct lpc32xx_udc
*udc
, u32 hwep
)
927 udc_protocol_cmd_data_w(udc
, CMD_SET_EP_STAT(hwep
),
931 /* Select an endpoint for endpoint status, clear, validate */
932 static void udc_select_hwep(struct lpc32xx_udc
*udc
, u32 hwep
)
934 udc_protocol_cmd_w(udc
, CMD_SEL_EP(hwep
));
939 * Endpoint buffer management functions
942 /* Clear the current endpoint's buffer */
943 static void udc_clr_buffer_hwep(struct lpc32xx_udc
*udc
, u32 hwep
)
945 udc_select_hwep(udc
, hwep
);
946 udc_protocol_cmd_w(udc
, CMD_CLR_BUF
);
949 /* Validate the current endpoint's buffer */
950 static void udc_val_buffer_hwep(struct lpc32xx_udc
*udc
, u32 hwep
)
952 udc_select_hwep(udc
, hwep
);
953 udc_protocol_cmd_w(udc
, CMD_VALID_BUF
);
956 static inline u32
udc_clearep_getsts(struct lpc32xx_udc
*udc
, u32 hwep
)
958 /* Clear EP interrupt */
959 uda_clear_hwepint(udc
, hwep
);
960 return udc_selep_clrint(udc
, hwep
);
968 /* Allocate a DMA Descriptor */
969 static struct lpc32xx_usbd_dd_gad
*udc_dd_alloc(struct lpc32xx_udc
*udc
)
972 struct lpc32xx_usbd_dd_gad
*dd
;
974 dd
= (struct lpc32xx_usbd_dd_gad
*) dma_pool_alloc(
975 udc
->dd_cache
, (GFP_KERNEL
| GFP_DMA
), &dma
);
982 /* Free a DMA Descriptor */
983 static void udc_dd_free(struct lpc32xx_udc
*udc
, struct lpc32xx_usbd_dd_gad
*dd
)
985 dma_pool_free(udc
->dd_cache
, dd
, dd
->this_dma
);
990 * USB setup and shutdown functions
993 /* Enables or disables most of the USB system clocks when low power mode is
994 * needed. Clocks are typically started on a connection event, and disabled
995 * when a cable is disconnected */
996 static void udc_clk_set(struct lpc32xx_udc
*udc
, int enable
)
1005 clk_enable(udc
->usb_pll_clk
);
1007 /* Enable the USB device clock */
1008 writel(readl(USB_CTRL
) | USB_DEV_NEED_CLK_EN
,
1011 clk_enable(udc
->usb_otg_clk
);
1018 /* Never disable the USB_HCLK during normal operation */
1020 /* 48MHz PLL dpwn */
1021 clk_disable(udc
->usb_pll_clk
);
1023 /* Disable the USB device clock */
1024 writel(readl(USB_CTRL
) & ~USB_DEV_NEED_CLK_EN
,
1027 clk_disable(udc
->usb_otg_clk
);
1031 /* Set/reset USB device address */
1032 static void udc_set_address(struct lpc32xx_udc
*udc
, u32 addr
)
1034 /* Address will be latched at the end of the status phase, or
1035 latched immediately if function is called twice */
1036 udc_protocol_cmd_data_w(udc
, CMD_SET_ADDR
,
1037 DAT_WR_BYTE(DEV_EN
| addr
));
1040 /* Setup up a IN request for DMA transfer - this consists of determining the
1041 * list of DMA addresses for the transfer, allocating DMA Descriptors,
1042 * installing the DD into the UDCA, and then enabling the DMA for that EP */
1043 static int udc_ep_in_req_dma(struct lpc32xx_udc
*udc
, struct lpc32xx_ep
*ep
)
1045 struct lpc32xx_request
*req
;
1046 u32 hwep
= ep
->hwep_num
;
1048 ep
->req_pending
= 1;
1050 /* There will always be a request waiting here */
1051 req
= list_entry(ep
->queue
.next
, struct lpc32xx_request
, queue
);
1053 /* Place the DD Descriptor into the UDCA */
1054 udc
->udca_v_base
[hwep
] = req
->dd_desc_ptr
->this_dma
;
1056 /* Enable DMA and interrupt for the HW EP */
1057 udc_ep_dma_enable(udc
, hwep
);
1059 /* Clear ZLP if last packet is not of MAXP size */
1060 if (req
->req
.length
% ep
->ep
.maxpacket
)
1066 /* Setup up a OUT request for DMA transfer - this consists of determining the
1067 * list of DMA addresses for the transfer, allocating DMA Descriptors,
1068 * installing the DD into the UDCA, and then enabling the DMA for that EP */
1069 static int udc_ep_out_req_dma(struct lpc32xx_udc
*udc
, struct lpc32xx_ep
*ep
)
1071 struct lpc32xx_request
*req
;
1072 u32 hwep
= ep
->hwep_num
;
1074 ep
->req_pending
= 1;
1076 /* There will always be a request waiting here */
1077 req
= list_entry(ep
->queue
.next
, struct lpc32xx_request
, queue
);
1079 /* Place the DD Descriptor into the UDCA */
1080 udc
->udca_v_base
[hwep
] = req
->dd_desc_ptr
->this_dma
;
1082 /* Enable DMA and interrupt for the HW EP */
1083 udc_ep_dma_enable(udc
, hwep
);
1087 static void udc_disable(struct lpc32xx_udc
*udc
)
1091 /* Disable device */
1092 udc_protocol_cmd_data_w(udc
, CMD_CFG_DEV
, DAT_WR_BYTE(0));
1093 udc_protocol_cmd_data_w(udc
, CMD_SET_DEV_STAT
, DAT_WR_BYTE(0));
1095 /* Disable all device interrupts (including EP0) */
1096 uda_disable_devint(udc
, 0x3FF);
1098 /* Disable and reset all endpoint interrupts */
1099 for (i
= 0; i
< 32; i
++) {
1100 uda_disable_hwepint(udc
, i
);
1101 uda_clear_hwepint(udc
, i
);
1102 udc_disable_hwep(udc
, i
);
1103 udc_unrealize_hwep(udc
, i
);
1104 udc
->udca_v_base
[i
] = 0;
1106 /* Disable and clear all interrupts and DMA */
1107 udc_ep_dma_disable(udc
, i
);
1108 writel((1 << i
), USBD_EOTINTCLR(udc
->udp_baseaddr
));
1109 writel((1 << i
), USBD_NDDRTINTCLR(udc
->udp_baseaddr
));
1110 writel((1 << i
), USBD_SYSERRTINTCLR(udc
->udp_baseaddr
));
1111 writel((1 << i
), USBD_DMARCLR(udc
->udp_baseaddr
));
1114 /* Disable DMA interrupts */
1115 writel(0, USBD_DMAINTEN(udc
->udp_baseaddr
));
1117 writel(0, USBD_UDCAH(udc
->udp_baseaddr
));
1120 static void udc_enable(struct lpc32xx_udc
*udc
)
1123 struct lpc32xx_ep
*ep
= &udc
->ep
[0];
1125 /* Start with known state */
1129 udc_protocol_cmd_data_w(udc
, CMD_SET_DEV_STAT
, DAT_WR_BYTE(DEV_CON
));
1131 /* EP interrupts on high priority, FRAME interrupt on low priority */
1132 writel(USBD_EP_FAST
, USBD_DEVINTPRI(udc
->udp_baseaddr
));
1133 writel(0xFFFF, USBD_EPINTPRI(udc
->udp_baseaddr
));
1135 /* Clear any pending device interrupts */
1136 writel(0x3FF, USBD_DEVINTCLR(udc
->udp_baseaddr
));
1138 /* Setup UDCA - not yet used (DMA) */
1139 writel(udc
->udca_p_base
, USBD_UDCAH(udc
->udp_baseaddr
));
1141 /* Only enable EP0 in and out for now, EP0 only works in FIFO mode */
1142 for (i
= 0; i
<= 1; i
++) {
1143 udc_realize_hwep(udc
, i
, ep
->ep
.maxpacket
);
1144 uda_enable_hwepint(udc
, i
);
1145 udc_select_hwep(udc
, i
);
1146 udc_clrstall_hwep(udc
, i
);
1147 udc_clr_buffer_hwep(udc
, i
);
1150 /* Device interrupt setup */
1151 uda_clear_devint(udc
, (USBD_ERR_INT
| USBD_DEV_STAT
| USBD_EP_SLOW
|
1153 uda_enable_devint(udc
, (USBD_ERR_INT
| USBD_DEV_STAT
| USBD_EP_SLOW
|
1156 /* Set device address to 0 - called twice to force a latch in the USB
1157 engine without the need of a setup packet status closure */
1158 udc_set_address(udc
, 0);
1159 udc_set_address(udc
, 0);
1161 /* Enable master DMA interrupts */
1162 writel((USBD_SYS_ERR_INT
| USBD_EOT_INT
),
1163 USBD_DMAINTEN(udc
->udp_baseaddr
));
1165 udc
->dev_status
= 0;
1170 * USB device board specific events handled via callbacks
1173 /* Connection change event - notify board function of change */
1174 static void uda_power_event(struct lpc32xx_udc
*udc
, u32 conn
)
1176 /* Just notify of a connection change event (optional) */
1177 if (udc
->board
->conn_chgb
!= NULL
)
1178 udc
->board
->conn_chgb(conn
);
1181 /* Suspend/resume event - notify board function of change */
1182 static void uda_resm_susp_event(struct lpc32xx_udc
*udc
, u32 conn
)
1184 /* Just notify of a Suspend/resume change event (optional) */
1185 if (udc
->board
->susp_chgb
!= NULL
)
1186 udc
->board
->susp_chgb(conn
);
1194 /* Remote wakeup enable/disable - notify board function of change */
1195 static void uda_remwkp_cgh(struct lpc32xx_udc
*udc
)
1197 if (udc
->board
->rmwk_chgb
!= NULL
)
1198 udc
->board
->rmwk_chgb(udc
->dev_status
&
1199 (1 << USB_DEVICE_REMOTE_WAKEUP
));
1202 /* Reads data from FIFO, adjusts for alignment and data size */
1203 static void udc_pop_fifo(struct lpc32xx_udc
*udc
, u8
*data
, u32 bytes
)
1207 u32
*p32
, tmp
, cbytes
;
1209 /* Use optimal data transfer method based on source address and size */
1210 switch (((u32
) data
) & 0x3) {
1211 case 0: /* 32-bit aligned */
1213 cbytes
= (bytes
& ~0x3);
1215 /* Copy 32-bit aligned data first */
1216 for (n
= 0; n
< cbytes
; n
+= 4)
1217 *p32
++ = readl(USBD_RXDATA(udc
->udp_baseaddr
));
1219 /* Handle any remaining bytes */
1220 bl
= bytes
- cbytes
;
1222 tmp
= readl(USBD_RXDATA(udc
->udp_baseaddr
));
1223 for (n
= 0; n
< bl
; n
++)
1224 data
[cbytes
+ n
] = ((tmp
>> (n
* 8)) & 0xFF);
1229 case 1: /* 8-bit aligned */
1231 /* Each byte has to be handled independently */
1232 for (n
= 0; n
< bytes
; n
+= 4) {
1233 tmp
= readl(USBD_RXDATA(udc
->udp_baseaddr
));
1239 for (i
= 0; i
< bl
; i
++)
1240 data
[n
+ i
] = (u8
) ((tmp
>> (n
* 8)) & 0xFF);
1244 case 2: /* 16-bit aligned */
1246 cbytes
= (bytes
& ~0x3);
1248 /* Copy 32-bit sized objects first with 16-bit alignment */
1249 for (n
= 0; n
< cbytes
; n
+= 4) {
1250 tmp
= readl(USBD_RXDATA(udc
->udp_baseaddr
));
1251 *p16
++ = (u16
)(tmp
& 0xFFFF);
1252 *p16
++ = (u16
)((tmp
>> 16) & 0xFFFF);
1255 /* Handle any remaining bytes */
1256 bl
= bytes
- cbytes
;
1258 tmp
= readl(USBD_RXDATA(udc
->udp_baseaddr
));
1259 for (n
= 0; n
< bl
; n
++)
1260 data
[cbytes
+ n
] = ((tmp
>> (n
* 8)) & 0xFF);
1266 /* Read data from the FIFO for an endpoint. This function is for endpoints (such
1267 * as EP0) that don't use DMA. This function should only be called if a packet
1268 * is known to be ready to read for the endpoint. Note that the endpoint must
1269 * be selected in the protocol engine prior to this call. */
1270 static u32
udc_read_hwep(struct lpc32xx_udc
*udc
, u32 hwep
, u32
*data
,
1275 u32 tmp
, hwrep
= ((hwep
& 0x1E) << 1) | CTRL_RD_EN
;
1277 /* Setup read of endpoint */
1278 writel(hwrep
, USBD_CTRL(udc
->udp_baseaddr
));
1280 /* Wait until packet is ready */
1281 while ((((tmpv
= readl(USBD_RXPLEN(udc
->udp_baseaddr
))) &
1282 PKT_RDY
) == 0) && (to
> 0))
1285 dev_dbg(udc
->dev
, "No packet ready on FIFO EP read\n");
1287 /* Mask out count */
1288 tmp
= tmpv
& PKT_LNGTH_MASK
;
1292 if ((tmp
> 0) && (data
!= NULL
))
1293 udc_pop_fifo(udc
, (u8
*) data
, tmp
);
1295 writel(((hwep
& 0x1E) << 1), USBD_CTRL(udc
->udp_baseaddr
));
1297 /* Clear the buffer */
1298 udc_clr_buffer_hwep(udc
, hwep
);
1303 /* Stuffs data into the FIFO, adjusts for alignment and data size */
1304 static void udc_stuff_fifo(struct lpc32xx_udc
*udc
, u8
*data
, u32 bytes
)
1308 u32
*p32
, tmp
, cbytes
;
1310 /* Use optimal data transfer method based on source address and size */
1311 switch (((u32
) data
) & 0x3) {
1312 case 0: /* 32-bit aligned */
1314 cbytes
= (bytes
& ~0x3);
1316 /* Copy 32-bit aligned data first */
1317 for (n
= 0; n
< cbytes
; n
+= 4)
1318 writel(*p32
++, USBD_TXDATA(udc
->udp_baseaddr
));
1320 /* Handle any remaining bytes */
1321 bl
= bytes
- cbytes
;
1324 for (n
= 0; n
< bl
; n
++)
1325 tmp
|= data
[cbytes
+ n
] << (n
* 8);
1327 writel(tmp
, USBD_TXDATA(udc
->udp_baseaddr
));
1331 case 1: /* 8-bit aligned */
1333 /* Each byte has to be handled independently */
1334 for (n
= 0; n
< bytes
; n
+= 4) {
1340 for (i
= 0; i
< bl
; i
++)
1341 tmp
|= data
[n
+ i
] << (i
* 8);
1343 writel(tmp
, USBD_TXDATA(udc
->udp_baseaddr
));
1347 case 2: /* 16-bit aligned */
1349 cbytes
= (bytes
& ~0x3);
1351 /* Copy 32-bit aligned data first */
1352 for (n
= 0; n
< cbytes
; n
+= 4) {
1353 tmp
= *p16
++ & 0xFFFF;
1354 tmp
|= (*p16
++ & 0xFFFF) << 16;
1355 writel(tmp
, USBD_TXDATA(udc
->udp_baseaddr
));
1358 /* Handle any remaining bytes */
1359 bl
= bytes
- cbytes
;
1362 for (n
= 0; n
< bl
; n
++)
1363 tmp
|= data
[cbytes
+ n
] << (n
* 8);
1365 writel(tmp
, USBD_TXDATA(udc
->udp_baseaddr
));
1371 /* Write data to the FIFO for an endpoint. This function is for endpoints (such
1372 * as EP0) that don't use DMA. Note that the endpoint must be selected in the
1373 * protocol engine prior to this call. */
1374 static void udc_write_hwep(struct lpc32xx_udc
*udc
, u32 hwep
, u32
*data
,
1377 u32 hwwep
= ((hwep
& 0x1E) << 1) | CTRL_WR_EN
;
1379 if ((bytes
> 0) && (data
== NULL
))
1382 /* Setup write of endpoint */
1383 writel(hwwep
, USBD_CTRL(udc
->udp_baseaddr
));
1385 writel(bytes
, USBD_TXPLEN(udc
->udp_baseaddr
));
1387 /* Need at least 1 byte to trigger TX */
1389 writel(0, USBD_TXDATA(udc
->udp_baseaddr
));
1391 udc_stuff_fifo(udc
, (u8
*) data
, bytes
);
1393 writel(((hwep
& 0x1E) << 1), USBD_CTRL(udc
->udp_baseaddr
));
1395 udc_val_buffer_hwep(udc
, hwep
);
1398 /* USB device reset - resets USB to a default state with just EP0
1400 static void uda_usb_reset(struct lpc32xx_udc
*udc
)
1403 /* Re-init device controller and EP0 */
1405 udc
->gadget
.speed
= USB_SPEED_FULL
;
1407 for (i
= 1; i
< NUM_ENDPOINTS
; i
++) {
1408 struct lpc32xx_ep
*ep
= &udc
->ep
[i
];
1409 ep
->req_pending
= 0;
1413 /* Send a ZLP on EP0 */
1414 static void udc_ep0_send_zlp(struct lpc32xx_udc
*udc
)
1416 udc_write_hwep(udc
, EP_IN
, NULL
, 0);
1419 /* Get current frame number */
1420 static u16
udc_get_current_frame(struct lpc32xx_udc
*udc
)
1424 udc_protocol_cmd_w(udc
, CMD_RD_FRAME
);
1425 flo
= (u16
) udc_protocol_cmd_r(udc
, DAT_RD_FRAME
);
1426 fhi
= (u16
) udc_protocol_cmd_r(udc
, DAT_RD_FRAME
);
1428 return (fhi
<< 8) | flo
;
1431 /* Set the device as configured - enables all endpoints */
1432 static inline void udc_set_device_configured(struct lpc32xx_udc
*udc
)
1434 udc_protocol_cmd_data_w(udc
, CMD_CFG_DEV
, DAT_WR_BYTE(CONF_DVICE
));
1437 /* Set the device as unconfigured - disables all endpoints */
1438 static inline void udc_set_device_unconfigured(struct lpc32xx_udc
*udc
)
1440 udc_protocol_cmd_data_w(udc
, CMD_CFG_DEV
, DAT_WR_BYTE(0));
1443 /* reinit == restore initial software state */
1444 static void udc_reinit(struct lpc32xx_udc
*udc
)
1448 INIT_LIST_HEAD(&udc
->gadget
.ep_list
);
1449 INIT_LIST_HEAD(&udc
->gadget
.ep0
->ep_list
);
1451 for (i
= 0; i
< NUM_ENDPOINTS
; i
++) {
1452 struct lpc32xx_ep
*ep
= &udc
->ep
[i
];
1455 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
1457 ep
->ep
.maxpacket
= ep
->maxpacket
;
1458 INIT_LIST_HEAD(&ep
->queue
);
1459 ep
->req_pending
= 0;
1462 udc
->ep0state
= WAIT_FOR_SETUP
;
1465 /* Must be called with lock */
1466 static void done(struct lpc32xx_ep
*ep
, struct lpc32xx_request
*req
, int status
)
1468 struct lpc32xx_udc
*udc
= ep
->udc
;
1470 list_del_init(&req
->queue
);
1471 if (req
->req
.status
== -EINPROGRESS
)
1472 req
->req
.status
= status
;
1474 status
= req
->req
.status
;
1477 enum dma_data_direction direction
;
1480 direction
= DMA_TO_DEVICE
;
1482 direction
= DMA_FROM_DEVICE
;
1485 dma_unmap_single(ep
->udc
->gadget
.dev
.parent
,
1486 req
->req
.dma
, req
->req
.length
,
1491 dma_sync_single_for_cpu(ep
->udc
->gadget
.dev
.parent
,
1492 req
->req
.dma
, req
->req
.length
,
1496 udc_dd_free(udc
, req
->dd_desc_ptr
);
1499 if (status
&& status
!= -ESHUTDOWN
)
1500 ep_dbg(ep
, "%s done %p, status %d\n", ep
->ep
.name
, req
, status
);
1502 ep
->req_pending
= 0;
1503 spin_unlock(&udc
->lock
);
1504 req
->req
.complete(&ep
->ep
, &req
->req
);
1505 spin_lock(&udc
->lock
);
1508 /* Must be called with lock */
1509 static void nuke(struct lpc32xx_ep
*ep
, int status
)
1511 struct lpc32xx_request
*req
;
1513 while (!list_empty(&ep
->queue
)) {
1514 req
= list_entry(ep
->queue
.next
, struct lpc32xx_request
, queue
);
1515 done(ep
, req
, status
);
1518 if (ep
->desc
&& status
== -ESHUTDOWN
) {
1519 uda_disable_hwepint(ep
->udc
, ep
->hwep_num
);
1520 udc_disable_hwep(ep
->udc
, ep
->hwep_num
);
1524 /* IN endpoint 0 transfer */
1525 static int udc_ep0_in_req(struct lpc32xx_udc
*udc
)
1527 struct lpc32xx_request
*req
;
1528 struct lpc32xx_ep
*ep0
= &udc
->ep
[0];
1531 if (list_empty(&ep0
->queue
))
1532 /* Nothing to send */
1535 req
= list_entry(ep0
->queue
.next
, struct lpc32xx_request
,
1538 tsend
= ts
= req
->req
.length
- req
->req
.actual
;
1541 udc_ep0_send_zlp(udc
);
1544 } else if (ts
> ep0
->ep
.maxpacket
)
1545 ts
= ep0
->ep
.maxpacket
; /* Just send what we can */
1547 /* Write data to the EP0 FIFO and start transfer */
1548 udc_write_hwep(udc
, EP_IN
, (req
->req
.buf
+ req
->req
.actual
), ts
);
1550 /* Increment data pointer */
1551 req
->req
.actual
+= ts
;
1553 if (tsend
>= ep0
->ep
.maxpacket
)
1554 return 0; /* Stay in data transfer state */
1556 /* Transfer request is complete */
1557 udc
->ep0state
= WAIT_FOR_SETUP
;
1562 /* OUT endpoint 0 transfer */
1563 static int udc_ep0_out_req(struct lpc32xx_udc
*udc
)
1565 struct lpc32xx_request
*req
;
1566 struct lpc32xx_ep
*ep0
= &udc
->ep
[0];
1567 u32 tr
, bufferspace
;
1569 if (list_empty(&ep0
->queue
))
1572 req
= list_entry(ep0
->queue
.next
, struct lpc32xx_request
,
1576 if (req
->req
.length
== 0) {
1577 /* Just dequeue request */
1579 udc
->ep0state
= WAIT_FOR_SETUP
;
1583 /* Get data from FIFO */
1584 bufferspace
= req
->req
.length
- req
->req
.actual
;
1585 if (bufferspace
> ep0
->ep
.maxpacket
)
1586 bufferspace
= ep0
->ep
.maxpacket
;
1588 /* Copy data to buffer */
1589 prefetchw(req
->req
.buf
+ req
->req
.actual
);
1590 tr
= udc_read_hwep(udc
, EP_OUT
, req
->req
.buf
+ req
->req
.actual
,
1592 req
->req
.actual
+= bufferspace
;
1594 if (tr
< ep0
->ep
.maxpacket
) {
1595 /* This is the last packet */
1597 udc
->ep0state
= WAIT_FOR_SETUP
;
1605 /* Must be called with lock */
1606 static void stop_activity(struct lpc32xx_udc
*udc
)
1608 struct usb_gadget_driver
*driver
= udc
->driver
;
1611 if (udc
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1614 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1617 for (i
= 0; i
< NUM_ENDPOINTS
; i
++) {
1618 struct lpc32xx_ep
*ep
= &udc
->ep
[i
];
1619 nuke(ep
, -ESHUTDOWN
);
1622 spin_unlock(&udc
->lock
);
1623 driver
->disconnect(&udc
->gadget
);
1624 spin_lock(&udc
->lock
);
1627 isp1301_pullup_enable(udc
, 0, 0);
1633 * Activate or kill host pullup
1634 * Can be called with or without lock
1636 static void pullup(struct lpc32xx_udc
*udc
, int is_on
)
1641 if (!udc
->enabled
|| !udc
->vbus
)
1644 if (is_on
!= udc
->pullup
)
1645 isp1301_pullup_enable(udc
, is_on
, 0);
1648 /* Must be called without lock */
1649 static int lpc32xx_ep_disable(struct usb_ep
*_ep
)
1651 struct lpc32xx_ep
*ep
= container_of(_ep
, struct lpc32xx_ep
, ep
);
1652 struct lpc32xx_udc
*udc
= ep
->udc
;
1653 unsigned long flags
;
1655 if ((ep
->hwep_num_base
== 0) || (ep
->hwep_num
== 0))
1657 spin_lock_irqsave(&udc
->lock
, flags
);
1659 nuke(ep
, -ESHUTDOWN
);
1661 /* restore the endpoint's pristine config */
1664 /* Clear all DMA statuses for this EP */
1665 udc_ep_dma_disable(udc
, ep
->hwep_num
);
1666 writel(1 << ep
->hwep_num
, USBD_EOTINTCLR(udc
->udp_baseaddr
));
1667 writel(1 << ep
->hwep_num
, USBD_NDDRTINTCLR(udc
->udp_baseaddr
));
1668 writel(1 << ep
->hwep_num
, USBD_SYSERRTINTCLR(udc
->udp_baseaddr
));
1669 writel(1 << ep
->hwep_num
, USBD_DMARCLR(udc
->udp_baseaddr
));
1671 /* Remove the DD pointer in the UDCA */
1672 udc
->udca_v_base
[ep
->hwep_num
] = 0;
1674 /* Disable and reset endpoint and interrupt */
1675 uda_clear_hwepint(udc
, ep
->hwep_num
);
1676 udc_unrealize_hwep(udc
, ep
->hwep_num
);
1680 spin_unlock_irqrestore(&udc
->lock
, flags
);
1682 atomic_dec(&udc
->enabled_ep_cnt
);
1683 wake_up(&udc
->ep_disable_wait_queue
);
1688 /* Must be called without lock */
1689 static int lpc32xx_ep_enable(struct usb_ep
*_ep
,
1690 const struct usb_endpoint_descriptor
*desc
)
1692 struct lpc32xx_ep
*ep
= container_of(_ep
, struct lpc32xx_ep
, ep
);
1693 struct lpc32xx_udc
*udc
= ep
->udc
;
1696 unsigned long flags
;
1698 /* Verify EP data */
1699 if ((!_ep
) || (!ep
) || (!desc
) || (ep
->desc
) ||
1700 (desc
->bDescriptorType
!= USB_DT_ENDPOINT
)) {
1701 dev_dbg(udc
->dev
, "bad ep or descriptor\n");
1704 maxpacket
= usb_endpoint_maxp(desc
);
1705 if ((maxpacket
== 0) || (maxpacket
> ep
->maxpacket
)) {
1706 dev_dbg(udc
->dev
, "bad ep descriptor's packet size\n");
1710 /* Don't touch EP0 */
1711 if (ep
->hwep_num_base
== 0) {
1712 dev_dbg(udc
->dev
, "Can't re-enable EP0!!!\n");
1716 /* Is driver ready? */
1717 if ((!udc
->driver
) || (udc
->gadget
.speed
== USB_SPEED_UNKNOWN
)) {
1718 dev_dbg(udc
->dev
, "bogus device state\n");
1722 tmp
= desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
;
1724 case USB_ENDPOINT_XFER_CONTROL
:
1727 case USB_ENDPOINT_XFER_INT
:
1728 if (maxpacket
> ep
->maxpacket
) {
1730 "Bad INT endpoint maxpacket %d\n", maxpacket
);
1735 case USB_ENDPOINT_XFER_BULK
:
1736 switch (maxpacket
) {
1745 "Bad BULK endpoint maxpacket %d\n", maxpacket
);
1750 case USB_ENDPOINT_XFER_ISOC
:
1753 spin_lock_irqsave(&udc
->lock
, flags
);
1755 /* Initialize endpoint to match the selected descriptor */
1756 ep
->is_in
= (desc
->bEndpointAddress
& USB_DIR_IN
) != 0;
1758 ep
->ep
.maxpacket
= maxpacket
;
1760 /* Map hardware endpoint from base and direction */
1762 /* IN endpoints are offset 1 from the OUT endpoint */
1763 ep
->hwep_num
= ep
->hwep_num_base
+ EP_IN
;
1765 ep
->hwep_num
= ep
->hwep_num_base
;
1767 ep_dbg(ep
, "EP enabled: %s, HW:%d, MP:%d IN:%d\n", ep
->ep
.name
,
1768 ep
->hwep_num
, maxpacket
, (ep
->is_in
== 1));
1770 /* Realize the endpoint, interrupt is enabled later when
1771 * buffers are queued, IN EPs will NAK until buffers are ready */
1772 udc_realize_hwep(udc
, ep
->hwep_num
, ep
->ep
.maxpacket
);
1773 udc_clr_buffer_hwep(udc
, ep
->hwep_num
);
1774 uda_disable_hwepint(udc
, ep
->hwep_num
);
1775 udc_clrstall_hwep(udc
, ep
->hwep_num
);
1777 /* Clear all DMA statuses for this EP */
1778 udc_ep_dma_disable(udc
, ep
->hwep_num
);
1779 writel(1 << ep
->hwep_num
, USBD_EOTINTCLR(udc
->udp_baseaddr
));
1780 writel(1 << ep
->hwep_num
, USBD_NDDRTINTCLR(udc
->udp_baseaddr
));
1781 writel(1 << ep
->hwep_num
, USBD_SYSERRTINTCLR(udc
->udp_baseaddr
));
1782 writel(1 << ep
->hwep_num
, USBD_DMARCLR(udc
->udp_baseaddr
));
1784 spin_unlock_irqrestore(&udc
->lock
, flags
);
1786 atomic_inc(&udc
->enabled_ep_cnt
);
1791 * Allocate a USB request list
1792 * Can be called with or without lock
1794 static struct usb_request
*lpc32xx_ep_alloc_request(struct usb_ep
*_ep
,
1797 struct lpc32xx_request
*req
;
1799 req
= kzalloc(sizeof(struct lpc32xx_request
), gfp_flags
);
1803 INIT_LIST_HEAD(&req
->queue
);
1808 * De-allocate a USB request list
1809 * Can be called with or without lock
1811 static void lpc32xx_ep_free_request(struct usb_ep
*_ep
,
1812 struct usb_request
*_req
)
1814 struct lpc32xx_request
*req
;
1816 req
= container_of(_req
, struct lpc32xx_request
, req
);
1817 BUG_ON(!list_empty(&req
->queue
));
1821 /* Must be called without lock */
1822 static int lpc32xx_ep_queue(struct usb_ep
*_ep
,
1823 struct usb_request
*_req
, gfp_t gfp_flags
)
1825 struct lpc32xx_request
*req
;
1826 struct lpc32xx_ep
*ep
;
1827 struct lpc32xx_udc
*udc
;
1828 unsigned long flags
;
1831 req
= container_of(_req
, struct lpc32xx_request
, req
);
1832 ep
= container_of(_ep
, struct lpc32xx_ep
, ep
);
1834 if (!_req
|| !_req
->complete
|| !_req
->buf
||
1835 !list_empty(&req
->queue
))
1840 if (!_ep
|| (!ep
->desc
&& ep
->hwep_num_base
!= 0)) {
1841 dev_dbg(udc
->dev
, "invalid ep\n");
1846 if ((!udc
) || (!udc
->driver
) ||
1847 (udc
->gadget
.speed
== USB_SPEED_UNKNOWN
)) {
1848 dev_dbg(udc
->dev
, "invalid device\n");
1853 enum dma_data_direction direction
;
1854 struct lpc32xx_usbd_dd_gad
*dd
;
1856 /* Map DMA pointer */
1858 direction
= DMA_TO_DEVICE
;
1860 direction
= DMA_FROM_DEVICE
;
1862 if (req
->req
.dma
== 0) {
1863 req
->req
.dma
= dma_map_single(
1864 ep
->udc
->gadget
.dev
.parent
,
1865 req
->req
.buf
, req
->req
.length
, direction
);
1868 dma_sync_single_for_device(
1869 ep
->udc
->gadget
.dev
.parent
, req
->req
.dma
,
1870 req
->req
.length
, direction
);
1874 /* For the request, build a list of DDs */
1875 dd
= udc_dd_alloc(udc
);
1877 /* Error allocating DD */
1880 req
->dd_desc_ptr
= dd
;
1882 /* Setup the DMA descriptor */
1883 dd
->dd_next_phy
= dd
->dd_next_v
= 0;
1884 dd
->dd_buffer_addr
= req
->req
.dma
;
1887 /* Special handling for ISO EPs */
1888 if (ep
->eptype
== EP_ISO_TYPE
) {
1889 dd
->dd_setup
= DD_SETUP_ISO_EP
|
1890 DD_SETUP_PACKETLEN(0) |
1891 DD_SETUP_DMALENBYTES(1);
1892 dd
->dd_iso_ps_mem_addr
= dd
->this_dma
+ 24;
1894 dd
->iso_status
[0] = req
->req
.length
;
1896 dd
->iso_status
[0] = 0;
1898 dd
->dd_setup
= DD_SETUP_PACKETLEN(ep
->ep
.maxpacket
) |
1899 DD_SETUP_DMALENBYTES(req
->req
.length
);
1902 ep_dbg(ep
, "%s queue req %p len %d buf %p (in=%d) z=%d\n", _ep
->name
,
1903 _req
, _req
->length
, _req
->buf
, ep
->is_in
, _req
->zero
);
1905 spin_lock_irqsave(&udc
->lock
, flags
);
1907 _req
->status
= -EINPROGRESS
;
1909 req
->send_zlp
= _req
->zero
;
1911 /* Kickstart empty queues */
1912 if (list_empty(&ep
->queue
)) {
1913 list_add_tail(&req
->queue
, &ep
->queue
);
1915 if (ep
->hwep_num_base
== 0) {
1916 /* Handle expected data direction */
1918 /* IN packet to host */
1919 udc
->ep0state
= DATA_IN
;
1920 status
= udc_ep0_in_req(udc
);
1922 /* OUT packet from host */
1923 udc
->ep0state
= DATA_OUT
;
1924 status
= udc_ep0_out_req(udc
);
1926 } else if (ep
->is_in
) {
1927 /* IN packet to host and kick off transfer */
1928 if (!ep
->req_pending
)
1929 udc_ep_in_req_dma(udc
, ep
);
1931 /* OUT packet from host and kick off list */
1932 if (!ep
->req_pending
)
1933 udc_ep_out_req_dma(udc
, ep
);
1935 list_add_tail(&req
->queue
, &ep
->queue
);
1937 spin_unlock_irqrestore(&udc
->lock
, flags
);
1939 return (status
< 0) ? status
: 0;
1942 /* Must be called without lock */
1943 static int lpc32xx_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1945 struct lpc32xx_ep
*ep
;
1946 struct lpc32xx_request
*req
;
1947 unsigned long flags
;
1949 ep
= container_of(_ep
, struct lpc32xx_ep
, ep
);
1950 if (!_ep
|| ep
->hwep_num_base
== 0)
1953 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
1955 /* make sure it's actually queued on this endpoint */
1956 list_for_each_entry(req
, &ep
->queue
, queue
) {
1957 if (&req
->req
== _req
)
1960 if (&req
->req
!= _req
) {
1961 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1965 done(ep
, req
, -ECONNRESET
);
1967 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1972 /* Must be called without lock */
1973 static int lpc32xx_ep_set_halt(struct usb_ep
*_ep
, int value
)
1975 struct lpc32xx_ep
*ep
= container_of(_ep
, struct lpc32xx_ep
, ep
);
1976 struct lpc32xx_udc
*udc
= ep
->udc
;
1977 unsigned long flags
;
1979 if ((!ep
) || (ep
->desc
== NULL
) || (ep
->hwep_num
<= 1))
1982 /* Don't halt an IN EP */
1986 spin_lock_irqsave(&udc
->lock
, flags
);
1990 udc_protocol_cmd_data_w(udc
, CMD_SET_EP_STAT(ep
->hwep_num
),
1991 DAT_WR_BYTE(EP_STAT_ST
));
1995 udc_protocol_cmd_data_w(udc
, CMD_SET_EP_STAT(ep
->hwep_num
),
1999 spin_unlock_irqrestore(&udc
->lock
, flags
);
2004 /* set the halt feature and ignores clear requests */
2005 static int lpc32xx_ep_set_wedge(struct usb_ep
*_ep
)
2007 struct lpc32xx_ep
*ep
= container_of(_ep
, struct lpc32xx_ep
, ep
);
2009 if (!_ep
|| !ep
->udc
)
2014 return usb_ep_set_halt(_ep
);
2017 static const struct usb_ep_ops lpc32xx_ep_ops
= {
2018 .enable
= lpc32xx_ep_enable
,
2019 .disable
= lpc32xx_ep_disable
,
2020 .alloc_request
= lpc32xx_ep_alloc_request
,
2021 .free_request
= lpc32xx_ep_free_request
,
2022 .queue
= lpc32xx_ep_queue
,
2023 .dequeue
= lpc32xx_ep_dequeue
,
2024 .set_halt
= lpc32xx_ep_set_halt
,
2025 .set_wedge
= lpc32xx_ep_set_wedge
,
2028 /* Send a ZLP on a non-0 IN EP */
2029 void udc_send_in_zlp(struct lpc32xx_udc
*udc
, struct lpc32xx_ep
*ep
)
2031 /* Clear EP status */
2032 udc_clearep_getsts(udc
, ep
->hwep_num
);
2034 /* Send ZLP via FIFO mechanism */
2035 udc_write_hwep(udc
, ep
->hwep_num
, NULL
, 0);
2039 * Handle EP completion for ZLP
2040 * This function will only be called when a delayed ZLP needs to be sent out
2041 * after a DMA transfer has filled both buffers.
2043 void udc_handle_eps(struct lpc32xx_udc
*udc
, struct lpc32xx_ep
*ep
)
2046 struct lpc32xx_request
*req
;
2048 if (ep
->hwep_num
<= 0)
2051 uda_clear_hwepint(udc
, ep
->hwep_num
);
2053 /* If this interrupt isn't enabled, return now */
2054 if (!(udc
->enabled_hwepints
& (1 << ep
->hwep_num
)))
2057 /* Get endpoint status */
2058 epstatus
= udc_clearep_getsts(udc
, ep
->hwep_num
);
2061 * This should never happen, but protect against writing to the
2064 if (epstatus
& EP_SEL_F
)
2068 udc_send_in_zlp(udc
, ep
);
2069 uda_disable_hwepint(udc
, ep
->hwep_num
);
2073 /* If there isn't a request waiting, something went wrong */
2074 req
= list_entry(ep
->queue
.next
, struct lpc32xx_request
, queue
);
2078 /* Start another request if ready */
2079 if (!list_empty(&ep
->queue
)) {
2081 udc_ep_in_req_dma(udc
, ep
);
2083 udc_ep_out_req_dma(udc
, ep
);
2085 ep
->req_pending
= 0;
2090 /* DMA end of transfer completion */
2091 static void udc_handle_dma_ep(struct lpc32xx_udc
*udc
, struct lpc32xx_ep
*ep
)
2093 u32 status
, epstatus
;
2094 struct lpc32xx_request
*req
;
2095 struct lpc32xx_usbd_dd_gad
*dd
;
2097 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2101 req
= list_entry(ep
->queue
.next
, struct lpc32xx_request
, queue
);
2103 ep_err(ep
, "DMA interrupt on no req!\n");
2106 dd
= req
->dd_desc_ptr
;
2108 /* DMA descriptor should always be retired for this call */
2109 if (!(dd
->dd_status
& DD_STATUS_DD_RETIRED
))
2110 ep_warn(ep
, "DMA descriptor did not retire\n");
2113 udc_ep_dma_disable(udc
, ep
->hwep_num
);
2114 writel((1 << ep
->hwep_num
), USBD_EOTINTCLR(udc
->udp_baseaddr
));
2115 writel((1 << ep
->hwep_num
), USBD_NDDRTINTCLR(udc
->udp_baseaddr
));
2118 if (readl(USBD_SYSERRTINTST(udc
->udp_baseaddr
)) &
2119 (1 << ep
->hwep_num
)) {
2120 writel((1 << ep
->hwep_num
),
2121 USBD_SYSERRTINTCLR(udc
->udp_baseaddr
));
2122 ep_err(ep
, "AHB critical error!\n");
2123 ep
->req_pending
= 0;
2125 /* The error could have occurred on a packet of a multipacket
2126 * transfer, so recovering the transfer is not possible. Close
2127 * the request with an error */
2128 done(ep
, req
, -ECONNABORTED
);
2132 /* Handle the current DD's status */
2133 status
= dd
->dd_status
;
2134 switch (status
& DD_STATUS_STS_MASK
) {
2135 case DD_STATUS_STS_NS
:
2136 /* DD not serviced? This shouldn't happen! */
2137 ep
->req_pending
= 0;
2138 ep_err(ep
, "DMA critical EP error: DD not serviced (0x%x)!\n",
2141 done(ep
, req
, -ECONNABORTED
);
2144 case DD_STATUS_STS_BS
:
2145 /* Interrupt only fires on EOT - This shouldn't happen! */
2146 ep
->req_pending
= 0;
2147 ep_err(ep
, "DMA critical EP error: EOT prior to service completion (0x%x)!\n",
2149 done(ep
, req
, -ECONNABORTED
);
2152 case DD_STATUS_STS_NC
:
2153 case DD_STATUS_STS_DUR
:
2154 /* Really just a short packet, not an underrun */
2155 /* This is a good status and what we expect */
2159 /* Data overrun, system error, or unknown */
2160 ep
->req_pending
= 0;
2161 ep_err(ep
, "DMA critical EP error: System error (0x%x)!\n",
2163 done(ep
, req
, -ECONNABORTED
);
2167 /* ISO endpoints are handled differently */
2168 if (ep
->eptype
== EP_ISO_TYPE
) {
2170 req
->req
.actual
= req
->req
.length
;
2172 req
->req
.actual
= dd
->iso_status
[0] & 0xFFFF;
2174 req
->req
.actual
+= DD_STATUS_CURDMACNT(status
);
2176 /* Send a ZLP if necessary. This will be done for non-int
2177 * packets which have a size that is a divisor of MAXP */
2178 if (req
->send_zlp
) {
2180 * If at least 1 buffer is available, send the ZLP now.
2181 * Otherwise, the ZLP send needs to be deferred until a
2182 * buffer is available.
2184 if (udc_clearep_getsts(udc
, ep
->hwep_num
) & EP_SEL_F
) {
2185 udc_clearep_getsts(udc
, ep
->hwep_num
);
2186 uda_enable_hwepint(udc
, ep
->hwep_num
);
2187 epstatus
= udc_clearep_getsts(udc
, ep
->hwep_num
);
2189 /* Let the EP interrupt handle the ZLP */
2192 udc_send_in_zlp(udc
, ep
);
2195 /* Transfer request is complete */
2198 /* Start another request if ready */
2199 udc_clearep_getsts(udc
, ep
->hwep_num
);
2200 if (!list_empty((&ep
->queue
))) {
2202 udc_ep_in_req_dma(udc
, ep
);
2204 udc_ep_out_req_dma(udc
, ep
);
2206 ep
->req_pending
= 0;
2212 * Endpoint 0 functions
2215 static void udc_handle_dev(struct lpc32xx_udc
*udc
)
2219 udc_protocol_cmd_w(udc
, CMD_GET_DEV_STAT
);
2220 tmp
= udc_protocol_cmd_r(udc
, DAT_GET_DEV_STAT
);
2224 else if (tmp
& DEV_CON_CH
)
2225 uda_power_event(udc
, (tmp
& DEV_CON
));
2226 else if (tmp
& DEV_SUS_CH
) {
2227 if (tmp
& DEV_SUS
) {
2230 else if ((udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
) &&
2232 /* Power down transceiver */
2234 schedule_work(&udc
->pullup_job
);
2235 uda_resm_susp_event(udc
, 1);
2237 } else if ((udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
) &&
2238 udc
->driver
&& udc
->vbus
) {
2239 uda_resm_susp_event(udc
, 0);
2240 /* Power up transceiver */
2242 schedule_work(&udc
->pullup_job
);
2247 static int udc_get_status(struct lpc32xx_udc
*udc
, u16 reqtype
, u16 wIndex
)
2249 struct lpc32xx_ep
*ep
;
2250 u32 ep0buff
= 0, tmp
;
2252 switch (reqtype
& USB_RECIP_MASK
) {
2253 case USB_RECIP_INTERFACE
:
2254 break; /* Not supported */
2256 case USB_RECIP_DEVICE
:
2257 ep0buff
= (udc
->selfpowered
<< USB_DEVICE_SELF_POWERED
);
2258 if (udc
->dev_status
& (1 << USB_DEVICE_REMOTE_WAKEUP
))
2259 ep0buff
|= (1 << USB_DEVICE_REMOTE_WAKEUP
);
2262 case USB_RECIP_ENDPOINT
:
2263 tmp
= wIndex
& USB_ENDPOINT_NUMBER_MASK
;
2265 if ((tmp
== 0) || (tmp
>= NUM_ENDPOINTS
) || (tmp
&& !ep
->desc
))
2268 if (wIndex
& USB_DIR_IN
) {
2270 return -EOPNOTSUPP
; /* Something's wrong */
2271 } else if (ep
->is_in
)
2272 return -EOPNOTSUPP
; /* Not an IN endpoint */
2274 /* Get status of the endpoint */
2275 udc_protocol_cmd_w(udc
, CMD_SEL_EP(ep
->hwep_num
));
2276 tmp
= udc_protocol_cmd_r(udc
, DAT_SEL_EP(ep
->hwep_num
));
2278 if (tmp
& EP_SEL_ST
)
2279 ep0buff
= (1 << USB_ENDPOINT_HALT
);
2289 udc_write_hwep(udc
, EP_IN
, &ep0buff
, 2);
2294 static void udc_handle_ep0_setup(struct lpc32xx_udc
*udc
)
2296 struct lpc32xx_ep
*ep
, *ep0
= &udc
->ep
[0];
2297 struct usb_ctrlrequest ctrlpkt
;
2299 u16 wIndex
, wValue
, wLength
, reqtype
, req
, tmp
;
2301 /* Nuke previous transfers */
2304 /* Get setup packet */
2305 bytes
= udc_read_hwep(udc
, EP_OUT
, (u32
*) &ctrlpkt
, 8);
2307 ep_warn(ep0
, "Incorrectly sized setup packet (s/b 8, is %d)!\n",
2312 /* Native endianness */
2313 wIndex
= le16_to_cpu(ctrlpkt
.wIndex
);
2314 wValue
= le16_to_cpu(ctrlpkt
.wValue
);
2315 wLength
= le16_to_cpu(ctrlpkt
.wLength
);
2316 reqtype
= le16_to_cpu(ctrlpkt
.bRequestType
);
2318 /* Set direction of EP0 */
2319 if (likely(reqtype
& USB_DIR_IN
))
2324 /* Handle SETUP packet */
2325 req
= le16_to_cpu(ctrlpkt
.bRequest
);
2327 case USB_REQ_CLEAR_FEATURE
:
2328 case USB_REQ_SET_FEATURE
:
2330 case (USB_TYPE_STANDARD
| USB_RECIP_DEVICE
):
2331 if (wValue
!= USB_DEVICE_REMOTE_WAKEUP
)
2332 goto stall
; /* Nothing else handled */
2334 /* Tell board about event */
2335 if (req
== USB_REQ_CLEAR_FEATURE
)
2337 ~(1 << USB_DEVICE_REMOTE_WAKEUP
);
2340 (1 << USB_DEVICE_REMOTE_WAKEUP
);
2341 uda_remwkp_cgh(udc
);
2344 case (USB_TYPE_STANDARD
| USB_RECIP_ENDPOINT
):
2345 tmp
= wIndex
& USB_ENDPOINT_NUMBER_MASK
;
2346 if ((wValue
!= USB_ENDPOINT_HALT
) ||
2347 (tmp
>= NUM_ENDPOINTS
))
2350 /* Find hardware endpoint from logical endpoint */
2356 if (req
== USB_REQ_SET_FEATURE
)
2357 udc_stall_hwep(udc
, tmp
);
2358 else if (!ep
->wedge
)
2359 udc_clrstall_hwep(udc
, tmp
);
2368 case USB_REQ_SET_ADDRESS
:
2369 if (reqtype
== (USB_TYPE_STANDARD
| USB_RECIP_DEVICE
)) {
2370 udc_set_address(udc
, wValue
);
2375 case USB_REQ_GET_STATUS
:
2376 udc_get_status(udc
, reqtype
, wIndex
);
2380 break; /* Let GadgetFS handle the descriptor instead */
2383 if (likely(udc
->driver
)) {
2384 /* device-2-host (IN) or no data setup command, process
2386 spin_unlock(&udc
->lock
);
2387 i
= udc
->driver
->setup(&udc
->gadget
, &ctrlpkt
);
2389 spin_lock(&udc
->lock
);
2390 if (req
== USB_REQ_SET_CONFIGURATION
) {
2391 /* Configuration is set after endpoints are realized */
2393 /* Set configuration */
2394 udc_set_device_configured(udc
);
2396 udc_protocol_cmd_data_w(udc
, CMD_SET_MODE
,
2397 DAT_WR_BYTE(AP_CLK
|
2398 INAK_BI
| INAK_II
));
2400 /* Clear configuration */
2401 udc_set_device_unconfigured(udc
);
2403 /* Disable NAK interrupts */
2404 udc_protocol_cmd_data_w(udc
, CMD_SET_MODE
,
2405 DAT_WR_BYTE(AP_CLK
));
2410 /* setup processing failed, force stall */
2412 "req %02x.%02x protocol STALL; stat %d\n",
2414 udc
->ep0state
= WAIT_FOR_SETUP
;
2420 udc_ep0_send_zlp(udc
); /* ZLP IN packet on data phase */
2425 udc_stall_hwep(udc
, EP_IN
);
2429 udc_ep0_send_zlp(udc
);
2433 /* IN endpoint 0 transfer */
2434 static void udc_handle_ep0_in(struct lpc32xx_udc
*udc
)
2436 struct lpc32xx_ep
*ep0
= &udc
->ep
[0];
2439 /* Clear EP interrupt */
2440 epstatus
= udc_clearep_getsts(udc
, EP_IN
);
2442 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2446 /* Stalled? Clear stall and reset buffers */
2447 if (epstatus
& EP_SEL_ST
) {
2448 udc_clrstall_hwep(udc
, EP_IN
);
2449 nuke(ep0
, -ECONNABORTED
);
2450 udc
->ep0state
= WAIT_FOR_SETUP
;
2454 /* Is a buffer available? */
2455 if (!(epstatus
& EP_SEL_F
)) {
2456 /* Handle based on current state */
2457 if (udc
->ep0state
== DATA_IN
)
2458 udc_ep0_in_req(udc
);
2460 /* Unknown state for EP0 oe end of DATA IN phase */
2461 nuke(ep0
, -ECONNABORTED
);
2462 udc
->ep0state
= WAIT_FOR_SETUP
;
2467 /* OUT endpoint 0 transfer */
2468 static void udc_handle_ep0_out(struct lpc32xx_udc
*udc
)
2470 struct lpc32xx_ep
*ep0
= &udc
->ep
[0];
2473 /* Clear EP interrupt */
2474 epstatus
= udc_clearep_getsts(udc
, EP_OUT
);
2477 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2482 if (epstatus
& EP_SEL_ST
) {
2483 udc_clrstall_hwep(udc
, EP_OUT
);
2484 nuke(ep0
, -ECONNABORTED
);
2485 udc
->ep0state
= WAIT_FOR_SETUP
;
2489 /* A NAK may occur if a packet couldn't be received yet */
2490 if (epstatus
& EP_SEL_EPN
)
2492 /* Setup packet incoming? */
2493 if (epstatus
& EP_SEL_STP
) {
2495 udc
->ep0state
= WAIT_FOR_SETUP
;
2498 /* Data available? */
2499 if (epstatus
& EP_SEL_F
)
2500 /* Handle based on current state */
2501 switch (udc
->ep0state
) {
2502 case WAIT_FOR_SETUP
:
2503 udc_handle_ep0_setup(udc
);
2507 udc_ep0_out_req(udc
);
2511 /* Unknown state for EP0 */
2512 nuke(ep0
, -ECONNABORTED
);
2513 udc
->ep0state
= WAIT_FOR_SETUP
;
2517 /* Must be called without lock */
2518 static int lpc32xx_get_frame(struct usb_gadget
*gadget
)
2521 unsigned long flags
;
2522 struct lpc32xx_udc
*udc
= to_udc(gadget
);
2527 spin_lock_irqsave(&udc
->lock
, flags
);
2529 frame
= (int) udc_get_current_frame(udc
);
2531 spin_unlock_irqrestore(&udc
->lock
, flags
);
2536 static int lpc32xx_wakeup(struct usb_gadget
*gadget
)
2541 static int lpc32xx_set_selfpowered(struct usb_gadget
*gadget
, int is_on
)
2543 struct lpc32xx_udc
*udc
= to_udc(gadget
);
2545 /* Always self-powered */
2546 udc
->selfpowered
= (is_on
!= 0);
2552 * vbus is here! turn everything on that's ready
2553 * Must be called without lock
2555 static int lpc32xx_vbus_session(struct usb_gadget
*gadget
, int is_active
)
2557 unsigned long flags
;
2558 struct lpc32xx_udc
*udc
= to_udc(gadget
);
2560 spin_lock_irqsave(&udc
->lock
, flags
);
2562 /* Doesn't need lock */
2564 udc_clk_set(udc
, 1);
2566 pullup(udc
, is_active
);
2571 spin_unlock_irqrestore(&udc
->lock
, flags
);
2573 * Wait for all the endpoints to disable,
2574 * before disabling clocks. Don't wait if
2575 * endpoints are not enabled.
2577 if (atomic_read(&udc
->enabled_ep_cnt
))
2578 wait_event_interruptible(udc
->ep_disable_wait_queue
,
2579 (atomic_read(&udc
->enabled_ep_cnt
) == 0));
2581 spin_lock_irqsave(&udc
->lock
, flags
);
2583 udc_clk_set(udc
, 0);
2586 spin_unlock_irqrestore(&udc
->lock
, flags
);
2591 /* Can be called with or without lock */
2592 static int lpc32xx_pullup(struct usb_gadget
*gadget
, int is_on
)
2594 struct lpc32xx_udc
*udc
= to_udc(gadget
);
2596 /* Doesn't need lock */
2602 static int lpc32xx_start(struct usb_gadget_driver
*driver
,
2603 int (*bind
)(struct usb_gadget
*));
2604 static int lpc32xx_stop(struct usb_gadget_driver
*driver
);
2606 static const struct usb_gadget_ops lpc32xx_udc_ops
= {
2607 .get_frame
= lpc32xx_get_frame
,
2608 .wakeup
= lpc32xx_wakeup
,
2609 .set_selfpowered
= lpc32xx_set_selfpowered
,
2610 .vbus_session
= lpc32xx_vbus_session
,
2611 .pullup
= lpc32xx_pullup
,
2612 .start
= lpc32xx_start
,
2613 .stop
= lpc32xx_stop
,
2616 static void nop_release(struct device
*dev
)
2618 /* nothing to free */
2621 static struct lpc32xx_udc controller
= {
2623 .ops
= &lpc32xx_udc_ops
,
2624 .ep0
= &controller
.ep
[0].ep
,
2625 .name
= driver_name
,
2627 .init_name
= "gadget",
2628 .release
= nop_release
,
2634 .ops
= &lpc32xx_ep_ops
,
2639 .hwep_num
= 0, /* Can be 0 or 1, has special handling */
2641 .eptype
= EP_CTL_TYPE
,
2646 .ops
= &lpc32xx_ep_ops
,
2651 .hwep_num
= 0, /* 2 or 3, will be set later */
2653 .eptype
= EP_INT_TYPE
,
2658 .ops
= &lpc32xx_ep_ops
,
2663 .hwep_num
= 0, /* 4 or 5, will be set later */
2665 .eptype
= EP_BLK_TYPE
,
2670 .ops
= &lpc32xx_ep_ops
,
2675 .hwep_num
= 0, /* 6 or 7, will be set later */
2677 .eptype
= EP_ISO_TYPE
,
2682 .ops
= &lpc32xx_ep_ops
,
2687 .hwep_num
= 0, /* 8 or 9, will be set later */
2689 .eptype
= EP_INT_TYPE
,
2694 .ops
= &lpc32xx_ep_ops
,
2698 .hwep_num_base
= 10,
2699 .hwep_num
= 0, /* 10 or 11, will be set later */
2701 .eptype
= EP_BLK_TYPE
,
2706 .ops
= &lpc32xx_ep_ops
,
2710 .hwep_num_base
= 12,
2711 .hwep_num
= 0, /* 12 or 13, will be set later */
2713 .eptype
= EP_ISO_TYPE
,
2718 .ops
= &lpc32xx_ep_ops
,
2722 .hwep_num_base
= 14,
2725 .eptype
= EP_INT_TYPE
,
2730 .ops
= &lpc32xx_ep_ops
,
2734 .hwep_num_base
= 16,
2737 .eptype
= EP_BLK_TYPE
,
2742 .ops
= &lpc32xx_ep_ops
,
2746 .hwep_num_base
= 18,
2749 .eptype
= EP_ISO_TYPE
,
2754 .ops
= &lpc32xx_ep_ops
,
2758 .hwep_num_base
= 20,
2761 .eptype
= EP_INT_TYPE
,
2765 .name
= "ep11-bulk",
2766 .ops
= &lpc32xx_ep_ops
,
2770 .hwep_num_base
= 22,
2773 .eptype
= EP_BLK_TYPE
,
2778 .ops
= &lpc32xx_ep_ops
,
2782 .hwep_num_base
= 24,
2785 .eptype
= EP_ISO_TYPE
,
2790 .ops
= &lpc32xx_ep_ops
,
2794 .hwep_num_base
= 26,
2797 .eptype
= EP_INT_TYPE
,
2801 .name
= "ep14-bulk",
2802 .ops
= &lpc32xx_ep_ops
,
2806 .hwep_num_base
= 28,
2809 .eptype
= EP_BLK_TYPE
,
2813 .name
= "ep15-bulk",
2814 .ops
= &lpc32xx_ep_ops
,
2818 .hwep_num_base
= 30,
2821 .eptype
= EP_BLK_TYPE
,
2825 /* ISO and status interrupts */
2826 static irqreturn_t
lpc32xx_usb_lp_irq(int irq
, void *_udc
)
2829 struct lpc32xx_udc
*udc
= _udc
;
2831 spin_lock(&udc
->lock
);
2833 /* Read the device status register */
2834 devstat
= readl(USBD_DEVINTST(udc
->udp_baseaddr
));
2836 devstat
&= ~USBD_EP_FAST
;
2837 writel(devstat
, USBD_DEVINTCLR(udc
->udp_baseaddr
));
2838 devstat
= devstat
& udc
->enabled_devints
;
2840 /* Device specific handling needed? */
2841 if (devstat
& USBD_DEV_STAT
)
2842 udc_handle_dev(udc
);
2844 /* Start of frame? (devstat & FRAME_INT):
2845 * The frame interrupt isn't really needed for ISO support,
2846 * as the driver will queue the necessary packets */
2849 if (devstat
& ERR_INT
) {
2850 /* All types of errors, from cable removal during transfer to
2851 * misc protocol and bit errors. These are mostly for just info,
2852 * as the USB hardware will work around these. If these errors
2853 * happen alot, something is wrong. */
2854 udc_protocol_cmd_w(udc
, CMD_RD_ERR_STAT
);
2855 tmp
= udc_protocol_cmd_r(udc
, DAT_RD_ERR_STAT
);
2856 dev_dbg(udc
->dev
, "Device error (0x%x)!\n", tmp
);
2859 spin_unlock(&udc
->lock
);
2865 static irqreturn_t
lpc32xx_usb_hp_irq(int irq
, void *_udc
)
2868 struct lpc32xx_udc
*udc
= _udc
;
2870 spin_lock(&udc
->lock
);
2872 /* Read the device status register */
2873 writel(USBD_EP_FAST
, USBD_DEVINTCLR(udc
->udp_baseaddr
));
2876 tmp
= readl(USBD_EPINTST(udc
->udp_baseaddr
));
2878 /* Special handling for EP0 */
2879 if (tmp
& (EP_MASK_SEL(0, EP_OUT
) | EP_MASK_SEL(0, EP_IN
))) {
2881 if (tmp
& (EP_MASK_SEL(0, EP_IN
)))
2882 udc_handle_ep0_in(udc
);
2884 /* Handle EP0 OUT */
2885 if (tmp
& (EP_MASK_SEL(0, EP_OUT
)))
2886 udc_handle_ep0_out(udc
);
2890 if (tmp
& ~(EP_MASK_SEL(0, EP_OUT
) | EP_MASK_SEL(0, EP_IN
))) {
2893 /* Handle other EP interrupts */
2894 for (i
= 1; i
< NUM_ENDPOINTS
; i
++) {
2895 if (tmp
& (1 << udc
->ep
[i
].hwep_num
))
2896 udc_handle_eps(udc
, &udc
->ep
[i
]);
2900 spin_unlock(&udc
->lock
);
2905 static irqreturn_t
lpc32xx_usb_devdma_irq(int irq
, void *_udc
)
2907 struct lpc32xx_udc
*udc
= _udc
;
2912 spin_lock(&udc
->lock
);
2914 /* Handle EP DMA EOT interrupts */
2915 tmp
= readl(USBD_EOTINTST(udc
->udp_baseaddr
)) |
2916 (readl(USBD_EPDMAST(udc
->udp_baseaddr
)) &
2917 readl(USBD_NDDRTINTST(udc
->udp_baseaddr
))) |
2918 readl(USBD_SYSERRTINTST(udc
->udp_baseaddr
));
2919 for (i
= 1; i
< NUM_ENDPOINTS
; i
++) {
2920 if (tmp
& (1 << udc
->ep
[i
].hwep_num
))
2921 udc_handle_dma_ep(udc
, &udc
->ep
[i
]);
2924 spin_unlock(&udc
->lock
);
2931 * VBUS detection, pullup handler, and Gadget cable state notification
2934 static void vbus_work(struct work_struct
*work
)
2937 struct lpc32xx_udc
*udc
= container_of(work
, struct lpc32xx_udc
,
2940 if (udc
->enabled
!= 0) {
2941 /* Discharge VBUS real quick */
2942 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
2943 ISP1301_I2C_OTG_CONTROL_1
, OTG1_VBUS_DISCHRG
);
2945 /* Give VBUS some time (100mS) to discharge */
2948 /* Disable VBUS discharge resistor */
2949 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
2950 ISP1301_I2C_OTG_CONTROL_1
| ISP1301_I2C_REG_CLEAR_ADDR
,
2953 /* Clear interrupt */
2954 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
2955 ISP1301_I2C_INTERRUPT_LATCH
|
2956 ISP1301_I2C_REG_CLEAR_ADDR
, ~0);
2958 /* Get the VBUS status from the transceiver */
2959 value
= i2c_smbus_read_byte_data(udc
->isp1301_i2c_client
,
2960 ISP1301_I2C_OTG_CONTROL_2
);
2962 /* VBUS on or off? */
2963 if (value
& OTG_B_SESS_VLD
)
2969 if (udc
->last_vbus
!= udc
->vbus
) {
2970 udc
->last_vbus
= udc
->vbus
;
2971 lpc32xx_vbus_session(&udc
->gadget
, udc
->vbus
);
2975 /* Re-enable after completion */
2976 enable_irq(udc
->udp_irq
[IRQ_USB_ATX
]);
2979 static irqreturn_t
lpc32xx_usb_vbus_irq(int irq
, void *_udc
)
2981 struct lpc32xx_udc
*udc
= _udc
;
2983 /* Defer handling of VBUS IRQ to work queue */
2984 disable_irq_nosync(udc
->udp_irq
[IRQ_USB_ATX
]);
2985 schedule_work(&udc
->vbus_job
);
2990 static int lpc32xx_start(struct usb_gadget_driver
*driver
,
2991 int (*bind
)(struct usb_gadget
*))
2993 struct lpc32xx_udc
*udc
= &controller
;
2996 if (!driver
|| driver
->max_speed
< USB_SPEED_FULL
||
2997 !bind
|| !driver
->setup
) {
2998 dev_err(udc
->dev
, "bad parameter.\n");
3003 dev_err(udc
->dev
, "UDC already has a gadget driver\n");
3007 udc
->driver
= driver
;
3008 udc
->gadget
.dev
.driver
= &driver
->driver
;
3009 udc
->gadget
.dev
.of_node
= udc
->dev
->of_node
;
3011 udc
->selfpowered
= 1;
3014 retval
= bind(&udc
->gadget
);
3016 dev_err(udc
->dev
, "bind() returned %d\n", retval
);
3018 udc
->selfpowered
= 0;
3020 udc
->gadget
.dev
.driver
= NULL
;
3024 dev_dbg(udc
->dev
, "bound to %s\n", driver
->driver
.name
);
3026 /* Force VBUS process once to check for cable insertion */
3027 udc
->last_vbus
= udc
->vbus
= 0;
3028 schedule_work(&udc
->vbus_job
);
3030 /* Do not re-enable ATX IRQ (3) */
3031 for (i
= IRQ_USB_LP
; i
< IRQ_USB_ATX
; i
++)
3032 enable_irq(udc
->udp_irq
[i
]);
3037 static int lpc32xx_stop(struct usb_gadget_driver
*driver
)
3040 struct lpc32xx_udc
*udc
= &controller
;
3042 if (!driver
|| driver
!= udc
->driver
|| !driver
->unbind
)
3045 /* Disable USB pullup */
3046 isp1301_pullup_enable(udc
, 0, 1);
3048 for (i
= IRQ_USB_LP
; i
<= IRQ_USB_ATX
; i
++)
3049 disable_irq(udc
->udp_irq
[i
]);
3053 spin_lock(&udc
->lock
);
3055 spin_unlock(&udc
->lock
);
3058 * Wait for all the endpoints to disable,
3059 * before disabling clocks. Don't wait if
3060 * endpoints are not enabled.
3062 if (atomic_read(&udc
->enabled_ep_cnt
))
3063 wait_event_interruptible(udc
->ep_disable_wait_queue
,
3064 (atomic_read(&udc
->enabled_ep_cnt
) == 0));
3066 spin_lock(&udc
->lock
);
3067 udc_clk_set(udc
, 0);
3068 spin_unlock(&udc
->lock
);
3074 driver
->unbind(&udc
->gadget
);
3075 udc
->gadget
.dev
.driver
= NULL
;
3078 dev_dbg(udc
->dev
, "unbound from %s\n", driver
->driver
.name
);
3082 static void lpc32xx_udc_shutdown(struct platform_device
*dev
)
3084 /* Force disconnect on reboot */
3085 struct lpc32xx_udc
*udc
= &controller
;
3091 * Callbacks to be overridden by options passed via OF (TODO)
3094 static void lpc32xx_usbd_conn_chg(int conn
)
3096 /* Do nothing, it might be nice to enable an LED
3097 * based on conn state being !0 */
3100 static void lpc32xx_usbd_susp_chg(int susp
)
3102 /* Device suspend if susp != 0 */
3105 static void lpc32xx_rmwkup_chg(int remote_wakup_enable
)
3107 /* Enable or disable USB remote wakeup */
3110 struct lpc32xx_usbd_cfg lpc32xx_usbddata
= {
3112 .conn_chgb
= &lpc32xx_usbd_conn_chg
,
3113 .susp_chgb
= &lpc32xx_usbd_susp_chg
,
3114 .rmwk_chgb
= &lpc32xx_rmwkup_chg
,
3118 static u64 lpc32xx_usbd_dmamask
= ~(u32
) 0x7F;
3120 static int __init
lpc32xx_udc_probe(struct platform_device
*pdev
)
3122 struct device
*dev
= &pdev
->dev
;
3123 struct lpc32xx_udc
*udc
= &controller
;
3125 struct resource
*res
;
3126 dma_addr_t dma_handle
;
3127 struct device_node
*isp1301_node
;
3129 /* init software state */
3130 udc
->gadget
.dev
.parent
= dev
;
3132 udc
->dev
= &pdev
->dev
;
3135 if (pdev
->dev
.of_node
) {
3136 isp1301_node
= of_parse_phandle(pdev
->dev
.of_node
,
3139 isp1301_node
= NULL
;
3142 udc
->isp1301_i2c_client
= isp1301_get_client(isp1301_node
);
3143 if (!udc
->isp1301_i2c_client
)
3144 return -EPROBE_DEFER
;
3146 dev_info(udc
->dev
, "ISP1301 I2C device at address 0x%x\n",
3147 udc
->isp1301_i2c_client
->addr
);
3149 pdev
->dev
.dma_mask
= &lpc32xx_usbd_dmamask
;
3150 pdev
->dev
.coherent_dma_mask
= DMA_BIT_MASK(32);
3152 udc
->board
= &lpc32xx_usbddata
;
3155 * Resources are mapped as follows:
3156 * IORESOURCE_MEM, base address and size of USB space
3157 * IORESOURCE_IRQ, USB device low priority interrupt number
3158 * IORESOURCE_IRQ, USB device high priority interrupt number
3159 * IORESOURCE_IRQ, USB device interrupt number
3160 * IORESOURCE_IRQ, USB transceiver interrupt number
3162 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
3166 spin_lock_init(&udc
->lock
);
3169 for (i
= 0; i
< 4; i
++) {
3170 udc
->udp_irq
[i
] = platform_get_irq(pdev
, i
);
3171 if (udc
->udp_irq
[i
] < 0) {
3173 "irq resource %d not available!\n", i
);
3174 return udc
->udp_irq
[i
];
3178 udc
->io_p_start
= res
->start
;
3179 udc
->io_p_size
= resource_size(res
);
3180 if (!request_mem_region(udc
->io_p_start
, udc
->io_p_size
, driver_name
)) {
3181 dev_err(udc
->dev
, "someone's using UDC memory\n");
3185 udc
->udp_baseaddr
= ioremap(udc
->io_p_start
, udc
->io_p_size
);
3186 if (!udc
->udp_baseaddr
) {
3188 dev_err(udc
->dev
, "IO map failure\n");
3192 /* Enable AHB slave USB clock, needed for further USB clock control */
3193 writel(USB_SLAVE_HCLK_EN
| (1 << 19), USB_CTRL
);
3195 /* Get required clocks */
3196 udc
->usb_pll_clk
= clk_get(&pdev
->dev
, "ck_pll5");
3197 if (IS_ERR(udc
->usb_pll_clk
)) {
3198 dev_err(udc
->dev
, "failed to acquire USB PLL\n");
3199 retval
= PTR_ERR(udc
->usb_pll_clk
);
3202 udc
->usb_slv_clk
= clk_get(&pdev
->dev
, "ck_usbd");
3203 if (IS_ERR(udc
->usb_slv_clk
)) {
3204 dev_err(udc
->dev
, "failed to acquire USB device clock\n");
3205 retval
= PTR_ERR(udc
->usb_slv_clk
);
3206 goto usb_clk_get_fail
;
3208 udc
->usb_otg_clk
= clk_get(&pdev
->dev
, "ck_usb_otg");
3209 if (IS_ERR(udc
->usb_otg_clk
)) {
3210 dev_err(udc
->dev
, "failed to acquire USB otg clock\n");
3211 retval
= PTR_ERR(udc
->usb_otg_clk
);
3212 goto usb_otg_clk_get_fail
;
3215 /* Setup PLL clock to 48MHz */
3216 retval
= clk_enable(udc
->usb_pll_clk
);
3218 dev_err(udc
->dev
, "failed to start USB PLL\n");
3219 goto pll_enable_fail
;
3222 retval
= clk_set_rate(udc
->usb_pll_clk
, 48000);
3224 dev_err(udc
->dev
, "failed to set USB clock rate\n");
3228 writel(readl(USB_CTRL
) | USB_DEV_NEED_CLK_EN
, USB_CTRL
);
3230 /* Enable USB device clock */
3231 retval
= clk_enable(udc
->usb_slv_clk
);
3233 dev_err(udc
->dev
, "failed to start USB device clock\n");
3234 goto usb_clk_enable_fail
;
3237 /* Enable USB OTG clock */
3238 retval
= clk_enable(udc
->usb_otg_clk
);
3240 dev_err(udc
->dev
, "failed to start USB otg clock\n");
3241 goto usb_otg_clk_enable_fail
;
3244 /* Setup deferred workqueue data */
3245 udc
->poweron
= udc
->pullup
= 0;
3246 INIT_WORK(&udc
->pullup_job
, pullup_work
);
3247 INIT_WORK(&udc
->vbus_job
, vbus_work
);
3249 INIT_WORK(&udc
->power_job
, power_work
);
3252 /* All clocks are now on */
3255 isp1301_udc_configure(udc
);
3256 /* Allocate memory for the UDCA */
3257 udc
->udca_v_base
= dma_alloc_coherent(&pdev
->dev
, UDCA_BUFF_SIZE
,
3259 (GFP_KERNEL
| GFP_DMA
));
3260 if (!udc
->udca_v_base
) {
3261 dev_err(udc
->dev
, "error getting UDCA region\n");
3265 udc
->udca_p_base
= dma_handle
;
3266 dev_dbg(udc
->dev
, "DMA buffer(0x%x bytes), P:0x%08x, V:0x%p\n",
3267 UDCA_BUFF_SIZE
, udc
->udca_p_base
, udc
->udca_v_base
);
3269 /* Setup the DD DMA memory pool */
3270 udc
->dd_cache
= dma_pool_create("udc_dd", udc
->dev
,
3271 sizeof(struct lpc32xx_usbd_dd_gad
),
3273 if (!udc
->dd_cache
) {
3274 dev_err(udc
->dev
, "error getting DD DMA region\n");
3276 goto dma_alloc_fail
;
3279 /* Clear USB peripheral and initialize gadget endpoints */
3283 retval
= device_register(&udc
->gadget
.dev
);
3285 dev_err(udc
->dev
, "Device registration failure\n");
3286 goto dev_register_fail
;
3289 /* Request IRQs - low and high priority USB device IRQs are routed to
3290 * the same handler, while the DMA interrupt is routed elsewhere */
3291 retval
= request_irq(udc
->udp_irq
[IRQ_USB_LP
], lpc32xx_usb_lp_irq
,
3294 dev_err(udc
->dev
, "LP request irq %d failed\n",
3295 udc
->udp_irq
[IRQ_USB_LP
]);
3298 retval
= request_irq(udc
->udp_irq
[IRQ_USB_HP
], lpc32xx_usb_hp_irq
,
3301 dev_err(udc
->dev
, "HP request irq %d failed\n",
3302 udc
->udp_irq
[IRQ_USB_HP
]);
3306 retval
= request_irq(udc
->udp_irq
[IRQ_USB_DEVDMA
],
3307 lpc32xx_usb_devdma_irq
, 0, "udc_dma", udc
);
3309 dev_err(udc
->dev
, "DEV request irq %d failed\n",
3310 udc
->udp_irq
[IRQ_USB_DEVDMA
]);
3314 /* The transceiver interrupt is used for VBUS detection and will
3315 kick off the VBUS handler function */
3316 retval
= request_irq(udc
->udp_irq
[IRQ_USB_ATX
], lpc32xx_usb_vbus_irq
,
3319 dev_err(udc
->dev
, "VBUS request irq %d failed\n",
3320 udc
->udp_irq
[IRQ_USB_ATX
]);
3324 /* Initialize wait queue */
3325 init_waitqueue_head(&udc
->ep_disable_wait_queue
);
3326 atomic_set(&udc
->enabled_ep_cnt
, 0);
3328 /* Keep all IRQs disabled until GadgetFS starts up */
3329 for (i
= IRQ_USB_LP
; i
<= IRQ_USB_ATX
; i
++)
3330 disable_irq(udc
->udp_irq
[i
]);
3332 retval
= usb_add_gadget_udc(dev
, &udc
->gadget
);
3334 goto add_gadget_fail
;
3336 dev_set_drvdata(dev
, udc
);
3337 device_init_wakeup(dev
, 1);
3338 create_debug_file(udc
);
3340 /* Disable clocks for now */
3341 udc_clk_set(udc
, 0);
3343 dev_info(udc
->dev
, "%s version %s\n", driver_name
, DRIVER_VERSION
);
3347 free_irq(udc
->udp_irq
[IRQ_USB_ATX
], udc
);
3349 free_irq(udc
->udp_irq
[IRQ_USB_DEVDMA
], udc
);
3351 free_irq(udc
->udp_irq
[IRQ_USB_HP
], udc
);
3353 free_irq(udc
->udp_irq
[IRQ_USB_LP
], udc
);
3355 device_unregister(&udc
->gadget
.dev
);
3357 dma_pool_destroy(udc
->dd_cache
);
3359 dma_free_coherent(&pdev
->dev
, UDCA_BUFF_SIZE
,
3360 udc
->udca_v_base
, udc
->udca_p_base
);
3362 clk_disable(udc
->usb_otg_clk
);
3363 usb_otg_clk_enable_fail
:
3364 clk_disable(udc
->usb_slv_clk
);
3365 usb_clk_enable_fail
:
3367 clk_disable(udc
->usb_pll_clk
);
3369 clk_put(udc
->usb_slv_clk
);
3370 usb_otg_clk_get_fail
:
3371 clk_put(udc
->usb_otg_clk
);
3373 clk_put(udc
->usb_pll_clk
);
3375 iounmap(udc
->udp_baseaddr
);
3377 release_mem_region(udc
->io_p_start
, udc
->io_p_size
);
3378 dev_err(udc
->dev
, "%s probe failed, %d\n", driver_name
, retval
);
3383 static int __devexit
lpc32xx_udc_remove(struct platform_device
*pdev
)
3385 struct lpc32xx_udc
*udc
= platform_get_drvdata(pdev
);
3387 usb_del_gadget_udc(&udc
->gadget
);
3391 udc_clk_set(udc
, 1);
3395 free_irq(udc
->udp_irq
[IRQ_USB_ATX
], udc
);
3397 device_init_wakeup(&pdev
->dev
, 0);
3398 remove_debug_file(udc
);
3400 dma_pool_destroy(udc
->dd_cache
);
3401 dma_free_coherent(&pdev
->dev
, UDCA_BUFF_SIZE
,
3402 udc
->udca_v_base
, udc
->udca_p_base
);
3403 free_irq(udc
->udp_irq
[IRQ_USB_DEVDMA
], udc
);
3404 free_irq(udc
->udp_irq
[IRQ_USB_HP
], udc
);
3405 free_irq(udc
->udp_irq
[IRQ_USB_LP
], udc
);
3407 device_unregister(&udc
->gadget
.dev
);
3409 clk_disable(udc
->usb_otg_clk
);
3410 clk_put(udc
->usb_otg_clk
);
3411 clk_disable(udc
->usb_slv_clk
);
3412 clk_put(udc
->usb_slv_clk
);
3413 clk_disable(udc
->usb_pll_clk
);
3414 clk_put(udc
->usb_pll_clk
);
3415 iounmap(udc
->udp_baseaddr
);
3416 release_mem_region(udc
->io_p_start
, udc
->io_p_size
);
3422 static int lpc32xx_udc_suspend(struct platform_device
*pdev
, pm_message_t mesg
)
3424 struct lpc32xx_udc
*udc
= platform_get_drvdata(pdev
);
3427 /* Power down ISP */
3429 isp1301_set_powerstate(udc
, 0);
3431 /* Disable clocking */
3432 udc_clk_set(udc
, 0);
3434 /* Keep clock flag on, so we know to re-enable clocks
3438 /* Kill global USB clock */
3439 clk_disable(udc
->usb_slv_clk
);
3445 static int lpc32xx_udc_resume(struct platform_device
*pdev
)
3447 struct lpc32xx_udc
*udc
= platform_get_drvdata(pdev
);
3450 /* Enable global USB clock */
3451 clk_enable(udc
->usb_slv_clk
);
3453 /* Enable clocking */
3454 udc_clk_set(udc
, 1);
3456 /* ISP back to normal power mode */
3458 isp1301_set_powerstate(udc
, 1);
3464 #define lpc32xx_udc_suspend NULL
3465 #define lpc32xx_udc_resume NULL
3469 static struct of_device_id lpc32xx_udc_of_match
[] = {
3470 { .compatible
= "nxp,lpc3220-udc", },
3473 MODULE_DEVICE_TABLE(of
, lpc32xx_udc_of_match
);
3476 static struct platform_driver lpc32xx_udc_driver
= {
3477 .remove
= __devexit_p(lpc32xx_udc_remove
),
3478 .shutdown
= lpc32xx_udc_shutdown
,
3479 .suspend
= lpc32xx_udc_suspend
,
3480 .resume
= lpc32xx_udc_resume
,
3482 .name
= (char *) driver_name
,
3483 .owner
= THIS_MODULE
,
3484 .of_match_table
= of_match_ptr(lpc32xx_udc_of_match
),
3488 static int __init
udc_init_module(void)
3490 return platform_driver_probe(&lpc32xx_udc_driver
, lpc32xx_udc_probe
);
3492 module_init(udc_init_module
);
3494 static void __exit
udc_exit_module(void)
3496 platform_driver_unregister(&lpc32xx_udc_driver
);
3498 module_exit(udc_exit_module
);
3500 MODULE_DESCRIPTION("LPC32XX udc driver");
3501 MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>");
3502 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
3503 MODULE_LICENSE("GPL");
3504 MODULE_ALIAS("platform:lpc32xx_udc");