blob: 65266b46db9b6a0e8da7b822d3739b5e0f10bac2 [file] [log] [blame]
Olaf Hering35e95e62005-10-28 17:46:19 -07001/*
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 PPC
10 *
11 */
12
13#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/slab.h>
16#include <linux/spinlock.h>
17#include <asm/uaccess.h>
18#include <asm/prom.h>
19#include <asm/machdep.h>
20#include "chrp.h"
21
22static unsigned int nvram_size;
23static unsigned char nvram_buf[4];
24static DEFINE_SPINLOCK(nvram_lock);
25
26static unsigned char chrp_nvram_read(int addr)
27{
28 unsigned long done, flags;
29 unsigned char ret;
30
31 if (addr >= nvram_size) {
32 printk(KERN_DEBUG "%s: read addr %d > nvram_size %u\n",
33 current->comm, addr, nvram_size);
34 return 0xff;
35 }
36 spin_lock_irqsave(&nvram_lock, flags);
Paul Mackerras19fa17e2005-11-02 15:04:26 +110037 if ((rtas_call(rtas_token("nvram-fetch"), 3, 2, &done, addr,
38 __pa(nvram_buf), 1) != 0) || 1 != done)
Olaf Hering35e95e62005-10-28 17:46:19 -070039 ret = 0xff;
40 else
41 ret = nvram_buf[0];
42 spin_unlock_irqrestore(&nvram_lock, flags);
43
44 return ret;
45}
46
47static void chrp_nvram_write(int addr, unsigned char val)
48{
49 unsigned long done, flags;
50
51 if (addr >= nvram_size) {
52 printk(KERN_DEBUG "%s: write addr %d > nvram_size %u\n",
53 current->comm, addr, nvram_size);
54 return;
55 }
56 spin_lock_irqsave(&nvram_lock, flags);
57 nvram_buf[0] = val;
Paul Mackerras19fa17e2005-11-02 15:04:26 +110058 if ((rtas_call(rtas_token("nvram-store"), 3, 2, &done, addr,
59 __pa(nvram_buf), 1) != 0) || 1 != done)
Olaf Hering35e95e62005-10-28 17:46:19 -070060 printk(KERN_DEBUG "rtas IO error storing 0x%02x at %d", val, addr);
61 spin_unlock_irqrestore(&nvram_lock, flags);
62}
63
64void __init chrp_nvram_init(void)
65{
66 struct device_node *nvram;
67 unsigned int *nbytes_p, proplen;
68
69 nvram = of_find_node_by_type(NULL, "nvram");
70 if (nvram == NULL)
71 return;
72
73 nbytes_p = (unsigned int *)get_property(nvram, "#bytes", &proplen);
74 if (nbytes_p == NULL || proplen != sizeof(unsigned int))
75 return;
76
77 nvram_size = *nbytes_p;
78
79 printk(KERN_INFO "CHRP nvram contains %u bytes\n", nvram_size);
80 of_node_put(nvram);
81
82 ppc_md.nvram_read_val = chrp_nvram_read;
83 ppc_md.nvram_write_val = chrp_nvram_write;
84
85 return;
86}