core.c 35 KB

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