include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / drivers / mtd / maps / gpio-addr-flash.c
blob7568c5f8b8ae648cb88754e94b34e03ec19c2d65
1 /*
2 * drivers/mtd/maps/gpio-addr-flash.c
4 * Handle the case where a flash device is mostly addressed using physical
5 * line and supplemented by GPIOs. This way you can hook up say a 8MiB flash
6 * to a 2MiB memory range and use the GPIOs to select a particular range.
8 * Copyright © 2000 Nicolas Pitre <nico@cam.org>
9 * Copyright © 2005-2009 Analog Devices Inc.
11 * Enter bugs at http://blackfin.uclinux.org/
13 * Licensed under the GPL-2 or later.
16 #include <linux/gpio.h>
17 #include <linux/init.h>
18 #include <linux/io.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/mtd/mtd.h>
22 #include <linux/mtd/map.h>
23 #include <linux/mtd/partitions.h>
24 #include <linux/mtd/physmap.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 #include <linux/types.h>
29 #define pr_devinit(fmt, args...) ({ static const __devinitconst char __fmt[] = fmt; printk(__fmt, ## args); })
31 #define DRIVER_NAME "gpio-addr-flash"
32 #define PFX DRIVER_NAME ": "
34 /**
35 * struct async_state - keep GPIO flash state
36 * @mtd: MTD state for this mapping
37 * @map: MTD map state for this flash
38 * @gpio_count: number of GPIOs used to address
39 * @gpio_addrs: array of GPIOs to twiddle
40 * @gpio_values: cached GPIO values
41 * @win_size: dedicated memory size (if no GPIOs)
43 struct async_state {
44 struct mtd_info *mtd;
45 struct map_info map;
46 size_t gpio_count;
47 unsigned *gpio_addrs;
48 int *gpio_values;
49 unsigned long win_size;
51 #define gf_map_info_to_state(mi) ((struct async_state *)(mi)->map_priv_1)
53 /**
54 * gf_set_gpios() - set GPIO address lines to access specified flash offset
55 * @state: GPIO flash state
56 * @ofs: desired offset to access
58 * Rather than call the GPIO framework every time, cache the last-programmed
59 * value. This speeds up sequential accesses (which are by far the most common
60 * type). We rely on the GPIO framework to treat non-zero value as high so
61 * that we don't have to normalize the bits.
63 static void gf_set_gpios(struct async_state *state, unsigned long ofs)
65 size_t i = 0;
66 int value;
67 ofs /= state->win_size;
68 do {
69 value = ofs & (1 << i);
70 if (state->gpio_values[i] != value) {
71 gpio_set_value(state->gpio_addrs[i], value);
72 state->gpio_values[i] = value;
74 } while (++i < state->gpio_count);
77 /**
78 * gf_read() - read a word at the specified offset
79 * @map: MTD map state
80 * @ofs: desired offset to read
82 static map_word gf_read(struct map_info *map, unsigned long ofs)
84 struct async_state *state = gf_map_info_to_state(map);
85 uint16_t word;
86 map_word test;
88 gf_set_gpios(state, ofs);
90 word = readw(map->virt + (ofs % state->win_size));
91 test.x[0] = word;
92 return test;
95 /**
96 * gf_copy_from() - copy a chunk of data from the flash
97 * @map: MTD map state
98 * @to: memory to copy to
99 * @from: flash offset to copy from
100 * @len: how much to copy
102 * We rely on the MTD layer to chunk up copies such that a single request here
103 * will not cross a window size. This allows us to only wiggle the GPIOs once
104 * before falling back to a normal memcpy. Reading the higher layer code shows
105 * that this is indeed the case, but add a BUG_ON() to future proof.
107 static void gf_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
109 struct async_state *state = gf_map_info_to_state(map);
111 gf_set_gpios(state, from);
113 /* BUG if operation crosses the win_size */
114 BUG_ON(!((from + len) % state->win_size <= (from + len)));
116 /* operation does not cross the win_size, so one shot it */
117 memcpy_fromio(to, map->virt + (from % state->win_size), len);
121 * gf_write() - write a word at the specified offset
122 * @map: MTD map state
123 * @ofs: desired offset to write
125 static void gf_write(struct map_info *map, map_word d1, unsigned long ofs)
127 struct async_state *state = gf_map_info_to_state(map);
128 uint16_t d;
130 gf_set_gpios(state, ofs);
132 d = d1.x[0];
133 writew(d, map->virt + (ofs % state->win_size));
137 * gf_copy_to() - copy a chunk of data to the flash
138 * @map: MTD map state
139 * @to: flash offset to copy to
140 * @from: memory to copy from
141 * @len: how much to copy
143 * See gf_copy_from() caveat.
145 static void gf_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
147 struct async_state *state = gf_map_info_to_state(map);
149 gf_set_gpios(state, to);
151 /* BUG if operation crosses the win_size */
152 BUG_ON(!((to + len) % state->win_size <= (to + len)));
154 /* operation does not cross the win_size, so one shot it */
155 memcpy_toio(map->virt + (to % state->win_size), from, len);
158 static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
161 * gpio_flash_probe() - setup a mapping for a GPIO assisted flash
162 * @pdev: platform device
164 * The platform resource layout expected looks something like:
165 * struct mtd_partition partitions[] = { ... };
166 * struct physmap_flash_data flash_data = { ... };
167 * unsigned flash_gpios[] = { GPIO_XX, GPIO_XX, ... };
168 * struct resource flash_resource[] = {
170 * .name = "cfi_probe",
171 * .start = 0x20000000,
172 * .end = 0x201fffff,
173 * .flags = IORESOURCE_MEM,
174 * }, {
175 * .start = (unsigned long)flash_gpios,
176 * .end = ARRAY_SIZE(flash_gpios),
177 * .flags = IORESOURCE_IRQ,
179 * };
180 * struct platform_device flash_device = {
181 * .name = "gpio-addr-flash",
182 * .dev = { .platform_data = &flash_data, },
183 * .num_resources = ARRAY_SIZE(flash_resource),
184 * .resource = flash_resource,
185 * ...
186 * };
188 static int __devinit gpio_flash_probe(struct platform_device *pdev)
190 int nr_parts;
191 size_t i, arr_size;
192 struct physmap_flash_data *pdata;
193 struct resource *memory;
194 struct resource *gpios;
195 struct async_state *state;
197 pdata = pdev->dev.platform_data;
198 memory = platform_get_resource(pdev, IORESOURCE_MEM, 0);
199 gpios = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
201 if (!memory || !gpios || !gpios->end)
202 return -EINVAL;
204 arr_size = sizeof(int) * gpios->end;
205 state = kzalloc(sizeof(*state) + arr_size, GFP_KERNEL);
206 if (!state)
207 return -ENOMEM;
210 * We cast start/end to known types in the boards file, so cast
211 * away their pointer types here to the known types (gpios->xxx).
213 state->gpio_count = gpios->end;
214 state->gpio_addrs = (void *)(unsigned long)gpios->start;
215 state->gpio_values = (void *)(state + 1);
216 state->win_size = resource_size(memory);
217 memset(state->gpio_values, 0xff, arr_size);
219 state->map.name = DRIVER_NAME;
220 state->map.read = gf_read;
221 state->map.copy_from = gf_copy_from;
222 state->map.write = gf_write;
223 state->map.copy_to = gf_copy_to;
224 state->map.bankwidth = pdata->width;
225 state->map.size = state->win_size * (1 << state->gpio_count);
226 state->map.virt = ioremap_nocache(memory->start, state->map.size);
227 state->map.phys = NO_XIP;
228 state->map.map_priv_1 = (unsigned long)state;
230 platform_set_drvdata(pdev, state);
232 i = 0;
233 do {
234 if (gpio_request(state->gpio_addrs[i], DRIVER_NAME)) {
235 pr_devinit(KERN_ERR PFX "failed to request gpio %d\n",
236 state->gpio_addrs[i]);
237 while (i--)
238 gpio_free(state->gpio_addrs[i]);
239 kfree(state);
240 return -EBUSY;
242 gpio_direction_output(state->gpio_addrs[i], 0);
243 } while (++i < state->gpio_count);
245 pr_devinit(KERN_NOTICE PFX "probing %d-bit flash bus\n",
246 state->map.bankwidth * 8);
247 state->mtd = do_map_probe(memory->name, &state->map);
248 if (!state->mtd) {
249 for (i = 0; i < state->gpio_count; ++i)
250 gpio_free(state->gpio_addrs[i]);
251 kfree(state);
252 return -ENXIO;
255 nr_parts = parse_mtd_partitions(state->mtd, part_probe_types,
256 &pdata->parts, 0);
257 if (nr_parts > 0) {
258 pr_devinit(KERN_NOTICE PFX "Using commandline partition definition\n");
259 kfree(pdata->parts);
260 } else if (pdata->nr_parts) {
261 pr_devinit(KERN_NOTICE PFX "Using board partition definition\n");
262 nr_parts = pdata->nr_parts;
263 } else {
264 pr_devinit(KERN_NOTICE PFX "no partition info available, registering whole flash at once\n");
265 nr_parts = 0;
268 mtd_device_register(state->mtd, pdata->parts, nr_parts);
270 return 0;
273 static int __devexit gpio_flash_remove(struct platform_device *pdev)
275 struct async_state *state = platform_get_drvdata(pdev);
276 size_t i = 0;
277 do {
278 gpio_free(state->gpio_addrs[i]);
279 } while (++i < state->gpio_count);
280 mtd_device_unregister(state->mtd);
281 map_destroy(state->mtd);
282 kfree(state);
283 return 0;
286 static struct platform_driver gpio_flash_driver = {
287 .probe = gpio_flash_probe,
288 .remove = __devexit_p(gpio_flash_remove),
289 .driver = {
290 .name = DRIVER_NAME,
294 static int __init gpio_flash_init(void)
296 return platform_driver_register(&gpio_flash_driver);
298 module_init(gpio_flash_init);
300 static void __exit gpio_flash_exit(void)
302 platform_driver_unregister(&gpio_flash_driver);
304 module_exit(gpio_flash_exit);
306 MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>");
307 MODULE_DESCRIPTION("MTD map driver for flashes addressed physically and with gpios");
308 MODULE_LICENSE("GPL");