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 | |
| 21 | #define SUNXI_USB_PMU_IRQ_ENABLE 0x800 |
Vishnu Patekar | 3702f14 | 2015-03-01 23:47:48 +0530 | [diff] [blame] | 22 | #ifdef CONFIG_MACH_SUN8I_A33 |
| 23 | #define SUNXI_USB_CSR 0x410 |
| 24 | #else |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 25 | #define SUNXI_USB_CSR 0x404 |
Vishnu Patekar | 3702f14 | 2015-03-01 23:47:48 +0530 | [diff] [blame] | 26 | #endif |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 27 | #define SUNXI_USB_PASSBY_EN 1 |
| 28 | |
| 29 | #define SUNXI_EHCI_AHB_ICHR8_EN (1 << 10) |
| 30 | #define SUNXI_EHCI_AHB_INCR4_BURST_EN (1 << 9) |
| 31 | #define SUNXI_EHCI_AHB_INCRX_ALIGN_EN (1 << 8) |
| 32 | #define SUNXI_EHCI_ULPI_BYPASS_EN (1 << 0) |
| 33 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 34 | static struct sunxi_usb_phy { |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 35 | int usb_rst_mask; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 36 | int gpio_vbus; |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 37 | int gpio_vbus_det; |
Hans de Goede | aadd97f | 2015-06-14 17:29:53 +0200 | [diff] [blame] | 38 | int gpio_id_det; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 39 | int id; |
Hans de Goede | e820402 | 2015-04-27 16:57:54 +0200 | [diff] [blame] | 40 | int init_count; |
| 41 | int power_on_count; |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 42 | } sunxi_usb_phy[] = { |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 43 | { |
Hans de Goede | e7b852a | 2015-01-07 15:26:06 +0100 | [diff] [blame] | 44 | .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] | 45 | .id = 0, |
| 46 | }, |
| 47 | { |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 48 | .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] | 49 | .id = 1, |
| 50 | }, |
Hans de Goede | 1168e09 | 2015-04-27 16:50:04 +0200 | [diff] [blame] | 51 | #if CONFIG_SUNXI_USB_PHYS >= 3 |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 52 | { |
| 53 | .usb_rst_mask = CCM_USB_CTRL_PHY2_RST | CCM_USB_CTRL_PHY2_CLK, |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 54 | .id = 2, |
| 55 | } |
| 56 | #endif |
| 57 | }; |
| 58 | |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 59 | static int get_vbus_gpio(int index) |
| 60 | { |
| 61 | switch (index) { |
Hans de Goede | e7b852a | 2015-01-07 15:26:06 +0100 | [diff] [blame] | 62 | case 0: return sunxi_name_to_gpio(CONFIG_USB0_VBUS_PIN); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 63 | case 1: return sunxi_name_to_gpio(CONFIG_USB1_VBUS_PIN); |
| 64 | case 2: return sunxi_name_to_gpio(CONFIG_USB2_VBUS_PIN); |
| 65 | } |
Hans de Goede | 494f5d0 | 2015-04-22 17:39:59 +0200 | [diff] [blame] | 66 | return -EINVAL; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 69 | static int get_vbus_detect_gpio(int index) |
| 70 | { |
| 71 | switch (index) { |
| 72 | case 0: return sunxi_name_to_gpio(CONFIG_USB0_VBUS_DET); |
| 73 | } |
Hans de Goede | 494f5d0 | 2015-04-22 17:39:59 +0200 | [diff] [blame] | 74 | return -EINVAL; |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Hans de Goede | aadd97f | 2015-06-14 17:29:53 +0200 | [diff] [blame] | 77 | static int get_id_detect_gpio(int index) |
| 78 | { |
| 79 | switch (index) { |
| 80 | case 0: return sunxi_name_to_gpio(CONFIG_USB0_ID_DET); |
| 81 | } |
| 82 | return -EINVAL; |
| 83 | } |
| 84 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 85 | static void usb_phy_write(struct sunxi_usb_phy *phy, int addr, |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 86 | int data, int len) |
| 87 | { |
| 88 | int j = 0, usbc_bit = 0; |
Hans de Goede | 1f4e1d1 | 2015-04-27 14:36:23 +0200 | [diff] [blame] | 89 | void *dest = (void *)SUNXI_USB0_BASE + SUNXI_USB_CSR; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 90 | |
Vishnu Patekar | 3702f14 | 2015-03-01 23:47:48 +0530 | [diff] [blame] | 91 | #ifdef CONFIG_MACH_SUN8I_A33 |
| 92 | /* CSR needs to be explicitly initialized to 0 on A33 */ |
| 93 | writel(0, dest); |
| 94 | #endif |
| 95 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 96 | usbc_bit = 1 << (phy->id * 2); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 97 | for (j = 0; j < len; j++) { |
| 98 | /* set the bit address to be written */ |
| 99 | clrbits_le32(dest, 0xff << 8); |
| 100 | setbits_le32(dest, (addr + j) << 8); |
| 101 | |
| 102 | clrbits_le32(dest, usbc_bit); |
| 103 | /* set data bit */ |
| 104 | if (data & 0x1) |
| 105 | setbits_le32(dest, 1 << 7); |
| 106 | else |
| 107 | clrbits_le32(dest, 1 << 7); |
| 108 | |
| 109 | setbits_le32(dest, usbc_bit); |
| 110 | |
| 111 | clrbits_le32(dest, usbc_bit); |
| 112 | |
| 113 | data >>= 1; |
| 114 | } |
| 115 | } |
| 116 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 117 | static void sunxi_usb_phy_config(struct sunxi_usb_phy *phy) |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 118 | { |
| 119 | /* The following comments are machine |
| 120 | * translated from Chinese, you have been warned! |
| 121 | */ |
| 122 | |
Hans de Goede | e7b852a | 2015-01-07 15:26:06 +0100 | [diff] [blame] | 123 | /* Regulation 45 ohms */ |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 124 | if (phy->id == 0) |
| 125 | usb_phy_write(phy, 0x0c, 0x01, 1); |
Hans de Goede | e7b852a | 2015-01-07 15:26:06 +0100 | [diff] [blame] | 126 | |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 127 | /* adjust PHY's magnitude and rate */ |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 128 | usb_phy_write(phy, 0x20, 0x14, 5); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 129 | |
| 130 | /* threshold adjustment disconnect */ |
Hans de Goede | 9395a0e | 2015-05-31 19:26:54 +0200 | [diff] [blame] | 131 | #if defined CONFIG_MACH_SUN5I || defined CONFIG_MACH_SUN7I |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 132 | usb_phy_write(phy, 0x2a, 2, 2); |
Hans de Goede | 9395a0e | 2015-05-31 19:26:54 +0200 | [diff] [blame] | 133 | #else |
| 134 | usb_phy_write(phy, 0x2a, 3, 2); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 135 | #endif |
| 136 | |
| 137 | return; |
| 138 | } |
| 139 | |
Hans de Goede | 1f4e1d1 | 2015-04-27 14:36:23 +0200 | [diff] [blame] | 140 | static void sunxi_usb_phy_passby(int index, int enable) |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 141 | { |
| 142 | unsigned long bits = 0; |
Hans de Goede | 1f4e1d1 | 2015-04-27 14:36:23 +0200 | [diff] [blame] | 143 | void *addr; |
| 144 | |
| 145 | if (index == 1) |
| 146 | addr = (void *)SUNXI_USB1_BASE + SUNXI_USB_PMU_IRQ_ENABLE; |
| 147 | else |
| 148 | addr = (void *)SUNXI_USB2_BASE + SUNXI_USB_PMU_IRQ_ENABLE; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 149 | |
| 150 | bits = SUNXI_EHCI_AHB_ICHR8_EN | |
| 151 | SUNXI_EHCI_AHB_INCR4_BURST_EN | |
| 152 | SUNXI_EHCI_AHB_INCRX_ALIGN_EN | |
| 153 | SUNXI_EHCI_ULPI_BYPASS_EN; |
| 154 | |
| 155 | if (enable) |
| 156 | setbits_le32(addr, bits); |
| 157 | else |
| 158 | clrbits_le32(addr, bits); |
| 159 | |
| 160 | return; |
| 161 | } |
| 162 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 163 | void sunxi_usb_phy_enable_squelch_detect(int index, int enable) |
Hans de Goede | ab721ad | 2015-03-27 20:54:25 +0100 | [diff] [blame] | 164 | { |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 165 | struct sunxi_usb_phy *phy = &sunxi_usb_phy[index]; |
Hans de Goede | ab721ad | 2015-03-27 20:54:25 +0100 | [diff] [blame] | 166 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 167 | usb_phy_write(phy, 0x3c, enable ? 0 : 2, 2); |
Hans de Goede | ab721ad | 2015-03-27 20:54:25 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 170 | void sunxi_usb_phy_init(int index) |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 171 | { |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 172 | struct sunxi_usb_phy *phy = &sunxi_usb_phy[index]; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 173 | struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 174 | |
Hans de Goede | e820402 | 2015-04-27 16:57:54 +0200 | [diff] [blame] | 175 | phy->init_count++; |
| 176 | if (phy->init_count != 1) |
| 177 | return; |
| 178 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 179 | setbits_le32(&ccm->usb_clk_cfg, phy->usb_rst_mask); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 180 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 181 | sunxi_usb_phy_config(phy); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 182 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 183 | if (phy->id != 0) |
Hans de Goede | 1f4e1d1 | 2015-04-27 14:36:23 +0200 | [diff] [blame] | 184 | sunxi_usb_phy_passby(index, SUNXI_USB_PASSBY_EN); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 187 | void sunxi_usb_phy_exit(int index) |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 188 | { |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 189 | struct sunxi_usb_phy *phy = &sunxi_usb_phy[index]; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 190 | struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 191 | |
Hans de Goede | e820402 | 2015-04-27 16:57:54 +0200 | [diff] [blame] | 192 | phy->init_count--; |
| 193 | if (phy->init_count != 0) |
| 194 | return; |
| 195 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 196 | if (phy->id != 0) |
Hans de Goede | 1f4e1d1 | 2015-04-27 14:36:23 +0200 | [diff] [blame] | 197 | sunxi_usb_phy_passby(index, !SUNXI_USB_PASSBY_EN); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 198 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 199 | clrbits_le32(&ccm->usb_clk_cfg, phy->usb_rst_mask); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 202 | void sunxi_usb_phy_power_on(int index) |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 203 | { |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 204 | struct sunxi_usb_phy *phy = &sunxi_usb_phy[index]; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 205 | |
Hans de Goede | e820402 | 2015-04-27 16:57:54 +0200 | [diff] [blame] | 206 | phy->power_on_count++; |
| 207 | if (phy->power_on_count != 1) |
| 208 | return; |
| 209 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 210 | if (phy->gpio_vbus >= 0) |
| 211 | gpio_set_value(phy->gpio_vbus, 1); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 212 | } |
| 213 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 214 | void sunxi_usb_phy_power_off(int index) |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 215 | { |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 216 | struct sunxi_usb_phy *phy = &sunxi_usb_phy[index]; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 217 | |
Hans de Goede | e820402 | 2015-04-27 16:57:54 +0200 | [diff] [blame] | 218 | phy->power_on_count--; |
| 219 | if (phy->power_on_count != 0) |
| 220 | return; |
| 221 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 222 | if (phy->gpio_vbus >= 0) |
| 223 | gpio_set_value(phy->gpio_vbus, 0); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 224 | } |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 225 | |
Hans de Goede | 39c119d | 2015-07-08 16:44:22 +0200 | [diff] [blame] | 226 | int sunxi_usb_phy_power_is_on(int index) |
| 227 | { |
| 228 | struct sunxi_usb_phy *phy = &sunxi_usb_phy[index]; |
| 229 | |
| 230 | return phy->power_on_count > 0; |
| 231 | } |
| 232 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 233 | int sunxi_usb_phy_vbus_detect(int index) |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 234 | { |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 235 | struct sunxi_usb_phy *phy = &sunxi_usb_phy[index]; |
Hans de Goede | 444af39 | 2015-03-27 21:46:00 +0100 | [diff] [blame] | 236 | int err, retries = 3; |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 237 | |
Hans de Goede | 78c2e64 | 2015-06-18 18:21:33 +0200 | [diff] [blame] | 238 | if (phy->gpio_vbus_det < 0) |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 239 | return phy->gpio_vbus_det; |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 240 | |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 241 | err = gpio_get_value(phy->gpio_vbus_det); |
Hans de Goede | 444af39 | 2015-03-27 21:46:00 +0100 | [diff] [blame] | 242 | /* |
| 243 | * Vbus may have been provided by the board and just been turned of |
| 244 | * some milliseconds ago on reset, what we're measuring then is a |
| 245 | * residual charge on Vbus, sleep a bit and try again. |
| 246 | */ |
| 247 | while (err > 0 && retries--) { |
| 248 | mdelay(100); |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 249 | err = gpio_get_value(phy->gpio_vbus_det); |
Hans de Goede | 444af39 | 2015-03-27 21:46:00 +0100 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | return err; |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 253 | } |
Hans de Goede | 1168e09 | 2015-04-27 16:50:04 +0200 | [diff] [blame] | 254 | |
Hans de Goede | aadd97f | 2015-06-14 17:29:53 +0200 | [diff] [blame] | 255 | int sunxi_usb_phy_id_detect(int index) |
| 256 | { |
| 257 | struct sunxi_usb_phy *phy = &sunxi_usb_phy[index]; |
| 258 | |
| 259 | if (phy->gpio_id_det < 0) |
| 260 | return phy->gpio_id_det; |
| 261 | |
| 262 | return gpio_get_value(phy->gpio_id_det); |
| 263 | } |
| 264 | |
Hans de Goede | 1168e09 | 2015-04-27 16:50:04 +0200 | [diff] [blame] | 265 | int sunxi_usb_phy_probe(void) |
| 266 | { |
| 267 | struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 268 | struct sunxi_usb_phy *phy; |
| 269 | int i, ret = 0; |
| 270 | |
| 271 | for (i = 0; i < CONFIG_SUNXI_USB_PHYS; i++) { |
| 272 | phy = &sunxi_usb_phy[i]; |
| 273 | |
| 274 | phy->gpio_vbus = get_vbus_gpio(i); |
| 275 | if (phy->gpio_vbus >= 0) { |
| 276 | ret = gpio_request(phy->gpio_vbus, "usb_vbus"); |
| 277 | if (ret) |
| 278 | return ret; |
| 279 | ret = gpio_direction_output(phy->gpio_vbus, 0); |
| 280 | if (ret) |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | phy->gpio_vbus_det = get_vbus_detect_gpio(i); |
| 285 | if (phy->gpio_vbus_det >= 0) { |
| 286 | ret = gpio_request(phy->gpio_vbus_det, "usb_vbus_det"); |
| 287 | if (ret) |
| 288 | return ret; |
| 289 | ret = gpio_direction_input(phy->gpio_vbus_det); |
| 290 | if (ret) |
| 291 | return ret; |
| 292 | } |
Hans de Goede | aadd97f | 2015-06-14 17:29:53 +0200 | [diff] [blame] | 293 | |
| 294 | phy->gpio_id_det = get_id_detect_gpio(i); |
| 295 | if (phy->gpio_id_det >= 0) { |
| 296 | ret = gpio_request(phy->gpio_id_det, "usb_id_det"); |
| 297 | if (ret) |
| 298 | return ret; |
| 299 | ret = gpio_direction_input(phy->gpio_id_det); |
| 300 | if (ret) |
| 301 | return ret; |
| 302 | sunxi_gpio_set_pull(phy->gpio_id_det, |
| 303 | SUNXI_GPIO_PULL_UP); |
| 304 | } |
Hans de Goede | 1168e09 | 2015-04-27 16:50:04 +0200 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | setbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE); |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | int sunxi_usb_phy_remove(void) |
| 313 | { |
| 314 | struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 315 | struct sunxi_usb_phy *phy; |
| 316 | int i; |
| 317 | |
| 318 | clrbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE); |
| 319 | |
| 320 | for (i = 0; i < CONFIG_SUNXI_USB_PHYS; i++) { |
| 321 | phy = &sunxi_usb_phy[i]; |
| 322 | |
| 323 | if (phy->gpio_vbus >= 0) |
| 324 | gpio_free(phy->gpio_vbus); |
| 325 | |
| 326 | if (phy->gpio_vbus_det >= 0) |
| 327 | gpio_free(phy->gpio_vbus_det); |
Hans de Goede | aadd97f | 2015-06-14 17:29:53 +0200 | [diff] [blame] | 328 | |
| 329 | if (phy->gpio_id_det >= 0) |
| 330 | gpio_free(phy->gpio_id_det); |
Hans de Goede | 1168e09 | 2015-04-27 16:50:04 +0200 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | return 0; |
| 334 | } |