nvram.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. */
  10. #include <linux/errno.h>
  11. #include <linux/init.h>
  12. #include <linux/mc146818rtc.h>
  13. #include <linux/module.h>
  14. #include <linux/nvram.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/types.h>
  19. #include <asm/atarihw.h>
  20. #include <asm/atariints.h>
  21. #define NVRAM_BYTES 50
  22. /* It is worth noting that these functions all access bytes of general
  23. * purpose memory in the NVRAM - that is to say, they all add the
  24. * NVRAM_FIRST_BYTE offset. Pass them offsets into NVRAM as if you did not
  25. * know about the RTC cruft.
  26. */
  27. /* Note that *all* calls to CMOS_READ and CMOS_WRITE must be done with
  28. * rtc_lock held. Due to the index-port/data-port design of the RTC, we
  29. * don't want two different things trying to get to it at once. (e.g. the
  30. * periodic 11 min sync from kernel/time/ntp.c vs. this driver.)
  31. */
  32. static unsigned char __nvram_read_byte(int i)
  33. {
  34. return CMOS_READ(NVRAM_FIRST_BYTE + i);
  35. }
  36. /* This races nicely with trying to read with checksum checking */
  37. static void __nvram_write_byte(unsigned char c, int i)
  38. {
  39. CMOS_WRITE(c, NVRAM_FIRST_BYTE + i);
  40. }
  41. /* On Ataris, the checksum is over all bytes except the checksum bytes
  42. * themselves; these are at the very end.
  43. */
  44. #define ATARI_CKS_RANGE_START 0
  45. #define ATARI_CKS_RANGE_END 47
  46. #define ATARI_CKS_LOC 48
  47. static int __nvram_check_checksum(void)
  48. {
  49. int i;
  50. unsigned char sum = 0;
  51. for (i = ATARI_CKS_RANGE_START; i <= ATARI_CKS_RANGE_END; ++i)
  52. sum += __nvram_read_byte(i);
  53. return (__nvram_read_byte(ATARI_CKS_LOC) == (~sum & 0xff)) &&
  54. (__nvram_read_byte(ATARI_CKS_LOC + 1) == (sum & 0xff));
  55. }
  56. static void __nvram_set_checksum(void)
  57. {
  58. int i;
  59. unsigned char sum = 0;
  60. for (i = ATARI_CKS_RANGE_START; i <= ATARI_CKS_RANGE_END; ++i)
  61. sum += __nvram_read_byte(i);
  62. __nvram_write_byte(~sum, ATARI_CKS_LOC);
  63. __nvram_write_byte(sum, ATARI_CKS_LOC + 1);
  64. }
  65. long atari_nvram_set_checksum(void)
  66. {
  67. spin_lock_irq(&rtc_lock);
  68. __nvram_set_checksum();
  69. spin_unlock_irq(&rtc_lock);
  70. return 0;
  71. }
  72. long atari_nvram_initialize(void)
  73. {
  74. loff_t i;
  75. spin_lock_irq(&rtc_lock);
  76. for (i = 0; i < NVRAM_BYTES; ++i)
  77. __nvram_write_byte(0, i);
  78. __nvram_set_checksum();
  79. spin_unlock_irq(&rtc_lock);
  80. return 0;
  81. }
  82. ssize_t atari_nvram_read(char *buf, size_t count, loff_t *ppos)
  83. {
  84. char *p = buf;
  85. loff_t i;
  86. spin_lock_irq(&rtc_lock);
  87. if (!__nvram_check_checksum()) {
  88. spin_unlock_irq(&rtc_lock);
  89. return -EIO;
  90. }
  91. for (i = *ppos; count > 0 && i < NVRAM_BYTES; --count, ++i, ++p)
  92. *p = __nvram_read_byte(i);
  93. spin_unlock_irq(&rtc_lock);
  94. *ppos = i;
  95. return p - buf;
  96. }
  97. ssize_t atari_nvram_write(char *buf, size_t count, loff_t *ppos)
  98. {
  99. char *p = buf;
  100. loff_t i;
  101. spin_lock_irq(&rtc_lock);
  102. if (!__nvram_check_checksum()) {
  103. spin_unlock_irq(&rtc_lock);
  104. return -EIO;
  105. }
  106. for (i = *ppos; count > 0 && i < NVRAM_BYTES; --count, ++i, ++p)
  107. __nvram_write_byte(*p, i);
  108. __nvram_set_checksum();
  109. spin_unlock_irq(&rtc_lock);
  110. *ppos = i;
  111. return p - buf;
  112. }
  113. ssize_t atari_nvram_get_size(void)
  114. {
  115. return NVRAM_BYTES;
  116. }
  117. #ifdef CONFIG_PROC_FS
  118. static struct {
  119. unsigned char val;
  120. const char *name;
  121. } boot_prefs[] = {
  122. { 0x80, "TOS" },
  123. { 0x40, "ASV" },
  124. { 0x20, "NetBSD (?)" },
  125. { 0x10, "Linux" },
  126. { 0x00, "unspecified" },
  127. };
  128. static const char * const languages[] = {
  129. "English (US)",
  130. "German",
  131. "French",
  132. "English (UK)",
  133. "Spanish",
  134. "Italian",
  135. "6 (undefined)",
  136. "Swiss (French)",
  137. "Swiss (German)",
  138. };
  139. static const char * const dateformat[] = {
  140. "MM%cDD%cYY",
  141. "DD%cMM%cYY",
  142. "YY%cMM%cDD",
  143. "YY%cDD%cMM",
  144. "4 (undefined)",
  145. "5 (undefined)",
  146. "6 (undefined)",
  147. "7 (undefined)",
  148. };
  149. static const char * const colors[] = {
  150. "2", "4", "16", "256", "65536", "??", "??", "??"
  151. };
  152. static void atari_nvram_proc_read(unsigned char *nvram, struct seq_file *seq,
  153. void *offset)
  154. {
  155. int checksum;
  156. int i;
  157. unsigned int vmode;
  158. spin_lock_irq(&rtc_lock);
  159. checksum = __nvram_check_checksum();
  160. spin_unlock_irq(&rtc_lock);
  161. seq_printf(seq, "Checksum status : %svalid\n", checksum ? "" : "not ");
  162. seq_puts(seq, "Boot preference : ");
  163. for (i = ARRAY_SIZE(boot_prefs) - 1; i >= 0; --i)
  164. if (nvram[1] == boot_prefs[i].val) {
  165. seq_printf(seq, "%s\n", boot_prefs[i].name);
  166. break;
  167. }
  168. if (i < 0)
  169. seq_printf(seq, "0x%02x (undefined)\n", nvram[1]);
  170. seq_printf(seq, "SCSI arbitration : %s\n",
  171. (nvram[16] & 0x80) ? "on" : "off");
  172. seq_puts(seq, "SCSI host ID : ");
  173. if (nvram[16] & 0x80)
  174. seq_printf(seq, "%d\n", nvram[16] & 7);
  175. else
  176. seq_puts(seq, "n/a\n");
  177. if (!MACH_IS_FALCON)
  178. return;
  179. seq_puts(seq, "OS language : ");
  180. if (nvram[6] < ARRAY_SIZE(languages))
  181. seq_printf(seq, "%s\n", languages[nvram[6]]);
  182. else
  183. seq_printf(seq, "%u (undefined)\n", nvram[6]);
  184. seq_puts(seq, "Keyboard language: ");
  185. if (nvram[7] < ARRAY_SIZE(languages))
  186. seq_printf(seq, "%s\n", languages[nvram[7]]);
  187. else
  188. seq_printf(seq, "%u (undefined)\n", nvram[7]);
  189. seq_puts(seq, "Date format : ");
  190. seq_printf(seq, dateformat[nvram[8] & 7],
  191. nvram[9] ? nvram[9] : '/', nvram[9] ? nvram[9] : '/');
  192. seq_printf(seq, ", %dh clock\n", nvram[8] & 16 ? 24 : 12);
  193. seq_puts(seq, "Boot delay : ");
  194. if (nvram[10] == 0)
  195. seq_puts(seq, "default\n");
  196. else
  197. seq_printf(seq, "%ds%s\n", nvram[10],
  198. nvram[10] < 8 ? ", no memory test" : "");
  199. vmode = (nvram[14] << 8) | nvram[15];
  200. seq_printf(seq,
  201. "Video mode : %s colors, %d columns, %s %s monitor\n",
  202. colors[vmode & 7], vmode & 8 ? 80 : 40,
  203. vmode & 16 ? "VGA" : "TV", vmode & 32 ? "PAL" : "NTSC");
  204. seq_printf(seq,
  205. " %soverscan, compat. mode %s%s\n",
  206. vmode & 64 ? "" : "no ", vmode & 128 ? "on" : "off",
  207. vmode & 256 ?
  208. (vmode & 16 ? ", line doubling" : ", half screen") : "");
  209. }
  210. static int nvram_proc_read(struct seq_file *seq, void *offset)
  211. {
  212. unsigned char contents[NVRAM_BYTES];
  213. int i;
  214. spin_lock_irq(&rtc_lock);
  215. for (i = 0; i < NVRAM_BYTES; ++i)
  216. contents[i] = __nvram_read_byte(i);
  217. spin_unlock_irq(&rtc_lock);
  218. atari_nvram_proc_read(contents, seq, offset);
  219. return 0;
  220. }
  221. static int __init atari_nvram_init(void)
  222. {
  223. if (!(MACH_IS_ATARI && ATARIHW_PRESENT(TT_CLK)))
  224. return -ENODEV;
  225. if (!proc_create_single("driver/nvram", 0, NULL, nvram_proc_read)) {
  226. pr_err("nvram: can't create /proc/driver/nvram\n");
  227. return -ENOMEM;
  228. }
  229. return 0;
  230. }
  231. device_initcall(atari_nvram_init);
  232. #endif /* CONFIG_PROC_FS */