ssb-hcd.c 6.6 KB

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