ohci-nxp.c 7.4 KB

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