blob: 91cc133953725c1b414e1df11e1ec058069d8488 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Anton Vorontsov6ab97e92008-01-14 23:09:32 +03002/*
3 * FSL UPM NAND driver
4 *
5 * Copyright (C) 2007 MontaVista Software, Inc.
6 * Anton Vorontsov <avorontsov@ru.mvista.com>
Anton Vorontsov6ab97e92008-01-14 23:09:32 +03007 */
8
9#include <config.h>
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030010#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030012#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060013#include <linux/delay.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090014#include <linux/errno.h>
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030015#include <linux/mtd/mtd.h>
Tom Rini3bde7e22021-09-22 14:50:35 -040016#include <linux/mtd/rawnand.h>
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030017#include <linux/mtd/fsl_upm.h>
18#include <nand.h>
19
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030020static void fsl_upm_start_pattern(struct fsl_upm *upm, u32 pat_offset)
21{
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020022 clrsetbits_be32(upm->mxmr, MxMR_MAD_MSK, MxMR_OP_RUNP | pat_offset);
John Schmollera5fb4032010-12-02 11:43:10 -060023 (void)in_be32(upm->mxmr);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030024}
25
26static void fsl_upm_end_pattern(struct fsl_upm *upm)
27{
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020028 clrbits_be32(upm->mxmr, MxMR_OP_RUNP);
29
30 while (in_be32(upm->mxmr) & MxMR_OP_RUNP)
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030031 eieio();
32}
33
Wolfgang Grandeggerf1540442009-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 Vorontsov6ab97e92008-01-14 23:09:32 +030036{
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +010037 out_be32(upm->mar, mar);
John Schmollera5fb4032010-12-02 11:43:10 -060038 (void)in_be32(upm->mar);
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020039 switch (width) {
40 case 8:
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +010041 out_8(io_addr, 0x0);
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020042 break;
43 case 16:
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +010044 out_be16(io_addr, 0x0);
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020045 break;
46 case 32:
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +010047 out_be32(io_addr, 0x0);
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020048 break;
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030049 }
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030050}
51
Wolfgang Grandegger64de8db2009-02-11 18:38:23 +010052static void fun_wait(struct fsl_upm_nand *fun)
53{
54 if (fun->dev_ready) {
55 while (!fun->dev_ready(fun->chip_nr))
56 debug("unexpected busy state\n");
57 } else {
58 /*
Wolfgang Denk178d49c2012-10-03 23:36:18 +000059 * If the R/B pin is not connected,
Wolfgang Grandegger64de8db2009-02-11 18:38:23 +010060 * a short delay is necessary.
61 */
62 udelay(1);
63 }
64}
65
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +010066#if CONFIG_SYS_NAND_MAX_CHIPS > 1
67static void fun_select_chip(struct mtd_info *mtd, int chip_nr)
68{
Scott Wood17fed142016-05-30 13:57:56 -050069 struct nand_chip *chip = mtd_to_nand(mtd);
70 struct fsl_upm_nand *fun = nand_get_controller_data(chip);
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +010071
72 if (chip_nr >= 0) {
73 fun->chip_nr = chip_nr;
74 chip->IO_ADDR_R = chip->IO_ADDR_W =
75 fun->upm.io_addr + fun->chip_offset * chip_nr;
76 } else if (chip_nr == -1) {
77 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
78 }
79}
80#endif
81
Anton Vorontsov67166032008-06-12 11:10:21 -050082static void fun_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030083{
Scott Wood17fed142016-05-30 13:57:56 -050084 struct nand_chip *chip = mtd_to_nand(mtd);
85 struct fsl_upm_nand *fun = nand_get_controller_data(chip);
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +010086 void __iomem *io_addr;
87 u32 mar;
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030088
Anton Vorontsov67166032008-06-12 11:10:21 -050089 if (!(ctrl & fun->last_ctrl)) {
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020090 fsl_upm_end_pattern(&fun->upm);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030091
Anton Vorontsov67166032008-06-12 11:10:21 -050092 if (cmd == NAND_CMD_NONE)
93 return;
94
95 fun->last_ctrl = ctrl & (NAND_ALE | NAND_CLE);
96 }
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030097
Anton Vorontsov67166032008-06-12 11:10:21 -050098 if (ctrl & NAND_CTRL_CHANGE) {
99 if (ctrl & NAND_ALE)
100 fsl_upm_start_pattern(&fun->upm, fun->upm_addr_offset);
101 else if (ctrl & NAND_CLE)
102 fsl_upm_start_pattern(&fun->upm, fun->upm_cmd_offset);
103 }
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300104
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +0100105 mar = cmd << (32 - fun->width);
106 io_addr = fun->upm.io_addr;
107#if CONFIG_SYS_NAND_MAX_CHIPS > 1
Wolfgang Grandeggerac205332009-02-11 18:38:22 +0100108 if (fun->chip_nr > 0) {
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +0100109 io_addr += fun->chip_offset * fun->chip_nr;
Wolfgang Grandeggerac205332009-02-11 18:38:22 +0100110 if (fun->upm_mar_chip_offset)
111 mar |= fun->upm_mar_chip_offset * fun->chip_nr;
112 }
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +0100113#endif
114 fsl_upm_run_pattern(&fun->upm, fun->width, io_addr, mar);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300115
Anton Vorontsov67166032008-06-12 11:10:21 -0500116 /*
Wolfgang Denkec7fbf52013-10-04 17:43:24 +0200117 * Some boards/chips needs this. At least the MPC8360E-RDK
118 * needs it. Probably weird chip, because I don't see any
119 * need for this on MPC8555E + Samsung K9F1G08U0A. Usually
120 * here are 0-2 unexpected busy states per block read.
Anton Vorontsov67166032008-06-12 11:10:21 -0500121 */
Wolfgang Grandegger64de8db2009-02-11 18:38:23 +0100122 if (fun->wait_flags & FSL_UPM_WAIT_RUN_PATTERN)
123 fun_wait(fun);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300124}
125
Marek Vasut3b8756f2011-10-04 00:56:07 +0200126static u8 upm_nand_read_byte(struct mtd_info *mtd)
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300127{
Scott Wood17fed142016-05-30 13:57:56 -0500128 struct nand_chip *chip = mtd_to_nand(mtd);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300129
130 return in_8(chip->IO_ADDR_R);
131}
132
Marek Vasut3b8756f2011-10-04 00:56:07 +0200133static void upm_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300134{
135 int i;
Scott Wood17fed142016-05-30 13:57:56 -0500136 struct nand_chip *chip = mtd_to_nand(mtd);
137 struct fsl_upm_nand *fun = nand_get_controller_data(chip);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300138
Wolfgang Grandegger64de8db2009-02-11 18:38:23 +0100139 for (i = 0; i < len; i++) {
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300140 out_8(chip->IO_ADDR_W, buf[i]);
Wolfgang Grandegger64de8db2009-02-11 18:38:23 +0100141 if (fun->wait_flags & FSL_UPM_WAIT_WRITE_BYTE)
142 fun_wait(fun);
143 }
144
145 if (fun->wait_flags & FSL_UPM_WAIT_WRITE_BUFFER)
146 fun_wait(fun);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300147}
148
Marek Vasut3b8756f2011-10-04 00:56:07 +0200149static void upm_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300150{
151 int i;
Scott Wood17fed142016-05-30 13:57:56 -0500152 struct nand_chip *chip = mtd_to_nand(mtd);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300153
154 for (i = 0; i < len; i++)
155 buf[i] = in_8(chip->IO_ADDR_R);
156}
157
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300158static int nand_dev_ready(struct mtd_info *mtd)
159{
Scott Wood17fed142016-05-30 13:57:56 -0500160 struct nand_chip *chip = mtd_to_nand(mtd);
161 struct fsl_upm_nand *fun = nand_get_controller_data(chip);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300162
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +0100163 return fun->dev_ready(fun->chip_nr);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300164}
165
166int fsl_upm_nand_init(struct nand_chip *chip, struct fsl_upm_nand *fun)
167{
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +0200168 if (fun->width != 8 && fun->width != 16 && fun->width != 32)
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300169 return -ENOSYS;
170
Anton Vorontsov67166032008-06-12 11:10:21 -0500171 fun->last_ctrl = NAND_CLE;
172
Scott Wood17fed142016-05-30 13:57:56 -0500173 nand_set_controller_data(chip, fun);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300174 chip->chip_delay = fun->chip_delay;
Anton Vorontsov67166032008-06-12 11:10:21 -0500175 chip->ecc.mode = NAND_ECC_SOFT;
176 chip->cmd_ctrl = fun_cmd_ctrl;
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +0100177#if CONFIG_SYS_NAND_MAX_CHIPS > 1
178 chip->select_chip = fun_select_chip;
179#endif
Marek Vasut3b8756f2011-10-04 00:56:07 +0200180 chip->read_byte = upm_nand_read_byte;
181 chip->read_buf = upm_nand_read_buf;
182 chip->write_buf = upm_nand_write_buf;
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +0200183 if (fun->dev_ready)
184 chip->dev_ready = nand_dev_ready;
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300185
186 return 0;
187}