blob: e7e746b550d6a57c7dd9c34b76d0c643603d874f [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);
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030024}
25
26static void fsl_upm_end_pattern(struct fsl_upm *upm)
27{
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +020028 clrbits_be32(upm->mxmr, MxMR_OP_RUNP);
29
30 while (in_be32(upm->mxmr) & MxMR_OP_RUNP)
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030031 eieio();
32}
33
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010034static void fsl_upm_run_pattern(struct fsl_upm *upm, int width,
35 void __iomem *io_addr, u32 mar)
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030036{
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010037 out_be32(upm->mar, mar);
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +020038 switch (width) {
39 case 8:
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010040 out_8(io_addr, 0x0);
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +020041 break;
42 case 16:
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010043 out_be16(io_addr, 0x0);
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +020044 break;
45 case 32:
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010046 out_be32(io_addr, 0x0);
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +020047 break;
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030048 }
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030049}
50
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010051#if CONFIG_SYS_NAND_MAX_CHIPS > 1
52static void fun_select_chip(struct mtd_info *mtd, int chip_nr)
53{
54 struct nand_chip *chip = mtd->priv;
55 struct fsl_upm_nand *fun = chip->priv;
56
57 if (chip_nr >= 0) {
58 fun->chip_nr = chip_nr;
59 chip->IO_ADDR_R = chip->IO_ADDR_W =
60 fun->upm.io_addr + fun->chip_offset * chip_nr;
61 } else if (chip_nr == -1) {
62 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
63 }
64}
65#endif
66
Anton Vorontsove1c3dba2008-06-12 11:10:21 -050067static void fun_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030068{
69 struct nand_chip *chip = mtd->priv;
70 struct fsl_upm_nand *fun = chip->priv;
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010071 void __iomem *io_addr;
72 u32 mar;
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030073
Anton Vorontsove1c3dba2008-06-12 11:10:21 -050074 if (!(ctrl & fun->last_ctrl)) {
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +020075 fsl_upm_end_pattern(&fun->upm);
Anton Vorontsove1c3dba2008-06-12 11:10:21 -050076
77 if (cmd == NAND_CMD_NONE)
78 return;
79
80 fun->last_ctrl = ctrl & (NAND_ALE | NAND_CLE);
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030081 }
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030082
Anton Vorontsove1c3dba2008-06-12 11:10:21 -050083 if (ctrl & NAND_CTRL_CHANGE) {
84 if (ctrl & NAND_ALE)
85 fsl_upm_start_pattern(&fun->upm, fun->upm_addr_offset);
86 else if (ctrl & NAND_CLE)
87 fsl_upm_start_pattern(&fun->upm, fun->upm_cmd_offset);
88 }
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030089
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010090 mar = cmd << (32 - fun->width);
91 io_addr = fun->upm.io_addr;
92#if CONFIG_SYS_NAND_MAX_CHIPS > 1
Wolfgang Grandegger06e9f7d2009-02-11 18:38:22 +010093 if (fun->chip_nr > 0) {
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010094 io_addr += fun->chip_offset * fun->chip_nr;
Wolfgang Grandegger06e9f7d2009-02-11 18:38:22 +010095 if (fun->upm_mar_chip_offset)
96 mar |= fun->upm_mar_chip_offset * fun->chip_nr;
97 }
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010098#endif
99 fsl_upm_run_pattern(&fun->upm, fun->width, io_addr, mar);
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +0200100
Anton Vorontsove1c3dba2008-06-12 11:10:21 -0500101 /*
102 * Some boards/chips needs this. At least on MPC8360E-RDK we
103 * need it. Probably weird chip, because I don't see any need
104 * for this on MPC8555E + Samsung K9F1G08U0A. Usually here are
105 * 0-2 unexpected busy states per block read.
106 */
107 if (fun->wait_pattern) {
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +0100108 while (!fun->dev_ready(fun->chip_nr))
Anton Vorontsove1c3dba2008-06-12 11:10:21 -0500109 debug("unexpected busy state\n");
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +0200110 }
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300111}
112
113static u8 nand_read_byte(struct mtd_info *mtd)
114{
115 struct nand_chip *chip = mtd->priv;
116
117 return in_8(chip->IO_ADDR_R);
118}
119
120static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
121{
122 int i;
123 struct nand_chip *chip = mtd->priv;
124
125 for (i = 0; i < len; i++)
126 out_8(chip->IO_ADDR_W, buf[i]);
127}
128
129static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
130{
131 int i;
132 struct nand_chip *chip = mtd->priv;
133
134 for (i = 0; i < len; i++)
135 buf[i] = in_8(chip->IO_ADDR_R);
136}
137
138static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
139{
140 int i;
141 struct nand_chip *chip = mtd->priv;
142
143 for (i = 0; i < len; i++) {
144 if (buf[i] != in_8(chip->IO_ADDR_R))
145 return -EFAULT;
146 }
147
148 return 0;
149}
150
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300151static int nand_dev_ready(struct mtd_info *mtd)
152{
153 struct nand_chip *chip = mtd->priv;
154 struct fsl_upm_nand *fun = chip->priv;
155
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +0100156 return fun->dev_ready(fun->chip_nr);
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300157}
158
159int fsl_upm_nand_init(struct nand_chip *chip, struct fsl_upm_nand *fun)
160{
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +0200161 if (fun->width != 8 && fun->width != 16 && fun->width != 32)
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300162 return -ENOSYS;
163
Anton Vorontsove1c3dba2008-06-12 11:10:21 -0500164 fun->last_ctrl = NAND_CLE;
165
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300166 chip->priv = fun;
167 chip->chip_delay = fun->chip_delay;
Anton Vorontsove1c3dba2008-06-12 11:10:21 -0500168 chip->ecc.mode = NAND_ECC_SOFT;
169 chip->cmd_ctrl = fun_cmd_ctrl;
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +0100170#if CONFIG_SYS_NAND_MAX_CHIPS > 1
171 chip->select_chip = fun_select_chip;
172#endif
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300173 chip->read_byte = nand_read_byte;
174 chip->read_buf = nand_read_buf;
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300175 chip->write_buf = nand_write_buf;
176 chip->verify_buf = nand_verify_buf;
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +0200177 if (fun->dev_ready)
178 chip->dev_ready = nand_dev_ready;
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300179
180 return 0;
181}