dwc3-omap.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. u32 ctrl;
  119. spin_lock(&omap->lock);
  120. reg = dwc3_readl(omap->base, USBOTGSS_IRQSTATUS_1);
  121. ctrl = dwc3_readl(omap->base, USBOTGSS_UTMI_OTG_CTRL);
  122. if (reg & USBOTGSS_IRQ1_DMADISABLECLR) {
  123. dev_dbg(omap->dev, "DMA Disable was Cleared\n");
  124. omap->dma_status = false;
  125. }
  126. if (reg & USBOTGSS_IRQ1_OEVT)
  127. dev_dbg(omap->dev, "OTG Event\n");
  128. if (reg & USBOTGSS_IRQ1_DRVVBUS_RISE) {
  129. dev_dbg(omap->dev, "DRVVBUS Rise\n");
  130. ctrl |= USBOTGSS_UTMI_OTG_CTRL_DRVVBUS;
  131. }
  132. if (reg & USBOTGSS_IRQ1_CHRGVBUS_RISE) {
  133. dev_dbg(omap->dev, "CHRGVBUS Rise\n");
  134. ctrl |= USBOTGSS_UTMI_OTG_CTRL_CHRGVBUS;
  135. }
  136. if (reg & USBOTGSS_IRQ1_DISCHRGVBUS_RISE) {
  137. dev_dbg(omap->dev, "DISCHRGVBUS Rise\n");
  138. ctrl |= USBOTGSS_UTMI_OTG_CTRL_DISCHRGVBUS;
  139. }
  140. if (reg & USBOTGSS_IRQ1_IDPULLUP_RISE) {
  141. dev_dbg(omap->dev, "IDPULLUP Rise\n");
  142. ctrl |= USBOTGSS_UTMI_OTG_CTRL_IDPULLUP;
  143. }
  144. if (reg & USBOTGSS_IRQ1_DRVVBUS_FALL) {
  145. dev_dbg(omap->dev, "DRVVBUS Fall\n");
  146. ctrl &= ~USBOTGSS_UTMI_OTG_CTRL_DRVVBUS;
  147. }
  148. if (reg & USBOTGSS_IRQ1_CHRGVBUS_FALL) {
  149. dev_dbg(omap->dev, "CHRGVBUS Fall\n");
  150. ctrl &= ~USBOTGSS_UTMI_OTG_CTRL_CHRGVBUS;
  151. }
  152. if (reg & USBOTGSS_IRQ1_DISCHRGVBUS_FALL) {
  153. dev_dbg(omap->dev, "DISCHRGVBUS Fall\n");
  154. ctrl &= ~USBOTGSS_UTMI_OTG_CTRL_DISCHRGVBUS;
  155. }
  156. if (reg & USBOTGSS_IRQ1_IDPULLUP_FALL) {
  157. dev_dbg(omap->dev, "IDPULLUP Fall\n");
  158. ctrl &= ~USBOTGSS_UTMI_OTG_CTRL_IDPULLUP;
  159. }
  160. dwc3_writel(omap->base, USBOTGSS_UTMI_OTG_CTRL, ctrl);
  161. spin_unlock(&omap->lock);
  162. return IRQ_HANDLED;
  163. }
  164. static int __devinit dwc3_omap_probe(struct platform_device *pdev)
  165. {
  166. struct dwc3_omap_data *pdata = pdev->dev.platform_data;
  167. struct platform_device *dwc3;
  168. struct dwc3_omap *omap;
  169. struct resource *res;
  170. int ret = -ENOMEM;
  171. int irq;
  172. u32 reg;
  173. void __iomem *base;
  174. void *context;
  175. omap = kzalloc(sizeof(*omap), GFP_KERNEL);
  176. if (!omap) {
  177. dev_err(&pdev->dev, "not enough memory\n");
  178. goto err0;
  179. }
  180. platform_set_drvdata(pdev, omap);
  181. irq = platform_get_irq(pdev, 1);
  182. if (irq < 0) {
  183. dev_err(&pdev->dev, "missing IRQ resource\n");
  184. ret = -EINVAL;
  185. goto err1;
  186. }
  187. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  188. if (!res) {
  189. dev_err(&pdev->dev, "missing memory base resource\n");
  190. ret = -EINVAL;
  191. goto err1;
  192. }
  193. base = ioremap_nocache(res->start, resource_size(res));
  194. if (!base) {
  195. dev_err(&pdev->dev, "ioremap failed\n");
  196. goto err1;
  197. }
  198. dwc3 = platform_device_alloc("dwc3-omap", -1);
  199. if (!dwc3) {
  200. dev_err(&pdev->dev, "couldn't allocate dwc3 device\n");
  201. goto err2;
  202. }
  203. context = kzalloc(resource_size(res), GFP_KERNEL);
  204. if (!context) {
  205. dev_err(&pdev->dev, "couldn't allocate dwc3 context memory\n");
  206. goto err3;
  207. }
  208. spin_lock_init(&omap->lock);
  209. dma_set_coherent_mask(&dwc3->dev, pdev->dev.coherent_dma_mask);
  210. dwc3->dev.parent = &pdev->dev;
  211. dwc3->dev.dma_mask = pdev->dev.dma_mask;
  212. dwc3->dev.dma_parms = pdev->dev.dma_parms;
  213. omap->resource_size = resource_size(res);
  214. omap->context = context;
  215. omap->dev = &pdev->dev;
  216. omap->irq = irq;
  217. omap->base = base;
  218. omap->dwc3 = dwc3;
  219. reg = dwc3_readl(omap->base, USBOTGSS_UTMI_OTG_STATUS);
  220. if (!pdata) {
  221. dev_dbg(&pdev->dev, "missing platform data\n");
  222. } else {
  223. switch (pdata->utmi_mode) {
  224. case DWC3_OMAP_UTMI_MODE_SW:
  225. reg |= USBOTGSS_UTMI_OTG_STATUS_SW_MODE;
  226. break;
  227. case DWC3_OMAP_UTMI_MODE_HW:
  228. reg &= ~USBOTGSS_UTMI_OTG_STATUS_SW_MODE;
  229. break;
  230. default:
  231. dev_dbg(&pdev->dev, "UNKNOWN utmi mode %d\n",
  232. pdata->utmi_mode);
  233. }
  234. }
  235. dwc3_writel(omap->base, USBOTGSS_UTMI_OTG_STATUS, reg);
  236. /* check the DMA Status */
  237. reg = dwc3_readl(omap->base, USBOTGSS_SYSCONFIG);
  238. omap->dma_status = !!(reg & USBOTGSS_SYSCONFIG_DMADISABLE);
  239. ret = request_irq(omap->irq, dwc3_omap_interrupt, 0,
  240. "dwc3-omap", omap);
  241. if (ret) {
  242. dev_err(&pdev->dev, "failed to request IRQ #%d --> %d\n",
  243. omap->irq, ret);
  244. goto err4;
  245. }
  246. /* enable all IRQs */
  247. reg = USBOTGSS_IRQO_COREIRQ_ST;
  248. dwc3_writel(omap->base, USBOTGSS_IRQENABLE_SET_0, reg);
  249. reg = (USBOTGSS_IRQ1_OEVT |
  250. USBOTGSS_IRQ1_DRVVBUS_RISE |
  251. USBOTGSS_IRQ1_CHRGVBUS_RISE |
  252. USBOTGSS_IRQ1_DISCHRGVBUS_RISE |
  253. USBOTGSS_IRQ1_IDPULLUP_RISE |
  254. USBOTGSS_IRQ1_DRVVBUS_FALL |
  255. USBOTGSS_IRQ1_CHRGVBUS_FALL |
  256. USBOTGSS_IRQ1_DISCHRGVBUS_FALL |
  257. USBOTGSS_IRQ1_IDPULLUP_FALL);
  258. dwc3_writel(omap->base, USBOTGSS_IRQENABLE_SET_1, reg);
  259. ret = platform_device_add_resources(dwc3, pdev->resource,
  260. pdev->num_resources);
  261. if (ret) {
  262. dev_err(&pdev->dev, "couldn't add resources to dwc3 device\n");
  263. goto err5;
  264. }
  265. ret = platform_device_add(dwc3);
  266. if (ret) {
  267. dev_err(&pdev->dev, "failed to register dwc3 device\n");
  268. goto err5;
  269. }
  270. return 0;
  271. err5:
  272. free_irq(omap->irq, omap);
  273. err4:
  274. kfree(omap->context);
  275. err3:
  276. platform_device_put(dwc3);
  277. err2:
  278. iounmap(base);
  279. err1:
  280. kfree(omap);
  281. err0:
  282. return ret;
  283. }
  284. static int __devexit dwc3_omap_remove(struct platform_device *pdev)
  285. {
  286. struct dwc3_omap *omap = platform_get_drvdata(pdev);
  287. platform_device_unregister(omap->dwc3);
  288. free_irq(omap->irq, omap);
  289. iounmap(omap->base);
  290. kfree(omap->context);
  291. kfree(omap);
  292. return 0;
  293. }
  294. static const struct of_device_id of_dwc3_matach[] = {
  295. {
  296. "ti,dwc3",
  297. },
  298. { },
  299. };
  300. MODULE_DEVICE_TABLE(of, of_dwc3_matach);
  301. static struct platform_driver dwc3_omap_driver = {
  302. .probe = dwc3_omap_probe,
  303. .remove = __devexit_p(dwc3_omap_remove),
  304. .driver = {
  305. .name = "omap-dwc3",
  306. .of_match_table = of_dwc3_matach,
  307. },
  308. };
  309. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  310. MODULE_LICENSE("Dual BSD/GPL");
  311. MODULE_DESCRIPTION("DesignWare USB3 OMAP Glue Layer");
  312. static int __devinit dwc3_omap_init(void)
  313. {
  314. return platform_driver_register(&dwc3_omap_driver);
  315. }
  316. module_init(dwc3_omap_init);
  317. static void __exit dwc3_omap_exit(void)
  318. {
  319. platform_driver_unregister(&dwc3_omap_driver);
  320. }
  321. module_exit(dwc3_omap_exit);