ceiva.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Ceiva flash memory driver.
  3. * Copyright (C) 2002 Rob Scott <rscott@mtrob.fdns.net>
  4. *
  5. * Note: this driver supports jedec compatible devices. Modification
  6. * for CFI compatible devices should be straight forward: change
  7. * jedec_probe to cfi_probe.
  8. *
  9. * Based on: sa1100-flash.c, which has the following copyright:
  10. * Flash memory access on SA11x0 based devices
  11. *
  12. * (C) 2000 Nicolas Pitre <nico@cam.org>
  13. *
  14. * $Id: ceiva.c,v 1.11 2004/09/16 23:27:12 gleixner Exp $
  15. */
  16. #include <linux/config.h>
  17. #include <linux/module.h>
  18. #include <linux/types.h>
  19. #include <linux/ioport.h>
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/mtd/mtd.h>
  23. #include <linux/mtd/map.h>
  24. #include <linux/mtd/partitions.h>
  25. #include <linux/mtd/concat.h>
  26. #include <asm/hardware.h>
  27. #include <asm/mach-types.h>
  28. #include <asm/io.h>
  29. #include <asm/sizes.h>
  30. /*
  31. * This isn't complete yet, so...
  32. */
  33. #define CONFIG_MTD_CEIVA_STATICMAP
  34. #ifdef CONFIG_MTD_CEIVA_STATICMAP
  35. /*
  36. * See include/linux/mtd/partitions.h for definition of the mtd_partition
  37. * structure.
  38. *
  39. * Please note:
  40. * 1. The flash size given should be the largest flash size that can
  41. * be accomodated.
  42. *
  43. * 2. The bus width must defined in clps_setup_flash.
  44. *
  45. * The MTD layer will detect flash chip aliasing and reduce the size of
  46. * the map accordingly.
  47. *
  48. */
  49. #ifdef CONFIG_ARCH_CEIVA
  50. /* Flash / Partition sizing */
  51. /* For the 28F8003, we use the block mapping to calcuate the sizes */
  52. #define MAX_SIZE_KiB (16 + 8 + 8 + 96 + (7*128))
  53. #define BOOT_PARTITION_SIZE_KiB (16)
  54. #define PARAMS_PARTITION_SIZE_KiB (8)
  55. #define KERNEL_PARTITION_SIZE_KiB (4*128)
  56. /* Use both remaing portion of first flash, and all of second flash */
  57. #define ROOT_PARTITION_SIZE_KiB (3*128) + (8*128)
  58. static struct mtd_partition ceiva_partitions[] = {
  59. {
  60. .name = "Ceiva BOOT partition",
  61. .size = BOOT_PARTITION_SIZE_KiB*1024,
  62. .offset = 0,
  63. },{
  64. .name = "Ceiva parameters partition",
  65. .size = PARAMS_PARTITION_SIZE_KiB*1024,
  66. .offset = (16 + 8) * 1024,
  67. },{
  68. .name = "Ceiva kernel partition",
  69. .size = (KERNEL_PARTITION_SIZE_KiB)*1024,
  70. .offset = 0x20000,
  71. },{
  72. .name = "Ceiva root filesystem partition",
  73. .offset = MTDPART_OFS_APPEND,
  74. .size = (ROOT_PARTITION_SIZE_KiB)*1024,
  75. }
  76. };
  77. #endif
  78. static int __init clps_static_partitions(struct mtd_partition **parts)
  79. {
  80. int nb_parts = 0;
  81. #ifdef CONFIG_ARCH_CEIVA
  82. if (machine_is_ceiva()) {
  83. *parts = ceiva_partitions;
  84. nb_parts = ARRAY_SIZE(ceiva_partitions);
  85. }
  86. #endif
  87. return nb_parts;
  88. }
  89. #endif
  90. struct clps_info {
  91. unsigned long base;
  92. unsigned long size;
  93. int width;
  94. void *vbase;
  95. struct map_info *map;
  96. struct mtd_info *mtd;
  97. struct resource *res;
  98. };
  99. #define NR_SUBMTD 4
  100. static struct clps_info info[NR_SUBMTD];
  101. static int __init clps_setup_mtd(struct clps_info *clps, int nr, struct mtd_info **rmtd)
  102. {
  103. struct mtd_info *subdev[nr];
  104. struct map_info *maps;
  105. int i, found = 0, ret = 0;
  106. /*
  107. * Allocate the map_info structs in one go.
  108. */
  109. maps = kmalloc(sizeof(struct map_info) * nr, GFP_KERNEL);
  110. if (!maps)
  111. return -ENOMEM;
  112. memset(maps, 0, sizeof(struct map_info) * nr);
  113. /*
  114. * Claim and then map the memory regions.
  115. */
  116. for (i = 0; i < nr; i++) {
  117. if (clps[i].base == (unsigned long)-1)
  118. break;
  119. clps[i].res = request_mem_region(clps[i].base, clps[i].size, "clps flash");
  120. if (!clps[i].res) {
  121. ret = -EBUSY;
  122. break;
  123. }
  124. clps[i].map = maps + i;
  125. clps[i].map->name = "clps flash";
  126. clps[i].map->phys = clps[i].base;
  127. clps[i].vbase = ioremap(clps[i].base, clps[i].size);
  128. if (!clps[i].vbase) {
  129. ret = -ENOMEM;
  130. break;
  131. }
  132. clps[i].map->virt = (void __iomem *)clps[i].vbase;
  133. clps[i].map->bankwidth = clps[i].width;
  134. clps[i].map->size = clps[i].size;
  135. simple_map_init(&clps[i].map);
  136. clps[i].mtd = do_map_probe("jedec_probe", clps[i].map);
  137. if (clps[i].mtd == NULL) {
  138. ret = -ENXIO;
  139. break;
  140. }
  141. clps[i].mtd->owner = THIS_MODULE;
  142. subdev[i] = clps[i].mtd;
  143. printk(KERN_INFO "clps flash: JEDEC device at 0x%08lx, %dMiB, "
  144. "%d-bit\n", clps[i].base, clps[i].mtd->size >> 20,
  145. clps[i].width * 8);
  146. found += 1;
  147. }
  148. /*
  149. * ENXIO is special. It means we didn't find a chip when
  150. * we probed. We need to tear down the mapping, free the
  151. * resource and mark it as such.
  152. */
  153. if (ret == -ENXIO) {
  154. iounmap(clps[i].vbase);
  155. clps[i].vbase = NULL;
  156. release_resource(clps[i].res);
  157. clps[i].res = NULL;
  158. }
  159. /*
  160. * If we found one device, don't bother with concat support.
  161. * If we found multiple devices, use concat if we have it
  162. * available, otherwise fail.
  163. */
  164. if (ret == 0 || ret == -ENXIO) {
  165. if (found == 1) {
  166. *rmtd = subdev[0];
  167. ret = 0;
  168. } else if (found > 1) {
  169. /*
  170. * We detected multiple devices. Concatenate
  171. * them together.
  172. */
  173. #ifdef CONFIG_MTD_CONCAT
  174. *rmtd = mtd_concat_create(subdev, found,
  175. "clps flash");
  176. if (*rmtd == NULL)
  177. ret = -ENXIO;
  178. #else
  179. printk(KERN_ERR "clps flash: multiple devices "
  180. "found but MTD concat support disabled.\n");
  181. ret = -ENXIO;
  182. #endif
  183. }
  184. }
  185. /*
  186. * If we failed, clean up.
  187. */
  188. if (ret) {
  189. do {
  190. if (clps[i].mtd)
  191. map_destroy(clps[i].mtd);
  192. if (clps[i].vbase)
  193. iounmap(clps[i].vbase);
  194. if (clps[i].res)
  195. release_resource(clps[i].res);
  196. } while (i--);
  197. kfree(maps);
  198. }
  199. return ret;
  200. }
  201. static void __exit clps_destroy_mtd(struct clps_info *clps, struct mtd_info *mtd)
  202. {
  203. int i;
  204. del_mtd_partitions(mtd);
  205. if (mtd != clps[0].mtd)
  206. mtd_concat_destroy(mtd);
  207. for (i = NR_SUBMTD; i >= 0; i--) {
  208. if (clps[i].mtd)
  209. map_destroy(clps[i].mtd);
  210. if (clps[i].vbase)
  211. iounmap(clps[i].vbase);
  212. if (clps[i].res)
  213. release_resource(clps[i].res);
  214. }
  215. kfree(clps[0].map);
  216. }
  217. /*
  218. * We define the memory space, size, and width for the flash memory
  219. * space here.
  220. */
  221. static int __init clps_setup_flash(void)
  222. {
  223. int nr;
  224. #ifdef CONFIG_ARCH_CEIVA
  225. if (machine_is_ceiva()) {
  226. info[0].base = CS0_PHYS_BASE;
  227. info[0].size = SZ_32M;
  228. info[0].width = CEIVA_FLASH_WIDTH;
  229. info[1].base = CS1_PHYS_BASE;
  230. info[1].size = SZ_32M;
  231. info[1].width = CEIVA_FLASH_WIDTH;
  232. nr = 2;
  233. }
  234. #endif
  235. return nr;
  236. }
  237. static struct mtd_partition *parsed_parts;
  238. static const char *probes[] = { "cmdlinepart", "RedBoot", NULL };
  239. static void __init clps_locate_partitions(struct mtd_info *mtd)
  240. {
  241. const char *part_type = NULL;
  242. int nr_parts = 0;
  243. do {
  244. /*
  245. * Partition selection stuff.
  246. */
  247. nr_parts = parse_mtd_partitions(mtd, probes, &parsed_parts, 0);
  248. if (nr_parts > 0) {
  249. part_type = "command line";
  250. break;
  251. }
  252. #ifdef CONFIG_MTD_CEIVA_STATICMAP
  253. nr_parts = clps_static_partitions(&parsed_parts);
  254. if (nr_parts > 0) {
  255. part_type = "static";
  256. break;
  257. }
  258. printk("found: %d partitions\n", nr_parts);
  259. #endif
  260. } while (0);
  261. if (nr_parts == 0) {
  262. printk(KERN_NOTICE "clps flash: no partition info "
  263. "available, registering whole flash\n");
  264. add_mtd_device(mtd);
  265. } else {
  266. printk(KERN_NOTICE "clps flash: using %s partition "
  267. "definition\n", part_type);
  268. add_mtd_partitions(mtd, parsed_parts, nr_parts);
  269. }
  270. /* Always succeeds. */
  271. }
  272. static void __exit clps_destroy_partitions(void)
  273. {
  274. if (parsed_parts)
  275. kfree(parsed_parts);
  276. }
  277. static struct mtd_info *mymtd;
  278. static int __init clps_mtd_init(void)
  279. {
  280. int ret;
  281. int nr;
  282. nr = clps_setup_flash();
  283. if (nr < 0)
  284. return nr;
  285. ret = clps_setup_mtd(info, nr, &mymtd);
  286. if (ret)
  287. return ret;
  288. clps_locate_partitions(mymtd);
  289. return 0;
  290. }
  291. static void __exit clps_mtd_cleanup(void)
  292. {
  293. clps_destroy_mtd(info, mymtd);
  294. clps_destroy_partitions();
  295. }
  296. module_init(clps_mtd_init);
  297. module_exit(clps_mtd_cleanup);
  298. MODULE_AUTHOR("Rob Scott");
  299. MODULE_DESCRIPTION("Cirrus Logic JEDEC map driver");
  300. MODULE_LICENSE("GPL");