ohci-nxp.c 9.2 KB

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