sharpsl.c 6.7 KB

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