ohci-at91.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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], !pdata->vbus_pin_inverted ^ enable);
  199. }
  200. static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
  201. {
  202. if (port < 0 || port >= 2)
  203. return -EINVAL;
  204. if (!gpio_is_valid(pdata->vbus_pin[port]))
  205. return -EINVAL;
  206. return gpio_get_value(pdata->vbus_pin[port]) ^ !pdata->vbus_pin_inverted;
  207. }
  208. /*
  209. * Update the status data from the hub with the over-current indicator change.
  210. */
  211. static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
  212. {
  213. struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
  214. int length = ohci_hub_status_data(hcd, buf);
  215. int port;
  216. for (port = 0; port < ARRAY_SIZE(pdata->overcurrent_pin); port++) {
  217. if (pdata->overcurrent_changed[port]) {
  218. if (! length)
  219. length = 1;
  220. buf[0] |= 1 << (port + 1);
  221. }
  222. }
  223. return length;
  224. }
  225. /*
  226. * Look at the control requests to the root hub and see if we need to override.
  227. */
  228. static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  229. u16 wIndex, char *buf, u16 wLength)
  230. {
  231. struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
  232. struct usb_hub_descriptor *desc;
  233. int ret = -EINVAL;
  234. u32 *data = (u32 *)buf;
  235. dev_dbg(hcd->self.controller,
  236. "ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
  237. hcd, typeReq, wValue, wIndex, buf, wLength);
  238. switch (typeReq) {
  239. case SetPortFeature:
  240. if (wValue == USB_PORT_FEAT_POWER) {
  241. dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
  242. ohci_at91_usb_set_power(pdata, wIndex - 1, 1);
  243. goto out;
  244. }
  245. break;
  246. case ClearPortFeature:
  247. switch (wValue) {
  248. case USB_PORT_FEAT_C_OVER_CURRENT:
  249. dev_dbg(hcd->self.controller,
  250. "ClearPortFeature: C_OVER_CURRENT\n");
  251. if (wIndex == 1 || wIndex == 2) {
  252. pdata->overcurrent_changed[wIndex-1] = 0;
  253. pdata->overcurrent_status[wIndex-1] = 0;
  254. }
  255. goto out;
  256. case USB_PORT_FEAT_OVER_CURRENT:
  257. dev_dbg(hcd->self.controller,
  258. "ClearPortFeature: OVER_CURRENT\n");
  259. if (wIndex == 1 || wIndex == 2) {
  260. pdata->overcurrent_status[wIndex-1] = 0;
  261. }
  262. goto out;
  263. case USB_PORT_FEAT_POWER:
  264. dev_dbg(hcd->self.controller,
  265. "ClearPortFeature: POWER\n");
  266. if (wIndex == 1 || wIndex == 2) {
  267. ohci_at91_usb_set_power(pdata, wIndex - 1, 0);
  268. return 0;
  269. }
  270. }
  271. break;
  272. }
  273. ret = ohci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
  274. if (ret)
  275. goto out;
  276. switch (typeReq) {
  277. case GetHubDescriptor:
  278. /* update the hub's descriptor */
  279. desc = (struct usb_hub_descriptor *)buf;
  280. dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
  281. desc->wHubCharacteristics);
  282. /* remove the old configurations for power-switching, and
  283. * over-current protection, and insert our new configuration
  284. */
  285. desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
  286. desc->wHubCharacteristics |= cpu_to_le16(0x0001);
  287. if (pdata->overcurrent_supported) {
  288. desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
  289. desc->wHubCharacteristics |= cpu_to_le16(0x0008|0x0001);
  290. }
  291. dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
  292. desc->wHubCharacteristics);
  293. return ret;
  294. case GetPortStatus:
  295. /* check port status */
  296. dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
  297. if (wIndex == 1 || wIndex == 2) {
  298. if (! ohci_at91_usb_get_power(pdata, wIndex-1)) {
  299. *data &= ~cpu_to_le32(RH_PS_PPS);
  300. }
  301. if (pdata->overcurrent_changed[wIndex-1]) {
  302. *data |= cpu_to_le32(RH_PS_OCIC);
  303. }
  304. if (pdata->overcurrent_status[wIndex-1]) {
  305. *data |= cpu_to_le32(RH_PS_POCI);
  306. }
  307. }
  308. }
  309. out:
  310. return ret;
  311. }
  312. /*-------------------------------------------------------------------------*/
  313. static const struct hc_driver ohci_at91_hc_driver = {
  314. .description = hcd_name,
  315. .product_desc = "AT91 OHCI",
  316. .hcd_priv_size = sizeof(struct ohci_hcd),
  317. /*
  318. * generic hardware linkage
  319. */
  320. .irq = ohci_irq,
  321. .flags = HCD_USB11 | HCD_MEMORY,
  322. /*
  323. * basic lifecycle operations
  324. */
  325. .start = ohci_at91_start,
  326. .stop = ohci_stop,
  327. .shutdown = ohci_shutdown,
  328. /*
  329. * managing i/o requests and associated device resources
  330. */
  331. .urb_enqueue = ohci_urb_enqueue,
  332. .urb_dequeue = ohci_urb_dequeue,
  333. .endpoint_disable = ohci_endpoint_disable,
  334. /*
  335. * scheduling support
  336. */
  337. .get_frame_number = ohci_get_frame,
  338. /*
  339. * root hub support
  340. */
  341. .hub_status_data = ohci_at91_hub_status_data,
  342. .hub_control = ohci_at91_hub_control,
  343. #ifdef CONFIG_PM
  344. .bus_suspend = ohci_bus_suspend,
  345. .bus_resume = ohci_bus_resume,
  346. #endif
  347. .start_port_reset = ohci_start_port_reset,
  348. };
  349. /*-------------------------------------------------------------------------*/
  350. static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
  351. {
  352. struct platform_device *pdev = data;
  353. struct at91_usbh_data *pdata = pdev->dev.platform_data;
  354. int val, gpio, port;
  355. /* From the GPIO notifying the over-current situation, find
  356. * out the corresponding port */
  357. gpio = irq_to_gpio(irq);
  358. for (port = 0; port < ARRAY_SIZE(pdata->overcurrent_pin); port++) {
  359. if (pdata->overcurrent_pin[port] == gpio)
  360. break;
  361. }
  362. if (port == ARRAY_SIZE(pdata->overcurrent_pin)) {
  363. dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
  364. return IRQ_HANDLED;
  365. }
  366. val = gpio_get_value(gpio);
  367. /* When notified of an over-current situation, disable power
  368. on the corresponding port, and mark this port in
  369. over-current. */
  370. if (! val) {
  371. ohci_at91_usb_set_power(pdata, port, 0);
  372. pdata->overcurrent_status[port] = 1;
  373. pdata->overcurrent_changed[port] = 1;
  374. }
  375. dev_dbg(& pdev->dev, "overcurrent situation %s\n",
  376. val ? "exited" : "notified");
  377. return IRQ_HANDLED;
  378. }
  379. /*-------------------------------------------------------------------------*/
  380. static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
  381. {
  382. struct at91_usbh_data *pdata = pdev->dev.platform_data;
  383. int i;
  384. if (pdata) {
  385. for (i = 0; i < ARRAY_SIZE(pdata->vbus_pin); i++) {
  386. if (!gpio_is_valid(pdata->vbus_pin[i]))
  387. continue;
  388. gpio_request(pdata->vbus_pin[i], "ohci_vbus");
  389. ohci_at91_usb_set_power(pdata, i, 1);
  390. }
  391. for (i = 0; i < ARRAY_SIZE(pdata->overcurrent_pin); i++) {
  392. int ret;
  393. if (!gpio_is_valid(pdata->overcurrent_pin[i]))
  394. continue;
  395. gpio_request(pdata->overcurrent_pin[i], "ohci_overcurrent");
  396. ret = request_irq(gpio_to_irq(pdata->overcurrent_pin[i]),
  397. ohci_hcd_at91_overcurrent_irq,
  398. IRQF_SHARED, "ohci_overcurrent", pdev);
  399. if (ret) {
  400. gpio_free(pdata->overcurrent_pin[i]);
  401. dev_warn(& pdev->dev, "cannot get GPIO IRQ for overcurrent\n");
  402. }
  403. }
  404. }
  405. device_init_wakeup(&pdev->dev, 1);
  406. return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
  407. }
  408. static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
  409. {
  410. struct at91_usbh_data *pdata = pdev->dev.platform_data;
  411. int i;
  412. if (pdata) {
  413. for (i = 0; i < ARRAY_SIZE(pdata->vbus_pin); i++) {
  414. if (!gpio_is_valid(pdata->vbus_pin[i]))
  415. continue;
  416. ohci_at91_usb_set_power(pdata, i, 0);
  417. gpio_free(pdata->vbus_pin[i]);
  418. }
  419. for (i = 0; i < ARRAY_SIZE(pdata->overcurrent_pin); i++) {
  420. if (!gpio_is_valid(pdata->overcurrent_pin[i]))
  421. continue;
  422. free_irq(gpio_to_irq(pdata->overcurrent_pin[i]), pdev);
  423. gpio_free(pdata->overcurrent_pin[i]);
  424. }
  425. }
  426. device_init_wakeup(&pdev->dev, 0);
  427. usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
  428. return 0;
  429. }
  430. #ifdef CONFIG_PM
  431. static int
  432. ohci_hcd_at91_drv_suspend(struct platform_device *pdev, pm_message_t mesg)
  433. {
  434. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  435. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  436. if (device_may_wakeup(&pdev->dev))
  437. enable_irq_wake(hcd->irq);
  438. /*
  439. * The integrated transceivers seem unable to notice disconnect,
  440. * reconnect, or wakeup without the 48 MHz clock active. so for
  441. * correctness, always discard connection state (using reset).
  442. *
  443. * REVISIT: some boards will be able to turn VBUS off...
  444. */
  445. if (at91_suspend_entering_slow_clock()) {
  446. ohci_usb_reset (ohci);
  447. /* flush the writes */
  448. (void) ohci_readl (ohci, &ohci->regs->control);
  449. at91_stop_clock();
  450. }
  451. return 0;
  452. }
  453. static int ohci_hcd_at91_drv_resume(struct platform_device *pdev)
  454. {
  455. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  456. if (device_may_wakeup(&pdev->dev))
  457. disable_irq_wake(hcd->irq);
  458. if (!clocked)
  459. at91_start_clock();
  460. ohci_finish_controller_resume(hcd);
  461. return 0;
  462. }
  463. #else
  464. #define ohci_hcd_at91_drv_suspend NULL
  465. #define ohci_hcd_at91_drv_resume NULL
  466. #endif
  467. MODULE_ALIAS("platform:at91_ohci");
  468. static struct platform_driver ohci_hcd_at91_driver = {
  469. .probe = ohci_hcd_at91_drv_probe,
  470. .remove = ohci_hcd_at91_drv_remove,
  471. .shutdown = usb_hcd_platform_shutdown,
  472. .suspend = ohci_hcd_at91_drv_suspend,
  473. .resume = ohci_hcd_at91_drv_resume,
  474. .driver = {
  475. .name = "at91_ohci",
  476. .owner = THIS_MODULE,
  477. },
  478. };