sa1100-flash.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Flash memory access on SA11x0 based devices
  3. *
  4. * (C) 2000 Nicolas Pitre <nico@fluxnic.net>
  5. */
  6. #include <linux/module.h>
  7. #include <linux/types.h>
  8. #include <linux/ioport.h>
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/errno.h>
  12. #include <linux/slab.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/err.h>
  15. #include <linux/io.h>
  16. #include <linux/mtd/mtd.h>
  17. #include <linux/mtd/map.h>
  18. #include <linux/mtd/partitions.h>
  19. #include <linux/mtd/concat.h>
  20. #include <mach/hardware.h>
  21. #include <asm/sizes.h>
  22. #include <asm/mach/flash.h>
  23. struct sa_subdev_info {
  24. char name[16];
  25. struct map_info map;
  26. struct mtd_info *mtd;
  27. struct flash_platform_data *plat;
  28. };
  29. struct sa_info {
  30. struct mtd_info *mtd;
  31. int num_subdev;
  32. struct sa_subdev_info subdev[0];
  33. };
  34. static void sa1100_set_vpp(struct map_info *map, int on)
  35. {
  36. struct sa_subdev_info *subdev = container_of(map, struct sa_subdev_info, map);
  37. subdev->plat->set_vpp(on);
  38. }
  39. static void sa1100_destroy_subdev(struct sa_subdev_info *subdev)
  40. {
  41. if (subdev->mtd)
  42. map_destroy(subdev->mtd);
  43. if (subdev->map.virt)
  44. iounmap(subdev->map.virt);
  45. release_mem_region(subdev->map.phys, subdev->map.size);
  46. }
  47. static int sa1100_probe_subdev(struct sa_subdev_info *subdev, struct resource *res)
  48. {
  49. unsigned long phys;
  50. unsigned int size;
  51. int ret;
  52. phys = res->start;
  53. size = res->end - phys + 1;
  54. /*
  55. * Retrieve the bankwidth from the MSC registers.
  56. * We currently only implement CS0 and CS1 here.
  57. */
  58. switch (phys) {
  59. default:
  60. printk(KERN_WARNING "SA1100 flash: unknown base address "
  61. "0x%08lx, assuming CS0\n", phys);
  62. case SA1100_CS0_PHYS:
  63. subdev->map.bankwidth = (MSC0 & MSC_RBW) ? 2 : 4;
  64. break;
  65. case SA1100_CS1_PHYS:
  66. subdev->map.bankwidth = ((MSC0 >> 16) & MSC_RBW) ? 2 : 4;
  67. break;
  68. }
  69. if (!request_mem_region(phys, size, subdev->name)) {
  70. ret = -EBUSY;
  71. goto out;
  72. }
  73. if (subdev->plat->set_vpp)
  74. subdev->map.set_vpp = sa1100_set_vpp;
  75. subdev->map.phys = phys;
  76. subdev->map.size = size;
  77. subdev->map.virt = ioremap(phys, size);
  78. if (!subdev->map.virt) {
  79. ret = -ENOMEM;
  80. goto err;
  81. }
  82. simple_map_init(&subdev->map);
  83. /*
  84. * Now let's probe for the actual flash. Do it here since
  85. * specific machine settings might have been set above.
  86. */
  87. subdev->mtd = do_map_probe(subdev->plat->map_name, &subdev->map);
  88. if (subdev->mtd == NULL) {
  89. ret = -ENXIO;
  90. goto err;
  91. }
  92. subdev->mtd->owner = THIS_MODULE;
  93. printk(KERN_INFO "SA1100 flash: CFI device at 0x%08lx, %uMiB, %d-bit\n",
  94. phys, (unsigned)(subdev->mtd->size >> 20),
  95. subdev->map.bankwidth * 8);
  96. return 0;
  97. err:
  98. sa1100_destroy_subdev(subdev);
  99. out:
  100. return ret;
  101. }
  102. static void sa1100_destroy(struct sa_info *info, struct flash_platform_data *plat)
  103. {
  104. int i;
  105. if (info->mtd) {
  106. mtd_device_unregister(info->mtd);
  107. if (info->mtd != info->subdev[0].mtd)
  108. mtd_concat_destroy(info->mtd);
  109. }
  110. for (i = info->num_subdev - 1; i >= 0; i--)
  111. sa1100_destroy_subdev(&info->subdev[i]);
  112. kfree(info);
  113. if (plat->exit)
  114. plat->exit();
  115. }
  116. static struct sa_info *__devinit
  117. sa1100_setup_mtd(struct platform_device *pdev, struct flash_platform_data *plat)
  118. {
  119. struct sa_info *info;
  120. int nr, size, i, ret = 0;
  121. /*
  122. * Count number of devices.
  123. */
  124. for (nr = 0; ; nr++)
  125. if (!platform_get_resource(pdev, IORESOURCE_MEM, nr))
  126. break;
  127. if (nr == 0) {
  128. ret = -ENODEV;
  129. goto out;
  130. }
  131. size = sizeof(struct sa_info) + sizeof(struct sa_subdev_info) * nr;
  132. /*
  133. * Allocate the map_info structs in one go.
  134. */
  135. info = kzalloc(size, GFP_KERNEL);
  136. if (!info) {
  137. ret = -ENOMEM;
  138. goto out;
  139. }
  140. if (plat->init) {
  141. ret = plat->init();
  142. if (ret)
  143. goto err;
  144. }
  145. /*
  146. * Claim and then map the memory regions.
  147. */
  148. for (i = 0; i < nr; i++) {
  149. struct sa_subdev_info *subdev = &info->subdev[i];
  150. struct resource *res;
  151. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  152. if (!res)
  153. break;
  154. subdev->map.name = subdev->name;
  155. sprintf(subdev->name, "%s-%d", plat->name, i);
  156. subdev->plat = plat;
  157. ret = sa1100_probe_subdev(subdev, res);
  158. if (ret)
  159. break;
  160. }
  161. info->num_subdev = i;
  162. /*
  163. * ENXIO is special. It means we didn't find a chip when we probed.
  164. */
  165. if (ret != 0 && !(ret == -ENXIO && info->num_subdev > 0))
  166. goto err;
  167. /*
  168. * If we found one device, don't bother with concat support. If
  169. * we found multiple devices, use concat if we have it available,
  170. * otherwise fail. Either way, it'll be called "sa1100".
  171. */
  172. if (info->num_subdev == 1) {
  173. strcpy(info->subdev[0].name, plat->name);
  174. info->mtd = info->subdev[0].mtd;
  175. ret = 0;
  176. } else if (info->num_subdev > 1) {
  177. struct mtd_info *cdev[nr];
  178. /*
  179. * We detected multiple devices. Concatenate them together.
  180. */
  181. for (i = 0; i < info->num_subdev; i++)
  182. cdev[i] = info->subdev[i].mtd;
  183. info->mtd = mtd_concat_create(cdev, info->num_subdev,
  184. plat->name);
  185. if (info->mtd == NULL)
  186. ret = -ENXIO;
  187. }
  188. if (ret == 0)
  189. return info;
  190. err:
  191. sa1100_destroy(info, plat);
  192. out:
  193. return ERR_PTR(ret);
  194. }
  195. static const char *part_probes[] = { "cmdlinepart", "RedBoot", NULL };
  196. static int __devinit sa1100_mtd_probe(struct platform_device *pdev)
  197. {
  198. struct flash_platform_data *plat = pdev->dev.platform_data;
  199. struct sa_info *info;
  200. int err;
  201. if (!plat)
  202. return -ENODEV;
  203. info = sa1100_setup_mtd(pdev, plat);
  204. if (IS_ERR(info)) {
  205. err = PTR_ERR(info);
  206. goto out;
  207. }
  208. /*
  209. * Partition selection stuff.
  210. */
  211. mtd_device_parse_register(info->mtd, part_probes, 0,
  212. plat->parts, plat->nr_parts);
  213. platform_set_drvdata(pdev, info);
  214. err = 0;
  215. out:
  216. return err;
  217. }
  218. static int __exit sa1100_mtd_remove(struct platform_device *pdev)
  219. {
  220. struct sa_info *info = platform_get_drvdata(pdev);
  221. struct flash_platform_data *plat = pdev->dev.platform_data;
  222. platform_set_drvdata(pdev, NULL);
  223. sa1100_destroy(info, plat);
  224. return 0;
  225. }
  226. static struct platform_driver sa1100_mtd_driver = {
  227. .probe = sa1100_mtd_probe,
  228. .remove = __exit_p(sa1100_mtd_remove),
  229. .driver = {
  230. .name = "sa1100-mtd",
  231. .owner = THIS_MODULE,
  232. },
  233. };
  234. module_platform_driver(sa1100_mtd_driver);
  235. MODULE_AUTHOR("Nicolas Pitre");
  236. MODULE_DESCRIPTION("SA1100 CFI map driver");
  237. MODULE_LICENSE("GPL");
  238. MODULE_ALIAS("platform:sa1100-mtd");