tusb6010.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. /*
  2. * TUSB6010 USB 2.0 OTG Dual Role controller
  3. *
  4. * Copyright (C) 2006 Nokia Corporation
  5. * Tony Lindgren <tony@atomide.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Notes:
  12. * - Driver assumes that interface to external host (main CPU) is
  13. * configured for NOR FLASH interface instead of VLYNQ serial
  14. * interface.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/errno.h>
  19. #include <linux/err.h>
  20. #include <linux/init.h>
  21. #include <linux/prefetch.h>
  22. #include <linux/usb.h>
  23. #include <linux/irq.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/usb/nop-usb-xceiv.h>
  27. #include "musb_core.h"
  28. struct tusb6010_glue {
  29. struct device *dev;
  30. struct platform_device *musb;
  31. };
  32. static void tusb_musb_set_vbus(struct musb *musb, int is_on);
  33. #define TUSB_REV_MAJOR(reg_val) ((reg_val >> 4) & 0xf)
  34. #define TUSB_REV_MINOR(reg_val) (reg_val & 0xf)
  35. /*
  36. * Checks the revision. We need to use the DMA register as 3.0 does not
  37. * have correct versions for TUSB_PRCM_REV or TUSB_INT_CTRL_REV.
  38. */
  39. u8 tusb_get_revision(struct musb *musb)
  40. {
  41. void __iomem *tbase = musb->ctrl_base;
  42. u32 die_id;
  43. u8 rev;
  44. rev = musb_readl(tbase, TUSB_DMA_CTRL_REV) & 0xff;
  45. if (TUSB_REV_MAJOR(rev) == 3) {
  46. die_id = TUSB_DIDR1_HI_CHIP_REV(musb_readl(tbase,
  47. TUSB_DIDR1_HI));
  48. if (die_id >= TUSB_DIDR1_HI_REV_31)
  49. rev |= 1;
  50. }
  51. return rev;
  52. }
  53. EXPORT_SYMBOL_GPL(tusb_get_revision);
  54. static int tusb_print_revision(struct musb *musb)
  55. {
  56. void __iomem *tbase = musb->ctrl_base;
  57. u8 rev;
  58. rev = tusb_get_revision(musb);
  59. pr_info("tusb: %s%i.%i %s%i.%i %s%i.%i %s%i.%i %s%i %s%i.%i\n",
  60. "prcm",
  61. TUSB_REV_MAJOR(musb_readl(tbase, TUSB_PRCM_REV)),
  62. TUSB_REV_MINOR(musb_readl(tbase, TUSB_PRCM_REV)),
  63. "int",
  64. TUSB_REV_MAJOR(musb_readl(tbase, TUSB_INT_CTRL_REV)),
  65. TUSB_REV_MINOR(musb_readl(tbase, TUSB_INT_CTRL_REV)),
  66. "gpio",
  67. TUSB_REV_MAJOR(musb_readl(tbase, TUSB_GPIO_REV)),
  68. TUSB_REV_MINOR(musb_readl(tbase, TUSB_GPIO_REV)),
  69. "dma",
  70. TUSB_REV_MAJOR(musb_readl(tbase, TUSB_DMA_CTRL_REV)),
  71. TUSB_REV_MINOR(musb_readl(tbase, TUSB_DMA_CTRL_REV)),
  72. "dieid",
  73. TUSB_DIDR1_HI_CHIP_REV(musb_readl(tbase, TUSB_DIDR1_HI)),
  74. "rev",
  75. TUSB_REV_MAJOR(rev), TUSB_REV_MINOR(rev));
  76. return tusb_get_revision(musb);
  77. }
  78. #define WBUS_QUIRK_MASK (TUSB_PHY_OTG_CTRL_TESTM2 | TUSB_PHY_OTG_CTRL_TESTM1 \
  79. | TUSB_PHY_OTG_CTRL_TESTM0)
  80. /*
  81. * Workaround for spontaneous WBUS wake-up issue #2 for tusb3.0.
  82. * Disables power detection in PHY for the duration of idle.
  83. */
  84. static void tusb_wbus_quirk(struct musb *musb, int enabled)
  85. {
  86. void __iomem *tbase = musb->ctrl_base;
  87. static u32 phy_otg_ctrl, phy_otg_ena;
  88. u32 tmp;
  89. if (enabled) {
  90. phy_otg_ctrl = musb_readl(tbase, TUSB_PHY_OTG_CTRL);
  91. phy_otg_ena = musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE);
  92. tmp = TUSB_PHY_OTG_CTRL_WRPROTECT
  93. | phy_otg_ena | WBUS_QUIRK_MASK;
  94. musb_writel(tbase, TUSB_PHY_OTG_CTRL, tmp);
  95. tmp = phy_otg_ena & ~WBUS_QUIRK_MASK;
  96. tmp |= TUSB_PHY_OTG_CTRL_WRPROTECT | TUSB_PHY_OTG_CTRL_TESTM2;
  97. musb_writel(tbase, TUSB_PHY_OTG_CTRL_ENABLE, tmp);
  98. dev_dbg(musb->controller, "Enabled tusb wbus quirk ctrl %08x ena %08x\n",
  99. musb_readl(tbase, TUSB_PHY_OTG_CTRL),
  100. musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE));
  101. } else if (musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE)
  102. & TUSB_PHY_OTG_CTRL_TESTM2) {
  103. tmp = TUSB_PHY_OTG_CTRL_WRPROTECT | phy_otg_ctrl;
  104. musb_writel(tbase, TUSB_PHY_OTG_CTRL, tmp);
  105. tmp = TUSB_PHY_OTG_CTRL_WRPROTECT | phy_otg_ena;
  106. musb_writel(tbase, TUSB_PHY_OTG_CTRL_ENABLE, tmp);
  107. dev_dbg(musb->controller, "Disabled tusb wbus quirk ctrl %08x ena %08x\n",
  108. musb_readl(tbase, TUSB_PHY_OTG_CTRL),
  109. musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE));
  110. phy_otg_ctrl = 0;
  111. phy_otg_ena = 0;
  112. }
  113. }
  114. /*
  115. * TUSB 6010 may use a parallel bus that doesn't support byte ops;
  116. * so both loading and unloading FIFOs need explicit byte counts.
  117. */
  118. static inline void
  119. tusb_fifo_write_unaligned(void __iomem *fifo, const u8 *buf, u16 len)
  120. {
  121. u32 val;
  122. int i;
  123. if (len > 4) {
  124. for (i = 0; i < (len >> 2); i++) {
  125. memcpy(&val, buf, 4);
  126. musb_writel(fifo, 0, val);
  127. buf += 4;
  128. }
  129. len %= 4;
  130. }
  131. if (len > 0) {
  132. /* Write the rest 1 - 3 bytes to FIFO */
  133. memcpy(&val, buf, len);
  134. musb_writel(fifo, 0, val);
  135. }
  136. }
  137. static inline void tusb_fifo_read_unaligned(void __iomem *fifo,
  138. void __iomem *buf, u16 len)
  139. {
  140. u32 val;
  141. int i;
  142. if (len > 4) {
  143. for (i = 0; i < (len >> 2); i++) {
  144. val = musb_readl(fifo, 0);
  145. memcpy(buf, &val, 4);
  146. buf += 4;
  147. }
  148. len %= 4;
  149. }
  150. if (len > 0) {
  151. /* Read the rest 1 - 3 bytes from FIFO */
  152. val = musb_readl(fifo, 0);
  153. memcpy(buf, &val, len);
  154. }
  155. }
  156. void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *buf)
  157. {
  158. struct musb *musb = hw_ep->musb;
  159. void __iomem *ep_conf = hw_ep->conf;
  160. void __iomem *fifo = hw_ep->fifo;
  161. u8 epnum = hw_ep->epnum;
  162. prefetch(buf);
  163. dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
  164. 'T', epnum, fifo, len, buf);
  165. if (epnum)
  166. musb_writel(ep_conf, TUSB_EP_TX_OFFSET,
  167. TUSB_EP_CONFIG_XFR_SIZE(len));
  168. else
  169. musb_writel(ep_conf, 0, TUSB_EP0_CONFIG_DIR_TX |
  170. TUSB_EP0_CONFIG_XFR_SIZE(len));
  171. if (likely((0x01 & (unsigned long) buf) == 0)) {
  172. /* Best case is 32bit-aligned destination address */
  173. if ((0x02 & (unsigned long) buf) == 0) {
  174. if (len >= 4) {
  175. writesl(fifo, buf, len >> 2);
  176. buf += (len & ~0x03);
  177. len &= 0x03;
  178. }
  179. } else {
  180. if (len >= 2) {
  181. u32 val;
  182. int i;
  183. /* Cannot use writesw, fifo is 32-bit */
  184. for (i = 0; i < (len >> 2); i++) {
  185. val = (u32)(*(u16 *)buf);
  186. buf += 2;
  187. val |= (*(u16 *)buf) << 16;
  188. buf += 2;
  189. musb_writel(fifo, 0, val);
  190. }
  191. len &= 0x03;
  192. }
  193. }
  194. }
  195. if (len > 0)
  196. tusb_fifo_write_unaligned(fifo, buf, len);
  197. }
  198. void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *buf)
  199. {
  200. struct musb *musb = hw_ep->musb;
  201. void __iomem *ep_conf = hw_ep->conf;
  202. void __iomem *fifo = hw_ep->fifo;
  203. u8 epnum = hw_ep->epnum;
  204. dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
  205. 'R', epnum, fifo, len, buf);
  206. if (epnum)
  207. musb_writel(ep_conf, TUSB_EP_RX_OFFSET,
  208. TUSB_EP_CONFIG_XFR_SIZE(len));
  209. else
  210. musb_writel(ep_conf, 0, TUSB_EP0_CONFIG_XFR_SIZE(len));
  211. if (likely((0x01 & (unsigned long) buf) == 0)) {
  212. /* Best case is 32bit-aligned destination address */
  213. if ((0x02 & (unsigned long) buf) == 0) {
  214. if (len >= 4) {
  215. readsl(fifo, buf, len >> 2);
  216. buf += (len & ~0x03);
  217. len &= 0x03;
  218. }
  219. } else {
  220. if (len >= 2) {
  221. u32 val;
  222. int i;
  223. /* Cannot use readsw, fifo is 32-bit */
  224. for (i = 0; i < (len >> 2); i++) {
  225. val = musb_readl(fifo, 0);
  226. *(u16 *)buf = (u16)(val & 0xffff);
  227. buf += 2;
  228. *(u16 *)buf = (u16)(val >> 16);
  229. buf += 2;
  230. }
  231. len &= 0x03;
  232. }
  233. }
  234. }
  235. if (len > 0)
  236. tusb_fifo_read_unaligned(fifo, buf, len);
  237. }
  238. static struct musb *the_musb;
  239. /* This is used by gadget drivers, and OTG transceiver logic, allowing
  240. * at most mA current to be drawn from VBUS during a Default-B session
  241. * (that is, while VBUS exceeds 4.4V). In Default-A (including pure host
  242. * mode), or low power Default-B sessions, something else supplies power.
  243. * Caller must take care of locking.
  244. */
  245. static int tusb_draw_power(struct usb_phy *x, unsigned mA)
  246. {
  247. struct musb *musb = the_musb;
  248. void __iomem *tbase = musb->ctrl_base;
  249. u32 reg;
  250. /* tps65030 seems to consume max 100mA, with maybe 60mA available
  251. * (measured on one board) for things other than tps and tusb.
  252. *
  253. * Boards sharing the CPU clock with CLKIN will need to prevent
  254. * certain idle sleep states while the USB link is active.
  255. *
  256. * REVISIT we could use VBUS to supply only _one_ of { 1.5V, 3.3V }.
  257. * The actual current usage would be very board-specific. For now,
  258. * it's simpler to just use an aggregate (also board-specific).
  259. */
  260. if (x->otg->default_a || mA < (musb->min_power << 1))
  261. mA = 0;
  262. reg = musb_readl(tbase, TUSB_PRCM_MNGMT);
  263. if (mA) {
  264. musb->is_bus_powered = 1;
  265. reg |= TUSB_PRCM_MNGMT_15_SW_EN | TUSB_PRCM_MNGMT_33_SW_EN;
  266. } else {
  267. musb->is_bus_powered = 0;
  268. reg &= ~(TUSB_PRCM_MNGMT_15_SW_EN | TUSB_PRCM_MNGMT_33_SW_EN);
  269. }
  270. musb_writel(tbase, TUSB_PRCM_MNGMT, reg);
  271. dev_dbg(musb->controller, "draw max %d mA VBUS\n", mA);
  272. return 0;
  273. }
  274. /* workaround for issue 13: change clock during chip idle
  275. * (to be fixed in rev3 silicon) ... symptoms include disconnect
  276. * or looping suspend/resume cycles
  277. */
  278. static void tusb_set_clock_source(struct musb *musb, unsigned mode)
  279. {
  280. void __iomem *tbase = musb->ctrl_base;
  281. u32 reg;
  282. reg = musb_readl(tbase, TUSB_PRCM_CONF);
  283. reg &= ~TUSB_PRCM_CONF_SYS_CLKSEL(0x3);
  284. /* 0 = refclk (clkin, XI)
  285. * 1 = PHY 60 MHz (internal PLL)
  286. * 2 = not supported
  287. * 3 = what?
  288. */
  289. if (mode > 0)
  290. reg |= TUSB_PRCM_CONF_SYS_CLKSEL(mode & 0x3);
  291. musb_writel(tbase, TUSB_PRCM_CONF, reg);
  292. /* FIXME tusb6010_platform_retime(mode == 0); */
  293. }
  294. /*
  295. * Idle TUSB6010 until next wake-up event; NOR access always wakes.
  296. * Other code ensures that we idle unless we're connected _and_ the
  297. * USB link is not suspended ... and tells us the relevant wakeup
  298. * events. SW_EN for voltage is handled separately.
  299. */
  300. static void tusb_allow_idle(struct musb *musb, u32 wakeup_enables)
  301. {
  302. void __iomem *tbase = musb->ctrl_base;
  303. u32 reg;
  304. if ((wakeup_enables & TUSB_PRCM_WBUS)
  305. && (tusb_get_revision(musb) == TUSB_REV_30))
  306. tusb_wbus_quirk(musb, 1);
  307. tusb_set_clock_source(musb, 0);
  308. wakeup_enables |= TUSB_PRCM_WNORCS;
  309. musb_writel(tbase, TUSB_PRCM_WAKEUP_MASK, ~wakeup_enables);
  310. /* REVISIT writeup of WID implies that if WID set and ID is grounded,
  311. * TUSB_PHY_OTG_CTRL.TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP must be cleared.
  312. * Presumably that's mostly to save power, hence WID is immaterial ...
  313. */
  314. reg = musb_readl(tbase, TUSB_PRCM_MNGMT);
  315. /* issue 4: when driving vbus, use hipower (vbus_det) comparator */
  316. if (is_host_active(musb)) {
  317. reg |= TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN;
  318. reg &= ~TUSB_PRCM_MNGMT_OTG_SESS_END_EN;
  319. } else {
  320. reg |= TUSB_PRCM_MNGMT_OTG_SESS_END_EN;
  321. reg &= ~TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN;
  322. }
  323. reg |= TUSB_PRCM_MNGMT_PM_IDLE | TUSB_PRCM_MNGMT_DEV_IDLE;
  324. musb_writel(tbase, TUSB_PRCM_MNGMT, reg);
  325. dev_dbg(musb->controller, "idle, wake on %02x\n", wakeup_enables);
  326. }
  327. /*
  328. * Updates cable VBUS status. Caller must take care of locking.
  329. */
  330. static int tusb_musb_vbus_status(struct musb *musb)
  331. {
  332. void __iomem *tbase = musb->ctrl_base;
  333. u32 otg_stat, prcm_mngmt;
  334. int ret = 0;
  335. otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
  336. prcm_mngmt = musb_readl(tbase, TUSB_PRCM_MNGMT);
  337. /* Temporarily enable VBUS detection if it was disabled for
  338. * suspend mode. Unless it's enabled otg_stat and devctl will
  339. * not show correct VBUS state.
  340. */
  341. if (!(prcm_mngmt & TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN)) {
  342. u32 tmp = prcm_mngmt;
  343. tmp |= TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN;
  344. musb_writel(tbase, TUSB_PRCM_MNGMT, tmp);
  345. otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
  346. musb_writel(tbase, TUSB_PRCM_MNGMT, prcm_mngmt);
  347. }
  348. if (otg_stat & TUSB_DEV_OTG_STAT_VBUS_VALID)
  349. ret = 1;
  350. return ret;
  351. }
  352. static struct timer_list musb_idle_timer;
  353. static void musb_do_idle(unsigned long _musb)
  354. {
  355. struct musb *musb = (void *)_musb;
  356. unsigned long flags;
  357. spin_lock_irqsave(&musb->lock, flags);
  358. switch (musb->xceiv->state) {
  359. case OTG_STATE_A_WAIT_BCON:
  360. if ((musb->a_wait_bcon != 0)
  361. && (musb->idle_timeout == 0
  362. || time_after(jiffies, musb->idle_timeout))) {
  363. dev_dbg(musb->controller, "Nothing connected %s, turning off VBUS\n",
  364. otg_state_string(musb->xceiv->state));
  365. }
  366. /* FALLTHROUGH */
  367. case OTG_STATE_A_IDLE:
  368. tusb_musb_set_vbus(musb, 0);
  369. default:
  370. break;
  371. }
  372. if (!musb->is_active) {
  373. u32 wakeups;
  374. /* wait until khubd handles port change status */
  375. if (is_host_active(musb) && (musb->port1_status >> 16))
  376. goto done;
  377. if (is_peripheral_enabled(musb) && !musb->gadget_driver) {
  378. wakeups = 0;
  379. } else {
  380. wakeups = TUSB_PRCM_WHOSTDISCON
  381. | TUSB_PRCM_WBUS
  382. | TUSB_PRCM_WVBUS;
  383. if (is_otg_enabled(musb))
  384. wakeups |= TUSB_PRCM_WID;
  385. }
  386. tusb_allow_idle(musb, wakeups);
  387. }
  388. done:
  389. spin_unlock_irqrestore(&musb->lock, flags);
  390. }
  391. /*
  392. * Maybe put TUSB6010 into idle mode mode depending on USB link status,
  393. * like "disconnected" or "suspended". We'll be woken out of it by
  394. * connect, resume, or disconnect.
  395. *
  396. * Needs to be called as the last function everywhere where there is
  397. * register access to TUSB6010 because of NOR flash wake-up.
  398. * Caller should own controller spinlock.
  399. *
  400. * Delay because peripheral enables D+ pullup 3msec after SE0, and
  401. * we don't want to treat that full speed J as a wakeup event.
  402. * ... peripherals must draw only suspend current after 10 msec.
  403. */
  404. static void tusb_musb_try_idle(struct musb *musb, unsigned long timeout)
  405. {
  406. unsigned long default_timeout = jiffies + msecs_to_jiffies(3);
  407. static unsigned long last_timer;
  408. if (timeout == 0)
  409. timeout = default_timeout;
  410. /* Never idle if active, or when VBUS timeout is not set as host */
  411. if (musb->is_active || ((musb->a_wait_bcon == 0)
  412. && (musb->xceiv->state == OTG_STATE_A_WAIT_BCON))) {
  413. dev_dbg(musb->controller, "%s active, deleting timer\n",
  414. otg_state_string(musb->xceiv->state));
  415. del_timer(&musb_idle_timer);
  416. last_timer = jiffies;
  417. return;
  418. }
  419. if (time_after(last_timer, timeout)) {
  420. if (!timer_pending(&musb_idle_timer))
  421. last_timer = timeout;
  422. else {
  423. dev_dbg(musb->controller, "Longer idle timer already pending, ignoring\n");
  424. return;
  425. }
  426. }
  427. last_timer = timeout;
  428. dev_dbg(musb->controller, "%s inactive, for idle timer for %lu ms\n",
  429. otg_state_string(musb->xceiv->state),
  430. (unsigned long)jiffies_to_msecs(timeout - jiffies));
  431. mod_timer(&musb_idle_timer, timeout);
  432. }
  433. /* ticks of 60 MHz clock */
  434. #define DEVCLOCK 60000000
  435. #define OTG_TIMER_MS(msecs) ((msecs) \
  436. ? (TUSB_DEV_OTG_TIMER_VAL((DEVCLOCK/1000)*(msecs)) \
  437. | TUSB_DEV_OTG_TIMER_ENABLE) \
  438. : 0)
  439. static void tusb_musb_set_vbus(struct musb *musb, int is_on)
  440. {
  441. void __iomem *tbase = musb->ctrl_base;
  442. u32 conf, prcm, timer;
  443. u8 devctl;
  444. struct usb_otg *otg = musb->xceiv->otg;
  445. /* HDRC controls CPEN, but beware current surges during device
  446. * connect. They can trigger transient overcurrent conditions
  447. * that must be ignored.
  448. */
  449. prcm = musb_readl(tbase, TUSB_PRCM_MNGMT);
  450. conf = musb_readl(tbase, TUSB_DEV_CONF);
  451. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  452. if (is_on) {
  453. timer = OTG_TIMER_MS(OTG_TIME_A_WAIT_VRISE);
  454. otg->default_a = 1;
  455. musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
  456. devctl |= MUSB_DEVCTL_SESSION;
  457. conf |= TUSB_DEV_CONF_USB_HOST_MODE;
  458. MUSB_HST_MODE(musb);
  459. } else {
  460. u32 otg_stat;
  461. timer = 0;
  462. /* If ID pin is grounded, we want to be a_idle */
  463. otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
  464. if (!(otg_stat & TUSB_DEV_OTG_STAT_ID_STATUS)) {
  465. switch (musb->xceiv->state) {
  466. case OTG_STATE_A_WAIT_VRISE:
  467. case OTG_STATE_A_WAIT_BCON:
  468. musb->xceiv->state = OTG_STATE_A_WAIT_VFALL;
  469. break;
  470. case OTG_STATE_A_WAIT_VFALL:
  471. musb->xceiv->state = OTG_STATE_A_IDLE;
  472. break;
  473. default:
  474. musb->xceiv->state = OTG_STATE_A_IDLE;
  475. }
  476. musb->is_active = 0;
  477. otg->default_a = 1;
  478. MUSB_HST_MODE(musb);
  479. } else {
  480. musb->is_active = 0;
  481. otg->default_a = 0;
  482. musb->xceiv->state = OTG_STATE_B_IDLE;
  483. MUSB_DEV_MODE(musb);
  484. }
  485. devctl &= ~MUSB_DEVCTL_SESSION;
  486. conf &= ~TUSB_DEV_CONF_USB_HOST_MODE;
  487. }
  488. prcm &= ~(TUSB_PRCM_MNGMT_15_SW_EN | TUSB_PRCM_MNGMT_33_SW_EN);
  489. musb_writel(tbase, TUSB_PRCM_MNGMT, prcm);
  490. musb_writel(tbase, TUSB_DEV_OTG_TIMER, timer);
  491. musb_writel(tbase, TUSB_DEV_CONF, conf);
  492. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  493. dev_dbg(musb->controller, "VBUS %s, devctl %02x otg %3x conf %08x prcm %08x\n",
  494. otg_state_string(musb->xceiv->state),
  495. musb_readb(musb->mregs, MUSB_DEVCTL),
  496. musb_readl(tbase, TUSB_DEV_OTG_STAT),
  497. conf, prcm);
  498. }
  499. /*
  500. * Sets the mode to OTG, peripheral or host by changing the ID detection.
  501. * Caller must take care of locking.
  502. *
  503. * Note that if a mini-A cable is plugged in the ID line will stay down as
  504. * the weak ID pull-up is not able to pull the ID up.
  505. *
  506. * REVISIT: It would be possible to add support for changing between host
  507. * and peripheral modes in non-OTG configurations by reconfiguring hardware
  508. * and then setting musb->board_mode. For now, only support OTG mode.
  509. */
  510. static int tusb_musb_set_mode(struct musb *musb, u8 musb_mode)
  511. {
  512. void __iomem *tbase = musb->ctrl_base;
  513. u32 otg_stat, phy_otg_ctrl, phy_otg_ena, dev_conf;
  514. if (musb->board_mode != MUSB_OTG) {
  515. ERR("Changing mode currently only supported in OTG mode\n");
  516. return -EINVAL;
  517. }
  518. otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
  519. phy_otg_ctrl = musb_readl(tbase, TUSB_PHY_OTG_CTRL);
  520. phy_otg_ena = musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE);
  521. dev_conf = musb_readl(tbase, TUSB_DEV_CONF);
  522. switch (musb_mode) {
  523. case MUSB_HOST: /* Disable PHY ID detect, ground ID */
  524. phy_otg_ctrl &= ~TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
  525. phy_otg_ena |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
  526. dev_conf |= TUSB_DEV_CONF_ID_SEL;
  527. dev_conf &= ~TUSB_DEV_CONF_SOFT_ID;
  528. break;
  529. case MUSB_PERIPHERAL: /* Disable PHY ID detect, keep ID pull-up on */
  530. phy_otg_ctrl |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
  531. phy_otg_ena |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
  532. dev_conf |= (TUSB_DEV_CONF_ID_SEL | TUSB_DEV_CONF_SOFT_ID);
  533. break;
  534. case MUSB_OTG: /* Use PHY ID detection */
  535. phy_otg_ctrl |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
  536. phy_otg_ena |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
  537. dev_conf &= ~(TUSB_DEV_CONF_ID_SEL | TUSB_DEV_CONF_SOFT_ID);
  538. break;
  539. default:
  540. dev_dbg(musb->controller, "Trying to set mode %i\n", musb_mode);
  541. return -EINVAL;
  542. }
  543. musb_writel(tbase, TUSB_PHY_OTG_CTRL,
  544. TUSB_PHY_OTG_CTRL_WRPROTECT | phy_otg_ctrl);
  545. musb_writel(tbase, TUSB_PHY_OTG_CTRL_ENABLE,
  546. TUSB_PHY_OTG_CTRL_WRPROTECT | phy_otg_ena);
  547. musb_writel(tbase, TUSB_DEV_CONF, dev_conf);
  548. otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
  549. if ((musb_mode == MUSB_PERIPHERAL) &&
  550. !(otg_stat & TUSB_DEV_OTG_STAT_ID_STATUS))
  551. INFO("Cannot be peripheral with mini-A cable "
  552. "otg_stat: %08x\n", otg_stat);
  553. return 0;
  554. }
  555. static inline unsigned long
  556. tusb_otg_ints(struct musb *musb, u32 int_src, void __iomem *tbase)
  557. {
  558. u32 otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
  559. unsigned long idle_timeout = 0;
  560. struct usb_otg *otg = musb->xceiv->otg;
  561. /* ID pin */
  562. if ((int_src & TUSB_INT_SRC_ID_STATUS_CHNG)) {
  563. int default_a;
  564. if (is_otg_enabled(musb))
  565. default_a = !(otg_stat & TUSB_DEV_OTG_STAT_ID_STATUS);
  566. else
  567. default_a = is_host_enabled(musb);
  568. dev_dbg(musb->controller, "Default-%c\n", default_a ? 'A' : 'B');
  569. otg->default_a = default_a;
  570. tusb_musb_set_vbus(musb, default_a);
  571. /* Don't allow idling immediately */
  572. if (default_a)
  573. idle_timeout = jiffies + (HZ * 3);
  574. }
  575. /* VBUS state change */
  576. if (int_src & TUSB_INT_SRC_VBUS_SENSE_CHNG) {
  577. /* B-dev state machine: no vbus ~= disconnect */
  578. if ((is_otg_enabled(musb) && !otg->default_a)
  579. || !is_host_enabled(musb)) {
  580. /* ? musb_root_disconnect(musb); */
  581. musb->port1_status &=
  582. ~(USB_PORT_STAT_CONNECTION
  583. | USB_PORT_STAT_ENABLE
  584. | USB_PORT_STAT_LOW_SPEED
  585. | USB_PORT_STAT_HIGH_SPEED
  586. | USB_PORT_STAT_TEST
  587. );
  588. if (otg_stat & TUSB_DEV_OTG_STAT_SESS_END) {
  589. dev_dbg(musb->controller, "Forcing disconnect (no interrupt)\n");
  590. if (musb->xceiv->state != OTG_STATE_B_IDLE) {
  591. /* INTR_DISCONNECT can hide... */
  592. musb->xceiv->state = OTG_STATE_B_IDLE;
  593. musb->int_usb |= MUSB_INTR_DISCONNECT;
  594. }
  595. musb->is_active = 0;
  596. }
  597. dev_dbg(musb->controller, "vbus change, %s, otg %03x\n",
  598. otg_state_string(musb->xceiv->state), otg_stat);
  599. idle_timeout = jiffies + (1 * HZ);
  600. schedule_work(&musb->irq_work);
  601. } else /* A-dev state machine */ {
  602. dev_dbg(musb->controller, "vbus change, %s, otg %03x\n",
  603. otg_state_string(musb->xceiv->state), otg_stat);
  604. switch (musb->xceiv->state) {
  605. case OTG_STATE_A_IDLE:
  606. dev_dbg(musb->controller, "Got SRP, turning on VBUS\n");
  607. musb_platform_set_vbus(musb, 1);
  608. /* CONNECT can wake if a_wait_bcon is set */
  609. if (musb->a_wait_bcon != 0)
  610. musb->is_active = 0;
  611. else
  612. musb->is_active = 1;
  613. /*
  614. * OPT FS A TD.4.6 needs few seconds for
  615. * A_WAIT_VRISE
  616. */
  617. idle_timeout = jiffies + (2 * HZ);
  618. break;
  619. case OTG_STATE_A_WAIT_VRISE:
  620. /* ignore; A-session-valid < VBUS_VALID/2,
  621. * we monitor this with the timer
  622. */
  623. break;
  624. case OTG_STATE_A_WAIT_VFALL:
  625. /* REVISIT this irq triggers during short
  626. * spikes caused by enumeration ...
  627. */
  628. if (musb->vbuserr_retry) {
  629. musb->vbuserr_retry--;
  630. tusb_musb_set_vbus(musb, 1);
  631. } else {
  632. musb->vbuserr_retry
  633. = VBUSERR_RETRY_COUNT;
  634. tusb_musb_set_vbus(musb, 0);
  635. }
  636. break;
  637. default:
  638. break;
  639. }
  640. }
  641. }
  642. /* OTG timer expiration */
  643. if (int_src & TUSB_INT_SRC_OTG_TIMEOUT) {
  644. u8 devctl;
  645. dev_dbg(musb->controller, "%s timer, %03x\n",
  646. otg_state_string(musb->xceiv->state), otg_stat);
  647. switch (musb->xceiv->state) {
  648. case OTG_STATE_A_WAIT_VRISE:
  649. /* VBUS has probably been valid for a while now,
  650. * but may well have bounced out of range a bit
  651. */
  652. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  653. if (otg_stat & TUSB_DEV_OTG_STAT_VBUS_VALID) {
  654. if ((devctl & MUSB_DEVCTL_VBUS)
  655. != MUSB_DEVCTL_VBUS) {
  656. dev_dbg(musb->controller, "devctl %02x\n", devctl);
  657. break;
  658. }
  659. musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
  660. musb->is_active = 0;
  661. idle_timeout = jiffies
  662. + msecs_to_jiffies(musb->a_wait_bcon);
  663. } else {
  664. /* REVISIT report overcurrent to hub? */
  665. ERR("vbus too slow, devctl %02x\n", devctl);
  666. tusb_musb_set_vbus(musb, 0);
  667. }
  668. break;
  669. case OTG_STATE_A_WAIT_BCON:
  670. if (musb->a_wait_bcon != 0)
  671. idle_timeout = jiffies
  672. + msecs_to_jiffies(musb->a_wait_bcon);
  673. break;
  674. case OTG_STATE_A_SUSPEND:
  675. break;
  676. case OTG_STATE_B_WAIT_ACON:
  677. break;
  678. default:
  679. break;
  680. }
  681. }
  682. schedule_work(&musb->irq_work);
  683. return idle_timeout;
  684. }
  685. static irqreturn_t tusb_musb_interrupt(int irq, void *__hci)
  686. {
  687. struct musb *musb = __hci;
  688. void __iomem *tbase = musb->ctrl_base;
  689. unsigned long flags, idle_timeout = 0;
  690. u32 int_mask, int_src;
  691. spin_lock_irqsave(&musb->lock, flags);
  692. /* Mask all interrupts to allow using both edge and level GPIO irq */
  693. int_mask = musb_readl(tbase, TUSB_INT_MASK);
  694. musb_writel(tbase, TUSB_INT_MASK, ~TUSB_INT_MASK_RESERVED_BITS);
  695. int_src = musb_readl(tbase, TUSB_INT_SRC) & ~TUSB_INT_SRC_RESERVED_BITS;
  696. dev_dbg(musb->controller, "TUSB IRQ %08x\n", int_src);
  697. musb->int_usb = (u8) int_src;
  698. /* Acknowledge wake-up source interrupts */
  699. if (int_src & TUSB_INT_SRC_DEV_WAKEUP) {
  700. u32 reg;
  701. u32 i;
  702. if (tusb_get_revision(musb) == TUSB_REV_30)
  703. tusb_wbus_quirk(musb, 0);
  704. /* there are issues re-locking the PLL on wakeup ... */
  705. /* work around issue 8 */
  706. for (i = 0xf7f7f7; i > 0xf7f7f7 - 1000; i--) {
  707. musb_writel(tbase, TUSB_SCRATCH_PAD, 0);
  708. musb_writel(tbase, TUSB_SCRATCH_PAD, i);
  709. reg = musb_readl(tbase, TUSB_SCRATCH_PAD);
  710. if (reg == i)
  711. break;
  712. dev_dbg(musb->controller, "TUSB NOR not ready\n");
  713. }
  714. /* work around issue 13 (2nd half) */
  715. tusb_set_clock_source(musb, 1);
  716. reg = musb_readl(tbase, TUSB_PRCM_WAKEUP_SOURCE);
  717. musb_writel(tbase, TUSB_PRCM_WAKEUP_CLEAR, reg);
  718. if (reg & ~TUSB_PRCM_WNORCS) {
  719. musb->is_active = 1;
  720. schedule_work(&musb->irq_work);
  721. }
  722. dev_dbg(musb->controller, "wake %sactive %02x\n",
  723. musb->is_active ? "" : "in", reg);
  724. /* REVISIT host side TUSB_PRCM_WHOSTDISCON, TUSB_PRCM_WBUS */
  725. }
  726. if (int_src & TUSB_INT_SRC_USB_IP_CONN)
  727. del_timer(&musb_idle_timer);
  728. /* OTG state change reports (annoyingly) not issued by Mentor core */
  729. if (int_src & (TUSB_INT_SRC_VBUS_SENSE_CHNG
  730. | TUSB_INT_SRC_OTG_TIMEOUT
  731. | TUSB_INT_SRC_ID_STATUS_CHNG))
  732. idle_timeout = tusb_otg_ints(musb, int_src, tbase);
  733. /* TX dma callback must be handled here, RX dma callback is
  734. * handled in tusb_omap_dma_cb.
  735. */
  736. if ((int_src & TUSB_INT_SRC_TXRX_DMA_DONE)) {
  737. u32 dma_src = musb_readl(tbase, TUSB_DMA_INT_SRC);
  738. u32 real_dma_src = musb_readl(tbase, TUSB_DMA_INT_MASK);
  739. dev_dbg(musb->controller, "DMA IRQ %08x\n", dma_src);
  740. real_dma_src = ~real_dma_src & dma_src;
  741. if (tusb_dma_omap() && real_dma_src) {
  742. int tx_source = (real_dma_src & 0xffff);
  743. int i;
  744. for (i = 1; i <= 15; i++) {
  745. if (tx_source & (1 << i)) {
  746. dev_dbg(musb->controller, "completing ep%i %s\n", i, "tx");
  747. musb_dma_completion(musb, i, 1);
  748. }
  749. }
  750. }
  751. musb_writel(tbase, TUSB_DMA_INT_CLEAR, dma_src);
  752. }
  753. /* EP interrupts. In OCP mode tusb6010 mirrors the MUSB interrupts */
  754. if (int_src & (TUSB_INT_SRC_USB_IP_TX | TUSB_INT_SRC_USB_IP_RX)) {
  755. u32 musb_src = musb_readl(tbase, TUSB_USBIP_INT_SRC);
  756. musb_writel(tbase, TUSB_USBIP_INT_CLEAR, musb_src);
  757. musb->int_rx = (((musb_src >> 16) & 0xffff) << 1);
  758. musb->int_tx = (musb_src & 0xffff);
  759. } else {
  760. musb->int_rx = 0;
  761. musb->int_tx = 0;
  762. }
  763. if (int_src & (TUSB_INT_SRC_USB_IP_TX | TUSB_INT_SRC_USB_IP_RX | 0xff))
  764. musb_interrupt(musb);
  765. /* Acknowledge TUSB interrupts. Clear only non-reserved bits */
  766. musb_writel(tbase, TUSB_INT_SRC_CLEAR,
  767. int_src & ~TUSB_INT_MASK_RESERVED_BITS);
  768. tusb_musb_try_idle(musb, idle_timeout);
  769. musb_writel(tbase, TUSB_INT_MASK, int_mask);
  770. spin_unlock_irqrestore(&musb->lock, flags);
  771. return IRQ_HANDLED;
  772. }
  773. static int dma_off;
  774. /*
  775. * Enables TUSB6010. Caller must take care of locking.
  776. * REVISIT:
  777. * - Check what is unnecessary in MGC_HdrcStart()
  778. */
  779. static void tusb_musb_enable(struct musb *musb)
  780. {
  781. void __iomem *tbase = musb->ctrl_base;
  782. /* Setup TUSB6010 main interrupt mask. Enable all interrupts except SOF.
  783. * REVISIT: Enable and deal with TUSB_INT_SRC_USB_IP_SOF */
  784. musb_writel(tbase, TUSB_INT_MASK, TUSB_INT_SRC_USB_IP_SOF);
  785. /* Setup TUSB interrupt, disable DMA and GPIO interrupts */
  786. musb_writel(tbase, TUSB_USBIP_INT_MASK, 0);
  787. musb_writel(tbase, TUSB_DMA_INT_MASK, 0x7fffffff);
  788. musb_writel(tbase, TUSB_GPIO_INT_MASK, 0x1ff);
  789. /* Clear all subsystem interrups */
  790. musb_writel(tbase, TUSB_USBIP_INT_CLEAR, 0x7fffffff);
  791. musb_writel(tbase, TUSB_DMA_INT_CLEAR, 0x7fffffff);
  792. musb_writel(tbase, TUSB_GPIO_INT_CLEAR, 0x1ff);
  793. /* Acknowledge pending interrupt(s) */
  794. musb_writel(tbase, TUSB_INT_SRC_CLEAR, ~TUSB_INT_MASK_RESERVED_BITS);
  795. /* Only 0 clock cycles for minimum interrupt de-assertion time and
  796. * interrupt polarity active low seems to work reliably here */
  797. musb_writel(tbase, TUSB_INT_CTRL_CONF,
  798. TUSB_INT_CTRL_CONF_INT_RELCYC(0));
  799. irq_set_irq_type(musb->nIrq, IRQ_TYPE_LEVEL_LOW);
  800. /* maybe force into the Default-A OTG state machine */
  801. if (!(musb_readl(tbase, TUSB_DEV_OTG_STAT)
  802. & TUSB_DEV_OTG_STAT_ID_STATUS))
  803. musb_writel(tbase, TUSB_INT_SRC_SET,
  804. TUSB_INT_SRC_ID_STATUS_CHNG);
  805. if (is_dma_capable() && dma_off)
  806. printk(KERN_WARNING "%s %s: dma not reactivated\n",
  807. __FILE__, __func__);
  808. else
  809. dma_off = 1;
  810. }
  811. /*
  812. * Disables TUSB6010. Caller must take care of locking.
  813. */
  814. static void tusb_musb_disable(struct musb *musb)
  815. {
  816. void __iomem *tbase = musb->ctrl_base;
  817. /* FIXME stop DMA, IRQs, timers, ... */
  818. /* disable all IRQs */
  819. musb_writel(tbase, TUSB_INT_MASK, ~TUSB_INT_MASK_RESERVED_BITS);
  820. musb_writel(tbase, TUSB_USBIP_INT_MASK, 0x7fffffff);
  821. musb_writel(tbase, TUSB_DMA_INT_MASK, 0x7fffffff);
  822. musb_writel(tbase, TUSB_GPIO_INT_MASK, 0x1ff);
  823. del_timer(&musb_idle_timer);
  824. if (is_dma_capable() && !dma_off) {
  825. printk(KERN_WARNING "%s %s: dma still active\n",
  826. __FILE__, __func__);
  827. dma_off = 1;
  828. }
  829. }
  830. /*
  831. * Sets up TUSB6010 CPU interface specific signals and registers
  832. * Note: Settings optimized for OMAP24xx
  833. */
  834. static void tusb_setup_cpu_interface(struct musb *musb)
  835. {
  836. void __iomem *tbase = musb->ctrl_base;
  837. /*
  838. * Disable GPIO[5:0] pullups (used as output DMA requests)
  839. * Don't disable GPIO[7:6] as they are needed for wake-up.
  840. */
  841. musb_writel(tbase, TUSB_PULLUP_1_CTRL, 0x0000003F);
  842. /* Disable all pullups on NOR IF, DMAREQ0 and DMAREQ1 */
  843. musb_writel(tbase, TUSB_PULLUP_2_CTRL, 0x01FFFFFF);
  844. /* Turn GPIO[5:0] to DMAREQ[5:0] signals */
  845. musb_writel(tbase, TUSB_GPIO_CONF, TUSB_GPIO_CONF_DMAREQ(0x3f));
  846. /* Burst size 16x16 bits, all six DMA requests enabled, DMA request
  847. * de-assertion time 2 system clocks p 62 */
  848. musb_writel(tbase, TUSB_DMA_REQ_CONF,
  849. TUSB_DMA_REQ_CONF_BURST_SIZE(2) |
  850. TUSB_DMA_REQ_CONF_DMA_REQ_EN(0x3f) |
  851. TUSB_DMA_REQ_CONF_DMA_REQ_ASSER(2));
  852. /* Set 0 wait count for synchronous burst access */
  853. musb_writel(tbase, TUSB_WAIT_COUNT, 1);
  854. }
  855. static int tusb_musb_start(struct musb *musb)
  856. {
  857. void __iomem *tbase = musb->ctrl_base;
  858. int ret = 0;
  859. unsigned long flags;
  860. u32 reg;
  861. if (musb->board_set_power)
  862. ret = musb->board_set_power(1);
  863. if (ret != 0) {
  864. printk(KERN_ERR "tusb: Cannot enable TUSB6010\n");
  865. return ret;
  866. }
  867. spin_lock_irqsave(&musb->lock, flags);
  868. if (musb_readl(tbase, TUSB_PROD_TEST_RESET) !=
  869. TUSB_PROD_TEST_RESET_VAL) {
  870. printk(KERN_ERR "tusb: Unable to detect TUSB6010\n");
  871. goto err;
  872. }
  873. ret = tusb_print_revision(musb);
  874. if (ret < 2) {
  875. printk(KERN_ERR "tusb: Unsupported TUSB6010 revision %i\n",
  876. ret);
  877. goto err;
  878. }
  879. /* The uint bit for "USB non-PDR interrupt enable" has to be 1 when
  880. * NOR FLASH interface is used */
  881. musb_writel(tbase, TUSB_VLYNQ_CTRL, 8);
  882. /* Select PHY free running 60MHz as a system clock */
  883. tusb_set_clock_source(musb, 1);
  884. /* VBus valid timer 1us, disable DFT/Debug and VLYNQ clocks for
  885. * power saving, enable VBus detect and session end comparators,
  886. * enable IDpullup, enable VBus charging */
  887. musb_writel(tbase, TUSB_PRCM_MNGMT,
  888. TUSB_PRCM_MNGMT_VBUS_VALID_TIMER(0xa) |
  889. TUSB_PRCM_MNGMT_VBUS_VALID_FLT_EN |
  890. TUSB_PRCM_MNGMT_OTG_SESS_END_EN |
  891. TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN |
  892. TUSB_PRCM_MNGMT_OTG_ID_PULLUP);
  893. tusb_setup_cpu_interface(musb);
  894. /* simplify: always sense/pullup ID pins, as if in OTG mode */
  895. reg = musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE);
  896. reg |= TUSB_PHY_OTG_CTRL_WRPROTECT | TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
  897. musb_writel(tbase, TUSB_PHY_OTG_CTRL_ENABLE, reg);
  898. reg = musb_readl(tbase, TUSB_PHY_OTG_CTRL);
  899. reg |= TUSB_PHY_OTG_CTRL_WRPROTECT | TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
  900. musb_writel(tbase, TUSB_PHY_OTG_CTRL, reg);
  901. spin_unlock_irqrestore(&musb->lock, flags);
  902. return 0;
  903. err:
  904. spin_unlock_irqrestore(&musb->lock, flags);
  905. if (musb->board_set_power)
  906. musb->board_set_power(0);
  907. return -ENODEV;
  908. }
  909. static int tusb_musb_init(struct musb *musb)
  910. {
  911. struct platform_device *pdev;
  912. struct resource *mem;
  913. void __iomem *sync = NULL;
  914. int ret;
  915. usb_nop_xceiv_register();
  916. musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
  917. if (IS_ERR_OR_NULL(musb->xceiv))
  918. return -ENODEV;
  919. pdev = to_platform_device(musb->controller);
  920. /* dma address for async dma */
  921. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  922. musb->async = mem->start;
  923. /* dma address for sync dma */
  924. mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  925. if (!mem) {
  926. pr_debug("no sync dma resource?\n");
  927. ret = -ENODEV;
  928. goto done;
  929. }
  930. musb->sync = mem->start;
  931. sync = ioremap(mem->start, resource_size(mem));
  932. if (!sync) {
  933. pr_debug("ioremap for sync failed\n");
  934. ret = -ENOMEM;
  935. goto done;
  936. }
  937. musb->sync_va = sync;
  938. /* Offsets from base: VLYNQ at 0x000, MUSB regs at 0x400,
  939. * FIFOs at 0x600, TUSB at 0x800
  940. */
  941. musb->mregs += TUSB_BASE_OFFSET;
  942. ret = tusb_musb_start(musb);
  943. if (ret) {
  944. printk(KERN_ERR "Could not start tusb6010 (%d)\n",
  945. ret);
  946. goto done;
  947. }
  948. musb->isr = tusb_musb_interrupt;
  949. if (is_peripheral_enabled(musb)) {
  950. musb->xceiv->set_power = tusb_draw_power;
  951. the_musb = musb;
  952. }
  953. setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
  954. done:
  955. if (ret < 0) {
  956. if (sync)
  957. iounmap(sync);
  958. usb_put_phy(musb->xceiv);
  959. usb_nop_xceiv_unregister();
  960. }
  961. return ret;
  962. }
  963. static int tusb_musb_exit(struct musb *musb)
  964. {
  965. del_timer_sync(&musb_idle_timer);
  966. the_musb = NULL;
  967. if (musb->board_set_power)
  968. musb->board_set_power(0);
  969. iounmap(musb->sync_va);
  970. usb_put_phy(musb->xceiv);
  971. usb_nop_xceiv_unregister();
  972. return 0;
  973. }
  974. static const struct musb_platform_ops tusb_ops = {
  975. .init = tusb_musb_init,
  976. .exit = tusb_musb_exit,
  977. .enable = tusb_musb_enable,
  978. .disable = tusb_musb_disable,
  979. .set_mode = tusb_musb_set_mode,
  980. .try_idle = tusb_musb_try_idle,
  981. .vbus_status = tusb_musb_vbus_status,
  982. .set_vbus = tusb_musb_set_vbus,
  983. };
  984. static u64 tusb_dmamask = DMA_BIT_MASK(32);
  985. static int __devinit tusb_probe(struct platform_device *pdev)
  986. {
  987. struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
  988. struct platform_device *musb;
  989. struct tusb6010_glue *glue;
  990. int ret = -ENOMEM;
  991. glue = kzalloc(sizeof(*glue), GFP_KERNEL);
  992. if (!glue) {
  993. dev_err(&pdev->dev, "failed to allocate glue context\n");
  994. goto err0;
  995. }
  996. musb = platform_device_alloc("musb-hdrc", -1);
  997. if (!musb) {
  998. dev_err(&pdev->dev, "failed to allocate musb device\n");
  999. goto err1;
  1000. }
  1001. musb->dev.parent = &pdev->dev;
  1002. musb->dev.dma_mask = &tusb_dmamask;
  1003. musb->dev.coherent_dma_mask = tusb_dmamask;
  1004. glue->dev = &pdev->dev;
  1005. glue->musb = musb;
  1006. pdata->platform_ops = &tusb_ops;
  1007. platform_set_drvdata(pdev, glue);
  1008. ret = platform_device_add_resources(musb, pdev->resource,
  1009. pdev->num_resources);
  1010. if (ret) {
  1011. dev_err(&pdev->dev, "failed to add resources\n");
  1012. goto err2;
  1013. }
  1014. ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
  1015. if (ret) {
  1016. dev_err(&pdev->dev, "failed to add platform_data\n");
  1017. goto err2;
  1018. }
  1019. ret = platform_device_add(musb);
  1020. if (ret) {
  1021. dev_err(&pdev->dev, "failed to register musb device\n");
  1022. goto err2;
  1023. }
  1024. return 0;
  1025. err2:
  1026. platform_device_put(musb);
  1027. err1:
  1028. kfree(glue);
  1029. err0:
  1030. return ret;
  1031. }
  1032. static int __devexit tusb_remove(struct platform_device *pdev)
  1033. {
  1034. struct tusb6010_glue *glue = platform_get_drvdata(pdev);
  1035. platform_device_del(glue->musb);
  1036. platform_device_put(glue->musb);
  1037. kfree(glue);
  1038. return 0;
  1039. }
  1040. static struct platform_driver tusb_driver = {
  1041. .probe = tusb_probe,
  1042. .remove = __devexit_p(tusb_remove),
  1043. .driver = {
  1044. .name = "musb-tusb",
  1045. },
  1046. };
  1047. MODULE_DESCRIPTION("TUSB6010 MUSB Glue Layer");
  1048. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  1049. MODULE_LICENSE("GPL v2");
  1050. static int __init tusb_init(void)
  1051. {
  1052. return platform_driver_register(&tusb_driver);
  1053. }
  1054. module_init(tusb_init);
  1055. static void __exit tusb_exit(void)
  1056. {
  1057. platform_driver_unregister(&tusb_driver);
  1058. }
  1059. module_exit(tusb_exit);