Merge master.kernel.org:/pub/scm/linux/kernel/git/airlied/drm-2.6
[pandora-kernel.git] / drivers / char / nvram.c
1 /*
2  * CMOS/NV-RAM driver for Linux
3  *
4  * Copyright (C) 1997 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
5  * idea by and with help from Richard Jelinek <rj@suse.de>
6  * Portions copyright (c) 2001,2002 Sun Microsystems (thockin@sun.com)
7  *
8  * This driver allows you to access the contents of the non-volatile memory in
9  * the mc146818rtc.h real-time clock. This chip is built into all PCs and into
10  * many Atari machines. In the former it's called "CMOS-RAM", in the latter
11  * "NVRAM" (NV stands for non-volatile).
12  *
13  * The data are supplied as a (seekable) character device, /dev/nvram. The
14  * size of this file is dependent on the controller.  The usual size is 114,
15  * the number of freely available bytes in the memory (i.e., not used by the
16  * RTC itself).
17  *
18  * Checksums over the NVRAM contents are managed by this driver. In case of a
19  * bad checksum, reads and writes return -EIO. The checksum can be initialized
20  * to a sane state either by ioctl(NVRAM_INIT) (clear whole NVRAM) or
21  * ioctl(NVRAM_SETCKS) (doesn't change contents, just makes checksum valid
22  * again; use with care!)
23  *
24  * This file also provides some functions for other parts of the kernel that
25  * want to access the NVRAM: nvram_{read,write,check_checksum,set_checksum}.
26  * Obviously this can be used only if this driver is always configured into
27  * the kernel and is not a module. Since the functions are used by some Atari
28  * drivers, this is the case on the Atari.
29  *
30  *
31  *      1.1     Cesar Barros: SMP locking fixes
32  *              added changelog
33  *      1.2     Erik Gilling: Cobalt Networks support
34  *              Tim Hockin: general cleanup, Cobalt support
35  *      1.3     Jon Ringle: Comdial MP1000 support
36  *
37  */
38
39 #define NVRAM_VERSION   "1.3"
40
41 #include <linux/module.h>
42 #include <linux/config.h>
43 #include <linux/sched.h>
44 #include <linux/smp_lock.h>
45 #include <linux/nvram.h>
46
47 #define PC              1
48 #define ATARI           2
49 #define COBALT          3
50 #define MP1000          4
51
52 /* select machine configuration */
53 #if defined(CONFIG_ATARI)
54 #  define MACH ATARI
55 #elif defined(__i386__) || defined(__x86_64__) || defined(__arm__)  /* and others?? */
56 #define MACH PC
57 #  if defined(CONFIG_COBALT)
58 #    include <linux/cobalt-nvram.h>
59 #    define MACH COBALT
60 #  elif defined(CONFIG_MACH_MP1000)
61 #    undef MACH
62 #    define MACH MP1000
63 #  else
64 #    define MACH PC
65 #  endif
66 #else
67 #  error Cannot build nvram driver for this machine configuration.
68 #endif
69
70 #if MACH == PC
71
72 /* RTC in a PC */
73 #define CHECK_DRIVER_INIT()     1
74
75 /* On PCs, the checksum is built only over bytes 2..31 */
76 #define PC_CKS_RANGE_START      2
77 #define PC_CKS_RANGE_END        31
78 #define PC_CKS_LOC              32
79 #define NVRAM_BYTES             (128-NVRAM_FIRST_BYTE)
80
81 #define mach_check_checksum     pc_check_checksum
82 #define mach_set_checksum       pc_set_checksum
83 #define mach_proc_infos         pc_proc_infos
84
85 #endif
86
87 #if MACH == COBALT
88
89 #define CHECK_DRIVER_INIT()     1
90
91 #define NVRAM_BYTES             (128-NVRAM_FIRST_BYTE)
92
93 #define mach_check_checksum     cobalt_check_checksum
94 #define mach_set_checksum       cobalt_set_checksum
95 #define mach_proc_infos         cobalt_proc_infos
96
97 #endif
98
99 #if MACH == ATARI
100
101 /* Special parameters for RTC in Atari machines */
102 #include <asm/atarihw.h>
103 #include <asm/atariints.h>
104 #define RTC_PORT(x)             (TT_RTC_BAS + 2*(x))
105 #define CHECK_DRIVER_INIT()     (MACH_IS_ATARI && ATARIHW_PRESENT(TT_CLK))
106
107 #define NVRAM_BYTES             50
108
109 /* On Ataris, the checksum is over all bytes except the checksum bytes
110  * themselves; these are at the very end */
111 #define ATARI_CKS_RANGE_START   0
112 #define ATARI_CKS_RANGE_END     47
113 #define ATARI_CKS_LOC           48
114
115 #define mach_check_checksum     atari_check_checksum
116 #define mach_set_checksum       atari_set_checksum
117 #define mach_proc_infos         atari_proc_infos
118
119 #endif
120
121 #if MACH == MP1000
122
123 /* RTC in a MP1000 */
124 #define CHECK_DRIVER_INIT()     1
125
126 #define MP1000_CKS_RANGE_START  0
127 #define MP1000_CKS_RANGE_END    111
128 #define MP1000_CKS_LOC          112
129
130 #define NVRAM_BYTES             (128-NVRAM_FIRST_BYTE)
131
132 #define mach_check_checksum     mp1000_check_checksum
133 #define mach_set_checksum       mp1000_set_checksum
134 #define mach_proc_infos         mp1000_proc_infos
135
136 #endif
137
138 /* Note that *all* calls to CMOS_READ and CMOS_WRITE must be done with
139  * rtc_lock held. Due to the index-port/data-port design of the RTC, we
140  * don't want two different things trying to get to it at once. (e.g. the
141  * periodic 11 min sync from time.c vs. this driver.)
142  */
143
144 #include <linux/types.h>
145 #include <linux/errno.h>
146 #include <linux/miscdevice.h>
147 #include <linux/slab.h>
148 #include <linux/ioport.h>
149 #include <linux/fcntl.h>
150 #include <linux/mc146818rtc.h>
151 #include <linux/init.h>
152 #include <linux/proc_fs.h>
153 #include <linux/spinlock.h>
154
155 #include <asm/io.h>
156 #include <asm/uaccess.h>
157 #include <asm/system.h>
158
159 static DEFINE_SPINLOCK(nvram_state_lock);
160 static int nvram_open_cnt;      /* #times opened */
161 static int nvram_open_mode;     /* special open modes */
162 #define NVRAM_WRITE             1 /* opened for writing (exclusive) */
163 #define NVRAM_EXCL              2 /* opened with O_EXCL */
164
165 static int mach_check_checksum(void);
166 static void mach_set_checksum(void);
167
168 #ifdef CONFIG_PROC_FS
169 static int mach_proc_infos(unsigned char *contents, char *buffer, int *len,
170     off_t *begin, off_t offset, int size);
171 #endif
172
173 /*
174  * These functions are provided to be called internally or by other parts of
175  * the kernel. It's up to the caller to ensure correct checksum before reading
176  * or after writing (needs to be done only once).
177  *
178  * It is worth noting that these functions all access bytes of general
179  * purpose memory in the NVRAM - that is to say, they all add the
180  * NVRAM_FIRST_BYTE offset.  Pass them offsets into NVRAM as if you did not 
181  * know about the RTC cruft.
182  */
183
184 unsigned char
185 __nvram_read_byte(int i)
186 {
187         return CMOS_READ(NVRAM_FIRST_BYTE + i);
188 }
189
190 unsigned char
191 nvram_read_byte(int i)
192 {
193         unsigned long flags;
194         unsigned char c;
195
196         spin_lock_irqsave(&rtc_lock, flags);
197         c = __nvram_read_byte(i);
198         spin_unlock_irqrestore(&rtc_lock, flags);
199         return c;
200 }
201
202 /* This races nicely with trying to read with checksum checking (nvram_read) */
203 void
204 __nvram_write_byte(unsigned char c, int i)
205 {
206         CMOS_WRITE(c, NVRAM_FIRST_BYTE + i);
207 }
208
209 void
210 nvram_write_byte(unsigned char c, int i)
211 {
212         unsigned long flags;
213
214         spin_lock_irqsave(&rtc_lock, flags);
215         __nvram_write_byte(c, i);
216         spin_unlock_irqrestore(&rtc_lock, flags);
217 }
218
219 int
220 __nvram_check_checksum(void)
221 {
222         return mach_check_checksum();
223 }
224
225 int
226 nvram_check_checksum(void)
227 {
228         unsigned long flags;
229         int rv;
230
231         spin_lock_irqsave(&rtc_lock, flags);
232         rv = __nvram_check_checksum();
233         spin_unlock_irqrestore(&rtc_lock, flags);
234         return rv;
235 }
236
237 static void
238 __nvram_set_checksum(void)
239 {
240         mach_set_checksum();
241 }
242
243 #if 0
244 void
245 nvram_set_checksum(void)
246 {
247         unsigned long flags;
248
249         spin_lock_irqsave(&rtc_lock, flags);
250         __nvram_set_checksum();
251         spin_unlock_irqrestore(&rtc_lock, flags);
252 }
253 #endif  /*  0  */
254
255 /*
256  * The are the file operation function for user access to /dev/nvram
257  */
258
259 static loff_t nvram_llseek(struct file *file,loff_t offset, int origin )
260 {
261         lock_kernel();
262         switch (origin) {
263         case 0:
264                 /* nothing to do */
265                 break;
266         case 1:
267                 offset += file->f_pos;
268                 break;
269         case 2:
270                 offset += NVRAM_BYTES;
271                 break;
272         }
273         unlock_kernel();
274         return (offset >= 0) ? (file->f_pos = offset) : -EINVAL;
275 }
276
277 static ssize_t
278 nvram_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
279 {
280         unsigned char contents[NVRAM_BYTES];
281         unsigned i = *ppos;
282         unsigned char *tmp;
283
284         spin_lock_irq(&rtc_lock);
285
286         if (!__nvram_check_checksum())
287                 goto checksum_err;
288
289         for (tmp = contents; count-- > 0 && i < NVRAM_BYTES; ++i, ++tmp)
290                 *tmp = __nvram_read_byte(i);
291
292         spin_unlock_irq(&rtc_lock);
293
294         if (copy_to_user(buf, contents, tmp - contents))
295                 return -EFAULT;
296
297         *ppos = i;
298
299         return tmp - contents;
300
301       checksum_err:
302         spin_unlock_irq(&rtc_lock);
303         return -EIO;
304 }
305
306 static ssize_t
307 nvram_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
308 {
309         unsigned char contents[NVRAM_BYTES];
310         unsigned i = *ppos;
311         unsigned char *tmp;
312         int len;
313
314         len = (NVRAM_BYTES - i) < count ? (NVRAM_BYTES - i) : count;
315         if (copy_from_user(contents, buf, len))
316                 return -EFAULT;
317
318         spin_lock_irq(&rtc_lock);
319
320         if (!__nvram_check_checksum())
321                 goto checksum_err;
322
323         for (tmp = contents; count-- > 0 && i < NVRAM_BYTES; ++i, ++tmp)
324                 __nvram_write_byte(*tmp, i);
325
326         __nvram_set_checksum();
327
328         spin_unlock_irq(&rtc_lock);
329
330         *ppos = i;
331
332         return tmp - contents;
333
334       checksum_err:
335         spin_unlock_irq(&rtc_lock);
336         return -EIO;
337 }
338
339 static int
340 nvram_ioctl(struct inode *inode, struct file *file,
341     unsigned int cmd, unsigned long arg)
342 {
343         int i;
344
345         switch (cmd) {
346
347         case NVRAM_INIT:
348                 /* initialize NVRAM contents and checksum */
349                 if (!capable(CAP_SYS_ADMIN))
350                         return -EACCES;
351
352                 spin_lock_irq(&rtc_lock);
353
354                 for (i = 0; i < NVRAM_BYTES; ++i)
355                         __nvram_write_byte(0, i);
356                 __nvram_set_checksum();
357
358                 spin_unlock_irq(&rtc_lock);
359                 return 0;
360
361         case NVRAM_SETCKS:
362                 /* just set checksum, contents unchanged (maybe useful after 
363                  * checksum garbaged somehow...) */
364                 if (!capable(CAP_SYS_ADMIN))
365                         return -EACCES;
366
367                 spin_lock_irq(&rtc_lock);
368                 __nvram_set_checksum();
369                 spin_unlock_irq(&rtc_lock);
370                 return 0;
371
372         default:
373                 return -ENOTTY;
374         }
375 }
376
377 static int
378 nvram_open(struct inode *inode, struct file *file)
379 {
380         spin_lock(&nvram_state_lock);
381
382         if ((nvram_open_cnt && (file->f_flags & O_EXCL)) ||
383             (nvram_open_mode & NVRAM_EXCL) ||
384             ((file->f_mode & 2) && (nvram_open_mode & NVRAM_WRITE))) {
385                 spin_unlock(&nvram_state_lock);
386                 return -EBUSY;
387         }
388
389         if (file->f_flags & O_EXCL)
390                 nvram_open_mode |= NVRAM_EXCL;
391         if (file->f_mode & 2)
392                 nvram_open_mode |= NVRAM_WRITE;
393         nvram_open_cnt++;
394
395         spin_unlock(&nvram_state_lock);
396
397         return 0;
398 }
399
400 static int
401 nvram_release(struct inode *inode, struct file *file)
402 {
403         spin_lock(&nvram_state_lock);
404
405         nvram_open_cnt--;
406
407         /* if only one instance is open, clear the EXCL bit */
408         if (nvram_open_mode & NVRAM_EXCL)
409                 nvram_open_mode &= ~NVRAM_EXCL;
410         if (file->f_mode & 2)
411                 nvram_open_mode &= ~NVRAM_WRITE;
412
413         spin_unlock(&nvram_state_lock);
414
415         return 0;
416 }
417
418 #ifndef CONFIG_PROC_FS
419 static int
420 nvram_read_proc(char *buffer, char **start, off_t offset,
421     int size, int *eof, void *data)
422 {
423         return 0;
424 }
425 #else
426
427 static int
428 nvram_read_proc(char *buffer, char **start, off_t offset,
429     int size, int *eof, void *data)
430 {
431         unsigned char contents[NVRAM_BYTES];
432         int i, len = 0;
433         off_t begin = 0;
434
435         spin_lock_irq(&rtc_lock);
436         for (i = 0; i < NVRAM_BYTES; ++i)
437                 contents[i] = __nvram_read_byte(i);
438         spin_unlock_irq(&rtc_lock);
439
440         *eof = mach_proc_infos(contents, buffer, &len, &begin, offset, size);
441
442         if (offset >= begin + len)
443                 return 0;
444         *start = buffer + (offset - begin);
445         return (size < begin + len - offset) ? size : begin + len - offset;
446
447 }
448
449 /* This macro frees the machine specific function from bounds checking and
450  * this like that... */
451 #define PRINT_PROC(fmt,args...)                                 \
452         do {                                                    \
453                 *len += sprintf(buffer+*len, fmt, ##args);      \
454                 if (*begin + *len > offset + size)              \
455                         return 0;                               \
456                 if (*begin + *len < offset) {                   \
457                         *begin += *len;                         \
458                         *len = 0;                               \
459                 }                                               \
460         } while(0)
461
462 #endif /* CONFIG_PROC_FS */
463
464 static struct file_operations nvram_fops = {
465         .owner          = THIS_MODULE,
466         .llseek         = nvram_llseek,
467         .read           = nvram_read,
468         .write          = nvram_write,
469         .ioctl          = nvram_ioctl,
470         .open           = nvram_open,
471         .release        = nvram_release,
472 };
473
474 static struct miscdevice nvram_dev = {
475         NVRAM_MINOR,
476         "nvram",
477         &nvram_fops
478 };
479
480 static int __init
481 nvram_init(void)
482 {
483         int ret;
484
485         /* First test whether the driver should init at all */
486         if (!CHECK_DRIVER_INIT())
487                 return -ENXIO;
488
489         ret = misc_register(&nvram_dev);
490         if (ret) {
491                 printk(KERN_ERR "nvram: can't misc_register on minor=%d\n",
492                     NVRAM_MINOR);
493                 goto out;
494         }
495         if (!create_proc_read_entry("driver/nvram", 0, NULL, nvram_read_proc,
496                 NULL)) {
497                 printk(KERN_ERR "nvram: can't create /proc/driver/nvram\n");
498                 ret = -ENOMEM;
499                 goto outmisc;
500         }
501         ret = 0;
502         printk(KERN_INFO "Non-volatile memory driver v" NVRAM_VERSION "\n");
503       out:
504         return ret;
505       outmisc:
506         misc_deregister(&nvram_dev);
507         goto out;
508 }
509
510 static void __exit
511 nvram_cleanup_module(void)
512 {
513         remove_proc_entry("driver/nvram", NULL);
514         misc_deregister(&nvram_dev);
515 }
516
517 module_init(nvram_init);
518 module_exit(nvram_cleanup_module);
519
520 /*
521  * Machine specific functions
522  */
523
524 #if MACH == PC
525
526 static int
527 pc_check_checksum(void)
528 {
529         int i;
530         unsigned short sum = 0;
531         unsigned short expect;
532
533         for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i)
534                 sum += __nvram_read_byte(i);
535         expect = __nvram_read_byte(PC_CKS_LOC)<<8 |
536             __nvram_read_byte(PC_CKS_LOC+1);
537         return ((sum & 0xffff) == expect);
538 }
539
540 static void
541 pc_set_checksum(void)
542 {
543         int i;
544         unsigned short sum = 0;
545
546         for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i)
547                 sum += __nvram_read_byte(i);
548         __nvram_write_byte(sum >> 8, PC_CKS_LOC);
549         __nvram_write_byte(sum & 0xff, PC_CKS_LOC + 1);
550 }
551
552 #ifdef CONFIG_PROC_FS
553
554 static char *floppy_types[] = {
555         "none", "5.25'' 360k", "5.25'' 1.2M", "3.5'' 720k", "3.5'' 1.44M",
556         "3.5'' 2.88M", "3.5'' 2.88M"
557 };
558
559 static char *gfx_types[] = {
560         "EGA, VGA, ... (with BIOS)",
561         "CGA (40 cols)",
562         "CGA (80 cols)",
563         "monochrome",
564 };
565
566 static int
567 pc_proc_infos(unsigned char *nvram, char *buffer, int *len,
568     off_t *begin, off_t offset, int size)
569 {
570         int checksum;
571         int type;
572
573         spin_lock_irq(&rtc_lock);
574         checksum = __nvram_check_checksum();
575         spin_unlock_irq(&rtc_lock);
576
577         PRINT_PROC("Checksum status: %svalid\n", checksum ? "" : "not ");
578
579         PRINT_PROC("# floppies     : %d\n",
580             (nvram[6] & 1) ? (nvram[6] >> 6) + 1 : 0);
581         PRINT_PROC("Floppy 0 type  : ");
582         type = nvram[2] >> 4;
583         if (type < sizeof (floppy_types) / sizeof (*floppy_types))
584                 PRINT_PROC("%s\n", floppy_types[type]);
585         else
586                 PRINT_PROC("%d (unknown)\n", type);
587         PRINT_PROC("Floppy 1 type  : ");
588         type = nvram[2] & 0x0f;
589         if (type < sizeof (floppy_types) / sizeof (*floppy_types))
590                 PRINT_PROC("%s\n", floppy_types[type]);
591         else
592                 PRINT_PROC("%d (unknown)\n", type);
593
594         PRINT_PROC("HD 0 type      : ");
595         type = nvram[4] >> 4;
596         if (type)
597                 PRINT_PROC("%02x\n", type == 0x0f ? nvram[11] : type);
598         else
599                 PRINT_PROC("none\n");
600
601         PRINT_PROC("HD 1 type      : ");
602         type = nvram[4] & 0x0f;
603         if (type)
604                 PRINT_PROC("%02x\n", type == 0x0f ? nvram[12] : type);
605         else
606                 PRINT_PROC("none\n");
607
608         PRINT_PROC("HD type 48 data: %d/%d/%d C/H/S, precomp %d, lz %d\n",
609             nvram[18] | (nvram[19] << 8),
610             nvram[20], nvram[25],
611             nvram[21] | (nvram[22] << 8), nvram[23] | (nvram[24] << 8));
612         PRINT_PROC("HD type 49 data: %d/%d/%d C/H/S, precomp %d, lz %d\n",
613             nvram[39] | (nvram[40] << 8),
614             nvram[41], nvram[46],
615             nvram[42] | (nvram[43] << 8), nvram[44] | (nvram[45] << 8));
616
617         PRINT_PROC("DOS base memory: %d kB\n", nvram[7] | (nvram[8] << 8));
618         PRINT_PROC("Extended memory: %d kB (configured), %d kB (tested)\n",
619             nvram[9] | (nvram[10] << 8), nvram[34] | (nvram[35] << 8));
620
621         PRINT_PROC("Gfx adapter    : %s\n", gfx_types[(nvram[6] >> 4) & 3]);
622
623         PRINT_PROC("FPU            : %sinstalled\n",
624             (nvram[6] & 2) ? "" : "not ");
625
626         return 1;
627 }
628 #endif
629
630 #endif /* MACH == PC */
631
632 #if MACH == COBALT
633
634 /* the cobalt CMOS has a wider range of its checksum */
635 static int cobalt_check_checksum(void)
636 {
637         int i;
638         unsigned short sum = 0;
639         unsigned short expect;
640
641         for (i = COBT_CMOS_CKS_START; i <= COBT_CMOS_CKS_END; ++i) {
642                 if ((i == COBT_CMOS_CHECKSUM) || (i == (COBT_CMOS_CHECKSUM+1)))
643                         continue;
644
645                 sum += __nvram_read_byte(i);
646         }
647         expect = __nvram_read_byte(COBT_CMOS_CHECKSUM) << 8 |
648             __nvram_read_byte(COBT_CMOS_CHECKSUM+1);
649         return ((sum & 0xffff) == expect);
650 }
651
652 static void cobalt_set_checksum(void)
653 {
654         int i;
655         unsigned short sum = 0;
656
657         for (i = COBT_CMOS_CKS_START; i <= COBT_CMOS_CKS_END; ++i) {
658                 if ((i == COBT_CMOS_CHECKSUM) || (i == (COBT_CMOS_CHECKSUM+1)))
659                         continue;
660
661                 sum += __nvram_read_byte(i);
662         }
663
664         __nvram_write_byte(sum >> 8, COBT_CMOS_CHECKSUM);
665         __nvram_write_byte(sum & 0xff, COBT_CMOS_CHECKSUM+1);
666 }
667
668 #ifdef CONFIG_PROC_FS
669
670 static int cobalt_proc_infos(unsigned char *nvram, char *buffer, int *len,
671         off_t *begin, off_t offset, int size)
672 {
673         int i;
674         unsigned int checksum;
675         unsigned int flags;
676         char sernum[14];
677         char *key = "cNoEbTaWlOtR!";
678         unsigned char bto_csum;
679
680         spin_lock_irq(&rtc_lock);
681         checksum = __nvram_check_checksum();
682         spin_unlock_irq(&rtc_lock);
683
684         PRINT_PROC("Checksum status: %svalid\n", checksum ? "" : "not ");
685
686         flags = nvram[COBT_CMOS_FLAG_BYTE_0] << 8 
687             | nvram[COBT_CMOS_FLAG_BYTE_1];
688
689         PRINT_PROC("Console: %s\n",
690                 flags & COBT_CMOS_CONSOLE_FLAG ?  "on": "off");
691
692         PRINT_PROC("Firmware Debug Messages: %s\n",
693                 flags & COBT_CMOS_DEBUG_FLAG ? "on": "off");
694
695         PRINT_PROC("Auto Prompt: %s\n",
696                 flags & COBT_CMOS_AUTO_PROMPT_FLAG ? "on": "off");
697
698         PRINT_PROC("Shutdown Status: %s\n",
699                 flags & COBT_CMOS_CLEAN_BOOT_FLAG ? "clean": "dirty");
700
701         PRINT_PROC("Hardware Probe: %s\n",
702                 flags & COBT_CMOS_HW_NOPROBE_FLAG ? "partial": "full");
703
704         PRINT_PROC("System Fault: %sdetected\n",
705                 flags & COBT_CMOS_SYSFAULT_FLAG ? "": "not ");
706
707         PRINT_PROC("Panic on OOPS: %s\n",
708                 flags & COBT_CMOS_OOPSPANIC_FLAG ? "yes": "no");
709
710         PRINT_PROC("Delayed Cache Initialization: %s\n",
711                 flags & COBT_CMOS_DELAY_CACHE_FLAG ? "yes": "no");
712
713         PRINT_PROC("Show Logo at Boot: %s\n",
714                 flags & COBT_CMOS_NOLOGO_FLAG ? "no": "yes");
715
716         PRINT_PROC("Boot Method: ");
717         switch (nvram[COBT_CMOS_BOOT_METHOD]) {
718         case COBT_CMOS_BOOT_METHOD_DISK:
719                 PRINT_PROC("disk\n");
720                 break;
721
722         case COBT_CMOS_BOOT_METHOD_ROM:
723                 PRINT_PROC("rom\n");
724                 break;
725
726         case COBT_CMOS_BOOT_METHOD_NET:
727                 PRINT_PROC("net\n");
728                 break;
729
730         default:
731                 PRINT_PROC("unknown\n");
732                 break;
733         }
734
735         PRINT_PROC("Primary Boot Device: %d:%d\n",
736                 nvram[COBT_CMOS_BOOT_DEV0_MAJ],
737                 nvram[COBT_CMOS_BOOT_DEV0_MIN] );
738         PRINT_PROC("Secondary Boot Device: %d:%d\n",
739                 nvram[COBT_CMOS_BOOT_DEV1_MAJ],
740                 nvram[COBT_CMOS_BOOT_DEV1_MIN] );
741         PRINT_PROC("Tertiary Boot Device: %d:%d\n",
742                 nvram[COBT_CMOS_BOOT_DEV2_MAJ],
743                 nvram[COBT_CMOS_BOOT_DEV2_MIN] );
744
745         PRINT_PROC("Uptime: %d\n",
746                 nvram[COBT_CMOS_UPTIME_0] << 24 |
747                 nvram[COBT_CMOS_UPTIME_1] << 16 |
748                 nvram[COBT_CMOS_UPTIME_2] << 8  |
749                 nvram[COBT_CMOS_UPTIME_3]);
750
751         PRINT_PROC("Boot Count: %d\n",
752                 nvram[COBT_CMOS_BOOTCOUNT_0] << 24 |
753                 nvram[COBT_CMOS_BOOTCOUNT_1] << 16 |
754                 nvram[COBT_CMOS_BOOTCOUNT_2] << 8  |
755                 nvram[COBT_CMOS_BOOTCOUNT_3]);
756
757         /* 13 bytes of serial num */
758         for (i=0 ; i<13 ; i++) {
759                 sernum[i] = nvram[COBT_CMOS_SYS_SERNUM_0 + i];
760         }
761         sernum[13] = '\0';
762
763         checksum = 0;
764         for (i=0 ; i<13 ; i++) {
765                 checksum += sernum[i] ^ key[i];
766         }
767         checksum = ((checksum & 0x7f) ^ (0xd6)) & 0xff;
768
769         PRINT_PROC("Serial Number: %s", sernum);
770         if (checksum != nvram[COBT_CMOS_SYS_SERNUM_CSUM]) {
771                 PRINT_PROC(" (invalid checksum)");
772         }
773         PRINT_PROC("\n");
774
775         PRINT_PROC("Rom Revison: %d.%d.%d\n", nvram[COBT_CMOS_ROM_REV_MAJ],
776                 nvram[COBT_CMOS_ROM_REV_MIN], nvram[COBT_CMOS_ROM_REV_REV]);
777
778         PRINT_PROC("BTO Server: %d.%d.%d.%d", nvram[COBT_CMOS_BTO_IP_0],
779                 nvram[COBT_CMOS_BTO_IP_1], nvram[COBT_CMOS_BTO_IP_2],
780                 nvram[COBT_CMOS_BTO_IP_3]);
781         bto_csum = nvram[COBT_CMOS_BTO_IP_0] + nvram[COBT_CMOS_BTO_IP_1]
782                 + nvram[COBT_CMOS_BTO_IP_2] + nvram[COBT_CMOS_BTO_IP_3];
783         if (bto_csum != nvram[COBT_CMOS_BTO_IP_CSUM]) {
784                 PRINT_PROC(" (invalid checksum)");
785         }
786         PRINT_PROC("\n");
787
788         if (flags & COBT_CMOS_VERSION_FLAG
789          && nvram[COBT_CMOS_VERSION] >= COBT_CMOS_VER_BTOCODE) {
790                 PRINT_PROC("BTO Code: 0x%x\n",
791                         nvram[COBT_CMOS_BTO_CODE_0] << 24 |
792                         nvram[COBT_CMOS_BTO_CODE_1] << 16 |
793                         nvram[COBT_CMOS_BTO_CODE_2] << 8 |
794                         nvram[COBT_CMOS_BTO_CODE_3]);
795         }
796
797         return 1;
798 }
799 #endif /* CONFIG_PROC_FS */
800
801 #endif /* MACH == COBALT */
802
803 #if MACH == ATARI
804
805 static int
806 atari_check_checksum(void)
807 {
808         int i;
809         unsigned char sum = 0;
810
811         for (i = ATARI_CKS_RANGE_START; i <= ATARI_CKS_RANGE_END; ++i)
812                 sum += __nvram_read_byte(i);
813         return (__nvram_read_byte(ATARI_CKS_LOC) == (~sum & 0xff) &&
814             __nvram_read_byte(ATARI_CKS_LOC + 1) == (sum & 0xff));
815 }
816
817 static void
818 atari_set_checksum(void)
819 {
820         int i;
821         unsigned char sum = 0;
822
823         for (i = ATARI_CKS_RANGE_START; i <= ATARI_CKS_RANGE_END; ++i)
824                 sum += __nvram_read_byte(i);
825         __nvram_write_byte(~sum, ATARI_CKS_LOC);
826         __nvram_write_byte(sum, ATARI_CKS_LOC + 1);
827 }
828
829 #ifdef CONFIG_PROC_FS
830
831 static struct {
832         unsigned char val;
833         char *name;
834 } boot_prefs[] = {
835         { 0x80, "TOS" },
836         { 0x40, "ASV" },
837         { 0x20, "NetBSD (?)" },
838         { 0x10, "Linux" },
839         { 0x00, "unspecified" }
840 };
841
842 static char *languages[] = {
843         "English (US)",
844         "German",
845         "French",
846         "English (UK)",
847         "Spanish",
848         "Italian",
849         "6 (undefined)",
850         "Swiss (French)",
851         "Swiss (German)"
852 };
853
854 static char *dateformat[] = {
855         "MM%cDD%cYY",
856         "DD%cMM%cYY",
857         "YY%cMM%cDD",
858         "YY%cDD%cMM",
859         "4 (undefined)",
860         "5 (undefined)",
861         "6 (undefined)",
862         "7 (undefined)"
863 };
864
865 static char *colors[] = {
866         "2", "4", "16", "256", "65536", "??", "??", "??"
867 };
868
869 #define fieldsize(a)    (sizeof(a)/sizeof(*a))
870
871 static int
872 atari_proc_infos(unsigned char *nvram, char *buffer, int *len,
873     off_t *begin, off_t offset, int size)
874 {
875         int checksum = nvram_check_checksum();
876         int i;
877         unsigned vmode;
878
879         PRINT_PROC("Checksum status  : %svalid\n", checksum ? "" : "not ");
880
881         PRINT_PROC("Boot preference  : ");
882         for (i = fieldsize(boot_prefs) - 1; i >= 0; --i) {
883                 if (nvram[1] == boot_prefs[i].val) {
884                         PRINT_PROC("%s\n", boot_prefs[i].name);
885                         break;
886                 }
887         }
888         if (i < 0)
889                 PRINT_PROC("0x%02x (undefined)\n", nvram[1]);
890
891         PRINT_PROC("SCSI arbitration : %s\n",
892             (nvram[16] & 0x80) ? "on" : "off");
893         PRINT_PROC("SCSI host ID     : ");
894         if (nvram[16] & 0x80)
895                 PRINT_PROC("%d\n", nvram[16] & 7);
896         else
897                 PRINT_PROC("n/a\n");
898
899         /* the following entries are defined only for the Falcon */
900         if ((atari_mch_cookie >> 16) != ATARI_MCH_FALCON)
901                 return 1;
902
903         PRINT_PROC("OS language      : ");
904         if (nvram[6] < fieldsize(languages))
905                 PRINT_PROC("%s\n", languages[nvram[6]]);
906         else
907                 PRINT_PROC("%u (undefined)\n", nvram[6]);
908         PRINT_PROC("Keyboard language: ");
909         if (nvram[7] < fieldsize(languages))
910                 PRINT_PROC("%s\n", languages[nvram[7]]);
911         else
912                 PRINT_PROC("%u (undefined)\n", nvram[7]);
913         PRINT_PROC("Date format      : ");
914         PRINT_PROC(dateformat[nvram[8] & 7],
915             nvram[9] ? nvram[9] : '/', nvram[9] ? nvram[9] : '/');
916         PRINT_PROC(", %dh clock\n", nvram[8] & 16 ? 24 : 12);
917         PRINT_PROC("Boot delay       : ");
918         if (nvram[10] == 0)
919                 PRINT_PROC("default");
920         else
921                 PRINT_PROC("%ds%s\n", nvram[10],
922                     nvram[10] < 8 ? ", no memory test" : "");
923
924         vmode = (nvram[14] << 8) || nvram[15];
925         PRINT_PROC("Video mode       : %s colors, %d columns, %s %s monitor\n",
926             colors[vmode & 7],
927             vmode & 8 ? 80 : 40,
928             vmode & 16 ? "VGA" : "TV", vmode & 32 ? "PAL" : "NTSC");
929         PRINT_PROC("                   %soverscan, compat. mode %s%s\n",
930             vmode & 64 ? "" : "no ",
931             vmode & 128 ? "on" : "off",
932             vmode & 256 ?
933             (vmode & 16 ? ", line doubling" : ", half screen") : "");
934
935         return 1;
936 }
937 #endif
938
939 #endif /* MACH == ATARI */
940
941 #if MACH == MP1000
942
943 static int
944 mp1000_check_checksum(void)
945 {
946         int i;
947         unsigned short sum = 0;
948         unsigned short expect;
949
950         for (i = MP1000_CKS_RANGE_START; i <= MP1000_CKS_RANGE_END; ++i)
951                 sum += __nvram_read_byte(i);
952
953         expect = __nvram_read_byte(MP1000_CKS_LOC+1)<<8 |
954             __nvram_read_byte(MP1000_CKS_LOC);
955         return ((sum & 0xffff) == expect);
956 }
957
958 static void
959 mp1000_set_checksum(void)
960 {
961         int i;
962         unsigned short sum = 0;
963
964         for (i = MP1000_CKS_RANGE_START; i <= MP1000_CKS_RANGE_END; ++i)
965                 sum += __nvram_read_byte(i);
966         __nvram_write_byte(sum >> 8, MP1000_CKS_LOC + 1);
967         __nvram_write_byte(sum & 0xff, MP1000_CKS_LOC);
968 }
969
970 #ifdef CONFIG_PROC_FS
971
972 #define         SERVER_N_LEN         32
973 #define         PATH_N_LEN           32
974 #define         FILE_N_LEN           32
975 #define         NVRAM_MAGIC_SIG      0xdead
976
977 typedef struct NvRamImage
978 {
979         unsigned short int    magic;
980         unsigned short int    mode;
981         char                  fname[FILE_N_LEN];
982         char                  path[PATH_N_LEN];
983         char                  server[SERVER_N_LEN];
984         char                  pad[12];
985 } NvRam;
986
987 static int
988 mp1000_proc_infos(unsigned char *nvram, char *buffer, int *len,
989     off_t *begin, off_t offset, int size)
990 {
991         int checksum;
992         NvRam* nv = (NvRam*)nvram;
993
994         spin_lock_irq(&rtc_lock);
995         checksum = __nvram_check_checksum();
996         spin_unlock_irq(&rtc_lock);
997
998         PRINT_PROC("Checksum status: %svalid\n", checksum ? "" : "not ");
999
1000         switch( nv->mode )
1001         {
1002            case 0 :
1003                     PRINT_PROC( "\tMode 0, tftp prompt\n" );
1004                     break;
1005            case 1 :
1006                     PRINT_PROC( "\tMode 1, booting from disk\n" );
1007                     break;
1008            case 2 :
1009                     PRINT_PROC( "\tMode 2, Alternate boot from disk /boot/%s\n", nv->fname );
1010                     break;
1011            case 3 :
1012                     PRINT_PROC( "\tMode 3, Booting from net:\n" );
1013                     PRINT_PROC( "\t\t%s:%s%s\n",nv->server, nv->path, nv->fname );
1014                     break;
1015            default:
1016                     PRINT_PROC( "\tInconsistant nvram?\n" );
1017                     break;
1018         }
1019
1020         return 1;
1021 }
1022 #endif
1023
1024 #endif /* MACH == MP1000 */
1025
1026 MODULE_LICENSE("GPL");
1027
1028 EXPORT_SYMBOL(__nvram_read_byte);
1029 EXPORT_SYMBOL(nvram_read_byte);
1030 EXPORT_SYMBOL(__nvram_write_byte);
1031 EXPORT_SYMBOL(nvram_write_byte);
1032 EXPORT_SYMBOL(__nvram_check_checksum);
1033 EXPORT_SYMBOL(nvram_check_checksum);
1034 MODULE_ALIAS_MISCDEV(NVRAM_MINOR);