1/*
2 * NV-RAM memory access on autcpu12
3 * (C) 2002 Thomas Gleixner (gleixner@autronix.de)
4 *
5 * $Id: autcpu12-nvram.c,v 1.1.1.1 2008/10/15 03:26:35 james26_jang Exp $
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 *
21 */
22
23#include <linux/module.h>
24#include <linux/types.h>
25#include <linux/kernel.h>
26#include <linux/ioport.h>
27#include <asm/io.h>
28#include <asm/sizes.h>
29#include <asm/hardware.h>
30#include <asm/arch/autcpu12.h>
31#include <linux/mtd/mtd.h>
32#include <linux/mtd/map.h>
33#include <linux/mtd/partitions.h>
34
35__u8 autcpu12_read8(struct map_info *map, unsigned long ofs)
36{
37	return __raw_readb(map->map_priv_1 + ofs);
38}
39
40__u16 autcpu12_read16(struct map_info *map, unsigned long ofs)
41{
42	return __raw_readw(map->map_priv_1 + ofs);
43}
44
45__u32 autcpu12_read32(struct map_info *map, unsigned long ofs)
46{
47	return __raw_readl(map->map_priv_1 + ofs);
48}
49
50void autcpu12_write8(struct map_info *map, __u8 d, unsigned long adr)
51{
52	__raw_writeb(d, map->map_priv_1 + adr);
53	mb();
54}
55
56void autcpu12_write16(struct map_info *map, __u16 d, unsigned long adr)
57{
58	__raw_writew(d, map->map_priv_1 + adr);
59	mb();
60}
61
62void autcpu12_write32(struct map_info *map, __u32 d, unsigned long adr)
63{
64	__raw_writel(d, map->map_priv_1 + adr);
65	mb();
66}
67
68void autcpu12_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
69{
70	memcpy_fromio(to, map->map_priv_1 + from, len);
71}
72
73void autcpu12_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
74{
75	while(len) {
76		__raw_writeb(*(unsigned char *) from, map->map_priv_1 + to);
77		from++;
78		to++;
79		len--;
80	}
81}
82
83static struct mtd_info *sram_mtd;
84
85struct map_info autcpu12_sram_map = {
86	name: "SRAM",
87	size: 32768,
88	buswidth: 8,
89	read8: autcpu12_read8,
90	read16: autcpu12_read16,
91	read32: autcpu12_read32,
92	copy_from: autcpu12_copy_from,
93	write8: autcpu12_write8,
94	write16: autcpu12_write16,
95	write32: autcpu12_write32,
96	copy_to: autcpu12_copy_to
97};
98
99static int __init init_autcpu12_sram (void)
100{
101	int err, save0, save1;
102
103	autcpu12_sram_map.map_priv_1 = (unsigned long)ioremap(0x12000000, SZ_128K);
104	if (!autcpu12_sram_map.map_priv_1) {
105		printk("Failed to ioremap autcpu12 NV-RAM space\n");
106		err = -EIO;
107		goto out;
108	}
109
110	/*
111	 * Check for 32K/128K
112	 * read ofs 0
113	 * read ofs 0x10000
114	 * Write complement to ofs 0x100000
115	 * Read	and check result on ofs 0x0
116	 * Restore contents
117	 */
118	save0 = autcpu12_read32(&autcpu12_sram_map,0);
119	save1 = autcpu12_read32(&autcpu12_sram_map,0x10000);
120	autcpu12_write32(&autcpu12_sram_map,~save0,0x10000);
121	/* if we find this pattern on 0x0, we have 32K size
122	 * restore contents and exit
123	 */
124	if ( autcpu12_read32(&autcpu12_sram_map,0) != save0) {
125		autcpu12_write32(&autcpu12_sram_map,save0,0x0);
126		goto map;
127	}
128	/* We have a 128K found, restore 0x10000 and set size
129	 * to 128K
130	 */
131	autcpu12_write32(&autcpu12_sram_map,save1,0x10000);
132	autcpu12_sram_map.size = SZ_128K;
133
134map:
135	sram_mtd = do_map_probe("map_ram", &autcpu12_sram_map);
136	if (!sram_mtd) {
137		printk("NV-RAM probe failed\n");
138		err = -ENXIO;
139		goto out_ioremap;
140	}
141
142	sram_mtd->module = THIS_MODULE;
143	sram_mtd->erasesize = 16;
144
145	if (add_mtd_device(sram_mtd)) {
146		printk("NV-RAM device addition failed\n");
147		err = -ENOMEM;
148		goto out_probe;
149	}
150
151	printk("NV-RAM device size %ldK registered on AUTCPU12\n",autcpu12_sram_map.size/SZ_1K);
152
153	return 0;
154
155out_probe:
156	map_destroy(sram_mtd);
157	sram_mtd = 0;
158
159out_ioremap:
160	iounmap((void *)autcpu12_sram_map.map_priv_1);
161out:
162	return err;
163}
164
165static void __exit cleanup_autcpu12_maps(void)
166{
167	if (sram_mtd) {
168		del_mtd_device(sram_mtd);
169		map_destroy(sram_mtd);
170		iounmap((void *)autcpu12_sram_map.map_priv_1);
171	}
172}
173
174module_init(init_autcpu12_sram);
175module_exit(cleanup_autcpu12_maps);
176
177MODULE_AUTHOR("Thomas Gleixner");
178MODULE_DESCRIPTION("autcpu12 NV-RAM map driver");
179MODULE_LICENSE("GPL");
180