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