latch-addr-flash.c 5.3 KB

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