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 351da177e4SLinus Torvalds */ 361da177e4SLinus Torvalds 37866237eaSRussell King #define NVRAM_VERSION "1.2" 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds #include <linux/module.h> 401da177e4SLinus Torvalds #include <linux/smp_lock.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 49*971ddcf8SWim 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 971da177e4SLinus Torvalds * periodic 11 min sync from time.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/slab.h> 1041da177e4SLinus Torvalds #include <linux/ioport.h> 1051da177e4SLinus Torvalds #include <linux/fcntl.h> 1061da177e4SLinus Torvalds #include <linux/mc146818rtc.h> 1071da177e4SLinus Torvalds #include <linux/init.h> 1081da177e4SLinus Torvalds #include <linux/proc_fs.h> 1091da177e4SLinus Torvalds #include <linux/spinlock.h> 110*971ddcf8SWim Van Sebroeck #include <linux/io.h> 111*971ddcf8SWim Van Sebroeck #include <linux/uaccess.h> 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds #include <asm/system.h> 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds static DEFINE_SPINLOCK(nvram_state_lock); 1161da177e4SLinus Torvalds static int nvram_open_cnt; /* #times opened */ 1171da177e4SLinus Torvalds static int nvram_open_mode; /* special open modes */ 1181da177e4SLinus Torvalds #define NVRAM_WRITE 1 /* opened for writing (exclusive) */ 1191da177e4SLinus Torvalds #define NVRAM_EXCL 2 /* opened with O_EXCL */ 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds static int mach_check_checksum(void); 1221da177e4SLinus Torvalds static void mach_set_checksum(void); 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 1251da177e4SLinus Torvalds static int mach_proc_infos(unsigned char *contents, char *buffer, int *len, 1261da177e4SLinus Torvalds off_t *begin, off_t offset, int size); 1271da177e4SLinus Torvalds #endif 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds /* 1301da177e4SLinus Torvalds * These functions are provided to be called internally or by other parts of 1311da177e4SLinus Torvalds * the kernel. It's up to the caller to ensure correct checksum before reading 1321da177e4SLinus Torvalds * or after writing (needs to be done only once). 1331da177e4SLinus Torvalds * 1341da177e4SLinus Torvalds * It is worth noting that these functions all access bytes of general 1351da177e4SLinus Torvalds * purpose memory in the NVRAM - that is to say, they all add the 1361da177e4SLinus Torvalds * NVRAM_FIRST_BYTE offset. Pass them offsets into NVRAM as if you did not 1371da177e4SLinus Torvalds * know about the RTC cruft. 1381da177e4SLinus Torvalds */ 1391da177e4SLinus Torvalds 140*971ddcf8SWim Van Sebroeck unsigned char __nvram_read_byte(int i) 1411da177e4SLinus Torvalds { 1421da177e4SLinus Torvalds return CMOS_READ(NVRAM_FIRST_BYTE + i); 1431da177e4SLinus Torvalds } 144*971ddcf8SWim Van Sebroeck EXPORT_SYMBOL(__nvram_read_byte); 1451da177e4SLinus Torvalds 146*971ddcf8SWim Van Sebroeck unsigned char nvram_read_byte(int i) 1471da177e4SLinus Torvalds { 1481da177e4SLinus Torvalds unsigned long flags; 1491da177e4SLinus Torvalds unsigned char c; 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds spin_lock_irqsave(&rtc_lock, flags); 1521da177e4SLinus Torvalds c = __nvram_read_byte(i); 1531da177e4SLinus Torvalds spin_unlock_irqrestore(&rtc_lock, flags); 1541da177e4SLinus Torvalds return c; 1551da177e4SLinus Torvalds } 156*971ddcf8SWim Van Sebroeck EXPORT_SYMBOL(nvram_read_byte); 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds /* This races nicely with trying to read with checksum checking (nvram_read) */ 159*971ddcf8SWim Van Sebroeck void __nvram_write_byte(unsigned char c, int i) 1601da177e4SLinus Torvalds { 1611da177e4SLinus Torvalds CMOS_WRITE(c, NVRAM_FIRST_BYTE + i); 1621da177e4SLinus Torvalds } 163*971ddcf8SWim Van Sebroeck EXPORT_SYMBOL(__nvram_write_byte); 1641da177e4SLinus Torvalds 165*971ddcf8SWim Van Sebroeck void nvram_write_byte(unsigned char c, int i) 1661da177e4SLinus Torvalds { 1671da177e4SLinus Torvalds unsigned long flags; 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds spin_lock_irqsave(&rtc_lock, flags); 1701da177e4SLinus Torvalds __nvram_write_byte(c, i); 1711da177e4SLinus Torvalds spin_unlock_irqrestore(&rtc_lock, flags); 1721da177e4SLinus Torvalds } 173*971ddcf8SWim Van Sebroeck EXPORT_SYMBOL(nvram_write_byte); 1741da177e4SLinus Torvalds 175*971ddcf8SWim Van Sebroeck int __nvram_check_checksum(void) 1761da177e4SLinus Torvalds { 1771da177e4SLinus Torvalds return mach_check_checksum(); 1781da177e4SLinus Torvalds } 179*971ddcf8SWim Van Sebroeck EXPORT_SYMBOL(__nvram_check_checksum); 1801da177e4SLinus Torvalds 181*971ddcf8SWim Van Sebroeck int nvram_check_checksum(void) 1821da177e4SLinus Torvalds { 1831da177e4SLinus Torvalds unsigned long flags; 1841da177e4SLinus Torvalds int rv; 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds spin_lock_irqsave(&rtc_lock, flags); 1871da177e4SLinus Torvalds rv = __nvram_check_checksum(); 1881da177e4SLinus Torvalds spin_unlock_irqrestore(&rtc_lock, flags); 1891da177e4SLinus Torvalds return rv; 1901da177e4SLinus Torvalds } 191*971ddcf8SWim Van Sebroeck EXPORT_SYMBOL(nvram_check_checksum); 1921da177e4SLinus Torvalds 193*971ddcf8SWim Van Sebroeck static void __nvram_set_checksum(void) 1941da177e4SLinus Torvalds { 1951da177e4SLinus Torvalds mach_set_checksum(); 1961da177e4SLinus Torvalds } 1971da177e4SLinus Torvalds 198681ea4b9SAdrian Bunk #if 0 199*971ddcf8SWim Van Sebroeck void nvram_set_checksum(void) 2001da177e4SLinus Torvalds { 2011da177e4SLinus Torvalds unsigned long flags; 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds spin_lock_irqsave(&rtc_lock, flags); 2041da177e4SLinus Torvalds __nvram_set_checksum(); 2051da177e4SLinus Torvalds spin_unlock_irqrestore(&rtc_lock, flags); 2061da177e4SLinus Torvalds } 207681ea4b9SAdrian Bunk #endif /* 0 */ 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds /* 2101da177e4SLinus Torvalds * The are the file operation function for user access to /dev/nvram 2111da177e4SLinus Torvalds */ 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds static loff_t nvram_llseek(struct file *file, loff_t offset, int origin) 2141da177e4SLinus Torvalds { 2151da177e4SLinus Torvalds lock_kernel(); 2161da177e4SLinus Torvalds switch (origin) { 2171da177e4SLinus Torvalds case 0: 2181da177e4SLinus Torvalds /* nothing to do */ 2191da177e4SLinus Torvalds break; 2201da177e4SLinus Torvalds case 1: 2211da177e4SLinus Torvalds offset += file->f_pos; 2221da177e4SLinus Torvalds break; 2231da177e4SLinus Torvalds case 2: 2241da177e4SLinus Torvalds offset += NVRAM_BYTES; 2251da177e4SLinus Torvalds break; 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds unlock_kernel(); 2281da177e4SLinus Torvalds return (offset >= 0) ? (file->f_pos = offset) : -EINVAL; 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds 231*971ddcf8SWim Van Sebroeck static ssize_t nvram_read(struct file *file, char __user *buf, 232*971ddcf8SWim Van Sebroeck size_t count, loff_t *ppos) 2331da177e4SLinus Torvalds { 2341da177e4SLinus Torvalds unsigned char contents[NVRAM_BYTES]; 2351da177e4SLinus Torvalds unsigned i = *ppos; 2361da177e4SLinus Torvalds unsigned char *tmp; 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds spin_lock_irq(&rtc_lock); 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds if (!__nvram_check_checksum()) 2411da177e4SLinus Torvalds goto checksum_err; 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds for (tmp = contents; count-- > 0 && i < NVRAM_BYTES; ++i, ++tmp) 2441da177e4SLinus Torvalds *tmp = __nvram_read_byte(i); 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds if (copy_to_user(buf, contents, tmp - contents)) 2491da177e4SLinus Torvalds return -EFAULT; 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds *ppos = i; 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds return tmp - contents; 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds checksum_err: 2561da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 2571da177e4SLinus Torvalds return -EIO; 2581da177e4SLinus Torvalds } 2591da177e4SLinus Torvalds 260*971ddcf8SWim Van Sebroeck static ssize_t nvram_write(struct file *file, const char __user *buf, 261*971ddcf8SWim Van Sebroeck size_t count, loff_t *ppos) 2621da177e4SLinus Torvalds { 2631da177e4SLinus Torvalds unsigned char contents[NVRAM_BYTES]; 2641da177e4SLinus Torvalds unsigned i = *ppos; 2651da177e4SLinus Torvalds unsigned char *tmp; 2661da177e4SLinus Torvalds int len; 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds len = (NVRAM_BYTES - i) < count ? (NVRAM_BYTES - i) : count; 2691da177e4SLinus Torvalds if (copy_from_user(contents, buf, len)) 2701da177e4SLinus Torvalds return -EFAULT; 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds spin_lock_irq(&rtc_lock); 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds if (!__nvram_check_checksum()) 2751da177e4SLinus Torvalds goto checksum_err; 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds for (tmp = contents; count-- > 0 && i < NVRAM_BYTES; ++i, ++tmp) 2781da177e4SLinus Torvalds __nvram_write_byte(*tmp, i); 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds __nvram_set_checksum(); 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds *ppos = i; 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds return tmp - contents; 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds checksum_err: 2891da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 2901da177e4SLinus Torvalds return -EIO; 2911da177e4SLinus Torvalds } 2921da177e4SLinus Torvalds 293*971ddcf8SWim Van Sebroeck static int nvram_ioctl(struct inode *inode, struct file *file, 2941da177e4SLinus Torvalds unsigned int cmd, unsigned long arg) 2951da177e4SLinus Torvalds { 2961da177e4SLinus Torvalds int i; 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds switch (cmd) { 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds case NVRAM_INIT: 3011da177e4SLinus Torvalds /* initialize NVRAM contents and checksum */ 3021da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 3031da177e4SLinus Torvalds return -EACCES; 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds spin_lock_irq(&rtc_lock); 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds for (i = 0; i < NVRAM_BYTES; ++i) 3081da177e4SLinus Torvalds __nvram_write_byte(0, i); 3091da177e4SLinus Torvalds __nvram_set_checksum(); 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 3121da177e4SLinus Torvalds return 0; 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds case NVRAM_SETCKS: 3151da177e4SLinus Torvalds /* just set checksum, contents unchanged (maybe useful after 3161da177e4SLinus Torvalds * checksum garbaged somehow...) */ 3171da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 3181da177e4SLinus Torvalds return -EACCES; 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds spin_lock_irq(&rtc_lock); 3211da177e4SLinus Torvalds __nvram_set_checksum(); 3221da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 3231da177e4SLinus Torvalds return 0; 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds default: 3261da177e4SLinus Torvalds return -ENOTTY; 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds } 3291da177e4SLinus Torvalds 330*971ddcf8SWim Van Sebroeck static int nvram_open(struct inode *inode, struct file *file) 3311da177e4SLinus Torvalds { 332930ab4e5SArnd Bergmann lock_kernel(); 3331da177e4SLinus Torvalds spin_lock(&nvram_state_lock); 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds if ((nvram_open_cnt && (file->f_flags & O_EXCL)) || 3361da177e4SLinus Torvalds (nvram_open_mode & NVRAM_EXCL) || 337aeb5d727SAl Viro ((file->f_mode & FMODE_WRITE) && (nvram_open_mode & NVRAM_WRITE))) { 3381da177e4SLinus Torvalds spin_unlock(&nvram_state_lock); 339930ab4e5SArnd Bergmann unlock_kernel(); 3401da177e4SLinus Torvalds return -EBUSY; 3411da177e4SLinus Torvalds } 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds if (file->f_flags & O_EXCL) 3441da177e4SLinus Torvalds nvram_open_mode |= NVRAM_EXCL; 345aeb5d727SAl Viro if (file->f_mode & FMODE_WRITE) 3461da177e4SLinus Torvalds nvram_open_mode |= NVRAM_WRITE; 3471da177e4SLinus Torvalds nvram_open_cnt++; 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds spin_unlock(&nvram_state_lock); 350930ab4e5SArnd Bergmann unlock_kernel(); 3511da177e4SLinus Torvalds 3521da177e4SLinus Torvalds return 0; 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds 355*971ddcf8SWim Van Sebroeck static int nvram_release(struct inode *inode, struct file *file) 3561da177e4SLinus Torvalds { 3571da177e4SLinus Torvalds spin_lock(&nvram_state_lock); 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds nvram_open_cnt--; 3601da177e4SLinus Torvalds 3611da177e4SLinus Torvalds /* if only one instance is open, clear the EXCL bit */ 3621da177e4SLinus Torvalds if (nvram_open_mode & NVRAM_EXCL) 3631da177e4SLinus Torvalds nvram_open_mode &= ~NVRAM_EXCL; 364aeb5d727SAl Viro if (file->f_mode & FMODE_WRITE) 3651da177e4SLinus Torvalds nvram_open_mode &= ~NVRAM_WRITE; 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds spin_unlock(&nvram_state_lock); 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds return 0; 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds #ifndef CONFIG_PROC_FS 373*971ddcf8SWim Van Sebroeck static int nvram_read_proc(char *buffer, char **start, off_t offset, 3741da177e4SLinus Torvalds int size, int *eof, void *data) 3751da177e4SLinus Torvalds { 3761da177e4SLinus Torvalds return 0; 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds #else 3791da177e4SLinus Torvalds 380*971ddcf8SWim Van Sebroeck static int nvram_read_proc(char *buffer, char **start, off_t offset, 3811da177e4SLinus Torvalds int size, int *eof, void *data) 3821da177e4SLinus Torvalds { 3831da177e4SLinus Torvalds unsigned char contents[NVRAM_BYTES]; 3841da177e4SLinus Torvalds int i, len = 0; 3851da177e4SLinus Torvalds off_t begin = 0; 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds spin_lock_irq(&rtc_lock); 3881da177e4SLinus Torvalds for (i = 0; i < NVRAM_BYTES; ++i) 3891da177e4SLinus Torvalds contents[i] = __nvram_read_byte(i); 3901da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds *eof = mach_proc_infos(contents, buffer, &len, &begin, offset, size); 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds if (offset >= begin + len) 3951da177e4SLinus Torvalds return 0; 3961da177e4SLinus Torvalds *start = buffer + (offset - begin); 3971da177e4SLinus Torvalds return (size < begin + len - offset) ? size : begin + len - offset; 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds /* This macro frees the machine specific function from bounds checking and 4021da177e4SLinus Torvalds * this like that... */ 4031da177e4SLinus Torvalds #define PRINT_PROC(fmt,args...) \ 4041da177e4SLinus Torvalds do { \ 4051da177e4SLinus Torvalds *len += sprintf(buffer + *len, fmt, ##args); \ 4061da177e4SLinus Torvalds if (*begin + *len > offset + size) \ 4071da177e4SLinus Torvalds return 0; \ 4081da177e4SLinus Torvalds if (*begin + *len < offset) { \ 4091da177e4SLinus Torvalds *begin += *len; \ 4101da177e4SLinus Torvalds *len = 0; \ 4111da177e4SLinus Torvalds } \ 4121da177e4SLinus Torvalds } while (0) 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 4151da177e4SLinus Torvalds 41662322d25SArjan van de Ven static const struct file_operations nvram_fops = { 4171da177e4SLinus Torvalds .owner = THIS_MODULE, 4181da177e4SLinus Torvalds .llseek = nvram_llseek, 4191da177e4SLinus Torvalds .read = nvram_read, 4201da177e4SLinus Torvalds .write = nvram_write, 4211da177e4SLinus Torvalds .ioctl = nvram_ioctl, 4221da177e4SLinus Torvalds .open = nvram_open, 4231da177e4SLinus Torvalds .release = nvram_release, 4241da177e4SLinus Torvalds }; 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds static struct miscdevice nvram_dev = { 4271da177e4SLinus Torvalds NVRAM_MINOR, 4281da177e4SLinus Torvalds "nvram", 4291da177e4SLinus Torvalds &nvram_fops 4301da177e4SLinus Torvalds }; 4311da177e4SLinus Torvalds 432*971ddcf8SWim Van Sebroeck static int __init nvram_init(void) 4331da177e4SLinus Torvalds { 4341da177e4SLinus Torvalds int ret; 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds /* First test whether the driver should init at all */ 4371da177e4SLinus Torvalds if (!CHECK_DRIVER_INIT()) 438e945b568SGeert Uytterhoeven return -ENODEV; 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds ret = misc_register(&nvram_dev); 4411da177e4SLinus Torvalds if (ret) { 4421da177e4SLinus Torvalds printk(KERN_ERR "nvram: can't misc_register on minor=%d\n", 4431da177e4SLinus Torvalds NVRAM_MINOR); 4441da177e4SLinus Torvalds goto out; 4451da177e4SLinus Torvalds } 4461da177e4SLinus Torvalds if (!create_proc_read_entry("driver/nvram", 0, NULL, nvram_read_proc, 4471da177e4SLinus Torvalds NULL)) { 4481da177e4SLinus Torvalds printk(KERN_ERR "nvram: can't create /proc/driver/nvram\n"); 4491da177e4SLinus Torvalds ret = -ENOMEM; 4501da177e4SLinus Torvalds goto outmisc; 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds ret = 0; 4531da177e4SLinus Torvalds printk(KERN_INFO "Non-volatile memory driver v" NVRAM_VERSION "\n"); 4541da177e4SLinus Torvalds out: 4551da177e4SLinus Torvalds return ret; 4561da177e4SLinus Torvalds outmisc: 4571da177e4SLinus Torvalds misc_deregister(&nvram_dev); 4581da177e4SLinus Torvalds goto out; 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds 461*971ddcf8SWim Van Sebroeck static void __exit nvram_cleanup_module(void) 4621da177e4SLinus Torvalds { 4631da177e4SLinus Torvalds remove_proc_entry("driver/nvram", NULL); 4641da177e4SLinus Torvalds misc_deregister(&nvram_dev); 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds module_init(nvram_init); 4681da177e4SLinus Torvalds module_exit(nvram_cleanup_module); 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds /* 4711da177e4SLinus Torvalds * Machine specific functions 4721da177e4SLinus Torvalds */ 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds #if MACH == PC 4751da177e4SLinus Torvalds 476*971ddcf8SWim Van Sebroeck static int pc_check_checksum(void) 4771da177e4SLinus Torvalds { 4781da177e4SLinus Torvalds int i; 4791da177e4SLinus Torvalds unsigned short sum = 0; 4801da177e4SLinus Torvalds unsigned short expect; 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i) 4831da177e4SLinus Torvalds sum += __nvram_read_byte(i); 4841da177e4SLinus Torvalds expect = __nvram_read_byte(PC_CKS_LOC)<<8 | 4851da177e4SLinus Torvalds __nvram_read_byte(PC_CKS_LOC+1); 486*971ddcf8SWim Van Sebroeck return (sum & 0xffff) == expect; 4871da177e4SLinus Torvalds } 4881da177e4SLinus Torvalds 489*971ddcf8SWim Van Sebroeck static void pc_set_checksum(void) 4901da177e4SLinus Torvalds { 4911da177e4SLinus Torvalds int i; 4921da177e4SLinus Torvalds unsigned short sum = 0; 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i) 4951da177e4SLinus Torvalds sum += __nvram_read_byte(i); 4961da177e4SLinus Torvalds __nvram_write_byte(sum >> 8, PC_CKS_LOC); 4971da177e4SLinus Torvalds __nvram_write_byte(sum & 0xff, PC_CKS_LOC + 1); 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds static char *floppy_types[] = { 5031da177e4SLinus Torvalds "none", "5.25'' 360k", "5.25'' 1.2M", "3.5'' 720k", "3.5'' 1.44M", 5041da177e4SLinus Torvalds "3.5'' 2.88M", "3.5'' 2.88M" 5051da177e4SLinus Torvalds }; 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds static char *gfx_types[] = { 5081da177e4SLinus Torvalds "EGA, VGA, ... (with BIOS)", 5091da177e4SLinus Torvalds "CGA (40 cols)", 5101da177e4SLinus Torvalds "CGA (80 cols)", 5111da177e4SLinus Torvalds "monochrome", 5121da177e4SLinus Torvalds }; 5131da177e4SLinus Torvalds 514*971ddcf8SWim Van Sebroeck static int pc_proc_infos(unsigned char *nvram, char *buffer, int *len, 5151da177e4SLinus Torvalds off_t *begin, off_t offset, int size) 5161da177e4SLinus Torvalds { 5171da177e4SLinus Torvalds int checksum; 5181da177e4SLinus Torvalds int type; 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds spin_lock_irq(&rtc_lock); 5211da177e4SLinus Torvalds checksum = __nvram_check_checksum(); 5221da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 5231da177e4SLinus Torvalds 5241da177e4SLinus Torvalds PRINT_PROC("Checksum status: %svalid\n", checksum ? "" : "not "); 5251da177e4SLinus Torvalds 5261da177e4SLinus Torvalds PRINT_PROC("# floppies : %d\n", 5271da177e4SLinus Torvalds (nvram[6] & 1) ? (nvram[6] >> 6) + 1 : 0); 5281da177e4SLinus Torvalds PRINT_PROC("Floppy 0 type : "); 5291da177e4SLinus Torvalds type = nvram[2] >> 4; 530fe971071STobias Klauser if (type < ARRAY_SIZE(floppy_types)) 5311da177e4SLinus Torvalds PRINT_PROC("%s\n", floppy_types[type]); 5321da177e4SLinus Torvalds else 5331da177e4SLinus Torvalds PRINT_PROC("%d (unknown)\n", type); 5341da177e4SLinus Torvalds PRINT_PROC("Floppy 1 type : "); 5351da177e4SLinus Torvalds type = nvram[2] & 0x0f; 536fe971071STobias Klauser if (type < ARRAY_SIZE(floppy_types)) 5371da177e4SLinus Torvalds PRINT_PROC("%s\n", floppy_types[type]); 5381da177e4SLinus Torvalds else 5391da177e4SLinus Torvalds PRINT_PROC("%d (unknown)\n", type); 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds PRINT_PROC("HD 0 type : "); 5421da177e4SLinus Torvalds type = nvram[4] >> 4; 5431da177e4SLinus Torvalds if (type) 5441da177e4SLinus Torvalds PRINT_PROC("%02x\n", type == 0x0f ? nvram[11] : type); 5451da177e4SLinus Torvalds else 5461da177e4SLinus Torvalds PRINT_PROC("none\n"); 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds PRINT_PROC("HD 1 type : "); 5491da177e4SLinus Torvalds type = nvram[4] & 0x0f; 5501da177e4SLinus Torvalds if (type) 5511da177e4SLinus Torvalds PRINT_PROC("%02x\n", type == 0x0f ? nvram[12] : type); 5521da177e4SLinus Torvalds else 5531da177e4SLinus Torvalds PRINT_PROC("none\n"); 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds PRINT_PROC("HD type 48 data: %d/%d/%d C/H/S, precomp %d, lz %d\n", 5561da177e4SLinus Torvalds nvram[18] | (nvram[19] << 8), 5571da177e4SLinus Torvalds nvram[20], nvram[25], 5581da177e4SLinus Torvalds nvram[21] | (nvram[22] << 8), nvram[23] | (nvram[24] << 8)); 5591da177e4SLinus Torvalds PRINT_PROC("HD type 49 data: %d/%d/%d C/H/S, precomp %d, lz %d\n", 5601da177e4SLinus Torvalds nvram[39] | (nvram[40] << 8), 5611da177e4SLinus Torvalds nvram[41], nvram[46], 5621da177e4SLinus Torvalds nvram[42] | (nvram[43] << 8), nvram[44] | (nvram[45] << 8)); 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds PRINT_PROC("DOS base memory: %d kB\n", nvram[7] | (nvram[8] << 8)); 5651da177e4SLinus Torvalds PRINT_PROC("Extended memory: %d kB (configured), %d kB (tested)\n", 5661da177e4SLinus Torvalds nvram[9] | (nvram[10] << 8), nvram[34] | (nvram[35] << 8)); 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds PRINT_PROC("Gfx adapter : %s\n", gfx_types[(nvram[6] >> 4) & 3]); 5691da177e4SLinus Torvalds 5701da177e4SLinus Torvalds PRINT_PROC("FPU : %sinstalled\n", 5711da177e4SLinus Torvalds (nvram[6] & 2) ? "" : "not "); 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds return 1; 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds #endif 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds #endif /* MACH == PC */ 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds #if MACH == ATARI 5801da177e4SLinus Torvalds 581*971ddcf8SWim Van Sebroeck static int atari_check_checksum(void) 5821da177e4SLinus Torvalds { 5831da177e4SLinus Torvalds int i; 5841da177e4SLinus Torvalds unsigned char sum = 0; 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds for (i = ATARI_CKS_RANGE_START; i <= ATARI_CKS_RANGE_END; ++i) 5871da177e4SLinus Torvalds sum += __nvram_read_byte(i); 588*971ddcf8SWim Van Sebroeck return (__nvram_read_byte(ATARI_CKS_LOC) == (~sum & 0xff)) && 589*971ddcf8SWim Van Sebroeck (__nvram_read_byte(ATARI_CKS_LOC + 1) == (sum & 0xff)); 5901da177e4SLinus Torvalds } 5911da177e4SLinus Torvalds 592*971ddcf8SWim Van Sebroeck static void atari_set_checksum(void) 5931da177e4SLinus Torvalds { 5941da177e4SLinus Torvalds int i; 5951da177e4SLinus Torvalds unsigned char sum = 0; 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds for (i = ATARI_CKS_RANGE_START; i <= ATARI_CKS_RANGE_END; ++i) 5981da177e4SLinus Torvalds sum += __nvram_read_byte(i); 5991da177e4SLinus Torvalds __nvram_write_byte(~sum, ATARI_CKS_LOC); 6001da177e4SLinus Torvalds __nvram_write_byte(sum, ATARI_CKS_LOC + 1); 6011da177e4SLinus Torvalds } 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds static struct { 6061da177e4SLinus Torvalds unsigned char val; 6071da177e4SLinus Torvalds char *name; 6081da177e4SLinus Torvalds } boot_prefs[] = { 6091da177e4SLinus Torvalds { 0x80, "TOS" }, 6101da177e4SLinus Torvalds { 0x40, "ASV" }, 6111da177e4SLinus Torvalds { 0x20, "NetBSD (?)" }, 6121da177e4SLinus Torvalds { 0x10, "Linux" }, 6131da177e4SLinus Torvalds { 0x00, "unspecified" } 6141da177e4SLinus Torvalds }; 6151da177e4SLinus Torvalds 6161da177e4SLinus Torvalds static char *languages[] = { 6171da177e4SLinus Torvalds "English (US)", 6181da177e4SLinus Torvalds "German", 6191da177e4SLinus Torvalds "French", 6201da177e4SLinus Torvalds "English (UK)", 6211da177e4SLinus Torvalds "Spanish", 6221da177e4SLinus Torvalds "Italian", 6231da177e4SLinus Torvalds "6 (undefined)", 6241da177e4SLinus Torvalds "Swiss (French)", 6251da177e4SLinus Torvalds "Swiss (German)" 6261da177e4SLinus Torvalds }; 6271da177e4SLinus Torvalds 6281da177e4SLinus Torvalds static char *dateformat[] = { 6291da177e4SLinus Torvalds "MM%cDD%cYY", 6301da177e4SLinus Torvalds "DD%cMM%cYY", 6311da177e4SLinus Torvalds "YY%cMM%cDD", 6321da177e4SLinus Torvalds "YY%cDD%cMM", 6331da177e4SLinus Torvalds "4 (undefined)", 6341da177e4SLinus Torvalds "5 (undefined)", 6351da177e4SLinus Torvalds "6 (undefined)", 6361da177e4SLinus Torvalds "7 (undefined)" 6371da177e4SLinus Torvalds }; 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds static char *colors[] = { 6401da177e4SLinus Torvalds "2", "4", "16", "256", "65536", "??", "??", "??" 6411da177e4SLinus Torvalds }; 6421da177e4SLinus Torvalds 643*971ddcf8SWim Van Sebroeck static int atari_proc_infos(unsigned char *nvram, char *buffer, int *len, 6441da177e4SLinus Torvalds off_t *begin, off_t offset, int size) 6451da177e4SLinus Torvalds { 6461da177e4SLinus Torvalds int checksum = nvram_check_checksum(); 6471da177e4SLinus Torvalds int i; 6481da177e4SLinus Torvalds unsigned vmode; 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds PRINT_PROC("Checksum status : %svalid\n", checksum ? "" : "not "); 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds PRINT_PROC("Boot preference : "); 653fe971071STobias Klauser for (i = ARRAY_SIZE(boot_prefs) - 1; i >= 0; --i) { 6541da177e4SLinus Torvalds if (nvram[1] == boot_prefs[i].val) { 6551da177e4SLinus Torvalds PRINT_PROC("%s\n", boot_prefs[i].name); 6561da177e4SLinus Torvalds break; 6571da177e4SLinus Torvalds } 6581da177e4SLinus Torvalds } 6591da177e4SLinus Torvalds if (i < 0) 6601da177e4SLinus Torvalds PRINT_PROC("0x%02x (undefined)\n", nvram[1]); 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds PRINT_PROC("SCSI arbitration : %s\n", 6631da177e4SLinus Torvalds (nvram[16] & 0x80) ? "on" : "off"); 6641da177e4SLinus Torvalds PRINT_PROC("SCSI host ID : "); 6651da177e4SLinus Torvalds if (nvram[16] & 0x80) 6661da177e4SLinus Torvalds PRINT_PROC("%d\n", nvram[16] & 7); 6671da177e4SLinus Torvalds else 6681da177e4SLinus Torvalds PRINT_PROC("n/a\n"); 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds /* the following entries are defined only for the Falcon */ 6711da177e4SLinus Torvalds if ((atari_mch_cookie >> 16) != ATARI_MCH_FALCON) 6721da177e4SLinus Torvalds return 1; 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds PRINT_PROC("OS language : "); 675fe971071STobias Klauser if (nvram[6] < ARRAY_SIZE(languages)) 6761da177e4SLinus Torvalds PRINT_PROC("%s\n", languages[nvram[6]]); 6771da177e4SLinus Torvalds else 6781da177e4SLinus Torvalds PRINT_PROC("%u (undefined)\n", nvram[6]); 6791da177e4SLinus Torvalds PRINT_PROC("Keyboard language: "); 680fe971071STobias Klauser if (nvram[7] < ARRAY_SIZE(languages)) 6811da177e4SLinus Torvalds PRINT_PROC("%s\n", languages[nvram[7]]); 6821da177e4SLinus Torvalds else 6831da177e4SLinus Torvalds PRINT_PROC("%u (undefined)\n", nvram[7]); 6841da177e4SLinus Torvalds PRINT_PROC("Date format : "); 6851da177e4SLinus Torvalds PRINT_PROC(dateformat[nvram[8] & 7], 6861da177e4SLinus Torvalds nvram[9] ? nvram[9] : '/', nvram[9] ? nvram[9] : '/'); 6871da177e4SLinus Torvalds PRINT_PROC(", %dh clock\n", nvram[8] & 16 ? 24 : 12); 6881da177e4SLinus Torvalds PRINT_PROC("Boot delay : "); 6891da177e4SLinus Torvalds if (nvram[10] == 0) 6901da177e4SLinus Torvalds PRINT_PROC("default"); 6911da177e4SLinus Torvalds else 6921da177e4SLinus Torvalds PRINT_PROC("%ds%s\n", nvram[10], 6931da177e4SLinus Torvalds nvram[10] < 8 ? ", no memory test" : ""); 6941da177e4SLinus Torvalds 6951da177e4SLinus Torvalds vmode = (nvram[14] << 8) || nvram[15]; 6961da177e4SLinus Torvalds PRINT_PROC("Video mode : %s colors, %d columns, %s %s monitor\n", 6971da177e4SLinus Torvalds colors[vmode & 7], 6981da177e4SLinus Torvalds vmode & 8 ? 80 : 40, 6991da177e4SLinus Torvalds vmode & 16 ? "VGA" : "TV", vmode & 32 ? "PAL" : "NTSC"); 7001da177e4SLinus Torvalds PRINT_PROC(" %soverscan, compat. mode %s%s\n", 7011da177e4SLinus Torvalds vmode & 64 ? "" : "no ", 7021da177e4SLinus Torvalds vmode & 128 ? "on" : "off", 7031da177e4SLinus Torvalds vmode & 256 ? 7041da177e4SLinus Torvalds (vmode & 16 ? ", line doubling" : ", half screen") : ""); 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds return 1; 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds #endif 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds #endif /* MACH == ATARI */ 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 7131da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(NVRAM_MINOR); 714