ux500.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * Copyright (C) 2010 ST-Ericsson AB
  3. * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
  4. *
  5. * Based on omap2430.c
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/clk.h>
  24. #include <linux/err.h>
  25. #include <linux/io.h>
  26. #include <linux/of.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/usb/musb-ux500.h>
  29. #include "musb_core.h"
  30. static struct musb_hdrc_config ux500_musb_hdrc_config = {
  31. .multipoint = true,
  32. .dyn_fifo = true,
  33. .num_eps = 16,
  34. .ram_bits = 16,
  35. };
  36. struct ux500_glue {
  37. struct device *dev;
  38. struct platform_device *musb;
  39. struct clk *clk;
  40. };
  41. #define glue_to_musb(g) platform_get_drvdata(g->musb)
  42. static void ux500_musb_set_vbus(struct musb *musb, int is_on)
  43. {
  44. u8 devctl;
  45. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  46. /* HDRC controls CPEN, but beware current surges during device
  47. * connect. They can trigger transient overcurrent conditions
  48. * that must be ignored.
  49. */
  50. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  51. if (is_on) {
  52. if (musb->xceiv->otg->state == OTG_STATE_A_IDLE) {
  53. /* start the session */
  54. devctl |= MUSB_DEVCTL_SESSION;
  55. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  56. /*
  57. * Wait for the musb to set as A device to enable the
  58. * VBUS
  59. */
  60. while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) {
  61. if (time_after(jiffies, timeout)) {
  62. dev_err(musb->controller,
  63. "configured as A device timeout");
  64. break;
  65. }
  66. }
  67. } else {
  68. musb->is_active = 1;
  69. musb->xceiv->otg->default_a = 1;
  70. musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
  71. devctl |= MUSB_DEVCTL_SESSION;
  72. MUSB_HST_MODE(musb);
  73. }
  74. } else {
  75. musb->is_active = 0;
  76. /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and jumping
  77. * right to B_IDLE...
  78. */
  79. musb->xceiv->otg->default_a = 0;
  80. devctl &= ~MUSB_DEVCTL_SESSION;
  81. MUSB_DEV_MODE(musb);
  82. }
  83. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  84. /*
  85. * Devctl values will be updated after vbus goes below
  86. * session_valid. The time taken depends on the capacitance
  87. * on VBUS line. The max discharge time can be upto 1 sec
  88. * as per the spec. Typically on our platform, it is 200ms
  89. */
  90. if (!is_on)
  91. mdelay(200);
  92. dev_dbg(musb->controller, "VBUS %s, devctl %02x\n",
  93. usb_otg_state_string(musb->xceiv->otg->state),
  94. musb_readb(musb->mregs, MUSB_DEVCTL));
  95. }
  96. static int musb_otg_notifications(struct notifier_block *nb,
  97. unsigned long event, void *unused)
  98. {
  99. struct musb *musb = container_of(nb, struct musb, nb);
  100. dev_dbg(musb->controller, "musb_otg_notifications %ld %s\n",
  101. event, usb_otg_state_string(musb->xceiv->otg->state));
  102. switch (event) {
  103. case UX500_MUSB_ID:
  104. dev_dbg(musb->controller, "ID GND\n");
  105. ux500_musb_set_vbus(musb, 1);
  106. break;
  107. case UX500_MUSB_VBUS:
  108. dev_dbg(musb->controller, "VBUS Connect\n");
  109. break;
  110. case UX500_MUSB_NONE:
  111. dev_dbg(musb->controller, "VBUS Disconnect\n");
  112. if (is_host_active(musb))
  113. ux500_musb_set_vbus(musb, 0);
  114. else
  115. musb->xceiv->otg->state = OTG_STATE_B_IDLE;
  116. break;
  117. default:
  118. dev_dbg(musb->controller, "ID float\n");
  119. return NOTIFY_DONE;
  120. }
  121. return NOTIFY_OK;
  122. }
  123. static irqreturn_t ux500_musb_interrupt(int irq, void *__hci)
  124. {
  125. unsigned long flags;
  126. irqreturn_t retval = IRQ_NONE;
  127. struct musb *musb = __hci;
  128. spin_lock_irqsave(&musb->lock, flags);
  129. musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
  130. musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
  131. musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
  132. if (musb->int_usb || musb->int_tx || musb->int_rx)
  133. retval = musb_interrupt(musb);
  134. spin_unlock_irqrestore(&musb->lock, flags);
  135. return retval;
  136. }
  137. static int ux500_musb_init(struct musb *musb)
  138. {
  139. int status;
  140. musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
  141. if (IS_ERR_OR_NULL(musb->xceiv)) {
  142. pr_err("HS USB OTG: no transceiver configured\n");
  143. return -EPROBE_DEFER;
  144. }
  145. musb->nb.notifier_call = musb_otg_notifications;
  146. status = usb_register_notifier(musb->xceiv, &musb->nb);
  147. if (status < 0) {
  148. dev_dbg(musb->controller, "notification register failed\n");
  149. return status;
  150. }
  151. musb->isr = ux500_musb_interrupt;
  152. return 0;
  153. }
  154. static int ux500_musb_exit(struct musb *musb)
  155. {
  156. usb_unregister_notifier(musb->xceiv, &musb->nb);
  157. usb_put_phy(musb->xceiv);
  158. return 0;
  159. }
  160. static const struct musb_platform_ops ux500_ops = {
  161. .quirks = MUSB_DMA_UX500 | MUSB_INDEXED_EP,
  162. #ifdef CONFIG_USB_UX500_DMA
  163. .dma_init = ux500_dma_controller_create,
  164. .dma_exit = ux500_dma_controller_destroy,
  165. #endif
  166. .init = ux500_musb_init,
  167. .exit = ux500_musb_exit,
  168. .fifo_mode = 5,
  169. .set_vbus = ux500_musb_set_vbus,
  170. };
  171. static struct musb_hdrc_platform_data *
  172. ux500_of_probe(struct platform_device *pdev, struct device_node *np)
  173. {
  174. struct musb_hdrc_platform_data *pdata;
  175. const char *mode;
  176. int strlen;
  177. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  178. if (!pdata)
  179. return NULL;
  180. mode = of_get_property(np, "dr_mode", &strlen);
  181. if (!mode) {
  182. dev_err(&pdev->dev, "No 'dr_mode' property found\n");
  183. return NULL;
  184. }
  185. if (strlen > 0) {
  186. if (!strcmp(mode, "host"))
  187. pdata->mode = MUSB_HOST;
  188. if (!strcmp(mode, "otg"))
  189. pdata->mode = MUSB_OTG;
  190. if (!strcmp(mode, "peripheral"))
  191. pdata->mode = MUSB_PERIPHERAL;
  192. }
  193. return pdata;
  194. }
  195. static int ux500_probe(struct platform_device *pdev)
  196. {
  197. struct resource musb_resources[2];
  198. struct musb_hdrc_platform_data *pdata = dev_get_platdata(&pdev->dev);
  199. struct device_node *np = pdev->dev.of_node;
  200. struct platform_device *musb;
  201. struct ux500_glue *glue;
  202. struct clk *clk;
  203. int ret = -ENOMEM;
  204. if (!pdata) {
  205. if (np) {
  206. pdata = ux500_of_probe(pdev, np);
  207. if (!pdata)
  208. goto err0;
  209. pdev->dev.platform_data = pdata;
  210. } else {
  211. dev_err(&pdev->dev, "no pdata or device tree found\n");
  212. goto err0;
  213. }
  214. }
  215. glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
  216. if (!glue)
  217. goto err0;
  218. musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
  219. if (!musb) {
  220. dev_err(&pdev->dev, "failed to allocate musb device\n");
  221. goto err0;
  222. }
  223. clk = devm_clk_get(&pdev->dev, NULL);
  224. if (IS_ERR(clk)) {
  225. dev_err(&pdev->dev, "failed to get clock\n");
  226. ret = PTR_ERR(clk);
  227. goto err1;
  228. }
  229. ret = clk_prepare_enable(clk);
  230. if (ret) {
  231. dev_err(&pdev->dev, "failed to enable clock\n");
  232. goto err1;
  233. }
  234. musb->dev.parent = &pdev->dev;
  235. musb->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  236. musb->dev.coherent_dma_mask = pdev->dev.coherent_dma_mask;
  237. glue->dev = &pdev->dev;
  238. glue->musb = musb;
  239. glue->clk = clk;
  240. pdata->platform_ops = &ux500_ops;
  241. pdata->config = &ux500_musb_hdrc_config;
  242. platform_set_drvdata(pdev, glue);
  243. memset(musb_resources, 0x00, sizeof(*musb_resources) *
  244. ARRAY_SIZE(musb_resources));
  245. musb_resources[0].name = pdev->resource[0].name;
  246. musb_resources[0].start = pdev->resource[0].start;
  247. musb_resources[0].end = pdev->resource[0].end;
  248. musb_resources[0].flags = pdev->resource[0].flags;
  249. musb_resources[1].name = pdev->resource[1].name;
  250. musb_resources[1].start = pdev->resource[1].start;
  251. musb_resources[1].end = pdev->resource[1].end;
  252. musb_resources[1].flags = pdev->resource[1].flags;
  253. ret = platform_device_add_resources(musb, musb_resources,
  254. ARRAY_SIZE(musb_resources));
  255. if (ret) {
  256. dev_err(&pdev->dev, "failed to add resources\n");
  257. goto err2;
  258. }
  259. ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
  260. if (ret) {
  261. dev_err(&pdev->dev, "failed to add platform_data\n");
  262. goto err2;
  263. }
  264. ret = platform_device_add(musb);
  265. if (ret) {
  266. dev_err(&pdev->dev, "failed to register musb device\n");
  267. goto err2;
  268. }
  269. return 0;
  270. err2:
  271. clk_disable_unprepare(clk);
  272. err1:
  273. platform_device_put(musb);
  274. err0:
  275. return ret;
  276. }
  277. static int ux500_remove(struct platform_device *pdev)
  278. {
  279. struct ux500_glue *glue = platform_get_drvdata(pdev);
  280. platform_device_unregister(glue->musb);
  281. clk_disable_unprepare(glue->clk);
  282. return 0;
  283. }
  284. #ifdef CONFIG_PM_SLEEP
  285. static int ux500_suspend(struct device *dev)
  286. {
  287. struct ux500_glue *glue = dev_get_drvdata(dev);
  288. struct musb *musb = glue_to_musb(glue);
  289. if (musb)
  290. usb_phy_set_suspend(musb->xceiv, 1);
  291. clk_disable_unprepare(glue->clk);
  292. return 0;
  293. }
  294. static int ux500_resume(struct device *dev)
  295. {
  296. struct ux500_glue *glue = dev_get_drvdata(dev);
  297. struct musb *musb = glue_to_musb(glue);
  298. int ret;
  299. ret = clk_prepare_enable(glue->clk);
  300. if (ret) {
  301. dev_err(dev, "failed to enable clock\n");
  302. return ret;
  303. }
  304. if (musb)
  305. usb_phy_set_suspend(musb->xceiv, 0);
  306. return 0;
  307. }
  308. #endif
  309. static SIMPLE_DEV_PM_OPS(ux500_pm_ops, ux500_suspend, ux500_resume);
  310. static const struct of_device_id ux500_match[] = {
  311. { .compatible = "stericsson,db8500-musb", },
  312. {}
  313. };
  314. MODULE_DEVICE_TABLE(of, ux500_match);
  315. static struct platform_driver ux500_driver = {
  316. .probe = ux500_probe,
  317. .remove = ux500_remove,
  318. .driver = {
  319. .name = "musb-ux500",
  320. .pm = &ux500_pm_ops,
  321. .of_match_table = ux500_match,
  322. },
  323. };
  324. MODULE_DESCRIPTION("UX500 MUSB Glue Layer");
  325. MODULE_AUTHOR("Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>");
  326. MODULE_LICENSE("GPL v2");
  327. module_platform_driver(ux500_driver);