latch-addr-flash.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Interface for NOR flash driver whose high address lines are latched
  3. *
  4. * Copyright © 2000 Nicolas Pitre <nico@cam.org>
  5. * Copyright © 2005-2008 Analog Devices Inc.
  6. * Copyright © 2008 MontaVista Software, Inc. <source@mvista.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public License
  9. * version 2. This program is licensed "as is" without any warranty of any
  10. * kind, whether express or implied.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/mtd/mtd.h>
  16. #include <linux/mtd/map.h>
  17. #include <linux/mtd/partitions.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/mtd/latch-addr-flash.h>
  20. #include <linux/slab.h>
  21. #define DRIVER_NAME "latch-addr-flash"
  22. struct latch_addr_flash_info {
  23. struct mtd_info *mtd;
  24. struct map_info map;
  25. struct resource *res;
  26. void (*set_window)(unsigned long offset, void *data);
  27. void *data;
  28. /* cache; could be found out of res */
  29. unsigned long win_mask;
  30. int nr_parts;
  31. struct mtd_partition *parts;
  32. spinlock_t lock;
  33. };
  34. static map_word lf_read(struct map_info *map, unsigned long ofs)
  35. {
  36. struct latch_addr_flash_info *info;
  37. map_word datum;
  38. info = (struct latch_addr_flash_info *)map->map_priv_1;
  39. spin_lock(&info->lock);
  40. info->set_window(ofs, info->data);
  41. datum = inline_map_read(map, info->win_mask & ofs);
  42. spin_unlock(&info->lock);
  43. return datum;
  44. }
  45. static void lf_write(struct map_info *map, map_word datum, unsigned long ofs)
  46. {
  47. struct latch_addr_flash_info *info;
  48. info = (struct latch_addr_flash_info *)map->map_priv_1;
  49. spin_lock(&info->lock);
  50. info->set_window(ofs, info->data);
  51. inline_map_write(map, datum, info->win_mask & ofs);
  52. spin_unlock(&info->lock);
  53. }
  54. static void lf_copy_from(struct map_info *map, void *to,
  55. unsigned long from, ssize_t len)
  56. {
  57. struct latch_addr_flash_info *info =
  58. (struct latch_addr_flash_info *) map->map_priv_1;
  59. unsigned n;
  60. while (len > 0) {
  61. n = info->win_mask + 1 - (from & info->win_mask);
  62. if (n > len)
  63. n = len;
  64. spin_lock(&info->lock);
  65. info->set_window(from, info->data);
  66. memcpy_fromio(to, map->virt + (from & info->win_mask), n);
  67. spin_unlock(&info->lock);
  68. to += n;
  69. from += n;
  70. len -= n;
  71. }
  72. }
  73. static char *rom_probe_types[] = { "cfi_probe", NULL };
  74. static char *part_probe_types[] = { "cmdlinepart", NULL };
  75. static int latch_addr_flash_remove(struct platform_device *dev)
  76. {
  77. struct latch_addr_flash_info *info;
  78. struct latch_addr_flash_data *latch_addr_data;
  79. info = platform_get_drvdata(dev);
  80. if (info == NULL)
  81. return 0;
  82. platform_set_drvdata(dev, NULL);
  83. latch_addr_data = dev->dev.platform_data;
  84. if (info->mtd != NULL) {
  85. if (mtd_has_partitions()) {
  86. if (info->nr_parts) {
  87. del_mtd_partitions(info->mtd);
  88. kfree(info->parts);
  89. } else if (latch_addr_data->nr_parts) {
  90. del_mtd_partitions(info->mtd);
  91. } else {
  92. del_mtd_device(info->mtd);
  93. }
  94. } else {
  95. del_mtd_device(info->mtd);
  96. }
  97. map_destroy(info->mtd);
  98. }
  99. if (info->map.virt != NULL)
  100. iounmap(info->map.virt);
  101. if (info->res != NULL)
  102. release_mem_region(info->res->start, resource_size(info->res));
  103. kfree(info);
  104. if (latch_addr_data->done)
  105. latch_addr_data->done(latch_addr_data->data);
  106. return 0;
  107. }
  108. static int __devinit latch_addr_flash_probe(struct platform_device *dev)
  109. {
  110. struct latch_addr_flash_data *latch_addr_data;
  111. struct latch_addr_flash_info *info;
  112. resource_size_t win_base = dev->resource->start;
  113. resource_size_t win_size = resource_size(dev->resource);
  114. char **probe_type;
  115. int chipsel;
  116. int err;
  117. latch_addr_data = dev->dev.platform_data;
  118. if (latch_addr_data == NULL)
  119. return -ENODEV;
  120. pr_notice("latch-addr platform flash device: %#llx byte "
  121. "window at %#.8llx\n",
  122. (unsigned long long)win_size, (unsigned long long)win_base);
  123. chipsel = dev->id;
  124. if (latch_addr_data->init) {
  125. err = latch_addr_data->init(latch_addr_data->data, chipsel);
  126. if (err != 0)
  127. return err;
  128. }
  129. info = kzalloc(sizeof(struct latch_addr_flash_info), GFP_KERNEL);
  130. if (info == NULL) {
  131. err = -ENOMEM;
  132. goto done;
  133. }
  134. platform_set_drvdata(dev, info);
  135. info->res = request_mem_region(win_base, win_size, DRIVER_NAME);
  136. if (info->res == NULL) {
  137. dev_err(&dev->dev, "Could not reserve memory region\n");
  138. err = -EBUSY;
  139. goto free_info;
  140. }
  141. info->map.name = DRIVER_NAME;
  142. info->map.size = latch_addr_data->size;
  143. info->map.bankwidth = latch_addr_data->width;
  144. info->map.phys = NO_XIP;
  145. info->map.virt = ioremap(win_base, win_size);
  146. if (!info->map.virt) {
  147. err = -ENOMEM;
  148. goto free_res;
  149. }
  150. info->map.map_priv_1 = (unsigned long)info;
  151. info->map.read = lf_read;
  152. info->map.copy_from = lf_copy_from;
  153. info->map.write = lf_write;
  154. info->set_window = latch_addr_data->set_window;
  155. info->data = latch_addr_data->data;
  156. info->win_mask = win_size - 1;
  157. spin_lock_init(&info->lock);
  158. for (probe_type = rom_probe_types; !info->mtd && *probe_type;
  159. probe_type++)
  160. info->mtd = do_map_probe(*probe_type, &info->map);
  161. if (info->mtd == NULL) {
  162. dev_err(&dev->dev, "map_probe failed\n");
  163. err = -ENODEV;
  164. goto iounmap;
  165. }
  166. info->mtd->owner = THIS_MODULE;
  167. if (mtd_has_partitions()) {
  168. err = parse_mtd_partitions(info->mtd,
  169. (const char **)part_probe_types,
  170. &info->parts, 0);
  171. if (err > 0) {
  172. add_mtd_partitions(info->mtd, info->parts, err);
  173. return 0;
  174. }
  175. if (latch_addr_data->nr_parts) {
  176. pr_notice("Using latch-addr-flash partition information\n");
  177. add_mtd_partitions(info->mtd, latch_addr_data->parts,
  178. latch_addr_data->nr_parts);
  179. return 0;
  180. }
  181. }
  182. add_mtd_device(info->mtd);
  183. return 0;
  184. iounmap:
  185. iounmap(info->map.virt);
  186. free_res:
  187. release_mem_region(info->res->start, resource_size(info->res));
  188. free_info:
  189. kfree(info);
  190. done:
  191. if (latch_addr_data->done)
  192. latch_addr_data->done(latch_addr_data->data);
  193. return err;
  194. }
  195. static struct platform_driver latch_addr_flash_driver = {
  196. .probe = latch_addr_flash_probe,
  197. .remove = __devexit_p(latch_addr_flash_remove),
  198. .driver = {
  199. .name = DRIVER_NAME,
  200. },
  201. };
  202. static int __init latch_addr_flash_init(void)
  203. {
  204. return platform_driver_register(&latch_addr_flash_driver);
  205. }
  206. module_init(latch_addr_flash_init);
  207. static void __exit latch_addr_flash_exit(void)
  208. {
  209. platform_driver_unregister(&latch_addr_flash_driver);
  210. }
  211. module_exit(latch_addr_flash_exit);
  212. MODULE_AUTHOR("David Griego <dgriego@mvista.com>");
  213. MODULE_DESCRIPTION("MTD map driver for flashes addressed physically with upper "
  214. "address lines being set board specifically");
  215. MODULE_LICENSE("GPL v2");