xref: /linux-6.15/include/linux/nvram.h (revision 2d58636e)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_NVRAM_H
3 #define _LINUX_NVRAM_H
4 
5 #include <linux/errno.h>
6 #include <uapi/linux/nvram.h>
7 
8 /**
9  * struct nvram_ops - NVRAM functionality made available to drivers
10  * @read: validate checksum (if any) then load a range of bytes from NVRAM
11  * @write: store a range of bytes to NVRAM then update checksum (if any)
12  * @read_byte: load a single byte from NVRAM
13  * @write_byte: store a single byte to NVRAM
14  * @get_size: return the fixed number of bytes in the NVRAM
15  *
16  * Architectures which provide an nvram ops struct need not implement all
17  * of these methods. If the NVRAM hardware can be accessed only one byte
18  * at a time then it may be sufficient to provide .read_byte and .write_byte.
19  * If the NVRAM has a checksum (and it is to be checked) the .read and
20  * .write methods can be used to implement that efficiently.
21  *
22  * Portable drivers may use the wrapper functions defined here.
23  * The nvram_read() and nvram_write() functions call the .read and .write
24  * methods when available and fall back on the .read_byte and .write_byte
25  * methods otherwise.
26  */
27 
28 struct nvram_ops {
29 	ssize_t         (*get_size)(void);
30 	unsigned char   (*read_byte)(int);
31 	void            (*write_byte)(unsigned char, int);
32 	ssize_t         (*read)(char *, size_t, loff_t *);
33 	ssize_t         (*write)(char *, size_t, loff_t *);
34 	long            (*initialize)(void);
35 	long            (*set_checksum)(void);
36 };
37 
38 extern const struct nvram_ops arch_nvram_ops;
39 
40 static inline ssize_t nvram_get_size(void)
41 {
42 #ifdef CONFIG_PPC
43 #else
44 	if (arch_nvram_ops.get_size)
45 		return arch_nvram_ops.get_size();
46 #endif
47 	return -ENODEV;
48 }
49 
50 static inline unsigned char nvram_read_byte(int addr)
51 {
52 #ifdef CONFIG_PPC
53 #else
54 	if (arch_nvram_ops.read_byte)
55 		return arch_nvram_ops.read_byte(addr);
56 #endif
57 	return 0xFF;
58 }
59 
60 static inline void nvram_write_byte(unsigned char val, int addr)
61 {
62 #ifdef CONFIG_PPC
63 #else
64 	if (arch_nvram_ops.write_byte)
65 		arch_nvram_ops.write_byte(val, addr);
66 #endif
67 }
68 
69 static inline ssize_t nvram_read(char *buf, size_t count, loff_t *ppos)
70 {
71 	if (arch_nvram_ops.read)
72 		return arch_nvram_ops.read(buf, count, ppos);
73 	return -ENODEV;
74 }
75 
76 static inline ssize_t nvram_write(char *buf, size_t count, loff_t *ppos)
77 {
78 	if (arch_nvram_ops.write)
79 		return arch_nvram_ops.write(buf, count, ppos);
80 	return -ENODEV;
81 }
82 
83 #endif  /* _LINUX_NVRAM_H */
84