blob: 81274ee13bca869567ecc3986f4f90fe8c071f7d [file] [log] [blame]
Michal Simek4f1ec4c2011-10-06 20:35:35 +00001/*
2 * Copyright (C) 2011 Michal Simek <monstr@monstr.eu>
3 * Copyright (C) 2011 PetaLogix
4 * Copyright (C) 2010 Xilinx, Inc. All rights reserved.
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Michal Simek4f1ec4c2011-10-06 20:35:35 +00007 */
8
9#include <config.h>
10#include <common.h>
Michal Simek75cc93f2015-12-08 15:44:41 +010011#include <dm.h>
Michal Simek4f1ec4c2011-10-06 20:35:35 +000012#include <net.h>
13#include <malloc.h>
14#include <asm/io.h>
15#include <phy.h>
16#include <miiphy.h>
17
Michal Simek75cc93f2015-12-08 15:44:41 +010018DECLARE_GLOBAL_DATA_PTR;
19
Michal Simek4f1ec4c2011-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
53/* DMA macros */
54/* Bitmasks of XAXIDMA_CR_OFFSET register */
55#define XAXIDMA_CR_RUNSTOP_MASK 0x00000001 /* Start/stop DMA channel */
56#define XAXIDMA_CR_RESET_MASK 0x00000004 /* Reset DMA engine */
57
58/* Bitmasks of XAXIDMA_SR_OFFSET register */
59#define XAXIDMA_HALTED_MASK 0x00000001 /* DMA channel halted */
60
61/* Bitmask for interrupts */
62#define XAXIDMA_IRQ_IOC_MASK 0x00001000 /* Completion intr */
63#define XAXIDMA_IRQ_DELAY_MASK 0x00002000 /* Delay interrupt */
64#define XAXIDMA_IRQ_ALL_MASK 0x00007000 /* All interrupts */
65
66/* Bitmasks of XAXIDMA_BD_CTRL_OFFSET register */
67#define XAXIDMA_BD_CTRL_TXSOF_MASK 0x08000000 /* First tx packet */
68#define XAXIDMA_BD_CTRL_TXEOF_MASK 0x04000000 /* Last tx packet */
69
70#define DMAALIGN 128
71
72static u8 rxframe[PKTSIZE_ALIGN] __attribute((aligned(DMAALIGN)));
73
74/* Reflect dma offsets */
75struct axidma_reg {
76 u32 control; /* DMACR */
77 u32 status; /* DMASR */
78 u32 current; /* CURDESC */
79 u32 reserved;
80 u32 tail; /* TAILDESC */
81};
82
83/* Private driver structures */
84struct axidma_priv {
85 struct axidma_reg *dmatx;
86 struct axidma_reg *dmarx;
87 int phyaddr;
Michal Simek6609f352015-12-09 14:39:42 +010088 struct axi_regs *iobase;
Michal Simek75cc93f2015-12-08 15:44:41 +010089 phy_interface_t interface;
Michal Simek4f1ec4c2011-10-06 20:35:35 +000090 struct phy_device *phydev;
91 struct mii_dev *bus;
92};
93
94/* BD descriptors */
95struct axidma_bd {
96 u32 next; /* Next descriptor pointer */
97 u32 reserved1;
98 u32 phys; /* Buffer address */
99 u32 reserved2;
100 u32 reserved3;
101 u32 reserved4;
102 u32 cntrl; /* Control */
103 u32 status; /* Status */
104 u32 app0;
105 u32 app1; /* TX start << 16 | insert */
106 u32 app2; /* TX csum seed */
107 u32 app3;
108 u32 app4;
109 u32 sw_id_offset;
110 u32 reserved5;
111 u32 reserved6;
112};
113
114/* Static BDs - driver uses only one BD */
115static struct axidma_bd tx_bd __attribute((aligned(DMAALIGN)));
116static struct axidma_bd rx_bd __attribute((aligned(DMAALIGN)));
117
118struct axi_regs {
119 u32 reserved[3];
120 u32 is; /* 0xC: Interrupt status */
121 u32 reserved2;
122 u32 ie; /* 0x14: Interrupt enable */
123 u32 reserved3[251];
124 u32 rcw1; /* 0x404: Rx Configuration Word 1 */
125 u32 tc; /* 0x408: Tx Configuration */
126 u32 reserved4;
127 u32 emmc; /* 0x410: EMAC mode configuration */
128 u32 reserved5[59];
129 u32 mdio_mc; /* 0x500: MII Management Config */
130 u32 mdio_mcr; /* 0x504: MII Management Control */
131 u32 mdio_mwd; /* 0x508: MII Management Write Data */
132 u32 mdio_mrd; /* 0x50C: MII Management Read Data */
133 u32 reserved6[124];
134 u32 uaw0; /* 0x700: Unicast address word 0 */
135 u32 uaw1; /* 0x704: Unicast address word 1 */
136};
137
138/* Use MII register 1 (MII status register) to detect PHY */
139#define PHY_DETECT_REG 1
140
141/*
142 * Mask used to verify certain PHY features (or register contents)
143 * in the register above:
144 * 0x1000: 10Mbps full duplex support
145 * 0x0800: 10Mbps half duplex support
146 * 0x0008: Auto-negotiation support
147 */
148#define PHY_DETECT_MASK 0x1808
149
Michal Simekf36bbcc2015-12-09 14:36:31 +0100150static inline int mdio_wait(struct axi_regs *regs)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000151{
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000152 u32 timeout = 200;
153
154 /* Wait till MDIO interface is ready to accept a new transaction. */
155 while (timeout && (!(in_be32(&regs->mdio_mcr)
156 & XAE_MDIO_MCR_READY_MASK))) {
157 timeout--;
158 udelay(1);
159 }
160 if (!timeout) {
161 printf("%s: Timeout\n", __func__);
162 return 1;
163 }
164 return 0;
165}
166
Michal Simek0d78abf2015-12-09 14:44:38 +0100167static u32 phyread(struct axidma_priv *priv, u32 phyaddress, u32 registernum,
168 u16 *val)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000169{
Michal Simek0d78abf2015-12-09 14:44:38 +0100170 struct axi_regs *regs = priv->iobase;
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000171 u32 mdioctrlreg = 0;
172
Michal Simekf36bbcc2015-12-09 14:36:31 +0100173 if (mdio_wait(regs))
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000174 return 1;
175
176 mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) &
177 XAE_MDIO_MCR_PHYAD_MASK) |
178 ((registernum << XAE_MDIO_MCR_REGAD_SHIFT)
179 & XAE_MDIO_MCR_REGAD_MASK) |
180 XAE_MDIO_MCR_INITIATE_MASK |
181 XAE_MDIO_MCR_OP_READ_MASK;
182
183 out_be32(&regs->mdio_mcr, mdioctrlreg);
184
Michal Simekf36bbcc2015-12-09 14:36:31 +0100185 if (mdio_wait(regs))
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000186 return 1;
187
188 /* Read data */
189 *val = in_be32(&regs->mdio_mrd);
190 return 0;
191}
192
Michal Simek0d78abf2015-12-09 14:44:38 +0100193static u32 phywrite(struct axidma_priv *priv, u32 phyaddress, u32 registernum,
194 u32 data)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000195{
Michal Simek0d78abf2015-12-09 14:44:38 +0100196 struct axi_regs *regs = priv->iobase;
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000197 u32 mdioctrlreg = 0;
198
Michal Simekf36bbcc2015-12-09 14:36:31 +0100199 if (mdio_wait(regs))
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000200 return 1;
201
202 mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) &
203 XAE_MDIO_MCR_PHYAD_MASK) |
204 ((registernum << XAE_MDIO_MCR_REGAD_SHIFT)
205 & XAE_MDIO_MCR_REGAD_MASK) |
206 XAE_MDIO_MCR_INITIATE_MASK |
207 XAE_MDIO_MCR_OP_WRITE_MASK;
208
209 /* Write data */
210 out_be32(&regs->mdio_mwd, data);
211
212 out_be32(&regs->mdio_mcr, mdioctrlreg);
213
Michal Simekf36bbcc2015-12-09 14:36:31 +0100214 if (mdio_wait(regs))
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000215 return 1;
216
217 return 0;
218}
219
Michal Simek5d0449d2015-12-08 16:10:05 +0100220static int axiemac_phy_init(struct udevice *dev)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000221{
222 u16 phyreg;
Michal Simek5d0449d2015-12-08 16:10:05 +0100223 u32 i, ret;
Michal Simek75cc93f2015-12-08 15:44:41 +0100224 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek6609f352015-12-09 14:39:42 +0100225 struct axi_regs *regs = priv->iobase;
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000226 struct phy_device *phydev;
227
228 u32 supported = SUPPORTED_10baseT_Half |
229 SUPPORTED_10baseT_Full |
230 SUPPORTED_100baseT_Half |
231 SUPPORTED_100baseT_Full |
232 SUPPORTED_1000baseT_Half |
233 SUPPORTED_1000baseT_Full;
234
Michal Simek5d0449d2015-12-08 16:10:05 +0100235 /* Set default MDIO divisor */
236 out_be32(&regs->mdio_mc, XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK);
237
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000238 if (priv->phyaddr == -1) {
239 /* Detect the PHY address */
240 for (i = 31; i >= 0; i--) {
Michal Simek0d78abf2015-12-09 14:44:38 +0100241 ret = phyread(priv, i, PHY_DETECT_REG, &phyreg);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000242 if (!ret && (phyreg != 0xFFFF) &&
243 ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
244 /* Found a valid PHY address */
245 priv->phyaddr = i;
246 debug("axiemac: Found valid phy address, %x\n",
Michal Simek2652a622015-12-09 10:54:53 +0100247 i);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000248 break;
249 }
250 }
251 }
252
253 /* Interface - look at tsec */
254 phydev = phy_connect(priv->bus, priv->phyaddr, dev, 0);
255
256 phydev->supported &= supported;
257 phydev->advertising = phydev->supported;
258 priv->phydev = phydev;
259 phy_config(phydev);
Michal Simek5d0449d2015-12-08 16:10:05 +0100260
261 return 0;
262}
263
264/* Setting axi emac and phy to proper setting */
265static int setup_phy(struct udevice *dev)
266{
267 u32 speed, emmc_reg;
268 struct axidma_priv *priv = dev_get_priv(dev);
269 struct axi_regs *regs = priv->iobase;
270 struct phy_device *phydev = priv->phydev;
271
Timur Tabi11af8d62012-07-09 08:52:43 +0000272 if (phy_startup(phydev)) {
273 printf("axiemac: could not initialize PHY %s\n",
274 phydev->dev->name);
275 return 0;
276 }
Michal Simek6f9b9372013-11-21 16:15:51 +0100277 if (!phydev->link) {
278 printf("%s: No link.\n", phydev->dev->name);
279 return 0;
280 }
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000281
282 switch (phydev->speed) {
283 case 1000:
284 speed = XAE_EMMC_LINKSPD_1000;
285 break;
286 case 100:
287 speed = XAE_EMMC_LINKSPD_100;
288 break;
289 case 10:
290 speed = XAE_EMMC_LINKSPD_10;
291 break;
292 default:
293 return 0;
294 }
295
296 /* Setup the emac for the phy speed */
297 emmc_reg = in_be32(&regs->emmc);
298 emmc_reg &= ~XAE_EMMC_LINKSPEED_MASK;
299 emmc_reg |= speed;
300
301 /* Write new speed setting out to Axi Ethernet */
302 out_be32(&regs->emmc, emmc_reg);
303
304 /*
305 * Setting the operating speed of the MAC needs a delay. There
306 * doesn't seem to be register to poll, so please consider this
307 * during your application design.
308 */
309 udelay(1);
310
311 return 1;
312}
313
314/* STOP DMA transfers */
Michal Simekad499e42015-12-16 09:18:12 +0100315static void axiemac_stop(struct udevice *dev)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000316{
Michal Simek75cc93f2015-12-08 15:44:41 +0100317 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000318 u32 temp;
319
320 /* Stop the hardware */
321 temp = in_be32(&priv->dmatx->control);
322 temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
323 out_be32(&priv->dmatx->control, temp);
324
325 temp = in_be32(&priv->dmarx->control);
326 temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
327 out_be32(&priv->dmarx->control, temp);
328
329 debug("axiemac: Halted\n");
330}
331
Michal Simekf0985482015-12-09 14:53:51 +0100332static int axi_ethernet_init(struct axidma_priv *priv)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000333{
Michal Simekf0985482015-12-09 14:53:51 +0100334 struct axi_regs *regs = priv->iobase;
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000335 u32 timeout = 200;
336
337 /*
338 * Check the status of the MgtRdy bit in the interrupt status
339 * registers. This must be done to allow the MGT clock to become stable
340 * for the Sgmii and 1000BaseX PHY interfaces. No other register reads
341 * will be valid until this bit is valid.
342 * The bit is always a 1 for all other PHY interfaces.
343 */
344 while (timeout && (!(in_be32(&regs->is) & XAE_INT_MGTRDY_MASK))) {
345 timeout--;
346 udelay(1);
347 }
348 if (!timeout) {
349 printf("%s: Timeout\n", __func__);
350 return 1;
351 }
352
353 /* Stop the device and reset HW */
354 /* Disable interrupts */
355 out_be32(&regs->ie, 0);
356
357 /* Disable the receiver */
358 out_be32(&regs->rcw1, in_be32(&regs->rcw1) & ~XAE_RCW1_RX_MASK);
359
360 /*
361 * Stopping the receiver in mid-packet causes a dropped packet
362 * indication from HW. Clear it.
363 */
364 /* Set the interrupt status register to clear the interrupt */
365 out_be32(&regs->is, XAE_INT_RXRJECT_MASK);
366
367 /* Setup HW */
368 /* Set default MDIO divisor */
369 out_be32(&regs->mdio_mc, XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK);
370
371 debug("axiemac: InitHw done\n");
372 return 0;
373}
374
Michal Simekad499e42015-12-16 09:18:12 +0100375static int axiemac_write_hwaddr(struct udevice *dev)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000376{
Michal Simek75cc93f2015-12-08 15:44:41 +0100377 struct eth_pdata *pdata = dev_get_platdata(dev);
378 struct axidma_priv *priv = dev_get_priv(dev);
379 struct axi_regs *regs = priv->iobase;
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000380
381 /* Set the MAC address */
Michal Simek75cc93f2015-12-08 15:44:41 +0100382 int val = ((pdata->enetaddr[3] << 24) | (pdata->enetaddr[2] << 16) |
383 (pdata->enetaddr[1] << 8) | (pdata->enetaddr[0]));
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000384 out_be32(&regs->uaw0, val);
385
Michal Simek75cc93f2015-12-08 15:44:41 +0100386 val = (pdata->enetaddr[5] << 8) | pdata->enetaddr[4];
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000387 val |= in_be32(&regs->uaw1) & ~XAE_UAW1_UNICASTADDR_MASK;
388 out_be32(&regs->uaw1, val);
389 return 0;
390}
391
392/* Reset DMA engine */
Michal Simekf0985482015-12-09 14:53:51 +0100393static void axi_dma_init(struct axidma_priv *priv)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000394{
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000395 u32 timeout = 500;
396
397 /* Reset the engine so the hardware starts from a known state */
398 out_be32(&priv->dmatx->control, XAXIDMA_CR_RESET_MASK);
399 out_be32(&priv->dmarx->control, XAXIDMA_CR_RESET_MASK);
400
401 /* At the initialization time, hardware should finish reset quickly */
402 while (timeout--) {
403 /* Check transmit/receive channel */
404 /* Reset is done when the reset bit is low */
Michal Simek3e3f8ba2015-10-28 11:00:47 +0100405 if (!((in_be32(&priv->dmatx->control) |
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000406 in_be32(&priv->dmarx->control))
Michal Simek3e3f8ba2015-10-28 11:00:47 +0100407 & XAXIDMA_CR_RESET_MASK)) {
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000408 break;
409 }
410 }
411 if (!timeout)
412 printf("%s: Timeout\n", __func__);
413}
414
Michal Simekad499e42015-12-16 09:18:12 +0100415static int axiemac_start(struct udevice *dev)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000416{
Michal Simek75cc93f2015-12-08 15:44:41 +0100417 struct axidma_priv *priv = dev_get_priv(dev);
418 struct axi_regs *regs = priv->iobase;
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000419 u32 temp;
420
421 debug("axiemac: Init started\n");
422 /*
423 * Initialize AXIDMA engine. AXIDMA engine must be initialized before
424 * AxiEthernet. During AXIDMA engine initialization, AXIDMA hardware is
425 * reset, and since AXIDMA reset line is connected to AxiEthernet, this
426 * would ensure a reset of AxiEthernet.
427 */
Michal Simekf0985482015-12-09 14:53:51 +0100428 axi_dma_init(priv);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000429
430 /* Initialize AxiEthernet hardware. */
Michal Simekf0985482015-12-09 14:53:51 +0100431 if (axi_ethernet_init(priv))
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000432 return -1;
433
434 /* Disable all RX interrupts before RxBD space setup */
435 temp = in_be32(&priv->dmarx->control);
436 temp &= ~XAXIDMA_IRQ_ALL_MASK;
437 out_be32(&priv->dmarx->control, temp);
438
439 /* Start DMA RX channel. Now it's ready to receive data.*/
440 out_be32(&priv->dmarx->current, (u32)&rx_bd);
441
442 /* Setup the BD. */
443 memset(&rx_bd, 0, sizeof(rx_bd));
444 rx_bd.next = (u32)&rx_bd;
445 rx_bd.phys = (u32)&rxframe;
446 rx_bd.cntrl = sizeof(rxframe);
447 /* Flush the last BD so DMA core could see the updates */
448 flush_cache((u32)&rx_bd, sizeof(rx_bd));
449
450 /* It is necessary to flush rxframe because if you don't do it
451 * then cache can contain uninitialized data */
452 flush_cache((u32)&rxframe, sizeof(rxframe));
453
454 /* Start the hardware */
455 temp = in_be32(&priv->dmarx->control);
456 temp |= XAXIDMA_CR_RUNSTOP_MASK;
457 out_be32(&priv->dmarx->control, temp);
458
459 /* Rx BD is ready - start */
460 out_be32(&priv->dmarx->tail, (u32)&rx_bd);
461
462 /* Enable TX */
463 out_be32(&regs->tc, XAE_TC_TX_MASK);
464 /* Enable RX */
465 out_be32(&regs->rcw1, XAE_RCW1_RX_MASK);
466
467 /* PHY setup */
468 if (!setup_phy(dev)) {
Michal Simekad499e42015-12-16 09:18:12 +0100469 axiemac_stop(dev);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000470 return -1;
471 }
472
473 debug("axiemac: Init complete\n");
474 return 0;
475}
476
Michal Simek75cc93f2015-12-08 15:44:41 +0100477static int axiemac_send(struct udevice *dev, void *ptr, int len)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000478{
Michal Simek75cc93f2015-12-08 15:44:41 +0100479 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000480 u32 timeout;
481
482 if (len > PKTSIZE_ALIGN)
483 len = PKTSIZE_ALIGN;
484
485 /* Flush packet to main memory to be trasfered by DMA */
486 flush_cache((u32)ptr, len);
487
488 /* Setup Tx BD */
489 memset(&tx_bd, 0, sizeof(tx_bd));
490 /* At the end of the ring, link the last BD back to the top */
491 tx_bd.next = (u32)&tx_bd;
492 tx_bd.phys = (u32)ptr;
493 /* Save len */
494 tx_bd.cntrl = len | XAXIDMA_BD_CTRL_TXSOF_MASK |
495 XAXIDMA_BD_CTRL_TXEOF_MASK;
496
497 /* Flush the last BD so DMA core could see the updates */
498 flush_cache((u32)&tx_bd, sizeof(tx_bd));
499
500 if (in_be32(&priv->dmatx->status) & XAXIDMA_HALTED_MASK) {
501 u32 temp;
502 out_be32(&priv->dmatx->current, (u32)&tx_bd);
503 /* Start the hardware */
504 temp = in_be32(&priv->dmatx->control);
505 temp |= XAXIDMA_CR_RUNSTOP_MASK;
506 out_be32(&priv->dmatx->control, temp);
507 }
508
509 /* Start transfer */
510 out_be32(&priv->dmatx->tail, (u32)&tx_bd);
511
512 /* Wait for transmission to complete */
513 debug("axiemac: Waiting for tx to be done\n");
514 timeout = 200;
Michal Simek3e3f8ba2015-10-28 11:00:47 +0100515 while (timeout && (!(in_be32(&priv->dmatx->status) &
516 (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))) {
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000517 timeout--;
518 udelay(1);
519 }
520 if (!timeout) {
521 printf("%s: Timeout\n", __func__);
522 return 1;
523 }
524
525 debug("axiemac: Sending complete\n");
526 return 0;
527}
528
Michal Simekf0985482015-12-09 14:53:51 +0100529static int isrxready(struct axidma_priv *priv)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000530{
531 u32 status;
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000532
533 /* Read pending interrupts */
534 status = in_be32(&priv->dmarx->status);
535
536 /* Acknowledge pending interrupts */
537 out_be32(&priv->dmarx->status, status & XAXIDMA_IRQ_ALL_MASK);
538
539 /*
540 * If Reception done interrupt is asserted, call RX call back function
541 * to handle the processed BDs and then raise the according flag.
542 */
543 if ((status & (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))
544 return 1;
545
546 return 0;
547}
548
Michal Simek75cc93f2015-12-08 15:44:41 +0100549static int axiemac_recv(struct udevice *dev, int flags, uchar **packetp)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000550{
551 u32 length;
Michal Simek75cc93f2015-12-08 15:44:41 +0100552 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000553 u32 temp;
554
555 /* Wait for an incoming packet */
Michal Simekf0985482015-12-09 14:53:51 +0100556 if (!isrxready(priv))
Michal Simek75cc93f2015-12-08 15:44:41 +0100557 return -1;
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000558
559 debug("axiemac: RX data ready\n");
560
561 /* Disable IRQ for a moment till packet is handled */
562 temp = in_be32(&priv->dmarx->control);
563 temp &= ~XAXIDMA_IRQ_ALL_MASK;
564 out_be32(&priv->dmarx->control, temp);
565
566 length = rx_bd.app4 & 0xFFFF; /* max length mask */
567#ifdef DEBUG
568 print_buffer(&rxframe, &rxframe[0], 1, length, 16);
569#endif
Michal Simek97d23632015-12-09 14:13:23 +0100570
571 *packetp = rxframe;
572 return length;
573}
574
575static int axiemac_free_pkt(struct udevice *dev, uchar *packet, int length)
576{
577 struct axidma_priv *priv = dev_get_priv(dev);
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000578
579#ifdef DEBUG
580 /* It is useful to clear buffer to be sure that it is consistent */
581 memset(rxframe, 0, sizeof(rxframe));
582#endif
583 /* Setup RxBD */
584 /* Clear the whole buffer and setup it again - all flags are cleared */
585 memset(&rx_bd, 0, sizeof(rx_bd));
586 rx_bd.next = (u32)&rx_bd;
587 rx_bd.phys = (u32)&rxframe;
588 rx_bd.cntrl = sizeof(rxframe);
589
590 /* Write bd to HW */
591 flush_cache((u32)&rx_bd, sizeof(rx_bd));
592
593 /* It is necessary to flush rxframe because if you don't do it
594 * then cache will contain previous packet */
595 flush_cache((u32)&rxframe, sizeof(rxframe));
596
597 /* Rx BD is ready - start again */
598 out_be32(&priv->dmarx->tail, (u32)&rx_bd);
599
600 debug("axiemac: RX completed, framelength = %d\n", length);
601
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000602 return 0;
603}
604
Michal Simek75cc93f2015-12-08 15:44:41 +0100605static int axiemac_miiphy_read(struct mii_dev *bus, int addr,
606 int devad, int reg)
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000607{
Michal Simek75cc93f2015-12-08 15:44:41 +0100608 int ret;
609 u16 value;
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000610
Michal Simek75cc93f2015-12-08 15:44:41 +0100611 ret = phyread(bus->priv, addr, reg, &value);
612 debug("axiemac: Read MII 0x%x, 0x%x, 0x%x, %d\n", addr, reg,
613 value, ret);
614 return value;
Michal Simek4f1ec4c2011-10-06 20:35:35 +0000615}
Michal Simek75cc93f2015-12-08 15:44:41 +0100616
617static int axiemac_miiphy_write(struct mii_dev *bus, int addr, int devad,
618 int reg, u16 value)
619{
620 debug("axiemac: Write MII 0x%x, 0x%x, 0x%x\n", addr, reg, value);
621 return phywrite(bus->priv, addr, reg, value);
622}
623
624static int axi_emac_probe(struct udevice *dev)
625{
626 struct axidma_priv *priv = dev_get_priv(dev);
627 int ret;
628
629 priv->bus = mdio_alloc();
630 priv->bus->read = axiemac_miiphy_read;
631 priv->bus->write = axiemac_miiphy_write;
632 priv->bus->priv = priv;
633 strcpy(priv->bus->name, "axi_emac");
634
635 ret = mdio_register(priv->bus);
636 if (ret)
637 return ret;
638
Michal Simek5d0449d2015-12-08 16:10:05 +0100639 axiemac_phy_init(dev);
640
Michal Simek75cc93f2015-12-08 15:44:41 +0100641 return 0;
642}
643
644static int axi_emac_remove(struct udevice *dev)
645{
646 struct axidma_priv *priv = dev_get_priv(dev);
647
648 free(priv->phydev);
649 mdio_unregister(priv->bus);
650 mdio_free(priv->bus);
651
652 return 0;
653}
654
655static const struct eth_ops axi_emac_ops = {
Michal Simekad499e42015-12-16 09:18:12 +0100656 .start = axiemac_start,
Michal Simek75cc93f2015-12-08 15:44:41 +0100657 .send = axiemac_send,
658 .recv = axiemac_recv,
Michal Simek97d23632015-12-09 14:13:23 +0100659 .free_pkt = axiemac_free_pkt,
Michal Simekad499e42015-12-16 09:18:12 +0100660 .stop = axiemac_stop,
661 .write_hwaddr = axiemac_write_hwaddr,
Michal Simek75cc93f2015-12-08 15:44:41 +0100662};
663
664static int axi_emac_ofdata_to_platdata(struct udevice *dev)
665{
666 struct eth_pdata *pdata = dev_get_platdata(dev);
667 struct axidma_priv *priv = dev_get_priv(dev);
668 int offset = 0;
669 const char *phy_mode;
670
671 pdata->iobase = (phys_addr_t)dev_get_addr(dev);
672 priv->iobase = (struct axi_regs *)pdata->iobase;
673
674 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev->of_offset,
675 "axistream-connected");
676 if (offset <= 0) {
677 printf("%s: axistream is not found\n", __func__);
678 return -EINVAL;
679 }
680 priv->dmatx = (struct axidma_reg *)fdtdec_get_int(gd->fdt_blob,
681 offset, "reg", 0);
682 if (!priv->dmatx) {
683 printf("%s: axi_dma register space not found\n", __func__);
684 return -EINVAL;
685 }
686 /* RX channel offset is 0x30 */
687 priv->dmarx = (struct axidma_reg *)((u32)priv->dmatx + 0x30);
688
689 priv->phyaddr = -1;
690
691 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev->of_offset,
692 "phy-handle");
693 if (offset > 0)
694 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
695
696 phy_mode = fdt_getprop(gd->fdt_blob, dev->of_offset, "phy-mode", NULL);
697 if (phy_mode)
698 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
699 if (pdata->phy_interface == -1) {
700 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
701 return -EINVAL;
702 }
703 priv->interface = pdata->phy_interface;
704
705 printf("AXI EMAC: %lx, phyaddr %d, interface %s\n", (ulong)priv->iobase,
706 priv->phyaddr, phy_string_for_interface(priv->interface));
707
708 return 0;
709}
710
711static const struct udevice_id axi_emac_ids[] = {
712 { .compatible = "xlnx,axi-ethernet-1.00.a" },
713 { }
714};
715
716U_BOOT_DRIVER(axi_emac) = {
717 .name = "axi_emac",
718 .id = UCLASS_ETH,
719 .of_match = axi_emac_ids,
720 .ofdata_to_platdata = axi_emac_ofdata_to_platdata,
721 .probe = axi_emac_probe,
722 .remove = axi_emac_remove,
723 .ops = &axi_emac_ops,
724 .priv_auto_alloc_size = sizeof(struct axidma_priv),
725 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
726};