latch-addr-flash.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. spinlock_t lock;
  31. };
  32. static map_word lf_read(struct map_info *map, unsigned long ofs)
  33. {
  34. struct latch_addr_flash_info *info;
  35. map_word datum;
  36. info = (struct latch_addr_flash_info *)map->map_priv_1;
  37. spin_lock(&info->lock);
  38. info->set_window(ofs, info->data);
  39. datum = inline_map_read(map, info->win_mask & ofs);
  40. spin_unlock(&info->lock);
  41. return datum;
  42. }
  43. static void lf_write(struct map_info *map, map_word datum, unsigned long ofs)
  44. {
  45. struct latch_addr_flash_info *info;
  46. info = (struct latch_addr_flash_info *)map->map_priv_1;
  47. spin_lock(&info->lock);
  48. info->set_window(ofs, info->data);
  49. inline_map_write(map, datum, info->win_mask & ofs);
  50. spin_unlock(&info->lock);
  51. }
  52. static void lf_copy_from(struct map_info *map, void *to,
  53. unsigned long from, ssize_t len)
  54. {
  55. struct latch_addr_flash_info *info =
  56. (struct latch_addr_flash_info *) map->map_priv_1;
  57. unsigned n;
  58. while (len > 0) {
  59. n = info->win_mask + 1 - (from & info->win_mask);
  60. if (n > len)
  61. n = len;
  62. spin_lock(&info->lock);
  63. info->set_window(from, info->data);
  64. memcpy_fromio(to, map->virt + (from & info->win_mask), n);
  65. spin_unlock(&info->lock);
  66. to += n;
  67. from += n;
  68. len -= n;
  69. }
  70. }
  71. static char *rom_probe_types[] = { "cfi_probe", NULL };
  72. static int latch_addr_flash_remove(struct platform_device *dev)
  73. {
  74. struct latch_addr_flash_info *info;
  75. struct latch_addr_flash_data *latch_addr_data;
  76. info = platform_get_drvdata(dev);
  77. if (info == NULL)
  78. return 0;
  79. platform_set_drvdata(dev, NULL);
  80. latch_addr_data = dev->dev.platform_data;
  81. if (info->mtd != NULL) {
  82. mtd_device_unregister(info->mtd);
  83. map_destroy(info->mtd);
  84. }
  85. if (info->map.virt != NULL)
  86. iounmap(info->map.virt);
  87. if (info->res != NULL)
  88. release_mem_region(info->res->start, resource_size(info->res));
  89. kfree(info);
  90. if (latch_addr_data->done)
  91. latch_addr_data->done(latch_addr_data->data);
  92. return 0;
  93. }
  94. static int __devinit latch_addr_flash_probe(struct platform_device *dev)
  95. {
  96. struct latch_addr_flash_data *latch_addr_data;
  97. struct latch_addr_flash_info *info;
  98. resource_size_t win_base = dev->resource->start;
  99. resource_size_t win_size = resource_size(dev->resource);
  100. char **probe_type;
  101. int chipsel;
  102. int err;
  103. latch_addr_data = dev->dev.platform_data;
  104. if (latch_addr_data == NULL)
  105. return -ENODEV;
  106. pr_notice("latch-addr platform flash device: %#llx byte "
  107. "window at %#.8llx\n",
  108. (unsigned long long)win_size, (unsigned long long)win_base);
  109. chipsel = dev->id;
  110. if (latch_addr_data->init) {
  111. err = latch_addr_data->init(latch_addr_data->data, chipsel);
  112. if (err != 0)
  113. return err;
  114. }
  115. info = kzalloc(sizeof(struct latch_addr_flash_info), GFP_KERNEL);
  116. if (info == NULL) {
  117. err = -ENOMEM;
  118. goto done;
  119. }
  120. platform_set_drvdata(dev, info);
  121. info->res = request_mem_region(win_base, win_size, DRIVER_NAME);
  122. if (info->res == NULL) {
  123. dev_err(&dev->dev, "Could not reserve memory region\n");
  124. err = -EBUSY;
  125. goto free_info;
  126. }
  127. info->map.name = DRIVER_NAME;
  128. info->map.size = latch_addr_data->size;
  129. info->map.bankwidth = latch_addr_data->width;
  130. info->map.phys = NO_XIP;
  131. info->map.virt = ioremap(win_base, win_size);
  132. if (!info->map.virt) {
  133. err = -ENOMEM;
  134. goto free_res;
  135. }
  136. info->map.map_priv_1 = (unsigned long)info;
  137. info->map.read = lf_read;
  138. info->map.copy_from = lf_copy_from;
  139. info->map.write = lf_write;
  140. info->set_window = latch_addr_data->set_window;
  141. info->data = latch_addr_data->data;
  142. info->win_mask = win_size - 1;
  143. spin_lock_init(&info->lock);
  144. for (probe_type = rom_probe_types; !info->mtd && *probe_type;
  145. probe_type++)
  146. info->mtd = do_map_probe(*probe_type, &info->map);
  147. if (info->mtd == NULL) {
  148. dev_err(&dev->dev, "map_probe failed\n");
  149. err = -ENODEV;
  150. goto iounmap;
  151. }
  152. info->mtd->owner = THIS_MODULE;
  153. mtd_device_parse_register(info->mtd, NULL, NULL,
  154. latch_addr_data->parts,
  155. latch_addr_data->nr_parts);
  156. return 0;
  157. iounmap:
  158. iounmap(info->map.virt);
  159. free_res:
  160. release_mem_region(info->res->start, resource_size(info->res));
  161. free_info:
  162. kfree(info);
  163. done:
  164. if (latch_addr_data->done)
  165. latch_addr_data->done(latch_addr_data->data);
  166. return err;
  167. }
  168. static struct platform_driver latch_addr_flash_driver = {
  169. .probe = latch_addr_flash_probe,
  170. .remove = __devexit_p(latch_addr_flash_remove),
  171. .driver = {
  172. .name = DRIVER_NAME,
  173. },
  174. };
  175. module_platform_driver(latch_addr_flash_driver);
  176. MODULE_AUTHOR("David Griego <dgriego@mvista.com>");
  177. MODULE_DESCRIPTION("MTD map driver for flashes addressed physically with upper "
  178. "address lines being set board specifically");
  179. MODULE_LICENSE("GPL v2");