Jean-Jacques Hiblot | 668257e | 2018-11-29 10:57:39 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * OMAP USB2 PHY LAYER |
| 4 | * |
| 5 | * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com |
| 6 | * Written by Jean-Jacques Hiblot <jjhiblot@ti.com> |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <asm/io.h> |
| 11 | #include <dm.h> |
| 12 | #include <errno.h> |
| 13 | #include <generic-phy.h> |
| 14 | #include <regmap.h> |
| 15 | #include <syscon.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 16 | #include <linux/bitops.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 17 | #include <linux/err.h> |
Jean-Jacques Hiblot | 668257e | 2018-11-29 10:57:39 +0100 | [diff] [blame] | 18 | |
| 19 | #define OMAP_USB2_CALIBRATE_FALSE_DISCONNECT BIT(0) |
Bin Liu | 5b9ee0f | 2020-06-03 14:46:22 +0300 | [diff] [blame] | 20 | #define OMAP_USB2_DISABLE_CHG_DET BIT(1) |
Jean-Jacques Hiblot | 668257e | 2018-11-29 10:57:39 +0100 | [diff] [blame] | 21 | |
| 22 | #define OMAP_DEV_PHY_PD BIT(0) |
| 23 | #define OMAP_USB2_PHY_PD BIT(28) |
| 24 | |
Jean-Jacques Hiblot | 512a84c | 2018-12-04 11:30:49 +0100 | [diff] [blame] | 25 | #define AM437X_USB2_PHY_PD BIT(0) |
| 26 | #define AM437X_USB2_OTG_PD BIT(1) |
| 27 | #define AM437X_USB2_OTGVDET_EN BIT(19) |
| 28 | #define AM437X_USB2_OTGSESSEND_EN BIT(20) |
| 29 | |
Jean-Jacques Hiblot | 668257e | 2018-11-29 10:57:39 +0100 | [diff] [blame] | 30 | #define USB2PHY_DISCON_BYP_LATCH BIT(31) |
| 31 | #define USB2PHY_ANA_CONFIG1 (0x4c) |
| 32 | |
Vignesh Raghavendra | 779f40b | 2019-12-09 10:37:31 +0530 | [diff] [blame] | 33 | #define AM654_USB2_OTG_PD BIT(8) |
| 34 | #define AM654_USB2_VBUS_DET_EN BIT(5) |
| 35 | #define AM654_USB2_VBUSVALID_DET_EN BIT(4) |
| 36 | |
Bin Liu | 5b9ee0f | 2020-06-03 14:46:22 +0300 | [diff] [blame] | 37 | #define USB2PHY_CHRG_DET 0x14 |
| 38 | #define USB2PHY_USE_CHG_DET_REG BIT(29) |
| 39 | #define USB2PHY_DIS_CHG_DET BIT(28) |
| 40 | |
Jean-Jacques Hiblot | 668257e | 2018-11-29 10:57:39 +0100 | [diff] [blame] | 41 | DECLARE_GLOBAL_DATA_PTR; |
| 42 | |
| 43 | struct omap_usb2_phy { |
| 44 | struct regmap *pwr_regmap; |
| 45 | ulong flags; |
| 46 | void *phy_base; |
| 47 | u32 pwr_reg_offset; |
| 48 | }; |
| 49 | |
| 50 | struct usb_phy_data { |
| 51 | const char *label; |
| 52 | u8 flags; |
| 53 | u32 mask; |
| 54 | u32 power_on; |
| 55 | u32 power_off; |
| 56 | }; |
| 57 | |
| 58 | static const struct usb_phy_data omap5_usb2_data = { |
| 59 | .label = "omap5_usb2", |
| 60 | .flags = 0, |
| 61 | .mask = OMAP_DEV_PHY_PD, |
| 62 | .power_off = OMAP_DEV_PHY_PD, |
| 63 | }; |
| 64 | |
| 65 | static const struct usb_phy_data dra7x_usb2_data = { |
| 66 | .label = "dra7x_usb2", |
| 67 | .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT, |
| 68 | .mask = OMAP_DEV_PHY_PD, |
| 69 | .power_off = OMAP_DEV_PHY_PD, |
| 70 | }; |
| 71 | |
| 72 | static const struct usb_phy_data dra7x_usb2_phy2_data = { |
| 73 | .label = "dra7x_usb2_phy2", |
| 74 | .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT, |
| 75 | .mask = OMAP_USB2_PHY_PD, |
| 76 | .power_off = OMAP_USB2_PHY_PD, |
| 77 | }; |
| 78 | |
Jean-Jacques Hiblot | 512a84c | 2018-12-04 11:30:49 +0100 | [diff] [blame] | 79 | static const struct usb_phy_data am437x_usb2_data = { |
| 80 | .label = "am437x_usb2", |
| 81 | .flags = 0, |
| 82 | .mask = AM437X_USB2_PHY_PD | AM437X_USB2_OTG_PD | |
| 83 | AM437X_USB2_OTGVDET_EN | AM437X_USB2_OTGSESSEND_EN, |
| 84 | .power_on = AM437X_USB2_OTGVDET_EN | AM437X_USB2_OTGSESSEND_EN, |
| 85 | .power_off = AM437X_USB2_PHY_PD | AM437X_USB2_OTG_PD, |
| 86 | }; |
| 87 | |
Vignesh Raghavendra | 779f40b | 2019-12-09 10:37:31 +0530 | [diff] [blame] | 88 | static const struct usb_phy_data am654_usb2_data = { |
| 89 | .label = "am654_usb2", |
| 90 | .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT, |
| 91 | .mask = AM654_USB2_OTG_PD | AM654_USB2_VBUS_DET_EN | |
| 92 | AM654_USB2_VBUSVALID_DET_EN, |
| 93 | .power_on = AM654_USB2_VBUS_DET_EN | AM654_USB2_VBUSVALID_DET_EN, |
| 94 | .power_off = AM654_USB2_OTG_PD, |
| 95 | }; |
| 96 | |
Jean-Jacques Hiblot | 668257e | 2018-11-29 10:57:39 +0100 | [diff] [blame] | 97 | static const struct udevice_id omap_usb2_id_table[] = { |
| 98 | { |
| 99 | .compatible = "ti,omap5-usb2", |
| 100 | .data = (ulong)&omap5_usb2_data, |
| 101 | }, |
| 102 | { |
| 103 | .compatible = "ti,dra7x-usb2", |
| 104 | .data = (ulong)&dra7x_usb2_data, |
| 105 | }, |
| 106 | { |
| 107 | .compatible = "ti,dra7x-usb2-phy2", |
| 108 | .data = (ulong)&dra7x_usb2_phy2_data, |
| 109 | }, |
Jean-Jacques Hiblot | 512a84c | 2018-12-04 11:30:49 +0100 | [diff] [blame] | 110 | { |
| 111 | .compatible = "ti,am437x-usb2", |
| 112 | .data = (ulong)&am437x_usb2_data, |
| 113 | }, |
Vignesh Raghavendra | 779f40b | 2019-12-09 10:37:31 +0530 | [diff] [blame] | 114 | { |
| 115 | .compatible = "ti,am654-usb2", |
| 116 | .data = (ulong)&am654_usb2_data, |
| 117 | }, |
Jean-Jacques Hiblot | 668257e | 2018-11-29 10:57:39 +0100 | [diff] [blame] | 118 | {}, |
| 119 | }; |
| 120 | |
| 121 | static int omap_usb_phy_power(struct phy *usb_phy, bool on) |
| 122 | { |
| 123 | struct udevice *dev = usb_phy->dev; |
| 124 | const struct usb_phy_data *data; |
| 125 | const struct omap_usb2_phy *phy = dev_get_priv(dev); |
| 126 | u32 val; |
| 127 | int rc; |
| 128 | |
| 129 | data = (const struct usb_phy_data *)dev_get_driver_data(dev); |
| 130 | if (!data) |
| 131 | return -EINVAL; |
| 132 | |
| 133 | rc = regmap_read(phy->pwr_regmap, phy->pwr_reg_offset, &val); |
| 134 | if (rc) |
| 135 | return rc; |
| 136 | val &= ~data->mask; |
| 137 | if (on) |
| 138 | val |= data->power_on; |
| 139 | else |
| 140 | val |= data->power_off; |
| 141 | rc = regmap_write(phy->pwr_regmap, phy->pwr_reg_offset, val); |
| 142 | if (rc) |
| 143 | return rc; |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static int omap_usb2_phy_init(struct phy *usb_phy) |
| 149 | { |
| 150 | struct udevice *dev = usb_phy->dev; |
| 151 | struct omap_usb2_phy *priv = dev_get_priv(dev); |
| 152 | u32 val; |
| 153 | |
| 154 | if (priv->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) { |
| 155 | /* |
| 156 | * |
| 157 | * Reduce the sensitivity of internal PHY by enabling the |
| 158 | * DISCON_BYP_LATCH of the USB2PHY_ANA_CONFIG1 register. This |
| 159 | * resolves issues with certain devices which can otherwise |
| 160 | * be prone to false disconnects. |
| 161 | * |
| 162 | */ |
| 163 | val = readl(priv->phy_base + USB2PHY_ANA_CONFIG1); |
| 164 | val |= USB2PHY_DISCON_BYP_LATCH; |
| 165 | writel(val, priv->phy_base + USB2PHY_ANA_CONFIG1); |
| 166 | } |
| 167 | |
Bin Liu | 5b9ee0f | 2020-06-03 14:46:22 +0300 | [diff] [blame] | 168 | if (priv->flags & OMAP_USB2_DISABLE_CHG_DET) { |
| 169 | val = readl(priv->phy_base + USB2PHY_CHRG_DET); |
| 170 | val |= USB2PHY_USE_CHG_DET_REG | USB2PHY_DIS_CHG_DET; |
| 171 | writel(val, priv->phy_base + USB2PHY_CHRG_DET); |
| 172 | } |
| 173 | |
Jean-Jacques Hiblot | 668257e | 2018-11-29 10:57:39 +0100 | [diff] [blame] | 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static int omap_usb2_phy_power_on(struct phy *usb_phy) |
| 178 | { |
| 179 | return omap_usb_phy_power(usb_phy, true); |
| 180 | } |
| 181 | |
| 182 | static int omap_usb2_phy_power_off(struct phy *usb_phy) |
| 183 | { |
| 184 | return omap_usb_phy_power(usb_phy, false); |
| 185 | } |
| 186 | |
| 187 | static int omap_usb2_phy_exit(struct phy *usb_phy) |
| 188 | { |
| 189 | return omap_usb_phy_power(usb_phy, false); |
| 190 | } |
| 191 | |
| 192 | struct phy_ops omap_usb2_phy_ops = { |
| 193 | .init = omap_usb2_phy_init, |
| 194 | .power_on = omap_usb2_phy_power_on, |
| 195 | .power_off = omap_usb2_phy_power_off, |
| 196 | .exit = omap_usb2_phy_exit, |
| 197 | }; |
| 198 | |
| 199 | int omap_usb2_phy_probe(struct udevice *dev) |
| 200 | { |
| 201 | int rc; |
| 202 | struct regmap *regmap; |
| 203 | struct omap_usb2_phy *priv = dev_get_priv(dev); |
| 204 | const struct usb_phy_data *data; |
| 205 | u32 tmp[2]; |
| 206 | |
| 207 | data = (const struct usb_phy_data *)dev_get_driver_data(dev); |
| 208 | if (!data) |
| 209 | return -EINVAL; |
| 210 | |
Bin Liu | 5b9ee0f | 2020-06-03 14:46:22 +0300 | [diff] [blame] | 211 | priv->phy_base = dev_read_addr_ptr(dev); |
Jean-Jacques Hiblot | 668257e | 2018-11-29 10:57:39 +0100 | [diff] [blame] | 212 | |
Bin Liu | 5b9ee0f | 2020-06-03 14:46:22 +0300 | [diff] [blame] | 213 | if (!priv->phy_base) |
| 214 | return -EINVAL; |
| 215 | |
| 216 | if (data->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) |
Jean-Jacques Hiblot | 668257e | 2018-11-29 10:57:39 +0100 | [diff] [blame] | 217 | priv->flags |= OMAP_USB2_CALIBRATE_FALSE_DISCONNECT; |
Bin Liu | 5b9ee0f | 2020-06-03 14:46:22 +0300 | [diff] [blame] | 218 | |
| 219 | /* |
| 220 | * AM654x PG1.0 has a silicon bug that D+ is pulled high after |
| 221 | * POR, which could cause enumeration failure with some USB hubs. |
| 222 | * Disabling the USB2_PHY Charger Detect function will put D+ |
| 223 | * into the normal state. |
| 224 | * |
| 225 | * Using property "ti,dis-chg-det-quirk" in the DT usb2-phy node |
| 226 | * to enable this workaround for AM654x PG1.0. |
| 227 | */ |
| 228 | if (dev_read_bool(dev, "ti,dis-chg-det-quirk")) |
| 229 | priv->flags |= OMAP_USB2_DISABLE_CHG_DET; |
Jean-Jacques Hiblot | 668257e | 2018-11-29 10:57:39 +0100 | [diff] [blame] | 230 | |
| 231 | regmap = syscon_regmap_lookup_by_phandle(dev, "syscon-phy-power"); |
Jean-Jacques Hiblot | 512a84c | 2018-12-04 11:30:49 +0100 | [diff] [blame] | 232 | if (!IS_ERR(regmap)) { |
| 233 | priv->pwr_regmap = regmap; |
| 234 | rc = dev_read_u32_array(dev, "syscon-phy-power", tmp, 2); |
| 235 | if (rc) { |
| 236 | printf("couldn't get power reg. offset (err %d)\n", rc); |
| 237 | return rc; |
| 238 | } |
| 239 | priv->pwr_reg_offset = tmp[1]; |
| 240 | return 0; |
Jean-Jacques Hiblot | 668257e | 2018-11-29 10:57:39 +0100 | [diff] [blame] | 241 | } |
Jean-Jacques Hiblot | 512a84c | 2018-12-04 11:30:49 +0100 | [diff] [blame] | 242 | regmap = syscon_regmap_lookup_by_phandle(dev, "ctrl-module"); |
| 243 | if (!IS_ERR(regmap)) { |
| 244 | priv->pwr_regmap = regmap; |
| 245 | priv->pwr_reg_offset = 0; |
| 246 | return 0; |
Jean-Jacques Hiblot | 668257e | 2018-11-29 10:57:39 +0100 | [diff] [blame] | 247 | } |
Jean-Jacques Hiblot | 668257e | 2018-11-29 10:57:39 +0100 | [diff] [blame] | 248 | |
Jean-Jacques Hiblot | 512a84c | 2018-12-04 11:30:49 +0100 | [diff] [blame] | 249 | printf("can't get regmap (err %ld)\n", PTR_ERR(regmap)); |
| 250 | return PTR_ERR(regmap); |
Jean-Jacques Hiblot | 668257e | 2018-11-29 10:57:39 +0100 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | U_BOOT_DRIVER(omap_usb2_phy) = { |
| 254 | .name = "omap_usb2_phy", |
| 255 | .id = UCLASS_PHY, |
| 256 | .of_match = omap_usb2_id_table, |
| 257 | .probe = omap_usb2_phy_probe, |
| 258 | .ops = &omap_usb2_phy_ops, |
| 259 | .priv_auto_alloc_size = sizeof(struct omap_usb2_phy), |
| 260 | }; |