Import 2.3.25pre1
[davej-history.git] / drivers / char / nvram.c
blob9ac9908723f4b9c28c16ca31b1529928741f1b9d
1 /*
2 * CMOS/NV-RAM driver for Linux
4 * Copyright (C) 1997 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
5 * idea by and with help from Richard Jelinek <rj@suse.de>
7 * This driver allows you to access the contents of the non-volatile memory in
8 * the mc146818rtc.h real-time clock. This chip is built into all PCs and into
9 * many Atari machines. In the former it's called "CMOS-RAM", in the latter
10 * "NVRAM" (NV stands for non-volatile).
12 * The data are supplied as a (seekable) character device, /dev/nvram. The
13 * size of this file is 50, the number of freely available bytes in the memory
14 * (i.e., not used by the RTC itself).
16 * Checksums over the NVRAM contents are managed by this driver. In case of a
17 * bad checksum, reads and writes return -EIO. The checksum can be initialized
18 * to a sane state either by ioctl(NVRAM_INIT) (clear whole NVRAM) or
19 * ioctl(NVRAM_SETCKS) (doesn't change contents, just makes checksum valid
20 * again; use with care!)
22 * This file also provides some functions for other parts of the kernel that
23 * want to access the NVRAM: nvram_{read,write,check_checksum,set_checksum}.
24 * Obviously this can be used only if this driver is always configured into
25 * the kernel and is not a module. Since the functions are used by some Atari
26 * drivers, this is the case on the Atari.
30 #define NVRAM_VERSION "1.0"
32 #include <linux/module.h>
33 #include <linux/config.h>
35 #define PC 1
36 #define ATARI 2
38 /* select machine configuration */
39 #if defined(CONFIG_ATARI)
40 #define MACH ATARI
41 #elif defined(__i386__) /* and others?? */
42 #define MACH PC
43 #else
44 #error Cannot build nvram driver for this machine configuration.
45 #endif
47 #if MACH == PC
49 /* RTC in a PC */
50 #define CHECK_DRIVER_INIT() 1
52 /* On PCs, the checksum is built only over bytes 2..31 */
53 #define PC_CKS_RANGE_START 2
54 #define PC_CKS_RANGE_END 31
55 #define PC_CKS_LOC 32
57 #define mach_check_checksum pc_check_checksum
58 #define mach_set_checksum pc_set_checksum
59 #define mach_proc_infos pc_proc_infos
61 #endif
63 #if MACH == ATARI
65 /* Special parameters for RTC in Atari machines */
66 #include <asm/atarihw.h>
67 #include <asm/atariints.h>
68 #define RTC_PORT(x) (TT_RTC_BAS + 2*(x))
69 #define CHECK_DRIVER_INIT() (MACH_IS_ATARI && ATARIHW_PRESENT(TT_CLK))
71 /* On Ataris, the checksum is over all bytes except the checksum bytes
72 * themselves; these are at the very end */
73 #define ATARI_CKS_RANGE_START 0
74 #define ATARI_CKS_RANGE_END 47
75 #define ATARI_CKS_LOC 48
77 #define mach_check_checksum atari_check_checksum
78 #define mach_set_checksum atari_set_checksum
79 #define mach_proc_infos atari_proc_infos
81 #endif
83 /* Note that *all* calls to CMOS_READ and CMOS_WRITE must be done with
84 * interrupts disabled. Due to the index-port/data-port design of the RTC, we
85 * don't want two different things trying to get to it at once. (e.g. the
86 * periodic 11 min sync from time.c vs. this driver.)
89 #include <linux/types.h>
90 #include <linux/errno.h>
91 #include <linux/miscdevice.h>
92 #include <linux/malloc.h>
93 #include <linux/ioport.h>
94 #include <linux/fcntl.h>
95 #include <linux/mc146818rtc.h>
96 #include <linux/nvram.h>
97 #include <linux/init.h>
98 #include <linux/proc_fs.h>
100 #include <asm/io.h>
101 #include <asm/uaccess.h>
102 #include <asm/system.h>
105 static int nvram_open_cnt = 0; /* #times opened */
106 static int nvram_open_mode; /* special open modes */
107 #define NVRAM_WRITE 1 /* opened for writing (exclusive) */
108 #define NVRAM_EXCL 2 /* opened with O_EXCL */
110 #define RTC_FIRST_BYTE 14 /* RTC register number of first NVRAM byte */
111 #define NVRAM_BYTES 50 /* number of NVRAM bytes */
114 static int mach_check_checksum( void );
115 static void mach_set_checksum( void );
116 #ifdef CONFIG_PROC_FS
117 static int mach_proc_infos( unsigned char *contents, char *buffer, int *len,
118 off_t *begin, off_t offset, int size );
119 #endif
123 * These are the internal NVRAM access functions, which do NOT disable
124 * interrupts and do not check the checksum. Both tasks are left to higher
125 * level function, so they need to be done only once per syscall.
128 static __inline__ unsigned char nvram_read_int( int i )
130 return( CMOS_READ( RTC_FIRST_BYTE+i ) );
133 static __inline__ void nvram_write_int( unsigned char c, int i )
135 CMOS_WRITE( c, RTC_FIRST_BYTE+i );
138 static __inline__ int nvram_check_checksum_int( void )
140 return( mach_check_checksum() );
143 static __inline__ void nvram_set_checksum_int( void )
145 mach_set_checksum();
148 #if MACH == ATARI
151 * These non-internal functions are provided to be called 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).
155 * They're only built if CONFIG_ATARI is defined, because Atari drivers use
156 * them. For other configurations (PC), the rest of the kernel can't rely on
157 * them being present (this driver may not be configured at all, or as a
158 * module), so they access config information themselves.
161 unsigned char nvram_read_byte( int i )
163 unsigned long flags;
164 unsigned char c;
166 save_flags(flags);
167 cli();
168 c = nvram_read_int( i );
169 restore_flags(flags);
170 return( c );
173 void nvram_write_byte( unsigned char c, int i )
175 unsigned long flags;
177 save_flags(flags);
178 cli();
179 nvram_write_int( c, i );
180 restore_flags(flags);
183 int nvram_check_checksum( void )
185 unsigned long flags;
186 int rv;
188 save_flags(flags);
189 cli();
190 rv = nvram_check_checksum_int();
191 restore_flags(flags);
192 return( rv );
195 void nvram_set_checksum( void )
197 unsigned long flags;
199 save_flags(flags);
200 cli();
201 nvram_set_checksum_int();
202 restore_flags(flags);
205 #endif /* MACH == ATARI */
209 * The are the file operation function for user access to /dev/nvram
212 static long long nvram_llseek(struct file *file,loff_t offset, int origin )
214 switch( origin ) {
215 case 0:
216 /* nothing to do */
217 break;
218 case 1:
219 offset += file->f_pos;
220 break;
221 case 2:
222 offset += NVRAM_BYTES;
223 break;
225 return( (offset >= 0) ? (file->f_pos = offset) : -EINVAL );
228 static ssize_t nvram_read(struct file * file,
229 char * buf, size_t count, loff_t *ppos )
231 unsigned long flags;
232 unsigned i = *ppos;
233 char *tmp = buf;
235 if (i != *ppos)
236 return -EINVAL;
238 save_flags(flags);
239 cli();
241 if (!nvram_check_checksum_int()) {
242 restore_flags(flags);
243 return( -EIO );
246 for( ; count-- > 0 && i < NVRAM_BYTES; ++i, ++tmp )
247 put_user( nvram_read_int(i), tmp );
248 *ppos = i;
250 restore_flags(flags);
251 return( tmp - buf );
254 static ssize_t nvram_write(struct file * file,
255 const char * buf, size_t count, loff_t *ppos )
257 unsigned long flags;
258 unsigned i = *ppos;
259 const char *tmp = buf;
260 char c;
262 if (i != *ppos)
263 return -EINVAL;
265 save_flags(flags);
266 cli();
268 if (!nvram_check_checksum_int()) {
269 restore_flags(flags);
270 return( -EIO );
273 for( ; count-- > 0 && i < NVRAM_BYTES; ++i, ++tmp ) {
274 get_user( c, tmp );
275 nvram_write_int( c, i );
277 nvram_set_checksum_int();
278 *ppos = i;
280 restore_flags(flags);
281 return( tmp - buf );
284 static int nvram_ioctl( struct inode *inode, struct file *file,
285 unsigned int cmd, unsigned long arg )
287 unsigned long flags;
288 int i;
290 switch( cmd ) {
292 case NVRAM_INIT: /* initialize NVRAM contents and checksum */
293 if (!capable(CAP_SYS_ADMIN))
294 return( -EACCES );
296 save_flags(flags);
297 cli();
299 for( i = 0; i < NVRAM_BYTES; ++i )
300 nvram_write_int( 0, i );
301 nvram_set_checksum_int();
303 restore_flags(flags);
304 return( 0 );
306 case NVRAM_SETCKS: /* just set checksum, contents unchanged
307 * (maybe useful after checksum garbaged
308 * somehow...) */
309 if (!capable(CAP_SYS_ADMIN))
310 return( -EACCES );
312 save_flags(flags);
313 cli();
314 nvram_set_checksum_int();
315 restore_flags(flags);
316 return( 0 );
318 default:
319 return( -EINVAL );
323 static int nvram_open( struct inode *inode, struct file *file )
325 if ((nvram_open_cnt && (file->f_flags & O_EXCL)) ||
326 (nvram_open_mode & NVRAM_EXCL) ||
327 ((file->f_mode & 2) && (nvram_open_mode & NVRAM_WRITE)))
328 return( -EBUSY );
330 if (file->f_flags & O_EXCL)
331 nvram_open_mode |= NVRAM_EXCL;
332 if (file->f_mode & 2)
333 nvram_open_mode |= NVRAM_WRITE;
334 nvram_open_cnt++;
335 MOD_INC_USE_COUNT;
336 return( 0 );
339 static int nvram_release( struct inode *inode, struct file *file )
341 nvram_open_cnt--;
342 if (file->f_flags & O_EXCL)
343 nvram_open_mode &= ~NVRAM_EXCL;
344 if (file->f_mode & 2)
345 nvram_open_mode &= ~NVRAM_WRITE;
347 MOD_DEC_USE_COUNT;
348 return( 0 );
352 #ifdef CONFIG_PROC_FS
353 static int nvram_read_proc( char *buffer, char **start, off_t offset,
354 int size, int *eof,
355 void *data) {}
356 #else
358 static int nvram_read_proc( char *buffer, char **start, off_t offset,
359 int size, int *eof, void *data )
361 unsigned long flags;
362 unsigned char contents[NVRAM_BYTES];
363 int i, len = 0;
364 off_t begin = 0;
366 save_flags(flags);
367 cli();
368 for( i = 0; i < NVRAM_BYTES; ++i )
369 contents[i] = nvram_read_int( i );
370 restore_flags(flags);
372 *eof = mach_proc_infos( contents, buffer, &len, &begin, offset, size );
374 if (offset >= begin + len)
375 return( 0 );
376 *start = buffer + (begin - offset);
377 return( size < begin + len - offset ? size : begin + len - offset );
381 /* This macro frees the machine specific function from bounds checking and
382 * this like that... */
383 #define PRINT_PROC(fmt,args...) \
384 do { \
385 *len += sprintf( buffer+*len, fmt, ##args ); \
386 if (*begin + *len > offset + size) \
387 return( 0 ); \
388 if (*begin + *len < offset) { \
389 *begin += *len; \
390 *len = 0; \
392 } while(0)
394 #endif /* CONFIG_PROC_FS */
396 static struct file_operations nvram_fops = {
397 nvram_llseek,
398 nvram_read,
399 nvram_write,
400 NULL, /* No readdir */
401 NULL, /* No poll */
402 nvram_ioctl,
403 NULL, /* No mmap */
404 nvram_open,
405 NULL, /* flush */
406 nvram_release
409 static struct miscdevice nvram_dev = {
410 NVRAM_MINOR,
411 "nvram",
412 &nvram_fops
416 static int __init nvram_init(void)
418 /* First test whether the driver should init at all */
419 if (!CHECK_DRIVER_INIT())
420 return( -ENXIO );
422 printk(KERN_INFO "Non-volatile memory driver v%s\n", NVRAM_VERSION );
423 misc_register( &nvram_dev );
424 create_proc_read_entry("driver/nvram",0,0,nvram_read_proc,NULL);
425 return( 0 );
428 static void __exit nvram_cleanup_module (void)
430 remove_proc_entry( "driver/nvram", 0 );
431 misc_deregister( &nvram_dev );
434 module_init(nvram_init);
435 module_exit(nvram_cleanup_module);
439 * Machine specific functions
443 #if MACH == PC
445 static int pc_check_checksum( void )
447 int i;
448 unsigned short sum = 0;
450 for( i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i )
451 sum += nvram_read_int( i );
452 return( (sum & 0xffff) ==
453 ((nvram_read_int(PC_CKS_LOC) << 8) |
454 nvram_read_int(PC_CKS_LOC+1)) );
457 static void pc_set_checksum( void )
459 int i;
460 unsigned short sum = 0;
462 for( i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i )
463 sum += nvram_read_int( i );
464 nvram_write_int( sum >> 8, PC_CKS_LOC );
465 nvram_write_int( sum & 0xff, PC_CKS_LOC+1 );
468 #ifdef CONFIG_PROC_FS
470 static char *floppy_types[] = {
471 "none", "5.25'' 360k", "5.25'' 1.2M", "3.5'' 720k", "3.5'' 1.44M", "3.5'' 2.88M"
474 static char *gfx_types[] = {
475 "EGA, VGA, ... (with BIOS)",
476 "CGA (40 cols)",
477 "CGA (80 cols)",
478 "monochrome",
481 static int pc_proc_infos( unsigned char *nvram, char *buffer, int *len,
482 off_t *begin, off_t offset, int size )
484 unsigned long flags;
485 int checksum;
486 int type;
488 save_flags(flags);
489 cli();
490 checksum = nvram_check_checksum_int();
491 restore_flags(flags);
493 PRINT_PROC( "Checksum status: %svalid\n", checksum ? "" : "not " );
495 PRINT_PROC( "# floppies : %d\n",
496 (nvram[6] & 1) ? (nvram[6] >> 6) + 1 : 0 );
497 PRINT_PROC( "Floppy 0 type : " );
498 type = nvram[2] >> 4;
499 if (type < sizeof(floppy_types)/sizeof(*floppy_types))
500 PRINT_PROC( "%s\n", floppy_types[type] );
501 else
502 PRINT_PROC( "%d (unknown)\n", type );
503 PRINT_PROC( "Floppy 1 type : " );
504 type = nvram[2] & 0x0f;
505 if (type < sizeof(floppy_types)/sizeof(*floppy_types))
506 PRINT_PROC( "%s\n", floppy_types[type] );
507 else
508 PRINT_PROC( "%d (unknown)\n", type );
510 PRINT_PROC( "HD 0 type : " );
511 type = nvram[4] >> 4;
512 if (type)
513 PRINT_PROC( "%02x\n", type == 0x0f ? nvram[11] : type );
514 else
515 PRINT_PROC( "none\n" );
517 PRINT_PROC( "HD 1 type : " );
518 type = nvram[4] & 0x0f;
519 if (type)
520 PRINT_PROC( "%02x\n", type == 0x0f ? nvram[12] : type );
521 else
522 PRINT_PROC( "none\n" );
524 PRINT_PROC( "HD type 48 data: %d/%d/%d C/H/S, precomp %d, lz %d\n",
525 nvram[18] | (nvram[19] << 8),
526 nvram[20], nvram[25],
527 nvram[21] | (nvram[22] << 8),
528 nvram[23] | (nvram[24] << 8) );
529 PRINT_PROC( "HD type 49 data: %d/%d/%d C/H/S, precomp %d, lz %d\n",
530 nvram[39] | (nvram[40] << 8),
531 nvram[41], nvram[46],
532 nvram[42] | (nvram[43] << 8),
533 nvram[44] | (nvram[45] << 8) );
535 PRINT_PROC( "DOS base memory: %d kB\n", nvram[7] | (nvram[8] << 8) );
536 PRINT_PROC( "Extended memory: %d kB (configured), %d kB (tested)\n",
537 nvram[9] | (nvram[10] << 8),
538 nvram[34] | (nvram[35] << 8) );
540 PRINT_PROC( "Gfx adapter : %s\n", gfx_types[ (nvram[6] >> 4)&3 ] );
542 PRINT_PROC( "FPU : %sinstalled\n",
543 (nvram[6] & 2) ? "" : "not " );
545 return( 1 );
547 #endif
549 #endif /* MACH == PC */
551 #if MACH == ATARI
553 static int atari_check_checksum( void )
555 int i;
556 unsigned char sum = 0;
558 for( i = ATARI_CKS_RANGE_START; i <= ATARI_CKS_RANGE_END; ++i )
559 sum += nvram_read_int( i );
560 return( nvram_read_int( ATARI_CKS_LOC ) == (~sum & 0xff) &&
561 nvram_read_int( ATARI_CKS_LOC+1 ) == (sum & 0xff) );
564 static void atari_set_checksum( void )
566 int i;
567 unsigned char sum = 0;
569 for( i = ATARI_CKS_RANGE_START; i <= ATARI_CKS_RANGE_END; ++i )
570 sum += nvram_read_int( i );
571 nvram_write_int( ~sum, ATARI_CKS_LOC );
572 nvram_write_int( sum, ATARI_CKS_LOC+1 );
575 #ifdef CONFIG_PROC_FS
577 static struct {
578 unsigned char val;
579 char *name;
580 } boot_prefs[] = {
581 { 0x80, "TOS" },
582 { 0x40, "ASV" },
583 { 0x20, "NetBSD (?)" },
584 { 0x10, "Linux" },
585 { 0x00, "unspecified" }
588 static char *languages[] = {
589 "English (US)",
590 "German",
591 "French",
592 "English (UK)",
593 "Spanish",
594 "Italian",
595 "6 (undefined)",
596 "Swiss (French)",
597 "Swiss (German)"
600 static char *dateformat[] = {
601 "MM%cDD%cYY",
602 "DD%cMM%cYY",
603 "YY%cMM%cDD",
604 "YY%cDD%cMM",
605 "4 (undefined)",
606 "5 (undefined)",
607 "6 (undefined)",
608 "7 (undefined)"
611 static char *colors[] = {
612 "2", "4", "16", "256", "65536", "??", "??", "??"
615 #define fieldsize(a) (sizeof(a)/sizeof(*a))
617 static int atari_proc_infos( unsigned char *nvram, char *buffer, int *len,
618 off_t *begin, off_t offset, int size )
620 int checksum = nvram_check_checksum();
621 int i;
622 unsigned vmode;
624 PRINT_PROC( "Checksum status : %svalid\n", checksum ? "" : "not " );
626 PRINT_PROC( "Boot preference : " );
627 for( i = fieldsize(boot_prefs)-1; i >= 0; --i ) {
628 if (nvram[1] == boot_prefs[i].val) {
629 PRINT_PROC( "%s\n", boot_prefs[i].name );
630 break;
633 if (i < 0)
634 PRINT_PROC( "0x%02x (undefined)\n", nvram[1] );
636 PRINT_PROC( "SCSI arbitration : %s\n", (nvram[16] & 0x80) ? "on" : "off" );
637 PRINT_PROC( "SCSI host ID : " );
638 if (nvram[16] & 0x80)
639 PRINT_PROC( "%d\n", nvram[16] & 7 );
640 else
641 PRINT_PROC( "n/a\n" );
643 /* the following entries are defined only for the Falcon */
644 if ((atari_mch_cookie >> 16) != ATARI_MCH_FALCON)
645 return 1;
647 PRINT_PROC( "OS language : " );
648 if (nvram[6] < fieldsize(languages))
649 PRINT_PROC( "%s\n", languages[nvram[6]] );
650 else
651 PRINT_PROC( "%u (undefined)\n", nvram[6] );
652 PRINT_PROC( "Keyboard language: " );
653 if (nvram[7] < fieldsize(languages))
654 PRINT_PROC( "%s\n", languages[nvram[7]] );
655 else
656 PRINT_PROC( "%u (undefined)\n", nvram[7] );
657 PRINT_PROC( "Date format : " );
658 PRINT_PROC( dateformat[nvram[8]&7],
659 nvram[9] ? nvram[9] : '/', nvram[9] ? nvram[9] : '/' );
660 PRINT_PROC( ", %dh clock\n", nvram[8] & 16 ? 24 : 12 );
661 PRINT_PROC( "Boot delay : " );
662 if (nvram[10] == 0)
663 PRINT_PROC( "default" );
664 else
665 PRINT_PROC( "%ds%s\n", nvram[10],
666 nvram[10] < 8 ? ", no memory test" : "" );
668 vmode = (nvram[14] << 8) || nvram[15];
669 PRINT_PROC( "Video mode : %s colors, %d columns, %s %s monitor\n",
670 colors[vmode & 7],
671 vmode & 8 ? 80 : 40,
672 vmode & 16 ? "VGA" : "TV",
673 vmode & 32 ? "PAL" : "NTSC" );
674 PRINT_PROC( " %soverscan, compat. mode %s%s\n",
675 vmode & 64 ? "" : "no ",
676 vmode & 128 ? "on" : "off",
677 vmode & 256 ?
678 (vmode & 16 ? ", line doubling" : ", half screen") : "" );
680 return( 1 );
682 #endif
684 #endif /* MACH == ATARI */
687 * Local variables:
688 * c-indent-level: 4
689 * tab-width: 4
690 * End: