2 * ADI Blackfin 537 MAC Ethernet
4 * Copyright (c) 2005 Analog Device, Inc.
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 #include <asm/blackfin.h>
31 #include "ether_bf537.h"
40 #define DEBUGF(fmt,args...) printf(fmt,##args)
42 #define DEBUGF(fmt,args...)
45 #if defined(CONFIG_CMD_NET)
47 #define RXBUF_BASE_ADDR 0xFF900000
48 #define TXBUF_BASE_ADDR 0xFF800000
51 #define TOUT_LOOP 1000000
53 ADI_ETHER_BUFFER *txbuf[TX_BUF_CNT];
54 ADI_ETHER_BUFFER *rxbuf[PKTBUFSRX];
55 static u16 txIdx; /* index of the current RX buffer */
56 static u16 rxIdx; /* index of the current TX buffer */
59 u16 PHYregs[NO_PHY_REGS]; /* u16 PHYADDR; */
61 /* DMAx_CONFIG values at DMA Restart */
62 const ADI_DMA_CONFIG_REG rxdmacfg = { 1, 1, 2, 0, 0, 0, 0, 5, 7 };
65 rxdmacfg.b_DMA_EN = 1; /* enabled */
66 rxdmacfg.b_WNR = 1; /* write to memory */
67 rxdmacfg.b_WDSIZE = 2; /* wordsize is 32 bits */
68 rxdmacfg.b_DMA2D = 0; /* N/A */
69 rxdmacfg.b_RESTART= 0; /* N/A */
70 rxdmacfg.b_DI_SEL = 0; /* N/A */
71 rxdmacfg.b_DI_EN = 0; /* no interrupt */
72 rxdmacfg.b_NDSIZE = 5; /* 5 half words is desc size. */
73 rxdmacfg.b_FLOW = 7; /* large desc flow */
76 const ADI_DMA_CONFIG_REG txdmacfg = { 1, 0, 2, 0, 0, 0, 0, 5, 7 };
79 txdmacfg.b_DMA_EN = 1; /* enabled */
80 txdmacfg.b_WNR = 0; /* read from memory */
81 txdmacfg.b_WDSIZE = 2; /* wordsize is 32 bits */
82 txdmacfg.b_DMA2D = 0; /* N/A */
83 txdmacfg.b_RESTART= 0; /* N/A */
84 txdmacfg.b_DI_SEL = 0; /* N/A */
85 txdmacfg.b_DI_EN = 0; /* no interrupt */
86 txdmacfg.b_NDSIZE = 5; /* 5 half words is desc size. */
87 txdmacfg.b_FLOW = 7; /* large desc flow */
90 ADI_ETHER_BUFFER *SetupRxBuffer(int no);
91 ADI_ETHER_BUFFER *SetupTxBuffer(int no);
93 static int bfin_EMAC_init(struct eth_device *dev, bd_t * bd);
94 static void bfin_EMAC_halt(struct eth_device *dev);
95 static int bfin_EMAC_send(struct eth_device *dev, volatile void *packet,
97 static int bfin_EMAC_recv(struct eth_device *dev);
99 int bfin_EMAC_initialize(bd_t * bis)
101 struct eth_device *dev;
102 dev = (struct eth_device *)malloc(sizeof(*dev));
106 memset(dev, 0, sizeof(*dev));
107 sprintf(dev->name, "BF537 ETHERNET");
111 dev->init = bfin_EMAC_init;
112 dev->halt = bfin_EMAC_halt;
113 dev->send = bfin_EMAC_send;
114 dev->recv = bfin_EMAC_recv;
121 static int bfin_EMAC_send(struct eth_device *dev, volatile void *packet,
127 buf = (unsigned int *)packet;
130 printf("Ethernet: bad packet size: %d\n", length);
134 if ((*pDMA2_IRQ_STATUS & DMA_ERR) != 0) {
135 printf("Ethernet: tx DMA error\n");
139 for (i = 0; (*pDMA2_IRQ_STATUS & DMA_RUN) != 0; i++) {
141 puts("Ethernet: tx time out\n");
145 txbuf[txIdx]->FrmData->NoBytes = length;
146 memcpy(txbuf[txIdx]->FrmData->Dest, (void *)packet, length);
147 txbuf[txIdx]->Dma[0].START_ADDR = (u32) txbuf[txIdx]->FrmData;
148 *pDMA2_NEXT_DESC_PTR = &txbuf[txIdx]->Dma[0];
149 *pDMA2_CONFIG = *(u16 *) (void *)(&txdmacfg);
152 for (i = 0; (txbuf[txIdx]->StatusWord & TX_COMP) == 0; i++) {
154 puts("Ethernet: tx error\n");
158 result = txbuf[txIdx]->StatusWord;
159 txbuf[txIdx]->StatusWord = 0;
160 if ((txIdx + 1) >= TX_BUF_CNT)
165 DEBUGF("BFIN EMAC send: length = %d\n", length);
169 static int bfin_EMAC_recv(struct eth_device *dev)
174 if ((rxbuf[rxIdx]->StatusWord & RX_COMP) == 0) {
178 if ((rxbuf[rxIdx]->StatusWord & RX_DMAO) != 0) {
179 printf("Ethernet: rx dma overrun\n");
182 if ((rxbuf[rxIdx]->StatusWord & RX_OK) == 0) {
183 printf("Ethernet: rx error\n");
186 length = rxbuf[rxIdx]->StatusWord & 0x000007FF;
188 printf("Ethernet: bad frame\n");
191 NetRxPackets[rxIdx] =
192 (volatile uchar *)(rxbuf[rxIdx]->FrmData->Dest);
193 NetReceive(NetRxPackets[rxIdx], length - 4);
194 *pDMA1_IRQ_STATUS |= DMA_DONE | DMA_ERR;
195 rxbuf[rxIdx]->StatusWord = 0x00000000;
196 if ((rxIdx + 1) >= PKTBUFSRX)
205 /**************************************************************
207 * Ethernet Initialization Routine
209 *************************************************************/
211 static int bfin_EMAC_init(struct eth_device *dev, bd_t * bd)
216 DEBUGF("Eth_init: ......\n");
221 /* Initialize System Register */
222 if (SetupSystemRegs(&dat) < 0)
225 /* Initialize EMAC address */
226 SetupMacAddr(SrcAddr);
228 /* Initialize TX and RX buffer */
229 for (i = 0; i < PKTBUFSRX; i++) {
230 rxbuf[i] = SetupRxBuffer(i);
232 rxbuf[i - 1]->Dma[1].NEXT_DESC_PTR =
234 if (i == (PKTBUFSRX - 1))
235 rxbuf[i]->Dma[1].NEXT_DESC_PTR =
239 for (i = 0; i < TX_BUF_CNT; i++) {
240 txbuf[i] = SetupTxBuffer(i);
242 txbuf[i - 1]->Dma[1].NEXT_DESC_PTR =
244 if (i == (TX_BUF_CNT - 1))
245 txbuf[i]->Dma[1].NEXT_DESC_PTR =
251 *pDMA1_NEXT_DESC_PTR = &rxbuf[0]->Dma[0];
252 *pDMA1_CONFIG = *((u16 *) (void *)&rxbuf[0]->Dma[0].CONFIG);
257 /* We enable only RX here */
258 /* ASTP : Enable Automatic Pad Stripping
259 PR : Promiscuous Mode for test
260 PSF : Receive frames with total length less than 64 bytes.
261 FDMODE : Full Duplex Mode
262 LB : Internal Loopback for test
263 RE : Receiver Enable */
265 opmode = ASTP | FDMODE | PSF;
269 #ifdef CONFIG_BFIN_MAC_RMII
272 /* Turn on the EMAC */
273 *pEMAC_OPMODE = opmode;
277 static void bfin_EMAC_halt(struct eth_device *dev)
279 DEBUGF("Eth_halt: ......\n");
280 /* Turn off the EMAC */
281 *pEMAC_OPMODE = 0x00000000;
282 /* Turn off the EMAC RX DMA */
283 *pDMA1_CONFIG = 0x0000;
284 *pDMA2_CONFIG = 0x0000;
288 void SetupMacAddr(u8 * MACaddr)
292 /* this depends on a little-endian machine */
293 tmp = getenv("ethaddr");
295 for (i = 0; i < 6; i++) {
296 MACaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
298 tmp = (*end) ? end + 1 : end;
301 #ifndef CONFIG_NETCONSOLE
302 printf("Using MAC Address %02X:%02X:%02X:%02X:%02X:%02X\n",
303 MACaddr[0], MACaddr[1],
304 MACaddr[2], MACaddr[3], MACaddr[4], MACaddr[5]);
306 *pEMAC_ADDRLO = MACaddr[0] | MACaddr[1] << 8 |
307 MACaddr[2] << 16 | MACaddr[3] << 24;
308 *pEMAC_ADDRHI = MACaddr[4] | MACaddr[5] << 8;
312 void PollMdcDone(void)
314 /* poll the STABUSY bit */
315 while (*pEMAC_STAADD & STABUSY) ;
318 void WrPHYReg(u16 PHYAddr, u16 RegAddr, u16 Data)
322 *pEMAC_STADAT = Data;
324 *pEMAC_STAADD = SET_PHYAD(PHYAddr) | SET_REGAD(RegAddr) |
325 STAOP | STAIE | STABUSY;
328 /*********************************************************************************
329 * Read an off-chip register in a PHY through the MDC/MDIO port *
330 *********************************************************************************/
331 u16 RdPHYReg(u16 PHYAddr, u16 RegAddr)
337 *pEMAC_STAADD = SET_PHYAD(PHYAddr) | SET_REGAD(RegAddr) |
342 Data = (u16) * pEMAC_STADAT;
344 PHYregs[RegAddr] = Data; /* save shadow copy */
349 void SoftResetPHY(void)
352 /* set the reset bit */
353 WrPHYReg(PHYADDR, PHY_MODECTL, PHY_RESET);
354 /* and clear it again */
355 WrPHYReg(PHYADDR, PHY_MODECTL, 0x0000);
357 /* poll until reset is complete */
358 phydat = RdPHYReg(PHYADDR, PHY_MODECTL);
359 } while ((phydat & PHY_RESET) != 0);
362 int SetupSystemRegs(int *opmode)
366 /* Enable PHY output */
367 *pVR_CTL |= PHYCLKOE;
369 sysctl = SET_MDCDIV(24);
370 /* Odd word alignment for Receive Frame DMA word */
371 /* Configure checksum support and rcve frame word alignment */
372 sysctl |= RXDWA | RXCKS;
373 *pEMAC_SYSCTL = sysctl;
374 /* auto negotiation on */
377 phydat = PHY_ANEG_EN | PHY_DUPLEX | PHY_SPD_SET;
378 WrPHYReg(PHYADDR, PHY_MODECTL, phydat);
381 phydat = RdPHYReg(PHYADDR, PHY_MODESTAT);
384 ("Link is down, please check your network connection\n");
388 } while (!(phydat & 0x0004));
390 phydat = RdPHYReg(PHYADDR, PHY_ANLPAR);
392 if ((phydat & 0x0100) || (phydat & 0x0040))
397 *pEMAC_MMC_CTL = RSTC | CROLL;
399 /* Initialize the TX DMA channel registers */
405 /* Initialize the RX DMA channel registers */
413 ADI_ETHER_BUFFER *SetupRxBuffer(int no)
415 ADI_ETHER_FRAME_BUFFER *frmbuf;
416 ADI_ETHER_BUFFER *buf;
417 int nobytes_buffer = sizeof(ADI_ETHER_BUFFER[2]) / 2; /* ensure a multi. of 4 */
418 int total_size = nobytes_buffer + RECV_BUFSIZE;
420 buf = (ADI_ETHER_BUFFER *) (RXBUF_BASE_ADDR + no * total_size);
422 (ADI_ETHER_FRAME_BUFFER *) (RXBUF_BASE_ADDR + no * total_size +
425 memset(buf, 0x00, nobytes_buffer);
426 buf->FrmData = frmbuf;
427 memset(frmbuf, 0xfe, RECV_BUFSIZE);
429 /* set up first desc to point to receive frame buffer */
430 buf->Dma[0].NEXT_DESC_PTR = &(buf->Dma[1]);
431 buf->Dma[0].START_ADDR = (u32) buf->FrmData;
432 buf->Dma[0].CONFIG.b_DMA_EN = 1; /* enabled */
433 buf->Dma[0].CONFIG.b_WNR = 1; /* Write to memory */
434 buf->Dma[0].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
435 buf->Dma[0].CONFIG.b_NDSIZE = 5; /* 5 half words is desc size. */
436 buf->Dma[0].CONFIG.b_FLOW = 7; /* large desc flow */
438 /* set up second desc to point to status word */
439 buf->Dma[1].NEXT_DESC_PTR = &(buf->Dma[0]);
440 buf->Dma[1].START_ADDR = (u32) & buf->IPHdrChksum;
441 buf->Dma[1].CONFIG.b_DMA_EN = 1; /* enabled */
442 buf->Dma[1].CONFIG.b_WNR = 1; /* Write to memory */
443 buf->Dma[1].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
444 buf->Dma[1].CONFIG.b_DI_EN = 1; /* enable interrupt */
445 buf->Dma[1].CONFIG.b_NDSIZE = 5; /* must be 0 when FLOW is 0 */
446 buf->Dma[1].CONFIG.b_FLOW = 7; /* stop */
451 ADI_ETHER_BUFFER *SetupTxBuffer(int no)
453 ADI_ETHER_FRAME_BUFFER *frmbuf;
454 ADI_ETHER_BUFFER *buf;
455 int nobytes_buffer = sizeof(ADI_ETHER_BUFFER[2]) / 2; /* ensure a multi. of 4 */
456 int total_size = nobytes_buffer + RECV_BUFSIZE;
458 buf = (ADI_ETHER_BUFFER *) (TXBUF_BASE_ADDR + no * total_size);
460 (ADI_ETHER_FRAME_BUFFER *) (TXBUF_BASE_ADDR + no * total_size +
463 memset(buf, 0x00, nobytes_buffer);
464 buf->FrmData = frmbuf;
465 memset(frmbuf, 0x00, RECV_BUFSIZE);
467 /* set up first desc to point to receive frame buffer */
468 buf->Dma[0].NEXT_DESC_PTR = &(buf->Dma[1]);
469 buf->Dma[0].START_ADDR = (u32) buf->FrmData;
470 buf->Dma[0].CONFIG.b_DMA_EN = 1; /* enabled */
471 buf->Dma[0].CONFIG.b_WNR = 0; /* Read to memory */
472 buf->Dma[0].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
473 buf->Dma[0].CONFIG.b_NDSIZE = 5; /* 5 half words is desc size. */
474 buf->Dma[0].CONFIG.b_FLOW = 7; /* large desc flow */
476 /* set up second desc to point to status word */
477 buf->Dma[1].NEXT_DESC_PTR = &(buf->Dma[0]);
478 buf->Dma[1].START_ADDR = (u32) & buf->StatusWord;
479 buf->Dma[1].CONFIG.b_DMA_EN = 1; /* enabled */
480 buf->Dma[1].CONFIG.b_WNR = 1; /* Write to memory */
481 buf->Dma[1].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
482 buf->Dma[1].CONFIG.b_DI_EN = 1; /* enable interrupt */
483 buf->Dma[1].CONFIG.b_NDSIZE = 0; /* must be 0 when FLOW is 0 */
484 buf->Dma[1].CONFIG.b_FLOW = 0; /* stop */
489 #if defined(CONFIG_POST) && defined(CFG_POST_ETHER)
490 int ether_post_test(int flags)
496 printf("\n--------");
497 bfin_EMAC_init(NULL, NULL);
498 /* construct the package */
499 buf[0] = buf[6] = (unsigned char)(*pEMAC_ADDRLO & 0xFF);
500 buf[1] = buf[7] = (unsigned char)((*pEMAC_ADDRLO & 0xFF00) >> 8);
501 buf[2] = buf[8] = (unsigned char)((*pEMAC_ADDRLO & 0xFF0000) >> 16);
502 buf[3] = buf[9] = (unsigned char)((*pEMAC_ADDRLO & 0xFF000000) >> 24);
503 buf[4] = buf[10] = (unsigned char)(*pEMAC_ADDRHI & 0xFF);
504 buf[5] = buf[11] = (unsigned char)((*pEMAC_ADDRHI & 0xFF00) >> 8);
505 buf[12] = 0x08; /* Type: ARP */
507 buf[14] = 0x00; /* Hardware type: Ethernet */
509 buf[16] = 0x08; /* Protocal type: IP */
511 buf[18] = 0x06; /* Hardware size */
512 buf[19] = 0x04; /* Protocol size */
513 buf[20] = 0x00; /* Opcode: request */
516 for (i = 0; i < 42; i++)
518 printf("--------Send 64 bytes......\n");
519 bfin_EMAC_send(NULL, (volatile void *)buf, 64);
520 for (i = 0; i < 100; i++) {
522 if ((rxbuf[rxIdx]->StatusWord & RX_COMP) != 0) {
528 printf("--------EMAC can't receive any data\n");
532 length = rxbuf[rxIdx]->StatusWord & 0x000007FF - 4;
533 for (i = 0; i < length; i++) {
534 if (rxbuf[rxIdx]->FrmData->Dest[i] != buf[i]) {
535 printf("--------EMAC receive error data!\n");
540 printf("--------receive %d bytes, matched\n", length);
541 bfin_EMAC_halt(NULL);