1*de6cc651SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2edf03c1eSArnd Bergmann /* 3edf03c1eSArnd Bergmann * memory mapped NVRAM 4edf03c1eSArnd Bergmann * 5edf03c1eSArnd Bergmann * (C) Copyright IBM Corp. 2005 6edf03c1eSArnd Bergmann * 7edf03c1eSArnd Bergmann * Authors : Utz Bacher <utz.bacher@de.ibm.com> 8edf03c1eSArnd Bergmann */ 9edf03c1eSArnd Bergmann 10edf03c1eSArnd Bergmann #include <linux/fs.h> 11edf03c1eSArnd Bergmann #include <linux/init.h> 12edf03c1eSArnd Bergmann #include <linux/kernel.h> 13edf03c1eSArnd Bergmann #include <linux/spinlock.h> 14edf03c1eSArnd Bergmann #include <linux/types.h> 15edf03c1eSArnd Bergmann 16edf03c1eSArnd Bergmann #include <asm/machdep.h> 17edf03c1eSArnd Bergmann #include <asm/nvram.h> 18edf03c1eSArnd Bergmann #include <asm/prom.h> 19edf03c1eSArnd Bergmann 20edf03c1eSArnd Bergmann static void __iomem *mmio_nvram_start; 21edf03c1eSArnd Bergmann static long mmio_nvram_len; 2234af946aSIngo Molnar static DEFINE_SPINLOCK(mmio_nvram_lock); 23edf03c1eSArnd Bergmann 24edf03c1eSArnd Bergmann static ssize_t mmio_nvram_read(char *buf, size_t count, loff_t *index) 25edf03c1eSArnd Bergmann { 26edf03c1eSArnd Bergmann unsigned long flags; 27edf03c1eSArnd Bergmann 28edf03c1eSArnd Bergmann if (*index >= mmio_nvram_len) 29edf03c1eSArnd Bergmann return 0; 30edf03c1eSArnd Bergmann if (*index + count > mmio_nvram_len) 31edf03c1eSArnd Bergmann count = mmio_nvram_len - *index; 32edf03c1eSArnd Bergmann 33edf03c1eSArnd Bergmann spin_lock_irqsave(&mmio_nvram_lock, flags); 34edf03c1eSArnd Bergmann 35edf03c1eSArnd Bergmann memcpy_fromio(buf, mmio_nvram_start + *index, count); 36edf03c1eSArnd Bergmann 37edf03c1eSArnd Bergmann spin_unlock_irqrestore(&mmio_nvram_lock, flags); 38edf03c1eSArnd Bergmann 39edf03c1eSArnd Bergmann *index += count; 40edf03c1eSArnd Bergmann return count; 41edf03c1eSArnd Bergmann } 42edf03c1eSArnd Bergmann 43690a2d07SMartyn Welch static unsigned char mmio_nvram_read_val(int addr) 44690a2d07SMartyn Welch { 45690a2d07SMartyn Welch unsigned long flags; 46690a2d07SMartyn Welch unsigned char val; 47690a2d07SMartyn Welch 48690a2d07SMartyn Welch if (addr >= mmio_nvram_len) 49690a2d07SMartyn Welch return 0xff; 50690a2d07SMartyn Welch 51690a2d07SMartyn Welch spin_lock_irqsave(&mmio_nvram_lock, flags); 52690a2d07SMartyn Welch 53690a2d07SMartyn Welch val = ioread8(mmio_nvram_start + addr); 54690a2d07SMartyn Welch 55690a2d07SMartyn Welch spin_unlock_irqrestore(&mmio_nvram_lock, flags); 56690a2d07SMartyn Welch 57690a2d07SMartyn Welch return val; 58690a2d07SMartyn Welch } 59690a2d07SMartyn Welch 60edf03c1eSArnd Bergmann static ssize_t mmio_nvram_write(char *buf, size_t count, loff_t *index) 61edf03c1eSArnd Bergmann { 62edf03c1eSArnd Bergmann unsigned long flags; 63edf03c1eSArnd Bergmann 64edf03c1eSArnd Bergmann if (*index >= mmio_nvram_len) 65edf03c1eSArnd Bergmann return 0; 66edf03c1eSArnd Bergmann if (*index + count > mmio_nvram_len) 67edf03c1eSArnd Bergmann count = mmio_nvram_len - *index; 68edf03c1eSArnd Bergmann 69edf03c1eSArnd Bergmann spin_lock_irqsave(&mmio_nvram_lock, flags); 70edf03c1eSArnd Bergmann 71edf03c1eSArnd Bergmann memcpy_toio(mmio_nvram_start + *index, buf, count); 72edf03c1eSArnd Bergmann 73edf03c1eSArnd Bergmann spin_unlock_irqrestore(&mmio_nvram_lock, flags); 74edf03c1eSArnd Bergmann 75edf03c1eSArnd Bergmann *index += count; 76edf03c1eSArnd Bergmann return count; 77edf03c1eSArnd Bergmann } 78edf03c1eSArnd Bergmann 797c98bd72SDaniel Axtens static void mmio_nvram_write_val(int addr, unsigned char val) 80690a2d07SMartyn Welch { 81690a2d07SMartyn Welch unsigned long flags; 82690a2d07SMartyn Welch 83690a2d07SMartyn Welch if (addr < mmio_nvram_len) { 84690a2d07SMartyn Welch spin_lock_irqsave(&mmio_nvram_lock, flags); 85690a2d07SMartyn Welch 86690a2d07SMartyn Welch iowrite8(val, mmio_nvram_start + addr); 87690a2d07SMartyn Welch 88690a2d07SMartyn Welch spin_unlock_irqrestore(&mmio_nvram_lock, flags); 89690a2d07SMartyn Welch } 90690a2d07SMartyn Welch } 91690a2d07SMartyn Welch 92edf03c1eSArnd Bergmann static ssize_t mmio_nvram_get_size(void) 93edf03c1eSArnd Bergmann { 94edf03c1eSArnd Bergmann return mmio_nvram_len; 95edf03c1eSArnd Bergmann } 96edf03c1eSArnd Bergmann 97edf03c1eSArnd Bergmann int __init mmio_nvram_init(void) 98edf03c1eSArnd Bergmann { 99edf03c1eSArnd Bergmann struct device_node *nvram_node; 100edf03c1eSArnd Bergmann unsigned long nvram_addr; 1016984ee79SBenjamin Herrenschmidt struct resource r; 102edf03c1eSArnd Bergmann int ret; 103edf03c1eSArnd Bergmann 104edf03c1eSArnd Bergmann nvram_node = of_find_node_by_type(NULL, "nvram"); 105411e689dSBenjamin Herrenschmidt if (!nvram_node) 106411e689dSBenjamin Herrenschmidt nvram_node = of_find_compatible_node(NULL, NULL, "nvram"); 1076984ee79SBenjamin Herrenschmidt if (!nvram_node) { 1086984ee79SBenjamin Herrenschmidt printk(KERN_WARNING "nvram: no node found in device-tree\n"); 1096984ee79SBenjamin Herrenschmidt return -ENODEV; 1106984ee79SBenjamin Herrenschmidt } 111edf03c1eSArnd Bergmann 1126984ee79SBenjamin Herrenschmidt ret = of_address_to_resource(nvram_node, 0, &r); 1136984ee79SBenjamin Herrenschmidt if (ret) { 1146984ee79SBenjamin Herrenschmidt printk(KERN_WARNING "nvram: failed to get address (err %d)\n", 1156984ee79SBenjamin Herrenschmidt ret); 1166984ee79SBenjamin Herrenschmidt goto out; 1176984ee79SBenjamin Herrenschmidt } 1186984ee79SBenjamin Herrenschmidt nvram_addr = r.start; 11928f65c11SJoe Perches mmio_nvram_len = resource_size(&r); 1206984ee79SBenjamin Herrenschmidt if ( (!mmio_nvram_len) || (!nvram_addr) ) { 12100d70419Sjoe@perches.com printk(KERN_WARNING "nvram: address or length is 0\n"); 122edf03c1eSArnd Bergmann ret = -EIO; 123edf03c1eSArnd Bergmann goto out; 1246984ee79SBenjamin Herrenschmidt } 125edf03c1eSArnd Bergmann 126edf03c1eSArnd Bergmann mmio_nvram_start = ioremap(nvram_addr, mmio_nvram_len); 1276984ee79SBenjamin Herrenschmidt if (!mmio_nvram_start) { 1286984ee79SBenjamin Herrenschmidt printk(KERN_WARNING "nvram: failed to ioremap\n"); 1296984ee79SBenjamin Herrenschmidt ret = -ENOMEM; 130edf03c1eSArnd Bergmann goto out; 1316984ee79SBenjamin Herrenschmidt } 132edf03c1eSArnd Bergmann 1336984ee79SBenjamin Herrenschmidt printk(KERN_INFO "mmio NVRAM, %luk at 0x%lx mapped to %p\n", 1346984ee79SBenjamin Herrenschmidt mmio_nvram_len >> 10, nvram_addr, mmio_nvram_start); 135edf03c1eSArnd Bergmann 136690a2d07SMartyn Welch ppc_md.nvram_read_val = mmio_nvram_read_val; 137690a2d07SMartyn Welch ppc_md.nvram_write_val = mmio_nvram_write_val; 138edf03c1eSArnd Bergmann ppc_md.nvram_read = mmio_nvram_read; 139edf03c1eSArnd Bergmann ppc_md.nvram_write = mmio_nvram_write; 140edf03c1eSArnd Bergmann ppc_md.nvram_size = mmio_nvram_get_size; 141edf03c1eSArnd Bergmann 142edf03c1eSArnd Bergmann out: 143edf03c1eSArnd Bergmann of_node_put(nvram_node); 144edf03c1eSArnd Bergmann return ret; 145edf03c1eSArnd Bergmann } 146