blob: a7740deb6face082621fa8d9d42c01a80acf785e [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
17/*
18 * Bind the device:
19 * - set a more explicit name on the interface
20 */
21static int enetc_bind(struct udevice *dev)
22{
23 char name[16];
24 static int eth_num_devices;
25
26 /*
27 * prefer using PCI function numbers to number interfaces, but these
28 * are only available if dts nodes are present. For PCI they are
29 * optional, handle that case too. Just in case some nodes are present
30 * and some are not, use different naming scheme - enetc-N based on
31 * PCI function # and enetc#N based on interface count
32 */
33 if (ofnode_valid(dev->node))
34 sprintf(name, "enetc-%u", PCI_FUNC(pci_get_devfn(dev)));
35 else
36 sprintf(name, "enetc#%u", eth_num_devices++);
37 device_set_name(dev, name);
38
39 return 0;
40}
41
Alex Margineane4aafd52019-07-03 12:11:42 +030042/* MDIO wrappers, we're using these to drive internal MDIO to get to serdes */
43static int enetc_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
44{
45 struct enetc_mdio_priv priv;
46
47 priv.regs_base = bus->priv;
48 return enetc_mdio_read_priv(&priv, addr, devad, reg);
49}
50
51static int enetc_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
52 u16 val)
53{
54 struct enetc_mdio_priv priv;
55
56 priv.regs_base = bus->priv;
57 return enetc_mdio_write_priv(&priv, addr, devad, reg, val);
58}
59
60/* only interfaces that can pin out through serdes have internal MDIO */
61static bool enetc_has_imdio(struct udevice *dev)
62{
63 struct enetc_priv *priv = dev_get_priv(dev);
64
65 return !!(priv->imdio.priv);
66}
67
68/* set up serdes for SGMII */
69static int enetc_init_sgmii(struct udevice *dev)
70{
71 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean9bc07e812019-07-15 11:48:47 +030072 bool is2500 = false;
73 u16 reg;
Alex Margineane4aafd52019-07-03 12:11:42 +030074
75 if (!enetc_has_imdio(dev))
76 return 0;
77
Alex Marginean9bc07e812019-07-15 11:48:47 +030078 if (priv->if_type == PHY_INTERFACE_MODE_SGMII_2500)
79 is2500 = true;
80
81 /*
82 * Set to SGMII mode, for 1Gbps enable AN, for 2.5Gbps set fixed speed.
83 * Although fixed speed is 1Gbps, we could be running at 2.5Gbps based
84 * on PLL configuration. Setting 1G for 2.5G here is counter intuitive
85 * but intentional.
86 */
87 reg = ENETC_PCS_IF_MODE_SGMII;
88 reg |= is2500 ? ENETC_PCS_IF_MODE_SPEED_1G : ENETC_PCS_IF_MODE_SGMII_AN;
Alex Margineane4aafd52019-07-03 12:11:42 +030089 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean9bc07e812019-07-15 11:48:47 +030090 ENETC_PCS_IF_MODE, reg);
Alex Margineane4aafd52019-07-03 12:11:42 +030091
92 /* Dev ability - SGMII */
93 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
94 ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SGMII);
95
96 /* Adjust link timer for SGMII */
97 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
98 ENETC_PCS_LINK_TIMER1, ENETC_PCS_LINK_TIMER1_VAL);
99 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
100 ENETC_PCS_LINK_TIMER2, ENETC_PCS_LINK_TIMER2_VAL);
101
Alex Marginean9bc07e812019-07-15 11:48:47 +0300102 reg = ENETC_PCS_CR_DEF_VAL;
103 reg |= is2500 ? ENETC_PCS_CR_RST : ENETC_PCS_CR_RESET_AN;
Alex Margineane4aafd52019-07-03 12:11:42 +0300104 /* restart PCS AN */
105 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean9bc07e812019-07-15 11:48:47 +0300106 ENETC_PCS_CR, reg);
Alex Margineane4aafd52019-07-03 12:11:42 +0300107
108 return 0;
109}
110
111/* set up MAC for RGMII */
112static int enetc_init_rgmii(struct udevice *dev)
113{
114 struct enetc_priv *priv = dev_get_priv(dev);
115 u32 if_mode;
116
117 /* enable RGMII AN */
118 if_mode = enetc_read_port(priv, ENETC_PM_IF_MODE);
119 if_mode |= ENETC_PM_IF_MODE_AN_ENA;
120 enetc_write_port(priv, ENETC_PM_IF_MODE, if_mode);
121
122 return 0;
123}
124
125/* set up MAC and serdes for SXGMII */
126static int enetc_init_sxgmii(struct udevice *dev)
127{
128 struct enetc_priv *priv = dev_get_priv(dev);
129 u32 if_mode;
130
131 /* set ifmode to (US)XGMII */
132 if_mode = enetc_read_port(priv, ENETC_PM_IF_MODE);
133 if_mode &= ~ENETC_PM_IF_IFMODE_MASK;
134 enetc_write_port(priv, ENETC_PM_IF_MODE, if_mode);
135
136 if (!enetc_has_imdio(dev))
137 return 0;
138
139 /* Dev ability - SXGMII */
140 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
141 ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SXGMII);
142
143 /* Restart PCS AN */
144 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
145 ENETC_PCS_CR,
Alex Marginean9bc07e812019-07-15 11:48:47 +0300146 ENETC_PCS_CR_RST | ENETC_PCS_CR_RESET_AN);
Alex Margineane4aafd52019-07-03 12:11:42 +0300147
148 return 0;
149}
150
151/* Apply protocol specific configuration to MAC, serdes as needed */
152static void enetc_start_pcs(struct udevice *dev)
153{
154 struct enetc_priv *priv = dev_get_priv(dev);
155 const char *if_str;
156
157 priv->if_type = PHY_INTERFACE_MODE_NONE;
158
159 /* check internal mdio capability, not all ports need it */
160 if (enetc_read_port(priv, ENETC_PCAPR0) & ENETC_PCAPRO_MDIO) {
161 /*
162 * set up internal MDIO, this is part of ETH PCI function and is
163 * used to access serdes / internal SoC PHYs.
164 * We don't currently register it as a MDIO bus as it goes away
165 * when the interface is removed, so it can't practically be
166 * used in the console.
167 */
168 priv->imdio.read = enetc_mdio_read;
169 priv->imdio.write = enetc_mdio_write;
170 priv->imdio.priv = priv->port_regs + ENETC_PM_IMDIO_BASE;
171 strncpy(priv->imdio.name, dev->name, MDIO_NAME_LEN);
172 }
173
174 if (!ofnode_valid(dev->node)) {
175 enetc_dbg(dev, "no enetc ofnode found, skipping PCS set-up\n");
176 return;
177 }
178
179 if_str = ofnode_read_string(dev->node, "phy-mode");
180 if (if_str)
181 priv->if_type = phy_get_interface_by_name(if_str);
182 else
183 enetc_dbg(dev,
184 "phy-mode property not found, defaulting to SGMII\n");
185 if (priv->if_type < 0)
186 priv->if_type = PHY_INTERFACE_MODE_NONE;
187
188 switch (priv->if_type) {
189 case PHY_INTERFACE_MODE_SGMII:
Alex Marginean9bc07e812019-07-15 11:48:47 +0300190 case PHY_INTERFACE_MODE_SGMII_2500:
Alex Margineane4aafd52019-07-03 12:11:42 +0300191 enetc_init_sgmii(dev);
192 break;
193 case PHY_INTERFACE_MODE_RGMII:
Michael Walle29a66172019-10-26 02:39:12 +0200194 case PHY_INTERFACE_MODE_RGMII_ID:
195 case PHY_INTERFACE_MODE_RGMII_RXID:
196 case PHY_INTERFACE_MODE_RGMII_TXID:
Alex Margineane4aafd52019-07-03 12:11:42 +0300197 enetc_init_rgmii(dev);
198 break;
199 case PHY_INTERFACE_MODE_XGMII:
Alex Margineane22e3af2019-11-14 18:28:38 +0200200 case PHY_INTERFACE_MODE_USXGMII:
201 case PHY_INTERFACE_MODE_XFI:
Alex Margineane4aafd52019-07-03 12:11:42 +0300202 enetc_init_sxgmii(dev);
203 break;
204 };
205}
206
Alex Marginean1d995342019-07-03 12:11:41 +0300207/* Configure the actual/external ethernet PHY, if one is found */
208static void enetc_start_phy(struct udevice *dev)
209{
210 struct enetc_priv *priv = dev_get_priv(dev);
211 struct udevice *miidev;
212 struct phy_device *phy;
213 u32 phandle, phy_id;
214 ofnode phy_node;
215 int supported;
216
217 if (!ofnode_valid(dev->node)) {
218 enetc_dbg(dev, "no enetc ofnode found, skipping PHY set-up\n");
219 return;
220 }
221
222 if (ofnode_read_u32(dev->node, "phy-handle", &phandle)) {
223 enetc_dbg(dev, "phy-handle not found, skipping PHY set-up\n");
224 return;
225 }
226
227 phy_node = ofnode_get_by_phandle(phandle);
228 if (!ofnode_valid(phy_node)) {
229 enetc_dbg(dev, "invalid phy node, skipping PHY set-up\n");
230 return;
231 }
232 enetc_dbg(dev, "phy node: %s\n", ofnode_get_name(phy_node));
233
234 if (ofnode_read_u32(phy_node, "reg", &phy_id)) {
235 enetc_dbg(dev,
236 "missing reg in PHY node, skipping PHY set-up\n");
237 return;
238 }
239
240 if (uclass_get_device_by_ofnode(UCLASS_MDIO,
241 ofnode_get_parent(phy_node),
242 &miidev)) {
243 enetc_dbg(dev, "can't find MDIO bus for node %s\n",
244 ofnode_get_name(ofnode_get_parent(phy_node)));
245 return;
246 }
247
248 phy = dm_mdio_phy_connect(miidev, phy_id, dev, priv->if_type);
249 if (!phy) {
250 enetc_dbg(dev, "dm_mdio_phy_connect returned null\n");
251 return;
252 }
253
254 supported = GENMASK(6, 0); /* speeds up to 1G & AN */
255 phy->advertising = phy->supported & supported;
Michael Walle2efb1472019-10-26 02:39:11 +0200256 phy->node = phy_node;
Alex Marginean1d995342019-07-03 12:11:41 +0300257 phy_config(phy);
258 phy_startup(phy);
259}
260
Alex Marginean120b5ef2019-07-03 12:11:40 +0300261/*
262 * Probe ENETC driver:
263 * - initialize port and station interface BARs
264 */
265static int enetc_probe(struct udevice *dev)
266{
267 struct enetc_priv *priv = dev_get_priv(dev);
268
269 if (ofnode_valid(dev->node) && !ofnode_is_available(dev->node)) {
270 enetc_dbg(dev, "interface disabled\n");
271 return -ENODEV;
272 }
273
274 priv->enetc_txbd = memalign(ENETC_BD_ALIGN,
275 sizeof(struct enetc_tx_bd) * ENETC_BD_CNT);
276 priv->enetc_rxbd = memalign(ENETC_BD_ALIGN,
277 sizeof(union enetc_rx_bd) * ENETC_BD_CNT);
278
279 if (!priv->enetc_txbd || !priv->enetc_rxbd) {
280 /* free should be able to handle NULL, just free all pointers */
281 free(priv->enetc_txbd);
282 free(priv->enetc_rxbd);
283
284 return -ENOMEM;
285 }
286
287 /* initialize register */
288 priv->regs_base = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0, 0);
289 if (!priv->regs_base) {
290 enetc_dbg(dev, "failed to map BAR0\n");
291 return -EINVAL;
292 }
293 priv->port_regs = priv->regs_base + ENETC_PORT_REGS_OFF;
294
295 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
296
297 return 0;
298}
299
300/*
301 * Remove the driver from an interface:
302 * - free up allocated memory
303 */
304static int enetc_remove(struct udevice *dev)
305{
306 struct enetc_priv *priv = dev_get_priv(dev);
307
308 free(priv->enetc_txbd);
309 free(priv->enetc_rxbd);
310
311 return 0;
312}
313
314/* ENETC Port MAC address registers, accepts big-endian format */
315static void enetc_set_primary_mac_addr(struct enetc_priv *priv, const u8 *addr)
316{
317 u16 lower = *(const u16 *)(addr + 4);
318 u32 upper = *(const u32 *)addr;
319
320 enetc_write_port(priv, ENETC_PSIPMAR0, upper);
321 enetc_write_port(priv, ENETC_PSIPMAR1, lower);
322}
323
324/* Configure port parameters (# of rings, frame size, enable port) */
325static void enetc_enable_si_port(struct enetc_priv *priv)
326{
327 u32 val;
328
329 /* set Rx/Tx BDR count */
330 val = ENETC_PSICFGR_SET_TXBDR(ENETC_TX_BDR_CNT);
331 val |= ENETC_PSICFGR_SET_RXBDR(ENETC_RX_BDR_CNT);
332 enetc_write_port(priv, ENETC_PSICFGR(0), val);
333 /* set Rx max frame size */
334 enetc_write_port(priv, ENETC_PM_MAXFRM, ENETC_RX_MAXFRM_SIZE);
335 /* enable MAC port */
336 enetc_write_port(priv, ENETC_PM_CC, ENETC_PM_CC_RX_TX_EN);
337 /* enable port */
338 enetc_write_port(priv, ENETC_PMR, ENETC_PMR_SI0_EN);
339 /* set SI cache policy */
340 enetc_write(priv, ENETC_SICAR0,
341 ENETC_SICAR_RD_CFG | ENETC_SICAR_WR_CFG);
342 /* enable SI */
343 enetc_write(priv, ENETC_SIMR, ENETC_SIMR_EN);
344}
345
346/* returns DMA address for a given buffer index */
347static inline u64 enetc_rxb_address(struct udevice *dev, int i)
348{
349 return cpu_to_le64(dm_pci_virt_to_mem(dev, net_rx_packets[i]));
350}
351
352/*
353 * Setup a single Tx BD Ring (ID = 0):
354 * - set Tx buffer descriptor address
355 * - set the BD count
356 * - initialize the producer and consumer index
357 */
358static void enetc_setup_tx_bdr(struct udevice *dev)
359{
360 struct enetc_priv *priv = dev_get_priv(dev);
361 struct bd_ring *tx_bdr = &priv->tx_bdr;
362 u64 tx_bd_add = (u64)priv->enetc_txbd;
363
364 /* used later to advance to the next Tx BD */
365 tx_bdr->bd_count = ENETC_BD_CNT;
366 tx_bdr->next_prod_idx = 0;
367 tx_bdr->next_cons_idx = 0;
368 tx_bdr->cons_idx = priv->regs_base +
369 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBCIR);
370 tx_bdr->prod_idx = priv->regs_base +
371 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBPIR);
372
373 /* set Tx BD address */
374 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR0,
375 lower_32_bits(tx_bd_add));
376 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR1,
377 upper_32_bits(tx_bd_add));
378 /* set Tx 8 BD count */
379 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBLENR,
380 tx_bdr->bd_count);
381
382 /* reset both producer/consumer indexes */
383 enetc_write_reg(tx_bdr->cons_idx, tx_bdr->next_cons_idx);
384 enetc_write_reg(tx_bdr->prod_idx, tx_bdr->next_prod_idx);
385
386 /* enable TX ring */
387 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBMR, ENETC_TBMR_EN);
388}
389
390/*
391 * Setup a single Rx BD Ring (ID = 0):
392 * - set Rx buffer descriptors address (one descriptor per buffer)
393 * - set buffer size as max frame size
394 * - enable Rx ring
395 * - reset consumer and producer indexes
396 * - set buffer for each descriptor
397 */
398static void enetc_setup_rx_bdr(struct udevice *dev)
399{
400 struct enetc_priv *priv = dev_get_priv(dev);
401 struct bd_ring *rx_bdr = &priv->rx_bdr;
402 u64 rx_bd_add = (u64)priv->enetc_rxbd;
403 int i;
404
405 /* used later to advance to the next BD produced by ENETC HW */
406 rx_bdr->bd_count = ENETC_BD_CNT;
407 rx_bdr->next_prod_idx = 0;
408 rx_bdr->next_cons_idx = 0;
409 rx_bdr->cons_idx = priv->regs_base +
410 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBCIR);
411 rx_bdr->prod_idx = priv->regs_base +
412 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBPIR);
413
414 /* set Rx BD address */
415 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR0,
416 lower_32_bits(rx_bd_add));
417 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR1,
418 upper_32_bits(rx_bd_add));
419 /* set Rx BD count (multiple of 8) */
420 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBLENR,
421 rx_bdr->bd_count);
422 /* set Rx buffer size */
423 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBSR, PKTSIZE_ALIGN);
424
425 /* fill Rx BD */
426 memset(priv->enetc_rxbd, 0,
427 rx_bdr->bd_count * sizeof(union enetc_rx_bd));
428 for (i = 0; i < rx_bdr->bd_count; i++) {
429 priv->enetc_rxbd[i].w.addr = enetc_rxb_address(dev, i);
430 /* each RX buffer must be aligned to 64B */
431 WARN_ON(priv->enetc_rxbd[i].w.addr & (ARCH_DMA_MINALIGN - 1));
432 }
433
434 /* reset producer (ENETC owned) and consumer (SW owned) index */
435 enetc_write_reg(rx_bdr->cons_idx, rx_bdr->next_cons_idx);
436 enetc_write_reg(rx_bdr->prod_idx, rx_bdr->next_prod_idx);
437
438 /* enable Rx ring */
439 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBMR, ENETC_RBMR_EN);
440}
441
442/*
443 * Start ENETC interface:
444 * - perform FLR
445 * - enable access to port and SI registers
446 * - set mac address
447 * - setup TX/RX buffer descriptors
448 * - enable Tx/Rx rings
449 */
450static int enetc_start(struct udevice *dev)
451{
452 struct eth_pdata *plat = dev_get_platdata(dev);
453 struct enetc_priv *priv = dev_get_priv(dev);
454
455 /* reset and enable the PCI device */
456 dm_pci_flr(dev);
457 dm_pci_clrset_config16(dev, PCI_COMMAND, 0,
458 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
459
460 if (!is_valid_ethaddr(plat->enetaddr)) {
461 enetc_dbg(dev, "invalid MAC address, generate random ...\n");
462 net_random_ethaddr(plat->enetaddr);
463 }
464 enetc_set_primary_mac_addr(priv, plat->enetaddr);
465
466 enetc_enable_si_port(priv);
467
468 /* setup Tx/Rx buffer descriptors */
469 enetc_setup_tx_bdr(dev);
470 enetc_setup_rx_bdr(dev);
471
Alex Margineane4aafd52019-07-03 12:11:42 +0300472 enetc_start_pcs(dev);
Alex Marginean1d995342019-07-03 12:11:41 +0300473 enetc_start_phy(dev);
474
Alex Marginean120b5ef2019-07-03 12:11:40 +0300475 return 0;
476}
477
478/*
479 * Stop the network interface:
480 * - just quiesce it, we can wipe all configuration as _start starts from
481 * scratch each time
482 */
483static void enetc_stop(struct udevice *dev)
484{
485 /* FLR is sufficient to quiesce the device */
486 dm_pci_flr(dev);
487}
488
489/*
490 * ENETC transmit packet:
491 * - check if Tx BD ring is full
492 * - set buffer/packet address (dma address)
493 * - set final fragment flag
494 * - try while producer index equals consumer index or timeout
495 */
496static int enetc_send(struct udevice *dev, void *packet, int length)
497{
498 struct enetc_priv *priv = dev_get_priv(dev);
499 struct bd_ring *txr = &priv->tx_bdr;
500 void *nv_packet = (void *)packet;
501 int tries = ENETC_POLL_TRIES;
502 u32 pi, ci;
503
504 pi = txr->next_prod_idx;
505 ci = enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK;
506 /* Tx ring is full when */
507 if (((pi + 1) % txr->bd_count) == ci) {
508 enetc_dbg(dev, "Tx BDR full\n");
509 return -ETIMEDOUT;
510 }
511 enetc_dbg(dev, "TxBD[%d]send: pkt_len=%d, buff @0x%x%08x\n", pi, length,
512 upper_32_bits((u64)nv_packet), lower_32_bits((u64)nv_packet));
513
514 /* prepare Tx BD */
515 memset(&priv->enetc_txbd[pi], 0x0, sizeof(struct enetc_tx_bd));
516 priv->enetc_txbd[pi].addr =
517 cpu_to_le64(dm_pci_virt_to_mem(dev, nv_packet));
518 priv->enetc_txbd[pi].buf_len = cpu_to_le16(length);
519 priv->enetc_txbd[pi].frm_len = cpu_to_le16(length);
520 priv->enetc_txbd[pi].flags = cpu_to_le16(ENETC_TXBD_FLAGS_F);
521 dmb();
522 /* send frame: increment producer index */
523 pi = (pi + 1) % txr->bd_count;
524 txr->next_prod_idx = pi;
525 enetc_write_reg(txr->prod_idx, pi);
526 while ((--tries >= 0) &&
527 (pi != (enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK)))
528 udelay(10);
529
530 return tries > 0 ? 0 : -ETIMEDOUT;
531}
532
533/*
534 * Receive frame:
535 * - wait for the next BD to get ready bit set
536 * - clean up the descriptor
537 * - move on and indicate to HW that the cleaned BD is available for Rx
538 */
539static int enetc_recv(struct udevice *dev, int flags, uchar **packetp)
540{
541 struct enetc_priv *priv = dev_get_priv(dev);
542 struct bd_ring *rxr = &priv->rx_bdr;
543 int tries = ENETC_POLL_TRIES;
544 int pi = rxr->next_prod_idx;
545 int ci = rxr->next_cons_idx;
546 u32 status;
547 int len;
548 u8 rdy;
549
550 do {
551 dmb();
552 status = le32_to_cpu(priv->enetc_rxbd[pi].r.lstatus);
553 /* check if current BD is ready to be consumed */
554 rdy = ENETC_RXBD_STATUS_R(status);
555 } while (--tries >= 0 && !rdy);
556
557 if (!rdy)
558 return -EAGAIN;
559
560 dmb();
561 len = le16_to_cpu(priv->enetc_rxbd[pi].r.buf_len);
562 *packetp = (uchar *)enetc_rxb_address(dev, pi);
563 enetc_dbg(dev, "RxBD[%d]: len=%d err=%d pkt=0x%x%08x\n", pi, len,
564 ENETC_RXBD_STATUS_ERRORS(status),
565 upper_32_bits((u64)*packetp), lower_32_bits((u64)*packetp));
566
567 /* BD clean up and advance to next in ring */
568 memset(&priv->enetc_rxbd[pi], 0, sizeof(union enetc_rx_bd));
569 priv->enetc_rxbd[pi].w.addr = enetc_rxb_address(dev, pi);
570 rxr->next_prod_idx = (pi + 1) % rxr->bd_count;
571 ci = (ci + 1) % rxr->bd_count;
572 rxr->next_cons_idx = ci;
573 dmb();
574 /* free up the slot in the ring for HW */
575 enetc_write_reg(rxr->cons_idx, ci);
576
577 return len;
578}
579
580static const struct eth_ops enetc_ops = {
581 .start = enetc_start,
582 .send = enetc_send,
583 .recv = enetc_recv,
584 .stop = enetc_stop,
585};
586
587U_BOOT_DRIVER(eth_enetc) = {
588 .name = "enetc_eth",
589 .id = UCLASS_ETH,
590 .bind = enetc_bind,
591 .probe = enetc_probe,
592 .remove = enetc_remove,
593 .ops = &enetc_ops,
594 .priv_auto_alloc_size = sizeof(struct enetc_priv),
595 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
596};
597
598static struct pci_device_id enetc_ids[] = {
599 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, PCI_DEVICE_ID_ENETC_ETH) },
600 {}
601};
602
603U_BOOT_PCI_DEVICE(eth_enetc, enetc_ids);