integrator-flash.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*======================================================================
  2. drivers/mtd/maps/integrator-flash.c: ARM Integrator flash map driver
  3. Copyright (C) 2000 ARM Limited
  4. Copyright (C) 2003 Deep Blue Solutions Ltd.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. This is access code for flashes using ARM's flash partitioning
  17. standards.
  18. ======================================================================*/
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/slab.h>
  23. #include <linux/ioport.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/init.h>
  26. #include <linux/io.h>
  27. #include <linux/mtd/mtd.h>
  28. #include <linux/mtd/map.h>
  29. #include <linux/mtd/partitions.h>
  30. #include <linux/mtd/concat.h>
  31. #include <asm/mach/flash.h>
  32. #include <mach/hardware.h>
  33. #include <asm/system.h>
  34. #define SUBDEV_NAME_SIZE (BUS_ID_SIZE + 2)
  35. struct armflash_subdev_info {
  36. char name[SUBDEV_NAME_SIZE];
  37. struct mtd_info *mtd;
  38. struct map_info map;
  39. struct flash_platform_data *plat;
  40. };
  41. struct armflash_info {
  42. struct resource *res;
  43. struct mtd_partition *parts;
  44. struct mtd_info *mtd;
  45. int nr_subdev;
  46. struct armflash_subdev_info subdev[0];
  47. };
  48. static void armflash_set_vpp(struct map_info *map, int on)
  49. {
  50. struct armflash_subdev_info *info =
  51. container_of(map, struct armflash_subdev_info, map);
  52. if (info->plat && info->plat->set_vpp)
  53. info->plat->set_vpp(on);
  54. }
  55. static const char *probes[] = { "cmdlinepart", "RedBoot", "afs", NULL };
  56. static int armflash_subdev_probe(struct armflash_subdev_info *subdev,
  57. struct resource *res)
  58. {
  59. struct flash_platform_data *plat = subdev->plat;
  60. resource_size_t size = res->end - res->start + 1;
  61. void __iomem *base;
  62. int err = 0;
  63. if (!request_mem_region(res->start, size, subdev->name)) {
  64. err = -EBUSY;
  65. goto out;
  66. }
  67. base = ioremap(res->start, size);
  68. if (!base) {
  69. err = -ENOMEM;
  70. goto no_mem;
  71. }
  72. /*
  73. * look for CFI based flash parts fitted to this board
  74. */
  75. subdev->map.size = size;
  76. subdev->map.bankwidth = plat->width;
  77. subdev->map.phys = res->start;
  78. subdev->map.virt = base;
  79. subdev->map.name = subdev->name;
  80. subdev->map.set_vpp = armflash_set_vpp;
  81. simple_map_init(&subdev->map);
  82. /*
  83. * Also, the CFI layer automatically works out what size
  84. * of chips we have, and does the necessary identification
  85. * for us automatically.
  86. */
  87. subdev->mtd = do_map_probe(plat->map_name, &subdev->map);
  88. if (!subdev->mtd) {
  89. err = -ENXIO;
  90. goto no_device;
  91. }
  92. subdev->mtd->owner = THIS_MODULE;
  93. /* Successful? */
  94. if (err == 0)
  95. return err;
  96. if (subdev->mtd)
  97. map_destroy(subdev->mtd);
  98. no_device:
  99. iounmap(base);
  100. no_mem:
  101. release_mem_region(res->start, size);
  102. out:
  103. return err;
  104. }
  105. static void armflash_subdev_remove(struct armflash_subdev_info *subdev)
  106. {
  107. if (subdev->mtd)
  108. map_destroy(subdev->mtd);
  109. if (subdev->map.virt)
  110. iounmap(subdev->map.virt);
  111. release_mem_region(subdev->map.phys, subdev->map.size);
  112. }
  113. static int armflash_probe(struct platform_device *dev)
  114. {
  115. struct flash_platform_data *plat = dev->dev.platform_data;
  116. unsigned int size;
  117. struct armflash_info *info;
  118. int i, nr, err;
  119. /* Count the number of devices */
  120. for (nr = 0; ; nr++)
  121. if (!platform_get_resource(dev, IORESOURCE_MEM, nr))
  122. break;
  123. if (nr == 0) {
  124. err = -ENODEV;
  125. goto out;
  126. }
  127. size = sizeof(struct armflash_info) +
  128. sizeof(struct armflash_subdev_info) * nr;
  129. info = kzalloc(size, GFP_KERNEL);
  130. if (!info) {
  131. err = -ENOMEM;
  132. goto out;
  133. }
  134. if (plat && plat->init) {
  135. err = plat->init();
  136. if (err)
  137. goto no_resource;
  138. }
  139. for (i = 0; i < nr; i++) {
  140. struct armflash_subdev_info *subdev = &info->subdev[i];
  141. struct resource *res;
  142. res = platform_get_resource(dev, IORESOURCE_MEM, i);
  143. if (!res)
  144. break;
  145. if (nr == 1)
  146. /* No MTD concatenation, just use the default name */
  147. snprintf(subdev->name, SUBDEV_NAME_SIZE, "%s",
  148. dev_name(&dev->dev));
  149. else
  150. snprintf(subdev->name, SUBDEV_NAME_SIZE, "%s-%d",
  151. dev_name(&dev->dev), i);
  152. subdev->plat = plat;
  153. err = armflash_subdev_probe(subdev, res);
  154. if (err)
  155. break;
  156. }
  157. info->nr_subdev = i;
  158. if (err)
  159. goto subdev_err;
  160. if (info->nr_subdev == 1)
  161. info->mtd = info->subdev[0].mtd;
  162. else if (info->nr_subdev > 1) {
  163. #ifdef CONFIG_MTD_CONCAT
  164. struct mtd_info *cdev[info->nr_subdev];
  165. /*
  166. * We detected multiple devices. Concatenate them together.
  167. */
  168. for (i = 0; i < info->nr_subdev; i++)
  169. cdev[i] = info->subdev[i].mtd;
  170. info->mtd = mtd_concat_create(cdev, info->nr_subdev,
  171. dev_name(&dev->dev));
  172. if (info->mtd == NULL)
  173. err = -ENXIO;
  174. #else
  175. printk(KERN_ERR "armflash: multiple devices found but "
  176. "MTD concat support disabled.\n");
  177. err = -ENXIO;
  178. #endif
  179. }
  180. if (err < 0)
  181. goto cleanup;
  182. err = parse_mtd_partitions(info->mtd, probes, &info->parts, 0);
  183. if (err > 0) {
  184. err = add_mtd_partitions(info->mtd, info->parts, err);
  185. if (err)
  186. printk(KERN_ERR
  187. "mtd partition registration failed: %d\n", err);
  188. }
  189. if (err == 0) {
  190. platform_set_drvdata(dev, info);
  191. return err;
  192. }
  193. /*
  194. * We got an error, free all resources.
  195. */
  196. cleanup:
  197. if (info->mtd) {
  198. del_mtd_partitions(info->mtd);
  199. #ifdef CONFIG_MTD_CONCAT
  200. if (info->mtd != info->subdev[0].mtd)
  201. mtd_concat_destroy(info->mtd);
  202. #endif
  203. }
  204. kfree(info->parts);
  205. subdev_err:
  206. for (i = info->nr_subdev - 1; i >= 0; i--)
  207. armflash_subdev_remove(&info->subdev[i]);
  208. no_resource:
  209. if (plat && plat->exit)
  210. plat->exit();
  211. kfree(info);
  212. out:
  213. return err;
  214. }
  215. static int armflash_remove(struct platform_device *dev)
  216. {
  217. struct armflash_info *info = platform_get_drvdata(dev);
  218. struct flash_platform_data *plat = dev->dev.platform_data;
  219. int i;
  220. platform_set_drvdata(dev, NULL);
  221. if (info) {
  222. if (info->mtd) {
  223. del_mtd_partitions(info->mtd);
  224. #ifdef CONFIG_MTD_CONCAT
  225. if (info->mtd != info->subdev[0].mtd)
  226. mtd_concat_destroy(info->mtd);
  227. #endif
  228. }
  229. kfree(info->parts);
  230. for (i = info->nr_subdev - 1; i >= 0; i--)
  231. armflash_subdev_remove(&info->subdev[i]);
  232. if (plat && plat->exit)
  233. plat->exit();
  234. kfree(info);
  235. }
  236. return 0;
  237. }
  238. static struct platform_driver armflash_driver = {
  239. .probe = armflash_probe,
  240. .remove = armflash_remove,
  241. .driver = {
  242. .name = "armflash",
  243. .owner = THIS_MODULE,
  244. },
  245. };
  246. static int __init armflash_init(void)
  247. {
  248. return platform_driver_register(&armflash_driver);
  249. }
  250. static void __exit armflash_exit(void)
  251. {
  252. platform_driver_unregister(&armflash_driver);
  253. }
  254. module_init(armflash_init);
  255. module_exit(armflash_exit);
  256. MODULE_AUTHOR("ARM Ltd");
  257. MODULE_DESCRIPTION("ARM Integrator CFI map driver");
  258. MODULE_LICENSE("GPL");
  259. MODULE_ALIAS("platform:armflash");