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