musb_dsps.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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_PORT_MODE_DUAL_ROLE)
  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_PORT_MODE_HOST) {
  203. dsps_mod_timer_optional(glue);
  204. break;
  205. }
  206. musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
  207. skip_session = 1;
  208. /* fall */
  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->default_a = 1;
  315. musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
  316. dsps_mod_timer_optional(glue);
  317. } else {
  318. musb->is_active = 0;
  319. MUSB_DEV_MODE(musb);
  320. musb->xceiv->otg->default_a = 0;
  321. musb->xceiv->otg->state = OTG_STATE_B_IDLE;
  322. }
  323. /* NOTE: this must complete power-on within 100 ms. */
  324. dev_dbg(musb->controller, "VBUS %s (%s)%s, devctl %02x\n",
  325. drvvbus ? "on" : "off",
  326. usb_otg_state_string(musb->xceiv->otg->state),
  327. err ? " ERROR" : "",
  328. devctl);
  329. ret = IRQ_HANDLED;
  330. }
  331. if (musb->int_tx || musb->int_rx || musb->int_usb)
  332. ret |= musb_interrupt(musb);
  333. /* Poll for ID change and connect */
  334. switch (musb->xceiv->otg->state) {
  335. case OTG_STATE_B_IDLE:
  336. case OTG_STATE_A_WAIT_BCON:
  337. dsps_mod_timer_optional(glue);
  338. break;
  339. default:
  340. break;
  341. }
  342. out:
  343. spin_unlock_irqrestore(&musb->lock, flags);
  344. return ret;
  345. }
  346. static int dsps_musb_dbg_init(struct musb *musb, struct dsps_glue *glue)
  347. {
  348. struct dentry *root;
  349. struct dentry *file;
  350. char buf[128];
  351. sprintf(buf, "%s.dsps", dev_name(musb->controller));
  352. root = debugfs_create_dir(buf, NULL);
  353. if (!root)
  354. return -ENOMEM;
  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. file = debugfs_create_regset32("regdump", S_IRUGO, root, &glue->regset);
  360. if (!file) {
  361. debugfs_remove_recursive(root);
  362. return -ENOMEM;
  363. }
  364. return 0;
  365. }
  366. static int dsps_musb_init(struct musb *musb)
  367. {
  368. struct device *dev = musb->controller;
  369. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  370. struct platform_device *parent = to_platform_device(dev->parent);
  371. const struct dsps_musb_wrapper *wrp = glue->wrp;
  372. void __iomem *reg_base;
  373. struct resource *r;
  374. u32 rev, val;
  375. int ret;
  376. r = platform_get_resource_byname(parent, IORESOURCE_MEM, "control");
  377. reg_base = devm_ioremap_resource(dev, r);
  378. if (IS_ERR(reg_base))
  379. return PTR_ERR(reg_base);
  380. musb->ctrl_base = reg_base;
  381. /* NOP driver needs change if supporting dual instance */
  382. musb->xceiv = devm_usb_get_phy_by_phandle(dev->parent, "phys", 0);
  383. if (IS_ERR(musb->xceiv))
  384. return PTR_ERR(musb->xceiv);
  385. musb->phy = devm_phy_get(dev->parent, "usb2-phy");
  386. /* Returns zero if e.g. not clocked */
  387. rev = musb_readl(reg_base, wrp->revision);
  388. if (!rev)
  389. return -ENODEV;
  390. if (IS_ERR(musb->phy)) {
  391. musb->phy = NULL;
  392. } else {
  393. ret = phy_init(musb->phy);
  394. if (ret < 0)
  395. return ret;
  396. ret = phy_power_on(musb->phy);
  397. if (ret) {
  398. phy_exit(musb->phy);
  399. return ret;
  400. }
  401. }
  402. timer_setup(&musb->dev_timer, otg_timer, 0);
  403. /* Reset the musb */
  404. musb_writel(reg_base, wrp->control, (1 << wrp->reset));
  405. musb->isr = dsps_interrupt;
  406. /* reset the otgdisable bit, needed for host mode to work */
  407. val = musb_readl(reg_base, wrp->phy_utmi);
  408. val &= ~(1 << wrp->otg_disable);
  409. musb_writel(musb->ctrl_base, wrp->phy_utmi, val);
  410. /*
  411. * Check whether the dsps version has babble control enabled.
  412. * In latest silicon revision the babble control logic is enabled.
  413. * If MUSB_BABBLE_CTL returns 0x4 then we have the babble control
  414. * logic enabled.
  415. */
  416. val = musb_readb(musb->mregs, MUSB_BABBLE_CTL);
  417. if (val & MUSB_BABBLE_RCV_DISABLE) {
  418. glue->sw_babble_enabled = true;
  419. val |= MUSB_BABBLE_SW_SESSION_CTRL;
  420. musb_writeb(musb->mregs, MUSB_BABBLE_CTL, val);
  421. }
  422. dsps_mod_timer(glue, -1);
  423. return dsps_musb_dbg_init(musb, glue);
  424. }
  425. static int dsps_musb_exit(struct musb *musb)
  426. {
  427. struct device *dev = musb->controller;
  428. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  429. del_timer_sync(&musb->dev_timer);
  430. phy_power_off(musb->phy);
  431. phy_exit(musb->phy);
  432. debugfs_remove_recursive(glue->dbgfs_root);
  433. return 0;
  434. }
  435. static int dsps_musb_set_mode(struct musb *musb, u8 mode)
  436. {
  437. struct device *dev = musb->controller;
  438. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  439. const struct dsps_musb_wrapper *wrp = glue->wrp;
  440. void __iomem *ctrl_base = musb->ctrl_base;
  441. u32 reg;
  442. reg = musb_readl(ctrl_base, wrp->mode);
  443. switch (mode) {
  444. case MUSB_HOST:
  445. reg &= ~(1 << wrp->iddig);
  446. /*
  447. * if we're setting mode to host-only or device-only, we're
  448. * going to ignore whatever the PHY sends us and just force
  449. * ID pin status by SW
  450. */
  451. reg |= (1 << wrp->iddig_mux);
  452. musb_writel(ctrl_base, wrp->mode, reg);
  453. musb_writel(ctrl_base, wrp->phy_utmi, 0x02);
  454. break;
  455. case MUSB_PERIPHERAL:
  456. reg |= (1 << wrp->iddig);
  457. /*
  458. * if we're setting mode to host-only or device-only, we're
  459. * going to ignore whatever the PHY sends us and just force
  460. * ID pin status by SW
  461. */
  462. reg |= (1 << wrp->iddig_mux);
  463. musb_writel(ctrl_base, wrp->mode, reg);
  464. break;
  465. case MUSB_OTG:
  466. musb_writel(ctrl_base, wrp->phy_utmi, 0x02);
  467. break;
  468. default:
  469. dev_err(glue->dev, "unsupported mode %d\n", mode);
  470. return -EINVAL;
  471. }
  472. return 0;
  473. }
  474. static bool dsps_sw_babble_control(struct musb *musb)
  475. {
  476. u8 babble_ctl;
  477. bool session_restart = false;
  478. babble_ctl = musb_readb(musb->mregs, MUSB_BABBLE_CTL);
  479. dev_dbg(musb->controller, "babble: MUSB_BABBLE_CTL value %x\n",
  480. babble_ctl);
  481. /*
  482. * check line monitor flag to check whether babble is
  483. * due to noise
  484. */
  485. dev_dbg(musb->controller, "STUCK_J is %s\n",
  486. babble_ctl & MUSB_BABBLE_STUCK_J ? "set" : "reset");
  487. if (babble_ctl & MUSB_BABBLE_STUCK_J) {
  488. int timeout = 10;
  489. /*
  490. * babble is due to noise, then set transmit idle (d7 bit)
  491. * to resume normal operation
  492. */
  493. babble_ctl = musb_readb(musb->mregs, MUSB_BABBLE_CTL);
  494. babble_ctl |= MUSB_BABBLE_FORCE_TXIDLE;
  495. musb_writeb(musb->mregs, MUSB_BABBLE_CTL, babble_ctl);
  496. /* wait till line monitor flag cleared */
  497. dev_dbg(musb->controller, "Set TXIDLE, wait J to clear\n");
  498. do {
  499. babble_ctl = musb_readb(musb->mregs, MUSB_BABBLE_CTL);
  500. udelay(1);
  501. } while ((babble_ctl & MUSB_BABBLE_STUCK_J) && timeout--);
  502. /* check whether stuck_at_j bit cleared */
  503. if (babble_ctl & MUSB_BABBLE_STUCK_J) {
  504. /*
  505. * real babble condition has occurred
  506. * restart the controller to start the
  507. * session again
  508. */
  509. dev_dbg(musb->controller, "J not cleared, misc (%x)\n",
  510. babble_ctl);
  511. session_restart = true;
  512. }
  513. } else {
  514. session_restart = true;
  515. }
  516. return session_restart;
  517. }
  518. static int dsps_musb_recover(struct musb *musb)
  519. {
  520. struct device *dev = musb->controller;
  521. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  522. int session_restart = 0;
  523. if (glue->sw_babble_enabled)
  524. session_restart = dsps_sw_babble_control(musb);
  525. else
  526. session_restart = 1;
  527. return session_restart ? 0 : -EPIPE;
  528. }
  529. /* Similar to am35x, dm81xx support only 32-bit read operation */
  530. static void dsps_read_fifo32(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
  531. {
  532. void __iomem *fifo = hw_ep->fifo;
  533. if (len >= 4) {
  534. ioread32_rep(fifo, dst, len >> 2);
  535. dst += len & ~0x03;
  536. len &= 0x03;
  537. }
  538. /* Read any remaining 1 to 3 bytes */
  539. if (len > 0) {
  540. u32 val = musb_readl(fifo, 0);
  541. memcpy(dst, &val, len);
  542. }
  543. }
  544. #ifdef CONFIG_USB_TI_CPPI41_DMA
  545. static void dsps_dma_controller_callback(struct dma_controller *c)
  546. {
  547. struct musb *musb = c->musb;
  548. struct dsps_glue *glue = dev_get_drvdata(musb->controller->parent);
  549. void __iomem *usbss_base = glue->usbss_base;
  550. u32 status;
  551. status = musb_readl(usbss_base, USBSS_IRQ_STATUS);
  552. if (status & USBSS_IRQ_PD_COMP)
  553. musb_writel(usbss_base, USBSS_IRQ_STATUS, USBSS_IRQ_PD_COMP);
  554. }
  555. static struct dma_controller *
  556. dsps_dma_controller_create(struct musb *musb, void __iomem *base)
  557. {
  558. struct dma_controller *controller;
  559. struct dsps_glue *glue = dev_get_drvdata(musb->controller->parent);
  560. void __iomem *usbss_base = glue->usbss_base;
  561. controller = cppi41_dma_controller_create(musb, base);
  562. if (IS_ERR_OR_NULL(controller))
  563. return controller;
  564. musb_writel(usbss_base, USBSS_IRQ_ENABLER, USBSS_IRQ_PD_COMP);
  565. controller->dma_callback = dsps_dma_controller_callback;
  566. return controller;
  567. }
  568. static void dsps_dma_controller_destroy(struct dma_controller *c)
  569. {
  570. struct musb *musb = c->musb;
  571. struct dsps_glue *glue = dev_get_drvdata(musb->controller->parent);
  572. void __iomem *usbss_base = glue->usbss_base;
  573. musb_writel(usbss_base, USBSS_IRQ_CLEARR, USBSS_IRQ_PD_COMP);
  574. cppi41_dma_controller_destroy(c);
  575. }
  576. #ifdef CONFIG_PM_SLEEP
  577. static void dsps_dma_controller_suspend(struct dsps_glue *glue)
  578. {
  579. void __iomem *usbss_base = glue->usbss_base;
  580. musb_writel(usbss_base, USBSS_IRQ_CLEARR, USBSS_IRQ_PD_COMP);
  581. }
  582. static void dsps_dma_controller_resume(struct dsps_glue *glue)
  583. {
  584. void __iomem *usbss_base = glue->usbss_base;
  585. musb_writel(usbss_base, USBSS_IRQ_ENABLER, USBSS_IRQ_PD_COMP);
  586. }
  587. #endif
  588. #else /* CONFIG_USB_TI_CPPI41_DMA */
  589. #ifdef CONFIG_PM_SLEEP
  590. static void dsps_dma_controller_suspend(struct dsps_glue *glue) {}
  591. static void dsps_dma_controller_resume(struct dsps_glue *glue) {}
  592. #endif
  593. #endif /* CONFIG_USB_TI_CPPI41_DMA */
  594. static struct musb_platform_ops dsps_ops = {
  595. .quirks = MUSB_DMA_CPPI41 | MUSB_INDEXED_EP,
  596. .init = dsps_musb_init,
  597. .exit = dsps_musb_exit,
  598. #ifdef CONFIG_USB_TI_CPPI41_DMA
  599. .dma_init = dsps_dma_controller_create,
  600. .dma_exit = dsps_dma_controller_destroy,
  601. #endif
  602. .enable = dsps_musb_enable,
  603. .disable = dsps_musb_disable,
  604. .set_mode = dsps_musb_set_mode,
  605. .recover = dsps_musb_recover,
  606. .clear_ep_rxintr = dsps_musb_clear_ep_rxintr,
  607. };
  608. static u64 musb_dmamask = DMA_BIT_MASK(32);
  609. static int get_int_prop(struct device_node *dn, const char *s)
  610. {
  611. int ret;
  612. u32 val;
  613. ret = of_property_read_u32(dn, s, &val);
  614. if (ret)
  615. return 0;
  616. return val;
  617. }
  618. static int get_musb_port_mode(struct device *dev)
  619. {
  620. enum usb_dr_mode mode;
  621. mode = usb_get_dr_mode(dev);
  622. switch (mode) {
  623. case USB_DR_MODE_HOST:
  624. return MUSB_PORT_MODE_HOST;
  625. case USB_DR_MODE_PERIPHERAL:
  626. return MUSB_PORT_MODE_GADGET;
  627. case USB_DR_MODE_UNKNOWN:
  628. case USB_DR_MODE_OTG:
  629. default:
  630. return MUSB_PORT_MODE_DUAL_ROLE;
  631. }
  632. }
  633. static int dsps_create_musb_pdev(struct dsps_glue *glue,
  634. struct platform_device *parent)
  635. {
  636. struct musb_hdrc_platform_data pdata;
  637. struct resource resources[2];
  638. struct resource *res;
  639. struct device *dev = &parent->dev;
  640. struct musb_hdrc_config *config;
  641. struct platform_device *musb;
  642. struct device_node *dn = parent->dev.of_node;
  643. int ret, val;
  644. memset(resources, 0, sizeof(resources));
  645. res = platform_get_resource_byname(parent, IORESOURCE_MEM, "mc");
  646. if (!res) {
  647. dev_err(dev, "failed to get memory.\n");
  648. return -EINVAL;
  649. }
  650. resources[0] = *res;
  651. res = platform_get_resource_byname(parent, IORESOURCE_IRQ, "mc");
  652. if (!res) {
  653. dev_err(dev, "failed to get irq.\n");
  654. return -EINVAL;
  655. }
  656. resources[1] = *res;
  657. /* allocate the child platform device */
  658. musb = platform_device_alloc("musb-hdrc",
  659. (resources[0].start & 0xFFF) == 0x400 ? 0 : 1);
  660. if (!musb) {
  661. dev_err(dev, "failed to allocate musb device\n");
  662. return -ENOMEM;
  663. }
  664. musb->dev.parent = dev;
  665. musb->dev.dma_mask = &musb_dmamask;
  666. musb->dev.coherent_dma_mask = musb_dmamask;
  667. glue->musb = musb;
  668. ret = platform_device_add_resources(musb, resources,
  669. ARRAY_SIZE(resources));
  670. if (ret) {
  671. dev_err(dev, "failed to add resources\n");
  672. goto err;
  673. }
  674. config = devm_kzalloc(&parent->dev, sizeof(*config), GFP_KERNEL);
  675. if (!config) {
  676. ret = -ENOMEM;
  677. goto err;
  678. }
  679. pdata.config = config;
  680. pdata.platform_ops = &dsps_ops;
  681. config->num_eps = get_int_prop(dn, "mentor,num-eps");
  682. config->ram_bits = get_int_prop(dn, "mentor,ram-bits");
  683. config->host_port_deassert_reset_at_resume = 1;
  684. pdata.mode = get_musb_port_mode(dev);
  685. /* DT keeps this entry in mA, musb expects it as per USB spec */
  686. pdata.power = get_int_prop(dn, "mentor,power") / 2;
  687. ret = of_property_read_u32(dn, "mentor,multipoint", &val);
  688. if (!ret && val)
  689. config->multipoint = true;
  690. config->maximum_speed = usb_get_maximum_speed(&parent->dev);
  691. switch (config->maximum_speed) {
  692. case USB_SPEED_LOW:
  693. case USB_SPEED_FULL:
  694. break;
  695. case USB_SPEED_SUPER:
  696. dev_warn(dev, "ignore incorrect maximum_speed "
  697. "(super-speed) setting in dts");
  698. /* fall through */
  699. default:
  700. config->maximum_speed = USB_SPEED_HIGH;
  701. }
  702. ret = platform_device_add_data(musb, &pdata, sizeof(pdata));
  703. if (ret) {
  704. dev_err(dev, "failed to add platform_data\n");
  705. goto err;
  706. }
  707. ret = platform_device_add(musb);
  708. if (ret) {
  709. dev_err(dev, "failed to register musb device\n");
  710. goto err;
  711. }
  712. return 0;
  713. err:
  714. platform_device_put(musb);
  715. return ret;
  716. }
  717. static irqreturn_t dsps_vbus_threaded_irq(int irq, void *priv)
  718. {
  719. struct dsps_glue *glue = priv;
  720. struct musb *musb = platform_get_drvdata(glue->musb);
  721. if (!musb)
  722. return IRQ_NONE;
  723. dev_dbg(glue->dev, "VBUS interrupt\n");
  724. dsps_mod_timer(glue, 0);
  725. return IRQ_HANDLED;
  726. }
  727. static int dsps_setup_optional_vbus_irq(struct platform_device *pdev,
  728. struct dsps_glue *glue)
  729. {
  730. int error;
  731. glue->vbus_irq = platform_get_irq_byname(pdev, "vbus");
  732. if (glue->vbus_irq == -EPROBE_DEFER)
  733. return -EPROBE_DEFER;
  734. if (glue->vbus_irq <= 0) {
  735. glue->vbus_irq = 0;
  736. return 0;
  737. }
  738. error = devm_request_threaded_irq(glue->dev, glue->vbus_irq,
  739. NULL, dsps_vbus_threaded_irq,
  740. IRQF_ONESHOT,
  741. "vbus", glue);
  742. if (error) {
  743. glue->vbus_irq = 0;
  744. return error;
  745. }
  746. dev_dbg(glue->dev, "VBUS irq %i configured\n", glue->vbus_irq);
  747. return 0;
  748. }
  749. static int dsps_probe(struct platform_device *pdev)
  750. {
  751. const struct of_device_id *match;
  752. const struct dsps_musb_wrapper *wrp;
  753. struct dsps_glue *glue;
  754. int ret;
  755. if (!strcmp(pdev->name, "musb-hdrc"))
  756. return -ENODEV;
  757. match = of_match_node(musb_dsps_of_match, pdev->dev.of_node);
  758. if (!match) {
  759. dev_err(&pdev->dev, "fail to get matching of_match struct\n");
  760. return -EINVAL;
  761. }
  762. wrp = match->data;
  763. if (of_device_is_compatible(pdev->dev.of_node, "ti,musb-dm816"))
  764. dsps_ops.read_fifo = dsps_read_fifo32;
  765. /* allocate glue */
  766. glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
  767. if (!glue)
  768. return -ENOMEM;
  769. glue->dev = &pdev->dev;
  770. glue->wrp = wrp;
  771. glue->usbss_base = of_iomap(pdev->dev.parent->of_node, 0);
  772. if (!glue->usbss_base)
  773. return -ENXIO;
  774. if (usb_get_dr_mode(&pdev->dev) == USB_DR_MODE_PERIPHERAL) {
  775. ret = dsps_setup_optional_vbus_irq(pdev, glue);
  776. if (ret)
  777. goto err_iounmap;
  778. }
  779. platform_set_drvdata(pdev, glue);
  780. pm_runtime_enable(&pdev->dev);
  781. ret = dsps_create_musb_pdev(glue, pdev);
  782. if (ret)
  783. goto err;
  784. return 0;
  785. err:
  786. pm_runtime_disable(&pdev->dev);
  787. err_iounmap:
  788. iounmap(glue->usbss_base);
  789. return ret;
  790. }
  791. static int dsps_remove(struct platform_device *pdev)
  792. {
  793. struct dsps_glue *glue = platform_get_drvdata(pdev);
  794. platform_device_unregister(glue->musb);
  795. pm_runtime_disable(&pdev->dev);
  796. iounmap(glue->usbss_base);
  797. return 0;
  798. }
  799. static const struct dsps_musb_wrapper am33xx_driver_data = {
  800. .revision = 0x00,
  801. .control = 0x14,
  802. .status = 0x18,
  803. .epintr_set = 0x38,
  804. .epintr_clear = 0x40,
  805. .epintr_status = 0x30,
  806. .coreintr_set = 0x3c,
  807. .coreintr_clear = 0x44,
  808. .coreintr_status = 0x34,
  809. .phy_utmi = 0xe0,
  810. .mode = 0xe8,
  811. .tx_mode = 0x70,
  812. .rx_mode = 0x74,
  813. .reset = 0,
  814. .otg_disable = 21,
  815. .iddig = 8,
  816. .iddig_mux = 7,
  817. .usb_shift = 0,
  818. .usb_mask = 0x1ff,
  819. .usb_bitmap = (0x1ff << 0),
  820. .drvvbus = 8,
  821. .txep_shift = 0,
  822. .txep_mask = 0xffff,
  823. .txep_bitmap = (0xffff << 0),
  824. .rxep_shift = 16,
  825. .rxep_mask = 0xfffe,
  826. .rxep_bitmap = (0xfffe << 16),
  827. .poll_timeout = 2000, /* ms */
  828. };
  829. static const struct of_device_id musb_dsps_of_match[] = {
  830. { .compatible = "ti,musb-am33xx",
  831. .data = &am33xx_driver_data, },
  832. { .compatible = "ti,musb-dm816",
  833. .data = &am33xx_driver_data, },
  834. { },
  835. };
  836. MODULE_DEVICE_TABLE(of, musb_dsps_of_match);
  837. #ifdef CONFIG_PM_SLEEP
  838. static int dsps_suspend(struct device *dev)
  839. {
  840. struct dsps_glue *glue = dev_get_drvdata(dev);
  841. const struct dsps_musb_wrapper *wrp = glue->wrp;
  842. struct musb *musb = platform_get_drvdata(glue->musb);
  843. void __iomem *mbase;
  844. int ret;
  845. if (!musb)
  846. /* This can happen if the musb device is in -EPROBE_DEFER */
  847. return 0;
  848. ret = pm_runtime_get_sync(dev);
  849. if (ret < 0) {
  850. pm_runtime_put_noidle(dev);
  851. return ret;
  852. }
  853. del_timer_sync(&musb->dev_timer);
  854. mbase = musb->ctrl_base;
  855. glue->context.control = musb_readl(mbase, wrp->control);
  856. glue->context.epintr = musb_readl(mbase, wrp->epintr_set);
  857. glue->context.coreintr = musb_readl(mbase, wrp->coreintr_set);
  858. glue->context.phy_utmi = musb_readl(mbase, wrp->phy_utmi);
  859. glue->context.mode = musb_readl(mbase, wrp->mode);
  860. glue->context.tx_mode = musb_readl(mbase, wrp->tx_mode);
  861. glue->context.rx_mode = musb_readl(mbase, wrp->rx_mode);
  862. dsps_dma_controller_suspend(glue);
  863. return 0;
  864. }
  865. static int dsps_resume(struct device *dev)
  866. {
  867. struct dsps_glue *glue = dev_get_drvdata(dev);
  868. const struct dsps_musb_wrapper *wrp = glue->wrp;
  869. struct musb *musb = platform_get_drvdata(glue->musb);
  870. void __iomem *mbase;
  871. if (!musb)
  872. return 0;
  873. dsps_dma_controller_resume(glue);
  874. mbase = musb->ctrl_base;
  875. musb_writel(mbase, wrp->control, glue->context.control);
  876. musb_writel(mbase, wrp->epintr_set, glue->context.epintr);
  877. musb_writel(mbase, wrp->coreintr_set, glue->context.coreintr);
  878. musb_writel(mbase, wrp->phy_utmi, glue->context.phy_utmi);
  879. musb_writel(mbase, wrp->mode, glue->context.mode);
  880. musb_writel(mbase, wrp->tx_mode, glue->context.tx_mode);
  881. musb_writel(mbase, wrp->rx_mode, glue->context.rx_mode);
  882. if (musb->xceiv->otg->state == OTG_STATE_B_IDLE &&
  883. musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE)
  884. dsps_mod_timer(glue, -1);
  885. pm_runtime_put(dev);
  886. return 0;
  887. }
  888. #endif
  889. static SIMPLE_DEV_PM_OPS(dsps_pm_ops, dsps_suspend, dsps_resume);
  890. static struct platform_driver dsps_usbss_driver = {
  891. .probe = dsps_probe,
  892. .remove = dsps_remove,
  893. .driver = {
  894. .name = "musb-dsps",
  895. .pm = &dsps_pm_ops,
  896. .of_match_table = musb_dsps_of_match,
  897. },
  898. };
  899. MODULE_DESCRIPTION("TI DSPS MUSB Glue Layer");
  900. MODULE_AUTHOR("Ravi B <ravibabu@ti.com>");
  901. MODULE_AUTHOR("Ajay Kumar Gupta <ajay.gupta@ti.com>");
  902. MODULE_LICENSE("GPL v2");
  903. module_platform_driver(dsps_usbss_driver);