blob: 16798c9e6f38553865ee3cad2c167ddbefa9e566 [file] [log] [blame]
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +03001/*
2 * Texas Instruments DA8xx/OMAP-L1x "glue layer"
3 *
4 * Copyright (c) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
5 *
6 * Based on the DaVinci "glue layer" code.
7 * Copyright (C) 2005-2006 by Texas Instruments
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
29#include <linux/init.h>
Felipe Balbiab570da2011-11-10 09:58:04 +020030#include <linux/module.h>
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +030031#include <linux/clk.h>
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +053032#include <linux/err.h>
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +030033#include <linux/io.h>
Felipe Balbi8ceae512010-12-02 09:19:35 +020034#include <linux/platform_device.h>
35#include <linux/dma-mapping.h>
Felipe Balbi78c289f2012-07-19 13:32:15 +030036#include <linux/usb/nop-usb-xceiv.h>
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +030037
38#include <mach/da8xx.h>
39#include <mach/usb.h>
40
41#include "musb_core.h"
42
43/*
44 * DA8XX specific definitions
45 */
46
47/* USB 2.0 OTG module registers */
48#define DA8XX_USB_REVISION_REG 0x00
49#define DA8XX_USB_CTRL_REG 0x04
50#define DA8XX_USB_STAT_REG 0x08
51#define DA8XX_USB_EMULATION_REG 0x0c
52#define DA8XX_USB_MODE_REG 0x10 /* Transparent, CDC, [Generic] RNDIS */
53#define DA8XX_USB_AUTOREQ_REG 0x14
54#define DA8XX_USB_SRP_FIX_TIME_REG 0x18
55#define DA8XX_USB_TEARDOWN_REG 0x1c
56#define DA8XX_USB_INTR_SRC_REG 0x20
57#define DA8XX_USB_INTR_SRC_SET_REG 0x24
58#define DA8XX_USB_INTR_SRC_CLEAR_REG 0x28
59#define DA8XX_USB_INTR_MASK_REG 0x2c
60#define DA8XX_USB_INTR_MASK_SET_REG 0x30
61#define DA8XX_USB_INTR_MASK_CLEAR_REG 0x34
62#define DA8XX_USB_INTR_SRC_MASKED_REG 0x38
63#define DA8XX_USB_END_OF_INTR_REG 0x3c
64#define DA8XX_USB_GENERIC_RNDIS_EP_SIZE_REG(n) (0x50 + (((n) - 1) << 2))
65
66/* Control register bits */
67#define DA8XX_SOFT_RESET_MASK 1
68
69#define DA8XX_USB_TX_EP_MASK 0x1f /* EP0 + 4 Tx EPs */
70#define DA8XX_USB_RX_EP_MASK 0x1e /* 4 Rx EPs */
71
72/* USB interrupt register bits */
73#define DA8XX_INTR_USB_SHIFT 16
74#define DA8XX_INTR_USB_MASK (0x1ff << DA8XX_INTR_USB_SHIFT) /* 8 Mentor */
75 /* interrupts and DRVVBUS interrupt */
76#define DA8XX_INTR_DRVVBUS 0x100
77#define DA8XX_INTR_RX_SHIFT 8
78#define DA8XX_INTR_RX_MASK (DA8XX_USB_RX_EP_MASK << DA8XX_INTR_RX_SHIFT)
79#define DA8XX_INTR_TX_SHIFT 0
80#define DA8XX_INTR_TX_MASK (DA8XX_USB_TX_EP_MASK << DA8XX_INTR_TX_SHIFT)
81
82#define DA8XX_MENTOR_CORE_OFFSET 0x400
83
84#define CFGCHIP2 IO_ADDRESS(DA8XX_SYSCFG0_BASE + DA8XX_CFGCHIP2_REG)
85
Felipe Balbie6480fa2010-12-02 09:40:34 +020086struct da8xx_glue {
87 struct device *dev;
88 struct platform_device *musb;
Felipe Balbi03491762010-12-02 09:57:08 +020089 struct clk *clk;
Felipe Balbie6480fa2010-12-02 09:40:34 +020090};
91
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +030092/*
93 * REVISIT (PM): we should be able to keep the PHY in low power mode most
94 * of the time (24 MHz oscillator and PLL off, etc.) by setting POWER.D0
95 * and, when in host mode, autosuspending idle root ports... PHY_PLLON
96 * (overriding SUSPENDM?) then likely needs to stay off.
97 */
98
99static inline void phy_on(void)
100{
101 u32 cfgchip2 = __raw_readl(CFGCHIP2);
102
103 /*
104 * Start the on-chip PHY and its PLL.
105 */
106 cfgchip2 &= ~(CFGCHIP2_RESET | CFGCHIP2_PHYPWRDN | CFGCHIP2_OTGPWRDN);
107 cfgchip2 |= CFGCHIP2_PHY_PLLON;
108 __raw_writel(cfgchip2, CFGCHIP2);
109
110 pr_info("Waiting for USB PHY clock good...\n");
111 while (!(__raw_readl(CFGCHIP2) & CFGCHIP2_PHYCLKGD))
112 cpu_relax();
113}
114
115static inline void phy_off(void)
116{
117 u32 cfgchip2 = __raw_readl(CFGCHIP2);
118
119 /*
120 * Ensure that USB 1.1 reference clock is not being sourced from
121 * USB 2.0 PHY. Otherwise do not power down the PHY.
122 */
123 if (!(cfgchip2 & CFGCHIP2_USB1PHYCLKMUX) &&
124 (cfgchip2 & CFGCHIP2_USB1SUSPENDM)) {
125 pr_warning("USB 1.1 clocked from USB 2.0 PHY -- "
126 "can't power it down\n");
127 return;
128 }
129
130 /*
131 * Power down the on-chip PHY.
132 */
133 cfgchip2 |= CFGCHIP2_PHYPWRDN | CFGCHIP2_OTGPWRDN;
134 __raw_writel(cfgchip2, CFGCHIP2);
135}
136
137/*
138 * Because we don't set CTRL.UINT, it's "important" to:
139 * - not read/write INTRUSB/INTRUSBE (except during
140 * initial setup, as a workaround);
141 * - use INTSET/INTCLR instead.
142 */
143
144/**
Felipe Balbi743411b2010-12-01 13:22:05 +0200145 * da8xx_musb_enable - enable interrupts
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300146 */
Felipe Balbi743411b2010-12-01 13:22:05 +0200147static void da8xx_musb_enable(struct musb *musb)
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300148{
149 void __iomem *reg_base = musb->ctrl_base;
150 u32 mask;
151
152 /* Workaround: setup IRQs through both register sets. */
153 mask = ((musb->epmask & DA8XX_USB_TX_EP_MASK) << DA8XX_INTR_TX_SHIFT) |
154 ((musb->epmask & DA8XX_USB_RX_EP_MASK) << DA8XX_INTR_RX_SHIFT) |
155 DA8XX_INTR_USB_MASK;
156 musb_writel(reg_base, DA8XX_USB_INTR_MASK_SET_REG, mask);
157
158 /* Force the DRVVBUS IRQ so we can start polling for ID change. */
159 if (is_otg_enabled(musb))
160 musb_writel(reg_base, DA8XX_USB_INTR_SRC_SET_REG,
161 DA8XX_INTR_DRVVBUS << DA8XX_INTR_USB_SHIFT);
162}
163
164/**
Felipe Balbi743411b2010-12-01 13:22:05 +0200165 * da8xx_musb_disable - disable HDRC and flush interrupts
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300166 */
Felipe Balbi743411b2010-12-01 13:22:05 +0200167static void da8xx_musb_disable(struct musb *musb)
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300168{
169 void __iomem *reg_base = musb->ctrl_base;
170
171 musb_writel(reg_base, DA8XX_USB_INTR_MASK_CLEAR_REG,
172 DA8XX_INTR_USB_MASK |
173 DA8XX_INTR_TX_MASK | DA8XX_INTR_RX_MASK);
174 musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
175 musb_writel(reg_base, DA8XX_USB_END_OF_INTR_REG, 0);
176}
177
Felipe Balbi62285962011-06-22 17:28:09 +0300178#define portstate(stmt) stmt
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300179
Felipe Balbi743411b2010-12-01 13:22:05 +0200180static void da8xx_musb_set_vbus(struct musb *musb, int is_on)
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300181{
182 WARN_ON(is_on && is_peripheral_active(musb));
183}
184
185#define POLL_SECONDS 2
186
187static struct timer_list otg_workaround;
188
189static void otg_timer(unsigned long _musb)
190{
191 struct musb *musb = (void *)_musb;
192 void __iomem *mregs = musb->mregs;
193 u8 devctl;
194 unsigned long flags;
195
196 /*
197 * We poll because DaVinci's won't expose several OTG-critical
198 * status change events (from the transceiver) otherwise.
199 */
200 devctl = musb_readb(mregs, MUSB_DEVCTL);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300201 dev_dbg(musb->controller, "Poll devctl %02x (%s)\n", devctl,
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200202 otg_state_string(musb->xceiv->state));
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300203
204 spin_lock_irqsave(&musb->lock, flags);
205 switch (musb->xceiv->state) {
206 case OTG_STATE_A_WAIT_BCON:
207 devctl &= ~MUSB_DEVCTL_SESSION;
208 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
209
210 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
211 if (devctl & MUSB_DEVCTL_BDEVICE) {
212 musb->xceiv->state = OTG_STATE_B_IDLE;
213 MUSB_DEV_MODE(musb);
214 } else {
215 musb->xceiv->state = OTG_STATE_A_IDLE;
216 MUSB_HST_MODE(musb);
217 }
218 break;
219 case OTG_STATE_A_WAIT_VFALL:
220 /*
221 * Wait till VBUS falls below SessionEnd (~0.2 V); the 1.3
222 * RTL seems to mis-handle session "start" otherwise (or in
223 * our case "recover"), in routine "VBUS was valid by the time
224 * VBUSERR got reported during enumeration" cases.
225 */
226 if (devctl & MUSB_DEVCTL_VBUS) {
227 mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ);
228 break;
229 }
230 musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
231 musb_writel(musb->ctrl_base, DA8XX_USB_INTR_SRC_SET_REG,
232 MUSB_INTR_VBUSERROR << DA8XX_INTR_USB_SHIFT);
233 break;
234 case OTG_STATE_B_IDLE:
235 if (!is_peripheral_enabled(musb))
236 break;
237
238 /*
239 * There's no ID-changed IRQ, so we have no good way to tell
240 * when to switch to the A-Default state machine (by setting
241 * the DEVCTL.Session bit).
242 *
243 * Workaround: whenever we're in B_IDLE, try setting the
244 * session flag every few seconds. If it works, ID was
245 * grounded and we're now in the A-Default state machine.
246 *
247 * NOTE: setting the session flag is _supposed_ to trigger
248 * SRP but clearly it doesn't.
249 */
250 musb_writeb(mregs, MUSB_DEVCTL, devctl | MUSB_DEVCTL_SESSION);
251 devctl = musb_readb(mregs, MUSB_DEVCTL);
252 if (devctl & MUSB_DEVCTL_BDEVICE)
253 mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ);
254 else
255 musb->xceiv->state = OTG_STATE_A_IDLE;
256 break;
257 default:
258 break;
259 }
260 spin_unlock_irqrestore(&musb->lock, flags);
261}
262
Felipe Balbi743411b2010-12-01 13:22:05 +0200263static void da8xx_musb_try_idle(struct musb *musb, unsigned long timeout)
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300264{
265 static unsigned long last_timer;
266
267 if (!is_otg_enabled(musb))
268 return;
269
270 if (timeout == 0)
271 timeout = jiffies + msecs_to_jiffies(3);
272
273 /* Never idle if active, or when VBUS timeout is not set as host */
274 if (musb->is_active || (musb->a_wait_bcon == 0 &&
275 musb->xceiv->state == OTG_STATE_A_WAIT_BCON)) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300276 dev_dbg(musb->controller, "%s active, deleting timer\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200277 otg_state_string(musb->xceiv->state));
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300278 del_timer(&otg_workaround);
279 last_timer = jiffies;
280 return;
281 }
282
283 if (time_after(last_timer, timeout) && timer_pending(&otg_workaround)) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300284 dev_dbg(musb->controller, "Longer idle timer already pending, ignoring...\n");
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300285 return;
286 }
287 last_timer = timeout;
288
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300289 dev_dbg(musb->controller, "%s inactive, starting idle timer for %u ms\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200290 otg_state_string(musb->xceiv->state),
291 jiffies_to_msecs(timeout - jiffies));
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300292 mod_timer(&otg_workaround, timeout);
293}
294
Felipe Balbi743411b2010-12-01 13:22:05 +0200295static irqreturn_t da8xx_musb_interrupt(int irq, void *hci)
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300296{
297 struct musb *musb = hci;
298 void __iomem *reg_base = musb->ctrl_base;
Heikki Krogerusd445b6d2012-02-13 13:24:15 +0200299 struct usb_otg *otg = musb->xceiv->otg;
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300300 unsigned long flags;
301 irqreturn_t ret = IRQ_NONE;
302 u32 status;
303
304 spin_lock_irqsave(&musb->lock, flags);
305
306 /*
307 * NOTE: DA8XX shadows the Mentor IRQs. Don't manage them through
308 * the Mentor registers (except for setup), use the TI ones and EOI.
309 */
310
311 /* Acknowledge and handle non-CPPI interrupts */
312 status = musb_readl(reg_base, DA8XX_USB_INTR_SRC_MASKED_REG);
313 if (!status)
314 goto eoi;
315
316 musb_writel(reg_base, DA8XX_USB_INTR_SRC_CLEAR_REG, status);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300317 dev_dbg(musb->controller, "USB IRQ %08x\n", status);
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300318
319 musb->int_rx = (status & DA8XX_INTR_RX_MASK) >> DA8XX_INTR_RX_SHIFT;
320 musb->int_tx = (status & DA8XX_INTR_TX_MASK) >> DA8XX_INTR_TX_SHIFT;
321 musb->int_usb = (status & DA8XX_INTR_USB_MASK) >> DA8XX_INTR_USB_SHIFT;
322
323 /*
324 * DRVVBUS IRQs are the only proxy we have (a very poor one!) for
325 * DA8xx's missing ID change IRQ. We need an ID change IRQ to
326 * switch appropriately between halves of the OTG state machine.
327 * Managing DEVCTL.Session per Mentor docs requires that we know its
328 * value but DEVCTL.BDevice is invalid without DEVCTL.Session set.
329 * Also, DRVVBUS pulses for SRP (but not at 5 V)...
330 */
331 if (status & (DA8XX_INTR_DRVVBUS << DA8XX_INTR_USB_SHIFT)) {
332 int drvvbus = musb_readl(reg_base, DA8XX_USB_STAT_REG);
333 void __iomem *mregs = musb->mregs;
334 u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
335 int err;
336
337 err = is_host_enabled(musb) && (musb->int_usb &
338 MUSB_INTR_VBUSERROR);
339 if (err) {
340 /*
341 * The Mentor core doesn't debounce VBUS as needed
342 * to cope with device connect current spikes. This
343 * means it's not uncommon for bus-powered devices
344 * to get VBUS errors during enumeration.
345 *
346 * This is a workaround, but newer RTL from Mentor
347 * seems to allow a better one: "re"-starting sessions
348 * without waiting for VBUS to stop registering in
349 * devctl.
350 */
351 musb->int_usb &= ~MUSB_INTR_VBUSERROR;
352 musb->xceiv->state = OTG_STATE_A_WAIT_VFALL;
353 mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ);
354 WARNING("VBUS error workaround (delay coming)\n");
355 } else if (is_host_enabled(musb) && drvvbus) {
356 MUSB_HST_MODE(musb);
Heikki Krogerusd445b6d2012-02-13 13:24:15 +0200357 otg->default_a = 1;
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300358 musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
359 portstate(musb->port1_status |= USB_PORT_STAT_POWER);
360 del_timer(&otg_workaround);
361 } else {
362 musb->is_active = 0;
363 MUSB_DEV_MODE(musb);
Heikki Krogerusd445b6d2012-02-13 13:24:15 +0200364 otg->default_a = 0;
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300365 musb->xceiv->state = OTG_STATE_B_IDLE;
366 portstate(musb->port1_status &= ~USB_PORT_STAT_POWER);
367 }
368
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300369 dev_dbg(musb->controller, "VBUS %s (%s)%s, devctl %02x\n",
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300370 drvvbus ? "on" : "off",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200371 otg_state_string(musb->xceiv->state),
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300372 err ? " ERROR" : "",
373 devctl);
374 ret = IRQ_HANDLED;
375 }
376
377 if (musb->int_tx || musb->int_rx || musb->int_usb)
378 ret |= musb_interrupt(musb);
379
380 eoi:
381 /* EOI needs to be written for the IRQ to be re-asserted. */
382 if (ret == IRQ_HANDLED || status)
383 musb_writel(reg_base, DA8XX_USB_END_OF_INTR_REG, 0);
384
385 /* Poll for ID change */
386 if (is_otg_enabled(musb) && musb->xceiv->state == OTG_STATE_B_IDLE)
387 mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ);
388
389 spin_unlock_irqrestore(&musb->lock, flags);
390
391 return ret;
392}
393
Felipe Balbi743411b2010-12-01 13:22:05 +0200394static int da8xx_musb_set_mode(struct musb *musb, u8 musb_mode)
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300395{
396 u32 cfgchip2 = __raw_readl(CFGCHIP2);
397
398 cfgchip2 &= ~CFGCHIP2_OTGMODE;
399 switch (musb_mode) {
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300400 case MUSB_HOST: /* Force VBUS valid, ID = 0 */
401 cfgchip2 |= CFGCHIP2_FORCE_HOST;
402 break;
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300403 case MUSB_PERIPHERAL: /* Force VBUS valid, ID = 1 */
404 cfgchip2 |= CFGCHIP2_FORCE_DEVICE;
405 break;
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300406 case MUSB_OTG: /* Don't override the VBUS/ID comparators */
407 cfgchip2 |= CFGCHIP2_NO_OVERRIDE;
408 break;
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300409 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300410 dev_dbg(musb->controller, "Trying to set unsupported mode %u\n", musb_mode);
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300411 }
412
413 __raw_writel(cfgchip2, CFGCHIP2);
414 return 0;
415}
416
Felipe Balbi743411b2010-12-01 13:22:05 +0200417static int da8xx_musb_init(struct musb *musb)
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300418{
419 void __iomem *reg_base = musb->ctrl_base;
420 u32 rev;
421
422 musb->mregs += DA8XX_MENTOR_CORE_OFFSET;
423
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300424 /* Returns zero if e.g. not clocked */
425 rev = musb_readl(reg_base, DA8XX_USB_REVISION_REG);
426 if (!rev)
427 goto fail;
428
429 usb_nop_xceiv_register();
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530430 musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530431 if (IS_ERR_OR_NULL(musb->xceiv))
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300432 goto fail;
433
434 if (is_host_enabled(musb))
435 setup_timer(&otg_workaround, otg_timer, (unsigned long)musb);
436
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300437 /* Reset the controller */
438 musb_writel(reg_base, DA8XX_USB_CTRL_REG, DA8XX_SOFT_RESET_MASK);
439
440 /* Start the on-chip PHY and its PLL. */
441 phy_on();
442
443 msleep(5);
444
445 /* NOTE: IRQs are in mixed mode, not bypass to pure MUSB */
446 pr_debug("DA8xx OTG revision %08x, PHY %03x, control %02x\n",
447 rev, __raw_readl(CFGCHIP2),
448 musb_readb(reg_base, DA8XX_USB_CTRL_REG));
449
Felipe Balbi743411b2010-12-01 13:22:05 +0200450 musb->isr = da8xx_musb_interrupt;
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300451 return 0;
452fail:
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300453 return -ENODEV;
454}
455
Felipe Balbi743411b2010-12-01 13:22:05 +0200456static int da8xx_musb_exit(struct musb *musb)
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300457{
458 if (is_host_enabled(musb))
459 del_timer_sync(&otg_workaround);
460
461 phy_off();
462
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530463 usb_put_phy(musb->xceiv);
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300464 usb_nop_xceiv_unregister();
465
Sergei Shtylyov3ee076d2010-09-24 13:44:03 +0300466 return 0;
467}
Felipe Balbi743411b2010-12-01 13:22:05 +0200468
Felipe Balbif7ec9432010-12-02 09:48:58 +0200469static const struct musb_platform_ops da8xx_ops = {
Felipe Balbi743411b2010-12-01 13:22:05 +0200470 .init = da8xx_musb_init,
471 .exit = da8xx_musb_exit,
472
473 .enable = da8xx_musb_enable,
474 .disable = da8xx_musb_disable,
475
476 .set_mode = da8xx_musb_set_mode,
477 .try_idle = da8xx_musb_try_idle,
478
479 .set_vbus = da8xx_musb_set_vbus,
480};
Felipe Balbi8ceae512010-12-02 09:19:35 +0200481
482static u64 da8xx_dmamask = DMA_BIT_MASK(32);
483
Felipe Balbie9e8c852012-01-26 12:40:23 +0200484static int __devinit da8xx_probe(struct platform_device *pdev)
Felipe Balbi8ceae512010-12-02 09:19:35 +0200485{
486 struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
487 struct platform_device *musb;
Felipe Balbie6480fa2010-12-02 09:40:34 +0200488 struct da8xx_glue *glue;
Felipe Balbi8ceae512010-12-02 09:19:35 +0200489
Felipe Balbi03491762010-12-02 09:57:08 +0200490 struct clk *clk;
491
Felipe Balbi8ceae512010-12-02 09:19:35 +0200492 int ret = -ENOMEM;
493
Felipe Balbie6480fa2010-12-02 09:40:34 +0200494 glue = kzalloc(sizeof(*glue), GFP_KERNEL);
495 if (!glue) {
496 dev_err(&pdev->dev, "failed to allocate glue context\n");
497 goto err0;
498 }
499
Felipe Balbi8ceae512010-12-02 09:19:35 +0200500 musb = platform_device_alloc("musb-hdrc", -1);
501 if (!musb) {
502 dev_err(&pdev->dev, "failed to allocate musb device\n");
Felipe Balbie6480fa2010-12-02 09:40:34 +0200503 goto err1;
Felipe Balbi8ceae512010-12-02 09:19:35 +0200504 }
505
Felipe Balbi03491762010-12-02 09:57:08 +0200506 clk = clk_get(&pdev->dev, "usb20");
507 if (IS_ERR(clk)) {
508 dev_err(&pdev->dev, "failed to get clock\n");
509 ret = PTR_ERR(clk);
510 goto err2;
511 }
512
513 ret = clk_enable(clk);
514 if (ret) {
515 dev_err(&pdev->dev, "failed to enable clock\n");
516 goto err3;
517 }
518
Felipe Balbi8ceae512010-12-02 09:19:35 +0200519 musb->dev.parent = &pdev->dev;
520 musb->dev.dma_mask = &da8xx_dmamask;
521 musb->dev.coherent_dma_mask = da8xx_dmamask;
522
Felipe Balbie6480fa2010-12-02 09:40:34 +0200523 glue->dev = &pdev->dev;
524 glue->musb = musb;
Felipe Balbi03491762010-12-02 09:57:08 +0200525 glue->clk = clk;
Felipe Balbie6480fa2010-12-02 09:40:34 +0200526
Felipe Balbif7ec9432010-12-02 09:48:58 +0200527 pdata->platform_ops = &da8xx_ops;
528
Felipe Balbie6480fa2010-12-02 09:40:34 +0200529 platform_set_drvdata(pdev, glue);
Felipe Balbi8ceae512010-12-02 09:19:35 +0200530
531 ret = platform_device_add_resources(musb, pdev->resource,
532 pdev->num_resources);
533 if (ret) {
534 dev_err(&pdev->dev, "failed to add resources\n");
Felipe Balbi03491762010-12-02 09:57:08 +0200535 goto err4;
Felipe Balbi8ceae512010-12-02 09:19:35 +0200536 }
537
538 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
539 if (ret) {
540 dev_err(&pdev->dev, "failed to add platform_data\n");
Felipe Balbi03491762010-12-02 09:57:08 +0200541 goto err4;
Felipe Balbi8ceae512010-12-02 09:19:35 +0200542 }
543
544 ret = platform_device_add(musb);
545 if (ret) {
546 dev_err(&pdev->dev, "failed to register musb device\n");
Felipe Balbi03491762010-12-02 09:57:08 +0200547 goto err4;
Felipe Balbi8ceae512010-12-02 09:19:35 +0200548 }
549
550 return 0;
551
Felipe Balbi03491762010-12-02 09:57:08 +0200552err4:
553 clk_disable(clk);
554
555err3:
556 clk_put(clk);
557
Felipe Balbie6480fa2010-12-02 09:40:34 +0200558err2:
Felipe Balbi8ceae512010-12-02 09:19:35 +0200559 platform_device_put(musb);
560
Felipe Balbie6480fa2010-12-02 09:40:34 +0200561err1:
562 kfree(glue);
563
Felipe Balbi8ceae512010-12-02 09:19:35 +0200564err0:
565 return ret;
566}
567
Felipe Balbie9e8c852012-01-26 12:40:23 +0200568static int __devexit da8xx_remove(struct platform_device *pdev)
Felipe Balbi8ceae512010-12-02 09:19:35 +0200569{
Felipe Balbie6480fa2010-12-02 09:40:34 +0200570 struct da8xx_glue *glue = platform_get_drvdata(pdev);
Felipe Balbi8ceae512010-12-02 09:19:35 +0200571
Felipe Balbie6480fa2010-12-02 09:40:34 +0200572 platform_device_del(glue->musb);
573 platform_device_put(glue->musb);
Felipe Balbi03491762010-12-02 09:57:08 +0200574 clk_disable(glue->clk);
575 clk_put(glue->clk);
Felipe Balbie6480fa2010-12-02 09:40:34 +0200576 kfree(glue);
Felipe Balbi8ceae512010-12-02 09:19:35 +0200577
578 return 0;
579}
580
581static struct platform_driver da8xx_driver = {
Felipe Balbie9e8c852012-01-26 12:40:23 +0200582 .probe = da8xx_probe,
583 .remove = __devexit_p(da8xx_remove),
Felipe Balbi8ceae512010-12-02 09:19:35 +0200584 .driver = {
585 .name = "musb-da8xx",
586 },
587};
588
589MODULE_DESCRIPTION("DA8xx/OMAP-L1x MUSB Glue Layer");
590MODULE_AUTHOR("Sergei Shtylyov <sshtylyov@ru.mvista.com>");
591MODULE_LICENSE("GPL v2");
592
593static int __init da8xx_init(void)
594{
Felipe Balbie9e8c852012-01-26 12:40:23 +0200595 return platform_driver_register(&da8xx_driver);
Felipe Balbi8ceae512010-12-02 09:19:35 +0200596}
Felipe Balbie9e8c852012-01-26 12:40:23 +0200597module_init(da8xx_init);
Felipe Balbi8ceae512010-12-02 09:19:35 +0200598
599static void __exit da8xx_exit(void)
600{
601 platform_driver_unregister(&da8xx_driver);
602}
603module_exit(da8xx_exit);