blob: 09cf704ea02e89669fde096c226cdccd8f1bfd24 [file] [log] [blame]
David Griegodcfb81d2010-11-30 15:32:05 +05301/*
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/init.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/mtd/mtd.h>
17#include <linux/mtd/map.h>
18#include <linux/mtd/partitions.h>
19#include <linux/platform_device.h>
20#include <linux/mtd/latch-addr-flash.h>
21#include <linux/slab.h>
22
23#define DRIVER_NAME "latch-addr-flash"
24
25struct latch_addr_flash_info {
26 struct mtd_info *mtd;
27 struct map_info map;
28 struct resource *res;
29
30 void (*set_window)(unsigned long offset, void *data);
31 void *data;
32
33 /* cache; could be found out of res */
34 unsigned long win_mask;
35
36 int nr_parts;
37 struct mtd_partition *parts;
38
39 spinlock_t lock;
40};
41
42static map_word lf_read(struct map_info *map, unsigned long ofs)
43{
44 struct latch_addr_flash_info *info;
45 map_word datum;
46
47 info = (struct latch_addr_flash_info *)map->map_priv_1;
48
49 spin_lock(&info->lock);
50
51 info->set_window(ofs, info->data);
52 datum = inline_map_read(map, info->win_mask & ofs);
53
54 spin_unlock(&info->lock);
55
56 return datum;
57}
58
59static void lf_write(struct map_info *map, map_word datum, unsigned long ofs)
60{
61 struct latch_addr_flash_info *info;
62
63 info = (struct latch_addr_flash_info *)map->map_priv_1;
64
65 spin_lock(&info->lock);
66
67 info->set_window(ofs, info->data);
68 inline_map_write(map, datum, info->win_mask & ofs);
69
70 spin_unlock(&info->lock);
71}
72
73static void lf_copy_from(struct map_info *map, void *to,
74 unsigned long from, ssize_t len)
75{
76 struct latch_addr_flash_info *info =
77 (struct latch_addr_flash_info *) map->map_priv_1;
78 unsigned n;
79
80 while (len > 0) {
81 n = info->win_mask + 1 - (from & info->win_mask);
82 if (n > len)
83 n = len;
84
85 spin_lock(&info->lock);
86
87 info->set_window(from, info->data);
88 memcpy_fromio(to, map->virt + (from & info->win_mask), n);
89
90 spin_unlock(&info->lock);
91
92 to += n;
93 from += n;
94 len -= n;
95 }
96}
97
98static char *rom_probe_types[] = { "cfi_probe", NULL };
99
David Griegodcfb81d2010-11-30 15:32:05 +0530100static int latch_addr_flash_remove(struct platform_device *dev)
101{
102 struct latch_addr_flash_info *info;
103 struct latch_addr_flash_data *latch_addr_data;
104
105 info = platform_get_drvdata(dev);
106 if (info == NULL)
107 return 0;
108 platform_set_drvdata(dev, NULL);
109
110 latch_addr_data = dev->dev.platform_data;
111
112 if (info->mtd != NULL) {
Jamie Iles902766b2011-05-23 10:23:06 +0100113 if (info->nr_parts)
114 kfree(info->parts);
115 mtd_device_unregister(info->mtd);
David Griegodcfb81d2010-11-30 15:32:05 +0530116 map_destroy(info->mtd);
117 }
118
119 if (info->map.virt != NULL)
120 iounmap(info->map.virt);
121
122 if (info->res != NULL)
123 release_mem_region(info->res->start, resource_size(info->res));
124
125 kfree(info);
126
127 if (latch_addr_data->done)
128 latch_addr_data->done(latch_addr_data->data);
129
130 return 0;
131}
132
133static int __devinit latch_addr_flash_probe(struct platform_device *dev)
134{
135 struct latch_addr_flash_data *latch_addr_data;
136 struct latch_addr_flash_info *info;
137 resource_size_t win_base = dev->resource->start;
138 resource_size_t win_size = resource_size(dev->resource);
139 char **probe_type;
140 int chipsel;
141 int err;
142
143 latch_addr_data = dev->dev.platform_data;
144 if (latch_addr_data == NULL)
145 return -ENODEV;
146
147 pr_notice("latch-addr platform flash device: %#llx byte "
148 "window at %#.8llx\n",
149 (unsigned long long)win_size, (unsigned long long)win_base);
150
151 chipsel = dev->id;
152
153 if (latch_addr_data->init) {
154 err = latch_addr_data->init(latch_addr_data->data, chipsel);
155 if (err != 0)
156 return err;
157 }
158
159 info = kzalloc(sizeof(struct latch_addr_flash_info), GFP_KERNEL);
160 if (info == NULL) {
161 err = -ENOMEM;
162 goto done;
163 }
164
165 platform_set_drvdata(dev, info);
166
167 info->res = request_mem_region(win_base, win_size, DRIVER_NAME);
168 if (info->res == NULL) {
169 dev_err(&dev->dev, "Could not reserve memory region\n");
170 err = -EBUSY;
171 goto free_info;
172 }
173
174 info->map.name = DRIVER_NAME;
175 info->map.size = latch_addr_data->size;
176 info->map.bankwidth = latch_addr_data->width;
177
178 info->map.phys = NO_XIP;
179 info->map.virt = ioremap(win_base, win_size);
180 if (!info->map.virt) {
181 err = -ENOMEM;
182 goto free_res;
183 }
184
185 info->map.map_priv_1 = (unsigned long)info;
186
187 info->map.read = lf_read;
188 info->map.copy_from = lf_copy_from;
189 info->map.write = lf_write;
190 info->set_window = latch_addr_data->set_window;
191 info->data = latch_addr_data->data;
192 info->win_mask = win_size - 1;
193
194 spin_lock_init(&info->lock);
195
196 for (probe_type = rom_probe_types; !info->mtd && *probe_type;
197 probe_type++)
198 info->mtd = do_map_probe(*probe_type, &info->map);
199
200 if (info->mtd == NULL) {
201 dev_err(&dev->dev, "map_probe failed\n");
202 err = -ENODEV;
203 goto iounmap;
204 }
205 info->mtd->owner = THIS_MODULE;
206
Dmitry Eremin-Solenikovac9b0f32011-05-29 20:25:01 +0400207 err = parse_mtd_partitions(info->mtd, NULL, &info->parts, 0);
Jamie Iles902766b2011-05-23 10:23:06 +0100208 if (err > 0) {
209 mtd_device_register(info->mtd, info->parts, err);
210 return 0;
David Griegodcfb81d2010-11-30 15:32:05 +0530211 }
Jamie Iles902766b2011-05-23 10:23:06 +0100212 if (latch_addr_data->nr_parts) {
213 pr_notice("Using latch-addr-flash partition information\n");
214 mtd_device_register(info->mtd,
215 latch_addr_data->parts,
216 latch_addr_data->nr_parts);
217 return 0;
218 }
219
220 mtd_device_register(info->mtd, NULL, 0);
David Griegodcfb81d2010-11-30 15:32:05 +0530221 return 0;
222
223iounmap:
224 iounmap(info->map.virt);
225free_res:
226 release_mem_region(info->res->start, resource_size(info->res));
227free_info:
228 kfree(info);
229done:
230 if (latch_addr_data->done)
231 latch_addr_data->done(latch_addr_data->data);
232 return err;
233}
234
235static struct platform_driver latch_addr_flash_driver = {
236 .probe = latch_addr_flash_probe,
237 .remove = __devexit_p(latch_addr_flash_remove),
238 .driver = {
239 .name = DRIVER_NAME,
240 },
241};
242
243static int __init latch_addr_flash_init(void)
244{
245 return platform_driver_register(&latch_addr_flash_driver);
246}
247module_init(latch_addr_flash_init);
248
249static void __exit latch_addr_flash_exit(void)
250{
251 platform_driver_unregister(&latch_addr_flash_driver);
252}
253module_exit(latch_addr_flash_exit);
254
255MODULE_AUTHOR("David Griego <dgriego@mvista.com>");
256MODULE_DESCRIPTION("MTD map driver for flashes addressed physically with upper "
257 "address lines being set board specifically");
258MODULE_LICENSE("GPL v2");