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