1 // SPDX-License-Identifier: GPL-2.0+
3 * bcm63xx_udc.c -- BCM63xx UDC high/full speed USB device controller
6 * Copyright (C) 2012 Broadcom Corporation
9 #include <linux/bitops.h>
10 #include <linux/bug.h>
11 #include <linux/clk.h>
12 #include <linux/compiler.h>
13 #include <linux/debugfs.h>
14 #include <linux/delay.h>
15 #include <linux/device.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/errno.h>
18 #include <linux/interrupt.h>
19 #include <linux/ioport.h>
20 #include <linux/kernel.h>
21 #include <linux/list.h>
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/platform_device.h>
25 #include <linux/sched.h>
26 #include <linux/seq_file.h>
27 #include <linux/slab.h>
28 #include <linux/timer.h>
29 #include <linux/usb.h>
30 #include <linux/usb/ch9.h>
31 #include <linux/usb/gadget.h>
32 #include <linux/workqueue.h>
34 #include <bcm63xx_cpu.h>
35 #include <bcm63xx_iudma.h>
36 #include <bcm63xx_dev_usb_usbd.h>
37 #include <bcm63xx_io.h>
38 #include <bcm63xx_regs.h>
40 #define DRV_MODULE_NAME "bcm63xx_udc"
42 static const char bcm63xx_ep0name[] = "ep0";
46 const struct usb_ep_caps caps;
47 } bcm63xx_ep_info[] = {
48 #define EP_INFO(_name, _caps) \
54 EP_INFO(bcm63xx_ep0name,
55 USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL, USB_EP_CAPS_DIR_ALL)),
57 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
58 EP_INFO("ep2out-bulk",
59 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
61 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
63 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_OUT)),
68 static bool use_fullspeed;
69 module_param(use_fullspeed, bool, S_IRUGO);
70 MODULE_PARM_DESC(use_fullspeed, "true for fullspeed only");
73 * RX IRQ coalescing options:
75 * false (default) - one IRQ per DATAx packet. Slow but reliable. The
76 * driver is able to pass the "testusb" suite and recover from conditions like:
78 * 1) Device queues up a 2048-byte RX IUDMA transaction on an OUT bulk ep
79 * 2) Host sends 512 bytes of data
80 * 3) Host decides to reconfigure the device and sends SET_INTERFACE
81 * 4) Device shuts down the endpoint and cancels the RX transaction
83 * true - one IRQ per transfer, for transfers <= 2048B. Generates
84 * considerably fewer IRQs, but error recovery is less robust. Does not
85 * reliably pass "testusb".
87 * TX always uses coalescing, because we can cancel partially complete TX
88 * transfers by repeatedly flushing the FIFO. The hardware doesn't allow
91 static bool irq_coalesce;
92 module_param(irq_coalesce, bool, S_IRUGO);
93 MODULE_PARM_DESC(irq_coalesce, "take one IRQ per RX transfer");
95 #define BCM63XX_NUM_EP 5
96 #define BCM63XX_NUM_IUDMA 6
97 #define BCM63XX_NUM_FIFO_PAIRS 3
99 #define IUDMA_RESET_TIMEOUT_US 10000
101 #define IUDMA_EP0_RXCHAN 0
102 #define IUDMA_EP0_TXCHAN 1
104 #define IUDMA_MAX_FRAGMENT 2048
105 #define BCM63XX_MAX_CTRL_PKT 64
107 #define BCMEP_CTRL 0x00
108 #define BCMEP_ISOC 0x01
109 #define BCMEP_BULK 0x02
110 #define BCMEP_INTR 0x03
112 #define BCMEP_OUT 0x00
113 #define BCMEP_IN 0x01
115 #define BCM63XX_SPD_FULL 1
116 #define BCM63XX_SPD_HIGH 0
118 #define IUDMA_DMAC_OFFSET 0x200
119 #define IUDMA_DMAS_OFFSET 0x400
121 enum bcm63xx_ep0_state {
124 EP0_IN_DATA_PHASE_SETUP,
125 EP0_IN_DATA_PHASE_COMPLETE,
126 EP0_OUT_DATA_PHASE_SETUP,
127 EP0_OUT_DATA_PHASE_COMPLETE,
128 EP0_OUT_STATUS_PHASE,
129 EP0_IN_FAKE_STATUS_PHASE,
133 static const char __maybe_unused bcm63xx_ep0_state_names[][32] = {
136 "IN_DATA_PHASE_SETUP",
137 "IN_DATA_PHASE_COMPLETE",
138 "OUT_DATA_PHASE_SETUP",
139 "OUT_DATA_PHASE_COMPLETE",
141 "IN_FAKE_STATUS_PHASE",
146 * struct iudma_ch_cfg - Static configuration for an IUDMA channel.
147 * @ep_num: USB endpoint number.
148 * @n_bds: Number of buffer descriptors in the ring.
149 * @ep_type: Endpoint type (control, bulk, interrupt).
150 * @dir: Direction (in, out).
151 * @n_fifo_slots: Number of FIFO entries to allocate for this channel.
152 * @max_pkt_hs: Maximum packet size in high speed mode.
153 * @max_pkt_fs: Maximum packet size in full speed mode.
155 struct iudma_ch_cfg {
165 static const struct iudma_ch_cfg iudma_defaults[] = {
167 /* This controller was designed to support a CDC/RNDIS application.
168 It may be possible to reconfigure some of the endpoints, but
169 the hardware limitations (FIFO sizing and number of DMA channels)
170 may significantly impact flexibility and/or stability. Change
171 these values at your own risk.
173 ep_num ep_type n_fifo_slots max_pkt_fs
174 idx | n_bds | dir | max_pkt_hs |
176 [0] = { -1, 4, BCMEP_CTRL, BCMEP_OUT, 32, 64, 64 },
177 [1] = { 0, 4, BCMEP_CTRL, BCMEP_OUT, 32, 64, 64 },
178 [2] = { 2, 16, BCMEP_BULK, BCMEP_OUT, 128, 512, 64 },
179 [3] = { 1, 16, BCMEP_BULK, BCMEP_IN, 128, 512, 64 },
180 [4] = { 4, 4, BCMEP_INTR, BCMEP_OUT, 32, 64, 64 },
181 [5] = { 3, 4, BCMEP_INTR, BCMEP_IN, 32, 64, 64 },
187 * struct iudma_ch - Represents the current state of a single IUDMA channel.
188 * @ch_idx: IUDMA channel index (0 to BCM63XX_NUM_IUDMA-1).
189 * @ep_num: USB endpoint number. -1 for ep0 RX.
190 * @enabled: Whether bcm63xx_ep_enable() has been called.
191 * @max_pkt: "Chunk size" on the USB interface. Based on interface speed.
192 * @is_tx: true for TX, false for RX.
193 * @bep: Pointer to the associated endpoint. NULL for ep0 RX.
194 * @udc: Reference to the device controller.
195 * @read_bd: Next buffer descriptor to reap from the hardware.
196 * @write_bd: Next BD available for a new packet.
197 * @end_bd: Points to the final BD in the ring.
198 * @n_bds_used: Number of BD entries currently occupied.
199 * @bd_ring: Base pointer to the BD ring.
200 * @bd_ring_dma: Physical (DMA) address of bd_ring.
201 * @n_bds: Total number of BDs in the ring.
203 * ep0 has two IUDMA channels (IUDMA_EP0_RXCHAN and IUDMA_EP0_TXCHAN), as it is
204 * bidirectional. The "struct usb_ep" associated with ep0 is for TX (IN)
207 * Each bulk/intr endpoint has a single IUDMA channel and a single
216 struct bcm63xx_ep *bep;
217 struct bcm63xx_udc *udc;
219 struct bcm_enet_desc *read_bd;
220 struct bcm_enet_desc *write_bd;
221 struct bcm_enet_desc *end_bd;
224 struct bcm_enet_desc *bd_ring;
225 dma_addr_t bd_ring_dma;
230 * struct bcm63xx_ep - Internal (driver) state of a single endpoint.
231 * @ep_num: USB endpoint number.
232 * @iudma: Pointer to IUDMA channel state.
233 * @ep: USB gadget layer representation of the EP.
234 * @udc: Reference to the device controller.
235 * @queue: Linked list of outstanding requests for this EP.
236 * @halted: 1 if the EP is stalled; 0 otherwise.
240 struct iudma_ch *iudma;
242 struct bcm63xx_udc *udc;
243 struct list_head queue;
248 * struct bcm63xx_req - Internal (driver) state of a single request.
249 * @queue: Links back to the EP's request list.
250 * @req: USB gadget layer representation of the request.
251 * @offset: Current byte offset into the data buffer (next byte to queue).
252 * @bd_bytes: Number of data bytes in outstanding BD entries.
253 * @iudma: IUDMA channel used for the request.
256 struct list_head queue; /* ep's requests */
257 struct usb_request req;
259 unsigned int bd_bytes;
260 struct iudma_ch *iudma;
264 * struct bcm63xx_udc - Driver/hardware private context.
265 * @lock: Spinlock to mediate access to this struct, and (most) HW regs.
266 * @dev: Generic Linux device structure.
267 * @pd: Platform data (board/port info).
268 * @usbd_clk: Clock descriptor for the USB device block.
269 * @usbh_clk: Clock descriptor for the USB host block.
270 * @gadget: USB device.
271 * @driver: Driver for USB device.
272 * @usbd_regs: Base address of the USBD/USB20D block.
273 * @iudma_regs: Base address of the USBD's associated IUDMA block.
274 * @bep: Array of endpoints, including ep0.
275 * @iudma: Array of all IUDMA channels used by this controller.
276 * @cfg: USB configuration number, from SET_CONFIGURATION wValue.
277 * @iface: USB interface number, from SET_INTERFACE wIndex.
278 * @alt_iface: USB alt interface number, from SET_INTERFACE wValue.
279 * @ep0_ctrl_req: Request object for bcm63xx_udc-initiated ep0 transactions.
280 * @ep0_ctrl_buf: Data buffer for ep0_ctrl_req.
281 * @ep0state: Current state of the ep0 state machine.
282 * @ep0_wq: Workqueue struct used to wake up the ep0 state machine.
283 * @wedgemap: Bitmap of wedged endpoints.
284 * @ep0_req_reset: USB reset is pending.
285 * @ep0_req_set_cfg: Need to spoof a SET_CONFIGURATION packet.
286 * @ep0_req_set_iface: Need to spoof a SET_INTERFACE packet.
287 * @ep0_req_shutdown: Driver is shutting down; requesting ep0 to halt activity.
288 * @ep0_req_completed: ep0 request has completed; worker has not seen it yet.
289 * @ep0_reply: Pending reply from gadget driver.
290 * @ep0_request: Outstanding ep0 request.
291 * @debugfs_root: debugfs directory: /sys/kernel/debug/<DRV_MODULE_NAME>.
297 struct bcm63xx_usbd_platform_data *pd;
298 struct clk *usbd_clk;
299 struct clk *usbh_clk;
301 struct usb_gadget gadget;
302 struct usb_gadget_driver *driver;
304 void __iomem *usbd_regs;
305 void __iomem *iudma_regs;
307 struct bcm63xx_ep bep[BCM63XX_NUM_EP];
308 struct iudma_ch iudma[BCM63XX_NUM_IUDMA];
314 struct bcm63xx_req ep0_ctrl_req;
318 struct work_struct ep0_wq;
320 unsigned long wedgemap;
322 unsigned ep0_req_reset:1;
323 unsigned ep0_req_set_cfg:1;
324 unsigned ep0_req_set_iface:1;
325 unsigned ep0_req_shutdown:1;
327 unsigned ep0_req_completed:1;
328 struct usb_request *ep0_reply;
329 struct usb_request *ep0_request;
331 struct dentry *debugfs_root;
334 static const struct usb_ep_ops bcm63xx_udc_ep_ops;
336 /***********************************************************************
337 * Convenience functions
338 ***********************************************************************/
340 static inline struct bcm63xx_udc *gadget_to_udc(struct usb_gadget *g)
342 return container_of(g, struct bcm63xx_udc, gadget);
345 static inline struct bcm63xx_ep *our_ep(struct usb_ep *ep)
347 return container_of(ep, struct bcm63xx_ep, ep);
350 static inline struct bcm63xx_req *our_req(struct usb_request *req)
352 return container_of(req, struct bcm63xx_req, req);
355 static inline u32 usbd_readl(struct bcm63xx_udc *udc, u32 off)
357 return bcm_readl(udc->usbd_regs + off);
360 static inline void usbd_writel(struct bcm63xx_udc *udc, u32 val, u32 off)
362 bcm_writel(val, udc->usbd_regs + off);
365 static inline u32 usb_dma_readl(struct bcm63xx_udc *udc, u32 off)
367 return bcm_readl(udc->iudma_regs + off);
370 static inline void usb_dma_writel(struct bcm63xx_udc *udc, u32 val, u32 off)
372 bcm_writel(val, udc->iudma_regs + off);
375 static inline u32 usb_dmac_readl(struct bcm63xx_udc *udc, u32 off, int chan)
377 return bcm_readl(udc->iudma_regs + IUDMA_DMAC_OFFSET + off +
378 (ENETDMA_CHAN_WIDTH * chan));
381 static inline void usb_dmac_writel(struct bcm63xx_udc *udc, u32 val, u32 off,
384 bcm_writel(val, udc->iudma_regs + IUDMA_DMAC_OFFSET + off +
385 (ENETDMA_CHAN_WIDTH * chan));
388 static inline u32 usb_dmas_readl(struct bcm63xx_udc *udc, u32 off, int chan)
390 return bcm_readl(udc->iudma_regs + IUDMA_DMAS_OFFSET + off +
391 (ENETDMA_CHAN_WIDTH * chan));
394 static inline void usb_dmas_writel(struct bcm63xx_udc *udc, u32 val, u32 off,
397 bcm_writel(val, udc->iudma_regs + IUDMA_DMAS_OFFSET + off +
398 (ENETDMA_CHAN_WIDTH * chan));
401 static inline void set_clocks(struct bcm63xx_udc *udc, bool is_enabled)
404 clk_enable(udc->usbh_clk);
405 clk_enable(udc->usbd_clk);
408 clk_disable(udc->usbd_clk);
409 clk_disable(udc->usbh_clk);
413 /***********************************************************************
414 * Low-level IUDMA / FIFO operations
415 ***********************************************************************/
418 * bcm63xx_ep_dma_select - Helper function to set up the init_sel signal.
419 * @udc: Reference to the device controller.
420 * @idx: Desired init_sel value.
422 * The "init_sel" signal is used as a selection index for both endpoints
423 * and IUDMA channels. Since these do not map 1:1, the use of this signal
424 * depends on the context.
426 static void bcm63xx_ep_dma_select(struct bcm63xx_udc *udc, int idx)
428 u32 val = usbd_readl(udc, USBD_CONTROL_REG);
430 val &= ~USBD_CONTROL_INIT_SEL_MASK;
431 val |= idx << USBD_CONTROL_INIT_SEL_SHIFT;
432 usbd_writel(udc, val, USBD_CONTROL_REG);
436 * bcm63xx_set_stall - Enable/disable stall on one endpoint.
437 * @udc: Reference to the device controller.
438 * @bep: Endpoint on which to operate.
439 * @is_stalled: true to enable stall, false to disable.
441 * See notes in bcm63xx_update_wedge() regarding automatic clearing of
442 * halt/stall conditions.
444 static void bcm63xx_set_stall(struct bcm63xx_udc *udc, struct bcm63xx_ep *bep,
449 val = USBD_STALL_UPDATE_MASK |
450 (is_stalled ? USBD_STALL_ENABLE_MASK : 0) |
451 (bep->ep_num << USBD_STALL_EPNUM_SHIFT);
452 usbd_writel(udc, val, USBD_STALL_REG);
456 * bcm63xx_fifo_setup - (Re)initialize FIFO boundaries and settings.
457 * @udc: Reference to the device controller.
459 * These parameters depend on the USB link speed. Settings are
460 * per-IUDMA-channel-pair.
462 static void bcm63xx_fifo_setup(struct bcm63xx_udc *udc)
464 int is_hs = udc->gadget.speed == USB_SPEED_HIGH;
465 u32 i, val, rx_fifo_slot, tx_fifo_slot;
467 /* set up FIFO boundaries and packet sizes; this is done in pairs */
468 rx_fifo_slot = tx_fifo_slot = 0;
469 for (i = 0; i < BCM63XX_NUM_IUDMA; i += 2) {
470 const struct iudma_ch_cfg *rx_cfg = &iudma_defaults[i];
471 const struct iudma_ch_cfg *tx_cfg = &iudma_defaults[i + 1];
473 bcm63xx_ep_dma_select(udc, i >> 1);
475 val = (rx_fifo_slot << USBD_RXFIFO_CONFIG_START_SHIFT) |
476 ((rx_fifo_slot + rx_cfg->n_fifo_slots - 1) <<
477 USBD_RXFIFO_CONFIG_END_SHIFT);
478 rx_fifo_slot += rx_cfg->n_fifo_slots;
479 usbd_writel(udc, val, USBD_RXFIFO_CONFIG_REG);
481 is_hs ? rx_cfg->max_pkt_hs : rx_cfg->max_pkt_fs,
482 USBD_RXFIFO_EPSIZE_REG);
484 val = (tx_fifo_slot << USBD_TXFIFO_CONFIG_START_SHIFT) |
485 ((tx_fifo_slot + tx_cfg->n_fifo_slots - 1) <<
486 USBD_TXFIFO_CONFIG_END_SHIFT);
487 tx_fifo_slot += tx_cfg->n_fifo_slots;
488 usbd_writel(udc, val, USBD_TXFIFO_CONFIG_REG);
490 is_hs ? tx_cfg->max_pkt_hs : tx_cfg->max_pkt_fs,
491 USBD_TXFIFO_EPSIZE_REG);
493 usbd_readl(udc, USBD_TXFIFO_EPSIZE_REG);
498 * bcm63xx_fifo_reset_ep - Flush a single endpoint's FIFO.
499 * @udc: Reference to the device controller.
500 * @ep_num: Endpoint number.
502 static void bcm63xx_fifo_reset_ep(struct bcm63xx_udc *udc, int ep_num)
506 bcm63xx_ep_dma_select(udc, ep_num);
508 val = usbd_readl(udc, USBD_CONTROL_REG);
509 val |= USBD_CONTROL_FIFO_RESET_MASK;
510 usbd_writel(udc, val, USBD_CONTROL_REG);
511 usbd_readl(udc, USBD_CONTROL_REG);
515 * bcm63xx_fifo_reset - Flush all hardware FIFOs.
516 * @udc: Reference to the device controller.
518 static void bcm63xx_fifo_reset(struct bcm63xx_udc *udc)
522 for (i = 0; i < BCM63XX_NUM_FIFO_PAIRS; i++)
523 bcm63xx_fifo_reset_ep(udc, i);
527 * bcm63xx_ep_init - Initial (one-time) endpoint initialization.
528 * @udc: Reference to the device controller.
530 static void bcm63xx_ep_init(struct bcm63xx_udc *udc)
534 for (i = 0; i < BCM63XX_NUM_IUDMA; i++) {
535 const struct iudma_ch_cfg *cfg = &iudma_defaults[i];
540 bcm63xx_ep_dma_select(udc, cfg->ep_num);
541 val = (cfg->ep_type << USBD_EPNUM_TYPEMAP_TYPE_SHIFT) |
542 ((i >> 1) << USBD_EPNUM_TYPEMAP_DMA_CH_SHIFT);
543 usbd_writel(udc, val, USBD_EPNUM_TYPEMAP_REG);
548 * bcm63xx_ep_setup - Configure per-endpoint settings.
549 * @udc: Reference to the device controller.
551 * This needs to be rerun if the speed/cfg/intf/altintf changes.
553 static void bcm63xx_ep_setup(struct bcm63xx_udc *udc)
557 usbd_writel(udc, USBD_CSR_SETUPADDR_DEF, USBD_CSR_SETUPADDR_REG);
559 for (i = 0; i < BCM63XX_NUM_IUDMA; i++) {
560 const struct iudma_ch_cfg *cfg = &iudma_defaults[i];
561 int max_pkt = udc->gadget.speed == USB_SPEED_HIGH ?
562 cfg->max_pkt_hs : cfg->max_pkt_fs;
563 int idx = cfg->ep_num;
565 udc->iudma[i].max_pkt = max_pkt;
569 usb_ep_set_maxpacket_limit(&udc->bep[idx].ep, max_pkt);
571 val = (idx << USBD_CSR_EP_LOG_SHIFT) |
572 (cfg->dir << USBD_CSR_EP_DIR_SHIFT) |
573 (cfg->ep_type << USBD_CSR_EP_TYPE_SHIFT) |
574 (udc->cfg << USBD_CSR_EP_CFG_SHIFT) |
575 (udc->iface << USBD_CSR_EP_IFACE_SHIFT) |
576 (udc->alt_iface << USBD_CSR_EP_ALTIFACE_SHIFT) |
577 (max_pkt << USBD_CSR_EP_MAXPKT_SHIFT);
578 usbd_writel(udc, val, USBD_CSR_EP_REG(idx));
583 * iudma_write - Queue a single IUDMA transaction.
584 * @udc: Reference to the device controller.
585 * @iudma: IUDMA channel to use.
586 * @breq: Request containing the transaction data.
588 * For RX IUDMA, this will queue a single buffer descriptor, as RX IUDMA
589 * does not honor SOP/EOP so the handling of multiple buffers is ambiguous.
590 * So iudma_write() may be called several times to fulfill a single
593 * For TX IUDMA, this can queue multiple buffer descriptors if needed.
595 static void iudma_write(struct bcm63xx_udc *udc, struct iudma_ch *iudma,
596 struct bcm63xx_req *breq)
598 int first_bd = 1, last_bd = 0, extra_zero_pkt = 0;
599 unsigned int bytes_left = breq->req.length - breq->offset;
600 const int max_bd_bytes = !irq_coalesce && !iudma->is_tx ?
601 iudma->max_pkt : IUDMA_MAX_FRAGMENT;
603 iudma->n_bds_used = 0;
607 if ((bytes_left % iudma->max_pkt == 0) && bytes_left && breq->req.zero)
611 struct bcm_enet_desc *d = iudma->write_bd;
613 unsigned int n_bytes;
615 if (d == iudma->end_bd) {
616 dmaflags |= DMADESC_WRAP_MASK;
617 iudma->write_bd = iudma->bd_ring;
623 n_bytes = min_t(int, bytes_left, max_bd_bytes);
625 dmaflags |= n_bytes << DMADESC_LENGTH_SHIFT;
627 dmaflags |= (1 << DMADESC_LENGTH_SHIFT) |
628 DMADESC_USB_ZERO_MASK;
630 dmaflags |= DMADESC_OWNER_MASK;
632 dmaflags |= DMADESC_SOP_MASK;
637 * extra_zero_pkt forces one more iteration through the loop
638 * after all data is queued up, to send the zero packet
640 if (extra_zero_pkt && !bytes_left)
643 if (!iudma->is_tx || iudma->n_bds_used == iudma->n_bds ||
644 (n_bytes == bytes_left && !extra_zero_pkt)) {
646 dmaflags |= DMADESC_EOP_MASK;
649 d->address = breq->req.dma + breq->offset;
651 d->len_stat = dmaflags;
653 breq->offset += n_bytes;
654 breq->bd_bytes += n_bytes;
655 bytes_left -= n_bytes;
658 usb_dmac_writel(udc, ENETDMAC_CHANCFG_EN_MASK,
659 ENETDMAC_CHANCFG_REG, iudma->ch_idx);
663 * iudma_read - Check for IUDMA buffer completion.
664 * @udc: Reference to the device controller.
665 * @iudma: IUDMA channel to use.
667 * This checks to see if ALL of the outstanding BDs on the DMA channel
668 * have been filled. If so, it returns the actual transfer length;
669 * otherwise it returns -EBUSY.
671 static int iudma_read(struct bcm63xx_udc *udc, struct iudma_ch *iudma)
673 int i, actual_len = 0;
674 struct bcm_enet_desc *d = iudma->read_bd;
676 if (!iudma->n_bds_used)
679 for (i = 0; i < iudma->n_bds_used; i++) {
682 dmaflags = d->len_stat;
684 if (dmaflags & DMADESC_OWNER_MASK)
687 actual_len += (dmaflags & DMADESC_LENGTH_MASK) >>
688 DMADESC_LENGTH_SHIFT;
689 if (d == iudma->end_bd)
696 iudma->n_bds_used = 0;
701 * iudma_reset_channel - Stop DMA on a single channel.
702 * @udc: Reference to the device controller.
703 * @iudma: IUDMA channel to reset.
705 static void iudma_reset_channel(struct bcm63xx_udc *udc, struct iudma_ch *iudma)
707 int timeout = IUDMA_RESET_TIMEOUT_US;
708 struct bcm_enet_desc *d;
709 int ch_idx = iudma->ch_idx;
712 bcm63xx_fifo_reset_ep(udc, max(0, iudma->ep_num));
714 /* stop DMA, then wait for the hardware to wrap up */
715 usb_dmac_writel(udc, 0, ENETDMAC_CHANCFG_REG, ch_idx);
717 while (usb_dmac_readl(udc, ENETDMAC_CHANCFG_REG, ch_idx) &
718 ENETDMAC_CHANCFG_EN_MASK) {
721 /* repeatedly flush the FIFO data until the BD completes */
722 if (iudma->is_tx && iudma->ep_num >= 0)
723 bcm63xx_fifo_reset_ep(udc, iudma->ep_num);
726 dev_err(udc->dev, "can't reset IUDMA channel %d\n",
730 if (timeout == IUDMA_RESET_TIMEOUT_US / 2) {
731 dev_warn(udc->dev, "forcibly halting IUDMA channel %d\n",
733 usb_dmac_writel(udc, ENETDMAC_CHANCFG_BUFHALT_MASK,
734 ENETDMAC_CHANCFG_REG, ch_idx);
737 usb_dmac_writel(udc, ~0, ENETDMAC_IR_REG, ch_idx);
739 /* don't leave "live" HW-owned entries for the next guy to step on */
740 for (d = iudma->bd_ring; d <= iudma->end_bd; d++)
744 iudma->read_bd = iudma->write_bd = iudma->bd_ring;
745 iudma->n_bds_used = 0;
747 /* set up IRQs, UBUS burst size, and BD base for this channel */
748 usb_dmac_writel(udc, ENETDMAC_IR_BUFDONE_MASK,
749 ENETDMAC_IRMASK_REG, ch_idx);
750 usb_dmac_writel(udc, 8, ENETDMAC_MAXBURST_REG, ch_idx);
752 usb_dmas_writel(udc, iudma->bd_ring_dma, ENETDMAS_RSTART_REG, ch_idx);
753 usb_dmas_writel(udc, 0, ENETDMAS_SRAM2_REG, ch_idx);
757 * iudma_init_channel - One-time IUDMA channel initialization.
758 * @udc: Reference to the device controller.
759 * @ch_idx: Channel to initialize.
761 static int iudma_init_channel(struct bcm63xx_udc *udc, unsigned int ch_idx)
763 struct iudma_ch *iudma = &udc->iudma[ch_idx];
764 const struct iudma_ch_cfg *cfg = &iudma_defaults[ch_idx];
765 unsigned int n_bds = cfg->n_bds;
766 struct bcm63xx_ep *bep = NULL;
768 iudma->ep_num = cfg->ep_num;
769 iudma->ch_idx = ch_idx;
770 iudma->is_tx = !!(ch_idx & 0x01);
771 if (iudma->ep_num >= 0) {
772 bep = &udc->bep[iudma->ep_num];
774 INIT_LIST_HEAD(&bep->queue);
780 /* ep0 is always active; others are controlled by the gadget driver */
781 if (iudma->ep_num <= 0)
782 iudma->enabled = true;
784 iudma->n_bds = n_bds;
785 iudma->bd_ring = dmam_alloc_coherent(udc->dev,
786 n_bds * sizeof(struct bcm_enet_desc),
787 &iudma->bd_ring_dma, GFP_KERNEL);
790 iudma->end_bd = &iudma->bd_ring[n_bds - 1];
796 * iudma_init - One-time initialization of all IUDMA channels.
797 * @udc: Reference to the device controller.
799 * Enable DMA, flush channels, and enable global IUDMA IRQs.
801 static int iudma_init(struct bcm63xx_udc *udc)
805 usb_dma_writel(udc, ENETDMA_CFG_EN_MASK, ENETDMA_CFG_REG);
807 for (i = 0; i < BCM63XX_NUM_IUDMA; i++) {
808 rc = iudma_init_channel(udc, i);
811 iudma_reset_channel(udc, &udc->iudma[i]);
814 usb_dma_writel(udc, BIT(BCM63XX_NUM_IUDMA)-1, ENETDMA_GLB_IRQMASK_REG);
819 * iudma_uninit - Uninitialize IUDMA channels.
820 * @udc: Reference to the device controller.
822 * Kill global IUDMA IRQs, flush channels, and kill DMA.
824 static void iudma_uninit(struct bcm63xx_udc *udc)
828 usb_dma_writel(udc, 0, ENETDMA_GLB_IRQMASK_REG);
830 for (i = 0; i < BCM63XX_NUM_IUDMA; i++)
831 iudma_reset_channel(udc, &udc->iudma[i]);
833 usb_dma_writel(udc, 0, ENETDMA_CFG_REG);
836 /***********************************************************************
837 * Other low-level USBD operations
838 ***********************************************************************/
841 * bcm63xx_set_ctrl_irqs - Mask/unmask control path interrupts.
842 * @udc: Reference to the device controller.
843 * @enable_irqs: true to enable, false to disable.
845 static void bcm63xx_set_ctrl_irqs(struct bcm63xx_udc *udc, bool enable_irqs)
849 usbd_writel(udc, 0, USBD_STATUS_REG);
851 val = BIT(USBD_EVENT_IRQ_USB_RESET) |
852 BIT(USBD_EVENT_IRQ_SETUP) |
853 BIT(USBD_EVENT_IRQ_SETCFG) |
854 BIT(USBD_EVENT_IRQ_SETINTF) |
855 BIT(USBD_EVENT_IRQ_USB_LINK);
856 usbd_writel(udc, enable_irqs ? val : 0, USBD_EVENT_IRQ_MASK_REG);
857 usbd_writel(udc, val, USBD_EVENT_IRQ_STATUS_REG);
861 * bcm63xx_select_phy_mode - Select between USB device and host mode.
862 * @udc: Reference to the device controller.
863 * @is_device: true for device, false for host.
865 * This should probably be reworked to use the drivers/usb/otg
868 * By default, the AFE/pullups are disabled in device mode, until
869 * bcm63xx_select_pullup() is called.
871 static void bcm63xx_select_phy_mode(struct bcm63xx_udc *udc, bool is_device)
873 u32 val, portmask = BIT(udc->pd->port_no);
875 if (BCMCPU_IS_6328()) {
876 /* configure pinmux to sense VBUS signal */
877 val = bcm_gpio_readl(GPIO_PINMUX_OTHR_REG);
878 val &= ~GPIO_PINMUX_OTHR_6328_USB_MASK;
879 val |= is_device ? GPIO_PINMUX_OTHR_6328_USB_DEV :
880 GPIO_PINMUX_OTHR_6328_USB_HOST;
881 bcm_gpio_writel(val, GPIO_PINMUX_OTHR_REG);
884 val = bcm_rset_readl(RSET_USBH_PRIV, USBH_PRIV_UTMI_CTL_6368_REG);
886 val |= (portmask << USBH_PRIV_UTMI_CTL_HOSTB_SHIFT);
887 val |= (portmask << USBH_PRIV_UTMI_CTL_NODRIV_SHIFT);
889 val &= ~(portmask << USBH_PRIV_UTMI_CTL_HOSTB_SHIFT);
890 val &= ~(portmask << USBH_PRIV_UTMI_CTL_NODRIV_SHIFT);
892 bcm_rset_writel(RSET_USBH_PRIV, val, USBH_PRIV_UTMI_CTL_6368_REG);
894 val = bcm_rset_readl(RSET_USBH_PRIV, USBH_PRIV_SWAP_6368_REG);
896 val |= USBH_PRIV_SWAP_USBD_MASK;
898 val &= ~USBH_PRIV_SWAP_USBD_MASK;
899 bcm_rset_writel(RSET_USBH_PRIV, val, USBH_PRIV_SWAP_6368_REG);
903 * bcm63xx_select_pullup - Enable/disable the pullup on D+
904 * @udc: Reference to the device controller.
905 * @is_on: true to enable the pullup, false to disable.
907 * If the pullup is active, the host will sense a FS/HS device connected to
908 * the port. If the pullup is inactive, the host will think the USB
909 * device has been disconnected.
911 static void bcm63xx_select_pullup(struct bcm63xx_udc *udc, bool is_on)
913 u32 val, portmask = BIT(udc->pd->port_no);
915 val = bcm_rset_readl(RSET_USBH_PRIV, USBH_PRIV_UTMI_CTL_6368_REG);
917 val &= ~(portmask << USBH_PRIV_UTMI_CTL_NODRIV_SHIFT);
919 val |= (portmask << USBH_PRIV_UTMI_CTL_NODRIV_SHIFT);
920 bcm_rset_writel(RSET_USBH_PRIV, val, USBH_PRIV_UTMI_CTL_6368_REG);
924 * bcm63xx_uninit_udc_hw - Shut down the hardware prior to driver removal.
925 * @udc: Reference to the device controller.
927 * This just masks the IUDMA IRQs and releases the clocks. It is assumed
928 * that bcm63xx_udc_stop() has already run, and the clocks are stopped.
930 static void bcm63xx_uninit_udc_hw(struct bcm63xx_udc *udc)
932 set_clocks(udc, true);
934 set_clocks(udc, false);
936 clk_put(udc->usbd_clk);
937 clk_put(udc->usbh_clk);
941 * bcm63xx_init_udc_hw - Initialize the controller hardware and data structures.
942 * @udc: Reference to the device controller.
944 static int bcm63xx_init_udc_hw(struct bcm63xx_udc *udc)
949 udc->ep0_ctrl_buf = devm_kzalloc(udc->dev, BCM63XX_MAX_CTRL_PKT,
951 if (!udc->ep0_ctrl_buf)
954 INIT_LIST_HEAD(&udc->gadget.ep_list);
955 for (i = 0; i < BCM63XX_NUM_EP; i++) {
956 struct bcm63xx_ep *bep = &udc->bep[i];
958 bep->ep.name = bcm63xx_ep_info[i].name;
959 bep->ep.caps = bcm63xx_ep_info[i].caps;
961 bep->ep.ops = &bcm63xx_udc_ep_ops;
962 list_add_tail(&bep->ep.ep_list, &udc->gadget.ep_list);
964 usb_ep_set_maxpacket_limit(&bep->ep, BCM63XX_MAX_CTRL_PKT);
967 INIT_LIST_HEAD(&bep->queue);
970 udc->gadget.ep0 = &udc->bep[0].ep;
971 list_del(&udc->bep[0].ep.ep_list);
973 udc->gadget.speed = USB_SPEED_UNKNOWN;
974 udc->ep0state = EP0_SHUTDOWN;
976 udc->usbh_clk = clk_get(udc->dev, "usbh");
977 if (IS_ERR(udc->usbh_clk))
980 udc->usbd_clk = clk_get(udc->dev, "usbd");
981 if (IS_ERR(udc->usbd_clk)) {
982 clk_put(udc->usbh_clk);
986 set_clocks(udc, true);
988 val = USBD_CONTROL_AUTO_CSRS_MASK |
989 USBD_CONTROL_DONE_CSRS_MASK |
990 (irq_coalesce ? USBD_CONTROL_RXZSCFG_MASK : 0);
991 usbd_writel(udc, val, USBD_CONTROL_REG);
993 val = USBD_STRAPS_APP_SELF_PWR_MASK |
994 USBD_STRAPS_APP_RAM_IF_MASK |
995 USBD_STRAPS_APP_CSRPRGSUP_MASK |
996 USBD_STRAPS_APP_8BITPHY_MASK |
997 USBD_STRAPS_APP_RMTWKUP_MASK;
999 if (udc->gadget.max_speed == USB_SPEED_HIGH)
1000 val |= (BCM63XX_SPD_HIGH << USBD_STRAPS_SPEED_SHIFT);
1002 val |= (BCM63XX_SPD_FULL << USBD_STRAPS_SPEED_SHIFT);
1003 usbd_writel(udc, val, USBD_STRAPS_REG);
1005 bcm63xx_set_ctrl_irqs(udc, false);
1007 usbd_writel(udc, 0, USBD_EVENT_IRQ_CFG_LO_REG);
1009 val = USBD_EVENT_IRQ_CFG_FALLING(USBD_EVENT_IRQ_ENUM_ON) |
1010 USBD_EVENT_IRQ_CFG_FALLING(USBD_EVENT_IRQ_SET_CSRS);
1011 usbd_writel(udc, val, USBD_EVENT_IRQ_CFG_HI_REG);
1013 rc = iudma_init(udc);
1014 set_clocks(udc, false);
1016 bcm63xx_uninit_udc_hw(udc);
1021 /***********************************************************************
1022 * Standard EP gadget operations
1023 ***********************************************************************/
1026 * bcm63xx_ep_enable - Enable one endpoint.
1027 * @ep: Endpoint to enable.
1028 * @desc: Contains max packet, direction, etc.
1030 * Most of the endpoint parameters are fixed in this controller, so there
1031 * isn't much for this function to do.
1033 static int bcm63xx_ep_enable(struct usb_ep *ep,
1034 const struct usb_endpoint_descriptor *desc)
1036 struct bcm63xx_ep *bep = our_ep(ep);
1037 struct bcm63xx_udc *udc = bep->udc;
1038 struct iudma_ch *iudma = bep->iudma;
1039 unsigned long flags;
1041 if (!ep || !desc || ep->name == bcm63xx_ep0name)
1047 spin_lock_irqsave(&udc->lock, flags);
1048 if (iudma->enabled) {
1049 spin_unlock_irqrestore(&udc->lock, flags);
1053 iudma->enabled = true;
1054 BUG_ON(!list_empty(&bep->queue));
1056 iudma_reset_channel(udc, iudma);
1059 bcm63xx_set_stall(udc, bep, false);
1060 clear_bit(bep->ep_num, &udc->wedgemap);
1063 ep->maxpacket = usb_endpoint_maxp(desc);
1065 spin_unlock_irqrestore(&udc->lock, flags);
1070 * bcm63xx_ep_disable - Disable one endpoint.
1071 * @ep: Endpoint to disable.
1073 static int bcm63xx_ep_disable(struct usb_ep *ep)
1075 struct bcm63xx_ep *bep = our_ep(ep);
1076 struct bcm63xx_udc *udc = bep->udc;
1077 struct iudma_ch *iudma = bep->iudma;
1078 struct bcm63xx_req *breq, *n;
1079 unsigned long flags;
1081 if (!ep || !ep->desc)
1084 spin_lock_irqsave(&udc->lock, flags);
1085 if (!iudma->enabled) {
1086 spin_unlock_irqrestore(&udc->lock, flags);
1089 iudma->enabled = false;
1091 iudma_reset_channel(udc, iudma);
1093 if (!list_empty(&bep->queue)) {
1094 list_for_each_entry_safe(breq, n, &bep->queue, queue) {
1095 usb_gadget_unmap_request(&udc->gadget, &breq->req,
1097 list_del(&breq->queue);
1098 breq->req.status = -ESHUTDOWN;
1100 spin_unlock_irqrestore(&udc->lock, flags);
1101 usb_gadget_giveback_request(&iudma->bep->ep, &breq->req);
1102 spin_lock_irqsave(&udc->lock, flags);
1107 spin_unlock_irqrestore(&udc->lock, flags);
1112 * bcm63xx_udc_alloc_request - Allocate a new request.
1113 * @ep: Endpoint associated with the request.
1114 * @mem_flags: Flags to pass to kzalloc().
1116 static struct usb_request *bcm63xx_udc_alloc_request(struct usb_ep *ep,
1119 struct bcm63xx_req *breq;
1121 breq = kzalloc(sizeof(*breq), mem_flags);
1128 * bcm63xx_udc_free_request - Free a request.
1129 * @ep: Endpoint associated with the request.
1130 * @req: Request to free.
1132 static void bcm63xx_udc_free_request(struct usb_ep *ep,
1133 struct usb_request *req)
1135 struct bcm63xx_req *breq = our_req(req);
1140 * bcm63xx_udc_queue - Queue up a new request.
1141 * @ep: Endpoint associated with the request.
1142 * @req: Request to add.
1143 * @mem_flags: Unused.
1145 * If the queue is empty, start this request immediately. Otherwise, add
1148 * ep0 replies are sent through this function from the gadget driver, but
1149 * they are treated differently because they need to be handled by the ep0
1150 * state machine. (Sometimes they are replies to control requests that
1151 * were spoofed by this driver, and so they shouldn't be transmitted at all.)
1153 static int bcm63xx_udc_queue(struct usb_ep *ep, struct usb_request *req,
1156 struct bcm63xx_ep *bep = our_ep(ep);
1157 struct bcm63xx_udc *udc = bep->udc;
1158 struct bcm63xx_req *breq = our_req(req);
1159 unsigned long flags;
1162 if (unlikely(!req || !req->complete || !req->buf || !ep))
1169 if (bep == &udc->bep[0]) {
1170 /* only one reply per request, please */
1174 udc->ep0_reply = req;
1175 schedule_work(&udc->ep0_wq);
1179 spin_lock_irqsave(&udc->lock, flags);
1180 if (!bep->iudma->enabled) {
1185 rc = usb_gadget_map_request(&udc->gadget, req, bep->iudma->is_tx);
1187 list_add_tail(&breq->queue, &bep->queue);
1188 if (list_is_singular(&bep->queue))
1189 iudma_write(udc, bep->iudma, breq);
1193 spin_unlock_irqrestore(&udc->lock, flags);
1198 * bcm63xx_udc_dequeue - Remove a pending request from the queue.
1199 * @ep: Endpoint associated with the request.
1200 * @req: Request to remove.
1202 * If the request is not at the head of the queue, this is easy - just nuke
1203 * it. If the request is at the head of the queue, we'll need to stop the
1204 * DMA transaction and then queue up the successor.
1206 static int bcm63xx_udc_dequeue(struct usb_ep *ep, struct usb_request *req)
1208 struct bcm63xx_ep *bep = our_ep(ep);
1209 struct bcm63xx_udc *udc = bep->udc;
1210 struct bcm63xx_req *breq = our_req(req), *cur;
1211 unsigned long flags;
1214 spin_lock_irqsave(&udc->lock, flags);
1215 if (list_empty(&bep->queue)) {
1220 cur = list_first_entry(&bep->queue, struct bcm63xx_req, queue);
1221 usb_gadget_unmap_request(&udc->gadget, &breq->req, bep->iudma->is_tx);
1224 iudma_reset_channel(udc, bep->iudma);
1225 list_del(&breq->queue);
1227 if (!list_empty(&bep->queue)) {
1228 struct bcm63xx_req *next;
1230 next = list_first_entry(&bep->queue,
1231 struct bcm63xx_req, queue);
1232 iudma_write(udc, bep->iudma, next);
1235 list_del(&breq->queue);
1239 spin_unlock_irqrestore(&udc->lock, flags);
1241 req->status = -ESHUTDOWN;
1242 req->complete(ep, req);
1248 * bcm63xx_udc_set_halt - Enable/disable STALL flag in the hardware.
1249 * @ep: Endpoint to halt.
1250 * @value: Zero to clear halt; nonzero to set halt.
1252 * See comments in bcm63xx_update_wedge().
1254 static int bcm63xx_udc_set_halt(struct usb_ep *ep, int value)
1256 struct bcm63xx_ep *bep = our_ep(ep);
1257 struct bcm63xx_udc *udc = bep->udc;
1258 unsigned long flags;
1260 spin_lock_irqsave(&udc->lock, flags);
1261 bcm63xx_set_stall(udc, bep, !!value);
1262 bep->halted = value;
1263 spin_unlock_irqrestore(&udc->lock, flags);
1269 * bcm63xx_udc_set_wedge - Stall the endpoint until the next reset.
1270 * @ep: Endpoint to wedge.
1272 * See comments in bcm63xx_update_wedge().
1274 static int bcm63xx_udc_set_wedge(struct usb_ep *ep)
1276 struct bcm63xx_ep *bep = our_ep(ep);
1277 struct bcm63xx_udc *udc = bep->udc;
1278 unsigned long flags;
1280 spin_lock_irqsave(&udc->lock, flags);
1281 set_bit(bep->ep_num, &udc->wedgemap);
1282 bcm63xx_set_stall(udc, bep, true);
1283 spin_unlock_irqrestore(&udc->lock, flags);
1288 static const struct usb_ep_ops bcm63xx_udc_ep_ops = {
1289 .enable = bcm63xx_ep_enable,
1290 .disable = bcm63xx_ep_disable,
1292 .alloc_request = bcm63xx_udc_alloc_request,
1293 .free_request = bcm63xx_udc_free_request,
1295 .queue = bcm63xx_udc_queue,
1296 .dequeue = bcm63xx_udc_dequeue,
1298 .set_halt = bcm63xx_udc_set_halt,
1299 .set_wedge = bcm63xx_udc_set_wedge,
1302 /***********************************************************************
1304 ***********************************************************************/
1307 * bcm63xx_ep0_setup_callback - Drop spinlock to invoke ->setup callback.
1308 * @udc: Reference to the device controller.
1309 * @ctrl: 8-byte SETUP request.
1311 static int bcm63xx_ep0_setup_callback(struct bcm63xx_udc *udc,
1312 struct usb_ctrlrequest *ctrl)
1316 spin_unlock_irq(&udc->lock);
1317 rc = udc->driver->setup(&udc->gadget, ctrl);
1318 spin_lock_irq(&udc->lock);
1323 * bcm63xx_ep0_spoof_set_cfg - Synthesize a SET_CONFIGURATION request.
1324 * @udc: Reference to the device controller.
1326 * Many standard requests are handled automatically in the hardware, but
1327 * we still need to pass them to the gadget driver so that it can
1328 * reconfigure the interfaces/endpoints if necessary.
1330 * Unfortunately we are not able to send a STALL response if the host
1331 * requests an invalid configuration. If this happens, we'll have to be
1332 * content with printing a warning.
1334 static int bcm63xx_ep0_spoof_set_cfg(struct bcm63xx_udc *udc)
1336 struct usb_ctrlrequest ctrl;
1339 ctrl.bRequestType = USB_DIR_OUT | USB_RECIP_DEVICE;
1340 ctrl.bRequest = USB_REQ_SET_CONFIGURATION;
1341 ctrl.wValue = cpu_to_le16(udc->cfg);
1345 rc = bcm63xx_ep0_setup_callback(udc, &ctrl);
1347 dev_warn_ratelimited(udc->dev,
1348 "hardware auto-acked bad SET_CONFIGURATION(%d) request\n",
1355 * bcm63xx_ep0_spoof_set_iface - Synthesize a SET_INTERFACE request.
1356 * @udc: Reference to the device controller.
1358 static int bcm63xx_ep0_spoof_set_iface(struct bcm63xx_udc *udc)
1360 struct usb_ctrlrequest ctrl;
1363 ctrl.bRequestType = USB_DIR_OUT | USB_RECIP_INTERFACE;
1364 ctrl.bRequest = USB_REQ_SET_INTERFACE;
1365 ctrl.wValue = cpu_to_le16(udc->alt_iface);
1366 ctrl.wIndex = cpu_to_le16(udc->iface);
1369 rc = bcm63xx_ep0_setup_callback(udc, &ctrl);
1371 dev_warn_ratelimited(udc->dev,
1372 "hardware auto-acked bad SET_INTERFACE(%d,%d) request\n",
1373 udc->iface, udc->alt_iface);
1379 * bcm63xx_ep0_map_write - dma_map and iudma_write a single request.
1380 * @udc: Reference to the device controller.
1381 * @ch_idx: IUDMA channel number.
1382 * @req: USB gadget layer representation of the request.
1384 static void bcm63xx_ep0_map_write(struct bcm63xx_udc *udc, int ch_idx,
1385 struct usb_request *req)
1387 struct bcm63xx_req *breq = our_req(req);
1388 struct iudma_ch *iudma = &udc->iudma[ch_idx];
1390 BUG_ON(udc->ep0_request);
1391 udc->ep0_request = req;
1395 usb_gadget_map_request(&udc->gadget, req, iudma->is_tx);
1396 iudma_write(udc, iudma, breq);
1400 * bcm63xx_ep0_complete - Set completion status and "stage" the callback.
1401 * @udc: Reference to the device controller.
1402 * @req: USB gadget layer representation of the request.
1403 * @status: Status to return to the gadget driver.
1405 static void bcm63xx_ep0_complete(struct bcm63xx_udc *udc,
1406 struct usb_request *req, int status)
1408 req->status = status;
1411 if (req->complete) {
1412 spin_unlock_irq(&udc->lock);
1413 req->complete(&udc->bep[0].ep, req);
1414 spin_lock_irq(&udc->lock);
1419 * bcm63xx_ep0_nuke_reply - Abort request from the gadget driver due to
1421 * @udc: Reference to the device controller.
1422 * @is_tx: Nonzero for TX (IN), zero for RX (OUT).
1424 static void bcm63xx_ep0_nuke_reply(struct bcm63xx_udc *udc, int is_tx)
1426 struct usb_request *req = udc->ep0_reply;
1428 udc->ep0_reply = NULL;
1429 usb_gadget_unmap_request(&udc->gadget, req, is_tx);
1430 if (udc->ep0_request == req) {
1431 udc->ep0_req_completed = 0;
1432 udc->ep0_request = NULL;
1434 bcm63xx_ep0_complete(udc, req, -ESHUTDOWN);
1438 * bcm63xx_ep0_read_complete - Close out the pending ep0 request; return
1440 * @udc: Reference to the device controller.
1442 static int bcm63xx_ep0_read_complete(struct bcm63xx_udc *udc)
1444 struct usb_request *req = udc->ep0_request;
1446 udc->ep0_req_completed = 0;
1447 udc->ep0_request = NULL;
1453 * bcm63xx_ep0_internal_request - Helper function to submit an ep0 request.
1454 * @udc: Reference to the device controller.
1455 * @ch_idx: IUDMA channel number.
1456 * @length: Number of bytes to TX/RX.
1458 * Used for simple transfers performed by the ep0 worker. This will always
1459 * use ep0_ctrl_req / ep0_ctrl_buf.
1461 static void bcm63xx_ep0_internal_request(struct bcm63xx_udc *udc, int ch_idx,
1464 struct usb_request *req = &udc->ep0_ctrl_req.req;
1466 req->buf = udc->ep0_ctrl_buf;
1467 req->length = length;
1468 req->complete = NULL;
1470 bcm63xx_ep0_map_write(udc, ch_idx, req);
1474 * bcm63xx_ep0_do_setup - Parse new SETUP packet and decide how to handle it.
1475 * @udc: Reference to the device controller.
1477 * EP0_IDLE probably shouldn't ever happen. EP0_REQUEUE means we're ready
1478 * for the next packet. Anything else means the transaction requires multiple
1479 * stages of handling.
1481 static enum bcm63xx_ep0_state bcm63xx_ep0_do_setup(struct bcm63xx_udc *udc)
1484 struct usb_ctrlrequest *ctrl = (void *)udc->ep0_ctrl_buf;
1486 rc = bcm63xx_ep0_read_complete(udc);
1489 dev_err(udc->dev, "missing SETUP packet\n");
1494 * Handle 0-byte IN STATUS acknowledgement. The hardware doesn't
1495 * ALWAYS deliver these 100% of the time, so if we happen to see one,
1496 * just throw it away.
1501 /* Drop malformed SETUP packets */
1502 if (rc != sizeof(*ctrl)) {
1503 dev_warn_ratelimited(udc->dev,
1504 "malformed SETUP packet (%d bytes)\n", rc);
1508 /* Process new SETUP packet arriving on ep0 */
1509 rc = bcm63xx_ep0_setup_callback(udc, ctrl);
1511 bcm63xx_set_stall(udc, &udc->bep[0], true);
1517 else if (ctrl->bRequestType & USB_DIR_IN)
1518 return EP0_IN_DATA_PHASE_SETUP;
1520 return EP0_OUT_DATA_PHASE_SETUP;
1524 * bcm63xx_ep0_do_idle - Check for outstanding requests if ep0 is idle.
1525 * @udc: Reference to the device controller.
1527 * In state EP0_IDLE, the RX descriptor is either pending, or has been
1528 * filled with a SETUP packet from the host. This function handles new
1529 * SETUP packets, control IRQ events (which can generate fake SETUP packets),
1530 * and reset/shutdown events.
1532 * Returns 0 if work was done; -EAGAIN if nothing to do.
1534 static int bcm63xx_ep0_do_idle(struct bcm63xx_udc *udc)
1536 if (udc->ep0_req_reset) {
1537 udc->ep0_req_reset = 0;
1538 } else if (udc->ep0_req_set_cfg) {
1539 udc->ep0_req_set_cfg = 0;
1540 if (bcm63xx_ep0_spoof_set_cfg(udc) >= 0)
1541 udc->ep0state = EP0_IN_FAKE_STATUS_PHASE;
1542 } else if (udc->ep0_req_set_iface) {
1543 udc->ep0_req_set_iface = 0;
1544 if (bcm63xx_ep0_spoof_set_iface(udc) >= 0)
1545 udc->ep0state = EP0_IN_FAKE_STATUS_PHASE;
1546 } else if (udc->ep0_req_completed) {
1547 udc->ep0state = bcm63xx_ep0_do_setup(udc);
1548 return udc->ep0state == EP0_IDLE ? -EAGAIN : 0;
1549 } else if (udc->ep0_req_shutdown) {
1550 udc->ep0_req_shutdown = 0;
1551 udc->ep0_req_completed = 0;
1552 udc->ep0_request = NULL;
1553 iudma_reset_channel(udc, &udc->iudma[IUDMA_EP0_RXCHAN]);
1554 usb_gadget_unmap_request(&udc->gadget,
1555 &udc->ep0_ctrl_req.req, 0);
1557 /* bcm63xx_udc_pullup() is waiting for this */
1559 udc->ep0state = EP0_SHUTDOWN;
1560 } else if (udc->ep0_reply) {
1562 * This could happen if a USB RESET shows up during an ep0
1563 * transaction (especially if a laggy driver like gadgetfs
1566 dev_warn(udc->dev, "nuking unexpected reply\n");
1567 bcm63xx_ep0_nuke_reply(udc, 0);
1576 * bcm63xx_ep0_one_round - Handle the current ep0 state.
1577 * @udc: Reference to the device controller.
1579 * Returns 0 if work was done; -EAGAIN if nothing to do.
1581 static int bcm63xx_ep0_one_round(struct bcm63xx_udc *udc)
1583 enum bcm63xx_ep0_state ep0state = udc->ep0state;
1584 bool shutdown = udc->ep0_req_reset || udc->ep0_req_shutdown;
1586 switch (udc->ep0state) {
1588 /* set up descriptor to receive SETUP packet */
1589 bcm63xx_ep0_internal_request(udc, IUDMA_EP0_RXCHAN,
1590 BCM63XX_MAX_CTRL_PKT);
1591 ep0state = EP0_IDLE;
1594 return bcm63xx_ep0_do_idle(udc);
1595 case EP0_IN_DATA_PHASE_SETUP:
1597 * Normal case: TX request is in ep0_reply (queued by the
1598 * callback), or will be queued shortly. When it's here,
1599 * send it to the HW and go to EP0_IN_DATA_PHASE_COMPLETE.
1601 * Shutdown case: Stop waiting for the reply. Just
1602 * REQUEUE->IDLE. The gadget driver is NOT expected to
1603 * queue anything else now.
1605 if (udc->ep0_reply) {
1606 bcm63xx_ep0_map_write(udc, IUDMA_EP0_TXCHAN,
1608 ep0state = EP0_IN_DATA_PHASE_COMPLETE;
1609 } else if (shutdown) {
1610 ep0state = EP0_REQUEUE;
1613 case EP0_IN_DATA_PHASE_COMPLETE: {
1615 * Normal case: TX packet (ep0_reply) is in flight; wait for
1616 * it to finish, then go back to REQUEUE->IDLE.
1618 * Shutdown case: Reset the TX channel, send -ESHUTDOWN
1619 * completion to the gadget driver, then REQUEUE->IDLE.
1621 if (udc->ep0_req_completed) {
1622 udc->ep0_reply = NULL;
1623 bcm63xx_ep0_read_complete(udc);
1625 * the "ack" sometimes gets eaten (see
1626 * bcm63xx_ep0_do_idle)
1628 ep0state = EP0_REQUEUE;
1629 } else if (shutdown) {
1630 iudma_reset_channel(udc, &udc->iudma[IUDMA_EP0_TXCHAN]);
1631 bcm63xx_ep0_nuke_reply(udc, 1);
1632 ep0state = EP0_REQUEUE;
1636 case EP0_OUT_DATA_PHASE_SETUP:
1637 /* Similar behavior to EP0_IN_DATA_PHASE_SETUP */
1638 if (udc->ep0_reply) {
1639 bcm63xx_ep0_map_write(udc, IUDMA_EP0_RXCHAN,
1641 ep0state = EP0_OUT_DATA_PHASE_COMPLETE;
1642 } else if (shutdown) {
1643 ep0state = EP0_REQUEUE;
1646 case EP0_OUT_DATA_PHASE_COMPLETE: {
1647 /* Similar behavior to EP0_IN_DATA_PHASE_COMPLETE */
1648 if (udc->ep0_req_completed) {
1649 udc->ep0_reply = NULL;
1650 bcm63xx_ep0_read_complete(udc);
1652 /* send 0-byte ack to host */
1653 bcm63xx_ep0_internal_request(udc, IUDMA_EP0_TXCHAN, 0);
1654 ep0state = EP0_OUT_STATUS_PHASE;
1655 } else if (shutdown) {
1656 iudma_reset_channel(udc, &udc->iudma[IUDMA_EP0_RXCHAN]);
1657 bcm63xx_ep0_nuke_reply(udc, 0);
1658 ep0state = EP0_REQUEUE;
1662 case EP0_OUT_STATUS_PHASE:
1664 * Normal case: 0-byte OUT ack packet is in flight; wait
1665 * for it to finish, then go back to REQUEUE->IDLE.
1667 * Shutdown case: just cancel the transmission. Don't bother
1668 * calling the completion, because it originated from this
1669 * function anyway. Then go back to REQUEUE->IDLE.
1671 if (udc->ep0_req_completed) {
1672 bcm63xx_ep0_read_complete(udc);
1673 ep0state = EP0_REQUEUE;
1674 } else if (shutdown) {
1675 iudma_reset_channel(udc, &udc->iudma[IUDMA_EP0_TXCHAN]);
1676 udc->ep0_request = NULL;
1677 ep0state = EP0_REQUEUE;
1680 case EP0_IN_FAKE_STATUS_PHASE: {
1682 * Normal case: we spoofed a SETUP packet and are now
1683 * waiting for the gadget driver to send a 0-byte reply.
1684 * This doesn't actually get sent to the HW because the
1685 * HW has already sent its own reply. Once we get the
1686 * response, return to IDLE.
1688 * Shutdown case: return to IDLE immediately.
1690 * Note that the ep0 RX descriptor has remained queued
1691 * (and possibly unfilled) during this entire transaction.
1692 * The HW datapath (IUDMA) never even sees SET_CONFIGURATION
1693 * or SET_INTERFACE transactions.
1695 struct usb_request *r = udc->ep0_reply;
1699 ep0state = EP0_IDLE;
1703 bcm63xx_ep0_complete(udc, r, 0);
1704 udc->ep0_reply = NULL;
1705 ep0state = EP0_IDLE;
1712 if (udc->ep0state == ep0state)
1715 udc->ep0state = ep0state;
1720 * bcm63xx_ep0_process - ep0 worker thread / state machine.
1721 * @w: Workqueue struct.
1723 * bcm63xx_ep0_process is triggered any time an event occurs on ep0. It
1724 * is used to synchronize ep0 events and ensure that both HW and SW events
1725 * occur in a well-defined order. When the ep0 IUDMA queues are idle, it may
1726 * synthesize SET_CONFIGURATION / SET_INTERFACE requests that were consumed
1727 * by the USBD hardware.
1729 * The worker function will continue iterating around the state machine
1730 * until there is nothing left to do. Usually "nothing left to do" means
1731 * that we're waiting for a new event from the hardware.
1733 static void bcm63xx_ep0_process(struct work_struct *w)
1735 struct bcm63xx_udc *udc = container_of(w, struct bcm63xx_udc, ep0_wq);
1736 spin_lock_irq(&udc->lock);
1737 while (bcm63xx_ep0_one_round(udc) == 0)
1739 spin_unlock_irq(&udc->lock);
1742 /***********************************************************************
1743 * Standard UDC gadget operations
1744 ***********************************************************************/
1747 * bcm63xx_udc_get_frame - Read current SOF frame number from the HW.
1748 * @gadget: USB device.
1750 static int bcm63xx_udc_get_frame(struct usb_gadget *gadget)
1752 struct bcm63xx_udc *udc = gadget_to_udc(gadget);
1754 return (usbd_readl(udc, USBD_STATUS_REG) &
1755 USBD_STATUS_SOF_MASK) >> USBD_STATUS_SOF_SHIFT;
1759 * bcm63xx_udc_pullup - Enable/disable pullup on D+ line.
1760 * @gadget: USB device.
1761 * @is_on: 0 to disable pullup, 1 to enable.
1763 * See notes in bcm63xx_select_pullup().
1765 static int bcm63xx_udc_pullup(struct usb_gadget *gadget, int is_on)
1767 struct bcm63xx_udc *udc = gadget_to_udc(gadget);
1768 unsigned long flags;
1769 int i, rc = -EINVAL;
1771 spin_lock_irqsave(&udc->lock, flags);
1772 if (is_on && udc->ep0state == EP0_SHUTDOWN) {
1773 udc->gadget.speed = USB_SPEED_UNKNOWN;
1774 udc->ep0state = EP0_REQUEUE;
1775 bcm63xx_fifo_setup(udc);
1776 bcm63xx_fifo_reset(udc);
1777 bcm63xx_ep_setup(udc);
1779 bitmap_zero(&udc->wedgemap, BCM63XX_NUM_EP);
1780 for (i = 0; i < BCM63XX_NUM_EP; i++)
1781 bcm63xx_set_stall(udc, &udc->bep[i], false);
1783 bcm63xx_set_ctrl_irqs(udc, true);
1784 bcm63xx_select_pullup(gadget_to_udc(gadget), true);
1786 } else if (!is_on && udc->ep0state != EP0_SHUTDOWN) {
1787 bcm63xx_select_pullup(gadget_to_udc(gadget), false);
1789 udc->ep0_req_shutdown = 1;
1790 spin_unlock_irqrestore(&udc->lock, flags);
1793 schedule_work(&udc->ep0_wq);
1794 if (udc->ep0state == EP0_SHUTDOWN)
1798 bcm63xx_set_ctrl_irqs(udc, false);
1799 cancel_work_sync(&udc->ep0_wq);
1803 spin_unlock_irqrestore(&udc->lock, flags);
1808 * bcm63xx_udc_start - Start the controller.
1809 * @gadget: USB device.
1810 * @driver: Driver for USB device.
1812 static int bcm63xx_udc_start(struct usb_gadget *gadget,
1813 struct usb_gadget_driver *driver)
1815 struct bcm63xx_udc *udc = gadget_to_udc(gadget);
1816 unsigned long flags;
1818 if (!driver || driver->max_speed < USB_SPEED_HIGH ||
1826 spin_lock_irqsave(&udc->lock, flags);
1828 set_clocks(udc, true);
1829 bcm63xx_fifo_setup(udc);
1830 bcm63xx_ep_init(udc);
1831 bcm63xx_ep_setup(udc);
1832 bcm63xx_fifo_reset(udc);
1833 bcm63xx_select_phy_mode(udc, true);
1835 udc->driver = driver;
1836 driver->driver.bus = NULL;
1837 udc->gadget.dev.of_node = udc->dev->of_node;
1839 spin_unlock_irqrestore(&udc->lock, flags);
1845 * bcm63xx_udc_stop - Shut down the controller.
1846 * @gadget: USB device.
1847 * @driver: Driver for USB device.
1849 static int bcm63xx_udc_stop(struct usb_gadget *gadget)
1851 struct bcm63xx_udc *udc = gadget_to_udc(gadget);
1852 unsigned long flags;
1854 spin_lock_irqsave(&udc->lock, flags);
1859 * If we switch the PHY too abruptly after dropping D+, the host
1860 * will often complain:
1862 * hub 1-0:1.0: port 1 disabled by hub (EMI?), re-enabling...
1866 bcm63xx_select_phy_mode(udc, false);
1867 set_clocks(udc, false);
1869 spin_unlock_irqrestore(&udc->lock, flags);
1874 static const struct usb_gadget_ops bcm63xx_udc_ops = {
1875 .get_frame = bcm63xx_udc_get_frame,
1876 .pullup = bcm63xx_udc_pullup,
1877 .udc_start = bcm63xx_udc_start,
1878 .udc_stop = bcm63xx_udc_stop,
1881 /***********************************************************************
1883 ***********************************************************************/
1886 * bcm63xx_update_cfg_iface - Read current configuration/interface settings.
1887 * @udc: Reference to the device controller.
1889 * This controller intercepts SET_CONFIGURATION and SET_INTERFACE messages.
1890 * The driver never sees the raw control packets coming in on the ep0
1891 * IUDMA channel, but at least we get an interrupt event to tell us that
1892 * new values are waiting in the USBD_STATUS register.
1894 static void bcm63xx_update_cfg_iface(struct bcm63xx_udc *udc)
1896 u32 reg = usbd_readl(udc, USBD_STATUS_REG);
1898 udc->cfg = (reg & USBD_STATUS_CFG_MASK) >> USBD_STATUS_CFG_SHIFT;
1899 udc->iface = (reg & USBD_STATUS_INTF_MASK) >> USBD_STATUS_INTF_SHIFT;
1900 udc->alt_iface = (reg & USBD_STATUS_ALTINTF_MASK) >>
1901 USBD_STATUS_ALTINTF_SHIFT;
1902 bcm63xx_ep_setup(udc);
1906 * bcm63xx_update_link_speed - Check to see if the link speed has changed.
1907 * @udc: Reference to the device controller.
1909 * The link speed update coincides with a SETUP IRQ. Returns 1 if the
1910 * speed has changed, so that the caller can update the endpoint settings.
1912 static int bcm63xx_update_link_speed(struct bcm63xx_udc *udc)
1914 u32 reg = usbd_readl(udc, USBD_STATUS_REG);
1915 enum usb_device_speed oldspeed = udc->gadget.speed;
1917 switch ((reg & USBD_STATUS_SPD_MASK) >> USBD_STATUS_SPD_SHIFT) {
1918 case BCM63XX_SPD_HIGH:
1919 udc->gadget.speed = USB_SPEED_HIGH;
1921 case BCM63XX_SPD_FULL:
1922 udc->gadget.speed = USB_SPEED_FULL;
1925 /* this should never happen */
1926 udc->gadget.speed = USB_SPEED_UNKNOWN;
1928 "received SETUP packet with invalid link speed\n");
1932 if (udc->gadget.speed != oldspeed) {
1933 dev_info(udc->dev, "link up, %s-speed mode\n",
1934 udc->gadget.speed == USB_SPEED_HIGH ? "high" : "full");
1942 * bcm63xx_update_wedge - Iterate through wedged endpoints.
1943 * @udc: Reference to the device controller.
1944 * @new_status: true to "refresh" wedge status; false to clear it.
1946 * On a SETUP interrupt, we need to manually "refresh" the wedge status
1947 * because the controller hardware is designed to automatically clear
1948 * stalls in response to a CLEAR_FEATURE request from the host.
1950 * On a RESET interrupt, we do want to restore all wedged endpoints.
1952 static void bcm63xx_update_wedge(struct bcm63xx_udc *udc, bool new_status)
1956 for_each_set_bit(i, &udc->wedgemap, BCM63XX_NUM_EP) {
1957 bcm63xx_set_stall(udc, &udc->bep[i], new_status);
1959 clear_bit(i, &udc->wedgemap);
1964 * bcm63xx_udc_ctrl_isr - ISR for control path events (USBD).
1965 * @irq: IRQ number (unused).
1966 * @dev_id: Reference to the device controller.
1968 * This is where we handle link (VBUS) down, USB reset, speed changes,
1969 * SET_CONFIGURATION, and SET_INTERFACE events.
1971 static irqreturn_t bcm63xx_udc_ctrl_isr(int irq, void *dev_id)
1973 struct bcm63xx_udc *udc = dev_id;
1975 bool disconnected = false, bus_reset = false;
1977 stat = usbd_readl(udc, USBD_EVENT_IRQ_STATUS_REG) &
1978 usbd_readl(udc, USBD_EVENT_IRQ_MASK_REG);
1980 usbd_writel(udc, stat, USBD_EVENT_IRQ_STATUS_REG);
1982 spin_lock(&udc->lock);
1983 if (stat & BIT(USBD_EVENT_IRQ_USB_LINK)) {
1986 if (!(usbd_readl(udc, USBD_EVENTS_REG) &
1987 USBD_EVENTS_USB_LINK_MASK) &&
1988 udc->gadget.speed != USB_SPEED_UNKNOWN)
1989 dev_info(udc->dev, "link down\n");
1991 udc->gadget.speed = USB_SPEED_UNKNOWN;
1992 disconnected = true;
1994 if (stat & BIT(USBD_EVENT_IRQ_USB_RESET)) {
1995 bcm63xx_fifo_setup(udc);
1996 bcm63xx_fifo_reset(udc);
1997 bcm63xx_ep_setup(udc);
1999 bcm63xx_update_wedge(udc, false);
2001 udc->ep0_req_reset = 1;
2002 schedule_work(&udc->ep0_wq);
2005 if (stat & BIT(USBD_EVENT_IRQ_SETUP)) {
2006 if (bcm63xx_update_link_speed(udc)) {
2007 bcm63xx_fifo_setup(udc);
2008 bcm63xx_ep_setup(udc);
2010 bcm63xx_update_wedge(udc, true);
2012 if (stat & BIT(USBD_EVENT_IRQ_SETCFG)) {
2013 bcm63xx_update_cfg_iface(udc);
2014 udc->ep0_req_set_cfg = 1;
2015 schedule_work(&udc->ep0_wq);
2017 if (stat & BIT(USBD_EVENT_IRQ_SETINTF)) {
2018 bcm63xx_update_cfg_iface(udc);
2019 udc->ep0_req_set_iface = 1;
2020 schedule_work(&udc->ep0_wq);
2022 spin_unlock(&udc->lock);
2024 if (disconnected && udc->driver)
2025 udc->driver->disconnect(&udc->gadget);
2026 else if (bus_reset && udc->driver)
2027 usb_gadget_udc_reset(&udc->gadget, udc->driver);
2033 * bcm63xx_udc_data_isr - ISR for data path events (IUDMA).
2034 * @irq: IRQ number (unused).
2035 * @dev_id: Reference to the IUDMA channel that generated the interrupt.
2037 * For the two ep0 channels, we have special handling that triggers the
2038 * ep0 worker thread. For normal bulk/intr channels, either queue up
2039 * the next buffer descriptor for the transaction (incomplete transaction),
2040 * or invoke the completion callback (complete transactions).
2042 static irqreturn_t bcm63xx_udc_data_isr(int irq, void *dev_id)
2044 struct iudma_ch *iudma = dev_id;
2045 struct bcm63xx_udc *udc = iudma->udc;
2046 struct bcm63xx_ep *bep;
2047 struct usb_request *req = NULL;
2048 struct bcm63xx_req *breq = NULL;
2050 bool is_done = false;
2052 spin_lock(&udc->lock);
2054 usb_dmac_writel(udc, ENETDMAC_IR_BUFDONE_MASK,
2055 ENETDMAC_IR_REG, iudma->ch_idx);
2057 rc = iudma_read(udc, iudma);
2059 /* special handling for EP0 RX (0) and TX (1) */
2060 if (iudma->ch_idx == IUDMA_EP0_RXCHAN ||
2061 iudma->ch_idx == IUDMA_EP0_TXCHAN) {
2062 req = udc->ep0_request;
2063 breq = our_req(req);
2065 /* a single request could require multiple submissions */
2069 if (req->actual >= req->length || breq->bd_bytes > rc) {
2070 udc->ep0_req_completed = 1;
2072 schedule_work(&udc->ep0_wq);
2074 /* "actual" on a ZLP is 1 byte */
2075 req->actual = min(req->actual, req->length);
2077 /* queue up the next BD (same request) */
2078 iudma_write(udc, iudma, breq);
2081 } else if (!list_empty(&bep->queue)) {
2082 breq = list_first_entry(&bep->queue, struct bcm63xx_req, queue);
2088 if (req->actual >= req->length || breq->bd_bytes > rc) {
2090 list_del(&breq->queue);
2092 req->actual = min(req->actual, req->length);
2094 if (!list_empty(&bep->queue)) {
2095 struct bcm63xx_req *next;
2097 next = list_first_entry(&bep->queue,
2098 struct bcm63xx_req, queue);
2099 iudma_write(udc, iudma, next);
2102 iudma_write(udc, iudma, breq);
2106 spin_unlock(&udc->lock);
2109 usb_gadget_unmap_request(&udc->gadget, req, iudma->is_tx);
2111 req->complete(&bep->ep, req);
2117 /***********************************************************************
2119 ***********************************************************************/
2122 * bcm63xx_usbd_dbg_show - Show USBD controller state.
2123 * @s: seq_file to which the information will be written.
2126 * This file nominally shows up as /sys/kernel/debug/bcm63xx_udc/usbd
2128 static int bcm63xx_usbd_dbg_show(struct seq_file *s, void *p)
2130 struct bcm63xx_udc *udc = s->private;
2135 seq_printf(s, "ep0 state: %s\n",
2136 bcm63xx_ep0_state_names[udc->ep0state]);
2137 seq_printf(s, " pending requests: %s%s%s%s%s%s%s\n",
2138 udc->ep0_req_reset ? "reset " : "",
2139 udc->ep0_req_set_cfg ? "set_cfg " : "",
2140 udc->ep0_req_set_iface ? "set_iface " : "",
2141 udc->ep0_req_shutdown ? "shutdown " : "",
2142 udc->ep0_request ? "pending " : "",
2143 udc->ep0_req_completed ? "completed " : "",
2144 udc->ep0_reply ? "reply " : "");
2145 seq_printf(s, "cfg: %d; iface: %d; alt_iface: %d\n",
2146 udc->cfg, udc->iface, udc->alt_iface);
2147 seq_printf(s, "regs:\n");
2148 seq_printf(s, " control: %08x; straps: %08x; status: %08x\n",
2149 usbd_readl(udc, USBD_CONTROL_REG),
2150 usbd_readl(udc, USBD_STRAPS_REG),
2151 usbd_readl(udc, USBD_STATUS_REG));
2152 seq_printf(s, " events: %08x; stall: %08x\n",
2153 usbd_readl(udc, USBD_EVENTS_REG),
2154 usbd_readl(udc, USBD_STALL_REG));
2158 DEFINE_SHOW_ATTRIBUTE(bcm63xx_usbd_dbg);
2161 * bcm63xx_iudma_dbg_show - Show IUDMA status and descriptors.
2162 * @s: seq_file to which the information will be written.
2165 * This file nominally shows up as /sys/kernel/debug/bcm63xx_udc/iudma
2167 static int bcm63xx_iudma_dbg_show(struct seq_file *s, void *p)
2169 struct bcm63xx_udc *udc = s->private;
2176 for (ch_idx = 0; ch_idx < BCM63XX_NUM_IUDMA; ch_idx++) {
2177 struct iudma_ch *iudma = &udc->iudma[ch_idx];
2178 struct list_head *pos;
2180 seq_printf(s, "IUDMA channel %d -- ", ch_idx);
2181 switch (iudma_defaults[ch_idx].ep_type) {
2183 seq_printf(s, "control");
2186 seq_printf(s, "bulk");
2189 seq_printf(s, "interrupt");
2192 seq_printf(s, ch_idx & 0x01 ? " tx" : " rx");
2193 seq_printf(s, " [ep%d]:\n",
2194 max_t(int, iudma_defaults[ch_idx].ep_num, 0));
2195 seq_printf(s, " cfg: %08x; irqstat: %08x; irqmask: %08x; maxburst: %08x\n",
2196 usb_dmac_readl(udc, ENETDMAC_CHANCFG_REG, ch_idx),
2197 usb_dmac_readl(udc, ENETDMAC_IR_REG, ch_idx),
2198 usb_dmac_readl(udc, ENETDMAC_IRMASK_REG, ch_idx),
2199 usb_dmac_readl(udc, ENETDMAC_MAXBURST_REG, ch_idx));
2201 sram2 = usb_dmas_readl(udc, ENETDMAS_SRAM2_REG, ch_idx);
2202 sram3 = usb_dmas_readl(udc, ENETDMAS_SRAM3_REG, ch_idx);
2203 seq_printf(s, " base: %08x; index: %04x_%04x; desc: %04x_%04x %08x\n",
2204 usb_dmas_readl(udc, ENETDMAS_RSTART_REG, ch_idx),
2205 sram2 >> 16, sram2 & 0xffff,
2206 sram3 >> 16, sram3 & 0xffff,
2207 usb_dmas_readl(udc, ENETDMAS_SRAM4_REG, ch_idx));
2208 seq_printf(s, " desc: %d/%d used", iudma->n_bds_used,
2213 list_for_each(pos, &iudma->bep->queue)
2215 seq_printf(s, "; %d queued\n", i);
2217 seq_printf(s, "\n");
2220 for (i = 0; i < iudma->n_bds; i++) {
2221 struct bcm_enet_desc *d = &iudma->bd_ring[i];
2223 seq_printf(s, " %03x (%02x): len_stat: %04x_%04x; pa %08x",
2225 d->len_stat >> 16, d->len_stat & 0xffff,
2227 if (d == iudma->read_bd)
2228 seq_printf(s, " <<RD");
2229 if (d == iudma->write_bd)
2230 seq_printf(s, " <<WR");
2231 seq_printf(s, "\n");
2234 seq_printf(s, "\n");
2239 DEFINE_SHOW_ATTRIBUTE(bcm63xx_iudma_dbg);
2242 * bcm63xx_udc_init_debugfs - Create debugfs entries.
2243 * @udc: Reference to the device controller.
2245 static void bcm63xx_udc_init_debugfs(struct bcm63xx_udc *udc)
2247 struct dentry *root;
2249 if (!IS_ENABLED(CONFIG_USB_GADGET_DEBUG_FS))
2252 root = debugfs_create_dir(udc->gadget.name, usb_debug_root);
2253 udc->debugfs_root = root;
2255 debugfs_create_file("usbd", 0400, root, udc, &bcm63xx_usbd_dbg_fops);
2256 debugfs_create_file("iudma", 0400, root, udc, &bcm63xx_iudma_dbg_fops);
2260 * bcm63xx_udc_cleanup_debugfs - Remove debugfs entries.
2261 * @udc: Reference to the device controller.
2263 * debugfs_remove() is safe to call with a NULL argument.
2265 static void bcm63xx_udc_cleanup_debugfs(struct bcm63xx_udc *udc)
2267 debugfs_remove_recursive(udc->debugfs_root);
2270 /***********************************************************************
2272 ***********************************************************************/
2275 * bcm63xx_udc_probe - Initialize a new instance of the UDC.
2276 * @pdev: Platform device struct from the bcm63xx BSP code.
2278 * Note that platform data is required, because pd.port_no varies from chip
2279 * to chip and is used to switch the correct USB port to device mode.
2281 static int bcm63xx_udc_probe(struct platform_device *pdev)
2283 struct device *dev = &pdev->dev;
2284 struct bcm63xx_usbd_platform_data *pd = dev_get_platdata(dev);
2285 struct bcm63xx_udc *udc;
2286 int rc = -ENOMEM, i, irq;
2288 udc = devm_kzalloc(dev, sizeof(*udc), GFP_KERNEL);
2292 platform_set_drvdata(pdev, udc);
2297 dev_err(dev, "missing platform data\n");
2301 udc->usbd_regs = devm_platform_ioremap_resource(pdev, 0);
2302 if (IS_ERR(udc->usbd_regs))
2303 return PTR_ERR(udc->usbd_regs);
2305 udc->iudma_regs = devm_platform_ioremap_resource(pdev, 1);
2306 if (IS_ERR(udc->iudma_regs))
2307 return PTR_ERR(udc->iudma_regs);
2309 spin_lock_init(&udc->lock);
2310 INIT_WORK(&udc->ep0_wq, bcm63xx_ep0_process);
2312 udc->gadget.ops = &bcm63xx_udc_ops;
2313 udc->gadget.name = dev_name(dev);
2315 if (!pd->use_fullspeed && !use_fullspeed)
2316 udc->gadget.max_speed = USB_SPEED_HIGH;
2318 udc->gadget.max_speed = USB_SPEED_FULL;
2320 /* request clocks, allocate buffers, and clear any pending IRQs */
2321 rc = bcm63xx_init_udc_hw(udc);
2327 /* IRQ resource #0: control interrupt (VBUS, speed, etc.) */
2328 irq = platform_get_irq(pdev, 0);
2331 if (devm_request_irq(dev, irq, &bcm63xx_udc_ctrl_isr, 0,
2332 dev_name(dev), udc) < 0)
2333 goto report_request_failure;
2335 /* IRQ resources #1-6: data interrupts for IUDMA channels 0-5 */
2336 for (i = 0; i < BCM63XX_NUM_IUDMA; i++) {
2337 irq = platform_get_irq(pdev, i + 1);
2340 if (devm_request_irq(dev, irq, &bcm63xx_udc_data_isr, 0,
2341 dev_name(dev), &udc->iudma[i]) < 0)
2342 goto report_request_failure;
2345 bcm63xx_udc_init_debugfs(udc);
2346 rc = usb_add_gadget_udc(dev, &udc->gadget);
2350 bcm63xx_udc_cleanup_debugfs(udc);
2352 bcm63xx_uninit_udc_hw(udc);
2355 report_request_failure:
2356 dev_err(dev, "error requesting IRQ #%d\n", irq);
2361 * bcm63xx_udc_remove - Remove the device from the system.
2362 * @pdev: Platform device struct from the bcm63xx BSP code.
2364 static int bcm63xx_udc_remove(struct platform_device *pdev)
2366 struct bcm63xx_udc *udc = platform_get_drvdata(pdev);
2368 bcm63xx_udc_cleanup_debugfs(udc);
2369 usb_del_gadget_udc(&udc->gadget);
2370 BUG_ON(udc->driver);
2372 bcm63xx_uninit_udc_hw(udc);
2377 static struct platform_driver bcm63xx_udc_driver = {
2378 .probe = bcm63xx_udc_probe,
2379 .remove = bcm63xx_udc_remove,
2381 .name = DRV_MODULE_NAME,
2384 module_platform_driver(bcm63xx_udc_driver);
2386 MODULE_DESCRIPTION("BCM63xx USB Peripheral Controller");
2388 MODULE_LICENSE("GPL");
2389 MODULE_ALIAS("platform:" DRV_MODULE_NAME);