net: enetc: require a PHY device when probing
[platform/kernel/u-boot.git] / drivers / net / fsl_enetc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * ENETC ethernet controller driver
4  * Copyright 2017-2021 NXP
5  */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <fdt_support.h>
11 #include <malloc.h>
12 #include <memalign.h>
13 #include <net.h>
14 #include <asm/cache.h>
15 #include <asm/io.h>
16 #include <pci.h>
17 #include <miiphy.h>
18 #include <linux/bug.h>
19 #include <linux/delay.h>
20
21 #include "fsl_enetc.h"
22
23 #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  */
29 static 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
41 static 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 */
55 void fdt_fixup_enetc_mac(void *blob)
56 {
57         struct pci_child_plat *ppdata;
58         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
71                 pdata = dev_get_plat(dev);
72                 ppdata = dev_get_parent_plat(dev);
73                 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
86 /*
87  * Bind the device:
88  * - set a more explicit name on the interface
89  */
90 static 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          */
102         if (ofnode_valid(dev_ofnode(dev)))
103                 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
111 /* MDIO wrappers, we're using these to drive internal MDIO to get to serdes */
112 static 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
120 static 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 */
130 static 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 */
138 static int enetc_init_sgmii(struct udevice *dev)
139 {
140         struct enetc_priv *priv = dev_get_priv(dev);
141         bool is2500 = false;
142         u16 reg;
143
144         if (!enetc_has_imdio(dev))
145                 return 0;
146
147         if (priv->if_type == PHY_INTERFACE_MODE_SGMII_2500)
148                 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;
158         enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
159                          ENETC_PCS_IF_MODE, reg);
160
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
171         reg = ENETC_PCS_CR_DEF_VAL;
172         reg |= is2500 ? ENETC_PCS_CR_RST : ENETC_PCS_CR_RESET_AN;
173         /* restart PCS AN */
174         enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
175                          ENETC_PCS_CR, reg);
176
177         return 0;
178 }
179
180 /* set up MAC for RGMII */
181 static int enetc_init_rgmii(struct udevice *dev)
182 {
183         struct enetc_priv *priv = dev_get_priv(dev);
184         u32 if_mode;
185
186         /* enable RGMII AN */
187         if_mode = enetc_read_port(priv, ENETC_PM_IF_MODE);
188         if_mode |= ENETC_PM_IF_MODE_AN_ENA;
189         enetc_write_port(priv, ENETC_PM_IF_MODE, if_mode);
190
191         return 0;
192 }
193
194 /* set up MAC configuration for the given interface type */
195 static void enetc_setup_mac_iface(struct udevice *dev)
196 {
197         struct enetc_priv *priv = dev_get_priv(dev);
198         u32 if_mode;
199
200         switch (priv->if_type) {
201         case PHY_INTERFACE_MODE_RGMII:
202         case PHY_INTERFACE_MODE_RGMII_ID:
203         case PHY_INTERFACE_MODE_RGMII_RXID:
204         case PHY_INTERFACE_MODE_RGMII_TXID:
205                 enetc_init_rgmii(dev);
206                 break;
207         case PHY_INTERFACE_MODE_XGMII:
208         case PHY_INTERFACE_MODE_USXGMII:
209         case PHY_INTERFACE_MODE_XFI:
210                 /* set ifmode to (US)XGMII */
211                 if_mode = enetc_read_port(priv, ENETC_PM_IF_MODE);
212                 if_mode &= ~ENETC_PM_IF_IFMODE_MASK;
213                 enetc_write_port(priv, ENETC_PM_IF_MODE, if_mode);
214                 break;
215         };
216 }
217
218 /* set up serdes for SXGMII */
219 static int enetc_init_sxgmii(struct udevice *dev)
220 {
221         struct enetc_priv *priv = dev_get_priv(dev);
222
223         if (!enetc_has_imdio(dev))
224                 return 0;
225
226         /* Dev ability - SXGMII */
227         enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
228                          ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SXGMII);
229
230         /* Restart PCS AN */
231         enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
232                          ENETC_PCS_CR,
233                          ENETC_PCS_CR_RST | ENETC_PCS_CR_RESET_AN);
234
235         return 0;
236 }
237
238 /* Apply protocol specific configuration to MAC, serdes as needed */
239 static void enetc_start_pcs(struct udevice *dev)
240 {
241         struct enetc_priv *priv = dev_get_priv(dev);
242         const char *if_str;
243
244         priv->if_type = PHY_INTERFACE_MODE_NONE;
245
246         /* register internal MDIO for debug purposes */
247         if (enetc_read_port(priv, ENETC_PCAPR0) & ENETC_PCAPRO_MDIO) {
248                 priv->imdio.read = enetc_mdio_read;
249                 priv->imdio.write = enetc_mdio_write;
250                 priv->imdio.priv = priv->port_regs + ENETC_PM_IMDIO_BASE;
251                 strncpy(priv->imdio.name, dev->name, MDIO_NAME_LEN);
252                 if (!miiphy_get_dev_by_name(priv->imdio.name))
253                         mdio_register(&priv->imdio);
254         }
255
256         if (!ofnode_valid(dev_ofnode(dev))) {
257                 enetc_dbg(dev, "no enetc ofnode found, skipping PCS set-up\n");
258                 return;
259         }
260
261         if_str = ofnode_read_string(dev_ofnode(dev), "phy-mode");
262         if (if_str)
263                 priv->if_type = phy_get_interface_by_name(if_str);
264         else
265                 enetc_dbg(dev,
266                           "phy-mode property not found, defaulting to SGMII\n");
267         if (priv->if_type < 0)
268                 priv->if_type = PHY_INTERFACE_MODE_NONE;
269
270         switch (priv->if_type) {
271         case PHY_INTERFACE_MODE_SGMII:
272         case PHY_INTERFACE_MODE_SGMII_2500:
273                 enetc_init_sgmii(dev);
274                 break;
275         case PHY_INTERFACE_MODE_XGMII:
276         case PHY_INTERFACE_MODE_USXGMII:
277         case PHY_INTERFACE_MODE_XFI:
278                 enetc_init_sxgmii(dev);
279                 break;
280         };
281 }
282
283 /* Configure the actual/external ethernet PHY, if one is found */
284 static int enetc_config_phy(struct udevice *dev)
285 {
286         struct enetc_priv *priv = dev_get_priv(dev);
287         int supported;
288
289         priv->phy = dm_eth_phy_connect(dev);
290         if (!priv->phy)
291                 return -ENODEV;
292
293         supported = PHY_GBIT_FEATURES | SUPPORTED_2500baseX_Full;
294         priv->phy->supported &= supported;
295         priv->phy->advertising &= supported;
296
297         return phy_config(priv->phy);
298 }
299
300 /*
301  * Probe ENETC driver:
302  * - initialize port and station interface BARs
303  */
304 static int enetc_probe(struct udevice *dev)
305 {
306         struct enetc_priv *priv = dev_get_priv(dev);
307
308         if (ofnode_valid(dev_ofnode(dev)) && !ofnode_is_available(dev_ofnode(dev))) {
309                 enetc_dbg(dev, "interface disabled\n");
310                 return -ENODEV;
311         }
312
313         priv->enetc_txbd = memalign(ENETC_BD_ALIGN,
314                                     sizeof(struct enetc_tx_bd) * ENETC_BD_CNT);
315         priv->enetc_rxbd = memalign(ENETC_BD_ALIGN,
316                                     sizeof(union enetc_rx_bd) * ENETC_BD_CNT);
317
318         if (!priv->enetc_txbd || !priv->enetc_rxbd) {
319                 /* free should be able to handle NULL, just free all pointers */
320                 free(priv->enetc_txbd);
321                 free(priv->enetc_rxbd);
322
323                 return -ENOMEM;
324         }
325
326         /* initialize register */
327         priv->regs_base = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0, 0);
328         if (!priv->regs_base) {
329                 enetc_dbg(dev, "failed to map BAR0\n");
330                 return -EINVAL;
331         }
332         priv->port_regs = priv->regs_base + ENETC_PORT_REGS_OFF;
333
334         dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
335
336         enetc_start_pcs(dev);
337
338         return enetc_config_phy(dev);
339 }
340
341 /*
342  * Remove the driver from an interface:
343  * - free up allocated memory
344  */
345 static int enetc_remove(struct udevice *dev)
346 {
347         struct enetc_priv *priv = dev_get_priv(dev);
348
349         free(priv->enetc_txbd);
350         free(priv->enetc_rxbd);
351
352         return 0;
353 }
354
355 /*
356  * LS1028A is the only part with IERB at this time and there are plans to
357  * change its structure, keep this LS1028A specific for now.
358  */
359 #define LS1028A_IERB_BASE               0x1f0800000ULL
360 #define LS1028A_IERB_PSIPMAR0(pf, vf)   (LS1028A_IERB_BASE + 0x8000 \
361                                          + (pf) * 0x100 + (vf) * 8)
362 #define LS1028A_IERB_PSIPMAR1(pf, vf)   (LS1028A_IERB_PSIPMAR0(pf, vf) + 4)
363
364 static int enetc_ls1028a_write_hwaddr(struct udevice *dev)
365 {
366         struct pci_child_plat *ppdata = dev_get_parent_plat(dev);
367         const int devfn_to_pf[] = {0, 1, 2, -1, -1, -1, 3};
368         struct eth_pdata *plat = dev_get_plat(dev);
369         int devfn = PCI_FUNC(ppdata->devfn);
370         u8 *addr = plat->enetaddr;
371         u32 lower, upper;
372         int pf;
373
374         if (devfn >= ARRAY_SIZE(devfn_to_pf))
375                 return 0;
376
377         pf = devfn_to_pf[devfn];
378         if (pf < 0)
379                 return 0;
380
381         lower = *(const u16 *)(addr + 4);
382         upper = *(const u32 *)addr;
383
384         out_le32(LS1028A_IERB_PSIPMAR0(pf, 0), upper);
385         out_le32(LS1028A_IERB_PSIPMAR1(pf, 0), lower);
386
387         return 0;
388 }
389
390 static int enetc_write_hwaddr(struct udevice *dev)
391 {
392         struct eth_pdata *plat = dev_get_plat(dev);
393         struct enetc_priv *priv = dev_get_priv(dev);
394         u8 *addr = plat->enetaddr;
395
396         if (IS_ENABLED(CONFIG_ARCH_LS1028A))
397                 return enetc_ls1028a_write_hwaddr(dev);
398
399         u16 lower = *(const u16 *)(addr + 4);
400         u32 upper = *(const u32 *)addr;
401
402         enetc_write_port(priv, ENETC_PSIPMAR0, upper);
403         enetc_write_port(priv, ENETC_PSIPMAR1, lower);
404
405         return 0;
406 }
407
408 /* Configure port parameters (# of rings, frame size, enable port) */
409 static void enetc_enable_si_port(struct enetc_priv *priv)
410 {
411         u32 val;
412
413         /* set Rx/Tx BDR count */
414         val = ENETC_PSICFGR_SET_TXBDR(ENETC_TX_BDR_CNT);
415         val |= ENETC_PSICFGR_SET_RXBDR(ENETC_RX_BDR_CNT);
416         enetc_write_port(priv, ENETC_PSICFGR(0), val);
417         /* set Rx max frame size */
418         enetc_write_port(priv, ENETC_PM_MAXFRM, ENETC_RX_MAXFRM_SIZE);
419         /* enable MAC port */
420         enetc_write_port(priv, ENETC_PM_CC, ENETC_PM_CC_RX_TX_EN);
421         /* enable port */
422         enetc_write_port(priv, ENETC_PMR, ENETC_PMR_SI0_EN);
423         /* set SI cache policy */
424         enetc_write(priv, ENETC_SICAR0,
425                     ENETC_SICAR_RD_CFG | ENETC_SICAR_WR_CFG);
426         /* enable SI */
427         enetc_write(priv, ENETC_SIMR, ENETC_SIMR_EN);
428 }
429
430 /* returns DMA address for a given buffer index */
431 static inline u64 enetc_rxb_address(struct udevice *dev, int i)
432 {
433         return cpu_to_le64(dm_pci_virt_to_mem(dev, net_rx_packets[i]));
434 }
435
436 /*
437  * Setup a single Tx BD Ring (ID = 0):
438  * - set Tx buffer descriptor address
439  * - set the BD count
440  * - initialize the producer and consumer index
441  */
442 static void enetc_setup_tx_bdr(struct udevice *dev)
443 {
444         struct enetc_priv *priv = dev_get_priv(dev);
445         struct bd_ring *tx_bdr = &priv->tx_bdr;
446         u64 tx_bd_add = (u64)priv->enetc_txbd;
447
448         /* used later to advance to the next Tx BD */
449         tx_bdr->bd_count = ENETC_BD_CNT;
450         tx_bdr->next_prod_idx = 0;
451         tx_bdr->next_cons_idx = 0;
452         tx_bdr->cons_idx = priv->regs_base +
453                                 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBCIR);
454         tx_bdr->prod_idx = priv->regs_base +
455                                 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBPIR);
456
457         /* set Tx BD address */
458         enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR0,
459                         lower_32_bits(tx_bd_add));
460         enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR1,
461                         upper_32_bits(tx_bd_add));
462         /* set Tx 8 BD count */
463         enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBLENR,
464                         tx_bdr->bd_count);
465
466         /* reset both producer/consumer indexes */
467         enetc_write_reg(tx_bdr->cons_idx, tx_bdr->next_cons_idx);
468         enetc_write_reg(tx_bdr->prod_idx, tx_bdr->next_prod_idx);
469
470         /* enable TX ring */
471         enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBMR, ENETC_TBMR_EN);
472 }
473
474 /*
475  * Setup a single Rx BD Ring (ID = 0):
476  * - set Rx buffer descriptors address (one descriptor per buffer)
477  * - set buffer size as max frame size
478  * - enable Rx ring
479  * - reset consumer and producer indexes
480  * - set buffer for each descriptor
481  */
482 static void enetc_setup_rx_bdr(struct udevice *dev)
483 {
484         struct enetc_priv *priv = dev_get_priv(dev);
485         struct bd_ring *rx_bdr = &priv->rx_bdr;
486         u64 rx_bd_add = (u64)priv->enetc_rxbd;
487         int i;
488
489         /* used later to advance to the next BD produced by ENETC HW */
490         rx_bdr->bd_count = ENETC_BD_CNT;
491         rx_bdr->next_prod_idx = 0;
492         rx_bdr->next_cons_idx = 0;
493         rx_bdr->cons_idx = priv->regs_base +
494                                 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBCIR);
495         rx_bdr->prod_idx = priv->regs_base +
496                                 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBPIR);
497
498         /* set Rx BD address */
499         enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR0,
500                         lower_32_bits(rx_bd_add));
501         enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR1,
502                         upper_32_bits(rx_bd_add));
503         /* set Rx BD count (multiple of 8) */
504         enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBLENR,
505                         rx_bdr->bd_count);
506         /* set Rx buffer  size */
507         enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBSR, PKTSIZE_ALIGN);
508
509         /* fill Rx BD */
510         memset(priv->enetc_rxbd, 0,
511                rx_bdr->bd_count * sizeof(union enetc_rx_bd));
512         for (i = 0; i < rx_bdr->bd_count; i++) {
513                 priv->enetc_rxbd[i].w.addr = enetc_rxb_address(dev, i);
514                 /* each RX buffer must be aligned to 64B */
515                 WARN_ON(priv->enetc_rxbd[i].w.addr & (ARCH_DMA_MINALIGN - 1));
516         }
517
518         /* reset producer (ENETC owned) and consumer (SW owned) index */
519         enetc_write_reg(rx_bdr->cons_idx, rx_bdr->next_cons_idx);
520         enetc_write_reg(rx_bdr->prod_idx, rx_bdr->next_prod_idx);
521
522         /* enable Rx ring */
523         enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBMR, ENETC_RBMR_EN);
524 }
525
526 /*
527  * Start ENETC interface:
528  * - perform FLR
529  * - enable access to port and SI registers
530  * - set mac address
531  * - setup TX/RX buffer descriptors
532  * - enable Tx/Rx rings
533  */
534 static int enetc_start(struct udevice *dev)
535 {
536         struct enetc_priv *priv = dev_get_priv(dev);
537
538         /* reset and enable the PCI device */
539         dm_pci_flr(dev);
540         dm_pci_clrset_config16(dev, PCI_COMMAND, 0,
541                                PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
542
543         enetc_enable_si_port(priv);
544
545         /* setup Tx/Rx buffer descriptors */
546         enetc_setup_tx_bdr(dev);
547         enetc_setup_rx_bdr(dev);
548
549         enetc_setup_mac_iface(dev);
550
551         phy_startup(priv->phy);
552
553         return 0;
554 }
555
556 /*
557  * Stop the network interface:
558  * - just quiesce it, we can wipe all configuration as _start starts from
559  * scratch each time
560  */
561 static void enetc_stop(struct udevice *dev)
562 {
563         /* FLR is sufficient to quiesce the device */
564         dm_pci_flr(dev);
565         /* leave the BARs accessible after we stop, this is needed to use
566          * internal MDIO in command line.
567          */
568         dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
569 }
570
571 /*
572  * ENETC transmit packet:
573  * - check if Tx BD ring is full
574  * - set buffer/packet address (dma address)
575  * - set final fragment flag
576  * - try while producer index equals consumer index or timeout
577  */
578 static int enetc_send(struct udevice *dev, void *packet, int length)
579 {
580         struct enetc_priv *priv = dev_get_priv(dev);
581         struct bd_ring *txr = &priv->tx_bdr;
582         void *nv_packet = (void *)packet;
583         int tries = ENETC_POLL_TRIES;
584         u32 pi, ci;
585
586         pi = txr->next_prod_idx;
587         ci = enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK;
588         /* Tx ring is full when */
589         if (((pi + 1) % txr->bd_count) == ci) {
590                 enetc_dbg(dev, "Tx BDR full\n");
591                 return -ETIMEDOUT;
592         }
593         enetc_dbg(dev, "TxBD[%d]send: pkt_len=%d, buff @0x%x%08x\n", pi, length,
594                   upper_32_bits((u64)nv_packet), lower_32_bits((u64)nv_packet));
595
596         /* prepare Tx BD */
597         memset(&priv->enetc_txbd[pi], 0x0, sizeof(struct enetc_tx_bd));
598         priv->enetc_txbd[pi].addr =
599                 cpu_to_le64(dm_pci_virt_to_mem(dev, nv_packet));
600         priv->enetc_txbd[pi].buf_len = cpu_to_le16(length);
601         priv->enetc_txbd[pi].frm_len = cpu_to_le16(length);
602         priv->enetc_txbd[pi].flags = cpu_to_le16(ENETC_TXBD_FLAGS_F);
603         dmb();
604         /* send frame: increment producer index */
605         pi = (pi + 1) % txr->bd_count;
606         txr->next_prod_idx = pi;
607         enetc_write_reg(txr->prod_idx, pi);
608         while ((--tries >= 0) &&
609                (pi != (enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK)))
610                 udelay(10);
611
612         return tries > 0 ? 0 : -ETIMEDOUT;
613 }
614
615 /*
616  * Receive frame:
617  * - wait for the next BD to get ready bit set
618  * - clean up the descriptor
619  * - move on and indicate to HW that the cleaned BD is available for Rx
620  */
621 static int enetc_recv(struct udevice *dev, int flags, uchar **packetp)
622 {
623         struct enetc_priv *priv = dev_get_priv(dev);
624         struct bd_ring *rxr = &priv->rx_bdr;
625         int tries = ENETC_POLL_TRIES;
626         int pi = rxr->next_prod_idx;
627         int ci = rxr->next_cons_idx;
628         u32 status;
629         int len;
630         u8 rdy;
631
632         do {
633                 dmb();
634                 status = le32_to_cpu(priv->enetc_rxbd[pi].r.lstatus);
635                 /* check if current BD is ready to be consumed */
636                 rdy = ENETC_RXBD_STATUS_R(status);
637         } while (--tries >= 0 && !rdy);
638
639         if (!rdy)
640                 return -EAGAIN;
641
642         dmb();
643         len = le16_to_cpu(priv->enetc_rxbd[pi].r.buf_len);
644         *packetp = (uchar *)enetc_rxb_address(dev, pi);
645         enetc_dbg(dev, "RxBD[%d]: len=%d err=%d pkt=0x%x%08x\n", pi, len,
646                   ENETC_RXBD_STATUS_ERRORS(status),
647                   upper_32_bits((u64)*packetp), lower_32_bits((u64)*packetp));
648
649         /* BD clean up and advance to next in ring */
650         memset(&priv->enetc_rxbd[pi], 0, sizeof(union enetc_rx_bd));
651         priv->enetc_rxbd[pi].w.addr = enetc_rxb_address(dev, pi);
652         rxr->next_prod_idx = (pi + 1) % rxr->bd_count;
653         ci = (ci + 1) % rxr->bd_count;
654         rxr->next_cons_idx = ci;
655         dmb();
656         /* free up the slot in the ring for HW */
657         enetc_write_reg(rxr->cons_idx, ci);
658
659         return len;
660 }
661
662 static const struct eth_ops enetc_ops = {
663         .start  = enetc_start,
664         .send   = enetc_send,
665         .recv   = enetc_recv,
666         .stop   = enetc_stop,
667         .write_hwaddr = enetc_write_hwaddr,
668 };
669
670 U_BOOT_DRIVER(eth_enetc) = {
671         .name   = ENETC_DRIVER_NAME,
672         .id     = UCLASS_ETH,
673         .bind   = enetc_bind,
674         .probe  = enetc_probe,
675         .remove = enetc_remove,
676         .ops    = &enetc_ops,
677         .priv_auto      = sizeof(struct enetc_priv),
678         .plat_auto      = sizeof(struct eth_pdata),
679 };
680
681 static struct pci_device_id enetc_ids[] = {
682         { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, PCI_DEVICE_ID_ENETC_ETH) },
683         {}
684 };
685
686 U_BOOT_PCI_DEVICE(eth_enetc, enetc_ids);