sharpsl.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * drivers/mtd/nand/sharpsl.c
  3. *
  4. * Copyright (C) 2004 Richard Purdie
  5. *
  6. * $Id: sharpsl.c,v 1.7 2005/11/07 11:14:31 gleixner Exp $
  7. *
  8. * Based on Sharp's NAND driver sharp_sl.c
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. */
  15. #include <linux/genhd.h>
  16. #include <linux/slab.h>
  17. #include <linux/module.h>
  18. #include <linux/delay.h>
  19. #include <linux/mtd/mtd.h>
  20. #include <linux/mtd/nand.h>
  21. #include <linux/mtd/nand_ecc.h>
  22. #include <linux/mtd/partitions.h>
  23. #include <linux/interrupt.h>
  24. #include <asm/io.h>
  25. #include <asm/hardware.h>
  26. #include <asm/mach-types.h>
  27. static void __iomem *sharpsl_io_base;
  28. static int sharpsl_phys_base = 0x0C000000;
  29. /* register offset */
  30. #define ECCLPLB sharpsl_io_base+0x00 /* line parity 7 - 0 bit */
  31. #define ECCLPUB sharpsl_io_base+0x04 /* line parity 15 - 8 bit */
  32. #define ECCCP sharpsl_io_base+0x08 /* column parity 5 - 0 bit */
  33. #define ECCCNTR sharpsl_io_base+0x0C /* ECC byte counter */
  34. #define ECCCLRR sharpsl_io_base+0x10 /* cleare ECC */
  35. #define FLASHIO sharpsl_io_base+0x14 /* Flash I/O */
  36. #define FLASHCTL sharpsl_io_base+0x18 /* Flash Control */
  37. /* Flash control bit */
  38. #define FLRYBY (1 << 5)
  39. #define FLCE1 (1 << 4)
  40. #define FLWP (1 << 3)
  41. #define FLALE (1 << 2)
  42. #define FLCLE (1 << 1)
  43. #define FLCE0 (1 << 0)
  44. /*
  45. * MTD structure for SharpSL
  46. */
  47. static struct mtd_info *sharpsl_mtd = NULL;
  48. /*
  49. * Define partitions for flash device
  50. */
  51. #define DEFAULT_NUM_PARTITIONS 3
  52. static int nr_partitions;
  53. static struct mtd_partition sharpsl_nand_default_partition_info[] = {
  54. {
  55. .name = "System Area",
  56. .offset = 0,
  57. .size = 7 * 1024 * 1024,
  58. },
  59. {
  60. .name = "Root Filesystem",
  61. .offset = 7 * 1024 * 1024,
  62. .size = 30 * 1024 * 1024,
  63. },
  64. {
  65. .name = "Home Filesystem",
  66. .offset = MTDPART_OFS_APPEND ,
  67. .size = MTDPART_SIZ_FULL ,
  68. },
  69. };
  70. /*
  71. * hardware specific access to control-lines
  72. */
  73. static void
  74. sharpsl_nand_hwcontrol(struct mtd_info* mtd, int cmd)
  75. {
  76. switch (cmd) {
  77. case NAND_CTL_SETCLE:
  78. writeb(readb(FLASHCTL) | FLCLE, FLASHCTL);
  79. break;
  80. case NAND_CTL_CLRCLE:
  81. writeb(readb(FLASHCTL) & ~FLCLE, FLASHCTL);
  82. break;
  83. case NAND_CTL_SETALE:
  84. writeb(readb(FLASHCTL) | FLALE, FLASHCTL);
  85. break;
  86. case NAND_CTL_CLRALE:
  87. writeb(readb(FLASHCTL) & ~FLALE, FLASHCTL);
  88. break;
  89. case NAND_CTL_SETNCE:
  90. writeb(readb(FLASHCTL) & ~(FLCE0|FLCE1), FLASHCTL);
  91. break;
  92. case NAND_CTL_CLRNCE:
  93. writeb(readb(FLASHCTL) | (FLCE0|FLCE1), FLASHCTL);
  94. break;
  95. }
  96. }
  97. static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
  98. static struct nand_bbt_descr sharpsl_bbt = {
  99. .options = 0,
  100. .offs = 4,
  101. .len = 2,
  102. .pattern = scan_ff_pattern
  103. };
  104. static struct nand_bbt_descr sharpsl_akita_bbt = {
  105. .options = 0,
  106. .offs = 4,
  107. .len = 1,
  108. .pattern = scan_ff_pattern
  109. };
  110. static struct nand_oobinfo akita_oobinfo = {
  111. .useecc = MTD_NANDECC_AUTOPLACE,
  112. .eccbytes = 24,
  113. .eccpos = {
  114. 0x5, 0x1, 0x2, 0x3, 0x6, 0x7, 0x15, 0x11,
  115. 0x12, 0x13, 0x16, 0x17, 0x25, 0x21, 0x22, 0x23,
  116. 0x26, 0x27, 0x35, 0x31, 0x32, 0x33, 0x36, 0x37},
  117. .oobfree = { {0x08, 0x09} }
  118. };
  119. static int
  120. sharpsl_nand_dev_ready(struct mtd_info* mtd)
  121. {
  122. return !((readb(FLASHCTL) & FLRYBY) == 0);
  123. }
  124. static void
  125. sharpsl_nand_enable_hwecc(struct mtd_info* mtd, int mode)
  126. {
  127. writeb(0 ,ECCCLRR);
  128. }
  129. static int
  130. sharpsl_nand_calculate_ecc(struct mtd_info* mtd, const u_char* dat,
  131. u_char* ecc_code)
  132. {
  133. ecc_code[0] = ~readb(ECCLPUB);
  134. ecc_code[1] = ~readb(ECCLPLB);
  135. ecc_code[2] = (~readb(ECCCP) << 2) | 0x03;
  136. return readb(ECCCNTR) != 0;
  137. }
  138. #ifdef CONFIG_MTD_PARTITIONS
  139. const char *part_probes[] = { "cmdlinepart", NULL };
  140. #endif
  141. /*
  142. * Main initialization routine
  143. */
  144. int __init
  145. sharpsl_nand_init(void)
  146. {
  147. struct nand_chip *this;
  148. struct mtd_partition* sharpsl_partition_info;
  149. int err = 0;
  150. /* Allocate memory for MTD device structure and private data */
  151. sharpsl_mtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip),
  152. GFP_KERNEL);
  153. if (!sharpsl_mtd) {
  154. printk ("Unable to allocate SharpSL NAND MTD device structure.\n");
  155. return -ENOMEM;
  156. }
  157. /* map physical adress */
  158. sharpsl_io_base = ioremap(sharpsl_phys_base, 0x1000);
  159. if(!sharpsl_io_base){
  160. printk("ioremap to access Sharp SL NAND chip failed\n");
  161. kfree(sharpsl_mtd);
  162. return -EIO;
  163. }
  164. /* Get pointer to private data */
  165. this = (struct nand_chip *) (&sharpsl_mtd[1]);
  166. /* Initialize structures */
  167. memset((char *) sharpsl_mtd, 0, sizeof(struct mtd_info));
  168. memset((char *) this, 0, sizeof(struct nand_chip));
  169. /* Link the private data with the MTD structure */
  170. sharpsl_mtd->priv = this;
  171. /*
  172. * PXA initialize
  173. */
  174. writeb(readb(FLASHCTL) | FLWP, FLASHCTL);
  175. /* Set address of NAND IO lines */
  176. this->IO_ADDR_R = FLASHIO;
  177. this->IO_ADDR_W = FLASHIO;
  178. /* Set address of hardware control function */
  179. this->hwcontrol = sharpsl_nand_hwcontrol;
  180. this->dev_ready = sharpsl_nand_dev_ready;
  181. /* 15 us command delay time */
  182. this->chip_delay = 15;
  183. /* set eccmode using hardware ECC */
  184. this->eccmode = NAND_ECC_HW3_256;
  185. this->badblock_pattern = &sharpsl_bbt;
  186. if (machine_is_akita() || machine_is_borzoi()) {
  187. this->badblock_pattern = &sharpsl_akita_bbt;
  188. this->autooob = &akita_oobinfo;
  189. }
  190. this->enable_hwecc = sharpsl_nand_enable_hwecc;
  191. this->calculate_ecc = sharpsl_nand_calculate_ecc;
  192. this->correct_data = nand_correct_data;
  193. /* Scan to find existence of the device */
  194. err=nand_scan(sharpsl_mtd,1);
  195. if (err) {
  196. iounmap(sharpsl_io_base);
  197. kfree(sharpsl_mtd);
  198. return err;
  199. }
  200. /* Register the partitions */
  201. sharpsl_mtd->name = "sharpsl-nand";
  202. nr_partitions = parse_mtd_partitions(sharpsl_mtd, part_probes,
  203. &sharpsl_partition_info, 0);
  204. if (nr_partitions <= 0) {
  205. nr_partitions = DEFAULT_NUM_PARTITIONS;
  206. sharpsl_partition_info = sharpsl_nand_default_partition_info;
  207. if (machine_is_poodle()) {
  208. sharpsl_partition_info[1].size=30 * 1024 * 1024;
  209. } else if (machine_is_corgi() || machine_is_shepherd()) {
  210. sharpsl_partition_info[1].size=25 * 1024 * 1024;
  211. } else if (machine_is_husky()) {
  212. sharpsl_partition_info[1].size=53 * 1024 * 1024;
  213. } else if (machine_is_spitz()) {
  214. sharpsl_partition_info[1].size=5 * 1024 * 1024;
  215. } else if (machine_is_akita()) {
  216. sharpsl_partition_info[1].size=58 * 1024 * 1024;
  217. } else if (machine_is_borzoi()) {
  218. sharpsl_partition_info[1].size=32 * 1024 * 1024;
  219. }
  220. }
  221. if (machine_is_husky() || machine_is_borzoi() || machine_is_akita()) {
  222. /* Need to use small eraseblock size for backward compatibility */
  223. sharpsl_mtd->flags |= MTD_NO_VIRTBLOCKS;
  224. }
  225. add_mtd_partitions(sharpsl_mtd, sharpsl_partition_info, nr_partitions);
  226. /* Return happy */
  227. return 0;
  228. }
  229. module_init(sharpsl_nand_init);
  230. /*
  231. * Clean up routine
  232. */
  233. #ifdef MODULE
  234. static void __exit sharpsl_nand_cleanup(void)
  235. {
  236. struct nand_chip *this = (struct nand_chip *) &sharpsl_mtd[1];
  237. /* Release resources, unregister device */
  238. nand_release(sharpsl_mtd);
  239. iounmap(sharpsl_io_base);
  240. /* Free the MTD device structure */
  241. kfree(sharpsl_mtd);
  242. }
  243. module_exit(sharpsl_nand_cleanup);
  244. #endif
  245. MODULE_LICENSE("GPL");
  246. MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
  247. MODULE_DESCRIPTION("Device specific logic for NAND flash on Sharp SL-C7xx Series");