nvram.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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/types.h>
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/string.h>
  17. #include <linux/mtd/mtd.h>
  18. #include <linux/bcm47xx_nvram.h>
  19. #define NVRAM_MAGIC 0x48534C46 /* 'FLSH' */
  20. #define NVRAM_SPACE 0x8000
  21. #define NVRAM_MAX_GPIO_ENTRIES 32
  22. #define NVRAM_MAX_GPIO_VALUE_LEN 30
  23. #define FLASH_MIN 0x00020000 /* Minimum flash size */
  24. struct nvram_header {
  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 */
  30. };
  31. static char nvram_buf[NVRAM_SPACE];
  32. static const u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
  33. static u32 find_nvram_size(void __iomem *end)
  34. {
  35. struct nvram_header __iomem *header;
  36. int i;
  37. for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
  38. header = (struct nvram_header *)(end - nvram_sizes[i]);
  39. if (header->magic == NVRAM_MAGIC)
  40. return nvram_sizes[i];
  41. }
  42. return 0;
  43. }
  44. /* Probe for NVRAM header */
  45. static int nvram_find_and_copy(void __iomem *iobase, u32 lim)
  46. {
  47. struct nvram_header __iomem *header;
  48. int i;
  49. u32 off;
  50. u32 *src, *dst;
  51. u32 size;
  52. if (nvram_buf[0]) {
  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. if (header->len > size)
  82. pr_err("The nvram size accoridng to the header seems to be bigger than the partition on flash\n");
  83. if (header->len > NVRAM_SPACE)
  84. pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
  85. header->len, NVRAM_SPACE);
  86. src = (u32 *)header;
  87. dst = (u32 *)nvram_buf;
  88. for (i = 0; i < sizeof(struct nvram_header); i += 4)
  89. *dst++ = __raw_readl(src++);
  90. for (; i < header->len && i < NVRAM_SPACE && i < size; i += 4)
  91. *dst++ = readl(src++);
  92. return 0;
  93. }
  94. /*
  95. * On bcm47xx we need access to the NVRAM very early, so we can't use mtd
  96. * subsystem to access flash. We can't even use platform device / driver to
  97. * store memory offset.
  98. * To handle this we provide following symbol. It's supposed to be called as
  99. * soon as we get info about flash device, before any NVRAM entry is needed.
  100. */
  101. int bcm47xx_nvram_init_from_mem(u32 base, u32 lim)
  102. {
  103. void __iomem *iobase;
  104. int err;
  105. iobase = ioremap_nocache(base, lim);
  106. if (!iobase)
  107. return -ENOMEM;
  108. err = nvram_find_and_copy(iobase, lim);
  109. iounmap(iobase);
  110. return err;
  111. }
  112. static int nvram_init(void)
  113. {
  114. #ifdef CONFIG_MTD
  115. struct mtd_info *mtd;
  116. struct nvram_header header;
  117. size_t bytes_read;
  118. int err, i;
  119. mtd = get_mtd_device_nm("nvram");
  120. if (IS_ERR(mtd))
  121. return -ENODEV;
  122. for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
  123. loff_t from = mtd->size - nvram_sizes[i];
  124. if (from < 0)
  125. continue;
  126. err = mtd_read(mtd, from, sizeof(header), &bytes_read,
  127. (uint8_t *)&header);
  128. if (!err && header.magic == NVRAM_MAGIC) {
  129. u8 *dst = (uint8_t *)nvram_buf;
  130. size_t len = header.len;
  131. if (header.len > NVRAM_SPACE) {
  132. pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
  133. header.len, NVRAM_SPACE);
  134. len = NVRAM_SPACE;
  135. }
  136. err = mtd_read(mtd, from, len, &bytes_read, dst);
  137. if (err)
  138. return err;
  139. return 0;
  140. }
  141. }
  142. #endif
  143. return -ENXIO;
  144. }
  145. int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len)
  146. {
  147. char *var, *value, *end, *eq;
  148. int data_left, err;
  149. if (!name)
  150. return -EINVAL;
  151. if (!nvram_buf[0]) {
  152. err = nvram_init();
  153. if (err)
  154. return err;
  155. }
  156. /* Look for name=value and return value */
  157. var = &nvram_buf[sizeof(struct nvram_header)];
  158. end = nvram_buf + sizeof(nvram_buf) - 2;
  159. end[0] = '\0';
  160. end[1] = '\0';
  161. for (; *var; var = value + strlen(value) + 1) {
  162. data_left = end - var;
  163. eq = strnchr(var, data_left, '=');
  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. }
  171. return -ENOENT;
  172. }
  173. EXPORT_SYMBOL(bcm47xx_nvram_getenv);
  174. int bcm47xx_nvram_gpio_pin(const char *name)
  175. {
  176. int i, err;
  177. char nvram_var[] = "gpioXX";
  178. char buf[NVRAM_MAX_GPIO_VALUE_LEN];
  179. /* TODO: Optimize it to don't call getenv so many times */
  180. for (i = 0; i < NVRAM_MAX_GPIO_ENTRIES; i++) {
  181. err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
  182. if (err <= 0)
  183. continue;
  184. err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf));
  185. if (err <= 0)
  186. continue;
  187. if (!strcmp(name, buf))
  188. return i;
  189. }
  190. return -ENOENT;
  191. }
  192. EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin);