Jagan Teki | 70e882f | 2020-05-26 11:33:44 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Rockchip USB2.0 PHY with Innosilicon IP block driver |
| 4 | * |
| 5 | * Copyright (C) 2016 Fuzhou Rockchip Electronics Co., Ltd |
| 6 | * Copyright (C) 2020 Amarula Solutions(India) |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <clk.h> |
| 11 | #include <dm.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Jagan Teki | 70e882f | 2020-05-26 11:33:44 +0800 | [diff] [blame] | 13 | #include <dm/device_compat.h> |
| 14 | #include <dm/lists.h> |
| 15 | #include <generic-phy.h> |
| 16 | #include <reset.h> |
| 17 | #include <syscon.h> |
| 18 | #include <asm/gpio.h> |
| 19 | #include <asm/io.h> |
| 20 | #include <linux/iopoll.h> |
| 21 | #include <asm/arch-rockchip/clock.h> |
| 22 | |
| 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
| 25 | #define usleep_range(a, b) udelay((b)) |
| 26 | #define BIT_WRITEABLE_SHIFT 16 |
| 27 | |
| 28 | enum rockchip_usb2phy_port_id { |
| 29 | USB2PHY_PORT_OTG, |
| 30 | USB2PHY_PORT_HOST, |
| 31 | USB2PHY_NUM_PORTS, |
| 32 | }; |
| 33 | |
| 34 | struct usb2phy_reg { |
| 35 | unsigned int offset; |
| 36 | unsigned int bitend; |
| 37 | unsigned int bitstart; |
| 38 | unsigned int disable; |
| 39 | unsigned int enable; |
| 40 | }; |
| 41 | |
| 42 | struct rockchip_usb2phy_port_cfg { |
| 43 | struct usb2phy_reg phy_sus; |
| 44 | struct usb2phy_reg bvalid_det_en; |
| 45 | struct usb2phy_reg bvalid_det_st; |
| 46 | struct usb2phy_reg bvalid_det_clr; |
| 47 | struct usb2phy_reg ls_det_en; |
| 48 | struct usb2phy_reg ls_det_st; |
| 49 | struct usb2phy_reg ls_det_clr; |
| 50 | struct usb2phy_reg utmi_avalid; |
| 51 | struct usb2phy_reg utmi_bvalid; |
| 52 | struct usb2phy_reg utmi_ls; |
| 53 | struct usb2phy_reg utmi_hstdet; |
| 54 | }; |
| 55 | |
| 56 | struct rockchip_usb2phy_cfg { |
| 57 | unsigned int reg; |
| 58 | const struct rockchip_usb2phy_port_cfg port_cfgs[USB2PHY_NUM_PORTS]; |
| 59 | }; |
| 60 | |
| 61 | struct rockchip_usb2phy { |
| 62 | void *reg_base; |
| 63 | struct clk phyclk; |
| 64 | const struct rockchip_usb2phy_cfg *phy_cfg; |
| 65 | }; |
| 66 | |
| 67 | static inline int property_enable(void *reg_base, |
| 68 | const struct usb2phy_reg *reg, bool en) |
| 69 | { |
| 70 | unsigned int val, mask, tmp; |
| 71 | |
| 72 | tmp = en ? reg->enable : reg->disable; |
| 73 | mask = GENMASK(reg->bitend, reg->bitstart); |
| 74 | val = (tmp << reg->bitstart) | (mask << BIT_WRITEABLE_SHIFT); |
| 75 | |
| 76 | return writel(val, reg_base + reg->offset); |
| 77 | } |
| 78 | |
| 79 | static const |
| 80 | struct rockchip_usb2phy_port_cfg *us2phy_get_port(struct phy *phy) |
| 81 | { |
| 82 | struct udevice *parent = dev_get_parent(phy->dev); |
| 83 | struct rockchip_usb2phy *priv = dev_get_priv(parent); |
| 84 | const struct rockchip_usb2phy_cfg *phy_cfg = priv->phy_cfg; |
| 85 | |
| 86 | return &phy_cfg->port_cfgs[phy->id]; |
| 87 | } |
| 88 | |
| 89 | static int rockchip_usb2phy_power_on(struct phy *phy) |
| 90 | { |
| 91 | struct udevice *parent = dev_get_parent(phy->dev); |
| 92 | struct rockchip_usb2phy *priv = dev_get_priv(parent); |
| 93 | const struct rockchip_usb2phy_port_cfg *port_cfg = us2phy_get_port(phy); |
| 94 | |
| 95 | property_enable(priv->reg_base, &port_cfg->phy_sus, false); |
| 96 | |
| 97 | /* waiting for the utmi_clk to become stable */ |
| 98 | usleep_range(1500, 2000); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static int rockchip_usb2phy_power_off(struct phy *phy) |
| 104 | { |
| 105 | struct udevice *parent = dev_get_parent(phy->dev); |
| 106 | struct rockchip_usb2phy *priv = dev_get_priv(parent); |
| 107 | const struct rockchip_usb2phy_port_cfg *port_cfg = us2phy_get_port(phy); |
| 108 | |
| 109 | property_enable(priv->reg_base, &port_cfg->phy_sus, true); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static int rockchip_usb2phy_init(struct phy *phy) |
| 115 | { |
| 116 | struct udevice *parent = dev_get_parent(phy->dev); |
| 117 | struct rockchip_usb2phy *priv = dev_get_priv(parent); |
| 118 | const struct rockchip_usb2phy_port_cfg *port_cfg = us2phy_get_port(phy); |
| 119 | int ret; |
| 120 | |
| 121 | ret = clk_enable(&priv->phyclk); |
John Keeping | 86cd834 | 2022-12-06 12:48:55 +0000 | [diff] [blame] | 122 | if (ret && ret != -ENOSYS) { |
Jagan Teki | 70e882f | 2020-05-26 11:33:44 +0800 | [diff] [blame] | 123 | dev_err(phy->dev, "failed to enable phyclk (ret=%d)\n", ret); |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | if (phy->id == USB2PHY_PORT_OTG) { |
| 128 | property_enable(priv->reg_base, &port_cfg->bvalid_det_clr, true); |
| 129 | property_enable(priv->reg_base, &port_cfg->bvalid_det_en, true); |
| 130 | } else if (phy->id == USB2PHY_PORT_HOST) { |
| 131 | property_enable(priv->reg_base, &port_cfg->bvalid_det_clr, true); |
| 132 | property_enable(priv->reg_base, &port_cfg->bvalid_det_en, true); |
| 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static int rockchip_usb2phy_exit(struct phy *phy) |
| 139 | { |
| 140 | struct udevice *parent = dev_get_parent(phy->dev); |
| 141 | struct rockchip_usb2phy *priv = dev_get_priv(parent); |
| 142 | |
| 143 | clk_disable(&priv->phyclk); |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static int rockchip_usb2phy_of_xlate(struct phy *phy, |
| 149 | struct ofnode_phandle_args *args) |
| 150 | { |
| 151 | const char *name = phy->dev->name; |
| 152 | |
| 153 | if (!strcasecmp(name, "host-port")) |
| 154 | phy->id = USB2PHY_PORT_HOST; |
| 155 | else if (!strcasecmp(name, "otg-port")) |
| 156 | phy->id = USB2PHY_PORT_OTG; |
| 157 | else |
| 158 | dev_err(phy->dev, "improper %s device\n", name); |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | static struct phy_ops rockchip_usb2phy_ops = { |
| 164 | .init = rockchip_usb2phy_init, |
| 165 | .exit = rockchip_usb2phy_exit, |
| 166 | .power_on = rockchip_usb2phy_power_on, |
| 167 | .power_off = rockchip_usb2phy_power_off, |
| 168 | .of_xlate = rockchip_usb2phy_of_xlate, |
| 169 | }; |
| 170 | |
| 171 | static int rockchip_usb2phy_probe(struct udevice *dev) |
| 172 | { |
| 173 | struct rockchip_usb2phy *priv = dev_get_priv(dev); |
| 174 | const struct rockchip_usb2phy_cfg *phy_cfgs; |
| 175 | unsigned int reg; |
| 176 | int index, ret; |
| 177 | |
| 178 | priv->reg_base = syscon_get_first_range(ROCKCHIP_SYSCON_GRF); |
| 179 | if (IS_ERR(priv->reg_base)) |
| 180 | return PTR_ERR(priv->reg_base); |
| 181 | |
Jagan Teki | 1b79951 | 2023-02-17 17:28:39 +0530 | [diff] [blame] | 182 | ret = ofnode_read_u32_index(dev_ofnode(dev), "reg", 0, ®); |
Jagan Teki | 70e882f | 2020-05-26 11:33:44 +0800 | [diff] [blame] | 183 | if (ret) { |
| 184 | dev_err(dev, "failed to read reg property (ret = %d)\n", ret); |
| 185 | return ret; |
| 186 | } |
| 187 | |
Jagan Teki | 1b79951 | 2023-02-17 17:28:39 +0530 | [diff] [blame] | 188 | /* support address_cells=2 */ |
| 189 | if (reg == 0) { |
| 190 | if (ofnode_read_u32_index(dev_ofnode(dev), "reg", 1, ®)) { |
| 191 | dev_err(dev, "%s must have reg[1]\n", |
| 192 | ofnode_get_name(dev_ofnode(dev))); |
| 193 | return -EINVAL; |
| 194 | } |
| 195 | } |
| 196 | |
Jagan Teki | 70e882f | 2020-05-26 11:33:44 +0800 | [diff] [blame] | 197 | phy_cfgs = (const struct rockchip_usb2phy_cfg *) |
| 198 | dev_get_driver_data(dev); |
| 199 | if (!phy_cfgs) |
| 200 | return -EINVAL; |
| 201 | |
| 202 | /* find out a proper config which can be matched with dt. */ |
| 203 | index = 0; |
| 204 | while (phy_cfgs[index].reg) { |
| 205 | if (phy_cfgs[index].reg == reg) { |
| 206 | priv->phy_cfg = &phy_cfgs[index]; |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | ++index; |
| 211 | } |
| 212 | |
| 213 | if (!priv->phy_cfg) { |
| 214 | dev_err(dev, "failed find proper phy-cfg\n"); |
| 215 | return -EINVAL; |
| 216 | } |
| 217 | |
| 218 | ret = clk_get_by_name(dev, "phyclk", &priv->phyclk); |
| 219 | if (ret) { |
| 220 | dev_err(dev, "failed to get the phyclk (ret=%d)\n", ret); |
| 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | static int rockchip_usb2phy_bind(struct udevice *dev) |
| 228 | { |
| 229 | struct udevice *usb2phy_dev; |
| 230 | ofnode node; |
| 231 | const char *name; |
| 232 | int ret = 0; |
| 233 | |
| 234 | dev_for_each_subnode(node, dev) { |
| 235 | if (!ofnode_valid(node)) { |
| 236 | dev_info(dev, "subnode %s not found\n", dev->name); |
| 237 | return -ENXIO; |
| 238 | } |
| 239 | |
| 240 | name = ofnode_get_name(node); |
| 241 | dev_dbg(dev, "subnode %s\n", name); |
| 242 | |
| 243 | ret = device_bind_driver_to_node(dev, "rockchip_usb2phy_port", |
| 244 | name, node, &usb2phy_dev); |
| 245 | if (ret) { |
| 246 | dev_err(dev, |
| 247 | "'%s' cannot bind 'rockchip_usb2phy_port'\n", name); |
| 248 | return ret; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return ret; |
| 253 | } |
| 254 | |
| 255 | static const struct rockchip_usb2phy_cfg rk3399_usb2phy_cfgs[] = { |
| 256 | { |
| 257 | .reg = 0xe450, |
| 258 | .port_cfgs = { |
| 259 | [USB2PHY_PORT_OTG] = { |
| 260 | .phy_sus = { 0xe454, 1, 0, 2, 1 }, |
| 261 | .bvalid_det_en = { 0xe3c0, 3, 3, 0, 1 }, |
| 262 | .bvalid_det_st = { 0xe3e0, 3, 3, 0, 1 }, |
| 263 | .bvalid_det_clr = { 0xe3d0, 3, 3, 0, 1 }, |
| 264 | .utmi_avalid = { 0xe2ac, 7, 7, 0, 1 }, |
| 265 | .utmi_bvalid = { 0xe2ac, 12, 12, 0, 1 }, |
| 266 | }, |
| 267 | [USB2PHY_PORT_HOST] = { |
| 268 | .phy_sus = { 0xe458, 1, 0, 0x2, 0x1 }, |
| 269 | .ls_det_en = { 0xe3c0, 6, 6, 0, 1 }, |
| 270 | .ls_det_st = { 0xe3e0, 6, 6, 0, 1 }, |
| 271 | .ls_det_clr = { 0xe3d0, 6, 6, 0, 1 }, |
| 272 | .utmi_ls = { 0xe2ac, 22, 21, 0, 1 }, |
| 273 | .utmi_hstdet = { 0xe2ac, 23, 23, 0, 1 } |
| 274 | } |
| 275 | }, |
| 276 | }, |
| 277 | { |
| 278 | .reg = 0xe460, |
| 279 | .port_cfgs = { |
| 280 | [USB2PHY_PORT_OTG] = { |
| 281 | .phy_sus = { 0xe464, 1, 0, 2, 1 }, |
| 282 | .bvalid_det_en = { 0xe3c0, 8, 8, 0, 1 }, |
| 283 | .bvalid_det_st = { 0xe3e0, 8, 8, 0, 1 }, |
| 284 | .bvalid_det_clr = { 0xe3d0, 8, 8, 0, 1 }, |
| 285 | .utmi_avalid = { 0xe2ac, 10, 10, 0, 1 }, |
| 286 | .utmi_bvalid = { 0xe2ac, 16, 16, 0, 1 }, |
| 287 | }, |
| 288 | [USB2PHY_PORT_HOST] = { |
| 289 | .phy_sus = { 0xe468, 1, 0, 0x2, 0x1 }, |
| 290 | .ls_det_en = { 0xe3c0, 11, 11, 0, 1 }, |
| 291 | .ls_det_st = { 0xe3e0, 11, 11, 0, 1 }, |
| 292 | .ls_det_clr = { 0xe3d0, 11, 11, 0, 1 }, |
| 293 | .utmi_ls = { 0xe2ac, 26, 25, 0, 1 }, |
| 294 | .utmi_hstdet = { 0xe2ac, 27, 27, 0, 1 } |
| 295 | } |
| 296 | }, |
| 297 | }, |
| 298 | { /* sentinel */ } |
| 299 | }; |
| 300 | |
Manoj Sai | 3a11167 | 2023-02-17 17:28:40 +0530 | [diff] [blame] | 301 | static const struct rockchip_usb2phy_cfg rk3568_phy_cfgs[] = { |
| 302 | { |
| 303 | .reg = 0xfe8a0000, |
| 304 | .port_cfgs = { |
| 305 | [USB2PHY_PORT_OTG] = { |
| 306 | .phy_sus = { 0x0000, 8, 0, 0x052, 0x1d1 }, |
| 307 | .bvalid_det_en = { 0x0080, 2, 2, 0, 1 }, |
| 308 | .bvalid_det_st = { 0x0084, 2, 2, 0, 1 }, |
| 309 | .bvalid_det_clr = { 0x0088, 2, 2, 0, 1 }, |
| 310 | .ls_det_en = { 0x0080, 0, 0, 0, 1 }, |
| 311 | .ls_det_st = { 0x0084, 0, 0, 0, 1 }, |
| 312 | .ls_det_clr = { 0x0088, 0, 0, 0, 1 }, |
| 313 | .utmi_avalid = { 0x00c0, 10, 10, 0, 1 }, |
| 314 | .utmi_bvalid = { 0x00c0, 9, 9, 0, 1 }, |
| 315 | .utmi_ls = { 0x00c0, 5, 4, 0, 1 }, |
| 316 | }, |
| 317 | [USB2PHY_PORT_HOST] = { |
| 318 | .phy_sus = { 0x0004, 8, 0, 0x1d2, 0x1d1 }, |
| 319 | .ls_det_en = { 0x0080, 1, 1, 0, 1 }, |
| 320 | .ls_det_st = { 0x0084, 1, 1, 0, 1 }, |
| 321 | .ls_det_clr = { 0x0088, 1, 1, 0, 1 }, |
| 322 | .utmi_ls = { 0x00c0, 17, 16, 0, 1 }, |
| 323 | .utmi_hstdet = { 0x00c0, 19, 19, 0, 1 } |
| 324 | } |
| 325 | }, |
| 326 | }, |
| 327 | { |
| 328 | .reg = 0xfe8b0000, |
| 329 | .port_cfgs = { |
| 330 | [USB2PHY_PORT_OTG] = { |
| 331 | .phy_sus = { 0x0000, 8, 0, 0x1d2, 0x1d1 }, |
| 332 | .ls_det_en = { 0x0080, 0, 0, 0, 1 }, |
| 333 | .ls_det_st = { 0x0084, 0, 0, 0, 1 }, |
| 334 | .ls_det_clr = { 0x0088, 0, 0, 0, 1 }, |
| 335 | .utmi_ls = { 0x00c0, 5, 4, 0, 1 }, |
| 336 | .utmi_hstdet = { 0x00c0, 7, 7, 0, 1 } |
| 337 | }, |
| 338 | [USB2PHY_PORT_HOST] = { |
| 339 | .phy_sus = { 0x0004, 8, 0, 0x1d2, 0x1d1 }, |
| 340 | .ls_det_en = { 0x0080, 1, 1, 0, 1 }, |
| 341 | .ls_det_st = { 0x0084, 1, 1, 0, 1 }, |
| 342 | .ls_det_clr = { 0x0088, 1, 1, 0, 1 }, |
| 343 | .utmi_ls = { 0x00c0, 17, 16, 0, 1 }, |
| 344 | .utmi_hstdet = { 0x00c0, 19, 19, 0, 1 } |
| 345 | } |
| 346 | }, |
| 347 | }, |
| 348 | { /* sentinel */ } |
| 349 | }; |
| 350 | |
Jagan Teki | 70e882f | 2020-05-26 11:33:44 +0800 | [diff] [blame] | 351 | static const struct udevice_id rockchip_usb2phy_ids[] = { |
| 352 | { |
| 353 | .compatible = "rockchip,rk3399-usb2phy", |
| 354 | .data = (ulong)&rk3399_usb2phy_cfgs, |
| 355 | }, |
Manoj Sai | 3a11167 | 2023-02-17 17:28:40 +0530 | [diff] [blame] | 356 | { |
| 357 | .compatible = "rockchip,rk3568-usb2phy", |
| 358 | .data = (ulong)&rk3568_phy_cfgs, |
| 359 | }, |
Jagan Teki | 70e882f | 2020-05-26 11:33:44 +0800 | [diff] [blame] | 360 | { /* sentinel */ } |
| 361 | }; |
| 362 | |
| 363 | U_BOOT_DRIVER(rockchip_usb2phy_port) = { |
| 364 | .name = "rockchip_usb2phy_port", |
| 365 | .id = UCLASS_PHY, |
| 366 | .ops = &rockchip_usb2phy_ops, |
| 367 | }; |
| 368 | |
| 369 | U_BOOT_DRIVER(rockchip_usb2phy) = { |
| 370 | .name = "rockchip_usb2phy", |
| 371 | .id = UCLASS_PHY, |
| 372 | .of_match = rockchip_usb2phy_ids, |
| 373 | .probe = rockchip_usb2phy_probe, |
| 374 | .bind = rockchip_usb2phy_bind, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 375 | .priv_auto = sizeof(struct rockchip_usb2phy), |
Jagan Teki | 70e882f | 2020-05-26 11:33:44 +0800 | [diff] [blame] | 376 | }; |