tusb6010.c 35 KB

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