musb_dsps.c 26 KB

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