blob: 31c174bd3bc05bd18532f9ac820ee16d4acd248b [file] [log] [blame]
Anton Vorontsovcd9d2302008-01-14 23:09:32 +03001/*
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
13#include <config.h>
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030014#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
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030021static void fsl_upm_start_pattern(struct fsl_upm *upm, u32 pat_offset)
22{
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +020023 clrsetbits_be32(upm->mxmr, MxMR_MAD_MSK, MxMR_OP_RUNP | pat_offset);
John Schmoller9fd84912010-12-02 11:43:10 -060024 (void)in_be32(upm->mxmr);
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030025}
26
27static void fsl_upm_end_pattern(struct fsl_upm *upm)
28{
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +020029 clrbits_be32(upm->mxmr, MxMR_OP_RUNP);
30
31 while (in_be32(upm->mxmr) & MxMR_OP_RUNP)
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030032 eieio();
33}
34
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010035static void fsl_upm_run_pattern(struct fsl_upm *upm, int width,
36 void __iomem *io_addr, u32 mar)
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030037{
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010038 out_be32(upm->mar, mar);
John Schmoller9fd84912010-12-02 11:43:10 -060039 (void)in_be32(upm->mar);
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +020040 switch (width) {
41 case 8:
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010042 out_8(io_addr, 0x0);
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +020043 break;
44 case 16:
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010045 out_be16(io_addr, 0x0);
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +020046 break;
47 case 32:
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010048 out_be32(io_addr, 0x0);
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +020049 break;
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030050 }
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030051}
52
Wolfgang Grandegger33846df2009-02-11 18:38:23 +010053static void fun_wait(struct fsl_upm_nand *fun)
54{
55 if (fun->dev_ready) {
56 while (!fun->dev_ready(fun->chip_nr))
57 debug("unexpected busy state\n");
58 } else {
59 /*
60 * If the R/B pin is not connected, like on the TQM8548,
61 * a short delay is necessary.
62 */
63 udelay(1);
64 }
65}
66
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010067#if CONFIG_SYS_NAND_MAX_CHIPS > 1
68static void fun_select_chip(struct mtd_info *mtd, int chip_nr)
69{
70 struct nand_chip *chip = mtd->priv;
71 struct fsl_upm_nand *fun = chip->priv;
72
73 if (chip_nr >= 0) {
74 fun->chip_nr = chip_nr;
75 chip->IO_ADDR_R = chip->IO_ADDR_W =
76 fun->upm.io_addr + fun->chip_offset * chip_nr;
77 } else if (chip_nr == -1) {
78 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
79 }
80}
81#endif
82
Anton Vorontsove1c3dba2008-06-12 11:10:21 -050083static void fun_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030084{
85 struct nand_chip *chip = mtd->priv;
86 struct fsl_upm_nand *fun = chip->priv;
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010087 void __iomem *io_addr;
88 u32 mar;
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030089
Anton Vorontsove1c3dba2008-06-12 11:10:21 -050090 if (!(ctrl & fun->last_ctrl)) {
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +020091 fsl_upm_end_pattern(&fun->upm);
Anton Vorontsove1c3dba2008-06-12 11:10:21 -050092
93 if (cmd == NAND_CMD_NONE)
94 return;
95
96 fun->last_ctrl = ctrl & (NAND_ALE | NAND_CLE);
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030097 }
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030098
Anton Vorontsove1c3dba2008-06-12 11:10:21 -050099 if (ctrl & NAND_CTRL_CHANGE) {
100 if (ctrl & NAND_ALE)
101 fsl_upm_start_pattern(&fun->upm, fun->upm_addr_offset);
102 else if (ctrl & NAND_CLE)
103 fsl_upm_start_pattern(&fun->upm, fun->upm_cmd_offset);
104 }
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300105
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +0100106 mar = cmd << (32 - fun->width);
107 io_addr = fun->upm.io_addr;
108#if CONFIG_SYS_NAND_MAX_CHIPS > 1
Wolfgang Grandegger06e9f7d2009-02-11 18:38:22 +0100109 if (fun->chip_nr > 0) {
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +0100110 io_addr += fun->chip_offset * fun->chip_nr;
Wolfgang Grandegger06e9f7d2009-02-11 18:38:22 +0100111 if (fun->upm_mar_chip_offset)
112 mar |= fun->upm_mar_chip_offset * fun->chip_nr;
113 }
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +0100114#endif
115 fsl_upm_run_pattern(&fun->upm, fun->width, io_addr, mar);
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +0200116
Anton Vorontsove1c3dba2008-06-12 11:10:21 -0500117 /*
Wolfgang Grandegger33846df2009-02-11 18:38:23 +0100118 * Some boards/chips needs this. At least the MPC8360E-RDK and
119 * TQM8548 need it. Probably weird chip, because I don't see
120 * any need for this on MPC8555E + Samsung K9F1G08U0A. Usually
121 * here are 0-2 unexpected busy states per block read.
Anton Vorontsove1c3dba2008-06-12 11:10:21 -0500122 */
Wolfgang Grandegger33846df2009-02-11 18:38:23 +0100123 if (fun->wait_flags & FSL_UPM_WAIT_RUN_PATTERN)
124 fun_wait(fun);
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300125}
126
Marek Vasut24dd8632011-10-04 00:56:07 +0200127static u8 upm_nand_read_byte(struct mtd_info *mtd)
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300128{
129 struct nand_chip *chip = mtd->priv;
130
131 return in_8(chip->IO_ADDR_R);
132}
133
Marek Vasut24dd8632011-10-04 00:56:07 +0200134static void upm_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300135{
136 int i;
137 struct nand_chip *chip = mtd->priv;
Wolfgang Grandegger33846df2009-02-11 18:38:23 +0100138 struct fsl_upm_nand *fun = chip->priv;
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300139
Wolfgang Grandegger33846df2009-02-11 18:38:23 +0100140 for (i = 0; i < len; i++) {
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300141 out_8(chip->IO_ADDR_W, buf[i]);
Wolfgang Grandegger33846df2009-02-11 18:38:23 +0100142 if (fun->wait_flags & FSL_UPM_WAIT_WRITE_BYTE)
143 fun_wait(fun);
144 }
145
146 if (fun->wait_flags & FSL_UPM_WAIT_WRITE_BUFFER)
147 fun_wait(fun);
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300148}
149
Marek Vasut24dd8632011-10-04 00:56:07 +0200150static void upm_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300151{
152 int i;
153 struct nand_chip *chip = mtd->priv;
154
155 for (i = 0; i < len; i++)
156 buf[i] = in_8(chip->IO_ADDR_R);
157}
158
Marek Vasut24dd8632011-10-04 00:56:07 +0200159static int upm_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300160{
161 int i;
162 struct nand_chip *chip = mtd->priv;
163
164 for (i = 0; i < len; i++) {
165 if (buf[i] != in_8(chip->IO_ADDR_R))
166 return -EFAULT;
167 }
168
169 return 0;
170}
171
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300172static int nand_dev_ready(struct mtd_info *mtd)
173{
174 struct nand_chip *chip = mtd->priv;
175 struct fsl_upm_nand *fun = chip->priv;
176
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +0100177 return fun->dev_ready(fun->chip_nr);
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300178}
179
180int fsl_upm_nand_init(struct nand_chip *chip, struct fsl_upm_nand *fun)
181{
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +0200182 if (fun->width != 8 && fun->width != 16 && fun->width != 32)
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300183 return -ENOSYS;
184
Anton Vorontsove1c3dba2008-06-12 11:10:21 -0500185 fun->last_ctrl = NAND_CLE;
186
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300187 chip->priv = fun;
188 chip->chip_delay = fun->chip_delay;
Anton Vorontsove1c3dba2008-06-12 11:10:21 -0500189 chip->ecc.mode = NAND_ECC_SOFT;
190 chip->cmd_ctrl = fun_cmd_ctrl;
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +0100191#if CONFIG_SYS_NAND_MAX_CHIPS > 1
192 chip->select_chip = fun_select_chip;
193#endif
Marek Vasut24dd8632011-10-04 00:56:07 +0200194 chip->read_byte = upm_nand_read_byte;
195 chip->read_buf = upm_nand_read_buf;
196 chip->write_buf = upm_nand_write_buf;
197 chip->verify_buf = upm_nand_verify_buf;
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +0200198 if (fun->dev_ready)
199 chip->dev_ready = nand_dev_ready;
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300200
201 return 0;
202}