core.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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 device *dev = &pdev->dev;
  295. struct dwc3_platform_data *pdata = dev_get_platdata(dev);
  296. struct device_node *node = dev->of_node;
  297. struct resource *res;
  298. struct dwc3 *dwc;
  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. if (node) {
  324. dwc->maximum_speed = of_usb_get_maximum_speed(node);
  325. dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
  326. dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
  327. dwc->needs_fifo_resize = of_property_read_bool(node, "tx-fifo-resize");
  328. dwc->dr_mode = of_usb_get_dr_mode(node);
  329. } else if (pdata) {
  330. dwc->maximum_speed = pdata->maximum_speed;
  331. dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
  332. dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
  333. dwc->needs_fifo_resize = pdata->tx_fifo_resize;
  334. dwc->dr_mode = pdata->dr_mode;
  335. } else {
  336. dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
  337. dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
  338. }
  339. /* default to superspeed if no maximum_speed passed */
  340. if (dwc->maximum_speed == USB_SPEED_UNKNOWN)
  341. dwc->maximum_speed = USB_SPEED_SUPER;
  342. if (IS_ERR(dwc->usb2_phy)) {
  343. ret = PTR_ERR(dwc->usb2_phy);
  344. /*
  345. * if -ENXIO is returned, it means PHY layer wasn't
  346. * enabled, so it makes no sense to return -EPROBE_DEFER
  347. * in that case, since no PHY driver will ever probe.
  348. */
  349. if (ret == -ENXIO)
  350. return ret;
  351. dev_err(dev, "no usb2 phy configured\n");
  352. return -EPROBE_DEFER;
  353. }
  354. if (IS_ERR(dwc->usb3_phy)) {
  355. ret = PTR_ERR(dwc->usb3_phy);
  356. /*
  357. * if -ENXIO is returned, it means PHY layer wasn't
  358. * enabled, so it makes no sense to return -EPROBE_DEFER
  359. * in that case, since no PHY driver will ever probe.
  360. */
  361. if (ret == -ENXIO)
  362. return ret;
  363. dev_err(dev, "no usb3 phy configured\n");
  364. return -EPROBE_DEFER;
  365. }
  366. dwc->xhci_resources[0].start = res->start;
  367. dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
  368. DWC3_XHCI_REGS_END;
  369. dwc->xhci_resources[0].flags = res->flags;
  370. dwc->xhci_resources[0].name = res->name;
  371. res->start += DWC3_GLOBALS_REGS_START;
  372. /*
  373. * Request memory region but exclude xHCI regs,
  374. * since it will be requested by the xhci-plat driver.
  375. */
  376. regs = devm_ioremap_resource(dev, res);
  377. if (IS_ERR(regs))
  378. return PTR_ERR(regs);
  379. usb_phy_set_suspend(dwc->usb2_phy, 0);
  380. usb_phy_set_suspend(dwc->usb3_phy, 0);
  381. spin_lock_init(&dwc->lock);
  382. platform_set_drvdata(pdev, dwc);
  383. dwc->regs = regs;
  384. dwc->regs_size = resource_size(res);
  385. dwc->dev = dev;
  386. dev->dma_mask = dev->parent->dma_mask;
  387. dev->dma_parms = dev->parent->dma_parms;
  388. dma_set_coherent_mask(dev, dev->parent->coherent_dma_mask);
  389. pm_runtime_enable(dev);
  390. pm_runtime_get_sync(dev);
  391. pm_runtime_forbid(dev);
  392. dwc3_cache_hwparams(dwc);
  393. ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
  394. if (ret) {
  395. dev_err(dwc->dev, "failed to allocate event buffers\n");
  396. ret = -ENOMEM;
  397. goto err0;
  398. }
  399. ret = dwc3_core_init(dwc);
  400. if (ret) {
  401. dev_err(dev, "failed to initialize core\n");
  402. goto err0;
  403. }
  404. ret = dwc3_event_buffers_setup(dwc);
  405. if (ret) {
  406. dev_err(dwc->dev, "failed to setup event buffers\n");
  407. goto err1;
  408. }
  409. if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
  410. dwc->dr_mode = USB_DR_MODE_HOST;
  411. else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
  412. dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
  413. if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
  414. dwc->dr_mode = USB_DR_MODE_OTG;
  415. switch (dwc->dr_mode) {
  416. case USB_DR_MODE_PERIPHERAL:
  417. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
  418. ret = dwc3_gadget_init(dwc);
  419. if (ret) {
  420. dev_err(dev, "failed to initialize gadget\n");
  421. goto err2;
  422. }
  423. break;
  424. case USB_DR_MODE_HOST:
  425. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
  426. ret = dwc3_host_init(dwc);
  427. if (ret) {
  428. dev_err(dev, "failed to initialize host\n");
  429. goto err2;
  430. }
  431. break;
  432. case USB_DR_MODE_OTG:
  433. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
  434. ret = dwc3_host_init(dwc);
  435. if (ret) {
  436. dev_err(dev, "failed to initialize host\n");
  437. goto err2;
  438. }
  439. ret = dwc3_gadget_init(dwc);
  440. if (ret) {
  441. dev_err(dev, "failed to initialize gadget\n");
  442. goto err2;
  443. }
  444. break;
  445. default:
  446. dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
  447. goto err2;
  448. }
  449. ret = dwc3_debugfs_init(dwc);
  450. if (ret) {
  451. dev_err(dev, "failed to initialize debugfs\n");
  452. goto err3;
  453. }
  454. pm_runtime_allow(dev);
  455. return 0;
  456. err3:
  457. switch (dwc->dr_mode) {
  458. case USB_DR_MODE_PERIPHERAL:
  459. dwc3_gadget_exit(dwc);
  460. break;
  461. case USB_DR_MODE_HOST:
  462. dwc3_host_exit(dwc);
  463. break;
  464. case USB_DR_MODE_OTG:
  465. dwc3_host_exit(dwc);
  466. dwc3_gadget_exit(dwc);
  467. break;
  468. default:
  469. /* do nothing */
  470. break;
  471. }
  472. err2:
  473. dwc3_event_buffers_cleanup(dwc);
  474. err1:
  475. dwc3_core_exit(dwc);
  476. err0:
  477. dwc3_free_event_buffers(dwc);
  478. return ret;
  479. }
  480. static int dwc3_remove(struct platform_device *pdev)
  481. {
  482. struct dwc3 *dwc = platform_get_drvdata(pdev);
  483. usb_phy_set_suspend(dwc->usb2_phy, 1);
  484. usb_phy_set_suspend(dwc->usb3_phy, 1);
  485. pm_runtime_put(&pdev->dev);
  486. pm_runtime_disable(&pdev->dev);
  487. dwc3_debugfs_exit(dwc);
  488. switch (dwc->dr_mode) {
  489. case USB_DR_MODE_PERIPHERAL:
  490. dwc3_gadget_exit(dwc);
  491. break;
  492. case USB_DR_MODE_HOST:
  493. dwc3_host_exit(dwc);
  494. break;
  495. case USB_DR_MODE_OTG:
  496. dwc3_host_exit(dwc);
  497. dwc3_gadget_exit(dwc);
  498. break;
  499. default:
  500. /* do nothing */
  501. break;
  502. }
  503. dwc3_event_buffers_cleanup(dwc);
  504. dwc3_free_event_buffers(dwc);
  505. dwc3_core_exit(dwc);
  506. return 0;
  507. }
  508. #ifdef CONFIG_PM_SLEEP
  509. static int dwc3_prepare(struct device *dev)
  510. {
  511. struct dwc3 *dwc = dev_get_drvdata(dev);
  512. unsigned long flags;
  513. spin_lock_irqsave(&dwc->lock, flags);
  514. switch (dwc->dr_mode) {
  515. case USB_DR_MODE_PERIPHERAL:
  516. case USB_DR_MODE_OTG:
  517. dwc3_gadget_prepare(dwc);
  518. /* FALLTHROUGH */
  519. case USB_DR_MODE_HOST:
  520. default:
  521. dwc3_event_buffers_cleanup(dwc);
  522. break;
  523. }
  524. spin_unlock_irqrestore(&dwc->lock, flags);
  525. return 0;
  526. }
  527. static void dwc3_complete(struct device *dev)
  528. {
  529. struct dwc3 *dwc = dev_get_drvdata(dev);
  530. unsigned long flags;
  531. spin_lock_irqsave(&dwc->lock, flags);
  532. switch (dwc->dr_mode) {
  533. case USB_DR_MODE_PERIPHERAL:
  534. case USB_DR_MODE_OTG:
  535. dwc3_gadget_complete(dwc);
  536. /* FALLTHROUGH */
  537. case USB_DR_MODE_HOST:
  538. default:
  539. dwc3_event_buffers_setup(dwc);
  540. break;
  541. }
  542. spin_unlock_irqrestore(&dwc->lock, flags);
  543. }
  544. static int dwc3_suspend(struct device *dev)
  545. {
  546. struct dwc3 *dwc = dev_get_drvdata(dev);
  547. unsigned long flags;
  548. spin_lock_irqsave(&dwc->lock, flags);
  549. switch (dwc->dr_mode) {
  550. case USB_DR_MODE_PERIPHERAL:
  551. case USB_DR_MODE_OTG:
  552. dwc3_gadget_suspend(dwc);
  553. /* FALLTHROUGH */
  554. case USB_DR_MODE_HOST:
  555. default:
  556. /* do nothing */
  557. break;
  558. }
  559. dwc->gctl = dwc3_readl(dwc->regs, DWC3_GCTL);
  560. spin_unlock_irqrestore(&dwc->lock, flags);
  561. usb_phy_shutdown(dwc->usb3_phy);
  562. usb_phy_shutdown(dwc->usb2_phy);
  563. return 0;
  564. }
  565. static int dwc3_resume(struct device *dev)
  566. {
  567. struct dwc3 *dwc = dev_get_drvdata(dev);
  568. unsigned long flags;
  569. usb_phy_init(dwc->usb3_phy);
  570. usb_phy_init(dwc->usb2_phy);
  571. msleep(100);
  572. spin_lock_irqsave(&dwc->lock, flags);
  573. dwc3_writel(dwc->regs, DWC3_GCTL, dwc->gctl);
  574. switch (dwc->dr_mode) {
  575. case USB_DR_MODE_PERIPHERAL:
  576. case USB_DR_MODE_OTG:
  577. dwc3_gadget_resume(dwc);
  578. /* FALLTHROUGH */
  579. case USB_DR_MODE_HOST:
  580. default:
  581. /* do nothing */
  582. break;
  583. }
  584. spin_unlock_irqrestore(&dwc->lock, flags);
  585. pm_runtime_disable(dev);
  586. pm_runtime_set_active(dev);
  587. pm_runtime_enable(dev);
  588. return 0;
  589. }
  590. static const struct dev_pm_ops dwc3_dev_pm_ops = {
  591. .prepare = dwc3_prepare,
  592. .complete = dwc3_complete,
  593. SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
  594. };
  595. #define DWC3_PM_OPS &(dwc3_dev_pm_ops)
  596. #else
  597. #define DWC3_PM_OPS NULL
  598. #endif
  599. #ifdef CONFIG_OF
  600. static const struct of_device_id of_dwc3_match[] = {
  601. {
  602. .compatible = "snps,dwc3"
  603. },
  604. {
  605. .compatible = "synopsys,dwc3"
  606. },
  607. { },
  608. };
  609. MODULE_DEVICE_TABLE(of, of_dwc3_match);
  610. #endif
  611. static struct platform_driver dwc3_driver = {
  612. .probe = dwc3_probe,
  613. .remove = dwc3_remove,
  614. .driver = {
  615. .name = "dwc3",
  616. .of_match_table = of_match_ptr(of_dwc3_match),
  617. .pm = DWC3_PM_OPS,
  618. },
  619. };
  620. module_platform_driver(dwc3_driver);
  621. MODULE_ALIAS("platform:dwc3");
  622. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  623. MODULE_LICENSE("GPL v2");
  624. MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");