ohci-nxp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * driver for NXP USB Host devices
  3. *
  4. * Currently supported OHCI host devices:
  5. * - Philips PNX4008
  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/platform_device.h>
  23. #include <linux/i2c.h>
  24. #include <mach/hardware.h>
  25. #include <asm/io.h>
  26. #include <mach/platform.h>
  27. #include <mach/irqs.h>
  28. #include <asm/gpio.h>
  29. #define USB_CTRL IO_ADDRESS(PNX4008_PWRMAN_BASE + 0x64)
  30. /* USB_CTRL bit defines */
  31. #define USB_SLAVE_HCLK_EN (1 << 24)
  32. #define USB_HOST_NEED_CLK_EN (1 << 21)
  33. #define USB_OTG_CLK_CTRL IO_ADDRESS(PNX4008_USB_CONFIG_BASE + 0xFF4)
  34. #define USB_OTG_CLK_STAT IO_ADDRESS(PNX4008_USB_CONFIG_BASE + 0xFF8)
  35. /* USB_OTG_CLK_CTRL bit defines */
  36. #define AHB_M_CLOCK_ON (1 << 4)
  37. #define OTG_CLOCK_ON (1 << 3)
  38. #define I2C_CLOCK_ON (1 << 2)
  39. #define DEV_CLOCK_ON (1 << 1)
  40. #define HOST_CLOCK_ON (1 << 0)
  41. #define USB_OTG_STAT_CONTROL IO_ADDRESS(PNX4008_USB_CONFIG_BASE + 0x110)
  42. /* USB_OTG_STAT_CONTROL bit defines */
  43. #define TRANSPARENT_I2C_EN (1 << 7)
  44. #define HOST_EN (1 << 0)
  45. /* ISP1301 USB transceiver I2C registers */
  46. #define ISP1301_MODE_CONTROL_1 0x04 /* u8 read, set, +1 clear */
  47. #define MC1_SPEED_REG (1 << 0)
  48. #define MC1_SUSPEND_REG (1 << 1)
  49. #define MC1_DAT_SE0 (1 << 2)
  50. #define MC1_TRANSPARENT (1 << 3)
  51. #define MC1_BDIS_ACON_EN (1 << 4)
  52. #define MC1_OE_INT_EN (1 << 5)
  53. #define MC1_UART_EN (1 << 6)
  54. #define MC1_MASK 0x7f
  55. #define ISP1301_MODE_CONTROL_2 0x12 /* u8 read, set, +1 clear */
  56. #define MC2_GLOBAL_PWR_DN (1 << 0)
  57. #define MC2_SPD_SUSP_CTRL (1 << 1)
  58. #define MC2_BI_DI (1 << 2)
  59. #define MC2_TRANSP_BDIR0 (1 << 3)
  60. #define MC2_TRANSP_BDIR1 (1 << 4)
  61. #define MC2_AUDIO_EN (1 << 5)
  62. #define MC2_PSW_EN (1 << 6)
  63. #define MC2_EN2V7 (1 << 7)
  64. #define ISP1301_OTG_CONTROL_1 0x06 /* u8 read, set, +1 clear */
  65. # define OTG1_DP_PULLUP (1 << 0)
  66. # define OTG1_DM_PULLUP (1 << 1)
  67. # define OTG1_DP_PULLDOWN (1 << 2)
  68. # define OTG1_DM_PULLDOWN (1 << 3)
  69. # define OTG1_ID_PULLDOWN (1 << 4)
  70. # define OTG1_VBUS_DRV (1 << 5)
  71. # define OTG1_VBUS_DISCHRG (1 << 6)
  72. # define OTG1_VBUS_CHRG (1 << 7)
  73. #define ISP1301_OTG_STATUS 0x10 /* u8 readonly */
  74. # define OTG_B_SESS_END (1 << 6)
  75. # define OTG_B_SESS_VLD (1 << 7)
  76. #define ISP1301_I2C_ADDR 0x2C
  77. #define ISP1301_I2C_MODE_CONTROL_1 0x4
  78. #define ISP1301_I2C_MODE_CONTROL_2 0x12
  79. #define ISP1301_I2C_OTG_CONTROL_1 0x6
  80. #define ISP1301_I2C_OTG_CONTROL_2 0x10
  81. #define ISP1301_I2C_INTERRUPT_SOURCE 0x8
  82. #define ISP1301_I2C_INTERRUPT_LATCH 0xA
  83. #define ISP1301_I2C_INTERRUPT_FALLING 0xC
  84. #define ISP1301_I2C_INTERRUPT_RISING 0xE
  85. #define ISP1301_I2C_REG_CLEAR_ADDR 1
  86. static struct i2c_driver isp1301_driver;
  87. static struct i2c_client *isp1301_i2c_client;
  88. extern int usb_disabled(void);
  89. extern int ocpi_enable(void);
  90. static struct clk *usb_clk;
  91. static const unsigned short normal_i2c[] =
  92. { ISP1301_I2C_ADDR, ISP1301_I2C_ADDR + 1, I2C_CLIENT_END };
  93. static int isp1301_probe(struct i2c_client *client,
  94. const struct i2c_device_id *id)
  95. {
  96. return 0;
  97. }
  98. static int isp1301_remove(struct i2c_client *client)
  99. {
  100. return 0;
  101. }
  102. static const struct i2c_device_id isp1301_id[] = {
  103. { "isp1301_nxp", 0 },
  104. { }
  105. };
  106. static struct i2c_driver isp1301_driver = {
  107. .driver = {
  108. .name = "isp1301_nxp",
  109. },
  110. .probe = isp1301_probe,
  111. .remove = isp1301_remove,
  112. .id_table = isp1301_id,
  113. };
  114. static void i2c_write(u8 buf, u8 subaddr)
  115. {
  116. char tmpbuf[2];
  117. tmpbuf[0] = subaddr; /*register number */
  118. tmpbuf[1] = buf; /*register data */
  119. i2c_master_send(isp1301_i2c_client, &tmpbuf[0], 2);
  120. }
  121. static void isp1301_configure(void)
  122. {
  123. /* PNX4008 only supports DAT_SE0 USB mode */
  124. /* PNX4008 R2A requires setting the MAX603 to output 3.6V */
  125. /* Power up externel charge-pump */
  126. i2c_write(MC1_DAT_SE0 | MC1_SPEED_REG, ISP1301_I2C_MODE_CONTROL_1);
  127. i2c_write(~(MC1_DAT_SE0 | MC1_SPEED_REG),
  128. ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR);
  129. i2c_write(MC2_BI_DI | MC2_PSW_EN | MC2_SPD_SUSP_CTRL,
  130. ISP1301_I2C_MODE_CONTROL_2);
  131. i2c_write(~(MC2_BI_DI | MC2_PSW_EN | MC2_SPD_SUSP_CTRL),
  132. ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR);
  133. i2c_write(OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN,
  134. ISP1301_I2C_OTG_CONTROL_1);
  135. i2c_write(~(OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN),
  136. ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR);
  137. i2c_write(0xFF,
  138. ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR);
  139. i2c_write(0xFF,
  140. ISP1301_I2C_INTERRUPT_FALLING | ISP1301_I2C_REG_CLEAR_ADDR);
  141. i2c_write(0xFF,
  142. ISP1301_I2C_INTERRUPT_RISING | ISP1301_I2C_REG_CLEAR_ADDR);
  143. }
  144. static inline void isp1301_vbus_on(void)
  145. {
  146. i2c_write(OTG1_VBUS_DRV, ISP1301_I2C_OTG_CONTROL_1);
  147. }
  148. static inline void isp1301_vbus_off(void)
  149. {
  150. i2c_write(OTG1_VBUS_DRV,
  151. ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR);
  152. }
  153. static void nxp_start_hc(void)
  154. {
  155. unsigned long tmp = __raw_readl(USB_OTG_STAT_CONTROL) | HOST_EN;
  156. __raw_writel(tmp, USB_OTG_STAT_CONTROL);
  157. isp1301_vbus_on();
  158. }
  159. static void nxp_stop_hc(void)
  160. {
  161. unsigned long tmp;
  162. isp1301_vbus_off();
  163. tmp = __raw_readl(USB_OTG_STAT_CONTROL) & ~HOST_EN;
  164. __raw_writel(tmp, USB_OTG_STAT_CONTROL);
  165. }
  166. static int __devinit ohci_nxp_start(struct usb_hcd *hcd)
  167. {
  168. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  169. int ret;
  170. if ((ret = ohci_init(ohci)) < 0)
  171. return ret;
  172. if ((ret = ohci_run(ohci)) < 0) {
  173. dev_err(hcd->self.controller, "can't start\n");
  174. ohci_stop(hcd);
  175. return ret;
  176. }
  177. return 0;
  178. }
  179. static const struct hc_driver ohci_nxp_hc_driver = {
  180. .description = hcd_name,
  181. .product_desc = "nxp OHCI",
  182. /*
  183. * generic hardware linkage
  184. */
  185. .irq = ohci_irq,
  186. .flags = HCD_USB11 | HCD_MEMORY,
  187. .hcd_priv_size = sizeof(struct ohci_hcd),
  188. /*
  189. * basic lifecycle operations
  190. */
  191. .start = ohci_nxp_start,
  192. .stop = ohci_stop,
  193. .shutdown = ohci_shutdown,
  194. /*
  195. * managing i/o requests and associated device resources
  196. */
  197. .urb_enqueue = ohci_urb_enqueue,
  198. .urb_dequeue = ohci_urb_dequeue,
  199. .endpoint_disable = ohci_endpoint_disable,
  200. /*
  201. * scheduling support
  202. */
  203. .get_frame_number = ohci_get_frame,
  204. /*
  205. * root hub support
  206. */
  207. .hub_status_data = ohci_hub_status_data,
  208. .hub_control = ohci_hub_control,
  209. #ifdef CONFIG_PM
  210. .bus_suspend = ohci_bus_suspend,
  211. .bus_resume = ohci_bus_resume,
  212. #endif
  213. .start_port_reset = ohci_start_port_reset,
  214. };
  215. #define USB_CLOCK_MASK (AHB_M_CLOCK_ON| OTG_CLOCK_ON | HOST_CLOCK_ON | I2C_CLOCK_ON)
  216. static void nxp_set_usb_bits(void)
  217. {
  218. start_int_set_falling_edge(SE_USB_OTG_ATX_INT_N);
  219. start_int_ack(SE_USB_OTG_ATX_INT_N);
  220. start_int_umask(SE_USB_OTG_ATX_INT_N);
  221. start_int_set_rising_edge(SE_USB_OTG_TIMER_INT);
  222. start_int_ack(SE_USB_OTG_TIMER_INT);
  223. start_int_umask(SE_USB_OTG_TIMER_INT);
  224. start_int_set_rising_edge(SE_USB_I2C_INT);
  225. start_int_ack(SE_USB_I2C_INT);
  226. start_int_umask(SE_USB_I2C_INT);
  227. start_int_set_rising_edge(SE_USB_INT);
  228. start_int_ack(SE_USB_INT);
  229. start_int_umask(SE_USB_INT);
  230. start_int_set_rising_edge(SE_USB_NEED_CLK_INT);
  231. start_int_ack(SE_USB_NEED_CLK_INT);
  232. start_int_umask(SE_USB_NEED_CLK_INT);
  233. start_int_set_rising_edge(SE_USB_AHB_NEED_CLK_INT);
  234. start_int_ack(SE_USB_AHB_NEED_CLK_INT);
  235. start_int_umask(SE_USB_AHB_NEED_CLK_INT);
  236. }
  237. static void nxp_unset_usb_bits(void)
  238. {
  239. start_int_mask(SE_USB_OTG_ATX_INT_N);
  240. start_int_mask(SE_USB_OTG_TIMER_INT);
  241. start_int_mask(SE_USB_I2C_INT);
  242. start_int_mask(SE_USB_INT);
  243. start_int_mask(SE_USB_NEED_CLK_INT);
  244. start_int_mask(SE_USB_AHB_NEED_CLK_INT);
  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 i2c_adapter *i2c_adap;
  252. struct i2c_board_info i2c_info;
  253. int ret = 0, irq;
  254. dev_dbg(&pdev->dev, "%s: " DRIVER_DESC " (nxp)\n", hcd_name);
  255. if (usb_disabled()) {
  256. err("USB is disabled");
  257. ret = -ENODEV;
  258. goto out;
  259. }
  260. if (pdev->num_resources != 2
  261. || pdev->resource[0].flags != IORESOURCE_MEM
  262. || pdev->resource[1].flags != IORESOURCE_IRQ) {
  263. err("Invalid resource configuration");
  264. ret = -ENODEV;
  265. goto out;
  266. }
  267. /* Enable AHB slave USB clock, needed for further USB clock control */
  268. __raw_writel(USB_SLAVE_HCLK_EN | (1 << 19), USB_CTRL);
  269. ret = i2c_add_driver(&isp1301_driver);
  270. if (ret < 0) {
  271. err("failed to add ISP1301 driver");
  272. goto out;
  273. }
  274. i2c_adap = i2c_get_adapter(2);
  275. memset(&i2c_info, 0, sizeof(struct i2c_board_info));
  276. strlcpy(i2c_info.type, "isp1301_nxp", I2C_NAME_SIZE);
  277. isp1301_i2c_client = i2c_new_probed_device(i2c_adap, &i2c_info,
  278. normal_i2c, NULL);
  279. i2c_put_adapter(i2c_adap);
  280. if (!isp1301_i2c_client) {
  281. err("failed to connect I2C to ISP1301 USB Transceiver");
  282. ret = -ENODEV;
  283. goto out_i2c_driver;
  284. }
  285. isp1301_configure();
  286. /* Enable USB PLL */
  287. usb_clk = clk_get(&pdev->dev, "ck_pll5");
  288. if (IS_ERR(usb_clk)) {
  289. err("failed to acquire USB PLL");
  290. ret = PTR_ERR(usb_clk);
  291. goto out1;
  292. }
  293. ret = clk_enable(usb_clk);
  294. if (ret < 0) {
  295. err("failed to start USB PLL");
  296. goto out2;
  297. }
  298. ret = clk_set_rate(usb_clk, 48000);
  299. if (ret < 0) {
  300. err("failed to set USB clock rate");
  301. goto out3;
  302. }
  303. __raw_writel(__raw_readl(USB_CTRL) | USB_HOST_NEED_CLK_EN, USB_CTRL);
  304. /* Set to enable all needed USB clocks */
  305. __raw_writel(USB_CLOCK_MASK, USB_OTG_CLK_CTRL);
  306. while ((__raw_readl(USB_OTG_CLK_STAT) & USB_CLOCK_MASK) !=
  307. USB_CLOCK_MASK) ;
  308. hcd = usb_create_hcd (driver, &pdev->dev, dev_name(&pdev->dev));
  309. if (!hcd) {
  310. err("Failed to allocate HC buffer");
  311. ret = -ENOMEM;
  312. goto out3;
  313. }
  314. /* Set all USB bits in the Start Enable register */
  315. nxp_set_usb_bits();
  316. hcd->rsrc_start = pdev->resource[0].start;
  317. hcd->rsrc_len = pdev->resource[0].end - pdev->resource[0].start + 1;
  318. if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
  319. dev_dbg(&pdev->dev, "request_mem_region failed\n");
  320. ret = -ENOMEM;
  321. goto out4;
  322. }
  323. hcd->regs = (void __iomem *)pdev->resource[0].start;
  324. irq = platform_get_irq(pdev, 0);
  325. if (irq < 0) {
  326. ret = -ENXIO;
  327. goto out4;
  328. }
  329. nxp_start_hc();
  330. platform_set_drvdata(pdev, hcd);
  331. ohci = hcd_to_ohci(hcd);
  332. ohci_hcd_init(ohci);
  333. dev_info(&pdev->dev, "at 0x%p, irq %d\n", hcd->regs, hcd->irq);
  334. ret = usb_add_hcd(hcd, irq, 0);
  335. if (ret == 0)
  336. return ret;
  337. nxp_stop_hc();
  338. out4:
  339. nxp_unset_usb_bits();
  340. usb_put_hcd(hcd);
  341. out3:
  342. clk_disable(usb_clk);
  343. out2:
  344. clk_put(usb_clk);
  345. out1:
  346. i2c_unregister_device(isp1301_i2c_client);
  347. isp1301_i2c_client = NULL;
  348. out_i2c_driver:
  349. i2c_del_driver(&isp1301_driver);
  350. out:
  351. return ret;
  352. }
  353. static int usb_hcd_nxp_remove(struct platform_device *pdev)
  354. {
  355. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  356. usb_remove_hcd(hcd);
  357. nxp_stop_hc();
  358. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  359. usb_put_hcd(hcd);
  360. nxp_unset_usb_bits();
  361. clk_disable(usb_clk);
  362. clk_put(usb_clk);
  363. i2c_unregister_device(isp1301_i2c_client);
  364. isp1301_i2c_client = NULL;
  365. i2c_del_driver(&isp1301_driver);
  366. platform_set_drvdata(pdev, NULL);
  367. return 0;
  368. }
  369. /* work with hotplug and coldplug */
  370. MODULE_ALIAS("platform:usb-ohci");
  371. static struct platform_driver usb_hcd_nxp_driver = {
  372. .driver = {
  373. .name = "usb-ohci",
  374. .owner = THIS_MODULE,
  375. },
  376. .probe = usb_hcd_nxp_probe,
  377. .remove = usb_hcd_nxp_remove,
  378. };