musb_dsps.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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/init.h>
  32. #include <linux/io.h>
  33. #include <linux/of.h>
  34. #include <linux/err.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/dma-mapping.h>
  37. #include <linux/pm_runtime.h>
  38. #include <linux/module.h>
  39. #include <linux/usb/nop-usb-xceiv.h>
  40. #include <linux/platform_data/usb-omap.h>
  41. #include <linux/of.h>
  42. #include <linux/of_device.h>
  43. #include <linux/of_address.h>
  44. #include "musb_core.h"
  45. #ifdef CONFIG_OF
  46. static const struct of_device_id musb_dsps_of_match[];
  47. #endif
  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 eoi;
  70. u16 epintr_set;
  71. u16 epintr_clear;
  72. u16 epintr_status;
  73. u16 coreintr_set;
  74. u16 coreintr_clear;
  75. u16 coreintr_status;
  76. u16 phy_utmi;
  77. u16 mode;
  78. /* bit positions for control */
  79. unsigned reset:5;
  80. /* bit positions for interrupt */
  81. unsigned usb_shift:5;
  82. u32 usb_mask;
  83. u32 usb_bitmap;
  84. unsigned drvvbus:5;
  85. unsigned txep_shift:5;
  86. u32 txep_mask;
  87. u32 txep_bitmap;
  88. unsigned rxep_shift:5;
  89. u32 rxep_mask;
  90. u32 rxep_bitmap;
  91. /* bit positions for phy_utmi */
  92. unsigned otg_disable:5;
  93. /* bit positions for mode */
  94. unsigned iddig:5;
  95. /* miscellaneous stuff */
  96. u32 musb_core_offset;
  97. u8 poll_seconds;
  98. /* number of musb instances */
  99. u8 instances;
  100. };
  101. /**
  102. * DSPS glue structure.
  103. */
  104. struct dsps_glue {
  105. struct device *dev;
  106. struct platform_device *musb[2]; /* child musb pdev */
  107. const struct dsps_musb_wrapper *wrp; /* wrapper register offsets */
  108. struct timer_list timer[2]; /* otg_workaround timer */
  109. unsigned long last_timer[2]; /* last timer data for each instance */
  110. u32 __iomem *usb_ctrl[2];
  111. };
  112. #define DSPS_AM33XX_CONTROL_MODULE_PHYS_0 0x44e10620
  113. #define DSPS_AM33XX_CONTROL_MODULE_PHYS_1 0x44e10628
  114. static const resource_size_t dsps_control_module_phys[] = {
  115. DSPS_AM33XX_CONTROL_MODULE_PHYS_0,
  116. DSPS_AM33XX_CONTROL_MODULE_PHYS_1,
  117. };
  118. /**
  119. * musb_dsps_phy_control - phy on/off
  120. * @glue: struct dsps_glue *
  121. * @id: musb instance
  122. * @on: flag for phy to be switched on or off
  123. *
  124. * This is to enable the PHY using usb_ctrl register in system control
  125. * module space.
  126. *
  127. * XXX: This function will be removed once we have a seperate driver for
  128. * control module
  129. */
  130. static void musb_dsps_phy_control(struct dsps_glue *glue, u8 id, u8 on)
  131. {
  132. u32 usbphycfg;
  133. usbphycfg = readl(glue->usb_ctrl[id]);
  134. if (on) {
  135. usbphycfg &= ~(USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN);
  136. usbphycfg |= USBPHY_OTGVDET_EN | USBPHY_OTGSESSEND_EN;
  137. } else {
  138. usbphycfg |= USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN;
  139. }
  140. writel(usbphycfg, glue->usb_ctrl[id]);
  141. }
  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 platform_device *pdev = to_platform_device(dev->parent);
  149. struct dsps_glue *glue = platform_get_drvdata(pdev);
  150. const struct dsps_musb_wrapper *wrp = glue->wrp;
  151. void __iomem *reg_base = musb->ctrl_base;
  152. u32 epmask, coremask;
  153. /* Workaround: setup IRQs through both register sets. */
  154. epmask = ((musb->epmask & wrp->txep_mask) << wrp->txep_shift) |
  155. ((musb->epmask & wrp->rxep_mask) << wrp->rxep_shift);
  156. coremask = (wrp->usb_bitmap & ~MUSB_INTR_SOF);
  157. dsps_writel(reg_base, wrp->epintr_set, epmask);
  158. dsps_writel(reg_base, wrp->coreintr_set, coremask);
  159. /* Force the DRVVBUS IRQ so we can start polling for ID change. */
  160. dsps_writel(reg_base, wrp->coreintr_set,
  161. (1 << wrp->drvvbus) << wrp->usb_shift);
  162. }
  163. /**
  164. * dsps_musb_disable - disable HDRC and flush interrupts
  165. */
  166. static void dsps_musb_disable(struct musb *musb)
  167. {
  168. struct device *dev = musb->controller;
  169. struct platform_device *pdev = to_platform_device(dev->parent);
  170. struct dsps_glue *glue = platform_get_drvdata(pdev);
  171. const struct dsps_musb_wrapper *wrp = glue->wrp;
  172. void __iomem *reg_base = musb->ctrl_base;
  173. dsps_writel(reg_base, wrp->coreintr_clear, wrp->usb_bitmap);
  174. dsps_writel(reg_base, wrp->epintr_clear,
  175. wrp->txep_bitmap | wrp->rxep_bitmap);
  176. dsps_writeb(musb->mregs, MUSB_DEVCTL, 0);
  177. dsps_writel(reg_base, wrp->eoi, 0);
  178. }
  179. static void otg_timer(unsigned long _musb)
  180. {
  181. struct musb *musb = (void *)_musb;
  182. void __iomem *mregs = musb->mregs;
  183. struct device *dev = musb->controller;
  184. struct platform_device *pdev = to_platform_device(dev);
  185. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  186. const struct dsps_musb_wrapper *wrp = glue->wrp;
  187. u8 devctl;
  188. unsigned long flags;
  189. /*
  190. * We poll because DSPS IP's won't expose several OTG-critical
  191. * status change events (from the transceiver) otherwise.
  192. */
  193. devctl = dsps_readb(mregs, MUSB_DEVCTL);
  194. dev_dbg(musb->controller, "Poll devctl %02x (%s)\n", devctl,
  195. otg_state_string(musb->xceiv->state));
  196. spin_lock_irqsave(&musb->lock, flags);
  197. switch (musb->xceiv->state) {
  198. case OTG_STATE_A_WAIT_BCON:
  199. devctl &= ~MUSB_DEVCTL_SESSION;
  200. dsps_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  201. devctl = dsps_readb(musb->mregs, MUSB_DEVCTL);
  202. if (devctl & MUSB_DEVCTL_BDEVICE) {
  203. musb->xceiv->state = OTG_STATE_B_IDLE;
  204. MUSB_DEV_MODE(musb);
  205. } else {
  206. musb->xceiv->state = OTG_STATE_A_IDLE;
  207. MUSB_HST_MODE(musb);
  208. }
  209. break;
  210. case OTG_STATE_A_WAIT_VFALL:
  211. musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
  212. dsps_writel(musb->ctrl_base, wrp->coreintr_set,
  213. MUSB_INTR_VBUSERROR << wrp->usb_shift);
  214. break;
  215. case OTG_STATE_B_IDLE:
  216. devctl = dsps_readb(mregs, MUSB_DEVCTL);
  217. if (devctl & MUSB_DEVCTL_BDEVICE)
  218. mod_timer(&glue->timer[pdev->id],
  219. jiffies + wrp->poll_seconds * HZ);
  220. else
  221. musb->xceiv->state = OTG_STATE_A_IDLE;
  222. break;
  223. default:
  224. break;
  225. }
  226. spin_unlock_irqrestore(&musb->lock, flags);
  227. }
  228. static void dsps_musb_try_idle(struct musb *musb, unsigned long timeout)
  229. {
  230. struct device *dev = musb->controller;
  231. struct platform_device *pdev = to_platform_device(dev);
  232. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  233. if (timeout == 0)
  234. timeout = jiffies + msecs_to_jiffies(3);
  235. /* Never idle if active, or when VBUS timeout is not set as host */
  236. if (musb->is_active || (musb->a_wait_bcon == 0 &&
  237. musb->xceiv->state == OTG_STATE_A_WAIT_BCON)) {
  238. dev_dbg(musb->controller, "%s active, deleting timer\n",
  239. otg_state_string(musb->xceiv->state));
  240. del_timer(&glue->timer[pdev->id]);
  241. glue->last_timer[pdev->id] = jiffies;
  242. return;
  243. }
  244. if (time_after(glue->last_timer[pdev->id], timeout) &&
  245. timer_pending(&glue->timer[pdev->id])) {
  246. dev_dbg(musb->controller,
  247. "Longer idle timer already pending, ignoring...\n");
  248. return;
  249. }
  250. glue->last_timer[pdev->id] = timeout;
  251. dev_dbg(musb->controller, "%s inactive, starting idle timer for %u ms\n",
  252. otg_state_string(musb->xceiv->state),
  253. jiffies_to_msecs(timeout - jiffies));
  254. mod_timer(&glue->timer[pdev->id], timeout);
  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 platform_device *pdev = to_platform_device(dev);
  262. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  263. const struct dsps_musb_wrapper *wrp = glue->wrp;
  264. unsigned long flags;
  265. irqreturn_t ret = IRQ_NONE;
  266. u32 epintr, usbintr;
  267. spin_lock_irqsave(&musb->lock, flags);
  268. /* Get endpoint interrupts */
  269. epintr = dsps_readl(reg_base, wrp->epintr_status);
  270. musb->int_rx = (epintr & wrp->rxep_bitmap) >> wrp->rxep_shift;
  271. musb->int_tx = (epintr & wrp->txep_bitmap) >> wrp->txep_shift;
  272. if (epintr)
  273. dsps_writel(reg_base, wrp->epintr_status, epintr);
  274. /* Get usb core interrupts */
  275. usbintr = dsps_readl(reg_base, wrp->coreintr_status);
  276. if (!usbintr && !epintr)
  277. goto eoi;
  278. musb->int_usb = (usbintr & wrp->usb_bitmap) >> wrp->usb_shift;
  279. if (usbintr)
  280. dsps_writel(reg_base, wrp->coreintr_status, usbintr);
  281. dev_dbg(musb->controller, "usbintr (%x) epintr(%x)\n",
  282. usbintr, epintr);
  283. /*
  284. * DRVVBUS IRQs are the only proxy we have (a very poor one!) for
  285. * DSPS IP's missing ID change IRQ. We need an ID change IRQ to
  286. * switch appropriately between halves of the OTG state machine.
  287. * Managing DEVCTL.SESSION per Mentor docs requires that we know its
  288. * value but DEVCTL.BDEVICE is invalid without DEVCTL.SESSION set.
  289. * Also, DRVVBUS pulses for SRP (but not at 5V) ...
  290. */
  291. if (usbintr & MUSB_INTR_BABBLE)
  292. pr_info("CAUTION: musb: Babble Interrupt Occurred\n");
  293. if (usbintr & ((1 << wrp->drvvbus) << wrp->usb_shift)) {
  294. int drvvbus = dsps_readl(reg_base, wrp->status);
  295. void __iomem *mregs = musb->mregs;
  296. u8 devctl = dsps_readb(mregs, MUSB_DEVCTL);
  297. int err;
  298. err = musb->int_usb & MUSB_INTR_VBUSERROR;
  299. if (err) {
  300. /*
  301. * The Mentor core doesn't debounce VBUS as needed
  302. * to cope with device connect current spikes. This
  303. * means it's not uncommon for bus-powered devices
  304. * to get VBUS errors during enumeration.
  305. *
  306. * This is a workaround, but newer RTL from Mentor
  307. * seems to allow a better one: "re"-starting sessions
  308. * without waiting for VBUS to stop registering in
  309. * devctl.
  310. */
  311. musb->int_usb &= ~MUSB_INTR_VBUSERROR;
  312. musb->xceiv->state = OTG_STATE_A_WAIT_VFALL;
  313. mod_timer(&glue->timer[pdev->id],
  314. jiffies + wrp->poll_seconds * HZ);
  315. WARNING("VBUS error workaround (delay coming)\n");
  316. } else if (drvvbus) {
  317. musb->is_active = 1;
  318. MUSB_HST_MODE(musb);
  319. musb->xceiv->otg->default_a = 1;
  320. musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
  321. del_timer(&glue->timer[pdev->id]);
  322. } else {
  323. musb->is_active = 0;
  324. MUSB_DEV_MODE(musb);
  325. musb->xceiv->otg->default_a = 0;
  326. musb->xceiv->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. otg_state_string(musb->xceiv->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. eoi:
  339. /* EOI needs to be written for the IRQ to be re-asserted. */
  340. if (ret == IRQ_HANDLED || epintr || usbintr)
  341. dsps_writel(reg_base, wrp->eoi, 1);
  342. /* Poll for ID change */
  343. if (musb->xceiv->state == OTG_STATE_B_IDLE)
  344. mod_timer(&glue->timer[pdev->id],
  345. jiffies + wrp->poll_seconds * HZ);
  346. spin_unlock_irqrestore(&musb->lock, flags);
  347. return ret;
  348. }
  349. static int dsps_musb_init(struct musb *musb)
  350. {
  351. struct device *dev = musb->controller;
  352. struct platform_device *pdev = to_platform_device(dev);
  353. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  354. const struct dsps_musb_wrapper *wrp = glue->wrp;
  355. void __iomem *reg_base = musb->ctrl_base;
  356. u32 rev, val;
  357. int status;
  358. /* mentor core register starts at offset of 0x400 from musb base */
  359. musb->mregs += wrp->musb_core_offset;
  360. /* NOP driver needs change if supporting dual instance */
  361. usb_nop_xceiv_register();
  362. musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
  363. if (IS_ERR_OR_NULL(musb->xceiv))
  364. return -ENODEV;
  365. /* Returns zero if e.g. not clocked */
  366. rev = dsps_readl(reg_base, wrp->revision);
  367. if (!rev) {
  368. status = -ENODEV;
  369. goto err0;
  370. }
  371. setup_timer(&glue->timer[pdev->id], otg_timer, (unsigned long) musb);
  372. /* Reset the musb */
  373. dsps_writel(reg_base, wrp->control, (1 << wrp->reset));
  374. /* Start the on-chip PHY and its PLL. */
  375. musb_dsps_phy_control(glue, pdev->id, 1);
  376. musb->isr = dsps_interrupt;
  377. /* reset the otgdisable bit, needed for host mode to work */
  378. val = dsps_readl(reg_base, wrp->phy_utmi);
  379. val &= ~(1 << wrp->otg_disable);
  380. dsps_writel(musb->ctrl_base, wrp->phy_utmi, val);
  381. /* clear level interrupt */
  382. dsps_writel(reg_base, wrp->eoi, 0);
  383. return 0;
  384. err0:
  385. usb_put_phy(musb->xceiv);
  386. usb_nop_xceiv_unregister();
  387. return status;
  388. }
  389. static int dsps_musb_exit(struct musb *musb)
  390. {
  391. struct device *dev = musb->controller;
  392. struct platform_device *pdev = to_platform_device(dev);
  393. struct dsps_glue *glue = dev_get_drvdata(dev->parent);
  394. del_timer_sync(&glue->timer[pdev->id]);
  395. /* Shutdown the on-chip PHY and its PLL. */
  396. musb_dsps_phy_control(glue, pdev->id, 0);
  397. /* NOP driver needs change if supporting dual instance */
  398. usb_put_phy(musb->xceiv);
  399. usb_nop_xceiv_unregister();
  400. return 0;
  401. }
  402. static struct musb_platform_ops dsps_ops = {
  403. .init = dsps_musb_init,
  404. .exit = dsps_musb_exit,
  405. .enable = dsps_musb_enable,
  406. .disable = dsps_musb_disable,
  407. .try_idle = dsps_musb_try_idle,
  408. };
  409. static u64 musb_dmamask = DMA_BIT_MASK(32);
  410. static int dsps_create_musb_pdev(struct dsps_glue *glue, u8 id)
  411. {
  412. struct device *dev = glue->dev;
  413. struct platform_device *pdev = to_platform_device(dev);
  414. struct musb_hdrc_platform_data *pdata = dev->platform_data;
  415. struct device_node *np = pdev->dev.of_node;
  416. struct musb_hdrc_config *config;
  417. struct platform_device *musb;
  418. struct resource *res;
  419. struct resource resources[2];
  420. char res_name[11];
  421. int ret;
  422. resources[0].start = dsps_control_module_phys[id];
  423. resources[0].end = resources[0].start + SZ_4 - 1;
  424. resources[0].flags = IORESOURCE_MEM;
  425. glue->usb_ctrl[id] = devm_request_and_ioremap(&pdev->dev, resources);
  426. if (glue->usb_ctrl[id] == NULL) {
  427. dev_err(dev, "Failed to obtain usb_ctrl%d memory\n", id);
  428. ret = -ENODEV;
  429. goto err0;
  430. }
  431. /* first resource is for usbss, so start index from 1 */
  432. res = platform_get_resource(pdev, IORESOURCE_MEM, id + 1);
  433. if (!res) {
  434. dev_err(dev, "failed to get memory for instance %d\n", id);
  435. ret = -ENODEV;
  436. goto err0;
  437. }
  438. res->parent = NULL;
  439. resources[0] = *res;
  440. /* first resource is for usbss, so start index from 1 */
  441. res = platform_get_resource(pdev, IORESOURCE_IRQ, id + 1);
  442. if (!res) {
  443. dev_err(dev, "failed to get irq for instance %d\n", id);
  444. ret = -ENODEV;
  445. goto err0;
  446. }
  447. res->parent = NULL;
  448. resources[1] = *res;
  449. resources[1].name = "mc";
  450. /* allocate the child platform device */
  451. musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
  452. if (!musb) {
  453. dev_err(dev, "failed to allocate musb device\n");
  454. ret = -ENOMEM;
  455. goto err0;
  456. }
  457. musb->dev.parent = dev;
  458. musb->dev.dma_mask = &musb_dmamask;
  459. musb->dev.coherent_dma_mask = musb_dmamask;
  460. glue->musb[id] = musb;
  461. ret = platform_device_add_resources(musb, resources, 2);
  462. if (ret) {
  463. dev_err(dev, "failed to add resources\n");
  464. goto err2;
  465. }
  466. if (np) {
  467. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  468. if (!pdata) {
  469. dev_err(&pdev->dev,
  470. "failed to allocate musb platfrom data\n");
  471. ret = -ENOMEM;
  472. goto err2;
  473. }
  474. config = devm_kzalloc(&pdev->dev, sizeof(*config), GFP_KERNEL);
  475. if (!config) {
  476. dev_err(&pdev->dev,
  477. "failed to allocate musb hdrc config\n");
  478. goto err2;
  479. }
  480. of_property_read_u32(np, "num-eps", (u32 *)&config->num_eps);
  481. of_property_read_u32(np, "ram-bits", (u32 *)&config->ram_bits);
  482. snprintf(res_name, sizeof(res_name), "port%d-mode", id);
  483. of_property_read_u32(np, res_name, (u32 *)&pdata->mode);
  484. of_property_read_u32(np, "power", (u32 *)&pdata->power);
  485. config->multipoint = of_property_read_bool(np, "multipoint");
  486. pdata->config = config;
  487. }
  488. pdata->platform_ops = &dsps_ops;
  489. ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
  490. if (ret) {
  491. dev_err(dev, "failed to add platform_data\n");
  492. goto err2;
  493. }
  494. ret = platform_device_add(musb);
  495. if (ret) {
  496. dev_err(dev, "failed to register musb device\n");
  497. goto err2;
  498. }
  499. return 0;
  500. err2:
  501. platform_device_put(musb);
  502. err0:
  503. return ret;
  504. }
  505. static int dsps_probe(struct platform_device *pdev)
  506. {
  507. struct device_node *np = pdev->dev.of_node;
  508. const struct of_device_id *match;
  509. const struct dsps_musb_wrapper *wrp;
  510. struct dsps_glue *glue;
  511. struct resource *iomem;
  512. int ret, i;
  513. match = of_match_node(musb_dsps_of_match, np);
  514. if (!match) {
  515. dev_err(&pdev->dev, "fail to get matching of_match struct\n");
  516. ret = -EINVAL;
  517. goto err0;
  518. }
  519. wrp = match->data;
  520. /* allocate glue */
  521. glue = kzalloc(sizeof(*glue), GFP_KERNEL);
  522. if (!glue) {
  523. dev_err(&pdev->dev, "unable to allocate glue memory\n");
  524. ret = -ENOMEM;
  525. goto err0;
  526. }
  527. /* get memory resource */
  528. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  529. if (!iomem) {
  530. dev_err(&pdev->dev, "failed to get usbss mem resourse\n");
  531. ret = -ENODEV;
  532. goto err1;
  533. }
  534. glue->dev = &pdev->dev;
  535. glue->wrp = kmemdup(wrp, sizeof(*wrp), GFP_KERNEL);
  536. if (!glue->wrp) {
  537. dev_err(&pdev->dev, "failed to duplicate wrapper struct memory\n");
  538. ret = -ENOMEM;
  539. goto err1;
  540. }
  541. platform_set_drvdata(pdev, glue);
  542. /* enable the usbss clocks */
  543. pm_runtime_enable(&pdev->dev);
  544. ret = pm_runtime_get_sync(&pdev->dev);
  545. if (ret < 0) {
  546. dev_err(&pdev->dev, "pm_runtime_get_sync FAILED");
  547. goto err2;
  548. }
  549. /* create the child platform device for all instances of musb */
  550. for (i = 0; i < wrp->instances ; i++) {
  551. ret = dsps_create_musb_pdev(glue, i);
  552. if (ret != 0) {
  553. dev_err(&pdev->dev, "failed to create child pdev\n");
  554. /* release resources of previously created instances */
  555. for (i--; i >= 0 ; i--)
  556. platform_device_unregister(glue->musb[i]);
  557. goto err3;
  558. }
  559. }
  560. return 0;
  561. err3:
  562. pm_runtime_put(&pdev->dev);
  563. err2:
  564. pm_runtime_disable(&pdev->dev);
  565. kfree(glue->wrp);
  566. err1:
  567. kfree(glue);
  568. err0:
  569. return ret;
  570. }
  571. static int dsps_remove(struct platform_device *pdev)
  572. {
  573. struct dsps_glue *glue = platform_get_drvdata(pdev);
  574. const struct dsps_musb_wrapper *wrp = glue->wrp;
  575. int i;
  576. /* delete the child platform device */
  577. for (i = 0; i < wrp->instances ; i++)
  578. platform_device_unregister(glue->musb[i]);
  579. /* disable usbss clocks */
  580. pm_runtime_put(&pdev->dev);
  581. pm_runtime_disable(&pdev->dev);
  582. kfree(glue->wrp);
  583. kfree(glue);
  584. return 0;
  585. }
  586. #ifdef CONFIG_PM_SLEEP
  587. static int dsps_suspend(struct device *dev)
  588. {
  589. struct platform_device *pdev = to_platform_device(dev->parent);
  590. struct dsps_glue *glue = platform_get_drvdata(pdev);
  591. const struct dsps_musb_wrapper *wrp = glue->wrp;
  592. int i;
  593. for (i = 0; i < wrp->instances; i++)
  594. musb_dsps_phy_control(glue, i, 0);
  595. return 0;
  596. }
  597. static int dsps_resume(struct device *dev)
  598. {
  599. struct platform_device *pdev = to_platform_device(dev->parent);
  600. struct dsps_glue *glue = platform_get_drvdata(pdev);
  601. const struct dsps_musb_wrapper *wrp = glue->wrp;
  602. int i;
  603. for (i = 0; i < wrp->instances; i++)
  604. musb_dsps_phy_control(glue, i, 1);
  605. return 0;
  606. }
  607. #endif
  608. static SIMPLE_DEV_PM_OPS(dsps_pm_ops, dsps_suspend, dsps_resume);
  609. static const struct dsps_musb_wrapper ti81xx_driver_data = {
  610. .revision = 0x00,
  611. .control = 0x14,
  612. .status = 0x18,
  613. .eoi = 0x24,
  614. .epintr_set = 0x38,
  615. .epintr_clear = 0x40,
  616. .epintr_status = 0x30,
  617. .coreintr_set = 0x3c,
  618. .coreintr_clear = 0x44,
  619. .coreintr_status = 0x34,
  620. .phy_utmi = 0xe0,
  621. .mode = 0xe8,
  622. .reset = 0,
  623. .otg_disable = 21,
  624. .iddig = 8,
  625. .usb_shift = 0,
  626. .usb_mask = 0x1ff,
  627. .usb_bitmap = (0x1ff << 0),
  628. .drvvbus = 8,
  629. .txep_shift = 0,
  630. .txep_mask = 0xffff,
  631. .txep_bitmap = (0xffff << 0),
  632. .rxep_shift = 16,
  633. .rxep_mask = 0xfffe,
  634. .rxep_bitmap = (0xfffe << 16),
  635. .musb_core_offset = 0x400,
  636. .poll_seconds = 2,
  637. .instances = 1,
  638. };
  639. static const struct platform_device_id musb_dsps_id_table[] = {
  640. {
  641. .name = "musb-ti81xx",
  642. .driver_data = (kernel_ulong_t) &ti81xx_driver_data,
  643. },
  644. { }, /* Terminating Entry */
  645. };
  646. MODULE_DEVICE_TABLE(platform, musb_dsps_id_table);
  647. #ifdef CONFIG_OF
  648. static const struct of_device_id musb_dsps_of_match[] = {
  649. { .compatible = "ti,musb-am33xx",
  650. .data = (void *) &ti81xx_driver_data, },
  651. { },
  652. };
  653. MODULE_DEVICE_TABLE(of, musb_dsps_of_match);
  654. #endif
  655. static struct platform_driver dsps_usbss_driver = {
  656. .probe = dsps_probe,
  657. .remove = dsps_remove,
  658. .driver = {
  659. .name = "musb-dsps",
  660. .pm = &dsps_pm_ops,
  661. .of_match_table = of_match_ptr(musb_dsps_of_match),
  662. },
  663. .id_table = musb_dsps_id_table,
  664. };
  665. MODULE_DESCRIPTION("TI DSPS MUSB Glue Layer");
  666. MODULE_AUTHOR("Ravi B <ravibabu@ti.com>");
  667. MODULE_AUTHOR("Ajay Kumar Gupta <ajay.gupta@ti.com>");
  668. MODULE_LICENSE("GPL v2");
  669. static int __init dsps_init(void)
  670. {
  671. return platform_driver_register(&dsps_usbss_driver);
  672. }
  673. subsys_initcall(dsps_init);
  674. static void __exit dsps_exit(void)
  675. {
  676. platform_driver_unregister(&dsps_usbss_driver);
  677. }
  678. module_exit(dsps_exit);