musb_dsps.c 24 KB

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