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