tusb6010.c 33 KB

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