tusb6010.c 35 KB

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