musb_dsps.c 27 KB

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