musb_dsps.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /*
  2. * Texas Instruments DSPS platforms "glue layer"
  3. *
  4. * Copyright (C) 2012, by Texas Instruments
  5. *
  6. * Based on the am35x "glue layer" code.
  7. *
  8. * This file is part of the Inventra Controller Driver for Linux.
  9. *
  10. * The Inventra Controller Driver for Linux is free software; you
  11. * can redistribute it and/or modify it under the terms of the GNU
  12. * General Public License version 2 as published by the Free Software
  13. * Foundation.
  14. *
  15. * The Inventra Controller Driver for Linux is distributed in
  16. * the hope that it will be useful, but WITHOUT ANY WARRANTY;
  17. * without even the implied warranty of MERCHANTABILITY or
  18. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  19. * License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with The Inventra Controller Driver for Linux ; if not,
  23. * write to the Free Software Foundation, Inc., 59 Temple Place,
  24. * Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. * musb_dsps.c will be a common file for all the TI DSPS platforms
  27. * such as dm64x, dm36x, dm35x, da8x, am35x and ti81x.
  28. * For now only ti81x is using this and in future davinci.c, am35x.c
  29. * da8xx.c would be merged to this file after testing.
  30. */
  31. #include <linux/io.h>
  32. #include <linux/err.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/pm_runtime.h>
  36. #include <linux/module.h>
  37. #include <linux/usb/usb_phy_generic.h>
  38. #include <linux/platform_data/usb-omap.h>
  39. #include <linux/sizes.h>
  40. #include <linux/of.h>
  41. #include <linux/of_device.h>
  42. #include <linux/of_address.h>
  43. #include <linux/of_irq.h>
  44. #include <linux/usb/of.h>
  45. #include <linux/debugfs.h>
  46. #include "musb_core.h"
  47. static const struct of_device_id musb_dsps_of_match[];
  48. /**
  49. * DSPS musb wrapper register offset.
  50. * FIXME: This should be expanded to have all the wrapper registers from TI DSPS
  51. * musb ips.
  52. */
  53. struct dsps_musb_wrapper {
  54. u16 revision;
  55. u16 control;
  56. u16 status;
  57. u16 epintr_set;
  58. u16 epintr_clear;
  59. u16 epintr_status;
  60. u16 coreintr_set;
  61. u16 coreintr_clear;
  62. u16 coreintr_status;
  63. u16 phy_utmi;
  64. u16 mode;
  65. u16 tx_mode;
  66. u16 rx_mode;
  67. /* bit positions for control */
  68. unsigned reset:5;
  69. /* bit positions for interrupt */
  70. unsigned usb_shift:5;
  71. u32 usb_mask;
  72. u32 usb_bitmap;
  73. unsigned drvvbus:5;
  74. unsigned txep_shift:5;
  75. u32 txep_mask;
  76. u32 txep_bitmap;
  77. unsigned rxep_shift:5;
  78. u32 rxep_mask;
  79. u32 rxep_bitmap;
  80. /* bit positions for phy_utmi */
  81. unsigned otg_disable:5;
  82. /* bit positions for mode */
  83. unsigned iddig:5;
  84. unsigned iddig_mux:5;
  85. /* miscellaneous stuff */
  86. unsigned poll_timeout;
  87. };
  88. /*
  89. * register shadow for suspend
  90. */
  91. struct dsps_context {
  92. u32 control;
  93. u32 epintr;
  94. u32 coreintr;
  95. u32 phy_utmi;
  96. u32 mode;
  97. u32 tx_mode;
  98. u32 rx_mode;
  99. };
  100. /**
  101. * DSPS glue structure.
  102. */
  103. struct dsps_glue {
  104. struct device *dev;
  105. struct platform_device *musb; /* child musb pdev */
  106. const struct dsps_musb_wrapper *wrp; /* wrapper register offsets */
  107. struct timer_list timer; /* otg_workaround timer */
  108. unsigned long last_timer; /* last timer data for each instance */
  109. bool sw_babble_enabled;
  110. struct dsps_context context;
  111. struct debugfs_regset32 regset;
  112. struct dentry *dbgfs_root;
  113. };
  114. static const struct debugfs_reg32 dsps_musb_regs[] = {
  115. { "revision", 0x00 },
  116. { "control", 0x14 },
  117. { "status", 0x18 },
  118. { "eoi", 0x24 },
  119. { "intr0_stat", 0x30 },
  120. { "intr1_stat", 0x34 },
  121. { "intr0_set", 0x38 },
  122. { "intr1_set", 0x3c },
  123. { "txmode", 0x70 },
  124. { "rxmode", 0x74 },
  125. { "autoreq", 0xd0 },
  126. { "srpfixtime", 0xd4 },
  127. { "tdown", 0xd8 },
  128. { "phy_utmi", 0xe0 },
  129. { "mode", 0xe8 },
  130. };
  131. /**
  132. * dsps_musb_enable - enable interrupts
  133. */
  134. static void dsps_musb_enable(struct musb *musb)
  135. {
  136. struct device *dev = musb->controller;
  137. struct platform_device *pdev = to_platform_device(dev->parent);
  138. struct dsps_glue *glue = platform_get_drvdata(pdev);
  139. const struct dsps_musb_wrapper *wrp = glue->wrp;
  140. void __iomem *reg_base = musb->ctrl_base;
  141. u32 epmask, coremask;
  142. /* Workaround: setup IRQs through both register sets. */
  143. epmask = ((musb->epmask & wrp->txep_mask) << wrp->txep_shift) |
  144. ((musb->epmask & wrp->rxep_mask) << wrp->rxep_shift);
  145. coremask = (wrp->usb_bitmap & ~MUSB_INTR_SOF);
  146. musb_writel(reg_base, wrp->epintr_set, epmask);
  147. musb_writel(reg_base, wrp->coreintr_set, coremask);
  148. /* start polling for ID change in dual-role idle mode */
  149. if (musb->xceiv->otg->state == OTG_STATE_B_IDLE &&
  150. musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE)
  151. mod_timer(&glue->timer, jiffies +
  152. msecs_to_jiffies(wrp->poll_timeout));
  153. }
  154. /**
  155. * dsps_musb_disable - disable HDRC and flush interrupts
  156. */
  157. static void dsps_musb_disable(struct musb *musb)
  158. {
  159. struct device *dev = musb->controller;
  160. struct platform_device *pdev = to_platform_device(dev->parent);
  161. struct dsps_glue *glue = platform_get_drvdata(pdev);
  162. const struct dsps_musb_wrapper *wrp = glue->wrp;
  163. void __iomem *reg_base = musb->ctrl_base;
  164. musb_writel(reg_base, wrp->coreintr_clear, wrp->usb_bitmap);
  165. musb_writel(reg_base, wrp->epintr_clear,
  166. wrp->txep_bitmap | wrp->rxep_bitmap);
  167. musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
  168. }
  169. static void otg_timer(unsigned long _musb)
  170. {
  171. struct musb *musb = (void *)_musb;
  172. void __iomem *mregs = musb->mregs;
  173. struct device *dev = musb->controller;
  174. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  175. const struct dsps_musb_wrapper *wrp = glue->wrp;
  176. u8 devctl;
  177. unsigned long flags;
  178. int skip_session = 0;
  179. int err;
  180. err = pm_runtime_get_sync(dev);
  181. if (err < 0)
  182. dev_err(dev, "Poll could not pm_runtime_get: %i\n", err);
  183. /*
  184. * We poll because DSPS IP's won't expose several OTG-critical
  185. * status change events (from the transceiver) otherwise.
  186. */
  187. devctl = musb_readb(mregs, MUSB_DEVCTL);
  188. dev_dbg(musb->controller, "Poll devctl %02x (%s)\n", devctl,
  189. usb_otg_state_string(musb->xceiv->otg->state));
  190. spin_lock_irqsave(&musb->lock, flags);
  191. switch (musb->xceiv->otg->state) {
  192. case OTG_STATE_A_WAIT_VRISE:
  193. mod_timer(&glue->timer, jiffies +
  194. msecs_to_jiffies(wrp->poll_timeout));
  195. break;
  196. case OTG_STATE_A_WAIT_BCON:
  197. musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
  198. skip_session = 1;
  199. /* fall */
  200. case OTG_STATE_A_IDLE:
  201. case OTG_STATE_B_IDLE:
  202. if (devctl & MUSB_DEVCTL_BDEVICE) {
  203. musb->xceiv->otg->state = OTG_STATE_B_IDLE;
  204. MUSB_DEV_MODE(musb);
  205. } else {
  206. musb->xceiv->otg->state = OTG_STATE_A_IDLE;
  207. MUSB_HST_MODE(musb);
  208. }
  209. if (!(devctl & MUSB_DEVCTL_SESSION) && !skip_session)
  210. musb_writeb(mregs, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
  211. mod_timer(&glue->timer, jiffies +
  212. msecs_to_jiffies(wrp->poll_timeout));
  213. break;
  214. case OTG_STATE_A_WAIT_VFALL:
  215. musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
  216. musb_writel(musb->ctrl_base, wrp->coreintr_set,
  217. MUSB_INTR_VBUSERROR << wrp->usb_shift);
  218. break;
  219. default:
  220. break;
  221. }
  222. spin_unlock_irqrestore(&musb->lock, flags);
  223. pm_runtime_mark_last_busy(dev);
  224. pm_runtime_put_autosuspend(dev);
  225. }
  226. static irqreturn_t dsps_interrupt(int irq, void *hci)
  227. {
  228. struct musb *musb = hci;
  229. void __iomem *reg_base = musb->ctrl_base;
  230. struct device *dev = musb->controller;
  231. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  232. const struct dsps_musb_wrapper *wrp = glue->wrp;
  233. unsigned long flags;
  234. irqreturn_t ret = IRQ_NONE;
  235. u32 epintr, usbintr;
  236. spin_lock_irqsave(&musb->lock, flags);
  237. /* Get endpoint interrupts */
  238. epintr = musb_readl(reg_base, wrp->epintr_status);
  239. musb->int_rx = (epintr & wrp->rxep_bitmap) >> wrp->rxep_shift;
  240. musb->int_tx = (epintr & wrp->txep_bitmap) >> wrp->txep_shift;
  241. if (epintr)
  242. musb_writel(reg_base, wrp->epintr_status, epintr);
  243. /* Get usb core interrupts */
  244. usbintr = musb_readl(reg_base, wrp->coreintr_status);
  245. if (!usbintr && !epintr)
  246. goto out;
  247. musb->int_usb = (usbintr & wrp->usb_bitmap) >> wrp->usb_shift;
  248. if (usbintr)
  249. musb_writel(reg_base, wrp->coreintr_status, usbintr);
  250. dev_dbg(musb->controller, "usbintr (%x) epintr(%x)\n",
  251. usbintr, epintr);
  252. if (usbintr & ((1 << wrp->drvvbus) << wrp->usb_shift)) {
  253. int drvvbus = musb_readl(reg_base, wrp->status);
  254. void __iomem *mregs = musb->mregs;
  255. u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
  256. int err;
  257. err = musb->int_usb & MUSB_INTR_VBUSERROR;
  258. if (err) {
  259. /*
  260. * The Mentor core doesn't debounce VBUS as needed
  261. * to cope with device connect current spikes. This
  262. * means it's not uncommon for bus-powered devices
  263. * to get VBUS errors during enumeration.
  264. *
  265. * This is a workaround, but newer RTL from Mentor
  266. * seems to allow a better one: "re"-starting sessions
  267. * without waiting for VBUS to stop registering in
  268. * devctl.
  269. */
  270. musb->int_usb &= ~MUSB_INTR_VBUSERROR;
  271. musb->xceiv->otg->state = OTG_STATE_A_WAIT_VFALL;
  272. mod_timer(&glue->timer, jiffies +
  273. msecs_to_jiffies(wrp->poll_timeout));
  274. WARNING("VBUS error workaround (delay coming)\n");
  275. } else if (drvvbus) {
  276. MUSB_HST_MODE(musb);
  277. musb->xceiv->otg->default_a = 1;
  278. musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
  279. mod_timer(&glue->timer, jiffies +
  280. msecs_to_jiffies(wrp->poll_timeout));
  281. } else {
  282. musb->is_active = 0;
  283. MUSB_DEV_MODE(musb);
  284. musb->xceiv->otg->default_a = 0;
  285. musb->xceiv->otg->state = OTG_STATE_B_IDLE;
  286. }
  287. /* NOTE: this must complete power-on within 100 ms. */
  288. dev_dbg(musb->controller, "VBUS %s (%s)%s, devctl %02x\n",
  289. drvvbus ? "on" : "off",
  290. usb_otg_state_string(musb->xceiv->otg->state),
  291. err ? " ERROR" : "",
  292. devctl);
  293. ret = IRQ_HANDLED;
  294. }
  295. if (musb->int_tx || musb->int_rx || musb->int_usb)
  296. ret |= musb_interrupt(musb);
  297. /* Poll for ID change and connect */
  298. switch (musb->xceiv->otg->state) {
  299. case OTG_STATE_B_IDLE:
  300. case OTG_STATE_A_WAIT_BCON:
  301. mod_timer(&glue->timer, jiffies +
  302. msecs_to_jiffies(wrp->poll_timeout));
  303. break;
  304. default:
  305. break;
  306. }
  307. out:
  308. spin_unlock_irqrestore(&musb->lock, flags);
  309. return ret;
  310. }
  311. static int dsps_musb_dbg_init(struct musb *musb, struct dsps_glue *glue)
  312. {
  313. struct dentry *root;
  314. struct dentry *file;
  315. char buf[128];
  316. sprintf(buf, "%s.dsps", dev_name(musb->controller));
  317. root = debugfs_create_dir(buf, NULL);
  318. if (!root)
  319. return -ENOMEM;
  320. glue->dbgfs_root = root;
  321. glue->regset.regs = dsps_musb_regs;
  322. glue->regset.nregs = ARRAY_SIZE(dsps_musb_regs);
  323. glue->regset.base = musb->ctrl_base;
  324. file = debugfs_create_regset32("regdump", S_IRUGO, root, &glue->regset);
  325. if (!file) {
  326. debugfs_remove_recursive(root);
  327. return -ENOMEM;
  328. }
  329. return 0;
  330. }
  331. static int dsps_musb_init(struct musb *musb)
  332. {
  333. struct device *dev = musb->controller;
  334. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  335. struct platform_device *parent = to_platform_device(dev->parent);
  336. const struct dsps_musb_wrapper *wrp = glue->wrp;
  337. void __iomem *reg_base;
  338. struct resource *r;
  339. u32 rev, val;
  340. int ret;
  341. r = platform_get_resource_byname(parent, IORESOURCE_MEM, "control");
  342. reg_base = devm_ioremap_resource(dev, r);
  343. if (IS_ERR(reg_base))
  344. return PTR_ERR(reg_base);
  345. musb->ctrl_base = reg_base;
  346. /* NOP driver needs change if supporting dual instance */
  347. musb->xceiv = devm_usb_get_phy_by_phandle(dev->parent, "phys", 0);
  348. if (IS_ERR(musb->xceiv))
  349. return PTR_ERR(musb->xceiv);
  350. musb->phy = devm_phy_get(dev->parent, "usb2-phy");
  351. /* Returns zero if e.g. not clocked */
  352. rev = musb_readl(reg_base, wrp->revision);
  353. if (!rev)
  354. return -ENODEV;
  355. usb_phy_init(musb->xceiv);
  356. if (IS_ERR(musb->phy)) {
  357. musb->phy = NULL;
  358. } else {
  359. ret = phy_init(musb->phy);
  360. if (ret < 0)
  361. return ret;
  362. ret = phy_power_on(musb->phy);
  363. if (ret) {
  364. phy_exit(musb->phy);
  365. return ret;
  366. }
  367. }
  368. setup_timer(&glue->timer, otg_timer, (unsigned long) musb);
  369. /* Reset the musb */
  370. musb_writel(reg_base, wrp->control, (1 << wrp->reset));
  371. musb->isr = dsps_interrupt;
  372. /* reset the otgdisable bit, needed for host mode to work */
  373. val = musb_readl(reg_base, wrp->phy_utmi);
  374. val &= ~(1 << wrp->otg_disable);
  375. musb_writel(musb->ctrl_base, wrp->phy_utmi, val);
  376. /*
  377. * Check whether the dsps version has babble control enabled.
  378. * In latest silicon revision the babble control logic is enabled.
  379. * If MUSB_BABBLE_CTL returns 0x4 then we have the babble control
  380. * logic enabled.
  381. */
  382. val = musb_readb(musb->mregs, MUSB_BABBLE_CTL);
  383. if (val & MUSB_BABBLE_RCV_DISABLE) {
  384. glue->sw_babble_enabled = true;
  385. val |= MUSB_BABBLE_SW_SESSION_CTRL;
  386. musb_writeb(musb->mregs, MUSB_BABBLE_CTL, val);
  387. }
  388. mod_timer(&glue->timer, jiffies +
  389. msecs_to_jiffies(glue->wrp->poll_timeout));
  390. return dsps_musb_dbg_init(musb, glue);
  391. }
  392. static int dsps_musb_exit(struct musb *musb)
  393. {
  394. struct device *dev = musb->controller;
  395. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  396. del_timer_sync(&glue->timer);
  397. usb_phy_shutdown(musb->xceiv);
  398. phy_power_off(musb->phy);
  399. phy_exit(musb->phy);
  400. debugfs_remove_recursive(glue->dbgfs_root);
  401. return 0;
  402. }
  403. static int dsps_musb_set_mode(struct musb *musb, u8 mode)
  404. {
  405. struct device *dev = musb->controller;
  406. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  407. const struct dsps_musb_wrapper *wrp = glue->wrp;
  408. void __iomem *ctrl_base = musb->ctrl_base;
  409. u32 reg;
  410. reg = musb_readl(ctrl_base, wrp->mode);
  411. switch (mode) {
  412. case MUSB_HOST:
  413. reg &= ~(1 << wrp->iddig);
  414. /*
  415. * if we're setting mode to host-only or device-only, we're
  416. * going to ignore whatever the PHY sends us and just force
  417. * ID pin status by SW
  418. */
  419. reg |= (1 << wrp->iddig_mux);
  420. musb_writel(ctrl_base, wrp->mode, reg);
  421. musb_writel(ctrl_base, wrp->phy_utmi, 0x02);
  422. break;
  423. case MUSB_PERIPHERAL:
  424. reg |= (1 << wrp->iddig);
  425. /*
  426. * if we're setting mode to host-only or device-only, we're
  427. * going to ignore whatever the PHY sends us and just force
  428. * ID pin status by SW
  429. */
  430. reg |= (1 << wrp->iddig_mux);
  431. musb_writel(ctrl_base, wrp->mode, reg);
  432. break;
  433. case MUSB_OTG:
  434. musb_writel(ctrl_base, wrp->phy_utmi, 0x02);
  435. break;
  436. default:
  437. dev_err(glue->dev, "unsupported mode %d\n", mode);
  438. return -EINVAL;
  439. }
  440. return 0;
  441. }
  442. static bool dsps_sw_babble_control(struct musb *musb)
  443. {
  444. u8 babble_ctl;
  445. bool session_restart = false;
  446. babble_ctl = musb_readb(musb->mregs, MUSB_BABBLE_CTL);
  447. dev_dbg(musb->controller, "babble: MUSB_BABBLE_CTL value %x\n",
  448. babble_ctl);
  449. /*
  450. * check line monitor flag to check whether babble is
  451. * due to noise
  452. */
  453. dev_dbg(musb->controller, "STUCK_J is %s\n",
  454. babble_ctl & MUSB_BABBLE_STUCK_J ? "set" : "reset");
  455. if (babble_ctl & MUSB_BABBLE_STUCK_J) {
  456. int timeout = 10;
  457. /*
  458. * babble is due to noise, then set transmit idle (d7 bit)
  459. * to resume normal operation
  460. */
  461. babble_ctl = musb_readb(musb->mregs, MUSB_BABBLE_CTL);
  462. babble_ctl |= MUSB_BABBLE_FORCE_TXIDLE;
  463. musb_writeb(musb->mregs, MUSB_BABBLE_CTL, babble_ctl);
  464. /* wait till line monitor flag cleared */
  465. dev_dbg(musb->controller, "Set TXIDLE, wait J to clear\n");
  466. do {
  467. babble_ctl = musb_readb(musb->mregs, MUSB_BABBLE_CTL);
  468. udelay(1);
  469. } while ((babble_ctl & MUSB_BABBLE_STUCK_J) && timeout--);
  470. /* check whether stuck_at_j bit cleared */
  471. if (babble_ctl & MUSB_BABBLE_STUCK_J) {
  472. /*
  473. * real babble condition has occurred
  474. * restart the controller to start the
  475. * session again
  476. */
  477. dev_dbg(musb->controller, "J not cleared, misc (%x)\n",
  478. babble_ctl);
  479. session_restart = true;
  480. }
  481. } else {
  482. session_restart = true;
  483. }
  484. return session_restart;
  485. }
  486. static int dsps_musb_recover(struct musb *musb)
  487. {
  488. struct device *dev = musb->controller;
  489. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  490. int session_restart = 0;
  491. if (glue->sw_babble_enabled)
  492. session_restart = dsps_sw_babble_control(musb);
  493. else
  494. session_restart = 1;
  495. return session_restart ? 0 : -EPIPE;
  496. }
  497. /* Similar to am35x, dm81xx support only 32-bit read operation */
  498. static void dsps_read_fifo32(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
  499. {
  500. void __iomem *fifo = hw_ep->fifo;
  501. if (len >= 4) {
  502. ioread32_rep(fifo, dst, len >> 2);
  503. dst += len & ~0x03;
  504. len &= 0x03;
  505. }
  506. /* Read any remaining 1 to 3 bytes */
  507. if (len > 0) {
  508. u32 val = musb_readl(fifo, 0);
  509. memcpy(dst, &val, len);
  510. }
  511. }
  512. static struct musb_platform_ops dsps_ops = {
  513. .quirks = MUSB_DMA_CPPI41 | MUSB_INDEXED_EP,
  514. .init = dsps_musb_init,
  515. .exit = dsps_musb_exit,
  516. #ifdef CONFIG_USB_TI_CPPI41_DMA
  517. .dma_init = cppi41_dma_controller_create,
  518. .dma_exit = cppi41_dma_controller_destroy,
  519. #endif
  520. .enable = dsps_musb_enable,
  521. .disable = dsps_musb_disable,
  522. .set_mode = dsps_musb_set_mode,
  523. .recover = dsps_musb_recover,
  524. };
  525. static u64 musb_dmamask = DMA_BIT_MASK(32);
  526. static int get_int_prop(struct device_node *dn, const char *s)
  527. {
  528. int ret;
  529. u32 val;
  530. ret = of_property_read_u32(dn, s, &val);
  531. if (ret)
  532. return 0;
  533. return val;
  534. }
  535. static int get_musb_port_mode(struct device *dev)
  536. {
  537. enum usb_dr_mode mode;
  538. mode = usb_get_dr_mode(dev);
  539. switch (mode) {
  540. case USB_DR_MODE_HOST:
  541. return MUSB_PORT_MODE_HOST;
  542. case USB_DR_MODE_PERIPHERAL:
  543. return MUSB_PORT_MODE_GADGET;
  544. case USB_DR_MODE_UNKNOWN:
  545. case USB_DR_MODE_OTG:
  546. default:
  547. return MUSB_PORT_MODE_DUAL_ROLE;
  548. }
  549. }
  550. static int dsps_create_musb_pdev(struct dsps_glue *glue,
  551. struct platform_device *parent)
  552. {
  553. struct musb_hdrc_platform_data pdata;
  554. struct resource resources[2];
  555. struct resource *res;
  556. struct device *dev = &parent->dev;
  557. struct musb_hdrc_config *config;
  558. struct platform_device *musb;
  559. struct device_node *dn = parent->dev.of_node;
  560. int ret, val;
  561. memset(resources, 0, sizeof(resources));
  562. res = platform_get_resource_byname(parent, IORESOURCE_MEM, "mc");
  563. if (!res) {
  564. dev_err(dev, "failed to get memory.\n");
  565. return -EINVAL;
  566. }
  567. resources[0] = *res;
  568. res = platform_get_resource_byname(parent, IORESOURCE_IRQ, "mc");
  569. if (!res) {
  570. dev_err(dev, "failed to get irq.\n");
  571. return -EINVAL;
  572. }
  573. resources[1] = *res;
  574. /* allocate the child platform device */
  575. musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
  576. if (!musb) {
  577. dev_err(dev, "failed to allocate musb device\n");
  578. return -ENOMEM;
  579. }
  580. musb->dev.parent = dev;
  581. musb->dev.dma_mask = &musb_dmamask;
  582. musb->dev.coherent_dma_mask = musb_dmamask;
  583. glue->musb = musb;
  584. ret = platform_device_add_resources(musb, resources,
  585. ARRAY_SIZE(resources));
  586. if (ret) {
  587. dev_err(dev, "failed to add resources\n");
  588. goto err;
  589. }
  590. config = devm_kzalloc(&parent->dev, sizeof(*config), GFP_KERNEL);
  591. if (!config) {
  592. ret = -ENOMEM;
  593. goto err;
  594. }
  595. pdata.config = config;
  596. pdata.platform_ops = &dsps_ops;
  597. config->num_eps = get_int_prop(dn, "mentor,num-eps");
  598. config->ram_bits = get_int_prop(dn, "mentor,ram-bits");
  599. config->host_port_deassert_reset_at_resume = 1;
  600. pdata.mode = get_musb_port_mode(dev);
  601. /* DT keeps this entry in mA, musb expects it as per USB spec */
  602. pdata.power = get_int_prop(dn, "mentor,power") / 2;
  603. ret = of_property_read_u32(dn, "mentor,multipoint", &val);
  604. if (!ret && val)
  605. config->multipoint = true;
  606. config->maximum_speed = usb_get_maximum_speed(&parent->dev);
  607. switch (config->maximum_speed) {
  608. case USB_SPEED_LOW:
  609. case USB_SPEED_FULL:
  610. break;
  611. case USB_SPEED_SUPER:
  612. dev_warn(dev, "ignore incorrect maximum_speed "
  613. "(super-speed) setting in dts");
  614. /* fall through */
  615. default:
  616. config->maximum_speed = USB_SPEED_HIGH;
  617. }
  618. ret = platform_device_add_data(musb, &pdata, sizeof(pdata));
  619. if (ret) {
  620. dev_err(dev, "failed to add platform_data\n");
  621. goto err;
  622. }
  623. ret = platform_device_add(musb);
  624. if (ret) {
  625. dev_err(dev, "failed to register musb device\n");
  626. goto err;
  627. }
  628. return 0;
  629. err:
  630. platform_device_put(musb);
  631. return ret;
  632. }
  633. static int dsps_probe(struct platform_device *pdev)
  634. {
  635. const struct of_device_id *match;
  636. const struct dsps_musb_wrapper *wrp;
  637. struct dsps_glue *glue;
  638. int ret;
  639. if (!strcmp(pdev->name, "musb-hdrc"))
  640. return -ENODEV;
  641. match = of_match_node(musb_dsps_of_match, pdev->dev.of_node);
  642. if (!match) {
  643. dev_err(&pdev->dev, "fail to get matching of_match struct\n");
  644. return -EINVAL;
  645. }
  646. wrp = match->data;
  647. if (of_device_is_compatible(pdev->dev.of_node, "ti,musb-dm816"))
  648. dsps_ops.read_fifo = dsps_read_fifo32;
  649. /* allocate glue */
  650. glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
  651. if (!glue)
  652. return -ENOMEM;
  653. glue->dev = &pdev->dev;
  654. glue->wrp = wrp;
  655. platform_set_drvdata(pdev, glue);
  656. pm_runtime_enable(&pdev->dev);
  657. pm_runtime_use_autosuspend(&pdev->dev);
  658. pm_runtime_set_autosuspend_delay(&pdev->dev, 200);
  659. ret = pm_runtime_get_sync(&pdev->dev);
  660. if (ret < 0) {
  661. dev_err(&pdev->dev, "pm_runtime_get_sync FAILED");
  662. goto err2;
  663. }
  664. ret = dsps_create_musb_pdev(glue, pdev);
  665. if (ret)
  666. goto err3;
  667. pm_runtime_mark_last_busy(&pdev->dev);
  668. pm_runtime_put_autosuspend(&pdev->dev);
  669. return 0;
  670. err3:
  671. pm_runtime_put_sync(&pdev->dev);
  672. err2:
  673. pm_runtime_dont_use_autosuspend(&pdev->dev);
  674. pm_runtime_disable(&pdev->dev);
  675. return ret;
  676. }
  677. static int dsps_remove(struct platform_device *pdev)
  678. {
  679. struct dsps_glue *glue = platform_get_drvdata(pdev);
  680. platform_device_unregister(glue->musb);
  681. /* disable usbss clocks */
  682. pm_runtime_dont_use_autosuspend(&pdev->dev);
  683. pm_runtime_put_sync(&pdev->dev);
  684. pm_runtime_disable(&pdev->dev);
  685. return 0;
  686. }
  687. static const struct dsps_musb_wrapper am33xx_driver_data = {
  688. .revision = 0x00,
  689. .control = 0x14,
  690. .status = 0x18,
  691. .epintr_set = 0x38,
  692. .epintr_clear = 0x40,
  693. .epintr_status = 0x30,
  694. .coreintr_set = 0x3c,
  695. .coreintr_clear = 0x44,
  696. .coreintr_status = 0x34,
  697. .phy_utmi = 0xe0,
  698. .mode = 0xe8,
  699. .tx_mode = 0x70,
  700. .rx_mode = 0x74,
  701. .reset = 0,
  702. .otg_disable = 21,
  703. .iddig = 8,
  704. .iddig_mux = 7,
  705. .usb_shift = 0,
  706. .usb_mask = 0x1ff,
  707. .usb_bitmap = (0x1ff << 0),
  708. .drvvbus = 8,
  709. .txep_shift = 0,
  710. .txep_mask = 0xffff,
  711. .txep_bitmap = (0xffff << 0),
  712. .rxep_shift = 16,
  713. .rxep_mask = 0xfffe,
  714. .rxep_bitmap = (0xfffe << 16),
  715. .poll_timeout = 2000, /* ms */
  716. };
  717. static const struct of_device_id musb_dsps_of_match[] = {
  718. { .compatible = "ti,musb-am33xx",
  719. .data = &am33xx_driver_data, },
  720. { .compatible = "ti,musb-dm816",
  721. .data = &am33xx_driver_data, },
  722. { },
  723. };
  724. MODULE_DEVICE_TABLE(of, musb_dsps_of_match);
  725. #ifdef CONFIG_PM_SLEEP
  726. static int dsps_suspend(struct device *dev)
  727. {
  728. struct dsps_glue *glue = dev_get_drvdata(dev);
  729. const struct dsps_musb_wrapper *wrp = glue->wrp;
  730. struct musb *musb = platform_get_drvdata(glue->musb);
  731. void __iomem *mbase;
  732. del_timer_sync(&glue->timer);
  733. if (!musb)
  734. /* This can happen if the musb device is in -EPROBE_DEFER */
  735. return 0;
  736. mbase = musb->ctrl_base;
  737. glue->context.control = musb_readl(mbase, wrp->control);
  738. glue->context.epintr = musb_readl(mbase, wrp->epintr_set);
  739. glue->context.coreintr = musb_readl(mbase, wrp->coreintr_set);
  740. glue->context.phy_utmi = musb_readl(mbase, wrp->phy_utmi);
  741. glue->context.mode = musb_readl(mbase, wrp->mode);
  742. glue->context.tx_mode = musb_readl(mbase, wrp->tx_mode);
  743. glue->context.rx_mode = musb_readl(mbase, wrp->rx_mode);
  744. return 0;
  745. }
  746. static int dsps_resume(struct device *dev)
  747. {
  748. struct dsps_glue *glue = dev_get_drvdata(dev);
  749. const struct dsps_musb_wrapper *wrp = glue->wrp;
  750. struct musb *musb = platform_get_drvdata(glue->musb);
  751. void __iomem *mbase;
  752. if (!musb)
  753. return 0;
  754. mbase = musb->ctrl_base;
  755. musb_writel(mbase, wrp->control, glue->context.control);
  756. musb_writel(mbase, wrp->epintr_set, glue->context.epintr);
  757. musb_writel(mbase, wrp->coreintr_set, glue->context.coreintr);
  758. musb_writel(mbase, wrp->phy_utmi, glue->context.phy_utmi);
  759. musb_writel(mbase, wrp->mode, glue->context.mode);
  760. musb_writel(mbase, wrp->tx_mode, glue->context.tx_mode);
  761. musb_writel(mbase, wrp->rx_mode, glue->context.rx_mode);
  762. if (musb->xceiv->otg->state == OTG_STATE_B_IDLE &&
  763. musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE)
  764. mod_timer(&glue->timer, jiffies +
  765. msecs_to_jiffies(wrp->poll_timeout));
  766. return 0;
  767. }
  768. #endif
  769. static SIMPLE_DEV_PM_OPS(dsps_pm_ops, dsps_suspend, dsps_resume);
  770. static struct platform_driver dsps_usbss_driver = {
  771. .probe = dsps_probe,
  772. .remove = dsps_remove,
  773. .driver = {
  774. .name = "musb-dsps",
  775. .pm = &dsps_pm_ops,
  776. .of_match_table = musb_dsps_of_match,
  777. },
  778. };
  779. MODULE_DESCRIPTION("TI DSPS MUSB Glue Layer");
  780. MODULE_AUTHOR("Ravi B <ravibabu@ti.com>");
  781. MODULE_AUTHOR("Ajay Kumar Gupta <ajay.gupta@ti.com>");
  782. MODULE_LICENSE("GPL v2");
  783. module_platform_driver(dsps_usbss_driver);