1 /* $Id: flash.c,v 1.24 2001/10/08 22:19:51 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
26 static spinlock_t flash_lock = SPIN_LOCK_UNLOCKED;
27 static struct {
28 unsigned long read_base; /* Physical read address */
29 unsigned long write_base; /* Physical write address */
30 unsigned long read_size; /* Size of read area */
31 unsigned long write_size; /* Size of write area */
32 unsigned long busy; /* In use? */
33 } flash;
34
35 #define FLASH_MINOR 152
36
37 static int
flash_mmap(struct file * file,struct vm_area_struct * vma)38 flash_mmap(struct file *file, struct vm_area_struct *vma)
39 {
40 unsigned long addr;
41 unsigned long size;
42
43 spin_lock(&flash_lock);
44 if (flash.read_base == flash.write_base) {
45 addr = flash.read_base;
46 size = flash.read_size;
47 } else {
48 if ((vma->vm_flags & VM_READ) &&
49 (vma->vm_flags & VM_WRITE)) {
50 spin_unlock(&flash_lock);
51 return -EINVAL;
52 }
53 if (vma->vm_flags & VM_READ) {
54 addr = flash.read_base;
55 size = flash.read_size;
56 } else if (vma->vm_flags & VM_WRITE) {
57 addr = flash.write_base;
58 size = flash.write_size;
59 } else {
60 spin_unlock(&flash_lock);
61 return -ENXIO;
62 }
63 }
64 spin_unlock(&flash_lock);
65
66 if ((vma->vm_pgoff << PAGE_SHIFT) > size)
67 return -ENXIO;
68 addr += (vma->vm_pgoff << PAGE_SHIFT);
69
70 if (vma->vm_end - (vma->vm_start + (vma->vm_pgoff << PAGE_SHIFT)) > size)
71 size = vma->vm_end - (vma->vm_start + (vma->vm_pgoff << PAGE_SHIFT));
72
73 pgprot_val(vma->vm_page_prot) &= ~(_PAGE_CACHE);
74 pgprot_val(vma->vm_page_prot) |= _PAGE_E;
75 vma->vm_flags |= (VM_SHM | VM_LOCKED);
76
77 if (remap_page_range(vma->vm_start, addr, size, vma->vm_page_prot))
78 return -EAGAIN;
79
80 return 0;
81 }
82
83 static long long
flash_llseek(struct file * file,long long offset,int origin)84 flash_llseek(struct file *file, long long offset, int origin)
85 {
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 return -EINVAL;
100 }
101 return file->f_pos;
102 }
103
104 static ssize_t
flash_read(struct file * file,char * buf,size_t count,loff_t * ppos)105 flash_read(struct file * file, char * buf,
106 size_t count, loff_t *ppos)
107 {
108 loff_t p = *ppos;
109 int i;
110
111 if (p > flash.read_size)
112 return 0;
113
114 if (p < 0)
115 return -EINVAL;
116
117 if (count > flash.read_size - p)
118 count = flash.read_size - p;
119
120 for (i = 0; i < count; i++) {
121 u8 data = readb(flash.read_base + p + i);
122 if (put_user(data, buf))
123 return -EFAULT;
124 buf++;
125 }
126
127 *ppos = p + count;
128 return count;
129 }
130
131 static int
flash_open(struct inode * inode,struct file * file)132 flash_open(struct inode *inode, struct file *file)
133 {
134 if (test_and_set_bit(0, (void *)&flash.busy) != 0)
135 return -EBUSY;
136
137 return 0;
138 }
139
140 static int
flash_release(struct inode * inode,struct file * file)141 flash_release(struct inode *inode, struct file *file)
142 {
143 spin_lock(&flash_lock);
144 flash.busy = 0;
145 spin_unlock(&flash_lock);
146
147 return 0;
148 }
149
150 static struct file_operations flash_fops = {
151 /* no write to the Flash, use mmap
152 * and play flash dependent tricks.
153 */
154 owner: THIS_MODULE,
155 llseek: flash_llseek,
156 read: flash_read,
157 mmap: flash_mmap,
158 open: flash_open,
159 release: flash_release,
160 };
161
162 static struct miscdevice flash_dev = { FLASH_MINOR, "flash", &flash_fops };
163
164 EXPORT_NO_SYMBOLS;
165
flash_init(void)166 static int __init flash_init(void)
167 {
168 struct sbus_bus *sbus;
169 struct sbus_dev *sdev = 0;
170 #ifdef CONFIG_PCI
171 struct linux_ebus *ebus;
172 struct linux_ebus_device *edev = 0;
173 struct linux_prom_registers regs[2];
174 int len, nregs;
175 #endif
176 int err;
177
178 for_all_sbusdev(sdev, sbus) {
179 if (!strcmp(sdev->prom_name, "flashprom")) {
180 if (sdev->reg_addrs[0].phys_addr == sdev->reg_addrs[1].phys_addr) {
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 = flash.read_base;
185 flash.write_size = flash.read_size;
186 } else {
187 flash.read_base = ((unsigned long)sdev->reg_addrs[0].phys_addr) |
188 (((unsigned long)sdev->reg_addrs[0].which_io)<<32UL);
189 flash.read_size = sdev->reg_addrs[0].reg_size;
190 flash.write_base = ((unsigned long)sdev->reg_addrs[1].phys_addr) |
191 (((unsigned long)sdev->reg_addrs[1].which_io)<<32UL);
192 flash.write_size = sdev->reg_addrs[1].reg_size;
193 }
194 flash.busy = 0;
195 break;
196 }
197 }
198 if (!sdev) {
199 #ifdef CONFIG_PCI
200 for_each_ebus(ebus) {
201 for_each_ebusdev(edev, ebus) {
202 if (!strcmp(edev->prom_name, "flashprom"))
203 goto ebus_done;
204 }
205 }
206 ebus_done:
207 if (!edev)
208 return -ENODEV;
209
210 len = prom_getproperty(edev->prom_node, "reg", (void *)regs, sizeof(regs));
211 if ((len % sizeof(regs[0])) != 0) {
212 printk("flash: Strange reg property size %d\n", len);
213 return -ENODEV;
214 }
215
216 nregs = len / sizeof(regs[0]);
217
218 flash.read_base = edev->resource[0].start;
219 flash.read_size = regs[0].reg_size;
220
221 if (nregs == 1) {
222 flash.write_base = edev->resource[0].start;
223 flash.write_size = regs[0].reg_size;
224 } else if (nregs == 2) {
225 flash.write_base = edev->resource[1].start;
226 flash.write_size = regs[1].reg_size;
227 } else {
228 printk("flash: Strange number of regs %d\n", nregs);
229 return -ENODEV;
230 }
231
232 flash.busy = 0;
233
234 #else
235 return -ENODEV;
236 #endif
237 }
238
239 printk("OBP Flash: RD %lx[%lx] WR %lx[%lx]\n",
240 flash.read_base, flash.read_size,
241 flash.write_base, flash.write_size);
242
243 err = misc_register(&flash_dev);
244 if (err) {
245 printk(KERN_ERR "flash: unable to get misc minor\n");
246 return err;
247 }
248
249 return 0;
250 }
251
flash_cleanup(void)252 static void __exit flash_cleanup(void)
253 {
254 misc_deregister(&flash_dev);
255 }
256
257 module_init(flash_init);
258 module_exit(flash_cleanup);
259 MODULE_LICENSE("GPL");
260