blob: 2a66c2e5f9b37889b96fd62a95334163eb0a4a90 [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>
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>
Alex Marginean120b5ef2019-07-03 12:11:40 +030018
19#include "fsl_enetc.h"
20
Alex Marginean9c2aee12019-12-10 16:55:39 +020021#define ENETC_DRIVER_NAME "enetc_eth"
22
23/*
24 * sets the MAC address in IERB registers, this setting is persistent and
25 * carried over to Linux.
26 */
27static void enetc_set_ierb_primary_mac(struct udevice *dev, int devfn,
28 const u8 *enetaddr)
29{
30#ifdef CONFIG_ARCH_LS1028A
31/*
32 * LS1028A is the only part with IERB at this time and there are plans to change
33 * its structure, keep this LS1028A specific for now
34 */
35#define IERB_BASE 0x1f0800000ULL
36#define IERB_PFMAC(pf, vf, n) (IERB_BASE + 0x8000 + (pf) * 0x100 + (vf) * 8 \
37 + (n) * 4)
38
39static int ierb_fn_to_pf[] = {0, 1, 2, -1, -1, -1, 3};
40
41 u16 lower = *(const u16 *)(enetaddr + 4);
42 u32 upper = *(const u32 *)enetaddr;
43
44 if (ierb_fn_to_pf[devfn] < 0)
45 return;
46
47 out_le32(IERB_PFMAC(ierb_fn_to_pf[devfn], 0, 0), upper);
48 out_le32(IERB_PFMAC(ierb_fn_to_pf[devfn], 0, 1), (u32)lower);
49#endif
50}
51
52/* sets up primary MAC addresses in DT/IERB */
53void fdt_fixup_enetc_mac(void *blob)
54{
55 struct pci_child_platdata *ppdata;
56 struct eth_pdata *pdata;
57 struct udevice *dev;
58 struct uclass *uc;
59 char path[256];
60 int offset;
61 int devfn;
62
63 uclass_get(UCLASS_ETH, &uc);
64 uclass_foreach_dev(dev, uc) {
65 if (!dev->driver || !dev->driver->name ||
66 strcmp(dev->driver->name, ENETC_DRIVER_NAME))
67 continue;
68
69 pdata = dev_get_platdata(dev);
70 ppdata = dev_get_parent_platdata(dev);
71 devfn = PCI_FUNC(ppdata->devfn);
72
73 enetc_set_ierb_primary_mac(dev, devfn, pdata->enetaddr);
74
75 snprintf(path, 256, "/soc/pcie@1f0000000/ethernet@%x,%x",
76 PCI_DEV(ppdata->devfn), PCI_FUNC(ppdata->devfn));
77 offset = fdt_path_offset(blob, path);
78 if (offset < 0)
79 continue;
80 fdt_setprop(blob, offset, "mac-address", pdata->enetaddr, 6);
81 }
82}
83
Alex Marginean120b5ef2019-07-03 12:11:40 +030084/*
85 * Bind the device:
86 * - set a more explicit name on the interface
87 */
88static int enetc_bind(struct udevice *dev)
89{
90 char name[16];
91 static int eth_num_devices;
92
93 /*
94 * prefer using PCI function numbers to number interfaces, but these
95 * are only available if dts nodes are present. For PCI they are
96 * optional, handle that case too. Just in case some nodes are present
97 * and some are not, use different naming scheme - enetc-N based on
98 * PCI function # and enetc#N based on interface count
99 */
100 if (ofnode_valid(dev->node))
101 sprintf(name, "enetc-%u", PCI_FUNC(pci_get_devfn(dev)));
102 else
103 sprintf(name, "enetc#%u", eth_num_devices++);
104 device_set_name(dev, name);
105
106 return 0;
107}
108
Alex Margineane4aafd52019-07-03 12:11:42 +0300109/* MDIO wrappers, we're using these to drive internal MDIO to get to serdes */
110static int enetc_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
111{
112 struct enetc_mdio_priv priv;
113
114 priv.regs_base = bus->priv;
115 return enetc_mdio_read_priv(&priv, addr, devad, reg);
116}
117
118static int enetc_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
119 u16 val)
120{
121 struct enetc_mdio_priv priv;
122
123 priv.regs_base = bus->priv;
124 return enetc_mdio_write_priv(&priv, addr, devad, reg, val);
125}
126
127/* only interfaces that can pin out through serdes have internal MDIO */
128static bool enetc_has_imdio(struct udevice *dev)
129{
130 struct enetc_priv *priv = dev_get_priv(dev);
131
132 return !!(priv->imdio.priv);
133}
134
135/* set up serdes for SGMII */
136static int enetc_init_sgmii(struct udevice *dev)
137{
138 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean9bc07e812019-07-15 11:48:47 +0300139 bool is2500 = false;
140 u16 reg;
Alex Margineane4aafd52019-07-03 12:11:42 +0300141
142 if (!enetc_has_imdio(dev))
143 return 0;
144
Alex Marginean9bc07e812019-07-15 11:48:47 +0300145 if (priv->if_type == PHY_INTERFACE_MODE_SGMII_2500)
146 is2500 = true;
147
148 /*
149 * Set to SGMII mode, for 1Gbps enable AN, for 2.5Gbps set fixed speed.
150 * Although fixed speed is 1Gbps, we could be running at 2.5Gbps based
151 * on PLL configuration. Setting 1G for 2.5G here is counter intuitive
152 * but intentional.
153 */
154 reg = ENETC_PCS_IF_MODE_SGMII;
155 reg |= is2500 ? ENETC_PCS_IF_MODE_SPEED_1G : ENETC_PCS_IF_MODE_SGMII_AN;
Alex Margineane4aafd52019-07-03 12:11:42 +0300156 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean9bc07e812019-07-15 11:48:47 +0300157 ENETC_PCS_IF_MODE, reg);
Alex Margineane4aafd52019-07-03 12:11:42 +0300158
159 /* Dev ability - SGMII */
160 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
161 ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SGMII);
162
163 /* Adjust link timer for SGMII */
164 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
165 ENETC_PCS_LINK_TIMER1, ENETC_PCS_LINK_TIMER1_VAL);
166 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
167 ENETC_PCS_LINK_TIMER2, ENETC_PCS_LINK_TIMER2_VAL);
168
Alex Marginean9bc07e812019-07-15 11:48:47 +0300169 reg = ENETC_PCS_CR_DEF_VAL;
170 reg |= is2500 ? ENETC_PCS_CR_RST : ENETC_PCS_CR_RESET_AN;
Alex Margineane4aafd52019-07-03 12:11:42 +0300171 /* restart PCS AN */
172 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, MDIO_DEVAD_NONE,
Alex Marginean9bc07e812019-07-15 11:48:47 +0300173 ENETC_PCS_CR, reg);
Alex Margineane4aafd52019-07-03 12:11:42 +0300174
175 return 0;
176}
177
178/* set up MAC for RGMII */
179static int enetc_init_rgmii(struct udevice *dev)
180{
181 struct enetc_priv *priv = dev_get_priv(dev);
182 u32 if_mode;
183
184 /* enable RGMII AN */
185 if_mode = enetc_read_port(priv, ENETC_PM_IF_MODE);
186 if_mode |= ENETC_PM_IF_MODE_AN_ENA;
187 enetc_write_port(priv, ENETC_PM_IF_MODE, if_mode);
188
189 return 0;
190}
191
Alex Margineanb8e4eec2020-01-10 23:32:20 +0200192/* set up MAC configuration for the given interface type */
193static void enetc_setup_mac_iface(struct udevice *dev)
Alex Margineane4aafd52019-07-03 12:11:42 +0300194{
195 struct enetc_priv *priv = dev_get_priv(dev);
196 u32 if_mode;
197
Alex Margineanb8e4eec2020-01-10 23:32:20 +0200198 switch (priv->if_type) {
199 case PHY_INTERFACE_MODE_RGMII:
200 case PHY_INTERFACE_MODE_RGMII_ID:
201 case PHY_INTERFACE_MODE_RGMII_RXID:
202 case PHY_INTERFACE_MODE_RGMII_TXID:
203 enetc_init_rgmii(dev);
204 break;
205 case PHY_INTERFACE_MODE_XGMII:
206 case PHY_INTERFACE_MODE_USXGMII:
207 case PHY_INTERFACE_MODE_XFI:
208 /* set ifmode to (US)XGMII */
209 if_mode = enetc_read_port(priv, ENETC_PM_IF_MODE);
210 if_mode &= ~ENETC_PM_IF_IFMODE_MASK;
211 enetc_write_port(priv, ENETC_PM_IF_MODE, if_mode);
212 break;
213 };
214}
215
216/* set up serdes for SXGMII */
217static int enetc_init_sxgmii(struct udevice *dev)
218{
219 struct enetc_priv *priv = dev_get_priv(dev);
Alex Margineane4aafd52019-07-03 12:11:42 +0300220
221 if (!enetc_has_imdio(dev))
222 return 0;
223
224 /* Dev ability - SXGMII */
225 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
226 ENETC_PCS_DEV_ABILITY, ENETC_PCS_DEV_ABILITY_SXGMII);
227
228 /* Restart PCS AN */
229 enetc_mdio_write(&priv->imdio, ENETC_PCS_PHY_ADDR, ENETC_PCS_DEVAD_REPL,
230 ENETC_PCS_CR,
Alex Marginean9bc07e812019-07-15 11:48:47 +0300231 ENETC_PCS_CR_RST | ENETC_PCS_CR_RESET_AN);
Alex Margineane4aafd52019-07-03 12:11:42 +0300232
233 return 0;
234}
235
236/* Apply protocol specific configuration to MAC, serdes as needed */
237static void enetc_start_pcs(struct udevice *dev)
238{
239 struct enetc_priv *priv = dev_get_priv(dev);
240 const char *if_str;
241
242 priv->if_type = PHY_INTERFACE_MODE_NONE;
243
Alex Marginean1e354cb2019-11-25 17:57:27 +0200244 /* register internal MDIO for debug purposes */
Alex Margineane4aafd52019-07-03 12:11:42 +0300245 if (enetc_read_port(priv, ENETC_PCAPR0) & ENETC_PCAPRO_MDIO) {
Alex Margineane4aafd52019-07-03 12:11:42 +0300246 priv->imdio.read = enetc_mdio_read;
247 priv->imdio.write = enetc_mdio_write;
248 priv->imdio.priv = priv->port_regs + ENETC_PM_IMDIO_BASE;
249 strncpy(priv->imdio.name, dev->name, MDIO_NAME_LEN);
Alex Marginean1e354cb2019-11-25 17:57:27 +0200250 if (!miiphy_get_dev_by_name(priv->imdio.name))
251 mdio_register(&priv->imdio);
Alex Margineane4aafd52019-07-03 12:11:42 +0300252 }
253
254 if (!ofnode_valid(dev->node)) {
255 enetc_dbg(dev, "no enetc ofnode found, skipping PCS set-up\n");
256 return;
257 }
258
259 if_str = ofnode_read_string(dev->node, "phy-mode");
260 if (if_str)
261 priv->if_type = phy_get_interface_by_name(if_str);
262 else
263 enetc_dbg(dev,
264 "phy-mode property not found, defaulting to SGMII\n");
265 if (priv->if_type < 0)
266 priv->if_type = PHY_INTERFACE_MODE_NONE;
267
268 switch (priv->if_type) {
269 case PHY_INTERFACE_MODE_SGMII:
Alex Marginean9bc07e812019-07-15 11:48:47 +0300270 case PHY_INTERFACE_MODE_SGMII_2500:
Alex Margineane4aafd52019-07-03 12:11:42 +0300271 enetc_init_sgmii(dev);
272 break;
Alex Margineane4aafd52019-07-03 12:11:42 +0300273 case PHY_INTERFACE_MODE_XGMII:
Alex Margineane22e3af2019-11-14 18:28:38 +0200274 case PHY_INTERFACE_MODE_USXGMII:
275 case PHY_INTERFACE_MODE_XFI:
Alex Margineane4aafd52019-07-03 12:11:42 +0300276 enetc_init_sxgmii(dev);
277 break;
278 };
279}
280
Alex Marginean1d995342019-07-03 12:11:41 +0300281/* Configure the actual/external ethernet PHY, if one is found */
Alex Marginean17bd7ea2019-11-25 17:15:13 +0200282static void enetc_config_phy(struct udevice *dev)
Alex Marginean1d995342019-07-03 12:11:41 +0300283{
284 struct enetc_priv *priv = dev_get_priv(dev);
Alex Marginean1d995342019-07-03 12:11:41 +0300285 int supported;
286
Alex Marginean17bd7ea2019-11-25 17:15:13 +0200287 priv->phy = dm_eth_phy_connect(dev);
Alex Marginean1d995342019-07-03 12:11:41 +0300288
Alex Marginean17bd7ea2019-11-25 17:15:13 +0200289 if (!priv->phy)
Alex Marginean1d995342019-07-03 12:11:41 +0300290 return;
Alex Marginean1d995342019-07-03 12:11:41 +0300291
Alex Marginean307f8a62019-11-14 18:58:45 +0200292 supported = PHY_GBIT_FEATURES | SUPPORTED_2500baseX_Full;
293 priv->phy->supported &= supported;
294 priv->phy->advertising &= supported;
Alex Marginean17bd7ea2019-11-25 17:15:13 +0200295
296 phy_config(priv->phy);
Alex Marginean1d995342019-07-03 12:11:41 +0300297}
298
Alex Marginean120b5ef2019-07-03 12:11:40 +0300299/*
300 * Probe ENETC driver:
301 * - initialize port and station interface BARs
302 */
303static int enetc_probe(struct udevice *dev)
304{
305 struct enetc_priv *priv = dev_get_priv(dev);
306
307 if (ofnode_valid(dev->node) && !ofnode_is_available(dev->node)) {
308 enetc_dbg(dev, "interface disabled\n");
309 return -ENODEV;
310 }
311
312 priv->enetc_txbd = memalign(ENETC_BD_ALIGN,
313 sizeof(struct enetc_tx_bd) * ENETC_BD_CNT);
314 priv->enetc_rxbd = memalign(ENETC_BD_ALIGN,
315 sizeof(union enetc_rx_bd) * ENETC_BD_CNT);
316
317 if (!priv->enetc_txbd || !priv->enetc_rxbd) {
318 /* free should be able to handle NULL, just free all pointers */
319 free(priv->enetc_txbd);
320 free(priv->enetc_rxbd);
321
322 return -ENOMEM;
323 }
324
325 /* initialize register */
326 priv->regs_base = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0, 0);
327 if (!priv->regs_base) {
328 enetc_dbg(dev, "failed to map BAR0\n");
329 return -EINVAL;
330 }
331 priv->port_regs = priv->regs_base + ENETC_PORT_REGS_OFF;
332
333 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
334
Alex Margineana931f782019-11-14 18:58:46 +0200335 enetc_start_pcs(dev);
336 enetc_config_phy(dev);
337
Alex Marginean120b5ef2019-07-03 12:11:40 +0300338 return 0;
339}
340
341/*
342 * Remove the driver from an interface:
343 * - free up allocated memory
344 */
345static 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
Michael Walle42c66f02019-12-20 14:16:48 +0100355/*
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
364static int enetc_ls1028a_write_hwaddr(struct udevice *dev)
365{
366 struct pci_child_platdata *ppdata = dev_get_parent_platdata(dev);
367 const int devfn_to_pf[] = {0, 1, 2, -1, -1, -1, 3};
368 struct eth_pdata *plat = dev_get_platdata(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
Michael Walleee5c70b2019-12-20 14:16:47 +0100390static int enetc_write_hwaddr(struct udevice *dev)
Alex Marginean120b5ef2019-07-03 12:11:40 +0300391{
Michael Walleee5c70b2019-12-20 14:16:47 +0100392 struct eth_pdata *plat = dev_get_platdata(dev);
393 struct enetc_priv *priv = dev_get_priv(dev);
394 u8 *addr = plat->enetaddr;
395
Michael Walle42c66f02019-12-20 14:16:48 +0100396 if (IS_ENABLED(CONFIG_ARCH_LS1028A))
397 return enetc_ls1028a_write_hwaddr(dev);
398
Alex Marginean120b5ef2019-07-03 12:11:40 +0300399 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);
Michael Walleee5c70b2019-12-20 14:16:47 +0100404
405 return 0;
Alex Marginean120b5ef2019-07-03 12:11:40 +0300406}
407
408/* Configure port parameters (# of rings, frame size, enable port) */
409static 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 */
431static 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 */
442static 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 */
482static 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 */
534static int enetc_start(struct udevice *dev)
535{
Alex Marginean120b5ef2019-07-03 12:11:40 +0300536 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
Alex Marginean120b5ef2019-07-03 12:11:40 +0300543 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
Alex Margineanb8e4eec2020-01-10 23:32:20 +0200549 enetc_setup_mac_iface(dev);
Alex Margineana931f782019-11-14 18:58:46 +0200550
Alex Marginean17bd7ea2019-11-25 17:15:13 +0200551 if (priv->phy)
552 phy_startup(priv->phy);
Alex Marginean1d995342019-07-03 12:11:41 +0300553
Alex Marginean120b5ef2019-07-03 12:11:40 +0300554 return 0;
555}
556
557/*
558 * Stop the network interface:
559 * - just quiesce it, we can wipe all configuration as _start starts from
560 * scratch each time
561 */
562static void enetc_stop(struct udevice *dev)
563{
564 /* FLR is sufficient to quiesce the device */
565 dm_pci_flr(dev);
Alex Marginean1e354cb2019-11-25 17:57:27 +0200566 /* leave the BARs accessible after we stop, this is needed to use
567 * internal MDIO in command line.
568 */
569 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
Alex Marginean120b5ef2019-07-03 12:11:40 +0300570}
571
572/*
573 * ENETC transmit packet:
574 * - check if Tx BD ring is full
575 * - set buffer/packet address (dma address)
576 * - set final fragment flag
577 * - try while producer index equals consumer index or timeout
578 */
579static int enetc_send(struct udevice *dev, void *packet, int length)
580{
581 struct enetc_priv *priv = dev_get_priv(dev);
582 struct bd_ring *txr = &priv->tx_bdr;
583 void *nv_packet = (void *)packet;
584 int tries = ENETC_POLL_TRIES;
585 u32 pi, ci;
586
587 pi = txr->next_prod_idx;
588 ci = enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK;
589 /* Tx ring is full when */
590 if (((pi + 1) % txr->bd_count) == ci) {
591 enetc_dbg(dev, "Tx BDR full\n");
592 return -ETIMEDOUT;
593 }
594 enetc_dbg(dev, "TxBD[%d]send: pkt_len=%d, buff @0x%x%08x\n", pi, length,
595 upper_32_bits((u64)nv_packet), lower_32_bits((u64)nv_packet));
596
597 /* prepare Tx BD */
598 memset(&priv->enetc_txbd[pi], 0x0, sizeof(struct enetc_tx_bd));
599 priv->enetc_txbd[pi].addr =
600 cpu_to_le64(dm_pci_virt_to_mem(dev, nv_packet));
601 priv->enetc_txbd[pi].buf_len = cpu_to_le16(length);
602 priv->enetc_txbd[pi].frm_len = cpu_to_le16(length);
603 priv->enetc_txbd[pi].flags = cpu_to_le16(ENETC_TXBD_FLAGS_F);
604 dmb();
605 /* send frame: increment producer index */
606 pi = (pi + 1) % txr->bd_count;
607 txr->next_prod_idx = pi;
608 enetc_write_reg(txr->prod_idx, pi);
609 while ((--tries >= 0) &&
610 (pi != (enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK)))
611 udelay(10);
612
613 return tries > 0 ? 0 : -ETIMEDOUT;
614}
615
616/*
617 * Receive frame:
618 * - wait for the next BD to get ready bit set
619 * - clean up the descriptor
620 * - move on and indicate to HW that the cleaned BD is available for Rx
621 */
622static int enetc_recv(struct udevice *dev, int flags, uchar **packetp)
623{
624 struct enetc_priv *priv = dev_get_priv(dev);
625 struct bd_ring *rxr = &priv->rx_bdr;
626 int tries = ENETC_POLL_TRIES;
627 int pi = rxr->next_prod_idx;
628 int ci = rxr->next_cons_idx;
629 u32 status;
630 int len;
631 u8 rdy;
632
633 do {
634 dmb();
635 status = le32_to_cpu(priv->enetc_rxbd[pi].r.lstatus);
636 /* check if current BD is ready to be consumed */
637 rdy = ENETC_RXBD_STATUS_R(status);
638 } while (--tries >= 0 && !rdy);
639
640 if (!rdy)
641 return -EAGAIN;
642
643 dmb();
644 len = le16_to_cpu(priv->enetc_rxbd[pi].r.buf_len);
645 *packetp = (uchar *)enetc_rxb_address(dev, pi);
646 enetc_dbg(dev, "RxBD[%d]: len=%d err=%d pkt=0x%x%08x\n", pi, len,
647 ENETC_RXBD_STATUS_ERRORS(status),
648 upper_32_bits((u64)*packetp), lower_32_bits((u64)*packetp));
649
650 /* BD clean up and advance to next in ring */
651 memset(&priv->enetc_rxbd[pi], 0, sizeof(union enetc_rx_bd));
652 priv->enetc_rxbd[pi].w.addr = enetc_rxb_address(dev, pi);
653 rxr->next_prod_idx = (pi + 1) % rxr->bd_count;
654 ci = (ci + 1) % rxr->bd_count;
655 rxr->next_cons_idx = ci;
656 dmb();
657 /* free up the slot in the ring for HW */
658 enetc_write_reg(rxr->cons_idx, ci);
659
660 return len;
661}
662
663static const struct eth_ops enetc_ops = {
664 .start = enetc_start,
665 .send = enetc_send,
666 .recv = enetc_recv,
667 .stop = enetc_stop,
Michael Walleee5c70b2019-12-20 14:16:47 +0100668 .write_hwaddr = enetc_write_hwaddr,
Alex Marginean120b5ef2019-07-03 12:11:40 +0300669};
670
671U_BOOT_DRIVER(eth_enetc) = {
Alex Marginean9c2aee12019-12-10 16:55:39 +0200672 .name = ENETC_DRIVER_NAME,
Alex Marginean120b5ef2019-07-03 12:11:40 +0300673 .id = UCLASS_ETH,
674 .bind = enetc_bind,
675 .probe = enetc_probe,
676 .remove = enetc_remove,
677 .ops = &enetc_ops,
678 .priv_auto_alloc_size = sizeof(struct enetc_priv),
679 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
680};
681
682static struct pci_device_id enetc_ids[] = {
683 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, PCI_DEVICE_ID_ENETC_ETH) },
684 {}
685};
686
687U_BOOT_PCI_DEVICE(eth_enetc, enetc_ids);