ssb-hcd.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Sonics Silicon Backplane
  4. * Broadcom USB-core driver (SSB bus glue)
  5. *
  6. * Copyright 2011-2012 Hauke Mehrtens <hauke@hauke-m.de>
  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. #include <linux/ssb/ssb.h>
  21. #include <linux/delay.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/usb/ehci_pdriver.h>
  26. #include <linux/usb/ohci_pdriver.h>
  27. MODULE_AUTHOR("Hauke Mehrtens");
  28. MODULE_DESCRIPTION("Common USB driver for SSB Bus");
  29. MODULE_LICENSE("GPL");
  30. #define SSB_HCD_TMSLOW_HOSTMODE (1 << 29)
  31. struct ssb_hcd_device {
  32. struct platform_device *ehci_dev;
  33. struct platform_device *ohci_dev;
  34. u32 enable_flags;
  35. };
  36. static void ssb_hcd_5354wa(struct ssb_device *dev)
  37. {
  38. #ifdef CONFIG_SSB_DRIVER_MIPS
  39. /* Work around for 5354 failures */
  40. if (dev->id.revision == 2 && dev->bus->chip_id == 0x5354) {
  41. /* Change syn01 reg */
  42. ssb_write32(dev, 0x894, 0x00fe00fe);
  43. /* Change syn03 reg */
  44. ssb_write32(dev, 0x89c, ssb_read32(dev, 0x89c) | 0x1);
  45. }
  46. #endif
  47. }
  48. static void ssb_hcd_usb20wa(struct ssb_device *dev)
  49. {
  50. if (dev->id.coreid == SSB_DEV_USB20_HOST) {
  51. /*
  52. * USB 2.0 special considerations:
  53. *
  54. * In addition to the standard SSB reset sequence, the Host
  55. * Control Register must be programmed to bring the USB core
  56. * and various phy components out of reset.
  57. */
  58. ssb_write32(dev, 0x200, 0x7ff);
  59. /* Change Flush control reg */
  60. ssb_write32(dev, 0x400, ssb_read32(dev, 0x400) & ~8);
  61. ssb_read32(dev, 0x400);
  62. /* Change Shim control reg */
  63. ssb_write32(dev, 0x304, ssb_read32(dev, 0x304) & ~0x100);
  64. ssb_read32(dev, 0x304);
  65. udelay(1);
  66. ssb_hcd_5354wa(dev);
  67. }
  68. }
  69. /* based on arch/mips/brcm-boards/bcm947xx/pcibios.c */
  70. static u32 ssb_hcd_init_chip(struct ssb_device *dev)
  71. {
  72. u32 flags = 0;
  73. if (dev->id.coreid == SSB_DEV_USB11_HOSTDEV)
  74. /* Put the device into host-mode. */
  75. flags |= SSB_HCD_TMSLOW_HOSTMODE;
  76. ssb_device_enable(dev, flags);
  77. ssb_hcd_usb20wa(dev);
  78. return flags;
  79. }
  80. static const struct usb_ehci_pdata ehci_pdata = {
  81. };
  82. static const struct usb_ohci_pdata ohci_pdata = {
  83. };
  84. static struct platform_device *ssb_hcd_create_pdev(struct ssb_device *dev, bool ohci, u32 addr, u32 len)
  85. {
  86. struct platform_device *hci_dev;
  87. struct resource hci_res[2];
  88. int ret;
  89. memset(hci_res, 0, sizeof(hci_res));
  90. hci_res[0].start = addr;
  91. hci_res[0].end = hci_res[0].start + len - 1;
  92. hci_res[0].flags = IORESOURCE_MEM;
  93. hci_res[1].start = dev->irq;
  94. hci_res[1].flags = IORESOURCE_IRQ;
  95. hci_dev = platform_device_alloc(ohci ? "ohci-platform" :
  96. "ehci-platform" , 0);
  97. if (!hci_dev)
  98. return ERR_PTR(-ENOMEM);
  99. hci_dev->dev.parent = dev->dev;
  100. hci_dev->dev.dma_mask = &hci_dev->dev.coherent_dma_mask;
  101. ret = platform_device_add_resources(hci_dev, hci_res,
  102. ARRAY_SIZE(hci_res));
  103. if (ret)
  104. goto err_alloc;
  105. if (ohci)
  106. ret = platform_device_add_data(hci_dev, &ohci_pdata,
  107. sizeof(ohci_pdata));
  108. else
  109. ret = platform_device_add_data(hci_dev, &ehci_pdata,
  110. sizeof(ehci_pdata));
  111. if (ret)
  112. goto err_alloc;
  113. ret = platform_device_add(hci_dev);
  114. if (ret)
  115. goto err_alloc;
  116. return hci_dev;
  117. err_alloc:
  118. platform_device_put(hci_dev);
  119. return ERR_PTR(ret);
  120. }
  121. static int ssb_hcd_probe(struct ssb_device *dev,
  122. const struct ssb_device_id *id)
  123. {
  124. int err, tmp;
  125. int start, len;
  126. u16 chipid_top;
  127. u16 coreid = dev->id.coreid;
  128. struct ssb_hcd_device *usb_dev;
  129. /* USBcores are only connected on embedded devices. */
  130. chipid_top = (dev->bus->chip_id & 0xFF00);
  131. if (chipid_top != 0x4700 && chipid_top != 0x5300)
  132. return -ENODEV;
  133. /* TODO: Probably need checks here; is the core connected? */
  134. if (dma_set_mask_and_coherent(dev->dma_dev, DMA_BIT_MASK(32)))
  135. return -EOPNOTSUPP;
  136. usb_dev = devm_kzalloc(dev->dev, sizeof(struct ssb_hcd_device),
  137. GFP_KERNEL);
  138. if (!usb_dev)
  139. return -ENOMEM;
  140. /* We currently always attach SSB_DEV_USB11_HOSTDEV
  141. * as HOST OHCI. If we want to attach it as Client device,
  142. * we must branch here and call into the (yet to
  143. * be written) Client mode driver. Same for remove(). */
  144. usb_dev->enable_flags = ssb_hcd_init_chip(dev);
  145. tmp = ssb_read32(dev, SSB_ADMATCH0);
  146. start = ssb_admatch_base(tmp);
  147. len = (coreid == SSB_DEV_USB20_HOST) ? 0x800 : ssb_admatch_size(tmp);
  148. usb_dev->ohci_dev = ssb_hcd_create_pdev(dev, true, start, len);
  149. if (IS_ERR(usb_dev->ohci_dev))
  150. return PTR_ERR(usb_dev->ohci_dev);
  151. if (coreid == SSB_DEV_USB20_HOST) {
  152. start = ssb_admatch_base(tmp) + 0x800; /* ehci core offset */
  153. usb_dev->ehci_dev = ssb_hcd_create_pdev(dev, false, start, len);
  154. if (IS_ERR(usb_dev->ehci_dev)) {
  155. err = PTR_ERR(usb_dev->ehci_dev);
  156. goto err_unregister_ohci_dev;
  157. }
  158. }
  159. ssb_set_drvdata(dev, usb_dev);
  160. return 0;
  161. err_unregister_ohci_dev:
  162. platform_device_unregister(usb_dev->ohci_dev);
  163. return err;
  164. }
  165. static void ssb_hcd_remove(struct ssb_device *dev)
  166. {
  167. struct ssb_hcd_device *usb_dev = ssb_get_drvdata(dev);
  168. struct platform_device *ohci_dev = usb_dev->ohci_dev;
  169. struct platform_device *ehci_dev = usb_dev->ehci_dev;
  170. if (ohci_dev)
  171. platform_device_unregister(ohci_dev);
  172. if (ehci_dev)
  173. platform_device_unregister(ehci_dev);
  174. ssb_device_disable(dev, 0);
  175. }
  176. static void ssb_hcd_shutdown(struct ssb_device *dev)
  177. {
  178. ssb_device_disable(dev, 0);
  179. }
  180. #ifdef CONFIG_PM
  181. static int ssb_hcd_suspend(struct ssb_device *dev, pm_message_t state)
  182. {
  183. ssb_device_disable(dev, 0);
  184. return 0;
  185. }
  186. static int ssb_hcd_resume(struct ssb_device *dev)
  187. {
  188. struct ssb_hcd_device *usb_dev = ssb_get_drvdata(dev);
  189. ssb_device_enable(dev, usb_dev->enable_flags);
  190. return 0;
  191. }
  192. #else /* !CONFIG_PM */
  193. #define ssb_hcd_suspend NULL
  194. #define ssb_hcd_resume NULL
  195. #endif /* CONFIG_PM */
  196. static const struct ssb_device_id ssb_hcd_table[] = {
  197. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_USB11_HOSTDEV, SSB_ANY_REV),
  198. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_USB11_HOST, SSB_ANY_REV),
  199. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_USB20_HOST, SSB_ANY_REV),
  200. {},
  201. };
  202. MODULE_DEVICE_TABLE(ssb, ssb_hcd_table);
  203. static struct ssb_driver ssb_hcd_driver = {
  204. .name = KBUILD_MODNAME,
  205. .id_table = ssb_hcd_table,
  206. .probe = ssb_hcd_probe,
  207. .remove = ssb_hcd_remove,
  208. .shutdown = ssb_hcd_shutdown,
  209. .suspend = ssb_hcd_suspend,
  210. .resume = ssb_hcd_resume,
  211. };
  212. static int __init ssb_hcd_init(void)
  213. {
  214. return ssb_driver_register(&ssb_hcd_driver);
  215. }
  216. module_init(ssb_hcd_init);
  217. static void __exit ssb_hcd_exit(void)
  218. {
  219. ssb_driver_unregister(&ssb_hcd_driver);
  220. }
  221. module_exit(ssb_hcd_exit);