gpio-addr-flash.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 pr_devinit(fmt, args...) \
  27. ({ static const 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. * The "from" region may straddle more than one window, so toggle the GPIOs for
  94. * each window region before reading its data.
  95. */
  96. static void gf_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
  97. {
  98. struct async_state *state = gf_map_info_to_state(map);
  99. int this_len;
  100. while (len) {
  101. if ((from % state->win_size) + len > state->win_size)
  102. this_len = state->win_size - (from % state->win_size);
  103. else
  104. this_len = len;
  105. gf_set_gpios(state, from);
  106. memcpy_fromio(to, map->virt + (from % state->win_size),
  107. this_len);
  108. len -= this_len;
  109. from += this_len;
  110. to += this_len;
  111. }
  112. }
  113. /**
  114. * gf_write() - write a word at the specified offset
  115. * @map: MTD map state
  116. * @ofs: desired offset to write
  117. */
  118. static void gf_write(struct map_info *map, map_word d1, unsigned long ofs)
  119. {
  120. struct async_state *state = gf_map_info_to_state(map);
  121. uint16_t d;
  122. gf_set_gpios(state, ofs);
  123. d = d1.x[0];
  124. writew(d, map->virt + (ofs % state->win_size));
  125. }
  126. /**
  127. * gf_copy_to() - copy a chunk of data to the flash
  128. * @map: MTD map state
  129. * @to: flash offset to copy to
  130. * @from: memory to copy from
  131. * @len: how much to copy
  132. *
  133. * See gf_copy_from() caveat.
  134. */
  135. static void gf_copy_to(struct map_info *map, unsigned long to,
  136. const void *from, ssize_t len)
  137. {
  138. struct async_state *state = gf_map_info_to_state(map);
  139. int this_len;
  140. while (len) {
  141. if ((to % state->win_size) + len > state->win_size)
  142. this_len = state->win_size - (to % state->win_size);
  143. else
  144. this_len = len;
  145. gf_set_gpios(state, to);
  146. memcpy_toio(map->virt + (to % state->win_size), from, len);
  147. len -= this_len;
  148. to += this_len;
  149. from += this_len;
  150. }
  151. }
  152. static const char * const part_probe_types[] = {
  153. "cmdlinepart", "RedBoot", NULL };
  154. /**
  155. * gpio_flash_probe() - setup a mapping for a GPIO assisted flash
  156. * @pdev: platform device
  157. *
  158. * The platform resource layout expected looks something like:
  159. * struct mtd_partition partitions[] = { ... };
  160. * struct physmap_flash_data flash_data = { ... };
  161. * unsigned flash_gpios[] = { GPIO_XX, GPIO_XX, ... };
  162. * struct resource flash_resource[] = {
  163. * {
  164. * .name = "cfi_probe",
  165. * .start = 0x20000000,
  166. * .end = 0x201fffff,
  167. * .flags = IORESOURCE_MEM,
  168. * }, {
  169. * .start = (unsigned long)flash_gpios,
  170. * .end = ARRAY_SIZE(flash_gpios),
  171. * .flags = IORESOURCE_IRQ,
  172. * }
  173. * };
  174. * struct platform_device flash_device = {
  175. * .name = "gpio-addr-flash",
  176. * .dev = { .platform_data = &flash_data, },
  177. * .num_resources = ARRAY_SIZE(flash_resource),
  178. * .resource = flash_resource,
  179. * ...
  180. * };
  181. */
  182. static int gpio_flash_probe(struct platform_device *pdev)
  183. {
  184. size_t i, arr_size;
  185. struct physmap_flash_data *pdata;
  186. struct resource *memory;
  187. struct resource *gpios;
  188. struct async_state *state;
  189. pdata = dev_get_platdata(&pdev->dev);
  190. memory = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  191. gpios = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  192. if (!memory || !gpios || !gpios->end)
  193. return -EINVAL;
  194. arr_size = sizeof(int) * gpios->end;
  195. state = kzalloc(sizeof(*state) + arr_size, GFP_KERNEL);
  196. if (!state)
  197. return -ENOMEM;
  198. /*
  199. * We cast start/end to known types in the boards file, so cast
  200. * away their pointer types here to the known types (gpios->xxx).
  201. */
  202. state->gpio_count = gpios->end;
  203. state->gpio_addrs = (void *)(unsigned long)gpios->start;
  204. state->gpio_values = (void *)(state + 1);
  205. state->win_size = resource_size(memory);
  206. memset(state->gpio_values, 0xff, arr_size);
  207. state->map.name = DRIVER_NAME;
  208. state->map.read = gf_read;
  209. state->map.copy_from = gf_copy_from;
  210. state->map.write = gf_write;
  211. state->map.copy_to = gf_copy_to;
  212. state->map.bankwidth = pdata->width;
  213. state->map.size = state->win_size * (1 << state->gpio_count);
  214. state->map.virt = ioremap_nocache(memory->start, state->map.size);
  215. state->map.phys = NO_XIP;
  216. state->map.map_priv_1 = (unsigned long)state;
  217. platform_set_drvdata(pdev, state);
  218. i = 0;
  219. do {
  220. if (gpio_request(state->gpio_addrs[i], DRIVER_NAME)) {
  221. pr_devinit(KERN_ERR PFX "failed to request gpio %d\n",
  222. state->gpio_addrs[i]);
  223. while (i--)
  224. gpio_free(state->gpio_addrs[i]);
  225. kfree(state);
  226. return -EBUSY;
  227. }
  228. gpio_direction_output(state->gpio_addrs[i], 0);
  229. } while (++i < state->gpio_count);
  230. pr_devinit(KERN_NOTICE PFX "probing %d-bit flash bus\n",
  231. state->map.bankwidth * 8);
  232. state->mtd = do_map_probe(memory->name, &state->map);
  233. if (!state->mtd) {
  234. for (i = 0; i < state->gpio_count; ++i)
  235. gpio_free(state->gpio_addrs[i]);
  236. kfree(state);
  237. return -ENXIO;
  238. }
  239. state->mtd->dev.parent = &pdev->dev;
  240. mtd_device_parse_register(state->mtd, part_probe_types, NULL,
  241. pdata->parts, pdata->nr_parts);
  242. return 0;
  243. }
  244. static int 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. mtd_device_unregister(state->mtd);
  252. map_destroy(state->mtd);
  253. kfree(state);
  254. return 0;
  255. }
  256. static struct platform_driver gpio_flash_driver = {
  257. .probe = gpio_flash_probe,
  258. .remove = gpio_flash_remove,
  259. .driver = {
  260. .name = DRIVER_NAME,
  261. },
  262. };
  263. module_platform_driver(gpio_flash_driver);
  264. MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>");
  265. MODULE_DESCRIPTION("MTD map driver for flashes addressed physically and with gpios");
  266. MODULE_LICENSE("GPL");