ohci-nxp.c 7.2 KB

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