Thomas Gleixner | de6cc65 | 2019-05-27 08:55:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 2 | /* |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 3 | * memory mapped NVRAM |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 4 | * |
| 5 | * (C) Copyright IBM Corp. 2005 |
| 6 | * |
| 7 | * Authors : Utz Bacher <utz.bacher@de.ibm.com> |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/fs.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/spinlock.h> |
| 14 | #include <linux/types.h> |
| 15 | |
| 16 | #include <asm/machdep.h> |
| 17 | #include <asm/nvram.h> |
| 18 | #include <asm/prom.h> |
| 19 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 20 | static void __iomem *mmio_nvram_start; |
| 21 | static long mmio_nvram_len; |
Ingo Molnar | 34af946 | 2006-06-27 02:53:55 -0700 | [diff] [blame] | 22 | static DEFINE_SPINLOCK(mmio_nvram_lock); |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 23 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 24 | 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] | 25 | { |
| 26 | unsigned long flags; |
| 27 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 28 | if (*index >= mmio_nvram_len) |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 29 | return 0; |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 30 | if (*index + count > mmio_nvram_len) |
| 31 | count = mmio_nvram_len - *index; |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 32 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 33 | spin_lock_irqsave(&mmio_nvram_lock, flags); |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 34 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 35 | memcpy_fromio(buf, mmio_nvram_start + *index, count); |
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 | spin_unlock_irqrestore(&mmio_nvram_lock, flags); |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 38 | |
| 39 | *index += count; |
| 40 | return count; |
| 41 | } |
| 42 | |
Martyn Welch | 690a2d0 | 2009-07-02 06:12:18 +0000 | [diff] [blame] | 43 | static unsigned char mmio_nvram_read_val(int addr) |
| 44 | { |
| 45 | unsigned long flags; |
| 46 | unsigned char val; |
| 47 | |
| 48 | if (addr >= mmio_nvram_len) |
| 49 | return 0xff; |
| 50 | |
| 51 | spin_lock_irqsave(&mmio_nvram_lock, flags); |
| 52 | |
| 53 | val = ioread8(mmio_nvram_start + addr); |
| 54 | |
| 55 | spin_unlock_irqrestore(&mmio_nvram_lock, flags); |
| 56 | |
| 57 | return val; |
| 58 | } |
| 59 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 60 | 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] | 61 | { |
| 62 | unsigned long flags; |
| 63 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 64 | if (*index >= mmio_nvram_len) |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 65 | return 0; |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 66 | if (*index + count > mmio_nvram_len) |
| 67 | count = mmio_nvram_len - *index; |
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_lock_irqsave(&mmio_nvram_lock, flags); |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 70 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 71 | memcpy_toio(mmio_nvram_start + *index, buf, count); |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 72 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 73 | spin_unlock_irqrestore(&mmio_nvram_lock, flags); |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 74 | |
| 75 | *index += count; |
| 76 | return count; |
| 77 | } |
| 78 | |
Daniel Axtens | 7c98bd7 | 2016-09-06 15:32:40 +1000 | [diff] [blame] | 79 | static void mmio_nvram_write_val(int addr, unsigned char val) |
Martyn Welch | 690a2d0 | 2009-07-02 06:12:18 +0000 | [diff] [blame] | 80 | { |
| 81 | unsigned long flags; |
| 82 | |
| 83 | if (addr < mmio_nvram_len) { |
| 84 | spin_lock_irqsave(&mmio_nvram_lock, flags); |
| 85 | |
| 86 | iowrite8(val, mmio_nvram_start + addr); |
| 87 | |
| 88 | spin_unlock_irqrestore(&mmio_nvram_lock, flags); |
| 89 | } |
| 90 | } |
| 91 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 92 | static ssize_t mmio_nvram_get_size(void) |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 93 | { |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 94 | return mmio_nvram_len; |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 95 | } |
| 96 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 97 | int __init mmio_nvram_init(void) |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 98 | { |
| 99 | struct device_node *nvram_node; |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 100 | unsigned long nvram_addr; |
Benjamin Herrenschmidt | 6984ee7 | 2007-01-11 16:08:41 +1100 | [diff] [blame] | 101 | struct resource r; |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 102 | int ret; |
| 103 | |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 104 | nvram_node = of_find_node_by_type(NULL, "nvram"); |
Benjamin Herrenschmidt | 411e689 | 2011-04-14 22:32:00 +0000 | [diff] [blame] | 105 | if (!nvram_node) |
| 106 | nvram_node = of_find_compatible_node(NULL, NULL, "nvram"); |
Benjamin Herrenschmidt | 6984ee7 | 2007-01-11 16:08:41 +1100 | [diff] [blame] | 107 | if (!nvram_node) { |
| 108 | printk(KERN_WARNING "nvram: no node found in device-tree\n"); |
| 109 | return -ENODEV; |
| 110 | } |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 111 | |
Benjamin Herrenschmidt | 6984ee7 | 2007-01-11 16:08:41 +1100 | [diff] [blame] | 112 | ret = of_address_to_resource(nvram_node, 0, &r); |
| 113 | if (ret) { |
| 114 | printk(KERN_WARNING "nvram: failed to get address (err %d)\n", |
| 115 | ret); |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 116 | goto out; |
Benjamin Herrenschmidt | 6984ee7 | 2007-01-11 16:08:41 +1100 | [diff] [blame] | 117 | } |
| 118 | nvram_addr = r.start; |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 119 | mmio_nvram_len = resource_size(&r); |
Benjamin Herrenschmidt | 6984ee7 | 2007-01-11 16:08:41 +1100 | [diff] [blame] | 120 | if ( (!mmio_nvram_len) || (!nvram_addr) ) { |
joe@perches.com | 00d7041 | 2007-12-18 06:30:12 +1100 | [diff] [blame] | 121 | printk(KERN_WARNING "nvram: address or length is 0\n"); |
Benjamin Herrenschmidt | 6984ee7 | 2007-01-11 16:08:41 +1100 | [diff] [blame] | 122 | ret = -EIO; |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 123 | goto out; |
Benjamin Herrenschmidt | 6984ee7 | 2007-01-11 16:08:41 +1100 | [diff] [blame] | 124 | } |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 125 | |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 126 | mmio_nvram_start = ioremap(nvram_addr, mmio_nvram_len); |
Benjamin Herrenschmidt | 6984ee7 | 2007-01-11 16:08:41 +1100 | [diff] [blame] | 127 | if (!mmio_nvram_start) { |
| 128 | printk(KERN_WARNING "nvram: failed to ioremap\n"); |
| 129 | ret = -ENOMEM; |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 130 | goto out; |
Benjamin Herrenschmidt | 6984ee7 | 2007-01-11 16:08:41 +1100 | [diff] [blame] | 131 | } |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 132 | |
Benjamin Herrenschmidt | 6984ee7 | 2007-01-11 16:08:41 +1100 | [diff] [blame] | 133 | printk(KERN_INFO "mmio NVRAM, %luk at 0x%lx mapped to %p\n", |
| 134 | mmio_nvram_len >> 10, nvram_addr, mmio_nvram_start); |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 135 | |
Martyn Welch | 690a2d0 | 2009-07-02 06:12:18 +0000 | [diff] [blame] | 136 | ppc_md.nvram_read_val = mmio_nvram_read_val; |
| 137 | ppc_md.nvram_write_val = mmio_nvram_write_val; |
Arnd Bergmann | f3f66f5 | 2005-10-31 20:08:37 -0500 | [diff] [blame] | 138 | ppc_md.nvram_read = mmio_nvram_read; |
| 139 | ppc_md.nvram_write = mmio_nvram_write; |
| 140 | ppc_md.nvram_size = mmio_nvram_get_size; |
Utz Bacher | 5f5b4e6 | 2005-06-23 09:43:31 +1000 | [diff] [blame] | 141 | |
| 142 | out: |
| 143 | of_node_put(nvram_node); |
| 144 | return ret; |
| 145 | } |