core.c 18 KB

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