Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | #ifndef _LINUX_NVRAM_H |
| 3 | #define _LINUX_NVRAM_H |
| 4 | |
Finn Thain | 1278cf6 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 5 | #include <linux/errno.h> |
David Howells | 607ca46 | 2012-10-13 10:46:48 +0100 | [diff] [blame] | 6 | #include <uapi/linux/nvram.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | |
Finn Thain | f9c3a57 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 8 | #ifdef CONFIG_PPC |
| 9 | #include <asm/machdep.h> |
| 10 | #endif |
| 11 | |
Finn Thain | d5bbb50 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 12 | /** |
| 13 | * struct nvram_ops - NVRAM functionality made available to drivers |
| 14 | * @read: validate checksum (if any) then load a range of bytes from NVRAM |
| 15 | * @write: store a range of bytes to NVRAM then update checksum (if any) |
| 16 | * @read_byte: load a single byte from NVRAM |
| 17 | * @write_byte: store a single byte to NVRAM |
| 18 | * @get_size: return the fixed number of bytes in the NVRAM |
| 19 | * |
| 20 | * Architectures which provide an nvram ops struct need not implement all |
| 21 | * of these methods. If the NVRAM hardware can be accessed only one byte |
| 22 | * at a time then it may be sufficient to provide .read_byte and .write_byte. |
| 23 | * If the NVRAM has a checksum (and it is to be checked) the .read and |
| 24 | * .write methods can be used to implement that efficiently. |
| 25 | * |
| 26 | * Portable drivers may use the wrapper functions defined here. |
| 27 | * The nvram_read() and nvram_write() functions call the .read and .write |
| 28 | * methods when available and fall back on the .read_byte and .write_byte |
| 29 | * methods otherwise. |
| 30 | */ |
| 31 | |
Finn Thain | a084dbf | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 32 | struct nvram_ops { |
| 33 | ssize_t (*get_size)(void); |
Finn Thain | d5bbb50 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 34 | unsigned char (*read_byte)(int); |
| 35 | void (*write_byte)(unsigned char, int); |
Finn Thain | a084dbf | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 36 | ssize_t (*read)(char *, size_t, loff_t *); |
| 37 | ssize_t (*write)(char *, size_t, loff_t *); |
Finn Thain | 95ac14b | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 38 | #if defined(CONFIG_X86) || defined(CONFIG_M68K) |
Finn Thain | 2d58636 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 39 | long (*initialize)(void); |
| 40 | long (*set_checksum)(void); |
Finn Thain | 95ac14b | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 41 | #endif |
Finn Thain | a084dbf | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | extern const struct nvram_ops arch_nvram_ops; |
| 45 | |
Finn Thain | 1278cf6 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 46 | static inline ssize_t nvram_get_size(void) |
| 47 | { |
Finn Thain | a156c7b | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 48 | #ifdef CONFIG_PPC |
Finn Thain | f9c3a57 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 49 | if (ppc_md.nvram_size) |
| 50 | return ppc_md.nvram_size(); |
Finn Thain | a156c7b | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 51 | #else |
Finn Thain | a084dbf | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 52 | if (arch_nvram_ops.get_size) |
| 53 | return arch_nvram_ops.get_size(); |
Finn Thain | a156c7b | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 54 | #endif |
Finn Thain | 1278cf6 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 55 | return -ENODEV; |
| 56 | } |
| 57 | |
| 58 | static inline unsigned char nvram_read_byte(int addr) |
| 59 | { |
Finn Thain | d5bbb50 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 60 | #ifdef CONFIG_PPC |
Finn Thain | f9c3a57 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 61 | if (ppc_md.nvram_read_val) |
| 62 | return ppc_md.nvram_read_val(addr); |
Finn Thain | d5bbb50 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 63 | #else |
| 64 | if (arch_nvram_ops.read_byte) |
| 65 | return arch_nvram_ops.read_byte(addr); |
| 66 | #endif |
Finn Thain | 1278cf6 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 67 | return 0xFF; |
| 68 | } |
| 69 | |
| 70 | static inline void nvram_write_byte(unsigned char val, int addr) |
| 71 | { |
Finn Thain | d5bbb50 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 72 | #ifdef CONFIG_PPC |
Finn Thain | f9c3a57 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 73 | if (ppc_md.nvram_write_val) |
| 74 | ppc_md.nvram_write_val(addr, val); |
Finn Thain | d5bbb50 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 75 | #else |
| 76 | if (arch_nvram_ops.write_byte) |
| 77 | arch_nvram_ops.write_byte(val, addr); |
| 78 | #endif |
Finn Thain | 1278cf6 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 79 | } |
| 80 | |
Finn Thain | 109b3a8 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 81 | static inline ssize_t nvram_read_bytes(char *buf, size_t count, loff_t *ppos) |
| 82 | { |
| 83 | ssize_t nvram_size = nvram_get_size(); |
| 84 | loff_t i; |
| 85 | char *p = buf; |
| 86 | |
| 87 | if (nvram_size < 0) |
| 88 | return nvram_size; |
| 89 | for (i = *ppos; count > 0 && i < nvram_size; ++i, ++p, --count) |
| 90 | *p = nvram_read_byte(i); |
| 91 | *ppos = i; |
| 92 | return p - buf; |
| 93 | } |
| 94 | |
| 95 | static inline ssize_t nvram_write_bytes(char *buf, size_t count, loff_t *ppos) |
| 96 | { |
| 97 | ssize_t nvram_size = nvram_get_size(); |
| 98 | loff_t i; |
| 99 | char *p = buf; |
| 100 | |
| 101 | if (nvram_size < 0) |
| 102 | return nvram_size; |
| 103 | for (i = *ppos; count > 0 && i < nvram_size; ++i, ++p, --count) |
| 104 | nvram_write_byte(*p, i); |
| 105 | *ppos = i; |
| 106 | return p - buf; |
| 107 | } |
| 108 | |
Finn Thain | 1278cf6 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 109 | static inline ssize_t nvram_read(char *buf, size_t count, loff_t *ppos) |
| 110 | { |
Finn Thain | f9c3a57 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 111 | #ifdef CONFIG_PPC |
| 112 | if (ppc_md.nvram_read) |
| 113 | return ppc_md.nvram_read(buf, count, ppos); |
| 114 | #else |
Finn Thain | a084dbf | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 115 | if (arch_nvram_ops.read) |
| 116 | return arch_nvram_ops.read(buf, count, ppos); |
Finn Thain | f9c3a57 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 117 | #endif |
Finn Thain | 109b3a8 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 118 | return nvram_read_bytes(buf, count, ppos); |
Finn Thain | 1278cf6 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static inline ssize_t nvram_write(char *buf, size_t count, loff_t *ppos) |
| 122 | { |
Finn Thain | f9c3a57 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 123 | #ifdef CONFIG_PPC |
| 124 | if (ppc_md.nvram_write) |
| 125 | return ppc_md.nvram_write(buf, count, ppos); |
| 126 | #else |
Finn Thain | a084dbf | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 127 | if (arch_nvram_ops.write) |
| 128 | return arch_nvram_ops.write(buf, count, ppos); |
Finn Thain | f9c3a57 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 129 | #endif |
Finn Thain | 109b3a8 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 130 | return nvram_write_bytes(buf, count, ppos); |
Finn Thain | 1278cf6 | 2019-01-15 15:18:56 +1100 | [diff] [blame] | 131 | } |
| 132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | #endif /* _LINUX_NVRAM_H */ |