ohci-nxp.c 8.4 KB

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