ohci-nxp.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 void isp1301_configure_lpc32xx(void)
  47. {
  48. /* LPC32XX only supports DAT_SE0 USB mode */
  49. /* This sequence is important */
  50. /* Disable transparent UART mode first */
  51. i2c_smbus_write_byte_data(isp1301_i2c_client,
  52. (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
  53. MC1_UART_EN);
  54. i2c_smbus_write_byte_data(isp1301_i2c_client,
  55. (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
  56. ~MC1_SPEED_REG);
  57. i2c_smbus_write_byte_data(isp1301_i2c_client,
  58. ISP1301_I2C_MODE_CONTROL_1, MC1_SPEED_REG);
  59. i2c_smbus_write_byte_data(isp1301_i2c_client,
  60. (ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR),
  61. ~0);
  62. i2c_smbus_write_byte_data(isp1301_i2c_client,
  63. ISP1301_I2C_MODE_CONTROL_2,
  64. (MC2_BI_DI | MC2_PSW_EN | MC2_SPD_SUSP_CTRL));
  65. i2c_smbus_write_byte_data(isp1301_i2c_client,
  66. (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), ~0);
  67. i2c_smbus_write_byte_data(isp1301_i2c_client,
  68. ISP1301_I2C_MODE_CONTROL_1, MC1_DAT_SE0);
  69. i2c_smbus_write_byte_data(isp1301_i2c_client,
  70. ISP1301_I2C_OTG_CONTROL_1,
  71. (OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
  72. i2c_smbus_write_byte_data(isp1301_i2c_client,
  73. (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
  74. (OTG1_DM_PULLUP | OTG1_DP_PULLUP));
  75. i2c_smbus_write_byte_data(isp1301_i2c_client,
  76. ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
  77. i2c_smbus_write_byte_data(isp1301_i2c_client,
  78. ISP1301_I2C_INTERRUPT_FALLING | ISP1301_I2C_REG_CLEAR_ADDR,
  79. ~0);
  80. i2c_smbus_write_byte_data(isp1301_i2c_client,
  81. ISP1301_I2C_INTERRUPT_RISING | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
  82. printk(KERN_INFO "ISP1301 Vendor ID : 0x%04x\n",
  83. i2c_smbus_read_word_data(isp1301_i2c_client, 0x00));
  84. printk(KERN_INFO "ISP1301 Product ID : 0x%04x\n",
  85. i2c_smbus_read_word_data(isp1301_i2c_client, 0x02));
  86. printk(KERN_INFO "ISP1301 Version ID : 0x%04x\n",
  87. i2c_smbus_read_word_data(isp1301_i2c_client, 0x14));
  88. }
  89. static void isp1301_configure(void)
  90. {
  91. isp1301_configure_lpc32xx();
  92. }
  93. static inline void isp1301_vbus_on(void)
  94. {
  95. i2c_smbus_write_byte_data(isp1301_i2c_client, ISP1301_I2C_OTG_CONTROL_1,
  96. OTG1_VBUS_DRV);
  97. }
  98. static inline void isp1301_vbus_off(void)
  99. {
  100. i2c_smbus_write_byte_data(isp1301_i2c_client,
  101. ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
  102. OTG1_VBUS_DRV);
  103. }
  104. static void ohci_nxp_start_hc(void)
  105. {
  106. void __iomem *usb_otg_stat_control = ioremap(USB_CONFIG_BASE + 0x110, 4);
  107. unsigned long tmp;
  108. if (WARN_ON(!usb_otg_stat_control))
  109. return;
  110. tmp = __raw_readl(usb_otg_stat_control) | HOST_EN;
  111. __raw_writel(tmp, usb_otg_stat_control);
  112. isp1301_vbus_on();
  113. iounmap(usb_otg_stat_control);
  114. }
  115. static void ohci_nxp_stop_hc(void)
  116. {
  117. void __iomem *usb_otg_stat_control = ioremap(USB_CONFIG_BASE + 0x110, 4);
  118. unsigned long tmp;
  119. if (WARN_ON(!usb_otg_stat_control))
  120. return;
  121. isp1301_vbus_off();
  122. tmp = __raw_readl(usb_otg_stat_control) & ~HOST_EN;
  123. __raw_writel(tmp, usb_otg_stat_control);
  124. iounmap(usb_otg_stat_control);
  125. }
  126. static int ohci_hcd_nxp_probe(struct platform_device *pdev)
  127. {
  128. struct usb_hcd *hcd = NULL;
  129. const struct hc_driver *driver = &ohci_nxp_hc_driver;
  130. struct resource *res;
  131. int ret = 0, irq;
  132. struct device_node *isp1301_node;
  133. struct clk *usb_host_clk;
  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. of_node_put(isp1301_node);
  142. if (!isp1301_i2c_client)
  143. return -EPROBE_DEFER;
  144. ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  145. if (ret)
  146. goto fail_disable;
  147. dev_dbg(&pdev->dev, "%s: " DRIVER_DESC " (nxp)\n", hcd_name);
  148. if (usb_disabled()) {
  149. dev_err(&pdev->dev, "USB is disabled\n");
  150. ret = -ENODEV;
  151. goto fail_disable;
  152. }
  153. /* Enable USB host clock */
  154. usb_host_clk = devm_clk_get_enabled(&pdev->dev, NULL);
  155. if (IS_ERR(usb_host_clk)) {
  156. dev_err(&pdev->dev, "failed to acquire and start USB OHCI clock\n");
  157. ret = PTR_ERR(usb_host_clk);
  158. goto fail_disable;
  159. }
  160. isp1301_configure();
  161. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  162. if (!hcd) {
  163. dev_err(&pdev->dev, "Failed to allocate HC buffer\n");
  164. ret = -ENOMEM;
  165. goto fail_disable;
  166. }
  167. hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  168. if (IS_ERR(hcd->regs)) {
  169. ret = PTR_ERR(hcd->regs);
  170. goto fail_resource;
  171. }
  172. hcd->rsrc_start = res->start;
  173. hcd->rsrc_len = resource_size(res);
  174. irq = platform_get_irq(pdev, 0);
  175. if (irq < 0) {
  176. ret = -ENXIO;
  177. goto fail_resource;
  178. }
  179. ohci_nxp_start_hc();
  180. platform_set_drvdata(pdev, hcd);
  181. dev_info(&pdev->dev, "at 0x%p, irq %d\n", hcd->regs, hcd->irq);
  182. ret = usb_add_hcd(hcd, irq, 0);
  183. if (ret == 0) {
  184. device_wakeup_enable(hcd->self.controller);
  185. return ret;
  186. }
  187. ohci_nxp_stop_hc();
  188. fail_resource:
  189. usb_put_hcd(hcd);
  190. fail_disable:
  191. isp1301_i2c_client = NULL;
  192. return ret;
  193. }
  194. static void ohci_hcd_nxp_remove(struct platform_device *pdev)
  195. {
  196. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  197. usb_remove_hcd(hcd);
  198. ohci_nxp_stop_hc();
  199. usb_put_hcd(hcd);
  200. isp1301_i2c_client = NULL;
  201. }
  202. /* work with hotplug and coldplug */
  203. MODULE_ALIAS("platform:usb-ohci");
  204. #ifdef CONFIG_OF
  205. static const struct of_device_id ohci_hcd_nxp_match[] = {
  206. { .compatible = "nxp,ohci-nxp" },
  207. {},
  208. };
  209. MODULE_DEVICE_TABLE(of, ohci_hcd_nxp_match);
  210. #endif
  211. static struct platform_driver ohci_hcd_nxp_driver = {
  212. .driver = {
  213. .name = "usb-ohci",
  214. .of_match_table = of_match_ptr(ohci_hcd_nxp_match),
  215. },
  216. .probe = ohci_hcd_nxp_probe,
  217. .remove_new = ohci_hcd_nxp_remove,
  218. };
  219. static int __init ohci_nxp_init(void)
  220. {
  221. if (usb_disabled())
  222. return -ENODEV;
  223. ohci_init_driver(&ohci_nxp_hc_driver, NULL);
  224. return platform_driver_register(&ohci_hcd_nxp_driver);
  225. }
  226. module_init(ohci_nxp_init);
  227. static void __exit ohci_nxp_cleanup(void)
  228. {
  229. platform_driver_unregister(&ohci_hcd_nxp_driver);
  230. }
  231. module_exit(ohci_nxp_cleanup);
  232. MODULE_DESCRIPTION(DRIVER_DESC);
  233. MODULE_LICENSE("GPL v2");