blob: 4a45b8740a732ae2562167341deba8619ea07fe4 [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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Anton Vorontsovcd9d2302008-01-14 23:09:32 +03008 */
9
10#include <config.h>
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030011#include <common.h>
12#include <asm/io.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090013#include <linux/errno.h>
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030014#include <linux/mtd/mtd.h>
15#include <linux/mtd/fsl_upm.h>
16#include <nand.h>
17
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030018static void fsl_upm_start_pattern(struct fsl_upm *upm, u32 pat_offset)
19{
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +020020 clrsetbits_be32(upm->mxmr, MxMR_MAD_MSK, MxMR_OP_RUNP | pat_offset);
John Schmoller9fd84912010-12-02 11:43:10 -060021 (void)in_be32(upm->mxmr);
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030022}
23
24static void fsl_upm_end_pattern(struct fsl_upm *upm)
25{
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +020026 clrbits_be32(upm->mxmr, MxMR_OP_RUNP);
27
28 while (in_be32(upm->mxmr) & MxMR_OP_RUNP)
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030029 eieio();
30}
31
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010032static void fsl_upm_run_pattern(struct fsl_upm *upm, int width,
33 void __iomem *io_addr, u32 mar)
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030034{
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010035 out_be32(upm->mar, mar);
John Schmoller9fd84912010-12-02 11:43:10 -060036 (void)in_be32(upm->mar);
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +020037 switch (width) {
38 case 8:
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010039 out_8(io_addr, 0x0);
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +020040 break;
41 case 16:
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010042 out_be16(io_addr, 0x0);
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +020043 break;
44 case 32:
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010045 out_be32(io_addr, 0x0);
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +020046 break;
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030047 }
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030048}
49
Wolfgang Grandegger33846df2009-02-11 18:38:23 +010050static void fun_wait(struct fsl_upm_nand *fun)
51{
52 if (fun->dev_ready) {
53 while (!fun->dev_ready(fun->chip_nr))
54 debug("unexpected busy state\n");
55 } else {
56 /*
Wolfgang Denkd923a5d2012-10-03 23:36:18 +000057 * If the R/B pin is not connected,
Wolfgang Grandegger33846df2009-02-11 18:38:23 +010058 * a short delay is necessary.
59 */
60 udelay(1);
61 }
62}
63
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010064#if CONFIG_SYS_NAND_MAX_CHIPS > 1
65static void fun_select_chip(struct mtd_info *mtd, int chip_nr)
66{
Scott Wood17cb4b82016-05-30 13:57:56 -050067 struct nand_chip *chip = mtd_to_nand(mtd);
68 struct fsl_upm_nand *fun = nand_get_controller_data(chip);
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010069
70 if (chip_nr >= 0) {
71 fun->chip_nr = chip_nr;
72 chip->IO_ADDR_R = chip->IO_ADDR_W =
73 fun->upm.io_addr + fun->chip_offset * chip_nr;
74 } else if (chip_nr == -1) {
75 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
76 }
77}
78#endif
79
Anton Vorontsove1c3dba2008-06-12 11:10:21 -050080static void fun_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030081{
Scott Wood17cb4b82016-05-30 13:57:56 -050082 struct nand_chip *chip = mtd_to_nand(mtd);
83 struct fsl_upm_nand *fun = nand_get_controller_data(chip);
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +010084 void __iomem *io_addr;
85 u32 mar;
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030086
Anton Vorontsove1c3dba2008-06-12 11:10:21 -050087 if (!(ctrl & fun->last_ctrl)) {
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +020088 fsl_upm_end_pattern(&fun->upm);
Anton Vorontsove1c3dba2008-06-12 11:10:21 -050089
90 if (cmd == NAND_CMD_NONE)
91 return;
92
93 fun->last_ctrl = ctrl & (NAND_ALE | NAND_CLE);
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030094 }
Anton Vorontsovcd9d2302008-01-14 23:09:32 +030095
Anton Vorontsove1c3dba2008-06-12 11:10:21 -050096 if (ctrl & NAND_CTRL_CHANGE) {
97 if (ctrl & NAND_ALE)
98 fsl_upm_start_pattern(&fun->upm, fun->upm_addr_offset);
99 else if (ctrl & NAND_CLE)
100 fsl_upm_start_pattern(&fun->upm, fun->upm_cmd_offset);
101 }
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300102
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +0100103 mar = cmd << (32 - fun->width);
104 io_addr = fun->upm.io_addr;
105#if CONFIG_SYS_NAND_MAX_CHIPS > 1
Wolfgang Grandegger06e9f7d2009-02-11 18:38:22 +0100106 if (fun->chip_nr > 0) {
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +0100107 io_addr += fun->chip_offset * fun->chip_nr;
Wolfgang Grandegger06e9f7d2009-02-11 18:38:22 +0100108 if (fun->upm_mar_chip_offset)
109 mar |= fun->upm_mar_chip_offset * fun->chip_nr;
110 }
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +0100111#endif
112 fsl_upm_run_pattern(&fun->upm, fun->width, io_addr, mar);
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +0200113
Anton Vorontsove1c3dba2008-06-12 11:10:21 -0500114 /*
Wolfgang Denk93e14592013-10-04 17:43:24 +0200115 * Some boards/chips needs this. At least the MPC8360E-RDK
116 * needs it. Probably weird chip, because I don't see any
117 * need for this on MPC8555E + Samsung K9F1G08U0A. Usually
118 * here are 0-2 unexpected busy states per block read.
Anton Vorontsove1c3dba2008-06-12 11:10:21 -0500119 */
Wolfgang Grandegger33846df2009-02-11 18:38:23 +0100120 if (fun->wait_flags & FSL_UPM_WAIT_RUN_PATTERN)
121 fun_wait(fun);
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300122}
123
Marek Vasut24dd8632011-10-04 00:56:07 +0200124static u8 upm_nand_read_byte(struct mtd_info *mtd)
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300125{
Scott Wood17cb4b82016-05-30 13:57:56 -0500126 struct nand_chip *chip = mtd_to_nand(mtd);
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300127
128 return in_8(chip->IO_ADDR_R);
129}
130
Marek Vasut24dd8632011-10-04 00:56:07 +0200131static void upm_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300132{
133 int i;
Scott Wood17cb4b82016-05-30 13:57:56 -0500134 struct nand_chip *chip = mtd_to_nand(mtd);
135 struct fsl_upm_nand *fun = nand_get_controller_data(chip);
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300136
Wolfgang Grandegger33846df2009-02-11 18:38:23 +0100137 for (i = 0; i < len; i++) {
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300138 out_8(chip->IO_ADDR_W, buf[i]);
Wolfgang Grandegger33846df2009-02-11 18:38:23 +0100139 if (fun->wait_flags & FSL_UPM_WAIT_WRITE_BYTE)
140 fun_wait(fun);
141 }
142
143 if (fun->wait_flags & FSL_UPM_WAIT_WRITE_BUFFER)
144 fun_wait(fun);
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300145}
146
Marek Vasut24dd8632011-10-04 00:56:07 +0200147static void upm_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300148{
149 int i;
Scott Wood17cb4b82016-05-30 13:57:56 -0500150 struct nand_chip *chip = mtd_to_nand(mtd);
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300151
152 for (i = 0; i < len; i++)
153 buf[i] = in_8(chip->IO_ADDR_R);
154}
155
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300156static int nand_dev_ready(struct mtd_info *mtd)
157{
Scott Wood17cb4b82016-05-30 13:57:56 -0500158 struct nand_chip *chip = mtd_to_nand(mtd);
159 struct fsl_upm_nand *fun = nand_get_controller_data(chip);
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300160
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +0100161 return fun->dev_ready(fun->chip_nr);
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300162}
163
164int fsl_upm_nand_init(struct nand_chip *chip, struct fsl_upm_nand *fun)
165{
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +0200166 if (fun->width != 8 && fun->width != 16 && fun->width != 32)
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300167 return -ENOSYS;
168
Anton Vorontsove1c3dba2008-06-12 11:10:21 -0500169 fun->last_ctrl = NAND_CLE;
170
Scott Wood17cb4b82016-05-30 13:57:56 -0500171 nand_set_controller_data(chip, fun);
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300172 chip->chip_delay = fun->chip_delay;
Anton Vorontsove1c3dba2008-06-12 11:10:21 -0500173 chip->ecc.mode = NAND_ECC_SOFT;
174 chip->cmd_ctrl = fun_cmd_ctrl;
Wolfgang Grandeggere93c1c12009-02-11 18:38:21 +0100175#if CONFIG_SYS_NAND_MAX_CHIPS > 1
176 chip->select_chip = fun_select_chip;
177#endif
Marek Vasut24dd8632011-10-04 00:56:07 +0200178 chip->read_byte = upm_nand_read_byte;
179 chip->read_buf = upm_nand_read_buf;
180 chip->write_buf = upm_nand_write_buf;
Wolfgang Grandeggera75a57e2008-06-05 13:02:29 +0200181 if (fun->dev_ready)
182 chip->dev_ready = nand_dev_ready;
Anton Vorontsovcd9d2302008-01-14 23:09:32 +0300183
184 return 0;
185}