blob: 25c738e9ef19cea55a7a48ccbfb4b874789b056b [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>
Arnd Bergmanna3108ca2010-07-20 23:36:39 -070012#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/spinlock.h>
Horst H. von Brand8a737092007-05-31 01:27:52 -070014#include <linux/mm.h>
David S. Millera9540d32008-08-27 01:13:12 -070015#include <linux/of.h>
16#include <linux/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/uaccess.h>
19#include <asm/pgtable.h>
20#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/upa.h>
22
Arnd Bergmanna3108ca2010-07-20 23:36:39 -070023static DEFINE_MUTEX(flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static DEFINE_SPINLOCK(flash_lock);
25static struct {
26 unsigned long read_base; /* Physical read address */
27 unsigned long write_base; /* Physical write address */
28 unsigned long read_size; /* Size of read area */
29 unsigned long write_size; /* Size of write area */
30 unsigned long busy; /* In use? */
31} flash;
32
33#define FLASH_MINOR 152
34
35static int
36flash_mmap(struct file *file, struct vm_area_struct *vma)
37{
38 unsigned long addr;
39 unsigned long size;
40
41 spin_lock(&flash_lock);
42 if (flash.read_base == flash.write_base) {
43 addr = flash.read_base;
44 size = flash.read_size;
45 } else {
46 if ((vma->vm_flags & VM_READ) &&
47 (vma->vm_flags & VM_WRITE)) {
48 spin_unlock(&flash_lock);
49 return -EINVAL;
50 }
51 if (vma->vm_flags & VM_READ) {
52 addr = flash.read_base;
53 size = flash.read_size;
54 } else if (vma->vm_flags & VM_WRITE) {
55 addr = flash.write_base;
56 size = flash.write_size;
57 } else {
58 spin_unlock(&flash_lock);
59 return -ENXIO;
60 }
61 }
62 spin_unlock(&flash_lock);
63
64 if ((vma->vm_pgoff << PAGE_SHIFT) > size)
65 return -ENXIO;
66 addr = vma->vm_pgoff + (addr >> PAGE_SHIFT);
67
68 if (vma->vm_end - (vma->vm_start + (vma->vm_pgoff << PAGE_SHIFT)) > size)
69 size = vma->vm_end - (vma->vm_start + (vma->vm_pgoff << PAGE_SHIFT));
70
David S. Miller14778d92006-03-21 02:29:39 -080071 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 if (io_remap_pfn_range(vma, vma->vm_start, addr, size, vma->vm_page_prot))
74 return -EAGAIN;
75
76 return 0;
77}
78
79static long long
80flash_llseek(struct file *file, long long offset, int origin)
81{
Arnd Bergmanna3108ca2010-07-20 23:36:39 -070082 mutex_lock(&flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 switch (origin) {
84 case 0:
85 file->f_pos = offset;
86 break;
87 case 1:
88 file->f_pos += offset;
89 if (file->f_pos > flash.read_size)
90 file->f_pos = flash.read_size;
91 break;
92 case 2:
93 file->f_pos = flash.read_size;
94 break;
95 default:
Arnd Bergmanna3108ca2010-07-20 23:36:39 -070096 mutex_unlock(&flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return -EINVAL;
98 }
Arnd Bergmanna3108ca2010-07-20 23:36:39 -070099 mutex_unlock(&flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return file->f_pos;
101}
102
103static ssize_t
104flash_read(struct file * file, char __user * buf,
105 size_t count, loff_t *ppos)
106{
Jan Blunckbe94bbb2010-04-27 18:48:52 -0700107 loff_t p = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 int i;
Jan Blunckbe94bbb2010-04-27 18:48:52 -0700109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (count > flash.read_size - p)
111 count = flash.read_size - p;
112
113 for (i = 0; i < count; i++) {
114 u8 data = upa_readb(flash.read_base + p + i);
115 if (put_user(data, buf))
116 return -EFAULT;
117 buf++;
118 }
119
Jan Blunckbe94bbb2010-04-27 18:48:52 -0700120 *ppos += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return count;
122}
123
124static int
125flash_open(struct inode *inode, struct file *file)
126{
Arnd Bergmanna3108ca2010-07-20 23:36:39 -0700127 mutex_lock(&flash_mutex);
Arnd Bergmann78abb6a2008-05-20 19:15:54 +0200128 if (test_and_set_bit(0, (void *)&flash.busy) != 0) {
Arnd Bergmanna3108ca2010-07-20 23:36:39 -0700129 mutex_unlock(&flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return -EBUSY;
Arnd Bergmann78abb6a2008-05-20 19:15:54 +0200131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Arnd Bergmanna3108ca2010-07-20 23:36:39 -0700133 mutex_unlock(&flash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 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
Arjan van de Ven00977a52007-02-12 00:55:34 -0800147static const struct file_operations flash_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 /* 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
Greg Kroah-Hartman082a2002012-12-21 13:24:23 -0800161static int flash_probe(struct platform_device *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Grant Likely61c7a082010-04-13 16:12:29 -0700163 struct device_node *dp = op->dev.of_node;
David S. Millera9540d32008-08-27 01:13:12 -0700164 struct device_node *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
David S. Millera9540d32008-08-27 01:13:12 -0700166 parent = dp->parent;
David S. Miller690c8fd2006-06-22 19:12:03 -0700167
David S. Millera9540d32008-08-27 01:13:12 -0700168 if (strcmp(parent->name, "sbus") &&
169 strcmp(parent->name, "sbi") &&
170 strcmp(parent->name, "ebus"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
David S. Millera9540d32008-08-27 01:13:12 -0700173 flash.read_base = op->resource[0].start;
174 flash.read_size = resource_size(&op->resource[0]);
175 if (op->resource[1].flags) {
176 flash.write_base = op->resource[1].start;
177 flash.write_size = resource_size(&op->resource[1]);
178 } else {
179 flash.write_base = op->resource[0].start;
180 flash.write_size = resource_size(&op->resource[0]);
181 }
182 flash.busy = 0;
183
184 printk(KERN_INFO "%s: OBP Flash, RD %lx[%lx] WR %lx[%lx]\n",
Grant Likely61c7a082010-04-13 16:12:29 -0700185 op->dev.of_node->full_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 flash.read_base, flash.read_size,
187 flash.write_base, flash.write_size);
188
David S. Millera9540d32008-08-27 01:13:12 -0700189 return misc_register(&flash_dev);
190}
191
Greg Kroah-Hartman082a2002012-12-21 13:24:23 -0800192static int flash_remove(struct platform_device *op)
David S. Millera9540d32008-08-27 01:13:12 -0700193{
194 misc_deregister(&flash_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 return 0;
197}
198
David S. Millerfd098312008-08-31 01:23:17 -0700199static const struct of_device_id flash_match[] = {
David S. Millera9540d32008-08-27 01:13:12 -0700200 {
201 .name = "flashprom",
202 },
203 {},
204};
205MODULE_DEVICE_TABLE(of, flash_match);
206
Grant Likely4ebb24f2011-02-22 20:01:33 -0700207static struct platform_driver flash_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700208 .driver = {
209 .name = "flash",
210 .owner = THIS_MODULE,
211 .of_match_table = flash_match,
212 },
David S. Millera9540d32008-08-27 01:13:12 -0700213 .probe = flash_probe,
Greg Kroah-Hartman082a2002012-12-21 13:24:23 -0800214 .remove = flash_remove,
David S. Millera9540d32008-08-27 01:13:12 -0700215};
216
Axel Lindbf2b922011-11-26 19:04:25 +0000217module_platform_driver(flash_driver);
David S. Millera9540d32008-08-27 01:13:12 -0700218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219MODULE_LICENSE("GPL");