nvram.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/slab.h>
  17. #include <linux/spinlock.h>
  18. #include <asm/uaccess.h>
  19. #include <asm/nvram.h>
  20. #include <asm/rtas.h>
  21. #include <asm/prom.h>
  22. #include <asm/machdep.h>
  23. static unsigned int nvram_size;
  24. static int nvram_fetch, nvram_store;
  25. static char nvram_buf[NVRW_CNT]; /* assume this is in the first 4GB */
  26. static DEFINE_SPINLOCK(nvram_lock);
  27. static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index)
  28. {
  29. unsigned int i;
  30. unsigned long len;
  31. int done;
  32. unsigned long flags;
  33. char *p = buf;
  34. if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE)
  35. return -ENODEV;
  36. if (*index >= nvram_size)
  37. return 0;
  38. i = *index;
  39. if (i + count > nvram_size)
  40. count = nvram_size - i;
  41. spin_lock_irqsave(&nvram_lock, flags);
  42. for (; count != 0; count -= len) {
  43. len = count;
  44. if (len > NVRW_CNT)
  45. len = NVRW_CNT;
  46. if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf),
  47. len) != 0) || len != done) {
  48. spin_unlock_irqrestore(&nvram_lock, flags);
  49. return -EIO;
  50. }
  51. memcpy(p, nvram_buf, len);
  52. p += len;
  53. i += len;
  54. }
  55. spin_unlock_irqrestore(&nvram_lock, flags);
  56. *index = i;
  57. return p - buf;
  58. }
  59. static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index)
  60. {
  61. unsigned int i;
  62. unsigned long len;
  63. int done;
  64. unsigned long flags;
  65. const char *p = buf;
  66. if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE)
  67. return -ENODEV;
  68. if (*index >= nvram_size)
  69. return 0;
  70. i = *index;
  71. if (i + count > nvram_size)
  72. count = nvram_size - i;
  73. spin_lock_irqsave(&nvram_lock, flags);
  74. for (; count != 0; count -= len) {
  75. len = count;
  76. if (len > NVRW_CNT)
  77. len = NVRW_CNT;
  78. memcpy(nvram_buf, p, len);
  79. if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf),
  80. len) != 0) || len != done) {
  81. spin_unlock_irqrestore(&nvram_lock, flags);
  82. return -EIO;
  83. }
  84. p += len;
  85. i += len;
  86. }
  87. spin_unlock_irqrestore(&nvram_lock, flags);
  88. *index = i;
  89. return p - buf;
  90. }
  91. static ssize_t pSeries_nvram_get_size(void)
  92. {
  93. return nvram_size ? nvram_size : -ENODEV;
  94. }
  95. int __init pSeries_nvram_init(void)
  96. {
  97. struct device_node *nvram;
  98. const unsigned int *nbytes_p;
  99. unsigned int proplen;
  100. nvram = of_find_node_by_type(NULL, "nvram");
  101. if (nvram == NULL)
  102. return -ENODEV;
  103. nbytes_p = of_get_property(nvram, "#bytes", &proplen);
  104. if (nbytes_p == NULL || proplen != sizeof(unsigned int)) {
  105. of_node_put(nvram);
  106. return -EIO;
  107. }
  108. nvram_size = *nbytes_p;
  109. nvram_fetch = rtas_token("nvram-fetch");
  110. nvram_store = rtas_token("nvram-store");
  111. printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size);
  112. of_node_put(nvram);
  113. ppc_md.nvram_read = pSeries_nvram_read;
  114. ppc_md.nvram_write = pSeries_nvram_write;
  115. ppc_md.nvram_size = pSeries_nvram_get_size;
  116. return 0;
  117. }