core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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/module.h>
  40. #include <linux/kernel.h>
  41. #include <linux/slab.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/platform_device.h>
  44. #include <linux/pm_runtime.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/ioport.h>
  47. #include <linux/io.h>
  48. #include <linux/list.h>
  49. #include <linux/delay.h>
  50. #include <linux/dma-mapping.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. /**
  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. /**
  204. * dwc3_core_init - Low-level initialization of DWC3 Core
  205. * @dwc: Pointer to our controller context structure
  206. *
  207. * Returns 0 on success otherwise negative errno.
  208. */
  209. static int __devinit dwc3_core_init(struct dwc3 *dwc)
  210. {
  211. unsigned long timeout;
  212. u32 reg;
  213. int ret;
  214. reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
  215. /* This should read as U3 followed by revision number */
  216. if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
  217. dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
  218. ret = -ENODEV;
  219. goto err0;
  220. }
  221. dwc->revision = reg & DWC3_GSNPSREV_MASK;
  222. dwc3_core_soft_reset(dwc);
  223. /* issue device SoftReset too */
  224. timeout = jiffies + msecs_to_jiffies(500);
  225. dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
  226. do {
  227. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  228. if (!(reg & DWC3_DCTL_CSFTRST))
  229. break;
  230. if (time_after(jiffies, timeout)) {
  231. dev_err(dwc->dev, "Reset Timed Out\n");
  232. ret = -ETIMEDOUT;
  233. goto err0;
  234. }
  235. cpu_relax();
  236. } while (true);
  237. ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_NUM,
  238. DWC3_EVENT_BUFFERS_SIZE);
  239. if (ret) {
  240. dev_err(dwc->dev, "failed to allocate event buffers\n");
  241. ret = -ENOMEM;
  242. goto err1;
  243. }
  244. ret = dwc3_event_buffers_setup(dwc);
  245. if (ret) {
  246. dev_err(dwc->dev, "failed to setup event buffers\n");
  247. goto err1;
  248. }
  249. return 0;
  250. err1:
  251. dwc3_free_event_buffers(dwc);
  252. err0:
  253. return ret;
  254. }
  255. static void dwc3_core_exit(struct dwc3 *dwc)
  256. {
  257. dwc3_event_buffers_cleanup(dwc);
  258. dwc3_free_event_buffers(dwc);
  259. }
  260. #define DWC3_ALIGN_MASK (16 - 1)
  261. static int __devinit dwc3_probe(struct platform_device *pdev)
  262. {
  263. const struct platform_device_id *id = platform_get_device_id(pdev);
  264. struct resource *res;
  265. struct dwc3 *dwc;
  266. void __iomem *regs;
  267. unsigned int features = id->driver_data;
  268. int ret = -ENOMEM;
  269. int irq;
  270. void *mem;
  271. mem = kzalloc(sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
  272. if (!mem) {
  273. dev_err(&pdev->dev, "not enough memory\n");
  274. goto err0;
  275. }
  276. dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
  277. dwc->mem = mem;
  278. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  279. if (!res) {
  280. dev_err(&pdev->dev, "missing resource\n");
  281. goto err1;
  282. }
  283. res = request_mem_region(res->start, resource_size(res),
  284. dev_name(&pdev->dev));
  285. if (!res) {
  286. dev_err(&pdev->dev, "can't request mem region\n");
  287. goto err1;
  288. }
  289. regs = ioremap(res->start, resource_size(res));
  290. if (!regs) {
  291. dev_err(&pdev->dev, "ioremap failed\n");
  292. goto err2;
  293. }
  294. irq = platform_get_irq(pdev, 0);
  295. if (irq < 0) {
  296. dev_err(&pdev->dev, "missing IRQ\n");
  297. goto err3;
  298. }
  299. spin_lock_init(&dwc->lock);
  300. platform_set_drvdata(pdev, dwc);
  301. dwc->regs = regs;
  302. dwc->regs_size = resource_size(res);
  303. dwc->dev = &pdev->dev;
  304. dwc->irq = irq;
  305. pm_runtime_enable(&pdev->dev);
  306. pm_runtime_get_sync(&pdev->dev);
  307. pm_runtime_forbid(&pdev->dev);
  308. ret = dwc3_core_init(dwc);
  309. if (ret) {
  310. dev_err(&pdev->dev, "failed to initialize core\n");
  311. goto err3;
  312. }
  313. if (features & DWC3_HAS_PERIPHERAL) {
  314. ret = dwc3_gadget_init(dwc);
  315. if (ret) {
  316. dev_err(&pdev->dev, "failed to initialized gadget\n");
  317. goto err4;
  318. }
  319. }
  320. ret = dwc3_debugfs_init(dwc);
  321. if (ret) {
  322. dev_err(&pdev->dev, "failed to initialize debugfs\n");
  323. goto err5;
  324. }
  325. pm_runtime_allow(&pdev->dev);
  326. return 0;
  327. err5:
  328. if (features & DWC3_HAS_PERIPHERAL)
  329. dwc3_gadget_exit(dwc);
  330. err4:
  331. dwc3_core_exit(dwc);
  332. err3:
  333. iounmap(regs);
  334. err2:
  335. release_mem_region(res->start, resource_size(res));
  336. err1:
  337. kfree(dwc->mem);
  338. err0:
  339. return ret;
  340. }
  341. static int __devexit dwc3_remove(struct platform_device *pdev)
  342. {
  343. const struct platform_device_id *id = platform_get_device_id(pdev);
  344. struct dwc3 *dwc = platform_get_drvdata(pdev);
  345. struct resource *res;
  346. unsigned int features = id->driver_data;
  347. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  348. pm_runtime_put(&pdev->dev);
  349. pm_runtime_disable(&pdev->dev);
  350. dwc3_debugfs_exit(dwc);
  351. if (features & DWC3_HAS_PERIPHERAL)
  352. dwc3_gadget_exit(dwc);
  353. dwc3_core_exit(dwc);
  354. release_mem_region(res->start, resource_size(res));
  355. iounmap(dwc->regs);
  356. kfree(dwc->mem);
  357. return 0;
  358. }
  359. static const struct platform_device_id dwc3_id_table[] __devinitconst = {
  360. {
  361. .name = "dwc3-omap",
  362. .driver_data = (DWC3_HAS_PERIPHERAL
  363. | DWC3_HAS_XHCI
  364. | DWC3_HAS_OTG),
  365. },
  366. {
  367. .name = "dwc3-pci",
  368. .driver_data = DWC3_HAS_PERIPHERAL,
  369. },
  370. { }, /* Terminating Entry */
  371. };
  372. MODULE_DEVICE_TABLE(platform, dwc3_id_table);
  373. static struct platform_driver dwc3_driver = {
  374. .probe = dwc3_probe,
  375. .remove = __devexit_p(dwc3_remove),
  376. .driver = {
  377. .name = "dwc3",
  378. },
  379. .id_table = dwc3_id_table,
  380. };
  381. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  382. MODULE_LICENSE("Dual BSD/GPL");
  383. MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");
  384. static int __devinit dwc3_init(void)
  385. {
  386. return platform_driver_register(&dwc3_driver);
  387. }
  388. module_init(dwc3_init);
  389. static void __exit dwc3_exit(void)
  390. {
  391. platform_driver_unregister(&dwc3_driver);
  392. }
  393. module_exit(dwc3_exit);