2 * c 2001 PPC 64 Team, IBM Corp
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.
9 * /dev/nvram driver for PPC64
11 * This perhaps should live in drivers/char
14 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/errno.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>
29 #include <asm/machdep.h>
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;
37 static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index)
46 if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE)
49 if (*index >= nvram_size)
53 if (i + count > nvram_size)
54 count = nvram_size - i;
56 spin_lock_irqsave(&nvram_lock, flags);
58 for (; count != 0; count -= len) {
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);
69 memcpy(p, nvram_buf, len);
75 spin_unlock_irqrestore(&nvram_lock, flags);
81 static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index)
89 if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE)
92 if (*index >= nvram_size)
96 if (i + count > nvram_size)
97 count = nvram_size - i;
99 spin_lock_irqsave(&nvram_lock, flags);
101 for (; count != 0; count -= len) {
106 memcpy(nvram_buf, p, len);
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);
117 spin_unlock_irqrestore(&nvram_lock, flags);
123 static ssize_t pSeries_nvram_get_size(void)
125 return nvram_size ? nvram_size : -ENODEV;
128 int __init pSeries_nvram_init(void)
130 struct device_node *nvram;
131 unsigned int *nbytes_p, proplen;
133 nvram = of_find_node_by_type(NULL, "nvram");
137 nbytes_p = (unsigned int *)get_property(nvram, "#bytes", &proplen);
138 if (nbytes_p == NULL || proplen != sizeof(unsigned int))
141 nvram_size = *nbytes_p;
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);
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;