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/mdio.h>
17 #include <linux/mii.h>
19 #include <asm/blackfin.h>
20 #include <asm/clock.h>
21 #include <asm/portmux.h>
22 #include <asm/mach-common/bits/dma.h>
23 #include <asm/mach-common/bits/emac.h>
24 #include <asm/mach-common/bits/pll.h>
28 #ifndef CONFIG_PHY_ADDR
29 # define CONFIG_PHY_ADDR 1
31 #ifndef CONFIG_PHY_CLOCK_FREQ
32 # define CONFIG_PHY_CLOCK_FREQ 2500000
39 #define RXBUF_BASE_ADDR 0xFF900000
40 #define TXBUF_BASE_ADDR 0xFF800000
43 #define TOUT_LOOP 1000000
45 static ADI_ETHER_BUFFER *txbuf[TX_BUF_CNT];
46 static ADI_ETHER_BUFFER *rxbuf[PKTBUFSRX];
47 static u16 txIdx; /* index of the current RX buffer */
48 static u16 rxIdx; /* index of the current TX buffer */
50 /* DMAx_CONFIG values at DMA Restart */
53 ADI_DMA_CONFIG_REG reg;
56 .b_DMA_EN = 1, /* enabled */
57 .b_WNR = 0, /* read from memory */
58 .b_WDSIZE = 2, /* wordsize is 32 bits */
62 .b_DI_EN = 0, /* no interrupt */
63 .b_NDSIZE = 5, /* 5 half words is desc size */
64 .b_FLOW = 7 /* large desc flow */
68 static int bfin_miiphy_wait(void)
70 /* poll the STABUSY bit */
71 while (bfin_read_EMAC_STAADD() & STABUSY)
76 static int bfin_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg)
79 if (bfin_miiphy_wait())
81 bfin_write_EMAC_STAADD(SET_PHYAD(addr) | SET_REGAD(reg) | STABUSY);
82 if (bfin_miiphy_wait())
84 val = bfin_read_EMAC_STADAT();
88 static int bfin_miiphy_write(struct mii_dev *bus, int addr, int devad,
91 if (bfin_miiphy_wait())
93 bfin_write_EMAC_STADAT(val);
94 bfin_write_EMAC_STAADD(SET_PHYAD(addr) | SET_REGAD(reg) | STAOP | STABUSY);
98 int bfin_EMAC_initialize(bd_t *bis)
100 struct eth_device *dev;
101 dev = malloc(sizeof(*dev));
105 memset(dev, 0, sizeof(*dev));
106 strcpy(dev->name, "bfin_mac");
110 dev->init = bfin_EMAC_init;
111 dev->halt = bfin_EMAC_halt;
112 dev->send = bfin_EMAC_send;
113 dev->recv = bfin_EMAC_recv;
114 dev->write_hwaddr = bfin_EMAC_setup_addr;
118 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
120 struct mii_dev *mdiodev = mdio_alloc();
123 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
124 mdiodev->read = bfin_miiphy_read;
125 mdiodev->write = bfin_miiphy_write;
127 retval = mdio_register(mdiodev);
137 static int bfin_EMAC_send(struct eth_device *dev, void *packet, int length)
143 printf("Ethernet: bad packet size: %d\n", length);
147 if (bfin_read_DMA2_IRQ_STATUS() & DMA_ERR) {
148 printf("Ethernet: tx DMA error\n");
152 for (i = 0; (bfin_read_DMA2_IRQ_STATUS() & DMA_RUN); ++i) {
154 puts("Ethernet: tx time out\n");
158 txbuf[txIdx]->FrmData->NoBytes = length;
159 memcpy(txbuf[txIdx]->FrmData->Dest, (void *)packet, length);
160 txbuf[txIdx]->Dma[0].START_ADDR = (u32) txbuf[txIdx]->FrmData;
161 bfin_write_DMA2_NEXT_DESC_PTR(txbuf[txIdx]->Dma);
162 bfin_write_DMA2_CONFIG(txdmacfg.data);
163 bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE);
165 for (i = 0; (txbuf[txIdx]->StatusWord & TX_COMP) == 0; i++) {
167 puts("Ethernet: tx error\n");
171 result = txbuf[txIdx]->StatusWord;
172 txbuf[txIdx]->StatusWord = 0;
173 if ((txIdx + 1) >= TX_BUF_CNT)
178 debug("BFIN EMAC send: length = %d\n", length);
182 static int bfin_EMAC_recv(struct eth_device *dev)
187 if ((rxbuf[rxIdx]->StatusWord & RX_COMP) == 0) {
191 if ((rxbuf[rxIdx]->StatusWord & RX_DMAO) != 0) {
192 printf("Ethernet: rx dma overrun\n");
195 if ((rxbuf[rxIdx]->StatusWord & RX_OK) == 0) {
196 printf("Ethernet: rx error\n");
199 length = rxbuf[rxIdx]->StatusWord & 0x000007FF;
201 printf("Ethernet: bad frame\n");
205 debug("%s: len = %d\n", __func__, length - 4);
207 net_rx_packets[rxIdx] = rxbuf[rxIdx]->FrmData->Dest;
208 net_process_received_packet(net_rx_packets[rxIdx], length - 4);
209 bfin_write_DMA1_IRQ_STATUS(DMA_DONE | DMA_ERR);
210 rxbuf[rxIdx]->StatusWord = 0x00000000;
211 if ((rxIdx + 1) >= PKTBUFSRX)
220 /**************************************************************
222 * Ethernet Initialization Routine
224 *************************************************************/
226 /* MDC = SCLK / MDC_freq / 2 - 1 */
227 #define MDC_FREQ_TO_DIV(mdc_freq) (get_sclk() / (mdc_freq) / 2 - 1)
229 #ifndef CONFIG_BFIN_MAC_PINS
231 # define CONFIG_BFIN_MAC_PINS P_RMII0
233 # define CONFIG_BFIN_MAC_PINS P_MII0
237 static int bfin_miiphy_init(struct eth_device *dev, int *opmode)
239 const unsigned short pins[] = CONFIG_BFIN_MAC_PINS;
242 struct mii_dev *mdiodev = dev->priv;
244 /* Enable PHY output */
245 bfin_write_VR_CTL(bfin_read_VR_CTL() | CLKBUFOE);
247 /* Set all the pins to peripheral mode */
248 peripheral_request_list(pins, "bfin_mac");
250 /* Odd word alignment for Receive Frame DMA word */
251 /* Configure checksum support and rcve frame word alignment */
252 bfin_write_EMAC_SYSCTL(RXDWA | RXCKS | SET_MDCDIV(MDC_FREQ_TO_DIV(CONFIG_PHY_CLOCK_FREQ)));
254 /* turn on auto-negotiation and wait for link to come up */
255 bfin_miiphy_write(mdiodev, CONFIG_PHY_ADDR, MDIO_DEVAD_NONE, MII_BMCR,
260 phydat = bfin_miiphy_read(mdiodev, CONFIG_PHY_ADDR,
261 MDIO_DEVAD_NONE, MII_BMSR);
264 if (phydat & BMSR_LSTATUS)
267 printf("%s: link down, check cable\n", dev->name);
273 /* see what kind of link we have */
274 phydat = bfin_miiphy_read(mdiodev, CONFIG_PHY_ADDR, MDIO_DEVAD_NONE,
278 if (phydat & LPA_DUPLEX)
283 bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
284 bfin_write_EMAC_VLAN1(EMAC_VLANX_DEF_VAL);
285 bfin_write_EMAC_VLAN2(EMAC_VLANX_DEF_VAL);
287 /* Initialize the TX DMA channel registers */
288 bfin_write_DMA2_X_COUNT(0);
289 bfin_write_DMA2_X_MODIFY(4);
290 bfin_write_DMA2_Y_COUNT(0);
291 bfin_write_DMA2_Y_MODIFY(0);
293 /* Initialize the RX DMA channel registers */
294 bfin_write_DMA1_X_COUNT(0);
295 bfin_write_DMA1_X_MODIFY(4);
296 bfin_write_DMA1_Y_COUNT(0);
297 bfin_write_DMA1_Y_MODIFY(0);
302 static int bfin_EMAC_setup_addr(struct eth_device *dev)
304 bfin_write_EMAC_ADDRLO(
306 dev->enetaddr[1] << 8 |
307 dev->enetaddr[2] << 16 |
308 dev->enetaddr[3] << 24
310 bfin_write_EMAC_ADDRHI(
312 dev->enetaddr[5] << 8
317 static int bfin_EMAC_init(struct eth_device *dev, bd_t *bd)
322 debug("Eth_init: ......\n");
327 /* Initialize System Register */
328 if (bfin_miiphy_init(dev, &dat) < 0)
331 /* Initialize EMAC address */
332 bfin_EMAC_setup_addr(dev);
334 /* Initialize TX and RX buffer */
335 for (i = 0; i < PKTBUFSRX; i++) {
336 rxbuf[i] = SetupRxBuffer(i);
338 rxbuf[i - 1]->Dma[1].NEXT_DESC_PTR = rxbuf[i]->Dma;
339 if (i == (PKTBUFSRX - 1))
340 rxbuf[i]->Dma[1].NEXT_DESC_PTR = rxbuf[0]->Dma;
343 for (i = 0; i < TX_BUF_CNT; i++) {
344 txbuf[i] = SetupTxBuffer(i);
346 txbuf[i - 1]->Dma[1].NEXT_DESC_PTR = txbuf[i]->Dma;
347 if (i == (TX_BUF_CNT - 1))
348 txbuf[i]->Dma[1].NEXT_DESC_PTR = txbuf[0]->Dma;
353 bfin_write_DMA1_NEXT_DESC_PTR(rxbuf[0]->Dma);
354 bfin_write_DMA1_CONFIG(rxbuf[0]->Dma[0].CONFIG_DATA);
359 /* We enable only RX here */
360 /* ASTP : Enable Automatic Pad Stripping
361 PR : Promiscuous Mode for test
362 PSF : Receive frames with total length less than 64 bytes.
363 FDMODE : Full Duplex Mode
364 LB : Internal Loopback for test
365 RE : Receiver Enable */
367 opmode = ASTP | FDMODE | PSF;
374 /* Turn on the EMAC */
375 bfin_write_EMAC_OPMODE(opmode);
379 static void bfin_EMAC_halt(struct eth_device *dev)
381 debug("Eth_halt: ......\n");
382 /* Turn off the EMAC */
383 bfin_write_EMAC_OPMODE(0);
384 /* Turn off the EMAC RX DMA */
385 bfin_write_DMA1_CONFIG(0);
386 bfin_write_DMA2_CONFIG(0);
389 ADI_ETHER_BUFFER *SetupRxBuffer(int no)
391 ADI_ETHER_FRAME_BUFFER *frmbuf;
392 ADI_ETHER_BUFFER *buf;
393 int nobytes_buffer = sizeof(ADI_ETHER_BUFFER[2]) / 2; /* ensure a multi. of 4 */
394 int total_size = nobytes_buffer + RECV_BUFSIZE;
396 buf = (void *) (RXBUF_BASE_ADDR + no * total_size);
397 frmbuf = (void *) (RXBUF_BASE_ADDR + no * total_size + nobytes_buffer);
399 memset(buf, 0x00, nobytes_buffer);
400 buf->FrmData = frmbuf;
401 memset(frmbuf, 0xfe, RECV_BUFSIZE);
403 /* set up first desc to point to receive frame buffer */
404 buf->Dma[0].NEXT_DESC_PTR = &(buf->Dma[1]);
405 buf->Dma[0].START_ADDR = (u32) buf->FrmData;
406 buf->Dma[0].CONFIG.b_DMA_EN = 1; /* enabled */
407 buf->Dma[0].CONFIG.b_WNR = 1; /* Write to memory */
408 buf->Dma[0].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
409 buf->Dma[0].CONFIG.b_NDSIZE = 5; /* 5 half words is desc size. */
410 buf->Dma[0].CONFIG.b_FLOW = 7; /* large desc flow */
412 /* set up second desc to point to status word */
413 buf->Dma[1].NEXT_DESC_PTR = buf->Dma;
414 buf->Dma[1].START_ADDR = (u32) & buf->IPHdrChksum;
415 buf->Dma[1].CONFIG.b_DMA_EN = 1; /* enabled */
416 buf->Dma[1].CONFIG.b_WNR = 1; /* Write to memory */
417 buf->Dma[1].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
418 buf->Dma[1].CONFIG.b_DI_EN = 1; /* enable interrupt */
419 buf->Dma[1].CONFIG.b_NDSIZE = 5; /* must be 0 when FLOW is 0 */
420 buf->Dma[1].CONFIG.b_FLOW = 7; /* stop */
425 ADI_ETHER_BUFFER *SetupTxBuffer(int no)
427 ADI_ETHER_FRAME_BUFFER *frmbuf;
428 ADI_ETHER_BUFFER *buf;
429 int nobytes_buffer = sizeof(ADI_ETHER_BUFFER[2]) / 2; /* ensure a multi. of 4 */
430 int total_size = nobytes_buffer + RECV_BUFSIZE;
432 buf = (void *) (TXBUF_BASE_ADDR + no * total_size);
433 frmbuf = (void *) (TXBUF_BASE_ADDR + no * total_size + nobytes_buffer);
435 memset(buf, 0x00, nobytes_buffer);
436 buf->FrmData = frmbuf;
437 memset(frmbuf, 0x00, RECV_BUFSIZE);
439 /* set up first desc to point to receive frame buffer */
440 buf->Dma[0].NEXT_DESC_PTR = &(buf->Dma[1]);
441 buf->Dma[0].START_ADDR = (u32) buf->FrmData;
442 buf->Dma[0].CONFIG.b_DMA_EN = 1; /* enabled */
443 buf->Dma[0].CONFIG.b_WNR = 0; /* Read to memory */
444 buf->Dma[0].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
445 buf->Dma[0].CONFIG.b_NDSIZE = 5; /* 5 half words is desc size. */
446 buf->Dma[0].CONFIG.b_FLOW = 7; /* large desc flow */
448 /* set up second desc to point to status word */
449 buf->Dma[1].NEXT_DESC_PTR = &(buf->Dma[0]);
450 buf->Dma[1].START_ADDR = (u32) & buf->StatusWord;
451 buf->Dma[1].CONFIG.b_DMA_EN = 1; /* enabled */
452 buf->Dma[1].CONFIG.b_WNR = 1; /* Write to memory */
453 buf->Dma[1].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
454 buf->Dma[1].CONFIG.b_DI_EN = 1; /* enable interrupt */
455 buf->Dma[1].CONFIG.b_NDSIZE = 0; /* must be 0 when FLOW is 0 */
456 buf->Dma[1].CONFIG.b_FLOW = 0; /* stop */
461 #if defined(CONFIG_POST) && defined(CONFIG_SYS_POST_ETHER)
462 int ether_post_test(int flags)
469 printf("\n--------");
470 bfin_EMAC_init(NULL, NULL);
471 /* construct the package */
472 addr = bfin_read_EMAC_ADDRLO();
473 buf[0] = buf[6] = addr;
474 buf[1] = buf[7] = addr >> 8;
475 buf[2] = buf[8] = addr >> 16;
476 buf[3] = buf[9] = addr >> 24;
477 addr = bfin_read_EMAC_ADDRHI();
478 buf[4] = buf[10] = addr;
479 buf[5] = buf[11] = addr >> 8;
480 buf[12] = 0x08; /* Type: ARP */
482 buf[14] = 0x00; /* Hardware type: Ethernet */
484 buf[16] = 0x08; /* Protocal type: IP */
486 buf[18] = 0x06; /* Hardware size */
487 buf[19] = 0x04; /* Protocol size */
488 buf[20] = 0x00; /* Opcode: request */
491 for (i = 0; i < 42; i++)
493 printf("--------Send 64 bytes......\n");
494 bfin_EMAC_send(NULL, buf, 64);
495 for (i = 0; i < 100; i++) {
497 if ((rxbuf[rxIdx]->StatusWord & RX_COMP) != 0) {
503 printf("--------EMAC can't receive any data\n");
507 length = rxbuf[rxIdx]->StatusWord & 0x000007FF - 4;
508 for (i = 0; i < length; i++) {
509 if (rxbuf[rxIdx]->FrmData->Dest[i] != buf[i]) {
510 printf("--------EMAC receive error data!\n");
515 printf("--------receive %d bytes, matched\n", length);
516 bfin_EMAC_halt(NULL);