ohci-at91.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. // SPDX-License-Identifier: GPL-1.0+
  2. /*
  3. * OHCI HCD (Host Controller Driver) for USB.
  4. *
  5. * Copyright (C) 2004 SAN People (Pty) Ltd.
  6. * Copyright (C) 2005 Thibaut VARENE <[email protected]>
  7. *
  8. * AT91 Bus Glue
  9. *
  10. * Based on fragments of 2.4 driver by Rick Bronson.
  11. * Based on ohci-omap.c
  12. *
  13. * This file is licenced under the GPL.
  14. */
  15. #include <linux/arm-smccc.h>
  16. #include <linux/clk.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/platform_data/atmel.h>
  22. #include <linux/io.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/mfd/syscon.h>
  26. #include <linux/regmap.h>
  27. #include <linux/usb.h>
  28. #include <linux/usb/hcd.h>
  29. #include <soc/at91/atmel-sfr.h>
  30. #include "ohci.h"
  31. #define valid_port(index) ((index) >= 0 && (index) < AT91_MAX_USBH_PORTS)
  32. #define at91_for_each_port(index) \
  33. for ((index) = 0; (index) < AT91_MAX_USBH_PORTS; (index)++)
  34. /* interface, function and usb clocks; sometimes also an AHB clock */
  35. #define hcd_to_ohci_at91_priv(h) \
  36. ((struct ohci_at91_priv *)hcd_to_ohci(h)->priv)
  37. #define AT91_MAX_USBH_PORTS 3
  38. struct at91_usbh_data {
  39. struct gpio_desc *vbus_pin[AT91_MAX_USBH_PORTS];
  40. struct gpio_desc *overcurrent_pin[AT91_MAX_USBH_PORTS];
  41. u8 ports; /* number of ports on root hub */
  42. u8 overcurrent_supported;
  43. u8 overcurrent_status[AT91_MAX_USBH_PORTS];
  44. u8 overcurrent_changed[AT91_MAX_USBH_PORTS];
  45. };
  46. struct ohci_at91_priv {
  47. struct clk *iclk;
  48. struct clk *fclk;
  49. struct clk *hclk;
  50. bool clocked;
  51. bool wakeup; /* Saved wake-up state for resume */
  52. struct regmap *sfr_regmap;
  53. u32 suspend_smc_id;
  54. };
  55. /* interface and function clocks; sometimes also an AHB clock */
  56. #define DRIVER_DESC "OHCI Atmel driver"
  57. static struct hc_driver __read_mostly ohci_at91_hc_driver;
  58. static const struct ohci_driver_overrides ohci_at91_drv_overrides __initconst = {
  59. .extra_priv_size = sizeof(struct ohci_at91_priv),
  60. };
  61. /*-------------------------------------------------------------------------*/
  62. static void at91_start_clock(struct ohci_at91_priv *ohci_at91)
  63. {
  64. if (ohci_at91->clocked)
  65. return;
  66. clk_set_rate(ohci_at91->fclk, 48000000);
  67. clk_prepare_enable(ohci_at91->hclk);
  68. clk_prepare_enable(ohci_at91->iclk);
  69. clk_prepare_enable(ohci_at91->fclk);
  70. ohci_at91->clocked = true;
  71. }
  72. static void at91_stop_clock(struct ohci_at91_priv *ohci_at91)
  73. {
  74. if (!ohci_at91->clocked)
  75. return;
  76. clk_disable_unprepare(ohci_at91->fclk);
  77. clk_disable_unprepare(ohci_at91->iclk);
  78. clk_disable_unprepare(ohci_at91->hclk);
  79. ohci_at91->clocked = false;
  80. }
  81. static void at91_start_hc(struct platform_device *pdev)
  82. {
  83. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  84. struct ohci_regs __iomem *regs = hcd->regs;
  85. struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
  86. dev_dbg(&pdev->dev, "start\n");
  87. /*
  88. * Start the USB clocks.
  89. */
  90. at91_start_clock(ohci_at91);
  91. /*
  92. * The USB host controller must remain in reset.
  93. */
  94. writel(0, &regs->control);
  95. }
  96. static void at91_stop_hc(struct platform_device *pdev)
  97. {
  98. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  99. struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
  100. dev_dbg(&pdev->dev, "stop\n");
  101. /*
  102. * Put the USB host controller into reset.
  103. */
  104. usb_hcd_platform_shutdown(pdev);
  105. /*
  106. * Stop the USB clocks.
  107. */
  108. at91_stop_clock(ohci_at91);
  109. }
  110. /*-------------------------------------------------------------------------*/
  111. static void usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
  112. static u32 at91_dt_suspend_smc(struct device *dev)
  113. {
  114. u32 suspend_smc_id;
  115. if (!dev->of_node)
  116. return 0;
  117. if (of_property_read_u32(dev->of_node, "microchip,suspend-smc-id", &suspend_smc_id))
  118. return 0;
  119. return suspend_smc_id;
  120. }
  121. static struct regmap *at91_dt_syscon_sfr(void)
  122. {
  123. struct regmap *regmap;
  124. regmap = syscon_regmap_lookup_by_compatible("atmel,sama5d2-sfr");
  125. if (IS_ERR(regmap)) {
  126. regmap = syscon_regmap_lookup_by_compatible("microchip,sam9x60-sfr");
  127. if (IS_ERR(regmap))
  128. regmap = NULL;
  129. }
  130. return regmap;
  131. }
  132. /* configure so an HC device and id are always provided */
  133. /* always called with process context; sleeping is OK */
  134. /*
  135. * usb_hcd_at91_probe - initialize AT91-based HCDs
  136. * @driver: Pointer to hc driver instance
  137. * @pdev: USB controller to probe
  138. *
  139. * Context: task context, might sleep
  140. *
  141. * Allocates basic resources for this USB host controller, and
  142. * then invokes the start() method for the HCD associated with it
  143. * through the hotplug entry's driver_data.
  144. */
  145. static int usb_hcd_at91_probe(const struct hc_driver *driver,
  146. struct platform_device *pdev)
  147. {
  148. struct at91_usbh_data *board;
  149. struct ohci_hcd *ohci;
  150. int retval;
  151. struct usb_hcd *hcd;
  152. struct ohci_at91_priv *ohci_at91;
  153. struct device *dev = &pdev->dev;
  154. struct resource *res;
  155. int irq;
  156. irq = platform_get_irq(pdev, 0);
  157. if (irq < 0) {
  158. dev_dbg(dev, "hcd probe: missing irq resource\n");
  159. return irq;
  160. }
  161. hcd = usb_create_hcd(driver, dev, "at91");
  162. if (!hcd)
  163. return -ENOMEM;
  164. ohci_at91 = hcd_to_ohci_at91_priv(hcd);
  165. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  166. hcd->regs = devm_ioremap_resource(dev, res);
  167. if (IS_ERR(hcd->regs)) {
  168. retval = PTR_ERR(hcd->regs);
  169. goto err;
  170. }
  171. hcd->rsrc_start = res->start;
  172. hcd->rsrc_len = resource_size(res);
  173. ohci_at91->iclk = devm_clk_get(dev, "ohci_clk");
  174. if (IS_ERR(ohci_at91->iclk)) {
  175. dev_err(dev, "failed to get ohci_clk\n");
  176. retval = PTR_ERR(ohci_at91->iclk);
  177. goto err;
  178. }
  179. ohci_at91->fclk = devm_clk_get(dev, "uhpck");
  180. if (IS_ERR(ohci_at91->fclk)) {
  181. dev_err(dev, "failed to get uhpck\n");
  182. retval = PTR_ERR(ohci_at91->fclk);
  183. goto err;
  184. }
  185. ohci_at91->hclk = devm_clk_get(dev, "hclk");
  186. if (IS_ERR(ohci_at91->hclk)) {
  187. dev_err(dev, "failed to get hclk\n");
  188. retval = PTR_ERR(ohci_at91->hclk);
  189. goto err;
  190. }
  191. ohci_at91->suspend_smc_id = at91_dt_suspend_smc(dev);
  192. if (!ohci_at91->suspend_smc_id) {
  193. dev_dbg(dev, "failed to find sfr suspend smc id, using regmap\n");
  194. ohci_at91->sfr_regmap = at91_dt_syscon_sfr();
  195. if (!ohci_at91->sfr_regmap)
  196. dev_dbg(dev, "failed to find sfr node\n");
  197. }
  198. board = hcd->self.controller->platform_data;
  199. ohci = hcd_to_ohci(hcd);
  200. ohci->num_ports = board->ports;
  201. at91_start_hc(pdev);
  202. /*
  203. * The RemoteWakeupConnected bit has to be set explicitly
  204. * before calling ohci_run. The reset value of this bit is 0.
  205. */
  206. ohci->hc_control = OHCI_CTRL_RWC;
  207. retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
  208. if (retval == 0) {
  209. device_wakeup_enable(hcd->self.controller);
  210. return retval;
  211. }
  212. /* Error handling */
  213. at91_stop_hc(pdev);
  214. err:
  215. usb_put_hcd(hcd);
  216. return retval;
  217. }
  218. /* may be called with controller, bus, and devices active */
  219. /*
  220. * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
  221. * @hcd: USB controller to remove
  222. * @pdev: Platform device required for cleanup
  223. *
  224. * Context: task context, might sleep
  225. *
  226. * Reverses the effect of usb_hcd_at91_probe(), first invoking
  227. * the HCD's stop() method. It is always called from a thread
  228. * context, "rmmod" or something similar.
  229. */
  230. static void usb_hcd_at91_remove(struct usb_hcd *hcd,
  231. struct platform_device *pdev)
  232. {
  233. usb_remove_hcd(hcd);
  234. at91_stop_hc(pdev);
  235. usb_put_hcd(hcd);
  236. }
  237. /*-------------------------------------------------------------------------*/
  238. static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
  239. {
  240. if (!valid_port(port))
  241. return;
  242. gpiod_set_value(pdata->vbus_pin[port], enable);
  243. }
  244. static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
  245. {
  246. if (!valid_port(port))
  247. return -EINVAL;
  248. return gpiod_get_value(pdata->vbus_pin[port]);
  249. }
  250. /*
  251. * Update the status data from the hub with the over-current indicator change.
  252. */
  253. static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
  254. {
  255. struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
  256. int length = ohci_hub_status_data(hcd, buf);
  257. int port;
  258. at91_for_each_port(port) {
  259. if (pdata->overcurrent_changed[port]) {
  260. if (!length)
  261. length = 1;
  262. buf[0] |= 1 << (port + 1);
  263. }
  264. }
  265. return length;
  266. }
  267. static int ohci_at91_port_suspend(struct ohci_at91_priv *ohci_at91, u8 set)
  268. {
  269. struct regmap *regmap = ohci_at91->sfr_regmap;
  270. u32 regval;
  271. int ret;
  272. if (ohci_at91->suspend_smc_id) {
  273. struct arm_smccc_res res;
  274. arm_smccc_smc(ohci_at91->suspend_smc_id, set, 0, 0, 0, 0, 0, 0, &res);
  275. if (res.a0)
  276. return -EINVAL;
  277. } else if (regmap) {
  278. ret = regmap_read(regmap, AT91_SFR_OHCIICR, &regval);
  279. if (ret)
  280. return ret;
  281. if (set)
  282. regval |= AT91_OHCIICR_USB_SUSPEND;
  283. else
  284. regval &= ~AT91_OHCIICR_USB_SUSPEND;
  285. regmap_write(regmap, AT91_SFR_OHCIICR, regval);
  286. }
  287. return 0;
  288. }
  289. /*
  290. * Look at the control requests to the root hub and see if we need to override.
  291. */
  292. static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  293. u16 wIndex, char *buf, u16 wLength)
  294. {
  295. struct at91_usbh_data *pdata = dev_get_platdata(hcd->self.controller);
  296. struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
  297. struct usb_hub_descriptor *desc;
  298. int ret = -EINVAL;
  299. u32 *data = (u32 *)buf;
  300. dev_dbg(hcd->self.controller,
  301. "ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
  302. hcd, typeReq, wValue, wIndex, buf, wLength);
  303. wIndex--;
  304. switch (typeReq) {
  305. case SetPortFeature:
  306. switch (wValue) {
  307. case USB_PORT_FEAT_POWER:
  308. dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
  309. if (valid_port(wIndex)) {
  310. ohci_at91_usb_set_power(pdata, wIndex, 1);
  311. ret = 0;
  312. }
  313. goto out;
  314. case USB_PORT_FEAT_SUSPEND:
  315. dev_dbg(hcd->self.controller, "SetPortFeat: SUSPEND\n");
  316. if (valid_port(wIndex)) {
  317. ohci_at91_port_suspend(ohci_at91, 1);
  318. return 0;
  319. }
  320. break;
  321. }
  322. break;
  323. case ClearPortFeature:
  324. switch (wValue) {
  325. case USB_PORT_FEAT_C_OVER_CURRENT:
  326. dev_dbg(hcd->self.controller,
  327. "ClearPortFeature: C_OVER_CURRENT\n");
  328. if (valid_port(wIndex)) {
  329. pdata->overcurrent_changed[wIndex] = 0;
  330. pdata->overcurrent_status[wIndex] = 0;
  331. }
  332. goto out;
  333. case USB_PORT_FEAT_OVER_CURRENT:
  334. dev_dbg(hcd->self.controller,
  335. "ClearPortFeature: OVER_CURRENT\n");
  336. if (valid_port(wIndex))
  337. pdata->overcurrent_status[wIndex] = 0;
  338. goto out;
  339. case USB_PORT_FEAT_POWER:
  340. dev_dbg(hcd->self.controller,
  341. "ClearPortFeature: POWER\n");
  342. if (valid_port(wIndex)) {
  343. ohci_at91_usb_set_power(pdata, wIndex, 0);
  344. return 0;
  345. }
  346. break;
  347. case USB_PORT_FEAT_SUSPEND:
  348. dev_dbg(hcd->self.controller, "ClearPortFeature: SUSPEND\n");
  349. if (valid_port(wIndex)) {
  350. ohci_at91_port_suspend(ohci_at91, 0);
  351. return 0;
  352. }
  353. break;
  354. }
  355. break;
  356. }
  357. ret = ohci_hub_control(hcd, typeReq, wValue, wIndex + 1, buf, wLength);
  358. if (ret)
  359. goto out;
  360. switch (typeReq) {
  361. case GetHubDescriptor:
  362. /* update the hub's descriptor */
  363. desc = (struct usb_hub_descriptor *)buf;
  364. dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
  365. desc->wHubCharacteristics);
  366. /* remove the old configurations for power-switching, and
  367. * over-current protection, and insert our new configuration
  368. */
  369. desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
  370. desc->wHubCharacteristics |=
  371. cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM);
  372. if (pdata->overcurrent_supported) {
  373. desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
  374. desc->wHubCharacteristics |=
  375. cpu_to_le16(HUB_CHAR_INDV_PORT_OCPM);
  376. }
  377. dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
  378. desc->wHubCharacteristics);
  379. return ret;
  380. case GetPortStatus:
  381. /* check port status */
  382. dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
  383. if (valid_port(wIndex)) {
  384. if (!ohci_at91_usb_get_power(pdata, wIndex))
  385. *data &= ~cpu_to_le32(RH_PS_PPS);
  386. if (pdata->overcurrent_changed[wIndex])
  387. *data |= cpu_to_le32(RH_PS_OCIC);
  388. if (pdata->overcurrent_status[wIndex])
  389. *data |= cpu_to_le32(RH_PS_POCI);
  390. }
  391. }
  392. out:
  393. return ret;
  394. }
  395. /*-------------------------------------------------------------------------*/
  396. static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
  397. {
  398. struct platform_device *pdev = data;
  399. struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
  400. int val, port;
  401. /* From the GPIO notifying the over-current situation, find
  402. * out the corresponding port */
  403. at91_for_each_port(port) {
  404. if (gpiod_to_irq(pdata->overcurrent_pin[port]) == irq)
  405. break;
  406. }
  407. if (port == AT91_MAX_USBH_PORTS) {
  408. dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
  409. return IRQ_HANDLED;
  410. }
  411. val = gpiod_get_value(pdata->overcurrent_pin[port]);
  412. /* When notified of an over-current situation, disable power
  413. on the corresponding port, and mark this port in
  414. over-current. */
  415. if (!val) {
  416. ohci_at91_usb_set_power(pdata, port, 0);
  417. pdata->overcurrent_status[port] = 1;
  418. pdata->overcurrent_changed[port] = 1;
  419. }
  420. dev_dbg(& pdev->dev, "overcurrent situation %s\n",
  421. val ? "exited" : "notified");
  422. return IRQ_HANDLED;
  423. }
  424. static const struct of_device_id at91_ohci_dt_ids[] = {
  425. { .compatible = "atmel,at91rm9200-ohci" },
  426. { /* sentinel */ }
  427. };
  428. MODULE_DEVICE_TABLE(of, at91_ohci_dt_ids);
  429. /*-------------------------------------------------------------------------*/
  430. static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
  431. {
  432. struct device_node *np = pdev->dev.of_node;
  433. struct at91_usbh_data *pdata;
  434. int i;
  435. int ret;
  436. int err;
  437. u32 ports;
  438. /* Right now device-tree probed devices don't get dma_mask set.
  439. * Since shared usb code relies on it, set it here for now.
  440. * Once we have dma capability bindings this can go away.
  441. */
  442. ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  443. if (ret)
  444. return ret;
  445. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  446. if (!pdata)
  447. return -ENOMEM;
  448. pdev->dev.platform_data = pdata;
  449. if (!of_property_read_u32(np, "num-ports", &ports))
  450. pdata->ports = ports;
  451. at91_for_each_port(i) {
  452. if (i >= pdata->ports)
  453. break;
  454. pdata->vbus_pin[i] =
  455. devm_gpiod_get_index_optional(&pdev->dev, "atmel,vbus",
  456. i, GPIOD_OUT_HIGH);
  457. if (IS_ERR(pdata->vbus_pin[i])) {
  458. err = PTR_ERR(pdata->vbus_pin[i]);
  459. dev_err(&pdev->dev, "unable to claim gpio \"vbus\": %d\n", err);
  460. continue;
  461. }
  462. }
  463. at91_for_each_port(i) {
  464. if (i >= pdata->ports)
  465. break;
  466. pdata->overcurrent_pin[i] =
  467. devm_gpiod_get_index_optional(&pdev->dev, "atmel,oc",
  468. i, GPIOD_IN);
  469. if (!pdata->overcurrent_pin[i])
  470. continue;
  471. if (IS_ERR(pdata->overcurrent_pin[i])) {
  472. err = PTR_ERR(pdata->overcurrent_pin[i]);
  473. dev_err(&pdev->dev, "unable to claim gpio \"overcurrent\": %d\n", err);
  474. continue;
  475. }
  476. ret = devm_request_irq(&pdev->dev,
  477. gpiod_to_irq(pdata->overcurrent_pin[i]),
  478. ohci_hcd_at91_overcurrent_irq,
  479. IRQF_SHARED,
  480. "ohci_overcurrent", pdev);
  481. if (ret)
  482. dev_info(&pdev->dev, "failed to request gpio \"overcurrent\" IRQ\n");
  483. }
  484. device_init_wakeup(&pdev->dev, 1);
  485. return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
  486. }
  487. static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
  488. {
  489. struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
  490. int i;
  491. if (pdata) {
  492. at91_for_each_port(i)
  493. ohci_at91_usb_set_power(pdata, i, 0);
  494. }
  495. device_init_wakeup(&pdev->dev, 0);
  496. usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
  497. return 0;
  498. }
  499. static int __maybe_unused
  500. ohci_hcd_at91_drv_suspend(struct device *dev)
  501. {
  502. struct usb_hcd *hcd = dev_get_drvdata(dev);
  503. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  504. struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
  505. int ret;
  506. /*
  507. * Disable wakeup if we are going to sleep with slow clock mode
  508. * enabled.
  509. */
  510. ohci_at91->wakeup = device_may_wakeup(dev)
  511. && !at91_suspend_entering_slow_clock();
  512. if (ohci_at91->wakeup)
  513. enable_irq_wake(hcd->irq);
  514. ret = ohci_suspend(hcd, ohci_at91->wakeup);
  515. if (ret) {
  516. if (ohci_at91->wakeup)
  517. disable_irq_wake(hcd->irq);
  518. return ret;
  519. }
  520. /*
  521. * The integrated transceivers seem unable to notice disconnect,
  522. * reconnect, or wakeup without the 48 MHz clock active. so for
  523. * correctness, always discard connection state (using reset).
  524. *
  525. * REVISIT: some boards will be able to turn VBUS off...
  526. */
  527. if (!ohci_at91->wakeup) {
  528. ohci->rh_state = OHCI_RH_HALTED;
  529. /* flush the writes */
  530. (void) ohci_readl (ohci, &ohci->regs->control);
  531. msleep(1);
  532. ohci_at91_port_suspend(ohci_at91, 1);
  533. at91_stop_clock(ohci_at91);
  534. } else {
  535. ohci_at91_port_suspend(ohci_at91, 1);
  536. }
  537. return ret;
  538. }
  539. static int __maybe_unused
  540. ohci_hcd_at91_drv_resume(struct device *dev)
  541. {
  542. struct usb_hcd *hcd = dev_get_drvdata(dev);
  543. struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
  544. ohci_at91_port_suspend(ohci_at91, 0);
  545. if (ohci_at91->wakeup)
  546. disable_irq_wake(hcd->irq);
  547. else
  548. at91_start_clock(ohci_at91);
  549. /*
  550. * According to the comment in ohci_hcd_at91_drv_suspend()
  551. * we need to do a reset if the 48Mhz clock was stopped,
  552. * that is, if ohci_at91->wakeup is clear. Tell ohci_resume()
  553. * to reset in this case by setting its "hibernated" flag.
  554. */
  555. ohci_resume(hcd, !ohci_at91->wakeup);
  556. return 0;
  557. }
  558. static SIMPLE_DEV_PM_OPS(ohci_hcd_at91_pm_ops, ohci_hcd_at91_drv_suspend,
  559. ohci_hcd_at91_drv_resume);
  560. static struct platform_driver ohci_hcd_at91_driver = {
  561. .probe = ohci_hcd_at91_drv_probe,
  562. .remove = ohci_hcd_at91_drv_remove,
  563. .shutdown = usb_hcd_platform_shutdown,
  564. .driver = {
  565. .name = "at91_ohci",
  566. .pm = &ohci_hcd_at91_pm_ops,
  567. .of_match_table = at91_ohci_dt_ids,
  568. },
  569. };
  570. static int __init ohci_at91_init(void)
  571. {
  572. if (usb_disabled())
  573. return -ENODEV;
  574. ohci_init_driver(&ohci_at91_hc_driver, &ohci_at91_drv_overrides);
  575. /*
  576. * The Atmel HW has some unusual quirks, which require Atmel-specific
  577. * workarounds. We override certain hc_driver functions here to
  578. * achieve that. We explicitly do not enhance ohci_driver_overrides to
  579. * allow this more easily, since this is an unusual case, and we don't
  580. * want to encourage others to override these functions by making it
  581. * too easy.
  582. */
  583. ohci_at91_hc_driver.hub_status_data = ohci_at91_hub_status_data;
  584. ohci_at91_hc_driver.hub_control = ohci_at91_hub_control;
  585. return platform_driver_register(&ohci_hcd_at91_driver);
  586. }
  587. module_init(ohci_at91_init);
  588. static void __exit ohci_at91_cleanup(void)
  589. {
  590. platform_driver_unregister(&ohci_hcd_at91_driver);
  591. }
  592. module_exit(ohci_at91_cleanup);
  593. MODULE_DESCRIPTION(DRIVER_DESC);
  594. MODULE_LICENSE("GPL");
  595. MODULE_ALIAS("platform:at91_ohci");