musb_dsps.c 25 KB

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