blob: dfbdbca3aec6de48528eb4c0e8e9985f9032bfc1 [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>
11#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090012#include <linux/errno.h>
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030013#include <linux/mtd/mtd.h>
14#include <linux/mtd/fsl_upm.h>
15#include <nand.h>
16
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030017static void fsl_upm_start_pattern(struct fsl_upm *upm, u32 pat_offset)
18{
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020019 clrsetbits_be32(upm->mxmr, MxMR_MAD_MSK, MxMR_OP_RUNP | pat_offset);
John Schmollera5fb4032010-12-02 11:43:10 -060020 (void)in_be32(upm->mxmr);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030021}
22
23static void fsl_upm_end_pattern(struct fsl_upm *upm)
24{
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020025 clrbits_be32(upm->mxmr, MxMR_OP_RUNP);
26
27 while (in_be32(upm->mxmr) & MxMR_OP_RUNP)
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030028 eieio();
29}
30
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +010031static void fsl_upm_run_pattern(struct fsl_upm *upm, int width,
32 void __iomem *io_addr, u32 mar)
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030033{
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +010034 out_be32(upm->mar, mar);
John Schmollera5fb4032010-12-02 11:43:10 -060035 (void)in_be32(upm->mar);
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020036 switch (width) {
37 case 8:
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +010038 out_8(io_addr, 0x0);
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020039 break;
40 case 16:
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +010041 out_be16(io_addr, 0x0);
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020042 break;
43 case 32:
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +010044 out_be32(io_addr, 0x0);
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020045 break;
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030046 }
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030047}
48
Wolfgang Grandegger64de8db2009-02-11 18:38:23 +010049static void fun_wait(struct fsl_upm_nand *fun)
50{
51 if (fun->dev_ready) {
52 while (!fun->dev_ready(fun->chip_nr))
53 debug("unexpected busy state\n");
54 } else {
55 /*
Wolfgang Denk178d49c2012-10-03 23:36:18 +000056 * If the R/B pin is not connected,
Wolfgang Grandegger64de8db2009-02-11 18:38:23 +010057 * a short delay is necessary.
58 */
59 udelay(1);
60 }
61}
62
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +010063#if CONFIG_SYS_NAND_MAX_CHIPS > 1
64static void fun_select_chip(struct mtd_info *mtd, int chip_nr)
65{
Scott Wood17fed142016-05-30 13:57:56 -050066 struct nand_chip *chip = mtd_to_nand(mtd);
67 struct fsl_upm_nand *fun = nand_get_controller_data(chip);
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +010068
69 if (chip_nr >= 0) {
70 fun->chip_nr = chip_nr;
71 chip->IO_ADDR_R = chip->IO_ADDR_W =
72 fun->upm.io_addr + fun->chip_offset * chip_nr;
73 } else if (chip_nr == -1) {
74 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
75 }
76}
77#endif
78
Anton Vorontsov67166032008-06-12 11:10:21 -050079static void fun_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030080{
Scott Wood17fed142016-05-30 13:57:56 -050081 struct nand_chip *chip = mtd_to_nand(mtd);
82 struct fsl_upm_nand *fun = nand_get_controller_data(chip);
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +010083 void __iomem *io_addr;
84 u32 mar;
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030085
Anton Vorontsov67166032008-06-12 11:10:21 -050086 if (!(ctrl & fun->last_ctrl)) {
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020087 fsl_upm_end_pattern(&fun->upm);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030088
Anton Vorontsov67166032008-06-12 11:10:21 -050089 if (cmd == NAND_CMD_NONE)
90 return;
91
92 fun->last_ctrl = ctrl & (NAND_ALE | NAND_CLE);
93 }
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030094
Anton Vorontsov67166032008-06-12 11:10:21 -050095 if (ctrl & NAND_CTRL_CHANGE) {
96 if (ctrl & NAND_ALE)
97 fsl_upm_start_pattern(&fun->upm, fun->upm_addr_offset);
98 else if (ctrl & NAND_CLE)
99 fsl_upm_start_pattern(&fun->upm, fun->upm_cmd_offset);
100 }
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300101
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +0100102 mar = cmd << (32 - fun->width);
103 io_addr = fun->upm.io_addr;
104#if CONFIG_SYS_NAND_MAX_CHIPS > 1
Wolfgang Grandeggerac205332009-02-11 18:38:22 +0100105 if (fun->chip_nr > 0) {
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +0100106 io_addr += fun->chip_offset * fun->chip_nr;
Wolfgang Grandeggerac205332009-02-11 18:38:22 +0100107 if (fun->upm_mar_chip_offset)
108 mar |= fun->upm_mar_chip_offset * fun->chip_nr;
109 }
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +0100110#endif
111 fsl_upm_run_pattern(&fun->upm, fun->width, io_addr, mar);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300112
Anton Vorontsov67166032008-06-12 11:10:21 -0500113 /*
Wolfgang Denkec7fbf52013-10-04 17:43:24 +0200114 * Some boards/chips needs this. At least the MPC8360E-RDK
115 * needs it. Probably weird chip, because I don't see any
116 * need for this on MPC8555E + Samsung K9F1G08U0A. Usually
117 * here are 0-2 unexpected busy states per block read.
Anton Vorontsov67166032008-06-12 11:10:21 -0500118 */
Wolfgang Grandegger64de8db2009-02-11 18:38:23 +0100119 if (fun->wait_flags & FSL_UPM_WAIT_RUN_PATTERN)
120 fun_wait(fun);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300121}
122
Marek Vasut3b8756f2011-10-04 00:56:07 +0200123static u8 upm_nand_read_byte(struct mtd_info *mtd)
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300124{
Scott Wood17fed142016-05-30 13:57:56 -0500125 struct nand_chip *chip = mtd_to_nand(mtd);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300126
127 return in_8(chip->IO_ADDR_R);
128}
129
Marek Vasut3b8756f2011-10-04 00:56:07 +0200130static void upm_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300131{
132 int i;
Scott Wood17fed142016-05-30 13:57:56 -0500133 struct nand_chip *chip = mtd_to_nand(mtd);
134 struct fsl_upm_nand *fun = nand_get_controller_data(chip);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300135
Wolfgang Grandegger64de8db2009-02-11 18:38:23 +0100136 for (i = 0; i < len; i++) {
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300137 out_8(chip->IO_ADDR_W, buf[i]);
Wolfgang Grandegger64de8db2009-02-11 18:38:23 +0100138 if (fun->wait_flags & FSL_UPM_WAIT_WRITE_BYTE)
139 fun_wait(fun);
140 }
141
142 if (fun->wait_flags & FSL_UPM_WAIT_WRITE_BUFFER)
143 fun_wait(fun);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300144}
145
Marek Vasut3b8756f2011-10-04 00:56:07 +0200146static void upm_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300147{
148 int i;
Scott Wood17fed142016-05-30 13:57:56 -0500149 struct nand_chip *chip = mtd_to_nand(mtd);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300150
151 for (i = 0; i < len; i++)
152 buf[i] = in_8(chip->IO_ADDR_R);
153}
154
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300155static int nand_dev_ready(struct mtd_info *mtd)
156{
Scott Wood17fed142016-05-30 13:57:56 -0500157 struct nand_chip *chip = mtd_to_nand(mtd);
158 struct fsl_upm_nand *fun = nand_get_controller_data(chip);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300159
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +0100160 return fun->dev_ready(fun->chip_nr);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300161}
162
163int fsl_upm_nand_init(struct nand_chip *chip, struct fsl_upm_nand *fun)
164{
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +0200165 if (fun->width != 8 && fun->width != 16 && fun->width != 32)
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300166 return -ENOSYS;
167
Anton Vorontsov67166032008-06-12 11:10:21 -0500168 fun->last_ctrl = NAND_CLE;
169
Scott Wood17fed142016-05-30 13:57:56 -0500170 nand_set_controller_data(chip, fun);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300171 chip->chip_delay = fun->chip_delay;
Anton Vorontsov67166032008-06-12 11:10:21 -0500172 chip->ecc.mode = NAND_ECC_SOFT;
173 chip->cmd_ctrl = fun_cmd_ctrl;
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +0100174#if CONFIG_SYS_NAND_MAX_CHIPS > 1
175 chip->select_chip = fun_select_chip;
176#endif
Marek Vasut3b8756f2011-10-04 00:56:07 +0200177 chip->read_byte = upm_nand_read_byte;
178 chip->read_buf = upm_nand_read_buf;
179 chip->write_buf = upm_nand_write_buf;
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +0200180 if (fun->dev_ready)
181 chip->dev_ready = nand_dev_ready;
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300182
183 return 0;
184}