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; 35*34af946aSIngo 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 *buffer; 84edf03c1eSArnd Bergmann int proplen; 85edf03c1eSArnd Bergmann unsigned long nvram_addr; 86edf03c1eSArnd Bergmann int ret; 87edf03c1eSArnd Bergmann 88edf03c1eSArnd Bergmann ret = -ENODEV; 89edf03c1eSArnd Bergmann nvram_node = of_find_node_by_type(NULL, "nvram"); 90edf03c1eSArnd Bergmann if (!nvram_node) 91edf03c1eSArnd Bergmann goto out; 92edf03c1eSArnd Bergmann 93edf03c1eSArnd Bergmann ret = -EIO; 94edf03c1eSArnd Bergmann buffer = (unsigned long *)get_property(nvram_node, "reg", &proplen); 95edf03c1eSArnd Bergmann if (proplen != 2*sizeof(unsigned long)) 96edf03c1eSArnd Bergmann goto out; 97edf03c1eSArnd Bergmann 98edf03c1eSArnd Bergmann ret = -ENODEV; 99edf03c1eSArnd Bergmann nvram_addr = buffer[0]; 100edf03c1eSArnd Bergmann mmio_nvram_len = buffer[1]; 101edf03c1eSArnd Bergmann if ( (!mmio_nvram_len) || (!nvram_addr) ) 102edf03c1eSArnd Bergmann goto out; 103edf03c1eSArnd Bergmann 104edf03c1eSArnd Bergmann mmio_nvram_start = ioremap(nvram_addr, mmio_nvram_len); 105edf03c1eSArnd Bergmann if (!mmio_nvram_start) 106edf03c1eSArnd Bergmann goto out; 107edf03c1eSArnd Bergmann 108edf03c1eSArnd Bergmann printk(KERN_INFO "mmio NVRAM, %luk mapped to %p\n", 109edf03c1eSArnd Bergmann mmio_nvram_len >> 10, mmio_nvram_start); 110edf03c1eSArnd Bergmann 111edf03c1eSArnd Bergmann ppc_md.nvram_read = mmio_nvram_read; 112edf03c1eSArnd Bergmann ppc_md.nvram_write = mmio_nvram_write; 113edf03c1eSArnd Bergmann ppc_md.nvram_size = mmio_nvram_get_size; 114edf03c1eSArnd Bergmann 115edf03c1eSArnd Bergmann out: 116edf03c1eSArnd Bergmann of_node_put(nvram_node); 117edf03c1eSArnd Bergmann return ret; 118edf03c1eSArnd Bergmann } 119