blob: 6c86a7e76712a80c08c77f2d36652d1787b0e800 [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>
16#include <linux/mtd/fsl_upm.h>
17#include <nand.h>
18
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030019static void fsl_upm_start_pattern(struct fsl_upm *upm, u32 pat_offset)
20{
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020021 clrsetbits_be32(upm->mxmr, MxMR_MAD_MSK, MxMR_OP_RUNP | pat_offset);
John Schmollera5fb4032010-12-02 11:43:10 -060022 (void)in_be32(upm->mxmr);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030023}
24
25static void fsl_upm_end_pattern(struct fsl_upm *upm)
26{
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020027 clrbits_be32(upm->mxmr, MxMR_OP_RUNP);
28
29 while (in_be32(upm->mxmr) & MxMR_OP_RUNP)
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030030 eieio();
31}
32
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +010033static void fsl_upm_run_pattern(struct fsl_upm *upm, int width,
34 void __iomem *io_addr, u32 mar)
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030035{
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +010036 out_be32(upm->mar, mar);
John Schmollera5fb4032010-12-02 11:43:10 -060037 (void)in_be32(upm->mar);
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020038 switch (width) {
39 case 8:
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +010040 out_8(io_addr, 0x0);
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020041 break;
42 case 16:
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +010043 out_be16(io_addr, 0x0);
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020044 break;
45 case 32:
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +010046 out_be32(io_addr, 0x0);
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020047 break;
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030048 }
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030049}
50
Wolfgang Grandegger64de8db2009-02-11 18:38:23 +010051static void fun_wait(struct fsl_upm_nand *fun)
52{
53 if (fun->dev_ready) {
54 while (!fun->dev_ready(fun->chip_nr))
55 debug("unexpected busy state\n");
56 } else {
57 /*
Wolfgang Denk178d49c2012-10-03 23:36:18 +000058 * If the R/B pin is not connected,
Wolfgang Grandegger64de8db2009-02-11 18:38:23 +010059 * a short delay is necessary.
60 */
61 udelay(1);
62 }
63}
64
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +010065#if CONFIG_SYS_NAND_MAX_CHIPS > 1
66static void fun_select_chip(struct mtd_info *mtd, int chip_nr)
67{
Scott Wood17fed142016-05-30 13:57:56 -050068 struct nand_chip *chip = mtd_to_nand(mtd);
69 struct fsl_upm_nand *fun = nand_get_controller_data(chip);
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +010070
71 if (chip_nr >= 0) {
72 fun->chip_nr = chip_nr;
73 chip->IO_ADDR_R = chip->IO_ADDR_W =
74 fun->upm.io_addr + fun->chip_offset * chip_nr;
75 } else if (chip_nr == -1) {
76 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
77 }
78}
79#endif
80
Anton Vorontsov67166032008-06-12 11:10:21 -050081static void fun_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030082{
Scott Wood17fed142016-05-30 13:57:56 -050083 struct nand_chip *chip = mtd_to_nand(mtd);
84 struct fsl_upm_nand *fun = nand_get_controller_data(chip);
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +010085 void __iomem *io_addr;
86 u32 mar;
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030087
Anton Vorontsov67166032008-06-12 11:10:21 -050088 if (!(ctrl & fun->last_ctrl)) {
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020089 fsl_upm_end_pattern(&fun->upm);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030090
Anton Vorontsov67166032008-06-12 11:10:21 -050091 if (cmd == NAND_CMD_NONE)
92 return;
93
94 fun->last_ctrl = ctrl & (NAND_ALE | NAND_CLE);
95 }
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030096
Anton Vorontsov67166032008-06-12 11:10:21 -050097 if (ctrl & NAND_CTRL_CHANGE) {
98 if (ctrl & NAND_ALE)
99 fsl_upm_start_pattern(&fun->upm, fun->upm_addr_offset);
100 else if (ctrl & NAND_CLE)
101 fsl_upm_start_pattern(&fun->upm, fun->upm_cmd_offset);
102 }
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300103
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +0100104 mar = cmd << (32 - fun->width);
105 io_addr = fun->upm.io_addr;
106#if CONFIG_SYS_NAND_MAX_CHIPS > 1
Wolfgang Grandeggerac205332009-02-11 18:38:22 +0100107 if (fun->chip_nr > 0) {
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +0100108 io_addr += fun->chip_offset * fun->chip_nr;
Wolfgang Grandeggerac205332009-02-11 18:38:22 +0100109 if (fun->upm_mar_chip_offset)
110 mar |= fun->upm_mar_chip_offset * fun->chip_nr;
111 }
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +0100112#endif
113 fsl_upm_run_pattern(&fun->upm, fun->width, io_addr, mar);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300114
Anton Vorontsov67166032008-06-12 11:10:21 -0500115 /*
Wolfgang Denkec7fbf52013-10-04 17:43:24 +0200116 * Some boards/chips needs this. At least the MPC8360E-RDK
117 * needs it. Probably weird chip, because I don't see any
118 * need for this on MPC8555E + Samsung K9F1G08U0A. Usually
119 * here are 0-2 unexpected busy states per block read.
Anton Vorontsov67166032008-06-12 11:10:21 -0500120 */
Wolfgang Grandegger64de8db2009-02-11 18:38:23 +0100121 if (fun->wait_flags & FSL_UPM_WAIT_RUN_PATTERN)
122 fun_wait(fun);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300123}
124
Marek Vasut3b8756f2011-10-04 00:56:07 +0200125static u8 upm_nand_read_byte(struct mtd_info *mtd)
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300126{
Scott Wood17fed142016-05-30 13:57:56 -0500127 struct nand_chip *chip = mtd_to_nand(mtd);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300128
129 return in_8(chip->IO_ADDR_R);
130}
131
Marek Vasut3b8756f2011-10-04 00:56:07 +0200132static void upm_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300133{
134 int i;
Scott Wood17fed142016-05-30 13:57:56 -0500135 struct nand_chip *chip = mtd_to_nand(mtd);
136 struct fsl_upm_nand *fun = nand_get_controller_data(chip);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300137
Wolfgang Grandegger64de8db2009-02-11 18:38:23 +0100138 for (i = 0; i < len; i++) {
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300139 out_8(chip->IO_ADDR_W, buf[i]);
Wolfgang Grandegger64de8db2009-02-11 18:38:23 +0100140 if (fun->wait_flags & FSL_UPM_WAIT_WRITE_BYTE)
141 fun_wait(fun);
142 }
143
144 if (fun->wait_flags & FSL_UPM_WAIT_WRITE_BUFFER)
145 fun_wait(fun);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300146}
147
Marek Vasut3b8756f2011-10-04 00:56:07 +0200148static void upm_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300149{
150 int i;
Scott Wood17fed142016-05-30 13:57:56 -0500151 struct nand_chip *chip = mtd_to_nand(mtd);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300152
153 for (i = 0; i < len; i++)
154 buf[i] = in_8(chip->IO_ADDR_R);
155}
156
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300157static int nand_dev_ready(struct mtd_info *mtd)
158{
Scott Wood17fed142016-05-30 13:57:56 -0500159 struct nand_chip *chip = mtd_to_nand(mtd);
160 struct fsl_upm_nand *fun = nand_get_controller_data(chip);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300161
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +0100162 return fun->dev_ready(fun->chip_nr);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300163}
164
165int fsl_upm_nand_init(struct nand_chip *chip, struct fsl_upm_nand *fun)
166{
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +0200167 if (fun->width != 8 && fun->width != 16 && fun->width != 32)
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300168 return -ENOSYS;
169
Anton Vorontsov67166032008-06-12 11:10:21 -0500170 fun->last_ctrl = NAND_CLE;
171
Scott Wood17fed142016-05-30 13:57:56 -0500172 nand_set_controller_data(chip, fun);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300173 chip->chip_delay = fun->chip_delay;
Anton Vorontsov67166032008-06-12 11:10:21 -0500174 chip->ecc.mode = NAND_ECC_SOFT;
175 chip->cmd_ctrl = fun_cmd_ctrl;
Wolfgang Grandeggerf1540442009-02-11 18:38:21 +0100176#if CONFIG_SYS_NAND_MAX_CHIPS > 1
177 chip->select_chip = fun_select_chip;
178#endif
Marek Vasut3b8756f2011-10-04 00:56:07 +0200179 chip->read_byte = upm_nand_read_byte;
180 chip->read_buf = upm_nand_read_buf;
181 chip->write_buf = upm_nand_write_buf;
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +0200182 if (fun->dev_ready)
183 chip->dev_ready = nand_dev_ready;
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300184
185 return 0;
186}