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