blob: 7c0ed9f606565d934a979fd81844e2e0c7df288b [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
188/* set up MAC and serdes for SXGMII */
189static int enetc_init_sxgmii(struct udevice *dev)
190{
191 struct enetc_priv *priv = dev_get_priv(dev);
192 u32 if_mode;
193
194 /* set ifmode to (US)XGMII */
195 if_mode = enetc_read_port(priv, ENETC_PM_IF_MODE);
196 if_mode &= ~ENETC_PM_IF_IFMODE_MASK;
197 enetc_write_port(priv, ENETC_PM_IF_MODE, if_mode);
198
199 if (!enetc_has_imdio(dev))
200 return 0;
201
202 /* Dev ability - SXGMII */
203 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
204 ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SXGMII);
205
206 /* Restart PCS AN */
207 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
208 ENETC_PCS_CR,
Alex Marginean9bc07e812019-07-15 11:48:47 +0300209 ENETC_PCS_CR_RST | ENETC_PCS_CR_RESET_AN);
Alex Margineane4aafd52019-07-03 12:11:42 +0300210
211 return 0;
212}
213
214/* Apply protocol specific configuration to MAC, serdes as needed */
215static void enetc_start_pcs(struct udevice *dev)
216{
217 struct enetc_priv *priv = dev_get_priv(dev);
218 const char *if_str;
219
220 priv->if_type = PHY_INTERFACE_MODE_NONE;
221
Alex Marginean1e354cb2019-11-25 17:57:27 +0200222 /* register internal MDIO for debug purposes */
Alex Margineane4aafd52019-07-03 12:11:42 +0300223 if (enetc_read_port(priv, ENETC_PCAPR0) & ENETC_PCAPRO_MDIO) {
Alex Margineane4aafd52019-07-03 12:11:42 +0300224 priv->imdio.read = enetc_mdio_read;
225 priv->imdio.write = enetc_mdio_write;
226 priv->imdio.priv = priv->port_regs + ENETC_PM_IMDIO_BASE;
227 strncpy(priv->imdio.name, dev->name, MDIO_NAME_LEN);
Alex Marginean1e354cb2019-11-25 17:57:27 +0200228 if (!miiphy_get_dev_by_name(priv->imdio.name))
229 mdio_register(&priv->imdio);
Alex Margineane4aafd52019-07-03 12:11:42 +0300230 }
231
232 if (!ofnode_valid(dev->node)) {
233 enetc_dbg(dev, "no enetc ofnode found, skipping PCS set-up\n");
234 return;
235 }
236
237 if_str = ofnode_read_string(dev->node, "phy-mode");
238 if (if_str)
239 priv->if_type = phy_get_interface_by_name(if_str);
240 else
241 enetc_dbg(dev,
242 "phy-mode property not found, defaulting to SGMII\n");
243 if (priv->if_type < 0)
244 priv->if_type = PHY_INTERFACE_MODE_NONE;
245
246 switch (priv->if_type) {
247 case PHY_INTERFACE_MODE_SGMII:
Alex Marginean9bc07e812019-07-15 11:48:47 +0300248 case PHY_INTERFACE_MODE_SGMII_2500:
Alex Margineane4aafd52019-07-03 12:11:42 +0300249 enetc_init_sgmii(dev);
250 break;
Alex Margineane4aafd52019-07-03 12:11:42 +0300251 case PHY_INTERFACE_MODE_XGMII:
Alex Margineane22e3af2019-11-14 18:28:38 +0200252 case PHY_INTERFACE_MODE_USXGMII:
253 case PHY_INTERFACE_MODE_XFI:
Alex Margineane4aafd52019-07-03 12:11:42 +0300254 enetc_init_sxgmii(dev);
255 break;
256 };
257}
258
Alex Marginean1d995342019-07-03 12:11:41 +0300259/* Configure the actual/external ethernet PHY, if one is found */
Alex Marginean17bd7ea2019-11-25 17:15:13 +0200260static void enetc_config_phy(struct udevice *dev)
Alex Marginean1d995342019-07-03 12:11:41 +0300261{
262 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean1d995342019-07-03 12:11:41 +0300263 int supported;
264
Alex Marginean17bd7ea2019-11-25 17:15:13 +0200265 priv->phy = dm_eth_phy_connect(dev);
Alex Marginean1d995342019-07-03 12:11:41 +0300266
Alex Marginean17bd7ea2019-11-25 17:15:13 +0200267 if (!priv->phy)
Alex Marginean1d995342019-07-03 12:11:41 +0300268 return;
Alex Marginean1d995342019-07-03 12:11:41 +0300269
Alex Marginean307f8a62019-11-14 18:58:45 +0200270 supported = PHY_GBIT_FEATURES | SUPPORTED_2500baseX_Full;
271 priv->phy->supported &= supported;
272 priv->phy->advertising &= supported;
Alex Marginean17bd7ea2019-11-25 17:15:13 +0200273
274 phy_config(priv->phy);
Alex Marginean1d995342019-07-03 12:11:41 +0300275}
276
Alex Marginean120b5ef2019-07-03 12:11:40 +0300277/*
278 * Probe ENETC driver:
279 * - initialize port and station interface BARs
280 */
281static int enetc_probe(struct udevice *dev)
282{
283 struct enetc_priv *priv = dev_get_priv(dev);
284
285 if (ofnode_valid(dev->node) && !ofnode_is_available(dev->node)) {
286 enetc_dbg(dev, "interface disabled\n");
287 return -ENODEV;
288 }
289
290 priv->enetc_txbd = memalign(ENETC_BD_ALIGN,
291 sizeof(struct enetc_tx_bd) * ENETC_BD_CNT);
292 priv->enetc_rxbd = memalign(ENETC_BD_ALIGN,
293 sizeof(union enetc_rx_bd) * ENETC_BD_CNT);
294
295 if (!priv->enetc_txbd || !priv->enetc_rxbd) {
296 /* free should be able to handle NULL, just free all pointers */
297 free(priv->enetc_txbd);
298 free(priv->enetc_rxbd);
299
300 return -ENOMEM;
301 }
302
303 /* initialize register */
304 priv->regs_base = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0, 0);
305 if (!priv->regs_base) {
306 enetc_dbg(dev, "failed to map BAR0\n");
307 return -EINVAL;
308 }
309 priv->port_regs = priv->regs_base + ENETC_PORT_REGS_OFF;
310
311 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
312
Alex Margineana931f782019-11-14 18:58:46 +0200313 enetc_start_pcs(dev);
314 enetc_config_phy(dev);
315
Alex Marginean120b5ef2019-07-03 12:11:40 +0300316 return 0;
317}
318
319/*
320 * Remove the driver from an interface:
321 * - free up allocated memory
322 */
323static int enetc_remove(struct udevice *dev)
324{
325 struct enetc_priv *priv = dev_get_priv(dev);
326
327 free(priv->enetc_txbd);
328 free(priv->enetc_rxbd);
329
330 return 0;
331}
332
Michael Walleee5c70b2019-12-20 14:16:47 +0100333static int enetc_write_hwaddr(struct udevice *dev)
Alex Marginean120b5ef2019-07-03 12:11:40 +0300334{
Michael Walleee5c70b2019-12-20 14:16:47 +0100335 struct eth_pdata *plat = dev_get_platdata(dev);
336 struct enetc_priv *priv = dev_get_priv(dev);
337 u8 *addr = plat->enetaddr;
338
Alex Marginean120b5ef2019-07-03 12:11:40 +0300339 u16 lower = *(const u16 *)(addr + 4);
340 u32 upper = *(const u32 *)addr;
341
342 enetc_write_port(priv, ENETC_PSIPMAR0, upper);
343 enetc_write_port(priv, ENETC_PSIPMAR1, lower);
Michael Walleee5c70b2019-12-20 14:16:47 +0100344
345 return 0;
Alex Marginean120b5ef2019-07-03 12:11:40 +0300346}
347
348/* Configure port parameters (# of rings, frame size, enable port) */
349static void enetc_enable_si_port(struct enetc_priv *priv)
350{
351 u32 val;
352
353 /* set Rx/Tx BDR count */
354 val = ENETC_PSICFGR_SET_TXBDR(ENETC_TX_BDR_CNT);
355 val |= ENETC_PSICFGR_SET_RXBDR(ENETC_RX_BDR_CNT);
356 enetc_write_port(priv, ENETC_PSICFGR(0), val);
357 /* set Rx max frame size */
358 enetc_write_port(priv, ENETC_PM_MAXFRM, ENETC_RX_MAXFRM_SIZE);
359 /* enable MAC port */
360 enetc_write_port(priv, ENETC_PM_CC, ENETC_PM_CC_RX_TX_EN);
361 /* enable port */
362 enetc_write_port(priv, ENETC_PMR, ENETC_PMR_SI0_EN);
363 /* set SI cache policy */
364 enetc_write(priv, ENETC_SICAR0,
365 ENETC_SICAR_RD_CFG | ENETC_SICAR_WR_CFG);
366 /* enable SI */
367 enetc_write(priv, ENETC_SIMR, ENETC_SIMR_EN);
368}
369
370/* returns DMA address for a given buffer index */
371static inline u64 enetc_rxb_address(struct udevice *dev, int i)
372{
373 return cpu_to_le64(dm_pci_virt_to_mem(dev, net_rx_packets[i]));
374}
375
376/*
377 * Setup a single Tx BD Ring (ID = 0):
378 * - set Tx buffer descriptor address
379 * - set the BD count
380 * - initialize the producer and consumer index
381 */
382static void enetc_setup_tx_bdr(struct udevice *dev)
383{
384 struct enetc_priv *priv = dev_get_priv(dev);
385 struct bd_ring *tx_bdr = &priv->tx_bdr;
386 u64 tx_bd_add = (u64)priv->enetc_txbd;
387
388 /* used later to advance to the next Tx BD */
389 tx_bdr->bd_count = ENETC_BD_CNT;
390 tx_bdr->next_prod_idx = 0;
391 tx_bdr->next_cons_idx = 0;
392 tx_bdr->cons_idx = priv->regs_base +
393 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBCIR);
394 tx_bdr->prod_idx = priv->regs_base +
395 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBPIR);
396
397 /* set Tx BD address */
398 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR0,
399 lower_32_bits(tx_bd_add));
400 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR1,
401 upper_32_bits(tx_bd_add));
402 /* set Tx 8 BD count */
403 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBLENR,
404 tx_bdr->bd_count);
405
406 /* reset both producer/consumer indexes */
407 enetc_write_reg(tx_bdr->cons_idx, tx_bdr->next_cons_idx);
408 enetc_write_reg(tx_bdr->prod_idx, tx_bdr->next_prod_idx);
409
410 /* enable TX ring */
411 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBMR, ENETC_TBMR_EN);
412}
413
414/*
415 * Setup a single Rx BD Ring (ID = 0):
416 * - set Rx buffer descriptors address (one descriptor per buffer)
417 * - set buffer size as max frame size
418 * - enable Rx ring
419 * - reset consumer and producer indexes
420 * - set buffer for each descriptor
421 */
422static void enetc_setup_rx_bdr(struct udevice *dev)
423{
424 struct enetc_priv *priv = dev_get_priv(dev);
425 struct bd_ring *rx_bdr = &priv->rx_bdr;
426 u64 rx_bd_add = (u64)priv->enetc_rxbd;
427 int i;
428
429 /* used later to advance to the next BD produced by ENETC HW */
430 rx_bdr->bd_count = ENETC_BD_CNT;
431 rx_bdr->next_prod_idx = 0;
432 rx_bdr->next_cons_idx = 0;
433 rx_bdr->cons_idx = priv->regs_base +
434 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBCIR);
435 rx_bdr->prod_idx = priv->regs_base +
436 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBPIR);
437
438 /* set Rx BD address */
439 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR0,
440 lower_32_bits(rx_bd_add));
441 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR1,
442 upper_32_bits(rx_bd_add));
443 /* set Rx BD count (multiple of 8) */
444 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBLENR,
445 rx_bdr->bd_count);
446 /* set Rx buffer size */
447 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBSR, PKTSIZE_ALIGN);
448
449 /* fill Rx BD */
450 memset(priv->enetc_rxbd, 0,
451 rx_bdr->bd_count * sizeof(union enetc_rx_bd));
452 for (i = 0; i < rx_bdr->bd_count; i++) {
453 priv->enetc_rxbd[i].w.addr = enetc_rxb_address(dev, i);
454 /* each RX buffer must be aligned to 64B */
455 WARN_ON(priv->enetc_rxbd[i].w.addr & (ARCH_DMA_MINALIGN - 1));
456 }
457
458 /* reset producer (ENETC owned) and consumer (SW owned) index */
459 enetc_write_reg(rx_bdr->cons_idx, rx_bdr->next_cons_idx);
460 enetc_write_reg(rx_bdr->prod_idx, rx_bdr->next_prod_idx);
461
462 /* enable Rx ring */
463 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBMR, ENETC_RBMR_EN);
464}
465
466/*
467 * Start ENETC interface:
468 * - perform FLR
469 * - enable access to port and SI registers
470 * - set mac address
471 * - setup TX/RX buffer descriptors
472 * - enable Tx/Rx rings
473 */
474static int enetc_start(struct udevice *dev)
475{
Alex Marginean120b5ef2019-07-03 12:11:40 +0300476 struct enetc_priv *priv = dev_get_priv(dev);
477
478 /* reset and enable the PCI device */
479 dm_pci_flr(dev);
480 dm_pci_clrset_config16(dev, PCI_COMMAND, 0,
481 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
482
Alex Marginean120b5ef2019-07-03 12:11:40 +0300483 enetc_enable_si_port(priv);
484
485 /* setup Tx/Rx buffer descriptors */
486 enetc_setup_tx_bdr(dev);
487 enetc_setup_rx_bdr(dev);
488
Alex Margineana931f782019-11-14 18:58:46 +0200489 if (priv->if_type == PHY_INTERFACE_MODE_RGMII ||
490 priv->if_type == PHY_INTERFACE_MODE_RGMII_ID ||
491 priv->if_type == PHY_INTERFACE_MODE_RGMII_RXID ||
492 priv->if_type == PHY_INTERFACE_MODE_RGMII_TXID)
493 enetc_init_rgmii(dev);
494
Alex Marginean17bd7ea2019-11-25 17:15:13 +0200495 if (priv->phy)
496 phy_startup(priv->phy);
Alex Marginean1d995342019-07-03 12:11:41 +0300497
Alex Marginean120b5ef2019-07-03 12:11:40 +0300498 return 0;
499}
500
501/*
502 * Stop the network interface:
503 * - just quiesce it, we can wipe all configuration as _start starts from
504 * scratch each time
505 */
506static void enetc_stop(struct udevice *dev)
507{
508 /* FLR is sufficient to quiesce the device */
509 dm_pci_flr(dev);
Alex Marginean1e354cb2019-11-25 17:57:27 +0200510 /* leave the BARs accessible after we stop, this is needed to use
511 * internal MDIO in command line.
512 */
513 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
Alex Marginean120b5ef2019-07-03 12:11:40 +0300514}
515
516/*
517 * ENETC transmit packet:
518 * - check if Tx BD ring is full
519 * - set buffer/packet address (dma address)
520 * - set final fragment flag
521 * - try while producer index equals consumer index or timeout
522 */
523static int enetc_send(struct udevice *dev, void *packet, int length)
524{
525 struct enetc_priv *priv = dev_get_priv(dev);
526 struct bd_ring *txr = &priv->tx_bdr;
527 void *nv_packet = (void *)packet;
528 int tries = ENETC_POLL_TRIES;
529 u32 pi, ci;
530
531 pi = txr->next_prod_idx;
532 ci = enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK;
533 /* Tx ring is full when */
534 if (((pi + 1) % txr->bd_count) == ci) {
535 enetc_dbg(dev, "Tx BDR full\n");
536 return -ETIMEDOUT;
537 }
538 enetc_dbg(dev, "TxBD[%d]send: pkt_len=%d, buff @0x%x%08x\n", pi, length,
539 upper_32_bits((u64)nv_packet), lower_32_bits((u64)nv_packet));
540
541 /* prepare Tx BD */
542 memset(&priv->enetc_txbd[pi], 0x0, sizeof(struct enetc_tx_bd));
543 priv->enetc_txbd[pi].addr =
544 cpu_to_le64(dm_pci_virt_to_mem(dev, nv_packet));
545 priv->enetc_txbd[pi].buf_len = cpu_to_le16(length);
546 priv->enetc_txbd[pi].frm_len = cpu_to_le16(length);
547 priv->enetc_txbd[pi].flags = cpu_to_le16(ENETC_TXBD_FLAGS_F);
548 dmb();
549 /* send frame: increment producer index */
550 pi = (pi + 1) % txr->bd_count;
551 txr->next_prod_idx = pi;
552 enetc_write_reg(txr->prod_idx, pi);
553 while ((--tries >= 0) &&
554 (pi != (enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK)))
555 udelay(10);
556
557 return tries > 0 ? 0 : -ETIMEDOUT;
558}
559
560/*
561 * Receive frame:
562 * - wait for the next BD to get ready bit set
563 * - clean up the descriptor
564 * - move on and indicate to HW that the cleaned BD is available for Rx
565 */
566static int enetc_recv(struct udevice *dev, int flags, uchar **packetp)
567{
568 struct enetc_priv *priv = dev_get_priv(dev);
569 struct bd_ring *rxr = &priv->rx_bdr;
570 int tries = ENETC_POLL_TRIES;
571 int pi = rxr->next_prod_idx;
572 int ci = rxr->next_cons_idx;
573 u32 status;
574 int len;
575 u8 rdy;
576
577 do {
578 dmb();
579 status = le32_to_cpu(priv->enetc_rxbd[pi].r.lstatus);
580 /* check if current BD is ready to be consumed */
581 rdy = ENETC_RXBD_STATUS_R(status);
582 } while (--tries >= 0 && !rdy);
583
584 if (!rdy)
585 return -EAGAIN;
586
587 dmb();
588 len = le16_to_cpu(priv->enetc_rxbd[pi].r.buf_len);
589 *packetp = (uchar *)enetc_rxb_address(dev, pi);
590 enetc_dbg(dev, "RxBD[%d]: len=%d err=%d pkt=0x%x%08x\n", pi, len,
591 ENETC_RXBD_STATUS_ERRORS(status),
592 upper_32_bits((u64)*packetp), lower_32_bits((u64)*packetp));
593
594 /* BD clean up and advance to next in ring */
595 memset(&priv->enetc_rxbd[pi], 0, sizeof(union enetc_rx_bd));
596 priv->enetc_rxbd[pi].w.addr = enetc_rxb_address(dev, pi);
597 rxr->next_prod_idx = (pi + 1) % rxr->bd_count;
598 ci = (ci + 1) % rxr->bd_count;
599 rxr->next_cons_idx = ci;
600 dmb();
601 /* free up the slot in the ring for HW */
602 enetc_write_reg(rxr->cons_idx, ci);
603
604 return len;
605}
606
607static const struct eth_ops enetc_ops = {
608 .start = enetc_start,
609 .send = enetc_send,
610 .recv = enetc_recv,
611 .stop = enetc_stop,
Michael Walleee5c70b2019-12-20 14:16:47 +0100612 .write_hwaddr = enetc_write_hwaddr,
Alex Marginean120b5ef2019-07-03 12:11:40 +0300613};
614
615U_BOOT_DRIVER(eth_enetc) = {
Alex Marginean9c2aee12019-12-10 16:55:39 +0200616 .name = ENETC_DRIVER_NAME,
Alex Marginean120b5ef2019-07-03 12:11:40 +0300617 .id = UCLASS_ETH,
618 .bind = enetc_bind,
619 .probe = enetc_probe,
620 .remove = enetc_remove,
621 .ops = &enetc_ops,
622 .priv_auto_alloc_size = sizeof(struct enetc_priv),
623 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
624};
625
626static struct pci_device_id enetc_ids[] = {
627 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, PCI_DEVICE_ID_ENETC_ETH) },
628 {}
629};
630
631U_BOOT_PCI_DEVICE(eth_enetc, enetc_ids);