blob: f0d15febcc8e0fc65664bafc65d166c6053b8329 [file] [log] [blame]
Alex Marginean120b5ef2019-07-03 12:11:40 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * ENETC ethernet controller driver
4 * Copyright 2017-2019 NXP
5 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
10#include <memalign.h>
11#include <asm/io.h>
12#include <pci.h>
Alex Marginean1d995342019-07-03 12:11:41 +030013#include <miiphy.h>
Alex Marginean120b5ef2019-07-03 12:11:40 +030014
15#include "fsl_enetc.h"
16
Alex Marginean9c2aee12019-12-10 16:55:39 +020017#define ENETC_DRIVER_NAME "enetc_eth"
18
19/*
20 * sets the MAC address in IERB registers, this setting is persistent and
21 * carried over to Linux.
22 */
23static void enetc_set_ierb_primary_mac(struct udevice *dev, int devfn,
24 const u8 *enetaddr)
25{
26#ifdef CONFIG_ARCH_LS1028A
27/*
28 * LS1028A is the only part with IERB at this time and there are plans to change
29 * its structure, keep this LS1028A specific for now
30 */
31#define IERB_BASE 0x1f0800000ULL
32#define IERB_PFMAC(pf, vf, n) (IERB_BASE + 0x8000 + (pf) * 0x100 + (vf) * 8 \
33 + (n) * 4)
34
35static int ierb_fn_to_pf[] = {0, 1, 2, -1, -1, -1, 3};
36
37 u16 lower = *(const u16 *)(enetaddr + 4);
38 u32 upper = *(const u32 *)enetaddr;
39
40 if (ierb_fn_to_pf[devfn] < 0)
41 return;
42
43 out_le32(IERB_PFMAC(ierb_fn_to_pf[devfn], 0, 0), upper);
44 out_le32(IERB_PFMAC(ierb_fn_to_pf[devfn], 0, 1), (u32)lower);
45#endif
46}
47
48/* sets up primary MAC addresses in DT/IERB */
49void fdt_fixup_enetc_mac(void *blob)
50{
51 struct pci_child_platdata *ppdata;
52 struct eth_pdata *pdata;
53 struct udevice *dev;
54 struct uclass *uc;
55 char path[256];
56 int offset;
57 int devfn;
58
59 uclass_get(UCLASS_ETH, &uc);
60 uclass_foreach_dev(dev, uc) {
61 if (!dev->driver || !dev->driver->name ||
62 strcmp(dev->driver->name, ENETC_DRIVER_NAME))
63 continue;
64
65 pdata = dev_get_platdata(dev);
66 ppdata = dev_get_parent_platdata(dev);
67 devfn = PCI_FUNC(ppdata->devfn);
68
69 enetc_set_ierb_primary_mac(dev, devfn, pdata->enetaddr);
70
71 snprintf(path, 256, "/soc/pcie@1f0000000/ethernet@%x,%x",
72 PCI_DEV(ppdata->devfn), PCI_FUNC(ppdata->devfn));
73 offset = fdt_path_offset(blob, path);
74 if (offset < 0)
75 continue;
76 fdt_setprop(blob, offset, "mac-address", pdata->enetaddr, 6);
77 }
78}
79
Alex Marginean120b5ef2019-07-03 12:11:40 +030080/*
81 * Bind the device:
82 * - set a more explicit name on the interface
83 */
84static int enetc_bind(struct udevice *dev)
85{
86 char name[16];
87 static int eth_num_devices;
88
89 /*
90 * prefer using PCI function numbers to number interfaces, but these
91 * are only available if dts nodes are present. For PCI they are
92 * optional, handle that case too. Just in case some nodes are present
93 * and some are not, use different naming scheme - enetc-N based on
94 * PCI function # and enetc#N based on interface count
95 */
96 if (ofnode_valid(dev->node))
97 sprintf(name, "enetc-%u", PCI_FUNC(pci_get_devfn(dev)));
98 else
99 sprintf(name, "enetc#%u", eth_num_devices++);
100 device_set_name(dev, name);
101
102 return 0;
103}
104
Alex Margineane4aafd52019-07-03 12:11:42 +0300105/* MDIO wrappers, we're using these to drive internal MDIO to get to serdes */
106static int enetc_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
107{
108 struct enetc_mdio_priv priv;
109
110 priv.regs_base = bus->priv;
111 return enetc_mdio_read_priv(&priv, addr, devad, reg);
112}
113
114static int enetc_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
115 u16 val)
116{
117 struct enetc_mdio_priv priv;
118
119 priv.regs_base = bus->priv;
120 return enetc_mdio_write_priv(&priv, addr, devad, reg, val);
121}
122
123/* only interfaces that can pin out through serdes have internal MDIO */
124static bool enetc_has_imdio(struct udevice *dev)
125{
126 struct enetc_priv *priv = dev_get_priv(dev);
127
128 return !!(priv->imdio.priv);
129}
130
131/* set up serdes for SGMII */
132static int enetc_init_sgmii(struct udevice *dev)
133{
134 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean9bc07e812019-07-15 11:48:47 +0300135 bool is2500 = false;
136 u16 reg;
Alex Margineane4aafd52019-07-03 12:11:42 +0300137
138 if (!enetc_has_imdio(dev))
139 return 0;
140
Alex Marginean9bc07e812019-07-15 11:48:47 +0300141 if (priv->if_type == PHY_INTERFACE_MODE_SGMII_2500)
142 is2500 = true;
143
144 /*
145 * Set to SGMII mode, for 1Gbps enable AN, for 2.5Gbps set fixed speed.
146 * Although fixed speed is 1Gbps, we could be running at 2.5Gbps based
147 * on PLL configuration. Setting 1G for 2.5G here is counter intuitive
148 * but intentional.
149 */
150 reg = ENETC_PCS_IF_MODE_SGMII;
151 reg |= is2500 ? ENETC_PCS_IF_MODE_SPEED_1G : ENETC_PCS_IF_MODE_SGMII_AN;
Alex Margineane4aafd52019-07-03 12:11:42 +0300152 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean9bc07e812019-07-15 11:48:47 +0300153 ENETC_PCS_IF_MODE, reg);
Alex Margineane4aafd52019-07-03 12:11:42 +0300154
155 /* Dev ability - SGMII */
156 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
157 ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SGMII);
158
159 /* Adjust link timer for SGMII */
160 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
161 ENETC_PCS_LINK_TIMER1, ENETC_PCS_LINK_TIMER1_VAL);
162 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
163 ENETC_PCS_LINK_TIMER2, ENETC_PCS_LINK_TIMER2_VAL);
164
Alex Marginean9bc07e812019-07-15 11:48:47 +0300165 reg = ENETC_PCS_CR_DEF_VAL;
166 reg |= is2500 ? ENETC_PCS_CR_RST : ENETC_PCS_CR_RESET_AN;
Alex Margineane4aafd52019-07-03 12:11:42 +0300167 /* restart PCS AN */
168 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean9bc07e812019-07-15 11:48:47 +0300169 ENETC_PCS_CR, reg);
Alex Margineane4aafd52019-07-03 12:11:42 +0300170
171 return 0;
172}
173
174/* set up MAC for RGMII */
175static int enetc_init_rgmii(struct udevice *dev)
176{
177 struct enetc_priv *priv = dev_get_priv(dev);
178 u32 if_mode;
179
180 /* enable RGMII AN */
181 if_mode = enetc_read_port(priv, ENETC_PM_IF_MODE);
182 if_mode |= ENETC_PM_IF_MODE_AN_ENA;
183 enetc_write_port(priv, ENETC_PM_IF_MODE, if_mode);
184
185 return 0;
186}
187
Alex Margineanb8e4eec2020-01-10 23:32:20 +0200188/* set up MAC configuration for the given interface type */
189static void enetc_setup_mac_iface(struct udevice *dev)
Alex Margineane4aafd52019-07-03 12:11:42 +0300190{
191 struct enetc_priv *priv = dev_get_priv(dev);
192 u32 if_mode;
193
Alex Margineanb8e4eec2020-01-10 23:32:20 +0200194 switch (priv->if_type) {
195 case PHY_INTERFACE_MODE_RGMII:
196 case PHY_INTERFACE_MODE_RGMII_ID:
197 case PHY_INTERFACE_MODE_RGMII_RXID:
198 case PHY_INTERFACE_MODE_RGMII_TXID:
199 enetc_init_rgmii(dev);
200 break;
201 case PHY_INTERFACE_MODE_XGMII:
202 case PHY_INTERFACE_MODE_USXGMII:
203 case PHY_INTERFACE_MODE_XFI:
204 /* set ifmode to (US)XGMII */
205 if_mode = enetc_read_port(priv, ENETC_PM_IF_MODE);
206 if_mode &= ~ENETC_PM_IF_IFMODE_MASK;
207 enetc_write_port(priv, ENETC_PM_IF_MODE, if_mode);
208 break;
209 };
210}
211
212/* set up serdes for SXGMII */
213static int enetc_init_sxgmii(struct udevice *dev)
214{
215 struct enetc_priv *priv = dev_get_priv(dev);
Alex Margineane4aafd52019-07-03 12:11:42 +0300216
217 if (!enetc_has_imdio(dev))
218 return 0;
219
220 /* Dev ability - SXGMII */
221 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
222 ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SXGMII);
223
224 /* Restart PCS AN */
225 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
226 ENETC_PCS_CR,
Alex Marginean9bc07e812019-07-15 11:48:47 +0300227 ENETC_PCS_CR_RST | ENETC_PCS_CR_RESET_AN);
Alex Margineane4aafd52019-07-03 12:11:42 +0300228
229 return 0;
230}
231
232/* Apply protocol specific configuration to MAC, serdes as needed */
233static void enetc_start_pcs(struct udevice *dev)
234{
235 struct enetc_priv *priv = dev_get_priv(dev);
236 const char *if_str;
237
238 priv->if_type = PHY_INTERFACE_MODE_NONE;
239
Alex Marginean1e354cb2019-11-25 17:57:27 +0200240 /* register internal MDIO for debug purposes */
Alex Margineane4aafd52019-07-03 12:11:42 +0300241 if (enetc_read_port(priv, ENETC_PCAPR0) & ENETC_PCAPRO_MDIO) {
Alex Margineane4aafd52019-07-03 12:11:42 +0300242 priv->imdio.read = enetc_mdio_read;
243 priv->imdio.write = enetc_mdio_write;
244 priv->imdio.priv = priv->port_regs + ENETC_PM_IMDIO_BASE;
245 strncpy(priv->imdio.name, dev->name, MDIO_NAME_LEN);
Alex Marginean1e354cb2019-11-25 17:57:27 +0200246 if (!miiphy_get_dev_by_name(priv->imdio.name))
247 mdio_register(&priv->imdio);
Alex Margineane4aafd52019-07-03 12:11:42 +0300248 }
249
250 if (!ofnode_valid(dev->node)) {
251 enetc_dbg(dev, "no enetc ofnode found, skipping PCS set-up\n");
252 return;
253 }
254
255 if_str = ofnode_read_string(dev->node, "phy-mode");
256 if (if_str)
257 priv->if_type = phy_get_interface_by_name(if_str);
258 else
259 enetc_dbg(dev,
260 "phy-mode property not found, defaulting to SGMII\n");
261 if (priv->if_type < 0)
262 priv->if_type = PHY_INTERFACE_MODE_NONE;
263
264 switch (priv->if_type) {
265 case PHY_INTERFACE_MODE_SGMII:
Alex Marginean9bc07e812019-07-15 11:48:47 +0300266 case PHY_INTERFACE_MODE_SGMII_2500:
Alex Margineane4aafd52019-07-03 12:11:42 +0300267 enetc_init_sgmii(dev);
268 break;
Alex Margineane4aafd52019-07-03 12:11:42 +0300269 case PHY_INTERFACE_MODE_XGMII:
Alex Margineane22e3af2019-11-14 18:28:38 +0200270 case PHY_INTERFACE_MODE_USXGMII:
271 case PHY_INTERFACE_MODE_XFI:
Alex Margineane4aafd52019-07-03 12:11:42 +0300272 enetc_init_sxgmii(dev);
273 break;
274 };
275}
276
Alex Marginean1d995342019-07-03 12:11:41 +0300277/* Configure the actual/external ethernet PHY, if one is found */
Alex Marginean17bd7ea2019-11-25 17:15:13 +0200278static void enetc_config_phy(struct udevice *dev)
Alex Marginean1d995342019-07-03 12:11:41 +0300279{
280 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean1d995342019-07-03 12:11:41 +0300281 int supported;
282
Alex Marginean17bd7ea2019-11-25 17:15:13 +0200283 priv->phy = dm_eth_phy_connect(dev);
Alex Marginean1d995342019-07-03 12:11:41 +0300284
Alex Marginean17bd7ea2019-11-25 17:15:13 +0200285 if (!priv->phy)
Alex Marginean1d995342019-07-03 12:11:41 +0300286 return;
Alex Marginean1d995342019-07-03 12:11:41 +0300287
Alex Marginean307f8a62019-11-14 18:58:45 +0200288 supported = PHY_GBIT_FEATURES | SUPPORTED_2500baseX_Full;
289 priv->phy->supported &= supported;
290 priv->phy->advertising &= supported;
Alex Marginean17bd7ea2019-11-25 17:15:13 +0200291
292 phy_config(priv->phy);
Alex Marginean1d995342019-07-03 12:11:41 +0300293}
294
Alex Marginean120b5ef2019-07-03 12:11:40 +0300295/*
296 * Probe ENETC driver:
297 * - initialize port and station interface BARs
298 */
299static int enetc_probe(struct udevice *dev)
300{
301 struct enetc_priv *priv = dev_get_priv(dev);
302
303 if (ofnode_valid(dev->node) && !ofnode_is_available(dev->node)) {
304 enetc_dbg(dev, "interface disabled\n");
305 return -ENODEV;
306 }
307
308 priv->enetc_txbd = memalign(ENETC_BD_ALIGN,
309 sizeof(struct enetc_tx_bd) * ENETC_BD_CNT);
310 priv->enetc_rxbd = memalign(ENETC_BD_ALIGN,
311 sizeof(union enetc_rx_bd) * ENETC_BD_CNT);
312
313 if (!priv->enetc_txbd || !priv->enetc_rxbd) {
314 /* free should be able to handle NULL, just free all pointers */
315 free(priv->enetc_txbd);
316 free(priv->enetc_rxbd);
317
318 return -ENOMEM;
319 }
320
321 /* initialize register */
322 priv->regs_base = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0, 0);
323 if (!priv->regs_base) {
324 enetc_dbg(dev, "failed to map BAR0\n");
325 return -EINVAL;
326 }
327 priv->port_regs = priv->regs_base + ENETC_PORT_REGS_OFF;
328
329 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
330
Alex Margineana931f782019-11-14 18:58:46 +0200331 enetc_start_pcs(dev);
332 enetc_config_phy(dev);
333
Alex Marginean120b5ef2019-07-03 12:11:40 +0300334 return 0;
335}
336
337/*
338 * Remove the driver from an interface:
339 * - free up allocated memory
340 */
341static int enetc_remove(struct udevice *dev)
342{
343 struct enetc_priv *priv = dev_get_priv(dev);
344
345 free(priv->enetc_txbd);
346 free(priv->enetc_rxbd);
347
348 return 0;
349}
350
Michael Walle42c66f02019-12-20 14:16:48 +0100351/*
352 * LS1028A is the only part with IERB at this time and there are plans to
353 * change its structure, keep this LS1028A specific for now.
354 */
355#define LS1028A_IERB_BASE 0x1f0800000ULL
356#define LS1028A_IERB_PSIPMAR0(pf, vf) (LS1028A_IERB_BASE + 0x8000 \
357 + (pf) * 0x100 + (vf) * 8)
358#define LS1028A_IERB_PSIPMAR1(pf, vf) (LS1028A_IERB_PSIPMAR0(pf, vf) + 4)
359
360static int enetc_ls1028a_write_hwaddr(struct udevice *dev)
361{
362 struct pci_child_platdata *ppdata = dev_get_parent_platdata(dev);
363 const int devfn_to_pf[] = {0, 1, 2, -1, -1, -1, 3};
364 struct eth_pdata *plat = dev_get_platdata(dev);
365 int devfn = PCI_FUNC(ppdata->devfn);
366 u8 *addr = plat->enetaddr;
367 u32 lower, upper;
368 int pf;
369
370 if (devfn >= ARRAY_SIZE(devfn_to_pf))
371 return 0;
372
373 pf = devfn_to_pf[devfn];
374 if (pf < 0)
375 return 0;
376
377 lower = *(const u16 *)(addr + 4);
378 upper = *(const u32 *)addr;
379
380 out_le32(LS1028A_IERB_PSIPMAR0(pf, 0), upper);
381 out_le32(LS1028A_IERB_PSIPMAR1(pf, 0), lower);
382
383 return 0;
384}
385
Michael Walleee5c70b2019-12-20 14:16:47 +0100386static int enetc_write_hwaddr(struct udevice *dev)
Alex Marginean120b5ef2019-07-03 12:11:40 +0300387{
Michael Walleee5c70b2019-12-20 14:16:47 +0100388 struct eth_pdata *plat = dev_get_platdata(dev);
389 struct enetc_priv *priv = dev_get_priv(dev);
390 u8 *addr = plat->enetaddr;
391
Michael Walle42c66f02019-12-20 14:16:48 +0100392 if (IS_ENABLED(CONFIG_ARCH_LS1028A))
393 return enetc_ls1028a_write_hwaddr(dev);
394
Alex Marginean120b5ef2019-07-03 12:11:40 +0300395 u16 lower = *(const u16 *)(addr + 4);
396 u32 upper = *(const u32 *)addr;
397
398 enetc_write_port(priv, ENETC_PSIPMAR0, upper);
399 enetc_write_port(priv, ENETC_PSIPMAR1, lower);
Michael Walleee5c70b2019-12-20 14:16:47 +0100400
401 return 0;
Alex Marginean120b5ef2019-07-03 12:11:40 +0300402}
403
404/* Configure port parameters (# of rings, frame size, enable port) */
405static void enetc_enable_si_port(struct enetc_priv *priv)
406{
407 u32 val;
408
409 /* set Rx/Tx BDR count */
410 val = ENETC_PSICFGR_SET_TXBDR(ENETC_TX_BDR_CNT);
411 val |= ENETC_PSICFGR_SET_RXBDR(ENETC_RX_BDR_CNT);
412 enetc_write_port(priv, ENETC_PSICFGR(0), val);
413 /* set Rx max frame size */
414 enetc_write_port(priv, ENETC_PM_MAXFRM, ENETC_RX_MAXFRM_SIZE);
415 /* enable MAC port */
416 enetc_write_port(priv, ENETC_PM_CC, ENETC_PM_CC_RX_TX_EN);
417 /* enable port */
418 enetc_write_port(priv, ENETC_PMR, ENETC_PMR_SI0_EN);
419 /* set SI cache policy */
420 enetc_write(priv, ENETC_SICAR0,
421 ENETC_SICAR_RD_CFG | ENETC_SICAR_WR_CFG);
422 /* enable SI */
423 enetc_write(priv, ENETC_SIMR, ENETC_SIMR_EN);
424}
425
426/* returns DMA address for a given buffer index */
427static inline u64 enetc_rxb_address(struct udevice *dev, int i)
428{
429 return cpu_to_le64(dm_pci_virt_to_mem(dev, net_rx_packets[i]));
430}
431
432/*
433 * Setup a single Tx BD Ring (ID = 0):
434 * - set Tx buffer descriptor address
435 * - set the BD count
436 * - initialize the producer and consumer index
437 */
438static void enetc_setup_tx_bdr(struct udevice *dev)
439{
440 struct enetc_priv *priv = dev_get_priv(dev);
441 struct bd_ring *tx_bdr = &priv->tx_bdr;
442 u64 tx_bd_add = (u64)priv->enetc_txbd;
443
444 /* used later to advance to the next Tx BD */
445 tx_bdr->bd_count = ENETC_BD_CNT;
446 tx_bdr->next_prod_idx = 0;
447 tx_bdr->next_cons_idx = 0;
448 tx_bdr->cons_idx = priv->regs_base +
449 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBCIR);
450 tx_bdr->prod_idx = priv->regs_base +
451 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBPIR);
452
453 /* set Tx BD address */
454 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR0,
455 lower_32_bits(tx_bd_add));
456 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR1,
457 upper_32_bits(tx_bd_add));
458 /* set Tx 8 BD count */
459 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBLENR,
460 tx_bdr->bd_count);
461
462 /* reset both producer/consumer indexes */
463 enetc_write_reg(tx_bdr->cons_idx, tx_bdr->next_cons_idx);
464 enetc_write_reg(tx_bdr->prod_idx, tx_bdr->next_prod_idx);
465
466 /* enable TX ring */
467 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBMR, ENETC_TBMR_EN);
468}
469
470/*
471 * Setup a single Rx BD Ring (ID = 0):
472 * - set Rx buffer descriptors address (one descriptor per buffer)
473 * - set buffer size as max frame size
474 * - enable Rx ring
475 * - reset consumer and producer indexes
476 * - set buffer for each descriptor
477 */
478static void enetc_setup_rx_bdr(struct udevice *dev)
479{
480 struct enetc_priv *priv = dev_get_priv(dev);
481 struct bd_ring *rx_bdr = &priv->rx_bdr;
482 u64 rx_bd_add = (u64)priv->enetc_rxbd;
483 int i;
484
485 /* used later to advance to the next BD produced by ENETC HW */
486 rx_bdr->bd_count = ENETC_BD_CNT;
487 rx_bdr->next_prod_idx = 0;
488 rx_bdr->next_cons_idx = 0;
489 rx_bdr->cons_idx = priv->regs_base +
490 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBCIR);
491 rx_bdr->prod_idx = priv->regs_base +
492 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBPIR);
493
494 /* set Rx BD address */
495 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR0,
496 lower_32_bits(rx_bd_add));
497 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR1,
498 upper_32_bits(rx_bd_add));
499 /* set Rx BD count (multiple of 8) */
500 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBLENR,
501 rx_bdr->bd_count);
502 /* set Rx buffer size */
503 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBSR, PKTSIZE_ALIGN);
504
505 /* fill Rx BD */
506 memset(priv->enetc_rxbd, 0,
507 rx_bdr->bd_count * sizeof(union enetc_rx_bd));
508 for (i = 0; i < rx_bdr->bd_count; i++) {
509 priv->enetc_rxbd[i].w.addr = enetc_rxb_address(dev, i);
510 /* each RX buffer must be aligned to 64B */
511 WARN_ON(priv->enetc_rxbd[i].w.addr & (ARCH_DMA_MINALIGN - 1));
512 }
513
514 /* reset producer (ENETC owned) and consumer (SW owned) index */
515 enetc_write_reg(rx_bdr->cons_idx, rx_bdr->next_cons_idx);
516 enetc_write_reg(rx_bdr->prod_idx, rx_bdr->next_prod_idx);
517
518 /* enable Rx ring */
519 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBMR, ENETC_RBMR_EN);
520}
521
522/*
523 * Start ENETC interface:
524 * - perform FLR
525 * - enable access to port and SI registers
526 * - set mac address
527 * - setup TX/RX buffer descriptors
528 * - enable Tx/Rx rings
529 */
530static int enetc_start(struct udevice *dev)
531{
Alex Marginean120b5ef2019-07-03 12:11:40 +0300532 struct enetc_priv *priv = dev_get_priv(dev);
533
534 /* reset and enable the PCI device */
535 dm_pci_flr(dev);
536 dm_pci_clrset_config16(dev, PCI_COMMAND, 0,
537 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
538
Alex Marginean120b5ef2019-07-03 12:11:40 +0300539 enetc_enable_si_port(priv);
540
541 /* setup Tx/Rx buffer descriptors */
542 enetc_setup_tx_bdr(dev);
543 enetc_setup_rx_bdr(dev);
544
Alex Margineanb8e4eec2020-01-10 23:32:20 +0200545 enetc_setup_mac_iface(dev);
Alex Margineana931f782019-11-14 18:58:46 +0200546
Alex Marginean17bd7ea2019-11-25 17:15:13 +0200547 if (priv->phy)
548 phy_startup(priv->phy);
Alex Marginean1d995342019-07-03 12:11:41 +0300549
Alex Marginean120b5ef2019-07-03 12:11:40 +0300550 return 0;
551}
552
553/*
554 * Stop the network interface:
555 * - just quiesce it, we can wipe all configuration as _start starts from
556 * scratch each time
557 */
558static void enetc_stop(struct udevice *dev)
559{
560 /* FLR is sufficient to quiesce the device */
561 dm_pci_flr(dev);
Alex Marginean1e354cb2019-11-25 17:57:27 +0200562 /* leave the BARs accessible after we stop, this is needed to use
563 * internal MDIO in command line.
564 */
565 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
Alex Marginean120b5ef2019-07-03 12:11:40 +0300566}
567
568/*
569 * ENETC transmit packet:
570 * - check if Tx BD ring is full
571 * - set buffer/packet address (dma address)
572 * - set final fragment flag
573 * - try while producer index equals consumer index or timeout
574 */
575static int enetc_send(struct udevice *dev, void *packet, int length)
576{
577 struct enetc_priv *priv = dev_get_priv(dev);
578 struct bd_ring *txr = &priv->tx_bdr;
579 void *nv_packet = (void *)packet;
580 int tries = ENETC_POLL_TRIES;
581 u32 pi, ci;
582
583 pi = txr->next_prod_idx;
584 ci = enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK;
585 /* Tx ring is full when */
586 if (((pi + 1) % txr->bd_count) == ci) {
587 enetc_dbg(dev, "Tx BDR full\n");
588 return -ETIMEDOUT;
589 }
590 enetc_dbg(dev, "TxBD[%d]send: pkt_len=%d, buff @0x%x%08x\n", pi, length,
591 upper_32_bits((u64)nv_packet), lower_32_bits((u64)nv_packet));
592
593 /* prepare Tx BD */
594 memset(&priv->enetc_txbd[pi], 0x0, sizeof(struct enetc_tx_bd));
595 priv->enetc_txbd[pi].addr =
596 cpu_to_le64(dm_pci_virt_to_mem(dev, nv_packet));
597 priv->enetc_txbd[pi].buf_len = cpu_to_le16(length);
598 priv->enetc_txbd[pi].frm_len = cpu_to_le16(length);
599 priv->enetc_txbd[pi].flags = cpu_to_le16(ENETC_TXBD_FLAGS_F);
600 dmb();
601 /* send frame: increment producer index */
602 pi = (pi + 1) % txr->bd_count;
603 txr->next_prod_idx = pi;
604 enetc_write_reg(txr->prod_idx, pi);
605 while ((--tries >= 0) &&
606 (pi != (enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK)))
607 udelay(10);
608
609 return tries > 0 ? 0 : -ETIMEDOUT;
610}
611
612/*
613 * Receive frame:
614 * - wait for the next BD to get ready bit set
615 * - clean up the descriptor
616 * - move on and indicate to HW that the cleaned BD is available for Rx
617 */
618static int enetc_recv(struct udevice *dev, int flags, uchar **packetp)
619{
620 struct enetc_priv *priv = dev_get_priv(dev);
621 struct bd_ring *rxr = &priv->rx_bdr;
622 int tries = ENETC_POLL_TRIES;
623 int pi = rxr->next_prod_idx;
624 int ci = rxr->next_cons_idx;
625 u32 status;
626 int len;
627 u8 rdy;
628
629 do {
630 dmb();
631 status = le32_to_cpu(priv->enetc_rxbd[pi].r.lstatus);
632 /* check if current BD is ready to be consumed */
633 rdy = ENETC_RXBD_STATUS_R(status);
634 } while (--tries >= 0 && !rdy);
635
636 if (!rdy)
637 return -EAGAIN;
638
639 dmb();
640 len = le16_to_cpu(priv->enetc_rxbd[pi].r.buf_len);
641 *packetp = (uchar *)enetc_rxb_address(dev, pi);
642 enetc_dbg(dev, "RxBD[%d]: len=%d err=%d pkt=0x%x%08x\n", pi, len,
643 ENETC_RXBD_STATUS_ERRORS(status),
644 upper_32_bits((u64)*packetp), lower_32_bits((u64)*packetp));
645
646 /* BD clean up and advance to next in ring */
647 memset(&priv->enetc_rxbd[pi], 0, sizeof(union enetc_rx_bd));
648 priv->enetc_rxbd[pi].w.addr = enetc_rxb_address(dev, pi);
649 rxr->next_prod_idx = (pi + 1) % rxr->bd_count;
650 ci = (ci + 1) % rxr->bd_count;
651 rxr->next_cons_idx = ci;
652 dmb();
653 /* free up the slot in the ring for HW */
654 enetc_write_reg(rxr->cons_idx, ci);
655
656 return len;
657}
658
659static const struct eth_ops enetc_ops = {
660 .start = enetc_start,
661 .send = enetc_send,
662 .recv = enetc_recv,
663 .stop = enetc_stop,
Michael Walleee5c70b2019-12-20 14:16:47 +0100664 .write_hwaddr = enetc_write_hwaddr,
Alex Marginean120b5ef2019-07-03 12:11:40 +0300665};
666
667U_BOOT_DRIVER(eth_enetc) = {
Alex Marginean9c2aee12019-12-10 16:55:39 +0200668 .name = ENETC_DRIVER_NAME,
Alex Marginean120b5ef2019-07-03 12:11:40 +0300669 .id = UCLASS_ETH,
670 .bind = enetc_bind,
671 .probe = enetc_probe,
672 .remove = enetc_remove,
673 .ops = &enetc_ops,
674 .priv_auto_alloc_size = sizeof(struct enetc_priv),
675 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
676};
677
678static struct pci_device_id enetc_ids[] = {
679 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, PCI_DEVICE_ID_ENETC_ETH) },
680 {}
681};
682
683U_BOOT_PCI_DEVICE(eth_enetc, enetc_ids);