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