1 /* $Id: flash.c,v 1.1.1.1 2005/04/11 02:50:34 jack Exp $
2 * flash.c: Allow mmap access to the OBP Flash, for OBP updates.
4 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
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>
19 #include <asm/system.h>
20 #include <asm/uaccess.h>
21 #include <asm/pgtable.h>
26 static spinlock_t flash_lock = SPIN_LOCK_UNLOCKED;
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? */
35 #define FLASH_MINOR 152
38 flash_mmap(struct file *file, struct vm_area_struct *vma)
43 spin_lock(&flash_lock);
44 if (flash.read_base == flash.write_base) {
45 addr = flash.read_base;
46 size = flash.read_size;
48 if ((vma->vm_flags & VM_READ) &&
49 (vma->vm_flags & VM_WRITE)) {
50 spin_unlock(&flash_lock);
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;
60 spin_unlock(&flash_lock);
64 spin_unlock(&flash_lock);
66 if ((vma->vm_pgoff << PAGE_SHIFT) > size)
68 addr += (vma->vm_pgoff << PAGE_SHIFT);
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));
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);
77 if (remap_page_range(vma->vm_start, addr, size, vma->vm_page_prot))
84 flash_llseek(struct file *file, long long offset, int origin)
91 file->f_pos += offset;
92 if (file->f_pos > flash.read_size)
93 file->f_pos = flash.read_size;
96 file->f_pos = flash.read_size;
105 flash_read(struct file * file, char * buf,
106 size_t count, loff_t *ppos)
108 unsigned long p = file->f_pos;
111 if (count > flash.read_size - p)
112 count = flash.read_size - p;
114 for (i = 0; i < count; i++) {
115 u8 data = readb(flash.read_base + p + i);
116 if (put_user(data, buf))
121 file->f_pos += count;
126 flash_open(struct inode *inode, struct file *file)
128 if (test_and_set_bit(0, (void *)&flash.busy) != 0)
135 flash_release(struct inode *inode, struct file *file)
137 spin_lock(&flash_lock);
139 spin_unlock(&flash_lock);
144 static struct file_operations flash_fops = {
145 /* no write to the Flash, use mmap
146 * and play flash dependent tricks.
149 llseek: flash_llseek,
153 release: flash_release,
156 static struct miscdevice flash_dev = { FLASH_MINOR, "flash", &flash_fops };
160 static int __init flash_init(void)
162 struct sbus_bus *sbus;
163 struct sbus_dev *sdev = 0;
164 struct linux_ebus *ebus;
165 struct linux_ebus_device *edev = 0;
166 struct linux_prom_registers regs[2];
169 for_all_sbusdev(sdev, sbus) {
170 if (!strcmp(sdev->prom_name, "flashprom")) {
171 if (sdev->reg_addrs[0].phys_addr == sdev->reg_addrs[1].phys_addr) {
172 flash.read_base = ((unsigned long)sdev->reg_addrs[0].phys_addr) |
173 (((unsigned long)sdev->reg_addrs[0].which_io)<<32UL);
174 flash.read_size = sdev->reg_addrs[0].reg_size;
175 flash.write_base = flash.read_base;
176 flash.write_size = flash.read_size;
178 flash.read_base = ((unsigned long)sdev->reg_addrs[0].phys_addr) |
179 (((unsigned long)sdev->reg_addrs[0].which_io)<<32UL);
180 flash.read_size = sdev->reg_addrs[0].reg_size;
181 flash.write_base = ((unsigned long)sdev->reg_addrs[1].phys_addr) |
182 (((unsigned long)sdev->reg_addrs[1].which_io)<<32UL);
183 flash.write_size = sdev->reg_addrs[1].reg_size;
191 for_each_ebus(ebus) {
192 for_each_ebusdev(edev, ebus) {
193 if (!strcmp(edev->prom_name, "flashprom"))
201 len = prom_getproperty(edev->prom_node, "reg", (void *)regs, sizeof(regs));
202 if ((len % sizeof(regs[0])) != 0) {
203 printk("flash: Strange reg property size %d\n", len);
207 nregs = len / sizeof(regs[0]);
209 flash.read_base = edev->resource[0].start;
210 flash.read_size = regs[0].reg_size;
213 flash.write_base = edev->resource[0].start;
214 flash.write_size = regs[0].reg_size;
215 } else if (nregs == 2) {
216 flash.write_base = edev->resource[1].start;
217 flash.write_size = regs[1].reg_size;
219 printk("flash: Strange number of regs %d\n", nregs);
230 printk("OBP Flash: RD %lx[%lx] WR %lx[%lx]\n",
231 flash.read_base, flash.read_size,
232 flash.write_base, flash.write_size);
234 err = misc_register(&flash_dev);
236 printk(KERN_ERR "flash: unable to get misc minor\n");
243 static void __exit flash_cleanup(void)
245 misc_deregister(&flash_dev);
248 module_init(flash_init);
249 module_exit(flash_cleanup);
250 MODULE_LICENSE("GPL");