ohci-nxp.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * driver for NXP USB Host devices
  4. *
  5. * Currently supported OHCI host devices:
  6. * - NXP LPC32xx
  7. *
  8. * Authors: Dmitry Chigirev <source@mvista.com>
  9. * Vitaly Wool <vitalywool@gmail.com>
  10. *
  11. * register initialization is based on code examples provided by Philips
  12. * Copyright (c) 2005 Koninklijke Philips Electronics N.V.
  13. *
  14. * NOTE: This driver does not have suspend/resume functionality
  15. * This driver is intended for engineering development purposes only
  16. *
  17. * 2005-2006 (c) MontaVista Software, Inc.
  18. */
  19. #include <linux/clk.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/io.h>
  22. #include <linux/i2c.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/usb/isp1301.h>
  27. #include <linux/usb.h>
  28. #include <linux/usb/hcd.h>
  29. #include "ohci.h"
  30. #define USB_CONFIG_BASE 0x31020000
  31. /* USB_OTG_STAT_CONTROL bit defines */
  32. #define TRANSPARENT_I2C_EN (1 << 7)
  33. #define HOST_EN (1 << 0)
  34. /* On LPC32xx, those are undefined */
  35. #ifndef start_int_set_falling_edge
  36. #define start_int_set_falling_edge(irq)
  37. #define start_int_set_rising_edge(irq)
  38. #define start_int_ack(irq)
  39. #define start_int_mask(irq)
  40. #define start_int_umask(irq)
  41. #endif
  42. #define DRIVER_DESC "OHCI NXP driver"
  43. static const char hcd_name[] = "ohci-nxp";
  44. static struct hc_driver __read_mostly ohci_nxp_hc_driver;
  45. static struct i2c_client *isp1301_i2c_client;
  46. static struct clk *usb_host_clk;
  47. static void isp1301_configure_lpc32xx(void)
  48. {
  49. /* LPC32XX only supports DAT_SE0 USB mode */
  50. /* This sequence is important */
  51. /* Disable transparent UART mode first */
  52. i2c_smbus_write_byte_data(isp1301_i2c_client,
  53. (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
  54. MC1_UART_EN);
  55. i2c_smbus_write_byte_data(isp1301_i2c_client,
  56. (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
  57. ~MC1_SPEED_REG);
  58. i2c_smbus_write_byte_data(isp1301_i2c_client,
  59. ISP1301_I2C_MODE_CONTROL_1, MC1_SPEED_REG);
  60. i2c_smbus_write_byte_data(isp1301_i2c_client,
  61. (ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR),
  62. ~0);
  63. i2c_smbus_write_byte_data(isp1301_i2c_client,
  64. ISP1301_I2C_MODE_CONTROL_2,
  65. (MC2_BI_DI | MC2_PSW_EN | MC2_SPD_SUSP_CTRL));
  66. i2c_smbus_write_byte_data(isp1301_i2c_client,
  67. (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), ~0);
  68. i2c_smbus_write_byte_data(isp1301_i2c_client,
  69. ISP1301_I2C_MODE_CONTROL_1, MC1_DAT_SE0);
  70. i2c_smbus_write_byte_data(isp1301_i2c_client,
  71. ISP1301_I2C_OTG_CONTROL_1,
  72. (OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
  73. i2c_smbus_write_byte_data(isp1301_i2c_client,
  74. (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
  75. (OTG1_DM_PULLUP | OTG1_DP_PULLUP));
  76. i2c_smbus_write_byte_data(isp1301_i2c_client,
  77. ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
  78. i2c_smbus_write_byte_data(isp1301_i2c_client,
  79. ISP1301_I2C_INTERRUPT_FALLING | ISP1301_I2C_REG_CLEAR_ADDR,
  80. ~0);
  81. i2c_smbus_write_byte_data(isp1301_i2c_client,
  82. ISP1301_I2C_INTERRUPT_RISING | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
  83. printk(KERN_INFO "ISP1301 Vendor ID : 0x%04x\n",
  84. i2c_smbus_read_word_data(isp1301_i2c_client, 0x00));
  85. printk(KERN_INFO "ISP1301 Product ID : 0x%04x\n",
  86. i2c_smbus_read_word_data(isp1301_i2c_client, 0x02));
  87. printk(KERN_INFO "ISP1301 Version ID : 0x%04x\n",
  88. i2c_smbus_read_word_data(isp1301_i2c_client, 0x14));
  89. }
  90. static void isp1301_configure(void)
  91. {
  92. isp1301_configure_lpc32xx();
  93. }
  94. static inline void isp1301_vbus_on(void)
  95. {
  96. i2c_smbus_write_byte_data(isp1301_i2c_client, ISP1301_I2C_OTG_CONTROL_1,
  97. OTG1_VBUS_DRV);
  98. }
  99. static inline void isp1301_vbus_off(void)
  100. {
  101. i2c_smbus_write_byte_data(isp1301_i2c_client,
  102. ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
  103. OTG1_VBUS_DRV);
  104. }
  105. static void ohci_nxp_start_hc(void)
  106. {
  107. void __iomem *usb_otg_stat_control = ioremap(USB_CONFIG_BASE + 0x110, 4);
  108. unsigned long tmp;
  109. if (WARN_ON(!usb_otg_stat_control))
  110. return;
  111. tmp = __raw_readl(usb_otg_stat_control) | HOST_EN;
  112. __raw_writel(tmp, usb_otg_stat_control);
  113. isp1301_vbus_on();
  114. iounmap(usb_otg_stat_control);
  115. }
  116. static void ohci_nxp_stop_hc(void)
  117. {
  118. void __iomem *usb_otg_stat_control = ioremap(USB_CONFIG_BASE + 0x110, 4);
  119. unsigned long tmp;
  120. if (WARN_ON(!usb_otg_stat_control))
  121. return;
  122. isp1301_vbus_off();
  123. tmp = __raw_readl(usb_otg_stat_control) & ~HOST_EN;
  124. __raw_writel(tmp, usb_otg_stat_control);
  125. iounmap(usb_otg_stat_control);
  126. }
  127. static int ohci_hcd_nxp_probe(struct platform_device *pdev)
  128. {
  129. struct usb_hcd *hcd = 0;
  130. const struct hc_driver *driver = &ohci_nxp_hc_driver;
  131. struct resource *res;
  132. int ret = 0, irq;
  133. struct device_node *isp1301_node;
  134. if (pdev->dev.of_node) {
  135. isp1301_node = of_parse_phandle(pdev->dev.of_node,
  136. "transceiver", 0);
  137. } else {
  138. isp1301_node = NULL;
  139. }
  140. isp1301_i2c_client = isp1301_get_client(isp1301_node);
  141. if (!isp1301_i2c_client)
  142. return -EPROBE_DEFER;
  143. ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  144. if (ret)
  145. goto fail_disable;
  146. dev_dbg(&pdev->dev, "%s: " DRIVER_DESC " (nxp)\n", hcd_name);
  147. if (usb_disabled()) {
  148. dev_err(&pdev->dev, "USB is disabled\n");
  149. ret = -ENODEV;
  150. goto fail_disable;
  151. }
  152. /* Enable USB host clock */
  153. usb_host_clk = devm_clk_get(&pdev->dev, NULL);
  154. if (IS_ERR(usb_host_clk)) {
  155. dev_err(&pdev->dev, "failed to acquire USB OHCI clock\n");
  156. ret = PTR_ERR(usb_host_clk);
  157. goto fail_disable;
  158. }
  159. ret = clk_prepare_enable(usb_host_clk);
  160. if (ret < 0) {
  161. dev_err(&pdev->dev, "failed to start USB OHCI clock\n");
  162. goto fail_disable;
  163. }
  164. isp1301_configure();
  165. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  166. if (!hcd) {
  167. dev_err(&pdev->dev, "Failed to allocate HC buffer\n");
  168. ret = -ENOMEM;
  169. goto fail_hcd;
  170. }
  171. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  172. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  173. if (IS_ERR(hcd->regs)) {
  174. ret = PTR_ERR(hcd->regs);
  175. goto fail_resource;
  176. }
  177. hcd->rsrc_start = res->start;
  178. hcd->rsrc_len = resource_size(res);
  179. irq = platform_get_irq(pdev, 0);
  180. if (irq < 0) {
  181. ret = -ENXIO;
  182. goto fail_resource;
  183. }
  184. ohci_nxp_start_hc();
  185. platform_set_drvdata(pdev, hcd);
  186. dev_info(&pdev->dev, "at 0x%p, irq %d\n", hcd->regs, hcd->irq);
  187. ret = usb_add_hcd(hcd, irq, 0);
  188. if (ret == 0) {
  189. device_wakeup_enable(hcd->self.controller);
  190. return ret;
  191. }
  192. ohci_nxp_stop_hc();
  193. fail_resource:
  194. usb_put_hcd(hcd);
  195. fail_hcd:
  196. clk_disable_unprepare(usb_host_clk);
  197. fail_disable:
  198. isp1301_i2c_client = NULL;
  199. return ret;
  200. }
  201. static int ohci_hcd_nxp_remove(struct platform_device *pdev)
  202. {
  203. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  204. usb_remove_hcd(hcd);
  205. ohci_nxp_stop_hc();
  206. usb_put_hcd(hcd);
  207. clk_disable_unprepare(usb_host_clk);
  208. isp1301_i2c_client = NULL;
  209. return 0;
  210. }
  211. /* work with hotplug and coldplug */
  212. MODULE_ALIAS("platform:usb-ohci");
  213. #ifdef CONFIG_OF
  214. static const struct of_device_id ohci_hcd_nxp_match[] = {
  215. { .compatible = "nxp,ohci-nxp" },
  216. {},
  217. };
  218. MODULE_DEVICE_TABLE(of, ohci_hcd_nxp_match);
  219. #endif
  220. static struct platform_driver ohci_hcd_nxp_driver = {
  221. .driver = {
  222. .name = "usb-ohci",
  223. .of_match_table = of_match_ptr(ohci_hcd_nxp_match),
  224. },
  225. .probe = ohci_hcd_nxp_probe,
  226. .remove = ohci_hcd_nxp_remove,
  227. };
  228. static int __init ohci_nxp_init(void)
  229. {
  230. if (usb_disabled())
  231. return -ENODEV;
  232. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  233. ohci_init_driver(&ohci_nxp_hc_driver, NULL);
  234. return platform_driver_register(&ohci_hcd_nxp_driver);
  235. }
  236. module_init(ohci_nxp_init);
  237. static void __exit ohci_nxp_cleanup(void)
  238. {
  239. platform_driver_unregister(&ohci_hcd_nxp_driver);
  240. }
  241. module_exit(ohci_nxp_cleanup);
  242. MODULE_DESCRIPTION(DRIVER_DESC);
  243. MODULE_LICENSE("GPL v2");