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