blob: e24ac4a174c12dd2064fb11510dd3e84b4f54cd1 [file] [log] [blame]
Aubrey Li26bf7de2007-03-19 01:24:52 +08001/*
Mike Frysinger395bce42008-02-24 23:58:13 -05002 * Driver for Blackfin On-Chip MAC device
Aubrey Li26bf7de2007-03-19 01:24:52 +08003 *
Mike Frysinger395bce42008-02-24 23:58:13 -05004 * Copyright (c) 2005-2008 Analog Device, Inc.
Aubrey Li26bf7de2007-03-19 01:24:52 +08005 *
Mike Frysinger395bce42008-02-24 23:58:13 -05006 * Licensed under the GPL-2 or later.
Aubrey Li26bf7de2007-03-19 01:24:52 +08007 */
8
9#include <common.h>
10#include <config.h>
Aubrey Li26bf7de2007-03-19 01:24:52 +080011#include <net.h>
Ben Warren89973f82008-08-31 22:22:04 -070012#include <netdev.h>
Aubrey Li26bf7de2007-03-19 01:24:52 +080013#include <command.h>
14#include <malloc.h>
Mike Frysingerac45af42008-10-14 04:52:00 -040015#include <miiphy.h>
16#include <linux/mii.h>
Aubrey Li26bf7de2007-03-19 01:24:52 +080017
Mike Frysinger395bce42008-02-24 23:58:13 -050018#include <asm/blackfin.h>
Mike Frysinger8339ad72010-06-02 05:56:22 -040019#include <asm/portmux.h>
Mike Frysingerd4d77302008-02-04 19:26:55 -050020#include <asm/mach-common/bits/dma.h>
21#include <asm/mach-common/bits/emac.h>
22#include <asm/mach-common/bits/pll.h>
23
Mike Frysinger395bce42008-02-24 23:58:13 -050024#include "bfin_mac.h"
25
Mike Frysingera7ec6ac2008-10-20 13:59:51 -040026#ifndef CONFIG_PHY_ADDR
27# define CONFIG_PHY_ADDR 1
28#endif
29#ifndef CONFIG_PHY_CLOCK_FREQ
30# define CONFIG_PHY_CLOCK_FREQ 2500000
31#endif
32
Aubrey Li26bf7de2007-03-19 01:24:52 +080033#ifdef CONFIG_POST
34#include <post.h>
35#endif
36
Aubrey Li26bf7de2007-03-19 01:24:52 +080037#define RXBUF_BASE_ADDR 0xFF900000
38#define TXBUF_BASE_ADDR 0xFF800000
39#define TX_BUF_CNT 1
40
Wolfgang Denk53677ef2008-05-20 16:00:29 +020041#define TOUT_LOOP 1000000
Aubrey Li26bf7de2007-03-19 01:24:52 +080042
Mike Frysinger6d7d4802009-01-08 11:57:57 -050043static ADI_ETHER_BUFFER *txbuf[TX_BUF_CNT];
44static ADI_ETHER_BUFFER *rxbuf[PKTBUFSRX];
Aubrey Li26bf7de2007-03-19 01:24:52 +080045static u16 txIdx; /* index of the current RX buffer */
46static u16 rxIdx; /* index of the current TX buffer */
47
Aubrey Li26bf7de2007-03-19 01:24:52 +080048/* DMAx_CONFIG values at DMA Restart */
Mike Frysinger6d7d4802009-01-08 11:57:57 -050049static const union {
50 u16 data;
51 ADI_DMA_CONFIG_REG reg;
52} txdmacfg = {
53 .reg = {
54 .b_DMA_EN = 1, /* enabled */
55 .b_WNR = 0, /* read from memory */
56 .b_WDSIZE = 2, /* wordsize is 32 bits */
57 .b_DMA2D = 0,
58 .b_RESTART = 0,
59 .b_DI_SEL = 0,
60 .b_DI_EN = 0, /* no interrupt */
61 .b_NDSIZE = 5, /* 5 half words is desc size */
62 .b_FLOW = 7 /* large desc flow */
63 },
Mike Frysinger395bce42008-02-24 23:58:13 -050064};
Aubrey Li26bf7de2007-03-19 01:24:52 +080065
Mike Frysingerac45af42008-10-14 04:52:00 -040066static int bfin_miiphy_wait(void)
67{
68 /* poll the STABUSY bit */
69 while (bfin_read_EMAC_STAADD() & STABUSY)
70 continue;
71 return 0;
72}
73
Mike Frysinger5700bb62010-07-27 18:35:08 -040074static int bfin_miiphy_read(const char *devname, uchar addr, uchar reg, ushort *val)
Mike Frysingerac45af42008-10-14 04:52:00 -040075{
76 if (bfin_miiphy_wait())
77 return 1;
78 bfin_write_EMAC_STAADD(SET_PHYAD(addr) | SET_REGAD(reg) | STABUSY);
79 if (bfin_miiphy_wait())
80 return 1;
81 *val = bfin_read_EMAC_STADAT();
82 return 0;
83}
84
Mike Frysinger5700bb62010-07-27 18:35:08 -040085static int bfin_miiphy_write(const char *devname, uchar addr, uchar reg, ushort val)
Mike Frysingerac45af42008-10-14 04:52:00 -040086{
87 if (bfin_miiphy_wait())
88 return 1;
89 bfin_write_EMAC_STADAT(val);
90 bfin_write_EMAC_STAADD(SET_PHYAD(addr) | SET_REGAD(reg) | STAOP | STABUSY);
91 return 0;
92}
93
Mike Frysinger395bce42008-02-24 23:58:13 -050094int bfin_EMAC_initialize(bd_t *bis)
Aubrey Li26bf7de2007-03-19 01:24:52 +080095{
96 struct eth_device *dev;
Mike Frysingerac45af42008-10-14 04:52:00 -040097 dev = malloc(sizeof(*dev));
Aubrey Li26bf7de2007-03-19 01:24:52 +080098 if (dev == NULL)
99 hang();
100
101 memset(dev, 0, sizeof(*dev));
Mike Frysinger94060a12010-06-09 21:50:48 -0400102 strcpy(dev->name, "bfin_mac");
Aubrey Li26bf7de2007-03-19 01:24:52 +0800103
104 dev->iobase = 0;
105 dev->priv = 0;
106 dev->init = bfin_EMAC_init;
107 dev->halt = bfin_EMAC_halt;
108 dev->send = bfin_EMAC_send;
109 dev->recv = bfin_EMAC_recv;
Mike Frysinger4324dc72010-04-27 14:15:28 -0400110 dev->write_hwaddr = bfin_EMAC_setup_addr;
Aubrey Li26bf7de2007-03-19 01:24:52 +0800111
112 eth_register(dev);
113
Mike Frysingerac45af42008-10-14 04:52:00 -0400114#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
115 miiphy_register(dev->name, bfin_miiphy_read, bfin_miiphy_write);
116#endif
117
Ben Warren91494732008-07-11 23:15:28 -0700118 return 0;
Aubrey Li26bf7de2007-03-19 01:24:52 +0800119}
120
Joe Hershberger10cbe3b2012-05-22 18:36:19 +0000121static int bfin_EMAC_send(struct eth_device *dev, void *packet, int length)
Aubrey Li26bf7de2007-03-19 01:24:52 +0800122{
123 int i;
124 int result = 0;
125 unsigned int *buf;
126 buf = (unsigned int *)packet;
127
128 if (length <= 0) {
129 printf("Ethernet: bad packet size: %d\n", length);
130 goto out;
131 }
132
Mike Frysinger0c714812010-07-25 16:38:12 -0400133 if (bfin_read_DMA2_IRQ_STATUS() & DMA_ERR) {
Aubrey Li26bf7de2007-03-19 01:24:52 +0800134 printf("Ethernet: tx DMA error\n");
135 goto out;
136 }
137
Mike Frysinger0c714812010-07-25 16:38:12 -0400138 for (i = 0; (bfin_read_DMA2_IRQ_STATUS() & DMA_RUN); ++i) {
Aubrey Li26bf7de2007-03-19 01:24:52 +0800139 if (i > TOUT_LOOP) {
140 puts("Ethernet: tx time out\n");
141 goto out;
142 }
143 }
144 txbuf[txIdx]->FrmData->NoBytes = length;
145 memcpy(txbuf[txIdx]->FrmData->Dest, (void *)packet, length);
146 txbuf[txIdx]->Dma[0].START_ADDR = (u32) txbuf[txIdx]->FrmData;
Mike Frysinger0c714812010-07-25 16:38:12 -0400147 bfin_write_DMA2_NEXT_DESC_PTR(txbuf[txIdx]->Dma);
148 bfin_write_DMA2_CONFIG(txdmacfg.data);
149 bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE);
Aubrey Li26bf7de2007-03-19 01:24:52 +0800150
151 for (i = 0; (txbuf[txIdx]->StatusWord & TX_COMP) == 0; i++) {
152 if (i > TOUT_LOOP) {
153 puts("Ethernet: tx error\n");
154 goto out;
155 }
156 }
157 result = txbuf[txIdx]->StatusWord;
158 txbuf[txIdx]->StatusWord = 0;
159 if ((txIdx + 1) >= TX_BUF_CNT)
160 txIdx = 0;
161 else
162 txIdx++;
Mike Frysinger395bce42008-02-24 23:58:13 -0500163 out:
Mike Frysinger8eed6ca2008-11-05 06:36:15 -0500164 debug("BFIN EMAC send: length = %d\n", length);
Aubrey Li26bf7de2007-03-19 01:24:52 +0800165 return result;
166}
167
168static int bfin_EMAC_recv(struct eth_device *dev)
169{
170 int length = 0;
171
172 for (;;) {
173 if ((rxbuf[rxIdx]->StatusWord & RX_COMP) == 0) {
174 length = -1;
175 break;
176 }
177 if ((rxbuf[rxIdx]->StatusWord & RX_DMAO) != 0) {
178 printf("Ethernet: rx dma overrun\n");
179 break;
180 }
181 if ((rxbuf[rxIdx]->StatusWord & RX_OK) == 0) {
182 printf("Ethernet: rx error\n");
183 break;
184 }
185 length = rxbuf[rxIdx]->StatusWord & 0x000007FF;
186 if (length <= 4) {
187 printf("Ethernet: bad frame\n");
188 break;
189 }
Robin Getz488feef2009-08-24 10:33:39 -0400190
191 debug("%s: len = %d\n", __func__, length - 4);
192
Aubrey Li26bf7de2007-03-19 01:24:52 +0800193 NetRxPackets[rxIdx] =
194 (volatile uchar *)(rxbuf[rxIdx]->FrmData->Dest);
195 NetReceive(NetRxPackets[rxIdx], length - 4);
Mike Frysinger0c714812010-07-25 16:38:12 -0400196 bfin_write_DMA1_IRQ_STATUS(DMA_DONE | DMA_ERR);
Aubrey Li26bf7de2007-03-19 01:24:52 +0800197 rxbuf[rxIdx]->StatusWord = 0x00000000;
198 if ((rxIdx + 1) >= PKTBUFSRX)
199 rxIdx = 0;
200 else
201 rxIdx++;
202 }
203
204 return length;
205}
206
207/**************************************************************
208 *
209 * Ethernet Initialization Routine
210 *
211 *************************************************************/
212
Mike Frysingerac45af42008-10-14 04:52:00 -0400213/* MDC = SCLK / MDC_freq / 2 - 1 */
214#define MDC_FREQ_TO_DIV(mdc_freq) (get_sclk() / (mdc_freq) / 2 - 1)
215
Mike Frysinger8339ad72010-06-02 05:56:22 -0400216#ifndef CONFIG_BFIN_MAC_PINS
217# ifdef CONFIG_RMII
218# define CONFIG_BFIN_MAC_PINS P_RMII0
219# else
220# define CONFIG_BFIN_MAC_PINS P_MII0
221# endif
222#endif
223
Mike Frysingerac45af42008-10-14 04:52:00 -0400224static int bfin_miiphy_init(struct eth_device *dev, int *opmode)
225{
Mike Frysinger8339ad72010-06-02 05:56:22 -0400226 const unsigned short pins[] = CONFIG_BFIN_MAC_PINS;
Mike Frysingerac45af42008-10-14 04:52:00 -0400227 u16 phydat;
228 size_t count;
229
230 /* Enable PHY output */
Mike Frysinger0c714812010-07-25 16:38:12 -0400231 bfin_write_VR_CTL(bfin_read_VR_CTL() | CLKBUFOE);
Mike Frysingerac45af42008-10-14 04:52:00 -0400232
233 /* Set all the pins to peripheral mode */
Mike Frysinger8339ad72010-06-02 05:56:22 -0400234 peripheral_request_list(pins, "bfin_mac");
Mike Frysingerac45af42008-10-14 04:52:00 -0400235
236 /* Odd word alignment for Receive Frame DMA word */
237 /* Configure checksum support and rcve frame word alignment */
Mike Frysingera7ec6ac2008-10-20 13:59:51 -0400238 bfin_write_EMAC_SYSCTL(RXDWA | RXCKS | SET_MDCDIV(MDC_FREQ_TO_DIV(CONFIG_PHY_CLOCK_FREQ)));
Mike Frysingerac45af42008-10-14 04:52:00 -0400239
240 /* turn on auto-negotiation and wait for link to come up */
Mike Frysingera7ec6ac2008-10-20 13:59:51 -0400241 bfin_miiphy_write(dev->name, CONFIG_PHY_ADDR, MII_BMCR, BMCR_ANENABLE);
Mike Frysingerac45af42008-10-14 04:52:00 -0400242 count = 0;
243 while (1) {
244 ++count;
Mike Frysingera7ec6ac2008-10-20 13:59:51 -0400245 if (bfin_miiphy_read(dev->name, CONFIG_PHY_ADDR, MII_BMSR, &phydat))
Mike Frysingerac45af42008-10-14 04:52:00 -0400246 return -1;
247 if (phydat & BMSR_LSTATUS)
248 break;
249 if (count > 30000) {
250 printf("%s: link down, check cable\n", dev->name);
251 return -1;
252 }
253 udelay(100);
254 }
255
256 /* see what kind of link we have */
Mike Frysingera7ec6ac2008-10-20 13:59:51 -0400257 if (bfin_miiphy_read(dev->name, CONFIG_PHY_ADDR, MII_LPA, &phydat))
Mike Frysingerac45af42008-10-14 04:52:00 -0400258 return -1;
259 if (phydat & LPA_DUPLEX)
260 *opmode = FDMODE;
261 else
262 *opmode = 0;
263
264 bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
265
266 /* Initialize the TX DMA channel registers */
Mike Frysinger0c714812010-07-25 16:38:12 -0400267 bfin_write_DMA2_X_COUNT(0);
268 bfin_write_DMA2_X_MODIFY(4);
269 bfin_write_DMA2_Y_COUNT(0);
270 bfin_write_DMA2_Y_MODIFY(0);
Mike Frysingerac45af42008-10-14 04:52:00 -0400271
272 /* Initialize the RX DMA channel registers */
Mike Frysinger0c714812010-07-25 16:38:12 -0400273 bfin_write_DMA1_X_COUNT(0);
274 bfin_write_DMA1_X_MODIFY(4);
275 bfin_write_DMA1_Y_COUNT(0);
276 bfin_write_DMA1_Y_MODIFY(0);
Mike Frysingerac45af42008-10-14 04:52:00 -0400277
278 return 0;
279}
280
Mike Frysinger4324dc72010-04-27 14:15:28 -0400281static int bfin_EMAC_setup_addr(struct eth_device *dev)
282{
Mike Frysinger0c714812010-07-25 16:38:12 -0400283 bfin_write_EMAC_ADDRLO(
Mike Frysinger4324dc72010-04-27 14:15:28 -0400284 dev->enetaddr[0] |
285 dev->enetaddr[1] << 8 |
286 dev->enetaddr[2] << 16 |
Mike Frysinger0c714812010-07-25 16:38:12 -0400287 dev->enetaddr[3] << 24
288 );
289 bfin_write_EMAC_ADDRHI(
Mike Frysinger4324dc72010-04-27 14:15:28 -0400290 dev->enetaddr[4] |
Mike Frysinger0c714812010-07-25 16:38:12 -0400291 dev->enetaddr[5] << 8
292 );
Mike Frysinger4324dc72010-04-27 14:15:28 -0400293 return 0;
294}
295
Mike Frysinger395bce42008-02-24 23:58:13 -0500296static int bfin_EMAC_init(struct eth_device *dev, bd_t *bd)
Aubrey Li26bf7de2007-03-19 01:24:52 +0800297{
298 u32 opmode;
299 int dat;
300 int i;
Mike Frysinger8eed6ca2008-11-05 06:36:15 -0500301 debug("Eth_init: ......\n");
Aubrey Li26bf7de2007-03-19 01:24:52 +0800302
303 txIdx = 0;
304 rxIdx = 0;
305
Mike Frysingerac45af42008-10-14 04:52:00 -0400306 /* Initialize System Register */
307 if (bfin_miiphy_init(dev, &dat) < 0)
Aubrey Li26bf7de2007-03-19 01:24:52 +0800308 return -1;
309
Mike Frysingerac45af42008-10-14 04:52:00 -0400310 /* Initialize EMAC address */
Mike Frysinger4324dc72010-04-27 14:15:28 -0400311 bfin_EMAC_setup_addr(dev);
Aubrey Li26bf7de2007-03-19 01:24:52 +0800312
Mike Frysingerac45af42008-10-14 04:52:00 -0400313 /* Initialize TX and RX buffer */
Aubrey Li26bf7de2007-03-19 01:24:52 +0800314 for (i = 0; i < PKTBUFSRX; i++) {
315 rxbuf[i] = SetupRxBuffer(i);
316 if (i > 0) {
Mike Frysinger6d7d4802009-01-08 11:57:57 -0500317 rxbuf[i - 1]->Dma[1].NEXT_DESC_PTR = rxbuf[i]->Dma;
Aubrey Li26bf7de2007-03-19 01:24:52 +0800318 if (i == (PKTBUFSRX - 1))
Mike Frysinger6d7d4802009-01-08 11:57:57 -0500319 rxbuf[i]->Dma[1].NEXT_DESC_PTR = rxbuf[0]->Dma;
Aubrey Li26bf7de2007-03-19 01:24:52 +0800320 }
321 }
322 for (i = 0; i < TX_BUF_CNT; i++) {
323 txbuf[i] = SetupTxBuffer(i);
324 if (i > 0) {
Mike Frysinger6d7d4802009-01-08 11:57:57 -0500325 txbuf[i - 1]->Dma[1].NEXT_DESC_PTR = txbuf[i]->Dma;
Aubrey Li26bf7de2007-03-19 01:24:52 +0800326 if (i == (TX_BUF_CNT - 1))
Mike Frysinger6d7d4802009-01-08 11:57:57 -0500327 txbuf[i]->Dma[1].NEXT_DESC_PTR = txbuf[0]->Dma;
Aubrey Li26bf7de2007-03-19 01:24:52 +0800328 }
329 }
330
331 /* Set RX DMA */
Mike Frysinger0c714812010-07-25 16:38:12 -0400332 bfin_write_DMA1_NEXT_DESC_PTR(rxbuf[0]->Dma);
333 bfin_write_DMA1_CONFIG(rxbuf[0]->Dma[0].CONFIG_DATA);
Aubrey Li26bf7de2007-03-19 01:24:52 +0800334
335 /* Wait MII done */
Mike Frysingerac45af42008-10-14 04:52:00 -0400336 bfin_miiphy_wait();
Aubrey Li26bf7de2007-03-19 01:24:52 +0800337
338 /* We enable only RX here */
339 /* ASTP : Enable Automatic Pad Stripping
340 PR : Promiscuous Mode for test
341 PSF : Receive frames with total length less than 64 bytes.
342 FDMODE : Full Duplex Mode
343 LB : Internal Loopback for test
344 RE : Receiver Enable */
345 if (dat == FDMODE)
346 opmode = ASTP | FDMODE | PSF;
347 else
348 opmode = ASTP | PSF;
349 opmode |= RE;
Mike Frysinger092d2482008-12-09 17:46:21 -0500350#ifdef CONFIG_RMII
Aubrey Li26bf7de2007-03-19 01:24:52 +0800351 opmode |= TE | RMII;
352#endif
353 /* Turn on the EMAC */
Mike Frysinger0c714812010-07-25 16:38:12 -0400354 bfin_write_EMAC_OPMODE(opmode);
Aubrey Li26bf7de2007-03-19 01:24:52 +0800355 return 0;
356}
357
358static void bfin_EMAC_halt(struct eth_device *dev)
359{
Mike Frysinger8eed6ca2008-11-05 06:36:15 -0500360 debug("Eth_halt: ......\n");
Aubrey Li26bf7de2007-03-19 01:24:52 +0800361 /* Turn off the EMAC */
Mike Frysinger0c714812010-07-25 16:38:12 -0400362 bfin_write_EMAC_OPMODE(0);
Aubrey Li26bf7de2007-03-19 01:24:52 +0800363 /* Turn off the EMAC RX DMA */
Mike Frysinger0c714812010-07-25 16:38:12 -0400364 bfin_write_DMA1_CONFIG(0);
365 bfin_write_DMA2_CONFIG(0);
Aubrey Li26bf7de2007-03-19 01:24:52 +0800366}
367
Aubrey Li26bf7de2007-03-19 01:24:52 +0800368ADI_ETHER_BUFFER *SetupRxBuffer(int no)
369{
370 ADI_ETHER_FRAME_BUFFER *frmbuf;
371 ADI_ETHER_BUFFER *buf;
372 int nobytes_buffer = sizeof(ADI_ETHER_BUFFER[2]) / 2; /* ensure a multi. of 4 */
373 int total_size = nobytes_buffer + RECV_BUFSIZE;
374
Mike Frysinger6d7d4802009-01-08 11:57:57 -0500375 buf = (void *) (RXBUF_BASE_ADDR + no * total_size);
376 frmbuf = (void *) (RXBUF_BASE_ADDR + no * total_size + nobytes_buffer);
Aubrey Li26bf7de2007-03-19 01:24:52 +0800377
378 memset(buf, 0x00, nobytes_buffer);
379 buf->FrmData = frmbuf;
380 memset(frmbuf, 0xfe, RECV_BUFSIZE);
381
382 /* set up first desc to point to receive frame buffer */
383 buf->Dma[0].NEXT_DESC_PTR = &(buf->Dma[1]);
384 buf->Dma[0].START_ADDR = (u32) buf->FrmData;
385 buf->Dma[0].CONFIG.b_DMA_EN = 1; /* enabled */
386 buf->Dma[0].CONFIG.b_WNR = 1; /* Write to memory */
387 buf->Dma[0].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
388 buf->Dma[0].CONFIG.b_NDSIZE = 5; /* 5 half words is desc size. */
389 buf->Dma[0].CONFIG.b_FLOW = 7; /* large desc flow */
390
391 /* set up second desc to point to status word */
Mike Frysinger6d7d4802009-01-08 11:57:57 -0500392 buf->Dma[1].NEXT_DESC_PTR = buf->Dma;
Aubrey Li26bf7de2007-03-19 01:24:52 +0800393 buf->Dma[1].START_ADDR = (u32) & buf->IPHdrChksum;
394 buf->Dma[1].CONFIG.b_DMA_EN = 1; /* enabled */
395 buf->Dma[1].CONFIG.b_WNR = 1; /* Write to memory */
396 buf->Dma[1].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
397 buf->Dma[1].CONFIG.b_DI_EN = 1; /* enable interrupt */
398 buf->Dma[1].CONFIG.b_NDSIZE = 5; /* must be 0 when FLOW is 0 */
399 buf->Dma[1].CONFIG.b_FLOW = 7; /* stop */
400
401 return buf;
402}
403
404ADI_ETHER_BUFFER *SetupTxBuffer(int no)
405{
406 ADI_ETHER_FRAME_BUFFER *frmbuf;
407 ADI_ETHER_BUFFER *buf;
408 int nobytes_buffer = sizeof(ADI_ETHER_BUFFER[2]) / 2; /* ensure a multi. of 4 */
409 int total_size = nobytes_buffer + RECV_BUFSIZE;
410
Mike Frysinger6d7d4802009-01-08 11:57:57 -0500411 buf = (void *) (TXBUF_BASE_ADDR + no * total_size);
412 frmbuf = (void *) (TXBUF_BASE_ADDR + no * total_size + nobytes_buffer);
Aubrey Li26bf7de2007-03-19 01:24:52 +0800413
414 memset(buf, 0x00, nobytes_buffer);
415 buf->FrmData = frmbuf;
416 memset(frmbuf, 0x00, RECV_BUFSIZE);
417
418 /* set up first desc to point to receive frame buffer */
419 buf->Dma[0].NEXT_DESC_PTR = &(buf->Dma[1]);
420 buf->Dma[0].START_ADDR = (u32) buf->FrmData;
421 buf->Dma[0].CONFIG.b_DMA_EN = 1; /* enabled */
422 buf->Dma[0].CONFIG.b_WNR = 0; /* Read to memory */
423 buf->Dma[0].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
424 buf->Dma[0].CONFIG.b_NDSIZE = 5; /* 5 half words is desc size. */
425 buf->Dma[0].CONFIG.b_FLOW = 7; /* large desc flow */
426
427 /* set up second desc to point to status word */
428 buf->Dma[1].NEXT_DESC_PTR = &(buf->Dma[0]);
429 buf->Dma[1].START_ADDR = (u32) & buf->StatusWord;
430 buf->Dma[1].CONFIG.b_DMA_EN = 1; /* enabled */
431 buf->Dma[1].CONFIG.b_WNR = 1; /* Write to memory */
432 buf->Dma[1].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
433 buf->Dma[1].CONFIG.b_DI_EN = 1; /* enable interrupt */
434 buf->Dma[1].CONFIG.b_NDSIZE = 0; /* must be 0 when FLOW is 0 */
435 buf->Dma[1].CONFIG.b_FLOW = 0; /* stop */
436
437 return buf;
438}
439
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200440#if defined(CONFIG_POST) && defined(CONFIG_SYS_POST_ETHER)
Aubrey Li26bf7de2007-03-19 01:24:52 +0800441int ether_post_test(int flags)
442{
443 uchar buf[64];
444 int i, value = 0;
445 int length;
Mike Frysinger0c714812010-07-25 16:38:12 -0400446 uint addr;
Aubrey Li26bf7de2007-03-19 01:24:52 +0800447
448 printf("\n--------");
449 bfin_EMAC_init(NULL, NULL);
450 /* construct the package */
Mike Frysinger0c714812010-07-25 16:38:12 -0400451 addr = bfin_read_EMAC_ADDRLO();
452 buf[0] = buf[6] = addr;
453 buf[1] = buf[7] = addr >> 8;
454 buf[2] = buf[8] = addr >> 16;
455 buf[3] = buf[9] = addr >> 24;
456 addr = bfin_read_EMAC_ADDRHI();
457 buf[4] = buf[10] = addr;
458 buf[5] = buf[11] = addr >> 8;
Aubrey Li26bf7de2007-03-19 01:24:52 +0800459 buf[12] = 0x08; /* Type: ARP */
460 buf[13] = 0x06;
461 buf[14] = 0x00; /* Hardware type: Ethernet */
462 buf[15] = 0x01;
463 buf[16] = 0x08; /* Protocal type: IP */
464 buf[17] = 0x00;
465 buf[18] = 0x06; /* Hardware size */
466 buf[19] = 0x04; /* Protocol size */
467 buf[20] = 0x00; /* Opcode: request */
468 buf[21] = 0x01;
469
470 for (i = 0; i < 42; i++)
471 buf[i + 22] = i;
472 printf("--------Send 64 bytes......\n");
Joe Hershberger10cbe3b2012-05-22 18:36:19 +0000473 bfin_EMAC_send(NULL, buf, 64);
Aubrey Li26bf7de2007-03-19 01:24:52 +0800474 for (i = 0; i < 100; i++) {
475 udelay(10000);
476 if ((rxbuf[rxIdx]->StatusWord & RX_COMP) != 0) {
477 value = 1;
478 break;
479 }
480 }
481 if (value == 0) {
482 printf("--------EMAC can't receive any data\n");
483 eth_halt();
484 return -1;
485 }
486 length = rxbuf[rxIdx]->StatusWord & 0x000007FF - 4;
487 for (i = 0; i < length; i++) {
488 if (rxbuf[rxIdx]->FrmData->Dest[i] != buf[i]) {
489 printf("--------EMAC receive error data!\n");
490 eth_halt();
491 return -1;
492 }
493 }
494 printf("--------receive %d bytes, matched\n", length);
495 bfin_EMAC_halt(NULL);
496 return 0;
497}
498#endif