]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/arm/cpu/armv7/sunxi/usbc.c
Merge branch 'karo-tx-uboot' into kc-merge
[karo-tx-uboot.git] / arch / arm / cpu / armv7 / sunxi / usbc.c
1 /*
2  * Sunxi usb-controller code shared between the ehci and musb controllers
3  *
4  * Copyright (C) 2014 Roman Byshko
5  *
6  * Roman Byshko <rbyshko@gmail.com>
7  *
8  * Based on code from
9  * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
10  *
11  * SPDX-License-Identifier:     GPL-2.0+
12  */
13
14 #include <asm/arch/clock.h>
15 #include <asm/arch/cpu.h>
16 #include <asm/arch/usbc.h>
17 #include <asm/gpio.h>
18 #include <asm/io.h>
19 #include <common.h>
20 #ifdef CONFIG_AXP152_POWER
21 #include <axp152.h>
22 #endif
23 #ifdef CONFIG_AXP209_POWER
24 #include <axp209.h>
25 #endif
26 #ifdef CONFIG_AXP221_POWER
27 #include <axp221.h>
28 #endif
29
30 #define SUNXI_USB_PMU_IRQ_ENABLE        0x800
31 #define SUNXI_USB_CSR                   0x404
32 #define SUNXI_USB_PASSBY_EN             1
33
34 #define SUNXI_EHCI_AHB_ICHR8_EN         (1 << 10)
35 #define SUNXI_EHCI_AHB_INCR4_BURST_EN   (1 << 9)
36 #define SUNXI_EHCI_AHB_INCRX_ALIGN_EN   (1 << 8)
37 #define SUNXI_EHCI_ULPI_BYPASS_EN       (1 << 0)
38
39 static struct sunxi_usbc_hcd {
40         struct usb_hcd *hcd;
41         int usb_rst_mask;
42         int ahb_clk_mask;
43         int gpio_vbus;
44         int irq;
45         int id;
46 } sunxi_usbc_hcd[] = {
47         {
48                 .usb_rst_mask = CCM_USB_CTRL_PHY0_RST | CCM_USB_CTRL_PHY0_CLK,
49                 .ahb_clk_mask = 1 << AHB_GATE_OFFSET_USB0,
50 #if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I
51                 .irq = 71,
52 #else
53                 .irq = 38,
54 #endif
55                 .id = 0,
56         },
57         {
58                 .usb_rst_mask = CCM_USB_CTRL_PHY1_RST | CCM_USB_CTRL_PHY1_CLK,
59                 .ahb_clk_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0,
60 #if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I
61                 .irq = 72,
62 #else
63                 .irq = 39,
64 #endif
65                 .id = 1,
66         },
67 #if (CONFIG_USB_MAX_CONTROLLER_COUNT > 1)
68         {
69                 .usb_rst_mask = CCM_USB_CTRL_PHY2_RST | CCM_USB_CTRL_PHY2_CLK,
70                 .ahb_clk_mask = 1 << AHB_GATE_OFFSET_USB_EHCI1,
71 #ifdef CONFIG_MACH_SUN6I
72                 .irq = 74,
73 #else
74                 .irq = 40,
75 #endif
76                 .id = 2,
77         }
78 #endif
79 };
80
81 static int enabled_hcd_count;
82
83 static bool use_axp_drivebus(int index)
84 {
85         return index == 0 &&
86                strcmp(CONFIG_USB0_VBUS_PIN, "axp_drivebus") == 0;
87 }
88
89 void *sunxi_usbc_get_io_base(int index)
90 {
91         switch (index) {
92         case 0:
93                 return (void *)SUNXI_USB0_BASE;
94         case 1:
95                 return (void *)SUNXI_USB1_BASE;
96         case 2:
97                 return (void *)SUNXI_USB2_BASE;
98         default:
99                 return NULL;
100         }
101 }
102
103 static int get_vbus_gpio(int index)
104 {
105         if (use_axp_drivebus(index))
106                 return -1;
107
108         switch (index) {
109         case 0: return sunxi_name_to_gpio(CONFIG_USB0_VBUS_PIN);
110         case 1: return sunxi_name_to_gpio(CONFIG_USB1_VBUS_PIN);
111         case 2: return sunxi_name_to_gpio(CONFIG_USB2_VBUS_PIN);
112         }
113         return -1;
114 }
115
116 static void usb_phy_write(struct sunxi_usbc_hcd *sunxi_usbc, int addr,
117                           int data, int len)
118 {
119         int j = 0, usbc_bit = 0;
120         void *dest = sunxi_usbc_get_io_base(0) + SUNXI_USB_CSR;
121
122         usbc_bit = 1 << (sunxi_usbc->id * 2);
123         for (j = 0; j < len; j++) {
124                 /* set the bit address to be written */
125                 clrbits_le32(dest, 0xff << 8);
126                 setbits_le32(dest, (addr + j) << 8);
127
128                 clrbits_le32(dest, usbc_bit);
129                 /* set data bit */
130                 if (data & 0x1)
131                         setbits_le32(dest, 1 << 7);
132                 else
133                         clrbits_le32(dest, 1 << 7);
134
135                 setbits_le32(dest, usbc_bit);
136
137                 clrbits_le32(dest, usbc_bit);
138
139                 data >>= 1;
140         }
141 }
142
143 static void sunxi_usb_phy_init(struct sunxi_usbc_hcd *sunxi_usbc)
144 {
145         /* The following comments are machine
146          * translated from Chinese, you have been warned!
147          */
148
149         /* Regulation 45 ohms */
150         if (sunxi_usbc->id == 0)
151                 usb_phy_write(sunxi_usbc, 0x0c, 0x01, 1);
152
153         /* adjust PHY's magnitude and rate */
154         usb_phy_write(sunxi_usbc, 0x20, 0x14, 5);
155
156         /* threshold adjustment disconnect */
157 #if defined CONFIG_MACH_SUN4I || defined CONFIG_MACH_SUN6I
158         usb_phy_write(sunxi_usbc, 0x2a, 3, 2);
159 #else
160         usb_phy_write(sunxi_usbc, 0x2a, 2, 2);
161 #endif
162
163         return;
164 }
165
166 static void sunxi_usb_passby(struct sunxi_usbc_hcd *sunxi_usbc, int enable)
167 {
168         unsigned long bits = 0;
169         void *addr = sunxi_usbc_get_io_base(sunxi_usbc->id) +
170                      SUNXI_USB_PMU_IRQ_ENABLE;
171
172         bits = SUNXI_EHCI_AHB_ICHR8_EN |
173                 SUNXI_EHCI_AHB_INCR4_BURST_EN |
174                 SUNXI_EHCI_AHB_INCRX_ALIGN_EN |
175                 SUNXI_EHCI_ULPI_BYPASS_EN;
176
177         if (enable)
178                 setbits_le32(addr, bits);
179         else
180                 clrbits_le32(addr, bits);
181
182         return;
183 }
184
185 int sunxi_usbc_request_resources(int index)
186 {
187         struct sunxi_usbc_hcd *sunxi_usbc = &sunxi_usbc_hcd[index];
188
189         sunxi_usbc->gpio_vbus = get_vbus_gpio(index);
190         if (sunxi_usbc->gpio_vbus != -1)
191                 return gpio_request(sunxi_usbc->gpio_vbus, "usbc_vbus");
192
193         return 0;
194 }
195
196 int sunxi_usbc_free_resources(int index)
197 {
198         struct sunxi_usbc_hcd *sunxi_usbc = &sunxi_usbc_hcd[index];
199
200         if (sunxi_usbc->gpio_vbus != -1)
201                 return gpio_free(sunxi_usbc->gpio_vbus);
202
203         return 0;
204 }
205
206 void sunxi_usbc_enable(int index)
207 {
208         struct sunxi_usbc_hcd *sunxi_usbc = &sunxi_usbc_hcd[index];
209         struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
210
211         /* enable common PHY only once */
212         if (enabled_hcd_count == 0)
213                 setbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE);
214
215         setbits_le32(&ccm->usb_clk_cfg, sunxi_usbc->usb_rst_mask);
216         setbits_le32(&ccm->ahb_gate0, sunxi_usbc->ahb_clk_mask);
217 #if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I
218         setbits_le32(&ccm->ahb_reset0_cfg, sunxi_usbc->ahb_clk_mask);
219 #endif
220
221         sunxi_usb_phy_init(sunxi_usbc);
222
223         if (sunxi_usbc->id != 0)
224                 sunxi_usb_passby(sunxi_usbc, SUNXI_USB_PASSBY_EN);
225
226         enabled_hcd_count++;
227 }
228
229 void sunxi_usbc_disable(int index)
230 {
231         struct sunxi_usbc_hcd *sunxi_usbc = &sunxi_usbc_hcd[index];
232         struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
233
234         if (sunxi_usbc->id != 0)
235                 sunxi_usb_passby(sunxi_usbc, !SUNXI_USB_PASSBY_EN);
236
237 #if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I
238         clrbits_le32(&ccm->ahb_reset0_cfg, sunxi_usbc->ahb_clk_mask);
239 #endif
240         clrbits_le32(&ccm->ahb_gate0, sunxi_usbc->ahb_clk_mask);
241         clrbits_le32(&ccm->usb_clk_cfg, sunxi_usbc->usb_rst_mask);
242
243         /* disable common PHY only once, for the last enabled hcd */
244         if (enabled_hcd_count == 1)
245                 clrbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE);
246
247         enabled_hcd_count--;
248 }
249
250 void sunxi_usbc_vbus_enable(int index)
251 {
252         struct sunxi_usbc_hcd *sunxi_usbc = &sunxi_usbc_hcd[index];
253
254 #ifdef AXP_DRIVEBUS
255         if (use_axp_drivebus(index))
256                 axp_drivebus_enable();
257 #endif
258         if (sunxi_usbc->gpio_vbus != -1)
259                 gpio_direction_output(sunxi_usbc->gpio_vbus, 1);
260 }
261
262 void sunxi_usbc_vbus_disable(int index)
263 {
264         struct sunxi_usbc_hcd *sunxi_usbc = &sunxi_usbc_hcd[index];
265
266 #ifdef AXP_DRIVEBUS
267         if (use_axp_drivebus(index))
268                 axp_drivebus_disable();
269 #endif
270         if (sunxi_usbc->gpio_vbus != -1)
271                 gpio_direction_output(sunxi_usbc->gpio_vbus, 0);
272 }