nvram.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * BCM947xx nvram variable access
  3. *
  4. * Copyright 2005, Broadcom Corporation
  5. * Copyright 2006, Felix Fietkau <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/ssb/ssb.h>
  15. #include <linux/kernel.h>
  16. #include <linux/string.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/slab.h>
  20. #include <asm/byteorder.h>
  21. #include <asm/bootinfo.h>
  22. #include <asm/addrspace.h>
  23. #include <asm/io.h>
  24. #include <asm/uaccess.h>
  25. #include <nvram.h>
  26. #define MB * 1048576
  27. extern struct ssb_bus ssb;
  28. static char nvram_buf[NVRAM_SPACE];
  29. static int cfe_env;
  30. extern char *cfe_env_get(char *nv_buf, const char *name);
  31. /* Probe for NVRAM header */
  32. static void __init early_nvram_init(void)
  33. {
  34. struct ssb_mipscore *mcore = &ssb.mipscore;
  35. struct nvram_header *header;
  36. int i;
  37. u32 base, lim, off;
  38. u32 *src, *dst;
  39. base = mcore->flash_window;
  40. lim = mcore->flash_window_size;
  41. cfe_env = 0;
  42. /* XXX: hack for supporting the CFE environment stuff on WGT634U */
  43. if (lim >= 8 MB) {
  44. src = (u32 *) KSEG1ADDR(base + 8 MB - 0x2000);
  45. dst = (u32 *) nvram_buf;
  46. if ((*src & 0xff00ff) == 0x000001) {
  47. printk("early_nvram_init: WGT634U NVRAM found.\n");
  48. for (i = 0; i < 0x1ff0; i++) {
  49. if (*src == 0xFFFFFFFF)
  50. break;
  51. *dst++ = *src++;
  52. }
  53. cfe_env = 1;
  54. return;
  55. }
  56. }
  57. off = 0x20000;
  58. while (off <= lim) {
  59. /* Windowed flash access */
  60. header = (struct nvram_header *) KSEG1ADDR(base + off - NVRAM_SPACE);
  61. if (header->magic == NVRAM_HEADER)
  62. goto found;
  63. off <<= 1;
  64. }
  65. /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
  66. header = (struct nvram_header *) KSEG1ADDR(base + 4096);
  67. if (header->magic == NVRAM_HEADER)
  68. goto found;
  69. header = (struct nvram_header *) KSEG1ADDR(base + 1024);
  70. if (header->magic == NVRAM_HEADER)
  71. goto found;
  72. return;
  73. found:
  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 += 4)
  79. *dst++ = le32_to_cpu(*src++);
  80. }
  81. char *nvram_get(const char *name)
  82. {
  83. char *var, *value, *end, *eq;
  84. if (!name)
  85. return NULL;
  86. if (!nvram_buf[0])
  87. early_nvram_init();
  88. if (cfe_env)
  89. return cfe_env_get(nvram_buf, name);
  90. /* Look for name=value and return value */
  91. var = &nvram_buf[sizeof(struct nvram_header)];
  92. end = nvram_buf + sizeof(nvram_buf) - 2;
  93. end[0] = end[1] = '\0';
  94. for (; *var; var = value + strlen(value) + 1) {
  95. if (!(eq = strchr(var, '=')))
  96. break;
  97. value = eq + 1;
  98. if ((eq - var) == strlen(name) && strncmp(var, name, (eq - var)) == 0)
  99. return value;
  100. }
  101. return NULL;
  102. }
  103. EXPORT_SYMBOL(nvram_get);