]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/arm/cpu/armv7/sunxi/usb_phy.c
sunxi: usb-phy: Never power off the usb ports
[karo-tx-uboot.git] / arch / arm / cpu / armv7 / sunxi / usb_phy.c
1 /*
2  * Sunxi usb-phy code
3  *
4  * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
5  * Copyright (C) 2014 Roman Byshko <rbyshko@gmail.com>
6  *
7  * Based on code from
8  * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
9  *
10  * SPDX-License-Identifier:     GPL-2.0+
11  */
12
13 #include <common.h>
14 #include <asm/arch/clock.h>
15 #include <asm/arch/cpu.h>
16 #include <asm/arch/usb_phy.h>
17 #include <asm/gpio.h>
18 #include <asm/io.h>
19 #include <errno.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 #ifdef CONFIG_MACH_SUN8I_A33
32 #define SUNXI_USB_CSR                   0x410
33 #else
34 #define SUNXI_USB_CSR                   0x404
35 #endif
36 #define SUNXI_USB_PASSBY_EN             1
37
38 #define SUNXI_EHCI_AHB_ICHR8_EN         (1 << 10)
39 #define SUNXI_EHCI_AHB_INCR4_BURST_EN   (1 << 9)
40 #define SUNXI_EHCI_AHB_INCRX_ALIGN_EN   (1 << 8)
41 #define SUNXI_EHCI_ULPI_BYPASS_EN       (1 << 0)
42
43 static struct sunxi_usb_phy {
44         int usb_rst_mask;
45         int gpio_vbus;
46         int gpio_vbus_det;
47         int gpio_id_det;
48         int id;
49         int init_count;
50         int power_on_count;
51 } sunxi_usb_phy[] = {
52         {
53                 .usb_rst_mask = CCM_USB_CTRL_PHY0_RST | CCM_USB_CTRL_PHY0_CLK,
54                 .id = 0,
55         },
56         {
57                 .usb_rst_mask = CCM_USB_CTRL_PHY1_RST | CCM_USB_CTRL_PHY1_CLK,
58                 .id = 1,
59         },
60 #if CONFIG_SUNXI_USB_PHYS >= 3
61         {
62                 .usb_rst_mask = CCM_USB_CTRL_PHY2_RST | CCM_USB_CTRL_PHY2_CLK,
63                 .id = 2,
64         }
65 #endif
66 };
67
68 static int get_vbus_gpio(int index)
69 {
70         switch (index) {
71         case 0: return sunxi_name_to_gpio(CONFIG_USB0_VBUS_PIN);
72         case 1: return sunxi_name_to_gpio(CONFIG_USB1_VBUS_PIN);
73         case 2: return sunxi_name_to_gpio(CONFIG_USB2_VBUS_PIN);
74         }
75         return -EINVAL;
76 }
77
78 static int get_vbus_detect_gpio(int index)
79 {
80         switch (index) {
81         case 0: return sunxi_name_to_gpio(CONFIG_USB0_VBUS_DET);
82         }
83         return -EINVAL;
84 }
85
86 static int get_id_detect_gpio(int index)
87 {
88         switch (index) {
89         case 0: return sunxi_name_to_gpio(CONFIG_USB0_ID_DET);
90         }
91         return -EINVAL;
92 }
93
94 static void usb_phy_write(struct sunxi_usb_phy *phy, int addr,
95                           int data, int len)
96 {
97         int j = 0, usbc_bit = 0;
98         void *dest = (void *)SUNXI_USB0_BASE + SUNXI_USB_CSR;
99
100 #ifdef CONFIG_MACH_SUN8I_A33
101         /* CSR needs to be explicitly initialized to 0 on A33 */
102         writel(0, dest);
103 #endif
104
105         usbc_bit = 1 << (phy->id * 2);
106         for (j = 0; j < len; j++) {
107                 /* set the bit address to be written */
108                 clrbits_le32(dest, 0xff << 8);
109                 setbits_le32(dest, (addr + j) << 8);
110
111                 clrbits_le32(dest, usbc_bit);
112                 /* set data bit */
113                 if (data & 0x1)
114                         setbits_le32(dest, 1 << 7);
115                 else
116                         clrbits_le32(dest, 1 << 7);
117
118                 setbits_le32(dest, usbc_bit);
119
120                 clrbits_le32(dest, usbc_bit);
121
122                 data >>= 1;
123         }
124 }
125
126 static void sunxi_usb_phy_config(struct sunxi_usb_phy *phy)
127 {
128         /* The following comments are machine
129          * translated from Chinese, you have been warned!
130          */
131
132         /* Regulation 45 ohms */
133         if (phy->id == 0)
134                 usb_phy_write(phy, 0x0c, 0x01, 1);
135
136         /* adjust PHY's magnitude and rate */
137         usb_phy_write(phy, 0x20, 0x14, 5);
138
139         /* threshold adjustment disconnect */
140 #if defined CONFIG_MACH_SUN5I || defined CONFIG_MACH_SUN7I
141         usb_phy_write(phy, 0x2a, 2, 2);
142 #else
143         usb_phy_write(phy, 0x2a, 3, 2);
144 #endif
145
146         return;
147 }
148
149 static void sunxi_usb_phy_passby(int index, int enable)
150 {
151         unsigned long bits = 0;
152         void *addr;
153
154         if (index == 1)
155                 addr = (void *)SUNXI_USB1_BASE + SUNXI_USB_PMU_IRQ_ENABLE;
156         else
157                 addr = (void *)SUNXI_USB2_BASE + SUNXI_USB_PMU_IRQ_ENABLE;
158
159         bits = SUNXI_EHCI_AHB_ICHR8_EN |
160                 SUNXI_EHCI_AHB_INCR4_BURST_EN |
161                 SUNXI_EHCI_AHB_INCRX_ALIGN_EN |
162                 SUNXI_EHCI_ULPI_BYPASS_EN;
163
164         if (enable)
165                 setbits_le32(addr, bits);
166         else
167                 clrbits_le32(addr, bits);
168
169         return;
170 }
171
172 void sunxi_usb_phy_enable_squelch_detect(int index, int enable)
173 {
174         struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
175
176         usb_phy_write(phy, 0x3c, enable ? 0 : 2, 2);
177 }
178
179 void sunxi_usb_phy_init(int index)
180 {
181         struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
182         struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
183
184         phy->init_count++;
185         if (phy->init_count != 1)
186                 return;
187
188         setbits_le32(&ccm->usb_clk_cfg, phy->usb_rst_mask);
189
190         sunxi_usb_phy_config(phy);
191
192         if (phy->id != 0)
193                 sunxi_usb_phy_passby(index, SUNXI_USB_PASSBY_EN);
194 }
195
196 void sunxi_usb_phy_exit(int index)
197 {
198         struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
199         struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
200
201         phy->init_count--;
202         if (phy->init_count != 0)
203                 return;
204
205         if (phy->id != 0)
206                 sunxi_usb_phy_passby(index, !SUNXI_USB_PASSBY_EN);
207
208         clrbits_le32(&ccm->usb_clk_cfg, phy->usb_rst_mask);
209 }
210
211 void sunxi_usb_phy_power_on(int index)
212 {
213         struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
214
215         phy->power_on_count++;
216         if (phy->power_on_count != 1)
217                 return;
218
219         if (phy->gpio_vbus >= 0)
220                 gpio_set_value(phy->gpio_vbus, 1);
221 }
222
223 void sunxi_usb_phy_power_off(int index)
224 {
225         struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
226
227         phy->power_on_count--;
228         if (phy->power_on_count != 0)
229                 return;
230
231         if (phy->gpio_vbus >= 0)
232                 gpio_set_value(phy->gpio_vbus, 0);
233 }
234
235 int sunxi_usb_phy_power_is_on(int index)
236 {
237         struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
238
239         return phy->power_on_count > 0;
240 }
241
242 int sunxi_usb_phy_vbus_detect(int index)
243 {
244         struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
245         int err, retries = 3;
246
247         if (phy->gpio_vbus_det < 0)
248                 return phy->gpio_vbus_det;
249
250         err = gpio_get_value(phy->gpio_vbus_det);
251         /*
252          * Vbus may have been provided by the board and just been turned of
253          * some milliseconds ago on reset, what we're measuring then is a
254          * residual charge on Vbus, sleep a bit and try again.
255          */
256         while (err > 0 && retries--) {
257                 mdelay(100);
258                 err = gpio_get_value(phy->gpio_vbus_det);
259         }
260
261         return err;
262 }
263
264 int sunxi_usb_phy_id_detect(int index)
265 {
266         struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
267
268         if (phy->gpio_id_det < 0)
269                 return phy->gpio_id_det;
270
271         return gpio_get_value(phy->gpio_id_det);
272 }
273
274 int sunxi_usb_phy_probe(void)
275 {
276         struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
277         struct sunxi_usb_phy *phy;
278         int i, ret = 0;
279
280         for (i = 0; i < CONFIG_SUNXI_USB_PHYS; i++) {
281                 phy = &sunxi_usb_phy[i];
282
283                 phy->gpio_vbus = get_vbus_gpio(i);
284                 if (phy->gpio_vbus >= 0) {
285                         ret = gpio_request(phy->gpio_vbus, "usb_vbus");
286                         if (ret)
287                                 return ret;
288                         ret = gpio_direction_output(phy->gpio_vbus, 0);
289                         if (ret)
290                                 return ret;
291                 }
292
293                 phy->gpio_vbus_det = get_vbus_detect_gpio(i);
294                 if (phy->gpio_vbus_det >= 0) {
295                         ret = gpio_request(phy->gpio_vbus_det, "usb_vbus_det");
296                         if (ret)
297                                 return ret;
298                         ret = gpio_direction_input(phy->gpio_vbus_det);
299                         if (ret)
300                                 return ret;
301                 }
302
303                 phy->gpio_id_det = get_id_detect_gpio(i);
304                 if (phy->gpio_id_det >= 0) {
305                         ret = gpio_request(phy->gpio_id_det, "usb_id_det");
306                         if (ret)
307                                 return ret;
308                         ret = gpio_direction_input(phy->gpio_id_det);
309                         if (ret)
310                                 return ret;
311                         sunxi_gpio_set_pull(phy->gpio_id_det,
312                                             SUNXI_GPIO_PULL_UP);
313                 }
314         }
315
316         setbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE);
317
318         return 0;
319 }
320
321 int sunxi_usb_phy_remove(void)
322 {
323         struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
324         struct sunxi_usb_phy *phy;
325         int i;
326
327         clrbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE);
328
329         for (i = 0; i < CONFIG_SUNXI_USB_PHYS; i++) {
330                 phy = &sunxi_usb_phy[i];
331
332                 if (phy->gpio_vbus >= 0)
333                         gpio_free(phy->gpio_vbus);
334
335                 if (phy->gpio_vbus_det >= 0)
336                         gpio_free(phy->gpio_vbus_det);
337
338                 if (phy->gpio_id_det >= 0)
339                         gpio_free(phy->gpio_id_det);
340         }
341
342         return 0;
343 }