tusb6010.c 34 KB

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