tusb6010.c 34 KB

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