nvram.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 const u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
  34. static u32 find_nvram_size(void __iomem *end)
  35. {
  36. struct nvram_header __iomem *header;
  37. int i;
  38. for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
  39. header = (struct nvram_header *)(end - nvram_sizes[i]);
  40. if (header->magic == NVRAM_MAGIC)
  41. return nvram_sizes[i];
  42. }
  43. return 0;
  44. }
  45. /* Probe for NVRAM header */
  46. static int nvram_find_and_copy(void __iomem *iobase, u32 lim)
  47. {
  48. struct nvram_header __iomem *header;
  49. int i;
  50. u32 off;
  51. u32 *src, *dst;
  52. u32 size;
  53. if (nvram_buf[0]) {
  54. pr_warn("nvram already initialized\n");
  55. return -EEXIST;
  56. }
  57. /* TODO: when nvram is on nand flash check for bad blocks first. */
  58. off = FLASH_MIN;
  59. while (off <= lim) {
  60. /* Windowed flash access */
  61. size = find_nvram_size(iobase + off);
  62. if (size) {
  63. header = (struct nvram_header *)(iobase + off - size);
  64. goto found;
  65. }
  66. off <<= 1;
  67. }
  68. /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
  69. header = (struct nvram_header *)(iobase + 4096);
  70. if (header->magic == NVRAM_MAGIC) {
  71. size = NVRAM_SPACE;
  72. goto found;
  73. }
  74. header = (struct nvram_header *)(iobase + 1024);
  75. if (header->magic == NVRAM_MAGIC) {
  76. size = NVRAM_SPACE;
  77. goto found;
  78. }
  79. pr_err("no nvram found\n");
  80. return -ENXIO;
  81. found:
  82. if (header->len > size)
  83. pr_err("The nvram size accoridng to the header seems to be bigger than the partition on flash\n");
  84. if (header->len > NVRAM_SPACE)
  85. pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
  86. header->len, NVRAM_SPACE);
  87. src = (u32 *)header;
  88. dst = (u32 *)nvram_buf;
  89. for (i = 0; i < sizeof(struct nvram_header); i += 4)
  90. *dst++ = __raw_readl(src++);
  91. for (; i < header->len && i < NVRAM_SPACE && i < size; i += 4)
  92. *dst++ = readl(src++);
  93. return 0;
  94. }
  95. /*
  96. * On bcm47xx we need access to the NVRAM very early, so we can't use mtd
  97. * subsystem to access flash. We can't even use platform device / driver to
  98. * store memory offset.
  99. * To handle this we provide following symbol. It's supposed to be called as
  100. * soon as we get info about flash device, before any NVRAM entry is needed.
  101. */
  102. int bcm47xx_nvram_init_from_mem(u32 base, u32 lim)
  103. {
  104. void __iomem *iobase;
  105. int err;
  106. iobase = ioremap_nocache(base, lim);
  107. if (!iobase)
  108. return -ENOMEM;
  109. err = nvram_find_and_copy(iobase, lim);
  110. iounmap(iobase);
  111. return err;
  112. }
  113. static int nvram_init(void)
  114. {
  115. #ifdef CONFIG_MTD
  116. struct mtd_info *mtd;
  117. struct nvram_header header;
  118. size_t bytes_read;
  119. int err;
  120. mtd = get_mtd_device_nm("nvram");
  121. if (IS_ERR(mtd))
  122. return -ENODEV;
  123. err = mtd_read(mtd, 0, sizeof(header), &bytes_read, (uint8_t *)&header);
  124. if (!err && header.magic == NVRAM_MAGIC) {
  125. u8 *dst = (uint8_t *)nvram_buf;
  126. size_t len = header.len;
  127. if (header.len > NVRAM_SPACE) {
  128. pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
  129. header.len, NVRAM_SPACE);
  130. len = NVRAM_SPACE;
  131. }
  132. err = mtd_read(mtd, 0, len, &bytes_read, dst);
  133. if (err)
  134. return err;
  135. return 0;
  136. }
  137. #endif
  138. return -ENXIO;
  139. }
  140. int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len)
  141. {
  142. char *var, *value, *end, *eq;
  143. int data_left, err;
  144. if (!name)
  145. return -EINVAL;
  146. if (!nvram_buf[0]) {
  147. err = nvram_init();
  148. if (err)
  149. return err;
  150. }
  151. /* Look for name=value and return value */
  152. var = &nvram_buf[sizeof(struct nvram_header)];
  153. end = nvram_buf + sizeof(nvram_buf) - 2;
  154. end[0] = '\0';
  155. end[1] = '\0';
  156. for (; *var; var = value + strlen(value) + 1) {
  157. data_left = end - var;
  158. eq = strnchr(var, data_left, '=');
  159. if (!eq)
  160. break;
  161. value = eq + 1;
  162. if (eq - var == strlen(name) &&
  163. strncmp(var, name, eq - var) == 0)
  164. return snprintf(val, val_len, "%s", value);
  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);