sharpsl.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 nand_chip chip;
  30. void __iomem *io;
  31. };
  32. static inline struct sharpsl_nand *mtd_to_sharpsl(struct mtd_info *mtd)
  33. {
  34. return container_of(mtd_to_nand(mtd), struct sharpsl_nand, chip);
  35. }
  36. /* register offset */
  37. #define ECCLPLB 0x00 /* line parity 7 - 0 bit */
  38. #define ECCLPUB 0x04 /* line parity 15 - 8 bit */
  39. #define ECCCP 0x08 /* column parity 5 - 0 bit */
  40. #define ECCCNTR 0x0C /* ECC byte counter */
  41. #define ECCCLRR 0x10 /* cleare ECC */
  42. #define FLASHIO 0x14 /* Flash I/O */
  43. #define FLASHCTL 0x18 /* Flash Control */
  44. /* Flash control bit */
  45. #define FLRYBY (1 << 5)
  46. #define FLCE1 (1 << 4)
  47. #define FLWP (1 << 3)
  48. #define FLALE (1 << 2)
  49. #define FLCLE (1 << 1)
  50. #define FLCE0 (1 << 0)
  51. /*
  52. * hardware specific access to control-lines
  53. * ctrl:
  54. * NAND_CNE: bit 0 -> ! bit 0 & 4
  55. * NAND_CLE: bit 1 -> bit 1
  56. * NAND_ALE: bit 2 -> bit 2
  57. *
  58. */
  59. static void sharpsl_nand_hwcontrol(struct mtd_info *mtd, int cmd,
  60. unsigned int ctrl)
  61. {
  62. struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
  63. struct nand_chip *chip = mtd_to_nand(mtd);
  64. if (ctrl & NAND_CTRL_CHANGE) {
  65. unsigned char bits = ctrl & 0x07;
  66. bits |= (ctrl & 0x01) << 4;
  67. bits ^= 0x11;
  68. writeb((readb(sharpsl->io + FLASHCTL) & ~0x17) | bits, sharpsl->io + FLASHCTL);
  69. }
  70. if (cmd != NAND_CMD_NONE)
  71. writeb(cmd, chip->IO_ADDR_W);
  72. }
  73. static int sharpsl_nand_dev_ready(struct mtd_info *mtd)
  74. {
  75. struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
  76. return !((readb(sharpsl->io + FLASHCTL) & FLRYBY) == 0);
  77. }
  78. static void sharpsl_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  79. {
  80. struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
  81. writeb(0, sharpsl->io + ECCCLRR);
  82. }
  83. static int sharpsl_nand_calculate_ecc(struct mtd_info *mtd, const u_char * dat, u_char * ecc_code)
  84. {
  85. struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
  86. ecc_code[0] = ~readb(sharpsl->io + ECCLPUB);
  87. ecc_code[1] = ~readb(sharpsl->io + ECCLPLB);
  88. ecc_code[2] = (~readb(sharpsl->io + ECCCP) << 2) | 0x03;
  89. return readb(sharpsl->io + ECCCNTR) != 0;
  90. }
  91. /*
  92. * Main initialization routine
  93. */
  94. static int sharpsl_nand_probe(struct platform_device *pdev)
  95. {
  96. struct nand_chip *this;
  97. struct mtd_info *mtd;
  98. struct resource *r;
  99. int err = 0;
  100. struct sharpsl_nand *sharpsl;
  101. struct sharpsl_nand_platform_data *data = dev_get_platdata(&pdev->dev);
  102. if (!data) {
  103. dev_err(&pdev->dev, "no platform data!\n");
  104. return -EINVAL;
  105. }
  106. /* Allocate memory for MTD device structure and private data */
  107. sharpsl = kzalloc(sizeof(struct sharpsl_nand), GFP_KERNEL);
  108. if (!sharpsl)
  109. return -ENOMEM;
  110. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  111. if (!r) {
  112. dev_err(&pdev->dev, "no io memory resource defined!\n");
  113. err = -ENODEV;
  114. goto err_get_res;
  115. }
  116. /* map physical address */
  117. sharpsl->io = ioremap(r->start, resource_size(r));
  118. if (!sharpsl->io) {
  119. dev_err(&pdev->dev, "ioremap to access Sharp SL NAND chip failed\n");
  120. err = -EIO;
  121. goto err_ioremap;
  122. }
  123. /* Get pointer to private data */
  124. this = (struct nand_chip *)(&sharpsl->chip);
  125. /* Link the private data with the MTD structure */
  126. mtd = nand_to_mtd(this);
  127. mtd->dev.parent = &pdev->dev;
  128. mtd_set_ooblayout(mtd, data->ecc_layout);
  129. platform_set_drvdata(pdev, sharpsl);
  130. /*
  131. * PXA initialize
  132. */
  133. writeb(readb(sharpsl->io + FLASHCTL) | FLWP, sharpsl->io + FLASHCTL);
  134. /* Set address of NAND IO lines */
  135. this->IO_ADDR_R = sharpsl->io + FLASHIO;
  136. this->IO_ADDR_W = sharpsl->io + FLASHIO;
  137. /* Set address of hardware control function */
  138. this->cmd_ctrl = sharpsl_nand_hwcontrol;
  139. this->dev_ready = sharpsl_nand_dev_ready;
  140. /* 15 us command delay time */
  141. this->chip_delay = 15;
  142. /* set eccmode using hardware ECC */
  143. this->ecc.mode = NAND_ECC_HW;
  144. this->ecc.size = 256;
  145. this->ecc.bytes = 3;
  146. this->ecc.strength = 1;
  147. this->badblock_pattern = data->badblock_pattern;
  148. this->ecc.hwctl = sharpsl_nand_enable_hwecc;
  149. this->ecc.calculate = sharpsl_nand_calculate_ecc;
  150. this->ecc.correct = nand_correct_data;
  151. /* Scan to find existence of the device */
  152. err = nand_scan(mtd, 1);
  153. if (err)
  154. goto err_scan;
  155. /* Register the partitions */
  156. mtd->name = "sharpsl-nand";
  157. err = mtd_device_parse_register(mtd, NULL, NULL,
  158. data->partitions, data->nr_partitions);
  159. if (err)
  160. goto err_add;
  161. /* Return happy */
  162. return 0;
  163. err_add:
  164. nand_release(mtd);
  165. err_scan:
  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 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(nand_to_mtd(&sharpsl->chip));
  180. iounmap(sharpsl->io);
  181. /* Free the MTD device structure */
  182. kfree(sharpsl);
  183. return 0;
  184. }
  185. static struct platform_driver sharpsl_nand_driver = {
  186. .driver = {
  187. .name = "sharpsl-nand",
  188. },
  189. .probe = sharpsl_nand_probe,
  190. .remove = sharpsl_nand_remove,
  191. };
  192. module_platform_driver(sharpsl_nand_driver);
  193. MODULE_LICENSE("GPL");
  194. MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
  195. MODULE_DESCRIPTION("Device specific logic for NAND flash on Sharp SL-C7xx Series");