ohci-nxp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * driver for NXP USB Host devices
  3. *
  4. * Currently supported OHCI host devices:
  5. * - Philips PNX4008
  6. * - NXP LPC32xx
  7. *
  8. * Authors: Dmitry Chigirev <source@mvista.com>
  9. * Vitaly Wool <vitalywool@gmail.com>
  10. *
  11. * register initialization is based on code examples provided by Philips
  12. * Copyright (c) 2005 Koninklijke Philips Electronics N.V.
  13. *
  14. * NOTE: This driver does not have suspend/resume functionality
  15. * This driver is intended for engineering development purposes only
  16. *
  17. * 2005-2006 (c) MontaVista Software, Inc. This file is licensed under
  18. * the terms of the GNU General Public License version 2. This program
  19. * is licensed "as is" without any warranty of any kind, whether express
  20. * or implied.
  21. */
  22. #include <linux/clk.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/i2c.h>
  25. #include <linux/of.h>
  26. #include <linux/usb/isp1301.h>
  27. #include <mach/hardware.h>
  28. #include <asm/mach-types.h>
  29. #include <asm/io.h>
  30. #include <mach/platform.h>
  31. #include <mach/irqs.h>
  32. #define USB_CONFIG_BASE 0x31020000
  33. #define PWRMAN_BASE 0x40004000
  34. #define USB_CTRL IO_ADDRESS(PWRMAN_BASE + 0x64)
  35. /* USB_CTRL bit defines */
  36. #define USB_SLAVE_HCLK_EN (1 << 24)
  37. #define USB_DEV_NEED_CLK_EN (1 << 22)
  38. #define USB_HOST_NEED_CLK_EN (1 << 21)
  39. #define PAD_CONTROL_LAST_DRIVEN (1 << 19)
  40. #define USB_OTG_STAT_CONTROL IO_ADDRESS(USB_CONFIG_BASE + 0x110)
  41. /* USB_OTG_STAT_CONTROL bit defines */
  42. #define TRANSPARENT_I2C_EN (1 << 7)
  43. #define HOST_EN (1 << 0)
  44. /* On LPC32xx, those are undefined */
  45. #ifndef start_int_set_falling_edge
  46. #define start_int_set_falling_edge(irq)
  47. #define start_int_set_rising_edge(irq)
  48. #define start_int_ack(irq)
  49. #define start_int_mask(irq)
  50. #define start_int_umask(irq)
  51. #endif
  52. static struct i2c_client *isp1301_i2c_client;
  53. extern int usb_disabled(void);
  54. static struct clk *usb_pll_clk;
  55. static struct clk *usb_dev_clk;
  56. static struct clk *usb_otg_clk;
  57. static void isp1301_configure_pnx4008(void)
  58. {
  59. /* PNX4008 only supports DAT_SE0 USB mode */
  60. /* PNX4008 R2A requires setting the MAX603 to output 3.6V */
  61. /* Power up externel charge-pump */
  62. i2c_smbus_write_byte_data(isp1301_i2c_client,
  63. ISP1301_I2C_MODE_CONTROL_1, MC1_DAT_SE0 | MC1_SPEED_REG);
  64. i2c_smbus_write_byte_data(isp1301_i2c_client,
  65. ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
  66. ~(MC1_DAT_SE0 | MC1_SPEED_REG));
  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_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR,
  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, OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN);
  75. i2c_smbus_write_byte_data(isp1301_i2c_client,
  76. ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
  77. ~(OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
  78. i2c_smbus_write_byte_data(isp1301_i2c_client,
  79. ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR, 0xFF);
  80. i2c_smbus_write_byte_data(isp1301_i2c_client,
  81. ISP1301_I2C_INTERRUPT_FALLING | ISP1301_I2C_REG_CLEAR_ADDR,
  82. 0xFF);
  83. i2c_smbus_write_byte_data(isp1301_i2c_client,
  84. ISP1301_I2C_INTERRUPT_RISING | ISP1301_I2C_REG_CLEAR_ADDR,
  85. 0xFF);
  86. }
  87. static void isp1301_configure_lpc32xx(void)
  88. {
  89. /* LPC32XX only supports DAT_SE0 USB mode */
  90. /* This sequence is important */
  91. /* Disable transparent UART mode first */
  92. i2c_smbus_write_byte_data(isp1301_i2c_client,
  93. (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
  94. MC1_UART_EN);
  95. i2c_smbus_write_byte_data(isp1301_i2c_client,
  96. (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
  97. ~MC1_SPEED_REG);
  98. i2c_smbus_write_byte_data(isp1301_i2c_client,
  99. ISP1301_I2C_MODE_CONTROL_1, MC1_SPEED_REG);
  100. i2c_smbus_write_byte_data(isp1301_i2c_client,
  101. (ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR),
  102. ~0);
  103. i2c_smbus_write_byte_data(isp1301_i2c_client,
  104. ISP1301_I2C_MODE_CONTROL_2,
  105. (MC2_BI_DI | MC2_PSW_EN | MC2_SPD_SUSP_CTRL));
  106. i2c_smbus_write_byte_data(isp1301_i2c_client,
  107. (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), ~0);
  108. i2c_smbus_write_byte_data(isp1301_i2c_client,
  109. ISP1301_I2C_MODE_CONTROL_1, MC1_DAT_SE0);
  110. i2c_smbus_write_byte_data(isp1301_i2c_client,
  111. ISP1301_I2C_OTG_CONTROL_1,
  112. (OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
  113. i2c_smbus_write_byte_data(isp1301_i2c_client,
  114. (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
  115. (OTG1_DM_PULLUP | OTG1_DP_PULLUP));
  116. i2c_smbus_write_byte_data(isp1301_i2c_client,
  117. ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
  118. i2c_smbus_write_byte_data(isp1301_i2c_client,
  119. ISP1301_I2C_INTERRUPT_FALLING | ISP1301_I2C_REG_CLEAR_ADDR,
  120. ~0);
  121. i2c_smbus_write_byte_data(isp1301_i2c_client,
  122. ISP1301_I2C_INTERRUPT_RISING | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
  123. /* Enable usb_need_clk clock after transceiver is initialized */
  124. __raw_writel(__raw_readl(USB_CTRL) | USB_HOST_NEED_CLK_EN, USB_CTRL);
  125. printk(KERN_INFO "ISP1301 Vendor ID : 0x%04x\n",
  126. i2c_smbus_read_word_data(isp1301_i2c_client, 0x00));
  127. printk(KERN_INFO "ISP1301 Product ID : 0x%04x\n",
  128. i2c_smbus_read_word_data(isp1301_i2c_client, 0x02));
  129. printk(KERN_INFO "ISP1301 Version ID : 0x%04x\n",
  130. i2c_smbus_read_word_data(isp1301_i2c_client, 0x14));
  131. }
  132. static void isp1301_configure(void)
  133. {
  134. if (machine_is_pnx4008())
  135. isp1301_configure_pnx4008();
  136. else
  137. isp1301_configure_lpc32xx();
  138. }
  139. static inline void isp1301_vbus_on(void)
  140. {
  141. i2c_smbus_write_byte_data(isp1301_i2c_client, ISP1301_I2C_OTG_CONTROL_1,
  142. OTG1_VBUS_DRV);
  143. }
  144. static inline void isp1301_vbus_off(void)
  145. {
  146. i2c_smbus_write_byte_data(isp1301_i2c_client,
  147. ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
  148. OTG1_VBUS_DRV);
  149. }
  150. static void nxp_start_hc(void)
  151. {
  152. unsigned long tmp = __raw_readl(USB_OTG_STAT_CONTROL) | HOST_EN;
  153. __raw_writel(tmp, USB_OTG_STAT_CONTROL);
  154. isp1301_vbus_on();
  155. }
  156. static void nxp_stop_hc(void)
  157. {
  158. unsigned long tmp;
  159. isp1301_vbus_off();
  160. tmp = __raw_readl(USB_OTG_STAT_CONTROL) & ~HOST_EN;
  161. __raw_writel(tmp, USB_OTG_STAT_CONTROL);
  162. }
  163. static int __devinit ohci_nxp_start(struct usb_hcd *hcd)
  164. {
  165. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  166. int ret;
  167. if ((ret = ohci_init(ohci)) < 0)
  168. return ret;
  169. if ((ret = ohci_run(ohci)) < 0) {
  170. dev_err(hcd->self.controller, "can't start\n");
  171. ohci_stop(hcd);
  172. return ret;
  173. }
  174. return 0;
  175. }
  176. static const struct hc_driver ohci_nxp_hc_driver = {
  177. .description = hcd_name,
  178. .product_desc = "nxp OHCI",
  179. /*
  180. * generic hardware linkage
  181. */
  182. .irq = ohci_irq,
  183. .flags = HCD_USB11 | HCD_MEMORY,
  184. .hcd_priv_size = sizeof(struct ohci_hcd),
  185. /*
  186. * basic lifecycle operations
  187. */
  188. .start = ohci_nxp_start,
  189. .stop = ohci_stop,
  190. .shutdown = ohci_shutdown,
  191. /*
  192. * managing i/o requests and associated device resources
  193. */
  194. .urb_enqueue = ohci_urb_enqueue,
  195. .urb_dequeue = ohci_urb_dequeue,
  196. .endpoint_disable = ohci_endpoint_disable,
  197. /*
  198. * scheduling support
  199. */
  200. .get_frame_number = ohci_get_frame,
  201. /*
  202. * root hub support
  203. */
  204. .hub_status_data = ohci_hub_status_data,
  205. .hub_control = ohci_hub_control,
  206. #ifdef CONFIG_PM
  207. .bus_suspend = ohci_bus_suspend,
  208. .bus_resume = ohci_bus_resume,
  209. #endif
  210. .start_port_reset = ohci_start_port_reset,
  211. };
  212. static void nxp_set_usb_bits(void)
  213. {
  214. if (machine_is_pnx4008()) {
  215. start_int_set_falling_edge(SE_USB_OTG_ATX_INT_N);
  216. start_int_ack(SE_USB_OTG_ATX_INT_N);
  217. start_int_umask(SE_USB_OTG_ATX_INT_N);
  218. start_int_set_rising_edge(SE_USB_OTG_TIMER_INT);
  219. start_int_ack(SE_USB_OTG_TIMER_INT);
  220. start_int_umask(SE_USB_OTG_TIMER_INT);
  221. start_int_set_rising_edge(SE_USB_I2C_INT);
  222. start_int_ack(SE_USB_I2C_INT);
  223. start_int_umask(SE_USB_I2C_INT);
  224. start_int_set_rising_edge(SE_USB_INT);
  225. start_int_ack(SE_USB_INT);
  226. start_int_umask(SE_USB_INT);
  227. start_int_set_rising_edge(SE_USB_NEED_CLK_INT);
  228. start_int_ack(SE_USB_NEED_CLK_INT);
  229. start_int_umask(SE_USB_NEED_CLK_INT);
  230. start_int_set_rising_edge(SE_USB_AHB_NEED_CLK_INT);
  231. start_int_ack(SE_USB_AHB_NEED_CLK_INT);
  232. start_int_umask(SE_USB_AHB_NEED_CLK_INT);
  233. }
  234. }
  235. static void nxp_unset_usb_bits(void)
  236. {
  237. if (machine_is_pnx4008()) {
  238. start_int_mask(SE_USB_OTG_ATX_INT_N);
  239. start_int_mask(SE_USB_OTG_TIMER_INT);
  240. start_int_mask(SE_USB_I2C_INT);
  241. start_int_mask(SE_USB_INT);
  242. start_int_mask(SE_USB_NEED_CLK_INT);
  243. start_int_mask(SE_USB_AHB_NEED_CLK_INT);
  244. }
  245. }
  246. static int __devinit usb_hcd_nxp_probe(struct platform_device *pdev)
  247. {
  248. struct usb_hcd *hcd = 0;
  249. struct ohci_hcd *ohci;
  250. const struct hc_driver *driver = &ohci_nxp_hc_driver;
  251. struct resource *res;
  252. int ret = 0, irq;
  253. struct device_node *isp1301_node;
  254. if (pdev->dev.of_node) {
  255. isp1301_node = of_parse_phandle(pdev->dev.of_node,
  256. "transceiver", 0);
  257. } else {
  258. isp1301_node = NULL;
  259. }
  260. isp1301_i2c_client = isp1301_get_client(isp1301_node);
  261. if (!isp1301_i2c_client) {
  262. ret = -EPROBE_DEFER;
  263. goto out;
  264. }
  265. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  266. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  267. dev_dbg(&pdev->dev, "%s: " DRIVER_DESC " (nxp)\n", hcd_name);
  268. if (usb_disabled()) {
  269. dev_err(&pdev->dev, "USB is disabled\n");
  270. ret = -ENODEV;
  271. goto out;
  272. }
  273. /* Enable AHB slave USB clock, needed for further USB clock control */
  274. __raw_writel(USB_SLAVE_HCLK_EN | PAD_CONTROL_LAST_DRIVEN, USB_CTRL);
  275. /* Enable USB PLL */
  276. usb_pll_clk = clk_get(&pdev->dev, "ck_pll5");
  277. if (IS_ERR(usb_pll_clk)) {
  278. dev_err(&pdev->dev, "failed to acquire USB PLL\n");
  279. ret = PTR_ERR(usb_pll_clk);
  280. goto out1;
  281. }
  282. ret = clk_enable(usb_pll_clk);
  283. if (ret < 0) {
  284. dev_err(&pdev->dev, "failed to start USB PLL\n");
  285. goto out2;
  286. }
  287. ret = clk_set_rate(usb_pll_clk, 48000);
  288. if (ret < 0) {
  289. dev_err(&pdev->dev, "failed to set USB clock rate\n");
  290. goto out3;
  291. }
  292. /* Enable USB device clock */
  293. usb_dev_clk = clk_get(&pdev->dev, "ck_usbd");
  294. if (IS_ERR(usb_dev_clk)) {
  295. dev_err(&pdev->dev, "failed to acquire USB DEV Clock\n");
  296. ret = PTR_ERR(usb_dev_clk);
  297. goto out4;
  298. }
  299. ret = clk_enable(usb_dev_clk);
  300. if (ret < 0) {
  301. dev_err(&pdev->dev, "failed to start USB DEV Clock\n");
  302. goto out5;
  303. }
  304. /* Enable USB otg clocks */
  305. usb_otg_clk = clk_get(&pdev->dev, "ck_usb_otg");
  306. if (IS_ERR(usb_otg_clk)) {
  307. dev_err(&pdev->dev, "failed to acquire USB DEV Clock\n");
  308. ret = PTR_ERR(usb_dev_clk);
  309. goto out6;
  310. }
  311. __raw_writel(__raw_readl(USB_CTRL) | USB_HOST_NEED_CLK_EN, USB_CTRL);
  312. ret = clk_enable(usb_otg_clk);
  313. if (ret < 0) {
  314. dev_err(&pdev->dev, "failed to start USB DEV Clock\n");
  315. goto out7;
  316. }
  317. isp1301_configure();
  318. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  319. if (!hcd) {
  320. dev_err(&pdev->dev, "Failed to allocate HC buffer\n");
  321. ret = -ENOMEM;
  322. goto out8;
  323. }
  324. /* Set all USB bits in the Start Enable register */
  325. nxp_set_usb_bits();
  326. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  327. if (!res) {
  328. dev_err(&pdev->dev, "Failed to get MEM resource\n");
  329. ret = -ENOMEM;
  330. goto out8;
  331. }
  332. hcd->regs = devm_request_and_ioremap(&pdev->dev, res);
  333. if (!hcd->regs) {
  334. dev_err(&pdev->dev, "Failed to devm_request_and_ioremap\n");
  335. ret = -ENOMEM;
  336. goto out8;
  337. }
  338. hcd->rsrc_start = res->start;
  339. hcd->rsrc_len = resource_size(res);
  340. irq = platform_get_irq(pdev, 0);
  341. if (irq < 0) {
  342. ret = -ENXIO;
  343. goto out8;
  344. }
  345. nxp_start_hc();
  346. platform_set_drvdata(pdev, hcd);
  347. ohci = hcd_to_ohci(hcd);
  348. ohci_hcd_init(ohci);
  349. dev_info(&pdev->dev, "at 0x%p, irq %d\n", hcd->regs, hcd->irq);
  350. ret = usb_add_hcd(hcd, irq, 0);
  351. if (ret == 0)
  352. return ret;
  353. nxp_stop_hc();
  354. out8:
  355. nxp_unset_usb_bits();
  356. usb_put_hcd(hcd);
  357. out7:
  358. clk_disable(usb_otg_clk);
  359. out6:
  360. clk_put(usb_otg_clk);
  361. out5:
  362. clk_disable(usb_dev_clk);
  363. out4:
  364. clk_put(usb_dev_clk);
  365. out3:
  366. clk_disable(usb_pll_clk);
  367. out2:
  368. clk_put(usb_pll_clk);
  369. out1:
  370. isp1301_i2c_client = NULL;
  371. out:
  372. return ret;
  373. }
  374. static int usb_hcd_nxp_remove(struct platform_device *pdev)
  375. {
  376. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  377. usb_remove_hcd(hcd);
  378. nxp_stop_hc();
  379. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  380. usb_put_hcd(hcd);
  381. nxp_unset_usb_bits();
  382. clk_disable(usb_pll_clk);
  383. clk_put(usb_pll_clk);
  384. clk_disable(usb_dev_clk);
  385. clk_put(usb_dev_clk);
  386. i2c_unregister_device(isp1301_i2c_client);
  387. isp1301_i2c_client = NULL;
  388. platform_set_drvdata(pdev, NULL);
  389. return 0;
  390. }
  391. /* work with hotplug and coldplug */
  392. MODULE_ALIAS("platform:usb-ohci");
  393. #ifdef CONFIG_OF
  394. static const struct of_device_id usb_hcd_nxp_match[] = {
  395. { .compatible = "nxp,ohci-nxp" },
  396. {},
  397. };
  398. MODULE_DEVICE_TABLE(of, usb_hcd_nxp_match);
  399. #endif
  400. static struct platform_driver usb_hcd_nxp_driver = {
  401. .driver = {
  402. .name = "usb-ohci",
  403. .owner = THIS_MODULE,
  404. .of_match_table = of_match_ptr(usb_hcd_nxp_match),
  405. },
  406. .probe = usb_hcd_nxp_probe,
  407. .remove = usb_hcd_nxp_remove,
  408. };