sa1100-flash.c 10 KB

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