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