1 // SPDX-License-Identifier: GPL-2.0+
3 * USB Gadget driver for LPC32xx
6 * Kevin Wells <kevin.wells@nxp.com>
8 * Roland Stigge <stigge@antcom.de>
10 * Copyright (C) 2006 Philips Semiconductors
11 * Copyright (C) 2009 NXP Semiconductors
12 * Copyright (C) 2012 Roland Stigge
14 * Note: This driver is based on original work done by Mike James for
18 #include <linux/clk.h>
19 #include <linux/delay.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/dmapool.h>
22 #include <linux/i2c.h>
23 #include <linux/interrupt.h>
24 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/prefetch.h>
28 #include <linux/proc_fs.h>
29 #include <linux/slab.h>
30 #include <linux/usb/ch9.h>
31 #include <linux/usb/gadget.h>
32 #include <linux/usb/isp1301.h>
34 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
35 #include <linux/debugfs.h>
36 #include <linux/seq_file.h>
40 * USB device configuration structure
42 typedef void (*usc_chg_event
)(int);
43 struct lpc32xx_usbd_cfg
{
44 int vbus_drv_pol
; /* 0=active low drive for VBUS via ISP1301 */
45 usc_chg_event conn_chgb
; /* Connection change event (optional) */
46 usc_chg_event susp_chgb
; /* Suspend/resume event (optional) */
47 usc_chg_event rmwk_chgb
; /* Enable/disable remote wakeup */
51 * controller driver data structures
54 /* 16 endpoints (not to be confused with 32 hardware endpoints) */
55 #define NUM_ENDPOINTS 16
58 * IRQ indices make reading the code a little easier
62 #define IRQ_USB_DEVDMA 2
65 #define EP_OUT 0 /* RX (from host) */
66 #define EP_IN 1 /* TX (to host) */
68 /* Returns the interrupt mask for the selected hardware endpoint */
69 #define EP_MASK_SEL(ep, dir) (1 << (((ep) * 2) + dir))
77 #define WAIT_FOR_SETUP 0 /* Wait for setup packet */
78 #define DATA_IN 1 /* Expect dev->host transfer */
79 #define DATA_OUT 2 /* Expect host->dev transfer */
81 /* DD (DMA Descriptor) structure, requires word alignment, this is already
82 * defined in the LPC32XX USB device header file, but this version is slightly
83 * modified to tag some work data with each DMA descriptor. */
84 struct lpc32xx_usbd_dd_gad
{
89 u32 dd_iso_ps_mem_addr
;
91 u32 iso_status
[6]; /* 5 spare */
96 * Logical endpoint structure
100 struct list_head queue
;
101 struct lpc32xx_udc
*udc
;
103 u32 hwep_num_base
; /* Physical hardware EP */
104 u32 hwep_num
; /* Maps to hardware endpoint */
123 * Common UDC structure
126 struct usb_gadget gadget
;
127 struct usb_gadget_driver
*driver
;
128 struct platform_device
*pdev
;
132 struct i2c_client
*isp1301_i2c_client
;
134 /* Board and device specific */
135 struct lpc32xx_usbd_cfg
*board
;
136 void __iomem
*udp_baseaddr
;
138 struct clk
*usb_slv_clk
;
143 struct dma_pool
*dd_cache
;
145 /* Common EP and control data */
147 u32 enabled_hwepints
;
151 /* VBUS detection, pullup, and power flags */
158 /* Work queues related to I2C support */
159 struct work_struct pullup_job
;
160 struct work_struct power_job
;
162 /* USB device peripheral - various */
163 struct lpc32xx_ep ep
[NUM_ENDPOINTS
];
168 atomic_t enabled_ep_cnt
;
169 wait_queue_head_t ep_disable_wait_queue
;
175 struct lpc32xx_request
{
176 struct usb_request req
;
177 struct list_head queue
;
178 struct lpc32xx_usbd_dd_gad
*dd_desc_ptr
;
183 static inline struct lpc32xx_udc
*to_udc(struct usb_gadget
*g
)
185 return container_of(g
, struct lpc32xx_udc
, gadget
);
188 #define ep_dbg(epp, fmt, arg...) \
189 dev_dbg(epp->udc->dev, "%s: " fmt, __func__, ## arg)
190 #define ep_err(epp, fmt, arg...) \
191 dev_err(epp->udc->dev, "%s: " fmt, __func__, ## arg)
192 #define ep_info(epp, fmt, arg...) \
193 dev_info(epp->udc->dev, "%s: " fmt, __func__, ## arg)
194 #define ep_warn(epp, fmt, arg...) \
195 dev_warn(epp->udc->dev, "%s:" fmt, __func__, ## arg)
197 #define UDCA_BUFF_SIZE (128)
199 /**********************************************************************
200 * USB device controller register offsets
201 **********************************************************************/
203 #define USBD_DEVINTST(x) ((x) + 0x200)
204 #define USBD_DEVINTEN(x) ((x) + 0x204)
205 #define USBD_DEVINTCLR(x) ((x) + 0x208)
206 #define USBD_DEVINTSET(x) ((x) + 0x20C)
207 #define USBD_CMDCODE(x) ((x) + 0x210)
208 #define USBD_CMDDATA(x) ((x) + 0x214)
209 #define USBD_RXDATA(x) ((x) + 0x218)
210 #define USBD_TXDATA(x) ((x) + 0x21C)
211 #define USBD_RXPLEN(x) ((x) + 0x220)
212 #define USBD_TXPLEN(x) ((x) + 0x224)
213 #define USBD_CTRL(x) ((x) + 0x228)
214 #define USBD_DEVINTPRI(x) ((x) + 0x22C)
215 #define USBD_EPINTST(x) ((x) + 0x230)
216 #define USBD_EPINTEN(x) ((x) + 0x234)
217 #define USBD_EPINTCLR(x) ((x) + 0x238)
218 #define USBD_EPINTSET(x) ((x) + 0x23C)
219 #define USBD_EPINTPRI(x) ((x) + 0x240)
220 #define USBD_REEP(x) ((x) + 0x244)
221 #define USBD_EPIND(x) ((x) + 0x248)
222 #define USBD_EPMAXPSIZE(x) ((x) + 0x24C)
223 /* DMA support registers only below */
224 /* Set, clear, or get enabled state of the DMA request status. If
225 * enabled, an IN or OUT token will start a DMA transfer for the EP */
226 #define USBD_DMARST(x) ((x) + 0x250)
227 #define USBD_DMARCLR(x) ((x) + 0x254)
228 #define USBD_DMARSET(x) ((x) + 0x258)
229 /* DMA UDCA head pointer */
230 #define USBD_UDCAH(x) ((x) + 0x280)
231 /* EP DMA status, enable, and disable. This is used to specifically
232 * enabled or disable DMA for a specific EP */
233 #define USBD_EPDMAST(x) ((x) + 0x284)
234 #define USBD_EPDMAEN(x) ((x) + 0x288)
235 #define USBD_EPDMADIS(x) ((x) + 0x28C)
236 /* DMA master interrupts enable and pending interrupts */
237 #define USBD_DMAINTST(x) ((x) + 0x290)
238 #define USBD_DMAINTEN(x) ((x) + 0x294)
239 /* DMA end of transfer interrupt enable, disable, status */
240 #define USBD_EOTINTST(x) ((x) + 0x2A0)
241 #define USBD_EOTINTCLR(x) ((x) + 0x2A4)
242 #define USBD_EOTINTSET(x) ((x) + 0x2A8)
243 /* New DD request interrupt enable, disable, status */
244 #define USBD_NDDRTINTST(x) ((x) + 0x2AC)
245 #define USBD_NDDRTINTCLR(x) ((x) + 0x2B0)
246 #define USBD_NDDRTINTSET(x) ((x) + 0x2B4)
247 /* DMA error interrupt enable, disable, status */
248 #define USBD_SYSERRTINTST(x) ((x) + 0x2B8)
249 #define USBD_SYSERRTINTCLR(x) ((x) + 0x2BC)
250 #define USBD_SYSERRTINTSET(x) ((x) + 0x2C0)
252 /**********************************************************************
253 * USBD_DEVINTST/USBD_DEVINTEN/USBD_DEVINTCLR/USBD_DEVINTSET/
254 * USBD_DEVINTPRI register definitions
255 **********************************************************************/
256 #define USBD_ERR_INT (1 << 9)
257 #define USBD_EP_RLZED (1 << 8)
258 #define USBD_TXENDPKT (1 << 7)
259 #define USBD_RXENDPKT (1 << 6)
260 #define USBD_CDFULL (1 << 5)
261 #define USBD_CCEMPTY (1 << 4)
262 #define USBD_DEV_STAT (1 << 3)
263 #define USBD_EP_SLOW (1 << 2)
264 #define USBD_EP_FAST (1 << 1)
265 #define USBD_FRAME (1 << 0)
267 /**********************************************************************
268 * USBD_EPINTST/USBD_EPINTEN/USBD_EPINTCLR/USBD_EPINTSET/
269 * USBD_EPINTPRI register definitions
270 **********************************************************************/
271 /* End point selection macro (RX) */
272 #define USBD_RX_EP_SEL(e) (1 << ((e) << 1))
274 /* End point selection macro (TX) */
275 #define USBD_TX_EP_SEL(e) (1 << (((e) << 1) + 1))
277 /**********************************************************************
278 * USBD_REEP/USBD_DMARST/USBD_DMARCLR/USBD_DMARSET/USBD_EPDMAST/
279 * USBD_EPDMAEN/USBD_EPDMADIS/
280 * USBD_NDDRTINTST/USBD_NDDRTINTCLR/USBD_NDDRTINTSET/
281 * USBD_EOTINTST/USBD_EOTINTCLR/USBD_EOTINTSET/
282 * USBD_SYSERRTINTST/USBD_SYSERRTINTCLR/USBD_SYSERRTINTSET
283 * register definitions
284 **********************************************************************/
285 /* Endpoint selection macro */
286 #define USBD_EP_SEL(e) (1 << (e))
288 /**********************************************************************
289 * SBD_DMAINTST/USBD_DMAINTEN
290 **********************************************************************/
291 #define USBD_SYS_ERR_INT (1 << 2)
292 #define USBD_NEW_DD_INT (1 << 1)
293 #define USBD_EOT_INT (1 << 0)
295 /**********************************************************************
296 * USBD_RXPLEN register definitions
297 **********************************************************************/
298 #define USBD_PKT_RDY (1 << 11)
299 #define USBD_DV (1 << 10)
300 #define USBD_PK_LEN_MASK 0x3FF
302 /**********************************************************************
303 * USBD_CTRL register definitions
304 **********************************************************************/
305 #define USBD_LOG_ENDPOINT(e) ((e) << 2)
306 #define USBD_WR_EN (1 << 1)
307 #define USBD_RD_EN (1 << 0)
309 /**********************************************************************
310 * USBD_CMDCODE register definitions
311 **********************************************************************/
312 #define USBD_CMD_CODE(c) ((c) << 16)
313 #define USBD_CMD_PHASE(p) ((p) << 8)
315 /**********************************************************************
316 * USBD_DMARST/USBD_DMARCLR/USBD_DMARSET register definitions
317 **********************************************************************/
318 #define USBD_DMAEP(e) (1 << (e))
320 /* DD (DMA Descriptor) structure, requires word alignment */
321 struct lpc32xx_usbd_dd
{
326 u32 dd_iso_ps_mem_addr
;
329 /* dd_setup bit defines */
330 #define DD_SETUP_ATLE_DMA_MODE 0x01
331 #define DD_SETUP_NEXT_DD_VALID 0x04
332 #define DD_SETUP_ISO_EP 0x10
333 #define DD_SETUP_PACKETLEN(n) (((n) & 0x7FF) << 5)
334 #define DD_SETUP_DMALENBYTES(n) (((n) & 0xFFFF) << 16)
336 /* dd_status bit defines */
337 #define DD_STATUS_DD_RETIRED 0x01
338 #define DD_STATUS_STS_MASK 0x1E
339 #define DD_STATUS_STS_NS 0x00 /* Not serviced */
340 #define DD_STATUS_STS_BS 0x02 /* Being serviced */
341 #define DD_STATUS_STS_NC 0x04 /* Normal completion */
342 #define DD_STATUS_STS_DUR 0x06 /* Data underrun (short packet) */
343 #define DD_STATUS_STS_DOR 0x08 /* Data overrun */
344 #define DD_STATUS_STS_SE 0x12 /* System error */
345 #define DD_STATUS_PKT_VAL 0x20 /* Packet valid */
346 #define DD_STATUS_LSB_EX 0x40 /* LS byte extracted (ATLE) */
347 #define DD_STATUS_MSB_EX 0x80 /* MS byte extracted (ATLE) */
348 #define DD_STATUS_MLEN(n) (((n) >> 8) & 0x3F)
349 #define DD_STATUS_CURDMACNT(n) (((n) >> 16) & 0xFFFF)
353 * Protocol engine bits below
356 /* Device Interrupt Bit Definitions */
357 #define FRAME_INT 0x00000001
358 #define EP_FAST_INT 0x00000002
359 #define EP_SLOW_INT 0x00000004
360 #define DEV_STAT_INT 0x00000008
361 #define CCEMTY_INT 0x00000010
362 #define CDFULL_INT 0x00000020
363 #define RxENDPKT_INT 0x00000040
364 #define TxENDPKT_INT 0x00000080
365 #define EP_RLZED_INT 0x00000100
366 #define ERR_INT 0x00000200
368 /* Rx & Tx Packet Length Definitions */
369 #define PKT_LNGTH_MASK 0x000003FF
370 #define PKT_DV 0x00000400
371 #define PKT_RDY 0x00000800
373 /* USB Control Definitions */
374 #define CTRL_RD_EN 0x00000001
375 #define CTRL_WR_EN 0x00000002
378 #define CMD_SET_ADDR 0x00D00500
379 #define CMD_CFG_DEV 0x00D80500
380 #define CMD_SET_MODE 0x00F30500
381 #define CMD_RD_FRAME 0x00F50500
382 #define DAT_RD_FRAME 0x00F50200
383 #define CMD_RD_TEST 0x00FD0500
384 #define DAT_RD_TEST 0x00FD0200
385 #define CMD_SET_DEV_STAT 0x00FE0500
386 #define CMD_GET_DEV_STAT 0x00FE0500
387 #define DAT_GET_DEV_STAT 0x00FE0200
388 #define CMD_GET_ERR_CODE 0x00FF0500
389 #define DAT_GET_ERR_CODE 0x00FF0200
390 #define CMD_RD_ERR_STAT 0x00FB0500
391 #define DAT_RD_ERR_STAT 0x00FB0200
392 #define DAT_WR_BYTE(x) (0x00000100 | ((x) << 16))
393 #define CMD_SEL_EP(x) (0x00000500 | ((x) << 16))
394 #define DAT_SEL_EP(x) (0x00000200 | ((x) << 16))
395 #define CMD_SEL_EP_CLRI(x) (0x00400500 | ((x) << 16))
396 #define DAT_SEL_EP_CLRI(x) (0x00400200 | ((x) << 16))
397 #define CMD_SET_EP_STAT(x) (0x00400500 | ((x) << 16))
398 #define CMD_CLR_BUF 0x00F20500
399 #define DAT_CLR_BUF 0x00F20200
400 #define CMD_VALID_BUF 0x00FA0500
402 /* Device Address Register Definitions */
403 #define DEV_ADDR_MASK 0x7F
406 /* Device Configure Register Definitions */
407 #define CONF_DVICE 0x01
409 /* Device Mode Register Definitions */
418 /* Device Status Register Definitions */
420 #define DEV_CON_CH 0x02
422 #define DEV_SUS_CH 0x08
425 /* Error Code Register Definitions */
426 #define ERR_EC_MASK 0x0F
429 /* Error Status Register Definitions */
431 #define ERR_UEPKT 0x02
432 #define ERR_DCRC 0x04
433 #define ERR_TIMOUT 0x08
435 #define ERR_B_OVRN 0x20
436 #define ERR_BTSTF 0x40
439 /* Endpoint Select Register Definitions */
440 #define EP_SEL_F 0x01
441 #define EP_SEL_ST 0x02
442 #define EP_SEL_STP 0x04
443 #define EP_SEL_PO 0x08
444 #define EP_SEL_EPN 0x10
445 #define EP_SEL_B_1_FULL 0x20
446 #define EP_SEL_B_2_FULL 0x40
448 /* Endpoint Status Register Definitions */
449 #define EP_STAT_ST 0x01
450 #define EP_STAT_DA 0x20
451 #define EP_STAT_RF_MO 0x40
452 #define EP_STAT_CND_ST 0x80
454 /* Clear Buffer Register Definitions */
455 #define CLR_BUF_PO 0x01
457 /* DMA Interrupt Bit Definitions */
459 #define NDD_REQ_INT 0x02
460 #define SYS_ERR_INT 0x04
462 #define DRIVER_VERSION "1.03"
463 static const char driver_name
[] = "lpc32xx_udc";
467 * proc interface support
470 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
471 static char *epnames
[] = {"INT", "ISO", "BULK", "CTRL"};
472 static const char debug_filename
[] = "driver/udc";
474 static void proc_ep_show(struct seq_file
*s
, struct lpc32xx_ep
*ep
)
476 struct lpc32xx_request
*req
;
479 seq_printf(s
, "%12s, maxpacket %4d %3s",
480 ep
->ep
.name
, ep
->ep
.maxpacket
,
481 ep
->is_in
? "in" : "out");
482 seq_printf(s
, " type %4s", epnames
[ep
->eptype
]);
483 seq_printf(s
, " ints: %12d", ep
->totalints
);
485 if (list_empty(&ep
->queue
))
486 seq_printf(s
, "\t(queue empty)\n");
488 list_for_each_entry(req
, &ep
->queue
, queue
) {
489 u32 length
= req
->req
.actual
;
491 seq_printf(s
, "\treq %p len %d/%d buf %p\n",
493 req
->req
.length
, req
->req
.buf
);
498 static int proc_udc_show(struct seq_file
*s
, void *unused
)
500 struct lpc32xx_udc
*udc
= s
->private;
501 struct lpc32xx_ep
*ep
;
504 seq_printf(s
, "%s: version %s\n", driver_name
, DRIVER_VERSION
);
506 spin_lock_irqsave(&udc
->lock
, flags
);
508 seq_printf(s
, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
509 udc
->vbus
? "present" : "off",
510 udc
->enabled
? (udc
->vbus
? "active" : "enabled") :
512 udc
->gadget
.is_selfpowered
? "self" : "VBUS",
513 udc
->suspended
? ", suspended" : "",
514 udc
->driver
? udc
->driver
->driver
.name
: "(none)");
516 if (udc
->enabled
&& udc
->vbus
) {
517 proc_ep_show(s
, &udc
->ep
[0]);
518 list_for_each_entry(ep
, &udc
->gadget
.ep_list
, ep
.ep_list
)
522 spin_unlock_irqrestore(&udc
->lock
, flags
);
527 static int proc_udc_open(struct inode
*inode
, struct file
*file
)
529 return single_open(file
, proc_udc_show
, PDE_DATA(inode
));
532 static const struct file_operations proc_ops
= {
533 .owner
= THIS_MODULE
,
534 .open
= proc_udc_open
,
537 .release
= single_release
,
540 static void create_debug_file(struct lpc32xx_udc
*udc
)
542 udc
->pde
= debugfs_create_file(debug_filename
, 0, NULL
, udc
, &proc_ops
);
545 static void remove_debug_file(struct lpc32xx_udc
*udc
)
547 debugfs_remove(udc
->pde
);
551 static inline void create_debug_file(struct lpc32xx_udc
*udc
) {}
552 static inline void remove_debug_file(struct lpc32xx_udc
*udc
) {}
555 /* Primary initialization sequence for the ISP1301 transceiver */
556 static void isp1301_udc_configure(struct lpc32xx_udc
*udc
)
561 vendor
= i2c_smbus_read_word_data(udc
->isp1301_i2c_client
, 0x00);
562 product
= i2c_smbus_read_word_data(udc
->isp1301_i2c_client
, 0x02);
564 if (vendor
== 0x0483 && product
== 0xa0c4)
567 /* LPC32XX only supports DAT_SE0 USB mode */
568 /* This sequence is important */
570 /* Disable transparent UART mode first */
571 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
572 (ISP1301_I2C_MODE_CONTROL_1
| ISP1301_I2C_REG_CLEAR_ADDR
),
575 /* Set full speed and SE0 mode */
576 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
577 (ISP1301_I2C_MODE_CONTROL_1
| ISP1301_I2C_REG_CLEAR_ADDR
), ~0);
578 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
579 ISP1301_I2C_MODE_CONTROL_1
, (MC1_SPEED_REG
| MC1_DAT_SE0
));
582 * The PSW_OE enable bit state is reversed in the ISP1301 User's Guide
584 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
585 (ISP1301_I2C_MODE_CONTROL_2
| ISP1301_I2C_REG_CLEAR_ADDR
), ~0);
588 if (udc
->atx
!= STOTG04
)
589 value
|= MC2_SPD_SUSP_CTRL
;
590 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
591 ISP1301_I2C_MODE_CONTROL_2
, value
);
593 /* Driver VBUS_DRV high or low depending on board setup */
594 if (udc
->board
->vbus_drv_pol
!= 0)
595 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
596 ISP1301_I2C_OTG_CONTROL_1
, OTG1_VBUS_DRV
);
598 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
599 ISP1301_I2C_OTG_CONTROL_1
| ISP1301_I2C_REG_CLEAR_ADDR
,
602 /* Bi-directional mode with suspend control
603 * Enable both pulldowns for now - the pullup will be enable when VBUS
605 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
606 (ISP1301_I2C_OTG_CONTROL_1
| ISP1301_I2C_REG_CLEAR_ADDR
), ~0);
607 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
608 ISP1301_I2C_OTG_CONTROL_1
,
609 (0 | OTG1_DM_PULLDOWN
| OTG1_DP_PULLDOWN
));
611 /* Discharge VBUS (just in case) */
612 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
613 ISP1301_I2C_OTG_CONTROL_1
, OTG1_VBUS_DISCHRG
);
615 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
616 (ISP1301_I2C_OTG_CONTROL_1
| ISP1301_I2C_REG_CLEAR_ADDR
),
619 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
620 ISP1301_I2C_INTERRUPT_LATCH
| ISP1301_I2C_REG_CLEAR_ADDR
, ~0);
622 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
623 ISP1301_I2C_INTERRUPT_FALLING
| ISP1301_I2C_REG_CLEAR_ADDR
, ~0);
624 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
625 ISP1301_I2C_INTERRUPT_RISING
| ISP1301_I2C_REG_CLEAR_ADDR
, ~0);
627 dev_info(udc
->dev
, "ISP1301 Vendor ID : 0x%04x\n", vendor
);
628 dev_info(udc
->dev
, "ISP1301 Product ID : 0x%04x\n", product
);
629 dev_info(udc
->dev
, "ISP1301 Version ID : 0x%04x\n",
630 i2c_smbus_read_word_data(udc
->isp1301_i2c_client
, 0x14));
634 /* Enables or disables the USB device pullup via the ISP1301 transceiver */
635 static void isp1301_pullup_set(struct lpc32xx_udc
*udc
)
638 /* Enable pullup for bus signalling */
639 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
640 ISP1301_I2C_OTG_CONTROL_1
, OTG1_DP_PULLUP
);
642 /* Enable pullup for bus signalling */
643 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
644 ISP1301_I2C_OTG_CONTROL_1
| ISP1301_I2C_REG_CLEAR_ADDR
,
648 static void pullup_work(struct work_struct
*work
)
650 struct lpc32xx_udc
*udc
=
651 container_of(work
, struct lpc32xx_udc
, pullup_job
);
653 isp1301_pullup_set(udc
);
656 static void isp1301_pullup_enable(struct lpc32xx_udc
*udc
, int en_pullup
,
659 if (en_pullup
== udc
->pullup
)
662 udc
->pullup
= en_pullup
;
664 isp1301_pullup_set(udc
);
666 /* defer slow i2c pull up setting */
667 schedule_work(&udc
->pullup_job
);
671 /* Powers up or down the ISP1301 transceiver */
672 static void isp1301_set_powerstate(struct lpc32xx_udc
*udc
, int enable
)
674 /* There is no "global power down" register for stotg04 */
675 if (udc
->atx
== STOTG04
)
679 /* Power up ISP1301 - this ISP1301 will automatically wakeup
680 when VBUS is detected */
681 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
682 ISP1301_I2C_MODE_CONTROL_2
| ISP1301_I2C_REG_CLEAR_ADDR
,
685 /* Power down ISP1301 */
686 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
687 ISP1301_I2C_MODE_CONTROL_2
, MC2_GLOBAL_PWR_DN
);
690 static void power_work(struct work_struct
*work
)
692 struct lpc32xx_udc
*udc
=
693 container_of(work
, struct lpc32xx_udc
, power_job
);
695 isp1301_set_powerstate(udc
, udc
->poweron
);
701 * USB protocol engine command/data read/write helper functions
704 /* Issues a single command to the USB device state machine */
705 static void udc_protocol_cmd_w(struct lpc32xx_udc
*udc
, u32 cmd
)
710 /* EP may lock on CLRI if this read isn't done */
711 u32 tmp
= readl(USBD_DEVINTST(udc
->udp_baseaddr
));
715 writel(USBD_CCEMPTY
, USBD_DEVINTCLR(udc
->udp_baseaddr
));
717 /* Write command code */
718 writel(cmd
, USBD_CMDCODE(udc
->udp_baseaddr
));
720 while (((readl(USBD_DEVINTST(udc
->udp_baseaddr
)) &
721 USBD_CCEMPTY
) == 0) && (to
> 0)) {
732 /* Issues 2 commands (or command and data) to the USB device state machine */
733 static inline void udc_protocol_cmd_data_w(struct lpc32xx_udc
*udc
, u32 cmd
,
736 udc_protocol_cmd_w(udc
, cmd
);
737 udc_protocol_cmd_w(udc
, data
);
740 /* Issues a single command to the USB device state machine and reads
742 static u32
udc_protocol_cmd_r(struct lpc32xx_udc
*udc
, u32 cmd
)
746 /* Write a command and read data from the protocol engine */
747 writel((USBD_CDFULL
| USBD_CCEMPTY
),
748 USBD_DEVINTCLR(udc
->udp_baseaddr
));
750 /* Write command code */
751 udc_protocol_cmd_w(udc
, cmd
);
753 while ((!(readl(USBD_DEVINTST(udc
->udp_baseaddr
)) & USBD_CDFULL
))
758 "Protocol engine didn't receive response (CDFULL)\n");
760 return readl(USBD_CMDDATA(udc
->udp_baseaddr
));
765 * USB device interrupt mask support functions
768 /* Enable one or more USB device interrupts */
769 static inline void uda_enable_devint(struct lpc32xx_udc
*udc
, u32 devmask
)
771 udc
->enabled_devints
|= devmask
;
772 writel(udc
->enabled_devints
, USBD_DEVINTEN(udc
->udp_baseaddr
));
775 /* Disable one or more USB device interrupts */
776 static inline void uda_disable_devint(struct lpc32xx_udc
*udc
, u32 mask
)
778 udc
->enabled_devints
&= ~mask
;
779 writel(udc
->enabled_devints
, USBD_DEVINTEN(udc
->udp_baseaddr
));
782 /* Clear one or more USB device interrupts */
783 static inline void uda_clear_devint(struct lpc32xx_udc
*udc
, u32 mask
)
785 writel(mask
, USBD_DEVINTCLR(udc
->udp_baseaddr
));
790 * Endpoint interrupt disable/enable functions
793 /* Enable one or more USB endpoint interrupts */
794 static void uda_enable_hwepint(struct lpc32xx_udc
*udc
, u32 hwep
)
796 udc
->enabled_hwepints
|= (1 << hwep
);
797 writel(udc
->enabled_hwepints
, USBD_EPINTEN(udc
->udp_baseaddr
));
800 /* Disable one or more USB endpoint interrupts */
801 static void uda_disable_hwepint(struct lpc32xx_udc
*udc
, u32 hwep
)
803 udc
->enabled_hwepints
&= ~(1 << hwep
);
804 writel(udc
->enabled_hwepints
, USBD_EPINTEN(udc
->udp_baseaddr
));
807 /* Clear one or more USB endpoint interrupts */
808 static inline void uda_clear_hwepint(struct lpc32xx_udc
*udc
, u32 hwep
)
810 writel((1 << hwep
), USBD_EPINTCLR(udc
->udp_baseaddr
));
813 /* Enable DMA for the HW channel */
814 static inline void udc_ep_dma_enable(struct lpc32xx_udc
*udc
, u32 hwep
)
816 writel((1 << hwep
), USBD_EPDMAEN(udc
->udp_baseaddr
));
819 /* Disable DMA for the HW channel */
820 static inline void udc_ep_dma_disable(struct lpc32xx_udc
*udc
, u32 hwep
)
822 writel((1 << hwep
), USBD_EPDMADIS(udc
->udp_baseaddr
));
827 * Endpoint realize/unrealize functions
830 /* Before an endpoint can be used, it needs to be realized
831 * in the USB protocol engine - this realizes the endpoint.
832 * The interrupt (FIFO or DMA) is not enabled with this function */
833 static void udc_realize_hwep(struct lpc32xx_udc
*udc
, u32 hwep
,
838 writel(USBD_EP_RLZED
, USBD_DEVINTCLR(udc
->udp_baseaddr
));
839 writel(hwep
, USBD_EPIND(udc
->udp_baseaddr
));
840 udc
->realized_eps
|= (1 << hwep
);
841 writel(udc
->realized_eps
, USBD_REEP(udc
->udp_baseaddr
));
842 writel(maxpacket
, USBD_EPMAXPSIZE(udc
->udp_baseaddr
));
844 /* Wait until endpoint is realized in hardware */
845 while ((!(readl(USBD_DEVINTST(udc
->udp_baseaddr
)) &
846 USBD_EP_RLZED
)) && (to
> 0))
849 dev_dbg(udc
->dev
, "EP not correctly realized in hardware\n");
851 writel(USBD_EP_RLZED
, USBD_DEVINTCLR(udc
->udp_baseaddr
));
854 /* Unrealize an EP */
855 static void udc_unrealize_hwep(struct lpc32xx_udc
*udc
, u32 hwep
)
857 udc
->realized_eps
&= ~(1 << hwep
);
858 writel(udc
->realized_eps
, USBD_REEP(udc
->udp_baseaddr
));
863 * Endpoint support functions
866 /* Select and clear endpoint interrupt */
867 static u32
udc_selep_clrint(struct lpc32xx_udc
*udc
, u32 hwep
)
869 udc_protocol_cmd_w(udc
, CMD_SEL_EP_CLRI(hwep
));
870 return udc_protocol_cmd_r(udc
, DAT_SEL_EP_CLRI(hwep
));
873 /* Disables the endpoint in the USB protocol engine */
874 static void udc_disable_hwep(struct lpc32xx_udc
*udc
, u32 hwep
)
876 udc_protocol_cmd_data_w(udc
, CMD_SET_EP_STAT(hwep
),
877 DAT_WR_BYTE(EP_STAT_DA
));
880 /* Stalls the endpoint - endpoint will return STALL */
881 static void udc_stall_hwep(struct lpc32xx_udc
*udc
, u32 hwep
)
883 udc_protocol_cmd_data_w(udc
, CMD_SET_EP_STAT(hwep
),
884 DAT_WR_BYTE(EP_STAT_ST
));
887 /* Clear stall or reset endpoint */
888 static void udc_clrstall_hwep(struct lpc32xx_udc
*udc
, u32 hwep
)
890 udc_protocol_cmd_data_w(udc
, CMD_SET_EP_STAT(hwep
),
894 /* Select an endpoint for endpoint status, clear, validate */
895 static void udc_select_hwep(struct lpc32xx_udc
*udc
, u32 hwep
)
897 udc_protocol_cmd_w(udc
, CMD_SEL_EP(hwep
));
902 * Endpoint buffer management functions
905 /* Clear the current endpoint's buffer */
906 static void udc_clr_buffer_hwep(struct lpc32xx_udc
*udc
, u32 hwep
)
908 udc_select_hwep(udc
, hwep
);
909 udc_protocol_cmd_w(udc
, CMD_CLR_BUF
);
912 /* Validate the current endpoint's buffer */
913 static void udc_val_buffer_hwep(struct lpc32xx_udc
*udc
, u32 hwep
)
915 udc_select_hwep(udc
, hwep
);
916 udc_protocol_cmd_w(udc
, CMD_VALID_BUF
);
919 static inline u32
udc_clearep_getsts(struct lpc32xx_udc
*udc
, u32 hwep
)
921 /* Clear EP interrupt */
922 uda_clear_hwepint(udc
, hwep
);
923 return udc_selep_clrint(udc
, hwep
);
931 /* Allocate a DMA Descriptor */
932 static struct lpc32xx_usbd_dd_gad
*udc_dd_alloc(struct lpc32xx_udc
*udc
)
935 struct lpc32xx_usbd_dd_gad
*dd
;
937 dd
= dma_pool_alloc(udc
->dd_cache
, GFP_ATOMIC
| GFP_DMA
, &dma
);
944 /* Free a DMA Descriptor */
945 static void udc_dd_free(struct lpc32xx_udc
*udc
, struct lpc32xx_usbd_dd_gad
*dd
)
947 dma_pool_free(udc
->dd_cache
, dd
, dd
->this_dma
);
952 * USB setup and shutdown functions
955 /* Enables or disables most of the USB system clocks when low power mode is
956 * needed. Clocks are typically started on a connection event, and disabled
957 * when a cable is disconnected */
958 static void udc_clk_set(struct lpc32xx_udc
*udc
, int enable
)
965 clk_prepare_enable(udc
->usb_slv_clk
);
971 clk_disable_unprepare(udc
->usb_slv_clk
);
975 /* Set/reset USB device address */
976 static void udc_set_address(struct lpc32xx_udc
*udc
, u32 addr
)
978 /* Address will be latched at the end of the status phase, or
979 latched immediately if function is called twice */
980 udc_protocol_cmd_data_w(udc
, CMD_SET_ADDR
,
981 DAT_WR_BYTE(DEV_EN
| addr
));
984 /* Setup up a IN request for DMA transfer - this consists of determining the
985 * list of DMA addresses for the transfer, allocating DMA Descriptors,
986 * installing the DD into the UDCA, and then enabling the DMA for that EP */
987 static int udc_ep_in_req_dma(struct lpc32xx_udc
*udc
, struct lpc32xx_ep
*ep
)
989 struct lpc32xx_request
*req
;
990 u32 hwep
= ep
->hwep_num
;
994 /* There will always be a request waiting here */
995 req
= list_entry(ep
->queue
.next
, struct lpc32xx_request
, queue
);
997 /* Place the DD Descriptor into the UDCA */
998 udc
->udca_v_base
[hwep
] = req
->dd_desc_ptr
->this_dma
;
1000 /* Enable DMA and interrupt for the HW EP */
1001 udc_ep_dma_enable(udc
, hwep
);
1003 /* Clear ZLP if last packet is not of MAXP size */
1004 if (req
->req
.length
% ep
->ep
.maxpacket
)
1010 /* Setup up a OUT request for DMA transfer - this consists of determining the
1011 * list of DMA addresses for the transfer, allocating DMA Descriptors,
1012 * installing the DD into the UDCA, and then enabling the DMA for that EP */
1013 static int udc_ep_out_req_dma(struct lpc32xx_udc
*udc
, struct lpc32xx_ep
*ep
)
1015 struct lpc32xx_request
*req
;
1016 u32 hwep
= ep
->hwep_num
;
1018 ep
->req_pending
= 1;
1020 /* There will always be a request waiting here */
1021 req
= list_entry(ep
->queue
.next
, struct lpc32xx_request
, queue
);
1023 /* Place the DD Descriptor into the UDCA */
1024 udc
->udca_v_base
[hwep
] = req
->dd_desc_ptr
->this_dma
;
1026 /* Enable DMA and interrupt for the HW EP */
1027 udc_ep_dma_enable(udc
, hwep
);
1031 static void udc_disable(struct lpc32xx_udc
*udc
)
1035 /* Disable device */
1036 udc_protocol_cmd_data_w(udc
, CMD_CFG_DEV
, DAT_WR_BYTE(0));
1037 udc_protocol_cmd_data_w(udc
, CMD_SET_DEV_STAT
, DAT_WR_BYTE(0));
1039 /* Disable all device interrupts (including EP0) */
1040 uda_disable_devint(udc
, 0x3FF);
1042 /* Disable and reset all endpoint interrupts */
1043 for (i
= 0; i
< 32; i
++) {
1044 uda_disable_hwepint(udc
, i
);
1045 uda_clear_hwepint(udc
, i
);
1046 udc_disable_hwep(udc
, i
);
1047 udc_unrealize_hwep(udc
, i
);
1048 udc
->udca_v_base
[i
] = 0;
1050 /* Disable and clear all interrupts and DMA */
1051 udc_ep_dma_disable(udc
, i
);
1052 writel((1 << i
), USBD_EOTINTCLR(udc
->udp_baseaddr
));
1053 writel((1 << i
), USBD_NDDRTINTCLR(udc
->udp_baseaddr
));
1054 writel((1 << i
), USBD_SYSERRTINTCLR(udc
->udp_baseaddr
));
1055 writel((1 << i
), USBD_DMARCLR(udc
->udp_baseaddr
));
1058 /* Disable DMA interrupts */
1059 writel(0, USBD_DMAINTEN(udc
->udp_baseaddr
));
1061 writel(0, USBD_UDCAH(udc
->udp_baseaddr
));
1064 static void udc_enable(struct lpc32xx_udc
*udc
)
1067 struct lpc32xx_ep
*ep
= &udc
->ep
[0];
1069 /* Start with known state */
1073 udc_protocol_cmd_data_w(udc
, CMD_SET_DEV_STAT
, DAT_WR_BYTE(DEV_CON
));
1075 /* EP interrupts on high priority, FRAME interrupt on low priority */
1076 writel(USBD_EP_FAST
, USBD_DEVINTPRI(udc
->udp_baseaddr
));
1077 writel(0xFFFF, USBD_EPINTPRI(udc
->udp_baseaddr
));
1079 /* Clear any pending device interrupts */
1080 writel(0x3FF, USBD_DEVINTCLR(udc
->udp_baseaddr
));
1082 /* Setup UDCA - not yet used (DMA) */
1083 writel(udc
->udca_p_base
, USBD_UDCAH(udc
->udp_baseaddr
));
1085 /* Only enable EP0 in and out for now, EP0 only works in FIFO mode */
1086 for (i
= 0; i
<= 1; i
++) {
1087 udc_realize_hwep(udc
, i
, ep
->ep
.maxpacket
);
1088 uda_enable_hwepint(udc
, i
);
1089 udc_select_hwep(udc
, i
);
1090 udc_clrstall_hwep(udc
, i
);
1091 udc_clr_buffer_hwep(udc
, i
);
1094 /* Device interrupt setup */
1095 uda_clear_devint(udc
, (USBD_ERR_INT
| USBD_DEV_STAT
| USBD_EP_SLOW
|
1097 uda_enable_devint(udc
, (USBD_ERR_INT
| USBD_DEV_STAT
| USBD_EP_SLOW
|
1100 /* Set device address to 0 - called twice to force a latch in the USB
1101 engine without the need of a setup packet status closure */
1102 udc_set_address(udc
, 0);
1103 udc_set_address(udc
, 0);
1105 /* Enable master DMA interrupts */
1106 writel((USBD_SYS_ERR_INT
| USBD_EOT_INT
),
1107 USBD_DMAINTEN(udc
->udp_baseaddr
));
1109 udc
->dev_status
= 0;
1114 * USB device board specific events handled via callbacks
1117 /* Connection change event - notify board function of change */
1118 static void uda_power_event(struct lpc32xx_udc
*udc
, u32 conn
)
1120 /* Just notify of a connection change event (optional) */
1121 if (udc
->board
->conn_chgb
!= NULL
)
1122 udc
->board
->conn_chgb(conn
);
1125 /* Suspend/resume event - notify board function of change */
1126 static void uda_resm_susp_event(struct lpc32xx_udc
*udc
, u32 conn
)
1128 /* Just notify of a Suspend/resume change event (optional) */
1129 if (udc
->board
->susp_chgb
!= NULL
)
1130 udc
->board
->susp_chgb(conn
);
1138 /* Remote wakeup enable/disable - notify board function of change */
1139 static void uda_remwkp_cgh(struct lpc32xx_udc
*udc
)
1141 if (udc
->board
->rmwk_chgb
!= NULL
)
1142 udc
->board
->rmwk_chgb(udc
->dev_status
&
1143 (1 << USB_DEVICE_REMOTE_WAKEUP
));
1146 /* Reads data from FIFO, adjusts for alignment and data size */
1147 static void udc_pop_fifo(struct lpc32xx_udc
*udc
, u8
*data
, u32 bytes
)
1151 u32
*p32
, tmp
, cbytes
;
1153 /* Use optimal data transfer method based on source address and size */
1154 switch (((uintptr_t) data
) & 0x3) {
1155 case 0: /* 32-bit aligned */
1157 cbytes
= (bytes
& ~0x3);
1159 /* Copy 32-bit aligned data first */
1160 for (n
= 0; n
< cbytes
; n
+= 4)
1161 *p32
++ = readl(USBD_RXDATA(udc
->udp_baseaddr
));
1163 /* Handle any remaining bytes */
1164 bl
= bytes
- cbytes
;
1166 tmp
= readl(USBD_RXDATA(udc
->udp_baseaddr
));
1167 for (n
= 0; n
< bl
; n
++)
1168 data
[cbytes
+ n
] = ((tmp
>> (n
* 8)) & 0xFF);
1173 case 1: /* 8-bit aligned */
1175 /* Each byte has to be handled independently */
1176 for (n
= 0; n
< bytes
; n
+= 4) {
1177 tmp
= readl(USBD_RXDATA(udc
->udp_baseaddr
));
1183 for (i
= 0; i
< bl
; i
++)
1184 data
[n
+ i
] = (u8
) ((tmp
>> (i
* 8)) & 0xFF);
1188 case 2: /* 16-bit aligned */
1190 cbytes
= (bytes
& ~0x3);
1192 /* Copy 32-bit sized objects first with 16-bit alignment */
1193 for (n
= 0; n
< cbytes
; n
+= 4) {
1194 tmp
= readl(USBD_RXDATA(udc
->udp_baseaddr
));
1195 *p16
++ = (u16
)(tmp
& 0xFFFF);
1196 *p16
++ = (u16
)((tmp
>> 16) & 0xFFFF);
1199 /* Handle any remaining bytes */
1200 bl
= bytes
- cbytes
;
1202 tmp
= readl(USBD_RXDATA(udc
->udp_baseaddr
));
1203 for (n
= 0; n
< bl
; n
++)
1204 data
[cbytes
+ n
] = ((tmp
>> (n
* 8)) & 0xFF);
1210 /* Read data from the FIFO for an endpoint. This function is for endpoints (such
1211 * as EP0) that don't use DMA. This function should only be called if a packet
1212 * is known to be ready to read for the endpoint. Note that the endpoint must
1213 * be selected in the protocol engine prior to this call. */
1214 static u32
udc_read_hwep(struct lpc32xx_udc
*udc
, u32 hwep
, u32
*data
,
1219 u32 tmp
, hwrep
= ((hwep
& 0x1E) << 1) | CTRL_RD_EN
;
1221 /* Setup read of endpoint */
1222 writel(hwrep
, USBD_CTRL(udc
->udp_baseaddr
));
1224 /* Wait until packet is ready */
1225 while ((((tmpv
= readl(USBD_RXPLEN(udc
->udp_baseaddr
))) &
1226 PKT_RDY
) == 0) && (to
> 0))
1229 dev_dbg(udc
->dev
, "No packet ready on FIFO EP read\n");
1231 /* Mask out count */
1232 tmp
= tmpv
& PKT_LNGTH_MASK
;
1236 if ((tmp
> 0) && (data
!= NULL
))
1237 udc_pop_fifo(udc
, (u8
*) data
, tmp
);
1239 writel(((hwep
& 0x1E) << 1), USBD_CTRL(udc
->udp_baseaddr
));
1241 /* Clear the buffer */
1242 udc_clr_buffer_hwep(udc
, hwep
);
1247 /* Stuffs data into the FIFO, adjusts for alignment and data size */
1248 static void udc_stuff_fifo(struct lpc32xx_udc
*udc
, u8
*data
, u32 bytes
)
1252 u32
*p32
, tmp
, cbytes
;
1254 /* Use optimal data transfer method based on source address and size */
1255 switch (((uintptr_t) data
) & 0x3) {
1256 case 0: /* 32-bit aligned */
1258 cbytes
= (bytes
& ~0x3);
1260 /* Copy 32-bit aligned data first */
1261 for (n
= 0; n
< cbytes
; n
+= 4)
1262 writel(*p32
++, USBD_TXDATA(udc
->udp_baseaddr
));
1264 /* Handle any remaining bytes */
1265 bl
= bytes
- cbytes
;
1268 for (n
= 0; n
< bl
; n
++)
1269 tmp
|= data
[cbytes
+ n
] << (n
* 8);
1271 writel(tmp
, USBD_TXDATA(udc
->udp_baseaddr
));
1275 case 1: /* 8-bit aligned */
1277 /* Each byte has to be handled independently */
1278 for (n
= 0; n
< bytes
; n
+= 4) {
1284 for (i
= 0; i
< bl
; i
++)
1285 tmp
|= data
[n
+ i
] << (i
* 8);
1287 writel(tmp
, USBD_TXDATA(udc
->udp_baseaddr
));
1291 case 2: /* 16-bit aligned */
1293 cbytes
= (bytes
& ~0x3);
1295 /* Copy 32-bit aligned data first */
1296 for (n
= 0; n
< cbytes
; n
+= 4) {
1297 tmp
= *p16
++ & 0xFFFF;
1298 tmp
|= (*p16
++ & 0xFFFF) << 16;
1299 writel(tmp
, USBD_TXDATA(udc
->udp_baseaddr
));
1302 /* Handle any remaining bytes */
1303 bl
= bytes
- cbytes
;
1306 for (n
= 0; n
< bl
; n
++)
1307 tmp
|= data
[cbytes
+ n
] << (n
* 8);
1309 writel(tmp
, USBD_TXDATA(udc
->udp_baseaddr
));
1315 /* Write data to the FIFO for an endpoint. This function is for endpoints (such
1316 * as EP0) that don't use DMA. Note that the endpoint must be selected in the
1317 * protocol engine prior to this call. */
1318 static void udc_write_hwep(struct lpc32xx_udc
*udc
, u32 hwep
, u32
*data
,
1321 u32 hwwep
= ((hwep
& 0x1E) << 1) | CTRL_WR_EN
;
1323 if ((bytes
> 0) && (data
== NULL
))
1326 /* Setup write of endpoint */
1327 writel(hwwep
, USBD_CTRL(udc
->udp_baseaddr
));
1329 writel(bytes
, USBD_TXPLEN(udc
->udp_baseaddr
));
1331 /* Need at least 1 byte to trigger TX */
1333 writel(0, USBD_TXDATA(udc
->udp_baseaddr
));
1335 udc_stuff_fifo(udc
, (u8
*) data
, bytes
);
1337 writel(((hwep
& 0x1E) << 1), USBD_CTRL(udc
->udp_baseaddr
));
1339 udc_val_buffer_hwep(udc
, hwep
);
1342 /* USB device reset - resets USB to a default state with just EP0
1344 static void uda_usb_reset(struct lpc32xx_udc
*udc
)
1347 /* Re-init device controller and EP0 */
1349 udc
->gadget
.speed
= USB_SPEED_FULL
;
1351 for (i
= 1; i
< NUM_ENDPOINTS
; i
++) {
1352 struct lpc32xx_ep
*ep
= &udc
->ep
[i
];
1353 ep
->req_pending
= 0;
1357 /* Send a ZLP on EP0 */
1358 static void udc_ep0_send_zlp(struct lpc32xx_udc
*udc
)
1360 udc_write_hwep(udc
, EP_IN
, NULL
, 0);
1363 /* Get current frame number */
1364 static u16
udc_get_current_frame(struct lpc32xx_udc
*udc
)
1368 udc_protocol_cmd_w(udc
, CMD_RD_FRAME
);
1369 flo
= (u16
) udc_protocol_cmd_r(udc
, DAT_RD_FRAME
);
1370 fhi
= (u16
) udc_protocol_cmd_r(udc
, DAT_RD_FRAME
);
1372 return (fhi
<< 8) | flo
;
1375 /* Set the device as configured - enables all endpoints */
1376 static inline void udc_set_device_configured(struct lpc32xx_udc
*udc
)
1378 udc_protocol_cmd_data_w(udc
, CMD_CFG_DEV
, DAT_WR_BYTE(CONF_DVICE
));
1381 /* Set the device as unconfigured - disables all endpoints */
1382 static inline void udc_set_device_unconfigured(struct lpc32xx_udc
*udc
)
1384 udc_protocol_cmd_data_w(udc
, CMD_CFG_DEV
, DAT_WR_BYTE(0));
1387 /* reinit == restore initial software state */
1388 static void udc_reinit(struct lpc32xx_udc
*udc
)
1392 INIT_LIST_HEAD(&udc
->gadget
.ep_list
);
1393 INIT_LIST_HEAD(&udc
->gadget
.ep0
->ep_list
);
1395 for (i
= 0; i
< NUM_ENDPOINTS
; i
++) {
1396 struct lpc32xx_ep
*ep
= &udc
->ep
[i
];
1399 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
1400 usb_ep_set_maxpacket_limit(&ep
->ep
, ep
->maxpacket
);
1401 INIT_LIST_HEAD(&ep
->queue
);
1402 ep
->req_pending
= 0;
1405 udc
->ep0state
= WAIT_FOR_SETUP
;
1408 /* Must be called with lock */
1409 static void done(struct lpc32xx_ep
*ep
, struct lpc32xx_request
*req
, int status
)
1411 struct lpc32xx_udc
*udc
= ep
->udc
;
1413 list_del_init(&req
->queue
);
1414 if (req
->req
.status
== -EINPROGRESS
)
1415 req
->req
.status
= status
;
1417 status
= req
->req
.status
;
1420 usb_gadget_unmap_request(&udc
->gadget
, &req
->req
, ep
->is_in
);
1423 udc_dd_free(udc
, req
->dd_desc_ptr
);
1426 if (status
&& status
!= -ESHUTDOWN
)
1427 ep_dbg(ep
, "%s done %p, status %d\n", ep
->ep
.name
, req
, status
);
1429 ep
->req_pending
= 0;
1430 spin_unlock(&udc
->lock
);
1431 usb_gadget_giveback_request(&ep
->ep
, &req
->req
);
1432 spin_lock(&udc
->lock
);
1435 /* Must be called with lock */
1436 static void nuke(struct lpc32xx_ep
*ep
, int status
)
1438 struct lpc32xx_request
*req
;
1440 while (!list_empty(&ep
->queue
)) {
1441 req
= list_entry(ep
->queue
.next
, struct lpc32xx_request
, queue
);
1442 done(ep
, req
, status
);
1445 if (status
== -ESHUTDOWN
) {
1446 uda_disable_hwepint(ep
->udc
, ep
->hwep_num
);
1447 udc_disable_hwep(ep
->udc
, ep
->hwep_num
);
1451 /* IN endpoint 0 transfer */
1452 static int udc_ep0_in_req(struct lpc32xx_udc
*udc
)
1454 struct lpc32xx_request
*req
;
1455 struct lpc32xx_ep
*ep0
= &udc
->ep
[0];
1458 if (list_empty(&ep0
->queue
))
1459 /* Nothing to send */
1462 req
= list_entry(ep0
->queue
.next
, struct lpc32xx_request
,
1465 tsend
= ts
= req
->req
.length
- req
->req
.actual
;
1468 udc_ep0_send_zlp(udc
);
1471 } else if (ts
> ep0
->ep
.maxpacket
)
1472 ts
= ep0
->ep
.maxpacket
; /* Just send what we can */
1474 /* Write data to the EP0 FIFO and start transfer */
1475 udc_write_hwep(udc
, EP_IN
, (req
->req
.buf
+ req
->req
.actual
), ts
);
1477 /* Increment data pointer */
1478 req
->req
.actual
+= ts
;
1480 if (tsend
>= ep0
->ep
.maxpacket
)
1481 return 0; /* Stay in data transfer state */
1483 /* Transfer request is complete */
1484 udc
->ep0state
= WAIT_FOR_SETUP
;
1489 /* OUT endpoint 0 transfer */
1490 static int udc_ep0_out_req(struct lpc32xx_udc
*udc
)
1492 struct lpc32xx_request
*req
;
1493 struct lpc32xx_ep
*ep0
= &udc
->ep
[0];
1494 u32 tr
, bufferspace
;
1496 if (list_empty(&ep0
->queue
))
1499 req
= list_entry(ep0
->queue
.next
, struct lpc32xx_request
,
1503 if (req
->req
.length
== 0) {
1504 /* Just dequeue request */
1506 udc
->ep0state
= WAIT_FOR_SETUP
;
1510 /* Get data from FIFO */
1511 bufferspace
= req
->req
.length
- req
->req
.actual
;
1512 if (bufferspace
> ep0
->ep
.maxpacket
)
1513 bufferspace
= ep0
->ep
.maxpacket
;
1515 /* Copy data to buffer */
1516 prefetchw(req
->req
.buf
+ req
->req
.actual
);
1517 tr
= udc_read_hwep(udc
, EP_OUT
, req
->req
.buf
+ req
->req
.actual
,
1519 req
->req
.actual
+= bufferspace
;
1521 if (tr
< ep0
->ep
.maxpacket
) {
1522 /* This is the last packet */
1524 udc
->ep0state
= WAIT_FOR_SETUP
;
1532 /* Must be called with lock */
1533 static void stop_activity(struct lpc32xx_udc
*udc
)
1535 struct usb_gadget_driver
*driver
= udc
->driver
;
1538 if (udc
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1541 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1544 for (i
= 0; i
< NUM_ENDPOINTS
; i
++) {
1545 struct lpc32xx_ep
*ep
= &udc
->ep
[i
];
1546 nuke(ep
, -ESHUTDOWN
);
1549 spin_unlock(&udc
->lock
);
1550 driver
->disconnect(&udc
->gadget
);
1551 spin_lock(&udc
->lock
);
1554 isp1301_pullup_enable(udc
, 0, 0);
1560 * Activate or kill host pullup
1561 * Can be called with or without lock
1563 static void pullup(struct lpc32xx_udc
*udc
, int is_on
)
1568 if (!udc
->enabled
|| !udc
->vbus
)
1571 if (is_on
!= udc
->pullup
)
1572 isp1301_pullup_enable(udc
, is_on
, 0);
1575 /* Must be called without lock */
1576 static int lpc32xx_ep_disable(struct usb_ep
*_ep
)
1578 struct lpc32xx_ep
*ep
= container_of(_ep
, struct lpc32xx_ep
, ep
);
1579 struct lpc32xx_udc
*udc
= ep
->udc
;
1580 unsigned long flags
;
1582 if ((ep
->hwep_num_base
== 0) || (ep
->hwep_num
== 0))
1584 spin_lock_irqsave(&udc
->lock
, flags
);
1586 nuke(ep
, -ESHUTDOWN
);
1588 /* Clear all DMA statuses for this EP */
1589 udc_ep_dma_disable(udc
, ep
->hwep_num
);
1590 writel(1 << ep
->hwep_num
, USBD_EOTINTCLR(udc
->udp_baseaddr
));
1591 writel(1 << ep
->hwep_num
, USBD_NDDRTINTCLR(udc
->udp_baseaddr
));
1592 writel(1 << ep
->hwep_num
, USBD_SYSERRTINTCLR(udc
->udp_baseaddr
));
1593 writel(1 << ep
->hwep_num
, USBD_DMARCLR(udc
->udp_baseaddr
));
1595 /* Remove the DD pointer in the UDCA */
1596 udc
->udca_v_base
[ep
->hwep_num
] = 0;
1598 /* Disable and reset endpoint and interrupt */
1599 uda_clear_hwepint(udc
, ep
->hwep_num
);
1600 udc_unrealize_hwep(udc
, ep
->hwep_num
);
1604 spin_unlock_irqrestore(&udc
->lock
, flags
);
1606 atomic_dec(&udc
->enabled_ep_cnt
);
1607 wake_up(&udc
->ep_disable_wait_queue
);
1612 /* Must be called without lock */
1613 static int lpc32xx_ep_enable(struct usb_ep
*_ep
,
1614 const struct usb_endpoint_descriptor
*desc
)
1616 struct lpc32xx_ep
*ep
= container_of(_ep
, struct lpc32xx_ep
, ep
);
1617 struct lpc32xx_udc
*udc
= ep
->udc
;
1620 unsigned long flags
;
1622 /* Verify EP data */
1623 if ((!_ep
) || (!ep
) || (!desc
) ||
1624 (desc
->bDescriptorType
!= USB_DT_ENDPOINT
)) {
1625 dev_dbg(udc
->dev
, "bad ep or descriptor\n");
1628 maxpacket
= usb_endpoint_maxp(desc
);
1629 if ((maxpacket
== 0) || (maxpacket
> ep
->maxpacket
)) {
1630 dev_dbg(udc
->dev
, "bad ep descriptor's packet size\n");
1634 /* Don't touch EP0 */
1635 if (ep
->hwep_num_base
== 0) {
1636 dev_dbg(udc
->dev
, "Can't re-enable EP0!!!\n");
1640 /* Is driver ready? */
1641 if ((!udc
->driver
) || (udc
->gadget
.speed
== USB_SPEED_UNKNOWN
)) {
1642 dev_dbg(udc
->dev
, "bogus device state\n");
1646 tmp
= desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
;
1648 case USB_ENDPOINT_XFER_CONTROL
:
1651 case USB_ENDPOINT_XFER_INT
:
1652 if (maxpacket
> ep
->maxpacket
) {
1654 "Bad INT endpoint maxpacket %d\n", maxpacket
);
1659 case USB_ENDPOINT_XFER_BULK
:
1660 switch (maxpacket
) {
1669 "Bad BULK endpoint maxpacket %d\n", maxpacket
);
1674 case USB_ENDPOINT_XFER_ISOC
:
1677 spin_lock_irqsave(&udc
->lock
, flags
);
1679 /* Initialize endpoint to match the selected descriptor */
1680 ep
->is_in
= (desc
->bEndpointAddress
& USB_DIR_IN
) != 0;
1681 ep
->ep
.maxpacket
= maxpacket
;
1683 /* Map hardware endpoint from base and direction */
1685 /* IN endpoints are offset 1 from the OUT endpoint */
1686 ep
->hwep_num
= ep
->hwep_num_base
+ EP_IN
;
1688 ep
->hwep_num
= ep
->hwep_num_base
;
1690 ep_dbg(ep
, "EP enabled: %s, HW:%d, MP:%d IN:%d\n", ep
->ep
.name
,
1691 ep
->hwep_num
, maxpacket
, (ep
->is_in
== 1));
1693 /* Realize the endpoint, interrupt is enabled later when
1694 * buffers are queued, IN EPs will NAK until buffers are ready */
1695 udc_realize_hwep(udc
, ep
->hwep_num
, ep
->ep
.maxpacket
);
1696 udc_clr_buffer_hwep(udc
, ep
->hwep_num
);
1697 uda_disable_hwepint(udc
, ep
->hwep_num
);
1698 udc_clrstall_hwep(udc
, ep
->hwep_num
);
1700 /* Clear all DMA statuses for this EP */
1701 udc_ep_dma_disable(udc
, ep
->hwep_num
);
1702 writel(1 << ep
->hwep_num
, USBD_EOTINTCLR(udc
->udp_baseaddr
));
1703 writel(1 << ep
->hwep_num
, USBD_NDDRTINTCLR(udc
->udp_baseaddr
));
1704 writel(1 << ep
->hwep_num
, USBD_SYSERRTINTCLR(udc
->udp_baseaddr
));
1705 writel(1 << ep
->hwep_num
, USBD_DMARCLR(udc
->udp_baseaddr
));
1707 spin_unlock_irqrestore(&udc
->lock
, flags
);
1709 atomic_inc(&udc
->enabled_ep_cnt
);
1714 * Allocate a USB request list
1715 * Can be called with or without lock
1717 static struct usb_request
*lpc32xx_ep_alloc_request(struct usb_ep
*_ep
,
1720 struct lpc32xx_request
*req
;
1722 req
= kzalloc(sizeof(struct lpc32xx_request
), gfp_flags
);
1726 INIT_LIST_HEAD(&req
->queue
);
1731 * De-allocate a USB request list
1732 * Can be called with or without lock
1734 static void lpc32xx_ep_free_request(struct usb_ep
*_ep
,
1735 struct usb_request
*_req
)
1737 struct lpc32xx_request
*req
;
1739 req
= container_of(_req
, struct lpc32xx_request
, req
);
1740 BUG_ON(!list_empty(&req
->queue
));
1744 /* Must be called without lock */
1745 static int lpc32xx_ep_queue(struct usb_ep
*_ep
,
1746 struct usb_request
*_req
, gfp_t gfp_flags
)
1748 struct lpc32xx_request
*req
;
1749 struct lpc32xx_ep
*ep
;
1750 struct lpc32xx_udc
*udc
;
1751 unsigned long flags
;
1754 req
= container_of(_req
, struct lpc32xx_request
, req
);
1755 ep
= container_of(_ep
, struct lpc32xx_ep
, ep
);
1757 if (!_ep
|| !_req
|| !_req
->complete
|| !_req
->buf
||
1758 !list_empty(&req
->queue
))
1763 if (udc
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1767 struct lpc32xx_usbd_dd_gad
*dd
;
1769 status
= usb_gadget_map_request(&udc
->gadget
, _req
, ep
->is_in
);
1773 /* For the request, build a list of DDs */
1774 dd
= udc_dd_alloc(udc
);
1776 /* Error allocating DD */
1779 req
->dd_desc_ptr
= dd
;
1781 /* Setup the DMA descriptor */
1782 dd
->dd_next_phy
= dd
->dd_next_v
= 0;
1783 dd
->dd_buffer_addr
= req
->req
.dma
;
1786 /* Special handling for ISO EPs */
1787 if (ep
->eptype
== EP_ISO_TYPE
) {
1788 dd
->dd_setup
= DD_SETUP_ISO_EP
|
1789 DD_SETUP_PACKETLEN(0) |
1790 DD_SETUP_DMALENBYTES(1);
1791 dd
->dd_iso_ps_mem_addr
= dd
->this_dma
+ 24;
1793 dd
->iso_status
[0] = req
->req
.length
;
1795 dd
->iso_status
[0] = 0;
1797 dd
->dd_setup
= DD_SETUP_PACKETLEN(ep
->ep
.maxpacket
) |
1798 DD_SETUP_DMALENBYTES(req
->req
.length
);
1801 ep_dbg(ep
, "%s queue req %p len %d buf %p (in=%d) z=%d\n", _ep
->name
,
1802 _req
, _req
->length
, _req
->buf
, ep
->is_in
, _req
->zero
);
1804 spin_lock_irqsave(&udc
->lock
, flags
);
1806 _req
->status
= -EINPROGRESS
;
1808 req
->send_zlp
= _req
->zero
;
1810 /* Kickstart empty queues */
1811 if (list_empty(&ep
->queue
)) {
1812 list_add_tail(&req
->queue
, &ep
->queue
);
1814 if (ep
->hwep_num_base
== 0) {
1815 /* Handle expected data direction */
1817 /* IN packet to host */
1818 udc
->ep0state
= DATA_IN
;
1819 status
= udc_ep0_in_req(udc
);
1821 /* OUT packet from host */
1822 udc
->ep0state
= DATA_OUT
;
1823 status
= udc_ep0_out_req(udc
);
1825 } else if (ep
->is_in
) {
1826 /* IN packet to host and kick off transfer */
1827 if (!ep
->req_pending
)
1828 udc_ep_in_req_dma(udc
, ep
);
1830 /* OUT packet from host and kick off list */
1831 if (!ep
->req_pending
)
1832 udc_ep_out_req_dma(udc
, ep
);
1834 list_add_tail(&req
->queue
, &ep
->queue
);
1836 spin_unlock_irqrestore(&udc
->lock
, flags
);
1838 return (status
< 0) ? status
: 0;
1841 /* Must be called without lock */
1842 static int lpc32xx_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1844 struct lpc32xx_ep
*ep
;
1845 struct lpc32xx_request
*req
;
1846 unsigned long flags
;
1848 ep
= container_of(_ep
, struct lpc32xx_ep
, ep
);
1849 if (!_ep
|| ep
->hwep_num_base
== 0)
1852 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
1854 /* make sure it's actually queued on this endpoint */
1855 list_for_each_entry(req
, &ep
->queue
, queue
) {
1856 if (&req
->req
== _req
)
1859 if (&req
->req
!= _req
) {
1860 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1864 done(ep
, req
, -ECONNRESET
);
1866 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1871 /* Must be called without lock */
1872 static int lpc32xx_ep_set_halt(struct usb_ep
*_ep
, int value
)
1874 struct lpc32xx_ep
*ep
= container_of(_ep
, struct lpc32xx_ep
, ep
);
1875 struct lpc32xx_udc
*udc
= ep
->udc
;
1876 unsigned long flags
;
1878 if ((!ep
) || (ep
->hwep_num
<= 1))
1881 /* Don't halt an IN EP */
1885 spin_lock_irqsave(&udc
->lock
, flags
);
1889 udc_protocol_cmd_data_w(udc
, CMD_SET_EP_STAT(ep
->hwep_num
),
1890 DAT_WR_BYTE(EP_STAT_ST
));
1894 udc_protocol_cmd_data_w(udc
, CMD_SET_EP_STAT(ep
->hwep_num
),
1898 spin_unlock_irqrestore(&udc
->lock
, flags
);
1903 /* set the halt feature and ignores clear requests */
1904 static int lpc32xx_ep_set_wedge(struct usb_ep
*_ep
)
1906 struct lpc32xx_ep
*ep
= container_of(_ep
, struct lpc32xx_ep
, ep
);
1908 if (!_ep
|| !ep
->udc
)
1913 return usb_ep_set_halt(_ep
);
1916 static const struct usb_ep_ops lpc32xx_ep_ops
= {
1917 .enable
= lpc32xx_ep_enable
,
1918 .disable
= lpc32xx_ep_disable
,
1919 .alloc_request
= lpc32xx_ep_alloc_request
,
1920 .free_request
= lpc32xx_ep_free_request
,
1921 .queue
= lpc32xx_ep_queue
,
1922 .dequeue
= lpc32xx_ep_dequeue
,
1923 .set_halt
= lpc32xx_ep_set_halt
,
1924 .set_wedge
= lpc32xx_ep_set_wedge
,
1927 /* Send a ZLP on a non-0 IN EP */
1928 void udc_send_in_zlp(struct lpc32xx_udc
*udc
, struct lpc32xx_ep
*ep
)
1930 /* Clear EP status */
1931 udc_clearep_getsts(udc
, ep
->hwep_num
);
1933 /* Send ZLP via FIFO mechanism */
1934 udc_write_hwep(udc
, ep
->hwep_num
, NULL
, 0);
1938 * Handle EP completion for ZLP
1939 * This function will only be called when a delayed ZLP needs to be sent out
1940 * after a DMA transfer has filled both buffers.
1942 void udc_handle_eps(struct lpc32xx_udc
*udc
, struct lpc32xx_ep
*ep
)
1945 struct lpc32xx_request
*req
;
1947 if (ep
->hwep_num
<= 0)
1950 uda_clear_hwepint(udc
, ep
->hwep_num
);
1952 /* If this interrupt isn't enabled, return now */
1953 if (!(udc
->enabled_hwepints
& (1 << ep
->hwep_num
)))
1956 /* Get endpoint status */
1957 epstatus
= udc_clearep_getsts(udc
, ep
->hwep_num
);
1960 * This should never happen, but protect against writing to the
1963 if (epstatus
& EP_SEL_F
)
1967 udc_send_in_zlp(udc
, ep
);
1968 uda_disable_hwepint(udc
, ep
->hwep_num
);
1972 /* If there isn't a request waiting, something went wrong */
1973 req
= list_entry(ep
->queue
.next
, struct lpc32xx_request
, queue
);
1977 /* Start another request if ready */
1978 if (!list_empty(&ep
->queue
)) {
1980 udc_ep_in_req_dma(udc
, ep
);
1982 udc_ep_out_req_dma(udc
, ep
);
1984 ep
->req_pending
= 0;
1989 /* DMA end of transfer completion */
1990 static void udc_handle_dma_ep(struct lpc32xx_udc
*udc
, struct lpc32xx_ep
*ep
)
1993 struct lpc32xx_request
*req
;
1994 struct lpc32xx_usbd_dd_gad
*dd
;
1996 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2000 req
= list_entry(ep
->queue
.next
, struct lpc32xx_request
, queue
);
2002 ep_err(ep
, "DMA interrupt on no req!\n");
2005 dd
= req
->dd_desc_ptr
;
2007 /* DMA descriptor should always be retired for this call */
2008 if (!(dd
->dd_status
& DD_STATUS_DD_RETIRED
))
2009 ep_warn(ep
, "DMA descriptor did not retire\n");
2012 udc_ep_dma_disable(udc
, ep
->hwep_num
);
2013 writel((1 << ep
->hwep_num
), USBD_EOTINTCLR(udc
->udp_baseaddr
));
2014 writel((1 << ep
->hwep_num
), USBD_NDDRTINTCLR(udc
->udp_baseaddr
));
2017 if (readl(USBD_SYSERRTINTST(udc
->udp_baseaddr
)) &
2018 (1 << ep
->hwep_num
)) {
2019 writel((1 << ep
->hwep_num
),
2020 USBD_SYSERRTINTCLR(udc
->udp_baseaddr
));
2021 ep_err(ep
, "AHB critical error!\n");
2022 ep
->req_pending
= 0;
2024 /* The error could have occurred on a packet of a multipacket
2025 * transfer, so recovering the transfer is not possible. Close
2026 * the request with an error */
2027 done(ep
, req
, -ECONNABORTED
);
2031 /* Handle the current DD's status */
2032 status
= dd
->dd_status
;
2033 switch (status
& DD_STATUS_STS_MASK
) {
2034 case DD_STATUS_STS_NS
:
2035 /* DD not serviced? This shouldn't happen! */
2036 ep
->req_pending
= 0;
2037 ep_err(ep
, "DMA critical EP error: DD not serviced (0x%x)!\n",
2040 done(ep
, req
, -ECONNABORTED
);
2043 case DD_STATUS_STS_BS
:
2044 /* Interrupt only fires on EOT - This shouldn't happen! */
2045 ep
->req_pending
= 0;
2046 ep_err(ep
, "DMA critical EP error: EOT prior to service completion (0x%x)!\n",
2048 done(ep
, req
, -ECONNABORTED
);
2051 case DD_STATUS_STS_NC
:
2052 case DD_STATUS_STS_DUR
:
2053 /* Really just a short packet, not an underrun */
2054 /* This is a good status and what we expect */
2058 /* Data overrun, system error, or unknown */
2059 ep
->req_pending
= 0;
2060 ep_err(ep
, "DMA critical EP error: System error (0x%x)!\n",
2062 done(ep
, req
, -ECONNABORTED
);
2066 /* ISO endpoints are handled differently */
2067 if (ep
->eptype
== EP_ISO_TYPE
) {
2069 req
->req
.actual
= req
->req
.length
;
2071 req
->req
.actual
= dd
->iso_status
[0] & 0xFFFF;
2073 req
->req
.actual
+= DD_STATUS_CURDMACNT(status
);
2075 /* Send a ZLP if necessary. This will be done for non-int
2076 * packets which have a size that is a divisor of MAXP */
2077 if (req
->send_zlp
) {
2079 * If at least 1 buffer is available, send the ZLP now.
2080 * Otherwise, the ZLP send needs to be deferred until a
2081 * buffer is available.
2083 if (udc_clearep_getsts(udc
, ep
->hwep_num
) & EP_SEL_F
) {
2084 udc_clearep_getsts(udc
, ep
->hwep_num
);
2085 uda_enable_hwepint(udc
, ep
->hwep_num
);
2086 udc_clearep_getsts(udc
, ep
->hwep_num
);
2088 /* Let the EP interrupt handle the ZLP */
2091 udc_send_in_zlp(udc
, ep
);
2094 /* Transfer request is complete */
2097 /* Start another request if ready */
2098 udc_clearep_getsts(udc
, ep
->hwep_num
);
2099 if (!list_empty((&ep
->queue
))) {
2101 udc_ep_in_req_dma(udc
, ep
);
2103 udc_ep_out_req_dma(udc
, ep
);
2105 ep
->req_pending
= 0;
2111 * Endpoint 0 functions
2114 static void udc_handle_dev(struct lpc32xx_udc
*udc
)
2118 udc_protocol_cmd_w(udc
, CMD_GET_DEV_STAT
);
2119 tmp
= udc_protocol_cmd_r(udc
, DAT_GET_DEV_STAT
);
2123 else if (tmp
& DEV_CON_CH
)
2124 uda_power_event(udc
, (tmp
& DEV_CON
));
2125 else if (tmp
& DEV_SUS_CH
) {
2126 if (tmp
& DEV_SUS
) {
2129 else if ((udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
) &&
2131 /* Power down transceiver */
2133 schedule_work(&udc
->pullup_job
);
2134 uda_resm_susp_event(udc
, 1);
2136 } else if ((udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
) &&
2137 udc
->driver
&& udc
->vbus
) {
2138 uda_resm_susp_event(udc
, 0);
2139 /* Power up transceiver */
2141 schedule_work(&udc
->pullup_job
);
2146 static int udc_get_status(struct lpc32xx_udc
*udc
, u16 reqtype
, u16 wIndex
)
2148 struct lpc32xx_ep
*ep
;
2149 u32 ep0buff
= 0, tmp
;
2151 switch (reqtype
& USB_RECIP_MASK
) {
2152 case USB_RECIP_INTERFACE
:
2153 break; /* Not supported */
2155 case USB_RECIP_DEVICE
:
2156 ep0buff
= udc
->gadget
.is_selfpowered
;
2157 if (udc
->dev_status
& (1 << USB_DEVICE_REMOTE_WAKEUP
))
2158 ep0buff
|= (1 << USB_DEVICE_REMOTE_WAKEUP
);
2161 case USB_RECIP_ENDPOINT
:
2162 tmp
= wIndex
& USB_ENDPOINT_NUMBER_MASK
;
2164 if ((tmp
== 0) || (tmp
>= NUM_ENDPOINTS
))
2167 if (wIndex
& USB_DIR_IN
) {
2169 return -EOPNOTSUPP
; /* Something's wrong */
2170 } else if (ep
->is_in
)
2171 return -EOPNOTSUPP
; /* Not an IN endpoint */
2173 /* Get status of the endpoint */
2174 udc_protocol_cmd_w(udc
, CMD_SEL_EP(ep
->hwep_num
));
2175 tmp
= udc_protocol_cmd_r(udc
, DAT_SEL_EP(ep
->hwep_num
));
2177 if (tmp
& EP_SEL_ST
)
2178 ep0buff
= (1 << USB_ENDPOINT_HALT
);
2188 udc_write_hwep(udc
, EP_IN
, &ep0buff
, 2);
2193 static void udc_handle_ep0_setup(struct lpc32xx_udc
*udc
)
2195 struct lpc32xx_ep
*ep
, *ep0
= &udc
->ep
[0];
2196 struct usb_ctrlrequest ctrlpkt
;
2198 u16 wIndex
, wValue
, reqtype
, req
, tmp
;
2200 /* Nuke previous transfers */
2203 /* Get setup packet */
2204 bytes
= udc_read_hwep(udc
, EP_OUT
, (u32
*) &ctrlpkt
, 8);
2206 ep_warn(ep0
, "Incorrectly sized setup packet (s/b 8, is %d)!\n",
2211 /* Native endianness */
2212 wIndex
= le16_to_cpu(ctrlpkt
.wIndex
);
2213 wValue
= le16_to_cpu(ctrlpkt
.wValue
);
2214 reqtype
= le16_to_cpu(ctrlpkt
.bRequestType
);
2216 /* Set direction of EP0 */
2217 if (likely(reqtype
& USB_DIR_IN
))
2222 /* Handle SETUP packet */
2223 req
= le16_to_cpu(ctrlpkt
.bRequest
);
2225 case USB_REQ_CLEAR_FEATURE
:
2226 case USB_REQ_SET_FEATURE
:
2228 case (USB_TYPE_STANDARD
| USB_RECIP_DEVICE
):
2229 if (wValue
!= USB_DEVICE_REMOTE_WAKEUP
)
2230 goto stall
; /* Nothing else handled */
2232 /* Tell board about event */
2233 if (req
== USB_REQ_CLEAR_FEATURE
)
2235 ~(1 << USB_DEVICE_REMOTE_WAKEUP
);
2238 (1 << USB_DEVICE_REMOTE_WAKEUP
);
2239 uda_remwkp_cgh(udc
);
2242 case (USB_TYPE_STANDARD
| USB_RECIP_ENDPOINT
):
2243 tmp
= wIndex
& USB_ENDPOINT_NUMBER_MASK
;
2244 if ((wValue
!= USB_ENDPOINT_HALT
) ||
2245 (tmp
>= NUM_ENDPOINTS
))
2248 /* Find hardware endpoint from logical endpoint */
2254 if (req
== USB_REQ_SET_FEATURE
)
2255 udc_stall_hwep(udc
, tmp
);
2256 else if (!ep
->wedge
)
2257 udc_clrstall_hwep(udc
, tmp
);
2266 case USB_REQ_SET_ADDRESS
:
2267 if (reqtype
== (USB_TYPE_STANDARD
| USB_RECIP_DEVICE
)) {
2268 udc_set_address(udc
, wValue
);
2273 case USB_REQ_GET_STATUS
:
2274 udc_get_status(udc
, reqtype
, wIndex
);
2278 break; /* Let GadgetFS handle the descriptor instead */
2281 if (likely(udc
->driver
)) {
2282 /* device-2-host (IN) or no data setup command, process
2284 spin_unlock(&udc
->lock
);
2285 i
= udc
->driver
->setup(&udc
->gadget
, &ctrlpkt
);
2287 spin_lock(&udc
->lock
);
2288 if (req
== USB_REQ_SET_CONFIGURATION
) {
2289 /* Configuration is set after endpoints are realized */
2291 /* Set configuration */
2292 udc_set_device_configured(udc
);
2294 udc_protocol_cmd_data_w(udc
, CMD_SET_MODE
,
2295 DAT_WR_BYTE(AP_CLK
|
2296 INAK_BI
| INAK_II
));
2298 /* Clear configuration */
2299 udc_set_device_unconfigured(udc
);
2301 /* Disable NAK interrupts */
2302 udc_protocol_cmd_data_w(udc
, CMD_SET_MODE
,
2303 DAT_WR_BYTE(AP_CLK
));
2308 /* setup processing failed, force stall */
2310 "req %02x.%02x protocol STALL; stat %d\n",
2312 udc
->ep0state
= WAIT_FOR_SETUP
;
2318 udc_ep0_send_zlp(udc
); /* ZLP IN packet on data phase */
2323 udc_stall_hwep(udc
, EP_IN
);
2327 udc_ep0_send_zlp(udc
);
2331 /* IN endpoint 0 transfer */
2332 static void udc_handle_ep0_in(struct lpc32xx_udc
*udc
)
2334 struct lpc32xx_ep
*ep0
= &udc
->ep
[0];
2337 /* Clear EP interrupt */
2338 epstatus
= udc_clearep_getsts(udc
, EP_IN
);
2340 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2344 /* Stalled? Clear stall and reset buffers */
2345 if (epstatus
& EP_SEL_ST
) {
2346 udc_clrstall_hwep(udc
, EP_IN
);
2347 nuke(ep0
, -ECONNABORTED
);
2348 udc
->ep0state
= WAIT_FOR_SETUP
;
2352 /* Is a buffer available? */
2353 if (!(epstatus
& EP_SEL_F
)) {
2354 /* Handle based on current state */
2355 if (udc
->ep0state
== DATA_IN
)
2356 udc_ep0_in_req(udc
);
2358 /* Unknown state for EP0 oe end of DATA IN phase */
2359 nuke(ep0
, -ECONNABORTED
);
2360 udc
->ep0state
= WAIT_FOR_SETUP
;
2365 /* OUT endpoint 0 transfer */
2366 static void udc_handle_ep0_out(struct lpc32xx_udc
*udc
)
2368 struct lpc32xx_ep
*ep0
= &udc
->ep
[0];
2371 /* Clear EP interrupt */
2372 epstatus
= udc_clearep_getsts(udc
, EP_OUT
);
2375 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2380 if (epstatus
& EP_SEL_ST
) {
2381 udc_clrstall_hwep(udc
, EP_OUT
);
2382 nuke(ep0
, -ECONNABORTED
);
2383 udc
->ep0state
= WAIT_FOR_SETUP
;
2387 /* A NAK may occur if a packet couldn't be received yet */
2388 if (epstatus
& EP_SEL_EPN
)
2390 /* Setup packet incoming? */
2391 if (epstatus
& EP_SEL_STP
) {
2393 udc
->ep0state
= WAIT_FOR_SETUP
;
2396 /* Data available? */
2397 if (epstatus
& EP_SEL_F
)
2398 /* Handle based on current state */
2399 switch (udc
->ep0state
) {
2400 case WAIT_FOR_SETUP
:
2401 udc_handle_ep0_setup(udc
);
2405 udc_ep0_out_req(udc
);
2409 /* Unknown state for EP0 */
2410 nuke(ep0
, -ECONNABORTED
);
2411 udc
->ep0state
= WAIT_FOR_SETUP
;
2415 /* Must be called without lock */
2416 static int lpc32xx_get_frame(struct usb_gadget
*gadget
)
2419 unsigned long flags
;
2420 struct lpc32xx_udc
*udc
= to_udc(gadget
);
2425 spin_lock_irqsave(&udc
->lock
, flags
);
2427 frame
= (int) udc_get_current_frame(udc
);
2429 spin_unlock_irqrestore(&udc
->lock
, flags
);
2434 static int lpc32xx_wakeup(struct usb_gadget
*gadget
)
2439 static int lpc32xx_set_selfpowered(struct usb_gadget
*gadget
, int is_on
)
2441 gadget
->is_selfpowered
= (is_on
!= 0);
2447 * vbus is here! turn everything on that's ready
2448 * Must be called without lock
2450 static int lpc32xx_vbus_session(struct usb_gadget
*gadget
, int is_active
)
2452 unsigned long flags
;
2453 struct lpc32xx_udc
*udc
= to_udc(gadget
);
2455 spin_lock_irqsave(&udc
->lock
, flags
);
2457 /* Doesn't need lock */
2459 udc_clk_set(udc
, 1);
2461 pullup(udc
, is_active
);
2466 spin_unlock_irqrestore(&udc
->lock
, flags
);
2468 * Wait for all the endpoints to disable,
2469 * before disabling clocks. Don't wait if
2470 * endpoints are not enabled.
2472 if (atomic_read(&udc
->enabled_ep_cnt
))
2473 wait_event_interruptible(udc
->ep_disable_wait_queue
,
2474 (atomic_read(&udc
->enabled_ep_cnt
) == 0));
2476 spin_lock_irqsave(&udc
->lock
, flags
);
2478 udc_clk_set(udc
, 0);
2481 spin_unlock_irqrestore(&udc
->lock
, flags
);
2486 /* Can be called with or without lock */
2487 static int lpc32xx_pullup(struct usb_gadget
*gadget
, int is_on
)
2489 struct lpc32xx_udc
*udc
= to_udc(gadget
);
2491 /* Doesn't need lock */
2497 static int lpc32xx_start(struct usb_gadget
*, struct usb_gadget_driver
*);
2498 static int lpc32xx_stop(struct usb_gadget
*);
2500 static const struct usb_gadget_ops lpc32xx_udc_ops
= {
2501 .get_frame
= lpc32xx_get_frame
,
2502 .wakeup
= lpc32xx_wakeup
,
2503 .set_selfpowered
= lpc32xx_set_selfpowered
,
2504 .vbus_session
= lpc32xx_vbus_session
,
2505 .pullup
= lpc32xx_pullup
,
2506 .udc_start
= lpc32xx_start
,
2507 .udc_stop
= lpc32xx_stop
,
2510 static void nop_release(struct device
*dev
)
2512 /* nothing to free */
2515 static const struct lpc32xx_udc controller_template
= {
2517 .ops
= &lpc32xx_udc_ops
,
2518 .name
= driver_name
,
2520 .init_name
= "gadget",
2521 .release
= nop_release
,
2527 .ops
= &lpc32xx_ep_ops
,
2528 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL
,
2529 USB_EP_CAPS_DIR_ALL
),
2533 .hwep_num
= 0, /* Can be 0 or 1, has special handling */
2535 .eptype
= EP_CTL_TYPE
,
2540 .ops
= &lpc32xx_ep_ops
,
2541 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_INT
,
2542 USB_EP_CAPS_DIR_ALL
),
2546 .hwep_num
= 0, /* 2 or 3, will be set later */
2548 .eptype
= EP_INT_TYPE
,
2553 .ops
= &lpc32xx_ep_ops
,
2554 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK
,
2555 USB_EP_CAPS_DIR_ALL
),
2559 .hwep_num
= 0, /* 4 or 5, will be set later */
2561 .eptype
= EP_BLK_TYPE
,
2566 .ops
= &lpc32xx_ep_ops
,
2567 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO
,
2568 USB_EP_CAPS_DIR_ALL
),
2572 .hwep_num
= 0, /* 6 or 7, will be set later */
2574 .eptype
= EP_ISO_TYPE
,
2579 .ops
= &lpc32xx_ep_ops
,
2580 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_INT
,
2581 USB_EP_CAPS_DIR_ALL
),
2585 .hwep_num
= 0, /* 8 or 9, will be set later */
2587 .eptype
= EP_INT_TYPE
,
2592 .ops
= &lpc32xx_ep_ops
,
2593 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK
,
2594 USB_EP_CAPS_DIR_ALL
),
2597 .hwep_num_base
= 10,
2598 .hwep_num
= 0, /* 10 or 11, will be set later */
2600 .eptype
= EP_BLK_TYPE
,
2605 .ops
= &lpc32xx_ep_ops
,
2606 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO
,
2607 USB_EP_CAPS_DIR_ALL
),
2610 .hwep_num_base
= 12,
2611 .hwep_num
= 0, /* 12 or 13, will be set later */
2613 .eptype
= EP_ISO_TYPE
,
2618 .ops
= &lpc32xx_ep_ops
,
2619 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_INT
,
2620 USB_EP_CAPS_DIR_ALL
),
2623 .hwep_num_base
= 14,
2626 .eptype
= EP_INT_TYPE
,
2631 .ops
= &lpc32xx_ep_ops
,
2632 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK
,
2633 USB_EP_CAPS_DIR_ALL
),
2636 .hwep_num_base
= 16,
2639 .eptype
= EP_BLK_TYPE
,
2644 .ops
= &lpc32xx_ep_ops
,
2645 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO
,
2646 USB_EP_CAPS_DIR_ALL
),
2649 .hwep_num_base
= 18,
2652 .eptype
= EP_ISO_TYPE
,
2657 .ops
= &lpc32xx_ep_ops
,
2658 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_INT
,
2659 USB_EP_CAPS_DIR_ALL
),
2662 .hwep_num_base
= 20,
2665 .eptype
= EP_INT_TYPE
,
2669 .name
= "ep11-bulk",
2670 .ops
= &lpc32xx_ep_ops
,
2671 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK
,
2672 USB_EP_CAPS_DIR_ALL
),
2675 .hwep_num_base
= 22,
2678 .eptype
= EP_BLK_TYPE
,
2683 .ops
= &lpc32xx_ep_ops
,
2684 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO
,
2685 USB_EP_CAPS_DIR_ALL
),
2688 .hwep_num_base
= 24,
2691 .eptype
= EP_ISO_TYPE
,
2696 .ops
= &lpc32xx_ep_ops
,
2697 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_INT
,
2698 USB_EP_CAPS_DIR_ALL
),
2701 .hwep_num_base
= 26,
2704 .eptype
= EP_INT_TYPE
,
2708 .name
= "ep14-bulk",
2709 .ops
= &lpc32xx_ep_ops
,
2710 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK
,
2711 USB_EP_CAPS_DIR_ALL
),
2714 .hwep_num_base
= 28,
2717 .eptype
= EP_BLK_TYPE
,
2721 .name
= "ep15-bulk",
2722 .ops
= &lpc32xx_ep_ops
,
2723 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK
,
2724 USB_EP_CAPS_DIR_ALL
),
2727 .hwep_num_base
= 30,
2730 .eptype
= EP_BLK_TYPE
,
2734 /* ISO and status interrupts */
2735 static irqreturn_t
lpc32xx_usb_lp_irq(int irq
, void *_udc
)
2738 struct lpc32xx_udc
*udc
= _udc
;
2740 spin_lock(&udc
->lock
);
2742 /* Read the device status register */
2743 devstat
= readl(USBD_DEVINTST(udc
->udp_baseaddr
));
2745 devstat
&= ~USBD_EP_FAST
;
2746 writel(devstat
, USBD_DEVINTCLR(udc
->udp_baseaddr
));
2747 devstat
= devstat
& udc
->enabled_devints
;
2749 /* Device specific handling needed? */
2750 if (devstat
& USBD_DEV_STAT
)
2751 udc_handle_dev(udc
);
2753 /* Start of frame? (devstat & FRAME_INT):
2754 * The frame interrupt isn't really needed for ISO support,
2755 * as the driver will queue the necessary packets */
2758 if (devstat
& ERR_INT
) {
2759 /* All types of errors, from cable removal during transfer to
2760 * misc protocol and bit errors. These are mostly for just info,
2761 * as the USB hardware will work around these. If these errors
2762 * happen alot, something is wrong. */
2763 udc_protocol_cmd_w(udc
, CMD_RD_ERR_STAT
);
2764 tmp
= udc_protocol_cmd_r(udc
, DAT_RD_ERR_STAT
);
2765 dev_dbg(udc
->dev
, "Device error (0x%x)!\n", tmp
);
2768 spin_unlock(&udc
->lock
);
2774 static irqreturn_t
lpc32xx_usb_hp_irq(int irq
, void *_udc
)
2777 struct lpc32xx_udc
*udc
= _udc
;
2779 spin_lock(&udc
->lock
);
2781 /* Read the device status register */
2782 writel(USBD_EP_FAST
, USBD_DEVINTCLR(udc
->udp_baseaddr
));
2785 tmp
= readl(USBD_EPINTST(udc
->udp_baseaddr
));
2787 /* Special handling for EP0 */
2788 if (tmp
& (EP_MASK_SEL(0, EP_OUT
) | EP_MASK_SEL(0, EP_IN
))) {
2790 if (tmp
& (EP_MASK_SEL(0, EP_IN
)))
2791 udc_handle_ep0_in(udc
);
2793 /* Handle EP0 OUT */
2794 if (tmp
& (EP_MASK_SEL(0, EP_OUT
)))
2795 udc_handle_ep0_out(udc
);
2799 if (tmp
& ~(EP_MASK_SEL(0, EP_OUT
) | EP_MASK_SEL(0, EP_IN
))) {
2802 /* Handle other EP interrupts */
2803 for (i
= 1; i
< NUM_ENDPOINTS
; i
++) {
2804 if (tmp
& (1 << udc
->ep
[i
].hwep_num
))
2805 udc_handle_eps(udc
, &udc
->ep
[i
]);
2809 spin_unlock(&udc
->lock
);
2814 static irqreturn_t
lpc32xx_usb_devdma_irq(int irq
, void *_udc
)
2816 struct lpc32xx_udc
*udc
= _udc
;
2821 spin_lock(&udc
->lock
);
2823 /* Handle EP DMA EOT interrupts */
2824 tmp
= readl(USBD_EOTINTST(udc
->udp_baseaddr
)) |
2825 (readl(USBD_EPDMAST(udc
->udp_baseaddr
)) &
2826 readl(USBD_NDDRTINTST(udc
->udp_baseaddr
))) |
2827 readl(USBD_SYSERRTINTST(udc
->udp_baseaddr
));
2828 for (i
= 1; i
< NUM_ENDPOINTS
; i
++) {
2829 if (tmp
& (1 << udc
->ep
[i
].hwep_num
))
2830 udc_handle_dma_ep(udc
, &udc
->ep
[i
]);
2833 spin_unlock(&udc
->lock
);
2840 * VBUS detection, pullup handler, and Gadget cable state notification
2843 static void vbus_work(struct lpc32xx_udc
*udc
)
2847 if (udc
->enabled
!= 0) {
2848 /* Discharge VBUS real quick */
2849 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
2850 ISP1301_I2C_OTG_CONTROL_1
, OTG1_VBUS_DISCHRG
);
2852 /* Give VBUS some time (100mS) to discharge */
2855 /* Disable VBUS discharge resistor */
2856 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
2857 ISP1301_I2C_OTG_CONTROL_1
| ISP1301_I2C_REG_CLEAR_ADDR
,
2860 /* Clear interrupt */
2861 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
2862 ISP1301_I2C_INTERRUPT_LATCH
|
2863 ISP1301_I2C_REG_CLEAR_ADDR
, ~0);
2865 /* Get the VBUS status from the transceiver */
2866 value
= i2c_smbus_read_byte_data(udc
->isp1301_i2c_client
,
2867 ISP1301_I2C_INTERRUPT_SOURCE
);
2869 /* VBUS on or off? */
2870 if (value
& INT_SESS_VLD
)
2876 if (udc
->last_vbus
!= udc
->vbus
) {
2877 udc
->last_vbus
= udc
->vbus
;
2878 lpc32xx_vbus_session(&udc
->gadget
, udc
->vbus
);
2883 static irqreturn_t
lpc32xx_usb_vbus_irq(int irq
, void *_udc
)
2885 struct lpc32xx_udc
*udc
= _udc
;
2892 static int lpc32xx_start(struct usb_gadget
*gadget
,
2893 struct usb_gadget_driver
*driver
)
2895 struct lpc32xx_udc
*udc
= to_udc(gadget
);
2897 if (!driver
|| driver
->max_speed
< USB_SPEED_FULL
|| !driver
->setup
) {
2898 dev_err(udc
->dev
, "bad parameter.\n");
2903 dev_err(udc
->dev
, "UDC already has a gadget driver\n");
2907 udc
->driver
= driver
;
2908 udc
->gadget
.dev
.of_node
= udc
->dev
->of_node
;
2910 udc
->gadget
.is_selfpowered
= 1;
2913 /* Force VBUS process once to check for cable insertion */
2914 udc
->last_vbus
= udc
->vbus
= 0;
2917 /* enable interrupts */
2918 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
2919 ISP1301_I2C_INTERRUPT_FALLING
, INT_SESS_VLD
| INT_VBUS_VLD
);
2920 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
2921 ISP1301_I2C_INTERRUPT_RISING
, INT_SESS_VLD
| INT_VBUS_VLD
);
2926 static int lpc32xx_stop(struct usb_gadget
*gadget
)
2928 struct lpc32xx_udc
*udc
= to_udc(gadget
);
2930 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
2931 ISP1301_I2C_INTERRUPT_FALLING
| ISP1301_I2C_REG_CLEAR_ADDR
, ~0);
2932 i2c_smbus_write_byte_data(udc
->isp1301_i2c_client
,
2933 ISP1301_I2C_INTERRUPT_RISING
| ISP1301_I2C_REG_CLEAR_ADDR
, ~0);
2936 spin_lock(&udc
->lock
);
2938 spin_unlock(&udc
->lock
);
2941 * Wait for all the endpoints to disable,
2942 * before disabling clocks. Don't wait if
2943 * endpoints are not enabled.
2945 if (atomic_read(&udc
->enabled_ep_cnt
))
2946 wait_event_interruptible(udc
->ep_disable_wait_queue
,
2947 (atomic_read(&udc
->enabled_ep_cnt
) == 0));
2949 spin_lock(&udc
->lock
);
2950 udc_clk_set(udc
, 0);
2951 spin_unlock(&udc
->lock
);
2960 static void lpc32xx_udc_shutdown(struct platform_device
*dev
)
2962 /* Force disconnect on reboot */
2963 struct lpc32xx_udc
*udc
= platform_get_drvdata(dev
);
2969 * Callbacks to be overridden by options passed via OF (TODO)
2972 static void lpc32xx_usbd_conn_chg(int conn
)
2974 /* Do nothing, it might be nice to enable an LED
2975 * based on conn state being !0 */
2978 static void lpc32xx_usbd_susp_chg(int susp
)
2980 /* Device suspend if susp != 0 */
2983 static void lpc32xx_rmwkup_chg(int remote_wakup_enable
)
2985 /* Enable or disable USB remote wakeup */
2988 struct lpc32xx_usbd_cfg lpc32xx_usbddata
= {
2990 .conn_chgb
= &lpc32xx_usbd_conn_chg
,
2991 .susp_chgb
= &lpc32xx_usbd_susp_chg
,
2992 .rmwk_chgb
= &lpc32xx_rmwkup_chg
,
2996 static u64 lpc32xx_usbd_dmamask
= ~(u32
) 0x7F;
2998 static int lpc32xx_udc_probe(struct platform_device
*pdev
)
3000 struct device
*dev
= &pdev
->dev
;
3001 struct lpc32xx_udc
*udc
;
3003 dma_addr_t dma_handle
;
3004 struct device_node
*isp1301_node
;
3006 udc
= devm_kmemdup(dev
, &controller_template
, sizeof(*udc
), GFP_KERNEL
);
3010 for (i
= 0; i
<= 15; i
++)
3011 udc
->ep
[i
].udc
= udc
;
3012 udc
->gadget
.ep0
= &udc
->ep
[0].ep
;
3014 /* init software state */
3015 udc
->gadget
.dev
.parent
= dev
;
3017 udc
->dev
= &pdev
->dev
;
3020 if (pdev
->dev
.of_node
) {
3021 isp1301_node
= of_parse_phandle(pdev
->dev
.of_node
,
3024 isp1301_node
= NULL
;
3027 udc
->isp1301_i2c_client
= isp1301_get_client(isp1301_node
);
3028 if (!udc
->isp1301_i2c_client
) {
3029 return -EPROBE_DEFER
;
3032 dev_info(udc
->dev
, "ISP1301 I2C device at address 0x%x\n",
3033 udc
->isp1301_i2c_client
->addr
);
3035 pdev
->dev
.dma_mask
= &lpc32xx_usbd_dmamask
;
3036 retval
= dma_set_coherent_mask(&pdev
->dev
, DMA_BIT_MASK(32));
3040 udc
->board
= &lpc32xx_usbddata
;
3043 * Resources are mapped as follows:
3044 * IORESOURCE_MEM, base address and size of USB space
3045 * IORESOURCE_IRQ, USB device low priority interrupt number
3046 * IORESOURCE_IRQ, USB device high priority interrupt number
3047 * IORESOURCE_IRQ, USB device interrupt number
3048 * IORESOURCE_IRQ, USB transceiver interrupt number
3051 spin_lock_init(&udc
->lock
);
3054 for (i
= 0; i
< 4; i
++) {
3055 udc
->udp_irq
[i
] = platform_get_irq(pdev
, i
);
3056 if (udc
->udp_irq
[i
] < 0)
3057 return udc
->udp_irq
[i
];
3060 udc
->udp_baseaddr
= devm_platform_ioremap_resource(pdev
, 0);
3061 if (IS_ERR(udc
->udp_baseaddr
)) {
3062 dev_err(udc
->dev
, "IO map failure\n");
3063 return PTR_ERR(udc
->udp_baseaddr
);
3066 /* Get USB device clock */
3067 udc
->usb_slv_clk
= devm_clk_get(&pdev
->dev
, NULL
);
3068 if (IS_ERR(udc
->usb_slv_clk
)) {
3069 dev_err(udc
->dev
, "failed to acquire USB device clock\n");
3070 return PTR_ERR(udc
->usb_slv_clk
);
3073 /* Enable USB device clock */
3074 retval
= clk_prepare_enable(udc
->usb_slv_clk
);
3076 dev_err(udc
->dev
, "failed to start USB device clock\n");
3080 /* Setup deferred workqueue data */
3081 udc
->poweron
= udc
->pullup
= 0;
3082 INIT_WORK(&udc
->pullup_job
, pullup_work
);
3084 INIT_WORK(&udc
->power_job
, power_work
);
3087 /* All clocks are now on */
3090 isp1301_udc_configure(udc
);
3091 /* Allocate memory for the UDCA */
3092 udc
->udca_v_base
= dma_alloc_coherent(&pdev
->dev
, UDCA_BUFF_SIZE
,
3094 (GFP_KERNEL
| GFP_DMA
));
3095 if (!udc
->udca_v_base
) {
3096 dev_err(udc
->dev
, "error getting UDCA region\n");
3100 udc
->udca_p_base
= dma_handle
;
3101 dev_dbg(udc
->dev
, "DMA buffer(0x%x bytes), P:0x%08x, V:0x%p\n",
3102 UDCA_BUFF_SIZE
, udc
->udca_p_base
, udc
->udca_v_base
);
3104 /* Setup the DD DMA memory pool */
3105 udc
->dd_cache
= dma_pool_create("udc_dd", udc
->dev
,
3106 sizeof(struct lpc32xx_usbd_dd_gad
),
3108 if (!udc
->dd_cache
) {
3109 dev_err(udc
->dev
, "error getting DD DMA region\n");
3111 goto dma_alloc_fail
;
3114 /* Clear USB peripheral and initialize gadget endpoints */
3118 /* Request IRQs - low and high priority USB device IRQs are routed to
3119 * the same handler, while the DMA interrupt is routed elsewhere */
3120 retval
= devm_request_irq(dev
, udc
->udp_irq
[IRQ_USB_LP
],
3121 lpc32xx_usb_lp_irq
, 0, "udc_lp", udc
);
3123 dev_err(udc
->dev
, "LP request irq %d failed\n",
3124 udc
->udp_irq
[IRQ_USB_LP
]);
3127 retval
= devm_request_irq(dev
, udc
->udp_irq
[IRQ_USB_HP
],
3128 lpc32xx_usb_hp_irq
, 0, "udc_hp", udc
);
3130 dev_err(udc
->dev
, "HP request irq %d failed\n",
3131 udc
->udp_irq
[IRQ_USB_HP
]);
3135 retval
= devm_request_irq(dev
, udc
->udp_irq
[IRQ_USB_DEVDMA
],
3136 lpc32xx_usb_devdma_irq
, 0, "udc_dma", udc
);
3138 dev_err(udc
->dev
, "DEV request irq %d failed\n",
3139 udc
->udp_irq
[IRQ_USB_DEVDMA
]);
3143 /* The transceiver interrupt is used for VBUS detection and will
3144 kick off the VBUS handler function */
3145 retval
= devm_request_threaded_irq(dev
, udc
->udp_irq
[IRQ_USB_ATX
], NULL
,
3146 lpc32xx_usb_vbus_irq
, IRQF_ONESHOT
,
3149 dev_err(udc
->dev
, "VBUS request irq %d failed\n",
3150 udc
->udp_irq
[IRQ_USB_ATX
]);
3154 /* Initialize wait queue */
3155 init_waitqueue_head(&udc
->ep_disable_wait_queue
);
3156 atomic_set(&udc
->enabled_ep_cnt
, 0);
3158 retval
= usb_add_gadget_udc(dev
, &udc
->gadget
);
3160 goto add_gadget_fail
;
3162 dev_set_drvdata(dev
, udc
);
3163 device_init_wakeup(dev
, 1);
3164 create_debug_file(udc
);
3166 /* Disable clocks for now */
3167 udc_clk_set(udc
, 0);
3169 dev_info(udc
->dev
, "%s version %s\n", driver_name
, DRIVER_VERSION
);
3174 dma_pool_destroy(udc
->dd_cache
);
3176 dma_free_coherent(&pdev
->dev
, UDCA_BUFF_SIZE
,
3177 udc
->udca_v_base
, udc
->udca_p_base
);
3179 clk_disable_unprepare(udc
->usb_slv_clk
);
3180 dev_err(udc
->dev
, "%s probe failed, %d\n", driver_name
, retval
);
3185 static int lpc32xx_udc_remove(struct platform_device
*pdev
)
3187 struct lpc32xx_udc
*udc
= platform_get_drvdata(pdev
);
3189 usb_del_gadget_udc(&udc
->gadget
);
3193 udc_clk_set(udc
, 1);
3197 device_init_wakeup(&pdev
->dev
, 0);
3198 remove_debug_file(udc
);
3200 dma_pool_destroy(udc
->dd_cache
);
3201 dma_free_coherent(&pdev
->dev
, UDCA_BUFF_SIZE
,
3202 udc
->udca_v_base
, udc
->udca_p_base
);
3204 clk_disable_unprepare(udc
->usb_slv_clk
);
3210 static int lpc32xx_udc_suspend(struct platform_device
*pdev
, pm_message_t mesg
)
3212 struct lpc32xx_udc
*udc
= platform_get_drvdata(pdev
);
3215 /* Power down ISP */
3217 isp1301_set_powerstate(udc
, 0);
3219 /* Disable clocking */
3220 udc_clk_set(udc
, 0);
3222 /* Keep clock flag on, so we know to re-enable clocks
3226 /* Kill global USB clock */
3227 clk_disable_unprepare(udc
->usb_slv_clk
);
3233 static int lpc32xx_udc_resume(struct platform_device
*pdev
)
3235 struct lpc32xx_udc
*udc
= platform_get_drvdata(pdev
);
3238 /* Enable global USB clock */
3239 clk_prepare_enable(udc
->usb_slv_clk
);
3241 /* Enable clocking */
3242 udc_clk_set(udc
, 1);
3244 /* ISP back to normal power mode */
3246 isp1301_set_powerstate(udc
, 1);
3252 #define lpc32xx_udc_suspend NULL
3253 #define lpc32xx_udc_resume NULL
3257 static const struct of_device_id lpc32xx_udc_of_match
[] = {
3258 { .compatible
= "nxp,lpc3220-udc", },
3261 MODULE_DEVICE_TABLE(of
, lpc32xx_udc_of_match
);
3264 static struct platform_driver lpc32xx_udc_driver
= {
3265 .remove
= lpc32xx_udc_remove
,
3266 .shutdown
= lpc32xx_udc_shutdown
,
3267 .suspend
= lpc32xx_udc_suspend
,
3268 .resume
= lpc32xx_udc_resume
,
3270 .name
= driver_name
,
3271 .of_match_table
= of_match_ptr(lpc32xx_udc_of_match
),
3275 module_platform_driver_probe(lpc32xx_udc_driver
, lpc32xx_udc_probe
);
3277 MODULE_DESCRIPTION("LPC32XX udc driver");
3278 MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>");
3279 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
3280 MODULE_LICENSE("GPL");
3281 MODULE_ALIAS("platform:lpc32xx_udc");