blob: 44e039865aa99714d4184e033a2d5bce65d94532 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: flash.c,v 1.25 2001/12/21 04:56:16 davem Exp $
2 * flash.c: Allow mmap access to the OBP Flash, for OBP updates.
3 *
4 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
5 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/module.h>
8#include <linux/types.h>
9#include <linux/errno.h>
10#include <linux/miscdevice.h>
11#include <linux/slab.h>
12#include <linux/fcntl.h>
13#include <linux/poll.h>
14#include <linux/init.h>
15#include <linux/smp_lock.h>
16#include <linux/spinlock.h>
Horst H. von Brand8a737092007-05-31 01:27:52 -070017#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <asm/system.h>
20#include <asm/uaccess.h>
21#include <asm/pgtable.h>
22#include <asm/io.h>
23#include <asm/sbus.h>
24#include <asm/ebus.h>
25#include <asm/upa.h>
26
27static DEFINE_SPINLOCK(flash_lock);
28static struct {
29 unsigned long read_base; /* Physical read address */
30 unsigned long write_base; /* Physical write address */
31 unsigned long read_size; /* Size of read area */
32 unsigned long write_size; /* Size of write area */
33 unsigned long busy; /* In use? */
34} flash;
35
36#define FLASH_MINOR 152
37
38static int
39flash_mmap(struct file *file, struct vm_area_struct *vma)
40{
41 unsigned long addr;
42 unsigned long size;
43
44 spin_lock(&flash_lock);
45 if (flash.read_base == flash.write_base) {
46 addr = flash.read_base;
47 size = flash.read_size;
48 } else {
49 if ((vma->vm_flags & VM_READ) &&
50 (vma->vm_flags & VM_WRITE)) {
51 spin_unlock(&flash_lock);
52 return -EINVAL;
53 }
54 if (vma->vm_flags & VM_READ) {
55 addr = flash.read_base;
56 size = flash.read_size;
57 } else if (vma->vm_flags & VM_WRITE) {
58 addr = flash.write_base;
59 size = flash.write_size;
60 } else {
61 spin_unlock(&flash_lock);
62 return -ENXIO;
63 }
64 }
65 spin_unlock(&flash_lock);
66
67 if ((vma->vm_pgoff << PAGE_SHIFT) > size)
68 return -ENXIO;
69 addr = vma->vm_pgoff + (addr >> PAGE_SHIFT);
70
71 if (vma->vm_end - (vma->vm_start + (vma->vm_pgoff << PAGE_SHIFT)) > size)
72 size = vma->vm_end - (vma->vm_start + (vma->vm_pgoff << PAGE_SHIFT));
73
David S. Miller14778d92006-03-21 02:29:39 -080074 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 if (io_remap_pfn_range(vma, vma->vm_start, addr, size, vma->vm_page_prot))
77 return -EAGAIN;
78
79 return 0;
80}
81
82static long long
83flash_llseek(struct file *file, long long offset, int origin)
84{
85 lock_kernel();
86 switch (origin) {
87 case 0:
88 file->f_pos = offset;
89 break;
90 case 1:
91 file->f_pos += offset;
92 if (file->f_pos > flash.read_size)
93 file->f_pos = flash.read_size;
94 break;
95 case 2:
96 file->f_pos = flash.read_size;
97 break;
98 default:
99 unlock_kernel();
100 return -EINVAL;
101 }
102 unlock_kernel();
103 return file->f_pos;
104}
105
106static ssize_t
107flash_read(struct file * file, char __user * buf,
108 size_t count, loff_t *ppos)
109{
110 unsigned long p = file->f_pos;
111 int i;
112
113 if (count > flash.read_size - p)
114 count = flash.read_size - p;
115
116 for (i = 0; i < count; i++) {
117 u8 data = upa_readb(flash.read_base + p + i);
118 if (put_user(data, buf))
119 return -EFAULT;
120 buf++;
121 }
122
123 file->f_pos += count;
124 return count;
125}
126
127static int
128flash_open(struct inode *inode, struct file *file)
129{
130 if (test_and_set_bit(0, (void *)&flash.busy) != 0)
131 return -EBUSY;
132
133 return 0;
134}
135
136static int
137flash_release(struct inode *inode, struct file *file)
138{
139 spin_lock(&flash_lock);
140 flash.busy = 0;
141 spin_unlock(&flash_lock);
142
143 return 0;
144}
145
Arjan van de Ven00977a52007-02-12 00:55:34 -0800146static const struct file_operations flash_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 /* no write to the Flash, use mmap
148 * and play flash dependent tricks.
149 */
150 .owner = THIS_MODULE,
151 .llseek = flash_llseek,
152 .read = flash_read,
153 .mmap = flash_mmap,
154 .open = flash_open,
155 .release = flash_release,
156};
157
158static struct miscdevice flash_dev = { FLASH_MINOR, "flash", &flash_fops };
159
160static int __init flash_init(void)
161{
162 struct sbus_bus *sbus;
163 struct sbus_dev *sdev = NULL;
164#ifdef CONFIG_PCI
165 struct linux_ebus *ebus;
166 struct linux_ebus_device *edev = NULL;
167 struct linux_prom_registers regs[2];
168 int len, nregs;
169#endif
170 int err;
171
172 for_all_sbusdev(sdev, sbus) {
173 if (!strcmp(sdev->prom_name, "flashprom")) {
174 if (sdev->reg_addrs[0].phys_addr == sdev->reg_addrs[1].phys_addr) {
175 flash.read_base = ((unsigned long)sdev->reg_addrs[0].phys_addr) |
176 (((unsigned long)sdev->reg_addrs[0].which_io)<<32UL);
177 flash.read_size = sdev->reg_addrs[0].reg_size;
178 flash.write_base = flash.read_base;
179 flash.write_size = flash.read_size;
180 } else {
181 flash.read_base = ((unsigned long)sdev->reg_addrs[0].phys_addr) |
182 (((unsigned long)sdev->reg_addrs[0].which_io)<<32UL);
183 flash.read_size = sdev->reg_addrs[0].reg_size;
184 flash.write_base = ((unsigned long)sdev->reg_addrs[1].phys_addr) |
185 (((unsigned long)sdev->reg_addrs[1].which_io)<<32UL);
186 flash.write_size = sdev->reg_addrs[1].reg_size;
187 }
188 flash.busy = 0;
189 break;
190 }
191 }
192 if (!sdev) {
193#ifdef CONFIG_PCI
Stephen Rothwellccf0dec2007-03-29 00:49:54 -0700194 const struct linux_prom_registers *ebus_regs;
David S. Miller690c8fd2006-06-22 19:12:03 -0700195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 for_each_ebus(ebus) {
197 for_each_ebusdev(edev, ebus) {
David S. Miller690c8fd2006-06-22 19:12:03 -0700198 if (!strcmp(edev->prom_node->name, "flashprom"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 goto ebus_done;
200 }
201 }
202 ebus_done:
203 if (!edev)
204 return -ENODEV;
205
David S. Miller690c8fd2006-06-22 19:12:03 -0700206 ebus_regs = of_get_property(edev->prom_node, "reg", &len);
207 if (!ebus_regs || (len % sizeof(regs[0])) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 printk("flash: Strange reg property size %d\n", len);
209 return -ENODEV;
210 }
211
David S. Miller690c8fd2006-06-22 19:12:03 -0700212 nregs = len / sizeof(ebus_regs[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 flash.read_base = edev->resource[0].start;
David S. Miller690c8fd2006-06-22 19:12:03 -0700215 flash.read_size = ebus_regs[0].reg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 if (nregs == 1) {
218 flash.write_base = edev->resource[0].start;
David S. Miller690c8fd2006-06-22 19:12:03 -0700219 flash.write_size = ebus_regs[0].reg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 } else if (nregs == 2) {
221 flash.write_base = edev->resource[1].start;
David S. Miller690c8fd2006-06-22 19:12:03 -0700222 flash.write_size = ebus_regs[1].reg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 } else {
224 printk("flash: Strange number of regs %d\n", nregs);
225 return -ENODEV;
226 }
227
228 flash.busy = 0;
229
230#else
231 return -ENODEV;
232#endif
233 }
234
235 printk("OBP Flash: RD %lx[%lx] WR %lx[%lx]\n",
236 flash.read_base, flash.read_size,
237 flash.write_base, flash.write_size);
238
239 err = misc_register(&flash_dev);
240 if (err) {
241 printk(KERN_ERR "flash: unable to get misc minor\n");
242 return err;
243 }
244
245 return 0;
246}
247
248static void __exit flash_cleanup(void)
249{
250 misc_deregister(&flash_dev);
251}
252
253module_init(flash_init);
254module_exit(flash_cleanup);
255MODULE_LICENSE("GPL");