Jagan Teki | 6768594 | 2018-05-07 13:03:26 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Allwinner sun4i USB PHY driver |
| 3 | * |
| 4 | * Copyright (C) 2017 Jagan Teki <jagan@amarulasolutions.com> |
| 5 | * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com> |
| 6 | * Copyright (C) 2014 Roman Byshko <rbyshko@gmail.com> |
| 7 | * |
| 8 | * Modelled arch/arm/mach-sunxi/usb_phy.c to compatible with generic-phy. |
| 9 | * |
| 10 | * SPDX-License-Identifier: GPL-2.0+ |
| 11 | */ |
| 12 | |
| 13 | #include <common.h> |
| 14 | #include <dm.h> |
| 15 | #include <dm/device.h> |
| 16 | #include <generic-phy.h> |
Jagan Teki | 129c45c | 2018-05-07 13:03:27 +0530 | [diff] [blame^] | 17 | #include <phy-sun4i-usb.h> |
Jagan Teki | 6768594 | 2018-05-07 13:03:26 +0530 | [diff] [blame] | 18 | #include <asm/gpio.h> |
| 19 | #include <asm/io.h> |
| 20 | #include <asm/arch/clock.h> |
| 21 | #include <asm/arch/cpu.h> |
| 22 | |
| 23 | #define REG_ISCR 0x00 |
| 24 | #define REG_PHYCTL_A10 0x04 |
| 25 | #define REG_PHYBIST 0x08 |
| 26 | #define REG_PHYTUNE 0x0c |
| 27 | #define REG_PHYCTL_A33 0x10 |
| 28 | #define REG_PHY_OTGCTL 0x20 |
| 29 | #define REG_PMU_UNK1 0x10 |
| 30 | |
| 31 | /* Common Control Bits for Both PHYs */ |
| 32 | #define PHY_PLL_BW 0x03 |
| 33 | #define PHY_RES45_CAL_EN 0x0c |
| 34 | |
| 35 | /* Private Control Bits for Each PHY */ |
| 36 | #define PHY_TX_AMPLITUDE_TUNE 0x20 |
| 37 | #define PHY_TX_SLEWRATE_TUNE 0x22 |
| 38 | #define PHY_DISCON_TH_SEL 0x2a |
| 39 | |
| 40 | #define PHYCTL_DATA BIT(7) |
| 41 | #define OTGCTL_ROUTE_MUSB BIT(0) |
| 42 | |
| 43 | #define PHY_TX_RATE BIT(4) |
| 44 | #define PHY_TX_MAGNITUDE BIT(2) |
| 45 | #define PHY_TX_AMPLITUDE_LEN 5 |
| 46 | |
| 47 | #define PHY_RES45_CAL_DATA BIT(0) |
| 48 | #define PHY_RES45_CAL_LEN 1 |
| 49 | #define PHY_DISCON_TH_LEN 2 |
| 50 | |
| 51 | #define SUNXI_AHB_ICHR8_EN BIT(10) |
| 52 | #define SUNXI_AHB_INCR4_BURST_EN BIT(9) |
| 53 | #define SUNXI_AHB_INCRX_ALIGN_EN BIT(8) |
| 54 | #define SUNXI_ULPI_BYPASS_EN BIT(0) |
| 55 | |
| 56 | #define MAX_PHYS 4 |
| 57 | |
| 58 | enum sun4i_usb_phy_type { |
| 59 | sun50i_a64_phy, |
| 60 | }; |
| 61 | |
| 62 | struct sun4i_usb_phy_cfg { |
| 63 | int num_phys; |
| 64 | enum sun4i_usb_phy_type type; |
| 65 | u32 disc_thresh; |
| 66 | u8 phyctl_offset; |
| 67 | bool enable_pmu_unk1; |
| 68 | bool phy0_dual_route; |
| 69 | }; |
| 70 | |
| 71 | struct sun4i_usb_phy_info { |
| 72 | const char *gpio_vbus; |
| 73 | const char *gpio_vbus_det; |
| 74 | const char *gpio_id_det; |
| 75 | int rst_mask; |
| 76 | } phy_info[] = { |
| 77 | { |
| 78 | .gpio_vbus = CONFIG_USB0_VBUS_PIN, |
| 79 | .gpio_vbus_det = CONFIG_USB0_VBUS_DET, |
| 80 | .gpio_id_det = CONFIG_USB0_ID_DET, |
| 81 | .rst_mask = (CCM_USB_CTRL_PHY0_RST | CCM_USB_CTRL_PHY0_CLK), |
| 82 | }, |
| 83 | { |
| 84 | .gpio_vbus = CONFIG_USB1_VBUS_PIN, |
| 85 | .gpio_vbus_det = NULL, |
| 86 | .gpio_id_det = NULL, |
| 87 | .rst_mask = (CCM_USB_CTRL_PHY1_RST | CCM_USB_CTRL_PHY1_CLK), |
| 88 | }, |
| 89 | { |
| 90 | .gpio_vbus = CONFIG_USB2_VBUS_PIN, |
| 91 | .gpio_vbus_det = NULL, |
| 92 | .gpio_id_det = NULL, |
| 93 | .rst_mask = (CCM_USB_CTRL_PHY2_RST | CCM_USB_CTRL_PHY2_CLK), |
| 94 | }, |
| 95 | { |
| 96 | .gpio_vbus = CONFIG_USB3_VBUS_PIN, |
| 97 | .gpio_vbus_det = NULL, |
| 98 | .gpio_id_det = NULL, |
| 99 | .rst_mask = (CCM_USB_CTRL_PHY3_RST | CCM_USB_CTRL_PHY3_CLK), |
| 100 | }, |
| 101 | }; |
| 102 | |
| 103 | struct sun4i_usb_phy_plat { |
| 104 | void __iomem *pmu; |
| 105 | int power_on_count; |
| 106 | int gpio_vbus; |
| 107 | int gpio_vbus_det; |
| 108 | int gpio_id_det; |
| 109 | int rst_mask; |
| 110 | int id; |
| 111 | }; |
| 112 | |
| 113 | struct sun4i_usb_phy_data { |
| 114 | void __iomem *base; |
| 115 | struct sunxi_ccm_reg *ccm; |
| 116 | const struct sun4i_usb_phy_cfg *cfg; |
| 117 | struct sun4i_usb_phy_plat *usb_phy; |
| 118 | }; |
| 119 | |
| 120 | static int initial_usb_scan_delay = CONFIG_INITIAL_USB_SCAN_DELAY; |
| 121 | |
| 122 | static void sun4i_usb_phy_write(struct phy *phy, u32 addr, u32 data, int len) |
| 123 | { |
| 124 | struct sun4i_usb_phy_data *phy_data = dev_get_priv(phy->dev); |
| 125 | struct sun4i_usb_phy_plat *usb_phy = &phy_data->usb_phy[phy->id]; |
| 126 | u32 temp, usbc_bit = BIT(usb_phy->id * 2); |
| 127 | void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset; |
| 128 | int i; |
| 129 | |
| 130 | if (phy_data->cfg->phyctl_offset == REG_PHYCTL_A33) { |
| 131 | /* SoCs newer than A33 need us to set phyctl to 0 explicitly */ |
| 132 | writel(0, phyctl); |
| 133 | } |
| 134 | |
| 135 | for (i = 0; i < len; i++) { |
| 136 | temp = readl(phyctl); |
| 137 | |
| 138 | /* clear the address portion */ |
| 139 | temp &= ~(0xff << 8); |
| 140 | |
| 141 | /* set the address */ |
| 142 | temp |= ((addr + i) << 8); |
| 143 | writel(temp, phyctl); |
| 144 | |
| 145 | /* set the data bit and clear usbc bit*/ |
| 146 | temp = readb(phyctl); |
| 147 | if (data & 0x1) |
| 148 | temp |= PHYCTL_DATA; |
| 149 | else |
| 150 | temp &= ~PHYCTL_DATA; |
| 151 | temp &= ~usbc_bit; |
| 152 | writeb(temp, phyctl); |
| 153 | |
| 154 | /* pulse usbc_bit */ |
| 155 | temp = readb(phyctl); |
| 156 | temp |= usbc_bit; |
| 157 | writeb(temp, phyctl); |
| 158 | |
| 159 | temp = readb(phyctl); |
| 160 | temp &= ~usbc_bit; |
| 161 | writeb(temp, phyctl); |
| 162 | |
| 163 | data >>= 1; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | static void sun4i_usb_phy_passby(struct sun4i_usb_phy_plat *usb_phy, |
| 168 | bool enable) |
| 169 | { |
| 170 | u32 bits, reg_value; |
| 171 | |
| 172 | if (!usb_phy->pmu) |
| 173 | return; |
| 174 | |
| 175 | bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN | |
| 176 | SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN; |
| 177 | reg_value = readl(usb_phy->pmu); |
| 178 | |
| 179 | if (enable) |
| 180 | reg_value |= bits; |
| 181 | else |
| 182 | reg_value &= ~bits; |
| 183 | |
| 184 | writel(reg_value, usb_phy->pmu); |
| 185 | } |
| 186 | |
| 187 | static int sun4i_usb_phy_power_on(struct phy *phy) |
| 188 | { |
| 189 | struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev); |
| 190 | struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id]; |
| 191 | |
| 192 | if (initial_usb_scan_delay) { |
| 193 | mdelay(initial_usb_scan_delay); |
| 194 | initial_usb_scan_delay = 0; |
| 195 | } |
| 196 | |
| 197 | usb_phy->power_on_count++; |
| 198 | if (usb_phy->power_on_count != 1) |
| 199 | return 0; |
| 200 | |
| 201 | if (usb_phy->gpio_vbus >= 0) |
| 202 | gpio_set_value(usb_phy->gpio_vbus, SUNXI_GPIO_PULL_UP); |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static int sun4i_usb_phy_power_off(struct phy *phy) |
| 208 | { |
| 209 | struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev); |
| 210 | struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id]; |
| 211 | |
| 212 | usb_phy->power_on_count--; |
| 213 | if (usb_phy->power_on_count != 0) |
| 214 | return 0; |
| 215 | |
| 216 | if (usb_phy->gpio_vbus >= 0) |
| 217 | gpio_set_value(usb_phy->gpio_vbus, SUNXI_GPIO_PULL_DISABLE); |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static void sun4i_usb_phy0_reroute(struct sun4i_usb_phy_data *data, bool id_det) |
| 223 | { |
| 224 | u32 regval; |
| 225 | |
| 226 | regval = readl(data->base + REG_PHY_OTGCTL); |
| 227 | if (!id_det) { |
| 228 | /* Host mode. Route phy0 to EHCI/OHCI */ |
| 229 | regval &= ~OTGCTL_ROUTE_MUSB; |
| 230 | } else { |
| 231 | /* Peripheral mode. Route phy0 to MUSB */ |
| 232 | regval |= OTGCTL_ROUTE_MUSB; |
| 233 | } |
| 234 | writel(regval, data->base + REG_PHY_OTGCTL); |
| 235 | } |
| 236 | |
| 237 | static int sun4i_usb_phy_init(struct phy *phy) |
| 238 | { |
| 239 | struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev); |
| 240 | struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id]; |
| 241 | u32 val; |
| 242 | |
| 243 | setbits_le32(&data->ccm->usb_clk_cfg, usb_phy->rst_mask); |
| 244 | |
| 245 | if (usb_phy->pmu && data->cfg->enable_pmu_unk1) { |
| 246 | val = readl(usb_phy->pmu + REG_PMU_UNK1); |
| 247 | writel(val & ~2, usb_phy->pmu + REG_PMU_UNK1); |
| 248 | } |
| 249 | |
| 250 | if (usb_phy->id == 0) |
| 251 | sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN, PHY_RES45_CAL_DATA, |
| 252 | PHY_RES45_CAL_LEN); |
| 253 | |
| 254 | /* Adjust PHY's magnitude and rate */ |
| 255 | sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, PHY_TX_MAGNITUDE | |
| 256 | PHY_TX_RATE, PHY_TX_AMPLITUDE_LEN); |
| 257 | |
| 258 | /* Disconnect threshold adjustment */ |
| 259 | sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL, data->cfg->disc_thresh, |
| 260 | PHY_DISCON_TH_LEN); |
| 261 | |
| 262 | if (usb_phy->id != 0) |
| 263 | sun4i_usb_phy_passby(usb_phy, true); |
| 264 | |
| 265 | sun4i_usb_phy0_reroute(data, true); |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static int sun4i_usb_phy_exit(struct phy *phy) |
| 271 | { |
| 272 | struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev); |
| 273 | struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id]; |
| 274 | |
| 275 | sun4i_usb_phy_passby(usb_phy, false); |
| 276 | |
| 277 | clrbits_le32(&data->ccm->usb_clk_cfg, usb_phy->rst_mask); |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static int sun4i_usb_phy_xlate(struct phy *phy, |
| 283 | struct ofnode_phandle_args *args) |
| 284 | { |
| 285 | struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev); |
| 286 | |
| 287 | if (args->args_count >= data->cfg->num_phys) |
| 288 | return -EINVAL; |
| 289 | |
| 290 | if (args->args_count) |
| 291 | phy->id = args->args[0]; |
| 292 | else |
| 293 | phy->id = 0; |
| 294 | |
| 295 | debug("%s: phy_id = %ld\n", __func__, phy->id); |
| 296 | return 0; |
| 297 | } |
| 298 | |
Jagan Teki | 129c45c | 2018-05-07 13:03:27 +0530 | [diff] [blame^] | 299 | int sun4i_usb_phy_vbus_detect(struct phy *phy) |
| 300 | { |
| 301 | struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev); |
| 302 | struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id]; |
| 303 | int err, retries = 3; |
| 304 | |
| 305 | debug("%s: id_det = %d\n", __func__, usb_phy->gpio_id_det); |
| 306 | |
| 307 | if (usb_phy->gpio_vbus_det < 0) |
| 308 | return usb_phy->gpio_vbus_det; |
| 309 | |
| 310 | err = gpio_get_value(usb_phy->gpio_vbus_det); |
| 311 | /* |
| 312 | * Vbus may have been provided by the board and just been turned of |
| 313 | * some milliseconds ago on reset, what we're measuring then is a |
| 314 | * residual charge on Vbus, sleep a bit and try again. |
| 315 | */ |
| 316 | while (err > 0 && retries--) { |
| 317 | mdelay(100); |
| 318 | err = gpio_get_value(usb_phy->gpio_vbus_det); |
| 319 | } |
| 320 | |
| 321 | return err; |
| 322 | } |
| 323 | |
| 324 | int sun4i_usb_phy_id_detect(struct phy *phy) |
| 325 | { |
| 326 | struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev); |
| 327 | struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id]; |
| 328 | |
| 329 | debug("%s: id_det = %d\n", __func__, usb_phy->gpio_id_det); |
| 330 | |
| 331 | if (usb_phy->gpio_id_det < 0) |
| 332 | return usb_phy->gpio_id_det; |
| 333 | |
| 334 | return gpio_get_value(usb_phy->gpio_id_det); |
| 335 | } |
| 336 | |
Jagan Teki | 6768594 | 2018-05-07 13:03:26 +0530 | [diff] [blame] | 337 | static struct phy_ops sun4i_usb_phy_ops = { |
| 338 | .of_xlate = sun4i_usb_phy_xlate, |
| 339 | .init = sun4i_usb_phy_init, |
| 340 | .power_on = sun4i_usb_phy_power_on, |
| 341 | .power_off = sun4i_usb_phy_power_off, |
| 342 | .exit = sun4i_usb_phy_exit, |
| 343 | }; |
| 344 | |
| 345 | static int sun4i_usb_phy_probe(struct udevice *dev) |
| 346 | { |
| 347 | struct sun4i_usb_phy_plat *plat = dev_get_platdata(dev); |
| 348 | struct sun4i_usb_phy_data *data = dev_get_priv(dev); |
| 349 | int i, ret; |
| 350 | |
| 351 | data->cfg = (const struct sun4i_usb_phy_cfg *)dev_get_driver_data(dev); |
| 352 | if (!data->cfg) |
| 353 | return -EINVAL; |
| 354 | |
| 355 | data->base = (void __iomem *)devfdt_get_addr_name(dev, "phy_ctrl"); |
| 356 | if (IS_ERR(data->base)) |
| 357 | return PTR_ERR(data->base); |
| 358 | |
| 359 | data->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 360 | if (IS_ERR(data->ccm)) |
| 361 | return PTR_ERR(data->ccm); |
| 362 | |
| 363 | data->usb_phy = plat; |
| 364 | for (i = 0; i < data->cfg->num_phys; i++) { |
| 365 | struct sun4i_usb_phy_plat *phy = &plat[i]; |
| 366 | struct sun4i_usb_phy_info *info = &phy_info[i]; |
| 367 | char name[16]; |
| 368 | |
| 369 | phy->gpio_vbus = sunxi_name_to_gpio(info->gpio_vbus); |
| 370 | if (phy->gpio_vbus >= 0) { |
| 371 | ret = gpio_request(phy->gpio_vbus, "usb_vbus"); |
| 372 | if (ret) |
| 373 | return ret; |
| 374 | ret = gpio_direction_output(phy->gpio_vbus, 0); |
| 375 | if (ret) |
| 376 | return ret; |
| 377 | } |
| 378 | |
| 379 | phy->gpio_vbus_det = sunxi_name_to_gpio(info->gpio_vbus_det); |
| 380 | if (phy->gpio_vbus_det >= 0) { |
| 381 | ret = gpio_request(phy->gpio_vbus_det, "usb_vbus_det"); |
| 382 | if (ret) |
| 383 | return ret; |
| 384 | ret = gpio_direction_input(phy->gpio_vbus_det); |
| 385 | if (ret) |
| 386 | return ret; |
| 387 | } |
| 388 | |
| 389 | phy->gpio_id_det = sunxi_name_to_gpio(info->gpio_id_det); |
| 390 | if (phy->gpio_id_det >= 0) { |
| 391 | ret = gpio_request(phy->gpio_id_det, "usb_id_det"); |
| 392 | if (ret) |
| 393 | return ret; |
| 394 | ret = gpio_direction_input(phy->gpio_id_det); |
| 395 | if (ret) |
| 396 | return ret; |
| 397 | sunxi_gpio_set_pull(phy->gpio_id_det, SUNXI_GPIO_PULL_UP); |
| 398 | } |
| 399 | |
| 400 | if (i || data->cfg->phy0_dual_route) { |
| 401 | snprintf(name, sizeof(name), "pmu%d", i); |
| 402 | phy->pmu = (void __iomem *)devfdt_get_addr_name(dev, name); |
| 403 | if (IS_ERR(phy->pmu)) |
| 404 | return PTR_ERR(phy->pmu); |
| 405 | } |
| 406 | |
| 407 | phy->id = i; |
| 408 | phy->rst_mask = info->rst_mask; |
| 409 | }; |
| 410 | |
| 411 | setbits_le32(&data->ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE); |
| 412 | |
| 413 | debug("Allwinner Sun4I USB PHY driver loaded\n"); |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | static const struct sun4i_usb_phy_cfg sun50i_a64_cfg = { |
| 418 | .num_phys = 2, |
| 419 | .type = sun50i_a64_phy, |
| 420 | .disc_thresh = 3, |
| 421 | .phyctl_offset = REG_PHYCTL_A33, |
| 422 | .enable_pmu_unk1 = true, |
| 423 | .phy0_dual_route = true, |
| 424 | }; |
| 425 | |
| 426 | static const struct udevice_id sun4i_usb_phy_ids[] = { |
| 427 | { .compatible = "allwinner,sun50i-a64-usb-phy", .data = (ulong)&sun50i_a64_cfg}, |
| 428 | { } |
| 429 | }; |
| 430 | |
| 431 | U_BOOT_DRIVER(sun4i_usb_phy) = { |
| 432 | .name = "sun4i_usb_phy", |
| 433 | .id = UCLASS_PHY, |
| 434 | .of_match = sun4i_usb_phy_ids, |
| 435 | .ops = &sun4i_usb_phy_ops, |
| 436 | .probe = sun4i_usb_phy_probe, |
| 437 | .platdata_auto_alloc_size = sizeof(struct sun4i_usb_phy_plat[MAX_PHYS]), |
| 438 | .priv_auto_alloc_size = sizeof(struct sun4i_usb_phy_data), |
| 439 | }; |