blob: 26cc17909b16693c79dc3b0688e06e06ce88045c [file] [log] [blame]
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001/*
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>
Robert Jarzmikd13d19e2011-11-19 16:02:55 +010032#include <linux/bitmap.h>
33#include <linux/bitrev.h>
34#include <linux/bch.h>
Robert Jarzmikefa2ca72011-10-05 15:22:34 +020035
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 :
Robert Jarzmikefa2ca72011-10-05 15:22:34 +020047 * - IPL write
Robert Jarzmikefa2ca72011-10-05 15:22:34 +020048 * - 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
Robert Jarzmikd13d19e2011-11-19 16:02:55 +010056 * The BCH ECC is :
Robert Jarzmikefa2ca72011-10-05 15:22:34 +020057 * - BCH is in GF(2^14)
58 * - BCH is over data of 520 bytes (512 page + 7 page_info bytes
59 * + 1 hamming byte)
60 * - BCH can correct up to 4 bits (t = 4)
61 * - BCH syndroms are calculated in hardware, and checked in hardware as well
62 *
63 */
64
Robert Jarzmik732b63b2011-11-19 16:02:49 +010065/**
66 * struct docg3_oobinfo - DiskOnChip G3 OOB layout
67 * @eccbytes: 8 bytes are used (1 for Hamming ECC, 7 for BCH ECC)
68 * @eccpos: ecc positions (byte 7 is Hamming ECC, byte 8-14 are BCH ECC)
69 * @oobfree: free pageinfo bytes (byte 0 until byte 6, byte 15
70 * @oobavail: 8 available bytes remaining after ECC toll
71 */
72static struct nand_ecclayout docg3_oobinfo = {
73 .eccbytes = 8,
74 .eccpos = {7, 8, 9, 10, 11, 12, 13, 14},
75 .oobfree = {{0, 7}, {15, 1} },
76 .oobavail = 8,
77};
78
Robert Jarzmikd13d19e2011-11-19 16:02:55 +010079/**
80 * struct docg3_bch - BCH engine
81 */
82static struct bch_control *docg3_bch;
83
Robert Jarzmikefa2ca72011-10-05 15:22:34 +020084static inline u8 doc_readb(struct docg3 *docg3, u16 reg)
85{
86 u8 val = readb(docg3->base + reg);
87
88 trace_docg3_io(0, 8, reg, (int)val);
89 return val;
90}
91
92static inline u16 doc_readw(struct docg3 *docg3, u16 reg)
93{
94 u16 val = readw(docg3->base + reg);
95
96 trace_docg3_io(0, 16, reg, (int)val);
97 return val;
98}
99
100static inline void doc_writeb(struct docg3 *docg3, u8 val, u16 reg)
101{
102 writeb(val, docg3->base + reg);
Robert Jarzmik84a93052011-11-19 16:02:44 +0100103 trace_docg3_io(1, 8, reg, val);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200104}
105
106static inline void doc_writew(struct docg3 *docg3, u16 val, u16 reg)
107{
108 writew(val, docg3->base + reg);
109 trace_docg3_io(1, 16, reg, val);
110}
111
112static inline void doc_flash_command(struct docg3 *docg3, u8 cmd)
113{
114 doc_writeb(docg3, cmd, DOC_FLASHCOMMAND);
115}
116
117static inline void doc_flash_sequence(struct docg3 *docg3, u8 seq)
118{
119 doc_writeb(docg3, seq, DOC_FLASHSEQUENCE);
120}
121
122static inline void doc_flash_address(struct docg3 *docg3, u8 addr)
123{
124 doc_writeb(docg3, addr, DOC_FLASHADDRESS);
125}
126
127static char const *part_probes[] = { "cmdlinepart", "saftlpart", NULL };
128
129static int doc_register_readb(struct docg3 *docg3, int reg)
130{
131 u8 val;
132
133 doc_writew(docg3, reg, DOC_READADDRESS);
134 val = doc_readb(docg3, reg);
135 doc_vdbg("Read register %04x : %02x\n", reg, val);
136 return val;
137}
138
139static int doc_register_readw(struct docg3 *docg3, int reg)
140{
141 u16 val;
142
143 doc_writew(docg3, reg, DOC_READADDRESS);
144 val = doc_readw(docg3, reg);
145 doc_vdbg("Read register %04x : %04x\n", reg, val);
146 return val;
147}
148
149/**
150 * doc_delay - delay docg3 operations
151 * @docg3: the device
152 * @nbNOPs: the number of NOPs to issue
153 *
154 * As no specification is available, the right timings between chip commands are
155 * unknown. The only available piece of information are the observed nops on a
156 * working docg3 chip.
157 * Therefore, doc_delay relies on a busy loop of NOPs, instead of scheduler
158 * friendlier msleep() functions or blocking mdelay().
159 */
160static void doc_delay(struct docg3 *docg3, int nbNOPs)
161{
162 int i;
163
Robert Jarzmikac48e802011-11-19 16:02:43 +0100164 doc_vdbg("NOP x %d\n", nbNOPs);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200165 for (i = 0; i < nbNOPs; i++)
166 doc_writeb(docg3, 0, DOC_NOP);
167}
168
169static int is_prot_seq_error(struct docg3 *docg3)
170{
171 int ctrl;
172
173 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
174 return ctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR);
175}
176
177static int doc_is_ready(struct docg3 *docg3)
178{
179 int ctrl;
180
181 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
182 return ctrl & DOC_CTRL_FLASHREADY;
183}
184
185static int doc_wait_ready(struct docg3 *docg3)
186{
187 int maxWaitCycles = 100;
188
189 do {
190 doc_delay(docg3, 4);
191 cpu_relax();
192 } while (!doc_is_ready(docg3) && maxWaitCycles--);
193 doc_delay(docg3, 2);
194 if (maxWaitCycles > 0)
195 return 0;
196 else
197 return -EIO;
198}
199
200static int doc_reset_seq(struct docg3 *docg3)
201{
202 int ret;
203
204 doc_writeb(docg3, 0x10, DOC_FLASHCONTROL);
205 doc_flash_sequence(docg3, DOC_SEQ_RESET);
206 doc_flash_command(docg3, DOC_CMD_RESET);
207 doc_delay(docg3, 2);
208 ret = doc_wait_ready(docg3);
209
210 doc_dbg("doc_reset_seq() -> isReady=%s\n", ret ? "false" : "true");
211 return ret;
212}
213
214/**
215 * doc_read_data_area - Read data from data area
216 * @docg3: the device
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100217 * @buf: the buffer to fill in (might be NULL is dummy reads)
218 * @len: the length to read
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200219 * @first: first time read, DOC_READADDRESS should be set
220 *
221 * Reads bytes from flash data. Handles the single byte / even bytes reads.
222 */
223static void doc_read_data_area(struct docg3 *docg3, void *buf, int len,
224 int first)
225{
226 int i, cdr, len4;
227 u16 data16, *dst16;
228 u8 data8, *dst8;
229
230 doc_dbg("doc_read_data_area(buf=%p, len=%d)\n", buf, len);
231 cdr = len & 0x3;
232 len4 = len - cdr;
233
234 if (first)
235 doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
236 dst16 = buf;
237 for (i = 0; i < len4; i += 2) {
238 data16 = doc_readw(docg3, DOC_IOSPACE_DATA);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100239 if (dst16) {
240 *dst16 = data16;
241 dst16++;
242 }
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200243 }
244
245 if (cdr) {
246 doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
247 DOC_READADDRESS);
248 doc_delay(docg3, 1);
249 dst8 = (u8 *)dst16;
250 for (i = 0; i < cdr; i++) {
251 data8 = doc_readb(docg3, DOC_IOSPACE_DATA);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100252 if (dst8) {
253 *dst8 = data8;
254 dst8++;
255 }
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200256 }
257 }
258}
259
260/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100261 * doc_write_data_area - Write data into data area
262 * @docg3: the device
263 * @buf: the buffer to get input bytes from
264 * @len: the length to write
265 *
266 * Writes bytes into flash data. Handles the single byte / even bytes writes.
267 */
268static void doc_write_data_area(struct docg3 *docg3, const void *buf, int len)
269{
270 int i, cdr, len4;
271 u16 *src16;
272 u8 *src8;
273
274 doc_dbg("doc_write_data_area(buf=%p, len=%d)\n", buf, len);
275 cdr = len & 0x3;
276 len4 = len - cdr;
277
278 doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
279 src16 = (u16 *)buf;
280 for (i = 0; i < len4; i += 2) {
281 doc_writew(docg3, *src16, DOC_IOSPACE_DATA);
282 src16++;
283 }
284
285 src8 = (u8 *)src16;
286 for (i = 0; i < cdr; i++) {
287 doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
288 DOC_READADDRESS);
289 doc_writeb(docg3, *src8, DOC_IOSPACE_DATA);
290 src8++;
291 }
292}
293
294/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200295 * doc_set_data_mode - Sets the flash to reliable data mode
296 * @docg3: the device
297 *
298 * The reliable data mode is a bit slower than the fast mode, but less errors
299 * occur. Entering the reliable mode cannot be done without entering the fast
300 * mode first.
301 */
302static void doc_set_reliable_mode(struct docg3 *docg3)
303{
304 doc_dbg("doc_set_reliable_mode()\n");
305 doc_flash_sequence(docg3, DOC_SEQ_SET_MODE);
306 doc_flash_command(docg3, DOC_CMD_FAST_MODE);
307 doc_flash_command(docg3, DOC_CMD_RELIABLE_MODE);
308 doc_delay(docg3, 2);
309}
310
311/**
312 * doc_set_asic_mode - Set the ASIC mode
313 * @docg3: the device
314 * @mode: the mode
315 *
316 * The ASIC can work in 3 modes :
317 * - RESET: all registers are zeroed
318 * - NORMAL: receives and handles commands
319 * - POWERDOWN: minimal poweruse, flash parts shut off
320 */
321static void doc_set_asic_mode(struct docg3 *docg3, u8 mode)
322{
323 int i;
324
325 for (i = 0; i < 12; i++)
326 doc_readb(docg3, DOC_IOSPACE_IPL);
327
328 mode |= DOC_ASICMODE_MDWREN;
329 doc_dbg("doc_set_asic_mode(%02x)\n", mode);
330 doc_writeb(docg3, mode, DOC_ASICMODE);
331 doc_writeb(docg3, ~mode, DOC_ASICMODECONFIRM);
332 doc_delay(docg3, 1);
333}
334
335/**
336 * doc_set_device_id - Sets the devices id for cascaded G3 chips
337 * @docg3: the device
338 * @id: the chip to select (amongst 0, 1, 2, 3)
339 *
340 * There can be 4 cascaded G3 chips. This function selects the one which will
341 * should be the active one.
342 */
343static void doc_set_device_id(struct docg3 *docg3, int id)
344{
345 u8 ctrl;
346
347 doc_dbg("doc_set_device_id(%d)\n", id);
348 doc_writeb(docg3, id, DOC_DEVICESELECT);
349 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
350
351 ctrl &= ~DOC_CTRL_VIOLATION;
352 ctrl |= DOC_CTRL_CE;
353 doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
354}
355
356/**
357 * doc_set_extra_page_mode - Change flash page layout
358 * @docg3: the device
359 *
360 * Normally, the flash page is split into the data (512 bytes) and the out of
361 * band data (16 bytes). For each, 4 more bytes can be accessed, where the wear
362 * leveling counters are stored. To access this last area of 4 bytes, a special
363 * mode must be input to the flash ASIC.
364 *
365 * Returns 0 if no error occured, -EIO else.
366 */
367static int doc_set_extra_page_mode(struct docg3 *docg3)
368{
369 int fctrl;
370
371 doc_dbg("doc_set_extra_page_mode()\n");
372 doc_flash_sequence(docg3, DOC_SEQ_PAGE_SIZE_532);
373 doc_flash_command(docg3, DOC_CMD_PAGE_SIZE_532);
374 doc_delay(docg3, 2);
375
376 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
377 if (fctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR))
378 return -EIO;
379 else
380 return 0;
381}
382
383/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100384 * doc_setup_addr_sector - Setup blocks/page/ofs address for one plane
385 * @docg3: the device
386 * @sector: the sector
387 */
388static void doc_setup_addr_sector(struct docg3 *docg3, int sector)
389{
390 doc_delay(docg3, 1);
391 doc_flash_address(docg3, sector & 0xff);
392 doc_flash_address(docg3, (sector >> 8) & 0xff);
393 doc_flash_address(docg3, (sector >> 16) & 0xff);
394 doc_delay(docg3, 1);
395}
396
397/**
398 * doc_setup_writeaddr_sector - Setup blocks/page/ofs address for one plane
399 * @docg3: the device
400 * @sector: the sector
401 * @ofs: the offset in the page, between 0 and (512 + 16 + 512)
402 */
403static void doc_setup_writeaddr_sector(struct docg3 *docg3, int sector, int ofs)
404{
405 ofs = ofs >> 2;
406 doc_delay(docg3, 1);
407 doc_flash_address(docg3, ofs & 0xff);
408 doc_flash_address(docg3, sector & 0xff);
409 doc_flash_address(docg3, (sector >> 8) & 0xff);
410 doc_flash_address(docg3, (sector >> 16) & 0xff);
411 doc_delay(docg3, 1);
412}
413
414/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200415 * doc_seek - Set both flash planes to the specified block, page for reading
416 * @docg3: the device
417 * @block0: the first plane block index
418 * @block1: the second plane block index
419 * @page: the page index within the block
420 * @wear: if true, read will occur on the 4 extra bytes of the wear area
421 * @ofs: offset in page to read
422 *
423 * Programs the flash even and odd planes to the specific block and page.
424 * Alternatively, programs the flash to the wear area of the specified page.
425 */
426static int doc_read_seek(struct docg3 *docg3, int block0, int block1, int page,
427 int wear, int ofs)
428{
429 int sector, ret = 0;
430
431 doc_dbg("doc_seek(blocks=(%d,%d), page=%d, ofs=%d, wear=%d)\n",
432 block0, block1, page, ofs, wear);
433
434 if (!wear && (ofs < 2 * DOC_LAYOUT_PAGE_SIZE)) {
435 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
436 doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
437 doc_delay(docg3, 2);
438 } else {
439 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
440 doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
441 doc_delay(docg3, 2);
442 }
443
444 doc_set_reliable_mode(docg3);
445 if (wear)
446 ret = doc_set_extra_page_mode(docg3);
447 if (ret)
448 goto out;
449
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200450 doc_flash_sequence(docg3, DOC_SEQ_READ);
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100451 sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200452 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100453 doc_setup_addr_sector(docg3, sector);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200454
455 sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
456 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100457 doc_setup_addr_sector(docg3, sector);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200458 doc_delay(docg3, 1);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200459
460out:
461 return ret;
462}
463
464/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100465 * doc_write_seek - Set both flash planes to the specified block, page for writing
466 * @docg3: the device
467 * @block0: the first plane block index
468 * @block1: the second plane block index
469 * @page: the page index within the block
470 * @ofs: offset in page to write
471 *
472 * Programs the flash even and odd planes to the specific block and page.
473 * Alternatively, programs the flash to the wear area of the specified page.
474 */
475static int doc_write_seek(struct docg3 *docg3, int block0, int block1, int page,
476 int ofs)
477{
478 int ret = 0, sector;
479
480 doc_dbg("doc_write_seek(blocks=(%d,%d), page=%d, ofs=%d)\n",
481 block0, block1, page, ofs);
482
483 doc_set_reliable_mode(docg3);
484
485 if (ofs < 2 * DOC_LAYOUT_PAGE_SIZE) {
486 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
487 doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
488 doc_delay(docg3, 2);
489 } else {
490 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
491 doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
492 doc_delay(docg3, 2);
493 }
494
495 doc_flash_sequence(docg3, DOC_SEQ_PAGE_SETUP);
496 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1);
497
498 sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
499 doc_setup_writeaddr_sector(docg3, sector, ofs);
500
501 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE3);
502 doc_delay(docg3, 2);
503 ret = doc_wait_ready(docg3);
504 if (ret)
505 goto out;
506
507 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1);
508 sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
509 doc_setup_writeaddr_sector(docg3, sector, ofs);
510 doc_delay(docg3, 1);
511
512out:
513 return ret;
514}
515
516
517/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200518 * doc_read_page_ecc_init - Initialize hardware ECC engine
519 * @docg3: the device
520 * @len: the number of bytes covered by the ECC (BCH covered)
521 *
522 * The function does initialize the hardware ECC engine to compute the Hamming
523 * ECC (on 1 byte) and the BCH Syndroms (on 7 bytes).
524 *
525 * Return 0 if succeeded, -EIO on error
526 */
527static int doc_read_page_ecc_init(struct docg3 *docg3, int len)
528{
529 doc_writew(docg3, DOC_ECCCONF0_READ_MODE
530 | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
531 | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
532 DOC_ECCCONF0);
533 doc_delay(docg3, 4);
534 doc_register_readb(docg3, DOC_FLASHCONTROL);
535 return doc_wait_ready(docg3);
536}
537
538/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100539 * doc_write_page_ecc_init - Initialize hardware BCH 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 Syndroms (on 7 bytes).
545 *
546 * Return 0 if succeeded, -EIO on error
547 */
548static int doc_write_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_ecc_disable - Disable Hamming and BCH ECC hardware calculator
561 * @docg3: the device
562 *
563 * Disables the hardware ECC generator and checker, for unchecked reads (as when
564 * reading OOB only or write status byte).
565 */
566static void doc_ecc_disable(struct docg3 *docg3)
567{
568 doc_writew(docg3, DOC_ECCCONF0_READ_MODE, DOC_ECCCONF0);
569 doc_delay(docg3, 4);
570}
571
572/**
573 * doc_hamming_ecc_init - Initialize hardware Hamming ECC engine
574 * @docg3: the device
575 * @nb_bytes: the number of bytes covered by the ECC (Hamming covered)
576 *
577 * This function programs the ECC hardware to compute the hamming code on the
578 * last provided N bytes to the hardware generator.
579 */
580static void doc_hamming_ecc_init(struct docg3 *docg3, int nb_bytes)
581{
582 u8 ecc_conf1;
583
584 ecc_conf1 = doc_register_readb(docg3, DOC_ECCCONF1);
585 ecc_conf1 &= ~DOC_ECCCONF1_HAMMING_BITS_MASK;
586 ecc_conf1 |= (nb_bytes & DOC_ECCCONF1_HAMMING_BITS_MASK);
587 doc_writeb(docg3, ecc_conf1, DOC_ECCCONF1);
588}
589
590/**
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100591 * doc_correct_data - Fix if need be read data from flash
592 * @docg3: the device
593 * @buf: the buffer of read data (512 + 7 + 1 bytes)
594 * @hwecc: the hardware calculated ECC.
595 * It's in fact recv_ecc ^ calc_ecc, where recv_ecc was read from OOB
596 * area data, and calc_ecc the ECC calculated by the hardware generator.
597 *
598 * Checks if the received data matches the ECC, and if an error is detected,
599 * tries to fix the bit flips (at most 4) in the buffer buf. As the docg3
600 * understands the (data, ecc, syndroms) in an inverted order in comparison to
601 * the BCH library, the function reverses the order of bits (ie. bit7 and bit0,
602 * bit6 and bit 1, ...) for all ECC data.
603 *
604 * The hardware ecc unit produces oob_ecc ^ calc_ecc. The kernel's bch
605 * algorithm is used to decode this. However the hw operates on page
606 * data in a bit order that is the reverse of that of the bch alg,
607 * requiring that the bits be reversed on the result. Thanks to Ivan
608 * Djelic for his analysis.
609 *
610 * Returns number of fixed bits (0, 1, 2, 3, 4) or -EBADMSG if too many bit
611 * errors were detected and cannot be fixed.
612 */
613static int doc_ecc_bch_fix_data(struct docg3 *docg3, void *buf, u8 *hwecc)
614{
615 u8 ecc[DOC_ECC_BCH_SIZE];
616 int errorpos[DOC_ECC_BCH_T], i, numerrs;
617
618 for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
619 ecc[i] = bitrev8(hwecc[i]);
620 numerrs = decode_bch(docg3_bch, NULL, DOC_ECC_BCH_COVERED_BYTES,
621 NULL, ecc, NULL, errorpos);
622 BUG_ON(numerrs == -EINVAL);
623 if (numerrs < 0)
624 goto out;
625
626 for (i = 0; i < numerrs; i++)
627 errorpos[i] = (errorpos[i] & ~7) | (7 - (errorpos[i] & 7));
628 for (i = 0; i < numerrs; i++)
629 if (errorpos[i] < DOC_ECC_BCH_COVERED_BYTES*8)
630 /* error is located in data, correct it */
631 change_bit(errorpos[i], buf);
632out:
633 doc_dbg("doc_ecc_bch_fix_data: flipped %d bits\n", numerrs);
634 return numerrs;
635}
636
637
638/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200639 * doc_read_page_prepare - Prepares reading data from a flash page
640 * @docg3: the device
641 * @block0: the first plane block index on flash memory
642 * @block1: the second plane block index on flash memory
643 * @page: the page index in the block
644 * @offset: the offset in the page (must be a multiple of 4)
645 *
646 * Prepares the page to be read in the flash memory :
647 * - tell ASIC to map the flash pages
648 * - tell ASIC to be in read mode
649 *
650 * After a call to this method, a call to doc_read_page_finish is mandatory,
651 * to end the read cycle of the flash.
652 *
653 * Read data from a flash page. The length to be read must be between 0 and
654 * (page_size + oob_size + wear_size), ie. 532, and a multiple of 4 (because
655 * the extra bytes reading is not implemented).
656 *
657 * As pages are grouped by 2 (in 2 planes), reading from a page must be done
658 * in two steps:
659 * - one read of 512 bytes at offset 0
660 * - one read of 512 bytes at offset 512 + 16
661 *
662 * Returns 0 if successful, -EIO if a read error occured.
663 */
664static int doc_read_page_prepare(struct docg3 *docg3, int block0, int block1,
665 int page, int offset)
666{
667 int wear_area = 0, ret = 0;
668
669 doc_dbg("doc_read_page_prepare(blocks=(%d,%d), page=%d, ofsInPage=%d)\n",
670 block0, block1, page, offset);
671 if (offset >= DOC_LAYOUT_WEAR_OFFSET)
672 wear_area = 1;
673 if (!wear_area && offset > (DOC_LAYOUT_PAGE_OOB_SIZE * 2))
674 return -EINVAL;
675
676 doc_set_device_id(docg3, docg3->device_id);
677 ret = doc_reset_seq(docg3);
678 if (ret)
679 goto err;
680
681 /* Program the flash address block and page */
682 ret = doc_read_seek(docg3, block0, block1, page, wear_area, offset);
683 if (ret)
684 goto err;
685
686 doc_flash_command(docg3, DOC_CMD_READ_ALL_PLANES);
687 doc_delay(docg3, 2);
688 doc_wait_ready(docg3);
689
690 doc_flash_command(docg3, DOC_CMD_SET_ADDR_READ);
691 doc_delay(docg3, 1);
692 if (offset >= DOC_LAYOUT_PAGE_SIZE * 2)
693 offset -= 2 * DOC_LAYOUT_PAGE_SIZE;
694 doc_flash_address(docg3, offset >> 2);
695 doc_delay(docg3, 1);
696 doc_wait_ready(docg3);
697
698 doc_flash_command(docg3, DOC_CMD_READ_FLASH);
699
700 return 0;
701err:
702 doc_writeb(docg3, 0, DOC_DATAEND);
703 doc_delay(docg3, 2);
704 return -EIO;
705}
706
707/**
708 * doc_read_page_getbytes - Reads bytes from a prepared page
709 * @docg3: the device
710 * @len: the number of bytes to be read (must be a multiple of 4)
711 * @buf: the buffer to be filled in
712 * @first: 1 if first time read, DOC_READADDRESS should be set
713 *
714 */
715static int doc_read_page_getbytes(struct docg3 *docg3, int len, u_char *buf,
716 int first)
717{
718 doc_read_data_area(docg3, buf, len, first);
719 doc_delay(docg3, 2);
720 return len;
721}
722
723/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100724 * doc_write_page_putbytes - Writes bytes into a prepared page
725 * @docg3: the device
726 * @len: the number of bytes to be written
727 * @buf: the buffer of input bytes
728 *
729 */
730static void doc_write_page_putbytes(struct docg3 *docg3, int len,
731 const u_char *buf)
732{
733 doc_write_data_area(docg3, buf, len);
734 doc_delay(docg3, 2);
735}
736
737/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200738 * doc_get_hw_bch_syndroms - Get hardware calculated BCH syndroms
739 * @docg3: the device
740 * @syns: the array of 7 integers where the syndroms will be stored
741 */
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100742static void doc_get_hw_bch_syndroms(struct docg3 *docg3, u8 *syns)
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200743{
744 int i;
745
746 for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
747 syns[i] = doc_register_readb(docg3, DOC_BCH_SYNDROM(i));
748}
749
750/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100751 * doc_page_finish - Ends reading/writing of a flash page
752 * @docg3: the device
753 */
754static void doc_page_finish(struct docg3 *docg3)
755{
756 doc_writeb(docg3, 0, DOC_DATAEND);
757 doc_delay(docg3, 2);
758}
759
760/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200761 * doc_read_page_finish - Ends reading of a flash page
762 * @docg3: the device
763 *
764 * As a side effect, resets the chip selector to 0. This ensures that after each
765 * read operation, the floor 0 is selected. Therefore, if the systems halts, the
766 * reboot will boot on floor 0, where the IPL is.
767 */
768static void doc_read_page_finish(struct docg3 *docg3)
769{
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100770 doc_page_finish(docg3);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200771 doc_set_device_id(docg3, 0);
772}
773
774/**
775 * calc_block_sector - Calculate blocks, pages and ofs.
776
777 * @from: offset in flash
778 * @block0: first plane block index calculated
779 * @block1: second plane block index calculated
780 * @page: page calculated
781 * @ofs: offset in page
782 */
783static void calc_block_sector(loff_t from, int *block0, int *block1, int *page,
784 int *ofs)
785{
786 uint sector;
787
788 sector = from / DOC_LAYOUT_PAGE_SIZE;
789 *block0 = sector / (DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_NBPLANES)
790 * DOC_LAYOUT_NBPLANES;
791 *block1 = *block0 + 1;
792 *page = sector % (DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_NBPLANES);
793 *page /= DOC_LAYOUT_NBPLANES;
794 if (sector % 2)
795 *ofs = DOC_LAYOUT_PAGE_OOB_SIZE;
796 else
797 *ofs = 0;
798}
799
800/**
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100801 * doc_read_oob - Read out of band bytes from flash
802 * @mtd: the device
803 * @from: the offset from first block and first page, in bytes, aligned on page
804 * size
805 * @ops: the mtd oob structure
806 *
807 * Reads flash memory OOB area of pages.
808 *
809 * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
810 */
811static int doc_read_oob(struct mtd_info *mtd, loff_t from,
812 struct mtd_oob_ops *ops)
813{
814 struct docg3 *docg3 = mtd->priv;
815 int block0, block1, page, ret, ofs = 0;
816 u8 *oobbuf = ops->oobbuf;
817 u8 *buf = ops->datbuf;
818 size_t len, ooblen, nbdata, nboob;
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100819 u8 hwecc[DOC_ECC_BCH_SIZE], eccconf1;
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100820
821 if (buf)
822 len = ops->len;
823 else
824 len = 0;
825 if (oobbuf)
826 ooblen = ops->ooblen;
827 else
828 ooblen = 0;
829
830 if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
831 oobbuf += ops->ooboffs;
832
833 doc_dbg("doc_read_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
834 from, ops->mode, buf, len, oobbuf, ooblen);
835 if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % DOC_LAYOUT_OOB_SIZE) ||
836 (from % DOC_LAYOUT_PAGE_SIZE))
837 return -EINVAL;
838
839 ret = -EINVAL;
840 calc_block_sector(from + len, &block0, &block1, &page, &ofs);
841 if (block1 > docg3->max_block)
842 goto err;
843
844 ops->oobretlen = 0;
845 ops->retlen = 0;
846 ret = 0;
847 while (!ret && (len > 0 || ooblen > 0)) {
848 calc_block_sector(from, &block0, &block1, &page, &ofs);
849 nbdata = min_t(size_t, len, (size_t)DOC_LAYOUT_PAGE_SIZE);
850 nboob = min_t(size_t, ooblen, (size_t)DOC_LAYOUT_OOB_SIZE);
851 ret = doc_read_page_prepare(docg3, block0, block1, page, ofs);
852 if (ret < 0)
853 goto err;
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100854 ret = doc_read_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100855 if (ret < 0)
856 goto err_in_read;
857 ret = doc_read_page_getbytes(docg3, nbdata, buf, 1);
858 if (ret < nbdata)
859 goto err_in_read;
860 doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE - nbdata,
861 NULL, 0);
862 ret = doc_read_page_getbytes(docg3, nboob, oobbuf, 0);
863 if (ret < nboob)
864 goto err_in_read;
865 doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE - nboob,
866 NULL, 0);
867
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100868 doc_get_hw_bch_syndroms(docg3, hwecc);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100869 eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1);
870
871 if (nboob >= DOC_LAYOUT_OOB_SIZE) {
872 doc_dbg("OOB - INFO: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
873 oobbuf[0], oobbuf[1], oobbuf[2], oobbuf[3],
874 oobbuf[4], oobbuf[5], oobbuf[6]);
875 doc_dbg("OOB - HAMMING: %02x\n", oobbuf[7]);
876 doc_dbg("OOB - BCH_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
877 oobbuf[8], oobbuf[9], oobbuf[10], oobbuf[11],
878 oobbuf[12], oobbuf[13], oobbuf[14]);
879 doc_dbg("OOB - UNUSED: %02x\n", oobbuf[15]);
880 }
881 doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1);
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100882 doc_dbg("ECC HW_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
883 hwecc[0], hwecc[1], hwecc[2], hwecc[3], hwecc[4],
884 hwecc[5], hwecc[6]);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100885
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100886 ret = -EIO;
887 if (is_prot_seq_error(docg3))
888 goto err_in_read;
889 ret = 0;
890 if ((block0 >= DOC_LAYOUT_BLOCK_FIRST_DATA) &&
891 (eccconf1 & DOC_ECCCONF1_BCH_SYNDROM_ERR) &&
892 (eccconf1 & DOC_ECCCONF1_PAGE_IS_WRITTEN) &&
893 (ops->mode != MTD_OPS_RAW) &&
894 (nbdata == DOC_LAYOUT_PAGE_SIZE)) {
895 ret = doc_ecc_bch_fix_data(docg3, buf, hwecc);
896 if (ret < 0) {
897 mtd->ecc_stats.failed++;
898 ret = -EBADMSG;
899 }
900 if (ret > 0) {
901 mtd->ecc_stats.corrected += ret;
902 ret = -EUCLEAN;
903 }
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100904 }
905
906 doc_read_page_finish(docg3);
907 ops->retlen += nbdata;
908 ops->oobretlen += nboob;
909 buf += nbdata;
910 oobbuf += nboob;
911 len -= nbdata;
912 ooblen -= nboob;
913 from += DOC_LAYOUT_PAGE_SIZE;
914 }
915
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100916 return ret;
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100917err_in_read:
918 doc_read_page_finish(docg3);
919err:
920 return ret;
921}
922
923/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200924 * doc_read - Read bytes from flash
925 * @mtd: the device
926 * @from: the offset from first block and first page, in bytes, aligned on page
927 * size
928 * @len: the number of bytes to read (must be a multiple of 4)
929 * @retlen: the number of bytes actually read
930 * @buf: the filled in buffer
931 *
932 * Reads flash memory pages. This function does not read the OOB chunk, but only
933 * the page data.
934 *
935 * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
936 */
937static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
938 size_t *retlen, u_char *buf)
939{
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100940 struct mtd_oob_ops ops;
941 size_t ret;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200942
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100943 memset(&ops, 0, sizeof(ops));
944 ops.datbuf = buf;
945 ops.len = len;
946 ops.mode = MTD_OPS_AUTO_OOB;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200947
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100948 ret = doc_read_oob(mtd, from, &ops);
949 *retlen = ops.retlen;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200950 return ret;
951}
952
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200953static int doc_reload_bbt(struct docg3 *docg3)
954{
955 int block = DOC_LAYOUT_BLOCK_BBT;
956 int ret = 0, nbpages, page;
957 u_char *buf = docg3->bbt;
958
959 nbpages = DIV_ROUND_UP(docg3->max_block + 1, 8 * DOC_LAYOUT_PAGE_SIZE);
960 for (page = 0; !ret && (page < nbpages); page++) {
961 ret = doc_read_page_prepare(docg3, block, block + 1,
962 page + DOC_LAYOUT_PAGE_BBT, 0);
963 if (!ret)
964 ret = doc_read_page_ecc_init(docg3,
965 DOC_LAYOUT_PAGE_SIZE);
966 if (!ret)
967 doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE,
968 buf, 1);
969 buf += DOC_LAYOUT_PAGE_SIZE;
970 }
971 doc_read_page_finish(docg3);
972 return ret;
973}
974
975/**
976 * doc_block_isbad - Checks whether a block is good or not
977 * @mtd: the device
978 * @from: the offset to find the correct block
979 *
980 * Returns 1 if block is bad, 0 if block is good
981 */
982static int doc_block_isbad(struct mtd_info *mtd, loff_t from)
983{
984 struct docg3 *docg3 = mtd->priv;
985 int block0, block1, page, ofs, is_good;
986
987 calc_block_sector(from, &block0, &block1, &page, &ofs);
988 doc_dbg("doc_block_isbad(from=%lld) => block=(%d,%d), page=%d, ofs=%d\n",
989 from, block0, block1, page, ofs);
990
991 if (block0 < DOC_LAYOUT_BLOCK_FIRST_DATA)
992 return 0;
993 if (block1 > docg3->max_block)
994 return -EINVAL;
995
996 is_good = docg3->bbt[block0 >> 3] & (1 << (block0 & 0x7));
997 return !is_good;
998}
999
1000/**
1001 * doc_get_erase_count - Get block erase count
1002 * @docg3: the device
1003 * @from: the offset in which the block is.
1004 *
1005 * Get the number of times a block was erased. The number is the maximum of
1006 * erase times between first and second plane (which should be equal normally).
1007 *
1008 * Returns The number of erases, or -EINVAL or -EIO on error.
1009 */
1010static int doc_get_erase_count(struct docg3 *docg3, loff_t from)
1011{
1012 u8 buf[DOC_LAYOUT_WEAR_SIZE];
1013 int ret, plane1_erase_count, plane2_erase_count;
1014 int block0, block1, page, ofs;
1015
1016 doc_dbg("doc_get_erase_count(from=%lld, buf=%p)\n", from, buf);
1017 if (from % DOC_LAYOUT_PAGE_SIZE)
1018 return -EINVAL;
1019 calc_block_sector(from, &block0, &block1, &page, &ofs);
1020 if (block1 > docg3->max_block)
1021 return -EINVAL;
1022
1023 ret = doc_reset_seq(docg3);
1024 if (!ret)
1025 ret = doc_read_page_prepare(docg3, block0, block1, page,
1026 ofs + DOC_LAYOUT_WEAR_OFFSET);
1027 if (!ret)
1028 ret = doc_read_page_getbytes(docg3, DOC_LAYOUT_WEAR_SIZE,
1029 buf, 1);
1030 doc_read_page_finish(docg3);
1031
1032 if (ret || (buf[0] != DOC_ERASE_MARK) || (buf[2] != DOC_ERASE_MARK))
1033 return -EIO;
1034 plane1_erase_count = (u8)(~buf[1]) | ((u8)(~buf[4]) << 8)
1035 | ((u8)(~buf[5]) << 16);
1036 plane2_erase_count = (u8)(~buf[3]) | ((u8)(~buf[6]) << 8)
1037 | ((u8)(~buf[7]) << 16);
1038
1039 return max(plane1_erase_count, plane2_erase_count);
1040}
1041
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001042/**
1043 * doc_get_op_status - get erase/write operation status
1044 * @docg3: the device
1045 *
1046 * Queries the status from the chip, and returns it
1047 *
1048 * Returns the status (bits DOC_PLANES_STATUS_*)
1049 */
1050static int doc_get_op_status(struct docg3 *docg3)
1051{
1052 u8 status;
1053
1054 doc_flash_sequence(docg3, DOC_SEQ_PLANES_STATUS);
1055 doc_flash_command(docg3, DOC_CMD_PLANES_STATUS);
1056 doc_delay(docg3, 5);
1057
1058 doc_ecc_disable(docg3);
1059 doc_read_data_area(docg3, &status, 1, 1);
1060 return status;
1061}
1062
1063/**
1064 * doc_write_erase_wait_status - wait for write or erase completion
1065 * @docg3: the device
1066 *
1067 * Wait for the chip to be ready again after erase or write operation, and check
1068 * erase/write status.
1069 *
1070 * Returns 0 if erase successfull, -EIO if erase/write issue, -ETIMEOUT if
1071 * timeout
1072 */
1073static int doc_write_erase_wait_status(struct docg3 *docg3)
1074{
1075 int status, ret = 0;
1076
1077 if (!doc_is_ready(docg3))
1078 usleep_range(3000, 3000);
1079 if (!doc_is_ready(docg3)) {
1080 doc_dbg("Timeout reached and the chip is still not ready\n");
1081 ret = -EAGAIN;
1082 goto out;
1083 }
1084
1085 status = doc_get_op_status(docg3);
1086 if (status & DOC_PLANES_STATUS_FAIL) {
1087 doc_dbg("Erase/Write failed on (a) plane(s), status = %x\n",
1088 status);
1089 ret = -EIO;
1090 }
1091
1092out:
1093 doc_page_finish(docg3);
1094 return ret;
1095}
1096
1097/**
Robert Jarzmikde03cd72011-11-19 16:02:53 +01001098 * doc_erase_block - Erase a couple of blocks
1099 * @docg3: the device
1100 * @block0: the first block to erase (leftmost plane)
1101 * @block1: the second block to erase (rightmost plane)
1102 *
1103 * Erase both blocks, and return operation status
1104 *
1105 * Returns 0 if erase successful, -EIO if erase issue, -ETIMEOUT if chip not
1106 * ready for too long
1107 */
1108static int doc_erase_block(struct docg3 *docg3, int block0, int block1)
1109{
1110 int ret, sector;
1111
1112 doc_dbg("doc_erase_block(blocks=(%d,%d))\n", block0, block1);
1113 ret = doc_reset_seq(docg3);
1114 if (ret)
1115 return -EIO;
1116
1117 doc_set_reliable_mode(docg3);
1118 doc_flash_sequence(docg3, DOC_SEQ_ERASE);
1119
1120 sector = block0 << DOC_ADDR_BLOCK_SHIFT;
1121 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
1122 doc_setup_addr_sector(docg3, sector);
1123 sector = block1 << DOC_ADDR_BLOCK_SHIFT;
1124 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
1125 doc_setup_addr_sector(docg3, sector);
1126 doc_delay(docg3, 1);
1127
1128 doc_flash_command(docg3, DOC_CMD_ERASECYCLE2);
1129 doc_delay(docg3, 2);
1130
1131 if (is_prot_seq_error(docg3)) {
1132 doc_err("Erase blocks %d,%d error\n", block0, block1);
1133 return -EIO;
1134 }
1135
1136 return doc_write_erase_wait_status(docg3);
1137}
1138
1139/**
1140 * doc_erase - Erase a portion of the chip
1141 * @mtd: the device
1142 * @info: the erase info
1143 *
1144 * Erase a bunch of contiguous blocks, by pairs, as a "mtd" page of 1024 is
1145 * split into 2 pages of 512 bytes on 2 contiguous blocks.
1146 *
1147 * Returns 0 if erase successful, -EINVAL if adressing error, -EIO if erase
1148 * issue
1149 */
1150static int doc_erase(struct mtd_info *mtd, struct erase_info *info)
1151{
1152 struct docg3 *docg3 = mtd->priv;
1153 uint64_t len;
1154 int block0, block1, page, ret, ofs = 0;
1155
1156 doc_dbg("doc_erase(from=%lld, len=%lld\n", info->addr, info->len);
1157 doc_set_device_id(docg3, docg3->device_id);
1158
1159 info->state = MTD_ERASE_PENDING;
1160 calc_block_sector(info->addr + info->len,
1161 &block0, &block1, &page, &ofs);
1162 ret = -EINVAL;
1163 if (block1 > docg3->max_block || page || ofs)
1164 goto reset_err;
1165
1166 ret = 0;
1167 calc_block_sector(info->addr, &block0, &block1, &page, &ofs);
1168 doc_set_reliable_mode(docg3);
1169 for (len = info->len; !ret && len > 0; len -= mtd->erasesize) {
1170 info->state = MTD_ERASING;
1171 ret = doc_erase_block(docg3, block0, block1);
1172 block0 += 2;
1173 block1 += 2;
1174 }
1175
1176 if (ret)
1177 goto reset_err;
1178
1179 info->state = MTD_ERASE_DONE;
1180 return 0;
1181
1182reset_err:
1183 info->state = MTD_ERASE_FAILED;
1184 return ret;
1185}
1186
1187/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001188 * doc_write_page - Write a single page to the chip
1189 * @docg3: the device
1190 * @to: the offset from first block and first page, in bytes, aligned on page
1191 * size
1192 * @buf: buffer to get bytes from
1193 * @oob: buffer to get out of band bytes from (can be NULL if no OOB should be
1194 * written)
1195 * @autoecc: if 0, all 16 bytes from OOB are taken, regardless of HW Hamming or
1196 * BCH computations. If 1, only bytes 0-7 and byte 15 are taken,
1197 * remaining ones are filled with hardware Hamming and BCH
1198 * computations. Its value is not meaningfull is oob == NULL.
1199 *
1200 * Write one full page (ie. 1 page split on two planes), of 512 bytes, with the
1201 * OOB data. The OOB ECC is automatically computed by the hardware Hamming and
1202 * BCH generator if autoecc is not null.
1203 *
1204 * Returns 0 if write successful, -EIO if write error, -EAGAIN if timeout
1205 */
1206static int doc_write_page(struct docg3 *docg3, loff_t to, const u_char *buf,
1207 const u_char *oob, int autoecc)
1208{
1209 int block0, block1, page, ret, ofs = 0;
1210 u8 syn[DOC_ECC_BCH_SIZE], hamming;
1211
1212 doc_dbg("doc_write_page(to=%lld)\n", to);
1213 calc_block_sector(to, &block0, &block1, &page, &ofs);
1214
1215 doc_set_device_id(docg3, docg3->device_id);
1216 ret = doc_reset_seq(docg3);
1217 if (ret)
1218 goto err;
1219
1220 /* Program the flash address block and page */
1221 ret = doc_write_seek(docg3, block0, block1, page, ofs);
1222 if (ret)
1223 goto err;
1224
Robert Jarzmikd13d19e2011-11-19 16:02:55 +01001225 doc_write_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001226 doc_delay(docg3, 2);
1227 doc_write_page_putbytes(docg3, DOC_LAYOUT_PAGE_SIZE, buf);
1228
1229 if (oob && autoecc) {
1230 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ, oob);
1231 doc_delay(docg3, 2);
1232 oob += DOC_LAYOUT_OOB_UNUSED_OFS;
1233
1234 hamming = doc_register_readb(docg3, DOC_HAMMINGPARITY);
1235 doc_delay(docg3, 2);
1236 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_HAMMING_SZ,
1237 &hamming);
1238 doc_delay(docg3, 2);
1239
1240 doc_get_hw_bch_syndroms(docg3, syn);
1241 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_BCH_SZ, syn);
1242 doc_delay(docg3, 2);
1243
1244 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_UNUSED_SZ, oob);
1245 }
1246 if (oob && !autoecc)
1247 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_SIZE, oob);
1248
1249 doc_delay(docg3, 2);
1250 doc_page_finish(docg3);
1251 doc_delay(docg3, 2);
1252 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE2);
1253 doc_delay(docg3, 2);
1254
1255 /*
1256 * The wait status will perform another doc_page_finish() call, but that
1257 * seems to please the docg3, so leave it.
1258 */
1259 ret = doc_write_erase_wait_status(docg3);
1260 return ret;
1261err:
1262 doc_read_page_finish(docg3);
1263 return ret;
1264}
1265
1266/**
1267 * doc_guess_autoecc - Guess autoecc mode from mbd_oob_ops
1268 * @ops: the oob operations
1269 *
1270 * Returns 0 or 1 if success, -EINVAL if invalid oob mode
1271 */
1272static int doc_guess_autoecc(struct mtd_oob_ops *ops)
1273{
1274 int autoecc;
1275
1276 switch (ops->mode) {
1277 case MTD_OPS_PLACE_OOB:
1278 case MTD_OPS_AUTO_OOB:
1279 autoecc = 1;
1280 break;
1281 case MTD_OPS_RAW:
1282 autoecc = 0;
1283 break;
1284 default:
1285 autoecc = -EINVAL;
1286 }
1287 return autoecc;
1288}
1289
1290/**
1291 * doc_fill_autooob - Fill a 16 bytes OOB from 8 non-ECC bytes
1292 * @dst: the target 16 bytes OOB buffer
1293 * @oobsrc: the source 8 bytes non-ECC OOB buffer
1294 *
1295 */
1296static void doc_fill_autooob(u8 *dst, u8 *oobsrc)
1297{
1298 memcpy(dst, oobsrc, DOC_LAYOUT_OOB_PAGEINFO_SZ);
1299 dst[DOC_LAYOUT_OOB_UNUSED_OFS] = oobsrc[DOC_LAYOUT_OOB_PAGEINFO_SZ];
1300}
1301
1302/**
1303 * doc_backup_oob - Backup OOB into docg3 structure
1304 * @docg3: the device
1305 * @to: the page offset in the chip
1306 * @ops: the OOB size and buffer
1307 *
1308 * As the docg3 should write a page with its OOB in one pass, and some userland
1309 * applications do write_oob() to setup the OOB and then write(), store the OOB
1310 * into a temporary storage. This is very dangerous, as 2 concurrent
1311 * applications could store an OOB, and then write their pages (which will
1312 * result into one having its OOB corrupted).
1313 *
1314 * The only reliable way would be for userland to call doc_write_oob() with both
1315 * the page data _and_ the OOB area.
1316 *
1317 * Returns 0 if success, -EINVAL if ops content invalid
1318 */
1319static int doc_backup_oob(struct docg3 *docg3, loff_t to,
1320 struct mtd_oob_ops *ops)
1321{
1322 int ooblen = ops->ooblen, autoecc;
1323
1324 if (ooblen != DOC_LAYOUT_OOB_SIZE)
1325 return -EINVAL;
1326 autoecc = doc_guess_autoecc(ops);
1327 if (autoecc < 0)
1328 return autoecc;
1329
1330 docg3->oob_write_ofs = to;
1331 docg3->oob_autoecc = autoecc;
1332 if (ops->mode == MTD_OPS_AUTO_OOB) {
1333 doc_fill_autooob(docg3->oob_write_buf, ops->oobbuf);
1334 ops->oobretlen = 8;
1335 } else {
1336 memcpy(docg3->oob_write_buf, ops->oobbuf, DOC_LAYOUT_OOB_SIZE);
1337 ops->oobretlen = DOC_LAYOUT_OOB_SIZE;
1338 }
1339 return 0;
1340}
1341
1342/**
1343 * doc_write_oob - Write out of band bytes to flash
1344 * @mtd: the device
1345 * @ofs: the offset from first block and first page, in bytes, aligned on page
1346 * size
1347 * @ops: the mtd oob structure
1348 *
1349 * Either write OOB data into a temporary buffer, for the subsequent write
1350 * page. The provided OOB should be 16 bytes long. If a data buffer is provided
1351 * as well, issue the page write.
1352 * Or provide data without OOB, and then a all zeroed OOB will be used (ECC will
1353 * still be filled in if asked for).
1354 *
1355 * Returns 0 is successfull, EINVAL if length is not 14 bytes
1356 */
1357static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
1358 struct mtd_oob_ops *ops)
1359{
1360 struct docg3 *docg3 = mtd->priv;
1361 int block0, block1, page, ret, pofs = 0, autoecc, oobdelta;
1362 u8 *oobbuf = ops->oobbuf;
1363 u8 *buf = ops->datbuf;
1364 size_t len, ooblen;
1365 u8 oob[DOC_LAYOUT_OOB_SIZE];
1366
1367 if (buf)
1368 len = ops->len;
1369 else
1370 len = 0;
1371 if (oobbuf)
1372 ooblen = ops->ooblen;
1373 else
1374 ooblen = 0;
1375
1376 if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
1377 oobbuf += ops->ooboffs;
1378
1379 doc_dbg("doc_write_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
1380 ofs, ops->mode, buf, len, oobbuf, ooblen);
1381 switch (ops->mode) {
1382 case MTD_OPS_PLACE_OOB:
1383 case MTD_OPS_RAW:
1384 oobdelta = mtd->oobsize;
1385 break;
1386 case MTD_OPS_AUTO_OOB:
1387 oobdelta = mtd->ecclayout->oobavail;
1388 break;
1389 default:
1390 oobdelta = 0;
1391 }
1392 if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % oobdelta) ||
1393 (ofs % DOC_LAYOUT_PAGE_SIZE))
1394 return -EINVAL;
1395 if (len && ooblen &&
1396 (len / DOC_LAYOUT_PAGE_SIZE) != (ooblen / oobdelta))
1397 return -EINVAL;
1398
1399 ret = -EINVAL;
1400 calc_block_sector(ofs + len, &block0, &block1, &page, &pofs);
1401 if (block1 > docg3->max_block)
1402 goto err;
1403
1404 ops->oobretlen = 0;
1405 ops->retlen = 0;
1406 ret = 0;
1407 if (len == 0 && ooblen == 0)
1408 return -EINVAL;
1409 if (len == 0 && ooblen > 0)
1410 return doc_backup_oob(docg3, ofs, ops);
1411
1412 autoecc = doc_guess_autoecc(ops);
1413 if (autoecc < 0)
1414 return autoecc;
1415
1416 while (!ret && len > 0) {
1417 memset(oob, 0, sizeof(oob));
1418 if (ofs == docg3->oob_write_ofs)
1419 memcpy(oob, docg3->oob_write_buf, DOC_LAYOUT_OOB_SIZE);
1420 else if (ooblen > 0 && ops->mode == MTD_OPS_AUTO_OOB)
1421 doc_fill_autooob(oob, oobbuf);
1422 else if (ooblen > 0)
1423 memcpy(oob, oobbuf, DOC_LAYOUT_OOB_SIZE);
1424 ret = doc_write_page(docg3, ofs, buf, oob, autoecc);
1425
1426 ofs += DOC_LAYOUT_PAGE_SIZE;
1427 len -= DOC_LAYOUT_PAGE_SIZE;
1428 buf += DOC_LAYOUT_PAGE_SIZE;
1429 if (ooblen) {
1430 oobbuf += oobdelta;
1431 ooblen -= oobdelta;
1432 ops->oobretlen += oobdelta;
1433 }
1434 ops->retlen += DOC_LAYOUT_PAGE_SIZE;
1435 }
1436err:
1437 doc_set_device_id(docg3, 0);
1438 return ret;
1439}
1440
1441/**
1442 * doc_write - Write a buffer to the chip
1443 * @mtd: the device
1444 * @to: the offset from first block and first page, in bytes, aligned on page
1445 * size
1446 * @len: the number of bytes to write (must be a full page size, ie. 512)
1447 * @retlen: the number of bytes actually written (0 or 512)
1448 * @buf: the buffer to get bytes from
1449 *
1450 * Writes data to the chip.
1451 *
1452 * Returns 0 if write successful, -EIO if write error
1453 */
1454static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
1455 size_t *retlen, const u_char *buf)
1456{
1457 struct docg3 *docg3 = mtd->priv;
1458 int ret;
1459 struct mtd_oob_ops ops;
1460
1461 doc_dbg("doc_write(to=%lld, len=%zu)\n", to, len);
1462 ops.datbuf = (char *)buf;
1463 ops.len = len;
1464 ops.mode = MTD_OPS_PLACE_OOB;
1465 ops.oobbuf = NULL;
1466 ops.ooblen = 0;
1467 ops.ooboffs = 0;
1468
1469 ret = doc_write_oob(mtd, to, &ops);
1470 *retlen = ops.retlen;
1471 return ret;
1472}
1473
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001474/*
1475 * Debug sysfs entries
1476 */
1477static int dbg_flashctrl_show(struct seq_file *s, void *p)
1478{
1479 struct docg3 *docg3 = (struct docg3 *)s->private;
1480
1481 int pos = 0;
1482 u8 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
1483
1484 pos += seq_printf(s,
1485 "FlashControl : 0x%02x (%s,CE# %s,%s,%s,flash %s)\n",
1486 fctrl,
1487 fctrl & DOC_CTRL_VIOLATION ? "protocol violation" : "-",
1488 fctrl & DOC_CTRL_CE ? "active" : "inactive",
1489 fctrl & DOC_CTRL_PROTECTION_ERROR ? "protection error" : "-",
1490 fctrl & DOC_CTRL_SEQUENCE_ERROR ? "sequence error" : "-",
1491 fctrl & DOC_CTRL_FLASHREADY ? "ready" : "not ready");
1492 return pos;
1493}
1494DEBUGFS_RO_ATTR(flashcontrol, dbg_flashctrl_show);
1495
1496static int dbg_asicmode_show(struct seq_file *s, void *p)
1497{
1498 struct docg3 *docg3 = (struct docg3 *)s->private;
1499
1500 int pos = 0;
1501 int pctrl = doc_register_readb(docg3, DOC_ASICMODE);
1502 int mode = pctrl & 0x03;
1503
1504 pos += seq_printf(s,
1505 "%04x : RAM_WE=%d,RSTIN_RESET=%d,BDETCT_RESET=%d,WRITE_ENABLE=%d,POWERDOWN=%d,MODE=%d%d (",
1506 pctrl,
1507 pctrl & DOC_ASICMODE_RAM_WE ? 1 : 0,
1508 pctrl & DOC_ASICMODE_RSTIN_RESET ? 1 : 0,
1509 pctrl & DOC_ASICMODE_BDETCT_RESET ? 1 : 0,
1510 pctrl & DOC_ASICMODE_MDWREN ? 1 : 0,
1511 pctrl & DOC_ASICMODE_POWERDOWN ? 1 : 0,
1512 mode >> 1, mode & 0x1);
1513
1514 switch (mode) {
1515 case DOC_ASICMODE_RESET:
1516 pos += seq_printf(s, "reset");
1517 break;
1518 case DOC_ASICMODE_NORMAL:
1519 pos += seq_printf(s, "normal");
1520 break;
1521 case DOC_ASICMODE_POWERDOWN:
1522 pos += seq_printf(s, "powerdown");
1523 break;
1524 }
1525 pos += seq_printf(s, ")\n");
1526 return pos;
1527}
1528DEBUGFS_RO_ATTR(asic_mode, dbg_asicmode_show);
1529
1530static int dbg_device_id_show(struct seq_file *s, void *p)
1531{
1532 struct docg3 *docg3 = (struct docg3 *)s->private;
1533 int pos = 0;
1534 int id = doc_register_readb(docg3, DOC_DEVICESELECT);
1535
1536 pos += seq_printf(s, "DeviceId = %d\n", id);
1537 return pos;
1538}
1539DEBUGFS_RO_ATTR(device_id, dbg_device_id_show);
1540
1541static int dbg_protection_show(struct seq_file *s, void *p)
1542{
1543 struct docg3 *docg3 = (struct docg3 *)s->private;
1544 int pos = 0;
Robert Jarzmikdbc26d92011-11-19 16:02:45 +01001545 int protect, dps0, dps0_low, dps0_high, dps1, dps1_low, dps1_high;
1546
1547 protect = doc_register_readb(docg3, DOC_PROTECTION);
1548 dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
1549 dps0_low = doc_register_readw(docg3, DOC_DPS0_ADDRLOW);
1550 dps0_high = doc_register_readw(docg3, DOC_DPS0_ADDRHIGH);
1551 dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
1552 dps1_low = doc_register_readw(docg3, DOC_DPS1_ADDRLOW);
1553 dps1_high = doc_register_readw(docg3, DOC_DPS1_ADDRHIGH);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001554
1555 pos += seq_printf(s, "Protection = 0x%02x (",
1556 protect);
1557 if (protect & DOC_PROTECT_FOUNDRY_OTP_LOCK)
1558 pos += seq_printf(s, "FOUNDRY_OTP_LOCK,");
1559 if (protect & DOC_PROTECT_CUSTOMER_OTP_LOCK)
1560 pos += seq_printf(s, "CUSTOMER_OTP_LOCK,");
1561 if (protect & DOC_PROTECT_LOCK_INPUT)
1562 pos += seq_printf(s, "LOCK_INPUT,");
1563 if (protect & DOC_PROTECT_STICKY_LOCK)
1564 pos += seq_printf(s, "STICKY_LOCK,");
1565 if (protect & DOC_PROTECT_PROTECTION_ENABLED)
1566 pos += seq_printf(s, "PROTECTION ON,");
1567 if (protect & DOC_PROTECT_IPL_DOWNLOAD_LOCK)
1568 pos += seq_printf(s, "IPL_DOWNLOAD_LOCK,");
1569 if (protect & DOC_PROTECT_PROTECTION_ERROR)
1570 pos += seq_printf(s, "PROTECT_ERR,");
1571 else
1572 pos += seq_printf(s, "NO_PROTECT_ERR");
1573 pos += seq_printf(s, ")\n");
1574
1575 pos += seq_printf(s, "DPS0 = 0x%02x : "
1576 "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
1577 "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1578 dps0, dps0_low, dps0_high,
1579 !!(dps0 & DOC_DPS_OTP_PROTECTED),
1580 !!(dps0 & DOC_DPS_READ_PROTECTED),
1581 !!(dps0 & DOC_DPS_WRITE_PROTECTED),
1582 !!(dps0 & DOC_DPS_HW_LOCK_ENABLED),
1583 !!(dps0 & DOC_DPS_KEY_OK));
1584 pos += seq_printf(s, "DPS1 = 0x%02x : "
1585 "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
1586 "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1587 dps1, dps1_low, dps1_high,
1588 !!(dps1 & DOC_DPS_OTP_PROTECTED),
1589 !!(dps1 & DOC_DPS_READ_PROTECTED),
1590 !!(dps1 & DOC_DPS_WRITE_PROTECTED),
1591 !!(dps1 & DOC_DPS_HW_LOCK_ENABLED),
1592 !!(dps1 & DOC_DPS_KEY_OK));
1593 return pos;
1594}
1595DEBUGFS_RO_ATTR(protection, dbg_protection_show);
1596
1597static int __init doc_dbg_register(struct docg3 *docg3)
1598{
1599 struct dentry *root, *entry;
1600
1601 root = debugfs_create_dir("docg3", NULL);
1602 if (!root)
1603 return -ENOMEM;
1604
1605 entry = debugfs_create_file("flashcontrol", S_IRUSR, root, docg3,
1606 &flashcontrol_fops);
1607 if (entry)
1608 entry = debugfs_create_file("asic_mode", S_IRUSR, root,
1609 docg3, &asic_mode_fops);
1610 if (entry)
1611 entry = debugfs_create_file("device_id", S_IRUSR, root,
1612 docg3, &device_id_fops);
1613 if (entry)
1614 entry = debugfs_create_file("protection", S_IRUSR, root,
1615 docg3, &protection_fops);
1616 if (entry) {
1617 docg3->debugfs_root = root;
1618 return 0;
1619 } else {
1620 debugfs_remove_recursive(root);
1621 return -ENOMEM;
1622 }
1623}
1624
1625static void __exit doc_dbg_unregister(struct docg3 *docg3)
1626{
1627 debugfs_remove_recursive(docg3->debugfs_root);
1628}
1629
1630/**
1631 * doc_set_driver_info - Fill the mtd_info structure and docg3 structure
1632 * @chip_id: The chip ID of the supported chip
1633 * @mtd: The structure to fill
1634 */
1635static void __init doc_set_driver_info(int chip_id, struct mtd_info *mtd)
1636{
1637 struct docg3 *docg3 = mtd->priv;
1638 int cfg;
1639
1640 cfg = doc_register_readb(docg3, DOC_CONFIGURATION);
1641 docg3->if_cfg = (cfg & DOC_CONF_IF_CFG ? 1 : 0);
1642
1643 switch (chip_id) {
1644 case DOC_CHIPID_G3:
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001645 mtd->name = kasprintf(GFP_KERNEL, "DiskOnChip G3 floor %d",
1646 docg3->device_id);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001647 docg3->max_block = 2047;
1648 break;
1649 }
1650 mtd->type = MTD_NANDFLASH;
Robert Jarzmik7a7fcf12011-11-19 16:02:54 +01001651 mtd->flags = MTD_CAP_NANDFLASH;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001652 mtd->size = (docg3->max_block + 1) * DOC_LAYOUT_BLOCK_SIZE;
1653 mtd->erasesize = DOC_LAYOUT_BLOCK_SIZE * DOC_LAYOUT_NBPLANES;
1654 mtd->writesize = DOC_LAYOUT_PAGE_SIZE;
1655 mtd->oobsize = DOC_LAYOUT_OOB_SIZE;
1656 mtd->owner = THIS_MODULE;
Robert Jarzmik7a7fcf12011-11-19 16:02:54 +01001657 mtd->erase = doc_erase;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001658 mtd->point = NULL;
1659 mtd->unpoint = NULL;
1660 mtd->read = doc_read;
Robert Jarzmik7a7fcf12011-11-19 16:02:54 +01001661 mtd->write = doc_write;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001662 mtd->read_oob = doc_read_oob;
Robert Jarzmik7a7fcf12011-11-19 16:02:54 +01001663 mtd->write_oob = doc_write_oob;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001664 mtd->sync = NULL;
1665 mtd->block_isbad = doc_block_isbad;
Robert Jarzmik732b63b2011-11-19 16:02:49 +01001666 mtd->ecclayout = &docg3_oobinfo;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001667}
1668
1669/**
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001670 * doc_probe_device - Check if a device is available
1671 * @base: the io space where the device is probed
1672 * @floor: the floor of the probed device
1673 * @dev: the device
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001674 *
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001675 * Checks whether a device at the specified IO range, and floor is available.
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001676 *
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001677 * Returns a mtd_info struct if there is a device, ENODEV if none found, ENOMEM
1678 * if a memory allocation failed. If floor 0 is checked, a reset of the ASIC is
1679 * launched.
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001680 */
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001681static struct mtd_info *doc_probe_device(void __iomem *base, int floor,
1682 struct device *dev)
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001683{
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001684 int ret, bbt_nbpages;
1685 u16 chip_id, chip_id_inv;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001686 struct docg3 *docg3;
1687 struct mtd_info *mtd;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001688
1689 ret = -ENOMEM;
1690 docg3 = kzalloc(sizeof(struct docg3), GFP_KERNEL);
1691 if (!docg3)
1692 goto nomem1;
1693 mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
1694 if (!mtd)
1695 goto nomem2;
1696 mtd->priv = docg3;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001697 bbt_nbpages = DIV_ROUND_UP(docg3->max_block + 1,
1698 8 * DOC_LAYOUT_PAGE_SIZE);
1699 docg3->bbt = kzalloc(bbt_nbpages * DOC_LAYOUT_PAGE_SIZE, GFP_KERNEL);
1700 if (!docg3->bbt)
1701 goto nomem3;
1702
1703 docg3->dev = dev;
1704 docg3->device_id = floor;
1705 docg3->base = base;
1706 doc_set_device_id(docg3, docg3->device_id);
1707 if (!floor)
1708 doc_set_asic_mode(docg3, DOC_ASICMODE_RESET);
1709 doc_set_asic_mode(docg3, DOC_ASICMODE_NORMAL);
1710
1711 chip_id = doc_register_readw(docg3, DOC_CHIPID);
1712 chip_id_inv = doc_register_readw(docg3, DOC_CHIPID_INV);
1713
1714 ret = 0;
1715 if (chip_id != (u16)(~chip_id_inv)) {
1716 goto nomem3;
1717 }
1718
1719 switch (chip_id) {
1720 case DOC_CHIPID_G3:
1721 doc_info("Found a G3 DiskOnChip at addr %p, floor %d\n",
1722 base, floor);
1723 break;
1724 default:
1725 doc_err("Chip id %04x is not a DiskOnChip G3 chip\n", chip_id);
1726 goto nomem3;
1727 }
1728
1729 doc_set_driver_info(chip_id, mtd);
1730
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001731 doc_hamming_ecc_init(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001732 doc_reload_bbt(docg3);
1733 return mtd;
1734
1735nomem3:
1736 kfree(mtd);
1737nomem2:
1738 kfree(docg3);
1739nomem1:
1740 return ERR_PTR(ret);
1741}
1742
1743/**
1744 * doc_release_device - Release a docg3 floor
1745 * @mtd: the device
1746 */
1747static void doc_release_device(struct mtd_info *mtd)
1748{
1749 struct docg3 *docg3 = mtd->priv;
1750
1751 mtd_device_unregister(mtd);
1752 kfree(docg3->bbt);
1753 kfree(docg3);
1754 kfree(mtd->name);
1755 kfree(mtd);
1756}
1757
1758/**
1759 * doc_probe - Probe the IO space for a DiskOnChip G3 chip
1760 * @pdev: platform device
1761 *
1762 * Probes for a G3 chip at the specified IO space in the platform data
1763 * ressources. The floor 0 must be available.
1764 *
1765 * Returns 0 on success, -ENOMEM, -ENXIO on error
1766 */
1767static int __init docg3_probe(struct platform_device *pdev)
1768{
1769 struct device *dev = &pdev->dev;
1770 struct mtd_info *mtd;
1771 struct resource *ress;
1772 void __iomem *base;
1773 int ret, floor, found = 0;
1774 struct mtd_info **docg3_floors;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001775
1776 ret = -ENXIO;
1777 ress = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1778 if (!ress) {
1779 dev_err(dev, "No I/O memory resource defined\n");
1780 goto noress;
1781 }
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001782 base = ioremap(ress->start, DOC_IOSPACE_SIZE);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001783
1784 ret = -ENOMEM;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001785 docg3_floors = kzalloc(sizeof(*docg3_floors) * DOC_MAX_NBFLOORS,
1786 GFP_KERNEL);
1787 if (!docg3_floors)
Robert Jarzmikd13d19e2011-11-19 16:02:55 +01001788 goto nomem1;
1789 docg3_bch = init_bch(DOC_ECC_BCH_M, DOC_ECC_BCH_T,
1790 DOC_ECC_BCH_PRIMPOLY);
1791 if (!docg3_bch)
1792 goto nomem2;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001793
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001794 ret = 0;
1795 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
1796 mtd = doc_probe_device(base, floor, dev);
1797 if (floor == 0 && !mtd)
1798 goto notfound;
1799 if (!IS_ERR_OR_NULL(mtd))
1800 ret = mtd_device_parse_register(mtd, part_probes,
1801 NULL, NULL, 0);
1802 else
1803 ret = PTR_ERR(mtd);
1804 docg3_floors[floor] = mtd;
1805 if (ret)
1806 goto err_probe;
1807 if (mtd)
1808 found++;
1809 }
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001810
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001811 if (!found)
1812 goto notfound;
1813
1814 platform_set_drvdata(pdev, docg3_floors);
1815 doc_dbg_register(docg3_floors[0]->priv);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001816 return 0;
1817
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001818notfound:
1819 ret = -ENODEV;
1820 dev_info(dev, "No supported DiskOnChip found\n");
1821err_probe:
Robert Jarzmikd13d19e2011-11-19 16:02:55 +01001822 free_bch(docg3_bch);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001823 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
1824 if (docg3_floors[floor])
1825 doc_release_device(docg3_floors[floor]);
Robert Jarzmikd13d19e2011-11-19 16:02:55 +01001826nomem2:
1827 kfree(docg3_floors);
1828nomem1:
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001829 iounmap(base);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001830noress:
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001831 return ret;
1832}
1833
1834/**
1835 * docg3_release - Release the driver
1836 * @pdev: the platform device
1837 *
1838 * Returns 0
1839 */
1840static int __exit docg3_release(struct platform_device *pdev)
1841{
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001842 struct mtd_info **docg3_floors = platform_get_drvdata(pdev);
1843 struct docg3 *docg3 = docg3_floors[0]->priv;
1844 void __iomem *base = docg3->base;
1845 int floor;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001846
1847 doc_dbg_unregister(docg3);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001848 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
1849 if (docg3_floors[floor])
1850 doc_release_device(docg3_floors[floor]);
1851
1852 kfree(docg3_floors);
Robert Jarzmikd13d19e2011-11-19 16:02:55 +01001853 free_bch(docg3_bch);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001854 iounmap(base);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001855 return 0;
1856}
1857
1858static struct platform_driver g3_driver = {
1859 .driver = {
1860 .name = "docg3",
1861 .owner = THIS_MODULE,
1862 },
1863 .remove = __exit_p(docg3_release),
1864};
1865
1866static int __init docg3_init(void)
1867{
1868 return platform_driver_probe(&g3_driver, docg3_probe);
1869}
1870module_init(docg3_init);
1871
1872
1873static void __exit docg3_exit(void)
1874{
1875 platform_driver_unregister(&g3_driver);
1876}
1877module_exit(docg3_exit);
1878
1879MODULE_LICENSE("GPL");
1880MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
1881MODULE_DESCRIPTION("MTD driver for DiskOnChip G3");