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