tusb6010.c 34 KB

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