1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * c 2001 PPC 64 Team, IBM Corp
5 * /dev/nvram driver for PPC
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/spinlock.h>
12 #include <linux/uaccess.h>
14 #include <asm/machdep.h>
18 static unsigned int nvram_size
;
19 static unsigned char nvram_buf
[4];
20 static DEFINE_SPINLOCK(nvram_lock
);
22 static unsigned char chrp_nvram_read_val(int addr
)
28 if (addr
>= nvram_size
) {
29 printk(KERN_DEBUG
"%s: read addr %d > nvram_size %u\n",
30 current
->comm
, addr
, nvram_size
);
33 spin_lock_irqsave(&nvram_lock
, flags
);
34 if ((rtas_call(rtas_token("nvram-fetch"), 3, 2, &done
, addr
,
35 __pa(nvram_buf
), 1) != 0) || 1 != done
)
39 spin_unlock_irqrestore(&nvram_lock
, flags
);
44 static void chrp_nvram_write_val(int addr
, unsigned char val
)
49 if (addr
>= nvram_size
) {
50 printk(KERN_DEBUG
"%s: write addr %d > nvram_size %u\n",
51 current
->comm
, addr
, nvram_size
);
54 spin_lock_irqsave(&nvram_lock
, flags
);
56 if ((rtas_call(rtas_token("nvram-store"), 3, 2, &done
, addr
,
57 __pa(nvram_buf
), 1) != 0) || 1 != done
)
58 printk(KERN_DEBUG
"rtas IO error storing 0x%02x at %d", val
, addr
);
59 spin_unlock_irqrestore(&nvram_lock
, flags
);
62 static ssize_t
chrp_nvram_size(void)
67 void __init
chrp_nvram_init(void)
69 struct device_node
*nvram
;
70 const __be32
*nbytes_p
;
73 nvram
= of_find_node_by_type(NULL
, "nvram");
77 nbytes_p
= of_get_property(nvram
, "#bytes", &proplen
);
78 if (nbytes_p
== NULL
|| proplen
!= sizeof(unsigned int)) {
83 nvram_size
= be32_to_cpup(nbytes_p
);
85 printk(KERN_INFO
"CHRP nvram contains %u bytes\n", nvram_size
);
88 ppc_md
.nvram_read_val
= chrp_nvram_read_val
;
89 ppc_md
.nvram_write_val
= chrp_nvram_write_val
;
90 ppc_md
.nvram_size
= chrp_nvram_size
;
95 MODULE_LICENSE("GPL v2");