integrator-flash.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. struct mtd_info *cdev[info->nr_subdev];
  171. /*
  172. * We detected multiple devices. Concatenate them together.
  173. */
  174. for (i = 0; i < info->nr_subdev; i++)
  175. cdev[i] = info->subdev[i].mtd;
  176. info->mtd = mtd_concat_create(cdev, info->nr_subdev,
  177. dev_name(&dev->dev));
  178. if (info->mtd == NULL)
  179. err = -ENXIO;
  180. }
  181. if (err < 0)
  182. goto cleanup;
  183. err = parse_mtd_partitions(info->mtd, probes, &info->parts, 0);
  184. if (err > 0) {
  185. err = add_mtd_partitions(info->mtd, info->parts, err);
  186. if (err)
  187. printk(KERN_ERR
  188. "mtd partition registration failed: %d\n", err);
  189. }
  190. if (err == 0) {
  191. platform_set_drvdata(dev, info);
  192. return err;
  193. }
  194. /*
  195. * We got an error, free all resources.
  196. */
  197. cleanup:
  198. if (info->mtd) {
  199. del_mtd_partitions(info->mtd);
  200. if (info->mtd != info->subdev[0].mtd)
  201. mtd_concat_destroy(info->mtd);
  202. }
  203. kfree(info->parts);
  204. subdev_err:
  205. for (i = info->nr_subdev - 1; i >= 0; i--)
  206. armflash_subdev_remove(&info->subdev[i]);
  207. no_resource:
  208. if (plat && plat->exit)
  209. plat->exit();
  210. kfree(info);
  211. out:
  212. return err;
  213. }
  214. static int armflash_remove(struct platform_device *dev)
  215. {
  216. struct armflash_info *info = platform_get_drvdata(dev);
  217. struct flash_platform_data *plat = dev->dev.platform_data;
  218. int i;
  219. platform_set_drvdata(dev, NULL);
  220. if (info) {
  221. if (info->mtd) {
  222. del_mtd_partitions(info->mtd);
  223. if (info->mtd != info->subdev[0].mtd)
  224. mtd_concat_destroy(info->mtd);
  225. }
  226. kfree(info->parts);
  227. for (i = info->nr_subdev - 1; i >= 0; i--)
  228. armflash_subdev_remove(&info->subdev[i]);
  229. if (plat && plat->exit)
  230. plat->exit();
  231. kfree(info);
  232. }
  233. return 0;
  234. }
  235. static struct platform_driver armflash_driver = {
  236. .probe = armflash_probe,
  237. .remove = armflash_remove,
  238. .driver = {
  239. .name = "armflash",
  240. .owner = THIS_MODULE,
  241. },
  242. };
  243. static int __init armflash_init(void)
  244. {
  245. return platform_driver_register(&armflash_driver);
  246. }
  247. static void __exit armflash_exit(void)
  248. {
  249. platform_driver_unregister(&armflash_driver);
  250. }
  251. module_init(armflash_init);
  252. module_exit(armflash_exit);
  253. MODULE_AUTHOR("ARM Ltd");
  254. MODULE_DESCRIPTION("ARM Integrator CFI map driver");
  255. MODULE_LICENSE("GPL");
  256. MODULE_ALIAS("platform:armflash");