core.c 39 KB

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