bcma-hcd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * Broadcom specific Advanced Microcontroller Bus
  3. * Broadcom USB-core driver (BCMA bus glue)
  4. *
  5. * Copyright 2011-2015 Hauke Mehrtens <hauke@hauke-m.de>
  6. * Copyright 2015 Felix Fietkau <nbd@openwrt.org>
  7. *
  8. * Based on ssb-ohci driver
  9. * Copyright 2007 Michael Buesch <m@bues.ch>
  10. *
  11. * Derived from the OHCI-PCI driver
  12. * Copyright 1999 Roman Weissgaerber
  13. * Copyright 2000-2002 David Brownell
  14. * Copyright 1999 Linus Torvalds
  15. * Copyright 1999 Gregory P. Smith
  16. *
  17. * Derived from the USBcore related parts of Broadcom-SB
  18. * Copyright 2005-2011 Broadcom Corporation
  19. *
  20. * Licensed under the GNU/GPL. See COPYING for details.
  21. */
  22. #include <linux/bcma/bcma.h>
  23. #include <linux/delay.h>
  24. #include <linux/gpio/consumer.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/module.h>
  27. #include <linux/slab.h>
  28. #include <linux/of.h>
  29. #include <linux/of_gpio.h>
  30. #include <linux/of_platform.h>
  31. #include <linux/usb/ehci_pdriver.h>
  32. #include <linux/usb/ohci_pdriver.h>
  33. MODULE_AUTHOR("Hauke Mehrtens");
  34. MODULE_DESCRIPTION("Common USB driver for BCMA Bus");
  35. MODULE_LICENSE("GPL");
  36. /* See BCMA_CLKCTLST_EXTRESREQ and BCMA_CLKCTLST_EXTRESST */
  37. #define USB_BCMA_CLKCTLST_USB_CLK_REQ 0x00000100
  38. struct bcma_hcd_device {
  39. struct bcma_device *core;
  40. struct platform_device *ehci_dev;
  41. struct platform_device *ohci_dev;
  42. struct gpio_desc *gpio_desc;
  43. };
  44. /* Wait for bitmask in a register to get set or cleared.
  45. * timeout is in units of ten-microseconds.
  46. */
  47. static int bcma_wait_bits(struct bcma_device *dev, u16 reg, u32 bitmask,
  48. int timeout)
  49. {
  50. int i;
  51. u32 val;
  52. for (i = 0; i < timeout; i++) {
  53. val = bcma_read32(dev, reg);
  54. if ((val & bitmask) == bitmask)
  55. return 0;
  56. udelay(10);
  57. }
  58. return -ETIMEDOUT;
  59. }
  60. static void bcma_hcd_4716wa(struct bcma_device *dev)
  61. {
  62. #ifdef CONFIG_BCMA_DRIVER_MIPS
  63. /* Work around for 4716 failures. */
  64. if (dev->bus->chipinfo.id == 0x4716) {
  65. u32 tmp;
  66. tmp = bcma_cpu_clock(&dev->bus->drv_mips);
  67. if (tmp >= 480000000)
  68. tmp = 0x1846b; /* set CDR to 0x11(fast) */
  69. else if (tmp == 453000000)
  70. tmp = 0x1046b; /* set CDR to 0x10(slow) */
  71. else
  72. tmp = 0;
  73. /* Change Shim mdio control reg to fix host not acking at
  74. * high frequencies
  75. */
  76. if (tmp) {
  77. bcma_write32(dev, 0x524, 0x1); /* write sel to enable */
  78. udelay(500);
  79. bcma_write32(dev, 0x524, tmp);
  80. udelay(500);
  81. bcma_write32(dev, 0x524, 0x4ab);
  82. udelay(500);
  83. bcma_read32(dev, 0x528);
  84. bcma_write32(dev, 0x528, 0x80000000);
  85. }
  86. }
  87. #endif /* CONFIG_BCMA_DRIVER_MIPS */
  88. }
  89. /* based on arch/mips/brcm-boards/bcm947xx/pcibios.c */
  90. static void bcma_hcd_init_chip_mips(struct bcma_device *dev)
  91. {
  92. u32 tmp;
  93. /*
  94. * USB 2.0 special considerations:
  95. *
  96. * 1. Since the core supports both OHCI and EHCI functions, it must
  97. * only be reset once.
  98. *
  99. * 2. In addition to the standard SI reset sequence, the Host Control
  100. * Register must be programmed to bring the USB core and various
  101. * phy components out of reset.
  102. */
  103. if (!bcma_core_is_enabled(dev)) {
  104. bcma_core_enable(dev, 0);
  105. mdelay(10);
  106. if (dev->id.rev >= 5) {
  107. /* Enable Misc PLL */
  108. tmp = bcma_read32(dev, 0x1e0);
  109. tmp |= 0x100;
  110. bcma_write32(dev, 0x1e0, tmp);
  111. if (bcma_wait_bits(dev, 0x1e0, 1 << 24, 100))
  112. printk(KERN_EMERG "Failed to enable misc PPL!\n");
  113. /* Take out of resets */
  114. bcma_write32(dev, 0x200, 0x4ff);
  115. udelay(25);
  116. bcma_write32(dev, 0x200, 0x6ff);
  117. udelay(25);
  118. /* Make sure digital and AFE are locked in USB PHY */
  119. bcma_write32(dev, 0x524, 0x6b);
  120. udelay(50);
  121. tmp = bcma_read32(dev, 0x524);
  122. udelay(50);
  123. bcma_write32(dev, 0x524, 0xab);
  124. udelay(50);
  125. tmp = bcma_read32(dev, 0x524);
  126. udelay(50);
  127. bcma_write32(dev, 0x524, 0x2b);
  128. udelay(50);
  129. tmp = bcma_read32(dev, 0x524);
  130. udelay(50);
  131. bcma_write32(dev, 0x524, 0x10ab);
  132. udelay(50);
  133. tmp = bcma_read32(dev, 0x524);
  134. if (bcma_wait_bits(dev, 0x528, 0xc000, 10000)) {
  135. tmp = bcma_read32(dev, 0x528);
  136. printk(KERN_EMERG
  137. "USB20H mdio_rddata 0x%08x\n", tmp);
  138. }
  139. bcma_write32(dev, 0x528, 0x80000000);
  140. tmp = bcma_read32(dev, 0x314);
  141. udelay(265);
  142. bcma_write32(dev, 0x200, 0x7ff);
  143. udelay(10);
  144. /* Take USB and HSIC out of non-driving modes */
  145. bcma_write32(dev, 0x510, 0);
  146. } else {
  147. bcma_write32(dev, 0x200, 0x7ff);
  148. udelay(1);
  149. }
  150. bcma_hcd_4716wa(dev);
  151. }
  152. }
  153. /**
  154. * bcma_hcd_usb20_old_arm_init - Initialize old USB 2.0 controller on ARM
  155. *
  156. * Old USB 2.0 core is identified as BCMA_CORE_USB20_HOST and was introduced
  157. * long before Northstar devices. It seems some cheaper chipsets like BCM53573
  158. * still use it.
  159. * Initialization of this old core differs between MIPS and ARM.
  160. */
  161. static int bcma_hcd_usb20_old_arm_init(struct bcma_hcd_device *usb_dev)
  162. {
  163. struct bcma_device *core = usb_dev->core;
  164. struct device *dev = &core->dev;
  165. struct bcma_device *pmu_core;
  166. usleep_range(10000, 20000);
  167. if (core->id.rev < 5)
  168. return 0;
  169. pmu_core = bcma_find_core(core->bus, BCMA_CORE_PMU);
  170. if (!pmu_core) {
  171. dev_err(dev, "Could not find PMU core\n");
  172. return -ENOENT;
  173. }
  174. /* Take USB core out of reset */
  175. bcma_awrite32(core, BCMA_IOCTL, BCMA_IOCTL_CLK | BCMA_IOCTL_FGC);
  176. usleep_range(100, 200);
  177. bcma_awrite32(core, BCMA_RESET_CTL, BCMA_RESET_CTL_RESET);
  178. usleep_range(100, 200);
  179. bcma_awrite32(core, BCMA_RESET_CTL, 0);
  180. usleep_range(100, 200);
  181. bcma_awrite32(core, BCMA_IOCTL, BCMA_IOCTL_CLK);
  182. usleep_range(100, 200);
  183. /* Enable Misc PLL */
  184. bcma_write32(core, BCMA_CLKCTLST, BCMA_CLKCTLST_FORCEHT |
  185. BCMA_CLKCTLST_HQCLKREQ |
  186. USB_BCMA_CLKCTLST_USB_CLK_REQ);
  187. usleep_range(100, 200);
  188. bcma_write32(core, 0x510, 0xc7f85000);
  189. bcma_write32(core, 0x510, 0xc7f85003);
  190. usleep_range(300, 600);
  191. /* Program USB PHY PLL parameters */
  192. bcma_write32(pmu_core, BCMA_CC_PMU_PLLCTL_ADDR, 0x6);
  193. bcma_write32(pmu_core, BCMA_CC_PMU_PLLCTL_DATA, 0x005360c1);
  194. usleep_range(100, 200);
  195. bcma_write32(pmu_core, BCMA_CC_PMU_PLLCTL_ADDR, 0x7);
  196. bcma_write32(pmu_core, BCMA_CC_PMU_PLLCTL_DATA, 0x0);
  197. usleep_range(100, 200);
  198. bcma_set32(pmu_core, BCMA_CC_PMU_CTL, BCMA_CC_PMU_CTL_PLL_UPD);
  199. usleep_range(100, 200);
  200. bcma_write32(core, 0x510, 0x7f8d007);
  201. udelay(1000);
  202. /* Take controller out of reset */
  203. bcma_write32(core, 0x200, 0x4ff);
  204. usleep_range(25, 50);
  205. bcma_write32(core, 0x200, 0x6ff);
  206. usleep_range(25, 50);
  207. bcma_write32(core, 0x200, 0x7ff);
  208. usleep_range(25, 50);
  209. of_platform_default_populate(dev->of_node, NULL, dev);
  210. return 0;
  211. }
  212. static void bcma_hcd_usb20_ns_init_hc(struct bcma_device *dev)
  213. {
  214. u32 val;
  215. /* Set packet buffer OUT threshold */
  216. val = bcma_read32(dev, 0x94);
  217. val &= 0xffff;
  218. val |= 0x80 << 16;
  219. bcma_write32(dev, 0x94, val);
  220. /* Enable break memory transfer */
  221. val = bcma_read32(dev, 0x9c);
  222. val |= 1;
  223. bcma_write32(dev, 0x9c, val);
  224. /*
  225. * Broadcom initializes PHY and then waits to ensure HC is ready to be
  226. * configured. In our case the order is reversed. We just initialized
  227. * controller and we let HCD initialize PHY, so let's wait (sleep) now.
  228. */
  229. usleep_range(1000, 2000);
  230. }
  231. /**
  232. * bcma_hcd_usb20_ns_init - Initialize Northstar USB 2.0 controller
  233. */
  234. static int bcma_hcd_usb20_ns_init(struct bcma_hcd_device *bcma_hcd)
  235. {
  236. struct bcma_device *core = bcma_hcd->core;
  237. struct bcma_chipinfo *ci = &core->bus->chipinfo;
  238. struct device *dev = &core->dev;
  239. bcma_core_enable(core, 0);
  240. if (ci->id == BCMA_CHIP_ID_BCM4707 ||
  241. ci->id == BCMA_CHIP_ID_BCM53018)
  242. bcma_hcd_usb20_ns_init_hc(core);
  243. of_platform_default_populate(dev->of_node, NULL, dev);
  244. return 0;
  245. }
  246. static void bcma_hci_platform_power_gpio(struct bcma_device *dev, bool val)
  247. {
  248. struct bcma_hcd_device *usb_dev = bcma_get_drvdata(dev);
  249. if (IS_ERR_OR_NULL(usb_dev->gpio_desc))
  250. return;
  251. gpiod_set_value(usb_dev->gpio_desc, val);
  252. }
  253. static const struct usb_ehci_pdata ehci_pdata = {
  254. };
  255. static const struct usb_ohci_pdata ohci_pdata = {
  256. };
  257. static struct platform_device *bcma_hcd_create_pdev(struct bcma_device *dev,
  258. const char *name, u32 addr,
  259. const void *data,
  260. size_t size)
  261. {
  262. struct platform_device *hci_dev;
  263. struct resource hci_res[2];
  264. int ret;
  265. memset(hci_res, 0, sizeof(hci_res));
  266. hci_res[0].start = addr;
  267. hci_res[0].end = hci_res[0].start + 0x1000 - 1;
  268. hci_res[0].flags = IORESOURCE_MEM;
  269. hci_res[1].start = dev->irq;
  270. hci_res[1].flags = IORESOURCE_IRQ;
  271. hci_dev = platform_device_alloc(name, 0);
  272. if (!hci_dev)
  273. return ERR_PTR(-ENOMEM);
  274. hci_dev->dev.parent = &dev->dev;
  275. hci_dev->dev.dma_mask = &hci_dev->dev.coherent_dma_mask;
  276. ret = platform_device_add_resources(hci_dev, hci_res,
  277. ARRAY_SIZE(hci_res));
  278. if (ret)
  279. goto err_alloc;
  280. if (data)
  281. ret = platform_device_add_data(hci_dev, data, size);
  282. if (ret)
  283. goto err_alloc;
  284. ret = platform_device_add(hci_dev);
  285. if (ret)
  286. goto err_alloc;
  287. return hci_dev;
  288. err_alloc:
  289. platform_device_put(hci_dev);
  290. return ERR_PTR(ret);
  291. }
  292. static int bcma_hcd_usb20_init(struct bcma_hcd_device *usb_dev)
  293. {
  294. struct bcma_device *dev = usb_dev->core;
  295. struct bcma_chipinfo *chipinfo = &dev->bus->chipinfo;
  296. u32 ohci_addr;
  297. int err;
  298. if (dma_set_mask_and_coherent(dev->dma_dev, DMA_BIT_MASK(32)))
  299. return -EOPNOTSUPP;
  300. bcma_hcd_init_chip_mips(dev);
  301. /* In AI chips EHCI is addrspace 0, OHCI is 1 */
  302. ohci_addr = dev->addr_s[0];
  303. if ((chipinfo->id == BCMA_CHIP_ID_BCM5357 ||
  304. chipinfo->id == BCMA_CHIP_ID_BCM4749)
  305. && chipinfo->rev == 0)
  306. ohci_addr = 0x18009000;
  307. usb_dev->ohci_dev = bcma_hcd_create_pdev(dev, "ohci-platform",
  308. ohci_addr, &ohci_pdata,
  309. sizeof(ohci_pdata));
  310. if (IS_ERR(usb_dev->ohci_dev))
  311. return PTR_ERR(usb_dev->ohci_dev);
  312. usb_dev->ehci_dev = bcma_hcd_create_pdev(dev, "ehci-platform",
  313. dev->addr, &ehci_pdata,
  314. sizeof(ehci_pdata));
  315. if (IS_ERR(usb_dev->ehci_dev)) {
  316. err = PTR_ERR(usb_dev->ehci_dev);
  317. goto err_unregister_ohci_dev;
  318. }
  319. return 0;
  320. err_unregister_ohci_dev:
  321. platform_device_unregister(usb_dev->ohci_dev);
  322. return err;
  323. }
  324. static int bcma_hcd_usb30_init(struct bcma_hcd_device *bcma_hcd)
  325. {
  326. struct bcma_device *core = bcma_hcd->core;
  327. struct device *dev = &core->dev;
  328. bcma_core_enable(core, 0);
  329. of_platform_default_populate(dev->of_node, NULL, dev);
  330. return 0;
  331. }
  332. static int bcma_hcd_probe(struct bcma_device *core)
  333. {
  334. int err;
  335. struct bcma_hcd_device *usb_dev;
  336. /* TODO: Probably need checks here; is the core connected? */
  337. usb_dev = devm_kzalloc(&core->dev, sizeof(struct bcma_hcd_device),
  338. GFP_KERNEL);
  339. if (!usb_dev)
  340. return -ENOMEM;
  341. usb_dev->core = core;
  342. if (core->dev.of_node)
  343. usb_dev->gpio_desc = devm_gpiod_get(&core->dev, "vcc",
  344. GPIOD_OUT_HIGH);
  345. switch (core->id.id) {
  346. case BCMA_CORE_USB20_HOST:
  347. if (IS_ENABLED(CONFIG_ARM))
  348. err = bcma_hcd_usb20_old_arm_init(usb_dev);
  349. else if (IS_ENABLED(CONFIG_MIPS))
  350. err = bcma_hcd_usb20_init(usb_dev);
  351. else
  352. err = -ENOTSUPP;
  353. break;
  354. case BCMA_CORE_NS_USB20:
  355. err = bcma_hcd_usb20_ns_init(usb_dev);
  356. break;
  357. case BCMA_CORE_NS_USB30:
  358. err = bcma_hcd_usb30_init(usb_dev);
  359. break;
  360. default:
  361. return -ENODEV;
  362. }
  363. if (err)
  364. return err;
  365. bcma_set_drvdata(core, usb_dev);
  366. return 0;
  367. }
  368. static void bcma_hcd_remove(struct bcma_device *dev)
  369. {
  370. struct bcma_hcd_device *usb_dev = bcma_get_drvdata(dev);
  371. struct platform_device *ohci_dev = usb_dev->ohci_dev;
  372. struct platform_device *ehci_dev = usb_dev->ehci_dev;
  373. if (ohci_dev)
  374. platform_device_unregister(ohci_dev);
  375. if (ehci_dev)
  376. platform_device_unregister(ehci_dev);
  377. bcma_core_disable(dev, 0);
  378. }
  379. static void bcma_hcd_shutdown(struct bcma_device *dev)
  380. {
  381. bcma_hci_platform_power_gpio(dev, false);
  382. bcma_core_disable(dev, 0);
  383. }
  384. #ifdef CONFIG_PM
  385. static int bcma_hcd_suspend(struct bcma_device *dev)
  386. {
  387. bcma_hci_platform_power_gpio(dev, false);
  388. bcma_core_disable(dev, 0);
  389. return 0;
  390. }
  391. static int bcma_hcd_resume(struct bcma_device *dev)
  392. {
  393. bcma_hci_platform_power_gpio(dev, true);
  394. bcma_core_enable(dev, 0);
  395. return 0;
  396. }
  397. #else /* !CONFIG_PM */
  398. #define bcma_hcd_suspend NULL
  399. #define bcma_hcd_resume NULL
  400. #endif /* CONFIG_PM */
  401. static const struct bcma_device_id bcma_hcd_table[] = {
  402. BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_USB20_HOST, BCMA_ANY_REV, BCMA_ANY_CLASS),
  403. BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_NS_USB20, BCMA_ANY_REV, BCMA_ANY_CLASS),
  404. BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_NS_USB30, BCMA_ANY_REV, BCMA_ANY_CLASS),
  405. {},
  406. };
  407. MODULE_DEVICE_TABLE(bcma, bcma_hcd_table);
  408. static struct bcma_driver bcma_hcd_driver = {
  409. .name = KBUILD_MODNAME,
  410. .id_table = bcma_hcd_table,
  411. .probe = bcma_hcd_probe,
  412. .remove = bcma_hcd_remove,
  413. .shutdown = bcma_hcd_shutdown,
  414. .suspend = bcma_hcd_suspend,
  415. .resume = bcma_hcd_resume,
  416. };
  417. static int __init bcma_hcd_init(void)
  418. {
  419. return bcma_driver_register(&bcma_hcd_driver);
  420. }
  421. module_init(bcma_hcd_init);
  422. static void __exit bcma_hcd_exit(void)
  423. {
  424. bcma_driver_unregister(&bcma_hcd_driver);
  425. }
  426. module_exit(bcma_hcd_exit);