Linux Audio

Check our new training course

Linux BSP development engineering services

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