blob: 30f25a75fe2876ef93605181f81c386c02953bfb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Implement 'Simple Boot Flag Specification 2.0'
3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#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 Torvalds1da177e2005-04-16 15:20:36 -070015#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 Torvalds1da177e2005-04-16 15:20:36 -070021int sbf_port __initdata = -1; /* set via acpi_boot_init() */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023static int __init parity(u8 v)
24{
25 int x = 0;
26 int i;
Cyrill Gorcunov8eed9262008-01-30 13:32:31 +010027
28 for (i = 0; i < 8; i++) {
29 x ^= (v & 1);
30 v >>= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 }
Cyrill Gorcunov8eed9262008-01-30 13:32:31 +010032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 return x;
34}
35
36static void __init sbf_write(u8 v)
37{
38 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Cyrill Gorcunov8eed9262008-01-30 13:32:31 +010040 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 Torvalds1da177e2005-04-16 15:20:36 -070047
48 spin_lock_irqsave(&rtc_lock, flags);
49 CMOS_WRITE(v, sbf_port);
50 spin_unlock_irqrestore(&rtc_lock, flags);
51 }
52}
53
54static u8 __init sbf_read(void)
55{
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 unsigned long flags;
Cyrill Gorcunov8eed9262008-01-30 13:32:31 +010057 u8 v;
58
59 if (sbf_port == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return 0;
Cyrill Gorcunov8eed9262008-01-30 13:32:31 +010061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 spin_lock_irqsave(&rtc_lock, flags);
63 v = CMOS_READ(sbf_port);
64 spin_unlock_irqrestore(&rtc_lock, flags);
Cyrill Gorcunov8eed9262008-01-30 13:32:31 +010065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return v;
67}
68
69static int __init sbf_value_valid(u8 v)
70{
Cyrill Gorcunov8eed9262008-01-30 13:32:31 +010071 if (v & SBF_RESERVED) /* Reserved bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return 0;
Cyrill Gorcunov8eed9262008-01-30 13:32:31 +010073 if (!parity(v))
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return 0;
Cyrill Gorcunov8eed9262008-01-30 13:32:31 +010075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return 1;
77}
78
79static int __init sbf_init(void)
80{
81 u8 v;
Cyrill Gorcunov8eed9262008-01-30 13:32:31 +010082
83 if (sbf_port == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return 0;
Cyrill Gorcunov8eed9262008-01-30 13:32:31 +010085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 v = sbf_read();
Cyrill Gorcunov8eed9262008-01-30 13:32:31 +010087 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 Torvalds1da177e2005-04-16 15:20:36 -070091
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 Gorcunov8eed9262008-01-30 13:32:31 +010099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return 0;
101}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102module_init(sbf_init);