1edf03c1eSArnd Bergmann /* 2edf03c1eSArnd Bergmann * memory mapped NVRAM 3edf03c1eSArnd Bergmann * 4edf03c1eSArnd Bergmann * (C) Copyright IBM Corp. 2005 5edf03c1eSArnd Bergmann * 6edf03c1eSArnd Bergmann * Authors : Utz Bacher <utz.bacher@de.ibm.com> 7edf03c1eSArnd Bergmann * 8edf03c1eSArnd Bergmann * This program is free software; you can redistribute it and/or modify 9edf03c1eSArnd Bergmann * it under the terms of the GNU General Public License as published by 10edf03c1eSArnd Bergmann * the Free Software Foundation; either version 2, or (at your option) 11edf03c1eSArnd Bergmann * any later version. 12edf03c1eSArnd Bergmann * 13edf03c1eSArnd Bergmann * This program is distributed in the hope that it will be useful, 14edf03c1eSArnd Bergmann * but WITHOUT ANY WARRANTY; without even the implied warranty of 15edf03c1eSArnd Bergmann * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16edf03c1eSArnd Bergmann * GNU General Public License for more details. 17edf03c1eSArnd Bergmann * 18edf03c1eSArnd Bergmann * You should have received a copy of the GNU General Public License 19edf03c1eSArnd Bergmann * along with this program; if not, write to the Free Software 20edf03c1eSArnd Bergmann * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 21edf03c1eSArnd Bergmann */ 22edf03c1eSArnd Bergmann 23edf03c1eSArnd Bergmann #include <linux/fs.h> 24edf03c1eSArnd Bergmann #include <linux/init.h> 25edf03c1eSArnd Bergmann #include <linux/kernel.h> 26edf03c1eSArnd Bergmann #include <linux/spinlock.h> 27edf03c1eSArnd Bergmann #include <linux/types.h> 28edf03c1eSArnd Bergmann 29edf03c1eSArnd Bergmann #include <asm/machdep.h> 30edf03c1eSArnd Bergmann #include <asm/nvram.h> 31edf03c1eSArnd Bergmann #include <asm/prom.h> 32edf03c1eSArnd Bergmann 33edf03c1eSArnd Bergmann static void __iomem *mmio_nvram_start; 34edf03c1eSArnd Bergmann static long mmio_nvram_len; 3534af946aSIngo Molnar static DEFINE_SPINLOCK(mmio_nvram_lock); 36edf03c1eSArnd Bergmann 37edf03c1eSArnd Bergmann static ssize_t mmio_nvram_read(char *buf, size_t count, loff_t *index) 38edf03c1eSArnd Bergmann { 39edf03c1eSArnd Bergmann unsigned long flags; 40edf03c1eSArnd Bergmann 41edf03c1eSArnd Bergmann if (*index >= mmio_nvram_len) 42edf03c1eSArnd Bergmann return 0; 43edf03c1eSArnd Bergmann if (*index + count > mmio_nvram_len) 44edf03c1eSArnd Bergmann count = mmio_nvram_len - *index; 45edf03c1eSArnd Bergmann 46edf03c1eSArnd Bergmann spin_lock_irqsave(&mmio_nvram_lock, flags); 47edf03c1eSArnd Bergmann 48edf03c1eSArnd Bergmann memcpy_fromio(buf, mmio_nvram_start + *index, count); 49edf03c1eSArnd Bergmann 50edf03c1eSArnd Bergmann spin_unlock_irqrestore(&mmio_nvram_lock, flags); 51edf03c1eSArnd Bergmann 52edf03c1eSArnd Bergmann *index += count; 53edf03c1eSArnd Bergmann return count; 54edf03c1eSArnd Bergmann } 55edf03c1eSArnd Bergmann 56690a2d07SMartyn Welch static unsigned char mmio_nvram_read_val(int addr) 57690a2d07SMartyn Welch { 58690a2d07SMartyn Welch unsigned long flags; 59690a2d07SMartyn Welch unsigned char val; 60690a2d07SMartyn Welch 61690a2d07SMartyn Welch if (addr >= mmio_nvram_len) 62690a2d07SMartyn Welch return 0xff; 63690a2d07SMartyn Welch 64690a2d07SMartyn Welch spin_lock_irqsave(&mmio_nvram_lock, flags); 65690a2d07SMartyn Welch 66690a2d07SMartyn Welch val = ioread8(mmio_nvram_start + addr); 67690a2d07SMartyn Welch 68690a2d07SMartyn Welch spin_unlock_irqrestore(&mmio_nvram_lock, flags); 69690a2d07SMartyn Welch 70690a2d07SMartyn Welch return val; 71690a2d07SMartyn Welch } 72690a2d07SMartyn Welch 73edf03c1eSArnd Bergmann static ssize_t mmio_nvram_write(char *buf, size_t count, loff_t *index) 74edf03c1eSArnd Bergmann { 75edf03c1eSArnd Bergmann unsigned long flags; 76edf03c1eSArnd Bergmann 77edf03c1eSArnd Bergmann if (*index >= mmio_nvram_len) 78edf03c1eSArnd Bergmann return 0; 79edf03c1eSArnd Bergmann if (*index + count > mmio_nvram_len) 80edf03c1eSArnd Bergmann count = mmio_nvram_len - *index; 81edf03c1eSArnd Bergmann 82edf03c1eSArnd Bergmann spin_lock_irqsave(&mmio_nvram_lock, flags); 83edf03c1eSArnd Bergmann 84edf03c1eSArnd Bergmann memcpy_toio(mmio_nvram_start + *index, buf, count); 85edf03c1eSArnd Bergmann 86edf03c1eSArnd Bergmann spin_unlock_irqrestore(&mmio_nvram_lock, flags); 87edf03c1eSArnd Bergmann 88edf03c1eSArnd Bergmann *index += count; 89edf03c1eSArnd Bergmann return count; 90edf03c1eSArnd Bergmann } 91edf03c1eSArnd Bergmann 92690a2d07SMartyn Welch void mmio_nvram_write_val(int addr, unsigned char val) 93690a2d07SMartyn Welch { 94690a2d07SMartyn Welch unsigned long flags; 95690a2d07SMartyn Welch 96690a2d07SMartyn Welch if (addr < mmio_nvram_len) { 97690a2d07SMartyn Welch spin_lock_irqsave(&mmio_nvram_lock, flags); 98690a2d07SMartyn Welch 99690a2d07SMartyn Welch iowrite8(val, mmio_nvram_start + addr); 100690a2d07SMartyn Welch 101690a2d07SMartyn Welch spin_unlock_irqrestore(&mmio_nvram_lock, flags); 102690a2d07SMartyn Welch } 103690a2d07SMartyn Welch } 104690a2d07SMartyn Welch 105edf03c1eSArnd Bergmann static ssize_t mmio_nvram_get_size(void) 106edf03c1eSArnd Bergmann { 107edf03c1eSArnd Bergmann return mmio_nvram_len; 108edf03c1eSArnd Bergmann } 109edf03c1eSArnd Bergmann 110edf03c1eSArnd Bergmann int __init mmio_nvram_init(void) 111edf03c1eSArnd Bergmann { 112edf03c1eSArnd Bergmann struct device_node *nvram_node; 113edf03c1eSArnd Bergmann unsigned long nvram_addr; 1146984ee79SBenjamin Herrenschmidt struct resource r; 115edf03c1eSArnd Bergmann int ret; 116edf03c1eSArnd Bergmann 117edf03c1eSArnd Bergmann nvram_node = of_find_node_by_type(NULL, "nvram"); 118*411e689dSBenjamin Herrenschmidt if (!nvram_node) 119*411e689dSBenjamin Herrenschmidt nvram_node = of_find_compatible_node(NULL, NULL, "nvram"); 1206984ee79SBenjamin Herrenschmidt if (!nvram_node) { 1216984ee79SBenjamin Herrenschmidt printk(KERN_WARNING "nvram: no node found in device-tree\n"); 1226984ee79SBenjamin Herrenschmidt return -ENODEV; 1236984ee79SBenjamin Herrenschmidt } 124edf03c1eSArnd Bergmann 1256984ee79SBenjamin Herrenschmidt ret = of_address_to_resource(nvram_node, 0, &r); 1266984ee79SBenjamin Herrenschmidt if (ret) { 1276984ee79SBenjamin Herrenschmidt printk(KERN_WARNING "nvram: failed to get address (err %d)\n", 1286984ee79SBenjamin Herrenschmidt ret); 1296984ee79SBenjamin Herrenschmidt goto out; 1306984ee79SBenjamin Herrenschmidt } 1316984ee79SBenjamin Herrenschmidt nvram_addr = r.start; 1326984ee79SBenjamin Herrenschmidt mmio_nvram_len = r.end - r.start + 1; 1336984ee79SBenjamin Herrenschmidt if ( (!mmio_nvram_len) || (!nvram_addr) ) { 13400d70419Sjoe@perches.com printk(KERN_WARNING "nvram: address or length is 0\n"); 135edf03c1eSArnd Bergmann ret = -EIO; 136edf03c1eSArnd Bergmann goto out; 1376984ee79SBenjamin Herrenschmidt } 138edf03c1eSArnd Bergmann 139edf03c1eSArnd Bergmann mmio_nvram_start = ioremap(nvram_addr, mmio_nvram_len); 1406984ee79SBenjamin Herrenschmidt if (!mmio_nvram_start) { 1416984ee79SBenjamin Herrenschmidt printk(KERN_WARNING "nvram: failed to ioremap\n"); 1426984ee79SBenjamin Herrenschmidt ret = -ENOMEM; 143edf03c1eSArnd Bergmann goto out; 1446984ee79SBenjamin Herrenschmidt } 145edf03c1eSArnd Bergmann 1466984ee79SBenjamin Herrenschmidt printk(KERN_INFO "mmio NVRAM, %luk at 0x%lx mapped to %p\n", 1476984ee79SBenjamin Herrenschmidt mmio_nvram_len >> 10, nvram_addr, mmio_nvram_start); 148edf03c1eSArnd Bergmann 149690a2d07SMartyn Welch ppc_md.nvram_read_val = mmio_nvram_read_val; 150690a2d07SMartyn Welch ppc_md.nvram_write_val = mmio_nvram_write_val; 151edf03c1eSArnd Bergmann ppc_md.nvram_read = mmio_nvram_read; 152edf03c1eSArnd Bergmann ppc_md.nvram_write = mmio_nvram_write; 153edf03c1eSArnd Bergmann ppc_md.nvram_size = mmio_nvram_get_size; 154edf03c1eSArnd Bergmann 155edf03c1eSArnd Bergmann out: 156edf03c1eSArnd Bergmann of_node_put(nvram_node); 157edf03c1eSArnd Bergmann return ret; 158edf03c1eSArnd Bergmann } 159