]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/devs/eth/cf/v2_0/src/if_sc_lpe.c
Initial revision
[karo-tx-redboot.git] / packages / devs / eth / cf / v2_0 / src / if_sc_lpe.c
1 //==========================================================================
2 //
3 //      dev/if_sc_lpe.c
4 //
5 //      Ethernet device driver for Socket Communications Compact Flash card
6 //
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
13 //
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.
17 //
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
21 // for more details.
22 //
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.
26 //
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.
33 //
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 // 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####
42 //
43 // -------------------------------------------
44 //
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.
47 //
48 // -------------------------------------------
49 //
50 //####BSDCOPYRIGHTEND####
51 //==========================================================================
52 //#####DESCRIPTIONBEGIN####
53 //
54 // Author(s):    gthomas
55 // Contributors: gthomas, jskov
56 // Date:         2000-07-07
57 // Purpose:      
58 // Description:  hardware driver for LPCF+ ethernet
59 //              
60 //
61 //####DESCRIPTIONEND####
62 //
63 //==========================================================================
64
65 #include <pkgconf/system.h>
66 #include <pkgconf/io_eth_drivers.h>
67
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>
75
76 #ifdef CYGPKG_NET
77 #include <pkgconf/net.h>
78 #else
79 #include <cyg/hal/hal_if.h>
80 #endif
81
82 #include <cyg/io/dp83902a.h>
83
84 #define DP_DATA         0x10
85 #define DP_CARD_RESET   0x1f
86
87 #define SC_LPE_MANUF 0x0104
88
89 #ifdef CYGPKG_KERNEL
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
95
96 __inline__ static void
97 do_delay(int _ticks)
98 {
99 #ifdef CYGPKG_KERNEL
100     cyg_thread_delay(_ticks);
101 #else
102     CYGACC_CALL_IF_DELAY_US(10000*_ticks);
103 #endif
104 }
105
106 //
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
109 // separate thread.
110 //
111 #ifdef CYGPKG_KERNEL
112 static void
113 #else
114 static int
115 #endif
116 sc_lpe_card_handler(cyg_addrword_t param)
117 {
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;
125     cyg_uint8* base;
126     unsigned char *vers_product, *vers_manuf, *vers_revision, *vers_date;
127 #ifndef CYGPKG_KERNEL
128     int tries = 0;
129 #endif
130     bool first = true;
131
132     slot = (struct cf_slot*)dp->plf_priv;
133     cyg_drv_dsr_lock();
134     while (true) {
135         cyg_drv_dsr_unlock();   // Give DSRs a chance to run (card insertion)
136         cyg_drv_dsr_lock();
137         if ((slot->state == CF_SLOT_STATE_Inserted) ||
138             ((slot->state == CF_SLOT_STATE_Ready) && first)) {
139             first = false;
140             if (slot->state != CF_SLOT_STATE_Ready) {
141                 cf_change_state(slot, CF_SLOT_STATE_Ready);
142             }
143             if (slot->state != CF_SLOT_STATE_Ready) {
144                 diag_printf("CF card won't go ready!\n");
145 #ifndef CYGPKG_KERNEL
146                 return false;
147 #else
148                 continue;
149 #endif
150             }
151             len = sizeof(buf);
152             ptr = 0;
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");
156                     continue;
157                 }
158             } 
159             ptr = 0;
160             if (cf_get_CIS(slot, CF_CISTPL_VERS_1, buf, &len, &ptr)) {
161                 // Find individual strings
162                 cp = &buf[4];
163                 vers_product = cp;
164                 while (*cp++) ;  // Skip to nul
165                 vers_manuf = cp;
166                 while (*cp++) ;  // Skip to nul
167                 vers_revision = cp;
168                 while (*cp++) ;  // Skip to nul
169                 vers_date = cp;
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);
173 #endif
174             }
175             ptr = 0;
176             if (cf_get_CIS(slot, CF_CISTPL_CONFIG, buf, &len, &ptr)) {
177                 if (cf_parse_config(buf, len, &config)) {
178                     cor = config.base;
179                 }
180             }
181             if (!cor) {
182 //                diag_printf("Couldn't find COR pointer!\n");
183                 continue;
184             }
185
186             ptr = 0;
187             if (cf_get_CIS(slot, CF_CISTPL_CFTABLE_ENTRY, buf, &len, &ptr)) {
188                 if (cf_parse_cftable(buf, len, &cftable)) {
189                     cyg_uint8 tmp;
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);
198                     // Wait for card
199                     do {
200                         DP_IN(base, DP_ISR, tmp);
201                     } while (0 == (tmp & DP_ISR_RESET));
202
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];
213                         } else {
214                             // Coudn't find it in the CIS (attribute) data
215                             unsigned char prom[32];
216
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]);
232                             }
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];
240                             } else {
241                                 diag_printf("No valid ESA found in CIS! Hardwiring to 00:C0:1B:00:99:9E\n");
242                             }
243                         }
244                         for (i = 0;  i < 6;  i++) {
245                             dp->esa[i] = sc_lpe_addr[i];
246                         }
247                     }
248
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();
255                     return true;
256 #endif
257                 } else {
258                     diag_printf("Can't parse CIS\n");
259                     continue;
260                 }
261             } else {
262                 diag_printf("Can't fetch config info\n");
263                 continue;
264             }
265         } else if (slot->state == CF_SLOT_STATE_Removed) {
266             diag_printf("Compact Flash card removed!\n");
267         } else {
268             cyg_drv_dsr_unlock();
269             do_delay(50);  // FIXME!
270 #ifndef CYGPKG_KERNEL
271             if (tries == 0) diag_printf("... Waiting for network card: ");
272             diag_printf(".");
273             if (++tries == 10) {
274                 // 5 seconds have elapsed - give up
275                 return false;
276             }
277             cf_hwr_poll(slot);  // Check to see if card has been inserted
278 #endif
279             cyg_drv_dsr_lock();
280         }
281     }
282 }
283
284 bool 
285 cyg_sc_lpe_init(struct cyg_netdevtab_entry *tab)
286 {
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;
290
291     cf_init();  // Make sure Compact Flash subsystem is initialized
292     slot = dp->plf_priv = (void*)cf_get_slot(0);
293     dp->tab = tab;
294
295 #ifdef CYGPKG_KERNEL
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
302                       STACK_SIZE,                            // Size
303                       &sc_lpe_card_handler_thread_handle,    // Handle
304                       &sc_lpe_card_handler_thread_data       // Thread data structure
305             );
306     cyg_thread_resume(sc_lpe_card_handler_thread_handle);    // Start it
307
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);
311
312     return false;  // Device is not ready until inserted, powered up, etc.
313 #else
314     // Initialize card
315     return sc_lpe_card_handler((cyg_addrword_t)sc);
316 #endif
317 }
318
319 int
320 cyg_sc_lpe_int_vector(struct eth_drv_sc *sc)
321 {
322     dp83902a_priv_data_t *dp = (dp83902a_priv_data_t *)sc->driver_private;
323     struct cf_slot* slot = (struct cf_slot*)dp->plf_priv;
324
325     return slot->int_num;
326 }