core.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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 "platform_data.h"
  38. #include "core.h"
  39. #include "gadget.h"
  40. #include "io.h"
  41. #include "debug.h"
  42. static char *maximum_speed = "super";
  43. module_param(maximum_speed, charp, 0);
  44. MODULE_PARM_DESC(maximum_speed, "Maximum supported speed.");
  45. /* -------------------------------------------------------------------------- */
  46. void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
  47. {
  48. u32 reg;
  49. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  50. reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
  51. reg |= DWC3_GCTL_PRTCAPDIR(mode);
  52. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  53. }
  54. /**
  55. * dwc3_core_soft_reset - Issues core soft reset and PHY reset
  56. * @dwc: pointer to our context structure
  57. */
  58. static void dwc3_core_soft_reset(struct dwc3 *dwc)
  59. {
  60. u32 reg;
  61. /* Before Resetting PHY, put Core in Reset */
  62. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  63. reg |= DWC3_GCTL_CORESOFTRESET;
  64. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  65. /* Assert USB3 PHY reset */
  66. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  67. reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
  68. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  69. /* Assert USB2 PHY reset */
  70. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  71. reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
  72. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  73. usb_phy_init(dwc->usb2_phy);
  74. usb_phy_init(dwc->usb3_phy);
  75. mdelay(100);
  76. /* Clear USB3 PHY reset */
  77. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  78. reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
  79. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  80. /* Clear USB2 PHY reset */
  81. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  82. reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
  83. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  84. mdelay(100);
  85. /* After PHYs are stable we can take Core out of reset state */
  86. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  87. reg &= ~DWC3_GCTL_CORESOFTRESET;
  88. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  89. }
  90. /**
  91. * dwc3_free_one_event_buffer - Frees one event buffer
  92. * @dwc: Pointer to our controller context structure
  93. * @evt: Pointer to event buffer to be freed
  94. */
  95. static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
  96. struct dwc3_event_buffer *evt)
  97. {
  98. dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
  99. }
  100. /**
  101. * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
  102. * @dwc: Pointer to our controller context structure
  103. * @length: size of the event buffer
  104. *
  105. * Returns a pointer to the allocated event buffer structure on success
  106. * otherwise ERR_PTR(errno).
  107. */
  108. static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
  109. unsigned length)
  110. {
  111. struct dwc3_event_buffer *evt;
  112. evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
  113. if (!evt)
  114. return ERR_PTR(-ENOMEM);
  115. evt->dwc = dwc;
  116. evt->length = length;
  117. evt->buf = dma_alloc_coherent(dwc->dev, length,
  118. &evt->dma, GFP_KERNEL);
  119. if (!evt->buf)
  120. return ERR_PTR(-ENOMEM);
  121. return evt;
  122. }
  123. /**
  124. * dwc3_free_event_buffers - frees all allocated event buffers
  125. * @dwc: Pointer to our controller context structure
  126. */
  127. static void dwc3_free_event_buffers(struct dwc3 *dwc)
  128. {
  129. struct dwc3_event_buffer *evt;
  130. int i;
  131. for (i = 0; i < dwc->num_event_buffers; i++) {
  132. evt = dwc->ev_buffs[i];
  133. if (evt)
  134. dwc3_free_one_event_buffer(dwc, evt);
  135. }
  136. }
  137. /**
  138. * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
  139. * @dwc: pointer to our controller context structure
  140. * @length: size of event buffer
  141. *
  142. * Returns 0 on success otherwise negative errno. In the error case, dwc
  143. * may contain some buffers allocated but not all which were requested.
  144. */
  145. static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
  146. {
  147. int num;
  148. int i;
  149. num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
  150. dwc->num_event_buffers = num;
  151. dwc->ev_buffs = devm_kzalloc(dwc->dev, sizeof(*dwc->ev_buffs) * num,
  152. GFP_KERNEL);
  153. if (!dwc->ev_buffs) {
  154. dev_err(dwc->dev, "can't allocate event buffers array\n");
  155. return -ENOMEM;
  156. }
  157. for (i = 0; i < num; i++) {
  158. struct dwc3_event_buffer *evt;
  159. evt = dwc3_alloc_one_event_buffer(dwc, length);
  160. if (IS_ERR(evt)) {
  161. dev_err(dwc->dev, "can't allocate event buffer\n");
  162. return PTR_ERR(evt);
  163. }
  164. dwc->ev_buffs[i] = evt;
  165. }
  166. return 0;
  167. }
  168. /**
  169. * dwc3_event_buffers_setup - setup our allocated event buffers
  170. * @dwc: pointer to our controller context structure
  171. *
  172. * Returns 0 on success otherwise negative errno.
  173. */
  174. static int dwc3_event_buffers_setup(struct dwc3 *dwc)
  175. {
  176. struct dwc3_event_buffer *evt;
  177. int n;
  178. for (n = 0; n < dwc->num_event_buffers; n++) {
  179. evt = dwc->ev_buffs[n];
  180. dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
  181. evt->buf, (unsigned long long) evt->dma,
  182. evt->length);
  183. evt->lpos = 0;
  184. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
  185. lower_32_bits(evt->dma));
  186. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
  187. upper_32_bits(evt->dma));
  188. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
  189. evt->length & 0xffff);
  190. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
  191. }
  192. return 0;
  193. }
  194. static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
  195. {
  196. struct dwc3_event_buffer *evt;
  197. int n;
  198. for (n = 0; n < dwc->num_event_buffers; n++) {
  199. evt = dwc->ev_buffs[n];
  200. evt->lpos = 0;
  201. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
  202. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
  203. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 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. u8 mode;
  303. mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
  304. if (!mem) {
  305. dev_err(dev, "not enough memory\n");
  306. return -ENOMEM;
  307. }
  308. dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
  309. dwc->mem = mem;
  310. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  311. if (!res) {
  312. dev_err(dev, "missing IRQ\n");
  313. return -ENODEV;
  314. }
  315. dwc->xhci_resources[1].start = res->start;
  316. dwc->xhci_resources[1].end = res->end;
  317. dwc->xhci_resources[1].flags = res->flags;
  318. dwc->xhci_resources[1].name = res->name;
  319. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  320. if (!res) {
  321. dev_err(dev, "missing memory resource\n");
  322. return -ENODEV;
  323. }
  324. dwc->xhci_resources[0].start = res->start;
  325. dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
  326. DWC3_XHCI_REGS_END;
  327. dwc->xhci_resources[0].flags = res->flags;
  328. dwc->xhci_resources[0].name = res->name;
  329. /*
  330. * Request memory region but exclude xHCI regs,
  331. * since it will be requested by the xhci-plat driver.
  332. */
  333. res = devm_request_mem_region(dev, res->start + DWC3_GLOBALS_REGS_START,
  334. resource_size(res) - DWC3_GLOBALS_REGS_START,
  335. dev_name(dev));
  336. if (!res) {
  337. dev_err(dev, "can't request mem region\n");
  338. return -ENOMEM;
  339. }
  340. regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
  341. if (!regs) {
  342. dev_err(dev, "ioremap failed\n");
  343. return -ENOMEM;
  344. }
  345. if (node) {
  346. dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
  347. dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
  348. dwc->needs_fifo_resize = of_property_read_bool(node, "tx-fifo-resize");
  349. } else {
  350. dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
  351. dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
  352. dwc->needs_fifo_resize = pdata->tx_fifo_resize;
  353. }
  354. if (IS_ERR(dwc->usb2_phy)) {
  355. ret = PTR_ERR(dwc->usb2_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 usb2 phy configured\n");
  364. return -EPROBE_DEFER;
  365. }
  366. if (IS_ERR(dwc->usb3_phy)) {
  367. ret = PTR_ERR(dwc->usb3_phy);
  368. /*
  369. * if -ENXIO is returned, it means PHY layer wasn't
  370. * enabled, so it makes no sense to return -EPROBE_DEFER
  371. * in that case, since no PHY driver will ever probe.
  372. */
  373. if (ret == -ENXIO)
  374. return ret;
  375. dev_err(dev, "no usb3 phy configured\n");
  376. return -EPROBE_DEFER;
  377. }
  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. if (!strncmp("super", maximum_speed, 5))
  389. dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
  390. else if (!strncmp("high", maximum_speed, 4))
  391. dwc->maximum_speed = DWC3_DCFG_HIGHSPEED;
  392. else if (!strncmp("full", maximum_speed, 4))
  393. dwc->maximum_speed = DWC3_DCFG_FULLSPEED1;
  394. else if (!strncmp("low", maximum_speed, 3))
  395. dwc->maximum_speed = DWC3_DCFG_LOWSPEED;
  396. else
  397. dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
  398. pm_runtime_enable(dev);
  399. pm_runtime_get_sync(dev);
  400. pm_runtime_forbid(dev);
  401. dwc3_cache_hwparams(dwc);
  402. ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
  403. if (ret) {
  404. dev_err(dwc->dev, "failed to allocate event buffers\n");
  405. ret = -ENOMEM;
  406. goto err0;
  407. }
  408. ret = dwc3_core_init(dwc);
  409. if (ret) {
  410. dev_err(dev, "failed to initialize core\n");
  411. goto err0;
  412. }
  413. ret = dwc3_event_buffers_setup(dwc);
  414. if (ret) {
  415. dev_err(dwc->dev, "failed to setup event buffers\n");
  416. goto err1;
  417. }
  418. if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
  419. mode = DWC3_MODE_HOST;
  420. else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
  421. mode = DWC3_MODE_DEVICE;
  422. else
  423. mode = DWC3_MODE_DRD;
  424. switch (mode) {
  425. case DWC3_MODE_DEVICE:
  426. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
  427. ret = dwc3_gadget_init(dwc);
  428. if (ret) {
  429. dev_err(dev, "failed to initialize gadget\n");
  430. goto err2;
  431. }
  432. break;
  433. case DWC3_MODE_HOST:
  434. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
  435. ret = dwc3_host_init(dwc);
  436. if (ret) {
  437. dev_err(dev, "failed to initialize host\n");
  438. goto err2;
  439. }
  440. break;
  441. case DWC3_MODE_DRD:
  442. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
  443. ret = dwc3_host_init(dwc);
  444. if (ret) {
  445. dev_err(dev, "failed to initialize host\n");
  446. goto err2;
  447. }
  448. ret = dwc3_gadget_init(dwc);
  449. if (ret) {
  450. dev_err(dev, "failed to initialize gadget\n");
  451. goto err2;
  452. }
  453. break;
  454. default:
  455. dev_err(dev, "Unsupported mode of operation %d\n", mode);
  456. goto err2;
  457. }
  458. dwc->mode = mode;
  459. ret = dwc3_debugfs_init(dwc);
  460. if (ret) {
  461. dev_err(dev, "failed to initialize debugfs\n");
  462. goto err3;
  463. }
  464. pm_runtime_allow(dev);
  465. return 0;
  466. err3:
  467. switch (mode) {
  468. case DWC3_MODE_DEVICE:
  469. dwc3_gadget_exit(dwc);
  470. break;
  471. case DWC3_MODE_HOST:
  472. dwc3_host_exit(dwc);
  473. break;
  474. case DWC3_MODE_DRD:
  475. dwc3_host_exit(dwc);
  476. dwc3_gadget_exit(dwc);
  477. break;
  478. default:
  479. /* do nothing */
  480. break;
  481. }
  482. err2:
  483. dwc3_event_buffers_cleanup(dwc);
  484. err1:
  485. dwc3_core_exit(dwc);
  486. err0:
  487. dwc3_free_event_buffers(dwc);
  488. return ret;
  489. }
  490. static int dwc3_remove(struct platform_device *pdev)
  491. {
  492. struct dwc3 *dwc = platform_get_drvdata(pdev);
  493. usb_phy_set_suspend(dwc->usb2_phy, 1);
  494. usb_phy_set_suspend(dwc->usb3_phy, 1);
  495. pm_runtime_put(&pdev->dev);
  496. pm_runtime_disable(&pdev->dev);
  497. dwc3_debugfs_exit(dwc);
  498. switch (dwc->mode) {
  499. case DWC3_MODE_DEVICE:
  500. dwc3_gadget_exit(dwc);
  501. break;
  502. case DWC3_MODE_HOST:
  503. dwc3_host_exit(dwc);
  504. break;
  505. case DWC3_MODE_DRD:
  506. dwc3_host_exit(dwc);
  507. dwc3_gadget_exit(dwc);
  508. break;
  509. default:
  510. /* do nothing */
  511. break;
  512. }
  513. dwc3_event_buffers_cleanup(dwc);
  514. dwc3_free_event_buffers(dwc);
  515. dwc3_core_exit(dwc);
  516. return 0;
  517. }
  518. #ifdef CONFIG_PM_SLEEP
  519. static int dwc3_prepare(struct device *dev)
  520. {
  521. struct dwc3 *dwc = dev_get_drvdata(dev);
  522. unsigned long flags;
  523. spin_lock_irqsave(&dwc->lock, flags);
  524. switch (dwc->mode) {
  525. case DWC3_MODE_DEVICE:
  526. case DWC3_MODE_DRD:
  527. dwc3_gadget_prepare(dwc);
  528. /* FALLTHROUGH */
  529. case DWC3_MODE_HOST:
  530. default:
  531. dwc3_event_buffers_cleanup(dwc);
  532. break;
  533. }
  534. spin_unlock_irqrestore(&dwc->lock, flags);
  535. return 0;
  536. }
  537. static void dwc3_complete(struct device *dev)
  538. {
  539. struct dwc3 *dwc = dev_get_drvdata(dev);
  540. unsigned long flags;
  541. spin_lock_irqsave(&dwc->lock, flags);
  542. switch (dwc->mode) {
  543. case DWC3_MODE_DEVICE:
  544. case DWC3_MODE_DRD:
  545. dwc3_gadget_complete(dwc);
  546. /* FALLTHROUGH */
  547. case DWC3_MODE_HOST:
  548. default:
  549. dwc3_event_buffers_setup(dwc);
  550. break;
  551. }
  552. spin_unlock_irqrestore(&dwc->lock, flags);
  553. }
  554. static int dwc3_suspend(struct device *dev)
  555. {
  556. struct dwc3 *dwc = dev_get_drvdata(dev);
  557. unsigned long flags;
  558. spin_lock_irqsave(&dwc->lock, flags);
  559. switch (dwc->mode) {
  560. case DWC3_MODE_DEVICE:
  561. case DWC3_MODE_DRD:
  562. dwc3_gadget_suspend(dwc);
  563. /* FALLTHROUGH */
  564. case DWC3_MODE_HOST:
  565. default:
  566. /* do nothing */
  567. break;
  568. }
  569. dwc->gctl = dwc3_readl(dwc->regs, DWC3_GCTL);
  570. spin_unlock_irqrestore(&dwc->lock, flags);
  571. usb_phy_shutdown(dwc->usb3_phy);
  572. usb_phy_shutdown(dwc->usb2_phy);
  573. return 0;
  574. }
  575. static int dwc3_resume(struct device *dev)
  576. {
  577. struct dwc3 *dwc = dev_get_drvdata(dev);
  578. unsigned long flags;
  579. usb_phy_init(dwc->usb3_phy);
  580. usb_phy_init(dwc->usb2_phy);
  581. msleep(100);
  582. spin_lock_irqsave(&dwc->lock, flags);
  583. dwc3_writel(dwc->regs, DWC3_GCTL, dwc->gctl);
  584. switch (dwc->mode) {
  585. case DWC3_MODE_DEVICE:
  586. case DWC3_MODE_DRD:
  587. dwc3_gadget_resume(dwc);
  588. /* FALLTHROUGH */
  589. case DWC3_MODE_HOST:
  590. default:
  591. /* do nothing */
  592. break;
  593. }
  594. spin_unlock_irqrestore(&dwc->lock, flags);
  595. pm_runtime_disable(dev);
  596. pm_runtime_set_active(dev);
  597. pm_runtime_enable(dev);
  598. return 0;
  599. }
  600. static const struct dev_pm_ops dwc3_dev_pm_ops = {
  601. .prepare = dwc3_prepare,
  602. .complete = dwc3_complete,
  603. SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
  604. };
  605. #define DWC3_PM_OPS &(dwc3_dev_pm_ops)
  606. #else
  607. #define DWC3_PM_OPS NULL
  608. #endif
  609. #ifdef CONFIG_OF
  610. static const struct of_device_id of_dwc3_match[] = {
  611. {
  612. .compatible = "synopsys,dwc3"
  613. },
  614. { },
  615. };
  616. MODULE_DEVICE_TABLE(of, of_dwc3_match);
  617. #endif
  618. static struct platform_driver dwc3_driver = {
  619. .probe = dwc3_probe,
  620. .remove = dwc3_remove,
  621. .driver = {
  622. .name = "dwc3",
  623. .of_match_table = of_match_ptr(of_dwc3_match),
  624. .pm = DWC3_PM_OPS,
  625. },
  626. };
  627. module_platform_driver(dwc3_driver);
  628. MODULE_ALIAS("platform:dwc3");
  629. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  630. MODULE_LICENSE("GPL v2");
  631. MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");