Add untested support for UNDI transmit and receive.
[people/sha0/gpxe.git] / src / core / pcmcia.c
1 /*
2  *      pcmcia.c
3  *
4  *      PCMCIA support routines for etherboot - generic stuff
5  *
6  *      This code has partly be taken from the linux kernel sources, .../drivers/pcmcia/
7  *      Started & put together by
8  *              Anselm Martin Hoffmeister
9  *              Stockholm Projekt Computer-Service
10  *              Sankt Augustin / Bonn, Germany
11  *
12  *      Distributed under GPL2
13  */
14
15 /*
16  *
17  *
18  *                      ******************************
19  *                      PLEASE DO NOT YET WORK ON THIS
20  *                      ******************************
21  *
22  *      I'm still fixing it up on every end, so we most probably would interfere
23  *      at some point. If there's anything obvious or better, not-so-obvious,
24  *      please contact me by e-mail: anselm (AT) hoffmeister (DOT) be   *THANKS*
25  */
26 #include "pcmcia.h"
27 #include "i82365.h"
28 #define CODE_STATUS "alpha"
29 #define CODE_VERSION "0.1.3"
30 #include "pcmcia-opts.h"
31 #include "console.h"
32 #include <gpxe/init.h>
33
34 int     sockets; /* AHTODO: Phase this out! */
35 u_int   pccsocks;
36 struct  pccsock_t pccsock[MAXPCCSOCKS];
37 int     inited = -1;
38 struct  pcc_config_t pccconfig[MAXPCCCONFIGS];
39
40 struct  driver_interact_t driver[] = {
41 #ifdef  SUPPORT_I82365
42         { I82365, i82365_interfacer, "Intel_82365" },
43 #endif
44 };
45
46 #define NUM_DRIVERS (sizeof(driver)/(sizeof(struct driver_interact_t)))
47
48 void    sleepticks(int numticks ) {
49         u_int   tmo;
50         for (tmo = currticks()+numticks; currticks() < tmo; ) {
51         }
52         return;
53 }
54
55 static void pcmcia_init_all(void) {
56         u_int i, j, k, l, m, n, ui, configs = 0;
57         u_int multicard[8];
58         u_char  *uc, upc;
59         if ( PDEBUG > 0 ) printf("Initializing PCMCIA subsystem (code-status: " CODE_STATUS ", Version " CODE_VERSION ")\n");
60         if ( PDEBUG > 2 ) {
61                 printf ( "Supporting %d driver(s): ", NUM_DRIVERS );
62                 for ( i = 0; i < NUM_DRIVERS; ++i ) {
63                         printf ( "[%s] ", driver[i].name );
64                 }
65                 printf ( "\n" );
66         }
67         pccsocks = 0;
68         sockets = 0;
69         // Init all drivers in the driver[] array:
70         for ( i = 0; i < NUM_DRIVERS; ++i ) {
71                 driver[i].f(INIT,0,i,0,0);      // init needs no params. It uses pccsocks and pccsock[].
72                                                 // Only i tells it which driver_id itself is.
73         }
74         for ( i = 0; i < pccsocks; ++i ) {
75                 printf ( "Socket %d: ", i );
76                 if ( pccsock[i].status != HASCARD ) {
77                         printf ( "is %s: skipping\n", pccsock[i].status == EMPTY? "empty":"[status unknown]" );
78                         continue;
79                 }
80                 if ( 0 != driver[pccsock[i].drivernum].f(MAPATTRMEM,pccsock[i].internalid,MAP_ATTRMEM_TO, MAP_ATTRMEM_LEN,0 ) ) {
81                         printf ("PCMCIA controller failed to map attribute memory.\n**** SEVERE ERROR CONDITION. Skipping controller.\n" );
82                         if ( PDEBUG > 2 ) {
83                                 printf ( "<press key. THIS CONDITION SHOULD BE REPORTED!>\n" ); getchar();
84                         }
85                         continue;
86                 }
87                 // parse configuration information
88                 uc = ioremap ( MAP_ATTRMEM_TO, MAP_ATTRMEM_LEN );
89                 pccsock[i].stringoffset = pccsock[i].configoffset = pccsock[i].stringlength = 0;
90                 pccsock[i].type = 0xff;
91                 for ( l = 0; l < 8; ++l ) multicard[l] = 0;
92                 sleepticks(2);
93                 for ( l = ui = 0; ui < 0x800; ui += uc[(2*ui)+2] + 2 ) {
94                         if ( uc[(2*ui)] == 0xff ) {
95                                 break;
96                         }
97                         // This loop is complete rubbish AFAICS.
98                         // But without it, my test system won't come up.
99                         // It's too bad to develop on broken hardware
100                         //                              - Anselm
101                 }
102                 sleepticks(2);
103                 configs = 0;
104                 inited = -1;
105                 for ( l = ui = 0; ui < 0x800; ui += uc[(2*ui)+2] + 2 ) {
106                         if ( uc[(2*ui)] == 0xff ) break;
107                         else if ( uc[2*ui] == 0x15 ) {
108                                 for ( k = 2 * ( ui + 2 ); ( uc[k] <= ' ' ) && ( k < ( 2 * ( uc[2*(ui+1)] + ui + 2 ) ) ) ; k += 2 ) { ; }
109                                 pccsock[i].stringoffset = k;
110                                 pccsock[i].stringlength = ( 2 * ( ui + 2 + uc[(2*ui)+2] ) - k ) / 2;
111                         } else if ( uc[2*ui] == 0x21 ) {
112                                 pccsock[i].type = uc[(2*ui)+4];
113                         } else if ( uc[2*ui] == 0x1a ) { // Configuration map
114                                 printf ( "\nConfig map 0x1a found [" );
115                                 for ( k = 0; k < uc[2*(ui+1)]; ++k ) {
116                                         printf ( "%02x ", uc[2*(ui+k+2)] );
117                                 }
118                                 printf ( "]\nHighest config available is %d\n", uc[2*(ui+3)] );
119                                 m = uc[2*(ui+2)];
120                                 pccsock[i].configoffset = 0;
121                                 for ( j = 0; j <= m & 3; ++j ) {
122                                         pccsock[i].configoffset += uc[2*(ui+4+j)] << (8*j);
123                                 }
124                                 pccsock[i].rmask0 = 0;
125                                 for ( j = 0; j <= ( ( ( m & 0x3c ) >> 2 ) & 3 ); ++j ) {
126                                         pccsock[i].rmask0 += uc[2*(ui+5+(m&3)+j)] << (8*j);
127                                 }
128                                 j = pccsock[i].rmask0;
129                                 printf ( "Config offset is %x, card has regs: < %s%s%s%s%s>\n", pccsock[i].configoffset,
130                                         j & 1 ? "COR ":"", j & 2 ? "CCSR ":"", j & 4 ? "PRR ":"", j & 8 ? "SCR ":"", j & 16? "ESR ":"" );
131                                 printf ( "COR + CCSR contents (si/du) %x %x/%x %x\n", uc[pccsock[i].configoffset+0],
132                                         uc[pccsock[i].configoffset+2],uc[pccsock[i].configoffset*2],uc[(pccsock[i].configoffset*2)+2] );
133                                 printf ( "          " );
134                         } else if ( uc[2*ui] == 0x1b ) { // Configuration data entry
135                                 //printf ( "Config data 0x1b found [\n" );getchar();
136                                 for ( k = 0; k < uc[2*(ui+1)]; ++k ) {
137                                 //      printf ( "%02x ", uc[2*(ui+k+2)] );
138                                 }
139                                 // Parse this tuple into pccconfig[configs]
140                                 // printf ( "]\n" );
141                                 if ( configs == MAXPCCCONFIGS ) continue;
142                                 k = 2*ui+4;
143                                 pccconfig[configs].index = uc[k] & 0x3f;
144                                 if ( uc[k] & 0x80 ) {
145                                 //      printf ( "Special config, unsupp. for now\n" );
146                                         continue;
147                                 }
148                                 k+=2;
149                                 // printf ( "Features: %2x\n", uc[k] );
150                                 if ( uc[k] & 0x7 ) {
151                                         // printf ( "Cannot work with Vcc/Timing configs right now\n" );
152                                         continue;
153                                 }
154                                 pccconfig[configs].iowin = pccconfig[configs].iolen = 0;
155                                 if ( 0 != ( uc[k] & 0x8 ) ) {
156                                         k+=2;
157                                         // printf ( "Reading IO config: " );
158                                         if ( 0 == ( uc[k] & 0x80 ) ) {
159                                         //      printf ( "Cannot work with auto/io config\n" );
160                                                 continue;
161                                         }
162                                         k+=2;
163                                         if ( 0 != ( uc[k] & 0x0f ) ) {
164                                         //      printf ( "Don't support more than 1 iowin right now\n" );
165                                                 continue;
166                                         }
167                                         j = (uc[k] & 0x30) >> 4;
168                                         m = (uc[k] & 0xc0) >> 6;
169                                         if ( 3 == j ) ++j;
170                                         if ( 3 == m ) ++m;
171                                         k += 2;
172                                         pccconfig[configs].iowin = 0;
173                                         pccconfig[configs].iolen = 1;
174                                         for ( n = 0; n < j; ++n, k+=2 ) {
175                                                 pccconfig[configs].iowin += uc[k] << (n*8);
176                                         }
177                                         for ( n = 0; n < m; ++n, k+=2 ) {
178                                                 pccconfig[configs].iolen += uc[k] << (n*8);
179                                         }
180                                         // printf ( "io %x len %d (%d)\n", pccconfig[configs].iowin, pccconfig[configs].iolen,configs );
181                                 }
182                                 for ( j = 0; j < (uc[k] & 3); ++j ) {
183                                 //      pccconfig[configs].iowin += (uc[k+(2*j)+2]) << (8*j);
184                                 }
185                                 ++configs;
186                         }
187                 }
188                 if ( pccsock[i].stringoffset > 0 ) {    // If no identifier, it's not a valid CIS (as of documentation...)
189                         printf ( "[" );
190                         for ( k = 0; ( k <  pccsock[i].stringlength ) && ( k < 64 ); ++k ) {
191                                 j = uc[pccsock[i].stringoffset + 2 * k];
192                                 printf ( "%c", (j>=' '? j:' ' ) );
193                         }
194                         printf ("]\n          is type %d (", pccsock[i].type );
195                         switch ( pccsock[i].type ) {
196                           case  0x00:
197                                 printf ( "MULTI" ); break;
198                           case  0x01:
199                                 printf ( "Memory" ); break;
200                           case  0x02:
201                                 printf ( "Serial" ); break;
202                           case  0x03:
203                                 printf ( "Parallel" ); break;
204                           case  0x04:
205                                 printf ( "Fixed" ); break;
206                           case  0x05:
207                                 printf ( "Video" ); break;
208                           case  0x06:
209                                 printf ( "Network" ); break;
210                           case  0x07:
211                                 printf ( "AIMS" ); break;
212                           case  0x08:
213                                 printf ( "SCSI" ); break;
214                           case  0x106: // Special / homebrew to say "Multi/network"
215                                 printf ( "MULTI, with Network" ); break; // AHTODO find a card for this
216                           default:
217                                 printf ( "UNSUPPORTED/UNKNOWN" );
218                         }
219                         printf ( ") with %d possible configuration(s)\n", configs );
220                         // Now set dependency: If it's Network or multi->network, accept
221                         if ( (inited <= 0 ) && (6 == (0xff & pccsock[i].type) ) && (0 < configs ) ) {
222                                 printf ( "activating this device with ioport %x-%x (config #%d)\n", 
223                                 pccconfig[0].iowin, pccconfig[0].iowin+pccconfig[0].iolen-1, pccconfig[0].index );
224                                 inited = i;
225                                 // And unmap attrmem ourselves!
226                                 printf ( "Activating config..." );
227                                 if ( m=driver[pccsock[i].drivernum].f(SELECTCONFIG,pccsock[i].internalid,pccconfig[0].index,0,&pccconfig[0]) ) {
228                                         printf ("Failure(%d)!",m); inited = -1;
229                                         driver[pccsock[i].drivernum].f(UNMAPATTRMEM,pccsock[i].internalid,0,0,0);
230                                 }
231                                 printf ( "done!\n" );
232                                 continue;
233                         }
234                 } else {
235                         printf ( "unsupported - no identifier string found in CIS\n" );
236                 }
237                 // unmap the PCMCIA device
238                 if ( i != inited ) {
239                     if ( 0 != driver[pccsock[i].drivernum].f(UNMAPATTRMEM,pccsock[i].internalid,0,0,0) ) {
240                         printf ("PCMCIA controller failed to unmap attribute memory.\n**** SEVERE ERROR CONDITION ****\n" );
241                         if ( PDEBUG > 2 ) {
242                                 printf ( "<press key. THIS CONDITION SHOULD BE REPORTED!>\n" ); getchar();
243                         }
244                         continue;
245                     }
246                 }
247         }
248         if ( PDEBUG > 2 ) {
249                 printf ( "<press key to exit the pcmcia_init_all routine>\n" );
250                 getchar();
251         }
252
253 }
254
255 static void     pcmcia_shutdown_all(void) {
256         int i;
257         //if ( PDEBUG > 2 ) {printf("<press key to continue>\n" ); getchar(); }
258         for ( i = 0; i < pccsocks; ++i ) {
259                 driver[pccsock[i].drivernum].f(SHUTDOWN,pccsock[i].internalid,0,0,0);
260         }
261         printf("Shutdown of PCMCIA subsystem completed");
262 }
263
264 INIT_FN ( INIT_PCMCIA, pcmcia_init_all, NULL, pcmcia_shutdown_all );