1 /* $Id: flash.c,v 1.13 1999/08/31 06:58:06 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/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/malloc.h>
13 #include <linux/fcntl.h>
14 #include <linux/poll.h>
15 #include <linux/init.h>
17 #include <asm/system.h>
18 #include <asm/uaccess.h>
19 #include <asm/pgtable.h>
25 unsigned long read_base
; /* Physical read address */
26 unsigned long write_base
; /* Physical write address */
27 unsigned long read_size
; /* Size of read area */
28 unsigned long write_size
; /* Size of write area */
29 unsigned long busy
; /* In use? */
32 #define FLASH_MINOR 152
35 flash_mmap(struct file
*file
, struct vm_area_struct
*vma
)
40 if (vma
->vm_offset
& ~(PAGE_MASK
))
43 if (flash
.read_base
== flash
.write_base
) {
44 addr
= flash
.read_base
;
45 size
= flash
.read_size
;
47 if ((vma
->vm_flags
& VM_READ
) &&
48 (vma
->vm_flags
& VM_WRITE
))
51 if (vma
->vm_flags
& VM_READ
) {
52 addr
= flash
.read_base
;
53 size
= flash
.read_size
;
54 } else if (vma
->vm_flags
& VM_WRITE
) {
55 addr
= flash
.write_base
;
56 size
= flash
.write_size
;
61 if (vma
->vm_offset
> size
)
63 addr
+= vma
->vm_offset
;
65 if (vma
->vm_end
- (vma
->vm_start
+ vma
->vm_offset
) > size
)
66 size
= vma
->vm_end
- (vma
->vm_start
+ vma
->vm_offset
);
68 pgprot_val(vma
->vm_page_prot
) &= ~(_PAGE_CACHE
);
69 pgprot_val(vma
->vm_page_prot
) |= _PAGE_E
;
70 vma
->vm_flags
|= (VM_SHM
| VM_LOCKED
);
72 if (remap_page_range(vma
->vm_start
, addr
, size
, vma
->vm_page_prot
))
79 flash_llseek(struct file
*file
, long long offset
, int origin
)
86 file
->f_pos
+= offset
;
87 if (file
->f_pos
> flash
.read_size
)
88 file
->f_pos
= flash
.read_size
;
91 file
->f_pos
= flash
.read_size
;
100 flash_read(struct file
* file
, char * buf
,
101 size_t count
, loff_t
*ppos
)
103 unsigned long p
= file
->f_pos
;
105 if (count
> flash
.read_size
- p
)
106 count
= flash
.read_size
- p
;
108 if (copy_to_user(buf
, flash
.read_base
+ p
, count
) < 0)
111 file
->f_pos
+= count
;
116 flash_open(struct inode
*inode
, struct file
*file
)
118 if (test_and_set_bit(0, (void *)&flash
.busy
) != 0)
126 flash_release(struct inode
*inode
, struct file
*file
)
133 static struct file_operations flash_fops
= {
136 NULL
, /* no write to the Flash, use mmap
137 * and play flash dependent tricks.
148 static struct miscdevice flash_dev
= { FLASH_MINOR
, "flash", &flash_fops
};
153 int init_module(void)
155 int __init
flash_init(void)
158 struct linux_sbus
*sbus
;
159 struct linux_sbus_device
*sdev
= 0;
160 struct linux_ebus
*ebus
;
161 struct linux_ebus_device
*edev
= 0;
162 struct linux_prom_registers regs
[2];
165 for_all_sbusdev(sdev
, sbus
) {
166 if (!strcmp(sdev
->prom_name
, "flashprom")) {
167 prom_apply_sbus_ranges(sdev
->my_bus
, &sdev
->reg_addrs
[0],
168 sdev
->num_registers
, sdev
);
169 if (sdev
->reg_addrs
[0].phys_addr
== sdev
->reg_addrs
[1].phys_addr
) {
170 flash
.read_base
= ((unsigned long)sdev
->reg_addrs
[0].phys_addr
) |
171 (((unsigned long)sdev
->reg_addrs
[0].which_io
)<<32UL);
172 flash
.read_size
= sdev
->reg_addrs
[0].reg_size
;
173 flash
.write_base
= flash
.read_base
;
174 flash
.write_size
= flash
.read_size
;
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
= ((unsigned long)sdev
->reg_addrs
[1].phys_addr
) |
180 (((unsigned long)sdev
->reg_addrs
[1].which_io
)<<32UL);
181 flash
.write_size
= sdev
->reg_addrs
[1].reg_size
;
189 for_each_ebus(ebus
) {
190 for_each_ebusdev(edev
, ebus
) {
191 if (!strcmp(edev
->prom_name
, "flashprom"))
199 len
= prom_getproperty(edev
->prom_node
, "reg", (void *)regs
, sizeof(regs
));
200 if ((len
% sizeof(regs
[0])) != 0) {
201 printk("flash: Strange reg property size %d\n", len
);
205 nregs
= len
/ sizeof(regs
[0]);
207 flash
.read_base
= edev
->resource
[0].start
;
208 flash
.read_size
= regs
[0].reg_size
;
211 flash
.write_base
= edev
->resource
[0].start
;
212 flash
.write_size
= regs
[0].reg_size
;
213 } else if (nregs
== 2) {
214 flash
.write_base
= edev
->resource
[1].start
;
215 flash
.write_size
= regs
[1].reg_size
;
217 printk("flash: Strange number of regs %d\n", nregs
);
228 printk("OBP Flash: RD %lx[%lx] WR %lx[%lx]\n",
229 flash
.read_base
, flash
.read_size
,
230 flash
.write_base
, flash
.write_size
);
232 err
= misc_register(&flash_dev
);
234 printk(KERN_ERR
"flash: unable to get misc minor\n");
242 void cleanup_module(void)
244 misc_deregister(&flash_dev
);