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