Marek Vasut | 8ec3077 | 2023-03-19 18:09:43 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* Renesas Ethernet SERDES device driver |
| 3 | * |
| 4 | * Copyright (C) 2022 Renesas Electronics Corporation |
| 5 | */ |
| 6 | |
| 7 | #include <asm/io.h> |
| 8 | #include <clk-uclass.h> |
| 9 | #include <clk.h> |
Marek Vasut | 8ec3077 | 2023-03-19 18:09:43 +0100 | [diff] [blame] | 10 | #include <div64.h> |
| 11 | #include <dm.h> |
| 12 | #include <dm/device_compat.h> |
| 13 | #include <dm/lists.h> |
| 14 | #include <dm/of_access.h> |
| 15 | #include <generic-phy.h> |
| 16 | #include <linux/bitfield.h> |
| 17 | #include <linux/bitops.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/iopoll.h> |
| 20 | #include <log.h> |
| 21 | #include <reset.h> |
| 22 | #include <syscon.h> |
| 23 | |
| 24 | #define R8A779F0_ETH_SERDES_NUM 3 |
| 25 | #define R8A779F0_ETH_SERDES_OFFSET 0x0400 |
| 26 | #define R8A779F0_ETH_SERDES_BANK_SELECT 0x03fc |
| 27 | #define R8A779F0_ETH_SERDES_TIMEOUT_US 100000 |
| 28 | #define R8A779F0_ETH_SERDES_NUM_RETRY_LINKUP 3 |
| 29 | |
| 30 | struct r8a779f0_eth_serdes_drv_data; |
| 31 | struct r8a779f0_eth_serdes_channel { |
| 32 | struct r8a779f0_eth_serdes_drv_data *dd; |
| 33 | struct phy *phy; |
| 34 | void __iomem *addr; |
| 35 | phy_interface_t phy_interface; |
| 36 | int speed; |
| 37 | int index; |
| 38 | }; |
| 39 | |
| 40 | struct r8a779f0_eth_serdes_drv_data { |
| 41 | void __iomem *addr; |
| 42 | struct reset_ctl *reset; |
| 43 | struct r8a779f0_eth_serdes_channel channel[R8A779F0_ETH_SERDES_NUM]; |
| 44 | bool initialized; |
| 45 | }; |
| 46 | |
| 47 | /* |
| 48 | * The datasheet describes initialization procedure without any information |
| 49 | * about registers' name/bits. So, this is all black magic to initialize |
| 50 | * the hardware. |
| 51 | */ |
| 52 | static void r8a779f0_eth_serdes_write32(void __iomem *addr, u32 offs, u32 bank, u32 data) |
| 53 | { |
| 54 | writel(bank, addr + R8A779F0_ETH_SERDES_BANK_SELECT); |
| 55 | writel(data, addr + offs); |
| 56 | } |
| 57 | |
| 58 | static int |
| 59 | r8a779f0_eth_serdes_reg_wait(struct r8a779f0_eth_serdes_channel *channel, |
| 60 | u32 offs, u32 bank, u32 mask, u32 expected) |
| 61 | { |
| 62 | u32 val = 0; |
| 63 | int ret; |
| 64 | |
| 65 | writel(bank, channel->addr + R8A779F0_ETH_SERDES_BANK_SELECT); |
| 66 | |
| 67 | ret = readl_poll_timeout(channel->addr + offs, val, |
| 68 | (val & mask) == expected, |
| 69 | R8A779F0_ETH_SERDES_TIMEOUT_US); |
| 70 | if (ret) |
| 71 | dev_dbg(channel->phy->dev, |
| 72 | "%s: index %d, offs %x, bank %x, mask %x, expected %x\n", |
| 73 | __func__, channel->index, offs, bank, mask, expected); |
| 74 | |
| 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | static int |
| 79 | r8a779f0_eth_serdes_common_init_ram(struct r8a779f0_eth_serdes_drv_data *dd) |
| 80 | { |
| 81 | struct r8a779f0_eth_serdes_channel *channel; |
| 82 | int i, ret; |
| 83 | |
| 84 | for (i = 0; i < R8A779F0_ETH_SERDES_NUM; i++) { |
| 85 | channel = &dd->channel[i]; |
| 86 | ret = r8a779f0_eth_serdes_reg_wait(channel, 0x026c, 0x180, BIT(0), 0x01); |
| 87 | if (ret) |
| 88 | return ret; |
| 89 | } |
| 90 | |
| 91 | r8a779f0_eth_serdes_write32(dd->addr, 0x026c, 0x180, 0x03); |
| 92 | |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | static int |
| 97 | r8a779f0_eth_serdes_common_setting(struct r8a779f0_eth_serdes_channel *channel) |
| 98 | { |
| 99 | struct r8a779f0_eth_serdes_drv_data *dd = channel->dd; |
| 100 | |
| 101 | switch (channel->phy_interface) { |
| 102 | case PHY_INTERFACE_MODE_SGMII: |
| 103 | r8a779f0_eth_serdes_write32(dd->addr, 0x0244, 0x180, 0x0097); |
| 104 | r8a779f0_eth_serdes_write32(dd->addr, 0x01d0, 0x180, 0x0060); |
| 105 | r8a779f0_eth_serdes_write32(dd->addr, 0x01d8, 0x180, 0x2200); |
| 106 | r8a779f0_eth_serdes_write32(dd->addr, 0x01d4, 0x180, 0x0000); |
| 107 | r8a779f0_eth_serdes_write32(dd->addr, 0x01e0, 0x180, 0x003d); |
| 108 | return 0; |
| 109 | default: |
| 110 | return -EOPNOTSUPP; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | static int |
| 115 | r8a779f0_eth_serdes_chan_setting(struct r8a779f0_eth_serdes_channel *channel) |
| 116 | { |
| 117 | int ret; |
| 118 | |
| 119 | switch (channel->phy_interface) { |
| 120 | case PHY_INTERFACE_MODE_SGMII: |
| 121 | r8a779f0_eth_serdes_write32(channel->addr, 0x0000, 0x380, 0x2000); |
| 122 | r8a779f0_eth_serdes_write32(channel->addr, 0x01c0, 0x180, 0x0011); |
| 123 | r8a779f0_eth_serdes_write32(channel->addr, 0x0248, 0x180, 0x0540); |
| 124 | r8a779f0_eth_serdes_write32(channel->addr, 0x0258, 0x180, 0x0015); |
| 125 | r8a779f0_eth_serdes_write32(channel->addr, 0x0144, 0x180, 0x0100); |
| 126 | r8a779f0_eth_serdes_write32(channel->addr, 0x01a0, 0x180, 0x0000); |
| 127 | r8a779f0_eth_serdes_write32(channel->addr, 0x00d0, 0x180, 0x0002); |
| 128 | r8a779f0_eth_serdes_write32(channel->addr, 0x0150, 0x180, 0x0003); |
| 129 | r8a779f0_eth_serdes_write32(channel->addr, 0x00c8, 0x180, 0x0100); |
| 130 | r8a779f0_eth_serdes_write32(channel->addr, 0x0148, 0x180, 0x0100); |
| 131 | r8a779f0_eth_serdes_write32(channel->addr, 0x0174, 0x180, 0x0000); |
| 132 | r8a779f0_eth_serdes_write32(channel->addr, 0x0160, 0x180, 0x0007); |
| 133 | r8a779f0_eth_serdes_write32(channel->addr, 0x01ac, 0x180, 0x0000); |
| 134 | r8a779f0_eth_serdes_write32(channel->addr, 0x00c4, 0x180, 0x0310); |
| 135 | r8a779f0_eth_serdes_write32(channel->addr, 0x00c8, 0x180, 0x0101); |
| 136 | ret = r8a779f0_eth_serdes_reg_wait(channel, 0x00c8, 0x0180, BIT(0), 0); |
| 137 | if (ret) |
| 138 | return ret; |
| 139 | |
| 140 | r8a779f0_eth_serdes_write32(channel->addr, 0x0148, 0x180, 0x0101); |
| 141 | ret = r8a779f0_eth_serdes_reg_wait(channel, 0x0148, 0x0180, BIT(0), 0); |
| 142 | if (ret) |
| 143 | return ret; |
| 144 | |
| 145 | r8a779f0_eth_serdes_write32(channel->addr, 0x00c4, 0x180, 0x1310); |
| 146 | r8a779f0_eth_serdes_write32(channel->addr, 0x00d8, 0x180, 0x1800); |
| 147 | r8a779f0_eth_serdes_write32(channel->addr, 0x00dc, 0x180, 0x0000); |
| 148 | r8a779f0_eth_serdes_write32(channel->addr, 0x001c, 0x300, 0x0001); |
| 149 | r8a779f0_eth_serdes_write32(channel->addr, 0x0000, 0x380, 0x2100); |
| 150 | ret = r8a779f0_eth_serdes_reg_wait(channel, 0x0000, 0x0380, BIT(8), 0); |
| 151 | if (ret) |
| 152 | return ret; |
| 153 | |
| 154 | if (channel->speed == 1000) |
| 155 | r8a779f0_eth_serdes_write32(channel->addr, 0x0000, 0x1f00, 0x0140); |
| 156 | else if (channel->speed == 100) |
| 157 | r8a779f0_eth_serdes_write32(channel->addr, 0x0000, 0x1f00, 0x2100); |
| 158 | |
| 159 | /* For AN_ON */ |
| 160 | r8a779f0_eth_serdes_write32(channel->addr, 0x0004, 0x1f80, 0x0005); |
| 161 | r8a779f0_eth_serdes_write32(channel->addr, 0x0028, 0x1f80, 0x07a1); |
| 162 | r8a779f0_eth_serdes_write32(channel->addr, 0x0000, 0x1f80, 0x0208); |
| 163 | break; |
| 164 | default: |
| 165 | return -EOPNOTSUPP; |
| 166 | } |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | static int |
| 172 | r8a779f0_eth_serdes_chan_speed(struct r8a779f0_eth_serdes_channel *channel) |
| 173 | { |
| 174 | int ret; |
| 175 | |
| 176 | switch (channel->phy_interface) { |
| 177 | case PHY_INTERFACE_MODE_SGMII: |
| 178 | /* For AN_ON */ |
| 179 | if (channel->speed == 1000) |
| 180 | r8a779f0_eth_serdes_write32(channel->addr, 0x0000, 0x1f00, 0x1140); |
| 181 | else if (channel->speed == 100) |
| 182 | r8a779f0_eth_serdes_write32(channel->addr, 0x0000, 0x1f00, 0x3100); |
| 183 | ret = r8a779f0_eth_serdes_reg_wait(channel, 0x0008, 0x1f80, BIT(0), 1); |
| 184 | if (ret) |
| 185 | return ret; |
| 186 | r8a779f0_eth_serdes_write32(channel->addr, 0x0008, 0x1f80, 0x0000); |
| 187 | break; |
| 188 | default: |
| 189 | return -EOPNOTSUPP; |
| 190 | } |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static int r8a779f0_eth_serdes_monitor_linkup(struct r8a779f0_eth_serdes_channel *channel) |
| 196 | { |
| 197 | int i, ret; |
| 198 | |
| 199 | for (i = 0; i < R8A779F0_ETH_SERDES_NUM_RETRY_LINKUP; i++) { |
| 200 | ret = r8a779f0_eth_serdes_reg_wait(channel, 0x0004, 0x300, |
| 201 | BIT(2), BIT(2)); |
| 202 | if (!ret) |
| 203 | break; |
| 204 | |
| 205 | /* restart */ |
| 206 | r8a779f0_eth_serdes_write32(channel->addr, 0x0144, 0x180, 0x0100); |
| 207 | udelay(1); |
| 208 | r8a779f0_eth_serdes_write32(channel->addr, 0x0144, 0x180, 0x0000); |
| 209 | } |
| 210 | |
| 211 | return ret; |
| 212 | } |
| 213 | |
| 214 | static int r8a779f0_eth_serdes_hw_init(struct r8a779f0_eth_serdes_channel *channel) |
| 215 | { |
| 216 | struct r8a779f0_eth_serdes_drv_data *dd = channel->dd; |
| 217 | int i, ret; |
| 218 | |
| 219 | if (dd->initialized) |
| 220 | return 0; |
| 221 | |
| 222 | ret = r8a779f0_eth_serdes_common_init_ram(dd); |
| 223 | if (ret) |
| 224 | return ret; |
| 225 | |
| 226 | for (i = 0; i < R8A779F0_ETH_SERDES_NUM; i++) { |
| 227 | ret = r8a779f0_eth_serdes_reg_wait(&dd->channel[i], 0x0000, |
| 228 | 0x300, BIT(15), 0); |
| 229 | if (ret) |
| 230 | return ret; |
| 231 | } |
| 232 | |
| 233 | for (i = 0; i < R8A779F0_ETH_SERDES_NUM; i++) |
| 234 | r8a779f0_eth_serdes_write32(dd->channel[i].addr, 0x03d4, 0x380, 0x0443); |
| 235 | |
| 236 | ret = r8a779f0_eth_serdes_common_setting(channel); |
| 237 | if (ret) |
| 238 | return ret; |
| 239 | |
| 240 | for (i = 0; i < R8A779F0_ETH_SERDES_NUM; i++) |
| 241 | r8a779f0_eth_serdes_write32(dd->channel[i].addr, 0x03d0, 0x380, 0x0001); |
| 242 | |
| 243 | r8a779f0_eth_serdes_write32(dd->addr, 0x0000, 0x380, 0x8000); |
| 244 | |
| 245 | ret = r8a779f0_eth_serdes_common_init_ram(dd); |
| 246 | if (ret) |
| 247 | return ret; |
| 248 | |
| 249 | return r8a779f0_eth_serdes_reg_wait(&dd->channel[0], 0x0000, 0x380, BIT(15), 0); |
| 250 | } |
| 251 | |
| 252 | static int r8a779f0_eth_serdes_init(struct phy *p) |
| 253 | { |
| 254 | struct r8a779f0_eth_serdes_drv_data *dd = dev_get_priv(p->dev); |
| 255 | struct r8a779f0_eth_serdes_channel *channel = dd->channel + p->id; |
| 256 | int ret; |
| 257 | |
| 258 | ret = r8a779f0_eth_serdes_hw_init(channel); |
| 259 | if (!ret) |
| 260 | channel->dd->initialized = true; |
| 261 | |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | static int r8a779f0_eth_serdes_hw_init_late(struct r8a779f0_eth_serdes_channel *channel) |
| 266 | { |
| 267 | int ret; |
| 268 | |
| 269 | ret = r8a779f0_eth_serdes_chan_setting(channel); |
| 270 | if (ret) |
| 271 | return ret; |
| 272 | |
| 273 | ret = r8a779f0_eth_serdes_chan_speed(channel); |
| 274 | if (ret) |
| 275 | return ret; |
| 276 | |
| 277 | r8a779f0_eth_serdes_write32(channel->addr, 0x03c0, 0x380, 0x0000); |
| 278 | |
| 279 | r8a779f0_eth_serdes_write32(channel->addr, 0x03d0, 0x380, 0x0000); |
| 280 | |
| 281 | return r8a779f0_eth_serdes_monitor_linkup(channel); |
| 282 | } |
| 283 | |
| 284 | static int r8a779f0_eth_serdes_power_on(struct phy *p) |
| 285 | { |
| 286 | struct r8a779f0_eth_serdes_drv_data *dd = dev_get_priv(p->dev); |
| 287 | struct r8a779f0_eth_serdes_channel *channel = dd->channel + p->id; |
| 288 | |
| 289 | return r8a779f0_eth_serdes_hw_init_late(channel); |
| 290 | } |
| 291 | |
| 292 | static int r8a779f0_eth_serdes_set_mode(struct phy *p, enum phy_mode mode, |
| 293 | int submode) |
| 294 | { |
| 295 | struct r8a779f0_eth_serdes_drv_data *dd = dev_get_priv(p->dev); |
| 296 | struct r8a779f0_eth_serdes_channel *channel = dd->channel + p->id; |
| 297 | |
| 298 | if (mode != PHY_MODE_ETHERNET) |
| 299 | return -EOPNOTSUPP; |
| 300 | |
| 301 | switch (submode) { |
| 302 | case PHY_INTERFACE_MODE_GMII: |
| 303 | case PHY_INTERFACE_MODE_SGMII: |
| 304 | case PHY_INTERFACE_MODE_USXGMII: |
| 305 | channel->phy_interface = submode; |
| 306 | return 0; |
| 307 | default: |
| 308 | return -EOPNOTSUPP; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | static int r8a779f0_eth_serdes_set_speed(struct phy *p, int speed) |
| 313 | { |
| 314 | struct r8a779f0_eth_serdes_drv_data *dd = dev_get_priv(p->dev); |
| 315 | struct r8a779f0_eth_serdes_channel *channel = dd->channel + p->id; |
| 316 | |
| 317 | channel->speed = speed; |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | static int r8a779f0_eth_serdes_of_xlate(struct phy *phy, |
| 323 | struct ofnode_phandle_args *args) |
| 324 | { |
| 325 | if (args->args_count < 1) |
| 326 | return -ENODEV; |
| 327 | |
| 328 | if (args->args[0] >= R8A779F0_ETH_SERDES_NUM) |
| 329 | return -ENODEV; |
| 330 | |
| 331 | phy->id = args->args[0]; |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static const struct phy_ops r8a779f0_eth_serdes_ops = { |
| 337 | .init = r8a779f0_eth_serdes_init, |
| 338 | .power_on = r8a779f0_eth_serdes_power_on, |
| 339 | .set_mode = r8a779f0_eth_serdes_set_mode, |
| 340 | .set_speed = r8a779f0_eth_serdes_set_speed, |
| 341 | .of_xlate = r8a779f0_eth_serdes_of_xlate, |
| 342 | }; |
| 343 | |
| 344 | static const struct udevice_id r8a779f0_eth_serdes_of_table[] = { |
| 345 | { .compatible = "renesas,r8a779f0-ether-serdes", }, |
| 346 | { } |
| 347 | }; |
| 348 | |
| 349 | static int r8a779f0_eth_serdes_probe(struct udevice *dev) |
| 350 | { |
| 351 | struct r8a779f0_eth_serdes_drv_data *dd = dev_get_priv(dev); |
| 352 | int i; |
| 353 | |
| 354 | dd->addr = dev_read_addr_ptr(dev); |
| 355 | if (!dd->addr) |
| 356 | return -EINVAL; |
| 357 | |
| 358 | dd->reset = devm_reset_control_get(dev, NULL); |
| 359 | if (IS_ERR(dd->reset)) |
| 360 | return PTR_ERR(dd->reset); |
| 361 | |
| 362 | reset_assert(dd->reset); |
| 363 | reset_deassert(dd->reset); |
| 364 | |
| 365 | for (i = 0; i < R8A779F0_ETH_SERDES_NUM; i++) { |
| 366 | struct r8a779f0_eth_serdes_channel *channel = &dd->channel[i]; |
| 367 | |
| 368 | channel->addr = dd->addr + R8A779F0_ETH_SERDES_OFFSET * i; |
| 369 | channel->dd = dd; |
| 370 | channel->index = i; |
| 371 | } |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | U_BOOT_DRIVER(r8a779f0_eth_serdes_driver_platform) = { |
| 377 | .name = "r8a779f0_eth_serdes", |
| 378 | .id = UCLASS_PHY, |
| 379 | .of_match = r8a779f0_eth_serdes_of_table, |
| 380 | .probe = r8a779f0_eth_serdes_probe, |
| 381 | .ops = &r8a779f0_eth_serdes_ops, |
| 382 | .priv_auto = sizeof(struct r8a779f0_eth_serdes_drv_data), |
| 383 | }; |