sharpsl.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 <linux/platform_device.h>
  23. #include <asm/io.h>
  24. #include <mach/hardware.h>
  25. #include <asm/mach-types.h>
  26. struct sharpsl_nand {
  27. struct mtd_info mtd;
  28. struct nand_chip chip;
  29. };
  30. static void __iomem *sharpsl_io_base;
  31. /* register offset */
  32. #define ECCLPLB sharpsl_io_base+0x00 /* line parity 7 - 0 bit */
  33. #define ECCLPUB sharpsl_io_base+0x04 /* line parity 15 - 8 bit */
  34. #define ECCCP sharpsl_io_base+0x08 /* column parity 5 - 0 bit */
  35. #define ECCCNTR sharpsl_io_base+0x0C /* ECC byte counter */
  36. #define ECCCLRR sharpsl_io_base+0x10 /* cleare ECC */
  37. #define FLASHIO sharpsl_io_base+0x14 /* Flash I/O */
  38. #define FLASHCTL sharpsl_io_base+0x18 /* Flash Control */
  39. /* Flash control bit */
  40. #define FLRYBY (1 << 5)
  41. #define FLCE1 (1 << 4)
  42. #define FLWP (1 << 3)
  43. #define FLALE (1 << 2)
  44. #define FLCLE (1 << 1)
  45. #define FLCE0 (1 << 0)
  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 __devinit sharpsl_nand_probe(struct platform_device *pdev)
  132. {
  133. struct nand_chip *this;
  134. struct mtd_partition *sharpsl_partition_info;
  135. struct resource *r;
  136. int err = 0;
  137. struct sharpsl_nand *sharpsl;
  138. /* Allocate memory for MTD device structure and private data */
  139. sharpsl = kzalloc(sizeof(struct sharpsl_nand), GFP_KERNEL);
  140. if (!sharpsl) {
  141. printk("Unable to allocate SharpSL NAND MTD device structure.\n");
  142. return -ENOMEM;
  143. }
  144. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  145. if (!r) {
  146. dev_err(&pdev->dev, "no io memory resource defined!\n");
  147. err = -ENODEV;
  148. goto err_get_res;
  149. }
  150. /* map physical address */
  151. sharpsl_io_base = ioremap(r->start, resource_size(r));
  152. if (!sharpsl_io_base) {
  153. printk("ioremap to access Sharp SL NAND chip failed\n");
  154. err = -EIO;
  155. goto err_ioremap;
  156. }
  157. /* Get pointer to private data */
  158. this = (struct nand_chip *)(&sharpsl->chip);
  159. /* Link the private data with the MTD structure */
  160. sharpsl->mtd.priv = this;
  161. sharpsl->mtd.owner = THIS_MODULE;
  162. platform_set_drvdata(pdev, sharpsl);
  163. /*
  164. * PXA initialize
  165. */
  166. writeb(readb(FLASHCTL) | FLWP, FLASHCTL);
  167. /* Set address of NAND IO lines */
  168. this->IO_ADDR_R = FLASHIO;
  169. this->IO_ADDR_W = FLASHIO;
  170. /* Set address of hardware control function */
  171. this->cmd_ctrl = sharpsl_nand_hwcontrol;
  172. this->dev_ready = sharpsl_nand_dev_ready;
  173. /* 15 us command delay time */
  174. this->chip_delay = 15;
  175. /* set eccmode using hardware ECC */
  176. this->ecc.mode = NAND_ECC_HW;
  177. this->ecc.size = 256;
  178. this->ecc.bytes = 3;
  179. this->badblock_pattern = &sharpsl_bbt;
  180. if (machine_is_akita() || machine_is_borzoi()) {
  181. this->badblock_pattern = &sharpsl_akita_bbt;
  182. this->ecc.layout = &akita_oobinfo;
  183. }
  184. this->ecc.hwctl = sharpsl_nand_enable_hwecc;
  185. this->ecc.calculate = sharpsl_nand_calculate_ecc;
  186. this->ecc.correct = nand_correct_data;
  187. /* Scan to find existence of the device */
  188. err = nand_scan(&sharpsl->mtd, 1);
  189. if (err) {
  190. platform_set_drvdata(pdev, NULL);
  191. iounmap(sharpsl_io_base);
  192. kfree(sharpsl);
  193. return err;
  194. }
  195. /* Register the partitions */
  196. sharpsl->mtd.name = "sharpsl-nand";
  197. nr_partitions = parse_mtd_partitions(&sharpsl->mtd, part_probes, &sharpsl_partition_info, 0);
  198. if (nr_partitions <= 0) {
  199. nr_partitions = DEFAULT_NUM_PARTITIONS;
  200. sharpsl_partition_info = sharpsl_nand_default_partition_info;
  201. if (machine_is_poodle()) {
  202. sharpsl_partition_info[1].size = 22 * 1024 * 1024;
  203. } else if (machine_is_corgi() || machine_is_shepherd()) {
  204. sharpsl_partition_info[1].size = 25 * 1024 * 1024;
  205. } else if (machine_is_husky()) {
  206. sharpsl_partition_info[1].size = 53 * 1024 * 1024;
  207. } else if (machine_is_spitz()) {
  208. sharpsl_partition_info[1].size = 5 * 1024 * 1024;
  209. } else if (machine_is_akita()) {
  210. sharpsl_partition_info[1].size = 58 * 1024 * 1024;
  211. } else if (machine_is_borzoi()) {
  212. sharpsl_partition_info[1].size = 32 * 1024 * 1024;
  213. }
  214. }
  215. add_mtd_partitions(&sharpsl->mtd, sharpsl_partition_info, nr_partitions);
  216. /* Return happy */
  217. return 0;
  218. err_ioremap:
  219. err_get_res:
  220. kfree(sharpsl);
  221. return err;
  222. }
  223. /*
  224. * Clean up routine
  225. */
  226. static int __devexit sharpsl_nand_remove(struct platform_device *pdev)
  227. {
  228. struct sharpsl_nand *sharpsl = platform_get_drvdata(pdev);
  229. /* Release resources, unregister device */
  230. nand_release(&sharpsl->mtd);
  231. platform_set_drvdata(pdev, NULL);
  232. iounmap(sharpsl_io_base);
  233. /* Free the MTD device structure */
  234. kfree(sharpsl);
  235. return 0;
  236. }
  237. static struct platform_driver sharpsl_nand_driver = {
  238. .driver = {
  239. .name = "sharpsl-nand",
  240. .owner = THIS_MODULE,
  241. },
  242. .probe = sharpsl_nand_probe,
  243. .remove = __devexit_p(sharpsl_nand_remove),
  244. };
  245. static struct resource sharpsl_nand_resources[] = {
  246. {
  247. .start = 0x0C000000,
  248. .end = 0x0C000FFF,
  249. .flags = IORESOURCE_MEM,
  250. },
  251. };
  252. static struct platform_device sharpsl_nand_device = {
  253. .name = "sharpsl-nand",
  254. .id = -1,
  255. .resource = sharpsl_nand_resources,
  256. .num_resources = ARRAY_SIZE(sharpsl_nand_resources),
  257. };
  258. static int __init sharpsl_nand_init(void)
  259. {
  260. platform_device_register(&sharpsl_nand_device);
  261. return platform_driver_register(&sharpsl_nand_driver);
  262. }
  263. module_init(sharpsl_nand_init);
  264. static void __exit sharpsl_nand_exit(void)
  265. {
  266. platform_driver_unregister(&sharpsl_nand_driver);
  267. platform_device_unregister(&sharpsl_nand_device);
  268. }
  269. module_exit(sharpsl_nand_exit);
  270. MODULE_LICENSE("GPL");
  271. MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
  272. MODULE_DESCRIPTION("Device specific logic for NAND flash on Sharp SL-C7xx Series");