dwc3-omap.c 9.9 KB

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