gpio-addr-flash.c 7.9 KB

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