2 * Driver for Blackfin On-Chip MAC device
4 * Copyright (c) 2005-2008 Analog Device, Inc.
6 * Licensed under the GPL-2 or later.
16 #include <linux/mii.h>
18 #include <asm/blackfin.h>
19 #include <asm/mach-common/bits/dma.h>
20 #include <asm/mach-common/bits/emac.h>
21 #include <asm/mach-common/bits/pll.h>
25 #ifndef CONFIG_PHY_ADDR
26 # define CONFIG_PHY_ADDR 1
28 #ifndef CONFIG_PHY_CLOCK_FREQ
29 # define CONFIG_PHY_CLOCK_FREQ 2500000
36 #define RXBUF_BASE_ADDR 0xFF900000
37 #define TXBUF_BASE_ADDR 0xFF800000
40 #define TOUT_LOOP 1000000
42 static ADI_ETHER_BUFFER *txbuf[TX_BUF_CNT];
43 static ADI_ETHER_BUFFER *rxbuf[PKTBUFSRX];
44 static u16 txIdx; /* index of the current RX buffer */
45 static u16 rxIdx; /* index of the current TX buffer */
47 /* DMAx_CONFIG values at DMA Restart */
50 ADI_DMA_CONFIG_REG reg;
53 .b_DMA_EN = 1, /* enabled */
54 .b_WNR = 0, /* read from memory */
55 .b_WDSIZE = 2, /* wordsize is 32 bits */
59 .b_DI_EN = 0, /* no interrupt */
60 .b_NDSIZE = 5, /* 5 half words is desc size */
61 .b_FLOW = 7 /* large desc flow */
65 static int bfin_miiphy_wait(void)
67 /* poll the STABUSY bit */
68 while (bfin_read_EMAC_STAADD() & STABUSY)
73 static int bfin_miiphy_read(char *devname, uchar addr, uchar reg, ushort *val)
75 if (bfin_miiphy_wait())
77 bfin_write_EMAC_STAADD(SET_PHYAD(addr) | SET_REGAD(reg) | STABUSY);
78 if (bfin_miiphy_wait())
80 *val = bfin_read_EMAC_STADAT();
84 static int bfin_miiphy_write(char *devname, uchar addr, uchar reg, ushort val)
86 if (bfin_miiphy_wait())
88 bfin_write_EMAC_STADAT(val);
89 bfin_write_EMAC_STAADD(SET_PHYAD(addr) | SET_REGAD(reg) | STAOP | STABUSY);
93 int bfin_EMAC_initialize(bd_t *bis)
95 struct eth_device *dev;
96 dev = malloc(sizeof(*dev));
100 memset(dev, 0, sizeof(*dev));
101 sprintf(dev->name, "Blackfin EMAC");
105 dev->init = bfin_EMAC_init;
106 dev->halt = bfin_EMAC_halt;
107 dev->send = bfin_EMAC_send;
108 dev->recv = bfin_EMAC_recv;
112 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
113 miiphy_register(dev->name, bfin_miiphy_read, bfin_miiphy_write);
119 static int bfin_EMAC_send(struct eth_device *dev, volatile void *packet,
125 buf = (unsigned int *)packet;
128 printf("Ethernet: bad packet size: %d\n", length);
132 if ((*pDMA2_IRQ_STATUS & DMA_ERR) != 0) {
133 printf("Ethernet: tx DMA error\n");
137 for (i = 0; (*pDMA2_IRQ_STATUS & DMA_RUN) != 0; i++) {
139 puts("Ethernet: tx time out\n");
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;
146 *pDMA2_NEXT_DESC_PTR = txbuf[txIdx]->Dma;
147 *pDMA2_CONFIG = txdmacfg.data;
150 for (i = 0; (txbuf[txIdx]->StatusWord & TX_COMP) == 0; i++) {
152 puts("Ethernet: tx error\n");
156 result = txbuf[txIdx]->StatusWord;
157 txbuf[txIdx]->StatusWord = 0;
158 if ((txIdx + 1) >= TX_BUF_CNT)
163 debug("BFIN EMAC send: length = %d\n", length);
167 static int bfin_EMAC_recv(struct eth_device *dev)
172 if ((rxbuf[rxIdx]->StatusWord & RX_COMP) == 0) {
176 if ((rxbuf[rxIdx]->StatusWord & RX_DMAO) != 0) {
177 printf("Ethernet: rx dma overrun\n");
180 if ((rxbuf[rxIdx]->StatusWord & RX_OK) == 0) {
181 printf("Ethernet: rx error\n");
184 length = rxbuf[rxIdx]->StatusWord & 0x000007FF;
186 printf("Ethernet: bad frame\n");
189 NetRxPackets[rxIdx] =
190 (volatile uchar *)(rxbuf[rxIdx]->FrmData->Dest);
191 NetReceive(NetRxPackets[rxIdx], length - 4);
192 *pDMA1_IRQ_STATUS |= DMA_DONE | DMA_ERR;
193 rxbuf[rxIdx]->StatusWord = 0x00000000;
194 if ((rxIdx + 1) >= PKTBUFSRX)
203 /**************************************************************
205 * Ethernet Initialization Routine
207 *************************************************************/
209 /* MDC = SCLK / MDC_freq / 2 - 1 */
210 #define MDC_FREQ_TO_DIV(mdc_freq) (get_sclk() / (mdc_freq) / 2 - 1)
212 static int bfin_miiphy_init(struct eth_device *dev, int *opmode)
217 /* Enable PHY output */
218 *pVR_CTL |= CLKBUFOE;
220 /* Set all the pins to peripheral mode */
223 # if defined(__ADSPBF51x__)
224 *pPORTF_MUX = (*pPORTF_MUX & \
225 ~(PORT_x_MUX_3_MASK | PORT_x_MUX_4_MASK | PORT_x_MUX_5_MASK)) | \
226 PORT_x_MUX_3_FUNC_1 | PORT_x_MUX_4_FUNC_1 | PORT_x_MUX_5_FUNC_1;
227 *pPORTF_FER |= PF8 | PF9 | PF10 | PF11 | PF12 | PF13 | PF14 | PF15;
228 *pPORTG_MUX = (*pPORTG_MUX & ~PORT_x_MUX_0_MASK) | PORT_x_MUX_0_FUNC_1;
229 *pPORTG_FER |= PG0 | PG1 | PG2;
230 # elif defined(__ADSPBF52x__)
231 *pPORTG_MUX = (*pPORTG_MUX & ~PORT_x_MUX_6_MASK) | PORT_x_MUX_6_FUNC_2;
232 *pPORTG_FER |= PG14 | PG15;
233 *pPORTH_MUX = (*pPORTH_MUX & ~(PORT_x_MUX_0_MASK | PORT_x_MUX_1_MASK)) | \
234 PORT_x_MUX_0_FUNC_2 | PORT_x_MUX_1_FUNC_2;
235 *pPORTH_FER |= PH0 | PH1 | PH2 | PH3 | PH4 | PH5 | PH6 | PH7 | PH8;
237 *pPORTH_FER |= PH0 | PH1 | PH4 | PH5 | PH6 | PH8 | PH9 | PH14 | PH15;
240 /* grab MII & RMII pins */
241 # if defined(__ADSPBF51x__)
242 *pPORTF_MUX = (*pPORTF_MUX & \
243 ~(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)) | \
244 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;
245 *pPORTF_FER |= PF0 | PF1 | PF2 | PF3 | PF4 | PF5 | PF6 | PF8 | PF9 | PF10 | PF11 | PF12 | PF13 | PF14 | PF15;
246 *pPORTG_MUX = (*pPORTG_MUX & ~PORT_x_MUX_0_MASK) | PORT_x_MUX_0_FUNC_1;
247 *pPORTG_FER |= PG0 | PG1 | PG2;
248 # elif defined(__ADSPBF52x__)
249 *pPORTG_MUX = (*pPORTG_MUX & ~PORT_x_MUX_6_MASK) | PORT_x_MUX_6_FUNC_2;
250 *pPORTG_FER |= PG14 | PG15;
251 *pPORTH_MUX = PORT_x_MUX_0_FUNC_2 | PORT_x_MUX_1_FUNC_2 | PORT_x_MUX_2_FUNC_2;
252 *pPORTH_FER = -1; /* all pins */
254 *pPORTH_FER = -1; /* all pins */
258 /* Odd word alignment for Receive Frame DMA word */
259 /* Configure checksum support and rcve frame word alignment */
260 bfin_write_EMAC_SYSCTL(RXDWA | RXCKS | SET_MDCDIV(MDC_FREQ_TO_DIV(CONFIG_PHY_CLOCK_FREQ)));
262 /* turn on auto-negotiation and wait for link to come up */
263 bfin_miiphy_write(dev->name, CONFIG_PHY_ADDR, MII_BMCR, BMCR_ANENABLE);
267 if (bfin_miiphy_read(dev->name, CONFIG_PHY_ADDR, MII_BMSR, &phydat))
269 if (phydat & BMSR_LSTATUS)
272 printf("%s: link down, check cable\n", dev->name);
278 /* see what kind of link we have */
279 if (bfin_miiphy_read(dev->name, CONFIG_PHY_ADDR, MII_LPA, &phydat))
281 if (phydat & LPA_DUPLEX)
286 bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
288 /* Initialize the TX DMA channel registers */
294 /* Initialize the RX DMA channel registers */
303 static int bfin_EMAC_init(struct eth_device *dev, bd_t *bd)
308 debug("Eth_init: ......\n");
313 /* Initialize System Register */
314 if (bfin_miiphy_init(dev, &dat) < 0)
317 /* Initialize EMAC address */
318 bfin_EMAC_setup_addr(bd);
320 /* Initialize TX and RX buffer */
321 for (i = 0; i < PKTBUFSRX; i++) {
322 rxbuf[i] = SetupRxBuffer(i);
324 rxbuf[i - 1]->Dma[1].NEXT_DESC_PTR = rxbuf[i]->Dma;
325 if (i == (PKTBUFSRX - 1))
326 rxbuf[i]->Dma[1].NEXT_DESC_PTR = rxbuf[0]->Dma;
329 for (i = 0; i < TX_BUF_CNT; i++) {
330 txbuf[i] = SetupTxBuffer(i);
332 txbuf[i - 1]->Dma[1].NEXT_DESC_PTR = txbuf[i]->Dma;
333 if (i == (TX_BUF_CNT - 1))
334 txbuf[i]->Dma[1].NEXT_DESC_PTR = txbuf[0]->Dma;
339 *pDMA1_NEXT_DESC_PTR = rxbuf[0]->Dma;
340 *pDMA1_CONFIG = rxbuf[0]->Dma[0].CONFIG_DATA;
345 /* We enable only RX here */
346 /* ASTP : Enable Automatic Pad Stripping
347 PR : Promiscuous Mode for test
348 PSF : Receive frames with total length less than 64 bytes.
349 FDMODE : Full Duplex Mode
350 LB : Internal Loopback for test
351 RE : Receiver Enable */
353 opmode = ASTP | FDMODE | PSF;
360 /* Turn on the EMAC */
361 *pEMAC_OPMODE = opmode;
365 static void bfin_EMAC_halt(struct eth_device *dev)
367 debug("Eth_halt: ......\n");
368 /* Turn off the EMAC */
369 *pEMAC_OPMODE = 0x00000000;
370 /* Turn off the EMAC RX DMA */
371 *pDMA1_CONFIG = 0x0000;
372 *pDMA2_CONFIG = 0x0000;
376 void bfin_EMAC_setup_addr(bd_t *bd)
380 bd->bi_enetaddr[1] << 8 |
381 bd->bi_enetaddr[2] << 16 |
382 bd->bi_enetaddr[3] << 24;
385 bd->bi_enetaddr[5] << 8;
388 ADI_ETHER_BUFFER *SetupRxBuffer(int no)
390 ADI_ETHER_FRAME_BUFFER *frmbuf;
391 ADI_ETHER_BUFFER *buf;
392 int nobytes_buffer = sizeof(ADI_ETHER_BUFFER[2]) / 2; /* ensure a multi. of 4 */
393 int total_size = nobytes_buffer + RECV_BUFSIZE;
395 buf = (void *) (RXBUF_BASE_ADDR + no * total_size);
396 frmbuf = (void *) (RXBUF_BASE_ADDR + no * total_size + nobytes_buffer);
398 memset(buf, 0x00, nobytes_buffer);
399 buf->FrmData = frmbuf;
400 memset(frmbuf, 0xfe, RECV_BUFSIZE);
402 /* set up first desc to point to receive frame buffer */
403 buf->Dma[0].NEXT_DESC_PTR = &(buf->Dma[1]);
404 buf->Dma[0].START_ADDR = (u32) buf->FrmData;
405 buf->Dma[0].CONFIG.b_DMA_EN = 1; /* enabled */
406 buf->Dma[0].CONFIG.b_WNR = 1; /* Write to memory */
407 buf->Dma[0].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
408 buf->Dma[0].CONFIG.b_NDSIZE = 5; /* 5 half words is desc size. */
409 buf->Dma[0].CONFIG.b_FLOW = 7; /* large desc flow */
411 /* set up second desc to point to status word */
412 buf->Dma[1].NEXT_DESC_PTR = buf->Dma;
413 buf->Dma[1].START_ADDR = (u32) & buf->IPHdrChksum;
414 buf->Dma[1].CONFIG.b_DMA_EN = 1; /* enabled */
415 buf->Dma[1].CONFIG.b_WNR = 1; /* Write to memory */
416 buf->Dma[1].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
417 buf->Dma[1].CONFIG.b_DI_EN = 1; /* enable interrupt */
418 buf->Dma[1].CONFIG.b_NDSIZE = 5; /* must be 0 when FLOW is 0 */
419 buf->Dma[1].CONFIG.b_FLOW = 7; /* stop */
424 ADI_ETHER_BUFFER *SetupTxBuffer(int no)
426 ADI_ETHER_FRAME_BUFFER *frmbuf;
427 ADI_ETHER_BUFFER *buf;
428 int nobytes_buffer = sizeof(ADI_ETHER_BUFFER[2]) / 2; /* ensure a multi. of 4 */
429 int total_size = nobytes_buffer + RECV_BUFSIZE;
431 buf = (void *) (TXBUF_BASE_ADDR + no * total_size);
432 frmbuf = (void *) (TXBUF_BASE_ADDR + no * total_size + nobytes_buffer);
434 memset(buf, 0x00, nobytes_buffer);
435 buf->FrmData = frmbuf;
436 memset(frmbuf, 0x00, RECV_BUFSIZE);
438 /* set up first desc to point to receive frame buffer */
439 buf->Dma[0].NEXT_DESC_PTR = &(buf->Dma[1]);
440 buf->Dma[0].START_ADDR = (u32) buf->FrmData;
441 buf->Dma[0].CONFIG.b_DMA_EN = 1; /* enabled */
442 buf->Dma[0].CONFIG.b_WNR = 0; /* Read to memory */
443 buf->Dma[0].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
444 buf->Dma[0].CONFIG.b_NDSIZE = 5; /* 5 half words is desc size. */
445 buf->Dma[0].CONFIG.b_FLOW = 7; /* large desc flow */
447 /* set up second desc to point to status word */
448 buf->Dma[1].NEXT_DESC_PTR = &(buf->Dma[0]);
449 buf->Dma[1].START_ADDR = (u32) & buf->StatusWord;
450 buf->Dma[1].CONFIG.b_DMA_EN = 1; /* enabled */
451 buf->Dma[1].CONFIG.b_WNR = 1; /* Write to memory */
452 buf->Dma[1].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
453 buf->Dma[1].CONFIG.b_DI_EN = 1; /* enable interrupt */
454 buf->Dma[1].CONFIG.b_NDSIZE = 0; /* must be 0 when FLOW is 0 */
455 buf->Dma[1].CONFIG.b_FLOW = 0; /* stop */
460 #if defined(CONFIG_POST) && defined(CONFIG_SYS_POST_ETHER)
461 int ether_post_test(int flags)
467 printf("\n--------");
468 bfin_EMAC_init(NULL, NULL);
469 /* construct the package */
470 buf[0] = buf[6] = (unsigned char)(*pEMAC_ADDRLO & 0xFF);
471 buf[1] = buf[7] = (unsigned char)((*pEMAC_ADDRLO & 0xFF00) >> 8);
472 buf[2] = buf[8] = (unsigned char)((*pEMAC_ADDRLO & 0xFF0000) >> 16);
473 buf[3] = buf[9] = (unsigned char)((*pEMAC_ADDRLO & 0xFF000000) >> 24);
474 buf[4] = buf[10] = (unsigned char)(*pEMAC_ADDRHI & 0xFF);
475 buf[5] = buf[11] = (unsigned char)((*pEMAC_ADDRHI & 0xFF00) >> 8);
476 buf[12] = 0x08; /* Type: ARP */
478 buf[14] = 0x00; /* Hardware type: Ethernet */
480 buf[16] = 0x08; /* Protocal type: IP */
482 buf[18] = 0x06; /* Hardware size */
483 buf[19] = 0x04; /* Protocol size */
484 buf[20] = 0x00; /* Opcode: request */
487 for (i = 0; i < 42; i++)
489 printf("--------Send 64 bytes......\n");
490 bfin_EMAC_send(NULL, (volatile void *)buf, 64);
491 for (i = 0; i < 100; i++) {
493 if ((rxbuf[rxIdx]->StatusWord & RX_COMP) != 0) {
499 printf("--------EMAC can't receive any data\n");
503 length = rxbuf[rxIdx]->StatusWord & 0x000007FF - 4;
504 for (i = 0; i < length; i++) {
505 if (rxbuf[rxIdx]->FrmData->Dest[i] != buf[i]) {
506 printf("--------EMAC receive error data!\n");
511 printf("--------receive %d bytes, matched\n", length);
512 bfin_EMAC_halt(NULL);