sa1100-flash.c 6.4 KB

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