gpio-addr-flash.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. #ifdef CONFIG_MTD_PARTITIONS
  139. static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
  140. #endif
  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 __devinit gpio_flash_probe(struct platform_device *pdev)
  170. {
  171. int ret;
  172. size_t i, arr_size;
  173. struct physmap_flash_data *pdata;
  174. struct resource *memory;
  175. struct resource *gpios;
  176. struct async_state *state;
  177. pdata = pdev->dev.platform_data;
  178. memory = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  179. gpios = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  180. if (!memory || !gpios || !gpios->end)
  181. return -EINVAL;
  182. arr_size = sizeof(int) * gpios->end;
  183. state = kzalloc(sizeof(*state) + arr_size, GFP_KERNEL);
  184. if (!state)
  185. return -ENOMEM;
  186. /*
  187. * We cast start/end to known types in the boards file, so cast
  188. * away their pointer types here to the known types (gpios->xxx).
  189. */
  190. state->gpio_count = gpios->end;
  191. state->gpio_addrs = (void *)(unsigned long)gpios->start;
  192. state->gpio_values = (void *)(state + 1);
  193. state->win_size = resource_size(memory);
  194. memset(state->gpio_values, 0xff, arr_size);
  195. state->map.name = DRIVER_NAME;
  196. state->map.read = gf_read;
  197. state->map.copy_from = gf_copy_from;
  198. state->map.write = gf_write;
  199. state->map.copy_to = gf_copy_to;
  200. state->map.bankwidth = pdata->width;
  201. state->map.size = state->win_size * (1 << state->gpio_count);
  202. state->map.virt = ioremap_nocache(memory->start, state->map.size);
  203. state->map.phys = NO_XIP;
  204. state->map.map_priv_1 = (unsigned long)state;
  205. platform_set_drvdata(pdev, state);
  206. i = 0;
  207. do {
  208. if (gpio_request(state->gpio_addrs[i], DRIVER_NAME)) {
  209. pr_devinit(KERN_ERR PFX "failed to request gpio %d\n",
  210. state->gpio_addrs[i]);
  211. while (i--)
  212. gpio_free(state->gpio_addrs[i]);
  213. kfree(state);
  214. return -EBUSY;
  215. }
  216. gpio_direction_output(state->gpio_addrs[i], 0);
  217. } while (++i < state->gpio_count);
  218. pr_devinit(KERN_NOTICE PFX "probing %d-bit flash bus\n",
  219. state->map.bankwidth * 8);
  220. state->mtd = do_map_probe(memory->name, &state->map);
  221. if (!state->mtd) {
  222. for (i = 0; i < state->gpio_count; ++i)
  223. gpio_free(state->gpio_addrs[i]);
  224. kfree(state);
  225. return -ENXIO;
  226. }
  227. #ifdef CONFIG_MTD_PARTITIONS
  228. ret = parse_mtd_partitions(state->mtd, part_probe_types, &pdata->parts, 0);
  229. if (ret > 0) {
  230. pr_devinit(KERN_NOTICE PFX "Using commandline partition definition\n");
  231. add_mtd_partitions(state->mtd, pdata->parts, ret);
  232. kfree(pdata->parts);
  233. } else if (pdata->nr_parts) {
  234. pr_devinit(KERN_NOTICE PFX "Using board partition definition\n");
  235. add_mtd_partitions(state->mtd, pdata->parts, pdata->nr_parts);
  236. } else
  237. #endif
  238. {
  239. pr_devinit(KERN_NOTICE PFX "no partition info available, registering whole flash at once\n");
  240. add_mtd_device(state->mtd);
  241. }
  242. return 0;
  243. }
  244. static int __devexit gpio_flash_remove(struct platform_device *pdev)
  245. {
  246. struct async_state *state = platform_get_drvdata(pdev);
  247. size_t i = 0;
  248. do {
  249. gpio_free(state->gpio_addrs[i]);
  250. } while (++i < state->gpio_count);
  251. #ifdef CONFIG_MTD_PARTITIONS
  252. del_mtd_partitions(state->mtd);
  253. #endif
  254. map_destroy(state->mtd);
  255. kfree(state);
  256. return 0;
  257. }
  258. static struct platform_driver gpio_flash_driver = {
  259. .probe = gpio_flash_probe,
  260. .remove = __devexit_p(gpio_flash_remove),
  261. .driver = {
  262. .name = DRIVER_NAME,
  263. },
  264. };
  265. static int __init gpio_flash_init(void)
  266. {
  267. return platform_driver_register(&gpio_flash_driver);
  268. }
  269. module_init(gpio_flash_init);
  270. static void __exit gpio_flash_exit(void)
  271. {
  272. platform_driver_unregister(&gpio_flash_driver);
  273. }
  274. module_exit(gpio_flash_exit);
  275. MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>");
  276. MODULE_DESCRIPTION("MTD map driver for flashes addressed physically and with gpios");
  277. MODULE_LICENSE("GPL");