blob: 11b6148ab632283e8fb02499fdddde38873135e1 [file] [log] [blame]
Amit Singh Tomar8f656c52020-01-27 01:14:42 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2019 Amit Singh Tomar <amittomer25@gmail.com>
4 *
5 * Driver for Broadcom GENETv5 Ethernet controller (as found on the RPi4)
6 * This driver is based on the Linux driver:
7 * drivers/net/ethernet/broadcom/genet/bcmgenet.c
8 * which is: Copyright (c) 2014-2017 Broadcom
9 *
10 * The hardware supports multiple queues (16 priority queues and one
11 * default queue), both for RX and TX. There are 256 DMA descriptors (both
12 * for TX and RX), and they live in MMIO registers. The hardware allows
13 * assigning descriptor ranges to queues, but we choose the most simple setup:
14 * All 256 descriptors are assigned to the default queue (#16).
15 * Also the Linux driver supports multiple generations of the MAC, whereas
16 * we only support v5, as used in the Raspberry Pi 4.
17 */
18
Simon Glass0f2af882020-05-10 11:40:05 -060019#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060020#include <asm/cache.h>
Amit Singh Tomar8f656c52020-01-27 01:14:42 +000021#include <asm/io.h>
22#include <clk.h>
23#include <cpu_func.h>
24#include <dm.h>
25#include <fdt_support.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060026#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060027#include <linux/delay.h>
Amit Singh Tomar8f656c52020-01-27 01:14:42 +000028#include <linux/err.h>
29#include <malloc.h>
30#include <miiphy.h>
31#include <net.h>
32#include <dm/of_access.h>
33#include <dm/ofnode.h>
34#include <linux/iopoll.h>
35#include <linux/sizes.h>
36#include <asm/dma-mapping.h>
37#include <wait_bit.h>
38
39/* Register definitions derived from Linux source */
40#define SYS_REV_CTRL 0x00
41
42#define SYS_PORT_CTRL 0x04
43#define PORT_MODE_EXT_GPHY 3
44
45#define GENET_SYS_OFF 0x0000
46#define SYS_RBUF_FLUSH_CTRL (GENET_SYS_OFF + 0x08)
47#define SYS_TBUF_FLUSH_CTRL (GENET_SYS_OFF + 0x0c)
48
49#define GENET_EXT_OFF 0x0080
50#define EXT_RGMII_OOB_CTRL (GENET_EXT_OFF + 0x0c)
51#define RGMII_LINK BIT(4)
52#define OOB_DISABLE BIT(5)
53#define RGMII_MODE_EN BIT(6)
54#define ID_MODE_DIS BIT(16)
55
56#define GENET_RBUF_OFF 0x0300
57#define RBUF_TBUF_SIZE_CTRL (GENET_RBUF_OFF + 0xb4)
58#define RBUF_CTRL (GENET_RBUF_OFF + 0x00)
59#define RBUF_ALIGN_2B BIT(1)
60
61#define GENET_UMAC_OFF 0x0800
62#define UMAC_MIB_CTRL (GENET_UMAC_OFF + 0x580)
63#define UMAC_MAX_FRAME_LEN (GENET_UMAC_OFF + 0x014)
64#define UMAC_MAC0 (GENET_UMAC_OFF + 0x00c)
65#define UMAC_MAC1 (GENET_UMAC_OFF + 0x010)
66#define UMAC_CMD (GENET_UMAC_OFF + 0x008)
67#define MDIO_CMD (GENET_UMAC_OFF + 0x614)
68#define UMAC_TX_FLUSH (GENET_UMAC_OFF + 0x334)
69#define MDIO_START_BUSY BIT(29)
70#define MDIO_READ_FAIL BIT(28)
71#define MDIO_RD (2 << 26)
72#define MDIO_WR BIT(26)
73#define MDIO_PMD_SHIFT 21
74#define MDIO_PMD_MASK 0x1f
75#define MDIO_REG_SHIFT 16
76#define MDIO_REG_MASK 0x1f
77
78#define CMD_TX_EN BIT(0)
79#define CMD_RX_EN BIT(1)
80#define UMAC_SPEED_10 0
81#define UMAC_SPEED_100 1
82#define UMAC_SPEED_1000 2
83#define UMAC_SPEED_2500 3
84#define CMD_SPEED_SHIFT 2
85#define CMD_SPEED_MASK 3
86#define CMD_SW_RESET BIT(13)
87#define CMD_LCL_LOOP_EN BIT(15)
88#define CMD_TX_EN BIT(0)
89#define CMD_RX_EN BIT(1)
90
91#define MIB_RESET_RX BIT(0)
92#define MIB_RESET_RUNT BIT(1)
93#define MIB_RESET_TX BIT(2)
94
95/* total number of Buffer Descriptors, same for Rx/Tx */
96#define TOTAL_DESCS 256
97#define RX_DESCS TOTAL_DESCS
98#define TX_DESCS TOTAL_DESCS
99
100#define DEFAULT_Q 0x10
101
102/* Body(1500) + EH_SIZE(14) + VLANTAG(4) + BRCMTAG(6) + FCS(4) = 1528.
103 * 1536 is multiple of 256 bytes
104 */
105#define ENET_BRCM_TAG_LEN 6
106#define ENET_PAD 8
107#define ENET_MAX_MTU_SIZE (ETH_DATA_LEN + ETH_HLEN + \
108 VLAN_HLEN + ENET_BRCM_TAG_LEN + \
109 ETH_FCS_LEN + ENET_PAD)
110
111/* Tx/Rx Dma Descriptor common bits */
112#define DMA_EN BIT(0)
113#define DMA_RING_BUF_EN_SHIFT 0x01
114#define DMA_RING_BUF_EN_MASK 0xffff
115#define DMA_BUFLENGTH_MASK 0x0fff
116#define DMA_BUFLENGTH_SHIFT 16
117#define DMA_RING_SIZE_SHIFT 16
118#define DMA_OWN 0x8000
119#define DMA_EOP 0x4000
120#define DMA_SOP 0x2000
121#define DMA_WRAP 0x1000
122#define DMA_MAX_BURST_LENGTH 0x8
123/* Tx specific DMA descriptor bits */
124#define DMA_TX_UNDERRUN 0x0200
125#define DMA_TX_APPEND_CRC 0x0040
126#define DMA_TX_OW_CRC 0x0020
127#define DMA_TX_DO_CSUM 0x0010
128#define DMA_TX_QTAG_SHIFT 7
129
130/* DMA rings size */
131#define DMA_RING_SIZE 0x40
132#define DMA_RINGS_SIZE (DMA_RING_SIZE * (DEFAULT_Q + 1))
133
134/* DMA descriptor */
135#define DMA_DESC_LENGTH_STATUS 0x00
136#define DMA_DESC_ADDRESS_LO 0x04
137#define DMA_DESC_ADDRESS_HI 0x08
138#define DMA_DESC_SIZE 12
139
140#define GENET_RX_OFF 0x2000
141#define GENET_RDMA_REG_OFF \
142 (GENET_RX_OFF + TOTAL_DESCS * DMA_DESC_SIZE)
143#define GENET_TX_OFF 0x4000
144#define GENET_TDMA_REG_OFF \
145 (GENET_TX_OFF + TOTAL_DESCS * DMA_DESC_SIZE)
146
147#define DMA_FC_THRESH_HI (RX_DESCS >> 4)
148#define DMA_FC_THRESH_LO 5
149#define DMA_FC_THRESH_VALUE ((DMA_FC_THRESH_LO << 16) | \
150 DMA_FC_THRESH_HI)
151
152#define DMA_XOFF_THRESHOLD_SHIFT 16
153
154#define TDMA_RING_REG_BASE \
155 (GENET_TDMA_REG_OFF + DEFAULT_Q * DMA_RING_SIZE)
156#define TDMA_READ_PTR (TDMA_RING_REG_BASE + 0x00)
157#define TDMA_CONS_INDEX (TDMA_RING_REG_BASE + 0x08)
158#define TDMA_PROD_INDEX (TDMA_RING_REG_BASE + 0x0c)
159#define DMA_RING_BUF_SIZE 0x10
160#define DMA_START_ADDR 0x14
161#define DMA_END_ADDR 0x1c
162#define DMA_MBUF_DONE_THRESH 0x24
163#define TDMA_FLOW_PERIOD (TDMA_RING_REG_BASE + 0x28)
164#define TDMA_WRITE_PTR (TDMA_RING_REG_BASE + 0x2c)
165
166#define RDMA_RING_REG_BASE \
167 (GENET_RDMA_REG_OFF + DEFAULT_Q * DMA_RING_SIZE)
168#define RDMA_WRITE_PTR (RDMA_RING_REG_BASE + 0x00)
169#define RDMA_PROD_INDEX (RDMA_RING_REG_BASE + 0x08)
170#define RDMA_CONS_INDEX (RDMA_RING_REG_BASE + 0x0c)
171#define RDMA_XON_XOFF_THRESH (RDMA_RING_REG_BASE + 0x28)
172#define RDMA_READ_PTR (RDMA_RING_REG_BASE + 0x2c)
173
174#define TDMA_REG_BASE (GENET_TDMA_REG_OFF + DMA_RINGS_SIZE)
175#define RDMA_REG_BASE (GENET_RDMA_REG_OFF + DMA_RINGS_SIZE)
176#define DMA_RING_CFG 0x00
177#define DMA_CTRL 0x04
178#define DMA_SCB_BURST_SIZE 0x0c
179
180#define RX_BUF_LENGTH 2048
181#define RX_TOTAL_BUFSIZE (RX_BUF_LENGTH * RX_DESCS)
182#define RX_BUF_OFFSET 2
183
184struct bcmgenet_eth_priv {
185 char rxbuffer[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
186 void *mac_reg;
187 void *tx_desc_base;
188 void *rx_desc_base;
189 int tx_index;
190 int rx_index;
191 int c_index;
192 int phyaddr;
193 u32 interface;
194 u32 speed;
195 struct phy_device *phydev;
196 struct mii_dev *bus;
197};
198
199static void bcmgenet_umac_reset(struct bcmgenet_eth_priv *priv)
200{
201 u32 reg;
202
203 reg = readl(priv->mac_reg + SYS_RBUF_FLUSH_CTRL);
204 reg |= BIT(1);
205 writel(reg, (priv->mac_reg + SYS_RBUF_FLUSH_CTRL));
206 udelay(10);
207
208 reg &= ~BIT(1);
209 writel(reg, (priv->mac_reg + SYS_RBUF_FLUSH_CTRL));
210 udelay(10);
211
212 writel(0, (priv->mac_reg + SYS_RBUF_FLUSH_CTRL));
213 udelay(10);
214
215 writel(0, priv->mac_reg + UMAC_CMD);
216
217 writel(CMD_SW_RESET | CMD_LCL_LOOP_EN, priv->mac_reg + UMAC_CMD);
218 udelay(2);
219 writel(0, priv->mac_reg + UMAC_CMD);
220
221 /* clear tx/rx counter */
222 writel(MIB_RESET_RX | MIB_RESET_TX | MIB_RESET_RUNT,
223 priv->mac_reg + UMAC_MIB_CTRL);
224 writel(0, priv->mac_reg + UMAC_MIB_CTRL);
225
226 writel(ENET_MAX_MTU_SIZE, priv->mac_reg + UMAC_MAX_FRAME_LEN);
227
228 /* init rx registers, enable ip header optimization */
229 reg = readl(priv->mac_reg + RBUF_CTRL);
230 reg |= RBUF_ALIGN_2B;
231 writel(reg, (priv->mac_reg + RBUF_CTRL));
232
233 writel(1, (priv->mac_reg + RBUF_TBUF_SIZE_CTRL));
234}
235
236static int bcmgenet_gmac_write_hwaddr(struct udevice *dev)
237{
238 struct bcmgenet_eth_priv *priv = dev_get_priv(dev);
239 struct eth_pdata *pdata = dev_get_platdata(dev);
240 uchar *addr = pdata->enetaddr;
241 u32 reg;
242
243 reg = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
244 writel_relaxed(reg, priv->mac_reg + UMAC_MAC0);
245
246 reg = addr[4] << 8 | addr[5];
247 writel_relaxed(reg, priv->mac_reg + UMAC_MAC1);
248
249 return 0;
250}
251
252static void bcmgenet_disable_dma(struct bcmgenet_eth_priv *priv)
253{
254 clrbits_32(priv->mac_reg + TDMA_REG_BASE + DMA_CTRL, DMA_EN);
255 clrbits_32(priv->mac_reg + RDMA_REG_BASE + DMA_CTRL, DMA_EN);
256
257 writel(1, priv->mac_reg + UMAC_TX_FLUSH);
258 udelay(10);
259 writel(0, priv->mac_reg + UMAC_TX_FLUSH);
260}
261
262static void bcmgenet_enable_dma(struct bcmgenet_eth_priv *priv)
263{
264 u32 dma_ctrl = (1 << (DEFAULT_Q + DMA_RING_BUF_EN_SHIFT)) | DMA_EN;
265
266 writel(dma_ctrl, priv->mac_reg + TDMA_REG_BASE + DMA_CTRL);
267
268 setbits_32(priv->mac_reg + RDMA_REG_BASE + DMA_CTRL, dma_ctrl);
269}
270
271static int bcmgenet_gmac_eth_send(struct udevice *dev, void *packet, int length)
272{
273 struct bcmgenet_eth_priv *priv = dev_get_priv(dev);
274 void *desc_base = priv->tx_desc_base + priv->tx_index * DMA_DESC_SIZE;
275 u32 len_stat = length << DMA_BUFLENGTH_SHIFT;
276 ulong packet_aligned = rounddown((ulong)packet, ARCH_DMA_MINALIGN);
277 u32 prod_index, cons;
278 u32 tries = 100;
279
280 prod_index = readl(priv->mac_reg + TDMA_PROD_INDEX);
281
282 /* There is actually no reason for the rounding here, but the ARMv7
283 * implementation of flush_dcache_range() checks for aligned
284 * boundaries of the flushed range.
285 * Adjust them here to pass that check and avoid misleading messages.
286 */
287 flush_dcache_range(packet_aligned,
288 packet_aligned + roundup(length, ARCH_DMA_MINALIGN));
289
290 len_stat |= 0x3F << DMA_TX_QTAG_SHIFT;
291 len_stat |= DMA_TX_APPEND_CRC | DMA_SOP | DMA_EOP;
292
293 /* Set-up packet for transmission */
294 writel(lower_32_bits((ulong)packet), (desc_base + DMA_DESC_ADDRESS_LO));
295 writel(upper_32_bits((ulong)packet), (desc_base + DMA_DESC_ADDRESS_HI));
296 writel(len_stat, (desc_base + DMA_DESC_LENGTH_STATUS));
297
298 /* Increment index and start transmission */
299 if (++priv->tx_index >= TX_DESCS)
300 priv->tx_index = 0;
301
302 prod_index++;
303
304 /* Start Transmisson */
305 writel(prod_index, priv->mac_reg + TDMA_PROD_INDEX);
306
307 do {
308 cons = readl(priv->mac_reg + TDMA_CONS_INDEX);
309 } while ((cons & 0xffff) < prod_index && --tries);
310 if (!tries)
311 return -ETIMEDOUT;
312
313 return 0;
314}
315
316/* Check whether all cache lines affected by an invalidate are within
317 * the buffer, to make sure we don't accidentally lose unrelated dirty
318 * data stored nearby.
319 * Alignment of the buffer start address will be checked in the implementation
320 * of invalidate_dcache_range().
321 */
322static void invalidate_dcache_check(unsigned long addr, size_t size,
323 size_t buffer_size)
324{
325 size_t inval_size = roundup(size, ARCH_DMA_MINALIGN);
326
327 if (unlikely(inval_size > buffer_size))
328 printf("WARNING: Cache invalidate area exceeds buffer size\n");
329
330 invalidate_dcache_range(addr, addr + inval_size);
331}
332
333static int bcmgenet_gmac_eth_recv(struct udevice *dev,
334 int flags, uchar **packetp)
335{
336 struct bcmgenet_eth_priv *priv = dev_get_priv(dev);
337 void *desc_base = priv->rx_desc_base + priv->rx_index * DMA_DESC_SIZE;
338 u32 prod_index = readl(priv->mac_reg + RDMA_PROD_INDEX);
339 u32 length, addr;
340
341 if (prod_index == priv->c_index)
342 return -EAGAIN;
343
344 length = readl(desc_base + DMA_DESC_LENGTH_STATUS);
345 length = (length >> DMA_BUFLENGTH_SHIFT) & DMA_BUFLENGTH_MASK;
346 addr = readl(desc_base + DMA_DESC_ADDRESS_LO);
347
348 invalidate_dcache_check(addr, length, RX_BUF_LENGTH);
349
350 /* To cater for the IP header alignment the hardware does.
351 * This would actually not be needed if we don't program
352 * RBUF_ALIGN_2B
353 */
354 *packetp = (uchar *)(ulong)addr + RX_BUF_OFFSET;
355
356 return length - RX_BUF_OFFSET;
357}
358
359static int bcmgenet_gmac_free_pkt(struct udevice *dev, uchar *packet,
360 int length)
361{
362 struct bcmgenet_eth_priv *priv = dev_get_priv(dev);
363
364 /* Tell the MAC we have consumed that last receive buffer. */
365 priv->c_index = (priv->c_index + 1) & 0xFFFF;
366 writel(priv->c_index, priv->mac_reg + RDMA_CONS_INDEX);
367
368 /* Forward our descriptor pointer, wrapping around if needed. */
369 if (++priv->rx_index >= RX_DESCS)
370 priv->rx_index = 0;
371
372 return 0;
373}
374
375static void rx_descs_init(struct bcmgenet_eth_priv *priv)
376{
377 char *rxbuffs = &priv->rxbuffer[0];
378 u32 len_stat, i;
379 void *desc_base = priv->rx_desc_base;
380
381 priv->c_index = 0;
382
383 len_stat = (RX_BUF_LENGTH << DMA_BUFLENGTH_SHIFT) | DMA_OWN;
384
385 for (i = 0; i < RX_DESCS; i++) {
386 writel(lower_32_bits((uintptr_t)&rxbuffs[i * RX_BUF_LENGTH]),
387 desc_base + i * DMA_DESC_SIZE + DMA_DESC_ADDRESS_LO);
388 writel(upper_32_bits((uintptr_t)&rxbuffs[i * RX_BUF_LENGTH]),
389 desc_base + i * DMA_DESC_SIZE + DMA_DESC_ADDRESS_HI);
390 writel(len_stat,
391 desc_base + i * DMA_DESC_SIZE + DMA_DESC_LENGTH_STATUS);
392 }
393}
394
395static void rx_ring_init(struct bcmgenet_eth_priv *priv)
396{
397 writel(DMA_MAX_BURST_LENGTH,
398 priv->mac_reg + RDMA_REG_BASE + DMA_SCB_BURST_SIZE);
399
400 writel(0x0, priv->mac_reg + RDMA_RING_REG_BASE + DMA_START_ADDR);
401 writel(0x0, priv->mac_reg + RDMA_READ_PTR);
402 writel(0x0, priv->mac_reg + RDMA_WRITE_PTR);
403 writel(RX_DESCS * DMA_DESC_SIZE / 4 - 1,
404 priv->mac_reg + RDMA_RING_REG_BASE + DMA_END_ADDR);
405
406 writel(0x0, priv->mac_reg + RDMA_PROD_INDEX);
407 writel(0x0, priv->mac_reg + RDMA_CONS_INDEX);
408 writel((RX_DESCS << DMA_RING_SIZE_SHIFT) | RX_BUF_LENGTH,
409 priv->mac_reg + RDMA_RING_REG_BASE + DMA_RING_BUF_SIZE);
410 writel(DMA_FC_THRESH_VALUE, priv->mac_reg + RDMA_XON_XOFF_THRESH);
411 writel(1 << DEFAULT_Q, priv->mac_reg + RDMA_REG_BASE + DMA_RING_CFG);
412}
413
414static void tx_ring_init(struct bcmgenet_eth_priv *priv)
415{
416 writel(DMA_MAX_BURST_LENGTH,
417 priv->mac_reg + TDMA_REG_BASE + DMA_SCB_BURST_SIZE);
418
419 writel(0x0, priv->mac_reg + TDMA_RING_REG_BASE + DMA_START_ADDR);
420 writel(0x0, priv->mac_reg + TDMA_READ_PTR);
421 writel(0x0, priv->mac_reg + TDMA_WRITE_PTR);
422 writel(TX_DESCS * DMA_DESC_SIZE / 4 - 1,
423 priv->mac_reg + TDMA_RING_REG_BASE + DMA_END_ADDR);
424 writel(0x0, priv->mac_reg + TDMA_PROD_INDEX);
425 writel(0x0, priv->mac_reg + TDMA_CONS_INDEX);
426 writel(0x1, priv->mac_reg + TDMA_RING_REG_BASE + DMA_MBUF_DONE_THRESH);
427 writel(0x0, priv->mac_reg + TDMA_FLOW_PERIOD);
428 writel((TX_DESCS << DMA_RING_SIZE_SHIFT) | RX_BUF_LENGTH,
429 priv->mac_reg + TDMA_RING_REG_BASE + DMA_RING_BUF_SIZE);
430
431 writel(1 << DEFAULT_Q, priv->mac_reg + TDMA_REG_BASE + DMA_RING_CFG);
432}
433
434static int bcmgenet_adjust_link(struct bcmgenet_eth_priv *priv)
435{
436 struct phy_device *phy_dev = priv->phydev;
437 u32 speed;
438
439 switch (phy_dev->speed) {
440 case SPEED_1000:
441 speed = UMAC_SPEED_1000;
442 break;
443 case SPEED_100:
444 speed = UMAC_SPEED_100;
445 break;
446 case SPEED_10:
447 speed = UMAC_SPEED_10;
448 break;
449 default:
450 printf("bcmgenet: Unsupported PHY speed: %d\n", phy_dev->speed);
451 return -EINVAL;
452 }
453
454 clrsetbits_32(priv->mac_reg + EXT_RGMII_OOB_CTRL, OOB_DISABLE,
Nicolas Saenz Julienne138f5952020-02-20 17:36:31 +0100455 RGMII_LINK | RGMII_MODE_EN);
456
457 if (phy_dev->interface == PHY_INTERFACE_MODE_RGMII)
458 setbits_32(priv->mac_reg + EXT_RGMII_OOB_CTRL, ID_MODE_DIS);
Amit Singh Tomar8f656c52020-01-27 01:14:42 +0000459
460 writel(speed << CMD_SPEED_SHIFT, (priv->mac_reg + UMAC_CMD));
461
462 return 0;
463}
464
465static int bcmgenet_gmac_eth_start(struct udevice *dev)
466{
467 struct bcmgenet_eth_priv *priv = dev_get_priv(dev);
468 int ret;
469
470 priv->tx_desc_base = priv->mac_reg + GENET_TX_OFF;
471 priv->rx_desc_base = priv->mac_reg + GENET_RX_OFF;
472 priv->tx_index = 0x0;
473 priv->rx_index = 0x0;
474
475 bcmgenet_umac_reset(priv);
476
477 bcmgenet_gmac_write_hwaddr(dev);
478
479 /* Disable RX/TX DMA and flush TX queues */
480 bcmgenet_disable_dma(priv);
481
482 rx_ring_init(priv);
483 rx_descs_init(priv);
484
485 tx_ring_init(priv);
486
487 /* Enable RX/TX DMA */
488 bcmgenet_enable_dma(priv);
489
490 /* read PHY properties over the wire from generic PHY set-up */
491 ret = phy_startup(priv->phydev);
492 if (ret) {
493 printf("bcmgenet: PHY startup failed: %d\n", ret);
494 return ret;
495 }
496
497 /* Update MAC registers based on PHY property */
498 ret = bcmgenet_adjust_link(priv);
499 if (ret) {
500 printf("bcmgenet: adjust PHY link failed: %d\n", ret);
501 return ret;
502 }
503
504 /* Enable Rx/Tx */
505 setbits_32(priv->mac_reg + UMAC_CMD, CMD_TX_EN | CMD_RX_EN);
506
507 return 0;
508}
509
510static int bcmgenet_phy_init(struct bcmgenet_eth_priv *priv, void *dev)
511{
512 struct phy_device *phydev;
513 int ret;
514
515 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
516 if (!phydev)
517 return -ENODEV;
518
519 phydev->supported &= PHY_GBIT_FEATURES;
520 if (priv->speed) {
521 ret = phy_set_supported(priv->phydev, priv->speed);
522 if (ret)
523 return ret;
524 }
525 phydev->advertising = phydev->supported;
526
527 phy_connect_dev(phydev, dev);
528
529 priv->phydev = phydev;
530 phy_config(priv->phydev);
531
532 return 0;
533}
534
535static void bcmgenet_mdio_start(struct bcmgenet_eth_priv *priv)
536{
537 setbits_32(priv->mac_reg + MDIO_CMD, MDIO_START_BUSY);
538}
539
540static int bcmgenet_mdio_write(struct mii_dev *bus, int addr, int devad,
541 int reg, u16 value)
542{
543 struct udevice *dev = bus->priv;
544 struct bcmgenet_eth_priv *priv = dev_get_priv(dev);
545 u32 val;
546
547 /* Prepare the read operation */
548 val = MDIO_WR | (addr << MDIO_PMD_SHIFT) |
549 (reg << MDIO_REG_SHIFT) | (0xffff & value);
550 writel_relaxed(val, priv->mac_reg + MDIO_CMD);
551
552 /* Start MDIO transaction */
553 bcmgenet_mdio_start(priv);
554
555 return wait_for_bit_32(priv->mac_reg + MDIO_CMD,
556 MDIO_START_BUSY, false, 20, true);
557}
558
559static int bcmgenet_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
560{
561 struct udevice *dev = bus->priv;
562 struct bcmgenet_eth_priv *priv = dev_get_priv(dev);
563 u32 val;
564 int ret;
565
566 /* Prepare the read operation */
567 val = MDIO_RD | (addr << MDIO_PMD_SHIFT) | (reg << MDIO_REG_SHIFT);
568 writel_relaxed(val, priv->mac_reg + MDIO_CMD);
569
570 /* Start MDIO transaction */
571 bcmgenet_mdio_start(priv);
572
573 ret = wait_for_bit_32(priv->mac_reg + MDIO_CMD,
574 MDIO_START_BUSY, false, 20, true);
575 if (ret)
576 return ret;
577
578 val = readl_relaxed(priv->mac_reg + MDIO_CMD);
579
580 return val & 0xffff;
581}
582
583static int bcmgenet_mdio_init(const char *name, struct udevice *priv)
584{
585 struct mii_dev *bus = mdio_alloc();
586
587 if (!bus) {
588 debug("Failed to allocate MDIO bus\n");
589 return -ENOMEM;
590 }
591
592 bus->read = bcmgenet_mdio_read;
593 bus->write = bcmgenet_mdio_write;
594 snprintf(bus->name, sizeof(bus->name), name);
595 bus->priv = (void *)priv;
596
597 return mdio_register(bus);
598}
599
600/* We only support RGMII (as used on the RPi4). */
601static int bcmgenet_interface_set(struct bcmgenet_eth_priv *priv)
602{
603 phy_interface_t phy_mode = priv->interface;
604
605 switch (phy_mode) {
606 case PHY_INTERFACE_MODE_RGMII:
607 case PHY_INTERFACE_MODE_RGMII_RXID:
608 writel(PORT_MODE_EXT_GPHY, priv->mac_reg + SYS_PORT_CTRL);
609 break;
610 default:
611 printf("unknown phy mode: %d\n", priv->interface);
612 return -EINVAL;
613 }
614
615 return 0;
616}
617
618static int bcmgenet_eth_probe(struct udevice *dev)
619{
620 struct eth_pdata *pdata = dev_get_platdata(dev);
621 struct bcmgenet_eth_priv *priv = dev_get_priv(dev);
622 ofnode mdio_node;
623 const char *name;
624 u32 reg;
625 int ret;
626 u8 major;
627
628 priv->mac_reg = map_physmem(pdata->iobase, SZ_64K, MAP_NOCACHE);
629 priv->interface = pdata->phy_interface;
630 priv->speed = pdata->max_speed;
631
632 /* Read GENET HW version */
633 reg = readl_relaxed(priv->mac_reg + SYS_REV_CTRL);
634 major = (reg >> 24) & 0x0f;
635 if (major != 6) {
636 if (major == 5)
637 major = 4;
638 else if (major == 0)
639 major = 1;
640
641 printf("Unsupported GENETv%d.%d\n", major, (reg >> 16) & 0x0f);
642 return -ENODEV;
643 }
644
645 ret = bcmgenet_interface_set(priv);
646 if (ret)
647 return ret;
648
649 writel(0, priv->mac_reg + SYS_RBUF_FLUSH_CTRL);
650 udelay(10);
651 /* disable MAC while updating its registers */
652 writel(0, priv->mac_reg + UMAC_CMD);
653 /* issue soft reset with (rg)mii loopback to ensure a stable rxclk */
654 writel(CMD_SW_RESET | CMD_LCL_LOOP_EN, priv->mac_reg + UMAC_CMD);
655
656 mdio_node = dev_read_first_subnode(dev);
657 name = ofnode_get_name(mdio_node);
658
659 ret = bcmgenet_mdio_init(name, dev);
660 if (ret)
661 return ret;
662
663 priv->bus = miiphy_get_dev_by_name(name);
664
665 return bcmgenet_phy_init(priv, dev);
666}
667
668static void bcmgenet_gmac_eth_stop(struct udevice *dev)
669{
670 struct bcmgenet_eth_priv *priv = dev_get_priv(dev);
671
672 clrbits_32(priv->mac_reg + UMAC_CMD, CMD_TX_EN | CMD_RX_EN);
673
674 bcmgenet_disable_dma(priv);
675}
676
677static const struct eth_ops bcmgenet_gmac_eth_ops = {
678 .start = bcmgenet_gmac_eth_start,
679 .write_hwaddr = bcmgenet_gmac_write_hwaddr,
680 .send = bcmgenet_gmac_eth_send,
681 .recv = bcmgenet_gmac_eth_recv,
682 .free_pkt = bcmgenet_gmac_free_pkt,
683 .stop = bcmgenet_gmac_eth_stop,
684};
685
686static int bcmgenet_eth_ofdata_to_platdata(struct udevice *dev)
687{
688 struct eth_pdata *pdata = dev_get_platdata(dev);
689 struct bcmgenet_eth_priv *priv = dev_get_priv(dev);
690 struct ofnode_phandle_args phy_node;
691 const char *phy_mode;
692 int ret;
693
694 pdata->iobase = dev_read_addr(dev);
695
696 /* Get phy mode from DT */
697 pdata->phy_interface = -1;
698 phy_mode = dev_read_string(dev, "phy-mode");
699 if (phy_mode)
700 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
701 if (pdata->phy_interface == -1) {
702 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
703 return -EINVAL;
704 }
705
706 ret = dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
707 &phy_node);
708 if (!ret) {
709 ofnode_read_s32(phy_node.node, "reg", &priv->phyaddr);
710 ofnode_read_s32(phy_node.node, "max-speed", &pdata->max_speed);
711 }
712
713 return 0;
714}
715
716/* The BCM2711 implementation has a limited burst length compared to a generic
717 * GENETv5 version, but we go with that shorter value (8) in both cases, for
718 * the sake of simplicity.
719 */
720static const struct udevice_id bcmgenet_eth_ids[] = {
721 {.compatible = "brcm,genet-v5"},
722 {.compatible = "brcm,bcm2711-genet-v5"},
723 {}
724};
725
726U_BOOT_DRIVER(eth_bcmgenet) = {
727 .name = "eth_bcmgenet",
728 .id = UCLASS_ETH,
729 .of_match = bcmgenet_eth_ids,
730 .ofdata_to_platdata = bcmgenet_eth_ofdata_to_platdata,
731 .probe = bcmgenet_eth_probe,
732 .ops = &bcmgenet_gmac_eth_ops,
733 .priv_auto_alloc_size = sizeof(struct bcmgenet_eth_priv),
734 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
735 .flags = DM_FLAG_ALLOC_PRIV_DMA,
736};