fsl_upm.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. (void)in_be32(upm->mxmr);
  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,
  31. void __iomem *io_addr, u32 mar)
  32. {
  33. out_be32(upm->mar, mar);
  34. (void)in_be32(upm->mar);
  35. switch (width) {
  36. case 8:
  37. out_8(io_addr, 0x0);
  38. break;
  39. case 16:
  40. out_be16(io_addr, 0x0);
  41. break;
  42. case 32:
  43. out_be32(io_addr, 0x0);
  44. break;
  45. }
  46. }
  47. static void fun_wait(struct fsl_upm_nand *fun)
  48. {
  49. if (fun->dev_ready) {
  50. while (!fun->dev_ready(fun->chip_nr))
  51. debug("unexpected busy state\n");
  52. } else {
  53. /*
  54. * If the R/B pin is not connected,
  55. * a short delay is necessary.
  56. */
  57. udelay(1);
  58. }
  59. }
  60. #if CONFIG_SYS_NAND_MAX_CHIPS > 1
  61. static void fun_select_chip(struct mtd_info *mtd, int chip_nr)
  62. {
  63. struct nand_chip *chip = mtd->priv;
  64. struct fsl_upm_nand *fun = chip->priv;
  65. if (chip_nr >= 0) {
  66. fun->chip_nr = chip_nr;
  67. chip->IO_ADDR_R = chip->IO_ADDR_W =
  68. fun->upm.io_addr + fun->chip_offset * chip_nr;
  69. } else if (chip_nr == -1) {
  70. chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
  71. }
  72. }
  73. #endif
  74. static void fun_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  75. {
  76. struct nand_chip *chip = mtd->priv;
  77. struct fsl_upm_nand *fun = chip->priv;
  78. void __iomem *io_addr;
  79. u32 mar;
  80. if (!(ctrl & fun->last_ctrl)) {
  81. fsl_upm_end_pattern(&fun->upm);
  82. if (cmd == NAND_CMD_NONE)
  83. return;
  84. fun->last_ctrl = ctrl & (NAND_ALE | NAND_CLE);
  85. }
  86. if (ctrl & NAND_CTRL_CHANGE) {
  87. if (ctrl & NAND_ALE)
  88. fsl_upm_start_pattern(&fun->upm, fun->upm_addr_offset);
  89. else if (ctrl & NAND_CLE)
  90. fsl_upm_start_pattern(&fun->upm, fun->upm_cmd_offset);
  91. }
  92. mar = cmd << (32 - fun->width);
  93. io_addr = fun->upm.io_addr;
  94. #if CONFIG_SYS_NAND_MAX_CHIPS > 1
  95. if (fun->chip_nr > 0) {
  96. io_addr += fun->chip_offset * fun->chip_nr;
  97. if (fun->upm_mar_chip_offset)
  98. mar |= fun->upm_mar_chip_offset * fun->chip_nr;
  99. }
  100. #endif
  101. fsl_upm_run_pattern(&fun->upm, fun->width, io_addr, mar);
  102. /*
  103. * Some boards/chips needs this. At least the MPC8360E-RDK
  104. * needs it. Probably weird chip, because I don't see any
  105. * need for this on MPC8555E + Samsung K9F1G08U0A. Usually
  106. * here are 0-2 unexpected busy states per block read.
  107. */
  108. if (fun->wait_flags & FSL_UPM_WAIT_RUN_PATTERN)
  109. fun_wait(fun);
  110. }
  111. static u8 upm_nand_read_byte(struct mtd_info *mtd)
  112. {
  113. struct nand_chip *chip = mtd->priv;
  114. return in_8(chip->IO_ADDR_R);
  115. }
  116. static void upm_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  117. {
  118. int i;
  119. struct nand_chip *chip = mtd->priv;
  120. struct fsl_upm_nand *fun = chip->priv;
  121. for (i = 0; i < len; i++) {
  122. out_8(chip->IO_ADDR_W, buf[i]);
  123. if (fun->wait_flags & FSL_UPM_WAIT_WRITE_BYTE)
  124. fun_wait(fun);
  125. }
  126. if (fun->wait_flags & FSL_UPM_WAIT_WRITE_BUFFER)
  127. fun_wait(fun);
  128. }
  129. static void upm_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  130. {
  131. int i;
  132. struct nand_chip *chip = mtd->priv;
  133. for (i = 0; i < len; i++)
  134. buf[i] = in_8(chip->IO_ADDR_R);
  135. }
  136. static int upm_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  137. {
  138. int i;
  139. struct nand_chip *chip = mtd->priv;
  140. for (i = 0; i < len; i++) {
  141. if (buf[i] != in_8(chip->IO_ADDR_R))
  142. return -EFAULT;
  143. }
  144. return 0;
  145. }
  146. static int nand_dev_ready(struct mtd_info *mtd)
  147. {
  148. struct nand_chip *chip = mtd->priv;
  149. struct fsl_upm_nand *fun = chip->priv;
  150. return fun->dev_ready(fun->chip_nr);
  151. }
  152. int fsl_upm_nand_init(struct nand_chip *chip, struct fsl_upm_nand *fun)
  153. {
  154. if (fun->width != 8 && fun->width != 16 && fun->width != 32)
  155. return -ENOSYS;
  156. fun->last_ctrl = NAND_CLE;
  157. chip->priv = fun;
  158. chip->chip_delay = fun->chip_delay;
  159. chip->ecc.mode = NAND_ECC_SOFT;
  160. chip->cmd_ctrl = fun_cmd_ctrl;
  161. #if CONFIG_SYS_NAND_MAX_CHIPS > 1
  162. chip->select_chip = fun_select_chip;
  163. #endif
  164. chip->read_byte = upm_nand_read_byte;
  165. chip->read_buf = upm_nand_read_buf;
  166. chip->write_buf = upm_nand_write_buf;
  167. chip->verify_buf = upm_nand_verify_buf;
  168. if (fun->dev_ready)
  169. chip->dev_ready = nand_dev_ready;
  170. return 0;
  171. }