1 // SPDX-License-Identifier: GPL-2.0+
3 * Atheros AR71xx / AR9xxx GMAC driver
5 * Copyright (C) 2016 Marek Vasut <marex@denx.de>
6 * Copyright (C) 2019 Rosy Song <rosysong@rosinson.com>
14 #include <linux/compiler.h>
15 #include <linux/err.h>
16 #include <linux/mii.h>
20 #include <mach/ath79.h>
22 DECLARE_GLOBAL_DATA_PTR;
31 /* MAC Configuration 1 */
32 #define AG7XXX_ETH_CFG1 0x00
33 #define AG7XXX_ETH_CFG1_SOFT_RST BIT(31)
34 #define AG7XXX_ETH_CFG1_RX_RST BIT(19)
35 #define AG7XXX_ETH_CFG1_TX_RST BIT(18)
36 #define AG7XXX_ETH_CFG1_LOOPBACK BIT(8)
37 #define AG7XXX_ETH_CFG1_RX_EN BIT(2)
38 #define AG7XXX_ETH_CFG1_TX_EN BIT(0)
40 /* MAC Configuration 2 */
41 #define AG7XXX_ETH_CFG2 0x04
42 #define AG7XXX_ETH_CFG2_IF_1000 BIT(9)
43 #define AG7XXX_ETH_CFG2_IF_10_100 BIT(8)
44 #define AG7XXX_ETH_CFG2_IF_SPEED_MASK (3 << 8)
45 #define AG7XXX_ETH_CFG2_HUGE_FRAME_EN BIT(5)
46 #define AG7XXX_ETH_CFG2_LEN_CHECK BIT(4)
47 #define AG7XXX_ETH_CFG2_PAD_CRC_EN BIT(2)
48 #define AG7XXX_ETH_CFG2_FDX BIT(0)
50 /* MII Configuration */
51 #define AG7XXX_ETH_MII_MGMT_CFG 0x20
52 #define AG7XXX_ETH_MII_MGMT_CFG_RESET BIT(31)
55 #define AG7XXX_ETH_MII_MGMT_CMD 0x24
56 #define AG7XXX_ETH_MII_MGMT_CMD_READ 0x1
59 #define AG7XXX_ETH_MII_MGMT_ADDRESS 0x28
60 #define AG7XXX_ETH_MII_MGMT_ADDRESS_SHIFT 8
63 #define AG7XXX_ETH_MII_MGMT_CTRL 0x2c
66 #define AG7XXX_ETH_MII_MGMT_STATUS 0x30
69 #define AG7XXX_ETH_MII_MGMT_IND 0x34
70 #define AG7XXX_ETH_MII_MGMT_IND_INVALID BIT(2)
71 #define AG7XXX_ETH_MII_MGMT_IND_BUSY BIT(0)
73 /* STA Address 1 & 2 */
74 #define AG7XXX_ETH_ADDR1 0x40
75 #define AG7XXX_ETH_ADDR2 0x44
77 /* ETH Configuration 0 - 5 */
78 #define AG7XXX_ETH_FIFO_CFG_0 0x48
79 #define AG7XXX_ETH_FIFO_CFG_1 0x4c
80 #define AG7XXX_ETH_FIFO_CFG_2 0x50
81 #define AG7XXX_ETH_FIFO_CFG_3 0x54
82 #define AG7XXX_ETH_FIFO_CFG_4 0x58
83 #define AG7XXX_ETH_FIFO_CFG_5 0x5c
85 /* DMA Transfer Control for Queue 0 */
86 #define AG7XXX_ETH_DMA_TX_CTRL 0x180
87 #define AG7XXX_ETH_DMA_TX_CTRL_TXE BIT(0)
89 /* Descriptor Address for Queue 0 Tx */
90 #define AG7XXX_ETH_DMA_TX_DESC 0x184
93 #define AG7XXX_ETH_DMA_TX_STATUS 0x188
96 #define AG7XXX_ETH_DMA_RX_CTRL 0x18c
97 #define AG7XXX_ETH_DMA_RX_CTRL_RXE BIT(0)
99 /* Pointer to Rx Descriptor */
100 #define AG7XXX_ETH_DMA_RX_DESC 0x190
103 #define AG7XXX_ETH_DMA_RX_STATUS 0x194
105 /* Custom register at 0x1805002C */
106 #define AG7XXX_ETH_XMII 0x2C
107 #define AG7XXX_ETH_XMII_TX_INVERT BIT(31)
108 #define AG7XXX_ETH_XMII_RX_DELAY_LSB 28
109 #define AG7XXX_ETH_XMII_RX_DELAY_MASK 0x30000000
110 #define AG7XXX_ETH_XMII_RX_DELAY_SET(x) \
111 (((x) << AG7XXX_ETH_XMII_RX_DELAY_LSB) & AG7XXX_ETH_XMII_RX_DELAY_MASK)
112 #define AG7XXX_ETH_XMII_TX_DELAY_LSB 26
113 #define AG7XXX_ETH_XMII_TX_DELAY_MASK 0x0c000000
114 #define AG7XXX_ETH_XMII_TX_DELAY_SET(x) \
115 (((x) << AG7XXX_ETH_XMII_TX_DELAY_LSB) & AG7XXX_ETH_XMII_TX_DELAY_MASK)
116 #define AG7XXX_ETH_XMII_GIGE BIT(25)
118 /* Custom register at 0x18070000 */
119 #define AG7XXX_GMAC_ETH_CFG 0x00
120 #define AG7XXX_ETH_CFG_RXDV_DELAY_LSB 16
121 #define AG7XXX_ETH_CFG_RXDV_DELAY_MASK 0x00030000
122 #define AG7XXX_ETH_CFG_RXDV_DELAY_SET(x) \
123 (((x) << AG7XXX_ETH_CFG_RXDV_DELAY_LSB) & AG7XXX_ETH_CFG_RXDV_DELAY_MASK)
124 #define AG7XXX_ETH_CFG_RXD_DELAY_LSB 14
125 #define AG7XXX_ETH_CFG_RXD_DELAY_MASK 0x0000c000
126 #define AG7XXX_ETH_CFG_RXD_DELAY_SET(x) \
127 (((x) << AG7XXX_ETH_CFG_RXD_DELAY_LSB) & AG7XXX_ETH_CFG_RXD_DELAY_MASK)
128 #define AG7XXX_ETH_CFG_SW_PHY_ADDR_SWAP BIT(8)
129 #define AG7XXX_ETH_CFG_SW_PHY_SWAP BIT(7)
130 #define AG7XXX_ETH_CFG_SW_ONLY_MODE BIT(6)
131 #define AG7XXX_ETH_CFG_GE0_ERR_EN BIT(5)
132 #define AG7XXX_ETH_CFG_MII_GE0_SLAVE BIT(4)
133 #define AG7XXX_ETH_CFG_MII_GE0_MASTER BIT(3)
134 #define AG7XXX_ETH_CFG_GMII_GE0 BIT(2)
135 #define AG7XXX_ETH_CFG_MII_GE0 BIT(1)
136 #define AG7XXX_ETH_CFG_RGMII_GE0 BIT(0)
138 #define CONFIG_TX_DESCR_NUM 8
139 #define CONFIG_RX_DESCR_NUM 8
140 #define CONFIG_ETH_BUFSIZE 2048
141 #define TX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_TX_DESCR_NUM)
142 #define RX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_RX_DESCR_NUM)
144 /* DMA descriptor. */
145 struct ag7xxx_dma_desc {
147 #define AG7XXX_DMADESC_IS_EMPTY BIT(31)
148 #define AG7XXX_DMADESC_FTPP_OVERRIDE_OFFSET 16
149 #define AG7XXX_DMADESC_PKT_SIZE_OFFSET 0
150 #define AG7XXX_DMADESC_PKT_SIZE_MASK 0xfff
156 struct ar7xxx_eth_priv {
157 struct ag7xxx_dma_desc tx_mac_descrtable[CONFIG_TX_DESCR_NUM];
158 struct ag7xxx_dma_desc rx_mac_descrtable[CONFIG_RX_DESCR_NUM];
159 char txbuffs[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
160 char rxbuffs[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
163 void __iomem *phyregs;
165 struct eth_device *dev;
166 struct phy_device *phydev;
172 enum ag7xxx_model model;
176 * Switch and MDIO access
178 static int ag7xxx_switch_read(struct mii_dev *bus, int addr, int reg, u16 *val)
180 struct ar7xxx_eth_priv *priv = bus->priv;
181 void __iomem *regs = priv->phyregs;
184 writel(0x0, regs + AG7XXX_ETH_MII_MGMT_CMD);
185 writel((addr << AG7XXX_ETH_MII_MGMT_ADDRESS_SHIFT) | reg,
186 regs + AG7XXX_ETH_MII_MGMT_ADDRESS);
187 writel(AG7XXX_ETH_MII_MGMT_CMD_READ,
188 regs + AG7XXX_ETH_MII_MGMT_CMD);
190 ret = wait_for_bit_le32(regs + AG7XXX_ETH_MII_MGMT_IND,
191 AG7XXX_ETH_MII_MGMT_IND_BUSY, 0, 1000, 0);
195 *val = readl(regs + AG7XXX_ETH_MII_MGMT_STATUS) & 0xffff;
196 writel(0x0, regs + AG7XXX_ETH_MII_MGMT_CMD);
201 static int ag7xxx_switch_write(struct mii_dev *bus, int addr, int reg, u16 val)
203 struct ar7xxx_eth_priv *priv = bus->priv;
204 void __iomem *regs = priv->phyregs;
207 writel((addr << AG7XXX_ETH_MII_MGMT_ADDRESS_SHIFT) | reg,
208 regs + AG7XXX_ETH_MII_MGMT_ADDRESS);
209 writel(val, regs + AG7XXX_ETH_MII_MGMT_CTRL);
211 ret = wait_for_bit_le32(regs + AG7XXX_ETH_MII_MGMT_IND,
212 AG7XXX_ETH_MII_MGMT_IND_BUSY, 0, 1000, 0);
217 static int ag7xxx_switch_reg_read(struct mii_dev *bus, int reg, u32 *val)
219 struct ar7xxx_eth_priv *priv = bus->priv;
224 u32 reg_temp_w = (reg & 0xfffffffc) >> 1;
228 if (priv->model == AG7XXX_MODEL_AG933X ||
229 priv->model == AG7XXX_MODEL_AG953X) {
232 } else if (priv->model == AG7XXX_MODEL_AG934X ||
233 priv->model == AG7XXX_MODEL_AG956X) {
239 if (priv->model == AG7XXX_MODEL_AG956X)
240 ret = ag7xxx_switch_write(bus, phy_addr, reg_addr, (reg >> 9) & 0x1ff);
242 ret = ag7xxx_switch_write(bus, phy_addr, reg_addr, reg >> 9);
246 phy_temp = ((reg >> 6) & 0x7) | 0x10;
247 if (priv->model == AG7XXX_MODEL_AG956X)
248 reg_temp = reg_temp_w & 0x1f;
250 reg_temp = (reg >> 1) & 0x1e;
253 ret = ag7xxx_switch_read(bus, phy_temp, reg_temp | 0, &rv);
258 if (priv->model == AG7XXX_MODEL_AG956X) {
259 phy_temp = (((reg_temp_w + 1) >> 5) & 0x7) | 0x10;
260 reg_temp = (reg_temp_w + 1) & 0x1f;
261 ret = ag7xxx_switch_read(bus, phy_temp, reg_temp, &rv);
263 ret = ag7xxx_switch_read(bus, phy_temp, reg_temp | 1, &rv);
272 static int ag7xxx_switch_reg_write(struct mii_dev *bus, int reg, u32 val)
274 struct ar7xxx_eth_priv *priv = bus->priv;
279 u32 reg_temp_w = (reg & 0xfffffffc) >> 1;
282 if (priv->model == AG7XXX_MODEL_AG933X ||
283 priv->model == AG7XXX_MODEL_AG953X) {
286 } else if (priv->model == AG7XXX_MODEL_AG934X ||
287 priv->model == AG7XXX_MODEL_AG956X) {
293 if (priv->model == AG7XXX_MODEL_AG956X)
294 ret = ag7xxx_switch_write(bus, phy_addr, reg_addr, (reg >> 9) & 0x1ff);
296 ret = ag7xxx_switch_write(bus, phy_addr, reg_addr, reg >> 9);
300 if (priv->model == AG7XXX_MODEL_AG956X) {
301 reg_temp = (reg_temp_w + 1) & 0x1f;
302 phy_temp = (((reg_temp_w + 1) >> 5) & 0x7) | 0x10;
304 phy_temp = ((reg >> 6) & 0x7) | 0x10;
305 reg_temp = (reg >> 1) & 0x1e;
309 * The switch on AR933x has some special register behavior, which
310 * expects particular write order of their nibbles:
311 * 0x40 ..... MSB first, LSB second
312 * 0x50 ..... MSB first, LSB second
313 * 0x98 ..... LSB first, MSB second
314 * others ... don't care
316 if ((priv->model == AG7XXX_MODEL_AG933X) && (reg == 0x98)) {
317 ret = ag7xxx_switch_write(bus, phy_temp, reg_temp | 0, val & 0xffff);
321 ret = ag7xxx_switch_write(bus, phy_temp, reg_temp | 1, val >> 16);
325 if (priv->model == AG7XXX_MODEL_AG956X)
326 ret = ag7xxx_switch_write(bus, phy_temp, reg_temp, val >> 16);
328 ret = ag7xxx_switch_write(bus, phy_temp, reg_temp | 1, val >> 16);
332 if (priv->model == AG7XXX_MODEL_AG956X) {
333 phy_temp = ((reg_temp_w >> 5) & 0x7) | 0x10;
334 reg_temp = reg_temp_w & 0x1f;
337 ret = ag7xxx_switch_write(bus, phy_temp, reg_temp | 0, val & 0xffff);
345 static int ag7xxx_mdio_rw(struct mii_dev *bus, int addr, int reg, u32 val)
350 /* No idea if this is long enough or too long */
351 int timeout_ms = 1000;
353 /* Dummy read followed by PHY read/write command. */
354 ret = ag7xxx_switch_reg_read(bus, 0x98, &data);
357 data = val | (reg << 16) | (addr << 21) | BIT(30) | BIT(31);
358 ret = ag7xxx_switch_reg_write(bus, 0x98, data);
362 start = get_timer(0);
364 /* Wait for operation to finish */
366 ret = ag7xxx_switch_reg_read(bus, 0x98, &data);
370 if (get_timer(start) > timeout_ms)
372 } while (data & BIT(31));
374 return data & 0xffff;
377 static int ag7xxx_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
379 return ag7xxx_mdio_rw(bus, addr, reg, BIT(27));
382 static int ag7xxx_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
387 ret = ag7xxx_mdio_rw(bus, addr, reg, val);
396 static void ag7xxx_dma_clean_tx(struct udevice *dev)
398 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
399 struct ag7xxx_dma_desc *curr, *next;
403 for (i = 0; i < CONFIG_TX_DESCR_NUM; i++) {
404 curr = &priv->tx_mac_descrtable[i];
405 next = &priv->tx_mac_descrtable[(i + 1) % CONFIG_TX_DESCR_NUM];
407 curr->data_addr = virt_to_phys(&priv->txbuffs[i * CONFIG_ETH_BUFSIZE]);
408 curr->config = AG7XXX_DMADESC_IS_EMPTY;
409 curr->next_desc = virt_to_phys(next);
412 priv->tx_currdescnum = 0;
414 /* Cache: Flush descriptors, don't care about buffers. */
415 start = (u32)(&priv->tx_mac_descrtable[0]);
416 end = start + sizeof(priv->tx_mac_descrtable);
417 flush_dcache_range(start, end);
420 static void ag7xxx_dma_clean_rx(struct udevice *dev)
422 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
423 struct ag7xxx_dma_desc *curr, *next;
427 for (i = 0; i < CONFIG_RX_DESCR_NUM; i++) {
428 curr = &priv->rx_mac_descrtable[i];
429 next = &priv->rx_mac_descrtable[(i + 1) % CONFIG_RX_DESCR_NUM];
431 curr->data_addr = virt_to_phys(&priv->rxbuffs[i * CONFIG_ETH_BUFSIZE]);
432 curr->config = AG7XXX_DMADESC_IS_EMPTY;
433 curr->next_desc = virt_to_phys(next);
436 priv->rx_currdescnum = 0;
438 /* Cache: Flush+Invalidate descriptors, Invalidate buffers. */
439 start = (u32)(&priv->rx_mac_descrtable[0]);
440 end = start + sizeof(priv->rx_mac_descrtable);
441 flush_dcache_range(start, end);
442 invalidate_dcache_range(start, end);
444 start = (u32)&priv->rxbuffs;
445 end = start + sizeof(priv->rxbuffs);
446 invalidate_dcache_range(start, end);
452 static int ag7xxx_eth_send(struct udevice *dev, void *packet, int length)
454 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
455 struct ag7xxx_dma_desc *curr;
458 curr = &priv->tx_mac_descrtable[priv->tx_currdescnum];
460 /* Cache: Invalidate descriptor. */
462 end = start + sizeof(*curr);
463 invalidate_dcache_range(start, end);
465 if (!(curr->config & AG7XXX_DMADESC_IS_EMPTY)) {
466 printf("ag7xxx: Out of TX DMA descriptors!\n");
470 /* Copy the packet into the data buffer. */
471 memcpy(phys_to_virt(curr->data_addr), packet, length);
472 curr->config = length & AG7XXX_DMADESC_PKT_SIZE_MASK;
474 /* Cache: Flush descriptor, Flush buffer. */
476 end = start + sizeof(*curr);
477 flush_dcache_range(start, end);
478 start = (u32)phys_to_virt(curr->data_addr);
479 end = start + length;
480 flush_dcache_range(start, end);
482 /* Load the DMA descriptor and start TX DMA. */
483 writel(AG7XXX_ETH_DMA_TX_CTRL_TXE,
484 priv->regs + AG7XXX_ETH_DMA_TX_CTRL);
486 /* Switch to next TX descriptor. */
487 priv->tx_currdescnum = (priv->tx_currdescnum + 1) % CONFIG_TX_DESCR_NUM;
492 static int ag7xxx_eth_recv(struct udevice *dev, int flags, uchar **packetp)
494 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
495 struct ag7xxx_dma_desc *curr;
496 u32 start, end, length;
498 curr = &priv->rx_mac_descrtable[priv->rx_currdescnum];
500 /* Cache: Invalidate descriptor. */
502 end = start + sizeof(*curr);
503 invalidate_dcache_range(start, end);
505 /* No packets received. */
506 if (curr->config & AG7XXX_DMADESC_IS_EMPTY)
509 length = curr->config & AG7XXX_DMADESC_PKT_SIZE_MASK;
511 /* Cache: Invalidate buffer. */
512 start = (u32)phys_to_virt(curr->data_addr);
513 end = start + length;
514 invalidate_dcache_range(start, end);
516 /* Receive one packet and return length. */
517 *packetp = phys_to_virt(curr->data_addr);
521 static int ag7xxx_eth_free_pkt(struct udevice *dev, uchar *packet,
524 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
525 struct ag7xxx_dma_desc *curr;
528 curr = &priv->rx_mac_descrtable[priv->rx_currdescnum];
530 curr->config = AG7XXX_DMADESC_IS_EMPTY;
532 /* Cache: Flush descriptor. */
534 end = start + sizeof(*curr);
535 flush_dcache_range(start, end);
537 /* Switch to next RX descriptor. */
538 priv->rx_currdescnum = (priv->rx_currdescnum + 1) % CONFIG_RX_DESCR_NUM;
543 static int ag7xxx_eth_start(struct udevice *dev)
545 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
547 /* FIXME: Check if link up */
549 /* Clear the DMA rings. */
550 ag7xxx_dma_clean_tx(dev);
551 ag7xxx_dma_clean_rx(dev);
553 /* Load DMA descriptors and start the RX DMA. */
554 writel(virt_to_phys(&priv->tx_mac_descrtable[priv->tx_currdescnum]),
555 priv->regs + AG7XXX_ETH_DMA_TX_DESC);
556 writel(virt_to_phys(&priv->rx_mac_descrtable[priv->rx_currdescnum]),
557 priv->regs + AG7XXX_ETH_DMA_RX_DESC);
558 writel(AG7XXX_ETH_DMA_RX_CTRL_RXE,
559 priv->regs + AG7XXX_ETH_DMA_RX_CTRL);
564 static void ag7xxx_eth_stop(struct udevice *dev)
566 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
568 /* Stop the TX DMA. */
569 writel(0, priv->regs + AG7XXX_ETH_DMA_TX_CTRL);
570 wait_for_bit_le32(priv->regs + AG7XXX_ETH_DMA_TX_CTRL, ~0, 0,
573 /* Stop the RX DMA. */
574 writel(0, priv->regs + AG7XXX_ETH_DMA_RX_CTRL);
575 wait_for_bit_le32(priv->regs + AG7XXX_ETH_DMA_RX_CTRL, ~0, 0,
582 static int ag7xxx_eth_write_hwaddr(struct udevice *dev)
584 struct eth_pdata *pdata = dev_get_platdata(dev);
585 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
586 unsigned char *mac = pdata->enetaddr;
587 u32 macid_lo, macid_hi;
589 macid_hi = mac[3] | (mac[2] << 8) | (mac[1] << 16) | (mac[0] << 24);
590 macid_lo = (mac[5] << 16) | (mac[4] << 24);
592 writel(macid_lo, priv->regs + AG7XXX_ETH_ADDR1);
593 writel(macid_hi, priv->regs + AG7XXX_ETH_ADDR2);
598 static void ag7xxx_hw_setup(struct udevice *dev)
600 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
603 setbits_be32(priv->regs + AG7XXX_ETH_CFG1,
604 AG7XXX_ETH_CFG1_RX_RST | AG7XXX_ETH_CFG1_TX_RST |
605 AG7XXX_ETH_CFG1_SOFT_RST);
609 writel(AG7XXX_ETH_CFG1_RX_EN | AG7XXX_ETH_CFG1_TX_EN,
610 priv->regs + AG7XXX_ETH_CFG1);
612 if (priv->interface == PHY_INTERFACE_MODE_RMII)
613 speed = AG7XXX_ETH_CFG2_IF_10_100;
615 speed = AG7XXX_ETH_CFG2_IF_1000;
617 clrsetbits_be32(priv->regs + AG7XXX_ETH_CFG2,
618 AG7XXX_ETH_CFG2_IF_SPEED_MASK,
619 speed | AG7XXX_ETH_CFG2_PAD_CRC_EN |
620 AG7XXX_ETH_CFG2_LEN_CHECK);
622 writel(0xfff0000, priv->regs + AG7XXX_ETH_FIFO_CFG_1);
623 writel(0x1fff, priv->regs + AG7XXX_ETH_FIFO_CFG_2);
625 writel(0x1f00, priv->regs + AG7XXX_ETH_FIFO_CFG_0);
626 setbits_be32(priv->regs + AG7XXX_ETH_FIFO_CFG_4, 0x3ffff);
627 writel(0x10ffff, priv->regs + AG7XXX_ETH_FIFO_CFG_1);
628 writel(0xaaa0555, priv->regs + AG7XXX_ETH_FIFO_CFG_2);
629 writel(0x7eccf, priv->regs + AG7XXX_ETH_FIFO_CFG_5);
630 writel(0x1f00140, priv->regs + AG7XXX_ETH_FIFO_CFG_3);
633 static int ag7xxx_mii_get_div(void)
635 ulong freq = get_bus_freq(0);
637 switch (freq / 1000000) {
638 case 150: return 0x7;
639 case 175: return 0x5;
640 case 200: return 0x4;
641 case 210: return 0x9;
642 case 220: return 0x9;
647 static int ag7xxx_mii_setup(struct udevice *dev)
649 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
650 int i, ret, div = ag7xxx_mii_get_div();
653 if (priv->model == AG7XXX_MODEL_AG933X) {
654 /* Unit 0 is PHY-less on AR9331, see datasheet Figure 2-3 */
655 if (priv->interface == PHY_INTERFACE_MODE_RMII)
659 if (priv->model == AG7XXX_MODEL_AG934X)
661 else if (priv->model == AG7XXX_MODEL_AG953X)
663 else if (priv->model == AG7XXX_MODEL_AG956X)
666 if (priv->model == AG7XXX_MODEL_AG934X ||
667 priv->model == AG7XXX_MODEL_AG953X ||
668 priv->model == AG7XXX_MODEL_AG956X) {
669 writel(AG7XXX_ETH_MII_MGMT_CFG_RESET | reg,
670 priv->regs + AG7XXX_ETH_MII_MGMT_CFG);
671 writel(reg, priv->regs + AG7XXX_ETH_MII_MGMT_CFG);
675 for (i = 0; i < 10; i++) {
676 writel(AG7XXX_ETH_MII_MGMT_CFG_RESET | div,
677 priv->regs + AG7XXX_ETH_MII_MGMT_CFG);
678 writel(div, priv->regs + AG7XXX_ETH_MII_MGMT_CFG);
680 /* Check the switch */
681 ret = ag7xxx_switch_reg_read(priv->bus, 0x10c, ®);
685 if (reg != 0x18007fff)
694 static int ag933x_phy_setup_wan(struct udevice *dev)
696 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
698 /* Configure switch port 4 (GMAC0) */
699 return ag7xxx_mdio_write(priv->bus, 4, 0, MII_BMCR, 0x9000);
702 static int ag933x_phy_setup_lan(struct udevice *dev)
704 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
708 /* Reset the switch */
709 ret = ag7xxx_switch_reg_read(priv->bus, 0, ®);
713 ret = ag7xxx_switch_reg_write(priv->bus, 0, reg);
718 ret = ag7xxx_switch_reg_read(priv->bus, 0, ®);
721 } while (reg & BIT(31));
723 /* Configure switch ports 0...3 (GMAC1) */
724 for (i = 0; i < 4; i++) {
725 ret = ag7xxx_mdio_write(priv->bus, 0x4, 0, MII_BMCR, 0x9000);
730 /* Enable CPU port */
731 ret = ag7xxx_switch_reg_write(priv->bus, 0x78, BIT(8));
735 for (i = 0; i < 4; i++) {
736 ret = ag7xxx_switch_reg_write(priv->bus, i * 0x100, BIT(9));
742 ret = ag7xxx_switch_reg_write(priv->bus, 0x38, 0xc000050e);
746 /* Disable Atheros header */
747 ret = ag7xxx_switch_reg_write(priv->bus, 0x104, 0x4004);
751 /* Tag priority mapping */
752 ret = ag7xxx_switch_reg_write(priv->bus, 0x70, 0xfa50);
756 /* Enable ARP packets to the CPU */
757 ret = ag7xxx_switch_reg_read(priv->bus, 0x5c, ®);
761 ret = ag7xxx_switch_reg_write(priv->bus, 0x5c, reg);
768 static int ag953x_phy_setup_wan(struct udevice *dev)
772 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
774 /* Set wan port connect to GE0 */
775 ret = ag7xxx_switch_reg_read(priv->bus, 0x8, ®);
779 ret = ag7xxx_switch_reg_write(priv->bus, 0x8, reg | BIT(28));
783 /* Configure switch port 4 (GMAC0) */
784 ret = ag7xxx_switch_write(priv->bus, 4, MII_BMCR, 0x9000);
791 static int ag953x_phy_setup_lan(struct udevice *dev)
793 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
797 /* Reset the switch */
798 ret = ag7xxx_switch_reg_read(priv->bus, 0, ®);
802 ret = ag7xxx_switch_reg_write(priv->bus, 0, reg | BIT(31));
807 ret = ag7xxx_switch_reg_read(priv->bus, 0, ®);
810 } while (reg & BIT(31));
812 ret = ag7xxx_switch_reg_write(priv->bus, 0x100, 0x4e);
817 ret = ag7xxx_switch_reg_read(priv->bus, 0x4, ®);
821 ret = ag7xxx_switch_reg_write(priv->bus, 0x4, reg | BIT(6));
825 /* Configure switch ports 0...4 (GMAC1) */
826 for (i = 0; i < 5; i++) {
827 ret = ag7xxx_switch_write(priv->bus, i, MII_BMCR, 0x9000);
832 for (i = 0; i < 5; i++) {
833 ret = ag7xxx_switch_reg_write(priv->bus, (i + 2) * 0x100, BIT(9));
839 ret = ag7xxx_switch_reg_write(priv->bus, 0x38, 0xc000050e);
843 /* Disable Atheros header */
844 ret = ag7xxx_switch_reg_write(priv->bus, 0x104, 0x4004);
848 /* Tag priority mapping */
849 ret = ag7xxx_switch_reg_write(priv->bus, 0x70, 0xfa50);
853 /* Enable ARP packets to the CPU */
854 ret = ag7xxx_switch_reg_read(priv->bus, 0x5c, ®);
858 ret = ag7xxx_switch_reg_write(priv->bus, 0x5c, reg | 0x100000);
862 /* Enable broadcast packets to the CPU */
863 ret = ag7xxx_switch_reg_read(priv->bus, 0x2c, ®);
867 ret = ag7xxx_switch_reg_write(priv->bus, 0x2c, reg | BIT(25) | BIT(26));
874 static int ag933x_phy_setup_reset_set(struct udevice *dev, int port)
876 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
879 if (priv->model == AG7XXX_MODEL_AG953X ||
880 priv->model == AG7XXX_MODEL_AG956X) {
881 ret = ag7xxx_switch_write(priv->bus, port, MII_ADVERTISE,
884 ret = ag7xxx_mdio_write(priv->bus, port, 0, MII_ADVERTISE,
885 ADVERTISE_ALL | ADVERTISE_PAUSE_CAP |
886 ADVERTISE_PAUSE_ASYM);
891 if (priv->model == AG7XXX_MODEL_AG934X) {
892 ret = ag7xxx_mdio_write(priv->bus, port, 0, MII_CTRL1000,
896 } else if (priv->model == AG7XXX_MODEL_AG956X) {
897 ret = ag7xxx_switch_write(priv->bus, port, MII_CTRL1000,
903 if (priv->model == AG7XXX_MODEL_AG953X ||
904 priv->model == AG7XXX_MODEL_AG956X)
905 return ag7xxx_switch_write(priv->bus, port, MII_BMCR,
906 BMCR_ANENABLE | BMCR_RESET);
908 return ag7xxx_mdio_write(priv->bus, port, 0, MII_BMCR,
909 BMCR_ANENABLE | BMCR_RESET);
912 static int ag933x_phy_setup_reset_fin(struct udevice *dev, int port)
914 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
918 if (priv->model == AG7XXX_MODEL_AG953X ||
919 priv->model == AG7XXX_MODEL_AG956X) {
921 ret = ag7xxx_switch_read(priv->bus, port, MII_BMCR, ®);
925 } while (reg & BMCR_RESET);
928 ret = ag7xxx_mdio_read(priv->bus, port, 0, MII_BMCR);
932 } while (ret & BMCR_RESET);
938 static int ag933x_phy_setup_common(struct udevice *dev)
940 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
944 if (priv->model == AG7XXX_MODEL_AG933X)
946 else if (priv->model == AG7XXX_MODEL_AG934X ||
947 priv->model == AG7XXX_MODEL_AG953X ||
948 priv->model == AG7XXX_MODEL_AG956X)
953 if (priv->interface == PHY_INTERFACE_MODE_RMII) {
954 ret = ag933x_phy_setup_reset_set(dev, phymax);
958 ret = ag933x_phy_setup_reset_fin(dev, phymax);
962 /* Read out link status */
963 if (priv->model == AG7XXX_MODEL_AG953X)
964 ret = ag7xxx_switch_read(priv->bus, phymax, MII_MIPSCR, ®);
966 ret = ag7xxx_mdio_read(priv->bus, phymax, 0, MII_MIPSCR);
974 for (i = 0; i < phymax; i++) {
975 ret = ag933x_phy_setup_reset_set(dev, i);
980 for (i = 0; i < phymax; i++) {
981 ret = ag933x_phy_setup_reset_fin(dev, i);
986 for (i = 0; i < phymax; i++) {
987 /* Read out link status */
988 if (priv->model == AG7XXX_MODEL_AG953X ||
989 priv->model == AG7XXX_MODEL_AG956X)
990 ret = ag7xxx_switch_read(priv->bus, i, MII_MIPSCR, ®);
992 ret = ag7xxx_mdio_read(priv->bus, i, 0, MII_MIPSCR);
1000 static int ag934x_phy_setup(struct udevice *dev)
1002 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
1006 ret = ag7xxx_switch_reg_write(priv->bus, 0x624, 0x7f7f7f7f);
1009 ret = ag7xxx_switch_reg_write(priv->bus, 0x10, 0x40000000);
1012 ret = ag7xxx_switch_reg_write(priv->bus, 0x4, 0x07600000);
1015 ret = ag7xxx_switch_reg_write(priv->bus, 0xc, 0x01000000);
1018 ret = ag7xxx_switch_reg_write(priv->bus, 0x7c, 0x0000007e);
1022 /* AR8327/AR8328 v1.0 fixup */
1023 ret = ag7xxx_switch_reg_read(priv->bus, 0, ®);
1026 if ((reg & 0xffff) == 0x1201) {
1027 for (i = 0; i < 5; i++) {
1028 ret = ag7xxx_mdio_write(priv->bus, i, 0, 0x1d, 0x0);
1031 ret = ag7xxx_mdio_write(priv->bus, i, 0, 0x1e, 0x02ea);
1034 ret = ag7xxx_mdio_write(priv->bus, i, 0, 0x1d, 0x3d);
1037 ret = ag7xxx_mdio_write(priv->bus, i, 0, 0x1e, 0x68a0);
1043 ret = ag7xxx_switch_reg_read(priv->bus, 0x66c, ®);
1047 ret = ag7xxx_switch_reg_write(priv->bus, 0x66c, reg);
1054 static int ag956x_phy_setup(struct udevice *dev)
1056 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
1060 ret = ag7xxx_switch_reg_read(priv->bus, 0x0, ®);
1063 if ((reg & 0xffff) >= 0x1301)
1068 ret = ag7xxx_switch_reg_write(priv->bus, 0x4, BIT(7));
1072 ret = ag7xxx_switch_reg_write(priv->bus, 0xe0, ctrl);
1076 ret = ag7xxx_switch_reg_write(priv->bus, 0x624, 0x7f7f7f7f);
1081 * Values suggested by the switch team when s17 in sgmii
1082 * configuration. 0x10(S17_PWS_REG) = 0x602613a0
1084 ret = ag7xxx_switch_reg_write(priv->bus, 0x10, 0x602613a0);
1088 ret = ag7xxx_switch_reg_write(priv->bus, 0x7c, 0x0000007e);
1092 /* AR8337/AR8334 v1.0 fixup */
1093 ret = ag7xxx_switch_reg_read(priv->bus, 0, ®);
1096 if ((reg & 0xffff) == 0x1301) {
1097 for (i = 0; i < 5; i++) {
1098 /* Turn on Gigabit clock */
1099 ret = ag7xxx_switch_write(priv->bus, i, 0x1d, 0x3d);
1102 ret = ag7xxx_switch_write(priv->bus, i, 0x1e, 0x6820);
1111 static int ag7xxx_mac_probe(struct udevice *dev)
1113 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
1116 ag7xxx_hw_setup(dev);
1117 ret = ag7xxx_mii_setup(dev);
1121 ag7xxx_eth_write_hwaddr(dev);
1123 if (priv->model == AG7XXX_MODEL_AG933X) {
1124 if (priv->interface == PHY_INTERFACE_MODE_RMII)
1125 ret = ag933x_phy_setup_wan(dev);
1127 ret = ag933x_phy_setup_lan(dev);
1128 } else if (priv->model == AG7XXX_MODEL_AG953X) {
1129 if (priv->interface == PHY_INTERFACE_MODE_RMII)
1130 ret = ag953x_phy_setup_wan(dev);
1132 ret = ag953x_phy_setup_lan(dev);
1133 } else if (priv->model == AG7XXX_MODEL_AG934X) {
1134 ret = ag934x_phy_setup(dev);
1135 } else if (priv->model == AG7XXX_MODEL_AG956X) {
1136 ret = ag956x_phy_setup(dev);
1144 return ag933x_phy_setup_common(dev);
1147 static int ag7xxx_mdio_probe(struct udevice *dev)
1149 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
1150 struct mii_dev *bus = mdio_alloc();
1155 bus->read = ag7xxx_mdio_read;
1156 bus->write = ag7xxx_mdio_write;
1157 snprintf(bus->name, sizeof(bus->name), dev->name);
1159 bus->priv = (void *)priv;
1161 return mdio_register(bus);
1164 static int ag7xxx_get_phy_iface_offset(struct udevice *dev)
1168 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev), "phy");
1170 debug("%s: PHY OF node not found (ret=%i)\n", __func__, offset);
1174 offset = fdt_parent_offset(gd->fdt_blob, offset);
1176 debug("%s: PHY OF node parent MDIO bus not found (ret=%i)\n",
1181 offset = fdt_parent_offset(gd->fdt_blob, offset);
1183 debug("%s: PHY MDIO OF node parent MAC not found (ret=%i)\n",
1191 static int ag7xxx_eth_probe(struct udevice *dev)
1193 struct eth_pdata *pdata = dev_get_platdata(dev);
1194 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
1195 void __iomem *iobase, *phyiobase;
1198 /* Decoding of convoluted PHY wiring on Atheros MIPS. */
1199 ret = ag7xxx_get_phy_iface_offset(dev);
1202 phyreg = fdtdec_get_int(gd->fdt_blob, ret, "reg", -1);
1204 iobase = map_physmem(pdata->iobase, 0x200, MAP_NOCACHE);
1205 phyiobase = map_physmem(phyreg, 0x200, MAP_NOCACHE);
1207 debug("%s, iobase=%p, phyiobase=%p, priv=%p\n",
1208 __func__, iobase, phyiobase, priv);
1209 priv->regs = iobase;
1210 priv->phyregs = phyiobase;
1211 priv->interface = pdata->phy_interface;
1212 priv->model = dev_get_driver_data(dev);
1214 ret = ag7xxx_mdio_probe(dev);
1218 priv->bus = miiphy_get_dev_by_name(dev->name);
1220 ret = ag7xxx_mac_probe(dev);
1221 debug("%s, ret=%d\n", __func__, ret);
1226 static int ag7xxx_eth_remove(struct udevice *dev)
1228 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
1231 mdio_unregister(priv->bus);
1232 mdio_free(priv->bus);
1237 static const struct eth_ops ag7xxx_eth_ops = {
1238 .start = ag7xxx_eth_start,
1239 .send = ag7xxx_eth_send,
1240 .recv = ag7xxx_eth_recv,
1241 .free_pkt = ag7xxx_eth_free_pkt,
1242 .stop = ag7xxx_eth_stop,
1243 .write_hwaddr = ag7xxx_eth_write_hwaddr,
1246 static int ag7xxx_eth_ofdata_to_platdata(struct udevice *dev)
1248 struct eth_pdata *pdata = dev_get_platdata(dev);
1249 const char *phy_mode;
1252 pdata->iobase = devfdt_get_addr(dev);
1253 pdata->phy_interface = -1;
1255 /* Decoding of convoluted PHY wiring on Atheros MIPS. */
1256 ret = ag7xxx_get_phy_iface_offset(dev);
1260 phy_mode = fdt_getprop(gd->fdt_blob, ret, "phy-mode", NULL);
1262 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
1263 if (pdata->phy_interface == -1) {
1264 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1271 static const struct udevice_id ag7xxx_eth_ids[] = {
1272 { .compatible = "qca,ag933x-mac", .data = AG7XXX_MODEL_AG933X },
1273 { .compatible = "qca,ag934x-mac", .data = AG7XXX_MODEL_AG934X },
1274 { .compatible = "qca,ag953x-mac", .data = AG7XXX_MODEL_AG953X },
1275 { .compatible = "qca,ag956x-mac", .data = AG7XXX_MODEL_AG956X },
1279 U_BOOT_DRIVER(eth_ag7xxx) = {
1280 .name = "eth_ag7xxx",
1282 .of_match = ag7xxx_eth_ids,
1283 .ofdata_to_platdata = ag7xxx_eth_ofdata_to_platdata,
1284 .probe = ag7xxx_eth_probe,
1285 .remove = ag7xxx_eth_remove,
1286 .ops = &ag7xxx_eth_ops,
1287 .priv_auto_alloc_size = sizeof(struct ar7xxx_eth_priv),
1288 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1289 .flags = DM_FLAG_ALLOC_PRIV_DMA,