1 // SPDX-License-Identifier: GPL-2.0+
3 * Atheros AR71xx / AR9xxx GMAC driver
5 * Copyright (C) 2016 Marek Vasut <marex@denx.de>
13 #include <linux/compiler.h>
14 #include <linux/err.h>
15 #include <linux/mii.h>
19 #include <mach/ath79.h>
21 DECLARE_GLOBAL_DATA_PTR;
28 /* MAC Configuration 1 */
29 #define AG7XXX_ETH_CFG1 0x00
30 #define AG7XXX_ETH_CFG1_SOFT_RST BIT(31)
31 #define AG7XXX_ETH_CFG1_RX_RST BIT(19)
32 #define AG7XXX_ETH_CFG1_TX_RST BIT(18)
33 #define AG7XXX_ETH_CFG1_LOOPBACK BIT(8)
34 #define AG7XXX_ETH_CFG1_RX_EN BIT(2)
35 #define AG7XXX_ETH_CFG1_TX_EN BIT(0)
37 /* MAC Configuration 2 */
38 #define AG7XXX_ETH_CFG2 0x04
39 #define AG7XXX_ETH_CFG2_IF_1000 BIT(9)
40 #define AG7XXX_ETH_CFG2_IF_10_100 BIT(8)
41 #define AG7XXX_ETH_CFG2_IF_SPEED_MASK (3 << 8)
42 #define AG7XXX_ETH_CFG2_HUGE_FRAME_EN BIT(5)
43 #define AG7XXX_ETH_CFG2_LEN_CHECK BIT(4)
44 #define AG7XXX_ETH_CFG2_PAD_CRC_EN BIT(2)
45 #define AG7XXX_ETH_CFG2_FDX BIT(0)
47 /* MII Configuration */
48 #define AG7XXX_ETH_MII_MGMT_CFG 0x20
49 #define AG7XXX_ETH_MII_MGMT_CFG_RESET BIT(31)
52 #define AG7XXX_ETH_MII_MGMT_CMD 0x24
53 #define AG7XXX_ETH_MII_MGMT_CMD_READ 0x1
56 #define AG7XXX_ETH_MII_MGMT_ADDRESS 0x28
57 #define AG7XXX_ETH_MII_MGMT_ADDRESS_SHIFT 8
60 #define AG7XXX_ETH_MII_MGMT_CTRL 0x2c
63 #define AG7XXX_ETH_MII_MGMT_STATUS 0x30
66 #define AG7XXX_ETH_MII_MGMT_IND 0x34
67 #define AG7XXX_ETH_MII_MGMT_IND_INVALID BIT(2)
68 #define AG7XXX_ETH_MII_MGMT_IND_BUSY BIT(0)
70 /* STA Address 1 & 2 */
71 #define AG7XXX_ETH_ADDR1 0x40
72 #define AG7XXX_ETH_ADDR2 0x44
74 /* ETH Configuration 0 - 5 */
75 #define AG7XXX_ETH_FIFO_CFG_0 0x48
76 #define AG7XXX_ETH_FIFO_CFG_1 0x4c
77 #define AG7XXX_ETH_FIFO_CFG_2 0x50
78 #define AG7XXX_ETH_FIFO_CFG_3 0x54
79 #define AG7XXX_ETH_FIFO_CFG_4 0x58
80 #define AG7XXX_ETH_FIFO_CFG_5 0x5c
82 /* DMA Transfer Control for Queue 0 */
83 #define AG7XXX_ETH_DMA_TX_CTRL 0x180
84 #define AG7XXX_ETH_DMA_TX_CTRL_TXE BIT(0)
86 /* Descriptor Address for Queue 0 Tx */
87 #define AG7XXX_ETH_DMA_TX_DESC 0x184
90 #define AG7XXX_ETH_DMA_TX_STATUS 0x188
93 #define AG7XXX_ETH_DMA_RX_CTRL 0x18c
94 #define AG7XXX_ETH_DMA_RX_CTRL_RXE BIT(0)
96 /* Pointer to Rx Descriptor */
97 #define AG7XXX_ETH_DMA_RX_DESC 0x190
100 #define AG7XXX_ETH_DMA_RX_STATUS 0x194
102 /* Custom register at 0x18070000 */
103 #define AG7XXX_GMAC_ETH_CFG 0x00
104 #define AG7XXX_ETH_CFG_SW_PHY_ADDR_SWAP BIT(8)
105 #define AG7XXX_ETH_CFG_SW_PHY_SWAP BIT(7)
106 #define AG7XXX_ETH_CFG_SW_ONLY_MODE BIT(6)
107 #define AG7XXX_ETH_CFG_GE0_ERR_EN BIT(5)
108 #define AG7XXX_ETH_CFG_MII_GE0_SLAVE BIT(4)
109 #define AG7XXX_ETH_CFG_MII_GE0_MASTER BIT(3)
110 #define AG7XXX_ETH_CFG_GMII_GE0 BIT(2)
111 #define AG7XXX_ETH_CFG_MII_GE0 BIT(1)
112 #define AG7XXX_ETH_CFG_RGMII_GE0 BIT(0)
114 #define CONFIG_TX_DESCR_NUM 8
115 #define CONFIG_RX_DESCR_NUM 8
116 #define CONFIG_ETH_BUFSIZE 2048
117 #define TX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_TX_DESCR_NUM)
118 #define RX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_RX_DESCR_NUM)
120 /* DMA descriptor. */
121 struct ag7xxx_dma_desc {
123 #define AG7XXX_DMADESC_IS_EMPTY BIT(31)
124 #define AG7XXX_DMADESC_FTPP_OVERRIDE_OFFSET 16
125 #define AG7XXX_DMADESC_PKT_SIZE_OFFSET 0
126 #define AG7XXX_DMADESC_PKT_SIZE_MASK 0xfff
132 struct ar7xxx_eth_priv {
133 struct ag7xxx_dma_desc tx_mac_descrtable[CONFIG_TX_DESCR_NUM];
134 struct ag7xxx_dma_desc rx_mac_descrtable[CONFIG_RX_DESCR_NUM];
135 char txbuffs[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
136 char rxbuffs[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
139 void __iomem *phyregs;
141 struct eth_device *dev;
142 struct phy_device *phydev;
148 enum ag7xxx_model model;
152 * Switch and MDIO access
154 static int ag7xxx_switch_read(struct mii_dev *bus, int addr, int reg, u16 *val)
156 struct ar7xxx_eth_priv *priv = bus->priv;
157 void __iomem *regs = priv->phyregs;
160 writel(0x0, regs + AG7XXX_ETH_MII_MGMT_CMD);
161 writel((addr << AG7XXX_ETH_MII_MGMT_ADDRESS_SHIFT) | reg,
162 regs + AG7XXX_ETH_MII_MGMT_ADDRESS);
163 writel(AG7XXX_ETH_MII_MGMT_CMD_READ,
164 regs + AG7XXX_ETH_MII_MGMT_CMD);
166 ret = wait_for_bit_le32(regs + AG7XXX_ETH_MII_MGMT_IND,
167 AG7XXX_ETH_MII_MGMT_IND_BUSY, 0, 1000, 0);
171 *val = readl(regs + AG7XXX_ETH_MII_MGMT_STATUS) & 0xffff;
172 writel(0x0, regs + AG7XXX_ETH_MII_MGMT_CMD);
177 static int ag7xxx_switch_write(struct mii_dev *bus, int addr, int reg, u16 val)
179 struct ar7xxx_eth_priv *priv = bus->priv;
180 void __iomem *regs = priv->phyregs;
183 writel((addr << AG7XXX_ETH_MII_MGMT_ADDRESS_SHIFT) | reg,
184 regs + AG7XXX_ETH_MII_MGMT_ADDRESS);
185 writel(val, regs + AG7XXX_ETH_MII_MGMT_CTRL);
187 ret = wait_for_bit_le32(regs + AG7XXX_ETH_MII_MGMT_IND,
188 AG7XXX_ETH_MII_MGMT_IND_BUSY, 0, 1000, 0);
193 static int ag7xxx_switch_reg_read(struct mii_dev *bus, int reg, u32 *val)
195 struct ar7xxx_eth_priv *priv = bus->priv;
203 if (priv->model == AG7XXX_MODEL_AG933X) {
206 } else if (priv->model == AG7XXX_MODEL_AG934X) {
212 ret = ag7xxx_switch_write(bus, phy_addr, reg_addr, reg >> 9);
216 phy_temp = ((reg >> 6) & 0x7) | 0x10;
217 reg_temp = (reg >> 1) & 0x1e;
220 ret = ag7xxx_switch_read(bus, phy_temp, reg_temp | 0, &rv);
225 ret = ag7xxx_switch_read(bus, phy_temp, reg_temp | 1, &rv);
233 static int ag7xxx_switch_reg_write(struct mii_dev *bus, int reg, u32 val)
235 struct ar7xxx_eth_priv *priv = bus->priv;
242 if (priv->model == AG7XXX_MODEL_AG933X) {
245 } else if (priv->model == AG7XXX_MODEL_AG934X) {
251 ret = ag7xxx_switch_write(bus, phy_addr, reg_addr, reg >> 9);
255 phy_temp = ((reg >> 6) & 0x7) | 0x10;
256 reg_temp = (reg >> 1) & 0x1e;
259 * The switch on AR933x has some special register behavior, which
260 * expects particular write order of their nibbles:
261 * 0x40 ..... MSB first, LSB second
262 * 0x50 ..... MSB first, LSB second
263 * 0x98 ..... LSB first, MSB second
264 * others ... don't care
266 if ((priv->model == AG7XXX_MODEL_AG933X) && (reg == 0x98)) {
267 ret = ag7xxx_switch_write(bus, phy_temp, reg_temp | 0, val & 0xffff);
271 ret = ag7xxx_switch_write(bus, phy_temp, reg_temp | 1, val >> 16);
275 ret = ag7xxx_switch_write(bus, phy_temp, reg_temp | 1, val >> 16);
279 ret = ag7xxx_switch_write(bus, phy_temp, reg_temp | 0, val & 0xffff);
287 static int ag7xxx_mdio_rw(struct mii_dev *bus, int addr, int reg, u32 val)
292 /* No idea if this is long enough or too long */
293 int timeout_ms = 1000;
295 /* Dummy read followed by PHY read/write command. */
296 ret = ag7xxx_switch_reg_read(bus, 0x98, &data);
299 data = val | (reg << 16) | (addr << 21) | BIT(30) | BIT(31);
300 ret = ag7xxx_switch_reg_write(bus, 0x98, data);
304 start = get_timer(0);
306 /* Wait for operation to finish */
308 ret = ag7xxx_switch_reg_read(bus, 0x98, &data);
312 if (get_timer(start) > timeout_ms)
314 } while (data & BIT(31));
316 return data & 0xffff;
319 static int ag7xxx_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
321 return ag7xxx_mdio_rw(bus, addr, reg, BIT(27));
324 static int ag7xxx_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
329 ret = ag7xxx_mdio_rw(bus, addr, reg, val);
338 static void ag7xxx_dma_clean_tx(struct udevice *dev)
340 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
341 struct ag7xxx_dma_desc *curr, *next;
345 for (i = 0; i < CONFIG_TX_DESCR_NUM; i++) {
346 curr = &priv->tx_mac_descrtable[i];
347 next = &priv->tx_mac_descrtable[(i + 1) % CONFIG_TX_DESCR_NUM];
349 curr->data_addr = virt_to_phys(&priv->txbuffs[i * CONFIG_ETH_BUFSIZE]);
350 curr->config = AG7XXX_DMADESC_IS_EMPTY;
351 curr->next_desc = virt_to_phys(next);
354 priv->tx_currdescnum = 0;
356 /* Cache: Flush descriptors, don't care about buffers. */
357 start = (u32)(&priv->tx_mac_descrtable[0]);
358 end = start + sizeof(priv->tx_mac_descrtable);
359 flush_dcache_range(start, end);
362 static void ag7xxx_dma_clean_rx(struct udevice *dev)
364 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
365 struct ag7xxx_dma_desc *curr, *next;
369 for (i = 0; i < CONFIG_RX_DESCR_NUM; i++) {
370 curr = &priv->rx_mac_descrtable[i];
371 next = &priv->rx_mac_descrtable[(i + 1) % CONFIG_RX_DESCR_NUM];
373 curr->data_addr = virt_to_phys(&priv->rxbuffs[i * CONFIG_ETH_BUFSIZE]);
374 curr->config = AG7XXX_DMADESC_IS_EMPTY;
375 curr->next_desc = virt_to_phys(next);
378 priv->rx_currdescnum = 0;
380 /* Cache: Flush+Invalidate descriptors, Invalidate buffers. */
381 start = (u32)(&priv->rx_mac_descrtable[0]);
382 end = start + sizeof(priv->rx_mac_descrtable);
383 flush_dcache_range(start, end);
384 invalidate_dcache_range(start, end);
386 start = (u32)&priv->rxbuffs;
387 end = start + sizeof(priv->rxbuffs);
388 invalidate_dcache_range(start, end);
394 static int ag7xxx_eth_send(struct udevice *dev, void *packet, int length)
396 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
397 struct ag7xxx_dma_desc *curr;
400 curr = &priv->tx_mac_descrtable[priv->tx_currdescnum];
402 /* Cache: Invalidate descriptor. */
404 end = start + sizeof(*curr);
405 invalidate_dcache_range(start, end);
407 if (!(curr->config & AG7XXX_DMADESC_IS_EMPTY)) {
408 printf("ag7xxx: Out of TX DMA descriptors!\n");
412 /* Copy the packet into the data buffer. */
413 memcpy(phys_to_virt(curr->data_addr), packet, length);
414 curr->config = length & AG7XXX_DMADESC_PKT_SIZE_MASK;
416 /* Cache: Flush descriptor, Flush buffer. */
418 end = start + sizeof(*curr);
419 flush_dcache_range(start, end);
420 start = (u32)phys_to_virt(curr->data_addr);
421 end = start + length;
422 flush_dcache_range(start, end);
424 /* Load the DMA descriptor and start TX DMA. */
425 writel(AG7XXX_ETH_DMA_TX_CTRL_TXE,
426 priv->regs + AG7XXX_ETH_DMA_TX_CTRL);
428 /* Switch to next TX descriptor. */
429 priv->tx_currdescnum = (priv->tx_currdescnum + 1) % CONFIG_TX_DESCR_NUM;
434 static int ag7xxx_eth_recv(struct udevice *dev, int flags, uchar **packetp)
436 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
437 struct ag7xxx_dma_desc *curr;
438 u32 start, end, length;
440 curr = &priv->rx_mac_descrtable[priv->rx_currdescnum];
442 /* Cache: Invalidate descriptor. */
444 end = start + sizeof(*curr);
445 invalidate_dcache_range(start, end);
447 /* No packets received. */
448 if (curr->config & AG7XXX_DMADESC_IS_EMPTY)
451 length = curr->config & AG7XXX_DMADESC_PKT_SIZE_MASK;
453 /* Cache: Invalidate buffer. */
454 start = (u32)phys_to_virt(curr->data_addr);
455 end = start + length;
456 invalidate_dcache_range(start, end);
458 /* Receive one packet and return length. */
459 *packetp = phys_to_virt(curr->data_addr);
463 static int ag7xxx_eth_free_pkt(struct udevice *dev, uchar *packet,
466 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
467 struct ag7xxx_dma_desc *curr;
470 curr = &priv->rx_mac_descrtable[priv->rx_currdescnum];
472 curr->config = AG7XXX_DMADESC_IS_EMPTY;
474 /* Cache: Flush descriptor. */
476 end = start + sizeof(*curr);
477 flush_dcache_range(start, end);
479 /* Switch to next RX descriptor. */
480 priv->rx_currdescnum = (priv->rx_currdescnum + 1) % CONFIG_RX_DESCR_NUM;
485 static int ag7xxx_eth_start(struct udevice *dev)
487 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
489 /* FIXME: Check if link up */
491 /* Clear the DMA rings. */
492 ag7xxx_dma_clean_tx(dev);
493 ag7xxx_dma_clean_rx(dev);
495 /* Load DMA descriptors and start the RX DMA. */
496 writel(virt_to_phys(&priv->tx_mac_descrtable[priv->tx_currdescnum]),
497 priv->regs + AG7XXX_ETH_DMA_TX_DESC);
498 writel(virt_to_phys(&priv->rx_mac_descrtable[priv->rx_currdescnum]),
499 priv->regs + AG7XXX_ETH_DMA_RX_DESC);
500 writel(AG7XXX_ETH_DMA_RX_CTRL_RXE,
501 priv->regs + AG7XXX_ETH_DMA_RX_CTRL);
506 static void ag7xxx_eth_stop(struct udevice *dev)
508 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
510 /* Stop the TX DMA. */
511 writel(0, priv->regs + AG7XXX_ETH_DMA_TX_CTRL);
512 wait_for_bit_le32(priv->regs + AG7XXX_ETH_DMA_TX_CTRL, ~0, 0,
515 /* Stop the RX DMA. */
516 writel(0, priv->regs + AG7XXX_ETH_DMA_RX_CTRL);
517 wait_for_bit_le32(priv->regs + AG7XXX_ETH_DMA_RX_CTRL, ~0, 0,
524 static int ag7xxx_eth_write_hwaddr(struct udevice *dev)
526 struct eth_pdata *pdata = dev_get_platdata(dev);
527 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
528 unsigned char *mac = pdata->enetaddr;
529 u32 macid_lo, macid_hi;
531 macid_hi = mac[3] | (mac[2] << 8) | (mac[1] << 16) | (mac[0] << 24);
532 macid_lo = (mac[5] << 16) | (mac[4] << 24);
534 writel(macid_lo, priv->regs + AG7XXX_ETH_ADDR1);
535 writel(macid_hi, priv->regs + AG7XXX_ETH_ADDR2);
540 static void ag7xxx_hw_setup(struct udevice *dev)
542 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
545 setbits_be32(priv->regs + AG7XXX_ETH_CFG1,
546 AG7XXX_ETH_CFG1_RX_RST | AG7XXX_ETH_CFG1_TX_RST |
547 AG7XXX_ETH_CFG1_SOFT_RST);
551 writel(AG7XXX_ETH_CFG1_RX_EN | AG7XXX_ETH_CFG1_TX_EN,
552 priv->regs + AG7XXX_ETH_CFG1);
554 if (priv->interface == PHY_INTERFACE_MODE_RMII)
555 speed = AG7XXX_ETH_CFG2_IF_10_100;
557 speed = AG7XXX_ETH_CFG2_IF_1000;
559 clrsetbits_be32(priv->regs + AG7XXX_ETH_CFG2,
560 AG7XXX_ETH_CFG2_IF_SPEED_MASK,
561 speed | AG7XXX_ETH_CFG2_PAD_CRC_EN |
562 AG7XXX_ETH_CFG2_LEN_CHECK);
564 writel(0xfff0000, priv->regs + AG7XXX_ETH_FIFO_CFG_1);
565 writel(0x1fff, priv->regs + AG7XXX_ETH_FIFO_CFG_2);
567 writel(0x1f00, priv->regs + AG7XXX_ETH_FIFO_CFG_0);
568 setbits_be32(priv->regs + AG7XXX_ETH_FIFO_CFG_4, 0x3ffff);
569 writel(0x10ffff, priv->regs + AG7XXX_ETH_FIFO_CFG_1);
570 writel(0xaaa0555, priv->regs + AG7XXX_ETH_FIFO_CFG_2);
571 writel(0x7eccf, priv->regs + AG7XXX_ETH_FIFO_CFG_5);
572 writel(0x1f00140, priv->regs + AG7XXX_ETH_FIFO_CFG_3);
575 static int ag7xxx_mii_get_div(void)
577 ulong freq = get_bus_freq(0);
579 switch (freq / 1000000) {
580 case 150: return 0x7;
581 case 175: return 0x5;
582 case 200: return 0x4;
583 case 210: return 0x9;
584 case 220: return 0x9;
589 static int ag7xxx_mii_setup(struct udevice *dev)
591 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
592 int i, ret, div = ag7xxx_mii_get_div();
595 if (priv->model == AG7XXX_MODEL_AG933X) {
596 /* Unit 0 is PHY-less on AR9331, see datasheet Figure 2-3 */
597 if (priv->interface == PHY_INTERFACE_MODE_RMII)
601 if (priv->model == AG7XXX_MODEL_AG934X) {
602 writel(AG7XXX_ETH_MII_MGMT_CFG_RESET | 0x4,
603 priv->regs + AG7XXX_ETH_MII_MGMT_CFG);
604 writel(0x4, priv->regs + AG7XXX_ETH_MII_MGMT_CFG);
608 for (i = 0; i < 10; i++) {
609 writel(AG7XXX_ETH_MII_MGMT_CFG_RESET | div,
610 priv->regs + AG7XXX_ETH_MII_MGMT_CFG);
611 writel(div, priv->regs + AG7XXX_ETH_MII_MGMT_CFG);
613 /* Check the switch */
614 ret = ag7xxx_switch_reg_read(priv->bus, 0x10c, ®);
618 if (reg != 0x18007fff)
627 static int ag933x_phy_setup_wan(struct udevice *dev)
629 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
631 /* Configure switch port 4 (GMAC0) */
632 return ag7xxx_mdio_write(priv->bus, 4, 0, MII_BMCR, 0x9000);
635 static int ag933x_phy_setup_lan(struct udevice *dev)
637 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
641 /* Reset the switch */
642 ret = ag7xxx_switch_reg_read(priv->bus, 0, ®);
646 ret = ag7xxx_switch_reg_write(priv->bus, 0, reg);
651 ret = ag7xxx_switch_reg_read(priv->bus, 0, ®);
654 } while (reg & BIT(31));
656 /* Configure switch ports 0...3 (GMAC1) */
657 for (i = 0; i < 4; i++) {
658 ret = ag7xxx_mdio_write(priv->bus, 0x4, 0, MII_BMCR, 0x9000);
663 /* Enable CPU port */
664 ret = ag7xxx_switch_reg_write(priv->bus, 0x78, BIT(8));
668 for (i = 0; i < 4; i++) {
669 ret = ag7xxx_switch_reg_write(priv->bus, i * 0x100, BIT(9));
675 ret = ag7xxx_switch_reg_write(priv->bus, 0x38, 0xc000050e);
679 /* Disable Atheros header */
680 ret = ag7xxx_switch_reg_write(priv->bus, 0x104, 0x4004);
684 /* Tag priority mapping */
685 ret = ag7xxx_switch_reg_write(priv->bus, 0x70, 0xfa50);
689 /* Enable ARP packets to the CPU */
690 ret = ag7xxx_switch_reg_read(priv->bus, 0x5c, ®);
694 ret = ag7xxx_switch_reg_write(priv->bus, 0x5c, reg);
701 static int ag933x_phy_setup_reset_set(struct udevice *dev, int port)
703 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
706 ret = ag7xxx_mdio_write(priv->bus, port, 0, MII_ADVERTISE,
707 ADVERTISE_ALL | ADVERTISE_PAUSE_CAP |
708 ADVERTISE_PAUSE_ASYM);
712 if (priv->model == AG7XXX_MODEL_AG934X) {
713 ret = ag7xxx_mdio_write(priv->bus, port, 0, MII_CTRL1000,
719 return ag7xxx_mdio_write(priv->bus, port, 0, MII_BMCR,
720 BMCR_ANENABLE | BMCR_RESET);
723 static int ag933x_phy_setup_reset_fin(struct udevice *dev, int port)
725 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
729 ret = ag7xxx_mdio_read(priv->bus, port, 0, MII_BMCR);
733 } while (ret & BMCR_RESET);
738 static int ag933x_phy_setup_common(struct udevice *dev)
740 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
743 if (priv->model == AG7XXX_MODEL_AG933X)
745 else if (priv->model == AG7XXX_MODEL_AG934X)
750 if (priv->interface == PHY_INTERFACE_MODE_RMII) {
751 ret = ag933x_phy_setup_reset_set(dev, phymax);
755 ret = ag933x_phy_setup_reset_fin(dev, phymax);
759 /* Read out link status */
760 ret = ag7xxx_mdio_read(priv->bus, phymax, 0, MII_MIPSCR);
768 for (i = 0; i < phymax; i++) {
769 ret = ag933x_phy_setup_reset_set(dev, i);
774 for (i = 0; i < phymax; i++) {
775 ret = ag933x_phy_setup_reset_fin(dev, i);
780 for (i = 0; i < phymax; i++) {
781 /* Read out link status */
782 ret = ag7xxx_mdio_read(priv->bus, i, 0, MII_MIPSCR);
790 static int ag934x_phy_setup(struct udevice *dev)
792 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
796 ret = ag7xxx_switch_reg_write(priv->bus, 0x624, 0x7f7f7f7f);
799 ret = ag7xxx_switch_reg_write(priv->bus, 0x10, 0x40000000);
802 ret = ag7xxx_switch_reg_write(priv->bus, 0x4, 0x07600000);
805 ret = ag7xxx_switch_reg_write(priv->bus, 0xc, 0x01000000);
808 ret = ag7xxx_switch_reg_write(priv->bus, 0x7c, 0x0000007e);
812 /* AR8327/AR8328 v1.0 fixup */
813 ret = ag7xxx_switch_reg_read(priv->bus, 0, ®);
816 if ((reg & 0xffff) == 0x1201) {
817 for (i = 0; i < 5; i++) {
818 ret = ag7xxx_mdio_write(priv->bus, i, 0, 0x1d, 0x0);
821 ret = ag7xxx_mdio_write(priv->bus, i, 0, 0x1e, 0x02ea);
824 ret = ag7xxx_mdio_write(priv->bus, i, 0, 0x1d, 0x3d);
827 ret = ag7xxx_mdio_write(priv->bus, i, 0, 0x1e, 0x68a0);
833 ret = ag7xxx_switch_reg_read(priv->bus, 0x66c, ®);
837 ret = ag7xxx_switch_reg_write(priv->bus, 0x66c, reg);
844 static int ag7xxx_mac_probe(struct udevice *dev)
846 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
849 ag7xxx_hw_setup(dev);
850 ret = ag7xxx_mii_setup(dev);
854 ag7xxx_eth_write_hwaddr(dev);
856 if (priv->model == AG7XXX_MODEL_AG933X) {
857 if (priv->interface == PHY_INTERFACE_MODE_RMII)
858 ret = ag933x_phy_setup_wan(dev);
860 ret = ag933x_phy_setup_lan(dev);
861 } else if (priv->model == AG7XXX_MODEL_AG934X) {
862 ret = ag934x_phy_setup(dev);
870 return ag933x_phy_setup_common(dev);
873 static int ag7xxx_mdio_probe(struct udevice *dev)
875 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
876 struct mii_dev *bus = mdio_alloc();
881 bus->read = ag7xxx_mdio_read;
882 bus->write = ag7xxx_mdio_write;
883 snprintf(bus->name, sizeof(bus->name), dev->name);
885 bus->priv = (void *)priv;
887 return mdio_register(bus);
890 static int ag7xxx_get_phy_iface_offset(struct udevice *dev)
894 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev), "phy");
896 debug("%s: PHY OF node not found (ret=%i)\n", __func__, offset);
900 offset = fdt_parent_offset(gd->fdt_blob, offset);
902 debug("%s: PHY OF node parent MDIO bus not found (ret=%i)\n",
907 offset = fdt_parent_offset(gd->fdt_blob, offset);
909 debug("%s: PHY MDIO OF node parent MAC not found (ret=%i)\n",
917 static int ag7xxx_eth_probe(struct udevice *dev)
919 struct eth_pdata *pdata = dev_get_platdata(dev);
920 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
921 void __iomem *iobase, *phyiobase;
924 /* Decoding of convoluted PHY wiring on Atheros MIPS. */
925 ret = ag7xxx_get_phy_iface_offset(dev);
928 phyreg = fdtdec_get_int(gd->fdt_blob, ret, "reg", -1);
930 iobase = map_physmem(pdata->iobase, 0x200, MAP_NOCACHE);
931 phyiobase = map_physmem(phyreg, 0x200, MAP_NOCACHE);
933 debug("%s, iobase=%p, phyiobase=%p, priv=%p\n",
934 __func__, iobase, phyiobase, priv);
936 priv->phyregs = phyiobase;
937 priv->interface = pdata->phy_interface;
938 priv->model = dev_get_driver_data(dev);
940 ret = ag7xxx_mdio_probe(dev);
944 priv->bus = miiphy_get_dev_by_name(dev->name);
946 ret = ag7xxx_mac_probe(dev);
947 debug("%s, ret=%d\n", __func__, ret);
952 static int ag7xxx_eth_remove(struct udevice *dev)
954 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
957 mdio_unregister(priv->bus);
958 mdio_free(priv->bus);
963 static const struct eth_ops ag7xxx_eth_ops = {
964 .start = ag7xxx_eth_start,
965 .send = ag7xxx_eth_send,
966 .recv = ag7xxx_eth_recv,
967 .free_pkt = ag7xxx_eth_free_pkt,
968 .stop = ag7xxx_eth_stop,
969 .write_hwaddr = ag7xxx_eth_write_hwaddr,
972 static int ag7xxx_eth_ofdata_to_platdata(struct udevice *dev)
974 struct eth_pdata *pdata = dev_get_platdata(dev);
975 const char *phy_mode;
978 pdata->iobase = devfdt_get_addr(dev);
979 pdata->phy_interface = -1;
981 /* Decoding of convoluted PHY wiring on Atheros MIPS. */
982 ret = ag7xxx_get_phy_iface_offset(dev);
986 phy_mode = fdt_getprop(gd->fdt_blob, ret, "phy-mode", NULL);
988 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
989 if (pdata->phy_interface == -1) {
990 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
997 static const struct udevice_id ag7xxx_eth_ids[] = {
998 { .compatible = "qca,ag933x-mac", .data = AG7XXX_MODEL_AG933X },
999 { .compatible = "qca,ag934x-mac", .data = AG7XXX_MODEL_AG934X },
1003 U_BOOT_DRIVER(eth_ag7xxx) = {
1004 .name = "eth_ag7xxx",
1006 .of_match = ag7xxx_eth_ids,
1007 .ofdata_to_platdata = ag7xxx_eth_ofdata_to_platdata,
1008 .probe = ag7xxx_eth_probe,
1009 .remove = ag7xxx_eth_remove,
1010 .ops = &ag7xxx_eth_ops,
1011 .priv_auto_alloc_size = sizeof(struct ar7xxx_eth_priv),
1012 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1013 .flags = DM_FLAG_ALLOC_PRIV_DMA,