tusb6010.c 33 KB

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