Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Implement 'Simple Boot Flag Specification 2.0' |
| 3 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | #include <linux/types.h> |
| 5 | #include <linux/kernel.h> |
| 6 | #include <linux/init.h> |
| 7 | #include <linux/string.h> |
| 8 | #include <linux/slab.h> |
| 9 | #include <linux/spinlock.h> |
| 10 | #include <linux/acpi.h> |
| 11 | #include <asm/io.h> |
| 12 | |
| 13 | #include <linux/mc146818rtc.h> |
| 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #define SBF_RESERVED (0x78) |
| 16 | #define SBF_PNPOS (1<<0) |
| 17 | #define SBF_BOOTING (1<<1) |
| 18 | #define SBF_DIAG (1<<2) |
| 19 | #define SBF_PARITY (1<<7) |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | int sbf_port __initdata = -1; /* set via acpi_boot_init() */ |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | static int __init parity(u8 v) |
| 24 | { |
| 25 | int x = 0; |
| 26 | int i; |
Cyrill Gorcunov | 8eed926 | 2008-01-30 13:32:31 +0100 | [diff] [blame] | 27 | |
| 28 | for (i = 0; i < 8; i++) { |
| 29 | x ^= (v & 1); |
| 30 | v >>= 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | } |
Cyrill Gorcunov | 8eed926 | 2008-01-30 13:32:31 +0100 | [diff] [blame] | 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | return x; |
| 34 | } |
| 35 | |
| 36 | static void __init sbf_write(u8 v) |
| 37 | { |
| 38 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Cyrill Gorcunov | 8eed926 | 2008-01-30 13:32:31 +0100 | [diff] [blame] | 40 | if (sbf_port != -1) { |
| 41 | v &= ~SBF_PARITY; |
| 42 | if (!parity(v)) |
| 43 | v |= SBF_PARITY; |
| 44 | |
| 45 | printk(KERN_INFO "Simple Boot Flag at 0x%x set to 0x%x\n", |
| 46 | sbf_port, v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | spin_lock_irqsave(&rtc_lock, flags); |
| 49 | CMOS_WRITE(v, sbf_port); |
| 50 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | static u8 __init sbf_read(void) |
| 55 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | unsigned long flags; |
Cyrill Gorcunov | 8eed926 | 2008-01-30 13:32:31 +0100 | [diff] [blame] | 57 | u8 v; |
| 58 | |
| 59 | if (sbf_port == -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | return 0; |
Cyrill Gorcunov | 8eed926 | 2008-01-30 13:32:31 +0100 | [diff] [blame] | 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | spin_lock_irqsave(&rtc_lock, flags); |
| 63 | v = CMOS_READ(sbf_port); |
| 64 | spin_unlock_irqrestore(&rtc_lock, flags); |
Cyrill Gorcunov | 8eed926 | 2008-01-30 13:32:31 +0100 | [diff] [blame] | 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | return v; |
| 67 | } |
| 68 | |
| 69 | static int __init sbf_value_valid(u8 v) |
| 70 | { |
Cyrill Gorcunov | 8eed926 | 2008-01-30 13:32:31 +0100 | [diff] [blame] | 71 | if (v & SBF_RESERVED) /* Reserved bits */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | return 0; |
Cyrill Gorcunov | 8eed926 | 2008-01-30 13:32:31 +0100 | [diff] [blame] | 73 | if (!parity(v)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | return 0; |
Cyrill Gorcunov | 8eed926 | 2008-01-30 13:32:31 +0100 | [diff] [blame] | 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | return 1; |
| 77 | } |
| 78 | |
| 79 | static int __init sbf_init(void) |
| 80 | { |
| 81 | u8 v; |
Cyrill Gorcunov | 8eed926 | 2008-01-30 13:32:31 +0100 | [diff] [blame] | 82 | |
| 83 | if (sbf_port == -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | return 0; |
Cyrill Gorcunov | 8eed926 | 2008-01-30 13:32:31 +0100 | [diff] [blame] | 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | v = sbf_read(); |
Cyrill Gorcunov | 8eed926 | 2008-01-30 13:32:31 +0100 | [diff] [blame] | 87 | if (!sbf_value_valid(v)) { |
| 88 | printk(KERN_WARNING "Simple Boot Flag value 0x%x read from " |
| 89 | "CMOS RAM was invalid\n", v); |
| 90 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | v &= ~SBF_RESERVED; |
| 93 | v &= ~SBF_BOOTING; |
| 94 | v &= ~SBF_DIAG; |
| 95 | #if defined(CONFIG_ISAPNP) |
| 96 | v |= SBF_PNPOS; |
| 97 | #endif |
| 98 | sbf_write(v); |
Cyrill Gorcunov | 8eed926 | 2008-01-30 13:32:31 +0100 | [diff] [blame] | 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | return 0; |
| 101 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | module_init(sbf_init); |