1/*
2 * Interface for NOR flash driver whose high address lines are latched
3 *
4 * Copyright © 2000 Nicolas Pitre <nico@cam.org>
5 * Copyright © 2005-2008 Analog Devices Inc.
6 * Copyright © 2008 MontaVista Software, Inc. <source@mvista.com>
7 *
8 * This file is licensed under the terms of the GNU General Public License
9 * version 2. This program is licensed "as is" without any warranty of any
10 * kind, whether express or implied.
11 */
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/mtd/mtd.h>
16#include <linux/mtd/map.h>
17#include <linux/mtd/partitions.h>
18#include <linux/platform_device.h>
19#include <linux/mtd/latch-addr-flash.h>
20#include <linux/slab.h>
21
22#define DRIVER_NAME "latch-addr-flash"
23
24struct latch_addr_flash_info {
25	struct mtd_info		*mtd;
26	struct map_info		map;
27	struct resource		*res;
28
29	void			(*set_window)(unsigned long offset, void *data);
30	void			*data;
31
32	/* cache; could be found out of res */
33	unsigned long		win_mask;
34
35	spinlock_t		lock;
36};
37
38static map_word lf_read(struct map_info *map, unsigned long ofs)
39{
40	struct latch_addr_flash_info *info;
41	map_word datum;
42
43	info = (struct latch_addr_flash_info *)map->map_priv_1;
44
45	spin_lock(&info->lock);
46
47	info->set_window(ofs, info->data);
48	datum = inline_map_read(map, info->win_mask & ofs);
49
50	spin_unlock(&info->lock);
51
52	return datum;
53}
54
55static void lf_write(struct map_info *map, map_word datum, unsigned long ofs)
56{
57	struct latch_addr_flash_info *info;
58
59	info = (struct latch_addr_flash_info *)map->map_priv_1;
60
61	spin_lock(&info->lock);
62
63	info->set_window(ofs, info->data);
64	inline_map_write(map, datum, info->win_mask & ofs);
65
66	spin_unlock(&info->lock);
67}
68
69static void lf_copy_from(struct map_info *map, void *to,
70		unsigned long from, ssize_t len)
71{
72	struct latch_addr_flash_info *info =
73		(struct latch_addr_flash_info *) map->map_priv_1;
74	unsigned n;
75
76	while (len > 0) {
77		n = info->win_mask + 1 - (from & info->win_mask);
78		if (n > len)
79			n = len;
80
81		spin_lock(&info->lock);
82
83		info->set_window(from, info->data);
84		memcpy_fromio(to, map->virt + (from & info->win_mask), n);
85
86		spin_unlock(&info->lock);
87
88		to += n;
89		from += n;
90		len -= n;
91	}
92}
93
94static char *rom_probe_types[] = { "cfi_probe", NULL };
95
96static int latch_addr_flash_remove(struct platform_device *dev)
97{
98	struct latch_addr_flash_info *info;
99	struct latch_addr_flash_data *latch_addr_data;
100
101	info = platform_get_drvdata(dev);
102	if (info == NULL)
103		return 0;
104
105	latch_addr_data = dev_get_platdata(&dev->dev);
106
107	if (info->mtd != NULL) {
108		mtd_device_unregister(info->mtd);
109		map_destroy(info->mtd);
110	}
111
112	if (info->map.virt != NULL)
113		iounmap(info->map.virt);
114
115	if (info->res != NULL)
116		release_mem_region(info->res->start, resource_size(info->res));
117
118	kfree(info);
119
120	if (latch_addr_data->done)
121		latch_addr_data->done(latch_addr_data->data);
122
123	return 0;
124}
125
126static int latch_addr_flash_probe(struct platform_device *dev)
127{
128	struct latch_addr_flash_data *latch_addr_data;
129	struct latch_addr_flash_info *info;
130	resource_size_t win_base = dev->resource->start;
131	resource_size_t win_size = resource_size(dev->resource);
132	char **probe_type;
133	int chipsel;
134	int err;
135
136	latch_addr_data = dev_get_platdata(&dev->dev);
137	if (latch_addr_data == NULL)
138		return -ENODEV;
139
140	pr_notice("latch-addr platform flash device: %#llx byte "
141		  "window at %#.8llx\n",
142		  (unsigned long long)win_size, (unsigned long long)win_base);
143
144	chipsel = dev->id;
145
146	if (latch_addr_data->init) {
147		err = latch_addr_data->init(latch_addr_data->data, chipsel);
148		if (err != 0)
149			return err;
150	}
151
152	info = kzalloc(sizeof(struct latch_addr_flash_info), GFP_KERNEL);
153	if (info == NULL) {
154		err = -ENOMEM;
155		goto done;
156	}
157
158	platform_set_drvdata(dev, info);
159
160	info->res = request_mem_region(win_base, win_size, DRIVER_NAME);
161	if (info->res == NULL) {
162		dev_err(&dev->dev, "Could not reserve memory region\n");
163		err = -EBUSY;
164		goto free_info;
165	}
166
167	info->map.name		= DRIVER_NAME;
168	info->map.size		= latch_addr_data->size;
169	info->map.bankwidth	= latch_addr_data->width;
170
171	info->map.phys		= NO_XIP;
172	info->map.virt		= ioremap(win_base, win_size);
173	if (!info->map.virt) {
174		err = -ENOMEM;
175		goto free_res;
176	}
177
178	info->map.map_priv_1	= (unsigned long)info;
179
180	info->map.read		= lf_read;
181	info->map.copy_from	= lf_copy_from;
182	info->map.write		= lf_write;
183	info->set_window	= latch_addr_data->set_window;
184	info->data		= latch_addr_data->data;
185	info->win_mask		= win_size - 1;
186
187	spin_lock_init(&info->lock);
188
189	for (probe_type = rom_probe_types; !info->mtd && *probe_type;
190		probe_type++)
191		info->mtd = do_map_probe(*probe_type, &info->map);
192
193	if (info->mtd == NULL) {
194		dev_err(&dev->dev, "map_probe failed\n");
195		err = -ENODEV;
196		goto iounmap;
197	}
198	info->mtd->owner = THIS_MODULE;
199
200	mtd_device_parse_register(info->mtd, NULL, NULL,
201				  latch_addr_data->parts,
202				  latch_addr_data->nr_parts);
203	return 0;
204
205iounmap:
206	iounmap(info->map.virt);
207free_res:
208	release_mem_region(info->res->start, resource_size(info->res));
209free_info:
210	kfree(info);
211done:
212	if (latch_addr_data->done)
213		latch_addr_data->done(latch_addr_data->data);
214	return err;
215}
216
217static struct platform_driver latch_addr_flash_driver = {
218	.probe		= latch_addr_flash_probe,
219	.remove		= latch_addr_flash_remove,
220	.driver		= {
221		.name	= DRIVER_NAME,
222	},
223};
224
225module_platform_driver(latch_addr_flash_driver);
226
227MODULE_AUTHOR("David Griego <dgriego@mvista.com>");
228MODULE_DESCRIPTION("MTD map driver for flashes addressed physically with upper "
229		"address lines being set board specifically");
230MODULE_LICENSE("GPL v2");
231