prep_nvram.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/segment.h>
  18. #include <asm/io.h>
  19. #include <asm/machdep.h>
  20. #include <asm/prep_nvram.h>
  21. static char nvramData[MAX_PREP_NVRAM];
  22. static NVRAM_MAP *nvram=(NVRAM_MAP *)&nvramData[0];
  23. unsigned char __prep prep_nvram_read_val(int addr)
  24. {
  25. outb(addr, PREP_NVRAM_AS0);
  26. outb(addr>>8, PREP_NVRAM_AS1);
  27. return inb(PREP_NVRAM_DATA);
  28. }
  29. void __prep prep_nvram_write_val(int addr,
  30. unsigned char val)
  31. {
  32. outb(addr, PREP_NVRAM_AS0);
  33. outb(addr>>8, PREP_NVRAM_AS1);
  34. outb(val, PREP_NVRAM_DATA);
  35. }
  36. void __init init_prep_nvram(void)
  37. {
  38. unsigned char *nvp;
  39. int i;
  40. int nvramSize;
  41. /*
  42. * The following could fail if the NvRAM were corrupt but
  43. * we expect the boot firmware to have checked its checksum
  44. * before boot
  45. */
  46. nvp = (char *) &nvram->Header;
  47. for (i=0; i<sizeof(HEADER); i++)
  48. {
  49. *nvp = ppc_md.nvram_read_val(i);
  50. nvp++;
  51. }
  52. /*
  53. * The PReP NvRAM may be any size so read in the header to
  54. * determine how much we must read in order to get the complete
  55. * GE area
  56. */
  57. nvramSize=(int)nvram->Header.GEAddress+nvram->Header.GELength;
  58. if(nvramSize>MAX_PREP_NVRAM)
  59. {
  60. /*
  61. * NvRAM is too large
  62. */
  63. nvram->Header.GELength=0;
  64. return;
  65. }
  66. /*
  67. * Read the remainder of the PReP NvRAM
  68. */
  69. nvp = (char *) &nvram->GEArea[0];
  70. for (i=sizeof(HEADER); i<nvramSize; i++)
  71. {
  72. *nvp = ppc_md.nvram_read_val(i);
  73. nvp++;
  74. }
  75. }
  76. __prep
  77. char __prep *prep_nvram_get_var(const char *name)
  78. {
  79. char *cp;
  80. int namelen;
  81. namelen = strlen(name);
  82. cp = prep_nvram_first_var();
  83. while (cp != NULL) {
  84. if ((strncmp(name, cp, namelen) == 0)
  85. && (cp[namelen] == '='))
  86. {
  87. return cp+namelen+1;
  88. }
  89. cp = prep_nvram_next_var(cp);
  90. }
  91. return NULL;
  92. }
  93. __prep
  94. char __prep *prep_nvram_first_var(void)
  95. {
  96. if (nvram->Header.GELength == 0) {
  97. return NULL;
  98. } else {
  99. return (((char *)nvram)
  100. + ((unsigned int) nvram->Header.GEAddress));
  101. }
  102. }
  103. __prep
  104. char __prep *prep_nvram_next_var(char *name)
  105. {
  106. char *cp;
  107. cp = name;
  108. while (((cp - ((char *) nvram->GEArea)) < nvram->Header.GELength)
  109. && (*cp != '\0'))
  110. {
  111. cp++;
  112. }
  113. /* Skip over any null characters. */
  114. while (((cp - ((char *) nvram->GEArea)) < nvram->Header.GELength)
  115. && (*cp == '\0'))
  116. {
  117. cp++;
  118. }
  119. if ((cp - ((char *) nvram->GEArea)) < nvram->Header.GELength) {
  120. return cp;
  121. } else {
  122. return NULL;
  123. }
  124. }