nvram.c 3.1 KB

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