nvram.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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-2011 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 <asm/mach-bcm47xx/nvram.h>
  21. #include <asm/mach-bcm47xx/bcm47xx.h>
  22. static char nvram_buf[NVRAM_SPACE];
  23. static int nvram_find_and_copy(u32 base, u32 lim)
  24. {
  25. struct nvram_header *header;
  26. int i;
  27. u32 off;
  28. u32 *src, *dst;
  29. /* TODO: when nvram is on nand flash check for bad blocks first. */
  30. off = FLASH_MIN;
  31. while (off <= lim) {
  32. /* Windowed flash access */
  33. header = (struct nvram_header *)
  34. KSEG1ADDR(base + off - NVRAM_SPACE);
  35. if (header->magic == NVRAM_HEADER)
  36. goto found;
  37. off <<= 1;
  38. }
  39. /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
  40. header = (struct nvram_header *) KSEG1ADDR(base + 4096);
  41. if (header->magic == NVRAM_HEADER)
  42. goto found;
  43. header = (struct nvram_header *) KSEG1ADDR(base + 1024);
  44. if (header->magic == NVRAM_HEADER)
  45. goto found;
  46. return -ENXIO;
  47. found:
  48. src = (u32 *) header;
  49. dst = (u32 *) nvram_buf;
  50. for (i = 0; i < sizeof(struct nvram_header); i += 4)
  51. *dst++ = *src++;
  52. for (; i < header->len && i < NVRAM_SPACE; i += 4)
  53. *dst++ = le32_to_cpu(*src++);
  54. return 0;
  55. }
  56. #ifdef CONFIG_BCM47XX_SSB
  57. static int nvram_init_ssb(void)
  58. {
  59. struct ssb_mipscore *mcore = &bcm47xx_bus.ssb.mipscore;
  60. u32 base;
  61. u32 lim;
  62. if (mcore->pflash.present) {
  63. base = mcore->pflash.window;
  64. lim = mcore->pflash.window_size;
  65. } else {
  66. pr_err("Couldn't find supported flash memory\n");
  67. return -ENXIO;
  68. }
  69. return nvram_find_and_copy(base, lim);
  70. }
  71. #endif
  72. #ifdef CONFIG_BCM47XX_BCMA
  73. static int nvram_init_bcma(void)
  74. {
  75. struct bcma_drv_cc *cc = &bcm47xx_bus.bcma.bus.drv_cc;
  76. u32 base;
  77. u32 lim;
  78. #ifdef CONFIG_BCMA_NFLASH
  79. if (cc->nflash.boot) {
  80. base = BCMA_SOC_FLASH1;
  81. lim = BCMA_SOC_FLASH1_SZ;
  82. } else
  83. #endif
  84. if (cc->pflash.present) {
  85. base = cc->pflash.window;
  86. lim = cc->pflash.window_size;
  87. #ifdef CONFIG_BCMA_SFLASH
  88. } else if (cc->sflash.present) {
  89. base = cc->sflash.window;
  90. lim = cc->sflash.size;
  91. #endif
  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. static int early_nvram_init(void)
  100. {
  101. switch (bcm47xx_bus_type) {
  102. #ifdef CONFIG_BCM47XX_SSB
  103. case BCM47XX_BUS_TYPE_SSB:
  104. return nvram_init_ssb();
  105. #endif
  106. #ifdef CONFIG_BCM47XX_BCMA
  107. case BCM47XX_BUS_TYPE_BCMA:
  108. return nvram_init_bcma();
  109. #endif
  110. }
  111. return -ENXIO;
  112. }
  113. int nvram_getenv(char *name, char *val, size_t val_len)
  114. {
  115. char *var, *value, *end, *eq;
  116. int err;
  117. if (!name)
  118. return -EINVAL;
  119. if (!nvram_buf[0]) {
  120. err = early_nvram_init();
  121. if (err)
  122. return err;
  123. }
  124. /* Look for name=value and return value */
  125. var = &nvram_buf[sizeof(struct nvram_header)];
  126. end = nvram_buf + sizeof(nvram_buf) - 2;
  127. end[0] = end[1] = '\0';
  128. for (; *var; var = value + strlen(value) + 1) {
  129. eq = strchr(var, '=');
  130. if (!eq)
  131. break;
  132. value = eq + 1;
  133. if ((eq - var) == strlen(name) &&
  134. strncmp(var, name, (eq - var)) == 0) {
  135. return snprintf(val, val_len, "%s", value);
  136. }
  137. }
  138. return -ENOENT;
  139. }
  140. EXPORT_SYMBOL(nvram_getenv);