core.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions, and the following disclaimer,
  14. * without modification.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. The names of the above-listed copyright holders may not be used
  19. * to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * ALTERNATIVELY, this software may be distributed under the terms of the
  23. * GNU General Public License ("GPL") version 2, as published by the Free
  24. * Software Foundation.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  27. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  30. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  31. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  32. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  33. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. #include <linux/module.h>
  39. #include <linux/kernel.h>
  40. #include <linux/slab.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/platform_device.h>
  43. #include <linux/pm_runtime.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/ioport.h>
  46. #include <linux/io.h>
  47. #include <linux/list.h>
  48. #include <linux/delay.h>
  49. #include <linux/dma-mapping.h>
  50. #include <linux/of.h>
  51. #include <linux/usb/ch9.h>
  52. #include <linux/usb/gadget.h>
  53. #include "core.h"
  54. #include "gadget.h"
  55. #include "io.h"
  56. #include "debug.h"
  57. static char *maximum_speed = "super";
  58. module_param(maximum_speed, charp, 0);
  59. MODULE_PARM_DESC(maximum_speed, "Maximum supported speed.");
  60. /* -------------------------------------------------------------------------- */
  61. #define DWC3_DEVS_POSSIBLE 32
  62. static DECLARE_BITMAP(dwc3_devs, DWC3_DEVS_POSSIBLE);
  63. #if defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_F_PROJECT) || defined(CONFIG_SEC_K_PROJECT)
  64. static void sec_reconnect_work(struct work_struct *data)
  65. {
  66. struct dwc3 *udc = container_of(data, struct dwc3, reconnect_work);
  67. usb_gadget_disconnect(&udc->gadget);
  68. printk(KERN_ERR"usb: Disconnected in case of Super speed support \n");
  69. mdelay(1);
  70. usb_gadget_connect(&udc->gadget);
  71. }
  72. #define WORK_INIT(udc) \
  73. INIT_WORK(&udc->reconnect_work, sec_reconnect_work);
  74. #else
  75. #define WORK_INIT(udc)
  76. #endif
  77. int dwc3_get_device_id(void)
  78. {
  79. int id;
  80. again:
  81. id = find_first_zero_bit(dwc3_devs, DWC3_DEVS_POSSIBLE);
  82. if (id < DWC3_DEVS_POSSIBLE) {
  83. int old;
  84. old = test_and_set_bit(id, dwc3_devs);
  85. if (old)
  86. goto again;
  87. } else {
  88. pr_err("dwc3: no space for new device\n");
  89. id = -ENOMEM;
  90. }
  91. return id;
  92. }
  93. EXPORT_SYMBOL_GPL(dwc3_get_device_id);
  94. void dwc3_put_device_id(int id)
  95. {
  96. int ret;
  97. if (id < 0)
  98. return;
  99. ret = test_bit(id, dwc3_devs);
  100. WARN(!ret, "dwc3: ID %d not in use\n", id);
  101. smp_mb__before_clear_bit();
  102. clear_bit(id, dwc3_devs);
  103. }
  104. EXPORT_SYMBOL_GPL(dwc3_put_device_id);
  105. void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
  106. {
  107. u32 reg;
  108. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  109. reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
  110. reg |= DWC3_GCTL_PRTCAPDIR(mode);
  111. /*
  112. * Set this bit so that device attempts three more times at SS, even
  113. * if it failed previously to operate in SS mode.
  114. */
  115. reg |= DWC3_GCTL_U2RSTECN;
  116. if (mode == DWC3_GCTL_PRTCAP_HOST) {
  117. /*
  118. * Allow ITP generated off of ref clk based counter instead
  119. * of UTMI/ULPI clk based counter, when superspeed only is
  120. * active so that UTMI/ULPI PHY can be suspened.
  121. */
  122. reg |= DWC3_GCTL_SOFITPSYNC;
  123. reg &= ~(DWC3_GCTL_PWRDNSCALEMASK);
  124. reg |= DWC3_GCTL_PWRDNSCALE(2);
  125. } else if (mode == DWC3_GCTL_PRTCAP_DEVICE) {
  126. reg &= ~(DWC3_GCTL_PWRDNSCALEMASK);
  127. reg |= DWC3_GCTL_PWRDNSCALE(2);
  128. reg &= ~(DWC3_GCTL_SOFITPSYNC);
  129. }
  130. reg |= DWC3_GCTL_U2EXIT_LFPS;
  131. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  132. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  133. reg |= DWC3_GUSB3PIPECTL_SUSPHY;
  134. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  135. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  136. reg |= DWC3_GUSB2PHYCFG_SUSPHY;
  137. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  138. }
  139. /**
  140. * dwc3_core_soft_reset - Issues core soft reset and PHY reset
  141. * @dwc: pointer to our context structure
  142. */
  143. static void dwc3_core_soft_reset(struct dwc3 *dwc)
  144. {
  145. u32 reg;
  146. /* Before Resetting PHY, put Core in Reset */
  147. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  148. reg |= DWC3_GCTL_CORESOFTRESET;
  149. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  150. dwc3_notify_event(dwc, DWC3_CONTROLLER_RESET_EVENT);
  151. /* Assert USB3 PHY reset */
  152. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  153. reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
  154. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  155. /* Assert USB2 PHY reset */
  156. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  157. reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
  158. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  159. mdelay(100);
  160. /* Clear USB3 PHY reset */
  161. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  162. reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
  163. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  164. /* Clear USB2 PHY reset */
  165. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  166. reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
  167. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  168. mdelay(100);
  169. /* After PHYs are stable we can take Core out of reset state */
  170. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  171. reg &= ~DWC3_GCTL_CORESOFTRESET;
  172. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  173. dwc3_notify_event(dwc, DWC3_CONTROLLER_POST_RESET_EVENT);
  174. }
  175. /**
  176. * dwc3_free_one_event_buffer - Frees one event buffer
  177. * @dwc: Pointer to our controller context structure
  178. * @evt: Pointer to event buffer to be freed
  179. */
  180. static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
  181. struct dwc3_event_buffer *evt)
  182. {
  183. dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
  184. kfree(evt);
  185. }
  186. /**
  187. * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
  188. * @dwc: Pointer to our controller context structure
  189. * @length: size of the event buffer
  190. *
  191. * Returns a pointer to the allocated event buffer structure on success
  192. * otherwise ERR_PTR(errno).
  193. */
  194. static struct dwc3_event_buffer *__devinit
  195. dwc3_alloc_one_event_buffer(struct dwc3 *dwc, unsigned length)
  196. {
  197. struct dwc3_event_buffer *evt;
  198. evt = kzalloc(sizeof(*evt), GFP_KERNEL);
  199. if (!evt)
  200. return ERR_PTR(-ENOMEM);
  201. evt->dwc = dwc;
  202. evt->length = length;
  203. evt->buf = dma_alloc_coherent(dwc->dev, length,
  204. &evt->dma, GFP_KERNEL);
  205. if (!evt->buf) {
  206. kfree(evt);
  207. return ERR_PTR(-ENOMEM);
  208. }
  209. return evt;
  210. }
  211. /**
  212. * dwc3_free_event_buffers - frees all allocated event buffers
  213. * @dwc: Pointer to our controller context structure
  214. */
  215. static void dwc3_free_event_buffers(struct dwc3 *dwc)
  216. {
  217. struct dwc3_event_buffer *evt;
  218. int i;
  219. for (i = 0; i < dwc->num_event_buffers; i++) {
  220. evt = dwc->ev_buffs[i];
  221. if (evt)
  222. dwc3_free_one_event_buffer(dwc, evt);
  223. }
  224. kfree(dwc->ev_buffs);
  225. }
  226. /**
  227. * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
  228. * @dwc: pointer to our controller context structure
  229. * @length: size of event buffer
  230. *
  231. * Returns 0 on success otherwise negative errno. In the error case, dwc
  232. * may contain some buffers allocated but not all which were requested.
  233. */
  234. static int __devinit dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
  235. {
  236. int num;
  237. int i;
  238. num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
  239. dwc->num_event_buffers = num;
  240. dwc->ev_buffs = kzalloc(sizeof(*dwc->ev_buffs) * num, GFP_KERNEL);
  241. if (!dwc->ev_buffs) {
  242. dev_err(dwc->dev, "can't allocate event buffers array\n");
  243. return -ENOMEM;
  244. }
  245. for (i = 0; i < num; i++) {
  246. struct dwc3_event_buffer *evt;
  247. /*
  248. * As SW workaround, allocate 8 bytes more than size of event
  249. * buffer given to USB Controller to avoid possible memory
  250. * corruption caused by event buffer overflow when Hw writes
  251. * Vendor Device test event which could be of 12 bytes.
  252. */
  253. evt = dwc3_alloc_one_event_buffer(dwc, (length + 8));
  254. if (IS_ERR(evt)) {
  255. dev_err(dwc->dev, "can't allocate event buffer\n");
  256. return PTR_ERR(evt);
  257. }
  258. dwc->ev_buffs[i] = evt;
  259. }
  260. return 0;
  261. }
  262. /**
  263. * dwc3_event_buffers_setup - setup our allocated event buffers
  264. * @dwc: pointer to our controller context structure
  265. *
  266. * Returns 0 on success otherwise negative errno.
  267. */
  268. int dwc3_event_buffers_setup(struct dwc3 *dwc)
  269. {
  270. struct dwc3_event_buffer *evt;
  271. int n;
  272. for (n = 0; n < dwc->num_event_buffers; n++) {
  273. evt = dwc->ev_buffs[n];
  274. dev_dbg(dwc->dev, "Event buf %pK dma %08llx length %d\n",
  275. evt->buf, (unsigned long long) evt->dma,
  276. evt->length);
  277. evt->lpos = 0;
  278. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
  279. lower_32_bits(evt->dma));
  280. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
  281. upper_32_bits(evt->dma));
  282. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
  283. (evt->length - 8) & 0xffff);
  284. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
  285. }
  286. return 0;
  287. }
  288. static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
  289. {
  290. struct dwc3_event_buffer *evt;
  291. int n;
  292. for (n = 0; n < dwc->num_event_buffers; n++) {
  293. evt = dwc->ev_buffs[n];
  294. evt->lpos = 0;
  295. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
  296. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
  297. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0);
  298. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
  299. }
  300. }
  301. static void dwc3_core_num_eps(struct dwc3 *dwc)
  302. {
  303. struct dwc3_hwparams *parms = &dwc->hwparams;
  304. dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
  305. dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
  306. dev_vdbg(dwc->dev, "found %d IN and %d OUT endpoints\n",
  307. dwc->num_in_eps, dwc->num_out_eps);
  308. }
  309. static void dwc3_cache_hwparams(struct dwc3 *dwc)
  310. {
  311. struct dwc3_hwparams *parms = &dwc->hwparams;
  312. parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
  313. parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
  314. parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
  315. parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
  316. parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
  317. parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
  318. parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
  319. parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
  320. parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
  321. }
  322. /**
  323. * dwc3_core_init - Low-level initialization of DWC3 Core
  324. * @dwc: Pointer to our controller context structure
  325. *
  326. * Returns 0 on success otherwise negative errno.
  327. */
  328. static int dwc3_core_init(struct dwc3 *dwc)
  329. {
  330. unsigned long timeout;
  331. u32 reg;
  332. int ret;
  333. reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
  334. /* This should read as U3 followed by revision number */
  335. if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
  336. dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
  337. ret = -ENODEV;
  338. goto err0;
  339. }
  340. dwc->revision = reg;
  341. /* issue device SoftReset too */
  342. timeout = jiffies + msecs_to_jiffies(500);
  343. dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
  344. do {
  345. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  346. if (!(reg & DWC3_DCTL_CSFTRST))
  347. break;
  348. if (time_after(jiffies, timeout)) {
  349. dev_err(dwc->dev, "Reset Timed Out\n");
  350. ret = -ETIMEDOUT;
  351. goto err0;
  352. }
  353. cpu_relax();
  354. } while (true);
  355. dwc3_core_soft_reset(dwc);
  356. dwc3_cache_hwparams(dwc);
  357. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  358. reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
  359. reg &= ~DWC3_GCTL_DISSCRAMBLE;
  360. switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
  361. case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
  362. reg &= ~DWC3_GCTL_DSBLCLKGTNG;
  363. break;
  364. default:
  365. dev_dbg(dwc->dev, "No power optimization available\n");
  366. }
  367. /*
  368. * WORKAROUND: DWC3 revisions <1.90a have a bug
  369. * where the device can fail to connect at SuperSpeed
  370. * and falls back to high-speed mode which causes
  371. * the device to enter a Connect/Disconnect loop
  372. */
  373. if (dwc->revision < DWC3_REVISION_190A)
  374. reg |= DWC3_GCTL_U2RSTECN;
  375. dwc3_core_num_eps(dwc);
  376. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  377. /*
  378. * The default value of GUCTL[31:22] should be 0x8. But on cores
  379. * revision < 2.30a, the default value is mistakenly overridden
  380. * with 0x0. Restore the correct default value.
  381. */
  382. if (dwc->revision < DWC3_REVISION_230A) {
  383. reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
  384. reg &= ~DWC3_GUCTL_REFCLKPER;
  385. reg |= 0x8 << __ffs(DWC3_GUCTL_REFCLKPER);
  386. dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
  387. }
  388. /*
  389. * Currently, the default and the recommended value for GUSB3PIPECTL
  390. * [21:19] in the RTL is 3'b100 or 32 consecutive errors. Based on
  391. * analysis and experiments in the lab, it is found that there is a
  392. * relatively low probability of getting 32 consecutive word errors
  393. * in the presence of random recovered noise (during electrical idle).
  394. * This can delay the entry to a low power state such that for
  395. * applications where the link stays in a non-U0 state for a short
  396. * duration (< 1 microsecond), the local PHY does not enter the low
  397. * power state prior to receiving a potential LFPS wakeup. This causes
  398. * the PHY CDR (Clock and Data Recovery) operation to be unstable for
  399. * some Synopsys PHYs.
  400. *
  401. * The proposal now is to change the default and the recommended value
  402. * for GUSB3PIPECTL[21:19] in the RTL from 3'b100 to a minimum of
  403. * 3'b001. Perform the same in software for controllers prior to 2.30a
  404. * revision.
  405. */
  406. if (dwc->revision < DWC3_REVISION_230A) {
  407. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  408. reg &= ~DWC3_GUSB3PIPECTL_DELAY_P1P2P3;
  409. reg |= 1 << __ffs(DWC3_GUSB3PIPECTL_DELAY_P1P2P3);
  410. /*
  411. * Receiver Detection in U3/Rx.Det is mistakenly disabled in
  412. * cores < 2.30a. Fix it here.
  413. */
  414. reg &= ~DWC3_GUSB3PIPECTL_DIS_RXDET_U3_RXDET;
  415. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  416. }
  417. /*
  418. * clear Elastic buffer mode in GUSBPIPE_CTRL(0) register, otherwise
  419. * it results in high link errors and could cause SS mode transfer
  420. * failure.
  421. */
  422. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  423. reg &= ~DWC3_GUSB3PIPECTL_ELASTIC_BUF_MODE;
  424. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  425. if (!dwc->ev_buffs) {
  426. ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
  427. if (ret) {
  428. dev_err(dwc->dev, "failed to allocate event buffers\n");
  429. ret = -ENOMEM;
  430. goto err1;
  431. }
  432. }
  433. ret = dwc3_event_buffers_setup(dwc);
  434. if (ret) {
  435. dev_err(dwc->dev, "failed to setup event buffers\n");
  436. goto err1;
  437. }
  438. return 0;
  439. err1:
  440. dwc3_free_event_buffers(dwc);
  441. err0:
  442. return ret;
  443. }
  444. static void dwc3_core_exit(struct dwc3 *dwc)
  445. {
  446. dwc3_event_buffers_cleanup(dwc);
  447. dwc3_free_event_buffers(dwc);
  448. }
  449. /* XHCI reset, resets other CORE registers as well, re-init those */
  450. void dwc3_post_host_reset_core_init(struct dwc3 *dwc)
  451. {
  452. dwc3_core_init(dwc);
  453. dwc3_gadget_restart(dwc);
  454. dwc3_notify_event(dwc, DWC3_CONTROLLER_POST_INITIALIZATION_EVENT);
  455. }
  456. static void (*notify_event) (struct dwc3 *, unsigned);
  457. void dwc3_set_notifier(void (*notify)(struct dwc3 *, unsigned))
  458. {
  459. notify_event = notify;
  460. }
  461. EXPORT_SYMBOL(dwc3_set_notifier);
  462. int dwc3_notify_event(struct dwc3 *dwc, unsigned event)
  463. {
  464. int ret = 0;
  465. if (dwc->notify_event)
  466. dwc->notify_event(dwc, event);
  467. else
  468. ret = -ENODEV;
  469. return ret;
  470. }
  471. EXPORT_SYMBOL(dwc3_notify_event);
  472. #define DWC3_ALIGN_MASK (16 - 1)
  473. static u64 dwc3_dma_mask = DMA_BIT_MASK(64);
  474. static int __devinit dwc3_probe(struct platform_device *pdev)
  475. {
  476. struct device_node *node = pdev->dev.of_node;
  477. struct resource *res;
  478. struct dwc3 *dwc;
  479. struct device *dev = &pdev->dev;
  480. int ret = -ENOMEM;
  481. void __iomem *regs;
  482. void *mem;
  483. u8 mode;
  484. bool host_only_mode;
  485. mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
  486. if (!mem) {
  487. dev_err(dev, "not enough memory\n");
  488. return -ENOMEM;
  489. }
  490. dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
  491. dwc->mem = mem;
  492. if (!dev->dma_mask)
  493. dev->dma_mask = &dwc3_dma_mask;
  494. if (!dev->coherent_dma_mask)
  495. dev->coherent_dma_mask = DMA_BIT_MASK(64);
  496. dwc->notify_event = notify_event;
  497. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  498. if (!res) {
  499. dev_err(dev, "missing IRQ\n");
  500. return -ENODEV;
  501. }
  502. dwc->xhci_resources[1].start = res->start;
  503. dwc->xhci_resources[1].end = res->end;
  504. dwc->xhci_resources[1].flags = res->flags;
  505. dwc->xhci_resources[1].name = res->name;
  506. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  507. if (!res) {
  508. dev_err(dev, "missing memory resource\n");
  509. return -ENODEV;
  510. }
  511. dwc->xhci_resources[0].start = res->start;
  512. dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
  513. DWC3_XHCI_REGS_END;
  514. dwc->xhci_resources[0].flags = res->flags;
  515. dwc->xhci_resources[0].name = res->name;
  516. /*
  517. * Request memory region but exclude xHCI regs,
  518. * since it will be requested by the xhci-plat driver.
  519. */
  520. res = devm_request_mem_region(dev, res->start + DWC3_GLOBALS_REGS_START,
  521. resource_size(res) - DWC3_GLOBALS_REGS_START,
  522. dev_name(dev));
  523. if (!res) {
  524. dev_err(dev, "can't request mem region\n");
  525. return -ENOMEM;
  526. }
  527. regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
  528. if (!regs) {
  529. dev_err(dev, "ioremap failed\n");
  530. return -ENOMEM;
  531. }
  532. spin_lock_init(&dwc->lock);
  533. platform_set_drvdata(pdev, dwc);
  534. dwc->regs = regs;
  535. dwc->regs_size = resource_size(res);
  536. dwc->dev = dev;
  537. if (!strncmp("super", maximum_speed, 5))
  538. dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
  539. else if (!strncmp("high", maximum_speed, 4))
  540. dwc->maximum_speed = DWC3_DCFG_HIGHSPEED;
  541. else if (!strncmp("full", maximum_speed, 4))
  542. dwc->maximum_speed = DWC3_DCFG_FULLSPEED1;
  543. else if (!strncmp("low", maximum_speed, 3))
  544. dwc->maximum_speed = DWC3_DCFG_LOWSPEED;
  545. else
  546. dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
  547. #if defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_F_PROJECT) || defined(CONFIG_SEC_K_PROJECT)
  548. dwc->speed_limit = dwc->maximum_speed;
  549. dwc->ss_host_avail = -1;
  550. #endif
  551. dwc->needs_fifo_resize = of_property_read_bool(node, "tx-fifo-resize");
  552. host_only_mode = of_property_read_bool(node, "host-only-mode");
  553. pm_runtime_no_callbacks(dev);
  554. pm_runtime_set_active(dev);
  555. pm_runtime_enable(dev);
  556. ret = dwc3_core_init(dwc);
  557. if (ret) {
  558. dev_err(dev, "failed to initialize core\n");
  559. return ret;
  560. }
  561. mode = DWC3_MODE(dwc->hwparams.hwparams0);
  562. /* Override mode if user selects host-only config with DRD core */
  563. if (host_only_mode && (mode == DWC3_MODE_DRD)) {
  564. dev_dbg(dev, "host only mode selected\n");
  565. mode = DWC3_MODE_HOST;
  566. }
  567. switch (mode) {
  568. case DWC3_MODE_DEVICE:
  569. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
  570. ret = dwc3_gadget_init(dwc);
  571. if (ret) {
  572. dev_err(dev, "failed to initialize gadget\n");
  573. goto err1;
  574. }
  575. break;
  576. case DWC3_MODE_HOST:
  577. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
  578. ret = dwc3_host_init(dwc);
  579. if (ret) {
  580. dev_err(dev, "failed to initialize host\n");
  581. goto err1;
  582. }
  583. break;
  584. case DWC3_MODE_DRD:
  585. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
  586. ret = dwc3_otg_init(dwc);
  587. if (ret) {
  588. dev_err(dev, "failed to initialize otg\n");
  589. goto err1;
  590. }
  591. ret = dwc3_host_init(dwc);
  592. if (ret) {
  593. dev_err(dev, "failed to initialize host\n");
  594. dwc3_otg_exit(dwc);
  595. goto err1;
  596. }
  597. ret = dwc3_gadget_init(dwc);
  598. if (ret) {
  599. dev_err(dev, "failed to initialize gadget\n");
  600. dwc3_host_exit(dwc);
  601. dwc3_otg_exit(dwc);
  602. goto err1;
  603. }
  604. break;
  605. default:
  606. dev_err(dev, "Unsupported mode of operation %d\n", mode);
  607. goto err1;
  608. }
  609. dwc->mode = mode;
  610. ret = dwc3_debugfs_init(dwc);
  611. if (ret) {
  612. dev_err(dev, "failed to initialize debugfs\n");
  613. goto err2;
  614. }
  615. dwc3_notify_event(dwc, DWC3_CONTROLLER_POST_INITIALIZATION_EVENT);
  616. #if defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_F_PROJECT) || defined(CONFIG_SEC_K_PROJECT)
  617. WORK_INIT(dwc);
  618. #endif
  619. return 0;
  620. err2:
  621. switch (mode) {
  622. case DWC3_MODE_DEVICE:
  623. dwc3_gadget_exit(dwc);
  624. break;
  625. case DWC3_MODE_HOST:
  626. dwc3_host_exit(dwc);
  627. break;
  628. case DWC3_MODE_DRD:
  629. dwc3_gadget_exit(dwc);
  630. dwc3_host_exit(dwc);
  631. dwc3_otg_exit(dwc);
  632. break;
  633. default:
  634. /* do nothing */
  635. break;
  636. }
  637. err1:
  638. dwc3_core_exit(dwc);
  639. return ret;
  640. }
  641. static int __devexit dwc3_remove(struct platform_device *pdev)
  642. {
  643. struct dwc3 *dwc = platform_get_drvdata(pdev);
  644. struct resource *res;
  645. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  646. dwc3_debugfs_exit(dwc);
  647. switch (dwc->mode) {
  648. case DWC3_MODE_DEVICE:
  649. dwc3_gadget_exit(dwc);
  650. break;
  651. case DWC3_MODE_HOST:
  652. dwc3_host_exit(dwc);
  653. break;
  654. case DWC3_MODE_DRD:
  655. dwc3_gadget_exit(dwc);
  656. dwc3_host_exit(dwc);
  657. dwc3_otg_exit(dwc);
  658. break;
  659. default:
  660. /* do nothing */
  661. break;
  662. }
  663. dwc3_core_exit(dwc);
  664. pm_runtime_disable(&pdev->dev);
  665. return 0;
  666. }
  667. #ifdef CONFIG_OF
  668. static const struct of_device_id of_dwc3_match[] = {
  669. {
  670. .compatible = "synopsys,dwc3"
  671. },
  672. { },
  673. };
  674. MODULE_DEVICE_TABLE(of, of_dwc3_match);
  675. #endif
  676. static struct platform_driver dwc3_driver = {
  677. .probe = dwc3_probe,
  678. .remove = __devexit_p(dwc3_remove),
  679. .driver = {
  680. .name = "dwc3",
  681. .of_match_table = of_match_ptr(of_dwc3_match),
  682. },
  683. };
  684. module_platform_driver(dwc3_driver);
  685. MODULE_ALIAS("platform:dwc3");
  686. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  687. MODULE_LICENSE("Dual BSD/GPL");
  688. MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");