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