blob: ff23f5a4d4b91887e3de34060b62e86dc3a9b158 [file] [log] [blame]
Utz Bacher5f5b4e62005-06-23 09:43:31 +10001/*
Arnd Bergmannf3f66f52005-10-31 20:08:37 -05002 * memory mapped NVRAM
Utz Bacher5f5b4e62005-06-23 09:43:31 +10003 *
4 * (C) Copyright IBM Corp. 2005
5 *
6 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
7 *
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)
11 * any later version.
12 *
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.
17 *
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.
21 */
22
23#include <linux/fs.h>
24#include <linux/init.h>
25#include <linux/kernel.h>
26#include <linux/spinlock.h>
27#include <linux/types.h>
28
29#include <asm/machdep.h>
30#include <asm/nvram.h>
31#include <asm/prom.h>
32
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050033static void __iomem *mmio_nvram_start;
34static long mmio_nvram_len;
Ingo Molnar34af9462006-06-27 02:53:55 -070035static DEFINE_SPINLOCK(mmio_nvram_lock);
Utz Bacher5f5b4e62005-06-23 09:43:31 +100036
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050037static ssize_t mmio_nvram_read(char *buf, size_t count, loff_t *index)
Utz Bacher5f5b4e62005-06-23 09:43:31 +100038{
39 unsigned long flags;
40
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050041 if (*index >= mmio_nvram_len)
Utz Bacher5f5b4e62005-06-23 09:43:31 +100042 return 0;
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050043 if (*index + count > mmio_nvram_len)
44 count = mmio_nvram_len - *index;
Utz Bacher5f5b4e62005-06-23 09:43:31 +100045
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050046 spin_lock_irqsave(&mmio_nvram_lock, flags);
Utz Bacher5f5b4e62005-06-23 09:43:31 +100047
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050048 memcpy_fromio(buf, mmio_nvram_start + *index, count);
Utz Bacher5f5b4e62005-06-23 09:43:31 +100049
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050050 spin_unlock_irqrestore(&mmio_nvram_lock, flags);
Utz Bacher5f5b4e62005-06-23 09:43:31 +100051
52 *index += count;
53 return count;
54}
55
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050056static ssize_t mmio_nvram_write(char *buf, size_t count, loff_t *index)
Utz Bacher5f5b4e62005-06-23 09:43:31 +100057{
58 unsigned long flags;
59
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050060 if (*index >= mmio_nvram_len)
Utz Bacher5f5b4e62005-06-23 09:43:31 +100061 return 0;
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050062 if (*index + count > mmio_nvram_len)
63 count = mmio_nvram_len - *index;
Utz Bacher5f5b4e62005-06-23 09:43:31 +100064
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050065 spin_lock_irqsave(&mmio_nvram_lock, flags);
Utz Bacher5f5b4e62005-06-23 09:43:31 +100066
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050067 memcpy_toio(mmio_nvram_start + *index, buf, count);
Utz Bacher5f5b4e62005-06-23 09:43:31 +100068
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050069 spin_unlock_irqrestore(&mmio_nvram_lock, flags);
Utz Bacher5f5b4e62005-06-23 09:43:31 +100070
71 *index += count;
72 return count;
73}
74
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050075static ssize_t mmio_nvram_get_size(void)
Utz Bacher5f5b4e62005-06-23 09:43:31 +100076{
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050077 return mmio_nvram_len;
Utz Bacher5f5b4e62005-06-23 09:43:31 +100078}
79
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050080int __init mmio_nvram_init(void)
Utz Bacher5f5b4e62005-06-23 09:43:31 +100081{
82 struct device_node *nvram_node;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +100083 const unsigned long *buffer;
Utz Bacher5f5b4e62005-06-23 09:43:31 +100084 int proplen;
85 unsigned long nvram_addr;
86 int ret;
87
88 ret = -ENODEV;
89 nvram_node = of_find_node_by_type(NULL, "nvram");
90 if (!nvram_node)
91 goto out;
92
93 ret = -EIO;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +100094 buffer = get_property(nvram_node, "reg", &proplen);
Utz Bacher5f5b4e62005-06-23 09:43:31 +100095 if (proplen != 2*sizeof(unsigned long))
96 goto out;
97
98 ret = -ENODEV;
99 nvram_addr = buffer[0];
Arnd Bergmannf3f66f52005-10-31 20:08:37 -0500100 mmio_nvram_len = buffer[1];
101 if ( (!mmio_nvram_len) || (!nvram_addr) )
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000102 goto out;
103
Arnd Bergmannf3f66f52005-10-31 20:08:37 -0500104 mmio_nvram_start = ioremap(nvram_addr, mmio_nvram_len);
105 if (!mmio_nvram_start)
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000106 goto out;
107
Arnd Bergmannf3f66f52005-10-31 20:08:37 -0500108 printk(KERN_INFO "mmio NVRAM, %luk mapped to %p\n",
109 mmio_nvram_len >> 10, mmio_nvram_start);
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000110
Arnd Bergmannf3f66f52005-10-31 20:08:37 -0500111 ppc_md.nvram_read = mmio_nvram_read;
112 ppc_md.nvram_write = mmio_nvram_write;
113 ppc_md.nvram_size = mmio_nvram_get_size;
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000114
115out:
116 of_node_put(nvram_node);
117 return ret;
118}