bcm47xx_nvram.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * BCM947xx nvram variable access
  3. *
  4. * Copyright (C) 2005 Broadcom Corporation
  5. * Copyright (C) 2006 Felix Fietkau <nbd@nbd.name>
  6. * Copyright (C) 2010-2012 Hauke Mehrtens <hauke@hauke-m.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/io.h>
  14. #include <linux/types.h>
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/string.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/bcm47xx_nvram.h>
  20. #define NVRAM_MAGIC 0x48534C46 /* 'FLSH' */
  21. #define NVRAM_SPACE 0x10000
  22. #define NVRAM_MAX_GPIO_ENTRIES 32
  23. #define NVRAM_MAX_GPIO_VALUE_LEN 30
  24. #define FLASH_MIN 0x00020000 /* Minimum flash size */
  25. struct nvram_header {
  26. u32 magic;
  27. u32 len;
  28. u32 crc_ver_init; /* 0:7 crc, 8:15 ver, 16:31 sdram_init */
  29. u32 config_refresh; /* 0:15 sdram_config, 16:31 sdram_refresh */
  30. u32 config_ncdl; /* ncdl values for memc */
  31. };
  32. static char nvram_buf[NVRAM_SPACE];
  33. static size_t nvram_len;
  34. static const u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
  35. static u32 find_nvram_size(void __iomem *end)
  36. {
  37. struct nvram_header __iomem *header;
  38. int i;
  39. for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
  40. header = (struct nvram_header *)(end - nvram_sizes[i]);
  41. if (header->magic == NVRAM_MAGIC)
  42. return nvram_sizes[i];
  43. }
  44. return 0;
  45. }
  46. /* Probe for NVRAM header */
  47. static int nvram_find_and_copy(void __iomem *iobase, u32 lim)
  48. {
  49. struct nvram_header __iomem *header;
  50. int i;
  51. u32 off;
  52. u32 *src, *dst;
  53. u32 size;
  54. if (nvram_len) {
  55. pr_warn("nvram already initialized\n");
  56. return -EEXIST;
  57. }
  58. /* TODO: when nvram is on nand flash check for bad blocks first. */
  59. off = FLASH_MIN;
  60. while (off <= lim) {
  61. /* Windowed flash access */
  62. size = find_nvram_size(iobase + off);
  63. if (size) {
  64. header = (struct nvram_header *)(iobase + off - size);
  65. goto found;
  66. }
  67. off <<= 1;
  68. }
  69. /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
  70. header = (struct nvram_header *)(iobase + 4096);
  71. if (header->magic == NVRAM_MAGIC) {
  72. size = NVRAM_SPACE;
  73. goto found;
  74. }
  75. header = (struct nvram_header *)(iobase + 1024);
  76. if (header->magic == NVRAM_MAGIC) {
  77. size = NVRAM_SPACE;
  78. goto found;
  79. }
  80. pr_err("no nvram found\n");
  81. return -ENXIO;
  82. found:
  83. src = (u32 *)header;
  84. dst = (u32 *)nvram_buf;
  85. for (i = 0; i < sizeof(struct nvram_header); i += 4)
  86. *dst++ = __raw_readl(src++);
  87. header = (struct nvram_header *)nvram_buf;
  88. nvram_len = header->len;
  89. if (nvram_len > size) {
  90. pr_err("The nvram size according to the header seems to be bigger than the partition on flash\n");
  91. nvram_len = size;
  92. }
  93. if (nvram_len >= NVRAM_SPACE) {
  94. pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
  95. header->len, NVRAM_SPACE - 1);
  96. nvram_len = NVRAM_SPACE - 1;
  97. }
  98. /* proceed reading data after header */
  99. for (; i < nvram_len; i += 4)
  100. *dst++ = readl(src++);
  101. nvram_buf[NVRAM_SPACE - 1] = '\0';
  102. return 0;
  103. }
  104. /*
  105. * On bcm47xx we need access to the NVRAM very early, so we can't use mtd
  106. * subsystem to access flash. We can't even use platform device / driver to
  107. * store memory offset.
  108. * To handle this we provide following symbol. It's supposed to be called as
  109. * soon as we get info about flash device, before any NVRAM entry is needed.
  110. */
  111. int bcm47xx_nvram_init_from_mem(u32 base, u32 lim)
  112. {
  113. void __iomem *iobase;
  114. int err;
  115. iobase = ioremap_nocache(base, lim);
  116. if (!iobase)
  117. return -ENOMEM;
  118. err = nvram_find_and_copy(iobase, lim);
  119. iounmap(iobase);
  120. return err;
  121. }
  122. static int nvram_init(void)
  123. {
  124. #ifdef CONFIG_MTD
  125. struct mtd_info *mtd;
  126. struct nvram_header header;
  127. size_t bytes_read;
  128. int err;
  129. mtd = get_mtd_device_nm("nvram");
  130. if (IS_ERR(mtd))
  131. return -ENODEV;
  132. err = mtd_read(mtd, 0, sizeof(header), &bytes_read, (uint8_t *)&header);
  133. if (!err && header.magic == NVRAM_MAGIC &&
  134. header.len > sizeof(header)) {
  135. nvram_len = header.len;
  136. if (nvram_len >= NVRAM_SPACE) {
  137. pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
  138. header.len, NVRAM_SPACE);
  139. nvram_len = NVRAM_SPACE - 1;
  140. }
  141. err = mtd_read(mtd, 0, nvram_len, &nvram_len,
  142. (u8 *)nvram_buf);
  143. return err;
  144. }
  145. #endif
  146. return -ENXIO;
  147. }
  148. int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len)
  149. {
  150. char *var, *value, *end, *eq;
  151. int err;
  152. if (!name)
  153. return -EINVAL;
  154. if (!nvram_len) {
  155. err = nvram_init();
  156. if (err)
  157. return err;
  158. }
  159. /* Look for name=value and return value */
  160. var = &nvram_buf[sizeof(struct nvram_header)];
  161. end = nvram_buf + sizeof(nvram_buf);
  162. while (var < end && *var) {
  163. eq = strchr(var, '=');
  164. if (!eq)
  165. break;
  166. value = eq + 1;
  167. if (eq - var == strlen(name) &&
  168. strncmp(var, name, eq - var) == 0)
  169. return snprintf(val, val_len, "%s", value);
  170. var = value + strlen(value) + 1;
  171. }
  172. return -ENOENT;
  173. }
  174. EXPORT_SYMBOL(bcm47xx_nvram_getenv);
  175. int bcm47xx_nvram_gpio_pin(const char *name)
  176. {
  177. int i, err;
  178. char nvram_var[] = "gpioXX";
  179. char buf[NVRAM_MAX_GPIO_VALUE_LEN];
  180. /* TODO: Optimize it to don't call getenv so many times */
  181. for (i = 0; i < NVRAM_MAX_GPIO_ENTRIES; i++) {
  182. err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
  183. if (err <= 0)
  184. continue;
  185. err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf));
  186. if (err <= 0)
  187. continue;
  188. if (!strcmp(name, buf))
  189. return i;
  190. }
  191. return -ENOENT;
  192. }
  193. EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin);
  194. char *bcm47xx_nvram_get_contents(size_t *nvram_size)
  195. {
  196. int err;
  197. char *nvram;
  198. if (!nvram_len) {
  199. err = nvram_init();
  200. if (err)
  201. return NULL;
  202. }
  203. *nvram_size = nvram_len - sizeof(struct nvram_header);
  204. nvram = vmalloc(*nvram_size);
  205. if (!nvram)
  206. return NULL;
  207. memcpy(nvram, &nvram_buf[sizeof(struct nvram_header)], *nvram_size);
  208. return nvram;
  209. }
  210. EXPORT_SYMBOL(bcm47xx_nvram_get_contents);
  211. MODULE_LICENSE("GPLv2");