nvram.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * c 2001 PPC 64 Team, IBM Corp
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * /dev/nvram driver for PPC64
  10. *
  11. * This perhaps should live in drivers/char
  12. */
  13. #include <linux/types.h>
  14. #include <linux/errno.h>
  15. #include <linux/init.h>
  16. #include <linux/spinlock.h>
  17. #include <asm/uaccess.h>
  18. #include <asm/nvram.h>
  19. #include <asm/rtas.h>
  20. #include <asm/prom.h>
  21. #include <asm/machdep.h>
  22. static unsigned int nvram_size;
  23. static int nvram_fetch, nvram_store;
  24. static char nvram_buf[NVRW_CNT]; /* assume this is in the first 4GB */
  25. static DEFINE_SPINLOCK(nvram_lock);
  26. /* Max bytes to read/write in one go */
  27. #define NVRW_CNT 0x20
  28. static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index)
  29. {
  30. unsigned int i;
  31. unsigned long len;
  32. int done;
  33. unsigned long flags;
  34. char *p = buf;
  35. if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE)
  36. return -ENODEV;
  37. if (*index >= nvram_size)
  38. return 0;
  39. i = *index;
  40. if (i + count > nvram_size)
  41. count = nvram_size - i;
  42. spin_lock_irqsave(&nvram_lock, flags);
  43. for (; count != 0; count -= len) {
  44. len = count;
  45. if (len > NVRW_CNT)
  46. len = NVRW_CNT;
  47. if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf),
  48. len) != 0) || len != done) {
  49. spin_unlock_irqrestore(&nvram_lock, flags);
  50. return -EIO;
  51. }
  52. memcpy(p, nvram_buf, len);
  53. p += len;
  54. i += len;
  55. }
  56. spin_unlock_irqrestore(&nvram_lock, flags);
  57. *index = i;
  58. return p - buf;
  59. }
  60. static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index)
  61. {
  62. unsigned int i;
  63. unsigned long len;
  64. int done;
  65. unsigned long flags;
  66. const char *p = buf;
  67. if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE)
  68. return -ENODEV;
  69. if (*index >= nvram_size)
  70. return 0;
  71. i = *index;
  72. if (i + count > nvram_size)
  73. count = nvram_size - i;
  74. spin_lock_irqsave(&nvram_lock, flags);
  75. for (; count != 0; count -= len) {
  76. len = count;
  77. if (len > NVRW_CNT)
  78. len = NVRW_CNT;
  79. memcpy(nvram_buf, p, len);
  80. if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf),
  81. len) != 0) || len != done) {
  82. spin_unlock_irqrestore(&nvram_lock, flags);
  83. return -EIO;
  84. }
  85. p += len;
  86. i += len;
  87. }
  88. spin_unlock_irqrestore(&nvram_lock, flags);
  89. *index = i;
  90. return p - buf;
  91. }
  92. static ssize_t pSeries_nvram_get_size(void)
  93. {
  94. return nvram_size ? nvram_size : -ENODEV;
  95. }
  96. int __init pSeries_nvram_init(void)
  97. {
  98. struct device_node *nvram;
  99. const unsigned int *nbytes_p;
  100. unsigned int proplen;
  101. nvram = of_find_node_by_type(NULL, "nvram");
  102. if (nvram == NULL)
  103. return -ENODEV;
  104. nbytes_p = of_get_property(nvram, "#bytes", &proplen);
  105. if (nbytes_p == NULL || proplen != sizeof(unsigned int)) {
  106. of_node_put(nvram);
  107. return -EIO;
  108. }
  109. nvram_size = *nbytes_p;
  110. nvram_fetch = rtas_token("nvram-fetch");
  111. nvram_store = rtas_token("nvram-store");
  112. printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size);
  113. of_node_put(nvram);
  114. ppc_md.nvram_read = pSeries_nvram_read;
  115. ppc_md.nvram_write = pSeries_nvram_write;
  116. ppc_md.nvram_size = pSeries_nvram_get_size;
  117. return 0;
  118. }