Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 1 | /* |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 2 | * memory mapped NVRAM |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 3 | * |
| 4 | * (C) Copyright IBM Corp. 2005 |
| 5 | * |
| 6 | * Authors : Utz Bacher <utz.bacher@de.ibm.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2, or (at your option) |
| 11 | * any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <linux/fs.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/spinlock.h> |
| 27 | #include <linux/types.h> |
| 28 | |
| 29 | #include <asm/machdep.h> |
| 30 | #include <asm/nvram.h> |
| 31 | #include <asm/prom.h> |
| 32 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 33 | static void __iomem *mmio_nvram_start; |
| 34 | static long mmio_nvram_len; |
Ingo Molnar | 34af946 | 2006-06-27 02:53:55 -0700 | [diff] [blame] | 35 | static DEFINE_SPINLOCK(mmio_nvram_lock); |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 36 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 37 | static ssize_t mmio_nvram_read(char *buf, size_t count, loff_t *index) |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 38 | { |
| 39 | unsigned long flags; |
| 40 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 41 | if (*index >= mmio_nvram_len) |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 42 | return 0; |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 43 | if (*index + count > mmio_nvram_len) |
| 44 | count = mmio_nvram_len - *index; |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 45 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 46 | spin_lock_irqsave(&mmio_nvram_lock, flags); |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 47 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 48 | memcpy_fromio(buf, mmio_nvram_start + *index, count); |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 49 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 50 | spin_unlock_irqrestore(&mmio_nvram_lock, flags); |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 51 | |
| 52 | *index += count; |
| 53 | return count; |
| 54 | } |
| 55 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 56 | static ssize_t mmio_nvram_write(char *buf, size_t count, loff_t *index) |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 57 | { |
| 58 | unsigned long flags; |
| 59 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 60 | if (*index >= mmio_nvram_len) |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 61 | return 0; |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 62 | if (*index + count > mmio_nvram_len) |
| 63 | count = mmio_nvram_len - *index; |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 64 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 65 | spin_lock_irqsave(&mmio_nvram_lock, flags); |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 66 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 67 | memcpy_toio(mmio_nvram_start + *index, buf, count); |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 68 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 69 | spin_unlock_irqrestore(&mmio_nvram_lock, flags); |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 70 | |
| 71 | *index += count; |
| 72 | return count; |
| 73 | } |
| 74 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 75 | static ssize_t mmio_nvram_get_size(void) |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 76 | { |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 77 | return mmio_nvram_len; |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 78 | } |
| 79 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 80 | int __init mmio_nvram_init(void) |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 81 | { |
| 82 | struct device_node *nvram_node; |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 83 | unsigned long nvram_addr; |
Benjamin Herrenschmidt | 6984ee7 | 2007-01-11 16:08:41 +1100 | [diff] [blame] | 84 | struct resource r; |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 85 | int ret; |
| 86 | |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 87 | nvram_node = of_find_node_by_type(NULL, "nvram"); |
Benjamin Herrenschmidt | 6984ee7 | 2007-01-11 16:08:41 +1100 | [diff] [blame] | 88 | if (!nvram_node) { |
| 89 | printk(KERN_WARNING "nvram: no node found in device-tree\n"); |
| 90 | return -ENODEV; |
| 91 | } |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 92 | |
Benjamin Herrenschmidt | 6984ee7 | 2007-01-11 16:08:41 +1100 | [diff] [blame] | 93 | ret = of_address_to_resource(nvram_node, 0, &r); |
| 94 | if (ret) { |
| 95 | printk(KERN_WARNING "nvram: failed to get address (err %d)\n", |
| 96 | ret); |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 97 | goto out; |
Benjamin Herrenschmidt | 6984ee7 | 2007-01-11 16:08:41 +1100 | [diff] [blame] | 98 | } |
| 99 | nvram_addr = r.start; |
| 100 | mmio_nvram_len = r.end - r.start + 1; |
| 101 | if ( (!mmio_nvram_len) || (!nvram_addr) ) { |
| 102 | printk(KERN_WARNING "nvram: address or lenght is 0\n"); |
| 103 | ret = -EIO; |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 104 | goto out; |
Benjamin Herrenschmidt | 6984ee7 | 2007-01-11 16:08:41 +1100 | [diff] [blame] | 105 | } |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 106 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 107 | mmio_nvram_start = ioremap(nvram_addr, mmio_nvram_len); |
Benjamin Herrenschmidt | 6984ee7 | 2007-01-11 16:08:41 +1100 | [diff] [blame] | 108 | if (!mmio_nvram_start) { |
| 109 | printk(KERN_WARNING "nvram: failed to ioremap\n"); |
| 110 | ret = -ENOMEM; |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 111 | goto out; |
Benjamin Herrenschmidt | 6984ee7 | 2007-01-11 16:08:41 +1100 | [diff] [blame] | 112 | } |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 113 | |
Benjamin Herrenschmidt | 6984ee7 | 2007-01-11 16:08:41 +1100 | [diff] [blame] | 114 | printk(KERN_INFO "mmio NVRAM, %luk at 0x%lx mapped to %p\n", |
| 115 | mmio_nvram_len >> 10, nvram_addr, mmio_nvram_start); |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 116 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 117 | ppc_md.nvram_read = mmio_nvram_read; |
| 118 | ppc_md.nvram_write = mmio_nvram_write; |
| 119 | ppc_md.nvram_size = mmio_nvram_get_size; |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 120 | |
| 121 | out: |
| 122 | of_node_put(nvram_node); |
| 123 | return ret; |
| 124 | } |