Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 1 | /* |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 2 | * Sunxi usb-phy code |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 3 | * |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 4 | * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com> |
| 5 | * Copyright (C) 2014 Roman Byshko <rbyshko@gmail.com> |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 6 | * |
| 7 | * Based on code from |
| 8 | * Allwinner Technology Co., Ltd. <www.allwinnertech.com> |
| 9 | * |
| 10 | * SPDX-License-Identifier: GPL-2.0+ |
| 11 | */ |
| 12 | |
Hans de Goede | 7e5aabd | 2015-04-27 11:44:22 +0200 | [diff] [blame] | 13 | #include <common.h> |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 14 | #include <asm/arch/clock.h> |
| 15 | #include <asm/arch/cpu.h> |
Hans de Goede | 26a9005 | 2015-04-27 15:05:10 +0200 | [diff] [blame] | 16 | #include <asm/arch/usb_phy.h> |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 17 | #include <asm/gpio.h> |
| 18 | #include <asm/io.h> |
Hans de Goede | 494f5d0 | 2015-04-22 17:39:59 +0200 | [diff] [blame] | 19 | #include <errno.h> |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 20 | |
Maxime Ripard | 53bba76 | 2017-08-23 13:31:08 +0200 | [diff] [blame] | 21 | #if defined(CONFIG_MACH_SUN4I) || \ |
| 22 | defined(CONFIG_MACH_SUN5I) || \ |
| 23 | defined(CONFIG_MACH_SUN6I) || \ |
| 24 | defined(CONFIG_MACH_SUN7I) || \ |
| 25 | defined(CONFIG_MACH_SUN8I_A23) || \ |
| 26 | defined(CONFIG_MACH_SUN9I) |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 27 | #define SUNXI_USB_CSR 0x404 |
Maxime Ripard | 53bba76 | 2017-08-23 13:31:08 +0200 | [diff] [blame] | 28 | #else |
| 29 | #define SUNXI_USB_CSR 0x410 |
Vishnu Patekar | 3702f14 | 2015-03-01 23:47:48 +0530 | [diff] [blame] | 30 | #endif |
Maxime Ripard | 53bba76 | 2017-08-23 13:31:08 +0200 | [diff] [blame] | 31 | |
| 32 | #define SUNXI_USB_PMU_IRQ_ENABLE 0x800 |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 33 | #define SUNXI_USB_PASSBY_EN 1 |
| 34 | |
| 35 | #define SUNXI_EHCI_AHB_ICHR8_EN (1 << 10) |
| 36 | #define SUNXI_EHCI_AHB_INCR4_BURST_EN (1 << 9) |
| 37 | #define SUNXI_EHCI_AHB_INCRX_ALIGN_EN (1 << 8) |
| 38 | #define SUNXI_EHCI_ULPI_BYPASS_EN (1 << 0) |
| 39 | |
Jelle van der Waa | a1f5d11 | 2016-02-09 23:59:33 +0100 | [diff] [blame] | 40 | #define REG_PHY_UNK_H3 0x420 |
| 41 | #define REG_PMU_UNK_H3 0x810 |
| 42 | |
Chen-Yu Tsai | ba56856 | 2016-03-30 00:26:51 +0800 | [diff] [blame] | 43 | /* A83T specific control bits for PHY0 */ |
| 44 | #define SUNXI_PHY_CTL_VBUSVLDEXT BIT(5) |
| 45 | #define SUNXI_PHY_CTL_SIDDQ BIT(3) |
| 46 | |
| 47 | /* A83T HSIC specific bits */ |
| 48 | #define SUNXI_EHCI_HS_FORCE BIT(20) |
| 49 | #define SUNXI_EHCI_CONNECT_DET BIT(17) |
| 50 | #define SUNXI_EHCI_CONNECT_INT BIT(16) |
| 51 | #define SUNXI_EHCI_HSIC BIT(1) |
| 52 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 53 | static struct sunxi_usb_phy { |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 54 | int usb_rst_mask; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 55 | int gpio_vbus; |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 56 | int gpio_vbus_det; |
Hans de Goede | aadd97f | 2015-06-14 17:29:53 +0200 | [diff] [blame] | 57 | int gpio_id_det; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 58 | int id; |
Hans de Goede | e820402 | 2015-04-27 16:57:54 +0200 | [diff] [blame] | 59 | int init_count; |
| 60 | int power_on_count; |
Alexander Graf | ee1d825 | 2016-03-29 17:29:09 +0200 | [diff] [blame] | 61 | ulong base; |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 62 | } sunxi_usb_phy[] = { |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 63 | { |
Hans de Goede | e7b852a | 2015-01-07 15:26:06 +0100 | [diff] [blame] | 64 | .usb_rst_mask = CCM_USB_CTRL_PHY0_RST | CCM_USB_CTRL_PHY0_CLK, |
Hans de Goede | e7b852a | 2015-01-07 15:26:06 +0100 | [diff] [blame] | 65 | .id = 0, |
Jelle van der Waa | a1f5d11 | 2016-02-09 23:59:33 +0100 | [diff] [blame] | 66 | .base = SUNXI_USB0_BASE, |
Hans de Goede | e7b852a | 2015-01-07 15:26:06 +0100 | [diff] [blame] | 67 | }, |
| 68 | { |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 69 | .usb_rst_mask = CCM_USB_CTRL_PHY1_RST | CCM_USB_CTRL_PHY1_CLK, |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 70 | .id = 1, |
Jelle van der Waa | a1f5d11 | 2016-02-09 23:59:33 +0100 | [diff] [blame] | 71 | .base = SUNXI_USB1_BASE, |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 72 | }, |
Hans de Goede | 1168e09 | 2015-04-27 16:50:04 +0200 | [diff] [blame] | 73 | #if CONFIG_SUNXI_USB_PHYS >= 3 |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 74 | { |
Chen-Yu Tsai | ba56856 | 2016-03-30 00:26:51 +0800 | [diff] [blame] | 75 | #ifdef CONFIG_MACH_SUN8I_A83T |
| 76 | .usb_rst_mask = CCM_USB_CTRL_HSIC_RST | CCM_USB_CTRL_HSIC_CLK | |
| 77 | CCM_USB_CTRL_12M_CLK, |
| 78 | #else |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 79 | .usb_rst_mask = CCM_USB_CTRL_PHY2_RST | CCM_USB_CTRL_PHY2_CLK, |
Chen-Yu Tsai | ba56856 | 2016-03-30 00:26:51 +0800 | [diff] [blame] | 80 | #endif |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 81 | .id = 2, |
Jelle van der Waa | a1f5d11 | 2016-02-09 23:59:33 +0100 | [diff] [blame] | 82 | .base = SUNXI_USB2_BASE, |
| 83 | }, |
| 84 | #endif |
| 85 | #if CONFIG_SUNXI_USB_PHYS >= 4 |
| 86 | { |
| 87 | .usb_rst_mask = CCM_USB_CTRL_PHY3_RST | CCM_USB_CTRL_PHY3_CLK, |
| 88 | .id = 3, |
| 89 | .base = SUNXI_USB3_BASE, |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 90 | } |
| 91 | #endif |
| 92 | }; |
| 93 | |
Hans de Goede | 99c9fb0 | 2016-04-01 22:39:26 +0200 | [diff] [blame] | 94 | static int initial_usb_scan_delay = CONFIG_INITIAL_USB_SCAN_DELAY; |
| 95 | |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 96 | static int get_vbus_gpio(int index) |
| 97 | { |
| 98 | switch (index) { |
Hans de Goede | e7b852a | 2015-01-07 15:26:06 +0100 | [diff] [blame] | 99 | case 0: return sunxi_name_to_gpio(CONFIG_USB0_VBUS_PIN); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 100 | case 1: return sunxi_name_to_gpio(CONFIG_USB1_VBUS_PIN); |
| 101 | case 2: return sunxi_name_to_gpio(CONFIG_USB2_VBUS_PIN); |
Hans de Goede | a60c3fc | 2016-03-18 08:42:01 +0100 | [diff] [blame] | 102 | case 3: return sunxi_name_to_gpio(CONFIG_USB3_VBUS_PIN); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 103 | } |
Hans de Goede | 494f5d0 | 2015-04-22 17:39:59 +0200 | [diff] [blame] | 104 | return -EINVAL; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 107 | static int get_vbus_detect_gpio(int index) |
| 108 | { |
| 109 | switch (index) { |
| 110 | case 0: return sunxi_name_to_gpio(CONFIG_USB0_VBUS_DET); |
| 111 | } |
Hans de Goede | 494f5d0 | 2015-04-22 17:39:59 +0200 | [diff] [blame] | 112 | return -EINVAL; |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Hans de Goede | aadd97f | 2015-06-14 17:29:53 +0200 | [diff] [blame] | 115 | static int get_id_detect_gpio(int index) |
| 116 | { |
| 117 | switch (index) { |
| 118 | case 0: return sunxi_name_to_gpio(CONFIG_USB0_ID_DET); |
| 119 | } |
| 120 | return -EINVAL; |
| 121 | } |
| 122 | |
Chen-Yu Tsai | ba56856 | 2016-03-30 00:26:51 +0800 | [diff] [blame] | 123 | __maybe_unused static void usb_phy_write(struct sunxi_usb_phy *phy, int addr, |
| 124 | int data, int len) |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 125 | { |
| 126 | int j = 0, usbc_bit = 0; |
Hans de Goede | 1f4e1d1 | 2015-04-27 14:36:23 +0200 | [diff] [blame] | 127 | void *dest = (void *)SUNXI_USB0_BASE + SUNXI_USB_CSR; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 128 | |
Vishnu Patekar | 3702f14 | 2015-03-01 23:47:48 +0530 | [diff] [blame] | 129 | #ifdef CONFIG_MACH_SUN8I_A33 |
| 130 | /* CSR needs to be explicitly initialized to 0 on A33 */ |
| 131 | writel(0, dest); |
| 132 | #endif |
| 133 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 134 | usbc_bit = 1 << (phy->id * 2); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 135 | for (j = 0; j < len; j++) { |
| 136 | /* set the bit address to be written */ |
| 137 | clrbits_le32(dest, 0xff << 8); |
| 138 | setbits_le32(dest, (addr + j) << 8); |
| 139 | |
| 140 | clrbits_le32(dest, usbc_bit); |
| 141 | /* set data bit */ |
| 142 | if (data & 0x1) |
| 143 | setbits_le32(dest, 1 << 7); |
| 144 | else |
| 145 | clrbits_le32(dest, 1 << 7); |
| 146 | |
| 147 | setbits_le32(dest, usbc_bit); |
| 148 | |
| 149 | clrbits_le32(dest, usbc_bit); |
| 150 | |
| 151 | data >>= 1; |
| 152 | } |
| 153 | } |
| 154 | |
Andre Przywara | 5fb9743 | 2017-02-16 01:20:27 +0000 | [diff] [blame] | 155 | #if defined(CONFIG_MACH_SUNXI_H3_H5) || defined(CONFIG_MACH_SUN50I) |
Jelle van der Waa | a1f5d11 | 2016-02-09 23:59:33 +0100 | [diff] [blame] | 156 | static void sunxi_usb_phy_config(struct sunxi_usb_phy *phy) |
| 157 | { |
Andre Przywara | 5fb9743 | 2017-02-16 01:20:27 +0000 | [diff] [blame] | 158 | #if defined CONFIG_MACH_SUNXI_H3_H5 |
Jelle van der Waa | a1f5d11 | 2016-02-09 23:59:33 +0100 | [diff] [blame] | 159 | if (phy->id == 0) |
| 160 | clrbits_le32(SUNXI_USBPHY_BASE + REG_PHY_UNK_H3, 0x01); |
Amit Singh Tomar | bd732d0 | 2016-10-21 02:24:30 +0100 | [diff] [blame] | 161 | #endif |
Jelle van der Waa | a1f5d11 | 2016-02-09 23:59:33 +0100 | [diff] [blame] | 162 | clrbits_le32(phy->base + REG_PMU_UNK_H3, 0x02); |
| 163 | } |
Chen-Yu Tsai | ba56856 | 2016-03-30 00:26:51 +0800 | [diff] [blame] | 164 | #elif defined CONFIG_MACH_SUN8I_A83T |
| 165 | static void sunxi_usb_phy_config(struct sunxi_usb_phy *phy) |
| 166 | { |
| 167 | } |
Jelle van der Waa | a1f5d11 | 2016-02-09 23:59:33 +0100 | [diff] [blame] | 168 | #else |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 169 | static void sunxi_usb_phy_config(struct sunxi_usb_phy *phy) |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 170 | { |
| 171 | /* The following comments are machine |
| 172 | * translated from Chinese, you have been warned! |
| 173 | */ |
| 174 | |
Hans de Goede | e7b852a | 2015-01-07 15:26:06 +0100 | [diff] [blame] | 175 | /* Regulation 45 ohms */ |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 176 | if (phy->id == 0) |
| 177 | usb_phy_write(phy, 0x0c, 0x01, 1); |
Hans de Goede | e7b852a | 2015-01-07 15:26:06 +0100 | [diff] [blame] | 178 | |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 179 | /* adjust PHY's magnitude and rate */ |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 180 | usb_phy_write(phy, 0x20, 0x14, 5); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 181 | |
| 182 | /* threshold adjustment disconnect */ |
Hans de Goede | 9395a0e | 2015-05-31 19:26:54 +0200 | [diff] [blame] | 183 | #if defined CONFIG_MACH_SUN5I || defined CONFIG_MACH_SUN7I |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 184 | usb_phy_write(phy, 0x2a, 2, 2); |
Hans de Goede | 9395a0e | 2015-05-31 19:26:54 +0200 | [diff] [blame] | 185 | #else |
| 186 | usb_phy_write(phy, 0x2a, 3, 2); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 187 | #endif |
| 188 | |
| 189 | return; |
| 190 | } |
Jelle van der Waa | a1f5d11 | 2016-02-09 23:59:33 +0100 | [diff] [blame] | 191 | #endif |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 192 | |
Jelle van der Waa | a1f5d11 | 2016-02-09 23:59:33 +0100 | [diff] [blame] | 193 | static void sunxi_usb_phy_passby(struct sunxi_usb_phy *phy, int enable) |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 194 | { |
| 195 | unsigned long bits = 0; |
Hans de Goede | 1f4e1d1 | 2015-04-27 14:36:23 +0200 | [diff] [blame] | 196 | void *addr; |
| 197 | |
Jelle van der Waa | a1f5d11 | 2016-02-09 23:59:33 +0100 | [diff] [blame] | 198 | addr = (void *)phy->base + SUNXI_USB_PMU_IRQ_ENABLE; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 199 | |
| 200 | bits = SUNXI_EHCI_AHB_ICHR8_EN | |
| 201 | SUNXI_EHCI_AHB_INCR4_BURST_EN | |
| 202 | SUNXI_EHCI_AHB_INCRX_ALIGN_EN | |
| 203 | SUNXI_EHCI_ULPI_BYPASS_EN; |
| 204 | |
Chen-Yu Tsai | ba56856 | 2016-03-30 00:26:51 +0800 | [diff] [blame] | 205 | #ifdef CONFIG_MACH_SUN8I_A83T |
| 206 | if (phy->id == 2) |
| 207 | bits |= SUNXI_EHCI_HS_FORCE | |
| 208 | SUNXI_EHCI_CONNECT_INT | |
| 209 | SUNXI_EHCI_HSIC; |
| 210 | #endif |
| 211 | |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 212 | if (enable) |
| 213 | setbits_le32(addr, bits); |
| 214 | else |
| 215 | clrbits_le32(addr, bits); |
| 216 | |
| 217 | return; |
| 218 | } |
| 219 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 220 | void sunxi_usb_phy_enable_squelch_detect(int index, int enable) |
Hans de Goede | ab721ad | 2015-03-27 20:54:25 +0100 | [diff] [blame] | 221 | { |
Chen-Yu Tsai | ba56856 | 2016-03-30 00:26:51 +0800 | [diff] [blame] | 222 | #ifndef CONFIG_MACH_SUN8I_A83T |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 223 | struct sunxi_usb_phy *phy = &sunxi_usb_phy[index]; |
Hans de Goede | ab721ad | 2015-03-27 20:54:25 +0100 | [diff] [blame] | 224 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 225 | usb_phy_write(phy, 0x3c, enable ? 0 : 2, 2); |
Chen-Yu Tsai | ba56856 | 2016-03-30 00:26:51 +0800 | [diff] [blame] | 226 | #endif |
Hans de Goede | ab721ad | 2015-03-27 20:54:25 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 229 | void sunxi_usb_phy_init(int index) |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 230 | { |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 231 | struct sunxi_usb_phy *phy = &sunxi_usb_phy[index]; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 232 | struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 233 | |
Hans de Goede | e820402 | 2015-04-27 16:57:54 +0200 | [diff] [blame] | 234 | phy->init_count++; |
| 235 | if (phy->init_count != 1) |
| 236 | return; |
| 237 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 238 | setbits_le32(&ccm->usb_clk_cfg, phy->usb_rst_mask); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 239 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 240 | sunxi_usb_phy_config(phy); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 241 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 242 | if (phy->id != 0) |
Jelle van der Waa | a1f5d11 | 2016-02-09 23:59:33 +0100 | [diff] [blame] | 243 | sunxi_usb_phy_passby(phy, SUNXI_USB_PASSBY_EN); |
Chen-Yu Tsai | ba56856 | 2016-03-30 00:26:51 +0800 | [diff] [blame] | 244 | |
| 245 | #ifdef CONFIG_MACH_SUN8I_A83T |
| 246 | if (phy->id == 0) { |
| 247 | setbits_le32(SUNXI_USB0_BASE + SUNXI_USB_CSR, |
| 248 | SUNXI_PHY_CTL_VBUSVLDEXT); |
| 249 | clrbits_le32(SUNXI_USB0_BASE + SUNXI_USB_CSR, |
| 250 | SUNXI_PHY_CTL_SIDDQ); |
| 251 | } |
| 252 | #endif |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 255 | void sunxi_usb_phy_exit(int index) |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 256 | { |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 257 | struct sunxi_usb_phy *phy = &sunxi_usb_phy[index]; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 258 | struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 259 | |
Hans de Goede | e820402 | 2015-04-27 16:57:54 +0200 | [diff] [blame] | 260 | phy->init_count--; |
| 261 | if (phy->init_count != 0) |
| 262 | return; |
| 263 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 264 | if (phy->id != 0) |
Jelle van der Waa | a1f5d11 | 2016-02-09 23:59:33 +0100 | [diff] [blame] | 265 | sunxi_usb_phy_passby(phy, !SUNXI_USB_PASSBY_EN); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 266 | |
Chen-Yu Tsai | ba56856 | 2016-03-30 00:26:51 +0800 | [diff] [blame] | 267 | #ifdef CONFIG_MACH_SUN8I_A83T |
| 268 | if (phy->id == 0) { |
| 269 | setbits_le32(SUNXI_USB0_BASE + SUNXI_USB_CSR, |
| 270 | SUNXI_PHY_CTL_SIDDQ); |
| 271 | } |
| 272 | #endif |
| 273 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 274 | clrbits_le32(&ccm->usb_clk_cfg, phy->usb_rst_mask); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 275 | } |
| 276 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 277 | void sunxi_usb_phy_power_on(int index) |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 278 | { |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 279 | struct sunxi_usb_phy *phy = &sunxi_usb_phy[index]; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 280 | |
Hans de Goede | 99c9fb0 | 2016-04-01 22:39:26 +0200 | [diff] [blame] | 281 | if (initial_usb_scan_delay) { |
| 282 | mdelay(initial_usb_scan_delay); |
| 283 | initial_usb_scan_delay = 0; |
| 284 | } |
| 285 | |
Hans de Goede | e820402 | 2015-04-27 16:57:54 +0200 | [diff] [blame] | 286 | phy->power_on_count++; |
| 287 | if (phy->power_on_count != 1) |
| 288 | return; |
| 289 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 290 | if (phy->gpio_vbus >= 0) |
| 291 | gpio_set_value(phy->gpio_vbus, 1); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 294 | void sunxi_usb_phy_power_off(int index) |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 295 | { |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 296 | struct sunxi_usb_phy *phy = &sunxi_usb_phy[index]; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 297 | |
Hans de Goede | e820402 | 2015-04-27 16:57:54 +0200 | [diff] [blame] | 298 | phy->power_on_count--; |
| 299 | if (phy->power_on_count != 0) |
| 300 | return; |
| 301 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 302 | if (phy->gpio_vbus >= 0) |
| 303 | gpio_set_value(phy->gpio_vbus, 0); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 304 | } |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 305 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 306 | int sunxi_usb_phy_vbus_detect(int index) |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 307 | { |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 308 | struct sunxi_usb_phy *phy = &sunxi_usb_phy[index]; |
Hans de Goede | 444af39 | 2015-03-27 21:46:00 +0100 | [diff] [blame] | 309 | int err, retries = 3; |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 310 | |
Hans de Goede | 78c2e64 | 2015-06-18 18:21:33 +0200 | [diff] [blame] | 311 | if (phy->gpio_vbus_det < 0) |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 312 | return phy->gpio_vbus_det; |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 313 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 314 | err = gpio_get_value(phy->gpio_vbus_det); |
Hans de Goede | 444af39 | 2015-03-27 21:46:00 +0100 | [diff] [blame] | 315 | /* |
| 316 | * Vbus may have been provided by the board and just been turned of |
| 317 | * some milliseconds ago on reset, what we're measuring then is a |
| 318 | * residual charge on Vbus, sleep a bit and try again. |
| 319 | */ |
| 320 | while (err > 0 && retries--) { |
| 321 | mdelay(100); |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 322 | err = gpio_get_value(phy->gpio_vbus_det); |
Hans de Goede | 444af39 | 2015-03-27 21:46:00 +0100 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | return err; |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 326 | } |
Hans de Goede | 1168e09 | 2015-04-27 16:50:04 +0200 | [diff] [blame] | 327 | |
Hans de Goede | aadd97f | 2015-06-14 17:29:53 +0200 | [diff] [blame] | 328 | int sunxi_usb_phy_id_detect(int index) |
| 329 | { |
| 330 | struct sunxi_usb_phy *phy = &sunxi_usb_phy[index]; |
| 331 | |
| 332 | if (phy->gpio_id_det < 0) |
| 333 | return phy->gpio_id_det; |
| 334 | |
| 335 | return gpio_get_value(phy->gpio_id_det); |
| 336 | } |
| 337 | |
Hans de Goede | 1168e09 | 2015-04-27 16:50:04 +0200 | [diff] [blame] | 338 | int sunxi_usb_phy_probe(void) |
| 339 | { |
| 340 | struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 341 | struct sunxi_usb_phy *phy; |
| 342 | int i, ret = 0; |
| 343 | |
| 344 | for (i = 0; i < CONFIG_SUNXI_USB_PHYS; i++) { |
| 345 | phy = &sunxi_usb_phy[i]; |
| 346 | |
| 347 | phy->gpio_vbus = get_vbus_gpio(i); |
| 348 | if (phy->gpio_vbus >= 0) { |
| 349 | ret = gpio_request(phy->gpio_vbus, "usb_vbus"); |
| 350 | if (ret) |
| 351 | return ret; |
| 352 | ret = gpio_direction_output(phy->gpio_vbus, 0); |
| 353 | if (ret) |
| 354 | return ret; |
| 355 | } |
| 356 | |
| 357 | phy->gpio_vbus_det = get_vbus_detect_gpio(i); |
| 358 | if (phy->gpio_vbus_det >= 0) { |
| 359 | ret = gpio_request(phy->gpio_vbus_det, "usb_vbus_det"); |
| 360 | if (ret) |
| 361 | return ret; |
| 362 | ret = gpio_direction_input(phy->gpio_vbus_det); |
| 363 | if (ret) |
| 364 | return ret; |
| 365 | } |
Hans de Goede | aadd97f | 2015-06-14 17:29:53 +0200 | [diff] [blame] | 366 | |
| 367 | phy->gpio_id_det = get_id_detect_gpio(i); |
| 368 | if (phy->gpio_id_det >= 0) { |
| 369 | ret = gpio_request(phy->gpio_id_det, "usb_id_det"); |
| 370 | if (ret) |
| 371 | return ret; |
| 372 | ret = gpio_direction_input(phy->gpio_id_det); |
| 373 | if (ret) |
| 374 | return ret; |
| 375 | sunxi_gpio_set_pull(phy->gpio_id_det, |
| 376 | SUNXI_GPIO_PULL_UP); |
| 377 | } |
Hans de Goede | 1168e09 | 2015-04-27 16:50:04 +0200 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | setbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE); |
| 381 | |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | int sunxi_usb_phy_remove(void) |
| 386 | { |
| 387 | struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 388 | struct sunxi_usb_phy *phy; |
| 389 | int i; |
| 390 | |
| 391 | clrbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE); |
| 392 | |
| 393 | for (i = 0; i < CONFIG_SUNXI_USB_PHYS; i++) { |
| 394 | phy = &sunxi_usb_phy[i]; |
| 395 | |
| 396 | if (phy->gpio_vbus >= 0) |
| 397 | gpio_free(phy->gpio_vbus); |
| 398 | |
| 399 | if (phy->gpio_vbus_det >= 0) |
| 400 | gpio_free(phy->gpio_vbus_det); |
Hans de Goede | aadd97f | 2015-06-14 17:29:53 +0200 | [diff] [blame] | 401 | |
| 402 | if (phy->gpio_id_det >= 0) |
| 403 | gpio_free(phy->gpio_id_det); |
Hans de Goede | 1168e09 | 2015-04-27 16:50:04 +0200 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | return 0; |
| 407 | } |