]>
Commit | Line | Data |
---|---|---|
932394ac WD |
1 | /* |
2 | * drivers/mtd/nand.c | |
3 | * | |
4 | * Overview: | |
5 | * This is the generic MTD driver for NAND flash devices. It should be | |
6 | * capable of working with almost all NAND chips currently available. | |
7 | * Basic support for AG-AND chips is provided. | |
ac7eb8a3 | 8 | * |
932394ac | 9 | * Additional technical information is available on |
c45912d8 | 10 | * http://www.linux-mtd.infradead.org/doc/nand.html |
ac7eb8a3 | 11 | * |
932394ac | 12 | * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com) |
cfa460ad | 13 | * 2002-2006 Thomas Gleixner (tglx@linutronix.de) |
932394ac | 14 | * |
cfa460ad | 15 | * Credits: |
ac7eb8a3 WD |
16 | * David Woodhouse for adding multichip support |
17 | * | |
932394ac WD |
18 | * Aleph One Ltd. and Toby Churchill Ltd. for supporting the |
19 | * rework for 2K page size chips | |
20 | * | |
cfa460ad | 21 | * TODO: |
932394ac WD |
22 | * Enable cached programming for 2k page size chips |
23 | * Check, if mtd->ecctype should be set to MTD_ECC_HW | |
24 | * if we have HW ecc support. | |
25 | * The AG-AND chips have nice features for speed improvement, | |
26 | * which are not supported yet. Read / program 4 pages in one go. | |
c45912d8 | 27 | * BBT table is not serialized, has to be fixed |
932394ac | 28 | * |
932394ac WD |
29 | * This program is free software; you can redistribute it and/or modify |
30 | * it under the terms of the GNU General Public License version 2 as | |
31 | * published by the Free Software Foundation. | |
32 | * | |
33 | */ | |
34 | ||
35 | /* XXX U-BOOT XXX */ | |
36 | #if 0 | |
cfa460ad | 37 | #include <linux/module.h> |
932394ac WD |
38 | #include <linux/delay.h> |
39 | #include <linux/errno.h> | |
cfa460ad | 40 | #include <linux/err.h> |
932394ac WD |
41 | #include <linux/sched.h> |
42 | #include <linux/slab.h> | |
43 | #include <linux/types.h> | |
44 | #include <linux/mtd/mtd.h> | |
45 | #include <linux/mtd/nand.h> | |
46 | #include <linux/mtd/nand_ecc.h> | |
47 | #include <linux/mtd/compatmac.h> | |
48 | #include <linux/interrupt.h> | |
49 | #include <linux/bitops.h> | |
cfa460ad | 50 | #include <linux/leds.h> |
932394ac WD |
51 | #include <asm/io.h> |
52 | ||
53 | #ifdef CONFIG_MTD_PARTITIONS | |
54 | #include <linux/mtd/partitions.h> | |
55 | #endif | |
56 | ||
ac7eb8a3 | 57 | #endif |
932394ac WD |
58 | |
59 | #include <common.h> | |
addb2e16 | 60 | |
cfa460ad WJ |
61 | #define ENOTSUPP 524 /* Operation is not supported */ |
62 | ||
932394ac WD |
63 | #include <malloc.h> |
64 | #include <watchdog.h> | |
cfa460ad | 65 | #include <linux/err.h> |
932394ac WD |
66 | #include <linux/mtd/compat.h> |
67 | #include <linux/mtd/mtd.h> | |
68 | #include <linux/mtd/nand.h> | |
69 | #include <linux/mtd/nand_ecc.h> | |
70 | ||
71 | #include <asm/io.h> | |
72 | #include <asm/errno.h> | |
73 | ||
74 | #ifdef CONFIG_JFFS2_NAND | |
75 | #include <jffs2/jffs2.h> | |
76 | #endif | |
77 | ||
932394ac | 78 | /* Define default oob placement schemes for large and small page devices */ |
cfa460ad | 79 | static struct nand_ecclayout nand_oob_8 = { |
932394ac WD |
80 | .eccbytes = 3, |
81 | .eccpos = {0, 1, 2}, | |
cfa460ad WJ |
82 | .oobfree = { |
83 | {.offset = 3, | |
84 | .length = 2}, | |
85 | {.offset = 6, | |
86 | .length = 2}} | |
932394ac WD |
87 | }; |
88 | ||
cfa460ad | 89 | static struct nand_ecclayout nand_oob_16 = { |
932394ac WD |
90 | .eccbytes = 6, |
91 | .eccpos = {0, 1, 2, 3, 6, 7}, | |
cfa460ad WJ |
92 | .oobfree = { |
93 | {.offset = 8, | |
94 | . length = 8}} | |
932394ac WD |
95 | }; |
96 | ||
cfa460ad | 97 | static struct nand_ecclayout nand_oob_64 = { |
932394ac WD |
98 | .eccbytes = 24, |
99 | .eccpos = { | |
cfa460ad WJ |
100 | 40, 41, 42, 43, 44, 45, 46, 47, |
101 | 48, 49, 50, 51, 52, 53, 54, 55, | |
102 | 56, 57, 58, 59, 60, 61, 62, 63}, | |
103 | .oobfree = { | |
104 | {.offset = 2, | |
105 | .length = 38}} | |
932394ac WD |
106 | }; |
107 | ||
cfa460ad | 108 | static struct nand_ecclayout nand_oob_128 = { |
248ae5cf SP |
109 | .eccbytes = 48, |
110 | .eccpos = { | |
cfa460ad WJ |
111 | 80, 81, 82, 83, 84, 85, 86, 87, |
112 | 88, 89, 90, 91, 92, 93, 94, 95, | |
113 | 96, 97, 98, 99, 100, 101, 102, 103, | |
114 | 104, 105, 106, 107, 108, 109, 110, 111, | |
115 | 112, 113, 114, 115, 116, 117, 118, 119, | |
116 | 120, 121, 122, 123, 124, 125, 126, 127}, | |
117 | .oobfree = { | |
118 | {.offset = 2, | |
119 | .length = 78}} | |
932394ac WD |
120 | }; |
121 | ||
cfa460ad WJ |
122 | |
123 | static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, | |
124 | int new_state); | |
125 | ||
126 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, | |
127 | struct mtd_oob_ops *ops); | |
128 | ||
129 | static int nand_wait(struct mtd_info *mtd, struct nand_chip *this); | |
248ae5cf | 130 | |
932394ac | 131 | /* |
c45912d8 | 132 | * For devices which display every fart in the system on a separate LED. Is |
cfa460ad | 133 | * compiled away when LED support is disabled. |
932394ac | 134 | */ |
932394ac WD |
135 | /* XXX U-BOOT XXX */ |
136 | #if 0 | |
cfa460ad | 137 | DEFINE_LED_TRIGGER(nand_led_trigger); |
932394ac | 138 | #endif |
ac7eb8a3 | 139 | |
932394ac WD |
140 | /** |
141 | * nand_release_device - [GENERIC] release chip | |
142 | * @mtd: MTD device structure | |
ac7eb8a3 WD |
143 | * |
144 | * Deselect, release chip lock and wake up anyone waiting on the device | |
932394ac WD |
145 | */ |
146 | /* XXX U-BOOT XXX */ | |
147 | #if 0 | |
cfa460ad | 148 | static void nand_release_device(struct mtd_info *mtd) |
932394ac | 149 | { |
cfa460ad | 150 | struct nand_chip *chip = mtd->priv; |
932394ac WD |
151 | |
152 | /* De-select the NAND device */ | |
cfa460ad WJ |
153 | chip->select_chip(mtd, -1); |
154 | ||
155 | /* Release the controller and the chip */ | |
156 | spin_lock(&chip->controller->lock); | |
157 | chip->controller->active = NULL; | |
158 | chip->state = FL_READY; | |
159 | wake_up(&chip->controller->wq); | |
160 | spin_unlock(&chip->controller->lock); | |
932394ac WD |
161 | } |
162 | #else | |
8e9655f8 WD |
163 | static void nand_release_device (struct mtd_info *mtd) |
164 | { | |
165 | struct nand_chip *this = mtd->priv; | |
166 | this->select_chip(mtd, -1); /* De-select the NAND device */ | |
167 | } | |
932394ac WD |
168 | #endif |
169 | ||
170 | /** | |
171 | * nand_read_byte - [DEFAULT] read one byte from the chip | |
172 | * @mtd: MTD device structure | |
173 | * | |
174 | * Default read function for 8bit buswith | |
175 | */ | |
cfa460ad | 176 | static uint8_t nand_read_byte(struct mtd_info *mtd) |
932394ac | 177 | { |
cfa460ad WJ |
178 | struct nand_chip *chip = mtd->priv; |
179 | return readb(chip->IO_ADDR_R); | |
932394ac WD |
180 | } |
181 | ||
182 | /** | |
183 | * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip | |
184 | * @mtd: MTD device structure | |
185 | * | |
ac7eb8a3 | 186 | * Default read function for 16bit buswith with |
932394ac WD |
187 | * endianess conversion |
188 | */ | |
cfa460ad | 189 | static uint8_t nand_read_byte16(struct mtd_info *mtd) |
932394ac | 190 | { |
cfa460ad WJ |
191 | struct nand_chip *chip = mtd->priv; |
192 | return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R)); | |
932394ac WD |
193 | } |
194 | ||
195 | /** | |
196 | * nand_read_word - [DEFAULT] read one word from the chip | |
197 | * @mtd: MTD device structure | |
198 | * | |
ac7eb8a3 | 199 | * Default read function for 16bit buswith without |
932394ac WD |
200 | * endianess conversion |
201 | */ | |
202 | static u16 nand_read_word(struct mtd_info *mtd) | |
203 | { | |
cfa460ad WJ |
204 | struct nand_chip *chip = mtd->priv; |
205 | return readw(chip->IO_ADDR_R); | |
932394ac WD |
206 | } |
207 | ||
208 | /** | |
209 | * nand_select_chip - [DEFAULT] control CE line | |
210 | * @mtd: MTD device structure | |
cfa460ad | 211 | * @chipnr: chipnumber to select, -1 for deselect |
932394ac WD |
212 | * |
213 | * Default select function for 1 chip devices. | |
214 | */ | |
cfa460ad | 215 | static void nand_select_chip(struct mtd_info *mtd, int chipnr) |
932394ac | 216 | { |
cfa460ad WJ |
217 | struct nand_chip *chip = mtd->priv; |
218 | ||
219 | switch (chipnr) { | |
932394ac | 220 | case -1: |
cfa460ad | 221 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE); |
932394ac WD |
222 | break; |
223 | case 0: | |
932394ac WD |
224 | break; |
225 | ||
226 | default: | |
227 | BUG(); | |
228 | } | |
229 | } | |
230 | ||
231 | /** | |
232 | * nand_write_buf - [DEFAULT] write buffer to chip | |
233 | * @mtd: MTD device structure | |
234 | * @buf: data buffer | |
235 | * @len: number of bytes to write | |
236 | * | |
237 | * Default write function for 8bit buswith | |
238 | */ | |
cfa460ad | 239 | static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
932394ac WD |
240 | { |
241 | int i; | |
cfa460ad | 242 | struct nand_chip *chip = mtd->priv; |
932394ac | 243 | |
cfa460ad WJ |
244 | for (i = 0; i < len; i++) |
245 | writeb(buf[i], chip->IO_ADDR_W); | |
932394ac WD |
246 | } |
247 | ||
248 | /** | |
ac7eb8a3 | 249 | * nand_read_buf - [DEFAULT] read chip data into buffer |
932394ac WD |
250 | * @mtd: MTD device structure |
251 | * @buf: buffer to store date | |
252 | * @len: number of bytes to read | |
253 | * | |
254 | * Default read function for 8bit buswith | |
255 | */ | |
cfa460ad | 256 | static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
932394ac WD |
257 | { |
258 | int i; | |
cfa460ad | 259 | struct nand_chip *chip = mtd->priv; |
932394ac | 260 | |
cfa460ad WJ |
261 | for (i = 0; i < len; i++) |
262 | buf[i] = readb(chip->IO_ADDR_R); | |
932394ac WD |
263 | } |
264 | ||
265 | /** | |
ac7eb8a3 | 266 | * nand_verify_buf - [DEFAULT] Verify chip data against buffer |
932394ac WD |
267 | * @mtd: MTD device structure |
268 | * @buf: buffer containing the data to compare | |
269 | * @len: number of bytes to compare | |
270 | * | |
271 | * Default verify function for 8bit buswith | |
272 | */ | |
cfa460ad | 273 | static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
932394ac WD |
274 | { |
275 | int i; | |
cfa460ad | 276 | struct nand_chip *chip = mtd->priv; |
932394ac | 277 | |
cfa460ad WJ |
278 | for (i = 0; i < len; i++) |
279 | if (buf[i] != readb(chip->IO_ADDR_R)) | |
932394ac | 280 | return -EFAULT; |
932394ac WD |
281 | return 0; |
282 | } | |
283 | ||
284 | /** | |
285 | * nand_write_buf16 - [DEFAULT] write buffer to chip | |
286 | * @mtd: MTD device structure | |
287 | * @buf: data buffer | |
288 | * @len: number of bytes to write | |
289 | * | |
290 | * Default write function for 16bit buswith | |
291 | */ | |
cfa460ad | 292 | static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len) |
932394ac WD |
293 | { |
294 | int i; | |
cfa460ad | 295 | struct nand_chip *chip = mtd->priv; |
932394ac WD |
296 | u16 *p = (u16 *) buf; |
297 | len >>= 1; | |
ac7eb8a3 | 298 | |
cfa460ad WJ |
299 | for (i = 0; i < len; i++) |
300 | writew(p[i], chip->IO_ADDR_W); | |
ac7eb8a3 | 301 | |
932394ac WD |
302 | } |
303 | ||
304 | /** | |
ac7eb8a3 | 305 | * nand_read_buf16 - [DEFAULT] read chip data into buffer |
932394ac WD |
306 | * @mtd: MTD device structure |
307 | * @buf: buffer to store date | |
308 | * @len: number of bytes to read | |
309 | * | |
310 | * Default read function for 16bit buswith | |
311 | */ | |
cfa460ad | 312 | static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len) |
932394ac WD |
313 | { |
314 | int i; | |
cfa460ad | 315 | struct nand_chip *chip = mtd->priv; |
932394ac WD |
316 | u16 *p = (u16 *) buf; |
317 | len >>= 1; | |
318 | ||
cfa460ad WJ |
319 | for (i = 0; i < len; i++) |
320 | p[i] = readw(chip->IO_ADDR_R); | |
932394ac WD |
321 | } |
322 | ||
323 | /** | |
ac7eb8a3 | 324 | * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer |
932394ac WD |
325 | * @mtd: MTD device structure |
326 | * @buf: buffer containing the data to compare | |
327 | * @len: number of bytes to compare | |
328 | * | |
329 | * Default verify function for 16bit buswith | |
330 | */ | |
cfa460ad | 331 | static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len) |
932394ac WD |
332 | { |
333 | int i; | |
cfa460ad | 334 | struct nand_chip *chip = mtd->priv; |
932394ac WD |
335 | u16 *p = (u16 *) buf; |
336 | len >>= 1; | |
337 | ||
cfa460ad WJ |
338 | for (i = 0; i < len; i++) |
339 | if (p[i] != readw(chip->IO_ADDR_R)) | |
932394ac WD |
340 | return -EFAULT; |
341 | ||
342 | return 0; | |
343 | } | |
344 | ||
345 | /** | |
346 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip | |
347 | * @mtd: MTD device structure | |
348 | * @ofs: offset from device start | |
349 | * @getchip: 0, if the chip is already selected | |
350 | * | |
ac7eb8a3 | 351 | * Check, if the block is bad. |
932394ac WD |
352 | */ |
353 | static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip) | |
354 | { | |
355 | int page, chipnr, res = 0; | |
cfa460ad | 356 | struct nand_chip *chip = mtd->priv; |
932394ac WD |
357 | u16 bad; |
358 | ||
cfa460ad | 359 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
a7988659 | 360 | |
932394ac | 361 | if (getchip) { |
cfa460ad | 362 | chipnr = (int)(ofs >> chip->chip_shift); |
932394ac | 363 | |
cfa460ad | 364 | nand_get_device(chip, mtd, FL_READING); |
932394ac WD |
365 | |
366 | /* Select the NAND device */ | |
cfa460ad | 367 | chip->select_chip(mtd, chipnr); |
a7988659 | 368 | } |
932394ac | 369 | |
cfa460ad WJ |
370 | if (chip->options & NAND_BUSWIDTH_16) { |
371 | chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE, | |
372 | page); | |
373 | bad = cpu_to_le16(chip->read_word(mtd)); | |
374 | if (chip->badblockpos & 0x1) | |
375 | bad >>= 8; | |
932394ac WD |
376 | if ((bad & 0xFF) != 0xff) |
377 | res = 1; | |
378 | } else { | |
cfa460ad WJ |
379 | chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page); |
380 | if (chip->read_byte(mtd) != 0xff) | |
932394ac WD |
381 | res = 1; |
382 | } | |
ac7eb8a3 | 383 | |
cfa460ad | 384 | if (getchip) |
932394ac | 385 | nand_release_device(mtd); |
ac7eb8a3 | 386 | |
932394ac WD |
387 | return res; |
388 | } | |
389 | ||
390 | /** | |
391 | * nand_default_block_markbad - [DEFAULT] mark a block bad | |
392 | * @mtd: MTD device structure | |
393 | * @ofs: offset from device start | |
394 | * | |
395 | * This is the default implementation, which can be overridden by | |
396 | * a hardware specific driver. | |
397 | */ | |
398 | static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) | |
399 | { | |
cfa460ad WJ |
400 | struct nand_chip *chip = mtd->priv; |
401 | uint8_t buf[2] = { 0, 0 }; | |
402 | int block, ret; | |
ac7eb8a3 | 403 | |
932394ac | 404 | /* Get block number */ |
cfa460ad WJ |
405 | block = (int)(ofs >> chip->bbt_erase_shift); |
406 | if (chip->bbt) | |
407 | chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1); | |
932394ac WD |
408 | |
409 | /* Do we have a flash based bad block table ? */ | |
cfa460ad WJ |
410 | if (chip->options & NAND_USE_FLASH_BBT) |
411 | ret = nand_update_bbt(mtd, ofs); | |
412 | else { | |
413 | /* We write two bytes, so we dont have to mess with 16 bit | |
414 | * access | |
415 | */ | |
c45912d8 | 416 | nand_get_device(chip, mtd, FL_WRITING); |
cfa460ad WJ |
417 | ofs += mtd->oobsize; |
418 | chip->ops.len = chip->ops.ooblen = 2; | |
419 | chip->ops.datbuf = NULL; | |
420 | chip->ops.oobbuf = buf; | |
421 | chip->ops.ooboffs = chip->badblockpos & ~0x01; | |
ac7eb8a3 | 422 | |
cfa460ad | 423 | ret = nand_do_write_oob(mtd, ofs, &chip->ops); |
c45912d8 | 424 | nand_release_device(mtd); |
cfa460ad WJ |
425 | } |
426 | if (!ret) | |
427 | mtd->ecc_stats.badblocks++; | |
c45912d8 | 428 | |
cfa460ad | 429 | return ret; |
932394ac WD |
430 | } |
431 | ||
ac7eb8a3 | 432 | /** |
932394ac WD |
433 | * nand_check_wp - [GENERIC] check if the chip is write protected |
434 | * @mtd: MTD device structure | |
ac7eb8a3 | 435 | * Check, if the device is write protected |
932394ac | 436 | * |
ac7eb8a3 | 437 | * The function expects, that the device is already selected |
932394ac | 438 | */ |
cfa460ad | 439 | static int nand_check_wp(struct mtd_info *mtd) |
932394ac | 440 | { |
cfa460ad | 441 | struct nand_chip *chip = mtd->priv; |
932394ac | 442 | /* Check the WP bit */ |
cfa460ad WJ |
443 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
444 | return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1; | |
932394ac | 445 | } |