opal-nvram.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/nvram.h>
  17. #include <asm/machdep.h>
  18. static unsigned int nvram_size;
  19. static ssize_t opal_nvram_size(void)
  20. {
  21. return nvram_size;
  22. }
  23. static ssize_t opal_nvram_read(char *buf, size_t count, loff_t *index)
  24. {
  25. s64 rc;
  26. int off;
  27. if (*index >= nvram_size)
  28. return 0;
  29. off = *index;
  30. if ((off + count) > nvram_size)
  31. count = nvram_size - off;
  32. rc = opal_read_nvram(__pa(buf), count, off);
  33. if (rc != OPAL_SUCCESS)
  34. return -EIO;
  35. *index += count;
  36. return count;
  37. }
  38. static ssize_t opal_nvram_write(char *buf, size_t count, loff_t *index)
  39. {
  40. s64 rc = OPAL_BUSY;
  41. int off;
  42. if (*index >= nvram_size)
  43. return 0;
  44. off = *index;
  45. if ((off + count) > nvram_size)
  46. count = nvram_size - off;
  47. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  48. rc = opal_write_nvram(__pa(buf), count, off);
  49. if (rc == OPAL_BUSY_EVENT)
  50. opal_poll_events(NULL);
  51. }
  52. *index += count;
  53. return count;
  54. }
  55. static int __init opal_nvram_init_log_partitions(void)
  56. {
  57. /* Scan nvram for partitions */
  58. nvram_scan_partitions();
  59. nvram_init_oops_partition(0);
  60. return 0;
  61. }
  62. machine_arch_initcall(powernv, opal_nvram_init_log_partitions);
  63. void __init opal_nvram_init(void)
  64. {
  65. struct device_node *np;
  66. const __be32 *nbytes_p;
  67. np = of_find_compatible_node(NULL, NULL, "ibm,opal-nvram");
  68. if (np == NULL)
  69. return;
  70. nbytes_p = of_get_property(np, "#bytes", NULL);
  71. if (!nbytes_p) {
  72. of_node_put(np);
  73. return;
  74. }
  75. nvram_size = be32_to_cpup(nbytes_p);
  76. pr_info("OPAL nvram setup, %u bytes\n", nvram_size);
  77. of_node_put(np);
  78. ppc_md.nvram_read = opal_nvram_read;
  79. ppc_md.nvram_write = opal_nvram_write;
  80. ppc_md.nvram_size = opal_nvram_size;
  81. }