tusb6010.c 34 KB

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