tusb6010.c 33 KB

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