latch-addr-flash.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 (info->nr_parts)
  86. kfree(info->parts);
  87. mtd_device_unregister(info->mtd);
  88. map_destroy(info->mtd);
  89. }
  90. if (info->map.virt != NULL)
  91. iounmap(info->map.virt);
  92. if (info->res != NULL)
  93. release_mem_region(info->res->start, resource_size(info->res));
  94. kfree(info);
  95. if (latch_addr_data->done)
  96. latch_addr_data->done(latch_addr_data->data);
  97. return 0;
  98. }
  99. static int __devinit latch_addr_flash_probe(struct platform_device *dev)
  100. {
  101. struct latch_addr_flash_data *latch_addr_data;
  102. struct latch_addr_flash_info *info;
  103. resource_size_t win_base = dev->resource->start;
  104. resource_size_t win_size = resource_size(dev->resource);
  105. char **probe_type;
  106. int chipsel;
  107. int err;
  108. latch_addr_data = dev->dev.platform_data;
  109. if (latch_addr_data == NULL)
  110. return -ENODEV;
  111. pr_notice("latch-addr platform flash device: %#llx byte "
  112. "window at %#.8llx\n",
  113. (unsigned long long)win_size, (unsigned long long)win_base);
  114. chipsel = dev->id;
  115. if (latch_addr_data->init) {
  116. err = latch_addr_data->init(latch_addr_data->data, chipsel);
  117. if (err != 0)
  118. return err;
  119. }
  120. info = kzalloc(sizeof(struct latch_addr_flash_info), GFP_KERNEL);
  121. if (info == NULL) {
  122. err = -ENOMEM;
  123. goto done;
  124. }
  125. platform_set_drvdata(dev, info);
  126. info->res = request_mem_region(win_base, win_size, DRIVER_NAME);
  127. if (info->res == NULL) {
  128. dev_err(&dev->dev, "Could not reserve memory region\n");
  129. err = -EBUSY;
  130. goto free_info;
  131. }
  132. info->map.name = DRIVER_NAME;
  133. info->map.size = latch_addr_data->size;
  134. info->map.bankwidth = latch_addr_data->width;
  135. info->map.phys = NO_XIP;
  136. info->map.virt = ioremap(win_base, win_size);
  137. if (!info->map.virt) {
  138. err = -ENOMEM;
  139. goto free_res;
  140. }
  141. info->map.map_priv_1 = (unsigned long)info;
  142. info->map.read = lf_read;
  143. info->map.copy_from = lf_copy_from;
  144. info->map.write = lf_write;
  145. info->set_window = latch_addr_data->set_window;
  146. info->data = latch_addr_data->data;
  147. info->win_mask = win_size - 1;
  148. spin_lock_init(&info->lock);
  149. for (probe_type = rom_probe_types; !info->mtd && *probe_type;
  150. probe_type++)
  151. info->mtd = do_map_probe(*probe_type, &info->map);
  152. if (info->mtd == NULL) {
  153. dev_err(&dev->dev, "map_probe failed\n");
  154. err = -ENODEV;
  155. goto iounmap;
  156. }
  157. info->mtd->owner = THIS_MODULE;
  158. err = parse_mtd_partitions(info->mtd, (const char **)part_probe_types,
  159. &info->parts, 0);
  160. if (err > 0) {
  161. mtd_device_register(info->mtd, info->parts, err);
  162. return 0;
  163. }
  164. if (latch_addr_data->nr_parts) {
  165. pr_notice("Using latch-addr-flash partition information\n");
  166. mtd_device_register(info->mtd,
  167. latch_addr_data->parts,
  168. latch_addr_data->nr_parts);
  169. return 0;
  170. }
  171. mtd_device_register(info->mtd, NULL, 0);
  172. return 0;
  173. iounmap:
  174. iounmap(info->map.virt);
  175. free_res:
  176. release_mem_region(info->res->start, resource_size(info->res));
  177. free_info:
  178. kfree(info);
  179. done:
  180. if (latch_addr_data->done)
  181. latch_addr_data->done(latch_addr_data->data);
  182. return err;
  183. }
  184. static struct platform_driver latch_addr_flash_driver = {
  185. .probe = latch_addr_flash_probe,
  186. .remove = __devexit_p(latch_addr_flash_remove),
  187. .driver = {
  188. .name = DRIVER_NAME,
  189. },
  190. };
  191. static int __init latch_addr_flash_init(void)
  192. {
  193. return platform_driver_register(&latch_addr_flash_driver);
  194. }
  195. module_init(latch_addr_flash_init);
  196. static void __exit latch_addr_flash_exit(void)
  197. {
  198. platform_driver_unregister(&latch_addr_flash_driver);
  199. }
  200. module_exit(latch_addr_flash_exit);
  201. MODULE_AUTHOR("David Griego <dgriego@mvista.com>");
  202. MODULE_DESCRIPTION("MTD map driver for flashes addressed physically with upper "
  203. "address lines being set board specifically");
  204. MODULE_LICENSE("GPL v2");