gpio-addr-flash.c 8.6 KB

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