ceiva.c 7.4 KB

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