xref: /openbmc/u-boot/drivers/usb/musb-new/omap2430.c (revision 4d93617d)
1 /*
2  * Copyright (C) 2005-2007 by Texas Instruments
3  * Some code has been taken from tusb6010.c
4  * Copyrights for that are attributable to:
5  * Copyright (C) 2006 Nokia Corporation
6  * Tony Lindgren <tony@atomide.com>
7  *
8  * This file is part of the Inventra Controller Driver for Linux.
9  *
10  * The Inventra Controller Driver for Linux is free software; you
11  * can redistribute it and/or modify it under the terms of the GNU
12  * General Public License version 2 as published by the Free Software
13  * Foundation.
14  *
15  * The Inventra Controller Driver for Linux is distributed in
16  * the hope that it will be useful, but WITHOUT ANY WARRANTY;
17  * without even the implied warranty of MERCHANTABILITY or
18  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
19  * License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with The Inventra Controller Driver for Linux ; if not,
23  * write to the Free Software Foundation, Inc., 59 Temple Place,
24  * Suite 330, Boston, MA  02111-1307  USA
25  *
26  */
27 #ifndef __UBOOT__
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/sched.h>
31 #include <linux/init.h>
32 #include <linux/list.h>
33 #include <linux/io.h>
34 #include <linux/platform_device.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/err.h>
38 #include <linux/usb/musb-omap.h>
39 #else
40 #include <common.h>
41 #include <asm/omap_musb.h>
42 #include <twl4030.h>
43 #include "linux-compat.h"
44 #endif
45 
46 #include "musb_core.h"
47 #include "omap2430.h"
48 
49 #ifndef __UBOOT__
50 struct omap2430_glue {
51 	struct device		*dev;
52 	struct platform_device	*musb;
53 	enum omap_musb_vbus_id_status status;
54 	struct work_struct	omap_musb_mailbox_work;
55 };
56 #define glue_to_musb(g)		platform_get_drvdata(g->musb)
57 
58 struct omap2430_glue		*_glue;
59 
60 static struct timer_list musb_idle_timer;
61 
62 static void musb_do_idle(unsigned long _musb)
63 {
64 	struct musb	*musb = (void *)_musb;
65 	unsigned long	flags;
66 	u8	power;
67 	u8	devctl;
68 
69 	spin_lock_irqsave(&musb->lock, flags);
70 
71 	switch (musb->xceiv->state) {
72 	case OTG_STATE_A_WAIT_BCON:
73 
74 		devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
75 		if (devctl & MUSB_DEVCTL_BDEVICE) {
76 			musb->xceiv->state = OTG_STATE_B_IDLE;
77 			MUSB_DEV_MODE(musb);
78 		} else {
79 			musb->xceiv->state = OTG_STATE_A_IDLE;
80 			MUSB_HST_MODE(musb);
81 		}
82 		break;
83 	case OTG_STATE_A_SUSPEND:
84 		/* finish RESUME signaling? */
85 		if (musb->port1_status & MUSB_PORT_STAT_RESUME) {
86 			power = musb_readb(musb->mregs, MUSB_POWER);
87 			power &= ~MUSB_POWER_RESUME;
88 			dev_dbg(musb->controller, "root port resume stopped, power %02x\n", power);
89 			musb_writeb(musb->mregs, MUSB_POWER, power);
90 			musb->is_active = 1;
91 			musb->port1_status &= ~(USB_PORT_STAT_SUSPEND
92 						| MUSB_PORT_STAT_RESUME);
93 			musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16;
94 			usb_hcd_poll_rh_status(musb_to_hcd(musb));
95 			/* NOTE: it might really be A_WAIT_BCON ... */
96 			musb->xceiv->state = OTG_STATE_A_HOST;
97 		}
98 		break;
99 	case OTG_STATE_A_HOST:
100 		devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
101 		if (devctl &  MUSB_DEVCTL_BDEVICE)
102 			musb->xceiv->state = OTG_STATE_B_IDLE;
103 		else
104 			musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
105 	default:
106 		break;
107 	}
108 	spin_unlock_irqrestore(&musb->lock, flags);
109 }
110 
111 
112 static void omap2430_musb_try_idle(struct musb *musb, unsigned long timeout)
113 {
114 	unsigned long		default_timeout = jiffies + msecs_to_jiffies(3);
115 	static unsigned long	last_timer;
116 
117 	if (timeout == 0)
118 		timeout = default_timeout;
119 
120 	/* Never idle if active, or when VBUS timeout is not set as host */
121 	if (musb->is_active || ((musb->a_wait_bcon == 0)
122 			&& (musb->xceiv->state == OTG_STATE_A_WAIT_BCON))) {
123 		dev_dbg(musb->controller, "%s active, deleting timer\n",
124 			otg_state_string(musb->xceiv->state));
125 		del_timer(&musb_idle_timer);
126 		last_timer = jiffies;
127 		return;
128 	}
129 
130 	if (time_after(last_timer, timeout)) {
131 		if (!timer_pending(&musb_idle_timer))
132 			last_timer = timeout;
133 		else {
134 			dev_dbg(musb->controller, "Longer idle timer already pending, ignoring\n");
135 			return;
136 		}
137 	}
138 	last_timer = timeout;
139 
140 	dev_dbg(musb->controller, "%s inactive, for idle timer for %lu ms\n",
141 		otg_state_string(musb->xceiv->state),
142 		(unsigned long)jiffies_to_msecs(timeout - jiffies));
143 	mod_timer(&musb_idle_timer, timeout);
144 }
145 
146 static void omap2430_musb_set_vbus(struct musb *musb, int is_on)
147 {
148 	struct usb_otg	*otg = musb->xceiv->otg;
149 	u8		devctl;
150 	unsigned long timeout = jiffies + msecs_to_jiffies(1000);
151 	int ret = 1;
152 	/* HDRC controls CPEN, but beware current surges during device
153 	 * connect.  They can trigger transient overcurrent conditions
154 	 * that must be ignored.
155 	 */
156 
157 	devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
158 
159 	if (is_on) {
160 		if (musb->xceiv->state == OTG_STATE_A_IDLE) {
161 			/* start the session */
162 			devctl |= MUSB_DEVCTL_SESSION;
163 			musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
164 			/*
165 			 * Wait for the musb to set as A device to enable the
166 			 * VBUS
167 			 */
168 			while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) {
169 
170 				cpu_relax();
171 
172 				if (time_after(jiffies, timeout)) {
173 					dev_err(musb->controller,
174 					"configured as A device timeout");
175 					ret = -EINVAL;
176 					break;
177 				}
178 			}
179 
180 			if (ret && otg->set_vbus)
181 				otg_set_vbus(otg, 1);
182 		} else {
183 			musb->is_active = 1;
184 			otg->default_a = 1;
185 			musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
186 			devctl |= MUSB_DEVCTL_SESSION;
187 			MUSB_HST_MODE(musb);
188 		}
189 	} else {
190 		musb->is_active = 0;
191 
192 		/* NOTE:  we're skipping A_WAIT_VFALL -> A_IDLE and
193 		 * jumping right to B_IDLE...
194 		 */
195 
196 		otg->default_a = 0;
197 		musb->xceiv->state = OTG_STATE_B_IDLE;
198 		devctl &= ~MUSB_DEVCTL_SESSION;
199 
200 		MUSB_DEV_MODE(musb);
201 	}
202 	musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
203 
204 	dev_dbg(musb->controller, "VBUS %s, devctl %02x "
205 		/* otg %3x conf %08x prcm %08x */ "\n",
206 		otg_state_string(musb->xceiv->state),
207 		musb_readb(musb->mregs, MUSB_DEVCTL));
208 }
209 
210 static int omap2430_musb_set_mode(struct musb *musb, u8 musb_mode)
211 {
212 	u8	devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
213 
214 	devctl |= MUSB_DEVCTL_SESSION;
215 	musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
216 
217 	return 0;
218 }
219 #endif
220 
221 static inline void omap2430_low_level_exit(struct musb *musb)
222 {
223 	u32 l;
224 
225 	/* in any role */
226 	l = musb_readl(musb->mregs, OTG_FORCESTDBY);
227 	l |= ENABLEFORCE;	/* enable MSTANDBY */
228 	musb_writel(musb->mregs, OTG_FORCESTDBY, l);
229 }
230 
231 static inline void omap2430_low_level_init(struct musb *musb)
232 {
233 	u32 l;
234 
235 	l = musb_readl(musb->mregs, OTG_FORCESTDBY);
236 	l &= ~ENABLEFORCE;	/* disable MSTANDBY */
237 	musb_writel(musb->mregs, OTG_FORCESTDBY, l);
238 }
239 
240 #ifndef __UBOOT__
241 void omap_musb_mailbox(enum omap_musb_vbus_id_status status)
242 {
243 	struct omap2430_glue	*glue = _glue;
244 	struct musb		*musb = glue_to_musb(glue);
245 
246 	glue->status = status;
247 	if (!musb) {
248 		dev_err(glue->dev, "musb core is not yet ready\n");
249 		return;
250 	}
251 
252 	schedule_work(&glue->omap_musb_mailbox_work);
253 }
254 EXPORT_SYMBOL_GPL(omap_musb_mailbox);
255 
256 static void omap_musb_set_mailbox(struct omap2430_glue *glue)
257 {
258 	struct musb *musb = glue_to_musb(glue);
259 	struct device *dev = musb->controller;
260 	struct musb_hdrc_platform_data *pdata = dev->platform_data;
261 	struct omap_musb_board_data *data = pdata->board_data;
262 	struct usb_otg *otg = musb->xceiv->otg;
263 
264 	switch (glue->status) {
265 	case OMAP_MUSB_ID_GROUND:
266 		dev_dbg(dev, "ID GND\n");
267 
268 		otg->default_a = true;
269 		musb->xceiv->state = OTG_STATE_A_IDLE;
270 		musb->xceiv->last_event = USB_EVENT_ID;
271 		if (!is_otg_enabled(musb) || musb->gadget_driver) {
272 			pm_runtime_get_sync(dev);
273 			usb_phy_init(musb->xceiv);
274 			omap2430_musb_set_vbus(musb, 1);
275 		}
276 		break;
277 
278 	case OMAP_MUSB_VBUS_VALID:
279 		dev_dbg(dev, "VBUS Connect\n");
280 
281 		otg->default_a = false;
282 		musb->xceiv->state = OTG_STATE_B_IDLE;
283 		musb->xceiv->last_event = USB_EVENT_VBUS;
284 		if (musb->gadget_driver)
285 			pm_runtime_get_sync(dev);
286 		usb_phy_init(musb->xceiv);
287 		break;
288 
289 	case OMAP_MUSB_ID_FLOAT:
290 	case OMAP_MUSB_VBUS_OFF:
291 		dev_dbg(dev, "VBUS Disconnect\n");
292 
293 		musb->xceiv->last_event = USB_EVENT_NONE;
294 		if (is_otg_enabled(musb) || is_peripheral_enabled(musb))
295 			if (musb->gadget_driver) {
296 				pm_runtime_mark_last_busy(dev);
297 				pm_runtime_put_autosuspend(dev);
298 			}
299 
300 		if (data->interface_type == MUSB_INTERFACE_UTMI) {
301 			if (musb->xceiv->otg->set_vbus)
302 				otg_set_vbus(musb->xceiv->otg, 0);
303 		}
304 		usb_phy_shutdown(musb->xceiv);
305 		break;
306 	default:
307 		dev_dbg(dev, "ID float\n");
308 	}
309 }
310 
311 
312 static void omap_musb_mailbox_work(struct work_struct *mailbox_work)
313 {
314 	struct omap2430_glue *glue = container_of(mailbox_work,
315 				struct omap2430_glue, omap_musb_mailbox_work);
316 	omap_musb_set_mailbox(glue);
317 }
318 #endif
319 
320 static int omap2430_musb_init(struct musb *musb)
321 {
322 	u32 l;
323 	int status = 0;
324 	unsigned long int start;
325 #ifndef __UBOOT__
326 	struct device *dev = musb->controller;
327 	struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
328 	struct musb_hdrc_platform_data *plat = dev->platform_data;
329 	struct omap_musb_board_data *data = plat->board_data;
330 #else
331 	struct omap_musb_board_data *data =
332 		(struct omap_musb_board_data *)musb->controller;
333 #endif
334 
335 	/* Reset the controller */
336 	musb_writel(musb->mregs, OTG_SYSCONFIG, SOFTRST);
337 
338 	start = get_timer(0);
339 
340 	while (1) {
341 		l = musb_readl(musb->mregs, OTG_SYSCONFIG);
342 		if ((l & SOFTRST) == 0)
343 			break;
344 
345 		if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
346 			dev_err(musb->controller, "MUSB reset is taking too long\n");
347 			return -ENODEV;
348 		}
349 	}
350 
351 #ifndef __UBOOT__
352 	/* We require some kind of external transceiver, hooked
353 	 * up through ULPI.  TWL4030-family PMICs include one,
354 	 * which needs a driver, drivers aren't always needed.
355 	 */
356 	musb->xceiv = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
357 	if (IS_ERR_OR_NULL(musb->xceiv)) {
358 		pr_err("HS USB OTG: no transceiver configured\n");
359 		return -ENODEV;
360 	}
361 
362 	status = pm_runtime_get_sync(dev);
363 	if (status < 0) {
364 		dev_err(dev, "pm_runtime_get_sync FAILED %d\n", status);
365 		goto err1;
366 	}
367 #endif
368 
369 	l = musb_readl(musb->mregs, OTG_INTERFSEL);
370 
371 	if (data->interface_type == MUSB_INTERFACE_UTMI) {
372 		/* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
373 		l &= ~ULPI_12PIN;       /* Disable ULPI */
374 		l |= UTMI_8BIT;         /* Enable UTMI  */
375 	} else {
376 		l |= ULPI_12PIN;
377 	}
378 
379 	musb_writel(musb->mregs, OTG_INTERFSEL, l);
380 
381 	pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
382 			"sysstatus 0x%x, intrfsel 0x%x, simenable  0x%x\n",
383 			musb_readl(musb->mregs, OTG_REVISION),
384 			musb_readl(musb->mregs, OTG_SYSCONFIG),
385 			musb_readl(musb->mregs, OTG_SYSSTATUS),
386 			musb_readl(musb->mregs, OTG_INTERFSEL),
387 			musb_readl(musb->mregs, OTG_SIMENABLE));
388 
389 #ifndef __UBOOT__
390 	setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
391 
392 	if (glue->status != OMAP_MUSB_UNKNOWN)
393 		omap_musb_set_mailbox(glue);
394 
395 	pm_runtime_put_noidle(musb->controller);
396 #endif
397 	return 0;
398 
399 err1:
400 	return status;
401 }
402 
403 static void omap2430_musb_enable(struct musb *musb)
404 {
405 #ifndef __UBOOT__
406 	u8		devctl;
407 	unsigned long timeout = jiffies + msecs_to_jiffies(1000);
408 	struct device *dev = musb->controller;
409 	struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
410 	struct musb_hdrc_platform_data *pdata = dev->platform_data;
411 	struct omap_musb_board_data *data = pdata->board_data;
412 
413 	switch (glue->status) {
414 
415 	case OMAP_MUSB_ID_GROUND:
416 		usb_phy_init(musb->xceiv);
417 		if (data->interface_type != MUSB_INTERFACE_UTMI)
418 			break;
419 		devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
420 		/* start the session */
421 		devctl |= MUSB_DEVCTL_SESSION;
422 		musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
423 		while (musb_readb(musb->mregs, MUSB_DEVCTL) &
424 				MUSB_DEVCTL_BDEVICE) {
425 			cpu_relax();
426 
427 			if (time_after(jiffies, timeout)) {
428 				dev_err(dev, "configured as A device timeout");
429 				break;
430 			}
431 		}
432 		break;
433 
434 	case OMAP_MUSB_VBUS_VALID:
435 		usb_phy_init(musb->xceiv);
436 		break;
437 
438 	default:
439 		break;
440 	}
441 #else
442 #ifdef CONFIG_TWL4030_USB
443 	if (twl4030_usb_ulpi_init()) {
444 		serial_printf("ERROR: %s Could not initialize PHY\n",
445 				__PRETTY_FUNCTION__);
446 	}
447 #endif
448 #endif
449 }
450 
451 static void omap2430_musb_disable(struct musb *musb)
452 {
453 #ifndef __UBOOT__
454 	struct device *dev = musb->controller;
455 	struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
456 
457 	if (glue->status != OMAP_MUSB_UNKNOWN)
458 		usb_phy_shutdown(musb->xceiv);
459 #endif
460 }
461 
462 static int omap2430_musb_exit(struct musb *musb)
463 {
464 	del_timer_sync(&musb_idle_timer);
465 
466 	omap2430_low_level_exit(musb);
467 
468 	return 0;
469 }
470 
471 #ifndef __UBOOT__
472 static const struct musb_platform_ops omap2430_ops = {
473 #else
474 const struct musb_platform_ops omap2430_ops = {
475 #endif
476 	.init		= omap2430_musb_init,
477 	.exit		= omap2430_musb_exit,
478 
479 #ifndef __UBOOT__
480 	.set_mode	= omap2430_musb_set_mode,
481 	.try_idle	= omap2430_musb_try_idle,
482 
483 	.set_vbus	= omap2430_musb_set_vbus,
484 #endif
485 
486 	.enable		= omap2430_musb_enable,
487 	.disable	= omap2430_musb_disable,
488 };
489 
490 #ifndef __UBOOT__
491 static u64 omap2430_dmamask = DMA_BIT_MASK(32);
492 
493 static int __devinit omap2430_probe(struct platform_device *pdev)
494 {
495 	struct musb_hdrc_platform_data	*pdata = pdev->dev.platform_data;
496 	struct platform_device		*musb;
497 	struct omap2430_glue		*glue;
498 	int				ret = -ENOMEM;
499 
500 	glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
501 	if (!glue) {
502 		dev_err(&pdev->dev, "failed to allocate glue context\n");
503 		goto err0;
504 	}
505 
506 	musb = platform_device_alloc("musb-hdrc", -1);
507 	if (!musb) {
508 		dev_err(&pdev->dev, "failed to allocate musb device\n");
509 		goto err0;
510 	}
511 
512 	musb->dev.parent		= &pdev->dev;
513 	musb->dev.dma_mask		= &omap2430_dmamask;
514 	musb->dev.coherent_dma_mask	= omap2430_dmamask;
515 
516 	glue->dev			= &pdev->dev;
517 	glue->musb			= musb;
518 	glue->status			= OMAP_MUSB_UNKNOWN;
519 
520 	pdata->platform_ops		= &omap2430_ops;
521 
522 	platform_set_drvdata(pdev, glue);
523 
524 	/*
525 	 * REVISIT if we ever have two instances of the wrapper, we will be
526 	 * in big trouble
527 	 */
528 	_glue	= glue;
529 
530 	INIT_WORK(&glue->omap_musb_mailbox_work, omap_musb_mailbox_work);
531 
532 	ret = platform_device_add_resources(musb, pdev->resource,
533 			pdev->num_resources);
534 	if (ret) {
535 		dev_err(&pdev->dev, "failed to add resources\n");
536 		goto err1;
537 	}
538 
539 	ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
540 	if (ret) {
541 		dev_err(&pdev->dev, "failed to add platform_data\n");
542 		goto err1;
543 	}
544 
545 	pm_runtime_enable(&pdev->dev);
546 
547 	ret = platform_device_add(musb);
548 	if (ret) {
549 		dev_err(&pdev->dev, "failed to register musb device\n");
550 		goto err1;
551 	}
552 
553 	return 0;
554 
555 err1:
556 	platform_device_put(musb);
557 
558 err0:
559 	return ret;
560 }
561 
562 static int __devexit omap2430_remove(struct platform_device *pdev)
563 {
564 	struct omap2430_glue		*glue = platform_get_drvdata(pdev);
565 
566 	cancel_work_sync(&glue->omap_musb_mailbox_work);
567 	platform_device_del(glue->musb);
568 	platform_device_put(glue->musb);
569 
570 	return 0;
571 }
572 
573 #ifdef CONFIG_PM
574 
575 static int omap2430_runtime_suspend(struct device *dev)
576 {
577 	struct omap2430_glue		*glue = dev_get_drvdata(dev);
578 	struct musb			*musb = glue_to_musb(glue);
579 
580 	if (musb) {
581 		musb->context.otg_interfsel = musb_readl(musb->mregs,
582 				OTG_INTERFSEL);
583 
584 		omap2430_low_level_exit(musb);
585 		usb_phy_set_suspend(musb->xceiv, 1);
586 	}
587 
588 	return 0;
589 }
590 
591 static int omap2430_runtime_resume(struct device *dev)
592 {
593 	struct omap2430_glue		*glue = dev_get_drvdata(dev);
594 	struct musb			*musb = glue_to_musb(glue);
595 
596 	if (musb) {
597 		omap2430_low_level_init(musb);
598 		musb_writel(musb->mregs, OTG_INTERFSEL,
599 				musb->context.otg_interfsel);
600 
601 		usb_phy_set_suspend(musb->xceiv, 0);
602 	}
603 
604 	return 0;
605 }
606 
607 static struct dev_pm_ops omap2430_pm_ops = {
608 	.runtime_suspend = omap2430_runtime_suspend,
609 	.runtime_resume = omap2430_runtime_resume,
610 };
611 
612 #define DEV_PM_OPS	(&omap2430_pm_ops)
613 #else
614 #define DEV_PM_OPS	NULL
615 #endif
616 
617 static struct platform_driver omap2430_driver = {
618 	.probe		= omap2430_probe,
619 	.remove		= __devexit_p(omap2430_remove),
620 	.driver		= {
621 		.name	= "musb-omap2430",
622 		.pm	= DEV_PM_OPS,
623 	},
624 };
625 
626 MODULE_DESCRIPTION("OMAP2PLUS MUSB Glue Layer");
627 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
628 MODULE_LICENSE("GPL v2");
629 
630 static int __init omap2430_init(void)
631 {
632 	return platform_driver_register(&omap2430_driver);
633 }
634 subsys_initcall(omap2430_init);
635 
636 static void __exit omap2430_exit(void)
637 {
638 	platform_driver_unregister(&omap2430_driver);
639 }
640 module_exit(omap2430_exit);
641 #endif
642