Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * memory mapped NVRAM
  4 *
  5 * (C) Copyright IBM Corp. 2005
  6 *
  7 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  8 */
  9
 10#include <linux/fs.h>
 11#include <linux/init.h>
 12#include <linux/kernel.h>
 13#include <linux/of_address.h>
 14#include <linux/spinlock.h>
 15#include <linux/types.h>
 16
 17#include <asm/machdep.h>
 18#include <asm/nvram.h>
 
 19
 20static void __iomem *mmio_nvram_start;
 21static long mmio_nvram_len;
 22static DEFINE_SPINLOCK(mmio_nvram_lock);
 23
 24static ssize_t mmio_nvram_read(char *buf, size_t count, loff_t *index)
 25{
 26	unsigned long flags;
 27
 28	if (*index >= mmio_nvram_len)
 29		return 0;
 30	if (*index + count > mmio_nvram_len)
 31		count = mmio_nvram_len - *index;
 32
 33	spin_lock_irqsave(&mmio_nvram_lock, flags);
 34
 35	memcpy_fromio(buf, mmio_nvram_start + *index, count);
 36
 37	spin_unlock_irqrestore(&mmio_nvram_lock, flags);
 38	
 39	*index += count;
 40	return count;
 41}
 42
 43static unsigned char mmio_nvram_read_val(int addr)
 44{
 45	unsigned long flags;
 46	unsigned char val;
 47
 48	if (addr >= mmio_nvram_len)
 49		return 0xff;
 50
 51	spin_lock_irqsave(&mmio_nvram_lock, flags);
 52
 53	val = ioread8(mmio_nvram_start + addr);
 54
 55	spin_unlock_irqrestore(&mmio_nvram_lock, flags);
 56
 57	return val;
 58}
 59
 60static ssize_t mmio_nvram_write(char *buf, size_t count, loff_t *index)
 61{
 62	unsigned long flags;
 63
 64	if (*index >= mmio_nvram_len)
 65		return 0;
 66	if (*index + count > mmio_nvram_len)
 67		count = mmio_nvram_len - *index;
 68
 69	spin_lock_irqsave(&mmio_nvram_lock, flags);
 70
 71	memcpy_toio(mmio_nvram_start + *index, buf, count);
 72
 73	spin_unlock_irqrestore(&mmio_nvram_lock, flags);
 74	
 75	*index += count;
 76	return count;
 77}
 78
 79static void mmio_nvram_write_val(int addr, unsigned char val)
 80{
 81	unsigned long flags;
 82
 83	if (addr < mmio_nvram_len) {
 84		spin_lock_irqsave(&mmio_nvram_lock, flags);
 85
 86		iowrite8(val, mmio_nvram_start + addr);
 87
 88		spin_unlock_irqrestore(&mmio_nvram_lock, flags);
 89	}
 90}
 91
 92static ssize_t mmio_nvram_get_size(void)
 93{
 94	return mmio_nvram_len;
 95}
 96
 97int __init mmio_nvram_init(void)
 98{
 99	struct device_node *nvram_node;
100	unsigned long nvram_addr;
101	struct resource r;
102	int ret;
103
104	nvram_node = of_find_node_by_type(NULL, "nvram");
105	if (!nvram_node)
106		nvram_node = of_find_compatible_node(NULL, NULL, "nvram");
107	if (!nvram_node) {
108		printk(KERN_WARNING "nvram: no node found in device-tree\n");
109		return -ENODEV;
110	}
111
112	ret = of_address_to_resource(nvram_node, 0, &r);
113	if (ret) {
114		printk(KERN_WARNING "nvram: failed to get address (err %d)\n",
115		       ret);
116		goto out;
117	}
118	nvram_addr = r.start;
119	mmio_nvram_len = resource_size(&r);
120	if ( (!mmio_nvram_len) || (!nvram_addr) ) {
121		printk(KERN_WARNING "nvram: address or length is 0\n");
122		ret = -EIO;
123		goto out;
124	}
125
126	mmio_nvram_start = ioremap(nvram_addr, mmio_nvram_len);
127	if (!mmio_nvram_start) {
128		printk(KERN_WARNING "nvram: failed to ioremap\n");
129		ret = -ENOMEM;
130		goto out;
131	}
132
133	printk(KERN_INFO "mmio NVRAM, %luk at 0x%lx mapped to %p\n",
134	       mmio_nvram_len >> 10, nvram_addr, mmio_nvram_start);
135
136	ppc_md.nvram_read_val	= mmio_nvram_read_val;
137	ppc_md.nvram_write_val	= mmio_nvram_write_val;
138	ppc_md.nvram_read	= mmio_nvram_read;
139	ppc_md.nvram_write	= mmio_nvram_write;
140	ppc_md.nvram_size	= mmio_nvram_get_size;
141
142out:
143	of_node_put(nvram_node);
144	return ret;
145}
v3.5.6
 
  1/*
  2 * memory mapped NVRAM
  3 *
  4 * (C) Copyright IBM Corp. 2005
  5 *
  6 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
  7 *
  8 * This program is free software; you can redistribute it and/or modify
  9 * it under the terms of the GNU General Public License as published by
 10 * the Free Software Foundation; either version 2, or (at your option)
 11 * any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 21 */
 22
 23#include <linux/fs.h>
 24#include <linux/init.h>
 25#include <linux/kernel.h>
 
 26#include <linux/spinlock.h>
 27#include <linux/types.h>
 28
 29#include <asm/machdep.h>
 30#include <asm/nvram.h>
 31#include <asm/prom.h>
 32
 33static void __iomem *mmio_nvram_start;
 34static long mmio_nvram_len;
 35static DEFINE_SPINLOCK(mmio_nvram_lock);
 36
 37static ssize_t mmio_nvram_read(char *buf, size_t count, loff_t *index)
 38{
 39	unsigned long flags;
 40
 41	if (*index >= mmio_nvram_len)
 42		return 0;
 43	if (*index + count > mmio_nvram_len)
 44		count = mmio_nvram_len - *index;
 45
 46	spin_lock_irqsave(&mmio_nvram_lock, flags);
 47
 48	memcpy_fromio(buf, mmio_nvram_start + *index, count);
 49
 50	spin_unlock_irqrestore(&mmio_nvram_lock, flags);
 51	
 52	*index += count;
 53	return count;
 54}
 55
 56static unsigned char mmio_nvram_read_val(int addr)
 57{
 58	unsigned long flags;
 59	unsigned char val;
 60
 61	if (addr >= mmio_nvram_len)
 62		return 0xff;
 63
 64	spin_lock_irqsave(&mmio_nvram_lock, flags);
 65
 66	val = ioread8(mmio_nvram_start + addr);
 67
 68	spin_unlock_irqrestore(&mmio_nvram_lock, flags);
 69
 70	return val;
 71}
 72
 73static ssize_t mmio_nvram_write(char *buf, size_t count, loff_t *index)
 74{
 75	unsigned long flags;
 76
 77	if (*index >= mmio_nvram_len)
 78		return 0;
 79	if (*index + count > mmio_nvram_len)
 80		count = mmio_nvram_len - *index;
 81
 82	spin_lock_irqsave(&mmio_nvram_lock, flags);
 83
 84	memcpy_toio(mmio_nvram_start + *index, buf, count);
 85
 86	spin_unlock_irqrestore(&mmio_nvram_lock, flags);
 87	
 88	*index += count;
 89	return count;
 90}
 91
 92void mmio_nvram_write_val(int addr, unsigned char val)
 93{
 94	unsigned long flags;
 95
 96	if (addr < mmio_nvram_len) {
 97		spin_lock_irqsave(&mmio_nvram_lock, flags);
 98
 99		iowrite8(val, mmio_nvram_start + addr);
100
101		spin_unlock_irqrestore(&mmio_nvram_lock, flags);
102	}
103}
104
105static ssize_t mmio_nvram_get_size(void)
106{
107	return mmio_nvram_len;
108}
109
110int __init mmio_nvram_init(void)
111{
112	struct device_node *nvram_node;
113	unsigned long nvram_addr;
114	struct resource r;
115	int ret;
116
117	nvram_node = of_find_node_by_type(NULL, "nvram");
118	if (!nvram_node)
119		nvram_node = of_find_compatible_node(NULL, NULL, "nvram");
120	if (!nvram_node) {
121		printk(KERN_WARNING "nvram: no node found in device-tree\n");
122		return -ENODEV;
123	}
124
125	ret = of_address_to_resource(nvram_node, 0, &r);
126	if (ret) {
127		printk(KERN_WARNING "nvram: failed to get address (err %d)\n",
128		       ret);
129		goto out;
130	}
131	nvram_addr = r.start;
132	mmio_nvram_len = resource_size(&r);
133	if ( (!mmio_nvram_len) || (!nvram_addr) ) {
134		printk(KERN_WARNING "nvram: address or length is 0\n");
135		ret = -EIO;
136		goto out;
137	}
138
139	mmio_nvram_start = ioremap(nvram_addr, mmio_nvram_len);
140	if (!mmio_nvram_start) {
141		printk(KERN_WARNING "nvram: failed to ioremap\n");
142		ret = -ENOMEM;
143		goto out;
144	}
145
146	printk(KERN_INFO "mmio NVRAM, %luk at 0x%lx mapped to %p\n",
147	       mmio_nvram_len >> 10, nvram_addr, mmio_nvram_start);
148
149	ppc_md.nvram_read_val	= mmio_nvram_read_val;
150	ppc_md.nvram_write_val	= mmio_nvram_write_val;
151	ppc_md.nvram_read	= mmio_nvram_read;
152	ppc_md.nvram_write	= mmio_nvram_write;
153	ppc_md.nvram_size	= mmio_nvram_get_size;
154
155out:
156	of_node_put(nvram_node);
157	return ret;
158}