1 /* $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.
4 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
7 #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>
19 #include <asm/system.h>
20 #include <asm/uaccess.h>
21 #include <asm/pgtable.h>
27 static DEFINE_SPINLOCK(flash_lock);
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? */
36 #define FLASH_MINOR 152
39 flash_mmap(struct file *file, struct vm_area_struct *vma)
44 spin_lock(&flash_lock);
45 if (flash.read_base == flash.write_base) {
46 addr = flash.read_base;
47 size = flash.read_size;
49 if ((vma->vm_flags & VM_READ) &&
50 (vma->vm_flags & VM_WRITE)) {
51 spin_unlock(&flash_lock);
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;
61 spin_unlock(&flash_lock);
65 spin_unlock(&flash_lock);
67 if ((vma->vm_pgoff << PAGE_SHIFT) > size)
69 addr = vma->vm_pgoff + (addr >> PAGE_SHIFT);
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));
74 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
76 if (io_remap_pfn_range(vma, vma->vm_start, addr, size, vma->vm_page_prot))
83 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;
107 flash_read(struct file * file, char __user * buf,
108 size_t count, loff_t *ppos)
110 unsigned long p = file->f_pos;
113 if (count > flash.read_size - p)
114 count = flash.read_size - p;
116 for (i = 0; i < count; i++) {
117 u8 data = upa_readb(flash.read_base + p + i);
118 if (put_user(data, buf))
123 file->f_pos += count;
128 flash_open(struct inode *inode, struct file *file)
131 if (test_and_set_bit(0, (void *)&flash.busy) != 0) {
141 flash_release(struct inode *inode, struct file *file)
143 spin_lock(&flash_lock);
145 spin_unlock(&flash_lock);
150 static const struct file_operations flash_fops = {
151 /* no write to the Flash, use mmap
152 * and play flash dependent tricks.
154 .owner = THIS_MODULE,
155 .llseek = flash_llseek,
159 .release = flash_release,
162 static struct miscdevice flash_dev = { FLASH_MINOR, "flash", &flash_fops };
164 static int __init flash_init(void)
166 struct sbus_bus *sbus;
167 struct sbus_dev *sdev = NULL;
169 struct linux_ebus *ebus;
170 struct linux_ebus_device *edev = NULL;
171 struct linux_prom_registers regs[2];
176 for_all_sbusdev(sdev, sbus) {
177 if (!strcmp(sdev->prom_name, "flashprom")) {
178 if (sdev->reg_addrs[0].phys_addr == sdev->reg_addrs[1].phys_addr) {
179 flash.read_base = ((unsigned long)sdev->reg_addrs[0].phys_addr) |
180 (((unsigned long)sdev->reg_addrs[0].which_io)<<32UL);
181 flash.read_size = sdev->reg_addrs[0].reg_size;
182 flash.write_base = flash.read_base;
183 flash.write_size = flash.read_size;
185 flash.read_base = ((unsigned long)sdev->reg_addrs[0].phys_addr) |
186 (((unsigned long)sdev->reg_addrs[0].which_io)<<32UL);
187 flash.read_size = sdev->reg_addrs[0].reg_size;
188 flash.write_base = ((unsigned long)sdev->reg_addrs[1].phys_addr) |
189 (((unsigned long)sdev->reg_addrs[1].which_io)<<32UL);
190 flash.write_size = sdev->reg_addrs[1].reg_size;
198 const struct linux_prom_registers *ebus_regs;
200 for_each_ebus(ebus) {
201 for_each_ebusdev(edev, ebus) {
202 if (!strcmp(edev->prom_node->name, "flashprom"))
210 ebus_regs = of_get_property(edev->prom_node, "reg", &len);
211 if (!ebus_regs || (len % sizeof(regs[0])) != 0) {
212 printk("flash: Strange reg property size %d\n", len);
216 nregs = len / sizeof(ebus_regs[0]);
218 flash.read_base = edev->resource[0].start;
219 flash.read_size = ebus_regs[0].reg_size;
222 flash.write_base = edev->resource[0].start;
223 flash.write_size = ebus_regs[0].reg_size;
224 } else if (nregs == 2) {
225 flash.write_base = edev->resource[1].start;
226 flash.write_size = ebus_regs[1].reg_size;
228 printk("flash: Strange number of regs %d\n", nregs);
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);
243 err = misc_register(&flash_dev);
245 printk(KERN_ERR "flash: unable to get misc minor\n");
252 static void __exit flash_cleanup(void)
254 misc_deregister(&flash_dev);
257 module_init(flash_init);
258 module_exit(flash_cleanup);
259 MODULE_LICENSE("GPL");