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 491da177e4SLinus Torvalds #elif defined(__i386__) || defined(__x86_64__) || defined(__arm__) /* and others?? */ 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> 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds #include <asm/io.h> 1121da177e4SLinus Torvalds #include <asm/uaccess.h> 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 1401da177e4SLinus Torvalds unsigned char 1411da177e4SLinus Torvalds __nvram_read_byte(int i) 1421da177e4SLinus Torvalds { 1431da177e4SLinus Torvalds return CMOS_READ(NVRAM_FIRST_BYTE + i); 1441da177e4SLinus Torvalds } 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds unsigned char 1471da177e4SLinus Torvalds nvram_read_byte(int i) 1481da177e4SLinus Torvalds { 1491da177e4SLinus Torvalds unsigned long flags; 1501da177e4SLinus Torvalds unsigned char c; 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds spin_lock_irqsave(&rtc_lock, flags); 1531da177e4SLinus Torvalds c = __nvram_read_byte(i); 1541da177e4SLinus Torvalds spin_unlock_irqrestore(&rtc_lock, flags); 1551da177e4SLinus Torvalds return c; 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds /* This races nicely with trying to read with checksum checking (nvram_read) */ 1591da177e4SLinus Torvalds void 1601da177e4SLinus Torvalds __nvram_write_byte(unsigned char c, int i) 1611da177e4SLinus Torvalds { 1621da177e4SLinus Torvalds CMOS_WRITE(c, NVRAM_FIRST_BYTE + i); 1631da177e4SLinus Torvalds } 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds void 1661da177e4SLinus Torvalds nvram_write_byte(unsigned char c, int i) 1671da177e4SLinus Torvalds { 1681da177e4SLinus Torvalds unsigned long flags; 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds spin_lock_irqsave(&rtc_lock, flags); 1711da177e4SLinus Torvalds __nvram_write_byte(c, i); 1721da177e4SLinus Torvalds spin_unlock_irqrestore(&rtc_lock, flags); 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds int 1761da177e4SLinus Torvalds __nvram_check_checksum(void) 1771da177e4SLinus Torvalds { 1781da177e4SLinus Torvalds return mach_check_checksum(); 1791da177e4SLinus Torvalds } 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds int 1821da177e4SLinus Torvalds nvram_check_checksum(void) 1831da177e4SLinus Torvalds { 1841da177e4SLinus Torvalds unsigned long flags; 1851da177e4SLinus Torvalds int rv; 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds spin_lock_irqsave(&rtc_lock, flags); 1881da177e4SLinus Torvalds rv = __nvram_check_checksum(); 1891da177e4SLinus Torvalds spin_unlock_irqrestore(&rtc_lock, flags); 1901da177e4SLinus Torvalds return rv; 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds 193681ea4b9SAdrian Bunk static void 1941da177e4SLinus Torvalds __nvram_set_checksum(void) 1951da177e4SLinus Torvalds { 1961da177e4SLinus Torvalds mach_set_checksum(); 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds 199681ea4b9SAdrian Bunk #if 0 2001da177e4SLinus Torvalds void 2011da177e4SLinus Torvalds 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 { 2171da177e4SLinus Torvalds lock_kernel(); 2181da177e4SLinus Torvalds switch (origin) { 2191da177e4SLinus Torvalds case 0: 2201da177e4SLinus Torvalds /* nothing to do */ 2211da177e4SLinus Torvalds break; 2221da177e4SLinus Torvalds case 1: 2231da177e4SLinus Torvalds offset += file->f_pos; 2241da177e4SLinus Torvalds break; 2251da177e4SLinus Torvalds case 2: 2261da177e4SLinus Torvalds offset += NVRAM_BYTES; 2271da177e4SLinus Torvalds break; 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds unlock_kernel(); 2301da177e4SLinus Torvalds return (offset >= 0) ? (file->f_pos = offset) : -EINVAL; 2311da177e4SLinus Torvalds } 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds static ssize_t 2341da177e4SLinus Torvalds nvram_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) 2351da177e4SLinus Torvalds { 2361da177e4SLinus Torvalds unsigned char contents[NVRAM_BYTES]; 2371da177e4SLinus Torvalds unsigned i = *ppos; 2381da177e4SLinus Torvalds unsigned char *tmp; 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds spin_lock_irq(&rtc_lock); 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds if (!__nvram_check_checksum()) 2431da177e4SLinus Torvalds goto checksum_err; 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds for (tmp = contents; count-- > 0 && i < NVRAM_BYTES; ++i, ++tmp) 2461da177e4SLinus Torvalds *tmp = __nvram_read_byte(i); 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds if (copy_to_user(buf, contents, tmp - contents)) 2511da177e4SLinus Torvalds return -EFAULT; 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds *ppos = i; 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds return tmp - contents; 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds checksum_err: 2581da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 2591da177e4SLinus Torvalds return -EIO; 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds static ssize_t 2631da177e4SLinus Torvalds nvram_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) 2641da177e4SLinus Torvalds { 2651da177e4SLinus Torvalds unsigned char contents[NVRAM_BYTES]; 2661da177e4SLinus Torvalds unsigned i = *ppos; 2671da177e4SLinus Torvalds unsigned char *tmp; 2681da177e4SLinus Torvalds int len; 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds len = (NVRAM_BYTES - i) < count ? (NVRAM_BYTES - i) : count; 2711da177e4SLinus Torvalds if (copy_from_user(contents, buf, len)) 2721da177e4SLinus Torvalds return -EFAULT; 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds spin_lock_irq(&rtc_lock); 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds if (!__nvram_check_checksum()) 2771da177e4SLinus Torvalds goto checksum_err; 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds for (tmp = contents; count-- > 0 && i < NVRAM_BYTES; ++i, ++tmp) 2801da177e4SLinus Torvalds __nvram_write_byte(*tmp, i); 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds __nvram_set_checksum(); 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds *ppos = i; 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds return tmp - contents; 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds checksum_err: 2911da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 2921da177e4SLinus Torvalds return -EIO; 2931da177e4SLinus Torvalds } 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds static int 2961da177e4SLinus Torvalds nvram_ioctl(struct inode *inode, struct file *file, 2971da177e4SLinus Torvalds unsigned int cmd, unsigned long arg) 2981da177e4SLinus Torvalds { 2991da177e4SLinus Torvalds int i; 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds switch (cmd) { 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds case NVRAM_INIT: 3041da177e4SLinus Torvalds /* initialize NVRAM contents and checksum */ 3051da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 3061da177e4SLinus Torvalds return -EACCES; 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds spin_lock_irq(&rtc_lock); 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds for (i = 0; i < NVRAM_BYTES; ++i) 3111da177e4SLinus Torvalds __nvram_write_byte(0, i); 3121da177e4SLinus Torvalds __nvram_set_checksum(); 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 3151da177e4SLinus Torvalds return 0; 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds case NVRAM_SETCKS: 3181da177e4SLinus Torvalds /* just set checksum, contents unchanged (maybe useful after 3191da177e4SLinus Torvalds * checksum garbaged somehow...) */ 3201da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 3211da177e4SLinus Torvalds return -EACCES; 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds spin_lock_irq(&rtc_lock); 3241da177e4SLinus Torvalds __nvram_set_checksum(); 3251da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 3261da177e4SLinus Torvalds return 0; 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds default: 3291da177e4SLinus Torvalds return -ENOTTY; 3301da177e4SLinus Torvalds } 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds static int 3341da177e4SLinus Torvalds nvram_open(struct inode *inode, struct file *file) 3351da177e4SLinus Torvalds { 336930ab4e5SArnd Bergmann lock_kernel(); 3371da177e4SLinus Torvalds spin_lock(&nvram_state_lock); 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds if ((nvram_open_cnt && (file->f_flags & O_EXCL)) || 3401da177e4SLinus Torvalds (nvram_open_mode & NVRAM_EXCL) || 341*aeb5d727SAl Viro ((file->f_mode & FMODE_WRITE) && (nvram_open_mode & NVRAM_WRITE))) { 3421da177e4SLinus Torvalds spin_unlock(&nvram_state_lock); 343930ab4e5SArnd Bergmann unlock_kernel(); 3441da177e4SLinus Torvalds return -EBUSY; 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds if (file->f_flags & O_EXCL) 3481da177e4SLinus Torvalds nvram_open_mode |= NVRAM_EXCL; 349*aeb5d727SAl Viro if (file->f_mode & FMODE_WRITE) 3501da177e4SLinus Torvalds nvram_open_mode |= NVRAM_WRITE; 3511da177e4SLinus Torvalds nvram_open_cnt++; 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds spin_unlock(&nvram_state_lock); 354930ab4e5SArnd Bergmann unlock_kernel(); 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds return 0; 3571da177e4SLinus Torvalds } 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds static int 3601da177e4SLinus Torvalds nvram_release(struct inode *inode, struct file *file) 3611da177e4SLinus Torvalds { 3621da177e4SLinus Torvalds spin_lock(&nvram_state_lock); 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds nvram_open_cnt--; 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds /* if only one instance is open, clear the EXCL bit */ 3671da177e4SLinus Torvalds if (nvram_open_mode & NVRAM_EXCL) 3681da177e4SLinus Torvalds nvram_open_mode &= ~NVRAM_EXCL; 369*aeb5d727SAl Viro if (file->f_mode & FMODE_WRITE) 3701da177e4SLinus Torvalds nvram_open_mode &= ~NVRAM_WRITE; 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds spin_unlock(&nvram_state_lock); 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds return 0; 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds #ifndef CONFIG_PROC_FS 3781da177e4SLinus Torvalds static int 3791da177e4SLinus Torvalds nvram_read_proc(char *buffer, char **start, off_t offset, 3801da177e4SLinus Torvalds int size, int *eof, void *data) 3811da177e4SLinus Torvalds { 3821da177e4SLinus Torvalds return 0; 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds #else 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds static int 3871da177e4SLinus Torvalds nvram_read_proc(char *buffer, char **start, off_t offset, 3881da177e4SLinus Torvalds int size, int *eof, void *data) 3891da177e4SLinus Torvalds { 3901da177e4SLinus Torvalds unsigned char contents[NVRAM_BYTES]; 3911da177e4SLinus Torvalds int i, len = 0; 3921da177e4SLinus Torvalds off_t begin = 0; 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds spin_lock_irq(&rtc_lock); 3951da177e4SLinus Torvalds for (i = 0; i < NVRAM_BYTES; ++i) 3961da177e4SLinus Torvalds contents[i] = __nvram_read_byte(i); 3971da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds *eof = mach_proc_infos(contents, buffer, &len, &begin, offset, size); 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds if (offset >= begin + len) 4021da177e4SLinus Torvalds return 0; 4031da177e4SLinus Torvalds *start = buffer + (offset - begin); 4041da177e4SLinus Torvalds return (size < begin + len - offset) ? size : begin + len - offset; 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds /* This macro frees the machine specific function from bounds checking and 4091da177e4SLinus Torvalds * this like that... */ 4101da177e4SLinus Torvalds #define PRINT_PROC(fmt,args...) \ 4111da177e4SLinus Torvalds do { \ 4121da177e4SLinus Torvalds *len += sprintf(buffer+*len, fmt, ##args); \ 4131da177e4SLinus Torvalds if (*begin + *len > offset + size) \ 4141da177e4SLinus Torvalds return 0; \ 4151da177e4SLinus Torvalds if (*begin + *len < offset) { \ 4161da177e4SLinus Torvalds *begin += *len; \ 4171da177e4SLinus Torvalds *len = 0; \ 4181da177e4SLinus Torvalds } \ 4191da177e4SLinus Torvalds } while(0) 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 4221da177e4SLinus Torvalds 42362322d25SArjan van de Ven static const struct file_operations nvram_fops = { 4241da177e4SLinus Torvalds .owner = THIS_MODULE, 4251da177e4SLinus Torvalds .llseek = nvram_llseek, 4261da177e4SLinus Torvalds .read = nvram_read, 4271da177e4SLinus Torvalds .write = nvram_write, 4281da177e4SLinus Torvalds .ioctl = nvram_ioctl, 4291da177e4SLinus Torvalds .open = nvram_open, 4301da177e4SLinus Torvalds .release = nvram_release, 4311da177e4SLinus Torvalds }; 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds static struct miscdevice nvram_dev = { 4341da177e4SLinus Torvalds NVRAM_MINOR, 4351da177e4SLinus Torvalds "nvram", 4361da177e4SLinus Torvalds &nvram_fops 4371da177e4SLinus Torvalds }; 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds static int __init 4401da177e4SLinus Torvalds nvram_init(void) 4411da177e4SLinus Torvalds { 4421da177e4SLinus Torvalds int ret; 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds /* First test whether the driver should init at all */ 4451da177e4SLinus Torvalds if (!CHECK_DRIVER_INIT()) 446e945b568SGeert Uytterhoeven return -ENODEV; 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds ret = misc_register(&nvram_dev); 4491da177e4SLinus Torvalds if (ret) { 4501da177e4SLinus Torvalds printk(KERN_ERR "nvram: can't misc_register on minor=%d\n", 4511da177e4SLinus Torvalds NVRAM_MINOR); 4521da177e4SLinus Torvalds goto out; 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds if (!create_proc_read_entry("driver/nvram", 0, NULL, nvram_read_proc, 4551da177e4SLinus Torvalds NULL)) { 4561da177e4SLinus Torvalds printk(KERN_ERR "nvram: can't create /proc/driver/nvram\n"); 4571da177e4SLinus Torvalds ret = -ENOMEM; 4581da177e4SLinus Torvalds goto outmisc; 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds ret = 0; 4611da177e4SLinus Torvalds printk(KERN_INFO "Non-volatile memory driver v" NVRAM_VERSION "\n"); 4621da177e4SLinus Torvalds out: 4631da177e4SLinus Torvalds return ret; 4641da177e4SLinus Torvalds outmisc: 4651da177e4SLinus Torvalds misc_deregister(&nvram_dev); 4661da177e4SLinus Torvalds goto out; 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds static void __exit 4701da177e4SLinus Torvalds nvram_cleanup_module(void) 4711da177e4SLinus Torvalds { 4721da177e4SLinus Torvalds remove_proc_entry("driver/nvram", NULL); 4731da177e4SLinus Torvalds misc_deregister(&nvram_dev); 4741da177e4SLinus Torvalds } 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds module_init(nvram_init); 4771da177e4SLinus Torvalds module_exit(nvram_cleanup_module); 4781da177e4SLinus Torvalds 4791da177e4SLinus Torvalds /* 4801da177e4SLinus Torvalds * Machine specific functions 4811da177e4SLinus Torvalds */ 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds #if MACH == PC 4841da177e4SLinus Torvalds 4851da177e4SLinus Torvalds static int 4861da177e4SLinus Torvalds pc_check_checksum(void) 4871da177e4SLinus Torvalds { 4881da177e4SLinus Torvalds int i; 4891da177e4SLinus Torvalds unsigned short sum = 0; 4901da177e4SLinus Torvalds unsigned short expect; 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i) 4931da177e4SLinus Torvalds sum += __nvram_read_byte(i); 4941da177e4SLinus Torvalds expect = __nvram_read_byte(PC_CKS_LOC)<<8 | 4951da177e4SLinus Torvalds __nvram_read_byte(PC_CKS_LOC+1); 4961da177e4SLinus Torvalds return ((sum & 0xffff) == expect); 4971da177e4SLinus Torvalds } 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds static void 5001da177e4SLinus Torvalds pc_set_checksum(void) 5011da177e4SLinus Torvalds { 5021da177e4SLinus Torvalds int i; 5031da177e4SLinus Torvalds unsigned short sum = 0; 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i) 5061da177e4SLinus Torvalds sum += __nvram_read_byte(i); 5071da177e4SLinus Torvalds __nvram_write_byte(sum >> 8, PC_CKS_LOC); 5081da177e4SLinus Torvalds __nvram_write_byte(sum & 0xff, PC_CKS_LOC + 1); 5091da177e4SLinus Torvalds } 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds static char *floppy_types[] = { 5141da177e4SLinus Torvalds "none", "5.25'' 360k", "5.25'' 1.2M", "3.5'' 720k", "3.5'' 1.44M", 5151da177e4SLinus Torvalds "3.5'' 2.88M", "3.5'' 2.88M" 5161da177e4SLinus Torvalds }; 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds static char *gfx_types[] = { 5191da177e4SLinus Torvalds "EGA, VGA, ... (with BIOS)", 5201da177e4SLinus Torvalds "CGA (40 cols)", 5211da177e4SLinus Torvalds "CGA (80 cols)", 5221da177e4SLinus Torvalds "monochrome", 5231da177e4SLinus Torvalds }; 5241da177e4SLinus Torvalds 5251da177e4SLinus Torvalds static int 5261da177e4SLinus Torvalds pc_proc_infos(unsigned char *nvram, char *buffer, int *len, 5271da177e4SLinus Torvalds off_t *begin, off_t offset, int size) 5281da177e4SLinus Torvalds { 5291da177e4SLinus Torvalds int checksum; 5301da177e4SLinus Torvalds int type; 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds spin_lock_irq(&rtc_lock); 5331da177e4SLinus Torvalds checksum = __nvram_check_checksum(); 5341da177e4SLinus Torvalds spin_unlock_irq(&rtc_lock); 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds PRINT_PROC("Checksum status: %svalid\n", checksum ? "" : "not "); 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds PRINT_PROC("# floppies : %d\n", 5391da177e4SLinus Torvalds (nvram[6] & 1) ? (nvram[6] >> 6) + 1 : 0); 5401da177e4SLinus Torvalds PRINT_PROC("Floppy 0 type : "); 5411da177e4SLinus Torvalds type = nvram[2] >> 4; 542fe971071STobias Klauser if (type < ARRAY_SIZE(floppy_types)) 5431da177e4SLinus Torvalds PRINT_PROC("%s\n", floppy_types[type]); 5441da177e4SLinus Torvalds else 5451da177e4SLinus Torvalds PRINT_PROC("%d (unknown)\n", type); 5461da177e4SLinus Torvalds PRINT_PROC("Floppy 1 type : "); 5471da177e4SLinus Torvalds type = nvram[2] & 0x0f; 548fe971071STobias Klauser if (type < ARRAY_SIZE(floppy_types)) 5491da177e4SLinus Torvalds PRINT_PROC("%s\n", floppy_types[type]); 5501da177e4SLinus Torvalds else 5511da177e4SLinus Torvalds PRINT_PROC("%d (unknown)\n", type); 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds PRINT_PROC("HD 0 type : "); 5541da177e4SLinus Torvalds type = nvram[4] >> 4; 5551da177e4SLinus Torvalds if (type) 5561da177e4SLinus Torvalds PRINT_PROC("%02x\n", type == 0x0f ? nvram[11] : type); 5571da177e4SLinus Torvalds else 5581da177e4SLinus Torvalds PRINT_PROC("none\n"); 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds PRINT_PROC("HD 1 type : "); 5611da177e4SLinus Torvalds type = nvram[4] & 0x0f; 5621da177e4SLinus Torvalds if (type) 5631da177e4SLinus Torvalds PRINT_PROC("%02x\n", type == 0x0f ? nvram[12] : type); 5641da177e4SLinus Torvalds else 5651da177e4SLinus Torvalds PRINT_PROC("none\n"); 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds PRINT_PROC("HD type 48 data: %d/%d/%d C/H/S, precomp %d, lz %d\n", 5681da177e4SLinus Torvalds nvram[18] | (nvram[19] << 8), 5691da177e4SLinus Torvalds nvram[20], nvram[25], 5701da177e4SLinus Torvalds nvram[21] | (nvram[22] << 8), nvram[23] | (nvram[24] << 8)); 5711da177e4SLinus Torvalds PRINT_PROC("HD type 49 data: %d/%d/%d C/H/S, precomp %d, lz %d\n", 5721da177e4SLinus Torvalds nvram[39] | (nvram[40] << 8), 5731da177e4SLinus Torvalds nvram[41], nvram[46], 5741da177e4SLinus Torvalds nvram[42] | (nvram[43] << 8), nvram[44] | (nvram[45] << 8)); 5751da177e4SLinus Torvalds 5761da177e4SLinus Torvalds PRINT_PROC("DOS base memory: %d kB\n", nvram[7] | (nvram[8] << 8)); 5771da177e4SLinus Torvalds PRINT_PROC("Extended memory: %d kB (configured), %d kB (tested)\n", 5781da177e4SLinus Torvalds nvram[9] | (nvram[10] << 8), nvram[34] | (nvram[35] << 8)); 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds PRINT_PROC("Gfx adapter : %s\n", gfx_types[(nvram[6] >> 4) & 3]); 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds PRINT_PROC("FPU : %sinstalled\n", 5831da177e4SLinus Torvalds (nvram[6] & 2) ? "" : "not "); 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds return 1; 5861da177e4SLinus Torvalds } 5871da177e4SLinus Torvalds #endif 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds #endif /* MACH == PC */ 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds #if MACH == ATARI 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds static int 5941da177e4SLinus Torvalds atari_check_checksum(void) 5951da177e4SLinus Torvalds { 5961da177e4SLinus Torvalds int i; 5971da177e4SLinus Torvalds unsigned char sum = 0; 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds for (i = ATARI_CKS_RANGE_START; i <= ATARI_CKS_RANGE_END; ++i) 6001da177e4SLinus Torvalds sum += __nvram_read_byte(i); 6011da177e4SLinus Torvalds return (__nvram_read_byte(ATARI_CKS_LOC) == (~sum & 0xff) && 6021da177e4SLinus Torvalds __nvram_read_byte(ATARI_CKS_LOC + 1) == (sum & 0xff)); 6031da177e4SLinus Torvalds } 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds static void 6061da177e4SLinus Torvalds atari_set_checksum(void) 6071da177e4SLinus Torvalds { 6081da177e4SLinus Torvalds int i; 6091da177e4SLinus Torvalds unsigned char sum = 0; 6101da177e4SLinus Torvalds 6111da177e4SLinus Torvalds for (i = ATARI_CKS_RANGE_START; i <= ATARI_CKS_RANGE_END; ++i) 6121da177e4SLinus Torvalds sum += __nvram_read_byte(i); 6131da177e4SLinus Torvalds __nvram_write_byte(~sum, ATARI_CKS_LOC); 6141da177e4SLinus Torvalds __nvram_write_byte(sum, ATARI_CKS_LOC + 1); 6151da177e4SLinus Torvalds } 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 6181da177e4SLinus Torvalds 6191da177e4SLinus Torvalds static struct { 6201da177e4SLinus Torvalds unsigned char val; 6211da177e4SLinus Torvalds char *name; 6221da177e4SLinus Torvalds } boot_prefs[] = { 6231da177e4SLinus Torvalds { 0x80, "TOS" }, 6241da177e4SLinus Torvalds { 0x40, "ASV" }, 6251da177e4SLinus Torvalds { 0x20, "NetBSD (?)" }, 6261da177e4SLinus Torvalds { 0x10, "Linux" }, 6271da177e4SLinus Torvalds { 0x00, "unspecified" } 6281da177e4SLinus Torvalds }; 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds static char *languages[] = { 6311da177e4SLinus Torvalds "English (US)", 6321da177e4SLinus Torvalds "German", 6331da177e4SLinus Torvalds "French", 6341da177e4SLinus Torvalds "English (UK)", 6351da177e4SLinus Torvalds "Spanish", 6361da177e4SLinus Torvalds "Italian", 6371da177e4SLinus Torvalds "6 (undefined)", 6381da177e4SLinus Torvalds "Swiss (French)", 6391da177e4SLinus Torvalds "Swiss (German)" 6401da177e4SLinus Torvalds }; 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds static char *dateformat[] = { 6431da177e4SLinus Torvalds "MM%cDD%cYY", 6441da177e4SLinus Torvalds "DD%cMM%cYY", 6451da177e4SLinus Torvalds "YY%cMM%cDD", 6461da177e4SLinus Torvalds "YY%cDD%cMM", 6471da177e4SLinus Torvalds "4 (undefined)", 6481da177e4SLinus Torvalds "5 (undefined)", 6491da177e4SLinus Torvalds "6 (undefined)", 6501da177e4SLinus Torvalds "7 (undefined)" 6511da177e4SLinus Torvalds }; 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds static char *colors[] = { 6541da177e4SLinus Torvalds "2", "4", "16", "256", "65536", "??", "??", "??" 6551da177e4SLinus Torvalds }; 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds static int 6581da177e4SLinus Torvalds atari_proc_infos(unsigned char *nvram, char *buffer, int *len, 6591da177e4SLinus Torvalds off_t *begin, off_t offset, int size) 6601da177e4SLinus Torvalds { 6611da177e4SLinus Torvalds int checksum = nvram_check_checksum(); 6621da177e4SLinus Torvalds int i; 6631da177e4SLinus Torvalds unsigned vmode; 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds PRINT_PROC("Checksum status : %svalid\n", checksum ? "" : "not "); 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds PRINT_PROC("Boot preference : "); 668fe971071STobias Klauser for (i = ARRAY_SIZE(boot_prefs) - 1; i >= 0; --i) { 6691da177e4SLinus Torvalds if (nvram[1] == boot_prefs[i].val) { 6701da177e4SLinus Torvalds PRINT_PROC("%s\n", boot_prefs[i].name); 6711da177e4SLinus Torvalds break; 6721da177e4SLinus Torvalds } 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds if (i < 0) 6751da177e4SLinus Torvalds PRINT_PROC("0x%02x (undefined)\n", nvram[1]); 6761da177e4SLinus Torvalds 6771da177e4SLinus Torvalds PRINT_PROC("SCSI arbitration : %s\n", 6781da177e4SLinus Torvalds (nvram[16] & 0x80) ? "on" : "off"); 6791da177e4SLinus Torvalds PRINT_PROC("SCSI host ID : "); 6801da177e4SLinus Torvalds if (nvram[16] & 0x80) 6811da177e4SLinus Torvalds PRINT_PROC("%d\n", nvram[16] & 7); 6821da177e4SLinus Torvalds else 6831da177e4SLinus Torvalds PRINT_PROC("n/a\n"); 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds /* the following entries are defined only for the Falcon */ 6861da177e4SLinus Torvalds if ((atari_mch_cookie >> 16) != ATARI_MCH_FALCON) 6871da177e4SLinus Torvalds return 1; 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds PRINT_PROC("OS language : "); 690fe971071STobias Klauser if (nvram[6] < ARRAY_SIZE(languages)) 6911da177e4SLinus Torvalds PRINT_PROC("%s\n", languages[nvram[6]]); 6921da177e4SLinus Torvalds else 6931da177e4SLinus Torvalds PRINT_PROC("%u (undefined)\n", nvram[6]); 6941da177e4SLinus Torvalds PRINT_PROC("Keyboard language: "); 695fe971071STobias Klauser if (nvram[7] < ARRAY_SIZE(languages)) 6961da177e4SLinus Torvalds PRINT_PROC("%s\n", languages[nvram[7]]); 6971da177e4SLinus Torvalds else 6981da177e4SLinus Torvalds PRINT_PROC("%u (undefined)\n", nvram[7]); 6991da177e4SLinus Torvalds PRINT_PROC("Date format : "); 7001da177e4SLinus Torvalds PRINT_PROC(dateformat[nvram[8] & 7], 7011da177e4SLinus Torvalds nvram[9] ? nvram[9] : '/', nvram[9] ? nvram[9] : '/'); 7021da177e4SLinus Torvalds PRINT_PROC(", %dh clock\n", nvram[8] & 16 ? 24 : 12); 7031da177e4SLinus Torvalds PRINT_PROC("Boot delay : "); 7041da177e4SLinus Torvalds if (nvram[10] == 0) 7051da177e4SLinus Torvalds PRINT_PROC("default"); 7061da177e4SLinus Torvalds else 7071da177e4SLinus Torvalds PRINT_PROC("%ds%s\n", nvram[10], 7081da177e4SLinus Torvalds nvram[10] < 8 ? ", no memory test" : ""); 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds vmode = (nvram[14] << 8) || nvram[15]; 7111da177e4SLinus Torvalds PRINT_PROC("Video mode : %s colors, %d columns, %s %s monitor\n", 7121da177e4SLinus Torvalds colors[vmode & 7], 7131da177e4SLinus Torvalds vmode & 8 ? 80 : 40, 7141da177e4SLinus Torvalds vmode & 16 ? "VGA" : "TV", vmode & 32 ? "PAL" : "NTSC"); 7151da177e4SLinus Torvalds PRINT_PROC(" %soverscan, compat. mode %s%s\n", 7161da177e4SLinus Torvalds vmode & 64 ? "" : "no ", 7171da177e4SLinus Torvalds vmode & 128 ? "on" : "off", 7181da177e4SLinus Torvalds vmode & 256 ? 7191da177e4SLinus Torvalds (vmode & 16 ? ", line doubling" : ", half screen") : ""); 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds return 1; 7221da177e4SLinus Torvalds } 7231da177e4SLinus Torvalds #endif 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds #endif /* MACH == ATARI */ 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds EXPORT_SYMBOL(__nvram_read_byte); 7301da177e4SLinus Torvalds EXPORT_SYMBOL(nvram_read_byte); 7311da177e4SLinus Torvalds EXPORT_SYMBOL(__nvram_write_byte); 7321da177e4SLinus Torvalds EXPORT_SYMBOL(nvram_write_byte); 7331da177e4SLinus Torvalds EXPORT_SYMBOL(__nvram_check_checksum); 7341da177e4SLinus Torvalds EXPORT_SYMBOL(nvram_check_checksum); 7351da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(NVRAM_MINOR); 736