xref: /linux/arch/powerpc/platforms/chrp/nvram.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
235e95e63SOlaf Hering /*
335e95e63SOlaf Hering  *  c 2001 PPC 64 Team, IBM Corp
435e95e63SOlaf Hering  *
535e95e63SOlaf Hering  * /dev/nvram driver for PPC
635e95e63SOlaf Hering  */
735e95e63SOlaf Hering 
835e95e63SOlaf Hering #include <linux/kernel.h>
9a8bf9e50SRandy Dunlap #include <linux/module.h>
1035e95e63SOlaf Hering #include <linux/init.h>
1135e95e63SOlaf Hering #include <linux/spinlock.h>
127c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
13e6f6390aSChristophe Leroy #include <linux/of.h>
1435e95e63SOlaf Hering #include <asm/machdep.h>
1539838299SPaul Mackerras #include <asm/rtas.h>
1635e95e63SOlaf Hering #include "chrp.h"
1735e95e63SOlaf Hering 
1835e95e63SOlaf Hering static unsigned int nvram_size;
1935e95e63SOlaf Hering static unsigned char nvram_buf[4];
2035e95e63SOlaf Hering static DEFINE_SPINLOCK(nvram_lock);
2135e95e63SOlaf Hering 
chrp_nvram_read_val(int addr)22ebcebc7fSFinn Thain static unsigned char chrp_nvram_read_val(int addr)
2335e95e63SOlaf Hering {
2439838299SPaul Mackerras 	unsigned int done;
2539838299SPaul Mackerras 	unsigned long flags;
2635e95e63SOlaf Hering 	unsigned char ret;
2735e95e63SOlaf Hering 
2835e95e63SOlaf Hering 	if (addr >= nvram_size) {
2935e95e63SOlaf Hering 		printk(KERN_DEBUG "%s: read addr %d > nvram_size %u\n",
3035e95e63SOlaf Hering 		       current->comm, addr, nvram_size);
3135e95e63SOlaf Hering 		return 0xff;
3235e95e63SOlaf Hering 	}
3335e95e63SOlaf Hering 	spin_lock_irqsave(&nvram_lock, flags);
3408273c9fSNathan Lynch 	if ((rtas_call(rtas_function_token(RTAS_FN_NVRAM_FETCH), 3, 2, &done, addr,
3519fa17e5SPaul Mackerras 		       __pa(nvram_buf), 1) != 0) || 1 != done)
3635e95e63SOlaf Hering 		ret = 0xff;
3735e95e63SOlaf Hering 	else
3835e95e63SOlaf Hering 		ret = nvram_buf[0];
3935e95e63SOlaf Hering 	spin_unlock_irqrestore(&nvram_lock, flags);
4035e95e63SOlaf Hering 
4135e95e63SOlaf Hering 	return ret;
4235e95e63SOlaf Hering }
4335e95e63SOlaf Hering 
chrp_nvram_write_val(int addr,unsigned char val)44ebcebc7fSFinn Thain static void chrp_nvram_write_val(int addr, unsigned char val)
4535e95e63SOlaf Hering {
4639838299SPaul Mackerras 	unsigned int done;
4739838299SPaul Mackerras 	unsigned long flags;
4835e95e63SOlaf Hering 
4935e95e63SOlaf Hering 	if (addr >= nvram_size) {
5035e95e63SOlaf Hering 		printk(KERN_DEBUG "%s: write addr %d > nvram_size %u\n",
5135e95e63SOlaf Hering 		       current->comm, addr, nvram_size);
5235e95e63SOlaf Hering 		return;
5335e95e63SOlaf Hering 	}
5435e95e63SOlaf Hering 	spin_lock_irqsave(&nvram_lock, flags);
5535e95e63SOlaf Hering 	nvram_buf[0] = val;
5608273c9fSNathan Lynch 	if ((rtas_call(rtas_function_token(RTAS_FN_NVRAM_STORE), 3, 2, &done, addr,
5719fa17e5SPaul Mackerras 		       __pa(nvram_buf), 1) != 0) || 1 != done)
5835e95e63SOlaf Hering 		printk(KERN_DEBUG "rtas IO error storing 0x%02x at %d", val, addr);
5935e95e63SOlaf Hering 	spin_unlock_irqrestore(&nvram_lock, flags);
6035e95e63SOlaf Hering }
6135e95e63SOlaf Hering 
chrp_nvram_size(void)62ebcebc7fSFinn Thain static ssize_t chrp_nvram_size(void)
63ebcebc7fSFinn Thain {
64ebcebc7fSFinn Thain 	return nvram_size;
65ebcebc7fSFinn Thain }
66ebcebc7fSFinn Thain 
chrp_nvram_init(void)6735e95e63SOlaf Hering void __init chrp_nvram_init(void)
6835e95e63SOlaf Hering {
6935e95e63SOlaf Hering 	struct device_node *nvram;
70563c5d8aSCedric Le Goater 	const __be32 *nbytes_p;
71ae6b4101SJeremy Kerr 	unsigned int proplen;
7235e95e63SOlaf Hering 
7335e95e63SOlaf Hering 	nvram = of_find_node_by_type(NULL, "nvram");
7435e95e63SOlaf Hering 	if (nvram == NULL)
7535e95e63SOlaf Hering 		return;
7635e95e63SOlaf Hering 
77e2eb6392SStephen Rothwell 	nbytes_p = of_get_property(nvram, "#bytes", &proplen);
787cf9bac5SJulia Lawall 	if (nbytes_p == NULL || proplen != sizeof(unsigned int)) {
797cf9bac5SJulia Lawall 		of_node_put(nvram);
8035e95e63SOlaf Hering 		return;
817cf9bac5SJulia Lawall 	}
8235e95e63SOlaf Hering 
83563c5d8aSCedric Le Goater 	nvram_size = be32_to_cpup(nbytes_p);
8435e95e63SOlaf Hering 
8535e95e63SOlaf Hering 	printk(KERN_INFO "CHRP nvram contains %u bytes\n", nvram_size);
8635e95e63SOlaf Hering 	of_node_put(nvram);
8735e95e63SOlaf Hering 
88ebcebc7fSFinn Thain 	ppc_md.nvram_read_val  = chrp_nvram_read_val;
89ebcebc7fSFinn Thain 	ppc_md.nvram_write_val = chrp_nvram_write_val;
90ebcebc7fSFinn Thain 	ppc_md.nvram_size      = chrp_nvram_size;
9135e95e63SOlaf Hering 
9235e95e63SOlaf Hering 	return;
9335e95e63SOlaf Hering }
94a8bf9e50SRandy Dunlap 
95*9c5f6473SJeff Johnson MODULE_DESCRIPTION("PPC NVRAM device driver");
96a8bf9e50SRandy Dunlap MODULE_LICENSE("GPL v2");
97