gpio-addr-flash.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. #include <linux/gpio.h>
  16. #include <linux/init.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. #define pr_devinit(fmt, args...) ({ static const __devinitconst char __fmt[] = fmt; printk(__fmt, ## args); })
  28. #define DRIVER_NAME "gpio-addr-flash"
  29. #define PFX DRIVER_NAME ": "
  30. /**
  31. * struct async_state - keep GPIO flash state
  32. * @mtd: MTD state for this mapping
  33. * @map: MTD map state for this flash
  34. * @gpio_count: number of GPIOs used to address
  35. * @gpio_addrs: array of GPIOs to twiddle
  36. * @gpio_values: cached GPIO values
  37. * @win_size: dedicated memory size (if no GPIOs)
  38. */
  39. struct async_state {
  40. struct mtd_info *mtd;
  41. struct map_info map;
  42. size_t gpio_count;
  43. unsigned *gpio_addrs;
  44. int *gpio_values;
  45. unsigned long win_size;
  46. };
  47. #define gf_map_info_to_state(mi) ((struct async_state *)(mi)->map_priv_1)
  48. /**
  49. * gf_set_gpios() - set GPIO address lines to access specified flash offset
  50. * @state: GPIO flash state
  51. * @ofs: desired offset to access
  52. *
  53. * Rather than call the GPIO framework every time, cache the last-programmed
  54. * value. This speeds up sequential accesses (which are by far the most common
  55. * type). We rely on the GPIO framework to treat non-zero value as high so
  56. * that we don't have to normalize the bits.
  57. */
  58. static void gf_set_gpios(struct async_state *state, unsigned long ofs)
  59. {
  60. size_t i = 0;
  61. int value;
  62. ofs /= state->win_size;
  63. do {
  64. value = ofs & (1 << i);
  65. if (state->gpio_values[i] != value) {
  66. gpio_set_value(state->gpio_addrs[i], value);
  67. state->gpio_values[i] = value;
  68. }
  69. } while (++i < state->gpio_count);
  70. }
  71. /**
  72. * gf_read() - read a word at the specified offset
  73. * @map: MTD map state
  74. * @ofs: desired offset to read
  75. */
  76. static map_word gf_read(struct map_info *map, unsigned long ofs)
  77. {
  78. struct async_state *state = gf_map_info_to_state(map);
  79. uint16_t word;
  80. map_word test;
  81. gf_set_gpios(state, ofs);
  82. word = readw(map->virt + (ofs % state->win_size));
  83. test.x[0] = word;
  84. return test;
  85. }
  86. /**
  87. * gf_copy_from() - copy a chunk of data from the flash
  88. * @map: MTD map state
  89. * @to: memory to copy to
  90. * @from: flash offset to copy from
  91. * @len: how much to copy
  92. *
  93. * We rely on the MTD layer to chunk up copies such that a single request here
  94. * will not cross a window size. This allows us to only wiggle the GPIOs once
  95. * before falling back to a normal memcpy. Reading the higher layer code shows
  96. * that this is indeed the case, but add a BUG_ON() to future proof.
  97. */
  98. static void gf_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
  99. {
  100. struct async_state *state = gf_map_info_to_state(map);
  101. gf_set_gpios(state, from);
  102. /* BUG if operation crosses the win_size */
  103. BUG_ON(!((from + len) % state->win_size <= (from + len)));
  104. /* operation does not cross the win_size, so one shot it */
  105. memcpy_fromio(to, map->virt + (from % state->win_size), len);
  106. }
  107. /**
  108. * gf_write() - write a word at the specified offset
  109. * @map: MTD map state
  110. * @ofs: desired offset to write
  111. */
  112. static void gf_write(struct map_info *map, map_word d1, unsigned long ofs)
  113. {
  114. struct async_state *state = gf_map_info_to_state(map);
  115. uint16_t d;
  116. gf_set_gpios(state, ofs);
  117. d = d1.x[0];
  118. writew(d, map->virt + (ofs % state->win_size));
  119. }
  120. /**
  121. * gf_copy_to() - copy a chunk of data to the flash
  122. * @map: MTD map state
  123. * @to: flash offset to copy to
  124. * @from: memory to copy from
  125. * @len: how much to copy
  126. *
  127. * See gf_copy_from() caveat.
  128. */
  129. static void gf_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
  130. {
  131. struct async_state *state = gf_map_info_to_state(map);
  132. gf_set_gpios(state, to);
  133. /* BUG if operation crosses the win_size */
  134. BUG_ON(!((to + len) % state->win_size <= (to + len)));
  135. /* operation does not cross the win_size, so one shot it */
  136. memcpy_toio(map->virt + (to % state->win_size), from, len);
  137. }
  138. static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
  139. /**
  140. * gpio_flash_probe() - setup a mapping for a GPIO assisted flash
  141. * @pdev: platform device
  142. *
  143. * The platform resource layout expected looks something like:
  144. * struct mtd_partition partitions[] = { ... };
  145. * struct physmap_flash_data flash_data = { ... };
  146. * unsigned flash_gpios[] = { GPIO_XX, GPIO_XX, ... };
  147. * struct resource flash_resource[] = {
  148. * {
  149. * .name = "cfi_probe",
  150. * .start = 0x20000000,
  151. * .end = 0x201fffff,
  152. * .flags = IORESOURCE_MEM,
  153. * }, {
  154. * .start = (unsigned long)flash_gpios,
  155. * .end = ARRAY_SIZE(flash_gpios),
  156. * .flags = IORESOURCE_IRQ,
  157. * }
  158. * };
  159. * struct platform_device flash_device = {
  160. * .name = "gpio-addr-flash",
  161. * .dev = { .platform_data = &flash_data, },
  162. * .num_resources = ARRAY_SIZE(flash_resource),
  163. * .resource = flash_resource,
  164. * ...
  165. * };
  166. */
  167. static int __devinit gpio_flash_probe(struct platform_device *pdev)
  168. {
  169. size_t i, arr_size;
  170. struct physmap_flash_data *pdata;
  171. struct resource *memory;
  172. struct resource *gpios;
  173. struct async_state *state;
  174. pdata = pdev->dev.platform_data;
  175. memory = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  176. gpios = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  177. if (!memory || !gpios || !gpios->end)
  178. return -EINVAL;
  179. arr_size = sizeof(int) * gpios->end;
  180. state = kzalloc(sizeof(*state) + arr_size, GFP_KERNEL);
  181. if (!state)
  182. return -ENOMEM;
  183. /*
  184. * We cast start/end to known types in the boards file, so cast
  185. * away their pointer types here to the known types (gpios->xxx).
  186. */
  187. state->gpio_count = gpios->end;
  188. state->gpio_addrs = (void *)(unsigned long)gpios->start;
  189. state->gpio_values = (void *)(state + 1);
  190. state->win_size = resource_size(memory);
  191. memset(state->gpio_values, 0xff, arr_size);
  192. state->map.name = DRIVER_NAME;
  193. state->map.read = gf_read;
  194. state->map.copy_from = gf_copy_from;
  195. state->map.write = gf_write;
  196. state->map.copy_to = gf_copy_to;
  197. state->map.bankwidth = pdata->width;
  198. state->map.size = state->win_size * (1 << state->gpio_count);
  199. state->map.virt = ioremap_nocache(memory->start, state->map.size);
  200. state->map.phys = NO_XIP;
  201. state->map.map_priv_1 = (unsigned long)state;
  202. platform_set_drvdata(pdev, state);
  203. i = 0;
  204. do {
  205. if (gpio_request(state->gpio_addrs[i], DRIVER_NAME)) {
  206. pr_devinit(KERN_ERR PFX "failed to request gpio %d\n",
  207. state->gpio_addrs[i]);
  208. while (i--)
  209. gpio_free(state->gpio_addrs[i]);
  210. kfree(state);
  211. return -EBUSY;
  212. }
  213. gpio_direction_output(state->gpio_addrs[i], 0);
  214. } while (++i < state->gpio_count);
  215. pr_devinit(KERN_NOTICE PFX "probing %d-bit flash bus\n",
  216. state->map.bankwidth * 8);
  217. state->mtd = do_map_probe(memory->name, &state->map);
  218. if (!state->mtd) {
  219. for (i = 0; i < state->gpio_count; ++i)
  220. gpio_free(state->gpio_addrs[i]);
  221. kfree(state);
  222. return -ENXIO;
  223. }
  224. mtd_device_parse_register(state->mtd, part_probe_types, NULL,
  225. pdata->parts, pdata->nr_parts);
  226. return 0;
  227. }
  228. static int __devexit gpio_flash_remove(struct platform_device *pdev)
  229. {
  230. struct async_state *state = platform_get_drvdata(pdev);
  231. size_t i = 0;
  232. do {
  233. gpio_free(state->gpio_addrs[i]);
  234. } while (++i < state->gpio_count);
  235. mtd_device_unregister(state->mtd);
  236. map_destroy(state->mtd);
  237. kfree(state);
  238. return 0;
  239. }
  240. static struct platform_driver gpio_flash_driver = {
  241. .probe = gpio_flash_probe,
  242. .remove = __devexit_p(gpio_flash_remove),
  243. .driver = {
  244. .name = DRIVER_NAME,
  245. },
  246. };
  247. module_platform_driver(gpio_flash_driver);
  248. MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>");
  249. MODULE_DESCRIPTION("MTD map driver for flashes addressed physically and with gpios");
  250. MODULE_LICENSE("GPL");