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>
22 #define ETH_RX_DESC PKTBUFSRX
23 #define ETH_MAX_MTU_SIZE 1518
24 #define ETH_TIMEOUT 100
25 #define ETH_TX_WATERMARK 32
27 /* ETH Receiver Configuration register */
28 #define ETH_RXCFG_REG 0x00
29 #define ETH_RXCFG_ENFLOW_SHIFT 5
30 #define ETH_RXCFG_ENFLOW_MASK (1 << ETH_RXCFG_ENFLOW_SHIFT)
32 /* ETH Receive Maximum Length register */
33 #define ETH_RXMAXLEN_REG 0x04
34 #define ETH_RXMAXLEN_SHIFT 0
35 #define ETH_RXMAXLEN_MASK (0x7ff << ETH_RXMAXLEN_SHIFT)
37 /* ETH Transmit Maximum Length register */
38 #define ETH_TXMAXLEN_REG 0x08
39 #define ETH_TXMAXLEN_SHIFT 0
40 #define ETH_TXMAXLEN_MASK (0x7ff << ETH_TXMAXLEN_SHIFT)
42 /* MII Status/Control register */
43 #define MII_SC_REG 0x10
44 #define MII_SC_MDCFREQDIV_SHIFT 0
45 #define MII_SC_MDCFREQDIV_MASK (0x7f << MII_SC_MDCFREQDIV_SHIFT)
46 #define MII_SC_PREAMBLE_EN_SHIFT 7
47 #define MII_SC_PREAMBLE_EN_MASK (1 << MII_SC_PREAMBLE_EN_SHIFT)
49 /* MII Data register */
50 #define MII_DAT_REG 0x14
51 #define MII_DAT_DATA_SHIFT 0
52 #define MII_DAT_DATA_MASK (0xffff << MII_DAT_DATA_SHIFT)
53 #define MII_DAT_TA_SHIFT 16
54 #define MII_DAT_TA_MASK (0x3 << MII_DAT_TA_SHIFT)
55 #define MII_DAT_REG_SHIFT 18
56 #define MII_DAT_REG_MASK (0x1f << MII_DAT_REG_SHIFT)
57 #define MII_DAT_PHY_SHIFT 23
58 #define MII_DAT_PHY_MASK (0x1f << MII_DAT_PHY_SHIFT)
59 #define MII_DAT_OP_SHIFT 28
60 #define MII_DAT_OP_WRITE (0x5 << MII_DAT_OP_SHIFT)
61 #define MII_DAT_OP_READ (0x6 << MII_DAT_OP_SHIFT)
63 /* ETH Interrupts Mask register */
64 #define ETH_IRMASK_REG 0x18
66 /* ETH Interrupts register */
67 #define ETH_IR_REG 0x1c
68 #define ETH_IR_MII_SHIFT 0
69 #define ETH_IR_MII_MASK (1 << ETH_IR_MII_SHIFT)
71 /* ETH Control register */
72 #define ETH_CTL_REG 0x2c
73 #define ETH_CTL_ENABLE_SHIFT 0
74 #define ETH_CTL_ENABLE_MASK (1 << ETH_CTL_ENABLE_SHIFT)
75 #define ETH_CTL_DISABLE_SHIFT 1
76 #define ETH_CTL_DISABLE_MASK (1 << ETH_CTL_DISABLE_SHIFT)
77 #define ETH_CTL_RESET_SHIFT 2
78 #define ETH_CTL_RESET_MASK (1 << ETH_CTL_RESET_SHIFT)
79 #define ETH_CTL_EPHY_SHIFT 3
80 #define ETH_CTL_EPHY_MASK (1 << ETH_CTL_EPHY_SHIFT)
82 /* ETH Transmit Control register */
83 #define ETH_TXCTL_REG 0x30
84 #define ETH_TXCTL_FD_SHIFT 0
85 #define ETH_TXCTL_FD_MASK (1 << ETH_TXCTL_FD_SHIFT)
87 /* ETH Transmit Watermask register */
88 #define ETH_TXWMARK_REG 0x34
89 #define ETH_TXWMARK_WM_SHIFT 0
90 #define ETH_TXWMARK_WM_MASK (0x3f << ETH_TXWMARK_WM_SHIFT)
92 /* MIB Control register */
93 #define MIB_CTL_REG 0x38
94 #define MIB_CTL_RDCLEAR_SHIFT 0
95 #define MIB_CTL_RDCLEAR_MASK (1 << MIB_CTL_RDCLEAR_SHIFT)
97 /* ETH Perfect Match registers */
99 #define ETH_PML_REG(x) (0x58 + (x) * 0x8)
100 #define ETH_PMH_REG(x) (0x5c + (x) * 0x8)
101 #define ETH_PMH_VALID_SHIFT 16
102 #define ETH_PMH_VALID_MASK (1 << ETH_PMH_VALID_SHIFT)
104 /* MIB Counters registers */
105 #define MIB_REG_CNT 55
106 #define MIB_REG(x) (0x200 + (x) * 4)
109 struct bcm6348_eth_priv {
116 struct phy_device *phy_dev;
119 static void bcm6348_eth_mac_disable(struct bcm6348_eth_priv *priv)
122 clrsetbits_be32(priv->base + ETH_CTL_REG, ETH_CTL_ENABLE_MASK,
123 ETH_CTL_DISABLE_MASK);
125 /* wait until emac is disabled */
126 if (wait_for_bit_be32(priv->base + ETH_CTL_REG,
127 ETH_CTL_DISABLE_MASK, false,
129 pr_err("%s: error disabling emac\n", __func__);
132 static void bcm6348_eth_mac_enable(struct bcm6348_eth_priv *priv)
134 setbits_be32(priv->base + ETH_CTL_REG, ETH_CTL_ENABLE_MASK);
137 static void bcm6348_eth_mac_reset(struct bcm6348_eth_priv *priv)
140 writel_be(ETH_CTL_RESET_MASK, priv->base + ETH_CTL_REG);
143 /* wait until emac is reset */
144 if (wait_for_bit_be32(priv->base + ETH_CTL_REG,
145 ETH_CTL_RESET_MASK, false,
147 pr_err("%s: error resetting emac\n", __func__);
150 static int bcm6348_eth_free_pkt(struct udevice *dev, uchar *packet, int len)
152 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
154 return dma_prepare_rcv_buf(&priv->rx_dma, packet, len);
157 static int bcm6348_eth_recv(struct udevice *dev, int flags, uchar **packetp)
159 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
161 return dma_receive(&priv->rx_dma, (void**)packetp, NULL);
164 static int bcm6348_eth_send(struct udevice *dev, void *packet, int length)
166 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
168 return dma_send(&priv->tx_dma, packet, length, NULL);
171 static int bcm6348_eth_adjust_link(struct udevice *dev,
172 struct phy_device *phydev)
174 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
176 /* mac duplex parameters */
178 setbits_be32(priv->base + ETH_TXCTL_REG, ETH_TXCTL_FD_MASK);
180 clrbits_be32(priv->base + ETH_TXCTL_REG, ETH_TXCTL_FD_MASK);
182 /* rx flow control (pause frame handling) */
184 setbits_be32(priv->base + ETH_RXCFG_REG,
185 ETH_RXCFG_ENFLOW_MASK);
187 clrbits_be32(priv->base + ETH_RXCFG_REG,
188 ETH_RXCFG_ENFLOW_MASK);
193 static int bcm6348_eth_start(struct udevice *dev)
195 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
198 /* prepare rx dma buffers */
199 for (i = 0; i < ETH_RX_DESC; i++) {
200 ret = dma_prepare_rcv_buf(&priv->rx_dma, net_rx_packets[i],
206 /* enable dma rx channel */
207 dma_enable(&priv->rx_dma);
209 /* enable dma tx channel */
210 dma_enable(&priv->tx_dma);
212 ret = phy_startup(priv->phy_dev);
214 pr_err("%s: could not initialize phy\n", __func__);
218 if (!priv->phy_dev->link) {
219 pr_err("%s: no phy link\n", __func__);
223 bcm6348_eth_adjust_link(dev, priv->phy_dev);
225 /* zero mib counters */
226 for (i = 0; i < MIB_REG_CNT; i++)
227 writel_be(0, MIB_REG(i));
229 /* enable rx flow control */
230 setbits_be32(priv->base + ETH_RXCFG_REG, ETH_RXCFG_ENFLOW_MASK);
232 /* set max rx/tx length */
233 writel_be((ETH_MAX_MTU_SIZE << ETH_RXMAXLEN_SHIFT) &
234 ETH_RXMAXLEN_MASK, priv->base + ETH_RXMAXLEN_REG);
235 writel_be((ETH_MAX_MTU_SIZE << ETH_TXMAXLEN_SHIFT) &
236 ETH_TXMAXLEN_MASK, priv->base + ETH_TXMAXLEN_REG);
238 /* set correct transmit fifo watermark */
239 writel_be((ETH_TX_WATERMARK << ETH_TXWMARK_WM_SHIFT) &
240 ETH_TXWMARK_WM_MASK, priv->base + ETH_TXWMARK_REG);
243 bcm6348_eth_mac_enable(priv);
245 /* clear interrupts */
246 writel_be(0, priv->base + ETH_IRMASK_REG);
251 static void bcm6348_eth_stop(struct udevice *dev)
253 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
255 /* disable dma rx channel */
256 dma_disable(&priv->rx_dma);
258 /* disable dma tx channel */
259 dma_disable(&priv->tx_dma);
262 bcm6348_eth_mac_disable(priv);
265 static int bcm6348_eth_write_hwaddr(struct udevice *dev)
267 struct eth_pdata *pdata = dev_get_plat(dev);
268 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
269 bool running = false;
271 /* check if emac is running */
272 if (readl_be(priv->base + ETH_CTL_REG) & ETH_CTL_ENABLE_MASK)
277 bcm6348_eth_mac_disable(priv);
279 /* set mac address */
280 writel_be((pdata->enetaddr[2] << 24) | (pdata->enetaddr[3]) << 16 |
281 (pdata->enetaddr[4]) << 8 | (pdata->enetaddr[5]),
282 priv->base + ETH_PML_REG(0));
283 writel_be((pdata->enetaddr[1]) | (pdata->enetaddr[0] << 8) |
284 ETH_PMH_VALID_MASK, priv->base + ETH_PMH_REG(0));
288 bcm6348_eth_mac_enable(priv);
293 static const struct eth_ops bcm6348_eth_ops = {
294 .free_pkt = bcm6348_eth_free_pkt,
295 .recv = bcm6348_eth_recv,
296 .send = bcm6348_eth_send,
297 .start = bcm6348_eth_start,
298 .stop = bcm6348_eth_stop,
299 .write_hwaddr = bcm6348_eth_write_hwaddr,
302 static const struct udevice_id bcm6348_eth_ids[] = {
303 { .compatible = "brcm,bcm6348-enet", },
307 static int bcm6348_mdio_op(void __iomem *base, uint32_t data)
309 /* make sure mii interrupt status is cleared */
310 writel_be(ETH_IR_MII_MASK, base + ETH_IR_REG);
313 writel_be(data, base + MII_DAT_REG);
315 /* wait until emac is disabled */
316 return wait_for_bit_be32(base + ETH_IR_REG,
317 ETH_IR_MII_MASK, true,
321 static int bcm6348_mdio_read(struct mii_dev *bus, int addr, int devaddr,
324 void __iomem *base = bus->priv;
327 val = MII_DAT_OP_READ;
328 val |= (reg << MII_DAT_REG_SHIFT) & MII_DAT_REG_MASK;
329 val |= (0x2 << MII_DAT_TA_SHIFT) & MII_DAT_TA_MASK;
330 val |= (addr << MII_DAT_PHY_SHIFT) & MII_DAT_PHY_MASK;
332 if (bcm6348_mdio_op(base, val)) {
333 pr_err("%s: timeout\n", __func__);
337 val = readl_be(base + MII_DAT_REG) & MII_DAT_DATA_MASK;
338 val >>= MII_DAT_DATA_SHIFT;
343 static int bcm6348_mdio_write(struct mii_dev *bus, int addr, int dev_addr,
346 void __iomem *base = bus->priv;
349 val = MII_DAT_OP_WRITE;
350 val |= (reg << MII_DAT_REG_SHIFT) & MII_DAT_REG_MASK;
351 val |= (0x2 << MII_DAT_TA_SHIFT) & MII_DAT_TA_MASK;
352 val |= (addr << MII_DAT_PHY_SHIFT) & MII_DAT_PHY_MASK;
353 val |= (value << MII_DAT_DATA_SHIFT) & MII_DAT_DATA_MASK;
355 if (bcm6348_mdio_op(base, val)) {
356 pr_err("%s: timeout\n", __func__);
363 static int bcm6348_mdio_init(const char *name, void __iomem *base)
369 pr_err("%s: failed to allocate MDIO bus\n", __func__);
373 bus->read = bcm6348_mdio_read;
374 bus->write = bcm6348_mdio_write;
376 snprintf(bus->name, sizeof(bus->name), "%s", name);
378 return mdio_register(bus);
381 static int bcm6348_phy_init(struct udevice *dev)
383 struct eth_pdata *pdata = dev_get_plat(dev);
384 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
388 bus = miiphy_get_dev_by_name(dev->name);
391 priv->phy_dev = phy_connect(bus, priv->phy_id, dev,
392 pdata->phy_interface);
393 if (!priv->phy_dev) {
394 pr_err("%s: no phy device\n", __func__);
398 priv->phy_dev->supported = (SUPPORTED_10baseT_Half |
399 SUPPORTED_10baseT_Full |
400 SUPPORTED_100baseT_Half |
401 SUPPORTED_100baseT_Full |
405 priv->phy_dev->advertising = priv->phy_dev->supported;
408 phy_config(priv->phy_dev);
413 static int bcm6348_eth_probe(struct udevice *dev)
415 struct eth_pdata *pdata = dev_get_plat(dev);
416 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
417 struct ofnode_phandle_args phy;
420 /* get base address */
421 priv->base = dev_remap_addr(dev);
424 pdata->iobase = (phys_addr_t) priv->base;
427 pdata->phy_interface = dev_read_phy_mode(dev);
428 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
432 if (dev_read_phandle_with_args(dev, "phy", NULL, 0, 0, &phy))
434 priv->phy_id = ofnode_read_u32_default(phy.node, "reg", -1);
436 /* get dma channels */
437 ret = dma_get_by_name(dev, "tx", &priv->tx_dma);
441 ret = dma_get_by_name(dev, "rx", &priv->rx_dma);
445 /* try to enable clocks */
450 ret = clk_get_by_index(dev, i, &clk);
454 ret = clk_enable(&clk);
456 pr_err("%s: error enabling clock %d\n", __func__, i);
463 /* try to perform resets */
465 struct reset_ctl reset;
468 ret = reset_get_by_index(dev, i, &reset);
472 ret = reset_deassert(&reset);
474 pr_err("%s: error deasserting reset %d\n", __func__, i);
478 ret = reset_free(&reset);
480 pr_err("%s: error freeing reset %d\n", __func__, i);
486 bcm6348_eth_mac_disable(priv);
489 bcm6348_eth_mac_reset(priv);
491 /* select correct mii interface */
492 if (pdata->phy_interface == PHY_INTERFACE_MODE_INTERNAL)
493 clrbits_be32(priv->base + ETH_CTL_REG, ETH_CTL_EPHY_MASK);
495 setbits_be32(priv->base + ETH_CTL_REG, ETH_CTL_EPHY_MASK);
497 /* turn on mdc clock */
498 writel_be((0x1f << MII_SC_MDCFREQDIV_SHIFT) |
499 MII_SC_PREAMBLE_EN_MASK, priv->base + MII_SC_REG);
501 /* set mib counters to not clear when read */
502 clrbits_be32(priv->base + MIB_CTL_REG, MIB_CTL_RDCLEAR_MASK);
504 /* initialize perfect match registers */
505 for (i = 0; i < ETH_PM_CNT; i++) {
506 writel_be(0, priv->base + ETH_PML_REG(i));
507 writel_be(0, priv->base + ETH_PMH_REG(i));
511 ret = bcm6348_mdio_init(dev->name, priv->base);
516 ret = bcm6348_phy_init(dev);
523 U_BOOT_DRIVER(bcm6348_eth) = {
524 .name = "bcm6348_eth",
526 .of_match = bcm6348_eth_ids,
527 .ops = &bcm6348_eth_ops,
528 .plat_auto = sizeof(struct eth_pdata),
529 .priv_auto = sizeof(struct bcm6348_eth_priv),
530 .probe = bcm6348_eth_probe,