dwc3-omap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /**
  2. * dwc3-omap.c - OMAP Specific Glue layer
  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/interrupt.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/platform_device.h>
  44. #include <linux/dma-mapping.h>
  45. #include <linux/ioport.h>
  46. #include <linux/io.h>
  47. #include "io.h"
  48. /*
  49. * All these registers belong to OMAP's Wrapper around the
  50. * DesignWare USB3 Core.
  51. */
  52. #define USBOTGSS_REVISION 0x0000
  53. #define USBOTGSS_SYSCONFIG 0x0010
  54. #define USBOTGSS_IRQ_EOI 0x0020
  55. #define USBOTGSS_IRQSTATUS_RAW_0 0x0024
  56. #define USBOTGSS_IRQSTATUS_0 0x0028
  57. #define USBOTGSS_IRQENABLE_SET_0 0x002c
  58. #define USBOTGSS_IRQENABLE_CLR_0 0x0030
  59. #define USBOTGSS_IRQSTATUS_RAW_1 0x0034
  60. #define USBOTGSS_IRQSTATUS_1 0x0038
  61. #define USBOTGSS_IRQENABLE_SET_1 0x003c
  62. #define USBOTGSS_IRQENABLE_CLR_1 0x0040
  63. #define USBOTGSS_UTMI_OTG_CTRL 0x0080
  64. #define USBOTGSS_UTMI_OTG_STATUS 0x0084
  65. #define USBOTGSS_MMRAM_OFFSET 0x0100
  66. #define USBOTGSS_FLADJ 0x0104
  67. #define USBOTGSS_DEBUG_CFG 0x0108
  68. #define USBOTGSS_DEBUG_DATA 0x010c
  69. /* SYSCONFIG REGISTER */
  70. #define USBOTGSS_SYSCONFIG_DMADISABLE (1 << 16)
  71. #define USBOTGSS_SYSCONFIG_STANDBYMODE(x) ((x) << 4)
  72. #define USBOTGSS_SYSCONFIG_IDLEMODE(x) ((x) << 2)
  73. /* IRQ_EOI REGISTER */
  74. #define USBOTGSS_IRQ_EOI_LINE_NUMBER (1 << 0)
  75. /* IRQS0 BITS */
  76. #define USBOTGSS_IRQO_COREIRQ_ST (1 << 0)
  77. /* IRQ1 BITS */
  78. #define USBOTGSS_IRQ1_DMADISABLECLR (1 << 17)
  79. #define USBOTGSS_IRQ1_OEVT (1 << 16)
  80. #define USBOTGSS_IRQ1_DRVVBUS_RISE (1 << 13)
  81. #define USBOTGSS_IRQ1_CHRGVBUS_RISE (1 << 12)
  82. #define USBOTGSS_IRQ1_DISCHRGVBUS_RISE (1 << 11)
  83. #define USBOTGSS_IRQ1_IDPULLUP_RISE (1 << 8)
  84. #define USBOTGSS_IRQ1_DRVVBUS_FALL (1 << 5)
  85. #define USBOTGSS_IRQ1_CHRGVBUS_FALL (1 << 4)
  86. #define USBOTGSS_IRQ1_DISCHRGVBUS_FALL (1 << 3)
  87. #define USBOTGSS_IRQ1_IDPULLUP_FALL (1 << 0)
  88. /* UTMI_OTG_CTRL REGISTER */
  89. #define USBOTGSS_UTMI_OTG_CTRL_DRVVBUS (1 << 5)
  90. #define USBOTGSS_UTMI_OTG_CTRL_CHRGVBUS (1 << 4)
  91. #define USBOTGSS_UTMI_OTG_CTRL_DISCHRGVBUS (1 << 3)
  92. #define USBOTGSS_UTMI_OTG_CTRL_IDPULLUP (1 << 0)
  93. /* UTMI_OTG_STATUS REGISTER */
  94. #define USBOTGSS_UTMI_OTG_STATUS_SW_MODE (1 << 31)
  95. #define USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT (1 << 9)
  96. #define USBOTGSS_UTMI_OTG_STATUS_TXBITSTUFFENABLE (1 << 8)
  97. #define USBOTGSS_UTMI_OTG_STATUS_IDDIG (1 << 4)
  98. #define USBOTGSS_UTMI_OTG_STATUS_SESSEND (1 << 3)
  99. #define USBOTGSS_UTMI_OTG_STATUS_SESSVALID (1 << 2)
  100. #define USBOTGSS_UTMI_OTG_STATUS_VBUSVALID (1 << 1)
  101. struct dwc3_omap {
  102. /* device lock */
  103. spinlock_t lock;
  104. struct platform_device *dwc3;
  105. struct device *dev;
  106. int irq;
  107. void __iomem *base;
  108. void *context;
  109. u32 resource_size;
  110. u32 dma_status:1;
  111. };
  112. #ifdef CONFIG_PM
  113. static int dwc3_omap_suspend(struct device *dev)
  114. {
  115. struct dwc3_omap *omap = dev_get_drvdata(dev);
  116. memcpy_fromio(omap->context, omap->base, omap->resource_size);
  117. return 0;
  118. }
  119. static int dwc3_omap_resume(struct device *dev)
  120. {
  121. struct dwc3_omap *omap = dev_get_drvdata(dev);
  122. memcpy_toio(omap->base, omap->context, omap->resource_size);
  123. return 0;
  124. }
  125. static int dwc3_omap_idle(struct device *dev)
  126. {
  127. struct dwc3_omap *omap = dev_get_drvdata(dev);
  128. u32 reg;
  129. /* stop DMA Engine */
  130. reg = dwc3_readl(omap->base, USBOTGSS_SYSCONFIG);
  131. reg &= ~(USBOTGSS_SYSCONFIG_DMADISABLE);
  132. dwc3_writel(omap->base, USBOTGSS_SYSCONFIG, reg);
  133. return 0;
  134. }
  135. static UNIVERSAL_DEV_PM_OPS(dwc3_omap_pm_ops, dwc3_omap_suspend,
  136. dwc3_omap_resume, dwc3_omap_idle);
  137. #define DEV_PM_OPS (&dwc3_omap_pm_ops)
  138. #else
  139. #define DEV_PM_OPS NULL
  140. #endif
  141. static irqreturn_t dwc3_omap_interrupt(int irq, void *_omap)
  142. {
  143. struct dwc3_omap *omap = _omap;
  144. u32 reg;
  145. u32 ctrl;
  146. spin_lock(&omap->lock);
  147. reg = dwc3_readl(omap->base, USBOTGSS_IRQSTATUS_1);
  148. ctrl = dwc3_readl(omap->base, USBOTGSS_UTMI_OTG_CTRL);
  149. if (reg & USBOTGSS_IRQ1_DMADISABLECLR) {
  150. dev_dbg(omap->dev, "DMA Disable was Cleared\n");
  151. omap->dma_status = false;
  152. }
  153. if (reg & USBOTGSS_IRQ1_OEVT)
  154. dev_dbg(omap->dev, "OTG Event\n");
  155. if (reg & USBOTGSS_IRQ1_DRVVBUS_RISE) {
  156. dev_dbg(omap->dev, "DRVVBUS Rise\n");
  157. ctrl |= USBOTGSS_UTMI_OTG_CTRL_DRVVBUS;
  158. }
  159. if (reg & USBOTGSS_IRQ1_CHRGVBUS_RISE) {
  160. dev_dbg(omap->dev, "CHRGVBUS Rise\n");
  161. ctrl |= USBOTGSS_UTMI_OTG_CTRL_CHRGVBUS;
  162. }
  163. if (reg & USBOTGSS_IRQ1_DISCHRGVBUS_RISE) {
  164. dev_dbg(omap->dev, "DISCHRGVBUS Rise\n");
  165. ctrl |= USBOTGSS_UTMI_OTG_CTRL_DISCHRGVBUS;
  166. }
  167. if (reg & USBOTGSS_IRQ1_IDPULLUP_RISE) {
  168. dev_dbg(omap->dev, "IDPULLUP Rise\n");
  169. ctrl |= USBOTGSS_UTMI_OTG_CTRL_IDPULLUP;
  170. }
  171. if (reg & USBOTGSS_IRQ1_DRVVBUS_FALL) {
  172. dev_dbg(omap->dev, "DRVVBUS Fall\n");
  173. ctrl &= ~USBOTGSS_UTMI_OTG_CTRL_DRVVBUS;
  174. }
  175. if (reg & USBOTGSS_IRQ1_CHRGVBUS_FALL) {
  176. dev_dbg(omap->dev, "CHRGVBUS Fall\n");
  177. ctrl &= ~USBOTGSS_UTMI_OTG_CTRL_CHRGVBUS;
  178. }
  179. if (reg & USBOTGSS_IRQ1_DISCHRGVBUS_FALL) {
  180. dev_dbg(omap->dev, "DISCHRGVBUS Fall\n");
  181. ctrl &= ~USBOTGSS_UTMI_OTG_CTRL_DISCHRGVBUS;
  182. }
  183. if (reg & USBOTGSS_IRQ1_IDPULLUP_FALL) {
  184. dev_dbg(omap->dev, "IDPULLUP Fall\n");
  185. ctrl &= ~USBOTGSS_UTMI_OTG_CTRL_IDPULLUP;
  186. }
  187. dwc3_writel(omap->base, USBOTGSS_UTMI_OTG_CTRL, ctrl);
  188. spin_unlock(&omap->lock);
  189. return IRQ_HANDLED;
  190. }
  191. static int __devinit dwc3_omap_probe(struct platform_device *pdev)
  192. {
  193. struct platform_device *dwc3;
  194. struct dwc3_omap *omap;
  195. struct resource *res;
  196. int ret = -ENOMEM;
  197. int irq;
  198. u32 reg;
  199. void __iomem *base;
  200. void *context;
  201. omap = kzalloc(sizeof(*omap), GFP_KERNEL);
  202. if (!omap) {
  203. dev_err(&pdev->dev, "not enough memory\n");
  204. goto err0;
  205. }
  206. platform_set_drvdata(pdev, omap);
  207. irq = platform_get_irq(pdev, 1);
  208. if (irq < 0) {
  209. dev_err(&pdev->dev, "missing IRQ resource\n");
  210. ret = -EINVAL;
  211. goto err1;
  212. }
  213. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  214. if (!res) {
  215. dev_err(&pdev->dev, "missing memory base resource\n");
  216. ret = -EINVAL;
  217. goto err1;
  218. }
  219. base = ioremap_nocache(res->start, resource_size(res));
  220. if (!base) {
  221. dev_err(&pdev->dev, "ioremap failed\n");
  222. goto err1;
  223. }
  224. dwc3 = platform_device_alloc("dwc3-omap", -1);
  225. if (!dwc3) {
  226. dev_err(&pdev->dev, "couldn't allocate dwc3 device\n");
  227. goto err2;
  228. }
  229. context = kzalloc(resource_size(res), GFP_KERNEL);
  230. if (!context) {
  231. dev_err(&pdev->dev, "couldn't allocate dwc3 context memory\n");
  232. goto err3;
  233. }
  234. spin_lock_init(&omap->lock);
  235. dma_set_coherent_mask(&dwc3->dev, pdev->dev.coherent_dma_mask);
  236. dwc3->dev.parent = &pdev->dev;
  237. dwc3->dev.dma_mask = pdev->dev.dma_mask;
  238. dwc3->dev.dma_parms = pdev->dev.dma_parms;
  239. omap->resource_size = resource_size(res);
  240. omap->context = context;
  241. omap->dev = &pdev->dev;
  242. omap->irq = irq;
  243. omap->base = base;
  244. omap->dwc3 = dwc3;
  245. /* check the DMA Status */
  246. reg = dwc3_readl(omap->base, USBOTGSS_SYSCONFIG);
  247. omap->dma_status = !!(reg & USBOTGSS_SYSCONFIG_DMADISABLE);
  248. ret = request_irq(omap->irq, dwc3_omap_interrupt, 0,
  249. "dwc3-wrapper", omap);
  250. if (ret) {
  251. dev_err(&pdev->dev, "failed to request IRQ #%d --> %d\n",
  252. omap->irq, ret);
  253. goto err4;
  254. }
  255. /* enable all IRQs */
  256. reg = USBOTGSS_IRQO_COREIRQ_ST;
  257. dwc3_writel(omap->base, USBOTGSS_IRQENABLE_SET_0, reg);
  258. reg = (USBOTGSS_IRQ1_OEVT |
  259. USBOTGSS_IRQ1_DRVVBUS_RISE |
  260. USBOTGSS_IRQ1_CHRGVBUS_RISE |
  261. USBOTGSS_IRQ1_DISCHRGVBUS_RISE |
  262. USBOTGSS_IRQ1_IDPULLUP_RISE |
  263. USBOTGSS_IRQ1_DRVVBUS_FALL |
  264. USBOTGSS_IRQ1_CHRGVBUS_FALL |
  265. USBOTGSS_IRQ1_DISCHRGVBUS_FALL |
  266. USBOTGSS_IRQ1_IDPULLUP_FALL);
  267. dwc3_writel(omap->base, USBOTGSS_IRQENABLE_SET_1, reg);
  268. ret = platform_device_add_resources(dwc3, pdev->resource,
  269. pdev->num_resources);
  270. if (ret) {
  271. dev_err(&pdev->dev, "couldn't add resources to dwc3 device\n");
  272. goto err5;
  273. }
  274. ret = platform_device_add(dwc3);
  275. if (ret) {
  276. dev_err(&pdev->dev, "failed to register dwc3 device\n");
  277. goto err5;
  278. }
  279. return 0;
  280. err5:
  281. free_irq(omap->irq, omap);
  282. err4:
  283. kfree(omap->context);
  284. err3:
  285. platform_device_put(dwc3);
  286. err2:
  287. iounmap(base);
  288. err1:
  289. kfree(omap);
  290. err0:
  291. return ret;
  292. }
  293. static int __devexit dwc3_omap_remove(struct platform_device *pdev)
  294. {
  295. struct dwc3_omap *omap = platform_get_drvdata(pdev);
  296. platform_device_unregister(omap->dwc3);
  297. free_irq(omap->irq, omap);
  298. iounmap(omap->base);
  299. kfree(omap->context);
  300. kfree(omap);
  301. return 0;
  302. }
  303. static const struct of_device_id of_dwc3_matach[] = {
  304. {
  305. "ti,dwc3",
  306. },
  307. { },
  308. };
  309. MODULE_DEVICE_TABLE(of, of_dwc3_matach);
  310. static struct platform_driver dwc3_omap_driver = {
  311. .probe = dwc3_omap_probe,
  312. .remove = __devexit_p(dwc3_omap_remove),
  313. .driver = {
  314. .name = "omap-dwc3",
  315. .pm = DEV_PM_OPS,
  316. .of_match_table = of_dwc3_matach,
  317. },
  318. };
  319. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  320. MODULE_LICENSE("Dual BSD/GPL");
  321. MODULE_DESCRIPTION("DesignWare USB3 OMAP Glue Layer");
  322. static int __devinit dwc3_omap_init(void)
  323. {
  324. return platform_driver_register(&dwc3_omap_driver);
  325. }
  326. module_init(dwc3_omap_init);
  327. static void __exit dwc3_omap_exit(void)
  328. {
  329. platform_driver_unregister(&dwc3_omap_driver);
  330. }
  331. module_exit(dwc3_omap_exit);