1 //==========================================================================
5 // Hardware independent ethernet driver for lwIP
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.
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
43 // Author(s): Jani Monoses <jani@iv.ro>
46 // Purpose: Hardware independent ethernet driver
47 // Description: Based on the standalone driver for RedBoot.
49 //####DESCRIPTIONEND####
51 //==========================================================================
53 #include <pkgconf/system.h>
54 #include <pkgconf/io_eth_drivers.h>
56 #include <cyg/infra/cyg_type.h>
57 #include <cyg/hal/hal_arch.h>
58 #include <cyg/infra/diag.h>
59 #include <cyg/hal/drv_api.h>
60 #include <cyg/hal/hal_if.h>
61 #include <cyg/io/eth/eth_drv.h>
62 #include <cyg/io/eth/netdev.h>
65 #include <cyg/hal/hal_tables.h>
66 #include <cyg/kernel/kapi.h>
71 #include "lwip/pbuf.h"
74 #include "netif/etharp.h"
77 // Interfaces exported to drivers
79 static void eth_drv_init(struct eth_drv_sc *sc, unsigned char *enaddr);
80 static void eth_drv_recv(struct eth_drv_sc *sc, int total_len);
81 static void eth_drv_tx_done(struct eth_drv_sc *sc, CYG_ADDRWORD key, int status);
83 struct eth_drv_funs eth_drv_funs = { eth_drv_init, eth_drv_recv, eth_drv_tx_done };
85 #ifdef CYGDBG_IO_ETH_DRIVERS_DEBUG
86 int cyg_io_eth_net_debug = CYGDBG_IO_ETH_DRIVERS_DEBUG_VERBOSITY;
89 extern void lwip_dsr_stuff(void);
90 extern void lwip_set_addr(struct netif *);
92 //DSR called from the low level driver.Signals the input_thread
94 eth_drv_dsr(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
96 struct eth_drv_sc *sc = (struct eth_drv_sc *) data;
97 sc->state |= ETH_DRV_NEEDS_DELIVERY;
101 err_t ecosif_init(struct netif *netif);
103 // This function is called during system initialization to register a
104 // network interface with the system.
106 eth_drv_init(struct eth_drv_sc *sc, unsigned char *enaddr)
108 struct netif *netif = &sc->sc_arpcom.ac_if;
113 // enaddr == 0 -> hardware init was incomplete (no ESA)
115 // Set up hardware address
116 memcpy(netif->hwaddr, enaddr, ETHER_ADDR_LEN);
117 // Perform any hardware initialization
118 (sc->funs->start) (sc, (unsigned char *) &netif->hwaddr, 0);
120 #ifdef CYGSEM_HAL_VIRTUAL_VECTOR_DIAG
121 // Set up interfaces so debug environment can share this device
123 void *dbg = CYGACC_CALL_IF_DBG_DATA();
125 CYGACC_CALL_IF_DBG_DATA_SET((void *)sc);
133 // Control whether any special locking needs to take place if we intend to
134 // cooperate with a ROM monitor (e.g. RedBoot) using this hardware.
136 #if defined(CYGSEM_HAL_USE_ROM_MONITOR) && \
137 defined(CYGSEM_HAL_VIRTUAL_VECTOR_DIAG) && \
138 !defined(CYGSEM_HAL_VIRTUAL_VECTOR_CLAIM_COMMS)
140 // Indicate that special locking precautions are warranted.
141 #define _LOCK_WITH_ROM_MONITOR
143 // This defines the [well known] channel that RedBoot will use when it is
144 // using the network hardware for the debug channel.
145 #define RedBoot_TCP_CHANNEL CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS
150 // Send a packet of data to the hardware
154 eth_drv_send(struct netif *netif, struct pbuf *p)
156 struct eth_drv_sg sg_list[MAX_ETH_DRV_SG];
157 struct eth_drv_sc *sc = netif->state;
161 #ifdef _LOCK_WITH_ROM_MONITOR
162 bool need_lock = false;
166 while (!(sc->funs->can_send) (sc));
168 for (q = p; q != NULL; q = q->next) {
169 sg_list[sg_len].buf = (CYG_ADDRESS) q->payload;
170 sg_list[sg_len++].len = q->len;
172 #ifdef _LOCK_WITH_ROM_MONITOR
173 debug_chan = CYGACC_CALL_IF_SET_DEBUG_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
174 if (debug_chan == RedBoot_TCP_CHANNEL) {
178 #endif // _LOCK_WITH_ROM_MONITOR
180 (sc->funs->send) (sc, sg_list, sg_len, p->tot_len,
183 #ifdef _LOCK_WITH_ROM_MONITOR
184 // Unlock the driver & hardware. It can once again be safely shared.
186 cyg_drv_dsr_unlock();
188 #endif // _LOCK_WITH_ROM_MONITOR
193 // This function is called from the hardware driver when an output operation
194 // has completed - i.e. the packet has been sent.
197 eth_drv_tx_done(struct eth_drv_sc *sc, CYG_ADDRWORD key, int status)
200 struct pbuf *p = (struct pbuf *)key;
201 struct netif *netif = &sc->sc_arpcom.ac_if;
203 CYGARC_HAL_SAVE_GP();
204 CYGARC_HAL_RESTORE_GP();
208 static void ecosif_input(struct netif *netif, struct pbuf* pbuf);
210 #define MAX_ETH_MSG 1540
212 // This function is called from a hardware driver to indicate that an input
213 // packet has arrived. The routine will set up appropriate network resources
214 // to hold the data and call back into the driver to retrieve the data.
217 eth_drv_recv(struct eth_drv_sc *sc, int total_len)
219 struct eth_drv_sg sg_list[MAX_ETH_DRV_SG];
220 struct netif *netif = &sc->sc_arpcom.ac_if;
225 CYGARC_HAL_SAVE_GP();
227 if ((total_len > MAX_ETH_MSG) || (total_len < 0)) {
228 total_len = MAX_ETH_MSG;
231 p = pbuf_alloc(PBUF_RAW, total_len, PBUF_POOL);
234 LWIP_DEBUGF(0, ("ecosif_input: low_level_input returned NULL\n"));
238 for (q = p; q != NULL; q = q->next) {
239 sg_list[sg_len].buf = (CYG_ADDRESS) q->payload;
240 sg_list[sg_len++].len = q->len;
242 (sc->funs->recv) (sc, sg_list, sg_len);
243 ecosif_input(netif, p);
244 CYGARC_HAL_RESTORE_GP();
254 // low_level_output():
256 // Should do the actual transmission of the packet. The packet is
257 // contained in the pbuf that is passed to the function. This pbuf
258 // might be chained.We pass the data down to the eCos hw independent
263 low_level_output(struct netif *netif, struct pbuf *p)
265 eth_drv_send(netif, p);
272 // This function is called by the TCP/IP stack when an IP packet
273 // should be sent. It calls the function called low_level_output() to
274 // do the actual transmission of the packet.
278 ecosif_output(struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr)
281 p = etharp_output(netif, ipaddr, p);
283 low_level_output(netif, p);
293 // This function is called when the eCos hw independent driver
294 // has some data to pass up to lwIP.It does it through ecosif_input.
297 ecosif_input(struct netif *netif, struct pbuf *p)
299 struct eth_hdr *ethhdr;
303 switch (htons(ethhdr->type)) {
305 LWIP_DEBUGF(0, ("ecosif_input: IP packet\n"));
306 etharp_ip_input(netif, p);
308 netif->input(p, netif);
311 LWIP_DEBUGF(0, ("ecosif_input: ARP packet\n"));
312 etharp_arp_input(netif, (struct eth_addr *) &netif->hwaddr, p);
322 ecosif_init(struct netif *netif)
324 netif->name[0] = IFNAME0;
325 netif->name[1] = IFNAME1;
326 netif->hwaddr_len = 6;
327 netif->output = ecosif_output;
328 netif->linkoutput = low_level_output;
330 lwip_set_addr(netif);