Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gpio-addr-flash.c
Go to the documentation of this file.
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 <[email protected]>
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/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>
28 
29 #define pr_devinit(fmt, args...) ({ static const __devinitconst char __fmt[] = fmt; printk(__fmt, ## args); })
30 
31 #define DRIVER_NAME "gpio-addr-flash"
32 #define PFX DRIVER_NAME ": "
33 
43 struct async_state {
44  struct mtd_info *mtd;
45  struct map_info map;
46  size_t gpio_count;
47  unsigned *gpio_addrs;
49  unsigned long win_size;
50 };
51 #define gf_map_info_to_state(mi) ((struct async_state *)(mi)->map_priv_1)
52 
63 static 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 
82 static 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 
107 static 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 
125 static 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 
145 static void gf_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
146 {
147  struct async_state *state = gf_map_info_to_state(map);
148 
149  gf_set_gpios(state, to);
150 
151  /* BUG if operation crosses the win_size */
152  BUG_ON(!((to + len) % state->win_size <= (to + len)));
153 
154  /* operation does not cross the win_size, so one shot it */
155  memcpy_toio(map->virt + (to % state->win_size), from, len);
156 }
157 
158 static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
159 
188 static int __devinit gpio_flash_probe(struct platform_device *pdev)
189 {
190  size_t i, arr_size;
191  struct physmap_flash_data *pdata;
192  struct resource *memory;
193  struct resource *gpios;
194  struct async_state *state;
195 
196  pdata = pdev->dev.platform_data;
197  memory = platform_get_resource(pdev, IORESOURCE_MEM, 0);
198  gpios = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
199 
200  if (!memory || !gpios || !gpios->end)
201  return -EINVAL;
202 
203  arr_size = sizeof(int) * gpios->end;
204  state = kzalloc(sizeof(*state) + arr_size, GFP_KERNEL);
205  if (!state)
206  return -ENOMEM;
207 
208  /*
209  * We cast start/end to known types in the boards file, so cast
210  * away their pointer types here to the known types (gpios->xxx).
211  */
212  state->gpio_count = gpios->end;
213  state->gpio_addrs = (void *)(unsigned long)gpios->start;
214  state->gpio_values = (void *)(state + 1);
215  state->win_size = resource_size(memory);
216  memset(state->gpio_values, 0xff, arr_size);
217 
218  state->map.name = DRIVER_NAME;
219  state->map.read = gf_read;
220  state->map.copy_from = gf_copy_from;
221  state->map.write = gf_write;
222  state->map.copy_to = gf_copy_to;
223  state->map.bankwidth = pdata->width;
224  state->map.size = state->win_size * (1 << state->gpio_count);
225  state->map.virt = ioremap_nocache(memory->start, state->map.size);
226  state->map.phys = NO_XIP;
227  state->map.map_priv_1 = (unsigned long)state;
228 
229  platform_set_drvdata(pdev, state);
230 
231  i = 0;
232  do {
233  if (gpio_request(state->gpio_addrs[i], DRIVER_NAME)) {
234  pr_devinit(KERN_ERR PFX "failed to request gpio %d\n",
235  state->gpio_addrs[i]);
236  while (i--)
237  gpio_free(state->gpio_addrs[i]);
238  kfree(state);
239  return -EBUSY;
240  }
241  gpio_direction_output(state->gpio_addrs[i], 0);
242  } while (++i < state->gpio_count);
243 
244  pr_devinit(KERN_NOTICE PFX "probing %d-bit flash bus\n",
245  state->map.bankwidth * 8);
246  state->mtd = do_map_probe(memory->name, &state->map);
247  if (!state->mtd) {
248  for (i = 0; i < state->gpio_count; ++i)
249  gpio_free(state->gpio_addrs[i]);
250  kfree(state);
251  return -ENXIO;
252  }
253 
254 
255  mtd_device_parse_register(state->mtd, part_probe_types, NULL,
256  pdata->parts, pdata->nr_parts);
257 
258  return 0;
259 }
260 
261 static int __devexit gpio_flash_remove(struct platform_device *pdev)
262 {
263  struct async_state *state = platform_get_drvdata(pdev);
264  size_t i = 0;
265  do {
266  gpio_free(state->gpio_addrs[i]);
267  } while (++i < state->gpio_count);
268  mtd_device_unregister(state->mtd);
269  map_destroy(state->mtd);
270  kfree(state);
271  return 0;
272 }
273 
274 static struct platform_driver gpio_flash_driver = {
275  .probe = gpio_flash_probe,
276  .remove = __devexit_p(gpio_flash_remove),
277  .driver = {
278  .name = DRIVER_NAME,
279  },
280 };
281 
282 module_platform_driver(gpio_flash_driver);
283 
284 MODULE_AUTHOR("Mike Frysinger <[email protected]>");
285 MODULE_DESCRIPTION("MTD map driver for flashes addressed physically and with gpios");
286 MODULE_LICENSE("GPL");