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/portmux.h>
20 #include <asm/mach-common/bits/dma.h>
21 #include <asm/mach-common/bits/emac.h>
22 #include <asm/mach-common/bits/pll.h>
26 #ifndef CONFIG_PHY_ADDR
27 # define CONFIG_PHY_ADDR 1
29 #ifndef CONFIG_PHY_CLOCK_FREQ
30 # define CONFIG_PHY_CLOCK_FREQ 2500000
37 #define RXBUF_BASE_ADDR 0xFF900000
38 #define TXBUF_BASE_ADDR 0xFF800000
41 #define TOUT_LOOP 1000000
43 static ADI_ETHER_BUFFER *txbuf[TX_BUF_CNT];
44 static ADI_ETHER_BUFFER *rxbuf[PKTBUFSRX];
45 static u16 txIdx; /* index of the current RX buffer */
46 static u16 rxIdx; /* index of the current TX buffer */
48 /* DMAx_CONFIG values at DMA Restart */
51 ADI_DMA_CONFIG_REG reg;
54 .b_DMA_EN = 1, /* enabled */
55 .b_WNR = 0, /* read from memory */
56 .b_WDSIZE = 2, /* wordsize is 32 bits */
60 .b_DI_EN = 0, /* no interrupt */
61 .b_NDSIZE = 5, /* 5 half words is desc size */
62 .b_FLOW = 7 /* large desc flow */
66 static int bfin_miiphy_wait(void)
68 /* poll the STABUSY bit */
69 while (bfin_read_EMAC_STAADD() & STABUSY)
74 static int bfin_miiphy_read(const char *devname, uchar addr, uchar reg, ushort *val)
76 if (bfin_miiphy_wait())
78 bfin_write_EMAC_STAADD(SET_PHYAD(addr) | SET_REGAD(reg) | STABUSY);
79 if (bfin_miiphy_wait())
81 *val = bfin_read_EMAC_STADAT();
85 static int bfin_miiphy_write(const char *devname, uchar addr, uchar reg, ushort val)
87 if (bfin_miiphy_wait())
89 bfin_write_EMAC_STADAT(val);
90 bfin_write_EMAC_STAADD(SET_PHYAD(addr) | SET_REGAD(reg) | STAOP | STABUSY);
94 int bfin_EMAC_initialize(bd_t *bis)
96 struct eth_device *dev;
97 dev = malloc(sizeof(*dev));
101 memset(dev, 0, sizeof(*dev));
102 strcpy(dev->name, "bfin_mac");
106 dev->init = bfin_EMAC_init;
107 dev->halt = bfin_EMAC_halt;
108 dev->send = bfin_EMAC_send;
109 dev->recv = bfin_EMAC_recv;
110 dev->write_hwaddr = bfin_EMAC_setup_addr;
114 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
115 miiphy_register(dev->name, bfin_miiphy_read, bfin_miiphy_write);
121 static int bfin_EMAC_send(struct eth_device *dev, void *packet, int length)
127 printf("Ethernet: bad packet size: %d\n", length);
131 if (bfin_read_DMA2_IRQ_STATUS() & DMA_ERR) {
132 printf("Ethernet: tx DMA error\n");
136 for (i = 0; (bfin_read_DMA2_IRQ_STATUS() & DMA_RUN); ++i) {
138 puts("Ethernet: tx time out\n");
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;
145 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);
149 for (i = 0; (txbuf[txIdx]->StatusWord & TX_COMP) == 0; i++) {
151 puts("Ethernet: tx error\n");
155 result = txbuf[txIdx]->StatusWord;
156 txbuf[txIdx]->StatusWord = 0;
157 if ((txIdx + 1) >= TX_BUF_CNT)
162 debug("BFIN EMAC send: length = %d\n", length);
166 static int bfin_EMAC_recv(struct eth_device *dev)
171 if ((rxbuf[rxIdx]->StatusWord & RX_COMP) == 0) {
175 if ((rxbuf[rxIdx]->StatusWord & RX_DMAO) != 0) {
176 printf("Ethernet: rx dma overrun\n");
179 if ((rxbuf[rxIdx]->StatusWord & RX_OK) == 0) {
180 printf("Ethernet: rx error\n");
183 length = rxbuf[rxIdx]->StatusWord & 0x000007FF;
185 printf("Ethernet: bad frame\n");
189 debug("%s: len = %d\n", __func__, length - 4);
191 NetRxPackets[rxIdx] = rxbuf[rxIdx]->FrmData->Dest;
192 NetReceive(NetRxPackets[rxIdx], length - 4);
193 bfin_write_DMA1_IRQ_STATUS(DMA_DONE | DMA_ERR);
194 rxbuf[rxIdx]->StatusWord = 0x00000000;
195 if ((rxIdx + 1) >= PKTBUFSRX)
204 /**************************************************************
206 * Ethernet Initialization Routine
208 *************************************************************/
210 /* MDC = SCLK / MDC_freq / 2 - 1 */
211 #define MDC_FREQ_TO_DIV(mdc_freq) (get_sclk() / (mdc_freq) / 2 - 1)
213 #ifndef CONFIG_BFIN_MAC_PINS
215 # define CONFIG_BFIN_MAC_PINS P_RMII0
217 # define CONFIG_BFIN_MAC_PINS P_MII0
221 static int bfin_miiphy_init(struct eth_device *dev, int *opmode)
223 const unsigned short pins[] = CONFIG_BFIN_MAC_PINS;
227 /* Enable PHY output */
228 bfin_write_VR_CTL(bfin_read_VR_CTL() | CLKBUFOE);
230 /* Set all the pins to peripheral mode */
231 peripheral_request_list(pins, "bfin_mac");
233 /* Odd word alignment for Receive Frame DMA word */
234 /* Configure checksum support and rcve frame word alignment */
235 bfin_write_EMAC_SYSCTL(RXDWA | RXCKS | SET_MDCDIV(MDC_FREQ_TO_DIV(CONFIG_PHY_CLOCK_FREQ)));
237 /* turn on auto-negotiation and wait for link to come up */
238 bfin_miiphy_write(dev->name, CONFIG_PHY_ADDR, MII_BMCR, BMCR_ANENABLE);
242 if (bfin_miiphy_read(dev->name, CONFIG_PHY_ADDR, MII_BMSR, &phydat))
244 if (phydat & BMSR_LSTATUS)
247 printf("%s: link down, check cable\n", dev->name);
253 /* see what kind of link we have */
254 if (bfin_miiphy_read(dev->name, CONFIG_PHY_ADDR, MII_LPA, &phydat))
256 if (phydat & LPA_DUPLEX)
261 bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
263 /* Initialize the TX DMA channel registers */
264 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);
269 /* Initialize the RX DMA channel registers */
270 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);
278 static int bfin_EMAC_setup_addr(struct eth_device *dev)
280 bfin_write_EMAC_ADDRLO(
282 dev->enetaddr[1] << 8 |
283 dev->enetaddr[2] << 16 |
284 dev->enetaddr[3] << 24
286 bfin_write_EMAC_ADDRHI(
288 dev->enetaddr[5] << 8
293 static int bfin_EMAC_init(struct eth_device *dev, bd_t *bd)
298 debug("Eth_init: ......\n");
303 /* Initialize System Register */
304 if (bfin_miiphy_init(dev, &dat) < 0)
307 /* Initialize EMAC address */
308 bfin_EMAC_setup_addr(dev);
310 /* Initialize TX and RX buffer */
311 for (i = 0; i < PKTBUFSRX; i++) {
312 rxbuf[i] = SetupRxBuffer(i);
314 rxbuf[i - 1]->Dma[1].NEXT_DESC_PTR = rxbuf[i]->Dma;
315 if (i == (PKTBUFSRX - 1))
316 rxbuf[i]->Dma[1].NEXT_DESC_PTR = rxbuf[0]->Dma;
319 for (i = 0; i < TX_BUF_CNT; i++) {
320 txbuf[i] = SetupTxBuffer(i);
322 txbuf[i - 1]->Dma[1].NEXT_DESC_PTR = txbuf[i]->Dma;
323 if (i == (TX_BUF_CNT - 1))
324 txbuf[i]->Dma[1].NEXT_DESC_PTR = txbuf[0]->Dma;
329 bfin_write_DMA1_NEXT_DESC_PTR(rxbuf[0]->Dma);
330 bfin_write_DMA1_CONFIG(rxbuf[0]->Dma[0].CONFIG_DATA);
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 */
343 opmode = ASTP | FDMODE | PSF;
350 /* Turn on the EMAC */
351 bfin_write_EMAC_OPMODE(opmode);
355 static void bfin_EMAC_halt(struct eth_device *dev)
357 debug("Eth_halt: ......\n");
358 /* Turn off the EMAC */
359 bfin_write_EMAC_OPMODE(0);
360 /* Turn off the EMAC RX DMA */
361 bfin_write_DMA1_CONFIG(0);
362 bfin_write_DMA2_CONFIG(0);
365 ADI_ETHER_BUFFER *SetupRxBuffer(int no)
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;
372 buf = (void *) (RXBUF_BASE_ADDR + no * total_size);
373 frmbuf = (void *) (RXBUF_BASE_ADDR + no * total_size + nobytes_buffer);
375 memset(buf, 0x00, nobytes_buffer);
376 buf->FrmData = frmbuf;
377 memset(frmbuf, 0xfe, RECV_BUFSIZE);
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 */
388 /* set up second desc to point to status word */
389 buf->Dma[1].NEXT_DESC_PTR = buf->Dma;
390 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 */
401 ADI_ETHER_BUFFER *SetupTxBuffer(int no)
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;
408 buf = (void *) (TXBUF_BASE_ADDR + no * total_size);
409 frmbuf = (void *) (TXBUF_BASE_ADDR + no * total_size + nobytes_buffer);
411 memset(buf, 0x00, nobytes_buffer);
412 buf->FrmData = frmbuf;
413 memset(frmbuf, 0x00, RECV_BUFSIZE);
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 */
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 */
437 #if defined(CONFIG_POST) && defined(CONFIG_SYS_POST_ETHER)
438 int ether_post_test(int flags)
445 printf("\n--------");
446 bfin_EMAC_init(NULL, NULL);
447 /* construct the package */
448 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;
456 buf[12] = 0x08; /* Type: ARP */
458 buf[14] = 0x00; /* Hardware type: Ethernet */
460 buf[16] = 0x08; /* Protocal type: IP */
462 buf[18] = 0x06; /* Hardware size */
463 buf[19] = 0x04; /* Protocol size */
464 buf[20] = 0x00; /* Opcode: request */
467 for (i = 0; i < 42; i++)
469 printf("--------Send 64 bytes......\n");
470 bfin_EMAC_send(NULL, buf, 64);
471 for (i = 0; i < 100; i++) {
473 if ((rxbuf[rxIdx]->StatusWord & RX_COMP) != 0) {
479 printf("--------EMAC can't receive any data\n");
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");
491 printf("--------receive %d bytes, matched\n", length);
492 bfin_EMAC_halt(NULL);