xref: /openbmc/linux/drivers/char/nvram.c (revision 62322d2554d2f9680c8ace7bbf1f97d8fa84ad1a)
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/sched.h>
411da177e4SLinus Torvalds #include <linux/smp_lock.h>
421da177e4SLinus Torvalds #include <linux/nvram.h>
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #define PC		1
451da177e4SLinus Torvalds #define ATARI		2
461da177e4SLinus Torvalds #define COBALT		3
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /* select machine configuration */
491da177e4SLinus Torvalds #if defined(CONFIG_ATARI)
501da177e4SLinus Torvalds #  define MACH ATARI
511da177e4SLinus Torvalds #elif defined(__i386__) || defined(__x86_64__) || defined(__arm__)  /* and others?? */
521da177e4SLinus Torvalds #define MACH PC
531da177e4SLinus Torvalds #  if defined(CONFIG_COBALT)
541da177e4SLinus Torvalds #    include <linux/cobalt-nvram.h>
551da177e4SLinus Torvalds #    define MACH COBALT
561da177e4SLinus Torvalds #  else
571da177e4SLinus Torvalds #    define MACH PC
581da177e4SLinus Torvalds #  endif
591da177e4SLinus Torvalds #else
601da177e4SLinus Torvalds #  error Cannot build nvram driver for this machine configuration.
611da177e4SLinus Torvalds #endif
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds #if MACH == PC
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /* RTC in a PC */
661da177e4SLinus Torvalds #define CHECK_DRIVER_INIT()	1
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /* On PCs, the checksum is built only over bytes 2..31 */
691da177e4SLinus Torvalds #define PC_CKS_RANGE_START	2
701da177e4SLinus Torvalds #define PC_CKS_RANGE_END	31
711da177e4SLinus Torvalds #define PC_CKS_LOC		32
721da177e4SLinus Torvalds #define NVRAM_BYTES		(128-NVRAM_FIRST_BYTE)
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds #define mach_check_checksum	pc_check_checksum
751da177e4SLinus Torvalds #define mach_set_checksum	pc_set_checksum
761da177e4SLinus Torvalds #define mach_proc_infos		pc_proc_infos
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds #endif
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds #if MACH == COBALT
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds #define CHECK_DRIVER_INIT()     1
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds #define NVRAM_BYTES		(128-NVRAM_FIRST_BYTE)
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds #define mach_check_checksum	cobalt_check_checksum
871da177e4SLinus Torvalds #define mach_set_checksum	cobalt_set_checksum
881da177e4SLinus Torvalds #define mach_proc_infos		cobalt_proc_infos
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds #endif
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds #if MACH == ATARI
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds /* Special parameters for RTC in Atari machines */
951da177e4SLinus Torvalds #include <asm/atarihw.h>
961da177e4SLinus Torvalds #include <asm/atariints.h>
971da177e4SLinus Torvalds #define RTC_PORT(x)		(TT_RTC_BAS + 2*(x))
981da177e4SLinus Torvalds #define CHECK_DRIVER_INIT()	(MACH_IS_ATARI && ATARIHW_PRESENT(TT_CLK))
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds #define NVRAM_BYTES		50
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds /* On Ataris, the checksum is over all bytes except the checksum bytes
1031da177e4SLinus Torvalds  * themselves; these are at the very end */
1041da177e4SLinus Torvalds #define ATARI_CKS_RANGE_START	0
1051da177e4SLinus Torvalds #define ATARI_CKS_RANGE_END	47
1061da177e4SLinus Torvalds #define ATARI_CKS_LOC		48
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds #define mach_check_checksum	atari_check_checksum
1091da177e4SLinus Torvalds #define mach_set_checksum	atari_set_checksum
1101da177e4SLinus Torvalds #define mach_proc_infos		atari_proc_infos
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds #endif
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds /* Note that *all* calls to CMOS_READ and CMOS_WRITE must be done with
1151da177e4SLinus Torvalds  * rtc_lock held. Due to the index-port/data-port design of the RTC, we
1161da177e4SLinus Torvalds  * don't want two different things trying to get to it at once. (e.g. the
1171da177e4SLinus Torvalds  * periodic 11 min sync from time.c vs. this driver.)
1181da177e4SLinus Torvalds  */
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds #include <linux/types.h>
1211da177e4SLinus Torvalds #include <linux/errno.h>
1221da177e4SLinus Torvalds #include <linux/miscdevice.h>
1231da177e4SLinus Torvalds #include <linux/slab.h>
1241da177e4SLinus Torvalds #include <linux/ioport.h>
1251da177e4SLinus Torvalds #include <linux/fcntl.h>
1261da177e4SLinus Torvalds #include <linux/mc146818rtc.h>
1271da177e4SLinus Torvalds #include <linux/init.h>
1281da177e4SLinus Torvalds #include <linux/proc_fs.h>
1291da177e4SLinus Torvalds #include <linux/spinlock.h>
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds #include <asm/io.h>
1321da177e4SLinus Torvalds #include <asm/uaccess.h>
1331da177e4SLinus Torvalds #include <asm/system.h>
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds static DEFINE_SPINLOCK(nvram_state_lock);
1361da177e4SLinus Torvalds static int nvram_open_cnt;	/* #times opened */
1371da177e4SLinus Torvalds static int nvram_open_mode;	/* special open modes */
1381da177e4SLinus Torvalds #define NVRAM_WRITE		1 /* opened for writing (exclusive) */
1391da177e4SLinus Torvalds #define NVRAM_EXCL		2 /* opened with O_EXCL */
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds static int mach_check_checksum(void);
1421da177e4SLinus Torvalds static void mach_set_checksum(void);
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
1451da177e4SLinus Torvalds static int mach_proc_infos(unsigned char *contents, char *buffer, int *len,
1461da177e4SLinus Torvalds     off_t *begin, off_t offset, int size);
1471da177e4SLinus Torvalds #endif
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds /*
1501da177e4SLinus Torvalds  * These functions are provided to be called internally or by other parts of
1511da177e4SLinus Torvalds  * the kernel. It's up to the caller to ensure correct checksum before reading
1521da177e4SLinus Torvalds  * or after writing (needs to be done only once).
1531da177e4SLinus Torvalds  *
1541da177e4SLinus Torvalds  * It is worth noting that these functions all access bytes of general
1551da177e4SLinus Torvalds  * purpose memory in the NVRAM - that is to say, they all add the
1561da177e4SLinus Torvalds  * NVRAM_FIRST_BYTE offset.  Pass them offsets into NVRAM as if you did not
1571da177e4SLinus Torvalds  * know about the RTC cruft.
1581da177e4SLinus Torvalds  */
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds unsigned char
1611da177e4SLinus Torvalds __nvram_read_byte(int i)
1621da177e4SLinus Torvalds {
1631da177e4SLinus Torvalds 	return CMOS_READ(NVRAM_FIRST_BYTE + i);
1641da177e4SLinus Torvalds }
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds unsigned char
1671da177e4SLinus Torvalds nvram_read_byte(int i)
1681da177e4SLinus Torvalds {
1691da177e4SLinus Torvalds 	unsigned long flags;
1701da177e4SLinus Torvalds 	unsigned char c;
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	spin_lock_irqsave(&rtc_lock, flags);
1731da177e4SLinus Torvalds 	c = __nvram_read_byte(i);
1741da177e4SLinus Torvalds 	spin_unlock_irqrestore(&rtc_lock, flags);
1751da177e4SLinus Torvalds 	return c;
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds /* This races nicely with trying to read with checksum checking (nvram_read) */
1791da177e4SLinus Torvalds void
1801da177e4SLinus Torvalds __nvram_write_byte(unsigned char c, int i)
1811da177e4SLinus Torvalds {
1821da177e4SLinus Torvalds 	CMOS_WRITE(c, NVRAM_FIRST_BYTE + i);
1831da177e4SLinus Torvalds }
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds void
1861da177e4SLinus Torvalds nvram_write_byte(unsigned char c, int i)
1871da177e4SLinus Torvalds {
1881da177e4SLinus Torvalds 	unsigned long flags;
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	spin_lock_irqsave(&rtc_lock, flags);
1911da177e4SLinus Torvalds 	__nvram_write_byte(c, i);
1921da177e4SLinus Torvalds 	spin_unlock_irqrestore(&rtc_lock, flags);
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds int
1961da177e4SLinus Torvalds __nvram_check_checksum(void)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds 	return mach_check_checksum();
1991da177e4SLinus Torvalds }
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds int
2021da177e4SLinus Torvalds nvram_check_checksum(void)
2031da177e4SLinus Torvalds {
2041da177e4SLinus Torvalds 	unsigned long flags;
2051da177e4SLinus Torvalds 	int rv;
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	spin_lock_irqsave(&rtc_lock, flags);
2081da177e4SLinus Torvalds 	rv = __nvram_check_checksum();
2091da177e4SLinus Torvalds 	spin_unlock_irqrestore(&rtc_lock, flags);
2101da177e4SLinus Torvalds 	return rv;
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
213681ea4b9SAdrian Bunk static void
2141da177e4SLinus Torvalds __nvram_set_checksum(void)
2151da177e4SLinus Torvalds {
2161da177e4SLinus Torvalds 	mach_set_checksum();
2171da177e4SLinus Torvalds }
2181da177e4SLinus Torvalds 
219681ea4b9SAdrian Bunk #if 0
2201da177e4SLinus Torvalds void
2211da177e4SLinus Torvalds nvram_set_checksum(void)
2221da177e4SLinus Torvalds {
2231da177e4SLinus Torvalds 	unsigned long flags;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	spin_lock_irqsave(&rtc_lock, flags);
2261da177e4SLinus Torvalds 	__nvram_set_checksum();
2271da177e4SLinus Torvalds 	spin_unlock_irqrestore(&rtc_lock, flags);
2281da177e4SLinus Torvalds }
229681ea4b9SAdrian Bunk #endif  /*  0  */
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds /*
2321da177e4SLinus Torvalds  * The are the file operation function for user access to /dev/nvram
2331da177e4SLinus Torvalds  */
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds static loff_t nvram_llseek(struct file *file,loff_t offset, int origin )
2361da177e4SLinus Torvalds {
2371da177e4SLinus Torvalds 	lock_kernel();
2381da177e4SLinus Torvalds 	switch (origin) {
2391da177e4SLinus Torvalds 	case 0:
2401da177e4SLinus Torvalds 		/* nothing to do */
2411da177e4SLinus Torvalds 		break;
2421da177e4SLinus Torvalds 	case 1:
2431da177e4SLinus Torvalds 		offset += file->f_pos;
2441da177e4SLinus Torvalds 		break;
2451da177e4SLinus Torvalds 	case 2:
2461da177e4SLinus Torvalds 		offset += NVRAM_BYTES;
2471da177e4SLinus Torvalds 		break;
2481da177e4SLinus Torvalds 	}
2491da177e4SLinus Torvalds 	unlock_kernel();
2501da177e4SLinus Torvalds 	return (offset >= 0) ? (file->f_pos = offset) : -EINVAL;
2511da177e4SLinus Torvalds }
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds static ssize_t
2541da177e4SLinus Torvalds nvram_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2551da177e4SLinus Torvalds {
2561da177e4SLinus Torvalds 	unsigned char contents[NVRAM_BYTES];
2571da177e4SLinus Torvalds 	unsigned i = *ppos;
2581da177e4SLinus Torvalds 	unsigned char *tmp;
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	spin_lock_irq(&rtc_lock);
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	if (!__nvram_check_checksum())
2631da177e4SLinus Torvalds 		goto checksum_err;
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	for (tmp = contents; count-- > 0 && i < NVRAM_BYTES; ++i, ++tmp)
2661da177e4SLinus Torvalds 		*tmp = __nvram_read_byte(i);
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	spin_unlock_irq(&rtc_lock);
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	if (copy_to_user(buf, contents, tmp - contents))
2711da177e4SLinus Torvalds 		return -EFAULT;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	*ppos = i;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	return tmp - contents;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds       checksum_err:
2781da177e4SLinus Torvalds 	spin_unlock_irq(&rtc_lock);
2791da177e4SLinus Torvalds 	return -EIO;
2801da177e4SLinus Torvalds }
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds static ssize_t
2831da177e4SLinus Torvalds nvram_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
2841da177e4SLinus Torvalds {
2851da177e4SLinus Torvalds 	unsigned char contents[NVRAM_BYTES];
2861da177e4SLinus Torvalds 	unsigned i = *ppos;
2871da177e4SLinus Torvalds 	unsigned char *tmp;
2881da177e4SLinus Torvalds 	int len;
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	len = (NVRAM_BYTES - i) < count ? (NVRAM_BYTES - i) : count;
2911da177e4SLinus Torvalds 	if (copy_from_user(contents, buf, len))
2921da177e4SLinus Torvalds 		return -EFAULT;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	spin_lock_irq(&rtc_lock);
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	if (!__nvram_check_checksum())
2971da177e4SLinus Torvalds 		goto checksum_err;
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	for (tmp = contents; count-- > 0 && i < NVRAM_BYTES; ++i, ++tmp)
3001da177e4SLinus Torvalds 		__nvram_write_byte(*tmp, i);
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	__nvram_set_checksum();
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	spin_unlock_irq(&rtc_lock);
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	*ppos = i;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	return tmp - contents;
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds       checksum_err:
3111da177e4SLinus Torvalds 	spin_unlock_irq(&rtc_lock);
3121da177e4SLinus Torvalds 	return -EIO;
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds static int
3161da177e4SLinus Torvalds nvram_ioctl(struct inode *inode, struct file *file,
3171da177e4SLinus Torvalds     unsigned int cmd, unsigned long arg)
3181da177e4SLinus Torvalds {
3191da177e4SLinus Torvalds 	int i;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	switch (cmd) {
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 	case NVRAM_INIT:
3241da177e4SLinus Torvalds 		/* initialize NVRAM contents and checksum */
3251da177e4SLinus Torvalds 		if (!capable(CAP_SYS_ADMIN))
3261da177e4SLinus Torvalds 			return -EACCES;
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 		spin_lock_irq(&rtc_lock);
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 		for (i = 0; i < NVRAM_BYTES; ++i)
3311da177e4SLinus Torvalds 			__nvram_write_byte(0, i);
3321da177e4SLinus Torvalds 		__nvram_set_checksum();
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 		spin_unlock_irq(&rtc_lock);
3351da177e4SLinus Torvalds 		return 0;
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	case NVRAM_SETCKS:
3381da177e4SLinus Torvalds 		/* just set checksum, contents unchanged (maybe useful after
3391da177e4SLinus Torvalds 		 * checksum garbaged somehow...) */
3401da177e4SLinus Torvalds 		if (!capable(CAP_SYS_ADMIN))
3411da177e4SLinus Torvalds 			return -EACCES;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 		spin_lock_irq(&rtc_lock);
3441da177e4SLinus Torvalds 		__nvram_set_checksum();
3451da177e4SLinus Torvalds 		spin_unlock_irq(&rtc_lock);
3461da177e4SLinus Torvalds 		return 0;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	default:
3491da177e4SLinus Torvalds 		return -ENOTTY;
3501da177e4SLinus Torvalds 	}
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds static int
3541da177e4SLinus Torvalds nvram_open(struct inode *inode, struct file *file)
3551da177e4SLinus Torvalds {
3561da177e4SLinus Torvalds 	spin_lock(&nvram_state_lock);
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	if ((nvram_open_cnt && (file->f_flags & O_EXCL)) ||
3591da177e4SLinus Torvalds 	    (nvram_open_mode & NVRAM_EXCL) ||
3601da177e4SLinus Torvalds 	    ((file->f_mode & 2) && (nvram_open_mode & NVRAM_WRITE))) {
3611da177e4SLinus Torvalds 		spin_unlock(&nvram_state_lock);
3621da177e4SLinus Torvalds 		return -EBUSY;
3631da177e4SLinus Torvalds 	}
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	if (file->f_flags & O_EXCL)
3661da177e4SLinus Torvalds 		nvram_open_mode |= NVRAM_EXCL;
3671da177e4SLinus Torvalds 	if (file->f_mode & 2)
3681da177e4SLinus Torvalds 		nvram_open_mode |= NVRAM_WRITE;
3691da177e4SLinus Torvalds 	nvram_open_cnt++;
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	spin_unlock(&nvram_state_lock);
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	return 0;
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds static int
3771da177e4SLinus Torvalds nvram_release(struct inode *inode, struct file *file)
3781da177e4SLinus Torvalds {
3791da177e4SLinus Torvalds 	spin_lock(&nvram_state_lock);
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	nvram_open_cnt--;
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	/* if only one instance is open, clear the EXCL bit */
3841da177e4SLinus Torvalds 	if (nvram_open_mode & NVRAM_EXCL)
3851da177e4SLinus Torvalds 		nvram_open_mode &= ~NVRAM_EXCL;
3861da177e4SLinus Torvalds 	if (file->f_mode & 2)
3871da177e4SLinus Torvalds 		nvram_open_mode &= ~NVRAM_WRITE;
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	spin_unlock(&nvram_state_lock);
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	return 0;
3921da177e4SLinus Torvalds }
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds #ifndef CONFIG_PROC_FS
3951da177e4SLinus Torvalds static int
3961da177e4SLinus Torvalds nvram_read_proc(char *buffer, char **start, off_t offset,
3971da177e4SLinus Torvalds     int size, int *eof, void *data)
3981da177e4SLinus Torvalds {
3991da177e4SLinus Torvalds 	return 0;
4001da177e4SLinus Torvalds }
4011da177e4SLinus Torvalds #else
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds static int
4041da177e4SLinus Torvalds nvram_read_proc(char *buffer, char **start, off_t offset,
4051da177e4SLinus Torvalds     int size, int *eof, void *data)
4061da177e4SLinus Torvalds {
4071da177e4SLinus Torvalds 	unsigned char contents[NVRAM_BYTES];
4081da177e4SLinus Torvalds 	int i, len = 0;
4091da177e4SLinus Torvalds 	off_t begin = 0;
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	spin_lock_irq(&rtc_lock);
4121da177e4SLinus Torvalds 	for (i = 0; i < NVRAM_BYTES; ++i)
4131da177e4SLinus Torvalds 		contents[i] = __nvram_read_byte(i);
4141da177e4SLinus Torvalds 	spin_unlock_irq(&rtc_lock);
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	*eof = mach_proc_infos(contents, buffer, &len, &begin, offset, size);
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	if (offset >= begin + len)
4191da177e4SLinus Torvalds 		return 0;
4201da177e4SLinus Torvalds 	*start = buffer + (offset - begin);
4211da177e4SLinus Torvalds 	return (size < begin + len - offset) ? size : begin + len - offset;
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds }
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds /* This macro frees the machine specific function from bounds checking and
4261da177e4SLinus Torvalds  * this like that... */
4271da177e4SLinus Torvalds #define PRINT_PROC(fmt,args...)					\
4281da177e4SLinus Torvalds 	do {							\
4291da177e4SLinus Torvalds 		*len += sprintf(buffer+*len, fmt, ##args);	\
4301da177e4SLinus Torvalds 		if (*begin + *len > offset + size)		\
4311da177e4SLinus Torvalds 			return 0;				\
4321da177e4SLinus Torvalds 		if (*begin + *len < offset) {			\
4331da177e4SLinus Torvalds 			*begin += *len;				\
4341da177e4SLinus Torvalds 			*len = 0;				\
4351da177e4SLinus Torvalds 		}						\
4361da177e4SLinus Torvalds 	} while(0)
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
4391da177e4SLinus Torvalds 
440*62322d25SArjan van de Ven static const struct file_operations nvram_fops = {
4411da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
4421da177e4SLinus Torvalds 	.llseek		= nvram_llseek,
4431da177e4SLinus Torvalds 	.read		= nvram_read,
4441da177e4SLinus Torvalds 	.write		= nvram_write,
4451da177e4SLinus Torvalds 	.ioctl		= nvram_ioctl,
4461da177e4SLinus Torvalds 	.open		= nvram_open,
4471da177e4SLinus Torvalds 	.release	= nvram_release,
4481da177e4SLinus Torvalds };
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds static struct miscdevice nvram_dev = {
4511da177e4SLinus Torvalds 	NVRAM_MINOR,
4521da177e4SLinus Torvalds 	"nvram",
4531da177e4SLinus Torvalds 	&nvram_fops
4541da177e4SLinus Torvalds };
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds static int __init
4571da177e4SLinus Torvalds nvram_init(void)
4581da177e4SLinus Torvalds {
4591da177e4SLinus Torvalds 	int ret;
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	/* First test whether the driver should init at all */
4621da177e4SLinus Torvalds 	if (!CHECK_DRIVER_INIT())
4631da177e4SLinus Torvalds 		return -ENXIO;
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	ret = misc_register(&nvram_dev);
4661da177e4SLinus Torvalds 	if (ret) {
4671da177e4SLinus Torvalds 		printk(KERN_ERR "nvram: can't misc_register on minor=%d\n",
4681da177e4SLinus Torvalds 		    NVRAM_MINOR);
4691da177e4SLinus Torvalds 		goto out;
4701da177e4SLinus Torvalds 	}
4711da177e4SLinus Torvalds 	if (!create_proc_read_entry("driver/nvram", 0, NULL, nvram_read_proc,
4721da177e4SLinus Torvalds 		NULL)) {
4731da177e4SLinus Torvalds 		printk(KERN_ERR "nvram: can't create /proc/driver/nvram\n");
4741da177e4SLinus Torvalds 		ret = -ENOMEM;
4751da177e4SLinus Torvalds 		goto outmisc;
4761da177e4SLinus Torvalds 	}
4771da177e4SLinus Torvalds 	ret = 0;
4781da177e4SLinus Torvalds 	printk(KERN_INFO "Non-volatile memory driver v" NVRAM_VERSION "\n");
4791da177e4SLinus Torvalds       out:
4801da177e4SLinus Torvalds 	return ret;
4811da177e4SLinus Torvalds       outmisc:
4821da177e4SLinus Torvalds 	misc_deregister(&nvram_dev);
4831da177e4SLinus Torvalds 	goto out;
4841da177e4SLinus Torvalds }
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds static void __exit
4871da177e4SLinus Torvalds nvram_cleanup_module(void)
4881da177e4SLinus Torvalds {
4891da177e4SLinus Torvalds 	remove_proc_entry("driver/nvram", NULL);
4901da177e4SLinus Torvalds 	misc_deregister(&nvram_dev);
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds module_init(nvram_init);
4941da177e4SLinus Torvalds module_exit(nvram_cleanup_module);
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds /*
4971da177e4SLinus Torvalds  * Machine specific functions
4981da177e4SLinus Torvalds  */
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds #if MACH == PC
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds static int
5031da177e4SLinus Torvalds pc_check_checksum(void)
5041da177e4SLinus Torvalds {
5051da177e4SLinus Torvalds 	int i;
5061da177e4SLinus Torvalds 	unsigned short sum = 0;
5071da177e4SLinus Torvalds 	unsigned short expect;
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i)
5101da177e4SLinus Torvalds 		sum += __nvram_read_byte(i);
5111da177e4SLinus Torvalds 	expect = __nvram_read_byte(PC_CKS_LOC)<<8 |
5121da177e4SLinus Torvalds 	    __nvram_read_byte(PC_CKS_LOC+1);
5131da177e4SLinus Torvalds 	return ((sum & 0xffff) == expect);
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds static void
5171da177e4SLinus Torvalds pc_set_checksum(void)
5181da177e4SLinus Torvalds {
5191da177e4SLinus Torvalds 	int i;
5201da177e4SLinus Torvalds 	unsigned short sum = 0;
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i)
5231da177e4SLinus Torvalds 		sum += __nvram_read_byte(i);
5241da177e4SLinus Torvalds 	__nvram_write_byte(sum >> 8, PC_CKS_LOC);
5251da177e4SLinus Torvalds 	__nvram_write_byte(sum & 0xff, PC_CKS_LOC + 1);
5261da177e4SLinus Torvalds }
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds static char *floppy_types[] = {
5311da177e4SLinus Torvalds 	"none", "5.25'' 360k", "5.25'' 1.2M", "3.5'' 720k", "3.5'' 1.44M",
5321da177e4SLinus Torvalds 	"3.5'' 2.88M", "3.5'' 2.88M"
5331da177e4SLinus Torvalds };
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds static char *gfx_types[] = {
5361da177e4SLinus Torvalds 	"EGA, VGA, ... (with BIOS)",
5371da177e4SLinus Torvalds 	"CGA (40 cols)",
5381da177e4SLinus Torvalds 	"CGA (80 cols)",
5391da177e4SLinus Torvalds 	"monochrome",
5401da177e4SLinus Torvalds };
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds static int
5431da177e4SLinus Torvalds pc_proc_infos(unsigned char *nvram, char *buffer, int *len,
5441da177e4SLinus Torvalds     off_t *begin, off_t offset, int size)
5451da177e4SLinus Torvalds {
5461da177e4SLinus Torvalds 	int checksum;
5471da177e4SLinus Torvalds 	int type;
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 	spin_lock_irq(&rtc_lock);
5501da177e4SLinus Torvalds 	checksum = __nvram_check_checksum();
5511da177e4SLinus Torvalds 	spin_unlock_irq(&rtc_lock);
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	PRINT_PROC("Checksum status: %svalid\n", checksum ? "" : "not ");
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 	PRINT_PROC("# floppies     : %d\n",
5561da177e4SLinus Torvalds 	    (nvram[6] & 1) ? (nvram[6] >> 6) + 1 : 0);
5571da177e4SLinus Torvalds 	PRINT_PROC("Floppy 0 type  : ");
5581da177e4SLinus Torvalds 	type = nvram[2] >> 4;
559fe971071STobias Klauser 	if (type < ARRAY_SIZE(floppy_types))
5601da177e4SLinus Torvalds 		PRINT_PROC("%s\n", floppy_types[type]);
5611da177e4SLinus Torvalds 	else
5621da177e4SLinus Torvalds 		PRINT_PROC("%d (unknown)\n", type);
5631da177e4SLinus Torvalds 	PRINT_PROC("Floppy 1 type  : ");
5641da177e4SLinus Torvalds 	type = nvram[2] & 0x0f;
565fe971071STobias Klauser 	if (type < ARRAY_SIZE(floppy_types))
5661da177e4SLinus Torvalds 		PRINT_PROC("%s\n", floppy_types[type]);
5671da177e4SLinus Torvalds 	else
5681da177e4SLinus Torvalds 		PRINT_PROC("%d (unknown)\n", type);
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	PRINT_PROC("HD 0 type      : ");
5711da177e4SLinus Torvalds 	type = nvram[4] >> 4;
5721da177e4SLinus Torvalds 	if (type)
5731da177e4SLinus Torvalds 		PRINT_PROC("%02x\n", type == 0x0f ? nvram[11] : type);
5741da177e4SLinus Torvalds 	else
5751da177e4SLinus Torvalds 		PRINT_PROC("none\n");
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	PRINT_PROC("HD 1 type      : ");
5781da177e4SLinus Torvalds 	type = nvram[4] & 0x0f;
5791da177e4SLinus Torvalds 	if (type)
5801da177e4SLinus Torvalds 		PRINT_PROC("%02x\n", type == 0x0f ? nvram[12] : type);
5811da177e4SLinus Torvalds 	else
5821da177e4SLinus Torvalds 		PRINT_PROC("none\n");
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	PRINT_PROC("HD type 48 data: %d/%d/%d C/H/S, precomp %d, lz %d\n",
5851da177e4SLinus Torvalds 	    nvram[18] | (nvram[19] << 8),
5861da177e4SLinus Torvalds 	    nvram[20], nvram[25],
5871da177e4SLinus Torvalds 	    nvram[21] | (nvram[22] << 8), nvram[23] | (nvram[24] << 8));
5881da177e4SLinus Torvalds 	PRINT_PROC("HD type 49 data: %d/%d/%d C/H/S, precomp %d, lz %d\n",
5891da177e4SLinus Torvalds 	    nvram[39] | (nvram[40] << 8),
5901da177e4SLinus Torvalds 	    nvram[41], nvram[46],
5911da177e4SLinus Torvalds 	    nvram[42] | (nvram[43] << 8), nvram[44] | (nvram[45] << 8));
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 	PRINT_PROC("DOS base memory: %d kB\n", nvram[7] | (nvram[8] << 8));
5941da177e4SLinus Torvalds 	PRINT_PROC("Extended memory: %d kB (configured), %d kB (tested)\n",
5951da177e4SLinus Torvalds 	    nvram[9] | (nvram[10] << 8), nvram[34] | (nvram[35] << 8));
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	PRINT_PROC("Gfx adapter    : %s\n", gfx_types[(nvram[6] >> 4) & 3]);
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	PRINT_PROC("FPU            : %sinstalled\n",
6001da177e4SLinus Torvalds 	    (nvram[6] & 2) ? "" : "not ");
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	return 1;
6031da177e4SLinus Torvalds }
6041da177e4SLinus Torvalds #endif
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds #endif /* MACH == PC */
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds #if MACH == COBALT
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds /* the cobalt CMOS has a wider range of its checksum */
6111da177e4SLinus Torvalds static int cobalt_check_checksum(void)
6121da177e4SLinus Torvalds {
6131da177e4SLinus Torvalds 	int i;
6141da177e4SLinus Torvalds 	unsigned short sum = 0;
6151da177e4SLinus Torvalds 	unsigned short expect;
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	for (i = COBT_CMOS_CKS_START; i <= COBT_CMOS_CKS_END; ++i) {
6181da177e4SLinus Torvalds 		if ((i == COBT_CMOS_CHECKSUM) || (i == (COBT_CMOS_CHECKSUM+1)))
6191da177e4SLinus Torvalds 			continue;
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 		sum += __nvram_read_byte(i);
6221da177e4SLinus Torvalds 	}
6231da177e4SLinus Torvalds 	expect = __nvram_read_byte(COBT_CMOS_CHECKSUM) << 8 |
6241da177e4SLinus Torvalds 	    __nvram_read_byte(COBT_CMOS_CHECKSUM+1);
6251da177e4SLinus Torvalds 	return ((sum & 0xffff) == expect);
6261da177e4SLinus Torvalds }
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds static void cobalt_set_checksum(void)
6291da177e4SLinus Torvalds {
6301da177e4SLinus Torvalds 	int i;
6311da177e4SLinus Torvalds 	unsigned short sum = 0;
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	for (i = COBT_CMOS_CKS_START; i <= COBT_CMOS_CKS_END; ++i) {
6341da177e4SLinus Torvalds 		if ((i == COBT_CMOS_CHECKSUM) || (i == (COBT_CMOS_CHECKSUM+1)))
6351da177e4SLinus Torvalds 			continue;
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 		sum += __nvram_read_byte(i);
6381da177e4SLinus Torvalds 	}
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	__nvram_write_byte(sum >> 8, COBT_CMOS_CHECKSUM);
6411da177e4SLinus Torvalds 	__nvram_write_byte(sum & 0xff, COBT_CMOS_CHECKSUM+1);
6421da177e4SLinus Torvalds }
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds static int cobalt_proc_infos(unsigned char *nvram, char *buffer, int *len,
6471da177e4SLinus Torvalds 	off_t *begin, off_t offset, int size)
6481da177e4SLinus Torvalds {
6491da177e4SLinus Torvalds 	int i;
6501da177e4SLinus Torvalds 	unsigned int checksum;
6511da177e4SLinus Torvalds 	unsigned int flags;
6521da177e4SLinus Torvalds 	char sernum[14];
6531da177e4SLinus Torvalds 	char *key = "cNoEbTaWlOtR!";
6541da177e4SLinus Torvalds 	unsigned char bto_csum;
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	spin_lock_irq(&rtc_lock);
6571da177e4SLinus Torvalds 	checksum = __nvram_check_checksum();
6581da177e4SLinus Torvalds 	spin_unlock_irq(&rtc_lock);
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	PRINT_PROC("Checksum status: %svalid\n", checksum ? "" : "not ");
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 	flags = nvram[COBT_CMOS_FLAG_BYTE_0] << 8
6631da177e4SLinus Torvalds 	    | nvram[COBT_CMOS_FLAG_BYTE_1];
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 	PRINT_PROC("Console: %s\n",
6661da177e4SLinus Torvalds 		flags & COBT_CMOS_CONSOLE_FLAG ?  "on": "off");
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	PRINT_PROC("Firmware Debug Messages: %s\n",
6691da177e4SLinus Torvalds 		flags & COBT_CMOS_DEBUG_FLAG ? "on": "off");
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 	PRINT_PROC("Auto Prompt: %s\n",
6721da177e4SLinus Torvalds 		flags & COBT_CMOS_AUTO_PROMPT_FLAG ? "on": "off");
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	PRINT_PROC("Shutdown Status: %s\n",
6751da177e4SLinus Torvalds 		flags & COBT_CMOS_CLEAN_BOOT_FLAG ? "clean": "dirty");
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 	PRINT_PROC("Hardware Probe: %s\n",
6781da177e4SLinus Torvalds 		flags & COBT_CMOS_HW_NOPROBE_FLAG ? "partial": "full");
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	PRINT_PROC("System Fault: %sdetected\n",
6811da177e4SLinus Torvalds 		flags & COBT_CMOS_SYSFAULT_FLAG ? "": "not ");
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	PRINT_PROC("Panic on OOPS: %s\n",
6841da177e4SLinus Torvalds 		flags & COBT_CMOS_OOPSPANIC_FLAG ? "yes": "no");
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 	PRINT_PROC("Delayed Cache Initialization: %s\n",
6871da177e4SLinus Torvalds 		flags & COBT_CMOS_DELAY_CACHE_FLAG ? "yes": "no");
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	PRINT_PROC("Show Logo at Boot: %s\n",
6901da177e4SLinus Torvalds 		flags & COBT_CMOS_NOLOGO_FLAG ? "no": "yes");
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	PRINT_PROC("Boot Method: ");
6931da177e4SLinus Torvalds 	switch (nvram[COBT_CMOS_BOOT_METHOD]) {
6941da177e4SLinus Torvalds 	case COBT_CMOS_BOOT_METHOD_DISK:
6951da177e4SLinus Torvalds 		PRINT_PROC("disk\n");
6961da177e4SLinus Torvalds 		break;
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	case COBT_CMOS_BOOT_METHOD_ROM:
6991da177e4SLinus Torvalds 		PRINT_PROC("rom\n");
7001da177e4SLinus Torvalds 		break;
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 	case COBT_CMOS_BOOT_METHOD_NET:
7031da177e4SLinus Torvalds 		PRINT_PROC("net\n");
7041da177e4SLinus Torvalds 		break;
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	default:
7071da177e4SLinus Torvalds 		PRINT_PROC("unknown\n");
7081da177e4SLinus Torvalds 		break;
7091da177e4SLinus Torvalds 	}
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 	PRINT_PROC("Primary Boot Device: %d:%d\n",
7121da177e4SLinus Torvalds 		nvram[COBT_CMOS_BOOT_DEV0_MAJ],
7131da177e4SLinus Torvalds 		nvram[COBT_CMOS_BOOT_DEV0_MIN] );
7141da177e4SLinus Torvalds 	PRINT_PROC("Secondary Boot Device: %d:%d\n",
7151da177e4SLinus Torvalds 		nvram[COBT_CMOS_BOOT_DEV1_MAJ],
7161da177e4SLinus Torvalds 		nvram[COBT_CMOS_BOOT_DEV1_MIN] );
7171da177e4SLinus Torvalds 	PRINT_PROC("Tertiary Boot Device: %d:%d\n",
7181da177e4SLinus Torvalds 		nvram[COBT_CMOS_BOOT_DEV2_MAJ],
7191da177e4SLinus Torvalds 		nvram[COBT_CMOS_BOOT_DEV2_MIN] );
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 	PRINT_PROC("Uptime: %d\n",
7221da177e4SLinus Torvalds 		nvram[COBT_CMOS_UPTIME_0] << 24 |
7231da177e4SLinus Torvalds 		nvram[COBT_CMOS_UPTIME_1] << 16 |
7241da177e4SLinus Torvalds 		nvram[COBT_CMOS_UPTIME_2] << 8  |
7251da177e4SLinus Torvalds 		nvram[COBT_CMOS_UPTIME_3]);
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 	PRINT_PROC("Boot Count: %d\n",
7281da177e4SLinus Torvalds 		nvram[COBT_CMOS_BOOTCOUNT_0] << 24 |
7291da177e4SLinus Torvalds 		nvram[COBT_CMOS_BOOTCOUNT_1] << 16 |
7301da177e4SLinus Torvalds 		nvram[COBT_CMOS_BOOTCOUNT_2] << 8  |
7311da177e4SLinus Torvalds 		nvram[COBT_CMOS_BOOTCOUNT_3]);
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 	/* 13 bytes of serial num */
7341da177e4SLinus Torvalds 	for (i=0 ; i<13 ; i++) {
7351da177e4SLinus Torvalds 		sernum[i] = nvram[COBT_CMOS_SYS_SERNUM_0 + i];
7361da177e4SLinus Torvalds 	}
7371da177e4SLinus Torvalds 	sernum[13] = '\0';
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	checksum = 0;
7401da177e4SLinus Torvalds 	for (i=0 ; i<13 ; i++) {
7411da177e4SLinus Torvalds 		checksum += sernum[i] ^ key[i];
7421da177e4SLinus Torvalds 	}
7431da177e4SLinus Torvalds 	checksum = ((checksum & 0x7f) ^ (0xd6)) & 0xff;
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 	PRINT_PROC("Serial Number: %s", sernum);
7461da177e4SLinus Torvalds 	if (checksum != nvram[COBT_CMOS_SYS_SERNUM_CSUM]) {
7471da177e4SLinus Torvalds 		PRINT_PROC(" (invalid checksum)");
7481da177e4SLinus Torvalds 	}
7491da177e4SLinus Torvalds 	PRINT_PROC("\n");
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 	PRINT_PROC("Rom Revison: %d.%d.%d\n", nvram[COBT_CMOS_ROM_REV_MAJ],
7521da177e4SLinus Torvalds 		nvram[COBT_CMOS_ROM_REV_MIN], nvram[COBT_CMOS_ROM_REV_REV]);
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 	PRINT_PROC("BTO Server: %d.%d.%d.%d", nvram[COBT_CMOS_BTO_IP_0],
7551da177e4SLinus Torvalds 		nvram[COBT_CMOS_BTO_IP_1], nvram[COBT_CMOS_BTO_IP_2],
7561da177e4SLinus Torvalds 		nvram[COBT_CMOS_BTO_IP_3]);
7571da177e4SLinus Torvalds 	bto_csum = nvram[COBT_CMOS_BTO_IP_0] + nvram[COBT_CMOS_BTO_IP_1]
7581da177e4SLinus Torvalds 		+ nvram[COBT_CMOS_BTO_IP_2] + nvram[COBT_CMOS_BTO_IP_3];
7591da177e4SLinus Torvalds 	if (bto_csum != nvram[COBT_CMOS_BTO_IP_CSUM]) {
7601da177e4SLinus Torvalds 		PRINT_PROC(" (invalid checksum)");
7611da177e4SLinus Torvalds 	}
7621da177e4SLinus Torvalds 	PRINT_PROC("\n");
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 	if (flags & COBT_CMOS_VERSION_FLAG
7651da177e4SLinus Torvalds 	 && nvram[COBT_CMOS_VERSION] >= COBT_CMOS_VER_BTOCODE) {
7661da177e4SLinus Torvalds 		PRINT_PROC("BTO Code: 0x%x\n",
7671da177e4SLinus Torvalds 			nvram[COBT_CMOS_BTO_CODE_0] << 24 |
7681da177e4SLinus Torvalds 			nvram[COBT_CMOS_BTO_CODE_1] << 16 |
7691da177e4SLinus Torvalds 			nvram[COBT_CMOS_BTO_CODE_2] << 8 |
7701da177e4SLinus Torvalds 			nvram[COBT_CMOS_BTO_CODE_3]);
7711da177e4SLinus Torvalds 	}
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	return 1;
7741da177e4SLinus Torvalds }
7751da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds #endif /* MACH == COBALT */
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds #if MACH == ATARI
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds static int
7821da177e4SLinus Torvalds atari_check_checksum(void)
7831da177e4SLinus Torvalds {
7841da177e4SLinus Torvalds 	int i;
7851da177e4SLinus Torvalds 	unsigned char sum = 0;
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	for (i = ATARI_CKS_RANGE_START; i <= ATARI_CKS_RANGE_END; ++i)
7881da177e4SLinus Torvalds 		sum += __nvram_read_byte(i);
7891da177e4SLinus Torvalds 	return (__nvram_read_byte(ATARI_CKS_LOC) == (~sum & 0xff) &&
7901da177e4SLinus Torvalds 	    __nvram_read_byte(ATARI_CKS_LOC + 1) == (sum & 0xff));
7911da177e4SLinus Torvalds }
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds static void
7941da177e4SLinus Torvalds atari_set_checksum(void)
7951da177e4SLinus Torvalds {
7961da177e4SLinus Torvalds 	int i;
7971da177e4SLinus Torvalds 	unsigned char sum = 0;
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	for (i = ATARI_CKS_RANGE_START; i <= ATARI_CKS_RANGE_END; ++i)
8001da177e4SLinus Torvalds 		sum += __nvram_read_byte(i);
8011da177e4SLinus Torvalds 	__nvram_write_byte(~sum, ATARI_CKS_LOC);
8021da177e4SLinus Torvalds 	__nvram_write_byte(sum, ATARI_CKS_LOC + 1);
8031da177e4SLinus Torvalds }
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds static struct {
8081da177e4SLinus Torvalds 	unsigned char val;
8091da177e4SLinus Torvalds 	char *name;
8101da177e4SLinus Torvalds } boot_prefs[] = {
8111da177e4SLinus Torvalds 	{ 0x80, "TOS" },
8121da177e4SLinus Torvalds 	{ 0x40, "ASV" },
8131da177e4SLinus Torvalds 	{ 0x20, "NetBSD (?)" },
8141da177e4SLinus Torvalds 	{ 0x10, "Linux" },
8151da177e4SLinus Torvalds 	{ 0x00, "unspecified" }
8161da177e4SLinus Torvalds };
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds static char *languages[] = {
8191da177e4SLinus Torvalds 	"English (US)",
8201da177e4SLinus Torvalds 	"German",
8211da177e4SLinus Torvalds 	"French",
8221da177e4SLinus Torvalds 	"English (UK)",
8231da177e4SLinus Torvalds 	"Spanish",
8241da177e4SLinus Torvalds 	"Italian",
8251da177e4SLinus Torvalds 	"6 (undefined)",
8261da177e4SLinus Torvalds 	"Swiss (French)",
8271da177e4SLinus Torvalds 	"Swiss (German)"
8281da177e4SLinus Torvalds };
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds static char *dateformat[] = {
8311da177e4SLinus Torvalds 	"MM%cDD%cYY",
8321da177e4SLinus Torvalds 	"DD%cMM%cYY",
8331da177e4SLinus Torvalds 	"YY%cMM%cDD",
8341da177e4SLinus Torvalds 	"YY%cDD%cMM",
8351da177e4SLinus Torvalds 	"4 (undefined)",
8361da177e4SLinus Torvalds 	"5 (undefined)",
8371da177e4SLinus Torvalds 	"6 (undefined)",
8381da177e4SLinus Torvalds 	"7 (undefined)"
8391da177e4SLinus Torvalds };
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds static char *colors[] = {
8421da177e4SLinus Torvalds 	"2", "4", "16", "256", "65536", "??", "??", "??"
8431da177e4SLinus Torvalds };
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds static int
8461da177e4SLinus Torvalds atari_proc_infos(unsigned char *nvram, char *buffer, int *len,
8471da177e4SLinus Torvalds     off_t *begin, off_t offset, int size)
8481da177e4SLinus Torvalds {
8491da177e4SLinus Torvalds 	int checksum = nvram_check_checksum();
8501da177e4SLinus Torvalds 	int i;
8511da177e4SLinus Torvalds 	unsigned vmode;
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 	PRINT_PROC("Checksum status  : %svalid\n", checksum ? "" : "not ");
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	PRINT_PROC("Boot preference  : ");
856fe971071STobias Klauser 	for (i = ARRAY_SIZE(boot_prefs) - 1; i >= 0; --i) {
8571da177e4SLinus Torvalds 		if (nvram[1] == boot_prefs[i].val) {
8581da177e4SLinus Torvalds 			PRINT_PROC("%s\n", boot_prefs[i].name);
8591da177e4SLinus Torvalds 			break;
8601da177e4SLinus Torvalds 		}
8611da177e4SLinus Torvalds 	}
8621da177e4SLinus Torvalds 	if (i < 0)
8631da177e4SLinus Torvalds 		PRINT_PROC("0x%02x (undefined)\n", nvram[1]);
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	PRINT_PROC("SCSI arbitration : %s\n",
8661da177e4SLinus Torvalds 	    (nvram[16] & 0x80) ? "on" : "off");
8671da177e4SLinus Torvalds 	PRINT_PROC("SCSI host ID     : ");
8681da177e4SLinus Torvalds 	if (nvram[16] & 0x80)
8691da177e4SLinus Torvalds 		PRINT_PROC("%d\n", nvram[16] & 7);
8701da177e4SLinus Torvalds 	else
8711da177e4SLinus Torvalds 		PRINT_PROC("n/a\n");
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	/* the following entries are defined only for the Falcon */
8741da177e4SLinus Torvalds 	if ((atari_mch_cookie >> 16) != ATARI_MCH_FALCON)
8751da177e4SLinus Torvalds 		return 1;
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	PRINT_PROC("OS language      : ");
878fe971071STobias Klauser 	if (nvram[6] < ARRAY_SIZE(languages))
8791da177e4SLinus Torvalds 		PRINT_PROC("%s\n", languages[nvram[6]]);
8801da177e4SLinus Torvalds 	else
8811da177e4SLinus Torvalds 		PRINT_PROC("%u (undefined)\n", nvram[6]);
8821da177e4SLinus Torvalds 	PRINT_PROC("Keyboard language: ");
883fe971071STobias Klauser 	if (nvram[7] < ARRAY_SIZE(languages))
8841da177e4SLinus Torvalds 		PRINT_PROC("%s\n", languages[nvram[7]]);
8851da177e4SLinus Torvalds 	else
8861da177e4SLinus Torvalds 		PRINT_PROC("%u (undefined)\n", nvram[7]);
8871da177e4SLinus Torvalds 	PRINT_PROC("Date format      : ");
8881da177e4SLinus Torvalds 	PRINT_PROC(dateformat[nvram[8] & 7],
8891da177e4SLinus Torvalds 	    nvram[9] ? nvram[9] : '/', nvram[9] ? nvram[9] : '/');
8901da177e4SLinus Torvalds 	PRINT_PROC(", %dh clock\n", nvram[8] & 16 ? 24 : 12);
8911da177e4SLinus Torvalds 	PRINT_PROC("Boot delay       : ");
8921da177e4SLinus Torvalds 	if (nvram[10] == 0)
8931da177e4SLinus Torvalds 		PRINT_PROC("default");
8941da177e4SLinus Torvalds 	else
8951da177e4SLinus Torvalds 		PRINT_PROC("%ds%s\n", nvram[10],
8961da177e4SLinus Torvalds 		    nvram[10] < 8 ? ", no memory test" : "");
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	vmode = (nvram[14] << 8) || nvram[15];
8991da177e4SLinus Torvalds 	PRINT_PROC("Video mode       : %s colors, %d columns, %s %s monitor\n",
9001da177e4SLinus Torvalds 	    colors[vmode & 7],
9011da177e4SLinus Torvalds 	    vmode & 8 ? 80 : 40,
9021da177e4SLinus Torvalds 	    vmode & 16 ? "VGA" : "TV", vmode & 32 ? "PAL" : "NTSC");
9031da177e4SLinus Torvalds 	PRINT_PROC("                   %soverscan, compat. mode %s%s\n",
9041da177e4SLinus Torvalds 	    vmode & 64 ? "" : "no ",
9051da177e4SLinus Torvalds 	    vmode & 128 ? "on" : "off",
9061da177e4SLinus Torvalds 	    vmode & 256 ?
9071da177e4SLinus Torvalds 	    (vmode & 16 ? ", line doubling" : ", half screen") : "");
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 	return 1;
9101da177e4SLinus Torvalds }
9111da177e4SLinus Torvalds #endif
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds #endif /* MACH == ATARI */
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds MODULE_LICENSE("GPL");
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds EXPORT_SYMBOL(__nvram_read_byte);
9181da177e4SLinus Torvalds EXPORT_SYMBOL(nvram_read_byte);
9191da177e4SLinus Torvalds EXPORT_SYMBOL(__nvram_write_byte);
9201da177e4SLinus Torvalds EXPORT_SYMBOL(nvram_write_byte);
9211da177e4SLinus Torvalds EXPORT_SYMBOL(__nvram_check_checksum);
9221da177e4SLinus Torvalds EXPORT_SYMBOL(nvram_check_checksum);
9231da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(NVRAM_MINOR);
924