Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 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> |
Hans de Goede | e573b3c | 2015-01-11 19:33:35 +0100 | [diff] [blame] | 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 |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 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; |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 44 | int gpio_vbus_det; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 45 | int irq; |
| 46 | int id; |
| 47 | } sunxi_usbc_hcd[] = { |
| 48 | { |
Hans de Goede | e7b852a | 2015-01-07 15:26:06 +0100 | [diff] [blame] | 49 | .usb_rst_mask = CCM_USB_CTRL_PHY0_RST | CCM_USB_CTRL_PHY0_CLK, |
| 50 | .ahb_clk_mask = 1 << AHB_GATE_OFFSET_USB0, |
| 51 | #if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I |
| 52 | .irq = 71, |
| 53 | #else |
| 54 | .irq = 38, |
| 55 | #endif |
| 56 | .id = 0, |
| 57 | }, |
| 58 | { |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 59 | .usb_rst_mask = CCM_USB_CTRL_PHY1_RST | CCM_USB_CTRL_PHY1_CLK, |
| 60 | .ahb_clk_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0, |
| 61 | #if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I |
| 62 | .irq = 72, |
| 63 | #else |
| 64 | .irq = 39, |
| 65 | #endif |
| 66 | .id = 1, |
| 67 | }, |
| 68 | #if (CONFIG_USB_MAX_CONTROLLER_COUNT > 1) |
| 69 | { |
| 70 | .usb_rst_mask = CCM_USB_CTRL_PHY2_RST | CCM_USB_CTRL_PHY2_CLK, |
| 71 | .ahb_clk_mask = 1 << AHB_GATE_OFFSET_USB_EHCI1, |
| 72 | #ifdef CONFIG_MACH_SUN6I |
| 73 | .irq = 74, |
| 74 | #else |
| 75 | .irq = 40, |
| 76 | #endif |
| 77 | .id = 2, |
| 78 | } |
| 79 | #endif |
| 80 | }; |
| 81 | |
| 82 | static int enabled_hcd_count; |
| 83 | |
| 84 | void *sunxi_usbc_get_io_base(int index) |
| 85 | { |
| 86 | switch (index) { |
| 87 | case 0: |
| 88 | return (void *)SUNXI_USB0_BASE; |
| 89 | case 1: |
| 90 | return (void *)SUNXI_USB1_BASE; |
| 91 | case 2: |
| 92 | return (void *)SUNXI_USB2_BASE; |
| 93 | default: |
| 94 | return NULL; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | static int get_vbus_gpio(int index) |
| 99 | { |
| 100 | switch (index) { |
Hans de Goede | e7b852a | 2015-01-07 15:26:06 +0100 | [diff] [blame] | 101 | case 0: return sunxi_name_to_gpio(CONFIG_USB0_VBUS_PIN); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 102 | case 1: return sunxi_name_to_gpio(CONFIG_USB1_VBUS_PIN); |
| 103 | case 2: return sunxi_name_to_gpio(CONFIG_USB2_VBUS_PIN); |
| 104 | } |
| 105 | return -1; |
| 106 | } |
| 107 | |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 108 | static int get_vbus_detect_gpio(int index) |
| 109 | { |
| 110 | switch (index) { |
| 111 | case 0: return sunxi_name_to_gpio(CONFIG_USB0_VBUS_DET); |
| 112 | } |
| 113 | return -1; |
| 114 | } |
| 115 | |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 116 | static void usb_phy_write(struct sunxi_usbc_hcd *sunxi_usbc, int addr, |
| 117 | int data, int len) |
| 118 | { |
| 119 | int j = 0, usbc_bit = 0; |
| 120 | void *dest = sunxi_usbc_get_io_base(0) + SUNXI_USB_CSR; |
| 121 | |
| 122 | usbc_bit = 1 << (sunxi_usbc->id * 2); |
| 123 | for (j = 0; j < len; j++) { |
| 124 | /* set the bit address to be written */ |
| 125 | clrbits_le32(dest, 0xff << 8); |
| 126 | setbits_le32(dest, (addr + j) << 8); |
| 127 | |
| 128 | clrbits_le32(dest, usbc_bit); |
| 129 | /* set data bit */ |
| 130 | if (data & 0x1) |
| 131 | setbits_le32(dest, 1 << 7); |
| 132 | else |
| 133 | clrbits_le32(dest, 1 << 7); |
| 134 | |
| 135 | setbits_le32(dest, usbc_bit); |
| 136 | |
| 137 | clrbits_le32(dest, usbc_bit); |
| 138 | |
| 139 | data >>= 1; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | static void sunxi_usb_phy_init(struct sunxi_usbc_hcd *sunxi_usbc) |
| 144 | { |
| 145 | /* The following comments are machine |
| 146 | * translated from Chinese, you have been warned! |
| 147 | */ |
| 148 | |
Hans de Goede | e7b852a | 2015-01-07 15:26:06 +0100 | [diff] [blame] | 149 | /* Regulation 45 ohms */ |
| 150 | if (sunxi_usbc->id == 0) |
| 151 | usb_phy_write(sunxi_usbc, 0x0c, 0x01, 1); |
| 152 | |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 153 | /* adjust PHY's magnitude and rate */ |
| 154 | usb_phy_write(sunxi_usbc, 0x20, 0x14, 5); |
| 155 | |
| 156 | /* threshold adjustment disconnect */ |
| 157 | #if defined CONFIG_MACH_SUN4I || defined CONFIG_MACH_SUN6I |
| 158 | usb_phy_write(sunxi_usbc, 0x2a, 3, 2); |
| 159 | #else |
| 160 | usb_phy_write(sunxi_usbc, 0x2a, 2, 2); |
| 161 | #endif |
| 162 | |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | static void sunxi_usb_passby(struct sunxi_usbc_hcd *sunxi_usbc, int enable) |
| 167 | { |
| 168 | unsigned long bits = 0; |
| 169 | void *addr = sunxi_usbc_get_io_base(sunxi_usbc->id) + |
| 170 | SUNXI_USB_PMU_IRQ_ENABLE; |
| 171 | |
| 172 | bits = SUNXI_EHCI_AHB_ICHR8_EN | |
| 173 | SUNXI_EHCI_AHB_INCR4_BURST_EN | |
| 174 | SUNXI_EHCI_AHB_INCRX_ALIGN_EN | |
| 175 | SUNXI_EHCI_ULPI_BYPASS_EN; |
| 176 | |
| 177 | if (enable) |
| 178 | setbits_le32(addr, bits); |
| 179 | else |
| 180 | clrbits_le32(addr, bits); |
| 181 | |
| 182 | return; |
| 183 | } |
| 184 | |
Hans de Goede | ab721ad | 2015-03-27 20:54:25 +0100 | [diff] [blame] | 185 | void sunxi_usbc_enable_squelch_detect(int index, int enable) |
| 186 | { |
| 187 | struct sunxi_usbc_hcd *sunxi_usbc = &sunxi_usbc_hcd[index]; |
| 188 | |
| 189 | usb_phy_write(sunxi_usbc, 0x3c, enable ? 0 : 2, 2); |
| 190 | } |
| 191 | |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 192 | int sunxi_usbc_request_resources(int index) |
| 193 | { |
Hans de Goede | e7b852a | 2015-01-07 15:26:06 +0100 | [diff] [blame] | 194 | struct sunxi_usbc_hcd *sunxi_usbc = &sunxi_usbc_hcd[index]; |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 195 | int ret = 0; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 196 | |
| 197 | sunxi_usbc->gpio_vbus = get_vbus_gpio(index); |
| 198 | if (sunxi_usbc->gpio_vbus != -1) |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 199 | ret |= gpio_request(sunxi_usbc->gpio_vbus, "usbc_vbus"); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 200 | |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 201 | sunxi_usbc->gpio_vbus_det = get_vbus_detect_gpio(index); |
| 202 | if (sunxi_usbc->gpio_vbus_det != -1) |
| 203 | ret |= gpio_request(sunxi_usbc->gpio_vbus_det, "usbc_vbus_det"); |
| 204 | |
| 205 | return ret; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | int sunxi_usbc_free_resources(int index) |
| 209 | { |
Hans de Goede | e7b852a | 2015-01-07 15:26:06 +0100 | [diff] [blame] | 210 | struct sunxi_usbc_hcd *sunxi_usbc = &sunxi_usbc_hcd[index]; |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 211 | int ret = 0; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 212 | |
| 213 | if (sunxi_usbc->gpio_vbus != -1) |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 214 | ret |= gpio_free(sunxi_usbc->gpio_vbus); |
| 215 | |
| 216 | if (sunxi_usbc->gpio_vbus_det != -1) |
| 217 | ret |= gpio_free(sunxi_usbc->gpio_vbus_det); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 218 | |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 219 | return ret; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void sunxi_usbc_enable(int index) |
| 223 | { |
Hans de Goede | e7b852a | 2015-01-07 15:26:06 +0100 | [diff] [blame] | 224 | struct sunxi_usbc_hcd *sunxi_usbc = &sunxi_usbc_hcd[index]; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 225 | struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 226 | |
| 227 | /* enable common PHY only once */ |
| 228 | if (enabled_hcd_count == 0) |
| 229 | setbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE); |
| 230 | |
| 231 | setbits_le32(&ccm->usb_clk_cfg, sunxi_usbc->usb_rst_mask); |
| 232 | setbits_le32(&ccm->ahb_gate0, sunxi_usbc->ahb_clk_mask); |
| 233 | #if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I |
| 234 | setbits_le32(&ccm->ahb_reset0_cfg, sunxi_usbc->ahb_clk_mask); |
| 235 | #endif |
| 236 | |
| 237 | sunxi_usb_phy_init(sunxi_usbc); |
| 238 | |
Hans de Goede | e7b852a | 2015-01-07 15:26:06 +0100 | [diff] [blame] | 239 | if (sunxi_usbc->id != 0) |
| 240 | sunxi_usb_passby(sunxi_usbc, SUNXI_USB_PASSBY_EN); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 241 | |
| 242 | enabled_hcd_count++; |
| 243 | } |
| 244 | |
| 245 | void sunxi_usbc_disable(int index) |
| 246 | { |
Hans de Goede | e7b852a | 2015-01-07 15:26:06 +0100 | [diff] [blame] | 247 | struct sunxi_usbc_hcd *sunxi_usbc = &sunxi_usbc_hcd[index]; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 248 | struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 249 | |
Hans de Goede | e7b852a | 2015-01-07 15:26:06 +0100 | [diff] [blame] | 250 | if (sunxi_usbc->id != 0) |
| 251 | sunxi_usb_passby(sunxi_usbc, !SUNXI_USB_PASSBY_EN); |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 252 | |
| 253 | #if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I |
| 254 | clrbits_le32(&ccm->ahb_reset0_cfg, sunxi_usbc->ahb_clk_mask); |
| 255 | #endif |
| 256 | clrbits_le32(&ccm->ahb_gate0, sunxi_usbc->ahb_clk_mask); |
| 257 | clrbits_le32(&ccm->usb_clk_cfg, sunxi_usbc->usb_rst_mask); |
| 258 | |
| 259 | /* disable common PHY only once, for the last enabled hcd */ |
| 260 | if (enabled_hcd_count == 1) |
| 261 | clrbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE); |
| 262 | |
| 263 | enabled_hcd_count--; |
| 264 | } |
| 265 | |
| 266 | void sunxi_usbc_vbus_enable(int index) |
| 267 | { |
Hans de Goede | e7b852a | 2015-01-07 15:26:06 +0100 | [diff] [blame] | 268 | struct sunxi_usbc_hcd *sunxi_usbc = &sunxi_usbc_hcd[index]; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 269 | |
| 270 | if (sunxi_usbc->gpio_vbus != -1) |
| 271 | gpio_direction_output(sunxi_usbc->gpio_vbus, 1); |
| 272 | } |
| 273 | |
| 274 | void sunxi_usbc_vbus_disable(int index) |
| 275 | { |
Hans de Goede | e7b852a | 2015-01-07 15:26:06 +0100 | [diff] [blame] | 276 | struct sunxi_usbc_hcd *sunxi_usbc = &sunxi_usbc_hcd[index]; |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 277 | |
Hans de Goede | a144198 | 2015-01-07 15:08:43 +0100 | [diff] [blame] | 278 | if (sunxi_usbc->gpio_vbus != -1) |
| 279 | gpio_direction_output(sunxi_usbc->gpio_vbus, 0); |
| 280 | } |
Paul Kocialkowski | 7e9b7de | 2015-03-22 18:07:12 +0100 | [diff] [blame] | 281 | |
| 282 | int sunxi_usbc_vbus_detect(int index) |
| 283 | { |
| 284 | struct sunxi_usbc_hcd *sunxi_usbc = &sunxi_usbc_hcd[index]; |
| 285 | int err; |
| 286 | |
| 287 | if (sunxi_usbc->gpio_vbus_det == -1) { |
| 288 | eprintf("Error: invalid vbus detection pin\n"); |
| 289 | return -1; |
| 290 | } |
| 291 | |
| 292 | err = gpio_direction_input(sunxi_usbc->gpio_vbus_det); |
| 293 | if (err) |
| 294 | return err; |
| 295 | |
| 296 | return gpio_get_value(sunxi_usbc->gpio_vbus_det); |
| 297 | } |