blob: 4d87e2d1f36fffe75e22c584ae291cd5dc85baca [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michal Simek6fc7c452011-10-06 20:35:35 +00002/*
Ashok Reddy Soma645aa762021-06-24 00:34:41 -06003 * Copyright (C) 2021 Waymo LLC
Michal Simek6fc7c452011-10-06 20:35:35 +00004 * Copyright (C) 2011 Michal Simek <monstr@monstr.eu>
5 * Copyright (C) 2011 PetaLogix
6 * Copyright (C) 2010 Xilinx, Inc. All rights reserved.
Michal Simek6fc7c452011-10-06 20:35:35 +00007 */
8
9#include <config.h>
Simon Glass63334482019-11-14 12:57:39 -070010#include <cpu_func.h>
Simon Glass1ab16922022-07-31 12:28:48 -060011#include <display_options.h>
Michal Simek682baac2015-12-08 15:44:41 +010012#include <dm.h>
Venkatesh Yadav Abbarapu1bbe4502022-09-29 10:26:05 +053013#include <dm/device_compat.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Michal Simek6fc7c452011-10-06 20:35:35 +000015#include <net.h>
16#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Michal Simek6fc7c452011-10-06 20:35:35 +000018#include <asm/io.h>
19#include <phy.h>
20#include <miiphy.h>
Siva Durga Prasad Paladugu20633622017-01-06 16:18:50 +053021#include <wait_bit.h>
Simon Glassdbd79542020-05-10 11:40:11 -060022#include <linux/delay.h>
T Karthik Reddy4a3f40b2022-05-10 13:26:09 +020023#include <eth_phy.h>
Michal Simek6fc7c452011-10-06 20:35:35 +000024
Michal Simek682baac2015-12-08 15:44:41 +010025DECLARE_GLOBAL_DATA_PTR;
26
Michal Simek6fc7c452011-10-06 20:35:35 +000027/* Link setup */
28#define XAE_EMMC_LINKSPEED_MASK 0xC0000000 /* Link speed */
29#define XAE_EMMC_LINKSPD_10 0x00000000 /* Link Speed mask for 10 Mbit */
30#define XAE_EMMC_LINKSPD_100 0x40000000 /* Link Speed mask for 100 Mbit */
31#define XAE_EMMC_LINKSPD_1000 0x80000000 /* Link Speed mask for 1000 Mbit */
32
33/* Interrupt Status/Enable/Mask Registers bit definitions */
34#define XAE_INT_RXRJECT_MASK 0x00000008 /* Rx frame rejected */
35#define XAE_INT_MGTRDY_MASK 0x00000080 /* MGT clock Lock */
36
37/* Receive Configuration Word 1 (RCW1) Register bit definitions */
38#define XAE_RCW1_RX_MASK 0x10000000 /* Receiver enable */
39
40/* Transmitter Configuration (TC) Register bit definitions */
41#define XAE_TC_TX_MASK 0x10000000 /* Transmitter enable */
42
43#define XAE_UAW1_UNICASTADDR_MASK 0x0000FFFF
44
45/* MDIO Management Configuration (MC) Register bit definitions */
46#define XAE_MDIO_MC_MDIOEN_MASK 0x00000040 /* MII management enable*/
47
48/* MDIO Management Control Register (MCR) Register bit definitions */
49#define XAE_MDIO_MCR_PHYAD_MASK 0x1F000000 /* Phy Address Mask */
50#define XAE_MDIO_MCR_PHYAD_SHIFT 24 /* Phy Address Shift */
51#define XAE_MDIO_MCR_REGAD_MASK 0x001F0000 /* Reg Address Mask */
52#define XAE_MDIO_MCR_REGAD_SHIFT 16 /* Reg Address Shift */
53#define XAE_MDIO_MCR_OP_READ_MASK 0x00008000 /* Op Code Read Mask */
54#define XAE_MDIO_MCR_OP_WRITE_MASK 0x00004000 /* Op Code Write Mask */
55#define XAE_MDIO_MCR_INITIATE_MASK 0x00000800 /* Ready Mask */
56#define XAE_MDIO_MCR_READY_MASK 0x00000080 /* Ready Mask */
57
58#define XAE_MDIO_DIV_DFT 29 /* Default MDIO clock divisor */
59
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +053060#define XAXIDMA_BD_STS_ACTUAL_LEN_MASK 0x007FFFFF /* Actual len */
61
Michal Simek6fc7c452011-10-06 20:35:35 +000062/* DMA macros */
63/* Bitmasks of XAXIDMA_CR_OFFSET register */
64#define XAXIDMA_CR_RUNSTOP_MASK 0x00000001 /* Start/stop DMA channel */
65#define XAXIDMA_CR_RESET_MASK 0x00000004 /* Reset DMA engine */
66
67/* Bitmasks of XAXIDMA_SR_OFFSET register */
68#define XAXIDMA_HALTED_MASK 0x00000001 /* DMA channel halted */
69
70/* Bitmask for interrupts */
71#define XAXIDMA_IRQ_IOC_MASK 0x00001000 /* Completion intr */
72#define XAXIDMA_IRQ_DELAY_MASK 0x00002000 /* Delay interrupt */
73#define XAXIDMA_IRQ_ALL_MASK 0x00007000 /* All interrupts */
74
75/* Bitmasks of XAXIDMA_BD_CTRL_OFFSET register */
76#define XAXIDMA_BD_CTRL_TXSOF_MASK 0x08000000 /* First tx packet */
77#define XAXIDMA_BD_CTRL_TXEOF_MASK 0x04000000 /* Last tx packet */
78
Ashok Reddy Soma645aa762021-06-24 00:34:41 -060079/* Bitmasks for XXV Ethernet MAC */
80#define XXV_TC_TX_MASK 0x00000001
81#define XXV_TC_FCS_MASK 0x00000002
82#define XXV_RCW1_RX_MASK 0x00000001
83#define XXV_RCW1_FCS_MASK 0x00000002
84
85#define DMAALIGN 128
86#define XXV_MIN_PKT_SIZE 60
Michal Simek6fc7c452011-10-06 20:35:35 +000087
88static u8 rxframe[PKTSIZE_ALIGN] __attribute((aligned(DMAALIGN)));
Ashok Reddy Soma645aa762021-06-24 00:34:41 -060089static u8 txminframe[XXV_MIN_PKT_SIZE] __attribute((aligned(DMAALIGN)));
90
91enum emac_variant {
92 EMAC_1G = 0,
93 EMAC_10G_25G = 1,
94};
Michal Simek6fc7c452011-10-06 20:35:35 +000095
96/* Reflect dma offsets */
97struct axidma_reg {
98 u32 control; /* DMACR */
99 u32 status; /* DMASR */
Vipul Kumar3b06a0f2018-01-23 14:52:35 +0530100 u32 current; /* CURDESC low 32 bit */
101 u32 current_hi; /* CURDESC high 32 bit */
102 u32 tail; /* TAILDESC low 32 bit */
103 u32 tail_hi; /* TAILDESC high 32 bit */
Michal Simek6fc7c452011-10-06 20:35:35 +0000104};
105
Ashok Reddy Soma3c4df952021-06-24 00:34:40 -0600106/* Platform data structures */
107struct axidma_plat {
108 struct eth_pdata eth_pdata;
109 struct axidma_reg *dmatx;
110 struct axidma_reg *dmarx;
Andy Chiu58af7eb2022-11-01 11:57:59 +0800111 int pcsaddr;
Ashok Reddy Soma3c4df952021-06-24 00:34:40 -0600112 int phyaddr;
113 u8 eth_hasnobuf;
Maxim Kochetkov61cd6fd2023-08-11 10:43:51 +0300114 ofnode phynode;
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600115 enum emac_variant mactype;
Ashok Reddy Soma3c4df952021-06-24 00:34:40 -0600116};
117
Michal Simek6fc7c452011-10-06 20:35:35 +0000118/* Private driver structures */
119struct axidma_priv {
120 struct axidma_reg *dmatx;
121 struct axidma_reg *dmarx;
Andy Chiu58af7eb2022-11-01 11:57:59 +0800122 int pcsaddr;
Michal Simek6fc7c452011-10-06 20:35:35 +0000123 int phyaddr;
Michal Simek6cb55e72015-12-09 14:39:42 +0100124 struct axi_regs *iobase;
Michal Simek682baac2015-12-08 15:44:41 +0100125 phy_interface_t interface;
Michal Simek6fc7c452011-10-06 20:35:35 +0000126 struct phy_device *phydev;
127 struct mii_dev *bus;
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530128 u8 eth_hasnobuf;
Maxim Kochetkov61cd6fd2023-08-11 10:43:51 +0300129 ofnode phynode;
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600130 enum emac_variant mactype;
Michal Simek6fc7c452011-10-06 20:35:35 +0000131};
132
133/* BD descriptors */
134struct axidma_bd {
Ashok Reddy Soma91d455a2020-09-03 08:36:43 -0600135 u32 next_desc; /* Next descriptor pointer */
136 u32 next_desc_msb;
137 u32 buf_addr; /* Buffer address */
138 u32 buf_addr_msb;
Michal Simek6fc7c452011-10-06 20:35:35 +0000139 u32 reserved3;
140 u32 reserved4;
141 u32 cntrl; /* Control */
142 u32 status; /* Status */
143 u32 app0;
144 u32 app1; /* TX start << 16 | insert */
145 u32 app2; /* TX csum seed */
146 u32 app3;
147 u32 app4;
148 u32 sw_id_offset;
149 u32 reserved5;
150 u32 reserved6;
151};
152
153/* Static BDs - driver uses only one BD */
154static struct axidma_bd tx_bd __attribute((aligned(DMAALIGN)));
155static struct axidma_bd rx_bd __attribute((aligned(DMAALIGN)));
156
157struct axi_regs {
158 u32 reserved[3];
159 u32 is; /* 0xC: Interrupt status */
160 u32 reserved2;
161 u32 ie; /* 0x14: Interrupt enable */
162 u32 reserved3[251];
163 u32 rcw1; /* 0x404: Rx Configuration Word 1 */
164 u32 tc; /* 0x408: Tx Configuration */
165 u32 reserved4;
166 u32 emmc; /* 0x410: EMAC mode configuration */
167 u32 reserved5[59];
168 u32 mdio_mc; /* 0x500: MII Management Config */
169 u32 mdio_mcr; /* 0x504: MII Management Control */
170 u32 mdio_mwd; /* 0x508: MII Management Write Data */
171 u32 mdio_mrd; /* 0x50C: MII Management Read Data */
172 u32 reserved6[124];
173 u32 uaw0; /* 0x700: Unicast address word 0 */
174 u32 uaw1; /* 0x704: Unicast address word 1 */
175};
176
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600177struct xxv_axi_regs {
178 u32 gt_reset; /* 0x0 */
179 u32 reserved[2];
180 u32 tc; /* 0xC: Tx Configuration */
181 u32 reserved2;
182 u32 rcw1; /* 0x14: Rx Configuration Word 1 */
183};
184
Michal Simek6fc7c452011-10-06 20:35:35 +0000185/* Use MII register 1 (MII status register) to detect PHY */
186#define PHY_DETECT_REG 1
187
188/*
189 * Mask used to verify certain PHY features (or register contents)
190 * in the register above:
191 * 0x1000: 10Mbps full duplex support
192 * 0x0800: 10Mbps half duplex support
193 * 0x0008: Auto-negotiation support
194 */
195#define PHY_DETECT_MASK 0x1808
196
Michal Simekf5221872015-12-09 14:36:31 +0100197static inline int mdio_wait(struct axi_regs *regs)
Michal Simek6fc7c452011-10-06 20:35:35 +0000198{
Michal Simek6fc7c452011-10-06 20:35:35 +0000199 u32 timeout = 200;
200
201 /* Wait till MDIO interface is ready to accept a new transaction. */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530202 while (timeout && (!(readl(&regs->mdio_mcr)
Michal Simek6fc7c452011-10-06 20:35:35 +0000203 & XAE_MDIO_MCR_READY_MASK))) {
204 timeout--;
205 udelay(1);
206 }
207 if (!timeout) {
208 printf("%s: Timeout\n", __func__);
209 return 1;
210 }
211 return 0;
212}
213
Vipul Kumar3b06a0f2018-01-23 14:52:35 +0530214/**
215 * axienet_dma_write - Memory mapped Axi DMA register Buffer Descriptor write.
216 * @bd: pointer to BD descriptor structure
217 * @desc: Address offset of DMA descriptors
218 *
219 * This function writes the value into the corresponding Axi DMA register.
220 */
221static inline void axienet_dma_write(struct axidma_bd *bd, u32 *desc)
222{
223#if defined(CONFIG_PHYS_64BIT)
Ashok Reddy Soma91d455a2020-09-03 08:36:43 -0600224 writeq((unsigned long)bd, desc);
Vipul Kumar3b06a0f2018-01-23 14:52:35 +0530225#else
226 writel((u32)bd, desc);
227#endif
228}
229
Michal Simek41beca12015-12-09 14:44:38 +0100230static u32 phyread(struct axidma_priv *priv, u32 phyaddress, u32 registernum,
231 u16 *val)
Michal Simek6fc7c452011-10-06 20:35:35 +0000232{
Michal Simek41beca12015-12-09 14:44:38 +0100233 struct axi_regs *regs = priv->iobase;
Michal Simek6fc7c452011-10-06 20:35:35 +0000234 u32 mdioctrlreg = 0;
235
Michal Simekf5221872015-12-09 14:36:31 +0100236 if (mdio_wait(regs))
Michal Simek6fc7c452011-10-06 20:35:35 +0000237 return 1;
238
239 mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) &
240 XAE_MDIO_MCR_PHYAD_MASK) |
241 ((registernum << XAE_MDIO_MCR_REGAD_SHIFT)
242 & XAE_MDIO_MCR_REGAD_MASK) |
243 XAE_MDIO_MCR_INITIATE_MASK |
244 XAE_MDIO_MCR_OP_READ_MASK;
245
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530246 writel(mdioctrlreg, &regs->mdio_mcr);
Michal Simek6fc7c452011-10-06 20:35:35 +0000247
Michal Simekf5221872015-12-09 14:36:31 +0100248 if (mdio_wait(regs))
Michal Simek6fc7c452011-10-06 20:35:35 +0000249 return 1;
250
251 /* Read data */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530252 *val = readl(&regs->mdio_mrd);
Michal Simek6fc7c452011-10-06 20:35:35 +0000253 return 0;
254}
255
Michal Simek41beca12015-12-09 14:44:38 +0100256static u32 phywrite(struct axidma_priv *priv, u32 phyaddress, u32 registernum,
257 u32 data)
Michal Simek6fc7c452011-10-06 20:35:35 +0000258{
Michal Simek41beca12015-12-09 14:44:38 +0100259 struct axi_regs *regs = priv->iobase;
Michal Simek6fc7c452011-10-06 20:35:35 +0000260 u32 mdioctrlreg = 0;
261
Michal Simekf5221872015-12-09 14:36:31 +0100262 if (mdio_wait(regs))
Michal Simek6fc7c452011-10-06 20:35:35 +0000263 return 1;
264
265 mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) &
266 XAE_MDIO_MCR_PHYAD_MASK) |
267 ((registernum << XAE_MDIO_MCR_REGAD_SHIFT)
268 & XAE_MDIO_MCR_REGAD_MASK) |
269 XAE_MDIO_MCR_INITIATE_MASK |
270 XAE_MDIO_MCR_OP_WRITE_MASK;
271
272 /* Write data */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530273 writel(data, &regs->mdio_mwd);
Michal Simek6fc7c452011-10-06 20:35:35 +0000274
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530275 writel(mdioctrlreg, &regs->mdio_mcr);
Michal Simek6fc7c452011-10-06 20:35:35 +0000276
Michal Simekf5221872015-12-09 14:36:31 +0100277 if (mdio_wait(regs))
Michal Simek6fc7c452011-10-06 20:35:35 +0000278 return 1;
279
280 return 0;
281}
282
Michal Simek0b13ee22015-12-08 16:10:05 +0100283static int axiemac_phy_init(struct udevice *dev)
Michal Simek6fc7c452011-10-06 20:35:35 +0000284{
285 u16 phyreg;
Patrick van Gelder70ab9432020-06-03 14:18:04 +0200286 int i;
287 u32 ret;
Michal Simek682baac2015-12-08 15:44:41 +0100288 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek6cb55e72015-12-09 14:39:42 +0100289 struct axi_regs *regs = priv->iobase;
Michal Simek6fc7c452011-10-06 20:35:35 +0000290 struct phy_device *phydev;
291
292 u32 supported = SUPPORTED_10baseT_Half |
293 SUPPORTED_10baseT_Full |
294 SUPPORTED_100baseT_Half |
295 SUPPORTED_100baseT_Full |
296 SUPPORTED_1000baseT_Half |
297 SUPPORTED_1000baseT_Full;
298
Michal Simek0b13ee22015-12-08 16:10:05 +0100299 /* Set default MDIO divisor */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530300 writel(XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK, &regs->mdio_mc);
Michal Simek0b13ee22015-12-08 16:10:05 +0100301
T Karthik Reddy4a3f40b2022-05-10 13:26:09 +0200302 if (IS_ENABLED(CONFIG_DM_ETH_PHY))
303 priv->phyaddr = eth_phy_get_addr(dev);
304
Andy Chiu58af7eb2022-11-01 11:57:59 +0800305 /*
306 * Set address of PCS/PMA PHY to the one pointed by phy-handle for
307 * backward compatibility.
308 */
309 if (priv->phyaddr != -1 && priv->pcsaddr == 0)
310 priv->pcsaddr = priv->phyaddr;
311
Michal Simek6fc7c452011-10-06 20:35:35 +0000312 if (priv->phyaddr == -1) {
313 /* Detect the PHY address */
314 for (i = 31; i >= 0; i--) {
Michal Simek41beca12015-12-09 14:44:38 +0100315 ret = phyread(priv, i, PHY_DETECT_REG, &phyreg);
Michal Simek6fc7c452011-10-06 20:35:35 +0000316 if (!ret && (phyreg != 0xFFFF) &&
317 ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
318 /* Found a valid PHY address */
319 priv->phyaddr = i;
320 debug("axiemac: Found valid phy address, %x\n",
Michal Simek2f1e0652015-12-09 10:54:53 +0100321 i);
Michal Simek6fc7c452011-10-06 20:35:35 +0000322 break;
323 }
324 }
325 }
326
327 /* Interface - look at tsec */
Siva Durga Prasad Paladugu49348512016-02-21 15:46:14 +0530328 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
Venkatesh Yadav Abbarapu1bbe4502022-09-29 10:26:05 +0530329 if (IS_ERR_OR_NULL(phydev)) {
330 dev_err(dev, "phy_connect() failed\n");
331 return -ENODEV;
332 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000333
334 phydev->supported &= supported;
335 phydev->advertising = phydev->supported;
336 priv->phydev = phydev;
Maxim Kochetkov61cd6fd2023-08-11 10:43:51 +0300337 if (ofnode_valid(priv->phynode))
338 priv->phydev->node = priv->phynode;
Michal Simek6fc7c452011-10-06 20:35:35 +0000339 phy_config(phydev);
Michal Simek0b13ee22015-12-08 16:10:05 +0100340
341 return 0;
342}
343
Andy Chiu8144a2a2022-11-01 11:58:00 +0800344static int pcs_pma_startup(struct axidma_priv *priv)
345{
346 u32 rc, retry_cnt = 0;
347 u16 mii_reg;
348
349 rc = phyread(priv, priv->pcsaddr, MII_BMCR, &mii_reg);
350 if (rc)
351 goto failed_mdio;
352
353 if (!(mii_reg & BMCR_ANENABLE)) {
354 mii_reg |= BMCR_ANENABLE;
355 if (phywrite(priv, priv->pcsaddr, MII_BMCR, mii_reg))
356 goto failed_mdio;
357 }
358
359 /*
360 * Check the internal PHY status and warn user if the link between it
361 * and the external PHY is not obtained.
362 */
363 debug("axiemac: waiting for link status of the PCS/PMA PHY");
Marek Vasute4d70fd2024-05-31 18:47:17 +0200364 while (retry_cnt * 10 < CONFIG_PHY_ANEG_TIMEOUT) {
Andy Chiu8144a2a2022-11-01 11:58:00 +0800365 rc = phyread(priv, priv->pcsaddr, MII_BMSR, &mii_reg);
366 if ((mii_reg & BMSR_LSTATUS) && mii_reg != 0xffff && !rc) {
367 debug(".Done\n");
368 return 0;
369 }
370 if ((retry_cnt++ % 10) == 0)
371 debug(".");
372 mdelay(10);
373 }
374 debug("\n");
375 printf("axiemac: Warning, PCS/PMA PHY@%d is not ready, link is down\n",
376 priv->pcsaddr);
377 return 1;
378failed_mdio:
379 printf("axiemac: MDIO to the PCS/PMA PHY has failed\n");
380 return 1;
381}
382
Michal Simek0b13ee22015-12-08 16:10:05 +0100383/* Setting axi emac and phy to proper setting */
384static int setup_phy(struct udevice *dev)
385{
Siva Durga Prasad Paladugua1c6ed82016-02-21 15:46:15 +0530386 u16 temp;
387 u32 speed, emmc_reg, ret;
Michal Simek0b13ee22015-12-08 16:10:05 +0100388 struct axidma_priv *priv = dev_get_priv(dev);
389 struct axi_regs *regs = priv->iobase;
390 struct phy_device *phydev = priv->phydev;
391
Siva Durga Prasad Paladugua1c6ed82016-02-21 15:46:15 +0530392 if (priv->interface == PHY_INTERFACE_MODE_SGMII) {
393 /*
394 * In SGMII cases the isolate bit might set
395 * after DMA and ethernet resets and hence
396 * check and clear if set.
397 */
Andy Chiu58af7eb2022-11-01 11:57:59 +0800398 ret = phyread(priv, priv->pcsaddr, MII_BMCR, &temp);
Siva Durga Prasad Paladugua1c6ed82016-02-21 15:46:15 +0530399 if (ret)
400 return 0;
401 if (temp & BMCR_ISOLATE) {
402 temp &= ~BMCR_ISOLATE;
Andy Chiu58af7eb2022-11-01 11:57:59 +0800403 ret = phywrite(priv, priv->pcsaddr, MII_BMCR, temp);
Siva Durga Prasad Paladugua1c6ed82016-02-21 15:46:15 +0530404 if (ret)
405 return 0;
406 }
407 }
408
Timur Tabi42387462012-07-09 08:52:43 +0000409 if (phy_startup(phydev)) {
410 printf("axiemac: could not initialize PHY %s\n",
411 phydev->dev->name);
412 return 0;
413 }
Andy Chiu8144a2a2022-11-01 11:58:00 +0800414 if (priv->interface == PHY_INTERFACE_MODE_SGMII ||
415 priv->interface == PHY_INTERFACE_MODE_1000BASEX) {
416 if (pcs_pma_startup(priv))
417 return 0;
418 }
Michal Simek5848f132013-11-21 16:15:51 +0100419 if (!phydev->link) {
420 printf("%s: No link.\n", phydev->dev->name);
421 return 0;
422 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000423
424 switch (phydev->speed) {
425 case 1000:
426 speed = XAE_EMMC_LINKSPD_1000;
427 break;
428 case 100:
429 speed = XAE_EMMC_LINKSPD_100;
430 break;
431 case 10:
432 speed = XAE_EMMC_LINKSPD_10;
433 break;
434 default:
435 return 0;
436 }
437
438 /* Setup the emac for the phy speed */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530439 emmc_reg = readl(&regs->emmc);
Michal Simek6fc7c452011-10-06 20:35:35 +0000440 emmc_reg &= ~XAE_EMMC_LINKSPEED_MASK;
441 emmc_reg |= speed;
442
443 /* Write new speed setting out to Axi Ethernet */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530444 writel(emmc_reg, &regs->emmc);
Michal Simek6fc7c452011-10-06 20:35:35 +0000445
446 /*
447 * Setting the operating speed of the MAC needs a delay. There
448 * doesn't seem to be register to poll, so please consider this
449 * during your application design.
450 */
451 udelay(1);
452
453 return 1;
454}
455
456/* STOP DMA transfers */
Michal Simek8fbf79f2015-12-16 09:18:12 +0100457static void axiemac_stop(struct udevice *dev)
Michal Simek6fc7c452011-10-06 20:35:35 +0000458{
Michal Simek682baac2015-12-08 15:44:41 +0100459 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek6fc7c452011-10-06 20:35:35 +0000460 u32 temp;
461
462 /* Stop the hardware */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530463 temp = readl(&priv->dmatx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000464 temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530465 writel(temp, &priv->dmatx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000466
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530467 temp = readl(&priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000468 temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530469 writel(temp, &priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000470
471 debug("axiemac: Halted\n");
472}
473
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600474static int xxv_axi_ethernet_init(struct axidma_priv *priv)
475{
476 struct xxv_axi_regs *regs = (struct xxv_axi_regs *)priv->iobase;
477
478 writel(readl(&regs->rcw1) | XXV_RCW1_FCS_MASK, &regs->rcw1);
479 writel(readl(&regs->tc) | XXV_TC_FCS_MASK, &regs->tc);
480 writel(readl(&regs->tc) | XXV_TC_TX_MASK, &regs->tc);
481 writel(readl(&regs->rcw1) | XXV_RCW1_RX_MASK, &regs->rcw1);
482
483 return 0;
484}
485
Michal Simek638c0ef2015-12-09 14:53:51 +0100486static int axi_ethernet_init(struct axidma_priv *priv)
Michal Simek6fc7c452011-10-06 20:35:35 +0000487{
Michal Simek638c0ef2015-12-09 14:53:51 +0100488 struct axi_regs *regs = priv->iobase;
Siva Durga Prasad Paladugu20633622017-01-06 16:18:50 +0530489 int err;
Michal Simek6fc7c452011-10-06 20:35:35 +0000490
491 /*
492 * Check the status of the MgtRdy bit in the interrupt status
493 * registers. This must be done to allow the MGT clock to become stable
494 * for the Sgmii and 1000BaseX PHY interfaces. No other register reads
495 * will be valid until this bit is valid.
496 * The bit is always a 1 for all other PHY interfaces.
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530497 * Interrupt status and enable registers are not available in non
498 * processor mode and hence bypass in this mode
Michal Simek6fc7c452011-10-06 20:35:35 +0000499 */
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530500 if (!priv->eth_hasnobuf) {
Álvaro Fernåndez Rojas918de032018-01-23 17:14:55 +0100501 err = wait_for_bit_le32(&regs->is, XAE_INT_MGTRDY_MASK,
502 true, 200, false);
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530503 if (err) {
504 printf("%s: Timeout\n", __func__);
505 return 1;
506 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000507
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530508 /*
509 * Stop the device and reset HW
510 * Disable interrupts
511 */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530512 writel(0, &regs->ie);
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530513 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000514
515 /* Disable the receiver */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530516 writel(readl(&regs->rcw1) & ~XAE_RCW1_RX_MASK, &regs->rcw1);
Michal Simek6fc7c452011-10-06 20:35:35 +0000517
518 /*
519 * Stopping the receiver in mid-packet causes a dropped packet
520 * indication from HW. Clear it.
521 */
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530522 if (!priv->eth_hasnobuf) {
523 /* Set the interrupt status register to clear the interrupt */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530524 writel(XAE_INT_RXRJECT_MASK, &regs->is);
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530525 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000526
527 /* Setup HW */
528 /* Set default MDIO divisor */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530529 writel(XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK, &regs->mdio_mc);
Michal Simek6fc7c452011-10-06 20:35:35 +0000530
531 debug("axiemac: InitHw done\n");
532 return 0;
533}
534
Michal Simek8fbf79f2015-12-16 09:18:12 +0100535static int axiemac_write_hwaddr(struct udevice *dev)
Michal Simek6fc7c452011-10-06 20:35:35 +0000536{
Simon Glassfa20e932020-12-03 16:55:20 -0700537 struct eth_pdata *pdata = dev_get_plat(dev);
Michal Simek682baac2015-12-08 15:44:41 +0100538 struct axidma_priv *priv = dev_get_priv(dev);
539 struct axi_regs *regs = priv->iobase;
Michal Simek6fc7c452011-10-06 20:35:35 +0000540
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600541 if (priv->mactype != EMAC_1G)
542 return 0;
543
Michal Simek6fc7c452011-10-06 20:35:35 +0000544 /* Set the MAC address */
Michal Simek682baac2015-12-08 15:44:41 +0100545 int val = ((pdata->enetaddr[3] << 24) | (pdata->enetaddr[2] << 16) |
546 (pdata->enetaddr[1] << 8) | (pdata->enetaddr[0]));
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530547 writel(val, &regs->uaw0);
Michal Simek6fc7c452011-10-06 20:35:35 +0000548
Michal Simek682baac2015-12-08 15:44:41 +0100549 val = (pdata->enetaddr[5] << 8) | pdata->enetaddr[4];
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530550 val |= readl(&regs->uaw1) & ~XAE_UAW1_UNICASTADDR_MASK;
551 writel(val, &regs->uaw1);
Michal Simek6fc7c452011-10-06 20:35:35 +0000552 return 0;
553}
554
555/* Reset DMA engine */
Michal Simek638c0ef2015-12-09 14:53:51 +0100556static void axi_dma_init(struct axidma_priv *priv)
Michal Simek6fc7c452011-10-06 20:35:35 +0000557{
Michal Simek6fc7c452011-10-06 20:35:35 +0000558 u32 timeout = 500;
559
560 /* Reset the engine so the hardware starts from a known state */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530561 writel(XAXIDMA_CR_RESET_MASK, &priv->dmatx->control);
562 writel(XAXIDMA_CR_RESET_MASK, &priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000563
564 /* At the initialization time, hardware should finish reset quickly */
565 while (timeout--) {
566 /* Check transmit/receive channel */
567 /* Reset is done when the reset bit is low */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530568 if (!((readl(&priv->dmatx->control) |
569 readl(&priv->dmarx->control))
Michal Simek5aa45392015-10-28 11:00:47 +0100570 & XAXIDMA_CR_RESET_MASK)) {
Michal Simek6fc7c452011-10-06 20:35:35 +0000571 break;
572 }
573 }
574 if (!timeout)
575 printf("%s: Timeout\n", __func__);
576}
577
Michal Simek8fbf79f2015-12-16 09:18:12 +0100578static int axiemac_start(struct udevice *dev)
Michal Simek6fc7c452011-10-06 20:35:35 +0000579{
Michal Simek682baac2015-12-08 15:44:41 +0100580 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek6fc7c452011-10-06 20:35:35 +0000581 u32 temp;
582
583 debug("axiemac: Init started\n");
584 /*
585 * Initialize AXIDMA engine. AXIDMA engine must be initialized before
586 * AxiEthernet. During AXIDMA engine initialization, AXIDMA hardware is
587 * reset, and since AXIDMA reset line is connected to AxiEthernet, this
588 * would ensure a reset of AxiEthernet.
589 */
Michal Simek638c0ef2015-12-09 14:53:51 +0100590 axi_dma_init(priv);
Michal Simek6fc7c452011-10-06 20:35:35 +0000591
592 /* Initialize AxiEthernet hardware. */
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600593 if (priv->mactype == EMAC_1G) {
594 if (axi_ethernet_init(priv))
595 return -1;
596 } else {
597 if (xxv_axi_ethernet_init(priv))
598 return -1;
599 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000600
601 /* Disable all RX interrupts before RxBD space setup */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530602 temp = readl(&priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000603 temp &= ~XAXIDMA_IRQ_ALL_MASK;
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530604 writel(temp, &priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000605
606 /* Start DMA RX channel. Now it's ready to receive data.*/
Vipul Kumar3b06a0f2018-01-23 14:52:35 +0530607 axienet_dma_write(&rx_bd, &priv->dmarx->current);
Michal Simek6fc7c452011-10-06 20:35:35 +0000608
609 /* Setup the BD. */
610 memset(&rx_bd, 0, sizeof(rx_bd));
Ashok Reddy Soma91d455a2020-09-03 08:36:43 -0600611 rx_bd.next_desc = lower_32_bits((unsigned long)&rx_bd);
612 rx_bd.buf_addr = lower_32_bits((unsigned long)&rxframe);
613#if defined(CONFIG_PHYS_64BIT)
614 rx_bd.next_desc_msb = upper_32_bits((unsigned long)&rx_bd);
615 rx_bd.buf_addr_msb = upper_32_bits((unsigned long)&rxframe);
616#endif
Michal Simek6fc7c452011-10-06 20:35:35 +0000617 rx_bd.cntrl = sizeof(rxframe);
618 /* Flush the last BD so DMA core could see the updates */
Ashok Reddy Somaf29a5702020-09-03 08:36:44 -0600619 flush_cache((phys_addr_t)&rx_bd, sizeof(rx_bd));
Michal Simek6fc7c452011-10-06 20:35:35 +0000620
621 /* It is necessary to flush rxframe because if you don't do it
622 * then cache can contain uninitialized data */
Ashok Reddy Somaf29a5702020-09-03 08:36:44 -0600623 flush_cache((phys_addr_t)&rxframe, sizeof(rxframe));
Michal Simek6fc7c452011-10-06 20:35:35 +0000624
625 /* Start the hardware */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530626 temp = readl(&priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000627 temp |= XAXIDMA_CR_RUNSTOP_MASK;
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530628 writel(temp, &priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000629
630 /* Rx BD is ready - start */
Vipul Kumar3b06a0f2018-01-23 14:52:35 +0530631 axienet_dma_write(&rx_bd, &priv->dmarx->tail);
Michal Simek6fc7c452011-10-06 20:35:35 +0000632
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600633 if (priv->mactype == EMAC_1G) {
634 struct axi_regs *regs = priv->iobase;
635 /* Enable TX */
636 writel(XAE_TC_TX_MASK, &regs->tc);
637 /* Enable RX */
638 writel(XAE_RCW1_RX_MASK, &regs->rcw1);
Michal Simek6fc7c452011-10-06 20:35:35 +0000639
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600640 /* PHY setup */
641 if (!setup_phy(dev)) {
642 axiemac_stop(dev);
643 return -1;
644 }
645 } else {
646 struct xxv_axi_regs *regs = (struct xxv_axi_regs *)priv->iobase;
647 /* Enable TX */
648 writel(readl(&regs->tc) | XXV_TC_TX_MASK, &regs->tc);
649
650 /* Enable RX */
651 writel(readl(&regs->rcw1) | XXV_RCW1_RX_MASK, &regs->rcw1);
Michal Simek6fc7c452011-10-06 20:35:35 +0000652 }
653
654 debug("axiemac: Init complete\n");
655 return 0;
656}
657
Michal Simek682baac2015-12-08 15:44:41 +0100658static int axiemac_send(struct udevice *dev, void *ptr, int len)
Michal Simek6fc7c452011-10-06 20:35:35 +0000659{
Michal Simek682baac2015-12-08 15:44:41 +0100660 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek6fc7c452011-10-06 20:35:35 +0000661 u32 timeout;
662
663 if (len > PKTSIZE_ALIGN)
664 len = PKTSIZE_ALIGN;
665
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600666 /* If size is less than min packet size, pad to min size */
667 if (priv->mactype == EMAC_10G_25G && len < XXV_MIN_PKT_SIZE) {
668 memset(txminframe, 0, XXV_MIN_PKT_SIZE);
669 memcpy(txminframe, ptr, len);
670 len = XXV_MIN_PKT_SIZE;
671 ptr = txminframe;
672 }
673
Michal Simek6fc7c452011-10-06 20:35:35 +0000674 /* Flush packet to main memory to be trasfered by DMA */
Ashok Reddy Somaf29a5702020-09-03 08:36:44 -0600675 flush_cache((phys_addr_t)ptr, len);
Michal Simek6fc7c452011-10-06 20:35:35 +0000676
677 /* Setup Tx BD */
678 memset(&tx_bd, 0, sizeof(tx_bd));
679 /* At the end of the ring, link the last BD back to the top */
Ashok Reddy Soma91d455a2020-09-03 08:36:43 -0600680 tx_bd.next_desc = lower_32_bits((unsigned long)&tx_bd);
681 tx_bd.buf_addr = lower_32_bits((unsigned long)ptr);
682#if defined(CONFIG_PHYS_64BIT)
683 tx_bd.next_desc_msb = upper_32_bits((unsigned long)&tx_bd);
684 tx_bd.buf_addr_msb = upper_32_bits((unsigned long)ptr);
685#endif
Michal Simek6fc7c452011-10-06 20:35:35 +0000686 /* Save len */
687 tx_bd.cntrl = len | XAXIDMA_BD_CTRL_TXSOF_MASK |
688 XAXIDMA_BD_CTRL_TXEOF_MASK;
689
690 /* Flush the last BD so DMA core could see the updates */
Ashok Reddy Somaf29a5702020-09-03 08:36:44 -0600691 flush_cache((phys_addr_t)&tx_bd, sizeof(tx_bd));
Michal Simek6fc7c452011-10-06 20:35:35 +0000692
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530693 if (readl(&priv->dmatx->status) & XAXIDMA_HALTED_MASK) {
Michal Simek6fc7c452011-10-06 20:35:35 +0000694 u32 temp;
Vipul Kumar3b06a0f2018-01-23 14:52:35 +0530695 axienet_dma_write(&tx_bd, &priv->dmatx->current);
Michal Simek6fc7c452011-10-06 20:35:35 +0000696 /* Start the hardware */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530697 temp = readl(&priv->dmatx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000698 temp |= XAXIDMA_CR_RUNSTOP_MASK;
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530699 writel(temp, &priv->dmatx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000700 }
701
702 /* Start transfer */
Vipul Kumar3b06a0f2018-01-23 14:52:35 +0530703 axienet_dma_write(&tx_bd, &priv->dmatx->tail);
Michal Simek6fc7c452011-10-06 20:35:35 +0000704
705 /* Wait for transmission to complete */
706 debug("axiemac: Waiting for tx to be done\n");
707 timeout = 200;
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530708 while (timeout && (!(readl(&priv->dmatx->status) &
Michal Simek5aa45392015-10-28 11:00:47 +0100709 (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))) {
Michal Simek6fc7c452011-10-06 20:35:35 +0000710 timeout--;
711 udelay(1);
712 }
713 if (!timeout) {
714 printf("%s: Timeout\n", __func__);
715 return 1;
716 }
717
718 debug("axiemac: Sending complete\n");
719 return 0;
720}
721
Michal Simek638c0ef2015-12-09 14:53:51 +0100722static int isrxready(struct axidma_priv *priv)
Michal Simek6fc7c452011-10-06 20:35:35 +0000723{
724 u32 status;
Michal Simek6fc7c452011-10-06 20:35:35 +0000725
726 /* Read pending interrupts */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530727 status = readl(&priv->dmarx->status);
Michal Simek6fc7c452011-10-06 20:35:35 +0000728
729 /* Acknowledge pending interrupts */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530730 writel(status & XAXIDMA_IRQ_ALL_MASK, &priv->dmarx->status);
Michal Simek6fc7c452011-10-06 20:35:35 +0000731
732 /*
733 * If Reception done interrupt is asserted, call RX call back function
734 * to handle the processed BDs and then raise the according flag.
735 */
736 if ((status & (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))
737 return 1;
738
739 return 0;
740}
741
Michal Simek682baac2015-12-08 15:44:41 +0100742static int axiemac_recv(struct udevice *dev, int flags, uchar **packetp)
Michal Simek6fc7c452011-10-06 20:35:35 +0000743{
744 u32 length;
Michal Simek682baac2015-12-08 15:44:41 +0100745 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek6fc7c452011-10-06 20:35:35 +0000746 u32 temp;
747
748 /* Wait for an incoming packet */
Michal Simek638c0ef2015-12-09 14:53:51 +0100749 if (!isrxready(priv))
Maksim Kiselev3789b992023-07-19 09:53:37 +0300750 return -EAGAIN;
Michal Simek6fc7c452011-10-06 20:35:35 +0000751
752 debug("axiemac: RX data ready\n");
753
754 /* Disable IRQ for a moment till packet is handled */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530755 temp = readl(&priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000756 temp &= ~XAXIDMA_IRQ_ALL_MASK;
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530757 writel(temp, &priv->dmarx->control);
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600758 if (!priv->eth_hasnobuf && priv->mactype == EMAC_1G)
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530759 length = rx_bd.app4 & 0xFFFF; /* max length mask */
760 else
761 length = rx_bd.status & XAXIDMA_BD_STS_ACTUAL_LEN_MASK;
Michal Simek6fc7c452011-10-06 20:35:35 +0000762
Michal Simek6fc7c452011-10-06 20:35:35 +0000763#ifdef DEBUG
764 print_buffer(&rxframe, &rxframe[0], 1, length, 16);
765#endif
Michal Simek0305be82015-12-09 14:13:23 +0100766
767 *packetp = rxframe;
768 return length;
769}
770
771static int axiemac_free_pkt(struct udevice *dev, uchar *packet, int length)
772{
773 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek6fc7c452011-10-06 20:35:35 +0000774
775#ifdef DEBUG
776 /* It is useful to clear buffer to be sure that it is consistent */
777 memset(rxframe, 0, sizeof(rxframe));
778#endif
779 /* Setup RxBD */
780 /* Clear the whole buffer and setup it again - all flags are cleared */
781 memset(&rx_bd, 0, sizeof(rx_bd));
Ashok Reddy Soma91d455a2020-09-03 08:36:43 -0600782 rx_bd.next_desc = lower_32_bits((unsigned long)&rx_bd);
783 rx_bd.buf_addr = lower_32_bits((unsigned long)&rxframe);
784#if defined(CONFIG_PHYS_64BIT)
785 rx_bd.next_desc_msb = upper_32_bits((unsigned long)&rx_bd);
786 rx_bd.buf_addr_msb = upper_32_bits((unsigned long)&rxframe);
787#endif
Michal Simek6fc7c452011-10-06 20:35:35 +0000788 rx_bd.cntrl = sizeof(rxframe);
789
790 /* Write bd to HW */
Ashok Reddy Somaf29a5702020-09-03 08:36:44 -0600791 flush_cache((phys_addr_t)&rx_bd, sizeof(rx_bd));
Michal Simek6fc7c452011-10-06 20:35:35 +0000792
793 /* It is necessary to flush rxframe because if you don't do it
794 * then cache will contain previous packet */
Ashok Reddy Somaf29a5702020-09-03 08:36:44 -0600795 flush_cache((phys_addr_t)&rxframe, sizeof(rxframe));
Michal Simek6fc7c452011-10-06 20:35:35 +0000796
797 /* Rx BD is ready - start again */
Vipul Kumar3b06a0f2018-01-23 14:52:35 +0530798 axienet_dma_write(&rx_bd, &priv->dmarx->tail);
Michal Simek6fc7c452011-10-06 20:35:35 +0000799
800 debug("axiemac: RX completed, framelength = %d\n", length);
801
Michal Simek682baac2015-12-08 15:44:41 +0100802 return 0;
Michal Simek6fc7c452011-10-06 20:35:35 +0000803}
804
Michal Simek682baac2015-12-08 15:44:41 +0100805static int axiemac_miiphy_read(struct mii_dev *bus, int addr,
806 int devad, int reg)
Michal Simek6fc7c452011-10-06 20:35:35 +0000807{
Michal Simek682baac2015-12-08 15:44:41 +0100808 int ret;
809 u16 value;
Michal Simek6fc7c452011-10-06 20:35:35 +0000810
Michal Simek682baac2015-12-08 15:44:41 +0100811 ret = phyread(bus->priv, addr, reg, &value);
812 debug("axiemac: Read MII 0x%x, 0x%x, 0x%x, %d\n", addr, reg,
813 value, ret);
814 return value;
Michal Simek6fc7c452011-10-06 20:35:35 +0000815}
816
Michal Simek682baac2015-12-08 15:44:41 +0100817static int axiemac_miiphy_write(struct mii_dev *bus, int addr, int devad,
818 int reg, u16 value)
Michal Simek6fc7c452011-10-06 20:35:35 +0000819{
Michal Simek682baac2015-12-08 15:44:41 +0100820 debug("axiemac: Write MII 0x%x, 0x%x, 0x%x\n", addr, reg, value);
821 return phywrite(bus->priv, addr, reg, value);
Michal Simek6fc7c452011-10-06 20:35:35 +0000822}
823
Michal Simek682baac2015-12-08 15:44:41 +0100824static int axi_emac_probe(struct udevice *dev)
Michal Simek6fc7c452011-10-06 20:35:35 +0000825{
Ashok Reddy Soma3c4df952021-06-24 00:34:40 -0600826 struct axidma_plat *plat = dev_get_plat(dev);
827 struct eth_pdata *pdata = &plat->eth_pdata;
Michal Simek682baac2015-12-08 15:44:41 +0100828 struct axidma_priv *priv = dev_get_priv(dev);
829 int ret;
830
Ashok Reddy Soma3c4df952021-06-24 00:34:40 -0600831 priv->iobase = (struct axi_regs *)pdata->iobase;
832 priv->dmatx = plat->dmatx;
833 /* RX channel offset is 0x30 */
834 priv->dmarx = (struct axidma_reg *)((phys_addr_t)priv->dmatx + 0x30);
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600835 priv->mactype = plat->mactype;
Ashok Reddy Soma3c4df952021-06-24 00:34:40 -0600836
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600837 if (priv->mactype == EMAC_1G) {
838 priv->eth_hasnobuf = plat->eth_hasnobuf;
Andy Chiu58af7eb2022-11-01 11:57:59 +0800839 priv->pcsaddr = plat->pcsaddr;
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600840 priv->phyaddr = plat->phyaddr;
Maxim Kochetkov61cd6fd2023-08-11 10:43:51 +0300841 priv->phynode = plat->phynode;
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600842 priv->interface = pdata->phy_interface;
Michal Simek682baac2015-12-08 15:44:41 +0100843
T Karthik Reddy4a3f40b2022-05-10 13:26:09 +0200844 if (IS_ENABLED(CONFIG_DM_ETH_PHY))
845 priv->bus = eth_phy_get_mdio_bus(dev);
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600846
T Karthik Reddy4a3f40b2022-05-10 13:26:09 +0200847 if (!priv->bus) {
848 priv->bus = mdio_alloc();
849 priv->bus->read = axiemac_miiphy_read;
850 priv->bus->write = axiemac_miiphy_write;
851 priv->bus->priv = priv;
852
853 ret = mdio_register_seq(priv->bus, dev_seq(dev));
854 if (ret)
855 return ret;
856 }
857
858 if (IS_ENABLED(CONFIG_DM_ETH_PHY))
859 eth_phy_set_mdio_bus(dev, priv->bus);
Michal Simek682baac2015-12-08 15:44:41 +0100860
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600861 axiemac_phy_init(dev);
862 }
Michal Simek0b13ee22015-12-08 16:10:05 +0100863
T Karthik Reddy4a3f40b2022-05-10 13:26:09 +0200864 printf("AXI EMAC: %lx, phyaddr %d, interface %s\n", (ulong)pdata->iobase,
865 priv->phyaddr, phy_string_for_interface(pdata->phy_interface));
866
Michal Simek6fc7c452011-10-06 20:35:35 +0000867 return 0;
868}
869
Michal Simek682baac2015-12-08 15:44:41 +0100870static int axi_emac_remove(struct udevice *dev)
Michal Simek6fc7c452011-10-06 20:35:35 +0000871{
Michal Simek682baac2015-12-08 15:44:41 +0100872 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek6fc7c452011-10-06 20:35:35 +0000873
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600874 if (priv->mactype == EMAC_1G) {
875 free(priv->phydev);
876 mdio_unregister(priv->bus);
877 mdio_free(priv->bus);
878 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000879
Michal Simek682baac2015-12-08 15:44:41 +0100880 return 0;
881}
882
883static const struct eth_ops axi_emac_ops = {
Michal Simek8fbf79f2015-12-16 09:18:12 +0100884 .start = axiemac_start,
Michal Simek682baac2015-12-08 15:44:41 +0100885 .send = axiemac_send,
886 .recv = axiemac_recv,
Michal Simek0305be82015-12-09 14:13:23 +0100887 .free_pkt = axiemac_free_pkt,
Michal Simek8fbf79f2015-12-16 09:18:12 +0100888 .stop = axiemac_stop,
889 .write_hwaddr = axiemac_write_hwaddr,
Michal Simek682baac2015-12-08 15:44:41 +0100890};
891
Simon Glassaad29ae2020-12-03 16:55:21 -0700892static int axi_emac_of_to_plat(struct udevice *dev)
Michal Simek682baac2015-12-08 15:44:41 +0100893{
Ashok Reddy Soma3c4df952021-06-24 00:34:40 -0600894 struct axidma_plat *plat = dev_get_plat(dev);
895 struct eth_pdata *pdata = &plat->eth_pdata;
Maxim Kochetkov61cd6fd2023-08-11 10:43:51 +0300896 struct ofnode_phandle_args pcs_node, axistream_node;
897 ofnode phynode;
898 int ret;
Michal Simek6fc7c452011-10-06 20:35:35 +0000899
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900900 pdata->iobase = dev_read_addr(dev);
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600901 plat->mactype = dev_get_driver_data(dev);
Michal Simek6fc7c452011-10-06 20:35:35 +0000902
Maxim Kochetkov61cd6fd2023-08-11 10:43:51 +0300903 ret = dev_read_phandle_with_args(dev, "axistream-connected", NULL, 0, 0,
904 &axistream_node);
Mayuresh Chitale69e51042023-11-16 22:10:24 +0530905 if (!ret)
906 plat->dmatx = (struct axidma_reg *)ofnode_get_addr(axistream_node.node);
907 else
908 plat->dmatx = (struct axidma_reg *)dev_read_addr_index(dev, 1);
Maxim Kochetkov61cd6fd2023-08-11 10:43:51 +0300909
Ashok Reddy Soma3c4df952021-06-24 00:34:40 -0600910 if (!plat->dmatx) {
Michal Simek682baac2015-12-08 15:44:41 +0100911 printf("%s: axi_dma register space not found\n", __func__);
912 return -EINVAL;
913 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000914
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600915 if (plat->mactype == EMAC_1G) {
916 plat->phyaddr = -1;
Andy Chiu58af7eb2022-11-01 11:57:59 +0800917 /* PHYAD 0 always redirects to the PCS/PMA PHY */
918 plat->pcsaddr = 0;
Michal Simek6fc7c452011-10-06 20:35:35 +0000919
Maxim Kochetkov61cd6fd2023-08-11 10:43:51 +0300920 phynode = dev_get_phy_node(dev);
921 if (ofnode_valid(phynode)) {
T Karthik Reddy4a3f40b2022-05-10 13:26:09 +0200922 if (!(IS_ENABLED(CONFIG_DM_ETH_PHY)))
Maxim Kochetkov61cd6fd2023-08-11 10:43:51 +0300923 plat->phyaddr = ofnode_read_u32_default(phynode,
924 "reg", -1);
925 plat->phynode = phynode;
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600926 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000927
Marek BehĂșnbc194772022-04-07 00:33:01 +0200928 pdata->phy_interface = dev_read_phy_mode(dev);
Marek BehĂșn48631e42022-04-07 00:33:03 +0200929 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600930 return -EINVAL;
Michal Simek682baac2015-12-08 15:44:41 +0100931
Maxim Kochetkov61cd6fd2023-08-11 10:43:51 +0300932 plat->eth_hasnobuf = dev_read_bool(dev, "xlnx,eth-hasnobuf");
Andy Chiu58af7eb2022-11-01 11:57:59 +0800933
934 if (pdata->phy_interface == PHY_INTERFACE_MODE_SGMII ||
935 pdata->phy_interface == PHY_INTERFACE_MODE_1000BASEX) {
Maxim Kochetkov61cd6fd2023-08-11 10:43:51 +0300936 ret = dev_read_phandle_with_args(dev, "pcs-handle", NULL, 0, 0,
937 &pcs_node);
938 if (!ret) {
939 plat->pcsaddr = ofnode_read_u32_default(pcs_node.node,
940 "reg", -1);
Andy Chiu58af7eb2022-11-01 11:57:59 +0800941 }
942 }
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600943 }
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530944
Michal Simek682baac2015-12-08 15:44:41 +0100945 return 0;
Michal Simek6fc7c452011-10-06 20:35:35 +0000946}
Michal Simek682baac2015-12-08 15:44:41 +0100947
948static const struct udevice_id axi_emac_ids[] = {
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600949 { .compatible = "xlnx,axi-ethernet-1.00.a", .data = (uintptr_t)EMAC_1G },
950 { .compatible = "xlnx,xxv-ethernet-1.0", .data = (uintptr_t)EMAC_10G_25G },
Michal Simek682baac2015-12-08 15:44:41 +0100951 { }
952};
953
954U_BOOT_DRIVER(axi_emac) = {
955 .name = "axi_emac",
956 .id = UCLASS_ETH,
957 .of_match = axi_emac_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700958 .of_to_plat = axi_emac_of_to_plat,
Michal Simek682baac2015-12-08 15:44:41 +0100959 .probe = axi_emac_probe,
960 .remove = axi_emac_remove,
961 .ops = &axi_emac_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700962 .priv_auto = sizeof(struct axidma_priv),
Ashok Reddy Soma3c4df952021-06-24 00:34:40 -0600963 .plat_auto = sizeof(struct axidma_plat),
Michal Simek682baac2015-12-08 15:44:41 +0100964};