1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * (C) Copyright IBM Corp. 2005
7 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/spinlock.h>
14 #include <linux/types.h>
16 #include <asm/machdep.h>
17 #include <asm/nvram.h>
20 static void __iomem
*mmio_nvram_start
;
21 static long mmio_nvram_len
;
22 static DEFINE_SPINLOCK(mmio_nvram_lock
);
24 static ssize_t
mmio_nvram_read(char *buf
, size_t count
, loff_t
*index
)
28 if (*index
>= mmio_nvram_len
)
30 if (*index
+ count
> mmio_nvram_len
)
31 count
= mmio_nvram_len
- *index
;
33 spin_lock_irqsave(&mmio_nvram_lock
, flags
);
35 memcpy_fromio(buf
, mmio_nvram_start
+ *index
, count
);
37 spin_unlock_irqrestore(&mmio_nvram_lock
, flags
);
43 static unsigned char mmio_nvram_read_val(int addr
)
48 if (addr
>= mmio_nvram_len
)
51 spin_lock_irqsave(&mmio_nvram_lock
, flags
);
53 val
= ioread8(mmio_nvram_start
+ addr
);
55 spin_unlock_irqrestore(&mmio_nvram_lock
, flags
);
60 static ssize_t
mmio_nvram_write(char *buf
, size_t count
, loff_t
*index
)
64 if (*index
>= mmio_nvram_len
)
66 if (*index
+ count
> mmio_nvram_len
)
67 count
= mmio_nvram_len
- *index
;
69 spin_lock_irqsave(&mmio_nvram_lock
, flags
);
71 memcpy_toio(mmio_nvram_start
+ *index
, buf
, count
);
73 spin_unlock_irqrestore(&mmio_nvram_lock
, flags
);
79 static void mmio_nvram_write_val(int addr
, unsigned char val
)
83 if (addr
< mmio_nvram_len
) {
84 spin_lock_irqsave(&mmio_nvram_lock
, flags
);
86 iowrite8(val
, mmio_nvram_start
+ addr
);
88 spin_unlock_irqrestore(&mmio_nvram_lock
, flags
);
92 static ssize_t
mmio_nvram_get_size(void)
94 return mmio_nvram_len
;
97 int __init
mmio_nvram_init(void)
99 struct device_node
*nvram_node
;
100 unsigned long nvram_addr
;
104 nvram_node
= of_find_node_by_type(NULL
, "nvram");
106 nvram_node
= of_find_compatible_node(NULL
, NULL
, "nvram");
108 printk(KERN_WARNING
"nvram: no node found in device-tree\n");
112 ret
= of_address_to_resource(nvram_node
, 0, &r
);
114 printk(KERN_WARNING
"nvram: failed to get address (err %d)\n",
118 nvram_addr
= r
.start
;
119 mmio_nvram_len
= resource_size(&r
);
120 if ( (!mmio_nvram_len
) || (!nvram_addr
) ) {
121 printk(KERN_WARNING
"nvram: address or length is 0\n");
126 mmio_nvram_start
= ioremap(nvram_addr
, mmio_nvram_len
);
127 if (!mmio_nvram_start
) {
128 printk(KERN_WARNING
"nvram: failed to ioremap\n");
133 printk(KERN_INFO
"mmio NVRAM, %luk at 0x%lx mapped to %p\n",
134 mmio_nvram_len
>> 10, nvram_addr
, mmio_nvram_start
);
136 ppc_md
.nvram_read_val
= mmio_nvram_read_val
;
137 ppc_md
.nvram_write_val
= mmio_nvram_write_val
;
138 ppc_md
.nvram_read
= mmio_nvram_read
;
139 ppc_md
.nvram_write
= mmio_nvram_write
;
140 ppc_md
.nvram_size
= mmio_nvram_get_size
;
143 of_node_put(nvram_node
);