nvram.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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/ssb/ssb.h>
  16. #include <linux/kernel.h>
  17. #include <linux/string.h>
  18. #include <asm/addrspace.h>
  19. #include <bcm47xx_nvram.h>
  20. #include <asm/mach-bcm47xx/bcm47xx.h>
  21. static char nvram_buf[NVRAM_SPACE];
  22. static const u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
  23. static u32 find_nvram_size(u32 end)
  24. {
  25. struct nvram_header *header;
  26. int i;
  27. for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
  28. header = (struct nvram_header *)KSEG1ADDR(end - nvram_sizes[i]);
  29. if (header->magic == NVRAM_HEADER)
  30. return nvram_sizes[i];
  31. }
  32. return 0;
  33. }
  34. /* Probe for NVRAM header */
  35. static int nvram_find_and_copy(u32 base, u32 lim)
  36. {
  37. struct nvram_header *header;
  38. int i;
  39. u32 off;
  40. u32 *src, *dst;
  41. u32 size;
  42. /* TODO: when nvram is on nand flash check for bad blocks first. */
  43. off = FLASH_MIN;
  44. while (off <= lim) {
  45. /* Windowed flash access */
  46. size = find_nvram_size(base + off);
  47. if (size) {
  48. header = (struct nvram_header *)KSEG1ADDR(base + off -
  49. size);
  50. goto found;
  51. }
  52. off <<= 1;
  53. }
  54. /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
  55. header = (struct nvram_header *) KSEG1ADDR(base + 4096);
  56. if (header->magic == NVRAM_HEADER) {
  57. size = NVRAM_SPACE;
  58. goto found;
  59. }
  60. header = (struct nvram_header *) KSEG1ADDR(base + 1024);
  61. if (header->magic == NVRAM_HEADER) {
  62. size = NVRAM_SPACE;
  63. goto found;
  64. }
  65. pr_err("no nvram found\n");
  66. return -ENXIO;
  67. found:
  68. if (header->len > size)
  69. pr_err("The nvram size accoridng to the header seems to be bigger than the partition on flash\n");
  70. if (header->len > NVRAM_SPACE)
  71. pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
  72. header->len, NVRAM_SPACE);
  73. src = (u32 *) header;
  74. dst = (u32 *) nvram_buf;
  75. for (i = 0; i < sizeof(struct nvram_header); i += 4)
  76. *dst++ = *src++;
  77. for (; i < header->len && i < NVRAM_SPACE && i < size; i += 4)
  78. *dst++ = le32_to_cpu(*src++);
  79. memset(dst, 0x0, NVRAM_SPACE - i);
  80. return 0;
  81. }
  82. #ifdef CONFIG_BCM47XX_SSB
  83. static int nvram_init_ssb(void)
  84. {
  85. struct ssb_mipscore *mcore = &bcm47xx_bus.ssb.mipscore;
  86. u32 base;
  87. u32 lim;
  88. if (mcore->pflash.present) {
  89. base = mcore->pflash.window;
  90. lim = mcore->pflash.window_size;
  91. } else {
  92. pr_err("Couldn't find supported flash memory\n");
  93. return -ENXIO;
  94. }
  95. return nvram_find_and_copy(base, lim);
  96. }
  97. #endif
  98. #ifdef CONFIG_BCM47XX_BCMA
  99. static int nvram_init_bcma(void)
  100. {
  101. struct bcma_drv_cc *cc = &bcm47xx_bus.bcma.bus.drv_cc;
  102. u32 base;
  103. u32 lim;
  104. #ifdef CONFIG_BCMA_NFLASH
  105. if (cc->nflash.boot) {
  106. base = BCMA_SOC_FLASH1;
  107. lim = BCMA_SOC_FLASH1_SZ;
  108. } else
  109. #endif
  110. if (cc->pflash.present) {
  111. base = cc->pflash.window;
  112. lim = cc->pflash.window_size;
  113. #ifdef CONFIG_BCMA_SFLASH
  114. } else if (cc->sflash.present) {
  115. base = cc->sflash.window;
  116. lim = cc->sflash.size;
  117. #endif
  118. } else {
  119. pr_err("Couldn't find supported flash memory\n");
  120. return -ENXIO;
  121. }
  122. return nvram_find_and_copy(base, lim);
  123. }
  124. #endif
  125. static int nvram_init(void)
  126. {
  127. switch (bcm47xx_bus_type) {
  128. #ifdef CONFIG_BCM47XX_SSB
  129. case BCM47XX_BUS_TYPE_SSB:
  130. return nvram_init_ssb();
  131. #endif
  132. #ifdef CONFIG_BCM47XX_BCMA
  133. case BCM47XX_BUS_TYPE_BCMA:
  134. return nvram_init_bcma();
  135. #endif
  136. }
  137. return -ENXIO;
  138. }
  139. int bcm47xx_nvram_getenv(char *name, char *val, size_t val_len)
  140. {
  141. char *var, *value, *end, *eq;
  142. int err;
  143. if (!name)
  144. return -EINVAL;
  145. if (!nvram_buf[0]) {
  146. err = nvram_init();
  147. if (err)
  148. return err;
  149. }
  150. /* Look for name=value and return value */
  151. var = &nvram_buf[sizeof(struct nvram_header)];
  152. end = nvram_buf + sizeof(nvram_buf) - 2;
  153. end[0] = end[1] = '\0';
  154. for (; *var; var = value + strlen(value) + 1) {
  155. eq = strchr(var, '=');
  156. if (!eq)
  157. break;
  158. value = eq + 1;
  159. if ((eq - var) == strlen(name) &&
  160. strncmp(var, name, (eq - var)) == 0) {
  161. return snprintf(val, val_len, "%s", value);
  162. }
  163. }
  164. return -ENOENT;
  165. }
  166. EXPORT_SYMBOL(bcm47xx_nvram_getenv);
  167. int bcm47xx_nvram_gpio_pin(const char *name)
  168. {
  169. int i, err;
  170. char nvram_var[10];
  171. char buf[30];
  172. for (i = 0; i < 32; i++) {
  173. err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
  174. if (err <= 0)
  175. continue;
  176. err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf));
  177. if (err <= 0)
  178. continue;
  179. if (!strcmp(name, buf))
  180. return i;
  181. }
  182. return -ENOENT;
  183. }
  184. EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin);