blob: 2b4b4b613c48b1b3a0153653e808e865f782d479 [file] [log] [blame]
David S. Millera9540d32008-08-27 01:13:12 -07001/* flash.c: Allow mmap access to the OBP Flash, for OBP updates.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/module.h>
7#include <linux/types.h>
8#include <linux/errno.h>
9#include <linux/miscdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/fcntl.h>
11#include <linux/poll.h>
12#include <linux/init.h>
Arnd Bergmanna3108ca2010-07-20 23:36:39 -070013#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/spinlock.h>
Horst H. von Brand8a737092007-05-31 01:27:52 -070015#include <linux/mm.h>
David S. Millera9540d32008-08-27 01:13:12 -070016#include <linux/of.h>
17#include <linux/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <asm/system.h>
20#include <asm/uaccess.h>
21#include <asm/pgtable.h>
22#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/upa.h>
24
Arnd Bergmanna3108ca2010-07-20 23:36:39 -070025static DEFINE_MUTEX(flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static 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
David S. Miller14778d92006-03-21 02:29:39 -080073 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
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{
Arnd Bergmanna3108ca2010-07-20 23:36:39 -070084 mutex_lock(&flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 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:
Arnd Bergmanna3108ca2010-07-20 23:36:39 -070098 mutex_unlock(&flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return -EINVAL;
100 }
Arnd Bergmanna3108ca2010-07-20 23:36:39 -0700101 mutex_unlock(&flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 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{
Jan Blunckbe94bbb2010-04-27 18:48:52 -0700109 loff_t p = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 int i;
Jan Blunckbe94bbb2010-04-27 18:48:52 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 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
Jan Blunckbe94bbb2010-04-27 18:48:52 -0700122 *ppos += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return count;
124}
125
126static int
127flash_open(struct inode *inode, struct file *file)
128{
Arnd Bergmanna3108ca2010-07-20 23:36:39 -0700129 mutex_lock(&flash_mutex);
Arnd Bergmann78abb6a2008-05-20 19:15:54 +0200130 if (test_and_set_bit(0, (void *)&flash.busy) != 0) {
Arnd Bergmanna3108ca2010-07-20 23:36:39 -0700131 mutex_unlock(&flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return -EBUSY;
Arnd Bergmann78abb6a2008-05-20 19:15:54 +0200133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Arnd Bergmanna3108ca2010-07-20 23:36:39 -0700135 mutex_unlock(&flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 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
Arjan van de Ven00977a52007-02-12 00:55:34 -0800149static const struct file_operations flash_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 /* 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
Grant Likely2dc11582010-08-06 09:25:50 -0600163static int __devinit flash_probe(struct platform_device *op,
David S. Millera9540d32008-08-27 01:13:12 -0700164 const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Grant Likely61c7a082010-04-13 16:12:29 -0700166 struct device_node *dp = op->dev.of_node;
David S. Millera9540d32008-08-27 01:13:12 -0700167 struct device_node *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
David S. Millera9540d32008-08-27 01:13:12 -0700169 parent = dp->parent;
David S. Miller690c8fd2006-06-22 19:12:03 -0700170
David S. Millera9540d32008-08-27 01:13:12 -0700171 if (strcmp(parent->name, "sbus") &&
172 strcmp(parent->name, "sbi") &&
173 strcmp(parent->name, "ebus"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
David S. Millera9540d32008-08-27 01:13:12 -0700176 flash.read_base = op->resource[0].start;
177 flash.read_size = resource_size(&op->resource[0]);
178 if (op->resource[1].flags) {
179 flash.write_base = op->resource[1].start;
180 flash.write_size = resource_size(&op->resource[1]);
181 } else {
182 flash.write_base = op->resource[0].start;
183 flash.write_size = resource_size(&op->resource[0]);
184 }
185 flash.busy = 0;
186
187 printk(KERN_INFO "%s: OBP Flash, RD %lx[%lx] WR %lx[%lx]\n",
Grant Likely61c7a082010-04-13 16:12:29 -0700188 op->dev.of_node->full_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 flash.read_base, flash.read_size,
190 flash.write_base, flash.write_size);
191
David S. Millera9540d32008-08-27 01:13:12 -0700192 return misc_register(&flash_dev);
193}
194
Grant Likely2dc11582010-08-06 09:25:50 -0600195static int __devexit flash_remove(struct platform_device *op)
David S. Millera9540d32008-08-27 01:13:12 -0700196{
197 misc_deregister(&flash_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 return 0;
200}
201
David S. Millerfd098312008-08-31 01:23:17 -0700202static const struct of_device_id flash_match[] = {
David S. Millera9540d32008-08-27 01:13:12 -0700203 {
204 .name = "flashprom",
205 },
206 {},
207};
208MODULE_DEVICE_TABLE(of, flash_match);
209
210static struct of_platform_driver flash_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700211 .driver = {
212 .name = "flash",
213 .owner = THIS_MODULE,
214 .of_match_table = flash_match,
215 },
David S. Millera9540d32008-08-27 01:13:12 -0700216 .probe = flash_probe,
217 .remove = __devexit_p(flash_remove),
218};
219
220static int __init flash_init(void)
221{
Grant Likely1ab1d632010-06-24 15:14:37 -0600222 return of_register_platform_driver(&flash_driver);
David S. Millera9540d32008-08-27 01:13:12 -0700223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static void __exit flash_cleanup(void)
226{
Grant Likely1ab1d632010-06-24 15:14:37 -0600227 of_unregister_platform_driver(&flash_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230module_init(flash_init);
231module_exit(flash_cleanup);
232MODULE_LICENSE("GPL");