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