13 #include <linux/bitops.h>
16 #include <linux/compiler.h>
19 #include <linux/device.h>
21 #include <linux/errno.h>
26 #include <linux/kernel.h>
27 #include <linux/list.h>
28 #include <linux/module.h>
31 #include <linux/sched.h>
33 #include <linux/slab.h>
35 #include <linux/usb/ch9.h>
45 #define DRV_MODULE_NAME "bcm63xx_udc"
47 static const char bcm63xx_ep0name[] =
"ep0";
48 static const char *
const bcm63xx_ep_name[] = {
50 "ep1in-bulk",
"ep2out-bulk",
"ep3in-int",
"ep4out-int",
53 static bool use_fullspeed;
76 static bool irq_coalesce;
80 #define BCM63XX_NUM_EP 5
81 #define BCM63XX_NUM_IUDMA 6
82 #define BCM63XX_NUM_FIFO_PAIRS 3
84 #define IUDMA_RESET_TIMEOUT_US 10000
86 #define IUDMA_EP0_RXCHAN 0
87 #define IUDMA_EP0_TXCHAN 1
89 #define IUDMA_MAX_FRAGMENT 2048
90 #define BCM63XX_MAX_CTRL_PKT 64
92 #define BCMEP_CTRL 0x00
93 #define BCMEP_ISOC 0x01
94 #define BCMEP_BULK 0x02
95 #define BCMEP_INTR 0x03
97 #define BCMEP_OUT 0x00
100 #define BCM63XX_SPD_FULL 1
101 #define BCM63XX_SPD_HIGH 0
103 #define IUDMA_DMAC_OFFSET 0x200
104 #define IUDMA_DMAS_OFFSET 0x400
118 static const char __maybe_unused bcm63xx_ep0_state_names[][32] = {
121 "IN_DATA_PHASE_SETUP",
122 "IN_DATA_PHASE_COMPLETE",
123 "OUT_DATA_PHASE_SETUP",
124 "OUT_DATA_PHASE_COMPLETE",
126 "IN_FAKE_STATUS_PHASE",
323 static const struct usb_ep_ops bcm63xx_udc_ep_ops;
409 static void bcm63xx_ep_dma_select(
struct bcm63xx_udc *udc,
int idx)
445 static void bcm63xx_fifo_setup(
struct bcm63xx_udc *udc)
448 u32 i,
val, rx_fifo_slot, tx_fifo_slot;
451 rx_fifo_slot = tx_fifo_slot = 0;
454 const struct iudma_ch_cfg *tx_cfg = &iudma_defaults[i + 1];
456 bcm63xx_ep_dma_select(udc, i >> 1);
489 bcm63xx_ep_dma_select(udc, ep_num);
501 static void bcm63xx_fifo_reset(
struct bcm63xx_udc *udc)
506 bcm63xx_fifo_reset_ep(udc, i);
513 static void bcm63xx_ep_init(
struct bcm63xx_udc *udc)
523 bcm63xx_ep_dma_select(udc, cfg->
ep_num);
536 static void bcm63xx_ep_setup(
struct bcm63xx_udc *udc)
548 udc->
iudma[
i].max_pkt = max_pkt;
552 udc->
bep[
idx].ep.maxpacket = max_pkt;
581 int first_bd = 1, last_bd = 0, extra_zero_pkt = 0;
582 unsigned int bytes_left = breq->
req.length - breq->
offset;
583 const int max_bd_bytes = !irq_coalesce && !iudma->
is_tx ?
590 if ((bytes_left % iudma->
max_pkt == 0) && bytes_left && breq->
req.zero)
596 unsigned int n_bytes;
606 n_bytes =
min_t(
int, bytes_left, max_bd_bytes);
623 if (extra_zero_pkt && !bytes_left)
627 (n_bytes == bytes_left && !extra_zero_pkt)) {
638 bytes_left -= n_bytes;
656 int i, actual_len = 0;
671 DMADESC_LENGTH_SHIFT;
692 int ch_idx = iudma->
ch_idx;
695 bcm63xx_fifo_reset_ep(udc,
max(0, iudma->
ep_num));
706 bcm63xx_fifo_reset_ep(udc, iudma->
ep_num);
709 dev_err(udc->
dev,
"can't reset IUDMA channel %d\n",
714 dev_warn(udc->
dev,
"forcibly halting IUDMA channel %d\n",
723 for (d = iudma->
bd_ring; d <= iudma->end_bd; d++)
744 static int iudma_init_channel(
struct bcm63xx_udc *udc,
unsigned int ch_idx)
747 const struct iudma_ch_cfg *cfg = &iudma_defaults[ch_idx];
753 iudma->
is_tx = !!(ch_idx & 0x01);
757 INIT_LIST_HEAD(&bep->
queue);
767 iudma->
n_bds = n_bds;
791 rc = iudma_init_channel(udc, i);
794 iudma_reset_channel(udc, &udc->
iudma[i]);
814 iudma_reset_channel(udc, &udc->
iudma[i]);
828 static void bcm63xx_set_ctrl_irqs(
struct bcm63xx_udc *udc,
bool enable_irqs)
854 static void bcm63xx_select_phy_mode(
struct bcm63xx_udc *udc,
bool is_device)
894 static void bcm63xx_select_pullup(
struct bcm63xx_udc *udc,
bool is_on)
913 static void bcm63xx_uninit_udc_hw(
struct bcm63xx_udc *udc)
915 set_clocks(udc,
true);
917 set_clocks(udc,
false);
927 static int bcm63xx_init_udc_hw(
struct bcm63xx_udc *udc)
937 INIT_LIST_HEAD(&udc->
gadget.ep_list);
941 bep->
ep.name = bcm63xx_ep_name[
i];
943 bep->
ep.ops = &bcm63xx_udc_ep_ops;
949 INIT_LIST_HEAD(&bep->
queue);
968 set_clocks(udc,
true);
987 bcm63xx_set_ctrl_irqs(udc,
false);
995 rc = iudma_init(udc);
996 set_clocks(udc,
false);
998 bcm63xx_uninit_udc_hw(udc);
1015 static int bcm63xx_ep_enable(
struct usb_ep *
ep,
1021 unsigned long flags;
1023 if (!ep || !desc || ep->
name == bcm63xx_ep0name)
1031 spin_unlock_irqrestore(&udc->
lock, flags);
1038 iudma_reset_channel(udc, iudma);
1041 bcm63xx_set_stall(udc, bep,
false);
1045 ep->
maxpacket = usb_endpoint_maxp(desc);
1047 spin_unlock_irqrestore(&udc->
lock, flags);
1055 static int bcm63xx_ep_disable(
struct usb_ep *ep)
1061 unsigned long flags;
1063 if (!ep || !ep->
desc)
1068 spin_unlock_irqrestore(&udc->
lock, flags);
1073 iudma_reset_channel(udc, iudma);
1075 if (!list_empty(&bep->
queue)) {
1085 spin_unlock_irqrestore(&udc->
lock, flags);
1086 breq->
req.complete(&iudma->
bep->ep, &breq->
req);
1092 spin_unlock_irqrestore(&udc->
lock, flags);
1106 breq = kzalloc(
sizeof(*breq), mem_flags);
1117 static void bcm63xx_udc_free_request(
struct usb_ep *ep,
1144 unsigned long flags;
1154 if (bep == &udc->
bep[0]) {
1165 if (!bep->
iudma->enabled) {
1173 if (list_is_singular(&bep->
queue))
1174 iudma_write(udc, bep->
iudma, breq);
1178 spin_unlock_irqrestore(&udc->
lock, flags);
1196 unsigned long flags;
1200 if (list_empty(&bep->
queue)) {
1209 iudma_reset_channel(udc, bep->
iudma);
1212 if (!list_empty(&bep->
queue)) {
1217 iudma_write(udc, bep->
iudma, next);
1224 spin_unlock_irqrestore(&udc->
lock, flags);
1239 static int bcm63xx_udc_set_halt(
struct usb_ep *ep,
int value)
1243 unsigned long flags;
1246 bcm63xx_set_stall(udc, bep, !!value);
1248 spin_unlock_irqrestore(&udc->
lock, flags);
1259 static int bcm63xx_udc_set_wedge(
struct usb_ep *ep)
1263 unsigned long flags;
1267 bcm63xx_set_stall(udc, bep,
true);
1268 spin_unlock_irqrestore(&udc->
lock, flags);
1273 static const struct usb_ep_ops bcm63xx_udc_ep_ops = {
1274 .enable = bcm63xx_ep_enable,
1275 .disable = bcm63xx_ep_disable,
1277 .alloc_request = bcm63xx_udc_alloc_request,
1278 .free_request = bcm63xx_udc_free_request,
1280 .queue = bcm63xx_udc_queue,
1281 .dequeue = bcm63xx_udc_dequeue,
1283 .set_halt = bcm63xx_udc_set_halt,
1284 .set_wedge = bcm63xx_udc_set_wedge,
1296 static int bcm63xx_ep0_setup_callback(
struct bcm63xx_udc *udc,
1301 spin_unlock_irq(&udc->
lock);
1303 spin_lock_irq(&udc->
lock);
1319 static int bcm63xx_ep0_spoof_set_cfg(
struct bcm63xx_udc *udc)
1330 rc = bcm63xx_ep0_setup_callback(udc, &ctrl);
1333 "hardware auto-acked bad SET_CONFIGURATION(%d) request\n",
1343 static int bcm63xx_ep0_spoof_set_iface(
struct bcm63xx_udc *udc)
1354 rc = bcm63xx_ep0_setup_callback(udc, &ctrl);
1357 "hardware auto-acked bad SET_INTERFACE(%d,%d) request\n",
1369 static void bcm63xx_ep0_map_write(
struct bcm63xx_udc *udc,
int ch_idx,
1381 iudma_write(udc, iudma, breq);
1390 static void bcm63xx_ep0_complete(
struct bcm63xx_udc *udc,
1397 spin_unlock_irq(&udc->
lock);
1399 spin_lock_irq(&udc->
lock);
1419 bcm63xx_ep0_complete(udc, req, -
ESHUTDOWN);
1427 static int bcm63xx_ep0_read_complete(
struct bcm63xx_udc *udc)
1446 static void bcm63xx_ep0_internal_request(
struct bcm63xx_udc *udc,
int ch_idx,
1455 bcm63xx_ep0_map_write(udc, ch_idx, req);
1471 rc = bcm63xx_ep0_read_complete(udc);
1487 if (rc !=
sizeof(*ctrl)) {
1489 "malformed SETUP packet (%d bytes)\n", rc);
1494 rc = bcm63xx_ep0_setup_callback(udc, ctrl);
1496 bcm63xx_set_stall(udc, &udc->
bep[0],
true);
1519 static int bcm63xx_ep0_do_idle(
struct bcm63xx_udc *udc)
1525 if (bcm63xx_ep0_spoof_set_cfg(udc) >= 0)
1529 if (bcm63xx_ep0_spoof_set_iface(udc) >= 0)
1532 udc->
ep0state = bcm63xx_ep0_do_setup(udc);
1552 bcm63xx_ep0_nuke_reply(udc, 0);
1566 static int bcm63xx_ep0_one_round(
struct bcm63xx_udc *udc)
1579 return bcm63xx_ep0_do_idle(udc);
1594 }
else if (shutdown) {
1608 bcm63xx_ep0_read_complete(udc);
1614 }
else if (shutdown) {
1616 bcm63xx_ep0_nuke_reply(udc, 1);
1627 }
else if (shutdown) {
1635 bcm63xx_ep0_read_complete(udc);
1640 }
else if (shutdown) {
1642 bcm63xx_ep0_nuke_reply(udc, 0);
1657 bcm63xx_ep0_read_complete(udc);
1659 }
else if (shutdown) {
1688 bcm63xx_ep0_complete(udc, r, 0);
1721 spin_lock_irq(&udc->
lock);
1722 while (bcm63xx_ep0_one_round(udc) == 0)
1724 spin_unlock_irq(&udc->
lock);
1750 static int bcm63xx_udc_pullup(
struct usb_gadget *gadget,
int is_on)
1753 unsigned long flags;
1760 bcm63xx_fifo_setup(udc);
1761 bcm63xx_fifo_reset(udc);
1762 bcm63xx_ep_setup(udc);
1764 bitmap_zero(&udc->
wedgemap, BCM63XX_NUM_EP);
1766 bcm63xx_set_stall(udc, &udc->
bep[i],
false);
1768 bcm63xx_set_ctrl_irqs(udc,
true);
1769 bcm63xx_select_pullup(gadget_to_udc(gadget),
true);
1772 bcm63xx_select_pullup(gadget_to_udc(gadget),
false);
1775 spin_unlock_irqrestore(&udc->
lock, flags);
1783 bcm63xx_set_ctrl_irqs(udc,
false);
1788 spin_unlock_irqrestore(&udc->
lock, flags);
1797 static int bcm63xx_udc_start(
struct usb_gadget *gadget,
1801 unsigned long flags;
1813 set_clocks(udc,
true);
1814 bcm63xx_fifo_setup(udc);
1815 bcm63xx_ep_init(udc);
1816 bcm63xx_ep_setup(udc);
1817 bcm63xx_fifo_reset(udc);
1818 bcm63xx_select_phy_mode(udc,
true);
1823 udc->
gadget.dev.of_node = udc->
dev->of_node;
1825 spin_unlock_irqrestore(&udc->
lock, flags);
1835 static int bcm63xx_udc_stop(
struct usb_gadget *gadget,
1839 unsigned long flags;
1854 bcm63xx_select_phy_mode(udc,
false);
1855 set_clocks(udc,
false);
1857 spin_unlock_irqrestore(&udc->
lock, flags);
1863 .get_frame = bcm63xx_udc_get_frame,
1864 .pullup = bcm63xx_udc_pullup,
1865 .udc_start = bcm63xx_udc_start,
1866 .udc_stop = bcm63xx_udc_stop,
1882 static void bcm63xx_update_cfg_iface(
struct bcm63xx_udc *udc)
1890 bcm63xx_ep_setup(udc);
1900 static int bcm63xx_update_link_speed(
struct bcm63xx_udc *udc)
1916 "received SETUP packet with invalid link speed\n");
1920 if (udc->
gadget.speed != oldspeed) {
1940 static void bcm63xx_update_wedge(
struct bcm63xx_udc *udc,
bool new_status)
1945 bcm63xx_set_stall(udc, &udc->
bep[i], new_status);
1970 spin_lock(&udc->
lock);
1980 disconnected =
true;
1983 bcm63xx_fifo_setup(udc);
1984 bcm63xx_fifo_reset(udc);
1985 bcm63xx_ep_setup(udc);
1987 bcm63xx_update_wedge(udc,
false);
1991 disconnected =
true;
1994 if (bcm63xx_update_link_speed(udc)) {
1995 bcm63xx_fifo_setup(udc);
1996 bcm63xx_ep_setup(udc);
1998 bcm63xx_update_wedge(udc,
true);
2001 bcm63xx_update_cfg_iface(udc);
2006 bcm63xx_update_cfg_iface(udc);
2010 spin_unlock(&udc->
lock);
2012 if (disconnected && udc->
driver)
2028 static irqreturn_t bcm63xx_udc_data_isr(
int irq,
void *dev_id)
2036 bool is_done =
false;
2038 spin_lock(&udc->
lock);
2043 rc = iudma_read(udc, iudma);
2049 breq = our_req(req);
2064 iudma_write(udc, iudma, breq);
2067 }
else if (!list_empty(&bep->
queue)) {
2080 if (!list_empty(&bep->
queue)) {
2085 iudma_write(udc, iudma, next);
2088 iudma_write(udc, iudma, breq);
2092 spin_unlock(&udc->
lock);
2114 static int bcm63xx_usbd_dbg_show(
struct seq_file *
s,
void *
p)
2122 bcm63xx_ep0_state_names[udc->
ep0state]);
2123 seq_printf(s,
" pending requests: %s%s%s%s%s%s%s\n",
2131 seq_printf(s,
"cfg: %d; iface: %d; alt_iface: %d\n",
2134 seq_printf(s,
" control: %08x; straps: %08x; status: %08x\n",
2138 seq_printf(s,
" events: %08x; stall: %08x\n",
2152 static int bcm63xx_iudma_dbg_show(
struct seq_file *s,
void *p)
2165 seq_printf(s,
"IUDMA channel %d -- ", ch_idx);
2166 switch (iudma_defaults[ch_idx].ep_type) {
2177 seq_printf(s, ch_idx & 0x01 ?
" tx" :
" rx");
2179 max_t(
int, iudma_defaults[ch_idx].ep_num, 0));
2180 seq_printf(s,
" cfg: %08x; irqstat: %08x; irqmask: %08x; maxburst: %08x\n",
2188 seq_printf(s,
" base: %08x; index: %04x_%04x; desc: %04x_%04x %08x\n",
2190 sram2 >> 16, sram2 & 0xffff,
2191 sram3 >> 16, sram3 & 0xffff,
2205 for (i = 0; i < iudma->
n_bds; i++) {
2208 seq_printf(s,
" %03x (%02x): len_stat: %04x_%04x; pa %08x",
2237 .open = bcm63xx_usbd_dbg_open,
2245 .open = bcm63xx_iudma_dbg_open,
2256 static void bcm63xx_udc_init_debugfs(
struct bcm63xx_udc *udc)
2264 if (IS_ERR(root) || !root)
2285 dev_err(udc->
dev,
"debugfs is not available\n");
2294 static void bcm63xx_udc_cleanup_debugfs(
struct bcm63xx_udc *udc)
2315 static void bcm63xx_udc_gadget_release(
struct device *
dev)
2336 dev_err(dev,
"cannot allocate memory\n");
2340 platform_set_drvdata(pdev, udc);
2345 dev_err(dev,
"missing platform data\n");
2351 dev_err(dev,
"error finding USBD resource\n");
2358 dev_err(dev,
"error finding IUDMA resource\n");
2364 dev_err(dev,
"error requesting resources\n");
2372 udc->
gadget.ops = &bcm63xx_udc_ops;
2373 udc->
gadget.name = dev_name(dev);
2375 udc->
gadget.dev.release = bcm63xx_udc_gadget_release;
2384 rc = bcm63xx_init_udc_hw(udc);
2393 dev_err(dev,
"missing IRQ resource #0\n");
2396 if (devm_request_irq(dev, irq, &bcm63xx_udc_ctrl_isr, 0,
2397 dev_name(dev), udc) < 0) {
2398 dev_err(dev,
"error requesting IRQ #%d\n", irq);
2406 dev_err(dev,
"missing IRQ resource #%d\n", i + 1);
2409 if (devm_request_irq(dev, irq, &bcm63xx_udc_data_isr, 0,
2410 dev_name(dev), &udc->
iudma[i]) < 0) {
2411 dev_err(dev,
"error requesting IRQ #%d\n", irq);
2420 bcm63xx_udc_init_debugfs(udc);
2425 bcm63xx_udc_cleanup_debugfs(udc);
2428 bcm63xx_uninit_udc_hw(udc);
2438 struct bcm63xx_udc *udc = platform_get_drvdata(pdev);
2440 bcm63xx_udc_cleanup_debugfs(udc);
2445 platform_set_drvdata(pdev,
NULL);
2446 bcm63xx_uninit_udc_hw(udc);
2452 .probe = bcm63xx_udc_probe,