1 //==========================================================================
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12 // Copyright (C) 2006 Andrew Lunn <andrew.lunn@ascom.ch>
14 // eCos is free software; you can redistribute it and/or modify it under
15 // the terms of the GNU General Public License as published by the Free
16 // Software Foundation; either version 2 or (at your option) any later version.
18 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 // You should have received a copy of the GNU General Public License along
24 // with eCos; if not, write to the Free Software Foundation, Inc.,
25 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
27 // As a special exception, if other files instantiate templates or use macros
28 // or inline functions from this file, or you compile this file and link it
29 // with other works to produce a work based on this file, this file does not
30 // by itself cause the resulting work to be covered by the GNU General Public
31 // License. However the source code for this file must still be made available
32 // in accordance with section (3) of the GNU General Public License.
34 // This exception does not invalidate any other reasons why a work based on
35 // this file might be covered by the GNU General Public License.
36 // -------------------------------------------
37 //####ECOSGPLCOPYRIGHTEND####
38 //==========================================================================
39 //#####DESCRIPTIONBEGIN####
41 // Author(s): Andrew Lunn, John Eigelaar
47 //####DESCRIPTIONEND####
49 //========================================================================*/
51 #include <pkgconf/system.h>
52 #include <pkgconf/hal.h>
53 #include <pkgconf/devs_eth_arm_at91.h>
54 #include <pkgconf/io_eth_drivers.h>
55 #if defined(CYGPKG_REDBOOT)
56 #include <pkgconf/redboot.h>
59 #include <cyg/hal/hal_io.h>
60 #include <cyg/hal/hal_intr.h>
61 #include <cyg/hal/hal_arch.h>
62 #include <cyg/hal/drv_api.h>
63 #include <cyg/hal/hal_diag.h>
64 #include <cyg/infra/cyg_type.h>
65 #include <cyg/infra/cyg_ass.h>
66 #include <cyg/infra/diag.h>
67 #include <cyg/io/eth/netdev.h>
68 #include <cyg/io/eth/eth_drv.h>
69 #include <cyg/io/eth/eth_drv_stats.h>
70 #include <cyg/io/eth_phy.h>
74 // Set up the level of debug output
75 #if CYGPKG_DEVS_ETH_ARM_AT91_DEBUG_LEVEL > 0
76 #define debug1_printf(args...) diag_printf(args)
78 #define debug1_printf(args...)
80 #if CYGPKG_DEVS_ETH_ARM_AT91_DEBUG_LEVEL > 1
81 #define debug2_printf(args...) diag_printf(args)
83 #define debug2_printf(args...)
86 //Driver interface callbacks
87 #define _eth_drv_init(sc,mac) \
88 (sc->funs->eth_drv->init)(sc,(unsigned char *)mac)
89 #define _eth_drv_tx_done(sc,key,status) \
90 (sc->funs->eth_drv->tx_done)(sc,key,status)
91 #define _eth_drv_recv(sc,len) \
92 (sc->funs->eth_drv->recv)(sc,len)
94 #ifdef CYGINT_IO_ETH_INT_SUPPORT_REQUIRED
96 at91_eth_isr (cyg_vector_t vector, cyg_addrword_t data);
99 // --------------------------------------------------------------
100 // RedBoot configuration options for managing ESAs for us
102 // Decide whether to have redboot config vars for it...
103 #if defined(CYGSEM_REDBOOT_FLASH_CONFIG) && defined(CYGPKG_REDBOOT_NETWORKING)
105 #include <flash_config.h>
107 #ifdef CYGSEM_DEVS_ETH_ARM_AT91_REDBOOT_HOLDS_ESA_ETH0
108 RedBoot_config_option("Network hardware address [MAC] for eth0",
110 ALWAYS_ENABLED, true,
114 #endif // CYGPKG_REDBOOT_NETWORKING && CYGSEM_REDBOOT_FLASH_CONFIG
116 // and initialization code to read them
117 // - independent of whether we are building RedBoot right now:
118 #ifdef CYGPKG_DEVS_ETH_ARM_AT91_REDBOOT_HOLDS_ESA
120 #include <cyg/hal/hal_if.h>
123 #define CONFIG_ESA (6)
126 #define CYGHWR_DEVS_ETH_ARM_AT91_GET_ESA( mac_address, ok ) \
128 ok = CYGACC_CALL_IF_FLASH_CFG_OP( CYGNUM_CALL_IF_FLASH_CFG_GET, \
134 #endif // CYGPKG_DEVS_ETH_AT91_ETH_REDBOOT_HOLDS_ESA
136 //============================================================================
138 // Private Data structures
140 #ifndef AT91_EMAC_RX_BUFF_SIZE
141 #define AT91_EMAC_RX_BUFF_SIZE 128
144 // Receive Buffer Descriptor
154 cyg_uint8 rb[AT91_EMAC_RX_BUFF_SIZE];
157 // Transmit Buffer Descriptor
164 // AT91 Ethernet private data
165 typedef struct at91_eth_priv_s
167 cyg_uint32 intr_vector;
168 char *esa_key; // RedBoot 'key' for device ESA
170 cyg_uint32 base; // Base address of device
171 eth_phy_access_t *phy;
172 rbd_t rbd[CYGNUM_DEVS_ETH_ARM_AT91_RX_BUFS];
173 rb_t rb[CYGNUM_DEVS_ETH_ARM_AT91_RX_BUFS];
174 tbd_t tbd[CYGNUM_DEVS_ETH_ARM_AT91_TX_BUFS];
175 unsigned long curr_tx_key;
177 cyg_uint32 last_tbd_idx;
178 cyg_uint32 curr_tbd_idx;
179 cyg_uint32 curr_rbd_idx;
180 #ifdef CYGINT_IO_ETH_INT_SUPPORT_REQUIRED
182 cyg_handle_t intr_handle;
186 //============================================================================
187 // PHY access bits and pieces
191 at91_mdio_enable(void)
194 HAL_READ_UINT32(AT91_EMAC + AT91_EMAC_NCR, val);
195 val |= AT91_EMAC_NCR_MPE; /* enable management port */
196 HAL_WRITE_UINT32(AT91_EMAC + AT91_EMAC_NCR, val);
200 at91_mdio_disable(void)
203 HAL_READ_UINT32(AT91_EMAC + AT91_EMAC_NCR, val);
204 val &= ~AT91_EMAC_NCR_MPE; /* disable management port */
205 HAL_WRITE_UINT32(AT91_EMAC + AT91_EMAC_NCR, val);
208 // Write one of the PHY registers via the MII bus
210 at91_write_phy(int reg_addr, int phy_addr, unsigned short data)
212 cyg_uint32 val, cnt=0;
214 CYG_ASSERTC(reg_addr >= 0 && reg_addr <= AT91_EMAC_MAN_REGA_MASK);
215 CYG_ASSERTC(phy_addr >= 0 && phy_addr <= AT91_EMAC_MAN_PHY_MASK);
217 val = (AT91_EMAC_MAN_SOF |
220 AT91_EMAC_MAN_PHYA(phy_addr) |
221 AT91_EMAC_MAN_REGA(reg_addr) |
222 AT91_EMAC_MAN_DATA(data));
224 HAL_WRITE_UINT32(AT91_EMAC + AT91_EMAC_MAN, val);
226 /* Wait until IDLE bit in Network Status register is cleared */
227 while (cnt < 1000000)
229 HAL_READ_UINT32((AT91_EMAC + AT91_EMAC_NSR), val);
230 if (!(val & AT91_EMAC_NSR_IDLE))
233 CYG_ASSERTC(cnt < 1000000);
237 // Read one of the PHY registers via the MII bus
239 at91_read_phy(int reg_addr, int phy_addr, unsigned short *data)
243 CYG_ASSERTC(reg_addr >= 0 && reg_addr <= AT91_EMAC_MAN_REGA_MASK);
244 CYG_ASSERTC(phy_addr >= 0 && phy_addr <= AT91_EMAC_MAN_PHY_MASK);
246 val = (AT91_EMAC_MAN_SOF |
249 AT91_EMAC_MAN_PHYA(phy_addr) |
250 AT91_EMAC_MAN_REGA(reg_addr));
253 HAL_WRITE_UINT32(AT91_EMAC + AT91_EMAC_MAN, val);
254 /* Wait until IDLE bit in Network Status register is cleared */
257 HAL_READ_UINT32((AT91_EMAC + AT91_EMAC_NSR), val);
258 }while(val & AT91_EMAC_NSR_IDLE);
262 HAL_READ_UINT32(AT91_EMAC + AT91_EMAC_MAN, val);
263 *data = val & AT91_EMAC_MAN_DATA_MASK;
268 // Enable the MDIO bit in MAC control register so that we can talk to
269 // the PHY. Also set the clock divider so that MDC is less than 2.5MHz.
276 HAL_READ_UINT32(AT91_EMAC + AT91_EMAC_NCFG, cfg);
277 cfg &=~ AT91_EMAC_NCFG_CLK_MASK;
279 div = (CYGNUM_HAL_ARM_AT91_CLOCK_SPEED / 2500000);
282 cfg |= AT91_EMAC_NCFG_CLK_HCLK_8;
286 cfg |= AT91_EMAC_NCFG_CLK_HCLK_16;
290 cfg |= AT91_EMAC_NCFG_CLK_HCLK_32;
294 cfg |= AT91_EMAC_NCFG_CLK_HCLK_64;
298 CYG_FAIL("Unable to program MII clock");
301 HAL_WRITE_UINT32(AT91_EMAC + AT91_EMAC_NCFG, cfg);
304 ETH_PHY_REG_LEVEL_ACCESS_FUNS(at91_phy,
310 //======================================================================
311 // Receiver buffer handling
313 // Initialize the receiver buffers and descriptors
315 at91_rb_init(at91_eth_priv_t *priv)
318 for (i = 0 ; i < CYGNUM_DEVS_ETH_ARM_AT91_RX_BUFS; i++)
320 priv->rbd[i].addr = ((cyg_uint32)&priv->rb[i]) & AT91_EMAC_RBD_ADDR_MASK;
323 // Set the wrap bit on the last entry
324 priv->rbd[CYGNUM_DEVS_ETH_ARM_AT91_RX_BUFS-1].addr |=
325 AT91_EMAC_RBD_ADDR_WRAP;
328 //======================================================================
329 // Transmit buffer handling
331 // Initialize the transmit buffer descriptors
333 at91_tb_init(at91_eth_priv_t *priv)
336 for (i = 0 ; i < CYGNUM_DEVS_ETH_ARM_AT91_TX_BUFS; i++)
338 priv->tbd[i].addr = 0;
339 priv->tbd[i].sr = AT91_EMAC_TBD_SR_USED;
341 // Set the wrap bit on the last entry
342 priv->tbd[CYGNUM_DEVS_ETH_ARM_AT91_TX_BUFS-1].sr |= AT91_EMAC_TBD_SR_WRAP;
345 //======================================================================
346 // Enable and Disable of the receiver and transmitter.
349 at91_disable_rx(at91_eth_priv_t *priv)
353 HAL_READ_UINT32(priv->base + AT91_EMAC_NCR, ctl);
354 ctl &= ~AT91_EMAC_NCR_RE;
355 HAL_WRITE_UINT32(priv->base + AT91_EMAC_NCR, ctl);
359 at91_disable_tx(at91_eth_priv_t *priv)
363 HAL_READ_UINT32(priv->base + AT91_EMAC_NCR, ctl);
364 ctl &= ~AT91_EMAC_NCR_TX;
365 HAL_WRITE_UINT32(priv->base + AT91_EMAC_NCR, ctl);
369 at91_enable_rx(at91_eth_priv_t *priv)
373 HAL_READ_UINT32(priv->base + AT91_EMAC_NCR, ctl);
374 ctl |= AT91_EMAC_NCR_RE;
375 HAL_WRITE_UINT32(priv->base + AT91_EMAC_NCR, ctl);
379 at91_enable_tx(at91_eth_priv_t *priv)
383 HAL_READ_UINT32(priv->base + AT91_EMAC_NCR, ctl);
384 ctl |= AT91_EMAC_NCR_TX;
385 HAL_WRITE_UINT32(priv->base + AT91_EMAC_NCR, ctl);
389 at91_enable(at91_eth_priv_t *priv)
391 at91_enable_tx(priv);
392 at91_enable_rx(priv);
396 at91_disable(at91_eth_priv_t *priv)
398 at91_disable_tx(priv);
399 at91_disable_rx(priv);
403 at91_start_transmitter(at91_eth_priv_t *priv)
407 HAL_READ_UINT32(priv->base + AT91_EMAC_NCR, ctl);
408 ctl |= AT91_EMAC_NCR_TSTART;
409 HAL_WRITE_UINT32(priv->base + AT91_EMAC_NCR, ctl);
413 //======================================================================
414 // Initialization code
416 // Configure the pins so that the EMAC has control of them. This
417 // assumes the MII is used, not the RMII
421 HAL_ARM_AT91_PIO_CFG(AT91_EMAC_EREFCK);
422 HAL_ARM_AT91_PIO_CFG(AT91_EMAC_ECRS);
423 HAL_ARM_AT91_PIO_CFG(AT91_EMAC_ECOL);
425 HAL_ARM_AT91_PIO_CFG(AT91_EMAC_ERXDV);
426 HAL_ARM_AT91_PIO_CFG(AT91_EMAC_ERX0);
427 HAL_ARM_AT91_PIO_CFG(AT91_EMAC_ERX1);
428 HAL_ARM_AT91_PIO_CFG(AT91_EMAC_ERX2);
429 HAL_ARM_AT91_PIO_CFG(AT91_EMAC_ERX3);
430 HAL_ARM_AT91_PIO_CFG(AT91_EMAC_ERXER);
431 HAL_ARM_AT91_PIO_CFG(AT91_EMAC_ERXCK);
433 HAL_ARM_AT91_PIO_CFG(AT91_EMAC_ETXEN);
434 HAL_ARM_AT91_PIO_CFG(AT91_EMAC_ETX0);
435 HAL_ARM_AT91_PIO_CFG(AT91_EMAC_ETX1);
436 HAL_ARM_AT91_PIO_CFG(AT91_EMAC_ETX2);
437 HAL_ARM_AT91_PIO_CFG(AT91_EMAC_ETX3);
438 HAL_ARM_AT91_PIO_CFG(AT91_EMAC_ETXER);
440 HAL_ARM_AT91_PIO_CFG(AT91_EMAC_EMDC);
441 HAL_ARM_AT91_PIO_CFG(AT91_EMAC_EMDIO);
444 // Set a specific address match to a given address. Packets received which
445 // match this address will be passed on.
447 at91_set_mac(at91_eth_priv_t * priv, cyg_uint8 * enaddr, int sa)
451 CYG_ASSERTC(sa > 0 && sa < 5);
454 lo = ((enaddr[3] << 24) |
459 hi = ((enaddr[5] << 8) |
462 HAL_WRITE_UINT32(priv->base + AT91_EMAC_SA1L + (8*sa), lo);
463 HAL_WRITE_UINT32(priv->base + AT91_EMAC_SA1H + (8*sa), hi);
467 at91_clear_stats(at91_eth_priv_t *priv)
471 HAL_READ_UINT32(priv->base + AT91_EMAC_NCR, ctl);
472 ctl |= AT91_EMAC_NCR_CSR;
473 HAL_WRITE_UINT32(priv->base + AT91_EMAC_NCR, ctl);
476 // Enable and Disable of the receiver and transmitter.
477 // Initialize the interface. This configures the interface ready for use.
478 // Interrupts are grabbed etc. This means the start function has
479 // little to do except enable the receiver
481 at91_eth_init(struct cyg_netdevtab_entry *tab)
483 struct eth_drv_sc *sc = (struct eth_drv_sc *)tab->device_instance;
484 at91_eth_priv_t *priv = (at91_eth_priv_t *)sc->driver_private;
486 unsigned char enaddr[6] = { CYGPKG_DEVS_ETH_ARM_AT91_MACADDR};
487 unsigned char enzero[6] = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
488 unsigned short phy_state = 0;
491 debug1_printf("\nAT91_ETH: Initialising @ %x\n",priv->base);
493 priv->tx_busy = false;
494 priv->curr_tbd_idx = 0;
495 priv->curr_rbd_idx = 0;
497 // Enable the clock to the EMAC
498 HAL_WRITE_UINT32(AT91_PMC + AT91_PMC_PCER, AT91_PMC_PCER_EMAC);
499 HAL_WRITE_UINT32(AT91_PMC + AT91_PMC_PCER, AT91_PMC_PCER_PIOB);
500 HAL_WRITE_UINT32(AT91_PMC + AT91_PMC_PCER, AT91_PMC_PCER_PIOA);
502 //at91_disable(priv);
505 /* Enable IO Clock */
506 HAL_WRITE_UINT32(priv->base+AT91_EMAC_USRIO,AT91_EMAC_USRIO_CLKEN);
508 /* Disable all the interrupts for the moment */
509 /* The Start function actually enables all that we need */
510 //HAL_WRITE_UINT32(priv->base + AT91_EMAC_IDR, 0x3FFF);
512 // If we are building an interrupt enabled version, install the
514 #ifdef CYGINT_IO_ETH_INT_SUPPORT_REQUIRED
515 debug1_printf("AT91_ETH: Installing Interrupts on IRQ %d\n",
517 cyg_drv_interrupt_create(priv->intr_vector,
525 cyg_drv_interrupt_attach(priv->intr_handle);
526 cyg_drv_interrupt_unmask(priv->intr_vector);
529 #ifdef CYGHWR_DEVS_ETH_ARM_AT91_GET_ESA
530 // Get MAC address from RedBoot configuration variables
531 CYGHWR_DEVS_ETH_ARM_AT91_GET_ESA(&enaddr[0], esa_ok);
532 // If this call fails myMacAddr is unchanged and MAC address from
538 // Can't figure out ESA
539 debug1_printf("AT91_ETH - Warning! ESA unknown\n");
541 debug1_printf("AT91_ETH: %02x:%02x:%02x:%02x:%02x:%02x\n",
542 enaddr[0],enaddr[1],enaddr[2],
543 enaddr[3],enaddr[4],enaddr[5]);
545 // Give the EMAC its address
546 at91_set_mac(priv, enaddr, 1);
547 at91_set_mac(priv, enzero, 2);
548 at91_set_mac(priv, enzero, 3);
549 at91_set_mac(priv, enzero, 4);
551 // Setup the receiver buffers and descriptors
554 // And tell the EMAC where the first receive buffer descriptor is
555 HAL_WRITE_UINT32(priv->base + AT91_EMAC_RBQP, (cyg_uint32)priv->rbd);
557 // Setup the transmit descriptors
560 // And tell the EMAC where the first transmit buffer descriptor is
561 HAL_WRITE_UINT32(priv->base + AT91_EMAC_TBQP, (cyg_uint32)priv->tbd);
564 CYG_ASSERTC(priv->phy);
567 if (!_eth_phy_init(priv->phy))
573 // Get the current mode and print it
574 phy_state = _eth_phy_state(priv->phy);
577 HAL_READ_UINT32(priv->base + AT91_EMAC_NCFG,ncfg);
580 if ((phy_state & ETH_PHY_STAT_LINK) != 0)
582 if (((phy_state & ETH_PHY_STAT_100MB) != 0))
584 debug1_printf("AT91_ETH: 100Mbyte/s");
585 ncfg |= AT91_EMAC_NCFG_SPD_100Mbps;
589 debug1_printf("AT91_ETH: 10Mbyte/s");
590 ncfg &= ~(AT91_EMAC_NCFG_SPD_100Mbps);
592 if((phy_state & ETH_PHY_STAT_FDX))
594 debug1_printf(" Full Duplex\n");
595 ncfg |= AT91_EMAC_NCFG_FD;
599 debug1_printf(" Half Duplex\n");
600 ncfg &= ~(AT91_EMAC_NCFG_FD);
605 debug1_printf("AT91_ETH: No Link\n");
609 //Setup the network configuration
610 ncfg |= (AT91_EMAC_NCFG_RLCE);
612 HAL_WRITE_UINT32(priv->base + AT91_EMAC_NCFG,ncfg);
614 // Clear the Statistics counters;
615 at91_clear_stats(priv);
618 /* Clear the status registers */
619 HAL_READ_UINT32(priv->base + AT91_EMAC_ISR,ncfg);
620 HAL_READ_UINT32(priv->base + AT91_EMAC_RSR,ncfg);
621 HAL_WRITE_UINT32(priv->base + AT91_EMAC_RSR,ncfg);
622 HAL_READ_UINT32(priv->base + AT91_EMAC_TSR,ncfg);
623 HAL_WRITE_UINT32(priv->base + AT91_EMAC_TSR,ncfg);
625 // Initialize the upper layer driver
626 _eth_drv_init(sc,enaddr);
631 // This function is called to stop the interface.
633 at91_eth_stop(struct eth_drv_sc *sc)
635 at91_eth_priv_t *priv = (at91_eth_priv_t *)sc->driver_private;
640 // This function is called to "start up" the interface. It may be called
641 // multiple times, even when the hardware is already running.
643 at91_eth_start(struct eth_drv_sc *sc, unsigned char *enaddr, int flags)
645 at91_eth_priv_t *priv = (at91_eth_priv_t *)sc->driver_private;
648 // Enable the interrupts we are interested in
649 // TODO: We probably need to add at least the RBNA interrupt here
650 // as well in order to do some error handling
651 bits = (AT91_EMAC_ISR_RCOM | AT91_EMAC_ISR_TCOM);
653 HAL_WRITE_UINT32(priv->base + AT91_EMAC_IER, bits);
655 // Enable the receiver and transmitter
659 // This function is called for low level "control" operations
661 at91_eth_control(struct eth_drv_sc *sc, unsigned long key,
662 void *data, int length)
667 case ETH_DRV_SET_MAC_ADDRESS:
669 if(length >= ETHER_ADDR_LEN)
673 cyg_uint8 * enaddr = (cyg_uint8 *)data;
674 debug1_printf("AT91_ETH: %02x:%02x:%02x:%02x:%02x:%02x\n",
675 enaddr[0],enaddr[1],enaddr[2],
676 enaddr[3],enaddr[4],enaddr[5]);
678 at91_set_mac((at91_eth_priv_t *)sc->driver_private,enaddr,1);
679 at91_eth_start(sc,enaddr,0);
686 diag_printf("%s.%d: key %lx\n", __FUNCTION__, __LINE__, key);
693 // This function is called to see if another packet can be sent.
694 // It should return the number of packets which can be handled.
695 // Zero should be returned if the interface is busy and can not send
698 // We allocate one buffer descriptor per scatter/gather entry. We assume that
699 // a typical packet will not have more than 3 such entries, and so we say we
700 // can send a packet when we have 3 or more buffer descriptors free
702 // TODO: Implement what the comment actually says!
704 at91_eth_can_send(struct eth_drv_sc *sc)
707 at91_eth_priv_t *priv = (at91_eth_priv_t *)sc->driver_private;
719 // This routine is called to send data to the hardware
721 at91_eth_send(struct eth_drv_sc *sc, struct eth_drv_sg *sg_list, int sg_len,
722 int total_len, unsigned long key)
724 at91_eth_priv_t *priv = (at91_eth_priv_t *)sc->driver_private;
728 priv->tx_busy = true;
730 priv->last_tbd_idx = priv->curr_tbd_idx;
732 for(i = 0;i<sg_len;i++)
734 priv->tbd[priv->curr_tbd_idx].addr = sg_list[i].buf;
736 sr = (sg_list[i].len & AT91_EMAC_TBD_SR_LEN_MASK);
737 // Set the End Of Frame bit in the last descriptor
740 sr |= AT91_EMAC_TBD_SR_EOF;
743 if(priv->curr_tbd_idx < (CYGNUM_DEVS_ETH_ARM_AT91_TX_BUFS-1))
745 priv->tbd[priv->curr_tbd_idx].sr = sr;
746 priv->curr_tbd_idx++;
750 priv->tbd[priv->curr_tbd_idx].sr = (sr | AT91_EMAC_TBD_SR_WRAP);
751 priv->curr_tbd_idx = 0;
755 // Store away the key for when the transmit has completed
756 // and we need to tell the stack which transmit has completed.
757 priv->curr_tx_key = key;
759 at91_start_transmitter(priv);
762 static void at91_reset_tbd(at91_eth_priv_t *priv)
764 while(priv->curr_tbd_idx != priv->last_tbd_idx)
766 if(priv->last_tbd_idx == (CYGNUM_DEVS_ETH_ARM_AT91_TX_BUFS-1))
768 priv->tbd[priv->last_tbd_idx].sr =
769 (AT91_EMAC_TBD_SR_USED|AT91_EMAC_TBD_SR_WRAP);
770 priv->last_tbd_idx = 0;
774 priv->tbd[priv->last_tbd_idx].sr = AT91_EMAC_TBD_SR_USED;
775 priv->last_tbd_idx++;
781 //======================================================================
783 #ifdef CYGINT_IO_ETH_INT_SUPPORT_REQUIRED
785 at91_eth_isr (cyg_vector_t vector, cyg_addrword_t data)
787 struct eth_drv_sc *sc = (struct eth_drv_sc *)data;
788 at91_eth_priv_t *priv = (at91_eth_priv_t *)sc->driver_private;
792 /* Get the interrupt status */
793 HAL_READ_UINT32(priv->base+AT91_EMAC_ISR,isr);
795 ret = CYG_ISR_HANDLED;
797 //TODO: We should probably be handling some of the error interrupts as well
798 if(isr & AT91_EMAC_ISR_TCOM)
800 ret = CYG_ISR_CALL_DSR;
803 if(isr & AT91_EMAC_ISR_RCOM)
805 ret = CYG_ISR_CALL_DSR;
807 cyg_interrupt_acknowledge(vector);
813 at91_eth_deliver(struct eth_drv_sc *sc)
815 at91_eth_priv_t *priv = (at91_eth_priv_t *)sc->driver_private;
824 /* Get the Transmit Status */
825 HAL_READ_UINT32(priv->base+AT91_EMAC_TSR,tsr);
826 HAL_WRITE_UINT32(priv->base+AT91_EMAC_TSR,tsr);
828 /* Get the Receive Status */
829 HAL_READ_UINT32(priv->base+AT91_EMAC_RSR,rsr);
830 HAL_WRITE_UINT32(priv->base+AT91_EMAC_RSR,rsr);
833 //TODO: The interrupts other than RCOMP and TCOMP needs to be
834 // handled properly especially stuff like RBNA which could have
835 // serious effects on driver performance
837 /* Service the TX buffers */
838 if (tsr&AT91_EMAC_TSR_COL) //1
840 debug1_printf("AT91_ETH: Tx COL\n");
843 if (tsr&AT91_EMAC_TSR_RLE) //2
845 debug1_printf("AT91_ETH: Tx RLE\n");
848 if (tsr&AT91_EMAC_TSR_BNQ) //4
850 debug1_printf("AT91_ETH: Tx BEX\n");
853 if (tsr&AT91_EMAC_TSR_UND) //6
855 debug1_printf("AT91_ETH: Tx UND\n");
858 /* Check that the last transmission is completed */
859 if (tsr&AT91_EMAC_TSR_COMP) //5
861 at91_reset_tbd(priv);
862 _eth_drv_tx_done(sc,priv->curr_tx_key,0);
863 priv->tx_busy = false;
866 /* Service the RX buffers when we get something */
867 if (rsr&AT91_EMAC_RSR_REC)
869 /* Do this all until we find the first EMAC Buffer */
870 while (priv->rbd[priv->curr_rbd_idx].addr & AT91_EMAC_RBD_ADDR_OWNER_SW)
873 //Firstly walk through to either the first buffer that belongs
874 // to the controller or the first SOF
875 while ((priv->rbd[priv->curr_rbd_idx].addr &
876 AT91_EMAC_RBD_ADDR_OWNER_SW) &&
877 !(priv->rbd[priv->curr_rbd_idx].sr &
878 AT91_EMAC_RBD_SR_SOF))
880 priv->rbd[priv->curr_rbd_idx].addr &=
881 ~(AT91_EMAC_RBD_ADDR_OWNER_SW);
882 priv->curr_rbd_idx++;
883 if (priv->curr_rbd_idx >= CYGNUM_DEVS_ETH_ARM_AT91_RX_BUFS)
885 priv->curr_rbd_idx = 0;
889 /* Check that we did find a SOF*/
890 if ((priv->rbd[priv->curr_rbd_idx].addr &
891 AT91_EMAC_RBD_ADDR_OWNER_SW) &&
892 (priv->rbd[priv->curr_rbd_idx].sr & AT91_EMAC_RBD_SR_SOF))
895 for (ctr=0;ctr<CYGNUM_DEVS_ETH_ARM_AT91_RX_BUFS;ctr++)
897 idx = (ctr+priv->curr_rbd_idx)%CYGNUM_DEVS_ETH_ARM_AT91_RX_BUFS;
898 cnt += (priv->rbd[idx].sr & AT91_EMAC_RBD_SR_LEN_MASK);
899 if (priv->rbd[idx].sr & AT91_EMAC_RBD_SR_EOF)
901 /* The recv function will adjust the current buffer idx
902 after the buffer has been cleared
905 _eth_drv_recv(sc,cnt);
913 if (rsr&AT91_EMAC_RSR_BNA)
915 debug1_printf("AT91_ETH: Rx BNA\n");
917 if (rsr&AT91_EMAC_RSR_OVR)
919 debug1_printf("AT91_ETH: Rx OVR\n");
925 at91_eth_recv(struct eth_drv_sc *sc,
926 struct eth_drv_sg *sg_list,
929 at91_eth_priv_t *priv = (at91_eth_priv_t *)sc->driver_private;
931 cyg_uint32 bytes_in_buffer;
932 cyg_uint32 bytes_in_list = 0;
933 cyg_uint32 bytes_needed_list = 0;
934 cyg_uint32 buffer_pos = 0;
936 cyg_uint32 total_bytes = 0;
938 for(i = 0;i<sg_len;i++)
940 while(bytes_in_list < sg_list[i].len)
942 bytes_needed_list = sg_list[i].len - bytes_in_list;
944 if(priv->rbd[priv->curr_rbd_idx].sr & AT91_EMAC_RBD_SR_EOF)
947 ((priv->rbd[priv->curr_rbd_idx].sr & AT91_EMAC_RBD_SR_LEN_MASK)
948 - total_bytes) - buffer_pos;
952 bytes_in_buffer = AT91_EMAC_RX_BUFF_SIZE - buffer_pos;
955 sg_buf = (cyg_uint8 *)(sg_list[i].buf);
957 if(bytes_needed_list < bytes_in_buffer)
960 memcpy(&sg_buf[bytes_in_list],
961 &priv->rb[priv->curr_rbd_idx].rb[buffer_pos],
963 bytes_in_list += bytes_needed_list;
964 buffer_pos += bytes_needed_list;
965 total_bytes += bytes_needed_list;
970 memcpy(&sg_buf[bytes_in_list],
971 &priv->rb[priv->curr_rbd_idx].rb[buffer_pos],
973 bytes_in_list += bytes_in_buffer;
974 total_bytes += bytes_in_buffer;
976 /* Step our buffer on one */
977 priv->rbd[priv->curr_rbd_idx].addr &=
978 ~(AT91_EMAC_RBD_ADDR_OWNER_SW);
979 priv->curr_rbd_idx++;
980 if(priv->curr_rbd_idx >= CYGNUM_DEVS_ETH_ARM_AT91_RX_BUFS)
982 priv->curr_rbd_idx = 0;
990 // routine called to handle ethernet controller in polled mode
992 at91_eth_poll(struct eth_drv_sc *sc)
994 /* Service the buffers */
995 at91_eth_deliver(sc);
999 at91_eth_int_vector(struct eth_drv_sc *sc)
1001 return(CYGNUM_HAL_INTERRUPT_EMAC);
1004 at91_eth_priv_t at91_priv_data =
1006 .intr_vector = CYGNUM_HAL_INTERRUPT_EMAC,
1012 &at91_priv_data, // Driver specific data
1013 "eth0", // Name for this interface
1022 at91_eth_int_vector);
1024 NETDEVTAB_ENTRY(at91_netdev,