fsl_upm.c 4.6 KB

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