1*cfcc706cSMiquel Raynal /*
2*cfcc706cSMiquel Raynal * FSL UPM NAND driver
3*cfcc706cSMiquel Raynal *
4*cfcc706cSMiquel Raynal * Copyright (C) 2007 MontaVista Software, Inc.
5*cfcc706cSMiquel Raynal * Anton Vorontsov <avorontsov@ru.mvista.com>
6*cfcc706cSMiquel Raynal *
7*cfcc706cSMiquel Raynal * SPDX-License-Identifier: GPL-2.0+
8*cfcc706cSMiquel Raynal */
9*cfcc706cSMiquel Raynal
10*cfcc706cSMiquel Raynal #include <config.h>
11*cfcc706cSMiquel Raynal #include <common.h>
12*cfcc706cSMiquel Raynal #include <asm/io.h>
13*cfcc706cSMiquel Raynal #include <linux/errno.h>
14*cfcc706cSMiquel Raynal #include <linux/mtd/mtd.h>
15*cfcc706cSMiquel Raynal #include <linux/mtd/fsl_upm.h>
16*cfcc706cSMiquel Raynal #include <nand.h>
17*cfcc706cSMiquel Raynal
fsl_upm_start_pattern(struct fsl_upm * upm,u32 pat_offset)18*cfcc706cSMiquel Raynal static void fsl_upm_start_pattern(struct fsl_upm *upm, u32 pat_offset)
19*cfcc706cSMiquel Raynal {
20*cfcc706cSMiquel Raynal clrsetbits_be32(upm->mxmr, MxMR_MAD_MSK, MxMR_OP_RUNP | pat_offset);
21*cfcc706cSMiquel Raynal (void)in_be32(upm->mxmr);
22*cfcc706cSMiquel Raynal }
23*cfcc706cSMiquel Raynal
fsl_upm_end_pattern(struct fsl_upm * upm)24*cfcc706cSMiquel Raynal static void fsl_upm_end_pattern(struct fsl_upm *upm)
25*cfcc706cSMiquel Raynal {
26*cfcc706cSMiquel Raynal clrbits_be32(upm->mxmr, MxMR_OP_RUNP);
27*cfcc706cSMiquel Raynal
28*cfcc706cSMiquel Raynal while (in_be32(upm->mxmr) & MxMR_OP_RUNP)
29*cfcc706cSMiquel Raynal eieio();
30*cfcc706cSMiquel Raynal }
31*cfcc706cSMiquel Raynal
fsl_upm_run_pattern(struct fsl_upm * upm,int width,void __iomem * io_addr,u32 mar)32*cfcc706cSMiquel Raynal static void fsl_upm_run_pattern(struct fsl_upm *upm, int width,
33*cfcc706cSMiquel Raynal void __iomem *io_addr, u32 mar)
34*cfcc706cSMiquel Raynal {
35*cfcc706cSMiquel Raynal out_be32(upm->mar, mar);
36*cfcc706cSMiquel Raynal (void)in_be32(upm->mar);
37*cfcc706cSMiquel Raynal switch (width) {
38*cfcc706cSMiquel Raynal case 8:
39*cfcc706cSMiquel Raynal out_8(io_addr, 0x0);
40*cfcc706cSMiquel Raynal break;
41*cfcc706cSMiquel Raynal case 16:
42*cfcc706cSMiquel Raynal out_be16(io_addr, 0x0);
43*cfcc706cSMiquel Raynal break;
44*cfcc706cSMiquel Raynal case 32:
45*cfcc706cSMiquel Raynal out_be32(io_addr, 0x0);
46*cfcc706cSMiquel Raynal break;
47*cfcc706cSMiquel Raynal }
48*cfcc706cSMiquel Raynal }
49*cfcc706cSMiquel Raynal
fun_wait(struct fsl_upm_nand * fun)50*cfcc706cSMiquel Raynal static void fun_wait(struct fsl_upm_nand *fun)
51*cfcc706cSMiquel Raynal {
52*cfcc706cSMiquel Raynal if (fun->dev_ready) {
53*cfcc706cSMiquel Raynal while (!fun->dev_ready(fun->chip_nr))
54*cfcc706cSMiquel Raynal debug("unexpected busy state\n");
55*cfcc706cSMiquel Raynal } else {
56*cfcc706cSMiquel Raynal /*
57*cfcc706cSMiquel Raynal * If the R/B pin is not connected,
58*cfcc706cSMiquel Raynal * a short delay is necessary.
59*cfcc706cSMiquel Raynal */
60*cfcc706cSMiquel Raynal udelay(1);
61*cfcc706cSMiquel Raynal }
62*cfcc706cSMiquel Raynal }
63*cfcc706cSMiquel Raynal
64*cfcc706cSMiquel Raynal #if CONFIG_SYS_NAND_MAX_CHIPS > 1
fun_select_chip(struct mtd_info * mtd,int chip_nr)65*cfcc706cSMiquel Raynal static void fun_select_chip(struct mtd_info *mtd, int chip_nr)
66*cfcc706cSMiquel Raynal {
67*cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd);
68*cfcc706cSMiquel Raynal struct fsl_upm_nand *fun = nand_get_controller_data(chip);
69*cfcc706cSMiquel Raynal
70*cfcc706cSMiquel Raynal if (chip_nr >= 0) {
71*cfcc706cSMiquel Raynal fun->chip_nr = chip_nr;
72*cfcc706cSMiquel Raynal chip->IO_ADDR_R = chip->IO_ADDR_W =
73*cfcc706cSMiquel Raynal fun->upm.io_addr + fun->chip_offset * chip_nr;
74*cfcc706cSMiquel Raynal } else if (chip_nr == -1) {
75*cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
76*cfcc706cSMiquel Raynal }
77*cfcc706cSMiquel Raynal }
78*cfcc706cSMiquel Raynal #endif
79*cfcc706cSMiquel Raynal
fun_cmd_ctrl(struct mtd_info * mtd,int cmd,unsigned int ctrl)80*cfcc706cSMiquel Raynal static void fun_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
81*cfcc706cSMiquel Raynal {
82*cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd);
83*cfcc706cSMiquel Raynal struct fsl_upm_nand *fun = nand_get_controller_data(chip);
84*cfcc706cSMiquel Raynal void __iomem *io_addr;
85*cfcc706cSMiquel Raynal u32 mar;
86*cfcc706cSMiquel Raynal
87*cfcc706cSMiquel Raynal if (!(ctrl & fun->last_ctrl)) {
88*cfcc706cSMiquel Raynal fsl_upm_end_pattern(&fun->upm);
89*cfcc706cSMiquel Raynal
90*cfcc706cSMiquel Raynal if (cmd == NAND_CMD_NONE)
91*cfcc706cSMiquel Raynal return;
92*cfcc706cSMiquel Raynal
93*cfcc706cSMiquel Raynal fun->last_ctrl = ctrl & (NAND_ALE | NAND_CLE);
94*cfcc706cSMiquel Raynal }
95*cfcc706cSMiquel Raynal
96*cfcc706cSMiquel Raynal if (ctrl & NAND_CTRL_CHANGE) {
97*cfcc706cSMiquel Raynal if (ctrl & NAND_ALE)
98*cfcc706cSMiquel Raynal fsl_upm_start_pattern(&fun->upm, fun->upm_addr_offset);
99*cfcc706cSMiquel Raynal else if (ctrl & NAND_CLE)
100*cfcc706cSMiquel Raynal fsl_upm_start_pattern(&fun->upm, fun->upm_cmd_offset);
101*cfcc706cSMiquel Raynal }
102*cfcc706cSMiquel Raynal
103*cfcc706cSMiquel Raynal mar = cmd << (32 - fun->width);
104*cfcc706cSMiquel Raynal io_addr = fun->upm.io_addr;
105*cfcc706cSMiquel Raynal #if CONFIG_SYS_NAND_MAX_CHIPS > 1
106*cfcc706cSMiquel Raynal if (fun->chip_nr > 0) {
107*cfcc706cSMiquel Raynal io_addr += fun->chip_offset * fun->chip_nr;
108*cfcc706cSMiquel Raynal if (fun->upm_mar_chip_offset)
109*cfcc706cSMiquel Raynal mar |= fun->upm_mar_chip_offset * fun->chip_nr;
110*cfcc706cSMiquel Raynal }
111*cfcc706cSMiquel Raynal #endif
112*cfcc706cSMiquel Raynal fsl_upm_run_pattern(&fun->upm, fun->width, io_addr, mar);
113*cfcc706cSMiquel Raynal
114*cfcc706cSMiquel Raynal /*
115*cfcc706cSMiquel Raynal * Some boards/chips needs this. At least the MPC8360E-RDK
116*cfcc706cSMiquel Raynal * needs it. Probably weird chip, because I don't see any
117*cfcc706cSMiquel Raynal * need for this on MPC8555E + Samsung K9F1G08U0A. Usually
118*cfcc706cSMiquel Raynal * here are 0-2 unexpected busy states per block read.
119*cfcc706cSMiquel Raynal */
120*cfcc706cSMiquel Raynal if (fun->wait_flags & FSL_UPM_WAIT_RUN_PATTERN)
121*cfcc706cSMiquel Raynal fun_wait(fun);
122*cfcc706cSMiquel Raynal }
123*cfcc706cSMiquel Raynal
upm_nand_read_byte(struct mtd_info * mtd)124*cfcc706cSMiquel Raynal static u8 upm_nand_read_byte(struct mtd_info *mtd)
125*cfcc706cSMiquel Raynal {
126*cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd);
127*cfcc706cSMiquel Raynal
128*cfcc706cSMiquel Raynal return in_8(chip->IO_ADDR_R);
129*cfcc706cSMiquel Raynal }
130*cfcc706cSMiquel Raynal
upm_nand_write_buf(struct mtd_info * mtd,const u_char * buf,int len)131*cfcc706cSMiquel Raynal static void upm_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
132*cfcc706cSMiquel Raynal {
133*cfcc706cSMiquel Raynal int i;
134*cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd);
135*cfcc706cSMiquel Raynal struct fsl_upm_nand *fun = nand_get_controller_data(chip);
136*cfcc706cSMiquel Raynal
137*cfcc706cSMiquel Raynal for (i = 0; i < len; i++) {
138*cfcc706cSMiquel Raynal out_8(chip->IO_ADDR_W, buf[i]);
139*cfcc706cSMiquel Raynal if (fun->wait_flags & FSL_UPM_WAIT_WRITE_BYTE)
140*cfcc706cSMiquel Raynal fun_wait(fun);
141*cfcc706cSMiquel Raynal }
142*cfcc706cSMiquel Raynal
143*cfcc706cSMiquel Raynal if (fun->wait_flags & FSL_UPM_WAIT_WRITE_BUFFER)
144*cfcc706cSMiquel Raynal fun_wait(fun);
145*cfcc706cSMiquel Raynal }
146*cfcc706cSMiquel Raynal
upm_nand_read_buf(struct mtd_info * mtd,u_char * buf,int len)147*cfcc706cSMiquel Raynal static void upm_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
148*cfcc706cSMiquel Raynal {
149*cfcc706cSMiquel Raynal int i;
150*cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd);
151*cfcc706cSMiquel Raynal
152*cfcc706cSMiquel Raynal for (i = 0; i < len; i++)
153*cfcc706cSMiquel Raynal buf[i] = in_8(chip->IO_ADDR_R);
154*cfcc706cSMiquel Raynal }
155*cfcc706cSMiquel Raynal
nand_dev_ready(struct mtd_info * mtd)156*cfcc706cSMiquel Raynal static int nand_dev_ready(struct mtd_info *mtd)
157*cfcc706cSMiquel Raynal {
158*cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd);
159*cfcc706cSMiquel Raynal struct fsl_upm_nand *fun = nand_get_controller_data(chip);
160*cfcc706cSMiquel Raynal
161*cfcc706cSMiquel Raynal return fun->dev_ready(fun->chip_nr);
162*cfcc706cSMiquel Raynal }
163*cfcc706cSMiquel Raynal
fsl_upm_nand_init(struct nand_chip * chip,struct fsl_upm_nand * fun)164*cfcc706cSMiquel Raynal int fsl_upm_nand_init(struct nand_chip *chip, struct fsl_upm_nand *fun)
165*cfcc706cSMiquel Raynal {
166*cfcc706cSMiquel Raynal if (fun->width != 8 && fun->width != 16 && fun->width != 32)
167*cfcc706cSMiquel Raynal return -ENOSYS;
168*cfcc706cSMiquel Raynal
169*cfcc706cSMiquel Raynal fun->last_ctrl = NAND_CLE;
170*cfcc706cSMiquel Raynal
171*cfcc706cSMiquel Raynal nand_set_controller_data(chip, fun);
172*cfcc706cSMiquel Raynal chip->chip_delay = fun->chip_delay;
173*cfcc706cSMiquel Raynal chip->ecc.mode = NAND_ECC_SOFT;
174*cfcc706cSMiquel Raynal chip->cmd_ctrl = fun_cmd_ctrl;
175*cfcc706cSMiquel Raynal #if CONFIG_SYS_NAND_MAX_CHIPS > 1
176*cfcc706cSMiquel Raynal chip->select_chip = fun_select_chip;
177*cfcc706cSMiquel Raynal #endif
178*cfcc706cSMiquel Raynal chip->read_byte = upm_nand_read_byte;
179*cfcc706cSMiquel Raynal chip->read_buf = upm_nand_read_buf;
180*cfcc706cSMiquel Raynal chip->write_buf = upm_nand_write_buf;
181*cfcc706cSMiquel Raynal if (fun->dev_ready)
182*cfcc706cSMiquel Raynal chip->dev_ready = nand_dev_ready;
183*cfcc706cSMiquel Raynal
184*cfcc706cSMiquel Raynal return 0;
185*cfcc706cSMiquel Raynal }
186