1 //==========================================================================
5 // Ethernet device driver for Socket Communications Compact Flash card
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) 2002 Gary Thomas
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.
37 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38 // at http://sources.redhat.com/ecos/ecos-license/
39 // -------------------------------------------
40 //####ECOSGPLCOPYRIGHTEND####
41 //####BSDCOPYRIGHTBEGIN####
43 // -------------------------------------------
45 // Portions of this software may have been derived from OpenBSD or other sources,
46 // and are covered by the appropriate copyright disclaimers included herein.
48 // -------------------------------------------
50 //####BSDCOPYRIGHTEND####
51 //==========================================================================
52 //#####DESCRIPTIONBEGIN####
55 // Contributors: gthomas, jskov
58 // Description: hardware driver for LPCF+ ethernet
61 //####DESCRIPTIONEND####
63 //==========================================================================
65 #include <pkgconf/system.h>
66 #include <pkgconf/io_eth_drivers.h>
68 #include <cyg/infra/cyg_type.h>
69 #include <cyg/hal/hal_arch.h>
70 #include <cyg/infra/diag.h>
71 #include <cyg/hal/drv_api.h>
72 #include <cyg/io/pcmcia.h>
73 #include <cyg/io/eth/eth_drv.h>
74 #include <cyg/io/eth/netdev.h>
77 #include <pkgconf/net.h>
79 #include <cyg/hal/hal_if.h>
82 #include <cyg/io/dp83902a.h>
85 #define DP_CARD_RESET 0x1f
87 #define SC_LPE_MANUF 0x0104
90 #define STACK_SIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
91 static char sc_lpe_card_handler_stack[STACK_SIZE];
92 static cyg_thread sc_lpe_card_handler_thread_data;
93 static cyg_handle_t sc_lpe_card_handler_thread_handle;
94 #endif // CYGPKG_KERNEL
96 __inline__ static void
100 cyg_thread_delay(_ticks);
102 CYGACC_CALL_IF_DELAY_US(10000*_ticks);
107 // This runs as a separate thread to handle the card. In particular, insertions
108 // and deletions need to be handled and they take time/coordination, thus the
116 sc_lpe_card_handler(cyg_addrword_t param)
118 struct eth_drv_sc *sc = (struct eth_drv_sc *)param;
119 dp83902a_priv_data_t *dp = (dp83902a_priv_data_t*)sc->driver_private;
120 struct cf_slot *slot;
121 struct cf_cftable cftable;
122 struct cf_config config;
123 int i, len, ptr, cor = 0;
124 unsigned char buf[256], *cp;
126 unsigned char *vers_product, *vers_manuf, *vers_revision, *vers_date;
127 #ifndef CYGPKG_KERNEL
132 slot = (struct cf_slot*)dp->plf_priv;
135 cyg_drv_dsr_unlock(); // Give DSRs a chance to run (card insertion)
137 if ((slot->state == CF_SLOT_STATE_Inserted) ||
138 ((slot->state == CF_SLOT_STATE_Ready) && first)) {
140 if (slot->state != CF_SLOT_STATE_Ready) {
141 cf_change_state(slot, CF_SLOT_STATE_Ready);
143 if (slot->state != CF_SLOT_STATE_Ready) {
144 diag_printf("CF card won't go ready!\n");
145 #ifndef CYGPKG_KERNEL
153 if (cf_get_CIS(slot, CF_CISTPL_MANFID, buf, &len, &ptr)) {
154 if (*(short *)&buf[2] != SC_LPE_MANUF) {
155 diag_printf("Not a SC LPE, sorry\n");
160 if (cf_get_CIS(slot, CF_CISTPL_VERS_1, buf, &len, &ptr)) {
161 // Find individual strings
164 while (*cp++) ; // Skip to nul
166 while (*cp++) ; // Skip to nul
168 while (*cp++) ; // Skip to nul
170 #ifndef CYGPKG_KERNEL
171 if (tries != 0) diag_printf("\n");
172 diag_printf("%s: %s %s %s\n", vers_manuf, vers_product, vers_revision, vers_date);
176 if (cf_get_CIS(slot, CF_CISTPL_CONFIG, buf, &len, &ptr)) {
177 if (cf_parse_config(buf, len, &config)) {
182 // diag_printf("Couldn't find COR pointer!\n");
187 if (cf_get_CIS(slot, CF_CISTPL_CFTABLE_ENTRY, buf, &len, &ptr)) {
188 if (cf_parse_cftable(buf, len, &cftable)) {
190 // Initialize dp83902a IO details
191 dp->base = base = (cyg_uint8*)&slot->io[cftable.io_space.base[0]];
192 dp->data = base + DP_DATA;
193 dp->interrupt = slot->int_num;
194 cf_set_COR(slot, cor, cftable.cor);
195 // Reset card (read issues RESET, write clears it)
196 HAL_READ_UINT8(base+DP_CARD_RESET, tmp);
197 HAL_WRITE_UINT8(base+DP_CARD_RESET, tmp);
200 DP_IN(base, DP_ISR, tmp);
201 } while (0 == (tmp & DP_ISR_RESET));
203 // Fetch hardware address from card - terrible, but not well defined
204 // Patterned after what Linux drivers do
205 if (!dp->hardwired_esa) {
206 static unsigned char sc_lpe_addr[] = { 0x00, 0xC0, 0x1B, 0x00, 0x99, 0x9E};
207 if ((slot->attr[0x1C0] == sc_lpe_addr[0]) &&
208 (slot->attr[0x1C2] == sc_lpe_addr[1]) &&
209 (slot->attr[0x1C4] == sc_lpe_addr[2])) {
210 sc_lpe_addr[3] = slot->attr[0x1C6];
211 sc_lpe_addr[4] = slot->attr[0x1C8];
212 sc_lpe_addr[5] = slot->attr[0x1CA];
214 // Coudn't find it in the CIS (attribute) data
215 unsigned char prom[32];
217 // Tell device to give up ESA
218 DP_OUT(base, DP_DCR, 0x48); // Bytewide access
219 DP_OUT(base, DP_RBCH, 0); // Remote byte count
220 DP_OUT(base, DP_RBCL, 0);
221 DP_OUT(base, DP_ISR, 0xFF); // Clear any pending interrupts
222 DP_OUT(base, DP_IMR, 0x00); // Mask all interrupts
223 DP_OUT(base, DP_RCR, 0x20); // Monitor
224 DP_OUT(base, DP_TCR, 0x02); // loopback
225 DP_OUT(base, DP_RBCH, 32); // Remote byte count
226 DP_OUT(base, DP_RBCL, 0);
227 DP_OUT(base, DP_RSAL, 0); // Remote address
228 DP_OUT(base, DP_RSAH, 0);
229 DP_OUT(base, DP_CR, DP_CR_START|DP_CR_RDMA); // Read data
230 for (i = 0; i < 32; i++) {
231 HAL_READ_UINT8(base+DP_DATAPORT, prom[i]);
233 if ((prom[0] == sc_lpe_addr[0]) &&
234 (prom[2] == sc_lpe_addr[1]) &&
235 (prom[4] == sc_lpe_addr[2])) {
236 diag_printf("Getting address from port\n");
237 sc_lpe_addr[3] = prom[6];
238 sc_lpe_addr[4] = prom[8];
239 sc_lpe_addr[5] = prom[10];
241 diag_printf("No valid ESA found in CIS! Hardwiring to 00:C0:1B:00:99:9E\n");
244 for (i = 0; i < 6; i++) {
245 dp->esa[i] = sc_lpe_addr[i];
249 // Initialize upper level driver
250 (sc->funs->eth_drv->init)(sc, dp->esa);
251 // Tell system card is ready to talk
252 dp->tab->status = CYG_NETDEVTAB_STATUS_AVAIL;
253 #ifndef CYGPKG_KERNEL
254 cyg_drv_dsr_unlock();
258 diag_printf("Can't parse CIS\n");
262 diag_printf("Can't fetch config info\n");
265 } else if (slot->state == CF_SLOT_STATE_Removed) {
266 diag_printf("Compact Flash card removed!\n");
268 cyg_drv_dsr_unlock();
269 do_delay(50); // FIXME!
270 #ifndef CYGPKG_KERNEL
271 if (tries == 0) diag_printf("... Waiting for network card: ");
274 // 5 seconds have elapsed - give up
277 cf_hwr_poll(slot); // Check to see if card has been inserted
285 cyg_sc_lpe_init(struct cyg_netdevtab_entry *tab)
287 struct eth_drv_sc *sc = (struct eth_drv_sc *)tab->device_instance;
288 dp83902a_priv_data_t *dp = (dp83902a_priv_data_t *)sc->driver_private;
289 struct cf_slot* slot;
291 cf_init(); // Make sure Compact Flash subsystem is initialized
292 slot = dp->plf_priv = (void*)cf_get_slot(0);
296 // Create card handling [background] thread
297 cyg_thread_create(CYGPKG_NET_THREAD_PRIORITY-1, // Priority
298 sc_lpe_card_handler, // entry
299 (cyg_addrword_t)sc, // entry parameter
300 "SC LP-E card support", // Name
301 &sc_lpe_card_handler_stack[0], // Stack
303 &sc_lpe_card_handler_thread_handle, // Handle
304 &sc_lpe_card_handler_thread_data // Thread data structure
306 cyg_thread_resume(sc_lpe_card_handler_thread_handle); // Start it
308 // Initialize environment, setup interrupt handler
309 // eth_drv_dsr is used to tell the fast net thread to run the deliver funcion.
310 cf_register_handler(slot, eth_drv_dsr, sc);
312 return false; // Device is not ready until inserted, powered up, etc.
315 return sc_lpe_card_handler((cyg_addrword_t)sc);
320 cyg_sc_lpe_int_vector(struct eth_drv_sc *sc)
322 dp83902a_priv_data_t *dp = (dp83902a_priv_data_t *)sc->driver_private;
323 struct cf_slot* slot = (struct cf_slot*)dp->plf_priv;
325 return slot->int_num;