sharpsl.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. * ctrl:
  73. * NAND_CNE: bit 0 -> ! bit 0 & 4
  74. * NAND_CLE: bit 1 -> bit 1
  75. * NAND_ALE: bit 2 -> bit 2
  76. *
  77. */
  78. static void sharpsl_nand_hwcontrol(struct mtd_info *mtd, int cmd,
  79. unsigned int ctrl)
  80. {
  81. struct nand_chip *chip = mtd->priv;
  82. if (ctrl & NAND_CTRL_CHANGE) {
  83. unsigned char bits = ctrl & 0x07;
  84. bits |= (ctrl & 0x01) << 4;
  85. bits ^= 0x11;
  86. writeb((readb(FLASHCTL) & ~0x17) | bits, FLASHCTL);
  87. }
  88. if (cmd != NAND_CMD_NONE)
  89. writeb(cmd, chip->IO_ADDR_W);
  90. }
  91. static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
  92. static struct nand_bbt_descr sharpsl_bbt = {
  93. .options = 0,
  94. .offs = 4,
  95. .len = 2,
  96. .pattern = scan_ff_pattern
  97. };
  98. static struct nand_bbt_descr sharpsl_akita_bbt = {
  99. .options = 0,
  100. .offs = 4,
  101. .len = 1,
  102. .pattern = scan_ff_pattern
  103. };
  104. static struct nand_ecclayout akita_oobinfo = {
  105. .eccbytes = 24,
  106. .eccpos = {
  107. 0x5, 0x1, 0x2, 0x3, 0x6, 0x7, 0x15, 0x11,
  108. 0x12, 0x13, 0x16, 0x17, 0x25, 0x21, 0x22, 0x23,
  109. 0x26, 0x27, 0x35, 0x31, 0x32, 0x33, 0x36, 0x37},
  110. .oobfree = {{0x08, 0x09}}
  111. };
  112. static int sharpsl_nand_dev_ready(struct mtd_info *mtd)
  113. {
  114. return !((readb(FLASHCTL) & FLRYBY) == 0);
  115. }
  116. static void sharpsl_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  117. {
  118. writeb(0, ECCCLRR);
  119. }
  120. static int sharpsl_nand_calculate_ecc(struct mtd_info *mtd, const u_char * dat, u_char * ecc_code)
  121. {
  122. ecc_code[0] = ~readb(ECCLPUB);
  123. ecc_code[1] = ~readb(ECCLPLB);
  124. ecc_code[2] = (~readb(ECCCP) << 2) | 0x03;
  125. return readb(ECCCNTR) != 0;
  126. }
  127. #ifdef CONFIG_MTD_PARTITIONS
  128. const char *part_probes[] = { "cmdlinepart", NULL };
  129. #endif
  130. /*
  131. * Main initialization routine
  132. */
  133. static int __init sharpsl_nand_init(void)
  134. {
  135. struct nand_chip *this;
  136. struct mtd_partition *sharpsl_partition_info;
  137. int err = 0;
  138. /* Allocate memory for MTD device structure and private data */
  139. sharpsl_mtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
  140. if (!sharpsl_mtd) {
  141. printk("Unable to allocate SharpSL NAND MTD device structure.\n");
  142. return -ENOMEM;
  143. }
  144. /* map physical adress */
  145. sharpsl_io_base = ioremap(sharpsl_phys_base, 0x1000);
  146. if (!sharpsl_io_base) {
  147. printk("ioremap to access Sharp SL NAND chip failed\n");
  148. kfree(sharpsl_mtd);
  149. return -EIO;
  150. }
  151. /* Get pointer to private data */
  152. this = (struct nand_chip *)(&sharpsl_mtd[1]);
  153. /* Initialize structures */
  154. memset(sharpsl_mtd, 0, sizeof(struct mtd_info));
  155. memset(this, 0, sizeof(struct nand_chip));
  156. /* Link the private data with the MTD structure */
  157. sharpsl_mtd->priv = this;
  158. sharpsl_mtd->owner = THIS_MODULE;
  159. /*
  160. * PXA initialize
  161. */
  162. writeb(readb(FLASHCTL) | FLWP, FLASHCTL);
  163. /* Set address of NAND IO lines */
  164. this->IO_ADDR_R = FLASHIO;
  165. this->IO_ADDR_W = FLASHIO;
  166. /* Set address of hardware control function */
  167. this->cmd_ctrl = sharpsl_nand_hwcontrol;
  168. this->dev_ready = sharpsl_nand_dev_ready;
  169. /* 15 us command delay time */
  170. this->chip_delay = 15;
  171. /* set eccmode using hardware ECC */
  172. this->ecc.mode = NAND_ECC_HW;
  173. this->ecc.size = 256;
  174. this->ecc.bytes = 3;
  175. this->badblock_pattern = &sharpsl_bbt;
  176. if (machine_is_akita() || machine_is_borzoi()) {
  177. this->badblock_pattern = &sharpsl_akita_bbt;
  178. this->ecc.layout = &akita_oobinfo;
  179. }
  180. this->ecc.hwctl = sharpsl_nand_enable_hwecc;
  181. this->ecc.calculate = sharpsl_nand_calculate_ecc;
  182. this->ecc.correct = nand_correct_data;
  183. /* Scan to find existence of the device */
  184. err = nand_scan(sharpsl_mtd, 1);
  185. if (err) {
  186. iounmap(sharpsl_io_base);
  187. kfree(sharpsl_mtd);
  188. return err;
  189. }
  190. /* Register the partitions */
  191. sharpsl_mtd->name = "sharpsl-nand";
  192. nr_partitions = parse_mtd_partitions(sharpsl_mtd, part_probes, &sharpsl_partition_info, 0);
  193. if (nr_partitions <= 0) {
  194. nr_partitions = DEFAULT_NUM_PARTITIONS;
  195. sharpsl_partition_info = sharpsl_nand_default_partition_info;
  196. if (machine_is_poodle()) {
  197. sharpsl_partition_info[1].size = 22 * 1024 * 1024;
  198. } else if (machine_is_corgi() || machine_is_shepherd()) {
  199. sharpsl_partition_info[1].size = 25 * 1024 * 1024;
  200. } else if (machine_is_husky()) {
  201. sharpsl_partition_info[1].size = 53 * 1024 * 1024;
  202. } else if (machine_is_spitz()) {
  203. sharpsl_partition_info[1].size = 5 * 1024 * 1024;
  204. } else if (machine_is_akita()) {
  205. sharpsl_partition_info[1].size = 58 * 1024 * 1024;
  206. } else if (machine_is_borzoi()) {
  207. sharpsl_partition_info[1].size = 32 * 1024 * 1024;
  208. }
  209. }
  210. add_mtd_partitions(sharpsl_mtd, sharpsl_partition_info, nr_partitions);
  211. /* Return happy */
  212. return 0;
  213. }
  214. module_init(sharpsl_nand_init);
  215. /*
  216. * Clean up routine
  217. */
  218. static void __exit sharpsl_nand_cleanup(void)
  219. {
  220. /* Release resources, unregister device */
  221. nand_release(sharpsl_mtd);
  222. iounmap(sharpsl_io_base);
  223. /* Free the MTD device structure */
  224. kfree(sharpsl_mtd);
  225. }
  226. module_exit(sharpsl_nand_cleanup);
  227. MODULE_LICENSE("GPL");
  228. MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
  229. MODULE_DESCRIPTION("Device specific logic for NAND flash on Sharp SL-C7xx Series");