core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /**
  2. * core.c - DesignWare USB3 DRD Controller Core file
  3. *
  4. * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
  5. * All rights reserved.
  6. *
  7. * Authors: Felipe Balbi <balbi@ti.com>,
  8. * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. The names of the above-listed copyright holders may not be used
  20. * to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * ALTERNATIVELY, this software may be distributed under the terms of the
  24. * GNU General Public License ("GPL") version 2, as published by the Free
  25. * Software Foundation.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  31. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. */
  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 "core.h"
  53. #include "gadget.h"
  54. #include "io.h"
  55. #include "debug.h"
  56. /**
  57. * dwc3_core_soft_reset - Issues core soft reset and PHY reset
  58. * @dwc: pointer to our context structure
  59. */
  60. static void dwc3_core_soft_reset(struct dwc3 *dwc)
  61. {
  62. u32 reg;
  63. /* Before Resetting PHY, put Core in Reset */
  64. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  65. reg |= DWC3_GCTL_CORESOFTRESET;
  66. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  67. /* Assert USB3 PHY reset */
  68. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  69. reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
  70. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  71. /* Assert USB2 PHY reset */
  72. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  73. reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
  74. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  75. mdelay(100);
  76. /* Clear USB3 PHY reset */
  77. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  78. reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
  79. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  80. /* Clear USB2 PHY reset */
  81. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  82. reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
  83. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  84. /* After PHYs are stable we can take Core out of reset state */
  85. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  86. reg &= ~DWC3_GCTL_CORESOFTRESET;
  87. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  88. }
  89. /**
  90. * dwc3_free_one_event_buffer - Frees one event buffer
  91. * @dwc: Pointer to our controller context structure
  92. * @evt: Pointer to event buffer to be freed
  93. */
  94. static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
  95. struct dwc3_event_buffer *evt)
  96. {
  97. dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
  98. kfree(evt);
  99. }
  100. /**
  101. * dwc3_alloc_one_event_buffer - Allocated one event buffer structure
  102. * @dwc: Pointer to our controller context structure
  103. * @length: size of the event buffer
  104. *
  105. * Returns a pointer to the allocated event buffer structure on succes
  106. * otherwise ERR_PTR(errno).
  107. */
  108. static struct dwc3_event_buffer *__devinit
  109. dwc3_alloc_one_event_buffer(struct dwc3 *dwc, unsigned length)
  110. {
  111. struct dwc3_event_buffer *evt;
  112. evt = kzalloc(sizeof(*evt), GFP_KERNEL);
  113. if (!evt)
  114. return ERR_PTR(-ENOMEM);
  115. evt->dwc = dwc;
  116. evt->length = length;
  117. evt->buf = dma_alloc_coherent(dwc->dev, length,
  118. &evt->dma, GFP_KERNEL);
  119. if (!evt->buf) {
  120. kfree(evt);
  121. return ERR_PTR(-ENOMEM);
  122. }
  123. return evt;
  124. }
  125. /**
  126. * dwc3_free_event_buffers - frees all allocated event buffers
  127. * @dwc: Pointer to our controller context structure
  128. */
  129. static void dwc3_free_event_buffers(struct dwc3 *dwc)
  130. {
  131. struct dwc3_event_buffer *evt;
  132. int i;
  133. for (i = 0; i < DWC3_EVENT_BUFFERS_NUM; i++) {
  134. evt = dwc->ev_buffs[i];
  135. if (evt) {
  136. dwc3_free_one_event_buffer(dwc, evt);
  137. dwc->ev_buffs[i] = NULL;
  138. }
  139. }
  140. }
  141. /**
  142. * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
  143. * @dwc: Pointer to out controller context structure
  144. * @num: number of event buffers to allocate
  145. * @length: size of event buffer
  146. *
  147. * Returns 0 on success otherwise negative errno. In error the case, dwc
  148. * may contain some buffers allocated but not all which were requested.
  149. */
  150. static int __devinit dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned num,
  151. unsigned length)
  152. {
  153. int i;
  154. for (i = 0; i < num; i++) {
  155. struct dwc3_event_buffer *evt;
  156. evt = dwc3_alloc_one_event_buffer(dwc, length);
  157. if (IS_ERR(evt)) {
  158. dev_err(dwc->dev, "can't allocate event buffer\n");
  159. return PTR_ERR(evt);
  160. }
  161. dwc->ev_buffs[i] = evt;
  162. }
  163. return 0;
  164. }
  165. /**
  166. * dwc3_event_buffers_setup - setup our allocated event buffers
  167. * @dwc: Pointer to out controller context structure
  168. *
  169. * Returns 0 on success otherwise negative errno.
  170. */
  171. static int __devinit dwc3_event_buffers_setup(struct dwc3 *dwc)
  172. {
  173. struct dwc3_event_buffer *evt;
  174. int n;
  175. for (n = 0; n < DWC3_EVENT_BUFFERS_NUM; n++) {
  176. evt = dwc->ev_buffs[n];
  177. dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
  178. evt->buf, (unsigned long long) evt->dma,
  179. evt->length);
  180. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
  181. lower_32_bits(evt->dma));
  182. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
  183. upper_32_bits(evt->dma));
  184. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
  185. evt->length & 0xffff);
  186. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
  187. }
  188. return 0;
  189. }
  190. static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
  191. {
  192. struct dwc3_event_buffer *evt;
  193. int n;
  194. for (n = 0; n < DWC3_EVENT_BUFFERS_NUM; n++) {
  195. evt = dwc->ev_buffs[n];
  196. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
  197. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
  198. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0);
  199. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
  200. }
  201. }
  202. /**
  203. * dwc3_core_init - Low-level initialization of DWC3 Core
  204. * @dwc: Pointer to our controller context structure
  205. *
  206. * Returns 0 on success otherwise negative errno.
  207. */
  208. static int __devinit dwc3_core_init(struct dwc3 *dwc)
  209. {
  210. unsigned long timeout;
  211. u32 reg;
  212. int ret;
  213. reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
  214. /* This should read as U3 followed by revision number */
  215. if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
  216. dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
  217. ret = -ENODEV;
  218. goto err0;
  219. }
  220. dwc->revision = reg & DWC3_GSNPSREV_MASK;
  221. dwc3_core_soft_reset(dwc);
  222. /* issue device SoftReset too */
  223. timeout = jiffies + msecs_to_jiffies(500);
  224. dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
  225. do {
  226. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  227. if (!(reg & DWC3_DCTL_CSFTRST))
  228. break;
  229. if (time_after(jiffies, timeout)) {
  230. dev_err(dwc->dev, "Reset Timed Out\n");
  231. ret = -ETIMEDOUT;
  232. goto err0;
  233. }
  234. cpu_relax();
  235. } while (true);
  236. ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_NUM,
  237. DWC3_EVENT_BUFFERS_SIZE);
  238. if (ret) {
  239. dev_err(dwc->dev, "failed to allocate event buffers\n");
  240. ret = -ENOMEM;
  241. goto err1;
  242. }
  243. ret = dwc3_event_buffers_setup(dwc);
  244. if (ret) {
  245. dev_err(dwc->dev, "failed to setup event buffers\n");
  246. goto err1;
  247. }
  248. return 0;
  249. err1:
  250. dwc3_free_event_buffers(dwc);
  251. err0:
  252. return ret;
  253. }
  254. static void dwc3_core_exit(struct dwc3 *dwc)
  255. {
  256. dwc3_event_buffers_cleanup(dwc);
  257. dwc3_free_event_buffers(dwc);
  258. }
  259. #define DWC3_ALIGN_MASK (16 - 1)
  260. static int __devinit dwc3_probe(struct platform_device *pdev)
  261. {
  262. const struct platform_device_id *id = platform_get_device_id(pdev);
  263. struct resource *res;
  264. struct dwc3 *dwc;
  265. void __iomem *regs;
  266. unsigned int features = id->driver_data;
  267. int ret = -ENOMEM;
  268. int irq;
  269. void *mem;
  270. mem = kzalloc(sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
  271. if (!mem) {
  272. dev_err(&pdev->dev, "not enough memory\n");
  273. goto err0;
  274. }
  275. dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
  276. dwc->mem = mem;
  277. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  278. if (!res) {
  279. dev_err(&pdev->dev, "missing resource\n");
  280. goto err1;
  281. }
  282. res = request_mem_region(res->start, resource_size(res),
  283. dev_name(&pdev->dev));
  284. if (!res) {
  285. dev_err(&pdev->dev, "can't request mem region\n");
  286. goto err1;
  287. }
  288. regs = ioremap(res->start, resource_size(res));
  289. if (!regs) {
  290. dev_err(&pdev->dev, "ioremap failed\n");
  291. goto err2;
  292. }
  293. irq = platform_get_irq(pdev, 0);
  294. if (irq < 0) {
  295. dev_err(&pdev->dev, "missing IRQ\n");
  296. goto err3;
  297. }
  298. spin_lock_init(&dwc->lock);
  299. platform_set_drvdata(pdev, dwc);
  300. dwc->regs = regs;
  301. dwc->regs_size = resource_size(res);
  302. dwc->dev = &pdev->dev;
  303. dwc->irq = irq;
  304. pm_runtime_enable(&pdev->dev);
  305. pm_runtime_get_sync(&pdev->dev);
  306. pm_runtime_forbid(&pdev->dev);
  307. ret = dwc3_core_init(dwc);
  308. if (ret) {
  309. dev_err(&pdev->dev, "failed to initialize core\n");
  310. goto err3;
  311. }
  312. if (features & DWC3_HAS_PERIPHERAL) {
  313. ret = dwc3_gadget_init(dwc);
  314. if (ret) {
  315. dev_err(&pdev->dev, "failed to initialized gadget\n");
  316. goto err4;
  317. }
  318. }
  319. ret = dwc3_debugfs_init(dwc);
  320. if (ret) {
  321. dev_err(&pdev->dev, "failed to initialize debugfs\n");
  322. goto err5;
  323. }
  324. pm_runtime_allow(&pdev->dev);
  325. return 0;
  326. err5:
  327. if (features & DWC3_HAS_PERIPHERAL)
  328. dwc3_gadget_exit(dwc);
  329. err4:
  330. dwc3_core_exit(dwc);
  331. err3:
  332. iounmap(regs);
  333. err2:
  334. release_mem_region(res->start, resource_size(res));
  335. err1:
  336. kfree(dwc->mem);
  337. err0:
  338. return ret;
  339. }
  340. static int __devexit dwc3_remove(struct platform_device *pdev)
  341. {
  342. const struct platform_device_id *id = platform_get_device_id(pdev);
  343. struct dwc3 *dwc = platform_get_drvdata(pdev);
  344. struct resource *res;
  345. unsigned int features = id->driver_data;
  346. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  347. pm_runtime_put(&pdev->dev);
  348. pm_runtime_disable(&pdev->dev);
  349. dwc3_debugfs_exit(dwc);
  350. if (features & DWC3_HAS_PERIPHERAL)
  351. dwc3_gadget_exit(dwc);
  352. dwc3_core_exit(dwc);
  353. release_mem_region(res->start, resource_size(res));
  354. iounmap(dwc->regs);
  355. kfree(dwc->mem);
  356. return 0;
  357. }
  358. static const struct platform_device_id dwc3_id_table[] __devinitconst = {
  359. {
  360. .name = "dwc3-omap",
  361. .driver_data = (DWC3_HAS_PERIPHERAL
  362. | DWC3_HAS_XHCI
  363. | DWC3_HAS_OTG),
  364. },
  365. {
  366. .name = "dwc3-pci",
  367. .driver_data = DWC3_HAS_PERIPHERAL,
  368. },
  369. { }, /* Terminating Entry */
  370. };
  371. MODULE_DEVICE_TABLE(platform, dwc3_id_table);
  372. static struct platform_driver dwc3_driver = {
  373. .probe = dwc3_probe,
  374. .remove = __devexit_p(dwc3_remove),
  375. .driver = {
  376. .name = "dwc3",
  377. },
  378. .id_table = dwc3_id_table,
  379. };
  380. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  381. MODULE_LICENSE("Dual BSD/GPL");
  382. MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");
  383. static int __devinit dwc3_init(void)
  384. {
  385. return platform_driver_register(&dwc3_driver);
  386. }
  387. module_init(dwc3_init);
  388. static void __exit dwc3_exit(void)
  389. {
  390. platform_driver_unregister(&dwc3_driver);
  391. }
  392. module_exit(dwc3_exit);