musb_dsps.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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. * avoid using musb_readx()/musb_writex() as glue layer should not be
  50. * dependent on musb core layer symbols.
  51. */
  52. static inline u8 dsps_readb(const void __iomem *addr, unsigned offset)
  53. { return __raw_readb(addr + offset); }
  54. static inline u32 dsps_readl(const void __iomem *addr, unsigned offset)
  55. { return __raw_readl(addr + offset); }
  56. static inline void dsps_writeb(void __iomem *addr, unsigned offset, u8 data)
  57. { __raw_writeb(data, addr + offset); }
  58. static inline void dsps_writel(void __iomem *addr, unsigned offset, u32 data)
  59. { __raw_writel(data, addr + offset); }
  60. /**
  61. * DSPS musb wrapper register offset.
  62. * FIXME: This should be expanded to have all the wrapper registers from TI DSPS
  63. * musb ips.
  64. */
  65. struct dsps_musb_wrapper {
  66. u16 revision;
  67. u16 control;
  68. u16 status;
  69. u16 epintr_set;
  70. u16 epintr_clear;
  71. u16 epintr_status;
  72. u16 coreintr_set;
  73. u16 coreintr_clear;
  74. u16 coreintr_status;
  75. u16 phy_utmi;
  76. u16 mode;
  77. u16 tx_mode;
  78. u16 rx_mode;
  79. /* bit positions for control */
  80. unsigned reset:5;
  81. /* bit positions for interrupt */
  82. unsigned usb_shift:5;
  83. u32 usb_mask;
  84. u32 usb_bitmap;
  85. unsigned drvvbus:5;
  86. unsigned txep_shift:5;
  87. u32 txep_mask;
  88. u32 txep_bitmap;
  89. unsigned rxep_shift:5;
  90. u32 rxep_mask;
  91. u32 rxep_bitmap;
  92. /* bit positions for phy_utmi */
  93. unsigned otg_disable:5;
  94. /* bit positions for mode */
  95. unsigned iddig:5;
  96. unsigned iddig_mux:5;
  97. /* miscellaneous stuff */
  98. u8 poll_seconds;
  99. };
  100. /*
  101. * register shadow for suspend
  102. */
  103. struct dsps_context {
  104. u32 control;
  105. u32 epintr;
  106. u32 coreintr;
  107. u32 phy_utmi;
  108. u32 mode;
  109. u32 tx_mode;
  110. u32 rx_mode;
  111. };
  112. /**
  113. * DSPS glue structure.
  114. */
  115. struct dsps_glue {
  116. struct device *dev;
  117. struct platform_device *musb; /* child musb pdev */
  118. const struct dsps_musb_wrapper *wrp; /* wrapper register offsets */
  119. struct timer_list timer; /* otg_workaround timer */
  120. unsigned long last_timer; /* last timer data for each instance */
  121. struct dsps_context context;
  122. struct debugfs_regset32 regset;
  123. struct dentry *dbgfs_root;
  124. };
  125. static const struct debugfs_reg32 dsps_musb_regs[] = {
  126. { "revision", 0x00 },
  127. { "control", 0x14 },
  128. { "status", 0x18 },
  129. { "eoi", 0x24 },
  130. { "intr0_stat", 0x30 },
  131. { "intr1_stat", 0x34 },
  132. { "intr0_set", 0x38 },
  133. { "intr1_set", 0x3c },
  134. { "txmode", 0x70 },
  135. { "rxmode", 0x74 },
  136. { "autoreq", 0xd0 },
  137. { "srpfixtime", 0xd4 },
  138. { "tdown", 0xd8 },
  139. { "phy_utmi", 0xe0 },
  140. { "mode", 0xe8 },
  141. };
  142. static void dsps_musb_try_idle(struct musb *musb, unsigned long timeout)
  143. {
  144. struct device *dev = musb->controller;
  145. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  146. if (timeout == 0)
  147. timeout = jiffies + msecs_to_jiffies(3);
  148. /* Never idle if active, or when VBUS timeout is not set as host */
  149. if (musb->is_active || (musb->a_wait_bcon == 0 &&
  150. musb->xceiv->state == OTG_STATE_A_WAIT_BCON)) {
  151. dev_dbg(musb->controller, "%s active, deleting timer\n",
  152. usb_otg_state_string(musb->xceiv->state));
  153. del_timer(&glue->timer);
  154. glue->last_timer = jiffies;
  155. return;
  156. }
  157. if (musb->port_mode != MUSB_PORT_MODE_DUAL_ROLE)
  158. return;
  159. if (!musb->g.dev.driver)
  160. return;
  161. if (time_after(glue->last_timer, timeout) &&
  162. timer_pending(&glue->timer)) {
  163. dev_dbg(musb->controller,
  164. "Longer idle timer already pending, ignoring...\n");
  165. return;
  166. }
  167. glue->last_timer = timeout;
  168. dev_dbg(musb->controller, "%s inactive, starting idle timer for %u ms\n",
  169. usb_otg_state_string(musb->xceiv->state),
  170. jiffies_to_msecs(timeout - jiffies));
  171. mod_timer(&glue->timer, timeout);
  172. }
  173. /**
  174. * dsps_musb_enable - enable interrupts
  175. */
  176. static void dsps_musb_enable(struct musb *musb)
  177. {
  178. struct device *dev = musb->controller;
  179. struct platform_device *pdev = to_platform_device(dev->parent);
  180. struct dsps_glue *glue = platform_get_drvdata(pdev);
  181. const struct dsps_musb_wrapper *wrp = glue->wrp;
  182. void __iomem *reg_base = musb->ctrl_base;
  183. u32 epmask, coremask;
  184. /* Workaround: setup IRQs through both register sets. */
  185. epmask = ((musb->epmask & wrp->txep_mask) << wrp->txep_shift) |
  186. ((musb->epmask & wrp->rxep_mask) << wrp->rxep_shift);
  187. coremask = (wrp->usb_bitmap & ~MUSB_INTR_SOF);
  188. dsps_writel(reg_base, wrp->epintr_set, epmask);
  189. dsps_writel(reg_base, wrp->coreintr_set, coremask);
  190. /* Force the DRVVBUS IRQ so we can start polling for ID change. */
  191. dsps_writel(reg_base, wrp->coreintr_set,
  192. (1 << wrp->drvvbus) << wrp->usb_shift);
  193. dsps_musb_try_idle(musb, 0);
  194. }
  195. /**
  196. * dsps_musb_disable - disable HDRC and flush interrupts
  197. */
  198. static void dsps_musb_disable(struct musb *musb)
  199. {
  200. struct device *dev = musb->controller;
  201. struct platform_device *pdev = to_platform_device(dev->parent);
  202. struct dsps_glue *glue = platform_get_drvdata(pdev);
  203. const struct dsps_musb_wrapper *wrp = glue->wrp;
  204. void __iomem *reg_base = musb->ctrl_base;
  205. dsps_writel(reg_base, wrp->coreintr_clear, wrp->usb_bitmap);
  206. dsps_writel(reg_base, wrp->epintr_clear,
  207. wrp->txep_bitmap | wrp->rxep_bitmap);
  208. dsps_writeb(musb->mregs, MUSB_DEVCTL, 0);
  209. }
  210. static void otg_timer(unsigned long _musb)
  211. {
  212. struct musb *musb = (void *)_musb;
  213. void __iomem *mregs = musb->mregs;
  214. struct device *dev = musb->controller;
  215. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  216. const struct dsps_musb_wrapper *wrp = glue->wrp;
  217. u8 devctl;
  218. unsigned long flags;
  219. int skip_session = 0;
  220. /*
  221. * We poll because DSPS IP's won't expose several OTG-critical
  222. * status change events (from the transceiver) otherwise.
  223. */
  224. devctl = dsps_readb(mregs, MUSB_DEVCTL);
  225. dev_dbg(musb->controller, "Poll devctl %02x (%s)\n", devctl,
  226. usb_otg_state_string(musb->xceiv->state));
  227. spin_lock_irqsave(&musb->lock, flags);
  228. switch (musb->xceiv->state) {
  229. case OTG_STATE_A_WAIT_BCON:
  230. dsps_writeb(musb->mregs, MUSB_DEVCTL, 0);
  231. skip_session = 1;
  232. /* fall */
  233. case OTG_STATE_A_IDLE:
  234. case OTG_STATE_B_IDLE:
  235. if (devctl & MUSB_DEVCTL_BDEVICE) {
  236. musb->xceiv->state = OTG_STATE_B_IDLE;
  237. MUSB_DEV_MODE(musb);
  238. } else {
  239. musb->xceiv->state = OTG_STATE_A_IDLE;
  240. MUSB_HST_MODE(musb);
  241. }
  242. if (!(devctl & MUSB_DEVCTL_SESSION) && !skip_session)
  243. dsps_writeb(mregs, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
  244. mod_timer(&glue->timer, jiffies + wrp->poll_seconds * HZ);
  245. break;
  246. case OTG_STATE_A_WAIT_VFALL:
  247. musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
  248. dsps_writel(musb->ctrl_base, wrp->coreintr_set,
  249. MUSB_INTR_VBUSERROR << wrp->usb_shift);
  250. break;
  251. default:
  252. break;
  253. }
  254. spin_unlock_irqrestore(&musb->lock, flags);
  255. }
  256. static irqreturn_t dsps_interrupt(int irq, void *hci)
  257. {
  258. struct musb *musb = hci;
  259. void __iomem *reg_base = musb->ctrl_base;
  260. struct device *dev = musb->controller;
  261. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  262. const struct dsps_musb_wrapper *wrp = glue->wrp;
  263. unsigned long flags;
  264. irqreturn_t ret = IRQ_NONE;
  265. u32 epintr, usbintr;
  266. spin_lock_irqsave(&musb->lock, flags);
  267. /* Get endpoint interrupts */
  268. epintr = dsps_readl(reg_base, wrp->epintr_status);
  269. musb->int_rx = (epintr & wrp->rxep_bitmap) >> wrp->rxep_shift;
  270. musb->int_tx = (epintr & wrp->txep_bitmap) >> wrp->txep_shift;
  271. if (epintr)
  272. dsps_writel(reg_base, wrp->epintr_status, epintr);
  273. /* Get usb core interrupts */
  274. usbintr = dsps_readl(reg_base, wrp->coreintr_status);
  275. if (!usbintr && !epintr)
  276. goto out;
  277. musb->int_usb = (usbintr & wrp->usb_bitmap) >> wrp->usb_shift;
  278. if (usbintr)
  279. dsps_writel(reg_base, wrp->coreintr_status, usbintr);
  280. dev_dbg(musb->controller, "usbintr (%x) epintr(%x)\n",
  281. usbintr, epintr);
  282. /*
  283. * DRVVBUS IRQs are the only proxy we have (a very poor one!) for
  284. * DSPS IP's missing ID change IRQ. We need an ID change IRQ to
  285. * switch appropriately between halves of the OTG state machine.
  286. * Managing DEVCTL.SESSION per Mentor docs requires that we know its
  287. * value but DEVCTL.BDEVICE is invalid without DEVCTL.SESSION set.
  288. * Also, DRVVBUS pulses for SRP (but not at 5V) ...
  289. */
  290. if (is_host_active(musb) && usbintr & MUSB_INTR_BABBLE) {
  291. pr_info("CAUTION: musb: Babble Interrupt Occurred\n");
  292. /*
  293. * When a babble condition occurs, the musb controller removes
  294. * the session and is no longer in host mode. Hence, all
  295. * devices connected to its root hub get disconnected.
  296. *
  297. * Hand this error down to the musb core isr, so it can
  298. * recover.
  299. */
  300. musb->int_usb = MUSB_INTR_BABBLE | MUSB_INTR_DISCONNECT;
  301. musb->int_tx = musb->int_rx = 0;
  302. }
  303. if (usbintr & ((1 << wrp->drvvbus) << wrp->usb_shift)) {
  304. int drvvbus = dsps_readl(reg_base, wrp->status);
  305. void __iomem *mregs = musb->mregs;
  306. u8 devctl = dsps_readb(mregs, MUSB_DEVCTL);
  307. int err;
  308. err = musb->int_usb & MUSB_INTR_VBUSERROR;
  309. if (err) {
  310. /*
  311. * The Mentor core doesn't debounce VBUS as needed
  312. * to cope with device connect current spikes. This
  313. * means it's not uncommon for bus-powered devices
  314. * to get VBUS errors during enumeration.
  315. *
  316. * This is a workaround, but newer RTL from Mentor
  317. * seems to allow a better one: "re"-starting sessions
  318. * without waiting for VBUS to stop registering in
  319. * devctl.
  320. */
  321. musb->int_usb &= ~MUSB_INTR_VBUSERROR;
  322. musb->xceiv->state = OTG_STATE_A_WAIT_VFALL;
  323. mod_timer(&glue->timer,
  324. jiffies + wrp->poll_seconds * HZ);
  325. WARNING("VBUS error workaround (delay coming)\n");
  326. } else if (drvvbus) {
  327. MUSB_HST_MODE(musb);
  328. musb->xceiv->otg->default_a = 1;
  329. musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
  330. del_timer(&glue->timer);
  331. } else {
  332. musb->is_active = 0;
  333. MUSB_DEV_MODE(musb);
  334. musb->xceiv->otg->default_a = 0;
  335. musb->xceiv->state = OTG_STATE_B_IDLE;
  336. }
  337. /* NOTE: this must complete power-on within 100 ms. */
  338. dev_dbg(musb->controller, "VBUS %s (%s)%s, devctl %02x\n",
  339. drvvbus ? "on" : "off",
  340. usb_otg_state_string(musb->xceiv->state),
  341. err ? " ERROR" : "",
  342. devctl);
  343. ret = IRQ_HANDLED;
  344. }
  345. if (musb->int_tx || musb->int_rx || musb->int_usb)
  346. ret |= musb_interrupt(musb);
  347. /* Poll for ID change in OTG port mode */
  348. if (musb->xceiv->state == OTG_STATE_B_IDLE &&
  349. musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE)
  350. mod_timer(&glue->timer, jiffies + wrp->poll_seconds * HZ);
  351. out:
  352. spin_unlock_irqrestore(&musb->lock, flags);
  353. return ret;
  354. }
  355. static int dsps_musb_dbg_init(struct musb *musb, struct dsps_glue *glue)
  356. {
  357. struct dentry *root;
  358. struct dentry *file;
  359. char buf[128];
  360. sprintf(buf, "%s.dsps", dev_name(musb->controller));
  361. root = debugfs_create_dir(buf, NULL);
  362. if (!root)
  363. return -ENOMEM;
  364. glue->dbgfs_root = root;
  365. glue->regset.regs = dsps_musb_regs;
  366. glue->regset.nregs = ARRAY_SIZE(dsps_musb_regs);
  367. glue->regset.base = musb->ctrl_base;
  368. file = debugfs_create_regset32("regdump", S_IRUGO, root, &glue->regset);
  369. if (!file) {
  370. debugfs_remove_recursive(root);
  371. return -ENOMEM;
  372. }
  373. return 0;
  374. }
  375. static int dsps_musb_init(struct musb *musb)
  376. {
  377. struct device *dev = musb->controller;
  378. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  379. struct platform_device *parent = to_platform_device(dev->parent);
  380. const struct dsps_musb_wrapper *wrp = glue->wrp;
  381. void __iomem *reg_base;
  382. struct resource *r;
  383. u32 rev, val;
  384. int ret;
  385. r = platform_get_resource_byname(parent, IORESOURCE_MEM, "control");
  386. if (!r)
  387. return -EINVAL;
  388. reg_base = devm_ioremap_resource(dev, r);
  389. if (IS_ERR(reg_base))
  390. return PTR_ERR(reg_base);
  391. musb->ctrl_base = reg_base;
  392. /* NOP driver needs change if supporting dual instance */
  393. musb->xceiv = devm_usb_get_phy_by_phandle(dev, "phys", 0);
  394. if (IS_ERR(musb->xceiv))
  395. return PTR_ERR(musb->xceiv);
  396. /* Returns zero if e.g. not clocked */
  397. rev = dsps_readl(reg_base, wrp->revision);
  398. if (!rev)
  399. return -ENODEV;
  400. usb_phy_init(musb->xceiv);
  401. setup_timer(&glue->timer, otg_timer, (unsigned long) musb);
  402. /* Reset the musb */
  403. dsps_writel(reg_base, wrp->control, (1 << wrp->reset));
  404. musb->isr = dsps_interrupt;
  405. /* reset the otgdisable bit, needed for host mode to work */
  406. val = dsps_readl(reg_base, wrp->phy_utmi);
  407. val &= ~(1 << wrp->otg_disable);
  408. dsps_writel(musb->ctrl_base, wrp->phy_utmi, val);
  409. ret = dsps_musb_dbg_init(musb, glue);
  410. if (ret)
  411. return ret;
  412. return 0;
  413. }
  414. static int dsps_musb_exit(struct musb *musb)
  415. {
  416. struct device *dev = musb->controller;
  417. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  418. del_timer_sync(&glue->timer);
  419. usb_phy_shutdown(musb->xceiv);
  420. debugfs_remove_recursive(glue->dbgfs_root);
  421. return 0;
  422. }
  423. static int dsps_musb_set_mode(struct musb *musb, u8 mode)
  424. {
  425. struct device *dev = musb->controller;
  426. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  427. const struct dsps_musb_wrapper *wrp = glue->wrp;
  428. void __iomem *ctrl_base = musb->ctrl_base;
  429. u32 reg;
  430. reg = dsps_readl(ctrl_base, wrp->mode);
  431. switch (mode) {
  432. case MUSB_HOST:
  433. reg &= ~(1 << wrp->iddig);
  434. /*
  435. * if we're setting mode to host-only or device-only, we're
  436. * going to ignore whatever the PHY sends us and just force
  437. * ID pin status by SW
  438. */
  439. reg |= (1 << wrp->iddig_mux);
  440. dsps_writel(ctrl_base, wrp->mode, reg);
  441. dsps_writel(ctrl_base, wrp->phy_utmi, 0x02);
  442. break;
  443. case MUSB_PERIPHERAL:
  444. reg |= (1 << wrp->iddig);
  445. /*
  446. * if we're setting mode to host-only or device-only, we're
  447. * going to ignore whatever the PHY sends us and just force
  448. * ID pin status by SW
  449. */
  450. reg |= (1 << wrp->iddig_mux);
  451. dsps_writel(ctrl_base, wrp->mode, reg);
  452. break;
  453. case MUSB_OTG:
  454. dsps_writel(ctrl_base, wrp->phy_utmi, 0x02);
  455. break;
  456. default:
  457. dev_err(glue->dev, "unsupported mode %d\n", mode);
  458. return -EINVAL;
  459. }
  460. return 0;
  461. }
  462. static void dsps_musb_reset(struct musb *musb)
  463. {
  464. struct device *dev = musb->controller;
  465. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  466. const struct dsps_musb_wrapper *wrp = glue->wrp;
  467. dsps_writel(musb->ctrl_base, wrp->control, (1 << wrp->reset));
  468. udelay(100);
  469. }
  470. static struct musb_platform_ops dsps_ops = {
  471. .init = dsps_musb_init,
  472. .exit = dsps_musb_exit,
  473. .enable = dsps_musb_enable,
  474. .disable = dsps_musb_disable,
  475. .try_idle = dsps_musb_try_idle,
  476. .set_mode = dsps_musb_set_mode,
  477. .reset = dsps_musb_reset,
  478. };
  479. static u64 musb_dmamask = DMA_BIT_MASK(32);
  480. static int get_int_prop(struct device_node *dn, const char *s)
  481. {
  482. int ret;
  483. u32 val;
  484. ret = of_property_read_u32(dn, s, &val);
  485. if (ret)
  486. return 0;
  487. return val;
  488. }
  489. static int get_musb_port_mode(struct device *dev)
  490. {
  491. enum usb_dr_mode mode;
  492. mode = of_usb_get_dr_mode(dev->of_node);
  493. switch (mode) {
  494. case USB_DR_MODE_HOST:
  495. return MUSB_PORT_MODE_HOST;
  496. case USB_DR_MODE_PERIPHERAL:
  497. return MUSB_PORT_MODE_GADGET;
  498. case USB_DR_MODE_UNKNOWN:
  499. case USB_DR_MODE_OTG:
  500. default:
  501. return MUSB_PORT_MODE_DUAL_ROLE;
  502. }
  503. }
  504. static int dsps_create_musb_pdev(struct dsps_glue *glue,
  505. struct platform_device *parent)
  506. {
  507. struct musb_hdrc_platform_data pdata;
  508. struct resource resources[2];
  509. struct resource *res;
  510. struct device *dev = &parent->dev;
  511. struct musb_hdrc_config *config;
  512. struct platform_device *musb;
  513. struct device_node *dn = parent->dev.of_node;
  514. int ret;
  515. memset(resources, 0, sizeof(resources));
  516. res = platform_get_resource_byname(parent, IORESOURCE_MEM, "mc");
  517. if (!res) {
  518. dev_err(dev, "failed to get memory.\n");
  519. return -EINVAL;
  520. }
  521. resources[0] = *res;
  522. res = platform_get_resource_byname(parent, IORESOURCE_IRQ, "mc");
  523. if (!res) {
  524. dev_err(dev, "failed to get irq.\n");
  525. return -EINVAL;
  526. }
  527. resources[1] = *res;
  528. /* allocate the child platform device */
  529. musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
  530. if (!musb) {
  531. dev_err(dev, "failed to allocate musb device\n");
  532. return -ENOMEM;
  533. }
  534. musb->dev.parent = dev;
  535. musb->dev.dma_mask = &musb_dmamask;
  536. musb->dev.coherent_dma_mask = musb_dmamask;
  537. musb->dev.of_node = of_node_get(dn);
  538. glue->musb = musb;
  539. ret = platform_device_add_resources(musb, resources,
  540. ARRAY_SIZE(resources));
  541. if (ret) {
  542. dev_err(dev, "failed to add resources\n");
  543. goto err;
  544. }
  545. config = devm_kzalloc(&parent->dev, sizeof(*config), GFP_KERNEL);
  546. if (!config) {
  547. dev_err(dev, "failed to allocate musb hdrc config\n");
  548. ret = -ENOMEM;
  549. goto err;
  550. }
  551. pdata.config = config;
  552. pdata.platform_ops = &dsps_ops;
  553. config->num_eps = get_int_prop(dn, "mentor,num-eps");
  554. config->ram_bits = get_int_prop(dn, "mentor,ram-bits");
  555. config->host_port_deassert_reset_at_resume = 1;
  556. pdata.mode = get_musb_port_mode(dev);
  557. /* DT keeps this entry in mA, musb expects it as per USB spec */
  558. pdata.power = get_int_prop(dn, "mentor,power") / 2;
  559. config->multipoint = of_property_read_bool(dn, "mentor,multipoint");
  560. ret = platform_device_add_data(musb, &pdata, sizeof(pdata));
  561. if (ret) {
  562. dev_err(dev, "failed to add platform_data\n");
  563. goto err;
  564. }
  565. ret = platform_device_add(musb);
  566. if (ret) {
  567. dev_err(dev, "failed to register musb device\n");
  568. goto err;
  569. }
  570. return 0;
  571. err:
  572. platform_device_put(musb);
  573. return ret;
  574. }
  575. static int dsps_probe(struct platform_device *pdev)
  576. {
  577. const struct of_device_id *match;
  578. const struct dsps_musb_wrapper *wrp;
  579. struct dsps_glue *glue;
  580. int ret;
  581. if (!strcmp(pdev->name, "musb-hdrc"))
  582. return -ENODEV;
  583. match = of_match_node(musb_dsps_of_match, pdev->dev.of_node);
  584. if (!match) {
  585. dev_err(&pdev->dev, "fail to get matching of_match struct\n");
  586. return -EINVAL;
  587. }
  588. wrp = match->data;
  589. /* allocate glue */
  590. glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
  591. if (!glue) {
  592. dev_err(&pdev->dev, "unable to allocate glue memory\n");
  593. return -ENOMEM;
  594. }
  595. glue->dev = &pdev->dev;
  596. glue->wrp = wrp;
  597. platform_set_drvdata(pdev, glue);
  598. pm_runtime_enable(&pdev->dev);
  599. ret = pm_runtime_get_sync(&pdev->dev);
  600. if (ret < 0) {
  601. dev_err(&pdev->dev, "pm_runtime_get_sync FAILED");
  602. goto err2;
  603. }
  604. ret = dsps_create_musb_pdev(glue, pdev);
  605. if (ret)
  606. goto err3;
  607. return 0;
  608. err3:
  609. pm_runtime_put(&pdev->dev);
  610. err2:
  611. pm_runtime_disable(&pdev->dev);
  612. return ret;
  613. }
  614. static int dsps_remove(struct platform_device *pdev)
  615. {
  616. struct dsps_glue *glue = platform_get_drvdata(pdev);
  617. platform_device_unregister(glue->musb);
  618. /* disable usbss clocks */
  619. pm_runtime_put(&pdev->dev);
  620. pm_runtime_disable(&pdev->dev);
  621. return 0;
  622. }
  623. static const struct dsps_musb_wrapper am33xx_driver_data = {
  624. .revision = 0x00,
  625. .control = 0x14,
  626. .status = 0x18,
  627. .epintr_set = 0x38,
  628. .epintr_clear = 0x40,
  629. .epintr_status = 0x30,
  630. .coreintr_set = 0x3c,
  631. .coreintr_clear = 0x44,
  632. .coreintr_status = 0x34,
  633. .phy_utmi = 0xe0,
  634. .mode = 0xe8,
  635. .tx_mode = 0x70,
  636. .rx_mode = 0x74,
  637. .reset = 0,
  638. .otg_disable = 21,
  639. .iddig = 8,
  640. .iddig_mux = 7,
  641. .usb_shift = 0,
  642. .usb_mask = 0x1ff,
  643. .usb_bitmap = (0x1ff << 0),
  644. .drvvbus = 8,
  645. .txep_shift = 0,
  646. .txep_mask = 0xffff,
  647. .txep_bitmap = (0xffff << 0),
  648. .rxep_shift = 16,
  649. .rxep_mask = 0xfffe,
  650. .rxep_bitmap = (0xfffe << 16),
  651. .poll_seconds = 2,
  652. };
  653. static const struct of_device_id musb_dsps_of_match[] = {
  654. { .compatible = "ti,musb-am33xx",
  655. .data = (void *) &am33xx_driver_data, },
  656. { },
  657. };
  658. MODULE_DEVICE_TABLE(of, musb_dsps_of_match);
  659. #ifdef CONFIG_PM_SLEEP
  660. static int dsps_suspend(struct device *dev)
  661. {
  662. struct dsps_glue *glue = dev_get_drvdata(dev);
  663. const struct dsps_musb_wrapper *wrp = glue->wrp;
  664. struct musb *musb = platform_get_drvdata(glue->musb);
  665. void __iomem *mbase = musb->ctrl_base;
  666. glue->context.control = dsps_readl(mbase, wrp->control);
  667. glue->context.epintr = dsps_readl(mbase, wrp->epintr_set);
  668. glue->context.coreintr = dsps_readl(mbase, wrp->coreintr_set);
  669. glue->context.phy_utmi = dsps_readl(mbase, wrp->phy_utmi);
  670. glue->context.mode = dsps_readl(mbase, wrp->mode);
  671. glue->context.tx_mode = dsps_readl(mbase, wrp->tx_mode);
  672. glue->context.rx_mode = dsps_readl(mbase, wrp->rx_mode);
  673. return 0;
  674. }
  675. static int dsps_resume(struct device *dev)
  676. {
  677. struct dsps_glue *glue = dev_get_drvdata(dev);
  678. const struct dsps_musb_wrapper *wrp = glue->wrp;
  679. struct musb *musb = platform_get_drvdata(glue->musb);
  680. void __iomem *mbase = musb->ctrl_base;
  681. dsps_writel(mbase, wrp->control, glue->context.control);
  682. dsps_writel(mbase, wrp->epintr_set, glue->context.epintr);
  683. dsps_writel(mbase, wrp->coreintr_set, glue->context.coreintr);
  684. dsps_writel(mbase, wrp->phy_utmi, glue->context.phy_utmi);
  685. dsps_writel(mbase, wrp->mode, glue->context.mode);
  686. dsps_writel(mbase, wrp->tx_mode, glue->context.tx_mode);
  687. dsps_writel(mbase, wrp->rx_mode, glue->context.rx_mode);
  688. return 0;
  689. }
  690. #endif
  691. static SIMPLE_DEV_PM_OPS(dsps_pm_ops, dsps_suspend, dsps_resume);
  692. static struct platform_driver dsps_usbss_driver = {
  693. .probe = dsps_probe,
  694. .remove = dsps_remove,
  695. .driver = {
  696. .name = "musb-dsps",
  697. .pm = &dsps_pm_ops,
  698. .of_match_table = musb_dsps_of_match,
  699. },
  700. };
  701. MODULE_DESCRIPTION("TI DSPS MUSB Glue Layer");
  702. MODULE_AUTHOR("Ravi B <ravibabu@ti.com>");
  703. MODULE_AUTHOR("Ajay Kumar Gupta <ajay.gupta@ti.com>");
  704. MODULE_LICENSE("GPL v2");
  705. module_platform_driver(dsps_usbss_driver);