core.c 18 KB

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