4 * Copyright (C) 2007 MontaVista Software, Inc.
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.
15 #if defined(CONFIG_CMD_NAND) && defined(CONFIG_NAND_FSL_UPM)
18 #include <asm/errno.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/fsl_upm.h>
23 static int fsl_upm_in_pattern;
25 static void fsl_upm_start_pattern(struct fsl_upm *upm, u32 pat_offset)
27 clrsetbits_be32(upm->mxmr, MxMR_MAD_MSK, MxMR_OP_RUNP | pat_offset);
30 static void fsl_upm_end_pattern(struct fsl_upm *upm)
32 clrbits_be32(upm->mxmr, MxMR_OP_RUNP);
34 while (in_be32(upm->mxmr) & MxMR_OP_RUNP)
38 static void fsl_upm_run_pattern(struct fsl_upm *upm, int width, u32 cmd)
40 out_be32(upm->mar, cmd << (32 - width));
43 out_8(upm->io_addr, 0x0);
46 out_be16(upm->io_addr, 0x0);
49 out_be32(upm->io_addr, 0x0);
54 static void nand_hwcontrol (struct mtd_info *mtd, int cmd)
56 struct nand_chip *chip = mtd->priv;
57 struct fsl_upm_nand *fun = chip->priv;
61 fsl_upm_start_pattern(&fun->upm, fun->upm_cmd_offset);
65 fsl_upm_start_pattern(&fun->upm, fun->upm_addr_offset);
70 fsl_upm_end_pattern(&fun->upm);
76 static void nand_write_byte(struct mtd_info *mtd, u_char byte)
78 struct nand_chip *chip = mtd->priv;
80 if (fsl_upm_in_pattern) {
81 struct fsl_upm_nand *fun = chip->priv;
83 fsl_upm_run_pattern(&fun->upm, fun->width, byte);
86 * Some boards/chips needs this. At least on MPC8360E-RDK we
87 * need it. Probably weird chip, because I don't see any need
88 * for this on MPC8555E + Samsung K9F1G08U0A. Usually here are
89 * 0-2 unexpected busy states per block read.
91 if (fun->wait_pattern) {
92 while (!fun->dev_ready())
93 debug("unexpected busy state\n");
96 out_8(chip->IO_ADDR_W, byte);
100 static u8 nand_read_byte(struct mtd_info *mtd)
102 struct nand_chip *chip = mtd->priv;
104 return in_8(chip->IO_ADDR_R);
107 static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
110 struct nand_chip *chip = mtd->priv;
112 for (i = 0; i < len; i++)
113 out_8(chip->IO_ADDR_W, buf[i]);
116 static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
119 struct nand_chip *chip = mtd->priv;
121 for (i = 0; i < len; i++)
122 buf[i] = in_8(chip->IO_ADDR_R);
125 static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
128 struct nand_chip *chip = mtd->priv;
130 for (i = 0; i < len; i++) {
131 if (buf[i] != in_8(chip->IO_ADDR_R))
138 static int nand_dev_ready(struct mtd_info *mtd)
140 struct nand_chip *chip = mtd->priv;
141 struct fsl_upm_nand *fun = chip->priv;
143 return fun->dev_ready();
146 int fsl_upm_nand_init(struct nand_chip *chip, struct fsl_upm_nand *fun)
148 if (fun->width != 8 && fun->width != 16 && fun->width != 32)
152 chip->chip_delay = fun->chip_delay;
153 chip->eccmode = NAND_ECC_SOFT;
154 chip->hwcontrol = nand_hwcontrol;
155 chip->read_byte = nand_read_byte;
156 chip->read_buf = nand_read_buf;
157 chip->write_byte = nand_write_byte;
158 chip->write_buf = nand_write_buf;
159 chip->verify_buf = nand_verify_buf;
161 chip->dev_ready = nand_dev_ready;
165 #endif /* CONFIG_CMD_NAND */