sa1100-flash.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. #if 0
  24. /*
  25. * This is here for documentation purposes only - until these people
  26. * submit their machine types. It will be gone January 2005.
  27. */
  28. static struct mtd_partition consus_partitions[] = {
  29. {
  30. .name = "Consus boot firmware",
  31. .offset = 0,
  32. .size = 0x00040000,
  33. .mask_flags = MTD_WRITABLE, /* force read-only */
  34. }, {
  35. .name = "Consus kernel",
  36. .offset = 0x00040000,
  37. .size = 0x00100000,
  38. .mask_flags = 0,
  39. }, {
  40. .name = "Consus disk",
  41. .offset = 0x00140000,
  42. /* The rest (up to 16M) for jffs. We could put 0 and
  43. make it find the size automatically, but right now
  44. i have 32 megs. jffs will use all 32 megs if given
  45. the chance, and this leads to horrible problems
  46. when you try to re-flash the image because blob
  47. won't erase the whole partition. */
  48. .size = 0x01000000 - 0x00140000,
  49. .mask_flags = 0,
  50. }, {
  51. /* this disk is a secondary disk, which can be used as
  52. needed, for simplicity, make it the size of the other
  53. consus partition, although realistically it could be
  54. the remainder of the disk (depending on the file
  55. system used) */
  56. .name = "Consus disk2",
  57. .offset = 0x01000000,
  58. .size = 0x01000000 - 0x00140000,
  59. .mask_flags = 0,
  60. }
  61. };
  62. /* Frodo has 2 x 16M 28F128J3A flash chips in bank 0: */
  63. static struct mtd_partition frodo_partitions[] =
  64. {
  65. {
  66. .name = "bootloader",
  67. .size = 0x00040000,
  68. .offset = 0x00000000,
  69. .mask_flags = MTD_WRITEABLE
  70. }, {
  71. .name = "bootloader params",
  72. .size = 0x00040000,
  73. .offset = MTDPART_OFS_APPEND,
  74. .mask_flags = MTD_WRITEABLE
  75. }, {
  76. .name = "kernel",
  77. .size = 0x00100000,
  78. .offset = MTDPART_OFS_APPEND,
  79. .mask_flags = MTD_WRITEABLE
  80. }, {
  81. .name = "ramdisk",
  82. .size = 0x00400000,
  83. .offset = MTDPART_OFS_APPEND,
  84. .mask_flags = MTD_WRITEABLE
  85. }, {
  86. .name = "file system",
  87. .size = MTDPART_SIZ_FULL,
  88. .offset = MTDPART_OFS_APPEND
  89. }
  90. };
  91. static struct mtd_partition jornada56x_partitions[] = {
  92. {
  93. .name = "bootldr",
  94. .size = 0x00040000,
  95. .offset = 0,
  96. .mask_flags = MTD_WRITEABLE,
  97. }, {
  98. .name = "rootfs",
  99. .size = MTDPART_SIZ_FULL,
  100. .offset = MTDPART_OFS_APPEND,
  101. }
  102. };
  103. static void jornada56x_set_vpp(int vpp)
  104. {
  105. if (vpp)
  106. GPSR = GPIO_GPIO26;
  107. else
  108. GPCR = GPIO_GPIO26;
  109. GPDR |= GPIO_GPIO26;
  110. }
  111. /*
  112. * Machine Phys Size set_vpp
  113. * Consus : SA1100_CS0_PHYS SZ_32M
  114. * Frodo : SA1100_CS0_PHYS SZ_32M
  115. * Jornada56x: SA1100_CS0_PHYS SZ_32M jornada56x_set_vpp
  116. */
  117. #endif
  118. struct sa_subdev_info {
  119. char name[16];
  120. struct map_info map;
  121. struct mtd_info *mtd;
  122. struct flash_platform_data *plat;
  123. };
  124. struct sa_info {
  125. struct mtd_partition *parts;
  126. struct mtd_info *mtd;
  127. int num_subdev;
  128. unsigned int nr_parts;
  129. struct sa_subdev_info subdev[0];
  130. };
  131. static void sa1100_set_vpp(struct map_info *map, int on)
  132. {
  133. struct sa_subdev_info *subdev = container_of(map, struct sa_subdev_info, map);
  134. subdev->plat->set_vpp(on);
  135. }
  136. static void sa1100_destroy_subdev(struct sa_subdev_info *subdev)
  137. {
  138. if (subdev->mtd)
  139. map_destroy(subdev->mtd);
  140. if (subdev->map.virt)
  141. iounmap(subdev->map.virt);
  142. release_mem_region(subdev->map.phys, subdev->map.size);
  143. }
  144. static int sa1100_probe_subdev(struct sa_subdev_info *subdev, struct resource *res)
  145. {
  146. unsigned long phys;
  147. unsigned int size;
  148. int ret;
  149. phys = res->start;
  150. size = res->end - phys + 1;
  151. /*
  152. * Retrieve the bankwidth from the MSC registers.
  153. * We currently only implement CS0 and CS1 here.
  154. */
  155. switch (phys) {
  156. default:
  157. printk(KERN_WARNING "SA1100 flash: unknown base address "
  158. "0x%08lx, assuming CS0\n", phys);
  159. case SA1100_CS0_PHYS:
  160. subdev->map.bankwidth = (MSC0 & MSC_RBW) ? 2 : 4;
  161. break;
  162. case SA1100_CS1_PHYS:
  163. subdev->map.bankwidth = ((MSC0 >> 16) & MSC_RBW) ? 2 : 4;
  164. break;
  165. }
  166. if (!request_mem_region(phys, size, subdev->name)) {
  167. ret = -EBUSY;
  168. goto out;
  169. }
  170. if (subdev->plat->set_vpp)
  171. subdev->map.set_vpp = sa1100_set_vpp;
  172. subdev->map.phys = phys;
  173. subdev->map.size = size;
  174. subdev->map.virt = ioremap(phys, size);
  175. if (!subdev->map.virt) {
  176. ret = -ENOMEM;
  177. goto err;
  178. }
  179. simple_map_init(&subdev->map);
  180. /*
  181. * Now let's probe for the actual flash. Do it here since
  182. * specific machine settings might have been set above.
  183. */
  184. subdev->mtd = do_map_probe(subdev->plat->map_name, &subdev->map);
  185. if (subdev->mtd == NULL) {
  186. ret = -ENXIO;
  187. goto err;
  188. }
  189. subdev->mtd->owner = THIS_MODULE;
  190. printk(KERN_INFO "SA1100 flash: CFI device at 0x%08lx, %uMiB, %d-bit\n",
  191. phys, (unsigned)(subdev->mtd->size >> 20),
  192. subdev->map.bankwidth * 8);
  193. return 0;
  194. err:
  195. sa1100_destroy_subdev(subdev);
  196. out:
  197. return ret;
  198. }
  199. static void sa1100_destroy(struct sa_info *info, struct flash_platform_data *plat)
  200. {
  201. int i;
  202. if (info->mtd) {
  203. mtd_device_unregister(info->mtd);
  204. if (info->mtd != info->subdev[0].mtd)
  205. mtd_concat_destroy(info->mtd);
  206. }
  207. kfree(info->parts);
  208. for (i = info->num_subdev - 1; i >= 0; i--)
  209. sa1100_destroy_subdev(&info->subdev[i]);
  210. kfree(info);
  211. if (plat->exit)
  212. plat->exit();
  213. }
  214. static struct sa_info *__devinit
  215. sa1100_setup_mtd(struct platform_device *pdev, struct flash_platform_data *plat)
  216. {
  217. struct sa_info *info;
  218. int nr, size, i, ret = 0;
  219. /*
  220. * Count number of devices.
  221. */
  222. for (nr = 0; ; nr++)
  223. if (!platform_get_resource(pdev, IORESOURCE_MEM, nr))
  224. break;
  225. if (nr == 0) {
  226. ret = -ENODEV;
  227. goto out;
  228. }
  229. size = sizeof(struct sa_info) + sizeof(struct sa_subdev_info) * nr;
  230. /*
  231. * Allocate the map_info structs in one go.
  232. */
  233. info = kzalloc(size, GFP_KERNEL);
  234. if (!info) {
  235. ret = -ENOMEM;
  236. goto out;
  237. }
  238. if (plat->init) {
  239. ret = plat->init();
  240. if (ret)
  241. goto err;
  242. }
  243. /*
  244. * Claim and then map the memory regions.
  245. */
  246. for (i = 0; i < nr; i++) {
  247. struct sa_subdev_info *subdev = &info->subdev[i];
  248. struct resource *res;
  249. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  250. if (!res)
  251. break;
  252. subdev->map.name = subdev->name;
  253. sprintf(subdev->name, "%s-%d", plat->name, i);
  254. subdev->plat = plat;
  255. ret = sa1100_probe_subdev(subdev, res);
  256. if (ret)
  257. break;
  258. }
  259. info->num_subdev = i;
  260. /*
  261. * ENXIO is special. It means we didn't find a chip when we probed.
  262. */
  263. if (ret != 0 && !(ret == -ENXIO && info->num_subdev > 0))
  264. goto err;
  265. /*
  266. * If we found one device, don't bother with concat support. If
  267. * we found multiple devices, use concat if we have it available,
  268. * otherwise fail. Either way, it'll be called "sa1100".
  269. */
  270. if (info->num_subdev == 1) {
  271. strcpy(info->subdev[0].name, plat->name);
  272. info->mtd = info->subdev[0].mtd;
  273. ret = 0;
  274. } else if (info->num_subdev > 1) {
  275. struct mtd_info *cdev[nr];
  276. /*
  277. * We detected multiple devices. Concatenate them together.
  278. */
  279. for (i = 0; i < info->num_subdev; i++)
  280. cdev[i] = info->subdev[i].mtd;
  281. info->mtd = mtd_concat_create(cdev, info->num_subdev,
  282. plat->name);
  283. if (info->mtd == NULL)
  284. ret = -ENXIO;
  285. }
  286. if (ret == 0)
  287. return info;
  288. err:
  289. sa1100_destroy(info, plat);
  290. out:
  291. return ERR_PTR(ret);
  292. }
  293. static const char *part_probes[] = { "cmdlinepart", "RedBoot", NULL };
  294. static int __devinit sa1100_mtd_probe(struct platform_device *pdev)
  295. {
  296. struct flash_platform_data *plat = pdev->dev.platform_data;
  297. struct mtd_partition *parts;
  298. const char *part_type = NULL;
  299. struct sa_info *info;
  300. int err, nr_parts = 0;
  301. if (!plat)
  302. return -ENODEV;
  303. info = sa1100_setup_mtd(pdev, plat);
  304. if (IS_ERR(info)) {
  305. err = PTR_ERR(info);
  306. goto out;
  307. }
  308. /*
  309. * Partition selection stuff.
  310. */
  311. nr_parts = parse_mtd_partitions(info->mtd, part_probes, &parts, 0);
  312. if (nr_parts > 0) {
  313. info->parts = parts;
  314. part_type = "dynamic";
  315. } else {
  316. parts = plat->parts;
  317. nr_parts = plat->nr_parts;
  318. part_type = "static";
  319. }
  320. if (nr_parts == 0)
  321. printk(KERN_NOTICE "SA1100 flash: no partition info "
  322. "available, registering whole flash\n");
  323. else
  324. printk(KERN_NOTICE "SA1100 flash: using %s partition "
  325. "definition\n", part_type);
  326. mtd_device_register(info->mtd, parts, nr_parts);
  327. info->nr_parts = nr_parts;
  328. platform_set_drvdata(pdev, info);
  329. err = 0;
  330. out:
  331. return err;
  332. }
  333. static int __exit sa1100_mtd_remove(struct platform_device *pdev)
  334. {
  335. struct sa_info *info = platform_get_drvdata(pdev);
  336. struct flash_platform_data *plat = pdev->dev.platform_data;
  337. platform_set_drvdata(pdev, NULL);
  338. sa1100_destroy(info, plat);
  339. return 0;
  340. }
  341. #ifdef CONFIG_PM
  342. static void sa1100_mtd_shutdown(struct platform_device *dev)
  343. {
  344. struct sa_info *info = platform_get_drvdata(dev);
  345. if (info && info->mtd->suspend(info->mtd) == 0)
  346. info->mtd->resume(info->mtd);
  347. }
  348. #else
  349. #define sa1100_mtd_shutdown NULL
  350. #endif
  351. static struct platform_driver sa1100_mtd_driver = {
  352. .probe = sa1100_mtd_probe,
  353. .remove = __exit_p(sa1100_mtd_remove),
  354. .shutdown = sa1100_mtd_shutdown,
  355. .driver = {
  356. .name = "sa1100-mtd",
  357. .owner = THIS_MODULE,
  358. },
  359. };
  360. static int __init sa1100_mtd_init(void)
  361. {
  362. return platform_driver_register(&sa1100_mtd_driver);
  363. }
  364. static void __exit sa1100_mtd_exit(void)
  365. {
  366. platform_driver_unregister(&sa1100_mtd_driver);
  367. }
  368. module_init(sa1100_mtd_init);
  369. module_exit(sa1100_mtd_exit);
  370. MODULE_AUTHOR("Nicolas Pitre");
  371. MODULE_DESCRIPTION("SA1100 CFI map driver");
  372. MODULE_LICENSE("GPL");
  373. MODULE_ALIAS("platform:sa1100-mtd");