ceiva.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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@fluxnic.net>
  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 accommodated.
  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 remaining 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. *rmtd = mtd_concat_create(subdev, found,
  172. "clps flash");
  173. if (*rmtd == NULL)
  174. ret = -ENXIO;
  175. }
  176. }
  177. /*
  178. * If we failed, clean up.
  179. */
  180. if (ret) {
  181. do {
  182. if (clps[i].mtd)
  183. map_destroy(clps[i].mtd);
  184. if (clps[i].vbase)
  185. iounmap(clps[i].vbase);
  186. if (clps[i].res)
  187. release_resource(clps[i].res);
  188. } while (i--);
  189. kfree(maps);
  190. }
  191. return ret;
  192. }
  193. static void __exit clps_destroy_mtd(struct clps_info *clps, struct mtd_info *mtd)
  194. {
  195. int i;
  196. mtd_device_unregister(mtd);
  197. if (mtd != clps[0].mtd)
  198. mtd_concat_destroy(mtd);
  199. for (i = NR_SUBMTD; i >= 0; i--) {
  200. if (clps[i].mtd)
  201. map_destroy(clps[i].mtd);
  202. if (clps[i].vbase)
  203. iounmap(clps[i].vbase);
  204. if (clps[i].res)
  205. release_resource(clps[i].res);
  206. }
  207. kfree(clps[0].map);
  208. }
  209. /*
  210. * We define the memory space, size, and width for the flash memory
  211. * space here.
  212. */
  213. static int __init clps_setup_flash(void)
  214. {
  215. int nr = 0;
  216. #ifdef CONFIG_ARCH_CEIVA
  217. if (machine_is_ceiva()) {
  218. info[0].base = CS0_PHYS_BASE;
  219. info[0].size = SZ_32M;
  220. info[0].width = CEIVA_FLASH_WIDTH;
  221. info[1].base = CS1_PHYS_BASE;
  222. info[1].size = SZ_32M;
  223. info[1].width = CEIVA_FLASH_WIDTH;
  224. nr = 2;
  225. }
  226. #endif
  227. return nr;
  228. }
  229. static struct mtd_partition *parsed_parts;
  230. static const char *probes[] = { "cmdlinepart", "RedBoot", NULL };
  231. static void __init clps_locate_partitions(struct mtd_info *mtd)
  232. {
  233. const char *part_type = NULL;
  234. int nr_parts = 0;
  235. do {
  236. /*
  237. * Partition selection stuff.
  238. */
  239. nr_parts = parse_mtd_partitions(mtd, probes, &parsed_parts, 0);
  240. if (nr_parts > 0) {
  241. part_type = "command line";
  242. break;
  243. }
  244. #ifdef CONFIG_MTD_CEIVA_STATICMAP
  245. nr_parts = clps_static_partitions(&parsed_parts);
  246. if (nr_parts > 0) {
  247. part_type = "static";
  248. break;
  249. }
  250. printk("found: %d partitions\n", nr_parts);
  251. #endif
  252. } while (0);
  253. if (nr_parts == 0) {
  254. printk(KERN_NOTICE "clps flash: no partition info "
  255. "available, registering whole flash\n");
  256. mtd_device_register(mtd, NULL, 0);
  257. } else {
  258. printk(KERN_NOTICE "clps flash: using %s partition "
  259. "definition\n", part_type);
  260. mtd_device_register(mtd, parsed_parts, nr_parts);
  261. }
  262. /* Always succeeds. */
  263. }
  264. static void __exit clps_destroy_partitions(void)
  265. {
  266. kfree(parsed_parts);
  267. }
  268. static struct mtd_info *mymtd;
  269. static int __init clps_mtd_init(void)
  270. {
  271. int ret;
  272. int nr;
  273. nr = clps_setup_flash();
  274. if (nr < 0)
  275. return nr;
  276. ret = clps_setup_mtd(info, nr, &mymtd);
  277. if (ret)
  278. return ret;
  279. clps_locate_partitions(mymtd);
  280. return 0;
  281. }
  282. static void __exit clps_mtd_cleanup(void)
  283. {
  284. clps_destroy_mtd(info, mymtd);
  285. clps_destroy_partitions();
  286. }
  287. module_init(clps_mtd_init);
  288. module_exit(clps_mtd_cleanup);
  289. MODULE_AUTHOR("Rob Scott");
  290. MODULE_DESCRIPTION("Cirrus Logic JEDEC map driver");
  291. MODULE_LICENSE("GPL");