sa1100-flash.c 6.3 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. printk(KERN_INFO "SA1100 flash: CFI device at 0x%08lx, %uMiB, %d-bit\n",
  104. phys, (unsigned)(subdev->mtd->size >> 20),
  105. subdev->map.bankwidth * 8);
  106. return 0;
  107. err:
  108. sa1100_destroy_subdev(subdev);
  109. out:
  110. return ret;
  111. }
  112. static void sa1100_destroy(struct sa_info *info, struct flash_platform_data *plat)
  113. {
  114. int i;
  115. if (info->mtd) {
  116. mtd_device_unregister(info->mtd);
  117. if (info->mtd != info->subdev[0].mtd)
  118. mtd_concat_destroy(info->mtd);
  119. }
  120. for (i = info->num_subdev - 1; i >= 0; i--)
  121. sa1100_destroy_subdev(&info->subdev[i]);
  122. kfree(info);
  123. if (plat->exit)
  124. plat->exit();
  125. }
  126. static struct sa_info *sa1100_setup_mtd(struct platform_device *pdev,
  127. struct flash_platform_data *plat)
  128. {
  129. struct sa_info *info;
  130. int nr, size, i, ret = 0;
  131. /*
  132. * Count number of devices.
  133. */
  134. for (nr = 0; ; nr++)
  135. if (!platform_get_resource(pdev, IORESOURCE_MEM, nr))
  136. break;
  137. if (nr == 0) {
  138. ret = -ENODEV;
  139. goto out;
  140. }
  141. size = sizeof(struct sa_info) + sizeof(struct sa_subdev_info) * nr;
  142. /*
  143. * Allocate the map_info structs in one go.
  144. */
  145. info = kzalloc(size, GFP_KERNEL);
  146. if (!info) {
  147. ret = -ENOMEM;
  148. goto out;
  149. }
  150. if (plat->init) {
  151. ret = plat->init();
  152. if (ret)
  153. goto err;
  154. }
  155. /*
  156. * Claim and then map the memory regions.
  157. */
  158. for (i = 0; i < nr; i++) {
  159. struct sa_subdev_info *subdev = &info->subdev[i];
  160. struct resource *res;
  161. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  162. if (!res)
  163. break;
  164. subdev->map.name = subdev->name;
  165. sprintf(subdev->name, "%s-%d", plat->name, i);
  166. subdev->plat = plat;
  167. ret = sa1100_probe_subdev(subdev, res);
  168. if (ret)
  169. break;
  170. }
  171. info->num_subdev = i;
  172. /*
  173. * ENXIO is special. It means we didn't find a chip when we probed.
  174. */
  175. if (ret != 0 && !(ret == -ENXIO && info->num_subdev > 0))
  176. goto err;
  177. /*
  178. * If we found one device, don't bother with concat support. If
  179. * we found multiple devices, use concat if we have it available,
  180. * otherwise fail. Either way, it'll be called "sa1100".
  181. */
  182. if (info->num_subdev == 1) {
  183. strcpy(info->subdev[0].name, plat->name);
  184. info->mtd = info->subdev[0].mtd;
  185. ret = 0;
  186. } else if (info->num_subdev > 1) {
  187. struct mtd_info *cdev[nr];
  188. /*
  189. * We detected multiple devices. Concatenate them together.
  190. */
  191. for (i = 0; i < info->num_subdev; i++)
  192. cdev[i] = info->subdev[i].mtd;
  193. info->mtd = mtd_concat_create(cdev, info->num_subdev,
  194. plat->name);
  195. if (info->mtd == NULL) {
  196. ret = -ENXIO;
  197. goto err;
  198. }
  199. }
  200. info->mtd->dev.parent = &pdev->dev;
  201. if (ret == 0)
  202. return info;
  203. err:
  204. sa1100_destroy(info, plat);
  205. out:
  206. return ERR_PTR(ret);
  207. }
  208. static const char * const part_probes[] = { "cmdlinepart", "RedBoot", NULL };
  209. static int sa1100_mtd_probe(struct platform_device *pdev)
  210. {
  211. struct flash_platform_data *plat = dev_get_platdata(&pdev->dev);
  212. struct sa_info *info;
  213. int err;
  214. if (!plat)
  215. return -ENODEV;
  216. info = sa1100_setup_mtd(pdev, plat);
  217. if (IS_ERR(info)) {
  218. err = PTR_ERR(info);
  219. goto out;
  220. }
  221. /*
  222. * Partition selection stuff.
  223. */
  224. mtd_device_parse_register(info->mtd, part_probes, NULL, plat->parts,
  225. plat->nr_parts);
  226. platform_set_drvdata(pdev, info);
  227. err = 0;
  228. out:
  229. return err;
  230. }
  231. static int sa1100_mtd_remove(struct platform_device *pdev)
  232. {
  233. struct sa_info *info = platform_get_drvdata(pdev);
  234. struct flash_platform_data *plat = dev_get_platdata(&pdev->dev);
  235. sa1100_destroy(info, plat);
  236. return 0;
  237. }
  238. static struct platform_driver sa1100_mtd_driver = {
  239. .probe = sa1100_mtd_probe,
  240. .remove = sa1100_mtd_remove,
  241. .driver = {
  242. .name = "sa1100-mtd",
  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");