1 // SPDX-License-Identifier: GPL-2.0-only
2 /* flash.c: Allow mmap access to the OBP Flash, for OBP updates.
4 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
7 #include <linux/module.h>
8 #include <linux/types.h>
9 #include <linux/errno.h>
10 #include <linux/miscdevice.h>
11 #include <linux/fcntl.h>
12 #include <linux/poll.h>
13 #include <linux/mutex.h>
14 #include <linux/spinlock.h>
17 #include <linux/of_device.h>
19 #include <linux/uaccess.h>
20 #include <asm/pgtable.h>
24 static DEFINE_MUTEX(flash_mutex
);
25 static DEFINE_SPINLOCK(flash_lock
);
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? */
34 #define FLASH_MINOR 152
37 flash_mmap(struct file
*file
, struct vm_area_struct
*vma
)
42 spin_lock(&flash_lock
);
43 if (flash
.read_base
== flash
.write_base
) {
44 addr
= flash
.read_base
;
45 size
= flash
.read_size
;
47 if ((vma
->vm_flags
& VM_READ
) &&
48 (vma
->vm_flags
& VM_WRITE
)) {
49 spin_unlock(&flash_lock
);
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
;
59 spin_unlock(&flash_lock
);
63 spin_unlock(&flash_lock
);
65 if ((vma
->vm_pgoff
<< PAGE_SHIFT
) > size
)
67 addr
= vma
->vm_pgoff
+ (addr
>> PAGE_SHIFT
);
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
));
72 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
74 if (io_remap_pfn_range(vma
, vma
->vm_start
, addr
, size
, vma
->vm_page_prot
))
81 flash_llseek(struct file
*file
, long long offset
, int origin
)
83 mutex_lock(&flash_mutex
);
89 file
->f_pos
+= offset
;
90 if (file
->f_pos
> flash
.read_size
)
91 file
->f_pos
= flash
.read_size
;
94 file
->f_pos
= flash
.read_size
;
97 mutex_unlock(&flash_mutex
);
100 mutex_unlock(&flash_mutex
);
105 flash_read(struct file
* file
, char __user
* buf
,
106 size_t count
, loff_t
*ppos
)
111 if (count
> flash
.read_size
- p
)
112 count
= flash
.read_size
- p
;
114 for (i
= 0; i
< count
; i
++) {
115 u8 data
= upa_readb(flash
.read_base
+ p
+ i
);
116 if (put_user(data
, buf
))
126 flash_open(struct inode
*inode
, struct file
*file
)
128 mutex_lock(&flash_mutex
);
129 if (test_and_set_bit(0, (void *)&flash
.busy
) != 0) {
130 mutex_unlock(&flash_mutex
);
134 mutex_unlock(&flash_mutex
);
139 flash_release(struct inode
*inode
, struct file
*file
)
141 spin_lock(&flash_lock
);
143 spin_unlock(&flash_lock
);
148 static const struct file_operations flash_fops
= {
149 /* no write to the Flash, use mmap
150 * and play flash dependent tricks.
152 .owner
= THIS_MODULE
,
153 .llseek
= flash_llseek
,
157 .release
= flash_release
,
160 static struct miscdevice flash_dev
= { FLASH_MINOR
, "flash", &flash_fops
};
162 static int flash_probe(struct platform_device
*op
)
164 struct device_node
*dp
= op
->dev
.of_node
;
165 struct device_node
*parent
;
169 if (!of_node_name_eq(parent
, "sbus") &&
170 !of_node_name_eq(parent
, "sbi") &&
171 !of_node_name_eq(parent
, "ebus"))
174 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]);
180 flash
.write_base
= op
->resource
[0].start
;
181 flash
.write_size
= resource_size(&op
->resource
[0]);
185 printk(KERN_INFO
"%pOF: OBP Flash, RD %lx[%lx] WR %lx[%lx]\n",
187 flash
.read_base
, flash
.read_size
,
188 flash
.write_base
, flash
.write_size
);
190 return misc_register(&flash_dev
);
193 static int flash_remove(struct platform_device
*op
)
195 misc_deregister(&flash_dev
);
200 static const struct of_device_id flash_match
[] = {
206 MODULE_DEVICE_TABLE(of
, flash_match
);
208 static struct platform_driver flash_driver
= {
211 .of_match_table
= flash_match
,
213 .probe
= flash_probe
,
214 .remove
= flash_remove
,
217 module_platform_driver(flash_driver
);
219 MODULE_LICENSE("GPL");