sharpsl.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * drivers/mtd/nand/sharpsl.c
  3. *
  4. * Copyright (C) 2004 Richard Purdie
  5. *
  6. * $Id: sharpsl.c,v 1.4 2005/01/23 11:09:19 rpurdie 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 int
  105. sharpsl_nand_dev_ready(struct mtd_info* mtd)
  106. {
  107. return !((readb(FLASHCTL) & FLRYBY) == 0);
  108. }
  109. static void
  110. sharpsl_nand_enable_hwecc(struct mtd_info* mtd, int mode)
  111. {
  112. writeb(0 ,ECCCLRR);
  113. }
  114. static int
  115. sharpsl_nand_calculate_ecc(struct mtd_info* mtd, const u_char* dat,
  116. u_char* ecc_code)
  117. {
  118. ecc_code[0] = ~readb(ECCLPUB);
  119. ecc_code[1] = ~readb(ECCLPLB);
  120. ecc_code[2] = (~readb(ECCCP) << 2) | 0x03;
  121. return readb(ECCCNTR) != 0;
  122. }
  123. #ifdef CONFIG_MTD_PARTITIONS
  124. const char *part_probes[] = { "cmdlinepart", NULL };
  125. #endif
  126. /*
  127. * Main initialization routine
  128. */
  129. int __init
  130. sharpsl_nand_init(void)
  131. {
  132. struct nand_chip *this;
  133. struct mtd_partition* sharpsl_partition_info;
  134. int err = 0;
  135. /* Allocate memory for MTD device structure and private data */
  136. sharpsl_mtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip),
  137. GFP_KERNEL);
  138. if (!sharpsl_mtd) {
  139. printk ("Unable to allocate SharpSL NAND MTD device structure.\n");
  140. return -ENOMEM;
  141. }
  142. /* map physical adress */
  143. sharpsl_io_base = ioremap(sharpsl_phys_base, 0x1000);
  144. if(!sharpsl_io_base){
  145. printk("ioremap to access Sharp SL NAND chip failed\n");
  146. kfree(sharpsl_mtd);
  147. return -EIO;
  148. }
  149. /* Get pointer to private data */
  150. this = (struct nand_chip *) (&sharpsl_mtd[1]);
  151. /* Initialize structures */
  152. memset((char *) sharpsl_mtd, 0, sizeof(struct mtd_info));
  153. memset((char *) this, 0, sizeof(struct nand_chip));
  154. /* Link the private data with the MTD structure */
  155. sharpsl_mtd->priv = this;
  156. /*
  157. * PXA initialize
  158. */
  159. writeb(readb(FLASHCTL) | FLWP, FLASHCTL);
  160. /* Set address of NAND IO lines */
  161. this->IO_ADDR_R = FLASHIO;
  162. this->IO_ADDR_W = FLASHIO;
  163. /* Set address of hardware control function */
  164. this->hwcontrol = sharpsl_nand_hwcontrol;
  165. this->dev_ready = sharpsl_nand_dev_ready;
  166. /* 15 us command delay time */
  167. this->chip_delay = 15;
  168. /* set eccmode using hardware ECC */
  169. this->eccmode = NAND_ECC_HW3_256;
  170. this->enable_hwecc = sharpsl_nand_enable_hwecc;
  171. this->calculate_ecc = sharpsl_nand_calculate_ecc;
  172. this->correct_data = nand_correct_data;
  173. this->badblock_pattern = &sharpsl_bbt;
  174. /* Scan to find existence of the device */
  175. err=nand_scan(sharpsl_mtd,1);
  176. if (err) {
  177. iounmap(sharpsl_io_base);
  178. kfree(sharpsl_mtd);
  179. return err;
  180. }
  181. /* Register the partitions */
  182. sharpsl_mtd->name = "sharpsl-nand";
  183. nr_partitions = parse_mtd_partitions(sharpsl_mtd, part_probes,
  184. &sharpsl_partition_info, 0);
  185. if (nr_partitions <= 0) {
  186. nr_partitions = DEFAULT_NUM_PARTITIONS;
  187. sharpsl_partition_info = sharpsl_nand_default_partition_info;
  188. if (machine_is_poodle()) {
  189. sharpsl_partition_info[1].size=30 * 1024 * 1024;
  190. } else if (machine_is_corgi() || machine_is_shepherd()) {
  191. sharpsl_partition_info[1].size=25 * 1024 * 1024;
  192. } else if (machine_is_husky()) {
  193. sharpsl_partition_info[1].size=53 * 1024 * 1024;
  194. }
  195. }
  196. if (machine_is_husky()) {
  197. /* Need to use small eraseblock size for backward compatibility */
  198. sharpsl_mtd->flags |= MTD_NO_VIRTBLOCKS;
  199. }
  200. add_mtd_partitions(sharpsl_mtd, sharpsl_partition_info, nr_partitions);
  201. /* Return happy */
  202. return 0;
  203. }
  204. module_init(sharpsl_nand_init);
  205. /*
  206. * Clean up routine
  207. */
  208. #ifdef MODULE
  209. static void __exit sharpsl_nand_cleanup(void)
  210. {
  211. struct nand_chip *this = (struct nand_chip *) &sharpsl_mtd[1];
  212. /* Release resources, unregister device */
  213. nand_release(sharpsl_mtd);
  214. iounmap(sharpsl_io_base);
  215. /* Free the MTD device structure */
  216. kfree(sharpsl_mtd);
  217. }
  218. module_exit(sharpsl_nand_cleanup);
  219. #endif
  220. MODULE_LICENSE("GPL");
  221. MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
  222. MODULE_DESCRIPTION("Device specific logic for NAND flash on Sharp SL-C7xx Series");