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>
20 #define ETH_RX_DESC PKTBUFSRX
21 #define ETH_MAX_MTU_SIZE 1518
22 #define ETH_TIMEOUT 100
23 #define ETH_TX_WATERMARK 32
25 /* ETH Receiver Configuration register */
26 #define ETH_RXCFG_REG 0x00
27 #define ETH_RXCFG_ENFLOW_SHIFT 5
28 #define ETH_RXCFG_ENFLOW_MASK (1 << ETH_RXCFG_ENFLOW_SHIFT)
30 /* ETH Receive Maximum Length register */
31 #define ETH_RXMAXLEN_REG 0x04
32 #define ETH_RXMAXLEN_SHIFT 0
33 #define ETH_RXMAXLEN_MASK (0x7ff << ETH_RXMAXLEN_SHIFT)
35 /* ETH Transmit Maximum Length register */
36 #define ETH_TXMAXLEN_REG 0x08
37 #define ETH_TXMAXLEN_SHIFT 0
38 #define ETH_TXMAXLEN_MASK (0x7ff << ETH_TXMAXLEN_SHIFT)
40 /* MII Status/Control register */
41 #define MII_SC_REG 0x10
42 #define MII_SC_MDCFREQDIV_SHIFT 0
43 #define MII_SC_MDCFREQDIV_MASK (0x7f << MII_SC_MDCFREQDIV_SHIFT)
44 #define MII_SC_PREAMBLE_EN_SHIFT 7
45 #define MII_SC_PREAMBLE_EN_MASK (1 << MII_SC_PREAMBLE_EN_SHIFT)
47 /* MII Data register */
48 #define MII_DAT_REG 0x14
49 #define MII_DAT_DATA_SHIFT 0
50 #define MII_DAT_DATA_MASK (0xffff << MII_DAT_DATA_SHIFT)
51 #define MII_DAT_TA_SHIFT 16
52 #define MII_DAT_TA_MASK (0x3 << MII_DAT_TA_SHIFT)
53 #define MII_DAT_REG_SHIFT 18
54 #define MII_DAT_REG_MASK (0x1f << MII_DAT_REG_SHIFT)
55 #define MII_DAT_PHY_SHIFT 23
56 #define MII_DAT_PHY_MASK (0x1f << MII_DAT_PHY_SHIFT)
57 #define MII_DAT_OP_SHIFT 28
58 #define MII_DAT_OP_WRITE (0x5 << MII_DAT_OP_SHIFT)
59 #define MII_DAT_OP_READ (0x6 << MII_DAT_OP_SHIFT)
61 /* ETH Interrupts Mask register */
62 #define ETH_IRMASK_REG 0x18
64 /* ETH Interrupts register */
65 #define ETH_IR_REG 0x1c
66 #define ETH_IR_MII_SHIFT 0
67 #define ETH_IR_MII_MASK (1 << ETH_IR_MII_SHIFT)
69 /* ETH Control register */
70 #define ETH_CTL_REG 0x2c
71 #define ETH_CTL_ENABLE_SHIFT 0
72 #define ETH_CTL_ENABLE_MASK (1 << ETH_CTL_ENABLE_SHIFT)
73 #define ETH_CTL_DISABLE_SHIFT 1
74 #define ETH_CTL_DISABLE_MASK (1 << ETH_CTL_DISABLE_SHIFT)
75 #define ETH_CTL_RESET_SHIFT 2
76 #define ETH_CTL_RESET_MASK (1 << ETH_CTL_RESET_SHIFT)
77 #define ETH_CTL_EPHY_SHIFT 3
78 #define ETH_CTL_EPHY_MASK (1 << ETH_CTL_EPHY_SHIFT)
80 /* ETH Transmit Control register */
81 #define ETH_TXCTL_REG 0x30
82 #define ETH_TXCTL_FD_SHIFT 0
83 #define ETH_TXCTL_FD_MASK (1 << ETH_TXCTL_FD_SHIFT)
85 /* ETH Transmit Watermask register */
86 #define ETH_TXWMARK_REG 0x34
87 #define ETH_TXWMARK_WM_SHIFT 0
88 #define ETH_TXWMARK_WM_MASK (0x3f << ETH_TXWMARK_WM_SHIFT)
90 /* MIB Control register */
91 #define MIB_CTL_REG 0x38
92 #define MIB_CTL_RDCLEAR_SHIFT 0
93 #define MIB_CTL_RDCLEAR_MASK (1 << MIB_CTL_RDCLEAR_SHIFT)
95 /* ETH Perfect Match registers */
97 #define ETH_PML_REG(x) (0x58 + (x) * 0x8)
98 #define ETH_PMH_REG(x) (0x5c + (x) * 0x8)
99 #define ETH_PMH_VALID_SHIFT 16
100 #define ETH_PMH_VALID_MASK (1 << ETH_PMH_VALID_SHIFT)
102 /* MIB Counters registers */
103 #define MIB_REG_CNT 55
104 #define MIB_REG(x) (0x200 + (x) * 4)
107 struct bcm6348_eth_priv {
114 struct phy_device *phy_dev;
117 static void bcm6348_eth_mac_disable(struct bcm6348_eth_priv *priv)
120 clrsetbits_be32(priv->base + ETH_CTL_REG, ETH_CTL_ENABLE_MASK,
121 ETH_CTL_DISABLE_MASK);
123 /* wait until emac is disabled */
124 if (wait_for_bit_be32(priv->base + ETH_CTL_REG,
125 ETH_CTL_DISABLE_MASK, false,
127 pr_err("%s: error disabling emac\n", __func__);
130 static void bcm6348_eth_mac_enable(struct bcm6348_eth_priv *priv)
132 setbits_be32(priv->base + ETH_CTL_REG, ETH_CTL_ENABLE_MASK);
135 static void bcm6348_eth_mac_reset(struct bcm6348_eth_priv *priv)
138 writel_be(ETH_CTL_RESET_MASK, priv->base + ETH_CTL_REG);
141 /* wait until emac is reset */
142 if (wait_for_bit_be32(priv->base + ETH_CTL_REG,
143 ETH_CTL_RESET_MASK, false,
145 pr_err("%s: error resetting emac\n", __func__);
148 static int bcm6348_eth_free_pkt(struct udevice *dev, uchar *packet, int len)
150 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
152 return dma_prepare_rcv_buf(&priv->rx_dma, packet, len);
155 static int bcm6348_eth_recv(struct udevice *dev, int flags, uchar **packetp)
157 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
159 return dma_receive(&priv->rx_dma, (void**)packetp, NULL);
162 static int bcm6348_eth_send(struct udevice *dev, void *packet, int length)
164 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
166 return dma_send(&priv->tx_dma, packet, length, NULL);
169 static int bcm6348_eth_adjust_link(struct udevice *dev,
170 struct phy_device *phydev)
172 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
174 /* mac duplex parameters */
176 setbits_be32(priv->base + ETH_TXCTL_REG, ETH_TXCTL_FD_MASK);
178 clrbits_be32(priv->base + ETH_TXCTL_REG, ETH_TXCTL_FD_MASK);
180 /* rx flow control (pause frame handling) */
182 setbits_be32(priv->base + ETH_RXCFG_REG,
183 ETH_RXCFG_ENFLOW_MASK);
185 clrbits_be32(priv->base + ETH_RXCFG_REG,
186 ETH_RXCFG_ENFLOW_MASK);
191 static int bcm6348_eth_start(struct udevice *dev)
193 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
196 /* prepare rx dma buffers */
197 for (i = 0; i < ETH_RX_DESC; i++) {
198 ret = dma_prepare_rcv_buf(&priv->rx_dma, net_rx_packets[i],
204 /* enable dma rx channel */
205 dma_enable(&priv->rx_dma);
207 /* enable dma tx channel */
208 dma_enable(&priv->tx_dma);
210 ret = phy_startup(priv->phy_dev);
212 pr_err("%s: could not initialize phy\n", __func__);
216 if (!priv->phy_dev->link) {
217 pr_err("%s: no phy link\n", __func__);
221 bcm6348_eth_adjust_link(dev, priv->phy_dev);
223 /* zero mib counters */
224 for (i = 0; i < MIB_REG_CNT; i++)
225 writel_be(0, MIB_REG(i));
227 /* enable rx flow control */
228 setbits_be32(priv->base + ETH_RXCFG_REG, ETH_RXCFG_ENFLOW_MASK);
230 /* set max rx/tx length */
231 writel_be((ETH_MAX_MTU_SIZE << ETH_RXMAXLEN_SHIFT) &
232 ETH_RXMAXLEN_MASK, priv->base + ETH_RXMAXLEN_REG);
233 writel_be((ETH_MAX_MTU_SIZE << ETH_TXMAXLEN_SHIFT) &
234 ETH_TXMAXLEN_MASK, priv->base + ETH_TXMAXLEN_REG);
236 /* set correct transmit fifo watermark */
237 writel_be((ETH_TX_WATERMARK << ETH_TXWMARK_WM_SHIFT) &
238 ETH_TXWMARK_WM_MASK, priv->base + ETH_TXWMARK_REG);
241 bcm6348_eth_mac_enable(priv);
243 /* clear interrupts */
244 writel_be(0, priv->base + ETH_IRMASK_REG);
249 static void bcm6348_eth_stop(struct udevice *dev)
251 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
253 /* disable dma rx channel */
254 dma_disable(&priv->rx_dma);
256 /* disable dma tx channel */
257 dma_disable(&priv->tx_dma);
260 bcm6348_eth_mac_disable(priv);
263 static int bcm6348_eth_write_hwaddr(struct udevice *dev)
265 struct eth_pdata *pdata = dev_get_platdata(dev);
266 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
267 bool running = false;
269 /* check if emac is running */
270 if (readl_be(priv->base + ETH_CTL_REG) & ETH_CTL_ENABLE_MASK)
275 bcm6348_eth_mac_disable(priv);
277 /* set mac address */
278 writel_be((pdata->enetaddr[2] << 24) | (pdata->enetaddr[3]) << 16 |
279 (pdata->enetaddr[4]) << 8 | (pdata->enetaddr[5]),
280 priv->base + ETH_PML_REG(0));
281 writel_be((pdata->enetaddr[1]) | (pdata->enetaddr[0] << 8) |
282 ETH_PMH_VALID_MASK, priv->base + ETH_PMH_REG(0));
286 bcm6348_eth_mac_enable(priv);
291 static const struct eth_ops bcm6348_eth_ops = {
292 .free_pkt = bcm6348_eth_free_pkt,
293 .recv = bcm6348_eth_recv,
294 .send = bcm6348_eth_send,
295 .start = bcm6348_eth_start,
296 .stop = bcm6348_eth_stop,
297 .write_hwaddr = bcm6348_eth_write_hwaddr,
300 static const struct udevice_id bcm6348_eth_ids[] = {
301 { .compatible = "brcm,bcm6348-enet", },
305 static int bcm6348_mdio_op(void __iomem *base, uint32_t data)
307 /* make sure mii interrupt status is cleared */
308 writel_be(ETH_IR_MII_MASK, base + ETH_IR_REG);
311 writel_be(data, base + MII_DAT_REG);
313 /* wait until emac is disabled */
314 return wait_for_bit_be32(base + ETH_IR_REG,
315 ETH_IR_MII_MASK, true,
319 static int bcm6348_mdio_read(struct mii_dev *bus, int addr, int devaddr,
322 void __iomem *base = bus->priv;
325 val = MII_DAT_OP_READ;
326 val |= (reg << MII_DAT_REG_SHIFT) & MII_DAT_REG_MASK;
327 val |= (0x2 << MII_DAT_TA_SHIFT) & MII_DAT_TA_MASK;
328 val |= (addr << MII_DAT_PHY_SHIFT) & MII_DAT_PHY_MASK;
330 if (bcm6348_mdio_op(base, val)) {
331 pr_err("%s: timeout\n", __func__);
335 val = readl_be(base + MII_DAT_REG) & MII_DAT_DATA_MASK;
336 val >>= MII_DAT_DATA_SHIFT;
341 static int bcm6348_mdio_write(struct mii_dev *bus, int addr, int dev_addr,
344 void __iomem *base = bus->priv;
347 val = MII_DAT_OP_WRITE;
348 val |= (reg << MII_DAT_REG_SHIFT) & MII_DAT_REG_MASK;
349 val |= (0x2 << MII_DAT_TA_SHIFT) & MII_DAT_TA_MASK;
350 val |= (addr << MII_DAT_PHY_SHIFT) & MII_DAT_PHY_MASK;
351 val |= (value << MII_DAT_DATA_SHIFT) & MII_DAT_DATA_MASK;
353 if (bcm6348_mdio_op(base, val)) {
354 pr_err("%s: timeout\n", __func__);
361 static int bcm6348_mdio_init(const char *name, void __iomem *base)
367 pr_err("%s: failed to allocate MDIO bus\n", __func__);
371 bus->read = bcm6348_mdio_read;
372 bus->write = bcm6348_mdio_write;
374 snprintf(bus->name, sizeof(bus->name), "%s", name);
376 return mdio_register(bus);
379 static int bcm6348_phy_init(struct udevice *dev)
381 struct eth_pdata *pdata = dev_get_platdata(dev);
382 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
386 bus = miiphy_get_dev_by_name(dev->name);
389 priv->phy_dev = phy_connect(bus, priv->phy_id, dev,
390 pdata->phy_interface);
391 if (!priv->phy_dev) {
392 pr_err("%s: no phy device\n", __func__);
396 priv->phy_dev->supported = (SUPPORTED_10baseT_Half |
397 SUPPORTED_10baseT_Full |
398 SUPPORTED_100baseT_Half |
399 SUPPORTED_100baseT_Full |
403 priv->phy_dev->advertising = priv->phy_dev->supported;
406 phy_config(priv->phy_dev);
411 static int bcm6348_eth_probe(struct udevice *dev)
413 struct eth_pdata *pdata = dev_get_platdata(dev);
414 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
415 struct ofnode_phandle_args phy;
416 const char *phy_mode;
419 /* get base address */
420 priv->base = dev_remap_addr(dev);
423 pdata->iobase = (phys_addr_t) priv->base;
426 pdata->phy_interface = PHY_INTERFACE_MODE_NONE;
427 phy_mode = dev_read_string(dev, "phy-mode");
429 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
430 if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
434 if (dev_read_phandle_with_args(dev, "phy", NULL, 0, 0, &phy))
436 priv->phy_id = ofnode_read_u32_default(phy.node, "reg", -1);
438 /* get dma channels */
439 ret = dma_get_by_name(dev, "tx", &priv->tx_dma);
443 ret = dma_get_by_name(dev, "rx", &priv->rx_dma);
447 /* try to enable clocks */
452 ret = clk_get_by_index(dev, i, &clk);
456 ret = clk_enable(&clk);
458 pr_err("%s: error enabling clock %d\n", __func__, i);
462 ret = clk_free(&clk);
464 pr_err("%s: error freeing clock %d\n", __func__, i);
469 /* try to perform resets */
471 struct reset_ctl reset;
474 ret = reset_get_by_index(dev, i, &reset);
478 ret = reset_deassert(&reset);
480 pr_err("%s: error deasserting reset %d\n", __func__, i);
484 ret = reset_free(&reset);
486 pr_err("%s: error freeing reset %d\n", __func__, i);
492 bcm6348_eth_mac_disable(priv);
495 bcm6348_eth_mac_reset(priv);
497 /* select correct mii interface */
498 if (pdata->phy_interface == PHY_INTERFACE_MODE_INTERNAL)
499 clrbits_be32(priv->base + ETH_CTL_REG, ETH_CTL_EPHY_MASK);
501 setbits_be32(priv->base + ETH_CTL_REG, ETH_CTL_EPHY_MASK);
503 /* turn on mdc clock */
504 writel_be((0x1f << MII_SC_MDCFREQDIV_SHIFT) |
505 MII_SC_PREAMBLE_EN_MASK, priv->base + MII_SC_REG);
507 /* set mib counters to not clear when read */
508 clrbits_be32(priv->base + MIB_CTL_REG, MIB_CTL_RDCLEAR_MASK);
510 /* initialize perfect match registers */
511 for (i = 0; i < ETH_PM_CNT; i++) {
512 writel_be(0, priv->base + ETH_PML_REG(i));
513 writel_be(0, priv->base + ETH_PMH_REG(i));
517 ret = bcm6348_mdio_init(dev->name, priv->base);
522 ret = bcm6348_phy_init(dev);
529 U_BOOT_DRIVER(bcm6348_eth) = {
530 .name = "bcm6348_eth",
532 .of_match = bcm6348_eth_ids,
533 .ops = &bcm6348_eth_ops,
534 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
535 .priv_auto_alloc_size = sizeof(struct bcm6348_eth_priv),
536 .probe = bcm6348_eth_probe,