tusb6010.c 33 KB

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