Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * CMOS/NV-RAM driver for Linux |
| 3 | * |
| 4 | * Copyright (C) 1997 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> |
| 5 | * idea by and with help from Richard Jelinek <rj@suse.de> |
| 6 | * Portions copyright (c) 2001,2002 Sun Microsystems (thockin@sun.com) |
| 7 | * |
| 8 | * This driver allows you to access the contents of the non-volatile memory in |
| 9 | * the mc146818rtc.h real-time clock. This chip is built into all PCs and into |
| 10 | * many Atari machines. In the former it's called "CMOS-RAM", in the latter |
| 11 | * "NVRAM" (NV stands for non-volatile). |
| 12 | * |
| 13 | * The data are supplied as a (seekable) character device, /dev/nvram. The |
| 14 | * size of this file is dependent on the controller. The usual size is 114, |
| 15 | * the number of freely available bytes in the memory (i.e., not used by the |
| 16 | * RTC itself). |
| 17 | * |
| 18 | * Checksums over the NVRAM contents are managed by this driver. In case of a |
| 19 | * bad checksum, reads and writes return -EIO. The checksum can be initialized |
| 20 | * to a sane state either by ioctl(NVRAM_INIT) (clear whole NVRAM) or |
| 21 | * ioctl(NVRAM_SETCKS) (doesn't change contents, just makes checksum valid |
| 22 | * again; use with care!) |
| 23 | * |
| 24 | * This file also provides some functions for other parts of the kernel that |
| 25 | * want to access the NVRAM: nvram_{read,write,check_checksum,set_checksum}. |
| 26 | * Obviously this can be used only if this driver is always configured into |
| 27 | * the kernel and is not a module. Since the functions are used by some Atari |
| 28 | * drivers, this is the case on the Atari. |
| 29 | * |
| 30 | * |
| 31 | * 1.1 Cesar Barros: SMP locking fixes |
| 32 | * added changelog |
| 33 | * 1.2 Erik Gilling: Cobalt Networks support |
| 34 | * Tim Hockin: general cleanup, Cobalt support |
| 35 | */ |
| 36 | |
Russell King | 866237e | 2005-11-04 17:26:57 +0000 | [diff] [blame] | 37 | #define NVRAM_VERSION "1.2" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/smp_lock.h> |
| 41 | #include <linux/nvram.h> |
| 42 | |
| 43 | #define PC 1 |
| 44 | #define ATARI 2 |
| 45 | #define COBALT 3 |
| 46 | |
| 47 | /* select machine configuration */ |
| 48 | #if defined(CONFIG_ATARI) |
| 49 | # define MACH ATARI |
| 50 | #elif defined(__i386__) || defined(__x86_64__) || defined(__arm__) /* and others?? */ |
| 51 | #define MACH PC |
| 52 | # if defined(CONFIG_COBALT) |
| 53 | # include <linux/cobalt-nvram.h> |
| 54 | # define MACH COBALT |
| 55 | # else |
| 56 | # define MACH PC |
| 57 | # endif |
| 58 | #else |
| 59 | # error Cannot build nvram driver for this machine configuration. |
| 60 | #endif |
| 61 | |
| 62 | #if MACH == PC |
| 63 | |
| 64 | /* RTC in a PC */ |
| 65 | #define CHECK_DRIVER_INIT() 1 |
| 66 | |
| 67 | /* On PCs, the checksum is built only over bytes 2..31 */ |
| 68 | #define PC_CKS_RANGE_START 2 |
| 69 | #define PC_CKS_RANGE_END 31 |
| 70 | #define PC_CKS_LOC 32 |
| 71 | #define NVRAM_BYTES (128-NVRAM_FIRST_BYTE) |
| 72 | |
| 73 | #define mach_check_checksum pc_check_checksum |
| 74 | #define mach_set_checksum pc_set_checksum |
| 75 | #define mach_proc_infos pc_proc_infos |
| 76 | |
| 77 | #endif |
| 78 | |
| 79 | #if MACH == COBALT |
| 80 | |
| 81 | #define CHECK_DRIVER_INIT() 1 |
| 82 | |
| 83 | #define NVRAM_BYTES (128-NVRAM_FIRST_BYTE) |
| 84 | |
| 85 | #define mach_check_checksum cobalt_check_checksum |
| 86 | #define mach_set_checksum cobalt_set_checksum |
| 87 | #define mach_proc_infos cobalt_proc_infos |
| 88 | |
| 89 | #endif |
| 90 | |
| 91 | #if MACH == ATARI |
| 92 | |
| 93 | /* Special parameters for RTC in Atari machines */ |
| 94 | #include <asm/atarihw.h> |
| 95 | #include <asm/atariints.h> |
| 96 | #define RTC_PORT(x) (TT_RTC_BAS + 2*(x)) |
| 97 | #define CHECK_DRIVER_INIT() (MACH_IS_ATARI && ATARIHW_PRESENT(TT_CLK)) |
| 98 | |
| 99 | #define NVRAM_BYTES 50 |
| 100 | |
| 101 | /* On Ataris, the checksum is over all bytes except the checksum bytes |
| 102 | * themselves; these are at the very end */ |
| 103 | #define ATARI_CKS_RANGE_START 0 |
| 104 | #define ATARI_CKS_RANGE_END 47 |
| 105 | #define ATARI_CKS_LOC 48 |
| 106 | |
| 107 | #define mach_check_checksum atari_check_checksum |
| 108 | #define mach_set_checksum atari_set_checksum |
| 109 | #define mach_proc_infos atari_proc_infos |
| 110 | |
| 111 | #endif |
| 112 | |
| 113 | /* Note that *all* calls to CMOS_READ and CMOS_WRITE must be done with |
| 114 | * rtc_lock held. Due to the index-port/data-port design of the RTC, we |
| 115 | * don't want two different things trying to get to it at once. (e.g. the |
| 116 | * periodic 11 min sync from time.c vs. this driver.) |
| 117 | */ |
| 118 | |
| 119 | #include <linux/types.h> |
| 120 | #include <linux/errno.h> |
| 121 | #include <linux/miscdevice.h> |
| 122 | #include <linux/slab.h> |
| 123 | #include <linux/ioport.h> |
| 124 | #include <linux/fcntl.h> |
| 125 | #include <linux/mc146818rtc.h> |
| 126 | #include <linux/init.h> |
| 127 | #include <linux/proc_fs.h> |
| 128 | #include <linux/spinlock.h> |
| 129 | |
| 130 | #include <asm/io.h> |
| 131 | #include <asm/uaccess.h> |
| 132 | #include <asm/system.h> |
| 133 | |
| 134 | static DEFINE_SPINLOCK(nvram_state_lock); |
| 135 | static int nvram_open_cnt; /* #times opened */ |
| 136 | static int nvram_open_mode; /* special open modes */ |
| 137 | #define NVRAM_WRITE 1 /* opened for writing (exclusive) */ |
| 138 | #define NVRAM_EXCL 2 /* opened with O_EXCL */ |
| 139 | |
| 140 | static int mach_check_checksum(void); |
| 141 | static void mach_set_checksum(void); |
| 142 | |
| 143 | #ifdef CONFIG_PROC_FS |
| 144 | static int mach_proc_infos(unsigned char *contents, char *buffer, int *len, |
| 145 | off_t *begin, off_t offset, int size); |
| 146 | #endif |
| 147 | |
| 148 | /* |
| 149 | * These functions are provided to be called internally or by other parts of |
| 150 | * the kernel. It's up to the caller to ensure correct checksum before reading |
| 151 | * or after writing (needs to be done only once). |
| 152 | * |
| 153 | * It is worth noting that these functions all access bytes of general |
| 154 | * purpose memory in the NVRAM - that is to say, they all add the |
| 155 | * NVRAM_FIRST_BYTE offset. Pass them offsets into NVRAM as if you did not |
| 156 | * know about the RTC cruft. |
| 157 | */ |
| 158 | |
| 159 | unsigned char |
| 160 | __nvram_read_byte(int i) |
| 161 | { |
| 162 | return CMOS_READ(NVRAM_FIRST_BYTE + i); |
| 163 | } |
| 164 | |
| 165 | unsigned char |
| 166 | nvram_read_byte(int i) |
| 167 | { |
| 168 | unsigned long flags; |
| 169 | unsigned char c; |
| 170 | |
| 171 | spin_lock_irqsave(&rtc_lock, flags); |
| 172 | c = __nvram_read_byte(i); |
| 173 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 174 | return c; |
| 175 | } |
| 176 | |
| 177 | /* This races nicely with trying to read with checksum checking (nvram_read) */ |
| 178 | void |
| 179 | __nvram_write_byte(unsigned char c, int i) |
| 180 | { |
| 181 | CMOS_WRITE(c, NVRAM_FIRST_BYTE + i); |
| 182 | } |
| 183 | |
| 184 | void |
| 185 | nvram_write_byte(unsigned char c, int i) |
| 186 | { |
| 187 | unsigned long flags; |
| 188 | |
| 189 | spin_lock_irqsave(&rtc_lock, flags); |
| 190 | __nvram_write_byte(c, i); |
| 191 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 192 | } |
| 193 | |
| 194 | int |
| 195 | __nvram_check_checksum(void) |
| 196 | { |
| 197 | return mach_check_checksum(); |
| 198 | } |
| 199 | |
| 200 | int |
| 201 | nvram_check_checksum(void) |
| 202 | { |
| 203 | unsigned long flags; |
| 204 | int rv; |
| 205 | |
| 206 | spin_lock_irqsave(&rtc_lock, flags); |
| 207 | rv = __nvram_check_checksum(); |
| 208 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 209 | return rv; |
| 210 | } |
| 211 | |
Adrian Bunk | 681ea4b | 2005-06-25 14:59:04 -0700 | [diff] [blame] | 212 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | __nvram_set_checksum(void) |
| 214 | { |
| 215 | mach_set_checksum(); |
| 216 | } |
| 217 | |
Adrian Bunk | 681ea4b | 2005-06-25 14:59:04 -0700 | [diff] [blame] | 218 | #if 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | void |
| 220 | nvram_set_checksum(void) |
| 221 | { |
| 222 | unsigned long flags; |
| 223 | |
| 224 | spin_lock_irqsave(&rtc_lock, flags); |
| 225 | __nvram_set_checksum(); |
| 226 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 227 | } |
Adrian Bunk | 681ea4b | 2005-06-25 14:59:04 -0700 | [diff] [blame] | 228 | #endif /* 0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | /* |
| 231 | * The are the file operation function for user access to /dev/nvram |
| 232 | */ |
| 233 | |
| 234 | static loff_t nvram_llseek(struct file *file,loff_t offset, int origin ) |
| 235 | { |
| 236 | lock_kernel(); |
| 237 | switch (origin) { |
| 238 | case 0: |
| 239 | /* nothing to do */ |
| 240 | break; |
| 241 | case 1: |
| 242 | offset += file->f_pos; |
| 243 | break; |
| 244 | case 2: |
| 245 | offset += NVRAM_BYTES; |
| 246 | break; |
| 247 | } |
| 248 | unlock_kernel(); |
| 249 | return (offset >= 0) ? (file->f_pos = offset) : -EINVAL; |
| 250 | } |
| 251 | |
| 252 | static ssize_t |
| 253 | nvram_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 254 | { |
| 255 | unsigned char contents[NVRAM_BYTES]; |
| 256 | unsigned i = *ppos; |
| 257 | unsigned char *tmp; |
| 258 | |
| 259 | spin_lock_irq(&rtc_lock); |
| 260 | |
| 261 | if (!__nvram_check_checksum()) |
| 262 | goto checksum_err; |
| 263 | |
| 264 | for (tmp = contents; count-- > 0 && i < NVRAM_BYTES; ++i, ++tmp) |
| 265 | *tmp = __nvram_read_byte(i); |
| 266 | |
| 267 | spin_unlock_irq(&rtc_lock); |
| 268 | |
| 269 | if (copy_to_user(buf, contents, tmp - contents)) |
| 270 | return -EFAULT; |
| 271 | |
| 272 | *ppos = i; |
| 273 | |
| 274 | return tmp - contents; |
| 275 | |
| 276 | checksum_err: |
| 277 | spin_unlock_irq(&rtc_lock); |
| 278 | return -EIO; |
| 279 | } |
| 280 | |
| 281 | static ssize_t |
| 282 | nvram_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) |
| 283 | { |
| 284 | unsigned char contents[NVRAM_BYTES]; |
| 285 | unsigned i = *ppos; |
| 286 | unsigned char *tmp; |
| 287 | int len; |
| 288 | |
| 289 | len = (NVRAM_BYTES - i) < count ? (NVRAM_BYTES - i) : count; |
| 290 | if (copy_from_user(contents, buf, len)) |
| 291 | return -EFAULT; |
| 292 | |
| 293 | spin_lock_irq(&rtc_lock); |
| 294 | |
| 295 | if (!__nvram_check_checksum()) |
| 296 | goto checksum_err; |
| 297 | |
| 298 | for (tmp = contents; count-- > 0 && i < NVRAM_BYTES; ++i, ++tmp) |
| 299 | __nvram_write_byte(*tmp, i); |
| 300 | |
| 301 | __nvram_set_checksum(); |
| 302 | |
| 303 | spin_unlock_irq(&rtc_lock); |
| 304 | |
| 305 | *ppos = i; |
| 306 | |
| 307 | return tmp - contents; |
| 308 | |
| 309 | checksum_err: |
| 310 | spin_unlock_irq(&rtc_lock); |
| 311 | return -EIO; |
| 312 | } |
| 313 | |
| 314 | static int |
| 315 | nvram_ioctl(struct inode *inode, struct file *file, |
| 316 | unsigned int cmd, unsigned long arg) |
| 317 | { |
| 318 | int i; |
| 319 | |
| 320 | switch (cmd) { |
| 321 | |
| 322 | case NVRAM_INIT: |
| 323 | /* initialize NVRAM contents and checksum */ |
| 324 | if (!capable(CAP_SYS_ADMIN)) |
| 325 | return -EACCES; |
| 326 | |
| 327 | spin_lock_irq(&rtc_lock); |
| 328 | |
| 329 | for (i = 0; i < NVRAM_BYTES; ++i) |
| 330 | __nvram_write_byte(0, i); |
| 331 | __nvram_set_checksum(); |
| 332 | |
| 333 | spin_unlock_irq(&rtc_lock); |
| 334 | return 0; |
| 335 | |
| 336 | case NVRAM_SETCKS: |
| 337 | /* just set checksum, contents unchanged (maybe useful after |
| 338 | * checksum garbaged somehow...) */ |
| 339 | if (!capable(CAP_SYS_ADMIN)) |
| 340 | return -EACCES; |
| 341 | |
| 342 | spin_lock_irq(&rtc_lock); |
| 343 | __nvram_set_checksum(); |
| 344 | spin_unlock_irq(&rtc_lock); |
| 345 | return 0; |
| 346 | |
| 347 | default: |
| 348 | return -ENOTTY; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | static int |
| 353 | nvram_open(struct inode *inode, struct file *file) |
| 354 | { |
| 355 | spin_lock(&nvram_state_lock); |
| 356 | |
| 357 | if ((nvram_open_cnt && (file->f_flags & O_EXCL)) || |
| 358 | (nvram_open_mode & NVRAM_EXCL) || |
| 359 | ((file->f_mode & 2) && (nvram_open_mode & NVRAM_WRITE))) { |
| 360 | spin_unlock(&nvram_state_lock); |
| 361 | return -EBUSY; |
| 362 | } |
| 363 | |
| 364 | if (file->f_flags & O_EXCL) |
| 365 | nvram_open_mode |= NVRAM_EXCL; |
| 366 | if (file->f_mode & 2) |
| 367 | nvram_open_mode |= NVRAM_WRITE; |
| 368 | nvram_open_cnt++; |
| 369 | |
| 370 | spin_unlock(&nvram_state_lock); |
| 371 | |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | static int |
| 376 | nvram_release(struct inode *inode, struct file *file) |
| 377 | { |
| 378 | spin_lock(&nvram_state_lock); |
| 379 | |
| 380 | nvram_open_cnt--; |
| 381 | |
| 382 | /* if only one instance is open, clear the EXCL bit */ |
| 383 | if (nvram_open_mode & NVRAM_EXCL) |
| 384 | nvram_open_mode &= ~NVRAM_EXCL; |
| 385 | if (file->f_mode & 2) |
| 386 | nvram_open_mode &= ~NVRAM_WRITE; |
| 387 | |
| 388 | spin_unlock(&nvram_state_lock); |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | #ifndef CONFIG_PROC_FS |
| 394 | static int |
| 395 | nvram_read_proc(char *buffer, char **start, off_t offset, |
| 396 | int size, int *eof, void *data) |
| 397 | { |
| 398 | return 0; |
| 399 | } |
| 400 | #else |
| 401 | |
| 402 | static int |
| 403 | nvram_read_proc(char *buffer, char **start, off_t offset, |
| 404 | int size, int *eof, void *data) |
| 405 | { |
| 406 | unsigned char contents[NVRAM_BYTES]; |
| 407 | int i, len = 0; |
| 408 | off_t begin = 0; |
| 409 | |
| 410 | spin_lock_irq(&rtc_lock); |
| 411 | for (i = 0; i < NVRAM_BYTES; ++i) |
| 412 | contents[i] = __nvram_read_byte(i); |
| 413 | spin_unlock_irq(&rtc_lock); |
| 414 | |
| 415 | *eof = mach_proc_infos(contents, buffer, &len, &begin, offset, size); |
| 416 | |
| 417 | if (offset >= begin + len) |
| 418 | return 0; |
| 419 | *start = buffer + (offset - begin); |
| 420 | return (size < begin + len - offset) ? size : begin + len - offset; |
| 421 | |
| 422 | } |
| 423 | |
| 424 | /* This macro frees the machine specific function from bounds checking and |
| 425 | * this like that... */ |
| 426 | #define PRINT_PROC(fmt,args...) \ |
| 427 | do { \ |
| 428 | *len += sprintf(buffer+*len, fmt, ##args); \ |
| 429 | if (*begin + *len > offset + size) \ |
| 430 | return 0; \ |
| 431 | if (*begin + *len < offset) { \ |
| 432 | *begin += *len; \ |
| 433 | *len = 0; \ |
| 434 | } \ |
| 435 | } while(0) |
| 436 | |
| 437 | #endif /* CONFIG_PROC_FS */ |
| 438 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 439 | static const struct file_operations nvram_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | .owner = THIS_MODULE, |
| 441 | .llseek = nvram_llseek, |
| 442 | .read = nvram_read, |
| 443 | .write = nvram_write, |
| 444 | .ioctl = nvram_ioctl, |
| 445 | .open = nvram_open, |
| 446 | .release = nvram_release, |
| 447 | }; |
| 448 | |
| 449 | static struct miscdevice nvram_dev = { |
| 450 | NVRAM_MINOR, |
| 451 | "nvram", |
| 452 | &nvram_fops |
| 453 | }; |
| 454 | |
| 455 | static int __init |
| 456 | nvram_init(void) |
| 457 | { |
| 458 | int ret; |
| 459 | |
| 460 | /* First test whether the driver should init at all */ |
| 461 | if (!CHECK_DRIVER_INIT()) |
| 462 | return -ENXIO; |
| 463 | |
| 464 | ret = misc_register(&nvram_dev); |
| 465 | if (ret) { |
| 466 | printk(KERN_ERR "nvram: can't misc_register on minor=%d\n", |
| 467 | NVRAM_MINOR); |
| 468 | goto out; |
| 469 | } |
| 470 | if (!create_proc_read_entry("driver/nvram", 0, NULL, nvram_read_proc, |
| 471 | NULL)) { |
| 472 | printk(KERN_ERR "nvram: can't create /proc/driver/nvram\n"); |
| 473 | ret = -ENOMEM; |
| 474 | goto outmisc; |
| 475 | } |
| 476 | ret = 0; |
| 477 | printk(KERN_INFO "Non-volatile memory driver v" NVRAM_VERSION "\n"); |
| 478 | out: |
| 479 | return ret; |
| 480 | outmisc: |
| 481 | misc_deregister(&nvram_dev); |
| 482 | goto out; |
| 483 | } |
| 484 | |
| 485 | static void __exit |
| 486 | nvram_cleanup_module(void) |
| 487 | { |
| 488 | remove_proc_entry("driver/nvram", NULL); |
| 489 | misc_deregister(&nvram_dev); |
| 490 | } |
| 491 | |
| 492 | module_init(nvram_init); |
| 493 | module_exit(nvram_cleanup_module); |
| 494 | |
| 495 | /* |
| 496 | * Machine specific functions |
| 497 | */ |
| 498 | |
| 499 | #if MACH == PC |
| 500 | |
| 501 | static int |
| 502 | pc_check_checksum(void) |
| 503 | { |
| 504 | int i; |
| 505 | unsigned short sum = 0; |
| 506 | unsigned short expect; |
| 507 | |
| 508 | for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i) |
| 509 | sum += __nvram_read_byte(i); |
| 510 | expect = __nvram_read_byte(PC_CKS_LOC)<<8 | |
| 511 | __nvram_read_byte(PC_CKS_LOC+1); |
| 512 | return ((sum & 0xffff) == expect); |
| 513 | } |
| 514 | |
| 515 | static void |
| 516 | pc_set_checksum(void) |
| 517 | { |
| 518 | int i; |
| 519 | unsigned short sum = 0; |
| 520 | |
| 521 | for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i) |
| 522 | sum += __nvram_read_byte(i); |
| 523 | __nvram_write_byte(sum >> 8, PC_CKS_LOC); |
| 524 | __nvram_write_byte(sum & 0xff, PC_CKS_LOC + 1); |
| 525 | } |
| 526 | |
| 527 | #ifdef CONFIG_PROC_FS |
| 528 | |
| 529 | static char *floppy_types[] = { |
| 530 | "none", "5.25'' 360k", "5.25'' 1.2M", "3.5'' 720k", "3.5'' 1.44M", |
| 531 | "3.5'' 2.88M", "3.5'' 2.88M" |
| 532 | }; |
| 533 | |
| 534 | static char *gfx_types[] = { |
| 535 | "EGA, VGA, ... (with BIOS)", |
| 536 | "CGA (40 cols)", |
| 537 | "CGA (80 cols)", |
| 538 | "monochrome", |
| 539 | }; |
| 540 | |
| 541 | static int |
| 542 | pc_proc_infos(unsigned char *nvram, char *buffer, int *len, |
| 543 | off_t *begin, off_t offset, int size) |
| 544 | { |
| 545 | int checksum; |
| 546 | int type; |
| 547 | |
| 548 | spin_lock_irq(&rtc_lock); |
| 549 | checksum = __nvram_check_checksum(); |
| 550 | spin_unlock_irq(&rtc_lock); |
| 551 | |
| 552 | PRINT_PROC("Checksum status: %svalid\n", checksum ? "" : "not "); |
| 553 | |
| 554 | PRINT_PROC("# floppies : %d\n", |
| 555 | (nvram[6] & 1) ? (nvram[6] >> 6) + 1 : 0); |
| 556 | PRINT_PROC("Floppy 0 type : "); |
| 557 | type = nvram[2] >> 4; |
Tobias Klauser | fe97107 | 2006-01-09 20:54:02 -0800 | [diff] [blame] | 558 | if (type < ARRAY_SIZE(floppy_types)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | PRINT_PROC("%s\n", floppy_types[type]); |
| 560 | else |
| 561 | PRINT_PROC("%d (unknown)\n", type); |
| 562 | PRINT_PROC("Floppy 1 type : "); |
| 563 | type = nvram[2] & 0x0f; |
Tobias Klauser | fe97107 | 2006-01-09 20:54:02 -0800 | [diff] [blame] | 564 | if (type < ARRAY_SIZE(floppy_types)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | PRINT_PROC("%s\n", floppy_types[type]); |
| 566 | else |
| 567 | PRINT_PROC("%d (unknown)\n", type); |
| 568 | |
| 569 | PRINT_PROC("HD 0 type : "); |
| 570 | type = nvram[4] >> 4; |
| 571 | if (type) |
| 572 | PRINT_PROC("%02x\n", type == 0x0f ? nvram[11] : type); |
| 573 | else |
| 574 | PRINT_PROC("none\n"); |
| 575 | |
| 576 | PRINT_PROC("HD 1 type : "); |
| 577 | type = nvram[4] & 0x0f; |
| 578 | if (type) |
| 579 | PRINT_PROC("%02x\n", type == 0x0f ? nvram[12] : type); |
| 580 | else |
| 581 | PRINT_PROC("none\n"); |
| 582 | |
| 583 | PRINT_PROC("HD type 48 data: %d/%d/%d C/H/S, precomp %d, lz %d\n", |
| 584 | nvram[18] | (nvram[19] << 8), |
| 585 | nvram[20], nvram[25], |
| 586 | nvram[21] | (nvram[22] << 8), nvram[23] | (nvram[24] << 8)); |
| 587 | PRINT_PROC("HD type 49 data: %d/%d/%d C/H/S, precomp %d, lz %d\n", |
| 588 | nvram[39] | (nvram[40] << 8), |
| 589 | nvram[41], nvram[46], |
| 590 | nvram[42] | (nvram[43] << 8), nvram[44] | (nvram[45] << 8)); |
| 591 | |
| 592 | PRINT_PROC("DOS base memory: %d kB\n", nvram[7] | (nvram[8] << 8)); |
| 593 | PRINT_PROC("Extended memory: %d kB (configured), %d kB (tested)\n", |
| 594 | nvram[9] | (nvram[10] << 8), nvram[34] | (nvram[35] << 8)); |
| 595 | |
| 596 | PRINT_PROC("Gfx adapter : %s\n", gfx_types[(nvram[6] >> 4) & 3]); |
| 597 | |
| 598 | PRINT_PROC("FPU : %sinstalled\n", |
| 599 | (nvram[6] & 2) ? "" : "not "); |
| 600 | |
| 601 | return 1; |
| 602 | } |
| 603 | #endif |
| 604 | |
| 605 | #endif /* MACH == PC */ |
| 606 | |
| 607 | #if MACH == COBALT |
| 608 | |
| 609 | /* the cobalt CMOS has a wider range of its checksum */ |
| 610 | static int cobalt_check_checksum(void) |
| 611 | { |
| 612 | int i; |
| 613 | unsigned short sum = 0; |
| 614 | unsigned short expect; |
| 615 | |
| 616 | for (i = COBT_CMOS_CKS_START; i <= COBT_CMOS_CKS_END; ++i) { |
| 617 | if ((i == COBT_CMOS_CHECKSUM) || (i == (COBT_CMOS_CHECKSUM+1))) |
| 618 | continue; |
| 619 | |
| 620 | sum += __nvram_read_byte(i); |
| 621 | } |
| 622 | expect = __nvram_read_byte(COBT_CMOS_CHECKSUM) << 8 | |
| 623 | __nvram_read_byte(COBT_CMOS_CHECKSUM+1); |
| 624 | return ((sum & 0xffff) == expect); |
| 625 | } |
| 626 | |
| 627 | static void cobalt_set_checksum(void) |
| 628 | { |
| 629 | int i; |
| 630 | unsigned short sum = 0; |
| 631 | |
| 632 | for (i = COBT_CMOS_CKS_START; i <= COBT_CMOS_CKS_END; ++i) { |
| 633 | if ((i == COBT_CMOS_CHECKSUM) || (i == (COBT_CMOS_CHECKSUM+1))) |
| 634 | continue; |
| 635 | |
| 636 | sum += __nvram_read_byte(i); |
| 637 | } |
| 638 | |
| 639 | __nvram_write_byte(sum >> 8, COBT_CMOS_CHECKSUM); |
| 640 | __nvram_write_byte(sum & 0xff, COBT_CMOS_CHECKSUM+1); |
| 641 | } |
| 642 | |
| 643 | #ifdef CONFIG_PROC_FS |
| 644 | |
| 645 | static int cobalt_proc_infos(unsigned char *nvram, char *buffer, int *len, |
| 646 | off_t *begin, off_t offset, int size) |
| 647 | { |
| 648 | int i; |
| 649 | unsigned int checksum; |
| 650 | unsigned int flags; |
| 651 | char sernum[14]; |
| 652 | char *key = "cNoEbTaWlOtR!"; |
| 653 | unsigned char bto_csum; |
| 654 | |
| 655 | spin_lock_irq(&rtc_lock); |
| 656 | checksum = __nvram_check_checksum(); |
| 657 | spin_unlock_irq(&rtc_lock); |
| 658 | |
| 659 | PRINT_PROC("Checksum status: %svalid\n", checksum ? "" : "not "); |
| 660 | |
| 661 | flags = nvram[COBT_CMOS_FLAG_BYTE_0] << 8 |
| 662 | | nvram[COBT_CMOS_FLAG_BYTE_1]; |
| 663 | |
| 664 | PRINT_PROC("Console: %s\n", |
| 665 | flags & COBT_CMOS_CONSOLE_FLAG ? "on": "off"); |
| 666 | |
| 667 | PRINT_PROC("Firmware Debug Messages: %s\n", |
| 668 | flags & COBT_CMOS_DEBUG_FLAG ? "on": "off"); |
| 669 | |
| 670 | PRINT_PROC("Auto Prompt: %s\n", |
| 671 | flags & COBT_CMOS_AUTO_PROMPT_FLAG ? "on": "off"); |
| 672 | |
| 673 | PRINT_PROC("Shutdown Status: %s\n", |
| 674 | flags & COBT_CMOS_CLEAN_BOOT_FLAG ? "clean": "dirty"); |
| 675 | |
| 676 | PRINT_PROC("Hardware Probe: %s\n", |
| 677 | flags & COBT_CMOS_HW_NOPROBE_FLAG ? "partial": "full"); |
| 678 | |
| 679 | PRINT_PROC("System Fault: %sdetected\n", |
| 680 | flags & COBT_CMOS_SYSFAULT_FLAG ? "": "not "); |
| 681 | |
| 682 | PRINT_PROC("Panic on OOPS: %s\n", |
| 683 | flags & COBT_CMOS_OOPSPANIC_FLAG ? "yes": "no"); |
| 684 | |
| 685 | PRINT_PROC("Delayed Cache Initialization: %s\n", |
| 686 | flags & COBT_CMOS_DELAY_CACHE_FLAG ? "yes": "no"); |
| 687 | |
| 688 | PRINT_PROC("Show Logo at Boot: %s\n", |
| 689 | flags & COBT_CMOS_NOLOGO_FLAG ? "no": "yes"); |
| 690 | |
| 691 | PRINT_PROC("Boot Method: "); |
| 692 | switch (nvram[COBT_CMOS_BOOT_METHOD]) { |
| 693 | case COBT_CMOS_BOOT_METHOD_DISK: |
| 694 | PRINT_PROC("disk\n"); |
| 695 | break; |
| 696 | |
| 697 | case COBT_CMOS_BOOT_METHOD_ROM: |
| 698 | PRINT_PROC("rom\n"); |
| 699 | break; |
| 700 | |
| 701 | case COBT_CMOS_BOOT_METHOD_NET: |
| 702 | PRINT_PROC("net\n"); |
| 703 | break; |
| 704 | |
| 705 | default: |
| 706 | PRINT_PROC("unknown\n"); |
| 707 | break; |
| 708 | } |
| 709 | |
| 710 | PRINT_PROC("Primary Boot Device: %d:%d\n", |
| 711 | nvram[COBT_CMOS_BOOT_DEV0_MAJ], |
| 712 | nvram[COBT_CMOS_BOOT_DEV0_MIN] ); |
| 713 | PRINT_PROC("Secondary Boot Device: %d:%d\n", |
| 714 | nvram[COBT_CMOS_BOOT_DEV1_MAJ], |
| 715 | nvram[COBT_CMOS_BOOT_DEV1_MIN] ); |
| 716 | PRINT_PROC("Tertiary Boot Device: %d:%d\n", |
| 717 | nvram[COBT_CMOS_BOOT_DEV2_MAJ], |
| 718 | nvram[COBT_CMOS_BOOT_DEV2_MIN] ); |
| 719 | |
| 720 | PRINT_PROC("Uptime: %d\n", |
| 721 | nvram[COBT_CMOS_UPTIME_0] << 24 | |
| 722 | nvram[COBT_CMOS_UPTIME_1] << 16 | |
| 723 | nvram[COBT_CMOS_UPTIME_2] << 8 | |
| 724 | nvram[COBT_CMOS_UPTIME_3]); |
| 725 | |
| 726 | PRINT_PROC("Boot Count: %d\n", |
| 727 | nvram[COBT_CMOS_BOOTCOUNT_0] << 24 | |
| 728 | nvram[COBT_CMOS_BOOTCOUNT_1] << 16 | |
| 729 | nvram[COBT_CMOS_BOOTCOUNT_2] << 8 | |
| 730 | nvram[COBT_CMOS_BOOTCOUNT_3]); |
| 731 | |
| 732 | /* 13 bytes of serial num */ |
| 733 | for (i=0 ; i<13 ; i++) { |
| 734 | sernum[i] = nvram[COBT_CMOS_SYS_SERNUM_0 + i]; |
| 735 | } |
| 736 | sernum[13] = '\0'; |
| 737 | |
| 738 | checksum = 0; |
| 739 | for (i=0 ; i<13 ; i++) { |
| 740 | checksum += sernum[i] ^ key[i]; |
| 741 | } |
| 742 | checksum = ((checksum & 0x7f) ^ (0xd6)) & 0xff; |
| 743 | |
| 744 | PRINT_PROC("Serial Number: %s", sernum); |
| 745 | if (checksum != nvram[COBT_CMOS_SYS_SERNUM_CSUM]) { |
| 746 | PRINT_PROC(" (invalid checksum)"); |
| 747 | } |
| 748 | PRINT_PROC("\n"); |
| 749 | |
| 750 | PRINT_PROC("Rom Revison: %d.%d.%d\n", nvram[COBT_CMOS_ROM_REV_MAJ], |
| 751 | nvram[COBT_CMOS_ROM_REV_MIN], nvram[COBT_CMOS_ROM_REV_REV]); |
| 752 | |
| 753 | PRINT_PROC("BTO Server: %d.%d.%d.%d", nvram[COBT_CMOS_BTO_IP_0], |
| 754 | nvram[COBT_CMOS_BTO_IP_1], nvram[COBT_CMOS_BTO_IP_2], |
| 755 | nvram[COBT_CMOS_BTO_IP_3]); |
| 756 | bto_csum = nvram[COBT_CMOS_BTO_IP_0] + nvram[COBT_CMOS_BTO_IP_1] |
| 757 | + nvram[COBT_CMOS_BTO_IP_2] + nvram[COBT_CMOS_BTO_IP_3]; |
| 758 | if (bto_csum != nvram[COBT_CMOS_BTO_IP_CSUM]) { |
| 759 | PRINT_PROC(" (invalid checksum)"); |
| 760 | } |
| 761 | PRINT_PROC("\n"); |
| 762 | |
| 763 | if (flags & COBT_CMOS_VERSION_FLAG |
| 764 | && nvram[COBT_CMOS_VERSION] >= COBT_CMOS_VER_BTOCODE) { |
| 765 | PRINT_PROC("BTO Code: 0x%x\n", |
| 766 | nvram[COBT_CMOS_BTO_CODE_0] << 24 | |
| 767 | nvram[COBT_CMOS_BTO_CODE_1] << 16 | |
| 768 | nvram[COBT_CMOS_BTO_CODE_2] << 8 | |
| 769 | nvram[COBT_CMOS_BTO_CODE_3]); |
| 770 | } |
| 771 | |
| 772 | return 1; |
| 773 | } |
| 774 | #endif /* CONFIG_PROC_FS */ |
| 775 | |
| 776 | #endif /* MACH == COBALT */ |
| 777 | |
| 778 | #if MACH == ATARI |
| 779 | |
| 780 | static int |
| 781 | atari_check_checksum(void) |
| 782 | { |
| 783 | int i; |
| 784 | unsigned char sum = 0; |
| 785 | |
| 786 | for (i = ATARI_CKS_RANGE_START; i <= ATARI_CKS_RANGE_END; ++i) |
| 787 | sum += __nvram_read_byte(i); |
| 788 | return (__nvram_read_byte(ATARI_CKS_LOC) == (~sum & 0xff) && |
| 789 | __nvram_read_byte(ATARI_CKS_LOC + 1) == (sum & 0xff)); |
| 790 | } |
| 791 | |
| 792 | static void |
| 793 | atari_set_checksum(void) |
| 794 | { |
| 795 | int i; |
| 796 | unsigned char sum = 0; |
| 797 | |
| 798 | for (i = ATARI_CKS_RANGE_START; i <= ATARI_CKS_RANGE_END; ++i) |
| 799 | sum += __nvram_read_byte(i); |
| 800 | __nvram_write_byte(~sum, ATARI_CKS_LOC); |
| 801 | __nvram_write_byte(sum, ATARI_CKS_LOC + 1); |
| 802 | } |
| 803 | |
| 804 | #ifdef CONFIG_PROC_FS |
| 805 | |
| 806 | static struct { |
| 807 | unsigned char val; |
| 808 | char *name; |
| 809 | } boot_prefs[] = { |
| 810 | { 0x80, "TOS" }, |
| 811 | { 0x40, "ASV" }, |
| 812 | { 0x20, "NetBSD (?)" }, |
| 813 | { 0x10, "Linux" }, |
| 814 | { 0x00, "unspecified" } |
| 815 | }; |
| 816 | |
| 817 | static char *languages[] = { |
| 818 | "English (US)", |
| 819 | "German", |
| 820 | "French", |
| 821 | "English (UK)", |
| 822 | "Spanish", |
| 823 | "Italian", |
| 824 | "6 (undefined)", |
| 825 | "Swiss (French)", |
| 826 | "Swiss (German)" |
| 827 | }; |
| 828 | |
| 829 | static char *dateformat[] = { |
| 830 | "MM%cDD%cYY", |
| 831 | "DD%cMM%cYY", |
| 832 | "YY%cMM%cDD", |
| 833 | "YY%cDD%cMM", |
| 834 | "4 (undefined)", |
| 835 | "5 (undefined)", |
| 836 | "6 (undefined)", |
| 837 | "7 (undefined)" |
| 838 | }; |
| 839 | |
| 840 | static char *colors[] = { |
| 841 | "2", "4", "16", "256", "65536", "??", "??", "??" |
| 842 | }; |
| 843 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | static int |
| 845 | atari_proc_infos(unsigned char *nvram, char *buffer, int *len, |
| 846 | off_t *begin, off_t offset, int size) |
| 847 | { |
| 848 | int checksum = nvram_check_checksum(); |
| 849 | int i; |
| 850 | unsigned vmode; |
| 851 | |
| 852 | PRINT_PROC("Checksum status : %svalid\n", checksum ? "" : "not "); |
| 853 | |
| 854 | PRINT_PROC("Boot preference : "); |
Tobias Klauser | fe97107 | 2006-01-09 20:54:02 -0800 | [diff] [blame] | 855 | for (i = ARRAY_SIZE(boot_prefs) - 1; i >= 0; --i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | if (nvram[1] == boot_prefs[i].val) { |
| 857 | PRINT_PROC("%s\n", boot_prefs[i].name); |
| 858 | break; |
| 859 | } |
| 860 | } |
| 861 | if (i < 0) |
| 862 | PRINT_PROC("0x%02x (undefined)\n", nvram[1]); |
| 863 | |
| 864 | PRINT_PROC("SCSI arbitration : %s\n", |
| 865 | (nvram[16] & 0x80) ? "on" : "off"); |
| 866 | PRINT_PROC("SCSI host ID : "); |
| 867 | if (nvram[16] & 0x80) |
| 868 | PRINT_PROC("%d\n", nvram[16] & 7); |
| 869 | else |
| 870 | PRINT_PROC("n/a\n"); |
| 871 | |
| 872 | /* the following entries are defined only for the Falcon */ |
| 873 | if ((atari_mch_cookie >> 16) != ATARI_MCH_FALCON) |
| 874 | return 1; |
| 875 | |
| 876 | PRINT_PROC("OS language : "); |
Tobias Klauser | fe97107 | 2006-01-09 20:54:02 -0800 | [diff] [blame] | 877 | if (nvram[6] < ARRAY_SIZE(languages)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | PRINT_PROC("%s\n", languages[nvram[6]]); |
| 879 | else |
| 880 | PRINT_PROC("%u (undefined)\n", nvram[6]); |
| 881 | PRINT_PROC("Keyboard language: "); |
Tobias Klauser | fe97107 | 2006-01-09 20:54:02 -0800 | [diff] [blame] | 882 | if (nvram[7] < ARRAY_SIZE(languages)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | PRINT_PROC("%s\n", languages[nvram[7]]); |
| 884 | else |
| 885 | PRINT_PROC("%u (undefined)\n", nvram[7]); |
| 886 | PRINT_PROC("Date format : "); |
| 887 | PRINT_PROC(dateformat[nvram[8] & 7], |
| 888 | nvram[9] ? nvram[9] : '/', nvram[9] ? nvram[9] : '/'); |
| 889 | PRINT_PROC(", %dh clock\n", nvram[8] & 16 ? 24 : 12); |
| 890 | PRINT_PROC("Boot delay : "); |
| 891 | if (nvram[10] == 0) |
| 892 | PRINT_PROC("default"); |
| 893 | else |
| 894 | PRINT_PROC("%ds%s\n", nvram[10], |
| 895 | nvram[10] < 8 ? ", no memory test" : ""); |
| 896 | |
| 897 | vmode = (nvram[14] << 8) || nvram[15]; |
| 898 | PRINT_PROC("Video mode : %s colors, %d columns, %s %s monitor\n", |
| 899 | colors[vmode & 7], |
| 900 | vmode & 8 ? 80 : 40, |
| 901 | vmode & 16 ? "VGA" : "TV", vmode & 32 ? "PAL" : "NTSC"); |
| 902 | PRINT_PROC(" %soverscan, compat. mode %s%s\n", |
| 903 | vmode & 64 ? "" : "no ", |
| 904 | vmode & 128 ? "on" : "off", |
| 905 | vmode & 256 ? |
| 906 | (vmode & 16 ? ", line doubling" : ", half screen") : ""); |
| 907 | |
| 908 | return 1; |
| 909 | } |
| 910 | #endif |
| 911 | |
| 912 | #endif /* MACH == ATARI */ |
| 913 | |
| 914 | MODULE_LICENSE("GPL"); |
| 915 | |
| 916 | EXPORT_SYMBOL(__nvram_read_byte); |
| 917 | EXPORT_SYMBOL(nvram_read_byte); |
| 918 | EXPORT_SYMBOL(__nvram_write_byte); |
| 919 | EXPORT_SYMBOL(nvram_write_byte); |
| 920 | EXPORT_SYMBOL(__nvram_check_checksum); |
| 921 | EXPORT_SYMBOL(nvram_check_checksum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | MODULE_ALIAS_MISCDEV(NVRAM_MINOR); |