Commit | Line | Data |
---|---|---|
1da177e4 LT |
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. | |
61b03bd7 | 8 | * |
1da177e4 | 9 | * Additional technical information is available on |
8b2b403c | 10 | * http://www.linux-mtd.infradead.org/doc/nand.html |
61b03bd7 | 11 | * |
1da177e4 | 12 | * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com) |
ace4dfee | 13 | * 2002-2006 Thomas Gleixner (tglx@linutronix.de) |
1da177e4 | 14 | * |
ace4dfee | 15 | * Credits: |
61b03bd7 TG |
16 | * David Woodhouse for adding multichip support |
17 | * | |
1da177e4 LT |
18 | * Aleph One Ltd. and Toby Churchill Ltd. for supporting the |
19 | * rework for 2K page size chips | |
20 | * | |
ace4dfee | 21 | * TODO: |
1da177e4 LT |
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. | |
c0b8ba7b | 27 | * BBT table is not serialized, has to be fixed |
1da177e4 | 28 | * |
1da177e4 LT |
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 | ||
552d9205 | 35 | #include <linux/module.h> |
1da177e4 LT |
36 | #include <linux/delay.h> |
37 | #include <linux/errno.h> | |
7aa65bfd | 38 | #include <linux/err.h> |
1da177e4 LT |
39 | #include <linux/sched.h> |
40 | #include <linux/slab.h> | |
41 | #include <linux/types.h> | |
42 | #include <linux/mtd/mtd.h> | |
43 | #include <linux/mtd/nand.h> | |
44 | #include <linux/mtd/nand_ecc.h> | |
45 | #include <linux/mtd/compatmac.h> | |
46 | #include <linux/interrupt.h> | |
47 | #include <linux/bitops.h> | |
8fe833c1 | 48 | #include <linux/leds.h> |
1da177e4 LT |
49 | #include <asm/io.h> |
50 | ||
51 | #ifdef CONFIG_MTD_PARTITIONS | |
52 | #include <linux/mtd/partitions.h> | |
53 | #endif | |
54 | ||
55 | /* Define default oob placement schemes for large and small page devices */ | |
5bd34c09 | 56 | static struct nand_ecclayout nand_oob_8 = { |
1da177e4 LT |
57 | .eccbytes = 3, |
58 | .eccpos = {0, 1, 2}, | |
5bd34c09 TG |
59 | .oobfree = { |
60 | {.offset = 3, | |
61 | .length = 2}, | |
62 | {.offset = 6, | |
63 | .length = 2}} | |
1da177e4 LT |
64 | }; |
65 | ||
5bd34c09 | 66 | static struct nand_ecclayout nand_oob_16 = { |
1da177e4 LT |
67 | .eccbytes = 6, |
68 | .eccpos = {0, 1, 2, 3, 6, 7}, | |
5bd34c09 TG |
69 | .oobfree = { |
70 | {.offset = 8, | |
71 | . length = 8}} | |
1da177e4 LT |
72 | }; |
73 | ||
5bd34c09 | 74 | static struct nand_ecclayout nand_oob_64 = { |
1da177e4 LT |
75 | .eccbytes = 24, |
76 | .eccpos = { | |
e0c7d767 DW |
77 | 40, 41, 42, 43, 44, 45, 46, 47, |
78 | 48, 49, 50, 51, 52, 53, 54, 55, | |
79 | 56, 57, 58, 59, 60, 61, 62, 63}, | |
5bd34c09 TG |
80 | .oobfree = { |
81 | {.offset = 2, | |
82 | .length = 38}} | |
1da177e4 LT |
83 | }; |
84 | ||
81ec5364 TG |
85 | static struct nand_ecclayout nand_oob_128 = { |
86 | .eccbytes = 48, | |
87 | .eccpos = { | |
88 | 80, 81, 82, 83, 84, 85, 86, 87, | |
89 | 88, 89, 90, 91, 92, 93, 94, 95, | |
90 | 96, 97, 98, 99, 100, 101, 102, 103, | |
91 | 104, 105, 106, 107, 108, 109, 110, 111, | |
92 | 112, 113, 114, 115, 116, 117, 118, 119, | |
93 | 120, 121, 122, 123, 124, 125, 126, 127}, | |
94 | .oobfree = { | |
95 | {.offset = 2, | |
96 | .length = 78}} | |
97 | }; | |
98 | ||
ace4dfee | 99 | static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, |
2c0a2bed | 100 | int new_state); |
1da177e4 | 101 | |
8593fbc6 TG |
102 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
103 | struct mtd_oob_ops *ops); | |
104 | ||
d470a97c | 105 | /* |
8e87d782 | 106 | * For devices which display every fart in the system on a separate LED. Is |
d470a97c TG |
107 | * compiled away when LED support is disabled. |
108 | */ | |
109 | DEFINE_LED_TRIGGER(nand_led_trigger); | |
110 | ||
1da177e4 LT |
111 | /** |
112 | * nand_release_device - [GENERIC] release chip | |
113 | * @mtd: MTD device structure | |
61b03bd7 TG |
114 | * |
115 | * Deselect, release chip lock and wake up anyone waiting on the device | |
1da177e4 | 116 | */ |
e0c7d767 | 117 | static void nand_release_device(struct mtd_info *mtd) |
1da177e4 | 118 | { |
ace4dfee | 119 | struct nand_chip *chip = mtd->priv; |
1da177e4 LT |
120 | |
121 | /* De-select the NAND device */ | |
ace4dfee | 122 | chip->select_chip(mtd, -1); |
0dfc6246 | 123 | |
a36ed299 | 124 | /* Release the controller and the chip */ |
ace4dfee TG |
125 | spin_lock(&chip->controller->lock); |
126 | chip->controller->active = NULL; | |
127 | chip->state = FL_READY; | |
128 | wake_up(&chip->controller->wq); | |
129 | spin_unlock(&chip->controller->lock); | |
1da177e4 LT |
130 | } |
131 | ||
132 | /** | |
133 | * nand_read_byte - [DEFAULT] read one byte from the chip | |
134 | * @mtd: MTD device structure | |
135 | * | |
136 | * Default read function for 8bit buswith | |
137 | */ | |
58dd8f2b | 138 | static uint8_t nand_read_byte(struct mtd_info *mtd) |
1da177e4 | 139 | { |
ace4dfee TG |
140 | struct nand_chip *chip = mtd->priv; |
141 | return readb(chip->IO_ADDR_R); | |
1da177e4 LT |
142 | } |
143 | ||
1da177e4 LT |
144 | /** |
145 | * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip | |
146 | * @mtd: MTD device structure | |
147 | * | |
61b03bd7 | 148 | * Default read function for 16bit buswith with |
1da177e4 LT |
149 | * endianess conversion |
150 | */ | |
58dd8f2b | 151 | static uint8_t nand_read_byte16(struct mtd_info *mtd) |
1da177e4 | 152 | { |
ace4dfee TG |
153 | struct nand_chip *chip = mtd->priv; |
154 | return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R)); | |
1da177e4 LT |
155 | } |
156 | ||
1da177e4 LT |
157 | /** |
158 | * nand_read_word - [DEFAULT] read one word from the chip | |
159 | * @mtd: MTD device structure | |
160 | * | |
61b03bd7 | 161 | * Default read function for 16bit buswith without |
1da177e4 LT |
162 | * endianess conversion |
163 | */ | |
164 | static u16 nand_read_word(struct mtd_info *mtd) | |
165 | { | |
ace4dfee TG |
166 | struct nand_chip *chip = mtd->priv; |
167 | return readw(chip->IO_ADDR_R); | |
1da177e4 LT |
168 | } |
169 | ||
1da177e4 LT |
170 | /** |
171 | * nand_select_chip - [DEFAULT] control CE line | |
172 | * @mtd: MTD device structure | |
844d3b42 | 173 | * @chipnr: chipnumber to select, -1 for deselect |
1da177e4 LT |
174 | * |
175 | * Default select function for 1 chip devices. | |
176 | */ | |
ace4dfee | 177 | static void nand_select_chip(struct mtd_info *mtd, int chipnr) |
1da177e4 | 178 | { |
ace4dfee TG |
179 | struct nand_chip *chip = mtd->priv; |
180 | ||
181 | switch (chipnr) { | |
1da177e4 | 182 | case -1: |
ace4dfee | 183 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE); |
1da177e4 LT |
184 | break; |
185 | case 0: | |
1da177e4 LT |
186 | break; |
187 | ||
188 | default: | |
189 | BUG(); | |
190 | } | |
191 | } | |
192 | ||
193 | /** | |
194 | * nand_write_buf - [DEFAULT] write buffer to chip | |
195 | * @mtd: MTD device structure | |
196 | * @buf: data buffer | |
197 | * @len: number of bytes to write | |
198 | * | |
199 | * Default write function for 8bit buswith | |
200 | */ | |
58dd8f2b | 201 | static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
1da177e4 LT |
202 | { |
203 | int i; | |
ace4dfee | 204 | struct nand_chip *chip = mtd->priv; |
1da177e4 | 205 | |
e0c7d767 | 206 | for (i = 0; i < len; i++) |
ace4dfee | 207 | writeb(buf[i], chip->IO_ADDR_W); |
1da177e4 LT |
208 | } |
209 | ||
210 | /** | |
61b03bd7 | 211 | * nand_read_buf - [DEFAULT] read chip data into buffer |
1da177e4 LT |
212 | * @mtd: MTD device structure |
213 | * @buf: buffer to store date | |
214 | * @len: number of bytes to read | |
215 | * | |
216 | * Default read function for 8bit buswith | |
217 | */ | |
58dd8f2b | 218 | static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
1da177e4 LT |
219 | { |
220 | int i; | |
ace4dfee | 221 | struct nand_chip *chip = mtd->priv; |
1da177e4 | 222 | |
e0c7d767 | 223 | for (i = 0; i < len; i++) |
ace4dfee | 224 | buf[i] = readb(chip->IO_ADDR_R); |
1da177e4 LT |
225 | } |
226 | ||
227 | /** | |
61b03bd7 | 228 | * nand_verify_buf - [DEFAULT] Verify chip data against buffer |
1da177e4 LT |
229 | * @mtd: MTD device structure |
230 | * @buf: buffer containing the data to compare | |
231 | * @len: number of bytes to compare | |
232 | * | |
233 | * Default verify function for 8bit buswith | |
234 | */ | |
58dd8f2b | 235 | static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
1da177e4 LT |
236 | { |
237 | int i; | |
ace4dfee | 238 | struct nand_chip *chip = mtd->priv; |
1da177e4 | 239 | |
e0c7d767 | 240 | for (i = 0; i < len; i++) |
ace4dfee | 241 | if (buf[i] != readb(chip->IO_ADDR_R)) |
1da177e4 | 242 | return -EFAULT; |
1da177e4 LT |
243 | return 0; |
244 | } | |
245 | ||
246 | /** | |
247 | * nand_write_buf16 - [DEFAULT] write buffer to chip | |
248 | * @mtd: MTD device structure | |
249 | * @buf: data buffer | |
250 | * @len: number of bytes to write | |
251 | * | |
252 | * Default write function for 16bit buswith | |
253 | */ | |
58dd8f2b | 254 | static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len) |
1da177e4 LT |
255 | { |
256 | int i; | |
ace4dfee | 257 | struct nand_chip *chip = mtd->priv; |
1da177e4 LT |
258 | u16 *p = (u16 *) buf; |
259 | len >>= 1; | |
61b03bd7 | 260 | |
e0c7d767 | 261 | for (i = 0; i < len; i++) |
ace4dfee | 262 | writew(p[i], chip->IO_ADDR_W); |
61b03bd7 | 263 | |
1da177e4 LT |
264 | } |
265 | ||
266 | /** | |
61b03bd7 | 267 | * nand_read_buf16 - [DEFAULT] read chip data into buffer |
1da177e4 LT |
268 | * @mtd: MTD device structure |
269 | * @buf: buffer to store date | |
270 | * @len: number of bytes to read | |
271 | * | |
272 | * Default read function for 16bit buswith | |
273 | */ | |
58dd8f2b | 274 | static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len) |
1da177e4 LT |
275 | { |
276 | int i; | |
ace4dfee | 277 | struct nand_chip *chip = mtd->priv; |
1da177e4 LT |
278 | u16 *p = (u16 *) buf; |
279 | len >>= 1; | |
280 | ||
e0c7d767 | 281 | for (i = 0; i < len; i++) |
ace4dfee | 282 | p[i] = readw(chip->IO_ADDR_R); |
1da177e4 LT |
283 | } |
284 | ||
285 | /** | |
61b03bd7 | 286 | * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer |
1da177e4 LT |
287 | * @mtd: MTD device structure |
288 | * @buf: buffer containing the data to compare | |
289 | * @len: number of bytes to compare | |
290 | * | |
291 | * Default verify function for 16bit buswith | |
292 | */ | |
58dd8f2b | 293 | static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len) |
1da177e4 LT |
294 | { |
295 | int i; | |
ace4dfee | 296 | struct nand_chip *chip = mtd->priv; |
1da177e4 LT |
297 | u16 *p = (u16 *) buf; |
298 | len >>= 1; | |
299 | ||
e0c7d767 | 300 | for (i = 0; i < len; i++) |
ace4dfee | 301 | if (p[i] != readw(chip->IO_ADDR_R)) |
1da177e4 LT |
302 | return -EFAULT; |
303 | ||
304 | return 0; | |
305 | } | |
306 | ||
307 | /** | |
308 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip | |
309 | * @mtd: MTD device structure | |
310 | * @ofs: offset from device start | |
311 | * @getchip: 0, if the chip is already selected | |
312 | * | |
61b03bd7 | 313 | * Check, if the block is bad. |
1da177e4 LT |
314 | */ |
315 | static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip) | |
316 | { | |
317 | int page, chipnr, res = 0; | |
ace4dfee | 318 | struct nand_chip *chip = mtd->priv; |
1da177e4 LT |
319 | u16 bad; |
320 | ||
1a12f46a TK |
321 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
322 | ||
1da177e4 | 323 | if (getchip) { |
ace4dfee | 324 | chipnr = (int)(ofs >> chip->chip_shift); |
1da177e4 | 325 | |
ace4dfee | 326 | nand_get_device(chip, mtd, FL_READING); |
1da177e4 LT |
327 | |
328 | /* Select the NAND device */ | |
ace4dfee | 329 | chip->select_chip(mtd, chipnr); |
1a12f46a | 330 | } |
1da177e4 | 331 | |
ace4dfee TG |
332 | if (chip->options & NAND_BUSWIDTH_16) { |
333 | chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE, | |
1a12f46a | 334 | page); |
ace4dfee TG |
335 | bad = cpu_to_le16(chip->read_word(mtd)); |
336 | if (chip->badblockpos & 0x1) | |
49196f33 | 337 | bad >>= 8; |
1da177e4 LT |
338 | if ((bad & 0xFF) != 0xff) |
339 | res = 1; | |
340 | } else { | |
1a12f46a | 341 | chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page); |
ace4dfee | 342 | if (chip->read_byte(mtd) != 0xff) |
1da177e4 LT |
343 | res = 1; |
344 | } | |
61b03bd7 | 345 | |
ace4dfee | 346 | if (getchip) |
1da177e4 | 347 | nand_release_device(mtd); |
61b03bd7 | 348 | |
1da177e4 LT |
349 | return res; |
350 | } | |
351 | ||
352 | /** | |
353 | * nand_default_block_markbad - [DEFAULT] mark a block bad | |
354 | * @mtd: MTD device structure | |
355 | * @ofs: offset from device start | |
356 | * | |
357 | * This is the default implementation, which can be overridden by | |
358 | * a hardware specific driver. | |
359 | */ | |
360 | static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) | |
361 | { | |
ace4dfee | 362 | struct nand_chip *chip = mtd->priv; |
58dd8f2b | 363 | uint8_t buf[2] = { 0, 0 }; |
f1a28c02 | 364 | int block, ret; |
61b03bd7 | 365 | |
1da177e4 | 366 | /* Get block number */ |
4226b510 | 367 | block = (int)(ofs >> chip->bbt_erase_shift); |
ace4dfee TG |
368 | if (chip->bbt) |
369 | chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1); | |
1da177e4 LT |
370 | |
371 | /* Do we have a flash based bad block table ? */ | |
ace4dfee | 372 | if (chip->options & NAND_USE_FLASH_BBT) |
f1a28c02 TG |
373 | ret = nand_update_bbt(mtd, ofs); |
374 | else { | |
375 | /* We write two bytes, so we dont have to mess with 16 bit | |
376 | * access | |
377 | */ | |
c0b8ba7b | 378 | nand_get_device(chip, mtd, FL_WRITING); |
f1a28c02 | 379 | ofs += mtd->oobsize; |
ff0dab64 | 380 | chip->ops.len = chip->ops.ooblen = 2; |
f1a28c02 TG |
381 | chip->ops.datbuf = NULL; |
382 | chip->ops.oobbuf = buf; | |
383 | chip->ops.ooboffs = chip->badblockpos & ~0x01; | |
384 | ||
385 | ret = nand_do_write_oob(mtd, ofs, &chip->ops); | |
c0b8ba7b | 386 | nand_release_device(mtd); |
f1a28c02 TG |
387 | } |
388 | if (!ret) | |
389 | mtd->ecc_stats.badblocks++; | |
c0b8ba7b | 390 | |
f1a28c02 | 391 | return ret; |
1da177e4 LT |
392 | } |
393 | ||
61b03bd7 | 394 | /** |
1da177e4 LT |
395 | * nand_check_wp - [GENERIC] check if the chip is write protected |
396 | * @mtd: MTD device structure | |
61b03bd7 | 397 | * Check, if the device is write protected |
1da177e4 | 398 | * |
61b03bd7 | 399 | * The function expects, that the device is already selected |
1da177e4 | 400 | */ |
e0c7d767 | 401 | static int nand_check_wp(struct mtd_info *mtd) |
1da177e4 | 402 | { |
ace4dfee | 403 | struct nand_chip *chip = mtd->priv; |
1da177e4 | 404 | /* Check the WP bit */ |
ace4dfee TG |
405 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
406 | return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1; | |
1da177e4 LT |
407 | } |
408 | ||
409 | /** | |
410 | * nand_block_checkbad - [GENERIC] Check if a block is marked bad | |
411 | * @mtd: MTD device structure | |
412 | * @ofs: offset from device start | |
413 | * @getchip: 0, if the chip is already selected | |
414 | * @allowbbt: 1, if its allowed to access the bbt area | |
415 | * | |
416 | * Check, if the block is bad. Either by reading the bad block table or | |
417 | * calling of the scan function. | |
418 | */ | |
2c0a2bed TG |
419 | static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, |
420 | int allowbbt) | |
1da177e4 | 421 | { |
ace4dfee | 422 | struct nand_chip *chip = mtd->priv; |
61b03bd7 | 423 | |
ace4dfee TG |
424 | if (!chip->bbt) |
425 | return chip->block_bad(mtd, ofs, getchip); | |
61b03bd7 | 426 | |
1da177e4 | 427 | /* Return info from the table */ |
e0c7d767 | 428 | return nand_isbad_bbt(mtd, ofs, allowbbt); |
1da177e4 LT |
429 | } |
430 | ||
61b03bd7 | 431 | /* |
3b88775c TG |
432 | * Wait for the ready pin, after a command |
433 | * The timeout is catched later. | |
434 | */ | |
4b648b02 | 435 | void nand_wait_ready(struct mtd_info *mtd) |
3b88775c | 436 | { |
ace4dfee | 437 | struct nand_chip *chip = mtd->priv; |
e0c7d767 | 438 | unsigned long timeo = jiffies + 2; |
3b88775c | 439 | |
8fe833c1 | 440 | led_trigger_event(nand_led_trigger, LED_FULL); |
3b88775c TG |
441 | /* wait until command is processed or timeout occures */ |
442 | do { | |
ace4dfee | 443 | if (chip->dev_ready(mtd)) |
8fe833c1 | 444 | break; |
8446f1d3 | 445 | touch_softlockup_watchdog(); |
61b03bd7 | 446 | } while (time_before(jiffies, timeo)); |
8fe833c1 | 447 | led_trigger_event(nand_led_trigger, LED_OFF); |
3b88775c | 448 | } |
4b648b02 | 449 | EXPORT_SYMBOL_GPL(nand_wait_ready); |
3b88775c | 450 | |
1da177e4 LT |
451 | /** |
452 | * nand_command - [DEFAULT] Send command to NAND device | |
453 | * @mtd: MTD device structure | |
454 | * @command: the command to be sent | |
455 | * @column: the column address for this command, -1 if none | |
456 | * @page_addr: the page address for this command, -1 if none | |
457 | * | |
458 | * Send command to NAND device. This function is used for small page | |
459 | * devices (256/512 Bytes per page) | |
460 | */ | |
7abd3ef9 TG |
461 | static void nand_command(struct mtd_info *mtd, unsigned int command, |
462 | int column, int page_addr) | |
1da177e4 | 463 | { |
ace4dfee | 464 | register struct nand_chip *chip = mtd->priv; |
7abd3ef9 | 465 | int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE; |
1da177e4 | 466 | |
1da177e4 LT |
467 | /* |
468 | * Write out the command to the device. | |
469 | */ | |
470 | if (command == NAND_CMD_SEQIN) { | |
471 | int readcmd; | |
472 | ||
28318776 | 473 | if (column >= mtd->writesize) { |
1da177e4 | 474 | /* OOB area */ |
28318776 | 475 | column -= mtd->writesize; |
1da177e4 LT |
476 | readcmd = NAND_CMD_READOOB; |
477 | } else if (column < 256) { | |
478 | /* First 256 bytes --> READ0 */ | |
479 | readcmd = NAND_CMD_READ0; | |
480 | } else { | |
481 | column -= 256; | |
482 | readcmd = NAND_CMD_READ1; | |
483 | } | |
ace4dfee | 484 | chip->cmd_ctrl(mtd, readcmd, ctrl); |
7abd3ef9 | 485 | ctrl &= ~NAND_CTRL_CHANGE; |
1da177e4 | 486 | } |
ace4dfee | 487 | chip->cmd_ctrl(mtd, command, ctrl); |
1da177e4 | 488 | |
7abd3ef9 TG |
489 | /* |
490 | * Address cycle, when necessary | |
491 | */ | |
492 | ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE; | |
493 | /* Serially input address */ | |
494 | if (column != -1) { | |
495 | /* Adjust columns for 16 bit buswidth */ | |
ace4dfee | 496 | if (chip->options & NAND_BUSWIDTH_16) |
7abd3ef9 | 497 | column >>= 1; |
ace4dfee | 498 | chip->cmd_ctrl(mtd, column, ctrl); |
7abd3ef9 TG |
499 | ctrl &= ~NAND_CTRL_CHANGE; |
500 | } | |
501 | if (page_addr != -1) { | |
ace4dfee | 502 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
7abd3ef9 | 503 | ctrl &= ~NAND_CTRL_CHANGE; |
ace4dfee | 504 | chip->cmd_ctrl(mtd, page_addr >> 8, ctrl); |
7abd3ef9 | 505 | /* One more address cycle for devices > 32MiB */ |
ace4dfee TG |
506 | if (chip->chipsize > (32 << 20)) |
507 | chip->cmd_ctrl(mtd, page_addr >> 16, ctrl); | |
1da177e4 | 508 | } |
ace4dfee | 509 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
61b03bd7 TG |
510 | |
511 | /* | |
512 | * program and erase have their own busy handlers | |
1da177e4 | 513 | * status and sequential in needs no delay |
e0c7d767 | 514 | */ |
1da177e4 | 515 | switch (command) { |
61b03bd7 | 516 | |
1da177e4 LT |
517 | case NAND_CMD_PAGEPROG: |
518 | case NAND_CMD_ERASE1: | |
519 | case NAND_CMD_ERASE2: | |
520 | case NAND_CMD_SEQIN: | |
521 | case NAND_CMD_STATUS: | |
522 | return; | |
523 | ||
524 | case NAND_CMD_RESET: | |
ace4dfee | 525 | if (chip->dev_ready) |
1da177e4 | 526 | break; |
ace4dfee TG |
527 | udelay(chip->chip_delay); |
528 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, | |
7abd3ef9 | 529 | NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
12efdde3 TG |
530 | chip->cmd_ctrl(mtd, |
531 | NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); | |
ace4dfee | 532 | while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ; |
1da177e4 LT |
533 | return; |
534 | ||
e0c7d767 | 535 | /* This applies to read commands */ |
1da177e4 | 536 | default: |
61b03bd7 | 537 | /* |
1da177e4 LT |
538 | * If we don't have access to the busy pin, we apply the given |
539 | * command delay | |
e0c7d767 | 540 | */ |
ace4dfee TG |
541 | if (!chip->dev_ready) { |
542 | udelay(chip->chip_delay); | |
1da177e4 | 543 | return; |
61b03bd7 | 544 | } |
1da177e4 | 545 | } |
1da177e4 LT |
546 | /* Apply this short delay always to ensure that we do wait tWB in |
547 | * any case on any machine. */ | |
e0c7d767 | 548 | ndelay(100); |
3b88775c TG |
549 | |
550 | nand_wait_ready(mtd); | |
1da177e4 LT |
551 | } |
552 | ||
553 | /** | |
554 | * nand_command_lp - [DEFAULT] Send command to NAND large page device | |
555 | * @mtd: MTD device structure | |
556 | * @command: the command to be sent | |
557 | * @column: the column address for this command, -1 if none | |
558 | * @page_addr: the page address for this command, -1 if none | |
559 | * | |
7abd3ef9 TG |
560 | * Send command to NAND device. This is the version for the new large page |
561 | * devices We dont have the separate regions as we have in the small page | |
562 | * devices. We must emulate NAND_CMD_READOOB to keep the code compatible. | |
1da177e4 | 563 | */ |
7abd3ef9 TG |
564 | static void nand_command_lp(struct mtd_info *mtd, unsigned int command, |
565 | int column, int page_addr) | |
1da177e4 | 566 | { |
ace4dfee | 567 | register struct nand_chip *chip = mtd->priv; |
1da177e4 LT |
568 | |
569 | /* Emulate NAND_CMD_READOOB */ | |
570 | if (command == NAND_CMD_READOOB) { | |
28318776 | 571 | column += mtd->writesize; |
1da177e4 LT |
572 | command = NAND_CMD_READ0; |
573 | } | |
61b03bd7 | 574 | |
7abd3ef9 | 575 | /* Command latch cycle */ |
ace4dfee | 576 | chip->cmd_ctrl(mtd, command & 0xff, |
7abd3ef9 | 577 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
1da177e4 LT |
578 | |
579 | if (column != -1 || page_addr != -1) { | |
7abd3ef9 | 580 | int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE; |
1da177e4 LT |
581 | |
582 | /* Serially input address */ | |
583 | if (column != -1) { | |
584 | /* Adjust columns for 16 bit buswidth */ | |
ace4dfee | 585 | if (chip->options & NAND_BUSWIDTH_16) |
1da177e4 | 586 | column >>= 1; |
ace4dfee | 587 | chip->cmd_ctrl(mtd, column, ctrl); |
7abd3ef9 | 588 | ctrl &= ~NAND_CTRL_CHANGE; |
ace4dfee | 589 | chip->cmd_ctrl(mtd, column >> 8, ctrl); |
61b03bd7 | 590 | } |
1da177e4 | 591 | if (page_addr != -1) { |
ace4dfee TG |
592 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
593 | chip->cmd_ctrl(mtd, page_addr >> 8, | |
7abd3ef9 | 594 | NAND_NCE | NAND_ALE); |
1da177e4 | 595 | /* One more address cycle for devices > 128MiB */ |
ace4dfee TG |
596 | if (chip->chipsize > (128 << 20)) |
597 | chip->cmd_ctrl(mtd, page_addr >> 16, | |
7abd3ef9 | 598 | NAND_NCE | NAND_ALE); |
1da177e4 | 599 | } |
1da177e4 | 600 | } |
ace4dfee | 601 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
61b03bd7 TG |
602 | |
603 | /* | |
604 | * program and erase have their own busy handlers | |
30f464b7 DM |
605 | * status, sequential in, and deplete1 need no delay |
606 | */ | |
1da177e4 | 607 | switch (command) { |
61b03bd7 | 608 | |
1da177e4 LT |
609 | case NAND_CMD_CACHEDPROG: |
610 | case NAND_CMD_PAGEPROG: | |
611 | case NAND_CMD_ERASE1: | |
612 | case NAND_CMD_ERASE2: | |
613 | case NAND_CMD_SEQIN: | |
7bc3312b | 614 | case NAND_CMD_RNDIN: |
1da177e4 | 615 | case NAND_CMD_STATUS: |
30f464b7 | 616 | case NAND_CMD_DEPLETE1: |
1da177e4 LT |
617 | return; |
618 | ||
e0c7d767 DW |
619 | /* |
620 | * read error status commands require only a short delay | |
621 | */ | |
30f464b7 DM |
622 | case NAND_CMD_STATUS_ERROR: |
623 | case NAND_CMD_STATUS_ERROR0: | |
624 | case NAND_CMD_STATUS_ERROR1: | |
625 | case NAND_CMD_STATUS_ERROR2: | |
626 | case NAND_CMD_STATUS_ERROR3: | |
ace4dfee | 627 | udelay(chip->chip_delay); |
30f464b7 | 628 | return; |
1da177e4 LT |
629 | |
630 | case NAND_CMD_RESET: | |
ace4dfee | 631 | if (chip->dev_ready) |
1da177e4 | 632 | break; |
ace4dfee | 633 | udelay(chip->chip_delay); |
12efdde3 TG |
634 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
635 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); | |
636 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, | |
637 | NAND_NCE | NAND_CTRL_CHANGE); | |
ace4dfee | 638 | while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ; |
1da177e4 LT |
639 | return; |
640 | ||
7bc3312b TG |
641 | case NAND_CMD_RNDOUT: |
642 | /* No ready / busy check necessary */ | |
643 | chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART, | |
644 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); | |
645 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, | |
646 | NAND_NCE | NAND_CTRL_CHANGE); | |
647 | return; | |
648 | ||
1da177e4 | 649 | case NAND_CMD_READ0: |
12efdde3 TG |
650 | chip->cmd_ctrl(mtd, NAND_CMD_READSTART, |
651 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); | |
652 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, | |
653 | NAND_NCE | NAND_CTRL_CHANGE); | |
61b03bd7 | 654 | |
e0c7d767 | 655 | /* This applies to read commands */ |
1da177e4 | 656 | default: |
61b03bd7 | 657 | /* |
1da177e4 LT |
658 | * If we don't have access to the busy pin, we apply the given |
659 | * command delay | |
e0c7d767 | 660 | */ |
ace4dfee TG |
661 | if (!chip->dev_ready) { |
662 | udelay(chip->chip_delay); | |
1da177e4 | 663 | return; |
61b03bd7 | 664 | } |
1da177e4 | 665 | } |
3b88775c | 666 | |
1da177e4 LT |
667 | /* Apply this short delay always to ensure that we do wait tWB in |
668 | * any case on any machine. */ | |
e0c7d767 | 669 | ndelay(100); |
3b88775c TG |
670 | |
671 | nand_wait_ready(mtd); | |
1da177e4 LT |
672 | } |
673 | ||
674 | /** | |
675 | * nand_get_device - [GENERIC] Get chip for selected access | |
844d3b42 | 676 | * @chip: the nand chip descriptor |
1da177e4 | 677 | * @mtd: MTD device structure |
61b03bd7 | 678 | * @new_state: the state which is requested |
1da177e4 LT |
679 | * |
680 | * Get the device and lock it for exclusive access | |
681 | */ | |
2c0a2bed | 682 | static int |
ace4dfee | 683 | nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state) |
1da177e4 | 684 | { |
ace4dfee TG |
685 | spinlock_t *lock = &chip->controller->lock; |
686 | wait_queue_head_t *wq = &chip->controller->wq; | |
e0c7d767 | 687 | DECLARE_WAITQUEUE(wait, current); |
e0c7d767 | 688 | retry: |
0dfc6246 TG |
689 | spin_lock(lock); |
690 | ||
b8b3ee9a | 691 | /* Hardware controller shared among independent devices */ |
ace4dfee TG |
692 | if (!chip->controller->active) |
693 | chip->controller->active = chip; | |
a36ed299 | 694 | |
ace4dfee TG |
695 | if (chip->controller->active == chip && chip->state == FL_READY) { |
696 | chip->state = new_state; | |
0dfc6246 | 697 | spin_unlock(lock); |
962034f4 VW |
698 | return 0; |
699 | } | |
700 | if (new_state == FL_PM_SUSPENDED) { | |
701 | spin_unlock(lock); | |
ace4dfee | 702 | return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN; |
0dfc6246 TG |
703 | } |
704 | set_current_state(TASK_UNINTERRUPTIBLE); | |
705 | add_wait_queue(wq, &wait); | |
706 | spin_unlock(lock); | |
707 | schedule(); | |
708 | remove_wait_queue(wq, &wait); | |
1da177e4 LT |
709 | goto retry; |
710 | } | |
711 | ||
712 | /** | |
713 | * nand_wait - [DEFAULT] wait until the command is done | |
714 | * @mtd: MTD device structure | |
844d3b42 | 715 | * @chip: NAND chip structure |
1da177e4 LT |
716 | * |
717 | * Wait for command done. This applies to erase and program only | |
61b03bd7 | 718 | * Erase can take up to 400ms and program up to 20ms according to |
1da177e4 | 719 | * general NAND and SmartMedia specs |
844d3b42 | 720 | */ |
7bc3312b | 721 | static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip) |
1da177e4 LT |
722 | { |
723 | ||
e0c7d767 | 724 | unsigned long timeo = jiffies; |
7bc3312b | 725 | int status, state = chip->state; |
61b03bd7 | 726 | |
1da177e4 | 727 | if (state == FL_ERASING) |
e0c7d767 | 728 | timeo += (HZ * 400) / 1000; |
1da177e4 | 729 | else |
e0c7d767 | 730 | timeo += (HZ * 20) / 1000; |
1da177e4 | 731 | |
8fe833c1 RP |
732 | led_trigger_event(nand_led_trigger, LED_FULL); |
733 | ||
1da177e4 LT |
734 | /* Apply this short delay always to ensure that we do wait tWB in |
735 | * any case on any machine. */ | |
e0c7d767 | 736 | ndelay(100); |
1da177e4 | 737 | |
ace4dfee TG |
738 | if ((state == FL_ERASING) && (chip->options & NAND_IS_AND)) |
739 | chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1); | |
61b03bd7 | 740 | else |
ace4dfee | 741 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
1da177e4 | 742 | |
61b03bd7 | 743 | while (time_before(jiffies, timeo)) { |
ace4dfee TG |
744 | if (chip->dev_ready) { |
745 | if (chip->dev_ready(mtd)) | |
61b03bd7 | 746 | break; |
1da177e4 | 747 | } else { |
ace4dfee | 748 | if (chip->read_byte(mtd) & NAND_STATUS_READY) |
1da177e4 LT |
749 | break; |
750 | } | |
20a6c211 | 751 | cond_resched(); |
1da177e4 | 752 | } |
8fe833c1 RP |
753 | led_trigger_event(nand_led_trigger, LED_OFF); |
754 | ||
ace4dfee | 755 | status = (int)chip->read_byte(mtd); |
1da177e4 LT |
756 | return status; |
757 | } | |
758 | ||
8593fbc6 TG |
759 | /** |
760 | * nand_read_page_raw - [Intern] read raw page data without ecc | |
761 | * @mtd: mtd info structure | |
762 | * @chip: nand chip info structure | |
763 | * @buf: buffer to store read data | |
52ff49df DB |
764 | * |
765 | * Not for syndrome calculating ecc controllers, which use a special oob layout | |
8593fbc6 TG |
766 | */ |
767 | static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip, | |
46a8cf2d | 768 | uint8_t *buf, int page) |
8593fbc6 TG |
769 | { |
770 | chip->read_buf(mtd, buf, mtd->writesize); | |
771 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); | |
772 | return 0; | |
773 | } | |
774 | ||
52ff49df DB |
775 | /** |
776 | * nand_read_page_raw_syndrome - [Intern] read raw page data without ecc | |
777 | * @mtd: mtd info structure | |
778 | * @chip: nand chip info structure | |
779 | * @buf: buffer to store read data | |
780 | * | |
781 | * We need a special oob layout and handling even when OOB isn't used. | |
782 | */ | |
783 | static int nand_read_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip, | |
46a8cf2d | 784 | uint8_t *buf, int page) |
52ff49df DB |
785 | { |
786 | int eccsize = chip->ecc.size; | |
787 | int eccbytes = chip->ecc.bytes; | |
788 | uint8_t *oob = chip->oob_poi; | |
789 | int steps, size; | |
790 | ||
791 | for (steps = chip->ecc.steps; steps > 0; steps--) { | |
792 | chip->read_buf(mtd, buf, eccsize); | |
793 | buf += eccsize; | |
794 | ||
795 | if (chip->ecc.prepad) { | |
796 | chip->read_buf(mtd, oob, chip->ecc.prepad); | |
797 | oob += chip->ecc.prepad; | |
798 | } | |
799 | ||
800 | chip->read_buf(mtd, oob, eccbytes); | |
801 | oob += eccbytes; | |
802 | ||
803 | if (chip->ecc.postpad) { | |
804 | chip->read_buf(mtd, oob, chip->ecc.postpad); | |
805 | oob += chip->ecc.postpad; | |
806 | } | |
807 | } | |
808 | ||
809 | size = mtd->oobsize - (oob - chip->oob_poi); | |
810 | if (size) | |
811 | chip->read_buf(mtd, oob, size); | |
812 | ||
813 | return 0; | |
814 | } | |
815 | ||
1da177e4 | 816 | /** |
d29ebdbe | 817 | * nand_read_page_swecc - [REPLACABLE] software ecc based page read function |
f5bbdacc TG |
818 | * @mtd: mtd info structure |
819 | * @chip: nand chip info structure | |
820 | * @buf: buffer to store read data | |
068e3c0a | 821 | */ |
f5bbdacc | 822 | static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
46a8cf2d | 823 | uint8_t *buf, int page) |
1da177e4 | 824 | { |
f5bbdacc TG |
825 | int i, eccsize = chip->ecc.size; |
826 | int eccbytes = chip->ecc.bytes; | |
827 | int eccsteps = chip->ecc.steps; | |
828 | uint8_t *p = buf; | |
4bf63fcb DW |
829 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
830 | uint8_t *ecc_code = chip->buffers->ecccode; | |
8b099a39 | 831 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
f5bbdacc | 832 | |
46a8cf2d | 833 | chip->ecc.read_page_raw(mtd, chip, buf, page); |
f5bbdacc TG |
834 | |
835 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) | |
836 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); | |
837 | ||
838 | for (i = 0; i < chip->ecc.total; i++) | |
f75e5097 | 839 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
f5bbdacc TG |
840 | |
841 | eccsteps = chip->ecc.steps; | |
842 | p = buf; | |
843 | ||
844 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { | |
845 | int stat; | |
846 | ||
847 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); | |
c32b8dcc | 848 | if (stat < 0) |
f5bbdacc TG |
849 | mtd->ecc_stats.failed++; |
850 | else | |
851 | mtd->ecc_stats.corrected += stat; | |
852 | } | |
853 | return 0; | |
22c60f5f | 854 | } |
1da177e4 | 855 | |
3d459559 AK |
856 | /** |
857 | * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function | |
858 | * @mtd: mtd info structure | |
859 | * @chip: nand chip info structure | |
17c1d2be AK |
860 | * @data_offs: offset of requested data within the page |
861 | * @readlen: data length | |
862 | * @bufpoi: buffer to store read data | |
3d459559 AK |
863 | */ |
864 | static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi) | |
865 | { | |
866 | int start_step, end_step, num_steps; | |
867 | uint32_t *eccpos = chip->ecc.layout->eccpos; | |
868 | uint8_t *p; | |
869 | int data_col_addr, i, gaps = 0; | |
870 | int datafrag_len, eccfrag_len, aligned_len, aligned_pos; | |
871 | int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1; | |
872 | ||
873 | /* Column address wihin the page aligned to ECC size (256bytes). */ | |
874 | start_step = data_offs / chip->ecc.size; | |
875 | end_step = (data_offs + readlen - 1) / chip->ecc.size; | |
876 | num_steps = end_step - start_step + 1; | |
877 | ||
878 | /* Data size aligned to ECC ecc.size*/ | |
879 | datafrag_len = num_steps * chip->ecc.size; | |
880 | eccfrag_len = num_steps * chip->ecc.bytes; | |
881 | ||
882 | data_col_addr = start_step * chip->ecc.size; | |
883 | /* If we read not a page aligned data */ | |
884 | if (data_col_addr != 0) | |
885 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1); | |
886 | ||
887 | p = bufpoi + data_col_addr; | |
888 | chip->read_buf(mtd, p, datafrag_len); | |
889 | ||
890 | /* Calculate ECC */ | |
891 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) | |
892 | chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]); | |
893 | ||
894 | /* The performance is faster if to position offsets | |
895 | according to ecc.pos. Let make sure here that | |
896 | there are no gaps in ecc positions */ | |
897 | for (i = 0; i < eccfrag_len - 1; i++) { | |
898 | if (eccpos[i + start_step * chip->ecc.bytes] + 1 != | |
899 | eccpos[i + start_step * chip->ecc.bytes + 1]) { | |
900 | gaps = 1; | |
901 | break; | |
902 | } | |
903 | } | |
904 | if (gaps) { | |
905 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1); | |
906 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); | |
907 | } else { | |
908 | /* send the command to read the particular ecc bytes */ | |
909 | /* take care about buswidth alignment in read_buf */ | |
910 | aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1); | |
911 | aligned_len = eccfrag_len; | |
912 | if (eccpos[start_step * chip->ecc.bytes] & (busw - 1)) | |
913 | aligned_len++; | |
914 | if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1)) | |
915 | aligned_len++; | |
916 | ||
917 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1); | |
918 | chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len); | |
919 | } | |
920 | ||
921 | for (i = 0; i < eccfrag_len; i++) | |
922 | chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]]; | |
923 | ||
924 | p = bufpoi + data_col_addr; | |
925 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) { | |
926 | int stat; | |
927 | ||
928 | stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]); | |
929 | if (stat == -1) | |
930 | mtd->ecc_stats.failed++; | |
931 | else | |
932 | mtd->ecc_stats.corrected += stat; | |
933 | } | |
934 | return 0; | |
935 | } | |
936 | ||
068e3c0a | 937 | /** |
d29ebdbe | 938 | * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function |
f5bbdacc TG |
939 | * @mtd: mtd info structure |
940 | * @chip: nand chip info structure | |
941 | * @buf: buffer to store read data | |
068e3c0a | 942 | * |
f5bbdacc | 943 | * Not for syndrome calculating ecc controllers which need a special oob layout |
068e3c0a | 944 | */ |
f5bbdacc | 945 | static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
46a8cf2d | 946 | uint8_t *buf, int page) |
1da177e4 | 947 | { |
f5bbdacc TG |
948 | int i, eccsize = chip->ecc.size; |
949 | int eccbytes = chip->ecc.bytes; | |
950 | int eccsteps = chip->ecc.steps; | |
951 | uint8_t *p = buf; | |
4bf63fcb DW |
952 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
953 | uint8_t *ecc_code = chip->buffers->ecccode; | |
8b099a39 | 954 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
f5bbdacc TG |
955 | |
956 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { | |
957 | chip->ecc.hwctl(mtd, NAND_ECC_READ); | |
958 | chip->read_buf(mtd, p, eccsize); | |
959 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); | |
1da177e4 | 960 | } |
f75e5097 | 961 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
1da177e4 | 962 | |
f5bbdacc | 963 | for (i = 0; i < chip->ecc.total; i++) |
f75e5097 | 964 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
1da177e4 | 965 | |
f5bbdacc TG |
966 | eccsteps = chip->ecc.steps; |
967 | p = buf; | |
61b03bd7 | 968 | |
f5bbdacc TG |
969 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
970 | int stat; | |
1da177e4 | 971 | |
f5bbdacc | 972 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
c32b8dcc | 973 | if (stat < 0) |
f5bbdacc TG |
974 | mtd->ecc_stats.failed++; |
975 | else | |
976 | mtd->ecc_stats.corrected += stat; | |
977 | } | |
978 | return 0; | |
979 | } | |
1da177e4 | 980 | |
f5bbdacc | 981 | /** |
d29ebdbe | 982 | * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read |
f5bbdacc TG |
983 | * @mtd: mtd info structure |
984 | * @chip: nand chip info structure | |
985 | * @buf: buffer to store read data | |
986 | * | |
987 | * The hw generator calculates the error syndrome automatically. Therefor | |
f75e5097 | 988 | * we need a special oob layout and handling. |
f5bbdacc TG |
989 | */ |
990 | static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip, | |
46a8cf2d | 991 | uint8_t *buf, int page) |
f5bbdacc TG |
992 | { |
993 | int i, eccsize = chip->ecc.size; | |
994 | int eccbytes = chip->ecc.bytes; | |
995 | int eccsteps = chip->ecc.steps; | |
996 | uint8_t *p = buf; | |
f75e5097 | 997 | uint8_t *oob = chip->oob_poi; |
1da177e4 | 998 | |
f5bbdacc TG |
999 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
1000 | int stat; | |
61b03bd7 | 1001 | |
f5bbdacc TG |
1002 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
1003 | chip->read_buf(mtd, p, eccsize); | |
1da177e4 | 1004 | |
f5bbdacc TG |
1005 | if (chip->ecc.prepad) { |
1006 | chip->read_buf(mtd, oob, chip->ecc.prepad); | |
1007 | oob += chip->ecc.prepad; | |
1008 | } | |
1da177e4 | 1009 | |
f5bbdacc TG |
1010 | chip->ecc.hwctl(mtd, NAND_ECC_READSYN); |
1011 | chip->read_buf(mtd, oob, eccbytes); | |
1012 | stat = chip->ecc.correct(mtd, p, oob, NULL); | |
61b03bd7 | 1013 | |
c32b8dcc | 1014 | if (stat < 0) |
f5bbdacc | 1015 | mtd->ecc_stats.failed++; |
61b03bd7 | 1016 | else |
f5bbdacc | 1017 | mtd->ecc_stats.corrected += stat; |
61b03bd7 | 1018 | |
f5bbdacc | 1019 | oob += eccbytes; |
1da177e4 | 1020 | |
f5bbdacc TG |
1021 | if (chip->ecc.postpad) { |
1022 | chip->read_buf(mtd, oob, chip->ecc.postpad); | |
1023 | oob += chip->ecc.postpad; | |
61b03bd7 | 1024 | } |
f5bbdacc | 1025 | } |
1da177e4 | 1026 | |
f5bbdacc | 1027 | /* Calculate remaining oob bytes */ |
7e4178f9 | 1028 | i = mtd->oobsize - (oob - chip->oob_poi); |
f5bbdacc TG |
1029 | if (i) |
1030 | chip->read_buf(mtd, oob, i); | |
61b03bd7 | 1031 | |
f5bbdacc TG |
1032 | return 0; |
1033 | } | |
1da177e4 | 1034 | |
f5bbdacc | 1035 | /** |
8593fbc6 TG |
1036 | * nand_transfer_oob - [Internal] Transfer oob to client buffer |
1037 | * @chip: nand chip structure | |
844d3b42 | 1038 | * @oob: oob destination address |
8593fbc6 | 1039 | * @ops: oob ops structure |
7014568b | 1040 | * @len: size of oob to transfer |
8593fbc6 TG |
1041 | */ |
1042 | static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob, | |
7014568b | 1043 | struct mtd_oob_ops *ops, size_t len) |
8593fbc6 | 1044 | { |
8593fbc6 TG |
1045 | switch(ops->mode) { |
1046 | ||
1047 | case MTD_OOB_PLACE: | |
1048 | case MTD_OOB_RAW: | |
1049 | memcpy(oob, chip->oob_poi + ops->ooboffs, len); | |
1050 | return oob + len; | |
1051 | ||
1052 | case MTD_OOB_AUTO: { | |
1053 | struct nand_oobfree *free = chip->ecc.layout->oobfree; | |
7bc3312b TG |
1054 | uint32_t boffs = 0, roffs = ops->ooboffs; |
1055 | size_t bytes = 0; | |
8593fbc6 TG |
1056 | |
1057 | for(; free->length && len; free++, len -= bytes) { | |
7bc3312b TG |
1058 | /* Read request not from offset 0 ? */ |
1059 | if (unlikely(roffs)) { | |
1060 | if (roffs >= free->length) { | |
1061 | roffs -= free->length; | |
1062 | continue; | |
1063 | } | |
1064 | boffs = free->offset + roffs; | |
1065 | bytes = min_t(size_t, len, | |
1066 | (free->length - roffs)); | |
1067 | roffs = 0; | |
1068 | } else { | |
1069 | bytes = min_t(size_t, len, free->length); | |
1070 | boffs = free->offset; | |
1071 | } | |
1072 | memcpy(oob, chip->oob_poi + boffs, bytes); | |
8593fbc6 TG |
1073 | oob += bytes; |
1074 | } | |
1075 | return oob; | |
1076 | } | |
1077 | default: | |
1078 | BUG(); | |
1079 | } | |
1080 | return NULL; | |
1081 | } | |
1082 | ||
1083 | /** | |
1084 | * nand_do_read_ops - [Internal] Read data with ECC | |
f5bbdacc TG |
1085 | * |
1086 | * @mtd: MTD device structure | |
1087 | * @from: offset to read from | |
844d3b42 | 1088 | * @ops: oob ops structure |
f5bbdacc TG |
1089 | * |
1090 | * Internal function. Called with chip held. | |
1091 | */ | |
8593fbc6 TG |
1092 | static int nand_do_read_ops(struct mtd_info *mtd, loff_t from, |
1093 | struct mtd_oob_ops *ops) | |
f5bbdacc TG |
1094 | { |
1095 | int chipnr, page, realpage, col, bytes, aligned; | |
1096 | struct nand_chip *chip = mtd->priv; | |
1097 | struct mtd_ecc_stats stats; | |
1098 | int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1; | |
1099 | int sndcmd = 1; | |
1100 | int ret = 0; | |
8593fbc6 | 1101 | uint32_t readlen = ops->len; |
7014568b | 1102 | uint32_t oobreadlen = ops->ooblen; |
8593fbc6 | 1103 | uint8_t *bufpoi, *oob, *buf; |
1da177e4 | 1104 | |
f5bbdacc | 1105 | stats = mtd->ecc_stats; |
1da177e4 | 1106 | |
f5bbdacc TG |
1107 | chipnr = (int)(from >> chip->chip_shift); |
1108 | chip->select_chip(mtd, chipnr); | |
61b03bd7 | 1109 | |
f5bbdacc TG |
1110 | realpage = (int)(from >> chip->page_shift); |
1111 | page = realpage & chip->pagemask; | |
1da177e4 | 1112 | |
f5bbdacc | 1113 | col = (int)(from & (mtd->writesize - 1)); |
61b03bd7 | 1114 | |
8593fbc6 TG |
1115 | buf = ops->datbuf; |
1116 | oob = ops->oobbuf; | |
1117 | ||
f5bbdacc TG |
1118 | while(1) { |
1119 | bytes = min(mtd->writesize - col, readlen); | |
1120 | aligned = (bytes == mtd->writesize); | |
61b03bd7 | 1121 | |
f5bbdacc | 1122 | /* Is the current page in the buffer ? */ |
8593fbc6 | 1123 | if (realpage != chip->pagebuf || oob) { |
4bf63fcb | 1124 | bufpoi = aligned ? buf : chip->buffers->databuf; |
61b03bd7 | 1125 | |
f5bbdacc TG |
1126 | if (likely(sndcmd)) { |
1127 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page); | |
1128 | sndcmd = 0; | |
1da177e4 | 1129 | } |
1da177e4 | 1130 | |
f5bbdacc | 1131 | /* Now read the page into the buffer */ |
956e944c | 1132 | if (unlikely(ops->mode == MTD_OOB_RAW)) |
46a8cf2d SN |
1133 | ret = chip->ecc.read_page_raw(mtd, chip, |
1134 | bufpoi, page); | |
3d459559 AK |
1135 | else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob) |
1136 | ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi); | |
956e944c | 1137 | else |
46a8cf2d SN |
1138 | ret = chip->ecc.read_page(mtd, chip, bufpoi, |
1139 | page); | |
f5bbdacc | 1140 | if (ret < 0) |
1da177e4 | 1141 | break; |
f5bbdacc TG |
1142 | |
1143 | /* Transfer not aligned data */ | |
1144 | if (!aligned) { | |
3d459559 AK |
1145 | if (!NAND_SUBPAGE_READ(chip) && !oob) |
1146 | chip->pagebuf = realpage; | |
4bf63fcb | 1147 | memcpy(buf, chip->buffers->databuf + col, bytes); |
f5bbdacc TG |
1148 | } |
1149 | ||
8593fbc6 TG |
1150 | buf += bytes; |
1151 | ||
1152 | if (unlikely(oob)) { | |
1153 | /* Raw mode does data:oob:data:oob */ | |
7014568b VW |
1154 | if (ops->mode != MTD_OOB_RAW) { |
1155 | int toread = min(oobreadlen, | |
1156 | chip->ecc.layout->oobavail); | |
1157 | if (toread) { | |
1158 | oob = nand_transfer_oob(chip, | |
1159 | oob, ops, toread); | |
1160 | oobreadlen -= toread; | |
1161 | } | |
1162 | } else | |
1163 | buf = nand_transfer_oob(chip, | |
1164 | buf, ops, mtd->oobsize); | |
8593fbc6 TG |
1165 | } |
1166 | ||
f5bbdacc TG |
1167 | if (!(chip->options & NAND_NO_READRDY)) { |
1168 | /* | |
1169 | * Apply delay or wait for ready/busy pin. Do | |
1170 | * this before the AUTOINCR check, so no | |
1171 | * problems arise if a chip which does auto | |
1172 | * increment is marked as NOAUTOINCR by the | |
1173 | * board driver. | |
1174 | */ | |
1175 | if (!chip->dev_ready) | |
1176 | udelay(chip->chip_delay); | |
1177 | else | |
1178 | nand_wait_ready(mtd); | |
1da177e4 | 1179 | } |
8593fbc6 | 1180 | } else { |
4bf63fcb | 1181 | memcpy(buf, chip->buffers->databuf + col, bytes); |
8593fbc6 TG |
1182 | buf += bytes; |
1183 | } | |
1da177e4 | 1184 | |
f5bbdacc | 1185 | readlen -= bytes; |
61b03bd7 | 1186 | |
f5bbdacc | 1187 | if (!readlen) |
61b03bd7 | 1188 | break; |
1da177e4 LT |
1189 | |
1190 | /* For subsequent reads align to page boundary. */ | |
1191 | col = 0; | |
1192 | /* Increment page address */ | |
1193 | realpage++; | |
1194 | ||
ace4dfee | 1195 | page = realpage & chip->pagemask; |
1da177e4 LT |
1196 | /* Check, if we cross a chip boundary */ |
1197 | if (!page) { | |
1198 | chipnr++; | |
ace4dfee TG |
1199 | chip->select_chip(mtd, -1); |
1200 | chip->select_chip(mtd, chipnr); | |
1da177e4 | 1201 | } |
f5bbdacc | 1202 | |
61b03bd7 TG |
1203 | /* Check, if the chip supports auto page increment |
1204 | * or if we have hit a block boundary. | |
e0c7d767 | 1205 | */ |
f5bbdacc | 1206 | if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck)) |
61b03bd7 | 1207 | sndcmd = 1; |
1da177e4 LT |
1208 | } |
1209 | ||
8593fbc6 | 1210 | ops->retlen = ops->len - (size_t) readlen; |
7014568b VW |
1211 | if (oob) |
1212 | ops->oobretlen = ops->ooblen - oobreadlen; | |
1da177e4 | 1213 | |
f5bbdacc TG |
1214 | if (ret) |
1215 | return ret; | |
1216 | ||
9a1fcdfd TG |
1217 | if (mtd->ecc_stats.failed - stats.failed) |
1218 | return -EBADMSG; | |
1219 | ||
1220 | return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0; | |
f5bbdacc TG |
1221 | } |
1222 | ||
1223 | /** | |
1224 | * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc | |
1225 | * @mtd: MTD device structure | |
1226 | * @from: offset to read from | |
1227 | * @len: number of bytes to read | |
1228 | * @retlen: pointer to variable to store the number of read bytes | |
1229 | * @buf: the databuffer to put data | |
1230 | * | |
1231 | * Get hold of the chip and call nand_do_read | |
1232 | */ | |
1233 | static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, | |
1234 | size_t *retlen, uint8_t *buf) | |
1235 | { | |
8593fbc6 | 1236 | struct nand_chip *chip = mtd->priv; |
f5bbdacc TG |
1237 | int ret; |
1238 | ||
f5bbdacc TG |
1239 | /* Do not allow reads past end of device */ |
1240 | if ((from + len) > mtd->size) | |
1241 | return -EINVAL; | |
1242 | if (!len) | |
1243 | return 0; | |
1244 | ||
8593fbc6 | 1245 | nand_get_device(chip, mtd, FL_READING); |
f5bbdacc | 1246 | |
8593fbc6 TG |
1247 | chip->ops.len = len; |
1248 | chip->ops.datbuf = buf; | |
1249 | chip->ops.oobbuf = NULL; | |
1250 | ||
1251 | ret = nand_do_read_ops(mtd, from, &chip->ops); | |
f5bbdacc | 1252 | |
7fd5aecc RP |
1253 | *retlen = chip->ops.retlen; |
1254 | ||
f5bbdacc TG |
1255 | nand_release_device(mtd); |
1256 | ||
1257 | return ret; | |
1da177e4 LT |
1258 | } |
1259 | ||
7bc3312b TG |
1260 | /** |
1261 | * nand_read_oob_std - [REPLACABLE] the most common OOB data read function | |
1262 | * @mtd: mtd info structure | |
1263 | * @chip: nand chip info structure | |
1264 | * @page: page number to read | |
1265 | * @sndcmd: flag whether to issue read command or not | |
1266 | */ | |
1267 | static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, | |
1268 | int page, int sndcmd) | |
1269 | { | |
1270 | if (sndcmd) { | |
1271 | chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); | |
1272 | sndcmd = 0; | |
1273 | } | |
1274 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); | |
1275 | return sndcmd; | |
1276 | } | |
1277 | ||
1278 | /** | |
1279 | * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC | |
1280 | * with syndromes | |
1281 | * @mtd: mtd info structure | |
1282 | * @chip: nand chip info structure | |
1283 | * @page: page number to read | |
1284 | * @sndcmd: flag whether to issue read command or not | |
1285 | */ | |
1286 | static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip, | |
1287 | int page, int sndcmd) | |
1288 | { | |
1289 | uint8_t *buf = chip->oob_poi; | |
1290 | int length = mtd->oobsize; | |
1291 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; | |
1292 | int eccsize = chip->ecc.size; | |
1293 | uint8_t *bufpoi = buf; | |
1294 | int i, toread, sndrnd = 0, pos; | |
1295 | ||
1296 | chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page); | |
1297 | for (i = 0; i < chip->ecc.steps; i++) { | |
1298 | if (sndrnd) { | |
1299 | pos = eccsize + i * (eccsize + chunk); | |
1300 | if (mtd->writesize > 512) | |
1301 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1); | |
1302 | else | |
1303 | chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page); | |
1304 | } else | |
1305 | sndrnd = 1; | |
1306 | toread = min_t(int, length, chunk); | |
1307 | chip->read_buf(mtd, bufpoi, toread); | |
1308 | bufpoi += toread; | |
1309 | length -= toread; | |
1310 | } | |
1311 | if (length > 0) | |
1312 | chip->read_buf(mtd, bufpoi, length); | |
1313 | ||
1314 | return 1; | |
1315 | } | |
1316 | ||
1317 | /** | |
1318 | * nand_write_oob_std - [REPLACABLE] the most common OOB data write function | |
1319 | * @mtd: mtd info structure | |
1320 | * @chip: nand chip info structure | |
1321 | * @page: page number to write | |
1322 | */ | |
1323 | static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, | |
1324 | int page) | |
1325 | { | |
1326 | int status = 0; | |
1327 | const uint8_t *buf = chip->oob_poi; | |
1328 | int length = mtd->oobsize; | |
1329 | ||
1330 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page); | |
1331 | chip->write_buf(mtd, buf, length); | |
1332 | /* Send command to program the OOB data */ | |
1333 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); | |
1334 | ||
1335 | status = chip->waitfunc(mtd, chip); | |
1336 | ||
0d420f9d | 1337 | return status & NAND_STATUS_FAIL ? -EIO : 0; |
7bc3312b TG |
1338 | } |
1339 | ||
1340 | /** | |
1341 | * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC | |
1342 | * with syndrome - only for large page flash ! | |
1343 | * @mtd: mtd info structure | |
1344 | * @chip: nand chip info structure | |
1345 | * @page: page number to write | |
1346 | */ | |
1347 | static int nand_write_oob_syndrome(struct mtd_info *mtd, | |
1348 | struct nand_chip *chip, int page) | |
1349 | { | |
1350 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; | |
1351 | int eccsize = chip->ecc.size, length = mtd->oobsize; | |
1352 | int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps; | |
1353 | const uint8_t *bufpoi = chip->oob_poi; | |
1354 | ||
1355 | /* | |
1356 | * data-ecc-data-ecc ... ecc-oob | |
1357 | * or | |
1358 | * data-pad-ecc-pad-data-pad .... ecc-pad-oob | |
1359 | */ | |
1360 | if (!chip->ecc.prepad && !chip->ecc.postpad) { | |
1361 | pos = steps * (eccsize + chunk); | |
1362 | steps = 0; | |
1363 | } else | |
8b0036ee | 1364 | pos = eccsize; |
7bc3312b TG |
1365 | |
1366 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page); | |
1367 | for (i = 0; i < steps; i++) { | |
1368 | if (sndcmd) { | |
1369 | if (mtd->writesize <= 512) { | |
1370 | uint32_t fill = 0xFFFFFFFF; | |
1371 | ||
1372 | len = eccsize; | |
1373 | while (len > 0) { | |
1374 | int num = min_t(int, len, 4); | |
1375 | chip->write_buf(mtd, (uint8_t *)&fill, | |
1376 | num); | |
1377 | len -= num; | |
1378 | } | |
1379 | } else { | |
1380 | pos = eccsize + i * (eccsize + chunk); | |
1381 | chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1); | |
1382 | } | |
1383 | } else | |
1384 | sndcmd = 1; | |
1385 | len = min_t(int, length, chunk); | |
1386 | chip->write_buf(mtd, bufpoi, len); | |
1387 | bufpoi += len; | |
1388 | length -= len; | |
1389 | } | |
1390 | if (length > 0) | |
1391 | chip->write_buf(mtd, bufpoi, length); | |
1392 | ||
1393 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); | |
1394 | status = chip->waitfunc(mtd, chip); | |
1395 | ||
1396 | return status & NAND_STATUS_FAIL ? -EIO : 0; | |
1397 | } | |
1398 | ||
1da177e4 | 1399 | /** |
8593fbc6 | 1400 | * nand_do_read_oob - [Intern] NAND read out-of-band |
1da177e4 LT |
1401 | * @mtd: MTD device structure |
1402 | * @from: offset to read from | |
8593fbc6 | 1403 | * @ops: oob operations description structure |
1da177e4 LT |
1404 | * |
1405 | * NAND read out-of-band data from the spare area | |
1406 | */ | |
8593fbc6 TG |
1407 | static int nand_do_read_oob(struct mtd_info *mtd, loff_t from, |
1408 | struct mtd_oob_ops *ops) | |
1da177e4 | 1409 | { |
7bc3312b | 1410 | int page, realpage, chipnr, sndcmd = 1; |
ace4dfee | 1411 | struct nand_chip *chip = mtd->priv; |
7314e9e7 | 1412 | int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1; |
7014568b VW |
1413 | int readlen = ops->ooblen; |
1414 | int len; | |
7bc3312b | 1415 | uint8_t *buf = ops->oobbuf; |
61b03bd7 | 1416 | |
20d8e248 | 1417 | DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08Lx, len = %i\n", |
1418 | __func__, (unsigned long long)from, readlen); | |
1da177e4 | 1419 | |
03736155 | 1420 | if (ops->mode == MTD_OOB_AUTO) |
7014568b | 1421 | len = chip->ecc.layout->oobavail; |
03736155 AH |
1422 | else |
1423 | len = mtd->oobsize; | |
1424 | ||
1425 | if (unlikely(ops->ooboffs >= len)) { | |
20d8e248 | 1426 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start read " |
1427 | "outside oob\n", __func__); | |
03736155 AH |
1428 | return -EINVAL; |
1429 | } | |
1430 | ||
1431 | /* Do not allow reads past end of device */ | |
1432 | if (unlikely(from >= mtd->size || | |
1433 | ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) - | |
1434 | (from >> chip->page_shift)) * len)) { | |
20d8e248 | 1435 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read beyond end " |
1436 | "of device\n", __func__); | |
03736155 AH |
1437 | return -EINVAL; |
1438 | } | |
7014568b | 1439 | |
7314e9e7 | 1440 | chipnr = (int)(from >> chip->chip_shift); |
ace4dfee | 1441 | chip->select_chip(mtd, chipnr); |
1da177e4 | 1442 | |
7314e9e7 TG |
1443 | /* Shift to get page */ |
1444 | realpage = (int)(from >> chip->page_shift); | |
1445 | page = realpage & chip->pagemask; | |
1da177e4 | 1446 | |
7314e9e7 | 1447 | while(1) { |
7bc3312b | 1448 | sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd); |
7014568b VW |
1449 | |
1450 | len = min(len, readlen); | |
1451 | buf = nand_transfer_oob(chip, buf, ops, len); | |
8593fbc6 | 1452 | |
7314e9e7 TG |
1453 | if (!(chip->options & NAND_NO_READRDY)) { |
1454 | /* | |
1455 | * Apply delay or wait for ready/busy pin. Do this | |
1456 | * before the AUTOINCR check, so no problems arise if a | |
1457 | * chip which does auto increment is marked as | |
1458 | * NOAUTOINCR by the board driver. | |
19870da7 | 1459 | */ |
ace4dfee TG |
1460 | if (!chip->dev_ready) |
1461 | udelay(chip->chip_delay); | |
19870da7 TG |
1462 | else |
1463 | nand_wait_ready(mtd); | |
7314e9e7 | 1464 | } |
19870da7 | 1465 | |
7014568b | 1466 | readlen -= len; |
0d420f9d SZ |
1467 | if (!readlen) |
1468 | break; | |
1469 | ||
7314e9e7 TG |
1470 | /* Increment page address */ |
1471 | realpage++; | |
1472 | ||
1473 | page = realpage & chip->pagemask; | |
1474 | /* Check, if we cross a chip boundary */ | |
1475 | if (!page) { | |
1476 | chipnr++; | |
1477 | chip->select_chip(mtd, -1); | |
1478 | chip->select_chip(mtd, chipnr); | |
1da177e4 | 1479 | } |
7314e9e7 TG |
1480 | |
1481 | /* Check, if the chip supports auto page increment | |
1482 | * or if we have hit a block boundary. | |
1483 | */ | |
1484 | if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck)) | |
1485 | sndcmd = 1; | |
1da177e4 LT |
1486 | } |
1487 | ||
7014568b | 1488 | ops->oobretlen = ops->ooblen; |
1da177e4 LT |
1489 | return 0; |
1490 | } | |
1491 | ||
1492 | /** | |
8593fbc6 | 1493 | * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band |
1da177e4 | 1494 | * @mtd: MTD device structure |
1da177e4 | 1495 | * @from: offset to read from |
8593fbc6 | 1496 | * @ops: oob operation description structure |
1da177e4 | 1497 | * |
8593fbc6 | 1498 | * NAND read data and/or out-of-band data |
1da177e4 | 1499 | */ |
8593fbc6 TG |
1500 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, |
1501 | struct mtd_oob_ops *ops) | |
1da177e4 | 1502 | { |
ace4dfee | 1503 | struct nand_chip *chip = mtd->priv; |
8593fbc6 TG |
1504 | int ret = -ENOTSUPP; |
1505 | ||
1506 | ops->retlen = 0; | |
1da177e4 LT |
1507 | |
1508 | /* Do not allow reads past end of device */ | |
7014568b | 1509 | if (ops->datbuf && (from + ops->len) > mtd->size) { |
20d8e248 | 1510 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read " |
1511 | "beyond end of device\n", __func__); | |
1da177e4 LT |
1512 | return -EINVAL; |
1513 | } | |
1514 | ||
ace4dfee | 1515 | nand_get_device(chip, mtd, FL_READING); |
1da177e4 | 1516 | |
8593fbc6 TG |
1517 | switch(ops->mode) { |
1518 | case MTD_OOB_PLACE: | |
1519 | case MTD_OOB_AUTO: | |
8593fbc6 | 1520 | case MTD_OOB_RAW: |
8593fbc6 | 1521 | break; |
1da177e4 | 1522 | |
8593fbc6 TG |
1523 | default: |
1524 | goto out; | |
1525 | } | |
1da177e4 | 1526 | |
8593fbc6 TG |
1527 | if (!ops->datbuf) |
1528 | ret = nand_do_read_oob(mtd, from, ops); | |
1529 | else | |
1530 | ret = nand_do_read_ops(mtd, from, ops); | |
61b03bd7 | 1531 | |
8593fbc6 TG |
1532 | out: |
1533 | nand_release_device(mtd); | |
1534 | return ret; | |
1535 | } | |
61b03bd7 | 1536 | |
1da177e4 | 1537 | |
8593fbc6 TG |
1538 | /** |
1539 | * nand_write_page_raw - [Intern] raw page write function | |
1540 | * @mtd: mtd info structure | |
1541 | * @chip: nand chip info structure | |
1542 | * @buf: data buffer | |
52ff49df DB |
1543 | * |
1544 | * Not for syndrome calculating ecc controllers, which use a special oob layout | |
8593fbc6 TG |
1545 | */ |
1546 | static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip, | |
1547 | const uint8_t *buf) | |
1548 | { | |
1549 | chip->write_buf(mtd, buf, mtd->writesize); | |
1550 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); | |
1da177e4 LT |
1551 | } |
1552 | ||
52ff49df DB |
1553 | /** |
1554 | * nand_write_page_raw_syndrome - [Intern] raw page write function | |
1555 | * @mtd: mtd info structure | |
1556 | * @chip: nand chip info structure | |
1557 | * @buf: data buffer | |
1558 | * | |
1559 | * We need a special oob layout and handling even when ECC isn't checked. | |
1560 | */ | |
1561 | static void nand_write_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip, | |
1562 | const uint8_t *buf) | |
1563 | { | |
1564 | int eccsize = chip->ecc.size; | |
1565 | int eccbytes = chip->ecc.bytes; | |
1566 | uint8_t *oob = chip->oob_poi; | |
1567 | int steps, size; | |
1568 | ||
1569 | for (steps = chip->ecc.steps; steps > 0; steps--) { | |
1570 | chip->write_buf(mtd, buf, eccsize); | |
1571 | buf += eccsize; | |
1572 | ||
1573 | if (chip->ecc.prepad) { | |
1574 | chip->write_buf(mtd, oob, chip->ecc.prepad); | |
1575 | oob += chip->ecc.prepad; | |
1576 | } | |
1577 | ||
1578 | chip->read_buf(mtd, oob, eccbytes); | |
1579 | oob += eccbytes; | |
1580 | ||
1581 | if (chip->ecc.postpad) { | |
1582 | chip->write_buf(mtd, oob, chip->ecc.postpad); | |
1583 | oob += chip->ecc.postpad; | |
1584 | } | |
1585 | } | |
1586 | ||
1587 | size = mtd->oobsize - (oob - chip->oob_poi); | |
1588 | if (size) | |
1589 | chip->write_buf(mtd, oob, size); | |
1590 | } | |
9223a456 | 1591 | /** |
d29ebdbe | 1592 | * nand_write_page_swecc - [REPLACABLE] software ecc based page write function |
f75e5097 TG |
1593 | * @mtd: mtd info structure |
1594 | * @chip: nand chip info structure | |
1595 | * @buf: data buffer | |
9223a456 | 1596 | */ |
f75e5097 TG |
1597 | static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
1598 | const uint8_t *buf) | |
9223a456 | 1599 | { |
f75e5097 TG |
1600 | int i, eccsize = chip->ecc.size; |
1601 | int eccbytes = chip->ecc.bytes; | |
1602 | int eccsteps = chip->ecc.steps; | |
4bf63fcb | 1603 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
f75e5097 | 1604 | const uint8_t *p = buf; |
8b099a39 | 1605 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
9223a456 | 1606 | |
8593fbc6 TG |
1607 | /* Software ecc calculation */ |
1608 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) | |
1609 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); | |
9223a456 | 1610 | |
8593fbc6 TG |
1611 | for (i = 0; i < chip->ecc.total; i++) |
1612 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; | |
9223a456 | 1613 | |
90424de8 | 1614 | chip->ecc.write_page_raw(mtd, chip, buf); |
f75e5097 | 1615 | } |
9223a456 | 1616 | |
f75e5097 | 1617 | /** |
d29ebdbe | 1618 | * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function |
f75e5097 TG |
1619 | * @mtd: mtd info structure |
1620 | * @chip: nand chip info structure | |
1621 | * @buf: data buffer | |
1622 | */ | |
1623 | static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, | |
1624 | const uint8_t *buf) | |
1625 | { | |
1626 | int i, eccsize = chip->ecc.size; | |
1627 | int eccbytes = chip->ecc.bytes; | |
1628 | int eccsteps = chip->ecc.steps; | |
4bf63fcb | 1629 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
f75e5097 | 1630 | const uint8_t *p = buf; |
8b099a39 | 1631 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
9223a456 | 1632 | |
f75e5097 TG |
1633 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
1634 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); | |
29da9cea | 1635 | chip->write_buf(mtd, p, eccsize); |
f75e5097 | 1636 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
9223a456 TG |
1637 | } |
1638 | ||
f75e5097 TG |
1639 | for (i = 0; i < chip->ecc.total; i++) |
1640 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; | |
1641 | ||
1642 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); | |
9223a456 TG |
1643 | } |
1644 | ||
61b03bd7 | 1645 | /** |
d29ebdbe | 1646 | * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write |
f75e5097 TG |
1647 | * @mtd: mtd info structure |
1648 | * @chip: nand chip info structure | |
1649 | * @buf: data buffer | |
1da177e4 | 1650 | * |
f75e5097 TG |
1651 | * The hw generator calculates the error syndrome automatically. Therefor |
1652 | * we need a special oob layout and handling. | |
1653 | */ | |
1654 | static void nand_write_page_syndrome(struct mtd_info *mtd, | |
1655 | struct nand_chip *chip, const uint8_t *buf) | |
1da177e4 | 1656 | { |
f75e5097 TG |
1657 | int i, eccsize = chip->ecc.size; |
1658 | int eccbytes = chip->ecc.bytes; | |
1659 | int eccsteps = chip->ecc.steps; | |
1660 | const uint8_t *p = buf; | |
1661 | uint8_t *oob = chip->oob_poi; | |
1da177e4 | 1662 | |
f75e5097 | 1663 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
1da177e4 | 1664 | |
f75e5097 TG |
1665 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
1666 | chip->write_buf(mtd, p, eccsize); | |
61b03bd7 | 1667 | |
f75e5097 TG |
1668 | if (chip->ecc.prepad) { |
1669 | chip->write_buf(mtd, oob, chip->ecc.prepad); | |
1670 | oob += chip->ecc.prepad; | |
1671 | } | |
1672 | ||
1673 | chip->ecc.calculate(mtd, p, oob); | |
1674 | chip->write_buf(mtd, oob, eccbytes); | |
1675 | oob += eccbytes; | |
1676 | ||
1677 | if (chip->ecc.postpad) { | |
1678 | chip->write_buf(mtd, oob, chip->ecc.postpad); | |
1679 | oob += chip->ecc.postpad; | |
1da177e4 | 1680 | } |
1da177e4 | 1681 | } |
f75e5097 TG |
1682 | |
1683 | /* Calculate remaining oob bytes */ | |
7e4178f9 | 1684 | i = mtd->oobsize - (oob - chip->oob_poi); |
f75e5097 TG |
1685 | if (i) |
1686 | chip->write_buf(mtd, oob, i); | |
1687 | } | |
1688 | ||
1689 | /** | |
956e944c | 1690 | * nand_write_page - [REPLACEABLE] write one page |
f75e5097 TG |
1691 | * @mtd: MTD device structure |
1692 | * @chip: NAND chip descriptor | |
1693 | * @buf: the data to write | |
1694 | * @page: page number to write | |
1695 | * @cached: cached programming | |
efbfe96c | 1696 | * @raw: use _raw version of write_page |
f75e5097 TG |
1697 | */ |
1698 | static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip, | |
956e944c | 1699 | const uint8_t *buf, int page, int cached, int raw) |
f75e5097 TG |
1700 | { |
1701 | int status; | |
1702 | ||
1703 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page); | |
1704 | ||
956e944c DW |
1705 | if (unlikely(raw)) |
1706 | chip->ecc.write_page_raw(mtd, chip, buf); | |
1707 | else | |
1708 | chip->ecc.write_page(mtd, chip, buf); | |
f75e5097 TG |
1709 | |
1710 | /* | |
1711 | * Cached progamming disabled for now, Not sure if its worth the | |
1712 | * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s) | |
1713 | */ | |
1714 | cached = 0; | |
1715 | ||
1716 | if (!cached || !(chip->options & NAND_CACHEPRG)) { | |
1717 | ||
1718 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); | |
7bc3312b | 1719 | status = chip->waitfunc(mtd, chip); |
f75e5097 TG |
1720 | /* |
1721 | * See if operation failed and additional status checks are | |
1722 | * available | |
1723 | */ | |
1724 | if ((status & NAND_STATUS_FAIL) && (chip->errstat)) | |
1725 | status = chip->errstat(mtd, chip, FL_WRITING, status, | |
1726 | page); | |
1727 | ||
1728 | if (status & NAND_STATUS_FAIL) | |
1729 | return -EIO; | |
1730 | } else { | |
1731 | chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1); | |
7bc3312b | 1732 | status = chip->waitfunc(mtd, chip); |
f75e5097 TG |
1733 | } |
1734 | ||
1735 | #ifdef CONFIG_MTD_NAND_VERIFY_WRITE | |
1736 | /* Send command to read back the data */ | |
1737 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page); | |
1738 | ||
1739 | if (chip->verify_buf(mtd, buf, mtd->writesize)) | |
1740 | return -EIO; | |
1741 | #endif | |
1742 | return 0; | |
1da177e4 LT |
1743 | } |
1744 | ||
8593fbc6 TG |
1745 | /** |
1746 | * nand_fill_oob - [Internal] Transfer client buffer to oob | |
1747 | * @chip: nand chip structure | |
1748 | * @oob: oob data buffer | |
1749 | * @ops: oob ops structure | |
1750 | */ | |
1751 | static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob, | |
1752 | struct mtd_oob_ops *ops) | |
1753 | { | |
1754 | size_t len = ops->ooblen; | |
1755 | ||
1756 | switch(ops->mode) { | |
1757 | ||
1758 | case MTD_OOB_PLACE: | |
1759 | case MTD_OOB_RAW: | |
1760 | memcpy(chip->oob_poi + ops->ooboffs, oob, len); | |
1761 | return oob + len; | |
1762 | ||
1763 | case MTD_OOB_AUTO: { | |
1764 | struct nand_oobfree *free = chip->ecc.layout->oobfree; | |
7bc3312b TG |
1765 | uint32_t boffs = 0, woffs = ops->ooboffs; |
1766 | size_t bytes = 0; | |
8593fbc6 TG |
1767 | |
1768 | for(; free->length && len; free++, len -= bytes) { | |
7bc3312b TG |
1769 | /* Write request not from offset 0 ? */ |
1770 | if (unlikely(woffs)) { | |
1771 | if (woffs >= free->length) { | |
1772 | woffs -= free->length; | |
1773 | continue; | |
1774 | } | |
1775 | boffs = free->offset + woffs; | |
1776 | bytes = min_t(size_t, len, | |
1777 | (free->length - woffs)); | |
1778 | woffs = 0; | |
1779 | } else { | |
1780 | bytes = min_t(size_t, len, free->length); | |
1781 | boffs = free->offset; | |
1782 | } | |
8b0036ee | 1783 | memcpy(chip->oob_poi + boffs, oob, bytes); |
8593fbc6 TG |
1784 | oob += bytes; |
1785 | } | |
1786 | return oob; | |
1787 | } | |
1788 | default: | |
1789 | BUG(); | |
1790 | } | |
1791 | return NULL; | |
1792 | } | |
1793 | ||
29072b96 | 1794 | #define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0 |
1da177e4 LT |
1795 | |
1796 | /** | |
8593fbc6 | 1797 | * nand_do_write_ops - [Internal] NAND write with ECC |
1da177e4 LT |
1798 | * @mtd: MTD device structure |
1799 | * @to: offset to write to | |
8593fbc6 | 1800 | * @ops: oob operations description structure |
1da177e4 LT |
1801 | * |
1802 | * NAND write with ECC | |
1803 | */ | |
8593fbc6 TG |
1804 | static int nand_do_write_ops(struct mtd_info *mtd, loff_t to, |
1805 | struct mtd_oob_ops *ops) | |
1da177e4 | 1806 | { |
29072b96 | 1807 | int chipnr, realpage, page, blockmask, column; |
ace4dfee | 1808 | struct nand_chip *chip = mtd->priv; |
8593fbc6 TG |
1809 | uint32_t writelen = ops->len; |
1810 | uint8_t *oob = ops->oobbuf; | |
1811 | uint8_t *buf = ops->datbuf; | |
29072b96 | 1812 | int ret, subpage; |
1da177e4 | 1813 | |
8593fbc6 | 1814 | ops->retlen = 0; |
29072b96 TG |
1815 | if (!writelen) |
1816 | return 0; | |
1da177e4 | 1817 | |
61b03bd7 | 1818 | /* reject writes, which are not page aligned */ |
8593fbc6 | 1819 | if (NOTALIGNED(to) || NOTALIGNED(ops->len)) { |
20d8e248 | 1820 | printk(KERN_NOTICE "%s: Attempt to write not " |
1821 | "page aligned data\n", __func__); | |
1da177e4 LT |
1822 | return -EINVAL; |
1823 | } | |
1824 | ||
29072b96 TG |
1825 | column = to & (mtd->writesize - 1); |
1826 | subpage = column || (writelen & (mtd->writesize - 1)); | |
1827 | ||
1828 | if (subpage && oob) | |
1829 | return -EINVAL; | |
1da177e4 | 1830 | |
6a930961 TG |
1831 | chipnr = (int)(to >> chip->chip_shift); |
1832 | chip->select_chip(mtd, chipnr); | |
1833 | ||
1da177e4 LT |
1834 | /* Check, if it is write protected */ |
1835 | if (nand_check_wp(mtd)) | |
8593fbc6 | 1836 | return -EIO; |
1da177e4 | 1837 | |
f75e5097 TG |
1838 | realpage = (int)(to >> chip->page_shift); |
1839 | page = realpage & chip->pagemask; | |
1840 | blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1; | |
1841 | ||
1842 | /* Invalidate the page cache, when we write to the cached page */ | |
1843 | if (to <= (chip->pagebuf << chip->page_shift) && | |
8593fbc6 | 1844 | (chip->pagebuf << chip->page_shift) < (to + ops->len)) |
ace4dfee | 1845 | chip->pagebuf = -1; |
61b03bd7 | 1846 | |
7dcdcbef DW |
1847 | /* If we're not given explicit OOB data, let it be 0xFF */ |
1848 | if (likely(!oob)) | |
1849 | memset(chip->oob_poi, 0xff, mtd->oobsize); | |
61b03bd7 | 1850 | |
f75e5097 | 1851 | while(1) { |
29072b96 | 1852 | int bytes = mtd->writesize; |
f75e5097 | 1853 | int cached = writelen > bytes && page != blockmask; |
29072b96 TG |
1854 | uint8_t *wbuf = buf; |
1855 | ||
1856 | /* Partial page write ? */ | |
1857 | if (unlikely(column || writelen < (mtd->writesize - 1))) { | |
1858 | cached = 0; | |
1859 | bytes = min_t(int, bytes - column, (int) writelen); | |
1860 | chip->pagebuf = -1; | |
1861 | memset(chip->buffers->databuf, 0xff, mtd->writesize); | |
1862 | memcpy(&chip->buffers->databuf[column], buf, bytes); | |
1863 | wbuf = chip->buffers->databuf; | |
1864 | } | |
1da177e4 | 1865 | |
8593fbc6 TG |
1866 | if (unlikely(oob)) |
1867 | oob = nand_fill_oob(chip, oob, ops); | |
1868 | ||
29072b96 | 1869 | ret = chip->write_page(mtd, chip, wbuf, page, cached, |
956e944c | 1870 | (ops->mode == MTD_OOB_RAW)); |
f75e5097 TG |
1871 | if (ret) |
1872 | break; | |
1873 | ||
1874 | writelen -= bytes; | |
1875 | if (!writelen) | |
1876 | break; | |
1877 | ||
29072b96 | 1878 | column = 0; |
f75e5097 TG |
1879 | buf += bytes; |
1880 | realpage++; | |
1881 | ||
1882 | page = realpage & chip->pagemask; | |
1883 | /* Check, if we cross a chip boundary */ | |
1884 | if (!page) { | |
1885 | chipnr++; | |
1886 | chip->select_chip(mtd, -1); | |
1887 | chip->select_chip(mtd, chipnr); | |
1da177e4 LT |
1888 | } |
1889 | } | |
8593fbc6 | 1890 | |
8593fbc6 | 1891 | ops->retlen = ops->len - writelen; |
7014568b VW |
1892 | if (unlikely(oob)) |
1893 | ops->oobretlen = ops->ooblen; | |
1da177e4 LT |
1894 | return ret; |
1895 | } | |
1896 | ||
f75e5097 | 1897 | /** |
8593fbc6 | 1898 | * nand_write - [MTD Interface] NAND write with ECC |
f75e5097 | 1899 | * @mtd: MTD device structure |
f75e5097 TG |
1900 | * @to: offset to write to |
1901 | * @len: number of bytes to write | |
8593fbc6 TG |
1902 | * @retlen: pointer to variable to store the number of written bytes |
1903 | * @buf: the data to write | |
f75e5097 | 1904 | * |
8593fbc6 | 1905 | * NAND write with ECC |
f75e5097 | 1906 | */ |
8593fbc6 TG |
1907 | static int nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
1908 | size_t *retlen, const uint8_t *buf) | |
f75e5097 TG |
1909 | { |
1910 | struct nand_chip *chip = mtd->priv; | |
f75e5097 TG |
1911 | int ret; |
1912 | ||
8593fbc6 TG |
1913 | /* Do not allow reads past end of device */ |
1914 | if ((to + len) > mtd->size) | |
f75e5097 | 1915 | return -EINVAL; |
8593fbc6 TG |
1916 | if (!len) |
1917 | return 0; | |
f75e5097 | 1918 | |
7bc3312b | 1919 | nand_get_device(chip, mtd, FL_WRITING); |
f75e5097 | 1920 | |
8593fbc6 TG |
1921 | chip->ops.len = len; |
1922 | chip->ops.datbuf = (uint8_t *)buf; | |
1923 | chip->ops.oobbuf = NULL; | |
f75e5097 | 1924 | |
8593fbc6 | 1925 | ret = nand_do_write_ops(mtd, to, &chip->ops); |
f75e5097 | 1926 | |
7fd5aecc RP |
1927 | *retlen = chip->ops.retlen; |
1928 | ||
f75e5097 | 1929 | nand_release_device(mtd); |
8593fbc6 | 1930 | |
8593fbc6 | 1931 | return ret; |
f75e5097 | 1932 | } |
7314e9e7 | 1933 | |
1da177e4 | 1934 | /** |
8593fbc6 | 1935 | * nand_do_write_oob - [MTD Interface] NAND write out-of-band |
1da177e4 LT |
1936 | * @mtd: MTD device structure |
1937 | * @to: offset to write to | |
8593fbc6 | 1938 | * @ops: oob operation description structure |
1da177e4 LT |
1939 | * |
1940 | * NAND write out-of-band | |
1941 | */ | |
8593fbc6 TG |
1942 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
1943 | struct mtd_oob_ops *ops) | |
1da177e4 | 1944 | { |
03736155 | 1945 | int chipnr, page, status, len; |
ace4dfee | 1946 | struct nand_chip *chip = mtd->priv; |
1da177e4 | 1947 | |
20d8e248 | 1948 | DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n", |
1949 | __func__, (unsigned int)to, (int)ops->ooblen); | |
1da177e4 | 1950 | |
03736155 AH |
1951 | if (ops->mode == MTD_OOB_AUTO) |
1952 | len = chip->ecc.layout->oobavail; | |
1953 | else | |
1954 | len = mtd->oobsize; | |
1955 | ||
1da177e4 | 1956 | /* Do not allow write past end of page */ |
03736155 | 1957 | if ((ops->ooboffs + ops->ooblen) > len) { |
20d8e248 | 1958 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to write " |
1959 | "past end of page\n", __func__); | |
1da177e4 LT |
1960 | return -EINVAL; |
1961 | } | |
1962 | ||
03736155 | 1963 | if (unlikely(ops->ooboffs >= len)) { |
20d8e248 | 1964 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start " |
1965 | "write outside oob\n", __func__); | |
03736155 AH |
1966 | return -EINVAL; |
1967 | } | |
1968 | ||
1969 | /* Do not allow reads past end of device */ | |
1970 | if (unlikely(to >= mtd->size || | |
1971 | ops->ooboffs + ops->ooblen > | |
1972 | ((mtd->size >> chip->page_shift) - | |
1973 | (to >> chip->page_shift)) * len)) { | |
20d8e248 | 1974 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond " |
1975 | "end of device\n", __func__); | |
03736155 AH |
1976 | return -EINVAL; |
1977 | } | |
1978 | ||
7314e9e7 | 1979 | chipnr = (int)(to >> chip->chip_shift); |
ace4dfee | 1980 | chip->select_chip(mtd, chipnr); |
1da177e4 | 1981 | |
7314e9e7 TG |
1982 | /* Shift to get page */ |
1983 | page = (int)(to >> chip->page_shift); | |
1984 | ||
1985 | /* | |
1986 | * Reset the chip. Some chips (like the Toshiba TC5832DC found in one | |
1987 | * of my DiskOnChip 2000 test units) will clear the whole data page too | |
1988 | * if we don't do this. I have no clue why, but I seem to have 'fixed' | |
1989 | * it in the doc2000 driver in August 1999. dwmw2. | |
1990 | */ | |
ace4dfee | 1991 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
1da177e4 LT |
1992 | |
1993 | /* Check, if it is write protected */ | |
1994 | if (nand_check_wp(mtd)) | |
8593fbc6 | 1995 | return -EROFS; |
61b03bd7 | 1996 | |
1da177e4 | 1997 | /* Invalidate the page cache, if we write to the cached page */ |
ace4dfee TG |
1998 | if (page == chip->pagebuf) |
1999 | chip->pagebuf = -1; | |
1da177e4 | 2000 | |
7bc3312b TG |
2001 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
2002 | nand_fill_oob(chip, ops->oobbuf, ops); | |
2003 | status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask); | |
2004 | memset(chip->oob_poi, 0xff, mtd->oobsize); | |
1da177e4 | 2005 | |
7bc3312b TG |
2006 | if (status) |
2007 | return status; | |
1da177e4 | 2008 | |
7014568b | 2009 | ops->oobretlen = ops->ooblen; |
1da177e4 | 2010 | |
7bc3312b | 2011 | return 0; |
8593fbc6 TG |
2012 | } |
2013 | ||
2014 | /** | |
2015 | * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band | |
2016 | * @mtd: MTD device structure | |
844d3b42 | 2017 | * @to: offset to write to |
8593fbc6 TG |
2018 | * @ops: oob operation description structure |
2019 | */ | |
2020 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, | |
2021 | struct mtd_oob_ops *ops) | |
2022 | { | |
8593fbc6 TG |
2023 | struct nand_chip *chip = mtd->priv; |
2024 | int ret = -ENOTSUPP; | |
2025 | ||
2026 | ops->retlen = 0; | |
2027 | ||
2028 | /* Do not allow writes past end of device */ | |
7014568b | 2029 | if (ops->datbuf && (to + ops->len) > mtd->size) { |
20d8e248 | 2030 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond " |
2031 | "end of device\n", __func__); | |
8593fbc6 TG |
2032 | return -EINVAL; |
2033 | } | |
2034 | ||
7bc3312b | 2035 | nand_get_device(chip, mtd, FL_WRITING); |
8593fbc6 TG |
2036 | |
2037 | switch(ops->mode) { | |
2038 | case MTD_OOB_PLACE: | |
2039 | case MTD_OOB_AUTO: | |
8593fbc6 | 2040 | case MTD_OOB_RAW: |
8593fbc6 TG |
2041 | break; |
2042 | ||
2043 | default: | |
2044 | goto out; | |
2045 | } | |
2046 | ||
2047 | if (!ops->datbuf) | |
2048 | ret = nand_do_write_oob(mtd, to, ops); | |
2049 | else | |
2050 | ret = nand_do_write_ops(mtd, to, ops); | |
2051 | ||
e0c7d767 | 2052 | out: |
1da177e4 | 2053 | nand_release_device(mtd); |
1da177e4 LT |
2054 | return ret; |
2055 | } | |
2056 | ||
1da177e4 LT |
2057 | /** |
2058 | * single_erease_cmd - [GENERIC] NAND standard block erase command function | |
2059 | * @mtd: MTD device structure | |
2060 | * @page: the page address of the block which will be erased | |
2061 | * | |
2062 | * Standard erase command for NAND chips | |
2063 | */ | |
e0c7d767 | 2064 | static void single_erase_cmd(struct mtd_info *mtd, int page) |
1da177e4 | 2065 | { |
ace4dfee | 2066 | struct nand_chip *chip = mtd->priv; |
1da177e4 | 2067 | /* Send commands to erase a block */ |
ace4dfee TG |
2068 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); |
2069 | chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); | |
1da177e4 LT |
2070 | } |
2071 | ||
2072 | /** | |
2073 | * multi_erease_cmd - [GENERIC] AND specific block erase command function | |
2074 | * @mtd: MTD device structure | |
2075 | * @page: the page address of the block which will be erased | |
2076 | * | |
2077 | * AND multi block erase command function | |
2078 | * Erase 4 consecutive blocks | |
2079 | */ | |
e0c7d767 | 2080 | static void multi_erase_cmd(struct mtd_info *mtd, int page) |
1da177e4 | 2081 | { |
ace4dfee | 2082 | struct nand_chip *chip = mtd->priv; |
1da177e4 | 2083 | /* Send commands to erase a block */ |
ace4dfee TG |
2084 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++); |
2085 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++); | |
2086 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++); | |
2087 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); | |
2088 | chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); | |
1da177e4 LT |
2089 | } |
2090 | ||
2091 | /** | |
2092 | * nand_erase - [MTD Interface] erase block(s) | |
2093 | * @mtd: MTD device structure | |
2094 | * @instr: erase instruction | |
2095 | * | |
2096 | * Erase one ore more blocks | |
2097 | */ | |
e0c7d767 | 2098 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) |
1da177e4 | 2099 | { |
e0c7d767 | 2100 | return nand_erase_nand(mtd, instr, 0); |
1da177e4 | 2101 | } |
61b03bd7 | 2102 | |
30f464b7 | 2103 | #define BBT_PAGE_MASK 0xffffff3f |
1da177e4 | 2104 | /** |
ace4dfee | 2105 | * nand_erase_nand - [Internal] erase block(s) |
1da177e4 LT |
2106 | * @mtd: MTD device structure |
2107 | * @instr: erase instruction | |
2108 | * @allowbbt: allow erasing the bbt area | |
2109 | * | |
2110 | * Erase one ore more blocks | |
2111 | */ | |
ace4dfee TG |
2112 | int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, |
2113 | int allowbbt) | |
1da177e4 | 2114 | { |
69423d99 | 2115 | int page, status, pages_per_block, ret, chipnr; |
ace4dfee | 2116 | struct nand_chip *chip = mtd->priv; |
69423d99 | 2117 | loff_t rewrite_bbt[NAND_MAX_CHIPS]={0}; |
ace4dfee | 2118 | unsigned int bbt_masked_page = 0xffffffff; |
69423d99 | 2119 | loff_t len; |
1da177e4 | 2120 | |
20d8e248 | 2121 | DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n", |
2122 | __func__, (unsigned long long)instr->addr, | |
2123 | (unsigned long long)instr->len); | |
1da177e4 LT |
2124 | |
2125 | /* Start address must align on block boundary */ | |
ace4dfee | 2126 | if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) { |
20d8e248 | 2127 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Unaligned address\n", __func__); |
1da177e4 LT |
2128 | return -EINVAL; |
2129 | } | |
2130 | ||
2131 | /* Length must align on block boundary */ | |
ace4dfee | 2132 | if (instr->len & ((1 << chip->phys_erase_shift) - 1)) { |
20d8e248 | 2133 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Length not block aligned\n", |
2134 | __func__); | |
1da177e4 LT |
2135 | return -EINVAL; |
2136 | } | |
2137 | ||
2138 | /* Do not allow erase past end of device */ | |
2139 | if ((instr->len + instr->addr) > mtd->size) { | |
20d8e248 | 2140 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Erase past end of device\n", |
2141 | __func__); | |
1da177e4 LT |
2142 | return -EINVAL; |
2143 | } | |
2144 | ||
bb0eb217 | 2145 | instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN; |
1da177e4 LT |
2146 | |
2147 | /* Grab the lock and see if the device is available */ | |
ace4dfee | 2148 | nand_get_device(chip, mtd, FL_ERASING); |
1da177e4 LT |
2149 | |
2150 | /* Shift to get first page */ | |
ace4dfee TG |
2151 | page = (int)(instr->addr >> chip->page_shift); |
2152 | chipnr = (int)(instr->addr >> chip->chip_shift); | |
1da177e4 LT |
2153 | |
2154 | /* Calculate pages in each block */ | |
ace4dfee | 2155 | pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); |
1da177e4 LT |
2156 | |
2157 | /* Select the NAND device */ | |
ace4dfee | 2158 | chip->select_chip(mtd, chipnr); |
1da177e4 | 2159 | |
1da177e4 LT |
2160 | /* Check, if it is write protected */ |
2161 | if (nand_check_wp(mtd)) { | |
20d8e248 | 2162 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n", |
2163 | __func__); | |
1da177e4 LT |
2164 | instr->state = MTD_ERASE_FAILED; |
2165 | goto erase_exit; | |
2166 | } | |
2167 | ||
ace4dfee TG |
2168 | /* |
2169 | * If BBT requires refresh, set the BBT page mask to see if the BBT | |
2170 | * should be rewritten. Otherwise the mask is set to 0xffffffff which | |
2171 | * can not be matched. This is also done when the bbt is actually | |
2172 | * erased to avoid recusrsive updates | |
2173 | */ | |
2174 | if (chip->options & BBT_AUTO_REFRESH && !allowbbt) | |
2175 | bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK; | |
30f464b7 | 2176 | |
1da177e4 LT |
2177 | /* Loop through the pages */ |
2178 | len = instr->len; | |
2179 | ||
2180 | instr->state = MTD_ERASING; | |
2181 | ||
2182 | while (len) { | |
ace4dfee TG |
2183 | /* |
2184 | * heck if we have a bad block, we do not erase bad blocks ! | |
2185 | */ | |
2186 | if (nand_block_checkbad(mtd, ((loff_t) page) << | |
2187 | chip->page_shift, 0, allowbbt)) { | |
20d8e248 | 2188 | printk(KERN_WARNING "%s: attempt to erase a bad block " |
2189 | "at page 0x%08x\n", __func__, page); | |
1da177e4 LT |
2190 | instr->state = MTD_ERASE_FAILED; |
2191 | goto erase_exit; | |
2192 | } | |
61b03bd7 | 2193 | |
ace4dfee TG |
2194 | /* |
2195 | * Invalidate the page cache, if we erase the block which | |
2196 | * contains the current cached page | |
2197 | */ | |
2198 | if (page <= chip->pagebuf && chip->pagebuf < | |
2199 | (page + pages_per_block)) | |
2200 | chip->pagebuf = -1; | |
1da177e4 | 2201 | |
ace4dfee | 2202 | chip->erase_cmd(mtd, page & chip->pagemask); |
61b03bd7 | 2203 | |
7bc3312b | 2204 | status = chip->waitfunc(mtd, chip); |
1da177e4 | 2205 | |
ace4dfee TG |
2206 | /* |
2207 | * See if operation failed and additional status checks are | |
2208 | * available | |
2209 | */ | |
2210 | if ((status & NAND_STATUS_FAIL) && (chip->errstat)) | |
2211 | status = chip->errstat(mtd, chip, FL_ERASING, | |
2212 | status, page); | |
068e3c0a | 2213 | |
1da177e4 | 2214 | /* See if block erase succeeded */ |
a4ab4c5d | 2215 | if (status & NAND_STATUS_FAIL) { |
20d8e248 | 2216 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Failed erase, " |
2217 | "page 0x%08x\n", __func__, page); | |
1da177e4 | 2218 | instr->state = MTD_ERASE_FAILED; |
69423d99 AH |
2219 | instr->fail_addr = |
2220 | ((loff_t)page << chip->page_shift); | |
1da177e4 LT |
2221 | goto erase_exit; |
2222 | } | |
30f464b7 | 2223 | |
ace4dfee TG |
2224 | /* |
2225 | * If BBT requires refresh, set the BBT rewrite flag to the | |
2226 | * page being erased | |
2227 | */ | |
2228 | if (bbt_masked_page != 0xffffffff && | |
2229 | (page & BBT_PAGE_MASK) == bbt_masked_page) | |
69423d99 AH |
2230 | rewrite_bbt[chipnr] = |
2231 | ((loff_t)page << chip->page_shift); | |
61b03bd7 | 2232 | |
1da177e4 | 2233 | /* Increment page address and decrement length */ |
ace4dfee | 2234 | len -= (1 << chip->phys_erase_shift); |
1da177e4 LT |
2235 | page += pages_per_block; |
2236 | ||
2237 | /* Check, if we cross a chip boundary */ | |
ace4dfee | 2238 | if (len && !(page & chip->pagemask)) { |
1da177e4 | 2239 | chipnr++; |
ace4dfee TG |
2240 | chip->select_chip(mtd, -1); |
2241 | chip->select_chip(mtd, chipnr); | |
30f464b7 | 2242 | |
ace4dfee TG |
2243 | /* |
2244 | * If BBT requires refresh and BBT-PERCHIP, set the BBT | |
2245 | * page mask to see if this BBT should be rewritten | |
2246 | */ | |
2247 | if (bbt_masked_page != 0xffffffff && | |
2248 | (chip->bbt_td->options & NAND_BBT_PERCHIP)) | |
2249 | bbt_masked_page = chip->bbt_td->pages[chipnr] & | |
2250 | BBT_PAGE_MASK; | |
1da177e4 LT |
2251 | } |
2252 | } | |
2253 | instr->state = MTD_ERASE_DONE; | |
2254 | ||
e0c7d767 | 2255 | erase_exit: |
1da177e4 LT |
2256 | |
2257 | ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO; | |
1da177e4 LT |
2258 | |
2259 | /* Deselect and wake up anyone waiting on the device */ | |
2260 | nand_release_device(mtd); | |
2261 | ||
49defc01 DW |
2262 | /* Do call back function */ |
2263 | if (!ret) | |
2264 | mtd_erase_callback(instr); | |
2265 | ||
ace4dfee TG |
2266 | /* |
2267 | * If BBT requires refresh and erase was successful, rewrite any | |
2268 | * selected bad block tables | |
2269 | */ | |
2270 | if (bbt_masked_page == 0xffffffff || ret) | |
2271 | return ret; | |
2272 | ||
2273 | for (chipnr = 0; chipnr < chip->numchips; chipnr++) { | |
2274 | if (!rewrite_bbt[chipnr]) | |
2275 | continue; | |
2276 | /* update the BBT for chip */ | |
20d8e248 | 2277 | DEBUG(MTD_DEBUG_LEVEL0, "%s: nand_update_bbt " |
2278 | "(%d:0x%0llx 0x%0x)\n", __func__, chipnr, | |
2279 | rewrite_bbt[chipnr], chip->bbt_td->pages[chipnr]); | |
ace4dfee | 2280 | nand_update_bbt(mtd, rewrite_bbt[chipnr]); |
30f464b7 DM |
2281 | } |
2282 | ||
1da177e4 LT |
2283 | /* Return more or less happy */ |
2284 | return ret; | |
2285 | } | |
2286 | ||
2287 | /** | |
2288 | * nand_sync - [MTD Interface] sync | |
2289 | * @mtd: MTD device structure | |
2290 | * | |
2291 | * Sync is actually a wait for chip ready function | |
2292 | */ | |
e0c7d767 | 2293 | static void nand_sync(struct mtd_info *mtd) |
1da177e4 | 2294 | { |
ace4dfee | 2295 | struct nand_chip *chip = mtd->priv; |
1da177e4 | 2296 | |
20d8e248 | 2297 | DEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__); |
1da177e4 LT |
2298 | |
2299 | /* Grab the lock and see if the device is available */ | |
ace4dfee | 2300 | nand_get_device(chip, mtd, FL_SYNCING); |
1da177e4 | 2301 | /* Release it and go back */ |
e0c7d767 | 2302 | nand_release_device(mtd); |
1da177e4 LT |
2303 | } |
2304 | ||
1da177e4 | 2305 | /** |
ace4dfee | 2306 | * nand_block_isbad - [MTD Interface] Check if block at offset is bad |
1da177e4 | 2307 | * @mtd: MTD device structure |
844d3b42 | 2308 | * @offs: offset relative to mtd start |
1da177e4 | 2309 | */ |
ace4dfee | 2310 | static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) |
1da177e4 LT |
2311 | { |
2312 | /* Check for invalid offset */ | |
ace4dfee | 2313 | if (offs > mtd->size) |
1da177e4 | 2314 | return -EINVAL; |
61b03bd7 | 2315 | |
ace4dfee | 2316 | return nand_block_checkbad(mtd, offs, 1, 0); |
1da177e4 LT |
2317 | } |
2318 | ||
2319 | /** | |
ace4dfee | 2320 | * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad |
1da177e4 LT |
2321 | * @mtd: MTD device structure |
2322 | * @ofs: offset relative to mtd start | |
2323 | */ | |
e0c7d767 | 2324 | static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
1da177e4 | 2325 | { |
ace4dfee | 2326 | struct nand_chip *chip = mtd->priv; |
1da177e4 LT |
2327 | int ret; |
2328 | ||
e0c7d767 DW |
2329 | if ((ret = nand_block_isbad(mtd, ofs))) { |
2330 | /* If it was bad already, return success and do nothing. */ | |
1da177e4 LT |
2331 | if (ret > 0) |
2332 | return 0; | |
e0c7d767 DW |
2333 | return ret; |
2334 | } | |
1da177e4 | 2335 | |
ace4dfee | 2336 | return chip->block_markbad(mtd, ofs); |
1da177e4 LT |
2337 | } |
2338 | ||
962034f4 VW |
2339 | /** |
2340 | * nand_suspend - [MTD Interface] Suspend the NAND flash | |
2341 | * @mtd: MTD device structure | |
2342 | */ | |
2343 | static int nand_suspend(struct mtd_info *mtd) | |
2344 | { | |
ace4dfee | 2345 | struct nand_chip *chip = mtd->priv; |
962034f4 | 2346 | |
ace4dfee | 2347 | return nand_get_device(chip, mtd, FL_PM_SUSPENDED); |
962034f4 VW |
2348 | } |
2349 | ||
2350 | /** | |
2351 | * nand_resume - [MTD Interface] Resume the NAND flash | |
2352 | * @mtd: MTD device structure | |
2353 | */ | |
2354 | static void nand_resume(struct mtd_info *mtd) | |
2355 | { | |
ace4dfee | 2356 | struct nand_chip *chip = mtd->priv; |
962034f4 | 2357 | |
ace4dfee | 2358 | if (chip->state == FL_PM_SUSPENDED) |
962034f4 VW |
2359 | nand_release_device(mtd); |
2360 | else | |
20d8e248 | 2361 | printk(KERN_ERR "%s called for a chip which is not " |
2362 | "in suspended state\n", __func__); | |
962034f4 VW |
2363 | } |
2364 | ||
7aa65bfd TG |
2365 | /* |
2366 | * Set default functions | |
2367 | */ | |
ace4dfee | 2368 | static void nand_set_defaults(struct nand_chip *chip, int busw) |
7aa65bfd | 2369 | { |
1da177e4 | 2370 | /* check for proper chip_delay setup, set 20us if not */ |
ace4dfee TG |
2371 | if (!chip->chip_delay) |
2372 | chip->chip_delay = 20; | |
1da177e4 LT |
2373 | |
2374 | /* check, if a user supplied command function given */ | |
ace4dfee TG |
2375 | if (chip->cmdfunc == NULL) |
2376 | chip->cmdfunc = nand_command; | |
1da177e4 LT |
2377 | |
2378 | /* check, if a user supplied wait function given */ | |
ace4dfee TG |
2379 | if (chip->waitfunc == NULL) |
2380 | chip->waitfunc = nand_wait; | |
2381 | ||
2382 | if (!chip->select_chip) | |
2383 | chip->select_chip = nand_select_chip; | |
2384 | if (!chip->read_byte) | |
2385 | chip->read_byte = busw ? nand_read_byte16 : nand_read_byte; | |
2386 | if (!chip->read_word) | |
2387 | chip->read_word = nand_read_word; | |
2388 | if (!chip->block_bad) | |
2389 | chip->block_bad = nand_block_bad; | |
2390 | if (!chip->block_markbad) | |
2391 | chip->block_markbad = nand_default_block_markbad; | |
2392 | if (!chip->write_buf) | |
2393 | chip->write_buf = busw ? nand_write_buf16 : nand_write_buf; | |
2394 | if (!chip->read_buf) | |
2395 | chip->read_buf = busw ? nand_read_buf16 : nand_read_buf; | |
2396 | if (!chip->verify_buf) | |
2397 | chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf; | |
2398 | if (!chip->scan_bbt) | |
2399 | chip->scan_bbt = nand_default_bbt; | |
f75e5097 TG |
2400 | |
2401 | if (!chip->controller) { | |
2402 | chip->controller = &chip->hwcontrol; | |
2403 | spin_lock_init(&chip->controller->lock); | |
2404 | init_waitqueue_head(&chip->controller->wq); | |
2405 | } | |
2406 | ||
7aa65bfd TG |
2407 | } |
2408 | ||
2409 | /* | |
ace4dfee | 2410 | * Get the flash and manufacturer id and lookup if the type is supported |
7aa65bfd TG |
2411 | */ |
2412 | static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd, | |
ace4dfee | 2413 | struct nand_chip *chip, |
7aa65bfd TG |
2414 | int busw, int *maf_id) |
2415 | { | |
2416 | struct nand_flash_dev *type = NULL; | |
2417 | int i, dev_id, maf_idx; | |
ed8165c7 | 2418 | int tmp_id, tmp_manf; |
1da177e4 LT |
2419 | |
2420 | /* Select the device */ | |
ace4dfee | 2421 | chip->select_chip(mtd, 0); |
1da177e4 | 2422 | |
ef89a880 KB |
2423 | /* |
2424 | * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx) | |
2425 | * after power-up | |
2426 | */ | |
2427 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); | |
2428 | ||
1da177e4 | 2429 | /* Send the command for reading device ID */ |
ace4dfee | 2430 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
1da177e4 LT |
2431 | |
2432 | /* Read manufacturer and device IDs */ | |
ace4dfee TG |
2433 | *maf_id = chip->read_byte(mtd); |
2434 | dev_id = chip->read_byte(mtd); | |
1da177e4 | 2435 | |
ed8165c7 BD |
2436 | /* Try again to make sure, as some systems the bus-hold or other |
2437 | * interface concerns can cause random data which looks like a | |
2438 | * possibly credible NAND flash to appear. If the two results do | |
2439 | * not match, ignore the device completely. | |
2440 | */ | |
2441 | ||
2442 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); | |
2443 | ||
2444 | /* Read manufacturer and device IDs */ | |
2445 | ||
2446 | tmp_manf = chip->read_byte(mtd); | |
2447 | tmp_id = chip->read_byte(mtd); | |
2448 | ||
2449 | if (tmp_manf != *maf_id || tmp_id != dev_id) { | |
2450 | printk(KERN_INFO "%s: second ID read did not match " | |
2451 | "%02x,%02x against %02x,%02x\n", __func__, | |
2452 | *maf_id, dev_id, tmp_manf, tmp_id); | |
2453 | return ERR_PTR(-ENODEV); | |
2454 | } | |
2455 | ||
7aa65bfd | 2456 | /* Lookup the flash id */ |
1da177e4 | 2457 | for (i = 0; nand_flash_ids[i].name != NULL; i++) { |
7aa65bfd TG |
2458 | if (dev_id == nand_flash_ids[i].id) { |
2459 | type = &nand_flash_ids[i]; | |
2460 | break; | |
2461 | } | |
2462 | } | |
61b03bd7 | 2463 | |
7aa65bfd TG |
2464 | if (!type) |
2465 | return ERR_PTR(-ENODEV); | |
2466 | ||
ba0251fe TG |
2467 | if (!mtd->name) |
2468 | mtd->name = type->name; | |
2469 | ||
69423d99 | 2470 | chip->chipsize = (uint64_t)type->chipsize << 20; |
7aa65bfd TG |
2471 | |
2472 | /* Newer devices have all the information in additional id bytes */ | |
ba0251fe | 2473 | if (!type->pagesize) { |
7aa65bfd | 2474 | int extid; |
29072b96 TG |
2475 | /* The 3rd id byte holds MLC / multichip data */ |
2476 | chip->cellinfo = chip->read_byte(mtd); | |
7aa65bfd | 2477 | /* The 4th id byte is the important one */ |
ace4dfee | 2478 | extid = chip->read_byte(mtd); |
7aa65bfd | 2479 | /* Calc pagesize */ |
4cbb9b80 | 2480 | mtd->writesize = 1024 << (extid & 0x3); |
7aa65bfd TG |
2481 | extid >>= 2; |
2482 | /* Calc oobsize */ | |
4cbb9b80 | 2483 | mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9); |
7aa65bfd TG |
2484 | extid >>= 2; |
2485 | /* Calc blocksize. Blocksize is multiples of 64KiB */ | |
2486 | mtd->erasesize = (64 * 1024) << (extid & 0x03); | |
2487 | extid >>= 2; | |
2488 | /* Get buswidth information */ | |
2489 | busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0; | |
61b03bd7 | 2490 | |
7aa65bfd TG |
2491 | } else { |
2492 | /* | |
ace4dfee | 2493 | * Old devices have chip data hardcoded in the device id table |
7aa65bfd | 2494 | */ |
ba0251fe TG |
2495 | mtd->erasesize = type->erasesize; |
2496 | mtd->writesize = type->pagesize; | |
4cbb9b80 | 2497 | mtd->oobsize = mtd->writesize / 32; |
ba0251fe | 2498 | busw = type->options & NAND_BUSWIDTH_16; |
7aa65bfd | 2499 | } |
1da177e4 | 2500 | |
7aa65bfd | 2501 | /* Try to identify manufacturer */ |
9a909867 | 2502 | for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) { |
7aa65bfd TG |
2503 | if (nand_manuf_ids[maf_idx].id == *maf_id) |
2504 | break; | |
2505 | } | |
0ea4a755 | 2506 | |
7aa65bfd TG |
2507 | /* |
2508 | * Check, if buswidth is correct. Hardware drivers should set | |
ace4dfee | 2509 | * chip correct ! |
7aa65bfd | 2510 | */ |
ace4dfee | 2511 | if (busw != (chip->options & NAND_BUSWIDTH_16)) { |
7aa65bfd TG |
2512 | printk(KERN_INFO "NAND device: Manufacturer ID:" |
2513 | " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, | |
2514 | dev_id, nand_manuf_ids[maf_idx].name, mtd->name); | |
2515 | printk(KERN_WARNING "NAND bus width %d instead %d bit\n", | |
ace4dfee | 2516 | (chip->options & NAND_BUSWIDTH_16) ? 16 : 8, |
7aa65bfd TG |
2517 | busw ? 16 : 8); |
2518 | return ERR_PTR(-EINVAL); | |
2519 | } | |
61b03bd7 | 2520 | |
7aa65bfd | 2521 | /* Calculate the address shift from the page size */ |
ace4dfee | 2522 | chip->page_shift = ffs(mtd->writesize) - 1; |
7aa65bfd | 2523 | /* Convert chipsize to number of pages per chip -1. */ |
ace4dfee | 2524 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; |
61b03bd7 | 2525 | |
ace4dfee | 2526 | chip->bbt_erase_shift = chip->phys_erase_shift = |
7aa65bfd | 2527 | ffs(mtd->erasesize) - 1; |
69423d99 AH |
2528 | if (chip->chipsize & 0xffffffff) |
2529 | chip->chip_shift = ffs((unsigned)chip->chipsize) - 1; | |
2530 | else | |
2531 | chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)) + 32 - 1; | |
1da177e4 | 2532 | |
7aa65bfd | 2533 | /* Set the bad block position */ |
ace4dfee | 2534 | chip->badblockpos = mtd->writesize > 512 ? |
7aa65bfd | 2535 | NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS; |
61b03bd7 | 2536 | |
7aa65bfd | 2537 | /* Get chip options, preserve non chip based options */ |
ace4dfee | 2538 | chip->options &= ~NAND_CHIPOPTIONS_MSK; |
ba0251fe | 2539 | chip->options |= type->options & NAND_CHIPOPTIONS_MSK; |
7aa65bfd TG |
2540 | |
2541 | /* | |
ace4dfee | 2542 | * Set chip as a default. Board drivers can override it, if necessary |
7aa65bfd | 2543 | */ |
ace4dfee | 2544 | chip->options |= NAND_NO_AUTOINCR; |
7aa65bfd | 2545 | |
ace4dfee | 2546 | /* Check if chip is a not a samsung device. Do not clear the |
7aa65bfd TG |
2547 | * options for chips which are not having an extended id. |
2548 | */ | |
ba0251fe | 2549 | if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize) |
ace4dfee | 2550 | chip->options &= ~NAND_SAMSUNG_LP_OPTIONS; |
7aa65bfd TG |
2551 | |
2552 | /* Check for AND chips with 4 page planes */ | |
ace4dfee TG |
2553 | if (chip->options & NAND_4PAGE_ARRAY) |
2554 | chip->erase_cmd = multi_erase_cmd; | |
7aa65bfd | 2555 | else |
ace4dfee | 2556 | chip->erase_cmd = single_erase_cmd; |
7aa65bfd TG |
2557 | |
2558 | /* Do not replace user supplied command function ! */ | |
ace4dfee TG |
2559 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
2560 | chip->cmdfunc = nand_command_lp; | |
7aa65bfd TG |
2561 | |
2562 | printk(KERN_INFO "NAND device: Manufacturer ID:" | |
2563 | " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id, | |
2564 | nand_manuf_ids[maf_idx].name, type->name); | |
2565 | ||
2566 | return type; | |
2567 | } | |
2568 | ||
7aa65bfd | 2569 | /** |
3b85c321 DW |
2570 | * nand_scan_ident - [NAND Interface] Scan for the NAND device |
2571 | * @mtd: MTD device structure | |
2572 | * @maxchips: Number of chips to scan for | |
7aa65bfd | 2573 | * |
3b85c321 DW |
2574 | * This is the first phase of the normal nand_scan() function. It |
2575 | * reads the flash ID and sets up MTD fields accordingly. | |
7aa65bfd | 2576 | * |
3b85c321 | 2577 | * The mtd->owner field must be set to the module of the caller. |
7aa65bfd | 2578 | */ |
3b85c321 | 2579 | int nand_scan_ident(struct mtd_info *mtd, int maxchips) |
7aa65bfd TG |
2580 | { |
2581 | int i, busw, nand_maf_id; | |
ace4dfee | 2582 | struct nand_chip *chip = mtd->priv; |
7aa65bfd TG |
2583 | struct nand_flash_dev *type; |
2584 | ||
7aa65bfd | 2585 | /* Get buswidth to select the correct functions */ |
ace4dfee | 2586 | busw = chip->options & NAND_BUSWIDTH_16; |
7aa65bfd | 2587 | /* Set the default functions */ |
ace4dfee | 2588 | nand_set_defaults(chip, busw); |
7aa65bfd TG |
2589 | |
2590 | /* Read the flash type */ | |
ace4dfee | 2591 | type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id); |
7aa65bfd TG |
2592 | |
2593 | if (IS_ERR(type)) { | |
e0c7d767 | 2594 | printk(KERN_WARNING "No NAND device found!!!\n"); |
ace4dfee | 2595 | chip->select_chip(mtd, -1); |
7aa65bfd | 2596 | return PTR_ERR(type); |
1da177e4 LT |
2597 | } |
2598 | ||
7aa65bfd | 2599 | /* Check for a chip array */ |
e0c7d767 | 2600 | for (i = 1; i < maxchips; i++) { |
ace4dfee | 2601 | chip->select_chip(mtd, i); |
ef89a880 KB |
2602 | /* See comment in nand_get_flash_type for reset */ |
2603 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); | |
1da177e4 | 2604 | /* Send the command for reading device ID */ |
ace4dfee | 2605 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
1da177e4 | 2606 | /* Read manufacturer and device IDs */ |
ace4dfee TG |
2607 | if (nand_maf_id != chip->read_byte(mtd) || |
2608 | type->id != chip->read_byte(mtd)) | |
1da177e4 LT |
2609 | break; |
2610 | } | |
2611 | if (i > 1) | |
2612 | printk(KERN_INFO "%d NAND chips detected\n", i); | |
61b03bd7 | 2613 | |
1da177e4 | 2614 | /* Store the number of chips and calc total size for mtd */ |
ace4dfee TG |
2615 | chip->numchips = i; |
2616 | mtd->size = i * chip->chipsize; | |
7aa65bfd | 2617 | |
3b85c321 DW |
2618 | return 0; |
2619 | } | |
2620 | ||
2621 | ||
2622 | /** | |
2623 | * nand_scan_tail - [NAND Interface] Scan for the NAND device | |
2624 | * @mtd: MTD device structure | |
3b85c321 DW |
2625 | * |
2626 | * This is the second phase of the normal nand_scan() function. It | |
2627 | * fills out all the uninitialized function pointers with the defaults | |
2628 | * and scans for a bad block table if appropriate. | |
2629 | */ | |
2630 | int nand_scan_tail(struct mtd_info *mtd) | |
2631 | { | |
2632 | int i; | |
2633 | struct nand_chip *chip = mtd->priv; | |
2634 | ||
4bf63fcb DW |
2635 | if (!(chip->options & NAND_OWN_BUFFERS)) |
2636 | chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL); | |
2637 | if (!chip->buffers) | |
2638 | return -ENOMEM; | |
2639 | ||
7dcdcbef | 2640 | /* Set the internal oob buffer location, just after the page data */ |
784f4d5e | 2641 | chip->oob_poi = chip->buffers->databuf + mtd->writesize; |
1da177e4 | 2642 | |
7aa65bfd TG |
2643 | /* |
2644 | * If no default placement scheme is given, select an appropriate one | |
2645 | */ | |
5bd34c09 | 2646 | if (!chip->ecc.layout) { |
61b03bd7 | 2647 | switch (mtd->oobsize) { |
1da177e4 | 2648 | case 8: |
5bd34c09 | 2649 | chip->ecc.layout = &nand_oob_8; |
1da177e4 LT |
2650 | break; |
2651 | case 16: | |
5bd34c09 | 2652 | chip->ecc.layout = &nand_oob_16; |
1da177e4 LT |
2653 | break; |
2654 | case 64: | |
5bd34c09 | 2655 | chip->ecc.layout = &nand_oob_64; |
1da177e4 | 2656 | break; |
81ec5364 TG |
2657 | case 128: |
2658 | chip->ecc.layout = &nand_oob_128; | |
2659 | break; | |
1da177e4 | 2660 | default: |
7aa65bfd TG |
2661 | printk(KERN_WARNING "No oob scheme defined for " |
2662 | "oobsize %d\n", mtd->oobsize); | |
1da177e4 LT |
2663 | BUG(); |
2664 | } | |
2665 | } | |
61b03bd7 | 2666 | |
956e944c DW |
2667 | if (!chip->write_page) |
2668 | chip->write_page = nand_write_page; | |
2669 | ||
61b03bd7 | 2670 | /* |
7aa65bfd TG |
2671 | * check ECC mode, default to software if 3byte/512byte hardware ECC is |
2672 | * selected and we have 256 byte pagesize fallback to software ECC | |
e0c7d767 | 2673 | */ |
956e944c | 2674 | |
ace4dfee | 2675 | switch (chip->ecc.mode) { |
6dfc6d25 | 2676 | case NAND_ECC_HW: |
f5bbdacc TG |
2677 | /* Use standard hwecc read page function ? */ |
2678 | if (!chip->ecc.read_page) | |
2679 | chip->ecc.read_page = nand_read_page_hwecc; | |
f75e5097 TG |
2680 | if (!chip->ecc.write_page) |
2681 | chip->ecc.write_page = nand_write_page_hwecc; | |
52ff49df DB |
2682 | if (!chip->ecc.read_page_raw) |
2683 | chip->ecc.read_page_raw = nand_read_page_raw; | |
2684 | if (!chip->ecc.write_page_raw) | |
2685 | chip->ecc.write_page_raw = nand_write_page_raw; | |
7bc3312b TG |
2686 | if (!chip->ecc.read_oob) |
2687 | chip->ecc.read_oob = nand_read_oob_std; | |
2688 | if (!chip->ecc.write_oob) | |
2689 | chip->ecc.write_oob = nand_write_oob_std; | |
f5bbdacc | 2690 | |
6dfc6d25 | 2691 | case NAND_ECC_HW_SYNDROME: |
78b65179 SW |
2692 | if ((!chip->ecc.calculate || !chip->ecc.correct || |
2693 | !chip->ecc.hwctl) && | |
2694 | (!chip->ecc.read_page || | |
1c45f604 | 2695 | chip->ecc.read_page == nand_read_page_hwecc || |
78b65179 | 2696 | !chip->ecc.write_page || |
1c45f604 | 2697 | chip->ecc.write_page == nand_write_page_hwecc)) { |
6dfc6d25 TG |
2698 | printk(KERN_WARNING "No ECC functions supplied, " |
2699 | "Hardware ECC not possible\n"); | |
2700 | BUG(); | |
2701 | } | |
f75e5097 | 2702 | /* Use standard syndrome read/write page function ? */ |
f5bbdacc TG |
2703 | if (!chip->ecc.read_page) |
2704 | chip->ecc.read_page = nand_read_page_syndrome; | |
f75e5097 TG |
2705 | if (!chip->ecc.write_page) |
2706 | chip->ecc.write_page = nand_write_page_syndrome; | |
52ff49df DB |
2707 | if (!chip->ecc.read_page_raw) |
2708 | chip->ecc.read_page_raw = nand_read_page_raw_syndrome; | |
2709 | if (!chip->ecc.write_page_raw) | |
2710 | chip->ecc.write_page_raw = nand_write_page_raw_syndrome; | |
7bc3312b TG |
2711 | if (!chip->ecc.read_oob) |
2712 | chip->ecc.read_oob = nand_read_oob_syndrome; | |
2713 | if (!chip->ecc.write_oob) | |
2714 | chip->ecc.write_oob = nand_write_oob_syndrome; | |
f5bbdacc | 2715 | |
ace4dfee | 2716 | if (mtd->writesize >= chip->ecc.size) |
6dfc6d25 TG |
2717 | break; |
2718 | printk(KERN_WARNING "%d byte HW ECC not possible on " | |
2719 | "%d byte page size, fallback to SW ECC\n", | |
ace4dfee TG |
2720 | chip->ecc.size, mtd->writesize); |
2721 | chip->ecc.mode = NAND_ECC_SOFT; | |
61b03bd7 | 2722 | |
6dfc6d25 | 2723 | case NAND_ECC_SOFT: |
ace4dfee TG |
2724 | chip->ecc.calculate = nand_calculate_ecc; |
2725 | chip->ecc.correct = nand_correct_data; | |
f5bbdacc | 2726 | chip->ecc.read_page = nand_read_page_swecc; |
3d459559 | 2727 | chip->ecc.read_subpage = nand_read_subpage; |
f75e5097 | 2728 | chip->ecc.write_page = nand_write_page_swecc; |
52ff49df DB |
2729 | chip->ecc.read_page_raw = nand_read_page_raw; |
2730 | chip->ecc.write_page_raw = nand_write_page_raw; | |
7bc3312b TG |
2731 | chip->ecc.read_oob = nand_read_oob_std; |
2732 | chip->ecc.write_oob = nand_write_oob_std; | |
9a73290d SV |
2733 | if (!chip->ecc.size) |
2734 | chip->ecc.size = 256; | |
ace4dfee | 2735 | chip->ecc.bytes = 3; |
1da177e4 | 2736 | break; |
61b03bd7 TG |
2737 | |
2738 | case NAND_ECC_NONE: | |
7aa65bfd TG |
2739 | printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. " |
2740 | "This is not recommended !!\n"); | |
8593fbc6 TG |
2741 | chip->ecc.read_page = nand_read_page_raw; |
2742 | chip->ecc.write_page = nand_write_page_raw; | |
7bc3312b | 2743 | chip->ecc.read_oob = nand_read_oob_std; |
52ff49df DB |
2744 | chip->ecc.read_page_raw = nand_read_page_raw; |
2745 | chip->ecc.write_page_raw = nand_write_page_raw; | |
7bc3312b | 2746 | chip->ecc.write_oob = nand_write_oob_std; |
ace4dfee TG |
2747 | chip->ecc.size = mtd->writesize; |
2748 | chip->ecc.bytes = 0; | |
1da177e4 | 2749 | break; |
956e944c | 2750 | |
1da177e4 | 2751 | default: |
7aa65bfd | 2752 | printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n", |
ace4dfee | 2753 | chip->ecc.mode); |
61b03bd7 | 2754 | BUG(); |
1da177e4 | 2755 | } |
61b03bd7 | 2756 | |
5bd34c09 TG |
2757 | /* |
2758 | * The number of bytes available for a client to place data into | |
2759 | * the out of band area | |
2760 | */ | |
2761 | chip->ecc.layout->oobavail = 0; | |
81d19b04 DB |
2762 | for (i = 0; chip->ecc.layout->oobfree[i].length |
2763 | && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++) | |
5bd34c09 TG |
2764 | chip->ecc.layout->oobavail += |
2765 | chip->ecc.layout->oobfree[i].length; | |
1f92267c | 2766 | mtd->oobavail = chip->ecc.layout->oobavail; |
5bd34c09 | 2767 | |
7aa65bfd TG |
2768 | /* |
2769 | * Set the number of read / write steps for one page depending on ECC | |
2770 | * mode | |
2771 | */ | |
ace4dfee TG |
2772 | chip->ecc.steps = mtd->writesize / chip->ecc.size; |
2773 | if(chip->ecc.steps * chip->ecc.size != mtd->writesize) { | |
6dfc6d25 TG |
2774 | printk(KERN_WARNING "Invalid ecc parameters\n"); |
2775 | BUG(); | |
1da177e4 | 2776 | } |
f5bbdacc | 2777 | chip->ecc.total = chip->ecc.steps * chip->ecc.bytes; |
61b03bd7 | 2778 | |
29072b96 TG |
2779 | /* |
2780 | * Allow subpage writes up to ecc.steps. Not possible for MLC | |
2781 | * FLASH. | |
2782 | */ | |
2783 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && | |
2784 | !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) { | |
2785 | switch(chip->ecc.steps) { | |
2786 | case 2: | |
2787 | mtd->subpage_sft = 1; | |
2788 | break; | |
2789 | case 4: | |
2790 | case 8: | |
81ec5364 | 2791 | case 16: |
29072b96 TG |
2792 | mtd->subpage_sft = 2; |
2793 | break; | |
2794 | } | |
2795 | } | |
2796 | chip->subpagesize = mtd->writesize >> mtd->subpage_sft; | |
2797 | ||
04bbd0ea | 2798 | /* Initialize state */ |
ace4dfee | 2799 | chip->state = FL_READY; |
1da177e4 LT |
2800 | |
2801 | /* De-select the device */ | |
ace4dfee | 2802 | chip->select_chip(mtd, -1); |
1da177e4 LT |
2803 | |
2804 | /* Invalidate the pagebuffer reference */ | |
ace4dfee | 2805 | chip->pagebuf = -1; |
1da177e4 LT |
2806 | |
2807 | /* Fill in remaining MTD driver data */ | |
2808 | mtd->type = MTD_NANDFLASH; | |
5fa43394 | 2809 | mtd->flags = MTD_CAP_NANDFLASH; |
1da177e4 LT |
2810 | mtd->erase = nand_erase; |
2811 | mtd->point = NULL; | |
2812 | mtd->unpoint = NULL; | |
2813 | mtd->read = nand_read; | |
2814 | mtd->write = nand_write; | |
1da177e4 LT |
2815 | mtd->read_oob = nand_read_oob; |
2816 | mtd->write_oob = nand_write_oob; | |
1da177e4 LT |
2817 | mtd->sync = nand_sync; |
2818 | mtd->lock = NULL; | |
2819 | mtd->unlock = NULL; | |
962034f4 VW |
2820 | mtd->suspend = nand_suspend; |
2821 | mtd->resume = nand_resume; | |
1da177e4 LT |
2822 | mtd->block_isbad = nand_block_isbad; |
2823 | mtd->block_markbad = nand_block_markbad; | |
2824 | ||
5bd34c09 TG |
2825 | /* propagate ecc.layout to mtd_info */ |
2826 | mtd->ecclayout = chip->ecc.layout; | |
1da177e4 | 2827 | |
0040bf38 | 2828 | /* Check, if we should skip the bad block table scan */ |
ace4dfee | 2829 | if (chip->options & NAND_SKIP_BBTSCAN) |
0040bf38 | 2830 | return 0; |
1da177e4 LT |
2831 | |
2832 | /* Build bad block table */ | |
ace4dfee | 2833 | return chip->scan_bbt(mtd); |
1da177e4 LT |
2834 | } |
2835 | ||
a6e6abd5 | 2836 | /* is_module_text_address() isn't exported, and it's mostly a pointless |
3b85c321 DW |
2837 | test if this is a module _anyway_ -- they'd have to try _really_ hard |
2838 | to call us from in-kernel code if the core NAND support is modular. */ | |
2839 | #ifdef MODULE | |
2840 | #define caller_is_module() (1) | |
2841 | #else | |
2842 | #define caller_is_module() \ | |
a6e6abd5 | 2843 | is_module_text_address((unsigned long)__builtin_return_address(0)) |
3b85c321 DW |
2844 | #endif |
2845 | ||
2846 | /** | |
2847 | * nand_scan - [NAND Interface] Scan for the NAND device | |
2848 | * @mtd: MTD device structure | |
2849 | * @maxchips: Number of chips to scan for | |
2850 | * | |
2851 | * This fills out all the uninitialized function pointers | |
2852 | * with the defaults. | |
2853 | * The flash ID is read and the mtd/chip structures are | |
2854 | * filled with the appropriate values. | |
2855 | * The mtd->owner field must be set to the module of the caller | |
2856 | * | |
2857 | */ | |
2858 | int nand_scan(struct mtd_info *mtd, int maxchips) | |
2859 | { | |
2860 | int ret; | |
2861 | ||
2862 | /* Many callers got this wrong, so check for it for a while... */ | |
2863 | if (!mtd->owner && caller_is_module()) { | |
20d8e248 | 2864 | printk(KERN_CRIT "%s called with NULL mtd->owner!\n", |
2865 | __func__); | |
3b85c321 DW |
2866 | BUG(); |
2867 | } | |
2868 | ||
2869 | ret = nand_scan_ident(mtd, maxchips); | |
2870 | if (!ret) | |
2871 | ret = nand_scan_tail(mtd); | |
2872 | return ret; | |
2873 | } | |
2874 | ||
1da177e4 | 2875 | /** |
61b03bd7 | 2876 | * nand_release - [NAND Interface] Free resources held by the NAND device |
1da177e4 LT |
2877 | * @mtd: MTD device structure |
2878 | */ | |
e0c7d767 | 2879 | void nand_release(struct mtd_info *mtd) |
1da177e4 | 2880 | { |
ace4dfee | 2881 | struct nand_chip *chip = mtd->priv; |
1da177e4 LT |
2882 | |
2883 | #ifdef CONFIG_MTD_PARTITIONS | |
2884 | /* Deregister partitions */ | |
e0c7d767 | 2885 | del_mtd_partitions(mtd); |
1da177e4 LT |
2886 | #endif |
2887 | /* Deregister the device */ | |
e0c7d767 | 2888 | del_mtd_device(mtd); |
1da177e4 | 2889 | |
fa671646 | 2890 | /* Free bad block table memory */ |
ace4dfee | 2891 | kfree(chip->bbt); |
4bf63fcb DW |
2892 | if (!(chip->options & NAND_OWN_BUFFERS)) |
2893 | kfree(chip->buffers); | |
1da177e4 LT |
2894 | } |
2895 | ||
e0c7d767 | 2896 | EXPORT_SYMBOL_GPL(nand_scan); |
3b85c321 DW |
2897 | EXPORT_SYMBOL_GPL(nand_scan_ident); |
2898 | EXPORT_SYMBOL_GPL(nand_scan_tail); | |
e0c7d767 | 2899 | EXPORT_SYMBOL_GPL(nand_release); |
8fe833c1 RP |
2900 | |
2901 | static int __init nand_base_init(void) | |
2902 | { | |
2903 | led_trigger_register_simple("nand-disk", &nand_led_trigger); | |
2904 | return 0; | |
2905 | } | |
2906 | ||
2907 | static void __exit nand_base_exit(void) | |
2908 | { | |
2909 | led_trigger_unregister_simple(nand_led_trigger); | |
2910 | } | |
2911 | ||
2912 | module_init(nand_base_init); | |
2913 | module_exit(nand_base_exit); | |
2914 | ||
e0c7d767 DW |
2915 | MODULE_LICENSE("GPL"); |
2916 | MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>"); | |
2917 | MODULE_DESCRIPTION("Generic NAND flash driver code"); |