ohci-pnx4008.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * drivers/usb/host/ohci-pnx4008.c
  3. *
  4. * driver for Philips PNX4008 USB Host
  5. *
  6. * Authors: Dmitry Chigirev <source@mvista.com>
  7. * Vitaly Wool <vitalywool@gmail.com>
  8. *
  9. * register initialization is based on code examples provided by Philips
  10. * Copyright (c) 2005 Koninklijke Philips Electronics N.V.
  11. *
  12. * NOTE: This driver does not have suspend/resume functionality
  13. * This driver is intended for engineering development purposes only
  14. *
  15. * 2005-2006 (c) MontaVista Software, Inc. This file is licensed under
  16. * the terms of the GNU General Public License version 2. This program
  17. * is licensed "as is" without any warranty of any kind, whether express
  18. * or implied.
  19. */
  20. #include <linux/clk.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/i2c.h>
  23. #include <asm/hardware.h>
  24. #include <asm/io.h>
  25. #include <asm/mach-types.h>
  26. #include <asm/arch/platform.h>
  27. #include <asm/arch/irqs.h>
  28. #include <asm/arch/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. struct i2c_driver isp1301_driver;
  87. 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 int isp1301_probe(struct i2c_adapter *adap);
  92. static int isp1301_detach(struct i2c_client *client);
  93. static int isp1301_command(struct i2c_client *client, unsigned int cmd,
  94. void *arg);
  95. static unsigned short normal_i2c[] =
  96. { ISP1301_I2C_ADDR, ISP1301_I2C_ADDR + 1, I2C_CLIENT_END };
  97. static unsigned short dummy_i2c_addrlist[] = { I2C_CLIENT_END };
  98. static struct i2c_client_address_data addr_data = {
  99. .normal_i2c = normal_i2c,
  100. .probe = dummy_i2c_addrlist,
  101. .ignore = dummy_i2c_addrlist,
  102. };
  103. struct i2c_driver isp1301_driver = {
  104. .id = I2C_DRIVERID_I2CDEV, /* Fake Id */
  105. .class = I2C_CLASS_HWMON,
  106. .attach_adapter = isp1301_probe,
  107. .detach_client = isp1301_detach,
  108. .command = isp1301_command
  109. };
  110. static int isp1301_attach(struct i2c_adapter *adap, int addr, int kind)
  111. {
  112. struct i2c_client *c;
  113. c = (struct i2c_client *)kzalloc(sizeof(*c), SLAB_KERNEL);
  114. if (!c)
  115. return -ENOMEM;
  116. strcpy(c->name, "isp1301");
  117. c->flags = 0;
  118. c->addr = addr;
  119. c->adapter = adap;
  120. c->driver = &isp1301_driver;
  121. isp1301_i2c_client = c;
  122. return i2c_attach_client(c);
  123. }
  124. static int isp1301_probe(struct i2c_adapter *adap)
  125. {
  126. return i2c_probe(adap, &addr_data, isp1301_attach);
  127. }
  128. static int isp1301_detach(struct i2c_client *client)
  129. {
  130. i2c_detach_client(client);
  131. kfree(isp1301_i2c_client);
  132. return 0;
  133. }
  134. /* No commands defined */
  135. static int isp1301_command(struct i2c_client *client, unsigned int cmd,
  136. void *arg)
  137. {
  138. return 0;
  139. }
  140. static void i2c_write(u8 buf, u8 subaddr)
  141. {
  142. char tmpbuf[2];
  143. tmpbuf[0] = subaddr; /*register number */
  144. tmpbuf[1] = buf; /*register data */
  145. i2c_master_send(isp1301_i2c_client, &tmpbuf[0], 2);
  146. }
  147. static void isp1301_configure(void)
  148. {
  149. /* PNX4008 only supports DAT_SE0 USB mode */
  150. /* PNX4008 R2A requires setting the MAX603 to output 3.6V */
  151. /* Power up externel charge-pump */
  152. i2c_write(MC1_DAT_SE0 | MC1_SPEED_REG, ISP1301_I2C_MODE_CONTROL_1);
  153. i2c_write(~(MC1_DAT_SE0 | MC1_SPEED_REG),
  154. ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR);
  155. i2c_write(MC2_BI_DI | MC2_PSW_EN | MC2_SPD_SUSP_CTRL,
  156. ISP1301_I2C_MODE_CONTROL_2);
  157. i2c_write(~(MC2_BI_DI | MC2_PSW_EN | MC2_SPD_SUSP_CTRL),
  158. ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR);
  159. i2c_write(OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN,
  160. ISP1301_I2C_OTG_CONTROL_1);
  161. i2c_write(~(OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN),
  162. ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR);
  163. i2c_write(0xFF,
  164. ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR);
  165. i2c_write(0xFF,
  166. ISP1301_I2C_INTERRUPT_FALLING | ISP1301_I2C_REG_CLEAR_ADDR);
  167. i2c_write(0xFF,
  168. ISP1301_I2C_INTERRUPT_RISING | ISP1301_I2C_REG_CLEAR_ADDR);
  169. }
  170. static inline void isp1301_vbus_on(void)
  171. {
  172. i2c_write(OTG1_VBUS_DRV, ISP1301_I2C_OTG_CONTROL_1);
  173. }
  174. static inline void isp1301_vbus_off(void)
  175. {
  176. i2c_write(OTG1_VBUS_DRV,
  177. ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR);
  178. }
  179. static void pnx4008_start_hc(void)
  180. {
  181. unsigned long tmp = __raw_readl(USB_OTG_STAT_CONTROL) | HOST_EN;
  182. __raw_writel(tmp, USB_OTG_STAT_CONTROL);
  183. isp1301_vbus_on();
  184. }
  185. static void pnx4008_stop_hc(void)
  186. {
  187. unsigned long tmp;
  188. isp1301_vbus_off();
  189. tmp = __raw_readl(USB_OTG_STAT_CONTROL) & ~HOST_EN;
  190. __raw_writel(tmp, USB_OTG_STAT_CONTROL);
  191. }
  192. static int __devinit ohci_pnx4008_start(struct usb_hcd *hcd)
  193. {
  194. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  195. int ret;
  196. if ((ret = ohci_init(ohci)) < 0)
  197. return ret;
  198. if ((ret = ohci_run(ohci)) < 0) {
  199. dev_err(hcd->self.controller, "can't start\n");
  200. ohci_stop(hcd);
  201. return ret;
  202. }
  203. return 0;
  204. }
  205. static const struct hc_driver ohci_pnx4008_hc_driver = {
  206. .description = hcd_name,
  207. .product_desc = "pnx4008 OHCI",
  208. /*
  209. * generic hardware linkage
  210. */
  211. .irq = ohci_irq,
  212. .flags = HCD_USB11 | HCD_MEMORY,
  213. .hcd_priv_size = sizeof(struct ohci_hcd),
  214. /*
  215. * basic lifecycle operations
  216. */
  217. .start = ohci_pnx4008_start,
  218. .stop = ohci_stop,
  219. .shutdown = ohci_shutdown,
  220. /*
  221. * managing i/o requests and associated device resources
  222. */
  223. .urb_enqueue = ohci_urb_enqueue,
  224. .urb_dequeue = ohci_urb_dequeue,
  225. .endpoint_disable = ohci_endpoint_disable,
  226. /*
  227. * scheduling support
  228. */
  229. .get_frame_number = ohci_get_frame,
  230. /*
  231. * root hub support
  232. */
  233. .hub_status_data = ohci_hub_status_data,
  234. .hub_control = ohci_hub_control,
  235. .hub_irq_enable = ohci_rhsc_enable,
  236. #ifdef CONFIG_PM
  237. .bus_suspend = ohci_bus_suspend,
  238. .bus_resume = ohci_bus_resume,
  239. #endif
  240. .start_port_reset = ohci_start_port_reset,
  241. };
  242. #define USB_CLOCK_MASK (AHB_M_CLOCK_ON| OTG_CLOCK_ON | HOST_CLOCK_ON | I2C_CLOCK_ON)
  243. static void pnx4008_set_usb_bits(void)
  244. {
  245. start_int_set_falling_edge(SE_USB_OTG_ATX_INT_N);
  246. start_int_ack(SE_USB_OTG_ATX_INT_N);
  247. start_int_umask(SE_USB_OTG_ATX_INT_N);
  248. start_int_set_rising_edge(SE_USB_OTG_TIMER_INT);
  249. start_int_ack(SE_USB_OTG_TIMER_INT);
  250. start_int_umask(SE_USB_OTG_TIMER_INT);
  251. start_int_set_rising_edge(SE_USB_I2C_INT);
  252. start_int_ack(SE_USB_I2C_INT);
  253. start_int_umask(SE_USB_I2C_INT);
  254. start_int_set_rising_edge(SE_USB_INT);
  255. start_int_ack(SE_USB_INT);
  256. start_int_umask(SE_USB_INT);
  257. start_int_set_rising_edge(SE_USB_NEED_CLK_INT);
  258. start_int_ack(SE_USB_NEED_CLK_INT);
  259. start_int_umask(SE_USB_NEED_CLK_INT);
  260. start_int_set_rising_edge(SE_USB_AHB_NEED_CLK_INT);
  261. start_int_ack(SE_USB_AHB_NEED_CLK_INT);
  262. start_int_umask(SE_USB_AHB_NEED_CLK_INT);
  263. }
  264. static void pnx4008_unset_usb_bits(void)
  265. {
  266. start_int_mask(SE_USB_OTG_ATX_INT_N);
  267. start_int_mask(SE_USB_OTG_TIMER_INT);
  268. start_int_mask(SE_USB_I2C_INT);
  269. start_int_mask(SE_USB_INT);
  270. start_int_mask(SE_USB_NEED_CLK_INT);
  271. start_int_mask(SE_USB_AHB_NEED_CLK_INT);
  272. }
  273. static int __devinit usb_hcd_pnx4008_probe(struct platform_device *pdev)
  274. {
  275. struct usb_hcd *hcd = 0;
  276. struct ohci_hcd *ohci;
  277. const struct hc_driver *driver = &ohci_pnx4008_hc_driver;
  278. int ret = 0, irq;
  279. dev_dbg(&pdev->dev, "%s: " DRIVER_INFO " (pnx4008)\n", hcd_name);
  280. if (usb_disabled()) {
  281. err("USB is disabled");
  282. ret = -ENODEV;
  283. goto out;
  284. }
  285. if (pdev->num_resources != 2
  286. || pdev->resource[0].flags != IORESOURCE_MEM
  287. || pdev->resource[1].flags != IORESOURCE_IRQ) {
  288. err("Invalid resource configuration");
  289. ret = -ENODEV;
  290. goto out;
  291. }
  292. /* Enable AHB slave USB clock, needed for further USB clock control */
  293. __raw_writel(USB_SLAVE_HCLK_EN | (1 << 19), USB_CTRL);
  294. ret = i2c_add_driver(&isp1301_driver);
  295. if (ret < 0) {
  296. err("failed to connect I2C to ISP1301 USB Transceiver");
  297. goto out;
  298. }
  299. isp1301_configure();
  300. /* Enable USB PLL */
  301. usb_clk = clk_get(&pdev->dev, "ck_pll5");
  302. if (IS_ERR(usb_clk)) {
  303. err("failed to acquire USB PLL");
  304. ret = PTR_ERR(usb_clk);
  305. goto out1;
  306. }
  307. ret = clk_enable(usb_clk);
  308. if (ret < 0) {
  309. err("failed to start USB PLL");
  310. goto out2;
  311. }
  312. ret = clk_set_rate(usb_clk, 48000);
  313. if (ret < 0) {
  314. err("failed to set USB clock rate");
  315. goto out3;
  316. }
  317. __raw_writel(__raw_readl(USB_CTRL) | USB_HOST_NEED_CLK_EN, USB_CTRL);
  318. /* Set to enable all needed USB clocks */
  319. __raw_writel(USB_CLOCK_MASK, USB_OTG_CLK_CTRL);
  320. while ((__raw_readl(USB_OTG_CLK_STAT) & USB_CLOCK_MASK) !=
  321. USB_CLOCK_MASK) ;
  322. hcd = usb_create_hcd (driver, &pdev->dev, pdev->dev.bus_id);
  323. if (!hcd) {
  324. err("Failed to allocate HC buffer");
  325. ret = -ENOMEM;
  326. goto out3;
  327. }
  328. /* Set all USB bits in the Start Enable register */
  329. pnx4008_set_usb_bits();
  330. hcd->rsrc_start = pdev->resource[0].start;
  331. hcd->rsrc_len = pdev->resource[0].end - pdev->resource[0].start + 1;
  332. if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
  333. dev_dbg(&pdev->dev, "request_mem_region failed\n");
  334. ret = -ENOMEM;
  335. goto out4;
  336. }
  337. hcd->regs = (void __iomem *)pdev->resource[0].start;
  338. irq = platform_get_irq(pdev, 0);
  339. if (irq < 0) {
  340. ret = -ENXIO;
  341. goto out4;
  342. }
  343. pnx4008_start_hc();
  344. platform_set_drvdata(pdev, hcd);
  345. ohci = hcd_to_ohci(hcd);
  346. ohci_hcd_init(ohci);
  347. dev_info(&pdev->dev, "at 0x%p, irq %d\n", hcd->regs, hcd->irq);
  348. ret = usb_add_hcd(hcd, irq, SA_INTERRUPT);
  349. if (ret == 0)
  350. return ret;
  351. pnx4008_stop_hc();
  352. out4:
  353. pnx4008_unset_usb_bits();
  354. usb_put_hcd(hcd);
  355. out3:
  356. clk_disable(usb_clk);
  357. out2:
  358. clk_put(usb_clk);
  359. out1:
  360. i2c_del_driver(&isp1301_driver);
  361. out:
  362. return ret;
  363. }
  364. static int usb_hcd_pnx4008_remove(struct platform_device *pdev)
  365. {
  366. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  367. usb_remove_hcd(hcd);
  368. pnx4008_stop_hc();
  369. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  370. usb_put_hcd(hcd);
  371. pnx4008_unset_usb_bits();
  372. clk_disable(usb_clk);
  373. clk_put(usb_clk);
  374. i2c_del_driver(&isp1301_driver);
  375. platform_set_drvdata(pdev, NULL);
  376. return 0;
  377. }
  378. static struct platform_driver usb_hcd_pnx4008_driver = {
  379. .driver = {
  380. .name = "usb-ohci",
  381. },
  382. .probe = usb_hcd_pnx4008_probe,
  383. .remove = usb_hcd_pnx4008_remove,
  384. };
  385. static int __init usb_hcd_pnx4008_init(void)
  386. {
  387. return platform_driver_register(&usb_hcd_pnx4008_driver);
  388. }
  389. static void __exit usb_hcd_pnx4008_cleanup(void)
  390. {
  391. return platform_driver_unregister(&usb_hcd_pnx4008_driver);
  392. }
  393. module_init(usb_hcd_pnx4008_init);
  394. module_exit(usb_hcd_pnx4008_cleanup);