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