musb_dsps.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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. musb->phy = devm_phy_get(dev->parent, "usb2-phy");
  404. /* Returns zero if e.g. not clocked */
  405. rev = dsps_readl(reg_base, wrp->revision);
  406. if (!rev)
  407. return -ENODEV;
  408. usb_phy_init(musb->xceiv);
  409. if (IS_ERR(musb->phy)) {
  410. musb->phy = NULL;
  411. } else {
  412. ret = phy_init(musb->phy);
  413. if (ret < 0)
  414. return ret;
  415. ret = phy_power_on(musb->phy);
  416. if (ret) {
  417. phy_exit(musb->phy);
  418. return ret;
  419. }
  420. }
  421. setup_timer(&glue->timer, otg_timer, (unsigned long) musb);
  422. /* Reset the musb */
  423. dsps_writel(reg_base, wrp->control, (1 << wrp->reset));
  424. musb->isr = dsps_interrupt;
  425. /* reset the otgdisable bit, needed for host mode to work */
  426. val = dsps_readl(reg_base, wrp->phy_utmi);
  427. val &= ~(1 << wrp->otg_disable);
  428. dsps_writel(musb->ctrl_base, wrp->phy_utmi, val);
  429. /*
  430. * Check whether the dsps version has babble control enabled.
  431. * In latest silicon revision the babble control logic is enabled.
  432. * If MUSB_BABBLE_CTL returns 0x4 then we have the babble control
  433. * logic enabled.
  434. */
  435. val = dsps_readb(musb->mregs, MUSB_BABBLE_CTL);
  436. if (val == MUSB_BABBLE_RCV_DISABLE) {
  437. glue->sw_babble_enabled = true;
  438. val |= MUSB_BABBLE_SW_SESSION_CTRL;
  439. dsps_writeb(musb->mregs, MUSB_BABBLE_CTL, val);
  440. }
  441. ret = dsps_musb_dbg_init(musb, glue);
  442. if (ret)
  443. return ret;
  444. return 0;
  445. }
  446. static int dsps_musb_exit(struct musb *musb)
  447. {
  448. struct device *dev = musb->controller;
  449. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  450. del_timer_sync(&glue->timer);
  451. usb_phy_shutdown(musb->xceiv);
  452. phy_power_off(musb->phy);
  453. phy_exit(musb->phy);
  454. debugfs_remove_recursive(glue->dbgfs_root);
  455. return 0;
  456. }
  457. static int dsps_musb_set_mode(struct musb *musb, u8 mode)
  458. {
  459. struct device *dev = musb->controller;
  460. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  461. const struct dsps_musb_wrapper *wrp = glue->wrp;
  462. void __iomem *ctrl_base = musb->ctrl_base;
  463. u32 reg;
  464. reg = dsps_readl(ctrl_base, wrp->mode);
  465. switch (mode) {
  466. case MUSB_HOST:
  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. dsps_writel(ctrl_base, wrp->phy_utmi, 0x02);
  476. break;
  477. case MUSB_PERIPHERAL:
  478. reg |= (1 << wrp->iddig);
  479. /*
  480. * if we're setting mode to host-only or device-only, we're
  481. * going to ignore whatever the PHY sends us and just force
  482. * ID pin status by SW
  483. */
  484. reg |= (1 << wrp->iddig_mux);
  485. dsps_writel(ctrl_base, wrp->mode, reg);
  486. break;
  487. case MUSB_OTG:
  488. dsps_writel(ctrl_base, wrp->phy_utmi, 0x02);
  489. break;
  490. default:
  491. dev_err(glue->dev, "unsupported mode %d\n", mode);
  492. return -EINVAL;
  493. }
  494. return 0;
  495. }
  496. static bool sw_babble_control(struct musb *musb)
  497. {
  498. u8 babble_ctl;
  499. bool session_restart = false;
  500. babble_ctl = dsps_readb(musb->mregs, MUSB_BABBLE_CTL);
  501. dev_dbg(musb->controller, "babble: MUSB_BABBLE_CTL value %x\n",
  502. babble_ctl);
  503. /*
  504. * check line monitor flag to check whether babble is
  505. * due to noise
  506. */
  507. dev_dbg(musb->controller, "STUCK_J is %s\n",
  508. babble_ctl & MUSB_BABBLE_STUCK_J ? "set" : "reset");
  509. if (babble_ctl & MUSB_BABBLE_STUCK_J) {
  510. int timeout = 10;
  511. /*
  512. * babble is due to noise, then set transmit idle (d7 bit)
  513. * to resume normal operation
  514. */
  515. babble_ctl = dsps_readb(musb->mregs, MUSB_BABBLE_CTL);
  516. babble_ctl |= MUSB_BABBLE_FORCE_TXIDLE;
  517. dsps_writeb(musb->mregs, MUSB_BABBLE_CTL, babble_ctl);
  518. /* wait till line monitor flag cleared */
  519. dev_dbg(musb->controller, "Set TXIDLE, wait J to clear\n");
  520. do {
  521. babble_ctl = dsps_readb(musb->mregs, MUSB_BABBLE_CTL);
  522. udelay(1);
  523. } while ((babble_ctl & MUSB_BABBLE_STUCK_J) && timeout--);
  524. /* check whether stuck_at_j bit cleared */
  525. if (babble_ctl & MUSB_BABBLE_STUCK_J) {
  526. /*
  527. * real babble condition has occurred
  528. * restart the controller to start the
  529. * session again
  530. */
  531. dev_dbg(musb->controller, "J not cleared, misc (%x)\n",
  532. babble_ctl);
  533. session_restart = true;
  534. }
  535. } else {
  536. session_restart = true;
  537. }
  538. return session_restart;
  539. }
  540. static int dsps_musb_reset(struct musb *musb)
  541. {
  542. struct device *dev = musb->controller;
  543. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  544. const struct dsps_musb_wrapper *wrp = glue->wrp;
  545. int session_restart = 0, error;
  546. if (glue->sw_babble_enabled)
  547. session_restart = sw_babble_control(musb);
  548. /*
  549. * In case of new silicon version babble condition can be recovered
  550. * without resetting the MUSB. But for older silicon versions, MUSB
  551. * reset is needed
  552. */
  553. if (session_restart || !glue->sw_babble_enabled) {
  554. dev_info(musb->controller, "Restarting MUSB to recover from Babble\n");
  555. dsps_writel(musb->ctrl_base, wrp->control, (1 << wrp->reset));
  556. usleep_range(100, 200);
  557. usb_phy_shutdown(musb->xceiv);
  558. error = phy_power_off(musb->phy);
  559. if (error)
  560. dev_err(dev, "phy shutdown failed: %i\n", error);
  561. usleep_range(100, 200);
  562. usb_phy_init(musb->xceiv);
  563. error = phy_power_on(musb->phy);
  564. if (error)
  565. dev_err(dev, "phy powerup failed: %i\n", error);
  566. session_restart = 1;
  567. }
  568. return !session_restart;
  569. }
  570. static struct musb_platform_ops dsps_ops = {
  571. .quirks = MUSB_INDEXED_EP,
  572. .init = dsps_musb_init,
  573. .exit = dsps_musb_exit,
  574. .enable = dsps_musb_enable,
  575. .disable = dsps_musb_disable,
  576. .try_idle = dsps_musb_try_idle,
  577. .set_mode = dsps_musb_set_mode,
  578. .reset = dsps_musb_reset,
  579. };
  580. static u64 musb_dmamask = DMA_BIT_MASK(32);
  581. static int get_int_prop(struct device_node *dn, const char *s)
  582. {
  583. int ret;
  584. u32 val;
  585. ret = of_property_read_u32(dn, s, &val);
  586. if (ret)
  587. return 0;
  588. return val;
  589. }
  590. static int get_musb_port_mode(struct device *dev)
  591. {
  592. enum usb_dr_mode mode;
  593. mode = of_usb_get_dr_mode(dev->of_node);
  594. switch (mode) {
  595. case USB_DR_MODE_HOST:
  596. return MUSB_PORT_MODE_HOST;
  597. case USB_DR_MODE_PERIPHERAL:
  598. return MUSB_PORT_MODE_GADGET;
  599. case USB_DR_MODE_UNKNOWN:
  600. case USB_DR_MODE_OTG:
  601. default:
  602. return MUSB_PORT_MODE_DUAL_ROLE;
  603. }
  604. }
  605. static int dsps_create_musb_pdev(struct dsps_glue *glue,
  606. struct platform_device *parent)
  607. {
  608. struct musb_hdrc_platform_data pdata;
  609. struct resource resources[2];
  610. struct resource *res;
  611. struct device *dev = &parent->dev;
  612. struct musb_hdrc_config *config;
  613. struct platform_device *musb;
  614. struct device_node *dn = parent->dev.of_node;
  615. int ret, val;
  616. memset(resources, 0, sizeof(resources));
  617. res = platform_get_resource_byname(parent, IORESOURCE_MEM, "mc");
  618. if (!res) {
  619. dev_err(dev, "failed to get memory.\n");
  620. return -EINVAL;
  621. }
  622. resources[0] = *res;
  623. res = platform_get_resource_byname(parent, IORESOURCE_IRQ, "mc");
  624. if (!res) {
  625. dev_err(dev, "failed to get irq.\n");
  626. return -EINVAL;
  627. }
  628. resources[1] = *res;
  629. /* allocate the child platform device */
  630. musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
  631. if (!musb) {
  632. dev_err(dev, "failed to allocate musb device\n");
  633. return -ENOMEM;
  634. }
  635. musb->dev.parent = dev;
  636. musb->dev.dma_mask = &musb_dmamask;
  637. musb->dev.coherent_dma_mask = musb_dmamask;
  638. musb->dev.of_node = of_node_get(dn);
  639. glue->musb = musb;
  640. ret = platform_device_add_resources(musb, resources,
  641. ARRAY_SIZE(resources));
  642. if (ret) {
  643. dev_err(dev, "failed to add resources\n");
  644. goto err;
  645. }
  646. config = devm_kzalloc(&parent->dev, sizeof(*config), GFP_KERNEL);
  647. if (!config) {
  648. ret = -ENOMEM;
  649. goto err;
  650. }
  651. pdata.config = config;
  652. pdata.platform_ops = &dsps_ops;
  653. config->num_eps = get_int_prop(dn, "mentor,num-eps");
  654. config->ram_bits = get_int_prop(dn, "mentor,ram-bits");
  655. config->host_port_deassert_reset_at_resume = 1;
  656. pdata.mode = get_musb_port_mode(dev);
  657. /* DT keeps this entry in mA, musb expects it as per USB spec */
  658. pdata.power = get_int_prop(dn, "mentor,power") / 2;
  659. ret = of_property_read_u32(dn, "mentor,multipoint", &val);
  660. if (!ret && val)
  661. config->multipoint = true;
  662. ret = platform_device_add_data(musb, &pdata, sizeof(pdata));
  663. if (ret) {
  664. dev_err(dev, "failed to add platform_data\n");
  665. goto err;
  666. }
  667. ret = platform_device_add(musb);
  668. if (ret) {
  669. dev_err(dev, "failed to register musb device\n");
  670. goto err;
  671. }
  672. return 0;
  673. err:
  674. platform_device_put(musb);
  675. return ret;
  676. }
  677. static int dsps_probe(struct platform_device *pdev)
  678. {
  679. const struct of_device_id *match;
  680. const struct dsps_musb_wrapper *wrp;
  681. struct dsps_glue *glue;
  682. int ret;
  683. if (!strcmp(pdev->name, "musb-hdrc"))
  684. return -ENODEV;
  685. match = of_match_node(musb_dsps_of_match, pdev->dev.of_node);
  686. if (!match) {
  687. dev_err(&pdev->dev, "fail to get matching of_match struct\n");
  688. return -EINVAL;
  689. }
  690. wrp = match->data;
  691. /* allocate glue */
  692. glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
  693. if (!glue)
  694. return -ENOMEM;
  695. glue->dev = &pdev->dev;
  696. glue->wrp = wrp;
  697. platform_set_drvdata(pdev, glue);
  698. pm_runtime_enable(&pdev->dev);
  699. ret = pm_runtime_get_sync(&pdev->dev);
  700. if (ret < 0) {
  701. dev_err(&pdev->dev, "pm_runtime_get_sync FAILED");
  702. goto err2;
  703. }
  704. ret = dsps_create_musb_pdev(glue, pdev);
  705. if (ret)
  706. goto err3;
  707. return 0;
  708. err3:
  709. pm_runtime_put(&pdev->dev);
  710. err2:
  711. pm_runtime_disable(&pdev->dev);
  712. return ret;
  713. }
  714. static int dsps_remove(struct platform_device *pdev)
  715. {
  716. struct dsps_glue *glue = platform_get_drvdata(pdev);
  717. platform_device_unregister(glue->musb);
  718. /* disable usbss clocks */
  719. pm_runtime_put(&pdev->dev);
  720. pm_runtime_disable(&pdev->dev);
  721. return 0;
  722. }
  723. static const struct dsps_musb_wrapper am33xx_driver_data = {
  724. .revision = 0x00,
  725. .control = 0x14,
  726. .status = 0x18,
  727. .epintr_set = 0x38,
  728. .epintr_clear = 0x40,
  729. .epintr_status = 0x30,
  730. .coreintr_set = 0x3c,
  731. .coreintr_clear = 0x44,
  732. .coreintr_status = 0x34,
  733. .phy_utmi = 0xe0,
  734. .mode = 0xe8,
  735. .tx_mode = 0x70,
  736. .rx_mode = 0x74,
  737. .reset = 0,
  738. .otg_disable = 21,
  739. .iddig = 8,
  740. .iddig_mux = 7,
  741. .usb_shift = 0,
  742. .usb_mask = 0x1ff,
  743. .usb_bitmap = (0x1ff << 0),
  744. .drvvbus = 8,
  745. .txep_shift = 0,
  746. .txep_mask = 0xffff,
  747. .txep_bitmap = (0xffff << 0),
  748. .rxep_shift = 16,
  749. .rxep_mask = 0xfffe,
  750. .rxep_bitmap = (0xfffe << 16),
  751. .poll_seconds = 2,
  752. };
  753. static const struct of_device_id musb_dsps_of_match[] = {
  754. { .compatible = "ti,musb-am33xx",
  755. .data = (void *) &am33xx_driver_data, },
  756. { },
  757. };
  758. MODULE_DEVICE_TABLE(of, musb_dsps_of_match);
  759. #ifdef CONFIG_PM_SLEEP
  760. static int dsps_suspend(struct device *dev)
  761. {
  762. struct dsps_glue *glue = dev_get_drvdata(dev);
  763. const struct dsps_musb_wrapper *wrp = glue->wrp;
  764. struct musb *musb = platform_get_drvdata(glue->musb);
  765. void __iomem *mbase;
  766. del_timer_sync(&glue->timer);
  767. if (!musb)
  768. /* This can happen if the musb device is in -EPROBE_DEFER */
  769. return 0;
  770. mbase = musb->ctrl_base;
  771. glue->context.control = dsps_readl(mbase, wrp->control);
  772. glue->context.epintr = dsps_readl(mbase, wrp->epintr_set);
  773. glue->context.coreintr = dsps_readl(mbase, wrp->coreintr_set);
  774. glue->context.phy_utmi = dsps_readl(mbase, wrp->phy_utmi);
  775. glue->context.mode = dsps_readl(mbase, wrp->mode);
  776. glue->context.tx_mode = dsps_readl(mbase, wrp->tx_mode);
  777. glue->context.rx_mode = dsps_readl(mbase, wrp->rx_mode);
  778. return 0;
  779. }
  780. static int dsps_resume(struct device *dev)
  781. {
  782. struct dsps_glue *glue = dev_get_drvdata(dev);
  783. const struct dsps_musb_wrapper *wrp = glue->wrp;
  784. struct musb *musb = platform_get_drvdata(glue->musb);
  785. void __iomem *mbase;
  786. if (!musb)
  787. return 0;
  788. mbase = musb->ctrl_base;
  789. dsps_writel(mbase, wrp->control, glue->context.control);
  790. dsps_writel(mbase, wrp->epintr_set, glue->context.epintr);
  791. dsps_writel(mbase, wrp->coreintr_set, glue->context.coreintr);
  792. dsps_writel(mbase, wrp->phy_utmi, glue->context.phy_utmi);
  793. dsps_writel(mbase, wrp->mode, glue->context.mode);
  794. dsps_writel(mbase, wrp->tx_mode, glue->context.tx_mode);
  795. dsps_writel(mbase, wrp->rx_mode, glue->context.rx_mode);
  796. if (musb->xceiv->otg->state == OTG_STATE_B_IDLE &&
  797. musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE)
  798. mod_timer(&glue->timer, jiffies + wrp->poll_seconds * HZ);
  799. return 0;
  800. }
  801. #endif
  802. static SIMPLE_DEV_PM_OPS(dsps_pm_ops, dsps_suspend, dsps_resume);
  803. static struct platform_driver dsps_usbss_driver = {
  804. .probe = dsps_probe,
  805. .remove = dsps_remove,
  806. .driver = {
  807. .name = "musb-dsps",
  808. .pm = &dsps_pm_ops,
  809. .of_match_table = musb_dsps_of_match,
  810. },
  811. };
  812. MODULE_DESCRIPTION("TI DSPS MUSB Glue Layer");
  813. MODULE_AUTHOR("Ravi B <ravibabu@ti.com>");
  814. MODULE_AUTHOR("Ajay Kumar Gupta <ajay.gupta@ti.com>");
  815. MODULE_LICENSE("GPL v2");
  816. module_platform_driver(dsps_usbss_driver);