blob: 615646b76a31ec819724a8118870d0db7084d276 [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
Alex Marginean805b8592019-12-10 16:55:39 +020035/*
36 * sets the MAC address in IERB registers, this setting is persistent and
37 * carried over to Linux.
38 */
Alex Marginean805b8592019-12-10 16:55:39 +020039#define IERB_BASE 0x1f0800000ULL
40#define IERB_PFMAC(pf, vf, n) (IERB_BASE + 0x8000 + (pf) * 0x100 + (vf) * 8 \
41 + (n) * 4)
42
Marek Vasutd9b36f62025-01-16 05:03:20 +010043static void enetc_set_ierb_primary_mac(struct udevice *dev, void *blob)
Marek Vasutc9997c72025-01-16 05:03:19 +010044{
Marek Vasutd9b36f62025-01-16 05:03:20 +010045 static int ierb_fn_to_pf[] = { 0, 1, 2, -1, -1, -1, 3 };
46 struct pci_child_plat *ppdata = dev_get_parent_plat(dev);
47 struct eth_pdata *pdata = dev_get_plat(dev);
48 const u8 *enetaddr = pdata->enetaddr;
Alex Marginean805b8592019-12-10 16:55:39 +020049 u16 lower = *(const u16 *)(enetaddr + 4);
50 u32 upper = *(const u32 *)enetaddr;
Marek Vasutd9b36f62025-01-16 05:03:20 +010051 int devfn, offset;
52 char path[256];
Alex Marginean805b8592019-12-10 16:55:39 +020053
Marek Vasutc9997c72025-01-16 05:03:19 +010054 if (enetc_is_ls1028a(dev)) {
55 /*
56 * LS1028A is the only part with IERB at this time and
57 * there are plans to change its structure, keep this
58 * LS1028A specific for now.
59 */
Marek Vasutd9b36f62025-01-16 05:03:20 +010060 devfn = PCI_FUNC(ppdata->devfn);
61
Marek Vasutc9997c72025-01-16 05:03:19 +010062 if (ierb_fn_to_pf[devfn] < 0)
63 return;
Alex Marginean805b8592019-12-10 16:55:39 +020064
Marek Vasutc9997c72025-01-16 05:03:19 +010065 out_le32(IERB_PFMAC(ierb_fn_to_pf[devfn], 0, 0), upper);
66 out_le32(IERB_PFMAC(ierb_fn_to_pf[devfn], 0, 1), (u32)lower);
Marek Vasutd9b36f62025-01-16 05:03:20 +010067
68 snprintf(path, 256, "/soc/pcie@1f0000000/ethernet@%x,%x",
69 PCI_DEV(ppdata->devfn), PCI_FUNC(ppdata->devfn));
70 } else {
71 return;
Marek Vasutc9997c72025-01-16 05:03:19 +010072 }
Marek Vasutd9b36f62025-01-16 05:03:20 +010073
74 offset = fdt_path_offset(blob, path);
75 if (offset >= 0)
76 fdt_setprop(blob, offset, "mac-address", pdata->enetaddr, 6);
Alex Marginean805b8592019-12-10 16:55:39 +020077}
78
79/* sets up primary MAC addresses in DT/IERB */
80void fdt_fixup_enetc_mac(void *blob)
81{
Alex Marginean805b8592019-12-10 16:55:39 +020082 struct udevice *dev;
83 struct uclass *uc;
Alex Marginean805b8592019-12-10 16:55:39 +020084
85 uclass_get(UCLASS_ETH, &uc);
86 uclass_foreach_dev(dev, uc) {
87 if (!dev->driver || !dev->driver->name ||
88 strcmp(dev->driver->name, ENETC_DRIVER_NAME))
89 continue;
90
Marek Vasutd9b36f62025-01-16 05:03:20 +010091 enetc_set_ierb_primary_mac(dev, blob);
Alex Marginean805b8592019-12-10 16:55:39 +020092 }
93}
94
Alex Marginean7a910c12019-07-03 12:11:40 +030095/*
96 * Bind the device:
97 * - set a more explicit name on the interface
98 */
99static int enetc_bind(struct udevice *dev)
100{
101 char name[16];
102 static int eth_num_devices;
103
104 /*
105 * prefer using PCI function numbers to number interfaces, but these
106 * are only available if dts nodes are present. For PCI they are
107 * optional, handle that case too. Just in case some nodes are present
108 * and some are not, use different naming scheme - enetc-N based on
109 * PCI function # and enetc#N based on interface count
110 */
Simon Glassa7ece582020-12-19 10:40:14 -0700111 if (ofnode_valid(dev_ofnode(dev)))
Alex Marginean7a910c12019-07-03 12:11:40 +0300112 sprintf(name, "enetc-%u", PCI_FUNC(pci_get_devfn(dev)));
113 else
114 sprintf(name, "enetc#%u", eth_num_devices++);
115 device_set_name(dev, name);
116
117 return 0;
118}
119
Alex Marginean38882ae2019-07-03 12:11:42 +0300120/* MDIO wrappers, we're using these to drive internal MDIO to get to serdes */
121static int enetc_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
122{
123 struct enetc_mdio_priv priv;
124
125 priv.regs_base = bus->priv;
126 return enetc_mdio_read_priv(&priv, addr, devad, reg);
127}
128
129static int enetc_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
130 u16 val)
131{
132 struct enetc_mdio_priv priv;
133
134 priv.regs_base = bus->priv;
135 return enetc_mdio_write_priv(&priv, addr, devad, reg, val);
136}
137
138/* only interfaces that can pin out through serdes have internal MDIO */
139static bool enetc_has_imdio(struct udevice *dev)
140{
141 struct enetc_priv *priv = dev_get_priv(dev);
142
143 return !!(priv->imdio.priv);
144}
145
146/* set up serdes for SGMII */
147static int enetc_init_sgmii(struct udevice *dev)
148{
149 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean41a7ac52019-07-15 11:48:47 +0300150 bool is2500 = false;
151 u16 reg;
Alex Marginean38882ae2019-07-03 12:11:42 +0300152
153 if (!enetc_has_imdio(dev))
154 return 0;
155
Simon Glassfada3f92022-09-17 09:00:09 -0600156 if (priv->uclass_id == PHY_INTERFACE_MODE_2500BASEX)
Alex Marginean41a7ac52019-07-15 11:48:47 +0300157 is2500 = true;
158
159 /*
160 * Set to SGMII mode, for 1Gbps enable AN, for 2.5Gbps set fixed speed.
161 * Although fixed speed is 1Gbps, we could be running at 2.5Gbps based
162 * on PLL configuration. Setting 1G for 2.5G here is counter intuitive
163 * but intentional.
164 */
165 reg = ENETC_PCS_IF_MODE_SGMII;
166 reg |= is2500 ? ENETC_PCS_IF_MODE_SPEED_1G : ENETC_PCS_IF_MODE_SGMII_AN;
Alex Marginean38882ae2019-07-03 12:11:42 +0300167 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300168 ENETC_PCS_IF_MODE, reg);
Alex Marginean38882ae2019-07-03 12:11:42 +0300169
170 /* Dev ability - SGMII */
171 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
172 ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SGMII);
173
174 /* Adjust link timer for SGMII */
175 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
176 ENETC_PCS_LINK_TIMER1, ENETC_PCS_LINK_TIMER1_VAL);
177 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
178 ENETC_PCS_LINK_TIMER2, ENETC_PCS_LINK_TIMER2_VAL);
179
Alex Marginean41a7ac52019-07-15 11:48:47 +0300180 reg = ENETC_PCS_CR_DEF_VAL;
181 reg |= is2500 ? ENETC_PCS_CR_RST : ENETC_PCS_CR_RESET_AN;
Alex Marginean38882ae2019-07-03 12:11:42 +0300182 /* restart PCS AN */
183 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300184 ENETC_PCS_CR, reg);
Alex Marginean38882ae2019-07-03 12:11:42 +0300185
186 return 0;
187}
188
189/* set up MAC for RGMII */
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300190static void enetc_init_rgmii(struct udevice *dev, struct phy_device *phydev)
Alex Marginean38882ae2019-07-03 12:11:42 +0300191{
192 struct enetc_priv *priv = dev_get_priv(dev);
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300193 u32 old_val, val;
Alex Marginean38882ae2019-07-03 12:11:42 +0300194
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300195 old_val = val = enetc_read_port(priv, ENETC_PM_IF_MODE);
Alex Marginean38882ae2019-07-03 12:11:42 +0300196
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300197 /* disable unreliable RGMII in-band signaling and force the MAC into
198 * the speed negotiated by the PHY.
199 */
200 val &= ~ENETC_PM_IF_MODE_AN_ENA;
201
202 if (phydev->speed == SPEED_1000) {
203 val &= ~ENETC_PM_IFM_SSP_MASK;
204 val |= ENETC_PM_IFM_SSP_1000;
205 } else if (phydev->speed == SPEED_100) {
206 val &= ~ENETC_PM_IFM_SSP_MASK;
207 val |= ENETC_PM_IFM_SSP_100;
208 } else if (phydev->speed == SPEED_10) {
209 val &= ~ENETC_PM_IFM_SSP_MASK;
210 val |= ENETC_PM_IFM_SSP_10;
211 }
212
213 if (phydev->duplex == DUPLEX_FULL)
214 val |= ENETC_PM_IFM_FULL_DPX;
215 else
216 val &= ~ENETC_PM_IFM_FULL_DPX;
217
218 if (val == old_val)
219 return;
220
221 enetc_write_port(priv, ENETC_PM_IF_MODE, val);
Alex Marginean38882ae2019-07-03 12:11:42 +0300222}
223
Alex Margineanafad2d02020-01-10 23:32:20 +0200224/* set up MAC configuration for the given interface type */
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300225static void enetc_setup_mac_iface(struct udevice *dev,
226 struct phy_device *phydev)
Alex Marginean38882ae2019-07-03 12:11:42 +0300227{
228 struct enetc_priv *priv = dev_get_priv(dev);
229 u32 if_mode;
230
Simon Glassfada3f92022-09-17 09:00:09 -0600231 switch (priv->uclass_id) {
Alex Margineanafad2d02020-01-10 23:32:20 +0200232 case PHY_INTERFACE_MODE_RGMII:
233 case PHY_INTERFACE_MODE_RGMII_ID:
234 case PHY_INTERFACE_MODE_RGMII_RXID:
235 case PHY_INTERFACE_MODE_RGMII_TXID:
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300236 enetc_init_rgmii(dev, phydev);
Alex Margineanafad2d02020-01-10 23:32:20 +0200237 break;
Alex Margineanafad2d02020-01-10 23:32:20 +0200238 case PHY_INTERFACE_MODE_USXGMII:
Vladimir Oltean6a6e4022021-09-18 15:32:34 +0300239 case PHY_INTERFACE_MODE_10GBASER:
Alex Margineanafad2d02020-01-10 23:32:20 +0200240 /* set ifmode to (US)XGMII */
241 if_mode = enetc_read_port(priv, ENETC_PM_IF_MODE);
242 if_mode &= ~ENETC_PM_IF_IFMODE_MASK;
243 enetc_write_port(priv, ENETC_PM_IF_MODE, if_mode);
244 break;
245 };
246}
247
248/* set up serdes for SXGMII */
249static int enetc_init_sxgmii(struct udevice *dev)
250{
251 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean38882ae2019-07-03 12:11:42 +0300252
253 if (!enetc_has_imdio(dev))
254 return 0;
255
256 /* Dev ability - SXGMII */
257 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
258 ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SXGMII);
259
260 /* Restart PCS AN */
261 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
262 ENETC_PCS_CR,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300263 ENETC_PCS_CR_RST | ENETC_PCS_CR_RESET_AN);
Alex Marginean38882ae2019-07-03 12:11:42 +0300264
265 return 0;
266}
267
268/* Apply protocol specific configuration to MAC, serdes as needed */
269static void enetc_start_pcs(struct udevice *dev)
270{
271 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean38882ae2019-07-03 12:11:42 +0300272
Alex Margineand4be7682019-11-25 17:57:27 +0200273 /* register internal MDIO for debug purposes */
Alex Marginean38882ae2019-07-03 12:11:42 +0300274 if (enetc_read_port(priv, ENETC_PCAPR0) & ENETC_PCAPRO_MDIO) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300275 priv->imdio.read = enetc_mdio_read;
276 priv->imdio.write = enetc_mdio_write;
277 priv->imdio.priv = priv->port_regs + ENETC_PM_IMDIO_BASE;
Vladimir Olteandcd21cc2021-09-27 14:21:48 +0300278 strlcpy(priv->imdio.name, dev->name, MDIO_NAME_LEN);
Alex Margineand4be7682019-11-25 17:57:27 +0200279 if (!miiphy_get_dev_by_name(priv->imdio.name))
280 mdio_register(&priv->imdio);
Alex Marginean38882ae2019-07-03 12:11:42 +0300281 }
282
Simon Glassa7ece582020-12-19 10:40:14 -0700283 if (!ofnode_valid(dev_ofnode(dev))) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300284 enetc_dbg(dev, "no enetc ofnode found, skipping PCS set-up\n");
285 return;
286 }
287
Simon Glassfada3f92022-09-17 09:00:09 -0600288 priv->uclass_id = dev_read_phy_mode(dev);
289 if (priv->uclass_id == PHY_INTERFACE_MODE_NA) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300290 enetc_dbg(dev,
291 "phy-mode property not found, defaulting to SGMII\n");
Simon Glassfada3f92022-09-17 09:00:09 -0600292 priv->uclass_id = PHY_INTERFACE_MODE_SGMII;
Marek Behúnbc194772022-04-07 00:33:01 +0200293 }
Alex Marginean38882ae2019-07-03 12:11:42 +0300294
Simon Glassfada3f92022-09-17 09:00:09 -0600295 switch (priv->uclass_id) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300296 case PHY_INTERFACE_MODE_SGMII:
Vladimir Oltean6caef972021-09-18 15:32:35 +0300297 case PHY_INTERFACE_MODE_2500BASEX:
Alex Marginean38882ae2019-07-03 12:11:42 +0300298 enetc_init_sgmii(dev);
299 break;
Alex Margineaned0460c2019-11-14 18:28:38 +0200300 case PHY_INTERFACE_MODE_USXGMII:
Vladimir Oltean6a6e4022021-09-18 15:32:34 +0300301 case PHY_INTERFACE_MODE_10GBASER:
Alex Marginean38882ae2019-07-03 12:11:42 +0300302 enetc_init_sxgmii(dev);
303 break;
304 };
305}
306
Alex Marginean02155392019-07-03 12:11:41 +0300307/* Configure the actual/external ethernet PHY, if one is found */
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300308static int enetc_config_phy(struct udevice *dev)
Alex Marginean02155392019-07-03 12:11:41 +0300309{
310 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean02155392019-07-03 12:11:41 +0300311 int supported;
312
Alex Marginean602e00f2019-11-25 17:15:13 +0200313 priv->phy = dm_eth_phy_connect(dev);
Alex Marginean602e00f2019-11-25 17:15:13 +0200314 if (!priv->phy)
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300315 return -ENODEV;
Alex Marginean02155392019-07-03 12:11:41 +0300316
Alex Margineanb93375c2019-11-14 18:58:45 +0200317 supported = PHY_GBIT_FEATURES | SUPPORTED_2500baseX_Full;
318 priv->phy->supported &= supported;
319 priv->phy->advertising &= supported;
Alex Marginean602e00f2019-11-25 17:15:13 +0200320
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300321 return phy_config(priv->phy);
Alex Marginean02155392019-07-03 12:11:41 +0300322}
323
Alex Marginean7a910c12019-07-03 12:11:40 +0300324/*
325 * Probe ENETC driver:
326 * - initialize port and station interface BARs
327 */
328static int enetc_probe(struct udevice *dev)
329{
330 struct enetc_priv *priv = dev_get_priv(dev);
Siarhei Yasinski25b798e2022-08-31 10:57:37 +0000331 int res;
Alex Marginean7a910c12019-07-03 12:11:40 +0300332
Simon Glass2e4938b2022-09-06 20:27:17 -0600333 if (ofnode_valid(dev_ofnode(dev)) && !ofnode_is_enabled(dev_ofnode(dev))) {
Alex Marginean7a910c12019-07-03 12:11:40 +0300334 enetc_dbg(dev, "interface disabled\n");
335 return -ENODEV;
336 }
337
338 priv->enetc_txbd = memalign(ENETC_BD_ALIGN,
339 sizeof(struct enetc_tx_bd) * ENETC_BD_CNT);
340 priv->enetc_rxbd = memalign(ENETC_BD_ALIGN,
341 sizeof(union enetc_rx_bd) * ENETC_BD_CNT);
342
343 if (!priv->enetc_txbd || !priv->enetc_rxbd) {
344 /* free should be able to handle NULL, just free all pointers */
345 free(priv->enetc_txbd);
346 free(priv->enetc_rxbd);
347
348 return -ENOMEM;
349 }
350
351 /* initialize register */
Andrew Scull6520c822022-04-21 16:11:13 +0000352 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 +0300353 if (!priv->regs_base) {
354 enetc_dbg(dev, "failed to map BAR0\n");
355 return -EINVAL;
356 }
357 priv->port_regs = priv->regs_base + ENETC_PORT_REGS_OFF;
358
359 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
360
Alex Margineanc905c212019-11-14 18:58:46 +0200361 enetc_start_pcs(dev);
Alex Margineanc905c212019-11-14 18:58:46 +0200362
Siarhei Yasinski25b798e2022-08-31 10:57:37 +0000363 res = enetc_config_phy(dev);
364 if(res)
365 enetc_remove(dev);
366 return res;
Alex Marginean7a910c12019-07-03 12:11:40 +0300367}
368
369/*
370 * Remove the driver from an interface:
371 * - free up allocated memory
372 */
373static int enetc_remove(struct udevice *dev)
374{
375 struct enetc_priv *priv = dev_get_priv(dev);
376
Michael Walle3f66e8e2022-05-31 18:36:16 +0200377 if (miiphy_get_dev_by_name(priv->imdio.name))
378 mdio_unregister(&priv->imdio);
379
Alex Marginean7a910c12019-07-03 12:11:40 +0300380 free(priv->enetc_txbd);
381 free(priv->enetc_rxbd);
382
383 return 0;
384}
385
Michael Walle1d3e24f2019-12-20 14:16:48 +0100386/*
387 * LS1028A is the only part with IERB at this time and there are plans to
388 * change its structure, keep this LS1028A specific for now.
389 */
390#define LS1028A_IERB_BASE 0x1f0800000ULL
391#define LS1028A_IERB_PSIPMAR0(pf, vf) (LS1028A_IERB_BASE + 0x8000 \
392 + (pf) * 0x100 + (vf) * 8)
393#define LS1028A_IERB_PSIPMAR1(pf, vf) (LS1028A_IERB_PSIPMAR0(pf, vf) + 4)
394
395static int enetc_ls1028a_write_hwaddr(struct udevice *dev)
396{
Simon Glassb75b15b2020-12-03 16:55:23 -0700397 struct pci_child_plat *ppdata = dev_get_parent_plat(dev);
Michael Walle1d3e24f2019-12-20 14:16:48 +0100398 const int devfn_to_pf[] = {0, 1, 2, -1, -1, -1, 3};
Simon Glassfa20e932020-12-03 16:55:20 -0700399 struct eth_pdata *plat = dev_get_plat(dev);
Michael Walle1d3e24f2019-12-20 14:16:48 +0100400 int devfn = PCI_FUNC(ppdata->devfn);
401 u8 *addr = plat->enetaddr;
402 u32 lower, upper;
403 int pf;
404
405 if (devfn >= ARRAY_SIZE(devfn_to_pf))
406 return 0;
407
408 pf = devfn_to_pf[devfn];
409 if (pf < 0)
410 return 0;
411
412 lower = *(const u16 *)(addr + 4);
413 upper = *(const u32 *)addr;
414
415 out_le32(LS1028A_IERB_PSIPMAR0(pf, 0), upper);
416 out_le32(LS1028A_IERB_PSIPMAR1(pf, 0), lower);
417
418 return 0;
419}
420
Michael Walle8c7188e2019-12-20 14:16:47 +0100421static int enetc_write_hwaddr(struct udevice *dev)
Alex Marginean7a910c12019-07-03 12:11:40 +0300422{
Simon Glassfa20e932020-12-03 16:55:20 -0700423 struct eth_pdata *plat = dev_get_plat(dev);
Michael Walle8c7188e2019-12-20 14:16:47 +0100424 struct enetc_priv *priv = dev_get_priv(dev);
425 u8 *addr = plat->enetaddr;
426
Marek Vasutc05f8dc2025-01-16 05:03:18 +0100427 if (enetc_is_ls1028a(dev))
Michael Walle1d3e24f2019-12-20 14:16:48 +0100428 return enetc_ls1028a_write_hwaddr(dev);
429
Alex Marginean7a910c12019-07-03 12:11:40 +0300430 u16 lower = *(const u16 *)(addr + 4);
431 u32 upper = *(const u32 *)addr;
432
433 enetc_write_port(priv, ENETC_PSIPMAR0, upper);
434 enetc_write_port(priv, ENETC_PSIPMAR1, lower);
Michael Walle8c7188e2019-12-20 14:16:47 +0100435
436 return 0;
Alex Marginean7a910c12019-07-03 12:11:40 +0300437}
438
439/* Configure port parameters (# of rings, frame size, enable port) */
Marek Vasutb0dc0b72025-01-16 05:03:21 +0100440static void enetc_enable_si_port(struct udevice *dev)
Alex Marginean7a910c12019-07-03 12:11:40 +0300441{
Marek Vasutb0dc0b72025-01-16 05:03:21 +0100442 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean7a910c12019-07-03 12:11:40 +0300443 u32 val;
444
445 /* set Rx/Tx BDR count */
446 val = ENETC_PSICFGR_SET_TXBDR(ENETC_TX_BDR_CNT);
447 val |= ENETC_PSICFGR_SET_RXBDR(ENETC_RX_BDR_CNT);
448 enetc_write_port(priv, ENETC_PSICFGR(0), val);
449 /* set Rx max frame size */
450 enetc_write_port(priv, ENETC_PM_MAXFRM, ENETC_RX_MAXFRM_SIZE);
451 /* enable MAC port */
452 enetc_write_port(priv, ENETC_PM_CC, ENETC_PM_CC_RX_TX_EN);
453 /* enable port */
454 enetc_write_port(priv, ENETC_PMR, ENETC_PMR_SI0_EN);
455 /* set SI cache policy */
456 enetc_write(priv, ENETC_SICAR0,
457 ENETC_SICAR_RD_CFG | ENETC_SICAR_WR_CFG);
458 /* enable SI */
459 enetc_write(priv, ENETC_SIMR, ENETC_SIMR_EN);
460}
461
462/* returns DMA address for a given buffer index */
463static inline u64 enetc_rxb_address(struct udevice *dev, int i)
464{
465 return cpu_to_le64(dm_pci_virt_to_mem(dev, net_rx_packets[i]));
466}
467
468/*
469 * Setup a single Tx BD Ring (ID = 0):
470 * - set Tx buffer descriptor address
471 * - set the BD count
472 * - initialize the producer and consumer index
473 */
474static void enetc_setup_tx_bdr(struct udevice *dev)
475{
476 struct enetc_priv *priv = dev_get_priv(dev);
477 struct bd_ring *tx_bdr = &priv->tx_bdr;
478 u64 tx_bd_add = (u64)priv->enetc_txbd;
479
480 /* used later to advance to the next Tx BD */
481 tx_bdr->bd_count = ENETC_BD_CNT;
482 tx_bdr->next_prod_idx = 0;
483 tx_bdr->next_cons_idx = 0;
484 tx_bdr->cons_idx = priv->regs_base +
485 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBCIR);
486 tx_bdr->prod_idx = priv->regs_base +
487 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBPIR);
488
489 /* set Tx BD address */
490 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR0,
491 lower_32_bits(tx_bd_add));
492 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR1,
493 upper_32_bits(tx_bd_add));
494 /* set Tx 8 BD count */
495 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBLENR,
496 tx_bdr->bd_count);
497
498 /* reset both producer/consumer indexes */
499 enetc_write_reg(tx_bdr->cons_idx, tx_bdr->next_cons_idx);
500 enetc_write_reg(tx_bdr->prod_idx, tx_bdr->next_prod_idx);
501
502 /* enable TX ring */
503 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBMR, ENETC_TBMR_EN);
504}
505
506/*
507 * Setup a single Rx BD Ring (ID = 0):
508 * - set Rx buffer descriptors address (one descriptor per buffer)
509 * - set buffer size as max frame size
510 * - enable Rx ring
511 * - reset consumer and producer indexes
512 * - set buffer for each descriptor
513 */
514static void enetc_setup_rx_bdr(struct udevice *dev)
515{
516 struct enetc_priv *priv = dev_get_priv(dev);
517 struct bd_ring *rx_bdr = &priv->rx_bdr;
518 u64 rx_bd_add = (u64)priv->enetc_rxbd;
519 int i;
520
521 /* used later to advance to the next BD produced by ENETC HW */
522 rx_bdr->bd_count = ENETC_BD_CNT;
523 rx_bdr->next_prod_idx = 0;
524 rx_bdr->next_cons_idx = 0;
525 rx_bdr->cons_idx = priv->regs_base +
526 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBCIR);
527 rx_bdr->prod_idx = priv->regs_base +
528 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBPIR);
529
530 /* set Rx BD address */
531 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR0,
532 lower_32_bits(rx_bd_add));
533 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR1,
534 upper_32_bits(rx_bd_add));
535 /* set Rx BD count (multiple of 8) */
536 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBLENR,
537 rx_bdr->bd_count);
538 /* set Rx buffer size */
539 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBSR, PKTSIZE_ALIGN);
540
541 /* fill Rx BD */
542 memset(priv->enetc_rxbd, 0,
543 rx_bdr->bd_count * sizeof(union enetc_rx_bd));
544 for (i = 0; i < rx_bdr->bd_count; i++) {
545 priv->enetc_rxbd[i].w.addr = enetc_rxb_address(dev, i);
546 /* each RX buffer must be aligned to 64B */
547 WARN_ON(priv->enetc_rxbd[i].w.addr & (ARCH_DMA_MINALIGN - 1));
548 }
549
550 /* reset producer (ENETC owned) and consumer (SW owned) index */
551 enetc_write_reg(rx_bdr->cons_idx, rx_bdr->next_cons_idx);
552 enetc_write_reg(rx_bdr->prod_idx, rx_bdr->next_prod_idx);
553
554 /* enable Rx ring */
555 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBMR, ENETC_RBMR_EN);
556}
557
558/*
559 * Start ENETC interface:
560 * - perform FLR
561 * - enable access to port and SI registers
562 * - set mac address
563 * - setup TX/RX buffer descriptors
564 * - enable Tx/Rx rings
565 */
566static int enetc_start(struct udevice *dev)
567{
Alex Marginean7a910c12019-07-03 12:11:40 +0300568 struct enetc_priv *priv = dev_get_priv(dev);
569
570 /* reset and enable the PCI device */
571 dm_pci_flr(dev);
572 dm_pci_clrset_config16(dev, PCI_COMMAND, 0,
573 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
574
Marek Vasutb0dc0b72025-01-16 05:03:21 +0100575 enetc_enable_si_port(dev);
Alex Marginean7a910c12019-07-03 12:11:40 +0300576
577 /* setup Tx/Rx buffer descriptors */
578 enetc_setup_tx_bdr(dev);
579 enetc_setup_rx_bdr(dev);
580
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300581 enetc_setup_mac_iface(dev, priv->phy);
582
Vladimir Oltean19363082021-06-29 20:53:17 +0300583 return phy_startup(priv->phy);
Alex Marginean7a910c12019-07-03 12:11:40 +0300584}
585
586/*
587 * Stop the network interface:
588 * - just quiesce it, we can wipe all configuration as _start starts from
589 * scratch each time
590 */
591static void enetc_stop(struct udevice *dev)
592{
593 /* FLR is sufficient to quiesce the device */
594 dm_pci_flr(dev);
Alex Margineand4be7682019-11-25 17:57:27 +0200595 /* leave the BARs accessible after we stop, this is needed to use
596 * internal MDIO in command line.
597 */
598 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
Alex Marginean7a910c12019-07-03 12:11:40 +0300599}
600
601/*
602 * ENETC transmit packet:
603 * - check if Tx BD ring is full
604 * - set buffer/packet address (dma address)
605 * - set final fragment flag
606 * - try while producer index equals consumer index or timeout
607 */
608static int enetc_send(struct udevice *dev, void *packet, int length)
609{
610 struct enetc_priv *priv = dev_get_priv(dev);
611 struct bd_ring *txr = &priv->tx_bdr;
612 void *nv_packet = (void *)packet;
613 int tries = ENETC_POLL_TRIES;
614 u32 pi, ci;
615
616 pi = txr->next_prod_idx;
617 ci = enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK;
618 /* Tx ring is full when */
619 if (((pi + 1) % txr->bd_count) == ci) {
620 enetc_dbg(dev, "Tx BDR full\n");
621 return -ETIMEDOUT;
622 }
623 enetc_dbg(dev, "TxBD[%d]send: pkt_len=%d, buff @0x%x%08x\n", pi, length,
624 upper_32_bits((u64)nv_packet), lower_32_bits((u64)nv_packet));
625
626 /* prepare Tx BD */
627 memset(&priv->enetc_txbd[pi], 0x0, sizeof(struct enetc_tx_bd));
628 priv->enetc_txbd[pi].addr =
629 cpu_to_le64(dm_pci_virt_to_mem(dev, nv_packet));
630 priv->enetc_txbd[pi].buf_len = cpu_to_le16(length);
631 priv->enetc_txbd[pi].frm_len = cpu_to_le16(length);
632 priv->enetc_txbd[pi].flags = cpu_to_le16(ENETC_TXBD_FLAGS_F);
633 dmb();
634 /* send frame: increment producer index */
635 pi = (pi + 1) % txr->bd_count;
636 txr->next_prod_idx = pi;
637 enetc_write_reg(txr->prod_idx, pi);
638 while ((--tries >= 0) &&
639 (pi != (enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK)))
640 udelay(10);
641
642 return tries > 0 ? 0 : -ETIMEDOUT;
643}
644
645/*
646 * Receive frame:
647 * - wait for the next BD to get ready bit set
648 * - clean up the descriptor
649 * - move on and indicate to HW that the cleaned BD is available for Rx
650 */
651static int enetc_recv(struct udevice *dev, int flags, uchar **packetp)
652{
653 struct enetc_priv *priv = dev_get_priv(dev);
654 struct bd_ring *rxr = &priv->rx_bdr;
655 int tries = ENETC_POLL_TRIES;
656 int pi = rxr->next_prod_idx;
657 int ci = rxr->next_cons_idx;
658 u32 status;
659 int len;
660 u8 rdy;
661
662 do {
663 dmb();
664 status = le32_to_cpu(priv->enetc_rxbd[pi].r.lstatus);
665 /* check if current BD is ready to be consumed */
666 rdy = ENETC_RXBD_STATUS_R(status);
667 } while (--tries >= 0 && !rdy);
668
669 if (!rdy)
670 return -EAGAIN;
671
672 dmb();
673 len = le16_to_cpu(priv->enetc_rxbd[pi].r.buf_len);
674 *packetp = (uchar *)enetc_rxb_address(dev, pi);
675 enetc_dbg(dev, "RxBD[%d]: len=%d err=%d pkt=0x%x%08x\n", pi, len,
676 ENETC_RXBD_STATUS_ERRORS(status),
677 upper_32_bits((u64)*packetp), lower_32_bits((u64)*packetp));
678
679 /* BD clean up and advance to next in ring */
680 memset(&priv->enetc_rxbd[pi], 0, sizeof(union enetc_rx_bd));
681 priv->enetc_rxbd[pi].w.addr = enetc_rxb_address(dev, pi);
682 rxr->next_prod_idx = (pi + 1) % rxr->bd_count;
683 ci = (ci + 1) % rxr->bd_count;
684 rxr->next_cons_idx = ci;
685 dmb();
686 /* free up the slot in the ring for HW */
687 enetc_write_reg(rxr->cons_idx, ci);
688
689 return len;
690}
691
692static const struct eth_ops enetc_ops = {
693 .start = enetc_start,
694 .send = enetc_send,
695 .recv = enetc_recv,
696 .stop = enetc_stop,
Michael Walle8c7188e2019-12-20 14:16:47 +0100697 .write_hwaddr = enetc_write_hwaddr,
Alex Marginean7a910c12019-07-03 12:11:40 +0300698};
699
700U_BOOT_DRIVER(eth_enetc) = {
Alex Marginean805b8592019-12-10 16:55:39 +0200701 .name = ENETC_DRIVER_NAME,
Alex Marginean7a910c12019-07-03 12:11:40 +0300702 .id = UCLASS_ETH,
703 .bind = enetc_bind,
704 .probe = enetc_probe,
705 .remove = enetc_remove,
706 .ops = &enetc_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700707 .priv_auto = sizeof(struct enetc_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700708 .plat_auto = sizeof(struct eth_pdata),
Alex Marginean7a910c12019-07-03 12:11:40 +0300709};
710
711static struct pci_device_id enetc_ids[] = {
712 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, PCI_DEVICE_ID_ENETC_ETH) },
713 {}
714};
715
716U_BOOT_PCI_DEVICE(eth_enetc, enetc_ids);