blob: 67ef5f34a8ab8d725d70fe6a19cfa15bf83d0889 [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
Alice Guo2e0be5a2025-01-16 05:03:30 +01005 * Copyright 2023-2025 NXP
Alex Marginean7a910c12019-07-03 12:11:40 +03006 */
7
Alex Marginean7a910c12019-07-03 12:11:40 +03008#include <dm.h>
9#include <errno.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060010#include <fdt_support.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <malloc.h>
Alex Marginean7a910c12019-07-03 12:11:40 +030012#include <memalign.h>
Simon Glass274e0b02020-05-10 11:39:56 -060013#include <net.h>
14#include <asm/cache.h>
Alex Marginean7a910c12019-07-03 12:11:40 +030015#include <asm/io.h>
16#include <pci.h>
Alex Marginean02155392019-07-03 12:11:41 +030017#include <miiphy.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060018#include <linux/bug.h>
Simon Glassdbd79542020-05-10 11:40:11 -060019#include <linux/delay.h>
Alice Guo2e0be5a2025-01-16 05:03:30 +010020#include <linux/build_bug.h>
21
22#ifdef CONFIG_ARCH_IMX9
23#include <asm/mach-imx/sys_proto.h>
24#include <cpu_func.h>
25#endif
Alex Marginean7a910c12019-07-03 12:11:40 +030026
27#include "fsl_enetc.h"
28
Alex Marginean805b8592019-12-10 16:55:39 +020029#define ENETC_DRIVER_NAME "enetc_eth"
30
Alice Guo2e0be5a2025-01-16 05:03:30 +010031/*
32 * Calculate number of buffer descriptors per cacheline, and compile-time
33 * validate that:
34 * - the RX and TX descriptors are the same size
35 * - the descriptors fit exactly into cachelines without overlap
36 * - all descriptors fit exactly into cachelines
37 */
38#define ENETC_NUM_BD_IN_CL \
39 ((ARCH_DMA_MINALIGN / sizeof(struct enetc_tx_bd)) + \
40 BUILD_BUG_ON_ZERO(sizeof(struct enetc_tx_bd) != \
41 sizeof(union enetc_rx_bd)) + \
42 BUILD_BUG_ON_ZERO(ARCH_DMA_MINALIGN % sizeof(struct enetc_tx_bd)) + \
43 BUILD_BUG_ON_ZERO(ARCH_DMA_MINALIGN % sizeof(union enetc_rx_bd)) + \
44 BUILD_BUG_ON_ZERO(ENETC_BD_CNT % \
45 (ARCH_DMA_MINALIGN / sizeof(struct enetc_tx_bd))))
46
Siarhei Yasinski25b798e2022-08-31 10:57:37 +000047static int enetc_remove(struct udevice *dev);
48
Alice Guo2e0be5a2025-01-16 05:03:30 +010049static int enetc_is_imx95(struct udevice *dev)
50{
51 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
52
53 /* Test whether this is i.MX95 ENETCv4. This may be optimized out. */
54 return IS_ENABLED(CONFIG_ARCH_IMX9) &&
55 pplat->vendor == PCI_VENDOR_ID_PHILIPS;
56}
57
Marek Vasutc05f8dc2025-01-16 05:03:18 +010058static int enetc_is_ls1028a(struct udevice *dev)
59{
60 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
61
62 /* Test whether this is LS1028A ENETC. This may be optimized out. */
63 return IS_ENABLED(CONFIG_ARCH_LS1028A) &&
64 pplat->vendor == PCI_VENDOR_ID_FREESCALE;
65}
66
Marek Vasutdbfb4bc2025-01-16 05:03:23 +010067static int enetc_dev_id(struct udevice *dev)
68{
Alice Guo2e0be5a2025-01-16 05:03:30 +010069 if (enetc_is_imx95(dev))
70 return PCI_DEV(pci_get_devfn(dev)) >> 3;
Marek Vasutdbfb4bc2025-01-16 05:03:23 +010071 if (enetc_is_ls1028a(dev))
72 return PCI_FUNC(pci_get_devfn(dev));
73
74 return 0;
75}
76
Alice Guo2e0be5a2025-01-16 05:03:30 +010077static void enetc_inval_rxbd(struct udevice *dev)
78{
79 struct enetc_priv *priv = dev_get_priv(dev);
80 union enetc_rx_bd *desc = &priv->enetc_rxbd[priv->rx_bdr.next_prod_idx];
81 unsigned long start = rounddown((unsigned long)desc, ARCH_DMA_MINALIGN);
82 unsigned long end = roundup((unsigned long)desc + sizeof(*desc),
83 ARCH_DMA_MINALIGN);
84
85 if (enetc_is_imx95(dev))
86 invalidate_dcache_range(start, end);
87}
88
89static void enetc_flush_bd(struct udevice *dev, int pi, bool tx)
90{
91 struct enetc_priv *priv = dev_get_priv(dev);
92 union enetc_rx_bd *rxdesc = &priv->enetc_rxbd[pi];
93 struct enetc_tx_bd *txdesc = &priv->enetc_txbd[pi];
94 unsigned long desc = tx ? (unsigned long)txdesc : (unsigned long)rxdesc;
95 unsigned long size = tx ? sizeof(*txdesc) : sizeof(*rxdesc);
96 unsigned long start = rounddown(desc, ARCH_DMA_MINALIGN);
97 unsigned long end = roundup(desc + size, ARCH_DMA_MINALIGN);
98
99 if (enetc_is_imx95(dev))
100 flush_dcache_range(start, end);
101}
102
103static void enetc_inval_buffer(struct udevice *dev, void *buf, size_t size)
104{
105 unsigned long start = rounddown((unsigned long)buf, ARCH_DMA_MINALIGN);
106 unsigned long end = roundup((unsigned long)buf + size,
107 ARCH_DMA_MINALIGN);
108
109 if (enetc_is_imx95(dev))
110 invalidate_dcache_range(start, end);
111}
112
113static void enetc_flush_buffer(struct udevice *dev, void *buf, size_t size)
114{
115 unsigned long start = rounddown((unsigned long)buf, ARCH_DMA_MINALIGN);
116 unsigned long end = roundup((unsigned long)buf + size,
117 ARCH_DMA_MINALIGN);
118
119 if (enetc_is_imx95(dev))
120 flush_dcache_range(start, end);
121}
122
Marek Vasutcd684142025-01-16 05:03:24 +0100123/* register accessors */
124static u32 enetc_read_reg(void __iomem *addr)
125{
126 return readl(addr);
127}
128
129static void enetc_write_reg(void __iomem *addr, u32 val)
130{
131 writel(val, addr);
132}
133
134static void enetc_write(struct enetc_priv *priv, u32 off, u32 val)
135{
136 enetc_write_reg(priv->regs_base + off, val);
137}
138
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100139/* base port register accessors */
Marek Vasut278c8442025-01-16 05:03:27 +0100140static void enetc_write_pmr(struct udevice *dev, u32 val)
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100141{
Marek Vasut278c8442025-01-16 05:03:27 +0100142 struct enetc_data *data = (struct enetc_data *)dev_get_driver_data(dev);
143 struct enetc_priv *priv = dev_get_priv(dev);
144 const u32 off = ENETC_PMR + data->reg_offset_pmr;
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100145
146 enetc_write_reg(priv->port_regs + off, val);
147}
148
Marek Vasut278c8442025-01-16 05:03:27 +0100149static void enetc_write_psipmar(struct udevice *dev, int n, u32 val)
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100150{
Marek Vasut278c8442025-01-16 05:03:27 +0100151 struct enetc_data *data = (struct enetc_data *)dev_get_driver_data(dev);
152 struct enetc_priv *priv = dev_get_priv(dev);
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100153 const u32 off = (n ? ENETC_PSIPMAR1 : ENETC_PSIPMAR0) +
Marek Vasut278c8442025-01-16 05:03:27 +0100154 data->reg_offset_psipmar;
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100155
156 enetc_write_reg(priv->port_regs + off, val);
157}
158
159/* port station register accessors */
Marek Vasut278c8442025-01-16 05:03:27 +0100160static void enetc_write_psicfgr(struct udevice *dev, int port, u32 val)
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100161{
Marek Vasut278c8442025-01-16 05:03:27 +0100162 struct enetc_data *data = (struct enetc_data *)dev_get_driver_data(dev);
163 struct enetc_priv *priv = dev_get_priv(dev);
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100164 const u32 off = ENETC_PSICFGR(port, ENETC_PSICFGR_SHIFT_LS) +
Marek Vasut278c8442025-01-16 05:03:27 +0100165 data->reg_offset_psicfgr;
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100166
167 enetc_write_reg(priv->port_regs + off, val);
168}
169
Marek Vasutcd684142025-01-16 05:03:24 +0100170/* port register accessors */
Marek Vasut278c8442025-01-16 05:03:27 +0100171static u32 enetc_read_pcapr_mdio(struct udevice *dev)
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100172{
Marek Vasut278c8442025-01-16 05:03:27 +0100173 struct enetc_data *data = (struct enetc_data *)dev_get_driver_data(dev);
174 struct enetc_priv *priv = dev_get_priv(dev);
175 const u32 off = ENETC_PCAPR0 + data->reg_offset_pcapr;
Alice Guo2e0be5a2025-01-16 05:03:30 +0100176 const u32 reg = enetc_read_reg(priv->port_regs + off);
177
178 if (enetc_is_imx95(dev))
179 return reg & ENETC_PCS_PROT;
180 else if (enetc_is_ls1028a(dev))
181 return reg & ENETC_PCAPRO_MDIO;
182
183 return 0;
184}
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100185
Alice Guo2e0be5a2025-01-16 05:03:30 +0100186static void enetc_write_port(struct enetc_priv *priv, u32 off, u32 val)
187{
188 enetc_write_reg(priv->port_regs + off, val);
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100189}
190
191/* MAC port register accessors */
Marek Vasut278c8442025-01-16 05:03:27 +0100192static u32 enetc_read_mac_port(struct udevice *dev, u32 off)
Marek Vasutcd684142025-01-16 05:03:24 +0100193{
Marek Vasut278c8442025-01-16 05:03:27 +0100194 struct enetc_data *data = (struct enetc_data *)dev_get_driver_data(dev);
195 struct enetc_priv *priv = dev_get_priv(dev);
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100196
Marek Vasut278c8442025-01-16 05:03:27 +0100197 return enetc_read_reg(priv->port_regs + data->reg_offset_mac + off);
Marek Vasutcd684142025-01-16 05:03:24 +0100198}
199
Marek Vasut278c8442025-01-16 05:03:27 +0100200static void enetc_write_mac_port(struct udevice *dev, u32 off, u32 val)
Marek Vasutcd684142025-01-16 05:03:24 +0100201{
Marek Vasut278c8442025-01-16 05:03:27 +0100202 struct enetc_data *data = (struct enetc_data *)dev_get_driver_data(dev);
203 struct enetc_priv *priv = dev_get_priv(dev);
Marek Vasuta1fa5cb2025-01-16 05:03:25 +0100204
Marek Vasut278c8442025-01-16 05:03:27 +0100205 enetc_write_reg(priv->port_regs + data->reg_offset_mac + off, val);
Marek Vasutcd684142025-01-16 05:03:24 +0100206}
207
208/* BDR register accessor, see also ENETC_BDR() */
209static void enetc_bdr_write(struct enetc_priv *priv, int type, int n,
210 u32 off, u32 val)
211{
212 enetc_write(priv, ENETC_BDR(type, n, off), val);
213}
214
Alex Marginean805b8592019-12-10 16:55:39 +0200215/*
216 * sets the MAC address in IERB registers, this setting is persistent and
217 * carried over to Linux.
218 */
Alex Marginean805b8592019-12-10 16:55:39 +0200219#define IERB_BASE 0x1f0800000ULL
220#define IERB_PFMAC(pf, vf, n) (IERB_BASE + 0x8000 + (pf) * 0x100 + (vf) * 8 \
221 + (n) * 4)
222
Marek Vasutd9b36f62025-01-16 05:03:20 +0100223static void enetc_set_ierb_primary_mac(struct udevice *dev, void *blob)
Marek Vasutc9997c72025-01-16 05:03:19 +0100224{
Marek Vasutd9b36f62025-01-16 05:03:20 +0100225 static int ierb_fn_to_pf[] = { 0, 1, 2, -1, -1, -1, 3 };
226 struct pci_child_plat *ppdata = dev_get_parent_plat(dev);
227 struct eth_pdata *pdata = dev_get_plat(dev);
Alice Guo2e0be5a2025-01-16 05:03:30 +0100228 struct enetc_priv *priv = dev_get_priv(dev);
Marek Vasutd9b36f62025-01-16 05:03:20 +0100229 const u8 *enetaddr = pdata->enetaddr;
Alex Marginean805b8592019-12-10 16:55:39 +0200230 u16 lower = *(const u16 *)(enetaddr + 4);
231 u32 upper = *(const u32 *)enetaddr;
Marek Vasutd9b36f62025-01-16 05:03:20 +0100232 int devfn, offset;
233 char path[256];
Alex Marginean805b8592019-12-10 16:55:39 +0200234
Alice Guo2e0be5a2025-01-16 05:03:30 +0100235 if (enetc_is_imx95(dev)) {
236 /*
237 * Configure the ENETC primary MAC addresses - Set register
238 * PMAR0/1 for SI 0 and PSIaPMAR0/1 for SI 1, 2 .. a
239 * (optionally pre-configured in IERB).
240 */
241 devfn = enetc_dev_id(dev);
242 if (devfn > 2)
243 return;
244
245 enetc_write(priv, IMX95_ENETC_SIPMAR0, upper);
246 enetc_write(priv, IMX95_ENETC_SIPMAR1, lower);
247
248 snprintf(path, 256, "/soc/pcie@%x/ethernet@%x,%x",
249 PCI_BUS(dm_pci_get_bdf(dev)), PCI_DEV(ppdata->devfn),
250 PCI_FUNC(ppdata->devfn));
251 } else if (enetc_is_ls1028a(dev)) {
Marek Vasutc9997c72025-01-16 05:03:19 +0100252 /*
253 * LS1028A is the only part with IERB at this time and
254 * there are plans to change its structure, keep this
255 * LS1028A specific for now.
256 */
Marek Vasutd9b36f62025-01-16 05:03:20 +0100257 devfn = PCI_FUNC(ppdata->devfn);
258
Marek Vasutc9997c72025-01-16 05:03:19 +0100259 if (ierb_fn_to_pf[devfn] < 0)
260 return;
Alex Marginean805b8592019-12-10 16:55:39 +0200261
Marek Vasutc9997c72025-01-16 05:03:19 +0100262 out_le32(IERB_PFMAC(ierb_fn_to_pf[devfn], 0, 0), upper);
263 out_le32(IERB_PFMAC(ierb_fn_to_pf[devfn], 0, 1), (u32)lower);
Marek Vasutd9b36f62025-01-16 05:03:20 +0100264
265 snprintf(path, 256, "/soc/pcie@1f0000000/ethernet@%x,%x",
266 PCI_DEV(ppdata->devfn), PCI_FUNC(ppdata->devfn));
267 } else {
268 return;
Marek Vasutc9997c72025-01-16 05:03:19 +0100269 }
Marek Vasutd9b36f62025-01-16 05:03:20 +0100270
271 offset = fdt_path_offset(blob, path);
272 if (offset >= 0)
273 fdt_setprop(blob, offset, "mac-address", pdata->enetaddr, 6);
Alex Marginean805b8592019-12-10 16:55:39 +0200274}
275
276/* sets up primary MAC addresses in DT/IERB */
277void fdt_fixup_enetc_mac(void *blob)
278{
Alex Marginean805b8592019-12-10 16:55:39 +0200279 struct udevice *dev;
280 struct uclass *uc;
Alex Marginean805b8592019-12-10 16:55:39 +0200281
282 uclass_get(UCLASS_ETH, &uc);
283 uclass_foreach_dev(dev, uc) {
284 if (!dev->driver || !dev->driver->name ||
285 strcmp(dev->driver->name, ENETC_DRIVER_NAME))
286 continue;
287
Marek Vasutd9b36f62025-01-16 05:03:20 +0100288 enetc_set_ierb_primary_mac(dev, blob);
Alex Marginean805b8592019-12-10 16:55:39 +0200289 }
290}
291
Alex Marginean7a910c12019-07-03 12:11:40 +0300292/*
293 * Bind the device:
294 * - set a more explicit name on the interface
295 */
296static int enetc_bind(struct udevice *dev)
297{
298 char name[16];
299 static int eth_num_devices;
300
301 /*
302 * prefer using PCI function numbers to number interfaces, but these
303 * are only available if dts nodes are present. For PCI they are
304 * optional, handle that case too. Just in case some nodes are present
305 * and some are not, use different naming scheme - enetc-N based on
306 * PCI function # and enetc#N based on interface count
307 */
Simon Glassa7ece582020-12-19 10:40:14 -0700308 if (ofnode_valid(dev_ofnode(dev)))
Marek Vasutdbfb4bc2025-01-16 05:03:23 +0100309 sprintf(name, "enetc-%u", enetc_dev_id(dev));
Alex Marginean7a910c12019-07-03 12:11:40 +0300310 else
311 sprintf(name, "enetc#%u", eth_num_devices++);
312 device_set_name(dev, name);
313
314 return 0;
315}
316
Alex Marginean38882ae2019-07-03 12:11:42 +0300317/* MDIO wrappers, we're using these to drive internal MDIO to get to serdes */
318static int enetc_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
319{
320 struct enetc_mdio_priv priv;
321
322 priv.regs_base = bus->priv;
323 return enetc_mdio_read_priv(&priv, addr, devad, reg);
324}
325
326static int enetc_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
327 u16 val)
328{
329 struct enetc_mdio_priv priv;
330
331 priv.regs_base = bus->priv;
332 return enetc_mdio_write_priv(&priv, addr, devad, reg, val);
333}
334
335/* only interfaces that can pin out through serdes have internal MDIO */
336static bool enetc_has_imdio(struct udevice *dev)
337{
338 struct enetc_priv *priv = dev_get_priv(dev);
339
340 return !!(priv->imdio.priv);
341}
342
343/* set up serdes for SGMII */
344static int enetc_init_sgmii(struct udevice *dev)
345{
346 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean41a7ac52019-07-15 11:48:47 +0300347 bool is2500 = false;
348 u16 reg;
Alex Marginean38882ae2019-07-03 12:11:42 +0300349
350 if (!enetc_has_imdio(dev))
351 return 0;
352
Simon Glassfada3f92022-09-17 09:00:09 -0600353 if (priv->uclass_id == PHY_INTERFACE_MODE_2500BASEX)
Alex Marginean41a7ac52019-07-15 11:48:47 +0300354 is2500 = true;
355
356 /*
357 * Set to SGMII mode, for 1Gbps enable AN, for 2.5Gbps set fixed speed.
358 * Although fixed speed is 1Gbps, we could be running at 2.5Gbps based
359 * on PLL configuration. Setting 1G for 2.5G here is counter intuitive
360 * but intentional.
361 */
362 reg = ENETC_PCS_IF_MODE_SGMII;
363 reg |= is2500 ? ENETC_PCS_IF_MODE_SPEED_1G : ENETC_PCS_IF_MODE_SGMII_AN;
Alex Marginean38882ae2019-07-03 12:11:42 +0300364 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300365 ENETC_PCS_IF_MODE, reg);
Alex Marginean38882ae2019-07-03 12:11:42 +0300366
367 /* Dev ability - SGMII */
368 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
369 ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SGMII);
370
371 /* Adjust link timer for SGMII */
372 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
373 ENETC_PCS_LINK_TIMER1, ENETC_PCS_LINK_TIMER1_VAL);
374 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
375 ENETC_PCS_LINK_TIMER2, ENETC_PCS_LINK_TIMER2_VAL);
376
Alex Marginean41a7ac52019-07-15 11:48:47 +0300377 reg = ENETC_PCS_CR_DEF_VAL;
378 reg |= is2500 ? ENETC_PCS_CR_RST : ENETC_PCS_CR_RESET_AN;
Alex Marginean38882ae2019-07-03 12:11:42 +0300379 /* restart PCS AN */
380 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300381 ENETC_PCS_CR, reg);
Alex Marginean38882ae2019-07-03 12:11:42 +0300382
383 return 0;
384}
385
386/* set up MAC for RGMII */
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300387static void enetc_init_rgmii(struct udevice *dev, struct phy_device *phydev)
Alex Marginean38882ae2019-07-03 12:11:42 +0300388{
Alice Guo2e0be5a2025-01-16 05:03:30 +0100389 u32 old_val, val, dpx = 0;
Alex Marginean38882ae2019-07-03 12:11:42 +0300390
Marek Vasut278c8442025-01-16 05:03:27 +0100391 old_val = val = enetc_read_mac_port(dev, ENETC_PM_IF_MODE);
Alex Marginean38882ae2019-07-03 12:11:42 +0300392
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300393 /* disable unreliable RGMII in-band signaling and force the MAC into
394 * the speed negotiated by the PHY.
395 */
396 val &= ~ENETC_PM_IF_MODE_AN_ENA;
397
398 if (phydev->speed == SPEED_1000) {
399 val &= ~ENETC_PM_IFM_SSP_MASK;
400 val |= ENETC_PM_IFM_SSP_1000;
401 } else if (phydev->speed == SPEED_100) {
402 val &= ~ENETC_PM_IFM_SSP_MASK;
403 val |= ENETC_PM_IFM_SSP_100;
404 } else if (phydev->speed == SPEED_10) {
405 val &= ~ENETC_PM_IFM_SSP_MASK;
406 val |= ENETC_PM_IFM_SSP_10;
407 }
408
Alice Guo2e0be5a2025-01-16 05:03:30 +0100409 if (enetc_is_imx95(dev))
410 dpx = ENETC_PM_IFM_FULL_DPX_IMX;
411 else if (enetc_is_ls1028a(dev))
412 dpx = ENETC_PM_IFM_FULL_DPX_LS;
413
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300414 if (phydev->duplex == DUPLEX_FULL)
Alice Guo2e0be5a2025-01-16 05:03:30 +0100415 val |= dpx;
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300416 else
Alice Guo2e0be5a2025-01-16 05:03:30 +0100417 val &= ~dpx;
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300418
419 if (val == old_val)
420 return;
421
Marek Vasut278c8442025-01-16 05:03:27 +0100422 enetc_write_mac_port(dev, ENETC_PM_IF_MODE, val);
Alex Marginean38882ae2019-07-03 12:11:42 +0300423}
424
Alex Margineanafad2d02020-01-10 23:32:20 +0200425/* set up MAC configuration for the given interface type */
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300426static void enetc_setup_mac_iface(struct udevice *dev,
427 struct phy_device *phydev)
Alex Marginean38882ae2019-07-03 12:11:42 +0300428{
429 struct enetc_priv *priv = dev_get_priv(dev);
430 u32 if_mode;
431
Simon Glassfada3f92022-09-17 09:00:09 -0600432 switch (priv->uclass_id) {
Alex Margineanafad2d02020-01-10 23:32:20 +0200433 case PHY_INTERFACE_MODE_RGMII:
434 case PHY_INTERFACE_MODE_RGMII_ID:
435 case PHY_INTERFACE_MODE_RGMII_RXID:
436 case PHY_INTERFACE_MODE_RGMII_TXID:
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300437 enetc_init_rgmii(dev, phydev);
Alex Margineanafad2d02020-01-10 23:32:20 +0200438 break;
Alex Margineanafad2d02020-01-10 23:32:20 +0200439 case PHY_INTERFACE_MODE_USXGMII:
Vladimir Oltean6a6e4022021-09-18 15:32:34 +0300440 case PHY_INTERFACE_MODE_10GBASER:
Alex Margineanafad2d02020-01-10 23:32:20 +0200441 /* set ifmode to (US)XGMII */
Marek Vasut278c8442025-01-16 05:03:27 +0100442 if_mode = enetc_read_mac_port(dev, ENETC_PM_IF_MODE);
Alice Guo2e0be5a2025-01-16 05:03:30 +0100443 if (enetc_is_imx95(dev))
444 if_mode &= ~ENETC_PM_IF_IFMODE_MASK_IMX;
445 else if (enetc_is_ls1028a(dev))
446 if_mode &= ~ENETC_PM_IF_IFMODE_MASK_LS;
Marek Vasut278c8442025-01-16 05:03:27 +0100447 enetc_write_mac_port(dev, ENETC_PM_IF_MODE, if_mode);
Alex Margineanafad2d02020-01-10 23:32:20 +0200448 break;
449 };
450}
451
452/* set up serdes for SXGMII */
453static int enetc_init_sxgmii(struct udevice *dev)
454{
455 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean38882ae2019-07-03 12:11:42 +0300456
457 if (!enetc_has_imdio(dev))
458 return 0;
459
460 /* Dev ability - SXGMII */
461 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
462 ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SXGMII);
463
464 /* Restart PCS AN */
465 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
466 ENETC_PCS_CR,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300467 ENETC_PCS_CR_RST | ENETC_PCS_CR_RESET_AN);
Alex Marginean38882ae2019-07-03 12:11:42 +0300468
469 return 0;
470}
471
472/* Apply protocol specific configuration to MAC, serdes as needed */
473static void enetc_start_pcs(struct udevice *dev)
474{
475 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean38882ae2019-07-03 12:11:42 +0300476
Alex Margineand4be7682019-11-25 17:57:27 +0200477 /* register internal MDIO for debug purposes */
Marek Vasut278c8442025-01-16 05:03:27 +0100478 if (enetc_read_pcapr_mdio(dev)) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300479 priv->imdio.read = enetc_mdio_read;
480 priv->imdio.write = enetc_mdio_write;
481 priv->imdio.priv = priv->port_regs + ENETC_PM_IMDIO_BASE;
Vladimir Olteandcd21cc2021-09-27 14:21:48 +0300482 strlcpy(priv->imdio.name, dev->name, MDIO_NAME_LEN);
Alex Margineand4be7682019-11-25 17:57:27 +0200483 if (!miiphy_get_dev_by_name(priv->imdio.name))
484 mdio_register(&priv->imdio);
Alex Marginean38882ae2019-07-03 12:11:42 +0300485 }
486
Simon Glassa7ece582020-12-19 10:40:14 -0700487 if (!ofnode_valid(dev_ofnode(dev))) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300488 enetc_dbg(dev, "no enetc ofnode found, skipping PCS set-up\n");
489 return;
490 }
491
Simon Glassfada3f92022-09-17 09:00:09 -0600492 priv->uclass_id = dev_read_phy_mode(dev);
493 if (priv->uclass_id == PHY_INTERFACE_MODE_NA) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300494 enetc_dbg(dev,
495 "phy-mode property not found, defaulting to SGMII\n");
Simon Glassfada3f92022-09-17 09:00:09 -0600496 priv->uclass_id = PHY_INTERFACE_MODE_SGMII;
Marek Behúnbc194772022-04-07 00:33:01 +0200497 }
Alex Marginean38882ae2019-07-03 12:11:42 +0300498
Simon Glassfada3f92022-09-17 09:00:09 -0600499 switch (priv->uclass_id) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300500 case PHY_INTERFACE_MODE_SGMII:
Vladimir Oltean6caef972021-09-18 15:32:35 +0300501 case PHY_INTERFACE_MODE_2500BASEX:
Alex Marginean38882ae2019-07-03 12:11:42 +0300502 enetc_init_sgmii(dev);
503 break;
Alex Margineaned0460c2019-11-14 18:28:38 +0200504 case PHY_INTERFACE_MODE_USXGMII:
Vladimir Oltean6a6e4022021-09-18 15:32:34 +0300505 case PHY_INTERFACE_MODE_10GBASER:
Alex Marginean38882ae2019-07-03 12:11:42 +0300506 enetc_init_sxgmii(dev);
507 break;
508 };
509}
510
Alex Marginean02155392019-07-03 12:11:41 +0300511/* Configure the actual/external ethernet PHY, if one is found */
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300512static int enetc_config_phy(struct udevice *dev)
Alex Marginean02155392019-07-03 12:11:41 +0300513{
514 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean02155392019-07-03 12:11:41 +0300515 int supported;
516
Alex Marginean602e00f2019-11-25 17:15:13 +0200517 priv->phy = dm_eth_phy_connect(dev);
Alex Marginean602e00f2019-11-25 17:15:13 +0200518 if (!priv->phy)
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300519 return -ENODEV;
Alex Marginean02155392019-07-03 12:11:41 +0300520
Alex Margineanb93375c2019-11-14 18:58:45 +0200521 supported = PHY_GBIT_FEATURES | SUPPORTED_2500baseX_Full;
522 priv->phy->supported &= supported;
523 priv->phy->advertising &= supported;
Alex Marginean602e00f2019-11-25 17:15:13 +0200524
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300525 return phy_config(priv->phy);
Alex Marginean02155392019-07-03 12:11:41 +0300526}
527
Alex Marginean7a910c12019-07-03 12:11:40 +0300528/*
529 * Probe ENETC driver:
530 * - initialize port and station interface BARs
531 */
532static int enetc_probe(struct udevice *dev)
533{
534 struct enetc_priv *priv = dev_get_priv(dev);
Siarhei Yasinski25b798e2022-08-31 10:57:37 +0000535 int res;
Alex Marginean7a910c12019-07-03 12:11:40 +0300536
Simon Glass2e4938b2022-09-06 20:27:17 -0600537 if (ofnode_valid(dev_ofnode(dev)) && !ofnode_is_enabled(dev_ofnode(dev))) {
Alex Marginean7a910c12019-07-03 12:11:40 +0300538 enetc_dbg(dev, "interface disabled\n");
539 return -ENODEV;
540 }
541
542 priv->enetc_txbd = memalign(ENETC_BD_ALIGN,
543 sizeof(struct enetc_tx_bd) * ENETC_BD_CNT);
544 priv->enetc_rxbd = memalign(ENETC_BD_ALIGN,
545 sizeof(union enetc_rx_bd) * ENETC_BD_CNT);
546
547 if (!priv->enetc_txbd || !priv->enetc_rxbd) {
548 /* free should be able to handle NULL, just free all pointers */
549 free(priv->enetc_txbd);
550 free(priv->enetc_rxbd);
551
552 return -ENOMEM;
553 }
554
555 /* initialize register */
Andrew Scull6520c822022-04-21 16:11:13 +0000556 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 +0300557 if (!priv->regs_base) {
558 enetc_dbg(dev, "failed to map BAR0\n");
559 return -EINVAL;
560 }
561 priv->port_regs = priv->regs_base + ENETC_PORT_REGS_OFF;
562
563 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
564
Alex Margineanc905c212019-11-14 18:58:46 +0200565 enetc_start_pcs(dev);
Alex Margineanc905c212019-11-14 18:58:46 +0200566
Siarhei Yasinski25b798e2022-08-31 10:57:37 +0000567 res = enetc_config_phy(dev);
568 if(res)
569 enetc_remove(dev);
570 return res;
Alex Marginean7a910c12019-07-03 12:11:40 +0300571}
572
573/*
574 * Remove the driver from an interface:
575 * - free up allocated memory
576 */
577static int enetc_remove(struct udevice *dev)
578{
579 struct enetc_priv *priv = dev_get_priv(dev);
580
Michael Walle3f66e8e2022-05-31 18:36:16 +0200581 if (miiphy_get_dev_by_name(priv->imdio.name))
582 mdio_unregister(&priv->imdio);
583
Alex Marginean7a910c12019-07-03 12:11:40 +0300584 free(priv->enetc_txbd);
585 free(priv->enetc_rxbd);
586
587 return 0;
588}
589
Alice Guo2e0be5a2025-01-16 05:03:30 +0100590static int enetc_imx95_write_hwaddr(struct udevice *dev)
591{
592 struct eth_pdata *plat = dev_get_plat(dev);
593 struct enetc_priv *priv = dev_get_priv(dev);
594 u8 *addr = plat->enetaddr;
595
596 u16 lower = *(const u16 *)(addr + 4);
597 u32 upper = *(const u32 *)addr;
598
599 enetc_write_port(priv, IMX95_ENETC_PMAR0, upper);
600 enetc_write_port(priv, IMX95_ENETC_PMAR1, lower);
601
602 return 0;
603}
604
Michael Walle1d3e24f2019-12-20 14:16:48 +0100605/*
606 * LS1028A is the only part with IERB at this time and there are plans to
607 * change its structure, keep this LS1028A specific for now.
608 */
609#define LS1028A_IERB_BASE 0x1f0800000ULL
610#define LS1028A_IERB_PSIPMAR0(pf, vf) (LS1028A_IERB_BASE + 0x8000 \
611 + (pf) * 0x100 + (vf) * 8)
612#define LS1028A_IERB_PSIPMAR1(pf, vf) (LS1028A_IERB_PSIPMAR0(pf, vf) + 4)
613
614static int enetc_ls1028a_write_hwaddr(struct udevice *dev)
615{
Simon Glassb75b15b2020-12-03 16:55:23 -0700616 struct pci_child_plat *ppdata = dev_get_parent_plat(dev);
Michael Walle1d3e24f2019-12-20 14:16:48 +0100617 const int devfn_to_pf[] = {0, 1, 2, -1, -1, -1, 3};
Simon Glassfa20e932020-12-03 16:55:20 -0700618 struct eth_pdata *plat = dev_get_plat(dev);
Michael Walle1d3e24f2019-12-20 14:16:48 +0100619 int devfn = PCI_FUNC(ppdata->devfn);
620 u8 *addr = plat->enetaddr;
621 u32 lower, upper;
622 int pf;
623
624 if (devfn >= ARRAY_SIZE(devfn_to_pf))
625 return 0;
626
627 pf = devfn_to_pf[devfn];
628 if (pf < 0)
629 return 0;
630
631 lower = *(const u16 *)(addr + 4);
632 upper = *(const u32 *)addr;
633
634 out_le32(LS1028A_IERB_PSIPMAR0(pf, 0), upper);
635 out_le32(LS1028A_IERB_PSIPMAR1(pf, 0), lower);
636
637 return 0;
638}
639
Michael Walle8c7188e2019-12-20 14:16:47 +0100640static int enetc_write_hwaddr(struct udevice *dev)
Alex Marginean7a910c12019-07-03 12:11:40 +0300641{
Simon Glassfa20e932020-12-03 16:55:20 -0700642 struct eth_pdata *plat = dev_get_plat(dev);
Michael Walle8c7188e2019-12-20 14:16:47 +0100643 u8 *addr = plat->enetaddr;
644
Alice Guo2e0be5a2025-01-16 05:03:30 +0100645 if (enetc_is_imx95(dev))
646 return enetc_imx95_write_hwaddr(dev);
Marek Vasutc05f8dc2025-01-16 05:03:18 +0100647 if (enetc_is_ls1028a(dev))
Michael Walle1d3e24f2019-12-20 14:16:48 +0100648 return enetc_ls1028a_write_hwaddr(dev);
649
Alex Marginean7a910c12019-07-03 12:11:40 +0300650 u16 lower = *(const u16 *)(addr + 4);
651 u32 upper = *(const u32 *)addr;
652
Marek Vasut278c8442025-01-16 05:03:27 +0100653 enetc_write_psipmar(dev, 0, upper);
654 enetc_write_psipmar(dev, 1, lower);
Michael Walle8c7188e2019-12-20 14:16:47 +0100655
656 return 0;
Alex Marginean7a910c12019-07-03 12:11:40 +0300657}
658
659/* Configure port parameters (# of rings, frame size, enable port) */
Marek Vasutb0dc0b72025-01-16 05:03:21 +0100660static void enetc_enable_si_port(struct udevice *dev)
Alex Marginean7a910c12019-07-03 12:11:40 +0300661{
Marek Vasutb0dc0b72025-01-16 05:03:21 +0100662 struct enetc_priv *priv = dev_get_priv(dev);
Alice Guo2e0be5a2025-01-16 05:03:30 +0100663 u32 val = ENETC_PM_CC_TXP_IMX | ENETC_PM_CC_TX | ENETC_PM_CC_RX;
Alex Marginean7a910c12019-07-03 12:11:40 +0300664
665 /* set Rx/Tx BDR count */
Marek Vasut278c8442025-01-16 05:03:27 +0100666 enetc_write_psicfgr(dev, 0, ENETC_PSICFGR_SET_BDR(ENETC_RX_BDR_CNT,
667 ENETC_TX_BDR_CNT));
Alex Marginean7a910c12019-07-03 12:11:40 +0300668 /* set Rx max frame size */
Marek Vasut278c8442025-01-16 05:03:27 +0100669 enetc_write_mac_port(dev, ENETC_PM_MAXFRM, ENETC_RX_MAXFRM_SIZE);
Alex Marginean7a910c12019-07-03 12:11:40 +0300670 /* enable MAC port */
Alice Guo2e0be5a2025-01-16 05:03:30 +0100671 if (enetc_is_ls1028a(dev))
672 val |= ENETC_PM_CC_TXP_LS | ENETC_PM_CC_PROMIS;
673 enetc_write_mac_port(dev, ENETC_PM_CC, val);
Alex Marginean7a910c12019-07-03 12:11:40 +0300674 /* enable port */
Alice Guo2e0be5a2025-01-16 05:03:30 +0100675 if (enetc_is_imx95(dev))
676 enetc_write_port(priv, ENETC_POR, 0x0);
Marek Vasut278c8442025-01-16 05:03:27 +0100677 enetc_write_pmr(dev, ENETC_PMR_SI0_EN);
Alex Marginean7a910c12019-07-03 12:11:40 +0300678 /* set SI cache policy */
Alice Guo2e0be5a2025-01-16 05:03:30 +0100679 enetc_write(priv, ENETC_SICAR0, ENETC_SICAR_WR_CFG |
680 (enetc_is_imx95(dev) ?
681 ENETC_SICAR_RD_CFG_IMX :
682 ENETC_SICAR_RD_CFG_LS));
Alex Marginean7a910c12019-07-03 12:11:40 +0300683 /* enable SI */
684 enetc_write(priv, ENETC_SIMR, ENETC_SIMR_EN);
685}
686
687/* returns DMA address for a given buffer index */
688static inline u64 enetc_rxb_address(struct udevice *dev, int i)
689{
690 return cpu_to_le64(dm_pci_virt_to_mem(dev, net_rx_packets[i]));
691}
692
693/*
694 * Setup a single Tx BD Ring (ID = 0):
695 * - set Tx buffer descriptor address
696 * - set the BD count
697 * - initialize the producer and consumer index
698 */
699static void enetc_setup_tx_bdr(struct udevice *dev)
700{
701 struct enetc_priv *priv = dev_get_priv(dev);
702 struct bd_ring *tx_bdr = &priv->tx_bdr;
703 u64 tx_bd_add = (u64)priv->enetc_txbd;
704
705 /* used later to advance to the next Tx BD */
706 tx_bdr->bd_count = ENETC_BD_CNT;
707 tx_bdr->next_prod_idx = 0;
708 tx_bdr->next_cons_idx = 0;
709 tx_bdr->cons_idx = priv->regs_base +
710 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBCIR);
711 tx_bdr->prod_idx = priv->regs_base +
712 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBPIR);
713
714 /* set Tx BD address */
715 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR0,
716 lower_32_bits(tx_bd_add));
717 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR1,
718 upper_32_bits(tx_bd_add));
719 /* set Tx 8 BD count */
720 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBLENR,
721 tx_bdr->bd_count);
722
723 /* reset both producer/consumer indexes */
724 enetc_write_reg(tx_bdr->cons_idx, tx_bdr->next_cons_idx);
725 enetc_write_reg(tx_bdr->prod_idx, tx_bdr->next_prod_idx);
726
727 /* enable TX ring */
728 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBMR, ENETC_TBMR_EN);
729}
730
731/*
732 * Setup a single Rx BD Ring (ID = 0):
733 * - set Rx buffer descriptors address (one descriptor per buffer)
734 * - set buffer size as max frame size
735 * - enable Rx ring
736 * - reset consumer and producer indexes
737 * - set buffer for each descriptor
738 */
739static void enetc_setup_rx_bdr(struct udevice *dev)
740{
741 struct enetc_priv *priv = dev_get_priv(dev);
742 struct bd_ring *rx_bdr = &priv->rx_bdr;
743 u64 rx_bd_add = (u64)priv->enetc_rxbd;
744 int i;
745
746 /* used later to advance to the next BD produced by ENETC HW */
747 rx_bdr->bd_count = ENETC_BD_CNT;
748 rx_bdr->next_prod_idx = 0;
749 rx_bdr->next_cons_idx = 0;
750 rx_bdr->cons_idx = priv->regs_base +
751 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBCIR);
752 rx_bdr->prod_idx = priv->regs_base +
753 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBPIR);
754
755 /* set Rx BD address */
756 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR0,
757 lower_32_bits(rx_bd_add));
758 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR1,
759 upper_32_bits(rx_bd_add));
760 /* set Rx BD count (multiple of 8) */
761 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBLENR,
762 rx_bdr->bd_count);
763 /* set Rx buffer size */
764 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBSR, PKTSIZE_ALIGN);
765
766 /* fill Rx BD */
767 memset(priv->enetc_rxbd, 0,
768 rx_bdr->bd_count * sizeof(union enetc_rx_bd));
769 for (i = 0; i < rx_bdr->bd_count; i++) {
770 priv->enetc_rxbd[i].w.addr = enetc_rxb_address(dev, i);
771 /* each RX buffer must be aligned to 64B */
772 WARN_ON(priv->enetc_rxbd[i].w.addr & (ARCH_DMA_MINALIGN - 1));
Alice Guo2e0be5a2025-01-16 05:03:30 +0100773
774 enetc_flush_bd(dev, i, false);
Alex Marginean7a910c12019-07-03 12:11:40 +0300775 }
776
777 /* reset producer (ENETC owned) and consumer (SW owned) index */
778 enetc_write_reg(rx_bdr->cons_idx, rx_bdr->next_cons_idx);
779 enetc_write_reg(rx_bdr->prod_idx, rx_bdr->next_prod_idx);
780
781 /* enable Rx ring */
782 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBMR, ENETC_RBMR_EN);
783}
784
785/*
786 * Start ENETC interface:
787 * - perform FLR
788 * - enable access to port and SI registers
789 * - set mac address
790 * - setup TX/RX buffer descriptors
791 * - enable Tx/Rx rings
792 */
793static int enetc_start(struct udevice *dev)
794{
Alice Guo2e0be5a2025-01-16 05:03:30 +0100795 int ret;
Alex Marginean7a910c12019-07-03 12:11:40 +0300796 struct enetc_priv *priv = dev_get_priv(dev);
797
798 /* reset and enable the PCI device */
799 dm_pci_flr(dev);
800 dm_pci_clrset_config16(dev, PCI_COMMAND, 0,
801 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
802
Marek Vasutb0dc0b72025-01-16 05:03:21 +0100803 enetc_enable_si_port(dev);
Alex Marginean7a910c12019-07-03 12:11:40 +0300804
805 /* setup Tx/Rx buffer descriptors */
806 enetc_setup_tx_bdr(dev);
807 enetc_setup_rx_bdr(dev);
808
Alice Guo2e0be5a2025-01-16 05:03:30 +0100809 ret = phy_startup(priv->phy);
810 if (ret)
811 return ret;
812
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300813 enetc_setup_mac_iface(dev, priv->phy);
814
Alice Guo2e0be5a2025-01-16 05:03:30 +0100815 return 0;
Alex Marginean7a910c12019-07-03 12:11:40 +0300816}
817
818/*
819 * Stop the network interface:
820 * - just quiesce it, we can wipe all configuration as _start starts from
821 * scratch each time
822 */
823static void enetc_stop(struct udevice *dev)
824{
825 /* FLR is sufficient to quiesce the device */
826 dm_pci_flr(dev);
Alex Margineand4be7682019-11-25 17:57:27 +0200827 /* leave the BARs accessible after we stop, this is needed to use
828 * internal MDIO in command line.
829 */
830 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
Alex Marginean7a910c12019-07-03 12:11:40 +0300831}
832
833/*
834 * ENETC transmit packet:
835 * - check if Tx BD ring is full
836 * - set buffer/packet address (dma address)
837 * - set final fragment flag
838 * - try while producer index equals consumer index or timeout
839 */
840static int enetc_send(struct udevice *dev, void *packet, int length)
841{
842 struct enetc_priv *priv = dev_get_priv(dev);
843 struct bd_ring *txr = &priv->tx_bdr;
844 void *nv_packet = (void *)packet;
845 int tries = ENETC_POLL_TRIES;
846 u32 pi, ci;
847
848 pi = txr->next_prod_idx;
849 ci = enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK;
850 /* Tx ring is full when */
851 if (((pi + 1) % txr->bd_count) == ci) {
852 enetc_dbg(dev, "Tx BDR full\n");
853 return -ETIMEDOUT;
854 }
855 enetc_dbg(dev, "TxBD[%d]send: pkt_len=%d, buff @0x%x%08x\n", pi, length,
856 upper_32_bits((u64)nv_packet), lower_32_bits((u64)nv_packet));
857
Alice Guo2e0be5a2025-01-16 05:03:30 +0100858 enetc_flush_buffer(dev, packet, length);
859
Alex Marginean7a910c12019-07-03 12:11:40 +0300860 /* prepare Tx BD */
861 memset(&priv->enetc_txbd[pi], 0x0, sizeof(struct enetc_tx_bd));
862 priv->enetc_txbd[pi].addr =
863 cpu_to_le64(dm_pci_virt_to_mem(dev, nv_packet));
864 priv->enetc_txbd[pi].buf_len = cpu_to_le16(length);
865 priv->enetc_txbd[pi].frm_len = cpu_to_le16(length);
866 priv->enetc_txbd[pi].flags = cpu_to_le16(ENETC_TXBD_FLAGS_F);
Alice Guo2e0be5a2025-01-16 05:03:30 +0100867
Alex Marginean7a910c12019-07-03 12:11:40 +0300868 dmb();
Alice Guo2e0be5a2025-01-16 05:03:30 +0100869 enetc_flush_bd(dev, pi, true);
870
Alex Marginean7a910c12019-07-03 12:11:40 +0300871 /* send frame: increment producer index */
872 pi = (pi + 1) % txr->bd_count;
873 txr->next_prod_idx = pi;
874 enetc_write_reg(txr->prod_idx, pi);
875 while ((--tries >= 0) &&
876 (pi != (enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK)))
877 udelay(10);
878
879 return tries > 0 ? 0 : -ETIMEDOUT;
880}
881
882/*
883 * Receive frame:
884 * - wait for the next BD to get ready bit set
885 * - clean up the descriptor
886 * - move on and indicate to HW that the cleaned BD is available for Rx
887 */
888static int enetc_recv(struct udevice *dev, int flags, uchar **packetp)
889{
890 struct enetc_priv *priv = dev_get_priv(dev);
891 struct bd_ring *rxr = &priv->rx_bdr;
Alex Marginean7a910c12019-07-03 12:11:40 +0300892 int pi = rxr->next_prod_idx;
Alice Guo2e0be5a2025-01-16 05:03:30 +0100893 int tries = ENETC_POLL_TRIES;
Alex Marginean7a910c12019-07-03 12:11:40 +0300894 u32 status;
895 int len;
896 u8 rdy;
897
898 do {
899 dmb();
Alice Guo2e0be5a2025-01-16 05:03:30 +0100900 enetc_inval_rxbd(dev);
Alex Marginean7a910c12019-07-03 12:11:40 +0300901 status = le32_to_cpu(priv->enetc_rxbd[pi].r.lstatus);
902 /* check if current BD is ready to be consumed */
903 rdy = ENETC_RXBD_STATUS_R(status);
904 } while (--tries >= 0 && !rdy);
905
906 if (!rdy)
907 return -EAGAIN;
908
909 dmb();
910 len = le16_to_cpu(priv->enetc_rxbd[pi].r.buf_len);
911 *packetp = (uchar *)enetc_rxb_address(dev, pi);
Alice Guo2e0be5a2025-01-16 05:03:30 +0100912 enetc_inval_buffer(dev, *packetp, len);
Alex Marginean7a910c12019-07-03 12:11:40 +0300913 enetc_dbg(dev, "RxBD[%d]: len=%d err=%d pkt=0x%x%08x\n", pi, len,
914 ENETC_RXBD_STATUS_ERRORS(status),
915 upper_32_bits((u64)*packetp), lower_32_bits((u64)*packetp));
916
Alice Guo2e0be5a2025-01-16 05:03:30 +0100917 return len;
918}
919
920static int enetc_free_pkt(struct udevice *dev, uchar *packet, int length)
921{
922 const int bd_num_in_cl = enetc_is_imx95(dev) ? ENETC_NUM_BD_IN_CL : 1;
923 struct enetc_priv *priv = dev_get_priv(dev);
924 struct bd_ring *rxr = &priv->rx_bdr;
925 int pi = rxr->next_prod_idx;
926 int ci = rxr->next_cons_idx;
927 uchar *packet_expected;
928 int i;
929
930 packet_expected = (uchar *)enetc_rxb_address(dev, pi);
931 if (packet != packet_expected) {
932 printf("%s: Unexpected packet (expected %p)\n", __func__,
933 packet_expected);
934 return -EINVAL;
935 }
936
Alex Marginean7a910c12019-07-03 12:11:40 +0300937 rxr->next_prod_idx = (pi + 1) % rxr->bd_count;
938 ci = (ci + 1) % rxr->bd_count;
939 rxr->next_cons_idx = ci;
940 dmb();
Alex Marginean7a910c12019-07-03 12:11:40 +0300941
Alice Guo2e0be5a2025-01-16 05:03:30 +0100942 if ((pi + 1) % bd_num_in_cl == 0) {
943 /* BD clean up and advance to next in ring */
944 for (i = 0; i < bd_num_in_cl; i++) {
945 memset(&priv->enetc_rxbd[pi - i], 0, sizeof(union enetc_rx_bd));
946 priv->enetc_rxbd[pi - i].w.addr = enetc_rxb_address(dev, pi - i);
947 }
948
949 /* Will flush all bds in one cacheline */
950 enetc_flush_bd(dev, pi - bd_num_in_cl + 1, false);
951
952 /* free up the slot in the ring for HW */
953 enetc_write_reg(rxr->cons_idx, ci);
954 }
955
956 return 0;
Alex Marginean7a910c12019-07-03 12:11:40 +0300957}
958
Alice Guo2e0be5a2025-01-16 05:03:30 +0100959#if IS_ENABLED(CONFIG_ARCH_IMX9)
960static int enetc_read_rom_hwaddr(struct udevice *dev)
961{
962 struct eth_pdata *pdata = dev_get_plat(dev);
963 unsigned int dev_id = enetc_dev_id(dev);
964 unsigned char *mac = pdata->enetaddr;
965
966 if (dev_id > 2)
967 return -EINVAL;
968
969 imx_get_mac_from_fuse(dev_id, mac);
970
971 return !is_valid_ethaddr(mac);
972}
973
974static const struct eth_ops enetc_ops_imx = {
975 .start = enetc_start,
976 .send = enetc_send,
977 .recv = enetc_recv,
978 .stop = enetc_stop,
979 .free_pkt = enetc_free_pkt,
980 .write_hwaddr = enetc_write_hwaddr,
981 .read_rom_hwaddr = enetc_read_rom_hwaddr,
982};
983
984U_BOOT_DRIVER(eth_enetc_imx) = {
985 .name = ENETC_DRIVER_NAME,
986 .id = UCLASS_ETH,
987 .bind = enetc_bind,
988 .probe = enetc_probe,
989 .remove = enetc_remove,
990 .ops = &enetc_ops_imx,
991 .priv_auto = sizeof(struct enetc_priv),
992 .plat_auto = sizeof(struct eth_pdata),
993};
994
995static const struct enetc_data enetc_data_imx = {
996 .reg_offset_pmr = ENETC_PMR_OFFSET_IMX,
997 .reg_offset_psipmar = ENETC_PSIPMARn_OFFSET_IMX,
998 .reg_offset_pcapr = ENETC_PCAPR_OFFSET_IMX,
999 .reg_offset_psicfgr = ENETC_PSICFGR_OFFSET_IMX,
1000 .reg_offset_mac = ENETC_PM_OFFSET_IMX,
1001};
1002
1003static struct pci_device_id enetc_ids_imx[] = {
1004 {
1005 PCI_DEVICE(PCI_VENDOR_ID_PHILIPS, PCI_DEVICE_ID_ENETC4_ETH),
1006 .driver_data = (ulong)&enetc_data_imx,
1007 },
1008 {}
1009};
1010
1011U_BOOT_PCI_DEVICE(eth_enetc_imx, enetc_ids_imx);
1012#endif
1013
Marek Vasut828b2362025-01-16 05:03:22 +01001014static const struct eth_ops enetc_ops_ls = {
Alex Marginean7a910c12019-07-03 12:11:40 +03001015 .start = enetc_start,
1016 .send = enetc_send,
1017 .recv = enetc_recv,
1018 .stop = enetc_stop,
Alice Guo2e0be5a2025-01-16 05:03:30 +01001019 .free_pkt = enetc_free_pkt,
Michael Walle8c7188e2019-12-20 14:16:47 +01001020 .write_hwaddr = enetc_write_hwaddr,
Alex Marginean7a910c12019-07-03 12:11:40 +03001021};
1022
Marek Vasut828b2362025-01-16 05:03:22 +01001023U_BOOT_DRIVER(eth_enetc_ls) = {
Alex Marginean805b8592019-12-10 16:55:39 +02001024 .name = ENETC_DRIVER_NAME,
Alex Marginean7a910c12019-07-03 12:11:40 +03001025 .id = UCLASS_ETH,
1026 .bind = enetc_bind,
1027 .probe = enetc_probe,
1028 .remove = enetc_remove,
Marek Vasut828b2362025-01-16 05:03:22 +01001029 .ops = &enetc_ops_ls,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001030 .priv_auto = sizeof(struct enetc_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -07001031 .plat_auto = sizeof(struct eth_pdata),
Alex Marginean7a910c12019-07-03 12:11:40 +03001032};
1033
Marek Vasutd89b2262025-01-16 05:03:26 +01001034static const struct enetc_data enetc_data_ls = {
1035 .reg_offset_pmr = ENETC_PMR_OFFSET_LS,
1036 .reg_offset_psipmar = ENETC_PSIPMARn_OFFSET_LS,
1037 .reg_offset_pcapr = ENETC_PCAPR_OFFSET_LS,
1038 .reg_offset_psicfgr = ENETC_PSICFGR_OFFSET_LS,
1039 .reg_offset_mac = ENETC_PM_OFFSET_LS,
1040};
1041
Marek Vasut828b2362025-01-16 05:03:22 +01001042static struct pci_device_id enetc_ids_ls[] = {
Marek Vasutd89b2262025-01-16 05:03:26 +01001043 {
1044 PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, PCI_DEVICE_ID_ENETC_ETH),
1045 .driver_data = (ulong)&enetc_data_ls,
1046 },
Alex Marginean7a910c12019-07-03 12:11:40 +03001047 {}
1048};
1049
Marek Vasut828b2362025-01-16 05:03:22 +01001050U_BOOT_PCI_DEVICE(eth_enetc_ls, enetc_ids_ls);