blob: 0ffd59d49780ec5ddf76a357079dd74ccbc72031 [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>
Mike Frysingereca7f572010-06-02 05:56:22 -040019#include <asm/portmux.h>
Mike Frysinger66c4cf42008-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 Frysingerb11eb3f2008-02-24 23:58:13 -050024#include "bfin_mac.h"
25
Mike Frysinger65b7d932008-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 Li10ebdd92007-03-19 01:24:52 +080033#ifdef CONFIG_POST
34#include <post.h>
35#endif
36
Aubrey Li10ebdd92007-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 Denka1be4762008-05-20 16:00:29 +020041#define TOUT_LOOP 1000000
Aubrey Li10ebdd92007-03-19 01:24:52 +080042
Mike Frysinger10be09f2009-01-08 11:57:57 -050043static ADI_ETHER_BUFFER *txbuf[TX_BUF_CNT];
44static ADI_ETHER_BUFFER *rxbuf[PKTBUFSRX];
Aubrey Li10ebdd92007-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 Li10ebdd92007-03-19 01:24:52 +080048/* DMAx_CONFIG values at DMA Restart */
Mike Frysinger10be09f2009-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 Frysingerb11eb3f2008-02-24 23:58:13 -050064};
Aubrey Li10ebdd92007-03-19 01:24:52 +080065
Mike Frysinger709ad972008-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 Frysinger5ff5fdb2010-07-27 18:35:08 -040074static int bfin_miiphy_read(const char *devname, uchar addr, uchar reg, ushort *val)
Mike Frysinger709ad972008-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 Frysinger5ff5fdb2010-07-27 18:35:08 -040085static int bfin_miiphy_write(const char *devname, uchar addr, uchar reg, ushort val)
Mike Frysinger709ad972008-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 Frysingerb11eb3f2008-02-24 23:58:13 -050094int bfin_EMAC_initialize(bd_t *bis)
Aubrey Li10ebdd92007-03-19 01:24:52 +080095{
96 struct eth_device *dev;
Mike Frysinger709ad972008-10-14 04:52:00 -040097 dev = malloc(sizeof(*dev));
Aubrey Li10ebdd92007-03-19 01:24:52 +080098 if (dev == NULL)
99 hang();
100
101 memset(dev, 0, sizeof(*dev));
Mike Frysinger95d834a2010-06-09 21:50:48 -0400102 strcpy(dev->name, "bfin_mac");
Aubrey Li10ebdd92007-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 Frysinger35be8722010-04-27 14:15:28 -0400110 dev->write_hwaddr = bfin_EMAC_setup_addr;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800111
112 eth_register(dev);
113
Mike Frysinger709ad972008-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 Warren82c2c6a2008-07-11 23:15:28 -0700118 return 0;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800119}
120
Joe Hershbergere4e04882012-05-22 18:36:19 +0000121static int bfin_EMAC_send(struct eth_device *dev, void *packet, int length)
Aubrey Li10ebdd92007-03-19 01:24:52 +0800122{
123 int i;
124 int result = 0;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800125
126 if (length <= 0) {
127 printf("Ethernet: bad packet size: %d\n", length);
128 goto out;
129 }
130
Mike Frysingerdec3c782010-07-25 16:38:12 -0400131 if (bfin_read_DMA2_IRQ_STATUS() & DMA_ERR) {
Aubrey Li10ebdd92007-03-19 01:24:52 +0800132 printf("Ethernet: tx DMA error\n");
133 goto out;
134 }
135
Mike Frysingerdec3c782010-07-25 16:38:12 -0400136 for (i = 0; (bfin_read_DMA2_IRQ_STATUS() & DMA_RUN); ++i) {
Aubrey Li10ebdd92007-03-19 01:24:52 +0800137 if (i > TOUT_LOOP) {
138 puts("Ethernet: tx time out\n");
139 goto out;
140 }
141 }
142 txbuf[txIdx]->FrmData->NoBytes = length;
143 memcpy(txbuf[txIdx]->FrmData->Dest, (void *)packet, length);
144 txbuf[txIdx]->Dma[0].START_ADDR = (u32) txbuf[txIdx]->FrmData;
Mike Frysingerdec3c782010-07-25 16:38:12 -0400145 bfin_write_DMA2_NEXT_DESC_PTR(txbuf[txIdx]->Dma);
146 bfin_write_DMA2_CONFIG(txdmacfg.data);
147 bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800148
149 for (i = 0; (txbuf[txIdx]->StatusWord & TX_COMP) == 0; i++) {
150 if (i > TOUT_LOOP) {
151 puts("Ethernet: tx error\n");
152 goto out;
153 }
154 }
155 result = txbuf[txIdx]->StatusWord;
156 txbuf[txIdx]->StatusWord = 0;
157 if ((txIdx + 1) >= TX_BUF_CNT)
158 txIdx = 0;
159 else
160 txIdx++;
Mike Frysingerb11eb3f2008-02-24 23:58:13 -0500161 out:
Mike Frysinger3ac20c32008-11-05 06:36:15 -0500162 debug("BFIN EMAC send: length = %d\n", length);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800163 return result;
164}
165
166static int bfin_EMAC_recv(struct eth_device *dev)
167{
168 int length = 0;
169
170 for (;;) {
171 if ((rxbuf[rxIdx]->StatusWord & RX_COMP) == 0) {
172 length = -1;
173 break;
174 }
175 if ((rxbuf[rxIdx]->StatusWord & RX_DMAO) != 0) {
176 printf("Ethernet: rx dma overrun\n");
177 break;
178 }
179 if ((rxbuf[rxIdx]->StatusWord & RX_OK) == 0) {
180 printf("Ethernet: rx error\n");
181 break;
182 }
183 length = rxbuf[rxIdx]->StatusWord & 0x000007FF;
184 if (length <= 4) {
185 printf("Ethernet: bad frame\n");
186 break;
187 }
Robin Getzb5edf812009-08-24 10:33:39 -0400188
189 debug("%s: len = %d\n", __func__, length - 4);
190
Mike Frysingerd1d3edd2012-07-19 00:08:47 -0400191 NetRxPackets[rxIdx] = rxbuf[rxIdx]->FrmData->Dest;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800192 NetReceive(NetRxPackets[rxIdx], length - 4);
Mike Frysingerdec3c782010-07-25 16:38:12 -0400193 bfin_write_DMA1_IRQ_STATUS(DMA_DONE | DMA_ERR);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800194 rxbuf[rxIdx]->StatusWord = 0x00000000;
195 if ((rxIdx + 1) >= PKTBUFSRX)
196 rxIdx = 0;
197 else
198 rxIdx++;
199 }
200
201 return length;
202}
203
204/**************************************************************
205 *
206 * Ethernet Initialization Routine
207 *
208 *************************************************************/
209
Mike Frysinger709ad972008-10-14 04:52:00 -0400210/* MDC = SCLK / MDC_freq / 2 - 1 */
211#define MDC_FREQ_TO_DIV(mdc_freq) (get_sclk() / (mdc_freq) / 2 - 1)
212
Mike Frysingereca7f572010-06-02 05:56:22 -0400213#ifndef CONFIG_BFIN_MAC_PINS
214# ifdef CONFIG_RMII
215# define CONFIG_BFIN_MAC_PINS P_RMII0
216# else
217# define CONFIG_BFIN_MAC_PINS P_MII0
218# endif
219#endif
220
Mike Frysinger709ad972008-10-14 04:52:00 -0400221static int bfin_miiphy_init(struct eth_device *dev, int *opmode)
222{
Mike Frysingereca7f572010-06-02 05:56:22 -0400223 const unsigned short pins[] = CONFIG_BFIN_MAC_PINS;
Mike Frysinger709ad972008-10-14 04:52:00 -0400224 u16 phydat;
225 size_t count;
226
227 /* Enable PHY output */
Mike Frysingerdec3c782010-07-25 16:38:12 -0400228 bfin_write_VR_CTL(bfin_read_VR_CTL() | CLKBUFOE);
Mike Frysinger709ad972008-10-14 04:52:00 -0400229
230 /* Set all the pins to peripheral mode */
Mike Frysingereca7f572010-06-02 05:56:22 -0400231 peripheral_request_list(pins, "bfin_mac");
Mike Frysinger709ad972008-10-14 04:52:00 -0400232
233 /* Odd word alignment for Receive Frame DMA word */
234 /* Configure checksum support and rcve frame word alignment */
Mike Frysinger65b7d932008-10-20 13:59:51 -0400235 bfin_write_EMAC_SYSCTL(RXDWA | RXCKS | SET_MDCDIV(MDC_FREQ_TO_DIV(CONFIG_PHY_CLOCK_FREQ)));
Mike Frysinger709ad972008-10-14 04:52:00 -0400236
237 /* turn on auto-negotiation and wait for link to come up */
Mike Frysinger65b7d932008-10-20 13:59:51 -0400238 bfin_miiphy_write(dev->name, CONFIG_PHY_ADDR, MII_BMCR, BMCR_ANENABLE);
Mike Frysinger709ad972008-10-14 04:52:00 -0400239 count = 0;
240 while (1) {
241 ++count;
Mike Frysinger65b7d932008-10-20 13:59:51 -0400242 if (bfin_miiphy_read(dev->name, CONFIG_PHY_ADDR, MII_BMSR, &phydat))
Mike Frysinger709ad972008-10-14 04:52:00 -0400243 return -1;
244 if (phydat & BMSR_LSTATUS)
245 break;
246 if (count > 30000) {
247 printf("%s: link down, check cable\n", dev->name);
248 return -1;
249 }
250 udelay(100);
251 }
252
253 /* see what kind of link we have */
Mike Frysinger65b7d932008-10-20 13:59:51 -0400254 if (bfin_miiphy_read(dev->name, CONFIG_PHY_ADDR, MII_LPA, &phydat))
Mike Frysinger709ad972008-10-14 04:52:00 -0400255 return -1;
256 if (phydat & LPA_DUPLEX)
257 *opmode = FDMODE;
258 else
259 *opmode = 0;
260
261 bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
262
263 /* Initialize the TX DMA channel registers */
Mike Frysingerdec3c782010-07-25 16:38:12 -0400264 bfin_write_DMA2_X_COUNT(0);
265 bfin_write_DMA2_X_MODIFY(4);
266 bfin_write_DMA2_Y_COUNT(0);
267 bfin_write_DMA2_Y_MODIFY(0);
Mike Frysinger709ad972008-10-14 04:52:00 -0400268
269 /* Initialize the RX DMA channel registers */
Mike Frysingerdec3c782010-07-25 16:38:12 -0400270 bfin_write_DMA1_X_COUNT(0);
271 bfin_write_DMA1_X_MODIFY(4);
272 bfin_write_DMA1_Y_COUNT(0);
273 bfin_write_DMA1_Y_MODIFY(0);
Mike Frysinger709ad972008-10-14 04:52:00 -0400274
275 return 0;
276}
277
Mike Frysinger35be8722010-04-27 14:15:28 -0400278static int bfin_EMAC_setup_addr(struct eth_device *dev)
279{
Mike Frysingerdec3c782010-07-25 16:38:12 -0400280 bfin_write_EMAC_ADDRLO(
Mike Frysinger35be8722010-04-27 14:15:28 -0400281 dev->enetaddr[0] |
282 dev->enetaddr[1] << 8 |
283 dev->enetaddr[2] << 16 |
Mike Frysingerdec3c782010-07-25 16:38:12 -0400284 dev->enetaddr[3] << 24
285 );
286 bfin_write_EMAC_ADDRHI(
Mike Frysinger35be8722010-04-27 14:15:28 -0400287 dev->enetaddr[4] |
Mike Frysingerdec3c782010-07-25 16:38:12 -0400288 dev->enetaddr[5] << 8
289 );
Mike Frysinger35be8722010-04-27 14:15:28 -0400290 return 0;
291}
292
Mike Frysingerb11eb3f2008-02-24 23:58:13 -0500293static int bfin_EMAC_init(struct eth_device *dev, bd_t *bd)
Aubrey Li10ebdd92007-03-19 01:24:52 +0800294{
295 u32 opmode;
296 int dat;
297 int i;
Mike Frysinger3ac20c32008-11-05 06:36:15 -0500298 debug("Eth_init: ......\n");
Aubrey Li10ebdd92007-03-19 01:24:52 +0800299
300 txIdx = 0;
301 rxIdx = 0;
302
Mike Frysinger709ad972008-10-14 04:52:00 -0400303 /* Initialize System Register */
304 if (bfin_miiphy_init(dev, &dat) < 0)
Aubrey Li10ebdd92007-03-19 01:24:52 +0800305 return -1;
306
Mike Frysinger709ad972008-10-14 04:52:00 -0400307 /* Initialize EMAC address */
Mike Frysinger35be8722010-04-27 14:15:28 -0400308 bfin_EMAC_setup_addr(dev);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800309
Mike Frysinger709ad972008-10-14 04:52:00 -0400310 /* Initialize TX and RX buffer */
Aubrey Li10ebdd92007-03-19 01:24:52 +0800311 for (i = 0; i < PKTBUFSRX; i++) {
312 rxbuf[i] = SetupRxBuffer(i);
313 if (i > 0) {
Mike Frysinger10be09f2009-01-08 11:57:57 -0500314 rxbuf[i - 1]->Dma[1].NEXT_DESC_PTR = rxbuf[i]->Dma;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800315 if (i == (PKTBUFSRX - 1))
Mike Frysinger10be09f2009-01-08 11:57:57 -0500316 rxbuf[i]->Dma[1].NEXT_DESC_PTR = rxbuf[0]->Dma;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800317 }
318 }
319 for (i = 0; i < TX_BUF_CNT; i++) {
320 txbuf[i] = SetupTxBuffer(i);
321 if (i > 0) {
Mike Frysinger10be09f2009-01-08 11:57:57 -0500322 txbuf[i - 1]->Dma[1].NEXT_DESC_PTR = txbuf[i]->Dma;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800323 if (i == (TX_BUF_CNT - 1))
Mike Frysinger10be09f2009-01-08 11:57:57 -0500324 txbuf[i]->Dma[1].NEXT_DESC_PTR = txbuf[0]->Dma;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800325 }
326 }
327
328 /* Set RX DMA */
Mike Frysingerdec3c782010-07-25 16:38:12 -0400329 bfin_write_DMA1_NEXT_DESC_PTR(rxbuf[0]->Dma);
330 bfin_write_DMA1_CONFIG(rxbuf[0]->Dma[0].CONFIG_DATA);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800331
332 /* Wait MII done */
Mike Frysinger709ad972008-10-14 04:52:00 -0400333 bfin_miiphy_wait();
Aubrey Li10ebdd92007-03-19 01:24:52 +0800334
335 /* We enable only RX here */
336 /* ASTP : Enable Automatic Pad Stripping
337 PR : Promiscuous Mode for test
338 PSF : Receive frames with total length less than 64 bytes.
339 FDMODE : Full Duplex Mode
340 LB : Internal Loopback for test
341 RE : Receiver Enable */
342 if (dat == FDMODE)
343 opmode = ASTP | FDMODE | PSF;
344 else
345 opmode = ASTP | PSF;
346 opmode |= RE;
Mike Frysingerd11ab932008-12-09 17:46:21 -0500347#ifdef CONFIG_RMII
Aubrey Li10ebdd92007-03-19 01:24:52 +0800348 opmode |= TE | RMII;
349#endif
350 /* Turn on the EMAC */
Mike Frysingerdec3c782010-07-25 16:38:12 -0400351 bfin_write_EMAC_OPMODE(opmode);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800352 return 0;
353}
354
355static void bfin_EMAC_halt(struct eth_device *dev)
356{
Mike Frysinger3ac20c32008-11-05 06:36:15 -0500357 debug("Eth_halt: ......\n");
Aubrey Li10ebdd92007-03-19 01:24:52 +0800358 /* Turn off the EMAC */
Mike Frysingerdec3c782010-07-25 16:38:12 -0400359 bfin_write_EMAC_OPMODE(0);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800360 /* Turn off the EMAC RX DMA */
Mike Frysingerdec3c782010-07-25 16:38:12 -0400361 bfin_write_DMA1_CONFIG(0);
362 bfin_write_DMA2_CONFIG(0);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800363}
364
Aubrey Li10ebdd92007-03-19 01:24:52 +0800365ADI_ETHER_BUFFER *SetupRxBuffer(int no)
366{
367 ADI_ETHER_FRAME_BUFFER *frmbuf;
368 ADI_ETHER_BUFFER *buf;
369 int nobytes_buffer = sizeof(ADI_ETHER_BUFFER[2]) / 2; /* ensure a multi. of 4 */
370 int total_size = nobytes_buffer + RECV_BUFSIZE;
371
Mike Frysinger10be09f2009-01-08 11:57:57 -0500372 buf = (void *) (RXBUF_BASE_ADDR + no * total_size);
373 frmbuf = (void *) (RXBUF_BASE_ADDR + no * total_size + nobytes_buffer);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800374
375 memset(buf, 0x00, nobytes_buffer);
376 buf->FrmData = frmbuf;
377 memset(frmbuf, 0xfe, RECV_BUFSIZE);
378
379 /* set up first desc to point to receive frame buffer */
380 buf->Dma[0].NEXT_DESC_PTR = &(buf->Dma[1]);
381 buf->Dma[0].START_ADDR = (u32) buf->FrmData;
382 buf->Dma[0].CONFIG.b_DMA_EN = 1; /* enabled */
383 buf->Dma[0].CONFIG.b_WNR = 1; /* Write to memory */
384 buf->Dma[0].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
385 buf->Dma[0].CONFIG.b_NDSIZE = 5; /* 5 half words is desc size. */
386 buf->Dma[0].CONFIG.b_FLOW = 7; /* large desc flow */
387
388 /* set up second desc to point to status word */
Mike Frysinger10be09f2009-01-08 11:57:57 -0500389 buf->Dma[1].NEXT_DESC_PTR = buf->Dma;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800390 buf->Dma[1].START_ADDR = (u32) & buf->IPHdrChksum;
391 buf->Dma[1].CONFIG.b_DMA_EN = 1; /* enabled */
392 buf->Dma[1].CONFIG.b_WNR = 1; /* Write to memory */
393 buf->Dma[1].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
394 buf->Dma[1].CONFIG.b_DI_EN = 1; /* enable interrupt */
395 buf->Dma[1].CONFIG.b_NDSIZE = 5; /* must be 0 when FLOW is 0 */
396 buf->Dma[1].CONFIG.b_FLOW = 7; /* stop */
397
398 return buf;
399}
400
401ADI_ETHER_BUFFER *SetupTxBuffer(int no)
402{
403 ADI_ETHER_FRAME_BUFFER *frmbuf;
404 ADI_ETHER_BUFFER *buf;
405 int nobytes_buffer = sizeof(ADI_ETHER_BUFFER[2]) / 2; /* ensure a multi. of 4 */
406 int total_size = nobytes_buffer + RECV_BUFSIZE;
407
Mike Frysinger10be09f2009-01-08 11:57:57 -0500408 buf = (void *) (TXBUF_BASE_ADDR + no * total_size);
409 frmbuf = (void *) (TXBUF_BASE_ADDR + no * total_size + nobytes_buffer);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800410
411 memset(buf, 0x00, nobytes_buffer);
412 buf->FrmData = frmbuf;
413 memset(frmbuf, 0x00, RECV_BUFSIZE);
414
415 /* set up first desc to point to receive frame buffer */
416 buf->Dma[0].NEXT_DESC_PTR = &(buf->Dma[1]);
417 buf->Dma[0].START_ADDR = (u32) buf->FrmData;
418 buf->Dma[0].CONFIG.b_DMA_EN = 1; /* enabled */
419 buf->Dma[0].CONFIG.b_WNR = 0; /* Read to memory */
420 buf->Dma[0].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
421 buf->Dma[0].CONFIG.b_NDSIZE = 5; /* 5 half words is desc size. */
422 buf->Dma[0].CONFIG.b_FLOW = 7; /* large desc flow */
423
424 /* set up second desc to point to status word */
425 buf->Dma[1].NEXT_DESC_PTR = &(buf->Dma[0]);
426 buf->Dma[1].START_ADDR = (u32) & buf->StatusWord;
427 buf->Dma[1].CONFIG.b_DMA_EN = 1; /* enabled */
428 buf->Dma[1].CONFIG.b_WNR = 1; /* Write to memory */
429 buf->Dma[1].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
430 buf->Dma[1].CONFIG.b_DI_EN = 1; /* enable interrupt */
431 buf->Dma[1].CONFIG.b_NDSIZE = 0; /* must be 0 when FLOW is 0 */
432 buf->Dma[1].CONFIG.b_FLOW = 0; /* stop */
433
434 return buf;
435}
436
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200437#if defined(CONFIG_POST) && defined(CONFIG_SYS_POST_ETHER)
Aubrey Li10ebdd92007-03-19 01:24:52 +0800438int ether_post_test(int flags)
439{
440 uchar buf[64];
441 int i, value = 0;
442 int length;
Mike Frysingerdec3c782010-07-25 16:38:12 -0400443 uint addr;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800444
445 printf("\n--------");
446 bfin_EMAC_init(NULL, NULL);
447 /* construct the package */
Mike Frysingerdec3c782010-07-25 16:38:12 -0400448 addr = bfin_read_EMAC_ADDRLO();
449 buf[0] = buf[6] = addr;
450 buf[1] = buf[7] = addr >> 8;
451 buf[2] = buf[8] = addr >> 16;
452 buf[3] = buf[9] = addr >> 24;
453 addr = bfin_read_EMAC_ADDRHI();
454 buf[4] = buf[10] = addr;
455 buf[5] = buf[11] = addr >> 8;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800456 buf[12] = 0x08; /* Type: ARP */
457 buf[13] = 0x06;
458 buf[14] = 0x00; /* Hardware type: Ethernet */
459 buf[15] = 0x01;
460 buf[16] = 0x08; /* Protocal type: IP */
461 buf[17] = 0x00;
462 buf[18] = 0x06; /* Hardware size */
463 buf[19] = 0x04; /* Protocol size */
464 buf[20] = 0x00; /* Opcode: request */
465 buf[21] = 0x01;
466
467 for (i = 0; i < 42; i++)
468 buf[i + 22] = i;
469 printf("--------Send 64 bytes......\n");
Joe Hershbergere4e04882012-05-22 18:36:19 +0000470 bfin_EMAC_send(NULL, buf, 64);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800471 for (i = 0; i < 100; i++) {
472 udelay(10000);
473 if ((rxbuf[rxIdx]->StatusWord & RX_COMP) != 0) {
474 value = 1;
475 break;
476 }
477 }
478 if (value == 0) {
479 printf("--------EMAC can't receive any data\n");
480 eth_halt();
481 return -1;
482 }
483 length = rxbuf[rxIdx]->StatusWord & 0x000007FF - 4;
484 for (i = 0; i < length; i++) {
485 if (rxbuf[rxIdx]->FrmData->Dest[i] != buf[i]) {
486 printf("--------EMAC receive error data!\n");
487 eth_halt();
488 return -1;
489 }
490 }
491 printf("--------receive %d bytes, matched\n", length);
492 bfin_EMAC_halt(NULL);
493 return 0;
494}
495#endif