Olaf Hering | 35e95e6 | 2005-10-28 17:46:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * c 2001 PPC 64 Team, IBM Corp |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * /dev/nvram driver for PPC |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/init.h> |
Olaf Hering | 35e95e6 | 2005-10-28 17:46:19 -0700 | [diff] [blame] | 15 | #include <linux/spinlock.h> |
| 16 | #include <asm/uaccess.h> |
| 17 | #include <asm/prom.h> |
| 18 | #include <asm/machdep.h> |
Paul Mackerras | 3983829 | 2005-11-02 19:58:12 +1100 | [diff] [blame] | 19 | #include <asm/rtas.h> |
Olaf Hering | 35e95e6 | 2005-10-28 17:46:19 -0700 | [diff] [blame] | 20 | #include "chrp.h" |
| 21 | |
| 22 | static unsigned int nvram_size; |
| 23 | static unsigned char nvram_buf[4]; |
| 24 | static DEFINE_SPINLOCK(nvram_lock); |
| 25 | |
| 26 | static unsigned char chrp_nvram_read(int addr) |
| 27 | { |
Paul Mackerras | 3983829 | 2005-11-02 19:58:12 +1100 | [diff] [blame] | 28 | unsigned int done; |
| 29 | unsigned long flags; |
Olaf Hering | 35e95e6 | 2005-10-28 17:46:19 -0700 | [diff] [blame] | 30 | unsigned char ret; |
| 31 | |
| 32 | if (addr >= nvram_size) { |
| 33 | printk(KERN_DEBUG "%s: read addr %d > nvram_size %u\n", |
| 34 | current->comm, addr, nvram_size); |
| 35 | return 0xff; |
| 36 | } |
| 37 | spin_lock_irqsave(&nvram_lock, flags); |
Paul Mackerras | 19fa17e | 2005-11-02 15:04:26 +1100 | [diff] [blame] | 38 | if ((rtas_call(rtas_token("nvram-fetch"), 3, 2, &done, addr, |
| 39 | __pa(nvram_buf), 1) != 0) || 1 != done) |
Olaf Hering | 35e95e6 | 2005-10-28 17:46:19 -0700 | [diff] [blame] | 40 | ret = 0xff; |
| 41 | else |
| 42 | ret = nvram_buf[0]; |
| 43 | spin_unlock_irqrestore(&nvram_lock, flags); |
| 44 | |
| 45 | return ret; |
| 46 | } |
| 47 | |
| 48 | static void chrp_nvram_write(int addr, unsigned char val) |
| 49 | { |
Paul Mackerras | 3983829 | 2005-11-02 19:58:12 +1100 | [diff] [blame] | 50 | unsigned int done; |
| 51 | unsigned long flags; |
Olaf Hering | 35e95e6 | 2005-10-28 17:46:19 -0700 | [diff] [blame] | 52 | |
| 53 | if (addr >= nvram_size) { |
| 54 | printk(KERN_DEBUG "%s: write addr %d > nvram_size %u\n", |
| 55 | current->comm, addr, nvram_size); |
| 56 | return; |
| 57 | } |
| 58 | spin_lock_irqsave(&nvram_lock, flags); |
| 59 | nvram_buf[0] = val; |
Paul Mackerras | 19fa17e | 2005-11-02 15:04:26 +1100 | [diff] [blame] | 60 | if ((rtas_call(rtas_token("nvram-store"), 3, 2, &done, addr, |
| 61 | __pa(nvram_buf), 1) != 0) || 1 != done) |
Olaf Hering | 35e95e6 | 2005-10-28 17:46:19 -0700 | [diff] [blame] | 62 | printk(KERN_DEBUG "rtas IO error storing 0x%02x at %d", val, addr); |
| 63 | spin_unlock_irqrestore(&nvram_lock, flags); |
| 64 | } |
| 65 | |
| 66 | void __init chrp_nvram_init(void) |
| 67 | { |
| 68 | struct device_node *nvram; |
Cedric Le Goater | 563c5d8 | 2013-10-30 14:47:07 +0100 | [diff] [blame] | 69 | const __be32 *nbytes_p; |
Jeremy Kerr | ae6b410 | 2006-07-12 15:40:05 +1000 | [diff] [blame] | 70 | unsigned int proplen; |
Olaf Hering | 35e95e6 | 2005-10-28 17:46:19 -0700 | [diff] [blame] | 71 | |
| 72 | nvram = of_find_node_by_type(NULL, "nvram"); |
| 73 | if (nvram == NULL) |
| 74 | return; |
| 75 | |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 76 | nbytes_p = of_get_property(nvram, "#bytes", &proplen); |
Julia Lawall | 7cf9bac | 2010-08-31 05:48:58 +0000 | [diff] [blame] | 77 | if (nbytes_p == NULL || proplen != sizeof(unsigned int)) { |
| 78 | of_node_put(nvram); |
Olaf Hering | 35e95e6 | 2005-10-28 17:46:19 -0700 | [diff] [blame] | 79 | return; |
Julia Lawall | 7cf9bac | 2010-08-31 05:48:58 +0000 | [diff] [blame] | 80 | } |
Olaf Hering | 35e95e6 | 2005-10-28 17:46:19 -0700 | [diff] [blame] | 81 | |
Cedric Le Goater | 563c5d8 | 2013-10-30 14:47:07 +0100 | [diff] [blame] | 82 | nvram_size = be32_to_cpup(nbytes_p); |
Olaf Hering | 35e95e6 | 2005-10-28 17:46:19 -0700 | [diff] [blame] | 83 | |
| 84 | printk(KERN_INFO "CHRP nvram contains %u bytes\n", nvram_size); |
| 85 | of_node_put(nvram); |
| 86 | |
| 87 | ppc_md.nvram_read_val = chrp_nvram_read; |
| 88 | ppc_md.nvram_write_val = chrp_nvram_write; |
| 89 | |
| 90 | return; |
| 91 | } |