1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2019, Linaro Limited
10 #include <linux/mii.h>
16 #define STATION_ADDR_LOW 0x0000
17 #define STATION_ADDR_HIGH 0x0004
18 #define MAC_DUPLEX_HALF_CTRL 0x0008
19 #define PORT_MODE 0x0040
20 #define PORT_EN 0x0044
21 #define BIT_TX_EN BIT(2)
22 #define BIT_RX_EN BIT(1)
23 #define MODE_CHANGE_EN 0x01b4
24 #define BIT_MODE_CHANGE_EN BIT(0)
25 #define MDIO_SINGLE_CMD 0x03c0
26 #define BIT_MDIO_BUSY BIT(20)
27 #define MDIO_READ (BIT(17) | BIT_MDIO_BUSY)
28 #define MDIO_WRITE (BIT(16) | BIT_MDIO_BUSY)
29 #define MDIO_SINGLE_DATA 0x03c4
30 #define MDIO_RDATA_STATUS 0x03d0
31 #define BIT_MDIO_RDATA_INVALID BIT(0)
32 #define RX_FQ_START_ADDR 0x0500
33 #define RX_FQ_DEPTH 0x0504
34 #define RX_FQ_WR_ADDR 0x0508
35 #define RX_FQ_RD_ADDR 0x050c
36 #define RX_FQ_REG_EN 0x0518
37 #define RX_BQ_START_ADDR 0x0520
38 #define RX_BQ_DEPTH 0x0524
39 #define RX_BQ_WR_ADDR 0x0528
40 #define RX_BQ_RD_ADDR 0x052c
41 #define RX_BQ_REG_EN 0x0538
42 #define TX_BQ_START_ADDR 0x0580
43 #define TX_BQ_DEPTH 0x0584
44 #define TX_BQ_WR_ADDR 0x0588
45 #define TX_BQ_RD_ADDR 0x058c
46 #define TX_BQ_REG_EN 0x0598
47 #define TX_RQ_START_ADDR 0x05a0
48 #define TX_RQ_DEPTH 0x05a4
49 #define TX_RQ_WR_ADDR 0x05a8
50 #define TX_RQ_RD_ADDR 0x05ac
51 #define TX_RQ_REG_EN 0x05b8
52 #define BIT_START_ADDR_EN BIT(2)
53 #define BIT_DEPTH_EN BIT(1)
54 #define DESC_WR_RD_ENA 0x05cc
55 #define BIT_RX_OUTCFF_WR BIT(3)
56 #define BIT_RX_CFF_RD BIT(2)
57 #define BIT_TX_OUTCFF_WR BIT(1)
58 #define BIT_TX_CFF_RD BIT(0)
59 #define BITS_DESC_ENA (BIT_RX_OUTCFF_WR | BIT_RX_CFF_RD | \
60 BIT_TX_OUTCFF_WR | BIT_TX_CFF_RD)
63 #define RGMII_SPEED_1000 0x2c
64 #define RGMII_SPEED_100 0x2f
65 #define RGMII_SPEED_10 0x2d
66 #define MII_SPEED_100 0x0f
67 #define MII_SPEED_10 0x0d
68 #define GMAC_SPEED_1000 0x05
69 #define GMAC_SPEED_100 0x01
70 #define GMAC_SPEED_10 0x00
71 #define GMAC_FULL_DUPLEX BIT(4)
73 #define RX_DESC_NUM 64
76 #define DESC_WORD_SHIFT 3
77 #define DESC_BYTE_SHIFT 5
78 #define DESC_CNT(n) ((n) >> DESC_BYTE_SHIFT)
79 #define DESC_BYTE(n) ((n) << DESC_BYTE_SHIFT)
80 #define DESC_VLD_FREE 0
81 #define DESC_VLD_BUSY 1
83 #define MAC_MAX_FRAME_SIZE 1600
93 unsigned int buf_addr;
94 unsigned int buf_len:11;
95 unsigned int reserve0:5;
96 unsigned int data_len:11;
97 unsigned int reserve1:2;
99 unsigned int descvid:1;
100 unsigned int reserve2[6];
105 void __iomem *macif_ctrl;
106 struct reset_ctl rst_phy;
107 struct higmac_desc *rxfq;
108 struct higmac_desc *rxbq;
109 struct higmac_desc *txbq;
110 struct higmac_desc *txrq;
113 struct phy_device *phydev;
118 #define flush_desc(d) flush_cache((unsigned long)(d), sizeof(*(d)))
119 #define invalidate_desc(d) \
120 invalidate_dcache_range((unsigned long)(d), \
121 (unsigned long)(d) + sizeof(*(d)))
123 static int higmac_write_hwaddr(struct udevice *dev)
125 struct eth_pdata *pdata = dev_get_platdata(dev);
126 struct higmac_priv *priv = dev_get_priv(dev);
127 unsigned char *mac = pdata->enetaddr;
130 val = mac[1] | (mac[0] << 8);
131 writel(val, priv->base + STATION_ADDR_HIGH);
133 val = mac[5] | (mac[4] << 8) | (mac[3] << 16) | (mac[2] << 24);
134 writel(val, priv->base + STATION_ADDR_LOW);
139 static int higmac_free_pkt(struct udevice *dev, uchar *packet, int length)
141 struct higmac_priv *priv = dev_get_priv(dev);
143 /* Inform GMAC that the RX descriptor is no longer in use */
144 writel(DESC_BYTE(priv->rxdesc_in_use), priv->base + RX_BQ_RD_ADDR);
149 static int higmac_recv(struct udevice *dev, int flags, uchar **packetp)
151 struct higmac_priv *priv = dev_get_priv(dev);
152 struct higmac_desc *fqd = priv->rxfq;
153 struct higmac_desc *bqd = priv->rxbq;
154 int fqw_pos, fqr_pos, bqw_pos, bqr_pos;
155 int timeout = 100000;
160 fqw_pos = DESC_CNT(readl(priv->base + RX_FQ_WR_ADDR));
161 fqr_pos = DESC_CNT(readl(priv->base + RX_FQ_RD_ADDR));
163 if (fqw_pos >= fqr_pos)
164 space = RX_DESC_NUM - (fqw_pos - fqr_pos);
166 space = fqr_pos - fqw_pos;
168 /* Leave one free to distinguish full filled from empty buffer */
169 for (i = 0; i < space - 1; i++) {
170 fqd = priv->rxfq + fqw_pos;
171 invalidate_dcache_range(fqd->buf_addr,
172 fqd->buf_addr + MAC_MAX_FRAME_SIZE);
174 if (++fqw_pos >= RX_DESC_NUM)
177 writel(DESC_BYTE(fqw_pos), priv->base + RX_FQ_WR_ADDR);
180 bqr_pos = DESC_CNT(readl(priv->base + RX_BQ_RD_ADDR));
182 /* BQ is only ever written by GMAC */
183 invalidate_desc(bqd);
186 bqw_pos = DESC_CNT(readl(priv->base + RX_BQ_WR_ADDR));
188 } while (--timeout && bqw_pos == bqr_pos);
193 if (++bqr_pos >= RX_DESC_NUM)
198 /* CPU should not have touched this buffer since we added it to FQ */
199 invalidate_dcache_range(bqd->buf_addr, bqd->buf_addr + len);
200 *packetp = (void *)(unsigned long)bqd->buf_addr;
202 /* Record the RX_BQ descriptor that is holding RX data */
203 priv->rxdesc_in_use = bqr_pos;
208 static int higmac_send(struct udevice *dev, void *packet, int length)
210 struct higmac_priv *priv = dev_get_priv(dev);
211 struct higmac_desc *bqd = priv->txbq;
212 int bqw_pos, rqw_pos, rqr_pos;
215 flush_cache((unsigned long)packet, length);
217 bqw_pos = DESC_CNT(readl(priv->base + TX_BQ_WR_ADDR));
219 bqd->buf_addr = (unsigned long)packet;
220 bqd->descvid = DESC_VLD_BUSY;
221 bqd->data_len = length;
224 if (++bqw_pos >= TX_DESC_NUM)
227 writel(DESC_BYTE(bqw_pos), priv->base + TX_BQ_WR_ADDR);
229 rqr_pos = DESC_CNT(readl(priv->base + TX_RQ_RD_ADDR));
230 if (++rqr_pos >= TX_DESC_NUM)
234 rqw_pos = DESC_CNT(readl(priv->base + TX_RQ_WR_ADDR));
236 } while (--timeout && rqr_pos != rqw_pos);
241 writel(DESC_BYTE(rqr_pos), priv->base + TX_RQ_RD_ADDR);
246 static int higmac_adjust_link(struct higmac_priv *priv)
248 struct phy_device *phydev = priv->phydev;
249 int interface = priv->phyintf;
253 case PHY_INTERFACE_MODE_RGMII:
254 if (phydev->speed == SPEED_1000)
255 val = RGMII_SPEED_1000;
256 else if (phydev->speed == SPEED_100)
257 val = RGMII_SPEED_100;
259 val = RGMII_SPEED_10;
261 case PHY_INTERFACE_MODE_MII:
262 if (phydev->speed == SPEED_100)
268 debug("unsupported mode: %d\n", interface);
273 val |= GMAC_FULL_DUPLEX;
275 writel(val, priv->macif_ctrl);
277 if (phydev->speed == SPEED_1000)
278 val = GMAC_SPEED_1000;
279 else if (phydev->speed == SPEED_100)
280 val = GMAC_SPEED_100;
284 writel(BIT_MODE_CHANGE_EN, priv->base + MODE_CHANGE_EN);
285 writel(val, priv->base + PORT_MODE);
286 writel(0, priv->base + MODE_CHANGE_EN);
287 writel(phydev->duplex, priv->base + MAC_DUPLEX_HALF_CTRL);
292 static int higmac_start(struct udevice *dev)
294 struct higmac_priv *priv = dev_get_priv(dev);
295 struct phy_device *phydev = priv->phydev;
298 ret = phy_startup(phydev);
303 debug("%s: link down\n", phydev->dev->name);
307 ret = higmac_adjust_link(priv);
312 writel(BITS_DESC_ENA, priv->base + DESC_WR_RD_ENA);
313 writel(BIT_TX_EN | BIT_RX_EN, priv->base + PORT_EN);
318 static void higmac_stop(struct udevice *dev)
320 struct higmac_priv *priv = dev_get_priv(dev);
323 writel(0, priv->base + PORT_EN);
324 writel(0, priv->base + DESC_WR_RD_ENA);
327 static const struct eth_ops higmac_ops = {
328 .start = higmac_start,
331 .free_pkt = higmac_free_pkt,
333 .write_hwaddr = higmac_write_hwaddr,
336 static int higmac_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
338 struct higmac_priv *priv = bus->priv;
341 ret = wait_for_bit_le32(priv->base + MDIO_SINGLE_CMD, BIT_MDIO_BUSY,
346 writel(MDIO_READ | addr << 8 | reg, priv->base + MDIO_SINGLE_CMD);
348 ret = wait_for_bit_le32(priv->base + MDIO_SINGLE_CMD, BIT_MDIO_BUSY,
353 if (readl(priv->base + MDIO_RDATA_STATUS) & BIT_MDIO_RDATA_INVALID)
356 return readl(priv->base + MDIO_SINGLE_DATA) >> 16;
359 static int higmac_mdio_write(struct mii_dev *bus, int addr, int devad,
362 struct higmac_priv *priv = bus->priv;
365 ret = wait_for_bit_le32(priv->base + MDIO_SINGLE_CMD, BIT_MDIO_BUSY,
370 writel(value, priv->base + MDIO_SINGLE_DATA);
371 writel(MDIO_WRITE | addr << 8 | reg, priv->base + MDIO_SINGLE_CMD);
376 static int higmac_init_rx_descs(struct higmac_desc *descs, int num)
380 for (i = 0; i < num; i++) {
381 struct higmac_desc *desc = &descs[i];
383 desc->buf_addr = (unsigned long)memalign(ARCH_DMA_MINALIGN,
388 desc->descvid = DESC_VLD_FREE;
389 desc->buf_len = MAC_MAX_FRAME_SIZE - 1;
397 free((void *)(unsigned long)descs[i].buf_addr);
401 static int higmac_init_hw_queue(struct higmac_priv *priv,
402 enum higmac_queue queue)
404 struct higmac_desc *desc, **pdesc;
405 u32 regaddr, regen, regdep;
411 regaddr = RX_FQ_START_ADDR;
412 regen = RX_FQ_REG_EN;
413 regdep = RX_FQ_DEPTH;
418 regaddr = RX_BQ_START_ADDR;
419 regen = RX_BQ_REG_EN;
420 regdep = RX_BQ_DEPTH;
425 regaddr = TX_BQ_START_ADDR;
426 regen = TX_BQ_REG_EN;
427 regdep = TX_BQ_DEPTH;
432 regaddr = TX_RQ_START_ADDR;
433 regen = TX_RQ_REG_EN;
434 regdep = TX_RQ_DEPTH;
441 writel(BIT_DEPTH_EN, priv->base + regen);
442 writel(depth << DESC_WORD_SHIFT, priv->base + regdep);
443 writel(0, priv->base + regen);
445 len = depth * sizeof(*desc);
446 desc = memalign(ARCH_DMA_MINALIGN, len);
449 memset(desc, 0, len);
450 flush_cache((unsigned long)desc, len);
453 /* Set up RX_FQ descriptors */
455 higmac_init_rx_descs(desc, depth);
457 /* Enable start address */
458 writel(BIT_START_ADDR_EN, priv->base + regen);
459 writel((unsigned long)desc, priv->base + regaddr);
460 writel(0, priv->base + regen);
465 static int higmac_hw_init(struct higmac_priv *priv)
469 /* Initialize hardware queues */
470 ret = higmac_init_hw_queue(priv, RX_FQ);
474 ret = higmac_init_hw_queue(priv, RX_BQ);
478 ret = higmac_init_hw_queue(priv, TX_BQ);
482 ret = higmac_init_hw_queue(priv, TX_RQ);
487 reset_deassert(&priv->rst_phy);
489 reset_assert(&priv->rst_phy);
491 reset_deassert(&priv->rst_phy);
505 static int higmac_probe(struct udevice *dev)
507 struct higmac_priv *priv = dev_get_priv(dev);
508 struct phy_device *phydev;
512 ret = higmac_hw_init(priv);
520 bus->read = higmac_mdio_read;
521 bus->write = higmac_mdio_write;
525 ret = mdio_register_seq(bus, dev->seq);
529 phydev = phy_connect(bus, priv->phyaddr, dev, priv->phyintf);
533 phydev->supported &= PHY_GBIT_FEATURES;
534 phydev->advertising = phydev->supported;
535 priv->phydev = phydev;
537 return phy_config(phydev);
540 static int higmac_remove(struct udevice *dev)
542 struct higmac_priv *priv = dev_get_priv(dev);
545 mdio_unregister(priv->bus);
546 mdio_free(priv->bus);
548 /* Free RX packet buffers */
549 for (i = 0; i < RX_DESC_NUM; i++)
550 free((void *)(unsigned long)priv->rxfq[i].buf_addr);
555 static int higmac_ofdata_to_platdata(struct udevice *dev)
557 struct higmac_priv *priv = dev_get_priv(dev);
558 int phyintf = PHY_INTERFACE_MODE_NONE;
559 const char *phy_mode;
562 priv->base = dev_remap_addr_index(dev, 0);
563 priv->macif_ctrl = dev_remap_addr_index(dev, 1);
565 phy_mode = dev_read_string(dev, "phy-mode");
567 phyintf = phy_get_interface_by_name(phy_mode);
568 if (phyintf == PHY_INTERFACE_MODE_NONE)
570 priv->phyintf = phyintf;
572 phy_node = dev_read_subnode(dev, "phy");
573 if (!ofnode_valid(phy_node)) {
574 debug("failed to find phy node\n");
577 priv->phyaddr = ofnode_read_u32_default(phy_node, "reg", 0);
579 return reset_get_by_name(dev, "phy", &priv->rst_phy);
582 static const struct udevice_id higmac_ids[] = {
583 { .compatible = "hisilicon,hi3798cv200-gmac" },
587 U_BOOT_DRIVER(eth_higmac) = {
588 .name = "eth_higmac",
590 .of_match = higmac_ids,
591 .ofdata_to_platdata = higmac_ofdata_to_platdata,
592 .probe = higmac_probe,
593 .remove = higmac_remove,
595 .priv_auto_alloc_size = sizeof(struct higmac_priv),
596 .platdata_auto_alloc_size = sizeof(struct eth_pdata),