tusb6010.c 31 KB

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