blob: 207324209065341df70115612c98b3b902bd4c3a [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
Martyn Welch690a2d02009-07-02 06:12:18 +000056static unsigned char mmio_nvram_read_val(int addr)
57{
58 unsigned long flags;
59 unsigned char val;
60
61 if (addr >= mmio_nvram_len)
62 return 0xff;
63
64 spin_lock_irqsave(&mmio_nvram_lock, flags);
65
66 val = ioread8(mmio_nvram_start + addr);
67
68 spin_unlock_irqrestore(&mmio_nvram_lock, flags);
69
70 return val;
71}
72
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050073static ssize_t mmio_nvram_write(char *buf, size_t count, loff_t *index)
Utz Bacher5f5b4e62005-06-23 09:43:31 +100074{
75 unsigned long flags;
76
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050077 if (*index >= mmio_nvram_len)
Utz Bacher5f5b4e62005-06-23 09:43:31 +100078 return 0;
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050079 if (*index + count > mmio_nvram_len)
80 count = mmio_nvram_len - *index;
Utz Bacher5f5b4e62005-06-23 09:43:31 +100081
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050082 spin_lock_irqsave(&mmio_nvram_lock, flags);
Utz Bacher5f5b4e62005-06-23 09:43:31 +100083
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050084 memcpy_toio(mmio_nvram_start + *index, buf, count);
Utz Bacher5f5b4e62005-06-23 09:43:31 +100085
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050086 spin_unlock_irqrestore(&mmio_nvram_lock, flags);
Utz Bacher5f5b4e62005-06-23 09:43:31 +100087
88 *index += count;
89 return count;
90}
91
Martyn Welch690a2d02009-07-02 06:12:18 +000092void mmio_nvram_write_val(int addr, unsigned char val)
93{
94 unsigned long flags;
95
96 if (addr < mmio_nvram_len) {
97 spin_lock_irqsave(&mmio_nvram_lock, flags);
98
99 iowrite8(val, mmio_nvram_start + addr);
100
101 spin_unlock_irqrestore(&mmio_nvram_lock, flags);
102 }
103}
104
Arnd Bergmannf3f66f52005-10-31 20:08:37 -0500105static ssize_t mmio_nvram_get_size(void)
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000106{
Arnd Bergmannf3f66f52005-10-31 20:08:37 -0500107 return mmio_nvram_len;
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000108}
109
Arnd Bergmannf3f66f52005-10-31 20:08:37 -0500110int __init mmio_nvram_init(void)
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000111{
112 struct device_node *nvram_node;
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000113 unsigned long nvram_addr;
Benjamin Herrenschmidt6984ee72007-01-11 16:08:41 +1100114 struct resource r;
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000115 int ret;
116
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000117 nvram_node = of_find_node_by_type(NULL, "nvram");
Benjamin Herrenschmidt6984ee72007-01-11 16:08:41 +1100118 if (!nvram_node) {
119 printk(KERN_WARNING "nvram: no node found in device-tree\n");
120 return -ENODEV;
121 }
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000122
Benjamin Herrenschmidt6984ee72007-01-11 16:08:41 +1100123 ret = of_address_to_resource(nvram_node, 0, &r);
124 if (ret) {
125 printk(KERN_WARNING "nvram: failed to get address (err %d)\n",
126 ret);
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000127 goto out;
Benjamin Herrenschmidt6984ee72007-01-11 16:08:41 +1100128 }
129 nvram_addr = r.start;
130 mmio_nvram_len = r.end - r.start + 1;
131 if ( (!mmio_nvram_len) || (!nvram_addr) ) {
joe@perches.com00d70412007-12-18 06:30:12 +1100132 printk(KERN_WARNING "nvram: address or length is 0\n");
Benjamin Herrenschmidt6984ee72007-01-11 16:08:41 +1100133 ret = -EIO;
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000134 goto out;
Benjamin Herrenschmidt6984ee72007-01-11 16:08:41 +1100135 }
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000136
Arnd Bergmannf3f66f52005-10-31 20:08:37 -0500137 mmio_nvram_start = ioremap(nvram_addr, mmio_nvram_len);
Benjamin Herrenschmidt6984ee72007-01-11 16:08:41 +1100138 if (!mmio_nvram_start) {
139 printk(KERN_WARNING "nvram: failed to ioremap\n");
140 ret = -ENOMEM;
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000141 goto out;
Benjamin Herrenschmidt6984ee72007-01-11 16:08:41 +1100142 }
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000143
Benjamin Herrenschmidt6984ee72007-01-11 16:08:41 +1100144 printk(KERN_INFO "mmio NVRAM, %luk at 0x%lx mapped to %p\n",
145 mmio_nvram_len >> 10, nvram_addr, mmio_nvram_start);
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000146
Martyn Welch690a2d02009-07-02 06:12:18 +0000147 ppc_md.nvram_read_val = mmio_nvram_read_val;
148 ppc_md.nvram_write_val = mmio_nvram_write_val;
Arnd Bergmannf3f66f52005-10-31 20:08:37 -0500149 ppc_md.nvram_read = mmio_nvram_read;
150 ppc_md.nvram_write = mmio_nvram_write;
151 ppc_md.nvram_size = mmio_nvram_get_size;
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000152
153out:
154 of_node_put(nvram_node);
155 return ret;
156}