1*2874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2628daa8dSBenjamin Herrenschmidt /*
3628daa8dSBenjamin Herrenschmidt * PowerNV nvram code.
4628daa8dSBenjamin Herrenschmidt *
5628daa8dSBenjamin Herrenschmidt * Copyright 2011 IBM Corp.
6628daa8dSBenjamin Herrenschmidt */
7628daa8dSBenjamin Herrenschmidt
8628daa8dSBenjamin Herrenschmidt #define DEBUG
9628daa8dSBenjamin Herrenschmidt
103b807033SNicholas Piggin #include <linux/delay.h>
11628daa8dSBenjamin Herrenschmidt #include <linux/kernel.h>
12628daa8dSBenjamin Herrenschmidt #include <linux/init.h>
13628daa8dSBenjamin Herrenschmidt #include <linux/of.h>
14628daa8dSBenjamin Herrenschmidt
15628daa8dSBenjamin Herrenschmidt #include <asm/opal.h>
16f7618299SHari Bathini #include <asm/nvram.h>
17628daa8dSBenjamin Herrenschmidt #include <asm/machdep.h>
18628daa8dSBenjamin Herrenschmidt
19628daa8dSBenjamin Herrenschmidt static unsigned int nvram_size;
20628daa8dSBenjamin Herrenschmidt
opal_nvram_size(void)21628daa8dSBenjamin Herrenschmidt static ssize_t opal_nvram_size(void)
22628daa8dSBenjamin Herrenschmidt {
23628daa8dSBenjamin Herrenschmidt return nvram_size;
24628daa8dSBenjamin Herrenschmidt }
25628daa8dSBenjamin Herrenschmidt
opal_nvram_read(char * buf,size_t count,loff_t * index)26628daa8dSBenjamin Herrenschmidt static ssize_t opal_nvram_read(char *buf, size_t count, loff_t *index)
27628daa8dSBenjamin Herrenschmidt {
28628daa8dSBenjamin Herrenschmidt s64 rc;
29628daa8dSBenjamin Herrenschmidt int off;
30628daa8dSBenjamin Herrenschmidt
31628daa8dSBenjamin Herrenschmidt if (*index >= nvram_size)
32628daa8dSBenjamin Herrenschmidt return 0;
33628daa8dSBenjamin Herrenschmidt off = *index;
34628daa8dSBenjamin Herrenschmidt if ((off + count) > nvram_size)
35628daa8dSBenjamin Herrenschmidt count = nvram_size - off;
36628daa8dSBenjamin Herrenschmidt rc = opal_read_nvram(__pa(buf), count, off);
37628daa8dSBenjamin Herrenschmidt if (rc != OPAL_SUCCESS)
38628daa8dSBenjamin Herrenschmidt return -EIO;
39628daa8dSBenjamin Herrenschmidt *index += count;
40628daa8dSBenjamin Herrenschmidt return count;
41628daa8dSBenjamin Herrenschmidt }
42628daa8dSBenjamin Herrenschmidt
43c1d2a313SNicholas Piggin /*
44c1d2a313SNicholas Piggin * This can be called in the panic path with interrupts off, so use
45c1d2a313SNicholas Piggin * mdelay in that case.
46c1d2a313SNicholas Piggin */
opal_nvram_write(char * buf,size_t count,loff_t * index)47628daa8dSBenjamin Herrenschmidt static ssize_t opal_nvram_write(char *buf, size_t count, loff_t *index)
48628daa8dSBenjamin Herrenschmidt {
49628daa8dSBenjamin Herrenschmidt s64 rc = OPAL_BUSY;
50628daa8dSBenjamin Herrenschmidt int off;
51628daa8dSBenjamin Herrenschmidt
52628daa8dSBenjamin Herrenschmidt if (*index >= nvram_size)
53628daa8dSBenjamin Herrenschmidt return 0;
54628daa8dSBenjamin Herrenschmidt off = *index;
55628daa8dSBenjamin Herrenschmidt if ((off + count) > nvram_size)
56628daa8dSBenjamin Herrenschmidt count = nvram_size - off;
57628daa8dSBenjamin Herrenschmidt
58628daa8dSBenjamin Herrenschmidt while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
59628daa8dSBenjamin Herrenschmidt rc = opal_write_nvram(__pa(buf), count, off);
603b807033SNicholas Piggin if (rc == OPAL_BUSY_EVENT) {
61c1d2a313SNicholas Piggin if (in_interrupt() || irqs_disabled())
62c1d2a313SNicholas Piggin mdelay(OPAL_BUSY_DELAY_MS);
63c1d2a313SNicholas Piggin else
643b807033SNicholas Piggin msleep(OPAL_BUSY_DELAY_MS);
65628daa8dSBenjamin Herrenschmidt opal_poll_events(NULL);
663b807033SNicholas Piggin } else if (rc == OPAL_BUSY) {
67c1d2a313SNicholas Piggin if (in_interrupt() || irqs_disabled())
68c1d2a313SNicholas Piggin mdelay(OPAL_BUSY_DELAY_MS);
69c1d2a313SNicholas Piggin else
703b807033SNicholas Piggin msleep(OPAL_BUSY_DELAY_MS);
713b807033SNicholas Piggin }
72628daa8dSBenjamin Herrenschmidt }
73741de617SNicholas Piggin
74741de617SNicholas Piggin if (rc)
75741de617SNicholas Piggin return -EIO;
76741de617SNicholas Piggin
77628daa8dSBenjamin Herrenschmidt *index += count;
78628daa8dSBenjamin Herrenschmidt return count;
79628daa8dSBenjamin Herrenschmidt }
80628daa8dSBenjamin Herrenschmidt
opal_nvram_init_log_partitions(void)81f7618299SHari Bathini static int __init opal_nvram_init_log_partitions(void)
82f7618299SHari Bathini {
83f7618299SHari Bathini /* Scan nvram for partitions */
84f7618299SHari Bathini nvram_scan_partitions();
85f7618299SHari Bathini nvram_init_oops_partition(0);
86f7618299SHari Bathini return 0;
87f7618299SHari Bathini }
88f7618299SHari Bathini machine_arch_initcall(powernv, opal_nvram_init_log_partitions);
89f7618299SHari Bathini
opal_nvram_init(void)90628daa8dSBenjamin Herrenschmidt void __init opal_nvram_init(void)
91628daa8dSBenjamin Herrenschmidt {
92628daa8dSBenjamin Herrenschmidt struct device_node *np;
9381063cddSBenjamin Herrenschmidt const __be32 *nbytes_p;
94628daa8dSBenjamin Herrenschmidt
95628daa8dSBenjamin Herrenschmidt np = of_find_compatible_node(NULL, NULL, "ibm,opal-nvram");
96628daa8dSBenjamin Herrenschmidt if (np == NULL)
97628daa8dSBenjamin Herrenschmidt return;
98628daa8dSBenjamin Herrenschmidt
99628daa8dSBenjamin Herrenschmidt nbytes_p = of_get_property(np, "#bytes", NULL);
100628daa8dSBenjamin Herrenschmidt if (!nbytes_p) {
101628daa8dSBenjamin Herrenschmidt of_node_put(np);
102628daa8dSBenjamin Herrenschmidt return;
103628daa8dSBenjamin Herrenschmidt }
10481063cddSBenjamin Herrenschmidt nvram_size = be32_to_cpup(nbytes_p);
105628daa8dSBenjamin Herrenschmidt
1069a4f5cd0SAnton Blanchard pr_info("OPAL nvram setup, %u bytes\n", nvram_size);
107628daa8dSBenjamin Herrenschmidt of_node_put(np);
108628daa8dSBenjamin Herrenschmidt
109628daa8dSBenjamin Herrenschmidt ppc_md.nvram_read = opal_nvram_read;
110628daa8dSBenjamin Herrenschmidt ppc_md.nvram_write = opal_nvram_write;
111628daa8dSBenjamin Herrenschmidt ppc_md.nvram_size = opal_nvram_size;
112628daa8dSBenjamin Herrenschmidt }
113628daa8dSBenjamin Herrenschmidt
114