1 // SPDX-License-Identifier: GPL-2.0+
3 * bdc_core.c - BRCM BDC USB3.0 device controller core operations
5 * Copyright (C) 2014 Broadcom Corporation
7 * Author: Ashwini Pahuja
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/platform_device.h>
14 #include <linux/interrupt.h>
15 #include <linux/ioport.h>
17 #include <linux/list.h>
18 #include <linux/delay.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/dmapool.h>
22 #include <linux/phy/phy.h>
23 #include <linux/moduleparam.h>
24 #include <linux/usb/ch9.h>
25 #include <linux/usb/gadget.h>
26 #include <linux/clk.h>
31 /* Poll till controller status is not OIP */
32 static int poll_oip(struct bdc *bdc, int usec)
35 /* Poll till STS!= OIP */
37 status = bdc_readl(bdc->regs, BDC_BDCSC);
38 if (BDC_CSTS(status) != BDC_OIP) {
40 "poll_oip complete status=%d",
47 dev_err(bdc->dev, "Err: operation timedout BDCSC: 0x%08x\n", status);
52 /* Stop the BDC controller */
53 int bdc_stop(struct bdc *bdc)
58 dev_dbg(bdc->dev, "%s ()\n\n", __func__);
59 temp = bdc_readl(bdc->regs, BDC_BDCSC);
60 /* Check if BDC is already halted */
61 if (BDC_CSTS(temp) == BDC_HLT) {
62 dev_vdbg(bdc->dev, "BDC already halted\n");
65 temp &= ~BDC_COP_MASK;
66 temp |= BDC_COS|BDC_COP_STP;
67 bdc_writel(bdc->regs, BDC_BDCSC, temp);
69 ret = poll_oip(bdc, BDC_COP_TIMEOUT);
71 dev_err(bdc->dev, "bdc stop operation failed");
76 /* Issue a reset to BDC controller */
77 int bdc_reset(struct bdc *bdc)
82 dev_dbg(bdc->dev, "%s ()\n", __func__);
83 /* First halt the controller */
88 temp = bdc_readl(bdc->regs, BDC_BDCSC);
89 temp &= ~BDC_COP_MASK;
90 temp |= BDC_COS|BDC_COP_RST;
91 bdc_writel(bdc->regs, BDC_BDCSC, temp);
92 ret = poll_oip(bdc, BDC_COP_TIMEOUT);
94 dev_err(bdc->dev, "bdc reset operation failed");
99 /* Run the BDC controller */
100 int bdc_run(struct bdc *bdc)
105 dev_dbg(bdc->dev, "%s ()\n", __func__);
106 temp = bdc_readl(bdc->regs, BDC_BDCSC);
107 /* if BDC is already in running state then do not do anything */
108 if (BDC_CSTS(temp) == BDC_NOR) {
109 dev_warn(bdc->dev, "bdc is already in running state\n");
112 temp &= ~BDC_COP_MASK;
115 bdc_writel(bdc->regs, BDC_BDCSC, temp);
116 ret = poll_oip(bdc, BDC_COP_TIMEOUT);
118 dev_err(bdc->dev, "bdc run operation failed:%d", ret);
121 temp = bdc_readl(bdc->regs, BDC_BDCSC);
122 if (BDC_CSTS(temp) != BDC_NOR) {
123 dev_err(bdc->dev, "bdc not in normal mode after RUN op :%d\n",
132 * Present the termination to the host, typically called from upstream port
133 * event with Vbus present =1
135 void bdc_softconn(struct bdc *bdc)
139 uspc = bdc_readl(bdc->regs, BDC_USPC);
140 uspc &= ~BDC_PST_MASK;
141 uspc |= BDC_LINK_STATE_RX_DET;
143 dev_dbg(bdc->dev, "%s () uspc=%08x\n", __func__, uspc);
144 bdc_writel(bdc->regs, BDC_USPC, uspc);
147 /* Remove the termination */
148 void bdc_softdisconn(struct bdc *bdc)
152 uspc = bdc_readl(bdc->regs, BDC_USPC);
155 dev_dbg(bdc->dev, "%s () uspc=%x\n", __func__, uspc);
156 bdc_writel(bdc->regs, BDC_USPC, uspc);
159 /* Set up the scratchpad buffer array and scratchpad buffers, if needed. */
160 static int scratchpad_setup(struct bdc *bdc)
166 sp_buff_size = BDC_SPB(bdc_readl(bdc->regs, BDC_BDCCFG0));
167 dev_dbg(bdc->dev, "%s() sp_buff_size=%d\n", __func__, sp_buff_size);
169 dev_dbg(bdc->dev, "Scratchpad buffer not needed\n");
172 /* Refer to BDC spec, Table 4 for description of SPB */
173 sp_buff_size = 1 << (sp_buff_size + 5);
174 dev_dbg(bdc->dev, "Allocating %d bytes for scratchpad\n", sp_buff_size);
175 bdc->scratchpad.buff = dma_alloc_coherent(bdc->dev, sp_buff_size,
176 &bdc->scratchpad.sp_dma,
179 if (!bdc->scratchpad.buff)
182 bdc->sp_buff_size = sp_buff_size;
183 bdc->scratchpad.size = sp_buff_size;
184 low32 = lower_32_bits(bdc->scratchpad.sp_dma);
185 upp32 = upper_32_bits(bdc->scratchpad.sp_dma);
186 cpu_to_le32s(&low32);
187 cpu_to_le32s(&upp32);
188 bdc_writel(bdc->regs, BDC_SPBBAL, low32);
189 bdc_writel(bdc->regs, BDC_SPBBAH, upp32);
193 bdc->scratchpad.buff = NULL;
198 /* Allocate the status report ring */
199 static int setup_srr(struct bdc *bdc, int interrupter)
201 dev_dbg(bdc->dev, "%s() NUM_SR_ENTRIES:%d\n", __func__, NUM_SR_ENTRIES);
203 bdc_writel(bdc->regs, BDC_SRRINT(0), BDC_SRR_RWS | BDC_SRR_RST);
204 bdc->srr.dqp_index = 0;
205 /* allocate the status report descriptors */
206 bdc->srr.sr_bds = dma_alloc_coherent(bdc->dev,
207 NUM_SR_ENTRIES * sizeof(struct bdc_bd),
208 &bdc->srr.dma_addr, GFP_KERNEL);
209 if (!bdc->srr.sr_bds)
215 /* Initialize the HW regs and internal data structures */
216 static void bdc_mem_init(struct bdc *bdc, bool reinit)
224 dev_dbg(bdc->dev, "%s ()\n", __func__);
225 bdc->ep0_state = WAIT_FOR_SETUP;
227 bdc->srr.eqp_index = 0;
228 bdc->srr.dqp_index = 0;
229 bdc->zlp_needed = false;
230 bdc->delayed_status = false;
232 bdc_writel(bdc->regs, BDC_SPBBAL, bdc->scratchpad.sp_dma);
234 temp = BDC_SRR_RWS | BDC_SRR_RST;
236 bdc_writel(bdc->regs, BDC_SRRINT(0), temp);
237 dev_dbg(bdc->dev, "bdc->srr.sr_bds =%p\n", bdc->srr.sr_bds);
238 temp = lower_32_bits(bdc->srr.dma_addr);
239 size = fls(NUM_SR_ENTRIES) - 2;
241 dev_dbg(bdc->dev, "SRRBAL[0]=%08x NUM_SR_ENTRIES:%d size:%d\n",
242 temp, NUM_SR_ENTRIES, size);
244 low32 = lower_32_bits(temp);
245 upp32 = upper_32_bits(bdc->srr.dma_addr);
246 cpu_to_le32s(&low32);
247 cpu_to_le32s(&upp32);
249 /* Write the dma addresses into regs*/
250 bdc_writel(bdc->regs, BDC_SRRBAL(0), low32);
251 bdc_writel(bdc->regs, BDC_SRRBAH(0), upp32);
253 temp = bdc_readl(bdc->regs, BDC_SRRINT(0));
255 temp &= ~(BDC_SRR_RST | BDC_SRR_RWS);
256 bdc_writel(bdc->regs, BDC_SRRINT(0), temp);
258 /* Set the Interrupt Coalescence ~500 usec */
259 temp = bdc_readl(bdc->regs, BDC_INTCTLS(0));
262 bdc_writel(bdc->regs, BDC_INTCTLS(0), temp);
264 usb2_pm = bdc_readl(bdc->regs, BDC_USPPM2);
265 dev_dbg(bdc->dev, "usb2_pm=%08x", usb2_pm);
266 /* Enable hardware LPM Enable */
268 bdc_writel(bdc->regs, BDC_USPPM2, usb2_pm);
270 /* readback for debug */
271 usb2_pm = bdc_readl(bdc->regs, BDC_USPPM2);
272 dev_dbg(bdc->dev, "usb2_pm=%08x\n", usb2_pm);
274 /* Disable any unwanted SR's on SRR */
275 temp = bdc_readl(bdc->regs, BDC_BDCSC);
276 /* We don't want Microframe counter wrap SR */
277 temp |= BDC_MASK_MCW;
278 bdc_writel(bdc->regs, BDC_BDCSC, temp);
281 * In some error cases, driver has to reset the entire BDC controller
282 * in that case reinit is passed as 1
285 /* Enable interrupts */
286 temp = bdc_readl(bdc->regs, BDC_BDCSC);
288 bdc_writel(bdc->regs, BDC_BDCSC, temp);
289 /* Init scratchpad to 0 */
290 memset(bdc->scratchpad.buff, 0, bdc->sp_buff_size);
291 /* Initialize SRR to 0 */
292 memset(bdc->srr.sr_bds, 0,
293 NUM_SR_ENTRIES * sizeof(struct bdc_bd));
295 /* One time initiaization only */
296 /* Enable status report function pointers */
297 bdc->sr_handler[0] = bdc_sr_xsf;
298 bdc->sr_handler[1] = bdc_sr_uspc;
300 /* EP0 status report function pointers */
301 bdc->sr_xsf_ep0[0] = bdc_xsf_ep0_setup_recv;
302 bdc->sr_xsf_ep0[1] = bdc_xsf_ep0_data_start;
303 bdc->sr_xsf_ep0[2] = bdc_xsf_ep0_status_start;
307 /* Free the dynamic memory */
308 static void bdc_mem_free(struct bdc *bdc)
310 dev_dbg(bdc->dev, "%s\n", __func__);
313 dma_free_coherent(bdc->dev,
314 NUM_SR_ENTRIES * sizeof(struct bdc_bd),
315 bdc->srr.sr_bds, bdc->srr.dma_addr);
317 /* Free scratchpad */
318 if (bdc->scratchpad.buff)
319 dma_free_coherent(bdc->dev, bdc->sp_buff_size,
320 bdc->scratchpad.buff, bdc->scratchpad.sp_dma);
322 /* Destroy the dma pools */
323 dma_pool_destroy(bdc->bd_table_pool);
325 /* Free the bdc_ep array */
326 kfree(bdc->bdc_ep_array);
328 bdc->srr.sr_bds = NULL;
329 bdc->scratchpad.buff = NULL;
330 bdc->bd_table_pool = NULL;
331 bdc->bdc_ep_array = NULL;
335 * bdc reinit gives a controller reset and reinitialize the registers,
336 * called from disconnect/bus reset scenario's, to ensure proper HW cleanup
338 int bdc_reinit(struct bdc *bdc)
342 dev_dbg(bdc->dev, "%s\n", __func__);
347 ret = bdc_reset(bdc);
351 /* the reinit flag is 1 */
352 bdc_mem_init(bdc, true);
360 /* Allocate all the dyanmic memory */
361 static int bdc_mem_alloc(struct bdc *bdc)
364 unsigned int num_ieps, num_oeps;
367 "%s() NUM_BDS_PER_TABLE:%d\n", __func__,
369 page_size = BDC_PGS(bdc_readl(bdc->regs, BDC_BDCCFG0));
370 /* page size is 2^pgs KB */
371 page_size = 1 << page_size;
374 dev_dbg(bdc->dev, "page_size=%d\n", page_size);
376 /* Create a pool of bd tables */
378 dma_pool_create("BDC BD tables", bdc->dev, NUM_BDS_PER_TABLE * 16,
381 if (!bdc->bd_table_pool)
384 if (scratchpad_setup(bdc))
388 num_ieps = NUM_NCS(bdc_readl(bdc->regs, BDC_FSCNIC));
389 num_oeps = NUM_NCS(bdc_readl(bdc->regs, BDC_FSCNOC));
390 /* +2: 1 for ep0 and the other is rsvd i.e. bdc_ep[0] is rsvd */
391 bdc->num_eps = num_ieps + num_oeps + 2;
393 "ieps:%d eops:%d num_eps:%d\n",
394 num_ieps, num_oeps, bdc->num_eps);
395 /* allocate array of ep pointers */
396 bdc->bdc_ep_array = kcalloc(bdc->num_eps, sizeof(struct bdc_ep *),
398 if (!bdc->bdc_ep_array)
401 dev_dbg(bdc->dev, "Allocating sr report0\n");
402 if (setup_srr(bdc, 0))
407 dev_warn(bdc->dev, "Couldn't initialize memory\n");
413 /* opposite to bdc_hw_init */
414 static void bdc_hw_exit(struct bdc *bdc)
416 dev_dbg(bdc->dev, "%s ()\n", __func__);
420 /* Initialize the bdc HW and memory */
421 static int bdc_hw_init(struct bdc *bdc)
425 dev_dbg(bdc->dev, "%s ()\n", __func__);
426 ret = bdc_reset(bdc);
428 dev_err(bdc->dev, "err resetting bdc abort bdc init%d\n", ret);
431 ret = bdc_mem_alloc(bdc);
433 dev_err(bdc->dev, "Mem alloc failed, aborting\n");
436 bdc_mem_init(bdc, 0);
438 dev_dbg(bdc->dev, "HW Init done\n");
443 static int bdc_phy_init(struct bdc *bdc)
448 for (phy_num = 0; phy_num < bdc->num_phys; phy_num++) {
449 ret = phy_init(bdc->phys[phy_num]);
452 ret = phy_power_on(bdc->phys[phy_num]);
454 phy_exit(bdc->phys[phy_num]);
462 while (--phy_num >= 0) {
463 phy_power_off(bdc->phys[phy_num]);
464 phy_exit(bdc->phys[phy_num]);
470 static void bdc_phy_exit(struct bdc *bdc)
474 for (phy_num = 0; phy_num < bdc->num_phys; phy_num++) {
475 phy_power_off(bdc->phys[phy_num]);
476 phy_exit(bdc->phys[phy_num]);
480 static int bdc_probe(struct platform_device *pdev)
483 struct resource *res;
487 struct device *dev = &pdev->dev;
491 dev_dbg(dev, "%s()\n", __func__);
493 clk = devm_clk_get(dev, "sw_usbd");
495 dev_info(dev, "Clock not found in Device Tree\n");
499 ret = clk_prepare_enable(clk);
501 dev_err(dev, "could not enable clock\n");
505 bdc = devm_kzalloc(dev, sizeof(*bdc), GFP_KERNEL);
511 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
512 bdc->regs = devm_ioremap_resource(dev, res);
513 if (IS_ERR(bdc->regs)) {
514 dev_err(dev, "ioremap error\n");
517 irq = platform_get_irq(pdev, 0);
519 dev_err(dev, "platform_get_irq failed:%d\n", irq);
522 spin_lock_init(&bdc->lock);
523 platform_set_drvdata(pdev, bdc);
526 dev_dbg(dev, "bdc->regs: %p irq=%d\n", bdc->regs, bdc->irq);
528 bdc->num_phys = of_count_phandle_with_args(dev->of_node,
529 "phys", "#phy-cells");
530 if (bdc->num_phys > 0) {
531 bdc->phys = devm_kcalloc(dev, bdc->num_phys,
532 sizeof(struct phy *), GFP_KERNEL);
538 dev_info(dev, "Using %d phy(s)\n", bdc->num_phys);
540 for (phy_num = 0; phy_num < bdc->num_phys; phy_num++) {
541 bdc->phys[phy_num] = devm_of_phy_get_by_index(
542 dev, dev->of_node, phy_num);
543 if (IS_ERR(bdc->phys[phy_num])) {
544 ret = PTR_ERR(bdc->phys[phy_num]);
546 "BDC phy specified but not found:%d\n", ret);
551 ret = bdc_phy_init(bdc);
553 dev_err(bdc->dev, "BDC phy init failure:%d\n", ret);
557 temp = bdc_readl(bdc->regs, BDC_BDCCAP1);
558 if ((temp & BDC_P64) &&
559 !dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64))) {
560 dev_dbg(dev, "Using 64-bit address\n");
562 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
565 "No suitable DMA config available, abort\n");
568 dev_dbg(dev, "Using 32-bit address\n");
570 ret = bdc_hw_init(bdc);
572 dev_err(dev, "BDC init failure:%d\n", ret);
575 ret = bdc_udc_init(bdc);
577 dev_err(dev, "BDC Gadget init failure:%d\n", ret);
589 static int bdc_remove(struct platform_device *pdev)
593 bdc = platform_get_drvdata(pdev);
594 dev_dbg(bdc->dev, "%s ()\n", __func__);
598 clk_disable_unprepare(bdc->clk);
602 #ifdef CONFIG_PM_SLEEP
603 static int bdc_suspend(struct device *dev)
605 struct bdc *bdc = dev_get_drvdata(dev);
607 clk_disable_unprepare(bdc->clk);
611 static int bdc_resume(struct device *dev)
613 struct bdc *bdc = dev_get_drvdata(dev);
616 ret = clk_prepare_enable(bdc->clk);
618 dev_err(bdc->dev, "err enabling the clock\n");
621 ret = bdc_reinit(bdc);
623 dev_err(bdc->dev, "err in bdc reinit\n");
630 #endif /* CONFIG_PM_SLEEP */
632 static SIMPLE_DEV_PM_OPS(bdc_pm_ops, bdc_suspend,
635 static const struct of_device_id bdc_of_match[] = {
636 { .compatible = "brcm,bdc-v0.16" },
637 { .compatible = "brcm,bdc" },
641 static struct platform_driver bdc_driver = {
643 .name = BRCM_BDC_NAME,
645 .of_match_table = bdc_of_match,
648 .remove = bdc_remove,
651 module_platform_driver(bdc_driver);
652 MODULE_AUTHOR("Ashwini Pahuja <ashwini.linux@gmail.com>");
653 MODULE_LICENSE("GPL");
654 MODULE_DESCRIPTION(BRCM_BDC_DESC);