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>
38 /* select machine configuration */
39 #if defined(CONFIG_ATARI)
41 #elif defined(__i386__) /* and others?? */
44 #error Cannot build nvram driver for this machine configuration.
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
57 #define mach_check_checksum pc_check_checksum
58 #define mach_set_checksum pc_set_checksum
59 #define mach_proc_infos pc_proc_infos
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
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>
99 #include <linux/proc_fs.h>
103 #include <asm/uaccess.h>
104 #include <asm/system.h>
107 static int nvram_open_cnt
= 0; /* #times opened */
108 static int nvram_open_mode
; /* special open modes */
109 #define NVRAM_WRITE 1 /* opened for writing (exclusive) */
110 #define NVRAM_EXCL 2 /* opened with O_EXCL */
112 #define RTC_FIRST_BYTE 14 /* RTC register number of first NVRAM byte */
113 #define NVRAM_BYTES 50 /* number of NVRAM bytes */
116 static int mach_check_checksum( void );
117 static void mach_set_checksum( void );
118 #ifdef CONFIG_PROC_FS
119 static int mach_proc_infos( unsigned char *contents
, char *buffer
, int *len
,
120 off_t
*begin
, off_t offset
, int size
);
125 * These are the internal NVRAM access functions, which do NOT disable
126 * interrupts and do not check the checksum. Both tasks are left to higher
127 * level function, so they need to be done only once per syscall.
130 static __inline__
unsigned char nvram_read_int( int i
)
132 return( CMOS_READ( RTC_FIRST_BYTE
+i
) );
135 static __inline__
void nvram_write_int( unsigned char c
, int i
)
137 CMOS_WRITE( c
, RTC_FIRST_BYTE
+i
);
140 static __inline__
int nvram_check_checksum_int( void )
142 return( mach_check_checksum() );
145 static __inline__
void nvram_set_checksum_int( void )
153 * These non-internal functions are provided to be called by other parts of
154 * the kernel. It's up to the caller to ensure correct checksum before reading
155 * or after writing (needs to be done only once).
157 * They're only built if CONFIG_ATARI is defined, because Atari drivers use
158 * them. For other configurations (PC), the rest of the kernel can't rely on
159 * them being present (this driver may not be configured at all, or as a
160 * module), so they access config information themselves.
163 unsigned char nvram_read_byte( int i
)
170 c
= nvram_read_int( i
);
171 restore_flags(flags
);
175 void nvram_write_byte( unsigned char c
, int i
)
181 nvram_write_int( c
, i
);
182 restore_flags(flags
);
185 int nvram_check_checksum( void )
192 rv
= nvram_check_checksum_int();
193 restore_flags(flags
);
197 void nvram_set_checksum( void )
203 nvram_set_checksum_int();
204 restore_flags(flags
);
207 #endif /* MACH == ATARI */
211 * The are the file operation function for user access to /dev/nvram
214 static long long nvram_llseek(struct file
*file
,loff_t offset
, int origin
)
221 offset
+= file
->f_pos
;
224 offset
+= NVRAM_BYTES
;
227 return( (offset
>= 0) ? (file
->f_pos
= offset
) : -EINVAL
);
230 static ssize_t
nvram_read(struct file
* file
,
231 char * buf
, size_t count
, loff_t
*ppos
)
243 if (!nvram_check_checksum_int()) {
244 restore_flags(flags
);
248 for( ; count
-- > 0 && i
< NVRAM_BYTES
; ++i
, ++tmp
)
249 put_user( nvram_read_int(i
), tmp
);
252 restore_flags(flags
);
256 static ssize_t
nvram_write(struct file
* file
,
257 const char * buf
, size_t count
, loff_t
*ppos
)
261 const char *tmp
= buf
;
270 if (!nvram_check_checksum_int()) {
271 restore_flags(flags
);
275 for( ; count
-- > 0 && i
< NVRAM_BYTES
; ++i
, ++tmp
) {
277 nvram_write_int( c
, i
);
279 nvram_set_checksum_int();
282 restore_flags(flags
);
286 static int nvram_ioctl( struct inode
*inode
, struct file
*file
,
287 unsigned int cmd
, unsigned long arg
)
294 case NVRAM_INIT
: /* initialize NVRAM contents and checksum */
295 if (!capable(CAP_SYS_ADMIN
))
301 for( i
= 0; i
< NVRAM_BYTES
; ++i
)
302 nvram_write_int( 0, i
);
303 nvram_set_checksum_int();
305 restore_flags(flags
);
308 case NVRAM_SETCKS
: /* just set checksum, contents unchanged
309 * (maybe useful after checksum garbaged
311 if (!capable(CAP_SYS_ADMIN
))
316 nvram_set_checksum_int();
317 restore_flags(flags
);
325 static int nvram_open( struct inode
*inode
, struct file
*file
)
327 if ((nvram_open_cnt
&& (file
->f_flags
& O_EXCL
)) ||
328 (nvram_open_mode
& NVRAM_EXCL
) ||
329 ((file
->f_mode
& 2) && (nvram_open_mode
& NVRAM_WRITE
)))
332 if (file
->f_flags
& O_EXCL
)
333 nvram_open_mode
|= NVRAM_EXCL
;
334 if (file
->f_mode
& 2)
335 nvram_open_mode
|= NVRAM_WRITE
;
341 static int nvram_release( struct inode
*inode
, struct file
*file
)
344 if (file
->f_flags
& O_EXCL
)
345 nvram_open_mode
&= ~NVRAM_EXCL
;
346 if (file
->f_mode
& 2)
347 nvram_open_mode
&= ~NVRAM_WRITE
;
354 #ifdef CONFIG_PROC_FS
356 struct proc_dir_entry
*proc_nvram
;
358 static int nvram_read_proc( char *buffer
, char **start
, off_t offset
,
359 int size
, int *eof
, void *data
)
362 unsigned char contents
[NVRAM_BYTES
];
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
)
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...) \
385 *len += sprintf( buffer+*len, fmt, ##args ); \
386 if (*begin + *len > offset + size) \
388 if (*begin + *len < offset) { \
396 static struct file_operations nvram_fops
= {
400 NULL
, /* No readdir */
409 static struct miscdevice nvram_dev
= {
416 int __init
nvram_init(void)
418 /* First test whether the driver should init at all */
419 if (!CHECK_DRIVER_INIT())
422 printk(KERN_INFO
"Non-volatile memory driver v%s\n", NVRAM_VERSION
);
423 misc_register( &nvram_dev
);
424 #ifdef CONFIG_PROC_FS
425 if ((proc_nvram
= create_proc_entry( "nvram", 0, 0 )))
426 proc_nvram
->read_proc
= nvram_read_proc
;
433 int init_module (void)
435 return( nvram_init() );
438 void cleanup_module (void)
440 #ifdef CONFIG_PROC_FS
442 remove_proc_entry( "nvram", 0 );
444 misc_deregister( &nvram_dev
);
450 * Machine specific functions
456 static int pc_check_checksum( void )
459 unsigned short sum
= 0;
461 for( i
= PC_CKS_RANGE_START
; i
<= PC_CKS_RANGE_END
; ++i
)
462 sum
+= nvram_read_int( i
);
463 return( (sum
& 0xffff) ==
464 ((nvram_read_int(PC_CKS_LOC
) << 8) |
465 nvram_read_int(PC_CKS_LOC
+1)) );
468 static void pc_set_checksum( void )
471 unsigned short sum
= 0;
473 for( i
= PC_CKS_RANGE_START
; i
<= PC_CKS_RANGE_END
; ++i
)
474 sum
+= nvram_read_int( i
);
475 nvram_write_int( sum
>> 8, PC_CKS_LOC
);
476 nvram_write_int( sum
& 0xff, PC_CKS_LOC
+1 );
479 #ifdef CONFIG_PROC_FS
481 static char *floppy_types
[] = {
482 "none", "5.25'' 360k", "5.25'' 1.2M", "3.5'' 720k", "3.5'' 1.44M", "3.5'' 2.88M"
485 static char *gfx_types
[] = {
486 "EGA, VGA, ... (with BIOS)",
492 static int pc_proc_infos( unsigned char *nvram
, char *buffer
, int *len
,
493 off_t
*begin
, off_t offset
, int size
)
501 checksum
= nvram_check_checksum_int();
502 restore_flags(flags
);
504 PRINT_PROC( "Checksum status: %svalid\n", checksum
? "" : "not " );
506 PRINT_PROC( "# floppies : %d\n",
507 (nvram
[6] & 1) ? (nvram
[6] >> 6) + 1 : 0 );
508 PRINT_PROC( "Floppy 0 type : " );
509 type
= nvram
[2] >> 4;
510 if (type
< sizeof(floppy_types
)/sizeof(*floppy_types
))
511 PRINT_PROC( "%s\n", floppy_types
[type
] );
513 PRINT_PROC( "%d (unknown)\n", type
);
514 PRINT_PROC( "Floppy 1 type : " );
515 type
= nvram
[2] & 0x0f;
516 if (type
< sizeof(floppy_types
)/sizeof(*floppy_types
))
517 PRINT_PROC( "%s\n", floppy_types
[type
] );
519 PRINT_PROC( "%d (unknown)\n", type
);
521 PRINT_PROC( "HD 0 type : " );
522 type
= nvram
[4] >> 4;
524 PRINT_PROC( "%02x\n", type
== 0x0f ? nvram
[11] : type
);
526 PRINT_PROC( "none\n" );
528 PRINT_PROC( "HD 1 type : " );
529 type
= nvram
[4] & 0x0f;
531 PRINT_PROC( "%02x\n", type
== 0x0f ? nvram
[12] : type
);
533 PRINT_PROC( "none\n" );
535 PRINT_PROC( "HD type 48 data: %d/%d/%d C/H/S, precomp %d, lz %d\n",
536 nvram
[18] | (nvram
[19] << 8),
537 nvram
[20], nvram
[25],
538 nvram
[21] | (nvram
[22] << 8),
539 nvram
[23] | (nvram
[24] << 8) );
540 PRINT_PROC( "HD type 49 data: %d/%d/%d C/H/S, precomp %d, lz %d\n",
541 nvram
[39] | (nvram
[40] << 8),
542 nvram
[41], nvram
[46],
543 nvram
[42] | (nvram
[43] << 8),
544 nvram
[44] | (nvram
[45] << 8) );
546 PRINT_PROC( "DOS base memory: %d kB\n", nvram
[7] | (nvram
[8] << 8) );
547 PRINT_PROC( "Extended memory: %d kB (configured), %d kB (tested)\n",
548 nvram
[9] | (nvram
[10] << 8),
549 nvram
[34] | (nvram
[35] << 8) );
551 PRINT_PROC( "Gfx adapter : %s\n", gfx_types
[ (nvram
[6] >> 4)&3 ] );
553 PRINT_PROC( "FPU : %sinstalled\n",
554 (nvram
[6] & 2) ? "" : "not " );
560 #endif /* MACH == PC */
564 static int atari_check_checksum( void )
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 return( nvram_read_int( ATARI_CKS_LOC
) == (~sum
& 0xff) &&
572 nvram_read_int( ATARI_CKS_LOC
+1 ) == (sum
& 0xff) );
575 static void atari_set_checksum( void )
578 unsigned char sum
= 0;
580 for( i
= ATARI_CKS_RANGE_START
; i
<= ATARI_CKS_RANGE_END
; ++i
)
581 sum
+= nvram_read_int( i
);
582 nvram_write_int( ~sum
, ATARI_CKS_LOC
);
583 nvram_write_int( sum
, ATARI_CKS_LOC
+1 );
586 #ifdef CONFIG_PROC_FS
594 { 0x20, "NetBSD (?)" },
596 { 0x00, "unspecified" }
599 static char *languages
[] = {
611 static char *dateformat
[] = {
622 static char *colors
[] = {
623 "2", "4", "16", "256", "65536", "??", "??", "??"
626 #define fieldsize(a) (sizeof(a)/sizeof(*a))
628 static int atari_proc_infos( unsigned char *nvram
, char *buffer
, int *len
,
629 off_t
*begin
, off_t offset
, int size
)
631 int checksum
= nvram_check_checksum();
635 PRINT_PROC( "Checksum status : %svalid\n", checksum
? "" : "not " );
637 PRINT_PROC( "Boot preference : " );
638 for( i
= fieldsize(boot_prefs
)-1; i
>= 0; --i
) {
639 if (nvram
[1] == boot_prefs
[i
].val
) {
640 PRINT_PROC( "%s\n", boot_prefs
[i
].name
);
645 PRINT_PROC( "0x%02x (undefined)\n", nvram
[1] );
647 PRINT_PROC( "SCSI arbitration : %s\n", (nvram
[16] & 0x80) ? "on" : "off" );
648 PRINT_PROC( "SCSI host ID : " );
649 if (nvram
[16] & 0x80)
650 PRINT_PROC( "%d\n", nvram
[16] & 7 );
652 PRINT_PROC( "n/a\n" );
654 /* the following entries are defined only for the Falcon */
655 if ((atari_mch_cookie
>> 16) != ATARI_MCH_FALCON
)
658 PRINT_PROC( "OS language : " );
659 if (nvram
[6] < fieldsize(languages
))
660 PRINT_PROC( "%s\n", languages
[nvram
[6]] );
662 PRINT_PROC( "%u (undefined)\n", nvram
[6] );
663 PRINT_PROC( "Keyboard language: " );
664 if (nvram
[7] < fieldsize(languages
))
665 PRINT_PROC( "%s\n", languages
[nvram
[7]] );
667 PRINT_PROC( "%u (undefined)\n", nvram
[7] );
668 PRINT_PROC( "Date format : " );
669 PRINT_PROC( dateformat
[nvram
[8]&7],
670 nvram
[9] ? nvram
[9] : '/', nvram
[9] ? nvram
[9] : '/' );
671 PRINT_PROC( ", %dh clock\n", nvram
[8] & 16 ? 24 : 12 );
672 PRINT_PROC( "Boot delay : " );
674 PRINT_PROC( "default" );
676 PRINT_PROC( "%ds%s\n", nvram
[10],
677 nvram
[10] < 8 ? ", no memory test" : "" );
679 vmode
= (nvram
[14] << 8) || nvram
[15];
680 PRINT_PROC( "Video mode : %s colors, %d columns, %s %s monitor\n",
683 vmode
& 16 ? "VGA" : "TV",
684 vmode
& 32 ? "PAL" : "NTSC" );
685 PRINT_PROC( " %soverscan, compat. mode %s%s\n",
686 vmode
& 64 ? "" : "no ",
687 vmode
& 128 ? "on" : "off",
689 (vmode
& 16 ? ", line doubling" : ", half screen") : "" );
695 #endif /* MACH == ATARI */