sharpsl.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * drivers/mtd/nand/sharpsl.c
  3. *
  4. * Copyright (C) 2004 Richard Purdie
  5. * Copyright (C) 2008 Dmitry Baryshkov
  6. *
  7. * Based on Sharp's NAND driver sharp_sl.c
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. */
  14. #include <linux/genhd.h>
  15. #include <linux/slab.h>
  16. #include <linux/module.h>
  17. #include <linux/delay.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/nand.h>
  20. #include <linux/mtd/nand_ecc.h>
  21. #include <linux/mtd/partitions.h>
  22. #include <linux/mtd/sharpsl.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/platform_device.h>
  25. #include <asm/io.h>
  26. #include <mach/hardware.h>
  27. #include <asm/mach-types.h>
  28. struct sharpsl_nand {
  29. struct mtd_info mtd;
  30. struct nand_chip chip;
  31. void __iomem *io;
  32. };
  33. #define mtd_to_sharpsl(_mtd) container_of(_mtd, struct sharpsl_nand, mtd)
  34. /* register offset */
  35. #define ECCLPLB 0x00 /* line parity 7 - 0 bit */
  36. #define ECCLPUB 0x04 /* line parity 15 - 8 bit */
  37. #define ECCCP 0x08 /* column parity 5 - 0 bit */
  38. #define ECCCNTR 0x0C /* ECC byte counter */
  39. #define ECCCLRR 0x10 /* cleare ECC */
  40. #define FLASHIO 0x14 /* Flash I/O */
  41. #define FLASHCTL 0x18 /* Flash Control */
  42. /* Flash control bit */
  43. #define FLRYBY (1 << 5)
  44. #define FLCE1 (1 << 4)
  45. #define FLWP (1 << 3)
  46. #define FLALE (1 << 2)
  47. #define FLCLE (1 << 1)
  48. #define FLCE0 (1 << 0)
  49. /*
  50. * hardware specific access to control-lines
  51. * ctrl:
  52. * NAND_CNE: bit 0 -> ! bit 0 & 4
  53. * NAND_CLE: bit 1 -> bit 1
  54. * NAND_ALE: bit 2 -> bit 2
  55. *
  56. */
  57. static void sharpsl_nand_hwcontrol(struct mtd_info *mtd, int cmd,
  58. unsigned int ctrl)
  59. {
  60. struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
  61. struct nand_chip *chip = mtd->priv;
  62. if (ctrl & NAND_CTRL_CHANGE) {
  63. unsigned char bits = ctrl & 0x07;
  64. bits |= (ctrl & 0x01) << 4;
  65. bits ^= 0x11;
  66. writeb((readb(sharpsl->io + FLASHCTL) & ~0x17) | bits, sharpsl->io + FLASHCTL);
  67. }
  68. if (cmd != NAND_CMD_NONE)
  69. writeb(cmd, chip->IO_ADDR_W);
  70. }
  71. static int sharpsl_nand_dev_ready(struct mtd_info *mtd)
  72. {
  73. struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
  74. return !((readb(sharpsl->io + FLASHCTL) & FLRYBY) == 0);
  75. }
  76. static void sharpsl_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  77. {
  78. struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
  79. writeb(0, sharpsl->io + ECCCLRR);
  80. }
  81. static int sharpsl_nand_calculate_ecc(struct mtd_info *mtd, const u_char * dat, u_char * ecc_code)
  82. {
  83. struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
  84. ecc_code[0] = ~readb(sharpsl->io + ECCLPUB);
  85. ecc_code[1] = ~readb(sharpsl->io + ECCLPLB);
  86. ecc_code[2] = (~readb(sharpsl->io + ECCCP) << 2) | 0x03;
  87. return readb(sharpsl->io + ECCCNTR) != 0;
  88. }
  89. /*
  90. * Main initialization routine
  91. */
  92. static int __devinit sharpsl_nand_probe(struct platform_device *pdev)
  93. {
  94. struct nand_chip *this;
  95. struct resource *r;
  96. int err = 0;
  97. struct sharpsl_nand *sharpsl;
  98. struct sharpsl_nand_platform_data *data = pdev->dev.platform_data;
  99. if (!data) {
  100. dev_err(&pdev->dev, "no platform data!\n");
  101. return -EINVAL;
  102. }
  103. /* Allocate memory for MTD device structure and private data */
  104. sharpsl = kzalloc(sizeof(struct sharpsl_nand), GFP_KERNEL);
  105. if (!sharpsl) {
  106. printk("Unable to allocate SharpSL NAND MTD device structure.\n");
  107. return -ENOMEM;
  108. }
  109. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  110. if (!r) {
  111. dev_err(&pdev->dev, "no io memory resource defined!\n");
  112. err = -ENODEV;
  113. goto err_get_res;
  114. }
  115. /* map physical address */
  116. sharpsl->io = ioremap(r->start, resource_size(r));
  117. if (!sharpsl->io) {
  118. printk("ioremap to access Sharp SL NAND chip failed\n");
  119. err = -EIO;
  120. goto err_ioremap;
  121. }
  122. /* Get pointer to private data */
  123. this = (struct nand_chip *)(&sharpsl->chip);
  124. /* Link the private data with the MTD structure */
  125. sharpsl->mtd.priv = this;
  126. sharpsl->mtd.owner = THIS_MODULE;
  127. platform_set_drvdata(pdev, sharpsl);
  128. /*
  129. * PXA initialize
  130. */
  131. writeb(readb(sharpsl->io + FLASHCTL) | FLWP, sharpsl->io + FLASHCTL);
  132. /* Set address of NAND IO lines */
  133. this->IO_ADDR_R = sharpsl->io + FLASHIO;
  134. this->IO_ADDR_W = sharpsl->io + FLASHIO;
  135. /* Set address of hardware control function */
  136. this->cmd_ctrl = sharpsl_nand_hwcontrol;
  137. this->dev_ready = sharpsl_nand_dev_ready;
  138. /* 15 us command delay time */
  139. this->chip_delay = 15;
  140. /* set eccmode using hardware ECC */
  141. this->ecc.mode = NAND_ECC_HW;
  142. this->ecc.size = 256;
  143. this->ecc.bytes = 3;
  144. this->ecc.strength = 1;
  145. this->badblock_pattern = data->badblock_pattern;
  146. this->ecc.layout = data->ecc_layout;
  147. this->ecc.hwctl = sharpsl_nand_enable_hwecc;
  148. this->ecc.calculate = sharpsl_nand_calculate_ecc;
  149. this->ecc.correct = nand_correct_data;
  150. /* Scan to find existence of the device */
  151. err = nand_scan(&sharpsl->mtd, 1);
  152. if (err)
  153. goto err_scan;
  154. /* Register the partitions */
  155. sharpsl->mtd.name = "sharpsl-nand";
  156. err = mtd_device_parse_register(&sharpsl->mtd, NULL, NULL,
  157. data->partitions, data->nr_partitions);
  158. if (err)
  159. goto err_add;
  160. /* Return happy */
  161. return 0;
  162. err_add:
  163. nand_release(&sharpsl->mtd);
  164. err_scan:
  165. platform_set_drvdata(pdev, NULL);
  166. iounmap(sharpsl->io);
  167. err_ioremap:
  168. err_get_res:
  169. kfree(sharpsl);
  170. return err;
  171. }
  172. /*
  173. * Clean up routine
  174. */
  175. static int __devexit sharpsl_nand_remove(struct platform_device *pdev)
  176. {
  177. struct sharpsl_nand *sharpsl = platform_get_drvdata(pdev);
  178. /* Release resources, unregister device */
  179. nand_release(&sharpsl->mtd);
  180. platform_set_drvdata(pdev, NULL);
  181. iounmap(sharpsl->io);
  182. /* Free the MTD device structure */
  183. kfree(sharpsl);
  184. return 0;
  185. }
  186. static struct platform_driver sharpsl_nand_driver = {
  187. .driver = {
  188. .name = "sharpsl-nand",
  189. .owner = THIS_MODULE,
  190. },
  191. .probe = sharpsl_nand_probe,
  192. .remove = __devexit_p(sharpsl_nand_remove),
  193. };
  194. module_platform_driver(sharpsl_nand_driver);
  195. MODULE_LICENSE("GPL");
  196. MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
  197. MODULE_DESCRIPTION("Device specific logic for NAND flash on Sharp SL-C7xx Series");