ohci-at91.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. * OHCI HCD (Host Controller Driver) for USB.
  3. *
  4. * Copyright (C) 2004 SAN People (Pty) Ltd.
  5. * Copyright (C) 2005 Thibaut VARENE <varenet@parisc-linux.org>
  6. *
  7. * AT91 Bus Glue
  8. *
  9. * Based on fragments of 2.4 driver by Rick Bronson.
  10. * Based on ohci-omap.c
  11. *
  12. * This file is licenced under the GPL.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/platform_device.h>
  16. #include <mach/hardware.h>
  17. #include <asm/gpio.h>
  18. #include <mach/board.h>
  19. #include <mach/cpu.h>
  20. #ifndef CONFIG_ARCH_AT91
  21. #error "CONFIG_ARCH_AT91 must be defined."
  22. #endif
  23. /* interface and function clocks; sometimes also an AHB clock */
  24. static struct clk *iclk, *fclk, *hclk;
  25. static int clocked;
  26. extern int usb_disabled(void);
  27. /*-------------------------------------------------------------------------*/
  28. static void at91_start_clock(void)
  29. {
  30. clk_enable(hclk);
  31. clk_enable(iclk);
  32. clk_enable(fclk);
  33. clocked = 1;
  34. }
  35. static void at91_stop_clock(void)
  36. {
  37. clk_disable(fclk);
  38. clk_disable(iclk);
  39. clk_disable(hclk);
  40. clocked = 0;
  41. }
  42. static void at91_start_hc(struct platform_device *pdev)
  43. {
  44. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  45. struct ohci_regs __iomem *regs = hcd->regs;
  46. dev_dbg(&pdev->dev, "start\n");
  47. /*
  48. * Start the USB clocks.
  49. */
  50. at91_start_clock();
  51. /*
  52. * The USB host controller must remain in reset.
  53. */
  54. writel(0, &regs->control);
  55. }
  56. static void at91_stop_hc(struct platform_device *pdev)
  57. {
  58. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  59. struct ohci_regs __iomem *regs = hcd->regs;
  60. dev_dbg(&pdev->dev, "stop\n");
  61. /*
  62. * Put the USB host controller into reset.
  63. */
  64. writel(0, &regs->control);
  65. /*
  66. * Stop the USB clocks.
  67. */
  68. at91_stop_clock();
  69. }
  70. /*-------------------------------------------------------------------------*/
  71. static void usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
  72. /* configure so an HC device and id are always provided */
  73. /* always called with process context; sleeping is OK */
  74. /**
  75. * usb_hcd_at91_probe - initialize AT91-based HCDs
  76. * Context: !in_interrupt()
  77. *
  78. * Allocates basic resources for this USB host controller, and
  79. * then invokes the start() method for the HCD associated with it
  80. * through the hotplug entry's driver_data.
  81. */
  82. static int usb_hcd_at91_probe(const struct hc_driver *driver,
  83. struct platform_device *pdev)
  84. {
  85. int retval;
  86. struct usb_hcd *hcd = NULL;
  87. if (pdev->num_resources != 2) {
  88. pr_debug("hcd probe: invalid num_resources");
  89. return -ENODEV;
  90. }
  91. if ((pdev->resource[0].flags != IORESOURCE_MEM)
  92. || (pdev->resource[1].flags != IORESOURCE_IRQ)) {
  93. pr_debug("hcd probe: invalid resource type\n");
  94. return -ENODEV;
  95. }
  96. hcd = usb_create_hcd(driver, &pdev->dev, "at91");
  97. if (!hcd)
  98. return -ENOMEM;
  99. hcd->rsrc_start = pdev->resource[0].start;
  100. hcd->rsrc_len = pdev->resource[0].end - pdev->resource[0].start + 1;
  101. if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
  102. pr_debug("request_mem_region failed\n");
  103. retval = -EBUSY;
  104. goto err1;
  105. }
  106. hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
  107. if (!hcd->regs) {
  108. pr_debug("ioremap failed\n");
  109. retval = -EIO;
  110. goto err2;
  111. }
  112. iclk = clk_get(&pdev->dev, "ohci_clk");
  113. if (IS_ERR(iclk)) {
  114. dev_err(&pdev->dev, "failed to get ohci_clk\n");
  115. retval = PTR_ERR(iclk);
  116. goto err3;
  117. }
  118. fclk = clk_get(&pdev->dev, "uhpck");
  119. if (IS_ERR(fclk)) {
  120. dev_err(&pdev->dev, "failed to get uhpck\n");
  121. retval = PTR_ERR(fclk);
  122. goto err4;
  123. }
  124. hclk = clk_get(&pdev->dev, "hclk");
  125. if (IS_ERR(hclk)) {
  126. dev_err(&pdev->dev, "failed to get hclk\n");
  127. retval = PTR_ERR(hclk);
  128. goto err5;
  129. }
  130. at91_start_hc(pdev);
  131. ohci_hcd_init(hcd_to_ohci(hcd));
  132. retval = usb_add_hcd(hcd, pdev->resource[1].start, IRQF_SHARED);
  133. if (retval == 0)
  134. return retval;
  135. /* Error handling */
  136. at91_stop_hc(pdev);
  137. clk_put(hclk);
  138. err5:
  139. clk_put(fclk);
  140. err4:
  141. clk_put(iclk);
  142. err3:
  143. iounmap(hcd->regs);
  144. err2:
  145. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  146. err1:
  147. usb_put_hcd(hcd);
  148. return retval;
  149. }
  150. /* may be called with controller, bus, and devices active */
  151. /**
  152. * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
  153. * @dev: USB Host Controller being removed
  154. * Context: !in_interrupt()
  155. *
  156. * Reverses the effect of usb_hcd_at91_probe(), first invoking
  157. * the HCD's stop() method. It is always called from a thread
  158. * context, "rmmod" or something similar.
  159. *
  160. */
  161. static void usb_hcd_at91_remove(struct usb_hcd *hcd,
  162. struct platform_device *pdev)
  163. {
  164. usb_remove_hcd(hcd);
  165. at91_stop_hc(pdev);
  166. iounmap(hcd->regs);
  167. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  168. usb_put_hcd(hcd);
  169. clk_put(hclk);
  170. clk_put(fclk);
  171. clk_put(iclk);
  172. fclk = iclk = hclk = NULL;
  173. dev_set_drvdata(&pdev->dev, NULL);
  174. }
  175. /*-------------------------------------------------------------------------*/
  176. static int __devinit
  177. ohci_at91_start (struct usb_hcd *hcd)
  178. {
  179. struct at91_usbh_data *board = hcd->self.controller->platform_data;
  180. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  181. int ret;
  182. if ((ret = ohci_init(ohci)) < 0)
  183. return ret;
  184. ohci->num_ports = board->ports;
  185. if ((ret = ohci_run(ohci)) < 0) {
  186. err("can't start %s", hcd->self.bus_name);
  187. ohci_stop(hcd);
  188. return ret;
  189. }
  190. return 0;
  191. }
  192. static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
  193. {
  194. if (port < 0 || port >= 2)
  195. return;
  196. if (!gpio_is_valid(pdata->vbus_pin[port]))
  197. return;
  198. gpio_set_value(pdata->vbus_pin[port],
  199. !pdata->vbus_pin_active_low[port] ^ enable);
  200. }
  201. static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
  202. {
  203. if (port < 0 || port >= 2)
  204. return -EINVAL;
  205. if (!gpio_is_valid(pdata->vbus_pin[port]))
  206. return -EINVAL;
  207. return gpio_get_value(pdata->vbus_pin[port]) ^
  208. !pdata->vbus_pin_active_low[port];
  209. }
  210. /*
  211. * Update the status data from the hub with the over-current indicator change.
  212. */
  213. static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
  214. {
  215. struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
  216. int length = ohci_hub_status_data(hcd, buf);
  217. int port;
  218. for (port = 0; port < ARRAY_SIZE(pdata->overcurrent_pin); port++) {
  219. if (pdata->overcurrent_changed[port]) {
  220. if (! length)
  221. length = 1;
  222. buf[0] |= 1 << (port + 1);
  223. }
  224. }
  225. return length;
  226. }
  227. /*
  228. * Look at the control requests to the root hub and see if we need to override.
  229. */
  230. static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  231. u16 wIndex, char *buf, u16 wLength)
  232. {
  233. struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
  234. struct usb_hub_descriptor *desc;
  235. int ret = -EINVAL;
  236. u32 *data = (u32 *)buf;
  237. dev_dbg(hcd->self.controller,
  238. "ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
  239. hcd, typeReq, wValue, wIndex, buf, wLength);
  240. switch (typeReq) {
  241. case SetPortFeature:
  242. if (wValue == USB_PORT_FEAT_POWER) {
  243. dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
  244. ohci_at91_usb_set_power(pdata, wIndex - 1, 1);
  245. goto out;
  246. }
  247. break;
  248. case ClearPortFeature:
  249. switch (wValue) {
  250. case USB_PORT_FEAT_C_OVER_CURRENT:
  251. dev_dbg(hcd->self.controller,
  252. "ClearPortFeature: C_OVER_CURRENT\n");
  253. if (wIndex == 1 || wIndex == 2) {
  254. pdata->overcurrent_changed[wIndex-1] = 0;
  255. pdata->overcurrent_status[wIndex-1] = 0;
  256. }
  257. goto out;
  258. case USB_PORT_FEAT_OVER_CURRENT:
  259. dev_dbg(hcd->self.controller,
  260. "ClearPortFeature: OVER_CURRENT\n");
  261. if (wIndex == 1 || wIndex == 2) {
  262. pdata->overcurrent_status[wIndex-1] = 0;
  263. }
  264. goto out;
  265. case USB_PORT_FEAT_POWER:
  266. dev_dbg(hcd->self.controller,
  267. "ClearPortFeature: POWER\n");
  268. if (wIndex == 1 || wIndex == 2) {
  269. ohci_at91_usb_set_power(pdata, wIndex - 1, 0);
  270. return 0;
  271. }
  272. }
  273. break;
  274. }
  275. ret = ohci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
  276. if (ret)
  277. goto out;
  278. switch (typeReq) {
  279. case GetHubDescriptor:
  280. /* update the hub's descriptor */
  281. desc = (struct usb_hub_descriptor *)buf;
  282. dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
  283. desc->wHubCharacteristics);
  284. /* remove the old configurations for power-switching, and
  285. * over-current protection, and insert our new configuration
  286. */
  287. desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
  288. desc->wHubCharacteristics |= cpu_to_le16(0x0001);
  289. if (pdata->overcurrent_supported) {
  290. desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
  291. desc->wHubCharacteristics |= cpu_to_le16(0x0008|0x0001);
  292. }
  293. dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
  294. desc->wHubCharacteristics);
  295. return ret;
  296. case GetPortStatus:
  297. /* check port status */
  298. dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
  299. if (wIndex == 1 || wIndex == 2) {
  300. if (! ohci_at91_usb_get_power(pdata, wIndex-1)) {
  301. *data &= ~cpu_to_le32(RH_PS_PPS);
  302. }
  303. if (pdata->overcurrent_changed[wIndex-1]) {
  304. *data |= cpu_to_le32(RH_PS_OCIC);
  305. }
  306. if (pdata->overcurrent_status[wIndex-1]) {
  307. *data |= cpu_to_le32(RH_PS_POCI);
  308. }
  309. }
  310. }
  311. out:
  312. return ret;
  313. }
  314. /*-------------------------------------------------------------------------*/
  315. static const struct hc_driver ohci_at91_hc_driver = {
  316. .description = hcd_name,
  317. .product_desc = "AT91 OHCI",
  318. .hcd_priv_size = sizeof(struct ohci_hcd),
  319. /*
  320. * generic hardware linkage
  321. */
  322. .irq = ohci_irq,
  323. .flags = HCD_USB11 | HCD_MEMORY,
  324. /*
  325. * basic lifecycle operations
  326. */
  327. .start = ohci_at91_start,
  328. .stop = ohci_stop,
  329. .shutdown = ohci_shutdown,
  330. /*
  331. * managing i/o requests and associated device resources
  332. */
  333. .urb_enqueue = ohci_urb_enqueue,
  334. .urb_dequeue = ohci_urb_dequeue,
  335. .endpoint_disable = ohci_endpoint_disable,
  336. /*
  337. * scheduling support
  338. */
  339. .get_frame_number = ohci_get_frame,
  340. /*
  341. * root hub support
  342. */
  343. .hub_status_data = ohci_at91_hub_status_data,
  344. .hub_control = ohci_at91_hub_control,
  345. #ifdef CONFIG_PM
  346. .bus_suspend = ohci_bus_suspend,
  347. .bus_resume = ohci_bus_resume,
  348. #endif
  349. .start_port_reset = ohci_start_port_reset,
  350. };
  351. /*-------------------------------------------------------------------------*/
  352. static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
  353. {
  354. struct platform_device *pdev = data;
  355. struct at91_usbh_data *pdata = pdev->dev.platform_data;
  356. int val, gpio, port;
  357. /* From the GPIO notifying the over-current situation, find
  358. * out the corresponding port */
  359. gpio = irq_to_gpio(irq);
  360. for (port = 0; port < ARRAY_SIZE(pdata->overcurrent_pin); port++) {
  361. if (pdata->overcurrent_pin[port] == gpio)
  362. break;
  363. }
  364. if (port == ARRAY_SIZE(pdata->overcurrent_pin)) {
  365. dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
  366. return IRQ_HANDLED;
  367. }
  368. val = gpio_get_value(gpio);
  369. /* When notified of an over-current situation, disable power
  370. on the corresponding port, and mark this port in
  371. over-current. */
  372. if (! val) {
  373. ohci_at91_usb_set_power(pdata, port, 0);
  374. pdata->overcurrent_status[port] = 1;
  375. pdata->overcurrent_changed[port] = 1;
  376. }
  377. dev_dbg(& pdev->dev, "overcurrent situation %s\n",
  378. val ? "exited" : "notified");
  379. return IRQ_HANDLED;
  380. }
  381. /*-------------------------------------------------------------------------*/
  382. static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
  383. {
  384. struct at91_usbh_data *pdata = pdev->dev.platform_data;
  385. int i;
  386. if (pdata) {
  387. for (i = 0; i < ARRAY_SIZE(pdata->vbus_pin); i++) {
  388. if (!gpio_is_valid(pdata->vbus_pin[i]))
  389. continue;
  390. gpio_request(pdata->vbus_pin[i], "ohci_vbus");
  391. ohci_at91_usb_set_power(pdata, i, 1);
  392. }
  393. for (i = 0; i < ARRAY_SIZE(pdata->overcurrent_pin); i++) {
  394. int ret;
  395. if (!gpio_is_valid(pdata->overcurrent_pin[i]))
  396. continue;
  397. gpio_request(pdata->overcurrent_pin[i], "ohci_overcurrent");
  398. ret = request_irq(gpio_to_irq(pdata->overcurrent_pin[i]),
  399. ohci_hcd_at91_overcurrent_irq,
  400. IRQF_SHARED, "ohci_overcurrent", pdev);
  401. if (ret) {
  402. gpio_free(pdata->overcurrent_pin[i]);
  403. dev_warn(& pdev->dev, "cannot get GPIO IRQ for overcurrent\n");
  404. }
  405. }
  406. }
  407. device_init_wakeup(&pdev->dev, 1);
  408. return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
  409. }
  410. static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
  411. {
  412. struct at91_usbh_data *pdata = pdev->dev.platform_data;
  413. int i;
  414. if (pdata) {
  415. for (i = 0; i < ARRAY_SIZE(pdata->vbus_pin); i++) {
  416. if (!gpio_is_valid(pdata->vbus_pin[i]))
  417. continue;
  418. ohci_at91_usb_set_power(pdata, i, 0);
  419. gpio_free(pdata->vbus_pin[i]);
  420. }
  421. for (i = 0; i < ARRAY_SIZE(pdata->overcurrent_pin); i++) {
  422. if (!gpio_is_valid(pdata->overcurrent_pin[i]))
  423. continue;
  424. free_irq(gpio_to_irq(pdata->overcurrent_pin[i]), pdev);
  425. gpio_free(pdata->overcurrent_pin[i]);
  426. }
  427. }
  428. device_init_wakeup(&pdev->dev, 0);
  429. usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
  430. return 0;
  431. }
  432. #ifdef CONFIG_PM
  433. static int
  434. ohci_hcd_at91_drv_suspend(struct platform_device *pdev, pm_message_t mesg)
  435. {
  436. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  437. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  438. if (device_may_wakeup(&pdev->dev))
  439. enable_irq_wake(hcd->irq);
  440. /*
  441. * The integrated transceivers seem unable to notice disconnect,
  442. * reconnect, or wakeup without the 48 MHz clock active. so for
  443. * correctness, always discard connection state (using reset).
  444. *
  445. * REVISIT: some boards will be able to turn VBUS off...
  446. */
  447. if (at91_suspend_entering_slow_clock()) {
  448. ohci_usb_reset (ohci);
  449. /* flush the writes */
  450. (void) ohci_readl (ohci, &ohci->regs->control);
  451. at91_stop_clock();
  452. }
  453. return 0;
  454. }
  455. static int ohci_hcd_at91_drv_resume(struct platform_device *pdev)
  456. {
  457. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  458. if (device_may_wakeup(&pdev->dev))
  459. disable_irq_wake(hcd->irq);
  460. if (!clocked)
  461. at91_start_clock();
  462. ohci_finish_controller_resume(hcd);
  463. return 0;
  464. }
  465. #else
  466. #define ohci_hcd_at91_drv_suspend NULL
  467. #define ohci_hcd_at91_drv_resume NULL
  468. #endif
  469. MODULE_ALIAS("platform:at91_ohci");
  470. static struct platform_driver ohci_hcd_at91_driver = {
  471. .probe = ohci_hcd_at91_drv_probe,
  472. .remove = ohci_hcd_at91_drv_remove,
  473. .shutdown = usb_hcd_platform_shutdown,
  474. .suspend = ohci_hcd_at91_drv_suspend,
  475. .resume = ohci_hcd_at91_drv_resume,
  476. .driver = {
  477. .name = "at91_ohci",
  478. .owner = THIS_MODULE,
  479. },
  480. };