bcma-hcd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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/usb/ehci_pdriver.h>
  31. #include <linux/usb/ohci_pdriver.h>
  32. MODULE_AUTHOR("Hauke Mehrtens");
  33. MODULE_DESCRIPTION("Common USB driver for BCMA Bus");
  34. MODULE_LICENSE("GPL");
  35. struct bcma_hcd_device {
  36. struct bcma_device *core;
  37. struct platform_device *ehci_dev;
  38. struct platform_device *ohci_dev;
  39. struct gpio_desc *gpio_desc;
  40. };
  41. /* Wait for bitmask in a register to get set or cleared.
  42. * timeout is in units of ten-microseconds.
  43. */
  44. static int bcma_wait_bits(struct bcma_device *dev, u16 reg, u32 bitmask,
  45. int timeout)
  46. {
  47. int i;
  48. u32 val;
  49. for (i = 0; i < timeout; i++) {
  50. val = bcma_read32(dev, reg);
  51. if ((val & bitmask) == bitmask)
  52. return 0;
  53. udelay(10);
  54. }
  55. return -ETIMEDOUT;
  56. }
  57. static void bcma_hcd_4716wa(struct bcma_device *dev)
  58. {
  59. #ifdef CONFIG_BCMA_DRIVER_MIPS
  60. /* Work around for 4716 failures. */
  61. if (dev->bus->chipinfo.id == 0x4716) {
  62. u32 tmp;
  63. tmp = bcma_cpu_clock(&dev->bus->drv_mips);
  64. if (tmp >= 480000000)
  65. tmp = 0x1846b; /* set CDR to 0x11(fast) */
  66. else if (tmp == 453000000)
  67. tmp = 0x1046b; /* set CDR to 0x10(slow) */
  68. else
  69. tmp = 0;
  70. /* Change Shim mdio control reg to fix host not acking at
  71. * high frequencies
  72. */
  73. if (tmp) {
  74. bcma_write32(dev, 0x524, 0x1); /* write sel to enable */
  75. udelay(500);
  76. bcma_write32(dev, 0x524, tmp);
  77. udelay(500);
  78. bcma_write32(dev, 0x524, 0x4ab);
  79. udelay(500);
  80. bcma_read32(dev, 0x528);
  81. bcma_write32(dev, 0x528, 0x80000000);
  82. }
  83. }
  84. #endif /* CONFIG_BCMA_DRIVER_MIPS */
  85. }
  86. /* based on arch/mips/brcm-boards/bcm947xx/pcibios.c */
  87. static void bcma_hcd_init_chip_mips(struct bcma_device *dev)
  88. {
  89. u32 tmp;
  90. /*
  91. * USB 2.0 special considerations:
  92. *
  93. * 1. Since the core supports both OHCI and EHCI functions, it must
  94. * only be reset once.
  95. *
  96. * 2. In addition to the standard SI reset sequence, the Host Control
  97. * Register must be programmed to bring the USB core and various
  98. * phy components out of reset.
  99. */
  100. if (!bcma_core_is_enabled(dev)) {
  101. bcma_core_enable(dev, 0);
  102. mdelay(10);
  103. if (dev->id.rev >= 5) {
  104. /* Enable Misc PLL */
  105. tmp = bcma_read32(dev, 0x1e0);
  106. tmp |= 0x100;
  107. bcma_write32(dev, 0x1e0, tmp);
  108. if (bcma_wait_bits(dev, 0x1e0, 1 << 24, 100))
  109. printk(KERN_EMERG "Failed to enable misc PPL!\n");
  110. /* Take out of resets */
  111. bcma_write32(dev, 0x200, 0x4ff);
  112. udelay(25);
  113. bcma_write32(dev, 0x200, 0x6ff);
  114. udelay(25);
  115. /* Make sure digital and AFE are locked in USB PHY */
  116. bcma_write32(dev, 0x524, 0x6b);
  117. udelay(50);
  118. tmp = bcma_read32(dev, 0x524);
  119. udelay(50);
  120. bcma_write32(dev, 0x524, 0xab);
  121. udelay(50);
  122. tmp = bcma_read32(dev, 0x524);
  123. udelay(50);
  124. bcma_write32(dev, 0x524, 0x2b);
  125. udelay(50);
  126. tmp = bcma_read32(dev, 0x524);
  127. udelay(50);
  128. bcma_write32(dev, 0x524, 0x10ab);
  129. udelay(50);
  130. tmp = bcma_read32(dev, 0x524);
  131. if (bcma_wait_bits(dev, 0x528, 0xc000, 10000)) {
  132. tmp = bcma_read32(dev, 0x528);
  133. printk(KERN_EMERG
  134. "USB20H mdio_rddata 0x%08x\n", tmp);
  135. }
  136. bcma_write32(dev, 0x528, 0x80000000);
  137. tmp = bcma_read32(dev, 0x314);
  138. udelay(265);
  139. bcma_write32(dev, 0x200, 0x7ff);
  140. udelay(10);
  141. /* Take USB and HSIC out of non-driving modes */
  142. bcma_write32(dev, 0x510, 0);
  143. } else {
  144. bcma_write32(dev, 0x200, 0x7ff);
  145. udelay(1);
  146. }
  147. bcma_hcd_4716wa(dev);
  148. }
  149. }
  150. static void bcma_hcd_init_chip_arm_phy(struct bcma_device *dev)
  151. {
  152. struct bcma_device *arm_core;
  153. void __iomem *dmu;
  154. arm_core = bcma_find_core(dev->bus, BCMA_CORE_ARMCA9);
  155. if (!arm_core) {
  156. dev_err(&dev->dev, "can not find ARM Cortex A9 ihost core\n");
  157. return;
  158. }
  159. dmu = ioremap_nocache(arm_core->addr_s[0], 0x1000);
  160. if (!dmu) {
  161. dev_err(&dev->dev, "can not map ARM Cortex A9 ihost core\n");
  162. return;
  163. }
  164. /* Unlock DMU PLL settings */
  165. iowrite32(0x0000ea68, dmu + 0x180);
  166. /* Write USB 2.0 PLL control setting */
  167. iowrite32(0x00dd10c3, dmu + 0x164);
  168. /* Lock DMU PLL settings */
  169. iowrite32(0x00000000, dmu + 0x180);
  170. iounmap(dmu);
  171. }
  172. static void bcma_hcd_init_chip_arm_hc(struct bcma_device *dev)
  173. {
  174. u32 val;
  175. /*
  176. * Delay after PHY initialized to ensure HC is ready to be configured
  177. */
  178. usleep_range(1000, 2000);
  179. /* Set packet buffer OUT threshold */
  180. val = bcma_read32(dev, 0x94);
  181. val &= 0xffff;
  182. val |= 0x80 << 16;
  183. bcma_write32(dev, 0x94, val);
  184. /* Enable break memory transfer */
  185. val = bcma_read32(dev, 0x9c);
  186. val |= 1;
  187. bcma_write32(dev, 0x9c, val);
  188. }
  189. static void bcma_hcd_init_chip_arm(struct bcma_device *dev)
  190. {
  191. bcma_core_enable(dev, 0);
  192. if (dev->bus->chipinfo.id == BCMA_CHIP_ID_BCM4707 ||
  193. dev->bus->chipinfo.id == BCMA_CHIP_ID_BCM53018) {
  194. if (dev->bus->chipinfo.pkg == BCMA_PKG_ID_BCM4707 ||
  195. dev->bus->chipinfo.pkg == BCMA_PKG_ID_BCM4708)
  196. bcma_hcd_init_chip_arm_phy(dev);
  197. bcma_hcd_init_chip_arm_hc(dev);
  198. }
  199. }
  200. static void bcma_hci_platform_power_gpio(struct bcma_device *dev, bool val)
  201. {
  202. struct bcma_hcd_device *usb_dev = bcma_get_drvdata(dev);
  203. if (IS_ERR_OR_NULL(usb_dev->gpio_desc))
  204. return;
  205. gpiod_set_value(usb_dev->gpio_desc, val);
  206. }
  207. static const struct usb_ehci_pdata ehci_pdata = {
  208. };
  209. static const struct usb_ohci_pdata ohci_pdata = {
  210. };
  211. static struct platform_device *bcma_hcd_create_pdev(struct bcma_device *dev,
  212. const char *name, u32 addr,
  213. const void *data,
  214. size_t size)
  215. {
  216. struct platform_device *hci_dev;
  217. struct resource hci_res[2];
  218. int ret;
  219. memset(hci_res, 0, sizeof(hci_res));
  220. hci_res[0].start = addr;
  221. hci_res[0].end = hci_res[0].start + 0x1000 - 1;
  222. hci_res[0].flags = IORESOURCE_MEM;
  223. hci_res[1].start = dev->irq;
  224. hci_res[1].flags = IORESOURCE_IRQ;
  225. hci_dev = platform_device_alloc(name, 0);
  226. if (!hci_dev)
  227. return ERR_PTR(-ENOMEM);
  228. hci_dev->dev.parent = &dev->dev;
  229. hci_dev->dev.dma_mask = &hci_dev->dev.coherent_dma_mask;
  230. ret = platform_device_add_resources(hci_dev, hci_res,
  231. ARRAY_SIZE(hci_res));
  232. if (ret)
  233. goto err_alloc;
  234. if (data)
  235. ret = platform_device_add_data(hci_dev, data, size);
  236. if (ret)
  237. goto err_alloc;
  238. ret = platform_device_add(hci_dev);
  239. if (ret)
  240. goto err_alloc;
  241. return hci_dev;
  242. err_alloc:
  243. platform_device_put(hci_dev);
  244. return ERR_PTR(ret);
  245. }
  246. static int bcma_hcd_usb20_init(struct bcma_hcd_device *usb_dev)
  247. {
  248. struct bcma_device *dev = usb_dev->core;
  249. struct bcma_chipinfo *chipinfo = &dev->bus->chipinfo;
  250. u32 ohci_addr;
  251. int err;
  252. if (dma_set_mask_and_coherent(dev->dma_dev, DMA_BIT_MASK(32)))
  253. return -EOPNOTSUPP;
  254. switch (dev->id.id) {
  255. case BCMA_CORE_NS_USB20:
  256. bcma_hcd_init_chip_arm(dev);
  257. break;
  258. case BCMA_CORE_USB20_HOST:
  259. bcma_hcd_init_chip_mips(dev);
  260. break;
  261. default:
  262. return -ENODEV;
  263. }
  264. /* In AI chips EHCI is addrspace 0, OHCI is 1 */
  265. ohci_addr = dev->addr_s[0];
  266. if ((chipinfo->id == BCMA_CHIP_ID_BCM5357 ||
  267. chipinfo->id == BCMA_CHIP_ID_BCM4749)
  268. && chipinfo->rev == 0)
  269. ohci_addr = 0x18009000;
  270. usb_dev->ohci_dev = bcma_hcd_create_pdev(dev, "ohci-platform",
  271. ohci_addr, &ohci_pdata,
  272. sizeof(ohci_pdata));
  273. if (IS_ERR(usb_dev->ohci_dev))
  274. return PTR_ERR(usb_dev->ohci_dev);
  275. usb_dev->ehci_dev = bcma_hcd_create_pdev(dev, "ehci-platform",
  276. dev->addr, &ehci_pdata,
  277. sizeof(ehci_pdata));
  278. if (IS_ERR(usb_dev->ehci_dev)) {
  279. err = PTR_ERR(usb_dev->ehci_dev);
  280. goto err_unregister_ohci_dev;
  281. }
  282. return 0;
  283. err_unregister_ohci_dev:
  284. platform_device_unregister(usb_dev->ohci_dev);
  285. return err;
  286. }
  287. static int bcma_hcd_probe(struct bcma_device *core)
  288. {
  289. int err;
  290. struct bcma_hcd_device *usb_dev;
  291. /* TODO: Probably need checks here; is the core connected? */
  292. usb_dev = devm_kzalloc(&core->dev, sizeof(struct bcma_hcd_device),
  293. GFP_KERNEL);
  294. if (!usb_dev)
  295. return -ENOMEM;
  296. usb_dev->core = core;
  297. if (core->dev.of_node)
  298. usb_dev->gpio_desc = devm_get_gpiod_from_child(&core->dev, "vcc",
  299. &core->dev.of_node->fwnode);
  300. if (!IS_ERR_OR_NULL(usb_dev->gpio_desc))
  301. gpiod_direction_output(usb_dev->gpio_desc, 1);
  302. switch (core->id.id) {
  303. case BCMA_CORE_USB20_HOST:
  304. case BCMA_CORE_NS_USB20:
  305. err = bcma_hcd_usb20_init(usb_dev);
  306. if (err)
  307. return err;
  308. break;
  309. default:
  310. return -ENODEV;
  311. }
  312. bcma_set_drvdata(core, usb_dev);
  313. return 0;
  314. }
  315. static void bcma_hcd_remove(struct bcma_device *dev)
  316. {
  317. struct bcma_hcd_device *usb_dev = bcma_get_drvdata(dev);
  318. struct platform_device *ohci_dev = usb_dev->ohci_dev;
  319. struct platform_device *ehci_dev = usb_dev->ehci_dev;
  320. if (ohci_dev)
  321. platform_device_unregister(ohci_dev);
  322. if (ehci_dev)
  323. platform_device_unregister(ehci_dev);
  324. bcma_core_disable(dev, 0);
  325. }
  326. static void bcma_hcd_shutdown(struct bcma_device *dev)
  327. {
  328. bcma_hci_platform_power_gpio(dev, false);
  329. bcma_core_disable(dev, 0);
  330. }
  331. #ifdef CONFIG_PM
  332. static int bcma_hcd_suspend(struct bcma_device *dev)
  333. {
  334. bcma_hci_platform_power_gpio(dev, false);
  335. bcma_core_disable(dev, 0);
  336. return 0;
  337. }
  338. static int bcma_hcd_resume(struct bcma_device *dev)
  339. {
  340. bcma_hci_platform_power_gpio(dev, true);
  341. bcma_core_enable(dev, 0);
  342. return 0;
  343. }
  344. #else /* !CONFIG_PM */
  345. #define bcma_hcd_suspend NULL
  346. #define bcma_hcd_resume NULL
  347. #endif /* CONFIG_PM */
  348. static const struct bcma_device_id bcma_hcd_table[] = {
  349. BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_USB20_HOST, BCMA_ANY_REV, BCMA_ANY_CLASS),
  350. BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_NS_USB20, BCMA_ANY_REV, BCMA_ANY_CLASS),
  351. {},
  352. };
  353. MODULE_DEVICE_TABLE(bcma, bcma_hcd_table);
  354. static struct bcma_driver bcma_hcd_driver = {
  355. .name = KBUILD_MODNAME,
  356. .id_table = bcma_hcd_table,
  357. .probe = bcma_hcd_probe,
  358. .remove = bcma_hcd_remove,
  359. .shutdown = bcma_hcd_shutdown,
  360. .suspend = bcma_hcd_suspend,
  361. .resume = bcma_hcd_resume,
  362. };
  363. static int __init bcma_hcd_init(void)
  364. {
  365. return bcma_driver_register(&bcma_hcd_driver);
  366. }
  367. module_init(bcma_hcd_init);
  368. static void __exit bcma_hcd_exit(void)
  369. {
  370. bcma_driver_unregister(&bcma_hcd_driver);
  371. }
  372. module_exit(bcma_hcd_exit);