Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010 Broadcom Corporation |
| 3 | * |
| 4 | * Permission to use, copy, modify, and/or distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 11 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 13 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 14 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | |
| 17 | #ifndef _bcmnvram_h_ |
| 18 | #define _bcmnvram_h_ |
| 19 | |
| 20 | #ifndef _LANGUAGE_ASSEMBLY |
| 21 | |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 22 | #include <bcmdefs.h> |
| 23 | |
| 24 | struct nvram_header { |
Greg Kroah-Hartman | 66cbd3a | 2010-10-08 11:05:47 -0700 | [diff] [blame] | 25 | u32 magic; |
| 26 | u32 len; |
| 27 | u32 crc_ver_init; /* 0:7 crc, 8:15 ver, 16:31 sdram_init */ |
| 28 | u32 config_refresh; /* 0:15 sdram_config, 16:31 sdram_refresh */ |
| 29 | u32 config_ncdl; /* ncdl values for memc */ |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | struct nvram_tuple { |
| 33 | char *name; |
| 34 | char *value; |
| 35 | struct nvram_tuple *next; |
| 36 | }; |
| 37 | |
| 38 | /* |
| 39 | * Get default value for an NVRAM variable |
| 40 | */ |
| 41 | extern char *nvram_default_get(const char *name); |
| 42 | |
| 43 | /* |
| 44 | * Initialize NVRAM access. May be unnecessary or undefined on certain |
| 45 | * platforms. |
| 46 | */ |
| 47 | extern int nvram_init(void *sih); |
| 48 | |
| 49 | /* |
| 50 | * Append a chunk of nvram variables to the global list |
| 51 | */ |
| 52 | extern int nvram_append(void *si, char *vars, uint varsz); |
| 53 | |
| 54 | /* |
| 55 | * Check for reset button press for restoring factory defaults. |
| 56 | */ |
| 57 | extern int nvram_reset(void *sih); |
| 58 | |
| 59 | /* |
| 60 | * Disable NVRAM access. May be unnecessary or undefined on certain |
| 61 | * platforms. |
| 62 | */ |
| 63 | extern void nvram_exit(void *sih); |
| 64 | |
| 65 | /* |
| 66 | * Get the value of an NVRAM variable. The pointer returned may be |
| 67 | * invalid after a set. |
| 68 | * @param name name of variable to get |
| 69 | * @return value of variable or NULL if undefined |
| 70 | */ |
| 71 | extern char *nvram_get(const char *name); |
| 72 | |
| 73 | /* |
| 74 | * Read the reset GPIO value from the nvram and set the GPIO |
| 75 | * as input |
| 76 | */ |
Jason Cooper | b4f790e | 2010-10-11 10:02:58 -0400 | [diff] [blame] | 77 | extern int nvram_resetgpio_init(void *sih); |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 78 | |
| 79 | /* |
| 80 | * Get the value of an NVRAM variable. |
| 81 | * @param name name of variable to get |
| 82 | * @return value of variable or NUL if undefined |
| 83 | */ |
| 84 | #define nvram_safe_get(name) (nvram_get(name) ? : "") |
| 85 | |
| 86 | /* |
| 87 | * Match an NVRAM variable. |
| 88 | * @param name name of variable to match |
| 89 | * @param match value to compare against value of variable |
Greg Kroah-Hartman | 0f0881b | 2010-10-12 12:15:18 -0700 | [diff] [blame] | 90 | * @return true if variable is defined and its value is string equal |
Greg Kroah-Hartman | 0965ae8 | 2010-10-12 12:50:15 -0700 | [diff] [blame] | 91 | * to match or false otherwise |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 92 | */ |
Greg Kroah-Hartman | 2d956e2 | 2010-10-05 09:40:02 -0700 | [diff] [blame] | 93 | static inline int nvram_match(char *name, char *match) |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 94 | { |
| 95 | const char *value = nvram_get(name); |
Jason Cooper | 90ea229 | 2010-09-14 09:45:32 -0400 | [diff] [blame] | 96 | return value && !strcmp(value, match); |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Inversely match an NVRAM variable. |
| 101 | * @param name name of variable to match |
| 102 | * @param match value to compare against value of variable |
Greg Kroah-Hartman | 0f0881b | 2010-10-12 12:15:18 -0700 | [diff] [blame] | 103 | * @return true if variable is defined and its value is not string |
Greg Kroah-Hartman | 0965ae8 | 2010-10-12 12:50:15 -0700 | [diff] [blame] | 104 | * equal to invmatch or false otherwise |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 105 | */ |
Greg Kroah-Hartman | 2d956e2 | 2010-10-05 09:40:02 -0700 | [diff] [blame] | 106 | static inline int nvram_invmatch(char *name, char *invmatch) |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 107 | { |
| 108 | const char *value = nvram_get(name); |
Jason Cooper | 90ea229 | 2010-09-14 09:45:32 -0400 | [diff] [blame] | 109 | return value && strcmp(value, invmatch); |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Set the value of an NVRAM variable. The name and value strings are |
| 114 | * copied into private storage. Pointers to previously set values |
| 115 | * may become invalid. The new value may be immediately |
| 116 | * retrieved but will not be permanently stored until a commit. |
| 117 | * @param name name of variable to set |
| 118 | * @param value value of variable |
| 119 | * @return 0 on success and errno on failure |
| 120 | */ |
| 121 | extern int nvram_set(const char *name, const char *value); |
| 122 | |
| 123 | /* |
| 124 | * Unset an NVRAM variable. Pointers to previously set values |
| 125 | * remain valid until a set. |
| 126 | * @param name name of variable to unset |
| 127 | * @return 0 on success and errno on failure |
| 128 | * NOTE: use nvram_commit to commit this change to flash. |
| 129 | */ |
| 130 | extern int nvram_unset(const char *name); |
| 131 | |
| 132 | /* |
| 133 | * Commit NVRAM variables to permanent storage. All pointers to values |
| 134 | * may be invalid after a commit. |
| 135 | * NVRAM values are undefined after a commit. |
| 136 | * @return 0 on success and errno on failure |
| 137 | */ |
| 138 | extern int nvram_commit(void); |
| 139 | |
| 140 | /* |
| 141 | * Get all NVRAM variables (format name=value\0 ... \0\0). |
| 142 | * @param buf buffer to store variables |
| 143 | * @param count size of buffer in bytes |
| 144 | * @return 0 on success and errno on failure |
| 145 | */ |
| 146 | extern int nvram_getall(char *nvram_buf, int count); |
| 147 | |
| 148 | /* |
| 149 | * returns the crc value of the nvram |
| 150 | * @param nvh nvram header pointer |
| 151 | */ |
Greg Kroah-Hartman | de9bca6 | 2010-10-05 10:23:40 -0700 | [diff] [blame] | 152 | u8 nvram_calc_crc(struct nvram_header *nvh); |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 153 | |
| 154 | #endif /* _LANGUAGE_ASSEMBLY */ |
| 155 | |
| 156 | /* The NVRAM version number stored as an NVRAM variable */ |
| 157 | #define NVRAM_SOFTWARE_VERSION "1" |
| 158 | |
| 159 | #define NVRAM_MAGIC 0x48534C46 /* 'FLSH' */ |
| 160 | #define NVRAM_CLEAR_MAGIC 0x0 |
| 161 | #define NVRAM_INVALID_MAGIC 0xFFFFFFFF |
| 162 | #define NVRAM_VERSION 1 |
| 163 | #define NVRAM_HEADER_SIZE 20 |
| 164 | #define NVRAM_SPACE 0x8000 |
| 165 | |
| 166 | #define NVRAM_MAX_VALUE_LEN 255 |
| 167 | #define NVRAM_MAX_PARAM_LEN 64 |
| 168 | |
| 169 | #define NVRAM_CRC_START_POSITION 9 /* magic, len, crc8 to be skipped */ |
| 170 | #define NVRAM_CRC_VER_MASK 0xffffff00 /* for crc_ver_init */ |
| 171 | |
| 172 | #endif /* _bcmnvram_h_ */ |