ohci-at91.c 18 KB

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