nvram.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 void 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;
  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. }
  54. #ifdef CONFIG_BCM47XX_SSB
  55. static void nvram_init_ssb(void)
  56. {
  57. struct ssb_mipscore *mcore = &bcm47xx_bus.ssb.mipscore;
  58. u32 base;
  59. u32 lim;
  60. if (mcore->pflash.present) {
  61. base = mcore->pflash.window;
  62. lim = mcore->pflash.window_size;
  63. } else {
  64. pr_err("Couldn't find supported flash memory\n");
  65. return;
  66. }
  67. nvram_find_and_copy(base, lim);
  68. }
  69. #endif
  70. #ifdef CONFIG_BCM47XX_BCMA
  71. static void nvram_init_bcma(void)
  72. {
  73. struct bcma_drv_cc *cc = &bcm47xx_bus.bcma.bus.drv_cc;
  74. u32 base;
  75. u32 lim;
  76. if (cc->pflash.present) {
  77. base = cc->pflash.window;
  78. lim = cc->pflash.window_size;
  79. #ifdef CONFIG_BCMA_SFLASH
  80. } else if (cc->sflash.present) {
  81. base = cc->sflash.window;
  82. lim = cc->sflash.size;
  83. #endif
  84. } else {
  85. pr_err("Couldn't find supported flash memory\n");
  86. return;
  87. }
  88. nvram_find_and_copy(base, lim);
  89. }
  90. #endif
  91. static void early_nvram_init(void)
  92. {
  93. switch (bcm47xx_bus_type) {
  94. #ifdef CONFIG_BCM47XX_SSB
  95. case BCM47XX_BUS_TYPE_SSB:
  96. nvram_init_ssb();
  97. break;
  98. #endif
  99. #ifdef CONFIG_BCM47XX_BCMA
  100. case BCM47XX_BUS_TYPE_BCMA:
  101. nvram_init_bcma();
  102. break;
  103. #endif
  104. }
  105. }
  106. int nvram_getenv(char *name, char *val, size_t val_len)
  107. {
  108. char *var, *value, *end, *eq;
  109. if (!name)
  110. return -EINVAL;
  111. if (!nvram_buf[0])
  112. early_nvram_init();
  113. /* Look for name=value and return value */
  114. var = &nvram_buf[sizeof(struct nvram_header)];
  115. end = nvram_buf + sizeof(nvram_buf) - 2;
  116. end[0] = end[1] = '\0';
  117. for (; *var; var = value + strlen(value) + 1) {
  118. eq = strchr(var, '=');
  119. if (!eq)
  120. break;
  121. value = eq + 1;
  122. if ((eq - var) == strlen(name) &&
  123. strncmp(var, name, (eq - var)) == 0) {
  124. return snprintf(val, val_len, "%s", value);
  125. }
  126. }
  127. return -ENOENT;
  128. }
  129. EXPORT_SYMBOL(nvram_getenv);