blob: 1a348f41ea047505d43b14151ce38e0aeb11eab7 [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 */
Marek Vasut278c8442025-01-16 05:03:27 +010060static void enetc_write_pmr(struct udevice *dev, u32 val)
Marek Vasuta1fa5cb2025-01-16 05:03:25 +010061{
Marek Vasut278c8442025-01-16 05:03:27 +010062 struct enetc_data *data = (struct enetc_data *)dev_get_driver_data(dev);
63 struct enetc_priv *priv = dev_get_priv(dev);
64 const u32 off = ENETC_PMR + data->reg_offset_pmr;
Marek Vasuta1fa5cb2025-01-16 05:03:25 +010065
66 enetc_write_reg(priv->port_regs + off, val);
67}
68
Marek Vasut278c8442025-01-16 05:03:27 +010069static void enetc_write_psipmar(struct udevice *dev, int n, u32 val)
Marek Vasuta1fa5cb2025-01-16 05:03:25 +010070{
Marek Vasut278c8442025-01-16 05:03:27 +010071 struct enetc_data *data = (struct enetc_data *)dev_get_driver_data(dev);
72 struct enetc_priv *priv = dev_get_priv(dev);
Marek Vasuta1fa5cb2025-01-16 05:03:25 +010073 const u32 off = (n ? ENETC_PSIPMAR1 : ENETC_PSIPMAR0) +
Marek Vasut278c8442025-01-16 05:03:27 +010074 data->reg_offset_psipmar;
Marek Vasuta1fa5cb2025-01-16 05:03:25 +010075
76 enetc_write_reg(priv->port_regs + off, val);
77}
78
79/* port station register accessors */
Marek Vasut278c8442025-01-16 05:03:27 +010080static void enetc_write_psicfgr(struct udevice *dev, int port, u32 val)
Marek Vasuta1fa5cb2025-01-16 05:03:25 +010081{
Marek Vasut278c8442025-01-16 05:03:27 +010082 struct enetc_data *data = (struct enetc_data *)dev_get_driver_data(dev);
83 struct enetc_priv *priv = dev_get_priv(dev);
Marek Vasuta1fa5cb2025-01-16 05:03:25 +010084 const u32 off = ENETC_PSICFGR(port, ENETC_PSICFGR_SHIFT_LS) +
Marek Vasut278c8442025-01-16 05:03:27 +010085 data->reg_offset_psicfgr;
Marek Vasuta1fa5cb2025-01-16 05:03:25 +010086
87 enetc_write_reg(priv->port_regs + off, val);
88}
89
Marek Vasutcd684142025-01-16 05:03:24 +010090/* port register accessors */
Marek Vasut278c8442025-01-16 05:03:27 +010091static u32 enetc_read_pcapr_mdio(struct udevice *dev)
Marek Vasuta1fa5cb2025-01-16 05:03:25 +010092{
Marek Vasut278c8442025-01-16 05:03:27 +010093 struct enetc_data *data = (struct enetc_data *)dev_get_driver_data(dev);
94 struct enetc_priv *priv = dev_get_priv(dev);
95 const u32 off = ENETC_PCAPR0 + data->reg_offset_pcapr;
Marek Vasuta1fa5cb2025-01-16 05:03:25 +010096 u32 reg = enetc_read_reg(priv->port_regs + off);
97
98 return reg & ENETC_PCAPRO_MDIO;
99}
100
101/* MAC port register accessors */
Marek Vasut278c8442025-01-16 05:03:27 +0100102static u32 enetc_read_mac_port(struct udevice *dev, u32 off)
Marek Vasutcd684142025-01-16 05:03:24 +0100103{
Marek Vasut278c8442025-01-16 05:03:27 +0100104 struct enetc_data *data = (struct enetc_data *)dev_get_driver_data(dev);
105 struct enetc_priv *priv = dev_get_priv(dev);
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100106
Marek Vasut278c8442025-01-16 05:03:27 +0100107 return enetc_read_reg(priv->port_regs + data->reg_offset_mac + off);
Marek Vasutcd684142025-01-16 05:03:24 +0100108}
109
Marek Vasut278c8442025-01-16 05:03:27 +0100110static void enetc_write_mac_port(struct udevice *dev, u32 off, u32 val)
Marek Vasutcd684142025-01-16 05:03:24 +0100111{
Marek Vasut278c8442025-01-16 05:03:27 +0100112 struct enetc_data *data = (struct enetc_data *)dev_get_driver_data(dev);
113 struct enetc_priv *priv = dev_get_priv(dev);
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100114
Marek Vasut278c8442025-01-16 05:03:27 +0100115 enetc_write_reg(priv->port_regs + data->reg_offset_mac + off, val);
Marek Vasutcd684142025-01-16 05:03:24 +0100116}
117
118/* BDR register accessor, see also ENETC_BDR() */
119static void enetc_bdr_write(struct enetc_priv *priv, int type, int n,
120 u32 off, u32 val)
121{
122 enetc_write(priv, ENETC_BDR(type, n, off), val);
123}
124
Alex Marginean805b8592019-12-10 16:55:39 +0200125/*
126 * sets the MAC address in IERB registers, this setting is persistent and
127 * carried over to Linux.
128 */
Alex Marginean805b8592019-12-10 16:55:39 +0200129#define IERB_BASE 0x1f0800000ULL
130#define IERB_PFMAC(pf, vf, n) (IERB_BASE + 0x8000 + (pf) * 0x100 + (vf) * 8 \
131 + (n) * 4)
132
Marek Vasutd9b36f62025-01-16 05:03:20 +0100133static void enetc_set_ierb_primary_mac(struct udevice *dev, void *blob)
Marek Vasutc9997c72025-01-16 05:03:19 +0100134{
Marek Vasutd9b36f62025-01-16 05:03:20 +0100135 static int ierb_fn_to_pf[] = { 0, 1, 2, -1, -1, -1, 3 };
136 struct pci_child_plat *ppdata = dev_get_parent_plat(dev);
137 struct eth_pdata *pdata = dev_get_plat(dev);
138 const u8 *enetaddr = pdata->enetaddr;
Alex Marginean805b8592019-12-10 16:55:39 +0200139 u16 lower = *(const u16 *)(enetaddr + 4);
140 u32 upper = *(const u32 *)enetaddr;
Marek Vasutd9b36f62025-01-16 05:03:20 +0100141 int devfn, offset;
142 char path[256];
Alex Marginean805b8592019-12-10 16:55:39 +0200143
Marek Vasutc9997c72025-01-16 05:03:19 +0100144 if (enetc_is_ls1028a(dev)) {
145 /*
146 * LS1028A is the only part with IERB at this time and
147 * there are plans to change its structure, keep this
148 * LS1028A specific for now.
149 */
Marek Vasutd9b36f62025-01-16 05:03:20 +0100150 devfn = PCI_FUNC(ppdata->devfn);
151
Marek Vasutc9997c72025-01-16 05:03:19 +0100152 if (ierb_fn_to_pf[devfn] < 0)
153 return;
Alex Marginean805b8592019-12-10 16:55:39 +0200154
Marek Vasutc9997c72025-01-16 05:03:19 +0100155 out_le32(IERB_PFMAC(ierb_fn_to_pf[devfn], 0, 0), upper);
156 out_le32(IERB_PFMAC(ierb_fn_to_pf[devfn], 0, 1), (u32)lower);
Marek Vasutd9b36f62025-01-16 05:03:20 +0100157
158 snprintf(path, 256, "/soc/pcie@1f0000000/ethernet@%x,%x",
159 PCI_DEV(ppdata->devfn), PCI_FUNC(ppdata->devfn));
160 } else {
161 return;
Marek Vasutc9997c72025-01-16 05:03:19 +0100162 }
Marek Vasutd9b36f62025-01-16 05:03:20 +0100163
164 offset = fdt_path_offset(blob, path);
165 if (offset >= 0)
166 fdt_setprop(blob, offset, "mac-address", pdata->enetaddr, 6);
Alex Marginean805b8592019-12-10 16:55:39 +0200167}
168
169/* sets up primary MAC addresses in DT/IERB */
170void fdt_fixup_enetc_mac(void *blob)
171{
Alex Marginean805b8592019-12-10 16:55:39 +0200172 struct udevice *dev;
173 struct uclass *uc;
Alex Marginean805b8592019-12-10 16:55:39 +0200174
175 uclass_get(UCLASS_ETH, &uc);
176 uclass_foreach_dev(dev, uc) {
177 if (!dev->driver || !dev->driver->name ||
178 strcmp(dev->driver->name, ENETC_DRIVER_NAME))
179 continue;
180
Marek Vasutd9b36f62025-01-16 05:03:20 +0100181 enetc_set_ierb_primary_mac(dev, blob);
Alex Marginean805b8592019-12-10 16:55:39 +0200182 }
183}
184
Alex Marginean7a910c12019-07-03 12:11:40 +0300185/*
186 * Bind the device:
187 * - set a more explicit name on the interface
188 */
189static int enetc_bind(struct udevice *dev)
190{
191 char name[16];
192 static int eth_num_devices;
193
194 /*
195 * prefer using PCI function numbers to number interfaces, but these
196 * are only available if dts nodes are present. For PCI they are
197 * optional, handle that case too. Just in case some nodes are present
198 * and some are not, use different naming scheme - enetc-N based on
199 * PCI function # and enetc#N based on interface count
200 */
Simon Glassa7ece582020-12-19 10:40:14 -0700201 if (ofnode_valid(dev_ofnode(dev)))
Marek Vasutdbfb4bc2025-01-16 05:03:23 +0100202 sprintf(name, "enetc-%u", enetc_dev_id(dev));
Alex Marginean7a910c12019-07-03 12:11:40 +0300203 else
204 sprintf(name, "enetc#%u", eth_num_devices++);
205 device_set_name(dev, name);
206
207 return 0;
208}
209
Alex Marginean38882ae2019-07-03 12:11:42 +0300210/* MDIO wrappers, we're using these to drive internal MDIO to get to serdes */
211static int enetc_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
212{
213 struct enetc_mdio_priv priv;
214
215 priv.regs_base = bus->priv;
216 return enetc_mdio_read_priv(&priv, addr, devad, reg);
217}
218
219static int enetc_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
220 u16 val)
221{
222 struct enetc_mdio_priv priv;
223
224 priv.regs_base = bus->priv;
225 return enetc_mdio_write_priv(&priv, addr, devad, reg, val);
226}
227
228/* only interfaces that can pin out through serdes have internal MDIO */
229static bool enetc_has_imdio(struct udevice *dev)
230{
231 struct enetc_priv *priv = dev_get_priv(dev);
232
233 return !!(priv->imdio.priv);
234}
235
236/* set up serdes for SGMII */
237static int enetc_init_sgmii(struct udevice *dev)
238{
239 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean41a7ac52019-07-15 11:48:47 +0300240 bool is2500 = false;
241 u16 reg;
Alex Marginean38882ae2019-07-03 12:11:42 +0300242
243 if (!enetc_has_imdio(dev))
244 return 0;
245
Simon Glassfada3f92022-09-17 09:00:09 -0600246 if (priv->uclass_id == PHY_INTERFACE_MODE_2500BASEX)
Alex Marginean41a7ac52019-07-15 11:48:47 +0300247 is2500 = true;
248
249 /*
250 * Set to SGMII mode, for 1Gbps enable AN, for 2.5Gbps set fixed speed.
251 * Although fixed speed is 1Gbps, we could be running at 2.5Gbps based
252 * on PLL configuration. Setting 1G for 2.5G here is counter intuitive
253 * but intentional.
254 */
255 reg = ENETC_PCS_IF_MODE_SGMII;
256 reg |= is2500 ? ENETC_PCS_IF_MODE_SPEED_1G : ENETC_PCS_IF_MODE_SGMII_AN;
Alex Marginean38882ae2019-07-03 12:11:42 +0300257 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300258 ENETC_PCS_IF_MODE, reg);
Alex Marginean38882ae2019-07-03 12:11:42 +0300259
260 /* Dev ability - SGMII */
261 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
262 ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SGMII);
263
264 /* Adjust link timer for SGMII */
265 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
266 ENETC_PCS_LINK_TIMER1, ENETC_PCS_LINK_TIMER1_VAL);
267 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
268 ENETC_PCS_LINK_TIMER2, ENETC_PCS_LINK_TIMER2_VAL);
269
Alex Marginean41a7ac52019-07-15 11:48:47 +0300270 reg = ENETC_PCS_CR_DEF_VAL;
271 reg |= is2500 ? ENETC_PCS_CR_RST : ENETC_PCS_CR_RESET_AN;
Alex Marginean38882ae2019-07-03 12:11:42 +0300272 /* restart PCS AN */
273 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300274 ENETC_PCS_CR, reg);
Alex Marginean38882ae2019-07-03 12:11:42 +0300275
276 return 0;
277}
278
279/* set up MAC for RGMII */
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300280static void enetc_init_rgmii(struct udevice *dev, struct phy_device *phydev)
Alex Marginean38882ae2019-07-03 12:11:42 +0300281{
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300282 u32 old_val, val;
Alex Marginean38882ae2019-07-03 12:11:42 +0300283
Marek Vasut278c8442025-01-16 05:03:27 +0100284 old_val = val = enetc_read_mac_port(dev, ENETC_PM_IF_MODE);
Alex Marginean38882ae2019-07-03 12:11:42 +0300285
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300286 /* disable unreliable RGMII in-band signaling and force the MAC into
287 * the speed negotiated by the PHY.
288 */
289 val &= ~ENETC_PM_IF_MODE_AN_ENA;
290
291 if (phydev->speed == SPEED_1000) {
292 val &= ~ENETC_PM_IFM_SSP_MASK;
293 val |= ENETC_PM_IFM_SSP_1000;
294 } else if (phydev->speed == SPEED_100) {
295 val &= ~ENETC_PM_IFM_SSP_MASK;
296 val |= ENETC_PM_IFM_SSP_100;
297 } else if (phydev->speed == SPEED_10) {
298 val &= ~ENETC_PM_IFM_SSP_MASK;
299 val |= ENETC_PM_IFM_SSP_10;
300 }
301
302 if (phydev->duplex == DUPLEX_FULL)
303 val |= ENETC_PM_IFM_FULL_DPX;
304 else
305 val &= ~ENETC_PM_IFM_FULL_DPX;
306
307 if (val == old_val)
308 return;
309
Marek Vasut278c8442025-01-16 05:03:27 +0100310 enetc_write_mac_port(dev, ENETC_PM_IF_MODE, val);
Alex Marginean38882ae2019-07-03 12:11:42 +0300311}
312
Alex Margineanafad2d02020-01-10 23:32:20 +0200313/* set up MAC configuration for the given interface type */
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300314static void enetc_setup_mac_iface(struct udevice *dev,
315 struct phy_device *phydev)
Alex Marginean38882ae2019-07-03 12:11:42 +0300316{
317 struct enetc_priv *priv = dev_get_priv(dev);
318 u32 if_mode;
319
Simon Glassfada3f92022-09-17 09:00:09 -0600320 switch (priv->uclass_id) {
Alex Margineanafad2d02020-01-10 23:32:20 +0200321 case PHY_INTERFACE_MODE_RGMII:
322 case PHY_INTERFACE_MODE_RGMII_ID:
323 case PHY_INTERFACE_MODE_RGMII_RXID:
324 case PHY_INTERFACE_MODE_RGMII_TXID:
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300325 enetc_init_rgmii(dev, phydev);
Alex Margineanafad2d02020-01-10 23:32:20 +0200326 break;
Alex Margineanafad2d02020-01-10 23:32:20 +0200327 case PHY_INTERFACE_MODE_USXGMII:
Vladimir Oltean6a6e4022021-09-18 15:32:34 +0300328 case PHY_INTERFACE_MODE_10GBASER:
Alex Margineanafad2d02020-01-10 23:32:20 +0200329 /* set ifmode to (US)XGMII */
Marek Vasut278c8442025-01-16 05:03:27 +0100330 if_mode = enetc_read_mac_port(dev, ENETC_PM_IF_MODE);
Alex Margineanafad2d02020-01-10 23:32:20 +0200331 if_mode &= ~ENETC_PM_IF_IFMODE_MASK;
Marek Vasut278c8442025-01-16 05:03:27 +0100332 enetc_write_mac_port(dev, ENETC_PM_IF_MODE, if_mode);
Alex Margineanafad2d02020-01-10 23:32:20 +0200333 break;
334 };
335}
336
337/* set up serdes for SXGMII */
338static int enetc_init_sxgmii(struct udevice *dev)
339{
340 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean38882ae2019-07-03 12:11:42 +0300341
342 if (!enetc_has_imdio(dev))
343 return 0;
344
345 /* Dev ability - SXGMII */
346 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
347 ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SXGMII);
348
349 /* Restart PCS AN */
350 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
351 ENETC_PCS_CR,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300352 ENETC_PCS_CR_RST | ENETC_PCS_CR_RESET_AN);
Alex Marginean38882ae2019-07-03 12:11:42 +0300353
354 return 0;
355}
356
357/* Apply protocol specific configuration to MAC, serdes as needed */
358static void enetc_start_pcs(struct udevice *dev)
359{
360 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean38882ae2019-07-03 12:11:42 +0300361
Alex Margineand4be7682019-11-25 17:57:27 +0200362 /* register internal MDIO for debug purposes */
Marek Vasut278c8442025-01-16 05:03:27 +0100363 if (enetc_read_pcapr_mdio(dev)) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300364 priv->imdio.read = enetc_mdio_read;
365 priv->imdio.write = enetc_mdio_write;
366 priv->imdio.priv = priv->port_regs + ENETC_PM_IMDIO_BASE;
Vladimir Olteandcd21cc2021-09-27 14:21:48 +0300367 strlcpy(priv->imdio.name, dev->name, MDIO_NAME_LEN);
Alex Margineand4be7682019-11-25 17:57:27 +0200368 if (!miiphy_get_dev_by_name(priv->imdio.name))
369 mdio_register(&priv->imdio);
Alex Marginean38882ae2019-07-03 12:11:42 +0300370 }
371
Simon Glassa7ece582020-12-19 10:40:14 -0700372 if (!ofnode_valid(dev_ofnode(dev))) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300373 enetc_dbg(dev, "no enetc ofnode found, skipping PCS set-up\n");
374 return;
375 }
376
Simon Glassfada3f92022-09-17 09:00:09 -0600377 priv->uclass_id = dev_read_phy_mode(dev);
378 if (priv->uclass_id == PHY_INTERFACE_MODE_NA) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300379 enetc_dbg(dev,
380 "phy-mode property not found, defaulting to SGMII\n");
Simon Glassfada3f92022-09-17 09:00:09 -0600381 priv->uclass_id = PHY_INTERFACE_MODE_SGMII;
Marek Behúnbc194772022-04-07 00:33:01 +0200382 }
Alex Marginean38882ae2019-07-03 12:11:42 +0300383
Simon Glassfada3f92022-09-17 09:00:09 -0600384 switch (priv->uclass_id) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300385 case PHY_INTERFACE_MODE_SGMII:
Vladimir Oltean6caef972021-09-18 15:32:35 +0300386 case PHY_INTERFACE_MODE_2500BASEX:
Alex Marginean38882ae2019-07-03 12:11:42 +0300387 enetc_init_sgmii(dev);
388 break;
Alex Margineaned0460c2019-11-14 18:28:38 +0200389 case PHY_INTERFACE_MODE_USXGMII:
Vladimir Oltean6a6e4022021-09-18 15:32:34 +0300390 case PHY_INTERFACE_MODE_10GBASER:
Alex Marginean38882ae2019-07-03 12:11:42 +0300391 enetc_init_sxgmii(dev);
392 break;
393 };
394}
395
Alex Marginean02155392019-07-03 12:11:41 +0300396/* Configure the actual/external ethernet PHY, if one is found */
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300397static int enetc_config_phy(struct udevice *dev)
Alex Marginean02155392019-07-03 12:11:41 +0300398{
399 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean02155392019-07-03 12:11:41 +0300400 int supported;
401
Alex Marginean602e00f2019-11-25 17:15:13 +0200402 priv->phy = dm_eth_phy_connect(dev);
Alex Marginean602e00f2019-11-25 17:15:13 +0200403 if (!priv->phy)
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300404 return -ENODEV;
Alex Marginean02155392019-07-03 12:11:41 +0300405
Alex Margineanb93375c2019-11-14 18:58:45 +0200406 supported = PHY_GBIT_FEATURES | SUPPORTED_2500baseX_Full;
407 priv->phy->supported &= supported;
408 priv->phy->advertising &= supported;
Alex Marginean602e00f2019-11-25 17:15:13 +0200409
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300410 return phy_config(priv->phy);
Alex Marginean02155392019-07-03 12:11:41 +0300411}
412
Alex Marginean7a910c12019-07-03 12:11:40 +0300413/*
414 * Probe ENETC driver:
415 * - initialize port and station interface BARs
416 */
417static int enetc_probe(struct udevice *dev)
418{
419 struct enetc_priv *priv = dev_get_priv(dev);
Siarhei Yasinski25b798e2022-08-31 10:57:37 +0000420 int res;
Alex Marginean7a910c12019-07-03 12:11:40 +0300421
Simon Glass2e4938b2022-09-06 20:27:17 -0600422 if (ofnode_valid(dev_ofnode(dev)) && !ofnode_is_enabled(dev_ofnode(dev))) {
Alex Marginean7a910c12019-07-03 12:11:40 +0300423 enetc_dbg(dev, "interface disabled\n");
424 return -ENODEV;
425 }
426
427 priv->enetc_txbd = memalign(ENETC_BD_ALIGN,
428 sizeof(struct enetc_tx_bd) * ENETC_BD_CNT);
429 priv->enetc_rxbd = memalign(ENETC_BD_ALIGN,
430 sizeof(union enetc_rx_bd) * ENETC_BD_CNT);
431
432 if (!priv->enetc_txbd || !priv->enetc_rxbd) {
433 /* free should be able to handle NULL, just free all pointers */
434 free(priv->enetc_txbd);
435 free(priv->enetc_rxbd);
436
437 return -ENOMEM;
438 }
439
440 /* initialize register */
Andrew Scull6520c822022-04-21 16:11:13 +0000441 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 +0300442 if (!priv->regs_base) {
443 enetc_dbg(dev, "failed to map BAR0\n");
444 return -EINVAL;
445 }
446 priv->port_regs = priv->regs_base + ENETC_PORT_REGS_OFF;
447
448 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
449
Alex Margineanc905c212019-11-14 18:58:46 +0200450 enetc_start_pcs(dev);
Alex Margineanc905c212019-11-14 18:58:46 +0200451
Siarhei Yasinski25b798e2022-08-31 10:57:37 +0000452 res = enetc_config_phy(dev);
453 if(res)
454 enetc_remove(dev);
455 return res;
Alex Marginean7a910c12019-07-03 12:11:40 +0300456}
457
458/*
459 * Remove the driver from an interface:
460 * - free up allocated memory
461 */
462static int enetc_remove(struct udevice *dev)
463{
464 struct enetc_priv *priv = dev_get_priv(dev);
465
Michael Walle3f66e8e2022-05-31 18:36:16 +0200466 if (miiphy_get_dev_by_name(priv->imdio.name))
467 mdio_unregister(&priv->imdio);
468
Alex Marginean7a910c12019-07-03 12:11:40 +0300469 free(priv->enetc_txbd);
470 free(priv->enetc_rxbd);
471
472 return 0;
473}
474
Michael Walle1d3e24f2019-12-20 14:16:48 +0100475/*
476 * LS1028A is the only part with IERB at this time and there are plans to
477 * change its structure, keep this LS1028A specific for now.
478 */
479#define LS1028A_IERB_BASE 0x1f0800000ULL
480#define LS1028A_IERB_PSIPMAR0(pf, vf) (LS1028A_IERB_BASE + 0x8000 \
481 + (pf) * 0x100 + (vf) * 8)
482#define LS1028A_IERB_PSIPMAR1(pf, vf) (LS1028A_IERB_PSIPMAR0(pf, vf) + 4)
483
484static int enetc_ls1028a_write_hwaddr(struct udevice *dev)
485{
Simon Glassb75b15b2020-12-03 16:55:23 -0700486 struct pci_child_plat *ppdata = dev_get_parent_plat(dev);
Michael Walle1d3e24f2019-12-20 14:16:48 +0100487 const int devfn_to_pf[] = {0, 1, 2, -1, -1, -1, 3};
Simon Glassfa20e932020-12-03 16:55:20 -0700488 struct eth_pdata *plat = dev_get_plat(dev);
Michael Walle1d3e24f2019-12-20 14:16:48 +0100489 int devfn = PCI_FUNC(ppdata->devfn);
490 u8 *addr = plat->enetaddr;
491 u32 lower, upper;
492 int pf;
493
494 if (devfn >= ARRAY_SIZE(devfn_to_pf))
495 return 0;
496
497 pf = devfn_to_pf[devfn];
498 if (pf < 0)
499 return 0;
500
501 lower = *(const u16 *)(addr + 4);
502 upper = *(const u32 *)addr;
503
504 out_le32(LS1028A_IERB_PSIPMAR0(pf, 0), upper);
505 out_le32(LS1028A_IERB_PSIPMAR1(pf, 0), lower);
506
507 return 0;
508}
509
Michael Walle8c7188e2019-12-20 14:16:47 +0100510static int enetc_write_hwaddr(struct udevice *dev)
Alex Marginean7a910c12019-07-03 12:11:40 +0300511{
Simon Glassfa20e932020-12-03 16:55:20 -0700512 struct eth_pdata *plat = dev_get_plat(dev);
Michael Walle8c7188e2019-12-20 14:16:47 +0100513 u8 *addr = plat->enetaddr;
514
Marek Vasutc05f8dc2025-01-16 05:03:18 +0100515 if (enetc_is_ls1028a(dev))
Michael Walle1d3e24f2019-12-20 14:16:48 +0100516 return enetc_ls1028a_write_hwaddr(dev);
517
Alex Marginean7a910c12019-07-03 12:11:40 +0300518 u16 lower = *(const u16 *)(addr + 4);
519 u32 upper = *(const u32 *)addr;
520
Marek Vasut278c8442025-01-16 05:03:27 +0100521 enetc_write_psipmar(dev, 0, upper);
522 enetc_write_psipmar(dev, 1, lower);
Michael Walle8c7188e2019-12-20 14:16:47 +0100523
524 return 0;
Alex Marginean7a910c12019-07-03 12:11:40 +0300525}
526
527/* Configure port parameters (# of rings, frame size, enable port) */
Marek Vasutb0dc0b72025-01-16 05:03:21 +0100528static void enetc_enable_si_port(struct udevice *dev)
Alex Marginean7a910c12019-07-03 12:11:40 +0300529{
Marek Vasutb0dc0b72025-01-16 05:03:21 +0100530 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean7a910c12019-07-03 12:11:40 +0300531
532 /* set Rx/Tx BDR count */
Marek Vasut278c8442025-01-16 05:03:27 +0100533 enetc_write_psicfgr(dev, 0, ENETC_PSICFGR_SET_BDR(ENETC_RX_BDR_CNT,
534 ENETC_TX_BDR_CNT));
Alex Marginean7a910c12019-07-03 12:11:40 +0300535 /* set Rx max frame size */
Marek Vasut278c8442025-01-16 05:03:27 +0100536 enetc_write_mac_port(dev, ENETC_PM_MAXFRM, ENETC_RX_MAXFRM_SIZE);
Alex Marginean7a910c12019-07-03 12:11:40 +0300537 /* enable MAC port */
Marek Vasut278c8442025-01-16 05:03:27 +0100538 enetc_write_mac_port(dev, ENETC_PM_CC, ENETC_PM_CC_RX_TX_EN);
Alex Marginean7a910c12019-07-03 12:11:40 +0300539 /* enable port */
Marek Vasut278c8442025-01-16 05:03:27 +0100540 enetc_write_pmr(dev, ENETC_PMR_SI0_EN);
Alex Marginean7a910c12019-07-03 12:11:40 +0300541 /* set SI cache policy */
542 enetc_write(priv, ENETC_SICAR0,
543 ENETC_SICAR_RD_CFG | ENETC_SICAR_WR_CFG);
544 /* enable SI */
545 enetc_write(priv, ENETC_SIMR, ENETC_SIMR_EN);
546}
547
548/* returns DMA address for a given buffer index */
549static inline u64 enetc_rxb_address(struct udevice *dev, int i)
550{
551 return cpu_to_le64(dm_pci_virt_to_mem(dev, net_rx_packets[i]));
552}
553
554/*
555 * Setup a single Tx BD Ring (ID = 0):
556 * - set Tx buffer descriptor address
557 * - set the BD count
558 * - initialize the producer and consumer index
559 */
560static void enetc_setup_tx_bdr(struct udevice *dev)
561{
562 struct enetc_priv *priv = dev_get_priv(dev);
563 struct bd_ring *tx_bdr = &priv->tx_bdr;
564 u64 tx_bd_add = (u64)priv->enetc_txbd;
565
566 /* used later to advance to the next Tx BD */
567 tx_bdr->bd_count = ENETC_BD_CNT;
568 tx_bdr->next_prod_idx = 0;
569 tx_bdr->next_cons_idx = 0;
570 tx_bdr->cons_idx = priv->regs_base +
571 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBCIR);
572 tx_bdr->prod_idx = priv->regs_base +
573 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBPIR);
574
575 /* set Tx BD address */
576 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR0,
577 lower_32_bits(tx_bd_add));
578 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR1,
579 upper_32_bits(tx_bd_add));
580 /* set Tx 8 BD count */
581 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBLENR,
582 tx_bdr->bd_count);
583
584 /* reset both producer/consumer indexes */
585 enetc_write_reg(tx_bdr->cons_idx, tx_bdr->next_cons_idx);
586 enetc_write_reg(tx_bdr->prod_idx, tx_bdr->next_prod_idx);
587
588 /* enable TX ring */
589 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBMR, ENETC_TBMR_EN);
590}
591
592/*
593 * Setup a single Rx BD Ring (ID = 0):
594 * - set Rx buffer descriptors address (one descriptor per buffer)
595 * - set buffer size as max frame size
596 * - enable Rx ring
597 * - reset consumer and producer indexes
598 * - set buffer for each descriptor
599 */
600static void enetc_setup_rx_bdr(struct udevice *dev)
601{
602 struct enetc_priv *priv = dev_get_priv(dev);
603 struct bd_ring *rx_bdr = &priv->rx_bdr;
604 u64 rx_bd_add = (u64)priv->enetc_rxbd;
605 int i;
606
607 /* used later to advance to the next BD produced by ENETC HW */
608 rx_bdr->bd_count = ENETC_BD_CNT;
609 rx_bdr->next_prod_idx = 0;
610 rx_bdr->next_cons_idx = 0;
611 rx_bdr->cons_idx = priv->regs_base +
612 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBCIR);
613 rx_bdr->prod_idx = priv->regs_base +
614 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBPIR);
615
616 /* set Rx BD address */
617 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR0,
618 lower_32_bits(rx_bd_add));
619 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR1,
620 upper_32_bits(rx_bd_add));
621 /* set Rx BD count (multiple of 8) */
622 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBLENR,
623 rx_bdr->bd_count);
624 /* set Rx buffer size */
625 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBSR, PKTSIZE_ALIGN);
626
627 /* fill Rx BD */
628 memset(priv->enetc_rxbd, 0,
629 rx_bdr->bd_count * sizeof(union enetc_rx_bd));
630 for (i = 0; i < rx_bdr->bd_count; i++) {
631 priv->enetc_rxbd[i].w.addr = enetc_rxb_address(dev, i);
632 /* each RX buffer must be aligned to 64B */
633 WARN_ON(priv->enetc_rxbd[i].w.addr & (ARCH_DMA_MINALIGN - 1));
634 }
635
636 /* reset producer (ENETC owned) and consumer (SW owned) index */
637 enetc_write_reg(rx_bdr->cons_idx, rx_bdr->next_cons_idx);
638 enetc_write_reg(rx_bdr->prod_idx, rx_bdr->next_prod_idx);
639
640 /* enable Rx ring */
641 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBMR, ENETC_RBMR_EN);
642}
643
644/*
645 * Start ENETC interface:
646 * - perform FLR
647 * - enable access to port and SI registers
648 * - set mac address
649 * - setup TX/RX buffer descriptors
650 * - enable Tx/Rx rings
651 */
652static int enetc_start(struct udevice *dev)
653{
Alex Marginean7a910c12019-07-03 12:11:40 +0300654 struct enetc_priv *priv = dev_get_priv(dev);
655
656 /* reset and enable the PCI device */
657 dm_pci_flr(dev);
658 dm_pci_clrset_config16(dev, PCI_COMMAND, 0,
659 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
660
Marek Vasutb0dc0b72025-01-16 05:03:21 +0100661 enetc_enable_si_port(dev);
Alex Marginean7a910c12019-07-03 12:11:40 +0300662
663 /* setup Tx/Rx buffer descriptors */
664 enetc_setup_tx_bdr(dev);
665 enetc_setup_rx_bdr(dev);
666
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300667 enetc_setup_mac_iface(dev, priv->phy);
668
Vladimir Oltean19363082021-06-29 20:53:17 +0300669 return phy_startup(priv->phy);
Alex Marginean7a910c12019-07-03 12:11:40 +0300670}
671
672/*
673 * Stop the network interface:
674 * - just quiesce it, we can wipe all configuration as _start starts from
675 * scratch each time
676 */
677static void enetc_stop(struct udevice *dev)
678{
679 /* FLR is sufficient to quiesce the device */
680 dm_pci_flr(dev);
Alex Margineand4be7682019-11-25 17:57:27 +0200681 /* leave the BARs accessible after we stop, this is needed to use
682 * internal MDIO in command line.
683 */
684 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
Alex Marginean7a910c12019-07-03 12:11:40 +0300685}
686
687/*
688 * ENETC transmit packet:
689 * - check if Tx BD ring is full
690 * - set buffer/packet address (dma address)
691 * - set final fragment flag
692 * - try while producer index equals consumer index or timeout
693 */
694static int enetc_send(struct udevice *dev, void *packet, int length)
695{
696 struct enetc_priv *priv = dev_get_priv(dev);
697 struct bd_ring *txr = &priv->tx_bdr;
698 void *nv_packet = (void *)packet;
699 int tries = ENETC_POLL_TRIES;
700 u32 pi, ci;
701
702 pi = txr->next_prod_idx;
703 ci = enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK;
704 /* Tx ring is full when */
705 if (((pi + 1) % txr->bd_count) == ci) {
706 enetc_dbg(dev, "Tx BDR full\n");
707 return -ETIMEDOUT;
708 }
709 enetc_dbg(dev, "TxBD[%d]send: pkt_len=%d, buff @0x%x%08x\n", pi, length,
710 upper_32_bits((u64)nv_packet), lower_32_bits((u64)nv_packet));
711
712 /* prepare Tx BD */
713 memset(&priv->enetc_txbd[pi], 0x0, sizeof(struct enetc_tx_bd));
714 priv->enetc_txbd[pi].addr =
715 cpu_to_le64(dm_pci_virt_to_mem(dev, nv_packet));
716 priv->enetc_txbd[pi].buf_len = cpu_to_le16(length);
717 priv->enetc_txbd[pi].frm_len = cpu_to_le16(length);
718 priv->enetc_txbd[pi].flags = cpu_to_le16(ENETC_TXBD_FLAGS_F);
719 dmb();
720 /* send frame: increment producer index */
721 pi = (pi + 1) % txr->bd_count;
722 txr->next_prod_idx = pi;
723 enetc_write_reg(txr->prod_idx, pi);
724 while ((--tries >= 0) &&
725 (pi != (enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK)))
726 udelay(10);
727
728 return tries > 0 ? 0 : -ETIMEDOUT;
729}
730
731/*
732 * Receive frame:
733 * - wait for the next BD to get ready bit set
734 * - clean up the descriptor
735 * - move on and indicate to HW that the cleaned BD is available for Rx
736 */
737static int enetc_recv(struct udevice *dev, int flags, uchar **packetp)
738{
739 struct enetc_priv *priv = dev_get_priv(dev);
740 struct bd_ring *rxr = &priv->rx_bdr;
741 int tries = ENETC_POLL_TRIES;
742 int pi = rxr->next_prod_idx;
743 int ci = rxr->next_cons_idx;
744 u32 status;
745 int len;
746 u8 rdy;
747
748 do {
749 dmb();
750 status = le32_to_cpu(priv->enetc_rxbd[pi].r.lstatus);
751 /* check if current BD is ready to be consumed */
752 rdy = ENETC_RXBD_STATUS_R(status);
753 } while (--tries >= 0 && !rdy);
754
755 if (!rdy)
756 return -EAGAIN;
757
758 dmb();
759 len = le16_to_cpu(priv->enetc_rxbd[pi].r.buf_len);
760 *packetp = (uchar *)enetc_rxb_address(dev, pi);
761 enetc_dbg(dev, "RxBD[%d]: len=%d err=%d pkt=0x%x%08x\n", pi, len,
762 ENETC_RXBD_STATUS_ERRORS(status),
763 upper_32_bits((u64)*packetp), lower_32_bits((u64)*packetp));
764
765 /* BD clean up and advance to next in ring */
766 memset(&priv->enetc_rxbd[pi], 0, sizeof(union enetc_rx_bd));
767 priv->enetc_rxbd[pi].w.addr = enetc_rxb_address(dev, pi);
768 rxr->next_prod_idx = (pi + 1) % rxr->bd_count;
769 ci = (ci + 1) % rxr->bd_count;
770 rxr->next_cons_idx = ci;
771 dmb();
772 /* free up the slot in the ring for HW */
773 enetc_write_reg(rxr->cons_idx, ci);
774
775 return len;
776}
777
Marek Vasut828b2362025-01-16 05:03:22 +0100778static const struct eth_ops enetc_ops_ls = {
Alex Marginean7a910c12019-07-03 12:11:40 +0300779 .start = enetc_start,
780 .send = enetc_send,
781 .recv = enetc_recv,
782 .stop = enetc_stop,
Michael Walle8c7188e2019-12-20 14:16:47 +0100783 .write_hwaddr = enetc_write_hwaddr,
Alex Marginean7a910c12019-07-03 12:11:40 +0300784};
785
Marek Vasut828b2362025-01-16 05:03:22 +0100786U_BOOT_DRIVER(eth_enetc_ls) = {
Alex Marginean805b8592019-12-10 16:55:39 +0200787 .name = ENETC_DRIVER_NAME,
Alex Marginean7a910c12019-07-03 12:11:40 +0300788 .id = UCLASS_ETH,
789 .bind = enetc_bind,
790 .probe = enetc_probe,
791 .remove = enetc_remove,
Marek Vasut828b2362025-01-16 05:03:22 +0100792 .ops = &enetc_ops_ls,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700793 .priv_auto = sizeof(struct enetc_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700794 .plat_auto = sizeof(struct eth_pdata),
Alex Marginean7a910c12019-07-03 12:11:40 +0300795};
796
Marek Vasutd89b2262025-01-16 05:03:26 +0100797static const struct enetc_data enetc_data_ls = {
798 .reg_offset_pmr = ENETC_PMR_OFFSET_LS,
799 .reg_offset_psipmar = ENETC_PSIPMARn_OFFSET_LS,
800 .reg_offset_pcapr = ENETC_PCAPR_OFFSET_LS,
801 .reg_offset_psicfgr = ENETC_PSICFGR_OFFSET_LS,
802 .reg_offset_mac = ENETC_PM_OFFSET_LS,
803};
804
Marek Vasut828b2362025-01-16 05:03:22 +0100805static struct pci_device_id enetc_ids_ls[] = {
Marek Vasutd89b2262025-01-16 05:03:26 +0100806 {
807 PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, PCI_DEVICE_ID_ENETC_ETH),
808 .driver_data = (ulong)&enetc_data_ls,
809 },
Alex Marginean7a910c12019-07-03 12:11:40 +0300810 {}
811};
812
Marek Vasut828b2362025-01-16 05:03:22 +0100813U_BOOT_PCI_DEVICE(eth_enetc_ls, enetc_ids_ls);