blob: 8efd4244701c2eb1d0a73b07d9e21888ae066e94 [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>
Paul Mackerras39838292005-11-02 19:58:12 +110020#include <asm/rtas.h>
Olaf Hering35e95e62005-10-28 17:46:19 -070021#include "chrp.h"
22
23static unsigned int nvram_size;
24static unsigned char nvram_buf[4];
25static DEFINE_SPINLOCK(nvram_lock);
26
27static unsigned char chrp_nvram_read(int addr)
28{
Paul Mackerras39838292005-11-02 19:58:12 +110029 unsigned int done;
30 unsigned long flags;
Olaf Hering35e95e62005-10-28 17:46:19 -070031 unsigned char ret;
32
33 if (addr >= nvram_size) {
34 printk(KERN_DEBUG "%s: read addr %d > nvram_size %u\n",
35 current->comm, addr, nvram_size);
36 return 0xff;
37 }
38 spin_lock_irqsave(&nvram_lock, flags);
Paul Mackerras19fa17e2005-11-02 15:04:26 +110039 if ((rtas_call(rtas_token("nvram-fetch"), 3, 2, &done, addr,
40 __pa(nvram_buf), 1) != 0) || 1 != done)
Olaf Hering35e95e62005-10-28 17:46:19 -070041 ret = 0xff;
42 else
43 ret = nvram_buf[0];
44 spin_unlock_irqrestore(&nvram_lock, flags);
45
46 return ret;
47}
48
49static void chrp_nvram_write(int addr, unsigned char val)
50{
Paul Mackerras39838292005-11-02 19:58:12 +110051 unsigned int done;
52 unsigned long flags;
Olaf Hering35e95e62005-10-28 17:46:19 -070053
54 if (addr >= nvram_size) {
55 printk(KERN_DEBUG "%s: write addr %d > nvram_size %u\n",
56 current->comm, addr, nvram_size);
57 return;
58 }
59 spin_lock_irqsave(&nvram_lock, flags);
60 nvram_buf[0] = val;
Paul Mackerras19fa17e2005-11-02 15:04:26 +110061 if ((rtas_call(rtas_token("nvram-store"), 3, 2, &done, addr,
62 __pa(nvram_buf), 1) != 0) || 1 != done)
Olaf Hering35e95e62005-10-28 17:46:19 -070063 printk(KERN_DEBUG "rtas IO error storing 0x%02x at %d", val, addr);
64 spin_unlock_irqrestore(&nvram_lock, flags);
65}
66
67void __init chrp_nvram_init(void)
68{
69 struct device_node *nvram;
Jeremy Kerrae6b4102006-07-12 15:40:05 +100070 const unsigned int *nbytes_p;
71 unsigned int proplen;
Olaf Hering35e95e62005-10-28 17:46:19 -070072
73 nvram = of_find_node_by_type(NULL, "nvram");
74 if (nvram == NULL)
75 return;
76
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100077 nbytes_p = of_get_property(nvram, "#bytes", &proplen);
Olaf Hering35e95e62005-10-28 17:46:19 -070078 if (nbytes_p == NULL || proplen != sizeof(unsigned int))
79 return;
80
81 nvram_size = *nbytes_p;
82
83 printk(KERN_INFO "CHRP nvram contains %u bytes\n", nvram_size);
84 of_node_put(nvram);
85
86 ppc_md.nvram_read_val = chrp_nvram_read;
87 ppc_md.nvram_write_val = chrp_nvram_write;
88
89 return;
90}