fsl_upm.c 3.4 KB

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