musb_dsps.c 24 KB

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