ohci-nxp.c 7.4 KB

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