musb_dsps.c 26 KB

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