Beniamino Galvani | df42f32 | 2019-08-18 15:42:54 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Meson8, Meson8b and GXBB USB2 PHY driver |
| 4 | * |
| 5 | * Copyright (C) 2016 Martin Blumenstingl <martin.blumenstingl@googlemail.com> |
| 6 | * Copyright (C) 2018 BayLibre, SAS |
| 7 | * |
| 8 | * Author: Beniamino Galvani <b.galvani@gmail.com> |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <clk.h> |
| 13 | #include <dm.h> |
| 14 | #include <generic-phy.h> |
Beniamino Galvani | df42f32 | 2019-08-18 15:42:54 +0200 | [diff] [blame] | 15 | #include <regmap.h> |
| 16 | #include <reset.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 17 | #include <linux/bitops.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 18 | #include <linux/printk.h> |
Beniamino Galvani | df42f32 | 2019-08-18 15:42:54 +0200 | [diff] [blame] | 19 | |
| 20 | #define REG_CONFIG 0x00 |
| 21 | #define REG_CONFIG_CLK_EN BIT(0) |
| 22 | #define REG_CONFIG_CLK_SEL_MASK GENMASK(3, 1) |
| 23 | #define REG_CONFIG_CLK_DIV_MASK GENMASK(10, 4) |
| 24 | #define REG_CONFIG_CLK_32k_ALTSEL BIT(15) |
| 25 | #define REG_CONFIG_TEST_TRIG BIT(31) |
| 26 | |
| 27 | #define REG_CTRL 0x04 |
| 28 | #define REG_CTRL_SOFT_PRST BIT(0) |
| 29 | #define REG_CTRL_SOFT_HRESET BIT(1) |
| 30 | #define REG_CTRL_SS_SCALEDOWN_MODE_MASK GENMASK(3, 2) |
| 31 | #define REG_CTRL_CLK_DET_RST BIT(4) |
| 32 | #define REG_CTRL_INTR_SEL BIT(5) |
| 33 | #define REG_CTRL_CLK_DETECTED BIT(8) |
| 34 | #define REG_CTRL_SOF_SENT_RCVD_TGL BIT(9) |
| 35 | #define REG_CTRL_SOF_TOGGLE_OUT BIT(10) |
| 36 | #define REG_CTRL_POWER_ON_RESET BIT(15) |
| 37 | #define REG_CTRL_SLEEPM BIT(16) |
| 38 | #define REG_CTRL_TX_BITSTUFF_ENN_H BIT(17) |
| 39 | #define REG_CTRL_TX_BITSTUFF_ENN BIT(18) |
| 40 | #define REG_CTRL_COMMON_ON BIT(19) |
| 41 | #define REG_CTRL_REF_CLK_SEL_MASK GENMASK(21, 20) |
| 42 | #define REG_CTRL_REF_CLK_SEL_SHIFT 20 |
| 43 | #define REG_CTRL_FSEL_MASK GENMASK(24, 22) |
| 44 | #define REG_CTRL_FSEL_SHIFT 22 |
| 45 | #define REG_CTRL_PORT_RESET BIT(25) |
| 46 | #define REG_CTRL_THREAD_ID_MASK GENMASK(31, 26) |
| 47 | |
| 48 | /* bits [31:26], [24:21] and [15:3] seem to be read-only */ |
| 49 | #define REG_ADP_BC 0x0c |
| 50 | #define REG_ADP_BC_VBUS_VLD_EXT_SEL BIT(0) |
| 51 | #define REG_ADP_BC_VBUS_VLD_EXT BIT(1) |
| 52 | #define REG_ADP_BC_OTG_DISABLE BIT(2) |
| 53 | #define REG_ADP_BC_ID_PULLUP BIT(3) |
| 54 | #define REG_ADP_BC_DRV_VBUS BIT(4) |
| 55 | #define REG_ADP_BC_ADP_PRB_EN BIT(5) |
| 56 | #define REG_ADP_BC_ADP_DISCHARGE BIT(6) |
| 57 | #define REG_ADP_BC_ADP_CHARGE BIT(7) |
| 58 | #define REG_ADP_BC_SESS_END BIT(8) |
| 59 | #define REG_ADP_BC_DEVICE_SESS_VLD BIT(9) |
| 60 | #define REG_ADP_BC_B_VALID BIT(10) |
| 61 | #define REG_ADP_BC_A_VALID BIT(11) |
| 62 | #define REG_ADP_BC_ID_DIG BIT(12) |
| 63 | #define REG_ADP_BC_VBUS_VALID BIT(13) |
| 64 | #define REG_ADP_BC_ADP_PROBE BIT(14) |
| 65 | #define REG_ADP_BC_ADP_SENSE BIT(15) |
| 66 | #define REG_ADP_BC_ACA_ENABLE BIT(16) |
| 67 | #define REG_ADP_BC_DCD_ENABLE BIT(17) |
| 68 | #define REG_ADP_BC_VDAT_DET_EN_B BIT(18) |
| 69 | #define REG_ADP_BC_VDAT_SRC_EN_B BIT(19) |
| 70 | #define REG_ADP_BC_CHARGE_SEL BIT(20) |
| 71 | #define REG_ADP_BC_CHARGE_DETECT BIT(21) |
| 72 | #define REG_ADP_BC_ACA_PIN_RANGE_C BIT(22) |
| 73 | #define REG_ADP_BC_ACA_PIN_RANGE_B BIT(23) |
| 74 | #define REG_ADP_BC_ACA_PIN_RANGE_A BIT(24) |
| 75 | #define REG_ADP_BC_ACA_PIN_GND BIT(25) |
| 76 | #define REG_ADP_BC_ACA_PIN_FLOAT BIT(26) |
| 77 | |
| 78 | #define RESET_COMPLETE_TIME 500 |
| 79 | #define ACA_ENABLE_COMPLETE_TIME 50 |
| 80 | |
| 81 | struct phy_meson_gxbb_usb2_priv { |
| 82 | struct regmap *regmap; |
| 83 | struct reset_ctl_bulk resets; |
Beniamino Galvani | df42f32 | 2019-08-18 15:42:54 +0200 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | static int phy_meson_gxbb_usb2_power_on(struct phy *phy) |
| 87 | { |
| 88 | struct udevice *dev = phy->dev; |
| 89 | struct phy_meson_gxbb_usb2_priv *priv = dev_get_priv(dev); |
| 90 | uint val; |
| 91 | |
Beniamino Galvani | df42f32 | 2019-08-18 15:42:54 +0200 | [diff] [blame] | 92 | regmap_update_bits(priv->regmap, REG_CONFIG, |
| 93 | REG_CONFIG_CLK_32k_ALTSEL, |
| 94 | REG_CONFIG_CLK_32k_ALTSEL); |
| 95 | regmap_update_bits(priv->regmap, REG_CTRL, |
| 96 | REG_CTRL_REF_CLK_SEL_MASK, |
| 97 | 0x2 << REG_CTRL_REF_CLK_SEL_SHIFT); |
| 98 | regmap_update_bits(priv->regmap, REG_CTRL, |
| 99 | REG_CTRL_FSEL_MASK, |
| 100 | 0x5 << REG_CTRL_FSEL_SHIFT); |
| 101 | |
| 102 | /* reset the PHY */ |
| 103 | regmap_update_bits(priv->regmap, REG_CTRL, |
| 104 | REG_CTRL_POWER_ON_RESET, |
| 105 | REG_CTRL_POWER_ON_RESET); |
| 106 | udelay(RESET_COMPLETE_TIME); |
| 107 | regmap_update_bits(priv->regmap, REG_CTRL, |
| 108 | REG_CTRL_POWER_ON_RESET, |
| 109 | 0); |
| 110 | udelay(RESET_COMPLETE_TIME); |
| 111 | |
| 112 | regmap_update_bits(priv->regmap, REG_CTRL, |
| 113 | REG_CTRL_SOF_TOGGLE_OUT, |
| 114 | REG_CTRL_SOF_TOGGLE_OUT); |
| 115 | |
| 116 | /* Set host mode */ |
| 117 | regmap_update_bits(priv->regmap, REG_ADP_BC, |
| 118 | REG_ADP_BC_ACA_ENABLE, |
| 119 | REG_ADP_BC_ACA_ENABLE); |
| 120 | udelay(ACA_ENABLE_COMPLETE_TIME); |
| 121 | |
| 122 | regmap_read(priv->regmap, REG_ADP_BC, &val); |
| 123 | if (val & REG_ADP_BC_ACA_PIN_FLOAT) { |
| 124 | pr_err("Error powering on GXBB USB PHY\n"); |
| 125 | return -EINVAL; |
| 126 | } |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
Beniamino Galvani | df42f32 | 2019-08-18 15:42:54 +0200 | [diff] [blame] | 131 | static struct phy_ops meson_gxbb_usb2_phy_ops = { |
| 132 | .power_on = phy_meson_gxbb_usb2_power_on, |
Beniamino Galvani | df42f32 | 2019-08-18 15:42:54 +0200 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | static int meson_gxbb_usb2_phy_probe(struct udevice *dev) |
| 136 | { |
| 137 | struct phy_meson_gxbb_usb2_priv *priv = dev_get_priv(dev); |
| 138 | struct clk clk_usb_general, clk_usb; |
| 139 | int ret; |
| 140 | |
| 141 | ret = regmap_init_mem(dev_ofnode(dev), &priv->regmap); |
| 142 | if (ret) |
| 143 | return ret; |
| 144 | |
| 145 | ret = clk_get_by_name(dev, "usb_general", &clk_usb_general); |
| 146 | if (ret) |
| 147 | return ret; |
| 148 | |
| 149 | ret = clk_enable(&clk_usb_general); |
| 150 | if (ret && ret != -ENOSYS && ret != -ENOTSUPP) { |
| 151 | pr_err("Failed to enable PHY general clock\n"); |
| 152 | return ret; |
| 153 | } |
| 154 | |
| 155 | ret = clk_get_by_name(dev, "usb", &clk_usb); |
| 156 | if (ret) |
| 157 | return ret; |
| 158 | |
| 159 | ret = clk_enable(&clk_usb); |
| 160 | if (ret && ret != -ENOSYS && ret != -ENOTSUPP) { |
| 161 | pr_err("Failed to enable PHY clock\n"); |
| 162 | return ret; |
| 163 | } |
| 164 | |
Beniamino Galvani | df42f32 | 2019-08-18 15:42:54 +0200 | [diff] [blame] | 165 | ret = reset_get_bulk(dev, &priv->resets); |
| 166 | if (!ret) { |
| 167 | ret = reset_deassert_bulk(&priv->resets); |
| 168 | if (ret) { |
| 169 | pr_err("Failed to deassert reset\n"); |
| 170 | return ret; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static int meson_gxbb_usb2_phy_remove(struct udevice *dev) |
| 178 | { |
| 179 | struct phy_meson_gxbb_usb2_priv *priv = dev_get_priv(dev); |
| 180 | |
| 181 | return reset_release_bulk(&priv->resets); |
| 182 | } |
| 183 | |
| 184 | static const struct udevice_id meson_gxbb_usb2_phy_ids[] = { |
| 185 | { .compatible = "amlogic,meson8-usb2-phy" }, |
| 186 | { .compatible = "amlogic,meson8b-usb2-phy" }, |
| 187 | { .compatible = "amlogic,meson-gxbb-usb2-phy" }, |
| 188 | { } |
| 189 | }; |
| 190 | |
| 191 | U_BOOT_DRIVER(meson_gxbb_usb2_phy) = { |
| 192 | .name = "meson_gxbb_usb2_phy", |
| 193 | .id = UCLASS_PHY, |
| 194 | .of_match = meson_gxbb_usb2_phy_ids, |
| 195 | .probe = meson_gxbb_usb2_phy_probe, |
| 196 | .remove = meson_gxbb_usb2_phy_remove, |
| 197 | .ops = &meson_gxbb_usb2_phy_ops, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 198 | .priv_auto = sizeof(struct phy_meson_gxbb_usb2_priv), |
Beniamino Galvani | df42f32 | 2019-08-18 15:42:54 +0200 | [diff] [blame] | 199 | }; |