blob: 5f5bc650bef3de6101a5c56193f59d668acdb7cc [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>
10#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070011#include <cpu_func.h>
Simon Glass1ab16922022-07-31 12:28:48 -060012#include <display_options.h>
Michal Simek682baac2015-12-08 15:44:41 +010013#include <dm.h>
Venkatesh Yadav Abbarapu1bbe4502022-09-29 10:26:05 +053014#include <dm/device_compat.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Michal Simek6fc7c452011-10-06 20:35:35 +000016#include <net.h>
17#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060018#include <asm/global_data.h>
Michal Simek6fc7c452011-10-06 20:35:35 +000019#include <asm/io.h>
20#include <phy.h>
21#include <miiphy.h>
Siva Durga Prasad Paladugu20633622017-01-06 16:18:50 +053022#include <wait_bit.h>
Simon Glassdbd79542020-05-10 11:40:11 -060023#include <linux/delay.h>
T Karthik Reddy4a3f40b2022-05-10 13:26:09 +020024#include <eth_phy.h>
Michal Simek6fc7c452011-10-06 20:35:35 +000025
Michal Simek682baac2015-12-08 15:44:41 +010026DECLARE_GLOBAL_DATA_PTR;
27
Michal Simek6fc7c452011-10-06 20:35:35 +000028/* Link setup */
29#define XAE_EMMC_LINKSPEED_MASK 0xC0000000 /* Link speed */
30#define XAE_EMMC_LINKSPD_10 0x00000000 /* Link Speed mask for 10 Mbit */
31#define XAE_EMMC_LINKSPD_100 0x40000000 /* Link Speed mask for 100 Mbit */
32#define XAE_EMMC_LINKSPD_1000 0x80000000 /* Link Speed mask for 1000 Mbit */
33
34/* Interrupt Status/Enable/Mask Registers bit definitions */
35#define XAE_INT_RXRJECT_MASK 0x00000008 /* Rx frame rejected */
36#define XAE_INT_MGTRDY_MASK 0x00000080 /* MGT clock Lock */
37
38/* Receive Configuration Word 1 (RCW1) Register bit definitions */
39#define XAE_RCW1_RX_MASK 0x10000000 /* Receiver enable */
40
41/* Transmitter Configuration (TC) Register bit definitions */
42#define XAE_TC_TX_MASK 0x10000000 /* Transmitter enable */
43
44#define XAE_UAW1_UNICASTADDR_MASK 0x0000FFFF
45
46/* MDIO Management Configuration (MC) Register bit definitions */
47#define XAE_MDIO_MC_MDIOEN_MASK 0x00000040 /* MII management enable*/
48
49/* MDIO Management Control Register (MCR) Register bit definitions */
50#define XAE_MDIO_MCR_PHYAD_MASK 0x1F000000 /* Phy Address Mask */
51#define XAE_MDIO_MCR_PHYAD_SHIFT 24 /* Phy Address Shift */
52#define XAE_MDIO_MCR_REGAD_MASK 0x001F0000 /* Reg Address Mask */
53#define XAE_MDIO_MCR_REGAD_SHIFT 16 /* Reg Address Shift */
54#define XAE_MDIO_MCR_OP_READ_MASK 0x00008000 /* Op Code Read Mask */
55#define XAE_MDIO_MCR_OP_WRITE_MASK 0x00004000 /* Op Code Write Mask */
56#define XAE_MDIO_MCR_INITIATE_MASK 0x00000800 /* Ready Mask */
57#define XAE_MDIO_MCR_READY_MASK 0x00000080 /* Ready Mask */
58
59#define XAE_MDIO_DIV_DFT 29 /* Default MDIO clock divisor */
60
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +053061#define XAXIDMA_BD_STS_ACTUAL_LEN_MASK 0x007FFFFF /* Actual len */
62
Michal Simek6fc7c452011-10-06 20:35:35 +000063/* DMA macros */
64/* Bitmasks of XAXIDMA_CR_OFFSET register */
65#define XAXIDMA_CR_RUNSTOP_MASK 0x00000001 /* Start/stop DMA channel */
66#define XAXIDMA_CR_RESET_MASK 0x00000004 /* Reset DMA engine */
67
68/* Bitmasks of XAXIDMA_SR_OFFSET register */
69#define XAXIDMA_HALTED_MASK 0x00000001 /* DMA channel halted */
70
71/* Bitmask for interrupts */
72#define XAXIDMA_IRQ_IOC_MASK 0x00001000 /* Completion intr */
73#define XAXIDMA_IRQ_DELAY_MASK 0x00002000 /* Delay interrupt */
74#define XAXIDMA_IRQ_ALL_MASK 0x00007000 /* All interrupts */
75
76/* Bitmasks of XAXIDMA_BD_CTRL_OFFSET register */
77#define XAXIDMA_BD_CTRL_TXSOF_MASK 0x08000000 /* First tx packet */
78#define XAXIDMA_BD_CTRL_TXEOF_MASK 0x04000000 /* Last tx packet */
79
Ashok Reddy Soma645aa762021-06-24 00:34:41 -060080/* Bitmasks for XXV Ethernet MAC */
81#define XXV_TC_TX_MASK 0x00000001
82#define XXV_TC_FCS_MASK 0x00000002
83#define XXV_RCW1_RX_MASK 0x00000001
84#define XXV_RCW1_FCS_MASK 0x00000002
85
86#define DMAALIGN 128
87#define XXV_MIN_PKT_SIZE 60
Michal Simek6fc7c452011-10-06 20:35:35 +000088
89static u8 rxframe[PKTSIZE_ALIGN] __attribute((aligned(DMAALIGN)));
Ashok Reddy Soma645aa762021-06-24 00:34:41 -060090static u8 txminframe[XXV_MIN_PKT_SIZE] __attribute((aligned(DMAALIGN)));
91
92enum emac_variant {
93 EMAC_1G = 0,
94 EMAC_10G_25G = 1,
95};
Michal Simek6fc7c452011-10-06 20:35:35 +000096
97/* Reflect dma offsets */
98struct axidma_reg {
99 u32 control; /* DMACR */
100 u32 status; /* DMASR */
Vipul Kumar3b06a0f2018-01-23 14:52:35 +0530101 u32 current; /* CURDESC low 32 bit */
102 u32 current_hi; /* CURDESC high 32 bit */
103 u32 tail; /* TAILDESC low 32 bit */
104 u32 tail_hi; /* TAILDESC high 32 bit */
Michal Simek6fc7c452011-10-06 20:35:35 +0000105};
106
Ashok Reddy Soma3c4df952021-06-24 00:34:40 -0600107/* Platform data structures */
108struct axidma_plat {
109 struct eth_pdata eth_pdata;
110 struct axidma_reg *dmatx;
111 struct axidma_reg *dmarx;
112 int phyaddr;
113 u8 eth_hasnobuf;
114 int phy_of_handle;
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;
122 int phyaddr;
Michal Simek6cb55e72015-12-09 14:39:42 +0100123 struct axi_regs *iobase;
Michal Simek682baac2015-12-08 15:44:41 +0100124 phy_interface_t interface;
Michal Simek6fc7c452011-10-06 20:35:35 +0000125 struct phy_device *phydev;
126 struct mii_dev *bus;
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530127 u8 eth_hasnobuf;
Siva Durga Prasad Paladuguc3d94f62019-03-15 17:46:45 +0530128 int phy_of_handle;
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600129 enum emac_variant mactype;
Michal Simek6fc7c452011-10-06 20:35:35 +0000130};
131
132/* BD descriptors */
133struct axidma_bd {
Ashok Reddy Soma91d455a2020-09-03 08:36:43 -0600134 u32 next_desc; /* Next descriptor pointer */
135 u32 next_desc_msb;
136 u32 buf_addr; /* Buffer address */
137 u32 buf_addr_msb;
Michal Simek6fc7c452011-10-06 20:35:35 +0000138 u32 reserved3;
139 u32 reserved4;
140 u32 cntrl; /* Control */
141 u32 status; /* Status */
142 u32 app0;
143 u32 app1; /* TX start << 16 | insert */
144 u32 app2; /* TX csum seed */
145 u32 app3;
146 u32 app4;
147 u32 sw_id_offset;
148 u32 reserved5;
149 u32 reserved6;
150};
151
152/* Static BDs - driver uses only one BD */
153static struct axidma_bd tx_bd __attribute((aligned(DMAALIGN)));
154static struct axidma_bd rx_bd __attribute((aligned(DMAALIGN)));
155
156struct axi_regs {
157 u32 reserved[3];
158 u32 is; /* 0xC: Interrupt status */
159 u32 reserved2;
160 u32 ie; /* 0x14: Interrupt enable */
161 u32 reserved3[251];
162 u32 rcw1; /* 0x404: Rx Configuration Word 1 */
163 u32 tc; /* 0x408: Tx Configuration */
164 u32 reserved4;
165 u32 emmc; /* 0x410: EMAC mode configuration */
166 u32 reserved5[59];
167 u32 mdio_mc; /* 0x500: MII Management Config */
168 u32 mdio_mcr; /* 0x504: MII Management Control */
169 u32 mdio_mwd; /* 0x508: MII Management Write Data */
170 u32 mdio_mrd; /* 0x50C: MII Management Read Data */
171 u32 reserved6[124];
172 u32 uaw0; /* 0x700: Unicast address word 0 */
173 u32 uaw1; /* 0x704: Unicast address word 1 */
174};
175
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600176struct xxv_axi_regs {
177 u32 gt_reset; /* 0x0 */
178 u32 reserved[2];
179 u32 tc; /* 0xC: Tx Configuration */
180 u32 reserved2;
181 u32 rcw1; /* 0x14: Rx Configuration Word 1 */
182};
183
Michal Simek6fc7c452011-10-06 20:35:35 +0000184/* Use MII register 1 (MII status register) to detect PHY */
185#define PHY_DETECT_REG 1
186
187/*
188 * Mask used to verify certain PHY features (or register contents)
189 * in the register above:
190 * 0x1000: 10Mbps full duplex support
191 * 0x0800: 10Mbps half duplex support
192 * 0x0008: Auto-negotiation support
193 */
194#define PHY_DETECT_MASK 0x1808
195
Michal Simekf5221872015-12-09 14:36:31 +0100196static inline int mdio_wait(struct axi_regs *regs)
Michal Simek6fc7c452011-10-06 20:35:35 +0000197{
Michal Simek6fc7c452011-10-06 20:35:35 +0000198 u32 timeout = 200;
199
200 /* Wait till MDIO interface is ready to accept a new transaction. */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530201 while (timeout && (!(readl(&regs->mdio_mcr)
Michal Simek6fc7c452011-10-06 20:35:35 +0000202 & XAE_MDIO_MCR_READY_MASK))) {
203 timeout--;
204 udelay(1);
205 }
206 if (!timeout) {
207 printf("%s: Timeout\n", __func__);
208 return 1;
209 }
210 return 0;
211}
212
Vipul Kumar3b06a0f2018-01-23 14:52:35 +0530213/**
214 * axienet_dma_write - Memory mapped Axi DMA register Buffer Descriptor write.
215 * @bd: pointer to BD descriptor structure
216 * @desc: Address offset of DMA descriptors
217 *
218 * This function writes the value into the corresponding Axi DMA register.
219 */
220static inline void axienet_dma_write(struct axidma_bd *bd, u32 *desc)
221{
222#if defined(CONFIG_PHYS_64BIT)
Ashok Reddy Soma91d455a2020-09-03 08:36:43 -0600223 writeq((unsigned long)bd, desc);
Vipul Kumar3b06a0f2018-01-23 14:52:35 +0530224#else
225 writel((u32)bd, desc);
226#endif
227}
228
Michal Simek41beca12015-12-09 14:44:38 +0100229static u32 phyread(struct axidma_priv *priv, u32 phyaddress, u32 registernum,
230 u16 *val)
Michal Simek6fc7c452011-10-06 20:35:35 +0000231{
Michal Simek41beca12015-12-09 14:44:38 +0100232 struct axi_regs *regs = priv->iobase;
Michal Simek6fc7c452011-10-06 20:35:35 +0000233 u32 mdioctrlreg = 0;
234
Michal Simekf5221872015-12-09 14:36:31 +0100235 if (mdio_wait(regs))
Michal Simek6fc7c452011-10-06 20:35:35 +0000236 return 1;
237
238 mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) &
239 XAE_MDIO_MCR_PHYAD_MASK) |
240 ((registernum << XAE_MDIO_MCR_REGAD_SHIFT)
241 & XAE_MDIO_MCR_REGAD_MASK) |
242 XAE_MDIO_MCR_INITIATE_MASK |
243 XAE_MDIO_MCR_OP_READ_MASK;
244
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530245 writel(mdioctrlreg, &regs->mdio_mcr);
Michal Simek6fc7c452011-10-06 20:35:35 +0000246
Michal Simekf5221872015-12-09 14:36:31 +0100247 if (mdio_wait(regs))
Michal Simek6fc7c452011-10-06 20:35:35 +0000248 return 1;
249
250 /* Read data */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530251 *val = readl(&regs->mdio_mrd);
Michal Simek6fc7c452011-10-06 20:35:35 +0000252 return 0;
253}
254
Michal Simek41beca12015-12-09 14:44:38 +0100255static u32 phywrite(struct axidma_priv *priv, u32 phyaddress, u32 registernum,
256 u32 data)
Michal Simek6fc7c452011-10-06 20:35:35 +0000257{
Michal Simek41beca12015-12-09 14:44:38 +0100258 struct axi_regs *regs = priv->iobase;
Michal Simek6fc7c452011-10-06 20:35:35 +0000259 u32 mdioctrlreg = 0;
260
Michal Simekf5221872015-12-09 14:36:31 +0100261 if (mdio_wait(regs))
Michal Simek6fc7c452011-10-06 20:35:35 +0000262 return 1;
263
264 mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) &
265 XAE_MDIO_MCR_PHYAD_MASK) |
266 ((registernum << XAE_MDIO_MCR_REGAD_SHIFT)
267 & XAE_MDIO_MCR_REGAD_MASK) |
268 XAE_MDIO_MCR_INITIATE_MASK |
269 XAE_MDIO_MCR_OP_WRITE_MASK;
270
271 /* Write data */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530272 writel(data, &regs->mdio_mwd);
Michal Simek6fc7c452011-10-06 20:35:35 +0000273
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530274 writel(mdioctrlreg, &regs->mdio_mcr);
Michal Simek6fc7c452011-10-06 20:35:35 +0000275
Michal Simekf5221872015-12-09 14:36:31 +0100276 if (mdio_wait(regs))
Michal Simek6fc7c452011-10-06 20:35:35 +0000277 return 1;
278
279 return 0;
280}
281
Michal Simek0b13ee22015-12-08 16:10:05 +0100282static int axiemac_phy_init(struct udevice *dev)
Michal Simek6fc7c452011-10-06 20:35:35 +0000283{
284 u16 phyreg;
Patrick van Gelder70ab9432020-06-03 14:18:04 +0200285 int i;
286 u32 ret;
Michal Simek682baac2015-12-08 15:44:41 +0100287 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek6cb55e72015-12-09 14:39:42 +0100288 struct axi_regs *regs = priv->iobase;
Michal Simek6fc7c452011-10-06 20:35:35 +0000289 struct phy_device *phydev;
290
291 u32 supported = SUPPORTED_10baseT_Half |
292 SUPPORTED_10baseT_Full |
293 SUPPORTED_100baseT_Half |
294 SUPPORTED_100baseT_Full |
295 SUPPORTED_1000baseT_Half |
296 SUPPORTED_1000baseT_Full;
297
Michal Simek0b13ee22015-12-08 16:10:05 +0100298 /* Set default MDIO divisor */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530299 writel(XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK, &regs->mdio_mc);
Michal Simek0b13ee22015-12-08 16:10:05 +0100300
T Karthik Reddy4a3f40b2022-05-10 13:26:09 +0200301 if (IS_ENABLED(CONFIG_DM_ETH_PHY))
302 priv->phyaddr = eth_phy_get_addr(dev);
303
Michal Simek6fc7c452011-10-06 20:35:35 +0000304 if (priv->phyaddr == -1) {
305 /* Detect the PHY address */
306 for (i = 31; i >= 0; i--) {
Michal Simek41beca12015-12-09 14:44:38 +0100307 ret = phyread(priv, i, PHY_DETECT_REG, &phyreg);
Michal Simek6fc7c452011-10-06 20:35:35 +0000308 if (!ret && (phyreg != 0xFFFF) &&
309 ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
310 /* Found a valid PHY address */
311 priv->phyaddr = i;
312 debug("axiemac: Found valid phy address, %x\n",
Michal Simek2f1e0652015-12-09 10:54:53 +0100313 i);
Michal Simek6fc7c452011-10-06 20:35:35 +0000314 break;
315 }
316 }
317 }
318
319 /* Interface - look at tsec */
Siva Durga Prasad Paladugu49348512016-02-21 15:46:14 +0530320 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
Venkatesh Yadav Abbarapu1bbe4502022-09-29 10:26:05 +0530321 if (IS_ERR_OR_NULL(phydev)) {
322 dev_err(dev, "phy_connect() failed\n");
323 return -ENODEV;
324 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000325
326 phydev->supported &= supported;
327 phydev->advertising = phydev->supported;
328 priv->phydev = phydev;
Siva Durga Prasad Paladuguc3d94f62019-03-15 17:46:45 +0530329 if (priv->phy_of_handle)
330 priv->phydev->node = offset_to_ofnode(priv->phy_of_handle);
Michal Simek6fc7c452011-10-06 20:35:35 +0000331 phy_config(phydev);
Michal Simek0b13ee22015-12-08 16:10:05 +0100332
333 return 0;
334}
335
336/* Setting axi emac and phy to proper setting */
337static int setup_phy(struct udevice *dev)
338{
Siva Durga Prasad Paladugua1c6ed82016-02-21 15:46:15 +0530339 u16 temp;
340 u32 speed, emmc_reg, ret;
Michal Simek0b13ee22015-12-08 16:10:05 +0100341 struct axidma_priv *priv = dev_get_priv(dev);
342 struct axi_regs *regs = priv->iobase;
343 struct phy_device *phydev = priv->phydev;
344
Siva Durga Prasad Paladugua1c6ed82016-02-21 15:46:15 +0530345 if (priv->interface == PHY_INTERFACE_MODE_SGMII) {
346 /*
347 * In SGMII cases the isolate bit might set
348 * after DMA and ethernet resets and hence
349 * check and clear if set.
350 */
351 ret = phyread(priv, priv->phyaddr, MII_BMCR, &temp);
352 if (ret)
353 return 0;
354 if (temp & BMCR_ISOLATE) {
355 temp &= ~BMCR_ISOLATE;
356 ret = phywrite(priv, priv->phyaddr, MII_BMCR, temp);
357 if (ret)
358 return 0;
359 }
360 }
361
Timur Tabi42387462012-07-09 08:52:43 +0000362 if (phy_startup(phydev)) {
363 printf("axiemac: could not initialize PHY %s\n",
364 phydev->dev->name);
365 return 0;
366 }
Michal Simek5848f132013-11-21 16:15:51 +0100367 if (!phydev->link) {
368 printf("%s: No link.\n", phydev->dev->name);
369 return 0;
370 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000371
372 switch (phydev->speed) {
373 case 1000:
374 speed = XAE_EMMC_LINKSPD_1000;
375 break;
376 case 100:
377 speed = XAE_EMMC_LINKSPD_100;
378 break;
379 case 10:
380 speed = XAE_EMMC_LINKSPD_10;
381 break;
382 default:
383 return 0;
384 }
385
386 /* Setup the emac for the phy speed */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530387 emmc_reg = readl(&regs->emmc);
Michal Simek6fc7c452011-10-06 20:35:35 +0000388 emmc_reg &= ~XAE_EMMC_LINKSPEED_MASK;
389 emmc_reg |= speed;
390
391 /* Write new speed setting out to Axi Ethernet */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530392 writel(emmc_reg, &regs->emmc);
Michal Simek6fc7c452011-10-06 20:35:35 +0000393
394 /*
395 * Setting the operating speed of the MAC needs a delay. There
396 * doesn't seem to be register to poll, so please consider this
397 * during your application design.
398 */
399 udelay(1);
400
401 return 1;
402}
403
404/* STOP DMA transfers */
Michal Simek8fbf79f2015-12-16 09:18:12 +0100405static void axiemac_stop(struct udevice *dev)
Michal Simek6fc7c452011-10-06 20:35:35 +0000406{
Michal Simek682baac2015-12-08 15:44:41 +0100407 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek6fc7c452011-10-06 20:35:35 +0000408 u32 temp;
409
410 /* Stop the hardware */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530411 temp = readl(&priv->dmatx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000412 temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530413 writel(temp, &priv->dmatx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000414
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530415 temp = readl(&priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000416 temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530417 writel(temp, &priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000418
419 debug("axiemac: Halted\n");
420}
421
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600422static int xxv_axi_ethernet_init(struct axidma_priv *priv)
423{
424 struct xxv_axi_regs *regs = (struct xxv_axi_regs *)priv->iobase;
425
426 writel(readl(&regs->rcw1) | XXV_RCW1_FCS_MASK, &regs->rcw1);
427 writel(readl(&regs->tc) | XXV_TC_FCS_MASK, &regs->tc);
428 writel(readl(&regs->tc) | XXV_TC_TX_MASK, &regs->tc);
429 writel(readl(&regs->rcw1) | XXV_RCW1_RX_MASK, &regs->rcw1);
430
431 return 0;
432}
433
Michal Simek638c0ef2015-12-09 14:53:51 +0100434static int axi_ethernet_init(struct axidma_priv *priv)
Michal Simek6fc7c452011-10-06 20:35:35 +0000435{
Michal Simek638c0ef2015-12-09 14:53:51 +0100436 struct axi_regs *regs = priv->iobase;
Siva Durga Prasad Paladugu20633622017-01-06 16:18:50 +0530437 int err;
Michal Simek6fc7c452011-10-06 20:35:35 +0000438
439 /*
440 * Check the status of the MgtRdy bit in the interrupt status
441 * registers. This must be done to allow the MGT clock to become stable
442 * for the Sgmii and 1000BaseX PHY interfaces. No other register reads
443 * will be valid until this bit is valid.
444 * The bit is always a 1 for all other PHY interfaces.
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530445 * Interrupt status and enable registers are not available in non
446 * processor mode and hence bypass in this mode
Michal Simek6fc7c452011-10-06 20:35:35 +0000447 */
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530448 if (!priv->eth_hasnobuf) {
Álvaro Fernåndez Rojas918de032018-01-23 17:14:55 +0100449 err = wait_for_bit_le32(&regs->is, XAE_INT_MGTRDY_MASK,
450 true, 200, false);
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530451 if (err) {
452 printf("%s: Timeout\n", __func__);
453 return 1;
454 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000455
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530456 /*
457 * Stop the device and reset HW
458 * Disable interrupts
459 */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530460 writel(0, &regs->ie);
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530461 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000462
463 /* Disable the receiver */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530464 writel(readl(&regs->rcw1) & ~XAE_RCW1_RX_MASK, &regs->rcw1);
Michal Simek6fc7c452011-10-06 20:35:35 +0000465
466 /*
467 * Stopping the receiver in mid-packet causes a dropped packet
468 * indication from HW. Clear it.
469 */
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530470 if (!priv->eth_hasnobuf) {
471 /* Set the interrupt status register to clear the interrupt */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530472 writel(XAE_INT_RXRJECT_MASK, &regs->is);
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530473 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000474
475 /* Setup HW */
476 /* Set default MDIO divisor */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530477 writel(XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK, &regs->mdio_mc);
Michal Simek6fc7c452011-10-06 20:35:35 +0000478
479 debug("axiemac: InitHw done\n");
480 return 0;
481}
482
Michal Simek8fbf79f2015-12-16 09:18:12 +0100483static int axiemac_write_hwaddr(struct udevice *dev)
Michal Simek6fc7c452011-10-06 20:35:35 +0000484{
Simon Glassfa20e932020-12-03 16:55:20 -0700485 struct eth_pdata *pdata = dev_get_plat(dev);
Michal Simek682baac2015-12-08 15:44:41 +0100486 struct axidma_priv *priv = dev_get_priv(dev);
487 struct axi_regs *regs = priv->iobase;
Michal Simek6fc7c452011-10-06 20:35:35 +0000488
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600489 if (priv->mactype != EMAC_1G)
490 return 0;
491
Michal Simek6fc7c452011-10-06 20:35:35 +0000492 /* Set the MAC address */
Michal Simek682baac2015-12-08 15:44:41 +0100493 int val = ((pdata->enetaddr[3] << 24) | (pdata->enetaddr[2] << 16) |
494 (pdata->enetaddr[1] << 8) | (pdata->enetaddr[0]));
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530495 writel(val, &regs->uaw0);
Michal Simek6fc7c452011-10-06 20:35:35 +0000496
Michal Simek682baac2015-12-08 15:44:41 +0100497 val = (pdata->enetaddr[5] << 8) | pdata->enetaddr[4];
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530498 val |= readl(&regs->uaw1) & ~XAE_UAW1_UNICASTADDR_MASK;
499 writel(val, &regs->uaw1);
Michal Simek6fc7c452011-10-06 20:35:35 +0000500 return 0;
501}
502
503/* Reset DMA engine */
Michal Simek638c0ef2015-12-09 14:53:51 +0100504static void axi_dma_init(struct axidma_priv *priv)
Michal Simek6fc7c452011-10-06 20:35:35 +0000505{
Michal Simek6fc7c452011-10-06 20:35:35 +0000506 u32 timeout = 500;
507
508 /* Reset the engine so the hardware starts from a known state */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530509 writel(XAXIDMA_CR_RESET_MASK, &priv->dmatx->control);
510 writel(XAXIDMA_CR_RESET_MASK, &priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000511
512 /* At the initialization time, hardware should finish reset quickly */
513 while (timeout--) {
514 /* Check transmit/receive channel */
515 /* Reset is done when the reset bit is low */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530516 if (!((readl(&priv->dmatx->control) |
517 readl(&priv->dmarx->control))
Michal Simek5aa45392015-10-28 11:00:47 +0100518 & XAXIDMA_CR_RESET_MASK)) {
Michal Simek6fc7c452011-10-06 20:35:35 +0000519 break;
520 }
521 }
522 if (!timeout)
523 printf("%s: Timeout\n", __func__);
524}
525
Michal Simek8fbf79f2015-12-16 09:18:12 +0100526static int axiemac_start(struct udevice *dev)
Michal Simek6fc7c452011-10-06 20:35:35 +0000527{
Michal Simek682baac2015-12-08 15:44:41 +0100528 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek6fc7c452011-10-06 20:35:35 +0000529 u32 temp;
530
531 debug("axiemac: Init started\n");
532 /*
533 * Initialize AXIDMA engine. AXIDMA engine must be initialized before
534 * AxiEthernet. During AXIDMA engine initialization, AXIDMA hardware is
535 * reset, and since AXIDMA reset line is connected to AxiEthernet, this
536 * would ensure a reset of AxiEthernet.
537 */
Michal Simek638c0ef2015-12-09 14:53:51 +0100538 axi_dma_init(priv);
Michal Simek6fc7c452011-10-06 20:35:35 +0000539
540 /* Initialize AxiEthernet hardware. */
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600541 if (priv->mactype == EMAC_1G) {
542 if (axi_ethernet_init(priv))
543 return -1;
544 } else {
545 if (xxv_axi_ethernet_init(priv))
546 return -1;
547 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000548
549 /* Disable all RX interrupts before RxBD space setup */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530550 temp = readl(&priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000551 temp &= ~XAXIDMA_IRQ_ALL_MASK;
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530552 writel(temp, &priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000553
554 /* Start DMA RX channel. Now it's ready to receive data.*/
Vipul Kumar3b06a0f2018-01-23 14:52:35 +0530555 axienet_dma_write(&rx_bd, &priv->dmarx->current);
Michal Simek6fc7c452011-10-06 20:35:35 +0000556
557 /* Setup the BD. */
558 memset(&rx_bd, 0, sizeof(rx_bd));
Ashok Reddy Soma91d455a2020-09-03 08:36:43 -0600559 rx_bd.next_desc = lower_32_bits((unsigned long)&rx_bd);
560 rx_bd.buf_addr = lower_32_bits((unsigned long)&rxframe);
561#if defined(CONFIG_PHYS_64BIT)
562 rx_bd.next_desc_msb = upper_32_bits((unsigned long)&rx_bd);
563 rx_bd.buf_addr_msb = upper_32_bits((unsigned long)&rxframe);
564#endif
Michal Simek6fc7c452011-10-06 20:35:35 +0000565 rx_bd.cntrl = sizeof(rxframe);
566 /* Flush the last BD so DMA core could see the updates */
Ashok Reddy Somaf29a5702020-09-03 08:36:44 -0600567 flush_cache((phys_addr_t)&rx_bd, sizeof(rx_bd));
Michal Simek6fc7c452011-10-06 20:35:35 +0000568
569 /* It is necessary to flush rxframe because if you don't do it
570 * then cache can contain uninitialized data */
Ashok Reddy Somaf29a5702020-09-03 08:36:44 -0600571 flush_cache((phys_addr_t)&rxframe, sizeof(rxframe));
Michal Simek6fc7c452011-10-06 20:35:35 +0000572
573 /* Start the hardware */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530574 temp = readl(&priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000575 temp |= XAXIDMA_CR_RUNSTOP_MASK;
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530576 writel(temp, &priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000577
578 /* Rx BD is ready - start */
Vipul Kumar3b06a0f2018-01-23 14:52:35 +0530579 axienet_dma_write(&rx_bd, &priv->dmarx->tail);
Michal Simek6fc7c452011-10-06 20:35:35 +0000580
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600581 if (priv->mactype == EMAC_1G) {
582 struct axi_regs *regs = priv->iobase;
583 /* Enable TX */
584 writel(XAE_TC_TX_MASK, &regs->tc);
585 /* Enable RX */
586 writel(XAE_RCW1_RX_MASK, &regs->rcw1);
Michal Simek6fc7c452011-10-06 20:35:35 +0000587
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600588 /* PHY setup */
589 if (!setup_phy(dev)) {
590 axiemac_stop(dev);
591 return -1;
592 }
593 } else {
594 struct xxv_axi_regs *regs = (struct xxv_axi_regs *)priv->iobase;
595 /* Enable TX */
596 writel(readl(&regs->tc) | XXV_TC_TX_MASK, &regs->tc);
597
598 /* Enable RX */
599 writel(readl(&regs->rcw1) | XXV_RCW1_RX_MASK, &regs->rcw1);
Michal Simek6fc7c452011-10-06 20:35:35 +0000600 }
601
602 debug("axiemac: Init complete\n");
603 return 0;
604}
605
Michal Simek682baac2015-12-08 15:44:41 +0100606static int axiemac_send(struct udevice *dev, void *ptr, int len)
Michal Simek6fc7c452011-10-06 20:35:35 +0000607{
Michal Simek682baac2015-12-08 15:44:41 +0100608 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek6fc7c452011-10-06 20:35:35 +0000609 u32 timeout;
610
611 if (len > PKTSIZE_ALIGN)
612 len = PKTSIZE_ALIGN;
613
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600614 /* If size is less than min packet size, pad to min size */
615 if (priv->mactype == EMAC_10G_25G && len < XXV_MIN_PKT_SIZE) {
616 memset(txminframe, 0, XXV_MIN_PKT_SIZE);
617 memcpy(txminframe, ptr, len);
618 len = XXV_MIN_PKT_SIZE;
619 ptr = txminframe;
620 }
621
Michal Simek6fc7c452011-10-06 20:35:35 +0000622 /* Flush packet to main memory to be trasfered by DMA */
Ashok Reddy Somaf29a5702020-09-03 08:36:44 -0600623 flush_cache((phys_addr_t)ptr, len);
Michal Simek6fc7c452011-10-06 20:35:35 +0000624
625 /* Setup Tx BD */
626 memset(&tx_bd, 0, sizeof(tx_bd));
627 /* At the end of the ring, link the last BD back to the top */
Ashok Reddy Soma91d455a2020-09-03 08:36:43 -0600628 tx_bd.next_desc = lower_32_bits((unsigned long)&tx_bd);
629 tx_bd.buf_addr = lower_32_bits((unsigned long)ptr);
630#if defined(CONFIG_PHYS_64BIT)
631 tx_bd.next_desc_msb = upper_32_bits((unsigned long)&tx_bd);
632 tx_bd.buf_addr_msb = upper_32_bits((unsigned long)ptr);
633#endif
Michal Simek6fc7c452011-10-06 20:35:35 +0000634 /* Save len */
635 tx_bd.cntrl = len | XAXIDMA_BD_CTRL_TXSOF_MASK |
636 XAXIDMA_BD_CTRL_TXEOF_MASK;
637
638 /* Flush the last BD so DMA core could see the updates */
Ashok Reddy Somaf29a5702020-09-03 08:36:44 -0600639 flush_cache((phys_addr_t)&tx_bd, sizeof(tx_bd));
Michal Simek6fc7c452011-10-06 20:35:35 +0000640
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530641 if (readl(&priv->dmatx->status) & XAXIDMA_HALTED_MASK) {
Michal Simek6fc7c452011-10-06 20:35:35 +0000642 u32 temp;
Vipul Kumar3b06a0f2018-01-23 14:52:35 +0530643 axienet_dma_write(&tx_bd, &priv->dmatx->current);
Michal Simek6fc7c452011-10-06 20:35:35 +0000644 /* Start the hardware */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530645 temp = readl(&priv->dmatx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000646 temp |= XAXIDMA_CR_RUNSTOP_MASK;
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530647 writel(temp, &priv->dmatx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000648 }
649
650 /* Start transfer */
Vipul Kumar3b06a0f2018-01-23 14:52:35 +0530651 axienet_dma_write(&tx_bd, &priv->dmatx->tail);
Michal Simek6fc7c452011-10-06 20:35:35 +0000652
653 /* Wait for transmission to complete */
654 debug("axiemac: Waiting for tx to be done\n");
655 timeout = 200;
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530656 while (timeout && (!(readl(&priv->dmatx->status) &
Michal Simek5aa45392015-10-28 11:00:47 +0100657 (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))) {
Michal Simek6fc7c452011-10-06 20:35:35 +0000658 timeout--;
659 udelay(1);
660 }
661 if (!timeout) {
662 printf("%s: Timeout\n", __func__);
663 return 1;
664 }
665
666 debug("axiemac: Sending complete\n");
667 return 0;
668}
669
Michal Simek638c0ef2015-12-09 14:53:51 +0100670static int isrxready(struct axidma_priv *priv)
Michal Simek6fc7c452011-10-06 20:35:35 +0000671{
672 u32 status;
Michal Simek6fc7c452011-10-06 20:35:35 +0000673
674 /* Read pending interrupts */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530675 status = readl(&priv->dmarx->status);
Michal Simek6fc7c452011-10-06 20:35:35 +0000676
677 /* Acknowledge pending interrupts */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530678 writel(status & XAXIDMA_IRQ_ALL_MASK, &priv->dmarx->status);
Michal Simek6fc7c452011-10-06 20:35:35 +0000679
680 /*
681 * If Reception done interrupt is asserted, call RX call back function
682 * to handle the processed BDs and then raise the according flag.
683 */
684 if ((status & (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))
685 return 1;
686
687 return 0;
688}
689
Michal Simek682baac2015-12-08 15:44:41 +0100690static int axiemac_recv(struct udevice *dev, int flags, uchar **packetp)
Michal Simek6fc7c452011-10-06 20:35:35 +0000691{
692 u32 length;
Michal Simek682baac2015-12-08 15:44:41 +0100693 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek6fc7c452011-10-06 20:35:35 +0000694 u32 temp;
695
696 /* Wait for an incoming packet */
Michal Simek638c0ef2015-12-09 14:53:51 +0100697 if (!isrxready(priv))
Michal Simek682baac2015-12-08 15:44:41 +0100698 return -1;
Michal Simek6fc7c452011-10-06 20:35:35 +0000699
700 debug("axiemac: RX data ready\n");
701
702 /* Disable IRQ for a moment till packet is handled */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530703 temp = readl(&priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000704 temp &= ~XAXIDMA_IRQ_ALL_MASK;
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530705 writel(temp, &priv->dmarx->control);
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600706 if (!priv->eth_hasnobuf && priv->mactype == EMAC_1G)
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530707 length = rx_bd.app4 & 0xFFFF; /* max length mask */
708 else
709 length = rx_bd.status & XAXIDMA_BD_STS_ACTUAL_LEN_MASK;
Michal Simek6fc7c452011-10-06 20:35:35 +0000710
Michal Simek6fc7c452011-10-06 20:35:35 +0000711#ifdef DEBUG
712 print_buffer(&rxframe, &rxframe[0], 1, length, 16);
713#endif
Michal Simek0305be82015-12-09 14:13:23 +0100714
715 *packetp = rxframe;
716 return length;
717}
718
719static int axiemac_free_pkt(struct udevice *dev, uchar *packet, int length)
720{
721 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek6fc7c452011-10-06 20:35:35 +0000722
723#ifdef DEBUG
724 /* It is useful to clear buffer to be sure that it is consistent */
725 memset(rxframe, 0, sizeof(rxframe));
726#endif
727 /* Setup RxBD */
728 /* Clear the whole buffer and setup it again - all flags are cleared */
729 memset(&rx_bd, 0, sizeof(rx_bd));
Ashok Reddy Soma91d455a2020-09-03 08:36:43 -0600730 rx_bd.next_desc = lower_32_bits((unsigned long)&rx_bd);
731 rx_bd.buf_addr = lower_32_bits((unsigned long)&rxframe);
732#if defined(CONFIG_PHYS_64BIT)
733 rx_bd.next_desc_msb = upper_32_bits((unsigned long)&rx_bd);
734 rx_bd.buf_addr_msb = upper_32_bits((unsigned long)&rxframe);
735#endif
Michal Simek6fc7c452011-10-06 20:35:35 +0000736 rx_bd.cntrl = sizeof(rxframe);
737
738 /* Write bd to HW */
Ashok Reddy Somaf29a5702020-09-03 08:36:44 -0600739 flush_cache((phys_addr_t)&rx_bd, sizeof(rx_bd));
Michal Simek6fc7c452011-10-06 20:35:35 +0000740
741 /* It is necessary to flush rxframe because if you don't do it
742 * then cache will contain previous packet */
Ashok Reddy Somaf29a5702020-09-03 08:36:44 -0600743 flush_cache((phys_addr_t)&rxframe, sizeof(rxframe));
Michal Simek6fc7c452011-10-06 20:35:35 +0000744
745 /* Rx BD is ready - start again */
Vipul Kumar3b06a0f2018-01-23 14:52:35 +0530746 axienet_dma_write(&rx_bd, &priv->dmarx->tail);
Michal Simek6fc7c452011-10-06 20:35:35 +0000747
748 debug("axiemac: RX completed, framelength = %d\n", length);
749
Michal Simek682baac2015-12-08 15:44:41 +0100750 return 0;
Michal Simek6fc7c452011-10-06 20:35:35 +0000751}
752
Michal Simek682baac2015-12-08 15:44:41 +0100753static int axiemac_miiphy_read(struct mii_dev *bus, int addr,
754 int devad, int reg)
Michal Simek6fc7c452011-10-06 20:35:35 +0000755{
Michal Simek682baac2015-12-08 15:44:41 +0100756 int ret;
757 u16 value;
Michal Simek6fc7c452011-10-06 20:35:35 +0000758
Michal Simek682baac2015-12-08 15:44:41 +0100759 ret = phyread(bus->priv, addr, reg, &value);
760 debug("axiemac: Read MII 0x%x, 0x%x, 0x%x, %d\n", addr, reg,
761 value, ret);
762 return value;
Michal Simek6fc7c452011-10-06 20:35:35 +0000763}
764
Michal Simek682baac2015-12-08 15:44:41 +0100765static int axiemac_miiphy_write(struct mii_dev *bus, int addr, int devad,
766 int reg, u16 value)
Michal Simek6fc7c452011-10-06 20:35:35 +0000767{
Michal Simek682baac2015-12-08 15:44:41 +0100768 debug("axiemac: Write MII 0x%x, 0x%x, 0x%x\n", addr, reg, value);
769 return phywrite(bus->priv, addr, reg, value);
Michal Simek6fc7c452011-10-06 20:35:35 +0000770}
771
Michal Simek682baac2015-12-08 15:44:41 +0100772static int axi_emac_probe(struct udevice *dev)
Michal Simek6fc7c452011-10-06 20:35:35 +0000773{
Ashok Reddy Soma3c4df952021-06-24 00:34:40 -0600774 struct axidma_plat *plat = dev_get_plat(dev);
775 struct eth_pdata *pdata = &plat->eth_pdata;
Michal Simek682baac2015-12-08 15:44:41 +0100776 struct axidma_priv *priv = dev_get_priv(dev);
777 int ret;
778
Ashok Reddy Soma3c4df952021-06-24 00:34:40 -0600779 priv->iobase = (struct axi_regs *)pdata->iobase;
780 priv->dmatx = plat->dmatx;
781 /* RX channel offset is 0x30 */
782 priv->dmarx = (struct axidma_reg *)((phys_addr_t)priv->dmatx + 0x30);
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600783 priv->mactype = plat->mactype;
Ashok Reddy Soma3c4df952021-06-24 00:34:40 -0600784
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600785 if (priv->mactype == EMAC_1G) {
786 priv->eth_hasnobuf = plat->eth_hasnobuf;
787 priv->phyaddr = plat->phyaddr;
788 priv->phy_of_handle = plat->phy_of_handle;
789 priv->interface = pdata->phy_interface;
Michal Simek682baac2015-12-08 15:44:41 +0100790
T Karthik Reddy4a3f40b2022-05-10 13:26:09 +0200791 if (IS_ENABLED(CONFIG_DM_ETH_PHY))
792 priv->bus = eth_phy_get_mdio_bus(dev);
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600793
T Karthik Reddy4a3f40b2022-05-10 13:26:09 +0200794 if (!priv->bus) {
795 priv->bus = mdio_alloc();
796 priv->bus->read = axiemac_miiphy_read;
797 priv->bus->write = axiemac_miiphy_write;
798 priv->bus->priv = priv;
799
800 ret = mdio_register_seq(priv->bus, dev_seq(dev));
801 if (ret)
802 return ret;
803 }
804
805 if (IS_ENABLED(CONFIG_DM_ETH_PHY))
806 eth_phy_set_mdio_bus(dev, priv->bus);
Michal Simek682baac2015-12-08 15:44:41 +0100807
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600808 axiemac_phy_init(dev);
809 }
Michal Simek0b13ee22015-12-08 16:10:05 +0100810
T Karthik Reddy4a3f40b2022-05-10 13:26:09 +0200811 printf("AXI EMAC: %lx, phyaddr %d, interface %s\n", (ulong)pdata->iobase,
812 priv->phyaddr, phy_string_for_interface(pdata->phy_interface));
813
Michal Simek6fc7c452011-10-06 20:35:35 +0000814 return 0;
815}
816
Michal Simek682baac2015-12-08 15:44:41 +0100817static int axi_emac_remove(struct udevice *dev)
Michal Simek6fc7c452011-10-06 20:35:35 +0000818{
Michal Simek682baac2015-12-08 15:44:41 +0100819 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek6fc7c452011-10-06 20:35:35 +0000820
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600821 if (priv->mactype == EMAC_1G) {
822 free(priv->phydev);
823 mdio_unregister(priv->bus);
824 mdio_free(priv->bus);
825 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000826
Michal Simek682baac2015-12-08 15:44:41 +0100827 return 0;
828}
829
830static const struct eth_ops axi_emac_ops = {
Michal Simek8fbf79f2015-12-16 09:18:12 +0100831 .start = axiemac_start,
Michal Simek682baac2015-12-08 15:44:41 +0100832 .send = axiemac_send,
833 .recv = axiemac_recv,
Michal Simek0305be82015-12-09 14:13:23 +0100834 .free_pkt = axiemac_free_pkt,
Michal Simek8fbf79f2015-12-16 09:18:12 +0100835 .stop = axiemac_stop,
836 .write_hwaddr = axiemac_write_hwaddr,
Michal Simek682baac2015-12-08 15:44:41 +0100837};
838
Simon Glassaad29ae2020-12-03 16:55:21 -0700839static int axi_emac_of_to_plat(struct udevice *dev)
Michal Simek682baac2015-12-08 15:44:41 +0100840{
Ashok Reddy Soma3c4df952021-06-24 00:34:40 -0600841 struct axidma_plat *plat = dev_get_plat(dev);
842 struct eth_pdata *pdata = &plat->eth_pdata;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700843 int node = dev_of_offset(dev);
Michal Simek682baac2015-12-08 15:44:41 +0100844 int offset = 0;
Michal Simek6fc7c452011-10-06 20:35:35 +0000845
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900846 pdata->iobase = dev_read_addr(dev);
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600847 plat->mactype = dev_get_driver_data(dev);
Michal Simek6fc7c452011-10-06 20:35:35 +0000848
Simon Glassdd79d6e2017-01-17 16:52:55 -0700849 offset = fdtdec_lookup_phandle(gd->fdt_blob, node,
Michal Simek682baac2015-12-08 15:44:41 +0100850 "axistream-connected");
851 if (offset <= 0) {
852 printf("%s: axistream is not found\n", __func__);
853 return -EINVAL;
854 }
Greentime Hue50c9ea2022-01-20 16:41:28 +0800855 plat->dmatx = (struct axidma_reg *)fdtdec_get_addr_size_auto_parent
856 (gd->fdt_blob, 0, offset, "reg", 0, NULL, false);
Ashok Reddy Soma3c4df952021-06-24 00:34:40 -0600857 if (!plat->dmatx) {
Michal Simek682baac2015-12-08 15:44:41 +0100858 printf("%s: axi_dma register space not found\n", __func__);
859 return -EINVAL;
860 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000861
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600862 if (plat->mactype == EMAC_1G) {
863 plat->phyaddr = -1;
Michal Simek6fc7c452011-10-06 20:35:35 +0000864
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600865 offset = fdtdec_lookup_phandle(gd->fdt_blob, node,
866 "phy-handle");
867 if (offset > 0) {
T Karthik Reddy4a3f40b2022-05-10 13:26:09 +0200868 if (!(IS_ENABLED(CONFIG_DM_ETH_PHY)))
869 plat->phyaddr = fdtdec_get_int(gd->fdt_blob,
870 offset,
871 "reg", -1);
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600872 plat->phy_of_handle = offset;
873 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000874
Marek BehĂșnbc194772022-04-07 00:33:01 +0200875 pdata->phy_interface = dev_read_phy_mode(dev);
Marek BehĂșn48631e42022-04-07 00:33:03 +0200876 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600877 return -EINVAL;
Michal Simek682baac2015-12-08 15:44:41 +0100878
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600879 plat->eth_hasnobuf = fdtdec_get_bool(gd->fdt_blob, node,
880 "xlnx,eth-hasnobuf");
881 }
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530882
Michal Simek682baac2015-12-08 15:44:41 +0100883 return 0;
Michal Simek6fc7c452011-10-06 20:35:35 +0000884}
Michal Simek682baac2015-12-08 15:44:41 +0100885
886static const struct udevice_id axi_emac_ids[] = {
Ashok Reddy Soma645aa762021-06-24 00:34:41 -0600887 { .compatible = "xlnx,axi-ethernet-1.00.a", .data = (uintptr_t)EMAC_1G },
888 { .compatible = "xlnx,xxv-ethernet-1.0", .data = (uintptr_t)EMAC_10G_25G },
Michal Simek682baac2015-12-08 15:44:41 +0100889 { }
890};
891
892U_BOOT_DRIVER(axi_emac) = {
893 .name = "axi_emac",
894 .id = UCLASS_ETH,
895 .of_match = axi_emac_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700896 .of_to_plat = axi_emac_of_to_plat,
Michal Simek682baac2015-12-08 15:44:41 +0100897 .probe = axi_emac_probe,
898 .remove = axi_emac_remove,
899 .ops = &axi_emac_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700900 .priv_auto = sizeof(struct axidma_priv),
Ashok Reddy Soma3c4df952021-06-24 00:34:40 -0600901 .plat_auto = sizeof(struct axidma_plat),
Michal Simek682baac2015-12-08 15:44:41 +0100902};