sharpsl.c 5.6 KB

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