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