4 * (C) Copyright IBM Corp. 2005
6 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/spinlock.h>
27 #include <linux/types.h>
29 #include <asm/machdep.h>
30 #include <asm/nvram.h>
33 static void __iomem *bpa_nvram_start;
34 static long bpa_nvram_len;
35 static spinlock_t bpa_nvram_lock = SPIN_LOCK_UNLOCKED;
37 static ssize_t bpa_nvram_read(char *buf, size_t count, loff_t *index)
41 if (*index >= bpa_nvram_len)
43 if (*index + count > bpa_nvram_len)
44 count = bpa_nvram_len - *index;
46 spin_lock_irqsave(&bpa_nvram_lock, flags);
48 memcpy_fromio(buf, bpa_nvram_start + *index, count);
50 spin_unlock_irqrestore(&bpa_nvram_lock, flags);
56 static ssize_t bpa_nvram_write(char *buf, size_t count, loff_t *index)
60 if (*index >= bpa_nvram_len)
62 if (*index + count > bpa_nvram_len)
63 count = bpa_nvram_len - *index;
65 spin_lock_irqsave(&bpa_nvram_lock, flags);
67 memcpy_toio(bpa_nvram_start + *index, buf, count);
69 spin_unlock_irqrestore(&bpa_nvram_lock, flags);
75 static ssize_t bpa_nvram_get_size(void)
80 int __init bpa_nvram_init(void)
82 struct device_node *nvram_node;
83 unsigned long *buffer;
85 unsigned long nvram_addr;
89 nvram_node = of_find_node_by_type(NULL, "nvram");
94 buffer = (unsigned long *)get_property(nvram_node, "reg", &proplen);
95 if (proplen != 2*sizeof(unsigned long))
99 nvram_addr = buffer[0];
100 bpa_nvram_len = buffer[1];
101 if ( (!bpa_nvram_len) || (!nvram_addr) )
104 bpa_nvram_start = ioremap(nvram_addr, bpa_nvram_len);
105 if (!bpa_nvram_start)
108 printk(KERN_INFO "BPA NVRAM, %luk mapped to %p\n",
109 bpa_nvram_len >> 10, bpa_nvram_start);
111 ppc_md.nvram_read = bpa_nvram_read;
112 ppc_md.nvram_write = bpa_nvram_write;
113 ppc_md.nvram_size = bpa_nvram_get_size;
116 of_node_put(nvram_node);