]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/sbus/char/flash.c
Merge branch 'tip/perf/core' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[mirror_ubuntu-eoan-kernel.git] / drivers / sbus / char / flash.c
CommitLineData
a9540d34 1/* flash.c: Allow mmap access to the OBP Flash, for OBP updates.
1da177e4
LT
2 *
3 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
4 */
5
1da177e4
LT
6#include <linux/module.h>
7#include <linux/types.h>
8#include <linux/errno.h>
9#include <linux/miscdevice.h>
1da177e4
LT
10#include <linux/fcntl.h>
11#include <linux/poll.h>
12#include <linux/init.h>
a3108ca2 13#include <linux/mutex.h>
1da177e4 14#include <linux/spinlock.h>
8a73709e 15#include <linux/mm.h>
a9540d34
DM
16#include <linux/of.h>
17#include <linux/of_device.h>
1da177e4
LT
18
19#include <asm/system.h>
20#include <asm/uaccess.h>
21#include <asm/pgtable.h>
22#include <asm/io.h>
1da177e4
LT
23#include <asm/upa.h>
24
a3108ca2 25static DEFINE_MUTEX(flash_mutex);
1da177e4
LT
26static DEFINE_SPINLOCK(flash_lock);
27static struct {
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? */
33} flash;
34
35#define FLASH_MINOR 152
36
37static int
38flash_mmap(struct file *file, struct vm_area_struct *vma)
39{
40 unsigned long addr;
41 unsigned long size;
42
43 spin_lock(&flash_lock);
44 if (flash.read_base == flash.write_base) {
45 addr = flash.read_base;
46 size = flash.read_size;
47 } else {
48 if ((vma->vm_flags & VM_READ) &&
49 (vma->vm_flags & VM_WRITE)) {
50 spin_unlock(&flash_lock);
51 return -EINVAL;
52 }
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;
59 } else {
60 spin_unlock(&flash_lock);
61 return -ENXIO;
62 }
63 }
64 spin_unlock(&flash_lock);
65
66 if ((vma->vm_pgoff << PAGE_SHIFT) > size)
67 return -ENXIO;
68 addr = vma->vm_pgoff + (addr >> PAGE_SHIFT);
69
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));
72
14778d90 73 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1da177e4
LT
74
75 if (io_remap_pfn_range(vma, vma->vm_start, addr, size, vma->vm_page_prot))
76 return -EAGAIN;
77
78 return 0;
79}
80
81static long long
82flash_llseek(struct file *file, long long offset, int origin)
83{
a3108ca2 84 mutex_lock(&flash_mutex);
1da177e4
LT
85 switch (origin) {
86 case 0:
87 file->f_pos = offset;
88 break;
89 case 1:
90 file->f_pos += offset;
91 if (file->f_pos > flash.read_size)
92 file->f_pos = flash.read_size;
93 break;
94 case 2:
95 file->f_pos = flash.read_size;
96 break;
97 default:
a3108ca2 98 mutex_unlock(&flash_mutex);
1da177e4
LT
99 return -EINVAL;
100 }
a3108ca2 101 mutex_unlock(&flash_mutex);
1da177e4
LT
102 return file->f_pos;
103}
104
105static ssize_t
106flash_read(struct file * file, char __user * buf,
107 size_t count, loff_t *ppos)
108{
be94bbb5 109 loff_t p = *ppos;
1da177e4 110 int i;
be94bbb5 111
1da177e4
LT
112 if (count > flash.read_size - p)
113 count = flash.read_size - p;
114
115 for (i = 0; i < count; i++) {
116 u8 data = upa_readb(flash.read_base + p + i);
117 if (put_user(data, buf))
118 return -EFAULT;
119 buf++;
120 }
121
be94bbb5 122 *ppos += count;
1da177e4
LT
123 return count;
124}
125
126static int
127flash_open(struct inode *inode, struct file *file)
128{
a3108ca2 129 mutex_lock(&flash_mutex);
78abb6ac 130 if (test_and_set_bit(0, (void *)&flash.busy) != 0) {
a3108ca2 131 mutex_unlock(&flash_mutex);
1da177e4 132 return -EBUSY;
78abb6ac 133 }
1da177e4 134
a3108ca2 135 mutex_unlock(&flash_mutex);
1da177e4
LT
136 return 0;
137}
138
139static int
140flash_release(struct inode *inode, struct file *file)
141{
142 spin_lock(&flash_lock);
143 flash.busy = 0;
144 spin_unlock(&flash_lock);
145
146 return 0;
147}
148
00977a59 149static const struct file_operations flash_fops = {
1da177e4
LT
150 /* no write to the Flash, use mmap
151 * and play flash dependent tricks.
152 */
153 .owner = THIS_MODULE,
154 .llseek = flash_llseek,
155 .read = flash_read,
156 .mmap = flash_mmap,
157 .open = flash_open,
158 .release = flash_release,
159};
160
161static struct miscdevice flash_dev = { FLASH_MINOR, "flash", &flash_fops };
162
4ebb24f7 163static int __devinit flash_probe(struct platform_device *op)
1da177e4 164{
61c7a080 165 struct device_node *dp = op->dev.of_node;
a9540d34 166 struct device_node *parent;
1da177e4 167
a9540d34 168 parent = dp->parent;
1da177e4 169
a9540d34
DM
170 if (strcmp(parent->name, "sbus") &&
171 strcmp(parent->name, "sbi") &&
172 strcmp(parent->name, "ebus"))
1da177e4 173 return -ENODEV;
a9540d34
DM
174
175 flash.read_base = op->resource[0].start;
176 flash.read_size = resource_size(&op->resource[0]);
177 if (op->resource[1].flags) {
178 flash.write_base = op->resource[1].start;
179 flash.write_size = resource_size(&op->resource[1]);
180 } else {
181 flash.write_base = op->resource[0].start;
182 flash.write_size = resource_size(&op->resource[0]);
1da177e4 183 }
a9540d34 184 flash.busy = 0;
1da177e4 185
a9540d34 186 printk(KERN_INFO "%s: OBP Flash, RD %lx[%lx] WR %lx[%lx]\n",
61c7a080 187 op->dev.of_node->full_name,
1da177e4
LT
188 flash.read_base, flash.read_size,
189 flash.write_base, flash.write_size);
190
a9540d34
DM
191 return misc_register(&flash_dev);
192}
193
2dc11581 194static int __devexit flash_remove(struct platform_device *op)
a9540d34
DM
195{
196 misc_deregister(&flash_dev);
1da177e4
LT
197
198 return 0;
199}
200
fd098316 201static const struct of_device_id flash_match[] = {
a9540d34
DM
202 {
203 .name = "flashprom",
204 },
205 {},
206};
207MODULE_DEVICE_TABLE(of, flash_match);
208
4ebb24f7 209static struct platform_driver flash_driver = {
4018294b
GL
210 .driver = {
211 .name = "flash",
212 .owner = THIS_MODULE,
213 .of_match_table = flash_match,
214 },
a9540d34
DM
215 .probe = flash_probe,
216 .remove = __devexit_p(flash_remove),
217};
218
dbf2b92d 219module_platform_driver(flash_driver);
1da177e4 220
1da177e4 221MODULE_LICENSE("GPL");