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 
12 #define DEBUG
13 
14 #include <linux/delay.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/of.h>
18 
19 #include <asm/opal.h>
20 #include <asm/nvram.h>
21 #include <asm/machdep.h>
22 
23 static unsigned int nvram_size;
24 
25 static ssize_t opal_nvram_size(void)
26 {
27 	return nvram_size;
28 }
29 
30 static ssize_t opal_nvram_read(char *buf, size_t count, loff_t *index)
31 {
32 	s64 rc;
33 	int off;
34 
35 	if (*index >= nvram_size)
36 		return 0;
37 	off = *index;
38 	if ((off + count) > nvram_size)
39 		count = nvram_size - off;
40 	rc = opal_read_nvram(__pa(buf), count, off);
41 	if (rc != OPAL_SUCCESS)
42 		return -EIO;
43 	*index += count;
44 	return count;
45 }
46 
47 static ssize_t opal_nvram_write(char *buf, size_t count, loff_t *index)
48 {
49 	s64 rc = OPAL_BUSY;
50 	int off;
51 
52 	if (*index >= nvram_size)
53 		return 0;
54 	off = *index;
55 	if ((off + count) > nvram_size)
56 		count = nvram_size - off;
57 
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 			msleep(OPAL_BUSY_DELAY_MS);
62 			opal_poll_events(NULL);
63 		} else if (rc == OPAL_BUSY) {
64 			msleep(OPAL_BUSY_DELAY_MS);
65 		}
66 	}
67 
68 	if (rc)
69 		return -EIO;
70 
71 	*index += count;
72 	return count;
73 }
74 
75 static int __init opal_nvram_init_log_partitions(void)
76 {
77 	/* Scan nvram for partitions */
78 	nvram_scan_partitions();
79 	nvram_init_oops_partition(0);
80 	return 0;
81 }
82 machine_arch_initcall(powernv, opal_nvram_init_log_partitions);
83 
84 void __init opal_nvram_init(void)
85 {
86 	struct device_node *np;
87 	const __be32 *nbytes_p;
88 
89 	np = of_find_compatible_node(NULL, NULL, "ibm,opal-nvram");
90 	if (np == NULL)
91 		return;
92 
93 	nbytes_p = of_get_property(np, "#bytes", NULL);
94 	if (!nbytes_p) {
95 		of_node_put(np);
96 		return;
97 	}
98 	nvram_size = be32_to_cpup(nbytes_p);
99 
100 	pr_info("OPAL nvram setup, %u bytes\n", nvram_size);
101 	of_node_put(np);
102 
103 	ppc_md.nvram_read = opal_nvram_read;
104 	ppc_md.nvram_write = opal_nvram_write;
105 	ppc_md.nvram_size = opal_nvram_size;
106 }
107 
108