Alex Marginean | 120b5ef | 2019-07-03 12:11:40 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * ENETC ethernet controller driver |
| 4 | * Copyright 2017-2019 NXP |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <errno.h> |
| 10 | #include <memalign.h> |
| 11 | #include <asm/io.h> |
| 12 | #include <pci.h> |
Alex Marginean | 1d99534 | 2019-07-03 12:11:41 +0300 | [diff] [blame] | 13 | #include <miiphy.h> |
Alex Marginean | 120b5ef | 2019-07-03 12:11:40 +0300 | [diff] [blame] | 14 | |
| 15 | #include "fsl_enetc.h" |
| 16 | |
| 17 | /* |
| 18 | * Bind the device: |
| 19 | * - set a more explicit name on the interface |
| 20 | */ |
| 21 | static int enetc_bind(struct udevice *dev) |
| 22 | { |
| 23 | char name[16]; |
| 24 | static int eth_num_devices; |
| 25 | |
| 26 | /* |
| 27 | * prefer using PCI function numbers to number interfaces, but these |
| 28 | * are only available if dts nodes are present. For PCI they are |
| 29 | * optional, handle that case too. Just in case some nodes are present |
| 30 | * and some are not, use different naming scheme - enetc-N based on |
| 31 | * PCI function # and enetc#N based on interface count |
| 32 | */ |
| 33 | if (ofnode_valid(dev->node)) |
| 34 | sprintf(name, "enetc-%u", PCI_FUNC(pci_get_devfn(dev))); |
| 35 | else |
| 36 | sprintf(name, "enetc#%u", eth_num_devices++); |
| 37 | device_set_name(dev, name); |
| 38 | |
| 39 | return 0; |
| 40 | } |
| 41 | |
Alex Marginean | e4aafd5 | 2019-07-03 12:11:42 +0300 | [diff] [blame] | 42 | /* MDIO wrappers, we're using these to drive internal MDIO to get to serdes */ |
| 43 | static int enetc_mdio_read(struct mii_dev *bus, int addr, int devad, int reg) |
| 44 | { |
| 45 | struct enetc_mdio_priv priv; |
| 46 | |
| 47 | priv.regs_base = bus->priv; |
| 48 | return enetc_mdio_read_priv(&priv, addr, devad, reg); |
| 49 | } |
| 50 | |
| 51 | static int enetc_mdio_write(struct mii_dev *bus, int addr, int devad, int reg, |
| 52 | u16 val) |
| 53 | { |
| 54 | struct enetc_mdio_priv priv; |
| 55 | |
| 56 | priv.regs_base = bus->priv; |
| 57 | return enetc_mdio_write_priv(&priv, addr, devad, reg, val); |
| 58 | } |
| 59 | |
| 60 | /* only interfaces that can pin out through serdes have internal MDIO */ |
| 61 | static bool enetc_has_imdio(struct udevice *dev) |
| 62 | { |
| 63 | struct enetc_priv *priv = dev_get_priv(dev); |
| 64 | |
| 65 | return !!(priv->imdio.priv); |
| 66 | } |
| 67 | |
| 68 | /* set up serdes for SGMII */ |
| 69 | static int enetc_init_sgmii(struct udevice *dev) |
| 70 | { |
| 71 | struct enetc_priv *priv = dev_get_priv(dev); |
Alex Marginean | 9bc07e81 | 2019-07-15 11:48:47 +0300 | [diff] [blame] | 72 | bool is2500 = false; |
| 73 | u16 reg; |
Alex Marginean | e4aafd5 | 2019-07-03 12:11:42 +0300 | [diff] [blame] | 74 | |
| 75 | if (!enetc_has_imdio(dev)) |
| 76 | return 0; |
| 77 | |
Alex Marginean | 9bc07e81 | 2019-07-15 11:48:47 +0300 | [diff] [blame] | 78 | if (priv->if_type == PHY_INTERFACE_MODE_SGMII_2500) |
| 79 | is2500 = true; |
| 80 | |
| 81 | /* |
| 82 | * Set to SGMII mode, for 1Gbps enable AN, for 2.5Gbps set fixed speed. |
| 83 | * Although fixed speed is 1Gbps, we could be running at 2.5Gbps based |
| 84 | * on PLL configuration. Setting 1G for 2.5G here is counter intuitive |
| 85 | * but intentional. |
| 86 | */ |
| 87 | reg = ENETC_PCS_IF_MODE_SGMII; |
| 88 | reg |= is2500 ? ENETC_PCS_IF_MODE_SPEED_1G : ENETC_PCS_IF_MODE_SGMII_AN; |
Alex Marginean | e4aafd5 | 2019-07-03 12:11:42 +0300 | [diff] [blame] | 89 | enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE, |
Alex Marginean | 9bc07e81 | 2019-07-15 11:48:47 +0300 | [diff] [blame] | 90 | ENETC_PCS_IF_MODE, reg); |
Alex Marginean | e4aafd5 | 2019-07-03 12:11:42 +0300 | [diff] [blame] | 91 | |
| 92 | /* Dev ability - SGMII */ |
| 93 | enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE, |
| 94 | ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SGMII); |
| 95 | |
| 96 | /* Adjust link timer for SGMII */ |
| 97 | enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE, |
| 98 | ENETC_PCS_LINK_TIMER1, ENETC_PCS_LINK_TIMER1_VAL); |
| 99 | enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE, |
| 100 | ENETC_PCS_LINK_TIMER2, ENETC_PCS_LINK_TIMER2_VAL); |
| 101 | |
Alex Marginean | 9bc07e81 | 2019-07-15 11:48:47 +0300 | [diff] [blame] | 102 | reg = ENETC_PCS_CR_DEF_VAL; |
| 103 | reg |= is2500 ? ENETC_PCS_CR_RST : ENETC_PCS_CR_RESET_AN; |
Alex Marginean | e4aafd5 | 2019-07-03 12:11:42 +0300 | [diff] [blame] | 104 | /* restart PCS AN */ |
| 105 | enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE, |
Alex Marginean | 9bc07e81 | 2019-07-15 11:48:47 +0300 | [diff] [blame] | 106 | ENETC_PCS_CR, reg); |
Alex Marginean | e4aafd5 | 2019-07-03 12:11:42 +0300 | [diff] [blame] | 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | /* set up MAC for RGMII */ |
| 112 | static int enetc_init_rgmii(struct udevice *dev) |
| 113 | { |
| 114 | struct enetc_priv *priv = dev_get_priv(dev); |
| 115 | u32 if_mode; |
| 116 | |
| 117 | /* enable RGMII AN */ |
| 118 | if_mode = enetc_read_port(priv, ENETC_PM_IF_MODE); |
| 119 | if_mode |= ENETC_PM_IF_MODE_AN_ENA; |
| 120 | enetc_write_port(priv, ENETC_PM_IF_MODE, if_mode); |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | /* set up MAC and serdes for SXGMII */ |
| 126 | static int enetc_init_sxgmii(struct udevice *dev) |
| 127 | { |
| 128 | struct enetc_priv *priv = dev_get_priv(dev); |
| 129 | u32 if_mode; |
| 130 | |
| 131 | /* set ifmode to (US)XGMII */ |
| 132 | if_mode = enetc_read_port(priv, ENETC_PM_IF_MODE); |
| 133 | if_mode &= ~ENETC_PM_IF_IFMODE_MASK; |
| 134 | enetc_write_port(priv, ENETC_PM_IF_MODE, if_mode); |
| 135 | |
| 136 | if (!enetc_has_imdio(dev)) |
| 137 | return 0; |
| 138 | |
| 139 | /* Dev ability - SXGMII */ |
| 140 | enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL, |
| 141 | ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SXGMII); |
| 142 | |
| 143 | /* Restart PCS AN */ |
| 144 | enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL, |
| 145 | ENETC_PCS_CR, |
Alex Marginean | 9bc07e81 | 2019-07-15 11:48:47 +0300 | [diff] [blame] | 146 | ENETC_PCS_CR_RST | ENETC_PCS_CR_RESET_AN); |
Alex Marginean | e4aafd5 | 2019-07-03 12:11:42 +0300 | [diff] [blame] | 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | /* Apply protocol specific configuration to MAC, serdes as needed */ |
| 152 | static void enetc_start_pcs(struct udevice *dev) |
| 153 | { |
| 154 | struct enetc_priv *priv = dev_get_priv(dev); |
| 155 | const char *if_str; |
| 156 | |
| 157 | priv->if_type = PHY_INTERFACE_MODE_NONE; |
| 158 | |
| 159 | /* check internal mdio capability, not all ports need it */ |
| 160 | if (enetc_read_port(priv, ENETC_PCAPR0) & ENETC_PCAPRO_MDIO) { |
| 161 | /* |
| 162 | * set up internal MDIO, this is part of ETH PCI function and is |
| 163 | * used to access serdes / internal SoC PHYs. |
| 164 | * We don't currently register it as a MDIO bus as it goes away |
| 165 | * when the interface is removed, so it can't practically be |
| 166 | * used in the console. |
| 167 | */ |
| 168 | priv->imdio.read = enetc_mdio_read; |
| 169 | priv->imdio.write = enetc_mdio_write; |
| 170 | priv->imdio.priv = priv->port_regs + ENETC_PM_IMDIO_BASE; |
| 171 | strncpy(priv->imdio.name, dev->name, MDIO_NAME_LEN); |
| 172 | } |
| 173 | |
| 174 | if (!ofnode_valid(dev->node)) { |
| 175 | enetc_dbg(dev, "no enetc ofnode found, skipping PCS set-up\n"); |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | if_str = ofnode_read_string(dev->node, "phy-mode"); |
| 180 | if (if_str) |
| 181 | priv->if_type = phy_get_interface_by_name(if_str); |
| 182 | else |
| 183 | enetc_dbg(dev, |
| 184 | "phy-mode property not found, defaulting to SGMII\n"); |
| 185 | if (priv->if_type < 0) |
| 186 | priv->if_type = PHY_INTERFACE_MODE_NONE; |
| 187 | |
| 188 | switch (priv->if_type) { |
| 189 | case PHY_INTERFACE_MODE_SGMII: |
Alex Marginean | 9bc07e81 | 2019-07-15 11:48:47 +0300 | [diff] [blame] | 190 | case PHY_INTERFACE_MODE_SGMII_2500: |
Alex Marginean | e4aafd5 | 2019-07-03 12:11:42 +0300 | [diff] [blame] | 191 | enetc_init_sgmii(dev); |
| 192 | break; |
| 193 | case PHY_INTERFACE_MODE_RGMII: |
| 194 | enetc_init_rgmii(dev); |
| 195 | break; |
| 196 | case PHY_INTERFACE_MODE_XGMII: |
| 197 | enetc_init_sxgmii(dev); |
| 198 | break; |
| 199 | }; |
| 200 | } |
| 201 | |
Alex Marginean | 1d99534 | 2019-07-03 12:11:41 +0300 | [diff] [blame] | 202 | /* Configure the actual/external ethernet PHY, if one is found */ |
| 203 | static void enetc_start_phy(struct udevice *dev) |
| 204 | { |
| 205 | struct enetc_priv *priv = dev_get_priv(dev); |
| 206 | struct udevice *miidev; |
| 207 | struct phy_device *phy; |
| 208 | u32 phandle, phy_id; |
| 209 | ofnode phy_node; |
| 210 | int supported; |
| 211 | |
| 212 | if (!ofnode_valid(dev->node)) { |
| 213 | enetc_dbg(dev, "no enetc ofnode found, skipping PHY set-up\n"); |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | if (ofnode_read_u32(dev->node, "phy-handle", &phandle)) { |
| 218 | enetc_dbg(dev, "phy-handle not found, skipping PHY set-up\n"); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | phy_node = ofnode_get_by_phandle(phandle); |
| 223 | if (!ofnode_valid(phy_node)) { |
| 224 | enetc_dbg(dev, "invalid phy node, skipping PHY set-up\n"); |
| 225 | return; |
| 226 | } |
| 227 | enetc_dbg(dev, "phy node: %s\n", ofnode_get_name(phy_node)); |
| 228 | |
| 229 | if (ofnode_read_u32(phy_node, "reg", &phy_id)) { |
| 230 | enetc_dbg(dev, |
| 231 | "missing reg in PHY node, skipping PHY set-up\n"); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | if (uclass_get_device_by_ofnode(UCLASS_MDIO, |
| 236 | ofnode_get_parent(phy_node), |
| 237 | &miidev)) { |
| 238 | enetc_dbg(dev, "can't find MDIO bus for node %s\n", |
| 239 | ofnode_get_name(ofnode_get_parent(phy_node))); |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | phy = dm_mdio_phy_connect(miidev, phy_id, dev, priv->if_type); |
| 244 | if (!phy) { |
| 245 | enetc_dbg(dev, "dm_mdio_phy_connect returned null\n"); |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | supported = GENMASK(6, 0); /* speeds up to 1G & AN */ |
| 250 | phy->advertising = phy->supported & supported; |
| 251 | phy_config(phy); |
| 252 | phy_startup(phy); |
| 253 | } |
| 254 | |
Alex Marginean | 120b5ef | 2019-07-03 12:11:40 +0300 | [diff] [blame] | 255 | /* |
| 256 | * Probe ENETC driver: |
| 257 | * - initialize port and station interface BARs |
| 258 | */ |
| 259 | static int enetc_probe(struct udevice *dev) |
| 260 | { |
| 261 | struct enetc_priv *priv = dev_get_priv(dev); |
| 262 | |
| 263 | if (ofnode_valid(dev->node) && !ofnode_is_available(dev->node)) { |
| 264 | enetc_dbg(dev, "interface disabled\n"); |
| 265 | return -ENODEV; |
| 266 | } |
| 267 | |
| 268 | priv->enetc_txbd = memalign(ENETC_BD_ALIGN, |
| 269 | sizeof(struct enetc_tx_bd) * ENETC_BD_CNT); |
| 270 | priv->enetc_rxbd = memalign(ENETC_BD_ALIGN, |
| 271 | sizeof(union enetc_rx_bd) * ENETC_BD_CNT); |
| 272 | |
| 273 | if (!priv->enetc_txbd || !priv->enetc_rxbd) { |
| 274 | /* free should be able to handle NULL, just free all pointers */ |
| 275 | free(priv->enetc_txbd); |
| 276 | free(priv->enetc_rxbd); |
| 277 | |
| 278 | return -ENOMEM; |
| 279 | } |
| 280 | |
| 281 | /* initialize register */ |
| 282 | priv->regs_base = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0, 0); |
| 283 | if (!priv->regs_base) { |
| 284 | enetc_dbg(dev, "failed to map BAR0\n"); |
| 285 | return -EINVAL; |
| 286 | } |
| 287 | priv->port_regs = priv->regs_base + ENETC_PORT_REGS_OFF; |
| 288 | |
| 289 | dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY); |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * Remove the driver from an interface: |
| 296 | * - free up allocated memory |
| 297 | */ |
| 298 | static int enetc_remove(struct udevice *dev) |
| 299 | { |
| 300 | struct enetc_priv *priv = dev_get_priv(dev); |
| 301 | |
| 302 | free(priv->enetc_txbd); |
| 303 | free(priv->enetc_rxbd); |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | /* ENETC Port MAC address registers, accepts big-endian format */ |
| 309 | static void enetc_set_primary_mac_addr(struct enetc_priv *priv, const u8 *addr) |
| 310 | { |
| 311 | u16 lower = *(const u16 *)(addr + 4); |
| 312 | u32 upper = *(const u32 *)addr; |
| 313 | |
| 314 | enetc_write_port(priv, ENETC_PSIPMAR0, upper); |
| 315 | enetc_write_port(priv, ENETC_PSIPMAR1, lower); |
| 316 | } |
| 317 | |
| 318 | /* Configure port parameters (# of rings, frame size, enable port) */ |
| 319 | static void enetc_enable_si_port(struct enetc_priv *priv) |
| 320 | { |
| 321 | u32 val; |
| 322 | |
| 323 | /* set Rx/Tx BDR count */ |
| 324 | val = ENETC_PSICFGR_SET_TXBDR(ENETC_TX_BDR_CNT); |
| 325 | val |= ENETC_PSICFGR_SET_RXBDR(ENETC_RX_BDR_CNT); |
| 326 | enetc_write_port(priv, ENETC_PSICFGR(0), val); |
| 327 | /* set Rx max frame size */ |
| 328 | enetc_write_port(priv, ENETC_PM_MAXFRM, ENETC_RX_MAXFRM_SIZE); |
| 329 | /* enable MAC port */ |
| 330 | enetc_write_port(priv, ENETC_PM_CC, ENETC_PM_CC_RX_TX_EN); |
| 331 | /* enable port */ |
| 332 | enetc_write_port(priv, ENETC_PMR, ENETC_PMR_SI0_EN); |
| 333 | /* set SI cache policy */ |
| 334 | enetc_write(priv, ENETC_SICAR0, |
| 335 | ENETC_SICAR_RD_CFG | ENETC_SICAR_WR_CFG); |
| 336 | /* enable SI */ |
| 337 | enetc_write(priv, ENETC_SIMR, ENETC_SIMR_EN); |
| 338 | } |
| 339 | |
| 340 | /* returns DMA address for a given buffer index */ |
| 341 | static inline u64 enetc_rxb_address(struct udevice *dev, int i) |
| 342 | { |
| 343 | return cpu_to_le64(dm_pci_virt_to_mem(dev, net_rx_packets[i])); |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * Setup a single Tx BD Ring (ID = 0): |
| 348 | * - set Tx buffer descriptor address |
| 349 | * - set the BD count |
| 350 | * - initialize the producer and consumer index |
| 351 | */ |
| 352 | static void enetc_setup_tx_bdr(struct udevice *dev) |
| 353 | { |
| 354 | struct enetc_priv *priv = dev_get_priv(dev); |
| 355 | struct bd_ring *tx_bdr = &priv->tx_bdr; |
| 356 | u64 tx_bd_add = (u64)priv->enetc_txbd; |
| 357 | |
| 358 | /* used later to advance to the next Tx BD */ |
| 359 | tx_bdr->bd_count = ENETC_BD_CNT; |
| 360 | tx_bdr->next_prod_idx = 0; |
| 361 | tx_bdr->next_cons_idx = 0; |
| 362 | tx_bdr->cons_idx = priv->regs_base + |
| 363 | ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBCIR); |
| 364 | tx_bdr->prod_idx = priv->regs_base + |
| 365 | ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBPIR); |
| 366 | |
| 367 | /* set Tx BD address */ |
| 368 | enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR0, |
| 369 | lower_32_bits(tx_bd_add)); |
| 370 | enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR1, |
| 371 | upper_32_bits(tx_bd_add)); |
| 372 | /* set Tx 8 BD count */ |
| 373 | enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBLENR, |
| 374 | tx_bdr->bd_count); |
| 375 | |
| 376 | /* reset both producer/consumer indexes */ |
| 377 | enetc_write_reg(tx_bdr->cons_idx, tx_bdr->next_cons_idx); |
| 378 | enetc_write_reg(tx_bdr->prod_idx, tx_bdr->next_prod_idx); |
| 379 | |
| 380 | /* enable TX ring */ |
| 381 | enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBMR, ENETC_TBMR_EN); |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * Setup a single Rx BD Ring (ID = 0): |
| 386 | * - set Rx buffer descriptors address (one descriptor per buffer) |
| 387 | * - set buffer size as max frame size |
| 388 | * - enable Rx ring |
| 389 | * - reset consumer and producer indexes |
| 390 | * - set buffer for each descriptor |
| 391 | */ |
| 392 | static void enetc_setup_rx_bdr(struct udevice *dev) |
| 393 | { |
| 394 | struct enetc_priv *priv = dev_get_priv(dev); |
| 395 | struct bd_ring *rx_bdr = &priv->rx_bdr; |
| 396 | u64 rx_bd_add = (u64)priv->enetc_rxbd; |
| 397 | int i; |
| 398 | |
| 399 | /* used later to advance to the next BD produced by ENETC HW */ |
| 400 | rx_bdr->bd_count = ENETC_BD_CNT; |
| 401 | rx_bdr->next_prod_idx = 0; |
| 402 | rx_bdr->next_cons_idx = 0; |
| 403 | rx_bdr->cons_idx = priv->regs_base + |
| 404 | ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBCIR); |
| 405 | rx_bdr->prod_idx = priv->regs_base + |
| 406 | ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBPIR); |
| 407 | |
| 408 | /* set Rx BD address */ |
| 409 | enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR0, |
| 410 | lower_32_bits(rx_bd_add)); |
| 411 | enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR1, |
| 412 | upper_32_bits(rx_bd_add)); |
| 413 | /* set Rx BD count (multiple of 8) */ |
| 414 | enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBLENR, |
| 415 | rx_bdr->bd_count); |
| 416 | /* set Rx buffer size */ |
| 417 | enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBSR, PKTSIZE_ALIGN); |
| 418 | |
| 419 | /* fill Rx BD */ |
| 420 | memset(priv->enetc_rxbd, 0, |
| 421 | rx_bdr->bd_count * sizeof(union enetc_rx_bd)); |
| 422 | for (i = 0; i < rx_bdr->bd_count; i++) { |
| 423 | priv->enetc_rxbd[i].w.addr = enetc_rxb_address(dev, i); |
| 424 | /* each RX buffer must be aligned to 64B */ |
| 425 | WARN_ON(priv->enetc_rxbd[i].w.addr & (ARCH_DMA_MINALIGN - 1)); |
| 426 | } |
| 427 | |
| 428 | /* reset producer (ENETC owned) and consumer (SW owned) index */ |
| 429 | enetc_write_reg(rx_bdr->cons_idx, rx_bdr->next_cons_idx); |
| 430 | enetc_write_reg(rx_bdr->prod_idx, rx_bdr->next_prod_idx); |
| 431 | |
| 432 | /* enable Rx ring */ |
| 433 | enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBMR, ENETC_RBMR_EN); |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * Start ENETC interface: |
| 438 | * - perform FLR |
| 439 | * - enable access to port and SI registers |
| 440 | * - set mac address |
| 441 | * - setup TX/RX buffer descriptors |
| 442 | * - enable Tx/Rx rings |
| 443 | */ |
| 444 | static int enetc_start(struct udevice *dev) |
| 445 | { |
| 446 | struct eth_pdata *plat = dev_get_platdata(dev); |
| 447 | struct enetc_priv *priv = dev_get_priv(dev); |
| 448 | |
| 449 | /* reset and enable the PCI device */ |
| 450 | dm_pci_flr(dev); |
| 451 | dm_pci_clrset_config16(dev, PCI_COMMAND, 0, |
| 452 | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER); |
| 453 | |
| 454 | if (!is_valid_ethaddr(plat->enetaddr)) { |
| 455 | enetc_dbg(dev, "invalid MAC address, generate random ...\n"); |
| 456 | net_random_ethaddr(plat->enetaddr); |
| 457 | } |
| 458 | enetc_set_primary_mac_addr(priv, plat->enetaddr); |
| 459 | |
| 460 | enetc_enable_si_port(priv); |
| 461 | |
| 462 | /* setup Tx/Rx buffer descriptors */ |
| 463 | enetc_setup_tx_bdr(dev); |
| 464 | enetc_setup_rx_bdr(dev); |
| 465 | |
Alex Marginean | e4aafd5 | 2019-07-03 12:11:42 +0300 | [diff] [blame] | 466 | enetc_start_pcs(dev); |
Alex Marginean | 1d99534 | 2019-07-03 12:11:41 +0300 | [diff] [blame] | 467 | enetc_start_phy(dev); |
| 468 | |
Alex Marginean | 120b5ef | 2019-07-03 12:11:40 +0300 | [diff] [blame] | 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | /* |
| 473 | * Stop the network interface: |
| 474 | * - just quiesce it, we can wipe all configuration as _start starts from |
| 475 | * scratch each time |
| 476 | */ |
| 477 | static void enetc_stop(struct udevice *dev) |
| 478 | { |
| 479 | /* FLR is sufficient to quiesce the device */ |
| 480 | dm_pci_flr(dev); |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * ENETC transmit packet: |
| 485 | * - check if Tx BD ring is full |
| 486 | * - set buffer/packet address (dma address) |
| 487 | * - set final fragment flag |
| 488 | * - try while producer index equals consumer index or timeout |
| 489 | */ |
| 490 | static int enetc_send(struct udevice *dev, void *packet, int length) |
| 491 | { |
| 492 | struct enetc_priv *priv = dev_get_priv(dev); |
| 493 | struct bd_ring *txr = &priv->tx_bdr; |
| 494 | void *nv_packet = (void *)packet; |
| 495 | int tries = ENETC_POLL_TRIES; |
| 496 | u32 pi, ci; |
| 497 | |
| 498 | pi = txr->next_prod_idx; |
| 499 | ci = enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK; |
| 500 | /* Tx ring is full when */ |
| 501 | if (((pi + 1) % txr->bd_count) == ci) { |
| 502 | enetc_dbg(dev, "Tx BDR full\n"); |
| 503 | return -ETIMEDOUT; |
| 504 | } |
| 505 | enetc_dbg(dev, "TxBD[%d]send: pkt_len=%d, buff @0x%x%08x\n", pi, length, |
| 506 | upper_32_bits((u64)nv_packet), lower_32_bits((u64)nv_packet)); |
| 507 | |
| 508 | /* prepare Tx BD */ |
| 509 | memset(&priv->enetc_txbd[pi], 0x0, sizeof(struct enetc_tx_bd)); |
| 510 | priv->enetc_txbd[pi].addr = |
| 511 | cpu_to_le64(dm_pci_virt_to_mem(dev, nv_packet)); |
| 512 | priv->enetc_txbd[pi].buf_len = cpu_to_le16(length); |
| 513 | priv->enetc_txbd[pi].frm_len = cpu_to_le16(length); |
| 514 | priv->enetc_txbd[pi].flags = cpu_to_le16(ENETC_TXBD_FLAGS_F); |
| 515 | dmb(); |
| 516 | /* send frame: increment producer index */ |
| 517 | pi = (pi + 1) % txr->bd_count; |
| 518 | txr->next_prod_idx = pi; |
| 519 | enetc_write_reg(txr->prod_idx, pi); |
| 520 | while ((--tries >= 0) && |
| 521 | (pi != (enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK))) |
| 522 | udelay(10); |
| 523 | |
| 524 | return tries > 0 ? 0 : -ETIMEDOUT; |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | * Receive frame: |
| 529 | * - wait for the next BD to get ready bit set |
| 530 | * - clean up the descriptor |
| 531 | * - move on and indicate to HW that the cleaned BD is available for Rx |
| 532 | */ |
| 533 | static int enetc_recv(struct udevice *dev, int flags, uchar **packetp) |
| 534 | { |
| 535 | struct enetc_priv *priv = dev_get_priv(dev); |
| 536 | struct bd_ring *rxr = &priv->rx_bdr; |
| 537 | int tries = ENETC_POLL_TRIES; |
| 538 | int pi = rxr->next_prod_idx; |
| 539 | int ci = rxr->next_cons_idx; |
| 540 | u32 status; |
| 541 | int len; |
| 542 | u8 rdy; |
| 543 | |
| 544 | do { |
| 545 | dmb(); |
| 546 | status = le32_to_cpu(priv->enetc_rxbd[pi].r.lstatus); |
| 547 | /* check if current BD is ready to be consumed */ |
| 548 | rdy = ENETC_RXBD_STATUS_R(status); |
| 549 | } while (--tries >= 0 && !rdy); |
| 550 | |
| 551 | if (!rdy) |
| 552 | return -EAGAIN; |
| 553 | |
| 554 | dmb(); |
| 555 | len = le16_to_cpu(priv->enetc_rxbd[pi].r.buf_len); |
| 556 | *packetp = (uchar *)enetc_rxb_address(dev, pi); |
| 557 | enetc_dbg(dev, "RxBD[%d]: len=%d err=%d pkt=0x%x%08x\n", pi, len, |
| 558 | ENETC_RXBD_STATUS_ERRORS(status), |
| 559 | upper_32_bits((u64)*packetp), lower_32_bits((u64)*packetp)); |
| 560 | |
| 561 | /* BD clean up and advance to next in ring */ |
| 562 | memset(&priv->enetc_rxbd[pi], 0, sizeof(union enetc_rx_bd)); |
| 563 | priv->enetc_rxbd[pi].w.addr = enetc_rxb_address(dev, pi); |
| 564 | rxr->next_prod_idx = (pi + 1) % rxr->bd_count; |
| 565 | ci = (ci + 1) % rxr->bd_count; |
| 566 | rxr->next_cons_idx = ci; |
| 567 | dmb(); |
| 568 | /* free up the slot in the ring for HW */ |
| 569 | enetc_write_reg(rxr->cons_idx, ci); |
| 570 | |
| 571 | return len; |
| 572 | } |
| 573 | |
| 574 | static const struct eth_ops enetc_ops = { |
| 575 | .start = enetc_start, |
| 576 | .send = enetc_send, |
| 577 | .recv = enetc_recv, |
| 578 | .stop = enetc_stop, |
| 579 | }; |
| 580 | |
| 581 | U_BOOT_DRIVER(eth_enetc) = { |
| 582 | .name = "enetc_eth", |
| 583 | .id = UCLASS_ETH, |
| 584 | .bind = enetc_bind, |
| 585 | .probe = enetc_probe, |
| 586 | .remove = enetc_remove, |
| 587 | .ops = &enetc_ops, |
| 588 | .priv_auto_alloc_size = sizeof(struct enetc_priv), |
| 589 | .platdata_auto_alloc_size = sizeof(struct eth_pdata), |
| 590 | }; |
| 591 | |
| 592 | static struct pci_device_id enetc_ids[] = { |
| 593 | { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, PCI_DEVICE_ID_ENETC_ETH) }, |
| 594 | {} |
| 595 | }; |
| 596 | |
| 597 | U_BOOT_PCI_DEVICE(eth_enetc, enetc_ids); |