blob: 6d40370d8964de2ea9a0413997dd9486706ab0cd [file] [log] [blame]
Aubrey Li10ebdd92007-03-19 01:24:52 +08001/*
Mike Frysingerb11eb3f2008-02-24 23:58:13 -05002 * Driver for Blackfin On-Chip MAC device
Aubrey Li10ebdd92007-03-19 01:24:52 +08003 *
Mike Frysingerb11eb3f2008-02-24 23:58:13 -05004 * Copyright (c) 2005-2008 Analog Device, Inc.
Aubrey Li10ebdd92007-03-19 01:24:52 +08005 *
Mike Frysingerb11eb3f2008-02-24 23:58:13 -05006 * Licensed under the GPL-2 or later.
Aubrey Li10ebdd92007-03-19 01:24:52 +08007 */
8
9#include <common.h>
10#include <config.h>
Aubrey Li10ebdd92007-03-19 01:24:52 +080011#include <net.h>
Ben Warren2f2b6b62008-08-31 22:22:04 -070012#include <netdev.h>
Aubrey Li10ebdd92007-03-19 01:24:52 +080013#include <command.h>
14#include <malloc.h>
Mike Frysinger709ad972008-10-14 04:52:00 -040015#include <miiphy.h>
16#include <linux/mii.h>
Aubrey Li10ebdd92007-03-19 01:24:52 +080017
Mike Frysingerb11eb3f2008-02-24 23:58:13 -050018#include <asm/blackfin.h>
Tom Rinic68e8512014-02-20 10:14:10 -050019#include <asm/clock.h>
Mike Frysingereca7f572010-06-02 05:56:22 -040020#include <asm/portmux.h>
Mike Frysinger66c4cf42008-02-04 19:26:55 -050021#include <asm/mach-common/bits/dma.h>
22#include <asm/mach-common/bits/emac.h>
23#include <asm/mach-common/bits/pll.h>
24
Mike Frysingerb11eb3f2008-02-24 23:58:13 -050025#include "bfin_mac.h"
26
Mike Frysinger65b7d932008-10-20 13:59:51 -040027#ifndef CONFIG_PHY_ADDR
28# define CONFIG_PHY_ADDR 1
29#endif
30#ifndef CONFIG_PHY_CLOCK_FREQ
31# define CONFIG_PHY_CLOCK_FREQ 2500000
32#endif
33
Aubrey Li10ebdd92007-03-19 01:24:52 +080034#ifdef CONFIG_POST
35#include <post.h>
36#endif
37
Aubrey Li10ebdd92007-03-19 01:24:52 +080038#define RXBUF_BASE_ADDR 0xFF900000
39#define TXBUF_BASE_ADDR 0xFF800000
40#define TX_BUF_CNT 1
41
Wolfgang Denka1be4762008-05-20 16:00:29 +020042#define TOUT_LOOP 1000000
Aubrey Li10ebdd92007-03-19 01:24:52 +080043
Mike Frysinger10be09f2009-01-08 11:57:57 -050044static ADI_ETHER_BUFFER *txbuf[TX_BUF_CNT];
45static ADI_ETHER_BUFFER *rxbuf[PKTBUFSRX];
Aubrey Li10ebdd92007-03-19 01:24:52 +080046static u16 txIdx; /* index of the current RX buffer */
47static u16 rxIdx; /* index of the current TX buffer */
48
Aubrey Li10ebdd92007-03-19 01:24:52 +080049/* DMAx_CONFIG values at DMA Restart */
Mike Frysinger10be09f2009-01-08 11:57:57 -050050static const union {
51 u16 data;
52 ADI_DMA_CONFIG_REG reg;
53} txdmacfg = {
54 .reg = {
55 .b_DMA_EN = 1, /* enabled */
56 .b_WNR = 0, /* read from memory */
57 .b_WDSIZE = 2, /* wordsize is 32 bits */
58 .b_DMA2D = 0,
59 .b_RESTART = 0,
60 .b_DI_SEL = 0,
61 .b_DI_EN = 0, /* no interrupt */
62 .b_NDSIZE = 5, /* 5 half words is desc size */
63 .b_FLOW = 7 /* large desc flow */
64 },
Mike Frysingerb11eb3f2008-02-24 23:58:13 -050065};
Aubrey Li10ebdd92007-03-19 01:24:52 +080066
Mike Frysinger709ad972008-10-14 04:52:00 -040067static int bfin_miiphy_wait(void)
68{
69 /* poll the STABUSY bit */
70 while (bfin_read_EMAC_STAADD() & STABUSY)
71 continue;
72 return 0;
73}
74
Joe Hershberger1fbcbed2016-08-08 11:28:38 -050075static int bfin_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg)
Mike Frysinger709ad972008-10-14 04:52:00 -040076{
Joe Hershberger1fbcbed2016-08-08 11:28:38 -050077 ushort val = 0;
Mike Frysinger709ad972008-10-14 04:52:00 -040078 if (bfin_miiphy_wait())
79 return 1;
80 bfin_write_EMAC_STAADD(SET_PHYAD(addr) | SET_REGAD(reg) | STABUSY);
81 if (bfin_miiphy_wait())
82 return 1;
Joe Hershberger1fbcbed2016-08-08 11:28:38 -050083 val = bfin_read_EMAC_STADAT();
84 return val;
Mike Frysinger709ad972008-10-14 04:52:00 -040085}
86
Joe Hershberger1fbcbed2016-08-08 11:28:38 -050087static int bfin_miiphy_write(struct mii_dev *bus, int addr, int devad,
88 int reg, u16 val)
Mike Frysinger709ad972008-10-14 04:52:00 -040089{
90 if (bfin_miiphy_wait())
91 return 1;
92 bfin_write_EMAC_STADAT(val);
93 bfin_write_EMAC_STAADD(SET_PHYAD(addr) | SET_REGAD(reg) | STAOP | STABUSY);
94 return 0;
95}
96
Mike Frysingerb11eb3f2008-02-24 23:58:13 -050097int bfin_EMAC_initialize(bd_t *bis)
Aubrey Li10ebdd92007-03-19 01:24:52 +080098{
99 struct eth_device *dev;
Mike Frysinger709ad972008-10-14 04:52:00 -0400100 dev = malloc(sizeof(*dev));
Aubrey Li10ebdd92007-03-19 01:24:52 +0800101 if (dev == NULL)
102 hang();
103
104 memset(dev, 0, sizeof(*dev));
Mike Frysinger95d834a2010-06-09 21:50:48 -0400105 strcpy(dev->name, "bfin_mac");
Aubrey Li10ebdd92007-03-19 01:24:52 +0800106
107 dev->iobase = 0;
108 dev->priv = 0;
109 dev->init = bfin_EMAC_init;
110 dev->halt = bfin_EMAC_halt;
111 dev->send = bfin_EMAC_send;
112 dev->recv = bfin_EMAC_recv;
Mike Frysinger35be8722010-04-27 14:15:28 -0400113 dev->write_hwaddr = bfin_EMAC_setup_addr;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800114
115 eth_register(dev);
116
Mike Frysinger709ad972008-10-14 04:52:00 -0400117#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500118 int retval;
119 struct mii_dev *mdiodev = mdio_alloc();
120 if (!mdiodev)
121 return -ENOMEM;
122 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
123 mdiodev->read = bfin_miiphy_read;
124 mdiodev->write = bfin_miiphy_write;
125
126 retval = mdio_register(mdiodev);
127 if (retval < 0)
128 return retval;
Mike Frysinger709ad972008-10-14 04:52:00 -0400129#endif
130
Ben Warren82c2c6a2008-07-11 23:15:28 -0700131 return 0;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800132}
133
Joe Hershbergere4e04882012-05-22 18:36:19 +0000134static int bfin_EMAC_send(struct eth_device *dev, void *packet, int length)
Aubrey Li10ebdd92007-03-19 01:24:52 +0800135{
136 int i;
137 int result = 0;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800138
139 if (length <= 0) {
140 printf("Ethernet: bad packet size: %d\n", length);
141 goto out;
142 }
143
Mike Frysingerdec3c782010-07-25 16:38:12 -0400144 if (bfin_read_DMA2_IRQ_STATUS() & DMA_ERR) {
Aubrey Li10ebdd92007-03-19 01:24:52 +0800145 printf("Ethernet: tx DMA error\n");
146 goto out;
147 }
148
Mike Frysingerdec3c782010-07-25 16:38:12 -0400149 for (i = 0; (bfin_read_DMA2_IRQ_STATUS() & DMA_RUN); ++i) {
Aubrey Li10ebdd92007-03-19 01:24:52 +0800150 if (i > TOUT_LOOP) {
151 puts("Ethernet: tx time out\n");
152 goto out;
153 }
154 }
155 txbuf[txIdx]->FrmData->NoBytes = length;
156 memcpy(txbuf[txIdx]->FrmData->Dest, (void *)packet, length);
157 txbuf[txIdx]->Dma[0].START_ADDR = (u32) txbuf[txIdx]->FrmData;
Mike Frysingerdec3c782010-07-25 16:38:12 -0400158 bfin_write_DMA2_NEXT_DESC_PTR(txbuf[txIdx]->Dma);
159 bfin_write_DMA2_CONFIG(txdmacfg.data);
160 bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800161
162 for (i = 0; (txbuf[txIdx]->StatusWord & TX_COMP) == 0; i++) {
163 if (i > TOUT_LOOP) {
164 puts("Ethernet: tx error\n");
165 goto out;
166 }
167 }
168 result = txbuf[txIdx]->StatusWord;
169 txbuf[txIdx]->StatusWord = 0;
170 if ((txIdx + 1) >= TX_BUF_CNT)
171 txIdx = 0;
172 else
173 txIdx++;
Mike Frysingerb11eb3f2008-02-24 23:58:13 -0500174 out:
Mike Frysinger3ac20c32008-11-05 06:36:15 -0500175 debug("BFIN EMAC send: length = %d\n", length);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800176 return result;
177}
178
179static int bfin_EMAC_recv(struct eth_device *dev)
180{
181 int length = 0;
182
183 for (;;) {
184 if ((rxbuf[rxIdx]->StatusWord & RX_COMP) == 0) {
185 length = -1;
186 break;
187 }
188 if ((rxbuf[rxIdx]->StatusWord & RX_DMAO) != 0) {
189 printf("Ethernet: rx dma overrun\n");
190 break;
191 }
192 if ((rxbuf[rxIdx]->StatusWord & RX_OK) == 0) {
193 printf("Ethernet: rx error\n");
194 break;
195 }
196 length = rxbuf[rxIdx]->StatusWord & 0x000007FF;
197 if (length <= 4) {
198 printf("Ethernet: bad frame\n");
199 break;
200 }
Robin Getzb5edf812009-08-24 10:33:39 -0400201
202 debug("%s: len = %d\n", __func__, length - 4);
203
Joe Hershberger9f09a362015-04-08 01:41:06 -0500204 net_rx_packets[rxIdx] = rxbuf[rxIdx]->FrmData->Dest;
205 net_process_received_packet(net_rx_packets[rxIdx], length - 4);
Mike Frysingerdec3c782010-07-25 16:38:12 -0400206 bfin_write_DMA1_IRQ_STATUS(DMA_DONE | DMA_ERR);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800207 rxbuf[rxIdx]->StatusWord = 0x00000000;
208 if ((rxIdx + 1) >= PKTBUFSRX)
209 rxIdx = 0;
210 else
211 rxIdx++;
212 }
213
214 return length;
215}
216
217/**************************************************************
218 *
219 * Ethernet Initialization Routine
220 *
221 *************************************************************/
222
Mike Frysinger709ad972008-10-14 04:52:00 -0400223/* MDC = SCLK / MDC_freq / 2 - 1 */
224#define MDC_FREQ_TO_DIV(mdc_freq) (get_sclk() / (mdc_freq) / 2 - 1)
225
Mike Frysingereca7f572010-06-02 05:56:22 -0400226#ifndef CONFIG_BFIN_MAC_PINS
227# ifdef CONFIG_RMII
228# define CONFIG_BFIN_MAC_PINS P_RMII0
229# else
230# define CONFIG_BFIN_MAC_PINS P_MII0
231# endif
232#endif
233
Mike Frysinger709ad972008-10-14 04:52:00 -0400234static int bfin_miiphy_init(struct eth_device *dev, int *opmode)
235{
Mike Frysingereca7f572010-06-02 05:56:22 -0400236 const unsigned short pins[] = CONFIG_BFIN_MAC_PINS;
Mike Frysinger709ad972008-10-14 04:52:00 -0400237 u16 phydat;
238 size_t count;
239
240 /* Enable PHY output */
Mike Frysingerdec3c782010-07-25 16:38:12 -0400241 bfin_write_VR_CTL(bfin_read_VR_CTL() | CLKBUFOE);
Mike Frysinger709ad972008-10-14 04:52:00 -0400242
243 /* Set all the pins to peripheral mode */
Mike Frysingereca7f572010-06-02 05:56:22 -0400244 peripheral_request_list(pins, "bfin_mac");
Mike Frysinger709ad972008-10-14 04:52:00 -0400245
246 /* Odd word alignment for Receive Frame DMA word */
247 /* Configure checksum support and rcve frame word alignment */
Mike Frysinger65b7d932008-10-20 13:59:51 -0400248 bfin_write_EMAC_SYSCTL(RXDWA | RXCKS | SET_MDCDIV(MDC_FREQ_TO_DIV(CONFIG_PHY_CLOCK_FREQ)));
Mike Frysinger709ad972008-10-14 04:52:00 -0400249
250 /* turn on auto-negotiation and wait for link to come up */
Mike Frysinger65b7d932008-10-20 13:59:51 -0400251 bfin_miiphy_write(dev->name, CONFIG_PHY_ADDR, MII_BMCR, BMCR_ANENABLE);
Mike Frysinger709ad972008-10-14 04:52:00 -0400252 count = 0;
253 while (1) {
254 ++count;
Mike Frysinger65b7d932008-10-20 13:59:51 -0400255 if (bfin_miiphy_read(dev->name, CONFIG_PHY_ADDR, MII_BMSR, &phydat))
Mike Frysinger709ad972008-10-14 04:52:00 -0400256 return -1;
257 if (phydat & BMSR_LSTATUS)
258 break;
259 if (count > 30000) {
260 printf("%s: link down, check cable\n", dev->name);
261 return -1;
262 }
263 udelay(100);
264 }
265
266 /* see what kind of link we have */
Mike Frysinger65b7d932008-10-20 13:59:51 -0400267 if (bfin_miiphy_read(dev->name, CONFIG_PHY_ADDR, MII_LPA, &phydat))
Mike Frysinger709ad972008-10-14 04:52:00 -0400268 return -1;
269 if (phydat & LPA_DUPLEX)
270 *opmode = FDMODE;
271 else
272 *opmode = 0;
273
274 bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
Aaron Wu158dc022011-11-23 11:23:56 +0800275 bfin_write_EMAC_VLAN1(EMAC_VLANX_DEF_VAL);
276 bfin_write_EMAC_VLAN2(EMAC_VLANX_DEF_VAL);
Mike Frysinger709ad972008-10-14 04:52:00 -0400277
278 /* Initialize the TX DMA channel registers */
Mike Frysingerdec3c782010-07-25 16:38:12 -0400279 bfin_write_DMA2_X_COUNT(0);
280 bfin_write_DMA2_X_MODIFY(4);
281 bfin_write_DMA2_Y_COUNT(0);
282 bfin_write_DMA2_Y_MODIFY(0);
Mike Frysinger709ad972008-10-14 04:52:00 -0400283
284 /* Initialize the RX DMA channel registers */
Mike Frysingerdec3c782010-07-25 16:38:12 -0400285 bfin_write_DMA1_X_COUNT(0);
286 bfin_write_DMA1_X_MODIFY(4);
287 bfin_write_DMA1_Y_COUNT(0);
288 bfin_write_DMA1_Y_MODIFY(0);
Mike Frysinger709ad972008-10-14 04:52:00 -0400289
290 return 0;
291}
292
Mike Frysinger35be8722010-04-27 14:15:28 -0400293static int bfin_EMAC_setup_addr(struct eth_device *dev)
294{
Mike Frysingerdec3c782010-07-25 16:38:12 -0400295 bfin_write_EMAC_ADDRLO(
Mike Frysinger35be8722010-04-27 14:15:28 -0400296 dev->enetaddr[0] |
297 dev->enetaddr[1] << 8 |
298 dev->enetaddr[2] << 16 |
Mike Frysingerdec3c782010-07-25 16:38:12 -0400299 dev->enetaddr[3] << 24
300 );
301 bfin_write_EMAC_ADDRHI(
Mike Frysinger35be8722010-04-27 14:15:28 -0400302 dev->enetaddr[4] |
Mike Frysingerdec3c782010-07-25 16:38:12 -0400303 dev->enetaddr[5] << 8
304 );
Mike Frysinger35be8722010-04-27 14:15:28 -0400305 return 0;
306}
307
Mike Frysingerb11eb3f2008-02-24 23:58:13 -0500308static int bfin_EMAC_init(struct eth_device *dev, bd_t *bd)
Aubrey Li10ebdd92007-03-19 01:24:52 +0800309{
310 u32 opmode;
311 int dat;
312 int i;
Mike Frysinger3ac20c32008-11-05 06:36:15 -0500313 debug("Eth_init: ......\n");
Aubrey Li10ebdd92007-03-19 01:24:52 +0800314
315 txIdx = 0;
316 rxIdx = 0;
317
Mike Frysinger709ad972008-10-14 04:52:00 -0400318 /* Initialize System Register */
319 if (bfin_miiphy_init(dev, &dat) < 0)
Aubrey Li10ebdd92007-03-19 01:24:52 +0800320 return -1;
321
Mike Frysinger709ad972008-10-14 04:52:00 -0400322 /* Initialize EMAC address */
Mike Frysinger35be8722010-04-27 14:15:28 -0400323 bfin_EMAC_setup_addr(dev);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800324
Mike Frysinger709ad972008-10-14 04:52:00 -0400325 /* Initialize TX and RX buffer */
Aubrey Li10ebdd92007-03-19 01:24:52 +0800326 for (i = 0; i < PKTBUFSRX; i++) {
327 rxbuf[i] = SetupRxBuffer(i);
328 if (i > 0) {
Mike Frysinger10be09f2009-01-08 11:57:57 -0500329 rxbuf[i - 1]->Dma[1].NEXT_DESC_PTR = rxbuf[i]->Dma;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800330 if (i == (PKTBUFSRX - 1))
Mike Frysinger10be09f2009-01-08 11:57:57 -0500331 rxbuf[i]->Dma[1].NEXT_DESC_PTR = rxbuf[0]->Dma;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800332 }
333 }
334 for (i = 0; i < TX_BUF_CNT; i++) {
335 txbuf[i] = SetupTxBuffer(i);
336 if (i > 0) {
Mike Frysinger10be09f2009-01-08 11:57:57 -0500337 txbuf[i - 1]->Dma[1].NEXT_DESC_PTR = txbuf[i]->Dma;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800338 if (i == (TX_BUF_CNT - 1))
Mike Frysinger10be09f2009-01-08 11:57:57 -0500339 txbuf[i]->Dma[1].NEXT_DESC_PTR = txbuf[0]->Dma;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800340 }
341 }
342
343 /* Set RX DMA */
Mike Frysingerdec3c782010-07-25 16:38:12 -0400344 bfin_write_DMA1_NEXT_DESC_PTR(rxbuf[0]->Dma);
345 bfin_write_DMA1_CONFIG(rxbuf[0]->Dma[0].CONFIG_DATA);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800346
347 /* Wait MII done */
Mike Frysinger709ad972008-10-14 04:52:00 -0400348 bfin_miiphy_wait();
Aubrey Li10ebdd92007-03-19 01:24:52 +0800349
350 /* We enable only RX here */
351 /* ASTP : Enable Automatic Pad Stripping
352 PR : Promiscuous Mode for test
353 PSF : Receive frames with total length less than 64 bytes.
354 FDMODE : Full Duplex Mode
355 LB : Internal Loopback for test
356 RE : Receiver Enable */
357 if (dat == FDMODE)
358 opmode = ASTP | FDMODE | PSF;
359 else
360 opmode = ASTP | PSF;
361 opmode |= RE;
Mike Frysingerd11ab932008-12-09 17:46:21 -0500362#ifdef CONFIG_RMII
Aubrey Li10ebdd92007-03-19 01:24:52 +0800363 opmode |= TE | RMII;
364#endif
365 /* Turn on the EMAC */
Mike Frysingerdec3c782010-07-25 16:38:12 -0400366 bfin_write_EMAC_OPMODE(opmode);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800367 return 0;
368}
369
370static void bfin_EMAC_halt(struct eth_device *dev)
371{
Mike Frysinger3ac20c32008-11-05 06:36:15 -0500372 debug("Eth_halt: ......\n");
Aubrey Li10ebdd92007-03-19 01:24:52 +0800373 /* Turn off the EMAC */
Mike Frysingerdec3c782010-07-25 16:38:12 -0400374 bfin_write_EMAC_OPMODE(0);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800375 /* Turn off the EMAC RX DMA */
Mike Frysingerdec3c782010-07-25 16:38:12 -0400376 bfin_write_DMA1_CONFIG(0);
377 bfin_write_DMA2_CONFIG(0);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800378}
379
Aubrey Li10ebdd92007-03-19 01:24:52 +0800380ADI_ETHER_BUFFER *SetupRxBuffer(int no)
381{
382 ADI_ETHER_FRAME_BUFFER *frmbuf;
383 ADI_ETHER_BUFFER *buf;
384 int nobytes_buffer = sizeof(ADI_ETHER_BUFFER[2]) / 2; /* ensure a multi. of 4 */
385 int total_size = nobytes_buffer + RECV_BUFSIZE;
386
Mike Frysinger10be09f2009-01-08 11:57:57 -0500387 buf = (void *) (RXBUF_BASE_ADDR + no * total_size);
388 frmbuf = (void *) (RXBUF_BASE_ADDR + no * total_size + nobytes_buffer);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800389
390 memset(buf, 0x00, nobytes_buffer);
391 buf->FrmData = frmbuf;
392 memset(frmbuf, 0xfe, RECV_BUFSIZE);
393
394 /* set up first desc to point to receive frame buffer */
395 buf->Dma[0].NEXT_DESC_PTR = &(buf->Dma[1]);
396 buf->Dma[0].START_ADDR = (u32) buf->FrmData;
397 buf->Dma[0].CONFIG.b_DMA_EN = 1; /* enabled */
398 buf->Dma[0].CONFIG.b_WNR = 1; /* Write to memory */
399 buf->Dma[0].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
400 buf->Dma[0].CONFIG.b_NDSIZE = 5; /* 5 half words is desc size. */
401 buf->Dma[0].CONFIG.b_FLOW = 7; /* large desc flow */
402
403 /* set up second desc to point to status word */
Mike Frysinger10be09f2009-01-08 11:57:57 -0500404 buf->Dma[1].NEXT_DESC_PTR = buf->Dma;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800405 buf->Dma[1].START_ADDR = (u32) & buf->IPHdrChksum;
406 buf->Dma[1].CONFIG.b_DMA_EN = 1; /* enabled */
407 buf->Dma[1].CONFIG.b_WNR = 1; /* Write to memory */
408 buf->Dma[1].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
409 buf->Dma[1].CONFIG.b_DI_EN = 1; /* enable interrupt */
410 buf->Dma[1].CONFIG.b_NDSIZE = 5; /* must be 0 when FLOW is 0 */
411 buf->Dma[1].CONFIG.b_FLOW = 7; /* stop */
412
413 return buf;
414}
415
416ADI_ETHER_BUFFER *SetupTxBuffer(int no)
417{
418 ADI_ETHER_FRAME_BUFFER *frmbuf;
419 ADI_ETHER_BUFFER *buf;
420 int nobytes_buffer = sizeof(ADI_ETHER_BUFFER[2]) / 2; /* ensure a multi. of 4 */
421 int total_size = nobytes_buffer + RECV_BUFSIZE;
422
Mike Frysinger10be09f2009-01-08 11:57:57 -0500423 buf = (void *) (TXBUF_BASE_ADDR + no * total_size);
424 frmbuf = (void *) (TXBUF_BASE_ADDR + no * total_size + nobytes_buffer);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800425
426 memset(buf, 0x00, nobytes_buffer);
427 buf->FrmData = frmbuf;
428 memset(frmbuf, 0x00, RECV_BUFSIZE);
429
430 /* set up first desc to point to receive frame buffer */
431 buf->Dma[0].NEXT_DESC_PTR = &(buf->Dma[1]);
432 buf->Dma[0].START_ADDR = (u32) buf->FrmData;
433 buf->Dma[0].CONFIG.b_DMA_EN = 1; /* enabled */
434 buf->Dma[0].CONFIG.b_WNR = 0; /* Read to memory */
435 buf->Dma[0].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
436 buf->Dma[0].CONFIG.b_NDSIZE = 5; /* 5 half words is desc size. */
437 buf->Dma[0].CONFIG.b_FLOW = 7; /* large desc flow */
438
439 /* set up second desc to point to status word */
440 buf->Dma[1].NEXT_DESC_PTR = &(buf->Dma[0]);
441 buf->Dma[1].START_ADDR = (u32) & buf->StatusWord;
442 buf->Dma[1].CONFIG.b_DMA_EN = 1; /* enabled */
443 buf->Dma[1].CONFIG.b_WNR = 1; /* Write to memory */
444 buf->Dma[1].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
445 buf->Dma[1].CONFIG.b_DI_EN = 1; /* enable interrupt */
446 buf->Dma[1].CONFIG.b_NDSIZE = 0; /* must be 0 when FLOW is 0 */
447 buf->Dma[1].CONFIG.b_FLOW = 0; /* stop */
448
449 return buf;
450}
451
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200452#if defined(CONFIG_POST) && defined(CONFIG_SYS_POST_ETHER)
Aubrey Li10ebdd92007-03-19 01:24:52 +0800453int ether_post_test(int flags)
454{
455 uchar buf[64];
456 int i, value = 0;
457 int length;
Mike Frysingerdec3c782010-07-25 16:38:12 -0400458 uint addr;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800459
460 printf("\n--------");
461 bfin_EMAC_init(NULL, NULL);
462 /* construct the package */
Mike Frysingerdec3c782010-07-25 16:38:12 -0400463 addr = bfin_read_EMAC_ADDRLO();
464 buf[0] = buf[6] = addr;
465 buf[1] = buf[7] = addr >> 8;
466 buf[2] = buf[8] = addr >> 16;
467 buf[3] = buf[9] = addr >> 24;
468 addr = bfin_read_EMAC_ADDRHI();
469 buf[4] = buf[10] = addr;
470 buf[5] = buf[11] = addr >> 8;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800471 buf[12] = 0x08; /* Type: ARP */
472 buf[13] = 0x06;
473 buf[14] = 0x00; /* Hardware type: Ethernet */
474 buf[15] = 0x01;
475 buf[16] = 0x08; /* Protocal type: IP */
476 buf[17] = 0x00;
477 buf[18] = 0x06; /* Hardware size */
478 buf[19] = 0x04; /* Protocol size */
479 buf[20] = 0x00; /* Opcode: request */
480 buf[21] = 0x01;
481
482 for (i = 0; i < 42; i++)
483 buf[i + 22] = i;
484 printf("--------Send 64 bytes......\n");
Joe Hershbergere4e04882012-05-22 18:36:19 +0000485 bfin_EMAC_send(NULL, buf, 64);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800486 for (i = 0; i < 100; i++) {
487 udelay(10000);
488 if ((rxbuf[rxIdx]->StatusWord & RX_COMP) != 0) {
489 value = 1;
490 break;
491 }
492 }
493 if (value == 0) {
494 printf("--------EMAC can't receive any data\n");
495 eth_halt();
496 return -1;
497 }
498 length = rxbuf[rxIdx]->StatusWord & 0x000007FF - 4;
499 for (i = 0; i < length; i++) {
500 if (rxbuf[rxIdx]->FrmData->Dest[i] != buf[i]) {
501 printf("--------EMAC receive error data!\n");
502 eth_halt();
503 return -1;
504 }
505 }
506 printf("--------receive %d bytes, matched\n", length);
507 bfin_EMAC_halt(NULL);
508 return 0;
509}
510#endif