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