]>
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 | |
7854d3f7 | 24 | * if we have HW ECC support. |
1da177e4 LT |
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> | |
193bd400 | 45 | #include <linux/mtd/nand_bch.h> |
1da177e4 LT |
46 | #include <linux/interrupt.h> |
47 | #include <linux/bitops.h> | |
8fe833c1 | 48 | #include <linux/leds.h> |
7351d3a5 | 49 | #include <linux/io.h> |
1da177e4 | 50 | #include <linux/mtd/partitions.h> |
1da177e4 LT |
51 | |
52 | /* Define default oob placement schemes for large and small page devices */ | |
5bd34c09 | 53 | static struct nand_ecclayout nand_oob_8 = { |
1da177e4 LT |
54 | .eccbytes = 3, |
55 | .eccpos = {0, 1, 2}, | |
5bd34c09 TG |
56 | .oobfree = { |
57 | {.offset = 3, | |
58 | .length = 2}, | |
59 | {.offset = 6, | |
f8ac0414 | 60 | .length = 2} } |
1da177e4 LT |
61 | }; |
62 | ||
5bd34c09 | 63 | static struct nand_ecclayout nand_oob_16 = { |
1da177e4 LT |
64 | .eccbytes = 6, |
65 | .eccpos = {0, 1, 2, 3, 6, 7}, | |
5bd34c09 TG |
66 | .oobfree = { |
67 | {.offset = 8, | |
f8ac0414 | 68 | . length = 8} } |
1da177e4 LT |
69 | }; |
70 | ||
5bd34c09 | 71 | static struct nand_ecclayout nand_oob_64 = { |
1da177e4 LT |
72 | .eccbytes = 24, |
73 | .eccpos = { | |
e0c7d767 DW |
74 | 40, 41, 42, 43, 44, 45, 46, 47, |
75 | 48, 49, 50, 51, 52, 53, 54, 55, | |
76 | 56, 57, 58, 59, 60, 61, 62, 63}, | |
5bd34c09 TG |
77 | .oobfree = { |
78 | {.offset = 2, | |
f8ac0414 | 79 | .length = 38} } |
1da177e4 LT |
80 | }; |
81 | ||
81ec5364 TG |
82 | static struct nand_ecclayout nand_oob_128 = { |
83 | .eccbytes = 48, | |
84 | .eccpos = { | |
85 | 80, 81, 82, 83, 84, 85, 86, 87, | |
86 | 88, 89, 90, 91, 92, 93, 94, 95, | |
87 | 96, 97, 98, 99, 100, 101, 102, 103, | |
88 | 104, 105, 106, 107, 108, 109, 110, 111, | |
89 | 112, 113, 114, 115, 116, 117, 118, 119, | |
90 | 120, 121, 122, 123, 124, 125, 126, 127}, | |
91 | .oobfree = { | |
92 | {.offset = 2, | |
f8ac0414 | 93 | .length = 78} } |
81ec5364 TG |
94 | }; |
95 | ||
ace4dfee | 96 | static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, |
2c0a2bed | 97 | int new_state); |
1da177e4 | 98 | |
8593fbc6 TG |
99 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
100 | struct mtd_oob_ops *ops); | |
101 | ||
d470a97c | 102 | /* |
8e87d782 | 103 | * For devices which display every fart in the system on a separate LED. Is |
d470a97c TG |
104 | * compiled away when LED support is disabled. |
105 | */ | |
106 | DEFINE_LED_TRIGGER(nand_led_trigger); | |
107 | ||
6fe5a6ac VS |
108 | static int check_offs_len(struct mtd_info *mtd, |
109 | loff_t ofs, uint64_t len) | |
110 | { | |
111 | struct nand_chip *chip = mtd->priv; | |
112 | int ret = 0; | |
113 | ||
114 | /* Start address must align on block boundary */ | |
115 | if (ofs & ((1 << chip->phys_erase_shift) - 1)) { | |
289c0522 | 116 | pr_debug("%s: unaligned address\n", __func__); |
6fe5a6ac VS |
117 | ret = -EINVAL; |
118 | } | |
119 | ||
120 | /* Length must align on block boundary */ | |
121 | if (len & ((1 << chip->phys_erase_shift) - 1)) { | |
289c0522 | 122 | pr_debug("%s: length not block aligned\n", __func__); |
6fe5a6ac VS |
123 | ret = -EINVAL; |
124 | } | |
125 | ||
6fe5a6ac VS |
126 | return ret; |
127 | } | |
128 | ||
1da177e4 LT |
129 | /** |
130 | * nand_release_device - [GENERIC] release chip | |
8b6e50c9 | 131 | * @mtd: MTD device structure |
61b03bd7 | 132 | * |
8b6e50c9 | 133 | * Deselect, release chip lock and wake up anyone waiting on the device. |
1da177e4 | 134 | */ |
e0c7d767 | 135 | static void nand_release_device(struct mtd_info *mtd) |
1da177e4 | 136 | { |
ace4dfee | 137 | struct nand_chip *chip = mtd->priv; |
1da177e4 LT |
138 | |
139 | /* De-select the NAND device */ | |
ace4dfee | 140 | chip->select_chip(mtd, -1); |
0dfc6246 | 141 | |
a36ed299 | 142 | /* Release the controller and the chip */ |
ace4dfee TG |
143 | spin_lock(&chip->controller->lock); |
144 | chip->controller->active = NULL; | |
145 | chip->state = FL_READY; | |
146 | wake_up(&chip->controller->wq); | |
147 | spin_unlock(&chip->controller->lock); | |
1da177e4 LT |
148 | } |
149 | ||
150 | /** | |
151 | * nand_read_byte - [DEFAULT] read one byte from the chip | |
8b6e50c9 | 152 | * @mtd: MTD device structure |
1da177e4 | 153 | * |
7854d3f7 | 154 | * Default read function for 8bit buswidth |
1da177e4 | 155 | */ |
58dd8f2b | 156 | static uint8_t nand_read_byte(struct mtd_info *mtd) |
1da177e4 | 157 | { |
ace4dfee TG |
158 | struct nand_chip *chip = mtd->priv; |
159 | return readb(chip->IO_ADDR_R); | |
1da177e4 LT |
160 | } |
161 | ||
1da177e4 LT |
162 | /** |
163 | * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip | |
7854d3f7 | 164 | * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip |
8b6e50c9 | 165 | * @mtd: MTD device structure |
1da177e4 | 166 | * |
7854d3f7 BN |
167 | * Default read function for 16bit buswidth with endianness conversion. |
168 | * | |
1da177e4 | 169 | */ |
58dd8f2b | 170 | static uint8_t nand_read_byte16(struct mtd_info *mtd) |
1da177e4 | 171 | { |
ace4dfee TG |
172 | struct nand_chip *chip = mtd->priv; |
173 | return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R)); | |
1da177e4 LT |
174 | } |
175 | ||
1da177e4 LT |
176 | /** |
177 | * nand_read_word - [DEFAULT] read one word from the chip | |
8b6e50c9 | 178 | * @mtd: MTD device structure |
1da177e4 | 179 | * |
7854d3f7 | 180 | * Default read function for 16bit buswidth without endianness conversion. |
1da177e4 LT |
181 | */ |
182 | static u16 nand_read_word(struct mtd_info *mtd) | |
183 | { | |
ace4dfee TG |
184 | struct nand_chip *chip = mtd->priv; |
185 | return readw(chip->IO_ADDR_R); | |
1da177e4 LT |
186 | } |
187 | ||
1da177e4 LT |
188 | /** |
189 | * nand_select_chip - [DEFAULT] control CE line | |
8b6e50c9 BN |
190 | * @mtd: MTD device structure |
191 | * @chipnr: chipnumber to select, -1 for deselect | |
1da177e4 LT |
192 | * |
193 | * Default select function for 1 chip devices. | |
194 | */ | |
ace4dfee | 195 | static void nand_select_chip(struct mtd_info *mtd, int chipnr) |
1da177e4 | 196 | { |
ace4dfee TG |
197 | struct nand_chip *chip = mtd->priv; |
198 | ||
199 | switch (chipnr) { | |
1da177e4 | 200 | case -1: |
ace4dfee | 201 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE); |
1da177e4 LT |
202 | break; |
203 | case 0: | |
1da177e4 LT |
204 | break; |
205 | ||
206 | default: | |
207 | BUG(); | |
208 | } | |
209 | } | |
210 | ||
211 | /** | |
212 | * nand_write_buf - [DEFAULT] write buffer to chip | |
8b6e50c9 BN |
213 | * @mtd: MTD device structure |
214 | * @buf: data buffer | |
215 | * @len: number of bytes to write | |
1da177e4 | 216 | * |
7854d3f7 | 217 | * Default write function for 8bit buswidth. |
1da177e4 | 218 | */ |
58dd8f2b | 219 | static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
1da177e4 LT |
220 | { |
221 | int i; | |
ace4dfee | 222 | struct nand_chip *chip = mtd->priv; |
1da177e4 | 223 | |
e0c7d767 | 224 | for (i = 0; i < len; i++) |
ace4dfee | 225 | writeb(buf[i], chip->IO_ADDR_W); |
1da177e4 LT |
226 | } |
227 | ||
228 | /** | |
61b03bd7 | 229 | * nand_read_buf - [DEFAULT] read chip data into buffer |
8b6e50c9 BN |
230 | * @mtd: MTD device structure |
231 | * @buf: buffer to store date | |
232 | * @len: number of bytes to read | |
1da177e4 | 233 | * |
7854d3f7 | 234 | * Default read function for 8bit buswidth. |
1da177e4 | 235 | */ |
58dd8f2b | 236 | static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
1da177e4 LT |
237 | { |
238 | int i; | |
ace4dfee | 239 | struct nand_chip *chip = mtd->priv; |
1da177e4 | 240 | |
e0c7d767 | 241 | for (i = 0; i < len; i++) |
ace4dfee | 242 | buf[i] = readb(chip->IO_ADDR_R); |
1da177e4 LT |
243 | } |
244 | ||
1da177e4 LT |
245 | /** |
246 | * nand_write_buf16 - [DEFAULT] write buffer to chip | |
8b6e50c9 BN |
247 | * @mtd: MTD device structure |
248 | * @buf: data buffer | |
249 | * @len: number of bytes to write | |
1da177e4 | 250 | * |
7854d3f7 | 251 | * Default write function for 16bit buswidth. |
1da177e4 | 252 | */ |
58dd8f2b | 253 | static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len) |
1da177e4 LT |
254 | { |
255 | int i; | |
ace4dfee | 256 | struct nand_chip *chip = mtd->priv; |
1da177e4 LT |
257 | u16 *p = (u16 *) buf; |
258 | len >>= 1; | |
61b03bd7 | 259 | |
e0c7d767 | 260 | for (i = 0; i < len; i++) |
ace4dfee | 261 | writew(p[i], chip->IO_ADDR_W); |
61b03bd7 | 262 | |
1da177e4 LT |
263 | } |
264 | ||
265 | /** | |
61b03bd7 | 266 | * nand_read_buf16 - [DEFAULT] read chip data into buffer |
8b6e50c9 BN |
267 | * @mtd: MTD device structure |
268 | * @buf: buffer to store date | |
269 | * @len: number of bytes to read | |
1da177e4 | 270 | * |
7854d3f7 | 271 | * Default read function for 16bit buswidth. |
1da177e4 | 272 | */ |
58dd8f2b | 273 | static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len) |
1da177e4 LT |
274 | { |
275 | int i; | |
ace4dfee | 276 | struct nand_chip *chip = mtd->priv; |
1da177e4 LT |
277 | u16 *p = (u16 *) buf; |
278 | len >>= 1; | |
279 | ||
e0c7d767 | 280 | for (i = 0; i < len; i++) |
ace4dfee | 281 | p[i] = readw(chip->IO_ADDR_R); |
1da177e4 LT |
282 | } |
283 | ||
1da177e4 LT |
284 | /** |
285 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip | |
8b6e50c9 BN |
286 | * @mtd: MTD device structure |
287 | * @ofs: offset from device start | |
288 | * @getchip: 0, if the chip is already selected | |
1da177e4 | 289 | * |
61b03bd7 | 290 | * Check, if the block is bad. |
1da177e4 LT |
291 | */ |
292 | static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip) | |
293 | { | |
cdbec050 | 294 | int page, chipnr, res = 0, i = 0; |
ace4dfee | 295 | struct nand_chip *chip = mtd->priv; |
1da177e4 LT |
296 | u16 bad; |
297 | ||
5fb1549d | 298 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
b60b08b0 KC |
299 | ofs += mtd->erasesize - mtd->writesize; |
300 | ||
1a12f46a TK |
301 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
302 | ||
1da177e4 | 303 | if (getchip) { |
ace4dfee | 304 | chipnr = (int)(ofs >> chip->chip_shift); |
1da177e4 | 305 | |
ace4dfee | 306 | nand_get_device(chip, mtd, FL_READING); |
1da177e4 LT |
307 | |
308 | /* Select the NAND device */ | |
ace4dfee | 309 | chip->select_chip(mtd, chipnr); |
1a12f46a | 310 | } |
1da177e4 | 311 | |
cdbec050 BN |
312 | do { |
313 | if (chip->options & NAND_BUSWIDTH_16) { | |
314 | chip->cmdfunc(mtd, NAND_CMD_READOOB, | |
315 | chip->badblockpos & 0xFE, page); | |
316 | bad = cpu_to_le16(chip->read_word(mtd)); | |
317 | if (chip->badblockpos & 0x1) | |
318 | bad >>= 8; | |
319 | else | |
320 | bad &= 0xFF; | |
321 | } else { | |
322 | chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, | |
323 | page); | |
324 | bad = chip->read_byte(mtd); | |
325 | } | |
326 | ||
327 | if (likely(chip->badblockbits == 8)) | |
328 | res = bad != 0xFF; | |
e0b58d0a | 329 | else |
cdbec050 BN |
330 | res = hweight8(bad) < chip->badblockbits; |
331 | ofs += mtd->writesize; | |
332 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; | |
333 | i++; | |
334 | } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE)); | |
e0b58d0a | 335 | |
ace4dfee | 336 | if (getchip) |
1da177e4 | 337 | nand_release_device(mtd); |
61b03bd7 | 338 | |
1da177e4 LT |
339 | return res; |
340 | } | |
341 | ||
342 | /** | |
343 | * nand_default_block_markbad - [DEFAULT] mark a block bad | |
8b6e50c9 BN |
344 | * @mtd: MTD device structure |
345 | * @ofs: offset from device start | |
1da177e4 | 346 | * |
8b6e50c9 | 347 | * This is the default implementation, which can be overridden by a hardware |
e2414f4c BN |
348 | * specific driver. We try operations in the following order, according to our |
349 | * bbt_options (NAND_BBT_NO_OOB_BBM and NAND_BBT_USE_FLASH): | |
350 | * (1) erase the affected block, to allow OOB marker to be written cleanly | |
351 | * (2) update in-memory BBT | |
352 | * (3) write bad block marker to OOB area of affected block | |
353 | * (4) update flash-based BBT | |
354 | * Note that we retain the first error encountered in (3) or (4), finish the | |
355 | * procedures, and dump the error in the end. | |
1da177e4 LT |
356 | */ |
357 | static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) | |
358 | { | |
ace4dfee | 359 | struct nand_chip *chip = mtd->priv; |
58dd8f2b | 360 | uint8_t buf[2] = { 0, 0 }; |
e2414f4c BN |
361 | int block, res, ret = 0, i = 0; |
362 | int write_oob = !(chip->bbt_options & NAND_BBT_NO_OOB_BBM); | |
61b03bd7 | 363 | |
e2414f4c | 364 | if (write_oob) { |
00918429 BN |
365 | struct erase_info einfo; |
366 | ||
367 | /* Attempt erase before marking OOB */ | |
368 | memset(&einfo, 0, sizeof(einfo)); | |
369 | einfo.mtd = mtd; | |
370 | einfo.addr = ofs; | |
371 | einfo.len = 1 << chip->phys_erase_shift; | |
372 | nand_erase_nand(mtd, &einfo, 0); | |
373 | } | |
374 | ||
1da177e4 | 375 | /* Get block number */ |
4226b510 | 376 | block = (int)(ofs >> chip->bbt_erase_shift); |
e2414f4c | 377 | /* Mark block bad in memory-based BBT */ |
ace4dfee TG |
378 | if (chip->bbt) |
379 | chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1); | |
1da177e4 | 380 | |
e2414f4c BN |
381 | /* Write bad block marker to OOB */ |
382 | if (write_oob) { | |
4a89ff88 | 383 | struct mtd_oob_ops ops; |
df698621 | 384 | loff_t wr_ofs = ofs; |
4a89ff88 | 385 | |
c0b8ba7b | 386 | nand_get_device(chip, mtd, FL_WRITING); |
f1a28c02 | 387 | |
4a89ff88 BN |
388 | ops.datbuf = NULL; |
389 | ops.oobbuf = buf; | |
85443319 BN |
390 | ops.ooboffs = chip->badblockpos; |
391 | if (chip->options & NAND_BUSWIDTH_16) { | |
392 | ops.ooboffs &= ~0x01; | |
393 | ops.len = ops.ooblen = 2; | |
394 | } else { | |
395 | ops.len = ops.ooblen = 1; | |
396 | } | |
23b1a99b | 397 | ops.mode = MTD_OPS_PLACE_OOB; |
df698621 | 398 | |
e2414f4c | 399 | /* Write to first/last page(s) if necessary */ |
df698621 BN |
400 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
401 | wr_ofs += mtd->erasesize - mtd->writesize; | |
02ed70bb | 402 | do { |
e2414f4c BN |
403 | res = nand_do_write_oob(mtd, wr_ofs, &ops); |
404 | if (!ret) | |
405 | ret = res; | |
02ed70bb | 406 | |
02ed70bb | 407 | i++; |
df698621 | 408 | wr_ofs += mtd->writesize; |
e2414f4c | 409 | } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2); |
02ed70bb | 410 | |
c0b8ba7b | 411 | nand_release_device(mtd); |
f1a28c02 | 412 | } |
e2414f4c BN |
413 | |
414 | /* Update flash-based bad block table */ | |
415 | if (chip->bbt_options & NAND_BBT_USE_FLASH) { | |
416 | res = nand_update_bbt(mtd, ofs); | |
417 | if (!ret) | |
418 | ret = res; | |
419 | } | |
420 | ||
f1a28c02 TG |
421 | if (!ret) |
422 | mtd->ecc_stats.badblocks++; | |
c0b8ba7b | 423 | |
f1a28c02 | 424 | return ret; |
1da177e4 LT |
425 | } |
426 | ||
61b03bd7 | 427 | /** |
1da177e4 | 428 | * nand_check_wp - [GENERIC] check if the chip is write protected |
8b6e50c9 | 429 | * @mtd: MTD device structure |
1da177e4 | 430 | * |
8b6e50c9 BN |
431 | * Check, if the device is write protected. The function expects, that the |
432 | * device is already selected. | |
1da177e4 | 433 | */ |
e0c7d767 | 434 | static int nand_check_wp(struct mtd_info *mtd) |
1da177e4 | 435 | { |
ace4dfee | 436 | struct nand_chip *chip = mtd->priv; |
93edbad6 | 437 | |
8b6e50c9 | 438 | /* Broken xD cards report WP despite being writable */ |
93edbad6 ML |
439 | if (chip->options & NAND_BROKEN_XD) |
440 | return 0; | |
441 | ||
1da177e4 | 442 | /* Check the WP bit */ |
ace4dfee TG |
443 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
444 | return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1; | |
1da177e4 LT |
445 | } |
446 | ||
447 | /** | |
448 | * nand_block_checkbad - [GENERIC] Check if a block is marked bad | |
8b6e50c9 BN |
449 | * @mtd: MTD device structure |
450 | * @ofs: offset from device start | |
451 | * @getchip: 0, if the chip is already selected | |
452 | * @allowbbt: 1, if its allowed to access the bbt area | |
1da177e4 LT |
453 | * |
454 | * Check, if the block is bad. Either by reading the bad block table or | |
455 | * calling of the scan function. | |
456 | */ | |
2c0a2bed TG |
457 | static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, |
458 | int allowbbt) | |
1da177e4 | 459 | { |
ace4dfee | 460 | struct nand_chip *chip = mtd->priv; |
61b03bd7 | 461 | |
ace4dfee TG |
462 | if (!chip->bbt) |
463 | return chip->block_bad(mtd, ofs, getchip); | |
61b03bd7 | 464 | |
1da177e4 | 465 | /* Return info from the table */ |
e0c7d767 | 466 | return nand_isbad_bbt(mtd, ofs, allowbbt); |
1da177e4 LT |
467 | } |
468 | ||
2af7c653 SK |
469 | /** |
470 | * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands. | |
8b6e50c9 BN |
471 | * @mtd: MTD device structure |
472 | * @timeo: Timeout | |
2af7c653 SK |
473 | * |
474 | * Helper function for nand_wait_ready used when needing to wait in interrupt | |
475 | * context. | |
476 | */ | |
477 | static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo) | |
478 | { | |
479 | struct nand_chip *chip = mtd->priv; | |
480 | int i; | |
481 | ||
482 | /* Wait for the device to get ready */ | |
483 | for (i = 0; i < timeo; i++) { | |
484 | if (chip->dev_ready(mtd)) | |
485 | break; | |
486 | touch_softlockup_watchdog(); | |
487 | mdelay(1); | |
488 | } | |
489 | } | |
490 | ||
7854d3f7 | 491 | /* Wait for the ready pin, after a command. The timeout is caught later. */ |
4b648b02 | 492 | void nand_wait_ready(struct mtd_info *mtd) |
3b88775c | 493 | { |
ace4dfee | 494 | struct nand_chip *chip = mtd->priv; |
e0c7d767 | 495 | unsigned long timeo = jiffies + 2; |
3b88775c | 496 | |
2af7c653 SK |
497 | /* 400ms timeout */ |
498 | if (in_interrupt() || oops_in_progress) | |
499 | return panic_nand_wait_ready(mtd, 400); | |
500 | ||
8fe833c1 | 501 | led_trigger_event(nand_led_trigger, LED_FULL); |
7854d3f7 | 502 | /* Wait until command is processed or timeout occurs */ |
3b88775c | 503 | do { |
ace4dfee | 504 | if (chip->dev_ready(mtd)) |
8fe833c1 | 505 | break; |
8446f1d3 | 506 | touch_softlockup_watchdog(); |
61b03bd7 | 507 | } while (time_before(jiffies, timeo)); |
8fe833c1 | 508 | led_trigger_event(nand_led_trigger, LED_OFF); |
3b88775c | 509 | } |
4b648b02 | 510 | EXPORT_SYMBOL_GPL(nand_wait_ready); |
3b88775c | 511 | |
1da177e4 LT |
512 | /** |
513 | * nand_command - [DEFAULT] Send command to NAND device | |
8b6e50c9 BN |
514 | * @mtd: MTD device structure |
515 | * @command: the command to be sent | |
516 | * @column: the column address for this command, -1 if none | |
517 | * @page_addr: the page address for this command, -1 if none | |
1da177e4 | 518 | * |
8b6e50c9 BN |
519 | * Send command to NAND device. This function is used for small page devices |
520 | * (256/512 Bytes per page). | |
1da177e4 | 521 | */ |
7abd3ef9 TG |
522 | static void nand_command(struct mtd_info *mtd, unsigned int command, |
523 | int column, int page_addr) | |
1da177e4 | 524 | { |
ace4dfee | 525 | register struct nand_chip *chip = mtd->priv; |
7abd3ef9 | 526 | int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE; |
1da177e4 | 527 | |
8b6e50c9 | 528 | /* Write out the command to the device */ |
1da177e4 LT |
529 | if (command == NAND_CMD_SEQIN) { |
530 | int readcmd; | |
531 | ||
28318776 | 532 | if (column >= mtd->writesize) { |
1da177e4 | 533 | /* OOB area */ |
28318776 | 534 | column -= mtd->writesize; |
1da177e4 LT |
535 | readcmd = NAND_CMD_READOOB; |
536 | } else if (column < 256) { | |
537 | /* First 256 bytes --> READ0 */ | |
538 | readcmd = NAND_CMD_READ0; | |
539 | } else { | |
540 | column -= 256; | |
541 | readcmd = NAND_CMD_READ1; | |
542 | } | |
ace4dfee | 543 | chip->cmd_ctrl(mtd, readcmd, ctrl); |
7abd3ef9 | 544 | ctrl &= ~NAND_CTRL_CHANGE; |
1da177e4 | 545 | } |
ace4dfee | 546 | chip->cmd_ctrl(mtd, command, ctrl); |
1da177e4 | 547 | |
8b6e50c9 | 548 | /* Address cycle, when necessary */ |
7abd3ef9 TG |
549 | ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE; |
550 | /* Serially input address */ | |
551 | if (column != -1) { | |
552 | /* Adjust columns for 16 bit buswidth */ | |
ace4dfee | 553 | if (chip->options & NAND_BUSWIDTH_16) |
7abd3ef9 | 554 | column >>= 1; |
ace4dfee | 555 | chip->cmd_ctrl(mtd, column, ctrl); |
7abd3ef9 TG |
556 | ctrl &= ~NAND_CTRL_CHANGE; |
557 | } | |
558 | if (page_addr != -1) { | |
ace4dfee | 559 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
7abd3ef9 | 560 | ctrl &= ~NAND_CTRL_CHANGE; |
ace4dfee | 561 | chip->cmd_ctrl(mtd, page_addr >> 8, ctrl); |
7abd3ef9 | 562 | /* One more address cycle for devices > 32MiB */ |
ace4dfee TG |
563 | if (chip->chipsize > (32 << 20)) |
564 | chip->cmd_ctrl(mtd, page_addr >> 16, ctrl); | |
1da177e4 | 565 | } |
ace4dfee | 566 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
61b03bd7 TG |
567 | |
568 | /* | |
8b6e50c9 BN |
569 | * Program and erase have their own busy handlers status and sequential |
570 | * in needs no delay | |
e0c7d767 | 571 | */ |
1da177e4 | 572 | switch (command) { |
61b03bd7 | 573 | |
1da177e4 LT |
574 | case NAND_CMD_PAGEPROG: |
575 | case NAND_CMD_ERASE1: | |
576 | case NAND_CMD_ERASE2: | |
577 | case NAND_CMD_SEQIN: | |
578 | case NAND_CMD_STATUS: | |
579 | return; | |
580 | ||
581 | case NAND_CMD_RESET: | |
ace4dfee | 582 | if (chip->dev_ready) |
1da177e4 | 583 | break; |
ace4dfee TG |
584 | udelay(chip->chip_delay); |
585 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, | |
7abd3ef9 | 586 | NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
12efdde3 TG |
587 | chip->cmd_ctrl(mtd, |
588 | NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); | |
f8ac0414 FF |
589 | while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) |
590 | ; | |
1da177e4 LT |
591 | return; |
592 | ||
e0c7d767 | 593 | /* This applies to read commands */ |
1da177e4 | 594 | default: |
61b03bd7 | 595 | /* |
1da177e4 LT |
596 | * If we don't have access to the busy pin, we apply the given |
597 | * command delay | |
e0c7d767 | 598 | */ |
ace4dfee TG |
599 | if (!chip->dev_ready) { |
600 | udelay(chip->chip_delay); | |
1da177e4 | 601 | return; |
61b03bd7 | 602 | } |
1da177e4 | 603 | } |
8b6e50c9 BN |
604 | /* |
605 | * Apply this short delay always to ensure that we do wait tWB in | |
606 | * any case on any machine. | |
607 | */ | |
e0c7d767 | 608 | ndelay(100); |
3b88775c TG |
609 | |
610 | nand_wait_ready(mtd); | |
1da177e4 LT |
611 | } |
612 | ||
613 | /** | |
614 | * nand_command_lp - [DEFAULT] Send command to NAND large page device | |
8b6e50c9 BN |
615 | * @mtd: MTD device structure |
616 | * @command: the command to be sent | |
617 | * @column: the column address for this command, -1 if none | |
618 | * @page_addr: the page address for this command, -1 if none | |
1da177e4 | 619 | * |
7abd3ef9 | 620 | * Send command to NAND device. This is the version for the new large page |
7854d3f7 BN |
621 | * devices. We don't have the separate regions as we have in the small page |
622 | * devices. We must emulate NAND_CMD_READOOB to keep the code compatible. | |
1da177e4 | 623 | */ |
7abd3ef9 TG |
624 | static void nand_command_lp(struct mtd_info *mtd, unsigned int command, |
625 | int column, int page_addr) | |
1da177e4 | 626 | { |
ace4dfee | 627 | register struct nand_chip *chip = mtd->priv; |
1da177e4 LT |
628 | |
629 | /* Emulate NAND_CMD_READOOB */ | |
630 | if (command == NAND_CMD_READOOB) { | |
28318776 | 631 | column += mtd->writesize; |
1da177e4 LT |
632 | command = NAND_CMD_READ0; |
633 | } | |
61b03bd7 | 634 | |
7abd3ef9 | 635 | /* Command latch cycle */ |
ace4dfee | 636 | chip->cmd_ctrl(mtd, command & 0xff, |
7abd3ef9 | 637 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
1da177e4 LT |
638 | |
639 | if (column != -1 || page_addr != -1) { | |
7abd3ef9 | 640 | int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE; |
1da177e4 LT |
641 | |
642 | /* Serially input address */ | |
643 | if (column != -1) { | |
644 | /* Adjust columns for 16 bit buswidth */ | |
ace4dfee | 645 | if (chip->options & NAND_BUSWIDTH_16) |
1da177e4 | 646 | column >>= 1; |
ace4dfee | 647 | chip->cmd_ctrl(mtd, column, ctrl); |
7abd3ef9 | 648 | ctrl &= ~NAND_CTRL_CHANGE; |
ace4dfee | 649 | chip->cmd_ctrl(mtd, column >> 8, ctrl); |
61b03bd7 | 650 | } |
1da177e4 | 651 | if (page_addr != -1) { |
ace4dfee TG |
652 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
653 | chip->cmd_ctrl(mtd, page_addr >> 8, | |
7abd3ef9 | 654 | NAND_NCE | NAND_ALE); |
1da177e4 | 655 | /* One more address cycle for devices > 128MiB */ |
ace4dfee TG |
656 | if (chip->chipsize > (128 << 20)) |
657 | chip->cmd_ctrl(mtd, page_addr >> 16, | |
7abd3ef9 | 658 | NAND_NCE | NAND_ALE); |
1da177e4 | 659 | } |
1da177e4 | 660 | } |
ace4dfee | 661 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
61b03bd7 TG |
662 | |
663 | /* | |
8b6e50c9 BN |
664 | * Program and erase have their own busy handlers status, sequential |
665 | * in, and deplete1 need no delay. | |
30f464b7 | 666 | */ |
1da177e4 | 667 | switch (command) { |
61b03bd7 | 668 | |
1da177e4 LT |
669 | case NAND_CMD_CACHEDPROG: |
670 | case NAND_CMD_PAGEPROG: | |
671 | case NAND_CMD_ERASE1: | |
672 | case NAND_CMD_ERASE2: | |
673 | case NAND_CMD_SEQIN: | |
7bc3312b | 674 | case NAND_CMD_RNDIN: |
1da177e4 | 675 | case NAND_CMD_STATUS: |
30f464b7 | 676 | case NAND_CMD_DEPLETE1: |
1da177e4 LT |
677 | return; |
678 | ||
30f464b7 DM |
679 | case NAND_CMD_STATUS_ERROR: |
680 | case NAND_CMD_STATUS_ERROR0: | |
681 | case NAND_CMD_STATUS_ERROR1: | |
682 | case NAND_CMD_STATUS_ERROR2: | |
683 | case NAND_CMD_STATUS_ERROR3: | |
8b6e50c9 | 684 | /* Read error status commands require only a short delay */ |
ace4dfee | 685 | udelay(chip->chip_delay); |
30f464b7 | 686 | return; |
1da177e4 LT |
687 | |
688 | case NAND_CMD_RESET: | |
ace4dfee | 689 | if (chip->dev_ready) |
1da177e4 | 690 | break; |
ace4dfee | 691 | udelay(chip->chip_delay); |
12efdde3 TG |
692 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
693 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); | |
694 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, | |
695 | NAND_NCE | NAND_CTRL_CHANGE); | |
f8ac0414 FF |
696 | while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) |
697 | ; | |
1da177e4 LT |
698 | return; |
699 | ||
7bc3312b TG |
700 | case NAND_CMD_RNDOUT: |
701 | /* No ready / busy check necessary */ | |
702 | chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART, | |
703 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); | |
704 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, | |
705 | NAND_NCE | NAND_CTRL_CHANGE); | |
706 | return; | |
707 | ||
1da177e4 | 708 | case NAND_CMD_READ0: |
12efdde3 TG |
709 | chip->cmd_ctrl(mtd, NAND_CMD_READSTART, |
710 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); | |
711 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, | |
712 | NAND_NCE | NAND_CTRL_CHANGE); | |
61b03bd7 | 713 | |
e0c7d767 | 714 | /* This applies to read commands */ |
1da177e4 | 715 | default: |
61b03bd7 | 716 | /* |
1da177e4 | 717 | * If we don't have access to the busy pin, we apply the given |
8b6e50c9 | 718 | * command delay. |
e0c7d767 | 719 | */ |
ace4dfee TG |
720 | if (!chip->dev_ready) { |
721 | udelay(chip->chip_delay); | |
1da177e4 | 722 | return; |
61b03bd7 | 723 | } |
1da177e4 | 724 | } |
3b88775c | 725 | |
8b6e50c9 BN |
726 | /* |
727 | * Apply this short delay always to ensure that we do wait tWB in | |
728 | * any case on any machine. | |
729 | */ | |
e0c7d767 | 730 | ndelay(100); |
3b88775c TG |
731 | |
732 | nand_wait_ready(mtd); | |
1da177e4 LT |
733 | } |
734 | ||
2af7c653 SK |
735 | /** |
736 | * panic_nand_get_device - [GENERIC] Get chip for selected access | |
8b6e50c9 BN |
737 | * @chip: the nand chip descriptor |
738 | * @mtd: MTD device structure | |
739 | * @new_state: the state which is requested | |
2af7c653 SK |
740 | * |
741 | * Used when in panic, no locks are taken. | |
742 | */ | |
743 | static void panic_nand_get_device(struct nand_chip *chip, | |
744 | struct mtd_info *mtd, int new_state) | |
745 | { | |
7854d3f7 | 746 | /* Hardware controller shared among independent devices */ |
2af7c653 SK |
747 | chip->controller->active = chip; |
748 | chip->state = new_state; | |
749 | } | |
750 | ||
1da177e4 LT |
751 | /** |
752 | * nand_get_device - [GENERIC] Get chip for selected access | |
8b6e50c9 BN |
753 | * @chip: the nand chip descriptor |
754 | * @mtd: MTD device structure | |
755 | * @new_state: the state which is requested | |
1da177e4 LT |
756 | * |
757 | * Get the device and lock it for exclusive access | |
758 | */ | |
2c0a2bed | 759 | static int |
ace4dfee | 760 | nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state) |
1da177e4 | 761 | { |
ace4dfee TG |
762 | spinlock_t *lock = &chip->controller->lock; |
763 | wait_queue_head_t *wq = &chip->controller->wq; | |
e0c7d767 | 764 | DECLARE_WAITQUEUE(wait, current); |
7351d3a5 | 765 | retry: |
0dfc6246 TG |
766 | spin_lock(lock); |
767 | ||
b8b3ee9a | 768 | /* Hardware controller shared among independent devices */ |
ace4dfee TG |
769 | if (!chip->controller->active) |
770 | chip->controller->active = chip; | |
a36ed299 | 771 | |
ace4dfee TG |
772 | if (chip->controller->active == chip && chip->state == FL_READY) { |
773 | chip->state = new_state; | |
0dfc6246 | 774 | spin_unlock(lock); |
962034f4 VW |
775 | return 0; |
776 | } | |
777 | if (new_state == FL_PM_SUSPENDED) { | |
6b0d9a84 LY |
778 | if (chip->controller->active->state == FL_PM_SUSPENDED) { |
779 | chip->state = FL_PM_SUSPENDED; | |
780 | spin_unlock(lock); | |
781 | return 0; | |
6b0d9a84 | 782 | } |
0dfc6246 TG |
783 | } |
784 | set_current_state(TASK_UNINTERRUPTIBLE); | |
785 | add_wait_queue(wq, &wait); | |
786 | spin_unlock(lock); | |
787 | schedule(); | |
788 | remove_wait_queue(wq, &wait); | |
1da177e4 LT |
789 | goto retry; |
790 | } | |
791 | ||
2af7c653 | 792 | /** |
8b6e50c9 BN |
793 | * panic_nand_wait - [GENERIC] wait until the command is done |
794 | * @mtd: MTD device structure | |
795 | * @chip: NAND chip structure | |
796 | * @timeo: timeout | |
2af7c653 SK |
797 | * |
798 | * Wait for command done. This is a helper function for nand_wait used when | |
799 | * we are in interrupt context. May happen when in panic and trying to write | |
b595076a | 800 | * an oops through mtdoops. |
2af7c653 SK |
801 | */ |
802 | static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip, | |
803 | unsigned long timeo) | |
804 | { | |
805 | int i; | |
806 | for (i = 0; i < timeo; i++) { | |
807 | if (chip->dev_ready) { | |
808 | if (chip->dev_ready(mtd)) | |
809 | break; | |
810 | } else { | |
811 | if (chip->read_byte(mtd) & NAND_STATUS_READY) | |
812 | break; | |
813 | } | |
814 | mdelay(1); | |
f8ac0414 | 815 | } |
2af7c653 SK |
816 | } |
817 | ||
1da177e4 | 818 | /** |
8b6e50c9 BN |
819 | * nand_wait - [DEFAULT] wait until the command is done |
820 | * @mtd: MTD device structure | |
821 | * @chip: NAND chip structure | |
1da177e4 | 822 | * |
8b6e50c9 BN |
823 | * Wait for command done. This applies to erase and program only. Erase can |
824 | * take up to 400ms and program up to 20ms according to general NAND and | |
825 | * SmartMedia specs. | |
844d3b42 | 826 | */ |
7bc3312b | 827 | static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip) |
1da177e4 LT |
828 | { |
829 | ||
e0c7d767 | 830 | unsigned long timeo = jiffies; |
7bc3312b | 831 | int status, state = chip->state; |
61b03bd7 | 832 | |
1da177e4 | 833 | if (state == FL_ERASING) |
e0c7d767 | 834 | timeo += (HZ * 400) / 1000; |
1da177e4 | 835 | else |
e0c7d767 | 836 | timeo += (HZ * 20) / 1000; |
1da177e4 | 837 | |
8fe833c1 RP |
838 | led_trigger_event(nand_led_trigger, LED_FULL); |
839 | ||
8b6e50c9 BN |
840 | /* |
841 | * Apply this short delay always to ensure that we do wait tWB in any | |
842 | * case on any machine. | |
843 | */ | |
e0c7d767 | 844 | ndelay(100); |
1da177e4 | 845 | |
ace4dfee TG |
846 | if ((state == FL_ERASING) && (chip->options & NAND_IS_AND)) |
847 | chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1); | |
61b03bd7 | 848 | else |
ace4dfee | 849 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
1da177e4 | 850 | |
2af7c653 SK |
851 | if (in_interrupt() || oops_in_progress) |
852 | panic_nand_wait(mtd, chip, timeo); | |
853 | else { | |
854 | while (time_before(jiffies, timeo)) { | |
855 | if (chip->dev_ready) { | |
856 | if (chip->dev_ready(mtd)) | |
857 | break; | |
858 | } else { | |
859 | if (chip->read_byte(mtd) & NAND_STATUS_READY) | |
860 | break; | |
861 | } | |
862 | cond_resched(); | |
1da177e4 | 863 | } |
1da177e4 | 864 | } |
8fe833c1 RP |
865 | led_trigger_event(nand_led_trigger, LED_OFF); |
866 | ||
ace4dfee | 867 | status = (int)chip->read_byte(mtd); |
1da177e4 LT |
868 | return status; |
869 | } | |
870 | ||
7d70f334 | 871 | /** |
b6d676db | 872 | * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks |
b6d676db RD |
873 | * @mtd: mtd info |
874 | * @ofs: offset to start unlock from | |
875 | * @len: length to unlock | |
8b6e50c9 BN |
876 | * @invert: when = 0, unlock the range of blocks within the lower and |
877 | * upper boundary address | |
878 | * when = 1, unlock the range of blocks outside the boundaries | |
879 | * of the lower and upper boundary address | |
7d70f334 | 880 | * |
8b6e50c9 | 881 | * Returs unlock status. |
7d70f334 VS |
882 | */ |
883 | static int __nand_unlock(struct mtd_info *mtd, loff_t ofs, | |
884 | uint64_t len, int invert) | |
885 | { | |
886 | int ret = 0; | |
887 | int status, page; | |
888 | struct nand_chip *chip = mtd->priv; | |
889 | ||
890 | /* Submit address of first page to unlock */ | |
891 | page = ofs >> chip->page_shift; | |
892 | chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask); | |
893 | ||
894 | /* Submit address of last page to unlock */ | |
895 | page = (ofs + len) >> chip->page_shift; | |
896 | chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1, | |
897 | (page | invert) & chip->pagemask); | |
898 | ||
899 | /* Call wait ready function */ | |
900 | status = chip->waitfunc(mtd, chip); | |
7d70f334 VS |
901 | /* See if device thinks it succeeded */ |
902 | if (status & 0x01) { | |
289c0522 | 903 | pr_debug("%s: error status = 0x%08x\n", |
7d70f334 VS |
904 | __func__, status); |
905 | ret = -EIO; | |
906 | } | |
907 | ||
908 | return ret; | |
909 | } | |
910 | ||
911 | /** | |
b6d676db | 912 | * nand_unlock - [REPLACEABLE] unlocks specified locked blocks |
b6d676db RD |
913 | * @mtd: mtd info |
914 | * @ofs: offset to start unlock from | |
915 | * @len: length to unlock | |
7d70f334 | 916 | * |
8b6e50c9 | 917 | * Returns unlock status. |
7d70f334 VS |
918 | */ |
919 | int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) | |
920 | { | |
921 | int ret = 0; | |
922 | int chipnr; | |
923 | struct nand_chip *chip = mtd->priv; | |
924 | ||
289c0522 | 925 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
7d70f334 VS |
926 | __func__, (unsigned long long)ofs, len); |
927 | ||
928 | if (check_offs_len(mtd, ofs, len)) | |
929 | ret = -EINVAL; | |
930 | ||
931 | /* Align to last block address if size addresses end of the device */ | |
932 | if (ofs + len == mtd->size) | |
933 | len -= mtd->erasesize; | |
934 | ||
935 | nand_get_device(chip, mtd, FL_UNLOCKING); | |
936 | ||
937 | /* Shift to get chip number */ | |
938 | chipnr = ofs >> chip->chip_shift; | |
939 | ||
940 | chip->select_chip(mtd, chipnr); | |
941 | ||
942 | /* Check, if it is write protected */ | |
943 | if (nand_check_wp(mtd)) { | |
289c0522 | 944 | pr_debug("%s: device is write protected!\n", |
7d70f334 VS |
945 | __func__); |
946 | ret = -EIO; | |
947 | goto out; | |
948 | } | |
949 | ||
950 | ret = __nand_unlock(mtd, ofs, len, 0); | |
951 | ||
952 | out: | |
7d70f334 VS |
953 | nand_release_device(mtd); |
954 | ||
955 | return ret; | |
956 | } | |
7351d3a5 | 957 | EXPORT_SYMBOL(nand_unlock); |
7d70f334 VS |
958 | |
959 | /** | |
b6d676db | 960 | * nand_lock - [REPLACEABLE] locks all blocks present in the device |
b6d676db RD |
961 | * @mtd: mtd info |
962 | * @ofs: offset to start unlock from | |
963 | * @len: length to unlock | |
7d70f334 | 964 | * |
8b6e50c9 BN |
965 | * This feature is not supported in many NAND parts. 'Micron' NAND parts do |
966 | * have this feature, but it allows only to lock all blocks, not for specified | |
967 | * range for block. Implementing 'lock' feature by making use of 'unlock', for | |
968 | * now. | |
7d70f334 | 969 | * |
8b6e50c9 | 970 | * Returns lock status. |
7d70f334 VS |
971 | */ |
972 | int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) | |
973 | { | |
974 | int ret = 0; | |
975 | int chipnr, status, page; | |
976 | struct nand_chip *chip = mtd->priv; | |
977 | ||
289c0522 | 978 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
7d70f334 VS |
979 | __func__, (unsigned long long)ofs, len); |
980 | ||
981 | if (check_offs_len(mtd, ofs, len)) | |
982 | ret = -EINVAL; | |
983 | ||
984 | nand_get_device(chip, mtd, FL_LOCKING); | |
985 | ||
986 | /* Shift to get chip number */ | |
987 | chipnr = ofs >> chip->chip_shift; | |
988 | ||
989 | chip->select_chip(mtd, chipnr); | |
990 | ||
991 | /* Check, if it is write protected */ | |
992 | if (nand_check_wp(mtd)) { | |
289c0522 | 993 | pr_debug("%s: device is write protected!\n", |
7d70f334 VS |
994 | __func__); |
995 | status = MTD_ERASE_FAILED; | |
996 | ret = -EIO; | |
997 | goto out; | |
998 | } | |
999 | ||
1000 | /* Submit address of first page to lock */ | |
1001 | page = ofs >> chip->page_shift; | |
1002 | chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask); | |
1003 | ||
1004 | /* Call wait ready function */ | |
1005 | status = chip->waitfunc(mtd, chip); | |
7d70f334 VS |
1006 | /* See if device thinks it succeeded */ |
1007 | if (status & 0x01) { | |
289c0522 | 1008 | pr_debug("%s: error status = 0x%08x\n", |
7d70f334 VS |
1009 | __func__, status); |
1010 | ret = -EIO; | |
1011 | goto out; | |
1012 | } | |
1013 | ||
1014 | ret = __nand_unlock(mtd, ofs, len, 0x1); | |
1015 | ||
1016 | out: | |
7d70f334 VS |
1017 | nand_release_device(mtd); |
1018 | ||
1019 | return ret; | |
1020 | } | |
7351d3a5 | 1021 | EXPORT_SYMBOL(nand_lock); |
7d70f334 | 1022 | |
8593fbc6 | 1023 | /** |
7854d3f7 | 1024 | * nand_read_page_raw - [INTERN] read raw page data without ecc |
8b6e50c9 BN |
1025 | * @mtd: mtd info structure |
1026 | * @chip: nand chip info structure | |
1027 | * @buf: buffer to store read data | |
1fbb938d | 1028 | * @oob_required: caller requires OOB data read to chip->oob_poi |
8b6e50c9 | 1029 | * @page: page number to read |
52ff49df | 1030 | * |
7854d3f7 | 1031 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
8593fbc6 TG |
1032 | */ |
1033 | static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip, | |
1fbb938d | 1034 | uint8_t *buf, int oob_required, int page) |
8593fbc6 TG |
1035 | { |
1036 | chip->read_buf(mtd, buf, mtd->writesize); | |
279f08d4 BN |
1037 | if (oob_required) |
1038 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); | |
8593fbc6 TG |
1039 | return 0; |
1040 | } | |
1041 | ||
52ff49df | 1042 | /** |
7854d3f7 | 1043 | * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc |
8b6e50c9 BN |
1044 | * @mtd: mtd info structure |
1045 | * @chip: nand chip info structure | |
1046 | * @buf: buffer to store read data | |
1fbb938d | 1047 | * @oob_required: caller requires OOB data read to chip->oob_poi |
8b6e50c9 | 1048 | * @page: page number to read |
52ff49df DB |
1049 | * |
1050 | * We need a special oob layout and handling even when OOB isn't used. | |
1051 | */ | |
7351d3a5 | 1052 | static int nand_read_page_raw_syndrome(struct mtd_info *mtd, |
1fbb938d BN |
1053 | struct nand_chip *chip, uint8_t *buf, |
1054 | int oob_required, int page) | |
52ff49df DB |
1055 | { |
1056 | int eccsize = chip->ecc.size; | |
1057 | int eccbytes = chip->ecc.bytes; | |
1058 | uint8_t *oob = chip->oob_poi; | |
1059 | int steps, size; | |
1060 | ||
1061 | for (steps = chip->ecc.steps; steps > 0; steps--) { | |
1062 | chip->read_buf(mtd, buf, eccsize); | |
1063 | buf += eccsize; | |
1064 | ||
1065 | if (chip->ecc.prepad) { | |
1066 | chip->read_buf(mtd, oob, chip->ecc.prepad); | |
1067 | oob += chip->ecc.prepad; | |
1068 | } | |
1069 | ||
1070 | chip->read_buf(mtd, oob, eccbytes); | |
1071 | oob += eccbytes; | |
1072 | ||
1073 | if (chip->ecc.postpad) { | |
1074 | chip->read_buf(mtd, oob, chip->ecc.postpad); | |
1075 | oob += chip->ecc.postpad; | |
1076 | } | |
1077 | } | |
1078 | ||
1079 | size = mtd->oobsize - (oob - chip->oob_poi); | |
1080 | if (size) | |
1081 | chip->read_buf(mtd, oob, size); | |
1082 | ||
1083 | return 0; | |
1084 | } | |
1085 | ||
1da177e4 | 1086 | /** |
7854d3f7 | 1087 | * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function |
8b6e50c9 BN |
1088 | * @mtd: mtd info structure |
1089 | * @chip: nand chip info structure | |
1090 | * @buf: buffer to store read data | |
1fbb938d | 1091 | * @oob_required: caller requires OOB data read to chip->oob_poi |
8b6e50c9 | 1092 | * @page: page number to read |
068e3c0a | 1093 | */ |
f5bbdacc | 1094 | static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
1fbb938d | 1095 | uint8_t *buf, int oob_required, int page) |
1da177e4 | 1096 | { |
f5bbdacc TG |
1097 | int i, eccsize = chip->ecc.size; |
1098 | int eccbytes = chip->ecc.bytes; | |
1099 | int eccsteps = chip->ecc.steps; | |
1100 | uint8_t *p = buf; | |
4bf63fcb DW |
1101 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
1102 | uint8_t *ecc_code = chip->buffers->ecccode; | |
8b099a39 | 1103 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
3f91e94f | 1104 | unsigned int max_bitflips = 0; |
f5bbdacc | 1105 | |
1fbb938d | 1106 | chip->ecc.read_page_raw(mtd, chip, buf, 1, page); |
f5bbdacc TG |
1107 | |
1108 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) | |
1109 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); | |
1110 | ||
1111 | for (i = 0; i < chip->ecc.total; i++) | |
f75e5097 | 1112 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
f5bbdacc TG |
1113 | |
1114 | eccsteps = chip->ecc.steps; | |
1115 | p = buf; | |
1116 | ||
1117 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { | |
1118 | int stat; | |
1119 | ||
1120 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); | |
3f91e94f | 1121 | if (stat < 0) { |
f5bbdacc | 1122 | mtd->ecc_stats.failed++; |
3f91e94f | 1123 | } else { |
f5bbdacc | 1124 | mtd->ecc_stats.corrected += stat; |
3f91e94f MD |
1125 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
1126 | } | |
f5bbdacc | 1127 | } |
3f91e94f | 1128 | return max_bitflips; |
22c60f5f | 1129 | } |
1da177e4 | 1130 | |
3d459559 | 1131 | /** |
7854d3f7 | 1132 | * nand_read_subpage - [REPLACEABLE] software ECC based sub-page read function |
8b6e50c9 BN |
1133 | * @mtd: mtd info structure |
1134 | * @chip: nand chip info structure | |
1135 | * @data_offs: offset of requested data within the page | |
1136 | * @readlen: data length | |
1137 | * @bufpoi: buffer to store read data | |
3d459559 | 1138 | */ |
7351d3a5 FF |
1139 | static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, |
1140 | uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi) | |
3d459559 AK |
1141 | { |
1142 | int start_step, end_step, num_steps; | |
1143 | uint32_t *eccpos = chip->ecc.layout->eccpos; | |
1144 | uint8_t *p; | |
1145 | int data_col_addr, i, gaps = 0; | |
1146 | int datafrag_len, eccfrag_len, aligned_len, aligned_pos; | |
1147 | int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1; | |
7351d3a5 | 1148 | int index = 0; |
3f91e94f | 1149 | unsigned int max_bitflips = 0; |
3d459559 | 1150 | |
7854d3f7 | 1151 | /* Column address within the page aligned to ECC size (256bytes) */ |
3d459559 AK |
1152 | start_step = data_offs / chip->ecc.size; |
1153 | end_step = (data_offs + readlen - 1) / chip->ecc.size; | |
1154 | num_steps = end_step - start_step + 1; | |
1155 | ||
8b6e50c9 | 1156 | /* Data size aligned to ECC ecc.size */ |
3d459559 AK |
1157 | datafrag_len = num_steps * chip->ecc.size; |
1158 | eccfrag_len = num_steps * chip->ecc.bytes; | |
1159 | ||
1160 | data_col_addr = start_step * chip->ecc.size; | |
1161 | /* If we read not a page aligned data */ | |
1162 | if (data_col_addr != 0) | |
1163 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1); | |
1164 | ||
1165 | p = bufpoi + data_col_addr; | |
1166 | chip->read_buf(mtd, p, datafrag_len); | |
1167 | ||
8b6e50c9 | 1168 | /* Calculate ECC */ |
3d459559 AK |
1169 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) |
1170 | chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]); | |
1171 | ||
8b6e50c9 BN |
1172 | /* |
1173 | * The performance is faster if we position offsets according to | |
7854d3f7 | 1174 | * ecc.pos. Let's make sure that there are no gaps in ECC positions. |
8b6e50c9 | 1175 | */ |
3d459559 AK |
1176 | for (i = 0; i < eccfrag_len - 1; i++) { |
1177 | if (eccpos[i + start_step * chip->ecc.bytes] + 1 != | |
1178 | eccpos[i + start_step * chip->ecc.bytes + 1]) { | |
1179 | gaps = 1; | |
1180 | break; | |
1181 | } | |
1182 | } | |
1183 | if (gaps) { | |
1184 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1); | |
1185 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); | |
1186 | } else { | |
8b6e50c9 | 1187 | /* |
7854d3f7 | 1188 | * Send the command to read the particular ECC bytes take care |
8b6e50c9 BN |
1189 | * about buswidth alignment in read_buf. |
1190 | */ | |
7351d3a5 FF |
1191 | index = start_step * chip->ecc.bytes; |
1192 | ||
1193 | aligned_pos = eccpos[index] & ~(busw - 1); | |
3d459559 | 1194 | aligned_len = eccfrag_len; |
7351d3a5 | 1195 | if (eccpos[index] & (busw - 1)) |
3d459559 | 1196 | aligned_len++; |
7351d3a5 | 1197 | if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1)) |
3d459559 AK |
1198 | aligned_len++; |
1199 | ||
7351d3a5 FF |
1200 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, |
1201 | mtd->writesize + aligned_pos, -1); | |
3d459559 AK |
1202 | chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len); |
1203 | } | |
1204 | ||
1205 | for (i = 0; i < eccfrag_len; i++) | |
7351d3a5 | 1206 | chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]]; |
3d459559 AK |
1207 | |
1208 | p = bufpoi + data_col_addr; | |
1209 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) { | |
1210 | int stat; | |
1211 | ||
7351d3a5 FF |
1212 | stat = chip->ecc.correct(mtd, p, |
1213 | &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]); | |
3f91e94f | 1214 | if (stat < 0) { |
3d459559 | 1215 | mtd->ecc_stats.failed++; |
3f91e94f | 1216 | } else { |
3d459559 | 1217 | mtd->ecc_stats.corrected += stat; |
3f91e94f MD |
1218 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
1219 | } | |
3d459559 | 1220 | } |
3f91e94f | 1221 | return max_bitflips; |
3d459559 AK |
1222 | } |
1223 | ||
068e3c0a | 1224 | /** |
7854d3f7 | 1225 | * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function |
8b6e50c9 BN |
1226 | * @mtd: mtd info structure |
1227 | * @chip: nand chip info structure | |
1228 | * @buf: buffer to store read data | |
1fbb938d | 1229 | * @oob_required: caller requires OOB data read to chip->oob_poi |
8b6e50c9 | 1230 | * @page: page number to read |
068e3c0a | 1231 | * |
7854d3f7 | 1232 | * Not for syndrome calculating ECC controllers which need a special oob layout. |
068e3c0a | 1233 | */ |
f5bbdacc | 1234 | static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
1fbb938d | 1235 | uint8_t *buf, int oob_required, int page) |
1da177e4 | 1236 | { |
f5bbdacc TG |
1237 | int i, eccsize = chip->ecc.size; |
1238 | int eccbytes = chip->ecc.bytes; | |
1239 | int eccsteps = chip->ecc.steps; | |
1240 | uint8_t *p = buf; | |
4bf63fcb DW |
1241 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
1242 | uint8_t *ecc_code = chip->buffers->ecccode; | |
8b099a39 | 1243 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
3f91e94f | 1244 | unsigned int max_bitflips = 0; |
f5bbdacc TG |
1245 | |
1246 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { | |
1247 | chip->ecc.hwctl(mtd, NAND_ECC_READ); | |
1248 | chip->read_buf(mtd, p, eccsize); | |
1249 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); | |
1da177e4 | 1250 | } |
f75e5097 | 1251 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
1da177e4 | 1252 | |
f5bbdacc | 1253 | for (i = 0; i < chip->ecc.total; i++) |
f75e5097 | 1254 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
1da177e4 | 1255 | |
f5bbdacc TG |
1256 | eccsteps = chip->ecc.steps; |
1257 | p = buf; | |
61b03bd7 | 1258 | |
f5bbdacc TG |
1259 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
1260 | int stat; | |
1da177e4 | 1261 | |
f5bbdacc | 1262 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
3f91e94f | 1263 | if (stat < 0) { |
f5bbdacc | 1264 | mtd->ecc_stats.failed++; |
3f91e94f | 1265 | } else { |
f5bbdacc | 1266 | mtd->ecc_stats.corrected += stat; |
3f91e94f MD |
1267 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
1268 | } | |
f5bbdacc | 1269 | } |
3f91e94f | 1270 | return max_bitflips; |
f5bbdacc | 1271 | } |
1da177e4 | 1272 | |
6e0cb135 | 1273 | /** |
7854d3f7 | 1274 | * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first |
8b6e50c9 BN |
1275 | * @mtd: mtd info structure |
1276 | * @chip: nand chip info structure | |
1277 | * @buf: buffer to store read data | |
1fbb938d | 1278 | * @oob_required: caller requires OOB data read to chip->oob_poi |
8b6e50c9 | 1279 | * @page: page number to read |
6e0cb135 | 1280 | * |
8b6e50c9 BN |
1281 | * Hardware ECC for large page chips, require OOB to be read first. For this |
1282 | * ECC mode, the write_page method is re-used from ECC_HW. These methods | |
1283 | * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with | |
1284 | * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from | |
1285 | * the data area, by overwriting the NAND manufacturer bad block markings. | |
6e0cb135 SN |
1286 | */ |
1287 | static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd, | |
1fbb938d | 1288 | struct nand_chip *chip, uint8_t *buf, int oob_required, int page) |
6e0cb135 SN |
1289 | { |
1290 | int i, eccsize = chip->ecc.size; | |
1291 | int eccbytes = chip->ecc.bytes; | |
1292 | int eccsteps = chip->ecc.steps; | |
1293 | uint8_t *p = buf; | |
1294 | uint8_t *ecc_code = chip->buffers->ecccode; | |
1295 | uint32_t *eccpos = chip->ecc.layout->eccpos; | |
1296 | uint8_t *ecc_calc = chip->buffers->ecccalc; | |
3f91e94f | 1297 | unsigned int max_bitflips = 0; |
6e0cb135 SN |
1298 | |
1299 | /* Read the OOB area first */ | |
1300 | chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); | |
1301 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); | |
1302 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page); | |
1303 | ||
1304 | for (i = 0; i < chip->ecc.total; i++) | |
1305 | ecc_code[i] = chip->oob_poi[eccpos[i]]; | |
1306 | ||
1307 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { | |
1308 | int stat; | |
1309 | ||
1310 | chip->ecc.hwctl(mtd, NAND_ECC_READ); | |
1311 | chip->read_buf(mtd, p, eccsize); | |
1312 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); | |
1313 | ||
1314 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL); | |
3f91e94f | 1315 | if (stat < 0) { |
6e0cb135 | 1316 | mtd->ecc_stats.failed++; |
3f91e94f | 1317 | } else { |
6e0cb135 | 1318 | mtd->ecc_stats.corrected += stat; |
3f91e94f MD |
1319 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
1320 | } | |
6e0cb135 | 1321 | } |
3f91e94f | 1322 | return max_bitflips; |
6e0cb135 SN |
1323 | } |
1324 | ||
f5bbdacc | 1325 | /** |
7854d3f7 | 1326 | * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read |
8b6e50c9 BN |
1327 | * @mtd: mtd info structure |
1328 | * @chip: nand chip info structure | |
1329 | * @buf: buffer to store read data | |
1fbb938d | 1330 | * @oob_required: caller requires OOB data read to chip->oob_poi |
8b6e50c9 | 1331 | * @page: page number to read |
f5bbdacc | 1332 | * |
8b6e50c9 BN |
1333 | * The hw generator calculates the error syndrome automatically. Therefore we |
1334 | * need a special oob layout and handling. | |
f5bbdacc TG |
1335 | */ |
1336 | static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip, | |
1fbb938d | 1337 | uint8_t *buf, int oob_required, int page) |
f5bbdacc TG |
1338 | { |
1339 | int i, eccsize = chip->ecc.size; | |
1340 | int eccbytes = chip->ecc.bytes; | |
1341 | int eccsteps = chip->ecc.steps; | |
1342 | uint8_t *p = buf; | |
f75e5097 | 1343 | uint8_t *oob = chip->oob_poi; |
3f91e94f | 1344 | unsigned int max_bitflips = 0; |
1da177e4 | 1345 | |
f5bbdacc TG |
1346 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
1347 | int stat; | |
61b03bd7 | 1348 | |
f5bbdacc TG |
1349 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
1350 | chip->read_buf(mtd, p, eccsize); | |
1da177e4 | 1351 | |
f5bbdacc TG |
1352 | if (chip->ecc.prepad) { |
1353 | chip->read_buf(mtd, oob, chip->ecc.prepad); | |
1354 | oob += chip->ecc.prepad; | |
1355 | } | |
1da177e4 | 1356 | |
f5bbdacc TG |
1357 | chip->ecc.hwctl(mtd, NAND_ECC_READSYN); |
1358 | chip->read_buf(mtd, oob, eccbytes); | |
1359 | stat = chip->ecc.correct(mtd, p, oob, NULL); | |
61b03bd7 | 1360 | |
3f91e94f | 1361 | if (stat < 0) { |
f5bbdacc | 1362 | mtd->ecc_stats.failed++; |
3f91e94f | 1363 | } else { |
f5bbdacc | 1364 | mtd->ecc_stats.corrected += stat; |
3f91e94f MD |
1365 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
1366 | } | |
61b03bd7 | 1367 | |
f5bbdacc | 1368 | oob += eccbytes; |
1da177e4 | 1369 | |
f5bbdacc TG |
1370 | if (chip->ecc.postpad) { |
1371 | chip->read_buf(mtd, oob, chip->ecc.postpad); | |
1372 | oob += chip->ecc.postpad; | |
61b03bd7 | 1373 | } |
f5bbdacc | 1374 | } |
1da177e4 | 1375 | |
f5bbdacc | 1376 | /* Calculate remaining oob bytes */ |
7e4178f9 | 1377 | i = mtd->oobsize - (oob - chip->oob_poi); |
f5bbdacc TG |
1378 | if (i) |
1379 | chip->read_buf(mtd, oob, i); | |
61b03bd7 | 1380 | |
3f91e94f | 1381 | return max_bitflips; |
f5bbdacc | 1382 | } |
1da177e4 | 1383 | |
f5bbdacc | 1384 | /** |
7854d3f7 | 1385 | * nand_transfer_oob - [INTERN] Transfer oob to client buffer |
8b6e50c9 BN |
1386 | * @chip: nand chip structure |
1387 | * @oob: oob destination address | |
1388 | * @ops: oob ops structure | |
1389 | * @len: size of oob to transfer | |
8593fbc6 TG |
1390 | */ |
1391 | static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob, | |
7014568b | 1392 | struct mtd_oob_ops *ops, size_t len) |
8593fbc6 | 1393 | { |
f8ac0414 | 1394 | switch (ops->mode) { |
8593fbc6 | 1395 | |
0612b9dd BN |
1396 | case MTD_OPS_PLACE_OOB: |
1397 | case MTD_OPS_RAW: | |
8593fbc6 TG |
1398 | memcpy(oob, chip->oob_poi + ops->ooboffs, len); |
1399 | return oob + len; | |
1400 | ||
0612b9dd | 1401 | case MTD_OPS_AUTO_OOB: { |
8593fbc6 | 1402 | struct nand_oobfree *free = chip->ecc.layout->oobfree; |
7bc3312b TG |
1403 | uint32_t boffs = 0, roffs = ops->ooboffs; |
1404 | size_t bytes = 0; | |
8593fbc6 | 1405 | |
f8ac0414 | 1406 | for (; free->length && len; free++, len -= bytes) { |
8b6e50c9 | 1407 | /* Read request not from offset 0? */ |
7bc3312b TG |
1408 | if (unlikely(roffs)) { |
1409 | if (roffs >= free->length) { | |
1410 | roffs -= free->length; | |
1411 | continue; | |
1412 | } | |
1413 | boffs = free->offset + roffs; | |
1414 | bytes = min_t(size_t, len, | |
1415 | (free->length - roffs)); | |
1416 | roffs = 0; | |
1417 | } else { | |
1418 | bytes = min_t(size_t, len, free->length); | |
1419 | boffs = free->offset; | |
1420 | } | |
1421 | memcpy(oob, chip->oob_poi + boffs, bytes); | |
8593fbc6 TG |
1422 | oob += bytes; |
1423 | } | |
1424 | return oob; | |
1425 | } | |
1426 | default: | |
1427 | BUG(); | |
1428 | } | |
1429 | return NULL; | |
1430 | } | |
1431 | ||
1432 | /** | |
7854d3f7 | 1433 | * nand_do_read_ops - [INTERN] Read data with ECC |
8b6e50c9 BN |
1434 | * @mtd: MTD device structure |
1435 | * @from: offset to read from | |
1436 | * @ops: oob ops structure | |
f5bbdacc TG |
1437 | * |
1438 | * Internal function. Called with chip held. | |
1439 | */ | |
8593fbc6 TG |
1440 | static int nand_do_read_ops(struct mtd_info *mtd, loff_t from, |
1441 | struct mtd_oob_ops *ops) | |
f5bbdacc | 1442 | { |
e47f3db4 | 1443 | int chipnr, page, realpage, col, bytes, aligned, oob_required; |
f5bbdacc TG |
1444 | struct nand_chip *chip = mtd->priv; |
1445 | struct mtd_ecc_stats stats; | |
f5bbdacc | 1446 | int ret = 0; |
8593fbc6 | 1447 | uint32_t readlen = ops->len; |
7014568b | 1448 | uint32_t oobreadlen = ops->ooblen; |
0612b9dd | 1449 | uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ? |
9aca334e ML |
1450 | mtd->oobavail : mtd->oobsize; |
1451 | ||
8593fbc6 | 1452 | uint8_t *bufpoi, *oob, *buf; |
edbc4540 | 1453 | unsigned int max_bitflips = 0; |
1da177e4 | 1454 | |
f5bbdacc | 1455 | stats = mtd->ecc_stats; |
1da177e4 | 1456 | |
f5bbdacc TG |
1457 | chipnr = (int)(from >> chip->chip_shift); |
1458 | chip->select_chip(mtd, chipnr); | |
61b03bd7 | 1459 | |
f5bbdacc TG |
1460 | realpage = (int)(from >> chip->page_shift); |
1461 | page = realpage & chip->pagemask; | |
1da177e4 | 1462 | |
f5bbdacc | 1463 | col = (int)(from & (mtd->writesize - 1)); |
61b03bd7 | 1464 | |
8593fbc6 TG |
1465 | buf = ops->datbuf; |
1466 | oob = ops->oobbuf; | |
e47f3db4 | 1467 | oob_required = oob ? 1 : 0; |
8593fbc6 | 1468 | |
f8ac0414 | 1469 | while (1) { |
f5bbdacc TG |
1470 | bytes = min(mtd->writesize - col, readlen); |
1471 | aligned = (bytes == mtd->writesize); | |
61b03bd7 | 1472 | |
8b6e50c9 | 1473 | /* Is the current page in the buffer? */ |
8593fbc6 | 1474 | if (realpage != chip->pagebuf || oob) { |
4bf63fcb | 1475 | bufpoi = aligned ? buf : chip->buffers->databuf; |
61b03bd7 | 1476 | |
c00a0991 | 1477 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page); |
1da177e4 | 1478 | |
edbc4540 MD |
1479 | /* |
1480 | * Now read the page into the buffer. Absent an error, | |
1481 | * the read methods return max bitflips per ecc step. | |
1482 | */ | |
0612b9dd | 1483 | if (unlikely(ops->mode == MTD_OPS_RAW)) |
1fbb938d | 1484 | ret = chip->ecc.read_page_raw(mtd, chip, bufpoi, |
e47f3db4 BN |
1485 | oob_required, |
1486 | page); | |
3d459559 | 1487 | else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob) |
7351d3a5 FF |
1488 | ret = chip->ecc.read_subpage(mtd, chip, |
1489 | col, bytes, bufpoi); | |
956e944c | 1490 | else |
46a8cf2d | 1491 | ret = chip->ecc.read_page(mtd, chip, bufpoi, |
e47f3db4 | 1492 | oob_required, page); |
6d77b9d0 BN |
1493 | if (ret < 0) { |
1494 | if (!aligned) | |
1495 | /* Invalidate page cache */ | |
1496 | chip->pagebuf = -1; | |
1da177e4 | 1497 | break; |
6d77b9d0 | 1498 | } |
f5bbdacc | 1499 | |
edbc4540 MD |
1500 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
1501 | ||
f5bbdacc TG |
1502 | /* Transfer not aligned data */ |
1503 | if (!aligned) { | |
c1194c79 | 1504 | if (!NAND_SUBPAGE_READ(chip) && !oob && |
6d77b9d0 | 1505 | !(mtd->ecc_stats.failed - stats.failed) && |
edbc4540 | 1506 | (ops->mode != MTD_OPS_RAW)) { |
3d459559 | 1507 | chip->pagebuf = realpage; |
edbc4540 MD |
1508 | chip->pagebuf_bitflips = ret; |
1509 | } else { | |
6d77b9d0 BN |
1510 | /* Invalidate page cache */ |
1511 | chip->pagebuf = -1; | |
edbc4540 | 1512 | } |
4bf63fcb | 1513 | memcpy(buf, chip->buffers->databuf + col, bytes); |
f5bbdacc TG |
1514 | } |
1515 | ||
8593fbc6 TG |
1516 | buf += bytes; |
1517 | ||
1518 | if (unlikely(oob)) { | |
b64d39d8 ML |
1519 | int toread = min(oobreadlen, max_oobsize); |
1520 | ||
1521 | if (toread) { | |
1522 | oob = nand_transfer_oob(chip, | |
1523 | oob, ops, toread); | |
1524 | oobreadlen -= toread; | |
1525 | } | |
8593fbc6 | 1526 | } |
8593fbc6 | 1527 | } else { |
4bf63fcb | 1528 | memcpy(buf, chip->buffers->databuf + col, bytes); |
8593fbc6 | 1529 | buf += bytes; |
edbc4540 MD |
1530 | max_bitflips = max_t(unsigned int, max_bitflips, |
1531 | chip->pagebuf_bitflips); | |
8593fbc6 | 1532 | } |
1da177e4 | 1533 | |
f5bbdacc | 1534 | readlen -= bytes; |
61b03bd7 | 1535 | |
f5bbdacc | 1536 | if (!readlen) |
61b03bd7 | 1537 | break; |
1da177e4 | 1538 | |
8b6e50c9 | 1539 | /* For subsequent reads align to page boundary */ |
1da177e4 LT |
1540 | col = 0; |
1541 | /* Increment page address */ | |
1542 | realpage++; | |
1543 | ||
ace4dfee | 1544 | page = realpage & chip->pagemask; |
1da177e4 LT |
1545 | /* Check, if we cross a chip boundary */ |
1546 | if (!page) { | |
1547 | chipnr++; | |
ace4dfee TG |
1548 | chip->select_chip(mtd, -1); |
1549 | chip->select_chip(mtd, chipnr); | |
1da177e4 | 1550 | } |
1da177e4 LT |
1551 | } |
1552 | ||
8593fbc6 | 1553 | ops->retlen = ops->len - (size_t) readlen; |
7014568b VW |
1554 | if (oob) |
1555 | ops->oobretlen = ops->ooblen - oobreadlen; | |
1da177e4 | 1556 | |
3f91e94f | 1557 | if (ret < 0) |
f5bbdacc TG |
1558 | return ret; |
1559 | ||
9a1fcdfd TG |
1560 | if (mtd->ecc_stats.failed - stats.failed) |
1561 | return -EBADMSG; | |
1562 | ||
edbc4540 | 1563 | return max_bitflips; |
f5bbdacc TG |
1564 | } |
1565 | ||
1566 | /** | |
25985edc | 1567 | * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc |
8b6e50c9 BN |
1568 | * @mtd: MTD device structure |
1569 | * @from: offset to read from | |
1570 | * @len: number of bytes to read | |
1571 | * @retlen: pointer to variable to store the number of read bytes | |
1572 | * @buf: the databuffer to put data | |
f5bbdacc | 1573 | * |
8b6e50c9 | 1574 | * Get hold of the chip and call nand_do_read. |
f5bbdacc TG |
1575 | */ |
1576 | static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, | |
1577 | size_t *retlen, uint8_t *buf) | |
1578 | { | |
8593fbc6 | 1579 | struct nand_chip *chip = mtd->priv; |
4a89ff88 | 1580 | struct mtd_oob_ops ops; |
f5bbdacc TG |
1581 | int ret; |
1582 | ||
8593fbc6 | 1583 | nand_get_device(chip, mtd, FL_READING); |
4a89ff88 BN |
1584 | ops.len = len; |
1585 | ops.datbuf = buf; | |
1586 | ops.oobbuf = NULL; | |
11041ae6 | 1587 | ops.mode = MTD_OPS_PLACE_OOB; |
4a89ff88 | 1588 | ret = nand_do_read_ops(mtd, from, &ops); |
4a89ff88 | 1589 | *retlen = ops.retlen; |
f5bbdacc | 1590 | nand_release_device(mtd); |
f5bbdacc | 1591 | return ret; |
1da177e4 LT |
1592 | } |
1593 | ||
7bc3312b | 1594 | /** |
7854d3f7 | 1595 | * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function |
8b6e50c9 BN |
1596 | * @mtd: mtd info structure |
1597 | * @chip: nand chip info structure | |
1598 | * @page: page number to read | |
7bc3312b TG |
1599 | */ |
1600 | static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, | |
5c2ffb11 | 1601 | int page) |
7bc3312b | 1602 | { |
5c2ffb11 | 1603 | chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); |
7bc3312b | 1604 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
5c2ffb11 | 1605 | return 0; |
7bc3312b TG |
1606 | } |
1607 | ||
1608 | /** | |
7854d3f7 | 1609 | * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC |
7bc3312b | 1610 | * with syndromes |
8b6e50c9 BN |
1611 | * @mtd: mtd info structure |
1612 | * @chip: nand chip info structure | |
1613 | * @page: page number to read | |
7bc3312b TG |
1614 | */ |
1615 | static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip, | |
5c2ffb11 | 1616 | int page) |
7bc3312b TG |
1617 | { |
1618 | uint8_t *buf = chip->oob_poi; | |
1619 | int length = mtd->oobsize; | |
1620 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; | |
1621 | int eccsize = chip->ecc.size; | |
1622 | uint8_t *bufpoi = buf; | |
1623 | int i, toread, sndrnd = 0, pos; | |
1624 | ||
1625 | chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page); | |
1626 | for (i = 0; i < chip->ecc.steps; i++) { | |
1627 | if (sndrnd) { | |
1628 | pos = eccsize + i * (eccsize + chunk); | |
1629 | if (mtd->writesize > 512) | |
1630 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1); | |
1631 | else | |
1632 | chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page); | |
1633 | } else | |
1634 | sndrnd = 1; | |
1635 | toread = min_t(int, length, chunk); | |
1636 | chip->read_buf(mtd, bufpoi, toread); | |
1637 | bufpoi += toread; | |
1638 | length -= toread; | |
1639 | } | |
1640 | if (length > 0) | |
1641 | chip->read_buf(mtd, bufpoi, length); | |
1642 | ||
5c2ffb11 | 1643 | return 0; |
7bc3312b TG |
1644 | } |
1645 | ||
1646 | /** | |
7854d3f7 | 1647 | * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function |
8b6e50c9 BN |
1648 | * @mtd: mtd info structure |
1649 | * @chip: nand chip info structure | |
1650 | * @page: page number to write | |
7bc3312b TG |
1651 | */ |
1652 | static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, | |
1653 | int page) | |
1654 | { | |
1655 | int status = 0; | |
1656 | const uint8_t *buf = chip->oob_poi; | |
1657 | int length = mtd->oobsize; | |
1658 | ||
1659 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page); | |
1660 | chip->write_buf(mtd, buf, length); | |
1661 | /* Send command to program the OOB data */ | |
1662 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); | |
1663 | ||
1664 | status = chip->waitfunc(mtd, chip); | |
1665 | ||
0d420f9d | 1666 | return status & NAND_STATUS_FAIL ? -EIO : 0; |
7bc3312b TG |
1667 | } |
1668 | ||
1669 | /** | |
7854d3f7 | 1670 | * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC |
8b6e50c9 BN |
1671 | * with syndrome - only for large page flash |
1672 | * @mtd: mtd info structure | |
1673 | * @chip: nand chip info structure | |
1674 | * @page: page number to write | |
7bc3312b TG |
1675 | */ |
1676 | static int nand_write_oob_syndrome(struct mtd_info *mtd, | |
1677 | struct nand_chip *chip, int page) | |
1678 | { | |
1679 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; | |
1680 | int eccsize = chip->ecc.size, length = mtd->oobsize; | |
1681 | int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps; | |
1682 | const uint8_t *bufpoi = chip->oob_poi; | |
1683 | ||
1684 | /* | |
1685 | * data-ecc-data-ecc ... ecc-oob | |
1686 | * or | |
1687 | * data-pad-ecc-pad-data-pad .... ecc-pad-oob | |
1688 | */ | |
1689 | if (!chip->ecc.prepad && !chip->ecc.postpad) { | |
1690 | pos = steps * (eccsize + chunk); | |
1691 | steps = 0; | |
1692 | } else | |
8b0036ee | 1693 | pos = eccsize; |
7bc3312b TG |
1694 | |
1695 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page); | |
1696 | for (i = 0; i < steps; i++) { | |
1697 | if (sndcmd) { | |
1698 | if (mtd->writesize <= 512) { | |
1699 | uint32_t fill = 0xFFFFFFFF; | |
1700 | ||
1701 | len = eccsize; | |
1702 | while (len > 0) { | |
1703 | int num = min_t(int, len, 4); | |
1704 | chip->write_buf(mtd, (uint8_t *)&fill, | |
1705 | num); | |
1706 | len -= num; | |
1707 | } | |
1708 | } else { | |
1709 | pos = eccsize + i * (eccsize + chunk); | |
1710 | chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1); | |
1711 | } | |
1712 | } else | |
1713 | sndcmd = 1; | |
1714 | len = min_t(int, length, chunk); | |
1715 | chip->write_buf(mtd, bufpoi, len); | |
1716 | bufpoi += len; | |
1717 | length -= len; | |
1718 | } | |
1719 | if (length > 0) | |
1720 | chip->write_buf(mtd, bufpoi, length); | |
1721 | ||
1722 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); | |
1723 | status = chip->waitfunc(mtd, chip); | |
1724 | ||
1725 | return status & NAND_STATUS_FAIL ? -EIO : 0; | |
1726 | } | |
1727 | ||
1da177e4 | 1728 | /** |
7854d3f7 | 1729 | * nand_do_read_oob - [INTERN] NAND read out-of-band |
8b6e50c9 BN |
1730 | * @mtd: MTD device structure |
1731 | * @from: offset to read from | |
1732 | * @ops: oob operations description structure | |
1da177e4 | 1733 | * |
8b6e50c9 | 1734 | * NAND read out-of-band data from the spare area. |
1da177e4 | 1735 | */ |
8593fbc6 TG |
1736 | static int nand_do_read_oob(struct mtd_info *mtd, loff_t from, |
1737 | struct mtd_oob_ops *ops) | |
1da177e4 | 1738 | { |
c00a0991 | 1739 | int page, realpage, chipnr; |
ace4dfee | 1740 | struct nand_chip *chip = mtd->priv; |
041e4575 | 1741 | struct mtd_ecc_stats stats; |
7014568b VW |
1742 | int readlen = ops->ooblen; |
1743 | int len; | |
7bc3312b | 1744 | uint8_t *buf = ops->oobbuf; |
1951f2f7 | 1745 | int ret = 0; |
61b03bd7 | 1746 | |
289c0522 | 1747 | pr_debug("%s: from = 0x%08Lx, len = %i\n", |
20d8e248 | 1748 | __func__, (unsigned long long)from, readlen); |
1da177e4 | 1749 | |
041e4575 BN |
1750 | stats = mtd->ecc_stats; |
1751 | ||
0612b9dd | 1752 | if (ops->mode == MTD_OPS_AUTO_OOB) |
7014568b | 1753 | len = chip->ecc.layout->oobavail; |
03736155 AH |
1754 | else |
1755 | len = mtd->oobsize; | |
1756 | ||
1757 | if (unlikely(ops->ooboffs >= len)) { | |
289c0522 BN |
1758 | pr_debug("%s: attempt to start read outside oob\n", |
1759 | __func__); | |
03736155 AH |
1760 | return -EINVAL; |
1761 | } | |
1762 | ||
1763 | /* Do not allow reads past end of device */ | |
1764 | if (unlikely(from >= mtd->size || | |
1765 | ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) - | |
1766 | (from >> chip->page_shift)) * len)) { | |
289c0522 BN |
1767 | pr_debug("%s: attempt to read beyond end of device\n", |
1768 | __func__); | |
03736155 AH |
1769 | return -EINVAL; |
1770 | } | |
7014568b | 1771 | |
7314e9e7 | 1772 | chipnr = (int)(from >> chip->chip_shift); |
ace4dfee | 1773 | chip->select_chip(mtd, chipnr); |
1da177e4 | 1774 | |
7314e9e7 TG |
1775 | /* Shift to get page */ |
1776 | realpage = (int)(from >> chip->page_shift); | |
1777 | page = realpage & chip->pagemask; | |
1da177e4 | 1778 | |
f8ac0414 | 1779 | while (1) { |
0612b9dd | 1780 | if (ops->mode == MTD_OPS_RAW) |
1951f2f7 | 1781 | ret = chip->ecc.read_oob_raw(mtd, chip, page); |
c46f6483 | 1782 | else |
1951f2f7 SL |
1783 | ret = chip->ecc.read_oob(mtd, chip, page); |
1784 | ||
1785 | if (ret < 0) | |
1786 | break; | |
7014568b VW |
1787 | |
1788 | len = min(len, readlen); | |
1789 | buf = nand_transfer_oob(chip, buf, ops, len); | |
8593fbc6 | 1790 | |
7014568b | 1791 | readlen -= len; |
0d420f9d SZ |
1792 | if (!readlen) |
1793 | break; | |
1794 | ||
7314e9e7 TG |
1795 | /* Increment page address */ |
1796 | realpage++; | |
1797 | ||
1798 | page = realpage & chip->pagemask; | |
1799 | /* Check, if we cross a chip boundary */ | |
1800 | if (!page) { | |
1801 | chipnr++; | |
1802 | chip->select_chip(mtd, -1); | |
1803 | chip->select_chip(mtd, chipnr); | |
1da177e4 LT |
1804 | } |
1805 | } | |
1806 | ||
1951f2f7 SL |
1807 | ops->oobretlen = ops->ooblen - readlen; |
1808 | ||
1809 | if (ret < 0) | |
1810 | return ret; | |
041e4575 BN |
1811 | |
1812 | if (mtd->ecc_stats.failed - stats.failed) | |
1813 | return -EBADMSG; | |
1814 | ||
1815 | return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0; | |
1da177e4 LT |
1816 | } |
1817 | ||
1818 | /** | |
8593fbc6 | 1819 | * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band |
8b6e50c9 BN |
1820 | * @mtd: MTD device structure |
1821 | * @from: offset to read from | |
1822 | * @ops: oob operation description structure | |
1da177e4 | 1823 | * |
8b6e50c9 | 1824 | * NAND read data and/or out-of-band data. |
1da177e4 | 1825 | */ |
8593fbc6 TG |
1826 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, |
1827 | struct mtd_oob_ops *ops) | |
1da177e4 | 1828 | { |
ace4dfee | 1829 | struct nand_chip *chip = mtd->priv; |
8593fbc6 TG |
1830 | int ret = -ENOTSUPP; |
1831 | ||
1832 | ops->retlen = 0; | |
1da177e4 LT |
1833 | |
1834 | /* Do not allow reads past end of device */ | |
7014568b | 1835 | if (ops->datbuf && (from + ops->len) > mtd->size) { |
289c0522 BN |
1836 | pr_debug("%s: attempt to read beyond end of device\n", |
1837 | __func__); | |
1da177e4 LT |
1838 | return -EINVAL; |
1839 | } | |
1840 | ||
ace4dfee | 1841 | nand_get_device(chip, mtd, FL_READING); |
1da177e4 | 1842 | |
f8ac0414 | 1843 | switch (ops->mode) { |
0612b9dd BN |
1844 | case MTD_OPS_PLACE_OOB: |
1845 | case MTD_OPS_AUTO_OOB: | |
1846 | case MTD_OPS_RAW: | |
8593fbc6 | 1847 | break; |
1da177e4 | 1848 | |
8593fbc6 TG |
1849 | default: |
1850 | goto out; | |
1851 | } | |
1da177e4 | 1852 | |
8593fbc6 TG |
1853 | if (!ops->datbuf) |
1854 | ret = nand_do_read_oob(mtd, from, ops); | |
1855 | else | |
1856 | ret = nand_do_read_ops(mtd, from, ops); | |
61b03bd7 | 1857 | |
7351d3a5 | 1858 | out: |
8593fbc6 TG |
1859 | nand_release_device(mtd); |
1860 | return ret; | |
1861 | } | |
61b03bd7 | 1862 | |
1da177e4 | 1863 | |
8593fbc6 | 1864 | /** |
7854d3f7 | 1865 | * nand_write_page_raw - [INTERN] raw page write function |
8b6e50c9 BN |
1866 | * @mtd: mtd info structure |
1867 | * @chip: nand chip info structure | |
1868 | * @buf: data buffer | |
1fbb938d | 1869 | * @oob_required: must write chip->oob_poi to OOB |
52ff49df | 1870 | * |
7854d3f7 | 1871 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
8593fbc6 | 1872 | */ |
fdbad98d | 1873 | static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
1fbb938d | 1874 | const uint8_t *buf, int oob_required) |
8593fbc6 TG |
1875 | { |
1876 | chip->write_buf(mtd, buf, mtd->writesize); | |
279f08d4 BN |
1877 | if (oob_required) |
1878 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); | |
fdbad98d JW |
1879 | |
1880 | return 0; | |
1da177e4 LT |
1881 | } |
1882 | ||
52ff49df | 1883 | /** |
7854d3f7 | 1884 | * nand_write_page_raw_syndrome - [INTERN] raw page write function |
8b6e50c9 BN |
1885 | * @mtd: mtd info structure |
1886 | * @chip: nand chip info structure | |
1887 | * @buf: data buffer | |
1fbb938d | 1888 | * @oob_required: must write chip->oob_poi to OOB |
52ff49df DB |
1889 | * |
1890 | * We need a special oob layout and handling even when ECC isn't checked. | |
1891 | */ | |
fdbad98d | 1892 | static int nand_write_page_raw_syndrome(struct mtd_info *mtd, |
7351d3a5 | 1893 | struct nand_chip *chip, |
1fbb938d | 1894 | const uint8_t *buf, int oob_required) |
52ff49df DB |
1895 | { |
1896 | int eccsize = chip->ecc.size; | |
1897 | int eccbytes = chip->ecc.bytes; | |
1898 | uint8_t *oob = chip->oob_poi; | |
1899 | int steps, size; | |
1900 | ||
1901 | for (steps = chip->ecc.steps; steps > 0; steps--) { | |
1902 | chip->write_buf(mtd, buf, eccsize); | |
1903 | buf += eccsize; | |
1904 | ||
1905 | if (chip->ecc.prepad) { | |
1906 | chip->write_buf(mtd, oob, chip->ecc.prepad); | |
1907 | oob += chip->ecc.prepad; | |
1908 | } | |
1909 | ||
1910 | chip->read_buf(mtd, oob, eccbytes); | |
1911 | oob += eccbytes; | |
1912 | ||
1913 | if (chip->ecc.postpad) { | |
1914 | chip->write_buf(mtd, oob, chip->ecc.postpad); | |
1915 | oob += chip->ecc.postpad; | |
1916 | } | |
1917 | } | |
1918 | ||
1919 | size = mtd->oobsize - (oob - chip->oob_poi); | |
1920 | if (size) | |
1921 | chip->write_buf(mtd, oob, size); | |
fdbad98d JW |
1922 | |
1923 | return 0; | |
52ff49df | 1924 | } |
9223a456 | 1925 | /** |
7854d3f7 | 1926 | * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function |
8b6e50c9 BN |
1927 | * @mtd: mtd info structure |
1928 | * @chip: nand chip info structure | |
1929 | * @buf: data buffer | |
1fbb938d | 1930 | * @oob_required: must write chip->oob_poi to OOB |
9223a456 | 1931 | */ |
fdbad98d | 1932 | static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
1fbb938d | 1933 | const uint8_t *buf, int oob_required) |
9223a456 | 1934 | { |
f75e5097 TG |
1935 | int i, eccsize = chip->ecc.size; |
1936 | int eccbytes = chip->ecc.bytes; | |
1937 | int eccsteps = chip->ecc.steps; | |
4bf63fcb | 1938 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
f75e5097 | 1939 | const uint8_t *p = buf; |
8b099a39 | 1940 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
9223a456 | 1941 | |
7854d3f7 | 1942 | /* Software ECC calculation */ |
8593fbc6 TG |
1943 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
1944 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); | |
9223a456 | 1945 | |
8593fbc6 TG |
1946 | for (i = 0; i < chip->ecc.total; i++) |
1947 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; | |
9223a456 | 1948 | |
fdbad98d | 1949 | return chip->ecc.write_page_raw(mtd, chip, buf, 1); |
f75e5097 | 1950 | } |
9223a456 | 1951 | |
f75e5097 | 1952 | /** |
7854d3f7 | 1953 | * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function |
8b6e50c9 BN |
1954 | * @mtd: mtd info structure |
1955 | * @chip: nand chip info structure | |
1956 | * @buf: data buffer | |
1fbb938d | 1957 | * @oob_required: must write chip->oob_poi to OOB |
f75e5097 | 1958 | */ |
fdbad98d | 1959 | static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
1fbb938d | 1960 | const uint8_t *buf, int oob_required) |
f75e5097 TG |
1961 | { |
1962 | int i, eccsize = chip->ecc.size; | |
1963 | int eccbytes = chip->ecc.bytes; | |
1964 | int eccsteps = chip->ecc.steps; | |
4bf63fcb | 1965 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
f75e5097 | 1966 | const uint8_t *p = buf; |
8b099a39 | 1967 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
9223a456 | 1968 | |
f75e5097 TG |
1969 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
1970 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); | |
29da9cea | 1971 | chip->write_buf(mtd, p, eccsize); |
f75e5097 | 1972 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
9223a456 TG |
1973 | } |
1974 | ||
f75e5097 TG |
1975 | for (i = 0; i < chip->ecc.total; i++) |
1976 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; | |
1977 | ||
1978 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); | |
fdbad98d JW |
1979 | |
1980 | return 0; | |
9223a456 TG |
1981 | } |
1982 | ||
61b03bd7 | 1983 | /** |
7854d3f7 | 1984 | * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write |
8b6e50c9 BN |
1985 | * @mtd: mtd info structure |
1986 | * @chip: nand chip info structure | |
1987 | * @buf: data buffer | |
1fbb938d | 1988 | * @oob_required: must write chip->oob_poi to OOB |
1da177e4 | 1989 | * |
8b6e50c9 BN |
1990 | * The hw generator calculates the error syndrome automatically. Therefore we |
1991 | * need a special oob layout and handling. | |
f75e5097 | 1992 | */ |
fdbad98d | 1993 | static int nand_write_page_syndrome(struct mtd_info *mtd, |
1fbb938d BN |
1994 | struct nand_chip *chip, |
1995 | const uint8_t *buf, int oob_required) | |
1da177e4 | 1996 | { |
f75e5097 TG |
1997 | int i, eccsize = chip->ecc.size; |
1998 | int eccbytes = chip->ecc.bytes; | |
1999 | int eccsteps = chip->ecc.steps; | |
2000 | const uint8_t *p = buf; | |
2001 | uint8_t *oob = chip->oob_poi; | |
1da177e4 | 2002 | |
f75e5097 | 2003 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
1da177e4 | 2004 | |
f75e5097 TG |
2005 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
2006 | chip->write_buf(mtd, p, eccsize); | |
61b03bd7 | 2007 | |
f75e5097 TG |
2008 | if (chip->ecc.prepad) { |
2009 | chip->write_buf(mtd, oob, chip->ecc.prepad); | |
2010 | oob += chip->ecc.prepad; | |
2011 | } | |
2012 | ||
2013 | chip->ecc.calculate(mtd, p, oob); | |
2014 | chip->write_buf(mtd, oob, eccbytes); | |
2015 | oob += eccbytes; | |
2016 | ||
2017 | if (chip->ecc.postpad) { | |
2018 | chip->write_buf(mtd, oob, chip->ecc.postpad); | |
2019 | oob += chip->ecc.postpad; | |
1da177e4 | 2020 | } |
1da177e4 | 2021 | } |
f75e5097 TG |
2022 | |
2023 | /* Calculate remaining oob bytes */ | |
7e4178f9 | 2024 | i = mtd->oobsize - (oob - chip->oob_poi); |
f75e5097 TG |
2025 | if (i) |
2026 | chip->write_buf(mtd, oob, i); | |
fdbad98d JW |
2027 | |
2028 | return 0; | |
f75e5097 TG |
2029 | } |
2030 | ||
2031 | /** | |
956e944c | 2032 | * nand_write_page - [REPLACEABLE] write one page |
8b6e50c9 BN |
2033 | * @mtd: MTD device structure |
2034 | * @chip: NAND chip descriptor | |
2035 | * @buf: the data to write | |
1fbb938d | 2036 | * @oob_required: must write chip->oob_poi to OOB |
8b6e50c9 BN |
2037 | * @page: page number to write |
2038 | * @cached: cached programming | |
2039 | * @raw: use _raw version of write_page | |
f75e5097 TG |
2040 | */ |
2041 | static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip, | |
1fbb938d BN |
2042 | const uint8_t *buf, int oob_required, int page, |
2043 | int cached, int raw) | |
f75e5097 TG |
2044 | { |
2045 | int status; | |
2046 | ||
2047 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page); | |
2048 | ||
956e944c | 2049 | if (unlikely(raw)) |
fdbad98d | 2050 | status = chip->ecc.write_page_raw(mtd, chip, buf, oob_required); |
956e944c | 2051 | else |
fdbad98d JW |
2052 | status = chip->ecc.write_page(mtd, chip, buf, oob_required); |
2053 | ||
2054 | if (status < 0) | |
2055 | return status; | |
f75e5097 TG |
2056 | |
2057 | /* | |
7854d3f7 | 2058 | * Cached progamming disabled for now. Not sure if it's worth the |
8b6e50c9 | 2059 | * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s). |
f75e5097 TG |
2060 | */ |
2061 | cached = 0; | |
2062 | ||
2063 | if (!cached || !(chip->options & NAND_CACHEPRG)) { | |
2064 | ||
2065 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); | |
7bc3312b | 2066 | status = chip->waitfunc(mtd, chip); |
f75e5097 TG |
2067 | /* |
2068 | * See if operation failed and additional status checks are | |
8b6e50c9 | 2069 | * available. |
f75e5097 TG |
2070 | */ |
2071 | if ((status & NAND_STATUS_FAIL) && (chip->errstat)) | |
2072 | status = chip->errstat(mtd, chip, FL_WRITING, status, | |
2073 | page); | |
2074 | ||
2075 | if (status & NAND_STATUS_FAIL) | |
2076 | return -EIO; | |
2077 | } else { | |
2078 | chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1); | |
7bc3312b | 2079 | status = chip->waitfunc(mtd, chip); |
f75e5097 TG |
2080 | } |
2081 | ||
f75e5097 | 2082 | return 0; |
1da177e4 LT |
2083 | } |
2084 | ||
8593fbc6 | 2085 | /** |
7854d3f7 | 2086 | * nand_fill_oob - [INTERN] Transfer client buffer to oob |
f722013e | 2087 | * @mtd: MTD device structure |
8b6e50c9 BN |
2088 | * @oob: oob data buffer |
2089 | * @len: oob data write length | |
2090 | * @ops: oob ops structure | |
8593fbc6 | 2091 | */ |
f722013e TAA |
2092 | static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len, |
2093 | struct mtd_oob_ops *ops) | |
8593fbc6 | 2094 | { |
f722013e TAA |
2095 | struct nand_chip *chip = mtd->priv; |
2096 | ||
2097 | /* | |
2098 | * Initialise to all 0xFF, to avoid the possibility of left over OOB | |
2099 | * data from a previous OOB read. | |
2100 | */ | |
2101 | memset(chip->oob_poi, 0xff, mtd->oobsize); | |
2102 | ||
f8ac0414 | 2103 | switch (ops->mode) { |
8593fbc6 | 2104 | |
0612b9dd BN |
2105 | case MTD_OPS_PLACE_OOB: |
2106 | case MTD_OPS_RAW: | |
8593fbc6 TG |
2107 | memcpy(chip->oob_poi + ops->ooboffs, oob, len); |
2108 | return oob + len; | |
2109 | ||
0612b9dd | 2110 | case MTD_OPS_AUTO_OOB: { |
8593fbc6 | 2111 | struct nand_oobfree *free = chip->ecc.layout->oobfree; |
7bc3312b TG |
2112 | uint32_t boffs = 0, woffs = ops->ooboffs; |
2113 | size_t bytes = 0; | |
8593fbc6 | 2114 | |
f8ac0414 | 2115 | for (; free->length && len; free++, len -= bytes) { |
8b6e50c9 | 2116 | /* Write request not from offset 0? */ |
7bc3312b TG |
2117 | if (unlikely(woffs)) { |
2118 | if (woffs >= free->length) { | |
2119 | woffs -= free->length; | |
2120 | continue; | |
2121 | } | |
2122 | boffs = free->offset + woffs; | |
2123 | bytes = min_t(size_t, len, | |
2124 | (free->length - woffs)); | |
2125 | woffs = 0; | |
2126 | } else { | |
2127 | bytes = min_t(size_t, len, free->length); | |
2128 | boffs = free->offset; | |
2129 | } | |
8b0036ee | 2130 | memcpy(chip->oob_poi + boffs, oob, bytes); |
8593fbc6 TG |
2131 | oob += bytes; |
2132 | } | |
2133 | return oob; | |
2134 | } | |
2135 | default: | |
2136 | BUG(); | |
2137 | } | |
2138 | return NULL; | |
2139 | } | |
2140 | ||
f8ac0414 | 2141 | #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0) |
1da177e4 LT |
2142 | |
2143 | /** | |
7854d3f7 | 2144 | * nand_do_write_ops - [INTERN] NAND write with ECC |
8b6e50c9 BN |
2145 | * @mtd: MTD device structure |
2146 | * @to: offset to write to | |
2147 | * @ops: oob operations description structure | |
1da177e4 | 2148 | * |
8b6e50c9 | 2149 | * NAND write with ECC. |
1da177e4 | 2150 | */ |
8593fbc6 TG |
2151 | static int nand_do_write_ops(struct mtd_info *mtd, loff_t to, |
2152 | struct mtd_oob_ops *ops) | |
1da177e4 | 2153 | { |
29072b96 | 2154 | int chipnr, realpage, page, blockmask, column; |
ace4dfee | 2155 | struct nand_chip *chip = mtd->priv; |
8593fbc6 | 2156 | uint32_t writelen = ops->len; |
782ce79a ML |
2157 | |
2158 | uint32_t oobwritelen = ops->ooblen; | |
0612b9dd | 2159 | uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ? |
782ce79a ML |
2160 | mtd->oobavail : mtd->oobsize; |
2161 | ||
8593fbc6 TG |
2162 | uint8_t *oob = ops->oobbuf; |
2163 | uint8_t *buf = ops->datbuf; | |
29072b96 | 2164 | int ret, subpage; |
e47f3db4 | 2165 | int oob_required = oob ? 1 : 0; |
1da177e4 | 2166 | |
8593fbc6 | 2167 | ops->retlen = 0; |
29072b96 TG |
2168 | if (!writelen) |
2169 | return 0; | |
1da177e4 | 2170 | |
8b6e50c9 | 2171 | /* Reject writes, which are not page aligned */ |
8593fbc6 | 2172 | if (NOTALIGNED(to) || NOTALIGNED(ops->len)) { |
d0370219 BN |
2173 | pr_notice("%s: attempt to write non page aligned data\n", |
2174 | __func__); | |
1da177e4 LT |
2175 | return -EINVAL; |
2176 | } | |
2177 | ||
29072b96 TG |
2178 | column = to & (mtd->writesize - 1); |
2179 | subpage = column || (writelen & (mtd->writesize - 1)); | |
2180 | ||
2181 | if (subpage && oob) | |
2182 | return -EINVAL; | |
1da177e4 | 2183 | |
6a930961 TG |
2184 | chipnr = (int)(to >> chip->chip_shift); |
2185 | chip->select_chip(mtd, chipnr); | |
2186 | ||
1da177e4 LT |
2187 | /* Check, if it is write protected */ |
2188 | if (nand_check_wp(mtd)) | |
8593fbc6 | 2189 | return -EIO; |
1da177e4 | 2190 | |
f75e5097 TG |
2191 | realpage = (int)(to >> chip->page_shift); |
2192 | page = realpage & chip->pagemask; | |
2193 | blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1; | |
2194 | ||
2195 | /* Invalidate the page cache, when we write to the cached page */ | |
2196 | if (to <= (chip->pagebuf << chip->page_shift) && | |
8593fbc6 | 2197 | (chip->pagebuf << chip->page_shift) < (to + ops->len)) |
ace4dfee | 2198 | chip->pagebuf = -1; |
61b03bd7 | 2199 | |
782ce79a | 2200 | /* Don't allow multipage oob writes with offset */ |
cdcf12b2 | 2201 | if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) |
782ce79a ML |
2202 | return -EINVAL; |
2203 | ||
f8ac0414 | 2204 | while (1) { |
29072b96 | 2205 | int bytes = mtd->writesize; |
f75e5097 | 2206 | int cached = writelen > bytes && page != blockmask; |
29072b96 TG |
2207 | uint8_t *wbuf = buf; |
2208 | ||
8b6e50c9 | 2209 | /* Partial page write? */ |
29072b96 TG |
2210 | if (unlikely(column || writelen < (mtd->writesize - 1))) { |
2211 | cached = 0; | |
2212 | bytes = min_t(int, bytes - column, (int) writelen); | |
2213 | chip->pagebuf = -1; | |
2214 | memset(chip->buffers->databuf, 0xff, mtd->writesize); | |
2215 | memcpy(&chip->buffers->databuf[column], buf, bytes); | |
2216 | wbuf = chip->buffers->databuf; | |
2217 | } | |
1da177e4 | 2218 | |
782ce79a ML |
2219 | if (unlikely(oob)) { |
2220 | size_t len = min(oobwritelen, oobmaxlen); | |
f722013e | 2221 | oob = nand_fill_oob(mtd, oob, len, ops); |
782ce79a | 2222 | oobwritelen -= len; |
f722013e TAA |
2223 | } else { |
2224 | /* We still need to erase leftover OOB data */ | |
2225 | memset(chip->oob_poi, 0xff, mtd->oobsize); | |
782ce79a | 2226 | } |
8593fbc6 | 2227 | |
e47f3db4 BN |
2228 | ret = chip->write_page(mtd, chip, wbuf, oob_required, page, |
2229 | cached, (ops->mode == MTD_OPS_RAW)); | |
f75e5097 TG |
2230 | if (ret) |
2231 | break; | |
2232 | ||
2233 | writelen -= bytes; | |
2234 | if (!writelen) | |
2235 | break; | |
2236 | ||
29072b96 | 2237 | column = 0; |
f75e5097 TG |
2238 | buf += bytes; |
2239 | realpage++; | |
2240 | ||
2241 | page = realpage & chip->pagemask; | |
2242 | /* Check, if we cross a chip boundary */ | |
2243 | if (!page) { | |
2244 | chipnr++; | |
2245 | chip->select_chip(mtd, -1); | |
2246 | chip->select_chip(mtd, chipnr); | |
1da177e4 LT |
2247 | } |
2248 | } | |
8593fbc6 | 2249 | |
8593fbc6 | 2250 | ops->retlen = ops->len - writelen; |
7014568b VW |
2251 | if (unlikely(oob)) |
2252 | ops->oobretlen = ops->ooblen; | |
1da177e4 LT |
2253 | return ret; |
2254 | } | |
2255 | ||
2af7c653 SK |
2256 | /** |
2257 | * panic_nand_write - [MTD Interface] NAND write with ECC | |
8b6e50c9 BN |
2258 | * @mtd: MTD device structure |
2259 | * @to: offset to write to | |
2260 | * @len: number of bytes to write | |
2261 | * @retlen: pointer to variable to store the number of written bytes | |
2262 | * @buf: the data to write | |
2af7c653 SK |
2263 | * |
2264 | * NAND write with ECC. Used when performing writes in interrupt context, this | |
2265 | * may for example be called by mtdoops when writing an oops while in panic. | |
2266 | */ | |
2267 | static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, | |
2268 | size_t *retlen, const uint8_t *buf) | |
2269 | { | |
2270 | struct nand_chip *chip = mtd->priv; | |
4a89ff88 | 2271 | struct mtd_oob_ops ops; |
2af7c653 SK |
2272 | int ret; |
2273 | ||
8b6e50c9 | 2274 | /* Wait for the device to get ready */ |
2af7c653 SK |
2275 | panic_nand_wait(mtd, chip, 400); |
2276 | ||
8b6e50c9 | 2277 | /* Grab the device */ |
2af7c653 SK |
2278 | panic_nand_get_device(chip, mtd, FL_WRITING); |
2279 | ||
4a89ff88 BN |
2280 | ops.len = len; |
2281 | ops.datbuf = (uint8_t *)buf; | |
2282 | ops.oobbuf = NULL; | |
11041ae6 | 2283 | ops.mode = MTD_OPS_PLACE_OOB; |
2af7c653 | 2284 | |
4a89ff88 | 2285 | ret = nand_do_write_ops(mtd, to, &ops); |
2af7c653 | 2286 | |
4a89ff88 | 2287 | *retlen = ops.retlen; |
2af7c653 SK |
2288 | return ret; |
2289 | } | |
2290 | ||
f75e5097 | 2291 | /** |
8593fbc6 | 2292 | * nand_write - [MTD Interface] NAND write with ECC |
8b6e50c9 BN |
2293 | * @mtd: MTD device structure |
2294 | * @to: offset to write to | |
2295 | * @len: number of bytes to write | |
2296 | * @retlen: pointer to variable to store the number of written bytes | |
2297 | * @buf: the data to write | |
f75e5097 | 2298 | * |
8b6e50c9 | 2299 | * NAND write with ECC. |
f75e5097 | 2300 | */ |
8593fbc6 TG |
2301 | static int nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
2302 | size_t *retlen, const uint8_t *buf) | |
f75e5097 TG |
2303 | { |
2304 | struct nand_chip *chip = mtd->priv; | |
4a89ff88 | 2305 | struct mtd_oob_ops ops; |
f75e5097 TG |
2306 | int ret; |
2307 | ||
7bc3312b | 2308 | nand_get_device(chip, mtd, FL_WRITING); |
4a89ff88 BN |
2309 | ops.len = len; |
2310 | ops.datbuf = (uint8_t *)buf; | |
2311 | ops.oobbuf = NULL; | |
11041ae6 | 2312 | ops.mode = MTD_OPS_PLACE_OOB; |
4a89ff88 | 2313 | ret = nand_do_write_ops(mtd, to, &ops); |
4a89ff88 | 2314 | *retlen = ops.retlen; |
f75e5097 | 2315 | nand_release_device(mtd); |
8593fbc6 | 2316 | return ret; |
f75e5097 | 2317 | } |
7314e9e7 | 2318 | |
1da177e4 | 2319 | /** |
8593fbc6 | 2320 | * nand_do_write_oob - [MTD Interface] NAND write out-of-band |
8b6e50c9 BN |
2321 | * @mtd: MTD device structure |
2322 | * @to: offset to write to | |
2323 | * @ops: oob operation description structure | |
1da177e4 | 2324 | * |
8b6e50c9 | 2325 | * NAND write out-of-band. |
1da177e4 | 2326 | */ |
8593fbc6 TG |
2327 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
2328 | struct mtd_oob_ops *ops) | |
1da177e4 | 2329 | { |
03736155 | 2330 | int chipnr, page, status, len; |
ace4dfee | 2331 | struct nand_chip *chip = mtd->priv; |
1da177e4 | 2332 | |
289c0522 | 2333 | pr_debug("%s: to = 0x%08x, len = %i\n", |
20d8e248 | 2334 | __func__, (unsigned int)to, (int)ops->ooblen); |
1da177e4 | 2335 | |
0612b9dd | 2336 | if (ops->mode == MTD_OPS_AUTO_OOB) |
03736155 AH |
2337 | len = chip->ecc.layout->oobavail; |
2338 | else | |
2339 | len = mtd->oobsize; | |
2340 | ||
1da177e4 | 2341 | /* Do not allow write past end of page */ |
03736155 | 2342 | if ((ops->ooboffs + ops->ooblen) > len) { |
289c0522 BN |
2343 | pr_debug("%s: attempt to write past end of page\n", |
2344 | __func__); | |
1da177e4 LT |
2345 | return -EINVAL; |
2346 | } | |
2347 | ||
03736155 | 2348 | if (unlikely(ops->ooboffs >= len)) { |
289c0522 BN |
2349 | pr_debug("%s: attempt to start write outside oob\n", |
2350 | __func__); | |
03736155 AH |
2351 | return -EINVAL; |
2352 | } | |
2353 | ||
775adc3d | 2354 | /* Do not allow write past end of device */ |
03736155 AH |
2355 | if (unlikely(to >= mtd->size || |
2356 | ops->ooboffs + ops->ooblen > | |
2357 | ((mtd->size >> chip->page_shift) - | |
2358 | (to >> chip->page_shift)) * len)) { | |
289c0522 BN |
2359 | pr_debug("%s: attempt to write beyond end of device\n", |
2360 | __func__); | |
03736155 AH |
2361 | return -EINVAL; |
2362 | } | |
2363 | ||
7314e9e7 | 2364 | chipnr = (int)(to >> chip->chip_shift); |
ace4dfee | 2365 | chip->select_chip(mtd, chipnr); |
1da177e4 | 2366 | |
7314e9e7 TG |
2367 | /* Shift to get page */ |
2368 | page = (int)(to >> chip->page_shift); | |
2369 | ||
2370 | /* | |
2371 | * Reset the chip. Some chips (like the Toshiba TC5832DC found in one | |
2372 | * of my DiskOnChip 2000 test units) will clear the whole data page too | |
2373 | * if we don't do this. I have no clue why, but I seem to have 'fixed' | |
2374 | * it in the doc2000 driver in August 1999. dwmw2. | |
2375 | */ | |
ace4dfee | 2376 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
1da177e4 LT |
2377 | |
2378 | /* Check, if it is write protected */ | |
2379 | if (nand_check_wp(mtd)) | |
8593fbc6 | 2380 | return -EROFS; |
61b03bd7 | 2381 | |
1da177e4 | 2382 | /* Invalidate the page cache, if we write to the cached page */ |
ace4dfee TG |
2383 | if (page == chip->pagebuf) |
2384 | chip->pagebuf = -1; | |
1da177e4 | 2385 | |
f722013e | 2386 | nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops); |
9ce244b3 | 2387 | |
0612b9dd | 2388 | if (ops->mode == MTD_OPS_RAW) |
9ce244b3 BN |
2389 | status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask); |
2390 | else | |
2391 | status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask); | |
1da177e4 | 2392 | |
7bc3312b TG |
2393 | if (status) |
2394 | return status; | |
1da177e4 | 2395 | |
7014568b | 2396 | ops->oobretlen = ops->ooblen; |
1da177e4 | 2397 | |
7bc3312b | 2398 | return 0; |
8593fbc6 TG |
2399 | } |
2400 | ||
2401 | /** | |
2402 | * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band | |
8b6e50c9 BN |
2403 | * @mtd: MTD device structure |
2404 | * @to: offset to write to | |
2405 | * @ops: oob operation description structure | |
8593fbc6 TG |
2406 | */ |
2407 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, | |
2408 | struct mtd_oob_ops *ops) | |
2409 | { | |
8593fbc6 TG |
2410 | struct nand_chip *chip = mtd->priv; |
2411 | int ret = -ENOTSUPP; | |
2412 | ||
2413 | ops->retlen = 0; | |
2414 | ||
2415 | /* Do not allow writes past end of device */ | |
7014568b | 2416 | if (ops->datbuf && (to + ops->len) > mtd->size) { |
289c0522 BN |
2417 | pr_debug("%s: attempt to write beyond end of device\n", |
2418 | __func__); | |
8593fbc6 TG |
2419 | return -EINVAL; |
2420 | } | |
2421 | ||
7bc3312b | 2422 | nand_get_device(chip, mtd, FL_WRITING); |
8593fbc6 | 2423 | |
f8ac0414 | 2424 | switch (ops->mode) { |
0612b9dd BN |
2425 | case MTD_OPS_PLACE_OOB: |
2426 | case MTD_OPS_AUTO_OOB: | |
2427 | case MTD_OPS_RAW: | |
8593fbc6 TG |
2428 | break; |
2429 | ||
2430 | default: | |
2431 | goto out; | |
2432 | } | |
2433 | ||
2434 | if (!ops->datbuf) | |
2435 | ret = nand_do_write_oob(mtd, to, ops); | |
2436 | else | |
2437 | ret = nand_do_write_ops(mtd, to, ops); | |
2438 | ||
7351d3a5 | 2439 | out: |
1da177e4 | 2440 | nand_release_device(mtd); |
1da177e4 LT |
2441 | return ret; |
2442 | } | |
2443 | ||
1da177e4 | 2444 | /** |
7854d3f7 | 2445 | * single_erase_cmd - [GENERIC] NAND standard block erase command function |
8b6e50c9 BN |
2446 | * @mtd: MTD device structure |
2447 | * @page: the page address of the block which will be erased | |
1da177e4 | 2448 | * |
8b6e50c9 | 2449 | * Standard erase command for NAND chips. |
1da177e4 | 2450 | */ |
e0c7d767 | 2451 | static void single_erase_cmd(struct mtd_info *mtd, int page) |
1da177e4 | 2452 | { |
ace4dfee | 2453 | struct nand_chip *chip = mtd->priv; |
1da177e4 | 2454 | /* Send commands to erase a block */ |
ace4dfee TG |
2455 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); |
2456 | chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); | |
1da177e4 LT |
2457 | } |
2458 | ||
2459 | /** | |
7854d3f7 | 2460 | * multi_erase_cmd - [GENERIC] AND specific block erase command function |
8b6e50c9 BN |
2461 | * @mtd: MTD device structure |
2462 | * @page: the page address of the block which will be erased | |
1da177e4 | 2463 | * |
8b6e50c9 | 2464 | * AND multi block erase command function. Erase 4 consecutive blocks. |
1da177e4 | 2465 | */ |
e0c7d767 | 2466 | static void multi_erase_cmd(struct mtd_info *mtd, int page) |
1da177e4 | 2467 | { |
ace4dfee | 2468 | struct nand_chip *chip = mtd->priv; |
1da177e4 | 2469 | /* Send commands to erase a block */ |
ace4dfee TG |
2470 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++); |
2471 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++); | |
2472 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++); | |
2473 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); | |
2474 | chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); | |
1da177e4 LT |
2475 | } |
2476 | ||
2477 | /** | |
2478 | * nand_erase - [MTD Interface] erase block(s) | |
8b6e50c9 BN |
2479 | * @mtd: MTD device structure |
2480 | * @instr: erase instruction | |
1da177e4 | 2481 | * |
8b6e50c9 | 2482 | * Erase one ore more blocks. |
1da177e4 | 2483 | */ |
e0c7d767 | 2484 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) |
1da177e4 | 2485 | { |
e0c7d767 | 2486 | return nand_erase_nand(mtd, instr, 0); |
1da177e4 | 2487 | } |
61b03bd7 | 2488 | |
30f464b7 | 2489 | #define BBT_PAGE_MASK 0xffffff3f |
1da177e4 | 2490 | /** |
7854d3f7 | 2491 | * nand_erase_nand - [INTERN] erase block(s) |
8b6e50c9 BN |
2492 | * @mtd: MTD device structure |
2493 | * @instr: erase instruction | |
2494 | * @allowbbt: allow erasing the bbt area | |
1da177e4 | 2495 | * |
8b6e50c9 | 2496 | * Erase one ore more blocks. |
1da177e4 | 2497 | */ |
ace4dfee TG |
2498 | int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, |
2499 | int allowbbt) | |
1da177e4 | 2500 | { |
69423d99 | 2501 | int page, status, pages_per_block, ret, chipnr; |
ace4dfee | 2502 | struct nand_chip *chip = mtd->priv; |
f8ac0414 | 2503 | loff_t rewrite_bbt[NAND_MAX_CHIPS] = {0}; |
ace4dfee | 2504 | unsigned int bbt_masked_page = 0xffffffff; |
69423d99 | 2505 | loff_t len; |
1da177e4 | 2506 | |
289c0522 BN |
2507 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
2508 | __func__, (unsigned long long)instr->addr, | |
2509 | (unsigned long long)instr->len); | |
1da177e4 | 2510 | |
6fe5a6ac | 2511 | if (check_offs_len(mtd, instr->addr, instr->len)) |
1da177e4 | 2512 | return -EINVAL; |
1da177e4 | 2513 | |
1da177e4 | 2514 | /* Grab the lock and see if the device is available */ |
ace4dfee | 2515 | nand_get_device(chip, mtd, FL_ERASING); |
1da177e4 LT |
2516 | |
2517 | /* Shift to get first page */ | |
ace4dfee TG |
2518 | page = (int)(instr->addr >> chip->page_shift); |
2519 | chipnr = (int)(instr->addr >> chip->chip_shift); | |
1da177e4 LT |
2520 | |
2521 | /* Calculate pages in each block */ | |
ace4dfee | 2522 | pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); |
1da177e4 LT |
2523 | |
2524 | /* Select the NAND device */ | |
ace4dfee | 2525 | chip->select_chip(mtd, chipnr); |
1da177e4 | 2526 | |
1da177e4 LT |
2527 | /* Check, if it is write protected */ |
2528 | if (nand_check_wp(mtd)) { | |
289c0522 BN |
2529 | pr_debug("%s: device is write protected!\n", |
2530 | __func__); | |
1da177e4 LT |
2531 | instr->state = MTD_ERASE_FAILED; |
2532 | goto erase_exit; | |
2533 | } | |
2534 | ||
ace4dfee TG |
2535 | /* |
2536 | * If BBT requires refresh, set the BBT page mask to see if the BBT | |
2537 | * should be rewritten. Otherwise the mask is set to 0xffffffff which | |
2538 | * can not be matched. This is also done when the bbt is actually | |
7854d3f7 | 2539 | * erased to avoid recursive updates. |
ace4dfee TG |
2540 | */ |
2541 | if (chip->options & BBT_AUTO_REFRESH && !allowbbt) | |
2542 | bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK; | |
30f464b7 | 2543 | |
1da177e4 LT |
2544 | /* Loop through the pages */ |
2545 | len = instr->len; | |
2546 | ||
2547 | instr->state = MTD_ERASING; | |
2548 | ||
2549 | while (len) { | |
12183a20 | 2550 | /* Check if we have a bad block, we do not erase bad blocks! */ |
ace4dfee TG |
2551 | if (nand_block_checkbad(mtd, ((loff_t) page) << |
2552 | chip->page_shift, 0, allowbbt)) { | |
d0370219 BN |
2553 | pr_warn("%s: attempt to erase a bad block at page 0x%08x\n", |
2554 | __func__, page); | |
1da177e4 LT |
2555 | instr->state = MTD_ERASE_FAILED; |
2556 | goto erase_exit; | |
2557 | } | |
61b03bd7 | 2558 | |
ace4dfee TG |
2559 | /* |
2560 | * Invalidate the page cache, if we erase the block which | |
8b6e50c9 | 2561 | * contains the current cached page. |
ace4dfee TG |
2562 | */ |
2563 | if (page <= chip->pagebuf && chip->pagebuf < | |
2564 | (page + pages_per_block)) | |
2565 | chip->pagebuf = -1; | |
1da177e4 | 2566 | |
ace4dfee | 2567 | chip->erase_cmd(mtd, page & chip->pagemask); |
61b03bd7 | 2568 | |
7bc3312b | 2569 | status = chip->waitfunc(mtd, chip); |
1da177e4 | 2570 | |
ace4dfee TG |
2571 | /* |
2572 | * See if operation failed and additional status checks are | |
2573 | * available | |
2574 | */ | |
2575 | if ((status & NAND_STATUS_FAIL) && (chip->errstat)) | |
2576 | status = chip->errstat(mtd, chip, FL_ERASING, | |
2577 | status, page); | |
068e3c0a | 2578 | |
1da177e4 | 2579 | /* See if block erase succeeded */ |
a4ab4c5d | 2580 | if (status & NAND_STATUS_FAIL) { |
289c0522 BN |
2581 | pr_debug("%s: failed erase, page 0x%08x\n", |
2582 | __func__, page); | |
1da177e4 | 2583 | instr->state = MTD_ERASE_FAILED; |
69423d99 AH |
2584 | instr->fail_addr = |
2585 | ((loff_t)page << chip->page_shift); | |
1da177e4 LT |
2586 | goto erase_exit; |
2587 | } | |
30f464b7 | 2588 | |
ace4dfee TG |
2589 | /* |
2590 | * If BBT requires refresh, set the BBT rewrite flag to the | |
8b6e50c9 | 2591 | * page being erased. |
ace4dfee TG |
2592 | */ |
2593 | if (bbt_masked_page != 0xffffffff && | |
2594 | (page & BBT_PAGE_MASK) == bbt_masked_page) | |
69423d99 AH |
2595 | rewrite_bbt[chipnr] = |
2596 | ((loff_t)page << chip->page_shift); | |
61b03bd7 | 2597 | |
1da177e4 | 2598 | /* Increment page address and decrement length */ |
ace4dfee | 2599 | len -= (1 << chip->phys_erase_shift); |
1da177e4 LT |
2600 | page += pages_per_block; |
2601 | ||
2602 | /* Check, if we cross a chip boundary */ | |
ace4dfee | 2603 | if (len && !(page & chip->pagemask)) { |
1da177e4 | 2604 | chipnr++; |
ace4dfee TG |
2605 | chip->select_chip(mtd, -1); |
2606 | chip->select_chip(mtd, chipnr); | |
30f464b7 | 2607 | |
ace4dfee TG |
2608 | /* |
2609 | * If BBT requires refresh and BBT-PERCHIP, set the BBT | |
8b6e50c9 | 2610 | * page mask to see if this BBT should be rewritten. |
ace4dfee TG |
2611 | */ |
2612 | if (bbt_masked_page != 0xffffffff && | |
2613 | (chip->bbt_td->options & NAND_BBT_PERCHIP)) | |
2614 | bbt_masked_page = chip->bbt_td->pages[chipnr] & | |
2615 | BBT_PAGE_MASK; | |
1da177e4 LT |
2616 | } |
2617 | } | |
2618 | instr->state = MTD_ERASE_DONE; | |
2619 | ||
7351d3a5 | 2620 | erase_exit: |
1da177e4 LT |
2621 | |
2622 | ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO; | |
1da177e4 LT |
2623 | |
2624 | /* Deselect and wake up anyone waiting on the device */ | |
2625 | nand_release_device(mtd); | |
2626 | ||
49defc01 DW |
2627 | /* Do call back function */ |
2628 | if (!ret) | |
2629 | mtd_erase_callback(instr); | |
2630 | ||
ace4dfee TG |
2631 | /* |
2632 | * If BBT requires refresh and erase was successful, rewrite any | |
8b6e50c9 | 2633 | * selected bad block tables. |
ace4dfee TG |
2634 | */ |
2635 | if (bbt_masked_page == 0xffffffff || ret) | |
2636 | return ret; | |
2637 | ||
2638 | for (chipnr = 0; chipnr < chip->numchips; chipnr++) { | |
2639 | if (!rewrite_bbt[chipnr]) | |
2640 | continue; | |
8b6e50c9 | 2641 | /* Update the BBT for chip */ |
289c0522 BN |
2642 | pr_debug("%s: nand_update_bbt (%d:0x%0llx 0x%0x)\n", |
2643 | __func__, chipnr, rewrite_bbt[chipnr], | |
2644 | chip->bbt_td->pages[chipnr]); | |
ace4dfee | 2645 | nand_update_bbt(mtd, rewrite_bbt[chipnr]); |
30f464b7 DM |
2646 | } |
2647 | ||
1da177e4 LT |
2648 | /* Return more or less happy */ |
2649 | return ret; | |
2650 | } | |
2651 | ||
2652 | /** | |
2653 | * nand_sync - [MTD Interface] sync | |
8b6e50c9 | 2654 | * @mtd: MTD device structure |
1da177e4 | 2655 | * |
8b6e50c9 | 2656 | * Sync is actually a wait for chip ready function. |
1da177e4 | 2657 | */ |
e0c7d767 | 2658 | static void nand_sync(struct mtd_info *mtd) |
1da177e4 | 2659 | { |
ace4dfee | 2660 | struct nand_chip *chip = mtd->priv; |
1da177e4 | 2661 | |
289c0522 | 2662 | pr_debug("%s: called\n", __func__); |
1da177e4 LT |
2663 | |
2664 | /* Grab the lock and see if the device is available */ | |
ace4dfee | 2665 | nand_get_device(chip, mtd, FL_SYNCING); |
1da177e4 | 2666 | /* Release it and go back */ |
e0c7d767 | 2667 | nand_release_device(mtd); |
1da177e4 LT |
2668 | } |
2669 | ||
1da177e4 | 2670 | /** |
ace4dfee | 2671 | * nand_block_isbad - [MTD Interface] Check if block at offset is bad |
8b6e50c9 BN |
2672 | * @mtd: MTD device structure |
2673 | * @offs: offset relative to mtd start | |
1da177e4 | 2674 | */ |
ace4dfee | 2675 | static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) |
1da177e4 | 2676 | { |
ace4dfee | 2677 | return nand_block_checkbad(mtd, offs, 1, 0); |
1da177e4 LT |
2678 | } |
2679 | ||
2680 | /** | |
ace4dfee | 2681 | * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad |
8b6e50c9 BN |
2682 | * @mtd: MTD device structure |
2683 | * @ofs: offset relative to mtd start | |
1da177e4 | 2684 | */ |
e0c7d767 | 2685 | static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
1da177e4 | 2686 | { |
ace4dfee | 2687 | struct nand_chip *chip = mtd->priv; |
1da177e4 LT |
2688 | int ret; |
2689 | ||
f8ac0414 FF |
2690 | ret = nand_block_isbad(mtd, ofs); |
2691 | if (ret) { | |
8b6e50c9 | 2692 | /* If it was bad already, return success and do nothing */ |
1da177e4 LT |
2693 | if (ret > 0) |
2694 | return 0; | |
e0c7d767 DW |
2695 | return ret; |
2696 | } | |
1da177e4 | 2697 | |
ace4dfee | 2698 | return chip->block_markbad(mtd, ofs); |
1da177e4 LT |
2699 | } |
2700 | ||
962034f4 VW |
2701 | /** |
2702 | * nand_suspend - [MTD Interface] Suspend the NAND flash | |
8b6e50c9 | 2703 | * @mtd: MTD device structure |
962034f4 VW |
2704 | */ |
2705 | static int nand_suspend(struct mtd_info *mtd) | |
2706 | { | |
ace4dfee | 2707 | struct nand_chip *chip = mtd->priv; |
962034f4 | 2708 | |
ace4dfee | 2709 | return nand_get_device(chip, mtd, FL_PM_SUSPENDED); |
962034f4 VW |
2710 | } |
2711 | ||
2712 | /** | |
2713 | * nand_resume - [MTD Interface] Resume the NAND flash | |
8b6e50c9 | 2714 | * @mtd: MTD device structure |
962034f4 VW |
2715 | */ |
2716 | static void nand_resume(struct mtd_info *mtd) | |
2717 | { | |
ace4dfee | 2718 | struct nand_chip *chip = mtd->priv; |
962034f4 | 2719 | |
ace4dfee | 2720 | if (chip->state == FL_PM_SUSPENDED) |
962034f4 VW |
2721 | nand_release_device(mtd); |
2722 | else | |
d0370219 BN |
2723 | pr_err("%s called for a chip which is not in suspended state\n", |
2724 | __func__); | |
962034f4 VW |
2725 | } |
2726 | ||
8b6e50c9 | 2727 | /* Set default functions */ |
ace4dfee | 2728 | static void nand_set_defaults(struct nand_chip *chip, int busw) |
7aa65bfd | 2729 | { |
1da177e4 | 2730 | /* check for proper chip_delay setup, set 20us if not */ |
ace4dfee TG |
2731 | if (!chip->chip_delay) |
2732 | chip->chip_delay = 20; | |
1da177e4 LT |
2733 | |
2734 | /* check, if a user supplied command function given */ | |
ace4dfee TG |
2735 | if (chip->cmdfunc == NULL) |
2736 | chip->cmdfunc = nand_command; | |
1da177e4 LT |
2737 | |
2738 | /* check, if a user supplied wait function given */ | |
ace4dfee TG |
2739 | if (chip->waitfunc == NULL) |
2740 | chip->waitfunc = nand_wait; | |
2741 | ||
2742 | if (!chip->select_chip) | |
2743 | chip->select_chip = nand_select_chip; | |
2744 | if (!chip->read_byte) | |
2745 | chip->read_byte = busw ? nand_read_byte16 : nand_read_byte; | |
2746 | if (!chip->read_word) | |
2747 | chip->read_word = nand_read_word; | |
2748 | if (!chip->block_bad) | |
2749 | chip->block_bad = nand_block_bad; | |
2750 | if (!chip->block_markbad) | |
2751 | chip->block_markbad = nand_default_block_markbad; | |
2752 | if (!chip->write_buf) | |
2753 | chip->write_buf = busw ? nand_write_buf16 : nand_write_buf; | |
2754 | if (!chip->read_buf) | |
2755 | chip->read_buf = busw ? nand_read_buf16 : nand_read_buf; | |
ace4dfee TG |
2756 | if (!chip->scan_bbt) |
2757 | chip->scan_bbt = nand_default_bbt; | |
f75e5097 TG |
2758 | |
2759 | if (!chip->controller) { | |
2760 | chip->controller = &chip->hwcontrol; | |
2761 | spin_lock_init(&chip->controller->lock); | |
2762 | init_waitqueue_head(&chip->controller->wq); | |
2763 | } | |
2764 | ||
7aa65bfd TG |
2765 | } |
2766 | ||
8b6e50c9 | 2767 | /* Sanitize ONFI strings so we can safely print them */ |
d1e1f4e4 FF |
2768 | static void sanitize_string(uint8_t *s, size_t len) |
2769 | { | |
2770 | ssize_t i; | |
2771 | ||
8b6e50c9 | 2772 | /* Null terminate */ |
d1e1f4e4 FF |
2773 | s[len - 1] = 0; |
2774 | ||
8b6e50c9 | 2775 | /* Remove non printable chars */ |
d1e1f4e4 FF |
2776 | for (i = 0; i < len - 1; i++) { |
2777 | if (s[i] < ' ' || s[i] > 127) | |
2778 | s[i] = '?'; | |
2779 | } | |
2780 | ||
8b6e50c9 | 2781 | /* Remove trailing spaces */ |
d1e1f4e4 FF |
2782 | strim(s); |
2783 | } | |
2784 | ||
2785 | static u16 onfi_crc16(u16 crc, u8 const *p, size_t len) | |
2786 | { | |
2787 | int i; | |
2788 | while (len--) { | |
2789 | crc ^= *p++ << 8; | |
2790 | for (i = 0; i < 8; i++) | |
2791 | crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0); | |
2792 | } | |
2793 | ||
2794 | return crc; | |
2795 | } | |
2796 | ||
6fb277ba | 2797 | /* |
8b6e50c9 | 2798 | * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise. |
6fb277ba FF |
2799 | */ |
2800 | static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip, | |
08c248fb | 2801 | int *busw) |
6fb277ba FF |
2802 | { |
2803 | struct nand_onfi_params *p = &chip->onfi_params; | |
2804 | int i; | |
2805 | int val; | |
2806 | ||
7854d3f7 | 2807 | /* Try ONFI for unknown chip or LP */ |
6fb277ba FF |
2808 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1); |
2809 | if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' || | |
2810 | chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I') | |
2811 | return 0; | |
2812 | ||
6fb277ba FF |
2813 | chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1); |
2814 | for (i = 0; i < 3; i++) { | |
2815 | chip->read_buf(mtd, (uint8_t *)p, sizeof(*p)); | |
2816 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) == | |
2817 | le16_to_cpu(p->crc)) { | |
9a4d4d69 | 2818 | pr_info("ONFI param page %d valid\n", i); |
6fb277ba FF |
2819 | break; |
2820 | } | |
2821 | } | |
2822 | ||
2823 | if (i == 3) | |
2824 | return 0; | |
2825 | ||
8b6e50c9 | 2826 | /* Check version */ |
6fb277ba | 2827 | val = le16_to_cpu(p->revision); |
b7b1a29d BN |
2828 | if (val & (1 << 5)) |
2829 | chip->onfi_version = 23; | |
2830 | else if (val & (1 << 4)) | |
6fb277ba FF |
2831 | chip->onfi_version = 22; |
2832 | else if (val & (1 << 3)) | |
2833 | chip->onfi_version = 21; | |
2834 | else if (val & (1 << 2)) | |
2835 | chip->onfi_version = 20; | |
b7b1a29d | 2836 | else if (val & (1 << 1)) |
6fb277ba | 2837 | chip->onfi_version = 10; |
b7b1a29d BN |
2838 | else |
2839 | chip->onfi_version = 0; | |
2840 | ||
2841 | if (!chip->onfi_version) { | |
d0370219 | 2842 | pr_info("%s: unsupported ONFI version: %d\n", __func__, val); |
b7b1a29d BN |
2843 | return 0; |
2844 | } | |
6fb277ba FF |
2845 | |
2846 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); | |
2847 | sanitize_string(p->model, sizeof(p->model)); | |
2848 | if (!mtd->name) | |
2849 | mtd->name = p->model; | |
2850 | mtd->writesize = le32_to_cpu(p->byte_per_page); | |
2851 | mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize; | |
2852 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); | |
63795755 MC |
2853 | chip->chipsize = le32_to_cpu(p->blocks_per_lun); |
2854 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; | |
08c248fb | 2855 | *busw = 0; |
6fb277ba | 2856 | if (le16_to_cpu(p->features) & 1) |
08c248fb | 2857 | *busw = NAND_BUSWIDTH_16; |
6fb277ba | 2858 | |
d42b5de3 | 2859 | pr_info("ONFI flash detected\n"); |
6fb277ba FF |
2860 | return 1; |
2861 | } | |
2862 | ||
7aa65bfd | 2863 | /* |
8b6e50c9 | 2864 | * Get the flash and manufacturer id and lookup if the type is supported. |
7aa65bfd TG |
2865 | */ |
2866 | static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd, | |
ace4dfee | 2867 | struct nand_chip *chip, |
7351d3a5 FF |
2868 | int busw, |
2869 | int *maf_id, int *dev_id, | |
5e81e88a | 2870 | struct nand_flash_dev *type) |
7aa65bfd | 2871 | { |
d1e1f4e4 | 2872 | int i, maf_idx; |
426c457a | 2873 | u8 id_data[8]; |
6fb277ba | 2874 | int ret; |
1da177e4 LT |
2875 | |
2876 | /* Select the device */ | |
ace4dfee | 2877 | chip->select_chip(mtd, 0); |
1da177e4 | 2878 | |
ef89a880 KB |
2879 | /* |
2880 | * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx) | |
8b6e50c9 | 2881 | * after power-up. |
ef89a880 KB |
2882 | */ |
2883 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); | |
2884 | ||
1da177e4 | 2885 | /* Send the command for reading device ID */ |
ace4dfee | 2886 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
1da177e4 LT |
2887 | |
2888 | /* Read manufacturer and device IDs */ | |
ace4dfee | 2889 | *maf_id = chip->read_byte(mtd); |
d1e1f4e4 | 2890 | *dev_id = chip->read_byte(mtd); |
1da177e4 | 2891 | |
8b6e50c9 BN |
2892 | /* |
2893 | * Try again to make sure, as some systems the bus-hold or other | |
ed8165c7 BD |
2894 | * interface concerns can cause random data which looks like a |
2895 | * possibly credible NAND flash to appear. If the two results do | |
2896 | * not match, ignore the device completely. | |
2897 | */ | |
2898 | ||
2899 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); | |
2900 | ||
d1e1f4e4 | 2901 | for (i = 0; i < 2; i++) |
426c457a | 2902 | id_data[i] = chip->read_byte(mtd); |
ed8165c7 | 2903 | |
d1e1f4e4 | 2904 | if (id_data[0] != *maf_id || id_data[1] != *dev_id) { |
9a4d4d69 | 2905 | pr_info("%s: second ID read did not match " |
d0370219 BN |
2906 | "%02x,%02x against %02x,%02x\n", __func__, |
2907 | *maf_id, *dev_id, id_data[0], id_data[1]); | |
ed8165c7 BD |
2908 | return ERR_PTR(-ENODEV); |
2909 | } | |
2910 | ||
7aa65bfd | 2911 | if (!type) |
5e81e88a DW |
2912 | type = nand_flash_ids; |
2913 | ||
2914 | for (; type->name != NULL; type++) | |
d1e1f4e4 | 2915 | if (*dev_id == type->id) |
f8ac0414 | 2916 | break; |
5e81e88a | 2917 | |
d1e1f4e4 FF |
2918 | chip->onfi_version = 0; |
2919 | if (!type->name || !type->pagesize) { | |
6fb277ba | 2920 | /* Check is chip is ONFI compliant */ |
08c248fb | 2921 | ret = nand_flash_detect_onfi(mtd, chip, &busw); |
6fb277ba FF |
2922 | if (ret) |
2923 | goto ident_done; | |
d1e1f4e4 FF |
2924 | } |
2925 | ||
2926 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); | |
2927 | ||
2928 | /* Read entire ID string */ | |
2929 | ||
2930 | for (i = 0; i < 8; i++) | |
2931 | id_data[i] = chip->read_byte(mtd); | |
2932 | ||
5e81e88a | 2933 | if (!type->name) |
7aa65bfd TG |
2934 | return ERR_PTR(-ENODEV); |
2935 | ||
ba0251fe TG |
2936 | if (!mtd->name) |
2937 | mtd->name = type->name; | |
2938 | ||
69423d99 | 2939 | chip->chipsize = (uint64_t)type->chipsize << 20; |
7aa65bfd | 2940 | |
12a40a57 | 2941 | if (!type->pagesize && chip->init_size) { |
8b6e50c9 | 2942 | /* Set the pagesize, oobsize, erasesize by the driver */ |
12a40a57 HS |
2943 | busw = chip->init_size(mtd, chip, id_data); |
2944 | } else if (!type->pagesize) { | |
7aa65bfd | 2945 | int extid; |
29072b96 | 2946 | /* The 3rd id byte holds MLC / multichip data */ |
426c457a | 2947 | chip->cellinfo = id_data[2]; |
7aa65bfd | 2948 | /* The 4th id byte is the important one */ |
426c457a | 2949 | extid = id_data[3]; |
61b03bd7 | 2950 | |
426c457a KC |
2951 | /* |
2952 | * Field definitions are in the following datasheets: | |
2953 | * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32) | |
34c5bf6c | 2954 | * New style (6 byte ID): Samsung K9GBG08U0M (p.40) |
426c457a KC |
2955 | * |
2956 | * Check for wraparound + Samsung ID + nonzero 6th byte | |
2957 | * to decide what to do. | |
2958 | */ | |
2959 | if (id_data[0] == id_data[6] && id_data[1] == id_data[7] && | |
2960 | id_data[0] == NAND_MFR_SAMSUNG && | |
cfe3fdad | 2961 | (chip->cellinfo & NAND_CI_CELLTYPE_MSK) && |
426c457a KC |
2962 | id_data[5] != 0x00) { |
2963 | /* Calc pagesize */ | |
2964 | mtd->writesize = 2048 << (extid & 0x03); | |
2965 | extid >>= 2; | |
2966 | /* Calc oobsize */ | |
34c5bf6c BN |
2967 | switch (extid & 0x03) { |
2968 | case 1: | |
2969 | mtd->oobsize = 128; | |
2970 | break; | |
2971 | case 2: | |
2972 | mtd->oobsize = 218; | |
2973 | break; | |
2974 | case 3: | |
2975 | mtd->oobsize = 400; | |
2976 | break; | |
2977 | default: | |
2978 | mtd->oobsize = 436; | |
2979 | break; | |
2980 | } | |
426c457a KC |
2981 | extid >>= 2; |
2982 | /* Calc blocksize */ | |
2983 | mtd->erasesize = (128 * 1024) << | |
2984 | (((extid >> 1) & 0x04) | (extid & 0x03)); | |
2985 | busw = 0; | |
2986 | } else { | |
2987 | /* Calc pagesize */ | |
2988 | mtd->writesize = 1024 << (extid & 0x03); | |
2989 | extid >>= 2; | |
2990 | /* Calc oobsize */ | |
2991 | mtd->oobsize = (8 << (extid & 0x01)) * | |
2992 | (mtd->writesize >> 9); | |
2993 | extid >>= 2; | |
2994 | /* Calc blocksize. Blocksize is multiples of 64KiB */ | |
2995 | mtd->erasesize = (64 * 1024) << (extid & 0x03); | |
2996 | extid >>= 2; | |
2997 | /* Get buswidth information */ | |
2998 | busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0; | |
2999 | } | |
7aa65bfd TG |
3000 | } else { |
3001 | /* | |
8b6e50c9 | 3002 | * Old devices have chip data hardcoded in the device id table. |
7aa65bfd | 3003 | */ |
ba0251fe TG |
3004 | mtd->erasesize = type->erasesize; |
3005 | mtd->writesize = type->pagesize; | |
4cbb9b80 | 3006 | mtd->oobsize = mtd->writesize / 32; |
ba0251fe | 3007 | busw = type->options & NAND_BUSWIDTH_16; |
2173bae8 BN |
3008 | |
3009 | /* | |
3010 | * Check for Spansion/AMD ID + repeating 5th, 6th byte since | |
3011 | * some Spansion chips have erasesize that conflicts with size | |
8b6e50c9 | 3012 | * listed in nand_ids table. |
2173bae8 BN |
3013 | * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39) |
3014 | */ | |
3015 | if (*maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && | |
3016 | id_data[5] == 0x00 && id_data[6] == 0x00 && | |
3017 | id_data[7] == 0x00 && mtd->writesize == 512) { | |
3018 | mtd->erasesize = 128 * 1024; | |
3019 | mtd->erasesize <<= ((id_data[3] & 0x03) << 1); | |
3020 | } | |
7aa65bfd | 3021 | } |
bf7a01bf BN |
3022 | /* Get chip options */ |
3023 | chip->options |= type->options; | |
d1e1f4e4 | 3024 | |
8b6e50c9 BN |
3025 | /* |
3026 | * Check if chip is not a Samsung device. Do not clear the | |
3027 | * options for chips which do not have an extended id. | |
d1e1f4e4 FF |
3028 | */ |
3029 | if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize) | |
3030 | chip->options &= ~NAND_SAMSUNG_LP_OPTIONS; | |
3031 | ident_done: | |
3032 | ||
7aa65bfd | 3033 | /* Try to identify manufacturer */ |
9a909867 | 3034 | for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) { |
7aa65bfd TG |
3035 | if (nand_manuf_ids[maf_idx].id == *maf_id) |
3036 | break; | |
3037 | } | |
0ea4a755 | 3038 | |
7aa65bfd TG |
3039 | /* |
3040 | * Check, if buswidth is correct. Hardware drivers should set | |
8b6e50c9 | 3041 | * chip correct! |
7aa65bfd | 3042 | */ |
ace4dfee | 3043 | if (busw != (chip->options & NAND_BUSWIDTH_16)) { |
9a4d4d69 | 3044 | pr_info("NAND device: Manufacturer ID:" |
d0370219 BN |
3045 | " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, |
3046 | *dev_id, nand_manuf_ids[maf_idx].name, mtd->name); | |
9a4d4d69 | 3047 | pr_warn("NAND bus width %d instead %d bit\n", |
d0370219 BN |
3048 | (chip->options & NAND_BUSWIDTH_16) ? 16 : 8, |
3049 | busw ? 16 : 8); | |
7aa65bfd TG |
3050 | return ERR_PTR(-EINVAL); |
3051 | } | |
61b03bd7 | 3052 | |
7aa65bfd | 3053 | /* Calculate the address shift from the page size */ |
ace4dfee | 3054 | chip->page_shift = ffs(mtd->writesize) - 1; |
8b6e50c9 | 3055 | /* Convert chipsize to number of pages per chip -1 */ |
ace4dfee | 3056 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; |
61b03bd7 | 3057 | |
ace4dfee | 3058 | chip->bbt_erase_shift = chip->phys_erase_shift = |
7aa65bfd | 3059 | ffs(mtd->erasesize) - 1; |
69423d99 AH |
3060 | if (chip->chipsize & 0xffffffff) |
3061 | chip->chip_shift = ffs((unsigned)chip->chipsize) - 1; | |
7351d3a5 FF |
3062 | else { |
3063 | chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)); | |
3064 | chip->chip_shift += 32 - 1; | |
3065 | } | |
1da177e4 | 3066 | |
26d9be11 AB |
3067 | chip->badblockbits = 8; |
3068 | ||
7aa65bfd | 3069 | /* Set the bad block position */ |
065a1ed8 | 3070 | if (mtd->writesize > 512 || (busw & NAND_BUSWIDTH_16)) |
c7b28e25 | 3071 | chip->badblockpos = NAND_LARGE_BADBLOCK_POS; |
065a1ed8 BN |
3072 | else |
3073 | chip->badblockpos = NAND_SMALL_BADBLOCK_POS; | |
61b03bd7 | 3074 | |
b60b08b0 KC |
3075 | /* |
3076 | * Bad block marker is stored in the last page of each block | |
c7b28e25 BN |
3077 | * on Samsung and Hynix MLC devices; stored in first two pages |
3078 | * of each block on Micron devices with 2KiB pages and on | |
8c342335 BN |
3079 | * SLC Samsung, Hynix, Toshiba, AMD/Spansion, and Macronix. |
3080 | * All others scan only the first page. | |
b60b08b0 KC |
3081 | */ |
3082 | if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) && | |
3083 | (*maf_id == NAND_MFR_SAMSUNG || | |
3084 | *maf_id == NAND_MFR_HYNIX)) | |
5fb1549d | 3085 | chip->bbt_options |= NAND_BBT_SCANLASTPAGE; |
c7b28e25 BN |
3086 | else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) && |
3087 | (*maf_id == NAND_MFR_SAMSUNG || | |
3088 | *maf_id == NAND_MFR_HYNIX || | |
13ed7aed | 3089 | *maf_id == NAND_MFR_TOSHIBA || |
8c342335 BN |
3090 | *maf_id == NAND_MFR_AMD || |
3091 | *maf_id == NAND_MFR_MACRONIX)) || | |
c7b28e25 BN |
3092 | (mtd->writesize == 2048 && |
3093 | *maf_id == NAND_MFR_MICRON)) | |
5fb1549d | 3094 | chip->bbt_options |= NAND_BBT_SCAN2NDPAGE; |
c7b28e25 | 3095 | |
7aa65bfd | 3096 | /* Check for AND chips with 4 page planes */ |
ace4dfee TG |
3097 | if (chip->options & NAND_4PAGE_ARRAY) |
3098 | chip->erase_cmd = multi_erase_cmd; | |
7aa65bfd | 3099 | else |
ace4dfee | 3100 | chip->erase_cmd = single_erase_cmd; |
7aa65bfd | 3101 | |
8b6e50c9 | 3102 | /* Do not replace user supplied command function! */ |
ace4dfee TG |
3103 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
3104 | chip->cmdfunc = nand_command_lp; | |
7aa65bfd | 3105 | |
886bd33d HS |
3106 | pr_info("NAND device: Manufacturer ID: 0x%02x, Chip ID: 0x%02x (%s %s)," |
3107 | " page size: %d, OOB size: %d\n", | |
3108 | *maf_id, *dev_id, nand_manuf_ids[maf_idx].name, | |
3109 | chip->onfi_version ? chip->onfi_params.model : type->name, | |
3110 | mtd->writesize, mtd->oobsize); | |
7aa65bfd TG |
3111 | |
3112 | return type; | |
3113 | } | |
3114 | ||
7aa65bfd | 3115 | /** |
3b85c321 | 3116 | * nand_scan_ident - [NAND Interface] Scan for the NAND device |
8b6e50c9 BN |
3117 | * @mtd: MTD device structure |
3118 | * @maxchips: number of chips to scan for | |
3119 | * @table: alternative NAND ID table | |
7aa65bfd | 3120 | * |
8b6e50c9 BN |
3121 | * This is the first phase of the normal nand_scan() function. It reads the |
3122 | * flash ID and sets up MTD fields accordingly. | |
7aa65bfd | 3123 | * |
3b85c321 | 3124 | * The mtd->owner field must be set to the module of the caller. |
7aa65bfd | 3125 | */ |
5e81e88a DW |
3126 | int nand_scan_ident(struct mtd_info *mtd, int maxchips, |
3127 | struct nand_flash_dev *table) | |
7aa65bfd | 3128 | { |
d1e1f4e4 | 3129 | int i, busw, nand_maf_id, nand_dev_id; |
ace4dfee | 3130 | struct nand_chip *chip = mtd->priv; |
7aa65bfd TG |
3131 | struct nand_flash_dev *type; |
3132 | ||
7aa65bfd | 3133 | /* Get buswidth to select the correct functions */ |
ace4dfee | 3134 | busw = chip->options & NAND_BUSWIDTH_16; |
7aa65bfd | 3135 | /* Set the default functions */ |
ace4dfee | 3136 | nand_set_defaults(chip, busw); |
7aa65bfd TG |
3137 | |
3138 | /* Read the flash type */ | |
7351d3a5 FF |
3139 | type = nand_get_flash_type(mtd, chip, busw, |
3140 | &nand_maf_id, &nand_dev_id, table); | |
7aa65bfd TG |
3141 | |
3142 | if (IS_ERR(type)) { | |
b1c6e6db | 3143 | if (!(chip->options & NAND_SCAN_SILENT_NODEV)) |
d0370219 | 3144 | pr_warn("No NAND device found\n"); |
ace4dfee | 3145 | chip->select_chip(mtd, -1); |
7aa65bfd | 3146 | return PTR_ERR(type); |
1da177e4 LT |
3147 | } |
3148 | ||
7aa65bfd | 3149 | /* Check for a chip array */ |
e0c7d767 | 3150 | for (i = 1; i < maxchips; i++) { |
ace4dfee | 3151 | chip->select_chip(mtd, i); |
ef89a880 KB |
3152 | /* See comment in nand_get_flash_type for reset */ |
3153 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); | |
1da177e4 | 3154 | /* Send the command for reading device ID */ |
ace4dfee | 3155 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
1da177e4 | 3156 | /* Read manufacturer and device IDs */ |
ace4dfee | 3157 | if (nand_maf_id != chip->read_byte(mtd) || |
d1e1f4e4 | 3158 | nand_dev_id != chip->read_byte(mtd)) |
1da177e4 LT |
3159 | break; |
3160 | } | |
3161 | if (i > 1) | |
9a4d4d69 | 3162 | pr_info("%d NAND chips detected\n", i); |
61b03bd7 | 3163 | |
1da177e4 | 3164 | /* Store the number of chips and calc total size for mtd */ |
ace4dfee TG |
3165 | chip->numchips = i; |
3166 | mtd->size = i * chip->chipsize; | |
7aa65bfd | 3167 | |
3b85c321 DW |
3168 | return 0; |
3169 | } | |
7351d3a5 | 3170 | EXPORT_SYMBOL(nand_scan_ident); |
3b85c321 DW |
3171 | |
3172 | ||
3173 | /** | |
3174 | * nand_scan_tail - [NAND Interface] Scan for the NAND device | |
8b6e50c9 | 3175 | * @mtd: MTD device structure |
3b85c321 | 3176 | * |
8b6e50c9 BN |
3177 | * This is the second phase of the normal nand_scan() function. It fills out |
3178 | * all the uninitialized function pointers with the defaults and scans for a | |
3179 | * bad block table if appropriate. | |
3b85c321 DW |
3180 | */ |
3181 | int nand_scan_tail(struct mtd_info *mtd) | |
3182 | { | |
3183 | int i; | |
3184 | struct nand_chip *chip = mtd->priv; | |
3185 | ||
e2414f4c BN |
3186 | /* New bad blocks should be marked in OOB, flash-based BBT, or both */ |
3187 | BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) && | |
3188 | !(chip->bbt_options & NAND_BBT_USE_FLASH)); | |
3189 | ||
4bf63fcb DW |
3190 | if (!(chip->options & NAND_OWN_BUFFERS)) |
3191 | chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL); | |
3192 | if (!chip->buffers) | |
3193 | return -ENOMEM; | |
3194 | ||
7dcdcbef | 3195 | /* Set the internal oob buffer location, just after the page data */ |
784f4d5e | 3196 | chip->oob_poi = chip->buffers->databuf + mtd->writesize; |
1da177e4 | 3197 | |
7aa65bfd | 3198 | /* |
8b6e50c9 | 3199 | * If no default placement scheme is given, select an appropriate one. |
7aa65bfd | 3200 | */ |
193bd400 | 3201 | if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) { |
61b03bd7 | 3202 | switch (mtd->oobsize) { |
1da177e4 | 3203 | case 8: |
5bd34c09 | 3204 | chip->ecc.layout = &nand_oob_8; |
1da177e4 LT |
3205 | break; |
3206 | case 16: | |
5bd34c09 | 3207 | chip->ecc.layout = &nand_oob_16; |
1da177e4 LT |
3208 | break; |
3209 | case 64: | |
5bd34c09 | 3210 | chip->ecc.layout = &nand_oob_64; |
1da177e4 | 3211 | break; |
81ec5364 TG |
3212 | case 128: |
3213 | chip->ecc.layout = &nand_oob_128; | |
3214 | break; | |
1da177e4 | 3215 | default: |
d0370219 BN |
3216 | pr_warn("No oob scheme defined for oobsize %d\n", |
3217 | mtd->oobsize); | |
1da177e4 LT |
3218 | BUG(); |
3219 | } | |
3220 | } | |
61b03bd7 | 3221 | |
956e944c DW |
3222 | if (!chip->write_page) |
3223 | chip->write_page = nand_write_page; | |
3224 | ||
61b03bd7 | 3225 | /* |
8b6e50c9 | 3226 | * Check ECC mode, default to software if 3byte/512byte hardware ECC is |
7aa65bfd | 3227 | * selected and we have 256 byte pagesize fallback to software ECC |
e0c7d767 | 3228 | */ |
956e944c | 3229 | |
ace4dfee | 3230 | switch (chip->ecc.mode) { |
6e0cb135 SN |
3231 | case NAND_ECC_HW_OOB_FIRST: |
3232 | /* Similar to NAND_ECC_HW, but a separate read_page handle */ | |
3233 | if (!chip->ecc.calculate || !chip->ecc.correct || | |
3234 | !chip->ecc.hwctl) { | |
9a4d4d69 | 3235 | pr_warn("No ECC functions supplied; " |
d0370219 | 3236 | "hardware ECC not possible\n"); |
6e0cb135 SN |
3237 | BUG(); |
3238 | } | |
3239 | if (!chip->ecc.read_page) | |
3240 | chip->ecc.read_page = nand_read_page_hwecc_oob_first; | |
3241 | ||
6dfc6d25 | 3242 | case NAND_ECC_HW: |
8b6e50c9 | 3243 | /* Use standard hwecc read page function? */ |
f5bbdacc TG |
3244 | if (!chip->ecc.read_page) |
3245 | chip->ecc.read_page = nand_read_page_hwecc; | |
f75e5097 TG |
3246 | if (!chip->ecc.write_page) |
3247 | chip->ecc.write_page = nand_write_page_hwecc; | |
52ff49df DB |
3248 | if (!chip->ecc.read_page_raw) |
3249 | chip->ecc.read_page_raw = nand_read_page_raw; | |
3250 | if (!chip->ecc.write_page_raw) | |
3251 | chip->ecc.write_page_raw = nand_write_page_raw; | |
7bc3312b TG |
3252 | if (!chip->ecc.read_oob) |
3253 | chip->ecc.read_oob = nand_read_oob_std; | |
3254 | if (!chip->ecc.write_oob) | |
3255 | chip->ecc.write_oob = nand_write_oob_std; | |
f5bbdacc | 3256 | |
6dfc6d25 | 3257 | case NAND_ECC_HW_SYNDROME: |
78b65179 SW |
3258 | if ((!chip->ecc.calculate || !chip->ecc.correct || |
3259 | !chip->ecc.hwctl) && | |
3260 | (!chip->ecc.read_page || | |
1c45f604 | 3261 | chip->ecc.read_page == nand_read_page_hwecc || |
78b65179 | 3262 | !chip->ecc.write_page || |
1c45f604 | 3263 | chip->ecc.write_page == nand_write_page_hwecc)) { |
9a4d4d69 | 3264 | pr_warn("No ECC functions supplied; " |
d0370219 | 3265 | "hardware ECC not possible\n"); |
6dfc6d25 TG |
3266 | BUG(); |
3267 | } | |
8b6e50c9 | 3268 | /* Use standard syndrome read/write page function? */ |
f5bbdacc TG |
3269 | if (!chip->ecc.read_page) |
3270 | chip->ecc.read_page = nand_read_page_syndrome; | |
f75e5097 TG |
3271 | if (!chip->ecc.write_page) |
3272 | chip->ecc.write_page = nand_write_page_syndrome; | |
52ff49df DB |
3273 | if (!chip->ecc.read_page_raw) |
3274 | chip->ecc.read_page_raw = nand_read_page_raw_syndrome; | |
3275 | if (!chip->ecc.write_page_raw) | |
3276 | chip->ecc.write_page_raw = nand_write_page_raw_syndrome; | |
7bc3312b TG |
3277 | if (!chip->ecc.read_oob) |
3278 | chip->ecc.read_oob = nand_read_oob_syndrome; | |
3279 | if (!chip->ecc.write_oob) | |
3280 | chip->ecc.write_oob = nand_write_oob_syndrome; | |
f5bbdacc | 3281 | |
e2788c98 MD |
3282 | if (mtd->writesize >= chip->ecc.size) { |
3283 | if (!chip->ecc.strength) { | |
3284 | pr_warn("Driver must set ecc.strength when using hardware ECC\n"); | |
3285 | BUG(); | |
3286 | } | |
6dfc6d25 | 3287 | break; |
e2788c98 | 3288 | } |
9a4d4d69 | 3289 | pr_warn("%d byte HW ECC not possible on " |
d0370219 BN |
3290 | "%d byte page size, fallback to SW ECC\n", |
3291 | chip->ecc.size, mtd->writesize); | |
ace4dfee | 3292 | chip->ecc.mode = NAND_ECC_SOFT; |
61b03bd7 | 3293 | |
6dfc6d25 | 3294 | case NAND_ECC_SOFT: |
ace4dfee TG |
3295 | chip->ecc.calculate = nand_calculate_ecc; |
3296 | chip->ecc.correct = nand_correct_data; | |
f5bbdacc | 3297 | chip->ecc.read_page = nand_read_page_swecc; |
3d459559 | 3298 | chip->ecc.read_subpage = nand_read_subpage; |
f75e5097 | 3299 | chip->ecc.write_page = nand_write_page_swecc; |
52ff49df DB |
3300 | chip->ecc.read_page_raw = nand_read_page_raw; |
3301 | chip->ecc.write_page_raw = nand_write_page_raw; | |
7bc3312b TG |
3302 | chip->ecc.read_oob = nand_read_oob_std; |
3303 | chip->ecc.write_oob = nand_write_oob_std; | |
9a73290d SV |
3304 | if (!chip->ecc.size) |
3305 | chip->ecc.size = 256; | |
ace4dfee | 3306 | chip->ecc.bytes = 3; |
6a918bad | 3307 | chip->ecc.strength = 1; |
1da177e4 | 3308 | break; |
61b03bd7 | 3309 | |
193bd400 ID |
3310 | case NAND_ECC_SOFT_BCH: |
3311 | if (!mtd_nand_has_bch()) { | |
9a4d4d69 | 3312 | pr_warn("CONFIG_MTD_ECC_BCH not enabled\n"); |
193bd400 ID |
3313 | BUG(); |
3314 | } | |
3315 | chip->ecc.calculate = nand_bch_calculate_ecc; | |
3316 | chip->ecc.correct = nand_bch_correct_data; | |
3317 | chip->ecc.read_page = nand_read_page_swecc; | |
3318 | chip->ecc.read_subpage = nand_read_subpage; | |
3319 | chip->ecc.write_page = nand_write_page_swecc; | |
3320 | chip->ecc.read_page_raw = nand_read_page_raw; | |
3321 | chip->ecc.write_page_raw = nand_write_page_raw; | |
3322 | chip->ecc.read_oob = nand_read_oob_std; | |
3323 | chip->ecc.write_oob = nand_write_oob_std; | |
3324 | /* | |
3325 | * Board driver should supply ecc.size and ecc.bytes values to | |
3326 | * select how many bits are correctable; see nand_bch_init() | |
8b6e50c9 BN |
3327 | * for details. Otherwise, default to 4 bits for large page |
3328 | * devices. | |
193bd400 ID |
3329 | */ |
3330 | if (!chip->ecc.size && (mtd->oobsize >= 64)) { | |
3331 | chip->ecc.size = 512; | |
3332 | chip->ecc.bytes = 7; | |
3333 | } | |
3334 | chip->ecc.priv = nand_bch_init(mtd, | |
3335 | chip->ecc.size, | |
3336 | chip->ecc.bytes, | |
3337 | &chip->ecc.layout); | |
3338 | if (!chip->ecc.priv) { | |
9a4d4d69 | 3339 | pr_warn("BCH ECC initialization failed!\n"); |
193bd400 ID |
3340 | BUG(); |
3341 | } | |
6a918bad | 3342 | chip->ecc.strength = |
e2788c98 | 3343 | chip->ecc.bytes * 8 / fls(8 * chip->ecc.size); |
193bd400 ID |
3344 | break; |
3345 | ||
61b03bd7 | 3346 | case NAND_ECC_NONE: |
9a4d4d69 | 3347 | pr_warn("NAND_ECC_NONE selected by board driver. " |
d0370219 | 3348 | "This is not recommended!\n"); |
8593fbc6 TG |
3349 | chip->ecc.read_page = nand_read_page_raw; |
3350 | chip->ecc.write_page = nand_write_page_raw; | |
7bc3312b | 3351 | chip->ecc.read_oob = nand_read_oob_std; |
52ff49df DB |
3352 | chip->ecc.read_page_raw = nand_read_page_raw; |
3353 | chip->ecc.write_page_raw = nand_write_page_raw; | |
7bc3312b | 3354 | chip->ecc.write_oob = nand_write_oob_std; |
ace4dfee TG |
3355 | chip->ecc.size = mtd->writesize; |
3356 | chip->ecc.bytes = 0; | |
6a918bad | 3357 | chip->ecc.strength = 0; |
1da177e4 | 3358 | break; |
956e944c | 3359 | |
1da177e4 | 3360 | default: |
d0370219 | 3361 | pr_warn("Invalid NAND_ECC_MODE %d\n", chip->ecc.mode); |
61b03bd7 | 3362 | BUG(); |
1da177e4 | 3363 | } |
61b03bd7 | 3364 | |
9ce244b3 | 3365 | /* For many systems, the standard OOB write also works for raw */ |
c46f6483 BN |
3366 | if (!chip->ecc.read_oob_raw) |
3367 | chip->ecc.read_oob_raw = chip->ecc.read_oob; | |
9ce244b3 BN |
3368 | if (!chip->ecc.write_oob_raw) |
3369 | chip->ecc.write_oob_raw = chip->ecc.write_oob; | |
3370 | ||
5bd34c09 TG |
3371 | /* |
3372 | * The number of bytes available for a client to place data into | |
8b6e50c9 | 3373 | * the out of band area. |
5bd34c09 TG |
3374 | */ |
3375 | chip->ecc.layout->oobavail = 0; | |
81d19b04 DB |
3376 | for (i = 0; chip->ecc.layout->oobfree[i].length |
3377 | && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++) | |
5bd34c09 TG |
3378 | chip->ecc.layout->oobavail += |
3379 | chip->ecc.layout->oobfree[i].length; | |
1f92267c | 3380 | mtd->oobavail = chip->ecc.layout->oobavail; |
5bd34c09 | 3381 | |
7aa65bfd TG |
3382 | /* |
3383 | * Set the number of read / write steps for one page depending on ECC | |
8b6e50c9 | 3384 | * mode. |
7aa65bfd | 3385 | */ |
ace4dfee | 3386 | chip->ecc.steps = mtd->writesize / chip->ecc.size; |
f8ac0414 | 3387 | if (chip->ecc.steps * chip->ecc.size != mtd->writesize) { |
9a4d4d69 | 3388 | pr_warn("Invalid ECC parameters\n"); |
6dfc6d25 | 3389 | BUG(); |
1da177e4 | 3390 | } |
f5bbdacc | 3391 | chip->ecc.total = chip->ecc.steps * chip->ecc.bytes; |
61b03bd7 | 3392 | |
8b6e50c9 | 3393 | /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */ |
29072b96 TG |
3394 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && |
3395 | !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) { | |
f8ac0414 | 3396 | switch (chip->ecc.steps) { |
29072b96 TG |
3397 | case 2: |
3398 | mtd->subpage_sft = 1; | |
3399 | break; | |
3400 | case 4: | |
3401 | case 8: | |
81ec5364 | 3402 | case 16: |
29072b96 TG |
3403 | mtd->subpage_sft = 2; |
3404 | break; | |
3405 | } | |
3406 | } | |
3407 | chip->subpagesize = mtd->writesize >> mtd->subpage_sft; | |
3408 | ||
04bbd0ea | 3409 | /* Initialize state */ |
ace4dfee | 3410 | chip->state = FL_READY; |
1da177e4 LT |
3411 | |
3412 | /* De-select the device */ | |
ace4dfee | 3413 | chip->select_chip(mtd, -1); |
1da177e4 LT |
3414 | |
3415 | /* Invalidate the pagebuffer reference */ | |
ace4dfee | 3416 | chip->pagebuf = -1; |
1da177e4 LT |
3417 | |
3418 | /* Fill in remaining MTD driver data */ | |
3419 | mtd->type = MTD_NANDFLASH; | |
93edbad6 ML |
3420 | mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM : |
3421 | MTD_CAP_NANDFLASH; | |
3c3c10bb AB |
3422 | mtd->_erase = nand_erase; |
3423 | mtd->_point = NULL; | |
3424 | mtd->_unpoint = NULL; | |
3425 | mtd->_read = nand_read; | |
3426 | mtd->_write = nand_write; | |
3427 | mtd->_panic_write = panic_nand_write; | |
3428 | mtd->_read_oob = nand_read_oob; | |
3429 | mtd->_write_oob = nand_write_oob; | |
3430 | mtd->_sync = nand_sync; | |
3431 | mtd->_lock = NULL; | |
3432 | mtd->_unlock = NULL; | |
3433 | mtd->_suspend = nand_suspend; | |
3434 | mtd->_resume = nand_resume; | |
3435 | mtd->_block_isbad = nand_block_isbad; | |
3436 | mtd->_block_markbad = nand_block_markbad; | |
cbcab65a | 3437 | mtd->writebufsize = mtd->writesize; |
1da177e4 | 3438 | |
6a918bad | 3439 | /* propagate ecc info to mtd_info */ |
5bd34c09 | 3440 | mtd->ecclayout = chip->ecc.layout; |
86c2072b | 3441 | mtd->ecc_strength = chip->ecc.strength; |
ea3b2ea2 SL |
3442 | /* |
3443 | * Initialize bitflip_threshold to its default prior scan_bbt() call. | |
3444 | * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be | |
3445 | * properly set. | |
3446 | */ | |
3447 | if (!mtd->bitflip_threshold) | |
3448 | mtd->bitflip_threshold = mtd->ecc_strength; | |
1da177e4 | 3449 | |
0040bf38 | 3450 | /* Check, if we should skip the bad block table scan */ |
ace4dfee | 3451 | if (chip->options & NAND_SKIP_BBTSCAN) |
0040bf38 | 3452 | return 0; |
1da177e4 LT |
3453 | |
3454 | /* Build bad block table */ | |
ace4dfee | 3455 | return chip->scan_bbt(mtd); |
1da177e4 | 3456 | } |
7351d3a5 | 3457 | EXPORT_SYMBOL(nand_scan_tail); |
1da177e4 | 3458 | |
8b6e50c9 BN |
3459 | /* |
3460 | * is_module_text_address() isn't exported, and it's mostly a pointless | |
7351d3a5 | 3461 | * test if this is a module _anyway_ -- they'd have to try _really_ hard |
8b6e50c9 BN |
3462 | * to call us from in-kernel code if the core NAND support is modular. |
3463 | */ | |
3b85c321 DW |
3464 | #ifdef MODULE |
3465 | #define caller_is_module() (1) | |
3466 | #else | |
3467 | #define caller_is_module() \ | |
a6e6abd5 | 3468 | is_module_text_address((unsigned long)__builtin_return_address(0)) |
3b85c321 DW |
3469 | #endif |
3470 | ||
3471 | /** | |
3472 | * nand_scan - [NAND Interface] Scan for the NAND device | |
8b6e50c9 BN |
3473 | * @mtd: MTD device structure |
3474 | * @maxchips: number of chips to scan for | |
3b85c321 | 3475 | * |
8b6e50c9 BN |
3476 | * This fills out all the uninitialized function pointers with the defaults. |
3477 | * The flash ID is read and the mtd/chip structures are filled with the | |
3478 | * appropriate values. The mtd->owner field must be set to the module of the | |
3479 | * caller. | |
3b85c321 DW |
3480 | */ |
3481 | int nand_scan(struct mtd_info *mtd, int maxchips) | |
3482 | { | |
3483 | int ret; | |
3484 | ||
3485 | /* Many callers got this wrong, so check for it for a while... */ | |
3486 | if (!mtd->owner && caller_is_module()) { | |
d0370219 | 3487 | pr_crit("%s called with NULL mtd->owner!\n", __func__); |
3b85c321 DW |
3488 | BUG(); |
3489 | } | |
3490 | ||
5e81e88a | 3491 | ret = nand_scan_ident(mtd, maxchips, NULL); |
3b85c321 DW |
3492 | if (!ret) |
3493 | ret = nand_scan_tail(mtd); | |
3494 | return ret; | |
3495 | } | |
7351d3a5 | 3496 | EXPORT_SYMBOL(nand_scan); |
3b85c321 | 3497 | |
1da177e4 | 3498 | /** |
61b03bd7 | 3499 | * nand_release - [NAND Interface] Free resources held by the NAND device |
8b6e50c9 BN |
3500 | * @mtd: MTD device structure |
3501 | */ | |
e0c7d767 | 3502 | void nand_release(struct mtd_info *mtd) |
1da177e4 | 3503 | { |
ace4dfee | 3504 | struct nand_chip *chip = mtd->priv; |
1da177e4 | 3505 | |
193bd400 ID |
3506 | if (chip->ecc.mode == NAND_ECC_SOFT_BCH) |
3507 | nand_bch_free((struct nand_bch_control *)chip->ecc.priv); | |
3508 | ||
5ffcaf3d | 3509 | mtd_device_unregister(mtd); |
1da177e4 | 3510 | |
fa671646 | 3511 | /* Free bad block table memory */ |
ace4dfee | 3512 | kfree(chip->bbt); |
4bf63fcb DW |
3513 | if (!(chip->options & NAND_OWN_BUFFERS)) |
3514 | kfree(chip->buffers); | |
58373ff0 BN |
3515 | |
3516 | /* Free bad block descriptor memory */ | |
3517 | if (chip->badblock_pattern && chip->badblock_pattern->options | |
3518 | & NAND_BBT_DYNAMICSTRUCT) | |
3519 | kfree(chip->badblock_pattern); | |
1da177e4 | 3520 | } |
e0c7d767 | 3521 | EXPORT_SYMBOL_GPL(nand_release); |
8fe833c1 RP |
3522 | |
3523 | static int __init nand_base_init(void) | |
3524 | { | |
3525 | led_trigger_register_simple("nand-disk", &nand_led_trigger); | |
3526 | return 0; | |
3527 | } | |
3528 | ||
3529 | static void __exit nand_base_exit(void) | |
3530 | { | |
3531 | led_trigger_unregister_simple(nand_led_trigger); | |
3532 | } | |
3533 | ||
3534 | module_init(nand_base_init); | |
3535 | module_exit(nand_base_exit); | |
3536 | ||
e0c7d767 | 3537 | MODULE_LICENSE("GPL"); |
7351d3a5 FF |
3538 | MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>"); |
3539 | MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>"); | |
e0c7d767 | 3540 | MODULE_DESCRIPTION("Generic NAND flash driver code"); |