1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright 2011 IBM Corp.
10 #include <linux/delay.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
16 #include <asm/nvram.h>
17 #include <asm/machdep.h>
19 static unsigned int nvram_size;
21 static ssize_t opal_nvram_size(void)
26 static ssize_t opal_nvram_read(char *buf, size_t count, loff_t *index)
31 if (*index >= nvram_size)
34 if ((off + count) > nvram_size)
35 count = nvram_size - off;
36 rc = opal_read_nvram(__pa(buf), count, off);
37 if (rc != OPAL_SUCCESS)
44 * This can be called in the panic path with interrupts off, so use
45 * mdelay in that case.
47 static ssize_t opal_nvram_write(char *buf, size_t count, loff_t *index)
52 if (*index >= nvram_size)
55 if ((off + count) > nvram_size)
56 count = nvram_size - off;
58 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
59 rc = opal_write_nvram(__pa(buf), count, off);
60 if (rc == OPAL_BUSY_EVENT) {
61 if (in_interrupt() || irqs_disabled())
62 mdelay(OPAL_BUSY_DELAY_MS);
64 msleep(OPAL_BUSY_DELAY_MS);
65 opal_poll_events(NULL);
66 } else if (rc == OPAL_BUSY) {
67 if (in_interrupt() || irqs_disabled())
68 mdelay(OPAL_BUSY_DELAY_MS);
70 msleep(OPAL_BUSY_DELAY_MS);
81 static int __init opal_nvram_init_log_partitions(void)
83 /* Scan nvram for partitions */
84 nvram_scan_partitions();
85 nvram_init_oops_partition(0);
88 machine_arch_initcall(powernv, opal_nvram_init_log_partitions);
90 void __init opal_nvram_init(void)
92 struct device_node *np;
93 const __be32 *nbytes_p;
95 np = of_find_compatible_node(NULL, NULL, "ibm,opal-nvram");
99 nbytes_p = of_get_property(np, "#bytes", NULL);
104 nvram_size = be32_to_cpup(nbytes_p);
106 pr_info("OPAL nvram setup, %u bytes\n", nvram_size);
109 ppc_md.nvram_read = opal_nvram_read;
110 ppc_md.nvram_write = opal_nvram_write;
111 ppc_md.nvram_size = opal_nvram_size;