prep_nvram.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (C) 1998 Corey Minyard
  3. *
  4. * This reads the NvRAM on PReP compliant machines (generally from IBM or
  5. * Motorola). Motorola kept the format of NvRAM in their ROM, PPCBUG, the
  6. * same, long after they had stopped producing PReP compliant machines. So
  7. * this code is useful in those cases as well.
  8. *
  9. */
  10. #include <linux/init.h>
  11. #include <linux/delay.h>
  12. #include <linux/slab.h>
  13. #include <linux/ioport.h>
  14. #include <asm/sections.h>
  15. #include <asm/io.h>
  16. #include <asm/machdep.h>
  17. #include <asm/prep_nvram.h>
  18. static char nvramData[MAX_PREP_NVRAM];
  19. static NVRAM_MAP *nvram=(NVRAM_MAP *)&nvramData[0];
  20. unsigned char prep_nvram_read_val(int addr)
  21. {
  22. outb(addr, PREP_NVRAM_AS0);
  23. outb(addr>>8, PREP_NVRAM_AS1);
  24. return inb(PREP_NVRAM_DATA);
  25. }
  26. void prep_nvram_write_val(int addr,
  27. unsigned char val)
  28. {
  29. outb(addr, PREP_NVRAM_AS0);
  30. outb(addr>>8, PREP_NVRAM_AS1);
  31. outb(val, PREP_NVRAM_DATA);
  32. }
  33. void __init init_prep_nvram(void)
  34. {
  35. unsigned char *nvp;
  36. int i;
  37. int nvramSize;
  38. /*
  39. * The following could fail if the NvRAM were corrupt but
  40. * we expect the boot firmware to have checked its checksum
  41. * before boot
  42. */
  43. nvp = (char *) &nvram->Header;
  44. for (i=0; i<sizeof(HEADER); i++)
  45. {
  46. *nvp = ppc_md.nvram_read_val(i);
  47. nvp++;
  48. }
  49. /*
  50. * The PReP NvRAM may be any size so read in the header to
  51. * determine how much we must read in order to get the complete
  52. * GE area
  53. */
  54. nvramSize=(int)nvram->Header.GEAddress+nvram->Header.GELength;
  55. if(nvramSize>MAX_PREP_NVRAM)
  56. {
  57. /*
  58. * NvRAM is too large
  59. */
  60. nvram->Header.GELength=0;
  61. return;
  62. }
  63. /*
  64. * Read the remainder of the PReP NvRAM
  65. */
  66. nvp = (char *) &nvram->GEArea[0];
  67. for (i=sizeof(HEADER); i<nvramSize; i++)
  68. {
  69. *nvp = ppc_md.nvram_read_val(i);
  70. nvp++;
  71. }
  72. }
  73. char *prep_nvram_get_var(const char *name)
  74. {
  75. char *cp;
  76. int namelen;
  77. namelen = strlen(name);
  78. cp = prep_nvram_first_var();
  79. while (cp != NULL) {
  80. if ((strncmp(name, cp, namelen) == 0)
  81. && (cp[namelen] == '='))
  82. {
  83. return cp+namelen+1;
  84. }
  85. cp = prep_nvram_next_var(cp);
  86. }
  87. return NULL;
  88. }
  89. char *prep_nvram_first_var(void)
  90. {
  91. if (nvram->Header.GELength == 0) {
  92. return NULL;
  93. } else {
  94. return (((char *)nvram)
  95. + ((unsigned int) nvram->Header.GEAddress));
  96. }
  97. }
  98. char *prep_nvram_next_var(char *name)
  99. {
  100. char *cp;
  101. cp = name;
  102. while (((cp - ((char *) nvram->GEArea)) < nvram->Header.GELength)
  103. && (*cp != '\0'))
  104. {
  105. cp++;
  106. }
  107. /* Skip over any null characters. */
  108. while (((cp - ((char *) nvram->GEArea)) < nvram->Header.GELength)
  109. && (*cp == '\0'))
  110. {
  111. cp++;
  112. }
  113. if ((cp - ((char *) nvram->GEArea)) < nvram->Header.GELength) {
  114. return cp;
  115. } else {
  116. return NULL;
  117. }
  118. }