opal-nvram.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * PowerNV nvram code.
  3. *
  4. * Copyright 2011 IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #define DEBUG
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/of.h>
  15. #include <asm/opal.h>
  16. #include <asm/machdep.h>
  17. static unsigned int nvram_size;
  18. static ssize_t opal_nvram_size(void)
  19. {
  20. return nvram_size;
  21. }
  22. static ssize_t opal_nvram_read(char *buf, size_t count, loff_t *index)
  23. {
  24. s64 rc;
  25. int off;
  26. if (*index >= nvram_size)
  27. return 0;
  28. off = *index;
  29. if ((off + count) > nvram_size)
  30. count = nvram_size - off;
  31. rc = opal_read_nvram(__pa(buf), count, off);
  32. if (rc != OPAL_SUCCESS)
  33. return -EIO;
  34. *index += count;
  35. return count;
  36. }
  37. static ssize_t opal_nvram_write(char *buf, size_t count, loff_t *index)
  38. {
  39. s64 rc = OPAL_BUSY;
  40. int off;
  41. if (*index >= nvram_size)
  42. return 0;
  43. off = *index;
  44. if ((off + count) > nvram_size)
  45. count = nvram_size - off;
  46. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  47. rc = opal_write_nvram(__pa(buf), count, off);
  48. if (rc == OPAL_BUSY_EVENT)
  49. opal_poll_events(NULL);
  50. }
  51. *index += count;
  52. return count;
  53. }
  54. void __init opal_nvram_init(void)
  55. {
  56. struct device_node *np;
  57. const u32 *nbytes_p;
  58. np = of_find_compatible_node(NULL, NULL, "ibm,opal-nvram");
  59. if (np == NULL)
  60. return;
  61. nbytes_p = of_get_property(np, "#bytes", NULL);
  62. if (!nbytes_p) {
  63. of_node_put(np);
  64. return;
  65. }
  66. nvram_size = *nbytes_p;
  67. printk(KERN_INFO "OPAL nvram setup, %u bytes\n", nvram_size);
  68. of_node_put(np);
  69. ppc_md.nvram_read = opal_nvram_read;
  70. ppc_md.nvram_write = opal_nvram_write;
  71. ppc_md.nvram_size = opal_nvram_size;
  72. }