core.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  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_power_efficient_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. return 0;
  716. err4:
  717. phy_power_off(dwc->usb3_generic_phy);
  718. err3:
  719. phy_power_off(dwc->usb2_generic_phy);
  720. err2:
  721. usb_phy_set_suspend(dwc->usb2_phy, 1);
  722. usb_phy_set_suspend(dwc->usb3_phy, 1);
  723. err1:
  724. usb_phy_shutdown(dwc->usb2_phy);
  725. usb_phy_shutdown(dwc->usb3_phy);
  726. phy_exit(dwc->usb2_generic_phy);
  727. phy_exit(dwc->usb3_generic_phy);
  728. err0a:
  729. dwc3_ulpi_exit(dwc);
  730. err0:
  731. return ret;
  732. }
  733. static int dwc3_core_get_phy(struct dwc3 *dwc)
  734. {
  735. struct device *dev = dwc->dev;
  736. struct device_node *node = dev->of_node;
  737. int ret;
  738. if (node) {
  739. dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
  740. dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
  741. } else {
  742. dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
  743. dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
  744. }
  745. if (IS_ERR(dwc->usb2_phy)) {
  746. ret = PTR_ERR(dwc->usb2_phy);
  747. if (ret == -ENXIO || ret == -ENODEV) {
  748. dwc->usb2_phy = NULL;
  749. } else if (ret == -EPROBE_DEFER) {
  750. return ret;
  751. } else {
  752. dev_err(dev, "no usb2 phy configured\n");
  753. return ret;
  754. }
  755. }
  756. if (IS_ERR(dwc->usb3_phy)) {
  757. ret = PTR_ERR(dwc->usb3_phy);
  758. if (ret == -ENXIO || ret == -ENODEV) {
  759. dwc->usb3_phy = NULL;
  760. } else if (ret == -EPROBE_DEFER) {
  761. return ret;
  762. } else {
  763. dev_err(dev, "no usb3 phy configured\n");
  764. return ret;
  765. }
  766. }
  767. dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
  768. if (IS_ERR(dwc->usb2_generic_phy)) {
  769. ret = PTR_ERR(dwc->usb2_generic_phy);
  770. if (ret == -ENOSYS || ret == -ENODEV) {
  771. dwc->usb2_generic_phy = NULL;
  772. } else if (ret == -EPROBE_DEFER) {
  773. return ret;
  774. } else {
  775. dev_err(dev, "no usb2 phy configured\n");
  776. return ret;
  777. }
  778. }
  779. dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
  780. if (IS_ERR(dwc->usb3_generic_phy)) {
  781. ret = PTR_ERR(dwc->usb3_generic_phy);
  782. if (ret == -ENOSYS || ret == -ENODEV) {
  783. dwc->usb3_generic_phy = NULL;
  784. } else if (ret == -EPROBE_DEFER) {
  785. return ret;
  786. } else {
  787. dev_err(dev, "no usb3 phy configured\n");
  788. return ret;
  789. }
  790. }
  791. return 0;
  792. }
  793. static int dwc3_core_init_mode(struct dwc3 *dwc)
  794. {
  795. struct device *dev = dwc->dev;
  796. int ret;
  797. switch (dwc->dr_mode) {
  798. case USB_DR_MODE_PERIPHERAL:
  799. dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
  800. if (dwc->usb2_phy)
  801. otg_set_vbus(dwc->usb2_phy->otg, false);
  802. phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
  803. phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
  804. ret = dwc3_gadget_init(dwc);
  805. if (ret) {
  806. if (ret != -EPROBE_DEFER)
  807. dev_err(dev, "failed to initialize gadget\n");
  808. return ret;
  809. }
  810. break;
  811. case USB_DR_MODE_HOST:
  812. dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
  813. if (dwc->usb2_phy)
  814. otg_set_vbus(dwc->usb2_phy->otg, true);
  815. phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
  816. phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
  817. ret = dwc3_host_init(dwc);
  818. if (ret) {
  819. if (ret != -EPROBE_DEFER)
  820. dev_err(dev, "failed to initialize host\n");
  821. return ret;
  822. }
  823. phy_calibrate(dwc->usb2_generic_phy);
  824. break;
  825. case USB_DR_MODE_OTG:
  826. INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
  827. ret = dwc3_drd_init(dwc);
  828. if (ret) {
  829. if (ret != -EPROBE_DEFER)
  830. dev_err(dev, "failed to initialize dual-role\n");
  831. return ret;
  832. }
  833. break;
  834. default:
  835. dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
  836. return -EINVAL;
  837. }
  838. return 0;
  839. }
  840. static void dwc3_core_exit_mode(struct dwc3 *dwc)
  841. {
  842. switch (dwc->dr_mode) {
  843. case USB_DR_MODE_PERIPHERAL:
  844. dwc3_gadget_exit(dwc);
  845. break;
  846. case USB_DR_MODE_HOST:
  847. dwc3_host_exit(dwc);
  848. break;
  849. case USB_DR_MODE_OTG:
  850. dwc3_drd_exit(dwc);
  851. break;
  852. default:
  853. /* do nothing */
  854. break;
  855. }
  856. }
  857. static void dwc3_get_properties(struct dwc3 *dwc)
  858. {
  859. struct device *dev = dwc->dev;
  860. u8 lpm_nyet_threshold;
  861. u8 tx_de_emphasis;
  862. u8 hird_threshold;
  863. /* default to highest possible threshold */
  864. lpm_nyet_threshold = 0xff;
  865. /* default to -3.5dB de-emphasis */
  866. tx_de_emphasis = 1;
  867. /*
  868. * default to assert utmi_sleep_n and use maximum allowed HIRD
  869. * threshold value of 0b1100
  870. */
  871. hird_threshold = 12;
  872. dwc->maximum_speed = usb_get_maximum_speed(dev);
  873. dwc->dr_mode = usb_get_dr_mode(dev);
  874. dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
  875. dwc->sysdev_is_parent = device_property_read_bool(dev,
  876. "linux,sysdev_is_parent");
  877. if (dwc->sysdev_is_parent)
  878. dwc->sysdev = dwc->dev->parent;
  879. else
  880. dwc->sysdev = dwc->dev;
  881. dwc->has_lpm_erratum = device_property_read_bool(dev,
  882. "snps,has-lpm-erratum");
  883. device_property_read_u8(dev, "snps,lpm-nyet-threshold",
  884. &lpm_nyet_threshold);
  885. dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
  886. "snps,is-utmi-l1-suspend");
  887. device_property_read_u8(dev, "snps,hird-threshold",
  888. &hird_threshold);
  889. dwc->usb3_lpm_capable = device_property_read_bool(dev,
  890. "snps,usb3_lpm_capable");
  891. dwc->disable_scramble_quirk = device_property_read_bool(dev,
  892. "snps,disable_scramble_quirk");
  893. dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
  894. "snps,u2exit_lfps_quirk");
  895. dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
  896. "snps,u2ss_inp3_quirk");
  897. dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
  898. "snps,req_p1p2p3_quirk");
  899. dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
  900. "snps,del_p1p2p3_quirk");
  901. dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
  902. "snps,del_phy_power_chg_quirk");
  903. dwc->lfps_filter_quirk = device_property_read_bool(dev,
  904. "snps,lfps_filter_quirk");
  905. dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
  906. "snps,rx_detect_poll_quirk");
  907. dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
  908. "snps,dis_u3_susphy_quirk");
  909. dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
  910. "snps,dis_u2_susphy_quirk");
  911. dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
  912. "snps,dis_enblslpm_quirk");
  913. dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
  914. "snps,dis_rxdet_inp3_quirk");
  915. dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
  916. "snps,dis-u2-freeclk-exists-quirk");
  917. dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
  918. "snps,dis-del-phy-power-chg-quirk");
  919. dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev,
  920. "snps,dis-tx-ipgap-linecheck-quirk");
  921. dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
  922. "snps,tx_de_emphasis_quirk");
  923. device_property_read_u8(dev, "snps,tx_de_emphasis",
  924. &tx_de_emphasis);
  925. device_property_read_string(dev, "snps,hsphy_interface",
  926. &dwc->hsphy_interface);
  927. device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
  928. &dwc->fladj);
  929. dwc->dis_metastability_quirk = device_property_read_bool(dev,
  930. "snps,dis_metastability_quirk");
  931. dwc->lpm_nyet_threshold = lpm_nyet_threshold;
  932. dwc->tx_de_emphasis = tx_de_emphasis;
  933. dwc->hird_threshold = hird_threshold
  934. | (dwc->is_utmi_l1_suspend << 4);
  935. dwc->imod_interval = 0;
  936. }
  937. /* check whether the core supports IMOD */
  938. bool dwc3_has_imod(struct dwc3 *dwc)
  939. {
  940. return ((dwc3_is_usb3(dwc) &&
  941. dwc->revision >= DWC3_REVISION_300A) ||
  942. (dwc3_is_usb31(dwc) &&
  943. dwc->revision >= DWC3_USB31_REVISION_120A));
  944. }
  945. static void dwc3_check_params(struct dwc3 *dwc)
  946. {
  947. struct device *dev = dwc->dev;
  948. /* Check for proper value of imod_interval */
  949. if (dwc->imod_interval && !dwc3_has_imod(dwc)) {
  950. dev_warn(dwc->dev, "Interrupt moderation not supported\n");
  951. dwc->imod_interval = 0;
  952. }
  953. /*
  954. * Workaround for STAR 9000961433 which affects only version
  955. * 3.00a of the DWC_usb3 core. This prevents the controller
  956. * interrupt from being masked while handling events. IMOD
  957. * allows us to work around this issue. Enable it for the
  958. * affected version.
  959. */
  960. if (!dwc->imod_interval &&
  961. (dwc->revision == DWC3_REVISION_300A))
  962. dwc->imod_interval = 1;
  963. /* Check the maximum_speed parameter */
  964. switch (dwc->maximum_speed) {
  965. case USB_SPEED_LOW:
  966. case USB_SPEED_FULL:
  967. case USB_SPEED_HIGH:
  968. case USB_SPEED_SUPER:
  969. case USB_SPEED_SUPER_PLUS:
  970. break;
  971. default:
  972. dev_err(dev, "invalid maximum_speed parameter %d\n",
  973. dwc->maximum_speed);
  974. /* fall through */
  975. case USB_SPEED_UNKNOWN:
  976. /* default to superspeed */
  977. dwc->maximum_speed = USB_SPEED_SUPER;
  978. /*
  979. * default to superspeed plus if we are capable.
  980. */
  981. if (dwc3_is_usb31(dwc) &&
  982. (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
  983. DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
  984. dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
  985. break;
  986. }
  987. }
  988. static int dwc3_probe(struct platform_device *pdev)
  989. {
  990. struct device *dev = &pdev->dev;
  991. struct resource *res;
  992. struct dwc3 *dwc;
  993. int ret;
  994. void __iomem *regs;
  995. dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
  996. if (!dwc)
  997. return -ENOMEM;
  998. dwc->dev = dev;
  999. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1000. if (!res) {
  1001. dev_err(dev, "missing memory resource\n");
  1002. return -ENODEV;
  1003. }
  1004. dwc->xhci_resources[0].start = res->start;
  1005. dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
  1006. DWC3_XHCI_REGS_END;
  1007. dwc->xhci_resources[0].flags = res->flags;
  1008. dwc->xhci_resources[0].name = res->name;
  1009. res->start += DWC3_GLOBALS_REGS_START;
  1010. /*
  1011. * Request memory region but exclude xHCI regs,
  1012. * since it will be requested by the xhci-plat driver.
  1013. */
  1014. regs = devm_ioremap_resource(dev, res);
  1015. if (IS_ERR(regs)) {
  1016. ret = PTR_ERR(regs);
  1017. goto err0;
  1018. }
  1019. dwc->regs = regs;
  1020. dwc->regs_size = resource_size(res);
  1021. dwc3_get_properties(dwc);
  1022. platform_set_drvdata(pdev, dwc);
  1023. dwc3_cache_hwparams(dwc);
  1024. spin_lock_init(&dwc->lock);
  1025. pm_runtime_set_active(dev);
  1026. pm_runtime_use_autosuspend(dev);
  1027. pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
  1028. pm_runtime_enable(dev);
  1029. ret = pm_runtime_get_sync(dev);
  1030. if (ret < 0)
  1031. goto err1;
  1032. pm_runtime_forbid(dev);
  1033. ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
  1034. if (ret) {
  1035. dev_err(dwc->dev, "failed to allocate event buffers\n");
  1036. ret = -ENOMEM;
  1037. goto err2;
  1038. }
  1039. ret = dwc3_get_dr_mode(dwc);
  1040. if (ret)
  1041. goto err3;
  1042. ret = dwc3_alloc_scratch_buffers(dwc);
  1043. if (ret)
  1044. goto err3;
  1045. ret = dwc3_core_init(dwc);
  1046. if (ret) {
  1047. dev_err(dev, "failed to initialize core\n");
  1048. goto err4;
  1049. }
  1050. dwc3_check_params(dwc);
  1051. ret = dwc3_core_init_mode(dwc);
  1052. if (ret)
  1053. goto err5;
  1054. dwc3_debugfs_init(dwc);
  1055. pm_runtime_put(dev);
  1056. return 0;
  1057. err5:
  1058. dwc3_event_buffers_cleanup(dwc);
  1059. err4:
  1060. dwc3_free_scratch_buffers(dwc);
  1061. err3:
  1062. dwc3_free_event_buffers(dwc);
  1063. err2:
  1064. pm_runtime_allow(&pdev->dev);
  1065. err1:
  1066. pm_runtime_put_sync(&pdev->dev);
  1067. pm_runtime_disable(&pdev->dev);
  1068. err0:
  1069. /*
  1070. * restore res->start back to its original value so that, in case the
  1071. * probe is deferred, we don't end up getting error in request the
  1072. * memory region the next time probe is called.
  1073. */
  1074. res->start -= DWC3_GLOBALS_REGS_START;
  1075. return ret;
  1076. }
  1077. static int dwc3_remove(struct platform_device *pdev)
  1078. {
  1079. struct dwc3 *dwc = platform_get_drvdata(pdev);
  1080. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1081. pm_runtime_get_sync(&pdev->dev);
  1082. /*
  1083. * restore res->start back to its original value so that, in case the
  1084. * probe is deferred, we don't end up getting error in request the
  1085. * memory region the next time probe is called.
  1086. */
  1087. res->start -= DWC3_GLOBALS_REGS_START;
  1088. dwc3_debugfs_exit(dwc);
  1089. dwc3_core_exit_mode(dwc);
  1090. dwc3_core_exit(dwc);
  1091. dwc3_ulpi_exit(dwc);
  1092. pm_runtime_put_sync(&pdev->dev);
  1093. pm_runtime_allow(&pdev->dev);
  1094. pm_runtime_disable(&pdev->dev);
  1095. dwc3_free_event_buffers(dwc);
  1096. dwc3_free_scratch_buffers(dwc);
  1097. return 0;
  1098. }
  1099. #ifdef CONFIG_PM
  1100. static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
  1101. {
  1102. unsigned long flags;
  1103. switch (dwc->current_dr_role) {
  1104. case DWC3_GCTL_PRTCAP_DEVICE:
  1105. spin_lock_irqsave(&dwc->lock, flags);
  1106. dwc3_gadget_suspend(dwc);
  1107. spin_unlock_irqrestore(&dwc->lock, flags);
  1108. dwc3_core_exit(dwc);
  1109. break;
  1110. case DWC3_GCTL_PRTCAP_HOST:
  1111. /* do nothing during host runtime_suspend */
  1112. if (!PMSG_IS_AUTO(msg))
  1113. dwc3_core_exit(dwc);
  1114. break;
  1115. case DWC3_GCTL_PRTCAP_OTG:
  1116. /* do nothing during runtime_suspend */
  1117. if (PMSG_IS_AUTO(msg))
  1118. break;
  1119. if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
  1120. spin_lock_irqsave(&dwc->lock, flags);
  1121. dwc3_gadget_suspend(dwc);
  1122. spin_unlock_irqrestore(&dwc->lock, flags);
  1123. }
  1124. dwc3_otg_exit(dwc);
  1125. dwc3_core_exit(dwc);
  1126. break;
  1127. default:
  1128. /* do nothing */
  1129. break;
  1130. }
  1131. return 0;
  1132. }
  1133. static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
  1134. {
  1135. unsigned long flags;
  1136. int ret;
  1137. switch (dwc->current_dr_role) {
  1138. case DWC3_GCTL_PRTCAP_DEVICE:
  1139. ret = dwc3_core_init(dwc);
  1140. if (ret)
  1141. return ret;
  1142. spin_lock_irqsave(&dwc->lock, flags);
  1143. dwc3_gadget_resume(dwc);
  1144. spin_unlock_irqrestore(&dwc->lock, flags);
  1145. break;
  1146. case DWC3_GCTL_PRTCAP_HOST:
  1147. /* nothing to do on host runtime_resume */
  1148. if (!PMSG_IS_AUTO(msg)) {
  1149. ret = dwc3_core_init(dwc);
  1150. if (ret)
  1151. return ret;
  1152. }
  1153. break;
  1154. case DWC3_GCTL_PRTCAP_OTG:
  1155. /* nothing to do on runtime_resume */
  1156. if (PMSG_IS_AUTO(msg))
  1157. break;
  1158. ret = dwc3_core_init(dwc);
  1159. if (ret)
  1160. return ret;
  1161. dwc3_set_prtcap(dwc, dwc->current_dr_role);
  1162. dwc3_otg_init(dwc);
  1163. if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
  1164. dwc3_otg_host_init(dwc);
  1165. } else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
  1166. spin_lock_irqsave(&dwc->lock, flags);
  1167. dwc3_gadget_resume(dwc);
  1168. spin_unlock_irqrestore(&dwc->lock, flags);
  1169. }
  1170. break;
  1171. default:
  1172. /* do nothing */
  1173. break;
  1174. }
  1175. return 0;
  1176. }
  1177. static int dwc3_runtime_checks(struct dwc3 *dwc)
  1178. {
  1179. switch (dwc->current_dr_role) {
  1180. case DWC3_GCTL_PRTCAP_DEVICE:
  1181. if (dwc->connected)
  1182. return -EBUSY;
  1183. break;
  1184. case DWC3_GCTL_PRTCAP_HOST:
  1185. default:
  1186. /* do nothing */
  1187. break;
  1188. }
  1189. return 0;
  1190. }
  1191. static int dwc3_runtime_suspend(struct device *dev)
  1192. {
  1193. struct dwc3 *dwc = dev_get_drvdata(dev);
  1194. int ret;
  1195. if (dwc3_runtime_checks(dwc))
  1196. return -EBUSY;
  1197. ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND);
  1198. if (ret)
  1199. return ret;
  1200. device_init_wakeup(dev, true);
  1201. return 0;
  1202. }
  1203. static int dwc3_runtime_resume(struct device *dev)
  1204. {
  1205. struct dwc3 *dwc = dev_get_drvdata(dev);
  1206. int ret;
  1207. device_init_wakeup(dev, false);
  1208. ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
  1209. if (ret)
  1210. return ret;
  1211. switch (dwc->current_dr_role) {
  1212. case DWC3_GCTL_PRTCAP_DEVICE:
  1213. dwc3_gadget_process_pending_events(dwc);
  1214. break;
  1215. case DWC3_GCTL_PRTCAP_HOST:
  1216. default:
  1217. /* do nothing */
  1218. break;
  1219. }
  1220. pm_runtime_mark_last_busy(dev);
  1221. return 0;
  1222. }
  1223. static int dwc3_runtime_idle(struct device *dev)
  1224. {
  1225. struct dwc3 *dwc = dev_get_drvdata(dev);
  1226. switch (dwc->current_dr_role) {
  1227. case DWC3_GCTL_PRTCAP_DEVICE:
  1228. if (dwc3_runtime_checks(dwc))
  1229. return -EBUSY;
  1230. break;
  1231. case DWC3_GCTL_PRTCAP_HOST:
  1232. default:
  1233. /* do nothing */
  1234. break;
  1235. }
  1236. pm_runtime_mark_last_busy(dev);
  1237. pm_runtime_autosuspend(dev);
  1238. return 0;
  1239. }
  1240. #endif /* CONFIG_PM */
  1241. #ifdef CONFIG_PM_SLEEP
  1242. static int dwc3_suspend(struct device *dev)
  1243. {
  1244. struct dwc3 *dwc = dev_get_drvdata(dev);
  1245. int ret;
  1246. ret = dwc3_suspend_common(dwc, PMSG_SUSPEND);
  1247. if (ret)
  1248. return ret;
  1249. pinctrl_pm_select_sleep_state(dev);
  1250. return 0;
  1251. }
  1252. static int dwc3_resume(struct device *dev)
  1253. {
  1254. struct dwc3 *dwc = dev_get_drvdata(dev);
  1255. int ret;
  1256. pinctrl_pm_select_default_state(dev);
  1257. ret = dwc3_resume_common(dwc, PMSG_RESUME);
  1258. if (ret)
  1259. return ret;
  1260. pm_runtime_disable(dev);
  1261. pm_runtime_set_active(dev);
  1262. pm_runtime_enable(dev);
  1263. return 0;
  1264. }
  1265. #endif /* CONFIG_PM_SLEEP */
  1266. static const struct dev_pm_ops dwc3_dev_pm_ops = {
  1267. SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
  1268. SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
  1269. dwc3_runtime_idle)
  1270. };
  1271. #ifdef CONFIG_OF
  1272. static const struct of_device_id of_dwc3_match[] = {
  1273. {
  1274. .compatible = "snps,dwc3"
  1275. },
  1276. {
  1277. .compatible = "synopsys,dwc3"
  1278. },
  1279. { },
  1280. };
  1281. MODULE_DEVICE_TABLE(of, of_dwc3_match);
  1282. #endif
  1283. #ifdef CONFIG_ACPI
  1284. #define ACPI_ID_INTEL_BSW "808622B7"
  1285. static const struct acpi_device_id dwc3_acpi_match[] = {
  1286. { ACPI_ID_INTEL_BSW, 0 },
  1287. { },
  1288. };
  1289. MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
  1290. #endif
  1291. static struct platform_driver dwc3_driver = {
  1292. .probe = dwc3_probe,
  1293. .remove = dwc3_remove,
  1294. .driver = {
  1295. .name = "dwc3",
  1296. .of_match_table = of_match_ptr(of_dwc3_match),
  1297. .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
  1298. .pm = &dwc3_dev_pm_ops,
  1299. },
  1300. };
  1301. module_platform_driver(dwc3_driver);
  1302. MODULE_ALIAS("platform:dwc3");
  1303. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  1304. MODULE_LICENSE("GPL v2");
  1305. MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");