1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32#include <common.h>
33#include <nand.h>
34#include <linux/mtd/ndfc.h>
35#include <linux/mtd/nand_ecc.h>
36#include <asm/processor.h>
37#include <asm/io.h>
38#include <asm/ppc4xx.h>
39
40#ifndef CONFIG_SYS_NAND_BCR
41#define CONFIG_SYS_NAND_BCR 0x80002222
42#endif
43#ifndef CONFIG_SYS_NDFC_EBC0_CFG
44#define CONFIG_SYS_NDFC_EBC0_CFG 0xb8400000
45#endif
46
47
48
49
50
51
52static int ndfc_cs[NDFC_MAX_BANKS];
53
54static void ndfc_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
55{
56 struct nand_chip *this = mtd->priv;
57 ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
58
59 if (cmd == NAND_CMD_NONE)
60 return;
61
62 if (ctrl & NAND_CLE)
63 out_8((u8 *)(base + NDFC_CMD), cmd & 0xFF);
64 else
65 out_8((u8 *)(base + NDFC_ALE), cmd & 0xFF);
66}
67
68static int ndfc_dev_ready(struct mtd_info *mtdinfo)
69{
70 struct nand_chip *this = mtdinfo->priv;
71 ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
72
73 return (in_be32((u32 *)(base + NDFC_STAT)) & NDFC_STAT_IS_READY);
74}
75
76static void ndfc_enable_hwecc(struct mtd_info *mtdinfo, int mode)
77{
78 struct nand_chip *this = mtdinfo->priv;
79 ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
80 u32 ccr;
81
82 ccr = in_be32((u32 *)(base + NDFC_CCR));
83 ccr |= NDFC_CCR_RESET_ECC;
84 out_be32((u32 *)(base + NDFC_CCR), ccr);
85}
86
87static int ndfc_calculate_ecc(struct mtd_info *mtdinfo,
88 const u_char *dat, u_char *ecc_code)
89{
90 struct nand_chip *this = mtdinfo->priv;
91 ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
92 u32 ecc;
93 u8 *p = (u8 *)&ecc;
94
95 ecc = in_be32((u32 *)(base + NDFC_ECC));
96
97
98
99 ecc_code[0] = p[1];
100 ecc_code[1] = p[2];
101 ecc_code[2] = p[3];
102
103 return 0;
104}
105
106
107
108
109
110
111
112
113static void ndfc_read_buf(struct mtd_info *mtdinfo, uint8_t *buf, int len)
114{
115 struct nand_chip *this = mtdinfo->priv;
116 ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
117 uint32_t *p = (uint32_t *) buf;
118
119 for (;len > 0; len -= 4)
120 *p++ = in_be32((u32 *)(base + NDFC_DATA));
121}
122
123#ifndef CONFIG_NAND_SPL
124
125
126
127
128static void ndfc_write_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
129{
130 struct nand_chip *this = mtdinfo->priv;
131 ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
132 uint32_t *p = (uint32_t *) buf;
133
134 for (; len > 0; len -= 4)
135 out_be32((u32 *)(base + NDFC_DATA), *p++);
136}
137
138static int ndfc_verify_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
139{
140 struct nand_chip *this = mtdinfo->priv;
141 ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
142 uint32_t *p = (uint32_t *) buf;
143
144 for (; len > 0; len -= 4)
145 if (*p++ != in_be32((u32 *)(base + NDFC_DATA)))
146 return -1;
147
148 return 0;
149}
150
151
152
153
154static uint8_t ndfc_read_byte(struct mtd_info *mtd)
155{
156
157 struct nand_chip *chip = mtd->priv;
158
159#ifdef CONFIG_SYS_NDFC_16BIT
160 return (uint8_t) readw(chip->IO_ADDR_R);
161#else
162 return readb(chip->IO_ADDR_R);
163#endif
164
165}
166
167#endif
168
169void board_nand_select_device(struct nand_chip *nand, int chip)
170{
171
172
173
174
175 ulong base = (ulong)nand->IO_ADDR_W & 0xffffff00;
176 int cs = ndfc_cs[chip];
177
178
179
180 out_be32((u32 *)(base + NDFC_CCR), 0x00000000 | (cs << 24));
181 out_be32((u32 *)(base + NDFC_BCFG0 + (cs << 2)), CONFIG_SYS_NAND_BCR);
182}
183
184static void ndfc_select_chip(struct mtd_info *mtd, int chip)
185{
186
187
188
189}
190
191int board_nand_init(struct nand_chip *nand)
192{
193 int cs = (ulong)nand->IO_ADDR_W & 0x00000003;
194 ulong base = (ulong)nand->IO_ADDR_W & 0xffffff00;
195 static int chip = 0;
196
197
198
199
200 ndfc_cs[chip] = cs;
201
202
203
204
205 board_nand_select_device(nand, chip);
206
207 nand->IO_ADDR_R = (void __iomem *)(base + NDFC_DATA);
208 nand->IO_ADDR_W = (void __iomem *)(base + NDFC_DATA);
209 nand->cmd_ctrl = ndfc_hwcontrol;
210 nand->chip_delay = 50;
211 nand->read_buf = ndfc_read_buf;
212 nand->dev_ready = ndfc_dev_ready;
213 nand->ecc.correct = nand_correct_data;
214 nand->ecc.hwctl = ndfc_enable_hwecc;
215 nand->ecc.calculate = ndfc_calculate_ecc;
216 nand->ecc.mode = NAND_ECC_HW;
217 nand->ecc.size = 256;
218 nand->ecc.bytes = 3;
219 nand->select_chip = ndfc_select_chip;
220
221#ifdef CONFIG_SYS_NDFC_16BIT
222 nand->options |= NAND_BUSWIDTH_16;
223#endif
224
225#ifndef CONFIG_NAND_SPL
226 nand->write_buf = ndfc_write_buf;
227 nand->verify_buf = ndfc_verify_buf;
228 nand->read_byte = ndfc_read_byte;
229
230 chip++;
231#else
232
233
234
235 mtebc(EBC0_CFG, CONFIG_SYS_NDFC_EBC0_CFG);
236
237 mtebc(PB0CR, CONFIG_SYS_EBC_PB0CR);
238 mtebc(PB0AP, CONFIG_SYS_EBC_PB0AP);
239#endif
240
241 return 0;
242}
243