fsl_upm.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * FSL UPM NAND driver
  3. *
  4. * Copyright (C) 2007 MontaVista Software, Inc.
  5. * Anton Vorontsov <avorontsov@ru.mvista.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. */
  12. #include <config.h>
  13. #if defined(CONFIG_CMD_NAND) && defined(CONFIG_NAND_FSL_UPM)
  14. #include <common.h>
  15. #include <asm/io.h>
  16. #include <asm/errno.h>
  17. #include <linux/mtd/mtd.h>
  18. #include <linux/mtd/fsl_upm.h>
  19. #include <nand.h>
  20. static int fsl_upm_in_pattern;
  21. static void fsl_upm_start_pattern(struct fsl_upm *upm, u32 pat_offset)
  22. {
  23. clrsetbits_be32(upm->mxmr, MxMR_MAD_MSK, MxMR_OP_RUNP | pat_offset);
  24. }
  25. static void fsl_upm_end_pattern(struct fsl_upm *upm)
  26. {
  27. clrbits_be32(upm->mxmr, MxMR_OP_RUNP);
  28. while (in_be32(upm->mxmr) & MxMR_OP_RUNP)
  29. eieio();
  30. }
  31. static void fsl_upm_run_pattern(struct fsl_upm *upm, int width, u32 cmd)
  32. {
  33. out_be32(upm->mar, cmd << (32 - width));
  34. switch (width) {
  35. case 8:
  36. out_8(upm->io_addr, 0x0);
  37. break;
  38. case 16:
  39. out_be16(upm->io_addr, 0x0);
  40. break;
  41. case 32:
  42. out_be32(upm->io_addr, 0x0);
  43. break;
  44. }
  45. }
  46. static void nand_hwcontrol (struct mtd_info *mtd, int cmd)
  47. {
  48. struct nand_chip *chip = mtd->priv;
  49. struct fsl_upm_nand *fun = chip->priv;
  50. switch (cmd) {
  51. case NAND_CTL_SETCLE:
  52. fsl_upm_start_pattern(&fun->upm, fun->upm_cmd_offset);
  53. fsl_upm_in_pattern++;
  54. break;
  55. case NAND_CTL_SETALE:
  56. fsl_upm_start_pattern(&fun->upm, fun->upm_addr_offset);
  57. fsl_upm_in_pattern++;
  58. break;
  59. case NAND_CTL_CLRCLE:
  60. case NAND_CTL_CLRALE:
  61. fsl_upm_end_pattern(&fun->upm);
  62. fsl_upm_in_pattern--;
  63. break;
  64. }
  65. }
  66. static void nand_write_byte(struct mtd_info *mtd, u_char byte)
  67. {
  68. struct nand_chip *chip = mtd->priv;
  69. if (fsl_upm_in_pattern) {
  70. struct fsl_upm_nand *fun = chip->priv;
  71. fsl_upm_run_pattern(&fun->upm, fun->width, byte);
  72. /*
  73. * Some boards/chips needs this. At least on MPC8360E-RDK we
  74. * need it. Probably weird chip, because I don't see any need
  75. * for this on MPC8555E + Samsung K9F1G08U0A. Usually here are
  76. * 0-2 unexpected busy states per block read.
  77. */
  78. if (fun->wait_pattern) {
  79. while (!fun->dev_ready())
  80. debug("unexpected busy state\n");
  81. }
  82. } else {
  83. out_8(chip->IO_ADDR_W, byte);
  84. }
  85. }
  86. static u8 nand_read_byte(struct mtd_info *mtd)
  87. {
  88. struct nand_chip *chip = mtd->priv;
  89. return in_8(chip->IO_ADDR_R);
  90. }
  91. static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  92. {
  93. int i;
  94. struct nand_chip *chip = mtd->priv;
  95. for (i = 0; i < len; i++)
  96. out_8(chip->IO_ADDR_W, buf[i]);
  97. }
  98. static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  99. {
  100. int i;
  101. struct nand_chip *chip = mtd->priv;
  102. for (i = 0; i < len; i++)
  103. buf[i] = in_8(chip->IO_ADDR_R);
  104. }
  105. static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  106. {
  107. int i;
  108. struct nand_chip *chip = mtd->priv;
  109. for (i = 0; i < len; i++) {
  110. if (buf[i] != in_8(chip->IO_ADDR_R))
  111. return -EFAULT;
  112. }
  113. return 0;
  114. }
  115. static int nand_dev_ready(struct mtd_info *mtd)
  116. {
  117. struct nand_chip *chip = mtd->priv;
  118. struct fsl_upm_nand *fun = chip->priv;
  119. return fun->dev_ready();
  120. }
  121. int fsl_upm_nand_init(struct nand_chip *chip, struct fsl_upm_nand *fun)
  122. {
  123. if (fun->width != 8 && fun->width != 16 && fun->width != 32)
  124. return -ENOSYS;
  125. chip->priv = fun;
  126. chip->chip_delay = fun->chip_delay;
  127. chip->eccmode = NAND_ECC_SOFT;
  128. chip->hwcontrol = nand_hwcontrol;
  129. chip->read_byte = nand_read_byte;
  130. chip->read_buf = nand_read_buf;
  131. chip->write_byte = nand_write_byte;
  132. chip->write_buf = nand_write_buf;
  133. chip->verify_buf = nand_verify_buf;
  134. if (fun->dev_ready)
  135. chip->dev_ready = nand_dev_ready;
  136. return 0;
  137. }
  138. #endif /* CONFIG_CMD_NAND */