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