ohci-at91.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. fclk = clk_get(&pdev->dev, "uhpck");
  114. hclk = clk_get(&pdev->dev, "hclk");
  115. at91_start_hc(pdev);
  116. ohci_hcd_init(hcd_to_ohci(hcd));
  117. retval = usb_add_hcd(hcd, pdev->resource[1].start, IRQF_SHARED);
  118. if (retval == 0)
  119. return retval;
  120. /* Error handling */
  121. at91_stop_hc(pdev);
  122. clk_put(hclk);
  123. clk_put(fclk);
  124. clk_put(iclk);
  125. iounmap(hcd->regs);
  126. err2:
  127. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  128. err1:
  129. usb_put_hcd(hcd);
  130. return retval;
  131. }
  132. /* may be called with controller, bus, and devices active */
  133. /**
  134. * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
  135. * @dev: USB Host Controller being removed
  136. * Context: !in_interrupt()
  137. *
  138. * Reverses the effect of usb_hcd_at91_probe(), first invoking
  139. * the HCD's stop() method. It is always called from a thread
  140. * context, "rmmod" or something similar.
  141. *
  142. */
  143. static void usb_hcd_at91_remove(struct usb_hcd *hcd,
  144. struct platform_device *pdev)
  145. {
  146. usb_remove_hcd(hcd);
  147. at91_stop_hc(pdev);
  148. iounmap(hcd->regs);
  149. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  150. usb_put_hcd(hcd);
  151. clk_put(hclk);
  152. clk_put(fclk);
  153. clk_put(iclk);
  154. fclk = iclk = hclk = NULL;
  155. dev_set_drvdata(&pdev->dev, NULL);
  156. }
  157. /*-------------------------------------------------------------------------*/
  158. static int __devinit
  159. ohci_at91_start (struct usb_hcd *hcd)
  160. {
  161. struct at91_usbh_data *board = hcd->self.controller->platform_data;
  162. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  163. int ret;
  164. if ((ret = ohci_init(ohci)) < 0)
  165. return ret;
  166. ohci->num_ports = board->ports;
  167. if ((ret = ohci_run(ohci)) < 0) {
  168. err("can't start %s", hcd->self.bus_name);
  169. ohci_stop(hcd);
  170. return ret;
  171. }
  172. return 0;
  173. }
  174. static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
  175. {
  176. if (port < 0 || port >= 2)
  177. return;
  178. if (!gpio_is_valid(pdata->vbus_pin[port]))
  179. return;
  180. gpio_set_value(pdata->vbus_pin[port], !pdata->vbus_pin_inverted ^ enable);
  181. }
  182. static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
  183. {
  184. if (port < 0 || port >= 2)
  185. return -EINVAL;
  186. if (!gpio_is_valid(pdata->vbus_pin[port]))
  187. return -EINVAL;
  188. return gpio_get_value(pdata->vbus_pin[port]) ^ !pdata->vbus_pin_inverted;
  189. }
  190. /*
  191. * Update the status data from the hub with the over-current indicator change.
  192. */
  193. static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
  194. {
  195. struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
  196. int length = ohci_hub_status_data(hcd, buf);
  197. int port;
  198. for (port = 0; port < ARRAY_SIZE(pdata->overcurrent_pin); port++) {
  199. if (pdata->overcurrent_changed[port]) {
  200. if (! length)
  201. length = 1;
  202. buf[0] |= 1 << (port + 1);
  203. }
  204. }
  205. return length;
  206. }
  207. /*
  208. * Look at the control requests to the root hub and see if we need to override.
  209. */
  210. static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  211. u16 wIndex, char *buf, u16 wLength)
  212. {
  213. struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
  214. struct usb_hub_descriptor *desc;
  215. int ret = -EINVAL;
  216. u32 *data = (u32 *)buf;
  217. dev_dbg(hcd->self.controller,
  218. "ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
  219. hcd, typeReq, wValue, wIndex, buf, wLength);
  220. switch (typeReq) {
  221. case SetPortFeature:
  222. if (wValue == USB_PORT_FEAT_POWER) {
  223. dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
  224. ohci_at91_usb_set_power(pdata, wIndex - 1, 1);
  225. goto out;
  226. }
  227. break;
  228. case ClearPortFeature:
  229. switch (wValue) {
  230. case USB_PORT_FEAT_C_OVER_CURRENT:
  231. dev_dbg(hcd->self.controller,
  232. "ClearPortFeature: C_OVER_CURRENT\n");
  233. if (wIndex == 1 || wIndex == 2) {
  234. pdata->overcurrent_changed[wIndex-1] = 0;
  235. pdata->overcurrent_status[wIndex-1] = 0;
  236. }
  237. goto out;
  238. case USB_PORT_FEAT_OVER_CURRENT:
  239. dev_dbg(hcd->self.controller,
  240. "ClearPortFeature: OVER_CURRENT\n");
  241. if (wIndex == 1 || wIndex == 2) {
  242. pdata->overcurrent_status[wIndex-1] = 0;
  243. }
  244. goto out;
  245. case USB_PORT_FEAT_POWER:
  246. dev_dbg(hcd->self.controller,
  247. "ClearPortFeature: POWER\n");
  248. if (wIndex == 1 || wIndex == 2) {
  249. ohci_at91_usb_set_power(pdata, wIndex - 1, 0);
  250. return 0;
  251. }
  252. }
  253. break;
  254. }
  255. ret = ohci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
  256. if (ret)
  257. goto out;
  258. switch (typeReq) {
  259. case GetHubDescriptor:
  260. /* update the hub's descriptor */
  261. desc = (struct usb_hub_descriptor *)buf;
  262. dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
  263. desc->wHubCharacteristics);
  264. /* remove the old configurations for power-switching, and
  265. * over-current protection, and insert our new configuration
  266. */
  267. desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
  268. desc->wHubCharacteristics |= cpu_to_le16(0x0001);
  269. if (pdata->overcurrent_supported) {
  270. desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
  271. desc->wHubCharacteristics |= cpu_to_le16(0x0008|0x0001);
  272. }
  273. dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
  274. desc->wHubCharacteristics);
  275. return ret;
  276. case GetPortStatus:
  277. /* check port status */
  278. dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
  279. if (wIndex == 1 || wIndex == 2) {
  280. if (! ohci_at91_usb_get_power(pdata, wIndex-1)) {
  281. *data &= ~cpu_to_le32(RH_PS_PPS);
  282. }
  283. if (pdata->overcurrent_changed[wIndex-1]) {
  284. *data |= cpu_to_le32(RH_PS_OCIC);
  285. }
  286. if (pdata->overcurrent_status[wIndex-1]) {
  287. *data |= cpu_to_le32(RH_PS_POCI);
  288. }
  289. }
  290. }
  291. out:
  292. return ret;
  293. }
  294. /*-------------------------------------------------------------------------*/
  295. static const struct hc_driver ohci_at91_hc_driver = {
  296. .description = hcd_name,
  297. .product_desc = "AT91 OHCI",
  298. .hcd_priv_size = sizeof(struct ohci_hcd),
  299. /*
  300. * generic hardware linkage
  301. */
  302. .irq = ohci_irq,
  303. .flags = HCD_USB11 | HCD_MEMORY,
  304. /*
  305. * basic lifecycle operations
  306. */
  307. .start = ohci_at91_start,
  308. .stop = ohci_stop,
  309. .shutdown = ohci_shutdown,
  310. /*
  311. * managing i/o requests and associated device resources
  312. */
  313. .urb_enqueue = ohci_urb_enqueue,
  314. .urb_dequeue = ohci_urb_dequeue,
  315. .endpoint_disable = ohci_endpoint_disable,
  316. /*
  317. * scheduling support
  318. */
  319. .get_frame_number = ohci_get_frame,
  320. /*
  321. * root hub support
  322. */
  323. .hub_status_data = ohci_at91_hub_status_data,
  324. .hub_control = ohci_at91_hub_control,
  325. #ifdef CONFIG_PM
  326. .bus_suspend = ohci_bus_suspend,
  327. .bus_resume = ohci_bus_resume,
  328. #endif
  329. .start_port_reset = ohci_start_port_reset,
  330. };
  331. /*-------------------------------------------------------------------------*/
  332. static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
  333. {
  334. struct platform_device *pdev = data;
  335. struct at91_usbh_data *pdata = pdev->dev.platform_data;
  336. int val, gpio, port;
  337. /* From the GPIO notifying the over-current situation, find
  338. * out the corresponding port */
  339. gpio = irq_to_gpio(irq);
  340. for (port = 0; port < ARRAY_SIZE(pdata->overcurrent_pin); port++) {
  341. if (pdata->overcurrent_pin[port] == gpio)
  342. break;
  343. }
  344. if (port == ARRAY_SIZE(pdata->overcurrent_pin)) {
  345. dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
  346. return IRQ_HANDLED;
  347. }
  348. val = gpio_get_value(gpio);
  349. /* When notified of an over-current situation, disable power
  350. on the corresponding port, and mark this port in
  351. over-current. */
  352. if (! val) {
  353. ohci_at91_usb_set_power(pdata, port, 0);
  354. pdata->overcurrent_status[port] = 1;
  355. pdata->overcurrent_changed[port] = 1;
  356. }
  357. dev_dbg(& pdev->dev, "overcurrent situation %s\n",
  358. val ? "exited" : "notified");
  359. return IRQ_HANDLED;
  360. }
  361. /*-------------------------------------------------------------------------*/
  362. static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
  363. {
  364. struct at91_usbh_data *pdata = pdev->dev.platform_data;
  365. int i;
  366. if (pdata) {
  367. for (i = 0; i < ARRAY_SIZE(pdata->vbus_pin); i++) {
  368. if (!gpio_is_valid(pdata->vbus_pin[i]))
  369. continue;
  370. gpio_request(pdata->vbus_pin[i], "ohci_vbus");
  371. ohci_at91_usb_set_power(pdata, i, 1);
  372. }
  373. for (i = 0; i < ARRAY_SIZE(pdata->overcurrent_pin); i++) {
  374. int ret;
  375. if (!gpio_is_valid(pdata->overcurrent_pin[i]))
  376. continue;
  377. gpio_request(pdata->overcurrent_pin[i], "ohci_overcurrent");
  378. ret = request_irq(gpio_to_irq(pdata->overcurrent_pin[i]),
  379. ohci_hcd_at91_overcurrent_irq,
  380. IRQF_SHARED, "ohci_overcurrent", pdev);
  381. if (ret) {
  382. gpio_free(pdata->overcurrent_pin[i]);
  383. dev_warn(& pdev->dev, "cannot get GPIO IRQ for overcurrent\n");
  384. }
  385. }
  386. }
  387. device_init_wakeup(&pdev->dev, 1);
  388. return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
  389. }
  390. static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
  391. {
  392. struct at91_usbh_data *pdata = pdev->dev.platform_data;
  393. int i;
  394. if (pdata) {
  395. for (i = 0; i < ARRAY_SIZE(pdata->vbus_pin); i++) {
  396. if (!gpio_is_valid(pdata->vbus_pin[i]))
  397. continue;
  398. ohci_at91_usb_set_power(pdata, i, 0);
  399. gpio_free(pdata->vbus_pin[i]);
  400. }
  401. for (i = 0; i < ARRAY_SIZE(pdata->overcurrent_pin); i++) {
  402. if (!gpio_is_valid(pdata->overcurrent_pin[i]))
  403. continue;
  404. free_irq(gpio_to_irq(pdata->overcurrent_pin[i]), pdev);
  405. gpio_free(pdata->overcurrent_pin[i]);
  406. }
  407. }
  408. device_init_wakeup(&pdev->dev, 0);
  409. usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
  410. return 0;
  411. }
  412. #ifdef CONFIG_PM
  413. static int
  414. ohci_hcd_at91_drv_suspend(struct platform_device *pdev, pm_message_t mesg)
  415. {
  416. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  417. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  418. if (device_may_wakeup(&pdev->dev))
  419. enable_irq_wake(hcd->irq);
  420. /*
  421. * The integrated transceivers seem unable to notice disconnect,
  422. * reconnect, or wakeup without the 48 MHz clock active. so for
  423. * correctness, always discard connection state (using reset).
  424. *
  425. * REVISIT: some boards will be able to turn VBUS off...
  426. */
  427. if (at91_suspend_entering_slow_clock()) {
  428. ohci_usb_reset (ohci);
  429. /* flush the writes */
  430. (void) ohci_readl (ohci, &ohci->regs->control);
  431. at91_stop_clock();
  432. }
  433. return 0;
  434. }
  435. static int ohci_hcd_at91_drv_resume(struct platform_device *pdev)
  436. {
  437. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  438. if (device_may_wakeup(&pdev->dev))
  439. disable_irq_wake(hcd->irq);
  440. if (!clocked)
  441. at91_start_clock();
  442. ohci_finish_controller_resume(hcd);
  443. return 0;
  444. }
  445. #else
  446. #define ohci_hcd_at91_drv_suspend NULL
  447. #define ohci_hcd_at91_drv_resume NULL
  448. #endif
  449. MODULE_ALIAS("platform:at91_ohci");
  450. static struct platform_driver ohci_hcd_at91_driver = {
  451. .probe = ohci_hcd_at91_drv_probe,
  452. .remove = ohci_hcd_at91_drv_remove,
  453. .shutdown = usb_hcd_platform_shutdown,
  454. .suspend = ohci_hcd_at91_drv_suspend,
  455. .resume = ohci_hcd_at91_drv_resume,
  456. .driver = {
  457. .name = "at91_ohci",
  458. .owner = THIS_MODULE,
  459. },
  460. };