blob: a6b0bafc8c60dea8f343ebcf08e93e27db0da083 [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
Alex Marginean805b8592019-12-10 16:55:39 +020026/*
27 * sets the MAC address in IERB registers, this setting is persistent and
28 * carried over to Linux.
29 */
30static void enetc_set_ierb_primary_mac(struct udevice *dev, int devfn,
31 const u8 *enetaddr)
32{
33#ifdef CONFIG_ARCH_LS1028A
34/*
35 * LS1028A is the only part with IERB at this time and there are plans to change
36 * its structure, keep this LS1028A specific for now
37 */
38#define IERB_BASE 0x1f0800000ULL
39#define IERB_PFMAC(pf, vf, n) (IERB_BASE + 0x8000 + (pf) * 0x100 + (vf) * 8 \
40 + (n) * 4)
41
42static int ierb_fn_to_pf[] = {0, 1, 2, -1, -1, -1, 3};
43
44 u16 lower = *(const u16 *)(enetaddr + 4);
45 u32 upper = *(const u32 *)enetaddr;
46
47 if (ierb_fn_to_pf[devfn] < 0)
48 return;
49
50 out_le32(IERB_PFMAC(ierb_fn_to_pf[devfn], 0, 0), upper);
51 out_le32(IERB_PFMAC(ierb_fn_to_pf[devfn], 0, 1), (u32)lower);
52#endif
53}
54
55/* sets up primary MAC addresses in DT/IERB */
56void fdt_fixup_enetc_mac(void *blob)
57{
Simon Glassb75b15b2020-12-03 16:55:23 -070058 struct pci_child_plat *ppdata;
Alex Marginean805b8592019-12-10 16:55:39 +020059 struct eth_pdata *pdata;
60 struct udevice *dev;
61 struct uclass *uc;
62 char path[256];
63 int offset;
64 int devfn;
65
66 uclass_get(UCLASS_ETH, &uc);
67 uclass_foreach_dev(dev, uc) {
68 if (!dev->driver || !dev->driver->name ||
69 strcmp(dev->driver->name, ENETC_DRIVER_NAME))
70 continue;
71
Simon Glassfa20e932020-12-03 16:55:20 -070072 pdata = dev_get_plat(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -070073 ppdata = dev_get_parent_plat(dev);
Alex Marginean805b8592019-12-10 16:55:39 +020074 devfn = PCI_FUNC(ppdata->devfn);
75
76 enetc_set_ierb_primary_mac(dev, devfn, pdata->enetaddr);
77
78 snprintf(path, 256, "/soc/pcie@1f0000000/ethernet@%x,%x",
79 PCI_DEV(ppdata->devfn), PCI_FUNC(ppdata->devfn));
80 offset = fdt_path_offset(blob, path);
81 if (offset < 0)
82 continue;
83 fdt_setprop(blob, offset, "mac-address", pdata->enetaddr, 6);
84 }
85}
86
Alex Marginean7a910c12019-07-03 12:11:40 +030087/*
88 * Bind the device:
89 * - set a more explicit name on the interface
90 */
91static int enetc_bind(struct udevice *dev)
92{
93 char name[16];
94 static int eth_num_devices;
95
96 /*
97 * prefer using PCI function numbers to number interfaces, but these
98 * are only available if dts nodes are present. For PCI they are
99 * optional, handle that case too. Just in case some nodes are present
100 * and some are not, use different naming scheme - enetc-N based on
101 * PCI function # and enetc#N based on interface count
102 */
Simon Glassa7ece582020-12-19 10:40:14 -0700103 if (ofnode_valid(dev_ofnode(dev)))
Alex Marginean7a910c12019-07-03 12:11:40 +0300104 sprintf(name, "enetc-%u", PCI_FUNC(pci_get_devfn(dev)));
105 else
106 sprintf(name, "enetc#%u", eth_num_devices++);
107 device_set_name(dev, name);
108
109 return 0;
110}
111
Alex Marginean38882ae2019-07-03 12:11:42 +0300112/* MDIO wrappers, we're using these to drive internal MDIO to get to serdes */
113static int enetc_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
114{
115 struct enetc_mdio_priv priv;
116
117 priv.regs_base = bus->priv;
118 return enetc_mdio_read_priv(&priv, addr, devad, reg);
119}
120
121static int enetc_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
122 u16 val)
123{
124 struct enetc_mdio_priv priv;
125
126 priv.regs_base = bus->priv;
127 return enetc_mdio_write_priv(&priv, addr, devad, reg, val);
128}
129
130/* only interfaces that can pin out through serdes have internal MDIO */
131static bool enetc_has_imdio(struct udevice *dev)
132{
133 struct enetc_priv *priv = dev_get_priv(dev);
134
135 return !!(priv->imdio.priv);
136}
137
138/* set up serdes for SGMII */
139static int enetc_init_sgmii(struct udevice *dev)
140{
141 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean41a7ac52019-07-15 11:48:47 +0300142 bool is2500 = false;
143 u16 reg;
Alex Marginean38882ae2019-07-03 12:11:42 +0300144
145 if (!enetc_has_imdio(dev))
146 return 0;
147
Simon Glassfada3f92022-09-17 09:00:09 -0600148 if (priv->uclass_id == PHY_INTERFACE_MODE_2500BASEX)
Alex Marginean41a7ac52019-07-15 11:48:47 +0300149 is2500 = true;
150
151 /*
152 * Set to SGMII mode, for 1Gbps enable AN, for 2.5Gbps set fixed speed.
153 * Although fixed speed is 1Gbps, we could be running at 2.5Gbps based
154 * on PLL configuration. Setting 1G for 2.5G here is counter intuitive
155 * but intentional.
156 */
157 reg = ENETC_PCS_IF_MODE_SGMII;
158 reg |= is2500 ? ENETC_PCS_IF_MODE_SPEED_1G : ENETC_PCS_IF_MODE_SGMII_AN;
Alex Marginean38882ae2019-07-03 12:11:42 +0300159 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300160 ENETC_PCS_IF_MODE, reg);
Alex Marginean38882ae2019-07-03 12:11:42 +0300161
162 /* Dev ability - SGMII */
163 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
164 ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SGMII);
165
166 /* Adjust link timer for SGMII */
167 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
168 ENETC_PCS_LINK_TIMER1, ENETC_PCS_LINK_TIMER1_VAL);
169 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
170 ENETC_PCS_LINK_TIMER2, ENETC_PCS_LINK_TIMER2_VAL);
171
Alex Marginean41a7ac52019-07-15 11:48:47 +0300172 reg = ENETC_PCS_CR_DEF_VAL;
173 reg |= is2500 ? ENETC_PCS_CR_RST : ENETC_PCS_CR_RESET_AN;
Alex Marginean38882ae2019-07-03 12:11:42 +0300174 /* restart PCS AN */
175 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300176 ENETC_PCS_CR, reg);
Alex Marginean38882ae2019-07-03 12:11:42 +0300177
178 return 0;
179}
180
181/* set up MAC for RGMII */
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300182static void enetc_init_rgmii(struct udevice *dev, struct phy_device *phydev)
Alex Marginean38882ae2019-07-03 12:11:42 +0300183{
184 struct enetc_priv *priv = dev_get_priv(dev);
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300185 u32 old_val, val;
Alex Marginean38882ae2019-07-03 12:11:42 +0300186
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300187 old_val = val = enetc_read_port(priv, ENETC_PM_IF_MODE);
Alex Marginean38882ae2019-07-03 12:11:42 +0300188
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300189 /* disable unreliable RGMII in-band signaling and force the MAC into
190 * the speed negotiated by the PHY.
191 */
192 val &= ~ENETC_PM_IF_MODE_AN_ENA;
193
194 if (phydev->speed == SPEED_1000) {
195 val &= ~ENETC_PM_IFM_SSP_MASK;
196 val |= ENETC_PM_IFM_SSP_1000;
197 } else if (phydev->speed == SPEED_100) {
198 val &= ~ENETC_PM_IFM_SSP_MASK;
199 val |= ENETC_PM_IFM_SSP_100;
200 } else if (phydev->speed == SPEED_10) {
201 val &= ~ENETC_PM_IFM_SSP_MASK;
202 val |= ENETC_PM_IFM_SSP_10;
203 }
204
205 if (phydev->duplex == DUPLEX_FULL)
206 val |= ENETC_PM_IFM_FULL_DPX;
207 else
208 val &= ~ENETC_PM_IFM_FULL_DPX;
209
210 if (val == old_val)
211 return;
212
213 enetc_write_port(priv, ENETC_PM_IF_MODE, val);
Alex Marginean38882ae2019-07-03 12:11:42 +0300214}
215
Alex Margineanafad2d02020-01-10 23:32:20 +0200216/* set up MAC configuration for the given interface type */
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300217static void enetc_setup_mac_iface(struct udevice *dev,
218 struct phy_device *phydev)
Alex Marginean38882ae2019-07-03 12:11:42 +0300219{
220 struct enetc_priv *priv = dev_get_priv(dev);
221 u32 if_mode;
222
Simon Glassfada3f92022-09-17 09:00:09 -0600223 switch (priv->uclass_id) {
Alex Margineanafad2d02020-01-10 23:32:20 +0200224 case PHY_INTERFACE_MODE_RGMII:
225 case PHY_INTERFACE_MODE_RGMII_ID:
226 case PHY_INTERFACE_MODE_RGMII_RXID:
227 case PHY_INTERFACE_MODE_RGMII_TXID:
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300228 enetc_init_rgmii(dev, phydev);
Alex Margineanafad2d02020-01-10 23:32:20 +0200229 break;
Alex Margineanafad2d02020-01-10 23:32:20 +0200230 case PHY_INTERFACE_MODE_USXGMII:
Vladimir Oltean6a6e4022021-09-18 15:32:34 +0300231 case PHY_INTERFACE_MODE_10GBASER:
Alex Margineanafad2d02020-01-10 23:32:20 +0200232 /* set ifmode to (US)XGMII */
233 if_mode = enetc_read_port(priv, ENETC_PM_IF_MODE);
234 if_mode &= ~ENETC_PM_IF_IFMODE_MASK;
235 enetc_write_port(priv, ENETC_PM_IF_MODE, if_mode);
236 break;
237 };
238}
239
240/* set up serdes for SXGMII */
241static int enetc_init_sxgmii(struct udevice *dev)
242{
243 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean38882ae2019-07-03 12:11:42 +0300244
245 if (!enetc_has_imdio(dev))
246 return 0;
247
248 /* Dev ability - SXGMII */
249 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
250 ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SXGMII);
251
252 /* Restart PCS AN */
253 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
254 ENETC_PCS_CR,
Alex Marginean41a7ac52019-07-15 11:48:47 +0300255 ENETC_PCS_CR_RST | ENETC_PCS_CR_RESET_AN);
Alex Marginean38882ae2019-07-03 12:11:42 +0300256
257 return 0;
258}
259
260/* Apply protocol specific configuration to MAC, serdes as needed */
261static void enetc_start_pcs(struct udevice *dev)
262{
263 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean38882ae2019-07-03 12:11:42 +0300264
Alex Margineand4be7682019-11-25 17:57:27 +0200265 /* register internal MDIO for debug purposes */
Alex Marginean38882ae2019-07-03 12:11:42 +0300266 if (enetc_read_port(priv, ENETC_PCAPR0) & ENETC_PCAPRO_MDIO) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300267 priv->imdio.read = enetc_mdio_read;
268 priv->imdio.write = enetc_mdio_write;
269 priv->imdio.priv = priv->port_regs + ENETC_PM_IMDIO_BASE;
Vladimir Olteandcd21cc2021-09-27 14:21:48 +0300270 strlcpy(priv->imdio.name, dev->name, MDIO_NAME_LEN);
Alex Margineand4be7682019-11-25 17:57:27 +0200271 if (!miiphy_get_dev_by_name(priv->imdio.name))
272 mdio_register(&priv->imdio);
Alex Marginean38882ae2019-07-03 12:11:42 +0300273 }
274
Simon Glassa7ece582020-12-19 10:40:14 -0700275 if (!ofnode_valid(dev_ofnode(dev))) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300276 enetc_dbg(dev, "no enetc ofnode found, skipping PCS set-up\n");
277 return;
278 }
279
Simon Glassfada3f92022-09-17 09:00:09 -0600280 priv->uclass_id = dev_read_phy_mode(dev);
281 if (priv->uclass_id == PHY_INTERFACE_MODE_NA) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300282 enetc_dbg(dev,
283 "phy-mode property not found, defaulting to SGMII\n");
Simon Glassfada3f92022-09-17 09:00:09 -0600284 priv->uclass_id = PHY_INTERFACE_MODE_SGMII;
Marek Behúnbc194772022-04-07 00:33:01 +0200285 }
Alex Marginean38882ae2019-07-03 12:11:42 +0300286
Simon Glassfada3f92022-09-17 09:00:09 -0600287 switch (priv->uclass_id) {
Alex Marginean38882ae2019-07-03 12:11:42 +0300288 case PHY_INTERFACE_MODE_SGMII:
Vladimir Oltean6caef972021-09-18 15:32:35 +0300289 case PHY_INTERFACE_MODE_2500BASEX:
Alex Marginean38882ae2019-07-03 12:11:42 +0300290 enetc_init_sgmii(dev);
291 break;
Alex Margineaned0460c2019-11-14 18:28:38 +0200292 case PHY_INTERFACE_MODE_USXGMII:
Vladimir Oltean6a6e4022021-09-18 15:32:34 +0300293 case PHY_INTERFACE_MODE_10GBASER:
Alex Marginean38882ae2019-07-03 12:11:42 +0300294 enetc_init_sxgmii(dev);
295 break;
296 };
297}
298
Alex Marginean02155392019-07-03 12:11:41 +0300299/* Configure the actual/external ethernet PHY, if one is found */
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300300static int enetc_config_phy(struct udevice *dev)
Alex Marginean02155392019-07-03 12:11:41 +0300301{
302 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean02155392019-07-03 12:11:41 +0300303 int supported;
304
Alex Marginean602e00f2019-11-25 17:15:13 +0200305 priv->phy = dm_eth_phy_connect(dev);
Alex Marginean602e00f2019-11-25 17:15:13 +0200306 if (!priv->phy)
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300307 return -ENODEV;
Alex Marginean02155392019-07-03 12:11:41 +0300308
Alex Margineanb93375c2019-11-14 18:58:45 +0200309 supported = PHY_GBIT_FEATURES | SUPPORTED_2500baseX_Full;
310 priv->phy->supported &= supported;
311 priv->phy->advertising &= supported;
Alex Marginean602e00f2019-11-25 17:15:13 +0200312
Vladimir Oltean10c6fe42021-06-29 20:53:15 +0300313 return phy_config(priv->phy);
Alex Marginean02155392019-07-03 12:11:41 +0300314}
315
Alex Marginean7a910c12019-07-03 12:11:40 +0300316/*
317 * Probe ENETC driver:
318 * - initialize port and station interface BARs
319 */
320static int enetc_probe(struct udevice *dev)
321{
322 struct enetc_priv *priv = dev_get_priv(dev);
Siarhei Yasinski25b798e2022-08-31 10:57:37 +0000323 int res;
Alex Marginean7a910c12019-07-03 12:11:40 +0300324
Simon Glass2e4938b2022-09-06 20:27:17 -0600325 if (ofnode_valid(dev_ofnode(dev)) && !ofnode_is_enabled(dev_ofnode(dev))) {
Alex Marginean7a910c12019-07-03 12:11:40 +0300326 enetc_dbg(dev, "interface disabled\n");
327 return -ENODEV;
328 }
329
330 priv->enetc_txbd = memalign(ENETC_BD_ALIGN,
331 sizeof(struct enetc_tx_bd) * ENETC_BD_CNT);
332 priv->enetc_rxbd = memalign(ENETC_BD_ALIGN,
333 sizeof(union enetc_rx_bd) * ENETC_BD_CNT);
334
335 if (!priv->enetc_txbd || !priv->enetc_rxbd) {
336 /* free should be able to handle NULL, just free all pointers */
337 free(priv->enetc_txbd);
338 free(priv->enetc_rxbd);
339
340 return -ENOMEM;
341 }
342
343 /* initialize register */
Andrew Scull6520c822022-04-21 16:11:13 +0000344 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 +0300345 if (!priv->regs_base) {
346 enetc_dbg(dev, "failed to map BAR0\n");
347 return -EINVAL;
348 }
349 priv->port_regs = priv->regs_base + ENETC_PORT_REGS_OFF;
350
351 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
352
Alex Margineanc905c212019-11-14 18:58:46 +0200353 enetc_start_pcs(dev);
Alex Margineanc905c212019-11-14 18:58:46 +0200354
Siarhei Yasinski25b798e2022-08-31 10:57:37 +0000355 res = enetc_config_phy(dev);
356 if(res)
357 enetc_remove(dev);
358 return res;
Alex Marginean7a910c12019-07-03 12:11:40 +0300359}
360
361/*
362 * Remove the driver from an interface:
363 * - free up allocated memory
364 */
365static int enetc_remove(struct udevice *dev)
366{
367 struct enetc_priv *priv = dev_get_priv(dev);
368
Michael Walle3f66e8e2022-05-31 18:36:16 +0200369 if (miiphy_get_dev_by_name(priv->imdio.name))
370 mdio_unregister(&priv->imdio);
371
Alex Marginean7a910c12019-07-03 12:11:40 +0300372 free(priv->enetc_txbd);
373 free(priv->enetc_rxbd);
374
375 return 0;
376}
377
Michael Walle1d3e24f2019-12-20 14:16:48 +0100378/*
379 * LS1028A is the only part with IERB at this time and there are plans to
380 * change its structure, keep this LS1028A specific for now.
381 */
382#define LS1028A_IERB_BASE 0x1f0800000ULL
383#define LS1028A_IERB_PSIPMAR0(pf, vf) (LS1028A_IERB_BASE + 0x8000 \
384 + (pf) * 0x100 + (vf) * 8)
385#define LS1028A_IERB_PSIPMAR1(pf, vf) (LS1028A_IERB_PSIPMAR0(pf, vf) + 4)
386
387static int enetc_ls1028a_write_hwaddr(struct udevice *dev)
388{
Simon Glassb75b15b2020-12-03 16:55:23 -0700389 struct pci_child_plat *ppdata = dev_get_parent_plat(dev);
Michael Walle1d3e24f2019-12-20 14:16:48 +0100390 const int devfn_to_pf[] = {0, 1, 2, -1, -1, -1, 3};
Simon Glassfa20e932020-12-03 16:55:20 -0700391 struct eth_pdata *plat = dev_get_plat(dev);
Michael Walle1d3e24f2019-12-20 14:16:48 +0100392 int devfn = PCI_FUNC(ppdata->devfn);
393 u8 *addr = plat->enetaddr;
394 u32 lower, upper;
395 int pf;
396
397 if (devfn >= ARRAY_SIZE(devfn_to_pf))
398 return 0;
399
400 pf = devfn_to_pf[devfn];
401 if (pf < 0)
402 return 0;
403
404 lower = *(const u16 *)(addr + 4);
405 upper = *(const u32 *)addr;
406
407 out_le32(LS1028A_IERB_PSIPMAR0(pf, 0), upper);
408 out_le32(LS1028A_IERB_PSIPMAR1(pf, 0), lower);
409
410 return 0;
411}
412
Michael Walle8c7188e2019-12-20 14:16:47 +0100413static int enetc_write_hwaddr(struct udevice *dev)
Alex Marginean7a910c12019-07-03 12:11:40 +0300414{
Simon Glassfa20e932020-12-03 16:55:20 -0700415 struct eth_pdata *plat = dev_get_plat(dev);
Michael Walle8c7188e2019-12-20 14:16:47 +0100416 struct enetc_priv *priv = dev_get_priv(dev);
417 u8 *addr = plat->enetaddr;
418
Michael Walle1d3e24f2019-12-20 14:16:48 +0100419 if (IS_ENABLED(CONFIG_ARCH_LS1028A))
420 return enetc_ls1028a_write_hwaddr(dev);
421
Alex Marginean7a910c12019-07-03 12:11:40 +0300422 u16 lower = *(const u16 *)(addr + 4);
423 u32 upper = *(const u32 *)addr;
424
425 enetc_write_port(priv, ENETC_PSIPMAR0, upper);
426 enetc_write_port(priv, ENETC_PSIPMAR1, lower);
Michael Walle8c7188e2019-12-20 14:16:47 +0100427
428 return 0;
Alex Marginean7a910c12019-07-03 12:11:40 +0300429}
430
431/* Configure port parameters (# of rings, frame size, enable port) */
432static void enetc_enable_si_port(struct enetc_priv *priv)
433{
434 u32 val;
435
436 /* set Rx/Tx BDR count */
437 val = ENETC_PSICFGR_SET_TXBDR(ENETC_TX_BDR_CNT);
438 val |= ENETC_PSICFGR_SET_RXBDR(ENETC_RX_BDR_CNT);
439 enetc_write_port(priv, ENETC_PSICFGR(0), val);
440 /* set Rx max frame size */
441 enetc_write_port(priv, ENETC_PM_MAXFRM, ENETC_RX_MAXFRM_SIZE);
442 /* enable MAC port */
443 enetc_write_port(priv, ENETC_PM_CC, ENETC_PM_CC_RX_TX_EN);
444 /* enable port */
445 enetc_write_port(priv, ENETC_PMR, ENETC_PMR_SI0_EN);
446 /* set SI cache policy */
447 enetc_write(priv, ENETC_SICAR0,
448 ENETC_SICAR_RD_CFG | ENETC_SICAR_WR_CFG);
449 /* enable SI */
450 enetc_write(priv, ENETC_SIMR, ENETC_SIMR_EN);
451}
452
453/* returns DMA address for a given buffer index */
454static inline u64 enetc_rxb_address(struct udevice *dev, int i)
455{
456 return cpu_to_le64(dm_pci_virt_to_mem(dev, net_rx_packets[i]));
457}
458
459/*
460 * Setup a single Tx BD Ring (ID = 0):
461 * - set Tx buffer descriptor address
462 * - set the BD count
463 * - initialize the producer and consumer index
464 */
465static void enetc_setup_tx_bdr(struct udevice *dev)
466{
467 struct enetc_priv *priv = dev_get_priv(dev);
468 struct bd_ring *tx_bdr = &priv->tx_bdr;
469 u64 tx_bd_add = (u64)priv->enetc_txbd;
470
471 /* used later to advance to the next Tx BD */
472 tx_bdr->bd_count = ENETC_BD_CNT;
473 tx_bdr->next_prod_idx = 0;
474 tx_bdr->next_cons_idx = 0;
475 tx_bdr->cons_idx = priv->regs_base +
476 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBCIR);
477 tx_bdr->prod_idx = priv->regs_base +
478 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBPIR);
479
480 /* set Tx BD address */
481 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR0,
482 lower_32_bits(tx_bd_add));
483 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR1,
484 upper_32_bits(tx_bd_add));
485 /* set Tx 8 BD count */
486 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBLENR,
487 tx_bdr->bd_count);
488
489 /* reset both producer/consumer indexes */
490 enetc_write_reg(tx_bdr->cons_idx, tx_bdr->next_cons_idx);
491 enetc_write_reg(tx_bdr->prod_idx, tx_bdr->next_prod_idx);
492
493 /* enable TX ring */
494 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBMR, ENETC_TBMR_EN);
495}
496
497/*
498 * Setup a single Rx BD Ring (ID = 0):
499 * - set Rx buffer descriptors address (one descriptor per buffer)
500 * - set buffer size as max frame size
501 * - enable Rx ring
502 * - reset consumer and producer indexes
503 * - set buffer for each descriptor
504 */
505static void enetc_setup_rx_bdr(struct udevice *dev)
506{
507 struct enetc_priv *priv = dev_get_priv(dev);
508 struct bd_ring *rx_bdr = &priv->rx_bdr;
509 u64 rx_bd_add = (u64)priv->enetc_rxbd;
510 int i;
511
512 /* used later to advance to the next BD produced by ENETC HW */
513 rx_bdr->bd_count = ENETC_BD_CNT;
514 rx_bdr->next_prod_idx = 0;
515 rx_bdr->next_cons_idx = 0;
516 rx_bdr->cons_idx = priv->regs_base +
517 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBCIR);
518 rx_bdr->prod_idx = priv->regs_base +
519 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBPIR);
520
521 /* set Rx BD address */
522 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR0,
523 lower_32_bits(rx_bd_add));
524 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR1,
525 upper_32_bits(rx_bd_add));
526 /* set Rx BD count (multiple of 8) */
527 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBLENR,
528 rx_bdr->bd_count);
529 /* set Rx buffer size */
530 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBSR, PKTSIZE_ALIGN);
531
532 /* fill Rx BD */
533 memset(priv->enetc_rxbd, 0,
534 rx_bdr->bd_count * sizeof(union enetc_rx_bd));
535 for (i = 0; i < rx_bdr->bd_count; i++) {
536 priv->enetc_rxbd[i].w.addr = enetc_rxb_address(dev, i);
537 /* each RX buffer must be aligned to 64B */
538 WARN_ON(priv->enetc_rxbd[i].w.addr & (ARCH_DMA_MINALIGN - 1));
539 }
540
541 /* reset producer (ENETC owned) and consumer (SW owned) index */
542 enetc_write_reg(rx_bdr->cons_idx, rx_bdr->next_cons_idx);
543 enetc_write_reg(rx_bdr->prod_idx, rx_bdr->next_prod_idx);
544
545 /* enable Rx ring */
546 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBMR, ENETC_RBMR_EN);
547}
548
549/*
550 * Start ENETC interface:
551 * - perform FLR
552 * - enable access to port and SI registers
553 * - set mac address
554 * - setup TX/RX buffer descriptors
555 * - enable Tx/Rx rings
556 */
557static int enetc_start(struct udevice *dev)
558{
Alex Marginean7a910c12019-07-03 12:11:40 +0300559 struct enetc_priv *priv = dev_get_priv(dev);
560
561 /* reset and enable the PCI device */
562 dm_pci_flr(dev);
563 dm_pci_clrset_config16(dev, PCI_COMMAND, 0,
564 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
565
Alex Marginean7a910c12019-07-03 12:11:40 +0300566 enetc_enable_si_port(priv);
567
568 /* setup Tx/Rx buffer descriptors */
569 enetc_setup_tx_bdr(dev);
570 enetc_setup_rx_bdr(dev);
571
Vladimir Oltean14ca0c32021-06-29 20:53:16 +0300572 enetc_setup_mac_iface(dev, priv->phy);
573
Vladimir Oltean19363082021-06-29 20:53:17 +0300574 return phy_startup(priv->phy);
Alex Marginean7a910c12019-07-03 12:11:40 +0300575}
576
577/*
578 * Stop the network interface:
579 * - just quiesce it, we can wipe all configuration as _start starts from
580 * scratch each time
581 */
582static void enetc_stop(struct udevice *dev)
583{
584 /* FLR is sufficient to quiesce the device */
585 dm_pci_flr(dev);
Alex Margineand4be7682019-11-25 17:57:27 +0200586 /* leave the BARs accessible after we stop, this is needed to use
587 * internal MDIO in command line.
588 */
589 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
Alex Marginean7a910c12019-07-03 12:11:40 +0300590}
591
592/*
593 * ENETC transmit packet:
594 * - check if Tx BD ring is full
595 * - set buffer/packet address (dma address)
596 * - set final fragment flag
597 * - try while producer index equals consumer index or timeout
598 */
599static int enetc_send(struct udevice *dev, void *packet, int length)
600{
601 struct enetc_priv *priv = dev_get_priv(dev);
602 struct bd_ring *txr = &priv->tx_bdr;
603 void *nv_packet = (void *)packet;
604 int tries = ENETC_POLL_TRIES;
605 u32 pi, ci;
606
607 pi = txr->next_prod_idx;
608 ci = enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK;
609 /* Tx ring is full when */
610 if (((pi + 1) % txr->bd_count) == ci) {
611 enetc_dbg(dev, "Tx BDR full\n");
612 return -ETIMEDOUT;
613 }
614 enetc_dbg(dev, "TxBD[%d]send: pkt_len=%d, buff @0x%x%08x\n", pi, length,
615 upper_32_bits((u64)nv_packet), lower_32_bits((u64)nv_packet));
616
617 /* prepare Tx BD */
618 memset(&priv->enetc_txbd[pi], 0x0, sizeof(struct enetc_tx_bd));
619 priv->enetc_txbd[pi].addr =
620 cpu_to_le64(dm_pci_virt_to_mem(dev, nv_packet));
621 priv->enetc_txbd[pi].buf_len = cpu_to_le16(length);
622 priv->enetc_txbd[pi].frm_len = cpu_to_le16(length);
623 priv->enetc_txbd[pi].flags = cpu_to_le16(ENETC_TXBD_FLAGS_F);
624 dmb();
625 /* send frame: increment producer index */
626 pi = (pi + 1) % txr->bd_count;
627 txr->next_prod_idx = pi;
628 enetc_write_reg(txr->prod_idx, pi);
629 while ((--tries >= 0) &&
630 (pi != (enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK)))
631 udelay(10);
632
633 return tries > 0 ? 0 : -ETIMEDOUT;
634}
635
636/*
637 * Receive frame:
638 * - wait for the next BD to get ready bit set
639 * - clean up the descriptor
640 * - move on and indicate to HW that the cleaned BD is available for Rx
641 */
642static int enetc_recv(struct udevice *dev, int flags, uchar **packetp)
643{
644 struct enetc_priv *priv = dev_get_priv(dev);
645 struct bd_ring *rxr = &priv->rx_bdr;
646 int tries = ENETC_POLL_TRIES;
647 int pi = rxr->next_prod_idx;
648 int ci = rxr->next_cons_idx;
649 u32 status;
650 int len;
651 u8 rdy;
652
653 do {
654 dmb();
655 status = le32_to_cpu(priv->enetc_rxbd[pi].r.lstatus);
656 /* check if current BD is ready to be consumed */
657 rdy = ENETC_RXBD_STATUS_R(status);
658 } while (--tries >= 0 && !rdy);
659
660 if (!rdy)
661 return -EAGAIN;
662
663 dmb();
664 len = le16_to_cpu(priv->enetc_rxbd[pi].r.buf_len);
665 *packetp = (uchar *)enetc_rxb_address(dev, pi);
666 enetc_dbg(dev, "RxBD[%d]: len=%d err=%d pkt=0x%x%08x\n", pi, len,
667 ENETC_RXBD_STATUS_ERRORS(status),
668 upper_32_bits((u64)*packetp), lower_32_bits((u64)*packetp));
669
670 /* BD clean up and advance to next in ring */
671 memset(&priv->enetc_rxbd[pi], 0, sizeof(union enetc_rx_bd));
672 priv->enetc_rxbd[pi].w.addr = enetc_rxb_address(dev, pi);
673 rxr->next_prod_idx = (pi + 1) % rxr->bd_count;
674 ci = (ci + 1) % rxr->bd_count;
675 rxr->next_cons_idx = ci;
676 dmb();
677 /* free up the slot in the ring for HW */
678 enetc_write_reg(rxr->cons_idx, ci);
679
680 return len;
681}
682
683static const struct eth_ops enetc_ops = {
684 .start = enetc_start,
685 .send = enetc_send,
686 .recv = enetc_recv,
687 .stop = enetc_stop,
Michael Walle8c7188e2019-12-20 14:16:47 +0100688 .write_hwaddr = enetc_write_hwaddr,
Alex Marginean7a910c12019-07-03 12:11:40 +0300689};
690
691U_BOOT_DRIVER(eth_enetc) = {
Alex Marginean805b8592019-12-10 16:55:39 +0200692 .name = ENETC_DRIVER_NAME,
Alex Marginean7a910c12019-07-03 12:11:40 +0300693 .id = UCLASS_ETH,
694 .bind = enetc_bind,
695 .probe = enetc_probe,
696 .remove = enetc_remove,
697 .ops = &enetc_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700698 .priv_auto = sizeof(struct enetc_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700699 .plat_auto = sizeof(struct eth_pdata),
Alex Marginean7a910c12019-07-03 12:11:40 +0300700};
701
702static struct pci_device_id enetc_ids[] = {
703 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, PCI_DEVICE_ID_ENETC_ETH) },
704 {}
705};
706
707U_BOOT_PCI_DEVICE(eth_enetc, enetc_ids);