prep_nvram.c 2.7 KB

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