bcm47xx_nvram.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * BCM947xx nvram variable access
  3. *
  4. * Copyright (C) 2005 Broadcom Corporation
  5. * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
  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[] = {0x6000, 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. u32 off;
  51. u32 size;
  52. if (nvram_len) {
  53. pr_warn("nvram already initialized\n");
  54. return -EEXIST;
  55. }
  56. /* TODO: when nvram is on nand flash check for bad blocks first. */
  57. off = FLASH_MIN;
  58. while (off <= lim) {
  59. /* Windowed flash access */
  60. size = find_nvram_size(iobase + off);
  61. if (size) {
  62. header = (struct nvram_header *)(iobase + off - size);
  63. goto found;
  64. }
  65. off <<= 1;
  66. }
  67. /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
  68. header = (struct nvram_header *)(iobase + 4096);
  69. if (header->magic == NVRAM_MAGIC) {
  70. size = NVRAM_SPACE;
  71. goto found;
  72. }
  73. header = (struct nvram_header *)(iobase + 1024);
  74. if (header->magic == NVRAM_MAGIC) {
  75. size = NVRAM_SPACE;
  76. goto found;
  77. }
  78. pr_err("no nvram found\n");
  79. return -ENXIO;
  80. found:
  81. __ioread32_copy(nvram_buf, header, sizeof(*header) / 4);
  82. nvram_len = ((struct nvram_header *)(nvram_buf))->len;
  83. if (nvram_len > size) {
  84. pr_err("The nvram size according to the header seems to be bigger than the partition on flash\n");
  85. nvram_len = size;
  86. }
  87. if (nvram_len >= NVRAM_SPACE) {
  88. pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
  89. nvram_len, NVRAM_SPACE - 1);
  90. nvram_len = NVRAM_SPACE - 1;
  91. }
  92. /* proceed reading data after header */
  93. __ioread32_copy(nvram_buf + sizeof(*header), header + 1,
  94. DIV_ROUND_UP(nvram_len, 4));
  95. nvram_buf[NVRAM_SPACE - 1] = '\0';
  96. return 0;
  97. }
  98. /*
  99. * On bcm47xx we need access to the NVRAM very early, so we can't use mtd
  100. * subsystem to access flash. We can't even use platform device / driver to
  101. * store memory offset.
  102. * To handle this we provide following symbol. It's supposed to be called as
  103. * soon as we get info about flash device, before any NVRAM entry is needed.
  104. */
  105. int bcm47xx_nvram_init_from_mem(u32 base, u32 lim)
  106. {
  107. void __iomem *iobase;
  108. int err;
  109. iobase = ioremap_nocache(base, lim);
  110. if (!iobase)
  111. return -ENOMEM;
  112. err = nvram_find_and_copy(iobase, lim);
  113. iounmap(iobase);
  114. return err;
  115. }
  116. static int nvram_init(void)
  117. {
  118. #ifdef CONFIG_MTD
  119. struct mtd_info *mtd;
  120. struct nvram_header header;
  121. size_t bytes_read;
  122. int err;
  123. mtd = get_mtd_device_nm("nvram");
  124. if (IS_ERR(mtd))
  125. return -ENODEV;
  126. err = mtd_read(mtd, 0, sizeof(header), &bytes_read, (uint8_t *)&header);
  127. if (!err && header.magic == NVRAM_MAGIC &&
  128. header.len > sizeof(header)) {
  129. nvram_len = header.len;
  130. if (nvram_len >= NVRAM_SPACE) {
  131. pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
  132. header.len, NVRAM_SPACE);
  133. nvram_len = NVRAM_SPACE - 1;
  134. }
  135. err = mtd_read(mtd, 0, nvram_len, &nvram_len,
  136. (u8 *)nvram_buf);
  137. return err;
  138. }
  139. #endif
  140. return -ENXIO;
  141. }
  142. int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len)
  143. {
  144. char *var, *value, *end, *eq;
  145. int err;
  146. if (!name)
  147. return -EINVAL;
  148. if (!nvram_len) {
  149. err = nvram_init();
  150. if (err)
  151. return err;
  152. }
  153. /* Look for name=value and return value */
  154. var = &nvram_buf[sizeof(struct nvram_header)];
  155. end = nvram_buf + sizeof(nvram_buf);
  156. while (var < end && *var) {
  157. eq = strchr(var, '=');
  158. if (!eq)
  159. break;
  160. value = eq + 1;
  161. if (eq - var == strlen(name) &&
  162. strncmp(var, name, eq - var) == 0)
  163. return snprintf(val, val_len, "%s", value);
  164. var = value + strlen(value) + 1;
  165. }
  166. return -ENOENT;
  167. }
  168. EXPORT_SYMBOL(bcm47xx_nvram_getenv);
  169. int bcm47xx_nvram_gpio_pin(const char *name)
  170. {
  171. int i, err;
  172. char nvram_var[] = "gpioXX";
  173. char buf[NVRAM_MAX_GPIO_VALUE_LEN];
  174. /* TODO: Optimize it to don't call getenv so many times */
  175. for (i = 0; i < NVRAM_MAX_GPIO_ENTRIES; i++) {
  176. err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
  177. if (err <= 0)
  178. continue;
  179. err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf));
  180. if (err <= 0)
  181. continue;
  182. if (!strcmp(name, buf))
  183. return i;
  184. }
  185. return -ENOENT;
  186. }
  187. EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin);
  188. char *bcm47xx_nvram_get_contents(size_t *nvram_size)
  189. {
  190. int err;
  191. char *nvram;
  192. if (!nvram_len) {
  193. err = nvram_init();
  194. if (err)
  195. return NULL;
  196. }
  197. *nvram_size = nvram_len - sizeof(struct nvram_header);
  198. nvram = vmalloc(*nvram_size);
  199. if (!nvram)
  200. return NULL;
  201. memcpy(nvram, &nvram_buf[sizeof(struct nvram_header)], *nvram_size);
  202. return nvram;
  203. }
  204. EXPORT_SYMBOL(bcm47xx_nvram_get_contents);
  205. MODULE_LICENSE("GPL v2");