nvram.c 3.3 KB

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