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