xref: /rk3399_rockchip-uboot/drivers/usb/musb/da8xx.c (revision 8ff43b03e9f38a6e136e2e20eec4e8b2eb4a1d3d)
1 /*
2  * da8xx.c - TI's DA8xx platform specific usb wrapper functions.
3  *
4  * Author: Ajay Kumar Gupta <ajay.gupta@ti.com>
5  *
6  * Based on drivers/usb/musb/davinci.c
7  *
8  * Copyright (C) 2009 Texas Instruments Incorporated
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24 #include <common.h>
25 
26 #include "musb_core.h"
27 #include <asm/arch/da8xx-usb.h>
28 
29 /* MUSB platform configuration */
30 struct musb_config musb_cfg = {
31 	.regs		= (struct musb_regs *)DA8XX_USB_OTG_CORE_BASE,
32 	.timeout	= DA8XX_USB_OTG_TIMEOUT,
33 	.musb_speed	= 0,
34 };
35 
36 /*
37  * This function enables VBUS by driving the GPIO Bank4 Pin 15 high.
38  */
39 static void enable_vbus(void)
40 {
41 	u32 value;
42 
43 	/* configure GPIO bank4 pin 15 in output direction */
44 	value = readl(&davinci_gpio_bank45->dir);
45 	writel((value & (~DA8XX_USB_VBUS_GPIO)), &davinci_gpio_bank45->dir);
46 
47 	/* set GPIO bank4 pin 15 high to drive VBUS */
48 	value = readl(&davinci_gpio_bank45->set_data);
49 	writel((value | DA8XX_USB_VBUS_GPIO), &davinci_gpio_bank45->set_data);
50 }
51 
52 /*
53  * Enable the usb0 phy. This initialization procedure is explained in
54  * the DA8xx USB user guide document.
55  */
56 static u8 phy_on(void)
57 {
58 	u32 timeout;
59 	u32 cfgchip2;
60 
61 	cfgchip2 = readl(&davinci_syscfg_regs->cfgchip2);
62 
63 	cfgchip2 &= ~(CFGCHIP2_RESET | CFGCHIP2_PHYPWRDN | CFGCHIP2_OTGPWRDN |
64 		      CFGCHIP2_OTGMODE | CFGCHIP2_REFFREQ);
65 	cfgchip2 |= CFGCHIP2_SESENDEN | CFGCHIP2_VBDTCTEN | CFGCHIP2_PHY_PLLON |
66 		    CFGCHIP2_REFFREQ_24MHZ;
67 
68 	writel(cfgchip2, &davinci_syscfg_regs->cfgchip2);
69 
70 	/* wait until the usb phy pll locks */
71 	timeout = musb_cfg.timeout;
72 	while (timeout--)
73 		if (readl(&davinci_syscfg_regs->cfgchip2) & CFGCHIP2_PHYCLKGD)
74 			return 1;
75 
76 	/* USB phy was not turned on */
77 	return 0;
78 }
79 
80 /*
81  * Disable the usb phy
82  */
83 static void phy_off(void)
84 {
85 	u32 cfgchip2;
86 
87 	/*
88 	 * Power down the on-chip PHY.
89 	 */
90 	cfgchip2 = readl(&davinci_syscfg_regs->cfgchip2);
91 	cfgchip2 &= ~CFGCHIP2_PHY_PLLON;
92 	cfgchip2 |= CFGCHIP2_PHYPWRDN | CFGCHIP2_OTGPWRDN;
93 	writel(cfgchip2, &davinci_syscfg_regs->cfgchip2);
94 }
95 
96 /*
97  * This function performs DA8xx platform specific initialization for usb0.
98  */
99 int musb_platform_init(void)
100 {
101 	u32  revision;
102 
103 	/* enable psc for usb2.0 */
104 	lpsc_on(33);
105 
106 	/* enable usb vbus */
107 	enable_vbus();
108 
109 	/* reset the controller */
110 	writel(0x1, &da8xx_usb_regs->control);
111 	udelay(5000);
112 
113 	/* start the on-chip usb phy and its pll */
114 	if (phy_on() == 0)
115 		return -1;
116 
117 	/* Returns zero if e.g. not clocked */
118 	revision = readl(&da8xx_usb_regs->revision);
119 	if (revision == 0)
120 		return -1;
121 
122 	/* Disable all interrupts */
123 	writel((DA8XX_USB_USBINT_MASK | DA8XX_USB_TXINT_MASK |
124 		DA8XX_USB_RXINT_MASK), &da8xx_usb_regs->intmsk_set);
125 	return 0;
126 }
127 
128 /*
129  * This function performs DA8xx platform specific deinitialization for usb0.
130  */
131 void musb_platform_deinit(void)
132 {
133 	/* Turn of the phy */
134 	phy_off();
135 
136 	/* flush any interrupts */
137 	writel((DA8XX_USB_USBINT_MASK | DA8XX_USB_TXINT_MASK |
138 		DA8XX_USB_RXINT_MASK), &da8xx_usb_regs->intmsk_clr);
139 	writel(0, &da8xx_usb_regs->eoi);
140 }
141