blob: 826b1971bb0aac0f260adffd93606e8e5479aee7 [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) */
440static void enetc_enable_si_port(struct enetc_priv *priv)
441{
442 u32 val;
443
444 /* set Rx/Tx BDR count */
445 val = ENETC_PSICFGR_SET_TXBDR(ENETC_TX_BDR_CNT);
446 val |= ENETC_PSICFGR_SET_RXBDR(ENETC_RX_BDR_CNT);
447 enetc_write_port(priv, ENETC_PSICFGR(0), val);
448 /* set Rx max frame size */
449 enetc_write_port(priv, ENETC_PM_MAXFRM, ENETC_RX_MAXFRM_SIZE);
450 /* enable MAC port */
451 enetc_write_port(priv, ENETC_PM_CC, ENETC_PM_CC_RX_TX_EN);
452 /* enable port */
453 enetc_write_port(priv, ENETC_PMR, ENETC_PMR_SI0_EN);
454 /* set SI cache policy */
455 enetc_write(priv, ENETC_SICAR0,
456 ENETC_SICAR_RD_CFG | ENETC_SICAR_WR_CFG);
457 /* enable SI */
458 enetc_write(priv, ENETC_SIMR, ENETC_SIMR_EN);
459}
460
461/* returns DMA address for a given buffer index */
462static inline u64 enetc_rxb_address(struct udevice *dev, int i)
463{
464 return cpu_to_le64(dm_pci_virt_to_mem(dev, net_rx_packets[i]));
465}
466
467/*
468 * Setup a single Tx BD Ring (ID = 0):
469 * - set Tx buffer descriptor address
470 * - set the BD count
471 * - initialize the producer and consumer index
472 */
473static void enetc_setup_tx_bdr(struct udevice *dev)
474{
475 struct enetc_priv *priv = dev_get_priv(dev);
476 struct bd_ring *tx_bdr = &priv->tx_bdr;
477 u64 tx_bd_add = (u64)priv->enetc_txbd;
478
479 /* used later to advance to the next Tx BD */
480 tx_bdr->bd_count = ENETC_BD_CNT;
481 tx_bdr->next_prod_idx = 0;
482 tx_bdr->next_cons_idx = 0;
483 tx_bdr->cons_idx = priv->regs_base +
484 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBCIR);
485 tx_bdr->prod_idx = priv->regs_base +
486 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBPIR);
487
488 /* set Tx BD address */
489 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR0,
490 lower_32_bits(tx_bd_add));
491 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR1,
492 upper_32_bits(tx_bd_add));
493 /* set Tx 8 BD count */
494 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBLENR,
495 tx_bdr->bd_count);
496
497 /* reset both producer/consumer indexes */
498 enetc_write_reg(tx_bdr->cons_idx, tx_bdr->next_cons_idx);
499 enetc_write_reg(tx_bdr->prod_idx, tx_bdr->next_prod_idx);
500
501 /* enable TX ring */
502 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBMR, ENETC_TBMR_EN);
503}
504
505/*
506 * Setup a single Rx BD Ring (ID = 0):
507 * - set Rx buffer descriptors address (one descriptor per buffer)
508 * - set buffer size as max frame size
509 * - enable Rx ring
510 * - reset consumer and producer indexes
511 * - set buffer for each descriptor
512 */
513static void enetc_setup_rx_bdr(struct udevice *dev)
514{
515 struct enetc_priv *priv = dev_get_priv(dev);
516 struct bd_ring *rx_bdr = &priv->rx_bdr;
517 u64 rx_bd_add = (u64)priv->enetc_rxbd;
518 int i;
519
520 /* used later to advance to the next BD produced by ENETC HW */
521 rx_bdr->bd_count = ENETC_BD_CNT;
522 rx_bdr->next_prod_idx = 0;
523 rx_bdr->next_cons_idx = 0;
524 rx_bdr->cons_idx = priv->regs_base +
525 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBCIR);
526 rx_bdr->prod_idx = priv->regs_base +
527 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBPIR);
528
529 /* set Rx BD address */
530 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR0,
531 lower_32_bits(rx_bd_add));
532 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR1,
533 upper_32_bits(rx_bd_add));
534 /* set Rx BD count (multiple of 8) */
535 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBLENR,
536 rx_bdr->bd_count);
537 /* set Rx buffer size */
538 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBSR, PKTSIZE_ALIGN);
539
540 /* fill Rx BD */
541 memset(priv->enetc_rxbd, 0,
542 rx_bdr->bd_count * sizeof(union enetc_rx_bd));
543 for (i = 0; i < rx_bdr->bd_count; i++) {
544 priv->enetc_rxbd[i].w.addr = enetc_rxb_address(dev, i);
545 /* each RX buffer must be aligned to 64B */
546 WARN_ON(priv->enetc_rxbd[i].w.addr & (ARCH_DMA_MINALIGN - 1));
547 }
548
549 /* reset producer (ENETC owned) and consumer (SW owned) index */
550 enetc_write_reg(rx_bdr->cons_idx, rx_bdr->next_cons_idx);
551 enetc_write_reg(rx_bdr->prod_idx, rx_bdr->next_prod_idx);
552
553 /* enable Rx ring */
554 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBMR, ENETC_RBMR_EN);
555}
556
557/*
558 * Start ENETC interface:
559 * - perform FLR
560 * - enable access to port and SI registers
561 * - set mac address
562 * - setup TX/RX buffer descriptors
563 * - enable Tx/Rx rings
564 */
565static int enetc_start(struct udevice *dev)
566{
Alex Marginean7a910c12019-07-03 12:11:40 +0300567 struct enetc_priv *priv = dev_get_priv(dev);
568
569 /* reset and enable the PCI device */
570 dm_pci_flr(dev);
571 dm_pci_clrset_config16(dev, PCI_COMMAND, 0,
572 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
573
Alex Marginean7a910c12019-07-03 12:11:40 +0300574 enetc_enable_si_port(priv);
575
576 /* setup Tx/Rx buffer descriptors */
577 enetc_setup_tx_bdr(dev);
578 enetc_setup_rx_bdr(dev);
579
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300580 enetc_setup_mac_iface(dev, priv->phy);
581
Vladimir Oltean19363082021-06-29 20:53:17 +0300582 return phy_startup(priv->phy);
Alex Marginean7a910c12019-07-03 12:11:40 +0300583}
584
585/*
586 * Stop the network interface:
587 * - just quiesce it, we can wipe all configuration as _start starts from
588 * scratch each time
589 */
590static void enetc_stop(struct udevice *dev)
591{
592 /* FLR is sufficient to quiesce the device */
593 dm_pci_flr(dev);
Alex Margineand4be7682019-11-25 17:57:27 +0200594 /* leave the BARs accessible after we stop, this is needed to use
595 * internal MDIO in command line.
596 */
597 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
Alex Marginean7a910c12019-07-03 12:11:40 +0300598}
599
600/*
601 * ENETC transmit packet:
602 * - check if Tx BD ring is full
603 * - set buffer/packet address (dma address)
604 * - set final fragment flag
605 * - try while producer index equals consumer index or timeout
606 */
607static int enetc_send(struct udevice *dev, void *packet, int length)
608{
609 struct enetc_priv *priv = dev_get_priv(dev);
610 struct bd_ring *txr = &priv->tx_bdr;
611 void *nv_packet = (void *)packet;
612 int tries = ENETC_POLL_TRIES;
613 u32 pi, ci;
614
615 pi = txr->next_prod_idx;
616 ci = enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK;
617 /* Tx ring is full when */
618 if (((pi + 1) % txr->bd_count) == ci) {
619 enetc_dbg(dev, "Tx BDR full\n");
620 return -ETIMEDOUT;
621 }
622 enetc_dbg(dev, "TxBD[%d]send: pkt_len=%d, buff @0x%x%08x\n", pi, length,
623 upper_32_bits((u64)nv_packet), lower_32_bits((u64)nv_packet));
624
625 /* prepare Tx BD */
626 memset(&priv->enetc_txbd[pi], 0x0, sizeof(struct enetc_tx_bd));
627 priv->enetc_txbd[pi].addr =
628 cpu_to_le64(dm_pci_virt_to_mem(dev, nv_packet));
629 priv->enetc_txbd[pi].buf_len = cpu_to_le16(length);
630 priv->enetc_txbd[pi].frm_len = cpu_to_le16(length);
631 priv->enetc_txbd[pi].flags = cpu_to_le16(ENETC_TXBD_FLAGS_F);
632 dmb();
633 /* send frame: increment producer index */
634 pi = (pi + 1) % txr->bd_count;
635 txr->next_prod_idx = pi;
636 enetc_write_reg(txr->prod_idx, pi);
637 while ((--tries >= 0) &&
638 (pi != (enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK)))
639 udelay(10);
640
641 return tries > 0 ? 0 : -ETIMEDOUT;
642}
643
644/*
645 * Receive frame:
646 * - wait for the next BD to get ready bit set
647 * - clean up the descriptor
648 * - move on and indicate to HW that the cleaned BD is available for Rx
649 */
650static int enetc_recv(struct udevice *dev, int flags, uchar **packetp)
651{
652 struct enetc_priv *priv = dev_get_priv(dev);
653 struct bd_ring *rxr = &priv->rx_bdr;
654 int tries = ENETC_POLL_TRIES;
655 int pi = rxr->next_prod_idx;
656 int ci = rxr->next_cons_idx;
657 u32 status;
658 int len;
659 u8 rdy;
660
661 do {
662 dmb();
663 status = le32_to_cpu(priv->enetc_rxbd[pi].r.lstatus);
664 /* check if current BD is ready to be consumed */
665 rdy = ENETC_RXBD_STATUS_R(status);
666 } while (--tries >= 0 && !rdy);
667
668 if (!rdy)
669 return -EAGAIN;
670
671 dmb();
672 len = le16_to_cpu(priv->enetc_rxbd[pi].r.buf_len);
673 *packetp = (uchar *)enetc_rxb_address(dev, pi);
674 enetc_dbg(dev, "RxBD[%d]: len=%d err=%d pkt=0x%x%08x\n", pi, len,
675 ENETC_RXBD_STATUS_ERRORS(status),
676 upper_32_bits((u64)*packetp), lower_32_bits((u64)*packetp));
677
678 /* BD clean up and advance to next in ring */
679 memset(&priv->enetc_rxbd[pi], 0, sizeof(union enetc_rx_bd));
680 priv->enetc_rxbd[pi].w.addr = enetc_rxb_address(dev, pi);
681 rxr->next_prod_idx = (pi + 1) % rxr->bd_count;
682 ci = (ci + 1) % rxr->bd_count;
683 rxr->next_cons_idx = ci;
684 dmb();
685 /* free up the slot in the ring for HW */
686 enetc_write_reg(rxr->cons_idx, ci);
687
688 return len;
689}
690
691static const struct eth_ops enetc_ops = {
692 .start = enetc_start,
693 .send = enetc_send,
694 .recv = enetc_recv,
695 .stop = enetc_stop,
Michael Walle8c7188e2019-12-20 14:16:47 +0100696 .write_hwaddr = enetc_write_hwaddr,
Alex Marginean7a910c12019-07-03 12:11:40 +0300697};
698
699U_BOOT_DRIVER(eth_enetc) = {
Alex Marginean805b8592019-12-10 16:55:39 +0200700 .name = ENETC_DRIVER_NAME,
Alex Marginean7a910c12019-07-03 12:11:40 +0300701 .id = UCLASS_ETH,
702 .bind = enetc_bind,
703 .probe = enetc_probe,
704 .remove = enetc_remove,
705 .ops = &enetc_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700706 .priv_auto = sizeof(struct enetc_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700707 .plat_auto = sizeof(struct eth_pdata),
Alex Marginean7a910c12019-07-03 12:11:40 +0300708};
709
710static struct pci_device_id enetc_ids[] = {
711 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, PCI_DEVICE_ID_ENETC_ETH) },
712 {}
713};
714
715U_BOOT_PCI_DEVICE(eth_enetc, enetc_ids);