blob: d9f268f237749df06e63f11cec4c19c69326f995 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/uaccess.h>
20#include <asm/pgtable.h>
21#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/upa.h>
23
Arnd Bergmanna3108ca2010-07-20 23:36:39 -070024static DEFINE_MUTEX(flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025static DEFINE_SPINLOCK(flash_lock);
26static struct {
27 unsigned long read_base; /* Physical read address */
28 unsigned long write_base; /* Physical write address */
29 unsigned long read_size; /* Size of read area */
30 unsigned long write_size; /* Size of write area */
31 unsigned long busy; /* In use? */
32} flash;
33
34#define FLASH_MINOR 152
35
36static int
37flash_mmap(struct file *file, struct vm_area_struct *vma)
38{
39 unsigned long addr;
40 unsigned long size;
41
42 spin_lock(&flash_lock);
43 if (flash.read_base == flash.write_base) {
44 addr = flash.read_base;
45 size = flash.read_size;
46 } else {
47 if ((vma->vm_flags & VM_READ) &&
48 (vma->vm_flags & VM_WRITE)) {
49 spin_unlock(&flash_lock);
50 return -EINVAL;
51 }
52 if (vma->vm_flags & VM_READ) {
53 addr = flash.read_base;
54 size = flash.read_size;
55 } else if (vma->vm_flags & VM_WRITE) {
56 addr = flash.write_base;
57 size = flash.write_size;
58 } else {
59 spin_unlock(&flash_lock);
60 return -ENXIO;
61 }
62 }
63 spin_unlock(&flash_lock);
64
65 if ((vma->vm_pgoff << PAGE_SHIFT) > size)
66 return -ENXIO;
67 addr = vma->vm_pgoff + (addr >> PAGE_SHIFT);
68
69 if (vma->vm_end - (vma->vm_start + (vma->vm_pgoff << PAGE_SHIFT)) > size)
70 size = vma->vm_end - (vma->vm_start + (vma->vm_pgoff << PAGE_SHIFT));
71
David S. Miller14778d92006-03-21 02:29:39 -080072 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 if (io_remap_pfn_range(vma, vma->vm_start, addr, size, vma->vm_page_prot))
75 return -EAGAIN;
76
77 return 0;
78}
79
80static long long
81flash_llseek(struct file *file, long long offset, int origin)
82{
Arnd Bergmanna3108ca2010-07-20 23:36:39 -070083 mutex_lock(&flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 switch (origin) {
85 case 0:
86 file->f_pos = offset;
87 break;
88 case 1:
89 file->f_pos += offset;
90 if (file->f_pos > flash.read_size)
91 file->f_pos = flash.read_size;
92 break;
93 case 2:
94 file->f_pos = flash.read_size;
95 break;
96 default:
Arnd Bergmanna3108ca2010-07-20 23:36:39 -070097 mutex_unlock(&flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return -EINVAL;
99 }
Arnd Bergmanna3108ca2010-07-20 23:36:39 -0700100 mutex_unlock(&flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return file->f_pos;
102}
103
104static ssize_t
105flash_read(struct file * file, char __user * buf,
106 size_t count, loff_t *ppos)
107{
Jan Blunckbe94bbb2010-04-27 18:48:52 -0700108 loff_t p = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 int i;
Jan Blunckbe94bbb2010-04-27 18:48:52 -0700110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (count > flash.read_size - p)
112 count = flash.read_size - p;
113
114 for (i = 0; i < count; i++) {
115 u8 data = upa_readb(flash.read_base + p + i);
116 if (put_user(data, buf))
117 return -EFAULT;
118 buf++;
119 }
120
Jan Blunckbe94bbb2010-04-27 18:48:52 -0700121 *ppos += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return count;
123}
124
125static int
126flash_open(struct inode *inode, struct file *file)
127{
Arnd Bergmanna3108ca2010-07-20 23:36:39 -0700128 mutex_lock(&flash_mutex);
Arnd Bergmann78abb6a2008-05-20 19:15:54 +0200129 if (test_and_set_bit(0, (void *)&flash.busy) != 0) {
Arnd Bergmanna3108ca2010-07-20 23:36:39 -0700130 mutex_unlock(&flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return -EBUSY;
Arnd Bergmann78abb6a2008-05-20 19:15:54 +0200132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Arnd Bergmanna3108ca2010-07-20 23:36:39 -0700134 mutex_unlock(&flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return 0;
136}
137
138static int
139flash_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
Arjan van de Ven00977a52007-02-12 00:55:34 -0800148static const struct file_operations flash_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 /* 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
160static struct miscdevice flash_dev = { FLASH_MINOR, "flash", &flash_fops };
161
Greg Kroah-Hartman082a2002012-12-21 13:24:23 -0800162static int flash_probe(struct platform_device *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Grant Likely61c7a082010-04-13 16:12:29 -0700164 struct device_node *dp = op->dev.of_node;
David S. Millera9540d32008-08-27 01:13:12 -0700165 struct device_node *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
David S. Millera9540d32008-08-27 01:13:12 -0700167 parent = dp->parent;
David S. Miller690c8fd2006-06-22 19:12:03 -0700168
David S. Millera9540d32008-08-27 01:13:12 -0700169 if (strcmp(parent->name, "sbus") &&
170 strcmp(parent->name, "sbi") &&
171 strcmp(parent->name, "ebus"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
David S. Millera9540d32008-08-27 01:13:12 -0700174 flash.read_base = op->resource[0].start;
175 flash.read_size = resource_size(&op->resource[0]);
176 if (op->resource[1].flags) {
177 flash.write_base = op->resource[1].start;
178 flash.write_size = resource_size(&op->resource[1]);
179 } else {
180 flash.write_base = op->resource[0].start;
181 flash.write_size = resource_size(&op->resource[0]);
182 }
183 flash.busy = 0;
184
185 printk(KERN_INFO "%s: OBP Flash, RD %lx[%lx] WR %lx[%lx]\n",
Grant Likely61c7a082010-04-13 16:12:29 -0700186 op->dev.of_node->full_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 flash.read_base, flash.read_size,
188 flash.write_base, flash.write_size);
189
David S. Millera9540d32008-08-27 01:13:12 -0700190 return misc_register(&flash_dev);
191}
192
Greg Kroah-Hartman082a2002012-12-21 13:24:23 -0800193static int flash_remove(struct platform_device *op)
David S. Millera9540d32008-08-27 01:13:12 -0700194{
195 misc_deregister(&flash_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 return 0;
198}
199
David S. Millerfd098312008-08-31 01:23:17 -0700200static const struct of_device_id flash_match[] = {
David S. Millera9540d32008-08-27 01:13:12 -0700201 {
202 .name = "flashprom",
203 },
204 {},
205};
206MODULE_DEVICE_TABLE(of, flash_match);
207
Grant Likely4ebb24f2011-02-22 20:01:33 -0700208static struct platform_driver flash_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700209 .driver = {
210 .name = "flash",
211 .owner = THIS_MODULE,
212 .of_match_table = flash_match,
213 },
David S. Millera9540d32008-08-27 01:13:12 -0700214 .probe = flash_probe,
Greg Kroah-Hartman082a2002012-12-21 13:24:23 -0800215 .remove = flash_remove,
David S. Millera9540d32008-08-27 01:13:12 -0700216};
217
Axel Lindbf2b922011-11-26 19:04:25 +0000218module_platform_driver(flash_driver);
David S. Millera9540d32008-08-27 01:13:12 -0700219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220MODULE_LICENSE("GPL");