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