blob: 532a367c24194fbc02d4cffe06207406e4b00500 [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
Marek Vasuta1fa5cb2025-01-16 05:03:25 +010059/* base port register accessors */
60static void enetc_write_pmr(struct enetc_priv *priv, u32 val)
61{
62 const u32 off = ENETC_PMR + ENETC_PMR_OFFSET_LS;
63
64 enetc_write_reg(priv->port_regs + off, val);
65}
66
67static void enetc_write_psipmar(struct enetc_priv *priv, int n, u32 val)
68{
69 const u32 off = (n ? ENETC_PSIPMAR1 : ENETC_PSIPMAR0) +
70 ENETC_PSIPMARn_OFFSET_LS;
71
72 enetc_write_reg(priv->port_regs + off, val);
73}
74
75/* port station register accessors */
76static void enetc_write_psicfgr(struct enetc_priv *priv, int port, u32 val)
77{
78 const u32 off = ENETC_PSICFGR(port, ENETC_PSICFGR_SHIFT_LS) +
79 ENETC_PSICFGR_OFFSET_LS;
80
81 enetc_write_reg(priv->port_regs + off, val);
82}
83
Marek Vasutcd684142025-01-16 05:03:24 +010084/* port register accessors */
Marek Vasuta1fa5cb2025-01-16 05:03:25 +010085static u32 enetc_read_pcapr_mdio(struct enetc_priv *priv)
86{
87 const u32 off = ENETC_PCAPR0 + ENETC_PCAPR_OFFSET_LS;
88 u32 reg = enetc_read_reg(priv->port_regs + off);
89
90 return reg & ENETC_PCAPRO_MDIO;
91}
92
93/* MAC port register accessors */
94static u32 enetc_read_mac_port(struct enetc_priv *priv, u32 off)
Marek Vasutcd684142025-01-16 05:03:24 +010095{
Marek Vasuta1fa5cb2025-01-16 05:03:25 +010096 off += ENETC_PM_OFFSET_LS;
97
Marek Vasutcd684142025-01-16 05:03:24 +010098 return enetc_read_reg(priv->port_regs + off);
99}
100
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100101static void enetc_write_mac_port(struct enetc_priv *priv, u32 off, u32 val)
Marek Vasutcd684142025-01-16 05:03:24 +0100102{
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100103 off += ENETC_PM_OFFSET_LS;
104
Marek Vasutcd684142025-01-16 05:03:24 +0100105 enetc_write_reg(priv->port_regs + off, val);
106}
107
108/* BDR register accessor, see also ENETC_BDR() */
109static void enetc_bdr_write(struct enetc_priv *priv, int type, int n,
110 u32 off, u32 val)
111{
112 enetc_write(priv, ENETC_BDR(type, n, off), val);
113}
114
Alex Marginean805b8592019-12-10 16:55:39 +0200115/*
116 * sets the MAC address in IERB registers, this setting is persistent and
117 * carried over to Linux.
118 */
Alex Marginean805b8592019-12-10 16:55:39 +0200119#define IERB_BASE 0x1f0800000ULL
120#define IERB_PFMAC(pf, vf, n) (IERB_BASE + 0x8000 + (pf) * 0x100 + (vf) * 8 \
121 + (n) * 4)
122
Marek Vasutd9b36f62025-01-16 05:03:20 +0100123static void enetc_set_ierb_primary_mac(struct udevice *dev, void *blob)
Marek Vasutc9997c72025-01-16 05:03:19 +0100124{
Marek Vasutd9b36f62025-01-16 05:03:20 +0100125 static int ierb_fn_to_pf[] = { 0, 1, 2, -1, -1, -1, 3 };
126 struct pci_child_plat *ppdata = dev_get_parent_plat(dev);
127 struct eth_pdata *pdata = dev_get_plat(dev);
128 const u8 *enetaddr = pdata->enetaddr;
Alex Marginean805b8592019-12-10 16:55:39 +0200129 u16 lower = *(const u16 *)(enetaddr + 4);
130 u32 upper = *(const u32 *)enetaddr;
Marek Vasutd9b36f62025-01-16 05:03:20 +0100131 int devfn, offset;
132 char path[256];
Alex Marginean805b8592019-12-10 16:55:39 +0200133
Marek Vasutc9997c72025-01-16 05:03:19 +0100134 if (enetc_is_ls1028a(dev)) {
135 /*
136 * LS1028A is the only part with IERB at this time and
137 * there are plans to change its structure, keep this
138 * LS1028A specific for now.
139 */
Marek Vasutd9b36f62025-01-16 05:03:20 +0100140 devfn = PCI_FUNC(ppdata->devfn);
141
Marek Vasutc9997c72025-01-16 05:03:19 +0100142 if (ierb_fn_to_pf[devfn] < 0)
143 return;
Alex Marginean805b8592019-12-10 16:55:39 +0200144
Marek Vasutc9997c72025-01-16 05:03:19 +0100145 out_le32(IERB_PFMAC(ierb_fn_to_pf[devfn], 0, 0), upper);
146 out_le32(IERB_PFMAC(ierb_fn_to_pf[devfn], 0, 1), (u32)lower);
Marek Vasutd9b36f62025-01-16 05:03:20 +0100147
148 snprintf(path, 256, "/soc/pcie@1f0000000/ethernet@%x,%x",
149 PCI_DEV(ppdata->devfn), PCI_FUNC(ppdata->devfn));
150 } else {
151 return;
Marek Vasutc9997c72025-01-16 05:03:19 +0100152 }
Marek Vasutd9b36f62025-01-16 05:03:20 +0100153
154 offset = fdt_path_offset(blob, path);
155 if (offset >= 0)
156 fdt_setprop(blob, offset, "mac-address", pdata->enetaddr, 6);
Alex Marginean805b8592019-12-10 16:55:39 +0200157}
158
159/* sets up primary MAC addresses in DT/IERB */
160void fdt_fixup_enetc_mac(void *blob)
161{
Alex Marginean805b8592019-12-10 16:55:39 +0200162 struct udevice *dev;
163 struct uclass *uc;
Alex Marginean805b8592019-12-10 16:55:39 +0200164
165 uclass_get(UCLASS_ETH, &uc);
166 uclass_foreach_dev(dev, uc) {
167 if (!dev->driver || !dev->driver->name ||
168 strcmp(dev->driver->name, ENETC_DRIVER_NAME))
169 continue;
170
Marek Vasutd9b36f62025-01-16 05:03:20 +0100171 enetc_set_ierb_primary_mac(dev, blob);
Alex Marginean805b8592019-12-10 16:55:39 +0200172 }
173}
174
Alex Marginean7a910c12019-07-03 12:11:40 +0300175/*
176 * Bind the device:
177 * - set a more explicit name on the interface
178 */
179static int enetc_bind(struct udevice *dev)
180{
181 char name[16];
182 static int eth_num_devices;
183
184 /*
185 * prefer using PCI function numbers to number interfaces, but these
186 * are only available if dts nodes are present. For PCI they are
187 * optional, handle that case too. Just in case some nodes are present
188 * and some are not, use different naming scheme - enetc-N based on
189 * PCI function # and enetc#N based on interface count
190 */
Simon Glassa7ece582020-12-19 10:40:14 -0700191 if (ofnode_valid(dev_ofnode(dev)))
Marek Vasutdbfb4bc2025-01-16 05:03:23 +0100192 sprintf(name, "enetc-%u", enetc_dev_id(dev));
Alex Marginean7a910c12019-07-03 12:11:40 +0300193 else
194 sprintf(name, "enetc#%u", eth_num_devices++);
195 device_set_name(dev, name);
196
197 return 0;
198}
199
Alex Marginean38882ae2019-07-03 12:11:42 +0300200/* MDIO wrappers, we're using these to drive internal MDIO to get to serdes */
201static int enetc_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
202{
203 struct enetc_mdio_priv priv;
204
205 priv.regs_base = bus->priv;
206 return enetc_mdio_read_priv(&priv, addr, devad, reg);
207}
208
209static int enetc_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
210 u16 val)
211{
212 struct enetc_mdio_priv priv;
213
214 priv.regs_base = bus->priv;
215 return enetc_mdio_write_priv(&priv, addr, devad, reg, val);
216}
217
218/* only interfaces that can pin out through serdes have internal MDIO */
219static bool enetc_has_imdio(struct udevice *dev)
220{
221 struct enetc_priv *priv = dev_get_priv(dev);
222
223 return !!(priv->imdio.priv);
224}
225
226/* set up serdes for SGMII */
227static int enetc_init_sgmii(struct udevice *dev)
228{
229 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean41a7ac52019-07-15 11:48:47 +0300230 bool is2500 = false;
231 u16 reg;
Alex Marginean38882ae2019-07-03 12:11:42 +0300232
233 if (!enetc_has_imdio(dev))
234 return 0;
235
Simon Glassfada3f92022-09-17 09:00:09 -0600236 if (priv->uclass_id == PHY_INTERFACE_MODE_2500BASEX)
Alex Marginean41a7ac52019-07-15 11:48:47 +0300237 is2500 = true;
238
239 /*
240 * Set to SGMII mode, for 1Gbps enable AN, for 2.5Gbps set fixed speed.
241 * Although fixed speed is 1Gbps, we could be running at 2.5Gbps based
242 * on PLL configuration. Setting 1G for 2.5G here is counter intuitive
243 * but intentional.
244 */
245 reg = ENETC_PCS_IF_MODE_SGMII;
246 reg |= is2500 ? ENETC_PCS_IF_MODE_SPEED_1G : ENETC_PCS_IF_MODE_SGMII_AN;
Alex Marginean38882ae2019-07-03 12:11:42 +0300247 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300248 ENETC_PCS_IF_MODE, reg);
Alex Marginean38882ae2019-07-03 12:11:42 +0300249
250 /* Dev ability - SGMII */
251 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
252 ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SGMII);
253
254 /* Adjust link timer for SGMII */
255 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
256 ENETC_PCS_LINK_TIMER1, ENETC_PCS_LINK_TIMER1_VAL);
257 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
258 ENETC_PCS_LINK_TIMER2, ENETC_PCS_LINK_TIMER2_VAL);
259
Alex Marginean41a7ac52019-07-15 11:48:47 +0300260 reg = ENETC_PCS_CR_DEF_VAL;
261 reg |= is2500 ? ENETC_PCS_CR_RST : ENETC_PCS_CR_RESET_AN;
Alex Marginean38882ae2019-07-03 12:11:42 +0300262 /* restart PCS AN */
263 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300264 ENETC_PCS_CR, reg);
Alex Marginean38882ae2019-07-03 12:11:42 +0300265
266 return 0;
267}
268
269/* set up MAC for RGMII */
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300270static void enetc_init_rgmii(struct udevice *dev, struct phy_device *phydev)
Alex Marginean38882ae2019-07-03 12:11:42 +0300271{
272 struct enetc_priv *priv = dev_get_priv(dev);
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300273 u32 old_val, val;
Alex Marginean38882ae2019-07-03 12:11:42 +0300274
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100275 old_val = val = enetc_read_mac_port(priv, ENETC_PM_IF_MODE);
Alex Marginean38882ae2019-07-03 12:11:42 +0300276
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300277 /* disable unreliable RGMII in-band signaling and force the MAC into
278 * the speed negotiated by the PHY.
279 */
280 val &= ~ENETC_PM_IF_MODE_AN_ENA;
281
282 if (phydev->speed == SPEED_1000) {
283 val &= ~ENETC_PM_IFM_SSP_MASK;
284 val |= ENETC_PM_IFM_SSP_1000;
285 } else if (phydev->speed == SPEED_100) {
286 val &= ~ENETC_PM_IFM_SSP_MASK;
287 val |= ENETC_PM_IFM_SSP_100;
288 } else if (phydev->speed == SPEED_10) {
289 val &= ~ENETC_PM_IFM_SSP_MASK;
290 val |= ENETC_PM_IFM_SSP_10;
291 }
292
293 if (phydev->duplex == DUPLEX_FULL)
294 val |= ENETC_PM_IFM_FULL_DPX;
295 else
296 val &= ~ENETC_PM_IFM_FULL_DPX;
297
298 if (val == old_val)
299 return;
300
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100301 enetc_write_mac_port(priv, ENETC_PM_IF_MODE, val);
Alex Marginean38882ae2019-07-03 12:11:42 +0300302}
303
Alex Margineanafad2d02020-01-10 23:32:20 +0200304/* set up MAC configuration for the given interface type */
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300305static void enetc_setup_mac_iface(struct udevice *dev,
306 struct phy_device *phydev)
Alex Marginean38882ae2019-07-03 12:11:42 +0300307{
308 struct enetc_priv *priv = dev_get_priv(dev);
309 u32 if_mode;
310
Simon Glassfada3f92022-09-17 09:00:09 -0600311 switch (priv->uclass_id) {
Alex Margineanafad2d02020-01-10 23:32:20 +0200312 case PHY_INTERFACE_MODE_RGMII:
313 case PHY_INTERFACE_MODE_RGMII_ID:
314 case PHY_INTERFACE_MODE_RGMII_RXID:
315 case PHY_INTERFACE_MODE_RGMII_TXID:
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300316 enetc_init_rgmii(dev, phydev);
Alex Margineanafad2d02020-01-10 23:32:20 +0200317 break;
Alex Margineanafad2d02020-01-10 23:32:20 +0200318 case PHY_INTERFACE_MODE_USXGMII:
Vladimir Oltean6a6e4022021-09-18 15:32:34 +0300319 case PHY_INTERFACE_MODE_10GBASER:
Alex Margineanafad2d02020-01-10 23:32:20 +0200320 /* set ifmode to (US)XGMII */
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100321 if_mode = enetc_read_mac_port(priv, ENETC_PM_IF_MODE);
Alex Margineanafad2d02020-01-10 23:32:20 +0200322 if_mode &= ~ENETC_PM_IF_IFMODE_MASK;
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100323 enetc_write_mac_port(priv, ENETC_PM_IF_MODE, if_mode);
Alex Margineanafad2d02020-01-10 23:32:20 +0200324 break;
325 };
326}
327
328/* set up serdes for SXGMII */
329static int enetc_init_sxgmii(struct udevice *dev)
330{
331 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean38882ae2019-07-03 12:11:42 +0300332
333 if (!enetc_has_imdio(dev))
334 return 0;
335
336 /* Dev ability - SXGMII */
337 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
338 ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SXGMII);
339
340 /* Restart PCS AN */
341 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
342 ENETC_PCS_CR,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300343 ENETC_PCS_CR_RST | ENETC_PCS_CR_RESET_AN);
Alex Marginean38882ae2019-07-03 12:11:42 +0300344
345 return 0;
346}
347
348/* Apply protocol specific configuration to MAC, serdes as needed */
349static void enetc_start_pcs(struct udevice *dev)
350{
351 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean38882ae2019-07-03 12:11:42 +0300352
Alex Margineand4be7682019-11-25 17:57:27 +0200353 /* register internal MDIO for debug purposes */
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100354 if (enetc_read_pcapr_mdio(priv)) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300355 priv->imdio.read = enetc_mdio_read;
356 priv->imdio.write = enetc_mdio_write;
357 priv->imdio.priv = priv->port_regs + ENETC_PM_IMDIO_BASE;
Vladimir Olteandcd21cc2021-09-27 14:21:48 +0300358 strlcpy(priv->imdio.name, dev->name, MDIO_NAME_LEN);
Alex Margineand4be7682019-11-25 17:57:27 +0200359 if (!miiphy_get_dev_by_name(priv->imdio.name))
360 mdio_register(&priv->imdio);
Alex Marginean38882ae2019-07-03 12:11:42 +0300361 }
362
Simon Glassa7ece582020-12-19 10:40:14 -0700363 if (!ofnode_valid(dev_ofnode(dev))) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300364 enetc_dbg(dev, "no enetc ofnode found, skipping PCS set-up\n");
365 return;
366 }
367
Simon Glassfada3f92022-09-17 09:00:09 -0600368 priv->uclass_id = dev_read_phy_mode(dev);
369 if (priv->uclass_id == PHY_INTERFACE_MODE_NA) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300370 enetc_dbg(dev,
371 "phy-mode property not found, defaulting to SGMII\n");
Simon Glassfada3f92022-09-17 09:00:09 -0600372 priv->uclass_id = PHY_INTERFACE_MODE_SGMII;
Marek Behúnbc194772022-04-07 00:33:01 +0200373 }
Alex Marginean38882ae2019-07-03 12:11:42 +0300374
Simon Glassfada3f92022-09-17 09:00:09 -0600375 switch (priv->uclass_id) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300376 case PHY_INTERFACE_MODE_SGMII:
Vladimir Oltean6caef972021-09-18 15:32:35 +0300377 case PHY_INTERFACE_MODE_2500BASEX:
Alex Marginean38882ae2019-07-03 12:11:42 +0300378 enetc_init_sgmii(dev);
379 break;
Alex Margineaned0460c2019-11-14 18:28:38 +0200380 case PHY_INTERFACE_MODE_USXGMII:
Vladimir Oltean6a6e4022021-09-18 15:32:34 +0300381 case PHY_INTERFACE_MODE_10GBASER:
Alex Marginean38882ae2019-07-03 12:11:42 +0300382 enetc_init_sxgmii(dev);
383 break;
384 };
385}
386
Alex Marginean02155392019-07-03 12:11:41 +0300387/* Configure the actual/external ethernet PHY, if one is found */
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300388static int enetc_config_phy(struct udevice *dev)
Alex Marginean02155392019-07-03 12:11:41 +0300389{
390 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean02155392019-07-03 12:11:41 +0300391 int supported;
392
Alex Marginean602e00f2019-11-25 17:15:13 +0200393 priv->phy = dm_eth_phy_connect(dev);
Alex Marginean602e00f2019-11-25 17:15:13 +0200394 if (!priv->phy)
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300395 return -ENODEV;
Alex Marginean02155392019-07-03 12:11:41 +0300396
Alex Margineanb93375c2019-11-14 18:58:45 +0200397 supported = PHY_GBIT_FEATURES | SUPPORTED_2500baseX_Full;
398 priv->phy->supported &= supported;
399 priv->phy->advertising &= supported;
Alex Marginean602e00f2019-11-25 17:15:13 +0200400
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300401 return phy_config(priv->phy);
Alex Marginean02155392019-07-03 12:11:41 +0300402}
403
Alex Marginean7a910c12019-07-03 12:11:40 +0300404/*
405 * Probe ENETC driver:
406 * - initialize port and station interface BARs
407 */
408static int enetc_probe(struct udevice *dev)
409{
410 struct enetc_priv *priv = dev_get_priv(dev);
Siarhei Yasinski25b798e2022-08-31 10:57:37 +0000411 int res;
Alex Marginean7a910c12019-07-03 12:11:40 +0300412
Simon Glass2e4938b2022-09-06 20:27:17 -0600413 if (ofnode_valid(dev_ofnode(dev)) && !ofnode_is_enabled(dev_ofnode(dev))) {
Alex Marginean7a910c12019-07-03 12:11:40 +0300414 enetc_dbg(dev, "interface disabled\n");
415 return -ENODEV;
416 }
417
418 priv->enetc_txbd = memalign(ENETC_BD_ALIGN,
419 sizeof(struct enetc_tx_bd) * ENETC_BD_CNT);
420 priv->enetc_rxbd = memalign(ENETC_BD_ALIGN,
421 sizeof(union enetc_rx_bd) * ENETC_BD_CNT);
422
423 if (!priv->enetc_txbd || !priv->enetc_rxbd) {
424 /* free should be able to handle NULL, just free all pointers */
425 free(priv->enetc_txbd);
426 free(priv->enetc_rxbd);
427
428 return -ENOMEM;
429 }
430
431 /* initialize register */
Andrew Scull6520c822022-04-21 16:11:13 +0000432 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 +0300433 if (!priv->regs_base) {
434 enetc_dbg(dev, "failed to map BAR0\n");
435 return -EINVAL;
436 }
437 priv->port_regs = priv->regs_base + ENETC_PORT_REGS_OFF;
438
439 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
440
Alex Margineanc905c212019-11-14 18:58:46 +0200441 enetc_start_pcs(dev);
Alex Margineanc905c212019-11-14 18:58:46 +0200442
Siarhei Yasinski25b798e2022-08-31 10:57:37 +0000443 res = enetc_config_phy(dev);
444 if(res)
445 enetc_remove(dev);
446 return res;
Alex Marginean7a910c12019-07-03 12:11:40 +0300447}
448
449/*
450 * Remove the driver from an interface:
451 * - free up allocated memory
452 */
453static int enetc_remove(struct udevice *dev)
454{
455 struct enetc_priv *priv = dev_get_priv(dev);
456
Michael Walle3f66e8e2022-05-31 18:36:16 +0200457 if (miiphy_get_dev_by_name(priv->imdio.name))
458 mdio_unregister(&priv->imdio);
459
Alex Marginean7a910c12019-07-03 12:11:40 +0300460 free(priv->enetc_txbd);
461 free(priv->enetc_rxbd);
462
463 return 0;
464}
465
Michael Walle1d3e24f2019-12-20 14:16:48 +0100466/*
467 * LS1028A is the only part with IERB at this time and there are plans to
468 * change its structure, keep this LS1028A specific for now.
469 */
470#define LS1028A_IERB_BASE 0x1f0800000ULL
471#define LS1028A_IERB_PSIPMAR0(pf, vf) (LS1028A_IERB_BASE + 0x8000 \
472 + (pf) * 0x100 + (vf) * 8)
473#define LS1028A_IERB_PSIPMAR1(pf, vf) (LS1028A_IERB_PSIPMAR0(pf, vf) + 4)
474
475static int enetc_ls1028a_write_hwaddr(struct udevice *dev)
476{
Simon Glassb75b15b2020-12-03 16:55:23 -0700477 struct pci_child_plat *ppdata = dev_get_parent_plat(dev);
Michael Walle1d3e24f2019-12-20 14:16:48 +0100478 const int devfn_to_pf[] = {0, 1, 2, -1, -1, -1, 3};
Simon Glassfa20e932020-12-03 16:55:20 -0700479 struct eth_pdata *plat = dev_get_plat(dev);
Michael Walle1d3e24f2019-12-20 14:16:48 +0100480 int devfn = PCI_FUNC(ppdata->devfn);
481 u8 *addr = plat->enetaddr;
482 u32 lower, upper;
483 int pf;
484
485 if (devfn >= ARRAY_SIZE(devfn_to_pf))
486 return 0;
487
488 pf = devfn_to_pf[devfn];
489 if (pf < 0)
490 return 0;
491
492 lower = *(const u16 *)(addr + 4);
493 upper = *(const u32 *)addr;
494
495 out_le32(LS1028A_IERB_PSIPMAR0(pf, 0), upper);
496 out_le32(LS1028A_IERB_PSIPMAR1(pf, 0), lower);
497
498 return 0;
499}
500
Michael Walle8c7188e2019-12-20 14:16:47 +0100501static int enetc_write_hwaddr(struct udevice *dev)
Alex Marginean7a910c12019-07-03 12:11:40 +0300502{
Simon Glassfa20e932020-12-03 16:55:20 -0700503 struct eth_pdata *plat = dev_get_plat(dev);
Michael Walle8c7188e2019-12-20 14:16:47 +0100504 struct enetc_priv *priv = dev_get_priv(dev);
505 u8 *addr = plat->enetaddr;
506
Marek Vasutc05f8dc2025-01-16 05:03:18 +0100507 if (enetc_is_ls1028a(dev))
Michael Walle1d3e24f2019-12-20 14:16:48 +0100508 return enetc_ls1028a_write_hwaddr(dev);
509
Alex Marginean7a910c12019-07-03 12:11:40 +0300510 u16 lower = *(const u16 *)(addr + 4);
511 u32 upper = *(const u32 *)addr;
512
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100513 enetc_write_psipmar(priv, 0, upper);
514 enetc_write_psipmar(priv, 1, lower);
Michael Walle8c7188e2019-12-20 14:16:47 +0100515
516 return 0;
Alex Marginean7a910c12019-07-03 12:11:40 +0300517}
518
519/* Configure port parameters (# of rings, frame size, enable port) */
Marek Vasutb0dc0b72025-01-16 05:03:21 +0100520static void enetc_enable_si_port(struct udevice *dev)
Alex Marginean7a910c12019-07-03 12:11:40 +0300521{
Marek Vasutb0dc0b72025-01-16 05:03:21 +0100522 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean7a910c12019-07-03 12:11:40 +0300523
524 /* set Rx/Tx BDR count */
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100525 enetc_write_psicfgr(priv, 0, ENETC_PSICFGR_SET_BDR(ENETC_RX_BDR_CNT,
526 ENETC_TX_BDR_CNT));
Alex Marginean7a910c12019-07-03 12:11:40 +0300527 /* set Rx max frame size */
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100528 enetc_write_mac_port(priv, ENETC_PM_MAXFRM, ENETC_RX_MAXFRM_SIZE);
Alex Marginean7a910c12019-07-03 12:11:40 +0300529 /* enable MAC port */
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100530 enetc_write_mac_port(priv, ENETC_PM_CC, ENETC_PM_CC_RX_TX_EN);
Alex Marginean7a910c12019-07-03 12:11:40 +0300531 /* enable port */
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100532 enetc_write_pmr(priv, ENETC_PMR_SI0_EN);
Alex Marginean7a910c12019-07-03 12:11:40 +0300533 /* set SI cache policy */
534 enetc_write(priv, ENETC_SICAR0,
535 ENETC_SICAR_RD_CFG | ENETC_SICAR_WR_CFG);
536 /* enable SI */
537 enetc_write(priv, ENETC_SIMR, ENETC_SIMR_EN);
538}
539
540/* returns DMA address for a given buffer index */
541static inline u64 enetc_rxb_address(struct udevice *dev, int i)
542{
543 return cpu_to_le64(dm_pci_virt_to_mem(dev, net_rx_packets[i]));
544}
545
546/*
547 * Setup a single Tx BD Ring (ID = 0):
548 * - set Tx buffer descriptor address
549 * - set the BD count
550 * - initialize the producer and consumer index
551 */
552static void enetc_setup_tx_bdr(struct udevice *dev)
553{
554 struct enetc_priv *priv = dev_get_priv(dev);
555 struct bd_ring *tx_bdr = &priv->tx_bdr;
556 u64 tx_bd_add = (u64)priv->enetc_txbd;
557
558 /* used later to advance to the next Tx BD */
559 tx_bdr->bd_count = ENETC_BD_CNT;
560 tx_bdr->next_prod_idx = 0;
561 tx_bdr->next_cons_idx = 0;
562 tx_bdr->cons_idx = priv->regs_base +
563 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBCIR);
564 tx_bdr->prod_idx = priv->regs_base +
565 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBPIR);
566
567 /* set Tx BD address */
568 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR0,
569 lower_32_bits(tx_bd_add));
570 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR1,
571 upper_32_bits(tx_bd_add));
572 /* set Tx 8 BD count */
573 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBLENR,
574 tx_bdr->bd_count);
575
576 /* reset both producer/consumer indexes */
577 enetc_write_reg(tx_bdr->cons_idx, tx_bdr->next_cons_idx);
578 enetc_write_reg(tx_bdr->prod_idx, tx_bdr->next_prod_idx);
579
580 /* enable TX ring */
581 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBMR, ENETC_TBMR_EN);
582}
583
584/*
585 * Setup a single Rx BD Ring (ID = 0):
586 * - set Rx buffer descriptors address (one descriptor per buffer)
587 * - set buffer size as max frame size
588 * - enable Rx ring
589 * - reset consumer and producer indexes
590 * - set buffer for each descriptor
591 */
592static void enetc_setup_rx_bdr(struct udevice *dev)
593{
594 struct enetc_priv *priv = dev_get_priv(dev);
595 struct bd_ring *rx_bdr = &priv->rx_bdr;
596 u64 rx_bd_add = (u64)priv->enetc_rxbd;
597 int i;
598
599 /* used later to advance to the next BD produced by ENETC HW */
600 rx_bdr->bd_count = ENETC_BD_CNT;
601 rx_bdr->next_prod_idx = 0;
602 rx_bdr->next_cons_idx = 0;
603 rx_bdr->cons_idx = priv->regs_base +
604 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBCIR);
605 rx_bdr->prod_idx = priv->regs_base +
606 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBPIR);
607
608 /* set Rx BD address */
609 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR0,
610 lower_32_bits(rx_bd_add));
611 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR1,
612 upper_32_bits(rx_bd_add));
613 /* set Rx BD count (multiple of 8) */
614 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBLENR,
615 rx_bdr->bd_count);
616 /* set Rx buffer size */
617 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBSR, PKTSIZE_ALIGN);
618
619 /* fill Rx BD */
620 memset(priv->enetc_rxbd, 0,
621 rx_bdr->bd_count * sizeof(union enetc_rx_bd));
622 for (i = 0; i < rx_bdr->bd_count; i++) {
623 priv->enetc_rxbd[i].w.addr = enetc_rxb_address(dev, i);
624 /* each RX buffer must be aligned to 64B */
625 WARN_ON(priv->enetc_rxbd[i].w.addr & (ARCH_DMA_MINALIGN - 1));
626 }
627
628 /* reset producer (ENETC owned) and consumer (SW owned) index */
629 enetc_write_reg(rx_bdr->cons_idx, rx_bdr->next_cons_idx);
630 enetc_write_reg(rx_bdr->prod_idx, rx_bdr->next_prod_idx);
631
632 /* enable Rx ring */
633 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBMR, ENETC_RBMR_EN);
634}
635
636/*
637 * Start ENETC interface:
638 * - perform FLR
639 * - enable access to port and SI registers
640 * - set mac address
641 * - setup TX/RX buffer descriptors
642 * - enable Tx/Rx rings
643 */
644static int enetc_start(struct udevice *dev)
645{
Alex Marginean7a910c12019-07-03 12:11:40 +0300646 struct enetc_priv *priv = dev_get_priv(dev);
647
648 /* reset and enable the PCI device */
649 dm_pci_flr(dev);
650 dm_pci_clrset_config16(dev, PCI_COMMAND, 0,
651 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
652
Marek Vasutb0dc0b72025-01-16 05:03:21 +0100653 enetc_enable_si_port(dev);
Alex Marginean7a910c12019-07-03 12:11:40 +0300654
655 /* setup Tx/Rx buffer descriptors */
656 enetc_setup_tx_bdr(dev);
657 enetc_setup_rx_bdr(dev);
658
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300659 enetc_setup_mac_iface(dev, priv->phy);
660
Vladimir Oltean19363082021-06-29 20:53:17 +0300661 return phy_startup(priv->phy);
Alex Marginean7a910c12019-07-03 12:11:40 +0300662}
663
664/*
665 * Stop the network interface:
666 * - just quiesce it, we can wipe all configuration as _start starts from
667 * scratch each time
668 */
669static void enetc_stop(struct udevice *dev)
670{
671 /* FLR is sufficient to quiesce the device */
672 dm_pci_flr(dev);
Alex Margineand4be7682019-11-25 17:57:27 +0200673 /* leave the BARs accessible after we stop, this is needed to use
674 * internal MDIO in command line.
675 */
676 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
Alex Marginean7a910c12019-07-03 12:11:40 +0300677}
678
679/*
680 * ENETC transmit packet:
681 * - check if Tx BD ring is full
682 * - set buffer/packet address (dma address)
683 * - set final fragment flag
684 * - try while producer index equals consumer index or timeout
685 */
686static int enetc_send(struct udevice *dev, void *packet, int length)
687{
688 struct enetc_priv *priv = dev_get_priv(dev);
689 struct bd_ring *txr = &priv->tx_bdr;
690 void *nv_packet = (void *)packet;
691 int tries = ENETC_POLL_TRIES;
692 u32 pi, ci;
693
694 pi = txr->next_prod_idx;
695 ci = enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK;
696 /* Tx ring is full when */
697 if (((pi + 1) % txr->bd_count) == ci) {
698 enetc_dbg(dev, "Tx BDR full\n");
699 return -ETIMEDOUT;
700 }
701 enetc_dbg(dev, "TxBD[%d]send: pkt_len=%d, buff @0x%x%08x\n", pi, length,
702 upper_32_bits((u64)nv_packet), lower_32_bits((u64)nv_packet));
703
704 /* prepare Tx BD */
705 memset(&priv->enetc_txbd[pi], 0x0, sizeof(struct enetc_tx_bd));
706 priv->enetc_txbd[pi].addr =
707 cpu_to_le64(dm_pci_virt_to_mem(dev, nv_packet));
708 priv->enetc_txbd[pi].buf_len = cpu_to_le16(length);
709 priv->enetc_txbd[pi].frm_len = cpu_to_le16(length);
710 priv->enetc_txbd[pi].flags = cpu_to_le16(ENETC_TXBD_FLAGS_F);
711 dmb();
712 /* send frame: increment producer index */
713 pi = (pi + 1) % txr->bd_count;
714 txr->next_prod_idx = pi;
715 enetc_write_reg(txr->prod_idx, pi);
716 while ((--tries >= 0) &&
717 (pi != (enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK)))
718 udelay(10);
719
720 return tries > 0 ? 0 : -ETIMEDOUT;
721}
722
723/*
724 * Receive frame:
725 * - wait for the next BD to get ready bit set
726 * - clean up the descriptor
727 * - move on and indicate to HW that the cleaned BD is available for Rx
728 */
729static int enetc_recv(struct udevice *dev, int flags, uchar **packetp)
730{
731 struct enetc_priv *priv = dev_get_priv(dev);
732 struct bd_ring *rxr = &priv->rx_bdr;
733 int tries = ENETC_POLL_TRIES;
734 int pi = rxr->next_prod_idx;
735 int ci = rxr->next_cons_idx;
736 u32 status;
737 int len;
738 u8 rdy;
739
740 do {
741 dmb();
742 status = le32_to_cpu(priv->enetc_rxbd[pi].r.lstatus);
743 /* check if current BD is ready to be consumed */
744 rdy = ENETC_RXBD_STATUS_R(status);
745 } while (--tries >= 0 && !rdy);
746
747 if (!rdy)
748 return -EAGAIN;
749
750 dmb();
751 len = le16_to_cpu(priv->enetc_rxbd[pi].r.buf_len);
752 *packetp = (uchar *)enetc_rxb_address(dev, pi);
753 enetc_dbg(dev, "RxBD[%d]: len=%d err=%d pkt=0x%x%08x\n", pi, len,
754 ENETC_RXBD_STATUS_ERRORS(status),
755 upper_32_bits((u64)*packetp), lower_32_bits((u64)*packetp));
756
757 /* BD clean up and advance to next in ring */
758 memset(&priv->enetc_rxbd[pi], 0, sizeof(union enetc_rx_bd));
759 priv->enetc_rxbd[pi].w.addr = enetc_rxb_address(dev, pi);
760 rxr->next_prod_idx = (pi + 1) % rxr->bd_count;
761 ci = (ci + 1) % rxr->bd_count;
762 rxr->next_cons_idx = ci;
763 dmb();
764 /* free up the slot in the ring for HW */
765 enetc_write_reg(rxr->cons_idx, ci);
766
767 return len;
768}
769
Marek Vasut828b2362025-01-16 05:03:22 +0100770static const struct eth_ops enetc_ops_ls = {
Alex Marginean7a910c12019-07-03 12:11:40 +0300771 .start = enetc_start,
772 .send = enetc_send,
773 .recv = enetc_recv,
774 .stop = enetc_stop,
Michael Walle8c7188e2019-12-20 14:16:47 +0100775 .write_hwaddr = enetc_write_hwaddr,
Alex Marginean7a910c12019-07-03 12:11:40 +0300776};
777
Marek Vasut828b2362025-01-16 05:03:22 +0100778U_BOOT_DRIVER(eth_enetc_ls) = {
Alex Marginean805b8592019-12-10 16:55:39 +0200779 .name = ENETC_DRIVER_NAME,
Alex Marginean7a910c12019-07-03 12:11:40 +0300780 .id = UCLASS_ETH,
781 .bind = enetc_bind,
782 .probe = enetc_probe,
783 .remove = enetc_remove,
Marek Vasut828b2362025-01-16 05:03:22 +0100784 .ops = &enetc_ops_ls,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700785 .priv_auto = sizeof(struct enetc_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700786 .plat_auto = sizeof(struct eth_pdata),
Alex Marginean7a910c12019-07-03 12:11:40 +0300787};
788
Marek Vasut828b2362025-01-16 05:03:22 +0100789static struct pci_device_id enetc_ids_ls[] = {
Alex Marginean7a910c12019-07-03 12:11:40 +0300790 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, PCI_DEVICE_ID_ENETC_ETH) },
791 {}
792};
793
Marek Vasut828b2362025-01-16 05:03:22 +0100794U_BOOT_PCI_DEVICE(eth_enetc_ls, enetc_ids_ls);