blob: 628f9b759c8486f0aea5d2af6d0155e1c3c7d003 [file] [log] [blame]
Thomas Gleixnerde6cc652019-05-27 08:55:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Utz Bacher5f5b4e62005-06-23 09:43:31 +10002/*
Arnd Bergmannf3f66f52005-10-31 20:08:37 -05003 * memory mapped NVRAM
Utz Bacher5f5b4e62005-06-23 09:43:31 +10004 *
5 * (C) Copyright IBM Corp. 2005
6 *
7 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
Utz Bacher5f5b4e62005-06-23 09:43:31 +10008 */
9
10#include <linux/fs.h>
11#include <linux/init.h>
12#include <linux/kernel.h>
13#include <linux/spinlock.h>
14#include <linux/types.h>
15
16#include <asm/machdep.h>
17#include <asm/nvram.h>
18#include <asm/prom.h>
19
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050020static void __iomem *mmio_nvram_start;
21static long mmio_nvram_len;
Ingo Molnar34af9462006-06-27 02:53:55 -070022static DEFINE_SPINLOCK(mmio_nvram_lock);
Utz Bacher5f5b4e62005-06-23 09:43:31 +100023
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050024static ssize_t mmio_nvram_read(char *buf, size_t count, loff_t *index)
Utz Bacher5f5b4e62005-06-23 09:43:31 +100025{
26 unsigned long flags;
27
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050028 if (*index >= mmio_nvram_len)
Utz Bacher5f5b4e62005-06-23 09:43:31 +100029 return 0;
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050030 if (*index + count > mmio_nvram_len)
31 count = mmio_nvram_len - *index;
Utz Bacher5f5b4e62005-06-23 09:43:31 +100032
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050033 spin_lock_irqsave(&mmio_nvram_lock, flags);
Utz Bacher5f5b4e62005-06-23 09:43:31 +100034
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050035 memcpy_fromio(buf, mmio_nvram_start + *index, count);
Utz Bacher5f5b4e62005-06-23 09:43:31 +100036
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050037 spin_unlock_irqrestore(&mmio_nvram_lock, flags);
Utz Bacher5f5b4e62005-06-23 09:43:31 +100038
39 *index += count;
40 return count;
41}
42
Martyn Welch690a2d02009-07-02 06:12:18 +000043static unsigned char mmio_nvram_read_val(int addr)
44{
45 unsigned long flags;
46 unsigned char val;
47
48 if (addr >= mmio_nvram_len)
49 return 0xff;
50
51 spin_lock_irqsave(&mmio_nvram_lock, flags);
52
53 val = ioread8(mmio_nvram_start + addr);
54
55 spin_unlock_irqrestore(&mmio_nvram_lock, flags);
56
57 return val;
58}
59
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050060static ssize_t mmio_nvram_write(char *buf, size_t count, loff_t *index)
Utz Bacher5f5b4e62005-06-23 09:43:31 +100061{
62 unsigned long flags;
63
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050064 if (*index >= mmio_nvram_len)
Utz Bacher5f5b4e62005-06-23 09:43:31 +100065 return 0;
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050066 if (*index + count > mmio_nvram_len)
67 count = mmio_nvram_len - *index;
Utz Bacher5f5b4e62005-06-23 09:43:31 +100068
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050069 spin_lock_irqsave(&mmio_nvram_lock, flags);
Utz Bacher5f5b4e62005-06-23 09:43:31 +100070
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050071 memcpy_toio(mmio_nvram_start + *index, buf, count);
Utz Bacher5f5b4e62005-06-23 09:43:31 +100072
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050073 spin_unlock_irqrestore(&mmio_nvram_lock, flags);
Utz Bacher5f5b4e62005-06-23 09:43:31 +100074
75 *index += count;
76 return count;
77}
78
Daniel Axtens7c98bd72016-09-06 15:32:40 +100079static void mmio_nvram_write_val(int addr, unsigned char val)
Martyn Welch690a2d02009-07-02 06:12:18 +000080{
81 unsigned long flags;
82
83 if (addr < mmio_nvram_len) {
84 spin_lock_irqsave(&mmio_nvram_lock, flags);
85
86 iowrite8(val, mmio_nvram_start + addr);
87
88 spin_unlock_irqrestore(&mmio_nvram_lock, flags);
89 }
90}
91
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050092static ssize_t mmio_nvram_get_size(void)
Utz Bacher5f5b4e62005-06-23 09:43:31 +100093{
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050094 return mmio_nvram_len;
Utz Bacher5f5b4e62005-06-23 09:43:31 +100095}
96
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050097int __init mmio_nvram_init(void)
Utz Bacher5f5b4e62005-06-23 09:43:31 +100098{
99 struct device_node *nvram_node;
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000100 unsigned long nvram_addr;
Benjamin Herrenschmidt6984ee72007-01-11 16:08:41 +1100101 struct resource r;
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000102 int ret;
103
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000104 nvram_node = of_find_node_by_type(NULL, "nvram");
Benjamin Herrenschmidt411e6892011-04-14 22:32:00 +0000105 if (!nvram_node)
106 nvram_node = of_find_compatible_node(NULL, NULL, "nvram");
Benjamin Herrenschmidt6984ee72007-01-11 16:08:41 +1100107 if (!nvram_node) {
108 printk(KERN_WARNING "nvram: no node found in device-tree\n");
109 return -ENODEV;
110 }
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000111
Benjamin Herrenschmidt6984ee72007-01-11 16:08:41 +1100112 ret = of_address_to_resource(nvram_node, 0, &r);
113 if (ret) {
114 printk(KERN_WARNING "nvram: failed to get address (err %d)\n",
115 ret);
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000116 goto out;
Benjamin Herrenschmidt6984ee72007-01-11 16:08:41 +1100117 }
118 nvram_addr = r.start;
Joe Perches28f65c112011-06-09 09:13:32 -0700119 mmio_nvram_len = resource_size(&r);
Benjamin Herrenschmidt6984ee72007-01-11 16:08:41 +1100120 if ( (!mmio_nvram_len) || (!nvram_addr) ) {
joe@perches.com00d70412007-12-18 06:30:12 +1100121 printk(KERN_WARNING "nvram: address or length is 0\n");
Benjamin Herrenschmidt6984ee72007-01-11 16:08:41 +1100122 ret = -EIO;
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000123 goto out;
Benjamin Herrenschmidt6984ee72007-01-11 16:08:41 +1100124 }
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000125
Arnd Bergmannf3f66f52005-10-31 20:08:37 -0500126 mmio_nvram_start = ioremap(nvram_addr, mmio_nvram_len);
Benjamin Herrenschmidt6984ee72007-01-11 16:08:41 +1100127 if (!mmio_nvram_start) {
128 printk(KERN_WARNING "nvram: failed to ioremap\n");
129 ret = -ENOMEM;
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000130 goto out;
Benjamin Herrenschmidt6984ee72007-01-11 16:08:41 +1100131 }
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000132
Benjamin Herrenschmidt6984ee72007-01-11 16:08:41 +1100133 printk(KERN_INFO "mmio NVRAM, %luk at 0x%lx mapped to %p\n",
134 mmio_nvram_len >> 10, nvram_addr, mmio_nvram_start);
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000135
Martyn Welch690a2d02009-07-02 06:12:18 +0000136 ppc_md.nvram_read_val = mmio_nvram_read_val;
137 ppc_md.nvram_write_val = mmio_nvram_write_val;
Arnd Bergmannf3f66f52005-10-31 20:08:37 -0500138 ppc_md.nvram_read = mmio_nvram_read;
139 ppc_md.nvram_write = mmio_nvram_write;
140 ppc_md.nvram_size = mmio_nvram_get_size;
Utz Bacher5f5b4e62005-06-23 09:43:31 +1000141
142out:
143 of_node_put(nvram_node);
144 return ret;
145}