Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Handles the M-Systems DiskOnChip G3 chip
4 *
5 * Copyright (C) 2011 Robert Jarzmik
6 */
7
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/errno.h>
11#include <linux/of.h>
12#include <linux/platform_device.h>
13#include <linux/string.h>
14#include <linux/slab.h>
15#include <linux/io.h>
16#include <linux/delay.h>
17#include <linux/mtd/mtd.h>
18#include <linux/mtd/partitions.h>
19#include <linux/bitmap.h>
20#include <linux/bitrev.h>
21#include <linux/bch.h>
22
23#include <linux/debugfs.h>
24#include <linux/seq_file.h>
25
26#define CREATE_TRACE_POINTS
27#include "docg3.h"
28
29/*
30 * This driver handles the DiskOnChip G3 flash memory.
31 *
32 * As no specification is available from M-Systems/Sandisk, this drivers lacks
33 * several functions available on the chip, as :
34 * - IPL write
35 *
36 * The bus data width (8bits versus 16bits) is not handled (if_cfg flag), and
37 * the driver assumes a 16bits data bus.
38 *
39 * DocG3 relies on 2 ECC algorithms, which are handled in hardware :
40 * - a 1 byte Hamming code stored in the OOB for each page
41 * - a 7 bytes BCH code stored in the OOB for each page
42 * The BCH ECC is :
43 * - BCH is in GF(2^14)
44 * - BCH is over data of 520 bytes (512 page + 7 page_info bytes
45 * + 1 hamming byte)
46 * - BCH can correct up to 4 bits (t = 4)
47 * - BCH syndroms are calculated in hardware, and checked in hardware as well
48 *
49 */
50
51static unsigned int reliable_mode;
52module_param(reliable_mode, uint, 0);
53MODULE_PARM_DESC(reliable_mode, "Set the docg3 mode (0=normal MLC, 1=fast, "
54 "2=reliable) : MLC normal operations are in normal mode");
55
56static int docg3_ooblayout_ecc(struct mtd_info *mtd, int section,
57 struct mtd_oob_region *oobregion)
58{
59 if (section)
60 return -ERANGE;
61
62 /* byte 7 is Hamming ECC, byte 8-14 are BCH ECC */
63 oobregion->offset = 7;
64 oobregion->length = 8;
65
66 return 0;
67}
68
69static int docg3_ooblayout_free(struct mtd_info *mtd, int section,
70 struct mtd_oob_region *oobregion)
71{
72 if (section > 1)
73 return -ERANGE;
74
75 /* free bytes: byte 0 until byte 6, byte 15 */
76 if (!section) {
77 oobregion->offset = 0;
78 oobregion->length = 7;
79 } else {
80 oobregion->offset = 15;
81 oobregion->length = 1;
82 }
83
84 return 0;
85}
86
87static const struct mtd_ooblayout_ops nand_ooblayout_docg3_ops = {
88 .ecc = docg3_ooblayout_ecc,
89 .free = docg3_ooblayout_free,
90};
91
92static inline u8 doc_readb(struct docg3 *docg3, u16 reg)
93{
94 u8 val = readb(docg3->cascade->base + reg);
95
96 trace_docg3_io(0, 8, reg, (int)val);
97 return val;
98}
99
100static inline u16 doc_readw(struct docg3 *docg3, u16 reg)
101{
102 u16 val = readw(docg3->cascade->base + reg);
103
104 trace_docg3_io(0, 16, reg, (int)val);
105 return val;
106}
107
108static inline void doc_writeb(struct docg3 *docg3, u8 val, u16 reg)
109{
110 writeb(val, docg3->cascade->base + reg);
111 trace_docg3_io(1, 8, reg, val);
112}
113
114static inline void doc_writew(struct docg3 *docg3, u16 val, u16 reg)
115{
116 writew(val, docg3->cascade->base + reg);
117 trace_docg3_io(1, 16, reg, val);
118}
119
120static inline void doc_flash_command(struct docg3 *docg3, u8 cmd)
121{
122 doc_writeb(docg3, cmd, DOC_FLASHCOMMAND);
123}
124
125static inline void doc_flash_sequence(struct docg3 *docg3, u8 seq)
126{
127 doc_writeb(docg3, seq, DOC_FLASHSEQUENCE);
128}
129
130static inline void doc_flash_address(struct docg3 *docg3, u8 addr)
131{
132 doc_writeb(docg3, addr, DOC_FLASHADDRESS);
133}
134
135static char const * const part_probes[] = { "cmdlinepart", "saftlpart", NULL };
136
137static int doc_register_readb(struct docg3 *docg3, int reg)
138{
139 u8 val;
140
141 doc_writew(docg3, reg, DOC_READADDRESS);
142 val = doc_readb(docg3, reg);
143 doc_vdbg("Read register %04x : %02x\n", reg, val);
144 return val;
145}
146
147static int doc_register_readw(struct docg3 *docg3, int reg)
148{
149 u16 val;
150
151 doc_writew(docg3, reg, DOC_READADDRESS);
152 val = doc_readw(docg3, reg);
153 doc_vdbg("Read register %04x : %04x\n", reg, val);
154 return val;
155}
156
157/**
158 * doc_delay - delay docg3 operations
159 * @docg3: the device
160 * @nbNOPs: the number of NOPs to issue
161 *
162 * As no specification is available, the right timings between chip commands are
163 * unknown. The only available piece of information are the observed nops on a
164 * working docg3 chip.
165 * Therefore, doc_delay relies on a busy loop of NOPs, instead of scheduler
166 * friendlier msleep() functions or blocking mdelay().
167 */
168static void doc_delay(struct docg3 *docg3, int nbNOPs)
169{
170 int i;
171
172 doc_vdbg("NOP x %d\n", nbNOPs);
173 for (i = 0; i < nbNOPs; i++)
174 doc_writeb(docg3, 0, DOC_NOP);
175}
176
177static int is_prot_seq_error(struct docg3 *docg3)
178{
179 int ctrl;
180
181 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
182 return ctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR);
183}
184
185static int doc_is_ready(struct docg3 *docg3)
186{
187 int ctrl;
188
189 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
190 return ctrl & DOC_CTRL_FLASHREADY;
191}
192
193static int doc_wait_ready(struct docg3 *docg3)
194{
195 int maxWaitCycles = 100;
196
197 do {
198 doc_delay(docg3, 4);
199 cpu_relax();
200 } while (!doc_is_ready(docg3) && maxWaitCycles--);
201 doc_delay(docg3, 2);
202 if (maxWaitCycles > 0)
203 return 0;
204 else
205 return -EIO;
206}
207
208static int doc_reset_seq(struct docg3 *docg3)
209{
210 int ret;
211
212 doc_writeb(docg3, 0x10, DOC_FLASHCONTROL);
213 doc_flash_sequence(docg3, DOC_SEQ_RESET);
214 doc_flash_command(docg3, DOC_CMD_RESET);
215 doc_delay(docg3, 2);
216 ret = doc_wait_ready(docg3);
217
218 doc_dbg("doc_reset_seq() -> isReady=%s\n", ret ? "false" : "true");
219 return ret;
220}
221
222/**
223 * doc_read_data_area - Read data from data area
224 * @docg3: the device
225 * @buf: the buffer to fill in (might be NULL is dummy reads)
226 * @len: the length to read
227 * @first: first time read, DOC_READADDRESS should be set
228 *
229 * Reads bytes from flash data. Handles the single byte / even bytes reads.
230 */
231static void doc_read_data_area(struct docg3 *docg3, void *buf, int len,
232 int first)
233{
234 int i, cdr, len4;
235 u16 data16, *dst16;
236 u8 data8, *dst8;
237
238 doc_dbg("doc_read_data_area(buf=%p, len=%d)\n", buf, len);
239 cdr = len & 0x1;
240 len4 = len - cdr;
241
242 if (first)
243 doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
244 dst16 = buf;
245 for (i = 0; i < len4; i += 2) {
246 data16 = doc_readw(docg3, DOC_IOSPACE_DATA);
247 if (dst16) {
248 *dst16 = data16;
249 dst16++;
250 }
251 }
252
253 if (cdr) {
254 doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
255 DOC_READADDRESS);
256 doc_delay(docg3, 1);
257 dst8 = (u8 *)dst16;
258 for (i = 0; i < cdr; i++) {
259 data8 = doc_readb(docg3, DOC_IOSPACE_DATA);
260 if (dst8) {
261 *dst8 = data8;
262 dst8++;
263 }
264 }
265 }
266}
267
268/**
269 * doc_write_data_area - Write data into data area
270 * @docg3: the device
271 * @buf: the buffer to get input bytes from
272 * @len: the length to write
273 *
274 * Writes bytes into flash data. Handles the single byte / even bytes writes.
275 */
276static void doc_write_data_area(struct docg3 *docg3, const void *buf, int len)
277{
278 int i, cdr, len4;
279 u16 *src16;
280 u8 *src8;
281
282 doc_dbg("doc_write_data_area(buf=%p, len=%d)\n", buf, len);
283 cdr = len & 0x3;
284 len4 = len - cdr;
285
286 doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
287 src16 = (u16 *)buf;
288 for (i = 0; i < len4; i += 2) {
289 doc_writew(docg3, *src16, DOC_IOSPACE_DATA);
290 src16++;
291 }
292
293 src8 = (u8 *)src16;
294 for (i = 0; i < cdr; i++) {
295 doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
296 DOC_READADDRESS);
297 doc_writeb(docg3, *src8, DOC_IOSPACE_DATA);
298 src8++;
299 }
300}
301
302/**
303 * doc_set_reliable_mode - Sets the flash to normal or reliable data mode
304 * @docg3: the device
305 *
306 * The reliable data mode is a bit slower than the fast mode, but less errors
307 * occur. Entering the reliable mode cannot be done without entering the fast
308 * mode first.
309 *
310 * In reliable mode, pages 2*n and 2*n+1 are clones. Writing to page 0 of blocks
311 * (4,5) make the hardware write also to page 1 of blocks blocks(4,5). Reading
312 * from page 0 of blocks (4,5) or from page 1 of blocks (4,5) gives the same
313 * result, which is a logical and between bytes from page 0 and page 1 (which is
314 * consistent with the fact that writing to a page is _clearing_ bits of that
315 * page).
316 */
317static void doc_set_reliable_mode(struct docg3 *docg3)
318{
319 static char *strmode[] = { "normal", "fast", "reliable", "invalid" };
320
321 doc_dbg("doc_set_reliable_mode(%s)\n", strmode[docg3->reliable]);
322 switch (docg3->reliable) {
323 case 0:
324 break;
325 case 1:
326 doc_flash_sequence(docg3, DOC_SEQ_SET_FASTMODE);
327 doc_flash_command(docg3, DOC_CMD_FAST_MODE);
328 break;
329 case 2:
330 doc_flash_sequence(docg3, DOC_SEQ_SET_RELIABLEMODE);
331 doc_flash_command(docg3, DOC_CMD_FAST_MODE);
332 doc_flash_command(docg3, DOC_CMD_RELIABLE_MODE);
333 break;
334 default:
335 doc_err("doc_set_reliable_mode(): invalid mode\n");
336 break;
337 }
338 doc_delay(docg3, 2);
339}
340
341/**
342 * doc_set_asic_mode - Set the ASIC mode
343 * @docg3: the device
344 * @mode: the mode
345 *
346 * The ASIC can work in 3 modes :
347 * - RESET: all registers are zeroed
348 * - NORMAL: receives and handles commands
349 * - POWERDOWN: minimal poweruse, flash parts shut off
350 */
351static void doc_set_asic_mode(struct docg3 *docg3, u8 mode)
352{
353 int i;
354
355 for (i = 0; i < 12; i++)
356 doc_readb(docg3, DOC_IOSPACE_IPL);
357
358 mode |= DOC_ASICMODE_MDWREN;
359 doc_dbg("doc_set_asic_mode(%02x)\n", mode);
360 doc_writeb(docg3, mode, DOC_ASICMODE);
361 doc_writeb(docg3, ~mode, DOC_ASICMODECONFIRM);
362 doc_delay(docg3, 1);
363}
364
365/**
366 * doc_set_device_id - Sets the devices id for cascaded G3 chips
367 * @docg3: the device
368 * @id: the chip to select (amongst 0, 1, 2, 3)
369 *
370 * There can be 4 cascaded G3 chips. This function selects the one which will
371 * should be the active one.
372 */
373static void doc_set_device_id(struct docg3 *docg3, int id)
374{
375 u8 ctrl;
376
377 doc_dbg("doc_set_device_id(%d)\n", id);
378 doc_writeb(docg3, id, DOC_DEVICESELECT);
379 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
380
381 ctrl &= ~DOC_CTRL_VIOLATION;
382 ctrl |= DOC_CTRL_CE;
383 doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
384}
385
386/**
387 * doc_set_extra_page_mode - Change flash page layout
388 * @docg3: the device
389 *
390 * Normally, the flash page is split into the data (512 bytes) and the out of
391 * band data (16 bytes). For each, 4 more bytes can be accessed, where the wear
392 * leveling counters are stored. To access this last area of 4 bytes, a special
393 * mode must be input to the flash ASIC.
394 *
395 * Returns 0 if no error occurred, -EIO else.
396 */
397static int doc_set_extra_page_mode(struct docg3 *docg3)
398{
399 int fctrl;
400
401 doc_dbg("doc_set_extra_page_mode()\n");
402 doc_flash_sequence(docg3, DOC_SEQ_PAGE_SIZE_532);
403 doc_flash_command(docg3, DOC_CMD_PAGE_SIZE_532);
404 doc_delay(docg3, 2);
405
406 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
407 if (fctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR))
408 return -EIO;
409 else
410 return 0;
411}
412
413/**
414 * doc_setup_addr_sector - Setup blocks/page/ofs address for one plane
415 * @docg3: the device
416 * @sector: the sector
417 */
418static void doc_setup_addr_sector(struct docg3 *docg3, int sector)
419{
420 doc_delay(docg3, 1);
421 doc_flash_address(docg3, sector & 0xff);
422 doc_flash_address(docg3, (sector >> 8) & 0xff);
423 doc_flash_address(docg3, (sector >> 16) & 0xff);
424 doc_delay(docg3, 1);
425}
426
427/**
428 * doc_setup_writeaddr_sector - Setup blocks/page/ofs address for one plane
429 * @docg3: the device
430 * @sector: the sector
431 * @ofs: the offset in the page, between 0 and (512 + 16 + 512)
432 */
433static void doc_setup_writeaddr_sector(struct docg3 *docg3, int sector, int ofs)
434{
435 ofs = ofs >> 2;
436 doc_delay(docg3, 1);
437 doc_flash_address(docg3, ofs & 0xff);
438 doc_flash_address(docg3, sector & 0xff);
439 doc_flash_address(docg3, (sector >> 8) & 0xff);
440 doc_flash_address(docg3, (sector >> 16) & 0xff);
441 doc_delay(docg3, 1);
442}
443
444/**
445 * doc_read_seek - Set both flash planes to the specified block, page for reading
446 * @docg3: the device
447 * @block0: the first plane block index
448 * @block1: the second plane block index
449 * @page: the page index within the block
450 * @wear: if true, read will occur on the 4 extra bytes of the wear area
451 * @ofs: offset in page to read
452 *
453 * Programs the flash even and odd planes to the specific block and page.
454 * Alternatively, programs the flash to the wear area of the specified page.
455 */
456static int doc_read_seek(struct docg3 *docg3, int block0, int block1, int page,
457 int wear, int ofs)
458{
459 int sector, ret = 0;
460
461 doc_dbg("doc_seek(blocks=(%d,%d), page=%d, ofs=%d, wear=%d)\n",
462 block0, block1, page, ofs, wear);
463
464 if (!wear && (ofs < 2 * DOC_LAYOUT_PAGE_SIZE)) {
465 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
466 doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
467 doc_delay(docg3, 2);
468 } else {
469 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
470 doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
471 doc_delay(docg3, 2);
472 }
473
474 doc_set_reliable_mode(docg3);
475 if (wear)
476 ret = doc_set_extra_page_mode(docg3);
477 if (ret)
478 goto out;
479
480 doc_flash_sequence(docg3, DOC_SEQ_READ);
481 sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
482 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
483 doc_setup_addr_sector(docg3, sector);
484
485 sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
486 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
487 doc_setup_addr_sector(docg3, sector);
488 doc_delay(docg3, 1);
489
490out:
491 return ret;
492}
493
494/**
495 * doc_write_seek - Set both flash planes to the specified block, page for writing
496 * @docg3: the device
497 * @block0: the first plane block index
498 * @block1: the second plane block index
499 * @page: the page index within the block
500 * @ofs: offset in page to write
501 *
502 * Programs the flash even and odd planes to the specific block and page.
503 * Alternatively, programs the flash to the wear area of the specified page.
504 */
505static int doc_write_seek(struct docg3 *docg3, int block0, int block1, int page,
506 int ofs)
507{
508 int ret = 0, sector;
509
510 doc_dbg("doc_write_seek(blocks=(%d,%d), page=%d, ofs=%d)\n",
511 block0, block1, page, ofs);
512
513 doc_set_reliable_mode(docg3);
514
515 if (ofs < 2 * DOC_LAYOUT_PAGE_SIZE) {
516 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
517 doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
518 doc_delay(docg3, 2);
519 } else {
520 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
521 doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
522 doc_delay(docg3, 2);
523 }
524
525 doc_flash_sequence(docg3, DOC_SEQ_PAGE_SETUP);
526 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1);
527
528 sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
529 doc_setup_writeaddr_sector(docg3, sector, ofs);
530
531 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE3);
532 doc_delay(docg3, 2);
533 ret = doc_wait_ready(docg3);
534 if (ret)
535 goto out;
536
537 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1);
538 sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
539 doc_setup_writeaddr_sector(docg3, sector, ofs);
540 doc_delay(docg3, 1);
541
542out:
543 return ret;
544}
545
546
547/**
548 * doc_read_page_ecc_init - Initialize hardware ECC engine
549 * @docg3: the device
550 * @len: the number of bytes covered by the ECC (BCH covered)
551 *
552 * The function does initialize the hardware ECC engine to compute the Hamming
553 * ECC (on 1 byte) and the BCH hardware ECC (on 7 bytes).
554 *
555 * Return 0 if succeeded, -EIO on error
556 */
557static int doc_read_page_ecc_init(struct docg3 *docg3, int len)
558{
559 doc_writew(docg3, DOC_ECCCONF0_READ_MODE
560 | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
561 | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
562 DOC_ECCCONF0);
563 doc_delay(docg3, 4);
564 doc_register_readb(docg3, DOC_FLASHCONTROL);
565 return doc_wait_ready(docg3);
566}
567
568/**
569 * doc_write_page_ecc_init - Initialize hardware BCH ECC engine
570 * @docg3: the device
571 * @len: the number of bytes covered by the ECC (BCH covered)
572 *
573 * The function does initialize the hardware ECC engine to compute the Hamming
574 * ECC (on 1 byte) and the BCH hardware ECC (on 7 bytes).
575 *
576 * Return 0 if succeeded, -EIO on error
577 */
578static int doc_write_page_ecc_init(struct docg3 *docg3, int len)
579{
580 doc_writew(docg3, DOC_ECCCONF0_WRITE_MODE
581 | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
582 | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
583 DOC_ECCCONF0);
584 doc_delay(docg3, 4);
585 doc_register_readb(docg3, DOC_FLASHCONTROL);
586 return doc_wait_ready(docg3);
587}
588
589/**
590 * doc_ecc_disable - Disable Hamming and BCH ECC hardware calculator
591 * @docg3: the device
592 *
593 * Disables the hardware ECC generator and checker, for unchecked reads (as when
594 * reading OOB only or write status byte).
595 */
596static void doc_ecc_disable(struct docg3 *docg3)
597{
598 doc_writew(docg3, DOC_ECCCONF0_READ_MODE, DOC_ECCCONF0);
599 doc_delay(docg3, 4);
600}
601
602/**
603 * doc_hamming_ecc_init - Initialize hardware Hamming ECC engine
604 * @docg3: the device
605 * @nb_bytes: the number of bytes covered by the ECC (Hamming covered)
606 *
607 * This function programs the ECC hardware to compute the hamming code on the
608 * last provided N bytes to the hardware generator.
609 */
610static void doc_hamming_ecc_init(struct docg3 *docg3, int nb_bytes)
611{
612 u8 ecc_conf1;
613
614 ecc_conf1 = doc_register_readb(docg3, DOC_ECCCONF1);
615 ecc_conf1 &= ~DOC_ECCCONF1_HAMMING_BITS_MASK;
616 ecc_conf1 |= (nb_bytes & DOC_ECCCONF1_HAMMING_BITS_MASK);
617 doc_writeb(docg3, ecc_conf1, DOC_ECCCONF1);
618}
619
620/**
621 * doc_ecc_bch_fix_data - Fix if need be read data from flash
622 * @docg3: the device
623 * @buf: the buffer of read data (512 + 7 + 1 bytes)
624 * @hwecc: the hardware calculated ECC.
625 * It's in fact recv_ecc ^ calc_ecc, where recv_ecc was read from OOB
626 * area data, and calc_ecc the ECC calculated by the hardware generator.
627 *
628 * Checks if the received data matches the ECC, and if an error is detected,
629 * tries to fix the bit flips (at most 4) in the buffer buf. As the docg3
630 * understands the (data, ecc, syndroms) in an inverted order in comparison to
631 * the BCH library, the function reverses the order of bits (ie. bit7 and bit0,
632 * bit6 and bit 1, ...) for all ECC data.
633 *
634 * The hardware ecc unit produces oob_ecc ^ calc_ecc. The kernel's bch
635 * algorithm is used to decode this. However the hw operates on page
636 * data in a bit order that is the reverse of that of the bch alg,
637 * requiring that the bits be reversed on the result. Thanks to Ivan
638 * Djelic for his analysis.
639 *
640 * Returns number of fixed bits (0, 1, 2, 3, 4) or -EBADMSG if too many bit
641 * errors were detected and cannot be fixed.
642 */
643static int doc_ecc_bch_fix_data(struct docg3 *docg3, void *buf, u8 *hwecc)
644{
645 u8 ecc[DOC_ECC_BCH_SIZE];
646 int errorpos[DOC_ECC_BCH_T], i, numerrs;
647
648 for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
649 ecc[i] = bitrev8(hwecc[i]);
650 numerrs = bch_decode(docg3->cascade->bch, NULL,
651 DOC_ECC_BCH_COVERED_BYTES,
652 NULL, ecc, NULL, errorpos);
653 BUG_ON(numerrs == -EINVAL);
654 if (numerrs < 0)
655 goto out;
656
657 for (i = 0; i < numerrs; i++)
658 errorpos[i] = (errorpos[i] & ~7) | (7 - (errorpos[i] & 7));
659 for (i = 0; i < numerrs; i++)
660 if (errorpos[i] < DOC_ECC_BCH_COVERED_BYTES*8)
661 /* error is located in data, correct it */
662 change_bit(errorpos[i], buf);
663out:
664 doc_dbg("doc_ecc_bch_fix_data: flipped %d bits\n", numerrs);
665 return numerrs;
666}
667
668
669/**
670 * doc_read_page_prepare - Prepares reading data from a flash page
671 * @docg3: the device
672 * @block0: the first plane block index on flash memory
673 * @block1: the second plane block index on flash memory
674 * @page: the page index in the block
675 * @offset: the offset in the page (must be a multiple of 4)
676 *
677 * Prepares the page to be read in the flash memory :
678 * - tell ASIC to map the flash pages
679 * - tell ASIC to be in read mode
680 *
681 * After a call to this method, a call to doc_read_page_finish is mandatory,
682 * to end the read cycle of the flash.
683 *
684 * Read data from a flash page. The length to be read must be between 0 and
685 * (page_size + oob_size + wear_size), ie. 532, and a multiple of 4 (because
686 * the extra bytes reading is not implemented).
687 *
688 * As pages are grouped by 2 (in 2 planes), reading from a page must be done
689 * in two steps:
690 * - one read of 512 bytes at offset 0
691 * - one read of 512 bytes at offset 512 + 16
692 *
693 * Returns 0 if successful, -EIO if a read error occurred.
694 */
695static int doc_read_page_prepare(struct docg3 *docg3, int block0, int block1,
696 int page, int offset)
697{
698 int wear_area = 0, ret = 0;
699
700 doc_dbg("doc_read_page_prepare(blocks=(%d,%d), page=%d, ofsInPage=%d)\n",
701 block0, block1, page, offset);
702 if (offset >= DOC_LAYOUT_WEAR_OFFSET)
703 wear_area = 1;
704 if (!wear_area && offset > (DOC_LAYOUT_PAGE_OOB_SIZE * 2))
705 return -EINVAL;
706
707 doc_set_device_id(docg3, docg3->device_id);
708 ret = doc_reset_seq(docg3);
709 if (ret)
710 goto err;
711
712 /* Program the flash address block and page */
713 ret = doc_read_seek(docg3, block0, block1, page, wear_area, offset);
714 if (ret)
715 goto err;
716
717 doc_flash_command(docg3, DOC_CMD_READ_ALL_PLANES);
718 doc_delay(docg3, 2);
719 doc_wait_ready(docg3);
720
721 doc_flash_command(docg3, DOC_CMD_SET_ADDR_READ);
722 doc_delay(docg3, 1);
723 if (offset >= DOC_LAYOUT_PAGE_SIZE * 2)
724 offset -= 2 * DOC_LAYOUT_PAGE_SIZE;
725 doc_flash_address(docg3, offset >> 2);
726 doc_delay(docg3, 1);
727 doc_wait_ready(docg3);
728
729 doc_flash_command(docg3, DOC_CMD_READ_FLASH);
730
731 return 0;
732err:
733 doc_writeb(docg3, 0, DOC_DATAEND);
734 doc_delay(docg3, 2);
735 return -EIO;
736}
737
738/**
739 * doc_read_page_getbytes - Reads bytes from a prepared page
740 * @docg3: the device
741 * @len: the number of bytes to be read (must be a multiple of 4)
742 * @buf: the buffer to be filled in (or NULL is forget bytes)
743 * @first: 1 if first time read, DOC_READADDRESS should be set
744 * @last_odd: 1 if last read ended up on an odd byte
745 *
746 * Reads bytes from a prepared page. There is a trickery here : if the last read
747 * ended up on an odd offset in the 1024 bytes double page, ie. between the 2
748 * planes, the first byte must be read apart. If a word (16bit) read was used,
749 * the read would return the byte of plane 2 as low *and* high endian, which
750 * will mess the read.
751 *
752 */
753static int doc_read_page_getbytes(struct docg3 *docg3, int len, u_char *buf,
754 int first, int last_odd)
755{
756 if (last_odd && len > 0) {
757 doc_read_data_area(docg3, buf, 1, first);
758 doc_read_data_area(docg3, buf ? buf + 1 : buf, len - 1, 0);
759 } else {
760 doc_read_data_area(docg3, buf, len, first);
761 }
762 doc_delay(docg3, 2);
763 return len;
764}
765
766/**
767 * doc_write_page_putbytes - Writes bytes into a prepared page
768 * @docg3: the device
769 * @len: the number of bytes to be written
770 * @buf: the buffer of input bytes
771 *
772 */
773static void doc_write_page_putbytes(struct docg3 *docg3, int len,
774 const u_char *buf)
775{
776 doc_write_data_area(docg3, buf, len);
777 doc_delay(docg3, 2);
778}
779
780/**
781 * doc_get_bch_hw_ecc - Get hardware calculated BCH ECC
782 * @docg3: the device
783 * @hwecc: the array of 7 integers where the hardware ecc will be stored
784 */
785static void doc_get_bch_hw_ecc(struct docg3 *docg3, u8 *hwecc)
786{
787 int i;
788
789 for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
790 hwecc[i] = doc_register_readb(docg3, DOC_BCH_HW_ECC(i));
791}
792
793/**
794 * doc_page_finish - Ends reading/writing of a flash page
795 * @docg3: the device
796 */
797static void doc_page_finish(struct docg3 *docg3)
798{
799 doc_writeb(docg3, 0, DOC_DATAEND);
800 doc_delay(docg3, 2);
801}
802
803/**
804 * doc_read_page_finish - Ends reading of a flash page
805 * @docg3: the device
806 *
807 * As a side effect, resets the chip selector to 0. This ensures that after each
808 * read operation, the floor 0 is selected. Therefore, if the systems halts, the
809 * reboot will boot on floor 0, where the IPL is.
810 */
811static void doc_read_page_finish(struct docg3 *docg3)
812{
813 doc_page_finish(docg3);
814 doc_set_device_id(docg3, 0);
815}
816
817/**
818 * calc_block_sector - Calculate blocks, pages and ofs.
819 *
820 * @from: offset in flash
821 * @block0: first plane block index calculated
822 * @block1: second plane block index calculated
823 * @page: page calculated
824 * @ofs: offset in page
825 * @reliable: 0 if docg3 in normal mode, 1 if docg3 in fast mode, 2 if docg3 in
826 * reliable mode.
827 *
828 * The calculation is based on the reliable/normal mode. In normal mode, the 64
829 * pages of a block are available. In reliable mode, as pages 2*n and 2*n+1 are
830 * clones, only 32 pages per block are available.
831 */
832static void calc_block_sector(loff_t from, int *block0, int *block1, int *page,
833 int *ofs, int reliable)
834{
835 uint sector, pages_biblock;
836
837 pages_biblock = DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_NBPLANES;
838 if (reliable == 1 || reliable == 2)
839 pages_biblock /= 2;
840
841 sector = from / DOC_LAYOUT_PAGE_SIZE;
842 *block0 = sector / pages_biblock * DOC_LAYOUT_NBPLANES;
843 *block1 = *block0 + 1;
844 *page = sector % pages_biblock;
845 *page /= DOC_LAYOUT_NBPLANES;
846 if (reliable == 1 || reliable == 2)
847 *page *= 2;
848 if (sector % 2)
849 *ofs = DOC_LAYOUT_PAGE_OOB_SIZE;
850 else
851 *ofs = 0;
852}
853
854/**
855 * doc_read_oob - Read out of band bytes from flash
856 * @mtd: the device
857 * @from: the offset from first block and first page, in bytes, aligned on page
858 * size
859 * @ops: the mtd oob structure
860 *
861 * Reads flash memory OOB area of pages.
862 *
863 * Returns 0 if read successful, of -EIO, -EINVAL if an error occurred
864 */
865static int doc_read_oob(struct mtd_info *mtd, loff_t from,
866 struct mtd_oob_ops *ops)
867{
868 struct docg3 *docg3 = mtd->priv;
869 int block0, block1, page, ret, skip, ofs = 0;
870 u8 *oobbuf = ops->oobbuf;
871 u8 *buf = ops->datbuf;
872 size_t len, ooblen, nbdata, nboob;
873 u8 hwecc[DOC_ECC_BCH_SIZE], eccconf1;
874 struct mtd_ecc_stats old_stats;
875 int max_bitflips = 0;
876
877 if (buf)
878 len = ops->len;
879 else
880 len = 0;
881 if (oobbuf)
882 ooblen = ops->ooblen;
883 else
884 ooblen = 0;
885
886 if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
887 oobbuf += ops->ooboffs;
888
889 doc_dbg("doc_read_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
890 from, ops->mode, buf, len, oobbuf, ooblen);
891 if (ooblen % DOC_LAYOUT_OOB_SIZE)
892 return -EINVAL;
893
894 ops->oobretlen = 0;
895 ops->retlen = 0;
896 ret = 0;
897 skip = from % DOC_LAYOUT_PAGE_SIZE;
898 mutex_lock(&docg3->cascade->lock);
899 old_stats = mtd->ecc_stats;
900 while (ret >= 0 && (len > 0 || ooblen > 0)) {
901 calc_block_sector(from - skip, &block0, &block1, &page, &ofs,
902 docg3->reliable);
903 nbdata = min_t(size_t, len, DOC_LAYOUT_PAGE_SIZE - skip);
904 nboob = min_t(size_t, ooblen, (size_t)DOC_LAYOUT_OOB_SIZE);
905 ret = doc_read_page_prepare(docg3, block0, block1, page, ofs);
906 if (ret < 0)
907 goto out;
908 ret = doc_read_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
909 if (ret < 0)
910 goto err_in_read;
911 ret = doc_read_page_getbytes(docg3, skip, NULL, 1, 0);
912 if (ret < skip)
913 goto err_in_read;
914 ret = doc_read_page_getbytes(docg3, nbdata, buf, 0, skip % 2);
915 if (ret < nbdata)
916 goto err_in_read;
917 doc_read_page_getbytes(docg3,
918 DOC_LAYOUT_PAGE_SIZE - nbdata - skip,
919 NULL, 0, (skip + nbdata) % 2);
920 ret = doc_read_page_getbytes(docg3, nboob, oobbuf, 0, 0);
921 if (ret < nboob)
922 goto err_in_read;
923 doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE - nboob,
924 NULL, 0, nboob % 2);
925
926 doc_get_bch_hw_ecc(docg3, hwecc);
927 eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1);
928
929 if (nboob >= DOC_LAYOUT_OOB_SIZE) {
930 doc_dbg("OOB - INFO: %*phC\n", 7, oobbuf);
931 doc_dbg("OOB - HAMMING: %02x\n", oobbuf[7]);
932 doc_dbg("OOB - BCH_ECC: %*phC\n", 7, oobbuf + 8);
933 doc_dbg("OOB - UNUSED: %02x\n", oobbuf[15]);
934 }
935 doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1);
936 doc_dbg("ECC HW_ECC: %*phC\n", 7, hwecc);
937
938 ret = -EIO;
939 if (is_prot_seq_error(docg3))
940 goto err_in_read;
941 ret = 0;
942 if ((block0 >= DOC_LAYOUT_BLOCK_FIRST_DATA) &&
943 (eccconf1 & DOC_ECCCONF1_BCH_SYNDROM_ERR) &&
944 (eccconf1 & DOC_ECCCONF1_PAGE_IS_WRITTEN) &&
945 (ops->mode != MTD_OPS_RAW) &&
946 (nbdata == DOC_LAYOUT_PAGE_SIZE)) {
947 ret = doc_ecc_bch_fix_data(docg3, buf, hwecc);
948 if (ret < 0) {
949 mtd->ecc_stats.failed++;
950 ret = -EBADMSG;
951 }
952 if (ret > 0) {
953 mtd->ecc_stats.corrected += ret;
954 max_bitflips = max(max_bitflips, ret);
955 ret = max_bitflips;
956 }
957 }
958
959 doc_read_page_finish(docg3);
960 ops->retlen += nbdata;
961 ops->oobretlen += nboob;
962 buf += nbdata;
963 oobbuf += nboob;
964 len -= nbdata;
965 ooblen -= nboob;
966 from += DOC_LAYOUT_PAGE_SIZE;
967 skip = 0;
968 }
969
970out:
971 if (ops->stats) {
972 ops->stats->uncorrectable_errors +=
973 mtd->ecc_stats.failed - old_stats.failed;
974 ops->stats->corrected_bitflips +=
975 mtd->ecc_stats.corrected - old_stats.corrected;
976 }
977 mutex_unlock(&docg3->cascade->lock);
978 return ret;
979err_in_read:
980 doc_read_page_finish(docg3);
981 goto out;
982}
983
984static int doc_reload_bbt(struct docg3 *docg3)
985{
986 int block = DOC_LAYOUT_BLOCK_BBT;
987 int ret = 0, nbpages, page;
988 u_char *buf = docg3->bbt;
989
990 nbpages = DIV_ROUND_UP(docg3->max_block + 1, 8 * DOC_LAYOUT_PAGE_SIZE);
991 for (page = 0; !ret && (page < nbpages); page++) {
992 ret = doc_read_page_prepare(docg3, block, block + 1,
993 page + DOC_LAYOUT_PAGE_BBT, 0);
994 if (!ret)
995 ret = doc_read_page_ecc_init(docg3,
996 DOC_LAYOUT_PAGE_SIZE);
997 if (!ret)
998 doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE,
999 buf, 1, 0);
1000 buf += DOC_LAYOUT_PAGE_SIZE;
1001 }
1002 doc_read_page_finish(docg3);
1003 return ret;
1004}
1005
1006/**
1007 * doc_block_isbad - Checks whether a block is good or not
1008 * @mtd: the device
1009 * @from: the offset to find the correct block
1010 *
1011 * Returns 1 if block is bad, 0 if block is good
1012 */
1013static int doc_block_isbad(struct mtd_info *mtd, loff_t from)
1014{
1015 struct docg3 *docg3 = mtd->priv;
1016 int block0, block1, page, ofs, is_good;
1017
1018 calc_block_sector(from, &block0, &block1, &page, &ofs,
1019 docg3->reliable);
1020 doc_dbg("doc_block_isbad(from=%lld) => block=(%d,%d), page=%d, ofs=%d\n",
1021 from, block0, block1, page, ofs);
1022
1023 if (block0 < DOC_LAYOUT_BLOCK_FIRST_DATA)
1024 return 0;
1025 if (block1 > docg3->max_block)
1026 return -EINVAL;
1027
1028 is_good = docg3->bbt[block0 >> 3] & (1 << (block0 & 0x7));
1029 return !is_good;
1030}
1031
1032#if 0
1033/**
1034 * doc_get_erase_count - Get block erase count
1035 * @docg3: the device
1036 * @from: the offset in which the block is.
1037 *
1038 * Get the number of times a block was erased. The number is the maximum of
1039 * erase times between first and second plane (which should be equal normally).
1040 *
1041 * Returns The number of erases, or -EINVAL or -EIO on error.
1042 */
1043static int doc_get_erase_count(struct docg3 *docg3, loff_t from)
1044{
1045 u8 buf[DOC_LAYOUT_WEAR_SIZE];
1046 int ret, plane1_erase_count, plane2_erase_count;
1047 int block0, block1, page, ofs;
1048
1049 doc_dbg("doc_get_erase_count(from=%lld, buf=%p)\n", from, buf);
1050 if (from % DOC_LAYOUT_PAGE_SIZE)
1051 return -EINVAL;
1052 calc_block_sector(from, &block0, &block1, &page, &ofs, docg3->reliable);
1053 if (block1 > docg3->max_block)
1054 return -EINVAL;
1055
1056 ret = doc_reset_seq(docg3);
1057 if (!ret)
1058 ret = doc_read_page_prepare(docg3, block0, block1, page,
1059 ofs + DOC_LAYOUT_WEAR_OFFSET, 0);
1060 if (!ret)
1061 ret = doc_read_page_getbytes(docg3, DOC_LAYOUT_WEAR_SIZE,
1062 buf, 1, 0);
1063 doc_read_page_finish(docg3);
1064
1065 if (ret || (buf[0] != DOC_ERASE_MARK) || (buf[2] != DOC_ERASE_MARK))
1066 return -EIO;
1067 plane1_erase_count = (u8)(~buf[1]) | ((u8)(~buf[4]) << 8)
1068 | ((u8)(~buf[5]) << 16);
1069 plane2_erase_count = (u8)(~buf[3]) | ((u8)(~buf[6]) << 8)
1070 | ((u8)(~buf[7]) << 16);
1071
1072 return max(plane1_erase_count, plane2_erase_count);
1073}
1074#endif
1075
1076/**
1077 * doc_get_op_status - get erase/write operation status
1078 * @docg3: the device
1079 *
1080 * Queries the status from the chip, and returns it
1081 *
1082 * Returns the status (bits DOC_PLANES_STATUS_*)
1083 */
1084static int doc_get_op_status(struct docg3 *docg3)
1085{
1086 u8 status;
1087
1088 doc_flash_sequence(docg3, DOC_SEQ_PLANES_STATUS);
1089 doc_flash_command(docg3, DOC_CMD_PLANES_STATUS);
1090 doc_delay(docg3, 5);
1091
1092 doc_ecc_disable(docg3);
1093 doc_read_data_area(docg3, &status, 1, 1);
1094 return status;
1095}
1096
1097/**
1098 * doc_write_erase_wait_status - wait for write or erase completion
1099 * @docg3: the device
1100 *
1101 * Wait for the chip to be ready again after erase or write operation, and check
1102 * erase/write status.
1103 *
1104 * Returns 0 if erase successful, -EIO if erase/write issue, -ETIMEOUT if
1105 * timeout
1106 */
1107static int doc_write_erase_wait_status(struct docg3 *docg3)
1108{
1109 int i, status, ret = 0;
1110
1111 for (i = 0; !doc_is_ready(docg3) && i < 5; i++)
1112 msleep(20);
1113 if (!doc_is_ready(docg3)) {
1114 doc_dbg("Timeout reached and the chip is still not ready\n");
1115 ret = -EAGAIN;
1116 goto out;
1117 }
1118
1119 status = doc_get_op_status(docg3);
1120 if (status & DOC_PLANES_STATUS_FAIL) {
1121 doc_dbg("Erase/Write failed on (a) plane(s), status = %x\n",
1122 status);
1123 ret = -EIO;
1124 }
1125
1126out:
1127 doc_page_finish(docg3);
1128 return ret;
1129}
1130
1131/**
1132 * doc_erase_block - Erase a couple of blocks
1133 * @docg3: the device
1134 * @block0: the first block to erase (leftmost plane)
1135 * @block1: the second block to erase (rightmost plane)
1136 *
1137 * Erase both blocks, and return operation status
1138 *
1139 * Returns 0 if erase successful, -EIO if erase issue, -ETIMEOUT if chip not
1140 * ready for too long
1141 */
1142static int doc_erase_block(struct docg3 *docg3, int block0, int block1)
1143{
1144 int ret, sector;
1145
1146 doc_dbg("doc_erase_block(blocks=(%d,%d))\n", block0, block1);
1147 ret = doc_reset_seq(docg3);
1148 if (ret)
1149 return -EIO;
1150
1151 doc_set_reliable_mode(docg3);
1152 doc_flash_sequence(docg3, DOC_SEQ_ERASE);
1153
1154 sector = block0 << DOC_ADDR_BLOCK_SHIFT;
1155 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
1156 doc_setup_addr_sector(docg3, sector);
1157 sector = block1 << DOC_ADDR_BLOCK_SHIFT;
1158 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
1159 doc_setup_addr_sector(docg3, sector);
1160 doc_delay(docg3, 1);
1161
1162 doc_flash_command(docg3, DOC_CMD_ERASECYCLE2);
1163 doc_delay(docg3, 2);
1164
1165 if (is_prot_seq_error(docg3)) {
1166 doc_err("Erase blocks %d,%d error\n", block0, block1);
1167 return -EIO;
1168 }
1169
1170 return doc_write_erase_wait_status(docg3);
1171}
1172
1173/**
1174 * doc_erase - Erase a portion of the chip
1175 * @mtd: the device
1176 * @info: the erase info
1177 *
1178 * Erase a bunch of contiguous blocks, by pairs, as a "mtd" page of 1024 is
1179 * split into 2 pages of 512 bytes on 2 contiguous blocks.
1180 *
1181 * Returns 0 if erase successful, -EINVAL if addressing error, -EIO if erase
1182 * issue
1183 */
1184static int doc_erase(struct mtd_info *mtd, struct erase_info *info)
1185{
1186 struct docg3 *docg3 = mtd->priv;
1187 uint64_t len;
1188 int block0, block1, page, ret = 0, ofs = 0;
1189
1190 doc_dbg("doc_erase(from=%lld, len=%lld\n", info->addr, info->len);
1191
1192 calc_block_sector(info->addr + info->len, &block0, &block1, &page,
1193 &ofs, docg3->reliable);
1194 if (info->addr + info->len > mtd->size || page || ofs)
1195 return -EINVAL;
1196
1197 calc_block_sector(info->addr, &block0, &block1, &page, &ofs,
1198 docg3->reliable);
1199 mutex_lock(&docg3->cascade->lock);
1200 doc_set_device_id(docg3, docg3->device_id);
1201 doc_set_reliable_mode(docg3);
1202 for (len = info->len; !ret && len > 0; len -= mtd->erasesize) {
1203 ret = doc_erase_block(docg3, block0, block1);
1204 block0 += 2;
1205 block1 += 2;
1206 }
1207 mutex_unlock(&docg3->cascade->lock);
1208
1209 return ret;
1210}
1211
1212/**
1213 * doc_write_page - Write a single page to the chip
1214 * @docg3: the device
1215 * @to: the offset from first block and first page, in bytes, aligned on page
1216 * size
1217 * @buf: buffer to get bytes from
1218 * @oob: buffer to get out of band bytes from (can be NULL if no OOB should be
1219 * written)
1220 * @autoecc: if 0, all 16 bytes from OOB are taken, regardless of HW Hamming or
1221 * BCH computations. If 1, only bytes 0-7 and byte 15 are taken,
1222 * remaining ones are filled with hardware Hamming and BCH
1223 * computations. Its value is not meaningfull is oob == NULL.
1224 *
1225 * Write one full page (ie. 1 page split on two planes), of 512 bytes, with the
1226 * OOB data. The OOB ECC is automatically computed by the hardware Hamming and
1227 * BCH generator if autoecc is not null.
1228 *
1229 * Returns 0 if write successful, -EIO if write error, -EAGAIN if timeout
1230 */
1231static int doc_write_page(struct docg3 *docg3, loff_t to, const u_char *buf,
1232 const u_char *oob, int autoecc)
1233{
1234 int block0, block1, page, ret, ofs = 0;
1235 u8 hwecc[DOC_ECC_BCH_SIZE], hamming;
1236
1237 doc_dbg("doc_write_page(to=%lld)\n", to);
1238 calc_block_sector(to, &block0, &block1, &page, &ofs, docg3->reliable);
1239
1240 doc_set_device_id(docg3, docg3->device_id);
1241 ret = doc_reset_seq(docg3);
1242 if (ret)
1243 goto err;
1244
1245 /* Program the flash address block and page */
1246 ret = doc_write_seek(docg3, block0, block1, page, ofs);
1247 if (ret)
1248 goto err;
1249
1250 doc_write_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
1251 doc_delay(docg3, 2);
1252 doc_write_page_putbytes(docg3, DOC_LAYOUT_PAGE_SIZE, buf);
1253
1254 if (oob && autoecc) {
1255 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ, oob);
1256 doc_delay(docg3, 2);
1257 oob += DOC_LAYOUT_OOB_UNUSED_OFS;
1258
1259 hamming = doc_register_readb(docg3, DOC_HAMMINGPARITY);
1260 doc_delay(docg3, 2);
1261 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_HAMMING_SZ,
1262 &hamming);
1263 doc_delay(docg3, 2);
1264
1265 doc_get_bch_hw_ecc(docg3, hwecc);
1266 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_BCH_SZ, hwecc);
1267 doc_delay(docg3, 2);
1268
1269 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_UNUSED_SZ, oob);
1270 }
1271 if (oob && !autoecc)
1272 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_SIZE, oob);
1273
1274 doc_delay(docg3, 2);
1275 doc_page_finish(docg3);
1276 doc_delay(docg3, 2);
1277 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE2);
1278 doc_delay(docg3, 2);
1279
1280 /*
1281 * The wait status will perform another doc_page_finish() call, but that
1282 * seems to please the docg3, so leave it.
1283 */
1284 ret = doc_write_erase_wait_status(docg3);
1285 return ret;
1286err:
1287 doc_read_page_finish(docg3);
1288 return ret;
1289}
1290
1291/**
1292 * doc_guess_autoecc - Guess autoecc mode from mbd_oob_ops
1293 * @ops: the oob operations
1294 *
1295 * Returns 0 or 1 if success, -EINVAL if invalid oob mode
1296 */
1297static int doc_guess_autoecc(struct mtd_oob_ops *ops)
1298{
1299 int autoecc;
1300
1301 switch (ops->mode) {
1302 case MTD_OPS_PLACE_OOB:
1303 case MTD_OPS_AUTO_OOB:
1304 autoecc = 1;
1305 break;
1306 case MTD_OPS_RAW:
1307 autoecc = 0;
1308 break;
1309 default:
1310 autoecc = -EINVAL;
1311 }
1312 return autoecc;
1313}
1314
1315/**
1316 * doc_fill_autooob - Fill a 16 bytes OOB from 8 non-ECC bytes
1317 * @dst: the target 16 bytes OOB buffer
1318 * @oobsrc: the source 8 bytes non-ECC OOB buffer
1319 *
1320 */
1321static void doc_fill_autooob(u8 *dst, u8 *oobsrc)
1322{
1323 memcpy(dst, oobsrc, DOC_LAYOUT_OOB_PAGEINFO_SZ);
1324 dst[DOC_LAYOUT_OOB_UNUSED_OFS] = oobsrc[DOC_LAYOUT_OOB_PAGEINFO_SZ];
1325}
1326
1327/**
1328 * doc_backup_oob - Backup OOB into docg3 structure
1329 * @docg3: the device
1330 * @to: the page offset in the chip
1331 * @ops: the OOB size and buffer
1332 *
1333 * As the docg3 should write a page with its OOB in one pass, and some userland
1334 * applications do write_oob() to setup the OOB and then write(), store the OOB
1335 * into a temporary storage. This is very dangerous, as 2 concurrent
1336 * applications could store an OOB, and then write their pages (which will
1337 * result into one having its OOB corrupted).
1338 *
1339 * The only reliable way would be for userland to call doc_write_oob() with both
1340 * the page data _and_ the OOB area.
1341 *
1342 * Returns 0 if success, -EINVAL if ops content invalid
1343 */
1344static int doc_backup_oob(struct docg3 *docg3, loff_t to,
1345 struct mtd_oob_ops *ops)
1346{
1347 int ooblen = ops->ooblen, autoecc;
1348
1349 if (ooblen != DOC_LAYOUT_OOB_SIZE)
1350 return -EINVAL;
1351 autoecc = doc_guess_autoecc(ops);
1352 if (autoecc < 0)
1353 return autoecc;
1354
1355 docg3->oob_write_ofs = to;
1356 docg3->oob_autoecc = autoecc;
1357 if (ops->mode == MTD_OPS_AUTO_OOB) {
1358 doc_fill_autooob(docg3->oob_write_buf, ops->oobbuf);
1359 ops->oobretlen = 8;
1360 } else {
1361 memcpy(docg3->oob_write_buf, ops->oobbuf, DOC_LAYOUT_OOB_SIZE);
1362 ops->oobretlen = DOC_LAYOUT_OOB_SIZE;
1363 }
1364 return 0;
1365}
1366
1367/**
1368 * doc_write_oob - Write out of band bytes to flash
1369 * @mtd: the device
1370 * @ofs: the offset from first block and first page, in bytes, aligned on page
1371 * size
1372 * @ops: the mtd oob structure
1373 *
1374 * Either write OOB data into a temporary buffer, for the subsequent write
1375 * page. The provided OOB should be 16 bytes long. If a data buffer is provided
1376 * as well, issue the page write.
1377 * Or provide data without OOB, and then a all zeroed OOB will be used (ECC will
1378 * still be filled in if asked for).
1379 *
1380 * Returns 0 is successful, EINVAL if length is not 14 bytes
1381 */
1382static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
1383 struct mtd_oob_ops *ops)
1384{
1385 struct docg3 *docg3 = mtd->priv;
1386 int ret, autoecc, oobdelta;
1387 u8 *oobbuf = ops->oobbuf;
1388 u8 *buf = ops->datbuf;
1389 size_t len, ooblen;
1390 u8 oob[DOC_LAYOUT_OOB_SIZE];
1391
1392 if (buf)
1393 len = ops->len;
1394 else
1395 len = 0;
1396 if (oobbuf)
1397 ooblen = ops->ooblen;
1398 else
1399 ooblen = 0;
1400
1401 if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
1402 oobbuf += ops->ooboffs;
1403
1404 doc_dbg("doc_write_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
1405 ofs, ops->mode, buf, len, oobbuf, ooblen);
1406 switch (ops->mode) {
1407 case MTD_OPS_PLACE_OOB:
1408 case MTD_OPS_RAW:
1409 oobdelta = mtd->oobsize;
1410 break;
1411 case MTD_OPS_AUTO_OOB:
1412 oobdelta = mtd->oobavail;
1413 break;
1414 default:
1415 return -EINVAL;
1416 }
1417 if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % oobdelta) ||
1418 (ofs % DOC_LAYOUT_PAGE_SIZE))
1419 return -EINVAL;
1420 if (len && ooblen &&
1421 (len / DOC_LAYOUT_PAGE_SIZE) != (ooblen / oobdelta))
1422 return -EINVAL;
1423
1424 ops->oobretlen = 0;
1425 ops->retlen = 0;
1426 ret = 0;
1427 if (len == 0 && ooblen == 0)
1428 return -EINVAL;
1429 if (len == 0 && ooblen > 0)
1430 return doc_backup_oob(docg3, ofs, ops);
1431
1432 autoecc = doc_guess_autoecc(ops);
1433 if (autoecc < 0)
1434 return autoecc;
1435
1436 mutex_lock(&docg3->cascade->lock);
1437 while (!ret && len > 0) {
1438 memset(oob, 0, sizeof(oob));
1439 if (ofs == docg3->oob_write_ofs)
1440 memcpy(oob, docg3->oob_write_buf, DOC_LAYOUT_OOB_SIZE);
1441 else if (ooblen > 0 && ops->mode == MTD_OPS_AUTO_OOB)
1442 doc_fill_autooob(oob, oobbuf);
1443 else if (ooblen > 0)
1444 memcpy(oob, oobbuf, DOC_LAYOUT_OOB_SIZE);
1445 ret = doc_write_page(docg3, ofs, buf, oob, autoecc);
1446
1447 ofs += DOC_LAYOUT_PAGE_SIZE;
1448 len -= DOC_LAYOUT_PAGE_SIZE;
1449 buf += DOC_LAYOUT_PAGE_SIZE;
1450 if (ooblen) {
1451 oobbuf += oobdelta;
1452 ooblen -= oobdelta;
1453 ops->oobretlen += oobdelta;
1454 }
1455 ops->retlen += DOC_LAYOUT_PAGE_SIZE;
1456 }
1457
1458 doc_set_device_id(docg3, 0);
1459 mutex_unlock(&docg3->cascade->lock);
1460 return ret;
1461}
1462
1463static struct docg3 *sysfs_dev2docg3(struct device *dev,
1464 struct device_attribute *attr)
1465{
1466 int floor;
1467 struct mtd_info **docg3_floors = dev_get_drvdata(dev);
1468
1469 floor = attr->attr.name[1] - '0';
1470 if (floor < 0 || floor >= DOC_MAX_NBFLOORS)
1471 return NULL;
1472 else
1473 return docg3_floors[floor]->priv;
1474}
1475
1476static ssize_t dps0_is_key_locked(struct device *dev,
1477 struct device_attribute *attr, char *buf)
1478{
1479 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1480 int dps0;
1481
1482 mutex_lock(&docg3->cascade->lock);
1483 doc_set_device_id(docg3, docg3->device_id);
1484 dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
1485 doc_set_device_id(docg3, 0);
1486 mutex_unlock(&docg3->cascade->lock);
1487
1488 return sprintf(buf, "%d\n", !(dps0 & DOC_DPS_KEY_OK));
1489}
1490
1491static ssize_t dps1_is_key_locked(struct device *dev,
1492 struct device_attribute *attr, char *buf)
1493{
1494 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1495 int dps1;
1496
1497 mutex_lock(&docg3->cascade->lock);
1498 doc_set_device_id(docg3, docg3->device_id);
1499 dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
1500 doc_set_device_id(docg3, 0);
1501 mutex_unlock(&docg3->cascade->lock);
1502
1503 return sprintf(buf, "%d\n", !(dps1 & DOC_DPS_KEY_OK));
1504}
1505
1506static ssize_t dps0_insert_key(struct device *dev,
1507 struct device_attribute *attr,
1508 const char *buf, size_t count)
1509{
1510 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1511 int i;
1512
1513 if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
1514 return -EINVAL;
1515
1516 mutex_lock(&docg3->cascade->lock);
1517 doc_set_device_id(docg3, docg3->device_id);
1518 for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
1519 doc_writeb(docg3, buf[i], DOC_DPS0_KEY);
1520 doc_set_device_id(docg3, 0);
1521 mutex_unlock(&docg3->cascade->lock);
1522 return count;
1523}
1524
1525static ssize_t dps1_insert_key(struct device *dev,
1526 struct device_attribute *attr,
1527 const char *buf, size_t count)
1528{
1529 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1530 int i;
1531
1532 if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
1533 return -EINVAL;
1534
1535 mutex_lock(&docg3->cascade->lock);
1536 doc_set_device_id(docg3, docg3->device_id);
1537 for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
1538 doc_writeb(docg3, buf[i], DOC_DPS1_KEY);
1539 doc_set_device_id(docg3, 0);
1540 mutex_unlock(&docg3->cascade->lock);
1541 return count;
1542}
1543
1544#define FLOOR_SYSFS(id) { \
1545 __ATTR(f##id##_dps0_is_keylocked, S_IRUGO, dps0_is_key_locked, NULL), \
1546 __ATTR(f##id##_dps1_is_keylocked, S_IRUGO, dps1_is_key_locked, NULL), \
1547 __ATTR(f##id##_dps0_protection_key, S_IWUSR|S_IWGRP, NULL, dps0_insert_key), \
1548 __ATTR(f##id##_dps1_protection_key, S_IWUSR|S_IWGRP, NULL, dps1_insert_key), \
1549}
1550
1551static struct device_attribute doc_sys_attrs[DOC_MAX_NBFLOORS][4] = {
1552 FLOOR_SYSFS(0), FLOOR_SYSFS(1), FLOOR_SYSFS(2), FLOOR_SYSFS(3)
1553};
1554
1555static int doc_register_sysfs(struct platform_device *pdev,
1556 struct docg3_cascade *cascade)
1557{
1558 struct device *dev = &pdev->dev;
1559 int floor;
1560 int ret;
1561 int i;
1562
1563 for (floor = 0;
1564 floor < DOC_MAX_NBFLOORS && cascade->floors[floor];
1565 floor++) {
1566 for (i = 0; i < 4; i++) {
1567 ret = device_create_file(dev, &doc_sys_attrs[floor][i]);
1568 if (ret)
1569 goto remove_files;
1570 }
1571 }
1572
1573 return 0;
1574
1575remove_files:
1576 do {
1577 while (--i >= 0)
1578 device_remove_file(dev, &doc_sys_attrs[floor][i]);
1579 i = 4;
1580 } while (--floor >= 0);
1581
1582 return ret;
1583}
1584
1585static void doc_unregister_sysfs(struct platform_device *pdev,
1586 struct docg3_cascade *cascade)
1587{
1588 struct device *dev = &pdev->dev;
1589 int floor, i;
1590
1591 for (floor = 0; floor < DOC_MAX_NBFLOORS && cascade->floors[floor];
1592 floor++)
1593 for (i = 0; i < 4; i++)
1594 device_remove_file(dev, &doc_sys_attrs[floor][i]);
1595}
1596
1597/*
1598 * Debug sysfs entries
1599 */
1600static int flashcontrol_show(struct seq_file *s, void *p)
1601{
1602 struct docg3 *docg3 = s->private;
1603
1604 u8 fctrl;
1605
1606 mutex_lock(&docg3->cascade->lock);
1607 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
1608 mutex_unlock(&docg3->cascade->lock);
1609
1610 seq_printf(s, "FlashControl : 0x%02x (%s,CE# %s,%s,%s,flash %s)\n",
1611 fctrl,
1612 fctrl & DOC_CTRL_VIOLATION ? "protocol violation" : "-",
1613 fctrl & DOC_CTRL_CE ? "active" : "inactive",
1614 fctrl & DOC_CTRL_PROTECTION_ERROR ? "protection error" : "-",
1615 fctrl & DOC_CTRL_SEQUENCE_ERROR ? "sequence error" : "-",
1616 fctrl & DOC_CTRL_FLASHREADY ? "ready" : "not ready");
1617
1618 return 0;
1619}
1620DEFINE_SHOW_ATTRIBUTE(flashcontrol);
1621
1622static int asic_mode_show(struct seq_file *s, void *p)
1623{
1624 struct docg3 *docg3 = s->private;
1625
1626 int pctrl, mode;
1627
1628 mutex_lock(&docg3->cascade->lock);
1629 pctrl = doc_register_readb(docg3, DOC_ASICMODE);
1630 mode = pctrl & 0x03;
1631 mutex_unlock(&docg3->cascade->lock);
1632
1633 seq_printf(s,
1634 "%04x : RAM_WE=%d,RSTIN_RESET=%d,BDETCT_RESET=%d,WRITE_ENABLE=%d,POWERDOWN=%d,MODE=%d%d (",
1635 pctrl,
1636 pctrl & DOC_ASICMODE_RAM_WE ? 1 : 0,
1637 pctrl & DOC_ASICMODE_RSTIN_RESET ? 1 : 0,
1638 pctrl & DOC_ASICMODE_BDETCT_RESET ? 1 : 0,
1639 pctrl & DOC_ASICMODE_MDWREN ? 1 : 0,
1640 pctrl & DOC_ASICMODE_POWERDOWN ? 1 : 0,
1641 mode >> 1, mode & 0x1);
1642
1643 switch (mode) {
1644 case DOC_ASICMODE_RESET:
1645 seq_puts(s, "reset");
1646 break;
1647 case DOC_ASICMODE_NORMAL:
1648 seq_puts(s, "normal");
1649 break;
1650 case DOC_ASICMODE_POWERDOWN:
1651 seq_puts(s, "powerdown");
1652 break;
1653 }
1654 seq_puts(s, ")\n");
1655 return 0;
1656}
1657DEFINE_SHOW_ATTRIBUTE(asic_mode);
1658
1659static int device_id_show(struct seq_file *s, void *p)
1660{
1661 struct docg3 *docg3 = s->private;
1662 int id;
1663
1664 mutex_lock(&docg3->cascade->lock);
1665 id = doc_register_readb(docg3, DOC_DEVICESELECT);
1666 mutex_unlock(&docg3->cascade->lock);
1667
1668 seq_printf(s, "DeviceId = %d\n", id);
1669 return 0;
1670}
1671DEFINE_SHOW_ATTRIBUTE(device_id);
1672
1673static int protection_show(struct seq_file *s, void *p)
1674{
1675 struct docg3 *docg3 = s->private;
1676 int protect, dps0, dps0_low, dps0_high, dps1, dps1_low, dps1_high;
1677
1678 mutex_lock(&docg3->cascade->lock);
1679 protect = doc_register_readb(docg3, DOC_PROTECTION);
1680 dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
1681 dps0_low = doc_register_readw(docg3, DOC_DPS0_ADDRLOW);
1682 dps0_high = doc_register_readw(docg3, DOC_DPS0_ADDRHIGH);
1683 dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
1684 dps1_low = doc_register_readw(docg3, DOC_DPS1_ADDRLOW);
1685 dps1_high = doc_register_readw(docg3, DOC_DPS1_ADDRHIGH);
1686 mutex_unlock(&docg3->cascade->lock);
1687
1688 seq_printf(s, "Protection = 0x%02x (", protect);
1689 if (protect & DOC_PROTECT_FOUNDRY_OTP_LOCK)
1690 seq_puts(s, "FOUNDRY_OTP_LOCK,");
1691 if (protect & DOC_PROTECT_CUSTOMER_OTP_LOCK)
1692 seq_puts(s, "CUSTOMER_OTP_LOCK,");
1693 if (protect & DOC_PROTECT_LOCK_INPUT)
1694 seq_puts(s, "LOCK_INPUT,");
1695 if (protect & DOC_PROTECT_STICKY_LOCK)
1696 seq_puts(s, "STICKY_LOCK,");
1697 if (protect & DOC_PROTECT_PROTECTION_ENABLED)
1698 seq_puts(s, "PROTECTION ON,");
1699 if (protect & DOC_PROTECT_IPL_DOWNLOAD_LOCK)
1700 seq_puts(s, "IPL_DOWNLOAD_LOCK,");
1701 if (protect & DOC_PROTECT_PROTECTION_ERROR)
1702 seq_puts(s, "PROTECT_ERR,");
1703 else
1704 seq_puts(s, "NO_PROTECT_ERR");
1705 seq_puts(s, ")\n");
1706
1707 seq_printf(s, "DPS0 = 0x%02x : Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1708 dps0, dps0_low, dps0_high,
1709 !!(dps0 & DOC_DPS_OTP_PROTECTED),
1710 !!(dps0 & DOC_DPS_READ_PROTECTED),
1711 !!(dps0 & DOC_DPS_WRITE_PROTECTED),
1712 !!(dps0 & DOC_DPS_HW_LOCK_ENABLED),
1713 !!(dps0 & DOC_DPS_KEY_OK));
1714 seq_printf(s, "DPS1 = 0x%02x : Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1715 dps1, dps1_low, dps1_high,
1716 !!(dps1 & DOC_DPS_OTP_PROTECTED),
1717 !!(dps1 & DOC_DPS_READ_PROTECTED),
1718 !!(dps1 & DOC_DPS_WRITE_PROTECTED),
1719 !!(dps1 & DOC_DPS_HW_LOCK_ENABLED),
1720 !!(dps1 & DOC_DPS_KEY_OK));
1721 return 0;
1722}
1723DEFINE_SHOW_ATTRIBUTE(protection);
1724
1725static void __init doc_dbg_register(struct mtd_info *floor)
1726{
1727 struct dentry *root = floor->dbg.dfs_dir;
1728 struct docg3 *docg3 = floor->priv;
1729
1730 if (IS_ERR_OR_NULL(root)) {
1731 if (IS_ENABLED(CONFIG_DEBUG_FS) &&
1732 !IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER))
1733 dev_warn(floor->dev.parent,
1734 "CONFIG_MTD_PARTITIONED_MASTER must be enabled to expose debugfs stuff\n");
1735 return;
1736 }
1737
1738 debugfs_create_file("docg3_flashcontrol", S_IRUSR, root, docg3,
1739 &flashcontrol_fops);
1740 debugfs_create_file("docg3_asic_mode", S_IRUSR, root, docg3,
1741 &asic_mode_fops);
1742 debugfs_create_file("docg3_device_id", S_IRUSR, root, docg3,
1743 &device_id_fops);
1744 debugfs_create_file("docg3_protection", S_IRUSR, root, docg3,
1745 &protection_fops);
1746}
1747
1748/**
1749 * doc_set_driver_info - Fill the mtd_info structure and docg3 structure
1750 * @chip_id: The chip ID of the supported chip
1751 * @mtd: The structure to fill
1752 */
1753static int __init doc_set_driver_info(int chip_id, struct mtd_info *mtd)
1754{
1755 struct docg3 *docg3 = mtd->priv;
1756 int cfg;
1757
1758 cfg = doc_register_readb(docg3, DOC_CONFIGURATION);
1759 docg3->if_cfg = (cfg & DOC_CONF_IF_CFG ? 1 : 0);
1760 docg3->reliable = reliable_mode;
1761
1762 switch (chip_id) {
1763 case DOC_CHIPID_G3:
1764 mtd->name = devm_kasprintf(docg3->dev, GFP_KERNEL, "docg3.%d",
1765 docg3->device_id);
1766 if (!mtd->name)
1767 return -ENOMEM;
1768 docg3->max_block = 2047;
1769 break;
1770 }
1771 mtd->type = MTD_NANDFLASH;
1772 mtd->flags = MTD_CAP_NANDFLASH;
1773 mtd->size = (docg3->max_block + 1) * DOC_LAYOUT_BLOCK_SIZE;
1774 if (docg3->reliable == 2)
1775 mtd->size /= 2;
1776 mtd->erasesize = DOC_LAYOUT_BLOCK_SIZE * DOC_LAYOUT_NBPLANES;
1777 if (docg3->reliable == 2)
1778 mtd->erasesize /= 2;
1779 mtd->writebufsize = mtd->writesize = DOC_LAYOUT_PAGE_SIZE;
1780 mtd->oobsize = DOC_LAYOUT_OOB_SIZE;
1781 mtd->_erase = doc_erase;
1782 mtd->_read_oob = doc_read_oob;
1783 mtd->_write_oob = doc_write_oob;
1784 mtd->_block_isbad = doc_block_isbad;
1785 mtd_set_ooblayout(mtd, &nand_ooblayout_docg3_ops);
1786 mtd->oobavail = 8;
1787 mtd->ecc_strength = DOC_ECC_BCH_T;
1788
1789 return 0;
1790}
1791
1792/**
1793 * doc_probe_device - Check if a device is available
1794 * @cascade: the cascade of chips this devices will belong to
1795 * @floor: the floor of the probed device
1796 * @dev: the device
1797 *
1798 * Checks whether a device at the specified IO range, and floor is available.
1799 *
1800 * Returns a mtd_info struct if there is a device, ENODEV if none found, ENOMEM
1801 * if a memory allocation failed. If floor 0 is checked, a reset of the ASIC is
1802 * launched.
1803 */
1804static struct mtd_info * __init
1805doc_probe_device(struct docg3_cascade *cascade, int floor, struct device *dev)
1806{
1807 int ret, bbt_nbpages;
1808 u16 chip_id, chip_id_inv;
1809 struct docg3 *docg3;
1810 struct mtd_info *mtd;
1811
1812 ret = -ENOMEM;
1813 docg3 = kzalloc(sizeof(struct docg3), GFP_KERNEL);
1814 if (!docg3)
1815 goto nomem1;
1816 mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
1817 if (!mtd)
1818 goto nomem2;
1819 mtd->priv = docg3;
1820 mtd->dev.parent = dev;
1821 bbt_nbpages = DIV_ROUND_UP(docg3->max_block + 1,
1822 8 * DOC_LAYOUT_PAGE_SIZE);
1823 docg3->bbt = kcalloc(DOC_LAYOUT_PAGE_SIZE, bbt_nbpages, GFP_KERNEL);
1824 if (!docg3->bbt)
1825 goto nomem3;
1826
1827 docg3->dev = dev;
1828 docg3->device_id = floor;
1829 docg3->cascade = cascade;
1830 doc_set_device_id(docg3, docg3->device_id);
1831 if (!floor)
1832 doc_set_asic_mode(docg3, DOC_ASICMODE_RESET);
1833 doc_set_asic_mode(docg3, DOC_ASICMODE_NORMAL);
1834
1835 chip_id = doc_register_readw(docg3, DOC_CHIPID);
1836 chip_id_inv = doc_register_readw(docg3, DOC_CHIPID_INV);
1837
1838 ret = 0;
1839 if (chip_id != (u16)(~chip_id_inv)) {
1840 goto nomem4;
1841 }
1842
1843 switch (chip_id) {
1844 case DOC_CHIPID_G3:
1845 doc_info("Found a G3 DiskOnChip at addr %p, floor %d\n",
1846 docg3->cascade->base, floor);
1847 break;
1848 default:
1849 doc_err("Chip id %04x is not a DiskOnChip G3 chip\n", chip_id);
1850 goto nomem4;
1851 }
1852
1853 ret = doc_set_driver_info(chip_id, mtd);
1854 if (ret)
1855 goto nomem4;
1856
1857 doc_hamming_ecc_init(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ);
1858 doc_reload_bbt(docg3);
1859 return mtd;
1860
1861nomem4:
1862 kfree(docg3->bbt);
1863nomem3:
1864 kfree(mtd);
1865nomem2:
1866 kfree(docg3);
1867nomem1:
1868 return ret ? ERR_PTR(ret) : NULL;
1869}
1870
1871/**
1872 * doc_release_device - Release a docg3 floor
1873 * @mtd: the device
1874 */
1875static void doc_release_device(struct mtd_info *mtd)
1876{
1877 struct docg3 *docg3 = mtd->priv;
1878
1879 mtd_device_unregister(mtd);
1880 kfree(docg3->bbt);
1881 kfree(docg3);
1882 kfree(mtd);
1883}
1884
1885/**
1886 * docg3_resume - Awakens docg3 floor
1887 * @pdev: platfrom device
1888 *
1889 * Returns 0 (always successful)
1890 */
1891static int docg3_resume(struct platform_device *pdev)
1892{
1893 int i;
1894 struct docg3_cascade *cascade;
1895 struct mtd_info **docg3_floors, *mtd;
1896 struct docg3 *docg3;
1897
1898 cascade = platform_get_drvdata(pdev);
1899 docg3_floors = cascade->floors;
1900 mtd = docg3_floors[0];
1901 docg3 = mtd->priv;
1902
1903 doc_dbg("docg3_resume()\n");
1904 for (i = 0; i < 12; i++)
1905 doc_readb(docg3, DOC_IOSPACE_IPL);
1906 return 0;
1907}
1908
1909/**
1910 * docg3_suspend - Put in low power mode the docg3 floor
1911 * @pdev: platform device
1912 * @state: power state
1913 *
1914 * Shuts off most of docg3 circuitery to lower power consumption.
1915 *
1916 * Returns 0 if suspend succeeded, -EIO if chip refused suspend
1917 */
1918static int docg3_suspend(struct platform_device *pdev, pm_message_t state)
1919{
1920 int floor, i;
1921 struct docg3_cascade *cascade;
1922 struct mtd_info **docg3_floors, *mtd;
1923 struct docg3 *docg3;
1924 u8 ctrl, pwr_down;
1925
1926 cascade = platform_get_drvdata(pdev);
1927 docg3_floors = cascade->floors;
1928 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
1929 mtd = docg3_floors[floor];
1930 if (!mtd)
1931 continue;
1932 docg3 = mtd->priv;
1933
1934 doc_writeb(docg3, floor, DOC_DEVICESELECT);
1935 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
1936 ctrl &= ~DOC_CTRL_VIOLATION & ~DOC_CTRL_CE;
1937 doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
1938
1939 for (i = 0; i < 10; i++) {
1940 usleep_range(3000, 4000);
1941 pwr_down = doc_register_readb(docg3, DOC_POWERMODE);
1942 if (pwr_down & DOC_POWERDOWN_READY)
1943 break;
1944 }
1945 if (pwr_down & DOC_POWERDOWN_READY) {
1946 doc_dbg("docg3_suspend(): floor %d powerdown ok\n",
1947 floor);
1948 } else {
1949 doc_err("docg3_suspend(): floor %d powerdown failed\n",
1950 floor);
1951 return -EIO;
1952 }
1953 }
1954
1955 mtd = docg3_floors[0];
1956 docg3 = mtd->priv;
1957 doc_set_asic_mode(docg3, DOC_ASICMODE_POWERDOWN);
1958 return 0;
1959}
1960
1961/**
1962 * docg3_probe - Probe the IO space for a DiskOnChip G3 chip
1963 * @pdev: platform device
1964 *
1965 * Probes for a G3 chip at the specified IO space in the platform data
1966 * ressources. The floor 0 must be available.
1967 *
1968 * Returns 0 on success, -ENOMEM, -ENXIO on error
1969 */
1970static int __init docg3_probe(struct platform_device *pdev)
1971{
1972 struct device *dev = &pdev->dev;
1973 struct mtd_info *mtd;
1974 struct resource *ress;
1975 void __iomem *base;
1976 int ret, floor;
1977 struct docg3_cascade *cascade;
1978
1979 ret = -ENXIO;
1980 ress = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1981 if (!ress) {
1982 dev_err(dev, "No I/O memory resource defined\n");
1983 return ret;
1984 }
1985
1986 ret = -ENOMEM;
1987 base = devm_ioremap(dev, ress->start, DOC_IOSPACE_SIZE);
1988 if (!base) {
1989 dev_err(dev, "devm_ioremap dev failed\n");
1990 return ret;
1991 }
1992
1993 cascade = devm_kcalloc(dev, DOC_MAX_NBFLOORS, sizeof(*cascade),
1994 GFP_KERNEL);
1995 if (!cascade)
1996 return ret;
1997 cascade->base = base;
1998 mutex_init(&cascade->lock);
1999 cascade->bch = bch_init(DOC_ECC_BCH_M, DOC_ECC_BCH_T,
2000 DOC_ECC_BCH_PRIMPOLY, false);
2001 if (!cascade->bch)
2002 return ret;
2003
2004 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
2005 mtd = doc_probe_device(cascade, floor, dev);
2006 if (IS_ERR(mtd)) {
2007 ret = PTR_ERR(mtd);
2008 goto err_probe;
2009 }
2010 if (!mtd) {
2011 if (floor == 0)
2012 goto notfound;
2013 else
2014 continue;
2015 }
2016 cascade->floors[floor] = mtd;
2017 ret = mtd_device_parse_register(mtd, part_probes, NULL, NULL,
2018 0);
2019 if (ret)
2020 goto err_probe;
2021
2022 doc_dbg_register(cascade->floors[floor]);
2023 }
2024
2025 ret = doc_register_sysfs(pdev, cascade);
2026 if (ret)
2027 goto err_probe;
2028
2029 platform_set_drvdata(pdev, cascade);
2030 return 0;
2031
2032notfound:
2033 ret = -ENODEV;
2034 dev_info(dev, "No supported DiskOnChip found\n");
2035err_probe:
2036 bch_free(cascade->bch);
2037 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
2038 if (cascade->floors[floor])
2039 doc_release_device(cascade->floors[floor]);
2040 return ret;
2041}
2042
2043/**
2044 * docg3_release - Release the driver
2045 * @pdev: the platform device
2046 *
2047 * Returns 0
2048 */
2049static void docg3_release(struct platform_device *pdev)
2050{
2051 struct docg3_cascade *cascade = platform_get_drvdata(pdev);
2052 struct docg3 *docg3 = cascade->floors[0]->priv;
2053 int floor;
2054
2055 doc_unregister_sysfs(pdev, cascade);
2056 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
2057 if (cascade->floors[floor])
2058 doc_release_device(cascade->floors[floor]);
2059
2060 bch_free(docg3->cascade->bch);
2061}
2062
2063#ifdef CONFIG_OF
2064static const struct of_device_id docg3_dt_ids[] = {
2065 { .compatible = "m-systems,diskonchip-g3" },
2066 {}
2067};
2068MODULE_DEVICE_TABLE(of, docg3_dt_ids);
2069#endif
2070
2071static struct platform_driver g3_driver = {
2072 .driver = {
2073 .name = "docg3",
2074 .of_match_table = of_match_ptr(docg3_dt_ids),
2075 },
2076 .suspend = docg3_suspend,
2077 .resume = docg3_resume,
2078 .remove_new = docg3_release,
2079};
2080
2081module_platform_driver_probe(g3_driver, docg3_probe);
2082
2083MODULE_LICENSE("GPL");
2084MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
2085MODULE_DESCRIPTION("MTD driver for DiskOnChip G3");
1/*
2 * Handles the M-Systems DiskOnChip G3 chip
3 *
4 * Copyright (C) 2011 Robert Jarzmik
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/errno.h>
25#include <linux/platform_device.h>
26#include <linux/string.h>
27#include <linux/slab.h>
28#include <linux/io.h>
29#include <linux/delay.h>
30#include <linux/mtd/mtd.h>
31#include <linux/mtd/partitions.h>
32#include <linux/bitmap.h>
33#include <linux/bitrev.h>
34#include <linux/bch.h>
35
36#include <linux/debugfs.h>
37#include <linux/seq_file.h>
38
39#define CREATE_TRACE_POINTS
40#include "docg3.h"
41
42/*
43 * This driver handles the DiskOnChip G3 flash memory.
44 *
45 * As no specification is available from M-Systems/Sandisk, this drivers lacks
46 * several functions available on the chip, as :
47 * - IPL write
48 *
49 * The bus data width (8bits versus 16bits) is not handled (if_cfg flag), and
50 * the driver assumes a 16bits data bus.
51 *
52 * DocG3 relies on 2 ECC algorithms, which are handled in hardware :
53 * - a 1 byte Hamming code stored in the OOB for each page
54 * - a 7 bytes BCH code stored in the OOB for each page
55 * The BCH ECC is :
56 * - BCH is in GF(2^14)
57 * - BCH is over data of 520 bytes (512 page + 7 page_info bytes
58 * + 1 hamming byte)
59 * - BCH can correct up to 4 bits (t = 4)
60 * - BCH syndroms are calculated in hardware, and checked in hardware as well
61 *
62 */
63
64static unsigned int reliable_mode;
65module_param(reliable_mode, uint, 0);
66MODULE_PARM_DESC(reliable_mode, "Set the docg3 mode (0=normal MLC, 1=fast, "
67 "2=reliable) : MLC normal operations are in normal mode");
68
69/**
70 * struct docg3_oobinfo - DiskOnChip G3 OOB layout
71 * @eccbytes: 8 bytes are used (1 for Hamming ECC, 7 for BCH ECC)
72 * @eccpos: ecc positions (byte 7 is Hamming ECC, byte 8-14 are BCH ECC)
73 * @oobfree: free pageinfo bytes (byte 0 until byte 6, byte 15
74 * @oobavail: 8 available bytes remaining after ECC toll
75 */
76static struct nand_ecclayout docg3_oobinfo = {
77 .eccbytes = 8,
78 .eccpos = {7, 8, 9, 10, 11, 12, 13, 14},
79 .oobfree = {{0, 7}, {15, 1} },
80 .oobavail = 8,
81};
82
83static inline u8 doc_readb(struct docg3 *docg3, u16 reg)
84{
85 u8 val = readb(docg3->cascade->base + reg);
86
87 trace_docg3_io(0, 8, reg, (int)val);
88 return val;
89}
90
91static inline u16 doc_readw(struct docg3 *docg3, u16 reg)
92{
93 u16 val = readw(docg3->cascade->base + reg);
94
95 trace_docg3_io(0, 16, reg, (int)val);
96 return val;
97}
98
99static inline void doc_writeb(struct docg3 *docg3, u8 val, u16 reg)
100{
101 writeb(val, docg3->cascade->base + reg);
102 trace_docg3_io(1, 8, reg, val);
103}
104
105static inline void doc_writew(struct docg3 *docg3, u16 val, u16 reg)
106{
107 writew(val, docg3->cascade->base + reg);
108 trace_docg3_io(1, 16, reg, val);
109}
110
111static inline void doc_flash_command(struct docg3 *docg3, u8 cmd)
112{
113 doc_writeb(docg3, cmd, DOC_FLASHCOMMAND);
114}
115
116static inline void doc_flash_sequence(struct docg3 *docg3, u8 seq)
117{
118 doc_writeb(docg3, seq, DOC_FLASHSEQUENCE);
119}
120
121static inline void doc_flash_address(struct docg3 *docg3, u8 addr)
122{
123 doc_writeb(docg3, addr, DOC_FLASHADDRESS);
124}
125
126static char const * const part_probes[] = { "cmdlinepart", "saftlpart", NULL };
127
128static int doc_register_readb(struct docg3 *docg3, int reg)
129{
130 u8 val;
131
132 doc_writew(docg3, reg, DOC_READADDRESS);
133 val = doc_readb(docg3, reg);
134 doc_vdbg("Read register %04x : %02x\n", reg, val);
135 return val;
136}
137
138static int doc_register_readw(struct docg3 *docg3, int reg)
139{
140 u16 val;
141
142 doc_writew(docg3, reg, DOC_READADDRESS);
143 val = doc_readw(docg3, reg);
144 doc_vdbg("Read register %04x : %04x\n", reg, val);
145 return val;
146}
147
148/**
149 * doc_delay - delay docg3 operations
150 * @docg3: the device
151 * @nbNOPs: the number of NOPs to issue
152 *
153 * As no specification is available, the right timings between chip commands are
154 * unknown. The only available piece of information are the observed nops on a
155 * working docg3 chip.
156 * Therefore, doc_delay relies on a busy loop of NOPs, instead of scheduler
157 * friendlier msleep() functions or blocking mdelay().
158 */
159static void doc_delay(struct docg3 *docg3, int nbNOPs)
160{
161 int i;
162
163 doc_vdbg("NOP x %d\n", nbNOPs);
164 for (i = 0; i < nbNOPs; i++)
165 doc_writeb(docg3, 0, DOC_NOP);
166}
167
168static int is_prot_seq_error(struct docg3 *docg3)
169{
170 int ctrl;
171
172 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
173 return ctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR);
174}
175
176static int doc_is_ready(struct docg3 *docg3)
177{
178 int ctrl;
179
180 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
181 return ctrl & DOC_CTRL_FLASHREADY;
182}
183
184static int doc_wait_ready(struct docg3 *docg3)
185{
186 int maxWaitCycles = 100;
187
188 do {
189 doc_delay(docg3, 4);
190 cpu_relax();
191 } while (!doc_is_ready(docg3) && maxWaitCycles--);
192 doc_delay(docg3, 2);
193 if (maxWaitCycles > 0)
194 return 0;
195 else
196 return -EIO;
197}
198
199static int doc_reset_seq(struct docg3 *docg3)
200{
201 int ret;
202
203 doc_writeb(docg3, 0x10, DOC_FLASHCONTROL);
204 doc_flash_sequence(docg3, DOC_SEQ_RESET);
205 doc_flash_command(docg3, DOC_CMD_RESET);
206 doc_delay(docg3, 2);
207 ret = doc_wait_ready(docg3);
208
209 doc_dbg("doc_reset_seq() -> isReady=%s\n", ret ? "false" : "true");
210 return ret;
211}
212
213/**
214 * doc_read_data_area - Read data from data area
215 * @docg3: the device
216 * @buf: the buffer to fill in (might be NULL is dummy reads)
217 * @len: the length to read
218 * @first: first time read, DOC_READADDRESS should be set
219 *
220 * Reads bytes from flash data. Handles the single byte / even bytes reads.
221 */
222static void doc_read_data_area(struct docg3 *docg3, void *buf, int len,
223 int first)
224{
225 int i, cdr, len4;
226 u16 data16, *dst16;
227 u8 data8, *dst8;
228
229 doc_dbg("doc_read_data_area(buf=%p, len=%d)\n", buf, len);
230 cdr = len & 0x1;
231 len4 = len - cdr;
232
233 if (first)
234 doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
235 dst16 = buf;
236 for (i = 0; i < len4; i += 2) {
237 data16 = doc_readw(docg3, DOC_IOSPACE_DATA);
238 if (dst16) {
239 *dst16 = data16;
240 dst16++;
241 }
242 }
243
244 if (cdr) {
245 doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
246 DOC_READADDRESS);
247 doc_delay(docg3, 1);
248 dst8 = (u8 *)dst16;
249 for (i = 0; i < cdr; i++) {
250 data8 = doc_readb(docg3, DOC_IOSPACE_DATA);
251 if (dst8) {
252 *dst8 = data8;
253 dst8++;
254 }
255 }
256 }
257}
258
259/**
260 * doc_write_data_area - Write data into data area
261 * @docg3: the device
262 * @buf: the buffer to get input bytes from
263 * @len: the length to write
264 *
265 * Writes bytes into flash data. Handles the single byte / even bytes writes.
266 */
267static void doc_write_data_area(struct docg3 *docg3, const void *buf, int len)
268{
269 int i, cdr, len4;
270 u16 *src16;
271 u8 *src8;
272
273 doc_dbg("doc_write_data_area(buf=%p, len=%d)\n", buf, len);
274 cdr = len & 0x3;
275 len4 = len - cdr;
276
277 doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
278 src16 = (u16 *)buf;
279 for (i = 0; i < len4; i += 2) {
280 doc_writew(docg3, *src16, DOC_IOSPACE_DATA);
281 src16++;
282 }
283
284 src8 = (u8 *)src16;
285 for (i = 0; i < cdr; i++) {
286 doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
287 DOC_READADDRESS);
288 doc_writeb(docg3, *src8, DOC_IOSPACE_DATA);
289 src8++;
290 }
291}
292
293/**
294 * doc_set_data_mode - Sets the flash to normal or reliable data mode
295 * @docg3: the device
296 *
297 * The reliable data mode is a bit slower than the fast mode, but less errors
298 * occur. Entering the reliable mode cannot be done without entering the fast
299 * mode first.
300 *
301 * In reliable mode, pages 2*n and 2*n+1 are clones. Writing to page 0 of blocks
302 * (4,5) make the hardware write also to page 1 of blocks blocks(4,5). Reading
303 * from page 0 of blocks (4,5) or from page 1 of blocks (4,5) gives the same
304 * result, which is a logical and between bytes from page 0 and page 1 (which is
305 * consistent with the fact that writing to a page is _clearing_ bits of that
306 * page).
307 */
308static void doc_set_reliable_mode(struct docg3 *docg3)
309{
310 static char *strmode[] = { "normal", "fast", "reliable", "invalid" };
311
312 doc_dbg("doc_set_reliable_mode(%s)\n", strmode[docg3->reliable]);
313 switch (docg3->reliable) {
314 case 0:
315 break;
316 case 1:
317 doc_flash_sequence(docg3, DOC_SEQ_SET_FASTMODE);
318 doc_flash_command(docg3, DOC_CMD_FAST_MODE);
319 break;
320 case 2:
321 doc_flash_sequence(docg3, DOC_SEQ_SET_RELIABLEMODE);
322 doc_flash_command(docg3, DOC_CMD_FAST_MODE);
323 doc_flash_command(docg3, DOC_CMD_RELIABLE_MODE);
324 break;
325 default:
326 doc_err("doc_set_reliable_mode(): invalid mode\n");
327 break;
328 }
329 doc_delay(docg3, 2);
330}
331
332/**
333 * doc_set_asic_mode - Set the ASIC mode
334 * @docg3: the device
335 * @mode: the mode
336 *
337 * The ASIC can work in 3 modes :
338 * - RESET: all registers are zeroed
339 * - NORMAL: receives and handles commands
340 * - POWERDOWN: minimal poweruse, flash parts shut off
341 */
342static void doc_set_asic_mode(struct docg3 *docg3, u8 mode)
343{
344 int i;
345
346 for (i = 0; i < 12; i++)
347 doc_readb(docg3, DOC_IOSPACE_IPL);
348
349 mode |= DOC_ASICMODE_MDWREN;
350 doc_dbg("doc_set_asic_mode(%02x)\n", mode);
351 doc_writeb(docg3, mode, DOC_ASICMODE);
352 doc_writeb(docg3, ~mode, DOC_ASICMODECONFIRM);
353 doc_delay(docg3, 1);
354}
355
356/**
357 * doc_set_device_id - Sets the devices id for cascaded G3 chips
358 * @docg3: the device
359 * @id: the chip to select (amongst 0, 1, 2, 3)
360 *
361 * There can be 4 cascaded G3 chips. This function selects the one which will
362 * should be the active one.
363 */
364static void doc_set_device_id(struct docg3 *docg3, int id)
365{
366 u8 ctrl;
367
368 doc_dbg("doc_set_device_id(%d)\n", id);
369 doc_writeb(docg3, id, DOC_DEVICESELECT);
370 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
371
372 ctrl &= ~DOC_CTRL_VIOLATION;
373 ctrl |= DOC_CTRL_CE;
374 doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
375}
376
377/**
378 * doc_set_extra_page_mode - Change flash page layout
379 * @docg3: the device
380 *
381 * Normally, the flash page is split into the data (512 bytes) and the out of
382 * band data (16 bytes). For each, 4 more bytes can be accessed, where the wear
383 * leveling counters are stored. To access this last area of 4 bytes, a special
384 * mode must be input to the flash ASIC.
385 *
386 * Returns 0 if no error occurred, -EIO else.
387 */
388static int doc_set_extra_page_mode(struct docg3 *docg3)
389{
390 int fctrl;
391
392 doc_dbg("doc_set_extra_page_mode()\n");
393 doc_flash_sequence(docg3, DOC_SEQ_PAGE_SIZE_532);
394 doc_flash_command(docg3, DOC_CMD_PAGE_SIZE_532);
395 doc_delay(docg3, 2);
396
397 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
398 if (fctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR))
399 return -EIO;
400 else
401 return 0;
402}
403
404/**
405 * doc_setup_addr_sector - Setup blocks/page/ofs address for one plane
406 * @docg3: the device
407 * @sector: the sector
408 */
409static void doc_setup_addr_sector(struct docg3 *docg3, int sector)
410{
411 doc_delay(docg3, 1);
412 doc_flash_address(docg3, sector & 0xff);
413 doc_flash_address(docg3, (sector >> 8) & 0xff);
414 doc_flash_address(docg3, (sector >> 16) & 0xff);
415 doc_delay(docg3, 1);
416}
417
418/**
419 * doc_setup_writeaddr_sector - Setup blocks/page/ofs address for one plane
420 * @docg3: the device
421 * @sector: the sector
422 * @ofs: the offset in the page, between 0 and (512 + 16 + 512)
423 */
424static void doc_setup_writeaddr_sector(struct docg3 *docg3, int sector, int ofs)
425{
426 ofs = ofs >> 2;
427 doc_delay(docg3, 1);
428 doc_flash_address(docg3, ofs & 0xff);
429 doc_flash_address(docg3, sector & 0xff);
430 doc_flash_address(docg3, (sector >> 8) & 0xff);
431 doc_flash_address(docg3, (sector >> 16) & 0xff);
432 doc_delay(docg3, 1);
433}
434
435/**
436 * doc_seek - Set both flash planes to the specified block, page for reading
437 * @docg3: the device
438 * @block0: the first plane block index
439 * @block1: the second plane block index
440 * @page: the page index within the block
441 * @wear: if true, read will occur on the 4 extra bytes of the wear area
442 * @ofs: offset in page to read
443 *
444 * Programs the flash even and odd planes to the specific block and page.
445 * Alternatively, programs the flash to the wear area of the specified page.
446 */
447static int doc_read_seek(struct docg3 *docg3, int block0, int block1, int page,
448 int wear, int ofs)
449{
450 int sector, ret = 0;
451
452 doc_dbg("doc_seek(blocks=(%d,%d), page=%d, ofs=%d, wear=%d)\n",
453 block0, block1, page, ofs, wear);
454
455 if (!wear && (ofs < 2 * DOC_LAYOUT_PAGE_SIZE)) {
456 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
457 doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
458 doc_delay(docg3, 2);
459 } else {
460 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
461 doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
462 doc_delay(docg3, 2);
463 }
464
465 doc_set_reliable_mode(docg3);
466 if (wear)
467 ret = doc_set_extra_page_mode(docg3);
468 if (ret)
469 goto out;
470
471 doc_flash_sequence(docg3, DOC_SEQ_READ);
472 sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
473 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
474 doc_setup_addr_sector(docg3, sector);
475
476 sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
477 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
478 doc_setup_addr_sector(docg3, sector);
479 doc_delay(docg3, 1);
480
481out:
482 return ret;
483}
484
485/**
486 * doc_write_seek - Set both flash planes to the specified block, page for writing
487 * @docg3: the device
488 * @block0: the first plane block index
489 * @block1: the second plane block index
490 * @page: the page index within the block
491 * @ofs: offset in page to write
492 *
493 * Programs the flash even and odd planes to the specific block and page.
494 * Alternatively, programs the flash to the wear area of the specified page.
495 */
496static int doc_write_seek(struct docg3 *docg3, int block0, int block1, int page,
497 int ofs)
498{
499 int ret = 0, sector;
500
501 doc_dbg("doc_write_seek(blocks=(%d,%d), page=%d, ofs=%d)\n",
502 block0, block1, page, ofs);
503
504 doc_set_reliable_mode(docg3);
505
506 if (ofs < 2 * DOC_LAYOUT_PAGE_SIZE) {
507 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
508 doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
509 doc_delay(docg3, 2);
510 } else {
511 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
512 doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
513 doc_delay(docg3, 2);
514 }
515
516 doc_flash_sequence(docg3, DOC_SEQ_PAGE_SETUP);
517 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1);
518
519 sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
520 doc_setup_writeaddr_sector(docg3, sector, ofs);
521
522 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE3);
523 doc_delay(docg3, 2);
524 ret = doc_wait_ready(docg3);
525 if (ret)
526 goto out;
527
528 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1);
529 sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
530 doc_setup_writeaddr_sector(docg3, sector, ofs);
531 doc_delay(docg3, 1);
532
533out:
534 return ret;
535}
536
537
538/**
539 * doc_read_page_ecc_init - Initialize hardware ECC engine
540 * @docg3: the device
541 * @len: the number of bytes covered by the ECC (BCH covered)
542 *
543 * The function does initialize the hardware ECC engine to compute the Hamming
544 * ECC (on 1 byte) and the BCH hardware ECC (on 7 bytes).
545 *
546 * Return 0 if succeeded, -EIO on error
547 */
548static int doc_read_page_ecc_init(struct docg3 *docg3, int len)
549{
550 doc_writew(docg3, DOC_ECCCONF0_READ_MODE
551 | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
552 | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
553 DOC_ECCCONF0);
554 doc_delay(docg3, 4);
555 doc_register_readb(docg3, DOC_FLASHCONTROL);
556 return doc_wait_ready(docg3);
557}
558
559/**
560 * doc_write_page_ecc_init - Initialize hardware BCH ECC engine
561 * @docg3: the device
562 * @len: the number of bytes covered by the ECC (BCH covered)
563 *
564 * The function does initialize the hardware ECC engine to compute the Hamming
565 * ECC (on 1 byte) and the BCH hardware ECC (on 7 bytes).
566 *
567 * Return 0 if succeeded, -EIO on error
568 */
569static int doc_write_page_ecc_init(struct docg3 *docg3, int len)
570{
571 doc_writew(docg3, DOC_ECCCONF0_WRITE_MODE
572 | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
573 | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
574 DOC_ECCCONF0);
575 doc_delay(docg3, 4);
576 doc_register_readb(docg3, DOC_FLASHCONTROL);
577 return doc_wait_ready(docg3);
578}
579
580/**
581 * doc_ecc_disable - Disable Hamming and BCH ECC hardware calculator
582 * @docg3: the device
583 *
584 * Disables the hardware ECC generator and checker, for unchecked reads (as when
585 * reading OOB only or write status byte).
586 */
587static void doc_ecc_disable(struct docg3 *docg3)
588{
589 doc_writew(docg3, DOC_ECCCONF0_READ_MODE, DOC_ECCCONF0);
590 doc_delay(docg3, 4);
591}
592
593/**
594 * doc_hamming_ecc_init - Initialize hardware Hamming ECC engine
595 * @docg3: the device
596 * @nb_bytes: the number of bytes covered by the ECC (Hamming covered)
597 *
598 * This function programs the ECC hardware to compute the hamming code on the
599 * last provided N bytes to the hardware generator.
600 */
601static void doc_hamming_ecc_init(struct docg3 *docg3, int nb_bytes)
602{
603 u8 ecc_conf1;
604
605 ecc_conf1 = doc_register_readb(docg3, DOC_ECCCONF1);
606 ecc_conf1 &= ~DOC_ECCCONF1_HAMMING_BITS_MASK;
607 ecc_conf1 |= (nb_bytes & DOC_ECCCONF1_HAMMING_BITS_MASK);
608 doc_writeb(docg3, ecc_conf1, DOC_ECCCONF1);
609}
610
611/**
612 * doc_ecc_bch_fix_data - Fix if need be read data from flash
613 * @docg3: the device
614 * @buf: the buffer of read data (512 + 7 + 1 bytes)
615 * @hwecc: the hardware calculated ECC.
616 * It's in fact recv_ecc ^ calc_ecc, where recv_ecc was read from OOB
617 * area data, and calc_ecc the ECC calculated by the hardware generator.
618 *
619 * Checks if the received data matches the ECC, and if an error is detected,
620 * tries to fix the bit flips (at most 4) in the buffer buf. As the docg3
621 * understands the (data, ecc, syndroms) in an inverted order in comparison to
622 * the BCH library, the function reverses the order of bits (ie. bit7 and bit0,
623 * bit6 and bit 1, ...) for all ECC data.
624 *
625 * The hardware ecc unit produces oob_ecc ^ calc_ecc. The kernel's bch
626 * algorithm is used to decode this. However the hw operates on page
627 * data in a bit order that is the reverse of that of the bch alg,
628 * requiring that the bits be reversed on the result. Thanks to Ivan
629 * Djelic for his analysis.
630 *
631 * Returns number of fixed bits (0, 1, 2, 3, 4) or -EBADMSG if too many bit
632 * errors were detected and cannot be fixed.
633 */
634static int doc_ecc_bch_fix_data(struct docg3 *docg3, void *buf, u8 *hwecc)
635{
636 u8 ecc[DOC_ECC_BCH_SIZE];
637 int errorpos[DOC_ECC_BCH_T], i, numerrs;
638
639 for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
640 ecc[i] = bitrev8(hwecc[i]);
641 numerrs = decode_bch(docg3->cascade->bch, NULL,
642 DOC_ECC_BCH_COVERED_BYTES,
643 NULL, ecc, NULL, errorpos);
644 BUG_ON(numerrs == -EINVAL);
645 if (numerrs < 0)
646 goto out;
647
648 for (i = 0; i < numerrs; i++)
649 errorpos[i] = (errorpos[i] & ~7) | (7 - (errorpos[i] & 7));
650 for (i = 0; i < numerrs; i++)
651 if (errorpos[i] < DOC_ECC_BCH_COVERED_BYTES*8)
652 /* error is located in data, correct it */
653 change_bit(errorpos[i], buf);
654out:
655 doc_dbg("doc_ecc_bch_fix_data: flipped %d bits\n", numerrs);
656 return numerrs;
657}
658
659
660/**
661 * doc_read_page_prepare - Prepares reading data from a flash page
662 * @docg3: the device
663 * @block0: the first plane block index on flash memory
664 * @block1: the second plane block index on flash memory
665 * @page: the page index in the block
666 * @offset: the offset in the page (must be a multiple of 4)
667 *
668 * Prepares the page to be read in the flash memory :
669 * - tell ASIC to map the flash pages
670 * - tell ASIC to be in read mode
671 *
672 * After a call to this method, a call to doc_read_page_finish is mandatory,
673 * to end the read cycle of the flash.
674 *
675 * Read data from a flash page. The length to be read must be between 0 and
676 * (page_size + oob_size + wear_size), ie. 532, and a multiple of 4 (because
677 * the extra bytes reading is not implemented).
678 *
679 * As pages are grouped by 2 (in 2 planes), reading from a page must be done
680 * in two steps:
681 * - one read of 512 bytes at offset 0
682 * - one read of 512 bytes at offset 512 + 16
683 *
684 * Returns 0 if successful, -EIO if a read error occurred.
685 */
686static int doc_read_page_prepare(struct docg3 *docg3, int block0, int block1,
687 int page, int offset)
688{
689 int wear_area = 0, ret = 0;
690
691 doc_dbg("doc_read_page_prepare(blocks=(%d,%d), page=%d, ofsInPage=%d)\n",
692 block0, block1, page, offset);
693 if (offset >= DOC_LAYOUT_WEAR_OFFSET)
694 wear_area = 1;
695 if (!wear_area && offset > (DOC_LAYOUT_PAGE_OOB_SIZE * 2))
696 return -EINVAL;
697
698 doc_set_device_id(docg3, docg3->device_id);
699 ret = doc_reset_seq(docg3);
700 if (ret)
701 goto err;
702
703 /* Program the flash address block and page */
704 ret = doc_read_seek(docg3, block0, block1, page, wear_area, offset);
705 if (ret)
706 goto err;
707
708 doc_flash_command(docg3, DOC_CMD_READ_ALL_PLANES);
709 doc_delay(docg3, 2);
710 doc_wait_ready(docg3);
711
712 doc_flash_command(docg3, DOC_CMD_SET_ADDR_READ);
713 doc_delay(docg3, 1);
714 if (offset >= DOC_LAYOUT_PAGE_SIZE * 2)
715 offset -= 2 * DOC_LAYOUT_PAGE_SIZE;
716 doc_flash_address(docg3, offset >> 2);
717 doc_delay(docg3, 1);
718 doc_wait_ready(docg3);
719
720 doc_flash_command(docg3, DOC_CMD_READ_FLASH);
721
722 return 0;
723err:
724 doc_writeb(docg3, 0, DOC_DATAEND);
725 doc_delay(docg3, 2);
726 return -EIO;
727}
728
729/**
730 * doc_read_page_getbytes - Reads bytes from a prepared page
731 * @docg3: the device
732 * @len: the number of bytes to be read (must be a multiple of 4)
733 * @buf: the buffer to be filled in (or NULL is forget bytes)
734 * @first: 1 if first time read, DOC_READADDRESS should be set
735 * @last_odd: 1 if last read ended up on an odd byte
736 *
737 * Reads bytes from a prepared page. There is a trickery here : if the last read
738 * ended up on an odd offset in the 1024 bytes double page, ie. between the 2
739 * planes, the first byte must be read apart. If a word (16bit) read was used,
740 * the read would return the byte of plane 2 as low *and* high endian, which
741 * will mess the read.
742 *
743 */
744static int doc_read_page_getbytes(struct docg3 *docg3, int len, u_char *buf,
745 int first, int last_odd)
746{
747 if (last_odd && len > 0) {
748 doc_read_data_area(docg3, buf, 1, first);
749 doc_read_data_area(docg3, buf ? buf + 1 : buf, len - 1, 0);
750 } else {
751 doc_read_data_area(docg3, buf, len, first);
752 }
753 doc_delay(docg3, 2);
754 return len;
755}
756
757/**
758 * doc_write_page_putbytes - Writes bytes into a prepared page
759 * @docg3: the device
760 * @len: the number of bytes to be written
761 * @buf: the buffer of input bytes
762 *
763 */
764static void doc_write_page_putbytes(struct docg3 *docg3, int len,
765 const u_char *buf)
766{
767 doc_write_data_area(docg3, buf, len);
768 doc_delay(docg3, 2);
769}
770
771/**
772 * doc_get_bch_hw_ecc - Get hardware calculated BCH ECC
773 * @docg3: the device
774 * @hwecc: the array of 7 integers where the hardware ecc will be stored
775 */
776static void doc_get_bch_hw_ecc(struct docg3 *docg3, u8 *hwecc)
777{
778 int i;
779
780 for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
781 hwecc[i] = doc_register_readb(docg3, DOC_BCH_HW_ECC(i));
782}
783
784/**
785 * doc_page_finish - Ends reading/writing of a flash page
786 * @docg3: the device
787 */
788static void doc_page_finish(struct docg3 *docg3)
789{
790 doc_writeb(docg3, 0, DOC_DATAEND);
791 doc_delay(docg3, 2);
792}
793
794/**
795 * doc_read_page_finish - Ends reading of a flash page
796 * @docg3: the device
797 *
798 * As a side effect, resets the chip selector to 0. This ensures that after each
799 * read operation, the floor 0 is selected. Therefore, if the systems halts, the
800 * reboot will boot on floor 0, where the IPL is.
801 */
802static void doc_read_page_finish(struct docg3 *docg3)
803{
804 doc_page_finish(docg3);
805 doc_set_device_id(docg3, 0);
806}
807
808/**
809 * calc_block_sector - Calculate blocks, pages and ofs.
810
811 * @from: offset in flash
812 * @block0: first plane block index calculated
813 * @block1: second plane block index calculated
814 * @page: page calculated
815 * @ofs: offset in page
816 * @reliable: 0 if docg3 in normal mode, 1 if docg3 in fast mode, 2 if docg3 in
817 * reliable mode.
818 *
819 * The calculation is based on the reliable/normal mode. In normal mode, the 64
820 * pages of a block are available. In reliable mode, as pages 2*n and 2*n+1 are
821 * clones, only 32 pages per block are available.
822 */
823static void calc_block_sector(loff_t from, int *block0, int *block1, int *page,
824 int *ofs, int reliable)
825{
826 uint sector, pages_biblock;
827
828 pages_biblock = DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_NBPLANES;
829 if (reliable == 1 || reliable == 2)
830 pages_biblock /= 2;
831
832 sector = from / DOC_LAYOUT_PAGE_SIZE;
833 *block0 = sector / pages_biblock * DOC_LAYOUT_NBPLANES;
834 *block1 = *block0 + 1;
835 *page = sector % pages_biblock;
836 *page /= DOC_LAYOUT_NBPLANES;
837 if (reliable == 1 || reliable == 2)
838 *page *= 2;
839 if (sector % 2)
840 *ofs = DOC_LAYOUT_PAGE_OOB_SIZE;
841 else
842 *ofs = 0;
843}
844
845/**
846 * doc_read_oob - Read out of band bytes from flash
847 * @mtd: the device
848 * @from: the offset from first block and first page, in bytes, aligned on page
849 * size
850 * @ops: the mtd oob structure
851 *
852 * Reads flash memory OOB area of pages.
853 *
854 * Returns 0 if read successful, of -EIO, -EINVAL if an error occurred
855 */
856static int doc_read_oob(struct mtd_info *mtd, loff_t from,
857 struct mtd_oob_ops *ops)
858{
859 struct docg3 *docg3 = mtd->priv;
860 int block0, block1, page, ret, skip, ofs = 0;
861 u8 *oobbuf = ops->oobbuf;
862 u8 *buf = ops->datbuf;
863 size_t len, ooblen, nbdata, nboob;
864 u8 hwecc[DOC_ECC_BCH_SIZE], eccconf1;
865 int max_bitflips = 0;
866
867 if (buf)
868 len = ops->len;
869 else
870 len = 0;
871 if (oobbuf)
872 ooblen = ops->ooblen;
873 else
874 ooblen = 0;
875
876 if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
877 oobbuf += ops->ooboffs;
878
879 doc_dbg("doc_read_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
880 from, ops->mode, buf, len, oobbuf, ooblen);
881 if (ooblen % DOC_LAYOUT_OOB_SIZE)
882 return -EINVAL;
883
884 if (from + len > mtd->size)
885 return -EINVAL;
886
887 ops->oobretlen = 0;
888 ops->retlen = 0;
889 ret = 0;
890 skip = from % DOC_LAYOUT_PAGE_SIZE;
891 mutex_lock(&docg3->cascade->lock);
892 while (ret >= 0 && (len > 0 || ooblen > 0)) {
893 calc_block_sector(from - skip, &block0, &block1, &page, &ofs,
894 docg3->reliable);
895 nbdata = min_t(size_t, len, DOC_LAYOUT_PAGE_SIZE - skip);
896 nboob = min_t(size_t, ooblen, (size_t)DOC_LAYOUT_OOB_SIZE);
897 ret = doc_read_page_prepare(docg3, block0, block1, page, ofs);
898 if (ret < 0)
899 goto out;
900 ret = doc_read_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
901 if (ret < 0)
902 goto err_in_read;
903 ret = doc_read_page_getbytes(docg3, skip, NULL, 1, 0);
904 if (ret < skip)
905 goto err_in_read;
906 ret = doc_read_page_getbytes(docg3, nbdata, buf, 0, skip % 2);
907 if (ret < nbdata)
908 goto err_in_read;
909 doc_read_page_getbytes(docg3,
910 DOC_LAYOUT_PAGE_SIZE - nbdata - skip,
911 NULL, 0, (skip + nbdata) % 2);
912 ret = doc_read_page_getbytes(docg3, nboob, oobbuf, 0, 0);
913 if (ret < nboob)
914 goto err_in_read;
915 doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE - nboob,
916 NULL, 0, nboob % 2);
917
918 doc_get_bch_hw_ecc(docg3, hwecc);
919 eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1);
920
921 if (nboob >= DOC_LAYOUT_OOB_SIZE) {
922 doc_dbg("OOB - INFO: %*phC\n", 7, oobbuf);
923 doc_dbg("OOB - HAMMING: %02x\n", oobbuf[7]);
924 doc_dbg("OOB - BCH_ECC: %*phC\n", 7, oobbuf + 8);
925 doc_dbg("OOB - UNUSED: %02x\n", oobbuf[15]);
926 }
927 doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1);
928 doc_dbg("ECC HW_ECC: %*phC\n", 7, hwecc);
929
930 ret = -EIO;
931 if (is_prot_seq_error(docg3))
932 goto err_in_read;
933 ret = 0;
934 if ((block0 >= DOC_LAYOUT_BLOCK_FIRST_DATA) &&
935 (eccconf1 & DOC_ECCCONF1_BCH_SYNDROM_ERR) &&
936 (eccconf1 & DOC_ECCCONF1_PAGE_IS_WRITTEN) &&
937 (ops->mode != MTD_OPS_RAW) &&
938 (nbdata == DOC_LAYOUT_PAGE_SIZE)) {
939 ret = doc_ecc_bch_fix_data(docg3, buf, hwecc);
940 if (ret < 0) {
941 mtd->ecc_stats.failed++;
942 ret = -EBADMSG;
943 }
944 if (ret > 0) {
945 mtd->ecc_stats.corrected += ret;
946 max_bitflips = max(max_bitflips, ret);
947 ret = max_bitflips;
948 }
949 }
950
951 doc_read_page_finish(docg3);
952 ops->retlen += nbdata;
953 ops->oobretlen += nboob;
954 buf += nbdata;
955 oobbuf += nboob;
956 len -= nbdata;
957 ooblen -= nboob;
958 from += DOC_LAYOUT_PAGE_SIZE;
959 skip = 0;
960 }
961
962out:
963 mutex_unlock(&docg3->cascade->lock);
964 return ret;
965err_in_read:
966 doc_read_page_finish(docg3);
967 goto out;
968}
969
970/**
971 * doc_read - Read bytes from flash
972 * @mtd: the device
973 * @from: the offset from first block and first page, in bytes, aligned on page
974 * size
975 * @len: the number of bytes to read (must be a multiple of 4)
976 * @retlen: the number of bytes actually read
977 * @buf: the filled in buffer
978 *
979 * Reads flash memory pages. This function does not read the OOB chunk, but only
980 * the page data.
981 *
982 * Returns 0 if read successful, of -EIO, -EINVAL if an error occurred
983 */
984static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
985 size_t *retlen, u_char *buf)
986{
987 struct mtd_oob_ops ops;
988 size_t ret;
989
990 memset(&ops, 0, sizeof(ops));
991 ops.datbuf = buf;
992 ops.len = len;
993 ops.mode = MTD_OPS_AUTO_OOB;
994
995 ret = doc_read_oob(mtd, from, &ops);
996 *retlen = ops.retlen;
997 return ret;
998}
999
1000static int doc_reload_bbt(struct docg3 *docg3)
1001{
1002 int block = DOC_LAYOUT_BLOCK_BBT;
1003 int ret = 0, nbpages, page;
1004 u_char *buf = docg3->bbt;
1005
1006 nbpages = DIV_ROUND_UP(docg3->max_block + 1, 8 * DOC_LAYOUT_PAGE_SIZE);
1007 for (page = 0; !ret && (page < nbpages); page++) {
1008 ret = doc_read_page_prepare(docg3, block, block + 1,
1009 page + DOC_LAYOUT_PAGE_BBT, 0);
1010 if (!ret)
1011 ret = doc_read_page_ecc_init(docg3,
1012 DOC_LAYOUT_PAGE_SIZE);
1013 if (!ret)
1014 doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE,
1015 buf, 1, 0);
1016 buf += DOC_LAYOUT_PAGE_SIZE;
1017 }
1018 doc_read_page_finish(docg3);
1019 return ret;
1020}
1021
1022/**
1023 * doc_block_isbad - Checks whether a block is good or not
1024 * @mtd: the device
1025 * @from: the offset to find the correct block
1026 *
1027 * Returns 1 if block is bad, 0 if block is good
1028 */
1029static int doc_block_isbad(struct mtd_info *mtd, loff_t from)
1030{
1031 struct docg3 *docg3 = mtd->priv;
1032 int block0, block1, page, ofs, is_good;
1033
1034 calc_block_sector(from, &block0, &block1, &page, &ofs,
1035 docg3->reliable);
1036 doc_dbg("doc_block_isbad(from=%lld) => block=(%d,%d), page=%d, ofs=%d\n",
1037 from, block0, block1, page, ofs);
1038
1039 if (block0 < DOC_LAYOUT_BLOCK_FIRST_DATA)
1040 return 0;
1041 if (block1 > docg3->max_block)
1042 return -EINVAL;
1043
1044 is_good = docg3->bbt[block0 >> 3] & (1 << (block0 & 0x7));
1045 return !is_good;
1046}
1047
1048#if 0
1049/**
1050 * doc_get_erase_count - Get block erase count
1051 * @docg3: the device
1052 * @from: the offset in which the block is.
1053 *
1054 * Get the number of times a block was erased. The number is the maximum of
1055 * erase times between first and second plane (which should be equal normally).
1056 *
1057 * Returns The number of erases, or -EINVAL or -EIO on error.
1058 */
1059static int doc_get_erase_count(struct docg3 *docg3, loff_t from)
1060{
1061 u8 buf[DOC_LAYOUT_WEAR_SIZE];
1062 int ret, plane1_erase_count, plane2_erase_count;
1063 int block0, block1, page, ofs;
1064
1065 doc_dbg("doc_get_erase_count(from=%lld, buf=%p)\n", from, buf);
1066 if (from % DOC_LAYOUT_PAGE_SIZE)
1067 return -EINVAL;
1068 calc_block_sector(from, &block0, &block1, &page, &ofs, docg3->reliable);
1069 if (block1 > docg3->max_block)
1070 return -EINVAL;
1071
1072 ret = doc_reset_seq(docg3);
1073 if (!ret)
1074 ret = doc_read_page_prepare(docg3, block0, block1, page,
1075 ofs + DOC_LAYOUT_WEAR_OFFSET, 0);
1076 if (!ret)
1077 ret = doc_read_page_getbytes(docg3, DOC_LAYOUT_WEAR_SIZE,
1078 buf, 1, 0);
1079 doc_read_page_finish(docg3);
1080
1081 if (ret || (buf[0] != DOC_ERASE_MARK) || (buf[2] != DOC_ERASE_MARK))
1082 return -EIO;
1083 plane1_erase_count = (u8)(~buf[1]) | ((u8)(~buf[4]) << 8)
1084 | ((u8)(~buf[5]) << 16);
1085 plane2_erase_count = (u8)(~buf[3]) | ((u8)(~buf[6]) << 8)
1086 | ((u8)(~buf[7]) << 16);
1087
1088 return max(plane1_erase_count, plane2_erase_count);
1089}
1090#endif
1091
1092/**
1093 * doc_get_op_status - get erase/write operation status
1094 * @docg3: the device
1095 *
1096 * Queries the status from the chip, and returns it
1097 *
1098 * Returns the status (bits DOC_PLANES_STATUS_*)
1099 */
1100static int doc_get_op_status(struct docg3 *docg3)
1101{
1102 u8 status;
1103
1104 doc_flash_sequence(docg3, DOC_SEQ_PLANES_STATUS);
1105 doc_flash_command(docg3, DOC_CMD_PLANES_STATUS);
1106 doc_delay(docg3, 5);
1107
1108 doc_ecc_disable(docg3);
1109 doc_read_data_area(docg3, &status, 1, 1);
1110 return status;
1111}
1112
1113/**
1114 * doc_write_erase_wait_status - wait for write or erase completion
1115 * @docg3: the device
1116 *
1117 * Wait for the chip to be ready again after erase or write operation, and check
1118 * erase/write status.
1119 *
1120 * Returns 0 if erase successful, -EIO if erase/write issue, -ETIMEOUT if
1121 * timeout
1122 */
1123static int doc_write_erase_wait_status(struct docg3 *docg3)
1124{
1125 int i, status, ret = 0;
1126
1127 for (i = 0; !doc_is_ready(docg3) && i < 5; i++)
1128 msleep(20);
1129 if (!doc_is_ready(docg3)) {
1130 doc_dbg("Timeout reached and the chip is still not ready\n");
1131 ret = -EAGAIN;
1132 goto out;
1133 }
1134
1135 status = doc_get_op_status(docg3);
1136 if (status & DOC_PLANES_STATUS_FAIL) {
1137 doc_dbg("Erase/Write failed on (a) plane(s), status = %x\n",
1138 status);
1139 ret = -EIO;
1140 }
1141
1142out:
1143 doc_page_finish(docg3);
1144 return ret;
1145}
1146
1147/**
1148 * doc_erase_block - Erase a couple of blocks
1149 * @docg3: the device
1150 * @block0: the first block to erase (leftmost plane)
1151 * @block1: the second block to erase (rightmost plane)
1152 *
1153 * Erase both blocks, and return operation status
1154 *
1155 * Returns 0 if erase successful, -EIO if erase issue, -ETIMEOUT if chip not
1156 * ready for too long
1157 */
1158static int doc_erase_block(struct docg3 *docg3, int block0, int block1)
1159{
1160 int ret, sector;
1161
1162 doc_dbg("doc_erase_block(blocks=(%d,%d))\n", block0, block1);
1163 ret = doc_reset_seq(docg3);
1164 if (ret)
1165 return -EIO;
1166
1167 doc_set_reliable_mode(docg3);
1168 doc_flash_sequence(docg3, DOC_SEQ_ERASE);
1169
1170 sector = block0 << DOC_ADDR_BLOCK_SHIFT;
1171 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
1172 doc_setup_addr_sector(docg3, sector);
1173 sector = block1 << DOC_ADDR_BLOCK_SHIFT;
1174 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
1175 doc_setup_addr_sector(docg3, sector);
1176 doc_delay(docg3, 1);
1177
1178 doc_flash_command(docg3, DOC_CMD_ERASECYCLE2);
1179 doc_delay(docg3, 2);
1180
1181 if (is_prot_seq_error(docg3)) {
1182 doc_err("Erase blocks %d,%d error\n", block0, block1);
1183 return -EIO;
1184 }
1185
1186 return doc_write_erase_wait_status(docg3);
1187}
1188
1189/**
1190 * doc_erase - Erase a portion of the chip
1191 * @mtd: the device
1192 * @info: the erase info
1193 *
1194 * Erase a bunch of contiguous blocks, by pairs, as a "mtd" page of 1024 is
1195 * split into 2 pages of 512 bytes on 2 contiguous blocks.
1196 *
1197 * Returns 0 if erase successful, -EINVAL if addressing error, -EIO if erase
1198 * issue
1199 */
1200static int doc_erase(struct mtd_info *mtd, struct erase_info *info)
1201{
1202 struct docg3 *docg3 = mtd->priv;
1203 uint64_t len;
1204 int block0, block1, page, ret, ofs = 0;
1205
1206 doc_dbg("doc_erase(from=%lld, len=%lld\n", info->addr, info->len);
1207
1208 info->state = MTD_ERASE_PENDING;
1209 calc_block_sector(info->addr + info->len, &block0, &block1, &page,
1210 &ofs, docg3->reliable);
1211 ret = -EINVAL;
1212 if (info->addr + info->len > mtd->size || page || ofs)
1213 goto reset_err;
1214
1215 ret = 0;
1216 calc_block_sector(info->addr, &block0, &block1, &page, &ofs,
1217 docg3->reliable);
1218 mutex_lock(&docg3->cascade->lock);
1219 doc_set_device_id(docg3, docg3->device_id);
1220 doc_set_reliable_mode(docg3);
1221 for (len = info->len; !ret && len > 0; len -= mtd->erasesize) {
1222 info->state = MTD_ERASING;
1223 ret = doc_erase_block(docg3, block0, block1);
1224 block0 += 2;
1225 block1 += 2;
1226 }
1227 mutex_unlock(&docg3->cascade->lock);
1228
1229 if (ret)
1230 goto reset_err;
1231
1232 info->state = MTD_ERASE_DONE;
1233 return 0;
1234
1235reset_err:
1236 info->state = MTD_ERASE_FAILED;
1237 return ret;
1238}
1239
1240/**
1241 * doc_write_page - Write a single page to the chip
1242 * @docg3: the device
1243 * @to: the offset from first block and first page, in bytes, aligned on page
1244 * size
1245 * @buf: buffer to get bytes from
1246 * @oob: buffer to get out of band bytes from (can be NULL if no OOB should be
1247 * written)
1248 * @autoecc: if 0, all 16 bytes from OOB are taken, regardless of HW Hamming or
1249 * BCH computations. If 1, only bytes 0-7 and byte 15 are taken,
1250 * remaining ones are filled with hardware Hamming and BCH
1251 * computations. Its value is not meaningfull is oob == NULL.
1252 *
1253 * Write one full page (ie. 1 page split on two planes), of 512 bytes, with the
1254 * OOB data. The OOB ECC is automatically computed by the hardware Hamming and
1255 * BCH generator if autoecc is not null.
1256 *
1257 * Returns 0 if write successful, -EIO if write error, -EAGAIN if timeout
1258 */
1259static int doc_write_page(struct docg3 *docg3, loff_t to, const u_char *buf,
1260 const u_char *oob, int autoecc)
1261{
1262 int block0, block1, page, ret, ofs = 0;
1263 u8 hwecc[DOC_ECC_BCH_SIZE], hamming;
1264
1265 doc_dbg("doc_write_page(to=%lld)\n", to);
1266 calc_block_sector(to, &block0, &block1, &page, &ofs, docg3->reliable);
1267
1268 doc_set_device_id(docg3, docg3->device_id);
1269 ret = doc_reset_seq(docg3);
1270 if (ret)
1271 goto err;
1272
1273 /* Program the flash address block and page */
1274 ret = doc_write_seek(docg3, block0, block1, page, ofs);
1275 if (ret)
1276 goto err;
1277
1278 doc_write_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
1279 doc_delay(docg3, 2);
1280 doc_write_page_putbytes(docg3, DOC_LAYOUT_PAGE_SIZE, buf);
1281
1282 if (oob && autoecc) {
1283 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ, oob);
1284 doc_delay(docg3, 2);
1285 oob += DOC_LAYOUT_OOB_UNUSED_OFS;
1286
1287 hamming = doc_register_readb(docg3, DOC_HAMMINGPARITY);
1288 doc_delay(docg3, 2);
1289 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_HAMMING_SZ,
1290 &hamming);
1291 doc_delay(docg3, 2);
1292
1293 doc_get_bch_hw_ecc(docg3, hwecc);
1294 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_BCH_SZ, hwecc);
1295 doc_delay(docg3, 2);
1296
1297 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_UNUSED_SZ, oob);
1298 }
1299 if (oob && !autoecc)
1300 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_SIZE, oob);
1301
1302 doc_delay(docg3, 2);
1303 doc_page_finish(docg3);
1304 doc_delay(docg3, 2);
1305 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE2);
1306 doc_delay(docg3, 2);
1307
1308 /*
1309 * The wait status will perform another doc_page_finish() call, but that
1310 * seems to please the docg3, so leave it.
1311 */
1312 ret = doc_write_erase_wait_status(docg3);
1313 return ret;
1314err:
1315 doc_read_page_finish(docg3);
1316 return ret;
1317}
1318
1319/**
1320 * doc_guess_autoecc - Guess autoecc mode from mbd_oob_ops
1321 * @ops: the oob operations
1322 *
1323 * Returns 0 or 1 if success, -EINVAL if invalid oob mode
1324 */
1325static int doc_guess_autoecc(struct mtd_oob_ops *ops)
1326{
1327 int autoecc;
1328
1329 switch (ops->mode) {
1330 case MTD_OPS_PLACE_OOB:
1331 case MTD_OPS_AUTO_OOB:
1332 autoecc = 1;
1333 break;
1334 case MTD_OPS_RAW:
1335 autoecc = 0;
1336 break;
1337 default:
1338 autoecc = -EINVAL;
1339 }
1340 return autoecc;
1341}
1342
1343/**
1344 * doc_fill_autooob - Fill a 16 bytes OOB from 8 non-ECC bytes
1345 * @dst: the target 16 bytes OOB buffer
1346 * @oobsrc: the source 8 bytes non-ECC OOB buffer
1347 *
1348 */
1349static void doc_fill_autooob(u8 *dst, u8 *oobsrc)
1350{
1351 memcpy(dst, oobsrc, DOC_LAYOUT_OOB_PAGEINFO_SZ);
1352 dst[DOC_LAYOUT_OOB_UNUSED_OFS] = oobsrc[DOC_LAYOUT_OOB_PAGEINFO_SZ];
1353}
1354
1355/**
1356 * doc_backup_oob - Backup OOB into docg3 structure
1357 * @docg3: the device
1358 * @to: the page offset in the chip
1359 * @ops: the OOB size and buffer
1360 *
1361 * As the docg3 should write a page with its OOB in one pass, and some userland
1362 * applications do write_oob() to setup the OOB and then write(), store the OOB
1363 * into a temporary storage. This is very dangerous, as 2 concurrent
1364 * applications could store an OOB, and then write their pages (which will
1365 * result into one having its OOB corrupted).
1366 *
1367 * The only reliable way would be for userland to call doc_write_oob() with both
1368 * the page data _and_ the OOB area.
1369 *
1370 * Returns 0 if success, -EINVAL if ops content invalid
1371 */
1372static int doc_backup_oob(struct docg3 *docg3, loff_t to,
1373 struct mtd_oob_ops *ops)
1374{
1375 int ooblen = ops->ooblen, autoecc;
1376
1377 if (ooblen != DOC_LAYOUT_OOB_SIZE)
1378 return -EINVAL;
1379 autoecc = doc_guess_autoecc(ops);
1380 if (autoecc < 0)
1381 return autoecc;
1382
1383 docg3->oob_write_ofs = to;
1384 docg3->oob_autoecc = autoecc;
1385 if (ops->mode == MTD_OPS_AUTO_OOB) {
1386 doc_fill_autooob(docg3->oob_write_buf, ops->oobbuf);
1387 ops->oobretlen = 8;
1388 } else {
1389 memcpy(docg3->oob_write_buf, ops->oobbuf, DOC_LAYOUT_OOB_SIZE);
1390 ops->oobretlen = DOC_LAYOUT_OOB_SIZE;
1391 }
1392 return 0;
1393}
1394
1395/**
1396 * doc_write_oob - Write out of band bytes to flash
1397 * @mtd: the device
1398 * @ofs: the offset from first block and first page, in bytes, aligned on page
1399 * size
1400 * @ops: the mtd oob structure
1401 *
1402 * Either write OOB data into a temporary buffer, for the subsequent write
1403 * page. The provided OOB should be 16 bytes long. If a data buffer is provided
1404 * as well, issue the page write.
1405 * Or provide data without OOB, and then a all zeroed OOB will be used (ECC will
1406 * still be filled in if asked for).
1407 *
1408 * Returns 0 is successful, EINVAL if length is not 14 bytes
1409 */
1410static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
1411 struct mtd_oob_ops *ops)
1412{
1413 struct docg3 *docg3 = mtd->priv;
1414 int ret, autoecc, oobdelta;
1415 u8 *oobbuf = ops->oobbuf;
1416 u8 *buf = ops->datbuf;
1417 size_t len, ooblen;
1418 u8 oob[DOC_LAYOUT_OOB_SIZE];
1419
1420 if (buf)
1421 len = ops->len;
1422 else
1423 len = 0;
1424 if (oobbuf)
1425 ooblen = ops->ooblen;
1426 else
1427 ooblen = 0;
1428
1429 if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
1430 oobbuf += ops->ooboffs;
1431
1432 doc_dbg("doc_write_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
1433 ofs, ops->mode, buf, len, oobbuf, ooblen);
1434 switch (ops->mode) {
1435 case MTD_OPS_PLACE_OOB:
1436 case MTD_OPS_RAW:
1437 oobdelta = mtd->oobsize;
1438 break;
1439 case MTD_OPS_AUTO_OOB:
1440 oobdelta = mtd->ecclayout->oobavail;
1441 break;
1442 default:
1443 return -EINVAL;
1444 }
1445 if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % oobdelta) ||
1446 (ofs % DOC_LAYOUT_PAGE_SIZE))
1447 return -EINVAL;
1448 if (len && ooblen &&
1449 (len / DOC_LAYOUT_PAGE_SIZE) != (ooblen / oobdelta))
1450 return -EINVAL;
1451 if (ofs + len > mtd->size)
1452 return -EINVAL;
1453
1454 ops->oobretlen = 0;
1455 ops->retlen = 0;
1456 ret = 0;
1457 if (len == 0 && ooblen == 0)
1458 return -EINVAL;
1459 if (len == 0 && ooblen > 0)
1460 return doc_backup_oob(docg3, ofs, ops);
1461
1462 autoecc = doc_guess_autoecc(ops);
1463 if (autoecc < 0)
1464 return autoecc;
1465
1466 mutex_lock(&docg3->cascade->lock);
1467 while (!ret && len > 0) {
1468 memset(oob, 0, sizeof(oob));
1469 if (ofs == docg3->oob_write_ofs)
1470 memcpy(oob, docg3->oob_write_buf, DOC_LAYOUT_OOB_SIZE);
1471 else if (ooblen > 0 && ops->mode == MTD_OPS_AUTO_OOB)
1472 doc_fill_autooob(oob, oobbuf);
1473 else if (ooblen > 0)
1474 memcpy(oob, oobbuf, DOC_LAYOUT_OOB_SIZE);
1475 ret = doc_write_page(docg3, ofs, buf, oob, autoecc);
1476
1477 ofs += DOC_LAYOUT_PAGE_SIZE;
1478 len -= DOC_LAYOUT_PAGE_SIZE;
1479 buf += DOC_LAYOUT_PAGE_SIZE;
1480 if (ooblen) {
1481 oobbuf += oobdelta;
1482 ooblen -= oobdelta;
1483 ops->oobretlen += oobdelta;
1484 }
1485 ops->retlen += DOC_LAYOUT_PAGE_SIZE;
1486 }
1487
1488 doc_set_device_id(docg3, 0);
1489 mutex_unlock(&docg3->cascade->lock);
1490 return ret;
1491}
1492
1493/**
1494 * doc_write - Write a buffer to the chip
1495 * @mtd: the device
1496 * @to: the offset from first block and first page, in bytes, aligned on page
1497 * size
1498 * @len: the number of bytes to write (must be a full page size, ie. 512)
1499 * @retlen: the number of bytes actually written (0 or 512)
1500 * @buf: the buffer to get bytes from
1501 *
1502 * Writes data to the chip.
1503 *
1504 * Returns 0 if write successful, -EIO if write error
1505 */
1506static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
1507 size_t *retlen, const u_char *buf)
1508{
1509 struct docg3 *docg3 = mtd->priv;
1510 int ret;
1511 struct mtd_oob_ops ops;
1512
1513 doc_dbg("doc_write(to=%lld, len=%zu)\n", to, len);
1514 ops.datbuf = (char *)buf;
1515 ops.len = len;
1516 ops.mode = MTD_OPS_PLACE_OOB;
1517 ops.oobbuf = NULL;
1518 ops.ooblen = 0;
1519 ops.ooboffs = 0;
1520
1521 ret = doc_write_oob(mtd, to, &ops);
1522 *retlen = ops.retlen;
1523 return ret;
1524}
1525
1526static struct docg3 *sysfs_dev2docg3(struct device *dev,
1527 struct device_attribute *attr)
1528{
1529 int floor;
1530 struct platform_device *pdev = to_platform_device(dev);
1531 struct mtd_info **docg3_floors = platform_get_drvdata(pdev);
1532
1533 floor = attr->attr.name[1] - '0';
1534 if (floor < 0 || floor >= DOC_MAX_NBFLOORS)
1535 return NULL;
1536 else
1537 return docg3_floors[floor]->priv;
1538}
1539
1540static ssize_t dps0_is_key_locked(struct device *dev,
1541 struct device_attribute *attr, char *buf)
1542{
1543 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1544 int dps0;
1545
1546 mutex_lock(&docg3->cascade->lock);
1547 doc_set_device_id(docg3, docg3->device_id);
1548 dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
1549 doc_set_device_id(docg3, 0);
1550 mutex_unlock(&docg3->cascade->lock);
1551
1552 return sprintf(buf, "%d\n", !(dps0 & DOC_DPS_KEY_OK));
1553}
1554
1555static ssize_t dps1_is_key_locked(struct device *dev,
1556 struct device_attribute *attr, char *buf)
1557{
1558 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1559 int dps1;
1560
1561 mutex_lock(&docg3->cascade->lock);
1562 doc_set_device_id(docg3, docg3->device_id);
1563 dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
1564 doc_set_device_id(docg3, 0);
1565 mutex_unlock(&docg3->cascade->lock);
1566
1567 return sprintf(buf, "%d\n", !(dps1 & DOC_DPS_KEY_OK));
1568}
1569
1570static ssize_t dps0_insert_key(struct device *dev,
1571 struct device_attribute *attr,
1572 const char *buf, size_t count)
1573{
1574 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1575 int i;
1576
1577 if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
1578 return -EINVAL;
1579
1580 mutex_lock(&docg3->cascade->lock);
1581 doc_set_device_id(docg3, docg3->device_id);
1582 for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
1583 doc_writeb(docg3, buf[i], DOC_DPS0_KEY);
1584 doc_set_device_id(docg3, 0);
1585 mutex_unlock(&docg3->cascade->lock);
1586 return count;
1587}
1588
1589static ssize_t dps1_insert_key(struct device *dev,
1590 struct device_attribute *attr,
1591 const char *buf, size_t count)
1592{
1593 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1594 int i;
1595
1596 if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
1597 return -EINVAL;
1598
1599 mutex_lock(&docg3->cascade->lock);
1600 doc_set_device_id(docg3, docg3->device_id);
1601 for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
1602 doc_writeb(docg3, buf[i], DOC_DPS1_KEY);
1603 doc_set_device_id(docg3, 0);
1604 mutex_unlock(&docg3->cascade->lock);
1605 return count;
1606}
1607
1608#define FLOOR_SYSFS(id) { \
1609 __ATTR(f##id##_dps0_is_keylocked, S_IRUGO, dps0_is_key_locked, NULL), \
1610 __ATTR(f##id##_dps1_is_keylocked, S_IRUGO, dps1_is_key_locked, NULL), \
1611 __ATTR(f##id##_dps0_protection_key, S_IWUGO, NULL, dps0_insert_key), \
1612 __ATTR(f##id##_dps1_protection_key, S_IWUGO, NULL, dps1_insert_key), \
1613}
1614
1615static struct device_attribute doc_sys_attrs[DOC_MAX_NBFLOORS][4] = {
1616 FLOOR_SYSFS(0), FLOOR_SYSFS(1), FLOOR_SYSFS(2), FLOOR_SYSFS(3)
1617};
1618
1619static int doc_register_sysfs(struct platform_device *pdev,
1620 struct docg3_cascade *cascade)
1621{
1622 int ret = 0, floor, i = 0;
1623 struct device *dev = &pdev->dev;
1624
1625 for (floor = 0; !ret && floor < DOC_MAX_NBFLOORS &&
1626 cascade->floors[floor]; floor++)
1627 for (i = 0; !ret && i < 4; i++)
1628 ret = device_create_file(dev, &doc_sys_attrs[floor][i]);
1629 if (!ret)
1630 return 0;
1631 do {
1632 while (--i >= 0)
1633 device_remove_file(dev, &doc_sys_attrs[floor][i]);
1634 i = 4;
1635 } while (--floor >= 0);
1636 return ret;
1637}
1638
1639static void doc_unregister_sysfs(struct platform_device *pdev,
1640 struct docg3_cascade *cascade)
1641{
1642 struct device *dev = &pdev->dev;
1643 int floor, i;
1644
1645 for (floor = 0; floor < DOC_MAX_NBFLOORS && cascade->floors[floor];
1646 floor++)
1647 for (i = 0; i < 4; i++)
1648 device_remove_file(dev, &doc_sys_attrs[floor][i]);
1649}
1650
1651/*
1652 * Debug sysfs entries
1653 */
1654static int dbg_flashctrl_show(struct seq_file *s, void *p)
1655{
1656 struct docg3 *docg3 = (struct docg3 *)s->private;
1657
1658 int pos = 0;
1659 u8 fctrl;
1660
1661 mutex_lock(&docg3->cascade->lock);
1662 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
1663 mutex_unlock(&docg3->cascade->lock);
1664
1665 pos += seq_printf(s,
1666 "FlashControl : 0x%02x (%s,CE# %s,%s,%s,flash %s)\n",
1667 fctrl,
1668 fctrl & DOC_CTRL_VIOLATION ? "protocol violation" : "-",
1669 fctrl & DOC_CTRL_CE ? "active" : "inactive",
1670 fctrl & DOC_CTRL_PROTECTION_ERROR ? "protection error" : "-",
1671 fctrl & DOC_CTRL_SEQUENCE_ERROR ? "sequence error" : "-",
1672 fctrl & DOC_CTRL_FLASHREADY ? "ready" : "not ready");
1673 return pos;
1674}
1675DEBUGFS_RO_ATTR(flashcontrol, dbg_flashctrl_show);
1676
1677static int dbg_asicmode_show(struct seq_file *s, void *p)
1678{
1679 struct docg3 *docg3 = (struct docg3 *)s->private;
1680
1681 int pos = 0, pctrl, mode;
1682
1683 mutex_lock(&docg3->cascade->lock);
1684 pctrl = doc_register_readb(docg3, DOC_ASICMODE);
1685 mode = pctrl & 0x03;
1686 mutex_unlock(&docg3->cascade->lock);
1687
1688 pos += seq_printf(s,
1689 "%04x : RAM_WE=%d,RSTIN_RESET=%d,BDETCT_RESET=%d,WRITE_ENABLE=%d,POWERDOWN=%d,MODE=%d%d (",
1690 pctrl,
1691 pctrl & DOC_ASICMODE_RAM_WE ? 1 : 0,
1692 pctrl & DOC_ASICMODE_RSTIN_RESET ? 1 : 0,
1693 pctrl & DOC_ASICMODE_BDETCT_RESET ? 1 : 0,
1694 pctrl & DOC_ASICMODE_MDWREN ? 1 : 0,
1695 pctrl & DOC_ASICMODE_POWERDOWN ? 1 : 0,
1696 mode >> 1, mode & 0x1);
1697
1698 switch (mode) {
1699 case DOC_ASICMODE_RESET:
1700 pos += seq_printf(s, "reset");
1701 break;
1702 case DOC_ASICMODE_NORMAL:
1703 pos += seq_printf(s, "normal");
1704 break;
1705 case DOC_ASICMODE_POWERDOWN:
1706 pos += seq_printf(s, "powerdown");
1707 break;
1708 }
1709 pos += seq_printf(s, ")\n");
1710 return pos;
1711}
1712DEBUGFS_RO_ATTR(asic_mode, dbg_asicmode_show);
1713
1714static int dbg_device_id_show(struct seq_file *s, void *p)
1715{
1716 struct docg3 *docg3 = (struct docg3 *)s->private;
1717 int pos = 0;
1718 int id;
1719
1720 mutex_lock(&docg3->cascade->lock);
1721 id = doc_register_readb(docg3, DOC_DEVICESELECT);
1722 mutex_unlock(&docg3->cascade->lock);
1723
1724 pos += seq_printf(s, "DeviceId = %d\n", id);
1725 return pos;
1726}
1727DEBUGFS_RO_ATTR(device_id, dbg_device_id_show);
1728
1729static int dbg_protection_show(struct seq_file *s, void *p)
1730{
1731 struct docg3 *docg3 = (struct docg3 *)s->private;
1732 int pos = 0;
1733 int protect, dps0, dps0_low, dps0_high, dps1, dps1_low, dps1_high;
1734
1735 mutex_lock(&docg3->cascade->lock);
1736 protect = doc_register_readb(docg3, DOC_PROTECTION);
1737 dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
1738 dps0_low = doc_register_readw(docg3, DOC_DPS0_ADDRLOW);
1739 dps0_high = doc_register_readw(docg3, DOC_DPS0_ADDRHIGH);
1740 dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
1741 dps1_low = doc_register_readw(docg3, DOC_DPS1_ADDRLOW);
1742 dps1_high = doc_register_readw(docg3, DOC_DPS1_ADDRHIGH);
1743 mutex_unlock(&docg3->cascade->lock);
1744
1745 pos += seq_printf(s, "Protection = 0x%02x (",
1746 protect);
1747 if (protect & DOC_PROTECT_FOUNDRY_OTP_LOCK)
1748 pos += seq_printf(s, "FOUNDRY_OTP_LOCK,");
1749 if (protect & DOC_PROTECT_CUSTOMER_OTP_LOCK)
1750 pos += seq_printf(s, "CUSTOMER_OTP_LOCK,");
1751 if (protect & DOC_PROTECT_LOCK_INPUT)
1752 pos += seq_printf(s, "LOCK_INPUT,");
1753 if (protect & DOC_PROTECT_STICKY_LOCK)
1754 pos += seq_printf(s, "STICKY_LOCK,");
1755 if (protect & DOC_PROTECT_PROTECTION_ENABLED)
1756 pos += seq_printf(s, "PROTECTION ON,");
1757 if (protect & DOC_PROTECT_IPL_DOWNLOAD_LOCK)
1758 pos += seq_printf(s, "IPL_DOWNLOAD_LOCK,");
1759 if (protect & DOC_PROTECT_PROTECTION_ERROR)
1760 pos += seq_printf(s, "PROTECT_ERR,");
1761 else
1762 pos += seq_printf(s, "NO_PROTECT_ERR");
1763 pos += seq_printf(s, ")\n");
1764
1765 pos += seq_printf(s, "DPS0 = 0x%02x : "
1766 "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
1767 "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1768 dps0, dps0_low, dps0_high,
1769 !!(dps0 & DOC_DPS_OTP_PROTECTED),
1770 !!(dps0 & DOC_DPS_READ_PROTECTED),
1771 !!(dps0 & DOC_DPS_WRITE_PROTECTED),
1772 !!(dps0 & DOC_DPS_HW_LOCK_ENABLED),
1773 !!(dps0 & DOC_DPS_KEY_OK));
1774 pos += seq_printf(s, "DPS1 = 0x%02x : "
1775 "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
1776 "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1777 dps1, dps1_low, dps1_high,
1778 !!(dps1 & DOC_DPS_OTP_PROTECTED),
1779 !!(dps1 & DOC_DPS_READ_PROTECTED),
1780 !!(dps1 & DOC_DPS_WRITE_PROTECTED),
1781 !!(dps1 & DOC_DPS_HW_LOCK_ENABLED),
1782 !!(dps1 & DOC_DPS_KEY_OK));
1783 return pos;
1784}
1785DEBUGFS_RO_ATTR(protection, dbg_protection_show);
1786
1787static int __init doc_dbg_register(struct docg3 *docg3)
1788{
1789 struct dentry *root, *entry;
1790
1791 root = debugfs_create_dir("docg3", NULL);
1792 if (!root)
1793 return -ENOMEM;
1794
1795 entry = debugfs_create_file("flashcontrol", S_IRUSR, root, docg3,
1796 &flashcontrol_fops);
1797 if (entry)
1798 entry = debugfs_create_file("asic_mode", S_IRUSR, root,
1799 docg3, &asic_mode_fops);
1800 if (entry)
1801 entry = debugfs_create_file("device_id", S_IRUSR, root,
1802 docg3, &device_id_fops);
1803 if (entry)
1804 entry = debugfs_create_file("protection", S_IRUSR, root,
1805 docg3, &protection_fops);
1806 if (entry) {
1807 docg3->debugfs_root = root;
1808 return 0;
1809 } else {
1810 debugfs_remove_recursive(root);
1811 return -ENOMEM;
1812 }
1813}
1814
1815static void __exit doc_dbg_unregister(struct docg3 *docg3)
1816{
1817 debugfs_remove_recursive(docg3->debugfs_root);
1818}
1819
1820/**
1821 * doc_set_driver_info - Fill the mtd_info structure and docg3 structure
1822 * @chip_id: The chip ID of the supported chip
1823 * @mtd: The structure to fill
1824 */
1825static void __init doc_set_driver_info(int chip_id, struct mtd_info *mtd)
1826{
1827 struct docg3 *docg3 = mtd->priv;
1828 int cfg;
1829
1830 cfg = doc_register_readb(docg3, DOC_CONFIGURATION);
1831 docg3->if_cfg = (cfg & DOC_CONF_IF_CFG ? 1 : 0);
1832 docg3->reliable = reliable_mode;
1833
1834 switch (chip_id) {
1835 case DOC_CHIPID_G3:
1836 mtd->name = kasprintf(GFP_KERNEL, "docg3.%d",
1837 docg3->device_id);
1838 docg3->max_block = 2047;
1839 break;
1840 }
1841 mtd->type = MTD_NANDFLASH;
1842 mtd->flags = MTD_CAP_NANDFLASH;
1843 mtd->size = (docg3->max_block + 1) * DOC_LAYOUT_BLOCK_SIZE;
1844 if (docg3->reliable == 2)
1845 mtd->size /= 2;
1846 mtd->erasesize = DOC_LAYOUT_BLOCK_SIZE * DOC_LAYOUT_NBPLANES;
1847 if (docg3->reliable == 2)
1848 mtd->erasesize /= 2;
1849 mtd->writebufsize = mtd->writesize = DOC_LAYOUT_PAGE_SIZE;
1850 mtd->oobsize = DOC_LAYOUT_OOB_SIZE;
1851 mtd->owner = THIS_MODULE;
1852 mtd->_erase = doc_erase;
1853 mtd->_read = doc_read;
1854 mtd->_write = doc_write;
1855 mtd->_read_oob = doc_read_oob;
1856 mtd->_write_oob = doc_write_oob;
1857 mtd->_block_isbad = doc_block_isbad;
1858 mtd->ecclayout = &docg3_oobinfo;
1859 mtd->ecc_strength = DOC_ECC_BCH_T;
1860}
1861
1862/**
1863 * doc_probe_device - Check if a device is available
1864 * @base: the io space where the device is probed
1865 * @floor: the floor of the probed device
1866 * @dev: the device
1867 * @cascade: the cascade of chips this devices will belong to
1868 *
1869 * Checks whether a device at the specified IO range, and floor is available.
1870 *
1871 * Returns a mtd_info struct if there is a device, ENODEV if none found, ENOMEM
1872 * if a memory allocation failed. If floor 0 is checked, a reset of the ASIC is
1873 * launched.
1874 */
1875static struct mtd_info * __init
1876doc_probe_device(struct docg3_cascade *cascade, int floor, struct device *dev)
1877{
1878 int ret, bbt_nbpages;
1879 u16 chip_id, chip_id_inv;
1880 struct docg3 *docg3;
1881 struct mtd_info *mtd;
1882
1883 ret = -ENOMEM;
1884 docg3 = kzalloc(sizeof(struct docg3), GFP_KERNEL);
1885 if (!docg3)
1886 goto nomem1;
1887 mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
1888 if (!mtd)
1889 goto nomem2;
1890 mtd->priv = docg3;
1891 bbt_nbpages = DIV_ROUND_UP(docg3->max_block + 1,
1892 8 * DOC_LAYOUT_PAGE_SIZE);
1893 docg3->bbt = kzalloc(bbt_nbpages * DOC_LAYOUT_PAGE_SIZE, GFP_KERNEL);
1894 if (!docg3->bbt)
1895 goto nomem3;
1896
1897 docg3->dev = dev;
1898 docg3->device_id = floor;
1899 docg3->cascade = cascade;
1900 doc_set_device_id(docg3, docg3->device_id);
1901 if (!floor)
1902 doc_set_asic_mode(docg3, DOC_ASICMODE_RESET);
1903 doc_set_asic_mode(docg3, DOC_ASICMODE_NORMAL);
1904
1905 chip_id = doc_register_readw(docg3, DOC_CHIPID);
1906 chip_id_inv = doc_register_readw(docg3, DOC_CHIPID_INV);
1907
1908 ret = 0;
1909 if (chip_id != (u16)(~chip_id_inv)) {
1910 goto nomem3;
1911 }
1912
1913 switch (chip_id) {
1914 case DOC_CHIPID_G3:
1915 doc_info("Found a G3 DiskOnChip at addr %p, floor %d\n",
1916 docg3->cascade->base, floor);
1917 break;
1918 default:
1919 doc_err("Chip id %04x is not a DiskOnChip G3 chip\n", chip_id);
1920 goto nomem3;
1921 }
1922
1923 doc_set_driver_info(chip_id, mtd);
1924
1925 doc_hamming_ecc_init(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ);
1926 doc_reload_bbt(docg3);
1927 return mtd;
1928
1929nomem3:
1930 kfree(mtd);
1931nomem2:
1932 kfree(docg3);
1933nomem1:
1934 return ERR_PTR(ret);
1935}
1936
1937/**
1938 * doc_release_device - Release a docg3 floor
1939 * @mtd: the device
1940 */
1941static void doc_release_device(struct mtd_info *mtd)
1942{
1943 struct docg3 *docg3 = mtd->priv;
1944
1945 mtd_device_unregister(mtd);
1946 kfree(docg3->bbt);
1947 kfree(docg3);
1948 kfree(mtd->name);
1949 kfree(mtd);
1950}
1951
1952/**
1953 * docg3_resume - Awakens docg3 floor
1954 * @pdev: platfrom device
1955 *
1956 * Returns 0 (always successful)
1957 */
1958static int docg3_resume(struct platform_device *pdev)
1959{
1960 int i;
1961 struct docg3_cascade *cascade;
1962 struct mtd_info **docg3_floors, *mtd;
1963 struct docg3 *docg3;
1964
1965 cascade = platform_get_drvdata(pdev);
1966 docg3_floors = cascade->floors;
1967 mtd = docg3_floors[0];
1968 docg3 = mtd->priv;
1969
1970 doc_dbg("docg3_resume()\n");
1971 for (i = 0; i < 12; i++)
1972 doc_readb(docg3, DOC_IOSPACE_IPL);
1973 return 0;
1974}
1975
1976/**
1977 * docg3_suspend - Put in low power mode the docg3 floor
1978 * @pdev: platform device
1979 * @state: power state
1980 *
1981 * Shuts off most of docg3 circuitery to lower power consumption.
1982 *
1983 * Returns 0 if suspend succeeded, -EIO if chip refused suspend
1984 */
1985static int docg3_suspend(struct platform_device *pdev, pm_message_t state)
1986{
1987 int floor, i;
1988 struct docg3_cascade *cascade;
1989 struct mtd_info **docg3_floors, *mtd;
1990 struct docg3 *docg3;
1991 u8 ctrl, pwr_down;
1992
1993 cascade = platform_get_drvdata(pdev);
1994 docg3_floors = cascade->floors;
1995 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
1996 mtd = docg3_floors[floor];
1997 if (!mtd)
1998 continue;
1999 docg3 = mtd->priv;
2000
2001 doc_writeb(docg3, floor, DOC_DEVICESELECT);
2002 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
2003 ctrl &= ~DOC_CTRL_VIOLATION & ~DOC_CTRL_CE;
2004 doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
2005
2006 for (i = 0; i < 10; i++) {
2007 usleep_range(3000, 4000);
2008 pwr_down = doc_register_readb(docg3, DOC_POWERMODE);
2009 if (pwr_down & DOC_POWERDOWN_READY)
2010 break;
2011 }
2012 if (pwr_down & DOC_POWERDOWN_READY) {
2013 doc_dbg("docg3_suspend(): floor %d powerdown ok\n",
2014 floor);
2015 } else {
2016 doc_err("docg3_suspend(): floor %d powerdown failed\n",
2017 floor);
2018 return -EIO;
2019 }
2020 }
2021
2022 mtd = docg3_floors[0];
2023 docg3 = mtd->priv;
2024 doc_set_asic_mode(docg3, DOC_ASICMODE_POWERDOWN);
2025 return 0;
2026}
2027
2028/**
2029 * doc_probe - Probe the IO space for a DiskOnChip G3 chip
2030 * @pdev: platform device
2031 *
2032 * Probes for a G3 chip at the specified IO space in the platform data
2033 * ressources. The floor 0 must be available.
2034 *
2035 * Returns 0 on success, -ENOMEM, -ENXIO on error
2036 */
2037static int __init docg3_probe(struct platform_device *pdev)
2038{
2039 struct device *dev = &pdev->dev;
2040 struct mtd_info *mtd;
2041 struct resource *ress;
2042 void __iomem *base;
2043 int ret, floor, found = 0;
2044 struct docg3_cascade *cascade;
2045
2046 ret = -ENXIO;
2047 ress = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2048 if (!ress) {
2049 dev_err(dev, "No I/O memory resource defined\n");
2050 return ret;
2051 }
2052 base = devm_ioremap(dev, ress->start, DOC_IOSPACE_SIZE);
2053
2054 ret = -ENOMEM;
2055 cascade = devm_kzalloc(dev, sizeof(*cascade) * DOC_MAX_NBFLOORS,
2056 GFP_KERNEL);
2057 if (!cascade)
2058 return ret;
2059 cascade->base = base;
2060 mutex_init(&cascade->lock);
2061 cascade->bch = init_bch(DOC_ECC_BCH_M, DOC_ECC_BCH_T,
2062 DOC_ECC_BCH_PRIMPOLY);
2063 if (!cascade->bch)
2064 return ret;
2065
2066 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
2067 mtd = doc_probe_device(cascade, floor, dev);
2068 if (IS_ERR(mtd)) {
2069 ret = PTR_ERR(mtd);
2070 goto err_probe;
2071 }
2072 if (!mtd) {
2073 if (floor == 0)
2074 goto notfound;
2075 else
2076 continue;
2077 }
2078 cascade->floors[floor] = mtd;
2079 ret = mtd_device_parse_register(mtd, part_probes, NULL, NULL,
2080 0);
2081 if (ret)
2082 goto err_probe;
2083 found++;
2084 }
2085
2086 ret = doc_register_sysfs(pdev, cascade);
2087 if (ret)
2088 goto err_probe;
2089 if (!found)
2090 goto notfound;
2091
2092 platform_set_drvdata(pdev, cascade);
2093 doc_dbg_register(cascade->floors[0]->priv);
2094 return 0;
2095
2096notfound:
2097 ret = -ENODEV;
2098 dev_info(dev, "No supported DiskOnChip found\n");
2099err_probe:
2100 free_bch(cascade->bch);
2101 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
2102 if (cascade->floors[floor])
2103 doc_release_device(cascade->floors[floor]);
2104 return ret;
2105}
2106
2107/**
2108 * docg3_release - Release the driver
2109 * @pdev: the platform device
2110 *
2111 * Returns 0
2112 */
2113static int __exit docg3_release(struct platform_device *pdev)
2114{
2115 struct docg3_cascade *cascade = platform_get_drvdata(pdev);
2116 struct docg3 *docg3 = cascade->floors[0]->priv;
2117 int floor;
2118
2119 doc_unregister_sysfs(pdev, cascade);
2120 doc_dbg_unregister(docg3);
2121 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
2122 if (cascade->floors[floor])
2123 doc_release_device(cascade->floors[floor]);
2124
2125 free_bch(docg3->cascade->bch);
2126 return 0;
2127}
2128
2129static struct platform_driver g3_driver = {
2130 .driver = {
2131 .name = "docg3",
2132 .owner = THIS_MODULE,
2133 },
2134 .suspend = docg3_suspend,
2135 .resume = docg3_resume,
2136 .remove = __exit_p(docg3_release),
2137};
2138
2139module_platform_driver_probe(g3_driver, docg3_probe);
2140
2141MODULE_LICENSE("GPL");
2142MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
2143MODULE_DESCRIPTION("MTD driver for DiskOnChip G3");