blob: d0683db80d88413ea95c765a9fb8b3fe613edb76 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michal Simek4f1ec4c2011-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 Simek4f1ec4c2011-10-06 20:35:35 +00006 */
7
8#include <config.h>
9#include <common.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070010#include <cpu_func.h>
Michal Simek75cc93f2015-12-08 15:44:41 +010011#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Michal Simek4f1ec4c2011-10-06 20:35:35 +000013#include <net.h>
14#include <malloc.h>
15#include <asm/io.h>
16#include <phy.h>
17#include <miiphy.h>
Siva Durga Prasad Paladugud02a0b12017-01-06 16:18:50 +053018#include <wait_bit.h>
Simon Glassc05ed002020-05-10 11:40:11 -060019#include <linux/delay.h>
Michal Simek4f1ec4c2011-10-06 20:35:35 +000020
Michal Simek75cc93f2015-12-08 15:44:41 +010021DECLARE_GLOBAL_DATA_PTR;
22
Michal Simek4f1ec4c2011-10-06 20:35:35 +000023/* Link setup */
24#define XAE_EMMC_LINKSPEED_MASK 0xC0000000 /* Link speed */
25#define XAE_EMMC_LINKSPD_10 0x00000000 /* Link Speed mask for 10 Mbit */
26#define XAE_EMMC_LINKSPD_100 0x40000000 /* Link Speed mask for 100 Mbit */
27#define XAE_EMMC_LINKSPD_1000 0x80000000 /* Link Speed mask for 1000 Mbit */
28
29/* Interrupt Status/Enable/Mask Registers bit definitions */
30#define XAE_INT_RXRJECT_MASK 0x00000008 /* Rx frame rejected */
31#define XAE_INT_MGTRDY_MASK 0x00000080 /* MGT clock Lock */
32
33/* Receive Configuration Word 1 (RCW1) Register bit definitions */
34#define XAE_RCW1_RX_MASK 0x10000000 /* Receiver enable */
35
36/* Transmitter Configuration (TC) Register bit definitions */
37#define XAE_TC_TX_MASK 0x10000000 /* Transmitter enable */
38
39#define XAE_UAW1_UNICASTADDR_MASK 0x0000FFFF
40
41/* MDIO Management Configuration (MC) Register bit definitions */
42#define XAE_MDIO_MC_MDIOEN_MASK 0x00000040 /* MII management enable*/
43
44/* MDIO Management Control Register (MCR) Register bit definitions */
45#define XAE_MDIO_MCR_PHYAD_MASK 0x1F000000 /* Phy Address Mask */
46#define XAE_MDIO_MCR_PHYAD_SHIFT 24 /* Phy Address Shift */
47#define XAE_MDIO_MCR_REGAD_MASK 0x001F0000 /* Reg Address Mask */
48#define XAE_MDIO_MCR_REGAD_SHIFT 16 /* Reg Address Shift */
49#define XAE_MDIO_MCR_OP_READ_MASK 0x00008000 /* Op Code Read Mask */
50#define XAE_MDIO_MCR_OP_WRITE_MASK 0x00004000 /* Op Code Write Mask */
51#define XAE_MDIO_MCR_INITIATE_MASK 0x00000800 /* Ready Mask */
52#define XAE_MDIO_MCR_READY_MASK 0x00000080 /* Ready Mask */
53
54#define XAE_MDIO_DIV_DFT 29 /* Default MDIO clock divisor */
55
Siva Durga Prasad Paladugu89ce5a92017-01-06 16:27:15 +053056#define XAXIDMA_BD_STS_ACTUAL_LEN_MASK 0x007FFFFF /* Actual len */
57
Michal Simek4f1ec4c2011-10-06 20:35:35 +000058/* DMA macros */
59/* Bitmasks of XAXIDMA_CR_OFFSET register */
60#define XAXIDMA_CR_RUNSTOP_MASK 0x00000001 /* Start/stop DMA channel */
61#define XAXIDMA_CR_RESET_MASK 0x00000004 /* Reset DMA engine */
62
63/* Bitmasks of XAXIDMA_SR_OFFSET register */
64#define XAXIDMA_HALTED_MASK 0x00000001 /* DMA channel halted */
65
66/* Bitmask for interrupts */
67#define XAXIDMA_IRQ_IOC_MASK 0x00001000 /* Completion intr */
68#define XAXIDMA_IRQ_DELAY_MASK 0x00002000 /* Delay interrupt */
69#define XAXIDMA_IRQ_ALL_MASK 0x00007000 /* All interrupts */
70
71/* Bitmasks of XAXIDMA_BD_CTRL_OFFSET register */
72#define XAXIDMA_BD_CTRL_TXSOF_MASK 0x08000000 /* First tx packet */
73#define XAXIDMA_BD_CTRL_TXEOF_MASK 0x04000000 /* Last tx packet */
74
75#define DMAALIGN 128
76
77static u8 rxframe[PKTSIZE_ALIGN] __attribute((aligned(DMAALIGN)));
78
79/* Reflect dma offsets */
80struct axidma_reg {
81 u32 control; /* DMACR */
82 u32 status; /* DMASR */
Vipul Kumar047f3bf2018-01-23 14:52:35 +053083 u32 current; /* CURDESC low 32 bit */
84 u32 current_hi; /* CURDESC high 32 bit */
85 u32 tail; /* TAILDESC low 32 bit */
86 u32 tail_hi; /* TAILDESC high 32 bit */
Michal Simek4f1ec4c2011-10-06 20:35:35 +000087};
88
89/* Private driver structures */
90struct axidma_priv {
91 struct axidma_reg *dmatx;
92 struct axidma_reg *dmarx;
93 int phyaddr;
Michal Simek6609f352015-12-09 14:39:42 +010094 struct axi_regs *iobase;
Michal Simek75cc93f2015-12-08 15:44:41 +010095 phy_interface_t interface;
Michal Simek4f1ec4c2011-10-06 20:35:35 +000096 struct phy_device *phydev;
97 struct mii_dev *bus;
Siva Durga Prasad Paladugu89ce5a92017-01-06 16:27:15 +053098 u8 eth_hasnobuf;
Siva Durga Prasad Paladugufccfb712019-03-15 17:46:45 +053099 int phy_of_handle;
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000100};
101
102/* BD descriptors */
103struct axidma_bd {
104 u32 next; /* Next descriptor pointer */
105 u32 reserved1;
106 u32 phys; /* Buffer address */
107 u32 reserved2;
108 u32 reserved3;
109 u32 reserved4;
110 u32 cntrl; /* Control */
111 u32 status; /* Status */
112 u32 app0;
113 u32 app1; /* TX start << 16 | insert */
114 u32 app2; /* TX csum seed */
115 u32 app3;
116 u32 app4;
117 u32 sw_id_offset;
118 u32 reserved5;
119 u32 reserved6;
120};
121
122/* Static BDs - driver uses only one BD */
123static struct axidma_bd tx_bd __attribute((aligned(DMAALIGN)));
124static struct axidma_bd rx_bd __attribute((aligned(DMAALIGN)));
125
126struct axi_regs {
127 u32 reserved[3];
128 u32 is; /* 0xC: Interrupt status */
129 u32 reserved2;
130 u32 ie; /* 0x14: Interrupt enable */
131 u32 reserved3[251];
132 u32 rcw1; /* 0x404: Rx Configuration Word 1 */
133 u32 tc; /* 0x408: Tx Configuration */
134 u32 reserved4;
135 u32 emmc; /* 0x410: EMAC mode configuration */
136 u32 reserved5[59];
137 u32 mdio_mc; /* 0x500: MII Management Config */
138 u32 mdio_mcr; /* 0x504: MII Management Control */
139 u32 mdio_mwd; /* 0x508: MII Management Write Data */
140 u32 mdio_mrd; /* 0x50C: MII Management Read Data */
141 u32 reserved6[124];
142 u32 uaw0; /* 0x700: Unicast address word 0 */
143 u32 uaw1; /* 0x704: Unicast address word 1 */
144};
145
146/* Use MII register 1 (MII status register) to detect PHY */
147#define PHY_DETECT_REG 1
148
149/*
150 * Mask used to verify certain PHY features (or register contents)
151 * in the register above:
152 * 0x1000: 10Mbps full duplex support
153 * 0x0800: 10Mbps half duplex support
154 * 0x0008: Auto-negotiation support
155 */
156#define PHY_DETECT_MASK 0x1808
157
Michal Simekf36bbcc2015-12-09 14:36:31 +0100158static inline int mdio_wait(struct axi_regs *regs)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000159{
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000160 u32 timeout = 200;
161
162 /* Wait till MDIO interface is ready to accept a new transaction. */
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530163 while (timeout && (!(readl(&regs->mdio_mcr)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000164 & XAE_MDIO_MCR_READY_MASK))) {
165 timeout--;
166 udelay(1);
167 }
168 if (!timeout) {
169 printf("%s: Timeout\n", __func__);
170 return 1;
171 }
172 return 0;
173}
174
Vipul Kumar047f3bf2018-01-23 14:52:35 +0530175/**
176 * axienet_dma_write - Memory mapped Axi DMA register Buffer Descriptor write.
177 * @bd: pointer to BD descriptor structure
178 * @desc: Address offset of DMA descriptors
179 *
180 * This function writes the value into the corresponding Axi DMA register.
181 */
182static inline void axienet_dma_write(struct axidma_bd *bd, u32 *desc)
183{
184#if defined(CONFIG_PHYS_64BIT)
185 writeq(bd, desc);
186#else
187 writel((u32)bd, desc);
188#endif
189}
190
Michal Simek0d78abf2015-12-09 14:44:38 +0100191static u32 phyread(struct axidma_priv *priv, u32 phyaddress, u32 registernum,
192 u16 *val)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000193{
Michal Simek0d78abf2015-12-09 14:44:38 +0100194 struct axi_regs *regs = priv->iobase;
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000195 u32 mdioctrlreg = 0;
196
Michal Simekf36bbcc2015-12-09 14:36:31 +0100197 if (mdio_wait(regs))
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000198 return 1;
199
200 mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) &
201 XAE_MDIO_MCR_PHYAD_MASK) |
202 ((registernum << XAE_MDIO_MCR_REGAD_SHIFT)
203 & XAE_MDIO_MCR_REGAD_MASK) |
204 XAE_MDIO_MCR_INITIATE_MASK |
205 XAE_MDIO_MCR_OP_READ_MASK;
206
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530207 writel(mdioctrlreg, &regs->mdio_mcr);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000208
Michal Simekf36bbcc2015-12-09 14:36:31 +0100209 if (mdio_wait(regs))
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000210 return 1;
211
212 /* Read data */
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530213 *val = readl(&regs->mdio_mrd);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000214 return 0;
215}
216
Michal Simek0d78abf2015-12-09 14:44:38 +0100217static u32 phywrite(struct axidma_priv *priv, u32 phyaddress, u32 registernum,
218 u32 data)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000219{
Michal Simek0d78abf2015-12-09 14:44:38 +0100220 struct axi_regs *regs = priv->iobase;
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000221 u32 mdioctrlreg = 0;
222
Michal Simekf36bbcc2015-12-09 14:36:31 +0100223 if (mdio_wait(regs))
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000224 return 1;
225
226 mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) &
227 XAE_MDIO_MCR_PHYAD_MASK) |
228 ((registernum << XAE_MDIO_MCR_REGAD_SHIFT)
229 & XAE_MDIO_MCR_REGAD_MASK) |
230 XAE_MDIO_MCR_INITIATE_MASK |
231 XAE_MDIO_MCR_OP_WRITE_MASK;
232
233 /* Write data */
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530234 writel(data, &regs->mdio_mwd);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000235
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530236 writel(mdioctrlreg, &regs->mdio_mcr);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000237
Michal Simekf36bbcc2015-12-09 14:36:31 +0100238 if (mdio_wait(regs))
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000239 return 1;
240
241 return 0;
242}
243
Michal Simek5d0449d2015-12-08 16:10:05 +0100244static int axiemac_phy_init(struct udevice *dev)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000245{
246 u16 phyreg;
Michal Simek5d0449d2015-12-08 16:10:05 +0100247 u32 i, ret;
Michal Simek75cc93f2015-12-08 15:44:41 +0100248 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek6609f352015-12-09 14:39:42 +0100249 struct axi_regs *regs = priv->iobase;
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000250 struct phy_device *phydev;
251
252 u32 supported = SUPPORTED_10baseT_Half |
253 SUPPORTED_10baseT_Full |
254 SUPPORTED_100baseT_Half |
255 SUPPORTED_100baseT_Full |
256 SUPPORTED_1000baseT_Half |
257 SUPPORTED_1000baseT_Full;
258
Michal Simek5d0449d2015-12-08 16:10:05 +0100259 /* Set default MDIO divisor */
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530260 writel(XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK, &regs->mdio_mc);
Michal Simek5d0449d2015-12-08 16:10:05 +0100261
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000262 if (priv->phyaddr == -1) {
263 /* Detect the PHY address */
264 for (i = 31; i >= 0; i--) {
Michal Simek0d78abf2015-12-09 14:44:38 +0100265 ret = phyread(priv, i, PHY_DETECT_REG, &phyreg);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000266 if (!ret && (phyreg != 0xFFFF) &&
267 ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
268 /* Found a valid PHY address */
269 priv->phyaddr = i;
270 debug("axiemac: Found valid phy address, %x\n",
Michal Simek2652a622015-12-09 10:54:53 +0100271 i);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000272 break;
273 }
274 }
275 }
276
277 /* Interface - look at tsec */
Siva Durga Prasad Paladugu9c0da762016-02-21 15:46:14 +0530278 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000279
280 phydev->supported &= supported;
281 phydev->advertising = phydev->supported;
282 priv->phydev = phydev;
Siva Durga Prasad Paladugufccfb712019-03-15 17:46:45 +0530283 if (priv->phy_of_handle)
284 priv->phydev->node = offset_to_ofnode(priv->phy_of_handle);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000285 phy_config(phydev);
Michal Simek5d0449d2015-12-08 16:10:05 +0100286
287 return 0;
288}
289
290/* Setting axi emac and phy to proper setting */
291static int setup_phy(struct udevice *dev)
292{
Siva Durga Prasad Paladugu8964f242016-02-21 15:46:15 +0530293 u16 temp;
294 u32 speed, emmc_reg, ret;
Michal Simek5d0449d2015-12-08 16:10:05 +0100295 struct axidma_priv *priv = dev_get_priv(dev);
296 struct axi_regs *regs = priv->iobase;
297 struct phy_device *phydev = priv->phydev;
298
Siva Durga Prasad Paladugu8964f242016-02-21 15:46:15 +0530299 if (priv->interface == PHY_INTERFACE_MODE_SGMII) {
300 /*
301 * In SGMII cases the isolate bit might set
302 * after DMA and ethernet resets and hence
303 * check and clear if set.
304 */
305 ret = phyread(priv, priv->phyaddr, MII_BMCR, &temp);
306 if (ret)
307 return 0;
308 if (temp & BMCR_ISOLATE) {
309 temp &= ~BMCR_ISOLATE;
310 ret = phywrite(priv, priv->phyaddr, MII_BMCR, temp);
311 if (ret)
312 return 0;
313 }
314 }
315
Timur Tabi11af8d62012-07-09 08:52:43 +0000316 if (phy_startup(phydev)) {
317 printf("axiemac: could not initialize PHY %s\n",
318 phydev->dev->name);
319 return 0;
320 }
Michal Simek6f9b9372013-11-21 16:15:51 +0100321 if (!phydev->link) {
322 printf("%s: No link.\n", phydev->dev->name);
323 return 0;
324 }
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000325
326 switch (phydev->speed) {
327 case 1000:
328 speed = XAE_EMMC_LINKSPD_1000;
329 break;
330 case 100:
331 speed = XAE_EMMC_LINKSPD_100;
332 break;
333 case 10:
334 speed = XAE_EMMC_LINKSPD_10;
335 break;
336 default:
337 return 0;
338 }
339
340 /* Setup the emac for the phy speed */
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530341 emmc_reg = readl(&regs->emmc);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000342 emmc_reg &= ~XAE_EMMC_LINKSPEED_MASK;
343 emmc_reg |= speed;
344
345 /* Write new speed setting out to Axi Ethernet */
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530346 writel(emmc_reg, &regs->emmc);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000347
348 /*
349 * Setting the operating speed of the MAC needs a delay. There
350 * doesn't seem to be register to poll, so please consider this
351 * during your application design.
352 */
353 udelay(1);
354
355 return 1;
356}
357
358/* STOP DMA transfers */
Michal Simekad499e42015-12-16 09:18:12 +0100359static void axiemac_stop(struct udevice *dev)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000360{
Michal Simek75cc93f2015-12-08 15:44:41 +0100361 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000362 u32 temp;
363
364 /* Stop the hardware */
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530365 temp = readl(&priv->dmatx->control);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000366 temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530367 writel(temp, &priv->dmatx->control);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000368
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530369 temp = readl(&priv->dmarx->control);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000370 temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530371 writel(temp, &priv->dmarx->control);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000372
373 debug("axiemac: Halted\n");
374}
375
Michal Simekf0985482015-12-09 14:53:51 +0100376static int axi_ethernet_init(struct axidma_priv *priv)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000377{
Michal Simekf0985482015-12-09 14:53:51 +0100378 struct axi_regs *regs = priv->iobase;
Siva Durga Prasad Paladugud02a0b12017-01-06 16:18:50 +0530379 int err;
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000380
381 /*
382 * Check the status of the MgtRdy bit in the interrupt status
383 * registers. This must be done to allow the MGT clock to become stable
384 * for the Sgmii and 1000BaseX PHY interfaces. No other register reads
385 * will be valid until this bit is valid.
386 * The bit is always a 1 for all other PHY interfaces.
Siva Durga Prasad Paladugu89ce5a92017-01-06 16:27:15 +0530387 * Interrupt status and enable registers are not available in non
388 * processor mode and hence bypass in this mode
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000389 */
Siva Durga Prasad Paladugu89ce5a92017-01-06 16:27:15 +0530390 if (!priv->eth_hasnobuf) {
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +0100391 err = wait_for_bit_le32(&regs->is, XAE_INT_MGTRDY_MASK,
392 true, 200, false);
Siva Durga Prasad Paladugu89ce5a92017-01-06 16:27:15 +0530393 if (err) {
394 printf("%s: Timeout\n", __func__);
395 return 1;
396 }
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000397
Siva Durga Prasad Paladugu89ce5a92017-01-06 16:27:15 +0530398 /*
399 * Stop the device and reset HW
400 * Disable interrupts
401 */
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530402 writel(0, &regs->ie);
Siva Durga Prasad Paladugu89ce5a92017-01-06 16:27:15 +0530403 }
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000404
405 /* Disable the receiver */
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530406 writel(readl(&regs->rcw1) & ~XAE_RCW1_RX_MASK, &regs->rcw1);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000407
408 /*
409 * Stopping the receiver in mid-packet causes a dropped packet
410 * indication from HW. Clear it.
411 */
Siva Durga Prasad Paladugu89ce5a92017-01-06 16:27:15 +0530412 if (!priv->eth_hasnobuf) {
413 /* Set the interrupt status register to clear the interrupt */
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530414 writel(XAE_INT_RXRJECT_MASK, &regs->is);
Siva Durga Prasad Paladugu89ce5a92017-01-06 16:27:15 +0530415 }
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000416
417 /* Setup HW */
418 /* Set default MDIO divisor */
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530419 writel(XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK, &regs->mdio_mc);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000420
421 debug("axiemac: InitHw done\n");
422 return 0;
423}
424
Michal Simekad499e42015-12-16 09:18:12 +0100425static int axiemac_write_hwaddr(struct udevice *dev)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000426{
Michal Simek75cc93f2015-12-08 15:44:41 +0100427 struct eth_pdata *pdata = dev_get_platdata(dev);
428 struct axidma_priv *priv = dev_get_priv(dev);
429 struct axi_regs *regs = priv->iobase;
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000430
431 /* Set the MAC address */
Michal Simek75cc93f2015-12-08 15:44:41 +0100432 int val = ((pdata->enetaddr[3] << 24) | (pdata->enetaddr[2] << 16) |
433 (pdata->enetaddr[1] << 8) | (pdata->enetaddr[0]));
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530434 writel(val, &regs->uaw0);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000435
Michal Simek75cc93f2015-12-08 15:44:41 +0100436 val = (pdata->enetaddr[5] << 8) | pdata->enetaddr[4];
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530437 val |= readl(&regs->uaw1) & ~XAE_UAW1_UNICASTADDR_MASK;
438 writel(val, &regs->uaw1);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000439 return 0;
440}
441
442/* Reset DMA engine */
Michal Simekf0985482015-12-09 14:53:51 +0100443static void axi_dma_init(struct axidma_priv *priv)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000444{
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000445 u32 timeout = 500;
446
447 /* Reset the engine so the hardware starts from a known state */
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530448 writel(XAXIDMA_CR_RESET_MASK, &priv->dmatx->control);
449 writel(XAXIDMA_CR_RESET_MASK, &priv->dmarx->control);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000450
451 /* At the initialization time, hardware should finish reset quickly */
452 while (timeout--) {
453 /* Check transmit/receive channel */
454 /* Reset is done when the reset bit is low */
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530455 if (!((readl(&priv->dmatx->control) |
456 readl(&priv->dmarx->control))
Michal Simek3e3f8ba2015-10-28 11:00:47 +0100457 & XAXIDMA_CR_RESET_MASK)) {
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000458 break;
459 }
460 }
461 if (!timeout)
462 printf("%s: Timeout\n", __func__);
463}
464
Michal Simekad499e42015-12-16 09:18:12 +0100465static int axiemac_start(struct udevice *dev)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000466{
Michal Simek75cc93f2015-12-08 15:44:41 +0100467 struct axidma_priv *priv = dev_get_priv(dev);
468 struct axi_regs *regs = priv->iobase;
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000469 u32 temp;
470
471 debug("axiemac: Init started\n");
472 /*
473 * Initialize AXIDMA engine. AXIDMA engine must be initialized before
474 * AxiEthernet. During AXIDMA engine initialization, AXIDMA hardware is
475 * reset, and since AXIDMA reset line is connected to AxiEthernet, this
476 * would ensure a reset of AxiEthernet.
477 */
Michal Simekf0985482015-12-09 14:53:51 +0100478 axi_dma_init(priv);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000479
480 /* Initialize AxiEthernet hardware. */
Michal Simekf0985482015-12-09 14:53:51 +0100481 if (axi_ethernet_init(priv))
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000482 return -1;
483
484 /* Disable all RX interrupts before RxBD space setup */
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530485 temp = readl(&priv->dmarx->control);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000486 temp &= ~XAXIDMA_IRQ_ALL_MASK;
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530487 writel(temp, &priv->dmarx->control);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000488
489 /* Start DMA RX channel. Now it's ready to receive data.*/
Vipul Kumar047f3bf2018-01-23 14:52:35 +0530490 axienet_dma_write(&rx_bd, &priv->dmarx->current);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000491
492 /* Setup the BD. */
493 memset(&rx_bd, 0, sizeof(rx_bd));
494 rx_bd.next = (u32)&rx_bd;
495 rx_bd.phys = (u32)&rxframe;
496 rx_bd.cntrl = sizeof(rxframe);
497 /* Flush the last BD so DMA core could see the updates */
498 flush_cache((u32)&rx_bd, sizeof(rx_bd));
499
500 /* It is necessary to flush rxframe because if you don't do it
501 * then cache can contain uninitialized data */
502 flush_cache((u32)&rxframe, sizeof(rxframe));
503
504 /* Start the hardware */
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530505 temp = readl(&priv->dmarx->control);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000506 temp |= XAXIDMA_CR_RUNSTOP_MASK;
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530507 writel(temp, &priv->dmarx->control);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000508
509 /* Rx BD is ready - start */
Vipul Kumar047f3bf2018-01-23 14:52:35 +0530510 axienet_dma_write(&rx_bd, &priv->dmarx->tail);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000511
512 /* Enable TX */
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530513 writel(XAE_TC_TX_MASK, &regs->tc);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000514 /* Enable RX */
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530515 writel(XAE_RCW1_RX_MASK, &regs->rcw1);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000516
517 /* PHY setup */
518 if (!setup_phy(dev)) {
Michal Simekad499e42015-12-16 09:18:12 +0100519 axiemac_stop(dev);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000520 return -1;
521 }
522
523 debug("axiemac: Init complete\n");
524 return 0;
525}
526
Michal Simek75cc93f2015-12-08 15:44:41 +0100527static int axiemac_send(struct udevice *dev, void *ptr, int len)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000528{
Michal Simek75cc93f2015-12-08 15:44:41 +0100529 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000530 u32 timeout;
531
532 if (len > PKTSIZE_ALIGN)
533 len = PKTSIZE_ALIGN;
534
535 /* Flush packet to main memory to be trasfered by DMA */
536 flush_cache((u32)ptr, len);
537
538 /* Setup Tx BD */
539 memset(&tx_bd, 0, sizeof(tx_bd));
540 /* At the end of the ring, link the last BD back to the top */
541 tx_bd.next = (u32)&tx_bd;
542 tx_bd.phys = (u32)ptr;
543 /* Save len */
544 tx_bd.cntrl = len | XAXIDMA_BD_CTRL_TXSOF_MASK |
545 XAXIDMA_BD_CTRL_TXEOF_MASK;
546
547 /* Flush the last BD so DMA core could see the updates */
548 flush_cache((u32)&tx_bd, sizeof(tx_bd));
549
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530550 if (readl(&priv->dmatx->status) & XAXIDMA_HALTED_MASK) {
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000551 u32 temp;
Vipul Kumar047f3bf2018-01-23 14:52:35 +0530552 axienet_dma_write(&tx_bd, &priv->dmatx->current);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000553 /* Start the hardware */
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530554 temp = readl(&priv->dmatx->control);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000555 temp |= XAXIDMA_CR_RUNSTOP_MASK;
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530556 writel(temp, &priv->dmatx->control);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000557 }
558
559 /* Start transfer */
Vipul Kumar047f3bf2018-01-23 14:52:35 +0530560 axienet_dma_write(&tx_bd, &priv->dmatx->tail);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000561
562 /* Wait for transmission to complete */
563 debug("axiemac: Waiting for tx to be done\n");
564 timeout = 200;
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530565 while (timeout && (!(readl(&priv->dmatx->status) &
Michal Simek3e3f8ba2015-10-28 11:00:47 +0100566 (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))) {
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000567 timeout--;
568 udelay(1);
569 }
570 if (!timeout) {
571 printf("%s: Timeout\n", __func__);
572 return 1;
573 }
574
575 debug("axiemac: Sending complete\n");
576 return 0;
577}
578
Michal Simekf0985482015-12-09 14:53:51 +0100579static int isrxready(struct axidma_priv *priv)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000580{
581 u32 status;
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000582
583 /* Read pending interrupts */
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530584 status = readl(&priv->dmarx->status);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000585
586 /* Acknowledge pending interrupts */
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530587 writel(status & XAXIDMA_IRQ_ALL_MASK, &priv->dmarx->status);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000588
589 /*
590 * If Reception done interrupt is asserted, call RX call back function
591 * to handle the processed BDs and then raise the according flag.
592 */
593 if ((status & (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))
594 return 1;
595
596 return 0;
597}
598
Michal Simek75cc93f2015-12-08 15:44:41 +0100599static int axiemac_recv(struct udevice *dev, int flags, uchar **packetp)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000600{
601 u32 length;
Michal Simek75cc93f2015-12-08 15:44:41 +0100602 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000603 u32 temp;
604
605 /* Wait for an incoming packet */
Michal Simekf0985482015-12-09 14:53:51 +0100606 if (!isrxready(priv))
Michal Simek75cc93f2015-12-08 15:44:41 +0100607 return -1;
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000608
609 debug("axiemac: RX data ready\n");
610
611 /* Disable IRQ for a moment till packet is handled */
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530612 temp = readl(&priv->dmarx->control);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000613 temp &= ~XAXIDMA_IRQ_ALL_MASK;
Siva Durga Prasad Paladugua04a5da2017-11-23 12:23:12 +0530614 writel(temp, &priv->dmarx->control);
Siva Durga Prasad Paladugu89ce5a92017-01-06 16:27:15 +0530615 if (!priv->eth_hasnobuf)
616 length = rx_bd.app4 & 0xFFFF; /* max length mask */
617 else
618 length = rx_bd.status & XAXIDMA_BD_STS_ACTUAL_LEN_MASK;
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000619
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000620#ifdef DEBUG
621 print_buffer(&rxframe, &rxframe[0], 1, length, 16);
622#endif
Michal Simek97d23632015-12-09 14:13:23 +0100623
624 *packetp = rxframe;
625 return length;
626}
627
628static int axiemac_free_pkt(struct udevice *dev, uchar *packet, int length)
629{
630 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000631
632#ifdef DEBUG
633 /* It is useful to clear buffer to be sure that it is consistent */
634 memset(rxframe, 0, sizeof(rxframe));
635#endif
636 /* Setup RxBD */
637 /* Clear the whole buffer and setup it again - all flags are cleared */
638 memset(&rx_bd, 0, sizeof(rx_bd));
639 rx_bd.next = (u32)&rx_bd;
640 rx_bd.phys = (u32)&rxframe;
641 rx_bd.cntrl = sizeof(rxframe);
642
643 /* Write bd to HW */
644 flush_cache((u32)&rx_bd, sizeof(rx_bd));
645
646 /* It is necessary to flush rxframe because if you don't do it
647 * then cache will contain previous packet */
648 flush_cache((u32)&rxframe, sizeof(rxframe));
649
650 /* Rx BD is ready - start again */
Vipul Kumar047f3bf2018-01-23 14:52:35 +0530651 axienet_dma_write(&rx_bd, &priv->dmarx->tail);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000652
653 debug("axiemac: RX completed, framelength = %d\n", length);
654
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000655 return 0;
656}
657
Michal Simek75cc93f2015-12-08 15:44:41 +0100658static int axiemac_miiphy_read(struct mii_dev *bus, int addr,
659 int devad, int reg)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000660{
Michal Simek75cc93f2015-12-08 15:44:41 +0100661 int ret;
662 u16 value;
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000663
Michal Simek75cc93f2015-12-08 15:44:41 +0100664 ret = phyread(bus->priv, addr, reg, &value);
665 debug("axiemac: Read MII 0x%x, 0x%x, 0x%x, %d\n", addr, reg,
666 value, ret);
667 return value;
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000668}
Michal Simek75cc93f2015-12-08 15:44:41 +0100669
670static int axiemac_miiphy_write(struct mii_dev *bus, int addr, int devad,
671 int reg, u16 value)
672{
673 debug("axiemac: Write MII 0x%x, 0x%x, 0x%x\n", addr, reg, value);
674 return phywrite(bus->priv, addr, reg, value);
675}
676
677static int axi_emac_probe(struct udevice *dev)
678{
679 struct axidma_priv *priv = dev_get_priv(dev);
680 int ret;
681
682 priv->bus = mdio_alloc();
683 priv->bus->read = axiemac_miiphy_read;
684 priv->bus->write = axiemac_miiphy_write;
685 priv->bus->priv = priv;
Michal Simek75cc93f2015-12-08 15:44:41 +0100686
Michal Simek6516e3f2016-12-08 10:25:44 +0100687 ret = mdio_register_seq(priv->bus, dev->seq);
Michal Simek75cc93f2015-12-08 15:44:41 +0100688 if (ret)
689 return ret;
690
Michal Simek5d0449d2015-12-08 16:10:05 +0100691 axiemac_phy_init(dev);
692
Michal Simek75cc93f2015-12-08 15:44:41 +0100693 return 0;
694}
695
696static int axi_emac_remove(struct udevice *dev)
697{
698 struct axidma_priv *priv = dev_get_priv(dev);
699
700 free(priv->phydev);
701 mdio_unregister(priv->bus);
702 mdio_free(priv->bus);
703
704 return 0;
705}
706
707static const struct eth_ops axi_emac_ops = {
Michal Simekad499e42015-12-16 09:18:12 +0100708 .start = axiemac_start,
Michal Simek75cc93f2015-12-08 15:44:41 +0100709 .send = axiemac_send,
710 .recv = axiemac_recv,
Michal Simek97d23632015-12-09 14:13:23 +0100711 .free_pkt = axiemac_free_pkt,
Michal Simekad499e42015-12-16 09:18:12 +0100712 .stop = axiemac_stop,
713 .write_hwaddr = axiemac_write_hwaddr,
Michal Simek75cc93f2015-12-08 15:44:41 +0100714};
715
716static int axi_emac_ofdata_to_platdata(struct udevice *dev)
717{
718 struct eth_pdata *pdata = dev_get_platdata(dev);
719 struct axidma_priv *priv = dev_get_priv(dev);
Simon Glasse160f7d2017-01-17 16:52:55 -0700720 int node = dev_of_offset(dev);
Michal Simek75cc93f2015-12-08 15:44:41 +0100721 int offset = 0;
722 const char *phy_mode;
723
Simon Glassa821c4a2017-05-17 17:18:05 -0600724 pdata->iobase = (phys_addr_t)devfdt_get_addr(dev);
Michal Simek75cc93f2015-12-08 15:44:41 +0100725 priv->iobase = (struct axi_regs *)pdata->iobase;
726
Simon Glasse160f7d2017-01-17 16:52:55 -0700727 offset = fdtdec_lookup_phandle(gd->fdt_blob, node,
Michal Simek75cc93f2015-12-08 15:44:41 +0100728 "axistream-connected");
729 if (offset <= 0) {
730 printf("%s: axistream is not found\n", __func__);
731 return -EINVAL;
732 }
Siva Durga Prasad Paladugudc1fcc42017-06-22 11:14:55 +0530733 priv->dmatx = (struct axidma_reg *)fdtdec_get_addr(gd->fdt_blob,
734 offset, "reg");
Michal Simek75cc93f2015-12-08 15:44:41 +0100735 if (!priv->dmatx) {
736 printf("%s: axi_dma register space not found\n", __func__);
737 return -EINVAL;
738 }
739 /* RX channel offset is 0x30 */
740 priv->dmarx = (struct axidma_reg *)((u32)priv->dmatx + 0x30);
741
742 priv->phyaddr = -1;
743
Simon Glasse160f7d2017-01-17 16:52:55 -0700744 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
Siva Durga Prasad Paladugufccfb712019-03-15 17:46:45 +0530745 if (offset > 0) {
Michal Simek75cc93f2015-12-08 15:44:41 +0100746 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
Siva Durga Prasad Paladugufccfb712019-03-15 17:46:45 +0530747 priv->phy_of_handle = offset;
748 }
Michal Simek75cc93f2015-12-08 15:44:41 +0100749
Simon Glasse160f7d2017-01-17 16:52:55 -0700750 phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
Michal Simek75cc93f2015-12-08 15:44:41 +0100751 if (phy_mode)
752 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
753 if (pdata->phy_interface == -1) {
Michal Simekceb04e12016-02-08 13:54:05 +0100754 printf("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
Michal Simek75cc93f2015-12-08 15:44:41 +0100755 return -EINVAL;
756 }
757 priv->interface = pdata->phy_interface;
758
Siva Durga Prasad Paladugu89ce5a92017-01-06 16:27:15 +0530759 priv->eth_hasnobuf = fdtdec_get_bool(gd->fdt_blob, node,
760 "xlnx,eth-hasnobuf");
761
Michal Simek75cc93f2015-12-08 15:44:41 +0100762 printf("AXI EMAC: %lx, phyaddr %d, interface %s\n", (ulong)priv->iobase,
763 priv->phyaddr, phy_string_for_interface(priv->interface));
764
765 return 0;
766}
767
768static const struct udevice_id axi_emac_ids[] = {
769 { .compatible = "xlnx,axi-ethernet-1.00.a" },
770 { }
771};
772
773U_BOOT_DRIVER(axi_emac) = {
774 .name = "axi_emac",
775 .id = UCLASS_ETH,
776 .of_match = axi_emac_ids,
777 .ofdata_to_platdata = axi_emac_ofdata_to_platdata,
778 .probe = axi_emac_probe,
779 .remove = axi_emac_remove,
780 .ops = &axi_emac_ops,
781 .priv_auto_alloc_size = sizeof(struct axidma_priv),
782 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
783};