Merge branch 'x86-txt-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / arch / powerpc / platforms / 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
15 #include <linux/types.h>
16 #include <linux/errno.h>
17 #include <linux/init.h>
18 #include <linux/spinlock.h>
19 #include <asm/uaccess.h>
20 #include <asm/nvram.h>
21 #include <asm/rtas.h>
22 #include <asm/prom.h>
23 #include <asm/machdep.h>
24
25 static unsigned int nvram_size;
26 static int nvram_fetch, nvram_store;
27 static char nvram_buf[NVRW_CNT];        /* assume this is in the first 4GB */
28 static DEFINE_SPINLOCK(nvram_lock);
29
30
31 static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index)
32 {
33         unsigned int i;
34         unsigned long len;
35         int done;
36         unsigned long flags;
37         char *p = buf;
38
39
40         if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE)
41                 return -ENODEV;
42
43         if (*index >= nvram_size)
44                 return 0;
45
46         i = *index;
47         if (i + count > nvram_size)
48                 count = nvram_size - i;
49
50         spin_lock_irqsave(&nvram_lock, flags);
51
52         for (; count != 0; count -= len) {
53                 len = count;
54                 if (len > NVRW_CNT)
55                         len = NVRW_CNT;
56                 
57                 if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf),
58                                len) != 0) || len != done) {
59                         spin_unlock_irqrestore(&nvram_lock, flags);
60                         return -EIO;
61                 }
62                 
63                 memcpy(p, nvram_buf, len);
64
65                 p += len;
66                 i += len;
67         }
68
69         spin_unlock_irqrestore(&nvram_lock, flags);
70         
71         *index = i;
72         return p - buf;
73 }
74
75 static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index)
76 {
77         unsigned int i;
78         unsigned long len;
79         int done;
80         unsigned long flags;
81         const char *p = buf;
82
83         if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE)
84                 return -ENODEV;
85
86         if (*index >= nvram_size)
87                 return 0;
88
89         i = *index;
90         if (i + count > nvram_size)
91                 count = nvram_size - i;
92
93         spin_lock_irqsave(&nvram_lock, flags);
94
95         for (; count != 0; count -= len) {
96                 len = count;
97                 if (len > NVRW_CNT)
98                         len = NVRW_CNT;
99
100                 memcpy(nvram_buf, p, len);
101
102                 if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf),
103                                len) != 0) || len != done) {
104                         spin_unlock_irqrestore(&nvram_lock, flags);
105                         return -EIO;
106                 }
107                 
108                 p += len;
109                 i += len;
110         }
111         spin_unlock_irqrestore(&nvram_lock, flags);
112         
113         *index = i;
114         return p - buf;
115 }
116
117 static ssize_t pSeries_nvram_get_size(void)
118 {
119         return nvram_size ? nvram_size : -ENODEV;
120 }
121
122 int __init pSeries_nvram_init(void)
123 {
124         struct device_node *nvram;
125         const unsigned int *nbytes_p;
126         unsigned int proplen;
127
128         nvram = of_find_node_by_type(NULL, "nvram");
129         if (nvram == NULL)
130                 return -ENODEV;
131
132         nbytes_p = of_get_property(nvram, "#bytes", &proplen);
133         if (nbytes_p == NULL || proplen != sizeof(unsigned int)) {
134                 of_node_put(nvram);
135                 return -EIO;
136         }
137
138         nvram_size = *nbytes_p;
139
140         nvram_fetch = rtas_token("nvram-fetch");
141         nvram_store = rtas_token("nvram-store");
142         printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size);
143         of_node_put(nvram);
144
145         ppc_md.nvram_read       = pSeries_nvram_read;
146         ppc_md.nvram_write      = pSeries_nvram_write;
147         ppc_md.nvram_size       = pSeries_nvram_get_size;
148
149         return 0;
150 }