hc
2024-01-05 071106ecf68c401173c58808b1cf5f68cc50d390
kernel/drivers/mtd/nand/raw/nand_base.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Overview:
34 * This is the generic MTD driver for NAND flash devices. It should be
....@@ -20,11 +21,6 @@
2021 * Check, if mtd->ecctype should be set to MTD_ECC_HW
2122 * if we have HW ECC support.
2223 * BBT table is not serialized, has to be fixed
23
- *
24
- * This program is free software; you can redistribute it and/or modify
25
- * it under the terms of the GNU General Public License version 2 as
26
- * published by the Free Software Foundation.
27
- *
2824 */
2925
3026 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
....@@ -36,10 +32,9 @@
3632 #include <linux/sched.h>
3733 #include <linux/slab.h>
3834 #include <linux/mm.h>
39
-#include <linux/nmi.h>
4035 #include <linux/types.h>
4136 #include <linux/mtd/mtd.h>
42
-#include <linux/mtd/rawnand.h>
37
+#include <linux/mtd/nand.h>
4338 #include <linux/mtd/nand_ecc.h>
4439 #include <linux/mtd/nand_bch.h>
4540 #include <linux/interrupt.h>
....@@ -47,176 +42,62 @@
4742 #include <linux/io.h>
4843 #include <linux/mtd/partitions.h>
4944 #include <linux/of.h>
45
+#include <linux/gpio/consumer.h>
5046
51
-static int nand_get_device(struct mtd_info *mtd, int new_state);
47
+#include "internals.h"
5248
53
-static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
54
- struct mtd_oob_ops *ops);
55
-
56
-/* Define default oob placement schemes for large and small page devices */
57
-static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
58
- struct mtd_oob_region *oobregion)
49
+static int nand_pairing_dist3_get_info(struct mtd_info *mtd, int page,
50
+ struct mtd_pairing_info *info)
5951 {
60
- struct nand_chip *chip = mtd_to_nand(mtd);
61
- struct nand_ecc_ctrl *ecc = &chip->ecc;
52
+ int lastpage = (mtd->erasesize / mtd->writesize) - 1;
53
+ int dist = 3;
6254
63
- if (section > 1)
64
- return -ERANGE;
55
+ if (page == lastpage)
56
+ dist = 2;
6557
66
- if (!section) {
67
- oobregion->offset = 0;
68
- if (mtd->oobsize == 16)
69
- oobregion->length = 4;
70
- else
71
- oobregion->length = 3;
58
+ if (!page || (page & 1)) {
59
+ info->group = 0;
60
+ info->pair = (page + 1) / 2;
7261 } else {
73
- if (mtd->oobsize == 8)
74
- return -ERANGE;
75
-
76
- oobregion->offset = 6;
77
- oobregion->length = ecc->total - 4;
62
+ info->group = 1;
63
+ info->pair = (page + 1 - dist) / 2;
7864 }
7965
8066 return 0;
8167 }
8268
83
-static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
84
- struct mtd_oob_region *oobregion)
69
+static int nand_pairing_dist3_get_wunit(struct mtd_info *mtd,
70
+ const struct mtd_pairing_info *info)
8571 {
86
- if (section > 1)
87
- return -ERANGE;
72
+ int lastpair = ((mtd->erasesize / mtd->writesize) - 1) / 2;
73
+ int page = info->pair * 2;
74
+ int dist = 3;
8875
89
- if (mtd->oobsize == 16) {
90
- if (section)
91
- return -ERANGE;
76
+ if (!info->group && !info->pair)
77
+ return 0;
9278
93
- oobregion->length = 8;
94
- oobregion->offset = 8;
95
- } else {
96
- oobregion->length = 2;
97
- if (!section)
98
- oobregion->offset = 3;
99
- else
100
- oobregion->offset = 6;
101
- }
79
+ if (info->pair == lastpair && info->group)
80
+ dist = 2;
10281
103
- return 0;
104
-}
82
+ if (!info->group)
83
+ page--;
84
+ else if (info->pair)
85
+ page += dist - 1;
10586
106
-const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
107
- .ecc = nand_ooblayout_ecc_sp,
108
- .free = nand_ooblayout_free_sp,
109
-};
110
-EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
111
-
112
-static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
113
- struct mtd_oob_region *oobregion)
114
-{
115
- struct nand_chip *chip = mtd_to_nand(mtd);
116
- struct nand_ecc_ctrl *ecc = &chip->ecc;
117
-
118
- if (section || !ecc->total)
119
- return -ERANGE;
120
-
121
- oobregion->length = ecc->total;
122
- oobregion->offset = mtd->oobsize - oobregion->length;
123
-
124
- return 0;
125
-}
126
-
127
-static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
128
- struct mtd_oob_region *oobregion)
129
-{
130
- struct nand_chip *chip = mtd_to_nand(mtd);
131
- struct nand_ecc_ctrl *ecc = &chip->ecc;
132
-
133
- if (section)
134
- return -ERANGE;
135
-
136
- oobregion->length = mtd->oobsize - ecc->total - 2;
137
- oobregion->offset = 2;
138
-
139
- return 0;
140
-}
141
-
142
-const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
143
- .ecc = nand_ooblayout_ecc_lp,
144
- .free = nand_ooblayout_free_lp,
145
-};
146
-EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
147
-
148
-/*
149
- * Support the old "large page" layout used for 1-bit Hamming ECC where ECC
150
- * are placed at a fixed offset.
151
- */
152
-static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section,
153
- struct mtd_oob_region *oobregion)
154
-{
155
- struct nand_chip *chip = mtd_to_nand(mtd);
156
- struct nand_ecc_ctrl *ecc = &chip->ecc;
157
-
158
- if (section)
159
- return -ERANGE;
160
-
161
- switch (mtd->oobsize) {
162
- case 64:
163
- oobregion->offset = 40;
164
- break;
165
- case 128:
166
- oobregion->offset = 80;
167
- break;
168
- default:
87
+ if (page >= mtd->erasesize / mtd->writesize)
16988 return -EINVAL;
170
- }
17189
172
- oobregion->length = ecc->total;
173
- if (oobregion->offset + oobregion->length > mtd->oobsize)
174
- return -ERANGE;
175
-
176
- return 0;
90
+ return page;
17791 }
17892
179
-static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section,
180
- struct mtd_oob_region *oobregion)
181
-{
182
- struct nand_chip *chip = mtd_to_nand(mtd);
183
- struct nand_ecc_ctrl *ecc = &chip->ecc;
184
- int ecc_offset = 0;
185
-
186
- if (section < 0 || section > 1)
187
- return -ERANGE;
188
-
189
- switch (mtd->oobsize) {
190
- case 64:
191
- ecc_offset = 40;
192
- break;
193
- case 128:
194
- ecc_offset = 80;
195
- break;
196
- default:
197
- return -EINVAL;
198
- }
199
-
200
- if (section == 0) {
201
- oobregion->offset = 2;
202
- oobregion->length = ecc_offset - 2;
203
- } else {
204
- oobregion->offset = ecc_offset + ecc->total;
205
- oobregion->length = mtd->oobsize - oobregion->offset;
206
- }
207
-
208
- return 0;
209
-}
210
-
211
-static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
212
- .ecc = nand_ooblayout_ecc_lp_hamming,
213
- .free = nand_ooblayout_free_lp_hamming,
93
+const struct mtd_pairing_scheme dist3_pairing_scheme = {
94
+ .ngroups = 2,
95
+ .get_info = nand_pairing_dist3_get_info,
96
+ .get_wunit = nand_pairing_dist3_get_wunit,
21497 };
21598
216
-static int check_offs_len(struct mtd_info *mtd,
217
- loff_t ofs, uint64_t len)
99
+static int check_offs_len(struct nand_chip *chip, loff_t ofs, uint64_t len)
218100 {
219
- struct nand_chip *chip = mtd_to_nand(mtd);
220101 int ret = 0;
221102
222103 /* Start address must align on block boundary */
....@@ -235,211 +116,150 @@
235116 }
236117
237118 /**
119
+ * nand_extract_bits - Copy unaligned bits from one buffer to another one
120
+ * @dst: destination buffer
121
+ * @dst_off: bit offset at which the writing starts
122
+ * @src: source buffer
123
+ * @src_off: bit offset at which the reading starts
124
+ * @nbits: number of bits to copy from @src to @dst
125
+ *
126
+ * Copy bits from one memory region to another (overlap authorized).
127
+ */
128
+void nand_extract_bits(u8 *dst, unsigned int dst_off, const u8 *src,
129
+ unsigned int src_off, unsigned int nbits)
130
+{
131
+ unsigned int tmp, n;
132
+
133
+ dst += dst_off / 8;
134
+ dst_off %= 8;
135
+ src += src_off / 8;
136
+ src_off %= 8;
137
+
138
+ while (nbits) {
139
+ n = min3(8 - dst_off, 8 - src_off, nbits);
140
+
141
+ tmp = (*src >> src_off) & GENMASK(n - 1, 0);
142
+ *dst &= ~GENMASK(n - 1 + dst_off, dst_off);
143
+ *dst |= tmp << dst_off;
144
+
145
+ dst_off += n;
146
+ if (dst_off >= 8) {
147
+ dst++;
148
+ dst_off -= 8;
149
+ }
150
+
151
+ src_off += n;
152
+ if (src_off >= 8) {
153
+ src++;
154
+ src_off -= 8;
155
+ }
156
+
157
+ nbits -= n;
158
+ }
159
+}
160
+EXPORT_SYMBOL_GPL(nand_extract_bits);
161
+
162
+/**
163
+ * nand_select_target() - Select a NAND target (A.K.A. die)
164
+ * @chip: NAND chip object
165
+ * @cs: the CS line to select. Note that this CS id is always from the chip
166
+ * PoV, not the controller one
167
+ *
168
+ * Select a NAND target so that further operations executed on @chip go to the
169
+ * selected NAND target.
170
+ */
171
+void nand_select_target(struct nand_chip *chip, unsigned int cs)
172
+{
173
+ /*
174
+ * cs should always lie between 0 and nanddev_ntargets(), when that's
175
+ * not the case it's a bug and the caller should be fixed.
176
+ */
177
+ if (WARN_ON(cs > nanddev_ntargets(&chip->base)))
178
+ return;
179
+
180
+ chip->cur_cs = cs;
181
+
182
+ if (chip->legacy.select_chip)
183
+ chip->legacy.select_chip(chip, cs);
184
+}
185
+EXPORT_SYMBOL_GPL(nand_select_target);
186
+
187
+/**
188
+ * nand_deselect_target() - Deselect the currently selected target
189
+ * @chip: NAND chip object
190
+ *
191
+ * Deselect the currently selected NAND target. The result of operations
192
+ * executed on @chip after the target has been deselected is undefined.
193
+ */
194
+void nand_deselect_target(struct nand_chip *chip)
195
+{
196
+ if (chip->legacy.select_chip)
197
+ chip->legacy.select_chip(chip, -1);
198
+
199
+ chip->cur_cs = -1;
200
+}
201
+EXPORT_SYMBOL_GPL(nand_deselect_target);
202
+
203
+/**
238204 * nand_release_device - [GENERIC] release chip
239
- * @mtd: MTD device structure
205
+ * @chip: NAND chip object
240206 *
241207 * Release chip lock and wake up anyone waiting on the device.
242208 */
243
-static void nand_release_device(struct mtd_info *mtd)
209
+static void nand_release_device(struct nand_chip *chip)
244210 {
245
- struct nand_chip *chip = mtd_to_nand(mtd);
246
-
247211 /* Release the controller and the chip */
248
- spin_lock(&chip->controller->lock);
249
- chip->controller->active = NULL;
250
- chip->state = FL_READY;
251
- wake_up(&chip->controller->wq);
252
- spin_unlock(&chip->controller->lock);
212
+ mutex_unlock(&chip->controller->lock);
213
+ mutex_unlock(&chip->lock);
253214 }
254215
255216 /**
256
- * nand_read_byte - [DEFAULT] read one byte from the chip
257
- * @mtd: MTD device structure
217
+ * nand_bbm_get_next_page - Get the next page for bad block markers
218
+ * @chip: NAND chip object
219
+ * @page: First page to start checking for bad block marker usage
258220 *
259
- * Default read function for 8bit buswidth
221
+ * Returns an integer that corresponds to the page offset within a block, for
222
+ * a page that is used to store bad block markers. If no more pages are
223
+ * available, -EINVAL is returned.
260224 */
261
-static uint8_t nand_read_byte(struct mtd_info *mtd)
225
+int nand_bbm_get_next_page(struct nand_chip *chip, int page)
262226 {
263
- struct nand_chip *chip = mtd_to_nand(mtd);
264
- return readb(chip->IO_ADDR_R);
265
-}
227
+ struct mtd_info *mtd = nand_to_mtd(chip);
228
+ int last_page = ((mtd->erasesize - mtd->writesize) >>
229
+ chip->page_shift) & chip->pagemask;
230
+ unsigned int bbm_flags = NAND_BBM_FIRSTPAGE | NAND_BBM_SECONDPAGE
231
+ | NAND_BBM_LASTPAGE;
266232
267
-/**
268
- * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
269
- * @mtd: MTD device structure
270
- *
271
- * Default read function for 16bit buswidth with endianness conversion.
272
- *
273
- */
274
-static uint8_t nand_read_byte16(struct mtd_info *mtd)
275
-{
276
- struct nand_chip *chip = mtd_to_nand(mtd);
277
- return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
278
-}
233
+ if (page == 0 && !(chip->options & bbm_flags))
234
+ return 0;
235
+ if (page == 0 && chip->options & NAND_BBM_FIRSTPAGE)
236
+ return 0;
237
+ if (page <= 1 && chip->options & NAND_BBM_SECONDPAGE)
238
+ return 1;
239
+ if (page <= last_page && chip->options & NAND_BBM_LASTPAGE)
240
+ return last_page;
279241
280
-/**
281
- * nand_read_word - [DEFAULT] read one word from the chip
282
- * @mtd: MTD device structure
283
- *
284
- * Default read function for 16bit buswidth without endianness conversion.
285
- */
286
-static u16 nand_read_word(struct mtd_info *mtd)
287
-{
288
- struct nand_chip *chip = mtd_to_nand(mtd);
289
- return readw(chip->IO_ADDR_R);
290
-}
291
-
292
-/**
293
- * nand_select_chip - [DEFAULT] control CE line
294
- * @mtd: MTD device structure
295
- * @chipnr: chipnumber to select, -1 for deselect
296
- *
297
- * Default select function for 1 chip devices.
298
- */
299
-static void nand_select_chip(struct mtd_info *mtd, int chipnr)
300
-{
301
- struct nand_chip *chip = mtd_to_nand(mtd);
302
-
303
- switch (chipnr) {
304
- case -1:
305
- chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
306
- break;
307
- case 0:
308
- break;
309
-
310
- default:
311
- BUG();
312
- }
313
-}
314
-
315
-/**
316
- * nand_write_byte - [DEFAULT] write single byte to chip
317
- * @mtd: MTD device structure
318
- * @byte: value to write
319
- *
320
- * Default function to write a byte to I/O[7:0]
321
- */
322
-static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
323
-{
324
- struct nand_chip *chip = mtd_to_nand(mtd);
325
-
326
- chip->write_buf(mtd, &byte, 1);
327
-}
328
-
329
-/**
330
- * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
331
- * @mtd: MTD device structure
332
- * @byte: value to write
333
- *
334
- * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
335
- */
336
-static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
337
-{
338
- struct nand_chip *chip = mtd_to_nand(mtd);
339
- uint16_t word = byte;
340
-
341
- /*
342
- * It's not entirely clear what should happen to I/O[15:8] when writing
343
- * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
344
- *
345
- * When the host supports a 16-bit bus width, only data is
346
- * transferred at the 16-bit width. All address and command line
347
- * transfers shall use only the lower 8-bits of the data bus. During
348
- * command transfers, the host may place any value on the upper
349
- * 8-bits of the data bus. During address transfers, the host shall
350
- * set the upper 8-bits of the data bus to 00h.
351
- *
352
- * One user of the write_byte callback is nand_set_features. The
353
- * four parameters are specified to be written to I/O[7:0], but this is
354
- * neither an address nor a command transfer. Let's assume a 0 on the
355
- * upper I/O lines is OK.
356
- */
357
- chip->write_buf(mtd, (uint8_t *)&word, 2);
358
-}
359
-
360
-/**
361
- * nand_write_buf - [DEFAULT] write buffer to chip
362
- * @mtd: MTD device structure
363
- * @buf: data buffer
364
- * @len: number of bytes to write
365
- *
366
- * Default write function for 8bit buswidth.
367
- */
368
-static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
369
-{
370
- struct nand_chip *chip = mtd_to_nand(mtd);
371
-
372
- iowrite8_rep(chip->IO_ADDR_W, buf, len);
373
-}
374
-
375
-/**
376
- * nand_read_buf - [DEFAULT] read chip data into buffer
377
- * @mtd: MTD device structure
378
- * @buf: buffer to store date
379
- * @len: number of bytes to read
380
- *
381
- * Default read function for 8bit buswidth.
382
- */
383
-static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
384
-{
385
- struct nand_chip *chip = mtd_to_nand(mtd);
386
-
387
- ioread8_rep(chip->IO_ADDR_R, buf, len);
388
-}
389
-
390
-/**
391
- * nand_write_buf16 - [DEFAULT] write buffer to chip
392
- * @mtd: MTD device structure
393
- * @buf: data buffer
394
- * @len: number of bytes to write
395
- *
396
- * Default write function for 16bit buswidth.
397
- */
398
-static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
399
-{
400
- struct nand_chip *chip = mtd_to_nand(mtd);
401
- u16 *p = (u16 *) buf;
402
-
403
- iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
404
-}
405
-
406
-/**
407
- * nand_read_buf16 - [DEFAULT] read chip data into buffer
408
- * @mtd: MTD device structure
409
- * @buf: buffer to store date
410
- * @len: number of bytes to read
411
- *
412
- * Default read function for 16bit buswidth.
413
- */
414
-static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
415
-{
416
- struct nand_chip *chip = mtd_to_nand(mtd);
417
- u16 *p = (u16 *) buf;
418
-
419
- ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
242
+ return -EINVAL;
420243 }
421244
422245 /**
423246 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
424
- * @mtd: MTD device structure
247
+ * @chip: NAND chip object
425248 * @ofs: offset from device start
426249 *
427250 * Check, if the block is bad.
428251 */
429
-static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
252
+static int nand_block_bad(struct nand_chip *chip, loff_t ofs)
430253 {
431
- int page, page_end, res;
432
- struct nand_chip *chip = mtd_to_nand(mtd);
254
+ int first_page, page_offset;
255
+ int res;
433256 u8 bad;
434257
435
- if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
436
- ofs += mtd->erasesize - mtd->writesize;
258
+ first_page = (int)(ofs >> chip->page_shift) & chip->pagemask;
259
+ page_offset = nand_bbm_get_next_page(chip, 0);
437260
438
- page = (int)(ofs >> chip->page_shift) & chip->pagemask;
439
- page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
440
-
441
- for (; page < page_end; page++) {
442
- res = chip->ecc.read_oob(mtd, chip, page);
261
+ while (page_offset >= 0) {
262
+ res = chip->ecc.read_oob(chip, first_page + page_offset);
443263 if (res < 0)
444264 return res;
445265
....@@ -451,115 +271,56 @@
451271 res = hweight8(bad) < chip->badblockbits;
452272 if (res)
453273 return res;
274
+
275
+ page_offset = nand_bbm_get_next_page(chip, page_offset + 1);
454276 }
455277
456278 return 0;
457279 }
458280
459
-/**
460
- * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
461
- * @mtd: MTD device structure
462
- * @ofs: offset from device start
463
- *
464
- * This is the default implementation, which can be overridden by a hardware
465
- * specific driver. It provides the details for writing a bad block marker to a
466
- * block.
467
- */
468
-static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
281
+static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs)
469282 {
470
- struct nand_chip *chip = mtd_to_nand(mtd);
471
- struct mtd_oob_ops ops;
472
- uint8_t buf[2] = { 0, 0 };
473
- int ret = 0, res, i = 0;
283
+ if (chip->options & NAND_NO_BBM_QUIRK)
284
+ return 0;
474285
475
- memset(&ops, 0, sizeof(ops));
476
- ops.oobbuf = buf;
477
- ops.ooboffs = chip->badblockpos;
478
- if (chip->options & NAND_BUSWIDTH_16) {
479
- ops.ooboffs &= ~0x01;
480
- ops.len = ops.ooblen = 2;
481
- } else {
482
- ops.len = ops.ooblen = 1;
483
- }
484
- ops.mode = MTD_OPS_PLACE_OOB;
286
+ if (chip->legacy.block_bad)
287
+ return chip->legacy.block_bad(chip, ofs);
485288
486
- /* Write to first/last page(s) if necessary */
487
- if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
488
- ofs += mtd->erasesize - mtd->writesize;
489
- do {
490
- res = nand_do_write_oob(mtd, ofs, &ops);
491
- if (!ret)
492
- ret = res;
493
-
494
- i++;
495
- ofs += mtd->writesize;
496
- } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
497
-
498
- return ret;
289
+ return nand_block_bad(chip, ofs);
499290 }
500291
501292 /**
502
- * nand_block_markbad_lowlevel - mark a block bad
503
- * @mtd: MTD device structure
504
- * @ofs: offset from device start
293
+ * nand_get_device - [GENERIC] Get chip for selected access
294
+ * @chip: NAND chip structure
505295 *
506
- * This function performs the generic NAND bad block marking steps (i.e., bad
507
- * block table(s) and/or marker(s)). We only allow the hardware driver to
508
- * specify how to write bad block markers to OOB (chip->block_markbad).
296
+ * Lock the device and its controller for exclusive access
509297 *
510
- * We try operations in the following order:
511
- *
512
- * (1) erase the affected block, to allow OOB marker to be written cleanly
513
- * (2) write bad block marker to OOB area of affected block (unless flag
514
- * NAND_BBT_NO_OOB_BBM is present)
515
- * (3) update the BBT
516
- *
517
- * Note that we retain the first error encountered in (2) or (3), finish the
518
- * procedures, and dump the error in the end.
519
-*/
520
-static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
298
+ * Return: -EBUSY if the chip has been suspended, 0 otherwise
299
+ */
300
+static void nand_get_device(struct nand_chip *chip)
521301 {
522
- struct nand_chip *chip = mtd_to_nand(mtd);
523
- int res, ret = 0;
302
+ /* Wait until the device is resumed. */
303
+ while (1) {
304
+ mutex_lock(&chip->lock);
305
+ if (!chip->suspended) {
306
+ mutex_lock(&chip->controller->lock);
307
+ return;
308
+ }
309
+ mutex_unlock(&chip->lock);
524310
525
- if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
526
- struct erase_info einfo;
527
-
528
- /* Attempt erase before marking OOB */
529
- memset(&einfo, 0, sizeof(einfo));
530
- einfo.addr = ofs;
531
- einfo.len = 1ULL << chip->phys_erase_shift;
532
- nand_erase_nand(mtd, &einfo, 0);
533
-
534
- /* Write bad block marker to OOB */
535
- nand_get_device(mtd, FL_WRITING);
536
- ret = chip->block_markbad(mtd, ofs);
537
- nand_release_device(mtd);
311
+ wait_event(chip->resume_wq, !chip->suspended);
538312 }
539
-
540
- /* Mark block bad in BBT */
541
- if (chip->bbt) {
542
- res = nand_markbad_bbt(mtd, ofs);
543
- if (!ret)
544
- ret = res;
545
- }
546
-
547
- if (!ret)
548
- mtd->ecc_stats.badblocks++;
549
-
550
- return ret;
551313 }
552314
553315 /**
554316 * nand_check_wp - [GENERIC] check if the chip is write protected
555
- * @mtd: MTD device structure
317
+ * @chip: NAND chip object
556318 *
557319 * Check, if the device is write protected. The function expects, that the
558320 * device is already selected.
559321 */
560
-static int nand_check_wp(struct mtd_info *mtd)
322
+static int nand_check_wp(struct nand_chip *chip)
561323 {
562
- struct nand_chip *chip = mtd_to_nand(mtd);
563324 u8 status;
564325 int ret;
565326
....@@ -576,6 +337,223 @@
576337 }
577338
578339 /**
340
+ * nand_fill_oob - [INTERN] Transfer client buffer to oob
341
+ * @chip: NAND chip object
342
+ * @oob: oob data buffer
343
+ * @len: oob data write length
344
+ * @ops: oob ops structure
345
+ */
346
+static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob, size_t len,
347
+ struct mtd_oob_ops *ops)
348
+{
349
+ struct mtd_info *mtd = nand_to_mtd(chip);
350
+ int ret;
351
+
352
+ /*
353
+ * Initialise to all 0xFF, to avoid the possibility of left over OOB
354
+ * data from a previous OOB read.
355
+ */
356
+ memset(chip->oob_poi, 0xff, mtd->oobsize);
357
+
358
+ switch (ops->mode) {
359
+
360
+ case MTD_OPS_PLACE_OOB:
361
+ case MTD_OPS_RAW:
362
+ memcpy(chip->oob_poi + ops->ooboffs, oob, len);
363
+ return oob + len;
364
+
365
+ case MTD_OPS_AUTO_OOB:
366
+ ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
367
+ ops->ooboffs, len);
368
+ BUG_ON(ret);
369
+ return oob + len;
370
+
371
+ default:
372
+ BUG();
373
+ }
374
+ return NULL;
375
+}
376
+
377
+/**
378
+ * nand_do_write_oob - [MTD Interface] NAND write out-of-band
379
+ * @chip: NAND chip object
380
+ * @to: offset to write to
381
+ * @ops: oob operation description structure
382
+ *
383
+ * NAND write out-of-band.
384
+ */
385
+static int nand_do_write_oob(struct nand_chip *chip, loff_t to,
386
+ struct mtd_oob_ops *ops)
387
+{
388
+ struct mtd_info *mtd = nand_to_mtd(chip);
389
+ int chipnr, page, status, len, ret;
390
+
391
+ pr_debug("%s: to = 0x%08x, len = %i\n",
392
+ __func__, (unsigned int)to, (int)ops->ooblen);
393
+
394
+ len = mtd_oobavail(mtd, ops);
395
+
396
+ /* Do not allow write past end of page */
397
+ if ((ops->ooboffs + ops->ooblen) > len) {
398
+ pr_debug("%s: attempt to write past end of page\n",
399
+ __func__);
400
+ return -EINVAL;
401
+ }
402
+
403
+ chipnr = (int)(to >> chip->chip_shift);
404
+
405
+ /*
406
+ * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
407
+ * of my DiskOnChip 2000 test units) will clear the whole data page too
408
+ * if we don't do this. I have no clue why, but I seem to have 'fixed'
409
+ * it in the doc2000 driver in August 1999. dwmw2.
410
+ */
411
+ ret = nand_reset(chip, chipnr);
412
+ if (ret)
413
+ return ret;
414
+
415
+ nand_select_target(chip, chipnr);
416
+
417
+ /* Shift to get page */
418
+ page = (int)(to >> chip->page_shift);
419
+
420
+ /* Check, if it is write protected */
421
+ if (nand_check_wp(chip)) {
422
+ nand_deselect_target(chip);
423
+ return -EROFS;
424
+ }
425
+
426
+ /* Invalidate the page cache, if we write to the cached page */
427
+ if (page == chip->pagecache.page)
428
+ chip->pagecache.page = -1;
429
+
430
+ nand_fill_oob(chip, ops->oobbuf, ops->ooblen, ops);
431
+
432
+ if (ops->mode == MTD_OPS_RAW)
433
+ status = chip->ecc.write_oob_raw(chip, page & chip->pagemask);
434
+ else
435
+ status = chip->ecc.write_oob(chip, page & chip->pagemask);
436
+
437
+ nand_deselect_target(chip);
438
+
439
+ if (status)
440
+ return status;
441
+
442
+ ops->oobretlen = ops->ooblen;
443
+
444
+ return 0;
445
+}
446
+
447
+/**
448
+ * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
449
+ * @chip: NAND chip object
450
+ * @ofs: offset from device start
451
+ *
452
+ * This is the default implementation, which can be overridden by a hardware
453
+ * specific driver. It provides the details for writing a bad block marker to a
454
+ * block.
455
+ */
456
+static int nand_default_block_markbad(struct nand_chip *chip, loff_t ofs)
457
+{
458
+ struct mtd_info *mtd = nand_to_mtd(chip);
459
+ struct mtd_oob_ops ops;
460
+ uint8_t buf[2] = { 0, 0 };
461
+ int ret = 0, res, page_offset;
462
+
463
+ memset(&ops, 0, sizeof(ops));
464
+ ops.oobbuf = buf;
465
+ ops.ooboffs = chip->badblockpos;
466
+ if (chip->options & NAND_BUSWIDTH_16) {
467
+ ops.ooboffs &= ~0x01;
468
+ ops.len = ops.ooblen = 2;
469
+ } else {
470
+ ops.len = ops.ooblen = 1;
471
+ }
472
+ ops.mode = MTD_OPS_PLACE_OOB;
473
+
474
+ page_offset = nand_bbm_get_next_page(chip, 0);
475
+
476
+ while (page_offset >= 0) {
477
+ res = nand_do_write_oob(chip,
478
+ ofs + (page_offset * mtd->writesize),
479
+ &ops);
480
+
481
+ if (!ret)
482
+ ret = res;
483
+
484
+ page_offset = nand_bbm_get_next_page(chip, page_offset + 1);
485
+ }
486
+
487
+ return ret;
488
+}
489
+
490
+/**
491
+ * nand_markbad_bbm - mark a block by updating the BBM
492
+ * @chip: NAND chip object
493
+ * @ofs: offset of the block to mark bad
494
+ */
495
+int nand_markbad_bbm(struct nand_chip *chip, loff_t ofs)
496
+{
497
+ if (chip->legacy.block_markbad)
498
+ return chip->legacy.block_markbad(chip, ofs);
499
+
500
+ return nand_default_block_markbad(chip, ofs);
501
+}
502
+
503
+/**
504
+ * nand_block_markbad_lowlevel - mark a block bad
505
+ * @chip: NAND chip object
506
+ * @ofs: offset from device start
507
+ *
508
+ * This function performs the generic NAND bad block marking steps (i.e., bad
509
+ * block table(s) and/or marker(s)). We only allow the hardware driver to
510
+ * specify how to write bad block markers to OOB (chip->legacy.block_markbad).
511
+ *
512
+ * We try operations in the following order:
513
+ *
514
+ * (1) erase the affected block, to allow OOB marker to be written cleanly
515
+ * (2) write bad block marker to OOB area of affected block (unless flag
516
+ * NAND_BBT_NO_OOB_BBM is present)
517
+ * (3) update the BBT
518
+ *
519
+ * Note that we retain the first error encountered in (2) or (3), finish the
520
+ * procedures, and dump the error in the end.
521
+*/
522
+static int nand_block_markbad_lowlevel(struct nand_chip *chip, loff_t ofs)
523
+{
524
+ struct mtd_info *mtd = nand_to_mtd(chip);
525
+ int res, ret = 0;
526
+
527
+ if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
528
+ struct erase_info einfo;
529
+
530
+ /* Attempt erase before marking OOB */
531
+ memset(&einfo, 0, sizeof(einfo));
532
+ einfo.addr = ofs;
533
+ einfo.len = 1ULL << chip->phys_erase_shift;
534
+ nand_erase_nand(chip, &einfo, 0);
535
+
536
+ /* Write bad block marker to OOB */
537
+ nand_get_device(chip);
538
+
539
+ ret = nand_markbad_bbm(chip, ofs);
540
+ nand_release_device(chip);
541
+ }
542
+
543
+ /* Mark block bad in BBT */
544
+ if (chip->bbt) {
545
+ res = nand_markbad_bbt(chip, ofs);
546
+ if (!ret)
547
+ ret = res;
548
+ }
549
+
550
+ if (!ret)
551
+ mtd->ecc_stats.badblocks++;
552
+
553
+ return ret;
554
+}
555
+
556
+/**
579557 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
580558 * @mtd: MTD device structure
581559 * @ofs: offset from device start
....@@ -589,103 +567,26 @@
589567 if (!chip->bbt)
590568 return 0;
591569 /* Return info from the table */
592
- return nand_isreserved_bbt(mtd, ofs);
570
+ return nand_isreserved_bbt(chip, ofs);
593571 }
594572
595573 /**
596574 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
597
- * @mtd: MTD device structure
575
+ * @chip: NAND chip object
598576 * @ofs: offset from device start
599577 * @allowbbt: 1, if its allowed to access the bbt area
600578 *
601579 * Check, if the block is bad. Either by reading the bad block table or
602580 * calling of the scan function.
603581 */
604
-static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
582
+static int nand_block_checkbad(struct nand_chip *chip, loff_t ofs, int allowbbt)
605583 {
606
- struct nand_chip *chip = mtd_to_nand(mtd);
607
-
608
- if (!chip->bbt)
609
- return chip->block_bad(mtd, ofs);
610
-
611584 /* Return info from the table */
612
- return nand_isbad_bbt(mtd, ofs, allowbbt);
585
+ if (chip->bbt)
586
+ return nand_isbad_bbt(chip, ofs, allowbbt);
587
+
588
+ return nand_isbad_bbm(chip, ofs);
613589 }
614
-
615
-/**
616
- * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
617
- * @mtd: MTD device structure
618
- * @timeo: Timeout
619
- *
620
- * Helper function for nand_wait_ready used when needing to wait in interrupt
621
- * context.
622
- */
623
-static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
624
-{
625
- struct nand_chip *chip = mtd_to_nand(mtd);
626
- int i;
627
-
628
- /* Wait for the device to get ready */
629
- for (i = 0; i < timeo; i++) {
630
- if (chip->dev_ready(mtd))
631
- break;
632
- touch_softlockup_watchdog();
633
- mdelay(1);
634
- }
635
-}
636
-
637
-/**
638
- * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
639
- * @mtd: MTD device structure
640
- *
641
- * Wait for the ready pin after a command, and warn if a timeout occurs.
642
- */
643
-void nand_wait_ready(struct mtd_info *mtd)
644
-{
645
- struct nand_chip *chip = mtd_to_nand(mtd);
646
- unsigned long timeo = 400;
647
-
648
- if (in_interrupt() || oops_in_progress)
649
- return panic_nand_wait_ready(mtd, timeo);
650
-
651
- /* Wait until command is processed or timeout occurs */
652
- timeo = jiffies + msecs_to_jiffies(timeo);
653
- do {
654
- if (chip->dev_ready(mtd))
655
- return;
656
- cond_resched();
657
- } while (time_before(jiffies, timeo));
658
-
659
- if (!chip->dev_ready(mtd))
660
- pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
661
-}
662
-EXPORT_SYMBOL_GPL(nand_wait_ready);
663
-
664
-/**
665
- * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
666
- * @mtd: MTD device structure
667
- * @timeo: Timeout in ms
668
- *
669
- * Wait for status ready (i.e. command done) or timeout.
670
- */
671
-static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
672
-{
673
- register struct nand_chip *chip = mtd_to_nand(mtd);
674
- int ret;
675
-
676
- timeo = jiffies + msecs_to_jiffies(timeo);
677
- do {
678
- u8 status;
679
-
680
- ret = nand_read_data_op(chip, &status, sizeof(status), true);
681
- if (ret)
682
- return;
683
-
684
- if (status & NAND_STATUS_READY)
685
- break;
686
- touch_softlockup_watchdog();
687
- } while (time_before(jiffies, timeo));
688
-};
689590
690591 /**
691592 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
....@@ -710,20 +611,26 @@
710611 u8 status = 0;
711612 int ret;
712613
713
- if (!chip->exec_op)
614
+ if (!nand_has_exec_op(chip))
714615 return -ENOTSUPP;
715616
716617 /* Wait tWB before polling the STATUS reg. */
717
- timings = nand_get_sdr_timings(&chip->data_interface);
618
+ timings = nand_get_sdr_timings(nand_get_interface_config(chip));
718619 ndelay(PSEC_TO_NSEC(timings->tWB_max));
719620
720621 ret = nand_status_op(chip, NULL);
721622 if (ret)
722623 return ret;
723624
724
- timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
625
+ /*
626
+ * +1 below is necessary because if we are now in the last fraction
627
+ * of jiffy and msecs_to_jiffies is 1 then we will wait only that
628
+ * small jiffy fraction - possibly leading to false timeout
629
+ */
630
+ timeout_ms = jiffies + msecs_to_jiffies(timeout_ms) + 1;
725631 do {
726
- ret = nand_read_data_op(chip, &status, sizeof(status), true);
632
+ ret = nand_read_data_op(chip, &status, sizeof(status), true,
633
+ false);
727634 if (ret)
728635 break;
729636
....@@ -753,332 +660,43 @@
753660 EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
754661
755662 /**
756
- * nand_command - [DEFAULT] Send command to NAND device
757
- * @mtd: MTD device structure
758
- * @command: the command to be sent
759
- * @column: the column address for this command, -1 if none
760
- * @page_addr: the page address for this command, -1 if none
663
+ * nand_gpio_waitrdy - Poll R/B GPIO pin until ready
664
+ * @chip: NAND chip structure
665
+ * @gpiod: GPIO descriptor of R/B pin
666
+ * @timeout_ms: Timeout in ms
761667 *
762
- * Send command to NAND device. This function is used for small page devices
763
- * (512 Bytes per page).
764
- */
765
-static void nand_command(struct mtd_info *mtd, unsigned int command,
766
- int column, int page_addr)
767
-{
768
- register struct nand_chip *chip = mtd_to_nand(mtd);
769
- int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
770
-
771
- /* Write out the command to the device */
772
- if (command == NAND_CMD_SEQIN) {
773
- int readcmd;
774
-
775
- if (column >= mtd->writesize) {
776
- /* OOB area */
777
- column -= mtd->writesize;
778
- readcmd = NAND_CMD_READOOB;
779
- } else if (column < 256) {
780
- /* First 256 bytes --> READ0 */
781
- readcmd = NAND_CMD_READ0;
782
- } else {
783
- column -= 256;
784
- readcmd = NAND_CMD_READ1;
785
- }
786
- chip->cmd_ctrl(mtd, readcmd, ctrl);
787
- ctrl &= ~NAND_CTRL_CHANGE;
788
- }
789
- if (command != NAND_CMD_NONE)
790
- chip->cmd_ctrl(mtd, command, ctrl);
791
-
792
- /* Address cycle, when necessary */
793
- ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
794
- /* Serially input address */
795
- if (column != -1) {
796
- /* Adjust columns for 16 bit buswidth */
797
- if (chip->options & NAND_BUSWIDTH_16 &&
798
- !nand_opcode_8bits(command))
799
- column >>= 1;
800
- chip->cmd_ctrl(mtd, column, ctrl);
801
- ctrl &= ~NAND_CTRL_CHANGE;
802
- }
803
- if (page_addr != -1) {
804
- chip->cmd_ctrl(mtd, page_addr, ctrl);
805
- ctrl &= ~NAND_CTRL_CHANGE;
806
- chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
807
- if (chip->options & NAND_ROW_ADDR_3)
808
- chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
809
- }
810
- chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
811
-
812
- /*
813
- * Program and erase have their own busy handlers status and sequential
814
- * in needs no delay
815
- */
816
- switch (command) {
817
-
818
- case NAND_CMD_NONE:
819
- case NAND_CMD_PAGEPROG:
820
- case NAND_CMD_ERASE1:
821
- case NAND_CMD_ERASE2:
822
- case NAND_CMD_SEQIN:
823
- case NAND_CMD_STATUS:
824
- case NAND_CMD_READID:
825
- case NAND_CMD_SET_FEATURES:
826
- return;
827
-
828
- case NAND_CMD_RESET:
829
- if (chip->dev_ready)
830
- break;
831
- udelay(chip->chip_delay);
832
- chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
833
- NAND_CTRL_CLE | NAND_CTRL_CHANGE);
834
- chip->cmd_ctrl(mtd,
835
- NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
836
- /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
837
- nand_wait_status_ready(mtd, 250);
838
- return;
839
-
840
- /* This applies to read commands */
841
- case NAND_CMD_READ0:
842
- /*
843
- * READ0 is sometimes used to exit GET STATUS mode. When this
844
- * is the case no address cycles are requested, and we can use
845
- * this information to detect that we should not wait for the
846
- * device to be ready.
847
- */
848
- if (column == -1 && page_addr == -1)
849
- return;
850
-
851
- default:
852
- /*
853
- * If we don't have access to the busy pin, we apply the given
854
- * command delay
855
- */
856
- if (!chip->dev_ready) {
857
- udelay(chip->chip_delay);
858
- return;
859
- }
860
- }
861
- /*
862
- * Apply this short delay always to ensure that we do wait tWB in
863
- * any case on any machine.
864
- */
865
- ndelay(100);
866
-
867
- nand_wait_ready(mtd);
868
-}
869
-
870
-static void nand_ccs_delay(struct nand_chip *chip)
871
-{
872
- /*
873
- * The controller already takes care of waiting for tCCS when the RNDIN
874
- * or RNDOUT command is sent, return directly.
875
- */
876
- if (!(chip->options & NAND_WAIT_TCCS))
877
- return;
878
-
879
- /*
880
- * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
881
- * (which should be safe for all NANDs).
882
- */
883
- if (chip->setup_data_interface)
884
- ndelay(chip->data_interface.timings.sdr.tCCS_min / 1000);
885
- else
886
- ndelay(500);
887
-}
888
-
889
-/**
890
- * nand_command_lp - [DEFAULT] Send command to NAND large page device
891
- * @mtd: MTD device structure
892
- * @command: the command to be sent
893
- * @column: the column address for this command, -1 if none
894
- * @page_addr: the page address for this command, -1 if none
668
+ * Poll the R/B GPIO pin until it becomes ready. If that does not happen
669
+ * whitin the specified timeout, -ETIMEDOUT is returned.
895670 *
896
- * Send command to NAND device. This is the version for the new large page
897
- * devices. We don't have the separate regions as we have in the small page
898
- * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
671
+ * This helper is intended to be used when the controller has access to the
672
+ * NAND R/B pin over GPIO.
673
+ *
674
+ * Return 0 if the R/B pin indicates chip is ready, a negative error otherwise.
899675 */
900
-static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
901
- int column, int page_addr)
676
+int nand_gpio_waitrdy(struct nand_chip *chip, struct gpio_desc *gpiod,
677
+ unsigned long timeout_ms)
902678 {
903
- register struct nand_chip *chip = mtd_to_nand(mtd);
904
-
905
- /* Emulate NAND_CMD_READOOB */
906
- if (command == NAND_CMD_READOOB) {
907
- column += mtd->writesize;
908
- command = NAND_CMD_READ0;
909
- }
910
-
911
- /* Command latch cycle */
912
- if (command != NAND_CMD_NONE)
913
- chip->cmd_ctrl(mtd, command,
914
- NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
915
-
916
- if (column != -1 || page_addr != -1) {
917
- int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
918
-
919
- /* Serially input address */
920
- if (column != -1) {
921
- /* Adjust columns for 16 bit buswidth */
922
- if (chip->options & NAND_BUSWIDTH_16 &&
923
- !nand_opcode_8bits(command))
924
- column >>= 1;
925
- chip->cmd_ctrl(mtd, column, ctrl);
926
- ctrl &= ~NAND_CTRL_CHANGE;
927
-
928
- /* Only output a single addr cycle for 8bits opcodes. */
929
- if (!nand_opcode_8bits(command))
930
- chip->cmd_ctrl(mtd, column >> 8, ctrl);
931
- }
932
- if (page_addr != -1) {
933
- chip->cmd_ctrl(mtd, page_addr, ctrl);
934
- chip->cmd_ctrl(mtd, page_addr >> 8,
935
- NAND_NCE | NAND_ALE);
936
- if (chip->options & NAND_ROW_ADDR_3)
937
- chip->cmd_ctrl(mtd, page_addr >> 16,
938
- NAND_NCE | NAND_ALE);
939
- }
940
- }
941
- chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
942679
943680 /*
944
- * Program and erase have their own busy handlers status, sequential
945
- * in and status need no delay.
681
+ * Wait until R/B pin indicates chip is ready or timeout occurs.
682
+ * +1 below is necessary because if we are now in the last fraction
683
+ * of jiffy and msecs_to_jiffies is 1 then we will wait only that
684
+ * small jiffy fraction - possibly leading to false timeout.
946685 */
947
- switch (command) {
948
-
949
- case NAND_CMD_NONE:
950
- case NAND_CMD_CACHEDPROG:
951
- case NAND_CMD_PAGEPROG:
952
- case NAND_CMD_ERASE1:
953
- case NAND_CMD_ERASE2:
954
- case NAND_CMD_SEQIN:
955
- case NAND_CMD_STATUS:
956
- case NAND_CMD_READID:
957
- case NAND_CMD_SET_FEATURES:
958
- return;
959
-
960
- case NAND_CMD_RNDIN:
961
- nand_ccs_delay(chip);
962
- return;
963
-
964
- case NAND_CMD_RESET:
965
- if (chip->dev_ready)
966
- break;
967
- udelay(chip->chip_delay);
968
- chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
969
- NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
970
- chip->cmd_ctrl(mtd, NAND_CMD_NONE,
971
- NAND_NCE | NAND_CTRL_CHANGE);
972
- /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
973
- nand_wait_status_ready(mtd, 250);
974
- return;
975
-
976
- case NAND_CMD_RNDOUT:
977
- /* No ready / busy check necessary */
978
- chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
979
- NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
980
- chip->cmd_ctrl(mtd, NAND_CMD_NONE,
981
- NAND_NCE | NAND_CTRL_CHANGE);
982
-
983
- nand_ccs_delay(chip);
984
- return;
985
-
986
- case NAND_CMD_READ0:
987
- /*
988
- * READ0 is sometimes used to exit GET STATUS mode. When this
989
- * is the case no address cycles are requested, and we can use
990
- * this information to detect that READSTART should not be
991
- * issued.
992
- */
993
- if (column == -1 && page_addr == -1)
994
- return;
995
-
996
- chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
997
- NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
998
- chip->cmd_ctrl(mtd, NAND_CMD_NONE,
999
- NAND_NCE | NAND_CTRL_CHANGE);
1000
-
1001
- /* This applies to read commands */
1002
- default:
1003
- /*
1004
- * If we don't have access to the busy pin, we apply the given
1005
- * command delay.
1006
- */
1007
- if (!chip->dev_ready) {
1008
- udelay(chip->chip_delay);
1009
- return;
1010
- }
1011
- }
1012
-
1013
- /*
1014
- * Apply this short delay always to ensure that we do wait tWB in
1015
- * any case on any machine.
1016
- */
1017
- ndelay(100);
1018
-
1019
- nand_wait_ready(mtd);
1020
-}
1021
-
1022
-/**
1023
- * panic_nand_get_device - [GENERIC] Get chip for selected access
1024
- * @chip: the nand chip descriptor
1025
- * @mtd: MTD device structure
1026
- * @new_state: the state which is requested
1027
- *
1028
- * Used when in panic, no locks are taken.
1029
- */
1030
-static void panic_nand_get_device(struct nand_chip *chip,
1031
- struct mtd_info *mtd, int new_state)
1032
-{
1033
- /* Hardware controller shared among independent devices */
1034
- chip->controller->active = chip;
1035
- chip->state = new_state;
1036
-}
1037
-
1038
-/**
1039
- * nand_get_device - [GENERIC] Get chip for selected access
1040
- * @mtd: MTD device structure
1041
- * @new_state: the state which is requested
1042
- *
1043
- * Get the device and lock it for exclusive access
1044
- */
1045
-static int
1046
-nand_get_device(struct mtd_info *mtd, int new_state)
1047
-{
1048
- struct nand_chip *chip = mtd_to_nand(mtd);
1049
- spinlock_t *lock = &chip->controller->lock;
1050
- wait_queue_head_t *wq = &chip->controller->wq;
1051
- DECLARE_WAITQUEUE(wait, current);
1052
-retry:
1053
- spin_lock(lock);
1054
-
1055
- /* Hardware controller shared among independent devices */
1056
- if (!chip->controller->active)
1057
- chip->controller->active = chip;
1058
-
1059
- if (chip->controller->active == chip && chip->state == FL_READY) {
1060
- chip->state = new_state;
1061
- spin_unlock(lock);
1062
- return 0;
1063
- }
1064
- if (new_state == FL_PM_SUSPENDED) {
1065
- if (chip->controller->active->state == FL_PM_SUSPENDED) {
1066
- chip->state = FL_PM_SUSPENDED;
1067
- spin_unlock(lock);
686
+ timeout_ms = jiffies + msecs_to_jiffies(timeout_ms) + 1;
687
+ do {
688
+ if (gpiod_get_value_cansleep(gpiod))
1068689 return 0;
1069
- }
1070
- }
1071
- set_current_state(TASK_UNINTERRUPTIBLE);
1072
- add_wait_queue(wq, &wait);
1073
- spin_unlock(lock);
1074
- schedule();
1075
- remove_wait_queue(wq, &wait);
1076
- goto retry;
1077
-}
690
+
691
+ cond_resched();
692
+ } while (time_before(jiffies, timeout_ms));
693
+
694
+ return gpiod_get_value_cansleep(gpiod) ? 0 : -ETIMEDOUT;
695
+};
696
+EXPORT_SYMBOL_GPL(nand_gpio_waitrdy);
1078697
1079698 /**
1080699 * panic_nand_wait - [GENERIC] wait until the command is done
1081
- * @mtd: MTD device structure
1082700 * @chip: NAND chip structure
1083701 * @timeo: timeout
1084702 *
....@@ -1086,20 +704,19 @@
1086704 * we are in interrupt context. May happen when in panic and trying to write
1087705 * an oops through mtdoops.
1088706 */
1089
-static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
1090
- unsigned long timeo)
707
+void panic_nand_wait(struct nand_chip *chip, unsigned long timeo)
1091708 {
1092709 int i;
1093710 for (i = 0; i < timeo; i++) {
1094
- if (chip->dev_ready) {
1095
- if (chip->dev_ready(mtd))
711
+ if (chip->legacy.dev_ready) {
712
+ if (chip->legacy.dev_ready(chip))
1096713 break;
1097714 } else {
1098715 int ret;
1099716 u8 status;
1100717
1101718 ret = nand_read_data_op(chip, &status, sizeof(status),
1102
- true);
719
+ true, false);
1103720 if (ret)
1104721 return;
1105722
....@@ -1108,60 +725,6 @@
1108725 }
1109726 mdelay(1);
1110727 }
1111
-}
1112
-
1113
-/**
1114
- * nand_wait - [DEFAULT] wait until the command is done
1115
- * @mtd: MTD device structure
1116
- * @chip: NAND chip structure
1117
- *
1118
- * Wait for command done. This applies to erase and program only.
1119
- */
1120
-static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
1121
-{
1122
-
1123
- unsigned long timeo = 400;
1124
- u8 status;
1125
- int ret;
1126
-
1127
- /*
1128
- * Apply this short delay always to ensure that we do wait tWB in any
1129
- * case on any machine.
1130
- */
1131
- ndelay(100);
1132
-
1133
- ret = nand_status_op(chip, NULL);
1134
- if (ret)
1135
- return ret;
1136
-
1137
- if (in_interrupt() || oops_in_progress)
1138
- panic_nand_wait(mtd, chip, timeo);
1139
- else {
1140
- timeo = jiffies + msecs_to_jiffies(timeo);
1141
- do {
1142
- if (chip->dev_ready) {
1143
- if (chip->dev_ready(mtd))
1144
- break;
1145
- } else {
1146
- ret = nand_read_data_op(chip, &status,
1147
- sizeof(status), true);
1148
- if (ret)
1149
- return ret;
1150
-
1151
- if (status & NAND_STATUS_READY)
1152
- break;
1153
- }
1154
- cond_resched();
1155
- } while (time_before(jiffies, timeo));
1156
- }
1157
-
1158
- ret = nand_read_data_op(chip, &status, sizeof(status), true);
1159
- if (ret)
1160
- return ret;
1161
-
1162
- /* This can happen if in case of timeout or buggy dev_ready */
1163
- WARN_ON(!(status & NAND_STATUS_READY));
1164
- return status;
1165728 }
1166729
1167730 static bool nand_supports_get_features(struct nand_chip *chip, int addr)
....@@ -1177,49 +740,7 @@
1177740 }
1178741
1179742 /**
1180
- * nand_get_features - wrapper to perform a GET_FEATURE
1181
- * @chip: NAND chip info structure
1182
- * @addr: feature address
1183
- * @subfeature_param: the subfeature parameters, a four bytes array
1184
- *
1185
- * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1186
- * operation cannot be handled.
1187
- */
1188
-int nand_get_features(struct nand_chip *chip, int addr,
1189
- u8 *subfeature_param)
1190
-{
1191
- struct mtd_info *mtd = nand_to_mtd(chip);
1192
-
1193
- if (!nand_supports_get_features(chip, addr))
1194
- return -ENOTSUPP;
1195
-
1196
- return chip->get_features(mtd, chip, addr, subfeature_param);
1197
-}
1198
-EXPORT_SYMBOL_GPL(nand_get_features);
1199
-
1200
-/**
1201
- * nand_set_features - wrapper to perform a SET_FEATURE
1202
- * @chip: NAND chip info structure
1203
- * @addr: feature address
1204
- * @subfeature_param: the subfeature parameters, a four bytes array
1205
- *
1206
- * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1207
- * operation cannot be handled.
1208
- */
1209
-int nand_set_features(struct nand_chip *chip, int addr,
1210
- u8 *subfeature_param)
1211
-{
1212
- struct mtd_info *mtd = nand_to_mtd(chip);
1213
-
1214
- if (!nand_supports_set_features(chip, addr))
1215
- return -ENOTSUPP;
1216
-
1217
- return chip->set_features(mtd, chip, addr, subfeature_param);
1218
-}
1219
-EXPORT_SYMBOL_GPL(nand_set_features);
1220
-
1221
-/**
1222
- * nand_reset_data_interface - Reset data interface and timings
743
+ * nand_reset_interface - Reset data interface and timings
1223744 * @chip: The NAND chip
1224745 * @chipnr: Internal die id
1225746 *
....@@ -1227,12 +748,12 @@
1227748 *
1228749 * Returns 0 for success or negative error code otherwise.
1229750 */
1230
-static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
751
+static int nand_reset_interface(struct nand_chip *chip, int chipnr)
1231752 {
1232
- struct mtd_info *mtd = nand_to_mtd(chip);
753
+ const struct nand_controller_ops *ops = chip->controller->ops;
1233754 int ret;
1234755
1235
- if (!chip->setup_data_interface)
756
+ if (!nand_controller_can_setup_interface(chip))
1236757 return 0;
1237758
1238759 /*
....@@ -1249,8 +770,9 @@
1249770 * timings to timing mode 0.
1250771 */
1251772
1252
- onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
1253
- ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
773
+ chip->current_interface_config = nand_get_reset_interface_config();
774
+ ret = ops->setup_interface(chip, chipnr,
775
+ chip->current_interface_config);
1254776 if (ret)
1255777 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1256778
....@@ -1258,61 +780,71 @@
1258780 }
1259781
1260782 /**
1261
- * nand_setup_data_interface - Setup the best data interface and timings
783
+ * nand_setup_interface - Setup the best data interface and timings
1262784 * @chip: The NAND chip
1263785 * @chipnr: Internal die id
1264786 *
1265
- * Find and configure the best data interface and NAND timings supported by
1266
- * the chip and the driver.
1267
- * First tries to retrieve supported timing modes from ONFI information,
1268
- * and if the NAND chip does not support ONFI, relies on the
1269
- * ->onfi_timing_mode_default specified in the nand_ids table.
787
+ * Configure what has been reported to be the best data interface and NAND
788
+ * timings supported by the chip and the driver.
1270789 *
1271790 * Returns 0 for success or negative error code otherwise.
1272791 */
1273
-static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
792
+static int nand_setup_interface(struct nand_chip *chip, int chipnr)
1274793 {
1275
- struct mtd_info *mtd = nand_to_mtd(chip);
1276
- u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1277
- chip->onfi_timing_mode_default,
1278
- };
794
+ const struct nand_controller_ops *ops = chip->controller->ops;
795
+ u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = { };
1279796 int ret;
1280797
1281
- if (!chip->setup_data_interface)
798
+ if (!nand_controller_can_setup_interface(chip))
1282799 return 0;
800
+
801
+ /*
802
+ * A nand_reset_interface() put both the NAND chip and the NAND
803
+ * controller in timings mode 0. If the default mode for this chip is
804
+ * also 0, no need to proceed to the change again. Plus, at probe time,
805
+ * nand_setup_interface() uses ->set/get_features() which would
806
+ * fail anyway as the parameter page is not available yet.
807
+ */
808
+ if (!chip->best_interface_config)
809
+ return 0;
810
+
811
+ tmode_param[0] = chip->best_interface_config->timings.mode;
1283812
1284813 /* Change the mode on the chip side (if supported by the NAND chip) */
1285814 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
1286
- chip->select_chip(mtd, chipnr);
815
+ nand_select_target(chip, chipnr);
1287816 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1288817 tmode_param);
1289
- chip->select_chip(mtd, -1);
818
+ nand_deselect_target(chip);
1290819 if (ret)
1291820 return ret;
1292821 }
1293822
1294823 /* Change the mode on the controller side */
1295
- ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
824
+ ret = ops->setup_interface(chip, chipnr, chip->best_interface_config);
1296825 if (ret)
1297826 return ret;
1298827
1299828 /* Check the mode has been accepted by the chip, if supported */
1300829 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
1301
- return 0;
830
+ goto update_interface_config;
1302831
1303832 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
1304
- chip->select_chip(mtd, chipnr);
833
+ nand_select_target(chip, chipnr);
1305834 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1306835 tmode_param);
1307
- chip->select_chip(mtd, -1);
836
+ nand_deselect_target(chip);
1308837 if (ret)
1309838 goto err_reset_chip;
1310839
1311
- if (tmode_param[0] != chip->onfi_timing_mode_default) {
840
+ if (tmode_param[0] != chip->best_interface_config->timings.mode) {
1312841 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
1313
- chip->onfi_timing_mode_default);
842
+ chip->best_interface_config->timings.mode);
1314843 goto err_reset_chip;
1315844 }
845
+
846
+update_interface_config:
847
+ chip->current_interface_config = chip->best_interface_config;
1316848
1317849 return 0;
1318850
....@@ -1321,69 +853,99 @@
1321853 * Fallback to mode 0 if the chip explicitly did not ack the chosen
1322854 * timing mode.
1323855 */
1324
- nand_reset_data_interface(chip, chipnr);
1325
- chip->select_chip(mtd, chipnr);
856
+ nand_reset_interface(chip, chipnr);
857
+ nand_select_target(chip, chipnr);
1326858 nand_reset_op(chip);
1327
- chip->select_chip(mtd, -1);
859
+ nand_deselect_target(chip);
1328860
1329861 return ret;
1330862 }
1331863
1332864 /**
1333
- * nand_init_data_interface - find the best data interface and timings
865
+ * nand_choose_best_sdr_timings - Pick up the best SDR timings that both the
866
+ * NAND controller and the NAND chip support
867
+ * @chip: the NAND chip
868
+ * @iface: the interface configuration (can eventually be updated)
869
+ * @spec_timings: specific timings, when not fitting the ONFI specification
870
+ *
871
+ * If specific timings are provided, use them. Otherwise, retrieve supported
872
+ * timing modes from ONFI information.
873
+ */
874
+int nand_choose_best_sdr_timings(struct nand_chip *chip,
875
+ struct nand_interface_config *iface,
876
+ struct nand_sdr_timings *spec_timings)
877
+{
878
+ const struct nand_controller_ops *ops = chip->controller->ops;
879
+ int best_mode = 0, mode, ret;
880
+
881
+ iface->type = NAND_SDR_IFACE;
882
+
883
+ if (spec_timings) {
884
+ iface->timings.sdr = *spec_timings;
885
+ iface->timings.mode = onfi_find_closest_sdr_mode(spec_timings);
886
+
887
+ /* Verify the controller supports the requested interface */
888
+ ret = ops->setup_interface(chip, NAND_DATA_IFACE_CHECK_ONLY,
889
+ iface);
890
+ if (!ret) {
891
+ chip->best_interface_config = iface;
892
+ return ret;
893
+ }
894
+
895
+ /* Fallback to slower modes */
896
+ best_mode = iface->timings.mode;
897
+ } else if (chip->parameters.onfi) {
898
+ best_mode = fls(chip->parameters.onfi->async_timing_mode) - 1;
899
+ }
900
+
901
+ for (mode = best_mode; mode >= 0; mode--) {
902
+ onfi_fill_interface_config(chip, iface, NAND_SDR_IFACE, mode);
903
+
904
+ ret = ops->setup_interface(chip, NAND_DATA_IFACE_CHECK_ONLY,
905
+ iface);
906
+ if (!ret)
907
+ break;
908
+ }
909
+
910
+ chip->best_interface_config = iface;
911
+
912
+ return 0;
913
+}
914
+
915
+/**
916
+ * nand_choose_interface_config - find the best data interface and timings
1334917 * @chip: The NAND chip
1335918 *
1336919 * Find the best data interface and NAND timings supported by the chip
1337
- * and the driver.
1338
- * First tries to retrieve supported timing modes from ONFI information,
1339
- * and if the NAND chip does not support ONFI, relies on the
1340
- * ->onfi_timing_mode_default specified in the nand_ids table. After this
1341
- * function nand_chip->data_interface is initialized with the best timing mode
1342
- * available.
920
+ * and the driver. Eventually let the NAND manufacturer driver propose his own
921
+ * set of timings.
922
+ *
923
+ * After this function nand_chip->interface_config is initialized with the best
924
+ * timing mode available.
1343925 *
1344926 * Returns 0 for success or negative error code otherwise.
1345927 */
1346
-static int nand_init_data_interface(struct nand_chip *chip)
928
+static int nand_choose_interface_config(struct nand_chip *chip)
1347929 {
1348
- struct mtd_info *mtd = nand_to_mtd(chip);
1349
- int modes, mode, ret;
930
+ struct nand_interface_config *iface;
931
+ int ret;
1350932
1351
- if (!chip->setup_data_interface)
933
+ if (!nand_controller_can_setup_interface(chip))
1352934 return 0;
1353935
1354
- /*
1355
- * First try to identify the best timings from ONFI parameters and
1356
- * if the NAND does not support ONFI, fallback to the default ONFI
1357
- * timing mode.
1358
- */
1359
- modes = onfi_get_async_timing_mode(chip);
1360
- if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1361
- if (!chip->onfi_timing_mode_default)
1362
- return 0;
936
+ iface = kzalloc(sizeof(*iface), GFP_KERNEL);
937
+ if (!iface)
938
+ return -ENOMEM;
1363939
1364
- modes = GENMASK(chip->onfi_timing_mode_default, 0);
1365
- }
940
+ if (chip->ops.choose_interface_config)
941
+ ret = chip->ops.choose_interface_config(chip, iface);
942
+ else
943
+ ret = nand_choose_best_sdr_timings(chip, iface, NULL);
1366944
945
+ if (ret)
946
+ kfree(iface);
1367947
1368
- for (mode = fls(modes) - 1; mode >= 0; mode--) {
1369
- ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
1370
- if (ret)
1371
- continue;
1372
-
1373
- /*
1374
- * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
1375
- * controller supports the requested timings.
1376
- */
1377
- ret = chip->setup_data_interface(mtd,
1378
- NAND_DATA_IFACE_CHECK_ONLY,
1379
- &chip->data_interface);
1380
- if (!ret) {
1381
- chip->onfi_timing_mode_default = mode;
1382
- break;
1383
- }
1384
- }
1385
-
1386
- return 0;
948
+ return ret;
1387949 }
1388950
1389951 /**
....@@ -1444,9 +1006,9 @@
14441006 unsigned int offset_in_page, void *buf,
14451007 unsigned int len)
14461008 {
1447
- struct mtd_info *mtd = nand_to_mtd(chip);
14481009 const struct nand_sdr_timings *sdr =
1449
- nand_get_sdr_timings(&chip->data_interface);
1010
+ nand_get_sdr_timings(nand_get_interface_config(chip));
1011
+ struct mtd_info *mtd = nand_to_mtd(chip);
14501012 u8 addrs[4];
14511013 struct nand_op_instr instrs[] = {
14521014 NAND_OP_CMD(NAND_CMD_READ0, 0),
....@@ -1455,7 +1017,7 @@
14551017 PSEC_TO_NSEC(sdr->tRR_min)),
14561018 NAND_OP_DATA_IN(len, buf, 0),
14571019 };
1458
- struct nand_operation op = NAND_OPERATION(instrs);
1020
+ struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
14591021 int ret;
14601022
14611023 /* Drop the DATA_IN instruction if len is set to 0. */
....@@ -1488,7 +1050,7 @@
14881050 unsigned int len)
14891051 {
14901052 const struct nand_sdr_timings *sdr =
1491
- nand_get_sdr_timings(&chip->data_interface);
1053
+ nand_get_sdr_timings(nand_get_interface_config(chip));
14921054 u8 addrs[5];
14931055 struct nand_op_instr instrs[] = {
14941056 NAND_OP_CMD(NAND_CMD_READ0, 0),
....@@ -1498,7 +1060,7 @@
14981060 PSEC_TO_NSEC(sdr->tRR_min)),
14991061 NAND_OP_DATA_IN(len, buf, 0),
15001062 };
1501
- struct nand_operation op = NAND_OPERATION(instrs);
1063
+ struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
15021064 int ret;
15031065
15041066 /* Drop the DATA_IN instruction if len is set to 0. */
....@@ -1544,7 +1106,7 @@
15441106 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
15451107 return -EINVAL;
15461108
1547
- if (chip->exec_op) {
1109
+ if (nand_has_exec_op(chip)) {
15481110 if (mtd->writesize > 512)
15491111 return nand_lp_exec_read_page_op(chip, page,
15501112 offset_in_page, buf,
....@@ -1554,9 +1116,9 @@
15541116 buf, len);
15551117 }
15561118
1557
- chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1119
+ chip->legacy.cmdfunc(chip, NAND_CMD_READ0, offset_in_page, page);
15581120 if (len)
1559
- chip->read_buf(mtd, buf, len);
1121
+ chip->legacy.read_buf(chip, buf, len);
15601122
15611123 return 0;
15621124 }
....@@ -1574,19 +1136,18 @@
15741136 *
15751137 * Returns 0 on success, a negative error code otherwise.
15761138 */
1577
-static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1578
- unsigned int len)
1139
+int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1140
+ unsigned int len)
15791141 {
1580
- struct mtd_info *mtd = nand_to_mtd(chip);
15811142 unsigned int i;
15821143 u8 *p = buf;
15831144
15841145 if (len && !buf)
15851146 return -EINVAL;
15861147
1587
- if (chip->exec_op) {
1148
+ if (nand_has_exec_op(chip)) {
15881149 const struct nand_sdr_timings *sdr =
1589
- nand_get_sdr_timings(&chip->data_interface);
1150
+ nand_get_sdr_timings(nand_get_interface_config(chip));
15901151 struct nand_op_instr instrs[] = {
15911152 NAND_OP_CMD(NAND_CMD_PARAM, 0),
15921153 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
....@@ -1594,7 +1155,7 @@
15941155 PSEC_TO_NSEC(sdr->tRR_min)),
15951156 NAND_OP_8BIT_DATA_IN(len, buf, 0),
15961157 };
1597
- struct nand_operation op = NAND_OPERATION(instrs);
1158
+ struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
15981159
15991160 /* Drop the DATA_IN instruction if len is set to 0. */
16001161 if (!len)
....@@ -1603,9 +1164,9 @@
16031164 return nand_exec_op(chip, &op);
16041165 }
16051166
1606
- chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1167
+ chip->legacy.cmdfunc(chip, NAND_CMD_PARAM, page, -1);
16071168 for (i = 0; i < len; i++)
1608
- p[i] = chip->read_byte(mtd);
1169
+ p[i] = chip->legacy.read_byte(chip);
16091170
16101171 return 0;
16111172 }
....@@ -1639,9 +1200,9 @@
16391200 if (mtd->writesize <= 512)
16401201 return -ENOTSUPP;
16411202
1642
- if (chip->exec_op) {
1203
+ if (nand_has_exec_op(chip)) {
16431204 const struct nand_sdr_timings *sdr =
1644
- nand_get_sdr_timings(&chip->data_interface);
1205
+ nand_get_sdr_timings(nand_get_interface_config(chip));
16451206 u8 addrs[2] = {};
16461207 struct nand_op_instr instrs[] = {
16471208 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
....@@ -1650,7 +1211,7 @@
16501211 PSEC_TO_NSEC(sdr->tCCS_min)),
16511212 NAND_OP_DATA_IN(len, buf, 0),
16521213 };
1653
- struct nand_operation op = NAND_OPERATION(instrs);
1214
+ struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
16541215 int ret;
16551216
16561217 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
....@@ -1666,9 +1227,9 @@
16661227 return nand_exec_op(chip, &op);
16671228 }
16681229
1669
- chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1230
+ chip->legacy.cmdfunc(chip, NAND_CMD_RNDOUT, offset_in_page, -1);
16701231 if (len)
1671
- chip->read_buf(mtd, buf, len);
1232
+ chip->legacy.read_buf(chip, buf, len);
16721233
16731234 return 0;
16741235 }
....@@ -1698,14 +1259,14 @@
16981259 if (offset_in_oob + len > mtd->oobsize)
16991260 return -EINVAL;
17001261
1701
- if (chip->exec_op)
1262
+ if (nand_has_exec_op(chip))
17021263 return nand_read_page_op(chip, page,
17031264 mtd->writesize + offset_in_oob,
17041265 buf, len);
17051266
1706
- chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1267
+ chip->legacy.cmdfunc(chip, NAND_CMD_READOOB, offset_in_oob, page);
17071268 if (len)
1708
- chip->read_buf(mtd, buf, len);
1269
+ chip->legacy.read_buf(chip, buf, len);
17091270
17101271 return 0;
17111272 }
....@@ -1715,9 +1276,9 @@
17151276 unsigned int offset_in_page, const void *buf,
17161277 unsigned int len, bool prog)
17171278 {
1718
- struct mtd_info *mtd = nand_to_mtd(chip);
17191279 const struct nand_sdr_timings *sdr =
1720
- nand_get_sdr_timings(&chip->data_interface);
1280
+ nand_get_sdr_timings(nand_get_interface_config(chip));
1281
+ struct mtd_info *mtd = nand_to_mtd(chip);
17211282 u8 addrs[5] = {};
17221283 struct nand_op_instr instrs[] = {
17231284 /*
....@@ -1732,7 +1293,7 @@
17321293 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
17331294 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
17341295 };
1735
- struct nand_operation op = NAND_OPERATION(instrs);
1296
+ struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
17361297 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
17371298 int ret;
17381299 u8 status;
....@@ -1811,14 +1372,14 @@
18111372 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
18121373 return -EINVAL;
18131374
1814
- if (chip->exec_op)
1375
+ if (nand_has_exec_op(chip))
18151376 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
18161377 len, false);
18171378
1818
- chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1379
+ chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, page);
18191380
18201381 if (buf)
1821
- chip->write_buf(mtd, buf, len);
1382
+ chip->legacy.write_buf(chip, buf, len);
18221383
18231384 return 0;
18241385 }
....@@ -1835,19 +1396,18 @@
18351396 */
18361397 int nand_prog_page_end_op(struct nand_chip *chip)
18371398 {
1838
- struct mtd_info *mtd = nand_to_mtd(chip);
18391399 int ret;
18401400 u8 status;
18411401
1842
- if (chip->exec_op) {
1402
+ if (nand_has_exec_op(chip)) {
18431403 const struct nand_sdr_timings *sdr =
1844
- nand_get_sdr_timings(&chip->data_interface);
1404
+ nand_get_sdr_timings(nand_get_interface_config(chip));
18451405 struct nand_op_instr instrs[] = {
18461406 NAND_OP_CMD(NAND_CMD_PAGEPROG,
18471407 PSEC_TO_NSEC(sdr->tWB_max)),
18481408 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
18491409 };
1850
- struct nand_operation op = NAND_OPERATION(instrs);
1410
+ struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
18511411
18521412 ret = nand_exec_op(chip, &op);
18531413 if (ret)
....@@ -1857,8 +1417,8 @@
18571417 if (ret)
18581418 return ret;
18591419 } else {
1860
- chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1861
- ret = chip->waitfunc(mtd, chip);
1420
+ chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
1421
+ ret = chip->legacy.waitfunc(chip);
18621422 if (ret < 0)
18631423 return ret;
18641424
....@@ -1898,14 +1458,15 @@
18981458 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
18991459 return -EINVAL;
19001460
1901
- if (chip->exec_op) {
1461
+ if (nand_has_exec_op(chip)) {
19021462 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
19031463 len, true);
19041464 } else {
1905
- chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1906
- chip->write_buf(mtd, buf, len);
1907
- chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1908
- status = chip->waitfunc(mtd, chip);
1465
+ chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page,
1466
+ page);
1467
+ chip->legacy.write_buf(chip, buf, len);
1468
+ chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
1469
+ status = chip->legacy.waitfunc(chip);
19091470 }
19101471
19111472 if (status & NAND_STATUS_FAIL)
....@@ -1945,16 +1506,16 @@
19451506 if (mtd->writesize <= 512)
19461507 return -ENOTSUPP;
19471508
1948
- if (chip->exec_op) {
1509
+ if (nand_has_exec_op(chip)) {
19491510 const struct nand_sdr_timings *sdr =
1950
- nand_get_sdr_timings(&chip->data_interface);
1511
+ nand_get_sdr_timings(nand_get_interface_config(chip));
19511512 u8 addrs[2];
19521513 struct nand_op_instr instrs[] = {
19531514 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
19541515 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
19551516 NAND_OP_DATA_OUT(len, buf, 0),
19561517 };
1957
- struct nand_operation op = NAND_OPERATION(instrs);
1518
+ struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
19581519 int ret;
19591520
19601521 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
....@@ -1970,9 +1531,9 @@
19701531 return nand_exec_op(chip, &op);
19711532 }
19721533
1973
- chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1534
+ chip->legacy.cmdfunc(chip, NAND_CMD_RNDIN, offset_in_page, -1);
19741535 if (len)
1975
- chip->write_buf(mtd, buf, len);
1536
+ chip->legacy.write_buf(chip, buf, len);
19761537
19771538 return 0;
19781539 }
....@@ -1994,22 +1555,21 @@
19941555 int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
19951556 unsigned int len)
19961557 {
1997
- struct mtd_info *mtd = nand_to_mtd(chip);
19981558 unsigned int i;
19991559 u8 *id = buf;
20001560
20011561 if (len && !buf)
20021562 return -EINVAL;
20031563
2004
- if (chip->exec_op) {
1564
+ if (nand_has_exec_op(chip)) {
20051565 const struct nand_sdr_timings *sdr =
2006
- nand_get_sdr_timings(&chip->data_interface);
1566
+ nand_get_sdr_timings(nand_get_interface_config(chip));
20071567 struct nand_op_instr instrs[] = {
20081568 NAND_OP_CMD(NAND_CMD_READID, 0),
20091569 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
20101570 NAND_OP_8BIT_DATA_IN(len, buf, 0),
20111571 };
2012
- struct nand_operation op = NAND_OPERATION(instrs);
1572
+ struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
20131573
20141574 /* Drop the DATA_IN instruction if len is set to 0. */
20151575 if (!len)
....@@ -2018,10 +1578,10 @@
20181578 return nand_exec_op(chip, &op);
20191579 }
20201580
2021
- chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
1581
+ chip->legacy.cmdfunc(chip, NAND_CMD_READID, addr, -1);
20221582
20231583 for (i = 0; i < len; i++)
2024
- id[i] = chip->read_byte(mtd);
1584
+ id[i] = chip->legacy.read_byte(chip);
20251585
20261586 return 0;
20271587 }
....@@ -2040,17 +1600,15 @@
20401600 */
20411601 int nand_status_op(struct nand_chip *chip, u8 *status)
20421602 {
2043
- struct mtd_info *mtd = nand_to_mtd(chip);
2044
-
2045
- if (chip->exec_op) {
1603
+ if (nand_has_exec_op(chip)) {
20461604 const struct nand_sdr_timings *sdr =
2047
- nand_get_sdr_timings(&chip->data_interface);
1605
+ nand_get_sdr_timings(nand_get_interface_config(chip));
20481606 struct nand_op_instr instrs[] = {
20491607 NAND_OP_CMD(NAND_CMD_STATUS,
20501608 PSEC_TO_NSEC(sdr->tADL_min)),
20511609 NAND_OP_8BIT_DATA_IN(1, status, 0),
20521610 };
2053
- struct nand_operation op = NAND_OPERATION(instrs);
1611
+ struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
20541612
20551613 if (!status)
20561614 op.ninstrs--;
....@@ -2058,9 +1616,9 @@
20581616 return nand_exec_op(chip, &op);
20591617 }
20601618
2061
- chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
1619
+ chip->legacy.cmdfunc(chip, NAND_CMD_STATUS, -1, -1);
20621620 if (status)
2063
- *status = chip->read_byte(mtd);
1621
+ *status = chip->legacy.read_byte(chip);
20641622
20651623 return 0;
20661624 }
....@@ -2079,22 +1637,19 @@
20791637 */
20801638 int nand_exit_status_op(struct nand_chip *chip)
20811639 {
2082
- struct mtd_info *mtd = nand_to_mtd(chip);
2083
-
2084
- if (chip->exec_op) {
1640
+ if (nand_has_exec_op(chip)) {
20851641 struct nand_op_instr instrs[] = {
20861642 NAND_OP_CMD(NAND_CMD_READ0, 0),
20871643 };
2088
- struct nand_operation op = NAND_OPERATION(instrs);
1644
+ struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
20891645
20901646 return nand_exec_op(chip, &op);
20911647 }
20921648
2093
- chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
1649
+ chip->legacy.cmdfunc(chip, NAND_CMD_READ0, -1, -1);
20941650
20951651 return 0;
20961652 }
2097
-EXPORT_SYMBOL_GPL(nand_exit_status_op);
20981653
20991654 /**
21001655 * nand_erase_op - Do an erase operation
....@@ -2109,15 +1664,14 @@
21091664 */
21101665 int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
21111666 {
2112
- struct mtd_info *mtd = nand_to_mtd(chip);
21131667 unsigned int page = eraseblock <<
21141668 (chip->phys_erase_shift - chip->page_shift);
21151669 int ret;
21161670 u8 status;
21171671
2118
- if (chip->exec_op) {
1672
+ if (nand_has_exec_op(chip)) {
21191673 const struct nand_sdr_timings *sdr =
2120
- nand_get_sdr_timings(&chip->data_interface);
1674
+ nand_get_sdr_timings(nand_get_interface_config(chip));
21211675 u8 addrs[3] = { page, page >> 8, page >> 16 };
21221676 struct nand_op_instr instrs[] = {
21231677 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
....@@ -2126,7 +1680,7 @@
21261680 PSEC_TO_MSEC(sdr->tWB_max)),
21271681 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
21281682 };
2129
- struct nand_operation op = NAND_OPERATION(instrs);
1683
+ struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
21301684
21311685 if (chip->options & NAND_ROW_ADDR_3)
21321686 instrs[1].ctx.addr.naddrs++;
....@@ -2139,10 +1693,10 @@
21391693 if (ret)
21401694 return ret;
21411695 } else {
2142
- chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2143
- chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1696
+ chip->legacy.cmdfunc(chip, NAND_CMD_ERASE1, -1, page);
1697
+ chip->legacy.cmdfunc(chip, NAND_CMD_ERASE2, -1, -1);
21441698
2145
- ret = chip->waitfunc(mtd, chip);
1699
+ ret = chip->legacy.waitfunc(chip);
21461700 if (ret < 0)
21471701 return ret;
21481702
....@@ -2171,13 +1725,12 @@
21711725 static int nand_set_features_op(struct nand_chip *chip, u8 feature,
21721726 const void *data)
21731727 {
2174
- struct mtd_info *mtd = nand_to_mtd(chip);
21751728 const u8 *params = data;
21761729 int i, ret;
21771730
2178
- if (chip->exec_op) {
1731
+ if (nand_has_exec_op(chip)) {
21791732 const struct nand_sdr_timings *sdr =
2180
- nand_get_sdr_timings(&chip->data_interface);
1733
+ nand_get_sdr_timings(nand_get_interface_config(chip));
21811734 struct nand_op_instr instrs[] = {
21821735 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
21831736 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
....@@ -2185,16 +1738,16 @@
21851738 PSEC_TO_NSEC(sdr->tWB_max)),
21861739 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
21871740 };
2188
- struct nand_operation op = NAND_OPERATION(instrs);
1741
+ struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
21891742
21901743 return nand_exec_op(chip, &op);
21911744 }
21921745
2193
- chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1);
1746
+ chip->legacy.cmdfunc(chip, NAND_CMD_SET_FEATURES, feature, -1);
21941747 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2195
- chip->write_byte(mtd, params[i]);
1748
+ chip->legacy.write_byte(chip, params[i]);
21961749
2197
- ret = chip->waitfunc(mtd, chip);
1750
+ ret = chip->legacy.waitfunc(chip);
21981751 if (ret < 0)
21991752 return ret;
22001753
....@@ -2219,13 +1772,12 @@
22191772 static int nand_get_features_op(struct nand_chip *chip, u8 feature,
22201773 void *data)
22211774 {
2222
- struct mtd_info *mtd = nand_to_mtd(chip);
22231775 u8 *params = data;
22241776 int i;
22251777
2226
- if (chip->exec_op) {
1778
+ if (nand_has_exec_op(chip)) {
22271779 const struct nand_sdr_timings *sdr =
2228
- nand_get_sdr_timings(&chip->data_interface);
1780
+ nand_get_sdr_timings(nand_get_interface_config(chip));
22291781 struct nand_op_instr instrs[] = {
22301782 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
22311783 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
....@@ -2234,14 +1786,36 @@
22341786 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
22351787 data, 0),
22361788 };
2237
- struct nand_operation op = NAND_OPERATION(instrs);
1789
+ struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
22381790
22391791 return nand_exec_op(chip, &op);
22401792 }
22411793
2242
- chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
1794
+ chip->legacy.cmdfunc(chip, NAND_CMD_GET_FEATURES, feature, -1);
22431795 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2244
- params[i] = chip->read_byte(mtd);
1796
+ params[i] = chip->legacy.read_byte(chip);
1797
+
1798
+ return 0;
1799
+}
1800
+
1801
+static int nand_wait_rdy_op(struct nand_chip *chip, unsigned int timeout_ms,
1802
+ unsigned int delay_ns)
1803
+{
1804
+ if (nand_has_exec_op(chip)) {
1805
+ struct nand_op_instr instrs[] = {
1806
+ NAND_OP_WAIT_RDY(PSEC_TO_MSEC(timeout_ms),
1807
+ PSEC_TO_NSEC(delay_ns)),
1808
+ };
1809
+ struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
1810
+
1811
+ return nand_exec_op(chip, &op);
1812
+ }
1813
+
1814
+ /* Apply delay or wait for ready/busy pin */
1815
+ if (!chip->legacy.dev_ready)
1816
+ udelay(chip->legacy.chip_delay);
1817
+ else
1818
+ nand_wait_ready(chip);
22451819
22461820 return 0;
22471821 }
....@@ -2258,21 +1832,19 @@
22581832 */
22591833 int nand_reset_op(struct nand_chip *chip)
22601834 {
2261
- struct mtd_info *mtd = nand_to_mtd(chip);
2262
-
2263
- if (chip->exec_op) {
1835
+ if (nand_has_exec_op(chip)) {
22641836 const struct nand_sdr_timings *sdr =
2265
- nand_get_sdr_timings(&chip->data_interface);
1837
+ nand_get_sdr_timings(nand_get_interface_config(chip));
22661838 struct nand_op_instr instrs[] = {
22671839 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
22681840 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
22691841 };
2270
- struct nand_operation op = NAND_OPERATION(instrs);
1842
+ struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
22711843
22721844 return nand_exec_op(chip, &op);
22731845 }
22741846
2275
- chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1847
+ chip->legacy.cmdfunc(chip, NAND_CMD_RESET, -1, -1);
22761848
22771849 return 0;
22781850 }
....@@ -2284,6 +1856,8 @@
22841856 * @buf: buffer used to store the data
22851857 * @len: length of the buffer
22861858 * @force_8bit: force 8-bit bus access
1859
+ * @check_only: do not actually run the command, only checks if the
1860
+ * controller driver supports it
22871861 *
22881862 * This function does a raw data read on the bus. Usually used after launching
22891863 * another NAND operation like nand_read_page_op().
....@@ -2292,32 +1866,36 @@
22921866 * Returns 0 on success, a negative error code otherwise.
22931867 */
22941868 int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
2295
- bool force_8bit)
1869
+ bool force_8bit, bool check_only)
22961870 {
2297
- struct mtd_info *mtd = nand_to_mtd(chip);
2298
-
22991871 if (!len || !buf)
23001872 return -EINVAL;
23011873
2302
- if (chip->exec_op) {
1874
+ if (nand_has_exec_op(chip)) {
23031875 struct nand_op_instr instrs[] = {
23041876 NAND_OP_DATA_IN(len, buf, 0),
23051877 };
2306
- struct nand_operation op = NAND_OPERATION(instrs);
1878
+ struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
23071879
23081880 instrs[0].ctx.data.force_8bit = force_8bit;
23091881
1882
+ if (check_only)
1883
+ return nand_check_op(chip, &op);
1884
+
23101885 return nand_exec_op(chip, &op);
23111886 }
1887
+
1888
+ if (check_only)
1889
+ return 0;
23121890
23131891 if (force_8bit) {
23141892 u8 *p = buf;
23151893 unsigned int i;
23161894
23171895 for (i = 0; i < len; i++)
2318
- p[i] = chip->read_byte(mtd);
1896
+ p[i] = chip->legacy.read_byte(chip);
23191897 } else {
2320
- chip->read_buf(mtd, buf, len);
1898
+ chip->legacy.read_buf(chip, buf, len);
23211899 }
23221900
23231901 return 0;
....@@ -2340,16 +1918,14 @@
23401918 int nand_write_data_op(struct nand_chip *chip, const void *buf,
23411919 unsigned int len, bool force_8bit)
23421920 {
2343
- struct mtd_info *mtd = nand_to_mtd(chip);
2344
-
23451921 if (!len || !buf)
23461922 return -EINVAL;
23471923
2348
- if (chip->exec_op) {
1924
+ if (nand_has_exec_op(chip)) {
23491925 struct nand_op_instr instrs[] = {
23501926 NAND_OP_DATA_OUT(len, buf, 0),
23511927 };
2352
- struct nand_operation op = NAND_OPERATION(instrs);
1928
+ struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
23531929
23541930 instrs[0].ctx.data.force_8bit = force_8bit;
23551931
....@@ -2361,9 +1937,9 @@
23611937 unsigned int i;
23621938
23631939 for (i = 0; i < len; i++)
2364
- chip->write_byte(mtd, p[i]);
1940
+ chip->legacy.write_byte(chip, p[i]);
23651941 } else {
2366
- chip->write_buf(mtd, buf, len);
1942
+ chip->legacy.write_buf(chip, buf, len);
23671943 }
23681944
23691945 return 0;
....@@ -2532,7 +2108,7 @@
25322108 char *prefix = " ";
25332109 unsigned int i;
25342110
2535
- pr_debug("executing subop:\n");
2111
+ pr_debug("executing subop (CS%d):\n", ctx->subop.cs);
25362112
25372113 for (i = 0; i < ctx->ninstrs; i++) {
25382114 instr = &ctx->instrs[i];
....@@ -2540,35 +2116,7 @@
25402116 if (instr == &ctx->subop.instrs[0])
25412117 prefix = " ->";
25422118
2543
- switch (instr->type) {
2544
- case NAND_OP_CMD_INSTR:
2545
- pr_debug("%sCMD [0x%02x]\n", prefix,
2546
- instr->ctx.cmd.opcode);
2547
- break;
2548
- case NAND_OP_ADDR_INSTR:
2549
- pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2550
- instr->ctx.addr.naddrs,
2551
- instr->ctx.addr.naddrs < 64 ?
2552
- instr->ctx.addr.naddrs : 64,
2553
- instr->ctx.addr.addrs);
2554
- break;
2555
- case NAND_OP_DATA_IN_INSTR:
2556
- pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2557
- instr->ctx.data.len,
2558
- instr->ctx.data.force_8bit ?
2559
- ", force 8-bit" : "");
2560
- break;
2561
- case NAND_OP_DATA_OUT_INSTR:
2562
- pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2563
- instr->ctx.data.len,
2564
- instr->ctx.data.force_8bit ?
2565
- ", force 8-bit" : "");
2566
- break;
2567
- case NAND_OP_WAITRDY_INSTR:
2568
- pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2569
- instr->ctx.waitrdy.timeout_ms);
2570
- break;
2571
- }
2119
+ nand_op_trace(prefix, instr);
25722120
25732121 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
25742122 prefix = " ";
....@@ -2580,6 +2128,22 @@
25802128 /* NOP */
25812129 }
25822130 #endif
2131
+
2132
+static int nand_op_parser_cmp_ctx(const struct nand_op_parser_ctx *a,
2133
+ const struct nand_op_parser_ctx *b)
2134
+{
2135
+ if (a->subop.ninstrs < b->subop.ninstrs)
2136
+ return -1;
2137
+ else if (a->subop.ninstrs > b->subop.ninstrs)
2138
+ return 1;
2139
+
2140
+ if (a->subop.last_instr_end_off < b->subop.last_instr_end_off)
2141
+ return -1;
2142
+ else if (a->subop.last_instr_end_off > b->subop.last_instr_end_off)
2143
+ return 1;
2144
+
2145
+ return 0;
2146
+}
25832147
25842148 /**
25852149 * nand_op_parser_exec_op - exec_op parser
....@@ -2608,6 +2172,7 @@
26082172 const struct nand_operation *op, bool check_only)
26092173 {
26102174 struct nand_op_parser_ctx ctx = {
2175
+ .subop.cs = op->cs,
26112176 .subop.instrs = op->instrs,
26122177 .instrs = op->instrs,
26132178 .ninstrs = op->ninstrs,
....@@ -2615,30 +2180,38 @@
26152180 unsigned int i;
26162181
26172182 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2618
- int ret;
2183
+ const struct nand_op_parser_pattern *pattern;
2184
+ struct nand_op_parser_ctx best_ctx;
2185
+ int ret, best_pattern = -1;
26192186
26202187 for (i = 0; i < parser->npatterns; i++) {
2621
- const struct nand_op_parser_pattern *pattern;
2188
+ struct nand_op_parser_ctx test_ctx = ctx;
26222189
26232190 pattern = &parser->patterns[i];
2624
- if (!nand_op_parser_match_pat(pattern, &ctx))
2191
+ if (!nand_op_parser_match_pat(pattern, &test_ctx))
26252192 continue;
26262193
2627
- nand_op_parser_trace(&ctx);
2194
+ if (best_pattern >= 0 &&
2195
+ nand_op_parser_cmp_ctx(&test_ctx, &best_ctx) <= 0)
2196
+ continue;
26282197
2629
- if (check_only)
2630
- break;
2198
+ best_pattern = i;
2199
+ best_ctx = test_ctx;
2200
+ }
26312201
2202
+ if (best_pattern < 0) {
2203
+ pr_debug("->exec_op() parser: pattern not found!\n");
2204
+ return -ENOTSUPP;
2205
+ }
2206
+
2207
+ ctx = best_ctx;
2208
+ nand_op_parser_trace(&ctx);
2209
+
2210
+ if (!check_only) {
2211
+ pattern = &parser->patterns[best_pattern];
26322212 ret = pattern->exec(chip, &ctx.subop);
26332213 if (ret)
26342214 return ret;
2635
-
2636
- break;
2637
- }
2638
-
2639
- if (i == parser->npatterns) {
2640
- pr_debug("->exec_op() parser: pattern not found!\n");
2641
- return -ENOTSUPP;
26422215 }
26432216
26442217 /*
....@@ -2791,49 +2364,79 @@
27912364 * @chipnr: Internal die id
27922365 *
27932366 * Save the timings data structure, then apply SDR timings mode 0 (see
2794
- * nand_reset_data_interface for details), do the reset operation, and
2795
- * apply back the previous timings.
2367
+ * nand_reset_interface for details), do the reset operation, and apply
2368
+ * back the previous timings.
27962369 *
27972370 * Returns 0 on success, a negative error code otherwise.
27982371 */
27992372 int nand_reset(struct nand_chip *chip, int chipnr)
28002373 {
2801
- struct mtd_info *mtd = nand_to_mtd(chip);
2802
- struct nand_data_interface saved_data_intf = chip->data_interface;
28032374 int ret;
28042375
2805
- ret = nand_reset_data_interface(chip, chipnr);
2376
+ ret = nand_reset_interface(chip, chipnr);
28062377 if (ret)
28072378 return ret;
28082379
28092380 /*
28102381 * The CS line has to be released before we can apply the new NAND
2811
- * interface settings, hence this weird ->select_chip() dance.
2382
+ * interface settings, hence this weird nand_select_target()
2383
+ * nand_deselect_target() dance.
28122384 */
2813
- chip->select_chip(mtd, chipnr);
2385
+ nand_select_target(chip, chipnr);
28142386 ret = nand_reset_op(chip);
2815
- chip->select_chip(mtd, -1);
2387
+ nand_deselect_target(chip);
28162388 if (ret)
28172389 return ret;
28182390
2819
- /*
2820
- * A nand_reset_data_interface() put both the NAND chip and the NAND
2821
- * controller in timings mode 0. If the default mode for this chip is
2822
- * also 0, no need to proceed to the change again. Plus, at probe time,
2823
- * nand_setup_data_interface() uses ->set/get_features() which would
2824
- * fail anyway as the parameter page is not available yet.
2825
- */
2826
- if (!chip->onfi_timing_mode_default)
2827
- return 0;
2828
-
2829
- chip->data_interface = saved_data_intf;
2830
- ret = nand_setup_data_interface(chip, chipnr);
2391
+ ret = nand_setup_interface(chip, chipnr);
28312392 if (ret)
28322393 return ret;
28332394
28342395 return 0;
28352396 }
28362397 EXPORT_SYMBOL_GPL(nand_reset);
2398
+
2399
+/**
2400
+ * nand_get_features - wrapper to perform a GET_FEATURE
2401
+ * @chip: NAND chip info structure
2402
+ * @addr: feature address
2403
+ * @subfeature_param: the subfeature parameters, a four bytes array
2404
+ *
2405
+ * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2406
+ * operation cannot be handled.
2407
+ */
2408
+int nand_get_features(struct nand_chip *chip, int addr,
2409
+ u8 *subfeature_param)
2410
+{
2411
+ if (!nand_supports_get_features(chip, addr))
2412
+ return -ENOTSUPP;
2413
+
2414
+ if (chip->legacy.get_features)
2415
+ return chip->legacy.get_features(chip, addr, subfeature_param);
2416
+
2417
+ return nand_get_features_op(chip, addr, subfeature_param);
2418
+}
2419
+
2420
+/**
2421
+ * nand_set_features - wrapper to perform a SET_FEATURE
2422
+ * @chip: NAND chip info structure
2423
+ * @addr: feature address
2424
+ * @subfeature_param: the subfeature parameters, a four bytes array
2425
+ *
2426
+ * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2427
+ * operation cannot be handled.
2428
+ */
2429
+int nand_set_features(struct nand_chip *chip, int addr,
2430
+ u8 *subfeature_param)
2431
+{
2432
+ if (!nand_supports_set_features(chip, addr))
2433
+ return -ENOTSUPP;
2434
+
2435
+ if (chip->legacy.set_features)
2436
+ return chip->legacy.set_features(chip, addr, subfeature_param);
2437
+
2438
+ return nand_set_features_op(chip, addr, subfeature_param);
2439
+}
28372440
28382441 /**
28392442 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
....@@ -2968,7 +2571,6 @@
29682571
29692572 /**
29702573 * nand_read_page_raw_notsupp - dummy read raw page function
2971
- * @mtd: mtd info structure
29722574 * @chip: nand chip info structure
29732575 * @buf: buffer to store read data
29742576 * @oob_required: caller requires OOB data read to chip->oob_poi
....@@ -2976,16 +2578,14 @@
29762578 *
29772579 * Returns -ENOTSUPP unconditionally.
29782580 */
2979
-int nand_read_page_raw_notsupp(struct mtd_info *mtd, struct nand_chip *chip,
2980
- u8 *buf, int oob_required, int page)
2581
+int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf,
2582
+ int oob_required, int page)
29812583 {
29822584 return -ENOTSUPP;
29832585 }
2984
-EXPORT_SYMBOL(nand_read_page_raw_notsupp);
29852586
29862587 /**
29872588 * nand_read_page_raw - [INTERN] read raw page data without ecc
2988
- * @mtd: mtd info structure
29892589 * @chip: nand chip info structure
29902590 * @buf: buffer to store read data
29912591 * @oob_required: caller requires OOB data read to chip->oob_poi
....@@ -2993,9 +2593,10 @@
29932593 *
29942594 * Not for syndrome calculating ECC controllers, which use a special oob layout.
29952595 */
2996
-int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2997
- uint8_t *buf, int oob_required, int page)
2596
+int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required,
2597
+ int page)
29982598 {
2599
+ struct mtd_info *mtd = nand_to_mtd(chip);
29992600 int ret;
30002601
30012602 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
....@@ -3004,7 +2605,7 @@
30042605
30052606 if (oob_required) {
30062607 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
3007
- false);
2608
+ false, false);
30082609 if (ret)
30092610 return ret;
30102611 }
....@@ -3014,8 +2615,48 @@
30142615 EXPORT_SYMBOL(nand_read_page_raw);
30152616
30162617 /**
2618
+ * nand_monolithic_read_page_raw - Monolithic page read in raw mode
2619
+ * @chip: NAND chip info structure
2620
+ * @buf: buffer to store read data
2621
+ * @oob_required: caller requires OOB data read to chip->oob_poi
2622
+ * @page: page number to read
2623
+ *
2624
+ * This is a raw page read, ie. without any error detection/correction.
2625
+ * Monolithic means we are requesting all the relevant data (main plus
2626
+ * eventually OOB) to be loaded in the NAND cache and sent over the
2627
+ * bus (from the NAND chip to the NAND controller) in a single
2628
+ * operation. This is an alternative to nand_read_page_raw(), which
2629
+ * first reads the main data, and if the OOB data is requested too,
2630
+ * then reads more data on the bus.
2631
+ */
2632
+int nand_monolithic_read_page_raw(struct nand_chip *chip, u8 *buf,
2633
+ int oob_required, int page)
2634
+{
2635
+ struct mtd_info *mtd = nand_to_mtd(chip);
2636
+ unsigned int size = mtd->writesize;
2637
+ u8 *read_buf = buf;
2638
+ int ret;
2639
+
2640
+ if (oob_required) {
2641
+ size += mtd->oobsize;
2642
+
2643
+ if (buf != chip->data_buf)
2644
+ read_buf = nand_get_data_buf(chip);
2645
+ }
2646
+
2647
+ ret = nand_read_page_op(chip, page, 0, read_buf, size);
2648
+ if (ret)
2649
+ return ret;
2650
+
2651
+ if (buf != chip->data_buf)
2652
+ memcpy(buf, read_buf, mtd->writesize);
2653
+
2654
+ return 0;
2655
+}
2656
+EXPORT_SYMBOL(nand_monolithic_read_page_raw);
2657
+
2658
+/**
30172659 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
3018
- * @mtd: mtd info structure
30192660 * @chip: nand chip info structure
30202661 * @buf: buffer to store read data
30212662 * @oob_required: caller requires OOB data read to chip->oob_poi
....@@ -3023,10 +2664,10 @@
30232664 *
30242665 * We need a special oob layout and handling even when OOB isn't used.
30252666 */
3026
-static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
3027
- struct nand_chip *chip, uint8_t *buf,
2667
+static int nand_read_page_raw_syndrome(struct nand_chip *chip, uint8_t *buf,
30282668 int oob_required, int page)
30292669 {
2670
+ struct mtd_info *mtd = nand_to_mtd(chip);
30302671 int eccsize = chip->ecc.size;
30312672 int eccbytes = chip->ecc.bytes;
30322673 uint8_t *oob = chip->oob_poi;
....@@ -3037,7 +2678,7 @@
30372678 return ret;
30382679
30392680 for (steps = chip->ecc.steps; steps > 0; steps--) {
3040
- ret = nand_read_data_op(chip, buf, eccsize, false);
2681
+ ret = nand_read_data_op(chip, buf, eccsize, false, false);
30412682 if (ret)
30422683 return ret;
30432684
....@@ -3045,14 +2686,14 @@
30452686
30462687 if (chip->ecc.prepad) {
30472688 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3048
- false);
2689
+ false, false);
30492690 if (ret)
30502691 return ret;
30512692
30522693 oob += chip->ecc.prepad;
30532694 }
30542695
3055
- ret = nand_read_data_op(chip, oob, eccbytes, false);
2696
+ ret = nand_read_data_op(chip, oob, eccbytes, false, false);
30562697 if (ret)
30572698 return ret;
30582699
....@@ -3060,7 +2701,7 @@
30602701
30612702 if (chip->ecc.postpad) {
30622703 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3063
- false);
2704
+ false, false);
30642705 if (ret)
30652706 return ret;
30662707
....@@ -3070,7 +2711,7 @@
30702711
30712712 size = mtd->oobsize - (oob - chip->oob_poi);
30722713 if (size) {
3073
- ret = nand_read_data_op(chip, oob, size, false);
2714
+ ret = nand_read_data_op(chip, oob, size, false, false);
30742715 if (ret)
30752716 return ret;
30762717 }
....@@ -3080,15 +2721,15 @@
30802721
30812722 /**
30822723 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
3083
- * @mtd: mtd info structure
30842724 * @chip: nand chip info structure
30852725 * @buf: buffer to store read data
30862726 * @oob_required: caller requires OOB data read to chip->oob_poi
30872727 * @page: page number to read
30882728 */
3089
-static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
3090
- uint8_t *buf, int oob_required, int page)
2729
+static int nand_read_page_swecc(struct nand_chip *chip, uint8_t *buf,
2730
+ int oob_required, int page)
30912731 {
2732
+ struct mtd_info *mtd = nand_to_mtd(chip);
30922733 int i, eccsize = chip->ecc.size, ret;
30932734 int eccbytes = chip->ecc.bytes;
30942735 int eccsteps = chip->ecc.steps;
....@@ -3097,10 +2738,10 @@
30972738 uint8_t *ecc_code = chip->ecc.code_buf;
30982739 unsigned int max_bitflips = 0;
30992740
3100
- chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
2741
+ chip->ecc.read_page_raw(chip, buf, 1, page);
31012742
31022743 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
3103
- chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2744
+ chip->ecc.calculate(chip, p, &ecc_calc[i]);
31042745
31052746 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
31062747 chip->ecc.total);
....@@ -3113,7 +2754,7 @@
31132754 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
31142755 int stat;
31152756
3116
- stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
2757
+ stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
31172758 if (stat < 0) {
31182759 mtd->ecc_stats.failed++;
31192760 } else {
....@@ -3126,17 +2767,16 @@
31262767
31272768 /**
31282769 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
3129
- * @mtd: mtd info structure
31302770 * @chip: nand chip info structure
31312771 * @data_offs: offset of requested data within the page
31322772 * @readlen: data length
31332773 * @bufpoi: buffer to store read data
31342774 * @page: page number to read
31352775 */
3136
-static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
3137
- uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
3138
- int page)
2776
+static int nand_read_subpage(struct nand_chip *chip, uint32_t data_offs,
2777
+ uint32_t readlen, uint8_t *bufpoi, int page)
31392778 {
2779
+ struct mtd_info *mtd = nand_to_mtd(chip);
31402780 int start_step, end_step, num_steps, ret;
31412781 uint8_t *p;
31422782 int data_col_addr, i, gaps = 0;
....@@ -3165,7 +2805,7 @@
31652805
31662806 /* Calculate ECC */
31672807 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
3168
- chip->ecc.calculate(mtd, p, &chip->ecc.calc_buf[i]);
2808
+ chip->ecc.calculate(chip, p, &chip->ecc.calc_buf[i]);
31692809
31702810 /*
31712811 * The performance is faster if we position offsets according to
....@@ -3214,7 +2854,7 @@
32142854 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
32152855 int stat;
32162856
3217
- stat = chip->ecc.correct(mtd, p, &chip->ecc.code_buf[i],
2857
+ stat = chip->ecc.correct(chip, p, &chip->ecc.code_buf[i],
32182858 &chip->ecc.calc_buf[i]);
32192859 if (stat == -EBADMSG &&
32202860 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
....@@ -3238,7 +2878,6 @@
32382878
32392879 /**
32402880 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
3241
- * @mtd: mtd info structure
32422881 * @chip: nand chip info structure
32432882 * @buf: buffer to store read data
32442883 * @oob_required: caller requires OOB data read to chip->oob_poi
....@@ -3246,9 +2885,10 @@
32462885 *
32472886 * Not for syndrome calculating ECC controllers which need a special oob layout.
32482887 */
3249
-static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
3250
- uint8_t *buf, int oob_required, int page)
2888
+static int nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf,
2889
+ int oob_required, int page)
32512890 {
2891
+ struct mtd_info *mtd = nand_to_mtd(chip);
32522892 int i, eccsize = chip->ecc.size, ret;
32532893 int eccbytes = chip->ecc.bytes;
32542894 int eccsteps = chip->ecc.steps;
....@@ -3262,16 +2902,17 @@
32622902 return ret;
32632903
32642904 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3265
- chip->ecc.hwctl(mtd, NAND_ECC_READ);
2905
+ chip->ecc.hwctl(chip, NAND_ECC_READ);
32662906
3267
- ret = nand_read_data_op(chip, p, eccsize, false);
2907
+ ret = nand_read_data_op(chip, p, eccsize, false, false);
32682908 if (ret)
32692909 return ret;
32702910
3271
- chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2911
+ chip->ecc.calculate(chip, p, &ecc_calc[i]);
32722912 }
32732913
3274
- ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2914
+ ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false,
2915
+ false);
32752916 if (ret)
32762917 return ret;
32772918
....@@ -3286,77 +2927,7 @@
32862927 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
32872928 int stat;
32882929
3289
- stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
3290
- if (stat == -EBADMSG &&
3291
- (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3292
- /* check for empty pages with bitflips */
3293
- stat = nand_check_erased_ecc_chunk(p, eccsize,
3294
- &ecc_code[i], eccbytes,
3295
- NULL, 0,
3296
- chip->ecc.strength);
3297
- }
3298
-
3299
- if (stat < 0) {
3300
- mtd->ecc_stats.failed++;
3301
- } else {
3302
- mtd->ecc_stats.corrected += stat;
3303
- max_bitflips = max_t(unsigned int, max_bitflips, stat);
3304
- }
3305
- }
3306
- return max_bitflips;
3307
-}
3308
-
3309
-/**
3310
- * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
3311
- * @mtd: mtd info structure
3312
- * @chip: nand chip info structure
3313
- * @buf: buffer to store read data
3314
- * @oob_required: caller requires OOB data read to chip->oob_poi
3315
- * @page: page number to read
3316
- *
3317
- * Hardware ECC for large page chips, require OOB to be read first. For this
3318
- * ECC mode, the write_page method is re-used from ECC_HW. These methods
3319
- * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
3320
- * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
3321
- * the data area, by overwriting the NAND manufacturer bad block markings.
3322
- */
3323
-static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
3324
- struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
3325
-{
3326
- int i, eccsize = chip->ecc.size, ret;
3327
- int eccbytes = chip->ecc.bytes;
3328
- int eccsteps = chip->ecc.steps;
3329
- uint8_t *p = buf;
3330
- uint8_t *ecc_code = chip->ecc.code_buf;
3331
- uint8_t *ecc_calc = chip->ecc.calc_buf;
3332
- unsigned int max_bitflips = 0;
3333
-
3334
- /* Read the OOB area first */
3335
- ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
3336
- if (ret)
3337
- return ret;
3338
-
3339
- ret = nand_read_page_op(chip, page, 0, NULL, 0);
3340
- if (ret)
3341
- return ret;
3342
-
3343
- ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3344
- chip->ecc.total);
3345
- if (ret)
3346
- return ret;
3347
-
3348
- for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3349
- int stat;
3350
-
3351
- chip->ecc.hwctl(mtd, NAND_ECC_READ);
3352
-
3353
- ret = nand_read_data_op(chip, p, eccsize, false);
3354
- if (ret)
3355
- return ret;
3356
-
3357
- chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3358
-
3359
- stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
2930
+ stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
33602931 if (stat == -EBADMSG &&
33612932 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
33622933 /* check for empty pages with bitflips */
....@@ -3378,7 +2949,6 @@
33782949
33792950 /**
33802951 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
3381
- * @mtd: mtd info structure
33822952 * @chip: nand chip info structure
33832953 * @buf: buffer to store read data
33842954 * @oob_required: caller requires OOB data read to chip->oob_poi
....@@ -3387,9 +2957,10 @@
33872957 * The hw generator calculates the error syndrome automatically. Therefore we
33882958 * need a special oob layout and handling.
33892959 */
3390
-static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3391
- uint8_t *buf, int oob_required, int page)
2960
+static int nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf,
2961
+ int oob_required, int page)
33922962 {
2963
+ struct mtd_info *mtd = nand_to_mtd(chip);
33932964 int ret, i, eccsize = chip->ecc.size;
33942965 int eccbytes = chip->ecc.bytes;
33952966 int eccsteps = chip->ecc.steps;
....@@ -3405,34 +2976,34 @@
34052976 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
34062977 int stat;
34072978
3408
- chip->ecc.hwctl(mtd, NAND_ECC_READ);
2979
+ chip->ecc.hwctl(chip, NAND_ECC_READ);
34092980
3410
- ret = nand_read_data_op(chip, p, eccsize, false);
2981
+ ret = nand_read_data_op(chip, p, eccsize, false, false);
34112982 if (ret)
34122983 return ret;
34132984
34142985 if (chip->ecc.prepad) {
34152986 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3416
- false);
2987
+ false, false);
34172988 if (ret)
34182989 return ret;
34192990
34202991 oob += chip->ecc.prepad;
34212992 }
34222993
3423
- chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
2994
+ chip->ecc.hwctl(chip, NAND_ECC_READSYN);
34242995
3425
- ret = nand_read_data_op(chip, oob, eccbytes, false);
2996
+ ret = nand_read_data_op(chip, oob, eccbytes, false, false);
34262997 if (ret)
34272998 return ret;
34282999
3429
- stat = chip->ecc.correct(mtd, p, oob, NULL);
3000
+ stat = chip->ecc.correct(chip, p, oob, NULL);
34303001
34313002 oob += eccbytes;
34323003
34333004 if (chip->ecc.postpad) {
34343005 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3435
- false);
3006
+ false, false);
34363007 if (ret)
34373008 return ret;
34383009
....@@ -3460,7 +3031,7 @@
34603031 /* Calculate remaining oob bytes */
34613032 i = mtd->oobsize - (oob - chip->oob_poi);
34623033 if (i) {
3463
- ret = nand_read_data_op(chip, oob, i, false);
3034
+ ret = nand_read_data_op(chip, oob, i, false, false);
34643035 if (ret)
34653036 return ret;
34663037 }
....@@ -3470,15 +3041,15 @@
34703041
34713042 /**
34723043 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
3473
- * @mtd: mtd info structure
3044
+ * @chip: NAND chip object
34743045 * @oob: oob destination address
34753046 * @ops: oob ops structure
34763047 * @len: size of oob to transfer
34773048 */
3478
-static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
3049
+static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
34793050 struct mtd_oob_ops *ops, size_t len)
34803051 {
3481
- struct nand_chip *chip = mtd_to_nand(mtd);
3052
+ struct mtd_info *mtd = nand_to_mtd(chip);
34823053 int ret;
34833054
34843055 switch (ops->mode) {
....@@ -3502,54 +3073,63 @@
35023073
35033074 /**
35043075 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
3505
- * @mtd: MTD device structure
3076
+ * @chip: NAND chip object
35063077 * @retry_mode: the retry mode to use
35073078 *
35083079 * Some vendors supply a special command to shift the Vt threshold, to be used
35093080 * when there are too many bitflips in a page (i.e., ECC error). After setting
35103081 * a new threshold, the host should retry reading the page.
35113082 */
3512
-static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
3083
+static int nand_setup_read_retry(struct nand_chip *chip, int retry_mode)
35133084 {
3514
- struct nand_chip *chip = mtd_to_nand(mtd);
3515
-
35163085 pr_debug("setting READ RETRY mode %d\n", retry_mode);
35173086
35183087 if (retry_mode >= chip->read_retries)
35193088 return -EINVAL;
35203089
3521
- if (!chip->setup_read_retry)
3090
+ if (!chip->ops.setup_read_retry)
35223091 return -EOPNOTSUPP;
35233092
3524
- return chip->setup_read_retry(mtd, retry_mode);
3093
+ return chip->ops.setup_read_retry(chip, retry_mode);
3094
+}
3095
+
3096
+static void nand_wait_readrdy(struct nand_chip *chip)
3097
+{
3098
+ const struct nand_sdr_timings *sdr;
3099
+
3100
+ if (!(chip->options & NAND_NEED_READRDY))
3101
+ return;
3102
+
3103
+ sdr = nand_get_sdr_timings(nand_get_interface_config(chip));
3104
+ WARN_ON(nand_wait_rdy_op(chip, PSEC_TO_MSEC(sdr->tR_max), 0));
35253105 }
35263106
35273107 /**
35283108 * nand_do_read_ops - [INTERN] Read data with ECC
3529
- * @mtd: MTD device structure
3109
+ * @chip: NAND chip object
35303110 * @from: offset to read from
35313111 * @ops: oob ops structure
35323112 *
35333113 * Internal function. Called with chip held.
35343114 */
3535
-static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
3115
+static int nand_do_read_ops(struct nand_chip *chip, loff_t from,
35363116 struct mtd_oob_ops *ops)
35373117 {
35383118 int chipnr, page, realpage, col, bytes, aligned, oob_required;
3539
- struct nand_chip *chip = mtd_to_nand(mtd);
3119
+ struct mtd_info *mtd = nand_to_mtd(chip);
35403120 int ret = 0;
35413121 uint32_t readlen = ops->len;
35423122 uint32_t oobreadlen = ops->ooblen;
35433123 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
35443124
35453125 uint8_t *bufpoi, *oob, *buf;
3546
- int use_bufpoi;
3126
+ int use_bounce_buf;
35473127 unsigned int max_bitflips = 0;
35483128 int retry_mode = 0;
35493129 bool ecc_fail = false;
35503130
35513131 chipnr = (int)(from >> chip->chip_shift);
3552
- chip->select_chip(mtd, chipnr);
3132
+ nand_select_target(chip, chipnr);
35533133
35543134 realpage = (int)(from >> chip->page_shift);
35553135 page = realpage & chip->pagemask;
....@@ -3561,25 +3141,25 @@
35613141 oob_required = oob ? 1 : 0;
35623142
35633143 while (1) {
3564
- unsigned int ecc_failures = mtd->ecc_stats.failed;
3144
+ struct mtd_ecc_stats ecc_stats = mtd->ecc_stats;
35653145
35663146 bytes = min(mtd->writesize - col, readlen);
35673147 aligned = (bytes == mtd->writesize);
35683148
35693149 if (!aligned)
3570
- use_bufpoi = 1;
3571
- else if (chip->options & NAND_USE_BOUNCE_BUFFER)
3572
- use_bufpoi = !virt_addr_valid(buf) ||
3573
- !IS_ALIGNED((unsigned long)buf,
3574
- chip->buf_align);
3150
+ use_bounce_buf = 1;
3151
+ else if (chip->options & NAND_USES_DMA)
3152
+ use_bounce_buf = !virt_addr_valid(buf) ||
3153
+ !IS_ALIGNED((unsigned long)buf,
3154
+ chip->buf_align);
35753155 else
3576
- use_bufpoi = 0;
3156
+ use_bounce_buf = 0;
35773157
35783158 /* Is the current page in the buffer? */
3579
- if (realpage != chip->pagebuf || oob) {
3580
- bufpoi = use_bufpoi ? chip->data_buf : buf;
3159
+ if (realpage != chip->pagecache.page || oob) {
3160
+ bufpoi = use_bounce_buf ? chip->data_buf : buf;
35813161
3582
- if (use_bufpoi && aligned)
3162
+ if (use_bounce_buf && aligned)
35833163 pr_debug("%s: using read bounce buffer for buf@%p\n",
35843164 __func__, buf);
35853165
....@@ -3589,66 +3169,62 @@
35893169 * the read methods return max bitflips per ecc step.
35903170 */
35913171 if (unlikely(ops->mode == MTD_OPS_RAW))
3592
- ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
3172
+ ret = chip->ecc.read_page_raw(chip, bufpoi,
35933173 oob_required,
35943174 page);
35953175 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
35963176 !oob)
3597
- ret = chip->ecc.read_subpage(mtd, chip,
3598
- col, bytes, bufpoi,
3599
- page);
3177
+ ret = chip->ecc.read_subpage(chip, col, bytes,
3178
+ bufpoi, page);
36003179 else
3601
- ret = chip->ecc.read_page(mtd, chip, bufpoi,
3180
+ ret = chip->ecc.read_page(chip, bufpoi,
36023181 oob_required, page);
36033182 if (ret < 0) {
3604
- if (use_bufpoi)
3183
+ if (use_bounce_buf)
36053184 /* Invalidate page cache */
3606
- chip->pagebuf = -1;
3185
+ chip->pagecache.page = -1;
36073186 break;
36083187 }
36093188
3610
- /* Transfer not aligned data */
3611
- if (use_bufpoi) {
3189
+ /*
3190
+ * Copy back the data in the initial buffer when reading
3191
+ * partial pages or when a bounce buffer is required.
3192
+ */
3193
+ if (use_bounce_buf) {
36123194 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
3613
- !(mtd->ecc_stats.failed - ecc_failures) &&
3195
+ !(mtd->ecc_stats.failed - ecc_stats.failed) &&
36143196 (ops->mode != MTD_OPS_RAW)) {
3615
- chip->pagebuf = realpage;
3616
- chip->pagebuf_bitflips = ret;
3197
+ chip->pagecache.page = realpage;
3198
+ chip->pagecache.bitflips = ret;
36173199 } else {
36183200 /* Invalidate page cache */
3619
- chip->pagebuf = -1;
3201
+ chip->pagecache.page = -1;
36203202 }
3621
- memcpy(buf, chip->data_buf + col, bytes);
3203
+ memcpy(buf, bufpoi + col, bytes);
36223204 }
36233205
36243206 if (unlikely(oob)) {
36253207 int toread = min(oobreadlen, max_oobsize);
36263208
36273209 if (toread) {
3628
- oob = nand_transfer_oob(mtd,
3629
- oob, ops, toread);
3210
+ oob = nand_transfer_oob(chip, oob, ops,
3211
+ toread);
36303212 oobreadlen -= toread;
36313213 }
36323214 }
36333215
3634
- if (chip->options & NAND_NEED_READRDY) {
3635
- /* Apply delay or wait for ready/busy pin */
3636
- if (!chip->dev_ready)
3637
- udelay(chip->chip_delay);
3638
- else
3639
- nand_wait_ready(mtd);
3640
- }
3216
+ nand_wait_readrdy(chip);
36413217
3642
- if (mtd->ecc_stats.failed - ecc_failures) {
3218
+ if (mtd->ecc_stats.failed - ecc_stats.failed) {
36433219 if (retry_mode + 1 < chip->read_retries) {
36443220 retry_mode++;
3645
- ret = nand_setup_read_retry(mtd,
3221
+ ret = nand_setup_read_retry(chip,
36463222 retry_mode);
36473223 if (ret < 0)
36483224 break;
36493225
3650
- /* Reset failures; retry */
3651
- mtd->ecc_stats.failed = ecc_failures;
3226
+ /* Reset ecc_stats; retry */
3227
+ mtd->ecc_stats = ecc_stats;
36523228 goto read_retry;
36533229 } else {
36543230 /* No more retry modes; real failure */
....@@ -3662,14 +3238,14 @@
36623238 memcpy(buf, chip->data_buf + col, bytes);
36633239 buf += bytes;
36643240 max_bitflips = max_t(unsigned int, max_bitflips,
3665
- chip->pagebuf_bitflips);
3241
+ chip->pagecache.bitflips);
36663242 }
36673243
36683244 readlen -= bytes;
36693245
36703246 /* Reset to retry mode 0 */
36713247 if (retry_mode) {
3672
- ret = nand_setup_read_retry(mtd, 0);
3248
+ ret = nand_setup_read_retry(chip, 0);
36733249 if (ret < 0)
36743250 break;
36753251 retry_mode = 0;
....@@ -3687,11 +3263,11 @@
36873263 /* Check, if we cross a chip boundary */
36883264 if (!page) {
36893265 chipnr++;
3690
- chip->select_chip(mtd, -1);
3691
- chip->select_chip(mtd, chipnr);
3266
+ nand_deselect_target(chip);
3267
+ nand_select_target(chip, chipnr);
36923268 }
36933269 }
3694
- chip->select_chip(mtd, -1);
3270
+ nand_deselect_target(chip);
36953271
36963272 ops->retlen = ops->len - (size_t) readlen;
36973273 if (oob)
....@@ -3708,12 +3284,13 @@
37083284
37093285 /**
37103286 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
3711
- * @mtd: mtd info structure
37123287 * @chip: nand chip info structure
37133288 * @page: page number to read
37143289 */
3715
-int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
3290
+int nand_read_oob_std(struct nand_chip *chip, int page)
37163291 {
3292
+ struct mtd_info *mtd = nand_to_mtd(chip);
3293
+
37173294 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
37183295 }
37193296 EXPORT_SYMBOL(nand_read_oob_std);
....@@ -3721,13 +3298,12 @@
37213298 /**
37223299 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
37233300 * with syndromes
3724
- * @mtd: mtd info structure
37253301 * @chip: nand chip info structure
37263302 * @page: page number to read
37273303 */
3728
-int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3729
- int page)
3304
+static int nand_read_oob_syndrome(struct nand_chip *chip, int page)
37303305 {
3306
+ struct mtd_info *mtd = nand_to_mtd(chip);
37313307 int length = mtd->oobsize;
37323308 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
37333309 int eccsize = chip->ecc.size;
....@@ -3757,7 +3333,7 @@
37573333 sndrnd = 1;
37583334 toread = min_t(int, length, chunk);
37593335
3760
- ret = nand_read_data_op(chip, bufpoi, toread, false);
3336
+ ret = nand_read_data_op(chip, bufpoi, toread, false, false);
37613337 if (ret)
37623338 return ret;
37633339
....@@ -3765,23 +3341,23 @@
37653341 length -= toread;
37663342 }
37673343 if (length > 0) {
3768
- ret = nand_read_data_op(chip, bufpoi, length, false);
3344
+ ret = nand_read_data_op(chip, bufpoi, length, false, false);
37693345 if (ret)
37703346 return ret;
37713347 }
37723348
37733349 return 0;
37743350 }
3775
-EXPORT_SYMBOL(nand_read_oob_syndrome);
37763351
37773352 /**
37783353 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
3779
- * @mtd: mtd info structure
37803354 * @chip: nand chip info structure
37813355 * @page: page number to write
37823356 */
3783
-int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
3357
+int nand_write_oob_std(struct nand_chip *chip, int page)
37843358 {
3359
+ struct mtd_info *mtd = nand_to_mtd(chip);
3360
+
37853361 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
37863362 mtd->oobsize);
37873363 }
....@@ -3790,13 +3366,12 @@
37903366 /**
37913367 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
37923368 * with syndrome - only for large page flash
3793
- * @mtd: mtd info structure
37943369 * @chip: nand chip info structure
37953370 * @page: page number to write
37963371 */
3797
-int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3798
- int page)
3372
+static int nand_write_oob_syndrome(struct nand_chip *chip, int page)
37993373 {
3374
+ struct mtd_info *mtd = nand_to_mtd(chip);
38003375 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
38013376 int eccsize = chip->ecc.size, length = mtd->oobsize;
38023377 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
....@@ -3860,22 +3435,21 @@
38603435
38613436 return nand_prog_page_end_op(chip);
38623437 }
3863
-EXPORT_SYMBOL(nand_write_oob_syndrome);
38643438
38653439 /**
38663440 * nand_do_read_oob - [INTERN] NAND read out-of-band
3867
- * @mtd: MTD device structure
3441
+ * @chip: NAND chip object
38683442 * @from: offset to read from
38693443 * @ops: oob operations description structure
38703444 *
38713445 * NAND read out-of-band data from the spare area.
38723446 */
3873
-static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
3447
+static int nand_do_read_oob(struct nand_chip *chip, loff_t from,
38743448 struct mtd_oob_ops *ops)
38753449 {
3450
+ struct mtd_info *mtd = nand_to_mtd(chip);
38763451 unsigned int max_bitflips = 0;
38773452 int page, realpage, chipnr;
3878
- struct nand_chip *chip = mtd_to_nand(mtd);
38793453 struct mtd_ecc_stats stats;
38803454 int readlen = ops->ooblen;
38813455 int len;
....@@ -3890,7 +3464,7 @@
38903464 len = mtd_oobavail(mtd, ops);
38913465
38923466 chipnr = (int)(from >> chip->chip_shift);
3893
- chip->select_chip(mtd, chipnr);
3467
+ nand_select_target(chip, chipnr);
38943468
38953469 /* Shift to get page */
38963470 realpage = (int)(from >> chip->page_shift);
....@@ -3898,23 +3472,17 @@
38983472
38993473 while (1) {
39003474 if (ops->mode == MTD_OPS_RAW)
3901
- ret = chip->ecc.read_oob_raw(mtd, chip, page);
3475
+ ret = chip->ecc.read_oob_raw(chip, page);
39023476 else
3903
- ret = chip->ecc.read_oob(mtd, chip, page);
3477
+ ret = chip->ecc.read_oob(chip, page);
39043478
39053479 if (ret < 0)
39063480 break;
39073481
39083482 len = min(len, readlen);
3909
- buf = nand_transfer_oob(mtd, buf, ops, len);
3483
+ buf = nand_transfer_oob(chip, buf, ops, len);
39103484
3911
- if (chip->options & NAND_NEED_READRDY) {
3912
- /* Apply delay or wait for ready/busy pin */
3913
- if (!chip->dev_ready)
3914
- udelay(chip->chip_delay);
3915
- else
3916
- nand_wait_ready(mtd);
3917
- }
3485
+ nand_wait_readrdy(chip);
39183486
39193487 max_bitflips = max_t(unsigned int, max_bitflips, ret);
39203488
....@@ -3929,11 +3497,11 @@
39293497 /* Check, if we cross a chip boundary */
39303498 if (!page) {
39313499 chipnr++;
3932
- chip->select_chip(mtd, -1);
3933
- chip->select_chip(mtd, chipnr);
3500
+ nand_deselect_target(chip);
3501
+ nand_select_target(chip, chipnr);
39343502 }
39353503 }
3936
- chip->select_chip(mtd, -1);
3504
+ nand_deselect_target(chip);
39373505
39383506 ops->oobretlen = ops->ooblen - readlen;
39393507
....@@ -3957,6 +3525,7 @@
39573525 static int nand_read_oob(struct mtd_info *mtd, loff_t from,
39583526 struct mtd_oob_ops *ops)
39593527 {
3528
+ struct nand_chip *chip = mtd_to_nand(mtd);
39603529 int ret;
39613530
39623531 ops->retlen = 0;
....@@ -3966,20 +3535,19 @@
39663535 ops->mode != MTD_OPS_RAW)
39673536 return -ENOTSUPP;
39683537
3969
- nand_get_device(mtd, FL_READING);
3538
+ nand_get_device(chip);
39703539
39713540 if (!ops->datbuf)
3972
- ret = nand_do_read_oob(mtd, from, ops);
3541
+ ret = nand_do_read_oob(chip, from, ops);
39733542 else
3974
- ret = nand_do_read_ops(mtd, from, ops);
3543
+ ret = nand_do_read_ops(chip, from, ops);
39753544
3976
- nand_release_device(mtd);
3545
+ nand_release_device(chip);
39773546 return ret;
39783547 }
39793548
39803549 /**
39813550 * nand_write_page_raw_notsupp - dummy raw page write function
3982
- * @mtd: mtd info structure
39833551 * @chip: nand chip info structure
39843552 * @buf: data buffer
39853553 * @oob_required: must write chip->oob_poi to OOB
....@@ -3987,16 +3555,14 @@
39873555 *
39883556 * Returns -ENOTSUPP unconditionally.
39893557 */
3990
-int nand_write_page_raw_notsupp(struct mtd_info *mtd, struct nand_chip *chip,
3991
- const u8 *buf, int oob_required, int page)
3558
+int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf,
3559
+ int oob_required, int page)
39923560 {
39933561 return -ENOTSUPP;
39943562 }
3995
-EXPORT_SYMBOL(nand_write_page_raw_notsupp);
39963563
39973564 /**
39983565 * nand_write_page_raw - [INTERN] raw page write function
3999
- * @mtd: mtd info structure
40003566 * @chip: nand chip info structure
40013567 * @buf: data buffer
40023568 * @oob_required: must write chip->oob_poi to OOB
....@@ -4004,9 +3570,10 @@
40043570 *
40053571 * Not for syndrome calculating ECC controllers, which use a special oob layout.
40063572 */
4007
-int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
4008
- const uint8_t *buf, int oob_required, int page)
3573
+int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
3574
+ int oob_required, int page)
40093575 {
3576
+ struct mtd_info *mtd = nand_to_mtd(chip);
40103577 int ret;
40113578
40123579 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
....@@ -4025,8 +3592,43 @@
40253592 EXPORT_SYMBOL(nand_write_page_raw);
40263593
40273594 /**
3595
+ * nand_monolithic_write_page_raw - Monolithic page write in raw mode
3596
+ * @chip: NAND chip info structure
3597
+ * @buf: data buffer to write
3598
+ * @oob_required: must write chip->oob_poi to OOB
3599
+ * @page: page number to write
3600
+ *
3601
+ * This is a raw page write, ie. without any error detection/correction.
3602
+ * Monolithic means we are requesting all the relevant data (main plus
3603
+ * eventually OOB) to be sent over the bus and effectively programmed
3604
+ * into the NAND chip arrays in a single operation. This is an
3605
+ * alternative to nand_write_page_raw(), which first sends the main
3606
+ * data, then eventually send the OOB data by latching more data
3607
+ * cycles on the NAND bus, and finally sends the program command to
3608
+ * synchronyze the NAND chip cache.
3609
+ */
3610
+int nand_monolithic_write_page_raw(struct nand_chip *chip, const u8 *buf,
3611
+ int oob_required, int page)
3612
+{
3613
+ struct mtd_info *mtd = nand_to_mtd(chip);
3614
+ unsigned int size = mtd->writesize;
3615
+ u8 *write_buf = (u8 *)buf;
3616
+
3617
+ if (oob_required) {
3618
+ size += mtd->oobsize;
3619
+
3620
+ if (buf != chip->data_buf) {
3621
+ write_buf = nand_get_data_buf(chip);
3622
+ memcpy(write_buf, buf, mtd->writesize);
3623
+ }
3624
+ }
3625
+
3626
+ return nand_prog_page_op(chip, page, 0, write_buf, size);
3627
+}
3628
+EXPORT_SYMBOL(nand_monolithic_write_page_raw);
3629
+
3630
+/**
40283631 * nand_write_page_raw_syndrome - [INTERN] raw page write function
4029
- * @mtd: mtd info structure
40303632 * @chip: nand chip info structure
40313633 * @buf: data buffer
40323634 * @oob_required: must write chip->oob_poi to OOB
....@@ -4034,11 +3636,11 @@
40343636 *
40353637 * We need a special oob layout and handling even when ECC isn't checked.
40363638 */
4037
-static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
4038
- struct nand_chip *chip,
3639
+static int nand_write_page_raw_syndrome(struct nand_chip *chip,
40393640 const uint8_t *buf, int oob_required,
40403641 int page)
40413642 {
3643
+ struct mtd_info *mtd = nand_to_mtd(chip);
40423644 int eccsize = chip->ecc.size;
40433645 int eccbytes = chip->ecc.bytes;
40443646 uint8_t *oob = chip->oob_poi;
....@@ -4091,16 +3693,15 @@
40913693 }
40923694 /**
40933695 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
4094
- * @mtd: mtd info structure
40953696 * @chip: nand chip info structure
40963697 * @buf: data buffer
40973698 * @oob_required: must write chip->oob_poi to OOB
40983699 * @page: page number to write
40993700 */
4100
-static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
4101
- const uint8_t *buf, int oob_required,
4102
- int page)
3701
+static int nand_write_page_swecc(struct nand_chip *chip, const uint8_t *buf,
3702
+ int oob_required, int page)
41033703 {
3704
+ struct mtd_info *mtd = nand_to_mtd(chip);
41043705 int i, eccsize = chip->ecc.size, ret;
41053706 int eccbytes = chip->ecc.bytes;
41063707 int eccsteps = chip->ecc.steps;
....@@ -4109,28 +3710,27 @@
41093710
41103711 /* Software ECC calculation */
41113712 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
4112
- chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3713
+ chip->ecc.calculate(chip, p, &ecc_calc[i]);
41133714
41143715 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
41153716 chip->ecc.total);
41163717 if (ret)
41173718 return ret;
41183719
4119
- return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
3720
+ return chip->ecc.write_page_raw(chip, buf, 1, page);
41203721 }
41213722
41223723 /**
41233724 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
4124
- * @mtd: mtd info structure
41253725 * @chip: nand chip info structure
41263726 * @buf: data buffer
41273727 * @oob_required: must write chip->oob_poi to OOB
41283728 * @page: page number to write
41293729 */
4130
-static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
4131
- const uint8_t *buf, int oob_required,
4132
- int page)
3730
+static int nand_write_page_hwecc(struct nand_chip *chip, const uint8_t *buf,
3731
+ int oob_required, int page)
41333732 {
3733
+ struct mtd_info *mtd = nand_to_mtd(chip);
41343734 int i, eccsize = chip->ecc.size, ret;
41353735 int eccbytes = chip->ecc.bytes;
41363736 int eccsteps = chip->ecc.steps;
....@@ -4142,13 +3742,13 @@
41423742 return ret;
41433743
41443744 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
4145
- chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
3745
+ chip->ecc.hwctl(chip, NAND_ECC_WRITE);
41463746
41473747 ret = nand_write_data_op(chip, p, eccsize, false);
41483748 if (ret)
41493749 return ret;
41503750
4151
- chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3751
+ chip->ecc.calculate(chip, p, &ecc_calc[i]);
41523752 }
41533753
41543754 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
....@@ -4166,7 +3766,6 @@
41663766
41673767 /**
41683768 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
4169
- * @mtd: mtd info structure
41703769 * @chip: nand chip info structure
41713770 * @offset: column address of subpage within the page
41723771 * @data_len: data length
....@@ -4174,11 +3773,11 @@
41743773 * @oob_required: must write chip->oob_poi to OOB
41753774 * @page: page number to write
41763775 */
4177
-static int nand_write_subpage_hwecc(struct mtd_info *mtd,
4178
- struct nand_chip *chip, uint32_t offset,
4179
- uint32_t data_len, const uint8_t *buf,
4180
- int oob_required, int page)
3776
+static int nand_write_subpage_hwecc(struct nand_chip *chip, uint32_t offset,
3777
+ uint32_t data_len, const uint8_t *buf,
3778
+ int oob_required, int page)
41813779 {
3780
+ struct mtd_info *mtd = nand_to_mtd(chip);
41823781 uint8_t *oob_buf = chip->oob_poi;
41833782 uint8_t *ecc_calc = chip->ecc.calc_buf;
41843783 int ecc_size = chip->ecc.size;
....@@ -4195,7 +3794,7 @@
41953794
41963795 for (step = 0; step < ecc_steps; step++) {
41973796 /* configure controller for WRITE access */
4198
- chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
3797
+ chip->ecc.hwctl(chip, NAND_ECC_WRITE);
41993798
42003799 /* write data (untouched subpages already masked by 0xFF) */
42013800 ret = nand_write_data_op(chip, buf, ecc_size, false);
....@@ -4206,7 +3805,7 @@
42063805 if ((step < start_step) || (step > end_step))
42073806 memset(ecc_calc, 0xff, ecc_bytes);
42083807 else
4209
- chip->ecc.calculate(mtd, buf, ecc_calc);
3808
+ chip->ecc.calculate(chip, buf, ecc_calc);
42103809
42113810 /* mask OOB of un-touched subpages by padding 0xFF */
42123811 /* if oob_required, preserve OOB metadata of written subpage */
....@@ -4237,7 +3836,6 @@
42373836
42383837 /**
42393838 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
4240
- * @mtd: mtd info structure
42413839 * @chip: nand chip info structure
42423840 * @buf: data buffer
42433841 * @oob_required: must write chip->oob_poi to OOB
....@@ -4246,11 +3844,10 @@
42463844 * The hw generator calculates the error syndrome automatically. Therefore we
42473845 * need a special oob layout and handling.
42483846 */
4249
-static int nand_write_page_syndrome(struct mtd_info *mtd,
4250
- struct nand_chip *chip,
4251
- const uint8_t *buf, int oob_required,
4252
- int page)
3847
+static int nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf,
3848
+ int oob_required, int page)
42533849 {
3850
+ struct mtd_info *mtd = nand_to_mtd(chip);
42543851 int i, eccsize = chip->ecc.size;
42553852 int eccbytes = chip->ecc.bytes;
42563853 int eccsteps = chip->ecc.steps;
....@@ -4263,7 +3860,7 @@
42633860 return ret;
42643861
42653862 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
4266
- chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
3863
+ chip->ecc.hwctl(chip, NAND_ECC_WRITE);
42673864
42683865 ret = nand_write_data_op(chip, p, eccsize, false);
42693866 if (ret)
....@@ -4278,7 +3875,7 @@
42783875 oob += chip->ecc.prepad;
42793876 }
42803877
4281
- chip->ecc.calculate(mtd, p, oob);
3878
+ chip->ecc.calculate(chip, p, oob);
42823879
42833880 ret = nand_write_data_op(chip, oob, eccbytes, false);
42843881 if (ret)
....@@ -4309,7 +3906,6 @@
43093906
43103907 /**
43113908 * nand_write_page - write one page
4312
- * @mtd: MTD device structure
43133909 * @chip: NAND chip descriptor
43143910 * @offset: address offset within the page
43153911 * @data_len: length of actual data to be written
....@@ -4318,10 +3914,11 @@
43183914 * @page: page number to write
43193915 * @raw: use _raw version of write_page
43203916 */
4321
-static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
4322
- uint32_t offset, int data_len, const uint8_t *buf,
4323
- int oob_required, int page, int raw)
3917
+static int nand_write_page(struct nand_chip *chip, uint32_t offset,
3918
+ int data_len, const uint8_t *buf, int oob_required,
3919
+ int page, int raw)
43243920 {
3921
+ struct mtd_info *mtd = nand_to_mtd(chip);
43253922 int status, subpage;
43263923
43273924 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
....@@ -4331,14 +3928,13 @@
43313928 subpage = 0;
43323929
43333930 if (unlikely(raw))
4334
- status = chip->ecc.write_page_raw(mtd, chip, buf,
4335
- oob_required, page);
3931
+ status = chip->ecc.write_page_raw(chip, buf, oob_required,
3932
+ page);
43363933 else if (subpage)
4337
- status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
4338
- buf, oob_required, page);
3934
+ status = chip->ecc.write_subpage(chip, offset, data_len, buf,
3935
+ oob_required, page);
43393936 else
4340
- status = chip->ecc.write_page(mtd, chip, buf, oob_required,
4341
- page);
3937
+ status = chip->ecc.write_page(chip, buf, oob_required, page);
43423938
43433939 if (status < 0)
43443940 return status;
....@@ -4346,59 +3942,21 @@
43463942 return 0;
43473943 }
43483944
4349
-/**
4350
- * nand_fill_oob - [INTERN] Transfer client buffer to oob
4351
- * @mtd: MTD device structure
4352
- * @oob: oob data buffer
4353
- * @len: oob data write length
4354
- * @ops: oob ops structure
4355
- */
4356
-static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
4357
- struct mtd_oob_ops *ops)
4358
-{
4359
- struct nand_chip *chip = mtd_to_nand(mtd);
4360
- int ret;
4361
-
4362
- /*
4363
- * Initialise to all 0xFF, to avoid the possibility of left over OOB
4364
- * data from a previous OOB read.
4365
- */
4366
- memset(chip->oob_poi, 0xff, mtd->oobsize);
4367
-
4368
- switch (ops->mode) {
4369
-
4370
- case MTD_OPS_PLACE_OOB:
4371
- case MTD_OPS_RAW:
4372
- memcpy(chip->oob_poi + ops->ooboffs, oob, len);
4373
- return oob + len;
4374
-
4375
- case MTD_OPS_AUTO_OOB:
4376
- ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
4377
- ops->ooboffs, len);
4378
- BUG_ON(ret);
4379
- return oob + len;
4380
-
4381
- default:
4382
- BUG();
4383
- }
4384
- return NULL;
4385
-}
4386
-
43873945 #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
43883946
43893947 /**
43903948 * nand_do_write_ops - [INTERN] NAND write with ECC
4391
- * @mtd: MTD device structure
3949
+ * @chip: NAND chip object
43923950 * @to: offset to write to
43933951 * @ops: oob operations description structure
43943952 *
43953953 * NAND write with ECC.
43963954 */
4397
-static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
3955
+static int nand_do_write_ops(struct nand_chip *chip, loff_t to,
43983956 struct mtd_oob_ops *ops)
43993957 {
3958
+ struct mtd_info *mtd = nand_to_mtd(chip);
44003959 int chipnr, realpage, page, column;
4401
- struct nand_chip *chip = mtd_to_nand(mtd);
44023960 uint32_t writelen = ops->len;
44033961
44043962 uint32_t oobwritelen = ops->ooblen;
....@@ -4423,10 +3981,10 @@
44233981 column = to & (mtd->writesize - 1);
44243982
44253983 chipnr = (int)(to >> chip->chip_shift);
4426
- chip->select_chip(mtd, chipnr);
3984
+ nand_select_target(chip, chipnr);
44273985
44283986 /* Check, if it is write protected */
4429
- if (nand_check_wp(mtd)) {
3987
+ if (nand_check_wp(chip)) {
44303988 ret = -EIO;
44313989 goto err_out;
44323990 }
....@@ -4435,9 +3993,9 @@
44353993 page = realpage & chip->pagemask;
44363994
44373995 /* Invalidate the page cache, when we write to the cached page */
4438
- if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
4439
- ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
4440
- chip->pagebuf = -1;
3996
+ if (to <= ((loff_t)chip->pagecache.page << chip->page_shift) &&
3997
+ ((loff_t)chip->pagecache.page << chip->page_shift) < (to + ops->len))
3998
+ chip->pagecache.page = -1;
44413999
44424000 /* Don't allow multipage oob writes with offset */
44434001 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
....@@ -4448,40 +4006,42 @@
44484006 while (1) {
44494007 int bytes = mtd->writesize;
44504008 uint8_t *wbuf = buf;
4451
- int use_bufpoi;
4009
+ int use_bounce_buf;
44524010 int part_pagewr = (column || writelen < mtd->writesize);
44534011
44544012 if (part_pagewr)
4455
- use_bufpoi = 1;
4456
- else if (chip->options & NAND_USE_BOUNCE_BUFFER)
4457
- use_bufpoi = !virt_addr_valid(buf) ||
4458
- !IS_ALIGNED((unsigned long)buf,
4459
- chip->buf_align);
4013
+ use_bounce_buf = 1;
4014
+ else if (chip->options & NAND_USES_DMA)
4015
+ use_bounce_buf = !virt_addr_valid(buf) ||
4016
+ !IS_ALIGNED((unsigned long)buf,
4017
+ chip->buf_align);
44604018 else
4461
- use_bufpoi = 0;
4019
+ use_bounce_buf = 0;
44624020
4463
- /* Partial page write?, or need to use bounce buffer */
4464
- if (use_bufpoi) {
4021
+ /*
4022
+ * Copy the data from the initial buffer when doing partial page
4023
+ * writes or when a bounce buffer is required.
4024
+ */
4025
+ if (use_bounce_buf) {
44654026 pr_debug("%s: using write bounce buffer for buf@%p\n",
44664027 __func__, buf);
44674028 if (part_pagewr)
44684029 bytes = min_t(int, bytes - column, writelen);
4469
- chip->pagebuf = -1;
4470
- memset(chip->data_buf, 0xff, mtd->writesize);
4471
- memcpy(&chip->data_buf[column], buf, bytes);
4472
- wbuf = chip->data_buf;
4030
+ wbuf = nand_get_data_buf(chip);
4031
+ memset(wbuf, 0xff, mtd->writesize);
4032
+ memcpy(&wbuf[column], buf, bytes);
44734033 }
44744034
44754035 if (unlikely(oob)) {
44764036 size_t len = min(oobwritelen, oobmaxlen);
4477
- oob = nand_fill_oob(mtd, oob, len, ops);
4037
+ oob = nand_fill_oob(chip, oob, len, ops);
44784038 oobwritelen -= len;
44794039 } else {
44804040 /* We still need to erase leftover OOB data */
44814041 memset(chip->oob_poi, 0xff, mtd->oobsize);
44824042 }
44834043
4484
- ret = nand_write_page(mtd, chip, column, bytes, wbuf,
4044
+ ret = nand_write_page(chip, column, bytes, wbuf,
44854045 oob_required, page,
44864046 (ops->mode == MTD_OPS_RAW));
44874047 if (ret)
....@@ -4499,8 +4059,8 @@
44994059 /* Check, if we cross a chip boundary */
45004060 if (!page) {
45014061 chipnr++;
4502
- chip->select_chip(mtd, -1);
4503
- chip->select_chip(mtd, chipnr);
4062
+ nand_deselect_target(chip);
4063
+ nand_select_target(chip, chipnr);
45044064 }
45054065 }
45064066
....@@ -4509,7 +4069,7 @@
45094069 ops->oobretlen = ops->ooblen;
45104070
45114071 err_out:
4512
- chip->select_chip(mtd, -1);
4072
+ nand_deselect_target(chip);
45134073 return ret;
45144074 }
45154075
....@@ -4532,91 +4092,20 @@
45324092 struct mtd_oob_ops ops;
45334093 int ret;
45344094
4535
- /* Grab the device */
4536
- panic_nand_get_device(chip, mtd, FL_WRITING);
4537
-
4538
- chip->select_chip(mtd, chipnr);
4095
+ nand_select_target(chip, chipnr);
45394096
45404097 /* Wait for the device to get ready */
4541
- panic_nand_wait(mtd, chip, 400);
4098
+ panic_nand_wait(chip, 400);
45424099
45434100 memset(&ops, 0, sizeof(ops));
45444101 ops.len = len;
45454102 ops.datbuf = (uint8_t *)buf;
45464103 ops.mode = MTD_OPS_PLACE_OOB;
45474104
4548
- ret = nand_do_write_ops(mtd, to, &ops);
4105
+ ret = nand_do_write_ops(chip, to, &ops);
45494106
45504107 *retlen = ops.retlen;
45514108 return ret;
4552
-}
4553
-
4554
-/**
4555
- * nand_do_write_oob - [MTD Interface] NAND write out-of-band
4556
- * @mtd: MTD device structure
4557
- * @to: offset to write to
4558
- * @ops: oob operation description structure
4559
- *
4560
- * NAND write out-of-band.
4561
- */
4562
-static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
4563
- struct mtd_oob_ops *ops)
4564
-{
4565
- int chipnr, page, status, len;
4566
- struct nand_chip *chip = mtd_to_nand(mtd);
4567
-
4568
- pr_debug("%s: to = 0x%08x, len = %i\n",
4569
- __func__, (unsigned int)to, (int)ops->ooblen);
4570
-
4571
- len = mtd_oobavail(mtd, ops);
4572
-
4573
- /* Do not allow write past end of page */
4574
- if ((ops->ooboffs + ops->ooblen) > len) {
4575
- pr_debug("%s: attempt to write past end of page\n",
4576
- __func__);
4577
- return -EINVAL;
4578
- }
4579
-
4580
- chipnr = (int)(to >> chip->chip_shift);
4581
-
4582
- /*
4583
- * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
4584
- * of my DiskOnChip 2000 test units) will clear the whole data page too
4585
- * if we don't do this. I have no clue why, but I seem to have 'fixed'
4586
- * it in the doc2000 driver in August 1999. dwmw2.
4587
- */
4588
- nand_reset(chip, chipnr);
4589
-
4590
- chip->select_chip(mtd, chipnr);
4591
-
4592
- /* Shift to get page */
4593
- page = (int)(to >> chip->page_shift);
4594
-
4595
- /* Check, if it is write protected */
4596
- if (nand_check_wp(mtd)) {
4597
- chip->select_chip(mtd, -1);
4598
- return -EROFS;
4599
- }
4600
-
4601
- /* Invalidate the page cache, if we write to the cached page */
4602
- if (page == chip->pagebuf)
4603
- chip->pagebuf = -1;
4604
-
4605
- nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
4606
-
4607
- if (ops->mode == MTD_OPS_RAW)
4608
- status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
4609
- else
4610
- status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
4611
-
4612
- chip->select_chip(mtd, -1);
4613
-
4614
- if (status)
4615
- return status;
4616
-
4617
- ops->oobretlen = ops->ooblen;
4618
-
4619
- return 0;
46204109 }
46214110
46224111 /**
....@@ -4628,11 +4117,12 @@
46284117 static int nand_write_oob(struct mtd_info *mtd, loff_t to,
46294118 struct mtd_oob_ops *ops)
46304119 {
4631
- int ret = -ENOTSUPP;
4120
+ struct nand_chip *chip = mtd_to_nand(mtd);
4121
+ int ret = 0;
46324122
46334123 ops->retlen = 0;
46344124
4635
- nand_get_device(mtd, FL_WRITING);
4125
+ nand_get_device(chip);
46364126
46374127 switch (ops->mode) {
46384128 case MTD_OPS_PLACE_OOB:
....@@ -4645,31 +4135,13 @@
46454135 }
46464136
46474137 if (!ops->datbuf)
4648
- ret = nand_do_write_oob(mtd, to, ops);
4138
+ ret = nand_do_write_oob(chip, to, ops);
46494139 else
4650
- ret = nand_do_write_ops(mtd, to, ops);
4140
+ ret = nand_do_write_ops(chip, to, ops);
46514141
46524142 out:
4653
- nand_release_device(mtd);
4143
+ nand_release_device(chip);
46544144 return ret;
4655
-}
4656
-
4657
-/**
4658
- * single_erase - [GENERIC] NAND standard block erase command function
4659
- * @mtd: MTD device structure
4660
- * @page: the page address of the block which will be erased
4661
- *
4662
- * Standard erase command for NAND chips. Returns NAND status.
4663
- */
4664
-static int single_erase(struct mtd_info *mtd, int page)
4665
-{
4666
- struct nand_chip *chip = mtd_to_nand(mtd);
4667
- unsigned int eraseblock;
4668
-
4669
- /* Send commands to erase a block */
4670
- eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
4671
-
4672
- return nand_erase_op(chip, eraseblock);
46734145 }
46744146
46754147 /**
....@@ -4681,33 +4153,32 @@
46814153 */
46824154 static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
46834155 {
4684
- return nand_erase_nand(mtd, instr, 0);
4156
+ return nand_erase_nand(mtd_to_nand(mtd), instr, 0);
46854157 }
46864158
46874159 /**
46884160 * nand_erase_nand - [INTERN] erase block(s)
4689
- * @mtd: MTD device structure
4161
+ * @chip: NAND chip object
46904162 * @instr: erase instruction
46914163 * @allowbbt: allow erasing the bbt area
46924164 *
46934165 * Erase one ore more blocks.
46944166 */
4695
-int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
4167
+int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr,
46964168 int allowbbt)
46974169 {
4698
- int page, status, pages_per_block, ret, chipnr;
4699
- struct nand_chip *chip = mtd_to_nand(mtd);
4170
+ int page, pages_per_block, ret, chipnr;
47004171 loff_t len;
47014172
47024173 pr_debug("%s: start = 0x%012llx, len = %llu\n",
47034174 __func__, (unsigned long long)instr->addr,
47044175 (unsigned long long)instr->len);
47054176
4706
- if (check_offs_len(mtd, instr->addr, instr->len))
4177
+ if (check_offs_len(chip, instr->addr, instr->len))
47074178 return -EINVAL;
47084179
47094180 /* Grab the lock and see if the device is available */
4710
- nand_get_device(mtd, FL_ERASING);
4181
+ nand_get_device(chip);
47114182
47124183 /* Shift to get first page */
47134184 page = (int)(instr->addr >> chip->page_shift);
....@@ -4717,10 +4188,10 @@
47174188 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
47184189
47194190 /* Select the NAND device */
4720
- chip->select_chip(mtd, chipnr);
4191
+ nand_select_target(chip, chipnr);
47214192
47224193 /* Check, if it is write protected */
4723
- if (nand_check_wp(mtd)) {
4194
+ if (nand_check_wp(chip)) {
47244195 pr_debug("%s: device is write protected!\n",
47254196 __func__);
47264197 ret = -EIO;
....@@ -4732,7 +4203,7 @@
47324203
47334204 while (len) {
47344205 /* Check if we have a bad block, we do not erase bad blocks! */
4735
- if (nand_block_checkbad(mtd, ((loff_t) page) <<
4206
+ if (nand_block_checkbad(chip, ((loff_t) page) <<
47364207 chip->page_shift, allowbbt)) {
47374208 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
47384209 __func__, page);
....@@ -4744,17 +4215,15 @@
47444215 * Invalidate the page cache, if we erase the block which
47454216 * contains the current cached page.
47464217 */
4747
- if (page <= chip->pagebuf && chip->pagebuf <
4218
+ if (page <= chip->pagecache.page && chip->pagecache.page <
47484219 (page + pages_per_block))
4749
- chip->pagebuf = -1;
4220
+ chip->pagecache.page = -1;
47504221
4751
- status = chip->erase(mtd, page & chip->pagemask);
4752
-
4753
- /* See if block erase succeeded */
4754
- if (status) {
4222
+ ret = nand_erase_op(chip, (page & chip->pagemask) >>
4223
+ (chip->phys_erase_shift - chip->page_shift));
4224
+ if (ret) {
47554225 pr_debug("%s: failed erase, page 0x%08x\n",
47564226 __func__, page);
4757
- ret = -EIO;
47584227 instr->fail_addr =
47594228 ((loff_t)page << chip->page_shift);
47604229 goto erase_exit;
....@@ -4767,8 +4236,8 @@
47674236 /* Check, if we cross a chip boundary */
47684237 if (len && !(page & chip->pagemask)) {
47694238 chipnr++;
4770
- chip->select_chip(mtd, -1);
4771
- chip->select_chip(mtd, chipnr);
4239
+ nand_deselect_target(chip);
4240
+ nand_select_target(chip, chipnr);
47724241 }
47734242 }
47744243
....@@ -4776,8 +4245,8 @@
47764245 erase_exit:
47774246
47784247 /* Deselect and wake up anyone waiting on the device */
4779
- chip->select_chip(mtd, -1);
4780
- nand_release_device(mtd);
4248
+ nand_deselect_target(chip);
4249
+ nand_release_device(chip);
47814250
47824251 /* Return more or less happy */
47834252 return ret;
....@@ -4791,12 +4260,14 @@
47914260 */
47924261 static void nand_sync(struct mtd_info *mtd)
47934262 {
4263
+ struct nand_chip *chip = mtd_to_nand(mtd);
4264
+
47944265 pr_debug("%s: called\n", __func__);
47954266
47964267 /* Grab the lock and see if the device is available */
4797
- nand_get_device(mtd, FL_SYNCING);
4268
+ nand_get_device(chip);
47984269 /* Release it and go back */
4799
- nand_release_device(mtd);
4270
+ nand_release_device(chip);
48004271 }
48014272
48024273 /**
....@@ -4811,13 +4282,14 @@
48114282 int ret;
48124283
48134284 /* Select the NAND device */
4814
- nand_get_device(mtd, FL_READING);
4815
- chip->select_chip(mtd, chipnr);
4285
+ nand_get_device(chip);
48164286
4817
- ret = nand_block_checkbad(mtd, offs, 0);
4287
+ nand_select_target(chip, chipnr);
48184288
4819
- chip->select_chip(mtd, -1);
4820
- nand_release_device(mtd);
4289
+ ret = nand_block_checkbad(chip, offs, 0);
4290
+
4291
+ nand_deselect_target(chip);
4292
+ nand_release_device(chip);
48214293
48224294 return ret;
48234295 }
....@@ -4839,97 +4311,28 @@
48394311 return ret;
48404312 }
48414313
4842
- return nand_block_markbad_lowlevel(mtd, ofs);
4314
+ return nand_block_markbad_lowlevel(mtd_to_nand(mtd), ofs);
48434315 }
4844
-
4845
-/**
4846
- * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4847
- * @mtd: MTD device structure
4848
- * @ofs: offset relative to mtd start
4849
- * @len: length of mtd
4850
- */
4851
-static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4852
-{
4853
- struct nand_chip *chip = mtd_to_nand(mtd);
4854
- u32 part_start_block;
4855
- u32 part_end_block;
4856
- u32 part_start_die;
4857
- u32 part_end_die;
4858
-
4859
- /*
4860
- * max_bb_per_die and blocks_per_die used to determine
4861
- * the maximum bad block count.
4862
- */
4863
- if (!chip->max_bb_per_die || !chip->blocks_per_die)
4864
- return -ENOTSUPP;
4865
-
4866
- /* Get the start and end of the partition in erase blocks. */
4867
- part_start_block = mtd_div_by_eb(ofs, mtd);
4868
- part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4869
-
4870
- /* Get the start and end LUNs of the partition. */
4871
- part_start_die = part_start_block / chip->blocks_per_die;
4872
- part_end_die = part_end_block / chip->blocks_per_die;
4873
-
4874
- /*
4875
- * Look up the bad blocks per unit and multiply by the number of units
4876
- * that the partition spans.
4877
- */
4878
- return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4879
-}
4880
-
4881
-/**
4882
- * nand_default_set_features- [REPLACEABLE] set NAND chip features
4883
- * @mtd: MTD device structure
4884
- * @chip: nand chip info structure
4885
- * @addr: feature address.
4886
- * @subfeature_param: the subfeature parameters, a four bytes array.
4887
- */
4888
-static int nand_default_set_features(struct mtd_info *mtd,
4889
- struct nand_chip *chip, int addr,
4890
- uint8_t *subfeature_param)
4891
-{
4892
- return nand_set_features_op(chip, addr, subfeature_param);
4893
-}
4894
-
4895
-/**
4896
- * nand_default_get_features- [REPLACEABLE] get NAND chip features
4897
- * @mtd: MTD device structure
4898
- * @chip: nand chip info structure
4899
- * @addr: feature address.
4900
- * @subfeature_param: the subfeature parameters, a four bytes array.
4901
- */
4902
-static int nand_default_get_features(struct mtd_info *mtd,
4903
- struct nand_chip *chip, int addr,
4904
- uint8_t *subfeature_param)
4905
-{
4906
- return nand_get_features_op(chip, addr, subfeature_param);
4907
-}
4908
-
4909
-/**
4910
- * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP
4911
- * @mtd: MTD device structure
4912
- * @chip: nand chip info structure
4913
- * @addr: feature address.
4914
- * @subfeature_param: the subfeature parameters, a four bytes array.
4915
- *
4916
- * Should be used by NAND controller drivers that do not support the SET/GET
4917
- * FEATURES operations.
4918
- */
4919
-int nand_get_set_features_notsupp(struct mtd_info *mtd, struct nand_chip *chip,
4920
- int addr, u8 *subfeature_param)
4921
-{
4922
- return -ENOTSUPP;
4923
-}
4924
-EXPORT_SYMBOL(nand_get_set_features_notsupp);
49254316
49264317 /**
49274318 * nand_suspend - [MTD Interface] Suspend the NAND flash
49284319 * @mtd: MTD device structure
4320
+ *
4321
+ * Returns 0 for success or negative error code otherwise.
49294322 */
49304323 static int nand_suspend(struct mtd_info *mtd)
49314324 {
4932
- return nand_get_device(mtd, FL_PM_SUSPENDED);
4325
+ struct nand_chip *chip = mtd_to_nand(mtd);
4326
+ int ret = 0;
4327
+
4328
+ mutex_lock(&chip->lock);
4329
+ if (chip->ops.suspend)
4330
+ ret = chip->ops.suspend(chip);
4331
+ if (!ret)
4332
+ chip->suspended = 1;
4333
+ mutex_unlock(&chip->lock);
4334
+
4335
+ return ret;
49334336 }
49344337
49354338 /**
....@@ -4940,11 +4343,18 @@
49404343 {
49414344 struct nand_chip *chip = mtd_to_nand(mtd);
49424345
4943
- if (chip->state == FL_PM_SUSPENDED)
4944
- nand_release_device(mtd);
4945
- else
4346
+ mutex_lock(&chip->lock);
4347
+ if (chip->suspended) {
4348
+ if (chip->ops.resume)
4349
+ chip->ops.resume(chip);
4350
+ chip->suspended = 0;
4351
+ } else {
49464352 pr_err("%s called for a chip which is not in suspended state\n",
49474353 __func__);
4354
+ }
4355
+ mutex_unlock(&chip->lock);
4356
+
4357
+ wake_up_all(&chip->resume_wq);
49484358 }
49494359
49504360 /**
....@@ -4954,62 +4364,58 @@
49544364 */
49554365 static void nand_shutdown(struct mtd_info *mtd)
49564366 {
4957
- nand_get_device(mtd, FL_PM_SUSPENDED);
4367
+ nand_suspend(mtd);
4368
+}
4369
+
4370
+/**
4371
+ * nand_lock - [MTD Interface] Lock the NAND flash
4372
+ * @mtd: MTD device structure
4373
+ * @ofs: offset byte address
4374
+ * @len: number of bytes to lock (must be a multiple of block/page size)
4375
+ */
4376
+static int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
4377
+{
4378
+ struct nand_chip *chip = mtd_to_nand(mtd);
4379
+
4380
+ if (!chip->ops.lock_area)
4381
+ return -ENOTSUPP;
4382
+
4383
+ return chip->ops.lock_area(chip, ofs, len);
4384
+}
4385
+
4386
+/**
4387
+ * nand_unlock - [MTD Interface] Unlock the NAND flash
4388
+ * @mtd: MTD device structure
4389
+ * @ofs: offset byte address
4390
+ * @len: number of bytes to unlock (must be a multiple of block/page size)
4391
+ */
4392
+static int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
4393
+{
4394
+ struct nand_chip *chip = mtd_to_nand(mtd);
4395
+
4396
+ if (!chip->ops.unlock_area)
4397
+ return -ENOTSUPP;
4398
+
4399
+ return chip->ops.unlock_area(chip, ofs, len);
49584400 }
49594401
49604402 /* Set default functions */
49614403 static void nand_set_defaults(struct nand_chip *chip)
49624404 {
4963
- unsigned int busw = chip->options & NAND_BUSWIDTH_16;
4964
-
4965
- /* check for proper chip_delay setup, set 20us if not */
4966
- if (!chip->chip_delay)
4967
- chip->chip_delay = 20;
4968
-
4969
- /* check, if a user supplied command function given */
4970
- if (!chip->cmdfunc && !chip->exec_op)
4971
- chip->cmdfunc = nand_command;
4972
-
4973
- /* check, if a user supplied wait function given */
4974
- if (chip->waitfunc == NULL)
4975
- chip->waitfunc = nand_wait;
4976
-
4977
- if (!chip->select_chip)
4978
- chip->select_chip = nand_select_chip;
4979
-
4980
- /* set for ONFI nand */
4981
- if (!chip->set_features)
4982
- chip->set_features = nand_default_set_features;
4983
- if (!chip->get_features)
4984
- chip->get_features = nand_default_get_features;
4985
-
4986
- /* If called twice, pointers that depend on busw may need to be reset */
4987
- if (!chip->read_byte || chip->read_byte == nand_read_byte)
4988
- chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
4989
- if (!chip->read_word)
4990
- chip->read_word = nand_read_word;
4991
- if (!chip->block_bad)
4992
- chip->block_bad = nand_block_bad;
4993
- if (!chip->block_markbad)
4994
- chip->block_markbad = nand_default_block_markbad;
4995
- if (!chip->write_buf || chip->write_buf == nand_write_buf)
4996
- chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
4997
- if (!chip->write_byte || chip->write_byte == nand_write_byte)
4998
- chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
4999
- if (!chip->read_buf || chip->read_buf == nand_read_buf)
5000
- chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
5001
-
4405
+ /* If no controller is provided, use the dummy, legacy one. */
50024406 if (!chip->controller) {
5003
- chip->controller = &chip->dummy_controller;
4407
+ chip->controller = &chip->legacy.dummy_controller;
50044408 nand_controller_init(chip->controller);
50054409 }
4410
+
4411
+ nand_legacy_set_defaults(chip);
50064412
50074413 if (!chip->buf_align)
50084414 chip->buf_align = 1;
50094415 }
50104416
50114417 /* Sanitize ONFI strings so we can safely print them */
5012
-static void sanitize_string(uint8_t *s, size_t len)
4418
+void sanitize_string(uint8_t *s, size_t len)
50134419 {
50144420 ssize_t i;
50154421
....@@ -5024,390 +4430,6 @@
50244430
50254431 /* Remove trailing spaces */
50264432 strim(s);
5027
-}
5028
-
5029
-static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
5030
-{
5031
- int i;
5032
- while (len--) {
5033
- crc ^= *p++ << 8;
5034
- for (i = 0; i < 8; i++)
5035
- crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
5036
- }
5037
-
5038
- return crc;
5039
-}
5040
-
5041
-/* Parse the Extended Parameter Page. */
5042
-static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
5043
- struct nand_onfi_params *p)
5044
-{
5045
- struct onfi_ext_param_page *ep;
5046
- struct onfi_ext_section *s;
5047
- struct onfi_ext_ecc_info *ecc;
5048
- uint8_t *cursor;
5049
- int ret;
5050
- int len;
5051
- int i;
5052
-
5053
- len = le16_to_cpu(p->ext_param_page_length) * 16;
5054
- ep = kmalloc(len, GFP_KERNEL);
5055
- if (!ep)
5056
- return -ENOMEM;
5057
-
5058
- /* Send our own NAND_CMD_PARAM. */
5059
- ret = nand_read_param_page_op(chip, 0, NULL, 0);
5060
- if (ret)
5061
- goto ext_out;
5062
-
5063
- /* Use the Change Read Column command to skip the ONFI param pages. */
5064
- ret = nand_change_read_column_op(chip,
5065
- sizeof(*p) * p->num_of_param_pages,
5066
- ep, len, true);
5067
- if (ret)
5068
- goto ext_out;
5069
-
5070
- ret = -EINVAL;
5071
- if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
5072
- != le16_to_cpu(ep->crc))) {
5073
- pr_debug("fail in the CRC.\n");
5074
- goto ext_out;
5075
- }
5076
-
5077
- /*
5078
- * Check the signature.
5079
- * Do not strictly follow the ONFI spec, maybe changed in future.
5080
- */
5081
- if (strncmp(ep->sig, "EPPS", 4)) {
5082
- pr_debug("The signature is invalid.\n");
5083
- goto ext_out;
5084
- }
5085
-
5086
- /* find the ECC section. */
5087
- cursor = (uint8_t *)(ep + 1);
5088
- for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
5089
- s = ep->sections + i;
5090
- if (s->type == ONFI_SECTION_TYPE_2)
5091
- break;
5092
- cursor += s->length * 16;
5093
- }
5094
- if (i == ONFI_EXT_SECTION_MAX) {
5095
- pr_debug("We can not find the ECC section.\n");
5096
- goto ext_out;
5097
- }
5098
-
5099
- /* get the info we want. */
5100
- ecc = (struct onfi_ext_ecc_info *)cursor;
5101
-
5102
- if (!ecc->codeword_size) {
5103
- pr_debug("Invalid codeword size\n");
5104
- goto ext_out;
5105
- }
5106
-
5107
- chip->ecc_strength_ds = ecc->ecc_bits;
5108
- chip->ecc_step_ds = 1 << ecc->codeword_size;
5109
- ret = 0;
5110
-
5111
-ext_out:
5112
- kfree(ep);
5113
- return ret;
5114
-}
5115
-
5116
-/*
5117
- * Recover data with bit-wise majority
5118
- */
5119
-static void nand_bit_wise_majority(const void **srcbufs,
5120
- unsigned int nsrcbufs,
5121
- void *dstbuf,
5122
- unsigned int bufsize)
5123
-{
5124
- int i, j, k;
5125
-
5126
- for (i = 0; i < bufsize; i++) {
5127
- u8 val = 0;
5128
-
5129
- for (j = 0; j < 8; j++) {
5130
- unsigned int cnt = 0;
5131
-
5132
- for (k = 0; k < nsrcbufs; k++) {
5133
- const u8 *srcbuf = srcbufs[k];
5134
-
5135
- if (srcbuf[i] & BIT(j))
5136
- cnt++;
5137
- }
5138
-
5139
- if (cnt > nsrcbufs / 2)
5140
- val |= BIT(j);
5141
- }
5142
-
5143
- ((u8 *)dstbuf)[i] = val;
5144
- }
5145
-}
5146
-
5147
-/*
5148
- * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
5149
- */
5150
-static int nand_flash_detect_onfi(struct nand_chip *chip)
5151
-{
5152
- struct mtd_info *mtd = nand_to_mtd(chip);
5153
- struct nand_onfi_params *p;
5154
- struct onfi_params *onfi;
5155
- int onfi_version = 0;
5156
- char id[4];
5157
- int i, ret, val;
5158
-
5159
- /* Try ONFI for unknown chip or LP */
5160
- ret = nand_readid_op(chip, 0x20, id, sizeof(id));
5161
- if (ret || strncmp(id, "ONFI", 4))
5162
- return 0;
5163
-
5164
- /* ONFI chip: allocate a buffer to hold its parameter page */
5165
- p = kzalloc((sizeof(*p) * 3), GFP_KERNEL);
5166
- if (!p)
5167
- return -ENOMEM;
5168
-
5169
- ret = nand_read_param_page_op(chip, 0, NULL, 0);
5170
- if (ret) {
5171
- ret = 0;
5172
- goto free_onfi_param_page;
5173
- }
5174
-
5175
- for (i = 0; i < 3; i++) {
5176
- ret = nand_read_data_op(chip, &p[i], sizeof(*p), true);
5177
- if (ret) {
5178
- ret = 0;
5179
- goto free_onfi_param_page;
5180
- }
5181
-
5182
- if (onfi_crc16(ONFI_CRC_BASE, (u8 *)&p[i], 254) ==
5183
- le16_to_cpu(p->crc)) {
5184
- if (i)
5185
- memcpy(p, &p[i], sizeof(*p));
5186
- break;
5187
- }
5188
- }
5189
-
5190
- if (i == 3) {
5191
- const void *srcbufs[3] = {p, p + 1, p + 2};
5192
-
5193
- pr_warn("Could not find a valid ONFI parameter page, trying bit-wise majority to recover it\n");
5194
- nand_bit_wise_majority(srcbufs, ARRAY_SIZE(srcbufs), p,
5195
- sizeof(*p));
5196
-
5197
- if (onfi_crc16(ONFI_CRC_BASE, (u8 *)p, 254) !=
5198
- le16_to_cpu(p->crc)) {
5199
- pr_err("ONFI parameter recovery failed, aborting\n");
5200
- goto free_onfi_param_page;
5201
- }
5202
- }
5203
-
5204
- if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5205
- chip->manufacturer.desc->ops->fixup_onfi_param_page)
5206
- chip->manufacturer.desc->ops->fixup_onfi_param_page(chip, p);
5207
-
5208
- /* Check version */
5209
- val = le16_to_cpu(p->revision);
5210
- if (val & ONFI_VERSION_2_3)
5211
- onfi_version = 23;
5212
- else if (val & ONFI_VERSION_2_2)
5213
- onfi_version = 22;
5214
- else if (val & ONFI_VERSION_2_1)
5215
- onfi_version = 21;
5216
- else if (val & ONFI_VERSION_2_0)
5217
- onfi_version = 20;
5218
- else if (val & ONFI_VERSION_1_0)
5219
- onfi_version = 10;
5220
-
5221
- if (!onfi_version) {
5222
- pr_info("unsupported ONFI version: %d\n", val);
5223
- goto free_onfi_param_page;
5224
- }
5225
-
5226
- sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5227
- sanitize_string(p->model, sizeof(p->model));
5228
- chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5229
- if (!chip->parameters.model) {
5230
- ret = -ENOMEM;
5231
- goto free_onfi_param_page;
5232
- }
5233
-
5234
- mtd->writesize = le32_to_cpu(p->byte_per_page);
5235
-
5236
- /*
5237
- * pages_per_block and blocks_per_lun may not be a power-of-2 size
5238
- * (don't ask me who thought of this...). MTD assumes that these
5239
- * dimensions will be power-of-2, so just truncate the remaining area.
5240
- */
5241
- mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5242
- mtd->erasesize *= mtd->writesize;
5243
-
5244
- mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
5245
-
5246
- /* See erasesize comment */
5247
- chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
5248
- chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
5249
- chip->bits_per_cell = p->bits_per_cell;
5250
-
5251
- chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
5252
- chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
5253
-
5254
- if (le16_to_cpu(p->features) & ONFI_FEATURE_16_BIT_BUS)
5255
- chip->options |= NAND_BUSWIDTH_16;
5256
-
5257
- if (p->ecc_bits != 0xff) {
5258
- chip->ecc_strength_ds = p->ecc_bits;
5259
- chip->ecc_step_ds = 512;
5260
- } else if (onfi_version >= 21 &&
5261
- (le16_to_cpu(p->features) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
5262
-
5263
- /*
5264
- * The nand_flash_detect_ext_param_page() uses the
5265
- * Change Read Column command which maybe not supported
5266
- * by the chip->cmdfunc. So try to update the chip->cmdfunc
5267
- * now. We do not replace user supplied command function.
5268
- */
5269
- if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5270
- chip->cmdfunc = nand_command_lp;
5271
-
5272
- /* The Extended Parameter Page is supported since ONFI 2.1. */
5273
- if (nand_flash_detect_ext_param_page(chip, p))
5274
- pr_warn("Failed to detect ONFI extended param page\n");
5275
- } else {
5276
- pr_warn("Could not retrieve ONFI ECC requirements\n");
5277
- }
5278
-
5279
- /* Save some parameters from the parameter page for future use */
5280
- if (le16_to_cpu(p->opt_cmd) & ONFI_OPT_CMD_SET_GET_FEATURES) {
5281
- chip->parameters.supports_set_get_features = true;
5282
- bitmap_set(chip->parameters.get_feature_list,
5283
- ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5284
- bitmap_set(chip->parameters.set_feature_list,
5285
- ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5286
- }
5287
-
5288
- onfi = kzalloc(sizeof(*onfi), GFP_KERNEL);
5289
- if (!onfi) {
5290
- ret = -ENOMEM;
5291
- goto free_model;
5292
- }
5293
-
5294
- onfi->version = onfi_version;
5295
- onfi->tPROG = le16_to_cpu(p->t_prog);
5296
- onfi->tBERS = le16_to_cpu(p->t_bers);
5297
- onfi->tR = le16_to_cpu(p->t_r);
5298
- onfi->tCCS = le16_to_cpu(p->t_ccs);
5299
- onfi->async_timing_mode = le16_to_cpu(p->async_timing_mode);
5300
- onfi->vendor_revision = le16_to_cpu(p->vendor_revision);
5301
- memcpy(onfi->vendor, p->vendor, sizeof(p->vendor));
5302
- chip->parameters.onfi = onfi;
5303
-
5304
- /* Identification done, free the full ONFI parameter page and exit */
5305
- kfree(p);
5306
-
5307
- return 1;
5308
-
5309
-free_model:
5310
- kfree(chip->parameters.model);
5311
-free_onfi_param_page:
5312
- kfree(p);
5313
-
5314
- return ret;
5315
-}
5316
-
5317
-/*
5318
- * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
5319
- */
5320
-static int nand_flash_detect_jedec(struct nand_chip *chip)
5321
-{
5322
- struct mtd_info *mtd = nand_to_mtd(chip);
5323
- struct nand_jedec_params *p;
5324
- struct jedec_ecc_info *ecc;
5325
- int jedec_version = 0;
5326
- char id[5];
5327
- int i, val, ret;
5328
-
5329
- /* Try JEDEC for unknown chip or LP */
5330
- ret = nand_readid_op(chip, 0x40, id, sizeof(id));
5331
- if (ret || strncmp(id, "JEDEC", sizeof(id)))
5332
- return 0;
5333
-
5334
- /* JEDEC chip: allocate a buffer to hold its parameter page */
5335
- p = kzalloc(sizeof(*p), GFP_KERNEL);
5336
- if (!p)
5337
- return -ENOMEM;
5338
-
5339
- ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
5340
- if (ret) {
5341
- ret = 0;
5342
- goto free_jedec_param_page;
5343
- }
5344
-
5345
- for (i = 0; i < 3; i++) {
5346
- ret = nand_read_data_op(chip, p, sizeof(*p), true);
5347
- if (ret) {
5348
- ret = 0;
5349
- goto free_jedec_param_page;
5350
- }
5351
-
5352
- if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
5353
- le16_to_cpu(p->crc))
5354
- break;
5355
- }
5356
-
5357
- if (i == 3) {
5358
- pr_err("Could not find valid JEDEC parameter page; aborting\n");
5359
- goto free_jedec_param_page;
5360
- }
5361
-
5362
- /* Check version */
5363
- val = le16_to_cpu(p->revision);
5364
- if (val & (1 << 2))
5365
- jedec_version = 10;
5366
- else if (val & (1 << 1))
5367
- jedec_version = 1; /* vendor specific version */
5368
-
5369
- if (!jedec_version) {
5370
- pr_info("unsupported JEDEC version: %d\n", val);
5371
- goto free_jedec_param_page;
5372
- }
5373
-
5374
- sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5375
- sanitize_string(p->model, sizeof(p->model));
5376
- chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5377
- if (!chip->parameters.model) {
5378
- ret = -ENOMEM;
5379
- goto free_jedec_param_page;
5380
- }
5381
-
5382
- mtd->writesize = le32_to_cpu(p->byte_per_page);
5383
-
5384
- /* Please reference to the comment for nand_flash_detect_onfi. */
5385
- mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5386
- mtd->erasesize *= mtd->writesize;
5387
-
5388
- mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
5389
-
5390
- /* Please reference to the comment for nand_flash_detect_onfi. */
5391
- chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
5392
- chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
5393
- chip->bits_per_cell = p->bits_per_cell;
5394
-
5395
- if (le16_to_cpu(p->features) & JEDEC_FEATURE_16_BIT_BUS)
5396
- chip->options |= NAND_BUSWIDTH_16;
5397
-
5398
- /* ECC info */
5399
- ecc = &p->ecc_info[0];
5400
-
5401
- if (ecc->codeword_size >= 9) {
5402
- chip->ecc_strength_ds = ecc->ecc_bits;
5403
- chip->ecc_step_ds = 1 << ecc->codeword_size;
5404
- } else {
5405
- pr_warn("Invalid codeword size\n");
5406
- }
5407
-
5408
-free_jedec_param_page:
5409
- kfree(p);
5410
- return ret;
54114433 }
54124434
54134435 /*
....@@ -5486,21 +4508,29 @@
54864508 */
54874509 void nand_decode_ext_id(struct nand_chip *chip)
54884510 {
4511
+ struct nand_memory_organization *memorg;
54894512 struct mtd_info *mtd = nand_to_mtd(chip);
54904513 int extid;
54914514 u8 *id_data = chip->id.data;
4515
+
4516
+ memorg = nanddev_get_memorg(&chip->base);
4517
+
54924518 /* The 3rd id byte holds MLC / multichip data */
5493
- chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
4519
+ memorg->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
54944520 /* The 4th id byte is the important one */
54954521 extid = id_data[3];
54964522
54974523 /* Calc pagesize */
5498
- mtd->writesize = 1024 << (extid & 0x03);
4524
+ memorg->pagesize = 1024 << (extid & 0x03);
4525
+ mtd->writesize = memorg->pagesize;
54994526 extid >>= 2;
55004527 /* Calc oobsize */
5501
- mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
4528
+ memorg->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
4529
+ mtd->oobsize = memorg->oobsize;
55024530 extid >>= 2;
55034531 /* Calc blocksize. Blocksize is multiples of 64KiB */
4532
+ memorg->pages_per_eraseblock = ((64 * 1024) << (extid & 0x03)) /
4533
+ memorg->pagesize;
55044534 mtd->erasesize = (64 * 1024) << (extid & 0x03);
55054535 extid >>= 2;
55064536 /* Get buswidth information */
....@@ -5517,13 +4547,19 @@
55174547 static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
55184548 {
55194549 struct mtd_info *mtd = nand_to_mtd(chip);
4550
+ struct nand_memory_organization *memorg;
55204551
4552
+ memorg = nanddev_get_memorg(&chip->base);
4553
+
4554
+ memorg->pages_per_eraseblock = type->erasesize / type->pagesize;
55214555 mtd->erasesize = type->erasesize;
5522
- mtd->writesize = type->pagesize;
5523
- mtd->oobsize = mtd->writesize / 32;
4556
+ memorg->pagesize = type->pagesize;
4557
+ mtd->writesize = memorg->pagesize;
4558
+ memorg->oobsize = memorg->pagesize / 32;
4559
+ mtd->oobsize = memorg->oobsize;
55244560
55254561 /* All legacy ID NAND are small-page, SLC */
5526
- chip->bits_per_cell = 1;
4562
+ memorg->bits_per_cell = 1;
55274563 }
55284564
55294565 /*
....@@ -5537,9 +4573,9 @@
55374573
55384574 /* Set the bad block position */
55394575 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
5540
- chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
4576
+ chip->badblockpos = NAND_BBM_POS_LARGE;
55414577 else
5542
- chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
4578
+ chip->badblockpos = NAND_BBM_POS_SMALL;
55434579 }
55444580
55454581 static inline bool is_full_id_nand(struct nand_flash_dev *type)
....@@ -5550,21 +4586,32 @@
55504586 static bool find_full_id_nand(struct nand_chip *chip,
55514587 struct nand_flash_dev *type)
55524588 {
4589
+ struct nand_device *base = &chip->base;
4590
+ struct nand_ecc_props requirements;
55534591 struct mtd_info *mtd = nand_to_mtd(chip);
4592
+ struct nand_memory_organization *memorg;
55544593 u8 *id_data = chip->id.data;
55554594
5556
- if (!strncmp(type->id, id_data, type->id_len)) {
5557
- mtd->writesize = type->pagesize;
5558
- mtd->erasesize = type->erasesize;
5559
- mtd->oobsize = type->oobsize;
4595
+ memorg = nanddev_get_memorg(&chip->base);
55604596
5561
- chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
5562
- chip->chipsize = (uint64_t)type->chipsize << 20;
4597
+ if (!strncmp(type->id, id_data, type->id_len)) {
4598
+ memorg->pagesize = type->pagesize;
4599
+ mtd->writesize = memorg->pagesize;
4600
+ memorg->pages_per_eraseblock = type->erasesize /
4601
+ type->pagesize;
4602
+ mtd->erasesize = type->erasesize;
4603
+ memorg->oobsize = type->oobsize;
4604
+ mtd->oobsize = memorg->oobsize;
4605
+
4606
+ memorg->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
4607
+ memorg->eraseblocks_per_lun =
4608
+ DIV_ROUND_DOWN_ULL((u64)type->chipsize << 20,
4609
+ memorg->pagesize *
4610
+ memorg->pages_per_eraseblock);
55634611 chip->options |= type->options;
5564
- chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
5565
- chip->ecc_step_ds = NAND_ECC_STEP(type);
5566
- chip->onfi_timing_mode_default =
5567
- type->onfi_timing_mode_default;
4612
+ requirements.strength = NAND_ECC_STRENGTH(type);
4613
+ requirements.step_size = NAND_ECC_STEP(type);
4614
+ nanddev_set_ecc_requirements(base, &requirements);
55684615
55694616 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
55704617 if (!chip->parameters.model)
....@@ -5588,8 +4635,12 @@
55884635 */
55894636 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
55904637 chip->manufacturer.desc->ops->detect) {
4638
+ struct nand_memory_organization *memorg;
4639
+
4640
+ memorg = nanddev_get_memorg(&chip->base);
4641
+
55914642 /* The 3rd id byte holds MLC / multichip data */
5592
- chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
4643
+ memorg->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
55934644 chip->manufacturer.desc->ops->detect(chip);
55944645 } else {
55954646 nand_decode_ext_id(chip);
....@@ -5625,16 +4676,32 @@
56254676 chip->manufacturer.desc->ops->cleanup(chip);
56264677 }
56274678
4679
+static const char *
4680
+nand_manufacturer_name(const struct nand_manufacturer_desc *manufacturer_desc)
4681
+{
4682
+ return manufacturer_desc ? manufacturer_desc->name : "Unknown";
4683
+}
4684
+
56284685 /*
56294686 * Get the flash and manufacturer id and lookup if the type is supported.
56304687 */
56314688 static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
56324689 {
5633
- const struct nand_manufacturer *manufacturer;
4690
+ const struct nand_manufacturer_desc *manufacturer_desc;
56344691 struct mtd_info *mtd = nand_to_mtd(chip);
4692
+ struct nand_memory_organization *memorg;
56354693 int busw, ret;
56364694 u8 *id_data = chip->id.data;
56374695 u8 maf_id, dev_id;
4696
+ u64 targetsize;
4697
+
4698
+ /*
4699
+ * Let's start by initializing memorg fields that might be left
4700
+ * unassigned by the ID-based detection logic.
4701
+ */
4702
+ memorg = nanddev_get_memorg(&chip->base);
4703
+ memorg->planes_per_lun = 1;
4704
+ memorg->luns_per_target = 1;
56384705
56394706 /*
56404707 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
....@@ -5645,7 +4712,7 @@
56454712 return ret;
56464713
56474714 /* Select the device */
5648
- chip->select_chip(mtd, 0);
4715
+ nand_select_target(chip, 0);
56494716
56504717 /* Send the command for reading device ID */
56514718 ret = nand_readid_op(chip, 0, id_data, 2);
....@@ -5677,8 +4744,8 @@
56774744 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
56784745
56794746 /* Try to identify manufacturer */
5680
- manufacturer = nand_get_manufacturer(maf_id);
5681
- chip->manufacturer.desc = manufacturer;
4747
+ manufacturer_desc = nand_get_manufacturer_desc(maf_id);
4748
+ chip->manufacturer.desc = manufacturer_desc;
56824749
56834750 if (!type)
56844751 type = nand_flash_ids;
....@@ -5709,14 +4776,14 @@
57094776
57104777 if (!type->name || !type->pagesize) {
57114778 /* Check if the chip is ONFI compliant */
5712
- ret = nand_flash_detect_onfi(chip);
4779
+ ret = nand_onfi_detect(chip);
57134780 if (ret < 0)
57144781 return ret;
57154782 else if (ret)
57164783 goto ident_done;
57174784
57184785 /* Check if the chip is JEDEC compliant */
5719
- ret = nand_flash_detect_jedec(chip);
4786
+ ret = nand_jedec_detect(chip);
57204787 if (ret < 0)
57214788 return ret;
57224789 else if (ret)
....@@ -5730,8 +4797,6 @@
57304797 if (!chip->parameters.model)
57314798 return -ENOMEM;
57324799
5733
- chip->chipsize = (uint64_t)type->chipsize << 20;
5734
-
57354800 if (!type->pagesize)
57364801 nand_manufacturer_detect(chip);
57374802 else
....@@ -5739,6 +4804,11 @@
57394804
57404805 /* Get chip options */
57414806 chip->options |= type->options;
4807
+
4808
+ memorg->eraseblocks_per_lun =
4809
+ DIV_ROUND_DOWN_ULL((u64)type->chipsize << 20,
4810
+ memorg->pagesize *
4811
+ memorg->pages_per_eraseblock);
57424812
57434813 ident_done:
57444814 if (!mtd->name)
....@@ -5754,7 +4824,7 @@
57544824 */
57554825 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
57564826 maf_id, dev_id);
5757
- pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4827
+ pr_info("%s %s\n", nand_manufacturer_name(manufacturer_desc),
57584828 mtd->name);
57594829 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
57604830 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
....@@ -5768,14 +4838,15 @@
57684838 /* Calculate the address shift from the page size */
57694839 chip->page_shift = ffs(mtd->writesize) - 1;
57704840 /* Convert chipsize to number of pages per chip -1 */
5771
- chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
4841
+ targetsize = nanddev_target_size(&chip->base);
4842
+ chip->pagemask = (targetsize >> chip->page_shift) - 1;
57724843
57734844 chip->bbt_erase_shift = chip->phys_erase_shift =
57744845 ffs(mtd->erasesize) - 1;
5775
- if (chip->chipsize & 0xffffffff)
5776
- chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
4846
+ if (targetsize & 0xffffffff)
4847
+ chip->chip_shift = ffs((unsigned)targetsize) - 1;
57774848 else {
5778
- chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4849
+ chip->chip_shift = ffs((unsigned)(targetsize >> 32));
57794850 chip->chip_shift += 32 - 1;
57804851 }
57814852
....@@ -5783,18 +4854,15 @@
57834854 chip->options |= NAND_ROW_ADDR_3;
57844855
57854856 chip->badblockbits = 8;
5786
- chip->erase = single_erase;
57874857
5788
- /* Do not replace user supplied command function! */
5789
- if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5790
- chip->cmdfunc = nand_command_lp;
4858
+ nand_legacy_adjust_cmdfunc(chip);
57914859
57924860 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
57934861 maf_id, dev_id);
5794
- pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4862
+ pr_info("%s %s\n", nand_manufacturer_name(manufacturer_desc),
57954863 chip->parameters.model);
57964864 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
5797
- (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
4865
+ (int)(targetsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
57984866 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
57994867 return 0;
58004868
....@@ -5804,90 +4872,101 @@
58044872 return ret;
58054873 }
58064874
5807
-static const char * const nand_ecc_modes[] = {
5808
- [NAND_ECC_NONE] = "none",
5809
- [NAND_ECC_SOFT] = "soft",
5810
- [NAND_ECC_HW] = "hw",
5811
- [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
5812
- [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
5813
- [NAND_ECC_ON_DIE] = "on-die",
5814
-};
5815
-
5816
-static int of_get_nand_ecc_mode(struct device_node *np)
4875
+static enum nand_ecc_engine_type
4876
+of_get_rawnand_ecc_engine_type_legacy(struct device_node *np)
58174877 {
4878
+ enum nand_ecc_legacy_mode {
4879
+ NAND_ECC_INVALID,
4880
+ NAND_ECC_NONE,
4881
+ NAND_ECC_SOFT,
4882
+ NAND_ECC_SOFT_BCH,
4883
+ NAND_ECC_HW,
4884
+ NAND_ECC_HW_SYNDROME,
4885
+ NAND_ECC_ON_DIE,
4886
+ };
4887
+ const char * const nand_ecc_legacy_modes[] = {
4888
+ [NAND_ECC_NONE] = "none",
4889
+ [NAND_ECC_SOFT] = "soft",
4890
+ [NAND_ECC_SOFT_BCH] = "soft_bch",
4891
+ [NAND_ECC_HW] = "hw",
4892
+ [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4893
+ [NAND_ECC_ON_DIE] = "on-die",
4894
+ };
4895
+ enum nand_ecc_legacy_mode eng_type;
58184896 const char *pm;
5819
- int err, i;
4897
+ int err;
58204898
58214899 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5822
- if (err < 0)
5823
- return err;
4900
+ if (err)
4901
+ return NAND_ECC_ENGINE_TYPE_INVALID;
58244902
5825
- for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
5826
- if (!strcasecmp(pm, nand_ecc_modes[i]))
5827
- return i;
5828
-
5829
- /*
5830
- * For backward compatibility we support few obsoleted values that don't
5831
- * have their mappings into nand_ecc_modes_t anymore (they were merged
5832
- * with other enums).
5833
- */
5834
- if (!strcasecmp(pm, "soft_bch"))
5835
- return NAND_ECC_SOFT;
5836
-
5837
- return -ENODEV;
5838
-}
5839
-
5840
-static const char * const nand_ecc_algos[] = {
5841
- [NAND_ECC_HAMMING] = "hamming",
5842
- [NAND_ECC_BCH] = "bch",
5843
- [NAND_ECC_RS] = "rs",
5844
-};
5845
-
5846
-static int of_get_nand_ecc_algo(struct device_node *np)
5847
-{
5848
- const char *pm;
5849
- int err, i;
5850
-
5851
- err = of_property_read_string(np, "nand-ecc-algo", &pm);
5852
- if (!err) {
5853
- for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
5854
- if (!strcasecmp(pm, nand_ecc_algos[i]))
5855
- return i;
5856
- return -ENODEV;
4903
+ for (eng_type = NAND_ECC_NONE;
4904
+ eng_type < ARRAY_SIZE(nand_ecc_legacy_modes); eng_type++) {
4905
+ if (!strcasecmp(pm, nand_ecc_legacy_modes[eng_type])) {
4906
+ switch (eng_type) {
4907
+ case NAND_ECC_NONE:
4908
+ return NAND_ECC_ENGINE_TYPE_NONE;
4909
+ case NAND_ECC_SOFT:
4910
+ case NAND_ECC_SOFT_BCH:
4911
+ return NAND_ECC_ENGINE_TYPE_SOFT;
4912
+ case NAND_ECC_HW:
4913
+ case NAND_ECC_HW_SYNDROME:
4914
+ return NAND_ECC_ENGINE_TYPE_ON_HOST;
4915
+ case NAND_ECC_ON_DIE:
4916
+ return NAND_ECC_ENGINE_TYPE_ON_DIE;
4917
+ default:
4918
+ break;
4919
+ }
4920
+ }
58574921 }
58584922
5859
- /*
5860
- * For backward compatibility we also read "nand-ecc-mode" checking
5861
- * for some obsoleted values that were specifying ECC algorithm.
5862
- */
4923
+ return NAND_ECC_ENGINE_TYPE_INVALID;
4924
+}
4925
+
4926
+static enum nand_ecc_placement
4927
+of_get_rawnand_ecc_placement_legacy(struct device_node *np)
4928
+{
4929
+ const char *pm;
4930
+ int err;
4931
+
58634932 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5864
- if (err < 0)
5865
- return err;
4933
+ if (!err) {
4934
+ if (!strcasecmp(pm, "hw_syndrome"))
4935
+ return NAND_ECC_PLACEMENT_INTERLEAVED;
4936
+ }
58664937
5867
- if (!strcasecmp(pm, "soft"))
5868
- return NAND_ECC_HAMMING;
5869
- else if (!strcasecmp(pm, "soft_bch"))
5870
- return NAND_ECC_BCH;
5871
-
5872
- return -ENODEV;
4938
+ return NAND_ECC_PLACEMENT_UNKNOWN;
58734939 }
58744940
5875
-static int of_get_nand_ecc_step_size(struct device_node *np)
4941
+static enum nand_ecc_algo of_get_rawnand_ecc_algo_legacy(struct device_node *np)
58764942 {
5877
- int ret;
5878
- u32 val;
4943
+ const char *pm;
4944
+ int err;
58794945
5880
- ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
5881
- return ret ? ret : val;
4946
+ err = of_property_read_string(np, "nand-ecc-mode", &pm);
4947
+ if (!err) {
4948
+ if (!strcasecmp(pm, "soft"))
4949
+ return NAND_ECC_ALGO_HAMMING;
4950
+ else if (!strcasecmp(pm, "soft_bch"))
4951
+ return NAND_ECC_ALGO_BCH;
4952
+ }
4953
+
4954
+ return NAND_ECC_ALGO_UNKNOWN;
58824955 }
58834956
5884
-static int of_get_nand_ecc_strength(struct device_node *np)
4957
+static void of_get_nand_ecc_legacy_user_config(struct nand_chip *chip)
58854958 {
5886
- int ret;
5887
- u32 val;
4959
+ struct device_node *dn = nand_get_flash_node(chip);
4960
+ struct nand_ecc_props *user_conf = &chip->base.ecc.user_conf;
58884961
5889
- ret = of_property_read_u32(np, "nand-ecc-strength", &val);
5890
- return ret ? ret : val;
4962
+ if (user_conf->engine_type == NAND_ECC_ENGINE_TYPE_INVALID)
4963
+ user_conf->engine_type = of_get_rawnand_ecc_engine_type_legacy(dn);
4964
+
4965
+ if (user_conf->algo == NAND_ECC_ALGO_UNKNOWN)
4966
+ user_conf->algo = of_get_rawnand_ecc_algo_legacy(dn);
4967
+
4968
+ if (user_conf->placement == NAND_ECC_PLACEMENT_UNKNOWN)
4969
+ user_conf->placement = of_get_rawnand_ecc_placement_legacy(dn);
58914970 }
58924971
58934972 static int of_get_nand_bus_width(struct device_node *np)
....@@ -5911,10 +4990,10 @@
59114990 return of_property_read_bool(np, "nand-on-flash-bbt");
59124991 }
59134992
5914
-static int nand_dt_init(struct nand_chip *chip)
4993
+static int rawnand_dt_init(struct nand_chip *chip)
59154994 {
4995
+ struct nand_device *nand = mtd_to_nanddev(nand_to_mtd(chip));
59164996 struct device_node *dn = nand_get_flash_node(chip);
5917
- int ecc_mode, ecc_algo, ecc_strength, ecc_step;
59184997
59194998 if (!dn)
59204999 return 0;
....@@ -5928,25 +5007,29 @@
59285007 if (of_get_nand_on_flash_bbt(dn))
59295008 chip->bbt_options |= NAND_BBT_USE_FLASH;
59305009
5931
- ecc_mode = of_get_nand_ecc_mode(dn);
5932
- ecc_algo = of_get_nand_ecc_algo(dn);
5933
- ecc_strength = of_get_nand_ecc_strength(dn);
5934
- ecc_step = of_get_nand_ecc_step_size(dn);
5010
+ of_get_nand_ecc_user_config(nand);
5011
+ of_get_nand_ecc_legacy_user_config(chip);
59355012
5936
- if (ecc_mode >= 0)
5937
- chip->ecc.mode = ecc_mode;
5013
+ /*
5014
+ * If neither the user nor the NAND controller have requested a specific
5015
+ * ECC engine type, we will default to NAND_ECC_ENGINE_TYPE_ON_HOST.
5016
+ */
5017
+ nand->ecc.defaults.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
59385018
5939
- if (ecc_algo >= 0)
5940
- chip->ecc.algo = ecc_algo;
5019
+ /*
5020
+ * Use the user requested engine type, unless there is none, in this
5021
+ * case default to the NAND controller choice, otherwise fallback to
5022
+ * the raw NAND default one.
5023
+ */
5024
+ if (nand->ecc.user_conf.engine_type != NAND_ECC_ENGINE_TYPE_INVALID)
5025
+ chip->ecc.engine_type = nand->ecc.user_conf.engine_type;
5026
+ if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_INVALID)
5027
+ chip->ecc.engine_type = nand->ecc.defaults.engine_type;
59415028
5942
- if (ecc_strength >= 0)
5943
- chip->ecc.strength = ecc_strength;
5944
-
5945
- if (ecc_step > 0)
5946
- chip->ecc.size = ecc_step;
5947
-
5948
- if (of_property_read_bool(dn, "nand-ecc-maximize"))
5949
- chip->ecc.options |= NAND_ECC_MAXIMIZE;
5029
+ chip->ecc.placement = nand->ecc.user_conf.placement;
5030
+ chip->ecc.algo = nand->ecc.user_conf.algo;
5031
+ chip->ecc.strength = nand->ecc.user_conf.strength;
5032
+ chip->ecc.size = nand->ecc.user_conf.step_size;
59505033
59515034 return 0;
59525035 }
....@@ -5965,78 +5048,83 @@
59655048 * prevented dynamic allocations during this phase which was unconvenient and
59665049 * as been banned for the benefit of the ->init_ecc()/cleanup_ecc() hooks.
59675050 */
5968
-static int nand_scan_ident(struct nand_chip *chip, int maxchips,
5051
+static int nand_scan_ident(struct nand_chip *chip, unsigned int maxchips,
59695052 struct nand_flash_dev *table)
59705053 {
59715054 struct mtd_info *mtd = nand_to_mtd(chip);
5972
- int i, nand_maf_id, nand_dev_id;
5055
+ struct nand_memory_organization *memorg;
5056
+ int nand_maf_id, nand_dev_id;
5057
+ unsigned int i;
59735058 int ret;
59745059
5975
- /* Enforce the right timings for reset/detection */
5976
- onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
5060
+ memorg = nanddev_get_memorg(&chip->base);
59775061
5978
- ret = nand_dt_init(chip);
5062
+ /* Assume all dies are deselected when we enter nand_scan_ident(). */
5063
+ chip->cur_cs = -1;
5064
+
5065
+ mutex_init(&chip->lock);
5066
+ init_waitqueue_head(&chip->resume_wq);
5067
+
5068
+ /* Enforce the right timings for reset/detection */
5069
+ chip->current_interface_config = nand_get_reset_interface_config();
5070
+
5071
+ ret = rawnand_dt_init(chip);
59795072 if (ret)
59805073 return ret;
59815074
59825075 if (!mtd->name && mtd->dev.parent)
59835076 mtd->name = dev_name(mtd->dev.parent);
59845077
5985
- /*
5986
- * ->cmdfunc() is legacy and will only be used if ->exec_op() is not
5987
- * populated.
5988
- */
5989
- if (!chip->exec_op) {
5990
- /*
5991
- * Default functions assigned for ->cmdfunc() and
5992
- * ->select_chip() both expect ->cmd_ctrl() to be populated.
5993
- */
5994
- if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
5995
- pr_err("->cmd_ctrl() should be provided\n");
5996
- return -EINVAL;
5997
- }
5998
- }
5999
-
60005078 /* Set the default functions */
60015079 nand_set_defaults(chip);
5080
+
5081
+ ret = nand_legacy_check_hooks(chip);
5082
+ if (ret)
5083
+ return ret;
5084
+
5085
+ memorg->ntargets = maxchips;
60025086
60035087 /* Read the flash type */
60045088 ret = nand_detect(chip, table);
60055089 if (ret) {
60065090 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
60075091 pr_warn("No NAND device found\n");
6008
- chip->select_chip(mtd, -1);
5092
+ nand_deselect_target(chip);
60095093 return ret;
60105094 }
60115095
60125096 nand_maf_id = chip->id.data[0];
60135097 nand_dev_id = chip->id.data[1];
60145098
6015
- chip->select_chip(mtd, -1);
5099
+ nand_deselect_target(chip);
60165100
60175101 /* Check for a chip array */
60185102 for (i = 1; i < maxchips; i++) {
60195103 u8 id[2];
60205104
60215105 /* See comment in nand_get_flash_type for reset */
6022
- nand_reset(chip, i);
5106
+ ret = nand_reset(chip, i);
5107
+ if (ret)
5108
+ break;
60235109
6024
- chip->select_chip(mtd, i);
5110
+ nand_select_target(chip, i);
60255111 /* Send the command for reading device ID */
6026
- nand_readid_op(chip, 0, id, sizeof(id));
5112
+ ret = nand_readid_op(chip, 0, id, sizeof(id));
5113
+ if (ret)
5114
+ break;
60275115 /* Read manufacturer and device IDs */
60285116 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
6029
- chip->select_chip(mtd, -1);
5117
+ nand_deselect_target(chip);
60305118 break;
60315119 }
6032
- chip->select_chip(mtd, -1);
5120
+ nand_deselect_target(chip);
60335121 }
60345122 if (i > 1)
60355123 pr_info("%d chips detected\n", i);
60365124
60375125 /* Store the number of chips and calc total size for mtd */
6038
- chip->numchips = i;
6039
- mtd->size = i * chip->chipsize;
5126
+ memorg->ntargets = i;
5127
+ mtd->size = i * nanddev_target_size(&chip->base);
60405128
60415129 return 0;
60425130 }
....@@ -6047,33 +5135,99 @@
60475135 kfree(chip->parameters.onfi);
60485136 }
60495137
6050
-static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
5138
+static int nand_set_ecc_on_host_ops(struct nand_chip *chip)
60515139 {
6052
- struct nand_chip *chip = mtd_to_nand(mtd);
60535140 struct nand_ecc_ctrl *ecc = &chip->ecc;
60545141
6055
- if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
5142
+ switch (ecc->placement) {
5143
+ case NAND_ECC_PLACEMENT_UNKNOWN:
5144
+ case NAND_ECC_PLACEMENT_OOB:
5145
+ /* Use standard hwecc read page function? */
5146
+ if (!ecc->read_page)
5147
+ ecc->read_page = nand_read_page_hwecc;
5148
+ if (!ecc->write_page)
5149
+ ecc->write_page = nand_write_page_hwecc;
5150
+ if (!ecc->read_page_raw)
5151
+ ecc->read_page_raw = nand_read_page_raw;
5152
+ if (!ecc->write_page_raw)
5153
+ ecc->write_page_raw = nand_write_page_raw;
5154
+ if (!ecc->read_oob)
5155
+ ecc->read_oob = nand_read_oob_std;
5156
+ if (!ecc->write_oob)
5157
+ ecc->write_oob = nand_write_oob_std;
5158
+ if (!ecc->read_subpage)
5159
+ ecc->read_subpage = nand_read_subpage;
5160
+ if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
5161
+ ecc->write_subpage = nand_write_subpage_hwecc;
5162
+ fallthrough;
5163
+
5164
+ case NAND_ECC_PLACEMENT_INTERLEAVED:
5165
+ if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
5166
+ (!ecc->read_page ||
5167
+ ecc->read_page == nand_read_page_hwecc ||
5168
+ !ecc->write_page ||
5169
+ ecc->write_page == nand_write_page_hwecc)) {
5170
+ WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
5171
+ return -EINVAL;
5172
+ }
5173
+ /* Use standard syndrome read/write page function? */
5174
+ if (!ecc->read_page)
5175
+ ecc->read_page = nand_read_page_syndrome;
5176
+ if (!ecc->write_page)
5177
+ ecc->write_page = nand_write_page_syndrome;
5178
+ if (!ecc->read_page_raw)
5179
+ ecc->read_page_raw = nand_read_page_raw_syndrome;
5180
+ if (!ecc->write_page_raw)
5181
+ ecc->write_page_raw = nand_write_page_raw_syndrome;
5182
+ if (!ecc->read_oob)
5183
+ ecc->read_oob = nand_read_oob_syndrome;
5184
+ if (!ecc->write_oob)
5185
+ ecc->write_oob = nand_write_oob_syndrome;
5186
+ break;
5187
+
5188
+ default:
5189
+ pr_warn("Invalid NAND_ECC_PLACEMENT %d\n",
5190
+ ecc->placement);
5191
+ return -EINVAL;
5192
+ }
5193
+
5194
+ return 0;
5195
+}
5196
+
5197
+static int nand_set_ecc_soft_ops(struct nand_chip *chip)
5198
+{
5199
+ struct mtd_info *mtd = nand_to_mtd(chip);
5200
+ struct nand_device *nanddev = mtd_to_nanddev(mtd);
5201
+ struct nand_ecc_ctrl *ecc = &chip->ecc;
5202
+
5203
+ if (WARN_ON(ecc->engine_type != NAND_ECC_ENGINE_TYPE_SOFT))
60565204 return -EINVAL;
60575205
60585206 switch (ecc->algo) {
6059
- case NAND_ECC_HAMMING:
5207
+ case NAND_ECC_ALGO_HAMMING:
60605208 ecc->calculate = nand_calculate_ecc;
60615209 ecc->correct = nand_correct_data;
60625210 ecc->read_page = nand_read_page_swecc;
60635211 ecc->read_subpage = nand_read_subpage;
60645212 ecc->write_page = nand_write_page_swecc;
6065
- ecc->read_page_raw = nand_read_page_raw;
6066
- ecc->write_page_raw = nand_write_page_raw;
5213
+ if (!ecc->read_page_raw)
5214
+ ecc->read_page_raw = nand_read_page_raw;
5215
+ if (!ecc->write_page_raw)
5216
+ ecc->write_page_raw = nand_write_page_raw;
60675217 ecc->read_oob = nand_read_oob_std;
60685218 ecc->write_oob = nand_write_oob_std;
60695219 if (!ecc->size)
60705220 ecc->size = 256;
60715221 ecc->bytes = 3;
60725222 ecc->strength = 1;
5223
+
5224
+ if (IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_HAMMING_SMC))
5225
+ ecc->options |= NAND_ECC_SOFT_HAMMING_SM_ORDER;
5226
+
60735227 return 0;
6074
- case NAND_ECC_BCH:
5228
+ case NAND_ECC_ALGO_BCH:
60755229 if (!mtd_nand_has_bch()) {
6076
- WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
5230
+ WARN(1, "CONFIG_MTD_NAND_ECC_SW_BCH not enabled\n");
60775231 return -EINVAL;
60785232 }
60795233 ecc->calculate = nand_bch_calculate_ecc;
....@@ -6081,8 +5235,10 @@
60815235 ecc->read_page = nand_read_page_swecc;
60825236 ecc->read_subpage = nand_read_subpage;
60835237 ecc->write_page = nand_write_page_swecc;
6084
- ecc->read_page_raw = nand_read_page_raw;
6085
- ecc->write_page_raw = nand_write_page_raw;
5238
+ if (!ecc->read_page_raw)
5239
+ ecc->read_page_raw = nand_read_page_raw;
5240
+ if (!ecc->write_page_raw)
5241
+ ecc->write_page_raw = nand_write_page_raw;
60865242 ecc->read_oob = nand_read_oob_std;
60875243 ecc->write_oob = nand_write_oob_std;
60885244
....@@ -6107,7 +5263,7 @@
61075263 return -EINVAL;
61085264 }
61095265
6110
- mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
5266
+ mtd_set_ooblayout(mtd, nand_get_large_page_ooblayout());
61115267
61125268 }
61135269
....@@ -6116,8 +5272,8 @@
61165272 * used, otherwise we don't know how many bytes can really be
61175273 * used.
61185274 */
6119
- if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
6120
- ecc->options & NAND_ECC_MAXIMIZE) {
5275
+ if (mtd->ooblayout == nand_get_large_page_ooblayout() &&
5276
+ nanddev->ecc.user_conf.flags & NAND_ECC_MAXIMIZE_STRENGTH) {
61215277 int steps, bytes;
61225278
61235279 /* Always prefer 1k blocks over 512bytes ones */
....@@ -6211,10 +5367,12 @@
62115367 nand_match_ecc_req(struct nand_chip *chip,
62125368 const struct nand_ecc_caps *caps, int oobavail)
62135369 {
5370
+ const struct nand_ecc_props *requirements =
5371
+ nanddev_get_ecc_requirements(&chip->base);
62145372 struct mtd_info *mtd = nand_to_mtd(chip);
62155373 const struct nand_ecc_step_info *stepinfo;
6216
- int req_step = chip->ecc_step_ds;
6217
- int req_strength = chip->ecc_strength_ds;
5374
+ int req_step = requirements->step_size;
5375
+ int req_strength = requirements->strength;
62185376 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
62195377 int best_step, best_strength, best_ecc_bytes;
62205378 int best_ecc_bytes_total = INT_MAX;
....@@ -6355,11 +5513,12 @@
63555513 * @caps: ECC engine caps info structure
63565514 * @oobavail: OOB size that the ECC engine can use
63575515 *
6358
- * Choose the ECC configuration according to following logic
5516
+ * Choose the ECC configuration according to following logic.
63595517 *
63605518 * 1. If both ECC step size and ECC strength are already set (usually by DT)
63615519 * then check if it is supported by this controller.
6362
- * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength.
5520
+ * 2. If the user provided the nand-ecc-maximize property, then select maximum
5521
+ * ECC strength.
63635522 * 3. Otherwise, try to match the ECC step size and ECC strength closest
63645523 * to the chip's requirement. If available OOB size can't fit the chip
63655524 * requirement then fallback to the maximum ECC step size and ECC strength.
....@@ -6370,6 +5529,7 @@
63705529 const struct nand_ecc_caps *caps, int oobavail)
63715530 {
63725531 struct mtd_info *mtd = nand_to_mtd(chip);
5532
+ struct nand_device *nanddev = mtd_to_nanddev(mtd);
63735533
63745534 if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize))
63755535 return -EINVAL;
....@@ -6377,7 +5537,7 @@
63775537 if (chip->ecc.size && chip->ecc.strength)
63785538 return nand_check_ecc_caps(chip, caps, oobavail);
63795539
6380
- if (chip->ecc.options & NAND_ECC_MAXIMIZE)
5540
+ if (nanddev->ecc.user_conf.flags & NAND_ECC_MAXIMIZE_STRENGTH)
63815541 return nand_maximize_ecc(chip, caps, oobavail);
63825542
63835543 if (!nand_match_ecc_req(chip, caps, oobavail))
....@@ -6387,39 +5547,49 @@
63875547 }
63885548 EXPORT_SYMBOL_GPL(nand_ecc_choose_conf);
63895549
6390
-/*
6391
- * Check if the chip configuration meet the datasheet requirements.
6392
-
6393
- * If our configuration corrects A bits per B bytes and the minimum
6394
- * required correction level is X bits per Y bytes, then we must ensure
6395
- * both of the following are true:
6396
- *
6397
- * (1) A / B >= X / Y
6398
- * (2) A >= X
6399
- *
6400
- * Requirement (1) ensures we can correct for the required bitflip density.
6401
- * Requirement (2) ensures we can correct even when all bitflips are clumped
6402
- * in the same sector.
6403
- */
6404
-static bool nand_ecc_strength_good(struct mtd_info *mtd)
5550
+static int rawnand_erase(struct nand_device *nand, const struct nand_pos *pos)
64055551 {
6406
- struct nand_chip *chip = mtd_to_nand(mtd);
6407
- struct nand_ecc_ctrl *ecc = &chip->ecc;
6408
- int corr, ds_corr;
5552
+ struct nand_chip *chip = container_of(nand, struct nand_chip,
5553
+ base);
5554
+ unsigned int eb = nanddev_pos_to_row(nand, pos);
5555
+ int ret;
64095556
6410
- if (ecc->size == 0 || chip->ecc_step_ds == 0)
6411
- /* Not enough information */
6412
- return true;
5557
+ eb >>= nand->rowconv.eraseblock_addr_shift;
64135558
6414
- /*
6415
- * We get the number of corrected bits per page to compare
6416
- * the correction density.
6417
- */
6418
- corr = (mtd->writesize * ecc->strength) / ecc->size;
6419
- ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
5559
+ nand_select_target(chip, pos->target);
5560
+ ret = nand_erase_op(chip, eb);
5561
+ nand_deselect_target(chip);
64205562
6421
- return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
5563
+ return ret;
64225564 }
5565
+
5566
+static int rawnand_markbad(struct nand_device *nand,
5567
+ const struct nand_pos *pos)
5568
+{
5569
+ struct nand_chip *chip = container_of(nand, struct nand_chip,
5570
+ base);
5571
+
5572
+ return nand_markbad_bbm(chip, nanddev_pos_to_offs(nand, pos));
5573
+}
5574
+
5575
+static bool rawnand_isbad(struct nand_device *nand, const struct nand_pos *pos)
5576
+{
5577
+ struct nand_chip *chip = container_of(nand, struct nand_chip,
5578
+ base);
5579
+ int ret;
5580
+
5581
+ nand_select_target(chip, pos->target);
5582
+ ret = nand_isbad_bbm(chip, nanddev_pos_to_offs(nand, pos));
5583
+ nand_deselect_target(chip);
5584
+
5585
+ return ret;
5586
+}
5587
+
5588
+static const struct nand_ops rawnand_ops = {
5589
+ .erase = rawnand_erase,
5590
+ .markbad = rawnand_markbad,
5591
+ .isbad = rawnand_isbad,
5592
+};
64235593
64245594 /**
64255595 * nand_scan_tail - Scan for the NAND device
....@@ -6451,9 +5621,9 @@
64515621 * to explictly select the relevant die when interacting with the NAND
64525622 * chip.
64535623 */
6454
- chip->select_chip(mtd, 0);
5624
+ nand_select_target(chip, 0);
64555625 ret = nand_manufacturer_init(chip);
6456
- chip->select_chip(mtd, -1);
5626
+ nand_deselect_target(chip);
64575627 if (ret)
64585628 goto err_free_buf;
64595629
....@@ -6464,15 +5634,17 @@
64645634 * If no default placement scheme is given, select an appropriate one.
64655635 */
64665636 if (!mtd->ooblayout &&
6467
- !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
5637
+ !(ecc->engine_type == NAND_ECC_ENGINE_TYPE_SOFT &&
5638
+ ecc->algo == NAND_ECC_ALGO_BCH)) {
64685639 switch (mtd->oobsize) {
64695640 case 8:
64705641 case 16:
6471
- mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
5642
+ mtd_set_ooblayout(mtd, nand_get_small_page_ooblayout());
64725643 break;
64735644 case 64:
64745645 case 128:
6475
- mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
5646
+ mtd_set_ooblayout(mtd,
5647
+ nand_get_large_page_hamming_ooblayout());
64765648 break;
64775649 default:
64785650 /*
....@@ -6482,9 +5654,9 @@
64825654 * page with ECC layout when ->oobsize <= 128 for
64835655 * compatibility reasons.
64845656 */
6485
- if (ecc->mode == NAND_ECC_NONE) {
5657
+ if (ecc->engine_type == NAND_ECC_ENGINE_TYPE_NONE) {
64865658 mtd_set_ooblayout(mtd,
6487
- &nand_ooblayout_lp_ops);
5659
+ nand_get_large_page_ooblayout());
64885660 break;
64895661 }
64905662
....@@ -6500,59 +5672,11 @@
65005672 * selected and we have 256 byte pagesize fallback to software ECC
65015673 */
65025674
6503
- switch (ecc->mode) {
6504
- case NAND_ECC_HW_OOB_FIRST:
6505
- /* Similar to NAND_ECC_HW, but a separate read_page handle */
6506
- if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
6507
- WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6508
- ret = -EINVAL;
5675
+ switch (ecc->engine_type) {
5676
+ case NAND_ECC_ENGINE_TYPE_ON_HOST:
5677
+ ret = nand_set_ecc_on_host_ops(chip);
5678
+ if (ret)
65095679 goto err_nand_manuf_cleanup;
6510
- }
6511
- if (!ecc->read_page)
6512
- ecc->read_page = nand_read_page_hwecc_oob_first;
6513
-
6514
- case NAND_ECC_HW:
6515
- /* Use standard hwecc read page function? */
6516
- if (!ecc->read_page)
6517
- ecc->read_page = nand_read_page_hwecc;
6518
- if (!ecc->write_page)
6519
- ecc->write_page = nand_write_page_hwecc;
6520
- if (!ecc->read_page_raw)
6521
- ecc->read_page_raw = nand_read_page_raw;
6522
- if (!ecc->write_page_raw)
6523
- ecc->write_page_raw = nand_write_page_raw;
6524
- if (!ecc->read_oob)
6525
- ecc->read_oob = nand_read_oob_std;
6526
- if (!ecc->write_oob)
6527
- ecc->write_oob = nand_write_oob_std;
6528
- if (!ecc->read_subpage)
6529
- ecc->read_subpage = nand_read_subpage;
6530
- if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
6531
- ecc->write_subpage = nand_write_subpage_hwecc;
6532
-
6533
- case NAND_ECC_HW_SYNDROME:
6534
- if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
6535
- (!ecc->read_page ||
6536
- ecc->read_page == nand_read_page_hwecc ||
6537
- !ecc->write_page ||
6538
- ecc->write_page == nand_write_page_hwecc)) {
6539
- WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6540
- ret = -EINVAL;
6541
- goto err_nand_manuf_cleanup;
6542
- }
6543
- /* Use standard syndrome read/write page function? */
6544
- if (!ecc->read_page)
6545
- ecc->read_page = nand_read_page_syndrome;
6546
- if (!ecc->write_page)
6547
- ecc->write_page = nand_write_page_syndrome;
6548
- if (!ecc->read_page_raw)
6549
- ecc->read_page_raw = nand_read_page_raw_syndrome;
6550
- if (!ecc->write_page_raw)
6551
- ecc->write_page_raw = nand_write_page_raw_syndrome;
6552
- if (!ecc->read_oob)
6553
- ecc->read_oob = nand_read_oob_syndrome;
6554
- if (!ecc->write_oob)
6555
- ecc->write_oob = nand_write_oob_syndrome;
65565680
65575681 if (mtd->writesize >= ecc->size) {
65585682 if (!ecc->strength) {
....@@ -6564,18 +5688,17 @@
65645688 }
65655689 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
65665690 ecc->size, mtd->writesize);
6567
- ecc->mode = NAND_ECC_SOFT;
6568
- ecc->algo = NAND_ECC_HAMMING;
5691
+ ecc->engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
5692
+ ecc->algo = NAND_ECC_ALGO_HAMMING;
5693
+ fallthrough;
65695694
6570
- case NAND_ECC_SOFT:
6571
- ret = nand_set_ecc_soft_ops(mtd);
6572
- if (ret) {
6573
- ret = -EINVAL;
5695
+ case NAND_ECC_ENGINE_TYPE_SOFT:
5696
+ ret = nand_set_ecc_soft_ops(chip);
5697
+ if (ret)
65745698 goto err_nand_manuf_cleanup;
6575
- }
65765699 break;
65775700
6578
- case NAND_ECC_ON_DIE:
5701
+ case NAND_ECC_ENGINE_TYPE_ON_DIE:
65795702 if (!ecc->read_page || !ecc->write_page) {
65805703 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
65815704 ret = -EINVAL;
....@@ -6587,8 +5710,8 @@
65875710 ecc->write_oob = nand_write_oob_std;
65885711 break;
65895712
6590
- case NAND_ECC_NONE:
6591
- pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
5713
+ case NAND_ECC_ENGINE_TYPE_NONE:
5714
+ pr_warn("NAND_ECC_ENGINE_TYPE_NONE selected by board driver. This is not recommended!\n");
65925715 ecc->read_page = nand_read_page_raw;
65935716 ecc->write_page = nand_write_page_raw;
65945717 ecc->read_oob = nand_read_oob_std;
....@@ -6601,7 +5724,7 @@
66015724 break;
66025725
66035726 default:
6604
- WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
5727
+ WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->engine_type);
66055728 ret = -EINVAL;
66065729 goto err_nand_manuf_cleanup;
66075730 }
....@@ -6635,7 +5758,10 @@
66355758 ret = -EINVAL;
66365759 goto err_nand_manuf_cleanup;
66375760 }
5761
+
66385762 ecc->total = ecc->steps * ecc->bytes;
5763
+ chip->base.ecc.ctx.total = ecc->total;
5764
+
66395765 if (ecc->total > mtd->oobsize) {
66405766 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
66415767 ret = -EINVAL;
....@@ -6653,9 +5779,11 @@
66535779 mtd->oobavail = ret;
66545780
66555781 /* ECC sanity check: warn if it's too weak */
6656
- if (!nand_ecc_strength_good(mtd))
6657
- pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
6658
- mtd->name);
5782
+ if (!nand_ecc_is_strong_enough(&chip->base))
5783
+ pr_warn("WARNING: %s: the ECC used on your system (%db/%dB) is too weak compared to the one required by the NAND chip (%db/%dB)\n",
5784
+ mtd->name, chip->ecc.strength, chip->ecc.size,
5785
+ nanddev_get_ecc_requirements(&chip->base)->strength,
5786
+ nanddev_get_ecc_requirements(&chip->base)->step_size);
66595787
66605788 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
66615789 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
....@@ -6672,15 +5800,12 @@
66725800 }
66735801 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
66745802
6675
- /* Initialize state */
6676
- chip->state = FL_READY;
6677
-
66785803 /* Invalidate the pagebuffer reference */
6679
- chip->pagebuf = -1;
5804
+ chip->pagecache.page = -1;
66805805
66815806 /* Large page NAND with SOFT_ECC should support subpage reads */
6682
- switch (ecc->mode) {
6683
- case NAND_ECC_SOFT:
5807
+ switch (ecc->engine_type) {
5808
+ case NAND_ECC_ENGINE_TYPE_SOFT:
66845809 if (chip->page_shift > 9)
66855810 chip->options |= NAND_SUBPAGE_READ;
66865811 break;
....@@ -6689,10 +5814,15 @@
66895814 break;
66905815 }
66915816
5817
+ ret = nanddev_init(&chip->base, &rawnand_ops, mtd->owner);
5818
+ if (ret)
5819
+ goto err_nand_manuf_cleanup;
5820
+
5821
+ /* Adjust the MTD_CAP_ flags when NAND_ROM is set. */
5822
+ if (chip->options & NAND_ROM)
5823
+ mtd->flags = MTD_CAP_ROM;
5824
+
66925825 /* Fill in remaining MTD driver data */
6693
- mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
6694
- mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
6695
- MTD_CAP_NANDFLASH;
66965826 mtd->_erase = nand_erase;
66975827 mtd->_point = NULL;
66985828 mtd->_unpoint = NULL;
....@@ -6700,16 +5830,15 @@
67005830 mtd->_read_oob = nand_read_oob;
67015831 mtd->_write_oob = nand_write_oob;
67025832 mtd->_sync = nand_sync;
6703
- mtd->_lock = NULL;
6704
- mtd->_unlock = NULL;
5833
+ mtd->_lock = nand_lock;
5834
+ mtd->_unlock = nand_unlock;
67055835 mtd->_suspend = nand_suspend;
67065836 mtd->_resume = nand_resume;
67075837 mtd->_reboot = nand_shutdown;
67085838 mtd->_block_isreserved = nand_block_isreserved;
67095839 mtd->_block_isbad = nand_block_isbad;
67105840 mtd->_block_markbad = nand_block_markbad;
6711
- mtd->_max_bad_blocks = nand_max_bad_blocks;
6712
- mtd->writebufsize = mtd->writesize;
5841
+ mtd->_max_bad_blocks = nanddev_mtd_max_bad_blocks;
67135842
67145843 /*
67155844 * Initialize bitflip_threshold to its default prior scan_bbt() call.
....@@ -6719,16 +5848,16 @@
67195848 if (!mtd->bitflip_threshold)
67205849 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
67215850
6722
- /* Initialize the ->data_interface field. */
6723
- ret = nand_init_data_interface(chip);
5851
+ /* Find the fastest data interface for this chip */
5852
+ ret = nand_choose_interface_config(chip);
67245853 if (ret)
6725
- goto err_nand_manuf_cleanup;
5854
+ goto err_nanddev_cleanup;
67265855
67275856 /* Enter fastest possible mode on all dies. */
6728
- for (i = 0; i < chip->numchips; i++) {
6729
- ret = nand_setup_data_interface(chip, i);
5857
+ for (i = 0; i < nanddev_ntargets(&chip->base); i++) {
5858
+ ret = nand_setup_interface(chip, i);
67305859 if (ret)
6731
- goto err_nand_manuf_cleanup;
5860
+ goto err_free_interface_config;
67325861 }
67335862
67345863 /* Check, if we should skip the bad block table scan */
....@@ -6738,10 +5867,15 @@
67385867 /* Build bad block table */
67395868 ret = nand_create_bbt(chip);
67405869 if (ret)
6741
- goto err_nand_manuf_cleanup;
5870
+ goto err_free_interface_config;
67425871
67435872 return 0;
67445873
5874
+err_free_interface_config:
5875
+ kfree(chip->best_interface_config);
5876
+
5877
+err_nanddev_cleanup:
5878
+ nanddev_cleanup(&chip->base);
67455879
67465880 err_nand_manuf_cleanup:
67475881 nand_manufacturer_cleanup(chip);
....@@ -6771,25 +5905,24 @@
67715905 /**
67725906 * nand_scan_with_ids - [NAND Interface] Scan for the NAND device
67735907 * @chip: NAND chip object
6774
- * @maxchips: number of chips to scan for. @nand_scan_ident() will not be run if
6775
- * this parameter is zero (useful for specific drivers that must
6776
- * handle this part of the process themselves, e.g docg4).
5908
+ * @maxchips: number of chips to scan for.
67775909 * @ids: optional flash IDs table
67785910 *
67795911 * This fills out all the uninitialized function pointers with the defaults.
67805912 * The flash ID is read and the mtd/chip structures are filled with the
67815913 * appropriate values.
67825914 */
6783
-int nand_scan_with_ids(struct nand_chip *chip, int maxchips,
5915
+int nand_scan_with_ids(struct nand_chip *chip, unsigned int maxchips,
67845916 struct nand_flash_dev *ids)
67855917 {
67865918 int ret;
67875919
6788
- if (maxchips) {
6789
- ret = nand_scan_ident(chip, maxchips, ids);
6790
- if (ret)
6791
- return ret;
6792
- }
5920
+ if (!maxchips)
5921
+ return -EINVAL;
5922
+
5923
+ ret = nand_scan_ident(chip, maxchips, ids);
5924
+ if (ret)
5925
+ return ret;
67935926
67945927 ret = nand_attach(chip);
67955928 if (ret)
....@@ -6816,9 +5949,11 @@
68165949 */
68175950 void nand_cleanup(struct nand_chip *chip)
68185951 {
6819
- if (chip->ecc.mode == NAND_ECC_SOFT &&
6820
- chip->ecc.algo == NAND_ECC_BCH)
5952
+ if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT &&
5953
+ chip->ecc.algo == NAND_ECC_ALGO_BCH)
68215954 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
5955
+
5956
+ nanddev_cleanup(&chip->base);
68225957
68235958 /* Free bad block table memory */
68245959 kfree(chip->bbt);
....@@ -6831,6 +5966,9 @@
68315966 & NAND_BBT_DYNAMICSTRUCT)
68325967 kfree(chip->badblock_pattern);
68335968
5969
+ /* Free the data interface */
5970
+ kfree(chip->best_interface_config);
5971
+
68345972 /* Free manufacturer priv data. */
68355973 nand_manufacturer_cleanup(chip);
68365974
....@@ -6842,18 +5980,6 @@
68425980 }
68435981
68445982 EXPORT_SYMBOL_GPL(nand_cleanup);
6845
-
6846
-/**
6847
- * nand_release - [NAND Interface] Unregister the MTD device and free resources
6848
- * held by the NAND device
6849
- * @chip: NAND chip object
6850
- */
6851
-void nand_release(struct nand_chip *chip)
6852
-{
6853
- mtd_device_unregister(nand_to_mtd(chip));
6854
- nand_cleanup(chip);
6855
-}
6856
-EXPORT_SYMBOL_GPL(nand_release);
68575983
68585984 MODULE_LICENSE("GPL");
68595985 MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");