Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v3.16-rc5 288 lines 8.2 kB view raw
1/* 2 * drivers/mtd/maps/gpio-addr-flash.c 3 * 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. 7 * 8 * Copyright © 2000 Nicolas Pitre <nico@cam.org> 9 * Copyright © 2005-2009 Analog Devices Inc. 10 * 11 * Enter bugs at http://blackfin.uclinux.org/ 12 * 13 * Licensed under the GPL-2 or later. 14 */ 15 16#include <linux/gpio.h> 17#include <linux/io.h> 18#include <linux/kernel.h> 19#include <linux/module.h> 20#include <linux/mtd/mtd.h> 21#include <linux/mtd/map.h> 22#include <linux/mtd/partitions.h> 23#include <linux/mtd/physmap.h> 24#include <linux/platform_device.h> 25#include <linux/slab.h> 26#include <linux/types.h> 27 28#define pr_devinit(fmt, args...) \ 29 ({ static const char __fmt[] = fmt; printk(__fmt, ## args); }) 30 31#define DRIVER_NAME "gpio-addr-flash" 32#define PFX DRIVER_NAME ": " 33 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) 42 */ 43struct 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; 50}; 51#define gf_map_info_to_state(mi) ((struct async_state *)(mi)->map_priv_1) 52 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 57 * 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. 62 */ 63static void gf_set_gpios(struct async_state *state, unsigned long ofs) 64{ 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; 73 } 74 } while (++i < state->gpio_count); 75} 76 77/** 78 * gf_read() - read a word at the specified offset 79 * @map: MTD map state 80 * @ofs: desired offset to read 81 */ 82static map_word gf_read(struct map_info *map, unsigned long ofs) 83{ 84 struct async_state *state = gf_map_info_to_state(map); 85 uint16_t word; 86 map_word test; 87 88 gf_set_gpios(state, ofs); 89 90 word = readw(map->virt + (ofs % state->win_size)); 91 test.x[0] = word; 92 return test; 93} 94 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 101 * 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. 106 */ 107static void gf_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len) 108{ 109 struct async_state *state = gf_map_info_to_state(map); 110 111 gf_set_gpios(state, from); 112 113 /* BUG if operation crosses the win_size */ 114 BUG_ON(!((from + len) % state->win_size <= (from + len))); 115 116 /* operation does not cross the win_size, so one shot it */ 117 memcpy_fromio(to, map->virt + (from % state->win_size), len); 118} 119 120/** 121 * gf_write() - write a word at the specified offset 122 * @map: MTD map state 123 * @ofs: desired offset to write 124 */ 125static void gf_write(struct map_info *map, map_word d1, unsigned long ofs) 126{ 127 struct async_state *state = gf_map_info_to_state(map); 128 uint16_t d; 129 130 gf_set_gpios(state, ofs); 131 132 d = d1.x[0]; 133 writew(d, map->virt + (ofs % state->win_size)); 134} 135 136/** 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 142 * 143 * See gf_copy_from() caveat. 144 */ 145static void gf_copy_to(struct map_info *map, unsigned long to, 146 const void *from, ssize_t len) 147{ 148 struct async_state *state = gf_map_info_to_state(map); 149 150 gf_set_gpios(state, to); 151 152 /* BUG if operation crosses the win_size */ 153 BUG_ON(!((to + len) % state->win_size <= (to + len))); 154 155 /* operation does not cross the win_size, so one shot it */ 156 memcpy_toio(map->virt + (to % state->win_size), from, len); 157} 158 159static const char * const part_probe_types[] = { 160 "cmdlinepart", "RedBoot", NULL }; 161 162/** 163 * gpio_flash_probe() - setup a mapping for a GPIO assisted flash 164 * @pdev: platform device 165 * 166 * The platform resource layout expected looks something like: 167 * struct mtd_partition partitions[] = { ... }; 168 * struct physmap_flash_data flash_data = { ... }; 169 * unsigned flash_gpios[] = { GPIO_XX, GPIO_XX, ... }; 170 * struct resource flash_resource[] = { 171 * { 172 * .name = "cfi_probe", 173 * .start = 0x20000000, 174 * .end = 0x201fffff, 175 * .flags = IORESOURCE_MEM, 176 * }, { 177 * .start = (unsigned long)flash_gpios, 178 * .end = ARRAY_SIZE(flash_gpios), 179 * .flags = IORESOURCE_IRQ, 180 * } 181 * }; 182 * struct platform_device flash_device = { 183 * .name = "gpio-addr-flash", 184 * .dev = { .platform_data = &flash_data, }, 185 * .num_resources = ARRAY_SIZE(flash_resource), 186 * .resource = flash_resource, 187 * ... 188 * }; 189 */ 190static int gpio_flash_probe(struct platform_device *pdev) 191{ 192 size_t i, arr_size; 193 struct physmap_flash_data *pdata; 194 struct resource *memory; 195 struct resource *gpios; 196 struct async_state *state; 197 198 pdata = dev_get_platdata(&pdev->dev); 199 memory = platform_get_resource(pdev, IORESOURCE_MEM, 0); 200 gpios = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 201 202 if (!memory || !gpios || !gpios->end) 203 return -EINVAL; 204 205 arr_size = sizeof(int) * gpios->end; 206 state = kzalloc(sizeof(*state) + arr_size, GFP_KERNEL); 207 if (!state) 208 return -ENOMEM; 209 210 /* 211 * We cast start/end to known types in the boards file, so cast 212 * away their pointer types here to the known types (gpios->xxx). 213 */ 214 state->gpio_count = gpios->end; 215 state->gpio_addrs = (void *)(unsigned long)gpios->start; 216 state->gpio_values = (void *)(state + 1); 217 state->win_size = resource_size(memory); 218 memset(state->gpio_values, 0xff, arr_size); 219 220 state->map.name = DRIVER_NAME; 221 state->map.read = gf_read; 222 state->map.copy_from = gf_copy_from; 223 state->map.write = gf_write; 224 state->map.copy_to = gf_copy_to; 225 state->map.bankwidth = pdata->width; 226 state->map.size = state->win_size * (1 << state->gpio_count); 227 state->map.virt = ioremap_nocache(memory->start, state->map.size); 228 state->map.phys = NO_XIP; 229 state->map.map_priv_1 = (unsigned long)state; 230 231 platform_set_drvdata(pdev, state); 232 233 i = 0; 234 do { 235 if (gpio_request(state->gpio_addrs[i], DRIVER_NAME)) { 236 pr_devinit(KERN_ERR PFX "failed to request gpio %d\n", 237 state->gpio_addrs[i]); 238 while (i--) 239 gpio_free(state->gpio_addrs[i]); 240 kfree(state); 241 return -EBUSY; 242 } 243 gpio_direction_output(state->gpio_addrs[i], 0); 244 } while (++i < state->gpio_count); 245 246 pr_devinit(KERN_NOTICE PFX "probing %d-bit flash bus\n", 247 state->map.bankwidth * 8); 248 state->mtd = do_map_probe(memory->name, &state->map); 249 if (!state->mtd) { 250 for (i = 0; i < state->gpio_count; ++i) 251 gpio_free(state->gpio_addrs[i]); 252 kfree(state); 253 return -ENXIO; 254 } 255 256 257 mtd_device_parse_register(state->mtd, part_probe_types, NULL, 258 pdata->parts, pdata->nr_parts); 259 260 return 0; 261} 262 263static int gpio_flash_remove(struct platform_device *pdev) 264{ 265 struct async_state *state = platform_get_drvdata(pdev); 266 size_t i = 0; 267 do { 268 gpio_free(state->gpio_addrs[i]); 269 } while (++i < state->gpio_count); 270 mtd_device_unregister(state->mtd); 271 map_destroy(state->mtd); 272 kfree(state); 273 return 0; 274} 275 276static struct platform_driver gpio_flash_driver = { 277 .probe = gpio_flash_probe, 278 .remove = gpio_flash_remove, 279 .driver = { 280 .name = DRIVER_NAME, 281 }, 282}; 283 284module_platform_driver(gpio_flash_driver); 285 286MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>"); 287MODULE_DESCRIPTION("MTD map driver for flashes addressed physically and with gpios"); 288MODULE_LICENSE("GPL");