Alex Marginean | 7a910c1 | 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 | 0215539 | 2019-07-03 12:11:41 +0300 | [diff] [blame] | 13 | #include <miiphy.h> |
Alex Marginean | 7a910c1 | 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 | 38882ae | 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 | 41a7ac5 | 2019-07-15 11:48:47 +0300 | [diff] [blame] | 72 | bool is2500 = false; |
| 73 | u16 reg; |
Alex Marginean | 38882ae | 2019-07-03 12:11:42 +0300 | [diff] [blame] | 74 | |
| 75 | if (!enetc_has_imdio(dev)) |
| 76 | return 0; |
| 77 | |
Alex Marginean | 41a7ac5 | 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 | 38882ae | 2019-07-03 12:11:42 +0300 | [diff] [blame] | 89 | enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE, |
Alex Marginean | 41a7ac5 | 2019-07-15 11:48:47 +0300 | [diff] [blame] | 90 | ENETC_PCS_IF_MODE, reg); |
Alex Marginean | 38882ae | 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 | 41a7ac5 | 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 | 38882ae | 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 | 41a7ac5 | 2019-07-15 11:48:47 +0300 | [diff] [blame] | 106 | ENETC_PCS_CR, reg); |
Alex Marginean | 38882ae | 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 | 41a7ac5 | 2019-07-15 11:48:47 +0300 | [diff] [blame] | 146 | ENETC_PCS_CR_RST | ENETC_PCS_CR_RESET_AN); |
Alex Marginean | 38882ae | 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 | 41a7ac5 | 2019-07-15 11:48:47 +0300 | [diff] [blame] | 190 | case PHY_INTERFACE_MODE_SGMII_2500: |
Alex Marginean | 38882ae | 2019-07-03 12:11:42 +0300 | [diff] [blame] | 191 | enetc_init_sgmii(dev); |
| 192 | break; |
| 193 | case PHY_INTERFACE_MODE_RGMII: |
Michael Walle | 2acda73 | 2019-10-26 02:39:12 +0200 | [diff] [blame] | 194 | case PHY_INTERFACE_MODE_RGMII_ID: |
| 195 | case PHY_INTERFACE_MODE_RGMII_RXID: |
| 196 | case PHY_INTERFACE_MODE_RGMII_TXID: |
Alex Marginean | 38882ae | 2019-07-03 12:11:42 +0300 | [diff] [blame] | 197 | enetc_init_rgmii(dev); |
| 198 | break; |
| 199 | case PHY_INTERFACE_MODE_XGMII: |
Alex Marginean | ed0460c | 2019-11-14 18:28:38 +0200 | [diff] [blame] | 200 | case PHY_INTERFACE_MODE_USXGMII: |
| 201 | case PHY_INTERFACE_MODE_XFI: |
Alex Marginean | 38882ae | 2019-07-03 12:11:42 +0300 | [diff] [blame] | 202 | enetc_init_sxgmii(dev); |
| 203 | break; |
| 204 | }; |
| 205 | } |
| 206 | |
Alex Marginean | 0215539 | 2019-07-03 12:11:41 +0300 | [diff] [blame] | 207 | /* Configure the actual/external ethernet PHY, if one is found */ |
Alex Marginean | 602e00f | 2019-11-25 17:15:13 +0200 | [diff] [blame] | 208 | static void enetc_config_phy(struct udevice *dev) |
Alex Marginean | 0215539 | 2019-07-03 12:11:41 +0300 | [diff] [blame] | 209 | { |
| 210 | struct enetc_priv *priv = dev_get_priv(dev); |
Alex Marginean | 0215539 | 2019-07-03 12:11:41 +0300 | [diff] [blame] | 211 | int supported; |
| 212 | |
Alex Marginean | 602e00f | 2019-11-25 17:15:13 +0200 | [diff] [blame] | 213 | priv->phy = dm_eth_phy_connect(dev); |
Alex Marginean | 0215539 | 2019-07-03 12:11:41 +0300 | [diff] [blame] | 214 | |
Alex Marginean | 602e00f | 2019-11-25 17:15:13 +0200 | [diff] [blame] | 215 | if (!priv->phy) |
Alex Marginean | 0215539 | 2019-07-03 12:11:41 +0300 | [diff] [blame] | 216 | return; |
Alex Marginean | 0215539 | 2019-07-03 12:11:41 +0300 | [diff] [blame] | 217 | |
Alex Marginean | b93375c | 2019-11-14 18:58:45 +0200 | [diff] [blame^] | 218 | supported = PHY_GBIT_FEATURES | SUPPORTED_2500baseX_Full; |
| 219 | priv->phy->supported &= supported; |
| 220 | priv->phy->advertising &= supported; |
Alex Marginean | 602e00f | 2019-11-25 17:15:13 +0200 | [diff] [blame] | 221 | |
| 222 | phy_config(priv->phy); |
Alex Marginean | 0215539 | 2019-07-03 12:11:41 +0300 | [diff] [blame] | 223 | } |
| 224 | |
Alex Marginean | 7a910c1 | 2019-07-03 12:11:40 +0300 | [diff] [blame] | 225 | /* |
| 226 | * Probe ENETC driver: |
| 227 | * - initialize port and station interface BARs |
| 228 | */ |
| 229 | static int enetc_probe(struct udevice *dev) |
| 230 | { |
| 231 | struct enetc_priv *priv = dev_get_priv(dev); |
| 232 | |
| 233 | if (ofnode_valid(dev->node) && !ofnode_is_available(dev->node)) { |
| 234 | enetc_dbg(dev, "interface disabled\n"); |
| 235 | return -ENODEV; |
| 236 | } |
| 237 | |
| 238 | priv->enetc_txbd = memalign(ENETC_BD_ALIGN, |
| 239 | sizeof(struct enetc_tx_bd) * ENETC_BD_CNT); |
| 240 | priv->enetc_rxbd = memalign(ENETC_BD_ALIGN, |
| 241 | sizeof(union enetc_rx_bd) * ENETC_BD_CNT); |
| 242 | |
| 243 | if (!priv->enetc_txbd || !priv->enetc_rxbd) { |
| 244 | /* free should be able to handle NULL, just free all pointers */ |
| 245 | free(priv->enetc_txbd); |
| 246 | free(priv->enetc_rxbd); |
| 247 | |
| 248 | return -ENOMEM; |
| 249 | } |
| 250 | |
| 251 | /* initialize register */ |
| 252 | priv->regs_base = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0, 0); |
| 253 | if (!priv->regs_base) { |
| 254 | enetc_dbg(dev, "failed to map BAR0\n"); |
| 255 | return -EINVAL; |
| 256 | } |
| 257 | priv->port_regs = priv->regs_base + ENETC_PORT_REGS_OFF; |
| 258 | |
| 259 | dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY); |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Remove the driver from an interface: |
| 266 | * - free up allocated memory |
| 267 | */ |
| 268 | static int enetc_remove(struct udevice *dev) |
| 269 | { |
| 270 | struct enetc_priv *priv = dev_get_priv(dev); |
| 271 | |
| 272 | free(priv->enetc_txbd); |
| 273 | free(priv->enetc_rxbd); |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | /* ENETC Port MAC address registers, accepts big-endian format */ |
| 279 | static void enetc_set_primary_mac_addr(struct enetc_priv *priv, const u8 *addr) |
| 280 | { |
| 281 | u16 lower = *(const u16 *)(addr + 4); |
| 282 | u32 upper = *(const u32 *)addr; |
| 283 | |
| 284 | enetc_write_port(priv, ENETC_PSIPMAR0, upper); |
| 285 | enetc_write_port(priv, ENETC_PSIPMAR1, lower); |
| 286 | } |
| 287 | |
| 288 | /* Configure port parameters (# of rings, frame size, enable port) */ |
| 289 | static void enetc_enable_si_port(struct enetc_priv *priv) |
| 290 | { |
| 291 | u32 val; |
| 292 | |
| 293 | /* set Rx/Tx BDR count */ |
| 294 | val = ENETC_PSICFGR_SET_TXBDR(ENETC_TX_BDR_CNT); |
| 295 | val |= ENETC_PSICFGR_SET_RXBDR(ENETC_RX_BDR_CNT); |
| 296 | enetc_write_port(priv, ENETC_PSICFGR(0), val); |
| 297 | /* set Rx max frame size */ |
| 298 | enetc_write_port(priv, ENETC_PM_MAXFRM, ENETC_RX_MAXFRM_SIZE); |
| 299 | /* enable MAC port */ |
| 300 | enetc_write_port(priv, ENETC_PM_CC, ENETC_PM_CC_RX_TX_EN); |
| 301 | /* enable port */ |
| 302 | enetc_write_port(priv, ENETC_PMR, ENETC_PMR_SI0_EN); |
| 303 | /* set SI cache policy */ |
| 304 | enetc_write(priv, ENETC_SICAR0, |
| 305 | ENETC_SICAR_RD_CFG | ENETC_SICAR_WR_CFG); |
| 306 | /* enable SI */ |
| 307 | enetc_write(priv, ENETC_SIMR, ENETC_SIMR_EN); |
| 308 | } |
| 309 | |
| 310 | /* returns DMA address for a given buffer index */ |
| 311 | static inline u64 enetc_rxb_address(struct udevice *dev, int i) |
| 312 | { |
| 313 | return cpu_to_le64(dm_pci_virt_to_mem(dev, net_rx_packets[i])); |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * Setup a single Tx BD Ring (ID = 0): |
| 318 | * - set Tx buffer descriptor address |
| 319 | * - set the BD count |
| 320 | * - initialize the producer and consumer index |
| 321 | */ |
| 322 | static void enetc_setup_tx_bdr(struct udevice *dev) |
| 323 | { |
| 324 | struct enetc_priv *priv = dev_get_priv(dev); |
| 325 | struct bd_ring *tx_bdr = &priv->tx_bdr; |
| 326 | u64 tx_bd_add = (u64)priv->enetc_txbd; |
| 327 | |
| 328 | /* used later to advance to the next Tx BD */ |
| 329 | tx_bdr->bd_count = ENETC_BD_CNT; |
| 330 | tx_bdr->next_prod_idx = 0; |
| 331 | tx_bdr->next_cons_idx = 0; |
| 332 | tx_bdr->cons_idx = priv->regs_base + |
| 333 | ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBCIR); |
| 334 | tx_bdr->prod_idx = priv->regs_base + |
| 335 | ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBPIR); |
| 336 | |
| 337 | /* set Tx BD address */ |
| 338 | enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR0, |
| 339 | lower_32_bits(tx_bd_add)); |
| 340 | enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR1, |
| 341 | upper_32_bits(tx_bd_add)); |
| 342 | /* set Tx 8 BD count */ |
| 343 | enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBLENR, |
| 344 | tx_bdr->bd_count); |
| 345 | |
| 346 | /* reset both producer/consumer indexes */ |
| 347 | enetc_write_reg(tx_bdr->cons_idx, tx_bdr->next_cons_idx); |
| 348 | enetc_write_reg(tx_bdr->prod_idx, tx_bdr->next_prod_idx); |
| 349 | |
| 350 | /* enable TX ring */ |
| 351 | enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBMR, ENETC_TBMR_EN); |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | * Setup a single Rx BD Ring (ID = 0): |
| 356 | * - set Rx buffer descriptors address (one descriptor per buffer) |
| 357 | * - set buffer size as max frame size |
| 358 | * - enable Rx ring |
| 359 | * - reset consumer and producer indexes |
| 360 | * - set buffer for each descriptor |
| 361 | */ |
| 362 | static void enetc_setup_rx_bdr(struct udevice *dev) |
| 363 | { |
| 364 | struct enetc_priv *priv = dev_get_priv(dev); |
| 365 | struct bd_ring *rx_bdr = &priv->rx_bdr; |
| 366 | u64 rx_bd_add = (u64)priv->enetc_rxbd; |
| 367 | int i; |
| 368 | |
| 369 | /* used later to advance to the next BD produced by ENETC HW */ |
| 370 | rx_bdr->bd_count = ENETC_BD_CNT; |
| 371 | rx_bdr->next_prod_idx = 0; |
| 372 | rx_bdr->next_cons_idx = 0; |
| 373 | rx_bdr->cons_idx = priv->regs_base + |
| 374 | ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBCIR); |
| 375 | rx_bdr->prod_idx = priv->regs_base + |
| 376 | ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBPIR); |
| 377 | |
| 378 | /* set Rx BD address */ |
| 379 | enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR0, |
| 380 | lower_32_bits(rx_bd_add)); |
| 381 | enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR1, |
| 382 | upper_32_bits(rx_bd_add)); |
| 383 | /* set Rx BD count (multiple of 8) */ |
| 384 | enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBLENR, |
| 385 | rx_bdr->bd_count); |
| 386 | /* set Rx buffer size */ |
| 387 | enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBSR, PKTSIZE_ALIGN); |
| 388 | |
| 389 | /* fill Rx BD */ |
| 390 | memset(priv->enetc_rxbd, 0, |
| 391 | rx_bdr->bd_count * sizeof(union enetc_rx_bd)); |
| 392 | for (i = 0; i < rx_bdr->bd_count; i++) { |
| 393 | priv->enetc_rxbd[i].w.addr = enetc_rxb_address(dev, i); |
| 394 | /* each RX buffer must be aligned to 64B */ |
| 395 | WARN_ON(priv->enetc_rxbd[i].w.addr & (ARCH_DMA_MINALIGN - 1)); |
| 396 | } |
| 397 | |
| 398 | /* reset producer (ENETC owned) and consumer (SW owned) index */ |
| 399 | enetc_write_reg(rx_bdr->cons_idx, rx_bdr->next_cons_idx); |
| 400 | enetc_write_reg(rx_bdr->prod_idx, rx_bdr->next_prod_idx); |
| 401 | |
| 402 | /* enable Rx ring */ |
| 403 | enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBMR, ENETC_RBMR_EN); |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * Start ENETC interface: |
| 408 | * - perform FLR |
| 409 | * - enable access to port and SI registers |
| 410 | * - set mac address |
| 411 | * - setup TX/RX buffer descriptors |
| 412 | * - enable Tx/Rx rings |
| 413 | */ |
| 414 | static int enetc_start(struct udevice *dev) |
| 415 | { |
| 416 | struct eth_pdata *plat = dev_get_platdata(dev); |
| 417 | struct enetc_priv *priv = dev_get_priv(dev); |
| 418 | |
| 419 | /* reset and enable the PCI device */ |
| 420 | dm_pci_flr(dev); |
| 421 | dm_pci_clrset_config16(dev, PCI_COMMAND, 0, |
| 422 | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER); |
| 423 | |
| 424 | if (!is_valid_ethaddr(plat->enetaddr)) { |
| 425 | enetc_dbg(dev, "invalid MAC address, generate random ...\n"); |
| 426 | net_random_ethaddr(plat->enetaddr); |
| 427 | } |
| 428 | enetc_set_primary_mac_addr(priv, plat->enetaddr); |
| 429 | |
| 430 | enetc_enable_si_port(priv); |
| 431 | |
| 432 | /* setup Tx/Rx buffer descriptors */ |
| 433 | enetc_setup_tx_bdr(dev); |
| 434 | enetc_setup_rx_bdr(dev); |
| 435 | |
Alex Marginean | 38882ae | 2019-07-03 12:11:42 +0300 | [diff] [blame] | 436 | enetc_start_pcs(dev); |
Alex Marginean | 602e00f | 2019-11-25 17:15:13 +0200 | [diff] [blame] | 437 | enetc_config_phy(dev); |
| 438 | if (priv->phy) |
| 439 | phy_startup(priv->phy); |
Alex Marginean | 0215539 | 2019-07-03 12:11:41 +0300 | [diff] [blame] | 440 | |
Alex Marginean | 7a910c1 | 2019-07-03 12:11:40 +0300 | [diff] [blame] | 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | * Stop the network interface: |
| 446 | * - just quiesce it, we can wipe all configuration as _start starts from |
| 447 | * scratch each time |
| 448 | */ |
| 449 | static void enetc_stop(struct udevice *dev) |
| 450 | { |
| 451 | /* FLR is sufficient to quiesce the device */ |
| 452 | dm_pci_flr(dev); |
| 453 | } |
| 454 | |
| 455 | /* |
| 456 | * ENETC transmit packet: |
| 457 | * - check if Tx BD ring is full |
| 458 | * - set buffer/packet address (dma address) |
| 459 | * - set final fragment flag |
| 460 | * - try while producer index equals consumer index or timeout |
| 461 | */ |
| 462 | static int enetc_send(struct udevice *dev, void *packet, int length) |
| 463 | { |
| 464 | struct enetc_priv *priv = dev_get_priv(dev); |
| 465 | struct bd_ring *txr = &priv->tx_bdr; |
| 466 | void *nv_packet = (void *)packet; |
| 467 | int tries = ENETC_POLL_TRIES; |
| 468 | u32 pi, ci; |
| 469 | |
| 470 | pi = txr->next_prod_idx; |
| 471 | ci = enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK; |
| 472 | /* Tx ring is full when */ |
| 473 | if (((pi + 1) % txr->bd_count) == ci) { |
| 474 | enetc_dbg(dev, "Tx BDR full\n"); |
| 475 | return -ETIMEDOUT; |
| 476 | } |
| 477 | enetc_dbg(dev, "TxBD[%d]send: pkt_len=%d, buff @0x%x%08x\n", pi, length, |
| 478 | upper_32_bits((u64)nv_packet), lower_32_bits((u64)nv_packet)); |
| 479 | |
| 480 | /* prepare Tx BD */ |
| 481 | memset(&priv->enetc_txbd[pi], 0x0, sizeof(struct enetc_tx_bd)); |
| 482 | priv->enetc_txbd[pi].addr = |
| 483 | cpu_to_le64(dm_pci_virt_to_mem(dev, nv_packet)); |
| 484 | priv->enetc_txbd[pi].buf_len = cpu_to_le16(length); |
| 485 | priv->enetc_txbd[pi].frm_len = cpu_to_le16(length); |
| 486 | priv->enetc_txbd[pi].flags = cpu_to_le16(ENETC_TXBD_FLAGS_F); |
| 487 | dmb(); |
| 488 | /* send frame: increment producer index */ |
| 489 | pi = (pi + 1) % txr->bd_count; |
| 490 | txr->next_prod_idx = pi; |
| 491 | enetc_write_reg(txr->prod_idx, pi); |
| 492 | while ((--tries >= 0) && |
| 493 | (pi != (enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK))) |
| 494 | udelay(10); |
| 495 | |
| 496 | return tries > 0 ? 0 : -ETIMEDOUT; |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | * Receive frame: |
| 501 | * - wait for the next BD to get ready bit set |
| 502 | * - clean up the descriptor |
| 503 | * - move on and indicate to HW that the cleaned BD is available for Rx |
| 504 | */ |
| 505 | static int enetc_recv(struct udevice *dev, int flags, uchar **packetp) |
| 506 | { |
| 507 | struct enetc_priv *priv = dev_get_priv(dev); |
| 508 | struct bd_ring *rxr = &priv->rx_bdr; |
| 509 | int tries = ENETC_POLL_TRIES; |
| 510 | int pi = rxr->next_prod_idx; |
| 511 | int ci = rxr->next_cons_idx; |
| 512 | u32 status; |
| 513 | int len; |
| 514 | u8 rdy; |
| 515 | |
| 516 | do { |
| 517 | dmb(); |
| 518 | status = le32_to_cpu(priv->enetc_rxbd[pi].r.lstatus); |
| 519 | /* check if current BD is ready to be consumed */ |
| 520 | rdy = ENETC_RXBD_STATUS_R(status); |
| 521 | } while (--tries >= 0 && !rdy); |
| 522 | |
| 523 | if (!rdy) |
| 524 | return -EAGAIN; |
| 525 | |
| 526 | dmb(); |
| 527 | len = le16_to_cpu(priv->enetc_rxbd[pi].r.buf_len); |
| 528 | *packetp = (uchar *)enetc_rxb_address(dev, pi); |
| 529 | enetc_dbg(dev, "RxBD[%d]: len=%d err=%d pkt=0x%x%08x\n", pi, len, |
| 530 | ENETC_RXBD_STATUS_ERRORS(status), |
| 531 | upper_32_bits((u64)*packetp), lower_32_bits((u64)*packetp)); |
| 532 | |
| 533 | /* BD clean up and advance to next in ring */ |
| 534 | memset(&priv->enetc_rxbd[pi], 0, sizeof(union enetc_rx_bd)); |
| 535 | priv->enetc_rxbd[pi].w.addr = enetc_rxb_address(dev, pi); |
| 536 | rxr->next_prod_idx = (pi + 1) % rxr->bd_count; |
| 537 | ci = (ci + 1) % rxr->bd_count; |
| 538 | rxr->next_cons_idx = ci; |
| 539 | dmb(); |
| 540 | /* free up the slot in the ring for HW */ |
| 541 | enetc_write_reg(rxr->cons_idx, ci); |
| 542 | |
| 543 | return len; |
| 544 | } |
| 545 | |
| 546 | static const struct eth_ops enetc_ops = { |
| 547 | .start = enetc_start, |
| 548 | .send = enetc_send, |
| 549 | .recv = enetc_recv, |
| 550 | .stop = enetc_stop, |
| 551 | }; |
| 552 | |
| 553 | U_BOOT_DRIVER(eth_enetc) = { |
| 554 | .name = "enetc_eth", |
| 555 | .id = UCLASS_ETH, |
| 556 | .bind = enetc_bind, |
| 557 | .probe = enetc_probe, |
| 558 | .remove = enetc_remove, |
| 559 | .ops = &enetc_ops, |
| 560 | .priv_auto_alloc_size = sizeof(struct enetc_priv), |
| 561 | .platdata_auto_alloc_size = sizeof(struct eth_pdata), |
| 562 | }; |
| 563 | |
| 564 | static struct pci_device_id enetc_ids[] = { |
| 565 | { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, PCI_DEVICE_ID_ENETC_ETH) }, |
| 566 | {} |
| 567 | }; |
| 568 | |
| 569 | U_BOOT_PCI_DEVICE(eth_enetc, enetc_ids); |