tusb6010.c 34 KB

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