Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_NVRAM_H
0003 #define _LINUX_NVRAM_H
0004 
0005 #include <linux/errno.h>
0006 #include <uapi/linux/nvram.h>
0007 
0008 #ifdef CONFIG_PPC
0009 #include <asm/machdep.h>
0010 #endif
0011 
0012 /**
0013  * struct nvram_ops - NVRAM functionality made available to drivers
0014  * @read: validate checksum (if any) then load a range of bytes from NVRAM
0015  * @write: store a range of bytes to NVRAM then update checksum (if any)
0016  * @read_byte: load a single byte from NVRAM
0017  * @write_byte: store a single byte to NVRAM
0018  * @get_size: return the fixed number of bytes in the NVRAM
0019  *
0020  * Architectures which provide an nvram ops struct need not implement all
0021  * of these methods. If the NVRAM hardware can be accessed only one byte
0022  * at a time then it may be sufficient to provide .read_byte and .write_byte.
0023  * If the NVRAM has a checksum (and it is to be checked) the .read and
0024  * .write methods can be used to implement that efficiently.
0025  *
0026  * Portable drivers may use the wrapper functions defined here.
0027  * The nvram_read() and nvram_write() functions call the .read and .write
0028  * methods when available and fall back on the .read_byte and .write_byte
0029  * methods otherwise.
0030  */
0031 
0032 struct nvram_ops {
0033     ssize_t         (*get_size)(void);
0034     unsigned char   (*read_byte)(int);
0035     void            (*write_byte)(unsigned char, int);
0036     ssize_t         (*read)(char *, size_t, loff_t *);
0037     ssize_t         (*write)(char *, size_t, loff_t *);
0038 #if defined(CONFIG_X86) || defined(CONFIG_M68K)
0039     long            (*initialize)(void);
0040     long            (*set_checksum)(void);
0041 #endif
0042 };
0043 
0044 extern const struct nvram_ops arch_nvram_ops;
0045 
0046 static inline ssize_t nvram_get_size(void)
0047 {
0048 #ifdef CONFIG_PPC
0049     if (ppc_md.nvram_size)
0050         return ppc_md.nvram_size();
0051 #else
0052     if (arch_nvram_ops.get_size)
0053         return arch_nvram_ops.get_size();
0054 #endif
0055     return -ENODEV;
0056 }
0057 
0058 static inline unsigned char nvram_read_byte(int addr)
0059 {
0060 #ifdef CONFIG_PPC
0061     if (ppc_md.nvram_read_val)
0062         return ppc_md.nvram_read_val(addr);
0063 #else
0064     if (arch_nvram_ops.read_byte)
0065         return arch_nvram_ops.read_byte(addr);
0066 #endif
0067     return 0xFF;
0068 }
0069 
0070 static inline void nvram_write_byte(unsigned char val, int addr)
0071 {
0072 #ifdef CONFIG_PPC
0073     if (ppc_md.nvram_write_val)
0074         ppc_md.nvram_write_val(addr, val);
0075 #else
0076     if (arch_nvram_ops.write_byte)
0077         arch_nvram_ops.write_byte(val, addr);
0078 #endif
0079 }
0080 
0081 static inline ssize_t nvram_read_bytes(char *buf, size_t count, loff_t *ppos)
0082 {
0083     ssize_t nvram_size = nvram_get_size();
0084     loff_t i;
0085     char *p = buf;
0086 
0087     if (nvram_size < 0)
0088         return nvram_size;
0089     for (i = *ppos; count > 0 && i < nvram_size; ++i, ++p, --count)
0090         *p = nvram_read_byte(i);
0091     *ppos = i;
0092     return p - buf;
0093 }
0094 
0095 static inline ssize_t nvram_write_bytes(char *buf, size_t count, loff_t *ppos)
0096 {
0097     ssize_t nvram_size = nvram_get_size();
0098     loff_t i;
0099     char *p = buf;
0100 
0101     if (nvram_size < 0)
0102         return nvram_size;
0103     for (i = *ppos; count > 0 && i < nvram_size; ++i, ++p, --count)
0104         nvram_write_byte(*p, i);
0105     *ppos = i;
0106     return p - buf;
0107 }
0108 
0109 static inline ssize_t nvram_read(char *buf, size_t count, loff_t *ppos)
0110 {
0111 #ifdef CONFIG_PPC
0112     if (ppc_md.nvram_read)
0113         return ppc_md.nvram_read(buf, count, ppos);
0114 #else
0115     if (arch_nvram_ops.read)
0116         return arch_nvram_ops.read(buf, count, ppos);
0117 #endif
0118     return nvram_read_bytes(buf, count, ppos);
0119 }
0120 
0121 static inline ssize_t nvram_write(char *buf, size_t count, loff_t *ppos)
0122 {
0123 #ifdef CONFIG_PPC
0124     if (ppc_md.nvram_write)
0125         return ppc_md.nvram_write(buf, count, ppos);
0126 #else
0127     if (arch_nvram_ops.write)
0128         return arch_nvram_ops.write(buf, count, ppos);
0129 #endif
0130     return nvram_write_bytes(buf, count, ppos);
0131 }
0132 
0133 #endif  /* _LINUX_NVRAM_H */