blob: ec45b6355a61ee927644a8e5044d59183ae32f55 [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 Frysinger66c4cf42008-02-04 19:26:55 -050019#include <asm/mach-common/bits/dma.h>
20#include <asm/mach-common/bits/emac.h>
21#include <asm/mach-common/bits/pll.h>
22
Mike Frysingerb11eb3f2008-02-24 23:58:13 -050023#include "bfin_mac.h"
24
Mike Frysinger65b7d932008-10-20 13:59:51 -040025#ifndef CONFIG_PHY_ADDR
26# define CONFIG_PHY_ADDR 1
27#endif
28#ifndef CONFIG_PHY_CLOCK_FREQ
29# define CONFIG_PHY_CLOCK_FREQ 2500000
30#endif
31
Aubrey Li10ebdd92007-03-19 01:24:52 +080032#ifdef CONFIG_POST
33#include <post.h>
34#endif
35
Aubrey Li10ebdd92007-03-19 01:24:52 +080036#define RXBUF_BASE_ADDR 0xFF900000
37#define TXBUF_BASE_ADDR 0xFF800000
38#define TX_BUF_CNT 1
39
Wolfgang Denka1be4762008-05-20 16:00:29 +020040#define TOUT_LOOP 1000000
Aubrey Li10ebdd92007-03-19 01:24:52 +080041
Mike Frysinger10be09f2009-01-08 11:57:57 -050042static ADI_ETHER_BUFFER *txbuf[TX_BUF_CNT];
43static ADI_ETHER_BUFFER *rxbuf[PKTBUFSRX];
Aubrey Li10ebdd92007-03-19 01:24:52 +080044static u16 txIdx; /* index of the current RX buffer */
45static u16 rxIdx; /* index of the current TX buffer */
46
Aubrey Li10ebdd92007-03-19 01:24:52 +080047/* DMAx_CONFIG values at DMA Restart */
Mike Frysinger10be09f2009-01-08 11:57:57 -050048static const union {
49 u16 data;
50 ADI_DMA_CONFIG_REG reg;
51} txdmacfg = {
52 .reg = {
53 .b_DMA_EN = 1, /* enabled */
54 .b_WNR = 0, /* read from memory */
55 .b_WDSIZE = 2, /* wordsize is 32 bits */
56 .b_DMA2D = 0,
57 .b_RESTART = 0,
58 .b_DI_SEL = 0,
59 .b_DI_EN = 0, /* no interrupt */
60 .b_NDSIZE = 5, /* 5 half words is desc size */
61 .b_FLOW = 7 /* large desc flow */
62 },
Mike Frysingerb11eb3f2008-02-24 23:58:13 -050063};
Aubrey Li10ebdd92007-03-19 01:24:52 +080064
Mike Frysinger709ad972008-10-14 04:52:00 -040065static int bfin_miiphy_wait(void)
66{
67 /* poll the STABUSY bit */
68 while (bfin_read_EMAC_STAADD() & STABUSY)
69 continue;
70 return 0;
71}
72
73static int bfin_miiphy_read(char *devname, uchar addr, uchar reg, ushort *val)
74{
75 if (bfin_miiphy_wait())
76 return 1;
77 bfin_write_EMAC_STAADD(SET_PHYAD(addr) | SET_REGAD(reg) | STABUSY);
78 if (bfin_miiphy_wait())
79 return 1;
80 *val = bfin_read_EMAC_STADAT();
81 return 0;
82}
83
84static int bfin_miiphy_write(char *devname, uchar addr, uchar reg, ushort val)
85{
86 if (bfin_miiphy_wait())
87 return 1;
88 bfin_write_EMAC_STADAT(val);
89 bfin_write_EMAC_STAADD(SET_PHYAD(addr) | SET_REGAD(reg) | STAOP | STABUSY);
90 return 0;
91}
92
Mike Frysingerb11eb3f2008-02-24 23:58:13 -050093int bfin_EMAC_initialize(bd_t *bis)
Aubrey Li10ebdd92007-03-19 01:24:52 +080094{
95 struct eth_device *dev;
Mike Frysinger709ad972008-10-14 04:52:00 -040096 dev = malloc(sizeof(*dev));
Aubrey Li10ebdd92007-03-19 01:24:52 +080097 if (dev == NULL)
98 hang();
99
100 memset(dev, 0, sizeof(*dev));
Mike Frysingerb11eb3f2008-02-24 23:58:13 -0500101 sprintf(dev->name, "Blackfin EMAC");
Aubrey Li10ebdd92007-03-19 01:24:52 +0800102
103 dev->iobase = 0;
104 dev->priv = 0;
105 dev->init = bfin_EMAC_init;
106 dev->halt = bfin_EMAC_halt;
107 dev->send = bfin_EMAC_send;
108 dev->recv = bfin_EMAC_recv;
109
110 eth_register(dev);
111
Mike Frysinger709ad972008-10-14 04:52:00 -0400112#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
113 miiphy_register(dev->name, bfin_miiphy_read, bfin_miiphy_write);
114#endif
115
Ben Warren82c2c6a2008-07-11 23:15:28 -0700116 return 0;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800117}
118
119static int bfin_EMAC_send(struct eth_device *dev, volatile void *packet,
120 int length)
121{
122 int i;
123 int result = 0;
124 unsigned int *buf;
125 buf = (unsigned int *)packet;
126
127 if (length <= 0) {
128 printf("Ethernet: bad packet size: %d\n", length);
129 goto out;
130 }
131
132 if ((*pDMA2_IRQ_STATUS & DMA_ERR) != 0) {
133 printf("Ethernet: tx DMA error\n");
134 goto out;
135 }
136
137 for (i = 0; (*pDMA2_IRQ_STATUS & DMA_RUN) != 0; i++) {
138 if (i > TOUT_LOOP) {
139 puts("Ethernet: tx time out\n");
140 goto out;
141 }
142 }
143 txbuf[txIdx]->FrmData->NoBytes = length;
144 memcpy(txbuf[txIdx]->FrmData->Dest, (void *)packet, length);
145 txbuf[txIdx]->Dma[0].START_ADDR = (u32) txbuf[txIdx]->FrmData;
Mike Frysinger10be09f2009-01-08 11:57:57 -0500146 *pDMA2_NEXT_DESC_PTR = txbuf[txIdx]->Dma;
147 *pDMA2_CONFIG = txdmacfg.data;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800148 *pEMAC_OPMODE |= TE;
149
150 for (i = 0; (txbuf[txIdx]->StatusWord & TX_COMP) == 0; i++) {
151 if (i > TOUT_LOOP) {
152 puts("Ethernet: tx error\n");
153 goto out;
154 }
155 }
156 result = txbuf[txIdx]->StatusWord;
157 txbuf[txIdx]->StatusWord = 0;
158 if ((txIdx + 1) >= TX_BUF_CNT)
159 txIdx = 0;
160 else
161 txIdx++;
Mike Frysingerb11eb3f2008-02-24 23:58:13 -0500162 out:
Mike Frysinger3ac20c32008-11-05 06:36:15 -0500163 debug("BFIN EMAC send: length = %d\n", length);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800164 return result;
165}
166
167static int bfin_EMAC_recv(struct eth_device *dev)
168{
169 int length = 0;
170
171 for (;;) {
172 if ((rxbuf[rxIdx]->StatusWord & RX_COMP) == 0) {
173 length = -1;
174 break;
175 }
176 if ((rxbuf[rxIdx]->StatusWord & RX_DMAO) != 0) {
177 printf("Ethernet: rx dma overrun\n");
178 break;
179 }
180 if ((rxbuf[rxIdx]->StatusWord & RX_OK) == 0) {
181 printf("Ethernet: rx error\n");
182 break;
183 }
184 length = rxbuf[rxIdx]->StatusWord & 0x000007FF;
185 if (length <= 4) {
186 printf("Ethernet: bad frame\n");
187 break;
188 }
Robin Getzb5edf812009-08-24 10:33:39 -0400189
190 debug("%s: len = %d\n", __func__, length - 4);
191
Aubrey Li10ebdd92007-03-19 01:24:52 +0800192 NetRxPackets[rxIdx] =
193 (volatile uchar *)(rxbuf[rxIdx]->FrmData->Dest);
194 NetReceive(NetRxPackets[rxIdx], length - 4);
195 *pDMA1_IRQ_STATUS |= DMA_DONE | DMA_ERR;
196 rxbuf[rxIdx]->StatusWord = 0x00000000;
197 if ((rxIdx + 1) >= PKTBUFSRX)
198 rxIdx = 0;
199 else
200 rxIdx++;
201 }
202
203 return length;
204}
205
206/**************************************************************
207 *
208 * Ethernet Initialization Routine
209 *
210 *************************************************************/
211
Mike Frysinger709ad972008-10-14 04:52:00 -0400212/* MDC = SCLK / MDC_freq / 2 - 1 */
213#define MDC_FREQ_TO_DIV(mdc_freq) (get_sclk() / (mdc_freq) / 2 - 1)
214
215static int bfin_miiphy_init(struct eth_device *dev, int *opmode)
216{
217 u16 phydat;
218 size_t count;
219
220 /* Enable PHY output */
221 *pVR_CTL |= CLKBUFOE;
222
223 /* Set all the pins to peripheral mode */
Mike Frysingerd11ab932008-12-09 17:46:21 -0500224#ifdef CONFIG_RMII
Mike Frysinger709ad972008-10-14 04:52:00 -0400225 /* grab RMII pins */
226# if defined(__ADSPBF51x__)
227 *pPORTF_MUX = (*pPORTF_MUX & \
228 ~(PORT_x_MUX_3_MASK | PORT_x_MUX_4_MASK | PORT_x_MUX_5_MASK)) | \
229 PORT_x_MUX_3_FUNC_1 | PORT_x_MUX_4_FUNC_1 | PORT_x_MUX_5_FUNC_1;
230 *pPORTF_FER |= PF8 | PF9 | PF10 | PF11 | PF12 | PF13 | PF14 | PF15;
231 *pPORTG_MUX = (*pPORTG_MUX & ~PORT_x_MUX_0_MASK) | PORT_x_MUX_0_FUNC_1;
232 *pPORTG_FER |= PG0 | PG1 | PG2;
233# elif defined(__ADSPBF52x__)
234 *pPORTG_MUX = (*pPORTG_MUX & ~PORT_x_MUX_6_MASK) | PORT_x_MUX_6_FUNC_2;
235 *pPORTG_FER |= PG14 | PG15;
236 *pPORTH_MUX = (*pPORTH_MUX & ~(PORT_x_MUX_0_MASK | PORT_x_MUX_1_MASK)) | \
237 PORT_x_MUX_0_FUNC_2 | PORT_x_MUX_1_FUNC_2;
238 *pPORTH_FER |= PH0 | PH1 | PH2 | PH3 | PH4 | PH5 | PH6 | PH7 | PH8;
239# else
240 *pPORTH_FER |= PH0 | PH1 | PH4 | PH5 | PH6 | PH8 | PH9 | PH14 | PH15;
241# endif
242#else
243 /* grab MII & RMII pins */
244# if defined(__ADSPBF51x__)
245 *pPORTF_MUX = (*pPORTF_MUX & \
246 ~(PORT_x_MUX_0_MASK | PORT_x_MUX_1_MASK | PORT_x_MUX_3_MASK | PORT_x_MUX_4_MASK | PORT_x_MUX_5_MASK)) | \
247 PORT_x_MUX_0_FUNC_1 | PORT_x_MUX_1_FUNC_1 | PORT_x_MUX_3_FUNC_1 | PORT_x_MUX_4_FUNC_1 | PORT_x_MUX_5_FUNC_1;
248 *pPORTF_FER |= PF0 | PF1 | PF2 | PF3 | PF4 | PF5 | PF6 | PF8 | PF9 | PF10 | PF11 | PF12 | PF13 | PF14 | PF15;
249 *pPORTG_MUX = (*pPORTG_MUX & ~PORT_x_MUX_0_MASK) | PORT_x_MUX_0_FUNC_1;
250 *pPORTG_FER |= PG0 | PG1 | PG2;
251# elif defined(__ADSPBF52x__)
252 *pPORTG_MUX = (*pPORTG_MUX & ~PORT_x_MUX_6_MASK) | PORT_x_MUX_6_FUNC_2;
253 *pPORTG_FER |= PG14 | PG15;
254 *pPORTH_MUX = PORT_x_MUX_0_FUNC_2 | PORT_x_MUX_1_FUNC_2 | PORT_x_MUX_2_FUNC_2;
255 *pPORTH_FER = -1; /* all pins */
256# else
257 *pPORTH_FER = -1; /* all pins */
258# endif
259#endif
260
261 /* Odd word alignment for Receive Frame DMA word */
262 /* Configure checksum support and rcve frame word alignment */
Mike Frysinger65b7d932008-10-20 13:59:51 -0400263 bfin_write_EMAC_SYSCTL(RXDWA | RXCKS | SET_MDCDIV(MDC_FREQ_TO_DIV(CONFIG_PHY_CLOCK_FREQ)));
Mike Frysinger709ad972008-10-14 04:52:00 -0400264
265 /* turn on auto-negotiation and wait for link to come up */
Mike Frysinger65b7d932008-10-20 13:59:51 -0400266 bfin_miiphy_write(dev->name, CONFIG_PHY_ADDR, MII_BMCR, BMCR_ANENABLE);
Mike Frysinger709ad972008-10-14 04:52:00 -0400267 count = 0;
268 while (1) {
269 ++count;
Mike Frysinger65b7d932008-10-20 13:59:51 -0400270 if (bfin_miiphy_read(dev->name, CONFIG_PHY_ADDR, MII_BMSR, &phydat))
Mike Frysinger709ad972008-10-14 04:52:00 -0400271 return -1;
272 if (phydat & BMSR_LSTATUS)
273 break;
274 if (count > 30000) {
275 printf("%s: link down, check cable\n", dev->name);
276 return -1;
277 }
278 udelay(100);
279 }
280
281 /* see what kind of link we have */
Mike Frysinger65b7d932008-10-20 13:59:51 -0400282 if (bfin_miiphy_read(dev->name, CONFIG_PHY_ADDR, MII_LPA, &phydat))
Mike Frysinger709ad972008-10-14 04:52:00 -0400283 return -1;
284 if (phydat & LPA_DUPLEX)
285 *opmode = FDMODE;
286 else
287 *opmode = 0;
288
289 bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
290
291 /* Initialize the TX DMA channel registers */
292 *pDMA2_X_COUNT = 0;
293 *pDMA2_X_MODIFY = 4;
294 *pDMA2_Y_COUNT = 0;
295 *pDMA2_Y_MODIFY = 0;
296
297 /* Initialize the RX DMA channel registers */
298 *pDMA1_X_COUNT = 0;
299 *pDMA1_X_MODIFY = 4;
300 *pDMA1_Y_COUNT = 0;
301 *pDMA1_Y_MODIFY = 0;
302
303 return 0;
304}
305
Mike Frysingerb11eb3f2008-02-24 23:58:13 -0500306static int bfin_EMAC_init(struct eth_device *dev, bd_t *bd)
Aubrey Li10ebdd92007-03-19 01:24:52 +0800307{
308 u32 opmode;
309 int dat;
310 int i;
Mike Frysinger3ac20c32008-11-05 06:36:15 -0500311 debug("Eth_init: ......\n");
Aubrey Li10ebdd92007-03-19 01:24:52 +0800312
313 txIdx = 0;
314 rxIdx = 0;
315
Mike Frysinger709ad972008-10-14 04:52:00 -0400316 /* Initialize System Register */
317 if (bfin_miiphy_init(dev, &dat) < 0)
Aubrey Li10ebdd92007-03-19 01:24:52 +0800318 return -1;
319
Mike Frysinger709ad972008-10-14 04:52:00 -0400320 /* Initialize EMAC address */
Mike Frysingerf72d3f12008-10-13 15:06:25 -0400321 bfin_EMAC_setup_addr(dev->enetaddr);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800322
Mike Frysinger709ad972008-10-14 04:52:00 -0400323 /* Initialize TX and RX buffer */
Aubrey Li10ebdd92007-03-19 01:24:52 +0800324 for (i = 0; i < PKTBUFSRX; i++) {
325 rxbuf[i] = SetupRxBuffer(i);
326 if (i > 0) {
Mike Frysinger10be09f2009-01-08 11:57:57 -0500327 rxbuf[i - 1]->Dma[1].NEXT_DESC_PTR = rxbuf[i]->Dma;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800328 if (i == (PKTBUFSRX - 1))
Mike Frysinger10be09f2009-01-08 11:57:57 -0500329 rxbuf[i]->Dma[1].NEXT_DESC_PTR = rxbuf[0]->Dma;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800330 }
331 }
332 for (i = 0; i < TX_BUF_CNT; i++) {
333 txbuf[i] = SetupTxBuffer(i);
334 if (i > 0) {
Mike Frysinger10be09f2009-01-08 11:57:57 -0500335 txbuf[i - 1]->Dma[1].NEXT_DESC_PTR = txbuf[i]->Dma;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800336 if (i == (TX_BUF_CNT - 1))
Mike Frysinger10be09f2009-01-08 11:57:57 -0500337 txbuf[i]->Dma[1].NEXT_DESC_PTR = txbuf[0]->Dma;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800338 }
339 }
340
341 /* Set RX DMA */
Mike Frysinger10be09f2009-01-08 11:57:57 -0500342 *pDMA1_NEXT_DESC_PTR = rxbuf[0]->Dma;
343 *pDMA1_CONFIG = rxbuf[0]->Dma[0].CONFIG_DATA;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800344
345 /* Wait MII done */
Mike Frysinger709ad972008-10-14 04:52:00 -0400346 bfin_miiphy_wait();
Aubrey Li10ebdd92007-03-19 01:24:52 +0800347
348 /* We enable only RX here */
349 /* ASTP : Enable Automatic Pad Stripping
350 PR : Promiscuous Mode for test
351 PSF : Receive frames with total length less than 64 bytes.
352 FDMODE : Full Duplex Mode
353 LB : Internal Loopback for test
354 RE : Receiver Enable */
355 if (dat == FDMODE)
356 opmode = ASTP | FDMODE | PSF;
357 else
358 opmode = ASTP | PSF;
359 opmode |= RE;
Mike Frysingerd11ab932008-12-09 17:46:21 -0500360#ifdef CONFIG_RMII
Aubrey Li10ebdd92007-03-19 01:24:52 +0800361 opmode |= TE | RMII;
362#endif
363 /* Turn on the EMAC */
364 *pEMAC_OPMODE = opmode;
365 return 0;
366}
367
368static void bfin_EMAC_halt(struct eth_device *dev)
369{
Mike Frysinger3ac20c32008-11-05 06:36:15 -0500370 debug("Eth_halt: ......\n");
Aubrey Li10ebdd92007-03-19 01:24:52 +0800371 /* Turn off the EMAC */
372 *pEMAC_OPMODE = 0x00000000;
373 /* Turn off the EMAC RX DMA */
374 *pDMA1_CONFIG = 0x0000;
375 *pDMA2_CONFIG = 0x0000;
376
377}
378
Mike Frysingerf72d3f12008-10-13 15:06:25 -0400379void bfin_EMAC_setup_addr(uchar *enetaddr)
Aubrey Li10ebdd92007-03-19 01:24:52 +0800380{
Mike Frysingerb11eb3f2008-02-24 23:58:13 -0500381 *pEMAC_ADDRLO =
Mike Frysingerf72d3f12008-10-13 15:06:25 -0400382 enetaddr[0] |
383 enetaddr[1] << 8 |
384 enetaddr[2] << 16 |
385 enetaddr[3] << 24;
Mike Frysingerb11eb3f2008-02-24 23:58:13 -0500386 *pEMAC_ADDRHI =
Mike Frysingerf72d3f12008-10-13 15:06:25 -0400387 enetaddr[4] |
388 enetaddr[5] << 8;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800389}
390
Aubrey Li10ebdd92007-03-19 01:24:52 +0800391ADI_ETHER_BUFFER *SetupRxBuffer(int no)
392{
393 ADI_ETHER_FRAME_BUFFER *frmbuf;
394 ADI_ETHER_BUFFER *buf;
395 int nobytes_buffer = sizeof(ADI_ETHER_BUFFER[2]) / 2; /* ensure a multi. of 4 */
396 int total_size = nobytes_buffer + RECV_BUFSIZE;
397
Mike Frysinger10be09f2009-01-08 11:57:57 -0500398 buf = (void *) (RXBUF_BASE_ADDR + no * total_size);
399 frmbuf = (void *) (RXBUF_BASE_ADDR + no * total_size + nobytes_buffer);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800400
401 memset(buf, 0x00, nobytes_buffer);
402 buf->FrmData = frmbuf;
403 memset(frmbuf, 0xfe, RECV_BUFSIZE);
404
405 /* set up first desc to point to receive frame buffer */
406 buf->Dma[0].NEXT_DESC_PTR = &(buf->Dma[1]);
407 buf->Dma[0].START_ADDR = (u32) buf->FrmData;
408 buf->Dma[0].CONFIG.b_DMA_EN = 1; /* enabled */
409 buf->Dma[0].CONFIG.b_WNR = 1; /* Write to memory */
410 buf->Dma[0].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
411 buf->Dma[0].CONFIG.b_NDSIZE = 5; /* 5 half words is desc size. */
412 buf->Dma[0].CONFIG.b_FLOW = 7; /* large desc flow */
413
414 /* set up second desc to point to status word */
Mike Frysinger10be09f2009-01-08 11:57:57 -0500415 buf->Dma[1].NEXT_DESC_PTR = buf->Dma;
Aubrey Li10ebdd92007-03-19 01:24:52 +0800416 buf->Dma[1].START_ADDR = (u32) & buf->IPHdrChksum;
417 buf->Dma[1].CONFIG.b_DMA_EN = 1; /* enabled */
418 buf->Dma[1].CONFIG.b_WNR = 1; /* Write to memory */
419 buf->Dma[1].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
420 buf->Dma[1].CONFIG.b_DI_EN = 1; /* enable interrupt */
421 buf->Dma[1].CONFIG.b_NDSIZE = 5; /* must be 0 when FLOW is 0 */
422 buf->Dma[1].CONFIG.b_FLOW = 7; /* stop */
423
424 return buf;
425}
426
427ADI_ETHER_BUFFER *SetupTxBuffer(int no)
428{
429 ADI_ETHER_FRAME_BUFFER *frmbuf;
430 ADI_ETHER_BUFFER *buf;
431 int nobytes_buffer = sizeof(ADI_ETHER_BUFFER[2]) / 2; /* ensure a multi. of 4 */
432 int total_size = nobytes_buffer + RECV_BUFSIZE;
433
Mike Frysinger10be09f2009-01-08 11:57:57 -0500434 buf = (void *) (TXBUF_BASE_ADDR + no * total_size);
435 frmbuf = (void *) (TXBUF_BASE_ADDR + no * total_size + nobytes_buffer);
Aubrey Li10ebdd92007-03-19 01:24:52 +0800436
437 memset(buf, 0x00, nobytes_buffer);
438 buf->FrmData = frmbuf;
439 memset(frmbuf, 0x00, RECV_BUFSIZE);
440
441 /* set up first desc to point to receive frame buffer */
442 buf->Dma[0].NEXT_DESC_PTR = &(buf->Dma[1]);
443 buf->Dma[0].START_ADDR = (u32) buf->FrmData;
444 buf->Dma[0].CONFIG.b_DMA_EN = 1; /* enabled */
445 buf->Dma[0].CONFIG.b_WNR = 0; /* Read to memory */
446 buf->Dma[0].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
447 buf->Dma[0].CONFIG.b_NDSIZE = 5; /* 5 half words is desc size. */
448 buf->Dma[0].CONFIG.b_FLOW = 7; /* large desc flow */
449
450 /* set up second desc to point to status word */
451 buf->Dma[1].NEXT_DESC_PTR = &(buf->Dma[0]);
452 buf->Dma[1].START_ADDR = (u32) & buf->StatusWord;
453 buf->Dma[1].CONFIG.b_DMA_EN = 1; /* enabled */
454 buf->Dma[1].CONFIG.b_WNR = 1; /* Write to memory */
455 buf->Dma[1].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
456 buf->Dma[1].CONFIG.b_DI_EN = 1; /* enable interrupt */
457 buf->Dma[1].CONFIG.b_NDSIZE = 0; /* must be 0 when FLOW is 0 */
458 buf->Dma[1].CONFIG.b_FLOW = 0; /* stop */
459
460 return buf;
461}
462
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200463#if defined(CONFIG_POST) && defined(CONFIG_SYS_POST_ETHER)
Aubrey Li10ebdd92007-03-19 01:24:52 +0800464int ether_post_test(int flags)
465{
466 uchar buf[64];
467 int i, value = 0;
468 int length;
469
470 printf("\n--------");
471 bfin_EMAC_init(NULL, NULL);
472 /* construct the package */
473 buf[0] = buf[6] = (unsigned char)(*pEMAC_ADDRLO & 0xFF);
474 buf[1] = buf[7] = (unsigned char)((*pEMAC_ADDRLO & 0xFF00) >> 8);
475 buf[2] = buf[8] = (unsigned char)((*pEMAC_ADDRLO & 0xFF0000) >> 16);
476 buf[3] = buf[9] = (unsigned char)((*pEMAC_ADDRLO & 0xFF000000) >> 24);
477 buf[4] = buf[10] = (unsigned char)(*pEMAC_ADDRHI & 0xFF);
478 buf[5] = buf[11] = (unsigned char)((*pEMAC_ADDRHI & 0xFF00) >> 8);
479 buf[12] = 0x08; /* Type: ARP */
480 buf[13] = 0x06;
481 buf[14] = 0x00; /* Hardware type: Ethernet */
482 buf[15] = 0x01;
483 buf[16] = 0x08; /* Protocal type: IP */
484 buf[17] = 0x00;
485 buf[18] = 0x06; /* Hardware size */
486 buf[19] = 0x04; /* Protocol size */
487 buf[20] = 0x00; /* Opcode: request */
488 buf[21] = 0x01;
489
490 for (i = 0; i < 42; i++)
491 buf[i + 22] = i;
492 printf("--------Send 64 bytes......\n");
493 bfin_EMAC_send(NULL, (volatile void *)buf, 64);
494 for (i = 0; i < 100; i++) {
495 udelay(10000);
496 if ((rxbuf[rxIdx]->StatusWord & RX_COMP) != 0) {
497 value = 1;
498 break;
499 }
500 }
501 if (value == 0) {
502 printf("--------EMAC can't receive any data\n");
503 eth_halt();
504 return -1;
505 }
506 length = rxbuf[rxIdx]->StatusWord & 0x000007FF - 4;
507 for (i = 0; i < length; i++) {
508 if (rxbuf[rxIdx]->FrmData->Dest[i] != buf[i]) {
509 printf("--------EMAC receive error data!\n");
510 eth_halt();
511 return -1;
512 }
513 }
514 printf("--------receive %d bytes, matched\n", length);
515 bfin_EMAC_halt(NULL);
516 return 0;
517}
518#endif