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/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 __iomem *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 (is_peripheral_enabled(musb) && !musb->gadget_driver) {
  377. wakeups = 0;
  378. } else {
  379. wakeups = TUSB_PRCM_WHOSTDISCON
  380. | TUSB_PRCM_WBUS
  381. | TUSB_PRCM_WVBUS;
  382. if (is_otg_enabled(musb))
  383. wakeups |= TUSB_PRCM_WID;
  384. }
  385. tusb_allow_idle(musb, wakeups);
  386. }
  387. done:
  388. spin_unlock_irqrestore(&musb->lock, flags);
  389. }
  390. /*
  391. * Maybe put TUSB6010 into idle mode mode depending on USB link status,
  392. * like "disconnected" or "suspended". We'll be woken out of it by
  393. * connect, resume, or disconnect.
  394. *
  395. * Needs to be called as the last function everywhere where there is
  396. * register access to TUSB6010 because of NOR flash wake-up.
  397. * Caller should own controller spinlock.
  398. *
  399. * Delay because peripheral enables D+ pullup 3msec after SE0, and
  400. * we don't want to treat that full speed J as a wakeup event.
  401. * ... peripherals must draw only suspend current after 10 msec.
  402. */
  403. static void tusb_musb_try_idle(struct musb *musb, unsigned long timeout)
  404. {
  405. unsigned long default_timeout = jiffies + msecs_to_jiffies(3);
  406. static unsigned long last_timer;
  407. if (timeout == 0)
  408. timeout = default_timeout;
  409. /* Never idle if active, or when VBUS timeout is not set as host */
  410. if (musb->is_active || ((musb->a_wait_bcon == 0)
  411. && (musb->xceiv->state == OTG_STATE_A_WAIT_BCON))) {
  412. dev_dbg(musb->controller, "%s active, deleting timer\n",
  413. otg_state_string(musb->xceiv->state));
  414. del_timer(&musb_idle_timer);
  415. last_timer = jiffies;
  416. return;
  417. }
  418. if (time_after(last_timer, timeout)) {
  419. if (!timer_pending(&musb_idle_timer))
  420. last_timer = timeout;
  421. else {
  422. dev_dbg(musb->controller, "Longer idle timer already pending, ignoring\n");
  423. return;
  424. }
  425. }
  426. last_timer = timeout;
  427. dev_dbg(musb->controller, "%s inactive, for idle timer for %lu ms\n",
  428. otg_state_string(musb->xceiv->state),
  429. (unsigned long)jiffies_to_msecs(timeout - jiffies));
  430. mod_timer(&musb_idle_timer, timeout);
  431. }
  432. /* ticks of 60 MHz clock */
  433. #define DEVCLOCK 60000000
  434. #define OTG_TIMER_MS(msecs) ((msecs) \
  435. ? (TUSB_DEV_OTG_TIMER_VAL((DEVCLOCK/1000)*(msecs)) \
  436. | TUSB_DEV_OTG_TIMER_ENABLE) \
  437. : 0)
  438. static void tusb_musb_set_vbus(struct musb *musb, int is_on)
  439. {
  440. void __iomem *tbase = musb->ctrl_base;
  441. u32 conf, prcm, timer;
  442. u8 devctl;
  443. struct usb_otg *otg = musb->xceiv->otg;
  444. /* HDRC controls CPEN, but beware current surges during device
  445. * connect. They can trigger transient overcurrent conditions
  446. * that must be ignored.
  447. */
  448. prcm = musb_readl(tbase, TUSB_PRCM_MNGMT);
  449. conf = musb_readl(tbase, TUSB_DEV_CONF);
  450. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  451. if (is_on) {
  452. timer = OTG_TIMER_MS(OTG_TIME_A_WAIT_VRISE);
  453. otg->default_a = 1;
  454. musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
  455. devctl |= MUSB_DEVCTL_SESSION;
  456. conf |= TUSB_DEV_CONF_USB_HOST_MODE;
  457. MUSB_HST_MODE(musb);
  458. } else {
  459. u32 otg_stat;
  460. timer = 0;
  461. /* If ID pin is grounded, we want to be a_idle */
  462. otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
  463. if (!(otg_stat & TUSB_DEV_OTG_STAT_ID_STATUS)) {
  464. switch (musb->xceiv->state) {
  465. case OTG_STATE_A_WAIT_VRISE:
  466. case OTG_STATE_A_WAIT_BCON:
  467. musb->xceiv->state = OTG_STATE_A_WAIT_VFALL;
  468. break;
  469. case OTG_STATE_A_WAIT_VFALL:
  470. musb->xceiv->state = OTG_STATE_A_IDLE;
  471. break;
  472. default:
  473. musb->xceiv->state = OTG_STATE_A_IDLE;
  474. }
  475. musb->is_active = 0;
  476. otg->default_a = 1;
  477. MUSB_HST_MODE(musb);
  478. } else {
  479. musb->is_active = 0;
  480. otg->default_a = 0;
  481. musb->xceiv->state = OTG_STATE_B_IDLE;
  482. MUSB_DEV_MODE(musb);
  483. }
  484. devctl &= ~MUSB_DEVCTL_SESSION;
  485. conf &= ~TUSB_DEV_CONF_USB_HOST_MODE;
  486. }
  487. prcm &= ~(TUSB_PRCM_MNGMT_15_SW_EN | TUSB_PRCM_MNGMT_33_SW_EN);
  488. musb_writel(tbase, TUSB_PRCM_MNGMT, prcm);
  489. musb_writel(tbase, TUSB_DEV_OTG_TIMER, timer);
  490. musb_writel(tbase, TUSB_DEV_CONF, conf);
  491. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  492. dev_dbg(musb->controller, "VBUS %s, devctl %02x otg %3x conf %08x prcm %08x\n",
  493. otg_state_string(musb->xceiv->state),
  494. musb_readb(musb->mregs, MUSB_DEVCTL),
  495. musb_readl(tbase, TUSB_DEV_OTG_STAT),
  496. conf, prcm);
  497. }
  498. /*
  499. * Sets the mode to OTG, peripheral or host by changing the ID detection.
  500. * Caller must take care of locking.
  501. *
  502. * Note that if a mini-A cable is plugged in the ID line will stay down as
  503. * the weak ID pull-up is not able to pull the ID up.
  504. *
  505. * REVISIT: It would be possible to add support for changing between host
  506. * and peripheral modes in non-OTG configurations by reconfiguring hardware
  507. * and then setting musb->board_mode. For now, only support OTG mode.
  508. */
  509. static int tusb_musb_set_mode(struct musb *musb, u8 musb_mode)
  510. {
  511. void __iomem *tbase = musb->ctrl_base;
  512. u32 otg_stat, phy_otg_ctrl, phy_otg_ena, dev_conf;
  513. if (musb->board_mode != MUSB_OTG) {
  514. ERR("Changing mode currently only supported in OTG mode\n");
  515. return -EINVAL;
  516. }
  517. otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
  518. phy_otg_ctrl = musb_readl(tbase, TUSB_PHY_OTG_CTRL);
  519. phy_otg_ena = musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE);
  520. dev_conf = musb_readl(tbase, TUSB_DEV_CONF);
  521. switch (musb_mode) {
  522. case MUSB_HOST: /* Disable PHY ID detect, ground ID */
  523. phy_otg_ctrl &= ~TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
  524. phy_otg_ena |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
  525. dev_conf |= TUSB_DEV_CONF_ID_SEL;
  526. dev_conf &= ~TUSB_DEV_CONF_SOFT_ID;
  527. break;
  528. case MUSB_PERIPHERAL: /* Disable PHY ID detect, keep ID pull-up on */
  529. phy_otg_ctrl |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
  530. phy_otg_ena |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
  531. dev_conf |= (TUSB_DEV_CONF_ID_SEL | TUSB_DEV_CONF_SOFT_ID);
  532. break;
  533. case MUSB_OTG: /* Use PHY ID detection */
  534. phy_otg_ctrl |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
  535. phy_otg_ena |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
  536. dev_conf &= ~(TUSB_DEV_CONF_ID_SEL | TUSB_DEV_CONF_SOFT_ID);
  537. break;
  538. default:
  539. dev_dbg(musb->controller, "Trying to set mode %i\n", musb_mode);
  540. return -EINVAL;
  541. }
  542. musb_writel(tbase, TUSB_PHY_OTG_CTRL,
  543. TUSB_PHY_OTG_CTRL_WRPROTECT | phy_otg_ctrl);
  544. musb_writel(tbase, TUSB_PHY_OTG_CTRL_ENABLE,
  545. TUSB_PHY_OTG_CTRL_WRPROTECT | phy_otg_ena);
  546. musb_writel(tbase, TUSB_DEV_CONF, dev_conf);
  547. otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
  548. if ((musb_mode == MUSB_PERIPHERAL) &&
  549. !(otg_stat & TUSB_DEV_OTG_STAT_ID_STATUS))
  550. INFO("Cannot be peripheral with mini-A cable "
  551. "otg_stat: %08x\n", otg_stat);
  552. return 0;
  553. }
  554. static inline unsigned long
  555. tusb_otg_ints(struct musb *musb, u32 int_src, void __iomem *tbase)
  556. {
  557. u32 otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
  558. unsigned long idle_timeout = 0;
  559. struct usb_otg *otg = musb->xceiv->otg;
  560. /* ID pin */
  561. if ((int_src & TUSB_INT_SRC_ID_STATUS_CHNG)) {
  562. int default_a;
  563. if (is_otg_enabled(musb))
  564. default_a = !(otg_stat & TUSB_DEV_OTG_STAT_ID_STATUS);
  565. else
  566. default_a = is_host_enabled(musb);
  567. dev_dbg(musb->controller, "Default-%c\n", default_a ? 'A' : 'B');
  568. otg->default_a = default_a;
  569. tusb_musb_set_vbus(musb, default_a);
  570. /* Don't allow idling immediately */
  571. if (default_a)
  572. idle_timeout = jiffies + (HZ * 3);
  573. }
  574. /* VBUS state change */
  575. if (int_src & TUSB_INT_SRC_VBUS_SENSE_CHNG) {
  576. /* B-dev state machine: no vbus ~= disconnect */
  577. if ((is_otg_enabled(musb) && !otg->default_a)
  578. || !is_host_enabled(musb)) {
  579. /* ? musb_root_disconnect(musb); */
  580. musb->port1_status &=
  581. ~(USB_PORT_STAT_CONNECTION
  582. | USB_PORT_STAT_ENABLE
  583. | USB_PORT_STAT_LOW_SPEED
  584. | USB_PORT_STAT_HIGH_SPEED
  585. | USB_PORT_STAT_TEST
  586. );
  587. if (otg_stat & TUSB_DEV_OTG_STAT_SESS_END) {
  588. dev_dbg(musb->controller, "Forcing disconnect (no interrupt)\n");
  589. if (musb->xceiv->state != OTG_STATE_B_IDLE) {
  590. /* INTR_DISCONNECT can hide... */
  591. musb->xceiv->state = OTG_STATE_B_IDLE;
  592. musb->int_usb |= MUSB_INTR_DISCONNECT;
  593. }
  594. musb->is_active = 0;
  595. }
  596. dev_dbg(musb->controller, "vbus change, %s, otg %03x\n",
  597. otg_state_string(musb->xceiv->state), otg_stat);
  598. idle_timeout = jiffies + (1 * HZ);
  599. schedule_work(&musb->irq_work);
  600. } else /* A-dev state machine */ {
  601. dev_dbg(musb->controller, "vbus change, %s, otg %03x\n",
  602. otg_state_string(musb->xceiv->state), otg_stat);
  603. switch (musb->xceiv->state) {
  604. case OTG_STATE_A_IDLE:
  605. dev_dbg(musb->controller, "Got SRP, turning on VBUS\n");
  606. musb_platform_set_vbus(musb, 1);
  607. /* CONNECT can wake if a_wait_bcon is set */
  608. if (musb->a_wait_bcon != 0)
  609. musb->is_active = 0;
  610. else
  611. musb->is_active = 1;
  612. /*
  613. * OPT FS A TD.4.6 needs few seconds for
  614. * A_WAIT_VRISE
  615. */
  616. idle_timeout = jiffies + (2 * HZ);
  617. break;
  618. case OTG_STATE_A_WAIT_VRISE:
  619. /* ignore; A-session-valid < VBUS_VALID/2,
  620. * we monitor this with the timer
  621. */
  622. break;
  623. case OTG_STATE_A_WAIT_VFALL:
  624. /* REVISIT this irq triggers during short
  625. * spikes caused by enumeration ...
  626. */
  627. if (musb->vbuserr_retry) {
  628. musb->vbuserr_retry--;
  629. tusb_musb_set_vbus(musb, 1);
  630. } else {
  631. musb->vbuserr_retry
  632. = VBUSERR_RETRY_COUNT;
  633. tusb_musb_set_vbus(musb, 0);
  634. }
  635. break;
  636. default:
  637. break;
  638. }
  639. }
  640. }
  641. /* OTG timer expiration */
  642. if (int_src & TUSB_INT_SRC_OTG_TIMEOUT) {
  643. u8 devctl;
  644. dev_dbg(musb->controller, "%s timer, %03x\n",
  645. otg_state_string(musb->xceiv->state), otg_stat);
  646. switch (musb->xceiv->state) {
  647. case OTG_STATE_A_WAIT_VRISE:
  648. /* VBUS has probably been valid for a while now,
  649. * but may well have bounced out of range a bit
  650. */
  651. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  652. if (otg_stat & TUSB_DEV_OTG_STAT_VBUS_VALID) {
  653. if ((devctl & MUSB_DEVCTL_VBUS)
  654. != MUSB_DEVCTL_VBUS) {
  655. dev_dbg(musb->controller, "devctl %02x\n", devctl);
  656. break;
  657. }
  658. musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
  659. musb->is_active = 0;
  660. idle_timeout = jiffies
  661. + msecs_to_jiffies(musb->a_wait_bcon);
  662. } else {
  663. /* REVISIT report overcurrent to hub? */
  664. ERR("vbus too slow, devctl %02x\n", devctl);
  665. tusb_musb_set_vbus(musb, 0);
  666. }
  667. break;
  668. case OTG_STATE_A_WAIT_BCON:
  669. if (musb->a_wait_bcon != 0)
  670. idle_timeout = jiffies
  671. + msecs_to_jiffies(musb->a_wait_bcon);
  672. break;
  673. case OTG_STATE_A_SUSPEND:
  674. break;
  675. case OTG_STATE_B_WAIT_ACON:
  676. break;
  677. default:
  678. break;
  679. }
  680. }
  681. schedule_work(&musb->irq_work);
  682. return idle_timeout;
  683. }
  684. static irqreturn_t tusb_musb_interrupt(int irq, void *__hci)
  685. {
  686. struct musb *musb = __hci;
  687. void __iomem *tbase = musb->ctrl_base;
  688. unsigned long flags, idle_timeout = 0;
  689. u32 int_mask, int_src;
  690. spin_lock_irqsave(&musb->lock, flags);
  691. /* Mask all interrupts to allow using both edge and level GPIO irq */
  692. int_mask = musb_readl(tbase, TUSB_INT_MASK);
  693. musb_writel(tbase, TUSB_INT_MASK, ~TUSB_INT_MASK_RESERVED_BITS);
  694. int_src = musb_readl(tbase, TUSB_INT_SRC) & ~TUSB_INT_SRC_RESERVED_BITS;
  695. dev_dbg(musb->controller, "TUSB IRQ %08x\n", int_src);
  696. musb->int_usb = (u8) int_src;
  697. /* Acknowledge wake-up source interrupts */
  698. if (int_src & TUSB_INT_SRC_DEV_WAKEUP) {
  699. u32 reg;
  700. u32 i;
  701. if (tusb_get_revision(musb) == TUSB_REV_30)
  702. tusb_wbus_quirk(musb, 0);
  703. /* there are issues re-locking the PLL on wakeup ... */
  704. /* work around issue 8 */
  705. for (i = 0xf7f7f7; i > 0xf7f7f7 - 1000; i--) {
  706. musb_writel(tbase, TUSB_SCRATCH_PAD, 0);
  707. musb_writel(tbase, TUSB_SCRATCH_PAD, i);
  708. reg = musb_readl(tbase, TUSB_SCRATCH_PAD);
  709. if (reg == i)
  710. break;
  711. dev_dbg(musb->controller, "TUSB NOR not ready\n");
  712. }
  713. /* work around issue 13 (2nd half) */
  714. tusb_set_clock_source(musb, 1);
  715. reg = musb_readl(tbase, TUSB_PRCM_WAKEUP_SOURCE);
  716. musb_writel(tbase, TUSB_PRCM_WAKEUP_CLEAR, reg);
  717. if (reg & ~TUSB_PRCM_WNORCS) {
  718. musb->is_active = 1;
  719. schedule_work(&musb->irq_work);
  720. }
  721. dev_dbg(musb->controller, "wake %sactive %02x\n",
  722. musb->is_active ? "" : "in", reg);
  723. /* REVISIT host side TUSB_PRCM_WHOSTDISCON, TUSB_PRCM_WBUS */
  724. }
  725. if (int_src & TUSB_INT_SRC_USB_IP_CONN)
  726. del_timer(&musb_idle_timer);
  727. /* OTG state change reports (annoyingly) not issued by Mentor core */
  728. if (int_src & (TUSB_INT_SRC_VBUS_SENSE_CHNG
  729. | TUSB_INT_SRC_OTG_TIMEOUT
  730. | TUSB_INT_SRC_ID_STATUS_CHNG))
  731. idle_timeout = tusb_otg_ints(musb, int_src, tbase);
  732. /* TX dma callback must be handled here, RX dma callback is
  733. * handled in tusb_omap_dma_cb.
  734. */
  735. if ((int_src & TUSB_INT_SRC_TXRX_DMA_DONE)) {
  736. u32 dma_src = musb_readl(tbase, TUSB_DMA_INT_SRC);
  737. u32 real_dma_src = musb_readl(tbase, TUSB_DMA_INT_MASK);
  738. dev_dbg(musb->controller, "DMA IRQ %08x\n", dma_src);
  739. real_dma_src = ~real_dma_src & dma_src;
  740. if (tusb_dma_omap() && real_dma_src) {
  741. int tx_source = (real_dma_src & 0xffff);
  742. int i;
  743. for (i = 1; i <= 15; i++) {
  744. if (tx_source & (1 << i)) {
  745. dev_dbg(musb->controller, "completing ep%i %s\n", i, "tx");
  746. musb_dma_completion(musb, i, 1);
  747. }
  748. }
  749. }
  750. musb_writel(tbase, TUSB_DMA_INT_CLEAR, dma_src);
  751. }
  752. /* EP interrupts. In OCP mode tusb6010 mirrors the MUSB interrupts */
  753. if (int_src & (TUSB_INT_SRC_USB_IP_TX | TUSB_INT_SRC_USB_IP_RX)) {
  754. u32 musb_src = musb_readl(tbase, TUSB_USBIP_INT_SRC);
  755. musb_writel(tbase, TUSB_USBIP_INT_CLEAR, musb_src);
  756. musb->int_rx = (((musb_src >> 16) & 0xffff) << 1);
  757. musb->int_tx = (musb_src & 0xffff);
  758. } else {
  759. musb->int_rx = 0;
  760. musb->int_tx = 0;
  761. }
  762. if (int_src & (TUSB_INT_SRC_USB_IP_TX | TUSB_INT_SRC_USB_IP_RX | 0xff))
  763. musb_interrupt(musb);
  764. /* Acknowledge TUSB interrupts. Clear only non-reserved bits */
  765. musb_writel(tbase, TUSB_INT_SRC_CLEAR,
  766. int_src & ~TUSB_INT_MASK_RESERVED_BITS);
  767. tusb_musb_try_idle(musb, idle_timeout);
  768. musb_writel(tbase, TUSB_INT_MASK, int_mask);
  769. spin_unlock_irqrestore(&musb->lock, flags);
  770. return IRQ_HANDLED;
  771. }
  772. static int dma_off;
  773. /*
  774. * Enables TUSB6010. Caller must take care of locking.
  775. * REVISIT:
  776. * - Check what is unnecessary in MGC_HdrcStart()
  777. */
  778. static void tusb_musb_enable(struct musb *musb)
  779. {
  780. void __iomem *tbase = musb->ctrl_base;
  781. /* Setup TUSB6010 main interrupt mask. Enable all interrupts except SOF.
  782. * REVISIT: Enable and deal with TUSB_INT_SRC_USB_IP_SOF */
  783. musb_writel(tbase, TUSB_INT_MASK, TUSB_INT_SRC_USB_IP_SOF);
  784. /* Setup TUSB interrupt, disable DMA and GPIO interrupts */
  785. musb_writel(tbase, TUSB_USBIP_INT_MASK, 0);
  786. musb_writel(tbase, TUSB_DMA_INT_MASK, 0x7fffffff);
  787. musb_writel(tbase, TUSB_GPIO_INT_MASK, 0x1ff);
  788. /* Clear all subsystem interrups */
  789. musb_writel(tbase, TUSB_USBIP_INT_CLEAR, 0x7fffffff);
  790. musb_writel(tbase, TUSB_DMA_INT_CLEAR, 0x7fffffff);
  791. musb_writel(tbase, TUSB_GPIO_INT_CLEAR, 0x1ff);
  792. /* Acknowledge pending interrupt(s) */
  793. musb_writel(tbase, TUSB_INT_SRC_CLEAR, ~TUSB_INT_MASK_RESERVED_BITS);
  794. /* Only 0 clock cycles for minimum interrupt de-assertion time and
  795. * interrupt polarity active low seems to work reliably here */
  796. musb_writel(tbase, TUSB_INT_CTRL_CONF,
  797. TUSB_INT_CTRL_CONF_INT_RELCYC(0));
  798. irq_set_irq_type(musb->nIrq, IRQ_TYPE_LEVEL_LOW);
  799. /* maybe force into the Default-A OTG state machine */
  800. if (!(musb_readl(tbase, TUSB_DEV_OTG_STAT)
  801. & TUSB_DEV_OTG_STAT_ID_STATUS))
  802. musb_writel(tbase, TUSB_INT_SRC_SET,
  803. TUSB_INT_SRC_ID_STATUS_CHNG);
  804. if (is_dma_capable() && dma_off)
  805. printk(KERN_WARNING "%s %s: dma not reactivated\n",
  806. __FILE__, __func__);
  807. else
  808. dma_off = 1;
  809. }
  810. /*
  811. * Disables TUSB6010. Caller must take care of locking.
  812. */
  813. static void tusb_musb_disable(struct musb *musb)
  814. {
  815. void __iomem *tbase = musb->ctrl_base;
  816. /* FIXME stop DMA, IRQs, timers, ... */
  817. /* disable all IRQs */
  818. musb_writel(tbase, TUSB_INT_MASK, ~TUSB_INT_MASK_RESERVED_BITS);
  819. musb_writel(tbase, TUSB_USBIP_INT_MASK, 0x7fffffff);
  820. musb_writel(tbase, TUSB_DMA_INT_MASK, 0x7fffffff);
  821. musb_writel(tbase, TUSB_GPIO_INT_MASK, 0x1ff);
  822. del_timer(&musb_idle_timer);
  823. if (is_dma_capable() && !dma_off) {
  824. printk(KERN_WARNING "%s %s: dma still active\n",
  825. __FILE__, __func__);
  826. dma_off = 1;
  827. }
  828. }
  829. /*
  830. * Sets up TUSB6010 CPU interface specific signals and registers
  831. * Note: Settings optimized for OMAP24xx
  832. */
  833. static void tusb_setup_cpu_interface(struct musb *musb)
  834. {
  835. void __iomem *tbase = musb->ctrl_base;
  836. /*
  837. * Disable GPIO[5:0] pullups (used as output DMA requests)
  838. * Don't disable GPIO[7:6] as they are needed for wake-up.
  839. */
  840. musb_writel(tbase, TUSB_PULLUP_1_CTRL, 0x0000003F);
  841. /* Disable all pullups on NOR IF, DMAREQ0 and DMAREQ1 */
  842. musb_writel(tbase, TUSB_PULLUP_2_CTRL, 0x01FFFFFF);
  843. /* Turn GPIO[5:0] to DMAREQ[5:0] signals */
  844. musb_writel(tbase, TUSB_GPIO_CONF, TUSB_GPIO_CONF_DMAREQ(0x3f));
  845. /* Burst size 16x16 bits, all six DMA requests enabled, DMA request
  846. * de-assertion time 2 system clocks p 62 */
  847. musb_writel(tbase, TUSB_DMA_REQ_CONF,
  848. TUSB_DMA_REQ_CONF_BURST_SIZE(2) |
  849. TUSB_DMA_REQ_CONF_DMA_REQ_EN(0x3f) |
  850. TUSB_DMA_REQ_CONF_DMA_REQ_ASSER(2));
  851. /* Set 0 wait count for synchronous burst access */
  852. musb_writel(tbase, TUSB_WAIT_COUNT, 1);
  853. }
  854. static int tusb_musb_start(struct musb *musb)
  855. {
  856. void __iomem *tbase = musb->ctrl_base;
  857. int ret = 0;
  858. unsigned long flags;
  859. u32 reg;
  860. if (musb->board_set_power)
  861. ret = musb->board_set_power(1);
  862. if (ret != 0) {
  863. printk(KERN_ERR "tusb: Cannot enable TUSB6010\n");
  864. return ret;
  865. }
  866. spin_lock_irqsave(&musb->lock, flags);
  867. if (musb_readl(tbase, TUSB_PROD_TEST_RESET) !=
  868. TUSB_PROD_TEST_RESET_VAL) {
  869. printk(KERN_ERR "tusb: Unable to detect TUSB6010\n");
  870. goto err;
  871. }
  872. ret = tusb_print_revision(musb);
  873. if (ret < 2) {
  874. printk(KERN_ERR "tusb: Unsupported TUSB6010 revision %i\n",
  875. ret);
  876. goto err;
  877. }
  878. /* The uint bit for "USB non-PDR interrupt enable" has to be 1 when
  879. * NOR FLASH interface is used */
  880. musb_writel(tbase, TUSB_VLYNQ_CTRL, 8);
  881. /* Select PHY free running 60MHz as a system clock */
  882. tusb_set_clock_source(musb, 1);
  883. /* VBus valid timer 1us, disable DFT/Debug and VLYNQ clocks for
  884. * power saving, enable VBus detect and session end comparators,
  885. * enable IDpullup, enable VBus charging */
  886. musb_writel(tbase, TUSB_PRCM_MNGMT,
  887. TUSB_PRCM_MNGMT_VBUS_VALID_TIMER(0xa) |
  888. TUSB_PRCM_MNGMT_VBUS_VALID_FLT_EN |
  889. TUSB_PRCM_MNGMT_OTG_SESS_END_EN |
  890. TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN |
  891. TUSB_PRCM_MNGMT_OTG_ID_PULLUP);
  892. tusb_setup_cpu_interface(musb);
  893. /* simplify: always sense/pullup ID pins, as if in OTG mode */
  894. reg = musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE);
  895. reg |= TUSB_PHY_OTG_CTRL_WRPROTECT | TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
  896. musb_writel(tbase, TUSB_PHY_OTG_CTRL_ENABLE, reg);
  897. reg = musb_readl(tbase, TUSB_PHY_OTG_CTRL);
  898. reg |= TUSB_PHY_OTG_CTRL_WRPROTECT | TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
  899. musb_writel(tbase, TUSB_PHY_OTG_CTRL, reg);
  900. spin_unlock_irqrestore(&musb->lock, flags);
  901. return 0;
  902. err:
  903. spin_unlock_irqrestore(&musb->lock, flags);
  904. if (musb->board_set_power)
  905. musb->board_set_power(0);
  906. return -ENODEV;
  907. }
  908. static int tusb_musb_init(struct musb *musb)
  909. {
  910. struct platform_device *pdev;
  911. struct resource *mem;
  912. void __iomem *sync = NULL;
  913. int ret;
  914. usb_nop_xceiv_register();
  915. musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
  916. if (IS_ERR_OR_NULL(musb->xceiv))
  917. return -ENODEV;
  918. pdev = to_platform_device(musb->controller);
  919. /* dma address for async dma */
  920. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  921. musb->async = mem->start;
  922. /* dma address for sync dma */
  923. mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  924. if (!mem) {
  925. pr_debug("no sync dma resource?\n");
  926. ret = -ENODEV;
  927. goto done;
  928. }
  929. musb->sync = mem->start;
  930. sync = ioremap(mem->start, resource_size(mem));
  931. if (!sync) {
  932. pr_debug("ioremap for sync failed\n");
  933. ret = -ENOMEM;
  934. goto done;
  935. }
  936. musb->sync_va = sync;
  937. /* Offsets from base: VLYNQ at 0x000, MUSB regs at 0x400,
  938. * FIFOs at 0x600, TUSB at 0x800
  939. */
  940. musb->mregs += TUSB_BASE_OFFSET;
  941. ret = tusb_musb_start(musb);
  942. if (ret) {
  943. printk(KERN_ERR "Could not start tusb6010 (%d)\n",
  944. ret);
  945. goto done;
  946. }
  947. musb->isr = tusb_musb_interrupt;
  948. if (is_peripheral_enabled(musb)) {
  949. musb->xceiv->set_power = tusb_draw_power;
  950. the_musb = musb;
  951. }
  952. setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
  953. done:
  954. if (ret < 0) {
  955. if (sync)
  956. iounmap(sync);
  957. usb_put_phy(musb->xceiv);
  958. usb_nop_xceiv_unregister();
  959. }
  960. return ret;
  961. }
  962. static int tusb_musb_exit(struct musb *musb)
  963. {
  964. del_timer_sync(&musb_idle_timer);
  965. the_musb = NULL;
  966. if (musb->board_set_power)
  967. musb->board_set_power(0);
  968. iounmap(musb->sync_va);
  969. usb_put_phy(musb->xceiv);
  970. usb_nop_xceiv_unregister();
  971. return 0;
  972. }
  973. static const struct musb_platform_ops tusb_ops = {
  974. .init = tusb_musb_init,
  975. .exit = tusb_musb_exit,
  976. .enable = tusb_musb_enable,
  977. .disable = tusb_musb_disable,
  978. .set_mode = tusb_musb_set_mode,
  979. .try_idle = tusb_musb_try_idle,
  980. .vbus_status = tusb_musb_vbus_status,
  981. .set_vbus = tusb_musb_set_vbus,
  982. };
  983. static u64 tusb_dmamask = DMA_BIT_MASK(32);
  984. static int __devinit tusb_probe(struct platform_device *pdev)
  985. {
  986. struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
  987. struct platform_device *musb;
  988. struct tusb6010_glue *glue;
  989. int ret = -ENOMEM;
  990. glue = kzalloc(sizeof(*glue), GFP_KERNEL);
  991. if (!glue) {
  992. dev_err(&pdev->dev, "failed to allocate glue context\n");
  993. goto err0;
  994. }
  995. musb = platform_device_alloc("musb-hdrc", -1);
  996. if (!musb) {
  997. dev_err(&pdev->dev, "failed to allocate musb device\n");
  998. goto err1;
  999. }
  1000. musb->dev.parent = &pdev->dev;
  1001. musb->dev.dma_mask = &tusb_dmamask;
  1002. musb->dev.coherent_dma_mask = tusb_dmamask;
  1003. glue->dev = &pdev->dev;
  1004. glue->musb = musb;
  1005. pdata->platform_ops = &tusb_ops;
  1006. platform_set_drvdata(pdev, glue);
  1007. ret = platform_device_add_resources(musb, pdev->resource,
  1008. pdev->num_resources);
  1009. if (ret) {
  1010. dev_err(&pdev->dev, "failed to add resources\n");
  1011. goto err2;
  1012. }
  1013. ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
  1014. if (ret) {
  1015. dev_err(&pdev->dev, "failed to add platform_data\n");
  1016. goto err2;
  1017. }
  1018. ret = platform_device_add(musb);
  1019. if (ret) {
  1020. dev_err(&pdev->dev, "failed to register musb device\n");
  1021. goto err1;
  1022. }
  1023. return 0;
  1024. err2:
  1025. platform_device_put(musb);
  1026. err1:
  1027. kfree(glue);
  1028. err0:
  1029. return ret;
  1030. }
  1031. static int __devexit tusb_remove(struct platform_device *pdev)
  1032. {
  1033. struct tusb6010_glue *glue = platform_get_drvdata(pdev);
  1034. platform_device_del(glue->musb);
  1035. platform_device_put(glue->musb);
  1036. kfree(glue);
  1037. return 0;
  1038. }
  1039. static struct platform_driver tusb_driver = {
  1040. .probe = tusb_probe,
  1041. .remove = __devexit_p(tusb_remove),
  1042. .driver = {
  1043. .name = "musb-tusb",
  1044. },
  1045. };
  1046. MODULE_DESCRIPTION("TUSB6010 MUSB Glue Layer");
  1047. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  1048. MODULE_LICENSE("GPL v2");
  1049. static int __init tusb_init(void)
  1050. {
  1051. return platform_driver_register(&tusb_driver);
  1052. }
  1053. module_init(tusb_init);
  1054. static void __exit tusb_exit(void)
  1055. {
  1056. platform_driver_unregister(&tusb_driver);
  1057. }
  1058. module_exit(tusb_exit);