blob: 36d651109cbc6553a25c5a9f29927c95927a3254 [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/*
3 * Copyright (C) 2011 Michal Simek <monstr@monstr.eu>
4 * Copyright (C) 2011 PetaLogix
5 * Copyright (C) 2010 Xilinx, Inc. All rights reserved.
Michal Simek6fc7c452011-10-06 20:35:35 +00006 */
7
8#include <config.h>
9#include <common.h>
Michal Simek682baac2015-12-08 15:44:41 +010010#include <dm.h>
Michal Simek6fc7c452011-10-06 20:35:35 +000011#include <net.h>
12#include <malloc.h>
13#include <asm/io.h>
14#include <phy.h>
15#include <miiphy.h>
Siva Durga Prasad Paladugu20633622017-01-06 16:18:50 +053016#include <wait_bit.h>
Michal Simek6fc7c452011-10-06 20:35:35 +000017
Michal Simek682baac2015-12-08 15:44:41 +010018DECLARE_GLOBAL_DATA_PTR;
19
Michal Simek6fc7c452011-10-06 20:35:35 +000020/* Link setup */
21#define XAE_EMMC_LINKSPEED_MASK 0xC0000000 /* Link speed */
22#define XAE_EMMC_LINKSPD_10 0x00000000 /* Link Speed mask for 10 Mbit */
23#define XAE_EMMC_LINKSPD_100 0x40000000 /* Link Speed mask for 100 Mbit */
24#define XAE_EMMC_LINKSPD_1000 0x80000000 /* Link Speed mask for 1000 Mbit */
25
26/* Interrupt Status/Enable/Mask Registers bit definitions */
27#define XAE_INT_RXRJECT_MASK 0x00000008 /* Rx frame rejected */
28#define XAE_INT_MGTRDY_MASK 0x00000080 /* MGT clock Lock */
29
30/* Receive Configuration Word 1 (RCW1) Register bit definitions */
31#define XAE_RCW1_RX_MASK 0x10000000 /* Receiver enable */
32
33/* Transmitter Configuration (TC) Register bit definitions */
34#define XAE_TC_TX_MASK 0x10000000 /* Transmitter enable */
35
36#define XAE_UAW1_UNICASTADDR_MASK 0x0000FFFF
37
38/* MDIO Management Configuration (MC) Register bit definitions */
39#define XAE_MDIO_MC_MDIOEN_MASK 0x00000040 /* MII management enable*/
40
41/* MDIO Management Control Register (MCR) Register bit definitions */
42#define XAE_MDIO_MCR_PHYAD_MASK 0x1F000000 /* Phy Address Mask */
43#define XAE_MDIO_MCR_PHYAD_SHIFT 24 /* Phy Address Shift */
44#define XAE_MDIO_MCR_REGAD_MASK 0x001F0000 /* Reg Address Mask */
45#define XAE_MDIO_MCR_REGAD_SHIFT 16 /* Reg Address Shift */
46#define XAE_MDIO_MCR_OP_READ_MASK 0x00008000 /* Op Code Read Mask */
47#define XAE_MDIO_MCR_OP_WRITE_MASK 0x00004000 /* Op Code Write Mask */
48#define XAE_MDIO_MCR_INITIATE_MASK 0x00000800 /* Ready Mask */
49#define XAE_MDIO_MCR_READY_MASK 0x00000080 /* Ready Mask */
50
51#define XAE_MDIO_DIV_DFT 29 /* Default MDIO clock divisor */
52
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +053053#define XAXIDMA_BD_STS_ACTUAL_LEN_MASK 0x007FFFFF /* Actual len */
54
Michal Simek6fc7c452011-10-06 20:35:35 +000055/* DMA macros */
56/* Bitmasks of XAXIDMA_CR_OFFSET register */
57#define XAXIDMA_CR_RUNSTOP_MASK 0x00000001 /* Start/stop DMA channel */
58#define XAXIDMA_CR_RESET_MASK 0x00000004 /* Reset DMA engine */
59
60/* Bitmasks of XAXIDMA_SR_OFFSET register */
61#define XAXIDMA_HALTED_MASK 0x00000001 /* DMA channel halted */
62
63/* Bitmask for interrupts */
64#define XAXIDMA_IRQ_IOC_MASK 0x00001000 /* Completion intr */
65#define XAXIDMA_IRQ_DELAY_MASK 0x00002000 /* Delay interrupt */
66#define XAXIDMA_IRQ_ALL_MASK 0x00007000 /* All interrupts */
67
68/* Bitmasks of XAXIDMA_BD_CTRL_OFFSET register */
69#define XAXIDMA_BD_CTRL_TXSOF_MASK 0x08000000 /* First tx packet */
70#define XAXIDMA_BD_CTRL_TXEOF_MASK 0x04000000 /* Last tx packet */
71
72#define DMAALIGN 128
73
74static u8 rxframe[PKTSIZE_ALIGN] __attribute((aligned(DMAALIGN)));
75
76/* Reflect dma offsets */
77struct axidma_reg {
78 u32 control; /* DMACR */
79 u32 status; /* DMASR */
Vipul Kumar3b06a0f2018-01-23 14:52:35 +053080 u32 current; /* CURDESC low 32 bit */
81 u32 current_hi; /* CURDESC high 32 bit */
82 u32 tail; /* TAILDESC low 32 bit */
83 u32 tail_hi; /* TAILDESC high 32 bit */
Michal Simek6fc7c452011-10-06 20:35:35 +000084};
85
86/* Private driver structures */
87struct axidma_priv {
88 struct axidma_reg *dmatx;
89 struct axidma_reg *dmarx;
90 int phyaddr;
Michal Simek6cb55e72015-12-09 14:39:42 +010091 struct axi_regs *iobase;
Michal Simek682baac2015-12-08 15:44:41 +010092 phy_interface_t interface;
Michal Simek6fc7c452011-10-06 20:35:35 +000093 struct phy_device *phydev;
94 struct mii_dev *bus;
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +053095 u8 eth_hasnobuf;
Siva Durga Prasad Paladuguc3d94f62019-03-15 17:46:45 +053096 int phy_of_handle;
Michal Simek6fc7c452011-10-06 20:35:35 +000097};
98
99/* BD descriptors */
100struct axidma_bd {
101 u32 next; /* Next descriptor pointer */
102 u32 reserved1;
103 u32 phys; /* Buffer address */
104 u32 reserved2;
105 u32 reserved3;
106 u32 reserved4;
107 u32 cntrl; /* Control */
108 u32 status; /* Status */
109 u32 app0;
110 u32 app1; /* TX start << 16 | insert */
111 u32 app2; /* TX csum seed */
112 u32 app3;
113 u32 app4;
114 u32 sw_id_offset;
115 u32 reserved5;
116 u32 reserved6;
117};
118
119/* Static BDs - driver uses only one BD */
120static struct axidma_bd tx_bd __attribute((aligned(DMAALIGN)));
121static struct axidma_bd rx_bd __attribute((aligned(DMAALIGN)));
122
123struct axi_regs {
124 u32 reserved[3];
125 u32 is; /* 0xC: Interrupt status */
126 u32 reserved2;
127 u32 ie; /* 0x14: Interrupt enable */
128 u32 reserved3[251];
129 u32 rcw1; /* 0x404: Rx Configuration Word 1 */
130 u32 tc; /* 0x408: Tx Configuration */
131 u32 reserved4;
132 u32 emmc; /* 0x410: EMAC mode configuration */
133 u32 reserved5[59];
134 u32 mdio_mc; /* 0x500: MII Management Config */
135 u32 mdio_mcr; /* 0x504: MII Management Control */
136 u32 mdio_mwd; /* 0x508: MII Management Write Data */
137 u32 mdio_mrd; /* 0x50C: MII Management Read Data */
138 u32 reserved6[124];
139 u32 uaw0; /* 0x700: Unicast address word 0 */
140 u32 uaw1; /* 0x704: Unicast address word 1 */
141};
142
143/* Use MII register 1 (MII status register) to detect PHY */
144#define PHY_DETECT_REG 1
145
146/*
147 * Mask used to verify certain PHY features (or register contents)
148 * in the register above:
149 * 0x1000: 10Mbps full duplex support
150 * 0x0800: 10Mbps half duplex support
151 * 0x0008: Auto-negotiation support
152 */
153#define PHY_DETECT_MASK 0x1808
154
Michal Simekf5221872015-12-09 14:36:31 +0100155static inline int mdio_wait(struct axi_regs *regs)
Michal Simek6fc7c452011-10-06 20:35:35 +0000156{
Michal Simek6fc7c452011-10-06 20:35:35 +0000157 u32 timeout = 200;
158
159 /* Wait till MDIO interface is ready to accept a new transaction. */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530160 while (timeout && (!(readl(&regs->mdio_mcr)
Michal Simek6fc7c452011-10-06 20:35:35 +0000161 & XAE_MDIO_MCR_READY_MASK))) {
162 timeout--;
163 udelay(1);
164 }
165 if (!timeout) {
166 printf("%s: Timeout\n", __func__);
167 return 1;
168 }
169 return 0;
170}
171
Vipul Kumar3b06a0f2018-01-23 14:52:35 +0530172/**
173 * axienet_dma_write - Memory mapped Axi DMA register Buffer Descriptor write.
174 * @bd: pointer to BD descriptor structure
175 * @desc: Address offset of DMA descriptors
176 *
177 * This function writes the value into the corresponding Axi DMA register.
178 */
179static inline void axienet_dma_write(struct axidma_bd *bd, u32 *desc)
180{
181#if defined(CONFIG_PHYS_64BIT)
182 writeq(bd, desc);
183#else
184 writel((u32)bd, desc);
185#endif
186}
187
Michal Simek41beca12015-12-09 14:44:38 +0100188static u32 phyread(struct axidma_priv *priv, u32 phyaddress, u32 registernum,
189 u16 *val)
Michal Simek6fc7c452011-10-06 20:35:35 +0000190{
Michal Simek41beca12015-12-09 14:44:38 +0100191 struct axi_regs *regs = priv->iobase;
Michal Simek6fc7c452011-10-06 20:35:35 +0000192 u32 mdioctrlreg = 0;
193
Michal Simekf5221872015-12-09 14:36:31 +0100194 if (mdio_wait(regs))
Michal Simek6fc7c452011-10-06 20:35:35 +0000195 return 1;
196
197 mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) &
198 XAE_MDIO_MCR_PHYAD_MASK) |
199 ((registernum << XAE_MDIO_MCR_REGAD_SHIFT)
200 & XAE_MDIO_MCR_REGAD_MASK) |
201 XAE_MDIO_MCR_INITIATE_MASK |
202 XAE_MDIO_MCR_OP_READ_MASK;
203
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530204 writel(mdioctrlreg, &regs->mdio_mcr);
Michal Simek6fc7c452011-10-06 20:35:35 +0000205
Michal Simekf5221872015-12-09 14:36:31 +0100206 if (mdio_wait(regs))
Michal Simek6fc7c452011-10-06 20:35:35 +0000207 return 1;
208
209 /* Read data */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530210 *val = readl(&regs->mdio_mrd);
Michal Simek6fc7c452011-10-06 20:35:35 +0000211 return 0;
212}
213
Michal Simek41beca12015-12-09 14:44:38 +0100214static u32 phywrite(struct axidma_priv *priv, u32 phyaddress, u32 registernum,
215 u32 data)
Michal Simek6fc7c452011-10-06 20:35:35 +0000216{
Michal Simek41beca12015-12-09 14:44:38 +0100217 struct axi_regs *regs = priv->iobase;
Michal Simek6fc7c452011-10-06 20:35:35 +0000218 u32 mdioctrlreg = 0;
219
Michal Simekf5221872015-12-09 14:36:31 +0100220 if (mdio_wait(regs))
Michal Simek6fc7c452011-10-06 20:35:35 +0000221 return 1;
222
223 mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) &
224 XAE_MDIO_MCR_PHYAD_MASK) |
225 ((registernum << XAE_MDIO_MCR_REGAD_SHIFT)
226 & XAE_MDIO_MCR_REGAD_MASK) |
227 XAE_MDIO_MCR_INITIATE_MASK |
228 XAE_MDIO_MCR_OP_WRITE_MASK;
229
230 /* Write data */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530231 writel(data, &regs->mdio_mwd);
Michal Simek6fc7c452011-10-06 20:35:35 +0000232
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530233 writel(mdioctrlreg, &regs->mdio_mcr);
Michal Simek6fc7c452011-10-06 20:35:35 +0000234
Michal Simekf5221872015-12-09 14:36:31 +0100235 if (mdio_wait(regs))
Michal Simek6fc7c452011-10-06 20:35:35 +0000236 return 1;
237
238 return 0;
239}
240
Michal Simek0b13ee22015-12-08 16:10:05 +0100241static int axiemac_phy_init(struct udevice *dev)
Michal Simek6fc7c452011-10-06 20:35:35 +0000242{
243 u16 phyreg;
Michal Simek0b13ee22015-12-08 16:10:05 +0100244 u32 i, ret;
Michal Simek682baac2015-12-08 15:44:41 +0100245 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek6cb55e72015-12-09 14:39:42 +0100246 struct axi_regs *regs = priv->iobase;
Michal Simek6fc7c452011-10-06 20:35:35 +0000247 struct phy_device *phydev;
248
249 u32 supported = SUPPORTED_10baseT_Half |
250 SUPPORTED_10baseT_Full |
251 SUPPORTED_100baseT_Half |
252 SUPPORTED_100baseT_Full |
253 SUPPORTED_1000baseT_Half |
254 SUPPORTED_1000baseT_Full;
255
Michal Simek0b13ee22015-12-08 16:10:05 +0100256 /* Set default MDIO divisor */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530257 writel(XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK, &regs->mdio_mc);
Michal Simek0b13ee22015-12-08 16:10:05 +0100258
Michal Simek6fc7c452011-10-06 20:35:35 +0000259 if (priv->phyaddr == -1) {
260 /* Detect the PHY address */
261 for (i = 31; i >= 0; i--) {
Michal Simek41beca12015-12-09 14:44:38 +0100262 ret = phyread(priv, i, PHY_DETECT_REG, &phyreg);
Michal Simek6fc7c452011-10-06 20:35:35 +0000263 if (!ret && (phyreg != 0xFFFF) &&
264 ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
265 /* Found a valid PHY address */
266 priv->phyaddr = i;
267 debug("axiemac: Found valid phy address, %x\n",
Michal Simek2f1e0652015-12-09 10:54:53 +0100268 i);
Michal Simek6fc7c452011-10-06 20:35:35 +0000269 break;
270 }
271 }
272 }
273
274 /* Interface - look at tsec */
Siva Durga Prasad Paladugu49348512016-02-21 15:46:14 +0530275 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
Michal Simek6fc7c452011-10-06 20:35:35 +0000276
277 phydev->supported &= supported;
278 phydev->advertising = phydev->supported;
279 priv->phydev = phydev;
Siva Durga Prasad Paladuguc3d94f62019-03-15 17:46:45 +0530280 if (priv->phy_of_handle)
281 priv->phydev->node = offset_to_ofnode(priv->phy_of_handle);
Michal Simek6fc7c452011-10-06 20:35:35 +0000282 phy_config(phydev);
Michal Simek0b13ee22015-12-08 16:10:05 +0100283
284 return 0;
285}
286
287/* Setting axi emac and phy to proper setting */
288static int setup_phy(struct udevice *dev)
289{
Siva Durga Prasad Paladugua1c6ed82016-02-21 15:46:15 +0530290 u16 temp;
291 u32 speed, emmc_reg, ret;
Michal Simek0b13ee22015-12-08 16:10:05 +0100292 struct axidma_priv *priv = dev_get_priv(dev);
293 struct axi_regs *regs = priv->iobase;
294 struct phy_device *phydev = priv->phydev;
295
Siva Durga Prasad Paladugua1c6ed82016-02-21 15:46:15 +0530296 if (priv->interface == PHY_INTERFACE_MODE_SGMII) {
297 /*
298 * In SGMII cases the isolate bit might set
299 * after DMA and ethernet resets and hence
300 * check and clear if set.
301 */
302 ret = phyread(priv, priv->phyaddr, MII_BMCR, &temp);
303 if (ret)
304 return 0;
305 if (temp & BMCR_ISOLATE) {
306 temp &= ~BMCR_ISOLATE;
307 ret = phywrite(priv, priv->phyaddr, MII_BMCR, temp);
308 if (ret)
309 return 0;
310 }
311 }
312
Timur Tabi42387462012-07-09 08:52:43 +0000313 if (phy_startup(phydev)) {
314 printf("axiemac: could not initialize PHY %s\n",
315 phydev->dev->name);
316 return 0;
317 }
Michal Simek5848f132013-11-21 16:15:51 +0100318 if (!phydev->link) {
319 printf("%s: No link.\n", phydev->dev->name);
320 return 0;
321 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000322
323 switch (phydev->speed) {
324 case 1000:
325 speed = XAE_EMMC_LINKSPD_1000;
326 break;
327 case 100:
328 speed = XAE_EMMC_LINKSPD_100;
329 break;
330 case 10:
331 speed = XAE_EMMC_LINKSPD_10;
332 break;
333 default:
334 return 0;
335 }
336
337 /* Setup the emac for the phy speed */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530338 emmc_reg = readl(&regs->emmc);
Michal Simek6fc7c452011-10-06 20:35:35 +0000339 emmc_reg &= ~XAE_EMMC_LINKSPEED_MASK;
340 emmc_reg |= speed;
341
342 /* Write new speed setting out to Axi Ethernet */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530343 writel(emmc_reg, &regs->emmc);
Michal Simek6fc7c452011-10-06 20:35:35 +0000344
345 /*
346 * Setting the operating speed of the MAC needs a delay. There
347 * doesn't seem to be register to poll, so please consider this
348 * during your application design.
349 */
350 udelay(1);
351
352 return 1;
353}
354
355/* STOP DMA transfers */
Michal Simek8fbf79f2015-12-16 09:18:12 +0100356static void axiemac_stop(struct udevice *dev)
Michal Simek6fc7c452011-10-06 20:35:35 +0000357{
Michal Simek682baac2015-12-08 15:44:41 +0100358 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek6fc7c452011-10-06 20:35:35 +0000359 u32 temp;
360
361 /* Stop the hardware */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530362 temp = readl(&priv->dmatx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000363 temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530364 writel(temp, &priv->dmatx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000365
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530366 temp = readl(&priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000367 temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530368 writel(temp, &priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000369
370 debug("axiemac: Halted\n");
371}
372
Michal Simek638c0ef2015-12-09 14:53:51 +0100373static int axi_ethernet_init(struct axidma_priv *priv)
Michal Simek6fc7c452011-10-06 20:35:35 +0000374{
Michal Simek638c0ef2015-12-09 14:53:51 +0100375 struct axi_regs *regs = priv->iobase;
Siva Durga Prasad Paladugu20633622017-01-06 16:18:50 +0530376 int err;
Michal Simek6fc7c452011-10-06 20:35:35 +0000377
378 /*
379 * Check the status of the MgtRdy bit in the interrupt status
380 * registers. This must be done to allow the MGT clock to become stable
381 * for the Sgmii and 1000BaseX PHY interfaces. No other register reads
382 * will be valid until this bit is valid.
383 * The bit is always a 1 for all other PHY interfaces.
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530384 * Interrupt status and enable registers are not available in non
385 * processor mode and hence bypass in this mode
Michal Simek6fc7c452011-10-06 20:35:35 +0000386 */
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530387 if (!priv->eth_hasnobuf) {
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100388 err = wait_for_bit_le32(&regs->is, XAE_INT_MGTRDY_MASK,
389 true, 200, false);
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530390 if (err) {
391 printf("%s: Timeout\n", __func__);
392 return 1;
393 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000394
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530395 /*
396 * Stop the device and reset HW
397 * Disable interrupts
398 */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530399 writel(0, &regs->ie);
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530400 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000401
402 /* Disable the receiver */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530403 writel(readl(&regs->rcw1) & ~XAE_RCW1_RX_MASK, &regs->rcw1);
Michal Simek6fc7c452011-10-06 20:35:35 +0000404
405 /*
406 * Stopping the receiver in mid-packet causes a dropped packet
407 * indication from HW. Clear it.
408 */
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530409 if (!priv->eth_hasnobuf) {
410 /* Set the interrupt status register to clear the interrupt */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530411 writel(XAE_INT_RXRJECT_MASK, &regs->is);
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530412 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000413
414 /* Setup HW */
415 /* Set default MDIO divisor */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530416 writel(XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK, &regs->mdio_mc);
Michal Simek6fc7c452011-10-06 20:35:35 +0000417
418 debug("axiemac: InitHw done\n");
419 return 0;
420}
421
Michal Simek8fbf79f2015-12-16 09:18:12 +0100422static int axiemac_write_hwaddr(struct udevice *dev)
Michal Simek6fc7c452011-10-06 20:35:35 +0000423{
Michal Simek682baac2015-12-08 15:44:41 +0100424 struct eth_pdata *pdata = dev_get_platdata(dev);
425 struct axidma_priv *priv = dev_get_priv(dev);
426 struct axi_regs *regs = priv->iobase;
Michal Simek6fc7c452011-10-06 20:35:35 +0000427
428 /* Set the MAC address */
Michal Simek682baac2015-12-08 15:44:41 +0100429 int val = ((pdata->enetaddr[3] << 24) | (pdata->enetaddr[2] << 16) |
430 (pdata->enetaddr[1] << 8) | (pdata->enetaddr[0]));
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530431 writel(val, &regs->uaw0);
Michal Simek6fc7c452011-10-06 20:35:35 +0000432
Michal Simek682baac2015-12-08 15:44:41 +0100433 val = (pdata->enetaddr[5] << 8) | pdata->enetaddr[4];
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530434 val |= readl(&regs->uaw1) & ~XAE_UAW1_UNICASTADDR_MASK;
435 writel(val, &regs->uaw1);
Michal Simek6fc7c452011-10-06 20:35:35 +0000436 return 0;
437}
438
439/* Reset DMA engine */
Michal Simek638c0ef2015-12-09 14:53:51 +0100440static void axi_dma_init(struct axidma_priv *priv)
Michal Simek6fc7c452011-10-06 20:35:35 +0000441{
Michal Simek6fc7c452011-10-06 20:35:35 +0000442 u32 timeout = 500;
443
444 /* Reset the engine so the hardware starts from a known state */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530445 writel(XAXIDMA_CR_RESET_MASK, &priv->dmatx->control);
446 writel(XAXIDMA_CR_RESET_MASK, &priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000447
448 /* At the initialization time, hardware should finish reset quickly */
449 while (timeout--) {
450 /* Check transmit/receive channel */
451 /* Reset is done when the reset bit is low */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530452 if (!((readl(&priv->dmatx->control) |
453 readl(&priv->dmarx->control))
Michal Simek5aa45392015-10-28 11:00:47 +0100454 & XAXIDMA_CR_RESET_MASK)) {
Michal Simek6fc7c452011-10-06 20:35:35 +0000455 break;
456 }
457 }
458 if (!timeout)
459 printf("%s: Timeout\n", __func__);
460}
461
Michal Simek8fbf79f2015-12-16 09:18:12 +0100462static int axiemac_start(struct udevice *dev)
Michal Simek6fc7c452011-10-06 20:35:35 +0000463{
Michal Simek682baac2015-12-08 15:44:41 +0100464 struct axidma_priv *priv = dev_get_priv(dev);
465 struct axi_regs *regs = priv->iobase;
Michal Simek6fc7c452011-10-06 20:35:35 +0000466 u32 temp;
467
468 debug("axiemac: Init started\n");
469 /*
470 * Initialize AXIDMA engine. AXIDMA engine must be initialized before
471 * AxiEthernet. During AXIDMA engine initialization, AXIDMA hardware is
472 * reset, and since AXIDMA reset line is connected to AxiEthernet, this
473 * would ensure a reset of AxiEthernet.
474 */
Michal Simek638c0ef2015-12-09 14:53:51 +0100475 axi_dma_init(priv);
Michal Simek6fc7c452011-10-06 20:35:35 +0000476
477 /* Initialize AxiEthernet hardware. */
Michal Simek638c0ef2015-12-09 14:53:51 +0100478 if (axi_ethernet_init(priv))
Michal Simek6fc7c452011-10-06 20:35:35 +0000479 return -1;
480
481 /* Disable all RX interrupts before RxBD space setup */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530482 temp = readl(&priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000483 temp &= ~XAXIDMA_IRQ_ALL_MASK;
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530484 writel(temp, &priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000485
486 /* Start DMA RX channel. Now it's ready to receive data.*/
Vipul Kumar3b06a0f2018-01-23 14:52:35 +0530487 axienet_dma_write(&rx_bd, &priv->dmarx->current);
Michal Simek6fc7c452011-10-06 20:35:35 +0000488
489 /* Setup the BD. */
490 memset(&rx_bd, 0, sizeof(rx_bd));
491 rx_bd.next = (u32)&rx_bd;
492 rx_bd.phys = (u32)&rxframe;
493 rx_bd.cntrl = sizeof(rxframe);
494 /* Flush the last BD so DMA core could see the updates */
495 flush_cache((u32)&rx_bd, sizeof(rx_bd));
496
497 /* It is necessary to flush rxframe because if you don't do it
498 * then cache can contain uninitialized data */
499 flush_cache((u32)&rxframe, sizeof(rxframe));
500
501 /* Start the hardware */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530502 temp = readl(&priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000503 temp |= XAXIDMA_CR_RUNSTOP_MASK;
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530504 writel(temp, &priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000505
506 /* Rx BD is ready - start */
Vipul Kumar3b06a0f2018-01-23 14:52:35 +0530507 axienet_dma_write(&rx_bd, &priv->dmarx->tail);
Michal Simek6fc7c452011-10-06 20:35:35 +0000508
509 /* Enable TX */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530510 writel(XAE_TC_TX_MASK, &regs->tc);
Michal Simek6fc7c452011-10-06 20:35:35 +0000511 /* Enable RX */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530512 writel(XAE_RCW1_RX_MASK, &regs->rcw1);
Michal Simek6fc7c452011-10-06 20:35:35 +0000513
514 /* PHY setup */
515 if (!setup_phy(dev)) {
Michal Simek8fbf79f2015-12-16 09:18:12 +0100516 axiemac_stop(dev);
Michal Simek6fc7c452011-10-06 20:35:35 +0000517 return -1;
518 }
519
520 debug("axiemac: Init complete\n");
521 return 0;
522}
523
Michal Simek682baac2015-12-08 15:44:41 +0100524static int axiemac_send(struct udevice *dev, void *ptr, int len)
Michal Simek6fc7c452011-10-06 20:35:35 +0000525{
Michal Simek682baac2015-12-08 15:44:41 +0100526 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek6fc7c452011-10-06 20:35:35 +0000527 u32 timeout;
528
529 if (len > PKTSIZE_ALIGN)
530 len = PKTSIZE_ALIGN;
531
532 /* Flush packet to main memory to be trasfered by DMA */
533 flush_cache((u32)ptr, len);
534
535 /* Setup Tx BD */
536 memset(&tx_bd, 0, sizeof(tx_bd));
537 /* At the end of the ring, link the last BD back to the top */
538 tx_bd.next = (u32)&tx_bd;
539 tx_bd.phys = (u32)ptr;
540 /* Save len */
541 tx_bd.cntrl = len | XAXIDMA_BD_CTRL_TXSOF_MASK |
542 XAXIDMA_BD_CTRL_TXEOF_MASK;
543
544 /* Flush the last BD so DMA core could see the updates */
545 flush_cache((u32)&tx_bd, sizeof(tx_bd));
546
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530547 if (readl(&priv->dmatx->status) & XAXIDMA_HALTED_MASK) {
Michal Simek6fc7c452011-10-06 20:35:35 +0000548 u32 temp;
Vipul Kumar3b06a0f2018-01-23 14:52:35 +0530549 axienet_dma_write(&tx_bd, &priv->dmatx->current);
Michal Simek6fc7c452011-10-06 20:35:35 +0000550 /* Start the hardware */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530551 temp = readl(&priv->dmatx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000552 temp |= XAXIDMA_CR_RUNSTOP_MASK;
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530553 writel(temp, &priv->dmatx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000554 }
555
556 /* Start transfer */
Vipul Kumar3b06a0f2018-01-23 14:52:35 +0530557 axienet_dma_write(&tx_bd, &priv->dmatx->tail);
Michal Simek6fc7c452011-10-06 20:35:35 +0000558
559 /* Wait for transmission to complete */
560 debug("axiemac: Waiting for tx to be done\n");
561 timeout = 200;
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530562 while (timeout && (!(readl(&priv->dmatx->status) &
Michal Simek5aa45392015-10-28 11:00:47 +0100563 (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))) {
Michal Simek6fc7c452011-10-06 20:35:35 +0000564 timeout--;
565 udelay(1);
566 }
567 if (!timeout) {
568 printf("%s: Timeout\n", __func__);
569 return 1;
570 }
571
572 debug("axiemac: Sending complete\n");
573 return 0;
574}
575
Michal Simek638c0ef2015-12-09 14:53:51 +0100576static int isrxready(struct axidma_priv *priv)
Michal Simek6fc7c452011-10-06 20:35:35 +0000577{
578 u32 status;
Michal Simek6fc7c452011-10-06 20:35:35 +0000579
580 /* Read pending interrupts */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530581 status = readl(&priv->dmarx->status);
Michal Simek6fc7c452011-10-06 20:35:35 +0000582
583 /* Acknowledge pending interrupts */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530584 writel(status & XAXIDMA_IRQ_ALL_MASK, &priv->dmarx->status);
Michal Simek6fc7c452011-10-06 20:35:35 +0000585
586 /*
587 * If Reception done interrupt is asserted, call RX call back function
588 * to handle the processed BDs and then raise the according flag.
589 */
590 if ((status & (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))
591 return 1;
592
593 return 0;
594}
595
Michal Simek682baac2015-12-08 15:44:41 +0100596static int axiemac_recv(struct udevice *dev, int flags, uchar **packetp)
Michal Simek6fc7c452011-10-06 20:35:35 +0000597{
598 u32 length;
Michal Simek682baac2015-12-08 15:44:41 +0100599 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek6fc7c452011-10-06 20:35:35 +0000600 u32 temp;
601
602 /* Wait for an incoming packet */
Michal Simek638c0ef2015-12-09 14:53:51 +0100603 if (!isrxready(priv))
Michal Simek682baac2015-12-08 15:44:41 +0100604 return -1;
Michal Simek6fc7c452011-10-06 20:35:35 +0000605
606 debug("axiemac: RX data ready\n");
607
608 /* Disable IRQ for a moment till packet is handled */
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530609 temp = readl(&priv->dmarx->control);
Michal Simek6fc7c452011-10-06 20:35:35 +0000610 temp &= ~XAXIDMA_IRQ_ALL_MASK;
Siva Durga Prasad Paladugud227c9a2017-11-23 12:23:12 +0530611 writel(temp, &priv->dmarx->control);
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530612 if (!priv->eth_hasnobuf)
613 length = rx_bd.app4 & 0xFFFF; /* max length mask */
614 else
615 length = rx_bd.status & XAXIDMA_BD_STS_ACTUAL_LEN_MASK;
Michal Simek6fc7c452011-10-06 20:35:35 +0000616
Michal Simek6fc7c452011-10-06 20:35:35 +0000617#ifdef DEBUG
618 print_buffer(&rxframe, &rxframe[0], 1, length, 16);
619#endif
Michal Simek0305be82015-12-09 14:13:23 +0100620
621 *packetp = rxframe;
622 return length;
623}
624
625static int axiemac_free_pkt(struct udevice *dev, uchar *packet, int length)
626{
627 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek6fc7c452011-10-06 20:35:35 +0000628
629#ifdef DEBUG
630 /* It is useful to clear buffer to be sure that it is consistent */
631 memset(rxframe, 0, sizeof(rxframe));
632#endif
633 /* Setup RxBD */
634 /* Clear the whole buffer and setup it again - all flags are cleared */
635 memset(&rx_bd, 0, sizeof(rx_bd));
636 rx_bd.next = (u32)&rx_bd;
637 rx_bd.phys = (u32)&rxframe;
638 rx_bd.cntrl = sizeof(rxframe);
639
640 /* Write bd to HW */
641 flush_cache((u32)&rx_bd, sizeof(rx_bd));
642
643 /* It is necessary to flush rxframe because if you don't do it
644 * then cache will contain previous packet */
645 flush_cache((u32)&rxframe, sizeof(rxframe));
646
647 /* Rx BD is ready - start again */
Vipul Kumar3b06a0f2018-01-23 14:52:35 +0530648 axienet_dma_write(&rx_bd, &priv->dmarx->tail);
Michal Simek6fc7c452011-10-06 20:35:35 +0000649
650 debug("axiemac: RX completed, framelength = %d\n", length);
651
Michal Simek682baac2015-12-08 15:44:41 +0100652 return 0;
Michal Simek6fc7c452011-10-06 20:35:35 +0000653}
654
Michal Simek682baac2015-12-08 15:44:41 +0100655static int axiemac_miiphy_read(struct mii_dev *bus, int addr,
656 int devad, int reg)
Michal Simek6fc7c452011-10-06 20:35:35 +0000657{
Michal Simek682baac2015-12-08 15:44:41 +0100658 int ret;
659 u16 value;
Michal Simek6fc7c452011-10-06 20:35:35 +0000660
Michal Simek682baac2015-12-08 15:44:41 +0100661 ret = phyread(bus->priv, addr, reg, &value);
662 debug("axiemac: Read MII 0x%x, 0x%x, 0x%x, %d\n", addr, reg,
663 value, ret);
664 return value;
Michal Simek6fc7c452011-10-06 20:35:35 +0000665}
666
Michal Simek682baac2015-12-08 15:44:41 +0100667static int axiemac_miiphy_write(struct mii_dev *bus, int addr, int devad,
668 int reg, u16 value)
Michal Simek6fc7c452011-10-06 20:35:35 +0000669{
Michal Simek682baac2015-12-08 15:44:41 +0100670 debug("axiemac: Write MII 0x%x, 0x%x, 0x%x\n", addr, reg, value);
671 return phywrite(bus->priv, addr, reg, value);
Michal Simek6fc7c452011-10-06 20:35:35 +0000672}
673
Michal Simek682baac2015-12-08 15:44:41 +0100674static int axi_emac_probe(struct udevice *dev)
Michal Simek6fc7c452011-10-06 20:35:35 +0000675{
Michal Simek682baac2015-12-08 15:44:41 +0100676 struct axidma_priv *priv = dev_get_priv(dev);
677 int ret;
678
679 priv->bus = mdio_alloc();
680 priv->bus->read = axiemac_miiphy_read;
681 priv->bus->write = axiemac_miiphy_write;
682 priv->bus->priv = priv;
Michal Simek682baac2015-12-08 15:44:41 +0100683
Michal Simeke4dab432016-12-08 10:25:44 +0100684 ret = mdio_register_seq(priv->bus, dev->seq);
Michal Simek682baac2015-12-08 15:44:41 +0100685 if (ret)
686 return ret;
687
Michal Simek0b13ee22015-12-08 16:10:05 +0100688 axiemac_phy_init(dev);
689
Michal Simek6fc7c452011-10-06 20:35:35 +0000690 return 0;
691}
692
Michal Simek682baac2015-12-08 15:44:41 +0100693static int axi_emac_remove(struct udevice *dev)
Michal Simek6fc7c452011-10-06 20:35:35 +0000694{
Michal Simek682baac2015-12-08 15:44:41 +0100695 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek6fc7c452011-10-06 20:35:35 +0000696
Michal Simek682baac2015-12-08 15:44:41 +0100697 free(priv->phydev);
698 mdio_unregister(priv->bus);
699 mdio_free(priv->bus);
Michal Simek6fc7c452011-10-06 20:35:35 +0000700
Michal Simek682baac2015-12-08 15:44:41 +0100701 return 0;
702}
703
704static const struct eth_ops axi_emac_ops = {
Michal Simek8fbf79f2015-12-16 09:18:12 +0100705 .start = axiemac_start,
Michal Simek682baac2015-12-08 15:44:41 +0100706 .send = axiemac_send,
707 .recv = axiemac_recv,
Michal Simek0305be82015-12-09 14:13:23 +0100708 .free_pkt = axiemac_free_pkt,
Michal Simek8fbf79f2015-12-16 09:18:12 +0100709 .stop = axiemac_stop,
710 .write_hwaddr = axiemac_write_hwaddr,
Michal Simek682baac2015-12-08 15:44:41 +0100711};
712
713static int axi_emac_ofdata_to_platdata(struct udevice *dev)
714{
715 struct eth_pdata *pdata = dev_get_platdata(dev);
716 struct axidma_priv *priv = dev_get_priv(dev);
Simon Glassdd79d6e2017-01-17 16:52:55 -0700717 int node = dev_of_offset(dev);
Michal Simek682baac2015-12-08 15:44:41 +0100718 int offset = 0;
719 const char *phy_mode;
Michal Simek6fc7c452011-10-06 20:35:35 +0000720
Simon Glassba1dea42017-05-17 17:18:05 -0600721 pdata->iobase = (phys_addr_t)devfdt_get_addr(dev);
Michal Simek682baac2015-12-08 15:44:41 +0100722 priv->iobase = (struct axi_regs *)pdata->iobase;
Michal Simek6fc7c452011-10-06 20:35:35 +0000723
Simon Glassdd79d6e2017-01-17 16:52:55 -0700724 offset = fdtdec_lookup_phandle(gd->fdt_blob, node,
Michal Simek682baac2015-12-08 15:44:41 +0100725 "axistream-connected");
726 if (offset <= 0) {
727 printf("%s: axistream is not found\n", __func__);
728 return -EINVAL;
729 }
Siva Durga Prasad Paladugu6372e3f2017-06-22 11:14:55 +0530730 priv->dmatx = (struct axidma_reg *)fdtdec_get_addr(gd->fdt_blob,
731 offset, "reg");
Michal Simek682baac2015-12-08 15:44:41 +0100732 if (!priv->dmatx) {
733 printf("%s: axi_dma register space not found\n", __func__);
734 return -EINVAL;
735 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000736 /* RX channel offset is 0x30 */
Michal Simek682baac2015-12-08 15:44:41 +0100737 priv->dmarx = (struct axidma_reg *)((u32)priv->dmatx + 0x30);
Michal Simek6fc7c452011-10-06 20:35:35 +0000738
Michal Simek6fc7c452011-10-06 20:35:35 +0000739 priv->phyaddr = -1;
Michal Simek6fc7c452011-10-06 20:35:35 +0000740
Simon Glassdd79d6e2017-01-17 16:52:55 -0700741 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
Siva Durga Prasad Paladuguc3d94f62019-03-15 17:46:45 +0530742 if (offset > 0) {
Michal Simek682baac2015-12-08 15:44:41 +0100743 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
Siva Durga Prasad Paladuguc3d94f62019-03-15 17:46:45 +0530744 priv->phy_of_handle = offset;
745 }
Michal Simek6fc7c452011-10-06 20:35:35 +0000746
Simon Glassdd79d6e2017-01-17 16:52:55 -0700747 phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
Michal Simek682baac2015-12-08 15:44:41 +0100748 if (phy_mode)
749 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
750 if (pdata->phy_interface == -1) {
Michal Simek9dbfc0d2016-02-08 13:54:05 +0100751 printf("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
Michal Simek682baac2015-12-08 15:44:41 +0100752 return -EINVAL;
753 }
754 priv->interface = pdata->phy_interface;
755
Siva Durga Prasad Paladuguf8026032017-01-06 16:27:15 +0530756 priv->eth_hasnobuf = fdtdec_get_bool(gd->fdt_blob, node,
757 "xlnx,eth-hasnobuf");
758
Michal Simek682baac2015-12-08 15:44:41 +0100759 printf("AXI EMAC: %lx, phyaddr %d, interface %s\n", (ulong)priv->iobase,
760 priv->phyaddr, phy_string_for_interface(priv->interface));
761
762 return 0;
Michal Simek6fc7c452011-10-06 20:35:35 +0000763}
Michal Simek682baac2015-12-08 15:44:41 +0100764
765static const struct udevice_id axi_emac_ids[] = {
766 { .compatible = "xlnx,axi-ethernet-1.00.a" },
767 { }
768};
769
770U_BOOT_DRIVER(axi_emac) = {
771 .name = "axi_emac",
772 .id = UCLASS_ETH,
773 .of_match = axi_emac_ids,
774 .ofdata_to_platdata = axi_emac_ofdata_to_platdata,
775 .probe = axi_emac_probe,
776 .remove = axi_emac_remove,
777 .ops = &axi_emac_ops,
778 .priv_auto_alloc_size = sizeof(struct axidma_priv),
779 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
780};