Blame view

arch/powerpc/sysdev/mmio_nvram.c 3.71 KB
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
1
  /*
f3f66f599   Arnd Bergmann   [PATCH] powerpc: ...
2
   * memory mapped NVRAM
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
   *
   * (C) Copyright IBM Corp. 2005
   *
   * Authors : Utz Bacher <utz.bacher@de.ibm.com>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2, or (at your option)
   * any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   */
  
  #include <linux/fs.h>
  #include <linux/init.h>
  #include <linux/kernel.h>
  #include <linux/spinlock.h>
  #include <linux/types.h>
  
  #include <asm/machdep.h>
  #include <asm/nvram.h>
  #include <asm/prom.h>
f3f66f599   Arnd Bergmann   [PATCH] powerpc: ...
32
33
  static void __iomem *mmio_nvram_start;
  static long mmio_nvram_len;
34af946a2   Ingo Molnar   [PATCH] spin/rwlo...
34
  static DEFINE_SPINLOCK(mmio_nvram_lock);
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
35

f3f66f599   Arnd Bergmann   [PATCH] powerpc: ...
36
  static ssize_t mmio_nvram_read(char *buf, size_t count, loff_t *index)
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
37
38
  {
  	unsigned long flags;
f3f66f599   Arnd Bergmann   [PATCH] powerpc: ...
39
  	if (*index >= mmio_nvram_len)
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
40
  		return 0;
f3f66f599   Arnd Bergmann   [PATCH] powerpc: ...
41
42
  	if (*index + count > mmio_nvram_len)
  		count = mmio_nvram_len - *index;
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
43

f3f66f599   Arnd Bergmann   [PATCH] powerpc: ...
44
  	spin_lock_irqsave(&mmio_nvram_lock, flags);
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
45

f3f66f599   Arnd Bergmann   [PATCH] powerpc: ...
46
  	memcpy_fromio(buf, mmio_nvram_start + *index, count);
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
47

f3f66f599   Arnd Bergmann   [PATCH] powerpc: ...
48
  	spin_unlock_irqrestore(&mmio_nvram_lock, flags);
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
49
50
51
52
  	
  	*index += count;
  	return count;
  }
690a2d074   Martyn Welch   powerpc/nvram: Al...
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  static unsigned char mmio_nvram_read_val(int addr)
  {
  	unsigned long flags;
  	unsigned char val;
  
  	if (addr >= mmio_nvram_len)
  		return 0xff;
  
  	spin_lock_irqsave(&mmio_nvram_lock, flags);
  
  	val = ioread8(mmio_nvram_start + addr);
  
  	spin_unlock_irqrestore(&mmio_nvram_lock, flags);
  
  	return val;
  }
f3f66f599   Arnd Bergmann   [PATCH] powerpc: ...
69
  static ssize_t mmio_nvram_write(char *buf, size_t count, loff_t *index)
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
70
71
  {
  	unsigned long flags;
f3f66f599   Arnd Bergmann   [PATCH] powerpc: ...
72
  	if (*index >= mmio_nvram_len)
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
73
  		return 0;
f3f66f599   Arnd Bergmann   [PATCH] powerpc: ...
74
75
  	if (*index + count > mmio_nvram_len)
  		count = mmio_nvram_len - *index;
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
76

f3f66f599   Arnd Bergmann   [PATCH] powerpc: ...
77
  	spin_lock_irqsave(&mmio_nvram_lock, flags);
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
78

f3f66f599   Arnd Bergmann   [PATCH] powerpc: ...
79
  	memcpy_toio(mmio_nvram_start + *index, buf, count);
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
80

f3f66f599   Arnd Bergmann   [PATCH] powerpc: ...
81
  	spin_unlock_irqrestore(&mmio_nvram_lock, flags);
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
82
83
84
85
  	
  	*index += count;
  	return count;
  }
690a2d074   Martyn Welch   powerpc/nvram: Al...
86
87
88
89
90
91
92
93
94
95
96
97
  void mmio_nvram_write_val(int addr, unsigned char val)
  {
  	unsigned long flags;
  
  	if (addr < mmio_nvram_len) {
  		spin_lock_irqsave(&mmio_nvram_lock, flags);
  
  		iowrite8(val, mmio_nvram_start + addr);
  
  		spin_unlock_irqrestore(&mmio_nvram_lock, flags);
  	}
  }
f3f66f599   Arnd Bergmann   [PATCH] powerpc: ...
98
  static ssize_t mmio_nvram_get_size(void)
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
99
  {
f3f66f599   Arnd Bergmann   [PATCH] powerpc: ...
100
  	return mmio_nvram_len;
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
101
  }
f3f66f599   Arnd Bergmann   [PATCH] powerpc: ...
102
  int __init mmio_nvram_init(void)
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
103
104
  {
  	struct device_node *nvram_node;
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
105
  	unsigned long nvram_addr;
6984ee797   Benjamin Herrenschmidt   [POWERPC] Fix cel...
106
  	struct resource r;
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
107
  	int ret;
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
108
  	nvram_node = of_find_node_by_type(NULL, "nvram");
411e689d9   Benjamin Herrenschmidt   powerpc/nvram: Se...
109
110
  	if (!nvram_node)
  		nvram_node = of_find_compatible_node(NULL, NULL, "nvram");
6984ee797   Benjamin Herrenschmidt   [POWERPC] Fix cel...
111
112
113
114
115
116
117
118
119
120
121
  	if (!nvram_node) {
  		printk(KERN_WARNING "nvram: no node found in device-tree
  ");
  		return -ENODEV;
  	}
  
  	ret = of_address_to_resource(nvram_node, 0, &r);
  	if (ret) {
  		printk(KERN_WARNING "nvram: failed to get address (err %d)
  ",
  		       ret);
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
122
  		goto out;
6984ee797   Benjamin Herrenschmidt   [POWERPC] Fix cel...
123
124
  	}
  	nvram_addr = r.start;
28f65c11f   Joe Perches   treewide: Convert...
125
  	mmio_nvram_len = resource_size(&r);
6984ee797   Benjamin Herrenschmidt   [POWERPC] Fix cel...
126
  	if ( (!mmio_nvram_len) || (!nvram_addr) ) {
00d70419f   joe@perches.com   [POWERPC] arch/po...
127
128
  		printk(KERN_WARNING "nvram: address or length is 0
  ");
6984ee797   Benjamin Herrenschmidt   [POWERPC] Fix cel...
129
  		ret = -EIO;
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
130
  		goto out;
6984ee797   Benjamin Herrenschmidt   [POWERPC] Fix cel...
131
  	}
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
132

f3f66f599   Arnd Bergmann   [PATCH] powerpc: ...
133
  	mmio_nvram_start = ioremap(nvram_addr, mmio_nvram_len);
6984ee797   Benjamin Herrenschmidt   [POWERPC] Fix cel...
134
135
136
137
  	if (!mmio_nvram_start) {
  		printk(KERN_WARNING "nvram: failed to ioremap
  ");
  		ret = -ENOMEM;
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
138
  		goto out;
6984ee797   Benjamin Herrenschmidt   [POWERPC] Fix cel...
139
  	}
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
140

6984ee797   Benjamin Herrenschmidt   [POWERPC] Fix cel...
141
142
143
  	printk(KERN_INFO "mmio NVRAM, %luk at 0x%lx mapped to %p
  ",
  	       mmio_nvram_len >> 10, nvram_addr, mmio_nvram_start);
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
144

690a2d074   Martyn Welch   powerpc/nvram: Al...
145
146
  	ppc_md.nvram_read_val	= mmio_nvram_read_val;
  	ppc_md.nvram_write_val	= mmio_nvram_write_val;
f3f66f599   Arnd Bergmann   [PATCH] powerpc: ...
147
148
149
  	ppc_md.nvram_read	= mmio_nvram_read;
  	ppc_md.nvram_write	= mmio_nvram_write;
  	ppc_md.nvram_size	= mmio_nvram_get_size;
5f5b4e669   Utz Bacher   [PATCH] ppc64: ad...
150
151
152
153
154
  
  out:
  	of_node_put(nvram_node);
  	return ret;
  }