musb_dsps.c 24 KB

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