bdc_core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*
  2. * bdc_core.c - BRCM BDC USB3.0 device controller core operations
  3. *
  4. * Copyright (C) 2014 Broadcom Corporation
  5. *
  6. * Author: Ashwini Pahuja
  7. *
  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.
  12. *
  13. */
  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>
  21. #include <linux/io.h>
  22. #include <linux/list.h>
  23. #include <linux/delay.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/dmapool.h>
  26. #include <linux/of.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/usb/ch9.h>
  29. #include <linux/usb/gadget.h>
  30. #include "bdc.h"
  31. #include "bdc_dbg.h"
  32. /* Poll till controller status is not OIP */
  33. static int poll_oip(struct bdc *bdc, int usec)
  34. {
  35. u32 status;
  36. /* Poll till STS!= OIP */
  37. while (usec) {
  38. status = bdc_readl(bdc->regs, BDC_BDCSC);
  39. if (BDC_CSTS(status) != BDC_OIP) {
  40. dev_dbg(bdc->dev,
  41. "poll_oip complete status=%d",
  42. BDC_CSTS(status));
  43. return 0;
  44. }
  45. udelay(10);
  46. usec -= 10;
  47. }
  48. dev_err(bdc->dev, "Err: operation timedout BDCSC: 0x%08x\n", status);
  49. return -ETIMEDOUT;
  50. }
  51. /* Stop the BDC controller */
  52. int bdc_stop(struct bdc *bdc)
  53. {
  54. int ret;
  55. u32 temp;
  56. dev_dbg(bdc->dev, "%s ()\n\n", __func__);
  57. temp = bdc_readl(bdc->regs, BDC_BDCSC);
  58. /* Check if BDC is already halted */
  59. if (BDC_CSTS(temp) == BDC_HLT) {
  60. dev_vdbg(bdc->dev, "BDC already halted\n");
  61. return 0;
  62. }
  63. temp &= ~BDC_COP_MASK;
  64. temp |= BDC_COS|BDC_COP_STP;
  65. bdc_writel(bdc->regs, BDC_BDCSC, temp);
  66. ret = poll_oip(bdc, BDC_COP_TIMEOUT);
  67. if (ret)
  68. dev_err(bdc->dev, "bdc stop operation failed");
  69. return ret;
  70. }
  71. /* Issue a reset to BDC controller */
  72. int bdc_reset(struct bdc *bdc)
  73. {
  74. u32 temp;
  75. int ret;
  76. dev_dbg(bdc->dev, "%s ()\n", __func__);
  77. /* First halt the controller */
  78. ret = bdc_stop(bdc);
  79. if (ret)
  80. return ret;
  81. temp = bdc_readl(bdc->regs, BDC_BDCSC);
  82. temp &= ~BDC_COP_MASK;
  83. temp |= BDC_COS|BDC_COP_RST;
  84. bdc_writel(bdc->regs, BDC_BDCSC, temp);
  85. ret = poll_oip(bdc, BDC_COP_TIMEOUT);
  86. if (ret)
  87. dev_err(bdc->dev, "bdc reset operation failed");
  88. return ret;
  89. }
  90. /* Run the BDC controller */
  91. int bdc_run(struct bdc *bdc)
  92. {
  93. u32 temp;
  94. int ret;
  95. dev_dbg(bdc->dev, "%s ()\n", __func__);
  96. temp = bdc_readl(bdc->regs, BDC_BDCSC);
  97. /* if BDC is already in running state then do not do anything */
  98. if (BDC_CSTS(temp) == BDC_NOR) {
  99. dev_warn(bdc->dev, "bdc is already in running state\n");
  100. return 0;
  101. }
  102. temp &= ~BDC_COP_MASK;
  103. temp |= BDC_COP_RUN;
  104. temp |= BDC_COS;
  105. bdc_writel(bdc->regs, BDC_BDCSC, temp);
  106. ret = poll_oip(bdc, BDC_COP_TIMEOUT);
  107. if (ret) {
  108. dev_err(bdc->dev, "bdc run operation failed:%d", ret);
  109. return ret;
  110. }
  111. temp = bdc_readl(bdc->regs, BDC_BDCSC);
  112. if (BDC_CSTS(temp) != BDC_NOR) {
  113. dev_err(bdc->dev, "bdc not in normal mode after RUN op :%d\n",
  114. BDC_CSTS(temp));
  115. return -ESHUTDOWN;
  116. }
  117. return 0;
  118. }
  119. /*
  120. * Present the termination to the host, typically called from upstream port
  121. * event with Vbus present =1
  122. */
  123. void bdc_softconn(struct bdc *bdc)
  124. {
  125. u32 uspc;
  126. uspc = bdc_readl(bdc->regs, BDC_USPC);
  127. uspc &= ~BDC_PST_MASK;
  128. uspc |= BDC_LINK_STATE_RX_DET;
  129. uspc |= BDC_SWS;
  130. dev_dbg(bdc->dev, "%s () uspc=%08x\n", __func__, uspc);
  131. bdc_writel(bdc->regs, BDC_USPC, uspc);
  132. }
  133. /* Remove the termination */
  134. void bdc_softdisconn(struct bdc *bdc)
  135. {
  136. u32 uspc;
  137. uspc = bdc_readl(bdc->regs, BDC_USPC);
  138. uspc |= BDC_SDC;
  139. uspc &= ~BDC_SCN;
  140. dev_dbg(bdc->dev, "%s () uspc=%x\n", __func__, uspc);
  141. bdc_writel(bdc->regs, BDC_USPC, uspc);
  142. }
  143. /* Set up the scratchpad buffer array and scratchpad buffers, if needed. */
  144. static int scratchpad_setup(struct bdc *bdc)
  145. {
  146. int sp_buff_size;
  147. u32 low32;
  148. u32 upp32;
  149. sp_buff_size = BDC_SPB(bdc_readl(bdc->regs, BDC_BDCCFG0));
  150. dev_dbg(bdc->dev, "%s() sp_buff_size=%d\n", __func__, sp_buff_size);
  151. if (!sp_buff_size) {
  152. dev_dbg(bdc->dev, "Scratchpad buffer not needed\n");
  153. return 0;
  154. }
  155. /* Refer to BDC spec, Table 4 for description of SPB */
  156. sp_buff_size = 1 << (sp_buff_size + 5);
  157. dev_dbg(bdc->dev, "Allocating %d bytes for scratchpad\n", sp_buff_size);
  158. bdc->scratchpad.buff = dma_zalloc_coherent(bdc->dev, sp_buff_size,
  159. &bdc->scratchpad.sp_dma, GFP_KERNEL);
  160. if (!bdc->scratchpad.buff)
  161. goto fail;
  162. bdc->sp_buff_size = sp_buff_size;
  163. bdc->scratchpad.size = sp_buff_size;
  164. low32 = lower_32_bits(bdc->scratchpad.sp_dma);
  165. upp32 = upper_32_bits(bdc->scratchpad.sp_dma);
  166. cpu_to_le32s(&low32);
  167. cpu_to_le32s(&upp32);
  168. bdc_writel(bdc->regs, BDC_SPBBAL, low32);
  169. bdc_writel(bdc->regs, BDC_SPBBAH, upp32);
  170. return 0;
  171. fail:
  172. bdc->scratchpad.buff = NULL;
  173. return -ENOMEM;
  174. }
  175. /* Allocate the status report ring */
  176. static int setup_srr(struct bdc *bdc, int interrupter)
  177. {
  178. dev_dbg(bdc->dev, "%s() NUM_SR_ENTRIES:%d\n", __func__, NUM_SR_ENTRIES);
  179. /* Reset the SRR */
  180. bdc_writel(bdc->regs, BDC_SRRINT(0), BDC_SRR_RWS | BDC_SRR_RST);
  181. bdc->srr.dqp_index = 0;
  182. /* allocate the status report descriptors */
  183. bdc->srr.sr_bds = dma_zalloc_coherent(
  184. bdc->dev,
  185. NUM_SR_ENTRIES * sizeof(struct bdc_bd),
  186. &bdc->srr.dma_addr,
  187. GFP_KERNEL);
  188. if (!bdc->srr.sr_bds)
  189. return -ENOMEM;
  190. return 0;
  191. }
  192. /* Initialize the HW regs and internal data structures */
  193. static void bdc_mem_init(struct bdc *bdc, bool reinit)
  194. {
  195. u8 size = 0;
  196. u32 usb2_pm;
  197. u32 low32;
  198. u32 upp32;
  199. u32 temp;
  200. dev_dbg(bdc->dev, "%s ()\n", __func__);
  201. bdc->ep0_state = WAIT_FOR_SETUP;
  202. bdc->dev_addr = 0;
  203. bdc->srr.eqp_index = 0;
  204. bdc->srr.dqp_index = 0;
  205. bdc->zlp_needed = false;
  206. bdc->delayed_status = false;
  207. bdc_writel(bdc->regs, BDC_SPBBAL, bdc->scratchpad.sp_dma);
  208. /* Init the SRR */
  209. temp = BDC_SRR_RWS | BDC_SRR_RST;
  210. /* Reset the SRR */
  211. bdc_writel(bdc->regs, BDC_SRRINT(0), temp);
  212. dev_dbg(bdc->dev, "bdc->srr.sr_bds =%p\n", bdc->srr.sr_bds);
  213. temp = lower_32_bits(bdc->srr.dma_addr);
  214. size = fls(NUM_SR_ENTRIES) - 2;
  215. temp |= size;
  216. dev_dbg(bdc->dev, "SRRBAL[0]=%08x NUM_SR_ENTRIES:%d size:%d\n",
  217. temp, NUM_SR_ENTRIES, size);
  218. low32 = lower_32_bits(temp);
  219. upp32 = upper_32_bits(bdc->srr.dma_addr);
  220. cpu_to_le32s(&low32);
  221. cpu_to_le32s(&upp32);
  222. /* Write the dma addresses into regs*/
  223. bdc_writel(bdc->regs, BDC_SRRBAL(0), low32);
  224. bdc_writel(bdc->regs, BDC_SRRBAH(0), upp32);
  225. temp = bdc_readl(bdc->regs, BDC_SRRINT(0));
  226. temp |= BDC_SRR_IE;
  227. temp &= ~(BDC_SRR_RST | BDC_SRR_RWS);
  228. bdc_writel(bdc->regs, BDC_SRRINT(0), temp);
  229. /* Set the Interrupt Coalescence ~500 usec */
  230. temp = bdc_readl(bdc->regs, BDC_INTCTLS(0));
  231. temp &= ~0xffff;
  232. temp |= INT_CLS;
  233. bdc_writel(bdc->regs, BDC_INTCTLS(0), temp);
  234. usb2_pm = bdc_readl(bdc->regs, BDC_USPPM2);
  235. dev_dbg(bdc->dev, "usb2_pm=%08x", usb2_pm);
  236. /* Enable hardware LPM Enable */
  237. usb2_pm |= BDC_HLE;
  238. bdc_writel(bdc->regs, BDC_USPPM2, usb2_pm);
  239. /* readback for debug */
  240. usb2_pm = bdc_readl(bdc->regs, BDC_USPPM2);
  241. dev_dbg(bdc->dev, "usb2_pm=%08x\n", usb2_pm);
  242. /* Disable any unwanted SR's on SRR */
  243. temp = bdc_readl(bdc->regs, BDC_BDCSC);
  244. /* We don't want Microframe counter wrap SR */
  245. temp |= BDC_MASK_MCW;
  246. bdc_writel(bdc->regs, BDC_BDCSC, temp);
  247. /*
  248. * In some error cases, driver has to reset the entire BDC controller
  249. * in that case reinit is passed as 1
  250. */
  251. if (reinit) {
  252. /* Enable interrupts */
  253. temp = bdc_readl(bdc->regs, BDC_BDCSC);
  254. temp |= BDC_GIE;
  255. bdc_writel(bdc->regs, BDC_BDCSC, temp);
  256. /* Init scratchpad to 0 */
  257. memset(bdc->scratchpad.buff, 0, bdc->sp_buff_size);
  258. /* Initialize SRR to 0 */
  259. memset(bdc->srr.sr_bds, 0,
  260. NUM_SR_ENTRIES * sizeof(struct bdc_bd));
  261. } else {
  262. /* One time initiaization only */
  263. /* Enable status report function pointers */
  264. bdc->sr_handler[0] = bdc_sr_xsf;
  265. bdc->sr_handler[1] = bdc_sr_uspc;
  266. /* EP0 status report function pointers */
  267. bdc->sr_xsf_ep0[0] = bdc_xsf_ep0_setup_recv;
  268. bdc->sr_xsf_ep0[1] = bdc_xsf_ep0_data_start;
  269. bdc->sr_xsf_ep0[2] = bdc_xsf_ep0_status_start;
  270. }
  271. }
  272. /* Free the dynamic memory */
  273. static void bdc_mem_free(struct bdc *bdc)
  274. {
  275. dev_dbg(bdc->dev, "%s\n", __func__);
  276. /* Free SRR */
  277. if (bdc->srr.sr_bds)
  278. dma_free_coherent(bdc->dev,
  279. NUM_SR_ENTRIES * sizeof(struct bdc_bd),
  280. bdc->srr.sr_bds, bdc->srr.dma_addr);
  281. /* Free scratchpad */
  282. if (bdc->scratchpad.buff)
  283. dma_free_coherent(bdc->dev, bdc->sp_buff_size,
  284. bdc->scratchpad.buff, bdc->scratchpad.sp_dma);
  285. /* Destroy the dma pools */
  286. dma_pool_destroy(bdc->bd_table_pool);
  287. /* Free the bdc_ep array */
  288. kfree(bdc->bdc_ep_array);
  289. bdc->srr.sr_bds = NULL;
  290. bdc->scratchpad.buff = NULL;
  291. bdc->bd_table_pool = NULL;
  292. bdc->bdc_ep_array = NULL;
  293. }
  294. /*
  295. * bdc reinit gives a controller reset and reinitialize the registers,
  296. * called from disconnect/bus reset scenario's, to ensure proper HW cleanup
  297. */
  298. int bdc_reinit(struct bdc *bdc)
  299. {
  300. int ret;
  301. dev_dbg(bdc->dev, "%s\n", __func__);
  302. ret = bdc_stop(bdc);
  303. if (ret)
  304. goto out;
  305. ret = bdc_reset(bdc);
  306. if (ret)
  307. goto out;
  308. /* the reinit flag is 1 */
  309. bdc_mem_init(bdc, true);
  310. ret = bdc_run(bdc);
  311. out:
  312. bdc->reinit = false;
  313. return ret;
  314. }
  315. /* Allocate all the dyanmic memory */
  316. static int bdc_mem_alloc(struct bdc *bdc)
  317. {
  318. u32 page_size;
  319. unsigned int num_ieps, num_oeps;
  320. dev_dbg(bdc->dev,
  321. "%s() NUM_BDS_PER_TABLE:%d\n", __func__,
  322. NUM_BDS_PER_TABLE);
  323. page_size = BDC_PGS(bdc_readl(bdc->regs, BDC_BDCCFG0));
  324. /* page size is 2^pgs KB */
  325. page_size = 1 << page_size;
  326. /* KB */
  327. page_size <<= 10;
  328. dev_dbg(bdc->dev, "page_size=%d\n", page_size);
  329. /* Create a pool of bd tables */
  330. bdc->bd_table_pool =
  331. dma_pool_create("BDC BD tables", bdc->dev, NUM_BDS_PER_TABLE * 16,
  332. 16, page_size);
  333. if (!bdc->bd_table_pool)
  334. goto fail;
  335. if (scratchpad_setup(bdc))
  336. goto fail;
  337. /* read from regs */
  338. num_ieps = NUM_NCS(bdc_readl(bdc->regs, BDC_FSCNIC));
  339. num_oeps = NUM_NCS(bdc_readl(bdc->regs, BDC_FSCNOC));
  340. /* +2: 1 for ep0 and the other is rsvd i.e. bdc_ep[0] is rsvd */
  341. bdc->num_eps = num_ieps + num_oeps + 2;
  342. dev_dbg(bdc->dev,
  343. "ieps:%d eops:%d num_eps:%d\n",
  344. num_ieps, num_oeps, bdc->num_eps);
  345. /* allocate array of ep pointers */
  346. bdc->bdc_ep_array = kcalloc(bdc->num_eps, sizeof(struct bdc_ep *),
  347. GFP_KERNEL);
  348. if (!bdc->bdc_ep_array)
  349. goto fail;
  350. dev_dbg(bdc->dev, "Allocating sr report0\n");
  351. if (setup_srr(bdc, 0))
  352. goto fail;
  353. return 0;
  354. fail:
  355. dev_warn(bdc->dev, "Couldn't initialize memory\n");
  356. bdc_mem_free(bdc);
  357. return -ENOMEM;
  358. }
  359. /* opposite to bdc_hw_init */
  360. static void bdc_hw_exit(struct bdc *bdc)
  361. {
  362. dev_dbg(bdc->dev, "%s ()\n", __func__);
  363. bdc_mem_free(bdc);
  364. }
  365. /* Initialize the bdc HW and memory */
  366. static int bdc_hw_init(struct bdc *bdc)
  367. {
  368. int ret;
  369. dev_dbg(bdc->dev, "%s ()\n", __func__);
  370. ret = bdc_reset(bdc);
  371. if (ret) {
  372. dev_err(bdc->dev, "err resetting bdc abort bdc init%d\n", ret);
  373. return ret;
  374. }
  375. ret = bdc_mem_alloc(bdc);
  376. if (ret) {
  377. dev_err(bdc->dev, "Mem alloc failed, aborting\n");
  378. return -ENOMEM;
  379. }
  380. bdc_mem_init(bdc, 0);
  381. bdc_dbg_regs(bdc);
  382. dev_dbg(bdc->dev, "HW Init done\n");
  383. return 0;
  384. }
  385. static int bdc_probe(struct platform_device *pdev)
  386. {
  387. struct bdc *bdc;
  388. struct resource *res;
  389. int ret = -ENOMEM;
  390. int irq;
  391. u32 temp;
  392. struct device *dev = &pdev->dev;
  393. dev_dbg(dev, "%s()\n", __func__);
  394. bdc = devm_kzalloc(dev, sizeof(*bdc), GFP_KERNEL);
  395. if (!bdc)
  396. return -ENOMEM;
  397. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  398. bdc->regs = devm_ioremap_resource(dev, res);
  399. if (IS_ERR(bdc->regs)) {
  400. dev_err(dev, "ioremap error\n");
  401. return -ENOMEM;
  402. }
  403. irq = platform_get_irq(pdev, 0);
  404. if (irq < 0) {
  405. dev_err(dev, "platform_get_irq failed:%d\n", irq);
  406. return irq;
  407. }
  408. spin_lock_init(&bdc->lock);
  409. platform_set_drvdata(pdev, bdc);
  410. bdc->irq = irq;
  411. bdc->dev = dev;
  412. dev_dbg(bdc->dev, "bdc->regs: %p irq=%d\n", bdc->regs, bdc->irq);
  413. temp = bdc_readl(bdc->regs, BDC_BDCCAP1);
  414. if ((temp & BDC_P64) &&
  415. !dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64))) {
  416. dev_dbg(bdc->dev, "Using 64-bit address\n");
  417. } else {
  418. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  419. if (ret) {
  420. dev_err(bdc->dev, "No suitable DMA config available, abort\n");
  421. return -ENOTSUPP;
  422. }
  423. dev_dbg(bdc->dev, "Using 32-bit address\n");
  424. }
  425. ret = bdc_hw_init(bdc);
  426. if (ret) {
  427. dev_err(bdc->dev, "BDC init failure:%d\n", ret);
  428. return ret;
  429. }
  430. ret = bdc_udc_init(bdc);
  431. if (ret) {
  432. dev_err(bdc->dev, "BDC Gadget init failure:%d\n", ret);
  433. goto cleanup;
  434. }
  435. return 0;
  436. cleanup:
  437. bdc_hw_exit(bdc);
  438. return ret;
  439. }
  440. static int bdc_remove(struct platform_device *pdev)
  441. {
  442. struct bdc *bdc;
  443. bdc = platform_get_drvdata(pdev);
  444. dev_dbg(bdc->dev, "%s ()\n", __func__);
  445. bdc_udc_exit(bdc);
  446. bdc_hw_exit(bdc);
  447. return 0;
  448. }
  449. static struct platform_driver bdc_driver = {
  450. .driver = {
  451. .name = BRCM_BDC_NAME,
  452. },
  453. .probe = bdc_probe,
  454. .remove = bdc_remove,
  455. };
  456. module_platform_driver(bdc_driver);
  457. MODULE_AUTHOR("Ashwini Pahuja <ashwini.linux@gmail.com>");
  458. MODULE_LICENSE("GPL");
  459. MODULE_DESCRIPTION(BRCM_BDC_DESC);