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
{
29 void (*set_window
)(unsigned long offset
, void *data
);
32 /* cache; could be found out of res */
33 unsigned long win_mask
;
38 static map_word
lf_read(struct map_info
*map
, unsigned long ofs
)
40 struct latch_addr_flash_info
*info
;
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
);
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
;
77 n
= info
->win_mask
+ 1 - (from
& info
->win_mask
);
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
);
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
);
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
));
120 if (latch_addr_data
->done
)
121 latch_addr_data
->done(latch_addr_data
->data
);
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
);
136 latch_addr_data
= dev_get_platdata(&dev
->dev
);
137 if (latch_addr_data
== NULL
)
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
);
146 if (latch_addr_data
->init
) {
147 err
= latch_addr_data
->init(latch_addr_data
->data
, chipsel
);
152 info
= kzalloc(sizeof(struct latch_addr_flash_info
), GFP_KERNEL
);
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");
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
) {
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
;
191 info
->mtd
= do_map_probe(*probe_type
, &info
->map
);
193 if (info
->mtd
== NULL
) {
194 dev_err(&dev
->dev
, "map_probe failed\n");
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
);
206 iounmap(info
->map
.virt
);
208 release_mem_region(info
->res
->start
, resource_size(info
->res
));
212 if (latch_addr_data
->done
)
213 latch_addr_data
->done(latch_addr_data
->data
);
217 static struct platform_driver latch_addr_flash_driver
= {
218 .probe
= latch_addr_flash_probe
,
219 .remove
= latch_addr_flash_remove
,
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");