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