tusb6010.c 35 KB

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