nvram.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_NVRAM_H
  3. #define _LINUX_NVRAM_H
  4. #include <linux/errno.h>
  5. #include <uapi/linux/nvram.h>
  6. #ifdef CONFIG_PPC
  7. #include <asm/machdep.h>
  8. #endif
  9. /**
  10. * struct nvram_ops - NVRAM functionality made available to drivers
  11. * @read: validate checksum (if any) then load a range of bytes from NVRAM
  12. * @write: store a range of bytes to NVRAM then update checksum (if any)
  13. * @read_byte: load a single byte from NVRAM
  14. * @write_byte: store a single byte to NVRAM
  15. * @get_size: return the fixed number of bytes in the NVRAM
  16. *
  17. * Architectures which provide an nvram ops struct need not implement all
  18. * of these methods. If the NVRAM hardware can be accessed only one byte
  19. * at a time then it may be sufficient to provide .read_byte and .write_byte.
  20. * If the NVRAM has a checksum (and it is to be checked) the .read and
  21. * .write methods can be used to implement that efficiently.
  22. *
  23. * Portable drivers may use the wrapper functions defined here.
  24. * The nvram_read() and nvram_write() functions call the .read and .write
  25. * methods when available and fall back on the .read_byte and .write_byte
  26. * methods otherwise.
  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. #if defined(CONFIG_X86) || defined(CONFIG_M68K)
  35. long (*initialize)(void);
  36. long (*set_checksum)(void);
  37. #endif
  38. };
  39. extern const struct nvram_ops arch_nvram_ops;
  40. static inline ssize_t nvram_get_size(void)
  41. {
  42. #ifdef CONFIG_PPC
  43. if (ppc_md.nvram_size)
  44. return ppc_md.nvram_size();
  45. #else
  46. if (arch_nvram_ops.get_size)
  47. return arch_nvram_ops.get_size();
  48. #endif
  49. return -ENODEV;
  50. }
  51. static inline unsigned char nvram_read_byte(int addr)
  52. {
  53. #ifdef CONFIG_PPC
  54. if (ppc_md.nvram_read_val)
  55. return ppc_md.nvram_read_val(addr);
  56. #else
  57. if (arch_nvram_ops.read_byte)
  58. return arch_nvram_ops.read_byte(addr);
  59. #endif
  60. return 0xFF;
  61. }
  62. static inline void nvram_write_byte(unsigned char val, int addr)
  63. {
  64. #ifdef CONFIG_PPC
  65. if (ppc_md.nvram_write_val)
  66. ppc_md.nvram_write_val(addr, val);
  67. #else
  68. if (arch_nvram_ops.write_byte)
  69. arch_nvram_ops.write_byte(val, addr);
  70. #endif
  71. }
  72. static inline ssize_t nvram_read_bytes(char *buf, size_t count, loff_t *ppos)
  73. {
  74. ssize_t nvram_size = nvram_get_size();
  75. loff_t i;
  76. char *p = buf;
  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. static inline ssize_t nvram_write_bytes(char *buf, size_t count, loff_t *ppos)
  85. {
  86. ssize_t nvram_size = nvram_get_size();
  87. loff_t i;
  88. char *p = buf;
  89. if (nvram_size < 0)
  90. return nvram_size;
  91. for (i = *ppos; count > 0 && i < nvram_size; ++i, ++p, --count)
  92. nvram_write_byte(*p, i);
  93. *ppos = i;
  94. return p - buf;
  95. }
  96. static inline ssize_t nvram_read(char *buf, size_t count, loff_t *ppos)
  97. {
  98. #ifdef CONFIG_PPC
  99. if (ppc_md.nvram_read)
  100. return ppc_md.nvram_read(buf, count, ppos);
  101. #else
  102. if (arch_nvram_ops.read)
  103. return arch_nvram_ops.read(buf, count, ppos);
  104. #endif
  105. return nvram_read_bytes(buf, count, ppos);
  106. }
  107. static inline ssize_t nvram_write(char *buf, size_t count, loff_t *ppos)
  108. {
  109. #ifdef CONFIG_PPC
  110. if (ppc_md.nvram_write)
  111. return ppc_md.nvram_write(buf, count, ppos);
  112. #else
  113. if (arch_nvram_ops.write)
  114. return arch_nvram_ops.write(buf, count, ppos);
  115. #endif
  116. return nvram_write_bytes(buf, count, ppos);
  117. }
  118. #endif /* _LINUX_NVRAM_H */