Linux 4.9.137
[linux/fpc-iii.git] / drivers / mtd / maps / latch-addr-flash.c
blob6dc97aa667dc553cc44033b34394374b3c8b36d1
1 /*
2 * Interface for NOR flash driver whose high address lines are latched
4 * Copyright © 2000 Nicolas Pitre <nico@cam.org>
5 * Copyright © 2005-2008 Analog Devices Inc.
6 * Copyright © 2008 MontaVista Software, Inc. <source@mvista.com>
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.
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>
22 #define DRIVER_NAME "latch-addr-flash"
24 struct latch_addr_flash_info {
25 struct mtd_info *mtd;
26 struct map_info map;
27 struct resource *res;
29 void (*set_window)(unsigned long offset, void *data);
30 void *data;
32 /* cache; could be found out of res */
33 unsigned long win_mask;
35 spinlock_t lock;
38 static map_word lf_read(struct map_info *map, unsigned long ofs)
40 struct latch_addr_flash_info *info;
41 map_word datum;
43 info = (struct latch_addr_flash_info *)map->map_priv_1;
45 spin_lock(&info->lock);
47 info->set_window(ofs, info->data);
48 datum = inline_map_read(map, info->win_mask & ofs);
50 spin_unlock(&info->lock);
52 return datum;
55 static void lf_write(struct map_info *map, map_word datum, unsigned long ofs)
57 struct latch_addr_flash_info *info;
59 info = (struct latch_addr_flash_info *)map->map_priv_1;
61 spin_lock(&info->lock);
63 info->set_window(ofs, info->data);
64 inline_map_write(map, datum, info->win_mask & ofs);
66 spin_unlock(&info->lock);
69 static void lf_copy_from(struct map_info *map, void *to,
70 unsigned long from, ssize_t len)
72 struct latch_addr_flash_info *info =
73 (struct latch_addr_flash_info *) map->map_priv_1;
74 unsigned n;
76 while (len > 0) {
77 n = info->win_mask + 1 - (from & info->win_mask);
78 if (n > len)
79 n = len;
81 spin_lock(&info->lock);
83 info->set_window(from, info->data);
84 memcpy_fromio(to, map->virt + (from & info->win_mask), n);
86 spin_unlock(&info->lock);
88 to += n;
89 from += n;
90 len -= n;
94 static char *rom_probe_types[] = { "cfi_probe", NULL };
96 static int latch_addr_flash_remove(struct platform_device *dev)
98 struct latch_addr_flash_info *info;
99 struct latch_addr_flash_data *latch_addr_data;
101 info = platform_get_drvdata(dev);
102 if (info == NULL)
103 return 0;
105 latch_addr_data = dev_get_platdata(&dev->dev);
107 if (info->mtd != NULL) {
108 mtd_device_unregister(info->mtd);
109 map_destroy(info->mtd);
112 if (info->map.virt != NULL)
113 iounmap(info->map.virt);
115 if (info->res != NULL)
116 release_mem_region(info->res->start, resource_size(info->res));
118 kfree(info);
120 if (latch_addr_data->done)
121 latch_addr_data->done(latch_addr_data->data);
123 return 0;
126 static int latch_addr_flash_probe(struct platform_device *dev)
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;
136 latch_addr_data = dev_get_platdata(&dev->dev);
137 if (latch_addr_data == NULL)
138 return -ENODEV;
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);
144 chipsel = dev->id;
146 if (latch_addr_data->init) {
147 err = latch_addr_data->init(latch_addr_data->data, chipsel);
148 if (err != 0)
149 return err;
152 info = kzalloc(sizeof(struct latch_addr_flash_info), GFP_KERNEL);
153 if (info == NULL) {
154 err = -ENOMEM;
155 goto done;
158 platform_set_drvdata(dev, info);
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;
167 info->map.name = DRIVER_NAME;
168 info->map.size = latch_addr_data->size;
169 info->map.bankwidth = latch_addr_data->width;
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;
178 info->map.map_priv_1 = (unsigned long)info;
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;
187 spin_lock_init(&info->lock);
189 for (probe_type = rom_probe_types; !info->mtd && *probe_type;
190 probe_type++)
191 info->mtd = do_map_probe(*probe_type, &info->map);
193 if (info->mtd == NULL) {
194 dev_err(&dev->dev, "map_probe failed\n");
195 err = -ENODEV;
196 goto iounmap;
198 info->mtd->dev.parent = &dev->dev;
200 mtd_device_parse_register(info->mtd, NULL, NULL,
201 latch_addr_data->parts,
202 latch_addr_data->nr_parts);
203 return 0;
205 iounmap:
206 iounmap(info->map.virt);
207 free_res:
208 release_mem_region(info->res->start, resource_size(info->res));
209 free_info:
210 kfree(info);
211 done:
212 if (latch_addr_data->done)
213 latch_addr_data->done(latch_addr_data->data);
214 return err;
217 static 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,
225 module_platform_driver(latch_addr_flash_driver);
227 MODULE_AUTHOR("David Griego <dgriego@mvista.com>");
228 MODULE_DESCRIPTION("MTD map driver for flashes addressed physically with upper "
229 "address lines being set board specifically");
230 MODULE_LICENSE("GPL v2");