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.
29 * 1.1 Cesar Barros: SMP locking fixes
33 #define NVRAM_VERSION "1.1"
35 #include <linux/module.h>
36 #include <linux/config.h>
41 /* select machine configuration */
42 #if defined(CONFIG_ATARI)
44 #elif defined(__i386__) || defined(__arm__) /* and others?? */
47 #error Cannot build nvram driver for this machine configuration.
53 #define CHECK_DRIVER_INIT() 1
55 /* On PCs, the checksum is built only over bytes 2..31 */
56 #define PC_CKS_RANGE_START 2
57 #define PC_CKS_RANGE_END 31
60 #define mach_check_checksum pc_check_checksum
61 #define mach_set_checksum pc_set_checksum
62 #define mach_proc_infos pc_proc_infos
68 /* Special parameters for RTC in Atari machines */
69 #include <asm/atarihw.h>
70 #include <asm/atariints.h>
71 #define RTC_PORT(x) (TT_RTC_BAS + 2*(x))
72 #define CHECK_DRIVER_INIT() (MACH_IS_ATARI && ATARIHW_PRESENT(TT_CLK))
74 /* On Ataris, the checksum is over all bytes except the checksum bytes
75 * themselves; these are at the very end */
76 #define ATARI_CKS_RANGE_START 0
77 #define ATARI_CKS_RANGE_END 47
78 #define ATARI_CKS_LOC 48
80 #define mach_check_checksum atari_check_checksum
81 #define mach_set_checksum atari_set_checksum
82 #define mach_proc_infos atari_proc_infos
86 /* Note that *all* calls to CMOS_READ and CMOS_WRITE must be done with
87 * rtc_lock held. Due to the index-port/data-port design of the RTC, we
88 * don't want two different things trying to get to it at once. (e.g. the
89 * periodic 11 min sync from time.c vs. this driver.)
92 #include <linux/types.h>
93 #include <linux/errno.h>
94 #include <linux/miscdevice.h>
95 #include <linux/malloc.h>
96 #include <linux/ioport.h>
97 #include <linux/fcntl.h>
98 #include <linux/mc146818rtc.h>
99 #include <linux/nvram.h>
100 #include <linux/init.h>
101 #include <linux/proc_fs.h>
102 #include <linux/spinlock.h>
105 #include <asm/uaccess.h>
106 #include <asm/system.h>
108 extern spinlock_t rtc_lock
;
110 static int nvram_open_cnt
= 0; /* #times opened */
111 static int nvram_open_mode
; /* special open modes */
112 #define NVRAM_WRITE 1 /* opened for writing (exclusive) */
113 #define NVRAM_EXCL 2 /* opened with O_EXCL */
115 #define RTC_FIRST_BYTE 14 /* RTC register number of first NVRAM byte */
116 #define NVRAM_BYTES 50 /* number of NVRAM bytes */
119 static int mach_check_checksum( void );
120 static void mach_set_checksum( void );
121 #ifdef CONFIG_PROC_FS
122 static int mach_proc_infos( unsigned char *contents
, char *buffer
, int *len
,
123 off_t
*begin
, off_t offset
, int size
);
128 * These are the internal NVRAM access functions, which do NOT disable
129 * interrupts and do not check the checksum. Both tasks are left to higher
130 * level function, so they need to be done only once per syscall.
133 static __inline__
unsigned char nvram_read_int( int i
)
135 return( CMOS_READ( RTC_FIRST_BYTE
+i
) );
138 static __inline__
void nvram_write_int( unsigned char c
, int i
)
140 CMOS_WRITE( c
, RTC_FIRST_BYTE
+i
);
143 static __inline__
int nvram_check_checksum_int( void )
145 return( mach_check_checksum() );
148 static __inline__
void nvram_set_checksum_int( void )
156 * These non-internal functions are provided to be called by other parts of
157 * the kernel. It's up to the caller to ensure correct checksum before reading
158 * or after writing (needs to be done only once).
160 * They're only built if CONFIG_ATARI is defined, because Atari drivers use
161 * them. For other configurations (PC), the rest of the kernel can't rely on
162 * them being present (this driver may not be configured at all, or as a
163 * module), so they access config information themselves.
166 unsigned char nvram_read_byte( int i
)
171 spin_lock_irqsave (&rtc_lock
, flags
);
172 c
= nvram_read_int( i
);
173 spin_unlock_irqrestore (&rtc_lock
, flags
);
177 /* This races nicely with trying to read with checksum checking (nvram_read) */
178 void nvram_write_byte( unsigned char c
, int i
)
182 spin_lock_irqsave (&rtc_lock
, flags
);
183 nvram_write_int( c
, i
);
184 spin_unlock_irqrestore (&rtc_lock
, flags
);
187 int nvram_check_checksum( void )
192 spin_lock_irqsave (&rtc_lock
, flags
);
193 rv
= nvram_check_checksum_int();
194 spin_unlock_irqrestore (&rtc_lock
, flags
);
198 void nvram_set_checksum( void )
202 spin_lock_irqsave (&rtc_lock
, flags
);
203 nvram_set_checksum_int();
204 spin_unlock_irqrestore (&rtc_lock
, 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
)
233 char contents
[NVRAM_BYTES
];
237 spin_lock_irq (&rtc_lock
);
239 if (!nvram_check_checksum_int())
242 for (tmp
= contents
; count
-- > 0 && i
< NVRAM_BYTES
; ++i
, ++tmp
)
243 *tmp
= nvram_read_int(i
);
245 spin_unlock_irq (&rtc_lock
);
247 copy_to_user_ret (buf
, contents
, tmp
- contents
, -EFAULT
);
251 return (tmp
- contents
);
254 spin_unlock_irq (&rtc_lock
);
258 static ssize_t
nvram_write(struct file
* file
,
259 const char * buf
, size_t count
, loff_t
*ppos
)
261 char contents
[NVRAM_BYTES
];
265 /* could comebody please help me indent this better? */
266 copy_from_user_ret (contents
, buf
, (NVRAM_BYTES
- i
) < count
?
267 (NVRAM_BYTES
- i
) : count
,
270 spin_lock_irq (&rtc_lock
);
272 if (!nvram_check_checksum_int())
275 for (tmp
= contents
; count
-- > 0 && i
< NVRAM_BYTES
; ++i
, ++tmp
)
276 nvram_write_int (*tmp
, i
);
278 nvram_set_checksum_int();
280 spin_unlock_irq (&rtc_lock
);
284 return (tmp
- contents
);
287 spin_unlock_irq (&rtc_lock
);
291 static int nvram_ioctl( struct inode
*inode
, struct file
*file
,
292 unsigned int cmd
, unsigned long arg
)
298 case NVRAM_INIT
: /* initialize NVRAM contents and checksum */
299 if (!capable(CAP_SYS_ADMIN
))
302 spin_lock_irq (&rtc_lock
);
304 for( i
= 0; i
< NVRAM_BYTES
; ++i
)
305 nvram_write_int( 0, i
);
306 nvram_set_checksum_int();
308 spin_unlock_irq (&rtc_lock
);
311 case NVRAM_SETCKS
: /* just set checksum, contents unchanged
312 * (maybe useful after checksum garbaged
314 if (!capable(CAP_SYS_ADMIN
))
317 spin_lock_irq (&rtc_lock
);
318 nvram_set_checksum_int();
319 spin_unlock_irq (&rtc_lock
);
327 static int nvram_open( struct inode
*inode
, struct file
*file
)
329 if ((nvram_open_cnt
&& (file
->f_flags
& O_EXCL
)) ||
330 (nvram_open_mode
& NVRAM_EXCL
) ||
331 ((file
->f_mode
& 2) && (nvram_open_mode
& NVRAM_WRITE
)))
334 if (file
->f_flags
& O_EXCL
)
335 nvram_open_mode
|= NVRAM_EXCL
;
336 if (file
->f_mode
& 2)
337 nvram_open_mode
|= NVRAM_WRITE
;
342 static int nvram_release( struct inode
*inode
, struct file
*file
)
345 if (file
->f_flags
& O_EXCL
)
346 nvram_open_mode
&= ~NVRAM_EXCL
;
347 if (file
->f_mode
& 2)
348 nvram_open_mode
&= ~NVRAM_WRITE
;
354 #ifndef CONFIG_PROC_FS
355 static int nvram_read_proc( char *buffer
, char **start
, off_t offset
,
356 int size
, int *eof
, void *data
) { return 0; }
359 static int nvram_read_proc( char *buffer
, char **start
, off_t offset
,
360 int size
, int *eof
, void *data
)
362 unsigned char contents
[NVRAM_BYTES
];
366 spin_lock_irq (&rtc_lock
);
367 for( i
= 0; i
< NVRAM_BYTES
; ++i
)
368 contents
[i
] = nvram_read_int( i
);
369 spin_unlock_irq (&rtc_lock
);
371 *eof
= mach_proc_infos( contents
, buffer
, &len
, &begin
, offset
, size
);
373 if (offset
>= begin
+ len
)
375 *start
= buffer
+ (offset
- begin
);
376 return( size
< begin
+ len
- offset
? size
: begin
+ len
- offset
);
380 /* This macro frees the machine specific function from bounds checking and
381 * this like that... */
382 #define PRINT_PROC(fmt,args...) \
384 *len += sprintf( buffer+*len, fmt, ##args ); \
385 if (*begin + *len > offset + size) \
387 if (*begin + *len < offset) { \
393 #endif /* CONFIG_PROC_FS */
395 static struct file_operations nvram_fops
= {
397 llseek
: nvram_llseek
,
402 release
: nvram_release
,
405 static struct miscdevice nvram_dev
= {
412 static int __init
nvram_init(void)
414 /* First test whether the driver should init at all */
415 if (!CHECK_DRIVER_INIT())
418 printk(KERN_INFO
"Non-volatile memory driver v%s\n", NVRAM_VERSION
);
419 misc_register( &nvram_dev
);
420 create_proc_read_entry("driver/nvram",0,0,nvram_read_proc
,NULL
);
424 static void __exit
nvram_cleanup_module (void)
426 remove_proc_entry( "driver/nvram", 0 );
427 misc_deregister( &nvram_dev
);
430 module_init(nvram_init
);
431 module_exit(nvram_cleanup_module
);
435 * Machine specific functions
441 static int pc_check_checksum( void )
444 unsigned short sum
= 0;
446 for( i
= PC_CKS_RANGE_START
; i
<= PC_CKS_RANGE_END
; ++i
)
447 sum
+= nvram_read_int( i
);
448 return( (sum
& 0xffff) ==
449 ((nvram_read_int(PC_CKS_LOC
) << 8) |
450 nvram_read_int(PC_CKS_LOC
+1)) );
453 static void pc_set_checksum( void )
456 unsigned short sum
= 0;
458 for( i
= PC_CKS_RANGE_START
; i
<= PC_CKS_RANGE_END
; ++i
)
459 sum
+= nvram_read_int( i
);
460 nvram_write_int( sum
>> 8, PC_CKS_LOC
);
461 nvram_write_int( sum
& 0xff, PC_CKS_LOC
+1 );
464 #ifdef CONFIG_PROC_FS
466 static char *floppy_types
[] = {
467 "none", "5.25'' 360k", "5.25'' 1.2M", "3.5'' 720k", "3.5'' 1.44M", "3.5'' 2.88M"
470 static char *gfx_types
[] = {
471 "EGA, VGA, ... (with BIOS)",
477 static int pc_proc_infos( unsigned char *nvram
, char *buffer
, int *len
,
478 off_t
*begin
, off_t offset
, int size
)
483 spin_lock_irq (&rtc_lock
);
484 checksum
= nvram_check_checksum_int();
485 spin_unlock_irq (&rtc_lock
);
487 PRINT_PROC( "Checksum status: %svalid\n", checksum
? "" : "not " );
489 PRINT_PROC( "# floppies : %d\n",
490 (nvram
[6] & 1) ? (nvram
[6] >> 6) + 1 : 0 );
491 PRINT_PROC( "Floppy 0 type : " );
492 type
= nvram
[2] >> 4;
493 if (type
< sizeof(floppy_types
)/sizeof(*floppy_types
))
494 PRINT_PROC( "%s\n", floppy_types
[type
] );
496 PRINT_PROC( "%d (unknown)\n", type
);
497 PRINT_PROC( "Floppy 1 type : " );
498 type
= nvram
[2] & 0x0f;
499 if (type
< sizeof(floppy_types
)/sizeof(*floppy_types
))
500 PRINT_PROC( "%s\n", floppy_types
[type
] );
502 PRINT_PROC( "%d (unknown)\n", type
);
504 PRINT_PROC( "HD 0 type : " );
505 type
= nvram
[4] >> 4;
507 PRINT_PROC( "%02x\n", type
== 0x0f ? nvram
[11] : type
);
509 PRINT_PROC( "none\n" );
511 PRINT_PROC( "HD 1 type : " );
512 type
= nvram
[4] & 0x0f;
514 PRINT_PROC( "%02x\n", type
== 0x0f ? nvram
[12] : type
);
516 PRINT_PROC( "none\n" );
518 PRINT_PROC( "HD type 48 data: %d/%d/%d C/H/S, precomp %d, lz %d\n",
519 nvram
[18] | (nvram
[19] << 8),
520 nvram
[20], nvram
[25],
521 nvram
[21] | (nvram
[22] << 8),
522 nvram
[23] | (nvram
[24] << 8) );
523 PRINT_PROC( "HD type 49 data: %d/%d/%d C/H/S, precomp %d, lz %d\n",
524 nvram
[39] | (nvram
[40] << 8),
525 nvram
[41], nvram
[46],
526 nvram
[42] | (nvram
[43] << 8),
527 nvram
[44] | (nvram
[45] << 8) );
529 PRINT_PROC( "DOS base memory: %d kB\n", nvram
[7] | (nvram
[8] << 8) );
530 PRINT_PROC( "Extended memory: %d kB (configured), %d kB (tested)\n",
531 nvram
[9] | (nvram
[10] << 8),
532 nvram
[34] | (nvram
[35] << 8) );
534 PRINT_PROC( "Gfx adapter : %s\n", gfx_types
[ (nvram
[6] >> 4)&3 ] );
536 PRINT_PROC( "FPU : %sinstalled\n",
537 (nvram
[6] & 2) ? "" : "not " );
543 #endif /* MACH == PC */
547 static int atari_check_checksum( void )
550 unsigned char sum
= 0;
552 for( i
= ATARI_CKS_RANGE_START
; i
<= ATARI_CKS_RANGE_END
; ++i
)
553 sum
+= nvram_read_int( i
);
554 return( nvram_read_int( ATARI_CKS_LOC
) == (~sum
& 0xff) &&
555 nvram_read_int( ATARI_CKS_LOC
+1 ) == (sum
& 0xff) );
558 static void atari_set_checksum( void )
561 unsigned char sum
= 0;
563 for( i
= ATARI_CKS_RANGE_START
; i
<= ATARI_CKS_RANGE_END
; ++i
)
564 sum
+= nvram_read_int( i
);
565 nvram_write_int( ~sum
, ATARI_CKS_LOC
);
566 nvram_write_int( sum
, ATARI_CKS_LOC
+1 );
569 #ifdef CONFIG_PROC_FS
577 { 0x20, "NetBSD (?)" },
579 { 0x00, "unspecified" }
582 static char *languages
[] = {
594 static char *dateformat
[] = {
605 static char *colors
[] = {
606 "2", "4", "16", "256", "65536", "??", "??", "??"
609 #define fieldsize(a) (sizeof(a)/sizeof(*a))
611 static int atari_proc_infos( unsigned char *nvram
, char *buffer
, int *len
,
612 off_t
*begin
, off_t offset
, int size
)
614 int checksum
= nvram_check_checksum();
618 PRINT_PROC( "Checksum status : %svalid\n", checksum
? "" : "not " );
620 PRINT_PROC( "Boot preference : " );
621 for( i
= fieldsize(boot_prefs
)-1; i
>= 0; --i
) {
622 if (nvram
[1] == boot_prefs
[i
].val
) {
623 PRINT_PROC( "%s\n", boot_prefs
[i
].name
);
628 PRINT_PROC( "0x%02x (undefined)\n", nvram
[1] );
630 PRINT_PROC( "SCSI arbitration : %s\n", (nvram
[16] & 0x80) ? "on" : "off" );
631 PRINT_PROC( "SCSI host ID : " );
632 if (nvram
[16] & 0x80)
633 PRINT_PROC( "%d\n", nvram
[16] & 7 );
635 PRINT_PROC( "n/a\n" );
637 /* the following entries are defined only for the Falcon */
638 if ((atari_mch_cookie
>> 16) != ATARI_MCH_FALCON
)
641 PRINT_PROC( "OS language : " );
642 if (nvram
[6] < fieldsize(languages
))
643 PRINT_PROC( "%s\n", languages
[nvram
[6]] );
645 PRINT_PROC( "%u (undefined)\n", nvram
[6] );
646 PRINT_PROC( "Keyboard language: " );
647 if (nvram
[7] < fieldsize(languages
))
648 PRINT_PROC( "%s\n", languages
[nvram
[7]] );
650 PRINT_PROC( "%u (undefined)\n", nvram
[7] );
651 PRINT_PROC( "Date format : " );
652 PRINT_PROC( dateformat
[nvram
[8]&7],
653 nvram
[9] ? nvram
[9] : '/', nvram
[9] ? nvram
[9] : '/' );
654 PRINT_PROC( ", %dh clock\n", nvram
[8] & 16 ? 24 : 12 );
655 PRINT_PROC( "Boot delay : " );
657 PRINT_PROC( "default" );
659 PRINT_PROC( "%ds%s\n", nvram
[10],
660 nvram
[10] < 8 ? ", no memory test" : "" );
662 vmode
= (nvram
[14] << 8) || nvram
[15];
663 PRINT_PROC( "Video mode : %s colors, %d columns, %s %s monitor\n",
666 vmode
& 16 ? "VGA" : "TV",
667 vmode
& 32 ? "PAL" : "NTSC" );
668 PRINT_PROC( " %soverscan, compat. mode %s%s\n",
669 vmode
& 64 ? "" : "no ",
670 vmode
& 128 ? "on" : "off",
672 (vmode
& 16 ? ", line doubling" : ", half screen") : "" );
678 #endif /* MACH == ATARI */