Benjamin Herrenschmidt | 628daa8 | 2011-09-19 17:45:01 +0000 | [diff] [blame] | 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 | |
Nicholas Piggin | 3b80703 | 2018-04-10 21:49:33 +1000 | [diff] [blame] | 14 | #include <linux/delay.h> |
Benjamin Herrenschmidt | 628daa8 | 2011-09-19 17:45:01 +0000 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/of.h> |
| 18 | |
| 19 | #include <asm/opal.h> |
Hari Bathini | f761829 | 2015-02-06 01:06:52 +0530 | [diff] [blame] | 20 | #include <asm/nvram.h> |
Benjamin Herrenschmidt | 628daa8 | 2011-09-19 17:45:01 +0000 | [diff] [blame] | 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 | |
Nicholas Piggin | c1d2a31 | 2018-05-15 01:59:47 +1000 | [diff] [blame] | 47 | /* |
| 48 | * This can be called in the panic path with interrupts off, so use |
| 49 | * mdelay in that case. |
| 50 | */ |
Benjamin Herrenschmidt | 628daa8 | 2011-09-19 17:45:01 +0000 | [diff] [blame] | 51 | static ssize_t opal_nvram_write(char *buf, size_t count, loff_t *index) |
| 52 | { |
| 53 | s64 rc = OPAL_BUSY; |
| 54 | int off; |
| 55 | |
| 56 | if (*index >= nvram_size) |
| 57 | return 0; |
| 58 | off = *index; |
| 59 | if ((off + count) > nvram_size) |
| 60 | count = nvram_size - off; |
| 61 | |
| 62 | while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) { |
| 63 | rc = opal_write_nvram(__pa(buf), count, off); |
Nicholas Piggin | 3b80703 | 2018-04-10 21:49:33 +1000 | [diff] [blame] | 64 | if (rc == OPAL_BUSY_EVENT) { |
Nicholas Piggin | c1d2a31 | 2018-05-15 01:59:47 +1000 | [diff] [blame] | 65 | if (in_interrupt() || irqs_disabled()) |
| 66 | mdelay(OPAL_BUSY_DELAY_MS); |
| 67 | else |
| 68 | msleep(OPAL_BUSY_DELAY_MS); |
Benjamin Herrenschmidt | 628daa8 | 2011-09-19 17:45:01 +0000 | [diff] [blame] | 69 | opal_poll_events(NULL); |
Nicholas Piggin | 3b80703 | 2018-04-10 21:49:33 +1000 | [diff] [blame] | 70 | } else if (rc == OPAL_BUSY) { |
Nicholas Piggin | c1d2a31 | 2018-05-15 01:59:47 +1000 | [diff] [blame] | 71 | if (in_interrupt() || irqs_disabled()) |
| 72 | mdelay(OPAL_BUSY_DELAY_MS); |
| 73 | else |
| 74 | msleep(OPAL_BUSY_DELAY_MS); |
Nicholas Piggin | 3b80703 | 2018-04-10 21:49:33 +1000 | [diff] [blame] | 75 | } |
Benjamin Herrenschmidt | 628daa8 | 2011-09-19 17:45:01 +0000 | [diff] [blame] | 76 | } |
Nicholas Piggin | 741de61 | 2018-03-27 01:02:33 +1000 | [diff] [blame] | 77 | |
| 78 | if (rc) |
| 79 | return -EIO; |
| 80 | |
Benjamin Herrenschmidt | 628daa8 | 2011-09-19 17:45:01 +0000 | [diff] [blame] | 81 | *index += count; |
| 82 | return count; |
| 83 | } |
| 84 | |
Hari Bathini | f761829 | 2015-02-06 01:06:52 +0530 | [diff] [blame] | 85 | static int __init opal_nvram_init_log_partitions(void) |
| 86 | { |
| 87 | /* Scan nvram for partitions */ |
| 88 | nvram_scan_partitions(); |
| 89 | nvram_init_oops_partition(0); |
| 90 | return 0; |
| 91 | } |
| 92 | machine_arch_initcall(powernv, opal_nvram_init_log_partitions); |
| 93 | |
Benjamin Herrenschmidt | 628daa8 | 2011-09-19 17:45:01 +0000 | [diff] [blame] | 94 | void __init opal_nvram_init(void) |
| 95 | { |
| 96 | struct device_node *np; |
Benjamin Herrenschmidt | 81063cd | 2013-09-23 12:05:00 +1000 | [diff] [blame] | 97 | const __be32 *nbytes_p; |
Benjamin Herrenschmidt | 628daa8 | 2011-09-19 17:45:01 +0000 | [diff] [blame] | 98 | |
| 99 | np = of_find_compatible_node(NULL, NULL, "ibm,opal-nvram"); |
| 100 | if (np == NULL) |
| 101 | return; |
| 102 | |
| 103 | nbytes_p = of_get_property(np, "#bytes", NULL); |
| 104 | if (!nbytes_p) { |
| 105 | of_node_put(np); |
| 106 | return; |
| 107 | } |
Benjamin Herrenschmidt | 81063cd | 2013-09-23 12:05:00 +1000 | [diff] [blame] | 108 | nvram_size = be32_to_cpup(nbytes_p); |
Benjamin Herrenschmidt | 628daa8 | 2011-09-19 17:45:01 +0000 | [diff] [blame] | 109 | |
Anton Blanchard | 9a4f5cd | 2014-09-17 14:39:38 +1000 | [diff] [blame] | 110 | pr_info("OPAL nvram setup, %u bytes\n", nvram_size); |
Benjamin Herrenschmidt | 628daa8 | 2011-09-19 17:45:01 +0000 | [diff] [blame] | 111 | of_node_put(np); |
| 112 | |
| 113 | ppc_md.nvram_read = opal_nvram_read; |
| 114 | ppc_md.nvram_write = opal_nvram_write; |
| 115 | ppc_md.nvram_size = opal_nvram_size; |
| 116 | } |
| 117 | |