ceiva.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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/module.h>
  17. #include <linux/types.h>
  18. #include <linux/ioport.h>
  19. #include <linux/kernel.h>
  20. #include <linux/init.h>
  21. #include <linux/slab.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 = kzalloc(sizeof(struct map_info) * nr, GFP_KERNEL);
  110. if (!maps)
  111. return -ENOMEM;
  112. /*
  113. * Claim and then map the memory regions.
  114. */
  115. for (i = 0; i < nr; i++) {
  116. if (clps[i].base == (unsigned long)-1)
  117. break;
  118. clps[i].res = request_mem_region(clps[i].base, clps[i].size, "clps flash");
  119. if (!clps[i].res) {
  120. ret = -EBUSY;
  121. break;
  122. }
  123. clps[i].map = maps + i;
  124. clps[i].map->name = "clps flash";
  125. clps[i].map->phys = clps[i].base;
  126. clps[i].vbase = ioremap(clps[i].base, clps[i].size);
  127. if (!clps[i].vbase) {
  128. ret = -ENOMEM;
  129. break;
  130. }
  131. clps[i].map->virt = (void __iomem *)clps[i].vbase;
  132. clps[i].map->bankwidth = clps[i].width;
  133. clps[i].map->size = clps[i].size;
  134. simple_map_init(&clps[i].map);
  135. clps[i].mtd = do_map_probe("jedec_probe", clps[i].map);
  136. if (clps[i].mtd == NULL) {
  137. ret = -ENXIO;
  138. break;
  139. }
  140. clps[i].mtd->owner = THIS_MODULE;
  141. subdev[i] = clps[i].mtd;
  142. printk(KERN_INFO "clps flash: JEDEC device at 0x%08lx, %dMiB, "
  143. "%d-bit\n", clps[i].base, clps[i].mtd->size >> 20,
  144. clps[i].width * 8);
  145. found += 1;
  146. }
  147. /*
  148. * ENXIO is special. It means we didn't find a chip when
  149. * we probed. We need to tear down the mapping, free the
  150. * resource and mark it as such.
  151. */
  152. if (ret == -ENXIO) {
  153. iounmap(clps[i].vbase);
  154. clps[i].vbase = NULL;
  155. release_resource(clps[i].res);
  156. clps[i].res = NULL;
  157. }
  158. /*
  159. * If we found one device, don't bother with concat support.
  160. * If we found multiple devices, use concat if we have it
  161. * available, otherwise fail.
  162. */
  163. if (ret == 0 || ret == -ENXIO) {
  164. if (found == 1) {
  165. *rmtd = subdev[0];
  166. ret = 0;
  167. } else if (found > 1) {
  168. /*
  169. * We detected multiple devices. Concatenate
  170. * them together.
  171. */
  172. #ifdef CONFIG_MTD_CONCAT
  173. *rmtd = mtd_concat_create(subdev, found,
  174. "clps flash");
  175. if (*rmtd == NULL)
  176. ret = -ENXIO;
  177. #else
  178. printk(KERN_ERR "clps flash: multiple devices "
  179. "found but MTD concat support disabled.\n");
  180. ret = -ENXIO;
  181. #endif
  182. }
  183. }
  184. /*
  185. * If we failed, clean up.
  186. */
  187. if (ret) {
  188. do {
  189. if (clps[i].mtd)
  190. map_destroy(clps[i].mtd);
  191. if (clps[i].vbase)
  192. iounmap(clps[i].vbase);
  193. if (clps[i].res)
  194. release_resource(clps[i].res);
  195. } while (i--);
  196. kfree(maps);
  197. }
  198. return ret;
  199. }
  200. static void __exit clps_destroy_mtd(struct clps_info *clps, struct mtd_info *mtd)
  201. {
  202. int i;
  203. del_mtd_partitions(mtd);
  204. if (mtd != clps[0].mtd)
  205. mtd_concat_destroy(mtd);
  206. for (i = NR_SUBMTD; i >= 0; i--) {
  207. if (clps[i].mtd)
  208. map_destroy(clps[i].mtd);
  209. if (clps[i].vbase)
  210. iounmap(clps[i].vbase);
  211. if (clps[i].res)
  212. release_resource(clps[i].res);
  213. }
  214. kfree(clps[0].map);
  215. }
  216. /*
  217. * We define the memory space, size, and width for the flash memory
  218. * space here.
  219. */
  220. static int __init clps_setup_flash(void)
  221. {
  222. int nr;
  223. #ifdef CONFIG_ARCH_CEIVA
  224. if (machine_is_ceiva()) {
  225. info[0].base = CS0_PHYS_BASE;
  226. info[0].size = SZ_32M;
  227. info[0].width = CEIVA_FLASH_WIDTH;
  228. info[1].base = CS1_PHYS_BASE;
  229. info[1].size = SZ_32M;
  230. info[1].width = CEIVA_FLASH_WIDTH;
  231. nr = 2;
  232. }
  233. #endif
  234. return nr;
  235. }
  236. static struct mtd_partition *parsed_parts;
  237. static const char *probes[] = { "cmdlinepart", "RedBoot", NULL };
  238. static void __init clps_locate_partitions(struct mtd_info *mtd)
  239. {
  240. const char *part_type = NULL;
  241. int nr_parts = 0;
  242. do {
  243. /*
  244. * Partition selection stuff.
  245. */
  246. nr_parts = parse_mtd_partitions(mtd, probes, &parsed_parts, 0);
  247. if (nr_parts > 0) {
  248. part_type = "command line";
  249. break;
  250. }
  251. #ifdef CONFIG_MTD_CEIVA_STATICMAP
  252. nr_parts = clps_static_partitions(&parsed_parts);
  253. if (nr_parts > 0) {
  254. part_type = "static";
  255. break;
  256. }
  257. printk("found: %d partitions\n", nr_parts);
  258. #endif
  259. } while (0);
  260. if (nr_parts == 0) {
  261. printk(KERN_NOTICE "clps flash: no partition info "
  262. "available, registering whole flash\n");
  263. add_mtd_device(mtd);
  264. } else {
  265. printk(KERN_NOTICE "clps flash: using %s partition "
  266. "definition\n", part_type);
  267. add_mtd_partitions(mtd, parsed_parts, nr_parts);
  268. }
  269. /* Always succeeds. */
  270. }
  271. static void __exit clps_destroy_partitions(void)
  272. {
  273. kfree(parsed_parts);
  274. }
  275. static struct mtd_info *mymtd;
  276. static int __init clps_mtd_init(void)
  277. {
  278. int ret;
  279. int nr;
  280. nr = clps_setup_flash();
  281. if (nr < 0)
  282. return nr;
  283. ret = clps_setup_mtd(info, nr, &mymtd);
  284. if (ret)
  285. return ret;
  286. clps_locate_partitions(mymtd);
  287. return 0;
  288. }
  289. static void __exit clps_mtd_cleanup(void)
  290. {
  291. clps_destroy_mtd(info, mymtd);
  292. clps_destroy_partitions();
  293. }
  294. module_init(clps_mtd_init);
  295. module_exit(clps_mtd_cleanup);
  296. MODULE_AUTHOR("Rob Scott");
  297. MODULE_DESCRIPTION("Cirrus Logic JEDEC map driver");
  298. MODULE_LICENSE("GPL");