tusb6010.c 33 KB

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