11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * CMOS/NV-RAM driver for Linux 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1997 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> 51da177e4SLinus Torvalds * idea by and with help from Richard Jelinek <rj@suse.de> 61da177e4SLinus Torvalds * Portions copyright (c) 2001,2002 Sun Microsystems (thockin@sun.com) 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * This driver allows you to access the contents of the non-volatile memory in 91da177e4SLinus Torvalds * the mc146818rtc.h real-time clock. This chip is built into all PCs and into 101da177e4SLinus Torvalds * many Atari machines. In the former it's called "CMOS-RAM", in the latter 111da177e4SLinus Torvalds * "NVRAM" (NV stands for non-volatile). 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds * The data are supplied as a (seekable) character device, /dev/nvram. The 141da177e4SLinus Torvalds * size of this file is dependent on the controller. The usual size is 114, 151da177e4SLinus Torvalds * the number of freely available bytes in the memory (i.e., not used by the 161da177e4SLinus Torvalds * RTC itself). 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * Checksums over the NVRAM contents are managed by this driver. In case of a 191da177e4SLinus Torvalds * bad checksum, reads and writes return -EIO. The checksum can be initialized 201da177e4SLinus Torvalds * to a sane state either by ioctl(NVRAM_INIT) (clear whole NVRAM) or 211da177e4SLinus Torvalds * ioctl(NVRAM_SETCKS) (doesn't change contents, just makes checksum valid 221da177e4SLinus Torvalds * again; use with care!) 231da177e4SLinus Torvalds * 241da177e4SLinus Torvalds * This file also provides some functions for other parts of the kernel that 251da177e4SLinus Torvalds * want to access the NVRAM: nvram_{read,write,check_checksum,set_checksum}. 261da177e4SLinus Torvalds * Obviously this can be used only if this driver is always configured into 271da177e4SLinus Torvalds * the kernel and is not a module. Since the functions are used by some Atari 281da177e4SLinus Torvalds * drivers, this is the case on the Atari. 291da177e4SLinus Torvalds * 301da177e4SLinus Torvalds * 311da177e4SLinus Torvalds * 1.1 Cesar Barros: SMP locking fixes 321da177e4SLinus Torvalds * added changelog 331da177e4SLinus Torvalds * 1.2 Erik Gilling: Cobalt Networks support 341da177e4SLinus Torvalds * Tim Hockin: general cleanup, Cobalt support 358587b33fSWim Van Sebroeck * 1.3 Wim Van Sebroeck: convert PRINT_PROC to seq_file 361da177e4SLinus Torvalds */ 371da177e4SLinus Torvalds 388587b33fSWim Van Sebroeck #define NVRAM_VERSION "1.3" 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds #include <linux/module.h> 411da177e4SLinus Torvalds #include <linux/nvram.h> 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds #define PC 1 441da177e4SLinus Torvalds #define ATARI 2 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds /* select machine configuration */ 471da177e4SLinus Torvalds #if defined(CONFIG_ATARI) 481da177e4SLinus Torvalds # define MACH ATARI 49971ddcf8SWim Van Sebroeck #elif defined(__i386__) || defined(__x86_64__) || defined(__arm__) /* and ?? */ 501da177e4SLinus Torvalds # define MACH PC 511da177e4SLinus Torvalds #else 521da177e4SLinus Torvalds # error Cannot build nvram driver for this machine configuration. 531da177e4SLinus Torvalds #endif 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds #if MACH == PC 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds /* RTC in a PC */ 581da177e4SLinus Torvalds #define CHECK_DRIVER_INIT() 1 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds /* On PCs, the checksum is built only over bytes 2..31 */ 611da177e4SLinus Torvalds #define PC_CKS_RANGE_START 2 621da177e4SLinus Torvalds #define PC_CKS_RANGE_END 31 631da177e4SLinus Torvalds #define PC_CKS_LOC 32 641da177e4SLinus Torvalds #define NVRAM_BYTES (128-NVRAM_FIRST_BYTE) 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds #define mach_check_checksum pc_check_checksum 671da177e4SLinus Torvalds #define mach_set_checksum pc_set_checksum 681da177e4SLinus Torvalds #define mach_proc_infos pc_proc_infos 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds #endif 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds #if MACH == ATARI 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds /* Special parameters for RTC in Atari machines */ 751da177e4SLinus Torvalds #include <asm/atarihw.h> 761da177e4SLinus Torvalds #include <asm/atariints.h> 771da177e4SLinus Torvalds #define RTC_PORT(x) (TT_RTC_BAS + 2*(x)) 781da177e4SLinus Torvalds #define CHECK_DRIVER_INIT() (MACH_IS_ATARI && ATARIHW_PRESENT(TT_CLK)) 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds #define NVRAM_BYTES 50 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds /* On Ataris, the checksum is over all bytes except the checksum bytes 831da177e4SLinus Torvalds * themselves; these are at the very end */ 841da177e4SLinus Torvalds #define ATARI_CKS_RANGE_START 0 851da177e4SLinus Torvalds #define ATARI_CKS_RANGE_END 47 861da177e4SLinus Torvalds #define ATARI_CKS_LOC 48 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds #define mach_check_checksum atari_check_checksum 891da177e4SLinus Torvalds #define mach_set_checksum atari_set_checksum 901da177e4SLinus Torvalds #define mach_proc_infos atari_proc_infos 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds #endif 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds /* Note that *all* calls to CMOS_READ and CMOS_WRITE must be done with 951da177e4SLinus Torvalds * rtc_lock held. Due to the index-port/data-port design of the RTC, we 961da177e4SLinus Torvalds * don't want two different things trying to get to it at once. (e.g. the 975196d203SMichael Witten * periodic 11 min sync from kernel/time/ntp.c vs. this driver.) 981da177e4SLinus Torvalds */ 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds #include <linux/types.h> 1011da177e4SLinus Torvalds #include <linux/errno.h> 1021da177e4SLinus Torvalds #include <linux/miscdevice.h> 1031da177e4SLinus Torvalds #include <linux/ioport.h> 1041da177e4SLinus Torvalds #include <linux/fcntl.h> 1051da177e4SLinus Torvalds #include <linux/mc146818rtc.h> 1061da177e4SLinus Torvalds #include <linux/init.h> 1071da177e4SLinus Torvalds #include <linux/proc_fs.h> 1088587b33fSWim Van Sebroeck #include <linux/seq_file.h> 1091da177e4SLinus Torvalds #include <linux/spinlock.h> 110971ddcf8SWim Van Sebroeck #include <linux/io.h> 111971ddcf8SWim Van Sebroeck #include <linux/uaccess.h> 112613655faSArnd Bergmann #include <linux/mutex.h> 113b808b1d6SAl Viro #include <linux/pagemap.h> 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds 116613655faSArnd Bergmann static DEFINE_MUTEX(nvram_mutex); 1171da177e4SLinus Torvalds static DEFINE_SPINLOCK(nvram_state_lock); 1181da177e4SLinus Torvalds static int nvram_open_cnt; /* #times opened */ 1191da177e4SLinus Torvalds static int nvram_open_mode; /* special open modes */ 1201da177e4SLinus Torvalds #define NVRAM_WRITE 1 /* opened for writing (exclusive) */ 1211da177e4SLinus Torvalds #define NVRAM_EXCL 2 /* opened with O_EXCL */ 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds static int mach_check_checksum(void); 1241da177e4SLinus Torvalds static void mach_set_checksum(void); 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 1278587b33fSWim Van Sebroeck static void mach_proc_infos(unsigned char *contents, struct seq_file *seq, 1288587b33fSWim Van Sebroeck void *offset); 1291da177e4SLinus Torvalds #endif 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds /* 1321da177e4SLinus Torvalds * These functions are provided to be called internally or by other parts of 1331da177e4SLinus Torvalds * the kernel. It's up to the caller to ensure correct checksum before reading 1341da177e4SLinus Torvalds * or after writing (needs to be done only once). 1351da177e4SLinus Torvalds * 1361da177e4SLinus Torvalds * It is worth noting that these functions all access bytes of general 1371da177e4SLinus Torvalds * purpose memory in the NVRAM - that is to say, they all add the 1381da177e4SLinus Torvalds * NVRAM_FIRST_BYTE offset. Pass them offsets into NVRAM as if you did not 1391da177e4SLinus Torvalds * know about the RTC cruft. 1401da177e4SLinus Torvalds */ 1411da177e4SLinus Torvalds 142971ddcf8SWim Van Sebroeck unsigned char __nvram_read_byte(int i) 1431da177e4SLinus Torvalds { 1441da177e4SLinus Torvalds return CMOS_READ(NVRAM_FIRST_BYTE + i); 1451da177e4SLinus Torvalds } 146971ddcf8SWim Van Sebroeck EXPORT_SYMBOL(__nvram_read_byte); 1471da177e4SLinus Torvalds 148971ddcf8SWim Van Sebroeck unsigned char nvram_read_byte(int i) 1491da177e4SLinus Torvalds { 1501da177e4SLinus Torvalds unsigned long flags; 1511da177e4SLinus Torvalds unsigned char c; 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds spin_lock_irqsave(&rtc_lock, flags); 1541da177e4SLinus Torvalds c = __nvram_read_byte(i); 1551da177e4SLinus Torvalds spin_unlock_irqrestore(&rtc_lock, flags); 1561da177e4SLinus Torvalds return c; 1571da177e4SLinus Torvalds } 158971ddcf8SWim Van Sebroeck EXPORT_SYMBOL(nvram_read_byte); 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds /* This races nicely with trying to read with checksum checking (nvram_read) */ 161971ddcf8SWim Van Sebroeck void __nvram_write_byte(unsigned char c, int i) 1621da177e4SLinus Torvalds { 1631da177e4SLinus Torvalds CMOS_WRITE(c, NVRAM_FIRST_BYTE + i); 1641da177e4SLinus Torvalds } 165971ddcf8SWim Van Sebroeck EXPORT_SYMBOL(__nvram_write_byte); 1661da177e4SLinus Torvalds 167971ddcf8SWim Van Sebroeck void nvram_write_byte(unsigned char c, int i) 1681da177e4SLinus Torvalds { 1691da177e4SLinus Torvalds unsigned long flags; 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds spin_lock_irqsave(&rtc_lock, flags); 1721da177e4SLinus Torvalds __nvram_write_byte(c, i); 1731da177e4SLinus Torvalds spin_unlock_irqrestore(&rtc_lock, flags); 1741da177e4SLinus Torvalds } 175971ddcf8SWim Van Sebroeck EXPORT_SYMBOL(nvram_write_byte); 1761da177e4SLinus Torvalds 177971ddcf8SWim Van Sebroeck int __nvram_check_checksum(void) 1781da177e4SLinus Torvalds { 1791da177e4SLinus Torvalds return mach_check_checksum(); 1801da177e4SLinus Torvalds } 181971ddcf8SWim Van Sebroeck EXPORT_SYMBOL(__nvram_check_checksum); 1821da177e4SLinus Torvalds 183971ddcf8SWim Van Sebroeck int nvram_check_checksum(void) 1841da177e4SLinus Torvalds { 1851da177e4SLinus Torvalds unsigned long flags; 1861da177e4SLinus Torvalds int rv; 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds spin_lock_irqsave(&rtc_lock, flags); 1891da177e4SLinus Torvalds rv = __nvram_check_checksum(); 1901da177e4SLinus Torvalds spin_unlock_irqrestore(&rtc_lock, flags); 1911da177e4SLinus Torvalds return rv; 1921da177e4SLinus Torvalds } 193971ddcf8SWim Van Sebroeck EXPORT_SYMBOL(nvram_check_checksum); 1941da177e4SLinus Torvalds 195971ddcf8SWim Van Sebroeck static void __nvram_set_checksum(void) 1961da177e4SLinus Torvalds { 1971da177e4SLinus Torvalds mach_set_checksum(); 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds 200681ea4b9SAdrian Bunk #if 0 201971ddcf8SWim Van Sebroeck void nvram_set_checksum(void) 2021da177e4SLinus Torvalds { 2031da177e4SLinus Torvalds unsigned long flags; 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds spin_lock_irqsave(&rtc_lock, flags); 2061da177e4SLinus Torvalds __nvram_set_checksum(); 2071da177e4SLinus Torvalds spin_unlock_irqrestore(&rtc_lock, flags); 2081da177e4SLinus Torvalds } 209681ea4b9SAdrian Bunk #endif /* 0 */ 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds /* 2121da177e4SLinus Torvalds * The are the file operation function for user access to /dev/nvram 2131da177e4SLinus Torvalds */ 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds static loff_t nvram_llseek(struct file *file, loff_t offset, int origin) 2161da177e4SLinus Torvalds { 217b808b1d6SAl Viro return generic_file_llseek_size(file, offset, origin, MAX_LFS_FILESIZE, 218b808b1d6SAl Viro NVRAM_BYTES); 2191da177e4SLinus Torvalds } 2201da177e4SLinus Torvalds 221971ddcf8SWim Van Sebroeck static ssize_t nvram_read(struct file *file, char __user *buf, 222971ddcf8SWim Van Sebroeck size_t count, loff_t *ppos) 2231da177e4SLinus Torvalds { 2241da177e4SLinus Torvalds unsigned char contents[NVRAM_BYTES]; 2251da177e4SLinus Torvalds unsigned i = *ppos; 2261da177e4SLinus Torvalds unsigned char *tmp; 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds spin_lock_irq(&rtc_lock); 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds if (!__nvram_check_checksum()) 2311da177e4SLinus Torvalds goto checksum_err; 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds for (tmp = contents; count-- > 0 && i < NVRAM_BYTES; ++i, ++tmp) 2341da177e4SLinus Torvalds *tmp = __nvram_read_byte(i); 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds if (copy_to_user(buf, contents, tmp - contents)) 2391da177e4SLinus Torvalds return -EFAULT; 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds *ppos = i; 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds return tmp - contents; 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds checksum_err: 2461da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 2471da177e4SLinus Torvalds return -EIO; 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds 250971ddcf8SWim Van Sebroeck static ssize_t nvram_write(struct file *file, const char __user *buf, 251971ddcf8SWim Van Sebroeck size_t count, loff_t *ppos) 2521da177e4SLinus Torvalds { 2531da177e4SLinus Torvalds unsigned char contents[NVRAM_BYTES]; 2541da177e4SLinus Torvalds unsigned i = *ppos; 2551da177e4SLinus Torvalds unsigned char *tmp; 2561da177e4SLinus Torvalds 257a01c7800SH. Peter Anvin if (i >= NVRAM_BYTES) 258a01c7800SH. Peter Anvin return 0; /* Past EOF */ 259a01c7800SH. Peter Anvin 260a01c7800SH. Peter Anvin if (count > NVRAM_BYTES - i) 261a01c7800SH. Peter Anvin count = NVRAM_BYTES - i; 262a01c7800SH. Peter Anvin if (count > NVRAM_BYTES) 263a01c7800SH. Peter Anvin return -EFAULT; /* Can't happen, but prove it to gcc */ 264a01c7800SH. Peter Anvin 265a01c7800SH. Peter Anvin if (copy_from_user(contents, buf, count)) 2661da177e4SLinus Torvalds return -EFAULT; 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds spin_lock_irq(&rtc_lock); 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds if (!__nvram_check_checksum()) 2711da177e4SLinus Torvalds goto checksum_err; 2721da177e4SLinus Torvalds 273a01c7800SH. Peter Anvin for (tmp = contents; count--; ++i, ++tmp) 2741da177e4SLinus Torvalds __nvram_write_byte(*tmp, i); 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds __nvram_set_checksum(); 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds *ppos = i; 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds return tmp - contents; 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds checksum_err: 2851da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 2861da177e4SLinus Torvalds return -EIO; 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds 28955929332SArnd Bergmann static long nvram_ioctl(struct file *file, unsigned int cmd, 29055929332SArnd Bergmann unsigned long arg) 2911da177e4SLinus Torvalds { 2921da177e4SLinus Torvalds int i; 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds switch (cmd) { 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds case NVRAM_INIT: 2971da177e4SLinus Torvalds /* initialize NVRAM contents and checksum */ 2981da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 2991da177e4SLinus Torvalds return -EACCES; 3001da177e4SLinus Torvalds 301613655faSArnd Bergmann mutex_lock(&nvram_mutex); 3021da177e4SLinus Torvalds spin_lock_irq(&rtc_lock); 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds for (i = 0; i < NVRAM_BYTES; ++i) 3051da177e4SLinus Torvalds __nvram_write_byte(0, i); 3061da177e4SLinus Torvalds __nvram_set_checksum(); 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 309613655faSArnd Bergmann mutex_unlock(&nvram_mutex); 3101da177e4SLinus Torvalds return 0; 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds case NVRAM_SETCKS: 3131da177e4SLinus Torvalds /* just set checksum, contents unchanged (maybe useful after 3141da177e4SLinus Torvalds * checksum garbaged somehow...) */ 3151da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 3161da177e4SLinus Torvalds return -EACCES; 3171da177e4SLinus Torvalds 318613655faSArnd Bergmann mutex_lock(&nvram_mutex); 3191da177e4SLinus Torvalds spin_lock_irq(&rtc_lock); 3201da177e4SLinus Torvalds __nvram_set_checksum(); 3211da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 322613655faSArnd Bergmann mutex_unlock(&nvram_mutex); 3231da177e4SLinus Torvalds return 0; 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds default: 3261da177e4SLinus Torvalds return -ENOTTY; 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds } 3291da177e4SLinus Torvalds 330971ddcf8SWim Van Sebroeck static int nvram_open(struct inode *inode, struct file *file) 3311da177e4SLinus Torvalds { 3321da177e4SLinus Torvalds spin_lock(&nvram_state_lock); 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds if ((nvram_open_cnt && (file->f_flags & O_EXCL)) || 3351da177e4SLinus Torvalds (nvram_open_mode & NVRAM_EXCL) || 336aeb5d727SAl Viro ((file->f_mode & FMODE_WRITE) && (nvram_open_mode & NVRAM_WRITE))) { 3371da177e4SLinus Torvalds spin_unlock(&nvram_state_lock); 3381da177e4SLinus Torvalds return -EBUSY; 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds if (file->f_flags & O_EXCL) 3421da177e4SLinus Torvalds nvram_open_mode |= NVRAM_EXCL; 343aeb5d727SAl Viro if (file->f_mode & FMODE_WRITE) 3441da177e4SLinus Torvalds nvram_open_mode |= NVRAM_WRITE; 3451da177e4SLinus Torvalds nvram_open_cnt++; 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds spin_unlock(&nvram_state_lock); 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds return 0; 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds 352971ddcf8SWim Van Sebroeck static int nvram_release(struct inode *inode, struct file *file) 3531da177e4SLinus Torvalds { 3541da177e4SLinus Torvalds spin_lock(&nvram_state_lock); 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds nvram_open_cnt--; 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds /* if only one instance is open, clear the EXCL bit */ 3591da177e4SLinus Torvalds if (nvram_open_mode & NVRAM_EXCL) 3601da177e4SLinus Torvalds nvram_open_mode &= ~NVRAM_EXCL; 361aeb5d727SAl Viro if (file->f_mode & FMODE_WRITE) 3621da177e4SLinus Torvalds nvram_open_mode &= ~NVRAM_WRITE; 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds spin_unlock(&nvram_state_lock); 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds return 0; 3671da177e4SLinus Torvalds } 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds #ifndef CONFIG_PROC_FS 3708587b33fSWim Van Sebroeck static int nvram_add_proc_fs(void) 3711da177e4SLinus Torvalds { 3721da177e4SLinus Torvalds return 0; 3731da177e4SLinus Torvalds } 3748587b33fSWim Van Sebroeck 3751da177e4SLinus Torvalds #else 3761da177e4SLinus Torvalds 3778587b33fSWim Van Sebroeck static int nvram_proc_read(struct seq_file *seq, void *offset) 3781da177e4SLinus Torvalds { 3791da177e4SLinus Torvalds unsigned char contents[NVRAM_BYTES]; 3808587b33fSWim Van Sebroeck int i = 0; 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds spin_lock_irq(&rtc_lock); 3831da177e4SLinus Torvalds for (i = 0; i < NVRAM_BYTES; ++i) 3841da177e4SLinus Torvalds contents[i] = __nvram_read_byte(i); 3851da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 3861da177e4SLinus Torvalds 3878587b33fSWim Van Sebroeck mach_proc_infos(contents, seq, offset); 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds return 0; 3901da177e4SLinus Torvalds } 3911da177e4SLinus Torvalds 3928587b33fSWim Van Sebroeck static int nvram_proc_open(struct inode *inode, struct file *file) 3938587b33fSWim Van Sebroeck { 3948587b33fSWim Van Sebroeck return single_open(file, nvram_proc_read, NULL); 3958587b33fSWim Van Sebroeck } 3968587b33fSWim Van Sebroeck 3978587b33fSWim Van Sebroeck static const struct file_operations nvram_proc_fops = { 3988587b33fSWim Van Sebroeck .owner = THIS_MODULE, 3998587b33fSWim Van Sebroeck .open = nvram_proc_open, 4008587b33fSWim Van Sebroeck .read = seq_read, 4018587b33fSWim Van Sebroeck .llseek = seq_lseek, 4028587b33fSWim Van Sebroeck .release = single_release, 4038587b33fSWim Van Sebroeck }; 4048587b33fSWim Van Sebroeck 4058587b33fSWim Van Sebroeck static int nvram_add_proc_fs(void) 4068587b33fSWim Van Sebroeck { 4078587b33fSWim Van Sebroeck if (!proc_create("driver/nvram", 0, NULL, &nvram_proc_fops)) 4088587b33fSWim Van Sebroeck return -ENOMEM; 4098587b33fSWim Van Sebroeck return 0; 4108587b33fSWim Van Sebroeck } 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 4131da177e4SLinus Torvalds 41462322d25SArjan van de Ven static const struct file_operations nvram_fops = { 4151da177e4SLinus Torvalds .owner = THIS_MODULE, 4161da177e4SLinus Torvalds .llseek = nvram_llseek, 4171da177e4SLinus Torvalds .read = nvram_read, 4181da177e4SLinus Torvalds .write = nvram_write, 41955929332SArnd Bergmann .unlocked_ioctl = nvram_ioctl, 4201da177e4SLinus Torvalds .open = nvram_open, 4211da177e4SLinus Torvalds .release = nvram_release, 4221da177e4SLinus Torvalds }; 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds static struct miscdevice nvram_dev = { 4251da177e4SLinus Torvalds NVRAM_MINOR, 4261da177e4SLinus Torvalds "nvram", 4271da177e4SLinus Torvalds &nvram_fops 4281da177e4SLinus Torvalds }; 4291da177e4SLinus Torvalds 430971ddcf8SWim Van Sebroeck static int __init nvram_init(void) 4311da177e4SLinus Torvalds { 4321da177e4SLinus Torvalds int ret; 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds /* First test whether the driver should init at all */ 4351da177e4SLinus Torvalds if (!CHECK_DRIVER_INIT()) 436e945b568SGeert Uytterhoeven return -ENODEV; 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds ret = misc_register(&nvram_dev); 4391da177e4SLinus Torvalds if (ret) { 4401da177e4SLinus Torvalds printk(KERN_ERR "nvram: can't misc_register on minor=%d\n", 4411da177e4SLinus Torvalds NVRAM_MINOR); 4421da177e4SLinus Torvalds goto out; 4431da177e4SLinus Torvalds } 4448587b33fSWim Van Sebroeck ret = nvram_add_proc_fs(); 4458587b33fSWim Van Sebroeck if (ret) { 4461da177e4SLinus Torvalds printk(KERN_ERR "nvram: can't create /proc/driver/nvram\n"); 4471da177e4SLinus Torvalds goto outmisc; 4481da177e4SLinus Torvalds } 4491da177e4SLinus Torvalds ret = 0; 4501da177e4SLinus Torvalds printk(KERN_INFO "Non-volatile memory driver v" NVRAM_VERSION "\n"); 4511da177e4SLinus Torvalds out: 4521da177e4SLinus Torvalds return ret; 4531da177e4SLinus Torvalds outmisc: 4541da177e4SLinus Torvalds misc_deregister(&nvram_dev); 4551da177e4SLinus Torvalds goto out; 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds 458971ddcf8SWim Van Sebroeck static void __exit nvram_cleanup_module(void) 4591da177e4SLinus Torvalds { 4601da177e4SLinus Torvalds remove_proc_entry("driver/nvram", NULL); 4611da177e4SLinus Torvalds misc_deregister(&nvram_dev); 4621da177e4SLinus Torvalds } 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds module_init(nvram_init); 4651da177e4SLinus Torvalds module_exit(nvram_cleanup_module); 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds /* 4681da177e4SLinus Torvalds * Machine specific functions 4691da177e4SLinus Torvalds */ 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds #if MACH == PC 4721da177e4SLinus Torvalds 473971ddcf8SWim Van Sebroeck static int pc_check_checksum(void) 4741da177e4SLinus Torvalds { 4751da177e4SLinus Torvalds int i; 4761da177e4SLinus Torvalds unsigned short sum = 0; 4771da177e4SLinus Torvalds unsigned short expect; 4781da177e4SLinus Torvalds 4791da177e4SLinus Torvalds for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i) 4801da177e4SLinus Torvalds sum += __nvram_read_byte(i); 4811da177e4SLinus Torvalds expect = __nvram_read_byte(PC_CKS_LOC)<<8 | 4821da177e4SLinus Torvalds __nvram_read_byte(PC_CKS_LOC+1); 483971ddcf8SWim Van Sebroeck return (sum & 0xffff) == expect; 4841da177e4SLinus Torvalds } 4851da177e4SLinus Torvalds 486971ddcf8SWim Van Sebroeck static void pc_set_checksum(void) 4871da177e4SLinus Torvalds { 4881da177e4SLinus Torvalds int i; 4891da177e4SLinus Torvalds unsigned short sum = 0; 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i) 4921da177e4SLinus Torvalds sum += __nvram_read_byte(i); 4931da177e4SLinus Torvalds __nvram_write_byte(sum >> 8, PC_CKS_LOC); 4941da177e4SLinus Torvalds __nvram_write_byte(sum & 0xff, PC_CKS_LOC + 1); 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 4981da177e4SLinus Torvalds 499*a116eaf1SLABBE Corentin static const char * const floppy_types[] = { 5001da177e4SLinus Torvalds "none", "5.25'' 360k", "5.25'' 1.2M", "3.5'' 720k", "3.5'' 1.44M", 5011da177e4SLinus Torvalds "3.5'' 2.88M", "3.5'' 2.88M" 5021da177e4SLinus Torvalds }; 5031da177e4SLinus Torvalds 504*a116eaf1SLABBE Corentin static const char * const gfx_types[] = { 5051da177e4SLinus Torvalds "EGA, VGA, ... (with BIOS)", 5061da177e4SLinus Torvalds "CGA (40 cols)", 5071da177e4SLinus Torvalds "CGA (80 cols)", 5081da177e4SLinus Torvalds "monochrome", 5091da177e4SLinus Torvalds }; 5101da177e4SLinus Torvalds 5118587b33fSWim Van Sebroeck static void pc_proc_infos(unsigned char *nvram, struct seq_file *seq, 5128587b33fSWim Van Sebroeck void *offset) 5131da177e4SLinus Torvalds { 5141da177e4SLinus Torvalds int checksum; 5151da177e4SLinus Torvalds int type; 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds spin_lock_irq(&rtc_lock); 5181da177e4SLinus Torvalds checksum = __nvram_check_checksum(); 5191da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 5201da177e4SLinus Torvalds 5218587b33fSWim Van Sebroeck seq_printf(seq, "Checksum status: %svalid\n", checksum ? "" : "not "); 5221da177e4SLinus Torvalds 5238587b33fSWim Van Sebroeck seq_printf(seq, "# floppies : %d\n", 5241da177e4SLinus Torvalds (nvram[6] & 1) ? (nvram[6] >> 6) + 1 : 0); 5258587b33fSWim Van Sebroeck seq_printf(seq, "Floppy 0 type : "); 5261da177e4SLinus Torvalds type = nvram[2] >> 4; 527fe971071STobias Klauser if (type < ARRAY_SIZE(floppy_types)) 5288587b33fSWim Van Sebroeck seq_printf(seq, "%s\n", floppy_types[type]); 5291da177e4SLinus Torvalds else 5308587b33fSWim Van Sebroeck seq_printf(seq, "%d (unknown)\n", type); 5318587b33fSWim Van Sebroeck seq_printf(seq, "Floppy 1 type : "); 5321da177e4SLinus Torvalds type = nvram[2] & 0x0f; 533fe971071STobias Klauser if (type < ARRAY_SIZE(floppy_types)) 5348587b33fSWim Van Sebroeck seq_printf(seq, "%s\n", floppy_types[type]); 5351da177e4SLinus Torvalds else 5368587b33fSWim Van Sebroeck seq_printf(seq, "%d (unknown)\n", type); 5371da177e4SLinus Torvalds 5388587b33fSWim Van Sebroeck seq_printf(seq, "HD 0 type : "); 5391da177e4SLinus Torvalds type = nvram[4] >> 4; 5401da177e4SLinus Torvalds if (type) 5418587b33fSWim Van Sebroeck seq_printf(seq, "%02x\n", type == 0x0f ? nvram[11] : type); 5421da177e4SLinus Torvalds else 5438587b33fSWim Van Sebroeck seq_printf(seq, "none\n"); 5441da177e4SLinus Torvalds 5458587b33fSWim Van Sebroeck seq_printf(seq, "HD 1 type : "); 5461da177e4SLinus Torvalds type = nvram[4] & 0x0f; 5471da177e4SLinus Torvalds if (type) 5488587b33fSWim Van Sebroeck seq_printf(seq, "%02x\n", type == 0x0f ? nvram[12] : type); 5491da177e4SLinus Torvalds else 5508587b33fSWim Van Sebroeck seq_printf(seq, "none\n"); 5511da177e4SLinus Torvalds 5528587b33fSWim Van Sebroeck seq_printf(seq, "HD type 48 data: %d/%d/%d C/H/S, precomp %d, lz %d\n", 5531da177e4SLinus Torvalds nvram[18] | (nvram[19] << 8), 5541da177e4SLinus Torvalds nvram[20], nvram[25], 5551da177e4SLinus Torvalds nvram[21] | (nvram[22] << 8), nvram[23] | (nvram[24] << 8)); 5568587b33fSWim Van Sebroeck seq_printf(seq, "HD type 49 data: %d/%d/%d C/H/S, precomp %d, lz %d\n", 5571da177e4SLinus Torvalds nvram[39] | (nvram[40] << 8), 5581da177e4SLinus Torvalds nvram[41], nvram[46], 5591da177e4SLinus Torvalds nvram[42] | (nvram[43] << 8), nvram[44] | (nvram[45] << 8)); 5601da177e4SLinus Torvalds 5618587b33fSWim Van Sebroeck seq_printf(seq, "DOS base memory: %d kB\n", nvram[7] | (nvram[8] << 8)); 5628587b33fSWim Van Sebroeck seq_printf(seq, "Extended memory: %d kB (configured), %d kB (tested)\n", 5631da177e4SLinus Torvalds nvram[9] | (nvram[10] << 8), nvram[34] | (nvram[35] << 8)); 5641da177e4SLinus Torvalds 5658587b33fSWim Van Sebroeck seq_printf(seq, "Gfx adapter : %s\n", 5668587b33fSWim Van Sebroeck gfx_types[(nvram[6] >> 4) & 3]); 5671da177e4SLinus Torvalds 5688587b33fSWim Van Sebroeck seq_printf(seq, "FPU : %sinstalled\n", 5691da177e4SLinus Torvalds (nvram[6] & 2) ? "" : "not "); 5701da177e4SLinus Torvalds 5718587b33fSWim Van Sebroeck return; 5721da177e4SLinus Torvalds } 5731da177e4SLinus Torvalds #endif 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds #endif /* MACH == PC */ 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds #if MACH == ATARI 5781da177e4SLinus Torvalds 579971ddcf8SWim Van Sebroeck static int atari_check_checksum(void) 5801da177e4SLinus Torvalds { 5811da177e4SLinus Torvalds int i; 5821da177e4SLinus Torvalds unsigned char sum = 0; 5831da177e4SLinus Torvalds 5841da177e4SLinus Torvalds for (i = ATARI_CKS_RANGE_START; i <= ATARI_CKS_RANGE_END; ++i) 5851da177e4SLinus Torvalds sum += __nvram_read_byte(i); 586971ddcf8SWim Van Sebroeck return (__nvram_read_byte(ATARI_CKS_LOC) == (~sum & 0xff)) && 587971ddcf8SWim Van Sebroeck (__nvram_read_byte(ATARI_CKS_LOC + 1) == (sum & 0xff)); 5881da177e4SLinus Torvalds } 5891da177e4SLinus Torvalds 590971ddcf8SWim Van Sebroeck static void atari_set_checksum(void) 5911da177e4SLinus Torvalds { 5921da177e4SLinus Torvalds int i; 5931da177e4SLinus Torvalds unsigned char sum = 0; 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds for (i = ATARI_CKS_RANGE_START; i <= ATARI_CKS_RANGE_END; ++i) 5961da177e4SLinus Torvalds sum += __nvram_read_byte(i); 5971da177e4SLinus Torvalds __nvram_write_byte(~sum, ATARI_CKS_LOC); 5981da177e4SLinus Torvalds __nvram_write_byte(sum, ATARI_CKS_LOC + 1); 5991da177e4SLinus Torvalds } 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds static struct { 6041da177e4SLinus Torvalds unsigned char val; 605*a116eaf1SLABBE Corentin const char *name; 6061da177e4SLinus Torvalds } boot_prefs[] = { 6071da177e4SLinus Torvalds { 0x80, "TOS" }, 6081da177e4SLinus Torvalds { 0x40, "ASV" }, 6091da177e4SLinus Torvalds { 0x20, "NetBSD (?)" }, 6101da177e4SLinus Torvalds { 0x10, "Linux" }, 6111da177e4SLinus Torvalds { 0x00, "unspecified" } 6121da177e4SLinus Torvalds }; 6131da177e4SLinus Torvalds 614*a116eaf1SLABBE Corentin static const char * const languages[] = { 6151da177e4SLinus Torvalds "English (US)", 6161da177e4SLinus Torvalds "German", 6171da177e4SLinus Torvalds "French", 6181da177e4SLinus Torvalds "English (UK)", 6191da177e4SLinus Torvalds "Spanish", 6201da177e4SLinus Torvalds "Italian", 6211da177e4SLinus Torvalds "6 (undefined)", 6221da177e4SLinus Torvalds "Swiss (French)", 6231da177e4SLinus Torvalds "Swiss (German)" 6241da177e4SLinus Torvalds }; 6251da177e4SLinus Torvalds 626*a116eaf1SLABBE Corentin static const char * const dateformat[] = { 6271da177e4SLinus Torvalds "MM%cDD%cYY", 6281da177e4SLinus Torvalds "DD%cMM%cYY", 6291da177e4SLinus Torvalds "YY%cMM%cDD", 6301da177e4SLinus Torvalds "YY%cDD%cMM", 6311da177e4SLinus Torvalds "4 (undefined)", 6321da177e4SLinus Torvalds "5 (undefined)", 6331da177e4SLinus Torvalds "6 (undefined)", 6341da177e4SLinus Torvalds "7 (undefined)" 6351da177e4SLinus Torvalds }; 6361da177e4SLinus Torvalds 637*a116eaf1SLABBE Corentin static const char * const colors[] = { 6381da177e4SLinus Torvalds "2", "4", "16", "256", "65536", "??", "??", "??" 6391da177e4SLinus Torvalds }; 6401da177e4SLinus Torvalds 6418587b33fSWim Van Sebroeck static void atari_proc_infos(unsigned char *nvram, struct seq_file *seq, 6428587b33fSWim Van Sebroeck void *offset) 6431da177e4SLinus Torvalds { 6441da177e4SLinus Torvalds int checksum = nvram_check_checksum(); 6451da177e4SLinus Torvalds int i; 6461da177e4SLinus Torvalds unsigned vmode; 6471da177e4SLinus Torvalds 6488587b33fSWim Van Sebroeck seq_printf(seq, "Checksum status : %svalid\n", checksum ? "" : "not "); 6491da177e4SLinus Torvalds 6508587b33fSWim Van Sebroeck seq_printf(seq, "Boot preference : "); 651fe971071STobias Klauser for (i = ARRAY_SIZE(boot_prefs) - 1; i >= 0; --i) { 6521da177e4SLinus Torvalds if (nvram[1] == boot_prefs[i].val) { 6538587b33fSWim Van Sebroeck seq_printf(seq, "%s\n", boot_prefs[i].name); 6541da177e4SLinus Torvalds break; 6551da177e4SLinus Torvalds } 6561da177e4SLinus Torvalds } 6571da177e4SLinus Torvalds if (i < 0) 6588587b33fSWim Van Sebroeck seq_printf(seq, "0x%02x (undefined)\n", nvram[1]); 6591da177e4SLinus Torvalds 6608587b33fSWim Van Sebroeck seq_printf(seq, "SCSI arbitration : %s\n", 6611da177e4SLinus Torvalds (nvram[16] & 0x80) ? "on" : "off"); 6628587b33fSWim Van Sebroeck seq_printf(seq, "SCSI host ID : "); 6631da177e4SLinus Torvalds if (nvram[16] & 0x80) 6648587b33fSWim Van Sebroeck seq_printf(seq, "%d\n", nvram[16] & 7); 6651da177e4SLinus Torvalds else 6668587b33fSWim Van Sebroeck seq_printf(seq, "n/a\n"); 6671da177e4SLinus Torvalds 6681da177e4SLinus Torvalds /* the following entries are defined only for the Falcon */ 6691da177e4SLinus Torvalds if ((atari_mch_cookie >> 16) != ATARI_MCH_FALCON) 6708587b33fSWim Van Sebroeck return; 6711da177e4SLinus Torvalds 6728587b33fSWim Van Sebroeck seq_printf(seq, "OS language : "); 673fe971071STobias Klauser if (nvram[6] < ARRAY_SIZE(languages)) 6748587b33fSWim Van Sebroeck seq_printf(seq, "%s\n", languages[nvram[6]]); 6751da177e4SLinus Torvalds else 6768587b33fSWim Van Sebroeck seq_printf(seq, "%u (undefined)\n", nvram[6]); 6778587b33fSWim Van Sebroeck seq_printf(seq, "Keyboard language: "); 678fe971071STobias Klauser if (nvram[7] < ARRAY_SIZE(languages)) 6798587b33fSWim Van Sebroeck seq_printf(seq, "%s\n", languages[nvram[7]]); 6801da177e4SLinus Torvalds else 6818587b33fSWim Van Sebroeck seq_printf(seq, "%u (undefined)\n", nvram[7]); 6828587b33fSWim Van Sebroeck seq_printf(seq, "Date format : "); 6838587b33fSWim Van Sebroeck seq_printf(seq, dateformat[nvram[8] & 7], 6841da177e4SLinus Torvalds nvram[9] ? nvram[9] : '/', nvram[9] ? nvram[9] : '/'); 6858587b33fSWim Van Sebroeck seq_printf(seq, ", %dh clock\n", nvram[8] & 16 ? 24 : 12); 6868587b33fSWim Van Sebroeck seq_printf(seq, "Boot delay : "); 6871da177e4SLinus Torvalds if (nvram[10] == 0) 6888587b33fSWim Van Sebroeck seq_printf(seq, "default"); 6891da177e4SLinus Torvalds else 6908587b33fSWim Van Sebroeck seq_printf(seq, "%ds%s\n", nvram[10], 6911da177e4SLinus Torvalds nvram[10] < 8 ? ", no memory test" : ""); 6921da177e4SLinus Torvalds 693d25ded8dSFinn Thain vmode = (nvram[14] << 8) | nvram[15]; 6948587b33fSWim Van Sebroeck seq_printf(seq, 6958587b33fSWim Van Sebroeck "Video mode : %s colors, %d columns, %s %s monitor\n", 6961da177e4SLinus Torvalds colors[vmode & 7], 6971da177e4SLinus Torvalds vmode & 8 ? 80 : 40, 6981da177e4SLinus Torvalds vmode & 16 ? "VGA" : "TV", vmode & 32 ? "PAL" : "NTSC"); 6998587b33fSWim Van Sebroeck seq_printf(seq, " %soverscan, compat. mode %s%s\n", 7001da177e4SLinus Torvalds vmode & 64 ? "" : "no ", 7011da177e4SLinus Torvalds vmode & 128 ? "on" : "off", 7021da177e4SLinus Torvalds vmode & 256 ? 7031da177e4SLinus Torvalds (vmode & 16 ? ", line doubling" : ", half screen") : ""); 7041da177e4SLinus Torvalds 7058587b33fSWim Van Sebroeck return; 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds #endif 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds #endif /* MACH == ATARI */ 7101da177e4SLinus Torvalds 7111da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 7121da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(NVRAM_MINOR); 713