tusb6010.c 33 KB

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