Merge tag 'x86-urgent-2025-01-28' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / arch / m68k / atari / nvram.c
blobededc31375c37d81f87b66661ebe122efb789c33
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * CMOS/NV-RAM driver for Atari. Adapted from drivers/char/nvram.c.
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 * Further contributions from Cesar Barros, Erik Gilling, Tim Hockin and
8 * Wim Van Sebroeck.
9 */
11 #include <linux/errno.h>
12 #include <linux/init.h>
13 #include <linux/mc146818rtc.h>
14 #include <linux/module.h>
15 #include <linux/nvram.h>
16 #include <linux/proc_fs.h>
17 #include <linux/seq_file.h>
18 #include <linux/spinlock.h>
19 #include <linux/string_choices.h>
20 #include <linux/types.h>
22 #include <asm/atarihw.h>
23 #include <asm/atariints.h>
25 #define NVRAM_BYTES 50
27 /* It is worth noting that these functions all access bytes of general
28 * purpose memory in the NVRAM - that is to say, they all add the
29 * NVRAM_FIRST_BYTE offset. Pass them offsets into NVRAM as if you did not
30 * know about the RTC cruft.
33 /* Note that *all* calls to CMOS_READ and CMOS_WRITE must be done with
34 * rtc_lock held. Due to the index-port/data-port design of the RTC, we
35 * don't want two different things trying to get to it at once. (e.g. the
36 * periodic 11 min sync from kernel/time/ntp.c vs. this driver.)
39 static unsigned char __nvram_read_byte(int i)
41 return CMOS_READ(NVRAM_FIRST_BYTE + i);
44 /* This races nicely with trying to read with checksum checking */
45 static void __nvram_write_byte(unsigned char c, int i)
47 CMOS_WRITE(c, NVRAM_FIRST_BYTE + i);
50 /* On Ataris, the checksum is over all bytes except the checksum bytes
51 * themselves; these are at the very end.
53 #define ATARI_CKS_RANGE_START 0
54 #define ATARI_CKS_RANGE_END 47
55 #define ATARI_CKS_LOC 48
57 static int __nvram_check_checksum(void)
59 int i;
60 unsigned char sum = 0;
62 for (i = ATARI_CKS_RANGE_START; i <= ATARI_CKS_RANGE_END; ++i)
63 sum += __nvram_read_byte(i);
64 return (__nvram_read_byte(ATARI_CKS_LOC) == (~sum & 0xff)) &&
65 (__nvram_read_byte(ATARI_CKS_LOC + 1) == (sum & 0xff));
68 static void __nvram_set_checksum(void)
70 int i;
71 unsigned char sum = 0;
73 for (i = ATARI_CKS_RANGE_START; i <= ATARI_CKS_RANGE_END; ++i)
74 sum += __nvram_read_byte(i);
75 __nvram_write_byte(~sum, ATARI_CKS_LOC);
76 __nvram_write_byte(sum, ATARI_CKS_LOC + 1);
79 long atari_nvram_set_checksum(void)
81 spin_lock_irq(&rtc_lock);
82 __nvram_set_checksum();
83 spin_unlock_irq(&rtc_lock);
84 return 0;
87 long atari_nvram_initialize(void)
89 loff_t i;
91 spin_lock_irq(&rtc_lock);
92 for (i = 0; i < NVRAM_BYTES; ++i)
93 __nvram_write_byte(0, i);
94 __nvram_set_checksum();
95 spin_unlock_irq(&rtc_lock);
96 return 0;
99 ssize_t atari_nvram_read(char *buf, size_t count, loff_t *ppos)
101 char *p = buf;
102 loff_t i;
104 spin_lock_irq(&rtc_lock);
105 if (!__nvram_check_checksum()) {
106 spin_unlock_irq(&rtc_lock);
107 return -EIO;
109 for (i = *ppos; count > 0 && i < NVRAM_BYTES; --count, ++i, ++p)
110 *p = __nvram_read_byte(i);
111 spin_unlock_irq(&rtc_lock);
113 *ppos = i;
114 return p - buf;
117 ssize_t atari_nvram_write(char *buf, size_t count, loff_t *ppos)
119 char *p = buf;
120 loff_t i;
122 spin_lock_irq(&rtc_lock);
123 if (!__nvram_check_checksum()) {
124 spin_unlock_irq(&rtc_lock);
125 return -EIO;
127 for (i = *ppos; count > 0 && i < NVRAM_BYTES; --count, ++i, ++p)
128 __nvram_write_byte(*p, i);
129 __nvram_set_checksum();
130 spin_unlock_irq(&rtc_lock);
132 *ppos = i;
133 return p - buf;
136 ssize_t atari_nvram_get_size(void)
138 return NVRAM_BYTES;
141 #ifdef CONFIG_PROC_FS
142 static struct {
143 unsigned char val;
144 const char *name;
145 } boot_prefs[] = {
146 { 0x80, "TOS" },
147 { 0x40, "ASV" },
148 { 0x20, "NetBSD (?)" },
149 { 0x10, "Linux" },
150 { 0x00, "unspecified" },
153 static const char * const languages[] = {
154 "English (US)",
155 "German",
156 "French",
157 "English (UK)",
158 "Spanish",
159 "Italian",
160 "6 (undefined)",
161 "Swiss (French)",
162 "Swiss (German)",
165 static const char * const dateformat[] = {
166 "MM%cDD%cYY",
167 "DD%cMM%cYY",
168 "YY%cMM%cDD",
169 "YY%cDD%cMM",
170 "4 (undefined)",
171 "5 (undefined)",
172 "6 (undefined)",
173 "7 (undefined)",
176 static const char * const colors[] = {
177 "2", "4", "16", "256", "65536", "??", "??", "??"
180 static void atari_nvram_proc_read(unsigned char *nvram, struct seq_file *seq,
181 void *offset)
183 int checksum;
184 int i;
185 unsigned int vmode;
187 spin_lock_irq(&rtc_lock);
188 checksum = __nvram_check_checksum();
189 spin_unlock_irq(&rtc_lock);
191 seq_printf(seq, "Checksum status : %svalid\n", checksum ? "" : "not ");
193 seq_puts(seq, "Boot preference : ");
194 for (i = ARRAY_SIZE(boot_prefs) - 1; i >= 0; --i)
195 if (nvram[1] == boot_prefs[i].val) {
196 seq_printf(seq, "%s\n", boot_prefs[i].name);
197 break;
199 if (i < 0)
200 seq_printf(seq, "0x%02x (undefined)\n", nvram[1]);
202 seq_printf(seq, "SCSI arbitration : %s\n",
203 str_on_off(nvram[16] & 0x80));
204 seq_puts(seq, "SCSI host ID : ");
205 if (nvram[16] & 0x80)
206 seq_printf(seq, "%d\n", nvram[16] & 7);
207 else
208 seq_puts(seq, "n/a\n");
210 if (!MACH_IS_FALCON)
211 return;
213 seq_puts(seq, "OS language : ");
214 if (nvram[6] < ARRAY_SIZE(languages))
215 seq_printf(seq, "%s\n", languages[nvram[6]]);
216 else
217 seq_printf(seq, "%u (undefined)\n", nvram[6]);
218 seq_puts(seq, "Keyboard language: ");
219 if (nvram[7] < ARRAY_SIZE(languages))
220 seq_printf(seq, "%s\n", languages[nvram[7]]);
221 else
222 seq_printf(seq, "%u (undefined)\n", nvram[7]);
223 seq_puts(seq, "Date format : ");
224 seq_printf(seq, dateformat[nvram[8] & 7],
225 nvram[9] ? nvram[9] : '/', nvram[9] ? nvram[9] : '/');
226 seq_printf(seq, ", %dh clock\n", nvram[8] & 16 ? 24 : 12);
227 seq_puts(seq, "Boot delay : ");
228 if (nvram[10] == 0)
229 seq_puts(seq, "default\n");
230 else
231 seq_printf(seq, "%ds%s\n", nvram[10],
232 nvram[10] < 8 ? ", no memory test" : "");
234 vmode = (nvram[14] << 8) | nvram[15];
235 seq_printf(seq,
236 "Video mode : %s colors, %d columns, %s %s monitor\n",
237 colors[vmode & 7], vmode & 8 ? 80 : 40,
238 vmode & 16 ? "VGA" : "TV", vmode & 32 ? "PAL" : "NTSC");
239 seq_printf(seq,
240 " %soverscan, compat. mode %s%s\n",
241 vmode & 64 ? "" : "no ", str_on_off(vmode & 128),
242 vmode & 256 ?
243 (vmode & 16 ? ", line doubling" : ", half screen") : "");
246 static int nvram_proc_read(struct seq_file *seq, void *offset)
248 unsigned char contents[NVRAM_BYTES];
249 int i;
251 spin_lock_irq(&rtc_lock);
252 for (i = 0; i < NVRAM_BYTES; ++i)
253 contents[i] = __nvram_read_byte(i);
254 spin_unlock_irq(&rtc_lock);
256 atari_nvram_proc_read(contents, seq, offset);
258 return 0;
261 static int __init atari_nvram_init(void)
263 if (!(MACH_IS_ATARI && ATARIHW_PRESENT(TT_CLK)))
264 return -ENODEV;
266 if (!proc_create_single("driver/nvram", 0, NULL, nvram_proc_read)) {
267 pr_err("nvram: can't create /proc/driver/nvram\n");
268 return -ENOMEM;
271 return 0;
273 device_initcall(atari_nvram_init);
274 #endif /* CONFIG_PROC_FS */