]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/mtd/devices/docg3.c
mtd: docg3: add OOB layout to mtdinfo
[mv-sheeva.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
33 #include <linux/debugfs.h>
34 #include <linux/seq_file.h>
35
36 #define CREATE_TRACE_POINTS
37 #include "docg3.h"
38
39 /*
40  * This driver handles the DiskOnChip G3 flash memory.
41  *
42  * As no specification is available from M-Systems/Sandisk, this drivers lacks
43  * several functions available on the chip, as :
44  *  - block erase
45  *  - page write
46  *  - IPL write
47  *  - ECC fixing (lack of BCH algorith understanding)
48  *  - powerdown / powerup
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 part is only used for check purpose, no correction is available as
57  * some information is missing. What is known is that :
58  *  - BCH is in GF(2^14)
59  *  - BCH is over data of 520 bytes (512 page + 7 page_info bytes
60  *                                   + 1 hamming byte)
61  *  - BCH can correct up to 4 bits (t = 4)
62  *  - BCH syndroms are calculated in hardware, and checked in hardware as well
63  *
64  */
65
66 /**
67  * struct docg3_oobinfo - DiskOnChip G3 OOB layout
68  * @eccbytes: 8 bytes are used (1 for Hamming ECC, 7 for BCH ECC)
69  * @eccpos: ecc positions (byte 7 is Hamming ECC, byte 8-14 are BCH ECC)
70  * @oobfree: free pageinfo bytes (byte 0 until byte 6, byte 15
71  * @oobavail: 8 available bytes remaining after ECC toll
72  */
73 static struct nand_ecclayout docg3_oobinfo = {
74         .eccbytes = 8,
75         .eccpos = {7, 8, 9, 10, 11, 12, 13, 14},
76         .oobfree = {{0, 7}, {15, 1} },
77         .oobavail = 8,
78 };
79
80 static inline u8 doc_readb(struct docg3 *docg3, u16 reg)
81 {
82         u8 val = readb(docg3->base + reg);
83
84         trace_docg3_io(0, 8, reg, (int)val);
85         return val;
86 }
87
88 static inline u16 doc_readw(struct docg3 *docg3, u16 reg)
89 {
90         u16 val = readw(docg3->base + reg);
91
92         trace_docg3_io(0, 16, reg, (int)val);
93         return val;
94 }
95
96 static inline void doc_writeb(struct docg3 *docg3, u8 val, u16 reg)
97 {
98         writeb(val, docg3->base + reg);
99         trace_docg3_io(1, 8, reg, val);
100 }
101
102 static inline void doc_writew(struct docg3 *docg3, u16 val, u16 reg)
103 {
104         writew(val, docg3->base + reg);
105         trace_docg3_io(1, 16, reg, val);
106 }
107
108 static inline void doc_flash_command(struct docg3 *docg3, u8 cmd)
109 {
110         doc_writeb(docg3, cmd, DOC_FLASHCOMMAND);
111 }
112
113 static inline void doc_flash_sequence(struct docg3 *docg3, u8 seq)
114 {
115         doc_writeb(docg3, seq, DOC_FLASHSEQUENCE);
116 }
117
118 static inline void doc_flash_address(struct docg3 *docg3, u8 addr)
119 {
120         doc_writeb(docg3, addr, DOC_FLASHADDRESS);
121 }
122
123 static char const *part_probes[] = { "cmdlinepart", "saftlpart", NULL };
124
125 static int doc_register_readb(struct docg3 *docg3, int reg)
126 {
127         u8 val;
128
129         doc_writew(docg3, reg, DOC_READADDRESS);
130         val = doc_readb(docg3, reg);
131         doc_vdbg("Read register %04x : %02x\n", reg, val);
132         return val;
133 }
134
135 static int doc_register_readw(struct docg3 *docg3, int reg)
136 {
137         u16 val;
138
139         doc_writew(docg3, reg, DOC_READADDRESS);
140         val = doc_readw(docg3, reg);
141         doc_vdbg("Read register %04x : %04x\n", reg, val);
142         return val;
143 }
144
145 /**
146  * doc_delay - delay docg3 operations
147  * @docg3: the device
148  * @nbNOPs: the number of NOPs to issue
149  *
150  * As no specification is available, the right timings between chip commands are
151  * unknown. The only available piece of information are the observed nops on a
152  * working docg3 chip.
153  * Therefore, doc_delay relies on a busy loop of NOPs, instead of scheduler
154  * friendlier msleep() functions or blocking mdelay().
155  */
156 static void doc_delay(struct docg3 *docg3, int nbNOPs)
157 {
158         int i;
159
160         doc_vdbg("NOP x %d\n", nbNOPs);
161         for (i = 0; i < nbNOPs; i++)
162                 doc_writeb(docg3, 0, DOC_NOP);
163 }
164
165 static int is_prot_seq_error(struct docg3 *docg3)
166 {
167         int ctrl;
168
169         ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
170         return ctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR);
171 }
172
173 static int doc_is_ready(struct docg3 *docg3)
174 {
175         int ctrl;
176
177         ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
178         return ctrl & DOC_CTRL_FLASHREADY;
179 }
180
181 static int doc_wait_ready(struct docg3 *docg3)
182 {
183         int maxWaitCycles = 100;
184
185         do {
186                 doc_delay(docg3, 4);
187                 cpu_relax();
188         } while (!doc_is_ready(docg3) && maxWaitCycles--);
189         doc_delay(docg3, 2);
190         if (maxWaitCycles > 0)
191                 return 0;
192         else
193                 return -EIO;
194 }
195
196 static int doc_reset_seq(struct docg3 *docg3)
197 {
198         int ret;
199
200         doc_writeb(docg3, 0x10, DOC_FLASHCONTROL);
201         doc_flash_sequence(docg3, DOC_SEQ_RESET);
202         doc_flash_command(docg3, DOC_CMD_RESET);
203         doc_delay(docg3, 2);
204         ret = doc_wait_ready(docg3);
205
206         doc_dbg("doc_reset_seq() -> isReady=%s\n", ret ? "false" : "true");
207         return ret;
208 }
209
210 /**
211  * doc_read_data_area - Read data from data area
212  * @docg3: the device
213  * @buf: the buffer to fill in (might be NULL is dummy reads)
214  * @len: the length to read
215  * @first: first time read, DOC_READADDRESS should be set
216  *
217  * Reads bytes from flash data. Handles the single byte / even bytes reads.
218  */
219 static void doc_read_data_area(struct docg3 *docg3, void *buf, int len,
220                                int first)
221 {
222         int i, cdr, len4;
223         u16 data16, *dst16;
224         u8 data8, *dst8;
225
226         doc_dbg("doc_read_data_area(buf=%p, len=%d)\n", buf, len);
227         cdr = len & 0x3;
228         len4 = len - cdr;
229
230         if (first)
231                 doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
232         dst16 = buf;
233         for (i = 0; i < len4; i += 2) {
234                 data16 = doc_readw(docg3, DOC_IOSPACE_DATA);
235                 if (dst16) {
236                         *dst16 = data16;
237                         dst16++;
238                 }
239         }
240
241         if (cdr) {
242                 doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
243                            DOC_READADDRESS);
244                 doc_delay(docg3, 1);
245                 dst8 = (u8 *)dst16;
246                 for (i = 0; i < cdr; i++) {
247                         data8 = doc_readb(docg3, DOC_IOSPACE_DATA);
248                         if (dst8) {
249                                 *dst8 = data8;
250                                 dst8++;
251                         }
252                 }
253         }
254 }
255
256 /**
257  * doc_set_data_mode - Sets the flash to reliable data mode
258  * @docg3: the device
259  *
260  * The reliable data mode is a bit slower than the fast mode, but less errors
261  * occur.  Entering the reliable mode cannot be done without entering the fast
262  * mode first.
263  */
264 static void doc_set_reliable_mode(struct docg3 *docg3)
265 {
266         doc_dbg("doc_set_reliable_mode()\n");
267         doc_flash_sequence(docg3, DOC_SEQ_SET_MODE);
268         doc_flash_command(docg3, DOC_CMD_FAST_MODE);
269         doc_flash_command(docg3, DOC_CMD_RELIABLE_MODE);
270         doc_delay(docg3, 2);
271 }
272
273 /**
274  * doc_set_asic_mode - Set the ASIC mode
275  * @docg3: the device
276  * @mode: the mode
277  *
278  * The ASIC can work in 3 modes :
279  *  - RESET: all registers are zeroed
280  *  - NORMAL: receives and handles commands
281  *  - POWERDOWN: minimal poweruse, flash parts shut off
282  */
283 static void doc_set_asic_mode(struct docg3 *docg3, u8 mode)
284 {
285         int i;
286
287         for (i = 0; i < 12; i++)
288                 doc_readb(docg3, DOC_IOSPACE_IPL);
289
290         mode |= DOC_ASICMODE_MDWREN;
291         doc_dbg("doc_set_asic_mode(%02x)\n", mode);
292         doc_writeb(docg3, mode, DOC_ASICMODE);
293         doc_writeb(docg3, ~mode, DOC_ASICMODECONFIRM);
294         doc_delay(docg3, 1);
295 }
296
297 /**
298  * doc_set_device_id - Sets the devices id for cascaded G3 chips
299  * @docg3: the device
300  * @id: the chip to select (amongst 0, 1, 2, 3)
301  *
302  * There can be 4 cascaded G3 chips. This function selects the one which will
303  * should be the active one.
304  */
305 static void doc_set_device_id(struct docg3 *docg3, int id)
306 {
307         u8 ctrl;
308
309         doc_dbg("doc_set_device_id(%d)\n", id);
310         doc_writeb(docg3, id, DOC_DEVICESELECT);
311         ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
312
313         ctrl &= ~DOC_CTRL_VIOLATION;
314         ctrl |= DOC_CTRL_CE;
315         doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
316 }
317
318 /**
319  * doc_set_extra_page_mode - Change flash page layout
320  * @docg3: the device
321  *
322  * Normally, the flash page is split into the data (512 bytes) and the out of
323  * band data (16 bytes). For each, 4 more bytes can be accessed, where the wear
324  * leveling counters are stored.  To access this last area of 4 bytes, a special
325  * mode must be input to the flash ASIC.
326  *
327  * Returns 0 if no error occured, -EIO else.
328  */
329 static int doc_set_extra_page_mode(struct docg3 *docg3)
330 {
331         int fctrl;
332
333         doc_dbg("doc_set_extra_page_mode()\n");
334         doc_flash_sequence(docg3, DOC_SEQ_PAGE_SIZE_532);
335         doc_flash_command(docg3, DOC_CMD_PAGE_SIZE_532);
336         doc_delay(docg3, 2);
337
338         fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
339         if (fctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR))
340                 return -EIO;
341         else
342                 return 0;
343 }
344
345 /**
346  * doc_seek - Set both flash planes to the specified block, page for reading
347  * @docg3: the device
348  * @block0: the first plane block index
349  * @block1: the second plane block index
350  * @page: the page index within the block
351  * @wear: if true, read will occur on the 4 extra bytes of the wear area
352  * @ofs: offset in page to read
353  *
354  * Programs the flash even and odd planes to the specific block and page.
355  * Alternatively, programs the flash to the wear area of the specified page.
356  */
357 static int doc_read_seek(struct docg3 *docg3, int block0, int block1, int page,
358                          int wear, int ofs)
359 {
360         int sector, ret = 0;
361
362         doc_dbg("doc_seek(blocks=(%d,%d), page=%d, ofs=%d, wear=%d)\n",
363                 block0, block1, page, ofs, wear);
364
365         if (!wear && (ofs < 2 * DOC_LAYOUT_PAGE_SIZE)) {
366                 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
367                 doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
368                 doc_delay(docg3, 2);
369         } else {
370                 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
371                 doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
372                 doc_delay(docg3, 2);
373         }
374
375         doc_set_reliable_mode(docg3);
376         if (wear)
377                 ret = doc_set_extra_page_mode(docg3);
378         if (ret)
379                 goto out;
380
381         sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
382         doc_flash_sequence(docg3, DOC_SEQ_READ);
383         doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
384         doc_delay(docg3, 1);
385         doc_flash_address(docg3, sector & 0xff);
386         doc_flash_address(docg3, (sector >> 8) & 0xff);
387         doc_flash_address(docg3, (sector >> 16) & 0xff);
388         doc_delay(docg3, 1);
389
390         sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
391         doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
392         doc_delay(docg3, 1);
393         doc_flash_address(docg3, sector & 0xff);
394         doc_flash_address(docg3, (sector >> 8) & 0xff);
395         doc_flash_address(docg3, (sector >> 16) & 0xff);
396         doc_delay(docg3, 2);
397
398 out:
399         return ret;
400 }
401
402 /**
403  * doc_read_page_ecc_init - Initialize hardware ECC engine
404  * @docg3: the device
405  * @len: the number of bytes covered by the ECC (BCH covered)
406  *
407  * The function does initialize the hardware ECC engine to compute the Hamming
408  * ECC (on 1 byte) and the BCH Syndroms (on 7 bytes).
409  *
410  * Return 0 if succeeded, -EIO on error
411  */
412 static int doc_read_page_ecc_init(struct docg3 *docg3, int len)
413 {
414         doc_writew(docg3, DOC_ECCCONF0_READ_MODE
415                    | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
416                    | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
417                    DOC_ECCCONF0);
418         doc_delay(docg3, 4);
419         doc_register_readb(docg3, DOC_FLASHCONTROL);
420         return doc_wait_ready(docg3);
421 }
422
423 /**
424  * doc_read_page_prepare - Prepares reading data from a flash page
425  * @docg3: the device
426  * @block0: the first plane block index on flash memory
427  * @block1: the second plane block index on flash memory
428  * @page: the page index in the block
429  * @offset: the offset in the page (must be a multiple of 4)
430  *
431  * Prepares the page to be read in the flash memory :
432  *   - tell ASIC to map the flash pages
433  *   - tell ASIC to be in read mode
434  *
435  * After a call to this method, a call to doc_read_page_finish is mandatory,
436  * to end the read cycle of the flash.
437  *
438  * Read data from a flash page. The length to be read must be between 0 and
439  * (page_size + oob_size + wear_size), ie. 532, and a multiple of 4 (because
440  * the extra bytes reading is not implemented).
441  *
442  * As pages are grouped by 2 (in 2 planes), reading from a page must be done
443  * in two steps:
444  *  - one read of 512 bytes at offset 0
445  *  - one read of 512 bytes at offset 512 + 16
446  *
447  * Returns 0 if successful, -EIO if a read error occured.
448  */
449 static int doc_read_page_prepare(struct docg3 *docg3, int block0, int block1,
450                                  int page, int offset)
451 {
452         int wear_area = 0, ret = 0;
453
454         doc_dbg("doc_read_page_prepare(blocks=(%d,%d), page=%d, ofsInPage=%d)\n",
455                 block0, block1, page, offset);
456         if (offset >= DOC_LAYOUT_WEAR_OFFSET)
457                 wear_area = 1;
458         if (!wear_area && offset > (DOC_LAYOUT_PAGE_OOB_SIZE * 2))
459                 return -EINVAL;
460
461         doc_set_device_id(docg3, docg3->device_id);
462         ret = doc_reset_seq(docg3);
463         if (ret)
464                 goto err;
465
466         /* Program the flash address block and page */
467         ret = doc_read_seek(docg3, block0, block1, page, wear_area, offset);
468         if (ret)
469                 goto err;
470
471         doc_flash_command(docg3, DOC_CMD_READ_ALL_PLANES);
472         doc_delay(docg3, 2);
473         doc_wait_ready(docg3);
474
475         doc_flash_command(docg3, DOC_CMD_SET_ADDR_READ);
476         doc_delay(docg3, 1);
477         if (offset >= DOC_LAYOUT_PAGE_SIZE * 2)
478                 offset -= 2 * DOC_LAYOUT_PAGE_SIZE;
479         doc_flash_address(docg3, offset >> 2);
480         doc_delay(docg3, 1);
481         doc_wait_ready(docg3);
482
483         doc_flash_command(docg3, DOC_CMD_READ_FLASH);
484
485         return 0;
486 err:
487         doc_writeb(docg3, 0, DOC_DATAEND);
488         doc_delay(docg3, 2);
489         return -EIO;
490 }
491
492 /**
493  * doc_read_page_getbytes - Reads bytes from a prepared page
494  * @docg3: the device
495  * @len: the number of bytes to be read (must be a multiple of 4)
496  * @buf: the buffer to be filled in
497  * @first: 1 if first time read, DOC_READADDRESS should be set
498  *
499  */
500 static int doc_read_page_getbytes(struct docg3 *docg3, int len, u_char *buf,
501                                   int first)
502 {
503         doc_read_data_area(docg3, buf, len, first);
504         doc_delay(docg3, 2);
505         return len;
506 }
507
508 /**
509  * doc_get_hw_bch_syndroms - Get hardware calculated BCH syndroms
510  * @docg3: the device
511  * @syns:  the array of 7 integers where the syndroms will be stored
512  */
513 static void doc_get_hw_bch_syndroms(struct docg3 *docg3, int *syns)
514 {
515         int i;
516
517         for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
518                 syns[i] = doc_register_readb(docg3, DOC_BCH_SYNDROM(i));
519 }
520
521 /**
522  * doc_read_page_finish - Ends reading of a flash page
523  * @docg3: the device
524  *
525  * As a side effect, resets the chip selector to 0. This ensures that after each
526  * read operation, the floor 0 is selected. Therefore, if the systems halts, the
527  * reboot will boot on floor 0, where the IPL is.
528  */
529 static void doc_read_page_finish(struct docg3 *docg3)
530 {
531         doc_writeb(docg3, 0, DOC_DATAEND);
532         doc_delay(docg3, 2);
533         doc_set_device_id(docg3, 0);
534 }
535
536 /**
537  * calc_block_sector - Calculate blocks, pages and ofs.
538
539  * @from: offset in flash
540  * @block0: first plane block index calculated
541  * @block1: second plane block index calculated
542  * @page: page calculated
543  * @ofs: offset in page
544  */
545 static void calc_block_sector(loff_t from, int *block0, int *block1, int *page,
546                               int *ofs)
547 {
548         uint sector;
549
550         sector = from / DOC_LAYOUT_PAGE_SIZE;
551         *block0 = sector / (DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_NBPLANES)
552                 * DOC_LAYOUT_NBPLANES;
553         *block1 = *block0 + 1;
554         *page = sector % (DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_NBPLANES);
555         *page /= DOC_LAYOUT_NBPLANES;
556         if (sector % 2)
557                 *ofs = DOC_LAYOUT_PAGE_OOB_SIZE;
558         else
559                 *ofs = 0;
560 }
561
562 /**
563  * doc_read_oob - Read out of band bytes from flash
564  * @mtd: the device
565  * @from: the offset from first block and first page, in bytes, aligned on page
566  *        size
567  * @ops: the mtd oob structure
568  *
569  * Reads flash memory OOB area of pages.
570  *
571  * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
572  */
573 static int doc_read_oob(struct mtd_info *mtd, loff_t from,
574                         struct mtd_oob_ops *ops)
575 {
576         struct docg3 *docg3 = mtd->priv;
577         int block0, block1, page, ret, ofs = 0;
578         u8 *oobbuf = ops->oobbuf;
579         u8 *buf = ops->datbuf;
580         size_t len, ooblen, nbdata, nboob;
581         u8 calc_ecc[DOC_ECC_BCH_SIZE], eccconf1;
582
583         if (buf)
584                 len = ops->len;
585         else
586                 len = 0;
587         if (oobbuf)
588                 ooblen = ops->ooblen;
589         else
590                 ooblen = 0;
591
592         if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
593                 oobbuf += ops->ooboffs;
594
595         doc_dbg("doc_read_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
596                 from, ops->mode, buf, len, oobbuf, ooblen);
597         if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % DOC_LAYOUT_OOB_SIZE) ||
598             (from % DOC_LAYOUT_PAGE_SIZE))
599                 return -EINVAL;
600
601         ret = -EINVAL;
602         calc_block_sector(from + len, &block0, &block1, &page, &ofs);
603         if (block1 > docg3->max_block)
604                 goto err;
605
606         ops->oobretlen = 0;
607         ops->retlen = 0;
608         ret = 0;
609         while (!ret && (len > 0 || ooblen > 0)) {
610                 calc_block_sector(from, &block0, &block1, &page, &ofs);
611                 nbdata = min_t(size_t, len, (size_t)DOC_LAYOUT_PAGE_SIZE);
612                 nboob = min_t(size_t, ooblen, (size_t)DOC_LAYOUT_OOB_SIZE);
613                 ret = doc_read_page_prepare(docg3, block0, block1, page, ofs);
614                 if (ret < 0)
615                         goto err;
616                 ret = doc_read_page_ecc_init(docg3, DOC_ECC_BCH_COVERED_BYTES);
617                 if (ret < 0)
618                         goto err_in_read;
619                 ret = doc_read_page_getbytes(docg3, nbdata, buf, 1);
620                 if (ret < nbdata)
621                         goto err_in_read;
622                 doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE - nbdata,
623                                        NULL, 0);
624                 ret = doc_read_page_getbytes(docg3, nboob, oobbuf, 0);
625                 if (ret < nboob)
626                         goto err_in_read;
627                 doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE - nboob,
628                                        NULL, 0);
629
630                 doc_get_hw_bch_syndroms(docg3, calc_ecc);
631                 eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1);
632
633                 if (nboob >= DOC_LAYOUT_OOB_SIZE) {
634                         doc_dbg("OOB - INFO: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
635                                 oobbuf[0], oobbuf[1], oobbuf[2], oobbuf[3],
636                                 oobbuf[4], oobbuf[5], oobbuf[6]);
637                         doc_dbg("OOB - HAMMING: %02x\n", oobbuf[7]);
638                         doc_dbg("OOB - BCH_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
639                                 oobbuf[8], oobbuf[9], oobbuf[10], oobbuf[11],
640                                 oobbuf[12], oobbuf[13], oobbuf[14]);
641                         doc_dbg("OOB - UNUSED: %02x\n", oobbuf[15]);
642                 }
643                 doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1);
644                 doc_dbg("ECC CALC_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
645                         calc_ecc[0], calc_ecc[1], calc_ecc[2],
646                         calc_ecc[3], calc_ecc[4], calc_ecc[5],
647                         calc_ecc[6]);
648
649                 ret = -EBADMSG;
650                 if (block0 >= DOC_LAYOUT_BLOCK_FIRST_DATA) {
651                         if ((eccconf1 & DOC_ECCCONF1_BCH_SYNDROM_ERR) &&
652                             (eccconf1 & DOC_ECCCONF1_PAGE_IS_WRITTEN))
653                                 goto err_in_read;
654                         if (is_prot_seq_error(docg3))
655                                 goto err_in_read;
656                 }
657
658                 doc_read_page_finish(docg3);
659                 ops->retlen += nbdata;
660                 ops->oobretlen += nboob;
661                 buf += nbdata;
662                 oobbuf += nboob;
663                 len -= nbdata;
664                 ooblen -= nboob;
665                 from += DOC_LAYOUT_PAGE_SIZE;
666         }
667
668         return 0;
669 err_in_read:
670         doc_read_page_finish(docg3);
671 err:
672         return ret;
673 }
674
675 /**
676  * doc_read - Read bytes from flash
677  * @mtd: the device
678  * @from: the offset from first block and first page, in bytes, aligned on page
679  *        size
680  * @len: the number of bytes to read (must be a multiple of 4)
681  * @retlen: the number of bytes actually read
682  * @buf: the filled in buffer
683  *
684  * Reads flash memory pages. This function does not read the OOB chunk, but only
685  * the page data.
686  *
687  * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
688  */
689 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
690              size_t *retlen, u_char *buf)
691 {
692         struct mtd_oob_ops ops;
693         size_t ret;
694
695         memset(&ops, 0, sizeof(ops));
696         ops.datbuf = buf;
697         ops.len = len;
698         ops.mode = MTD_OPS_AUTO_OOB;
699
700         ret = doc_read_oob(mtd, from, &ops);
701         *retlen = ops.retlen;
702         return ret;
703 }
704
705 static int doc_reload_bbt(struct docg3 *docg3)
706 {
707         int block = DOC_LAYOUT_BLOCK_BBT;
708         int ret = 0, nbpages, page;
709         u_char *buf = docg3->bbt;
710
711         nbpages = DIV_ROUND_UP(docg3->max_block + 1, 8 * DOC_LAYOUT_PAGE_SIZE);
712         for (page = 0; !ret && (page < nbpages); page++) {
713                 ret = doc_read_page_prepare(docg3, block, block + 1,
714                                             page + DOC_LAYOUT_PAGE_BBT, 0);
715                 if (!ret)
716                         ret = doc_read_page_ecc_init(docg3,
717                                                      DOC_LAYOUT_PAGE_SIZE);
718                 if (!ret)
719                         doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE,
720                                                buf, 1);
721                 buf += DOC_LAYOUT_PAGE_SIZE;
722         }
723         doc_read_page_finish(docg3);
724         return ret;
725 }
726
727 /**
728  * doc_block_isbad - Checks whether a block is good or not
729  * @mtd: the device
730  * @from: the offset to find the correct block
731  *
732  * Returns 1 if block is bad, 0 if block is good
733  */
734 static int doc_block_isbad(struct mtd_info *mtd, loff_t from)
735 {
736         struct docg3 *docg3 = mtd->priv;
737         int block0, block1, page, ofs, is_good;
738
739         calc_block_sector(from, &block0, &block1, &page, &ofs);
740         doc_dbg("doc_block_isbad(from=%lld) => block=(%d,%d), page=%d, ofs=%d\n",
741                 from, block0, block1, page, ofs);
742
743         if (block0 < DOC_LAYOUT_BLOCK_FIRST_DATA)
744                 return 0;
745         if (block1 > docg3->max_block)
746                 return -EINVAL;
747
748         is_good = docg3->bbt[block0 >> 3] & (1 << (block0 & 0x7));
749         return !is_good;
750 }
751
752 /**
753  * doc_get_erase_count - Get block erase count
754  * @docg3: the device
755  * @from: the offset in which the block is.
756  *
757  * Get the number of times a block was erased. The number is the maximum of
758  * erase times between first and second plane (which should be equal normally).
759  *
760  * Returns The number of erases, or -EINVAL or -EIO on error.
761  */
762 static int doc_get_erase_count(struct docg3 *docg3, loff_t from)
763 {
764         u8 buf[DOC_LAYOUT_WEAR_SIZE];
765         int ret, plane1_erase_count, plane2_erase_count;
766         int block0, block1, page, ofs;
767
768         doc_dbg("doc_get_erase_count(from=%lld, buf=%p)\n", from, buf);
769         if (from % DOC_LAYOUT_PAGE_SIZE)
770                 return -EINVAL;
771         calc_block_sector(from, &block0, &block1, &page, &ofs);
772         if (block1 > docg3->max_block)
773                 return -EINVAL;
774
775         ret = doc_reset_seq(docg3);
776         if (!ret)
777                 ret = doc_read_page_prepare(docg3, block0, block1, page,
778                                             ofs + DOC_LAYOUT_WEAR_OFFSET);
779         if (!ret)
780                 ret = doc_read_page_getbytes(docg3, DOC_LAYOUT_WEAR_SIZE,
781                                              buf, 1);
782         doc_read_page_finish(docg3);
783
784         if (ret || (buf[0] != DOC_ERASE_MARK) || (buf[2] != DOC_ERASE_MARK))
785                 return -EIO;
786         plane1_erase_count = (u8)(~buf[1]) | ((u8)(~buf[4]) << 8)
787                 | ((u8)(~buf[5]) << 16);
788         plane2_erase_count = (u8)(~buf[3]) | ((u8)(~buf[6]) << 8)
789                 | ((u8)(~buf[7]) << 16);
790
791         return max(plane1_erase_count, plane2_erase_count);
792 }
793
794 /*
795  * Debug sysfs entries
796  */
797 static int dbg_flashctrl_show(struct seq_file *s, void *p)
798 {
799         struct docg3 *docg3 = (struct docg3 *)s->private;
800
801         int pos = 0;
802         u8 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
803
804         pos += seq_printf(s,
805                  "FlashControl : 0x%02x (%s,CE# %s,%s,%s,flash %s)\n",
806                  fctrl,
807                  fctrl & DOC_CTRL_VIOLATION ? "protocol violation" : "-",
808                  fctrl & DOC_CTRL_CE ? "active" : "inactive",
809                  fctrl & DOC_CTRL_PROTECTION_ERROR ? "protection error" : "-",
810                  fctrl & DOC_CTRL_SEQUENCE_ERROR ? "sequence error" : "-",
811                  fctrl & DOC_CTRL_FLASHREADY ? "ready" : "not ready");
812         return pos;
813 }
814 DEBUGFS_RO_ATTR(flashcontrol, dbg_flashctrl_show);
815
816 static int dbg_asicmode_show(struct seq_file *s, void *p)
817 {
818         struct docg3 *docg3 = (struct docg3 *)s->private;
819
820         int pos = 0;
821         int pctrl = doc_register_readb(docg3, DOC_ASICMODE);
822         int mode = pctrl & 0x03;
823
824         pos += seq_printf(s,
825                          "%04x : RAM_WE=%d,RSTIN_RESET=%d,BDETCT_RESET=%d,WRITE_ENABLE=%d,POWERDOWN=%d,MODE=%d%d (",
826                          pctrl,
827                          pctrl & DOC_ASICMODE_RAM_WE ? 1 : 0,
828                          pctrl & DOC_ASICMODE_RSTIN_RESET ? 1 : 0,
829                          pctrl & DOC_ASICMODE_BDETCT_RESET ? 1 : 0,
830                          pctrl & DOC_ASICMODE_MDWREN ? 1 : 0,
831                          pctrl & DOC_ASICMODE_POWERDOWN ? 1 : 0,
832                          mode >> 1, mode & 0x1);
833
834         switch (mode) {
835         case DOC_ASICMODE_RESET:
836                 pos += seq_printf(s, "reset");
837                 break;
838         case DOC_ASICMODE_NORMAL:
839                 pos += seq_printf(s, "normal");
840                 break;
841         case DOC_ASICMODE_POWERDOWN:
842                 pos += seq_printf(s, "powerdown");
843                 break;
844         }
845         pos += seq_printf(s, ")\n");
846         return pos;
847 }
848 DEBUGFS_RO_ATTR(asic_mode, dbg_asicmode_show);
849
850 static int dbg_device_id_show(struct seq_file *s, void *p)
851 {
852         struct docg3 *docg3 = (struct docg3 *)s->private;
853         int pos = 0;
854         int id = doc_register_readb(docg3, DOC_DEVICESELECT);
855
856         pos += seq_printf(s, "DeviceId = %d\n", id);
857         return pos;
858 }
859 DEBUGFS_RO_ATTR(device_id, dbg_device_id_show);
860
861 static int dbg_protection_show(struct seq_file *s, void *p)
862 {
863         struct docg3 *docg3 = (struct docg3 *)s->private;
864         int pos = 0;
865         int protect, dps0, dps0_low, dps0_high, dps1, dps1_low, dps1_high;
866
867         protect = doc_register_readb(docg3, DOC_PROTECTION);
868         dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
869         dps0_low = doc_register_readw(docg3, DOC_DPS0_ADDRLOW);
870         dps0_high = doc_register_readw(docg3, DOC_DPS0_ADDRHIGH);
871         dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
872         dps1_low = doc_register_readw(docg3, DOC_DPS1_ADDRLOW);
873         dps1_high = doc_register_readw(docg3, DOC_DPS1_ADDRHIGH);
874
875         pos += seq_printf(s, "Protection = 0x%02x (",
876                          protect);
877         if (protect & DOC_PROTECT_FOUNDRY_OTP_LOCK)
878                 pos += seq_printf(s, "FOUNDRY_OTP_LOCK,");
879         if (protect & DOC_PROTECT_CUSTOMER_OTP_LOCK)
880                 pos += seq_printf(s, "CUSTOMER_OTP_LOCK,");
881         if (protect & DOC_PROTECT_LOCK_INPUT)
882                 pos += seq_printf(s, "LOCK_INPUT,");
883         if (protect & DOC_PROTECT_STICKY_LOCK)
884                 pos += seq_printf(s, "STICKY_LOCK,");
885         if (protect & DOC_PROTECT_PROTECTION_ENABLED)
886                 pos += seq_printf(s, "PROTECTION ON,");
887         if (protect & DOC_PROTECT_IPL_DOWNLOAD_LOCK)
888                 pos += seq_printf(s, "IPL_DOWNLOAD_LOCK,");
889         if (protect & DOC_PROTECT_PROTECTION_ERROR)
890                 pos += seq_printf(s, "PROTECT_ERR,");
891         else
892                 pos += seq_printf(s, "NO_PROTECT_ERR");
893         pos += seq_printf(s, ")\n");
894
895         pos += seq_printf(s, "DPS0 = 0x%02x : "
896                          "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
897                          "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
898                          dps0, dps0_low, dps0_high,
899                          !!(dps0 & DOC_DPS_OTP_PROTECTED),
900                          !!(dps0 & DOC_DPS_READ_PROTECTED),
901                          !!(dps0 & DOC_DPS_WRITE_PROTECTED),
902                          !!(dps0 & DOC_DPS_HW_LOCK_ENABLED),
903                          !!(dps0 & DOC_DPS_KEY_OK));
904         pos += seq_printf(s, "DPS1 = 0x%02x : "
905                          "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
906                          "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
907                          dps1, dps1_low, dps1_high,
908                          !!(dps1 & DOC_DPS_OTP_PROTECTED),
909                          !!(dps1 & DOC_DPS_READ_PROTECTED),
910                          !!(dps1 & DOC_DPS_WRITE_PROTECTED),
911                          !!(dps1 & DOC_DPS_HW_LOCK_ENABLED),
912                          !!(dps1 & DOC_DPS_KEY_OK));
913         return pos;
914 }
915 DEBUGFS_RO_ATTR(protection, dbg_protection_show);
916
917 static int __init doc_dbg_register(struct docg3 *docg3)
918 {
919         struct dentry *root, *entry;
920
921         root = debugfs_create_dir("docg3", NULL);
922         if (!root)
923                 return -ENOMEM;
924
925         entry = debugfs_create_file("flashcontrol", S_IRUSR, root, docg3,
926                                   &flashcontrol_fops);
927         if (entry)
928                 entry = debugfs_create_file("asic_mode", S_IRUSR, root,
929                                             docg3, &asic_mode_fops);
930         if (entry)
931                 entry = debugfs_create_file("device_id", S_IRUSR, root,
932                                             docg3, &device_id_fops);
933         if (entry)
934                 entry = debugfs_create_file("protection", S_IRUSR, root,
935                                             docg3, &protection_fops);
936         if (entry) {
937                 docg3->debugfs_root = root;
938                 return 0;
939         } else {
940                 debugfs_remove_recursive(root);
941                 return -ENOMEM;
942         }
943 }
944
945 static void __exit doc_dbg_unregister(struct docg3 *docg3)
946 {
947         debugfs_remove_recursive(docg3->debugfs_root);
948 }
949
950 /**
951  * doc_set_driver_info - Fill the mtd_info structure and docg3 structure
952  * @chip_id: The chip ID of the supported chip
953  * @mtd: The structure to fill
954  */
955 static void __init doc_set_driver_info(int chip_id, struct mtd_info *mtd)
956 {
957         struct docg3 *docg3 = mtd->priv;
958         int cfg;
959
960         cfg = doc_register_readb(docg3, DOC_CONFIGURATION);
961         docg3->if_cfg = (cfg & DOC_CONF_IF_CFG ? 1 : 0);
962
963         switch (chip_id) {
964         case DOC_CHIPID_G3:
965                 mtd->name = kasprintf(GFP_KERNEL, "DiskOnChip G3 floor %d",
966                                       docg3->device_id);
967                 docg3->max_block = 2047;
968                 break;
969         }
970         mtd->type = MTD_NANDFLASH;
971         /*
972          * Once write methods are added, the correct flags will be set.
973          * mtd->flags = MTD_CAP_NANDFLASH;
974          */
975         mtd->flags = MTD_CAP_ROM;
976         mtd->size = (docg3->max_block + 1) * DOC_LAYOUT_BLOCK_SIZE;
977         mtd->erasesize = DOC_LAYOUT_BLOCK_SIZE * DOC_LAYOUT_NBPLANES;
978         mtd->writesize = DOC_LAYOUT_PAGE_SIZE;
979         mtd->oobsize = DOC_LAYOUT_OOB_SIZE;
980         mtd->owner = THIS_MODULE;
981         mtd->erase = NULL;
982         mtd->point = NULL;
983         mtd->unpoint = NULL;
984         mtd->read = doc_read;
985         mtd->write = NULL;
986         mtd->read_oob = doc_read_oob;
987         mtd->write_oob = NULL;
988         mtd->sync = NULL;
989         mtd->block_isbad = doc_block_isbad;
990         mtd->ecclayout = &docg3_oobinfo;
991 }
992
993 /**
994  * doc_probe_device - Check if a device is available
995  * @base: the io space where the device is probed
996  * @floor: the floor of the probed device
997  * @dev: the device
998  *
999  * Checks whether a device at the specified IO range, and floor is available.
1000  *
1001  * Returns a mtd_info struct if there is a device, ENODEV if none found, ENOMEM
1002  * if a memory allocation failed. If floor 0 is checked, a reset of the ASIC is
1003  * launched.
1004  */
1005 static struct mtd_info *doc_probe_device(void __iomem *base, int floor,
1006                                          struct device *dev)
1007 {
1008         int ret, bbt_nbpages;
1009         u16 chip_id, chip_id_inv;
1010         struct docg3 *docg3;
1011         struct mtd_info *mtd;
1012
1013         ret = -ENOMEM;
1014         docg3 = kzalloc(sizeof(struct docg3), GFP_KERNEL);
1015         if (!docg3)
1016                 goto nomem1;
1017         mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
1018         if (!mtd)
1019                 goto nomem2;
1020         mtd->priv = docg3;
1021         bbt_nbpages = DIV_ROUND_UP(docg3->max_block + 1,
1022                                    8 * DOC_LAYOUT_PAGE_SIZE);
1023         docg3->bbt = kzalloc(bbt_nbpages * DOC_LAYOUT_PAGE_SIZE, GFP_KERNEL);
1024         if (!docg3->bbt)
1025                 goto nomem3;
1026
1027         docg3->dev = dev;
1028         docg3->device_id = floor;
1029         docg3->base = base;
1030         doc_set_device_id(docg3, docg3->device_id);
1031         if (!floor)
1032                 doc_set_asic_mode(docg3, DOC_ASICMODE_RESET);
1033         doc_set_asic_mode(docg3, DOC_ASICMODE_NORMAL);
1034
1035         chip_id = doc_register_readw(docg3, DOC_CHIPID);
1036         chip_id_inv = doc_register_readw(docg3, DOC_CHIPID_INV);
1037
1038         ret = 0;
1039         if (chip_id != (u16)(~chip_id_inv)) {
1040                 goto nomem3;
1041         }
1042
1043         switch (chip_id) {
1044         case DOC_CHIPID_G3:
1045                 doc_info("Found a G3 DiskOnChip at addr %p, floor %d\n",
1046                          base, floor);
1047                 break;
1048         default:
1049                 doc_err("Chip id %04x is not a DiskOnChip G3 chip\n", chip_id);
1050                 goto nomem3;
1051         }
1052
1053         doc_set_driver_info(chip_id, mtd);
1054
1055         doc_reload_bbt(docg3);
1056         return mtd;
1057
1058 nomem3:
1059         kfree(mtd);
1060 nomem2:
1061         kfree(docg3);
1062 nomem1:
1063         return ERR_PTR(ret);
1064 }
1065
1066 /**
1067  * doc_release_device - Release a docg3 floor
1068  * @mtd: the device
1069  */
1070 static void doc_release_device(struct mtd_info *mtd)
1071 {
1072         struct docg3 *docg3 = mtd->priv;
1073
1074         mtd_device_unregister(mtd);
1075         kfree(docg3->bbt);
1076         kfree(docg3);
1077         kfree(mtd->name);
1078         kfree(mtd);
1079 }
1080
1081 /**
1082  * doc_probe - Probe the IO space for a DiskOnChip G3 chip
1083  * @pdev: platform device
1084  *
1085  * Probes for a G3 chip at the specified IO space in the platform data
1086  * ressources. The floor 0 must be available.
1087  *
1088  * Returns 0 on success, -ENOMEM, -ENXIO on error
1089  */
1090 static int __init docg3_probe(struct platform_device *pdev)
1091 {
1092         struct device *dev = &pdev->dev;
1093         struct mtd_info *mtd;
1094         struct resource *ress;
1095         void __iomem *base;
1096         int ret, floor, found = 0;
1097         struct mtd_info **docg3_floors;
1098
1099         ret = -ENXIO;
1100         ress = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1101         if (!ress) {
1102                 dev_err(dev, "No I/O memory resource defined\n");
1103                 goto noress;
1104         }
1105         base = ioremap(ress->start, DOC_IOSPACE_SIZE);
1106
1107         ret = -ENOMEM;
1108         docg3_floors = kzalloc(sizeof(*docg3_floors) * DOC_MAX_NBFLOORS,
1109                                GFP_KERNEL);
1110         if (!docg3_floors)
1111                 goto nomem;
1112
1113         ret = 0;
1114         for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
1115                 mtd = doc_probe_device(base, floor, dev);
1116                 if (floor == 0 && !mtd)
1117                         goto notfound;
1118                 if (!IS_ERR_OR_NULL(mtd))
1119                         ret = mtd_device_parse_register(mtd, part_probes,
1120                                                         NULL, NULL, 0);
1121                 else
1122                         ret = PTR_ERR(mtd);
1123                 docg3_floors[floor] = mtd;
1124                 if (ret)
1125                         goto err_probe;
1126                 if (mtd)
1127                         found++;
1128         }
1129
1130         if (!found)
1131                 goto notfound;
1132
1133         platform_set_drvdata(pdev, docg3_floors);
1134         doc_dbg_register(docg3_floors[0]->priv);
1135         return 0;
1136
1137 notfound:
1138         ret = -ENODEV;
1139         dev_info(dev, "No supported DiskOnChip found\n");
1140 err_probe:
1141         for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
1142                 if (docg3_floors[floor])
1143                         doc_release_device(docg3_floors[floor]);
1144 nomem:
1145         iounmap(base);
1146 noress:
1147         return ret;
1148 }
1149
1150 /**
1151  * docg3_release - Release the driver
1152  * @pdev: the platform device
1153  *
1154  * Returns 0
1155  */
1156 static int __exit docg3_release(struct platform_device *pdev)
1157 {
1158         struct mtd_info **docg3_floors = platform_get_drvdata(pdev);
1159         struct docg3 *docg3 = docg3_floors[0]->priv;
1160         void __iomem *base = docg3->base;
1161         int floor;
1162
1163         doc_dbg_unregister(docg3);
1164         for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
1165                 if (docg3_floors[floor])
1166                         doc_release_device(docg3_floors[floor]);
1167
1168         kfree(docg3_floors);
1169         iounmap(base);
1170         return 0;
1171 }
1172
1173 static struct platform_driver g3_driver = {
1174         .driver         = {
1175                 .name   = "docg3",
1176                 .owner  = THIS_MODULE,
1177         },
1178         .remove         = __exit_p(docg3_release),
1179 };
1180
1181 static int __init docg3_init(void)
1182 {
1183         return platform_driver_probe(&g3_driver, docg3_probe);
1184 }
1185 module_init(docg3_init);
1186
1187
1188 static void __exit docg3_exit(void)
1189 {
1190         platform_driver_unregister(&g3_driver);
1191 }
1192 module_exit(docg3_exit);
1193
1194 MODULE_LICENSE("GPL");
1195 MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
1196 MODULE_DESCRIPTION("MTD driver for DiskOnChip G3");