nvram.c 4.3 KB

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