blob: 1724f948bcd564540ffb83a570af2b48e16c6270 [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
7#include <common.h>
8#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>
Alex Marginean7a910c12019-07-03 12:11:40 +030020
21#include "fsl_enetc.h"
22
Alex Marginean805b8592019-12-10 16:55:39 +020023#define ENETC_DRIVER_NAME "enetc_eth"
24
25/*
26 * sets the MAC address in IERB registers, this setting is persistent and
27 * carried over to Linux.
28 */
29static void enetc_set_ierb_primary_mac(struct udevice *dev, int devfn,
30 const u8 *enetaddr)
31{
32#ifdef CONFIG_ARCH_LS1028A
33/*
34 * LS1028A is the only part with IERB at this time and there are plans to change
35 * its structure, keep this LS1028A specific for now
36 */
37#define IERB_BASE 0x1f0800000ULL
38#define IERB_PFMAC(pf, vf, n) (IERB_BASE + 0x8000 + (pf) * 0x100 + (vf) * 8 \
39 + (n) * 4)
40
41static int ierb_fn_to_pf[] = {0, 1, 2, -1, -1, -1, 3};
42
43 u16 lower = *(const u16 *)(enetaddr + 4);
44 u32 upper = *(const u32 *)enetaddr;
45
46 if (ierb_fn_to_pf[devfn] < 0)
47 return;
48
49 out_le32(IERB_PFMAC(ierb_fn_to_pf[devfn], 0, 0), upper);
50 out_le32(IERB_PFMAC(ierb_fn_to_pf[devfn], 0, 1), (u32)lower);
51#endif
52}
53
54/* sets up primary MAC addresses in DT/IERB */
55void fdt_fixup_enetc_mac(void *blob)
56{
Simon Glassb75b15b2020-12-03 16:55:23 -070057 struct pci_child_plat *ppdata;
Alex Marginean805b8592019-12-10 16:55:39 +020058 struct eth_pdata *pdata;
59 struct udevice *dev;
60 struct uclass *uc;
61 char path[256];
62 int offset;
63 int devfn;
64
65 uclass_get(UCLASS_ETH, &uc);
66 uclass_foreach_dev(dev, uc) {
67 if (!dev->driver || !dev->driver->name ||
68 strcmp(dev->driver->name, ENETC_DRIVER_NAME))
69 continue;
70
Simon Glassfa20e932020-12-03 16:55:20 -070071 pdata = dev_get_plat(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -070072 ppdata = dev_get_parent_plat(dev);
Alex Marginean805b8592019-12-10 16:55:39 +020073 devfn = PCI_FUNC(ppdata->devfn);
74
75 enetc_set_ierb_primary_mac(dev, devfn, pdata->enetaddr);
76
77 snprintf(path, 256, "/soc/pcie@1f0000000/ethernet@%x,%x",
78 PCI_DEV(ppdata->devfn), PCI_FUNC(ppdata->devfn));
79 offset = fdt_path_offset(blob, path);
80 if (offset < 0)
81 continue;
82 fdt_setprop(blob, offset, "mac-address", pdata->enetaddr, 6);
83 }
84}
85
Alex Marginean7a910c12019-07-03 12:11:40 +030086/*
87 * Bind the device:
88 * - set a more explicit name on the interface
89 */
90static int enetc_bind(struct udevice *dev)
91{
92 char name[16];
93 static int eth_num_devices;
94
95 /*
96 * prefer using PCI function numbers to number interfaces, but these
97 * are only available if dts nodes are present. For PCI they are
98 * optional, handle that case too. Just in case some nodes are present
99 * and some are not, use different naming scheme - enetc-N based on
100 * PCI function # and enetc#N based on interface count
101 */
Simon Glassa7ece582020-12-19 10:40:14 -0700102 if (ofnode_valid(dev_ofnode(dev)))
Alex Marginean7a910c12019-07-03 12:11:40 +0300103 sprintf(name, "enetc-%u", PCI_FUNC(pci_get_devfn(dev)));
104 else
105 sprintf(name, "enetc#%u", eth_num_devices++);
106 device_set_name(dev, name);
107
108 return 0;
109}
110
Alex Marginean38882ae2019-07-03 12:11:42 +0300111/* MDIO wrappers, we're using these to drive internal MDIO to get to serdes */
112static int enetc_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
113{
114 struct enetc_mdio_priv priv;
115
116 priv.regs_base = bus->priv;
117 return enetc_mdio_read_priv(&priv, addr, devad, reg);
118}
119
120static int enetc_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
121 u16 val)
122{
123 struct enetc_mdio_priv priv;
124
125 priv.regs_base = bus->priv;
126 return enetc_mdio_write_priv(&priv, addr, devad, reg, val);
127}
128
129/* only interfaces that can pin out through serdes have internal MDIO */
130static bool enetc_has_imdio(struct udevice *dev)
131{
132 struct enetc_priv *priv = dev_get_priv(dev);
133
134 return !!(priv->imdio.priv);
135}
136
137/* set up serdes for SGMII */
138static int enetc_init_sgmii(struct udevice *dev)
139{
140 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean41a7ac52019-07-15 11:48:47 +0300141 bool is2500 = false;
142 u16 reg;
Alex Marginean38882ae2019-07-03 12:11:42 +0300143
144 if (!enetc_has_imdio(dev))
145 return 0;
146
Vladimir Oltean6caef972021-09-18 15:32:35 +0300147 if (priv->if_type == PHY_INTERFACE_MODE_2500BASEX)
Alex Marginean41a7ac52019-07-15 11:48:47 +0300148 is2500 = true;
149
150 /*
151 * Set to SGMII mode, for 1Gbps enable AN, for 2.5Gbps set fixed speed.
152 * Although fixed speed is 1Gbps, we could be running at 2.5Gbps based
153 * on PLL configuration. Setting 1G for 2.5G here is counter intuitive
154 * but intentional.
155 */
156 reg = ENETC_PCS_IF_MODE_SGMII;
157 reg |= is2500 ? ENETC_PCS_IF_MODE_SPEED_1G : ENETC_PCS_IF_MODE_SGMII_AN;
Alex Marginean38882ae2019-07-03 12:11:42 +0300158 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300159 ENETC_PCS_IF_MODE, reg);
Alex Marginean38882ae2019-07-03 12:11:42 +0300160
161 /* Dev ability - SGMII */
162 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
163 ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SGMII);
164
165 /* Adjust link timer for SGMII */
166 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
167 ENETC_PCS_LINK_TIMER1, ENETC_PCS_LINK_TIMER1_VAL);
168 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
169 ENETC_PCS_LINK_TIMER2, ENETC_PCS_LINK_TIMER2_VAL);
170
Alex Marginean41a7ac52019-07-15 11:48:47 +0300171 reg = ENETC_PCS_CR_DEF_VAL;
172 reg |= is2500 ? ENETC_PCS_CR_RST : ENETC_PCS_CR_RESET_AN;
Alex Marginean38882ae2019-07-03 12:11:42 +0300173 /* restart PCS AN */
174 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300175 ENETC_PCS_CR, reg);
Alex Marginean38882ae2019-07-03 12:11:42 +0300176
177 return 0;
178}
179
180/* set up MAC for RGMII */
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300181static void enetc_init_rgmii(struct udevice *dev, struct phy_device *phydev)
Alex Marginean38882ae2019-07-03 12:11:42 +0300182{
183 struct enetc_priv *priv = dev_get_priv(dev);
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300184 u32 old_val, val;
Alex Marginean38882ae2019-07-03 12:11:42 +0300185
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300186 old_val = val = enetc_read_port(priv, ENETC_PM_IF_MODE);
Alex Marginean38882ae2019-07-03 12:11:42 +0300187
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300188 /* disable unreliable RGMII in-band signaling and force the MAC into
189 * the speed negotiated by the PHY.
190 */
191 val &= ~ENETC_PM_IF_MODE_AN_ENA;
192
193 if (phydev->speed == SPEED_1000) {
194 val &= ~ENETC_PM_IFM_SSP_MASK;
195 val |= ENETC_PM_IFM_SSP_1000;
196 } else if (phydev->speed == SPEED_100) {
197 val &= ~ENETC_PM_IFM_SSP_MASK;
198 val |= ENETC_PM_IFM_SSP_100;
199 } else if (phydev->speed == SPEED_10) {
200 val &= ~ENETC_PM_IFM_SSP_MASK;
201 val |= ENETC_PM_IFM_SSP_10;
202 }
203
204 if (phydev->duplex == DUPLEX_FULL)
205 val |= ENETC_PM_IFM_FULL_DPX;
206 else
207 val &= ~ENETC_PM_IFM_FULL_DPX;
208
209 if (val == old_val)
210 return;
211
212 enetc_write_port(priv, ENETC_PM_IF_MODE, val);
Alex Marginean38882ae2019-07-03 12:11:42 +0300213}
214
Alex Margineanafad2d02020-01-10 23:32:20 +0200215/* set up MAC configuration for the given interface type */
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300216static void enetc_setup_mac_iface(struct udevice *dev,
217 struct phy_device *phydev)
Alex Marginean38882ae2019-07-03 12:11:42 +0300218{
219 struct enetc_priv *priv = dev_get_priv(dev);
220 u32 if_mode;
221
Alex Margineanafad2d02020-01-10 23:32:20 +0200222 switch (priv->if_type) {
223 case PHY_INTERFACE_MODE_RGMII:
224 case PHY_INTERFACE_MODE_RGMII_ID:
225 case PHY_INTERFACE_MODE_RGMII_RXID:
226 case PHY_INTERFACE_MODE_RGMII_TXID:
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300227 enetc_init_rgmii(dev, phydev);
Alex Margineanafad2d02020-01-10 23:32:20 +0200228 break;
Alex Margineanafad2d02020-01-10 23:32:20 +0200229 case PHY_INTERFACE_MODE_USXGMII:
Vladimir Oltean6a6e4022021-09-18 15:32:34 +0300230 case PHY_INTERFACE_MODE_10GBASER:
Alex Margineanafad2d02020-01-10 23:32:20 +0200231 /* set ifmode to (US)XGMII */
232 if_mode = enetc_read_port(priv, ENETC_PM_IF_MODE);
233 if_mode &= ~ENETC_PM_IF_IFMODE_MASK;
234 enetc_write_port(priv, ENETC_PM_IF_MODE, if_mode);
235 break;
236 };
237}
238
239/* set up serdes for SXGMII */
240static int enetc_init_sxgmii(struct udevice *dev)
241{
242 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean38882ae2019-07-03 12:11:42 +0300243
244 if (!enetc_has_imdio(dev))
245 return 0;
246
247 /* Dev ability - SXGMII */
248 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
249 ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SXGMII);
250
251 /* Restart PCS AN */
252 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
253 ENETC_PCS_CR,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300254 ENETC_PCS_CR_RST | ENETC_PCS_CR_RESET_AN);
Alex Marginean38882ae2019-07-03 12:11:42 +0300255
256 return 0;
257}
258
259/* Apply protocol specific configuration to MAC, serdes as needed */
260static void enetc_start_pcs(struct udevice *dev)
261{
262 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean38882ae2019-07-03 12:11:42 +0300263
Alex Margineand4be7682019-11-25 17:57:27 +0200264 /* register internal MDIO for debug purposes */
Alex Marginean38882ae2019-07-03 12:11:42 +0300265 if (enetc_read_port(priv, ENETC_PCAPR0) & ENETC_PCAPRO_MDIO) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300266 priv->imdio.read = enetc_mdio_read;
267 priv->imdio.write = enetc_mdio_write;
268 priv->imdio.priv = priv->port_regs + ENETC_PM_IMDIO_BASE;
Vladimir Olteandcd21cc2021-09-27 14:21:48 +0300269 strlcpy(priv->imdio.name, dev->name, MDIO_NAME_LEN);
Alex Margineand4be7682019-11-25 17:57:27 +0200270 if (!miiphy_get_dev_by_name(priv->imdio.name))
271 mdio_register(&priv->imdio);
Alex Marginean38882ae2019-07-03 12:11:42 +0300272 }
273
Simon Glassa7ece582020-12-19 10:40:14 -0700274 if (!ofnode_valid(dev_ofnode(dev))) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300275 enetc_dbg(dev, "no enetc ofnode found, skipping PCS set-up\n");
276 return;
277 }
278
Marek Behúnbc194772022-04-07 00:33:01 +0200279 priv->if_type = dev_read_phy_mode(dev);
Marek Behún48631e42022-04-07 00:33:03 +0200280 if (priv->if_type == PHY_INTERFACE_MODE_NA) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300281 enetc_dbg(dev,
282 "phy-mode property not found, defaulting to SGMII\n");
Marek Behúnbc194772022-04-07 00:33:01 +0200283 priv->if_type = PHY_INTERFACE_MODE_SGMII;
284 }
Alex Marginean38882ae2019-07-03 12:11:42 +0300285
286 switch (priv->if_type) {
287 case PHY_INTERFACE_MODE_SGMII:
Vladimir Oltean6caef972021-09-18 15:32:35 +0300288 case PHY_INTERFACE_MODE_2500BASEX:
Alex Marginean38882ae2019-07-03 12:11:42 +0300289 enetc_init_sgmii(dev);
290 break;
Alex Margineaned0460c2019-11-14 18:28:38 +0200291 case PHY_INTERFACE_MODE_USXGMII:
Vladimir Oltean6a6e4022021-09-18 15:32:34 +0300292 case PHY_INTERFACE_MODE_10GBASER:
Alex Marginean38882ae2019-07-03 12:11:42 +0300293 enetc_init_sxgmii(dev);
294 break;
295 };
296}
297
Alex Marginean02155392019-07-03 12:11:41 +0300298/* Configure the actual/external ethernet PHY, if one is found */
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300299static int enetc_config_phy(struct udevice *dev)
Alex Marginean02155392019-07-03 12:11:41 +0300300{
301 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean02155392019-07-03 12:11:41 +0300302 int supported;
303
Alex Marginean602e00f2019-11-25 17:15:13 +0200304 priv->phy = dm_eth_phy_connect(dev);
Alex Marginean602e00f2019-11-25 17:15:13 +0200305 if (!priv->phy)
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300306 return -ENODEV;
Alex Marginean02155392019-07-03 12:11:41 +0300307
Alex Margineanb93375c2019-11-14 18:58:45 +0200308 supported = PHY_GBIT_FEATURES | SUPPORTED_2500baseX_Full;
309 priv->phy->supported &= supported;
310 priv->phy->advertising &= supported;
Alex Marginean602e00f2019-11-25 17:15:13 +0200311
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300312 return phy_config(priv->phy);
Alex Marginean02155392019-07-03 12:11:41 +0300313}
314
Alex Marginean7a910c12019-07-03 12:11:40 +0300315/*
316 * Probe ENETC driver:
317 * - initialize port and station interface BARs
318 */
319static int enetc_probe(struct udevice *dev)
320{
321 struct enetc_priv *priv = dev_get_priv(dev);
322
Simon Glassa7ece582020-12-19 10:40:14 -0700323 if (ofnode_valid(dev_ofnode(dev)) && !ofnode_is_available(dev_ofnode(dev))) {
Alex Marginean7a910c12019-07-03 12:11:40 +0300324 enetc_dbg(dev, "interface disabled\n");
325 return -ENODEV;
326 }
327
328 priv->enetc_txbd = memalign(ENETC_BD_ALIGN,
329 sizeof(struct enetc_tx_bd) * ENETC_BD_CNT);
330 priv->enetc_rxbd = memalign(ENETC_BD_ALIGN,
331 sizeof(union enetc_rx_bd) * ENETC_BD_CNT);
332
333 if (!priv->enetc_txbd || !priv->enetc_rxbd) {
334 /* free should be able to handle NULL, just free all pointers */
335 free(priv->enetc_txbd);
336 free(priv->enetc_rxbd);
337
338 return -ENOMEM;
339 }
340
341 /* initialize register */
342 priv->regs_base = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0, 0);
343 if (!priv->regs_base) {
344 enetc_dbg(dev, "failed to map BAR0\n");
345 return -EINVAL;
346 }
347 priv->port_regs = priv->regs_base + ENETC_PORT_REGS_OFF;
348
349 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
350
Alex Margineanc905c212019-11-14 18:58:46 +0200351 enetc_start_pcs(dev);
Alex Margineanc905c212019-11-14 18:58:46 +0200352
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300353 return enetc_config_phy(dev);
Alex Marginean7a910c12019-07-03 12:11:40 +0300354}
355
356/*
357 * Remove the driver from an interface:
358 * - free up allocated memory
359 */
360static int enetc_remove(struct udevice *dev)
361{
362 struct enetc_priv *priv = dev_get_priv(dev);
363
364 free(priv->enetc_txbd);
365 free(priv->enetc_rxbd);
366
367 return 0;
368}
369
Michael Walle1d3e24f2019-12-20 14:16:48 +0100370/*
371 * LS1028A is the only part with IERB at this time and there are plans to
372 * change its structure, keep this LS1028A specific for now.
373 */
374#define LS1028A_IERB_BASE 0x1f0800000ULL
375#define LS1028A_IERB_PSIPMAR0(pf, vf) (LS1028A_IERB_BASE + 0x8000 \
376 + (pf) * 0x100 + (vf) * 8)
377#define LS1028A_IERB_PSIPMAR1(pf, vf) (LS1028A_IERB_PSIPMAR0(pf, vf) + 4)
378
379static int enetc_ls1028a_write_hwaddr(struct udevice *dev)
380{
Simon Glassb75b15b2020-12-03 16:55:23 -0700381 struct pci_child_plat *ppdata = dev_get_parent_plat(dev);
Michael Walle1d3e24f2019-12-20 14:16:48 +0100382 const int devfn_to_pf[] = {0, 1, 2, -1, -1, -1, 3};
Simon Glassfa20e932020-12-03 16:55:20 -0700383 struct eth_pdata *plat = dev_get_plat(dev);
Michael Walle1d3e24f2019-12-20 14:16:48 +0100384 int devfn = PCI_FUNC(ppdata->devfn);
385 u8 *addr = plat->enetaddr;
386 u32 lower, upper;
387 int pf;
388
389 if (devfn >= ARRAY_SIZE(devfn_to_pf))
390 return 0;
391
392 pf = devfn_to_pf[devfn];
393 if (pf < 0)
394 return 0;
395
396 lower = *(const u16 *)(addr + 4);
397 upper = *(const u32 *)addr;
398
399 out_le32(LS1028A_IERB_PSIPMAR0(pf, 0), upper);
400 out_le32(LS1028A_IERB_PSIPMAR1(pf, 0), lower);
401
402 return 0;
403}
404
Michael Walle8c7188e2019-12-20 14:16:47 +0100405static int enetc_write_hwaddr(struct udevice *dev)
Alex Marginean7a910c12019-07-03 12:11:40 +0300406{
Simon Glassfa20e932020-12-03 16:55:20 -0700407 struct eth_pdata *plat = dev_get_plat(dev);
Michael Walle8c7188e2019-12-20 14:16:47 +0100408 struct enetc_priv *priv = dev_get_priv(dev);
409 u8 *addr = plat->enetaddr;
410
Michael Walle1d3e24f2019-12-20 14:16:48 +0100411 if (IS_ENABLED(CONFIG_ARCH_LS1028A))
412 return enetc_ls1028a_write_hwaddr(dev);
413
Alex Marginean7a910c12019-07-03 12:11:40 +0300414 u16 lower = *(const u16 *)(addr + 4);
415 u32 upper = *(const u32 *)addr;
416
417 enetc_write_port(priv, ENETC_PSIPMAR0, upper);
418 enetc_write_port(priv, ENETC_PSIPMAR1, lower);
Michael Walle8c7188e2019-12-20 14:16:47 +0100419
420 return 0;
Alex Marginean7a910c12019-07-03 12:11:40 +0300421}
422
423/* Configure port parameters (# of rings, frame size, enable port) */
424static void enetc_enable_si_port(struct enetc_priv *priv)
425{
426 u32 val;
427
428 /* set Rx/Tx BDR count */
429 val = ENETC_PSICFGR_SET_TXBDR(ENETC_TX_BDR_CNT);
430 val |= ENETC_PSICFGR_SET_RXBDR(ENETC_RX_BDR_CNT);
431 enetc_write_port(priv, ENETC_PSICFGR(0), val);
432 /* set Rx max frame size */
433 enetc_write_port(priv, ENETC_PM_MAXFRM, ENETC_RX_MAXFRM_SIZE);
434 /* enable MAC port */
435 enetc_write_port(priv, ENETC_PM_CC, ENETC_PM_CC_RX_TX_EN);
436 /* enable port */
437 enetc_write_port(priv, ENETC_PMR, ENETC_PMR_SI0_EN);
438 /* set SI cache policy */
439 enetc_write(priv, ENETC_SICAR0,
440 ENETC_SICAR_RD_CFG | ENETC_SICAR_WR_CFG);
441 /* enable SI */
442 enetc_write(priv, ENETC_SIMR, ENETC_SIMR_EN);
443}
444
445/* returns DMA address for a given buffer index */
446static inline u64 enetc_rxb_address(struct udevice *dev, int i)
447{
448 return cpu_to_le64(dm_pci_virt_to_mem(dev, net_rx_packets[i]));
449}
450
451/*
452 * Setup a single Tx BD Ring (ID = 0):
453 * - set Tx buffer descriptor address
454 * - set the BD count
455 * - initialize the producer and consumer index
456 */
457static void enetc_setup_tx_bdr(struct udevice *dev)
458{
459 struct enetc_priv *priv = dev_get_priv(dev);
460 struct bd_ring *tx_bdr = &priv->tx_bdr;
461 u64 tx_bd_add = (u64)priv->enetc_txbd;
462
463 /* used later to advance to the next Tx BD */
464 tx_bdr->bd_count = ENETC_BD_CNT;
465 tx_bdr->next_prod_idx = 0;
466 tx_bdr->next_cons_idx = 0;
467 tx_bdr->cons_idx = priv->regs_base +
468 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBCIR);
469 tx_bdr->prod_idx = priv->regs_base +
470 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBPIR);
471
472 /* set Tx BD address */
473 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR0,
474 lower_32_bits(tx_bd_add));
475 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR1,
476 upper_32_bits(tx_bd_add));
477 /* set Tx 8 BD count */
478 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBLENR,
479 tx_bdr->bd_count);
480
481 /* reset both producer/consumer indexes */
482 enetc_write_reg(tx_bdr->cons_idx, tx_bdr->next_cons_idx);
483 enetc_write_reg(tx_bdr->prod_idx, tx_bdr->next_prod_idx);
484
485 /* enable TX ring */
486 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBMR, ENETC_TBMR_EN);
487}
488
489/*
490 * Setup a single Rx BD Ring (ID = 0):
491 * - set Rx buffer descriptors address (one descriptor per buffer)
492 * - set buffer size as max frame size
493 * - enable Rx ring
494 * - reset consumer and producer indexes
495 * - set buffer for each descriptor
496 */
497static void enetc_setup_rx_bdr(struct udevice *dev)
498{
499 struct enetc_priv *priv = dev_get_priv(dev);
500 struct bd_ring *rx_bdr = &priv->rx_bdr;
501 u64 rx_bd_add = (u64)priv->enetc_rxbd;
502 int i;
503
504 /* used later to advance to the next BD produced by ENETC HW */
505 rx_bdr->bd_count = ENETC_BD_CNT;
506 rx_bdr->next_prod_idx = 0;
507 rx_bdr->next_cons_idx = 0;
508 rx_bdr->cons_idx = priv->regs_base +
509 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBCIR);
510 rx_bdr->prod_idx = priv->regs_base +
511 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBPIR);
512
513 /* set Rx BD address */
514 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR0,
515 lower_32_bits(rx_bd_add));
516 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR1,
517 upper_32_bits(rx_bd_add));
518 /* set Rx BD count (multiple of 8) */
519 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBLENR,
520 rx_bdr->bd_count);
521 /* set Rx buffer size */
522 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBSR, PKTSIZE_ALIGN);
523
524 /* fill Rx BD */
525 memset(priv->enetc_rxbd, 0,
526 rx_bdr->bd_count * sizeof(union enetc_rx_bd));
527 for (i = 0; i < rx_bdr->bd_count; i++) {
528 priv->enetc_rxbd[i].w.addr = enetc_rxb_address(dev, i);
529 /* each RX buffer must be aligned to 64B */
530 WARN_ON(priv->enetc_rxbd[i].w.addr & (ARCH_DMA_MINALIGN - 1));
531 }
532
533 /* reset producer (ENETC owned) and consumer (SW owned) index */
534 enetc_write_reg(rx_bdr->cons_idx, rx_bdr->next_cons_idx);
535 enetc_write_reg(rx_bdr->prod_idx, rx_bdr->next_prod_idx);
536
537 /* enable Rx ring */
538 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBMR, ENETC_RBMR_EN);
539}
540
541/*
542 * Start ENETC interface:
543 * - perform FLR
544 * - enable access to port and SI registers
545 * - set mac address
546 * - setup TX/RX buffer descriptors
547 * - enable Tx/Rx rings
548 */
549static int enetc_start(struct udevice *dev)
550{
Alex Marginean7a910c12019-07-03 12:11:40 +0300551 struct enetc_priv *priv = dev_get_priv(dev);
552
553 /* reset and enable the PCI device */
554 dm_pci_flr(dev);
555 dm_pci_clrset_config16(dev, PCI_COMMAND, 0,
556 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
557
Alex Marginean7a910c12019-07-03 12:11:40 +0300558 enetc_enable_si_port(priv);
559
560 /* setup Tx/Rx buffer descriptors */
561 enetc_setup_tx_bdr(dev);
562 enetc_setup_rx_bdr(dev);
563
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300564 enetc_setup_mac_iface(dev, priv->phy);
565
Vladimir Oltean19363082021-06-29 20:53:17 +0300566 return phy_startup(priv->phy);
Alex Marginean7a910c12019-07-03 12:11:40 +0300567}
568
569/*
570 * Stop the network interface:
571 * - just quiesce it, we can wipe all configuration as _start starts from
572 * scratch each time
573 */
574static void enetc_stop(struct udevice *dev)
575{
576 /* FLR is sufficient to quiesce the device */
577 dm_pci_flr(dev);
Alex Margineand4be7682019-11-25 17:57:27 +0200578 /* leave the BARs accessible after we stop, this is needed to use
579 * internal MDIO in command line.
580 */
581 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
Alex Marginean7a910c12019-07-03 12:11:40 +0300582}
583
584/*
585 * ENETC transmit packet:
586 * - check if Tx BD ring is full
587 * - set buffer/packet address (dma address)
588 * - set final fragment flag
589 * - try while producer index equals consumer index or timeout
590 */
591static int enetc_send(struct udevice *dev, void *packet, int length)
592{
593 struct enetc_priv *priv = dev_get_priv(dev);
594 struct bd_ring *txr = &priv->tx_bdr;
595 void *nv_packet = (void *)packet;
596 int tries = ENETC_POLL_TRIES;
597 u32 pi, ci;
598
599 pi = txr->next_prod_idx;
600 ci = enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK;
601 /* Tx ring is full when */
602 if (((pi + 1) % txr->bd_count) == ci) {
603 enetc_dbg(dev, "Tx BDR full\n");
604 return -ETIMEDOUT;
605 }
606 enetc_dbg(dev, "TxBD[%d]send: pkt_len=%d, buff @0x%x%08x\n", pi, length,
607 upper_32_bits((u64)nv_packet), lower_32_bits((u64)nv_packet));
608
609 /* prepare Tx BD */
610 memset(&priv->enetc_txbd[pi], 0x0, sizeof(struct enetc_tx_bd));
611 priv->enetc_txbd[pi].addr =
612 cpu_to_le64(dm_pci_virt_to_mem(dev, nv_packet));
613 priv->enetc_txbd[pi].buf_len = cpu_to_le16(length);
614 priv->enetc_txbd[pi].frm_len = cpu_to_le16(length);
615 priv->enetc_txbd[pi].flags = cpu_to_le16(ENETC_TXBD_FLAGS_F);
616 dmb();
617 /* send frame: increment producer index */
618 pi = (pi + 1) % txr->bd_count;
619 txr->next_prod_idx = pi;
620 enetc_write_reg(txr->prod_idx, pi);
621 while ((--tries >= 0) &&
622 (pi != (enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK)))
623 udelay(10);
624
625 return tries > 0 ? 0 : -ETIMEDOUT;
626}
627
628/*
629 * Receive frame:
630 * - wait for the next BD to get ready bit set
631 * - clean up the descriptor
632 * - move on and indicate to HW that the cleaned BD is available for Rx
633 */
634static int enetc_recv(struct udevice *dev, int flags, uchar **packetp)
635{
636 struct enetc_priv *priv = dev_get_priv(dev);
637 struct bd_ring *rxr = &priv->rx_bdr;
638 int tries = ENETC_POLL_TRIES;
639 int pi = rxr->next_prod_idx;
640 int ci = rxr->next_cons_idx;
641 u32 status;
642 int len;
643 u8 rdy;
644
645 do {
646 dmb();
647 status = le32_to_cpu(priv->enetc_rxbd[pi].r.lstatus);
648 /* check if current BD is ready to be consumed */
649 rdy = ENETC_RXBD_STATUS_R(status);
650 } while (--tries >= 0 && !rdy);
651
652 if (!rdy)
653 return -EAGAIN;
654
655 dmb();
656 len = le16_to_cpu(priv->enetc_rxbd[pi].r.buf_len);
657 *packetp = (uchar *)enetc_rxb_address(dev, pi);
658 enetc_dbg(dev, "RxBD[%d]: len=%d err=%d pkt=0x%x%08x\n", pi, len,
659 ENETC_RXBD_STATUS_ERRORS(status),
660 upper_32_bits((u64)*packetp), lower_32_bits((u64)*packetp));
661
662 /* BD clean up and advance to next in ring */
663 memset(&priv->enetc_rxbd[pi], 0, sizeof(union enetc_rx_bd));
664 priv->enetc_rxbd[pi].w.addr = enetc_rxb_address(dev, pi);
665 rxr->next_prod_idx = (pi + 1) % rxr->bd_count;
666 ci = (ci + 1) % rxr->bd_count;
667 rxr->next_cons_idx = ci;
668 dmb();
669 /* free up the slot in the ring for HW */
670 enetc_write_reg(rxr->cons_idx, ci);
671
672 return len;
673}
674
675static const struct eth_ops enetc_ops = {
676 .start = enetc_start,
677 .send = enetc_send,
678 .recv = enetc_recv,
679 .stop = enetc_stop,
Michael Walle8c7188e2019-12-20 14:16:47 +0100680 .write_hwaddr = enetc_write_hwaddr,
Alex Marginean7a910c12019-07-03 12:11:40 +0300681};
682
683U_BOOT_DRIVER(eth_enetc) = {
Alex Marginean805b8592019-12-10 16:55:39 +0200684 .name = ENETC_DRIVER_NAME,
Alex Marginean7a910c12019-07-03 12:11:40 +0300685 .id = UCLASS_ETH,
686 .bind = enetc_bind,
687 .probe = enetc_probe,
688 .remove = enetc_remove,
689 .ops = &enetc_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700690 .priv_auto = sizeof(struct enetc_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700691 .plat_auto = sizeof(struct eth_pdata),
Alex Marginean7a910c12019-07-03 12:11:40 +0300692};
693
694static struct pci_device_id enetc_ids[] = {
695 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, PCI_DEVICE_ID_ENETC_ETH) },
696 {}
697};
698
699U_BOOT_PCI_DEVICE(eth_enetc, enetc_ids);