1/*
2 * $Id: ocelot.c,v 1.1.1.1 2007/08/03 18:52:44 Exp $
3 *
4 * Flash on Momenco Ocelot
5 */
6
7#include <linux/module.h>
8#include <linux/types.h>
9#include <linux/kernel.h>
10#include <linux/init.h>
11#include <asm/io.h>
12#include <linux/mtd/mtd.h>
13#include <linux/mtd/map.h>
14#include <linux/mtd/partitions.h>
15
16#define OCELOT_PLD 0x2c000000
17#define FLASH_WINDOW_ADDR 0x2fc00000
18#define FLASH_WINDOW_SIZE 0x00080000
19#define FLASH_BUSWIDTH 1
20#define NVRAM_WINDOW_ADDR 0x2c800000
21#define NVRAM_WINDOW_SIZE 0x00007FF0
22#define NVRAM_BUSWIDTH 1
23
24static unsigned int cacheflush = 0;
25
26static struct mtd_info *flash_mtd;
27static struct mtd_info *nvram_mtd;
28
29static void ocelot_ram_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
30{
31        struct map_info *map = mtd->priv;
32	size_t done = 0;
33
34	/* If we use memcpy, it does word-wide writes. Even though we told the
35	   GT64120A that it's an 8-bit wide region, word-wide writes don't work.
36	   We end up just writing the first byte of the four to all four bytes.
37	   So we have this loop instead */
38	*retlen = len;
39	while(len) {
40		__raw_writeb(*(unsigned char *) from, map->virt + to);
41		from++;
42		to++;
43		len--;
44	}
45}
46
47static struct mtd_partition *parsed_parts;
48
49struct map_info ocelot_flash_map = {
50	.name = "Ocelot boot flash",
51	.size = FLASH_WINDOW_SIZE,
52	.bankwidth = FLASH_BUSWIDTH,
53	.phys = FLASH_WINDOW_ADDR,
54};
55
56struct map_info ocelot_nvram_map = {
57	.name = "Ocelot NVRAM",
58	.size = NVRAM_WINDOW_SIZE,
59	.bankwidth = NVRAM_BUSWIDTH,
60	.phys = NVRAM_WINDOW_ADDR,
61};
62
63static const char *probes[] = { "RedBoot", NULL };
64
65static int __init init_ocelot_maps(void)
66{
67	void *pld;
68	int nr_parts;
69	unsigned char brd_status;
70
71       	printk(KERN_INFO "Momenco Ocelot MTD mappings: Flash 0x%x at 0x%x, NVRAM 0x%x at 0x%x\n",
72	       FLASH_WINDOW_SIZE, FLASH_WINDOW_ADDR, NVRAM_WINDOW_SIZE, NVRAM_WINDOW_ADDR);
73
74	/* First check whether the flash jumper is present */
75	pld = ioremap(OCELOT_PLD, 0x10);
76	if (!pld) {
77		printk(KERN_NOTICE "Failed to ioremap Ocelot PLD\n");
78		return -EIO;
79	}
80	brd_status = readb(pld+4);
81	iounmap(pld);
82
83	/* Now ioremap the NVRAM space */
84	ocelot_nvram_map.virt = ioremap_nocache(NVRAM_WINDOW_ADDR, NVRAM_WINDOW_SIZE);
85	if (!ocelot_nvram_map.virt) {
86		printk(KERN_NOTICE "Failed to ioremap Ocelot NVRAM space\n");
87		return -EIO;
88	}
89
90	simple_map_init(&ocelot_nvram_map);
91
92	/* And do the RAM probe on it to get an MTD device */
93	nvram_mtd = do_map_probe("map_ram", &ocelot_nvram_map);
94	if (!nvram_mtd) {
95		printk("NVRAM probe failed\n");
96		goto fail_1;
97	}
98	nvram_mtd->owner = THIS_MODULE;
99	nvram_mtd->erasesize = 16;
100	/* Override the write() method */
101	nvram_mtd->write = ocelot_ram_write;
102
103	/* Now map the flash space */
104	ocelot_flash_map.virt = ioremap_nocache(FLASH_WINDOW_ADDR, FLASH_WINDOW_SIZE);
105	if (!ocelot_flash_map.virt) {
106		printk(KERN_NOTICE "Failed to ioremap Ocelot flash space\n");
107		goto fail_2;
108	}
109	/* Now the cached version */
110	ocelot_flash_map.cached = (unsigned long)__ioremap(FLASH_WINDOW_ADDR, FLASH_WINDOW_SIZE, 0);
111
112	simple_map_init(&ocelot_flash_map);
113
114	/* Only probe for flash if the write jumper is present */
115	if (brd_status & 0x40) {
116		flash_mtd = do_map_probe("jedec", &ocelot_flash_map);
117	} else {
118		printk(KERN_NOTICE "Ocelot flash write jumper not present. Treating as ROM\n");
119	}
120	/* If that failed or the jumper's absent, pretend it's ROM */
121	if (!flash_mtd) {
122		flash_mtd = do_map_probe("map_rom", &ocelot_flash_map);
123		/* If we're treating it as ROM, set the erase size */
124		if (flash_mtd)
125			flash_mtd->erasesize = 0x10000;
126	}
127	if (!flash_mtd)
128		goto fail3;
129
130	add_mtd_device(nvram_mtd);
131
132	flash_mtd->owner = THIS_MODULE;
133	nr_parts = parse_mtd_partitions(flash_mtd, probes, &parsed_parts, 0);
134
135	if (nr_parts > 0)
136		add_mtd_partitions(flash_mtd, parsed_parts, nr_parts);
137	else
138		add_mtd_device(flash_mtd);
139
140	return 0;
141
142 fail3:
143	iounmap((void *)ocelot_flash_map.virt);
144	if (ocelot_flash_map.cached)
145			iounmap((void *)ocelot_flash_map.cached);
146 fail_2:
147	map_destroy(nvram_mtd);
148 fail_1:
149	iounmap((void *)ocelot_nvram_map.virt);
150
151	return -ENXIO;
152}
153
154static void __exit cleanup_ocelot_maps(void)
155{
156	del_mtd_device(nvram_mtd);
157	map_destroy(nvram_mtd);
158	iounmap((void *)ocelot_nvram_map.virt);
159
160	if (parsed_parts)
161		del_mtd_partitions(flash_mtd);
162	else
163		del_mtd_device(flash_mtd);
164	map_destroy(flash_mtd);
165	iounmap((void *)ocelot_flash_map.virt);
166	if (ocelot_flash_map.cached)
167		iounmap((void *)ocelot_flash_map.cached);
168}
169
170module_init(init_ocelot_maps);
171module_exit(cleanup_ocelot_maps);
172
173MODULE_LICENSE("GPL");
174MODULE_AUTHOR("Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>");
175MODULE_DESCRIPTION("MTD map driver for Momenco Ocelot board");
176