]> git.sur5r.net Git - u-boot/blob - arch/arm/cpu/armv7/sunxi/usb_phy.c
sunxi: usb: Rename the usbc.? files to usb_phy.?
[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 #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 id;
48 } sunxi_usb_phy[] = {
49         {
50                 .usb_rst_mask = CCM_USB_CTRL_PHY0_RST | CCM_USB_CTRL_PHY0_CLK,
51                 .id = 0,
52         },
53         {
54                 .usb_rst_mask = CCM_USB_CTRL_PHY1_RST | CCM_USB_CTRL_PHY1_CLK,
55                 .id = 1,
56         },
57 #if (CONFIG_USB_MAX_CONTROLLER_COUNT > 1)
58         {
59                 .usb_rst_mask = CCM_USB_CTRL_PHY2_RST | CCM_USB_CTRL_PHY2_CLK,
60                 .id = 2,
61         }
62 #endif
63 };
64
65 static int sunxi_usb_phy_enabled_count;
66
67 static int get_vbus_gpio(int index)
68 {
69         switch (index) {
70         case 0: return sunxi_name_to_gpio(CONFIG_USB0_VBUS_PIN);
71         case 1: return sunxi_name_to_gpio(CONFIG_USB1_VBUS_PIN);
72         case 2: return sunxi_name_to_gpio(CONFIG_USB2_VBUS_PIN);
73         }
74         return -EINVAL;
75 }
76
77 static int get_vbus_detect_gpio(int index)
78 {
79         switch (index) {
80         case 0: return sunxi_name_to_gpio(CONFIG_USB0_VBUS_DET);
81         }
82         return -EINVAL;
83 }
84
85 static void usb_phy_write(struct sunxi_usb_phy *phy, int addr,
86                           int data, int len)
87 {
88         int j = 0, usbc_bit = 0;
89         void *dest = (void *)SUNXI_USB0_BASE + SUNXI_USB_CSR;
90
91 #ifdef CONFIG_MACH_SUN8I_A33
92         /* CSR needs to be explicitly initialized to 0 on A33 */
93         writel(0, dest);
94 #endif
95
96         usbc_bit = 1 << (phy->id * 2);
97         for (j = 0; j < len; j++) {
98                 /* set the bit address to be written */
99                 clrbits_le32(dest, 0xff << 8);
100                 setbits_le32(dest, (addr + j) << 8);
101
102                 clrbits_le32(dest, usbc_bit);
103                 /* set data bit */
104                 if (data & 0x1)
105                         setbits_le32(dest, 1 << 7);
106                 else
107                         clrbits_le32(dest, 1 << 7);
108
109                 setbits_le32(dest, usbc_bit);
110
111                 clrbits_le32(dest, usbc_bit);
112
113                 data >>= 1;
114         }
115 }
116
117 static void sunxi_usb_phy_config(struct sunxi_usb_phy *phy)
118 {
119         /* The following comments are machine
120          * translated from Chinese, you have been warned!
121          */
122
123         /* Regulation 45 ohms */
124         if (phy->id == 0)
125                 usb_phy_write(phy, 0x0c, 0x01, 1);
126
127         /* adjust PHY's magnitude and rate */
128         usb_phy_write(phy, 0x20, 0x14, 5);
129
130         /* threshold adjustment disconnect */
131 #if defined CONFIG_MACH_SUN4I || defined CONFIG_MACH_SUN6I
132         usb_phy_write(phy, 0x2a, 3, 2);
133 #else
134         usb_phy_write(phy, 0x2a, 2, 2);
135 #endif
136
137         return;
138 }
139
140 static void sunxi_usb_phy_passby(int index, int enable)
141 {
142         unsigned long bits = 0;
143         void *addr;
144
145         if (index == 1)
146                 addr = (void *)SUNXI_USB1_BASE + SUNXI_USB_PMU_IRQ_ENABLE;
147         else
148                 addr = (void *)SUNXI_USB2_BASE + SUNXI_USB_PMU_IRQ_ENABLE;
149
150         bits = SUNXI_EHCI_AHB_ICHR8_EN |
151                 SUNXI_EHCI_AHB_INCR4_BURST_EN |
152                 SUNXI_EHCI_AHB_INCRX_ALIGN_EN |
153                 SUNXI_EHCI_ULPI_BYPASS_EN;
154
155         if (enable)
156                 setbits_le32(addr, bits);
157         else
158                 clrbits_le32(addr, bits);
159
160         return;
161 }
162
163 void sunxi_usb_phy_enable_squelch_detect(int index, int enable)
164 {
165         struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
166
167         usb_phy_write(phy, 0x3c, enable ? 0 : 2, 2);
168 }
169
170 int sunxi_usb_phy_probe(int index)
171 {
172         struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
173         int ret = 0;
174
175         phy->gpio_vbus = get_vbus_gpio(index);
176         if (phy->gpio_vbus >= 0) {
177                 ret |= gpio_request(phy->gpio_vbus, "usbc_vbus");
178                 ret |= gpio_direction_output(phy->gpio_vbus, 0);
179         }
180
181         phy->gpio_vbus_det = get_vbus_detect_gpio(index);
182         if (phy->gpio_vbus_det >= 0) {
183                 ret |= gpio_request(phy->gpio_vbus_det, "usbc_vbus_det");
184                 ret |= gpio_direction_input(phy->gpio_vbus_det);
185         }
186
187         return ret;
188 }
189
190 int sunxi_usb_phy_remove(int index)
191 {
192         struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
193         int ret = 0;
194
195         if (phy->gpio_vbus >= 0)
196                 ret |= gpio_free(phy->gpio_vbus);
197
198         if (phy->gpio_vbus_det >= 0)
199                 ret |= gpio_free(phy->gpio_vbus_det);
200
201         return ret;
202 }
203
204 void sunxi_usb_phy_init(int index)
205 {
206         struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
207         struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
208
209         /* enable common PHY only once */
210         if (sunxi_usb_phy_enabled_count == 0)
211                 setbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE);
212
213         setbits_le32(&ccm->usb_clk_cfg, phy->usb_rst_mask);
214
215         sunxi_usb_phy_config(phy);
216
217         if (phy->id != 0)
218                 sunxi_usb_phy_passby(index, SUNXI_USB_PASSBY_EN);
219
220         sunxi_usb_phy_enabled_count++;
221 }
222
223 void sunxi_usb_phy_exit(int index)
224 {
225         struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
226         struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
227
228         if (phy->id != 0)
229                 sunxi_usb_phy_passby(index, !SUNXI_USB_PASSBY_EN);
230
231         clrbits_le32(&ccm->usb_clk_cfg, phy->usb_rst_mask);
232
233         /* disable common PHY only once, for the last enabled phy */
234         if (sunxi_usb_phy_enabled_count == 1)
235                 clrbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE);
236
237         sunxi_usb_phy_enabled_count--;
238 }
239
240 void sunxi_usb_phy_power_on(int index)
241 {
242         struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
243
244         if (phy->gpio_vbus >= 0)
245                 gpio_set_value(phy->gpio_vbus, 1);
246 }
247
248 void sunxi_usb_phy_power_off(int index)
249 {
250         struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
251
252         if (phy->gpio_vbus >= 0)
253                 gpio_set_value(phy->gpio_vbus, 0);
254 }
255
256 int sunxi_usb_phy_vbus_detect(int index)
257 {
258         struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
259         int err, retries = 3;
260
261         if (phy->gpio_vbus_det < 0) {
262                 eprintf("Error: invalid vbus detection pin\n");
263                 return phy->gpio_vbus_det;
264         }
265
266         err = gpio_get_value(phy->gpio_vbus_det);
267         /*
268          * Vbus may have been provided by the board and just been turned of
269          * some milliseconds ago on reset, what we're measuring then is a
270          * residual charge on Vbus, sleep a bit and try again.
271          */
272         while (err > 0 && retries--) {
273                 mdelay(100);
274                 err = gpio_get_value(phy->gpio_vbus_det);
275         }
276
277         return err;
278 }