blob: 474dccbc4a8a9eff06be27bde3614e652e05e722 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1998 Corey Minyard
3 *
4 * This reads the NvRAM on PReP compliant machines (generally from IBM or
5 * Motorola). Motorola kept the format of NvRAM in their ROM, PPCBUG, the
6 * same, long after they had stopped producing PReP compliant machines. So
7 * this code is useful in those cases as well.
8 *
9 */
10#include <linux/init.h>
11#include <linux/delay.h>
12#include <linux/slab.h>
13#include <linux/ioport.h>
14
15#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/io.h>
17#include <asm/machdep.h>
18#include <asm/prep_nvram.h>
19
20static char nvramData[MAX_PREP_NVRAM];
21static NVRAM_MAP *nvram=(NVRAM_MAP *)&nvramData[0];
22
Jon Loeligerf495a8b2005-09-17 10:35:08 -050023unsigned char prep_nvram_read_val(int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
25 outb(addr, PREP_NVRAM_AS0);
26 outb(addr>>8, PREP_NVRAM_AS1);
27 return inb(PREP_NVRAM_DATA);
28}
29
Jon Loeligerf495a8b2005-09-17 10:35:08 -050030void prep_nvram_write_val(int addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 unsigned char val)
32{
33 outb(addr, PREP_NVRAM_AS0);
34 outb(addr>>8, PREP_NVRAM_AS1);
35 outb(val, PREP_NVRAM_DATA);
36}
37
38void __init init_prep_nvram(void)
39{
40 unsigned char *nvp;
41 int i;
42 int nvramSize;
43
44 /*
45 * The following could fail if the NvRAM were corrupt but
46 * we expect the boot firmware to have checked its checksum
47 * before boot
48 */
49 nvp = (char *) &nvram->Header;
50 for (i=0; i<sizeof(HEADER); i++)
51 {
52 *nvp = ppc_md.nvram_read_val(i);
53 nvp++;
54 }
55
56 /*
57 * The PReP NvRAM may be any size so read in the header to
58 * determine how much we must read in order to get the complete
59 * GE area
60 */
61 nvramSize=(int)nvram->Header.GEAddress+nvram->Header.GELength;
62 if(nvramSize>MAX_PREP_NVRAM)
63 {
64 /*
65 * NvRAM is too large
66 */
67 nvram->Header.GELength=0;
68 return;
69 }
70
71 /*
72 * Read the remainder of the PReP NvRAM
73 */
74 nvp = (char *) &nvram->GEArea[0];
75 for (i=sizeof(HEADER); i<nvramSize; i++)
76 {
77 *nvp = ppc_md.nvram_read_val(i);
78 nvp++;
79 }
80}
81
Jon Loeligerf495a8b2005-09-17 10:35:08 -050082char *prep_nvram_get_var(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 char *cp;
85 int namelen;
86
87 namelen = strlen(name);
88 cp = prep_nvram_first_var();
89 while (cp != NULL) {
90 if ((strncmp(name, cp, namelen) == 0)
91 && (cp[namelen] == '='))
92 {
93 return cp+namelen+1;
94 }
95 cp = prep_nvram_next_var(cp);
96 }
97
98 return NULL;
99}
100
Jon Loeligerf495a8b2005-09-17 10:35:08 -0500101char *prep_nvram_first_var(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 if (nvram->Header.GELength == 0) {
104 return NULL;
105 } else {
106 return (((char *)nvram)
107 + ((unsigned int) nvram->Header.GEAddress));
108 }
109}
110
Jon Loeligerf495a8b2005-09-17 10:35:08 -0500111char *prep_nvram_next_var(char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 char *cp;
114
115
116 cp = name;
117 while (((cp - ((char *) nvram->GEArea)) < nvram->Header.GELength)
118 && (*cp != '\0'))
119 {
120 cp++;
121 }
122
123 /* Skip over any null characters. */
124 while (((cp - ((char *) nvram->GEArea)) < nvram->Header.GELength)
125 && (*cp == '\0'))
126 {
127 cp++;
128 }
129
130 if ((cp - ((char *) nvram->GEArea)) < nvram->Header.GELength) {
131 return cp;
132 } else {
133 return NULL;
134 }
135}