4 * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
5 * Copyright (C) 2014 Roman Byshko <rbyshko@gmail.com>
8 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
10 * SPDX-License-Identifier: GPL-2.0+
14 #include <asm/arch/clock.h>
15 #include <asm/arch/cpu.h>
16 #include <asm/arch/usb_phy.h>
20 #ifdef CONFIG_AXP152_POWER
23 #ifdef CONFIG_AXP209_POWER
26 #ifdef CONFIG_AXP221_POWER
30 #define SUNXI_USB_PMU_IRQ_ENABLE 0x800
31 #ifdef CONFIG_MACH_SUN8I_A33
32 #define SUNXI_USB_CSR 0x410
34 #define SUNXI_USB_CSR 0x404
36 #define SUNXI_USB_PASSBY_EN 1
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)
43 static struct sunxi_usb_phy {
53 .usb_rst_mask = CCM_USB_CTRL_PHY0_RST | CCM_USB_CTRL_PHY0_CLK,
57 .usb_rst_mask = CCM_USB_CTRL_PHY1_RST | CCM_USB_CTRL_PHY1_CLK,
60 #if CONFIG_SUNXI_USB_PHYS >= 3
62 .usb_rst_mask = CCM_USB_CTRL_PHY2_RST | CCM_USB_CTRL_PHY2_CLK,
68 static int get_vbus_gpio(int 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);
78 static int get_vbus_detect_gpio(int index)
81 case 0: return sunxi_name_to_gpio(CONFIG_USB0_VBUS_DET);
86 static int get_id_detect_gpio(int index)
89 case 0: return sunxi_name_to_gpio(CONFIG_USB0_ID_DET);
94 static void usb_phy_write(struct sunxi_usb_phy *phy, int addr,
97 int j = 0, usbc_bit = 0;
98 void *dest = (void *)SUNXI_USB0_BASE + SUNXI_USB_CSR;
100 #ifdef CONFIG_MACH_SUN8I_A33
101 /* CSR needs to be explicitly initialized to 0 on A33 */
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);
111 clrbits_le32(dest, usbc_bit);
114 setbits_le32(dest, 1 << 7);
116 clrbits_le32(dest, 1 << 7);
118 setbits_le32(dest, usbc_bit);
120 clrbits_le32(dest, usbc_bit);
126 static void sunxi_usb_phy_config(struct sunxi_usb_phy *phy)
128 /* The following comments are machine
129 * translated from Chinese, you have been warned!
132 /* Regulation 45 ohms */
134 usb_phy_write(phy, 0x0c, 0x01, 1);
136 /* adjust PHY's magnitude and rate */
137 usb_phy_write(phy, 0x20, 0x14, 5);
139 /* threshold adjustment disconnect */
140 #if defined CONFIG_MACH_SUN5I || defined CONFIG_MACH_SUN7I
141 usb_phy_write(phy, 0x2a, 2, 2);
143 usb_phy_write(phy, 0x2a, 3, 2);
149 static void sunxi_usb_phy_passby(int index, int enable)
151 unsigned long bits = 0;
155 addr = (void *)SUNXI_USB1_BASE + SUNXI_USB_PMU_IRQ_ENABLE;
157 addr = (void *)SUNXI_USB2_BASE + SUNXI_USB_PMU_IRQ_ENABLE;
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;
165 setbits_le32(addr, bits);
167 clrbits_le32(addr, bits);
172 void sunxi_usb_phy_enable_squelch_detect(int index, int enable)
174 struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
176 usb_phy_write(phy, 0x3c, enable ? 0 : 2, 2);
179 void sunxi_usb_phy_init(int index)
181 struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
182 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
185 if (phy->init_count != 1)
188 setbits_le32(&ccm->usb_clk_cfg, phy->usb_rst_mask);
190 sunxi_usb_phy_config(phy);
193 sunxi_usb_phy_passby(index, SUNXI_USB_PASSBY_EN);
196 void sunxi_usb_phy_exit(int index)
198 struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
199 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
202 if (phy->init_count != 0)
206 sunxi_usb_phy_passby(index, !SUNXI_USB_PASSBY_EN);
208 clrbits_le32(&ccm->usb_clk_cfg, phy->usb_rst_mask);
211 void sunxi_usb_phy_power_on(int index)
213 struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
215 phy->power_on_count++;
216 if (phy->power_on_count != 1)
219 if (phy->gpio_vbus >= 0)
220 gpio_set_value(phy->gpio_vbus, 1);
223 void sunxi_usb_phy_power_off(int index)
225 struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
227 phy->power_on_count--;
228 if (phy->power_on_count != 0)
231 if (phy->gpio_vbus >= 0)
232 gpio_set_value(phy->gpio_vbus, 0);
235 int sunxi_usb_phy_power_is_on(int index)
237 struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
239 return phy->power_on_count > 0;
242 int sunxi_usb_phy_vbus_detect(int index)
244 struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
245 int err, retries = 3;
247 if (phy->gpio_vbus_det < 0)
248 return phy->gpio_vbus_det;
250 err = gpio_get_value(phy->gpio_vbus_det);
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.
256 while (err > 0 && retries--) {
258 err = gpio_get_value(phy->gpio_vbus_det);
264 int sunxi_usb_phy_id_detect(int index)
266 struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
268 if (phy->gpio_id_det < 0)
269 return phy->gpio_id_det;
271 return gpio_get_value(phy->gpio_id_det);
274 int sunxi_usb_phy_probe(void)
276 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
277 struct sunxi_usb_phy *phy;
280 for (i = 0; i < CONFIG_SUNXI_USB_PHYS; i++) {
281 phy = &sunxi_usb_phy[i];
283 phy->gpio_vbus = get_vbus_gpio(i);
284 if (phy->gpio_vbus >= 0) {
285 ret = gpio_request(phy->gpio_vbus, "usb_vbus");
288 ret = gpio_direction_output(phy->gpio_vbus, 0);
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");
298 ret = gpio_direction_input(phy->gpio_vbus_det);
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");
308 ret = gpio_direction_input(phy->gpio_id_det);
311 sunxi_gpio_set_pull(phy->gpio_id_det,
316 setbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE);
321 int sunxi_usb_phy_remove(void)
323 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
324 struct sunxi_usb_phy *phy;
327 clrbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE);
329 for (i = 0; i < CONFIG_SUNXI_USB_PHYS; i++) {
330 phy = &sunxi_usb_phy[i];
332 if (phy->gpio_vbus >= 0)
333 gpio_free(phy->gpio_vbus);
335 if (phy->gpio_vbus_det >= 0)
336 gpio_free(phy->gpio_vbus_det);
338 if (phy->gpio_id_det >= 0)
339 gpio_free(phy->gpio_id_det);