blob: fe3532930aa4112e22e708a668b89e915214761b [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
9#include <common.h>
10#include <clk.h>
11#include <dm.h>
12#include <dma.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>
20
21#define ETH_RX_DESC PKTBUFSRX
22#define ETH_MAX_MTU_SIZE 1518
23#define ETH_TIMEOUT 100
24#define ETH_TX_WATERMARK 32
25
26/* ETH Receiver Configuration register */
27#define ETH_RXCFG_REG 0x00
28#define ETH_RXCFG_ENFLOW_SHIFT 5
29#define ETH_RXCFG_ENFLOW_MASK (1 << ETH_RXCFG_ENFLOW_SHIFT)
30
31/* ETH Receive Maximum Length register */
32#define ETH_RXMAXLEN_REG 0x04
33#define ETH_RXMAXLEN_SHIFT 0
34#define ETH_RXMAXLEN_MASK (0x7ff << ETH_RXMAXLEN_SHIFT)
35
36/* ETH Transmit Maximum Length register */
37#define ETH_TXMAXLEN_REG 0x08
38#define ETH_TXMAXLEN_SHIFT 0
39#define ETH_TXMAXLEN_MASK (0x7ff << ETH_TXMAXLEN_SHIFT)
40
41/* MII Status/Control register */
42#define MII_SC_REG 0x10
43#define MII_SC_MDCFREQDIV_SHIFT 0
44#define MII_SC_MDCFREQDIV_MASK (0x7f << MII_SC_MDCFREQDIV_SHIFT)
45#define MII_SC_PREAMBLE_EN_SHIFT 7
46#define MII_SC_PREAMBLE_EN_MASK (1 << MII_SC_PREAMBLE_EN_SHIFT)
47
48/* MII Data register */
49#define MII_DAT_REG 0x14
50#define MII_DAT_DATA_SHIFT 0
51#define MII_DAT_DATA_MASK (0xffff << MII_DAT_DATA_SHIFT)
52#define MII_DAT_TA_SHIFT 16
53#define MII_DAT_TA_MASK (0x3 << MII_DAT_TA_SHIFT)
54#define MII_DAT_REG_SHIFT 18
55#define MII_DAT_REG_MASK (0x1f << MII_DAT_REG_SHIFT)
56#define MII_DAT_PHY_SHIFT 23
57#define MII_DAT_PHY_MASK (0x1f << MII_DAT_PHY_SHIFT)
58#define MII_DAT_OP_SHIFT 28
59#define MII_DAT_OP_WRITE (0x5 << MII_DAT_OP_SHIFT)
60#define MII_DAT_OP_READ (0x6 << MII_DAT_OP_SHIFT)
61
62/* ETH Interrupts Mask register */
63#define ETH_IRMASK_REG 0x18
64
65/* ETH Interrupts register */
66#define ETH_IR_REG 0x1c
67#define ETH_IR_MII_SHIFT 0
68#define ETH_IR_MII_MASK (1 << ETH_IR_MII_SHIFT)
69
70/* ETH Control register */
71#define ETH_CTL_REG 0x2c
72#define ETH_CTL_ENABLE_SHIFT 0
73#define ETH_CTL_ENABLE_MASK (1 << ETH_CTL_ENABLE_SHIFT)
74#define ETH_CTL_DISABLE_SHIFT 1
75#define ETH_CTL_DISABLE_MASK (1 << ETH_CTL_DISABLE_SHIFT)
76#define ETH_CTL_RESET_SHIFT 2
77#define ETH_CTL_RESET_MASK (1 << ETH_CTL_RESET_SHIFT)
78#define ETH_CTL_EPHY_SHIFT 3
79#define ETH_CTL_EPHY_MASK (1 << ETH_CTL_EPHY_SHIFT)
80
81/* ETH Transmit Control register */
82#define ETH_TXCTL_REG 0x30
83#define ETH_TXCTL_FD_SHIFT 0
84#define ETH_TXCTL_FD_MASK (1 << ETH_TXCTL_FD_SHIFT)
85
86/* ETH Transmit Watermask register */
87#define ETH_TXWMARK_REG 0x34
88#define ETH_TXWMARK_WM_SHIFT 0
89#define ETH_TXWMARK_WM_MASK (0x3f << ETH_TXWMARK_WM_SHIFT)
90
91/* MIB Control register */
92#define MIB_CTL_REG 0x38
93#define MIB_CTL_RDCLEAR_SHIFT 0
94#define MIB_CTL_RDCLEAR_MASK (1 << MIB_CTL_RDCLEAR_SHIFT)
95
96/* ETH Perfect Match registers */
97#define ETH_PM_CNT 4
98#define ETH_PML_REG(x) (0x58 + (x) * 0x8)
99#define ETH_PMH_REG(x) (0x5c + (x) * 0x8)
100#define ETH_PMH_VALID_SHIFT 16
101#define ETH_PMH_VALID_MASK (1 << ETH_PMH_VALID_SHIFT)
102
103/* MIB Counters registers */
104#define MIB_REG_CNT 55
105#define MIB_REG(x) (0x200 + (x) * 4)
106
107/* ETH data */
108struct bcm6348_eth_priv {
109 void __iomem *base;
110 /* DMA */
111 struct dma rx_dma;
112 struct dma tx_dma;
113 /* PHY */
114 int phy_id;
115 struct phy_device *phy_dev;
116};
117
118static void bcm6348_eth_mac_disable(struct bcm6348_eth_priv *priv)
119{
120 /* disable emac */
121 clrsetbits_be32(priv->base + ETH_CTL_REG, ETH_CTL_ENABLE_MASK,
122 ETH_CTL_DISABLE_MASK);
123
124 /* wait until emac is disabled */
125 if (wait_for_bit_be32(priv->base + ETH_CTL_REG,
126 ETH_CTL_DISABLE_MASK, false,
127 ETH_TIMEOUT, false))
128 pr_err("%s: error disabling emac\n", __func__);
129}
130
131static void bcm6348_eth_mac_enable(struct bcm6348_eth_priv *priv)
132{
133 setbits_be32(priv->base + ETH_CTL_REG, ETH_CTL_ENABLE_MASK);
134}
135
136static void bcm6348_eth_mac_reset(struct bcm6348_eth_priv *priv)
137{
138 /* reset emac */
139 writel_be(ETH_CTL_RESET_MASK, priv->base + ETH_CTL_REG);
140 wmb();
141
142 /* wait until emac is reset */
143 if (wait_for_bit_be32(priv->base + ETH_CTL_REG,
144 ETH_CTL_RESET_MASK, false,
145 ETH_TIMEOUT, false))
146 pr_err("%s: error resetting emac\n", __func__);
147}
148
149static int bcm6348_eth_free_pkt(struct udevice *dev, uchar *packet, int len)
150{
151 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
152
153 return dma_prepare_rcv_buf(&priv->rx_dma, packet, len);
154}
155
156static int bcm6348_eth_recv(struct udevice *dev, int flags, uchar **packetp)
157{
158 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
159
160 return dma_receive(&priv->rx_dma, (void**)packetp, NULL);
161}
162
163static int bcm6348_eth_send(struct udevice *dev, void *packet, int length)
164{
165 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
166
167 return dma_send(&priv->tx_dma, packet, length, NULL);
168}
169
170static int bcm6348_eth_adjust_link(struct udevice *dev,
171 struct phy_device *phydev)
172{
173 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
174
175 /* mac duplex parameters */
176 if (phydev->duplex)
177 setbits_be32(priv->base + ETH_TXCTL_REG, ETH_TXCTL_FD_MASK);
178 else
179 clrbits_be32(priv->base + ETH_TXCTL_REG, ETH_TXCTL_FD_MASK);
180
181 /* rx flow control (pause frame handling) */
182 if (phydev->pause)
183 setbits_be32(priv->base + ETH_RXCFG_REG,
184 ETH_RXCFG_ENFLOW_MASK);
185 else
186 clrbits_be32(priv->base + ETH_RXCFG_REG,
187 ETH_RXCFG_ENFLOW_MASK);
188
189 return 0;
190}
191
192static int bcm6348_eth_start(struct udevice *dev)
193{
194 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
195 int ret, i;
196
197 /* prepare rx dma buffers */
198 for (i = 0; i < ETH_RX_DESC; i++) {
199 ret = dma_prepare_rcv_buf(&priv->rx_dma, net_rx_packets[i],
200 PKTSIZE_ALIGN);
201 if (ret < 0)
202 break;
203 }
204
205 /* enable dma rx channel */
206 dma_enable(&priv->rx_dma);
207
208 /* enable dma tx channel */
209 dma_enable(&priv->tx_dma);
210
211 ret = phy_startup(priv->phy_dev);
212 if (ret) {
213 pr_err("%s: could not initialize phy\n", __func__);
214 return ret;
215 }
216
217 if (!priv->phy_dev->link) {
218 pr_err("%s: no phy link\n", __func__);
219 return -EIO;
220 }
221
222 bcm6348_eth_adjust_link(dev, priv->phy_dev);
223
224 /* zero mib counters */
225 for (i = 0; i < MIB_REG_CNT; i++)
226 writel_be(0, MIB_REG(i));
227
228 /* enable rx flow control */
229 setbits_be32(priv->base + ETH_RXCFG_REG, ETH_RXCFG_ENFLOW_MASK);
230
231 /* set max rx/tx length */
232 writel_be((ETH_MAX_MTU_SIZE << ETH_RXMAXLEN_SHIFT) &
233 ETH_RXMAXLEN_MASK, priv->base + ETH_RXMAXLEN_REG);
234 writel_be((ETH_MAX_MTU_SIZE << ETH_TXMAXLEN_SHIFT) &
235 ETH_TXMAXLEN_MASK, priv->base + ETH_TXMAXLEN_REG);
236
237 /* set correct transmit fifo watermark */
238 writel_be((ETH_TX_WATERMARK << ETH_TXWMARK_WM_SHIFT) &
239 ETH_TXWMARK_WM_MASK, priv->base + ETH_TXWMARK_REG);
240
241 /* enable emac */
242 bcm6348_eth_mac_enable(priv);
243
244 /* clear interrupts */
245 writel_be(0, priv->base + ETH_IRMASK_REG);
246
247 return 0;
248}
249
250static void bcm6348_eth_stop(struct udevice *dev)
251{
252 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
253
254 /* disable dma rx channel */
255 dma_disable(&priv->rx_dma);
256
257 /* disable dma tx channel */
258 dma_disable(&priv->tx_dma);
259
260 /* disable emac */
261 bcm6348_eth_mac_disable(priv);
262}
263
264static int bcm6348_eth_write_hwaddr(struct udevice *dev)
265{
266 struct eth_pdata *pdata = dev_get_platdata(dev);
267 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
268 bool running = false;
269
270 /* check if emac is running */
271 if (readl_be(priv->base + ETH_CTL_REG) & ETH_CTL_ENABLE_MASK)
272 running = true;
273
274 /* disable emac */
275 if (running)
276 bcm6348_eth_mac_disable(priv);
277
278 /* set mac address */
279 writel_be((pdata->enetaddr[2] << 24) | (pdata->enetaddr[3]) << 16 |
280 (pdata->enetaddr[4]) << 8 | (pdata->enetaddr[5]),
281 priv->base + ETH_PML_REG(0));
282 writel_be((pdata->enetaddr[1]) | (pdata->enetaddr[0] << 8) |
283 ETH_PMH_VALID_MASK, priv->base + ETH_PMH_REG(0));
284
285 /* enable emac */
286 if (running)
287 bcm6348_eth_mac_enable(priv);
288
289 return 0;
290}
291
292static const struct eth_ops bcm6348_eth_ops = {
293 .free_pkt = bcm6348_eth_free_pkt,
294 .recv = bcm6348_eth_recv,
295 .send = bcm6348_eth_send,
296 .start = bcm6348_eth_start,
297 .stop = bcm6348_eth_stop,
298 .write_hwaddr = bcm6348_eth_write_hwaddr,
299};
300
301static const struct udevice_id bcm6348_eth_ids[] = {
302 { .compatible = "brcm,bcm6348-enet", },
303 { /* sentinel */ }
304};
305
306static int bcm6348_mdio_op(void __iomem *base, uint32_t data)
307{
308 /* make sure mii interrupt status is cleared */
309 writel_be(ETH_IR_MII_MASK, base + ETH_IR_REG);
310
311 /* issue mii op */
312 writel_be(data, base + MII_DAT_REG);
313
314 /* wait until emac is disabled */
315 return wait_for_bit_be32(base + ETH_IR_REG,
316 ETH_IR_MII_MASK, true,
317 ETH_TIMEOUT, false);
318}
319
320static int bcm6348_mdio_read(struct mii_dev *bus, int addr, int devaddr,
321 int reg)
322{
323 void __iomem *base = bus->priv;
324 uint32_t val;
325
326 val = MII_DAT_OP_READ;
327 val |= (reg << MII_DAT_REG_SHIFT) & MII_DAT_REG_MASK;
328 val |= (0x2 << MII_DAT_TA_SHIFT) & MII_DAT_TA_MASK;
329 val |= (addr << MII_DAT_PHY_SHIFT) & MII_DAT_PHY_MASK;
330
331 if (bcm6348_mdio_op(base, val)) {
332 pr_err("%s: timeout\n", __func__);
333 return -EINVAL;
334 }
335
336 val = readl_be(base + MII_DAT_REG) & MII_DAT_DATA_MASK;
337 val >>= MII_DAT_DATA_SHIFT;
338
339 return val;
340}
341
342static int bcm6348_mdio_write(struct mii_dev *bus, int addr, int dev_addr,
343 int reg, u16 value)
344{
345 void __iomem *base = bus->priv;
346 uint32_t val;
347
348 val = MII_DAT_OP_WRITE;
349 val |= (reg << MII_DAT_REG_SHIFT) & MII_DAT_REG_MASK;
350 val |= (0x2 << MII_DAT_TA_SHIFT) & MII_DAT_TA_MASK;
351 val |= (addr << MII_DAT_PHY_SHIFT) & MII_DAT_PHY_MASK;
352 val |= (value << MII_DAT_DATA_SHIFT) & MII_DAT_DATA_MASK;
353
354 if (bcm6348_mdio_op(base, val)) {
355 pr_err("%s: timeout\n", __func__);
356 return -EINVAL;
357 }
358
359 return 0;
360}
361
362static int bcm6348_mdio_init(const char *name, void __iomem *base)
363{
364 struct mii_dev *bus;
365
366 bus = mdio_alloc();
367 if (!bus) {
368 pr_err("%s: failed to allocate MDIO bus\n", __func__);
369 return -ENOMEM;
370 }
371
372 bus->read = bcm6348_mdio_read;
373 bus->write = bcm6348_mdio_write;
374 bus->priv = base;
375 snprintf(bus->name, sizeof(bus->name), "%s", name);
376
377 return mdio_register(bus);
378}
379
380static int bcm6348_phy_init(struct udevice *dev)
381{
382 struct eth_pdata *pdata = dev_get_platdata(dev);
383 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
384 struct mii_dev *bus;
385
386 /* get mii bus */
387 bus = miiphy_get_dev_by_name(dev->name);
388
389 /* phy connect */
390 priv->phy_dev = phy_connect(bus, priv->phy_id, dev,
391 pdata->phy_interface);
392 if (!priv->phy_dev) {
393 pr_err("%s: no phy device\n", __func__);
394 return -ENODEV;
395 }
396
397 priv->phy_dev->supported = (SUPPORTED_10baseT_Half |
398 SUPPORTED_10baseT_Full |
399 SUPPORTED_100baseT_Half |
400 SUPPORTED_100baseT_Full |
401 SUPPORTED_Autoneg |
402 SUPPORTED_Pause |
403 SUPPORTED_MII);
404 priv->phy_dev->advertising = priv->phy_dev->supported;
405
406 /* phy config */
407 phy_config(priv->phy_dev);
408
409 return 0;
410}
411
412static int bcm6348_eth_probe(struct udevice *dev)
413{
414 struct eth_pdata *pdata = dev_get_platdata(dev);
415 struct bcm6348_eth_priv *priv = dev_get_priv(dev);
416 struct ofnode_phandle_args phy;
417 const char *phy_mode;
418 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 */
427 pdata->phy_interface = PHY_INTERFACE_MODE_NONE;
428 phy_mode = dev_read_string(dev, "phy-mode");
429 if (phy_mode)
430 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
431 if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
432 return -ENODEV;
433
434 /* get phy */
435 if (dev_read_phandle_with_args(dev, "phy", NULL, 0, 0, &phy))
436 return -ENOENT;
437 priv->phy_id = ofnode_read_u32_default(phy.node, "reg", -1);
438
439 /* get dma channels */
440 ret = dma_get_by_name(dev, "tx", &priv->tx_dma);
441 if (ret)
442 return -EINVAL;
443
444 ret = dma_get_by_name(dev, "rx", &priv->rx_dma);
445 if (ret)
446 return -EINVAL;
447
448 /* try to enable clocks */
449 for (i = 0; ; i++) {
450 struct clk clk;
451 int ret;
452
453 ret = clk_get_by_index(dev, i, &clk);
454 if (ret < 0)
455 break;
456
457 ret = clk_enable(&clk);
458 if (ret < 0) {
459 pr_err("%s: error enabling clock %d\n", __func__, i);
460 return ret;
461 }
462
463 ret = clk_free(&clk);
464 if (ret < 0) {
465 pr_err("%s: error freeing clock %d\n", __func__, i);
466 return ret;
467 }
468 }
469
470 /* try to perform resets */
471 for (i = 0; ; i++) {
472 struct reset_ctl reset;
473 int ret;
474
475 ret = reset_get_by_index(dev, i, &reset);
476 if (ret < 0)
477 break;
478
479 ret = reset_deassert(&reset);
480 if (ret < 0) {
481 pr_err("%s: error deasserting reset %d\n", __func__, i);
482 return ret;
483 }
484
485 ret = reset_free(&reset);
486 if (ret < 0) {
487 pr_err("%s: error freeing reset %d\n", __func__, i);
488 return ret;
489 }
490 }
491
492 /* disable emac */
493 bcm6348_eth_mac_disable(priv);
494
495 /* reset emac */
496 bcm6348_eth_mac_reset(priv);
497
498 /* select correct mii interface */
499 if (pdata->phy_interface == PHY_INTERFACE_MODE_INTERNAL)
500 clrbits_be32(priv->base + ETH_CTL_REG, ETH_CTL_EPHY_MASK);
501 else
502 setbits_be32(priv->base + ETH_CTL_REG, ETH_CTL_EPHY_MASK);
503
504 /* turn on mdc clock */
505 writel_be((0x1f << MII_SC_MDCFREQDIV_SHIFT) |
506 MII_SC_PREAMBLE_EN_MASK, priv->base + MII_SC_REG);
507
508 /* set mib counters to not clear when read */
509 clrbits_be32(priv->base + MIB_CTL_REG, MIB_CTL_RDCLEAR_MASK);
510
511 /* initialize perfect match registers */
512 for (i = 0; i < ETH_PM_CNT; i++) {
513 writel_be(0, priv->base + ETH_PML_REG(i));
514 writel_be(0, priv->base + ETH_PMH_REG(i));
515 }
516
517 /* init mii bus */
518 ret = bcm6348_mdio_init(dev->name, priv->base);
519 if (ret)
520 return ret;
521
522 /* init phy */
523 ret = bcm6348_phy_init(dev);
524 if (ret)
525 return ret;
526
527 return 0;
528}
529
530U_BOOT_DRIVER(bcm6348_eth) = {
531 .name = "bcm6348_eth",
532 .id = UCLASS_ETH,
533 .of_match = bcm6348_eth_ids,
534 .ops = &bcm6348_eth_ops,
535 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
536 .priv_auto_alloc_size = sizeof(struct bcm6348_eth_priv),
537 .probe = bcm6348_eth_probe,
538};