blob: 9e3a957c8f1f80c0986368d5c557d2de6c1319e7 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef _LINUX_NVRAM_H
3#define _LINUX_NVRAM_H
4
Finn Thain1278cf62019-01-15 15:18:56 +11005#include <linux/errno.h>
David Howells607ca462012-10-13 10:46:48 +01006#include <uapi/linux/nvram.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
Finn Thaind5bbb502019-01-15 15:18:56 +11008/**
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
Finn Thaina084dbf2019-01-15 15:18:56 +110028struct nvram_ops {
29 ssize_t (*get_size)(void);
Finn Thaind5bbb502019-01-15 15:18:56 +110030 unsigned char (*read_byte)(int);
31 void (*write_byte)(unsigned char, int);
Finn Thaina084dbf2019-01-15 15:18:56 +110032 ssize_t (*read)(char *, size_t, loff_t *);
33 ssize_t (*write)(char *, size_t, loff_t *);
Finn Thain95ac14b2019-01-15 15:18:56 +110034#if defined(CONFIG_X86) || defined(CONFIG_M68K)
Finn Thain2d586362019-01-15 15:18:56 +110035 long (*initialize)(void);
36 long (*set_checksum)(void);
Finn Thain95ac14b2019-01-15 15:18:56 +110037#endif
Finn Thaina084dbf2019-01-15 15:18:56 +110038};
39
40extern const struct nvram_ops arch_nvram_ops;
41
Finn Thain1278cf62019-01-15 15:18:56 +110042static inline ssize_t nvram_get_size(void)
43{
Finn Thaina156c7b2019-01-15 15:18:56 +110044#ifdef CONFIG_PPC
45#else
Finn Thaina084dbf2019-01-15 15:18:56 +110046 if (arch_nvram_ops.get_size)
47 return arch_nvram_ops.get_size();
Finn Thaina156c7b2019-01-15 15:18:56 +110048#endif
Finn Thain1278cf62019-01-15 15:18:56 +110049 return -ENODEV;
50}
51
52static inline unsigned char nvram_read_byte(int addr)
53{
Finn Thaind5bbb502019-01-15 15:18:56 +110054#ifdef CONFIG_PPC
55#else
56 if (arch_nvram_ops.read_byte)
57 return arch_nvram_ops.read_byte(addr);
58#endif
Finn Thain1278cf62019-01-15 15:18:56 +110059 return 0xFF;
60}
61
62static inline void nvram_write_byte(unsigned char val, int addr)
63{
Finn Thaind5bbb502019-01-15 15:18:56 +110064#ifdef CONFIG_PPC
65#else
66 if (arch_nvram_ops.write_byte)
67 arch_nvram_ops.write_byte(val, addr);
68#endif
Finn Thain1278cf62019-01-15 15:18:56 +110069}
70
Finn Thain109b3a82019-01-15 15:18:56 +110071static inline ssize_t nvram_read_bytes(char *buf, size_t count, loff_t *ppos)
72{
73 ssize_t nvram_size = nvram_get_size();
74 loff_t i;
75 char *p = buf;
76
77 if (nvram_size < 0)
78 return nvram_size;
79 for (i = *ppos; count > 0 && i < nvram_size; ++i, ++p, --count)
80 *p = nvram_read_byte(i);
81 *ppos = i;
82 return p - buf;
83}
84
85static inline ssize_t nvram_write_bytes(char *buf, size_t count, loff_t *ppos)
86{
87 ssize_t nvram_size = nvram_get_size();
88 loff_t i;
89 char *p = buf;
90
91 if (nvram_size < 0)
92 return nvram_size;
93 for (i = *ppos; count > 0 && i < nvram_size; ++i, ++p, --count)
94 nvram_write_byte(*p, i);
95 *ppos = i;
96 return p - buf;
97}
98
Finn Thain1278cf62019-01-15 15:18:56 +110099static inline ssize_t nvram_read(char *buf, size_t count, loff_t *ppos)
100{
Finn Thaina084dbf2019-01-15 15:18:56 +1100101 if (arch_nvram_ops.read)
102 return arch_nvram_ops.read(buf, count, ppos);
Finn Thain109b3a82019-01-15 15:18:56 +1100103 return nvram_read_bytes(buf, count, ppos);
Finn Thain1278cf62019-01-15 15:18:56 +1100104}
105
106static inline ssize_t nvram_write(char *buf, size_t count, loff_t *ppos)
107{
Finn Thaina084dbf2019-01-15 15:18:56 +1100108 if (arch_nvram_ops.write)
109 return arch_nvram_ops.write(buf, count, ppos);
Finn Thain109b3a82019-01-15 15:18:56 +1100110 return nvram_write_bytes(buf, count, ppos);
Finn Thain1278cf62019-01-15 15:18:56 +1100111}
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#endif /* _LINUX_NVRAM_H */