blob: ace1069efd951ccca4c03aa57ad8ba374145cc6b [file] [log] [blame]
Alex Marginean7a910c12019-07-03 12:11:40 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * ENETC ethernet controller driver
Vladimir Oltean10c6fe42021-06-29 20:53:15 +03004 * Copyright 2017-2021 NXP
Alex Marginean7a910c12019-07-03 12:11:40 +03005 */
6
Alex Marginean7a910c12019-07-03 12:11:40 +03007#include <dm.h>
8#include <errno.h>
Simon Glass2dc9c342020-05-10 11:40:01 -06009#include <fdt_support.h>
Simon Glass9bc15642020-02-03 07:36:16 -070010#include <malloc.h>
Alex Marginean7a910c12019-07-03 12:11:40 +030011#include <memalign.h>
Simon Glass274e0b02020-05-10 11:39:56 -060012#include <net.h>
13#include <asm/cache.h>
Alex Marginean7a910c12019-07-03 12:11:40 +030014#include <asm/io.h>
15#include <pci.h>
Alex Marginean02155392019-07-03 12:11:41 +030016#include <miiphy.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060017#include <linux/bug.h>
Simon Glassdbd79542020-05-10 11:40:11 -060018#include <linux/delay.h>
Alex Marginean7a910c12019-07-03 12:11:40 +030019
20#include "fsl_enetc.h"
21
Alex Marginean805b8592019-12-10 16:55:39 +020022#define ENETC_DRIVER_NAME "enetc_eth"
23
Siarhei Yasinski25b798e2022-08-31 10:57:37 +000024static int enetc_remove(struct udevice *dev);
25
Marek Vasutc05f8dc2025-01-16 05:03:18 +010026static int enetc_is_ls1028a(struct udevice *dev)
27{
28 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
29
30 /* Test whether this is LS1028A ENETC. This may be optimized out. */
31 return IS_ENABLED(CONFIG_ARCH_LS1028A) &&
32 pplat->vendor == PCI_VENDOR_ID_FREESCALE;
33}
34
Marek Vasutdbfb4bc2025-01-16 05:03:23 +010035static int enetc_dev_id(struct udevice *dev)
36{
37 if (enetc_is_ls1028a(dev))
38 return PCI_FUNC(pci_get_devfn(dev));
39
40 return 0;
41}
42
Marek Vasutcd684142025-01-16 05:03:24 +010043/* register accessors */
44static u32 enetc_read_reg(void __iomem *addr)
45{
46 return readl(addr);
47}
48
49static void enetc_write_reg(void __iomem *addr, u32 val)
50{
51 writel(val, addr);
52}
53
54static void enetc_write(struct enetc_priv *priv, u32 off, u32 val)
55{
56 enetc_write_reg(priv->regs_base + off, val);
57}
58
59/* port register accessors */
60static u32 enetc_read_port(struct enetc_priv *priv, u32 off)
61{
62 return enetc_read_reg(priv->port_regs + off);
63}
64
65static void enetc_write_port(struct enetc_priv *priv, u32 off, u32 val)
66{
67 enetc_write_reg(priv->port_regs + off, val);
68}
69
70/* BDR register accessor, see also ENETC_BDR() */
71static void enetc_bdr_write(struct enetc_priv *priv, int type, int n,
72 u32 off, u32 val)
73{
74 enetc_write(priv, ENETC_BDR(type, n, off), val);
75}
76
Alex Marginean805b8592019-12-10 16:55:39 +020077/*
78 * sets the MAC address in IERB registers, this setting is persistent and
79 * carried over to Linux.
80 */
Alex Marginean805b8592019-12-10 16:55:39 +020081#define IERB_BASE 0x1f0800000ULL
82#define IERB_PFMAC(pf, vf, n) (IERB_BASE + 0x8000 + (pf) * 0x100 + (vf) * 8 \
83 + (n) * 4)
84
Marek Vasutd9b36f62025-01-16 05:03:20 +010085static void enetc_set_ierb_primary_mac(struct udevice *dev, void *blob)
Marek Vasutc9997c72025-01-16 05:03:19 +010086{
Marek Vasutd9b36f62025-01-16 05:03:20 +010087 static int ierb_fn_to_pf[] = { 0, 1, 2, -1, -1, -1, 3 };
88 struct pci_child_plat *ppdata = dev_get_parent_plat(dev);
89 struct eth_pdata *pdata = dev_get_plat(dev);
90 const u8 *enetaddr = pdata->enetaddr;
Alex Marginean805b8592019-12-10 16:55:39 +020091 u16 lower = *(const u16 *)(enetaddr + 4);
92 u32 upper = *(const u32 *)enetaddr;
Marek Vasutd9b36f62025-01-16 05:03:20 +010093 int devfn, offset;
94 char path[256];
Alex Marginean805b8592019-12-10 16:55:39 +020095
Marek Vasutc9997c72025-01-16 05:03:19 +010096 if (enetc_is_ls1028a(dev)) {
97 /*
98 * LS1028A is the only part with IERB at this time and
99 * there are plans to change its structure, keep this
100 * LS1028A specific for now.
101 */
Marek Vasutd9b36f62025-01-16 05:03:20 +0100102 devfn = PCI_FUNC(ppdata->devfn);
103
Marek Vasutc9997c72025-01-16 05:03:19 +0100104 if (ierb_fn_to_pf[devfn] < 0)
105 return;
Alex Marginean805b8592019-12-10 16:55:39 +0200106
Marek Vasutc9997c72025-01-16 05:03:19 +0100107 out_le32(IERB_PFMAC(ierb_fn_to_pf[devfn], 0, 0), upper);
108 out_le32(IERB_PFMAC(ierb_fn_to_pf[devfn], 0, 1), (u32)lower);
Marek Vasutd9b36f62025-01-16 05:03:20 +0100109
110 snprintf(path, 256, "/soc/pcie@1f0000000/ethernet@%x,%x",
111 PCI_DEV(ppdata->devfn), PCI_FUNC(ppdata->devfn));
112 } else {
113 return;
Marek Vasutc9997c72025-01-16 05:03:19 +0100114 }
Marek Vasutd9b36f62025-01-16 05:03:20 +0100115
116 offset = fdt_path_offset(blob, path);
117 if (offset >= 0)
118 fdt_setprop(blob, offset, "mac-address", pdata->enetaddr, 6);
Alex Marginean805b8592019-12-10 16:55:39 +0200119}
120
121/* sets up primary MAC addresses in DT/IERB */
122void fdt_fixup_enetc_mac(void *blob)
123{
Alex Marginean805b8592019-12-10 16:55:39 +0200124 struct udevice *dev;
125 struct uclass *uc;
Alex Marginean805b8592019-12-10 16:55:39 +0200126
127 uclass_get(UCLASS_ETH, &uc);
128 uclass_foreach_dev(dev, uc) {
129 if (!dev->driver || !dev->driver->name ||
130 strcmp(dev->driver->name, ENETC_DRIVER_NAME))
131 continue;
132
Marek Vasutd9b36f62025-01-16 05:03:20 +0100133 enetc_set_ierb_primary_mac(dev, blob);
Alex Marginean805b8592019-12-10 16:55:39 +0200134 }
135}
136
Alex Marginean7a910c12019-07-03 12:11:40 +0300137/*
138 * Bind the device:
139 * - set a more explicit name on the interface
140 */
141static int enetc_bind(struct udevice *dev)
142{
143 char name[16];
144 static int eth_num_devices;
145
146 /*
147 * prefer using PCI function numbers to number interfaces, but these
148 * are only available if dts nodes are present. For PCI they are
149 * optional, handle that case too. Just in case some nodes are present
150 * and some are not, use different naming scheme - enetc-N based on
151 * PCI function # and enetc#N based on interface count
152 */
Simon Glassa7ece582020-12-19 10:40:14 -0700153 if (ofnode_valid(dev_ofnode(dev)))
Marek Vasutdbfb4bc2025-01-16 05:03:23 +0100154 sprintf(name, "enetc-%u", enetc_dev_id(dev));
Alex Marginean7a910c12019-07-03 12:11:40 +0300155 else
156 sprintf(name, "enetc#%u", eth_num_devices++);
157 device_set_name(dev, name);
158
159 return 0;
160}
161
Alex Marginean38882ae2019-07-03 12:11:42 +0300162/* MDIO wrappers, we're using these to drive internal MDIO to get to serdes */
163static int enetc_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
164{
165 struct enetc_mdio_priv priv;
166
167 priv.regs_base = bus->priv;
168 return enetc_mdio_read_priv(&priv, addr, devad, reg);
169}
170
171static int enetc_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
172 u16 val)
173{
174 struct enetc_mdio_priv priv;
175
176 priv.regs_base = bus->priv;
177 return enetc_mdio_write_priv(&priv, addr, devad, reg, val);
178}
179
180/* only interfaces that can pin out through serdes have internal MDIO */
181static bool enetc_has_imdio(struct udevice *dev)
182{
183 struct enetc_priv *priv = dev_get_priv(dev);
184
185 return !!(priv->imdio.priv);
186}
187
188/* set up serdes for SGMII */
189static int enetc_init_sgmii(struct udevice *dev)
190{
191 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean41a7ac52019-07-15 11:48:47 +0300192 bool is2500 = false;
193 u16 reg;
Alex Marginean38882ae2019-07-03 12:11:42 +0300194
195 if (!enetc_has_imdio(dev))
196 return 0;
197
Simon Glassfada3f92022-09-17 09:00:09 -0600198 if (priv->uclass_id == PHY_INTERFACE_MODE_2500BASEX)
Alex Marginean41a7ac52019-07-15 11:48:47 +0300199 is2500 = true;
200
201 /*
202 * Set to SGMII mode, for 1Gbps enable AN, for 2.5Gbps set fixed speed.
203 * Although fixed speed is 1Gbps, we could be running at 2.5Gbps based
204 * on PLL configuration. Setting 1G for 2.5G here is counter intuitive
205 * but intentional.
206 */
207 reg = ENETC_PCS_IF_MODE_SGMII;
208 reg |= is2500 ? ENETC_PCS_IF_MODE_SPEED_1G : ENETC_PCS_IF_MODE_SGMII_AN;
Alex Marginean38882ae2019-07-03 12:11:42 +0300209 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300210 ENETC_PCS_IF_MODE, reg);
Alex Marginean38882ae2019-07-03 12:11:42 +0300211
212 /* Dev ability - SGMII */
213 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
214 ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SGMII);
215
216 /* Adjust link timer for SGMII */
217 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
218 ENETC_PCS_LINK_TIMER1, ENETC_PCS_LINK_TIMER1_VAL);
219 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
220 ENETC_PCS_LINK_TIMER2, ENETC_PCS_LINK_TIMER2_VAL);
221
Alex Marginean41a7ac52019-07-15 11:48:47 +0300222 reg = ENETC_PCS_CR_DEF_VAL;
223 reg |= is2500 ? ENETC_PCS_CR_RST : ENETC_PCS_CR_RESET_AN;
Alex Marginean38882ae2019-07-03 12:11:42 +0300224 /* restart PCS AN */
225 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300226 ENETC_PCS_CR, reg);
Alex Marginean38882ae2019-07-03 12:11:42 +0300227
228 return 0;
229}
230
231/* set up MAC for RGMII */
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300232static void enetc_init_rgmii(struct udevice *dev, struct phy_device *phydev)
Alex Marginean38882ae2019-07-03 12:11:42 +0300233{
234 struct enetc_priv *priv = dev_get_priv(dev);
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300235 u32 old_val, val;
Alex Marginean38882ae2019-07-03 12:11:42 +0300236
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300237 old_val = val = enetc_read_port(priv, ENETC_PM_IF_MODE);
Alex Marginean38882ae2019-07-03 12:11:42 +0300238
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300239 /* disable unreliable RGMII in-band signaling and force the MAC into
240 * the speed negotiated by the PHY.
241 */
242 val &= ~ENETC_PM_IF_MODE_AN_ENA;
243
244 if (phydev->speed == SPEED_1000) {
245 val &= ~ENETC_PM_IFM_SSP_MASK;
246 val |= ENETC_PM_IFM_SSP_1000;
247 } else if (phydev->speed == SPEED_100) {
248 val &= ~ENETC_PM_IFM_SSP_MASK;
249 val |= ENETC_PM_IFM_SSP_100;
250 } else if (phydev->speed == SPEED_10) {
251 val &= ~ENETC_PM_IFM_SSP_MASK;
252 val |= ENETC_PM_IFM_SSP_10;
253 }
254
255 if (phydev->duplex == DUPLEX_FULL)
256 val |= ENETC_PM_IFM_FULL_DPX;
257 else
258 val &= ~ENETC_PM_IFM_FULL_DPX;
259
260 if (val == old_val)
261 return;
262
263 enetc_write_port(priv, ENETC_PM_IF_MODE, val);
Alex Marginean38882ae2019-07-03 12:11:42 +0300264}
265
Alex Margineanafad2d02020-01-10 23:32:20 +0200266/* set up MAC configuration for the given interface type */
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300267static void enetc_setup_mac_iface(struct udevice *dev,
268 struct phy_device *phydev)
Alex Marginean38882ae2019-07-03 12:11:42 +0300269{
270 struct enetc_priv *priv = dev_get_priv(dev);
271 u32 if_mode;
272
Simon Glassfada3f92022-09-17 09:00:09 -0600273 switch (priv->uclass_id) {
Alex Margineanafad2d02020-01-10 23:32:20 +0200274 case PHY_INTERFACE_MODE_RGMII:
275 case PHY_INTERFACE_MODE_RGMII_ID:
276 case PHY_INTERFACE_MODE_RGMII_RXID:
277 case PHY_INTERFACE_MODE_RGMII_TXID:
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300278 enetc_init_rgmii(dev, phydev);
Alex Margineanafad2d02020-01-10 23:32:20 +0200279 break;
Alex Margineanafad2d02020-01-10 23:32:20 +0200280 case PHY_INTERFACE_MODE_USXGMII:
Vladimir Oltean6a6e4022021-09-18 15:32:34 +0300281 case PHY_INTERFACE_MODE_10GBASER:
Alex Margineanafad2d02020-01-10 23:32:20 +0200282 /* set ifmode to (US)XGMII */
283 if_mode = enetc_read_port(priv, ENETC_PM_IF_MODE);
284 if_mode &= ~ENETC_PM_IF_IFMODE_MASK;
285 enetc_write_port(priv, ENETC_PM_IF_MODE, if_mode);
286 break;
287 };
288}
289
290/* set up serdes for SXGMII */
291static int enetc_init_sxgmii(struct udevice *dev)
292{
293 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean38882ae2019-07-03 12:11:42 +0300294
295 if (!enetc_has_imdio(dev))
296 return 0;
297
298 /* Dev ability - SXGMII */
299 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
300 ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SXGMII);
301
302 /* Restart PCS AN */
303 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
304 ENETC_PCS_CR,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300305 ENETC_PCS_CR_RST | ENETC_PCS_CR_RESET_AN);
Alex Marginean38882ae2019-07-03 12:11:42 +0300306
307 return 0;
308}
309
310/* Apply protocol specific configuration to MAC, serdes as needed */
311static void enetc_start_pcs(struct udevice *dev)
312{
313 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean38882ae2019-07-03 12:11:42 +0300314
Alex Margineand4be7682019-11-25 17:57:27 +0200315 /* register internal MDIO for debug purposes */
Alex Marginean38882ae2019-07-03 12:11:42 +0300316 if (enetc_read_port(priv, ENETC_PCAPR0) & ENETC_PCAPRO_MDIO) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300317 priv->imdio.read = enetc_mdio_read;
318 priv->imdio.write = enetc_mdio_write;
319 priv->imdio.priv = priv->port_regs + ENETC_PM_IMDIO_BASE;
Vladimir Olteandcd21cc2021-09-27 14:21:48 +0300320 strlcpy(priv->imdio.name, dev->name, MDIO_NAME_LEN);
Alex Margineand4be7682019-11-25 17:57:27 +0200321 if (!miiphy_get_dev_by_name(priv->imdio.name))
322 mdio_register(&priv->imdio);
Alex Marginean38882ae2019-07-03 12:11:42 +0300323 }
324
Simon Glassa7ece582020-12-19 10:40:14 -0700325 if (!ofnode_valid(dev_ofnode(dev))) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300326 enetc_dbg(dev, "no enetc ofnode found, skipping PCS set-up\n");
327 return;
328 }
329
Simon Glassfada3f92022-09-17 09:00:09 -0600330 priv->uclass_id = dev_read_phy_mode(dev);
331 if (priv->uclass_id == PHY_INTERFACE_MODE_NA) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300332 enetc_dbg(dev,
333 "phy-mode property not found, defaulting to SGMII\n");
Simon Glassfada3f92022-09-17 09:00:09 -0600334 priv->uclass_id = PHY_INTERFACE_MODE_SGMII;
Marek Behúnbc194772022-04-07 00:33:01 +0200335 }
Alex Marginean38882ae2019-07-03 12:11:42 +0300336
Simon Glassfada3f92022-09-17 09:00:09 -0600337 switch (priv->uclass_id) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300338 case PHY_INTERFACE_MODE_SGMII:
Vladimir Oltean6caef972021-09-18 15:32:35 +0300339 case PHY_INTERFACE_MODE_2500BASEX:
Alex Marginean38882ae2019-07-03 12:11:42 +0300340 enetc_init_sgmii(dev);
341 break;
Alex Margineaned0460c2019-11-14 18:28:38 +0200342 case PHY_INTERFACE_MODE_USXGMII:
Vladimir Oltean6a6e4022021-09-18 15:32:34 +0300343 case PHY_INTERFACE_MODE_10GBASER:
Alex Marginean38882ae2019-07-03 12:11:42 +0300344 enetc_init_sxgmii(dev);
345 break;
346 };
347}
348
Alex Marginean02155392019-07-03 12:11:41 +0300349/* Configure the actual/external ethernet PHY, if one is found */
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300350static int enetc_config_phy(struct udevice *dev)
Alex Marginean02155392019-07-03 12:11:41 +0300351{
352 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean02155392019-07-03 12:11:41 +0300353 int supported;
354
Alex Marginean602e00f2019-11-25 17:15:13 +0200355 priv->phy = dm_eth_phy_connect(dev);
Alex Marginean602e00f2019-11-25 17:15:13 +0200356 if (!priv->phy)
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300357 return -ENODEV;
Alex Marginean02155392019-07-03 12:11:41 +0300358
Alex Margineanb93375c2019-11-14 18:58:45 +0200359 supported = PHY_GBIT_FEATURES | SUPPORTED_2500baseX_Full;
360 priv->phy->supported &= supported;
361 priv->phy->advertising &= supported;
Alex Marginean602e00f2019-11-25 17:15:13 +0200362
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300363 return phy_config(priv->phy);
Alex Marginean02155392019-07-03 12:11:41 +0300364}
365
Alex Marginean7a910c12019-07-03 12:11:40 +0300366/*
367 * Probe ENETC driver:
368 * - initialize port and station interface BARs
369 */
370static int enetc_probe(struct udevice *dev)
371{
372 struct enetc_priv *priv = dev_get_priv(dev);
Siarhei Yasinski25b798e2022-08-31 10:57:37 +0000373 int res;
Alex Marginean7a910c12019-07-03 12:11:40 +0300374
Simon Glass2e4938b2022-09-06 20:27:17 -0600375 if (ofnode_valid(dev_ofnode(dev)) && !ofnode_is_enabled(dev_ofnode(dev))) {
Alex Marginean7a910c12019-07-03 12:11:40 +0300376 enetc_dbg(dev, "interface disabled\n");
377 return -ENODEV;
378 }
379
380 priv->enetc_txbd = memalign(ENETC_BD_ALIGN,
381 sizeof(struct enetc_tx_bd) * ENETC_BD_CNT);
382 priv->enetc_rxbd = memalign(ENETC_BD_ALIGN,
383 sizeof(union enetc_rx_bd) * ENETC_BD_CNT);
384
385 if (!priv->enetc_txbd || !priv->enetc_rxbd) {
386 /* free should be able to handle NULL, just free all pointers */
387 free(priv->enetc_txbd);
388 free(priv->enetc_rxbd);
389
390 return -ENOMEM;
391 }
392
393 /* initialize register */
Andrew Scull6520c822022-04-21 16:11:13 +0000394 priv->regs_base = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0, 0, 0, PCI_REGION_TYPE, 0);
Alex Marginean7a910c12019-07-03 12:11:40 +0300395 if (!priv->regs_base) {
396 enetc_dbg(dev, "failed to map BAR0\n");
397 return -EINVAL;
398 }
399 priv->port_regs = priv->regs_base + ENETC_PORT_REGS_OFF;
400
401 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
402
Alex Margineanc905c212019-11-14 18:58:46 +0200403 enetc_start_pcs(dev);
Alex Margineanc905c212019-11-14 18:58:46 +0200404
Siarhei Yasinski25b798e2022-08-31 10:57:37 +0000405 res = enetc_config_phy(dev);
406 if(res)
407 enetc_remove(dev);
408 return res;
Alex Marginean7a910c12019-07-03 12:11:40 +0300409}
410
411/*
412 * Remove the driver from an interface:
413 * - free up allocated memory
414 */
415static int enetc_remove(struct udevice *dev)
416{
417 struct enetc_priv *priv = dev_get_priv(dev);
418
Michael Walle3f66e8e2022-05-31 18:36:16 +0200419 if (miiphy_get_dev_by_name(priv->imdio.name))
420 mdio_unregister(&priv->imdio);
421
Alex Marginean7a910c12019-07-03 12:11:40 +0300422 free(priv->enetc_txbd);
423 free(priv->enetc_rxbd);
424
425 return 0;
426}
427
Michael Walle1d3e24f2019-12-20 14:16:48 +0100428/*
429 * LS1028A is the only part with IERB at this time and there are plans to
430 * change its structure, keep this LS1028A specific for now.
431 */
432#define LS1028A_IERB_BASE 0x1f0800000ULL
433#define LS1028A_IERB_PSIPMAR0(pf, vf) (LS1028A_IERB_BASE + 0x8000 \
434 + (pf) * 0x100 + (vf) * 8)
435#define LS1028A_IERB_PSIPMAR1(pf, vf) (LS1028A_IERB_PSIPMAR0(pf, vf) + 4)
436
437static int enetc_ls1028a_write_hwaddr(struct udevice *dev)
438{
Simon Glassb75b15b2020-12-03 16:55:23 -0700439 struct pci_child_plat *ppdata = dev_get_parent_plat(dev);
Michael Walle1d3e24f2019-12-20 14:16:48 +0100440 const int devfn_to_pf[] = {0, 1, 2, -1, -1, -1, 3};
Simon Glassfa20e932020-12-03 16:55:20 -0700441 struct eth_pdata *plat = dev_get_plat(dev);
Michael Walle1d3e24f2019-12-20 14:16:48 +0100442 int devfn = PCI_FUNC(ppdata->devfn);
443 u8 *addr = plat->enetaddr;
444 u32 lower, upper;
445 int pf;
446
447 if (devfn >= ARRAY_SIZE(devfn_to_pf))
448 return 0;
449
450 pf = devfn_to_pf[devfn];
451 if (pf < 0)
452 return 0;
453
454 lower = *(const u16 *)(addr + 4);
455 upper = *(const u32 *)addr;
456
457 out_le32(LS1028A_IERB_PSIPMAR0(pf, 0), upper);
458 out_le32(LS1028A_IERB_PSIPMAR1(pf, 0), lower);
459
460 return 0;
461}
462
Michael Walle8c7188e2019-12-20 14:16:47 +0100463static int enetc_write_hwaddr(struct udevice *dev)
Alex Marginean7a910c12019-07-03 12:11:40 +0300464{
Simon Glassfa20e932020-12-03 16:55:20 -0700465 struct eth_pdata *plat = dev_get_plat(dev);
Michael Walle8c7188e2019-12-20 14:16:47 +0100466 struct enetc_priv *priv = dev_get_priv(dev);
467 u8 *addr = plat->enetaddr;
468
Marek Vasutc05f8dc2025-01-16 05:03:18 +0100469 if (enetc_is_ls1028a(dev))
Michael Walle1d3e24f2019-12-20 14:16:48 +0100470 return enetc_ls1028a_write_hwaddr(dev);
471
Alex Marginean7a910c12019-07-03 12:11:40 +0300472 u16 lower = *(const u16 *)(addr + 4);
473 u32 upper = *(const u32 *)addr;
474
475 enetc_write_port(priv, ENETC_PSIPMAR0, upper);
476 enetc_write_port(priv, ENETC_PSIPMAR1, lower);
Michael Walle8c7188e2019-12-20 14:16:47 +0100477
478 return 0;
Alex Marginean7a910c12019-07-03 12:11:40 +0300479}
480
481/* Configure port parameters (# of rings, frame size, enable port) */
Marek Vasutb0dc0b72025-01-16 05:03:21 +0100482static void enetc_enable_si_port(struct udevice *dev)
Alex Marginean7a910c12019-07-03 12:11:40 +0300483{
Marek Vasutb0dc0b72025-01-16 05:03:21 +0100484 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean7a910c12019-07-03 12:11:40 +0300485 u32 val;
486
487 /* set Rx/Tx BDR count */
488 val = ENETC_PSICFGR_SET_TXBDR(ENETC_TX_BDR_CNT);
489 val |= ENETC_PSICFGR_SET_RXBDR(ENETC_RX_BDR_CNT);
490 enetc_write_port(priv, ENETC_PSICFGR(0), val);
491 /* set Rx max frame size */
492 enetc_write_port(priv, ENETC_PM_MAXFRM, ENETC_RX_MAXFRM_SIZE);
493 /* enable MAC port */
494 enetc_write_port(priv, ENETC_PM_CC, ENETC_PM_CC_RX_TX_EN);
495 /* enable port */
496 enetc_write_port(priv, ENETC_PMR, ENETC_PMR_SI0_EN);
497 /* set SI cache policy */
498 enetc_write(priv, ENETC_SICAR0,
499 ENETC_SICAR_RD_CFG | ENETC_SICAR_WR_CFG);
500 /* enable SI */
501 enetc_write(priv, ENETC_SIMR, ENETC_SIMR_EN);
502}
503
504/* returns DMA address for a given buffer index */
505static inline u64 enetc_rxb_address(struct udevice *dev, int i)
506{
507 return cpu_to_le64(dm_pci_virt_to_mem(dev, net_rx_packets[i]));
508}
509
510/*
511 * Setup a single Tx BD Ring (ID = 0):
512 * - set Tx buffer descriptor address
513 * - set the BD count
514 * - initialize the producer and consumer index
515 */
516static void enetc_setup_tx_bdr(struct udevice *dev)
517{
518 struct enetc_priv *priv = dev_get_priv(dev);
519 struct bd_ring *tx_bdr = &priv->tx_bdr;
520 u64 tx_bd_add = (u64)priv->enetc_txbd;
521
522 /* used later to advance to the next Tx BD */
523 tx_bdr->bd_count = ENETC_BD_CNT;
524 tx_bdr->next_prod_idx = 0;
525 tx_bdr->next_cons_idx = 0;
526 tx_bdr->cons_idx = priv->regs_base +
527 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBCIR);
528 tx_bdr->prod_idx = priv->regs_base +
529 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBPIR);
530
531 /* set Tx BD address */
532 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR0,
533 lower_32_bits(tx_bd_add));
534 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR1,
535 upper_32_bits(tx_bd_add));
536 /* set Tx 8 BD count */
537 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBLENR,
538 tx_bdr->bd_count);
539
540 /* reset both producer/consumer indexes */
541 enetc_write_reg(tx_bdr->cons_idx, tx_bdr->next_cons_idx);
542 enetc_write_reg(tx_bdr->prod_idx, tx_bdr->next_prod_idx);
543
544 /* enable TX ring */
545 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBMR, ENETC_TBMR_EN);
546}
547
548/*
549 * Setup a single Rx BD Ring (ID = 0):
550 * - set Rx buffer descriptors address (one descriptor per buffer)
551 * - set buffer size as max frame size
552 * - enable Rx ring
553 * - reset consumer and producer indexes
554 * - set buffer for each descriptor
555 */
556static void enetc_setup_rx_bdr(struct udevice *dev)
557{
558 struct enetc_priv *priv = dev_get_priv(dev);
559 struct bd_ring *rx_bdr = &priv->rx_bdr;
560 u64 rx_bd_add = (u64)priv->enetc_rxbd;
561 int i;
562
563 /* used later to advance to the next BD produced by ENETC HW */
564 rx_bdr->bd_count = ENETC_BD_CNT;
565 rx_bdr->next_prod_idx = 0;
566 rx_bdr->next_cons_idx = 0;
567 rx_bdr->cons_idx = priv->regs_base +
568 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBCIR);
569 rx_bdr->prod_idx = priv->regs_base +
570 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBPIR);
571
572 /* set Rx BD address */
573 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR0,
574 lower_32_bits(rx_bd_add));
575 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR1,
576 upper_32_bits(rx_bd_add));
577 /* set Rx BD count (multiple of 8) */
578 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBLENR,
579 rx_bdr->bd_count);
580 /* set Rx buffer size */
581 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBSR, PKTSIZE_ALIGN);
582
583 /* fill Rx BD */
584 memset(priv->enetc_rxbd, 0,
585 rx_bdr->bd_count * sizeof(union enetc_rx_bd));
586 for (i = 0; i < rx_bdr->bd_count; i++) {
587 priv->enetc_rxbd[i].w.addr = enetc_rxb_address(dev, i);
588 /* each RX buffer must be aligned to 64B */
589 WARN_ON(priv->enetc_rxbd[i].w.addr & (ARCH_DMA_MINALIGN - 1));
590 }
591
592 /* reset producer (ENETC owned) and consumer (SW owned) index */
593 enetc_write_reg(rx_bdr->cons_idx, rx_bdr->next_cons_idx);
594 enetc_write_reg(rx_bdr->prod_idx, rx_bdr->next_prod_idx);
595
596 /* enable Rx ring */
597 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBMR, ENETC_RBMR_EN);
598}
599
600/*
601 * Start ENETC interface:
602 * - perform FLR
603 * - enable access to port and SI registers
604 * - set mac address
605 * - setup TX/RX buffer descriptors
606 * - enable Tx/Rx rings
607 */
608static int enetc_start(struct udevice *dev)
609{
Alex Marginean7a910c12019-07-03 12:11:40 +0300610 struct enetc_priv *priv = dev_get_priv(dev);
611
612 /* reset and enable the PCI device */
613 dm_pci_flr(dev);
614 dm_pci_clrset_config16(dev, PCI_COMMAND, 0,
615 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
616
Marek Vasutb0dc0b72025-01-16 05:03:21 +0100617 enetc_enable_si_port(dev);
Alex Marginean7a910c12019-07-03 12:11:40 +0300618
619 /* setup Tx/Rx buffer descriptors */
620 enetc_setup_tx_bdr(dev);
621 enetc_setup_rx_bdr(dev);
622
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300623 enetc_setup_mac_iface(dev, priv->phy);
624
Vladimir Oltean19363082021-06-29 20:53:17 +0300625 return phy_startup(priv->phy);
Alex Marginean7a910c12019-07-03 12:11:40 +0300626}
627
628/*
629 * Stop the network interface:
630 * - just quiesce it, we can wipe all configuration as _start starts from
631 * scratch each time
632 */
633static void enetc_stop(struct udevice *dev)
634{
635 /* FLR is sufficient to quiesce the device */
636 dm_pci_flr(dev);
Alex Margineand4be7682019-11-25 17:57:27 +0200637 /* leave the BARs accessible after we stop, this is needed to use
638 * internal MDIO in command line.
639 */
640 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
Alex Marginean7a910c12019-07-03 12:11:40 +0300641}
642
643/*
644 * ENETC transmit packet:
645 * - check if Tx BD ring is full
646 * - set buffer/packet address (dma address)
647 * - set final fragment flag
648 * - try while producer index equals consumer index or timeout
649 */
650static int enetc_send(struct udevice *dev, void *packet, int length)
651{
652 struct enetc_priv *priv = dev_get_priv(dev);
653 struct bd_ring *txr = &priv->tx_bdr;
654 void *nv_packet = (void *)packet;
655 int tries = ENETC_POLL_TRIES;
656 u32 pi, ci;
657
658 pi = txr->next_prod_idx;
659 ci = enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK;
660 /* Tx ring is full when */
661 if (((pi + 1) % txr->bd_count) == ci) {
662 enetc_dbg(dev, "Tx BDR full\n");
663 return -ETIMEDOUT;
664 }
665 enetc_dbg(dev, "TxBD[%d]send: pkt_len=%d, buff @0x%x%08x\n", pi, length,
666 upper_32_bits((u64)nv_packet), lower_32_bits((u64)nv_packet));
667
668 /* prepare Tx BD */
669 memset(&priv->enetc_txbd[pi], 0x0, sizeof(struct enetc_tx_bd));
670 priv->enetc_txbd[pi].addr =
671 cpu_to_le64(dm_pci_virt_to_mem(dev, nv_packet));
672 priv->enetc_txbd[pi].buf_len = cpu_to_le16(length);
673 priv->enetc_txbd[pi].frm_len = cpu_to_le16(length);
674 priv->enetc_txbd[pi].flags = cpu_to_le16(ENETC_TXBD_FLAGS_F);
675 dmb();
676 /* send frame: increment producer index */
677 pi = (pi + 1) % txr->bd_count;
678 txr->next_prod_idx = pi;
679 enetc_write_reg(txr->prod_idx, pi);
680 while ((--tries >= 0) &&
681 (pi != (enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK)))
682 udelay(10);
683
684 return tries > 0 ? 0 : -ETIMEDOUT;
685}
686
687/*
688 * Receive frame:
689 * - wait for the next BD to get ready bit set
690 * - clean up the descriptor
691 * - move on and indicate to HW that the cleaned BD is available for Rx
692 */
693static int enetc_recv(struct udevice *dev, int flags, uchar **packetp)
694{
695 struct enetc_priv *priv = dev_get_priv(dev);
696 struct bd_ring *rxr = &priv->rx_bdr;
697 int tries = ENETC_POLL_TRIES;
698 int pi = rxr->next_prod_idx;
699 int ci = rxr->next_cons_idx;
700 u32 status;
701 int len;
702 u8 rdy;
703
704 do {
705 dmb();
706 status = le32_to_cpu(priv->enetc_rxbd[pi].r.lstatus);
707 /* check if current BD is ready to be consumed */
708 rdy = ENETC_RXBD_STATUS_R(status);
709 } while (--tries >= 0 && !rdy);
710
711 if (!rdy)
712 return -EAGAIN;
713
714 dmb();
715 len = le16_to_cpu(priv->enetc_rxbd[pi].r.buf_len);
716 *packetp = (uchar *)enetc_rxb_address(dev, pi);
717 enetc_dbg(dev, "RxBD[%d]: len=%d err=%d pkt=0x%x%08x\n", pi, len,
718 ENETC_RXBD_STATUS_ERRORS(status),
719 upper_32_bits((u64)*packetp), lower_32_bits((u64)*packetp));
720
721 /* BD clean up and advance to next in ring */
722 memset(&priv->enetc_rxbd[pi], 0, sizeof(union enetc_rx_bd));
723 priv->enetc_rxbd[pi].w.addr = enetc_rxb_address(dev, pi);
724 rxr->next_prod_idx = (pi + 1) % rxr->bd_count;
725 ci = (ci + 1) % rxr->bd_count;
726 rxr->next_cons_idx = ci;
727 dmb();
728 /* free up the slot in the ring for HW */
729 enetc_write_reg(rxr->cons_idx, ci);
730
731 return len;
732}
733
Marek Vasut828b2362025-01-16 05:03:22 +0100734static const struct eth_ops enetc_ops_ls = {
Alex Marginean7a910c12019-07-03 12:11:40 +0300735 .start = enetc_start,
736 .send = enetc_send,
737 .recv = enetc_recv,
738 .stop = enetc_stop,
Michael Walle8c7188e2019-12-20 14:16:47 +0100739 .write_hwaddr = enetc_write_hwaddr,
Alex Marginean7a910c12019-07-03 12:11:40 +0300740};
741
Marek Vasut828b2362025-01-16 05:03:22 +0100742U_BOOT_DRIVER(eth_enetc_ls) = {
Alex Marginean805b8592019-12-10 16:55:39 +0200743 .name = ENETC_DRIVER_NAME,
Alex Marginean7a910c12019-07-03 12:11:40 +0300744 .id = UCLASS_ETH,
745 .bind = enetc_bind,
746 .probe = enetc_probe,
747 .remove = enetc_remove,
Marek Vasut828b2362025-01-16 05:03:22 +0100748 .ops = &enetc_ops_ls,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700749 .priv_auto = sizeof(struct enetc_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700750 .plat_auto = sizeof(struct eth_pdata),
Alex Marginean7a910c12019-07-03 12:11:40 +0300751};
752
Marek Vasut828b2362025-01-16 05:03:22 +0100753static struct pci_device_id enetc_ids_ls[] = {
Alex Marginean7a910c12019-07-03 12:11:40 +0300754 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, PCI_DEVICE_ID_ENETC_ETH) },
755 {}
756};
757
Marek Vasut828b2362025-01-16 05:03:22 +0100758U_BOOT_PCI_DEVICE(eth_enetc_ls, enetc_ids_ls);