blob: f87db4ab46e4d67b2ad0f1f13b788bc629e6a0b4 [file] [log] [blame]
Álvaro Fernández Rojas55e55fe2018-12-01 19:00:24 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018 Álvaro Fernández Rojas <noltari@gmail.com>
4 *
5 * Derived from linux/drivers/net/ethernet/broadcom/bcm63xx_enet.c:
6 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
7 */
8
Álvaro Fernández Rojas55e55fe2018-12-01 19:00:24 +01009#include <clk.h>
10#include <dm.h>
11#include <dma.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070013#include <malloc.h>
Álvaro Fernández Rojas55e55fe2018-12-01 19:00:24 +010014#include <miiphy.h>
15#include <net.h>
16#include <phy.h>
17#include <reset.h>
18#include <wait_bit.h>
19#include <asm/io.h>
Simon Glass1e94b462023-09-14 18:21:46 -060020#include <linux/printk.h>
Álvaro Fernández Rojas55e55fe2018-12-01 19:00:24 +010021
22#define ETH_RX_DESC PKTBUFSRX
23#define ETH_MAX_MTU_SIZE 1518
24#define ETH_TIMEOUT 100
25#define ETH_TX_WATERMARK 32
26
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)
31
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)
36
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)
41
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)
48
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)
62
63/* ETH Interrupts Mask register */
64#define ETH_IRMASK_REG 0x18
65
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)
70
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)
81
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)
86
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)
91
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)
96
97/* ETH Perfect Match registers */
98#define ETH_PM_CNT 4
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)
103
104/* MIB Counters registers */
105#define MIB_REG_CNT 55
106#define MIB_REG(x) (0x200 + (x) * 4)
107
108/* ETH data */
109struct bcm6348_eth_priv {
110 void __iomem *base;
111 /* DMA */
112 struct dma rx_dma;
113 struct dma tx_dma;
114 /* PHY */
115 int phy_id;
116 struct phy_device *phy_dev;
117};
118
119static void bcm6348_eth_mac_disable(struct bcm6348_eth_priv *priv)
120{
121 /* disable emac */
122 clrsetbits_be32(priv->base + ETH_CTL_REG, ETH_CTL_ENABLE_MASK,
123 ETH_CTL_DISABLE_MASK);
124
125 /* wait until emac is disabled */
126 if (wait_for_bit_be32(priv->base + ETH_CTL_REG,
127 ETH_CTL_DISABLE_MASK, false,
128 ETH_TIMEOUT, false))
129 pr_err("%s: error disabling emac\n", __func__);
130}
131
132static void bcm6348_eth_mac_enable(struct bcm6348_eth_priv *priv)
133{
134 setbits_be32(priv->base + ETH_CTL_REG, ETH_CTL_ENABLE_MASK);
135}
136
137static void bcm6348_eth_mac_reset(struct bcm6348_eth_priv *priv)
138{
139 /* reset emac */
140 writel_be(ETH_CTL_RESET_MASK, priv->base + ETH_CTL_REG);
141 wmb();
142
143 /* wait until emac is reset */
144 if (wait_for_bit_be32(priv->base + ETH_CTL_REG,
145 ETH_CTL_RESET_MASK, false,
146 ETH_TIMEOUT, false))
147 pr_err("%s: error resetting emac\n", __func__);
148}
149
150static int bcm6348_eth_free_pkt(struct udevice *dev, uchar *packet, int len)
151{
152 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
153
154 return dma_prepare_rcv_buf(&priv->rx_dma, packet, len);
155}
156
157static int bcm6348_eth_recv(struct udevice *dev, int flags, uchar **packetp)
158{
159 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
160
161 return dma_receive(&priv->rx_dma, (void**)packetp, NULL);
162}
163
164static int bcm6348_eth_send(struct udevice *dev, void *packet, int length)
165{
166 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
167
168 return dma_send(&priv->tx_dma, packet, length, NULL);
169}
170
171static int bcm6348_eth_adjust_link(struct udevice *dev,
172 struct phy_device *phydev)
173{
174 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
175
176 /* mac duplex parameters */
177 if (phydev->duplex)
178 setbits_be32(priv->base + ETH_TXCTL_REG, ETH_TXCTL_FD_MASK);
179 else
180 clrbits_be32(priv->base + ETH_TXCTL_REG, ETH_TXCTL_FD_MASK);
181
182 /* rx flow control (pause frame handling) */
183 if (phydev->pause)
184 setbits_be32(priv->base + ETH_RXCFG_REG,
185 ETH_RXCFG_ENFLOW_MASK);
186 else
187 clrbits_be32(priv->base + ETH_RXCFG_REG,
188 ETH_RXCFG_ENFLOW_MASK);
189
190 return 0;
191}
192
193static int bcm6348_eth_start(struct udevice *dev)
194{
195 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
196 int ret, i;
197
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],
201 PKTSIZE_ALIGN);
202 if (ret < 0)
203 break;
204 }
205
206 /* enable dma rx channel */
207 dma_enable(&priv->rx_dma);
208
209 /* enable dma tx channel */
210 dma_enable(&priv->tx_dma);
211
212 ret = phy_startup(priv->phy_dev);
213 if (ret) {
214 pr_err("%s: could not initialize phy\n", __func__);
215 return ret;
216 }
217
218 if (!priv->phy_dev->link) {
219 pr_err("%s: no phy link\n", __func__);
220 return -EIO;
221 }
222
223 bcm6348_eth_adjust_link(dev, priv->phy_dev);
224
225 /* zero mib counters */
226 for (i = 0; i < MIB_REG_CNT; i++)
227 writel_be(0, MIB_REG(i));
228
229 /* enable rx flow control */
230 setbits_be32(priv->base + ETH_RXCFG_REG, ETH_RXCFG_ENFLOW_MASK);
231
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);
237
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);
241
242 /* enable emac */
243 bcm6348_eth_mac_enable(priv);
244
245 /* clear interrupts */
246 writel_be(0, priv->base + ETH_IRMASK_REG);
247
248 return 0;
249}
250
251static void bcm6348_eth_stop(struct udevice *dev)
252{
253 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
254
255 /* disable dma rx channel */
256 dma_disable(&priv->rx_dma);
257
258 /* disable dma tx channel */
259 dma_disable(&priv->tx_dma);
260
261 /* disable emac */
262 bcm6348_eth_mac_disable(priv);
263}
264
265static int bcm6348_eth_write_hwaddr(struct udevice *dev)
266{
Simon Glassc69cda22020-12-03 16:55:20 -0700267 struct eth_pdata *pdata = dev_get_plat(dev);
Álvaro Fernández Rojas55e55fe2018-12-01 19:00:24 +0100268 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
269 bool running = false;
270
271 /* check if emac is running */
272 if (readl_be(priv->base + ETH_CTL_REG) & ETH_CTL_ENABLE_MASK)
273 running = true;
274
275 /* disable emac */
276 if (running)
277 bcm6348_eth_mac_disable(priv);
278
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));
285
286 /* enable emac */
287 if (running)
288 bcm6348_eth_mac_enable(priv);
289
290 return 0;
291}
292
293static 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,
300};
301
302static const struct udevice_id bcm6348_eth_ids[] = {
303 { .compatible = "brcm,bcm6348-enet", },
304 { /* sentinel */ }
305};
306
307static int bcm6348_mdio_op(void __iomem *base, uint32_t data)
308{
309 /* make sure mii interrupt status is cleared */
310 writel_be(ETH_IR_MII_MASK, base + ETH_IR_REG);
311
312 /* issue mii op */
313 writel_be(data, base + MII_DAT_REG);
314
315 /* wait until emac is disabled */
316 return wait_for_bit_be32(base + ETH_IR_REG,
317 ETH_IR_MII_MASK, true,
318 ETH_TIMEOUT, false);
319}
320
321static int bcm6348_mdio_read(struct mii_dev *bus, int addr, int devaddr,
322 int reg)
323{
324 void __iomem *base = bus->priv;
325 uint32_t val;
326
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;
331
332 if (bcm6348_mdio_op(base, val)) {
333 pr_err("%s: timeout\n", __func__);
334 return -EINVAL;
335 }
336
337 val = readl_be(base + MII_DAT_REG) & MII_DAT_DATA_MASK;
338 val >>= MII_DAT_DATA_SHIFT;
339
340 return val;
341}
342
343static int bcm6348_mdio_write(struct mii_dev *bus, int addr, int dev_addr,
344 int reg, u16 value)
345{
346 void __iomem *base = bus->priv;
347 uint32_t val;
348
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;
354
355 if (bcm6348_mdio_op(base, val)) {
356 pr_err("%s: timeout\n", __func__);
357 return -EINVAL;
358 }
359
360 return 0;
361}
362
363static int bcm6348_mdio_init(const char *name, void __iomem *base)
364{
365 struct mii_dev *bus;
366
367 bus = mdio_alloc();
368 if (!bus) {
369 pr_err("%s: failed to allocate MDIO bus\n", __func__);
370 return -ENOMEM;
371 }
372
373 bus->read = bcm6348_mdio_read;
374 bus->write = bcm6348_mdio_write;
375 bus->priv = base;
376 snprintf(bus->name, sizeof(bus->name), "%s", name);
377
378 return mdio_register(bus);
379}
380
381static int bcm6348_phy_init(struct udevice *dev)
382{
Simon Glassc69cda22020-12-03 16:55:20 -0700383 struct eth_pdata *pdata = dev_get_plat(dev);
Álvaro Fernández Rojas55e55fe2018-12-01 19:00:24 +0100384 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
385 struct mii_dev *bus;
386
387 /* get mii bus */
388 bus = miiphy_get_dev_by_name(dev->name);
389
390 /* phy connect */
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__);
395 return -ENODEV;
396 }
397
398 priv->phy_dev->supported = (SUPPORTED_10baseT_Half |
399 SUPPORTED_10baseT_Full |
400 SUPPORTED_100baseT_Half |
401 SUPPORTED_100baseT_Full |
402 SUPPORTED_Autoneg |
403 SUPPORTED_Pause |
404 SUPPORTED_MII);
405 priv->phy_dev->advertising = priv->phy_dev->supported;
406
407 /* phy config */
408 phy_config(priv->phy_dev);
409
410 return 0;
411}
412
413static int bcm6348_eth_probe(struct udevice *dev)
414{
Simon Glassc69cda22020-12-03 16:55:20 -0700415 struct eth_pdata *pdata = dev_get_plat(dev);
Álvaro Fernández Rojas55e55fe2018-12-01 19:00:24 +0100416 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
417 struct ofnode_phandle_args phy;
Álvaro Fernández Rojas55e55fe2018-12-01 19:00:24 +0100418 int ret, i;
419
420 /* get base address */
421 priv->base = dev_remap_addr(dev);
422 if (!priv->base)
423 return -EINVAL;
424 pdata->iobase = (phys_addr_t) priv->base;
425
426 /* get phy mode */
Marek Behún123ca112022-04-07 00:33:01 +0200427 pdata->phy_interface = dev_read_phy_mode(dev);
Marek Behúnffb0f6f2022-04-07 00:33:03 +0200428 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
Álvaro Fernández Rojas55e55fe2018-12-01 19:00:24 +0100429 return -ENODEV;
430
431 /* get phy */
432 if (dev_read_phandle_with_args(dev, "phy", NULL, 0, 0, &phy))
433 return -ENOENT;
434 priv->phy_id = ofnode_read_u32_default(phy.node, "reg", -1);
435
436 /* get dma channels */
437 ret = dma_get_by_name(dev, "tx", &priv->tx_dma);
438 if (ret)
439 return -EINVAL;
440
441 ret = dma_get_by_name(dev, "rx", &priv->rx_dma);
442 if (ret)
443 return -EINVAL;
444
445 /* try to enable clocks */
446 for (i = 0; ; i++) {
447 struct clk clk;
448 int ret;
449
450 ret = clk_get_by_index(dev, i, &clk);
451 if (ret < 0)
452 break;
453
454 ret = clk_enable(&clk);
455 if (ret < 0) {
456 pr_err("%s: error enabling clock %d\n", __func__, i);
457 return ret;
458 }
Álvaro Fernández Rojas55e55fe2018-12-01 19:00:24 +0100459 }
460
461 /* try to perform resets */
462 for (i = 0; ; i++) {
463 struct reset_ctl reset;
464 int ret;
465
466 ret = reset_get_by_index(dev, i, &reset);
467 if (ret < 0)
468 break;
469
470 ret = reset_deassert(&reset);
471 if (ret < 0) {
472 pr_err("%s: error deasserting reset %d\n", __func__, i);
473 return ret;
474 }
475
476 ret = reset_free(&reset);
477 if (ret < 0) {
478 pr_err("%s: error freeing reset %d\n", __func__, i);
479 return ret;
480 }
481 }
482
483 /* disable emac */
484 bcm6348_eth_mac_disable(priv);
485
486 /* reset emac */
487 bcm6348_eth_mac_reset(priv);
488
489 /* select correct mii interface */
490 if (pdata->phy_interface == PHY_INTERFACE_MODE_INTERNAL)
491 clrbits_be32(priv->base + ETH_CTL_REG, ETH_CTL_EPHY_MASK);
492 else
493 setbits_be32(priv->base + ETH_CTL_REG, ETH_CTL_EPHY_MASK);
494
495 /* turn on mdc clock */
496 writel_be((0x1f << MII_SC_MDCFREQDIV_SHIFT) |
497 MII_SC_PREAMBLE_EN_MASK, priv->base + MII_SC_REG);
498
499 /* set mib counters to not clear when read */
500 clrbits_be32(priv->base + MIB_CTL_REG, MIB_CTL_RDCLEAR_MASK);
501
502 /* initialize perfect match registers */
503 for (i = 0; i < ETH_PM_CNT; i++) {
504 writel_be(0, priv->base + ETH_PML_REG(i));
505 writel_be(0, priv->base + ETH_PMH_REG(i));
506 }
507
508 /* init mii bus */
509 ret = bcm6348_mdio_init(dev->name, priv->base);
510 if (ret)
511 return ret;
512
513 /* init phy */
514 ret = bcm6348_phy_init(dev);
515 if (ret)
516 return ret;
517
518 return 0;
519}
520
521U_BOOT_DRIVER(bcm6348_eth) = {
522 .name = "bcm6348_eth",
523 .id = UCLASS_ETH,
524 .of_match = bcm6348_eth_ids,
525 .ops = &bcm6348_eth_ops,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700526 .plat_auto = sizeof(struct eth_pdata),
Simon Glass41575d82020-12-03 16:55:17 -0700527 .priv_auto = sizeof(struct bcm6348_eth_priv),
Álvaro Fernández Rojas55e55fe2018-12-01 19:00:24 +0100528 .probe = bcm6348_eth_probe,
529};