blob: 915c7c80256fd7e1b5e424a5fa97f609ee863126 [file] [log] [blame]
Alex Marginean120b5ef2019-07-03 12:11:40 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * ENETC ethernet controller driver
Vladimir Olteancd8817a2021-06-29 20:53:15 +03004 * Copyright 2017-2021 NXP
Alex Marginean120b5ef2019-07-03 12:11:40 +03005 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060010#include <fdt_support.h>
Simon Glass336d4612020-02-03 07:36:16 -070011#include <malloc.h>
Alex Marginean120b5ef2019-07-03 12:11:40 +030012#include <memalign.h>
Simon Glass90526e92020-05-10 11:39:56 -060013#include <net.h>
14#include <asm/cache.h>
Alex Marginean120b5ef2019-07-03 12:11:40 +030015#include <asm/io.h>
16#include <pci.h>
Alex Marginean1d995342019-07-03 12:11:41 +030017#include <miiphy.h>
Simon Glasseb41d8a2020-05-10 11:40:08 -060018#include <linux/bug.h>
Simon Glassc05ed002020-05-10 11:40:11 -060019#include <linux/delay.h>
Alex Marginean120b5ef2019-07-03 12:11:40 +030020
21#include "fsl_enetc.h"
22
Alex Marginean9c2aee12019-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 Glass8a8d24b2020-12-03 16:55:23 -070057 struct pci_child_plat *ppdata;
Alex Marginean9c2aee12019-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 Glassc69cda22020-12-03 16:55:20 -070071 pdata = dev_get_plat(dev);
Simon Glasscaa4daa2020-12-03 16:55:18 -070072 ppdata = dev_get_parent_plat(dev);
Alex Marginean9c2aee12019-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 Marginean120b5ef2019-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 Glassf10643c2020-12-19 10:40:14 -0700102 if (ofnode_valid(dev_ofnode(dev)))
Alex Marginean120b5ef2019-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 Margineane4aafd52019-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 Marginean9bc07e812019-07-15 11:48:47 +0300141 bool is2500 = false;
142 u16 reg;
Alex Margineane4aafd52019-07-03 12:11:42 +0300143
144 if (!enetc_has_imdio(dev))
145 return 0;
146
Vladimir Oltean7c2d5d12021-09-18 15:32:35 +0300147 if (priv->if_type == PHY_INTERFACE_MODE_2500BASEX)
Alex Marginean9bc07e812019-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 Margineane4aafd52019-07-03 12:11:42 +0300158 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean9bc07e812019-07-15 11:48:47 +0300159 ENETC_PCS_IF_MODE, reg);
Alex Margineane4aafd52019-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 Marginean9bc07e812019-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 Margineane4aafd52019-07-03 12:11:42 +0300173 /* restart PCS AN */
174 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean9bc07e812019-07-15 11:48:47 +0300175 ENETC_PCS_CR, reg);
Alex Margineane4aafd52019-07-03 12:11:42 +0300176
177 return 0;
178}
179
180/* set up MAC for RGMII */
Vladimir Oltean71346a82021-06-29 20:53:16 +0300181static void enetc_init_rgmii(struct udevice *dev, struct phy_device *phydev)
Alex Margineane4aafd52019-07-03 12:11:42 +0300182{
183 struct enetc_priv *priv = dev_get_priv(dev);
Vladimir Oltean71346a82021-06-29 20:53:16 +0300184 u32 old_val, val;
Alex Margineane4aafd52019-07-03 12:11:42 +0300185
Vladimir Oltean71346a82021-06-29 20:53:16 +0300186 old_val = val = enetc_read_port(priv, ENETC_PM_IF_MODE);
Alex Margineane4aafd52019-07-03 12:11:42 +0300187
Vladimir Oltean71346a82021-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 Margineane4aafd52019-07-03 12:11:42 +0300213}
214
Alex Margineanb8e4eec2020-01-10 23:32:20 +0200215/* set up MAC configuration for the given interface type */
Vladimir Oltean71346a82021-06-29 20:53:16 +0300216static void enetc_setup_mac_iface(struct udevice *dev,
217 struct phy_device *phydev)
Alex Margineane4aafd52019-07-03 12:11:42 +0300218{
219 struct enetc_priv *priv = dev_get_priv(dev);
220 u32 if_mode;
221
Alex Margineanb8e4eec2020-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 Oltean71346a82021-06-29 20:53:16 +0300227 enetc_init_rgmii(dev, phydev);
Alex Margineanb8e4eec2020-01-10 23:32:20 +0200228 break;
Alex Margineanb8e4eec2020-01-10 23:32:20 +0200229 case PHY_INTERFACE_MODE_USXGMII:
Vladimir Oltean77b11f72021-09-18 15:32:34 +0300230 case PHY_INTERFACE_MODE_10GBASER:
Alex Margineanb8e4eec2020-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 Margineane4aafd52019-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 Marginean9bc07e812019-07-15 11:48:47 +0300254 ENETC_PCS_CR_RST | ENETC_PCS_CR_RESET_AN);
Alex Margineane4aafd52019-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);
263 const char *if_str;
264
265 priv->if_type = PHY_INTERFACE_MODE_NONE;
266
Alex Marginean1e354cb2019-11-25 17:57:27 +0200267 /* register internal MDIO for debug purposes */
Alex Margineane4aafd52019-07-03 12:11:42 +0300268 if (enetc_read_port(priv, ENETC_PCAPR0) & ENETC_PCAPRO_MDIO) {
Alex Margineane4aafd52019-07-03 12:11:42 +0300269 priv->imdio.read = enetc_mdio_read;
270 priv->imdio.write = enetc_mdio_write;
271 priv->imdio.priv = priv->port_regs + ENETC_PM_IMDIO_BASE;
Vladimir Olteanf848b482021-09-27 14:21:48 +0300272 strlcpy(priv->imdio.name, dev->name, MDIO_NAME_LEN);
Alex Marginean1e354cb2019-11-25 17:57:27 +0200273 if (!miiphy_get_dev_by_name(priv->imdio.name))
274 mdio_register(&priv->imdio);
Alex Margineane4aafd52019-07-03 12:11:42 +0300275 }
276
Simon Glassf10643c2020-12-19 10:40:14 -0700277 if (!ofnode_valid(dev_ofnode(dev))) {
Alex Margineane4aafd52019-07-03 12:11:42 +0300278 enetc_dbg(dev, "no enetc ofnode found, skipping PCS set-up\n");
279 return;
280 }
281
Simon Glassf10643c2020-12-19 10:40:14 -0700282 if_str = ofnode_read_string(dev_ofnode(dev), "phy-mode");
Alex Margineane4aafd52019-07-03 12:11:42 +0300283 if (if_str)
284 priv->if_type = phy_get_interface_by_name(if_str);
285 else
286 enetc_dbg(dev,
287 "phy-mode property not found, defaulting to SGMII\n");
288 if (priv->if_type < 0)
289 priv->if_type = PHY_INTERFACE_MODE_NONE;
290
291 switch (priv->if_type) {
292 case PHY_INTERFACE_MODE_SGMII:
Vladimir Oltean7c2d5d12021-09-18 15:32:35 +0300293 case PHY_INTERFACE_MODE_2500BASEX:
Alex Margineane4aafd52019-07-03 12:11:42 +0300294 enetc_init_sgmii(dev);
295 break;
Alex Margineane22e3af2019-11-14 18:28:38 +0200296 case PHY_INTERFACE_MODE_USXGMII:
Vladimir Oltean77b11f72021-09-18 15:32:34 +0300297 case PHY_INTERFACE_MODE_10GBASER:
Alex Margineane4aafd52019-07-03 12:11:42 +0300298 enetc_init_sxgmii(dev);
299 break;
300 };
301}
302
Alex Marginean1d995342019-07-03 12:11:41 +0300303/* Configure the actual/external ethernet PHY, if one is found */
Vladimir Olteancd8817a2021-06-29 20:53:15 +0300304static int enetc_config_phy(struct udevice *dev)
Alex Marginean1d995342019-07-03 12:11:41 +0300305{
306 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean1d995342019-07-03 12:11:41 +0300307 int supported;
308
Alex Marginean17bd7ea2019-11-25 17:15:13 +0200309 priv->phy = dm_eth_phy_connect(dev);
Alex Marginean17bd7ea2019-11-25 17:15:13 +0200310 if (!priv->phy)
Vladimir Olteancd8817a2021-06-29 20:53:15 +0300311 return -ENODEV;
Alex Marginean1d995342019-07-03 12:11:41 +0300312
Alex Marginean307f8a62019-11-14 18:58:45 +0200313 supported = PHY_GBIT_FEATURES | SUPPORTED_2500baseX_Full;
314 priv->phy->supported &= supported;
315 priv->phy->advertising &= supported;
Alex Marginean17bd7ea2019-11-25 17:15:13 +0200316
Vladimir Olteancd8817a2021-06-29 20:53:15 +0300317 return phy_config(priv->phy);
Alex Marginean1d995342019-07-03 12:11:41 +0300318}
319
Alex Marginean120b5ef2019-07-03 12:11:40 +0300320/*
321 * Probe ENETC driver:
322 * - initialize port and station interface BARs
323 */
324static int enetc_probe(struct udevice *dev)
325{
326 struct enetc_priv *priv = dev_get_priv(dev);
327
Simon Glassf10643c2020-12-19 10:40:14 -0700328 if (ofnode_valid(dev_ofnode(dev)) && !ofnode_is_available(dev_ofnode(dev))) {
Alex Marginean120b5ef2019-07-03 12:11:40 +0300329 enetc_dbg(dev, "interface disabled\n");
330 return -ENODEV;
331 }
332
333 priv->enetc_txbd = memalign(ENETC_BD_ALIGN,
334 sizeof(struct enetc_tx_bd) * ENETC_BD_CNT);
335 priv->enetc_rxbd = memalign(ENETC_BD_ALIGN,
336 sizeof(union enetc_rx_bd) * ENETC_BD_CNT);
337
338 if (!priv->enetc_txbd || !priv->enetc_rxbd) {
339 /* free should be able to handle NULL, just free all pointers */
340 free(priv->enetc_txbd);
341 free(priv->enetc_rxbd);
342
343 return -ENOMEM;
344 }
345
346 /* initialize register */
347 priv->regs_base = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0, 0);
348 if (!priv->regs_base) {
349 enetc_dbg(dev, "failed to map BAR0\n");
350 return -EINVAL;
351 }
352 priv->port_regs = priv->regs_base + ENETC_PORT_REGS_OFF;
353
354 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
355
Alex Margineana931f782019-11-14 18:58:46 +0200356 enetc_start_pcs(dev);
Alex Margineana931f782019-11-14 18:58:46 +0200357
Vladimir Olteancd8817a2021-06-29 20:53:15 +0300358 return enetc_config_phy(dev);
Alex Marginean120b5ef2019-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
369 free(priv->enetc_txbd);
370 free(priv->enetc_rxbd);
371
372 return 0;
373}
374
Michael Walle42c66f02019-12-20 14:16:48 +0100375/*
376 * LS1028A is the only part with IERB at this time and there are plans to
377 * change its structure, keep this LS1028A specific for now.
378 */
379#define LS1028A_IERB_BASE 0x1f0800000ULL
380#define LS1028A_IERB_PSIPMAR0(pf, vf) (LS1028A_IERB_BASE + 0x8000 \
381 + (pf) * 0x100 + (vf) * 8)
382#define LS1028A_IERB_PSIPMAR1(pf, vf) (LS1028A_IERB_PSIPMAR0(pf, vf) + 4)
383
384static int enetc_ls1028a_write_hwaddr(struct udevice *dev)
385{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700386 struct pci_child_plat *ppdata = dev_get_parent_plat(dev);
Michael Walle42c66f02019-12-20 14:16:48 +0100387 const int devfn_to_pf[] = {0, 1, 2, -1, -1, -1, 3};
Simon Glassc69cda22020-12-03 16:55:20 -0700388 struct eth_pdata *plat = dev_get_plat(dev);
Michael Walle42c66f02019-12-20 14:16:48 +0100389 int devfn = PCI_FUNC(ppdata->devfn);
390 u8 *addr = plat->enetaddr;
391 u32 lower, upper;
392 int pf;
393
394 if (devfn >= ARRAY_SIZE(devfn_to_pf))
395 return 0;
396
397 pf = devfn_to_pf[devfn];
398 if (pf < 0)
399 return 0;
400
401 lower = *(const u16 *)(addr + 4);
402 upper = *(const u32 *)addr;
403
404 out_le32(LS1028A_IERB_PSIPMAR0(pf, 0), upper);
405 out_le32(LS1028A_IERB_PSIPMAR1(pf, 0), lower);
406
407 return 0;
408}
409
Michael Walleee5c70b2019-12-20 14:16:47 +0100410static int enetc_write_hwaddr(struct udevice *dev)
Alex Marginean120b5ef2019-07-03 12:11:40 +0300411{
Simon Glassc69cda22020-12-03 16:55:20 -0700412 struct eth_pdata *plat = dev_get_plat(dev);
Michael Walleee5c70b2019-12-20 14:16:47 +0100413 struct enetc_priv *priv = dev_get_priv(dev);
414 u8 *addr = plat->enetaddr;
415
Michael Walle42c66f02019-12-20 14:16:48 +0100416 if (IS_ENABLED(CONFIG_ARCH_LS1028A))
417 return enetc_ls1028a_write_hwaddr(dev);
418
Alex Marginean120b5ef2019-07-03 12:11:40 +0300419 u16 lower = *(const u16 *)(addr + 4);
420 u32 upper = *(const u32 *)addr;
421
422 enetc_write_port(priv, ENETC_PSIPMAR0, upper);
423 enetc_write_port(priv, ENETC_PSIPMAR1, lower);
Michael Walleee5c70b2019-12-20 14:16:47 +0100424
425 return 0;
Alex Marginean120b5ef2019-07-03 12:11:40 +0300426}
427
428/* Configure port parameters (# of rings, frame size, enable port) */
429static void enetc_enable_si_port(struct enetc_priv *priv)
430{
431 u32 val;
432
433 /* set Rx/Tx BDR count */
434 val = ENETC_PSICFGR_SET_TXBDR(ENETC_TX_BDR_CNT);
435 val |= ENETC_PSICFGR_SET_RXBDR(ENETC_RX_BDR_CNT);
436 enetc_write_port(priv, ENETC_PSICFGR(0), val);
437 /* set Rx max frame size */
438 enetc_write_port(priv, ENETC_PM_MAXFRM, ENETC_RX_MAXFRM_SIZE);
439 /* enable MAC port */
440 enetc_write_port(priv, ENETC_PM_CC, ENETC_PM_CC_RX_TX_EN);
441 /* enable port */
442 enetc_write_port(priv, ENETC_PMR, ENETC_PMR_SI0_EN);
443 /* set SI cache policy */
444 enetc_write(priv, ENETC_SICAR0,
445 ENETC_SICAR_RD_CFG | ENETC_SICAR_WR_CFG);
446 /* enable SI */
447 enetc_write(priv, ENETC_SIMR, ENETC_SIMR_EN);
448}
449
450/* returns DMA address for a given buffer index */
451static inline u64 enetc_rxb_address(struct udevice *dev, int i)
452{
453 return cpu_to_le64(dm_pci_virt_to_mem(dev, net_rx_packets[i]));
454}
455
456/*
457 * Setup a single Tx BD Ring (ID = 0):
458 * - set Tx buffer descriptor address
459 * - set the BD count
460 * - initialize the producer and consumer index
461 */
462static void enetc_setup_tx_bdr(struct udevice *dev)
463{
464 struct enetc_priv *priv = dev_get_priv(dev);
465 struct bd_ring *tx_bdr = &priv->tx_bdr;
466 u64 tx_bd_add = (u64)priv->enetc_txbd;
467
468 /* used later to advance to the next Tx BD */
469 tx_bdr->bd_count = ENETC_BD_CNT;
470 tx_bdr->next_prod_idx = 0;
471 tx_bdr->next_cons_idx = 0;
472 tx_bdr->cons_idx = priv->regs_base +
473 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBCIR);
474 tx_bdr->prod_idx = priv->regs_base +
475 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBPIR);
476
477 /* set Tx BD address */
478 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR0,
479 lower_32_bits(tx_bd_add));
480 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR1,
481 upper_32_bits(tx_bd_add));
482 /* set Tx 8 BD count */
483 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBLENR,
484 tx_bdr->bd_count);
485
486 /* reset both producer/consumer indexes */
487 enetc_write_reg(tx_bdr->cons_idx, tx_bdr->next_cons_idx);
488 enetc_write_reg(tx_bdr->prod_idx, tx_bdr->next_prod_idx);
489
490 /* enable TX ring */
491 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBMR, ENETC_TBMR_EN);
492}
493
494/*
495 * Setup a single Rx BD Ring (ID = 0):
496 * - set Rx buffer descriptors address (one descriptor per buffer)
497 * - set buffer size as max frame size
498 * - enable Rx ring
499 * - reset consumer and producer indexes
500 * - set buffer for each descriptor
501 */
502static void enetc_setup_rx_bdr(struct udevice *dev)
503{
504 struct enetc_priv *priv = dev_get_priv(dev);
505 struct bd_ring *rx_bdr = &priv->rx_bdr;
506 u64 rx_bd_add = (u64)priv->enetc_rxbd;
507 int i;
508
509 /* used later to advance to the next BD produced by ENETC HW */
510 rx_bdr->bd_count = ENETC_BD_CNT;
511 rx_bdr->next_prod_idx = 0;
512 rx_bdr->next_cons_idx = 0;
513 rx_bdr->cons_idx = priv->regs_base +
514 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBCIR);
515 rx_bdr->prod_idx = priv->regs_base +
516 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBPIR);
517
518 /* set Rx BD address */
519 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR0,
520 lower_32_bits(rx_bd_add));
521 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR1,
522 upper_32_bits(rx_bd_add));
523 /* set Rx BD count (multiple of 8) */
524 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBLENR,
525 rx_bdr->bd_count);
526 /* set Rx buffer size */
527 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBSR, PKTSIZE_ALIGN);
528
529 /* fill Rx BD */
530 memset(priv->enetc_rxbd, 0,
531 rx_bdr->bd_count * sizeof(union enetc_rx_bd));
532 for (i = 0; i < rx_bdr->bd_count; i++) {
533 priv->enetc_rxbd[i].w.addr = enetc_rxb_address(dev, i);
534 /* each RX buffer must be aligned to 64B */
535 WARN_ON(priv->enetc_rxbd[i].w.addr & (ARCH_DMA_MINALIGN - 1));
536 }
537
538 /* reset producer (ENETC owned) and consumer (SW owned) index */
539 enetc_write_reg(rx_bdr->cons_idx, rx_bdr->next_cons_idx);
540 enetc_write_reg(rx_bdr->prod_idx, rx_bdr->next_prod_idx);
541
542 /* enable Rx ring */
543 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBMR, ENETC_RBMR_EN);
544}
545
546/*
547 * Start ENETC interface:
548 * - perform FLR
549 * - enable access to port and SI registers
550 * - set mac address
551 * - setup TX/RX buffer descriptors
552 * - enable Tx/Rx rings
553 */
554static int enetc_start(struct udevice *dev)
555{
Alex Marginean120b5ef2019-07-03 12:11:40 +0300556 struct enetc_priv *priv = dev_get_priv(dev);
557
558 /* reset and enable the PCI device */
559 dm_pci_flr(dev);
560 dm_pci_clrset_config16(dev, PCI_COMMAND, 0,
561 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
562
Alex Marginean120b5ef2019-07-03 12:11:40 +0300563 enetc_enable_si_port(priv);
564
565 /* setup Tx/Rx buffer descriptors */
566 enetc_setup_tx_bdr(dev);
567 enetc_setup_rx_bdr(dev);
568
Vladimir Oltean71346a82021-06-29 20:53:16 +0300569 enetc_setup_mac_iface(dev, priv->phy);
570
Vladimir Olteanc4428502021-06-29 20:53:17 +0300571 return phy_startup(priv->phy);
Alex Marginean120b5ef2019-07-03 12:11:40 +0300572}
573
574/*
575 * Stop the network interface:
576 * - just quiesce it, we can wipe all configuration as _start starts from
577 * scratch each time
578 */
579static void enetc_stop(struct udevice *dev)
580{
581 /* FLR is sufficient to quiesce the device */
582 dm_pci_flr(dev);
Alex Marginean1e354cb2019-11-25 17:57:27 +0200583 /* leave the BARs accessible after we stop, this is needed to use
584 * internal MDIO in command line.
585 */
586 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
Alex Marginean120b5ef2019-07-03 12:11:40 +0300587}
588
589/*
590 * ENETC transmit packet:
591 * - check if Tx BD ring is full
592 * - set buffer/packet address (dma address)
593 * - set final fragment flag
594 * - try while producer index equals consumer index or timeout
595 */
596static int enetc_send(struct udevice *dev, void *packet, int length)
597{
598 struct enetc_priv *priv = dev_get_priv(dev);
599 struct bd_ring *txr = &priv->tx_bdr;
600 void *nv_packet = (void *)packet;
601 int tries = ENETC_POLL_TRIES;
602 u32 pi, ci;
603
604 pi = txr->next_prod_idx;
605 ci = enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK;
606 /* Tx ring is full when */
607 if (((pi + 1) % txr->bd_count) == ci) {
608 enetc_dbg(dev, "Tx BDR full\n");
609 return -ETIMEDOUT;
610 }
611 enetc_dbg(dev, "TxBD[%d]send: pkt_len=%d, buff @0x%x%08x\n", pi, length,
612 upper_32_bits((u64)nv_packet), lower_32_bits((u64)nv_packet));
613
614 /* prepare Tx BD */
615 memset(&priv->enetc_txbd[pi], 0x0, sizeof(struct enetc_tx_bd));
616 priv->enetc_txbd[pi].addr =
617 cpu_to_le64(dm_pci_virt_to_mem(dev, nv_packet));
618 priv->enetc_txbd[pi].buf_len = cpu_to_le16(length);
619 priv->enetc_txbd[pi].frm_len = cpu_to_le16(length);
620 priv->enetc_txbd[pi].flags = cpu_to_le16(ENETC_TXBD_FLAGS_F);
621 dmb();
622 /* send frame: increment producer index */
623 pi = (pi + 1) % txr->bd_count;
624 txr->next_prod_idx = pi;
625 enetc_write_reg(txr->prod_idx, pi);
626 while ((--tries >= 0) &&
627 (pi != (enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK)))
628 udelay(10);
629
630 return tries > 0 ? 0 : -ETIMEDOUT;
631}
632
633/*
634 * Receive frame:
635 * - wait for the next BD to get ready bit set
636 * - clean up the descriptor
637 * - move on and indicate to HW that the cleaned BD is available for Rx
638 */
639static int enetc_recv(struct udevice *dev, int flags, uchar **packetp)
640{
641 struct enetc_priv *priv = dev_get_priv(dev);
642 struct bd_ring *rxr = &priv->rx_bdr;
643 int tries = ENETC_POLL_TRIES;
644 int pi = rxr->next_prod_idx;
645 int ci = rxr->next_cons_idx;
646 u32 status;
647 int len;
648 u8 rdy;
649
650 do {
651 dmb();
652 status = le32_to_cpu(priv->enetc_rxbd[pi].r.lstatus);
653 /* check if current BD is ready to be consumed */
654 rdy = ENETC_RXBD_STATUS_R(status);
655 } while (--tries >= 0 && !rdy);
656
657 if (!rdy)
658 return -EAGAIN;
659
660 dmb();
661 len = le16_to_cpu(priv->enetc_rxbd[pi].r.buf_len);
662 *packetp = (uchar *)enetc_rxb_address(dev, pi);
663 enetc_dbg(dev, "RxBD[%d]: len=%d err=%d pkt=0x%x%08x\n", pi, len,
664 ENETC_RXBD_STATUS_ERRORS(status),
665 upper_32_bits((u64)*packetp), lower_32_bits((u64)*packetp));
666
667 /* BD clean up and advance to next in ring */
668 memset(&priv->enetc_rxbd[pi], 0, sizeof(union enetc_rx_bd));
669 priv->enetc_rxbd[pi].w.addr = enetc_rxb_address(dev, pi);
670 rxr->next_prod_idx = (pi + 1) % rxr->bd_count;
671 ci = (ci + 1) % rxr->bd_count;
672 rxr->next_cons_idx = ci;
673 dmb();
674 /* free up the slot in the ring for HW */
675 enetc_write_reg(rxr->cons_idx, ci);
676
677 return len;
678}
679
680static const struct eth_ops enetc_ops = {
681 .start = enetc_start,
682 .send = enetc_send,
683 .recv = enetc_recv,
684 .stop = enetc_stop,
Michael Walleee5c70b2019-12-20 14:16:47 +0100685 .write_hwaddr = enetc_write_hwaddr,
Alex Marginean120b5ef2019-07-03 12:11:40 +0300686};
687
688U_BOOT_DRIVER(eth_enetc) = {
Alex Marginean9c2aee12019-12-10 16:55:39 +0200689 .name = ENETC_DRIVER_NAME,
Alex Marginean120b5ef2019-07-03 12:11:40 +0300690 .id = UCLASS_ETH,
691 .bind = enetc_bind,
692 .probe = enetc_probe,
693 .remove = enetc_remove,
694 .ops = &enetc_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700695 .priv_auto = sizeof(struct enetc_priv),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700696 .plat_auto = sizeof(struct eth_pdata),
Alex Marginean120b5ef2019-07-03 12:11:40 +0300697};
698
699static struct pci_device_id enetc_ids[] = {
700 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, PCI_DEVICE_ID_ENETC_ETH) },
701 {}
702};
703
704U_BOOT_PCI_DEVICE(eth_enetc, enetc_ids);