integrator-flash.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. struct armflash_subdev_info {
  35. char *name;
  36. struct mtd_info *mtd;
  37. struct map_info map;
  38. struct flash_platform_data *plat;
  39. };
  40. struct armflash_info {
  41. struct resource *res;
  42. struct mtd_partition *parts;
  43. struct mtd_info *mtd;
  44. int nr_subdev;
  45. struct armflash_subdev_info subdev[0];
  46. };
  47. static void armflash_set_vpp(struct map_info *map, int on)
  48. {
  49. struct armflash_subdev_info *info =
  50. container_of(map, struct armflash_subdev_info, map);
  51. if (info->plat && info->plat->set_vpp)
  52. info->plat->set_vpp(on);
  53. }
  54. static const char *probes[] = { "cmdlinepart", "RedBoot", "afs", NULL };
  55. static int armflash_subdev_probe(struct armflash_subdev_info *subdev,
  56. struct resource *res)
  57. {
  58. struct flash_platform_data *plat = subdev->plat;
  59. resource_size_t size = res->end - res->start + 1;
  60. void __iomem *base;
  61. int err = 0;
  62. if (!request_mem_region(res->start, size, subdev->name)) {
  63. err = -EBUSY;
  64. goto out;
  65. }
  66. base = ioremap(res->start, size);
  67. if (!base) {
  68. err = -ENOMEM;
  69. goto no_mem;
  70. }
  71. /*
  72. * look for CFI based flash parts fitted to this board
  73. */
  74. subdev->map.size = size;
  75. subdev->map.bankwidth = plat->width;
  76. subdev->map.phys = res->start;
  77. subdev->map.virt = base;
  78. subdev->map.name = subdev->name;
  79. subdev->map.set_vpp = armflash_set_vpp;
  80. simple_map_init(&subdev->map);
  81. /*
  82. * Also, the CFI layer automatically works out what size
  83. * of chips we have, and does the necessary identification
  84. * for us automatically.
  85. */
  86. subdev->mtd = do_map_probe(plat->map_name, &subdev->map);
  87. if (!subdev->mtd) {
  88. err = -ENXIO;
  89. goto no_device;
  90. }
  91. subdev->mtd->owner = THIS_MODULE;
  92. /* Successful? */
  93. if (err == 0)
  94. return err;
  95. if (subdev->mtd)
  96. map_destroy(subdev->mtd);
  97. no_device:
  98. iounmap(base);
  99. no_mem:
  100. release_mem_region(res->start, size);
  101. out:
  102. return err;
  103. }
  104. static void armflash_subdev_remove(struct armflash_subdev_info *subdev)
  105. {
  106. if (subdev->mtd)
  107. map_destroy(subdev->mtd);
  108. if (subdev->map.virt)
  109. iounmap(subdev->map.virt);
  110. kfree(subdev->name);
  111. subdev->name = NULL;
  112. release_mem_region(subdev->map.phys, subdev->map.size);
  113. }
  114. static int armflash_probe(struct platform_device *dev)
  115. {
  116. struct flash_platform_data *plat = dev->dev.platform_data;
  117. unsigned int size;
  118. struct armflash_info *info;
  119. int i, nr, err;
  120. /* Count the number of devices */
  121. for (nr = 0; ; nr++)
  122. if (!platform_get_resource(dev, IORESOURCE_MEM, nr))
  123. break;
  124. if (nr == 0) {
  125. err = -ENODEV;
  126. goto out;
  127. }
  128. size = sizeof(struct armflash_info) +
  129. sizeof(struct armflash_subdev_info) * nr;
  130. info = kzalloc(size, GFP_KERNEL);
  131. if (!info) {
  132. err = -ENOMEM;
  133. goto out;
  134. }
  135. if (plat && plat->init) {
  136. err = plat->init();
  137. if (err)
  138. goto no_resource;
  139. }
  140. for (i = 0; i < nr; i++) {
  141. struct armflash_subdev_info *subdev = &info->subdev[i];
  142. struct resource *res;
  143. res = platform_get_resource(dev, IORESOURCE_MEM, i);
  144. if (!res)
  145. break;
  146. if (nr == 1)
  147. /* No MTD concatenation, just use the default name */
  148. subdev->name = kstrdup(dev_name(&dev->dev), GFP_KERNEL);
  149. else
  150. subdev->name = kasprintf(GFP_KERNEL, "%s-%d",
  151. dev_name(&dev->dev), i);
  152. if (!subdev->name) {
  153. err = -ENOMEM;
  154. break;
  155. }
  156. subdev->plat = plat;
  157. err = armflash_subdev_probe(subdev, res);
  158. if (err) {
  159. kfree(subdev->name);
  160. subdev->name = NULL;
  161. break;
  162. }
  163. }
  164. info->nr_subdev = i;
  165. if (err)
  166. goto subdev_err;
  167. if (info->nr_subdev == 1)
  168. info->mtd = info->subdev[0].mtd;
  169. else if (info->nr_subdev > 1) {
  170. #ifdef CONFIG_MTD_CONCAT
  171. struct mtd_info *cdev[info->nr_subdev];
  172. /*
  173. * We detected multiple devices. Concatenate them together.
  174. */
  175. for (i = 0; i < info->nr_subdev; i++)
  176. cdev[i] = info->subdev[i].mtd;
  177. info->mtd = mtd_concat_create(cdev, info->nr_subdev,
  178. dev_name(&dev->dev));
  179. if (info->mtd == NULL)
  180. err = -ENXIO;
  181. #else
  182. printk(KERN_ERR "armflash: multiple devices found but "
  183. "MTD concat support disabled.\n");
  184. err = -ENXIO;
  185. #endif
  186. }
  187. if (err < 0)
  188. goto cleanup;
  189. err = parse_mtd_partitions(info->mtd, probes, &info->parts, 0);
  190. if (err > 0) {
  191. err = add_mtd_partitions(info->mtd, info->parts, err);
  192. if (err)
  193. printk(KERN_ERR
  194. "mtd partition registration failed: %d\n", err);
  195. }
  196. if (err == 0) {
  197. platform_set_drvdata(dev, info);
  198. return err;
  199. }
  200. /*
  201. * We got an error, free all resources.
  202. */
  203. cleanup:
  204. if (info->mtd) {
  205. del_mtd_partitions(info->mtd);
  206. #ifdef CONFIG_MTD_CONCAT
  207. if (info->mtd != info->subdev[0].mtd)
  208. mtd_concat_destroy(info->mtd);
  209. #endif
  210. }
  211. kfree(info->parts);
  212. subdev_err:
  213. for (i = info->nr_subdev - 1; i >= 0; i--)
  214. armflash_subdev_remove(&info->subdev[i]);
  215. no_resource:
  216. if (plat && plat->exit)
  217. plat->exit();
  218. kfree(info);
  219. out:
  220. return err;
  221. }
  222. static int armflash_remove(struct platform_device *dev)
  223. {
  224. struct armflash_info *info = platform_get_drvdata(dev);
  225. struct flash_platform_data *plat = dev->dev.platform_data;
  226. int i;
  227. platform_set_drvdata(dev, NULL);
  228. if (info) {
  229. if (info->mtd) {
  230. del_mtd_partitions(info->mtd);
  231. #ifdef CONFIG_MTD_CONCAT
  232. if (info->mtd != info->subdev[0].mtd)
  233. mtd_concat_destroy(info->mtd);
  234. #endif
  235. }
  236. kfree(info->parts);
  237. for (i = info->nr_subdev - 1; i >= 0; i--)
  238. armflash_subdev_remove(&info->subdev[i]);
  239. if (plat && plat->exit)
  240. plat->exit();
  241. kfree(info);
  242. }
  243. return 0;
  244. }
  245. static struct platform_driver armflash_driver = {
  246. .probe = armflash_probe,
  247. .remove = armflash_remove,
  248. .driver = {
  249. .name = "armflash",
  250. .owner = THIS_MODULE,
  251. },
  252. };
  253. static int __init armflash_init(void)
  254. {
  255. return platform_driver_register(&armflash_driver);
  256. }
  257. static void __exit armflash_exit(void)
  258. {
  259. platform_driver_unregister(&armflash_driver);
  260. }
  261. module_init(armflash_init);
  262. module_exit(armflash_exit);
  263. MODULE_AUTHOR("ARM Ltd");
  264. MODULE_DESCRIPTION("ARM Integrator CFI map driver");
  265. MODULE_LICENSE("GPL");
  266. MODULE_ALIAS("platform:armflash");