core.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  1. /**
  2. * core.c - DesignWare USB3 DRD Controller Core file
  3. *
  4. * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
  5. *
  6. * Authors: Felipe Balbi <balbi@ti.com>,
  7. * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 of
  11. * the License as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/version.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/slab.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/ioport.h>
  30. #include <linux/io.h>
  31. #include <linux/list.h>
  32. #include <linux/delay.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/of.h>
  35. #include <linux/acpi.h>
  36. #include <linux/usb/ch9.h>
  37. #include <linux/usb/gadget.h>
  38. #include <linux/usb/of.h>
  39. #include <linux/usb/otg.h>
  40. #include "platform_data.h"
  41. #include "core.h"
  42. #include "gadget.h"
  43. #include "io.h"
  44. #include "debug.h"
  45. /* -------------------------------------------------------------------------- */
  46. void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
  47. {
  48. u32 reg;
  49. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  50. reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
  51. reg |= DWC3_GCTL_PRTCAPDIR(mode);
  52. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  53. }
  54. /**
  55. * dwc3_core_soft_reset - Issues core soft reset and PHY reset
  56. * @dwc: pointer to our context structure
  57. */
  58. static int dwc3_core_soft_reset(struct dwc3 *dwc)
  59. {
  60. u32 reg;
  61. int ret;
  62. /* Before Resetting PHY, put Core in Reset */
  63. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  64. reg |= DWC3_GCTL_CORESOFTRESET;
  65. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  66. /* Assert USB3 PHY reset */
  67. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  68. reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
  69. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  70. /* Assert USB2 PHY reset */
  71. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  72. reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
  73. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  74. usb_phy_init(dwc->usb2_phy);
  75. usb_phy_init(dwc->usb3_phy);
  76. ret = phy_init(dwc->usb2_generic_phy);
  77. if (ret < 0)
  78. return ret;
  79. ret = phy_init(dwc->usb3_generic_phy);
  80. if (ret < 0) {
  81. phy_exit(dwc->usb2_generic_phy);
  82. return ret;
  83. }
  84. mdelay(100);
  85. /* Clear USB3 PHY reset */
  86. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  87. reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
  88. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  89. /* Clear USB2 PHY reset */
  90. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  91. reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
  92. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  93. mdelay(100);
  94. /* After PHYs are stable we can take Core out of reset state */
  95. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  96. reg &= ~DWC3_GCTL_CORESOFTRESET;
  97. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  98. return 0;
  99. }
  100. /**
  101. * dwc3_free_one_event_buffer - Frees one event buffer
  102. * @dwc: Pointer to our controller context structure
  103. * @evt: Pointer to event buffer to be freed
  104. */
  105. static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
  106. struct dwc3_event_buffer *evt)
  107. {
  108. dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
  109. }
  110. /**
  111. * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
  112. * @dwc: Pointer to our controller context structure
  113. * @length: size of the event buffer
  114. *
  115. * Returns a pointer to the allocated event buffer structure on success
  116. * otherwise ERR_PTR(errno).
  117. */
  118. static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
  119. unsigned length)
  120. {
  121. struct dwc3_event_buffer *evt;
  122. evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
  123. if (!evt)
  124. return ERR_PTR(-ENOMEM);
  125. evt->dwc = dwc;
  126. evt->length = length;
  127. evt->buf = dma_alloc_coherent(dwc->dev, length,
  128. &evt->dma, GFP_KERNEL);
  129. if (!evt->buf)
  130. return ERR_PTR(-ENOMEM);
  131. return evt;
  132. }
  133. /**
  134. * dwc3_free_event_buffers - frees all allocated event buffers
  135. * @dwc: Pointer to our controller context structure
  136. */
  137. static void dwc3_free_event_buffers(struct dwc3 *dwc)
  138. {
  139. struct dwc3_event_buffer *evt;
  140. int i;
  141. for (i = 0; i < dwc->num_event_buffers; i++) {
  142. evt = dwc->ev_buffs[i];
  143. if (evt)
  144. dwc3_free_one_event_buffer(dwc, evt);
  145. }
  146. }
  147. /**
  148. * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
  149. * @dwc: pointer to our controller context structure
  150. * @length: size of event buffer
  151. *
  152. * Returns 0 on success otherwise negative errno. In the error case, dwc
  153. * may contain some buffers allocated but not all which were requested.
  154. */
  155. static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
  156. {
  157. int num;
  158. int i;
  159. num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
  160. dwc->num_event_buffers = num;
  161. dwc->ev_buffs = devm_kzalloc(dwc->dev, sizeof(*dwc->ev_buffs) * num,
  162. GFP_KERNEL);
  163. if (!dwc->ev_buffs)
  164. return -ENOMEM;
  165. for (i = 0; i < num; i++) {
  166. struct dwc3_event_buffer *evt;
  167. evt = dwc3_alloc_one_event_buffer(dwc, length);
  168. if (IS_ERR(evt)) {
  169. dev_err(dwc->dev, "can't allocate event buffer\n");
  170. return PTR_ERR(evt);
  171. }
  172. dwc->ev_buffs[i] = evt;
  173. }
  174. return 0;
  175. }
  176. /**
  177. * dwc3_event_buffers_setup - setup our allocated event buffers
  178. * @dwc: pointer to our controller context structure
  179. *
  180. * Returns 0 on success otherwise negative errno.
  181. */
  182. static int dwc3_event_buffers_setup(struct dwc3 *dwc)
  183. {
  184. struct dwc3_event_buffer *evt;
  185. int n;
  186. for (n = 0; n < dwc->num_event_buffers; n++) {
  187. evt = dwc->ev_buffs[n];
  188. dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
  189. evt->buf, (unsigned long long) evt->dma,
  190. evt->length);
  191. evt->lpos = 0;
  192. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
  193. lower_32_bits(evt->dma));
  194. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
  195. upper_32_bits(evt->dma));
  196. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
  197. DWC3_GEVNTSIZ_SIZE(evt->length));
  198. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
  199. }
  200. return 0;
  201. }
  202. static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
  203. {
  204. struct dwc3_event_buffer *evt;
  205. int n;
  206. for (n = 0; n < dwc->num_event_buffers; n++) {
  207. evt = dwc->ev_buffs[n];
  208. evt->lpos = 0;
  209. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
  210. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
  211. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), DWC3_GEVNTSIZ_INTMASK
  212. | DWC3_GEVNTSIZ_SIZE(0));
  213. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
  214. }
  215. }
  216. static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
  217. {
  218. if (!dwc->has_hibernation)
  219. return 0;
  220. if (!dwc->nr_scratch)
  221. return 0;
  222. dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
  223. DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
  224. if (!dwc->scratchbuf)
  225. return -ENOMEM;
  226. return 0;
  227. }
  228. static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
  229. {
  230. dma_addr_t scratch_addr;
  231. u32 param;
  232. int ret;
  233. if (!dwc->has_hibernation)
  234. return 0;
  235. if (!dwc->nr_scratch)
  236. return 0;
  237. /* should never fall here */
  238. if (!WARN_ON(dwc->scratchbuf))
  239. return 0;
  240. scratch_addr = dma_map_single(dwc->dev, dwc->scratchbuf,
  241. dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
  242. DMA_BIDIRECTIONAL);
  243. if (dma_mapping_error(dwc->dev, scratch_addr)) {
  244. dev_err(dwc->dev, "failed to map scratch buffer\n");
  245. ret = -EFAULT;
  246. goto err0;
  247. }
  248. dwc->scratch_addr = scratch_addr;
  249. param = lower_32_bits(scratch_addr);
  250. ret = dwc3_send_gadget_generic_command(dwc,
  251. DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
  252. if (ret < 0)
  253. goto err1;
  254. param = upper_32_bits(scratch_addr);
  255. ret = dwc3_send_gadget_generic_command(dwc,
  256. DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
  257. if (ret < 0)
  258. goto err1;
  259. return 0;
  260. err1:
  261. dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
  262. DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
  263. err0:
  264. return ret;
  265. }
  266. static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
  267. {
  268. if (!dwc->has_hibernation)
  269. return;
  270. if (!dwc->nr_scratch)
  271. return;
  272. /* should never fall here */
  273. if (!WARN_ON(dwc->scratchbuf))
  274. return;
  275. dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
  276. DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
  277. kfree(dwc->scratchbuf);
  278. }
  279. static void dwc3_core_num_eps(struct dwc3 *dwc)
  280. {
  281. struct dwc3_hwparams *parms = &dwc->hwparams;
  282. dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
  283. dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
  284. dev_vdbg(dwc->dev, "found %d IN and %d OUT endpoints\n",
  285. dwc->num_in_eps, dwc->num_out_eps);
  286. }
  287. static void dwc3_cache_hwparams(struct dwc3 *dwc)
  288. {
  289. struct dwc3_hwparams *parms = &dwc->hwparams;
  290. parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
  291. parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
  292. parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
  293. parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
  294. parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
  295. parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
  296. parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
  297. parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
  298. parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
  299. }
  300. /**
  301. * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
  302. * @dwc: Pointer to our controller context structure
  303. */
  304. static void dwc3_phy_setup(struct dwc3 *dwc)
  305. {
  306. u32 reg;
  307. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  308. /*
  309. * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
  310. * to '0' during coreConsultant configuration. So default value
  311. * will be '0' when the core is reset. Application needs to set it
  312. * to '1' after the core initialization is completed.
  313. */
  314. if (dwc->revision > DWC3_REVISION_194A)
  315. reg |= DWC3_GUSB3PIPECTL_SUSPHY;
  316. if (dwc->u2ss_inp3_quirk)
  317. reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
  318. if (dwc->req_p1p2p3_quirk)
  319. reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
  320. if (dwc->del_p1p2p3_quirk)
  321. reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
  322. if (dwc->del_phy_power_chg_quirk)
  323. reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
  324. if (dwc->lfps_filter_quirk)
  325. reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
  326. if (dwc->rx_detect_poll_quirk)
  327. reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
  328. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  329. mdelay(100);
  330. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  331. /*
  332. * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
  333. * '0' during coreConsultant configuration. So default value will
  334. * be '0' when the core is reset. Application needs to set it to
  335. * '1' after the core initialization is completed.
  336. */
  337. if (dwc->revision > DWC3_REVISION_194A)
  338. reg |= DWC3_GUSB2PHYCFG_SUSPHY;
  339. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  340. mdelay(100);
  341. }
  342. /**
  343. * dwc3_core_init - Low-level initialization of DWC3 Core
  344. * @dwc: Pointer to our controller context structure
  345. *
  346. * Returns 0 on success otherwise negative errno.
  347. */
  348. static int dwc3_core_init(struct dwc3 *dwc)
  349. {
  350. unsigned long timeout;
  351. u32 hwparams4 = dwc->hwparams.hwparams4;
  352. u32 reg;
  353. int ret;
  354. reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
  355. /* This should read as U3 followed by revision number */
  356. if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
  357. dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
  358. ret = -ENODEV;
  359. goto err0;
  360. }
  361. dwc->revision = reg;
  362. /*
  363. * Write Linux Version Code to our GUID register so it's easy to figure
  364. * out which kernel version a bug was found.
  365. */
  366. dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
  367. /* Handle USB2.0-only core configuration */
  368. if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
  369. DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
  370. if (dwc->maximum_speed == USB_SPEED_SUPER)
  371. dwc->maximum_speed = USB_SPEED_HIGH;
  372. }
  373. /* issue device SoftReset too */
  374. timeout = jiffies + msecs_to_jiffies(500);
  375. dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
  376. do {
  377. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  378. if (!(reg & DWC3_DCTL_CSFTRST))
  379. break;
  380. if (time_after(jiffies, timeout)) {
  381. dev_err(dwc->dev, "Reset Timed Out\n");
  382. ret = -ETIMEDOUT;
  383. goto err0;
  384. }
  385. cpu_relax();
  386. } while (true);
  387. ret = dwc3_core_soft_reset(dwc);
  388. if (ret)
  389. goto err0;
  390. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  391. reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
  392. switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
  393. case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
  394. /**
  395. * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
  396. * issue which would cause xHCI compliance tests to fail.
  397. *
  398. * Because of that we cannot enable clock gating on such
  399. * configurations.
  400. *
  401. * Refers to:
  402. *
  403. * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
  404. * SOF/ITP Mode Used
  405. */
  406. if ((dwc->dr_mode == USB_DR_MODE_HOST ||
  407. dwc->dr_mode == USB_DR_MODE_OTG) &&
  408. (dwc->revision >= DWC3_REVISION_210A &&
  409. dwc->revision <= DWC3_REVISION_250A))
  410. reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
  411. else
  412. reg &= ~DWC3_GCTL_DSBLCLKGTNG;
  413. break;
  414. case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
  415. /* enable hibernation here */
  416. dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
  417. /*
  418. * REVISIT Enabling this bit so that host-mode hibernation
  419. * will work. Device-mode hibernation is not yet implemented.
  420. */
  421. reg |= DWC3_GCTL_GBLHIBERNATIONEN;
  422. break;
  423. default:
  424. dev_dbg(dwc->dev, "No power optimization available\n");
  425. }
  426. /* check if current dwc3 is on simulation board */
  427. if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
  428. dev_dbg(dwc->dev, "it is on FPGA board\n");
  429. dwc->is_fpga = true;
  430. }
  431. WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
  432. "disable_scramble cannot be used on non-FPGA builds\n");
  433. if (dwc->disable_scramble_quirk && dwc->is_fpga)
  434. reg |= DWC3_GCTL_DISSCRAMBLE;
  435. else
  436. reg &= ~DWC3_GCTL_DISSCRAMBLE;
  437. if (dwc->u2exit_lfps_quirk)
  438. reg |= DWC3_GCTL_U2EXIT_LFPS;
  439. /*
  440. * WORKAROUND: DWC3 revisions <1.90a have a bug
  441. * where the device can fail to connect at SuperSpeed
  442. * and falls back to high-speed mode which causes
  443. * the device to enter a Connect/Disconnect loop
  444. */
  445. if (dwc->revision < DWC3_REVISION_190A)
  446. reg |= DWC3_GCTL_U2RSTECN;
  447. dwc3_core_num_eps(dwc);
  448. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  449. dwc3_phy_setup(dwc);
  450. ret = dwc3_alloc_scratch_buffers(dwc);
  451. if (ret)
  452. goto err1;
  453. ret = dwc3_setup_scratch_buffers(dwc);
  454. if (ret)
  455. goto err2;
  456. return 0;
  457. err2:
  458. dwc3_free_scratch_buffers(dwc);
  459. err1:
  460. usb_phy_shutdown(dwc->usb2_phy);
  461. usb_phy_shutdown(dwc->usb3_phy);
  462. phy_exit(dwc->usb2_generic_phy);
  463. phy_exit(dwc->usb3_generic_phy);
  464. err0:
  465. return ret;
  466. }
  467. static void dwc3_core_exit(struct dwc3 *dwc)
  468. {
  469. dwc3_free_scratch_buffers(dwc);
  470. usb_phy_shutdown(dwc->usb2_phy);
  471. usb_phy_shutdown(dwc->usb3_phy);
  472. phy_exit(dwc->usb2_generic_phy);
  473. phy_exit(dwc->usb3_generic_phy);
  474. }
  475. static int dwc3_core_get_phy(struct dwc3 *dwc)
  476. {
  477. struct device *dev = dwc->dev;
  478. struct device_node *node = dev->of_node;
  479. int ret;
  480. if (node) {
  481. dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
  482. dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
  483. } else {
  484. dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
  485. dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
  486. }
  487. if (IS_ERR(dwc->usb2_phy)) {
  488. ret = PTR_ERR(dwc->usb2_phy);
  489. if (ret == -ENXIO || ret == -ENODEV) {
  490. dwc->usb2_phy = NULL;
  491. } else if (ret == -EPROBE_DEFER) {
  492. return ret;
  493. } else {
  494. dev_err(dev, "no usb2 phy configured\n");
  495. return ret;
  496. }
  497. }
  498. if (IS_ERR(dwc->usb3_phy)) {
  499. ret = PTR_ERR(dwc->usb3_phy);
  500. if (ret == -ENXIO || ret == -ENODEV) {
  501. dwc->usb3_phy = NULL;
  502. } else if (ret == -EPROBE_DEFER) {
  503. return ret;
  504. } else {
  505. dev_err(dev, "no usb3 phy configured\n");
  506. return ret;
  507. }
  508. }
  509. dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
  510. if (IS_ERR(dwc->usb2_generic_phy)) {
  511. ret = PTR_ERR(dwc->usb2_generic_phy);
  512. if (ret == -ENOSYS || ret == -ENODEV) {
  513. dwc->usb2_generic_phy = NULL;
  514. } else if (ret == -EPROBE_DEFER) {
  515. return ret;
  516. } else {
  517. dev_err(dev, "no usb2 phy configured\n");
  518. return ret;
  519. }
  520. }
  521. dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
  522. if (IS_ERR(dwc->usb3_generic_phy)) {
  523. ret = PTR_ERR(dwc->usb3_generic_phy);
  524. if (ret == -ENOSYS || ret == -ENODEV) {
  525. dwc->usb3_generic_phy = NULL;
  526. } else if (ret == -EPROBE_DEFER) {
  527. return ret;
  528. } else {
  529. dev_err(dev, "no usb3 phy configured\n");
  530. return ret;
  531. }
  532. }
  533. return 0;
  534. }
  535. static int dwc3_core_init_mode(struct dwc3 *dwc)
  536. {
  537. struct device *dev = dwc->dev;
  538. int ret;
  539. switch (dwc->dr_mode) {
  540. case USB_DR_MODE_PERIPHERAL:
  541. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
  542. ret = dwc3_gadget_init(dwc);
  543. if (ret) {
  544. dev_err(dev, "failed to initialize gadget\n");
  545. return ret;
  546. }
  547. break;
  548. case USB_DR_MODE_HOST:
  549. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
  550. ret = dwc3_host_init(dwc);
  551. if (ret) {
  552. dev_err(dev, "failed to initialize host\n");
  553. return ret;
  554. }
  555. break;
  556. case USB_DR_MODE_OTG:
  557. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
  558. ret = dwc3_host_init(dwc);
  559. if (ret) {
  560. dev_err(dev, "failed to initialize host\n");
  561. return ret;
  562. }
  563. ret = dwc3_gadget_init(dwc);
  564. if (ret) {
  565. dev_err(dev, "failed to initialize gadget\n");
  566. return ret;
  567. }
  568. break;
  569. default:
  570. dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
  571. return -EINVAL;
  572. }
  573. return 0;
  574. }
  575. static void dwc3_core_exit_mode(struct dwc3 *dwc)
  576. {
  577. switch (dwc->dr_mode) {
  578. case USB_DR_MODE_PERIPHERAL:
  579. dwc3_gadget_exit(dwc);
  580. break;
  581. case USB_DR_MODE_HOST:
  582. dwc3_host_exit(dwc);
  583. break;
  584. case USB_DR_MODE_OTG:
  585. dwc3_host_exit(dwc);
  586. dwc3_gadget_exit(dwc);
  587. break;
  588. default:
  589. /* do nothing */
  590. break;
  591. }
  592. }
  593. #define DWC3_ALIGN_MASK (16 - 1)
  594. static int dwc3_probe(struct platform_device *pdev)
  595. {
  596. struct device *dev = &pdev->dev;
  597. struct dwc3_platform_data *pdata = dev_get_platdata(dev);
  598. struct device_node *node = dev->of_node;
  599. struct resource *res;
  600. struct dwc3 *dwc;
  601. u8 lpm_nyet_threshold;
  602. int ret;
  603. void __iomem *regs;
  604. void *mem;
  605. mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
  606. if (!mem)
  607. return -ENOMEM;
  608. dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
  609. dwc->mem = mem;
  610. dwc->dev = dev;
  611. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  612. if (!res) {
  613. dev_err(dev, "missing IRQ\n");
  614. return -ENODEV;
  615. }
  616. dwc->xhci_resources[1].start = res->start;
  617. dwc->xhci_resources[1].end = res->end;
  618. dwc->xhci_resources[1].flags = res->flags;
  619. dwc->xhci_resources[1].name = res->name;
  620. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  621. if (!res) {
  622. dev_err(dev, "missing memory resource\n");
  623. return -ENODEV;
  624. }
  625. dwc->xhci_resources[0].start = res->start;
  626. dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
  627. DWC3_XHCI_REGS_END;
  628. dwc->xhci_resources[0].flags = res->flags;
  629. dwc->xhci_resources[0].name = res->name;
  630. res->start += DWC3_GLOBALS_REGS_START;
  631. /*
  632. * Request memory region but exclude xHCI regs,
  633. * since it will be requested by the xhci-plat driver.
  634. */
  635. regs = devm_ioremap_resource(dev, res);
  636. if (IS_ERR(regs))
  637. return PTR_ERR(regs);
  638. dwc->regs = regs;
  639. dwc->regs_size = resource_size(res);
  640. /*
  641. * restore res->start back to its original value so that,
  642. * in case the probe is deferred, we don't end up getting error in
  643. * request the memory region the next time probe is called.
  644. */
  645. res->start -= DWC3_GLOBALS_REGS_START;
  646. /* default to highest possible threshold */
  647. lpm_nyet_threshold = 0xff;
  648. if (node) {
  649. dwc->maximum_speed = of_usb_get_maximum_speed(node);
  650. dwc->has_lpm_erratum = of_property_read_bool(node,
  651. "snps,has-lpm-erratum");
  652. of_property_read_u8(node, "snps,lpm-nyet-threshold",
  653. &lpm_nyet_threshold);
  654. dwc->needs_fifo_resize = of_property_read_bool(node,
  655. "tx-fifo-resize");
  656. dwc->dr_mode = of_usb_get_dr_mode(node);
  657. dwc->disable_scramble_quirk = of_property_read_bool(node,
  658. "snps,disable_scramble_quirk");
  659. dwc->u2exit_lfps_quirk = of_property_read_bool(node,
  660. "snps,u2exit_lfps_quirk");
  661. dwc->u2ss_inp3_quirk = of_property_read_bool(node,
  662. "snps,u2ss_inp3_quirk");
  663. dwc->req_p1p2p3_quirk = of_property_read_bool(node,
  664. "snps,req_p1p2p3_quirk");
  665. dwc->del_p1p2p3_quirk = of_property_read_bool(node,
  666. "snps,del_p1p2p3_quirk");
  667. dwc->del_phy_power_chg_quirk = of_property_read_bool(node,
  668. "snps,del_phy_power_chg_quirk");
  669. dwc->lfps_filter_quirk = of_property_read_bool(node,
  670. "snps,lfps_filter_quirk");
  671. dwc->rx_detect_poll_quirk = of_property_read_bool(node,
  672. "snps,rx_detect_poll_quirk");
  673. } else if (pdata) {
  674. dwc->maximum_speed = pdata->maximum_speed;
  675. dwc->has_lpm_erratum = pdata->has_lpm_erratum;
  676. if (pdata->lpm_nyet_threshold)
  677. lpm_nyet_threshold = pdata->lpm_nyet_threshold;
  678. dwc->needs_fifo_resize = pdata->tx_fifo_resize;
  679. dwc->dr_mode = pdata->dr_mode;
  680. dwc->disable_scramble_quirk = pdata->disable_scramble_quirk;
  681. dwc->u2exit_lfps_quirk = pdata->u2exit_lfps_quirk;
  682. dwc->u2ss_inp3_quirk = pdata->u2ss_inp3_quirk;
  683. dwc->req_p1p2p3_quirk = pdata->req_p1p2p3_quirk;
  684. dwc->del_p1p2p3_quirk = pdata->del_p1p2p3_quirk;
  685. dwc->del_phy_power_chg_quirk = pdata->del_phy_power_chg_quirk;
  686. dwc->lfps_filter_quirk = pdata->lfps_filter_quirk;
  687. dwc->rx_detect_poll_quirk = pdata->rx_detect_poll_quirk;
  688. }
  689. /* default to superspeed if no maximum_speed passed */
  690. if (dwc->maximum_speed == USB_SPEED_UNKNOWN)
  691. dwc->maximum_speed = USB_SPEED_SUPER;
  692. dwc->lpm_nyet_threshold = lpm_nyet_threshold;
  693. ret = dwc3_core_get_phy(dwc);
  694. if (ret)
  695. return ret;
  696. spin_lock_init(&dwc->lock);
  697. platform_set_drvdata(pdev, dwc);
  698. if (!dev->dma_mask) {
  699. dev->dma_mask = dev->parent->dma_mask;
  700. dev->dma_parms = dev->parent->dma_parms;
  701. dma_set_coherent_mask(dev, dev->parent->coherent_dma_mask);
  702. }
  703. pm_runtime_enable(dev);
  704. pm_runtime_get_sync(dev);
  705. pm_runtime_forbid(dev);
  706. dwc3_cache_hwparams(dwc);
  707. ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
  708. if (ret) {
  709. dev_err(dwc->dev, "failed to allocate event buffers\n");
  710. ret = -ENOMEM;
  711. goto err0;
  712. }
  713. if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
  714. dwc->dr_mode = USB_DR_MODE_HOST;
  715. else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
  716. dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
  717. if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
  718. dwc->dr_mode = USB_DR_MODE_OTG;
  719. ret = dwc3_core_init(dwc);
  720. if (ret) {
  721. dev_err(dev, "failed to initialize core\n");
  722. goto err0;
  723. }
  724. usb_phy_set_suspend(dwc->usb2_phy, 0);
  725. usb_phy_set_suspend(dwc->usb3_phy, 0);
  726. ret = phy_power_on(dwc->usb2_generic_phy);
  727. if (ret < 0)
  728. goto err1;
  729. ret = phy_power_on(dwc->usb3_generic_phy);
  730. if (ret < 0)
  731. goto err_usb2phy_power;
  732. ret = dwc3_event_buffers_setup(dwc);
  733. if (ret) {
  734. dev_err(dwc->dev, "failed to setup event buffers\n");
  735. goto err_usb3phy_power;
  736. }
  737. ret = dwc3_core_init_mode(dwc);
  738. if (ret)
  739. goto err2;
  740. ret = dwc3_debugfs_init(dwc);
  741. if (ret) {
  742. dev_err(dev, "failed to initialize debugfs\n");
  743. goto err3;
  744. }
  745. pm_runtime_allow(dev);
  746. return 0;
  747. err3:
  748. dwc3_core_exit_mode(dwc);
  749. err2:
  750. dwc3_event_buffers_cleanup(dwc);
  751. err_usb3phy_power:
  752. phy_power_off(dwc->usb3_generic_phy);
  753. err_usb2phy_power:
  754. phy_power_off(dwc->usb2_generic_phy);
  755. err1:
  756. usb_phy_set_suspend(dwc->usb2_phy, 1);
  757. usb_phy_set_suspend(dwc->usb3_phy, 1);
  758. dwc3_core_exit(dwc);
  759. err0:
  760. dwc3_free_event_buffers(dwc);
  761. return ret;
  762. }
  763. static int dwc3_remove(struct platform_device *pdev)
  764. {
  765. struct dwc3 *dwc = platform_get_drvdata(pdev);
  766. dwc3_debugfs_exit(dwc);
  767. dwc3_core_exit_mode(dwc);
  768. dwc3_event_buffers_cleanup(dwc);
  769. dwc3_free_event_buffers(dwc);
  770. usb_phy_set_suspend(dwc->usb2_phy, 1);
  771. usb_phy_set_suspend(dwc->usb3_phy, 1);
  772. phy_power_off(dwc->usb2_generic_phy);
  773. phy_power_off(dwc->usb3_generic_phy);
  774. dwc3_core_exit(dwc);
  775. pm_runtime_put_sync(&pdev->dev);
  776. pm_runtime_disable(&pdev->dev);
  777. return 0;
  778. }
  779. #ifdef CONFIG_PM_SLEEP
  780. static int dwc3_suspend(struct device *dev)
  781. {
  782. struct dwc3 *dwc = dev_get_drvdata(dev);
  783. unsigned long flags;
  784. spin_lock_irqsave(&dwc->lock, flags);
  785. switch (dwc->dr_mode) {
  786. case USB_DR_MODE_PERIPHERAL:
  787. case USB_DR_MODE_OTG:
  788. dwc3_gadget_suspend(dwc);
  789. /* FALLTHROUGH */
  790. case USB_DR_MODE_HOST:
  791. default:
  792. dwc3_event_buffers_cleanup(dwc);
  793. break;
  794. }
  795. dwc->gctl = dwc3_readl(dwc->regs, DWC3_GCTL);
  796. spin_unlock_irqrestore(&dwc->lock, flags);
  797. usb_phy_shutdown(dwc->usb3_phy);
  798. usb_phy_shutdown(dwc->usb2_phy);
  799. phy_exit(dwc->usb2_generic_phy);
  800. phy_exit(dwc->usb3_generic_phy);
  801. return 0;
  802. }
  803. static int dwc3_resume(struct device *dev)
  804. {
  805. struct dwc3 *dwc = dev_get_drvdata(dev);
  806. unsigned long flags;
  807. int ret;
  808. usb_phy_init(dwc->usb3_phy);
  809. usb_phy_init(dwc->usb2_phy);
  810. ret = phy_init(dwc->usb2_generic_phy);
  811. if (ret < 0)
  812. return ret;
  813. ret = phy_init(dwc->usb3_generic_phy);
  814. if (ret < 0)
  815. goto err_usb2phy_init;
  816. spin_lock_irqsave(&dwc->lock, flags);
  817. dwc3_event_buffers_setup(dwc);
  818. dwc3_writel(dwc->regs, DWC3_GCTL, dwc->gctl);
  819. switch (dwc->dr_mode) {
  820. case USB_DR_MODE_PERIPHERAL:
  821. case USB_DR_MODE_OTG:
  822. dwc3_gadget_resume(dwc);
  823. /* FALLTHROUGH */
  824. case USB_DR_MODE_HOST:
  825. default:
  826. /* do nothing */
  827. break;
  828. }
  829. spin_unlock_irqrestore(&dwc->lock, flags);
  830. pm_runtime_disable(dev);
  831. pm_runtime_set_active(dev);
  832. pm_runtime_enable(dev);
  833. return 0;
  834. err_usb2phy_init:
  835. phy_exit(dwc->usb2_generic_phy);
  836. return ret;
  837. }
  838. static const struct dev_pm_ops dwc3_dev_pm_ops = {
  839. SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
  840. };
  841. #define DWC3_PM_OPS &(dwc3_dev_pm_ops)
  842. #else
  843. #define DWC3_PM_OPS NULL
  844. #endif
  845. #ifdef CONFIG_OF
  846. static const struct of_device_id of_dwc3_match[] = {
  847. {
  848. .compatible = "snps,dwc3"
  849. },
  850. {
  851. .compatible = "synopsys,dwc3"
  852. },
  853. { },
  854. };
  855. MODULE_DEVICE_TABLE(of, of_dwc3_match);
  856. #endif
  857. #ifdef CONFIG_ACPI
  858. #define ACPI_ID_INTEL_BSW "808622B7"
  859. static const struct acpi_device_id dwc3_acpi_match[] = {
  860. { ACPI_ID_INTEL_BSW, 0 },
  861. { },
  862. };
  863. MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
  864. #endif
  865. static struct platform_driver dwc3_driver = {
  866. .probe = dwc3_probe,
  867. .remove = dwc3_remove,
  868. .driver = {
  869. .name = "dwc3",
  870. .of_match_table = of_match_ptr(of_dwc3_match),
  871. .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
  872. .pm = DWC3_PM_OPS,
  873. },
  874. };
  875. module_platform_driver(dwc3_driver);
  876. MODULE_ALIAS("platform:dwc3");
  877. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  878. MODULE_LICENSE("GPL v2");
  879. MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");