gpio-addr-flash.c 8.0 KB

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