blob: e6f5ca633aa91460b80858fb1049a485b2e85a93 [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
Alex Marginean805b8592019-12-10 16:55:39 +020043/*
44 * sets the MAC address in IERB registers, this setting is persistent and
45 * carried over to Linux.
46 */
Alex Marginean805b8592019-12-10 16:55:39 +020047#define IERB_BASE 0x1f0800000ULL
48#define IERB_PFMAC(pf, vf, n) (IERB_BASE + 0x8000 + (pf) * 0x100 + (vf) * 8 \
49 + (n) * 4)
50
Marek Vasutd9b36f62025-01-16 05:03:20 +010051static void enetc_set_ierb_primary_mac(struct udevice *dev, void *blob)
Marek Vasutc9997c72025-01-16 05:03:19 +010052{
Marek Vasutd9b36f62025-01-16 05:03:20 +010053 static int ierb_fn_to_pf[] = { 0, 1, 2, -1, -1, -1, 3 };
54 struct pci_child_plat *ppdata = dev_get_parent_plat(dev);
55 struct eth_pdata *pdata = dev_get_plat(dev);
56 const u8 *enetaddr = pdata->enetaddr;
Alex Marginean805b8592019-12-10 16:55:39 +020057 u16 lower = *(const u16 *)(enetaddr + 4);
58 u32 upper = *(const u32 *)enetaddr;
Marek Vasutd9b36f62025-01-16 05:03:20 +010059 int devfn, offset;
60 char path[256];
Alex Marginean805b8592019-12-10 16:55:39 +020061
Marek Vasutc9997c72025-01-16 05:03:19 +010062 if (enetc_is_ls1028a(dev)) {
63 /*
64 * LS1028A is the only part with IERB at this time and
65 * there are plans to change its structure, keep this
66 * LS1028A specific for now.
67 */
Marek Vasutd9b36f62025-01-16 05:03:20 +010068 devfn = PCI_FUNC(ppdata->devfn);
69
Marek Vasutc9997c72025-01-16 05:03:19 +010070 if (ierb_fn_to_pf[devfn] < 0)
71 return;
Alex Marginean805b8592019-12-10 16:55:39 +020072
Marek Vasutc9997c72025-01-16 05:03:19 +010073 out_le32(IERB_PFMAC(ierb_fn_to_pf[devfn], 0, 0), upper);
74 out_le32(IERB_PFMAC(ierb_fn_to_pf[devfn], 0, 1), (u32)lower);
Marek Vasutd9b36f62025-01-16 05:03:20 +010075
76 snprintf(path, 256, "/soc/pcie@1f0000000/ethernet@%x,%x",
77 PCI_DEV(ppdata->devfn), PCI_FUNC(ppdata->devfn));
78 } else {
79 return;
Marek Vasutc9997c72025-01-16 05:03:19 +010080 }
Marek Vasutd9b36f62025-01-16 05:03:20 +010081
82 offset = fdt_path_offset(blob, path);
83 if (offset >= 0)
84 fdt_setprop(blob, offset, "mac-address", pdata->enetaddr, 6);
Alex Marginean805b8592019-12-10 16:55:39 +020085}
86
87/* sets up primary MAC addresses in DT/IERB */
88void fdt_fixup_enetc_mac(void *blob)
89{
Alex Marginean805b8592019-12-10 16:55:39 +020090 struct udevice *dev;
91 struct uclass *uc;
Alex Marginean805b8592019-12-10 16:55:39 +020092
93 uclass_get(UCLASS_ETH, &uc);
94 uclass_foreach_dev(dev, uc) {
95 if (!dev->driver || !dev->driver->name ||
96 strcmp(dev->driver->name, ENETC_DRIVER_NAME))
97 continue;
98
Marek Vasutd9b36f62025-01-16 05:03:20 +010099 enetc_set_ierb_primary_mac(dev, blob);
Alex Marginean805b8592019-12-10 16:55:39 +0200100 }
101}
102
Alex Marginean7a910c12019-07-03 12:11:40 +0300103/*
104 * Bind the device:
105 * - set a more explicit name on the interface
106 */
107static int enetc_bind(struct udevice *dev)
108{
109 char name[16];
110 static int eth_num_devices;
111
112 /*
113 * prefer using PCI function numbers to number interfaces, but these
114 * are only available if dts nodes are present. For PCI they are
115 * optional, handle that case too. Just in case some nodes are present
116 * and some are not, use different naming scheme - enetc-N based on
117 * PCI function # and enetc#N based on interface count
118 */
Simon Glassa7ece582020-12-19 10:40:14 -0700119 if (ofnode_valid(dev_ofnode(dev)))
Marek Vasutdbfb4bc2025-01-16 05:03:23 +0100120 sprintf(name, "enetc-%u", enetc_dev_id(dev));
Alex Marginean7a910c12019-07-03 12:11:40 +0300121 else
122 sprintf(name, "enetc#%u", eth_num_devices++);
123 device_set_name(dev, name);
124
125 return 0;
126}
127
Alex Marginean38882ae2019-07-03 12:11:42 +0300128/* MDIO wrappers, we're using these to drive internal MDIO to get to serdes */
129static int enetc_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
130{
131 struct enetc_mdio_priv priv;
132
133 priv.regs_base = bus->priv;
134 return enetc_mdio_read_priv(&priv, addr, devad, reg);
135}
136
137static int enetc_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
138 u16 val)
139{
140 struct enetc_mdio_priv priv;
141
142 priv.regs_base = bus->priv;
143 return enetc_mdio_write_priv(&priv, addr, devad, reg, val);
144}
145
146/* only interfaces that can pin out through serdes have internal MDIO */
147static bool enetc_has_imdio(struct udevice *dev)
148{
149 struct enetc_priv *priv = dev_get_priv(dev);
150
151 return !!(priv->imdio.priv);
152}
153
154/* set up serdes for SGMII */
155static int enetc_init_sgmii(struct udevice *dev)
156{
157 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean41a7ac52019-07-15 11:48:47 +0300158 bool is2500 = false;
159 u16 reg;
Alex Marginean38882ae2019-07-03 12:11:42 +0300160
161 if (!enetc_has_imdio(dev))
162 return 0;
163
Simon Glassfada3f92022-09-17 09:00:09 -0600164 if (priv->uclass_id == PHY_INTERFACE_MODE_2500BASEX)
Alex Marginean41a7ac52019-07-15 11:48:47 +0300165 is2500 = true;
166
167 /*
168 * Set to SGMII mode, for 1Gbps enable AN, for 2.5Gbps set fixed speed.
169 * Although fixed speed is 1Gbps, we could be running at 2.5Gbps based
170 * on PLL configuration. Setting 1G for 2.5G here is counter intuitive
171 * but intentional.
172 */
173 reg = ENETC_PCS_IF_MODE_SGMII;
174 reg |= is2500 ? ENETC_PCS_IF_MODE_SPEED_1G : ENETC_PCS_IF_MODE_SGMII_AN;
Alex Marginean38882ae2019-07-03 12:11:42 +0300175 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300176 ENETC_PCS_IF_MODE, reg);
Alex Marginean38882ae2019-07-03 12:11:42 +0300177
178 /* Dev ability - SGMII */
179 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
180 ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SGMII);
181
182 /* Adjust link timer for SGMII */
183 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
184 ENETC_PCS_LINK_TIMER1, ENETC_PCS_LINK_TIMER1_VAL);
185 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
186 ENETC_PCS_LINK_TIMER2, ENETC_PCS_LINK_TIMER2_VAL);
187
Alex Marginean41a7ac52019-07-15 11:48:47 +0300188 reg = ENETC_PCS_CR_DEF_VAL;
189 reg |= is2500 ? ENETC_PCS_CR_RST : ENETC_PCS_CR_RESET_AN;
Alex Marginean38882ae2019-07-03 12:11:42 +0300190 /* restart PCS AN */
191 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300192 ENETC_PCS_CR, reg);
Alex Marginean38882ae2019-07-03 12:11:42 +0300193
194 return 0;
195}
196
197/* set up MAC for RGMII */
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300198static void enetc_init_rgmii(struct udevice *dev, struct phy_device *phydev)
Alex Marginean38882ae2019-07-03 12:11:42 +0300199{
200 struct enetc_priv *priv = dev_get_priv(dev);
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300201 u32 old_val, val;
Alex Marginean38882ae2019-07-03 12:11:42 +0300202
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300203 old_val = val = enetc_read_port(priv, ENETC_PM_IF_MODE);
Alex Marginean38882ae2019-07-03 12:11:42 +0300204
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300205 /* disable unreliable RGMII in-band signaling and force the MAC into
206 * the speed negotiated by the PHY.
207 */
208 val &= ~ENETC_PM_IF_MODE_AN_ENA;
209
210 if (phydev->speed == SPEED_1000) {
211 val &= ~ENETC_PM_IFM_SSP_MASK;
212 val |= ENETC_PM_IFM_SSP_1000;
213 } else if (phydev->speed == SPEED_100) {
214 val &= ~ENETC_PM_IFM_SSP_MASK;
215 val |= ENETC_PM_IFM_SSP_100;
216 } else if (phydev->speed == SPEED_10) {
217 val &= ~ENETC_PM_IFM_SSP_MASK;
218 val |= ENETC_PM_IFM_SSP_10;
219 }
220
221 if (phydev->duplex == DUPLEX_FULL)
222 val |= ENETC_PM_IFM_FULL_DPX;
223 else
224 val &= ~ENETC_PM_IFM_FULL_DPX;
225
226 if (val == old_val)
227 return;
228
229 enetc_write_port(priv, ENETC_PM_IF_MODE, val);
Alex Marginean38882ae2019-07-03 12:11:42 +0300230}
231
Alex Margineanafad2d02020-01-10 23:32:20 +0200232/* set up MAC configuration for the given interface type */
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300233static void enetc_setup_mac_iface(struct udevice *dev,
234 struct phy_device *phydev)
Alex Marginean38882ae2019-07-03 12:11:42 +0300235{
236 struct enetc_priv *priv = dev_get_priv(dev);
237 u32 if_mode;
238
Simon Glassfada3f92022-09-17 09:00:09 -0600239 switch (priv->uclass_id) {
Alex Margineanafad2d02020-01-10 23:32:20 +0200240 case PHY_INTERFACE_MODE_RGMII:
241 case PHY_INTERFACE_MODE_RGMII_ID:
242 case PHY_INTERFACE_MODE_RGMII_RXID:
243 case PHY_INTERFACE_MODE_RGMII_TXID:
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300244 enetc_init_rgmii(dev, phydev);
Alex Margineanafad2d02020-01-10 23:32:20 +0200245 break;
Alex Margineanafad2d02020-01-10 23:32:20 +0200246 case PHY_INTERFACE_MODE_USXGMII:
Vladimir Oltean6a6e4022021-09-18 15:32:34 +0300247 case PHY_INTERFACE_MODE_10GBASER:
Alex Margineanafad2d02020-01-10 23:32:20 +0200248 /* set ifmode to (US)XGMII */
249 if_mode = enetc_read_port(priv, ENETC_PM_IF_MODE);
250 if_mode &= ~ENETC_PM_IF_IFMODE_MASK;
251 enetc_write_port(priv, ENETC_PM_IF_MODE, if_mode);
252 break;
253 };
254}
255
256/* set up serdes for SXGMII */
257static int enetc_init_sxgmii(struct udevice *dev)
258{
259 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean38882ae2019-07-03 12:11:42 +0300260
261 if (!enetc_has_imdio(dev))
262 return 0;
263
264 /* Dev ability - SXGMII */
265 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
266 ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SXGMII);
267
268 /* Restart PCS AN */
269 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
270 ENETC_PCS_CR,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300271 ENETC_PCS_CR_RST | ENETC_PCS_CR_RESET_AN);
Alex Marginean38882ae2019-07-03 12:11:42 +0300272
273 return 0;
274}
275
276/* Apply protocol specific configuration to MAC, serdes as needed */
277static void enetc_start_pcs(struct udevice *dev)
278{
279 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean38882ae2019-07-03 12:11:42 +0300280
Alex Margineand4be7682019-11-25 17:57:27 +0200281 /* register internal MDIO for debug purposes */
Alex Marginean38882ae2019-07-03 12:11:42 +0300282 if (enetc_read_port(priv, ENETC_PCAPR0) & ENETC_PCAPRO_MDIO) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300283 priv->imdio.read = enetc_mdio_read;
284 priv->imdio.write = enetc_mdio_write;
285 priv->imdio.priv = priv->port_regs + ENETC_PM_IMDIO_BASE;
Vladimir Olteandcd21cc2021-09-27 14:21:48 +0300286 strlcpy(priv->imdio.name, dev->name, MDIO_NAME_LEN);
Alex Margineand4be7682019-11-25 17:57:27 +0200287 if (!miiphy_get_dev_by_name(priv->imdio.name))
288 mdio_register(&priv->imdio);
Alex Marginean38882ae2019-07-03 12:11:42 +0300289 }
290
Simon Glassa7ece582020-12-19 10:40:14 -0700291 if (!ofnode_valid(dev_ofnode(dev))) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300292 enetc_dbg(dev, "no enetc ofnode found, skipping PCS set-up\n");
293 return;
294 }
295
Simon Glassfada3f92022-09-17 09:00:09 -0600296 priv->uclass_id = dev_read_phy_mode(dev);
297 if (priv->uclass_id == PHY_INTERFACE_MODE_NA) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300298 enetc_dbg(dev,
299 "phy-mode property not found, defaulting to SGMII\n");
Simon Glassfada3f92022-09-17 09:00:09 -0600300 priv->uclass_id = PHY_INTERFACE_MODE_SGMII;
Marek Behúnbc194772022-04-07 00:33:01 +0200301 }
Alex Marginean38882ae2019-07-03 12:11:42 +0300302
Simon Glassfada3f92022-09-17 09:00:09 -0600303 switch (priv->uclass_id) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300304 case PHY_INTERFACE_MODE_SGMII:
Vladimir Oltean6caef972021-09-18 15:32:35 +0300305 case PHY_INTERFACE_MODE_2500BASEX:
Alex Marginean38882ae2019-07-03 12:11:42 +0300306 enetc_init_sgmii(dev);
307 break;
Alex Margineaned0460c2019-11-14 18:28:38 +0200308 case PHY_INTERFACE_MODE_USXGMII:
Vladimir Oltean6a6e4022021-09-18 15:32:34 +0300309 case PHY_INTERFACE_MODE_10GBASER:
Alex Marginean38882ae2019-07-03 12:11:42 +0300310 enetc_init_sxgmii(dev);
311 break;
312 };
313}
314
Alex Marginean02155392019-07-03 12:11:41 +0300315/* Configure the actual/external ethernet PHY, if one is found */
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300316static int enetc_config_phy(struct udevice *dev)
Alex Marginean02155392019-07-03 12:11:41 +0300317{
318 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean02155392019-07-03 12:11:41 +0300319 int supported;
320
Alex Marginean602e00f2019-11-25 17:15:13 +0200321 priv->phy = dm_eth_phy_connect(dev);
Alex Marginean602e00f2019-11-25 17:15:13 +0200322 if (!priv->phy)
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300323 return -ENODEV;
Alex Marginean02155392019-07-03 12:11:41 +0300324
Alex Margineanb93375c2019-11-14 18:58:45 +0200325 supported = PHY_GBIT_FEATURES | SUPPORTED_2500baseX_Full;
326 priv->phy->supported &= supported;
327 priv->phy->advertising &= supported;
Alex Marginean602e00f2019-11-25 17:15:13 +0200328
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300329 return phy_config(priv->phy);
Alex Marginean02155392019-07-03 12:11:41 +0300330}
331
Alex Marginean7a910c12019-07-03 12:11:40 +0300332/*
333 * Probe ENETC driver:
334 * - initialize port and station interface BARs
335 */
336static int enetc_probe(struct udevice *dev)
337{
338 struct enetc_priv *priv = dev_get_priv(dev);
Siarhei Yasinski25b798e2022-08-31 10:57:37 +0000339 int res;
Alex Marginean7a910c12019-07-03 12:11:40 +0300340
Simon Glass2e4938b2022-09-06 20:27:17 -0600341 if (ofnode_valid(dev_ofnode(dev)) && !ofnode_is_enabled(dev_ofnode(dev))) {
Alex Marginean7a910c12019-07-03 12:11:40 +0300342 enetc_dbg(dev, "interface disabled\n");
343 return -ENODEV;
344 }
345
346 priv->enetc_txbd = memalign(ENETC_BD_ALIGN,
347 sizeof(struct enetc_tx_bd) * ENETC_BD_CNT);
348 priv->enetc_rxbd = memalign(ENETC_BD_ALIGN,
349 sizeof(union enetc_rx_bd) * ENETC_BD_CNT);
350
351 if (!priv->enetc_txbd || !priv->enetc_rxbd) {
352 /* free should be able to handle NULL, just free all pointers */
353 free(priv->enetc_txbd);
354 free(priv->enetc_rxbd);
355
356 return -ENOMEM;
357 }
358
359 /* initialize register */
Andrew Scull6520c822022-04-21 16:11:13 +0000360 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 +0300361 if (!priv->regs_base) {
362 enetc_dbg(dev, "failed to map BAR0\n");
363 return -EINVAL;
364 }
365 priv->port_regs = priv->regs_base + ENETC_PORT_REGS_OFF;
366
367 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
368
Alex Margineanc905c212019-11-14 18:58:46 +0200369 enetc_start_pcs(dev);
Alex Margineanc905c212019-11-14 18:58:46 +0200370
Siarhei Yasinski25b798e2022-08-31 10:57:37 +0000371 res = enetc_config_phy(dev);
372 if(res)
373 enetc_remove(dev);
374 return res;
Alex Marginean7a910c12019-07-03 12:11:40 +0300375}
376
377/*
378 * Remove the driver from an interface:
379 * - free up allocated memory
380 */
381static int enetc_remove(struct udevice *dev)
382{
383 struct enetc_priv *priv = dev_get_priv(dev);
384
Michael Walle3f66e8e2022-05-31 18:36:16 +0200385 if (miiphy_get_dev_by_name(priv->imdio.name))
386 mdio_unregister(&priv->imdio);
387
Alex Marginean7a910c12019-07-03 12:11:40 +0300388 free(priv->enetc_txbd);
389 free(priv->enetc_rxbd);
390
391 return 0;
392}
393
Michael Walle1d3e24f2019-12-20 14:16:48 +0100394/*
395 * LS1028A is the only part with IERB at this time and there are plans to
396 * change its structure, keep this LS1028A specific for now.
397 */
398#define LS1028A_IERB_BASE 0x1f0800000ULL
399#define LS1028A_IERB_PSIPMAR0(pf, vf) (LS1028A_IERB_BASE + 0x8000 \
400 + (pf) * 0x100 + (vf) * 8)
401#define LS1028A_IERB_PSIPMAR1(pf, vf) (LS1028A_IERB_PSIPMAR0(pf, vf) + 4)
402
403static int enetc_ls1028a_write_hwaddr(struct udevice *dev)
404{
Simon Glassb75b15b2020-12-03 16:55:23 -0700405 struct pci_child_plat *ppdata = dev_get_parent_plat(dev);
Michael Walle1d3e24f2019-12-20 14:16:48 +0100406 const int devfn_to_pf[] = {0, 1, 2, -1, -1, -1, 3};
Simon Glassfa20e932020-12-03 16:55:20 -0700407 struct eth_pdata *plat = dev_get_plat(dev);
Michael Walle1d3e24f2019-12-20 14:16:48 +0100408 int devfn = PCI_FUNC(ppdata->devfn);
409 u8 *addr = plat->enetaddr;
410 u32 lower, upper;
411 int pf;
412
413 if (devfn >= ARRAY_SIZE(devfn_to_pf))
414 return 0;
415
416 pf = devfn_to_pf[devfn];
417 if (pf < 0)
418 return 0;
419
420 lower = *(const u16 *)(addr + 4);
421 upper = *(const u32 *)addr;
422
423 out_le32(LS1028A_IERB_PSIPMAR0(pf, 0), upper);
424 out_le32(LS1028A_IERB_PSIPMAR1(pf, 0), lower);
425
426 return 0;
427}
428
Michael Walle8c7188e2019-12-20 14:16:47 +0100429static int enetc_write_hwaddr(struct udevice *dev)
Alex Marginean7a910c12019-07-03 12:11:40 +0300430{
Simon Glassfa20e932020-12-03 16:55:20 -0700431 struct eth_pdata *plat = dev_get_plat(dev);
Michael Walle8c7188e2019-12-20 14:16:47 +0100432 struct enetc_priv *priv = dev_get_priv(dev);
433 u8 *addr = plat->enetaddr;
434
Marek Vasutc05f8dc2025-01-16 05:03:18 +0100435 if (enetc_is_ls1028a(dev))
Michael Walle1d3e24f2019-12-20 14:16:48 +0100436 return enetc_ls1028a_write_hwaddr(dev);
437
Alex Marginean7a910c12019-07-03 12:11:40 +0300438 u16 lower = *(const u16 *)(addr + 4);
439 u32 upper = *(const u32 *)addr;
440
441 enetc_write_port(priv, ENETC_PSIPMAR0, upper);
442 enetc_write_port(priv, ENETC_PSIPMAR1, lower);
Michael Walle8c7188e2019-12-20 14:16:47 +0100443
444 return 0;
Alex Marginean7a910c12019-07-03 12:11:40 +0300445}
446
447/* Configure port parameters (# of rings, frame size, enable port) */
Marek Vasutb0dc0b72025-01-16 05:03:21 +0100448static void enetc_enable_si_port(struct udevice *dev)
Alex Marginean7a910c12019-07-03 12:11:40 +0300449{
Marek Vasutb0dc0b72025-01-16 05:03:21 +0100450 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean7a910c12019-07-03 12:11:40 +0300451 u32 val;
452
453 /* set Rx/Tx BDR count */
454 val = ENETC_PSICFGR_SET_TXBDR(ENETC_TX_BDR_CNT);
455 val |= ENETC_PSICFGR_SET_RXBDR(ENETC_RX_BDR_CNT);
456 enetc_write_port(priv, ENETC_PSICFGR(0), val);
457 /* set Rx max frame size */
458 enetc_write_port(priv, ENETC_PM_MAXFRM, ENETC_RX_MAXFRM_SIZE);
459 /* enable MAC port */
460 enetc_write_port(priv, ENETC_PM_CC, ENETC_PM_CC_RX_TX_EN);
461 /* enable port */
462 enetc_write_port(priv, ENETC_PMR, ENETC_PMR_SI0_EN);
463 /* set SI cache policy */
464 enetc_write(priv, ENETC_SICAR0,
465 ENETC_SICAR_RD_CFG | ENETC_SICAR_WR_CFG);
466 /* enable SI */
467 enetc_write(priv, ENETC_SIMR, ENETC_SIMR_EN);
468}
469
470/* returns DMA address for a given buffer index */
471static inline u64 enetc_rxb_address(struct udevice *dev, int i)
472{
473 return cpu_to_le64(dm_pci_virt_to_mem(dev, net_rx_packets[i]));
474}
475
476/*
477 * Setup a single Tx BD Ring (ID = 0):
478 * - set Tx buffer descriptor address
479 * - set the BD count
480 * - initialize the producer and consumer index
481 */
482static void enetc_setup_tx_bdr(struct udevice *dev)
483{
484 struct enetc_priv *priv = dev_get_priv(dev);
485 struct bd_ring *tx_bdr = &priv->tx_bdr;
486 u64 tx_bd_add = (u64)priv->enetc_txbd;
487
488 /* used later to advance to the next Tx BD */
489 tx_bdr->bd_count = ENETC_BD_CNT;
490 tx_bdr->next_prod_idx = 0;
491 tx_bdr->next_cons_idx = 0;
492 tx_bdr->cons_idx = priv->regs_base +
493 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBCIR);
494 tx_bdr->prod_idx = priv->regs_base +
495 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBPIR);
496
497 /* set Tx BD address */
498 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR0,
499 lower_32_bits(tx_bd_add));
500 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR1,
501 upper_32_bits(tx_bd_add));
502 /* set Tx 8 BD count */
503 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBLENR,
504 tx_bdr->bd_count);
505
506 /* reset both producer/consumer indexes */
507 enetc_write_reg(tx_bdr->cons_idx, tx_bdr->next_cons_idx);
508 enetc_write_reg(tx_bdr->prod_idx, tx_bdr->next_prod_idx);
509
510 /* enable TX ring */
511 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBMR, ENETC_TBMR_EN);
512}
513
514/*
515 * Setup a single Rx BD Ring (ID = 0):
516 * - set Rx buffer descriptors address (one descriptor per buffer)
517 * - set buffer size as max frame size
518 * - enable Rx ring
519 * - reset consumer and producer indexes
520 * - set buffer for each descriptor
521 */
522static void enetc_setup_rx_bdr(struct udevice *dev)
523{
524 struct enetc_priv *priv = dev_get_priv(dev);
525 struct bd_ring *rx_bdr = &priv->rx_bdr;
526 u64 rx_bd_add = (u64)priv->enetc_rxbd;
527 int i;
528
529 /* used later to advance to the next BD produced by ENETC HW */
530 rx_bdr->bd_count = ENETC_BD_CNT;
531 rx_bdr->next_prod_idx = 0;
532 rx_bdr->next_cons_idx = 0;
533 rx_bdr->cons_idx = priv->regs_base +
534 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBCIR);
535 rx_bdr->prod_idx = priv->regs_base +
536 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBPIR);
537
538 /* set Rx BD address */
539 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR0,
540 lower_32_bits(rx_bd_add));
541 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR1,
542 upper_32_bits(rx_bd_add));
543 /* set Rx BD count (multiple of 8) */
544 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBLENR,
545 rx_bdr->bd_count);
546 /* set Rx buffer size */
547 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBSR, PKTSIZE_ALIGN);
548
549 /* fill Rx BD */
550 memset(priv->enetc_rxbd, 0,
551 rx_bdr->bd_count * sizeof(union enetc_rx_bd));
552 for (i = 0; i < rx_bdr->bd_count; i++) {
553 priv->enetc_rxbd[i].w.addr = enetc_rxb_address(dev, i);
554 /* each RX buffer must be aligned to 64B */
555 WARN_ON(priv->enetc_rxbd[i].w.addr & (ARCH_DMA_MINALIGN - 1));
556 }
557
558 /* reset producer (ENETC owned) and consumer (SW owned) index */
559 enetc_write_reg(rx_bdr->cons_idx, rx_bdr->next_cons_idx);
560 enetc_write_reg(rx_bdr->prod_idx, rx_bdr->next_prod_idx);
561
562 /* enable Rx ring */
563 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBMR, ENETC_RBMR_EN);
564}
565
566/*
567 * Start ENETC interface:
568 * - perform FLR
569 * - enable access to port and SI registers
570 * - set mac address
571 * - setup TX/RX buffer descriptors
572 * - enable Tx/Rx rings
573 */
574static int enetc_start(struct udevice *dev)
575{
Alex Marginean7a910c12019-07-03 12:11:40 +0300576 struct enetc_priv *priv = dev_get_priv(dev);
577
578 /* reset and enable the PCI device */
579 dm_pci_flr(dev);
580 dm_pci_clrset_config16(dev, PCI_COMMAND, 0,
581 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
582
Marek Vasutb0dc0b72025-01-16 05:03:21 +0100583 enetc_enable_si_port(dev);
Alex Marginean7a910c12019-07-03 12:11:40 +0300584
585 /* setup Tx/Rx buffer descriptors */
586 enetc_setup_tx_bdr(dev);
587 enetc_setup_rx_bdr(dev);
588
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300589 enetc_setup_mac_iface(dev, priv->phy);
590
Vladimir Oltean19363082021-06-29 20:53:17 +0300591 return phy_startup(priv->phy);
Alex Marginean7a910c12019-07-03 12:11:40 +0300592}
593
594/*
595 * Stop the network interface:
596 * - just quiesce it, we can wipe all configuration as _start starts from
597 * scratch each time
598 */
599static void enetc_stop(struct udevice *dev)
600{
601 /* FLR is sufficient to quiesce the device */
602 dm_pci_flr(dev);
Alex Margineand4be7682019-11-25 17:57:27 +0200603 /* leave the BARs accessible after we stop, this is needed to use
604 * internal MDIO in command line.
605 */
606 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
Alex Marginean7a910c12019-07-03 12:11:40 +0300607}
608
609/*
610 * ENETC transmit packet:
611 * - check if Tx BD ring is full
612 * - set buffer/packet address (dma address)
613 * - set final fragment flag
614 * - try while producer index equals consumer index or timeout
615 */
616static int enetc_send(struct udevice *dev, void *packet, int length)
617{
618 struct enetc_priv *priv = dev_get_priv(dev);
619 struct bd_ring *txr = &priv->tx_bdr;
620 void *nv_packet = (void *)packet;
621 int tries = ENETC_POLL_TRIES;
622 u32 pi, ci;
623
624 pi = txr->next_prod_idx;
625 ci = enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK;
626 /* Tx ring is full when */
627 if (((pi + 1) % txr->bd_count) == ci) {
628 enetc_dbg(dev, "Tx BDR full\n");
629 return -ETIMEDOUT;
630 }
631 enetc_dbg(dev, "TxBD[%d]send: pkt_len=%d, buff @0x%x%08x\n", pi, length,
632 upper_32_bits((u64)nv_packet), lower_32_bits((u64)nv_packet));
633
634 /* prepare Tx BD */
635 memset(&priv->enetc_txbd[pi], 0x0, sizeof(struct enetc_tx_bd));
636 priv->enetc_txbd[pi].addr =
637 cpu_to_le64(dm_pci_virt_to_mem(dev, nv_packet));
638 priv->enetc_txbd[pi].buf_len = cpu_to_le16(length);
639 priv->enetc_txbd[pi].frm_len = cpu_to_le16(length);
640 priv->enetc_txbd[pi].flags = cpu_to_le16(ENETC_TXBD_FLAGS_F);
641 dmb();
642 /* send frame: increment producer index */
643 pi = (pi + 1) % txr->bd_count;
644 txr->next_prod_idx = pi;
645 enetc_write_reg(txr->prod_idx, pi);
646 while ((--tries >= 0) &&
647 (pi != (enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK)))
648 udelay(10);
649
650 return tries > 0 ? 0 : -ETIMEDOUT;
651}
652
653/*
654 * Receive frame:
655 * - wait for the next BD to get ready bit set
656 * - clean up the descriptor
657 * - move on and indicate to HW that the cleaned BD is available for Rx
658 */
659static int enetc_recv(struct udevice *dev, int flags, uchar **packetp)
660{
661 struct enetc_priv *priv = dev_get_priv(dev);
662 struct bd_ring *rxr = &priv->rx_bdr;
663 int tries = ENETC_POLL_TRIES;
664 int pi = rxr->next_prod_idx;
665 int ci = rxr->next_cons_idx;
666 u32 status;
667 int len;
668 u8 rdy;
669
670 do {
671 dmb();
672 status = le32_to_cpu(priv->enetc_rxbd[pi].r.lstatus);
673 /* check if current BD is ready to be consumed */
674 rdy = ENETC_RXBD_STATUS_R(status);
675 } while (--tries >= 0 && !rdy);
676
677 if (!rdy)
678 return -EAGAIN;
679
680 dmb();
681 len = le16_to_cpu(priv->enetc_rxbd[pi].r.buf_len);
682 *packetp = (uchar *)enetc_rxb_address(dev, pi);
683 enetc_dbg(dev, "RxBD[%d]: len=%d err=%d pkt=0x%x%08x\n", pi, len,
684 ENETC_RXBD_STATUS_ERRORS(status),
685 upper_32_bits((u64)*packetp), lower_32_bits((u64)*packetp));
686
687 /* BD clean up and advance to next in ring */
688 memset(&priv->enetc_rxbd[pi], 0, sizeof(union enetc_rx_bd));
689 priv->enetc_rxbd[pi].w.addr = enetc_rxb_address(dev, pi);
690 rxr->next_prod_idx = (pi + 1) % rxr->bd_count;
691 ci = (ci + 1) % rxr->bd_count;
692 rxr->next_cons_idx = ci;
693 dmb();
694 /* free up the slot in the ring for HW */
695 enetc_write_reg(rxr->cons_idx, ci);
696
697 return len;
698}
699
Marek Vasut828b2362025-01-16 05:03:22 +0100700static const struct eth_ops enetc_ops_ls = {
Alex Marginean7a910c12019-07-03 12:11:40 +0300701 .start = enetc_start,
702 .send = enetc_send,
703 .recv = enetc_recv,
704 .stop = enetc_stop,
Michael Walle8c7188e2019-12-20 14:16:47 +0100705 .write_hwaddr = enetc_write_hwaddr,
Alex Marginean7a910c12019-07-03 12:11:40 +0300706};
707
Marek Vasut828b2362025-01-16 05:03:22 +0100708U_BOOT_DRIVER(eth_enetc_ls) = {
Alex Marginean805b8592019-12-10 16:55:39 +0200709 .name = ENETC_DRIVER_NAME,
Alex Marginean7a910c12019-07-03 12:11:40 +0300710 .id = UCLASS_ETH,
711 .bind = enetc_bind,
712 .probe = enetc_probe,
713 .remove = enetc_remove,
Marek Vasut828b2362025-01-16 05:03:22 +0100714 .ops = &enetc_ops_ls,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700715 .priv_auto = sizeof(struct enetc_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700716 .plat_auto = sizeof(struct eth_pdata),
Alex Marginean7a910c12019-07-03 12:11:40 +0300717};
718
Marek Vasut828b2362025-01-16 05:03:22 +0100719static struct pci_device_id enetc_ids_ls[] = {
Alex Marginean7a910c12019-07-03 12:11:40 +0300720 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, PCI_DEVICE_ID_ENETC_ETH) },
721 {}
722};
723
Marek Vasut828b2362025-01-16 05:03:22 +0100724U_BOOT_PCI_DEVICE(eth_enetc_ls, enetc_ids_ls);