1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Álvaro Fernández Rojas <noltari@gmail.com>
5 * Derived from linux/drivers/net/ethernet/broadcom/bcm63xx_enet.c:
6 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
19 #define ETH_PORT_STR "brcm,enetsw-port"
21 #define ETH_RX_DESC PKTBUFSRX
23 #define ETH_TIMEOUT 100
25 #define ETH_MAX_PORT 8
26 #define ETH_RGMII_PORT0 4
28 /* Port traffic control */
29 #define ETH_PTCTRL_REG(x) (0x0 + (x))
30 #define ETH_PTCTRL_RXDIS_SHIFT 0
31 #define ETH_PTCTRL_RXDIS_MASK (1 << ETH_PTCTRL_RXDIS_SHIFT)
32 #define ETH_PTCTRL_TXDIS_SHIFT 1
33 #define ETH_PTCTRL_TXDIS_MASK (1 << ETH_PTCTRL_TXDIS_SHIFT)
35 /* Switch mode register */
36 #define ETH_SWMODE_REG 0xb
37 #define ETH_SWMODE_FWD_EN_SHIFT 1
38 #define ETH_SWMODE_FWD_EN_MASK (1 << ETH_SWMODE_FWD_EN_SHIFT)
40 /* IMP override Register */
41 #define ETH_IMPOV_REG 0xe
42 #define ETH_IMPOV_LINKUP_SHIFT 0
43 #define ETH_IMPOV_LINKUP_MASK (1 << ETH_IMPOV_LINKUP_SHIFT)
44 #define ETH_IMPOV_FDX_SHIFT 1
45 #define ETH_IMPOV_FDX_MASK (1 << ETH_IMPOV_FDX_SHIFT)
46 #define ETH_IMPOV_100_SHIFT 2
47 #define ETH_IMPOV_100_MASK (1 << ETH_IMPOV_100_SHIFT)
48 #define ETH_IMPOV_1000_SHIFT 3
49 #define ETH_IMPOV_1000_MASK (1 << ETH_IMPOV_1000_SHIFT)
50 #define ETH_IMPOV_RXFLOW_SHIFT 4
51 #define ETH_IMPOV_RXFLOW_MASK (1 << ETH_IMPOV_RXFLOW_SHIFT)
52 #define ETH_IMPOV_TXFLOW_SHIFT 5
53 #define ETH_IMPOV_TXFLOW_MASK (1 << ETH_IMPOV_TXFLOW_SHIFT)
54 #define ETH_IMPOV_FORCE_SHIFT 7
55 #define ETH_IMPOV_FORCE_MASK (1 << ETH_IMPOV_FORCE_SHIFT)
57 /* Port override Register */
58 #define ETH_PORTOV_REG(x) (0x58 + (x))
59 #define ETH_PORTOV_LINKUP_SHIFT 0
60 #define ETH_PORTOV_LINKUP_MASK (1 << ETH_PORTOV_LINKUP_SHIFT)
61 #define ETH_PORTOV_FDX_SHIFT 1
62 #define ETH_PORTOV_FDX_MASK (1 << ETH_PORTOV_FDX_SHIFT)
63 #define ETH_PORTOV_100_SHIFT 2
64 #define ETH_PORTOV_100_MASK (1 << ETH_PORTOV_100_SHIFT)
65 #define ETH_PORTOV_1000_SHIFT 3
66 #define ETH_PORTOV_1000_MASK (1 << ETH_PORTOV_1000_SHIFT)
67 #define ETH_PORTOV_RXFLOW_SHIFT 4
68 #define ETH_PORTOV_RXFLOW_MASK (1 << ETH_PORTOV_RXFLOW_SHIFT)
69 #define ETH_PORTOV_TXFLOW_SHIFT 5
70 #define ETH_PORTOV_TXFLOW_MASK (1 << ETH_PORTOV_TXFLOW_SHIFT)
71 #define ETH_PORTOV_ENABLE_SHIFT 6
72 #define ETH_PORTOV_ENABLE_MASK (1 << ETH_PORTOV_ENABLE_SHIFT)
74 /* Port RGMII control register */
75 #define ETH_RGMII_CTRL_REG(x) (0x60 + (x))
76 #define ETH_RGMII_CTRL_GMII_CLK_EN (1 << 7)
77 #define ETH_RGMII_CTRL_MII_OVERRIDE_EN (1 << 6)
78 #define ETH_RGMII_CTRL_MII_MODE_MASK (3 << 4)
79 #define ETH_RGMII_CTRL_RGMII_MODE (0 << 4)
80 #define ETH_RGMII_CTRL_MII_MODE (1 << 4)
81 #define ETH_RGMII_CTRL_RVMII_MODE (2 << 4)
82 #define ETH_RGMII_CTRL_TIMING_SEL_EN (1 << 0)
84 /* Port RGMII timing register */
85 #define ENETSW_RGMII_TIMING_REG(x) (0x68 + (x))
87 /* MDIO control register */
88 #define MII_SC_REG 0xb0
89 #define MII_SC_EXT_SHIFT 16
90 #define MII_SC_EXT_MASK (1 << MII_SC_EXT_SHIFT)
91 #define MII_SC_REG_SHIFT 20
92 #define MII_SC_PHYID_SHIFT 25
93 #define MII_SC_RD_SHIFT 30
94 #define MII_SC_RD_MASK (1 << MII_SC_RD_SHIFT)
95 #define MII_SC_WR_SHIFT 31
96 #define MII_SC_WR_MASK (1 << MII_SC_WR_SHIFT)
98 /* MDIO data register */
99 #define MII_DAT_REG 0xb4
101 /* Global Management Configuration Register */
102 #define ETH_GMCR_REG 0x200
103 #define ETH_GMCR_RST_MIB_SHIFT 0
104 #define ETH_GMCR_RST_MIB_MASK (1 << ETH_GMCR_RST_MIB_SHIFT)
106 /* Jumbo control register port mask register */
107 #define ETH_JMBCTL_PORT_REG 0x4004
109 /* Jumbo control mib good frame register */
110 #define ETH_JMBCTL_MAXSIZE_REG 0x4008
113 struct bcm_enetsw_port {
119 bool force_duplex_full;
125 struct bcm6368_eth_priv {
132 struct bcm_enetsw_port used_ports[ETH_MAX_PORT];
133 int sw_port_link[ETH_MAX_PORT];
140 static inline bool bcm_enet_port_is_rgmii(int portid)
142 return portid >= ETH_RGMII_PORT0;
145 static int bcm6368_mdio_read(struct bcm6368_eth_priv *priv, uint8_t ext,
150 writel_be(0, priv->base + MII_SC_REG);
152 val = MII_SC_RD_MASK |
153 (phy_id << MII_SC_PHYID_SHIFT) |
154 (reg << MII_SC_REG_SHIFT);
157 val |= MII_SC_EXT_MASK;
159 writel_be(val, priv->base + MII_SC_REG);
162 return readw_be(priv->base + MII_DAT_REG);
165 static int bcm6368_mdio_write(struct bcm6368_eth_priv *priv, uint8_t ext,
166 int phy_id, int reg, u16 data)
170 writel_be(0, priv->base + MII_SC_REG);
172 val = MII_SC_WR_MASK |
173 (phy_id << MII_SC_PHYID_SHIFT) |
174 (reg << MII_SC_REG_SHIFT);
177 val |= MII_SC_EXT_MASK;
181 writel_be(val, priv->base + MII_SC_REG);
187 static int bcm6368_eth_free_pkt(struct udevice *dev, uchar *packet, int len)
189 struct bcm6368_eth_priv *priv = dev_get_priv(dev);
191 return dma_prepare_rcv_buf(&priv->rx_dma, packet, len);
194 static int bcm6368_eth_recv(struct udevice *dev, int flags, uchar **packetp)
196 struct bcm6368_eth_priv *priv = dev_get_priv(dev);
198 return dma_receive(&priv->rx_dma, (void**)packetp, NULL);
201 static int bcm6368_eth_send(struct udevice *dev, void *packet, int length)
203 struct bcm6368_eth_priv *priv = dev_get_priv(dev);
205 /* pad packets smaller than ETH_ZLEN */
206 if (length < ETH_ZLEN) {
207 memset(packet + length, 0, ETH_ZLEN - length);
211 return dma_send(&priv->tx_dma, packet, length, NULL);
214 static int bcm6368_eth_adjust_link(struct udevice *dev)
216 struct bcm6368_eth_priv *priv = dev_get_priv(dev);
219 for (i = 0; i < priv->num_ports; i++) {
220 struct bcm_enetsw_port *port;
221 int val, j, up, adv, lpa, speed, duplex, media;
222 int external_phy = bcm_enet_port_is_rgmii(i);
225 port = &priv->used_ports[i];
229 if (port->bypass_link)
232 /* dummy read to clear */
233 for (j = 0; j < 2; j++)
234 val = bcm6368_mdio_read(priv, external_phy,
235 port->phy_id, MII_BMSR);
240 up = (val & BMSR_LSTATUS) ? 1 : 0;
241 if (!(up ^ priv->sw_port_link[i]))
244 priv->sw_port_link[i] = up;
248 dev_info(&priv->pdev->dev, "link DOWN on %s\n",
250 writeb_be(ETH_PORTOV_ENABLE_MASK,
251 priv->base + ETH_PORTOV_REG(i));
252 writeb_be(ETH_PTCTRL_RXDIS_MASK |
253 ETH_PTCTRL_TXDIS_MASK,
254 priv->base + ETH_PTCTRL_REG(i));
258 adv = bcm6368_mdio_read(priv, external_phy,
259 port->phy_id, MII_ADVERTISE);
261 lpa = bcm6368_mdio_read(priv, external_phy, port->phy_id,
264 /* figure out media and duplex from advertise and LPA values */
265 media = mii_nway_result(lpa & adv);
266 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
268 if (media & (ADVERTISE_100FULL | ADVERTISE_100HALF))
273 if (val & BMSR_ESTATEN) {
274 adv = bcm6368_mdio_read(priv, external_phy,
275 port->phy_id, MII_CTRL1000);
277 lpa = bcm6368_mdio_read(priv, external_phy,
278 port->phy_id, MII_STAT1000);
280 if ((adv & (ADVERTISE_1000FULL | ADVERTISE_1000HALF)) &&
281 (lpa & (LPA_1000FULL | LPA_1000HALF))) {
283 duplex = (lpa & LPA_1000FULL);
287 pr_alert("link UP on %s, %dMbps, %s-duplex\n",
288 port->name, speed, duplex ? "full" : "half");
290 override = ETH_PORTOV_ENABLE_MASK |
291 ETH_PORTOV_LINKUP_MASK;
294 override |= ETH_PORTOV_1000_MASK;
295 else if (speed == 100)
296 override |= ETH_PORTOV_100_MASK;
298 override |= ETH_PORTOV_FDX_MASK;
300 writeb_be(override, priv->base + ETH_PORTOV_REG(i));
301 writeb_be(0, priv->base + ETH_PTCTRL_REG(i));
307 static int bcm6368_eth_start(struct udevice *dev)
309 struct bcm6368_eth_priv *priv = dev_get_priv(dev);
312 /* disable all ports */
313 for (i = 0; i < priv->num_ports; i++) {
314 setbits_8(priv->base + ETH_PORTOV_REG(i),
315 ETH_PORTOV_ENABLE_MASK);
316 setbits_8(priv->base + ETH_PTCTRL_REG(i),
317 ETH_PTCTRL_RXDIS_MASK | ETH_PTCTRL_TXDIS_MASK);
318 priv->sw_port_link[i] = 0;
321 /* enable external ports */
322 for (i = ETH_RGMII_PORT0; i < priv->num_ports; i++) {
323 u8 rgmii_ctrl = ETH_RGMII_CTRL_GMII_CLK_EN;
325 if (!priv->used_ports[i].used)
328 if (priv->rgmii_override)
329 rgmii_ctrl |= ETH_RGMII_CTRL_MII_OVERRIDE_EN;
330 if (priv->rgmii_timing)
331 rgmii_ctrl |= ETH_RGMII_CTRL_TIMING_SEL_EN;
333 setbits_8(priv->base + ETH_RGMII_CTRL_REG(i), rgmii_ctrl);
337 setbits_8(priv->base + ETH_GMCR_REG, ETH_GMCR_RST_MIB_MASK);
339 clrbits_8(priv->base + ETH_GMCR_REG, ETH_GMCR_RST_MIB_MASK);
342 /* force CPU port state */
343 setbits_8(priv->base + ETH_IMPOV_REG,
344 ETH_IMPOV_FORCE_MASK | ETH_IMPOV_LINKUP_MASK);
346 /* enable switch forward engine */
347 setbits_8(priv->base + ETH_SWMODE_REG, ETH_SWMODE_FWD_EN_MASK);
349 /* prepare rx dma buffers */
350 for (i = 0; i < ETH_RX_DESC; i++) {
351 int ret = dma_prepare_rcv_buf(&priv->rx_dma, net_rx_packets[i],
357 /* enable dma rx channel */
358 dma_enable(&priv->rx_dma);
360 /* enable dma tx channel */
361 dma_enable(&priv->tx_dma);
363 /* apply override config for bypass_link ports here. */
364 for (i = 0; i < priv->num_ports; i++) {
365 struct bcm_enetsw_port *port;
368 port = &priv->used_ports[i];
372 if (!port->bypass_link)
375 override = ETH_PORTOV_ENABLE_MASK |
376 ETH_PORTOV_LINKUP_MASK;
378 switch (port->force_speed) {
380 override |= ETH_PORTOV_1000_MASK;
383 override |= ETH_PORTOV_100_MASK;
388 pr_warn("%s: invalid forced speed on port %s\n",
389 __func__, port->name);
393 if (port->force_duplex_full)
394 override |= ETH_PORTOV_FDX_MASK;
396 writeb_be(override, priv->base + ETH_PORTOV_REG(i));
397 writeb_be(0, priv->base + ETH_PTCTRL_REG(i));
400 bcm6368_eth_adjust_link(dev);
405 static void bcm6368_eth_stop(struct udevice *dev)
407 struct bcm6368_eth_priv *priv = dev_get_priv(dev);
410 /* disable all ports */
411 for (i = 0; i < priv->num_ports; i++) {
412 setbits_8(priv->base + ETH_PORTOV_REG(i),
413 ETH_PORTOV_ENABLE_MASK);
414 setbits_8(priv->base + ETH_PTCTRL_REG(i),
415 ETH_PTCTRL_RXDIS_MASK | ETH_PTCTRL_TXDIS_MASK);
418 /* disable external ports */
419 for (i = ETH_RGMII_PORT0; i < priv->num_ports; i++) {
420 if (!priv->used_ports[i].used)
423 clrbits_8(priv->base + ETH_RGMII_CTRL_REG(i),
424 ETH_RGMII_CTRL_GMII_CLK_EN);
427 /* disable CPU port */
428 clrbits_8(priv->base + ETH_IMPOV_REG,
429 ETH_IMPOV_FORCE_MASK | ETH_IMPOV_LINKUP_MASK);
431 /* disable switch forward engine */
432 clrbits_8(priv->base + ETH_SWMODE_REG, ETH_SWMODE_FWD_EN_MASK);
434 /* disable dma rx channel */
435 dma_disable(&priv->rx_dma);
437 /* disable dma tx channel */
438 dma_disable(&priv->tx_dma);
441 static const struct eth_ops bcm6368_eth_ops = {
442 .free_pkt = bcm6368_eth_free_pkt,
443 .recv = bcm6368_eth_recv,
444 .send = bcm6368_eth_send,
445 .start = bcm6368_eth_start,
446 .stop = bcm6368_eth_stop,
449 static const struct udevice_id bcm6368_eth_ids[] = {
450 { .compatible = "brcm,bcm6368-enet", },
454 static bool bcm6368_phy_is_external(struct bcm6368_eth_priv *priv, int phy_id)
458 for (i = 0; i < priv->num_ports; ++i) {
459 if (!priv->used_ports[i].used)
461 if (priv->used_ports[i].phy_id == phy_id)
462 return bcm_enet_port_is_rgmii(i);
468 static int bcm6368_mii_mdio_read(struct mii_dev *bus, int addr, int devaddr,
471 struct bcm6368_eth_priv *priv = bus->priv;
472 bool ext = bcm6368_phy_is_external(priv, addr);
474 return bcm6368_mdio_read(priv, ext, addr, reg);
477 static int bcm6368_mii_mdio_write(struct mii_dev *bus, int addr, int devaddr,
480 struct bcm6368_eth_priv *priv = bus->priv;
481 bool ext = bcm6368_phy_is_external(priv, addr);
483 return bcm6368_mdio_write(priv, ext, addr, reg, data);
486 static int bcm6368_mdio_init(const char *name, struct bcm6368_eth_priv *priv)
492 pr_err("%s: failed to allocate MDIO bus\n", __func__);
496 bus->read = bcm6368_mii_mdio_read;
497 bus->write = bcm6368_mii_mdio_write;
499 snprintf(bus->name, sizeof(bus->name), "%s", name);
501 return mdio_register(bus);
504 static int bcm6368_eth_probe(struct udevice *dev)
506 struct eth_pdata *pdata = dev_get_platdata(dev);
507 struct bcm6368_eth_priv *priv = dev_get_priv(dev);
508 int num_ports, ret, i;
511 /* get base address */
512 priv->base = dev_remap_addr(dev);
515 pdata->iobase = (phys_addr_t) priv->base;
517 /* get number of ports */
518 num_ports = dev_read_u32_default(dev, "brcm,num-ports", ETH_MAX_PORT);
519 if (!num_ports || num_ports > ETH_MAX_PORT)
522 /* get dma channels */
523 ret = dma_get_by_name(dev, "tx", &priv->tx_dma);
527 ret = dma_get_by_name(dev, "rx", &priv->rx_dma);
531 /* try to enable clocks */
536 ret = clk_get_by_index(dev, i, &clk);
540 ret = clk_enable(&clk);
542 pr_err("%s: error enabling clock %d\n", __func__, i);
546 ret = clk_free(&clk);
548 pr_err("%s: error freeing clock %d\n", __func__, i);
553 /* try to perform resets */
555 struct reset_ctl reset;
558 ret = reset_get_by_index(dev, i, &reset);
562 ret = reset_deassert(&reset);
564 pr_err("%s: error deasserting reset %d\n", __func__, i);
568 ret = reset_free(&reset);
570 pr_err("%s: error freeing reset %d\n", __func__, i);
576 priv->num_ports = num_ports;
577 if (dev_read_bool(dev, "brcm,rgmii-override"))
578 priv->rgmii_override = true;
579 if (dev_read_bool(dev, "brcm,rgmii-timing"))
580 priv->rgmii_timing = true;
583 dev_for_each_subnode(node, dev) {
590 comp = ofnode_read_string(node, "compatible");
591 if (!comp || memcmp(comp, ETH_PORT_STR, sizeof(ETH_PORT_STR)))
594 p = ofnode_read_u32_default(node, "reg", ETH_MAX_PORT);
598 label = ofnode_read_string(node, "label");
600 debug("%s: node %s has no label\n", __func__,
601 ofnode_get_name(node));
605 phy_id = ofnode_read_u32_default(node, "brcm,phy-id", -1);
607 priv->used_ports[p].used = true;
608 priv->used_ports[p].name = label;
609 priv->used_ports[p].phy_id = phy_id;
611 if (ofnode_read_bool(node, "full-duplex"))
612 priv->used_ports[p].force_duplex_full = true;
613 if (ofnode_read_bool(node, "bypass-link"))
614 priv->used_ports[p].bypass_link = true;
615 speed = ofnode_read_u32_default(node, "speed", 0);
617 priv->used_ports[p].force_speed = speed;
621 ret = bcm6368_mdio_init(dev->name, priv);
625 /* enable jumbo on all ports */
626 writel_be(0x1ff, priv->base + ETH_JMBCTL_PORT_REG);
627 writew_be(9728, priv->base + ETH_JMBCTL_MAXSIZE_REG);
632 U_BOOT_DRIVER(bcm6368_eth) = {
633 .name = "bcm6368_eth",
635 .of_match = bcm6368_eth_ids,
636 .ops = &bcm6368_eth_ops,
637 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
638 .priv_auto_alloc_size = sizeof(struct bcm6368_eth_priv),
639 .probe = bcm6368_eth_probe,