am35x.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * Texas Instruments AM35x "glue layer"
  3. *
  4. * Copyright (c) 2010, by Texas Instruments
  5. *
  6. * Based on the DA8xx "glue layer" code.
  7. * Copyright (c) 2008-2009, MontaVista Software, Inc. <source@mvista.com>
  8. *
  9. * This file is part of the Inventra Controller Driver for Linux.
  10. *
  11. * The Inventra Controller Driver for Linux is free software; you
  12. * can redistribute it and/or modify it under the terms of the GNU
  13. * General Public License version 2 as published by the Free Software
  14. * Foundation.
  15. *
  16. * The Inventra Controller Driver for Linux is distributed in
  17. * the hope that it will be useful, but WITHOUT ANY WARRANTY;
  18. * without even the implied warranty of MERCHANTABILITY or
  19. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  20. * License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with The Inventra Controller Driver for Linux ; if not,
  24. * write to the Free Software Foundation, Inc., 59 Temple Place,
  25. * Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. */
  28. #include <linux/init.h>
  29. #include <linux/clk.h>
  30. #include <linux/io.h>
  31. #include <plat/control.h>
  32. #include <plat/usb.h>
  33. #include "musb_core.h"
  34. /*
  35. * AM35x specific definitions
  36. */
  37. /* USB 2.0 OTG module registers */
  38. #define USB_REVISION_REG 0x00
  39. #define USB_CTRL_REG 0x04
  40. #define USB_STAT_REG 0x08
  41. #define USB_EMULATION_REG 0x0c
  42. /* 0x10 Reserved */
  43. #define USB_AUTOREQ_REG 0x14
  44. #define USB_SRP_FIX_TIME_REG 0x18
  45. #define USB_TEARDOWN_REG 0x1c
  46. #define EP_INTR_SRC_REG 0x20
  47. #define EP_INTR_SRC_SET_REG 0x24
  48. #define EP_INTR_SRC_CLEAR_REG 0x28
  49. #define EP_INTR_MASK_REG 0x2c
  50. #define EP_INTR_MASK_SET_REG 0x30
  51. #define EP_INTR_MASK_CLEAR_REG 0x34
  52. #define EP_INTR_SRC_MASKED_REG 0x38
  53. #define CORE_INTR_SRC_REG 0x40
  54. #define CORE_INTR_SRC_SET_REG 0x44
  55. #define CORE_INTR_SRC_CLEAR_REG 0x48
  56. #define CORE_INTR_MASK_REG 0x4c
  57. #define CORE_INTR_MASK_SET_REG 0x50
  58. #define CORE_INTR_MASK_CLEAR_REG 0x54
  59. #define CORE_INTR_SRC_MASKED_REG 0x58
  60. /* 0x5c Reserved */
  61. #define USB_END_OF_INTR_REG 0x60
  62. /* Control register bits */
  63. #define AM35X_SOFT_RESET_MASK 1
  64. /* USB interrupt register bits */
  65. #define AM35X_INTR_USB_SHIFT 16
  66. #define AM35X_INTR_USB_MASK (0x1ff << AM35X_INTR_USB_SHIFT)
  67. #define AM35X_INTR_DRVVBUS 0x100
  68. #define AM35X_INTR_RX_SHIFT 16
  69. #define AM35X_INTR_TX_SHIFT 0
  70. #define AM35X_TX_EP_MASK 0xffff /* EP0 + 15 Tx EPs */
  71. #define AM35X_RX_EP_MASK 0xfffe /* 15 Rx EPs */
  72. #define AM35X_TX_INTR_MASK (AM35X_TX_EP_MASK << AM35X_INTR_TX_SHIFT)
  73. #define AM35X_RX_INTR_MASK (AM35X_RX_EP_MASK << AM35X_INTR_RX_SHIFT)
  74. #define USB_MENTOR_CORE_OFFSET 0x400
  75. static inline void phy_on(void)
  76. {
  77. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  78. u32 devconf2;
  79. /*
  80. * Start the on-chip PHY and its PLL.
  81. */
  82. devconf2 = omap_ctrl_readl(AM35XX_CONTROL_DEVCONF2);
  83. devconf2 &= ~(CONF2_RESET | CONF2_PHYPWRDN | CONF2_OTGPWRDN);
  84. devconf2 |= CONF2_PHY_PLLON;
  85. omap_ctrl_writel(devconf2, AM35XX_CONTROL_DEVCONF2);
  86. DBG(1, "Waiting for PHY clock good...\n");
  87. while (!(omap_ctrl_readl(AM35XX_CONTROL_DEVCONF2)
  88. & CONF2_PHYCLKGD)) {
  89. cpu_relax();
  90. if (time_after(jiffies, timeout)) {
  91. DBG(1, "musb PHY clock good timed out\n");
  92. break;
  93. }
  94. }
  95. }
  96. static inline void phy_off(void)
  97. {
  98. u32 devconf2;
  99. /*
  100. * Power down the on-chip PHY.
  101. */
  102. devconf2 = omap_ctrl_readl(AM35XX_CONTROL_DEVCONF2);
  103. devconf2 &= ~CONF2_PHY_PLLON;
  104. devconf2 |= CONF2_PHYPWRDN | CONF2_OTGPWRDN;
  105. omap_ctrl_writel(devconf2, AM35XX_CONTROL_DEVCONF2);
  106. }
  107. /*
  108. * musb_platform_enable - enable interrupts
  109. */
  110. void musb_platform_enable(struct musb *musb)
  111. {
  112. void __iomem *reg_base = musb->ctrl_base;
  113. u32 epmask;
  114. /* Workaround: setup IRQs through both register sets. */
  115. epmask = ((musb->epmask & AM35X_TX_EP_MASK) << AM35X_INTR_TX_SHIFT) |
  116. ((musb->epmask & AM35X_RX_EP_MASK) << AM35X_INTR_RX_SHIFT);
  117. musb_writel(reg_base, EP_INTR_MASK_SET_REG, epmask);
  118. musb_writel(reg_base, CORE_INTR_MASK_SET_REG, AM35X_INTR_USB_MASK);
  119. /* Force the DRVVBUS IRQ so we can start polling for ID change. */
  120. if (is_otg_enabled(musb))
  121. musb_writel(reg_base, CORE_INTR_SRC_SET_REG,
  122. AM35X_INTR_DRVVBUS << AM35X_INTR_USB_SHIFT);
  123. }
  124. /*
  125. * musb_platform_disable - disable HDRC and flush interrupts
  126. */
  127. void musb_platform_disable(struct musb *musb)
  128. {
  129. void __iomem *reg_base = musb->ctrl_base;
  130. musb_writel(reg_base, CORE_INTR_MASK_CLEAR_REG, AM35X_INTR_USB_MASK);
  131. musb_writel(reg_base, EP_INTR_MASK_CLEAR_REG,
  132. AM35X_TX_INTR_MASK | AM35X_RX_INTR_MASK);
  133. musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
  134. musb_writel(reg_base, USB_END_OF_INTR_REG, 0);
  135. }
  136. #ifdef CONFIG_USB_MUSB_HDRC_HCD
  137. #define portstate(stmt) stmt
  138. #else
  139. #define portstate(stmt)
  140. #endif
  141. static void am35x_set_vbus(struct musb *musb, int is_on)
  142. {
  143. WARN_ON(is_on && is_peripheral_active(musb));
  144. }
  145. #define POLL_SECONDS 2
  146. static struct timer_list otg_workaround;
  147. static void otg_timer(unsigned long _musb)
  148. {
  149. struct musb *musb = (void *)_musb;
  150. void __iomem *mregs = musb->mregs;
  151. u8 devctl;
  152. unsigned long flags;
  153. /*
  154. * We poll because AM35x's won't expose several OTG-critical
  155. * status change events (from the transceiver) otherwise.
  156. */
  157. devctl = musb_readb(mregs, MUSB_DEVCTL);
  158. DBG(7, "Poll devctl %02x (%s)\n", devctl, otg_state_string(musb));
  159. spin_lock_irqsave(&musb->lock, flags);
  160. switch (musb->xceiv->state) {
  161. case OTG_STATE_A_WAIT_BCON:
  162. devctl &= ~MUSB_DEVCTL_SESSION;
  163. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  164. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  165. if (devctl & MUSB_DEVCTL_BDEVICE) {
  166. musb->xceiv->state = OTG_STATE_B_IDLE;
  167. MUSB_DEV_MODE(musb);
  168. } else {
  169. musb->xceiv->state = OTG_STATE_A_IDLE;
  170. MUSB_HST_MODE(musb);
  171. }
  172. break;
  173. case OTG_STATE_A_WAIT_VFALL:
  174. musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
  175. musb_writel(musb->ctrl_base, CORE_INTR_SRC_SET_REG,
  176. MUSB_INTR_VBUSERROR << AM35X_INTR_USB_SHIFT);
  177. break;
  178. case OTG_STATE_B_IDLE:
  179. if (!is_peripheral_enabled(musb))
  180. break;
  181. devctl = musb_readb(mregs, MUSB_DEVCTL);
  182. if (devctl & MUSB_DEVCTL_BDEVICE)
  183. mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ);
  184. else
  185. musb->xceiv->state = OTG_STATE_A_IDLE;
  186. break;
  187. default:
  188. break;
  189. }
  190. spin_unlock_irqrestore(&musb->lock, flags);
  191. }
  192. void musb_platform_try_idle(struct musb *musb, unsigned long timeout)
  193. {
  194. static unsigned long last_timer;
  195. if (!is_otg_enabled(musb))
  196. return;
  197. if (timeout == 0)
  198. timeout = jiffies + msecs_to_jiffies(3);
  199. /* Never idle if active, or when VBUS timeout is not set as host */
  200. if (musb->is_active || (musb->a_wait_bcon == 0 &&
  201. musb->xceiv->state == OTG_STATE_A_WAIT_BCON)) {
  202. DBG(4, "%s active, deleting timer\n", otg_state_string(musb));
  203. del_timer(&otg_workaround);
  204. last_timer = jiffies;
  205. return;
  206. }
  207. if (time_after(last_timer, timeout) && timer_pending(&otg_workaround)) {
  208. DBG(4, "Longer idle timer already pending, ignoring...\n");
  209. return;
  210. }
  211. last_timer = timeout;
  212. DBG(4, "%s inactive, starting idle timer for %u ms\n",
  213. otg_state_string(musb), jiffies_to_msecs(timeout - jiffies));
  214. mod_timer(&otg_workaround, timeout);
  215. }
  216. static irqreturn_t am35x_interrupt(int irq, void *hci)
  217. {
  218. struct musb *musb = hci;
  219. void __iomem *reg_base = musb->ctrl_base;
  220. unsigned long flags;
  221. irqreturn_t ret = IRQ_NONE;
  222. u32 epintr, usbintr, lvl_intr;
  223. spin_lock_irqsave(&musb->lock, flags);
  224. /* Get endpoint interrupts */
  225. epintr = musb_readl(reg_base, EP_INTR_SRC_MASKED_REG);
  226. if (epintr) {
  227. musb_writel(reg_base, EP_INTR_SRC_CLEAR_REG, epintr);
  228. musb->int_rx =
  229. (epintr & AM35X_RX_INTR_MASK) >> AM35X_INTR_RX_SHIFT;
  230. musb->int_tx =
  231. (epintr & AM35X_TX_INTR_MASK) >> AM35X_INTR_TX_SHIFT;
  232. }
  233. /* Get usb core interrupts */
  234. usbintr = musb_readl(reg_base, CORE_INTR_SRC_MASKED_REG);
  235. if (!usbintr && !epintr)
  236. goto eoi;
  237. if (usbintr) {
  238. musb_writel(reg_base, CORE_INTR_SRC_CLEAR_REG, usbintr);
  239. musb->int_usb =
  240. (usbintr & AM35X_INTR_USB_MASK) >> AM35X_INTR_USB_SHIFT;
  241. }
  242. /*
  243. * DRVVBUS IRQs are the only proxy we have (a very poor one!) for
  244. * AM35x's missing ID change IRQ. We need an ID change IRQ to
  245. * switch appropriately between halves of the OTG state machine.
  246. * Managing DEVCTL.SESSION per Mentor docs requires that we know its
  247. * value but DEVCTL.BDEVICE is invalid without DEVCTL.SESSION set.
  248. * Also, DRVVBUS pulses for SRP (but not at 5V) ...
  249. */
  250. if (usbintr & (AM35X_INTR_DRVVBUS << AM35X_INTR_USB_SHIFT)) {
  251. int drvvbus = musb_readl(reg_base, USB_STAT_REG);
  252. void __iomem *mregs = musb->mregs;
  253. u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
  254. int err;
  255. err = is_host_enabled(musb) && (musb->int_usb &
  256. MUSB_INTR_VBUSERROR);
  257. if (err) {
  258. /*
  259. * The Mentor core doesn't debounce VBUS as needed
  260. * to cope with device connect current spikes. This
  261. * means it's not uncommon for bus-powered devices
  262. * to get VBUS errors during enumeration.
  263. *
  264. * This is a workaround, but newer RTL from Mentor
  265. * seems to allow a better one: "re"-starting sessions
  266. * without waiting for VBUS to stop registering in
  267. * devctl.
  268. */
  269. musb->int_usb &= ~MUSB_INTR_VBUSERROR;
  270. musb->xceiv->state = OTG_STATE_A_WAIT_VFALL;
  271. mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ);
  272. WARNING("VBUS error workaround (delay coming)\n");
  273. } else if (is_host_enabled(musb) && drvvbus) {
  274. MUSB_HST_MODE(musb);
  275. musb->xceiv->default_a = 1;
  276. musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
  277. portstate(musb->port1_status |= USB_PORT_STAT_POWER);
  278. del_timer(&otg_workaround);
  279. } else {
  280. musb->is_active = 0;
  281. MUSB_DEV_MODE(musb);
  282. musb->xceiv->default_a = 0;
  283. musb->xceiv->state = OTG_STATE_B_IDLE;
  284. portstate(musb->port1_status &= ~USB_PORT_STAT_POWER);
  285. }
  286. /* NOTE: this must complete power-on within 100 ms. */
  287. DBG(2, "VBUS %s (%s)%s, devctl %02x\n",
  288. drvvbus ? "on" : "off",
  289. otg_state_string(musb),
  290. err ? " ERROR" : "",
  291. devctl);
  292. ret = IRQ_HANDLED;
  293. }
  294. if (musb->int_tx || musb->int_rx || musb->int_usb)
  295. ret |= musb_interrupt(musb);
  296. eoi:
  297. /* EOI needs to be written for the IRQ to be re-asserted. */
  298. if (ret == IRQ_HANDLED || epintr || usbintr) {
  299. /* clear level interrupt */
  300. lvl_intr = omap_ctrl_readl(AM35XX_CONTROL_LVL_INTR_CLEAR);
  301. lvl_intr |= AM35XX_USBOTGSS_INT_CLR;
  302. omap_ctrl_writel(lvl_intr, AM35XX_CONTROL_LVL_INTR_CLEAR);
  303. /* write EOI */
  304. musb_writel(reg_base, USB_END_OF_INTR_REG, 0);
  305. }
  306. /* Poll for ID change */
  307. if (is_otg_enabled(musb) && musb->xceiv->state == OTG_STATE_B_IDLE)
  308. mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ);
  309. spin_unlock_irqrestore(&musb->lock, flags);
  310. return ret;
  311. }
  312. int musb_platform_set_mode(struct musb *musb, u8 musb_mode)
  313. {
  314. u32 devconf2 = omap_ctrl_readl(AM35XX_CONTROL_DEVCONF2);
  315. devconf2 &= ~CONF2_OTGMODE;
  316. switch (musb_mode) {
  317. #ifdef CONFIG_USB_MUSB_HDRC_HCD
  318. case MUSB_HOST: /* Force VBUS valid, ID = 0 */
  319. devconf2 |= CONF2_FORCE_HOST;
  320. break;
  321. #endif
  322. #ifdef CONFIG_USB_GADGET_MUSB_HDRC
  323. case MUSB_PERIPHERAL: /* Force VBUS valid, ID = 1 */
  324. devconf2 |= CONF2_FORCE_DEVICE;
  325. break;
  326. #endif
  327. #ifdef CONFIG_USB_MUSB_OTG
  328. case MUSB_OTG: /* Don't override the VBUS/ID comparators */
  329. devconf2 |= CONF2_NO_OVERRIDE;
  330. break;
  331. #endif
  332. default:
  333. DBG(2, "Trying to set unsupported mode %u\n", musb_mode);
  334. }
  335. omap_ctrl_writel(devconf2, AM35XX_CONTROL_DEVCONF2);
  336. return 0;
  337. }
  338. int __init musb_platform_init(struct musb *musb, void *board_data)
  339. {
  340. void __iomem *reg_base = musb->ctrl_base;
  341. u32 rev, lvl_intr, sw_reset;
  342. int status;
  343. musb->mregs += USB_MENTOR_CORE_OFFSET;
  344. clk_enable(musb->clock);
  345. DBG(2, "musb->clock=%lud\n", clk_get_rate(musb->clock));
  346. musb->phy_clock = clk_get(musb->controller, "fck");
  347. if (IS_ERR(musb->phy_clock)) {
  348. status = PTR_ERR(musb->phy_clock);
  349. goto exit0;
  350. }
  351. clk_enable(musb->phy_clock);
  352. DBG(2, "musb->phy_clock=%lud\n", clk_get_rate(musb->phy_clock));
  353. /* Returns zero if e.g. not clocked */
  354. rev = musb_readl(reg_base, USB_REVISION_REG);
  355. if (!rev) {
  356. status = -ENODEV;
  357. goto exit1;
  358. }
  359. usb_nop_xceiv_register();
  360. musb->xceiv = otg_get_transceiver();
  361. if (!musb->xceiv) {
  362. status = -ENODEV;
  363. goto exit1;
  364. }
  365. if (is_host_enabled(musb))
  366. setup_timer(&otg_workaround, otg_timer, (unsigned long) musb);
  367. musb->board_set_vbus = am35x_set_vbus;
  368. /* Global reset */
  369. sw_reset = omap_ctrl_readl(AM35XX_CONTROL_IP_SW_RESET);
  370. sw_reset |= AM35XX_USBOTGSS_SW_RST;
  371. omap_ctrl_writel(sw_reset, AM35XX_CONTROL_IP_SW_RESET);
  372. sw_reset &= ~AM35XX_USBOTGSS_SW_RST;
  373. omap_ctrl_writel(sw_reset, AM35XX_CONTROL_IP_SW_RESET);
  374. /* Reset the controller */
  375. musb_writel(reg_base, USB_CTRL_REG, AM35X_SOFT_RESET_MASK);
  376. /* Start the on-chip PHY and its PLL. */
  377. phy_on();
  378. msleep(5);
  379. musb->isr = am35x_interrupt;
  380. /* clear level interrupt */
  381. lvl_intr = omap_ctrl_readl(AM35XX_CONTROL_LVL_INTR_CLEAR);
  382. lvl_intr |= AM35XX_USBOTGSS_INT_CLR;
  383. omap_ctrl_writel(lvl_intr, AM35XX_CONTROL_LVL_INTR_CLEAR);
  384. return 0;
  385. exit1:
  386. clk_disable(musb->phy_clock);
  387. clk_put(musb->phy_clock);
  388. exit0:
  389. clk_disable(musb->clock);
  390. return status;
  391. }
  392. int musb_platform_exit(struct musb *musb)
  393. {
  394. if (is_host_enabled(musb))
  395. del_timer_sync(&otg_workaround);
  396. phy_off();
  397. otg_put_transceiver(musb->xceiv);
  398. usb_nop_xceiv_unregister();
  399. clk_disable(musb->clock);
  400. clk_disable(musb->phy_clock);
  401. clk_put(musb->phy_clock);
  402. return 0;
  403. }
  404. #ifdef CONFIG_PM
  405. void musb_platform_save_context(struct musb *musb,
  406. struct musb_context_registers *musb_context)
  407. {
  408. phy_off();
  409. }
  410. void musb_platform_restore_context(struct musb *musb,
  411. struct musb_context_registers *musb_context)
  412. {
  413. phy_on();
  414. }
  415. #endif
  416. /* AM35x supports only 32bit read operation */
  417. void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
  418. {
  419. void __iomem *fifo = hw_ep->fifo;
  420. u32 val;
  421. int i;
  422. /* Read for 32bit-aligned destination address */
  423. if (likely((0x03 & (unsigned long) dst) == 0) && len >= 4) {
  424. readsl(fifo, dst, len >> 2);
  425. dst += len & ~0x03;
  426. len &= 0x03;
  427. }
  428. /*
  429. * Now read the remaining 1 to 3 byte or complete length if
  430. * unaligned address.
  431. */
  432. if (len > 4) {
  433. for (i = 0; i < (len >> 2); i++) {
  434. *(u32 *) dst = musb_readl(fifo, 0);
  435. dst += 4;
  436. }
  437. len &= 0x03;
  438. }
  439. if (len > 0) {
  440. val = musb_readl(fifo, 0);
  441. memcpy(dst, &val, len);
  442. }
  443. }