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