core.c 35 KB

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