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
*mmio_nvram_start
;
34 static long mmio_nvram_len
;
35 static DEFINE_SPINLOCK(mmio_nvram_lock
);
37 static ssize_t
mmio_nvram_read(char *buf
, size_t count
, loff_t
*index
)
41 if (*index
>= mmio_nvram_len
)
43 if (*index
+ count
> mmio_nvram_len
)
44 count
= mmio_nvram_len
- *index
;
46 spin_lock_irqsave(&mmio_nvram_lock
, flags
);
48 memcpy_fromio(buf
, mmio_nvram_start
+ *index
, count
);
50 spin_unlock_irqrestore(&mmio_nvram_lock
, flags
);
56 static unsigned char mmio_nvram_read_val(int addr
)
61 if (addr
>= mmio_nvram_len
)
64 spin_lock_irqsave(&mmio_nvram_lock
, flags
);
66 val
= ioread8(mmio_nvram_start
+ addr
);
68 spin_unlock_irqrestore(&mmio_nvram_lock
, flags
);
73 static ssize_t
mmio_nvram_write(char *buf
, size_t count
, loff_t
*index
)
77 if (*index
>= mmio_nvram_len
)
79 if (*index
+ count
> mmio_nvram_len
)
80 count
= mmio_nvram_len
- *index
;
82 spin_lock_irqsave(&mmio_nvram_lock
, flags
);
84 memcpy_toio(mmio_nvram_start
+ *index
, buf
, count
);
86 spin_unlock_irqrestore(&mmio_nvram_lock
, flags
);
92 void mmio_nvram_write_val(int addr
, unsigned char val
)
96 if (addr
< mmio_nvram_len
) {
97 spin_lock_irqsave(&mmio_nvram_lock
, flags
);
99 iowrite8(val
, mmio_nvram_start
+ addr
);
101 spin_unlock_irqrestore(&mmio_nvram_lock
, flags
);
105 static ssize_t
mmio_nvram_get_size(void)
107 return mmio_nvram_len
;
110 int __init
mmio_nvram_init(void)
112 struct device_node
*nvram_node
;
113 unsigned long nvram_addr
;
117 nvram_node
= of_find_node_by_type(NULL
, "nvram");
119 nvram_node
= of_find_compatible_node(NULL
, NULL
, "nvram");
121 printk(KERN_WARNING
"nvram: no node found in device-tree\n");
125 ret
= of_address_to_resource(nvram_node
, 0, &r
);
127 printk(KERN_WARNING
"nvram: failed to get address (err %d)\n",
131 nvram_addr
= r
.start
;
132 mmio_nvram_len
= resource_size(&r
);
133 if ( (!mmio_nvram_len
) || (!nvram_addr
) ) {
134 printk(KERN_WARNING
"nvram: address or length is 0\n");
139 mmio_nvram_start
= ioremap(nvram_addr
, mmio_nvram_len
);
140 if (!mmio_nvram_start
) {
141 printk(KERN_WARNING
"nvram: failed to ioremap\n");
146 printk(KERN_INFO
"mmio NVRAM, %luk at 0x%lx mapped to %p\n",
147 mmio_nvram_len
>> 10, nvram_addr
, mmio_nvram_start
);
149 ppc_md
.nvram_read_val
= mmio_nvram_read_val
;
150 ppc_md
.nvram_write_val
= mmio_nvram_write_val
;
151 ppc_md
.nvram_read
= mmio_nvram_read
;
152 ppc_md
.nvram_write
= mmio_nvram_write
;
153 ppc_md
.nvram_size
= mmio_nvram_get_size
;
156 of_node_put(nvram_node
);