fsl_upm.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 void fsl_upm_start_pattern(struct fsl_upm *upm, u32 pat_offset)
  21. {
  22. clrsetbits_be32(upm->mxmr, MxMR_MAD_MSK, MxMR_OP_RUNP | pat_offset);
  23. }
  24. static void fsl_upm_end_pattern(struct fsl_upm *upm)
  25. {
  26. clrbits_be32(upm->mxmr, MxMR_OP_RUNP);
  27. while (in_be32(upm->mxmr) & MxMR_OP_RUNP)
  28. eieio();
  29. }
  30. static void fsl_upm_run_pattern(struct fsl_upm *upm, int width, u32 cmd)
  31. {
  32. out_be32(upm->mar, cmd << (32 - width));
  33. switch (width) {
  34. case 8:
  35. out_8(upm->io_addr, 0x0);
  36. break;
  37. case 16:
  38. out_be16(upm->io_addr, 0x0);
  39. break;
  40. case 32:
  41. out_be32(upm->io_addr, 0x0);
  42. break;
  43. }
  44. }
  45. static void fun_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  46. {
  47. struct nand_chip *chip = mtd->priv;
  48. struct fsl_upm_nand *fun = chip->priv;
  49. if (!(ctrl & fun->last_ctrl)) {
  50. fsl_upm_end_pattern(&fun->upm);
  51. if (cmd == NAND_CMD_NONE)
  52. return;
  53. fun->last_ctrl = ctrl & (NAND_ALE | NAND_CLE);
  54. }
  55. if (ctrl & NAND_CTRL_CHANGE) {
  56. if (ctrl & NAND_ALE)
  57. fsl_upm_start_pattern(&fun->upm, fun->upm_addr_offset);
  58. else if (ctrl & NAND_CLE)
  59. fsl_upm_start_pattern(&fun->upm, fun->upm_cmd_offset);
  60. }
  61. fsl_upm_run_pattern(&fun->upm, fun->width, cmd);
  62. /*
  63. * Some boards/chips needs this. At least on MPC8360E-RDK we
  64. * need it. Probably weird chip, because I don't see any need
  65. * for this on MPC8555E + Samsung K9F1G08U0A. Usually here are
  66. * 0-2 unexpected busy states per block read.
  67. */
  68. if (fun->wait_pattern) {
  69. while (!fun->dev_ready())
  70. debug("unexpected busy state\n");
  71. }
  72. }
  73. static u8 nand_read_byte(struct mtd_info *mtd)
  74. {
  75. struct nand_chip *chip = mtd->priv;
  76. return in_8(chip->IO_ADDR_R);
  77. }
  78. static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  79. {
  80. int i;
  81. struct nand_chip *chip = mtd->priv;
  82. for (i = 0; i < len; i++)
  83. out_8(chip->IO_ADDR_W, buf[i]);
  84. }
  85. static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  86. {
  87. int i;
  88. struct nand_chip *chip = mtd->priv;
  89. for (i = 0; i < len; i++)
  90. buf[i] = in_8(chip->IO_ADDR_R);
  91. }
  92. static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  93. {
  94. int i;
  95. struct nand_chip *chip = mtd->priv;
  96. for (i = 0; i < len; i++) {
  97. if (buf[i] != in_8(chip->IO_ADDR_R))
  98. return -EFAULT;
  99. }
  100. return 0;
  101. }
  102. static int nand_dev_ready(struct mtd_info *mtd)
  103. {
  104. struct nand_chip *chip = mtd->priv;
  105. struct fsl_upm_nand *fun = chip->priv;
  106. return fun->dev_ready();
  107. }
  108. int fsl_upm_nand_init(struct nand_chip *chip, struct fsl_upm_nand *fun)
  109. {
  110. if (fun->width != 8 && fun->width != 16 && fun->width != 32)
  111. return -ENOSYS;
  112. fun->last_ctrl = NAND_CLE;
  113. chip->priv = fun;
  114. chip->chip_delay = fun->chip_delay;
  115. chip->ecc.mode = NAND_ECC_SOFT;
  116. chip->cmd_ctrl = fun_cmd_ctrl;
  117. chip->read_byte = nand_read_byte;
  118. chip->read_buf = nand_read_buf;
  119. chip->write_buf = nand_write_buf;
  120. chip->verify_buf = nand_verify_buf;
  121. if (fun->dev_ready)
  122. chip->dev_ready = nand_dev_ready;
  123. return 0;
  124. }
  125. #endif /* CONFIG_CMD_NAND */