core.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /**
  2. * core.c - DesignWare USB3 DRD Controller Core file
  3. *
  4. * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
  5. *
  6. * Authors: Felipe Balbi <balbi@ti.com>,
  7. * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 of
  11. * the License as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/slab.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/ioport.h>
  29. #include <linux/io.h>
  30. #include <linux/list.h>
  31. #include <linux/delay.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/of.h>
  34. #include <linux/usb/otg.h>
  35. #include <linux/usb/ch9.h>
  36. #include <linux/usb/gadget.h>
  37. #include <linux/usb/of.h>
  38. #include <linux/usb/otg.h>
  39. #include "platform_data.h"
  40. #include "core.h"
  41. #include "gadget.h"
  42. #include "io.h"
  43. #include "debug.h"
  44. /* -------------------------------------------------------------------------- */
  45. void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
  46. {
  47. u32 reg;
  48. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  49. reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
  50. reg |= DWC3_GCTL_PRTCAPDIR(mode);
  51. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  52. }
  53. /**
  54. * dwc3_core_soft_reset - Issues core soft reset and PHY reset
  55. * @dwc: pointer to our context structure
  56. */
  57. static void dwc3_core_soft_reset(struct dwc3 *dwc)
  58. {
  59. u32 reg;
  60. /* Before Resetting PHY, put Core in Reset */
  61. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  62. reg |= DWC3_GCTL_CORESOFTRESET;
  63. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  64. /* Assert USB3 PHY reset */
  65. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  66. reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
  67. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  68. /* Assert USB2 PHY reset */
  69. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  70. reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
  71. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  72. usb_phy_init(dwc->usb2_phy);
  73. usb_phy_init(dwc->usb3_phy);
  74. mdelay(100);
  75. /* Clear USB3 PHY reset */
  76. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  77. reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
  78. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  79. /* Clear USB2 PHY reset */
  80. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  81. reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
  82. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  83. mdelay(100);
  84. /* After PHYs are stable we can take Core out of reset state */
  85. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  86. reg &= ~DWC3_GCTL_CORESOFTRESET;
  87. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  88. }
  89. /**
  90. * dwc3_free_one_event_buffer - Frees one event buffer
  91. * @dwc: Pointer to our controller context structure
  92. * @evt: Pointer to event buffer to be freed
  93. */
  94. static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
  95. struct dwc3_event_buffer *evt)
  96. {
  97. dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
  98. }
  99. /**
  100. * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
  101. * @dwc: Pointer to our controller context structure
  102. * @length: size of the event buffer
  103. *
  104. * Returns a pointer to the allocated event buffer structure on success
  105. * otherwise ERR_PTR(errno).
  106. */
  107. static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
  108. unsigned length)
  109. {
  110. struct dwc3_event_buffer *evt;
  111. evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
  112. if (!evt)
  113. return ERR_PTR(-ENOMEM);
  114. evt->dwc = dwc;
  115. evt->length = length;
  116. evt->buf = dma_alloc_coherent(dwc->dev, length,
  117. &evt->dma, GFP_KERNEL);
  118. if (!evt->buf)
  119. return ERR_PTR(-ENOMEM);
  120. return evt;
  121. }
  122. /**
  123. * dwc3_free_event_buffers - frees all allocated event buffers
  124. * @dwc: Pointer to our controller context structure
  125. */
  126. static void dwc3_free_event_buffers(struct dwc3 *dwc)
  127. {
  128. struct dwc3_event_buffer *evt;
  129. int i;
  130. for (i = 0; i < dwc->num_event_buffers; i++) {
  131. evt = dwc->ev_buffs[i];
  132. if (evt)
  133. dwc3_free_one_event_buffer(dwc, evt);
  134. }
  135. }
  136. /**
  137. * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
  138. * @dwc: pointer to our controller context structure
  139. * @length: size of event buffer
  140. *
  141. * Returns 0 on success otherwise negative errno. In the error case, dwc
  142. * may contain some buffers allocated but not all which were requested.
  143. */
  144. static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
  145. {
  146. int num;
  147. int i;
  148. num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
  149. dwc->num_event_buffers = num;
  150. dwc->ev_buffs = devm_kzalloc(dwc->dev, sizeof(*dwc->ev_buffs) * num,
  151. GFP_KERNEL);
  152. if (!dwc->ev_buffs) {
  153. dev_err(dwc->dev, "can't allocate event buffers array\n");
  154. return -ENOMEM;
  155. }
  156. for (i = 0; i < num; i++) {
  157. struct dwc3_event_buffer *evt;
  158. evt = dwc3_alloc_one_event_buffer(dwc, length);
  159. if (IS_ERR(evt)) {
  160. dev_err(dwc->dev, "can't allocate event buffer\n");
  161. return PTR_ERR(evt);
  162. }
  163. dwc->ev_buffs[i] = evt;
  164. }
  165. return 0;
  166. }
  167. /**
  168. * dwc3_event_buffers_setup - setup our allocated event buffers
  169. * @dwc: pointer to our controller context structure
  170. *
  171. * Returns 0 on success otherwise negative errno.
  172. */
  173. static int dwc3_event_buffers_setup(struct dwc3 *dwc)
  174. {
  175. struct dwc3_event_buffer *evt;
  176. int n;
  177. for (n = 0; n < dwc->num_event_buffers; n++) {
  178. evt = dwc->ev_buffs[n];
  179. dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
  180. evt->buf, (unsigned long long) evt->dma,
  181. evt->length);
  182. evt->lpos = 0;
  183. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
  184. lower_32_bits(evt->dma));
  185. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
  186. upper_32_bits(evt->dma));
  187. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
  188. DWC3_GEVNTSIZ_SIZE(evt->length));
  189. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
  190. }
  191. return 0;
  192. }
  193. static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
  194. {
  195. struct dwc3_event_buffer *evt;
  196. int n;
  197. for (n = 0; n < dwc->num_event_buffers; n++) {
  198. evt = dwc->ev_buffs[n];
  199. evt->lpos = 0;
  200. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
  201. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
  202. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), DWC3_GEVNTSIZ_INTMASK
  203. | DWC3_GEVNTSIZ_SIZE(0));
  204. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
  205. }
  206. }
  207. static void dwc3_core_num_eps(struct dwc3 *dwc)
  208. {
  209. struct dwc3_hwparams *parms = &dwc->hwparams;
  210. dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
  211. dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
  212. dev_vdbg(dwc->dev, "found %d IN and %d OUT endpoints\n",
  213. dwc->num_in_eps, dwc->num_out_eps);
  214. }
  215. static void dwc3_cache_hwparams(struct dwc3 *dwc)
  216. {
  217. struct dwc3_hwparams *parms = &dwc->hwparams;
  218. parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
  219. parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
  220. parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
  221. parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
  222. parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
  223. parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
  224. parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
  225. parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
  226. parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
  227. }
  228. /**
  229. * dwc3_core_init - Low-level initialization of DWC3 Core
  230. * @dwc: Pointer to our controller context structure
  231. *
  232. * Returns 0 on success otherwise negative errno.
  233. */
  234. static int dwc3_core_init(struct dwc3 *dwc)
  235. {
  236. unsigned long timeout;
  237. u32 reg;
  238. int ret;
  239. reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
  240. /* This should read as U3 followed by revision number */
  241. if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
  242. dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
  243. ret = -ENODEV;
  244. goto err0;
  245. }
  246. dwc->revision = reg;
  247. /* issue device SoftReset too */
  248. timeout = jiffies + msecs_to_jiffies(500);
  249. dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
  250. do {
  251. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  252. if (!(reg & DWC3_DCTL_CSFTRST))
  253. break;
  254. if (time_after(jiffies, timeout)) {
  255. dev_err(dwc->dev, "Reset Timed Out\n");
  256. ret = -ETIMEDOUT;
  257. goto err0;
  258. }
  259. cpu_relax();
  260. } while (true);
  261. dwc3_core_soft_reset(dwc);
  262. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  263. reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
  264. reg &= ~DWC3_GCTL_DISSCRAMBLE;
  265. switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
  266. case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
  267. reg &= ~DWC3_GCTL_DSBLCLKGTNG;
  268. break;
  269. default:
  270. dev_dbg(dwc->dev, "No power optimization available\n");
  271. }
  272. /*
  273. * WORKAROUND: DWC3 revisions <1.90a have a bug
  274. * where the device can fail to connect at SuperSpeed
  275. * and falls back to high-speed mode which causes
  276. * the device to enter a Connect/Disconnect loop
  277. */
  278. if (dwc->revision < DWC3_REVISION_190A)
  279. reg |= DWC3_GCTL_U2RSTECN;
  280. dwc3_core_num_eps(dwc);
  281. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  282. return 0;
  283. err0:
  284. return ret;
  285. }
  286. static void dwc3_core_exit(struct dwc3 *dwc)
  287. {
  288. usb_phy_shutdown(dwc->usb2_phy);
  289. usb_phy_shutdown(dwc->usb3_phy);
  290. }
  291. #define DWC3_ALIGN_MASK (16 - 1)
  292. static int dwc3_probe(struct platform_device *pdev)
  293. {
  294. struct dwc3_platform_data *pdata = pdev->dev.platform_data;
  295. struct device_node *node = pdev->dev.of_node;
  296. struct resource *res;
  297. struct dwc3 *dwc;
  298. struct device *dev = &pdev->dev;
  299. int ret = -ENOMEM;
  300. void __iomem *regs;
  301. void *mem;
  302. mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
  303. if (!mem) {
  304. dev_err(dev, "not enough memory\n");
  305. return -ENOMEM;
  306. }
  307. dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
  308. dwc->mem = mem;
  309. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  310. if (!res) {
  311. dev_err(dev, "missing IRQ\n");
  312. return -ENODEV;
  313. }
  314. dwc->xhci_resources[1].start = res->start;
  315. dwc->xhci_resources[1].end = res->end;
  316. dwc->xhci_resources[1].flags = res->flags;
  317. dwc->xhci_resources[1].name = res->name;
  318. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  319. if (!res) {
  320. dev_err(dev, "missing memory resource\n");
  321. return -ENODEV;
  322. }
  323. dwc->xhci_resources[0].start = res->start;
  324. dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
  325. DWC3_XHCI_REGS_END;
  326. dwc->xhci_resources[0].flags = res->flags;
  327. dwc->xhci_resources[0].name = res->name;
  328. res->start += DWC3_GLOBALS_REGS_START;
  329. /*
  330. * Request memory region but exclude xHCI regs,
  331. * since it will be requested by the xhci-plat driver.
  332. */
  333. regs = devm_ioremap_resource(dev, res);
  334. if (IS_ERR(regs))
  335. return PTR_ERR(regs);
  336. if (node) {
  337. dwc->maximum_speed = of_usb_get_maximum_speed(node);
  338. dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
  339. dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
  340. dwc->needs_fifo_resize = of_property_read_bool(node, "tx-fifo-resize");
  341. dwc->dr_mode = of_usb_get_dr_mode(node);
  342. } else {
  343. dwc->maximum_speed = pdata->maximum_speed;
  344. dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
  345. dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
  346. dwc->needs_fifo_resize = pdata->tx_fifo_resize;
  347. dwc->dr_mode = pdata->dr_mode;
  348. }
  349. /* default to superspeed if no maximum_speed passed */
  350. if (dwc->maximum_speed == USB_SPEED_UNKNOWN)
  351. dwc->maximum_speed = USB_SPEED_SUPER;
  352. if (IS_ERR(dwc->usb2_phy)) {
  353. ret = PTR_ERR(dwc->usb2_phy);
  354. /*
  355. * if -ENXIO is returned, it means PHY layer wasn't
  356. * enabled, so it makes no sense to return -EPROBE_DEFER
  357. * in that case, since no PHY driver will ever probe.
  358. */
  359. if (ret == -ENXIO)
  360. return ret;
  361. dev_err(dev, "no usb2 phy configured\n");
  362. return -EPROBE_DEFER;
  363. }
  364. if (IS_ERR(dwc->usb3_phy)) {
  365. ret = PTR_ERR(dwc->usb3_phy);
  366. /*
  367. * if -ENXIO is returned, it means PHY layer wasn't
  368. * enabled, so it makes no sense to return -EPROBE_DEFER
  369. * in that case, since no PHY driver will ever probe.
  370. */
  371. if (ret == -ENXIO)
  372. return ret;
  373. dev_err(dev, "no usb3 phy configured\n");
  374. return -EPROBE_DEFER;
  375. }
  376. usb_phy_set_suspend(dwc->usb2_phy, 0);
  377. usb_phy_set_suspend(dwc->usb3_phy, 0);
  378. spin_lock_init(&dwc->lock);
  379. platform_set_drvdata(pdev, dwc);
  380. dwc->regs = regs;
  381. dwc->regs_size = resource_size(res);
  382. dwc->dev = dev;
  383. dev->dma_mask = dev->parent->dma_mask;
  384. dev->dma_parms = dev->parent->dma_parms;
  385. dma_set_coherent_mask(dev, dev->parent->coherent_dma_mask);
  386. pm_runtime_enable(dev);
  387. pm_runtime_get_sync(dev);
  388. pm_runtime_forbid(dev);
  389. dwc3_cache_hwparams(dwc);
  390. ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
  391. if (ret) {
  392. dev_err(dwc->dev, "failed to allocate event buffers\n");
  393. ret = -ENOMEM;
  394. goto err0;
  395. }
  396. ret = dwc3_core_init(dwc);
  397. if (ret) {
  398. dev_err(dev, "failed to initialize core\n");
  399. goto err0;
  400. }
  401. ret = dwc3_event_buffers_setup(dwc);
  402. if (ret) {
  403. dev_err(dwc->dev, "failed to setup event buffers\n");
  404. goto err1;
  405. }
  406. if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
  407. dwc->dr_mode = USB_DR_MODE_HOST;
  408. else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
  409. dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
  410. if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
  411. dwc->dr_mode = USB_DR_MODE_OTG;
  412. switch (dwc->dr_mode) {
  413. case USB_DR_MODE_PERIPHERAL:
  414. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
  415. ret = dwc3_gadget_init(dwc);
  416. if (ret) {
  417. dev_err(dev, "failed to initialize gadget\n");
  418. goto err2;
  419. }
  420. break;
  421. case USB_DR_MODE_HOST:
  422. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
  423. ret = dwc3_host_init(dwc);
  424. if (ret) {
  425. dev_err(dev, "failed to initialize host\n");
  426. goto err2;
  427. }
  428. break;
  429. case USB_DR_MODE_OTG:
  430. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
  431. ret = dwc3_host_init(dwc);
  432. if (ret) {
  433. dev_err(dev, "failed to initialize host\n");
  434. goto err2;
  435. }
  436. ret = dwc3_gadget_init(dwc);
  437. if (ret) {
  438. dev_err(dev, "failed to initialize gadget\n");
  439. goto err2;
  440. }
  441. break;
  442. default:
  443. dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
  444. goto err2;
  445. }
  446. ret = dwc3_debugfs_init(dwc);
  447. if (ret) {
  448. dev_err(dev, "failed to initialize debugfs\n");
  449. goto err3;
  450. }
  451. pm_runtime_allow(dev);
  452. return 0;
  453. err3:
  454. switch (dwc->dr_mode) {
  455. case USB_DR_MODE_PERIPHERAL:
  456. dwc3_gadget_exit(dwc);
  457. break;
  458. case USB_DR_MODE_HOST:
  459. dwc3_host_exit(dwc);
  460. break;
  461. case USB_DR_MODE_OTG:
  462. dwc3_host_exit(dwc);
  463. dwc3_gadget_exit(dwc);
  464. break;
  465. default:
  466. /* do nothing */
  467. break;
  468. }
  469. err2:
  470. dwc3_event_buffers_cleanup(dwc);
  471. err1:
  472. dwc3_core_exit(dwc);
  473. err0:
  474. dwc3_free_event_buffers(dwc);
  475. return ret;
  476. }
  477. static int dwc3_remove(struct platform_device *pdev)
  478. {
  479. struct dwc3 *dwc = platform_get_drvdata(pdev);
  480. usb_phy_set_suspend(dwc->usb2_phy, 1);
  481. usb_phy_set_suspend(dwc->usb3_phy, 1);
  482. pm_runtime_put(&pdev->dev);
  483. pm_runtime_disable(&pdev->dev);
  484. dwc3_debugfs_exit(dwc);
  485. switch (dwc->dr_mode) {
  486. case USB_DR_MODE_PERIPHERAL:
  487. dwc3_gadget_exit(dwc);
  488. break;
  489. case USB_DR_MODE_HOST:
  490. dwc3_host_exit(dwc);
  491. break;
  492. case USB_DR_MODE_OTG:
  493. dwc3_host_exit(dwc);
  494. dwc3_gadget_exit(dwc);
  495. break;
  496. default:
  497. /* do nothing */
  498. break;
  499. }
  500. dwc3_event_buffers_cleanup(dwc);
  501. dwc3_free_event_buffers(dwc);
  502. dwc3_core_exit(dwc);
  503. return 0;
  504. }
  505. #ifdef CONFIG_PM_SLEEP
  506. static int dwc3_prepare(struct device *dev)
  507. {
  508. struct dwc3 *dwc = dev_get_drvdata(dev);
  509. unsigned long flags;
  510. spin_lock_irqsave(&dwc->lock, flags);
  511. switch (dwc->dr_mode) {
  512. case USB_DR_MODE_PERIPHERAL:
  513. case USB_DR_MODE_OTG:
  514. dwc3_gadget_prepare(dwc);
  515. /* FALLTHROUGH */
  516. case USB_DR_MODE_HOST:
  517. default:
  518. dwc3_event_buffers_cleanup(dwc);
  519. break;
  520. }
  521. spin_unlock_irqrestore(&dwc->lock, flags);
  522. return 0;
  523. }
  524. static void dwc3_complete(struct device *dev)
  525. {
  526. struct dwc3 *dwc = dev_get_drvdata(dev);
  527. unsigned long flags;
  528. spin_lock_irqsave(&dwc->lock, flags);
  529. switch (dwc->dr_mode) {
  530. case USB_DR_MODE_PERIPHERAL:
  531. case USB_DR_MODE_OTG:
  532. dwc3_gadget_complete(dwc);
  533. /* FALLTHROUGH */
  534. case USB_DR_MODE_HOST:
  535. default:
  536. dwc3_event_buffers_setup(dwc);
  537. break;
  538. }
  539. spin_unlock_irqrestore(&dwc->lock, flags);
  540. }
  541. static int dwc3_suspend(struct device *dev)
  542. {
  543. struct dwc3 *dwc = dev_get_drvdata(dev);
  544. unsigned long flags;
  545. spin_lock_irqsave(&dwc->lock, flags);
  546. switch (dwc->dr_mode) {
  547. case USB_DR_MODE_PERIPHERAL:
  548. case USB_DR_MODE_OTG:
  549. dwc3_gadget_suspend(dwc);
  550. /* FALLTHROUGH */
  551. case USB_DR_MODE_HOST:
  552. default:
  553. /* do nothing */
  554. break;
  555. }
  556. dwc->gctl = dwc3_readl(dwc->regs, DWC3_GCTL);
  557. spin_unlock_irqrestore(&dwc->lock, flags);
  558. usb_phy_shutdown(dwc->usb3_phy);
  559. usb_phy_shutdown(dwc->usb2_phy);
  560. return 0;
  561. }
  562. static int dwc3_resume(struct device *dev)
  563. {
  564. struct dwc3 *dwc = dev_get_drvdata(dev);
  565. unsigned long flags;
  566. usb_phy_init(dwc->usb3_phy);
  567. usb_phy_init(dwc->usb2_phy);
  568. msleep(100);
  569. spin_lock_irqsave(&dwc->lock, flags);
  570. dwc3_writel(dwc->regs, DWC3_GCTL, dwc->gctl);
  571. switch (dwc->dr_mode) {
  572. case USB_DR_MODE_PERIPHERAL:
  573. case USB_DR_MODE_OTG:
  574. dwc3_gadget_resume(dwc);
  575. /* FALLTHROUGH */
  576. case USB_DR_MODE_HOST:
  577. default:
  578. /* do nothing */
  579. break;
  580. }
  581. spin_unlock_irqrestore(&dwc->lock, flags);
  582. pm_runtime_disable(dev);
  583. pm_runtime_set_active(dev);
  584. pm_runtime_enable(dev);
  585. return 0;
  586. }
  587. static const struct dev_pm_ops dwc3_dev_pm_ops = {
  588. .prepare = dwc3_prepare,
  589. .complete = dwc3_complete,
  590. SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
  591. };
  592. #define DWC3_PM_OPS &(dwc3_dev_pm_ops)
  593. #else
  594. #define DWC3_PM_OPS NULL
  595. #endif
  596. #ifdef CONFIG_OF
  597. static const struct of_device_id of_dwc3_match[] = {
  598. {
  599. .compatible = "snps,dwc3"
  600. },
  601. {
  602. .compatible = "synopsys,dwc3"
  603. },
  604. { },
  605. };
  606. MODULE_DEVICE_TABLE(of, of_dwc3_match);
  607. #endif
  608. static struct platform_driver dwc3_driver = {
  609. .probe = dwc3_probe,
  610. .remove = dwc3_remove,
  611. .driver = {
  612. .name = "dwc3",
  613. .of_match_table = of_match_ptr(of_dwc3_match),
  614. .pm = DWC3_PM_OPS,
  615. },
  616. };
  617. module_platform_driver(dwc3_driver);
  618. MODULE_ALIAS("platform:dwc3");
  619. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  620. MODULE_LICENSE("GPL v2");
  621. MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");