2 * bdc_core.c - BRCM BDC USB3.0 device controller core operations
4 * Copyright (C) 2014 Broadcom Corporation
6 * Author: Ashwini Pahuja
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
18 #include <linux/platform_device.h>
19 #include <linux/interrupt.h>
20 #include <linux/ioport.h>
22 #include <linux/list.h>
23 #include <linux/delay.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/dmapool.h>
27 #include <linux/phy/phy.h>
28 #include <linux/moduleparam.h>
29 #include <linux/usb/ch9.h>
30 #include <linux/usb/gadget.h>
31 #include <linux/clk.h>
36 /* Poll till controller status is not OIP */
37 static int poll_oip(struct bdc *bdc, int usec)
40 /* Poll till STS!= OIP */
42 status = bdc_readl(bdc->regs, BDC_BDCSC);
43 if (BDC_CSTS(status) != BDC_OIP) {
45 "poll_oip complete status=%d",
52 dev_err(bdc->dev, "Err: operation timedout BDCSC: 0x%08x\n", status);
57 /* Stop the BDC controller */
58 int bdc_stop(struct bdc *bdc)
63 dev_dbg(bdc->dev, "%s ()\n\n", __func__);
64 temp = bdc_readl(bdc->regs, BDC_BDCSC);
65 /* Check if BDC is already halted */
66 if (BDC_CSTS(temp) == BDC_HLT) {
67 dev_vdbg(bdc->dev, "BDC already halted\n");
70 temp &= ~BDC_COP_MASK;
71 temp |= BDC_COS|BDC_COP_STP;
72 bdc_writel(bdc->regs, BDC_BDCSC, temp);
74 ret = poll_oip(bdc, BDC_COP_TIMEOUT);
76 dev_err(bdc->dev, "bdc stop operation failed");
81 /* Issue a reset to BDC controller */
82 int bdc_reset(struct bdc *bdc)
87 dev_dbg(bdc->dev, "%s ()\n", __func__);
88 /* First halt the controller */
93 temp = bdc_readl(bdc->regs, BDC_BDCSC);
94 temp &= ~BDC_COP_MASK;
95 temp |= BDC_COS|BDC_COP_RST;
96 bdc_writel(bdc->regs, BDC_BDCSC, temp);
97 ret = poll_oip(bdc, BDC_COP_TIMEOUT);
99 dev_err(bdc->dev, "bdc reset operation failed");
104 /* Run the BDC controller */
105 int bdc_run(struct bdc *bdc)
110 dev_dbg(bdc->dev, "%s ()\n", __func__);
111 temp = bdc_readl(bdc->regs, BDC_BDCSC);
112 /* if BDC is already in running state then do not do anything */
113 if (BDC_CSTS(temp) == BDC_NOR) {
114 dev_warn(bdc->dev, "bdc is already in running state\n");
117 temp &= ~BDC_COP_MASK;
120 bdc_writel(bdc->regs, BDC_BDCSC, temp);
121 ret = poll_oip(bdc, BDC_COP_TIMEOUT);
123 dev_err(bdc->dev, "bdc run operation failed:%d", ret);
126 temp = bdc_readl(bdc->regs, BDC_BDCSC);
127 if (BDC_CSTS(temp) != BDC_NOR) {
128 dev_err(bdc->dev, "bdc not in normal mode after RUN op :%d\n",
137 * Present the termination to the host, typically called from upstream port
138 * event with Vbus present =1
140 void bdc_softconn(struct bdc *bdc)
144 uspc = bdc_readl(bdc->regs, BDC_USPC);
145 uspc &= ~BDC_PST_MASK;
146 uspc |= BDC_LINK_STATE_RX_DET;
148 dev_dbg(bdc->dev, "%s () uspc=%08x\n", __func__, uspc);
149 bdc_writel(bdc->regs, BDC_USPC, uspc);
152 /* Remove the termination */
153 void bdc_softdisconn(struct bdc *bdc)
157 uspc = bdc_readl(bdc->regs, BDC_USPC);
160 dev_dbg(bdc->dev, "%s () uspc=%x\n", __func__, uspc);
161 bdc_writel(bdc->regs, BDC_USPC, uspc);
164 /* Set up the scratchpad buffer array and scratchpad buffers, if needed. */
165 static int scratchpad_setup(struct bdc *bdc)
171 sp_buff_size = BDC_SPB(bdc_readl(bdc->regs, BDC_BDCCFG0));
172 dev_dbg(bdc->dev, "%s() sp_buff_size=%d\n", __func__, sp_buff_size);
174 dev_dbg(bdc->dev, "Scratchpad buffer not needed\n");
177 /* Refer to BDC spec, Table 4 for description of SPB */
178 sp_buff_size = 1 << (sp_buff_size + 5);
179 dev_dbg(bdc->dev, "Allocating %d bytes for scratchpad\n", sp_buff_size);
180 bdc->scratchpad.buff = dma_zalloc_coherent(bdc->dev, sp_buff_size,
181 &bdc->scratchpad.sp_dma, GFP_KERNEL);
183 if (!bdc->scratchpad.buff)
186 bdc->sp_buff_size = sp_buff_size;
187 bdc->scratchpad.size = sp_buff_size;
188 low32 = lower_32_bits(bdc->scratchpad.sp_dma);
189 upp32 = upper_32_bits(bdc->scratchpad.sp_dma);
190 cpu_to_le32s(&low32);
191 cpu_to_le32s(&upp32);
192 bdc_writel(bdc->regs, BDC_SPBBAL, low32);
193 bdc_writel(bdc->regs, BDC_SPBBAH, upp32);
197 bdc->scratchpad.buff = NULL;
202 /* Allocate the status report ring */
203 static int setup_srr(struct bdc *bdc, int interrupter)
205 dev_dbg(bdc->dev, "%s() NUM_SR_ENTRIES:%d\n", __func__, NUM_SR_ENTRIES);
207 bdc_writel(bdc->regs, BDC_SRRINT(0), BDC_SRR_RWS | BDC_SRR_RST);
208 bdc->srr.dqp_index = 0;
209 /* allocate the status report descriptors */
210 bdc->srr.sr_bds = dma_zalloc_coherent(
212 NUM_SR_ENTRIES * sizeof(struct bdc_bd),
215 if (!bdc->srr.sr_bds)
221 /* Initialize the HW regs and internal data structures */
222 static void bdc_mem_init(struct bdc *bdc, bool reinit)
230 dev_dbg(bdc->dev, "%s ()\n", __func__);
231 bdc->ep0_state = WAIT_FOR_SETUP;
233 bdc->srr.eqp_index = 0;
234 bdc->srr.dqp_index = 0;
235 bdc->zlp_needed = false;
236 bdc->delayed_status = false;
238 bdc_writel(bdc->regs, BDC_SPBBAL, bdc->scratchpad.sp_dma);
240 temp = BDC_SRR_RWS | BDC_SRR_RST;
242 bdc_writel(bdc->regs, BDC_SRRINT(0), temp);
243 dev_dbg(bdc->dev, "bdc->srr.sr_bds =%p\n", bdc->srr.sr_bds);
244 temp = lower_32_bits(bdc->srr.dma_addr);
245 size = fls(NUM_SR_ENTRIES) - 2;
247 dev_dbg(bdc->dev, "SRRBAL[0]=%08x NUM_SR_ENTRIES:%d size:%d\n",
248 temp, NUM_SR_ENTRIES, size);
250 low32 = lower_32_bits(temp);
251 upp32 = upper_32_bits(bdc->srr.dma_addr);
252 cpu_to_le32s(&low32);
253 cpu_to_le32s(&upp32);
255 /* Write the dma addresses into regs*/
256 bdc_writel(bdc->regs, BDC_SRRBAL(0), low32);
257 bdc_writel(bdc->regs, BDC_SRRBAH(0), upp32);
259 temp = bdc_readl(bdc->regs, BDC_SRRINT(0));
261 temp &= ~(BDC_SRR_RST | BDC_SRR_RWS);
262 bdc_writel(bdc->regs, BDC_SRRINT(0), temp);
264 /* Set the Interrupt Coalescence ~500 usec */
265 temp = bdc_readl(bdc->regs, BDC_INTCTLS(0));
268 bdc_writel(bdc->regs, BDC_INTCTLS(0), temp);
270 usb2_pm = bdc_readl(bdc->regs, BDC_USPPM2);
271 dev_dbg(bdc->dev, "usb2_pm=%08x", usb2_pm);
272 /* Enable hardware LPM Enable */
274 bdc_writel(bdc->regs, BDC_USPPM2, usb2_pm);
276 /* readback for debug */
277 usb2_pm = bdc_readl(bdc->regs, BDC_USPPM2);
278 dev_dbg(bdc->dev, "usb2_pm=%08x\n", usb2_pm);
280 /* Disable any unwanted SR's on SRR */
281 temp = bdc_readl(bdc->regs, BDC_BDCSC);
282 /* We don't want Microframe counter wrap SR */
283 temp |= BDC_MASK_MCW;
284 bdc_writel(bdc->regs, BDC_BDCSC, temp);
287 * In some error cases, driver has to reset the entire BDC controller
288 * in that case reinit is passed as 1
292 /* Enable interrupts */
293 temp = bdc_readl(bdc->regs, BDC_BDCSC);
295 bdc_writel(bdc->regs, BDC_BDCSC, temp);
296 /* Init scratchpad to 0 */
297 memset(bdc->scratchpad.buff, 0, bdc->sp_buff_size);
298 /* Initialize SRR to 0 */
299 memset(bdc->srr.sr_bds, 0,
300 NUM_SR_ENTRIES * sizeof(struct bdc_bd));
301 /* clear ep flags to avoid post disconnect stops/deconfigs */
302 for (i = 1; i < bdc->num_eps; ++i)
303 bdc->bdc_ep_array[i]->flags = 0;
305 /* One time initiaization only */
306 /* Enable status report function pointers */
307 bdc->sr_handler[0] = bdc_sr_xsf;
308 bdc->sr_handler[1] = bdc_sr_uspc;
310 /* EP0 status report function pointers */
311 bdc->sr_xsf_ep0[0] = bdc_xsf_ep0_setup_recv;
312 bdc->sr_xsf_ep0[1] = bdc_xsf_ep0_data_start;
313 bdc->sr_xsf_ep0[2] = bdc_xsf_ep0_status_start;
317 /* Free the dynamic memory */
318 static void bdc_mem_free(struct bdc *bdc)
320 dev_dbg(bdc->dev, "%s\n", __func__);
323 dma_free_coherent(bdc->dev,
324 NUM_SR_ENTRIES * sizeof(struct bdc_bd),
325 bdc->srr.sr_bds, bdc->srr.dma_addr);
327 /* Free scratchpad */
328 if (bdc->scratchpad.buff)
329 dma_free_coherent(bdc->dev, bdc->sp_buff_size,
330 bdc->scratchpad.buff, bdc->scratchpad.sp_dma);
332 /* Destroy the dma pools */
333 dma_pool_destroy(bdc->bd_table_pool);
335 /* Free the bdc_ep array */
336 kfree(bdc->bdc_ep_array);
338 bdc->srr.sr_bds = NULL;
339 bdc->scratchpad.buff = NULL;
340 bdc->bd_table_pool = NULL;
341 bdc->bdc_ep_array = NULL;
345 * bdc reinit gives a controller reset and reinitialize the registers,
346 * called from disconnect/bus reset scenario's, to ensure proper HW cleanup
348 int bdc_reinit(struct bdc *bdc)
352 dev_dbg(bdc->dev, "%s\n", __func__);
357 ret = bdc_reset(bdc);
361 /* the reinit flag is 1 */
362 bdc_mem_init(bdc, true);
370 /* Allocate all the dyanmic memory */
371 static int bdc_mem_alloc(struct bdc *bdc)
374 unsigned int num_ieps, num_oeps;
377 "%s() NUM_BDS_PER_TABLE:%d\n", __func__,
379 page_size = BDC_PGS(bdc_readl(bdc->regs, BDC_BDCCFG0));
380 /* page size is 2^pgs KB */
381 page_size = 1 << page_size;
384 dev_dbg(bdc->dev, "page_size=%d\n", page_size);
386 /* Create a pool of bd tables */
388 dma_pool_create("BDC BD tables", bdc->dev, NUM_BDS_PER_TABLE * 16,
391 if (!bdc->bd_table_pool)
394 if (scratchpad_setup(bdc))
398 num_ieps = NUM_NCS(bdc_readl(bdc->regs, BDC_FSCNIC));
399 num_oeps = NUM_NCS(bdc_readl(bdc->regs, BDC_FSCNOC));
400 /* +2: 1 for ep0 and the other is rsvd i.e. bdc_ep[0] is rsvd */
401 bdc->num_eps = num_ieps + num_oeps + 2;
403 "ieps:%d eops:%d num_eps:%d\n",
404 num_ieps, num_oeps, bdc->num_eps);
405 /* allocate array of ep pointers */
406 bdc->bdc_ep_array = kcalloc(bdc->num_eps, sizeof(struct bdc_ep *),
408 if (!bdc->bdc_ep_array)
411 dev_dbg(bdc->dev, "Allocating sr report0\n");
412 if (setup_srr(bdc, 0))
417 dev_warn(bdc->dev, "Couldn't initialize memory\n");
423 /* opposite to bdc_hw_init */
424 static void bdc_hw_exit(struct bdc *bdc)
426 dev_dbg(bdc->dev, "%s ()\n", __func__);
430 /* Initialize the bdc HW and memory */
431 static int bdc_hw_init(struct bdc *bdc)
435 dev_dbg(bdc->dev, "%s ()\n", __func__);
436 ret = bdc_reset(bdc);
438 dev_err(bdc->dev, "err resetting bdc abort bdc init%d\n", ret);
441 ret = bdc_mem_alloc(bdc);
443 dev_err(bdc->dev, "Mem alloc failed, aborting\n");
446 bdc_mem_init(bdc, 0);
448 dev_dbg(bdc->dev, "HW Init done\n");
453 static int bdc_phy_init(struct bdc *bdc)
458 for (phy_num = 0; phy_num < bdc->num_phys; phy_num++) {
459 ret = phy_init(bdc->phys[phy_num]);
462 ret = phy_power_on(bdc->phys[phy_num]);
464 phy_exit(bdc->phys[phy_num]);
472 while (--phy_num >= 0) {
473 phy_power_off(bdc->phys[phy_num]);
474 phy_exit(bdc->phys[phy_num]);
480 static void bdc_phy_exit(struct bdc *bdc)
484 for (phy_num = 0; phy_num < bdc->num_phys; phy_num++) {
485 phy_power_off(bdc->phys[phy_num]);
486 phy_exit(bdc->phys[phy_num]);
490 static int bdc_probe(struct platform_device *pdev)
493 struct resource *res;
497 struct device *dev = &pdev->dev;
501 dev_dbg(dev, "%s()\n", __func__);
503 clk = devm_clk_get(dev, "sw_usbd");
505 dev_info(dev, "Clock not found in Device Tree\n");
509 ret = clk_prepare_enable(clk);
511 dev_err(dev, "could not enable clock\n");
515 bdc = devm_kzalloc(dev, sizeof(*bdc), GFP_KERNEL);
521 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
522 bdc->regs = devm_ioremap_resource(dev, res);
523 if (IS_ERR(bdc->regs)) {
524 dev_err(dev, "ioremap error\n");
527 irq = platform_get_irq(pdev, 0);
529 dev_err(dev, "platform_get_irq failed:%d\n", irq);
532 spin_lock_init(&bdc->lock);
533 platform_set_drvdata(pdev, bdc);
536 dev_dbg(dev, "bdc->regs: %p irq=%d\n", bdc->regs, bdc->irq);
538 bdc->num_phys = of_count_phandle_with_args(dev->of_node,
539 "phys", "#phy-cells");
540 if (bdc->num_phys > 0) {
541 bdc->phys = devm_kcalloc(dev, bdc->num_phys,
542 sizeof(struct phy *), GFP_KERNEL);
548 dev_info(dev, "Using %d phy(s)\n", bdc->num_phys);
550 for (phy_num = 0; phy_num < bdc->num_phys; phy_num++) {
551 bdc->phys[phy_num] = devm_of_phy_get_by_index(
552 dev, dev->of_node, phy_num);
553 if (IS_ERR(bdc->phys[phy_num])) {
554 ret = PTR_ERR(bdc->phys[phy_num]);
556 "BDC phy specified but not found:%d\n", ret);
561 ret = bdc_phy_init(bdc);
563 dev_err(bdc->dev, "BDC phy init failure:%d\n", ret);
567 temp = bdc_readl(bdc->regs, BDC_BDCCAP1);
568 if ((temp & BDC_P64) &&
569 !dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64))) {
570 dev_dbg(dev, "Using 64-bit address\n");
572 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
575 "No suitable DMA config available, abort\n");
579 dev_dbg(dev, "Using 32-bit address\n");
581 ret = bdc_hw_init(bdc);
583 dev_err(dev, "BDC init failure:%d\n", ret);
586 ret = bdc_udc_init(bdc);
588 dev_err(dev, "BDC Gadget init failure:%d\n", ret);
600 static int bdc_remove(struct platform_device *pdev)
604 bdc = platform_get_drvdata(pdev);
605 dev_dbg(bdc->dev, "%s ()\n", __func__);
609 clk_disable_unprepare(bdc->clk);
613 #ifdef CONFIG_PM_SLEEP
614 static int bdc_suspend(struct device *dev)
616 struct bdc *bdc = dev_get_drvdata(dev);
619 /* Halt the controller */
622 clk_disable_unprepare(bdc->clk);
627 static int bdc_resume(struct device *dev)
629 struct bdc *bdc = dev_get_drvdata(dev);
632 ret = clk_prepare_enable(bdc->clk);
634 dev_err(bdc->dev, "err enabling the clock\n");
637 ret = bdc_reinit(bdc);
639 dev_err(bdc->dev, "err in bdc reinit\n");
646 #endif /* CONFIG_PM_SLEEP */
648 static SIMPLE_DEV_PM_OPS(bdc_pm_ops, bdc_suspend,
651 static const struct of_device_id bdc_of_match[] = {
652 { .compatible = "brcm,bdc-v0.16" },
653 { .compatible = "brcm,bdc" },
657 static struct platform_driver bdc_driver = {
659 .name = BRCM_BDC_NAME,
660 .owner = THIS_MODULE,
662 .of_match_table = bdc_of_match,
665 .remove = bdc_remove,
668 module_platform_driver(bdc_driver);
669 MODULE_AUTHOR("Ashwini Pahuja <ashwini.linux@gmail.com>");
670 MODULE_LICENSE("GPL");
671 MODULE_DESCRIPTION(BRCM_BDC_DESC);