sharpsl.c 6.7 KB

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