musb_dsps.c 26 KB

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