blob: 2beb3dded087cf5771ae022ad9ee528d2d9d73c5 [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
7#include <linux/config.h>
8#include <linux/module.h>
9#include <linux/types.h>
10#include <linux/errno.h>
11#include <linux/miscdevice.h>
12#include <linux/slab.h>
13#include <linux/fcntl.h>
14#include <linux/poll.h>
15#include <linux/init.h>
16#include <linux/smp_lock.h>
17#include <linux/spinlock.h>
18
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 vma->vm_flags |= (VM_SHM | VM_LOCKED);
David S. Miller14778d92006-03-21 02:29:39 -080075 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 if (io_remap_pfn_range(vma, vma->vm_start, addr, size, vma->vm_page_prot))
78 return -EAGAIN;
79
80 return 0;
81}
82
83static long long
84flash_llseek(struct file *file, long long offset, int origin)
85{
86 lock_kernel();
87 switch (origin) {
88 case 0:
89 file->f_pos = offset;
90 break;
91 case 1:
92 file->f_pos += offset;
93 if (file->f_pos > flash.read_size)
94 file->f_pos = flash.read_size;
95 break;
96 case 2:
97 file->f_pos = flash.read_size;
98 break;
99 default:
100 unlock_kernel();
101 return -EINVAL;
102 }
103 unlock_kernel();
104 return file->f_pos;
105}
106
107static ssize_t
108flash_read(struct file * file, char __user * buf,
109 size_t count, loff_t *ppos)
110{
111 unsigned long p = file->f_pos;
112 int i;
113
114 if (count > flash.read_size - p)
115 count = flash.read_size - p;
116
117 for (i = 0; i < count; i++) {
118 u8 data = upa_readb(flash.read_base + p + i);
119 if (put_user(data, buf))
120 return -EFAULT;
121 buf++;
122 }
123
124 file->f_pos += count;
125 return count;
126}
127
128static int
129flash_open(struct inode *inode, struct file *file)
130{
131 if (test_and_set_bit(0, (void *)&flash.busy) != 0)
132 return -EBUSY;
133
134 return 0;
135}
136
137static int
138flash_release(struct inode *inode, struct file *file)
139{
140 spin_lock(&flash_lock);
141 flash.busy = 0;
142 spin_unlock(&flash_lock);
143
144 return 0;
145}
146
147static struct file_operations flash_fops = {
148 /* no write to the Flash, use mmap
149 * and play flash dependent tricks.
150 */
151 .owner = THIS_MODULE,
152 .llseek = flash_llseek,
153 .read = flash_read,
154 .mmap = flash_mmap,
155 .open = flash_open,
156 .release = flash_release,
157};
158
159static struct miscdevice flash_dev = { FLASH_MINOR, "flash", &flash_fops };
160
161static int __init flash_init(void)
162{
163 struct sbus_bus *sbus;
164 struct sbus_dev *sdev = NULL;
165#ifdef CONFIG_PCI
166 struct linux_ebus *ebus;
167 struct linux_ebus_device *edev = NULL;
168 struct linux_prom_registers regs[2];
169 int len, nregs;
170#endif
171 int err;
172
173 for_all_sbusdev(sdev, sbus) {
174 if (!strcmp(sdev->prom_name, "flashprom")) {
175 if (sdev->reg_addrs[0].phys_addr == sdev->reg_addrs[1].phys_addr) {
176 flash.read_base = ((unsigned long)sdev->reg_addrs[0].phys_addr) |
177 (((unsigned long)sdev->reg_addrs[0].which_io)<<32UL);
178 flash.read_size = sdev->reg_addrs[0].reg_size;
179 flash.write_base = flash.read_base;
180 flash.write_size = flash.read_size;
181 } else {
182 flash.read_base = ((unsigned long)sdev->reg_addrs[0].phys_addr) |
183 (((unsigned long)sdev->reg_addrs[0].which_io)<<32UL);
184 flash.read_size = sdev->reg_addrs[0].reg_size;
185 flash.write_base = ((unsigned long)sdev->reg_addrs[1].phys_addr) |
186 (((unsigned long)sdev->reg_addrs[1].which_io)<<32UL);
187 flash.write_size = sdev->reg_addrs[1].reg_size;
188 }
189 flash.busy = 0;
190 break;
191 }
192 }
193 if (!sdev) {
194#ifdef CONFIG_PCI
195 for_each_ebus(ebus) {
196 for_each_ebusdev(edev, ebus) {
197 if (!strcmp(edev->prom_name, "flashprom"))
198 goto ebus_done;
199 }
200 }
201 ebus_done:
202 if (!edev)
203 return -ENODEV;
204
205 len = prom_getproperty(edev->prom_node, "reg", (void *)regs, sizeof(regs));
206 if ((len % sizeof(regs[0])) != 0) {
207 printk("flash: Strange reg property size %d\n", len);
208 return -ENODEV;
209 }
210
211 nregs = len / sizeof(regs[0]);
212
213 flash.read_base = edev->resource[0].start;
214 flash.read_size = regs[0].reg_size;
215
216 if (nregs == 1) {
217 flash.write_base = edev->resource[0].start;
218 flash.write_size = regs[0].reg_size;
219 } else if (nregs == 2) {
220 flash.write_base = edev->resource[1].start;
221 flash.write_size = regs[1].reg_size;
222 } else {
223 printk("flash: Strange number of regs %d\n", nregs);
224 return -ENODEV;
225 }
226
227 flash.busy = 0;
228
229#else
230 return -ENODEV;
231#endif
232 }
233
234 printk("OBP Flash: RD %lx[%lx] WR %lx[%lx]\n",
235 flash.read_base, flash.read_size,
236 flash.write_base, flash.write_size);
237
238 err = misc_register(&flash_dev);
239 if (err) {
240 printk(KERN_ERR "flash: unable to get misc minor\n");
241 return err;
242 }
243
244 return 0;
245}
246
247static void __exit flash_cleanup(void)
248{
249 misc_deregister(&flash_dev);
250}
251
252module_init(flash_init);
253module_exit(flash_cleanup);
254MODULE_LICENSE("GPL");