blob: f14b484b26e5c47d625514928ba5fb0aa4499d5c [file] [log] [blame]
Alex Marginean7a910c12019-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 Marginean02155392019-07-03 12:11:41 +030013#include <miiphy.h>
Alex Marginean7a910c12019-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 Marginean02155392019-07-03 12:11:41 +030042/* Configure the actual/external ethernet PHY, if one is found */
43static void enetc_start_phy(struct udevice *dev)
44{
45 struct enetc_priv *priv = dev_get_priv(dev);
46 struct udevice *miidev;
47 struct phy_device *phy;
48 u32 phandle, phy_id;
49 ofnode phy_node;
50 int supported;
51
52 if (!ofnode_valid(dev->node)) {
53 enetc_dbg(dev, "no enetc ofnode found, skipping PHY set-up\n");
54 return;
55 }
56
57 if (ofnode_read_u32(dev->node, "phy-handle", &phandle)) {
58 enetc_dbg(dev, "phy-handle not found, skipping PHY set-up\n");
59 return;
60 }
61
62 phy_node = ofnode_get_by_phandle(phandle);
63 if (!ofnode_valid(phy_node)) {
64 enetc_dbg(dev, "invalid phy node, skipping PHY set-up\n");
65 return;
66 }
67 enetc_dbg(dev, "phy node: %s\n", ofnode_get_name(phy_node));
68
69 if (ofnode_read_u32(phy_node, "reg", &phy_id)) {
70 enetc_dbg(dev,
71 "missing reg in PHY node, skipping PHY set-up\n");
72 return;
73 }
74
75 if (uclass_get_device_by_ofnode(UCLASS_MDIO,
76 ofnode_get_parent(phy_node),
77 &miidev)) {
78 enetc_dbg(dev, "can't find MDIO bus for node %s\n",
79 ofnode_get_name(ofnode_get_parent(phy_node)));
80 return;
81 }
82
83 phy = dm_mdio_phy_connect(miidev, phy_id, dev, priv->if_type);
84 if (!phy) {
85 enetc_dbg(dev, "dm_mdio_phy_connect returned null\n");
86 return;
87 }
88
89 supported = GENMASK(6, 0); /* speeds up to 1G & AN */
90 phy->advertising = phy->supported & supported;
91 phy_config(phy);
92 phy_startup(phy);
93}
94
Alex Marginean7a910c12019-07-03 12:11:40 +030095/*
96 * Probe ENETC driver:
97 * - initialize port and station interface BARs
98 */
99static int enetc_probe(struct udevice *dev)
100{
101 struct enetc_priv *priv = dev_get_priv(dev);
102
103 if (ofnode_valid(dev->node) && !ofnode_is_available(dev->node)) {
104 enetc_dbg(dev, "interface disabled\n");
105 return -ENODEV;
106 }
107
108 priv->enetc_txbd = memalign(ENETC_BD_ALIGN,
109 sizeof(struct enetc_tx_bd) * ENETC_BD_CNT);
110 priv->enetc_rxbd = memalign(ENETC_BD_ALIGN,
111 sizeof(union enetc_rx_bd) * ENETC_BD_CNT);
112
113 if (!priv->enetc_txbd || !priv->enetc_rxbd) {
114 /* free should be able to handle NULL, just free all pointers */
115 free(priv->enetc_txbd);
116 free(priv->enetc_rxbd);
117
118 return -ENOMEM;
119 }
120
121 /* initialize register */
122 priv->regs_base = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0, 0);
123 if (!priv->regs_base) {
124 enetc_dbg(dev, "failed to map BAR0\n");
125 return -EINVAL;
126 }
127 priv->port_regs = priv->regs_base + ENETC_PORT_REGS_OFF;
128
129 dm_pci_clrset_config16(dev, PCI_COMMAND, 0, PCI_COMMAND_MEMORY);
130
131 return 0;
132}
133
134/*
135 * Remove the driver from an interface:
136 * - free up allocated memory
137 */
138static int enetc_remove(struct udevice *dev)
139{
140 struct enetc_priv *priv = dev_get_priv(dev);
141
142 free(priv->enetc_txbd);
143 free(priv->enetc_rxbd);
144
145 return 0;
146}
147
148/* ENETC Port MAC address registers, accepts big-endian format */
149static void enetc_set_primary_mac_addr(struct enetc_priv *priv, const u8 *addr)
150{
151 u16 lower = *(const u16 *)(addr + 4);
152 u32 upper = *(const u32 *)addr;
153
154 enetc_write_port(priv, ENETC_PSIPMAR0, upper);
155 enetc_write_port(priv, ENETC_PSIPMAR1, lower);
156}
157
158/* Configure port parameters (# of rings, frame size, enable port) */
159static void enetc_enable_si_port(struct enetc_priv *priv)
160{
161 u32 val;
162
163 /* set Rx/Tx BDR count */
164 val = ENETC_PSICFGR_SET_TXBDR(ENETC_TX_BDR_CNT);
165 val |= ENETC_PSICFGR_SET_RXBDR(ENETC_RX_BDR_CNT);
166 enetc_write_port(priv, ENETC_PSICFGR(0), val);
167 /* set Rx max frame size */
168 enetc_write_port(priv, ENETC_PM_MAXFRM, ENETC_RX_MAXFRM_SIZE);
169 /* enable MAC port */
170 enetc_write_port(priv, ENETC_PM_CC, ENETC_PM_CC_RX_TX_EN);
171 /* enable port */
172 enetc_write_port(priv, ENETC_PMR, ENETC_PMR_SI0_EN);
173 /* set SI cache policy */
174 enetc_write(priv, ENETC_SICAR0,
175 ENETC_SICAR_RD_CFG | ENETC_SICAR_WR_CFG);
176 /* enable SI */
177 enetc_write(priv, ENETC_SIMR, ENETC_SIMR_EN);
178}
179
180/* returns DMA address for a given buffer index */
181static inline u64 enetc_rxb_address(struct udevice *dev, int i)
182{
183 return cpu_to_le64(dm_pci_virt_to_mem(dev, net_rx_packets[i]));
184}
185
186/*
187 * Setup a single Tx BD Ring (ID = 0):
188 * - set Tx buffer descriptor address
189 * - set the BD count
190 * - initialize the producer and consumer index
191 */
192static void enetc_setup_tx_bdr(struct udevice *dev)
193{
194 struct enetc_priv *priv = dev_get_priv(dev);
195 struct bd_ring *tx_bdr = &priv->tx_bdr;
196 u64 tx_bd_add = (u64)priv->enetc_txbd;
197
198 /* used later to advance to the next Tx BD */
199 tx_bdr->bd_count = ENETC_BD_CNT;
200 tx_bdr->next_prod_idx = 0;
201 tx_bdr->next_cons_idx = 0;
202 tx_bdr->cons_idx = priv->regs_base +
203 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBCIR);
204 tx_bdr->prod_idx = priv->regs_base +
205 ENETC_BDR(TX, ENETC_TX_BDR_ID, ENETC_TBPIR);
206
207 /* set Tx BD address */
208 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR0,
209 lower_32_bits(tx_bd_add));
210 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBBAR1,
211 upper_32_bits(tx_bd_add));
212 /* set Tx 8 BD count */
213 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBLENR,
214 tx_bdr->bd_count);
215
216 /* reset both producer/consumer indexes */
217 enetc_write_reg(tx_bdr->cons_idx, tx_bdr->next_cons_idx);
218 enetc_write_reg(tx_bdr->prod_idx, tx_bdr->next_prod_idx);
219
220 /* enable TX ring */
221 enetc_bdr_write(priv, TX, ENETC_TX_BDR_ID, ENETC_TBMR, ENETC_TBMR_EN);
222}
223
224/*
225 * Setup a single Rx BD Ring (ID = 0):
226 * - set Rx buffer descriptors address (one descriptor per buffer)
227 * - set buffer size as max frame size
228 * - enable Rx ring
229 * - reset consumer and producer indexes
230 * - set buffer for each descriptor
231 */
232static void enetc_setup_rx_bdr(struct udevice *dev)
233{
234 struct enetc_priv *priv = dev_get_priv(dev);
235 struct bd_ring *rx_bdr = &priv->rx_bdr;
236 u64 rx_bd_add = (u64)priv->enetc_rxbd;
237 int i;
238
239 /* used later to advance to the next BD produced by ENETC HW */
240 rx_bdr->bd_count = ENETC_BD_CNT;
241 rx_bdr->next_prod_idx = 0;
242 rx_bdr->next_cons_idx = 0;
243 rx_bdr->cons_idx = priv->regs_base +
244 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBCIR);
245 rx_bdr->prod_idx = priv->regs_base +
246 ENETC_BDR(RX, ENETC_RX_BDR_ID, ENETC_RBPIR);
247
248 /* set Rx BD address */
249 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR0,
250 lower_32_bits(rx_bd_add));
251 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBAR1,
252 upper_32_bits(rx_bd_add));
253 /* set Rx BD count (multiple of 8) */
254 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBLENR,
255 rx_bdr->bd_count);
256 /* set Rx buffer size */
257 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBBSR, PKTSIZE_ALIGN);
258
259 /* fill Rx BD */
260 memset(priv->enetc_rxbd, 0,
261 rx_bdr->bd_count * sizeof(union enetc_rx_bd));
262 for (i = 0; i < rx_bdr->bd_count; i++) {
263 priv->enetc_rxbd[i].w.addr = enetc_rxb_address(dev, i);
264 /* each RX buffer must be aligned to 64B */
265 WARN_ON(priv->enetc_rxbd[i].w.addr & (ARCH_DMA_MINALIGN - 1));
266 }
267
268 /* reset producer (ENETC owned) and consumer (SW owned) index */
269 enetc_write_reg(rx_bdr->cons_idx, rx_bdr->next_cons_idx);
270 enetc_write_reg(rx_bdr->prod_idx, rx_bdr->next_prod_idx);
271
272 /* enable Rx ring */
273 enetc_bdr_write(priv, RX, ENETC_RX_BDR_ID, ENETC_RBMR, ENETC_RBMR_EN);
274}
275
276/*
277 * Start ENETC interface:
278 * - perform FLR
279 * - enable access to port and SI registers
280 * - set mac address
281 * - setup TX/RX buffer descriptors
282 * - enable Tx/Rx rings
283 */
284static int enetc_start(struct udevice *dev)
285{
286 struct eth_pdata *plat = dev_get_platdata(dev);
287 struct enetc_priv *priv = dev_get_priv(dev);
288
289 /* reset and enable the PCI device */
290 dm_pci_flr(dev);
291 dm_pci_clrset_config16(dev, PCI_COMMAND, 0,
292 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
293
294 if (!is_valid_ethaddr(plat->enetaddr)) {
295 enetc_dbg(dev, "invalid MAC address, generate random ...\n");
296 net_random_ethaddr(plat->enetaddr);
297 }
298 enetc_set_primary_mac_addr(priv, plat->enetaddr);
299
300 enetc_enable_si_port(priv);
301
302 /* setup Tx/Rx buffer descriptors */
303 enetc_setup_tx_bdr(dev);
304 enetc_setup_rx_bdr(dev);
305
Alex Marginean02155392019-07-03 12:11:41 +0300306 priv->if_type = PHY_INTERFACE_MODE_NONE;
307 enetc_start_phy(dev);
308
Alex Marginean7a910c12019-07-03 12:11:40 +0300309 return 0;
310}
311
312/*
313 * Stop the network interface:
314 * - just quiesce it, we can wipe all configuration as _start starts from
315 * scratch each time
316 */
317static void enetc_stop(struct udevice *dev)
318{
319 /* FLR is sufficient to quiesce the device */
320 dm_pci_flr(dev);
321}
322
323/*
324 * ENETC transmit packet:
325 * - check if Tx BD ring is full
326 * - set buffer/packet address (dma address)
327 * - set final fragment flag
328 * - try while producer index equals consumer index or timeout
329 */
330static int enetc_send(struct udevice *dev, void *packet, int length)
331{
332 struct enetc_priv *priv = dev_get_priv(dev);
333 struct bd_ring *txr = &priv->tx_bdr;
334 void *nv_packet = (void *)packet;
335 int tries = ENETC_POLL_TRIES;
336 u32 pi, ci;
337
338 pi = txr->next_prod_idx;
339 ci = enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK;
340 /* Tx ring is full when */
341 if (((pi + 1) % txr->bd_count) == ci) {
342 enetc_dbg(dev, "Tx BDR full\n");
343 return -ETIMEDOUT;
344 }
345 enetc_dbg(dev, "TxBD[%d]send: pkt_len=%d, buff @0x%x%08x\n", pi, length,
346 upper_32_bits((u64)nv_packet), lower_32_bits((u64)nv_packet));
347
348 /* prepare Tx BD */
349 memset(&priv->enetc_txbd[pi], 0x0, sizeof(struct enetc_tx_bd));
350 priv->enetc_txbd[pi].addr =
351 cpu_to_le64(dm_pci_virt_to_mem(dev, nv_packet));
352 priv->enetc_txbd[pi].buf_len = cpu_to_le16(length);
353 priv->enetc_txbd[pi].frm_len = cpu_to_le16(length);
354 priv->enetc_txbd[pi].flags = cpu_to_le16(ENETC_TXBD_FLAGS_F);
355 dmb();
356 /* send frame: increment producer index */
357 pi = (pi + 1) % txr->bd_count;
358 txr->next_prod_idx = pi;
359 enetc_write_reg(txr->prod_idx, pi);
360 while ((--tries >= 0) &&
361 (pi != (enetc_read_reg(txr->cons_idx) & ENETC_BDR_IDX_MASK)))
362 udelay(10);
363
364 return tries > 0 ? 0 : -ETIMEDOUT;
365}
366
367/*
368 * Receive frame:
369 * - wait for the next BD to get ready bit set
370 * - clean up the descriptor
371 * - move on and indicate to HW that the cleaned BD is available for Rx
372 */
373static int enetc_recv(struct udevice *dev, int flags, uchar **packetp)
374{
375 struct enetc_priv *priv = dev_get_priv(dev);
376 struct bd_ring *rxr = &priv->rx_bdr;
377 int tries = ENETC_POLL_TRIES;
378 int pi = rxr->next_prod_idx;
379 int ci = rxr->next_cons_idx;
380 u32 status;
381 int len;
382 u8 rdy;
383
384 do {
385 dmb();
386 status = le32_to_cpu(priv->enetc_rxbd[pi].r.lstatus);
387 /* check if current BD is ready to be consumed */
388 rdy = ENETC_RXBD_STATUS_R(status);
389 } while (--tries >= 0 && !rdy);
390
391 if (!rdy)
392 return -EAGAIN;
393
394 dmb();
395 len = le16_to_cpu(priv->enetc_rxbd[pi].r.buf_len);
396 *packetp = (uchar *)enetc_rxb_address(dev, pi);
397 enetc_dbg(dev, "RxBD[%d]: len=%d err=%d pkt=0x%x%08x\n", pi, len,
398 ENETC_RXBD_STATUS_ERRORS(status),
399 upper_32_bits((u64)*packetp), lower_32_bits((u64)*packetp));
400
401 /* BD clean up and advance to next in ring */
402 memset(&priv->enetc_rxbd[pi], 0, sizeof(union enetc_rx_bd));
403 priv->enetc_rxbd[pi].w.addr = enetc_rxb_address(dev, pi);
404 rxr->next_prod_idx = (pi + 1) % rxr->bd_count;
405 ci = (ci + 1) % rxr->bd_count;
406 rxr->next_cons_idx = ci;
407 dmb();
408 /* free up the slot in the ring for HW */
409 enetc_write_reg(rxr->cons_idx, ci);
410
411 return len;
412}
413
414static const struct eth_ops enetc_ops = {
415 .start = enetc_start,
416 .send = enetc_send,
417 .recv = enetc_recv,
418 .stop = enetc_stop,
419};
420
421U_BOOT_DRIVER(eth_enetc) = {
422 .name = "enetc_eth",
423 .id = UCLASS_ETH,
424 .bind = enetc_bind,
425 .probe = enetc_probe,
426 .remove = enetc_remove,
427 .ops = &enetc_ops,
428 .priv_auto_alloc_size = sizeof(struct enetc_priv),
429 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
430};
431
432static struct pci_device_id enetc_ids[] = {
433 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, PCI_DEVICE_ID_ENETC_ETH) },
434 {}
435};
436
437U_BOOT_PCI_DEVICE(eth_enetc, enetc_ids);