core.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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 int dwc3_core_soft_reset(struct dwc3 *dwc)
  57. {
  58. u32 reg;
  59. int ret;
  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. ret = phy_init(dwc->usb2_generic_phy);
  75. if (ret < 0)
  76. return ret;
  77. ret = phy_init(dwc->usb3_generic_phy);
  78. if (ret < 0) {
  79. phy_exit(dwc->usb2_generic_phy);
  80. return ret;
  81. }
  82. mdelay(100);
  83. /* Clear USB3 PHY reset */
  84. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  85. reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
  86. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  87. /* Clear USB2 PHY reset */
  88. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  89. reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
  90. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  91. mdelay(100);
  92. /* After PHYs are stable we can take Core out of reset state */
  93. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  94. reg &= ~DWC3_GCTL_CORESOFTRESET;
  95. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  96. return 0;
  97. }
  98. /**
  99. * dwc3_free_one_event_buffer - Frees one event buffer
  100. * @dwc: Pointer to our controller context structure
  101. * @evt: Pointer to event buffer to be freed
  102. */
  103. static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
  104. struct dwc3_event_buffer *evt)
  105. {
  106. dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
  107. }
  108. /**
  109. * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
  110. * @dwc: Pointer to our controller context structure
  111. * @length: size of the event buffer
  112. *
  113. * Returns a pointer to the allocated event buffer structure on success
  114. * otherwise ERR_PTR(errno).
  115. */
  116. static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
  117. unsigned length)
  118. {
  119. struct dwc3_event_buffer *evt;
  120. evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
  121. if (!evt)
  122. return ERR_PTR(-ENOMEM);
  123. evt->dwc = dwc;
  124. evt->length = length;
  125. evt->buf = dma_alloc_coherent(dwc->dev, length,
  126. &evt->dma, GFP_KERNEL);
  127. if (!evt->buf)
  128. return ERR_PTR(-ENOMEM);
  129. return evt;
  130. }
  131. /**
  132. * dwc3_free_event_buffers - frees all allocated event buffers
  133. * @dwc: Pointer to our controller context structure
  134. */
  135. static void dwc3_free_event_buffers(struct dwc3 *dwc)
  136. {
  137. struct dwc3_event_buffer *evt;
  138. int i;
  139. for (i = 0; i < dwc->num_event_buffers; i++) {
  140. evt = dwc->ev_buffs[i];
  141. if (evt)
  142. dwc3_free_one_event_buffer(dwc, evt);
  143. }
  144. }
  145. /**
  146. * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
  147. * @dwc: pointer to our controller context structure
  148. * @length: size of event buffer
  149. *
  150. * Returns 0 on success otherwise negative errno. In the error case, dwc
  151. * may contain some buffers allocated but not all which were requested.
  152. */
  153. static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
  154. {
  155. int num;
  156. int i;
  157. num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
  158. dwc->num_event_buffers = num;
  159. dwc->ev_buffs = devm_kzalloc(dwc->dev, sizeof(*dwc->ev_buffs) * num,
  160. GFP_KERNEL);
  161. if (!dwc->ev_buffs)
  162. return -ENOMEM;
  163. for (i = 0; i < num; i++) {
  164. struct dwc3_event_buffer *evt;
  165. evt = dwc3_alloc_one_event_buffer(dwc, length);
  166. if (IS_ERR(evt)) {
  167. dev_err(dwc->dev, "can't allocate event buffer\n");
  168. return PTR_ERR(evt);
  169. }
  170. dwc->ev_buffs[i] = evt;
  171. }
  172. return 0;
  173. }
  174. /**
  175. * dwc3_event_buffers_setup - setup our allocated event buffers
  176. * @dwc: pointer to our controller context structure
  177. *
  178. * Returns 0 on success otherwise negative errno.
  179. */
  180. static int dwc3_event_buffers_setup(struct dwc3 *dwc)
  181. {
  182. struct dwc3_event_buffer *evt;
  183. int n;
  184. for (n = 0; n < dwc->num_event_buffers; n++) {
  185. evt = dwc->ev_buffs[n];
  186. dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
  187. evt->buf, (unsigned long long) evt->dma,
  188. evt->length);
  189. evt->lpos = 0;
  190. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
  191. lower_32_bits(evt->dma));
  192. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
  193. upper_32_bits(evt->dma));
  194. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
  195. DWC3_GEVNTSIZ_SIZE(evt->length));
  196. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
  197. }
  198. return 0;
  199. }
  200. static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
  201. {
  202. struct dwc3_event_buffer *evt;
  203. int n;
  204. for (n = 0; n < dwc->num_event_buffers; n++) {
  205. evt = dwc->ev_buffs[n];
  206. evt->lpos = 0;
  207. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
  208. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
  209. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), DWC3_GEVNTSIZ_INTMASK
  210. | DWC3_GEVNTSIZ_SIZE(0));
  211. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
  212. }
  213. }
  214. static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
  215. {
  216. if (!dwc->has_hibernation)
  217. return 0;
  218. if (!dwc->nr_scratch)
  219. return 0;
  220. dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
  221. DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
  222. if (!dwc->scratchbuf)
  223. return -ENOMEM;
  224. return 0;
  225. }
  226. static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
  227. {
  228. dma_addr_t scratch_addr;
  229. u32 param;
  230. int ret;
  231. if (!dwc->has_hibernation)
  232. return 0;
  233. if (!dwc->nr_scratch)
  234. return 0;
  235. /* should never fall here */
  236. if (!WARN_ON(dwc->scratchbuf))
  237. return 0;
  238. scratch_addr = dma_map_single(dwc->dev, dwc->scratchbuf,
  239. dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
  240. DMA_BIDIRECTIONAL);
  241. if (dma_mapping_error(dwc->dev, scratch_addr)) {
  242. dev_err(dwc->dev, "failed to map scratch buffer\n");
  243. ret = -EFAULT;
  244. goto err0;
  245. }
  246. dwc->scratch_addr = scratch_addr;
  247. param = lower_32_bits(scratch_addr);
  248. ret = dwc3_send_gadget_generic_command(dwc,
  249. DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
  250. if (ret < 0)
  251. goto err1;
  252. param = upper_32_bits(scratch_addr);
  253. ret = dwc3_send_gadget_generic_command(dwc,
  254. DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
  255. if (ret < 0)
  256. goto err1;
  257. return 0;
  258. err1:
  259. dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
  260. DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
  261. err0:
  262. return ret;
  263. }
  264. static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
  265. {
  266. if (!dwc->has_hibernation)
  267. return;
  268. if (!dwc->nr_scratch)
  269. return;
  270. /* should never fall here */
  271. if (!WARN_ON(dwc->scratchbuf))
  272. return;
  273. dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
  274. DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
  275. kfree(dwc->scratchbuf);
  276. }
  277. static void dwc3_core_num_eps(struct dwc3 *dwc)
  278. {
  279. struct dwc3_hwparams *parms = &dwc->hwparams;
  280. dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
  281. dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
  282. dev_vdbg(dwc->dev, "found %d IN and %d OUT endpoints\n",
  283. dwc->num_in_eps, dwc->num_out_eps);
  284. }
  285. static void dwc3_cache_hwparams(struct dwc3 *dwc)
  286. {
  287. struct dwc3_hwparams *parms = &dwc->hwparams;
  288. parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
  289. parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
  290. parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
  291. parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
  292. parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
  293. parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
  294. parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
  295. parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
  296. parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
  297. }
  298. /**
  299. * dwc3_core_init - Low-level initialization of DWC3 Core
  300. * @dwc: Pointer to our controller context structure
  301. *
  302. * Returns 0 on success otherwise negative errno.
  303. */
  304. static int dwc3_core_init(struct dwc3 *dwc)
  305. {
  306. unsigned long timeout;
  307. u32 hwparams4 = dwc->hwparams.hwparams4;
  308. u32 reg;
  309. int ret;
  310. reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
  311. /* This should read as U3 followed by revision number */
  312. if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
  313. dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
  314. ret = -ENODEV;
  315. goto err0;
  316. }
  317. dwc->revision = reg;
  318. /* Handle USB2.0-only core configuration */
  319. if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
  320. DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
  321. if (dwc->maximum_speed == USB_SPEED_SUPER)
  322. dwc->maximum_speed = USB_SPEED_HIGH;
  323. }
  324. /* issue device SoftReset too */
  325. timeout = jiffies + msecs_to_jiffies(500);
  326. dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
  327. do {
  328. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  329. if (!(reg & DWC3_DCTL_CSFTRST))
  330. break;
  331. if (time_after(jiffies, timeout)) {
  332. dev_err(dwc->dev, "Reset Timed Out\n");
  333. ret = -ETIMEDOUT;
  334. goto err0;
  335. }
  336. cpu_relax();
  337. } while (true);
  338. ret = dwc3_core_soft_reset(dwc);
  339. if (ret)
  340. goto err0;
  341. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  342. reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
  343. reg &= ~DWC3_GCTL_DISSCRAMBLE;
  344. switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
  345. case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
  346. /**
  347. * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
  348. * issue which would cause xHCI compliance tests to fail.
  349. *
  350. * Because of that we cannot enable clock gating on such
  351. * configurations.
  352. *
  353. * Refers to:
  354. *
  355. * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
  356. * SOF/ITP Mode Used
  357. */
  358. if ((dwc->dr_mode == USB_DR_MODE_HOST ||
  359. dwc->dr_mode == USB_DR_MODE_OTG) &&
  360. (dwc->revision >= DWC3_REVISION_210A &&
  361. dwc->revision <= DWC3_REVISION_250A))
  362. reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
  363. else
  364. reg &= ~DWC3_GCTL_DSBLCLKGTNG;
  365. break;
  366. case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
  367. /* enable hibernation here */
  368. dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
  369. break;
  370. default:
  371. dev_dbg(dwc->dev, "No power optimization available\n");
  372. }
  373. /*
  374. * WORKAROUND: DWC3 revisions <1.90a have a bug
  375. * where the device can fail to connect at SuperSpeed
  376. * and falls back to high-speed mode which causes
  377. * the device to enter a Connect/Disconnect loop
  378. */
  379. if (dwc->revision < DWC3_REVISION_190A)
  380. reg |= DWC3_GCTL_U2RSTECN;
  381. dwc3_core_num_eps(dwc);
  382. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  383. ret = dwc3_alloc_scratch_buffers(dwc);
  384. if (ret)
  385. goto err1;
  386. ret = dwc3_setup_scratch_buffers(dwc);
  387. if (ret)
  388. goto err2;
  389. return 0;
  390. err2:
  391. dwc3_free_scratch_buffers(dwc);
  392. err1:
  393. usb_phy_shutdown(dwc->usb2_phy);
  394. usb_phy_shutdown(dwc->usb3_phy);
  395. phy_exit(dwc->usb2_generic_phy);
  396. phy_exit(dwc->usb3_generic_phy);
  397. err0:
  398. return ret;
  399. }
  400. static void dwc3_core_exit(struct dwc3 *dwc)
  401. {
  402. dwc3_free_scratch_buffers(dwc);
  403. usb_phy_shutdown(dwc->usb2_phy);
  404. usb_phy_shutdown(dwc->usb3_phy);
  405. phy_exit(dwc->usb2_generic_phy);
  406. phy_exit(dwc->usb3_generic_phy);
  407. }
  408. static int dwc3_core_get_phy(struct dwc3 *dwc)
  409. {
  410. struct device *dev = dwc->dev;
  411. struct device_node *node = dev->of_node;
  412. int ret;
  413. if (node) {
  414. dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
  415. dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
  416. } else {
  417. dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
  418. dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
  419. }
  420. if (IS_ERR(dwc->usb2_phy)) {
  421. ret = PTR_ERR(dwc->usb2_phy);
  422. if (ret == -ENXIO || ret == -ENODEV) {
  423. dwc->usb2_phy = NULL;
  424. } else if (ret == -EPROBE_DEFER) {
  425. return ret;
  426. } else {
  427. dev_err(dev, "no usb2 phy configured\n");
  428. return ret;
  429. }
  430. }
  431. if (IS_ERR(dwc->usb3_phy)) {
  432. ret = PTR_ERR(dwc->usb3_phy);
  433. if (ret == -ENXIO || ret == -ENODEV) {
  434. dwc->usb3_phy = NULL;
  435. } else if (ret == -EPROBE_DEFER) {
  436. return ret;
  437. } else {
  438. dev_err(dev, "no usb3 phy configured\n");
  439. return ret;
  440. }
  441. }
  442. dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
  443. if (IS_ERR(dwc->usb2_generic_phy)) {
  444. ret = PTR_ERR(dwc->usb2_generic_phy);
  445. if (ret == -ENOSYS || ret == -ENODEV) {
  446. dwc->usb2_generic_phy = NULL;
  447. } else if (ret == -EPROBE_DEFER) {
  448. return ret;
  449. } else {
  450. dev_err(dev, "no usb2 phy configured\n");
  451. return ret;
  452. }
  453. }
  454. dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
  455. if (IS_ERR(dwc->usb3_generic_phy)) {
  456. ret = PTR_ERR(dwc->usb3_generic_phy);
  457. if (ret == -ENOSYS || ret == -ENODEV) {
  458. dwc->usb3_generic_phy = NULL;
  459. } else if (ret == -EPROBE_DEFER) {
  460. return ret;
  461. } else {
  462. dev_err(dev, "no usb3 phy configured\n");
  463. return ret;
  464. }
  465. }
  466. return 0;
  467. }
  468. static int dwc3_core_init_mode(struct dwc3 *dwc)
  469. {
  470. struct device *dev = dwc->dev;
  471. int ret;
  472. switch (dwc->dr_mode) {
  473. case USB_DR_MODE_PERIPHERAL:
  474. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
  475. ret = dwc3_gadget_init(dwc);
  476. if (ret) {
  477. dev_err(dev, "failed to initialize gadget\n");
  478. return ret;
  479. }
  480. break;
  481. case USB_DR_MODE_HOST:
  482. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
  483. ret = dwc3_host_init(dwc);
  484. if (ret) {
  485. dev_err(dev, "failed to initialize host\n");
  486. return ret;
  487. }
  488. break;
  489. case USB_DR_MODE_OTG:
  490. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
  491. ret = dwc3_host_init(dwc);
  492. if (ret) {
  493. dev_err(dev, "failed to initialize host\n");
  494. return ret;
  495. }
  496. ret = dwc3_gadget_init(dwc);
  497. if (ret) {
  498. dev_err(dev, "failed to initialize gadget\n");
  499. return ret;
  500. }
  501. break;
  502. default:
  503. dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
  504. return -EINVAL;
  505. }
  506. return 0;
  507. }
  508. static void dwc3_core_exit_mode(struct dwc3 *dwc)
  509. {
  510. switch (dwc->dr_mode) {
  511. case USB_DR_MODE_PERIPHERAL:
  512. dwc3_gadget_exit(dwc);
  513. break;
  514. case USB_DR_MODE_HOST:
  515. dwc3_host_exit(dwc);
  516. break;
  517. case USB_DR_MODE_OTG:
  518. dwc3_host_exit(dwc);
  519. dwc3_gadget_exit(dwc);
  520. break;
  521. default:
  522. /* do nothing */
  523. break;
  524. }
  525. }
  526. #define DWC3_ALIGN_MASK (16 - 1)
  527. static int dwc3_probe(struct platform_device *pdev)
  528. {
  529. struct device *dev = &pdev->dev;
  530. struct dwc3_platform_data *pdata = dev_get_platdata(dev);
  531. struct device_node *node = dev->of_node;
  532. struct resource *res;
  533. struct dwc3 *dwc;
  534. int ret;
  535. void __iomem *regs;
  536. void *mem;
  537. mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
  538. if (!mem)
  539. return -ENOMEM;
  540. dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
  541. dwc->mem = mem;
  542. dwc->dev = dev;
  543. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  544. if (!res) {
  545. dev_err(dev, "missing IRQ\n");
  546. return -ENODEV;
  547. }
  548. dwc->xhci_resources[1].start = res->start;
  549. dwc->xhci_resources[1].end = res->end;
  550. dwc->xhci_resources[1].flags = res->flags;
  551. dwc->xhci_resources[1].name = res->name;
  552. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  553. if (!res) {
  554. dev_err(dev, "missing memory resource\n");
  555. return -ENODEV;
  556. }
  557. dwc->xhci_resources[0].start = res->start;
  558. dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
  559. DWC3_XHCI_REGS_END;
  560. dwc->xhci_resources[0].flags = res->flags;
  561. dwc->xhci_resources[0].name = res->name;
  562. res->start += DWC3_GLOBALS_REGS_START;
  563. /*
  564. * Request memory region but exclude xHCI regs,
  565. * since it will be requested by the xhci-plat driver.
  566. */
  567. regs = devm_ioremap_resource(dev, res);
  568. if (IS_ERR(regs))
  569. return PTR_ERR(regs);
  570. dwc->regs = regs;
  571. dwc->regs_size = resource_size(res);
  572. /*
  573. * restore res->start back to its original value so that,
  574. * in case the probe is deferred, we don't end up getting error in
  575. * request the memory region the next time probe is called.
  576. */
  577. res->start -= DWC3_GLOBALS_REGS_START;
  578. if (node) {
  579. dwc->maximum_speed = of_usb_get_maximum_speed(node);
  580. dwc->needs_fifo_resize = of_property_read_bool(node, "tx-fifo-resize");
  581. dwc->dr_mode = of_usb_get_dr_mode(node);
  582. } else if (pdata) {
  583. dwc->maximum_speed = pdata->maximum_speed;
  584. dwc->needs_fifo_resize = pdata->tx_fifo_resize;
  585. dwc->dr_mode = pdata->dr_mode;
  586. }
  587. /* default to superspeed if no maximum_speed passed */
  588. if (dwc->maximum_speed == USB_SPEED_UNKNOWN)
  589. dwc->maximum_speed = USB_SPEED_SUPER;
  590. ret = dwc3_core_get_phy(dwc);
  591. if (ret)
  592. return ret;
  593. spin_lock_init(&dwc->lock);
  594. platform_set_drvdata(pdev, dwc);
  595. dev->dma_mask = dev->parent->dma_mask;
  596. dev->dma_parms = dev->parent->dma_parms;
  597. dma_set_coherent_mask(dev, dev->parent->coherent_dma_mask);
  598. pm_runtime_enable(dev);
  599. pm_runtime_get_sync(dev);
  600. pm_runtime_forbid(dev);
  601. dwc3_cache_hwparams(dwc);
  602. ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
  603. if (ret) {
  604. dev_err(dwc->dev, "failed to allocate event buffers\n");
  605. ret = -ENOMEM;
  606. goto err0;
  607. }
  608. if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
  609. dwc->dr_mode = USB_DR_MODE_HOST;
  610. else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
  611. dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
  612. if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
  613. dwc->dr_mode = USB_DR_MODE_OTG;
  614. ret = dwc3_core_init(dwc);
  615. if (ret) {
  616. dev_err(dev, "failed to initialize core\n");
  617. goto err0;
  618. }
  619. usb_phy_set_suspend(dwc->usb2_phy, 0);
  620. usb_phy_set_suspend(dwc->usb3_phy, 0);
  621. ret = phy_power_on(dwc->usb2_generic_phy);
  622. if (ret < 0)
  623. goto err1;
  624. ret = phy_power_on(dwc->usb3_generic_phy);
  625. if (ret < 0)
  626. goto err_usb2phy_power;
  627. ret = dwc3_event_buffers_setup(dwc);
  628. if (ret) {
  629. dev_err(dwc->dev, "failed to setup event buffers\n");
  630. goto err_usb3phy_power;
  631. }
  632. ret = dwc3_core_init_mode(dwc);
  633. if (ret)
  634. goto err2;
  635. ret = dwc3_debugfs_init(dwc);
  636. if (ret) {
  637. dev_err(dev, "failed to initialize debugfs\n");
  638. goto err3;
  639. }
  640. pm_runtime_allow(dev);
  641. return 0;
  642. err3:
  643. dwc3_core_exit_mode(dwc);
  644. err2:
  645. dwc3_event_buffers_cleanup(dwc);
  646. err_usb3phy_power:
  647. phy_power_off(dwc->usb3_generic_phy);
  648. err_usb2phy_power:
  649. phy_power_off(dwc->usb2_generic_phy);
  650. err1:
  651. usb_phy_set_suspend(dwc->usb2_phy, 1);
  652. usb_phy_set_suspend(dwc->usb3_phy, 1);
  653. dwc3_core_exit(dwc);
  654. err0:
  655. dwc3_free_event_buffers(dwc);
  656. return ret;
  657. }
  658. static int dwc3_remove(struct platform_device *pdev)
  659. {
  660. struct dwc3 *dwc = platform_get_drvdata(pdev);
  661. dwc3_debugfs_exit(dwc);
  662. dwc3_core_exit_mode(dwc);
  663. dwc3_event_buffers_cleanup(dwc);
  664. dwc3_free_event_buffers(dwc);
  665. usb_phy_set_suspend(dwc->usb2_phy, 1);
  666. usb_phy_set_suspend(dwc->usb3_phy, 1);
  667. phy_power_off(dwc->usb2_generic_phy);
  668. phy_power_off(dwc->usb3_generic_phy);
  669. dwc3_core_exit(dwc);
  670. pm_runtime_put_sync(&pdev->dev);
  671. pm_runtime_disable(&pdev->dev);
  672. return 0;
  673. }
  674. #ifdef CONFIG_PM_SLEEP
  675. static int dwc3_prepare(struct device *dev)
  676. {
  677. struct dwc3 *dwc = dev_get_drvdata(dev);
  678. unsigned long flags;
  679. spin_lock_irqsave(&dwc->lock, flags);
  680. switch (dwc->dr_mode) {
  681. case USB_DR_MODE_PERIPHERAL:
  682. case USB_DR_MODE_OTG:
  683. dwc3_gadget_prepare(dwc);
  684. /* FALLTHROUGH */
  685. case USB_DR_MODE_HOST:
  686. default:
  687. dwc3_event_buffers_cleanup(dwc);
  688. break;
  689. }
  690. spin_unlock_irqrestore(&dwc->lock, flags);
  691. return 0;
  692. }
  693. static void dwc3_complete(struct device *dev)
  694. {
  695. struct dwc3 *dwc = dev_get_drvdata(dev);
  696. unsigned long flags;
  697. spin_lock_irqsave(&dwc->lock, flags);
  698. dwc3_event_buffers_setup(dwc);
  699. switch (dwc->dr_mode) {
  700. case USB_DR_MODE_PERIPHERAL:
  701. case USB_DR_MODE_OTG:
  702. dwc3_gadget_complete(dwc);
  703. /* FALLTHROUGH */
  704. case USB_DR_MODE_HOST:
  705. default:
  706. break;
  707. }
  708. spin_unlock_irqrestore(&dwc->lock, flags);
  709. }
  710. static int dwc3_suspend(struct device *dev)
  711. {
  712. struct dwc3 *dwc = dev_get_drvdata(dev);
  713. unsigned long flags;
  714. spin_lock_irqsave(&dwc->lock, flags);
  715. switch (dwc->dr_mode) {
  716. case USB_DR_MODE_PERIPHERAL:
  717. case USB_DR_MODE_OTG:
  718. dwc3_gadget_suspend(dwc);
  719. /* FALLTHROUGH */
  720. case USB_DR_MODE_HOST:
  721. default:
  722. /* do nothing */
  723. break;
  724. }
  725. dwc->gctl = dwc3_readl(dwc->regs, DWC3_GCTL);
  726. spin_unlock_irqrestore(&dwc->lock, flags);
  727. usb_phy_shutdown(dwc->usb3_phy);
  728. usb_phy_shutdown(dwc->usb2_phy);
  729. phy_exit(dwc->usb2_generic_phy);
  730. phy_exit(dwc->usb3_generic_phy);
  731. return 0;
  732. }
  733. static int dwc3_resume(struct device *dev)
  734. {
  735. struct dwc3 *dwc = dev_get_drvdata(dev);
  736. unsigned long flags;
  737. int ret;
  738. usb_phy_init(dwc->usb3_phy);
  739. usb_phy_init(dwc->usb2_phy);
  740. ret = phy_init(dwc->usb2_generic_phy);
  741. if (ret < 0)
  742. return ret;
  743. ret = phy_init(dwc->usb3_generic_phy);
  744. if (ret < 0)
  745. goto err_usb2phy_init;
  746. spin_lock_irqsave(&dwc->lock, flags);
  747. dwc3_writel(dwc->regs, DWC3_GCTL, dwc->gctl);
  748. switch (dwc->dr_mode) {
  749. case USB_DR_MODE_PERIPHERAL:
  750. case USB_DR_MODE_OTG:
  751. dwc3_gadget_resume(dwc);
  752. /* FALLTHROUGH */
  753. case USB_DR_MODE_HOST:
  754. default:
  755. /* do nothing */
  756. break;
  757. }
  758. spin_unlock_irqrestore(&dwc->lock, flags);
  759. pm_runtime_disable(dev);
  760. pm_runtime_set_active(dev);
  761. pm_runtime_enable(dev);
  762. return 0;
  763. err_usb2phy_init:
  764. phy_exit(dwc->usb2_generic_phy);
  765. return ret;
  766. }
  767. static const struct dev_pm_ops dwc3_dev_pm_ops = {
  768. .prepare = dwc3_prepare,
  769. .complete = dwc3_complete,
  770. SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
  771. };
  772. #define DWC3_PM_OPS &(dwc3_dev_pm_ops)
  773. #else
  774. #define DWC3_PM_OPS NULL
  775. #endif
  776. #ifdef CONFIG_OF
  777. static const struct of_device_id of_dwc3_match[] = {
  778. {
  779. .compatible = "snps,dwc3"
  780. },
  781. {
  782. .compatible = "synopsys,dwc3"
  783. },
  784. { },
  785. };
  786. MODULE_DEVICE_TABLE(of, of_dwc3_match);
  787. #endif
  788. static struct platform_driver dwc3_driver = {
  789. .probe = dwc3_probe,
  790. .remove = dwc3_remove,
  791. .driver = {
  792. .name = "dwc3",
  793. .of_match_table = of_match_ptr(of_dwc3_match),
  794. .pm = DWC3_PM_OPS,
  795. },
  796. };
  797. module_platform_driver(dwc3_driver);
  798. MODULE_ALIAS("platform:dwc3");
  799. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  800. MODULE_LICENSE("GPL v2");
  801. MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");