Álvaro Fernández Rojas | b0b2254 | 2018-12-01 19:00:32 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018 Álvaro Fernández Rojas <noltari@gmail.com> |
| 4 | * |
| 5 | * Derived from linux/drivers/net/ethernet/broadcom/bcm63xx_enet.c: |
| 6 | * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr> |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <clk.h> |
| 11 | #include <dm.h> |
| 12 | #include <dma.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Álvaro Fernández Rojas | b0b2254 | 2018-12-01 19:00:32 +0100 | [diff] [blame] | 14 | #include <miiphy.h> |
| 15 | #include <net.h> |
| 16 | #include <reset.h> |
| 17 | #include <wait_bit.h> |
| 18 | #include <asm/io.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 19 | #include <dm/device_compat.h> |
Álvaro Fernández Rojas | b0b2254 | 2018-12-01 19:00:32 +0100 | [diff] [blame] | 20 | |
| 21 | #define ETH_PORT_STR "brcm,enetsw-port" |
| 22 | |
| 23 | #define ETH_RX_DESC PKTBUFSRX |
| 24 | #define ETH_ZLEN 60 |
| 25 | #define ETH_TIMEOUT 100 |
| 26 | |
| 27 | #define ETH_MAX_PORT 8 |
| 28 | #define ETH_RGMII_PORT0 4 |
| 29 | |
| 30 | /* Port traffic control */ |
| 31 | #define ETH_PTCTRL_REG(x) (0x0 + (x)) |
| 32 | #define ETH_PTCTRL_RXDIS_SHIFT 0 |
| 33 | #define ETH_PTCTRL_RXDIS_MASK (1 << ETH_PTCTRL_RXDIS_SHIFT) |
| 34 | #define ETH_PTCTRL_TXDIS_SHIFT 1 |
| 35 | #define ETH_PTCTRL_TXDIS_MASK (1 << ETH_PTCTRL_TXDIS_SHIFT) |
| 36 | |
| 37 | /* Switch mode register */ |
| 38 | #define ETH_SWMODE_REG 0xb |
| 39 | #define ETH_SWMODE_FWD_EN_SHIFT 1 |
| 40 | #define ETH_SWMODE_FWD_EN_MASK (1 << ETH_SWMODE_FWD_EN_SHIFT) |
| 41 | |
| 42 | /* IMP override Register */ |
| 43 | #define ETH_IMPOV_REG 0xe |
| 44 | #define ETH_IMPOV_LINKUP_SHIFT 0 |
| 45 | #define ETH_IMPOV_LINKUP_MASK (1 << ETH_IMPOV_LINKUP_SHIFT) |
| 46 | #define ETH_IMPOV_FDX_SHIFT 1 |
| 47 | #define ETH_IMPOV_FDX_MASK (1 << ETH_IMPOV_FDX_SHIFT) |
| 48 | #define ETH_IMPOV_100_SHIFT 2 |
| 49 | #define ETH_IMPOV_100_MASK (1 << ETH_IMPOV_100_SHIFT) |
| 50 | #define ETH_IMPOV_1000_SHIFT 3 |
| 51 | #define ETH_IMPOV_1000_MASK (1 << ETH_IMPOV_1000_SHIFT) |
| 52 | #define ETH_IMPOV_RXFLOW_SHIFT 4 |
| 53 | #define ETH_IMPOV_RXFLOW_MASK (1 << ETH_IMPOV_RXFLOW_SHIFT) |
| 54 | #define ETH_IMPOV_TXFLOW_SHIFT 5 |
| 55 | #define ETH_IMPOV_TXFLOW_MASK (1 << ETH_IMPOV_TXFLOW_SHIFT) |
| 56 | #define ETH_IMPOV_FORCE_SHIFT 7 |
| 57 | #define ETH_IMPOV_FORCE_MASK (1 << ETH_IMPOV_FORCE_SHIFT) |
| 58 | |
| 59 | /* Port override Register */ |
| 60 | #define ETH_PORTOV_REG(x) (0x58 + (x)) |
| 61 | #define ETH_PORTOV_LINKUP_SHIFT 0 |
| 62 | #define ETH_PORTOV_LINKUP_MASK (1 << ETH_PORTOV_LINKUP_SHIFT) |
| 63 | #define ETH_PORTOV_FDX_SHIFT 1 |
| 64 | #define ETH_PORTOV_FDX_MASK (1 << ETH_PORTOV_FDX_SHIFT) |
| 65 | #define ETH_PORTOV_100_SHIFT 2 |
| 66 | #define ETH_PORTOV_100_MASK (1 << ETH_PORTOV_100_SHIFT) |
| 67 | #define ETH_PORTOV_1000_SHIFT 3 |
| 68 | #define ETH_PORTOV_1000_MASK (1 << ETH_PORTOV_1000_SHIFT) |
| 69 | #define ETH_PORTOV_RXFLOW_SHIFT 4 |
| 70 | #define ETH_PORTOV_RXFLOW_MASK (1 << ETH_PORTOV_RXFLOW_SHIFT) |
| 71 | #define ETH_PORTOV_TXFLOW_SHIFT 5 |
| 72 | #define ETH_PORTOV_TXFLOW_MASK (1 << ETH_PORTOV_TXFLOW_SHIFT) |
| 73 | #define ETH_PORTOV_ENABLE_SHIFT 6 |
| 74 | #define ETH_PORTOV_ENABLE_MASK (1 << ETH_PORTOV_ENABLE_SHIFT) |
| 75 | |
| 76 | /* Port RGMII control register */ |
| 77 | #define ETH_RGMII_CTRL_REG(x) (0x60 + (x)) |
| 78 | #define ETH_RGMII_CTRL_GMII_CLK_EN (1 << 7) |
| 79 | #define ETH_RGMII_CTRL_MII_OVERRIDE_EN (1 << 6) |
| 80 | #define ETH_RGMII_CTRL_MII_MODE_MASK (3 << 4) |
| 81 | #define ETH_RGMII_CTRL_RGMII_MODE (0 << 4) |
| 82 | #define ETH_RGMII_CTRL_MII_MODE (1 << 4) |
| 83 | #define ETH_RGMII_CTRL_RVMII_MODE (2 << 4) |
| 84 | #define ETH_RGMII_CTRL_TIMING_SEL_EN (1 << 0) |
| 85 | |
| 86 | /* Port RGMII timing register */ |
| 87 | #define ENETSW_RGMII_TIMING_REG(x) (0x68 + (x)) |
| 88 | |
| 89 | /* MDIO control register */ |
| 90 | #define MII_SC_REG 0xb0 |
| 91 | #define MII_SC_EXT_SHIFT 16 |
| 92 | #define MII_SC_EXT_MASK (1 << MII_SC_EXT_SHIFT) |
| 93 | #define MII_SC_REG_SHIFT 20 |
| 94 | #define MII_SC_PHYID_SHIFT 25 |
| 95 | #define MII_SC_RD_SHIFT 30 |
| 96 | #define MII_SC_RD_MASK (1 << MII_SC_RD_SHIFT) |
| 97 | #define MII_SC_WR_SHIFT 31 |
| 98 | #define MII_SC_WR_MASK (1 << MII_SC_WR_SHIFT) |
| 99 | |
| 100 | /* MDIO data register */ |
| 101 | #define MII_DAT_REG 0xb4 |
| 102 | |
| 103 | /* Global Management Configuration Register */ |
| 104 | #define ETH_GMCR_REG 0x200 |
| 105 | #define ETH_GMCR_RST_MIB_SHIFT 0 |
| 106 | #define ETH_GMCR_RST_MIB_MASK (1 << ETH_GMCR_RST_MIB_SHIFT) |
| 107 | |
| 108 | /* Jumbo control register port mask register */ |
| 109 | #define ETH_JMBCTL_PORT_REG 0x4004 |
| 110 | |
| 111 | /* Jumbo control mib good frame register */ |
| 112 | #define ETH_JMBCTL_MAXSIZE_REG 0x4008 |
| 113 | |
| 114 | /* ETH port data */ |
| 115 | struct bcm_enetsw_port { |
| 116 | bool used; |
| 117 | const char *name; |
| 118 | /* Config */ |
| 119 | bool bypass_link; |
| 120 | int force_speed; |
| 121 | bool force_duplex_full; |
| 122 | /* PHY */ |
| 123 | int phy_id; |
| 124 | }; |
| 125 | |
| 126 | /* ETH data */ |
| 127 | struct bcm6368_eth_priv { |
| 128 | void __iomem *base; |
| 129 | /* DMA */ |
| 130 | struct dma rx_dma; |
| 131 | struct dma tx_dma; |
| 132 | /* Ports */ |
| 133 | uint8_t num_ports; |
| 134 | struct bcm_enetsw_port used_ports[ETH_MAX_PORT]; |
| 135 | int sw_port_link[ETH_MAX_PORT]; |
| 136 | bool rgmii_override; |
| 137 | bool rgmii_timing; |
| 138 | /* PHY */ |
| 139 | int phy_id; |
| 140 | }; |
| 141 | |
| 142 | static inline bool bcm_enet_port_is_rgmii(int portid) |
| 143 | { |
| 144 | return portid >= ETH_RGMII_PORT0; |
| 145 | } |
| 146 | |
| 147 | static int bcm6368_mdio_read(struct bcm6368_eth_priv *priv, uint8_t ext, |
| 148 | int phy_id, int reg) |
| 149 | { |
| 150 | uint32_t val; |
| 151 | |
| 152 | writel_be(0, priv->base + MII_SC_REG); |
| 153 | |
| 154 | val = MII_SC_RD_MASK | |
| 155 | (phy_id << MII_SC_PHYID_SHIFT) | |
| 156 | (reg << MII_SC_REG_SHIFT); |
| 157 | |
| 158 | if (ext) |
| 159 | val |= MII_SC_EXT_MASK; |
| 160 | |
| 161 | writel_be(val, priv->base + MII_SC_REG); |
| 162 | udelay(50); |
| 163 | |
| 164 | return readw_be(priv->base + MII_DAT_REG); |
| 165 | } |
| 166 | |
| 167 | static int bcm6368_mdio_write(struct bcm6368_eth_priv *priv, uint8_t ext, |
| 168 | int phy_id, int reg, u16 data) |
| 169 | { |
| 170 | uint32_t val; |
| 171 | |
| 172 | writel_be(0, priv->base + MII_SC_REG); |
| 173 | |
| 174 | val = MII_SC_WR_MASK | |
| 175 | (phy_id << MII_SC_PHYID_SHIFT) | |
| 176 | (reg << MII_SC_REG_SHIFT); |
| 177 | |
| 178 | if (ext) |
| 179 | val |= MII_SC_EXT_MASK; |
| 180 | |
| 181 | val |= data; |
| 182 | |
| 183 | writel_be(val, priv->base + MII_SC_REG); |
| 184 | udelay(50); |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static int bcm6368_eth_free_pkt(struct udevice *dev, uchar *packet, int len) |
| 190 | { |
| 191 | struct bcm6368_eth_priv *priv = dev_get_priv(dev); |
| 192 | |
| 193 | return dma_prepare_rcv_buf(&priv->rx_dma, packet, len); |
| 194 | } |
| 195 | |
| 196 | static int bcm6368_eth_recv(struct udevice *dev, int flags, uchar **packetp) |
| 197 | { |
| 198 | struct bcm6368_eth_priv *priv = dev_get_priv(dev); |
| 199 | |
| 200 | return dma_receive(&priv->rx_dma, (void**)packetp, NULL); |
| 201 | } |
| 202 | |
| 203 | static int bcm6368_eth_send(struct udevice *dev, void *packet, int length) |
| 204 | { |
| 205 | struct bcm6368_eth_priv *priv = dev_get_priv(dev); |
| 206 | |
| 207 | /* pad packets smaller than ETH_ZLEN */ |
| 208 | if (length < ETH_ZLEN) { |
| 209 | memset(packet + length, 0, ETH_ZLEN - length); |
| 210 | length = ETH_ZLEN; |
| 211 | } |
| 212 | |
| 213 | return dma_send(&priv->tx_dma, packet, length, NULL); |
| 214 | } |
| 215 | |
| 216 | static int bcm6368_eth_adjust_link(struct udevice *dev) |
| 217 | { |
| 218 | struct bcm6368_eth_priv *priv = dev_get_priv(dev); |
| 219 | unsigned int i; |
| 220 | |
| 221 | for (i = 0; i < priv->num_ports; i++) { |
| 222 | struct bcm_enetsw_port *port; |
| 223 | int val, j, up, adv, lpa, speed, duplex, media; |
| 224 | int external_phy = bcm_enet_port_is_rgmii(i); |
| 225 | u8 override; |
| 226 | |
| 227 | port = &priv->used_ports[i]; |
| 228 | if (!port->used) |
| 229 | continue; |
| 230 | |
| 231 | if (port->bypass_link) |
| 232 | continue; |
| 233 | |
| 234 | /* dummy read to clear */ |
| 235 | for (j = 0; j < 2; j++) |
| 236 | val = bcm6368_mdio_read(priv, external_phy, |
| 237 | port->phy_id, MII_BMSR); |
| 238 | |
| 239 | if (val == 0xffff) |
| 240 | continue; |
| 241 | |
| 242 | up = (val & BMSR_LSTATUS) ? 1 : 0; |
| 243 | if (!(up ^ priv->sw_port_link[i])) |
| 244 | continue; |
| 245 | |
| 246 | priv->sw_port_link[i] = up; |
| 247 | |
| 248 | /* link changed */ |
| 249 | if (!up) { |
| 250 | dev_info(&priv->pdev->dev, "link DOWN on %s\n", |
| 251 | port->name); |
| 252 | writeb_be(ETH_PORTOV_ENABLE_MASK, |
| 253 | priv->base + ETH_PORTOV_REG(i)); |
| 254 | writeb_be(ETH_PTCTRL_RXDIS_MASK | |
| 255 | ETH_PTCTRL_TXDIS_MASK, |
| 256 | priv->base + ETH_PTCTRL_REG(i)); |
| 257 | continue; |
| 258 | } |
| 259 | |
| 260 | adv = bcm6368_mdio_read(priv, external_phy, |
| 261 | port->phy_id, MII_ADVERTISE); |
| 262 | |
| 263 | lpa = bcm6368_mdio_read(priv, external_phy, port->phy_id, |
| 264 | MII_LPA); |
| 265 | |
| 266 | /* figure out media and duplex from advertise and LPA values */ |
| 267 | media = mii_nway_result(lpa & adv); |
| 268 | duplex = (media & ADVERTISE_FULL) ? 1 : 0; |
| 269 | |
| 270 | if (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)) |
| 271 | speed = 100; |
| 272 | else |
| 273 | speed = 10; |
| 274 | |
| 275 | if (val & BMSR_ESTATEN) { |
| 276 | adv = bcm6368_mdio_read(priv, external_phy, |
| 277 | port->phy_id, MII_CTRL1000); |
| 278 | |
| 279 | lpa = bcm6368_mdio_read(priv, external_phy, |
| 280 | port->phy_id, MII_STAT1000); |
| 281 | |
| 282 | if ((adv & (ADVERTISE_1000FULL | ADVERTISE_1000HALF)) && |
| 283 | (lpa & (LPA_1000FULL | LPA_1000HALF))) { |
| 284 | speed = 1000; |
| 285 | duplex = (lpa & LPA_1000FULL); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | pr_alert("link UP on %s, %dMbps, %s-duplex\n", |
| 290 | port->name, speed, duplex ? "full" : "half"); |
| 291 | |
| 292 | override = ETH_PORTOV_ENABLE_MASK | |
| 293 | ETH_PORTOV_LINKUP_MASK; |
| 294 | |
| 295 | if (speed == 1000) |
| 296 | override |= ETH_PORTOV_1000_MASK; |
| 297 | else if (speed == 100) |
| 298 | override |= ETH_PORTOV_100_MASK; |
| 299 | if (duplex) |
| 300 | override |= ETH_PORTOV_FDX_MASK; |
| 301 | |
| 302 | writeb_be(override, priv->base + ETH_PORTOV_REG(i)); |
| 303 | writeb_be(0, priv->base + ETH_PTCTRL_REG(i)); |
| 304 | } |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | static int bcm6368_eth_start(struct udevice *dev) |
| 310 | { |
| 311 | struct bcm6368_eth_priv *priv = dev_get_priv(dev); |
| 312 | uint8_t i; |
| 313 | |
Álvaro Fernández Rojas | 42056a5 | 2019-01-01 19:44:46 +0100 | [diff] [blame] | 314 | /* disable all ports */ |
| 315 | for (i = 0; i < priv->num_ports; i++) { |
| 316 | setbits_8(priv->base + ETH_PORTOV_REG(i), |
| 317 | ETH_PORTOV_ENABLE_MASK); |
| 318 | setbits_8(priv->base + ETH_PTCTRL_REG(i), |
| 319 | ETH_PTCTRL_RXDIS_MASK | ETH_PTCTRL_TXDIS_MASK); |
| 320 | priv->sw_port_link[i] = 0; |
| 321 | } |
| 322 | |
| 323 | /* enable external ports */ |
| 324 | for (i = ETH_RGMII_PORT0; i < priv->num_ports; i++) { |
| 325 | u8 rgmii_ctrl = ETH_RGMII_CTRL_GMII_CLK_EN; |
| 326 | |
| 327 | if (!priv->used_ports[i].used) |
| 328 | continue; |
| 329 | |
| 330 | if (priv->rgmii_override) |
| 331 | rgmii_ctrl |= ETH_RGMII_CTRL_MII_OVERRIDE_EN; |
| 332 | if (priv->rgmii_timing) |
| 333 | rgmii_ctrl |= ETH_RGMII_CTRL_TIMING_SEL_EN; |
| 334 | |
| 335 | setbits_8(priv->base + ETH_RGMII_CTRL_REG(i), rgmii_ctrl); |
| 336 | } |
| 337 | |
| 338 | /* reset mib */ |
| 339 | setbits_8(priv->base + ETH_GMCR_REG, ETH_GMCR_RST_MIB_MASK); |
| 340 | mdelay(1); |
| 341 | clrbits_8(priv->base + ETH_GMCR_REG, ETH_GMCR_RST_MIB_MASK); |
| 342 | mdelay(1); |
| 343 | |
| 344 | /* force CPU port state */ |
| 345 | setbits_8(priv->base + ETH_IMPOV_REG, |
| 346 | ETH_IMPOV_FORCE_MASK | ETH_IMPOV_LINKUP_MASK); |
| 347 | |
| 348 | /* enable switch forward engine */ |
| 349 | setbits_8(priv->base + ETH_SWMODE_REG, ETH_SWMODE_FWD_EN_MASK); |
| 350 | |
Álvaro Fernández Rojas | b0b2254 | 2018-12-01 19:00:32 +0100 | [diff] [blame] | 351 | /* prepare rx dma buffers */ |
| 352 | for (i = 0; i < ETH_RX_DESC; i++) { |
| 353 | int ret = dma_prepare_rcv_buf(&priv->rx_dma, net_rx_packets[i], |
| 354 | PKTSIZE_ALIGN); |
| 355 | if (ret < 0) |
| 356 | break; |
| 357 | } |
| 358 | |
| 359 | /* enable dma rx channel */ |
| 360 | dma_enable(&priv->rx_dma); |
| 361 | |
| 362 | /* enable dma tx channel */ |
| 363 | dma_enable(&priv->tx_dma); |
| 364 | |
| 365 | /* apply override config for bypass_link ports here. */ |
| 366 | for (i = 0; i < priv->num_ports; i++) { |
| 367 | struct bcm_enetsw_port *port; |
| 368 | u8 override; |
| 369 | |
| 370 | port = &priv->used_ports[i]; |
| 371 | if (!port->used) |
| 372 | continue; |
| 373 | |
| 374 | if (!port->bypass_link) |
| 375 | continue; |
| 376 | |
| 377 | override = ETH_PORTOV_ENABLE_MASK | |
| 378 | ETH_PORTOV_LINKUP_MASK; |
| 379 | |
| 380 | switch (port->force_speed) { |
| 381 | case 1000: |
| 382 | override |= ETH_PORTOV_1000_MASK; |
| 383 | break; |
| 384 | case 100: |
| 385 | override |= ETH_PORTOV_100_MASK; |
| 386 | break; |
| 387 | case 10: |
| 388 | break; |
| 389 | default: |
| 390 | pr_warn("%s: invalid forced speed on port %s\n", |
| 391 | __func__, port->name); |
| 392 | break; |
| 393 | } |
| 394 | |
| 395 | if (port->force_duplex_full) |
| 396 | override |= ETH_PORTOV_FDX_MASK; |
| 397 | |
| 398 | writeb_be(override, priv->base + ETH_PORTOV_REG(i)); |
| 399 | writeb_be(0, priv->base + ETH_PTCTRL_REG(i)); |
| 400 | } |
| 401 | |
| 402 | bcm6368_eth_adjust_link(dev); |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | static void bcm6368_eth_stop(struct udevice *dev) |
| 408 | { |
| 409 | struct bcm6368_eth_priv *priv = dev_get_priv(dev); |
Álvaro Fernández Rojas | 42056a5 | 2019-01-01 19:44:46 +0100 | [diff] [blame] | 410 | uint8_t i; |
| 411 | |
| 412 | /* disable all ports */ |
| 413 | for (i = 0; i < priv->num_ports; i++) { |
| 414 | setbits_8(priv->base + ETH_PORTOV_REG(i), |
| 415 | ETH_PORTOV_ENABLE_MASK); |
| 416 | setbits_8(priv->base + ETH_PTCTRL_REG(i), |
| 417 | ETH_PTCTRL_RXDIS_MASK | ETH_PTCTRL_TXDIS_MASK); |
| 418 | } |
| 419 | |
| 420 | /* disable external ports */ |
| 421 | for (i = ETH_RGMII_PORT0; i < priv->num_ports; i++) { |
| 422 | if (!priv->used_ports[i].used) |
| 423 | continue; |
| 424 | |
| 425 | clrbits_8(priv->base + ETH_RGMII_CTRL_REG(i), |
| 426 | ETH_RGMII_CTRL_GMII_CLK_EN); |
| 427 | } |
| 428 | |
| 429 | /* disable CPU port */ |
| 430 | clrbits_8(priv->base + ETH_IMPOV_REG, |
| 431 | ETH_IMPOV_FORCE_MASK | ETH_IMPOV_LINKUP_MASK); |
| 432 | |
| 433 | /* disable switch forward engine */ |
| 434 | clrbits_8(priv->base + ETH_SWMODE_REG, ETH_SWMODE_FWD_EN_MASK); |
Álvaro Fernández Rojas | b0b2254 | 2018-12-01 19:00:32 +0100 | [diff] [blame] | 435 | |
| 436 | /* disable dma rx channel */ |
| 437 | dma_disable(&priv->rx_dma); |
| 438 | |
| 439 | /* disable dma tx channel */ |
| 440 | dma_disable(&priv->tx_dma); |
| 441 | } |
| 442 | |
| 443 | static const struct eth_ops bcm6368_eth_ops = { |
| 444 | .free_pkt = bcm6368_eth_free_pkt, |
| 445 | .recv = bcm6368_eth_recv, |
| 446 | .send = bcm6368_eth_send, |
| 447 | .start = bcm6368_eth_start, |
| 448 | .stop = bcm6368_eth_stop, |
| 449 | }; |
| 450 | |
| 451 | static const struct udevice_id bcm6368_eth_ids[] = { |
| 452 | { .compatible = "brcm,bcm6368-enet", }, |
| 453 | { /* sentinel */ } |
| 454 | }; |
| 455 | |
| 456 | static bool bcm6368_phy_is_external(struct bcm6368_eth_priv *priv, int phy_id) |
| 457 | { |
| 458 | uint8_t i; |
| 459 | |
| 460 | for (i = 0; i < priv->num_ports; ++i) { |
| 461 | if (!priv->used_ports[i].used) |
| 462 | continue; |
| 463 | if (priv->used_ports[i].phy_id == phy_id) |
| 464 | return bcm_enet_port_is_rgmii(i); |
| 465 | } |
| 466 | |
| 467 | return true; |
| 468 | } |
| 469 | |
| 470 | static int bcm6368_mii_mdio_read(struct mii_dev *bus, int addr, int devaddr, |
| 471 | int reg) |
| 472 | { |
| 473 | struct bcm6368_eth_priv *priv = bus->priv; |
| 474 | bool ext = bcm6368_phy_is_external(priv, addr); |
| 475 | |
| 476 | return bcm6368_mdio_read(priv, ext, addr, reg); |
| 477 | } |
| 478 | |
| 479 | static int bcm6368_mii_mdio_write(struct mii_dev *bus, int addr, int devaddr, |
| 480 | int reg, u16 data) |
| 481 | { |
| 482 | struct bcm6368_eth_priv *priv = bus->priv; |
| 483 | bool ext = bcm6368_phy_is_external(priv, addr); |
| 484 | |
| 485 | return bcm6368_mdio_write(priv, ext, addr, reg, data); |
| 486 | } |
| 487 | |
| 488 | static int bcm6368_mdio_init(const char *name, struct bcm6368_eth_priv *priv) |
| 489 | { |
| 490 | struct mii_dev *bus; |
| 491 | |
| 492 | bus = mdio_alloc(); |
| 493 | if (!bus) { |
| 494 | pr_err("%s: failed to allocate MDIO bus\n", __func__); |
| 495 | return -ENOMEM; |
| 496 | } |
| 497 | |
| 498 | bus->read = bcm6368_mii_mdio_read; |
| 499 | bus->write = bcm6368_mii_mdio_write; |
| 500 | bus->priv = priv; |
| 501 | snprintf(bus->name, sizeof(bus->name), "%s", name); |
| 502 | |
| 503 | return mdio_register(bus); |
| 504 | } |
| 505 | |
| 506 | static int bcm6368_eth_probe(struct udevice *dev) |
| 507 | { |
| 508 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 509 | struct bcm6368_eth_priv *priv = dev_get_priv(dev); |
| 510 | int num_ports, ret, i; |
Álvaro Fernández Rojas | b0b2254 | 2018-12-01 19:00:32 +0100 | [diff] [blame] | 511 | ofnode node; |
| 512 | |
| 513 | /* get base address */ |
| 514 | priv->base = dev_remap_addr(dev); |
| 515 | if (!priv->base) |
| 516 | return -EINVAL; |
| 517 | pdata->iobase = (phys_addr_t) priv->base; |
| 518 | |
| 519 | /* get number of ports */ |
| 520 | num_ports = dev_read_u32_default(dev, "brcm,num-ports", ETH_MAX_PORT); |
| 521 | if (!num_ports || num_ports > ETH_MAX_PORT) |
| 522 | return -EINVAL; |
| 523 | |
| 524 | /* get dma channels */ |
| 525 | ret = dma_get_by_name(dev, "tx", &priv->tx_dma); |
| 526 | if (ret) |
| 527 | return -EINVAL; |
| 528 | |
| 529 | ret = dma_get_by_name(dev, "rx", &priv->rx_dma); |
| 530 | if (ret) |
| 531 | return -EINVAL; |
| 532 | |
| 533 | /* try to enable clocks */ |
| 534 | for (i = 0; ; i++) { |
| 535 | struct clk clk; |
| 536 | int ret; |
| 537 | |
| 538 | ret = clk_get_by_index(dev, i, &clk); |
| 539 | if (ret < 0) |
| 540 | break; |
| 541 | |
| 542 | ret = clk_enable(&clk); |
| 543 | if (ret < 0) { |
| 544 | pr_err("%s: error enabling clock %d\n", __func__, i); |
| 545 | return ret; |
| 546 | } |
| 547 | |
| 548 | ret = clk_free(&clk); |
| 549 | if (ret < 0) { |
| 550 | pr_err("%s: error freeing clock %d\n", __func__, i); |
| 551 | return ret; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | /* try to perform resets */ |
| 556 | for (i = 0; ; i++) { |
| 557 | struct reset_ctl reset; |
| 558 | int ret; |
| 559 | |
| 560 | ret = reset_get_by_index(dev, i, &reset); |
| 561 | if (ret < 0) |
| 562 | break; |
| 563 | |
| 564 | ret = reset_deassert(&reset); |
| 565 | if (ret < 0) { |
| 566 | pr_err("%s: error deasserting reset %d\n", __func__, i); |
| 567 | return ret; |
| 568 | } |
| 569 | |
| 570 | ret = reset_free(&reset); |
| 571 | if (ret < 0) { |
| 572 | pr_err("%s: error freeing reset %d\n", __func__, i); |
| 573 | return ret; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | /* set priv data */ |
| 578 | priv->num_ports = num_ports; |
| 579 | if (dev_read_bool(dev, "brcm,rgmii-override")) |
| 580 | priv->rgmii_override = true; |
| 581 | if (dev_read_bool(dev, "brcm,rgmii-timing")) |
| 582 | priv->rgmii_timing = true; |
| 583 | |
| 584 | /* get ports */ |
| 585 | dev_for_each_subnode(node, dev) { |
| 586 | const char *comp; |
| 587 | const char *label; |
| 588 | unsigned int p; |
| 589 | int phy_id; |
| 590 | int speed; |
| 591 | |
| 592 | comp = ofnode_read_string(node, "compatible"); |
| 593 | if (!comp || memcmp(comp, ETH_PORT_STR, sizeof(ETH_PORT_STR))) |
| 594 | continue; |
| 595 | |
| 596 | p = ofnode_read_u32_default(node, "reg", ETH_MAX_PORT); |
| 597 | if (p >= num_ports) |
| 598 | return -EINVAL; |
| 599 | |
| 600 | label = ofnode_read_string(node, "label"); |
| 601 | if (!label) { |
| 602 | debug("%s: node %s has no label\n", __func__, |
| 603 | ofnode_get_name(node)); |
| 604 | return -EINVAL; |
| 605 | } |
| 606 | |
| 607 | phy_id = ofnode_read_u32_default(node, "brcm,phy-id", -1); |
| 608 | |
| 609 | priv->used_ports[p].used = true; |
| 610 | priv->used_ports[p].name = label; |
| 611 | priv->used_ports[p].phy_id = phy_id; |
| 612 | |
| 613 | if (ofnode_read_bool(node, "full-duplex")) |
| 614 | priv->used_ports[p].force_duplex_full = true; |
| 615 | if (ofnode_read_bool(node, "bypass-link")) |
| 616 | priv->used_ports[p].bypass_link = true; |
| 617 | speed = ofnode_read_u32_default(node, "speed", 0); |
| 618 | if (speed) |
| 619 | priv->used_ports[p].force_speed = speed; |
| 620 | } |
| 621 | |
| 622 | /* init mii bus */ |
| 623 | ret = bcm6368_mdio_init(dev->name, priv); |
| 624 | if (ret) |
| 625 | return ret; |
| 626 | |
Álvaro Fernández Rojas | b0b2254 | 2018-12-01 19:00:32 +0100 | [diff] [blame] | 627 | /* enable jumbo on all ports */ |
| 628 | writel_be(0x1ff, priv->base + ETH_JMBCTL_PORT_REG); |
| 629 | writew_be(9728, priv->base + ETH_JMBCTL_MAXSIZE_REG); |
| 630 | |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | U_BOOT_DRIVER(bcm6368_eth) = { |
| 635 | .name = "bcm6368_eth", |
| 636 | .id = UCLASS_ETH, |
| 637 | .of_match = bcm6368_eth_ids, |
| 638 | .ops = &bcm6368_eth_ops, |
| 639 | .platdata_auto_alloc_size = sizeof(struct eth_pdata), |
| 640 | .priv_auto_alloc_size = sizeof(struct bcm6368_eth_priv), |
| 641 | .probe = bcm6368_eth_probe, |
| 642 | }; |