core.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  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/version.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/slab.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/ioport.h>
  30. #include <linux/io.h>
  31. #include <linux/list.h>
  32. #include <linux/delay.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/of.h>
  35. #include <linux/acpi.h>
  36. #include <linux/pinctrl/consumer.h>
  37. #include <linux/usb/ch9.h>
  38. #include <linux/usb/gadget.h>
  39. #include <linux/usb/of.h>
  40. #include <linux/usb/otg.h>
  41. #include "core.h"
  42. #include "gadget.h"
  43. #include "io.h"
  44. #include "debug.h"
  45. #define DWC3_DEFAULT_AUTOSUSPEND_DELAY 5000 /* ms */
  46. /**
  47. * dwc3_get_dr_mode - Validates and sets dr_mode
  48. * @dwc: pointer to our context structure
  49. */
  50. static int dwc3_get_dr_mode(struct dwc3 *dwc)
  51. {
  52. enum usb_dr_mode mode;
  53. struct device *dev = dwc->dev;
  54. unsigned int hw_mode;
  55. if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
  56. dwc->dr_mode = USB_DR_MODE_OTG;
  57. mode = dwc->dr_mode;
  58. hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
  59. switch (hw_mode) {
  60. case DWC3_GHWPARAMS0_MODE_GADGET:
  61. if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) {
  62. dev_err(dev,
  63. "Controller does not support host mode.\n");
  64. return -EINVAL;
  65. }
  66. mode = USB_DR_MODE_PERIPHERAL;
  67. break;
  68. case DWC3_GHWPARAMS0_MODE_HOST:
  69. if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) {
  70. dev_err(dev,
  71. "Controller does not support device mode.\n");
  72. return -EINVAL;
  73. }
  74. mode = USB_DR_MODE_HOST;
  75. break;
  76. default:
  77. if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
  78. mode = USB_DR_MODE_HOST;
  79. else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
  80. mode = USB_DR_MODE_PERIPHERAL;
  81. }
  82. if (mode != dwc->dr_mode) {
  83. dev_warn(dev,
  84. "Configuration mismatch. dr_mode forced to %s\n",
  85. mode == USB_DR_MODE_HOST ? "host" : "gadget");
  86. dwc->dr_mode = mode;
  87. }
  88. return 0;
  89. }
  90. static void dwc3_event_buffers_cleanup(struct dwc3 *dwc);
  91. static int dwc3_event_buffers_setup(struct dwc3 *dwc);
  92. static void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
  93. {
  94. u32 reg;
  95. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  96. reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
  97. reg |= DWC3_GCTL_PRTCAPDIR(mode);
  98. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  99. }
  100. static void __dwc3_set_mode(struct work_struct *work)
  101. {
  102. struct dwc3 *dwc = work_to_dwc(work);
  103. unsigned long flags;
  104. int ret;
  105. if (!dwc->desired_dr_role)
  106. return;
  107. if (dwc->desired_dr_role == dwc->current_dr_role)
  108. return;
  109. if (dwc->dr_mode != USB_DR_MODE_OTG)
  110. return;
  111. switch (dwc->current_dr_role) {
  112. case DWC3_GCTL_PRTCAP_HOST:
  113. dwc3_host_exit(dwc);
  114. break;
  115. case DWC3_GCTL_PRTCAP_DEVICE:
  116. dwc3_gadget_exit(dwc);
  117. dwc3_event_buffers_cleanup(dwc);
  118. break;
  119. default:
  120. break;
  121. }
  122. spin_lock_irqsave(&dwc->lock, flags);
  123. dwc3_set_prtcap(dwc, dwc->desired_dr_role);
  124. dwc->current_dr_role = dwc->desired_dr_role;
  125. spin_unlock_irqrestore(&dwc->lock, flags);
  126. switch (dwc->desired_dr_role) {
  127. case DWC3_GCTL_PRTCAP_HOST:
  128. ret = dwc3_host_init(dwc);
  129. if (ret) {
  130. dev_err(dwc->dev, "failed to initialize host\n");
  131. } else {
  132. if (dwc->usb2_phy)
  133. otg_set_vbus(dwc->usb2_phy->otg, true);
  134. if (dwc->usb2_generic_phy)
  135. phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
  136. }
  137. break;
  138. case DWC3_GCTL_PRTCAP_DEVICE:
  139. dwc3_event_buffers_setup(dwc);
  140. if (dwc->usb2_phy)
  141. otg_set_vbus(dwc->usb2_phy->otg, false);
  142. if (dwc->usb2_generic_phy)
  143. phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
  144. ret = dwc3_gadget_init(dwc);
  145. if (ret)
  146. dev_err(dwc->dev, "failed to initialize peripheral\n");
  147. break;
  148. default:
  149. break;
  150. }
  151. }
  152. void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
  153. {
  154. unsigned long flags;
  155. spin_lock_irqsave(&dwc->lock, flags);
  156. dwc->desired_dr_role = mode;
  157. spin_unlock_irqrestore(&dwc->lock, flags);
  158. queue_work(system_power_efficient_wq, &dwc->drd_work);
  159. }
  160. u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
  161. {
  162. struct dwc3 *dwc = dep->dwc;
  163. u32 reg;
  164. dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
  165. DWC3_GDBGFIFOSPACE_NUM(dep->number) |
  166. DWC3_GDBGFIFOSPACE_TYPE(type));
  167. reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
  168. return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
  169. }
  170. /**
  171. * dwc3_core_soft_reset - Issues core soft reset and PHY reset
  172. * @dwc: pointer to our context structure
  173. */
  174. static int dwc3_core_soft_reset(struct dwc3 *dwc)
  175. {
  176. u32 reg;
  177. int retries = 1000;
  178. int ret;
  179. usb_phy_init(dwc->usb2_phy);
  180. usb_phy_init(dwc->usb3_phy);
  181. ret = phy_init(dwc->usb2_generic_phy);
  182. if (ret < 0)
  183. return ret;
  184. ret = phy_init(dwc->usb3_generic_phy);
  185. if (ret < 0) {
  186. phy_exit(dwc->usb2_generic_phy);
  187. return ret;
  188. }
  189. /*
  190. * We're resetting only the device side because, if we're in host mode,
  191. * XHCI driver will reset the host block. If dwc3 was configured for
  192. * host-only mode, then we can return early.
  193. */
  194. if (dwc->dr_mode == USB_DR_MODE_HOST)
  195. return 0;
  196. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  197. reg |= DWC3_DCTL_CSFTRST;
  198. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  199. do {
  200. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  201. if (!(reg & DWC3_DCTL_CSFTRST))
  202. return 0;
  203. udelay(1);
  204. } while (--retries);
  205. return -ETIMEDOUT;
  206. }
  207. /*
  208. * dwc3_frame_length_adjustment - Adjusts frame length if required
  209. * @dwc3: Pointer to our controller context structure
  210. */
  211. static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
  212. {
  213. u32 reg;
  214. u32 dft;
  215. if (dwc->revision < DWC3_REVISION_250A)
  216. return;
  217. if (dwc->fladj == 0)
  218. return;
  219. reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
  220. dft = reg & DWC3_GFLADJ_30MHZ_MASK;
  221. if (!dev_WARN_ONCE(dwc->dev, dft == dwc->fladj,
  222. "request value same as default, ignoring\n")) {
  223. reg &= ~DWC3_GFLADJ_30MHZ_MASK;
  224. reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
  225. dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
  226. }
  227. }
  228. /**
  229. * dwc3_free_one_event_buffer - Frees one event buffer
  230. * @dwc: Pointer to our controller context structure
  231. * @evt: Pointer to event buffer to be freed
  232. */
  233. static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
  234. struct dwc3_event_buffer *evt)
  235. {
  236. dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma);
  237. }
  238. /**
  239. * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
  240. * @dwc: Pointer to our controller context structure
  241. * @length: size of the event buffer
  242. *
  243. * Returns a pointer to the allocated event buffer structure on success
  244. * otherwise ERR_PTR(errno).
  245. */
  246. static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
  247. unsigned length)
  248. {
  249. struct dwc3_event_buffer *evt;
  250. evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
  251. if (!evt)
  252. return ERR_PTR(-ENOMEM);
  253. evt->dwc = dwc;
  254. evt->length = length;
  255. evt->cache = devm_kzalloc(dwc->dev, length, GFP_KERNEL);
  256. if (!evt->cache)
  257. return ERR_PTR(-ENOMEM);
  258. evt->buf = dma_alloc_coherent(dwc->sysdev, length,
  259. &evt->dma, GFP_KERNEL);
  260. if (!evt->buf)
  261. return ERR_PTR(-ENOMEM);
  262. return evt;
  263. }
  264. /**
  265. * dwc3_free_event_buffers - frees all allocated event buffers
  266. * @dwc: Pointer to our controller context structure
  267. */
  268. static void dwc3_free_event_buffers(struct dwc3 *dwc)
  269. {
  270. struct dwc3_event_buffer *evt;
  271. evt = dwc->ev_buf;
  272. if (evt)
  273. dwc3_free_one_event_buffer(dwc, evt);
  274. }
  275. /**
  276. * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
  277. * @dwc: pointer to our controller context structure
  278. * @length: size of event buffer
  279. *
  280. * Returns 0 on success otherwise negative errno. In the error case, dwc
  281. * may contain some buffers allocated but not all which were requested.
  282. */
  283. static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
  284. {
  285. struct dwc3_event_buffer *evt;
  286. evt = dwc3_alloc_one_event_buffer(dwc, length);
  287. if (IS_ERR(evt)) {
  288. dev_err(dwc->dev, "can't allocate event buffer\n");
  289. return PTR_ERR(evt);
  290. }
  291. dwc->ev_buf = evt;
  292. return 0;
  293. }
  294. /**
  295. * dwc3_event_buffers_setup - setup our allocated event buffers
  296. * @dwc: pointer to our controller context structure
  297. *
  298. * Returns 0 on success otherwise negative errno.
  299. */
  300. static int dwc3_event_buffers_setup(struct dwc3 *dwc)
  301. {
  302. struct dwc3_event_buffer *evt;
  303. evt = dwc->ev_buf;
  304. evt->lpos = 0;
  305. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
  306. lower_32_bits(evt->dma));
  307. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
  308. upper_32_bits(evt->dma));
  309. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
  310. DWC3_GEVNTSIZ_SIZE(evt->length));
  311. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
  312. return 0;
  313. }
  314. static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
  315. {
  316. struct dwc3_event_buffer *evt;
  317. evt = dwc->ev_buf;
  318. evt->lpos = 0;
  319. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
  320. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
  321. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
  322. | DWC3_GEVNTSIZ_SIZE(0));
  323. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
  324. }
  325. static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
  326. {
  327. if (!dwc->has_hibernation)
  328. return 0;
  329. if (!dwc->nr_scratch)
  330. return 0;
  331. dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
  332. DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
  333. if (!dwc->scratchbuf)
  334. return -ENOMEM;
  335. return 0;
  336. }
  337. static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
  338. {
  339. dma_addr_t scratch_addr;
  340. u32 param;
  341. int ret;
  342. if (!dwc->has_hibernation)
  343. return 0;
  344. if (!dwc->nr_scratch)
  345. return 0;
  346. /* should never fall here */
  347. if (!WARN_ON(dwc->scratchbuf))
  348. return 0;
  349. scratch_addr = dma_map_single(dwc->sysdev, dwc->scratchbuf,
  350. dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
  351. DMA_BIDIRECTIONAL);
  352. if (dma_mapping_error(dwc->sysdev, scratch_addr)) {
  353. dev_err(dwc->sysdev, "failed to map scratch buffer\n");
  354. ret = -EFAULT;
  355. goto err0;
  356. }
  357. dwc->scratch_addr = scratch_addr;
  358. param = lower_32_bits(scratch_addr);
  359. ret = dwc3_send_gadget_generic_command(dwc,
  360. DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
  361. if (ret < 0)
  362. goto err1;
  363. param = upper_32_bits(scratch_addr);
  364. ret = dwc3_send_gadget_generic_command(dwc,
  365. DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
  366. if (ret < 0)
  367. goto err1;
  368. return 0;
  369. err1:
  370. dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
  371. DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
  372. err0:
  373. return ret;
  374. }
  375. static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
  376. {
  377. if (!dwc->has_hibernation)
  378. return;
  379. if (!dwc->nr_scratch)
  380. return;
  381. /* should never fall here */
  382. if (!WARN_ON(dwc->scratchbuf))
  383. return;
  384. dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
  385. DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
  386. kfree(dwc->scratchbuf);
  387. }
  388. static void dwc3_core_num_eps(struct dwc3 *dwc)
  389. {
  390. struct dwc3_hwparams *parms = &dwc->hwparams;
  391. dwc->num_eps = DWC3_NUM_EPS(parms);
  392. }
  393. static void dwc3_cache_hwparams(struct dwc3 *dwc)
  394. {
  395. struct dwc3_hwparams *parms = &dwc->hwparams;
  396. parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
  397. parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
  398. parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
  399. parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
  400. parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
  401. parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
  402. parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
  403. parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
  404. parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
  405. }
  406. /**
  407. * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
  408. * @dwc: Pointer to our controller context structure
  409. *
  410. * Returns 0 on success. The USB PHY interfaces are configured but not
  411. * initialized. The PHY interfaces and the PHYs get initialized together with
  412. * the core in dwc3_core_init.
  413. */
  414. static int dwc3_phy_setup(struct dwc3 *dwc)
  415. {
  416. u32 reg;
  417. int ret;
  418. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  419. /*
  420. * Make sure UX_EXIT_PX is cleared as that causes issues with some
  421. * PHYs. Also, this bit is not supposed to be used in normal operation.
  422. */
  423. reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX;
  424. /*
  425. * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
  426. * to '0' during coreConsultant configuration. So default value
  427. * will be '0' when the core is reset. Application needs to set it
  428. * to '1' after the core initialization is completed.
  429. */
  430. if (dwc->revision > DWC3_REVISION_194A)
  431. reg |= DWC3_GUSB3PIPECTL_SUSPHY;
  432. if (dwc->u2ss_inp3_quirk)
  433. reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
  434. if (dwc->dis_rxdet_inp3_quirk)
  435. reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
  436. if (dwc->req_p1p2p3_quirk)
  437. reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
  438. if (dwc->del_p1p2p3_quirk)
  439. reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
  440. if (dwc->del_phy_power_chg_quirk)
  441. reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
  442. if (dwc->lfps_filter_quirk)
  443. reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
  444. if (dwc->rx_detect_poll_quirk)
  445. reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
  446. if (dwc->tx_de_emphasis_quirk)
  447. reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
  448. if (dwc->dis_u3_susphy_quirk)
  449. reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
  450. if (dwc->dis_del_phy_power_chg_quirk)
  451. reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
  452. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  453. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  454. /* Select the HS PHY interface */
  455. switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
  456. case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
  457. if (dwc->hsphy_interface &&
  458. !strncmp(dwc->hsphy_interface, "utmi", 4)) {
  459. reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
  460. break;
  461. } else if (dwc->hsphy_interface &&
  462. !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
  463. reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
  464. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  465. } else {
  466. /* Relying on default value. */
  467. if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
  468. break;
  469. }
  470. /* FALLTHROUGH */
  471. case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
  472. ret = dwc3_ulpi_init(dwc);
  473. if (ret)
  474. return ret;
  475. /* FALLTHROUGH */
  476. default:
  477. break;
  478. }
  479. switch (dwc->hsphy_mode) {
  480. case USBPHY_INTERFACE_MODE_UTMI:
  481. reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
  482. DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
  483. reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
  484. DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
  485. break;
  486. case USBPHY_INTERFACE_MODE_UTMIW:
  487. reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
  488. DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
  489. reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
  490. DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
  491. break;
  492. default:
  493. break;
  494. }
  495. /*
  496. * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
  497. * '0' during coreConsultant configuration. So default value will
  498. * be '0' when the core is reset. Application needs to set it to
  499. * '1' after the core initialization is completed.
  500. */
  501. if (dwc->revision > DWC3_REVISION_194A)
  502. reg |= DWC3_GUSB2PHYCFG_SUSPHY;
  503. if (dwc->dis_u2_susphy_quirk)
  504. reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
  505. if (dwc->dis_enblslpm_quirk)
  506. reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
  507. if (dwc->dis_u2_freeclk_exists_quirk)
  508. reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
  509. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  510. return 0;
  511. }
  512. static void dwc3_core_exit(struct dwc3 *dwc)
  513. {
  514. dwc3_event_buffers_cleanup(dwc);
  515. usb_phy_shutdown(dwc->usb2_phy);
  516. usb_phy_shutdown(dwc->usb3_phy);
  517. phy_exit(dwc->usb2_generic_phy);
  518. phy_exit(dwc->usb3_generic_phy);
  519. usb_phy_set_suspend(dwc->usb2_phy, 1);
  520. usb_phy_set_suspend(dwc->usb3_phy, 1);
  521. phy_power_off(dwc->usb2_generic_phy);
  522. phy_power_off(dwc->usb3_generic_phy);
  523. }
  524. static bool dwc3_core_is_valid(struct dwc3 *dwc)
  525. {
  526. u32 reg;
  527. reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
  528. /* This should read as U3 followed by revision number */
  529. if ((reg & DWC3_GSNPSID_MASK) == 0x55330000) {
  530. /* Detected DWC_usb3 IP */
  531. dwc->revision = reg;
  532. } else if ((reg & DWC3_GSNPSID_MASK) == 0x33310000) {
  533. /* Detected DWC_usb31 IP */
  534. dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
  535. dwc->revision |= DWC3_REVISION_IS_DWC31;
  536. } else {
  537. return false;
  538. }
  539. return true;
  540. }
  541. static void dwc3_core_setup_global_control(struct dwc3 *dwc)
  542. {
  543. u32 hwparams4 = dwc->hwparams.hwparams4;
  544. u32 reg;
  545. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  546. reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
  547. switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
  548. case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
  549. /**
  550. * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
  551. * issue which would cause xHCI compliance tests to fail.
  552. *
  553. * Because of that we cannot enable clock gating on such
  554. * configurations.
  555. *
  556. * Refers to:
  557. *
  558. * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
  559. * SOF/ITP Mode Used
  560. */
  561. if ((dwc->dr_mode == USB_DR_MODE_HOST ||
  562. dwc->dr_mode == USB_DR_MODE_OTG) &&
  563. (dwc->revision >= DWC3_REVISION_210A &&
  564. dwc->revision <= DWC3_REVISION_250A))
  565. reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
  566. else
  567. reg &= ~DWC3_GCTL_DSBLCLKGTNG;
  568. break;
  569. case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
  570. /* enable hibernation here */
  571. dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
  572. /*
  573. * REVISIT Enabling this bit so that host-mode hibernation
  574. * will work. Device-mode hibernation is not yet implemented.
  575. */
  576. reg |= DWC3_GCTL_GBLHIBERNATIONEN;
  577. break;
  578. default:
  579. /* nothing */
  580. break;
  581. }
  582. /* check if current dwc3 is on simulation board */
  583. if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
  584. dev_info(dwc->dev, "Running with FPGA optmizations\n");
  585. dwc->is_fpga = true;
  586. }
  587. WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
  588. "disable_scramble cannot be used on non-FPGA builds\n");
  589. if (dwc->disable_scramble_quirk && dwc->is_fpga)
  590. reg |= DWC3_GCTL_DISSCRAMBLE;
  591. else
  592. reg &= ~DWC3_GCTL_DISSCRAMBLE;
  593. if (dwc->u2exit_lfps_quirk)
  594. reg |= DWC3_GCTL_U2EXIT_LFPS;
  595. /*
  596. * WORKAROUND: DWC3 revisions <1.90a have a bug
  597. * where the device can fail to connect at SuperSpeed
  598. * and falls back to high-speed mode which causes
  599. * the device to enter a Connect/Disconnect loop
  600. */
  601. if (dwc->revision < DWC3_REVISION_190A)
  602. reg |= DWC3_GCTL_U2RSTECN;
  603. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  604. }
  605. static int dwc3_core_get_phy(struct dwc3 *dwc);
  606. /**
  607. * dwc3_core_init - Low-level initialization of DWC3 Core
  608. * @dwc: Pointer to our controller context structure
  609. *
  610. * Returns 0 on success otherwise negative errno.
  611. */
  612. static int dwc3_core_init(struct dwc3 *dwc)
  613. {
  614. u32 reg;
  615. int ret;
  616. if (!dwc3_core_is_valid(dwc)) {
  617. dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
  618. ret = -ENODEV;
  619. goto err0;
  620. }
  621. /*
  622. * Write Linux Version Code to our GUID register so it's easy to figure
  623. * out which kernel version a bug was found.
  624. */
  625. dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
  626. /* Handle USB2.0-only core configuration */
  627. if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
  628. DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
  629. if (dwc->maximum_speed == USB_SPEED_SUPER)
  630. dwc->maximum_speed = USB_SPEED_HIGH;
  631. }
  632. ret = dwc3_core_get_phy(dwc);
  633. if (ret)
  634. goto err0;
  635. ret = dwc3_core_soft_reset(dwc);
  636. if (ret)
  637. goto err0;
  638. ret = dwc3_phy_setup(dwc);
  639. if (ret)
  640. goto err0;
  641. dwc3_core_setup_global_control(dwc);
  642. dwc3_core_num_eps(dwc);
  643. ret = dwc3_setup_scratch_buffers(dwc);
  644. if (ret)
  645. goto err1;
  646. /* Adjust Frame Length */
  647. dwc3_frame_length_adjustment(dwc);
  648. usb_phy_set_suspend(dwc->usb2_phy, 0);
  649. usb_phy_set_suspend(dwc->usb3_phy, 0);
  650. ret = phy_power_on(dwc->usb2_generic_phy);
  651. if (ret < 0)
  652. goto err2;
  653. ret = phy_power_on(dwc->usb3_generic_phy);
  654. if (ret < 0)
  655. goto err3;
  656. ret = dwc3_event_buffers_setup(dwc);
  657. if (ret) {
  658. dev_err(dwc->dev, "failed to setup event buffers\n");
  659. goto err4;
  660. }
  661. /*
  662. * ENDXFER polling is available on version 3.10a and later of
  663. * the DWC_usb3 controller. It is NOT available in the
  664. * DWC_usb31 controller.
  665. */
  666. if (!dwc3_is_usb31(dwc) && dwc->revision >= DWC3_REVISION_310A) {
  667. reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
  668. reg |= DWC3_GUCTL2_RST_ACTBITLATER;
  669. dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
  670. }
  671. if (dwc->revision >= DWC3_REVISION_250A) {
  672. reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
  673. /*
  674. * Enable hardware control of sending remote wakeup
  675. * in HS when the device is in the L1 state.
  676. */
  677. if (dwc->revision >= DWC3_REVISION_290A)
  678. reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
  679. if (dwc->dis_tx_ipgap_linecheck_quirk)
  680. reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
  681. dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
  682. }
  683. return 0;
  684. err4:
  685. phy_power_off(dwc->usb3_generic_phy);
  686. err3:
  687. phy_power_off(dwc->usb2_generic_phy);
  688. err2:
  689. usb_phy_set_suspend(dwc->usb2_phy, 1);
  690. usb_phy_set_suspend(dwc->usb3_phy, 1);
  691. err1:
  692. usb_phy_shutdown(dwc->usb2_phy);
  693. usb_phy_shutdown(dwc->usb3_phy);
  694. phy_exit(dwc->usb2_generic_phy);
  695. phy_exit(dwc->usb3_generic_phy);
  696. err0:
  697. return ret;
  698. }
  699. static int dwc3_core_get_phy(struct dwc3 *dwc)
  700. {
  701. struct device *dev = dwc->dev;
  702. struct device_node *node = dev->of_node;
  703. int ret;
  704. if (node) {
  705. dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
  706. dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
  707. } else {
  708. dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
  709. dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
  710. }
  711. if (IS_ERR(dwc->usb2_phy)) {
  712. ret = PTR_ERR(dwc->usb2_phy);
  713. if (ret == -ENXIO || ret == -ENODEV) {
  714. dwc->usb2_phy = NULL;
  715. } else if (ret == -EPROBE_DEFER) {
  716. return ret;
  717. } else {
  718. dev_err(dev, "no usb2 phy configured\n");
  719. return ret;
  720. }
  721. }
  722. if (IS_ERR(dwc->usb3_phy)) {
  723. ret = PTR_ERR(dwc->usb3_phy);
  724. if (ret == -ENXIO || ret == -ENODEV) {
  725. dwc->usb3_phy = NULL;
  726. } else if (ret == -EPROBE_DEFER) {
  727. return ret;
  728. } else {
  729. dev_err(dev, "no usb3 phy configured\n");
  730. return ret;
  731. }
  732. }
  733. dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
  734. if (IS_ERR(dwc->usb2_generic_phy)) {
  735. ret = PTR_ERR(dwc->usb2_generic_phy);
  736. if (ret == -ENOSYS || ret == -ENODEV) {
  737. dwc->usb2_generic_phy = NULL;
  738. } else if (ret == -EPROBE_DEFER) {
  739. return ret;
  740. } else {
  741. dev_err(dev, "no usb2 phy configured\n");
  742. return ret;
  743. }
  744. }
  745. dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
  746. if (IS_ERR(dwc->usb3_generic_phy)) {
  747. ret = PTR_ERR(dwc->usb3_generic_phy);
  748. if (ret == -ENOSYS || ret == -ENODEV) {
  749. dwc->usb3_generic_phy = NULL;
  750. } else if (ret == -EPROBE_DEFER) {
  751. return ret;
  752. } else {
  753. dev_err(dev, "no usb3 phy configured\n");
  754. return ret;
  755. }
  756. }
  757. return 0;
  758. }
  759. static int dwc3_core_init_mode(struct dwc3 *dwc)
  760. {
  761. struct device *dev = dwc->dev;
  762. int ret;
  763. switch (dwc->dr_mode) {
  764. case USB_DR_MODE_PERIPHERAL:
  765. dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
  766. if (dwc->usb2_phy)
  767. otg_set_vbus(dwc->usb2_phy->otg, false);
  768. if (dwc->usb2_generic_phy)
  769. phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
  770. ret = dwc3_gadget_init(dwc);
  771. if (ret) {
  772. if (ret != -EPROBE_DEFER)
  773. dev_err(dev, "failed to initialize gadget\n");
  774. return ret;
  775. }
  776. break;
  777. case USB_DR_MODE_HOST:
  778. dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
  779. if (dwc->usb2_phy)
  780. otg_set_vbus(dwc->usb2_phy->otg, true);
  781. if (dwc->usb2_generic_phy)
  782. phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
  783. ret = dwc3_host_init(dwc);
  784. if (ret) {
  785. if (ret != -EPROBE_DEFER)
  786. dev_err(dev, "failed to initialize host\n");
  787. return ret;
  788. }
  789. break;
  790. case USB_DR_MODE_OTG:
  791. INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
  792. ret = dwc3_drd_init(dwc);
  793. if (ret) {
  794. if (ret != -EPROBE_DEFER)
  795. dev_err(dev, "failed to initialize dual-role\n");
  796. return ret;
  797. }
  798. break;
  799. default:
  800. dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
  801. return -EINVAL;
  802. }
  803. return 0;
  804. }
  805. static void dwc3_core_exit_mode(struct dwc3 *dwc)
  806. {
  807. switch (dwc->dr_mode) {
  808. case USB_DR_MODE_PERIPHERAL:
  809. dwc3_gadget_exit(dwc);
  810. break;
  811. case USB_DR_MODE_HOST:
  812. dwc3_host_exit(dwc);
  813. break;
  814. case USB_DR_MODE_OTG:
  815. dwc3_drd_exit(dwc);
  816. break;
  817. default:
  818. /* do nothing */
  819. break;
  820. }
  821. }
  822. static void dwc3_get_properties(struct dwc3 *dwc)
  823. {
  824. struct device *dev = dwc->dev;
  825. u8 lpm_nyet_threshold;
  826. u8 tx_de_emphasis;
  827. u8 hird_threshold;
  828. /* default to highest possible threshold */
  829. lpm_nyet_threshold = 0xff;
  830. /* default to -3.5dB de-emphasis */
  831. tx_de_emphasis = 1;
  832. /*
  833. * default to assert utmi_sleep_n and use maximum allowed HIRD
  834. * threshold value of 0b1100
  835. */
  836. hird_threshold = 12;
  837. dwc->maximum_speed = usb_get_maximum_speed(dev);
  838. dwc->dr_mode = usb_get_dr_mode(dev);
  839. dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
  840. dwc->sysdev_is_parent = device_property_read_bool(dev,
  841. "linux,sysdev_is_parent");
  842. if (dwc->sysdev_is_parent)
  843. dwc->sysdev = dwc->dev->parent;
  844. else
  845. dwc->sysdev = dwc->dev;
  846. dwc->has_lpm_erratum = device_property_read_bool(dev,
  847. "snps,has-lpm-erratum");
  848. device_property_read_u8(dev, "snps,lpm-nyet-threshold",
  849. &lpm_nyet_threshold);
  850. dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
  851. "snps,is-utmi-l1-suspend");
  852. device_property_read_u8(dev, "snps,hird-threshold",
  853. &hird_threshold);
  854. dwc->usb3_lpm_capable = device_property_read_bool(dev,
  855. "snps,usb3_lpm_capable");
  856. dwc->disable_scramble_quirk = device_property_read_bool(dev,
  857. "snps,disable_scramble_quirk");
  858. dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
  859. "snps,u2exit_lfps_quirk");
  860. dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
  861. "snps,u2ss_inp3_quirk");
  862. dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
  863. "snps,req_p1p2p3_quirk");
  864. dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
  865. "snps,del_p1p2p3_quirk");
  866. dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
  867. "snps,del_phy_power_chg_quirk");
  868. dwc->lfps_filter_quirk = device_property_read_bool(dev,
  869. "snps,lfps_filter_quirk");
  870. dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
  871. "snps,rx_detect_poll_quirk");
  872. dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
  873. "snps,dis_u3_susphy_quirk");
  874. dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
  875. "snps,dis_u2_susphy_quirk");
  876. dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
  877. "snps,dis_enblslpm_quirk");
  878. dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
  879. "snps,dis_rxdet_inp3_quirk");
  880. dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
  881. "snps,dis-u2-freeclk-exists-quirk");
  882. dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
  883. "snps,dis-del-phy-power-chg-quirk");
  884. dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev,
  885. "snps,dis-tx-ipgap-linecheck-quirk");
  886. dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
  887. "snps,tx_de_emphasis_quirk");
  888. device_property_read_u8(dev, "snps,tx_de_emphasis",
  889. &tx_de_emphasis);
  890. device_property_read_string(dev, "snps,hsphy_interface",
  891. &dwc->hsphy_interface);
  892. device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
  893. &dwc->fladj);
  894. dwc->lpm_nyet_threshold = lpm_nyet_threshold;
  895. dwc->tx_de_emphasis = tx_de_emphasis;
  896. dwc->hird_threshold = hird_threshold
  897. | (dwc->is_utmi_l1_suspend << 4);
  898. dwc->imod_interval = 0;
  899. }
  900. /* check whether the core supports IMOD */
  901. bool dwc3_has_imod(struct dwc3 *dwc)
  902. {
  903. return ((dwc3_is_usb3(dwc) &&
  904. dwc->revision >= DWC3_REVISION_300A) ||
  905. (dwc3_is_usb31(dwc) &&
  906. dwc->revision >= DWC3_USB31_REVISION_120A));
  907. }
  908. static void dwc3_check_params(struct dwc3 *dwc)
  909. {
  910. struct device *dev = dwc->dev;
  911. /* Check for proper value of imod_interval */
  912. if (dwc->imod_interval && !dwc3_has_imod(dwc)) {
  913. dev_warn(dwc->dev, "Interrupt moderation not supported\n");
  914. dwc->imod_interval = 0;
  915. }
  916. /*
  917. * Workaround for STAR 9000961433 which affects only version
  918. * 3.00a of the DWC_usb3 core. This prevents the controller
  919. * interrupt from being masked while handling events. IMOD
  920. * allows us to work around this issue. Enable it for the
  921. * affected version.
  922. */
  923. if (!dwc->imod_interval &&
  924. (dwc->revision == DWC3_REVISION_300A))
  925. dwc->imod_interval = 1;
  926. /* Check the maximum_speed parameter */
  927. switch (dwc->maximum_speed) {
  928. case USB_SPEED_LOW:
  929. case USB_SPEED_FULL:
  930. case USB_SPEED_HIGH:
  931. case USB_SPEED_SUPER:
  932. case USB_SPEED_SUPER_PLUS:
  933. break;
  934. default:
  935. dev_err(dev, "invalid maximum_speed parameter %d\n",
  936. dwc->maximum_speed);
  937. /* fall through */
  938. case USB_SPEED_UNKNOWN:
  939. /* default to superspeed */
  940. dwc->maximum_speed = USB_SPEED_SUPER;
  941. /*
  942. * default to superspeed plus if we are capable.
  943. */
  944. if (dwc3_is_usb31(dwc) &&
  945. (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
  946. DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
  947. dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
  948. break;
  949. }
  950. }
  951. static int dwc3_probe(struct platform_device *pdev)
  952. {
  953. struct device *dev = &pdev->dev;
  954. struct resource *res;
  955. struct dwc3 *dwc;
  956. int ret;
  957. void __iomem *regs;
  958. dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
  959. if (!dwc)
  960. return -ENOMEM;
  961. dwc->dev = dev;
  962. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  963. if (!res) {
  964. dev_err(dev, "missing memory resource\n");
  965. return -ENODEV;
  966. }
  967. dwc->xhci_resources[0].start = res->start;
  968. dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
  969. DWC3_XHCI_REGS_END;
  970. dwc->xhci_resources[0].flags = res->flags;
  971. dwc->xhci_resources[0].name = res->name;
  972. res->start += DWC3_GLOBALS_REGS_START;
  973. /*
  974. * Request memory region but exclude xHCI regs,
  975. * since it will be requested by the xhci-plat driver.
  976. */
  977. regs = devm_ioremap_resource(dev, res);
  978. if (IS_ERR(regs)) {
  979. ret = PTR_ERR(regs);
  980. goto err0;
  981. }
  982. dwc->regs = regs;
  983. dwc->regs_size = resource_size(res);
  984. dwc3_get_properties(dwc);
  985. platform_set_drvdata(pdev, dwc);
  986. dwc3_cache_hwparams(dwc);
  987. spin_lock_init(&dwc->lock);
  988. pm_runtime_set_active(dev);
  989. pm_runtime_use_autosuspend(dev);
  990. pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
  991. pm_runtime_enable(dev);
  992. ret = pm_runtime_get_sync(dev);
  993. if (ret < 0)
  994. goto err1;
  995. pm_runtime_forbid(dev);
  996. ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
  997. if (ret) {
  998. dev_err(dwc->dev, "failed to allocate event buffers\n");
  999. ret = -ENOMEM;
  1000. goto err2;
  1001. }
  1002. ret = dwc3_get_dr_mode(dwc);
  1003. if (ret)
  1004. goto err3;
  1005. ret = dwc3_alloc_scratch_buffers(dwc);
  1006. if (ret)
  1007. goto err3;
  1008. ret = dwc3_core_init(dwc);
  1009. if (ret) {
  1010. dev_err(dev, "failed to initialize core\n");
  1011. goto err4;
  1012. }
  1013. dwc3_check_params(dwc);
  1014. ret = dwc3_core_init_mode(dwc);
  1015. if (ret)
  1016. goto err5;
  1017. dwc3_debugfs_init(dwc);
  1018. pm_runtime_put(dev);
  1019. return 0;
  1020. err5:
  1021. dwc3_event_buffers_cleanup(dwc);
  1022. err4:
  1023. dwc3_free_scratch_buffers(dwc);
  1024. err3:
  1025. dwc3_free_event_buffers(dwc);
  1026. dwc3_ulpi_exit(dwc);
  1027. err2:
  1028. pm_runtime_allow(&pdev->dev);
  1029. err1:
  1030. pm_runtime_put_sync(&pdev->dev);
  1031. pm_runtime_disable(&pdev->dev);
  1032. err0:
  1033. /*
  1034. * restore res->start back to its original value so that, in case the
  1035. * probe is deferred, we don't end up getting error in request the
  1036. * memory region the next time probe is called.
  1037. */
  1038. res->start -= DWC3_GLOBALS_REGS_START;
  1039. return ret;
  1040. }
  1041. static int dwc3_remove(struct platform_device *pdev)
  1042. {
  1043. struct dwc3 *dwc = platform_get_drvdata(pdev);
  1044. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1045. pm_runtime_get_sync(&pdev->dev);
  1046. /*
  1047. * restore res->start back to its original value so that, in case the
  1048. * probe is deferred, we don't end up getting error in request the
  1049. * memory region the next time probe is called.
  1050. */
  1051. res->start -= DWC3_GLOBALS_REGS_START;
  1052. dwc3_debugfs_exit(dwc);
  1053. dwc3_core_exit_mode(dwc);
  1054. dwc3_core_exit(dwc);
  1055. dwc3_ulpi_exit(dwc);
  1056. pm_runtime_put_sync(&pdev->dev);
  1057. pm_runtime_allow(&pdev->dev);
  1058. pm_runtime_disable(&pdev->dev);
  1059. dwc3_free_event_buffers(dwc);
  1060. dwc3_free_scratch_buffers(dwc);
  1061. return 0;
  1062. }
  1063. #ifdef CONFIG_PM
  1064. static int dwc3_suspend_common(struct dwc3 *dwc)
  1065. {
  1066. unsigned long flags;
  1067. switch (dwc->dr_mode) {
  1068. case USB_DR_MODE_PERIPHERAL:
  1069. case USB_DR_MODE_OTG:
  1070. spin_lock_irqsave(&dwc->lock, flags);
  1071. dwc3_gadget_suspend(dwc);
  1072. spin_unlock_irqrestore(&dwc->lock, flags);
  1073. break;
  1074. case USB_DR_MODE_HOST:
  1075. default:
  1076. /* do nothing */
  1077. break;
  1078. }
  1079. dwc3_core_exit(dwc);
  1080. return 0;
  1081. }
  1082. static int dwc3_resume_common(struct dwc3 *dwc)
  1083. {
  1084. unsigned long flags;
  1085. int ret;
  1086. ret = dwc3_core_init(dwc);
  1087. if (ret)
  1088. return ret;
  1089. switch (dwc->dr_mode) {
  1090. case USB_DR_MODE_PERIPHERAL:
  1091. case USB_DR_MODE_OTG:
  1092. spin_lock_irqsave(&dwc->lock, flags);
  1093. dwc3_gadget_resume(dwc);
  1094. spin_unlock_irqrestore(&dwc->lock, flags);
  1095. /* FALLTHROUGH */
  1096. case USB_DR_MODE_HOST:
  1097. default:
  1098. /* do nothing */
  1099. break;
  1100. }
  1101. return 0;
  1102. }
  1103. static int dwc3_runtime_checks(struct dwc3 *dwc)
  1104. {
  1105. switch (dwc->dr_mode) {
  1106. case USB_DR_MODE_PERIPHERAL:
  1107. case USB_DR_MODE_OTG:
  1108. if (dwc->connected)
  1109. return -EBUSY;
  1110. break;
  1111. case USB_DR_MODE_HOST:
  1112. default:
  1113. /* do nothing */
  1114. break;
  1115. }
  1116. return 0;
  1117. }
  1118. static int dwc3_runtime_suspend(struct device *dev)
  1119. {
  1120. struct dwc3 *dwc = dev_get_drvdata(dev);
  1121. int ret;
  1122. if (dwc3_runtime_checks(dwc))
  1123. return -EBUSY;
  1124. ret = dwc3_suspend_common(dwc);
  1125. if (ret)
  1126. return ret;
  1127. device_init_wakeup(dev, true);
  1128. return 0;
  1129. }
  1130. static int dwc3_runtime_resume(struct device *dev)
  1131. {
  1132. struct dwc3 *dwc = dev_get_drvdata(dev);
  1133. int ret;
  1134. device_init_wakeup(dev, false);
  1135. ret = dwc3_resume_common(dwc);
  1136. if (ret)
  1137. return ret;
  1138. switch (dwc->dr_mode) {
  1139. case USB_DR_MODE_PERIPHERAL:
  1140. case USB_DR_MODE_OTG:
  1141. dwc3_gadget_process_pending_events(dwc);
  1142. break;
  1143. case USB_DR_MODE_HOST:
  1144. default:
  1145. /* do nothing */
  1146. break;
  1147. }
  1148. pm_runtime_mark_last_busy(dev);
  1149. pm_runtime_put(dev);
  1150. return 0;
  1151. }
  1152. static int dwc3_runtime_idle(struct device *dev)
  1153. {
  1154. struct dwc3 *dwc = dev_get_drvdata(dev);
  1155. switch (dwc->dr_mode) {
  1156. case USB_DR_MODE_PERIPHERAL:
  1157. case USB_DR_MODE_OTG:
  1158. if (dwc3_runtime_checks(dwc))
  1159. return -EBUSY;
  1160. break;
  1161. case USB_DR_MODE_HOST:
  1162. default:
  1163. /* do nothing */
  1164. break;
  1165. }
  1166. pm_runtime_mark_last_busy(dev);
  1167. pm_runtime_autosuspend(dev);
  1168. return 0;
  1169. }
  1170. #endif /* CONFIG_PM */
  1171. #ifdef CONFIG_PM_SLEEP
  1172. static int dwc3_suspend(struct device *dev)
  1173. {
  1174. struct dwc3 *dwc = dev_get_drvdata(dev);
  1175. int ret;
  1176. ret = dwc3_suspend_common(dwc);
  1177. if (ret)
  1178. return ret;
  1179. pinctrl_pm_select_sleep_state(dev);
  1180. return 0;
  1181. }
  1182. static int dwc3_resume(struct device *dev)
  1183. {
  1184. struct dwc3 *dwc = dev_get_drvdata(dev);
  1185. int ret;
  1186. pinctrl_pm_select_default_state(dev);
  1187. ret = dwc3_resume_common(dwc);
  1188. if (ret)
  1189. return ret;
  1190. pm_runtime_disable(dev);
  1191. pm_runtime_set_active(dev);
  1192. pm_runtime_enable(dev);
  1193. return 0;
  1194. }
  1195. #endif /* CONFIG_PM_SLEEP */
  1196. static const struct dev_pm_ops dwc3_dev_pm_ops = {
  1197. SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
  1198. SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
  1199. dwc3_runtime_idle)
  1200. };
  1201. #ifdef CONFIG_OF
  1202. static const struct of_device_id of_dwc3_match[] = {
  1203. {
  1204. .compatible = "snps,dwc3"
  1205. },
  1206. {
  1207. .compatible = "synopsys,dwc3"
  1208. },
  1209. { },
  1210. };
  1211. MODULE_DEVICE_TABLE(of, of_dwc3_match);
  1212. #endif
  1213. #ifdef CONFIG_ACPI
  1214. #define ACPI_ID_INTEL_BSW "808622B7"
  1215. static const struct acpi_device_id dwc3_acpi_match[] = {
  1216. { ACPI_ID_INTEL_BSW, 0 },
  1217. { },
  1218. };
  1219. MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
  1220. #endif
  1221. static struct platform_driver dwc3_driver = {
  1222. .probe = dwc3_probe,
  1223. .remove = dwc3_remove,
  1224. .driver = {
  1225. .name = "dwc3",
  1226. .of_match_table = of_match_ptr(of_dwc3_match),
  1227. .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
  1228. .pm = &dwc3_dev_pm_ops,
  1229. },
  1230. };
  1231. module_platform_driver(dwc3_driver);
  1232. MODULE_ALIAS("platform:dwc3");
  1233. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  1234. MODULE_LICENSE("GPL v2");
  1235. MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");