blob: a8e26a0f2ad4087a8540d1c962a76e2274dcc4ce [file] [log] [blame]
Felipe Balbi550a7372008-07-24 12:27:36 +03001/*
2 * TUSB6010 USB 2.0 OTG Dual Role controller
3 *
4 * Copyright (C) 2006 Nokia Corporation
Felipe Balbi550a7372008-07-24 12:27:36 +03005 * 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
17#include <linux/module.h>
18#include <linux/kernel.h>
19#include <linux/errno.h>
20#include <linux/init.h>
21#include <linux/usb.h>
22#include <linux/irq.h>
23#include <linux/platform_device.h>
Felipe Balbi18688fb2010-12-02 09:13:54 +020024#include <linux/dma-mapping.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030025
26#include "musb_core.h"
27
Felipe Balbi743411b2010-12-01 13:22:05 +020028static void tusb_musb_set_vbus(struct musb *musb, int is_on);
Felipe Balbi550a7372008-07-24 12:27:36 +030029
30#define TUSB_REV_MAJOR(reg_val) ((reg_val >> 4) & 0xf)
31#define TUSB_REV_MINOR(reg_val) (reg_val & 0xf)
32
33/*
34 * Checks the revision. We need to use the DMA register as 3.0 does not
35 * have correct versions for TUSB_PRCM_REV or TUSB_INT_CTRL_REV.
36 */
37u8 tusb_get_revision(struct musb *musb)
38{
39 void __iomem *tbase = musb->ctrl_base;
40 u32 die_id;
41 u8 rev;
42
43 rev = musb_readl(tbase, TUSB_DMA_CTRL_REV) & 0xff;
44 if (TUSB_REV_MAJOR(rev) == 3) {
45 die_id = TUSB_DIDR1_HI_CHIP_REV(musb_readl(tbase,
46 TUSB_DIDR1_HI));
47 if (die_id >= TUSB_DIDR1_HI_REV_31)
48 rev |= 1;
49 }
50
51 return rev;
52}
53
Felipe Balbi743411b2010-12-01 13:22:05 +020054static int tusb_print_revision(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +030055{
56 void __iomem *tbase = musb->ctrl_base;
57 u8 rev;
58
59 rev = tusb_get_revision(musb);
60
61 pr_info("tusb: %s%i.%i %s%i.%i %s%i.%i %s%i.%i %s%i %s%i.%i\n",
62 "prcm",
63 TUSB_REV_MAJOR(musb_readl(tbase, TUSB_PRCM_REV)),
64 TUSB_REV_MINOR(musb_readl(tbase, TUSB_PRCM_REV)),
65 "int",
66 TUSB_REV_MAJOR(musb_readl(tbase, TUSB_INT_CTRL_REV)),
67 TUSB_REV_MINOR(musb_readl(tbase, TUSB_INT_CTRL_REV)),
68 "gpio",
69 TUSB_REV_MAJOR(musb_readl(tbase, TUSB_GPIO_REV)),
70 TUSB_REV_MINOR(musb_readl(tbase, TUSB_GPIO_REV)),
71 "dma",
72 TUSB_REV_MAJOR(musb_readl(tbase, TUSB_DMA_CTRL_REV)),
73 TUSB_REV_MINOR(musb_readl(tbase, TUSB_DMA_CTRL_REV)),
74 "dieid",
75 TUSB_DIDR1_HI_CHIP_REV(musb_readl(tbase, TUSB_DIDR1_HI)),
76 "rev",
77 TUSB_REV_MAJOR(rev), TUSB_REV_MINOR(rev));
78
79 return tusb_get_revision(musb);
80}
81
82#define WBUS_QUIRK_MASK (TUSB_PHY_OTG_CTRL_TESTM2 | TUSB_PHY_OTG_CTRL_TESTM1 \
83 | TUSB_PHY_OTG_CTRL_TESTM0)
84
85/*
86 * Workaround for spontaneous WBUS wake-up issue #2 for tusb3.0.
87 * Disables power detection in PHY for the duration of idle.
88 */
89static void tusb_wbus_quirk(struct musb *musb, int enabled)
90{
91 void __iomem *tbase = musb->ctrl_base;
92 static u32 phy_otg_ctrl, phy_otg_ena;
93 u32 tmp;
94
95 if (enabled) {
96 phy_otg_ctrl = musb_readl(tbase, TUSB_PHY_OTG_CTRL);
97 phy_otg_ena = musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE);
98 tmp = TUSB_PHY_OTG_CTRL_WRPROTECT
99 | phy_otg_ena | WBUS_QUIRK_MASK;
100 musb_writel(tbase, TUSB_PHY_OTG_CTRL, tmp);
101 tmp = phy_otg_ena & ~WBUS_QUIRK_MASK;
102 tmp |= TUSB_PHY_OTG_CTRL_WRPROTECT | TUSB_PHY_OTG_CTRL_TESTM2;
103 musb_writel(tbase, TUSB_PHY_OTG_CTRL_ENABLE, tmp);
104 DBG(2, "Enabled tusb wbus quirk ctrl %08x ena %08x\n",
105 musb_readl(tbase, TUSB_PHY_OTG_CTRL),
106 musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE));
107 } else if (musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE)
108 & TUSB_PHY_OTG_CTRL_TESTM2) {
109 tmp = TUSB_PHY_OTG_CTRL_WRPROTECT | phy_otg_ctrl;
110 musb_writel(tbase, TUSB_PHY_OTG_CTRL, tmp);
111 tmp = TUSB_PHY_OTG_CTRL_WRPROTECT | phy_otg_ena;
112 musb_writel(tbase, TUSB_PHY_OTG_CTRL_ENABLE, tmp);
113 DBG(2, "Disabled tusb wbus quirk ctrl %08x ena %08x\n",
114 musb_readl(tbase, TUSB_PHY_OTG_CTRL),
115 musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE));
116 phy_otg_ctrl = 0;
117 phy_otg_ena = 0;
118 }
119}
120
121/*
122 * TUSB 6010 may use a parallel bus that doesn't support byte ops;
123 * so both loading and unloading FIFOs need explicit byte counts.
124 */
125
126static inline void
127tusb_fifo_write_unaligned(void __iomem *fifo, const u8 *buf, u16 len)
128{
129 u32 val;
130 int i;
131
132 if (len > 4) {
133 for (i = 0; i < (len >> 2); i++) {
134 memcpy(&val, buf, 4);
135 musb_writel(fifo, 0, val);
136 buf += 4;
137 }
138 len %= 4;
139 }
140 if (len > 0) {
141 /* Write the rest 1 - 3 bytes to FIFO */
142 memcpy(&val, buf, len);
143 musb_writel(fifo, 0, val);
144 }
145}
146
147static inline void tusb_fifo_read_unaligned(void __iomem *fifo,
148 void __iomem *buf, u16 len)
149{
150 u32 val;
151 int i;
152
153 if (len > 4) {
154 for (i = 0; i < (len >> 2); i++) {
155 val = musb_readl(fifo, 0);
156 memcpy(buf, &val, 4);
157 buf += 4;
158 }
159 len %= 4;
160 }
161 if (len > 0) {
162 /* Read the rest 1 - 3 bytes from FIFO */
163 val = musb_readl(fifo, 0);
164 memcpy(buf, &val, len);
165 }
166}
167
168void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *buf)
169{
170 void __iomem *ep_conf = hw_ep->conf;
171 void __iomem *fifo = hw_ep->fifo;
172 u8 epnum = hw_ep->epnum;
173
174 prefetch(buf);
175
176 DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
177 'T', epnum, fifo, len, buf);
178
179 if (epnum)
180 musb_writel(ep_conf, TUSB_EP_TX_OFFSET,
181 TUSB_EP_CONFIG_XFR_SIZE(len));
182 else
183 musb_writel(ep_conf, 0, TUSB_EP0_CONFIG_DIR_TX |
184 TUSB_EP0_CONFIG_XFR_SIZE(len));
185
186 if (likely((0x01 & (unsigned long) buf) == 0)) {
187
188 /* Best case is 32bit-aligned destination address */
189 if ((0x02 & (unsigned long) buf) == 0) {
190 if (len >= 4) {
191 writesl(fifo, buf, len >> 2);
192 buf += (len & ~0x03);
193 len &= 0x03;
194 }
195 } else {
196 if (len >= 2) {
197 u32 val;
198 int i;
199
200 /* Cannot use writesw, fifo is 32-bit */
201 for (i = 0; i < (len >> 2); i++) {
202 val = (u32)(*(u16 *)buf);
203 buf += 2;
204 val |= (*(u16 *)buf) << 16;
205 buf += 2;
206 musb_writel(fifo, 0, val);
207 }
208 len &= 0x03;
209 }
210 }
211 }
212
213 if (len > 0)
214 tusb_fifo_write_unaligned(fifo, buf, len);
215}
216
217void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *buf)
218{
219 void __iomem *ep_conf = hw_ep->conf;
220 void __iomem *fifo = hw_ep->fifo;
221 u8 epnum = hw_ep->epnum;
222
223 DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
224 'R', epnum, fifo, len, buf);
225
226 if (epnum)
227 musb_writel(ep_conf, TUSB_EP_RX_OFFSET,
228 TUSB_EP_CONFIG_XFR_SIZE(len));
229 else
230 musb_writel(ep_conf, 0, TUSB_EP0_CONFIG_XFR_SIZE(len));
231
232 if (likely((0x01 & (unsigned long) buf) == 0)) {
233
234 /* Best case is 32bit-aligned destination address */
235 if ((0x02 & (unsigned long) buf) == 0) {
236 if (len >= 4) {
237 readsl(fifo, buf, len >> 2);
238 buf += (len & ~0x03);
239 len &= 0x03;
240 }
241 } else {
242 if (len >= 2) {
243 u32 val;
244 int i;
245
246 /* Cannot use readsw, fifo is 32-bit */
247 for (i = 0; i < (len >> 2); i++) {
248 val = musb_readl(fifo, 0);
249 *(u16 *)buf = (u16)(val & 0xffff);
250 buf += 2;
251 *(u16 *)buf = (u16)(val >> 16);
252 buf += 2;
253 }
254 len &= 0x03;
255 }
256 }
257 }
258
259 if (len > 0)
260 tusb_fifo_read_unaligned(fifo, buf, len);
261}
262
David Brownell84e250f2009-03-31 12:30:04 -0700263static struct musb *the_musb;
264
Felipe Balbi550a7372008-07-24 12:27:36 +0300265#ifdef CONFIG_USB_GADGET_MUSB_HDRC
266
267/* This is used by gadget drivers, and OTG transceiver logic, allowing
268 * at most mA current to be drawn from VBUS during a Default-B session
269 * (that is, while VBUS exceeds 4.4V). In Default-A (including pure host
270 * mode), or low power Default-B sessions, something else supplies power.
271 * Caller must take care of locking.
272 */
273static int tusb_draw_power(struct otg_transceiver *x, unsigned mA)
274{
David Brownell84e250f2009-03-31 12:30:04 -0700275 struct musb *musb = the_musb;
Felipe Balbi550a7372008-07-24 12:27:36 +0300276 void __iomem *tbase = musb->ctrl_base;
277 u32 reg;
278
279 /*
280 * Keep clock active when enabled. Note that this is not tied to
281 * drawing VBUS, as with OTG mA can be less than musb->min_power.
282 */
283 if (musb->set_clock) {
284 if (mA)
285 musb->set_clock(musb->clock, 1);
286 else
287 musb->set_clock(musb->clock, 0);
288 }
289
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->default_a || mA < (musb->min_power << 1))
301 mA = 0;
302
303 reg = musb_readl(tbase, TUSB_PRCM_MNGMT);
304 if (mA) {
305 musb->is_bus_powered = 1;
306 reg |= TUSB_PRCM_MNGMT_15_SW_EN | TUSB_PRCM_MNGMT_33_SW_EN;
307 } else {
308 musb->is_bus_powered = 0;
309 reg &= ~(TUSB_PRCM_MNGMT_15_SW_EN | TUSB_PRCM_MNGMT_33_SW_EN);
310 }
311 musb_writel(tbase, TUSB_PRCM_MNGMT, reg);
312
313 DBG(2, "draw max %d mA VBUS\n", mA);
314 return 0;
315}
316
317#else
318#define tusb_draw_power NULL
319#endif
320
321/* workaround for issue 13: change clock during chip idle
322 * (to be fixed in rev3 silicon) ... symptoms include disconnect
323 * or looping suspend/resume cycles
324 */
325static void tusb_set_clock_source(struct musb *musb, unsigned mode)
326{
327 void __iomem *tbase = musb->ctrl_base;
328 u32 reg;
329
330 reg = musb_readl(tbase, TUSB_PRCM_CONF);
331 reg &= ~TUSB_PRCM_CONF_SYS_CLKSEL(0x3);
332
333 /* 0 = refclk (clkin, XI)
334 * 1 = PHY 60 MHz (internal PLL)
335 * 2 = not supported
336 * 3 = what?
337 */
338 if (mode > 0)
339 reg |= TUSB_PRCM_CONF_SYS_CLKSEL(mode & 0x3);
340
341 musb_writel(tbase, TUSB_PRCM_CONF, reg);
342
343 /* FIXME tusb6010_platform_retime(mode == 0); */
344}
345
346/*
347 * Idle TUSB6010 until next wake-up event; NOR access always wakes.
348 * Other code ensures that we idle unless we're connected _and_ the
349 * USB link is not suspended ... and tells us the relevant wakeup
350 * events. SW_EN for voltage is handled separately.
351 */
Felipe Balbi743411b2010-12-01 13:22:05 +0200352static void tusb_allow_idle(struct musb *musb, u32 wakeup_enables)
Felipe Balbi550a7372008-07-24 12:27:36 +0300353{
354 void __iomem *tbase = musb->ctrl_base;
355 u32 reg;
356
357 if ((wakeup_enables & TUSB_PRCM_WBUS)
358 && (tusb_get_revision(musb) == TUSB_REV_30))
359 tusb_wbus_quirk(musb, 1);
360
361 tusb_set_clock_source(musb, 0);
362
363 wakeup_enables |= TUSB_PRCM_WNORCS;
364 musb_writel(tbase, TUSB_PRCM_WAKEUP_MASK, ~wakeup_enables);
365
366 /* REVISIT writeup of WID implies that if WID set and ID is grounded,
367 * TUSB_PHY_OTG_CTRL.TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP must be cleared.
368 * Presumably that's mostly to save power, hence WID is immaterial ...
369 */
370
371 reg = musb_readl(tbase, TUSB_PRCM_MNGMT);
372 /* issue 4: when driving vbus, use hipower (vbus_det) comparator */
373 if (is_host_active(musb)) {
374 reg |= TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN;
375 reg &= ~TUSB_PRCM_MNGMT_OTG_SESS_END_EN;
376 } else {
377 reg |= TUSB_PRCM_MNGMT_OTG_SESS_END_EN;
378 reg &= ~TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN;
379 }
380 reg |= TUSB_PRCM_MNGMT_PM_IDLE | TUSB_PRCM_MNGMT_DEV_IDLE;
381 musb_writel(tbase, TUSB_PRCM_MNGMT, reg);
382
383 DBG(6, "idle, wake on %02x\n", wakeup_enables);
384}
385
386/*
387 * Updates cable VBUS status. Caller must take care of locking.
388 */
Felipe Balbi743411b2010-12-01 13:22:05 +0200389static int tusb_musb_vbus_status(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300390{
391 void __iomem *tbase = musb->ctrl_base;
392 u32 otg_stat, prcm_mngmt;
393 int ret = 0;
394
395 otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
396 prcm_mngmt = musb_readl(tbase, TUSB_PRCM_MNGMT);
397
398 /* Temporarily enable VBUS detection if it was disabled for
399 * suspend mode. Unless it's enabled otg_stat and devctl will
400 * not show correct VBUS state.
401 */
402 if (!(prcm_mngmt & TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN)) {
403 u32 tmp = prcm_mngmt;
404 tmp |= TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN;
405 musb_writel(tbase, TUSB_PRCM_MNGMT, tmp);
406 otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
407 musb_writel(tbase, TUSB_PRCM_MNGMT, prcm_mngmt);
408 }
409
410 if (otg_stat & TUSB_DEV_OTG_STAT_VBUS_VALID)
411 ret = 1;
412
413 return ret;
414}
415
416static struct timer_list musb_idle_timer;
417
418static void musb_do_idle(unsigned long _musb)
419{
420 struct musb *musb = (void *)_musb;
421 unsigned long flags;
422
423 spin_lock_irqsave(&musb->lock, flags);
424
David Brownell84e250f2009-03-31 12:30:04 -0700425 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300426 case OTG_STATE_A_WAIT_BCON:
427 if ((musb->a_wait_bcon != 0)
428 && (musb->idle_timeout == 0
429 || time_after(jiffies, musb->idle_timeout))) {
430 DBG(4, "Nothing connected %s, turning off VBUS\n",
431 otg_state_string(musb));
432 }
433 /* FALLTHROUGH */
434 case OTG_STATE_A_IDLE:
Felipe Balbi743411b2010-12-01 13:22:05 +0200435 tusb_musb_set_vbus(musb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +0300436 default:
437 break;
438 }
439
440 if (!musb->is_active) {
441 u32 wakeups;
442
443 /* wait until khubd handles port change status */
444 if (is_host_active(musb) && (musb->port1_status >> 16))
445 goto done;
446
447#ifdef CONFIG_USB_GADGET_MUSB_HDRC
448 if (is_peripheral_enabled(musb) && !musb->gadget_driver)
449 wakeups = 0;
450 else {
451 wakeups = TUSB_PRCM_WHOSTDISCON
452 | TUSB_PRCM_WBUS
453 | TUSB_PRCM_WVBUS;
454 if (is_otg_enabled(musb))
455 wakeups |= TUSB_PRCM_WID;
456 }
457#else
458 wakeups = TUSB_PRCM_WHOSTDISCON | TUSB_PRCM_WBUS;
459#endif
460 tusb_allow_idle(musb, wakeups);
461 }
462done:
463 spin_unlock_irqrestore(&musb->lock, flags);
464}
465
466/*
467 * Maybe put TUSB6010 into idle mode mode depending on USB link status,
468 * like "disconnected" or "suspended". We'll be woken out of it by
469 * connect, resume, or disconnect.
470 *
471 * Needs to be called as the last function everywhere where there is
472 * register access to TUSB6010 because of NOR flash wake-up.
473 * Caller should own controller spinlock.
474 *
475 * Delay because peripheral enables D+ pullup 3msec after SE0, and
476 * we don't want to treat that full speed J as a wakeup event.
477 * ... peripherals must draw only suspend current after 10 msec.
478 */
Felipe Balbi743411b2010-12-01 13:22:05 +0200479static void tusb_musb_try_idle(struct musb *musb, unsigned long timeout)
Felipe Balbi550a7372008-07-24 12:27:36 +0300480{
481 unsigned long default_timeout = jiffies + msecs_to_jiffies(3);
482 static unsigned long last_timer;
483
484 if (timeout == 0)
485 timeout = default_timeout;
486
487 /* Never idle if active, or when VBUS timeout is not set as host */
488 if (musb->is_active || ((musb->a_wait_bcon == 0)
David Brownell84e250f2009-03-31 12:30:04 -0700489 && (musb->xceiv->state == OTG_STATE_A_WAIT_BCON))) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300490 DBG(4, "%s active, deleting timer\n", otg_state_string(musb));
491 del_timer(&musb_idle_timer);
492 last_timer = jiffies;
493 return;
494 }
495
496 if (time_after(last_timer, timeout)) {
497 if (!timer_pending(&musb_idle_timer))
498 last_timer = timeout;
499 else {
500 DBG(4, "Longer idle timer already pending, ignoring\n");
501 return;
502 }
503 }
504 last_timer = timeout;
505
506 DBG(4, "%s inactive, for idle timer for %lu ms\n",
507 otg_state_string(musb),
508 (unsigned long)jiffies_to_msecs(timeout - jiffies));
509 mod_timer(&musb_idle_timer, timeout);
510}
511
512/* ticks of 60 MHz clock */
513#define DEVCLOCK 60000000
514#define OTG_TIMER_MS(msecs) ((msecs) \
515 ? (TUSB_DEV_OTG_TIMER_VAL((DEVCLOCK/1000)*(msecs)) \
516 | TUSB_DEV_OTG_TIMER_ENABLE) \
517 : 0)
518
Felipe Balbi743411b2010-12-01 13:22:05 +0200519static void tusb_musb_set_vbus(struct musb *musb, int is_on)
Felipe Balbi550a7372008-07-24 12:27:36 +0300520{
521 void __iomem *tbase = musb->ctrl_base;
522 u32 conf, prcm, timer;
523 u8 devctl;
524
525 /* HDRC controls CPEN, but beware current surges during device
526 * connect. They can trigger transient overcurrent conditions
527 * that must be ignored.
528 */
529
530 prcm = musb_readl(tbase, TUSB_PRCM_MNGMT);
531 conf = musb_readl(tbase, TUSB_DEV_CONF);
532 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
533
534 if (is_on) {
535 if (musb->set_clock)
536 musb->set_clock(musb->clock, 1);
537 timer = OTG_TIMER_MS(OTG_TIME_A_WAIT_VRISE);
David Brownell84e250f2009-03-31 12:30:04 -0700538 musb->xceiv->default_a = 1;
539 musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300540 devctl |= MUSB_DEVCTL_SESSION;
541
542 conf |= TUSB_DEV_CONF_USB_HOST_MODE;
543 MUSB_HST_MODE(musb);
544 } else {
545 u32 otg_stat;
546
547 timer = 0;
548
549 /* If ID pin is grounded, we want to be a_idle */
550 otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
551 if (!(otg_stat & TUSB_DEV_OTG_STAT_ID_STATUS)) {
David Brownell84e250f2009-03-31 12:30:04 -0700552 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300553 case OTG_STATE_A_WAIT_VRISE:
554 case OTG_STATE_A_WAIT_BCON:
David Brownell84e250f2009-03-31 12:30:04 -0700555 musb->xceiv->state = OTG_STATE_A_WAIT_VFALL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300556 break;
557 case OTG_STATE_A_WAIT_VFALL:
David Brownell84e250f2009-03-31 12:30:04 -0700558 musb->xceiv->state = OTG_STATE_A_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300559 break;
560 default:
David Brownell84e250f2009-03-31 12:30:04 -0700561 musb->xceiv->state = OTG_STATE_A_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300562 }
563 musb->is_active = 0;
David Brownell84e250f2009-03-31 12:30:04 -0700564 musb->xceiv->default_a = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +0300565 MUSB_HST_MODE(musb);
566 } else {
567 musb->is_active = 0;
David Brownell84e250f2009-03-31 12:30:04 -0700568 musb->xceiv->default_a = 0;
569 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300570 MUSB_DEV_MODE(musb);
571 }
572
573 devctl &= ~MUSB_DEVCTL_SESSION;
574 conf &= ~TUSB_DEV_CONF_USB_HOST_MODE;
575 if (musb->set_clock)
576 musb->set_clock(musb->clock, 0);
577 }
578 prcm &= ~(TUSB_PRCM_MNGMT_15_SW_EN | TUSB_PRCM_MNGMT_33_SW_EN);
579
580 musb_writel(tbase, TUSB_PRCM_MNGMT, prcm);
581 musb_writel(tbase, TUSB_DEV_OTG_TIMER, timer);
582 musb_writel(tbase, TUSB_DEV_CONF, conf);
583 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
584
585 DBG(1, "VBUS %s, devctl %02x otg %3x conf %08x prcm %08x\n",
586 otg_state_string(musb),
587 musb_readb(musb->mregs, MUSB_DEVCTL),
588 musb_readl(tbase, TUSB_DEV_OTG_STAT),
589 conf, prcm);
590}
591
592/*
593 * Sets the mode to OTG, peripheral or host by changing the ID detection.
594 * Caller must take care of locking.
595 *
596 * Note that if a mini-A cable is plugged in the ID line will stay down as
597 * the weak ID pull-up is not able to pull the ID up.
598 *
599 * REVISIT: It would be possible to add support for changing between host
600 * and peripheral modes in non-OTG configurations by reconfiguring hardware
601 * and then setting musb->board_mode. For now, only support OTG mode.
602 */
Felipe Balbi743411b2010-12-01 13:22:05 +0200603static int tusb_musb_set_mode(struct musb *musb, u8 musb_mode)
Felipe Balbi550a7372008-07-24 12:27:36 +0300604{
605 void __iomem *tbase = musb->ctrl_base;
606 u32 otg_stat, phy_otg_ctrl, phy_otg_ena, dev_conf;
607
608 if (musb->board_mode != MUSB_OTG) {
609 ERR("Changing mode currently only supported in OTG mode\n");
Felipe Balbi14a2c962008-10-29 15:10:36 +0200610 return -EINVAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300611 }
612
613 otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
614 phy_otg_ctrl = musb_readl(tbase, TUSB_PHY_OTG_CTRL);
615 phy_otg_ena = musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE);
616 dev_conf = musb_readl(tbase, TUSB_DEV_CONF);
617
618 switch (musb_mode) {
619
620#ifdef CONFIG_USB_MUSB_HDRC_HCD
621 case MUSB_HOST: /* Disable PHY ID detect, ground ID */
622 phy_otg_ctrl &= ~TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
623 phy_otg_ena |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
624 dev_conf |= TUSB_DEV_CONF_ID_SEL;
625 dev_conf &= ~TUSB_DEV_CONF_SOFT_ID;
626 break;
627#endif
628
629#ifdef CONFIG_USB_GADGET_MUSB_HDRC
630 case MUSB_PERIPHERAL: /* Disable PHY ID detect, keep ID pull-up on */
631 phy_otg_ctrl |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
632 phy_otg_ena |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
633 dev_conf |= (TUSB_DEV_CONF_ID_SEL | TUSB_DEV_CONF_SOFT_ID);
634 break;
635#endif
636
637#ifdef CONFIG_USB_MUSB_OTG
638 case MUSB_OTG: /* Use PHY ID detection */
639 phy_otg_ctrl |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
640 phy_otg_ena |= TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
641 dev_conf &= ~(TUSB_DEV_CONF_ID_SEL | TUSB_DEV_CONF_SOFT_ID);
642 break;
643#endif
644
645 default:
David Brownell96a274d2008-11-24 13:06:47 +0200646 DBG(2, "Trying to set mode %i\n", musb_mode);
647 return -EINVAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300648 }
649
650 musb_writel(tbase, TUSB_PHY_OTG_CTRL,
651 TUSB_PHY_OTG_CTRL_WRPROTECT | phy_otg_ctrl);
652 musb_writel(tbase, TUSB_PHY_OTG_CTRL_ENABLE,
653 TUSB_PHY_OTG_CTRL_WRPROTECT | phy_otg_ena);
654 musb_writel(tbase, TUSB_DEV_CONF, dev_conf);
655
656 otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
657 if ((musb_mode == MUSB_PERIPHERAL) &&
658 !(otg_stat & TUSB_DEV_OTG_STAT_ID_STATUS))
659 INFO("Cannot be peripheral with mini-A cable "
660 "otg_stat: %08x\n", otg_stat);
David Brownell96a274d2008-11-24 13:06:47 +0200661
662 return 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300663}
664
665static inline unsigned long
666tusb_otg_ints(struct musb *musb, u32 int_src, void __iomem *tbase)
667{
668 u32 otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
669 unsigned long idle_timeout = 0;
670
671 /* ID pin */
672 if ((int_src & TUSB_INT_SRC_ID_STATUS_CHNG)) {
673 int default_a;
674
675 if (is_otg_enabled(musb))
676 default_a = !(otg_stat & TUSB_DEV_OTG_STAT_ID_STATUS);
677 else
678 default_a = is_host_enabled(musb);
679 DBG(2, "Default-%c\n", default_a ? 'A' : 'B');
David Brownell84e250f2009-03-31 12:30:04 -0700680 musb->xceiv->default_a = default_a;
Felipe Balbi743411b2010-12-01 13:22:05 +0200681 tusb_musb_set_vbus(musb, default_a);
Felipe Balbi550a7372008-07-24 12:27:36 +0300682
683 /* Don't allow idling immediately */
684 if (default_a)
685 idle_timeout = jiffies + (HZ * 3);
686 }
687
688 /* VBUS state change */
689 if (int_src & TUSB_INT_SRC_VBUS_SENSE_CHNG) {
690
691 /* B-dev state machine: no vbus ~= disconnect */
David Brownell84e250f2009-03-31 12:30:04 -0700692 if ((is_otg_enabled(musb) && !musb->xceiv->default_a)
Felipe Balbi550a7372008-07-24 12:27:36 +0300693 || !is_host_enabled(musb)) {
694#ifdef CONFIG_USB_MUSB_HDRC_HCD
695 /* ? musb_root_disconnect(musb); */
696 musb->port1_status &=
697 ~(USB_PORT_STAT_CONNECTION
698 | USB_PORT_STAT_ENABLE
699 | USB_PORT_STAT_LOW_SPEED
700 | USB_PORT_STAT_HIGH_SPEED
701 | USB_PORT_STAT_TEST
702 );
703#endif
704
705 if (otg_stat & TUSB_DEV_OTG_STAT_SESS_END) {
706 DBG(1, "Forcing disconnect (no interrupt)\n");
David Brownell84e250f2009-03-31 12:30:04 -0700707 if (musb->xceiv->state != OTG_STATE_B_IDLE) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300708 /* INTR_DISCONNECT can hide... */
David Brownell84e250f2009-03-31 12:30:04 -0700709 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300710 musb->int_usb |= MUSB_INTR_DISCONNECT;
711 }
712 musb->is_active = 0;
713 }
714 DBG(2, "vbus change, %s, otg %03x\n",
715 otg_state_string(musb), otg_stat);
716 idle_timeout = jiffies + (1 * HZ);
717 schedule_work(&musb->irq_work);
718
719 } else /* A-dev state machine */ {
720 DBG(2, "vbus change, %s, otg %03x\n",
721 otg_state_string(musb), otg_stat);
722
David Brownell84e250f2009-03-31 12:30:04 -0700723 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300724 case OTG_STATE_A_IDLE:
725 DBG(2, "Got SRP, turning on VBUS\n");
Felipe Balbi743411b2010-12-01 13:22:05 +0200726 musb_platform_set_vbus(musb, 1);
Felipe Balbi550a7372008-07-24 12:27:36 +0300727
728 /* CONNECT can wake if a_wait_bcon is set */
729 if (musb->a_wait_bcon != 0)
730 musb->is_active = 0;
731 else
732 musb->is_active = 1;
733
734 /*
735 * OPT FS A TD.4.6 needs few seconds for
736 * A_WAIT_VRISE
737 */
738 idle_timeout = jiffies + (2 * HZ);
739
740 break;
741 case OTG_STATE_A_WAIT_VRISE:
742 /* ignore; A-session-valid < VBUS_VALID/2,
743 * we monitor this with the timer
744 */
745 break;
746 case OTG_STATE_A_WAIT_VFALL:
747 /* REVISIT this irq triggers during short
748 * spikes caused by enumeration ...
749 */
750 if (musb->vbuserr_retry) {
751 musb->vbuserr_retry--;
Felipe Balbi743411b2010-12-01 13:22:05 +0200752 tusb_musb_set_vbus(musb, 1);
Felipe Balbi550a7372008-07-24 12:27:36 +0300753 } else {
754 musb->vbuserr_retry
755 = VBUSERR_RETRY_COUNT;
Felipe Balbi743411b2010-12-01 13:22:05 +0200756 tusb_musb_set_vbus(musb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +0300757 }
758 break;
759 default:
760 break;
761 }
762 }
763 }
764
765 /* OTG timer expiration */
766 if (int_src & TUSB_INT_SRC_OTG_TIMEOUT) {
767 u8 devctl;
768
769 DBG(4, "%s timer, %03x\n", otg_state_string(musb), otg_stat);
770
David Brownell84e250f2009-03-31 12:30:04 -0700771 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300772 case OTG_STATE_A_WAIT_VRISE:
773 /* VBUS has probably been valid for a while now,
774 * but may well have bounced out of range a bit
775 */
776 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
777 if (otg_stat & TUSB_DEV_OTG_STAT_VBUS_VALID) {
778 if ((devctl & MUSB_DEVCTL_VBUS)
779 != MUSB_DEVCTL_VBUS) {
780 DBG(2, "devctl %02x\n", devctl);
781 break;
782 }
David Brownell84e250f2009-03-31 12:30:04 -0700783 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
Felipe Balbi550a7372008-07-24 12:27:36 +0300784 musb->is_active = 0;
785 idle_timeout = jiffies
786 + msecs_to_jiffies(musb->a_wait_bcon);
787 } else {
788 /* REVISIT report overcurrent to hub? */
789 ERR("vbus too slow, devctl %02x\n", devctl);
Felipe Balbi743411b2010-12-01 13:22:05 +0200790 tusb_musb_set_vbus(musb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +0300791 }
792 break;
793 case OTG_STATE_A_WAIT_BCON:
794 if (musb->a_wait_bcon != 0)
795 idle_timeout = jiffies
796 + msecs_to_jiffies(musb->a_wait_bcon);
797 break;
798 case OTG_STATE_A_SUSPEND:
799 break;
800 case OTG_STATE_B_WAIT_ACON:
801 break;
802 default:
803 break;
804 }
805 }
806 schedule_work(&musb->irq_work);
807
808 return idle_timeout;
809}
810
Felipe Balbi743411b2010-12-01 13:22:05 +0200811static irqreturn_t tusb_musb_interrupt(int irq, void *__hci)
Felipe Balbi550a7372008-07-24 12:27:36 +0300812{
813 struct musb *musb = __hci;
814 void __iomem *tbase = musb->ctrl_base;
815 unsigned long flags, idle_timeout = 0;
816 u32 int_mask, int_src;
817
818 spin_lock_irqsave(&musb->lock, flags);
819
820 /* Mask all interrupts to allow using both edge and level GPIO irq */
821 int_mask = musb_readl(tbase, TUSB_INT_MASK);
822 musb_writel(tbase, TUSB_INT_MASK, ~TUSB_INT_MASK_RESERVED_BITS);
823
824 int_src = musb_readl(tbase, TUSB_INT_SRC) & ~TUSB_INT_SRC_RESERVED_BITS;
825 DBG(3, "TUSB IRQ %08x\n", int_src);
826
827 musb->int_usb = (u8) int_src;
828
829 /* Acknowledge wake-up source interrupts */
830 if (int_src & TUSB_INT_SRC_DEV_WAKEUP) {
831 u32 reg;
832 u32 i;
833
834 if (tusb_get_revision(musb) == TUSB_REV_30)
835 tusb_wbus_quirk(musb, 0);
836
837 /* there are issues re-locking the PLL on wakeup ... */
838
839 /* work around issue 8 */
840 for (i = 0xf7f7f7; i > 0xf7f7f7 - 1000; i--) {
841 musb_writel(tbase, TUSB_SCRATCH_PAD, 0);
842 musb_writel(tbase, TUSB_SCRATCH_PAD, i);
843 reg = musb_readl(tbase, TUSB_SCRATCH_PAD);
844 if (reg == i)
845 break;
846 DBG(6, "TUSB NOR not ready\n");
847 }
848
849 /* work around issue 13 (2nd half) */
850 tusb_set_clock_source(musb, 1);
851
852 reg = musb_readl(tbase, TUSB_PRCM_WAKEUP_SOURCE);
853 musb_writel(tbase, TUSB_PRCM_WAKEUP_CLEAR, reg);
854 if (reg & ~TUSB_PRCM_WNORCS) {
855 musb->is_active = 1;
856 schedule_work(&musb->irq_work);
857 }
858 DBG(3, "wake %sactive %02x\n",
859 musb->is_active ? "" : "in", reg);
860
861 /* REVISIT host side TUSB_PRCM_WHOSTDISCON, TUSB_PRCM_WBUS */
862 }
863
864 if (int_src & TUSB_INT_SRC_USB_IP_CONN)
865 del_timer(&musb_idle_timer);
866
867 /* OTG state change reports (annoyingly) not issued by Mentor core */
868 if (int_src & (TUSB_INT_SRC_VBUS_SENSE_CHNG
869 | TUSB_INT_SRC_OTG_TIMEOUT
870 | TUSB_INT_SRC_ID_STATUS_CHNG))
871 idle_timeout = tusb_otg_ints(musb, int_src, tbase);
872
873 /* TX dma callback must be handled here, RX dma callback is
874 * handled in tusb_omap_dma_cb.
875 */
876 if ((int_src & TUSB_INT_SRC_TXRX_DMA_DONE)) {
877 u32 dma_src = musb_readl(tbase, TUSB_DMA_INT_SRC);
878 u32 real_dma_src = musb_readl(tbase, TUSB_DMA_INT_MASK);
879
880 DBG(3, "DMA IRQ %08x\n", dma_src);
881 real_dma_src = ~real_dma_src & dma_src;
882 if (tusb_dma_omap() && real_dma_src) {
883 int tx_source = (real_dma_src & 0xffff);
884 int i;
885
886 for (i = 1; i <= 15; i++) {
887 if (tx_source & (1 << i)) {
888 DBG(3, "completing ep%i %s\n", i, "tx");
889 musb_dma_completion(musb, i, 1);
890 }
891 }
892 }
893 musb_writel(tbase, TUSB_DMA_INT_CLEAR, dma_src);
894 }
895
896 /* EP interrupts. In OCP mode tusb6010 mirrors the MUSB interrupts */
897 if (int_src & (TUSB_INT_SRC_USB_IP_TX | TUSB_INT_SRC_USB_IP_RX)) {
898 u32 musb_src = musb_readl(tbase, TUSB_USBIP_INT_SRC);
899
900 musb_writel(tbase, TUSB_USBIP_INT_CLEAR, musb_src);
901 musb->int_rx = (((musb_src >> 16) & 0xffff) << 1);
902 musb->int_tx = (musb_src & 0xffff);
903 } else {
904 musb->int_rx = 0;
905 musb->int_tx = 0;
906 }
907
908 if (int_src & (TUSB_INT_SRC_USB_IP_TX | TUSB_INT_SRC_USB_IP_RX | 0xff))
909 musb_interrupt(musb);
910
911 /* Acknowledge TUSB interrupts. Clear only non-reserved bits */
912 musb_writel(tbase, TUSB_INT_SRC_CLEAR,
913 int_src & ~TUSB_INT_MASK_RESERVED_BITS);
914
Felipe Balbi743411b2010-12-01 13:22:05 +0200915 tusb_musb_try_idle(musb, idle_timeout);
Felipe Balbi550a7372008-07-24 12:27:36 +0300916
917 musb_writel(tbase, TUSB_INT_MASK, int_mask);
918 spin_unlock_irqrestore(&musb->lock, flags);
919
920 return IRQ_HANDLED;
921}
922
923static int dma_off;
924
925/*
926 * Enables TUSB6010. Caller must take care of locking.
927 * REVISIT:
928 * - Check what is unnecessary in MGC_HdrcStart()
929 */
Felipe Balbi743411b2010-12-01 13:22:05 +0200930static void tusb_musb_enable(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300931{
932 void __iomem *tbase = musb->ctrl_base;
933
934 /* Setup TUSB6010 main interrupt mask. Enable all interrupts except SOF.
935 * REVISIT: Enable and deal with TUSB_INT_SRC_USB_IP_SOF */
936 musb_writel(tbase, TUSB_INT_MASK, TUSB_INT_SRC_USB_IP_SOF);
937
938 /* Setup TUSB interrupt, disable DMA and GPIO interrupts */
939 musb_writel(tbase, TUSB_USBIP_INT_MASK, 0);
940 musb_writel(tbase, TUSB_DMA_INT_MASK, 0x7fffffff);
941 musb_writel(tbase, TUSB_GPIO_INT_MASK, 0x1ff);
942
943 /* Clear all subsystem interrups */
944 musb_writel(tbase, TUSB_USBIP_INT_CLEAR, 0x7fffffff);
945 musb_writel(tbase, TUSB_DMA_INT_CLEAR, 0x7fffffff);
946 musb_writel(tbase, TUSB_GPIO_INT_CLEAR, 0x1ff);
947
948 /* Acknowledge pending interrupt(s) */
949 musb_writel(tbase, TUSB_INT_SRC_CLEAR, ~TUSB_INT_MASK_RESERVED_BITS);
950
951 /* Only 0 clock cycles for minimum interrupt de-assertion time and
952 * interrupt polarity active low seems to work reliably here */
953 musb_writel(tbase, TUSB_INT_CTRL_CONF,
954 TUSB_INT_CTRL_CONF_INT_RELCYC(0));
955
956 set_irq_type(musb->nIrq, IRQ_TYPE_LEVEL_LOW);
957
958 /* maybe force into the Default-A OTG state machine */
959 if (!(musb_readl(tbase, TUSB_DEV_OTG_STAT)
960 & TUSB_DEV_OTG_STAT_ID_STATUS))
961 musb_writel(tbase, TUSB_INT_SRC_SET,
962 TUSB_INT_SRC_ID_STATUS_CHNG);
963
964 if (is_dma_capable() && dma_off)
965 printk(KERN_WARNING "%s %s: dma not reactivated\n",
966 __FILE__, __func__);
967 else
968 dma_off = 1;
969}
970
971/*
972 * Disables TUSB6010. Caller must take care of locking.
973 */
Felipe Balbi743411b2010-12-01 13:22:05 +0200974static void tusb_musb_disable(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300975{
976 void __iomem *tbase = musb->ctrl_base;
977
978 /* FIXME stop DMA, IRQs, timers, ... */
979
980 /* disable all IRQs */
981 musb_writel(tbase, TUSB_INT_MASK, ~TUSB_INT_MASK_RESERVED_BITS);
982 musb_writel(tbase, TUSB_USBIP_INT_MASK, 0x7fffffff);
983 musb_writel(tbase, TUSB_DMA_INT_MASK, 0x7fffffff);
984 musb_writel(tbase, TUSB_GPIO_INT_MASK, 0x1ff);
985
986 del_timer(&musb_idle_timer);
987
988 if (is_dma_capable() && !dma_off) {
989 printk(KERN_WARNING "%s %s: dma still active\n",
990 __FILE__, __func__);
991 dma_off = 1;
992 }
993}
994
995/*
996 * Sets up TUSB6010 CPU interface specific signals and registers
997 * Note: Settings optimized for OMAP24xx
998 */
Felipe Balbi743411b2010-12-01 13:22:05 +0200999static void tusb_setup_cpu_interface(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001000{
1001 void __iomem *tbase = musb->ctrl_base;
1002
1003 /*
1004 * Disable GPIO[5:0] pullups (used as output DMA requests)
1005 * Don't disable GPIO[7:6] as they are needed for wake-up.
1006 */
1007 musb_writel(tbase, TUSB_PULLUP_1_CTRL, 0x0000003F);
1008
1009 /* Disable all pullups on NOR IF, DMAREQ0 and DMAREQ1 */
1010 musb_writel(tbase, TUSB_PULLUP_2_CTRL, 0x01FFFFFF);
1011
1012 /* Turn GPIO[5:0] to DMAREQ[5:0] signals */
1013 musb_writel(tbase, TUSB_GPIO_CONF, TUSB_GPIO_CONF_DMAREQ(0x3f));
1014
1015 /* Burst size 16x16 bits, all six DMA requests enabled, DMA request
1016 * de-assertion time 2 system clocks p 62 */
1017 musb_writel(tbase, TUSB_DMA_REQ_CONF,
1018 TUSB_DMA_REQ_CONF_BURST_SIZE(2) |
1019 TUSB_DMA_REQ_CONF_DMA_REQ_EN(0x3f) |
1020 TUSB_DMA_REQ_CONF_DMA_REQ_ASSER(2));
1021
1022 /* Set 0 wait count for synchronous burst access */
1023 musb_writel(tbase, TUSB_WAIT_COUNT, 1);
1024}
1025
Felipe Balbi743411b2010-12-01 13:22:05 +02001026static int tusb_musb_start(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001027{
1028 void __iomem *tbase = musb->ctrl_base;
1029 int ret = 0;
1030 unsigned long flags;
1031 u32 reg;
1032
1033 if (musb->board_set_power)
1034 ret = musb->board_set_power(1);
1035 if (ret != 0) {
1036 printk(KERN_ERR "tusb: Cannot enable TUSB6010\n");
1037 return ret;
1038 }
1039
1040 spin_lock_irqsave(&musb->lock, flags);
1041
1042 if (musb_readl(tbase, TUSB_PROD_TEST_RESET) !=
1043 TUSB_PROD_TEST_RESET_VAL) {
1044 printk(KERN_ERR "tusb: Unable to detect TUSB6010\n");
1045 goto err;
1046 }
1047
1048 ret = tusb_print_revision(musb);
1049 if (ret < 2) {
1050 printk(KERN_ERR "tusb: Unsupported TUSB6010 revision %i\n",
1051 ret);
1052 goto err;
1053 }
1054
1055 /* The uint bit for "USB non-PDR interrupt enable" has to be 1 when
1056 * NOR FLASH interface is used */
1057 musb_writel(tbase, TUSB_VLYNQ_CTRL, 8);
1058
1059 /* Select PHY free running 60MHz as a system clock */
1060 tusb_set_clock_source(musb, 1);
1061
1062 /* VBus valid timer 1us, disable DFT/Debug and VLYNQ clocks for
1063 * power saving, enable VBus detect and session end comparators,
1064 * enable IDpullup, enable VBus charging */
1065 musb_writel(tbase, TUSB_PRCM_MNGMT,
1066 TUSB_PRCM_MNGMT_VBUS_VALID_TIMER(0xa) |
1067 TUSB_PRCM_MNGMT_VBUS_VALID_FLT_EN |
1068 TUSB_PRCM_MNGMT_OTG_SESS_END_EN |
1069 TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN |
1070 TUSB_PRCM_MNGMT_OTG_ID_PULLUP);
1071 tusb_setup_cpu_interface(musb);
1072
1073 /* simplify: always sense/pullup ID pins, as if in OTG mode */
1074 reg = musb_readl(tbase, TUSB_PHY_OTG_CTRL_ENABLE);
1075 reg |= TUSB_PHY_OTG_CTRL_WRPROTECT | TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
1076 musb_writel(tbase, TUSB_PHY_OTG_CTRL_ENABLE, reg);
1077
1078 reg = musb_readl(tbase, TUSB_PHY_OTG_CTRL);
1079 reg |= TUSB_PHY_OTG_CTRL_WRPROTECT | TUSB_PHY_OTG_CTRL_OTG_ID_PULLUP;
1080 musb_writel(tbase, TUSB_PHY_OTG_CTRL, reg);
1081
1082 spin_unlock_irqrestore(&musb->lock, flags);
1083
1084 return 0;
1085
1086err:
1087 spin_unlock_irqrestore(&musb->lock, flags);
1088
1089 if (musb->board_set_power)
1090 musb->board_set_power(0);
1091
1092 return -ENODEV;
1093}
1094
Felipe Balbi743411b2010-12-01 13:22:05 +02001095static int tusb_musb_init(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001096{
1097 struct platform_device *pdev;
1098 struct resource *mem;
David Brownell84e250f2009-03-31 12:30:04 -07001099 void __iomem *sync = NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001100 int ret;
1101
David Brownell84e250f2009-03-31 12:30:04 -07001102 usb_nop_xceiv_register();
1103 musb->xceiv = otg_get_transceiver();
1104 if (!musb->xceiv)
1105 return -ENODEV;
1106
Felipe Balbi550a7372008-07-24 12:27:36 +03001107 pdev = to_platform_device(musb->controller);
1108
1109 /* dma address for async dma */
1110 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1111 musb->async = mem->start;
1112
1113 /* dma address for sync dma */
1114 mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1115 if (!mem) {
1116 pr_debug("no sync dma resource?\n");
David Brownell84e250f2009-03-31 12:30:04 -07001117 ret = -ENODEV;
1118 goto done;
Felipe Balbi550a7372008-07-24 12:27:36 +03001119 }
1120 musb->sync = mem->start;
1121
Felipe Balbi3d268642010-01-21 15:33:56 +02001122 sync = ioremap(mem->start, resource_size(mem));
Felipe Balbi550a7372008-07-24 12:27:36 +03001123 if (!sync) {
1124 pr_debug("ioremap for sync failed\n");
David Brownell84e250f2009-03-31 12:30:04 -07001125 ret = -ENOMEM;
1126 goto done;
Felipe Balbi550a7372008-07-24 12:27:36 +03001127 }
1128 musb->sync_va = sync;
1129
1130 /* Offsets from base: VLYNQ at 0x000, MUSB regs at 0x400,
1131 * FIFOs at 0x600, TUSB at 0x800
1132 */
1133 musb->mregs += TUSB_BASE_OFFSET;
1134
Felipe Balbi743411b2010-12-01 13:22:05 +02001135 ret = tusb_musb_start(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001136 if (ret) {
1137 printk(KERN_ERR "Could not start tusb6010 (%d)\n",
1138 ret);
David Brownell84e250f2009-03-31 12:30:04 -07001139 goto done;
Felipe Balbi550a7372008-07-24 12:27:36 +03001140 }
Felipe Balbi743411b2010-12-01 13:22:05 +02001141 musb->isr = tusb_musb_interrupt;
Felipe Balbi550a7372008-07-24 12:27:36 +03001142
David Brownell84e250f2009-03-31 12:30:04 -07001143 if (is_peripheral_enabled(musb)) {
1144 musb->xceiv->set_power = tusb_draw_power;
1145 the_musb = musb;
1146 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001147
1148 setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
1149
David Brownell84e250f2009-03-31 12:30:04 -07001150done:
1151 if (ret < 0) {
1152 if (sync)
1153 iounmap(sync);
Sergei Shtylyovf4053872010-09-29 09:54:29 +03001154
1155 otg_put_transceiver(musb->xceiv);
David Brownell84e250f2009-03-31 12:30:04 -07001156 usb_nop_xceiv_unregister();
1157 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001158 return ret;
1159}
1160
Felipe Balbi743411b2010-12-01 13:22:05 +02001161static int tusb_musb_exit(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001162{
1163 del_timer_sync(&musb_idle_timer);
David Brownell84e250f2009-03-31 12:30:04 -07001164 the_musb = NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001165
1166 if (musb->board_set_power)
1167 musb->board_set_power(0);
1168
1169 iounmap(musb->sync_va);
Sergei Shtylyovf4053872010-09-29 09:54:29 +03001170
1171 otg_put_transceiver(musb->xceiv);
David Brownell84e250f2009-03-31 12:30:04 -07001172 usb_nop_xceiv_unregister();
Felipe Balbi550a7372008-07-24 12:27:36 +03001173 return 0;
1174}
Felipe Balbi743411b2010-12-01 13:22:05 +02001175
1176const struct musb_platform_ops musb_ops = {
1177 .init = tusb_musb_init,
1178 .exit = tusb_musb_exit,
1179
1180 .enable = tusb_musb_enable,
1181 .disable = tusb_musb_disable,
1182
1183 .set_mode = tusb_musb_set_mode,
1184 .try_idle = tusb_musb_try_idle,
1185
1186 .vbus_status = tusb_musb_vbus_status,
1187 .set_vbus = tusb_musb_set_vbus,
1188};
Felipe Balbi18688fb2010-12-02 09:13:54 +02001189
1190static u64 tusb_dmamask = DMA_BIT_MASK(32);
1191
1192static int __init tusb_probe(struct platform_device *pdev)
1193{
1194 struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
1195 struct platform_device *musb;
1196
1197 int ret = -ENOMEM;
1198
1199 musb = platform_device_alloc("musb-hdrc", -1);
1200 if (!musb) {
1201 dev_err(&pdev->dev, "failed to allocate musb device\n");
1202 goto err0;
1203 }
1204
1205 musb->dev.parent = &pdev->dev;
1206 musb->dev.dma_mask = &tusb_dmamask;
1207 musb->dev.coherent_dma_mask = tusb_dmamask;
1208
1209 platform_set_drvdata(pdev, musb);
1210
1211 ret = platform_device_add_resources(musb, pdev->resource,
1212 pdev->num_resources);
1213 if (ret) {
1214 dev_err(&pdev->dev, "failed to add resources\n");
1215 goto err1;
1216 }
1217
1218 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
1219 if (ret) {
1220 dev_err(&pdev->dev, "failed to add platform_data\n");
1221 goto err1;
1222 }
1223
1224 ret = platform_device_add(musb);
1225 if (ret) {
1226 dev_err(&pdev->dev, "failed to register musb device\n");
1227 goto err1;
1228 }
1229
1230 return 0;
1231
1232err1:
1233 platform_device_put(musb);
1234
1235err0:
1236 return ret;
1237}
1238
1239static int __exit tusb_remove(struct platform_device *pdev)
1240{
1241 struct platform_device *musb = platform_get_drvdata(pdev);
1242
1243 platform_device_del(musb);
1244 platform_device_put(musb);
1245
1246 return 0;
1247}
1248
1249static struct platform_driver tusb_driver = {
1250 .remove = __exit_p(tusb_remove),
1251 .driver = {
1252 .name = "musb-tusb",
1253 },
1254};
1255
1256MODULE_DESCRIPTION("TUSB6010 MUSB Glue Layer");
1257MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
1258MODULE_LICENSE("GPL v2");
1259
1260static int __init tusb_init(void)
1261{
1262 return platform_driver_probe(&tusb_driver, tusb_probe);
1263}
1264subsys_initcall(tusb_init);
1265
1266static void __exit tusb_exit(void)
1267{
1268 platform_driver_unregister(&tusb_driver);
1269}
1270module_exit(tusb_exit);