core.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647
  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;
  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. res->start += DWC3_GLOBALS_REGS_START;
  1054. /*
  1055. * Request memory region but exclude xHCI regs,
  1056. * since it will be requested by the xhci-plat driver.
  1057. */
  1058. regs = devm_ioremap_resource(dev, res);
  1059. if (IS_ERR(regs)) {
  1060. ret = PTR_ERR(regs);
  1061. goto err0;
  1062. }
  1063. dwc->regs = regs;
  1064. dwc->regs_size = resource_size(res);
  1065. dwc3_get_properties(dwc);
  1066. platform_set_drvdata(pdev, dwc);
  1067. dwc3_cache_hwparams(dwc);
  1068. spin_lock_init(&dwc->lock);
  1069. pm_runtime_set_active(dev);
  1070. pm_runtime_use_autosuspend(dev);
  1071. pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
  1072. pm_runtime_enable(dev);
  1073. ret = pm_runtime_get_sync(dev);
  1074. if (ret < 0)
  1075. goto err1;
  1076. pm_runtime_forbid(dev);
  1077. ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
  1078. if (ret) {
  1079. dev_err(dwc->dev, "failed to allocate event buffers\n");
  1080. ret = -ENOMEM;
  1081. goto err2;
  1082. }
  1083. ret = dwc3_get_dr_mode(dwc);
  1084. if (ret)
  1085. goto err3;
  1086. ret = dwc3_alloc_scratch_buffers(dwc);
  1087. if (ret)
  1088. goto err3;
  1089. ret = dwc3_core_init(dwc);
  1090. if (ret) {
  1091. dev_err(dev, "failed to initialize core\n");
  1092. goto err4;
  1093. }
  1094. dwc3_check_params(dwc);
  1095. ret = dwc3_core_init_mode(dwc);
  1096. if (ret)
  1097. goto err5;
  1098. dwc3_debugfs_init(dwc);
  1099. pm_runtime_put(dev);
  1100. return 0;
  1101. err5:
  1102. dwc3_event_buffers_cleanup(dwc);
  1103. err4:
  1104. dwc3_free_scratch_buffers(dwc);
  1105. err3:
  1106. dwc3_free_event_buffers(dwc);
  1107. err2:
  1108. pm_runtime_allow(&pdev->dev);
  1109. err1:
  1110. pm_runtime_put_sync(&pdev->dev);
  1111. pm_runtime_disable(&pdev->dev);
  1112. err0:
  1113. /*
  1114. * restore res->start back to its original value so that, in case the
  1115. * probe is deferred, we don't end up getting error in request the
  1116. * memory region the next time probe is called.
  1117. */
  1118. res->start -= DWC3_GLOBALS_REGS_START;
  1119. return ret;
  1120. }
  1121. static int dwc3_remove(struct platform_device *pdev)
  1122. {
  1123. struct dwc3 *dwc = platform_get_drvdata(pdev);
  1124. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1125. pm_runtime_get_sync(&pdev->dev);
  1126. /*
  1127. * restore res->start back to its original value so that, in case the
  1128. * probe is deferred, we don't end up getting error in request the
  1129. * memory region the next time probe is called.
  1130. */
  1131. res->start -= DWC3_GLOBALS_REGS_START;
  1132. dwc3_debugfs_exit(dwc);
  1133. dwc3_core_exit_mode(dwc);
  1134. dwc3_core_exit(dwc);
  1135. dwc3_ulpi_exit(dwc);
  1136. pm_runtime_put_sync(&pdev->dev);
  1137. pm_runtime_allow(&pdev->dev);
  1138. pm_runtime_disable(&pdev->dev);
  1139. dwc3_free_event_buffers(dwc);
  1140. dwc3_free_scratch_buffers(dwc);
  1141. return 0;
  1142. }
  1143. #ifdef CONFIG_PM
  1144. static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
  1145. {
  1146. unsigned long flags;
  1147. switch (dwc->current_dr_role) {
  1148. case DWC3_GCTL_PRTCAP_DEVICE:
  1149. spin_lock_irqsave(&dwc->lock, flags);
  1150. dwc3_gadget_suspend(dwc);
  1151. spin_unlock_irqrestore(&dwc->lock, flags);
  1152. dwc3_core_exit(dwc);
  1153. break;
  1154. case DWC3_GCTL_PRTCAP_HOST:
  1155. /* do nothing during host runtime_suspend */
  1156. if (!PMSG_IS_AUTO(msg))
  1157. dwc3_core_exit(dwc);
  1158. break;
  1159. case DWC3_GCTL_PRTCAP_OTG:
  1160. /* do nothing during runtime_suspend */
  1161. if (PMSG_IS_AUTO(msg))
  1162. break;
  1163. if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
  1164. spin_lock_irqsave(&dwc->lock, flags);
  1165. dwc3_gadget_suspend(dwc);
  1166. spin_unlock_irqrestore(&dwc->lock, flags);
  1167. }
  1168. dwc3_otg_exit(dwc);
  1169. dwc3_core_exit(dwc);
  1170. break;
  1171. default:
  1172. /* do nothing */
  1173. break;
  1174. }
  1175. return 0;
  1176. }
  1177. static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
  1178. {
  1179. unsigned long flags;
  1180. int ret;
  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. /* nothing to do on host runtime_resume */
  1193. if (!PMSG_IS_AUTO(msg)) {
  1194. ret = dwc3_core_init(dwc);
  1195. if (ret)
  1196. return ret;
  1197. dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
  1198. }
  1199. break;
  1200. case DWC3_GCTL_PRTCAP_OTG:
  1201. /* nothing to do on runtime_resume */
  1202. if (PMSG_IS_AUTO(msg))
  1203. break;
  1204. ret = dwc3_core_init(dwc);
  1205. if (ret)
  1206. return ret;
  1207. dwc3_set_prtcap(dwc, dwc->current_dr_role);
  1208. dwc3_otg_init(dwc);
  1209. if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
  1210. dwc3_otg_host_init(dwc);
  1211. } else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
  1212. spin_lock_irqsave(&dwc->lock, flags);
  1213. dwc3_gadget_resume(dwc);
  1214. spin_unlock_irqrestore(&dwc->lock, flags);
  1215. }
  1216. break;
  1217. default:
  1218. /* do nothing */
  1219. break;
  1220. }
  1221. return 0;
  1222. }
  1223. static int dwc3_runtime_checks(struct dwc3 *dwc)
  1224. {
  1225. switch (dwc->current_dr_role) {
  1226. case DWC3_GCTL_PRTCAP_DEVICE:
  1227. if (dwc->connected)
  1228. return -EBUSY;
  1229. break;
  1230. case DWC3_GCTL_PRTCAP_HOST:
  1231. default:
  1232. /* do nothing */
  1233. break;
  1234. }
  1235. return 0;
  1236. }
  1237. static int dwc3_runtime_suspend(struct device *dev)
  1238. {
  1239. struct dwc3 *dwc = dev_get_drvdata(dev);
  1240. int ret;
  1241. if (dwc3_runtime_checks(dwc))
  1242. return -EBUSY;
  1243. ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND);
  1244. if (ret)
  1245. return ret;
  1246. device_init_wakeup(dev, true);
  1247. return 0;
  1248. }
  1249. static int dwc3_runtime_resume(struct device *dev)
  1250. {
  1251. struct dwc3 *dwc = dev_get_drvdata(dev);
  1252. int ret;
  1253. device_init_wakeup(dev, false);
  1254. ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
  1255. if (ret)
  1256. return ret;
  1257. switch (dwc->current_dr_role) {
  1258. case DWC3_GCTL_PRTCAP_DEVICE:
  1259. dwc3_gadget_process_pending_events(dwc);
  1260. break;
  1261. case DWC3_GCTL_PRTCAP_HOST:
  1262. default:
  1263. /* do nothing */
  1264. break;
  1265. }
  1266. pm_runtime_mark_last_busy(dev);
  1267. return 0;
  1268. }
  1269. static int dwc3_runtime_idle(struct device *dev)
  1270. {
  1271. struct dwc3 *dwc = dev_get_drvdata(dev);
  1272. switch (dwc->current_dr_role) {
  1273. case DWC3_GCTL_PRTCAP_DEVICE:
  1274. if (dwc3_runtime_checks(dwc))
  1275. return -EBUSY;
  1276. break;
  1277. case DWC3_GCTL_PRTCAP_HOST:
  1278. default:
  1279. /* do nothing */
  1280. break;
  1281. }
  1282. pm_runtime_mark_last_busy(dev);
  1283. pm_runtime_autosuspend(dev);
  1284. return 0;
  1285. }
  1286. #endif /* CONFIG_PM */
  1287. #ifdef CONFIG_PM_SLEEP
  1288. static int dwc3_suspend(struct device *dev)
  1289. {
  1290. struct dwc3 *dwc = dev_get_drvdata(dev);
  1291. int ret;
  1292. ret = dwc3_suspend_common(dwc, PMSG_SUSPEND);
  1293. if (ret)
  1294. return ret;
  1295. pinctrl_pm_select_sleep_state(dev);
  1296. return 0;
  1297. }
  1298. static int dwc3_resume(struct device *dev)
  1299. {
  1300. struct dwc3 *dwc = dev_get_drvdata(dev);
  1301. int ret;
  1302. pinctrl_pm_select_default_state(dev);
  1303. ret = dwc3_resume_common(dwc, PMSG_RESUME);
  1304. if (ret)
  1305. return ret;
  1306. pm_runtime_disable(dev);
  1307. pm_runtime_set_active(dev);
  1308. pm_runtime_enable(dev);
  1309. return 0;
  1310. }
  1311. #endif /* CONFIG_PM_SLEEP */
  1312. static const struct dev_pm_ops dwc3_dev_pm_ops = {
  1313. SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
  1314. SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
  1315. dwc3_runtime_idle)
  1316. };
  1317. #ifdef CONFIG_OF
  1318. static const struct of_device_id of_dwc3_match[] = {
  1319. {
  1320. .compatible = "snps,dwc3"
  1321. },
  1322. {
  1323. .compatible = "synopsys,dwc3"
  1324. },
  1325. { },
  1326. };
  1327. MODULE_DEVICE_TABLE(of, of_dwc3_match);
  1328. #endif
  1329. #ifdef CONFIG_ACPI
  1330. #define ACPI_ID_INTEL_BSW "808622B7"
  1331. static const struct acpi_device_id dwc3_acpi_match[] = {
  1332. { ACPI_ID_INTEL_BSW, 0 },
  1333. { },
  1334. };
  1335. MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
  1336. #endif
  1337. static struct platform_driver dwc3_driver = {
  1338. .probe = dwc3_probe,
  1339. .remove = dwc3_remove,
  1340. .driver = {
  1341. .name = "dwc3",
  1342. .of_match_table = of_match_ptr(of_dwc3_match),
  1343. .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
  1344. .pm = &dwc3_dev_pm_ops,
  1345. },
  1346. };
  1347. module_platform_driver(dwc3_driver);
  1348. MODULE_ALIAS("platform:dwc3");
  1349. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  1350. MODULE_LICENSE("GPL v2");
  1351. MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");