mtd: docg3 refactor cascade floors structure
[deliverable/linux.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/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
64 static unsigned int reliable_mode;
65 module_param(reliable_mode, uint, 0);
66 MODULE_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 */
76 static 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
83 static 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
91 static 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
99 static 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
105 static 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
111 static inline void doc_flash_command(struct docg3 *docg3, u8 cmd)
112 {
113 doc_writeb(docg3, cmd, DOC_FLASHCOMMAND);
114 }
115
116 static inline void doc_flash_sequence(struct docg3 *docg3, u8 seq)
117 {
118 doc_writeb(docg3, seq, DOC_FLASHSEQUENCE);
119 }
120
121 static inline void doc_flash_address(struct docg3 *docg3, u8 addr)
122 {
123 doc_writeb(docg3, addr, DOC_FLASHADDRESS);
124 }
125
126 static char const *part_probes[] = { "cmdlinepart", "saftlpart", NULL };
127
128 static 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
138 static 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 */
159 static 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
168 static 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
176 static 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
184 static 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
199 static 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 */
222 static 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 & 0x3;
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 */
267 static 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 */
308 static 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 */
342 static 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 */
364 static 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 occured, -EIO else.
387 */
388 static 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 */
409 static 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 */
424 static 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 */
447 static 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
481 out:
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 */
496 static 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
533 out:
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 */
548 static 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 */
569 static 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 */
587 static 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 */
601 static 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 */
634 static 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);
654 out:
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 occured.
685 */
686 static 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;
723 err:
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 *
736 */
737 static int doc_read_page_getbytes(struct docg3 *docg3, int len, u_char *buf,
738 int first)
739 {
740 doc_read_data_area(docg3, buf, len, first);
741 doc_delay(docg3, 2);
742 return len;
743 }
744
745 /**
746 * doc_write_page_putbytes - Writes bytes into a prepared page
747 * @docg3: the device
748 * @len: the number of bytes to be written
749 * @buf: the buffer of input bytes
750 *
751 */
752 static void doc_write_page_putbytes(struct docg3 *docg3, int len,
753 const u_char *buf)
754 {
755 doc_write_data_area(docg3, buf, len);
756 doc_delay(docg3, 2);
757 }
758
759 /**
760 * doc_get_bch_hw_ecc - Get hardware calculated BCH ECC
761 * @docg3: the device
762 * @hwecc: the array of 7 integers where the hardware ecc will be stored
763 */
764 static void doc_get_bch_hw_ecc(struct docg3 *docg3, u8 *hwecc)
765 {
766 int i;
767
768 for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
769 hwecc[i] = doc_register_readb(docg3, DOC_BCH_HW_ECC(i));
770 }
771
772 /**
773 * doc_page_finish - Ends reading/writing of a flash page
774 * @docg3: the device
775 */
776 static void doc_page_finish(struct docg3 *docg3)
777 {
778 doc_writeb(docg3, 0, DOC_DATAEND);
779 doc_delay(docg3, 2);
780 }
781
782 /**
783 * doc_read_page_finish - Ends reading of a flash page
784 * @docg3: the device
785 *
786 * As a side effect, resets the chip selector to 0. This ensures that after each
787 * read operation, the floor 0 is selected. Therefore, if the systems halts, the
788 * reboot will boot on floor 0, where the IPL is.
789 */
790 static void doc_read_page_finish(struct docg3 *docg3)
791 {
792 doc_page_finish(docg3);
793 doc_set_device_id(docg3, 0);
794 }
795
796 /**
797 * calc_block_sector - Calculate blocks, pages and ofs.
798
799 * @from: offset in flash
800 * @block0: first plane block index calculated
801 * @block1: second plane block index calculated
802 * @page: page calculated
803 * @ofs: offset in page
804 * @reliable: 0 if docg3 in normal mode, 1 if docg3 in fast mode, 2 if docg3 in
805 * reliable mode.
806 *
807 * The calculation is based on the reliable/normal mode. In normal mode, the 64
808 * pages of a block are available. In reliable mode, as pages 2*n and 2*n+1 are
809 * clones, only 32 pages per block are available.
810 */
811 static void calc_block_sector(loff_t from, int *block0, int *block1, int *page,
812 int *ofs, int reliable)
813 {
814 uint sector, pages_biblock;
815
816 pages_biblock = DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_NBPLANES;
817 if (reliable == 1 || reliable == 2)
818 pages_biblock /= 2;
819
820 sector = from / DOC_LAYOUT_PAGE_SIZE;
821 *block0 = sector / pages_biblock * DOC_LAYOUT_NBPLANES;
822 *block1 = *block0 + 1;
823 *page = sector % pages_biblock;
824 *page /= DOC_LAYOUT_NBPLANES;
825 if (reliable == 1 || reliable == 2)
826 *page *= 2;
827 if (sector % 2)
828 *ofs = DOC_LAYOUT_PAGE_OOB_SIZE;
829 else
830 *ofs = 0;
831 }
832
833 /**
834 * doc_read_oob - Read out of band bytes from flash
835 * @mtd: the device
836 * @from: the offset from first block and first page, in bytes, aligned on page
837 * size
838 * @ops: the mtd oob structure
839 *
840 * Reads flash memory OOB area of pages.
841 *
842 * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
843 */
844 static int doc_read_oob(struct mtd_info *mtd, loff_t from,
845 struct mtd_oob_ops *ops)
846 {
847 struct docg3 *docg3 = mtd->priv;
848 int block0, block1, page, ret, skip, ofs = 0;
849 u8 *oobbuf = ops->oobbuf;
850 u8 *buf = ops->datbuf;
851 size_t len, ooblen, nbdata, nboob;
852 u8 hwecc[DOC_ECC_BCH_SIZE], eccconf1;
853
854 if (buf)
855 len = ops->len;
856 else
857 len = 0;
858 if (oobbuf)
859 ooblen = ops->ooblen;
860 else
861 ooblen = 0;
862
863 if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
864 oobbuf += ops->ooboffs;
865
866 doc_dbg("doc_read_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
867 from, ops->mode, buf, len, oobbuf, ooblen);
868 if (ooblen % DOC_LAYOUT_OOB_SIZE)
869 return -EINVAL;
870
871 if (from + len > mtd->size)
872 return -EINVAL;
873
874 ops->oobretlen = 0;
875 ops->retlen = 0;
876 ret = 0;
877 skip = from % DOC_LAYOUT_PAGE_SIZE;
878 while (!ret && (len > 0 || ooblen > 0)) {
879 calc_block_sector(from - skip, &block0, &block1, &page, &ofs,
880 docg3->reliable);
881 nbdata = min_t(size_t, len, DOC_LAYOUT_PAGE_SIZE - skip);
882 nboob = min_t(size_t, ooblen, (size_t)DOC_LAYOUT_OOB_SIZE);
883 ret = doc_read_page_prepare(docg3, block0, block1, page, ofs);
884 if (ret < 0)
885 goto err;
886 ret = doc_read_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
887 if (ret < 0)
888 goto err_in_read;
889 ret = doc_read_page_getbytes(docg3, skip, NULL, 1);
890 if (ret < skip)
891 goto err_in_read;
892 ret = doc_read_page_getbytes(docg3, nbdata, buf, 0);
893 if (ret < nbdata)
894 goto err_in_read;
895 doc_read_page_getbytes(docg3,
896 DOC_LAYOUT_PAGE_SIZE - nbdata - skip,
897 NULL, 0);
898 ret = doc_read_page_getbytes(docg3, nboob, oobbuf, 0);
899 if (ret < nboob)
900 goto err_in_read;
901 doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE - nboob,
902 NULL, 0);
903
904 doc_get_bch_hw_ecc(docg3, hwecc);
905 eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1);
906
907 if (nboob >= DOC_LAYOUT_OOB_SIZE) {
908 doc_dbg("OOB - INFO: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
909 oobbuf[0], oobbuf[1], oobbuf[2], oobbuf[3],
910 oobbuf[4], oobbuf[5], oobbuf[6]);
911 doc_dbg("OOB - HAMMING: %02x\n", oobbuf[7]);
912 doc_dbg("OOB - BCH_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
913 oobbuf[8], oobbuf[9], oobbuf[10], oobbuf[11],
914 oobbuf[12], oobbuf[13], oobbuf[14]);
915 doc_dbg("OOB - UNUSED: %02x\n", oobbuf[15]);
916 }
917 doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1);
918 doc_dbg("ECC HW_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
919 hwecc[0], hwecc[1], hwecc[2], hwecc[3], hwecc[4],
920 hwecc[5], hwecc[6]);
921
922 ret = -EIO;
923 if (is_prot_seq_error(docg3))
924 goto err_in_read;
925 ret = 0;
926 if ((block0 >= DOC_LAYOUT_BLOCK_FIRST_DATA) &&
927 (eccconf1 & DOC_ECCCONF1_BCH_SYNDROM_ERR) &&
928 (eccconf1 & DOC_ECCCONF1_PAGE_IS_WRITTEN) &&
929 (ops->mode != MTD_OPS_RAW) &&
930 (nbdata == DOC_LAYOUT_PAGE_SIZE)) {
931 ret = doc_ecc_bch_fix_data(docg3, buf, hwecc);
932 if (ret < 0) {
933 mtd->ecc_stats.failed++;
934 ret = -EBADMSG;
935 }
936 if (ret > 0) {
937 mtd->ecc_stats.corrected += ret;
938 ret = -EUCLEAN;
939 }
940 }
941
942 doc_read_page_finish(docg3);
943 ops->retlen += nbdata;
944 ops->oobretlen += nboob;
945 buf += nbdata;
946 oobbuf += nboob;
947 len -= nbdata;
948 ooblen -= nboob;
949 from += DOC_LAYOUT_PAGE_SIZE;
950 skip = 0;
951 }
952
953 return ret;
954 err_in_read:
955 doc_read_page_finish(docg3);
956 err:
957 return ret;
958 }
959
960 /**
961 * doc_read - Read bytes from flash
962 * @mtd: the device
963 * @from: the offset from first block and first page, in bytes, aligned on page
964 * size
965 * @len: the number of bytes to read (must be a multiple of 4)
966 * @retlen: the number of bytes actually read
967 * @buf: the filled in buffer
968 *
969 * Reads flash memory pages. This function does not read the OOB chunk, but only
970 * the page data.
971 *
972 * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
973 */
974 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
975 size_t *retlen, u_char *buf)
976 {
977 struct mtd_oob_ops ops;
978 size_t ret;
979
980 memset(&ops, 0, sizeof(ops));
981 ops.datbuf = buf;
982 ops.len = len;
983 ops.mode = MTD_OPS_AUTO_OOB;
984
985 ret = doc_read_oob(mtd, from, &ops);
986 *retlen = ops.retlen;
987 return ret;
988 }
989
990 static int doc_reload_bbt(struct docg3 *docg3)
991 {
992 int block = DOC_LAYOUT_BLOCK_BBT;
993 int ret = 0, nbpages, page;
994 u_char *buf = docg3->bbt;
995
996 nbpages = DIV_ROUND_UP(docg3->max_block + 1, 8 * DOC_LAYOUT_PAGE_SIZE);
997 for (page = 0; !ret && (page < nbpages); page++) {
998 ret = doc_read_page_prepare(docg3, block, block + 1,
999 page + DOC_LAYOUT_PAGE_BBT, 0);
1000 if (!ret)
1001 ret = doc_read_page_ecc_init(docg3,
1002 DOC_LAYOUT_PAGE_SIZE);
1003 if (!ret)
1004 doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE,
1005 buf, 1);
1006 buf += DOC_LAYOUT_PAGE_SIZE;
1007 }
1008 doc_read_page_finish(docg3);
1009 return ret;
1010 }
1011
1012 /**
1013 * doc_block_isbad - Checks whether a block is good or not
1014 * @mtd: the device
1015 * @from: the offset to find the correct block
1016 *
1017 * Returns 1 if block is bad, 0 if block is good
1018 */
1019 static int doc_block_isbad(struct mtd_info *mtd, loff_t from)
1020 {
1021 struct docg3 *docg3 = mtd->priv;
1022 int block0, block1, page, ofs, is_good;
1023
1024 calc_block_sector(from, &block0, &block1, &page, &ofs,
1025 docg3->reliable);
1026 doc_dbg("doc_block_isbad(from=%lld) => block=(%d,%d), page=%d, ofs=%d\n",
1027 from, block0, block1, page, ofs);
1028
1029 if (block0 < DOC_LAYOUT_BLOCK_FIRST_DATA)
1030 return 0;
1031 if (block1 > docg3->max_block)
1032 return -EINVAL;
1033
1034 is_good = docg3->bbt[block0 >> 3] & (1 << (block0 & 0x7));
1035 return !is_good;
1036 }
1037
1038 #if 0
1039 /**
1040 * doc_get_erase_count - Get block erase count
1041 * @docg3: the device
1042 * @from: the offset in which the block is.
1043 *
1044 * Get the number of times a block was erased. The number is the maximum of
1045 * erase times between first and second plane (which should be equal normally).
1046 *
1047 * Returns The number of erases, or -EINVAL or -EIO on error.
1048 */
1049 static int doc_get_erase_count(struct docg3 *docg3, loff_t from)
1050 {
1051 u8 buf[DOC_LAYOUT_WEAR_SIZE];
1052 int ret, plane1_erase_count, plane2_erase_count;
1053 int block0, block1, page, ofs;
1054
1055 doc_dbg("doc_get_erase_count(from=%lld, buf=%p)\n", from, buf);
1056 if (from % DOC_LAYOUT_PAGE_SIZE)
1057 return -EINVAL;
1058 calc_block_sector(from, &block0, &block1, &page, &ofs, docg3->reliable);
1059 if (block1 > docg3->max_block)
1060 return -EINVAL;
1061
1062 ret = doc_reset_seq(docg3);
1063 if (!ret)
1064 ret = doc_read_page_prepare(docg3, block0, block1, page,
1065 ofs + DOC_LAYOUT_WEAR_OFFSET);
1066 if (!ret)
1067 ret = doc_read_page_getbytes(docg3, DOC_LAYOUT_WEAR_SIZE,
1068 buf, 1);
1069 doc_read_page_finish(docg3);
1070
1071 if (ret || (buf[0] != DOC_ERASE_MARK) || (buf[2] != DOC_ERASE_MARK))
1072 return -EIO;
1073 plane1_erase_count = (u8)(~buf[1]) | ((u8)(~buf[4]) << 8)
1074 | ((u8)(~buf[5]) << 16);
1075 plane2_erase_count = (u8)(~buf[3]) | ((u8)(~buf[6]) << 8)
1076 | ((u8)(~buf[7]) << 16);
1077
1078 return max(plane1_erase_count, plane2_erase_count);
1079 }
1080 #endif
1081
1082 /**
1083 * doc_get_op_status - get erase/write operation status
1084 * @docg3: the device
1085 *
1086 * Queries the status from the chip, and returns it
1087 *
1088 * Returns the status (bits DOC_PLANES_STATUS_*)
1089 */
1090 static int doc_get_op_status(struct docg3 *docg3)
1091 {
1092 u8 status;
1093
1094 doc_flash_sequence(docg3, DOC_SEQ_PLANES_STATUS);
1095 doc_flash_command(docg3, DOC_CMD_PLANES_STATUS);
1096 doc_delay(docg3, 5);
1097
1098 doc_ecc_disable(docg3);
1099 doc_read_data_area(docg3, &status, 1, 1);
1100 return status;
1101 }
1102
1103 /**
1104 * doc_write_erase_wait_status - wait for write or erase completion
1105 * @docg3: the device
1106 *
1107 * Wait for the chip to be ready again after erase or write operation, and check
1108 * erase/write status.
1109 *
1110 * Returns 0 if erase successfull, -EIO if erase/write issue, -ETIMEOUT if
1111 * timeout
1112 */
1113 static int doc_write_erase_wait_status(struct docg3 *docg3)
1114 {
1115 int i, status, ret = 0;
1116
1117 for (i = 0; !doc_is_ready(docg3) && i < 5; i++)
1118 msleep(20);
1119 if (!doc_is_ready(docg3)) {
1120 doc_dbg("Timeout reached and the chip is still not ready\n");
1121 ret = -EAGAIN;
1122 goto out;
1123 }
1124
1125 status = doc_get_op_status(docg3);
1126 if (status & DOC_PLANES_STATUS_FAIL) {
1127 doc_dbg("Erase/Write failed on (a) plane(s), status = %x\n",
1128 status);
1129 ret = -EIO;
1130 }
1131
1132 out:
1133 doc_page_finish(docg3);
1134 return ret;
1135 }
1136
1137 /**
1138 * doc_erase_block - Erase a couple of blocks
1139 * @docg3: the device
1140 * @block0: the first block to erase (leftmost plane)
1141 * @block1: the second block to erase (rightmost plane)
1142 *
1143 * Erase both blocks, and return operation status
1144 *
1145 * Returns 0 if erase successful, -EIO if erase issue, -ETIMEOUT if chip not
1146 * ready for too long
1147 */
1148 static int doc_erase_block(struct docg3 *docg3, int block0, int block1)
1149 {
1150 int ret, sector;
1151
1152 doc_dbg("doc_erase_block(blocks=(%d,%d))\n", block0, block1);
1153 ret = doc_reset_seq(docg3);
1154 if (ret)
1155 return -EIO;
1156
1157 doc_set_reliable_mode(docg3);
1158 doc_flash_sequence(docg3, DOC_SEQ_ERASE);
1159
1160 sector = block0 << DOC_ADDR_BLOCK_SHIFT;
1161 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
1162 doc_setup_addr_sector(docg3, sector);
1163 sector = block1 << DOC_ADDR_BLOCK_SHIFT;
1164 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
1165 doc_setup_addr_sector(docg3, sector);
1166 doc_delay(docg3, 1);
1167
1168 doc_flash_command(docg3, DOC_CMD_ERASECYCLE2);
1169 doc_delay(docg3, 2);
1170
1171 if (is_prot_seq_error(docg3)) {
1172 doc_err("Erase blocks %d,%d error\n", block0, block1);
1173 return -EIO;
1174 }
1175
1176 return doc_write_erase_wait_status(docg3);
1177 }
1178
1179 /**
1180 * doc_erase - Erase a portion of the chip
1181 * @mtd: the device
1182 * @info: the erase info
1183 *
1184 * Erase a bunch of contiguous blocks, by pairs, as a "mtd" page of 1024 is
1185 * split into 2 pages of 512 bytes on 2 contiguous blocks.
1186 *
1187 * Returns 0 if erase successful, -EINVAL if adressing error, -EIO if erase
1188 * issue
1189 */
1190 static int doc_erase(struct mtd_info *mtd, struct erase_info *info)
1191 {
1192 struct docg3 *docg3 = mtd->priv;
1193 uint64_t len;
1194 int block0, block1, page, ret, ofs = 0;
1195
1196 doc_dbg("doc_erase(from=%lld, len=%lld\n", info->addr, info->len);
1197 doc_set_device_id(docg3, docg3->device_id);
1198
1199 info->state = MTD_ERASE_PENDING;
1200 calc_block_sector(info->addr + info->len, &block0, &block1, &page,
1201 &ofs, docg3->reliable);
1202 ret = -EINVAL;
1203 if (info->addr + info->len > mtd->size || page || ofs)
1204 goto reset_err;
1205
1206 ret = 0;
1207 calc_block_sector(info->addr, &block0, &block1, &page, &ofs,
1208 docg3->reliable);
1209 doc_set_reliable_mode(docg3);
1210 for (len = info->len; !ret && len > 0; len -= mtd->erasesize) {
1211 info->state = MTD_ERASING;
1212 ret = doc_erase_block(docg3, block0, block1);
1213 block0 += 2;
1214 block1 += 2;
1215 }
1216
1217 if (ret)
1218 goto reset_err;
1219
1220 info->state = MTD_ERASE_DONE;
1221 return 0;
1222
1223 reset_err:
1224 info->state = MTD_ERASE_FAILED;
1225 return ret;
1226 }
1227
1228 /**
1229 * doc_write_page - Write a single page to the chip
1230 * @docg3: the device
1231 * @to: the offset from first block and first page, in bytes, aligned on page
1232 * size
1233 * @buf: buffer to get bytes from
1234 * @oob: buffer to get out of band bytes from (can be NULL if no OOB should be
1235 * written)
1236 * @autoecc: if 0, all 16 bytes from OOB are taken, regardless of HW Hamming or
1237 * BCH computations. If 1, only bytes 0-7 and byte 15 are taken,
1238 * remaining ones are filled with hardware Hamming and BCH
1239 * computations. Its value is not meaningfull is oob == NULL.
1240 *
1241 * Write one full page (ie. 1 page split on two planes), of 512 bytes, with the
1242 * OOB data. The OOB ECC is automatically computed by the hardware Hamming and
1243 * BCH generator if autoecc is not null.
1244 *
1245 * Returns 0 if write successful, -EIO if write error, -EAGAIN if timeout
1246 */
1247 static int doc_write_page(struct docg3 *docg3, loff_t to, const u_char *buf,
1248 const u_char *oob, int autoecc)
1249 {
1250 int block0, block1, page, ret, ofs = 0;
1251 u8 hwecc[DOC_ECC_BCH_SIZE], hamming;
1252
1253 doc_dbg("doc_write_page(to=%lld)\n", to);
1254 calc_block_sector(to, &block0, &block1, &page, &ofs, docg3->reliable);
1255
1256 doc_set_device_id(docg3, docg3->device_id);
1257 ret = doc_reset_seq(docg3);
1258 if (ret)
1259 goto err;
1260
1261 /* Program the flash address block and page */
1262 ret = doc_write_seek(docg3, block0, block1, page, ofs);
1263 if (ret)
1264 goto err;
1265
1266 doc_write_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
1267 doc_delay(docg3, 2);
1268 doc_write_page_putbytes(docg3, DOC_LAYOUT_PAGE_SIZE, buf);
1269
1270 if (oob && autoecc) {
1271 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ, oob);
1272 doc_delay(docg3, 2);
1273 oob += DOC_LAYOUT_OOB_UNUSED_OFS;
1274
1275 hamming = doc_register_readb(docg3, DOC_HAMMINGPARITY);
1276 doc_delay(docg3, 2);
1277 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_HAMMING_SZ,
1278 &hamming);
1279 doc_delay(docg3, 2);
1280
1281 doc_get_bch_hw_ecc(docg3, hwecc);
1282 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_BCH_SZ, hwecc);
1283 doc_delay(docg3, 2);
1284
1285 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_UNUSED_SZ, oob);
1286 }
1287 if (oob && !autoecc)
1288 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_SIZE, oob);
1289
1290 doc_delay(docg3, 2);
1291 doc_page_finish(docg3);
1292 doc_delay(docg3, 2);
1293 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE2);
1294 doc_delay(docg3, 2);
1295
1296 /*
1297 * The wait status will perform another doc_page_finish() call, but that
1298 * seems to please the docg3, so leave it.
1299 */
1300 ret = doc_write_erase_wait_status(docg3);
1301 return ret;
1302 err:
1303 doc_read_page_finish(docg3);
1304 return ret;
1305 }
1306
1307 /**
1308 * doc_guess_autoecc - Guess autoecc mode from mbd_oob_ops
1309 * @ops: the oob operations
1310 *
1311 * Returns 0 or 1 if success, -EINVAL if invalid oob mode
1312 */
1313 static int doc_guess_autoecc(struct mtd_oob_ops *ops)
1314 {
1315 int autoecc;
1316
1317 switch (ops->mode) {
1318 case MTD_OPS_PLACE_OOB:
1319 case MTD_OPS_AUTO_OOB:
1320 autoecc = 1;
1321 break;
1322 case MTD_OPS_RAW:
1323 autoecc = 0;
1324 break;
1325 default:
1326 autoecc = -EINVAL;
1327 }
1328 return autoecc;
1329 }
1330
1331 /**
1332 * doc_fill_autooob - Fill a 16 bytes OOB from 8 non-ECC bytes
1333 * @dst: the target 16 bytes OOB buffer
1334 * @oobsrc: the source 8 bytes non-ECC OOB buffer
1335 *
1336 */
1337 static void doc_fill_autooob(u8 *dst, u8 *oobsrc)
1338 {
1339 memcpy(dst, oobsrc, DOC_LAYOUT_OOB_PAGEINFO_SZ);
1340 dst[DOC_LAYOUT_OOB_UNUSED_OFS] = oobsrc[DOC_LAYOUT_OOB_PAGEINFO_SZ];
1341 }
1342
1343 /**
1344 * doc_backup_oob - Backup OOB into docg3 structure
1345 * @docg3: the device
1346 * @to: the page offset in the chip
1347 * @ops: the OOB size and buffer
1348 *
1349 * As the docg3 should write a page with its OOB in one pass, and some userland
1350 * applications do write_oob() to setup the OOB and then write(), store the OOB
1351 * into a temporary storage. This is very dangerous, as 2 concurrent
1352 * applications could store an OOB, and then write their pages (which will
1353 * result into one having its OOB corrupted).
1354 *
1355 * The only reliable way would be for userland to call doc_write_oob() with both
1356 * the page data _and_ the OOB area.
1357 *
1358 * Returns 0 if success, -EINVAL if ops content invalid
1359 */
1360 static int doc_backup_oob(struct docg3 *docg3, loff_t to,
1361 struct mtd_oob_ops *ops)
1362 {
1363 int ooblen = ops->ooblen, autoecc;
1364
1365 if (ooblen != DOC_LAYOUT_OOB_SIZE)
1366 return -EINVAL;
1367 autoecc = doc_guess_autoecc(ops);
1368 if (autoecc < 0)
1369 return autoecc;
1370
1371 docg3->oob_write_ofs = to;
1372 docg3->oob_autoecc = autoecc;
1373 if (ops->mode == MTD_OPS_AUTO_OOB) {
1374 doc_fill_autooob(docg3->oob_write_buf, ops->oobbuf);
1375 ops->oobretlen = 8;
1376 } else {
1377 memcpy(docg3->oob_write_buf, ops->oobbuf, DOC_LAYOUT_OOB_SIZE);
1378 ops->oobretlen = DOC_LAYOUT_OOB_SIZE;
1379 }
1380 return 0;
1381 }
1382
1383 /**
1384 * doc_write_oob - Write out of band bytes to flash
1385 * @mtd: the device
1386 * @ofs: the offset from first block and first page, in bytes, aligned on page
1387 * size
1388 * @ops: the mtd oob structure
1389 *
1390 * Either write OOB data into a temporary buffer, for the subsequent write
1391 * page. The provided OOB should be 16 bytes long. If a data buffer is provided
1392 * as well, issue the page write.
1393 * Or provide data without OOB, and then a all zeroed OOB will be used (ECC will
1394 * still be filled in if asked for).
1395 *
1396 * Returns 0 is successfull, EINVAL if length is not 14 bytes
1397 */
1398 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
1399 struct mtd_oob_ops *ops)
1400 {
1401 struct docg3 *docg3 = mtd->priv;
1402 int block0, block1, page, ret, pofs = 0, autoecc, oobdelta;
1403 u8 *oobbuf = ops->oobbuf;
1404 u8 *buf = ops->datbuf;
1405 size_t len, ooblen;
1406 u8 oob[DOC_LAYOUT_OOB_SIZE];
1407
1408 if (buf)
1409 len = ops->len;
1410 else
1411 len = 0;
1412 if (oobbuf)
1413 ooblen = ops->ooblen;
1414 else
1415 ooblen = 0;
1416
1417 if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
1418 oobbuf += ops->ooboffs;
1419
1420 doc_dbg("doc_write_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
1421 ofs, ops->mode, buf, len, oobbuf, ooblen);
1422 switch (ops->mode) {
1423 case MTD_OPS_PLACE_OOB:
1424 case MTD_OPS_RAW:
1425 oobdelta = mtd->oobsize;
1426 break;
1427 case MTD_OPS_AUTO_OOB:
1428 oobdelta = mtd->ecclayout->oobavail;
1429 break;
1430 default:
1431 oobdelta = 0;
1432 }
1433 if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % oobdelta) ||
1434 (ofs % DOC_LAYOUT_PAGE_SIZE))
1435 return -EINVAL;
1436 if (len && ooblen &&
1437 (len / DOC_LAYOUT_PAGE_SIZE) != (ooblen / oobdelta))
1438 return -EINVAL;
1439 if (ofs + len > mtd->size)
1440 return -EINVAL;
1441
1442 ops->oobretlen = 0;
1443 ops->retlen = 0;
1444 ret = 0;
1445 if (len == 0 && ooblen == 0)
1446 return -EINVAL;
1447 if (len == 0 && ooblen > 0)
1448 return doc_backup_oob(docg3, ofs, ops);
1449
1450 autoecc = doc_guess_autoecc(ops);
1451 if (autoecc < 0)
1452 return autoecc;
1453
1454 while (!ret && len > 0) {
1455 memset(oob, 0, sizeof(oob));
1456 if (ofs == docg3->oob_write_ofs)
1457 memcpy(oob, docg3->oob_write_buf, DOC_LAYOUT_OOB_SIZE);
1458 else if (ooblen > 0 && ops->mode == MTD_OPS_AUTO_OOB)
1459 doc_fill_autooob(oob, oobbuf);
1460 else if (ooblen > 0)
1461 memcpy(oob, oobbuf, DOC_LAYOUT_OOB_SIZE);
1462 ret = doc_write_page(docg3, ofs, buf, oob, autoecc);
1463
1464 ofs += DOC_LAYOUT_PAGE_SIZE;
1465 len -= DOC_LAYOUT_PAGE_SIZE;
1466 buf += DOC_LAYOUT_PAGE_SIZE;
1467 if (ooblen) {
1468 oobbuf += oobdelta;
1469 ooblen -= oobdelta;
1470 ops->oobretlen += oobdelta;
1471 }
1472 ops->retlen += DOC_LAYOUT_PAGE_SIZE;
1473 }
1474 err:
1475 doc_set_device_id(docg3, 0);
1476 return ret;
1477 }
1478
1479 /**
1480 * doc_write - Write a buffer to the chip
1481 * @mtd: the device
1482 * @to: the offset from first block and first page, in bytes, aligned on page
1483 * size
1484 * @len: the number of bytes to write (must be a full page size, ie. 512)
1485 * @retlen: the number of bytes actually written (0 or 512)
1486 * @buf: the buffer to get bytes from
1487 *
1488 * Writes data to the chip.
1489 *
1490 * Returns 0 if write successful, -EIO if write error
1491 */
1492 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
1493 size_t *retlen, const u_char *buf)
1494 {
1495 struct docg3 *docg3 = mtd->priv;
1496 int ret;
1497 struct mtd_oob_ops ops;
1498
1499 doc_dbg("doc_write(to=%lld, len=%zu)\n", to, len);
1500 ops.datbuf = (char *)buf;
1501 ops.len = len;
1502 ops.mode = MTD_OPS_PLACE_OOB;
1503 ops.oobbuf = NULL;
1504 ops.ooblen = 0;
1505 ops.ooboffs = 0;
1506
1507 ret = doc_write_oob(mtd, to, &ops);
1508 *retlen = ops.retlen;
1509 return ret;
1510 }
1511
1512 static struct docg3 *sysfs_dev2docg3(struct device *dev,
1513 struct device_attribute *attr)
1514 {
1515 int floor;
1516 struct platform_device *pdev = to_platform_device(dev);
1517 struct mtd_info **docg3_floors = platform_get_drvdata(pdev);
1518
1519 floor = attr->attr.name[1] - '0';
1520 if (floor < 0 || floor >= DOC_MAX_NBFLOORS)
1521 return NULL;
1522 else
1523 return docg3_floors[floor]->priv;
1524 }
1525
1526 static ssize_t dps0_is_key_locked(struct device *dev,
1527 struct device_attribute *attr, char *buf)
1528 {
1529 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1530 int dps0;
1531
1532 doc_set_device_id(docg3, docg3->device_id);
1533 dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
1534 doc_set_device_id(docg3, 0);
1535
1536 return sprintf(buf, "%d\n", !(dps0 & DOC_DPS_KEY_OK));
1537 }
1538
1539 static ssize_t dps1_is_key_locked(struct device *dev,
1540 struct device_attribute *attr, char *buf)
1541 {
1542 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1543 int dps1;
1544
1545 doc_set_device_id(docg3, docg3->device_id);
1546 dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
1547 doc_set_device_id(docg3, 0);
1548
1549 return sprintf(buf, "%d\n", !(dps1 & DOC_DPS_KEY_OK));
1550 }
1551
1552 static ssize_t dps0_insert_key(struct device *dev,
1553 struct device_attribute *attr,
1554 const char *buf, size_t count)
1555 {
1556 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1557 int i;
1558
1559 if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
1560 return -EINVAL;
1561
1562 doc_set_device_id(docg3, docg3->device_id);
1563 for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
1564 doc_writeb(docg3, buf[i], DOC_DPS0_KEY);
1565 doc_set_device_id(docg3, 0);
1566 return count;
1567 }
1568
1569 static ssize_t dps1_insert_key(struct device *dev,
1570 struct device_attribute *attr,
1571 const char *buf, size_t count)
1572 {
1573 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1574 int i;
1575
1576 if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
1577 return -EINVAL;
1578
1579 doc_set_device_id(docg3, docg3->device_id);
1580 for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
1581 doc_writeb(docg3, buf[i], DOC_DPS1_KEY);
1582 doc_set_device_id(docg3, 0);
1583 return count;
1584 }
1585
1586 #define FLOOR_SYSFS(id) { \
1587 __ATTR(f##id##_dps0_is_keylocked, S_IRUGO, dps0_is_key_locked, NULL), \
1588 __ATTR(f##id##_dps1_is_keylocked, S_IRUGO, dps1_is_key_locked, NULL), \
1589 __ATTR(f##id##_dps0_protection_key, S_IWUGO, NULL, dps0_insert_key), \
1590 __ATTR(f##id##_dps1_protection_key, S_IWUGO, NULL, dps1_insert_key), \
1591 }
1592
1593 static struct device_attribute doc_sys_attrs[DOC_MAX_NBFLOORS][4] = {
1594 FLOOR_SYSFS(0), FLOOR_SYSFS(1), FLOOR_SYSFS(2), FLOOR_SYSFS(3)
1595 };
1596
1597 static int doc_register_sysfs(struct platform_device *pdev,
1598 struct docg3_cascade *cascade)
1599 {
1600 int ret = 0, floor, i = 0;
1601 struct device *dev = &pdev->dev;
1602
1603 for (floor = 0; !ret && floor < DOC_MAX_NBFLOORS &&
1604 cascade->floors[floor]; floor++)
1605 for (i = 0; !ret && i < 4; i++)
1606 ret = device_create_file(dev, &doc_sys_attrs[floor][i]);
1607 if (!ret)
1608 return 0;
1609 do {
1610 while (--i >= 0)
1611 device_remove_file(dev, &doc_sys_attrs[floor][i]);
1612 i = 4;
1613 } while (--floor >= 0);
1614 return ret;
1615 }
1616
1617 static void doc_unregister_sysfs(struct platform_device *pdev,
1618 struct docg3_cascade *cascade)
1619 {
1620 struct device *dev = &pdev->dev;
1621 int floor, i;
1622
1623 for (floor = 0; floor < DOC_MAX_NBFLOORS && cascade->floors[floor];
1624 floor++)
1625 for (i = 0; i < 4; i++)
1626 device_remove_file(dev, &doc_sys_attrs[floor][i]);
1627 }
1628
1629 /*
1630 * Debug sysfs entries
1631 */
1632 static int dbg_flashctrl_show(struct seq_file *s, void *p)
1633 {
1634 struct docg3 *docg3 = (struct docg3 *)s->private;
1635
1636 int pos = 0;
1637 u8 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
1638
1639 pos += seq_printf(s,
1640 "FlashControl : 0x%02x (%s,CE# %s,%s,%s,flash %s)\n",
1641 fctrl,
1642 fctrl & DOC_CTRL_VIOLATION ? "protocol violation" : "-",
1643 fctrl & DOC_CTRL_CE ? "active" : "inactive",
1644 fctrl & DOC_CTRL_PROTECTION_ERROR ? "protection error" : "-",
1645 fctrl & DOC_CTRL_SEQUENCE_ERROR ? "sequence error" : "-",
1646 fctrl & DOC_CTRL_FLASHREADY ? "ready" : "not ready");
1647 return pos;
1648 }
1649 DEBUGFS_RO_ATTR(flashcontrol, dbg_flashctrl_show);
1650
1651 static int dbg_asicmode_show(struct seq_file *s, void *p)
1652 {
1653 struct docg3 *docg3 = (struct docg3 *)s->private;
1654
1655 int pos = 0;
1656 int pctrl = doc_register_readb(docg3, DOC_ASICMODE);
1657 int mode = pctrl & 0x03;
1658
1659 pos += seq_printf(s,
1660 "%04x : RAM_WE=%d,RSTIN_RESET=%d,BDETCT_RESET=%d,WRITE_ENABLE=%d,POWERDOWN=%d,MODE=%d%d (",
1661 pctrl,
1662 pctrl & DOC_ASICMODE_RAM_WE ? 1 : 0,
1663 pctrl & DOC_ASICMODE_RSTIN_RESET ? 1 : 0,
1664 pctrl & DOC_ASICMODE_BDETCT_RESET ? 1 : 0,
1665 pctrl & DOC_ASICMODE_MDWREN ? 1 : 0,
1666 pctrl & DOC_ASICMODE_POWERDOWN ? 1 : 0,
1667 mode >> 1, mode & 0x1);
1668
1669 switch (mode) {
1670 case DOC_ASICMODE_RESET:
1671 pos += seq_printf(s, "reset");
1672 break;
1673 case DOC_ASICMODE_NORMAL:
1674 pos += seq_printf(s, "normal");
1675 break;
1676 case DOC_ASICMODE_POWERDOWN:
1677 pos += seq_printf(s, "powerdown");
1678 break;
1679 }
1680 pos += seq_printf(s, ")\n");
1681 return pos;
1682 }
1683 DEBUGFS_RO_ATTR(asic_mode, dbg_asicmode_show);
1684
1685 static int dbg_device_id_show(struct seq_file *s, void *p)
1686 {
1687 struct docg3 *docg3 = (struct docg3 *)s->private;
1688 int pos = 0;
1689 int id = doc_register_readb(docg3, DOC_DEVICESELECT);
1690
1691 pos += seq_printf(s, "DeviceId = %d\n", id);
1692 return pos;
1693 }
1694 DEBUGFS_RO_ATTR(device_id, dbg_device_id_show);
1695
1696 static int dbg_protection_show(struct seq_file *s, void *p)
1697 {
1698 struct docg3 *docg3 = (struct docg3 *)s->private;
1699 int pos = 0;
1700 int protect, dps0, dps0_low, dps0_high, dps1, dps1_low, dps1_high;
1701
1702 protect = doc_register_readb(docg3, DOC_PROTECTION);
1703 dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
1704 dps0_low = doc_register_readw(docg3, DOC_DPS0_ADDRLOW);
1705 dps0_high = doc_register_readw(docg3, DOC_DPS0_ADDRHIGH);
1706 dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
1707 dps1_low = doc_register_readw(docg3, DOC_DPS1_ADDRLOW);
1708 dps1_high = doc_register_readw(docg3, DOC_DPS1_ADDRHIGH);
1709
1710 pos += seq_printf(s, "Protection = 0x%02x (",
1711 protect);
1712 if (protect & DOC_PROTECT_FOUNDRY_OTP_LOCK)
1713 pos += seq_printf(s, "FOUNDRY_OTP_LOCK,");
1714 if (protect & DOC_PROTECT_CUSTOMER_OTP_LOCK)
1715 pos += seq_printf(s, "CUSTOMER_OTP_LOCK,");
1716 if (protect & DOC_PROTECT_LOCK_INPUT)
1717 pos += seq_printf(s, "LOCK_INPUT,");
1718 if (protect & DOC_PROTECT_STICKY_LOCK)
1719 pos += seq_printf(s, "STICKY_LOCK,");
1720 if (protect & DOC_PROTECT_PROTECTION_ENABLED)
1721 pos += seq_printf(s, "PROTECTION ON,");
1722 if (protect & DOC_PROTECT_IPL_DOWNLOAD_LOCK)
1723 pos += seq_printf(s, "IPL_DOWNLOAD_LOCK,");
1724 if (protect & DOC_PROTECT_PROTECTION_ERROR)
1725 pos += seq_printf(s, "PROTECT_ERR,");
1726 else
1727 pos += seq_printf(s, "NO_PROTECT_ERR");
1728 pos += seq_printf(s, ")\n");
1729
1730 pos += seq_printf(s, "DPS0 = 0x%02x : "
1731 "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
1732 "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1733 dps0, dps0_low, dps0_high,
1734 !!(dps0 & DOC_DPS_OTP_PROTECTED),
1735 !!(dps0 & DOC_DPS_READ_PROTECTED),
1736 !!(dps0 & DOC_DPS_WRITE_PROTECTED),
1737 !!(dps0 & DOC_DPS_HW_LOCK_ENABLED),
1738 !!(dps0 & DOC_DPS_KEY_OK));
1739 pos += seq_printf(s, "DPS1 = 0x%02x : "
1740 "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
1741 "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1742 dps1, dps1_low, dps1_high,
1743 !!(dps1 & DOC_DPS_OTP_PROTECTED),
1744 !!(dps1 & DOC_DPS_READ_PROTECTED),
1745 !!(dps1 & DOC_DPS_WRITE_PROTECTED),
1746 !!(dps1 & DOC_DPS_HW_LOCK_ENABLED),
1747 !!(dps1 & DOC_DPS_KEY_OK));
1748 return pos;
1749 }
1750 DEBUGFS_RO_ATTR(protection, dbg_protection_show);
1751
1752 static int __init doc_dbg_register(struct docg3 *docg3)
1753 {
1754 struct dentry *root, *entry;
1755
1756 root = debugfs_create_dir("docg3", NULL);
1757 if (!root)
1758 return -ENOMEM;
1759
1760 entry = debugfs_create_file("flashcontrol", S_IRUSR, root, docg3,
1761 &flashcontrol_fops);
1762 if (entry)
1763 entry = debugfs_create_file("asic_mode", S_IRUSR, root,
1764 docg3, &asic_mode_fops);
1765 if (entry)
1766 entry = debugfs_create_file("device_id", S_IRUSR, root,
1767 docg3, &device_id_fops);
1768 if (entry)
1769 entry = debugfs_create_file("protection", S_IRUSR, root,
1770 docg3, &protection_fops);
1771 if (entry) {
1772 docg3->debugfs_root = root;
1773 return 0;
1774 } else {
1775 debugfs_remove_recursive(root);
1776 return -ENOMEM;
1777 }
1778 }
1779
1780 static void __exit doc_dbg_unregister(struct docg3 *docg3)
1781 {
1782 debugfs_remove_recursive(docg3->debugfs_root);
1783 }
1784
1785 /**
1786 * doc_set_driver_info - Fill the mtd_info structure and docg3 structure
1787 * @chip_id: The chip ID of the supported chip
1788 * @mtd: The structure to fill
1789 */
1790 static void __init doc_set_driver_info(int chip_id, struct mtd_info *mtd)
1791 {
1792 struct docg3 *docg3 = mtd->priv;
1793 int cfg;
1794
1795 cfg = doc_register_readb(docg3, DOC_CONFIGURATION);
1796 docg3->if_cfg = (cfg & DOC_CONF_IF_CFG ? 1 : 0);
1797 docg3->reliable = reliable_mode;
1798
1799 switch (chip_id) {
1800 case DOC_CHIPID_G3:
1801 mtd->name = kasprintf(GFP_KERNEL, "docg3.%d",
1802 docg3->device_id);
1803 docg3->max_block = 2047;
1804 break;
1805 }
1806 mtd->type = MTD_NANDFLASH;
1807 mtd->flags = MTD_CAP_NANDFLASH;
1808 mtd->size = (docg3->max_block + 1) * DOC_LAYOUT_BLOCK_SIZE;
1809 if (docg3->reliable == 2)
1810 mtd->size /= 2;
1811 mtd->erasesize = DOC_LAYOUT_BLOCK_SIZE * DOC_LAYOUT_NBPLANES;
1812 if (docg3->reliable == 2)
1813 mtd->erasesize /= 2;
1814 mtd->writebufsize = mtd->writesize = DOC_LAYOUT_PAGE_SIZE;
1815 mtd->oobsize = DOC_LAYOUT_OOB_SIZE;
1816 mtd->owner = THIS_MODULE;
1817 mtd->_erase = doc_erase;
1818 mtd->_read = doc_read;
1819 mtd->_write = doc_write;
1820 mtd->_read_oob = doc_read_oob;
1821 mtd->_write_oob = doc_write_oob;
1822 mtd->_block_isbad = doc_block_isbad;
1823 mtd->ecclayout = &docg3_oobinfo;
1824 mtd->ecc_strength = DOC_ECC_BCH_T;
1825 }
1826
1827 /**
1828 * doc_probe_device - Check if a device is available
1829 * @base: the io space where the device is probed
1830 * @floor: the floor of the probed device
1831 * @dev: the device
1832 * @cascade: the cascade of chips this devices will belong to
1833 *
1834 * Checks whether a device at the specified IO range, and floor is available.
1835 *
1836 * Returns a mtd_info struct if there is a device, ENODEV if none found, ENOMEM
1837 * if a memory allocation failed. If floor 0 is checked, a reset of the ASIC is
1838 * launched.
1839 */
1840 static struct mtd_info * __init
1841 doc_probe_device(struct docg3_cascade *cascade, int floor, struct device *dev)
1842 {
1843 int ret, bbt_nbpages;
1844 u16 chip_id, chip_id_inv;
1845 struct docg3 *docg3;
1846 struct mtd_info *mtd;
1847
1848 ret = -ENOMEM;
1849 docg3 = kzalloc(sizeof(struct docg3), GFP_KERNEL);
1850 if (!docg3)
1851 goto nomem1;
1852 mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
1853 if (!mtd)
1854 goto nomem2;
1855 mtd->priv = docg3;
1856 bbt_nbpages = DIV_ROUND_UP(docg3->max_block + 1,
1857 8 * DOC_LAYOUT_PAGE_SIZE);
1858 docg3->bbt = kzalloc(bbt_nbpages * DOC_LAYOUT_PAGE_SIZE, GFP_KERNEL);
1859 if (!docg3->bbt)
1860 goto nomem3;
1861
1862 docg3->dev = dev;
1863 docg3->device_id = floor;
1864 docg3->cascade = cascade;
1865 doc_set_device_id(docg3, docg3->device_id);
1866 if (!floor)
1867 doc_set_asic_mode(docg3, DOC_ASICMODE_RESET);
1868 doc_set_asic_mode(docg3, DOC_ASICMODE_NORMAL);
1869
1870 chip_id = doc_register_readw(docg3, DOC_CHIPID);
1871 chip_id_inv = doc_register_readw(docg3, DOC_CHIPID_INV);
1872
1873 ret = 0;
1874 if (chip_id != (u16)(~chip_id_inv)) {
1875 goto nomem3;
1876 }
1877
1878 switch (chip_id) {
1879 case DOC_CHIPID_G3:
1880 doc_info("Found a G3 DiskOnChip at addr %p, floor %d\n",
1881 docg3->cascade->base, floor);
1882 break;
1883 default:
1884 doc_err("Chip id %04x is not a DiskOnChip G3 chip\n", chip_id);
1885 goto nomem3;
1886 }
1887
1888 doc_set_driver_info(chip_id, mtd);
1889
1890 doc_hamming_ecc_init(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ);
1891 doc_reload_bbt(docg3);
1892 return mtd;
1893
1894 nomem3:
1895 kfree(mtd);
1896 nomem2:
1897 kfree(docg3);
1898 nomem1:
1899 return ERR_PTR(ret);
1900 }
1901
1902 /**
1903 * doc_release_device - Release a docg3 floor
1904 * @mtd: the device
1905 */
1906 static void doc_release_device(struct mtd_info *mtd)
1907 {
1908 struct docg3 *docg3 = mtd->priv;
1909
1910 mtd_device_unregister(mtd);
1911 kfree(docg3->bbt);
1912 kfree(docg3);
1913 kfree(mtd->name);
1914 kfree(mtd);
1915 }
1916
1917 /**
1918 * docg3_resume - Awakens docg3 floor
1919 * @pdev: platfrom device
1920 *
1921 * Returns 0 (always successfull)
1922 */
1923 static int docg3_resume(struct platform_device *pdev)
1924 {
1925 int i;
1926 struct docg3_cascade *cascade;
1927 struct mtd_info **docg3_floors, *mtd;
1928 struct docg3 *docg3;
1929
1930 cascade = platform_get_drvdata(pdev);
1931 docg3_floors = cascade->floors;
1932 mtd = docg3_floors[0];
1933 docg3 = mtd->priv;
1934
1935 doc_dbg("docg3_resume()\n");
1936 for (i = 0; i < 12; i++)
1937 doc_readb(docg3, DOC_IOSPACE_IPL);
1938 return 0;
1939 }
1940
1941 /**
1942 * docg3_suspend - Put in low power mode the docg3 floor
1943 * @pdev: platform device
1944 * @state: power state
1945 *
1946 * Shuts off most of docg3 circuitery to lower power consumption.
1947 *
1948 * Returns 0 if suspend succeeded, -EIO if chip refused suspend
1949 */
1950 static int docg3_suspend(struct platform_device *pdev, pm_message_t state)
1951 {
1952 int floor, i;
1953 struct docg3_cascade *cascade;
1954 struct mtd_info **docg3_floors, *mtd;
1955 struct docg3 *docg3;
1956 u8 ctrl, pwr_down;
1957
1958 cascade = platform_get_drvdata(pdev);
1959 docg3_floors = cascade->floors;
1960 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
1961 mtd = docg3_floors[floor];
1962 if (!mtd)
1963 continue;
1964 docg3 = mtd->priv;
1965
1966 doc_writeb(docg3, floor, DOC_DEVICESELECT);
1967 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
1968 ctrl &= ~DOC_CTRL_VIOLATION & ~DOC_CTRL_CE;
1969 doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
1970
1971 for (i = 0; i < 10; i++) {
1972 usleep_range(3000, 4000);
1973 pwr_down = doc_register_readb(docg3, DOC_POWERMODE);
1974 if (pwr_down & DOC_POWERDOWN_READY)
1975 break;
1976 }
1977 if (pwr_down & DOC_POWERDOWN_READY) {
1978 doc_dbg("docg3_suspend(): floor %d powerdown ok\n",
1979 floor);
1980 } else {
1981 doc_err("docg3_suspend(): floor %d powerdown failed\n",
1982 floor);
1983 return -EIO;
1984 }
1985 }
1986
1987 mtd = docg3_floors[0];
1988 docg3 = mtd->priv;
1989 doc_set_asic_mode(docg3, DOC_ASICMODE_POWERDOWN);
1990 return 0;
1991 }
1992
1993 /**
1994 * doc_probe - Probe the IO space for a DiskOnChip G3 chip
1995 * @pdev: platform device
1996 *
1997 * Probes for a G3 chip at the specified IO space in the platform data
1998 * ressources. The floor 0 must be available.
1999 *
2000 * Returns 0 on success, -ENOMEM, -ENXIO on error
2001 */
2002 static int __init docg3_probe(struct platform_device *pdev)
2003 {
2004 struct device *dev = &pdev->dev;
2005 struct mtd_info *mtd;
2006 struct resource *ress;
2007 void __iomem *base;
2008 int ret, floor, found = 0;
2009 struct docg3_cascade *cascade;
2010
2011 ret = -ENXIO;
2012 ress = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2013 if (!ress) {
2014 dev_err(dev, "No I/O memory resource defined\n");
2015 goto noress;
2016 }
2017 base = ioremap(ress->start, DOC_IOSPACE_SIZE);
2018
2019 ret = -ENOMEM;
2020 cascade = kzalloc(sizeof(*cascade) * DOC_MAX_NBFLOORS,
2021 GFP_KERNEL);
2022 if (!cascade)
2023 goto nomem1;
2024 cascade->base = base;
2025 cascade->bch = init_bch(DOC_ECC_BCH_M, DOC_ECC_BCH_T,
2026 DOC_ECC_BCH_PRIMPOLY);
2027 if (!cascade->bch)
2028 goto nomem2;
2029
2030 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
2031 mtd = doc_probe_device(cascade, floor, dev);
2032 if (IS_ERR(mtd)) {
2033 ret = PTR_ERR(mtd);
2034 goto err_probe;
2035 }
2036 if (!mtd) {
2037 if (floor == 0)
2038 goto notfound;
2039 else
2040 continue;
2041 }
2042 cascade->floors[floor] = mtd;
2043 ret = mtd_device_parse_register(mtd, part_probes, NULL, NULL,
2044 0);
2045 if (ret)
2046 goto err_probe;
2047 found++;
2048 }
2049
2050 ret = doc_register_sysfs(pdev, cascade);
2051 if (ret)
2052 goto err_probe;
2053 if (!found)
2054 goto notfound;
2055
2056 platform_set_drvdata(pdev, cascade);
2057 doc_dbg_register(cascade->floors[0]->priv);
2058 return 0;
2059
2060 notfound:
2061 ret = -ENODEV;
2062 dev_info(dev, "No supported DiskOnChip found\n");
2063 err_probe:
2064 kfree(cascade->bch);
2065 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
2066 if (cascade->floors[floor])
2067 doc_release_device(cascade->floors[floor]);
2068 nomem2:
2069 kfree(cascade);
2070 nomem1:
2071 iounmap(base);
2072 noress:
2073 return ret;
2074 }
2075
2076 /**
2077 * docg3_release - Release the driver
2078 * @pdev: the platform device
2079 *
2080 * Returns 0
2081 */
2082 static int __exit docg3_release(struct platform_device *pdev)
2083 {
2084 struct docg3_cascade *cascade = platform_get_drvdata(pdev);
2085 struct docg3 *docg3 = cascade->floors[0]->priv;
2086 void __iomem *base = cascade->base;
2087 int floor;
2088
2089 doc_unregister_sysfs(pdev, cascade);
2090 doc_dbg_unregister(docg3);
2091 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
2092 if (cascade->floors[floor])
2093 doc_release_device(cascade->floors[floor]);
2094
2095 free_bch(docg3->cascade->bch);
2096 kfree(cascade);
2097 iounmap(base);
2098 return 0;
2099 }
2100
2101 static struct platform_driver g3_driver = {
2102 .driver = {
2103 .name = "docg3",
2104 .owner = THIS_MODULE,
2105 },
2106 .suspend = docg3_suspend,
2107 .resume = docg3_resume,
2108 .remove = __exit_p(docg3_release),
2109 };
2110
2111 static int __init docg3_init(void)
2112 {
2113 return platform_driver_probe(&g3_driver, docg3_probe);
2114 }
2115 module_init(docg3_init);
2116
2117
2118 static void __exit docg3_exit(void)
2119 {
2120 platform_driver_unregister(&g3_driver);
2121 }
2122 module_exit(docg3_exit);
2123
2124 MODULE_LICENSE("GPL");
2125 MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
2126 MODULE_DESCRIPTION("MTD driver for DiskOnChip G3");
This page took 0.345377 seconds and 5 git commands to generate.