ohci-nxp.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  157. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  158. dev_dbg(&pdev->dev, "%s: " DRIVER_DESC " (nxp)\n", hcd_name);
  159. if (usb_disabled()) {
  160. dev_err(&pdev->dev, "USB is disabled\n");
  161. ret = -ENODEV;
  162. goto fail_disable;
  163. }
  164. /* Enable AHB slave USB clock, needed for further USB clock control */
  165. __raw_writel(USB_SLAVE_HCLK_EN | PAD_CONTROL_LAST_DRIVEN, USB_CTRL);
  166. /* Enable USB PLL */
  167. usb_pll_clk = clk_get(&pdev->dev, "ck_pll5");
  168. if (IS_ERR(usb_pll_clk)) {
  169. dev_err(&pdev->dev, "failed to acquire USB PLL\n");
  170. ret = PTR_ERR(usb_pll_clk);
  171. goto fail_pll;
  172. }
  173. ret = clk_enable(usb_pll_clk);
  174. if (ret < 0) {
  175. dev_err(&pdev->dev, "failed to start USB PLL\n");
  176. goto fail_pllen;
  177. }
  178. ret = clk_set_rate(usb_pll_clk, 48000);
  179. if (ret < 0) {
  180. dev_err(&pdev->dev, "failed to set USB clock rate\n");
  181. goto fail_rate;
  182. }
  183. /* Enable USB device clock */
  184. usb_dev_clk = clk_get(&pdev->dev, "ck_usbd");
  185. if (IS_ERR(usb_dev_clk)) {
  186. dev_err(&pdev->dev, "failed to acquire USB DEV Clock\n");
  187. ret = PTR_ERR(usb_dev_clk);
  188. goto fail_dev;
  189. }
  190. ret = clk_enable(usb_dev_clk);
  191. if (ret < 0) {
  192. dev_err(&pdev->dev, "failed to start USB DEV Clock\n");
  193. goto fail_deven;
  194. }
  195. /* Enable USB otg clocks */
  196. usb_otg_clk = clk_get(&pdev->dev, "ck_usb_otg");
  197. if (IS_ERR(usb_otg_clk)) {
  198. dev_err(&pdev->dev, "failed to acquire USB DEV Clock\n");
  199. ret = PTR_ERR(usb_otg_clk);
  200. goto fail_otg;
  201. }
  202. __raw_writel(__raw_readl(USB_CTRL) | USB_HOST_NEED_CLK_EN, USB_CTRL);
  203. ret = clk_enable(usb_otg_clk);
  204. if (ret < 0) {
  205. dev_err(&pdev->dev, "failed to start USB DEV Clock\n");
  206. goto fail_otgen;
  207. }
  208. isp1301_configure();
  209. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  210. if (!hcd) {
  211. dev_err(&pdev->dev, "Failed to allocate HC buffer\n");
  212. ret = -ENOMEM;
  213. goto fail_hcd;
  214. }
  215. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  216. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  217. if (IS_ERR(hcd->regs)) {
  218. ret = PTR_ERR(hcd->regs);
  219. goto fail_resource;
  220. }
  221. hcd->rsrc_start = res->start;
  222. hcd->rsrc_len = resource_size(res);
  223. irq = platform_get_irq(pdev, 0);
  224. if (irq < 0) {
  225. ret = -ENXIO;
  226. goto fail_resource;
  227. }
  228. ohci_nxp_start_hc();
  229. platform_set_drvdata(pdev, hcd);
  230. dev_info(&pdev->dev, "at 0x%p, irq %d\n", hcd->regs, hcd->irq);
  231. ret = usb_add_hcd(hcd, irq, 0);
  232. if (ret == 0)
  233. return ret;
  234. ohci_nxp_stop_hc();
  235. fail_resource:
  236. usb_put_hcd(hcd);
  237. fail_hcd:
  238. clk_disable(usb_otg_clk);
  239. fail_otgen:
  240. clk_put(usb_otg_clk);
  241. fail_otg:
  242. clk_disable(usb_dev_clk);
  243. fail_deven:
  244. clk_put(usb_dev_clk);
  245. fail_dev:
  246. fail_rate:
  247. clk_disable(usb_pll_clk);
  248. fail_pllen:
  249. clk_put(usb_pll_clk);
  250. fail_pll:
  251. fail_disable:
  252. isp1301_i2c_client = NULL;
  253. return ret;
  254. }
  255. static int ohci_hcd_nxp_remove(struct platform_device *pdev)
  256. {
  257. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  258. usb_remove_hcd(hcd);
  259. ohci_nxp_stop_hc();
  260. usb_put_hcd(hcd);
  261. clk_disable(usb_pll_clk);
  262. clk_put(usb_pll_clk);
  263. clk_disable(usb_dev_clk);
  264. clk_put(usb_dev_clk);
  265. i2c_unregister_device(isp1301_i2c_client);
  266. isp1301_i2c_client = NULL;
  267. return 0;
  268. }
  269. /* work with hotplug and coldplug */
  270. MODULE_ALIAS("platform:usb-ohci");
  271. #ifdef CONFIG_OF
  272. static const struct of_device_id ohci_hcd_nxp_match[] = {
  273. { .compatible = "nxp,ohci-nxp" },
  274. {},
  275. };
  276. MODULE_DEVICE_TABLE(of, ohci_hcd_nxp_match);
  277. #endif
  278. static struct platform_driver ohci_hcd_nxp_driver = {
  279. .driver = {
  280. .name = "usb-ohci",
  281. .owner = THIS_MODULE,
  282. .of_match_table = of_match_ptr(ohci_hcd_nxp_match),
  283. },
  284. .probe = ohci_hcd_nxp_probe,
  285. .remove = ohci_hcd_nxp_remove,
  286. };
  287. static int __init ohci_nxp_init(void)
  288. {
  289. if (usb_disabled())
  290. return -ENODEV;
  291. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  292. ohci_init_driver(&ohci_nxp_hc_driver, NULL);
  293. return platform_driver_register(&ohci_hcd_nxp_driver);
  294. }
  295. module_init(ohci_nxp_init);
  296. static void __exit ohci_nxp_cleanup(void)
  297. {
  298. platform_driver_unregister(&ohci_hcd_nxp_driver);
  299. }
  300. module_exit(ohci_nxp_cleanup);
  301. MODULE_DESCRIPTION(DRIVER_DESC);
  302. MODULE_LICENSE("GPL v2");