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