vserver 1.9.3
[linux-2.6.git] / drivers / scsi / sym53c8xx_2 / sym_nvram.c
1 /*
2  * Device driver for the SYMBIOS/LSILOGIC 53C8XX and 53C1010 family 
3  * of PCI-SCSI IO processors.
4  *
5  * Copyright (C) 1999-2001  Gerard Roudier <groudier@free.fr>
6  *
7  * This driver is derived from the Linux sym53c8xx driver.
8  * Copyright (C) 1998-2000  Gerard Roudier
9  *
10  * The sym53c8xx driver is derived from the ncr53c8xx driver that had been 
11  * a port of the FreeBSD ncr driver to Linux-1.2.13.
12  *
13  * The original ncr driver has been written for 386bsd and FreeBSD by
14  *         Wolfgang Stanglmeier        <wolf@cologne.de>
15  *         Stefan Esser                <se@mi.Uni-Koeln.de>
16  * Copyright (C) 1994  Wolfgang Stanglmeier
17  *
18  * Other major contributions:
19  *
20  * NVRAM detection and reading.
21  * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
22  *
23  *-----------------------------------------------------------------------------
24  *
25  * Redistribution and use in source and binary forms, with or without
26  * modification, are permitted provided that the following conditions
27  * are met:
28  * 1. Redistributions of source code must retain the above copyright
29  *    notice, this list of conditions and the following disclaimer.
30  * 2. The name of the author may not be used to endorse or promote products
31  *    derived from this software without specific prior written permission.
32  *
33  * Where this Software is combined with software released under the terms of 
34  * the GNU Public License ("GPL") and the terms of the GPL would require the 
35  * combined work to also be released under the terms of the GPL, the terms
36  * and conditions of this License will apply in addition to those of the
37  * GPL with the exception of any terms or conditions of this License that
38  * conflict with, or are expressly prohibited by, the GPL.
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
44  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  */
52
53 #include "sym_glue.h"
54 #include "sym_nvram.h"
55
56 #ifdef  SYM_CONF_DEBUG_NVRAM
57 static u_char Tekram_boot_delay[7] = {3, 5, 10, 20, 30, 60, 120};
58 #endif
59
60 /*
61  *  Get host setup from NVRAM.
62  */
63 void sym_nvram_setup_host(struct sym_hcb *np, struct sym_nvram *nvram)
64 {
65         /*
66          *  Get parity checking, host ID, verbose mode 
67          *  and miscellaneous host flags from NVRAM.
68          */
69         switch (nvram->type) {
70         case SYM_SYMBIOS_NVRAM:
71                 if (!(nvram->data.Symbios.flags & SYMBIOS_PARITY_ENABLE))
72                         np->rv_scntl0  &= ~0x0a;
73                 np->myaddr = nvram->data.Symbios.host_id & 0x0f;
74                 if (nvram->data.Symbios.flags & SYMBIOS_VERBOSE_MSGS)
75                         np->verbose += 1;
76                 if (nvram->data.Symbios.flags1 & SYMBIOS_SCAN_HI_LO)
77                         np->usrflags |= SYM_SCAN_TARGETS_HILO;
78                 if (nvram->data.Symbios.flags2 & SYMBIOS_AVOID_BUS_RESET)
79                         np->usrflags |= SYM_AVOID_BUS_RESET;
80                 break;
81         case SYM_TEKRAM_NVRAM:
82                 np->myaddr = nvram->data.Tekram.host_id & 0x0f;
83                 break;
84         default:
85                 break;
86         }
87 }
88
89 /*
90  *  Get target set-up from Symbios format NVRAM.
91  */
92 static void
93 sym_Symbios_setup_target(struct sym_hcb *np, int target, Symbios_nvram *nvram)
94 {
95         struct sym_tcb *tp = &np->target[target];
96         Symbios_target *tn = &nvram->target[target];
97
98         tp->usrtags =
99                 (tn->flags & SYMBIOS_QUEUE_TAGS_ENABLED)? SYM_SETUP_MAX_TAG : 0;
100
101         if (!(tn->flags & SYMBIOS_DISCONNECT_ENABLE))
102                 tp->usrflags &= ~SYM_DISC_ENABLED;
103         if (!(tn->flags & SYMBIOS_SCAN_AT_BOOT_TIME))
104                 tp->usrflags |= SYM_SCAN_BOOT_DISABLED;
105         if (!(tn->flags & SYMBIOS_SCAN_LUNS))
106                 tp->usrflags |= SYM_SCAN_LUNS_DISABLED;
107 }
108
109 /*
110  *  Get target set-up from Tekram format NVRAM.
111  */
112 static void
113 sym_Tekram_setup_target(struct sym_hcb *np, int target, Tekram_nvram *nvram)
114 {
115         struct sym_tcb *tp = &np->target[target];
116         struct Tekram_target *tn = &nvram->target[target];
117
118         if (tn->flags & TEKRAM_TAGGED_COMMANDS) {
119                 tp->usrtags = 2 << nvram->max_tags_index;
120         }
121
122         if (tn->flags & TEKRAM_DISCONNECT_ENABLE)
123                 tp->usrflags |= SYM_DISC_ENABLED;
124  
125         /* If any device does not support parity, we will not use this option */
126         if (!(tn->flags & TEKRAM_PARITY_CHECK))
127                 np->rv_scntl0  &= ~0x0a; /* SCSI parity checking disabled */
128 }
129
130 /*
131  *  Get target setup from NVRAM.
132  */
133 void sym_nvram_setup_target(struct sym_hcb *np, int target, struct sym_nvram *nvp)
134 {
135         switch (nvp->type) {
136         case SYM_SYMBIOS_NVRAM:
137                 sym_Symbios_setup_target(np, target, &nvp->data.Symbios);
138                 break;
139         case SYM_TEKRAM_NVRAM:
140                 sym_Tekram_setup_target(np, target, &nvp->data.Tekram);
141                 break;
142         default:
143                 break;
144         }
145 }
146
147 #ifdef  SYM_CONF_DEBUG_NVRAM
148 /*
149  *  Dump Symbios format NVRAM for debugging purpose.
150  */
151 static void sym_display_Symbios_nvram(struct sym_device *np, Symbios_nvram *nvram)
152 {
153         int i;
154
155         /* display Symbios nvram host data */
156         printf("%s: HOST ID=%d%s%s%s%s%s%s\n",
157                 sym_name(np), nvram->host_id & 0x0f,
158                 (nvram->flags  & SYMBIOS_SCAM_ENABLE)   ? " SCAM"       :"",
159                 (nvram->flags  & SYMBIOS_PARITY_ENABLE) ? " PARITY"     :"",
160                 (nvram->flags  & SYMBIOS_VERBOSE_MSGS)  ? " VERBOSE"    :"", 
161                 (nvram->flags  & SYMBIOS_CHS_MAPPING)   ? " CHS_ALT"    :"", 
162                 (nvram->flags2 & SYMBIOS_AVOID_BUS_RESET)?" NO_RESET"   :"",
163                 (nvram->flags1 & SYMBIOS_SCAN_HI_LO)    ? " HI_LO"      :"");
164
165         /* display Symbios nvram drive data */
166         for (i = 0 ; i < 15 ; i++) {
167                 struct Symbios_target *tn = &nvram->target[i];
168                 printf("%s-%d:%s%s%s%s WIDTH=%d SYNC=%d TMO=%d\n",
169                 sym_name(np), i,
170                 (tn->flags & SYMBIOS_DISCONNECT_ENABLE) ? " DISC"       : "",
171                 (tn->flags & SYMBIOS_SCAN_AT_BOOT_TIME) ? " SCAN_BOOT"  : "",
172                 (tn->flags & SYMBIOS_SCAN_LUNS)         ? " SCAN_LUNS"  : "",
173                 (tn->flags & SYMBIOS_QUEUE_TAGS_ENABLED)? " TCQ"        : "",
174                 tn->bus_width,
175                 tn->sync_period / 4,
176                 tn->timeout);
177         }
178 }
179
180 /*
181  *  Dump TEKRAM format NVRAM for debugging purpose.
182  */
183 static void sym_display_Tekram_nvram(struct sym_device *np, Tekram_nvram *nvram)
184 {
185         int i, tags, boot_delay;
186         char *rem;
187
188         /* display Tekram nvram host data */
189         tags = 2 << nvram->max_tags_index;
190         boot_delay = 0;
191         if (nvram->boot_delay_index < 6)
192                 boot_delay = Tekram_boot_delay[nvram->boot_delay_index];
193         switch ((nvram->flags & TEKRAM_REMOVABLE_FLAGS) >> 6) {
194         default:
195         case 0: rem = "";                       break;
196         case 1: rem = " REMOVABLE=boot device"; break;
197         case 2: rem = " REMOVABLE=all";         break;
198         }
199
200         printf("%s: HOST ID=%d%s%s%s%s%s%s%s%s%s BOOT DELAY=%d tags=%d\n",
201                 sym_name(np), nvram->host_id & 0x0f,
202                 (nvram->flags1 & SYMBIOS_SCAM_ENABLE)   ? " SCAM"       :"",
203                 (nvram->flags & TEKRAM_MORE_THAN_2_DRIVES) ? " >2DRIVES":"",
204                 (nvram->flags & TEKRAM_DRIVES_SUP_1GB)  ? " >1GB"       :"",
205                 (nvram->flags & TEKRAM_RESET_ON_POWER_ON) ? " RESET"    :"",
206                 (nvram->flags & TEKRAM_ACTIVE_NEGATION) ? " ACT_NEG"    :"",
207                 (nvram->flags & TEKRAM_IMMEDIATE_SEEK)  ? " IMM_SEEK"   :"",
208                 (nvram->flags & TEKRAM_SCAN_LUNS)       ? " SCAN_LUNS"  :"",
209                 (nvram->flags1 & TEKRAM_F2_F6_ENABLED)  ? " F2_F6"      :"",
210                 rem, boot_delay, tags);
211
212         /* display Tekram nvram drive data */
213         for (i = 0; i <= 15; i++) {
214                 int sync, j;
215                 struct Tekram_target *tn = &nvram->target[i];
216                 j = tn->sync_index & 0xf;
217                 sync = Tekram_sync[j];
218                 printf("%s-%d:%s%s%s%s%s%s PERIOD=%d\n",
219                 sym_name(np), i,
220                 (tn->flags & TEKRAM_PARITY_CHECK)       ? " PARITY"     : "",
221                 (tn->flags & TEKRAM_SYNC_NEGO)          ? " SYNC"       : "",
222                 (tn->flags & TEKRAM_DISCONNECT_ENABLE)  ? " DISC"       : "",
223                 (tn->flags & TEKRAM_START_CMD)          ? " START"      : "",
224                 (tn->flags & TEKRAM_TAGGED_COMMANDS)    ? " TCQ"        : "",
225                 (tn->flags & TEKRAM_WIDE_NEGO)          ? " WIDE"       : "",
226                 sync);
227         }
228 }
229 #else
230 static void sym_display_Symbios_nvram(struct sym_device *np, Symbios_nvram *nvram) { (void)np; (void)nvram; }
231 static void sym_display_Tekram_nvram(struct sym_device *np, Tekram_nvram *nvram) { (void)np; (void)nvram; }
232 #endif  /* SYM_CONF_DEBUG_NVRAM */
233
234
235 /*
236  *  24C16 EEPROM reading.
237  *
238  *  GPOI0 - data in/data out
239  *  GPIO1 - clock
240  *  Symbios NVRAM wiring now also used by Tekram.
241  */
242
243 #define SET_BIT 0
244 #define CLR_BIT 1
245 #define SET_CLK 2
246 #define CLR_CLK 3
247
248 /*
249  *  Set/clear data/clock bit in GPIO0
250  */
251 static void S24C16_set_bit(struct sym_device *np, u_char write_bit, u_char *gpreg, 
252                           int bit_mode)
253 {
254         UDELAY (5);
255         switch (bit_mode) {
256         case SET_BIT:
257                 *gpreg |= write_bit;
258                 break;
259         case CLR_BIT:
260                 *gpreg &= 0xfe;
261                 break;
262         case SET_CLK:
263                 *gpreg |= 0x02;
264                 break;
265         case CLR_CLK:
266                 *gpreg &= 0xfd;
267                 break;
268
269         }
270         OUTB (nc_gpreg, *gpreg);
271         UDELAY (5);
272 }
273
274 /*
275  *  Send START condition to NVRAM to wake it up.
276  */
277 static void S24C16_start(struct sym_device *np, u_char *gpreg)
278 {
279         S24C16_set_bit(np, 1, gpreg, SET_BIT);
280         S24C16_set_bit(np, 0, gpreg, SET_CLK);
281         S24C16_set_bit(np, 0, gpreg, CLR_BIT);
282         S24C16_set_bit(np, 0, gpreg, CLR_CLK);
283 }
284
285 /*
286  *  Send STOP condition to NVRAM - puts NVRAM to sleep... ZZzzzz!!
287  */
288 static void S24C16_stop(struct sym_device *np, u_char *gpreg)
289 {
290         S24C16_set_bit(np, 0, gpreg, SET_CLK);
291         S24C16_set_bit(np, 1, gpreg, SET_BIT);
292 }
293
294 /*
295  *  Read or write a bit to the NVRAM,
296  *  read if GPIO0 input else write if GPIO0 output
297  */
298 static void S24C16_do_bit(struct sym_device *np, u_char *read_bit, u_char write_bit, 
299                          u_char *gpreg)
300 {
301         S24C16_set_bit(np, write_bit, gpreg, SET_BIT);
302         S24C16_set_bit(np, 0, gpreg, SET_CLK);
303         if (read_bit)
304                 *read_bit = INB (nc_gpreg);
305         S24C16_set_bit(np, 0, gpreg, CLR_CLK);
306         S24C16_set_bit(np, 0, gpreg, CLR_BIT);
307 }
308
309 /*
310  *  Output an ACK to the NVRAM after reading,
311  *  change GPIO0 to output and when done back to an input
312  */
313 static void S24C16_write_ack(struct sym_device *np, u_char write_bit, u_char *gpreg, 
314                             u_char *gpcntl)
315 {
316         OUTB (nc_gpcntl, *gpcntl & 0xfe);
317         S24C16_do_bit(np, NULL, write_bit, gpreg);
318         OUTB (nc_gpcntl, *gpcntl);
319 }
320
321 /*
322  *  Input an ACK from NVRAM after writing,
323  *  change GPIO0 to input and when done back to an output
324  */
325 static void S24C16_read_ack(struct sym_device *np, u_char *read_bit, u_char *gpreg, 
326                            u_char *gpcntl)
327 {
328         OUTB (nc_gpcntl, *gpcntl | 0x01);
329         S24C16_do_bit(np, read_bit, 1, gpreg);
330         OUTB (nc_gpcntl, *gpcntl);
331 }
332
333 /*
334  *  WRITE a byte to the NVRAM and then get an ACK to see it was accepted OK,
335  *  GPIO0 must already be set as an output
336  */
337 static void S24C16_write_byte(struct sym_device *np, u_char *ack_data, u_char write_data, 
338                              u_char *gpreg, u_char *gpcntl)
339 {
340         int x;
341         
342         for (x = 0; x < 8; x++)
343                 S24C16_do_bit(np, NULL, (write_data >> (7 - x)) & 0x01, gpreg);
344                 
345         S24C16_read_ack(np, ack_data, gpreg, gpcntl);
346 }
347
348 /*
349  *  READ a byte from the NVRAM and then send an ACK to say we have got it,
350  *  GPIO0 must already be set as an input
351  */
352 static void S24C16_read_byte(struct sym_device *np, u_char *read_data, u_char ack_data, 
353                             u_char *gpreg, u_char *gpcntl)
354 {
355         int x;
356         u_char read_bit;
357
358         *read_data = 0;
359         for (x = 0; x < 8; x++) {
360                 S24C16_do_bit(np, &read_bit, 1, gpreg);
361                 *read_data |= ((read_bit & 0x01) << (7 - x));
362         }
363
364         S24C16_write_ack(np, ack_data, gpreg, gpcntl);
365 }
366
367 #if SYM_CONF_NVRAM_WRITE_SUPPORT
368 /*
369  *  Write 'len' bytes starting at 'offset'.
370  */
371 static int sym_write_S24C16_nvram(struct sym_device *np, int offset,
372                 u_char *data, int len)
373 {
374         u_char  gpcntl, gpreg;
375         u_char  old_gpcntl, old_gpreg;
376         u_char  ack_data;
377         int     x;
378
379         /* save current state of GPCNTL and GPREG */
380         old_gpreg       = INB (nc_gpreg);
381         old_gpcntl      = INB (nc_gpcntl);
382         gpcntl          = old_gpcntl & 0x1c;
383
384         /* set up GPREG & GPCNTL to set GPIO0 and GPIO1 in to known state */
385         OUTB (nc_gpreg,  old_gpreg);
386         OUTB (nc_gpcntl, gpcntl);
387
388         /* this is to set NVRAM into a known state with GPIO0/1 both low */
389         gpreg = old_gpreg;
390         S24C16_set_bit(np, 0, &gpreg, CLR_CLK);
391         S24C16_set_bit(np, 0, &gpreg, CLR_BIT);
392                 
393         /* now set NVRAM inactive with GPIO0/1 both high */
394         S24C16_stop(np, &gpreg);
395
396         /* NVRAM has to be written in segments of 16 bytes */
397         for (x = 0; x < len ; x += 16) {
398                 do {
399                         S24C16_start(np, &gpreg);
400                         S24C16_write_byte(np, &ack_data,
401                                           0xa0 | (((offset+x) >> 7) & 0x0e),
402                                           &gpreg, &gpcntl);
403                 } while (ack_data & 0x01);
404
405                 S24C16_write_byte(np, &ack_data, (offset+x) & 0xff, 
406                                   &gpreg, &gpcntl);
407
408                 for (y = 0; y < 16; y++)
409                         S24C16_write_byte(np, &ack_data, data[x+y], 
410                                           &gpreg, &gpcntl);
411                 S24C16_stop(np, &gpreg);
412         }
413
414         /* return GPIO0/1 to original states after having accessed NVRAM */
415         OUTB (nc_gpcntl, old_gpcntl);
416         OUTB (nc_gpreg,  old_gpreg);
417
418         return 0;
419 }
420 #endif /* SYM_CONF_NVRAM_WRITE_SUPPORT */
421
422 /*
423  *  Read 'len' bytes starting at 'offset'.
424  */
425 static int sym_read_S24C16_nvram(struct sym_device *np, int offset, u_char *data, int len)
426 {
427         u_char  gpcntl, gpreg;
428         u_char  old_gpcntl, old_gpreg;
429         u_char  ack_data;
430         int     retv = 1;
431         int     x;
432
433         /* save current state of GPCNTL and GPREG */
434         old_gpreg       = INB (nc_gpreg);
435         old_gpcntl      = INB (nc_gpcntl);
436         gpcntl          = old_gpcntl & 0x1c;
437
438         /* set up GPREG & GPCNTL to set GPIO0 and GPIO1 in to known state */
439         OUTB (nc_gpreg,  old_gpreg);
440         OUTB (nc_gpcntl, gpcntl);
441
442         /* this is to set NVRAM into a known state with GPIO0/1 both low */
443         gpreg = old_gpreg;
444         S24C16_set_bit(np, 0, &gpreg, CLR_CLK);
445         S24C16_set_bit(np, 0, &gpreg, CLR_BIT);
446                 
447         /* now set NVRAM inactive with GPIO0/1 both high */
448         S24C16_stop(np, &gpreg);
449         
450         /* activate NVRAM */
451         S24C16_start(np, &gpreg);
452
453         /* write device code and random address MSB */
454         S24C16_write_byte(np, &ack_data,
455                 0xa0 | ((offset >> 7) & 0x0e), &gpreg, &gpcntl);
456         if (ack_data & 0x01)
457                 goto out;
458
459         /* write random address LSB */
460         S24C16_write_byte(np, &ack_data,
461                 offset & 0xff, &gpreg, &gpcntl);
462         if (ack_data & 0x01)
463                 goto out;
464
465         /* regenerate START state to set up for reading */
466         S24C16_start(np, &gpreg);
467         
468         /* rewrite device code and address MSB with read bit set (lsb = 0x01) */
469         S24C16_write_byte(np, &ack_data,
470                 0xa1 | ((offset >> 7) & 0x0e), &gpreg, &gpcntl);
471         if (ack_data & 0x01)
472                 goto out;
473
474         /* now set up GPIO0 for inputting data */
475         gpcntl |= 0x01;
476         OUTB (nc_gpcntl, gpcntl);
477                 
478         /* input all requested data - only part of total NVRAM */
479         for (x = 0; x < len; x++) 
480                 S24C16_read_byte(np, &data[x], (x == (len-1)), &gpreg, &gpcntl);
481
482         /* finally put NVRAM back in inactive mode */
483         gpcntl &= 0xfe;
484         OUTB (nc_gpcntl, gpcntl);
485         S24C16_stop(np, &gpreg);
486         retv = 0;
487 out:
488         /* return GPIO0/1 to original states after having accessed NVRAM */
489         OUTB (nc_gpcntl, old_gpcntl);
490         OUTB (nc_gpreg,  old_gpreg);
491
492         return retv;
493 }
494
495 #undef SET_BIT
496 #undef CLR_BIT
497 #undef SET_CLK
498 #undef CLR_CLK
499
500 /*
501  *  Try reading Symbios NVRAM.
502  *  Return 0 if OK.
503  */
504 static int sym_read_Symbios_nvram(struct sym_device *np, Symbios_nvram *nvram)
505 {
506         static u_char Symbios_trailer[6] = {0xfe, 0xfe, 0, 0, 0, 0};
507         u_char *data = (u_char *) nvram;
508         int len  = sizeof(*nvram);
509         u_short csum;
510         int x;
511
512         /* probe the 24c16 and read the SYMBIOS 24c16 area */
513         if (sym_read_S24C16_nvram (np, SYMBIOS_NVRAM_ADDRESS, data, len))
514                 return 1;
515
516         /* check valid NVRAM signature, verify byte count and checksum */
517         if (nvram->type != 0 ||
518             memcmp(nvram->trailer, Symbios_trailer, 6) ||
519             nvram->byte_count != len - 12)
520                 return 1;
521
522         /* verify checksum */
523         for (x = 6, csum = 0; x < len - 6; x++)
524                 csum += data[x];
525         if (csum != nvram->checksum)
526                 return 1;
527
528         return 0;
529 }
530
531 /*
532  *  93C46 EEPROM reading.
533  *
534  *  GPOI0 - data in
535  *  GPIO1 - data out
536  *  GPIO2 - clock
537  *  GPIO4 - chip select
538  *
539  *  Used by Tekram.
540  */
541
542 /*
543  *  Pulse clock bit in GPIO0
544  */
545 static void T93C46_Clk(struct sym_device *np, u_char *gpreg)
546 {
547         OUTB (nc_gpreg, *gpreg | 0x04);
548         UDELAY (2);
549         OUTB (nc_gpreg, *gpreg);
550 }
551
552 /* 
553  *  Read bit from NVRAM
554  */
555 static void T93C46_Read_Bit(struct sym_device *np, u_char *read_bit, u_char *gpreg)
556 {
557         UDELAY (2);
558         T93C46_Clk(np, gpreg);
559         *read_bit = INB (nc_gpreg);
560 }
561
562 /*
563  *  Write bit to GPIO0
564  */
565 static void T93C46_Write_Bit(struct sym_device *np, u_char write_bit, u_char *gpreg)
566 {
567         if (write_bit & 0x01)
568                 *gpreg |= 0x02;
569         else
570                 *gpreg &= 0xfd;
571                 
572         *gpreg |= 0x10;
573                 
574         OUTB (nc_gpreg, *gpreg);
575         UDELAY (2);
576
577         T93C46_Clk(np, gpreg);
578 }
579
580 /*
581  *  Send STOP condition to NVRAM - puts NVRAM to sleep... ZZZzzz!!
582  */
583 static void T93C46_Stop(struct sym_device *np, u_char *gpreg)
584 {
585         *gpreg &= 0xef;
586         OUTB (nc_gpreg, *gpreg);
587         UDELAY (2);
588
589         T93C46_Clk(np, gpreg);
590 }
591
592 /*
593  *  Send read command and address to NVRAM
594  */
595 static void T93C46_Send_Command(struct sym_device *np, u_short write_data, 
596                                 u_char *read_bit, u_char *gpreg)
597 {
598         int x;
599
600         /* send 9 bits, start bit (1), command (2), address (6)  */
601         for (x = 0; x < 9; x++)
602                 T93C46_Write_Bit(np, (u_char) (write_data >> (8 - x)), gpreg);
603
604         *read_bit = INB (nc_gpreg);
605 }
606
607 /*
608  *  READ 2 bytes from the NVRAM
609  */
610 static void T93C46_Read_Word(struct sym_device *np,
611                 unsigned short *nvram_data, unsigned char *gpreg)
612 {
613         int x;
614         u_char read_bit;
615
616         *nvram_data = 0;
617         for (x = 0; x < 16; x++) {
618                 T93C46_Read_Bit(np, &read_bit, gpreg);
619
620                 if (read_bit & 0x01)
621                         *nvram_data |=  (0x01 << (15 - x));
622                 else
623                         *nvram_data &= ~(0x01 << (15 - x));
624         }
625 }
626
627 /*
628  *  Read Tekram NvRAM data.
629  */
630 static int T93C46_Read_Data(struct sym_device *np, unsigned short *data,
631                 int len, unsigned char *gpreg)
632 {
633         int x;
634
635         for (x = 0; x < len; x++)  {
636                 unsigned char read_bit;
637                 /* output read command and address */
638                 T93C46_Send_Command(np, 0x180 | x, &read_bit, gpreg);
639                 if (read_bit & 0x01)
640                         return 1; /* Bad */
641                 T93C46_Read_Word(np, &data[x], gpreg);
642                 T93C46_Stop(np, gpreg);
643         }
644
645         return 0;
646 }
647
648 /*
649  *  Try reading 93C46 Tekram NVRAM.
650  */
651 static int sym_read_T93C46_nvram(struct sym_device *np, Tekram_nvram *nvram)
652 {
653         u_char gpcntl, gpreg;
654         u_char old_gpcntl, old_gpreg;
655         int retv = 1;
656
657         /* save current state of GPCNTL and GPREG */
658         old_gpreg       = INB (nc_gpreg);
659         old_gpcntl      = INB (nc_gpcntl);
660
661         /* set up GPREG & GPCNTL to set GPIO0/1/2/4 in to known state, 0 in,
662            1/2/4 out */
663         gpreg = old_gpreg & 0xe9;
664         OUTB (nc_gpreg, gpreg);
665         gpcntl = (old_gpcntl & 0xe9) | 0x09;
666         OUTB (nc_gpcntl, gpcntl);
667
668         /* input all of NVRAM, 64 words */
669         retv = T93C46_Read_Data(np, (u_short *) nvram,
670                                 sizeof(*nvram) / sizeof(short), &gpreg);
671         
672         /* return GPIO0/1/2/4 to original states after having accessed NVRAM */
673         OUTB (nc_gpcntl, old_gpcntl);
674         OUTB (nc_gpreg,  old_gpreg);
675
676         return retv;
677 }
678
679 /*
680  *  Try reading Tekram NVRAM.
681  *  Return 0 if OK.
682  */
683 static int sym_read_Tekram_nvram (struct sym_device *np, Tekram_nvram *nvram)
684 {
685         u_char *data = (u_char *) nvram;
686         int len = sizeof(*nvram);
687         u_short csum;
688         int x;
689
690         switch (np->device_id) {
691         case PCI_DEVICE_ID_NCR_53C885:
692         case PCI_DEVICE_ID_NCR_53C895:
693         case PCI_DEVICE_ID_NCR_53C896:
694                 x = sym_read_S24C16_nvram(np, TEKRAM_24C16_NVRAM_ADDRESS,
695                                           data, len);
696                 break;
697         case PCI_DEVICE_ID_NCR_53C875:
698                 x = sym_read_S24C16_nvram(np, TEKRAM_24C16_NVRAM_ADDRESS,
699                                           data, len);
700                 if (!x)
701                         break;
702         default:
703                 x = sym_read_T93C46_nvram(np, nvram);
704                 break;
705         }
706         if (x)
707                 return 1;
708
709         /* verify checksum */
710         for (x = 0, csum = 0; x < len - 1; x += 2)
711                 csum += data[x] + (data[x+1] << 8);
712         if (csum != 0x1234)
713                 return 1;
714
715         return 0;
716 }
717
718 /*
719  *  Try reading Symbios or Tekram NVRAM
720  */
721 int sym_read_nvram(struct sym_device *np, struct sym_nvram *nvp)
722 {
723         if (!sym_read_Symbios_nvram(np, &nvp->data.Symbios)) {
724                 nvp->type = SYM_SYMBIOS_NVRAM;
725                 sym_display_Symbios_nvram(np, &nvp->data.Symbios);
726         } else if (!sym_read_Tekram_nvram(np, &nvp->data.Tekram)) {
727                 nvp->type = SYM_TEKRAM_NVRAM;
728                 sym_display_Tekram_nvram(np, &nvp->data.Tekram);
729         } else {
730                 nvp->type = 0;
731         }
732         return nvp->type;
733 }