core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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/usb/ch9.h>
  51. #include <linux/usb/gadget.h>
  52. #include <linux/module.h>
  53. #include "core.h"
  54. #include "gadget.h"
  55. #include "io.h"
  56. #include "debug.h"
  57. /**
  58. * dwc3_core_soft_reset - Issues core soft reset and PHY reset
  59. * @dwc: pointer to our context structure
  60. */
  61. static void dwc3_core_soft_reset(struct dwc3 *dwc)
  62. {
  63. u32 reg;
  64. /* Before Resetting PHY, put Core in Reset */
  65. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  66. reg |= DWC3_GCTL_CORESOFTRESET;
  67. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  68. /* Assert USB3 PHY reset */
  69. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  70. reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
  71. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  72. /* Assert USB2 PHY reset */
  73. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  74. reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
  75. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  76. mdelay(100);
  77. /* Clear USB3 PHY reset */
  78. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  79. reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
  80. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  81. /* Clear USB2 PHY reset */
  82. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  83. reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
  84. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  85. /* After PHYs are stable we can take Core out of reset state */
  86. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  87. reg &= ~DWC3_GCTL_CORESOFTRESET;
  88. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  89. }
  90. /**
  91. * dwc3_free_one_event_buffer - Frees one event buffer
  92. * @dwc: Pointer to our controller context structure
  93. * @evt: Pointer to event buffer to be freed
  94. */
  95. static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
  96. struct dwc3_event_buffer *evt)
  97. {
  98. dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
  99. kfree(evt);
  100. }
  101. /**
  102. * dwc3_alloc_one_event_buffer - Allocated one event buffer structure
  103. * @dwc: Pointer to our controller context structure
  104. * @length: size of the event buffer
  105. *
  106. * Returns a pointer to the allocated event buffer structure on succes
  107. * otherwise ERR_PTR(errno).
  108. */
  109. static struct dwc3_event_buffer *__devinit
  110. dwc3_alloc_one_event_buffer(struct dwc3 *dwc, unsigned length)
  111. {
  112. struct dwc3_event_buffer *evt;
  113. evt = kzalloc(sizeof(*evt), GFP_KERNEL);
  114. if (!evt)
  115. return ERR_PTR(-ENOMEM);
  116. evt->dwc = dwc;
  117. evt->length = length;
  118. evt->buf = dma_alloc_coherent(dwc->dev, length,
  119. &evt->dma, GFP_KERNEL);
  120. if (!evt->buf) {
  121. kfree(evt);
  122. return ERR_PTR(-ENOMEM);
  123. }
  124. return evt;
  125. }
  126. /**
  127. * dwc3_free_event_buffers - frees all allocated event buffers
  128. * @dwc: Pointer to our controller context structure
  129. */
  130. static void dwc3_free_event_buffers(struct dwc3 *dwc)
  131. {
  132. struct dwc3_event_buffer *evt;
  133. int i;
  134. for (i = 0; i < DWC3_EVENT_BUFFERS_NUM; i++) {
  135. evt = dwc->ev_buffs[i];
  136. if (evt) {
  137. dwc3_free_one_event_buffer(dwc, evt);
  138. dwc->ev_buffs[i] = NULL;
  139. }
  140. }
  141. }
  142. /**
  143. * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
  144. * @dwc: Pointer to out controller context structure
  145. * @num: number of event buffers to allocate
  146. * @length: size of event buffer
  147. *
  148. * Returns 0 on success otherwise negative errno. In error the case, dwc
  149. * may contain some buffers allocated but not all which were requested.
  150. */
  151. static int __devinit dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned num,
  152. unsigned length)
  153. {
  154. int i;
  155. for (i = 0; i < num; i++) {
  156. struct dwc3_event_buffer *evt;
  157. evt = dwc3_alloc_one_event_buffer(dwc, length);
  158. if (IS_ERR(evt)) {
  159. dev_err(dwc->dev, "can't allocate event buffer\n");
  160. return PTR_ERR(evt);
  161. }
  162. dwc->ev_buffs[i] = evt;
  163. }
  164. return 0;
  165. }
  166. /**
  167. * dwc3_event_buffers_setup - setup our allocated event buffers
  168. * @dwc: Pointer to out controller context structure
  169. *
  170. * Returns 0 on success otherwise negative errno.
  171. */
  172. static int __devinit dwc3_event_buffers_setup(struct dwc3 *dwc)
  173. {
  174. struct dwc3_event_buffer *evt;
  175. int n;
  176. for (n = 0; n < DWC3_EVENT_BUFFERS_NUM; n++) {
  177. evt = dwc->ev_buffs[n];
  178. dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
  179. evt->buf, (unsigned long long) evt->dma,
  180. evt->length);
  181. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
  182. lower_32_bits(evt->dma));
  183. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
  184. upper_32_bits(evt->dma));
  185. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
  186. evt->length & 0xffff);
  187. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
  188. }
  189. return 0;
  190. }
  191. static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
  192. {
  193. struct dwc3_event_buffer *evt;
  194. int n;
  195. for (n = 0; n < DWC3_EVENT_BUFFERS_NUM; n++) {
  196. evt = dwc->ev_buffs[n];
  197. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
  198. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
  199. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0);
  200. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
  201. }
  202. }
  203. static void __devinit dwc3_cache_hwparams(struct dwc3 *dwc)
  204. {
  205. struct dwc3_hwparams *parms = &dwc->hwparams;
  206. parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
  207. parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
  208. parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
  209. parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
  210. parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
  211. parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
  212. parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
  213. parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
  214. parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
  215. }
  216. /**
  217. * dwc3_core_init - Low-level initialization of DWC3 Core
  218. * @dwc: Pointer to our controller context structure
  219. *
  220. * Returns 0 on success otherwise negative errno.
  221. */
  222. static int __devinit dwc3_core_init(struct dwc3 *dwc)
  223. {
  224. unsigned long timeout;
  225. u32 reg;
  226. int ret;
  227. reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
  228. /* This should read as U3 followed by revision number */
  229. if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
  230. dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
  231. ret = -ENODEV;
  232. goto err0;
  233. }
  234. dwc->revision = reg;
  235. dwc3_core_soft_reset(dwc);
  236. /* issue device SoftReset too */
  237. timeout = jiffies + msecs_to_jiffies(500);
  238. dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
  239. do {
  240. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  241. if (!(reg & DWC3_DCTL_CSFTRST))
  242. break;
  243. if (time_after(jiffies, timeout)) {
  244. dev_err(dwc->dev, "Reset Timed Out\n");
  245. ret = -ETIMEDOUT;
  246. goto err0;
  247. }
  248. cpu_relax();
  249. } while (true);
  250. ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_NUM,
  251. DWC3_EVENT_BUFFERS_SIZE);
  252. if (ret) {
  253. dev_err(dwc->dev, "failed to allocate event buffers\n");
  254. ret = -ENOMEM;
  255. goto err1;
  256. }
  257. ret = dwc3_event_buffers_setup(dwc);
  258. if (ret) {
  259. dev_err(dwc->dev, "failed to setup event buffers\n");
  260. goto err1;
  261. }
  262. dwc3_cache_hwparams(dwc);
  263. return 0;
  264. err1:
  265. dwc3_free_event_buffers(dwc);
  266. err0:
  267. return ret;
  268. }
  269. static void dwc3_core_exit(struct dwc3 *dwc)
  270. {
  271. dwc3_event_buffers_cleanup(dwc);
  272. dwc3_free_event_buffers(dwc);
  273. }
  274. #define DWC3_ALIGN_MASK (16 - 1)
  275. static int __devinit dwc3_probe(struct platform_device *pdev)
  276. {
  277. const struct platform_device_id *id = platform_get_device_id(pdev);
  278. struct resource *res;
  279. struct dwc3 *dwc;
  280. void __iomem *regs;
  281. unsigned int features = id->driver_data;
  282. int ret = -ENOMEM;
  283. int irq;
  284. void *mem;
  285. mem = kzalloc(sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
  286. if (!mem) {
  287. dev_err(&pdev->dev, "not enough memory\n");
  288. goto err0;
  289. }
  290. dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
  291. dwc->mem = mem;
  292. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  293. if (!res) {
  294. dev_err(&pdev->dev, "missing resource\n");
  295. goto err1;
  296. }
  297. res = request_mem_region(res->start, resource_size(res),
  298. dev_name(&pdev->dev));
  299. if (!res) {
  300. dev_err(&pdev->dev, "can't request mem region\n");
  301. goto err1;
  302. }
  303. regs = ioremap(res->start, resource_size(res));
  304. if (!regs) {
  305. dev_err(&pdev->dev, "ioremap failed\n");
  306. goto err2;
  307. }
  308. irq = platform_get_irq(pdev, 0);
  309. if (irq < 0) {
  310. dev_err(&pdev->dev, "missing IRQ\n");
  311. goto err3;
  312. }
  313. spin_lock_init(&dwc->lock);
  314. platform_set_drvdata(pdev, dwc);
  315. dwc->regs = regs;
  316. dwc->regs_size = resource_size(res);
  317. dwc->dev = &pdev->dev;
  318. dwc->irq = irq;
  319. pm_runtime_enable(&pdev->dev);
  320. pm_runtime_get_sync(&pdev->dev);
  321. pm_runtime_forbid(&pdev->dev);
  322. ret = dwc3_core_init(dwc);
  323. if (ret) {
  324. dev_err(&pdev->dev, "failed to initialize core\n");
  325. goto err3;
  326. }
  327. if (features & DWC3_HAS_PERIPHERAL) {
  328. ret = dwc3_gadget_init(dwc);
  329. if (ret) {
  330. dev_err(&pdev->dev, "failed to initialized gadget\n");
  331. goto err4;
  332. }
  333. }
  334. ret = dwc3_debugfs_init(dwc);
  335. if (ret) {
  336. dev_err(&pdev->dev, "failed to initialize debugfs\n");
  337. goto err5;
  338. }
  339. pm_runtime_allow(&pdev->dev);
  340. return 0;
  341. err5:
  342. if (features & DWC3_HAS_PERIPHERAL)
  343. dwc3_gadget_exit(dwc);
  344. err4:
  345. dwc3_core_exit(dwc);
  346. err3:
  347. iounmap(regs);
  348. err2:
  349. release_mem_region(res->start, resource_size(res));
  350. err1:
  351. kfree(dwc->mem);
  352. err0:
  353. return ret;
  354. }
  355. static int __devexit dwc3_remove(struct platform_device *pdev)
  356. {
  357. const struct platform_device_id *id = platform_get_device_id(pdev);
  358. struct dwc3 *dwc = platform_get_drvdata(pdev);
  359. struct resource *res;
  360. unsigned int features = id->driver_data;
  361. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  362. pm_runtime_put(&pdev->dev);
  363. pm_runtime_disable(&pdev->dev);
  364. dwc3_debugfs_exit(dwc);
  365. if (features & DWC3_HAS_PERIPHERAL)
  366. dwc3_gadget_exit(dwc);
  367. dwc3_core_exit(dwc);
  368. release_mem_region(res->start, resource_size(res));
  369. iounmap(dwc->regs);
  370. kfree(dwc->mem);
  371. return 0;
  372. }
  373. static const struct platform_device_id dwc3_id_table[] __devinitconst = {
  374. {
  375. .name = "dwc3-omap",
  376. .driver_data = (DWC3_HAS_PERIPHERAL
  377. | DWC3_HAS_XHCI
  378. | DWC3_HAS_OTG),
  379. },
  380. {
  381. .name = "dwc3-pci",
  382. .driver_data = DWC3_HAS_PERIPHERAL,
  383. },
  384. { }, /* Terminating Entry */
  385. };
  386. MODULE_DEVICE_TABLE(platform, dwc3_id_table);
  387. static struct platform_driver dwc3_driver = {
  388. .probe = dwc3_probe,
  389. .remove = __devexit_p(dwc3_remove),
  390. .driver = {
  391. .name = "dwc3",
  392. },
  393. .id_table = dwc3_id_table,
  394. };
  395. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  396. MODULE_LICENSE("Dual BSD/GPL");
  397. MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");
  398. static int __devinit dwc3_init(void)
  399. {
  400. return platform_driver_register(&dwc3_driver);
  401. }
  402. module_init(dwc3_init);
  403. static void __exit dwc3_exit(void)
  404. {
  405. platform_driver_unregister(&dwc3_driver);
  406. }
  407. module_exit(dwc3_exit);