VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / arch / ppc64 / kernel / pSeries_nvram.c
1 /*
2  *  c 2001 PPC 64 Team, IBM Corp
3  *
4  *      This program is free software; you can redistribute it and/or
5  *      modify it under the terms of the GNU General Public License
6  *      as published by the Free Software Foundation; either version
7  *      2 of the License, or (at your option) any later version.
8  *
9  * /dev/nvram driver for PPC64
10  *
11  * This perhaps should live in drivers/char
12  */
13
14 #include <linux/module.h>
15
16 #include <linux/types.h>
17 #include <linux/errno.h>
18 #include <linux/fs.h>
19 #include <linux/miscdevice.h>
20 #include <linux/fcntl.h>
21 #include <linux/nvram.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/spinlock.h>
25 #include <asm/uaccess.h>
26 #include <asm/nvram.h>
27 #include <asm/rtas.h>
28 #include <asm/prom.h>
29 #include <asm/machdep.h>
30
31 static unsigned int nvram_size;
32 static int nvram_fetch, nvram_store;
33 static char nvram_buf[NVRW_CNT];        /* assume this is in the first 4GB */
34 static spinlock_t nvram_lock = SPIN_LOCK_UNLOCKED;
35
36
37 static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index)
38 {
39         unsigned int i;
40         unsigned long len;
41         int done;
42         unsigned long flags;
43         char *p = buf;
44
45
46         if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE)
47                 return -ENODEV;
48
49         if (*index >= nvram_size)
50                 return 0;
51
52         i = *index;
53         if (i + count > nvram_size)
54                 count = nvram_size - i;
55
56         spin_lock_irqsave(&nvram_lock, flags);
57
58         for (; count != 0; count -= len) {
59                 len = count;
60                 if (len > NVRW_CNT)
61                         len = NVRW_CNT;
62                 
63                 if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf),
64                                len) != 0) || len != done) {
65                         spin_unlock_irqrestore(&nvram_lock, flags);
66                         return -EIO;
67                 }
68                 
69                 memcpy(p, nvram_buf, len);
70
71                 p += len;
72                 i += len;
73         }
74
75         spin_unlock_irqrestore(&nvram_lock, flags);
76         
77         *index = i;
78         return p - buf;
79 }
80
81 static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index)
82 {
83         unsigned int i;
84         unsigned long len;
85         int done;
86         unsigned long flags;
87         const char *p = buf;
88
89         if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE)
90                 return -ENODEV;
91
92         if (*index >= nvram_size)
93                 return 0;
94
95         i = *index;
96         if (i + count > nvram_size)
97                 count = nvram_size - i;
98
99         spin_lock_irqsave(&nvram_lock, flags);
100
101         for (; count != 0; count -= len) {
102                 len = count;
103                 if (len > NVRW_CNT)
104                         len = NVRW_CNT;
105
106                 memcpy(nvram_buf, p, len);
107
108                 if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf),
109                                len) != 0) || len != done) {
110                         spin_unlock_irqrestore(&nvram_lock, flags);
111                         return -EIO;
112                 }
113                 
114                 p += len;
115                 i += len;
116         }
117         spin_unlock_irqrestore(&nvram_lock, flags);
118         
119         *index = i;
120         return p - buf;
121 }
122
123 static ssize_t pSeries_nvram_get_size(void)
124 {
125         return nvram_size ? nvram_size : -ENODEV;
126 }
127
128 int __init pSeries_nvram_init(void)
129 {
130         struct device_node *nvram;
131         unsigned int *nbytes_p, proplen;
132
133         nvram = of_find_node_by_type(NULL, "nvram");
134         if (nvram == NULL)
135                 return -ENODEV;
136
137         nbytes_p = (unsigned int *)get_property(nvram, "#bytes", &proplen);
138         if (nbytes_p == NULL || proplen != sizeof(unsigned int))
139                 return -EIO;
140
141         nvram_size = *nbytes_p;
142
143         nvram_fetch = rtas_token("nvram-fetch");
144         nvram_store = rtas_token("nvram-store");
145         printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size);
146         of_node_put(nvram);
147
148         ppc_md.nvram_read       = pSeries_nvram_read;
149         ppc_md.nvram_write      = pSeries_nvram_write;
150         ppc_md.nvram_size       = pSeries_nvram_get_size;
151
152         return 0;
153 }