ohci-at91.c 13 KB

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