sa1100-flash.c 6.5 KB

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