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 56edf03c1eSArnd Bergmann static ssize_t mmio_nvram_write(char *buf, size_t count, loff_t *index) 57edf03c1eSArnd Bergmann { 58edf03c1eSArnd Bergmann unsigned long flags; 59edf03c1eSArnd Bergmann 60edf03c1eSArnd Bergmann if (*index >= mmio_nvram_len) 61edf03c1eSArnd Bergmann return 0; 62edf03c1eSArnd Bergmann if (*index + count > mmio_nvram_len) 63edf03c1eSArnd Bergmann count = mmio_nvram_len - *index; 64edf03c1eSArnd Bergmann 65edf03c1eSArnd Bergmann spin_lock_irqsave(&mmio_nvram_lock, flags); 66edf03c1eSArnd Bergmann 67edf03c1eSArnd Bergmann memcpy_toio(mmio_nvram_start + *index, buf, count); 68edf03c1eSArnd Bergmann 69edf03c1eSArnd Bergmann spin_unlock_irqrestore(&mmio_nvram_lock, flags); 70edf03c1eSArnd Bergmann 71edf03c1eSArnd Bergmann *index += count; 72edf03c1eSArnd Bergmann return count; 73edf03c1eSArnd Bergmann } 74edf03c1eSArnd Bergmann 75edf03c1eSArnd Bergmann static ssize_t mmio_nvram_get_size(void) 76edf03c1eSArnd Bergmann { 77edf03c1eSArnd Bergmann return mmio_nvram_len; 78edf03c1eSArnd Bergmann } 79edf03c1eSArnd Bergmann 80edf03c1eSArnd Bergmann int __init mmio_nvram_init(void) 81edf03c1eSArnd Bergmann { 82edf03c1eSArnd Bergmann struct device_node *nvram_node; 83edf03c1eSArnd Bergmann unsigned long nvram_addr; 846984ee79SBenjamin Herrenschmidt struct resource r; 85edf03c1eSArnd Bergmann int ret; 86edf03c1eSArnd Bergmann 87edf03c1eSArnd Bergmann nvram_node = of_find_node_by_type(NULL, "nvram"); 886984ee79SBenjamin Herrenschmidt if (!nvram_node) { 896984ee79SBenjamin Herrenschmidt printk(KERN_WARNING "nvram: no node found in device-tree\n"); 906984ee79SBenjamin Herrenschmidt return -ENODEV; 916984ee79SBenjamin Herrenschmidt } 92edf03c1eSArnd Bergmann 936984ee79SBenjamin Herrenschmidt ret = of_address_to_resource(nvram_node, 0, &r); 946984ee79SBenjamin Herrenschmidt if (ret) { 956984ee79SBenjamin Herrenschmidt printk(KERN_WARNING "nvram: failed to get address (err %d)\n", 966984ee79SBenjamin Herrenschmidt ret); 976984ee79SBenjamin Herrenschmidt goto out; 986984ee79SBenjamin Herrenschmidt } 996984ee79SBenjamin Herrenschmidt nvram_addr = r.start; 1006984ee79SBenjamin Herrenschmidt mmio_nvram_len = r.end - r.start + 1; 1016984ee79SBenjamin Herrenschmidt if ( (!mmio_nvram_len) || (!nvram_addr) ) { 102*00d70419Sjoe@perches.com printk(KERN_WARNING "nvram: address or length is 0\n"); 103edf03c1eSArnd Bergmann ret = -EIO; 104edf03c1eSArnd Bergmann goto out; 1056984ee79SBenjamin Herrenschmidt } 106edf03c1eSArnd Bergmann 107edf03c1eSArnd Bergmann mmio_nvram_start = ioremap(nvram_addr, mmio_nvram_len); 1086984ee79SBenjamin Herrenschmidt if (!mmio_nvram_start) { 1096984ee79SBenjamin Herrenschmidt printk(KERN_WARNING "nvram: failed to ioremap\n"); 1106984ee79SBenjamin Herrenschmidt ret = -ENOMEM; 111edf03c1eSArnd Bergmann goto out; 1126984ee79SBenjamin Herrenschmidt } 113edf03c1eSArnd Bergmann 1146984ee79SBenjamin Herrenschmidt printk(KERN_INFO "mmio NVRAM, %luk at 0x%lx mapped to %p\n", 1156984ee79SBenjamin Herrenschmidt mmio_nvram_len >> 10, nvram_addr, mmio_nvram_start); 116edf03c1eSArnd Bergmann 117edf03c1eSArnd Bergmann ppc_md.nvram_read = mmio_nvram_read; 118edf03c1eSArnd Bergmann ppc_md.nvram_write = mmio_nvram_write; 119edf03c1eSArnd Bergmann ppc_md.nvram_size = mmio_nvram_get_size; 120edf03c1eSArnd Bergmann 121edf03c1eSArnd Bergmann out: 122edf03c1eSArnd Bergmann of_node_put(nvram_node); 123edf03c1eSArnd Bergmann return ret; 124edf03c1eSArnd Bergmann } 125