musb_dsps.c 26 KB

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