patch-2_6_7-vs1_9_1_12
[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, done;
41         unsigned long flags;
42         char *p = buf;
43
44
45         if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE)
46                 return -ENODEV;
47
48         if (*index >= nvram_size)
49                 return 0;
50
51         i = *index;
52         if (i + count > nvram_size)
53                 count = nvram_size - i;
54
55         spin_lock_irqsave(&nvram_lock, flags);
56
57         for (; count != 0; count -= len) {
58                 len = count;
59                 if (len > NVRW_CNT)
60                         len = NVRW_CNT;
61                 
62                 if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf),
63                                len) != 0) || len != done) {
64                         spin_unlock_irqrestore(&nvram_lock, flags);
65                         return -EIO;
66                 }
67                 
68                 memcpy(p, nvram_buf, len);
69
70                 p += len;
71                 i += len;
72         }
73
74         spin_unlock_irqrestore(&nvram_lock, flags);
75         
76         *index = i;
77         return p - buf;
78 }
79
80 static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index)
81 {
82         unsigned int i;
83         unsigned long len, done;
84         unsigned long flags;
85         const char *p = buf;
86
87         if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE)
88                 return -ENODEV;
89
90         if (*index >= nvram_size)
91                 return 0;
92
93         i = *index;
94         if (i + count > nvram_size)
95                 count = nvram_size - i;
96
97         spin_lock_irqsave(&nvram_lock, flags);
98
99         for (; count != 0; count -= len) {
100                 len = count;
101                 if (len > NVRW_CNT)
102                         len = NVRW_CNT;
103
104                 memcpy(nvram_buf, p, len);
105
106                 if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf),
107                                len) != 0) || len != done) {
108                         spin_unlock_irqrestore(&nvram_lock, flags);
109                         return -EIO;
110                 }
111                 
112                 p += len;
113                 i += len;
114         }
115         spin_unlock_irqrestore(&nvram_lock, flags);
116         
117         *index = i;
118         return p - buf;
119 }
120
121 static ssize_t pSeries_nvram_get_size(void)
122 {
123         return nvram_size ? nvram_size : -ENODEV;
124 }
125
126 int __init pSeries_nvram_init(void)
127 {
128         struct device_node *nvram;
129         unsigned int *nbytes_p, proplen;
130
131         nvram = of_find_node_by_type(NULL, "nvram");
132         if (nvram == NULL)
133                 return -ENODEV;
134
135         nbytes_p = (unsigned int *)get_property(nvram, "#bytes", &proplen);
136         if (nbytes_p == NULL || proplen != sizeof(unsigned int))
137                 return -EIO;
138
139         nvram_size = *nbytes_p;
140
141         nvram_fetch = rtas_token("nvram-fetch");
142         nvram_store = rtas_token("nvram-store");
143         printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size);
144         of_node_put(nvram);
145
146         ppc_md.nvram_read       = pSeries_nvram_read;
147         ppc_md.nvram_write      = pSeries_nvram_write;
148         ppc_md.nvram_size       = pSeries_nvram_get_size;
149
150         return 0;
151 }