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