blob: b833e6cc684c389985bbe91aec15c2612daf6b5e [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>
Robert Jarzmika59459f2014-09-27 21:34:29 +020025#include <linux/of.h>
Robert Jarzmikefa2ca72011-10-05 15:22:34 +020026#include <linux/platform_device.h>
27#include <linux/string.h>
28#include <linux/slab.h>
29#include <linux/io.h>
30#include <linux/delay.h>
31#include <linux/mtd/mtd.h>
32#include <linux/mtd/partitions.h>
Robert Jarzmikd13d19e2011-11-19 16:02:55 +010033#include <linux/bitmap.h>
34#include <linux/bitrev.h>
35#include <linux/bch.h>
Robert Jarzmikefa2ca72011-10-05 15:22:34 +020036
37#include <linux/debugfs.h>
38#include <linux/seq_file.h>
39
40#define CREATE_TRACE_POINTS
41#include "docg3.h"
42
43/*
44 * This driver handles the DiskOnChip G3 flash memory.
45 *
46 * As no specification is available from M-Systems/Sandisk, this drivers lacks
47 * several functions available on the chip, as :
Robert Jarzmikefa2ca72011-10-05 15:22:34 +020048 * - IPL write
Robert Jarzmikefa2ca72011-10-05 15:22:34 +020049 *
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 Jarzmikb6044362011-12-02 20:00:12 +010065static unsigned int reliable_mode;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +010066module_param(reliable_mode, uint, 0);
67MODULE_PARM_DESC(reliable_mode, "Set the docg3 mode (0=normal MLC, 1=fast, "
68 "2=reliable) : MLC normal operations are in normal mode");
69
Boris Brezillon1bd0b242016-02-03 19:05:04 +010070static int docg3_ooblayout_ecc(struct mtd_info *mtd, int section,
71 struct mtd_oob_region *oobregion)
72{
73 if (section)
74 return -ERANGE;
75
76 /* byte 7 is Hamming ECC, byte 8-14 are BCH ECC */
77 oobregion->offset = 7;
78 oobregion->length = 8;
79
80 return 0;
81}
82
83static int docg3_ooblayout_free(struct mtd_info *mtd, int section,
84 struct mtd_oob_region *oobregion)
85{
86 if (section > 1)
87 return -ERANGE;
88
89 /* free bytes: byte 0 until byte 6, byte 15 */
90 if (!section) {
91 oobregion->offset = 0;
92 oobregion->length = 7;
93 } else {
94 oobregion->offset = 15;
95 oobregion->length = 1;
96 }
97
98 return 0;
99}
100
101static const struct mtd_ooblayout_ops nand_ooblayout_docg3_ops = {
102 .ecc = docg3_ooblayout_ecc,
103 .free = docg3_ooblayout_free,
Robert Jarzmik732b63b2011-11-19 16:02:49 +0100104};
105
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200106static inline u8 doc_readb(struct docg3 *docg3, u16 reg)
107{
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +0100108 u8 val = readb(docg3->cascade->base + reg);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200109
110 trace_docg3_io(0, 8, reg, (int)val);
111 return val;
112}
113
114static inline u16 doc_readw(struct docg3 *docg3, u16 reg)
115{
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +0100116 u16 val = readw(docg3->cascade->base + reg);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200117
118 trace_docg3_io(0, 16, reg, (int)val);
119 return val;
120}
121
122static inline void doc_writeb(struct docg3 *docg3, u8 val, u16 reg)
123{
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +0100124 writeb(val, docg3->cascade->base + reg);
Robert Jarzmik84a93052011-11-19 16:02:44 +0100125 trace_docg3_io(1, 8, reg, val);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200126}
127
128static inline void doc_writew(struct docg3 *docg3, u16 val, u16 reg)
129{
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +0100130 writew(val, docg3->cascade->base + reg);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200131 trace_docg3_io(1, 16, reg, val);
132}
133
134static inline void doc_flash_command(struct docg3 *docg3, u8 cmd)
135{
136 doc_writeb(docg3, cmd, DOC_FLASHCOMMAND);
137}
138
139static inline void doc_flash_sequence(struct docg3 *docg3, u8 seq)
140{
141 doc_writeb(docg3, seq, DOC_FLASHSEQUENCE);
142}
143
144static inline void doc_flash_address(struct docg3 *docg3, u8 addr)
145{
146 doc_writeb(docg3, addr, DOC_FLASHADDRESS);
147}
148
Artem Bityutskiyafffeec2013-03-12 10:50:07 +0200149static char const * const part_probes[] = { "cmdlinepart", "saftlpart", NULL };
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200150
151static int doc_register_readb(struct docg3 *docg3, int reg)
152{
153 u8 val;
154
155 doc_writew(docg3, reg, DOC_READADDRESS);
156 val = doc_readb(docg3, reg);
157 doc_vdbg("Read register %04x : %02x\n", reg, val);
158 return val;
159}
160
161static int doc_register_readw(struct docg3 *docg3, int reg)
162{
163 u16 val;
164
165 doc_writew(docg3, reg, DOC_READADDRESS);
166 val = doc_readw(docg3, reg);
167 doc_vdbg("Read register %04x : %04x\n", reg, val);
168 return val;
169}
170
171/**
172 * doc_delay - delay docg3 operations
173 * @docg3: the device
174 * @nbNOPs: the number of NOPs to issue
175 *
176 * As no specification is available, the right timings between chip commands are
177 * unknown. The only available piece of information are the observed nops on a
178 * working docg3 chip.
179 * Therefore, doc_delay relies on a busy loop of NOPs, instead of scheduler
180 * friendlier msleep() functions or blocking mdelay().
181 */
182static void doc_delay(struct docg3 *docg3, int nbNOPs)
183{
184 int i;
185
Robert Jarzmikac48e802011-11-19 16:02:43 +0100186 doc_vdbg("NOP x %d\n", nbNOPs);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200187 for (i = 0; i < nbNOPs; i++)
188 doc_writeb(docg3, 0, DOC_NOP);
189}
190
191static int is_prot_seq_error(struct docg3 *docg3)
192{
193 int ctrl;
194
195 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
196 return ctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR);
197}
198
199static int doc_is_ready(struct docg3 *docg3)
200{
201 int ctrl;
202
203 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
204 return ctrl & DOC_CTRL_FLASHREADY;
205}
206
207static int doc_wait_ready(struct docg3 *docg3)
208{
209 int maxWaitCycles = 100;
210
211 do {
212 doc_delay(docg3, 4);
213 cpu_relax();
214 } while (!doc_is_ready(docg3) && maxWaitCycles--);
215 doc_delay(docg3, 2);
216 if (maxWaitCycles > 0)
217 return 0;
218 else
219 return -EIO;
220}
221
222static int doc_reset_seq(struct docg3 *docg3)
223{
224 int ret;
225
226 doc_writeb(docg3, 0x10, DOC_FLASHCONTROL);
227 doc_flash_sequence(docg3, DOC_SEQ_RESET);
228 doc_flash_command(docg3, DOC_CMD_RESET);
229 doc_delay(docg3, 2);
230 ret = doc_wait_ready(docg3);
231
232 doc_dbg("doc_reset_seq() -> isReady=%s\n", ret ? "false" : "true");
233 return ret;
234}
235
236/**
237 * doc_read_data_area - Read data from data area
238 * @docg3: the device
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100239 * @buf: the buffer to fill in (might be NULL is dummy reads)
240 * @len: the length to read
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200241 * @first: first time read, DOC_READADDRESS should be set
242 *
243 * Reads bytes from flash data. Handles the single byte / even bytes reads.
244 */
245static void doc_read_data_area(struct docg3 *docg3, void *buf, int len,
246 int first)
247{
248 int i, cdr, len4;
249 u16 data16, *dst16;
250 u8 data8, *dst8;
251
252 doc_dbg("doc_read_data_area(buf=%p, len=%d)\n", buf, len);
Robert Jarzmik52c2d9a2012-04-09 13:19:08 +0200253 cdr = len & 0x1;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200254 len4 = len - cdr;
255
256 if (first)
257 doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
258 dst16 = buf;
259 for (i = 0; i < len4; i += 2) {
260 data16 = doc_readw(docg3, DOC_IOSPACE_DATA);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100261 if (dst16) {
262 *dst16 = data16;
263 dst16++;
264 }
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200265 }
266
267 if (cdr) {
268 doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
269 DOC_READADDRESS);
270 doc_delay(docg3, 1);
271 dst8 = (u8 *)dst16;
272 for (i = 0; i < cdr; i++) {
273 data8 = doc_readb(docg3, DOC_IOSPACE_DATA);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100274 if (dst8) {
275 *dst8 = data8;
276 dst8++;
277 }
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200278 }
279 }
280}
281
282/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100283 * doc_write_data_area - Write data into data area
284 * @docg3: the device
285 * @buf: the buffer to get input bytes from
286 * @len: the length to write
287 *
288 * Writes bytes into flash data. Handles the single byte / even bytes writes.
289 */
290static void doc_write_data_area(struct docg3 *docg3, const void *buf, int len)
291{
292 int i, cdr, len4;
293 u16 *src16;
294 u8 *src8;
295
296 doc_dbg("doc_write_data_area(buf=%p, len=%d)\n", buf, len);
297 cdr = len & 0x3;
298 len4 = len - cdr;
299
300 doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
301 src16 = (u16 *)buf;
302 for (i = 0; i < len4; i += 2) {
303 doc_writew(docg3, *src16, DOC_IOSPACE_DATA);
304 src16++;
305 }
306
307 src8 = (u8 *)src16;
308 for (i = 0; i < cdr; i++) {
309 doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
310 DOC_READADDRESS);
311 doc_writeb(docg3, *src8, DOC_IOSPACE_DATA);
312 src8++;
313 }
314}
315
316/**
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100317 * doc_set_data_mode - Sets the flash to normal or reliable data mode
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200318 * @docg3: the device
319 *
320 * The reliable data mode is a bit slower than the fast mode, but less errors
321 * occur. Entering the reliable mode cannot be done without entering the fast
322 * mode first.
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100323 *
324 * In reliable mode, pages 2*n and 2*n+1 are clones. Writing to page 0 of blocks
325 * (4,5) make the hardware write also to page 1 of blocks blocks(4,5). Reading
326 * from page 0 of blocks (4,5) or from page 1 of blocks (4,5) gives the same
327 * result, which is a logical and between bytes from page 0 and page 1 (which is
328 * consistent with the fact that writing to a page is _clearing_ bits of that
329 * page).
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200330 */
331static void doc_set_reliable_mode(struct docg3 *docg3)
332{
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100333 static char *strmode[] = { "normal", "fast", "reliable", "invalid" };
334
335 doc_dbg("doc_set_reliable_mode(%s)\n", strmode[docg3->reliable]);
336 switch (docg3->reliable) {
337 case 0:
338 break;
339 case 1:
340 doc_flash_sequence(docg3, DOC_SEQ_SET_FASTMODE);
341 doc_flash_command(docg3, DOC_CMD_FAST_MODE);
342 break;
343 case 2:
344 doc_flash_sequence(docg3, DOC_SEQ_SET_RELIABLEMODE);
345 doc_flash_command(docg3, DOC_CMD_FAST_MODE);
346 doc_flash_command(docg3, DOC_CMD_RELIABLE_MODE);
347 break;
348 default:
349 doc_err("doc_set_reliable_mode(): invalid mode\n");
350 break;
351 }
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200352 doc_delay(docg3, 2);
353}
354
355/**
356 * doc_set_asic_mode - Set the ASIC mode
357 * @docg3: the device
358 * @mode: the mode
359 *
360 * The ASIC can work in 3 modes :
361 * - RESET: all registers are zeroed
362 * - NORMAL: receives and handles commands
363 * - POWERDOWN: minimal poweruse, flash parts shut off
364 */
365static void doc_set_asic_mode(struct docg3 *docg3, u8 mode)
366{
367 int i;
368
369 for (i = 0; i < 12; i++)
370 doc_readb(docg3, DOC_IOSPACE_IPL);
371
372 mode |= DOC_ASICMODE_MDWREN;
373 doc_dbg("doc_set_asic_mode(%02x)\n", mode);
374 doc_writeb(docg3, mode, DOC_ASICMODE);
375 doc_writeb(docg3, ~mode, DOC_ASICMODECONFIRM);
376 doc_delay(docg3, 1);
377}
378
379/**
380 * doc_set_device_id - Sets the devices id for cascaded G3 chips
381 * @docg3: the device
382 * @id: the chip to select (amongst 0, 1, 2, 3)
383 *
384 * There can be 4 cascaded G3 chips. This function selects the one which will
385 * should be the active one.
386 */
387static void doc_set_device_id(struct docg3 *docg3, int id)
388{
389 u8 ctrl;
390
391 doc_dbg("doc_set_device_id(%d)\n", id);
392 doc_writeb(docg3, id, DOC_DEVICESELECT);
393 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
394
395 ctrl &= ~DOC_CTRL_VIOLATION;
396 ctrl |= DOC_CTRL_CE;
397 doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
398}
399
400/**
401 * doc_set_extra_page_mode - Change flash page layout
402 * @docg3: the device
403 *
404 * Normally, the flash page is split into the data (512 bytes) and the out of
405 * band data (16 bytes). For each, 4 more bytes can be accessed, where the wear
406 * leveling counters are stored. To access this last area of 4 bytes, a special
407 * mode must be input to the flash ASIC.
408 *
Octavian Moraru86d2f6f2012-03-03 16:19:39 +0200409 * Returns 0 if no error occurred, -EIO else.
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200410 */
411static int doc_set_extra_page_mode(struct docg3 *docg3)
412{
413 int fctrl;
414
415 doc_dbg("doc_set_extra_page_mode()\n");
416 doc_flash_sequence(docg3, DOC_SEQ_PAGE_SIZE_532);
417 doc_flash_command(docg3, DOC_CMD_PAGE_SIZE_532);
418 doc_delay(docg3, 2);
419
420 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
421 if (fctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR))
422 return -EIO;
423 else
424 return 0;
425}
426
427/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100428 * doc_setup_addr_sector - Setup blocks/page/ofs address for one plane
429 * @docg3: the device
430 * @sector: the sector
431 */
432static void doc_setup_addr_sector(struct docg3 *docg3, int sector)
433{
434 doc_delay(docg3, 1);
435 doc_flash_address(docg3, sector & 0xff);
436 doc_flash_address(docg3, (sector >> 8) & 0xff);
437 doc_flash_address(docg3, (sector >> 16) & 0xff);
438 doc_delay(docg3, 1);
439}
440
441/**
442 * doc_setup_writeaddr_sector - Setup blocks/page/ofs address for one plane
443 * @docg3: the device
444 * @sector: the sector
445 * @ofs: the offset in the page, between 0 and (512 + 16 + 512)
446 */
447static void doc_setup_writeaddr_sector(struct docg3 *docg3, int sector, int ofs)
448{
449 ofs = ofs >> 2;
450 doc_delay(docg3, 1);
451 doc_flash_address(docg3, ofs & 0xff);
452 doc_flash_address(docg3, sector & 0xff);
453 doc_flash_address(docg3, (sector >> 8) & 0xff);
454 doc_flash_address(docg3, (sector >> 16) & 0xff);
455 doc_delay(docg3, 1);
456}
457
458/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200459 * doc_seek - Set both flash planes to the specified block, page for reading
460 * @docg3: the device
461 * @block0: the first plane block index
462 * @block1: the second plane block index
463 * @page: the page index within the block
464 * @wear: if true, read will occur on the 4 extra bytes of the wear area
465 * @ofs: offset in page to read
466 *
467 * Programs the flash even and odd planes to the specific block and page.
468 * Alternatively, programs the flash to the wear area of the specified page.
469 */
470static int doc_read_seek(struct docg3 *docg3, int block0, int block1, int page,
471 int wear, int ofs)
472{
473 int sector, ret = 0;
474
475 doc_dbg("doc_seek(blocks=(%d,%d), page=%d, ofs=%d, wear=%d)\n",
476 block0, block1, page, ofs, wear);
477
478 if (!wear && (ofs < 2 * DOC_LAYOUT_PAGE_SIZE)) {
479 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
480 doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
481 doc_delay(docg3, 2);
482 } else {
483 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
484 doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
485 doc_delay(docg3, 2);
486 }
487
488 doc_set_reliable_mode(docg3);
489 if (wear)
490 ret = doc_set_extra_page_mode(docg3);
491 if (ret)
492 goto out;
493
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200494 doc_flash_sequence(docg3, DOC_SEQ_READ);
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100495 sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200496 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100497 doc_setup_addr_sector(docg3, sector);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200498
499 sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
500 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100501 doc_setup_addr_sector(docg3, sector);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200502 doc_delay(docg3, 1);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200503
504out:
505 return ret;
506}
507
508/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100509 * doc_write_seek - Set both flash planes to the specified block, page for writing
510 * @docg3: the device
511 * @block0: the first plane block index
512 * @block1: the second plane block index
513 * @page: the page index within the block
514 * @ofs: offset in page to write
515 *
516 * Programs the flash even and odd planes to the specific block and page.
517 * Alternatively, programs the flash to the wear area of the specified page.
518 */
519static int doc_write_seek(struct docg3 *docg3, int block0, int block1, int page,
520 int ofs)
521{
522 int ret = 0, sector;
523
524 doc_dbg("doc_write_seek(blocks=(%d,%d), page=%d, ofs=%d)\n",
525 block0, block1, page, ofs);
526
527 doc_set_reliable_mode(docg3);
528
529 if (ofs < 2 * DOC_LAYOUT_PAGE_SIZE) {
530 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
531 doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
532 doc_delay(docg3, 2);
533 } else {
534 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
535 doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
536 doc_delay(docg3, 2);
537 }
538
539 doc_flash_sequence(docg3, DOC_SEQ_PAGE_SETUP);
540 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1);
541
542 sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
543 doc_setup_writeaddr_sector(docg3, sector, ofs);
544
545 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE3);
546 doc_delay(docg3, 2);
547 ret = doc_wait_ready(docg3);
548 if (ret)
549 goto out;
550
551 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1);
552 sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
553 doc_setup_writeaddr_sector(docg3, sector, ofs);
554 doc_delay(docg3, 1);
555
556out:
557 return ret;
558}
559
560
561/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200562 * doc_read_page_ecc_init - Initialize hardware ECC engine
563 * @docg3: the device
564 * @len: the number of bytes covered by the ECC (BCH covered)
565 *
566 * The function does initialize the hardware ECC engine to compute the Hamming
Robert Jarzmikb6044362011-12-02 20:00:12 +0100567 * ECC (on 1 byte) and the BCH hardware ECC (on 7 bytes).
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200568 *
569 * Return 0 if succeeded, -EIO on error
570 */
571static int doc_read_page_ecc_init(struct docg3 *docg3, int len)
572{
573 doc_writew(docg3, DOC_ECCCONF0_READ_MODE
574 | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
575 | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
576 DOC_ECCCONF0);
577 doc_delay(docg3, 4);
578 doc_register_readb(docg3, DOC_FLASHCONTROL);
579 return doc_wait_ready(docg3);
580}
581
582/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100583 * doc_write_page_ecc_init - Initialize hardware BCH ECC engine
584 * @docg3: the device
585 * @len: the number of bytes covered by the ECC (BCH covered)
586 *
587 * The function does initialize the hardware ECC engine to compute the Hamming
Robert Jarzmikb6044362011-12-02 20:00:12 +0100588 * ECC (on 1 byte) and the BCH hardware ECC (on 7 bytes).
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100589 *
590 * Return 0 if succeeded, -EIO on error
591 */
592static int doc_write_page_ecc_init(struct docg3 *docg3, int len)
593{
Robert Jarzmikb6044362011-12-02 20:00:12 +0100594 doc_writew(docg3, DOC_ECCCONF0_WRITE_MODE
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100595 | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
596 | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
597 DOC_ECCCONF0);
598 doc_delay(docg3, 4);
599 doc_register_readb(docg3, DOC_FLASHCONTROL);
600 return doc_wait_ready(docg3);
601}
602
603/**
604 * doc_ecc_disable - Disable Hamming and BCH ECC hardware calculator
605 * @docg3: the device
606 *
607 * Disables the hardware ECC generator and checker, for unchecked reads (as when
608 * reading OOB only or write status byte).
609 */
610static void doc_ecc_disable(struct docg3 *docg3)
611{
612 doc_writew(docg3, DOC_ECCCONF0_READ_MODE, DOC_ECCCONF0);
613 doc_delay(docg3, 4);
614}
615
616/**
617 * doc_hamming_ecc_init - Initialize hardware Hamming ECC engine
618 * @docg3: the device
619 * @nb_bytes: the number of bytes covered by the ECC (Hamming covered)
620 *
621 * This function programs the ECC hardware to compute the hamming code on the
622 * last provided N bytes to the hardware generator.
623 */
624static void doc_hamming_ecc_init(struct docg3 *docg3, int nb_bytes)
625{
626 u8 ecc_conf1;
627
628 ecc_conf1 = doc_register_readb(docg3, DOC_ECCCONF1);
629 ecc_conf1 &= ~DOC_ECCCONF1_HAMMING_BITS_MASK;
630 ecc_conf1 |= (nb_bytes & DOC_ECCCONF1_HAMMING_BITS_MASK);
631 doc_writeb(docg3, ecc_conf1, DOC_ECCCONF1);
632}
633
634/**
Robert Jarzmikb6044362011-12-02 20:00:12 +0100635 * doc_ecc_bch_fix_data - Fix if need be read data from flash
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100636 * @docg3: the device
637 * @buf: the buffer of read data (512 + 7 + 1 bytes)
638 * @hwecc: the hardware calculated ECC.
639 * It's in fact recv_ecc ^ calc_ecc, where recv_ecc was read from OOB
640 * area data, and calc_ecc the ECC calculated by the hardware generator.
641 *
642 * Checks if the received data matches the ECC, and if an error is detected,
643 * tries to fix the bit flips (at most 4) in the buffer buf. As the docg3
644 * understands the (data, ecc, syndroms) in an inverted order in comparison to
645 * the BCH library, the function reverses the order of bits (ie. bit7 and bit0,
646 * bit6 and bit 1, ...) for all ECC data.
647 *
648 * The hardware ecc unit produces oob_ecc ^ calc_ecc. The kernel's bch
649 * algorithm is used to decode this. However the hw operates on page
650 * data in a bit order that is the reverse of that of the bch alg,
651 * requiring that the bits be reversed on the result. Thanks to Ivan
652 * Djelic for his analysis.
653 *
654 * Returns number of fixed bits (0, 1, 2, 3, 4) or -EBADMSG if too many bit
655 * errors were detected and cannot be fixed.
656 */
657static int doc_ecc_bch_fix_data(struct docg3 *docg3, void *buf, u8 *hwecc)
658{
659 u8 ecc[DOC_ECC_BCH_SIZE];
660 int errorpos[DOC_ECC_BCH_T], i, numerrs;
661
662 for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
663 ecc[i] = bitrev8(hwecc[i]);
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +0100664 numerrs = decode_bch(docg3->cascade->bch, NULL,
665 DOC_ECC_BCH_COVERED_BYTES,
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100666 NULL, ecc, NULL, errorpos);
667 BUG_ON(numerrs == -EINVAL);
668 if (numerrs < 0)
669 goto out;
670
671 for (i = 0; i < numerrs; i++)
672 errorpos[i] = (errorpos[i] & ~7) | (7 - (errorpos[i] & 7));
673 for (i = 0; i < numerrs; i++)
674 if (errorpos[i] < DOC_ECC_BCH_COVERED_BYTES*8)
675 /* error is located in data, correct it */
676 change_bit(errorpos[i], buf);
677out:
678 doc_dbg("doc_ecc_bch_fix_data: flipped %d bits\n", numerrs);
679 return numerrs;
680}
681
682
683/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200684 * doc_read_page_prepare - Prepares reading data from a flash page
685 * @docg3: the device
686 * @block0: the first plane block index on flash memory
687 * @block1: the second plane block index on flash memory
688 * @page: the page index in the block
689 * @offset: the offset in the page (must be a multiple of 4)
690 *
691 * Prepares the page to be read in the flash memory :
692 * - tell ASIC to map the flash pages
693 * - tell ASIC to be in read mode
694 *
695 * After a call to this method, a call to doc_read_page_finish is mandatory,
696 * to end the read cycle of the flash.
697 *
698 * Read data from a flash page. The length to be read must be between 0 and
699 * (page_size + oob_size + wear_size), ie. 532, and a multiple of 4 (because
700 * the extra bytes reading is not implemented).
701 *
702 * As pages are grouped by 2 (in 2 planes), reading from a page must be done
703 * in two steps:
704 * - one read of 512 bytes at offset 0
705 * - one read of 512 bytes at offset 512 + 16
706 *
Octavian Moraru86d2f6f2012-03-03 16:19:39 +0200707 * Returns 0 if successful, -EIO if a read error occurred.
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200708 */
709static int doc_read_page_prepare(struct docg3 *docg3, int block0, int block1,
710 int page, int offset)
711{
712 int wear_area = 0, ret = 0;
713
714 doc_dbg("doc_read_page_prepare(blocks=(%d,%d), page=%d, ofsInPage=%d)\n",
715 block0, block1, page, offset);
716 if (offset >= DOC_LAYOUT_WEAR_OFFSET)
717 wear_area = 1;
718 if (!wear_area && offset > (DOC_LAYOUT_PAGE_OOB_SIZE * 2))
719 return -EINVAL;
720
721 doc_set_device_id(docg3, docg3->device_id);
722 ret = doc_reset_seq(docg3);
723 if (ret)
724 goto err;
725
726 /* Program the flash address block and page */
727 ret = doc_read_seek(docg3, block0, block1, page, wear_area, offset);
728 if (ret)
729 goto err;
730
731 doc_flash_command(docg3, DOC_CMD_READ_ALL_PLANES);
732 doc_delay(docg3, 2);
733 doc_wait_ready(docg3);
734
735 doc_flash_command(docg3, DOC_CMD_SET_ADDR_READ);
736 doc_delay(docg3, 1);
737 if (offset >= DOC_LAYOUT_PAGE_SIZE * 2)
738 offset -= 2 * DOC_LAYOUT_PAGE_SIZE;
739 doc_flash_address(docg3, offset >> 2);
740 doc_delay(docg3, 1);
741 doc_wait_ready(docg3);
742
743 doc_flash_command(docg3, DOC_CMD_READ_FLASH);
744
745 return 0;
746err:
747 doc_writeb(docg3, 0, DOC_DATAEND);
748 doc_delay(docg3, 2);
749 return -EIO;
750}
751
752/**
753 * doc_read_page_getbytes - Reads bytes from a prepared page
754 * @docg3: the device
755 * @len: the number of bytes to be read (must be a multiple of 4)
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100756 * @buf: the buffer to be filled in (or NULL is forget bytes)
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200757 * @first: 1 if first time read, DOC_READADDRESS should be set
Robert Jarzmik52c2d9a2012-04-09 13:19:08 +0200758 * @last_odd: 1 if last read ended up on an odd byte
759 *
760 * Reads bytes from a prepared page. There is a trickery here : if the last read
761 * ended up on an odd offset in the 1024 bytes double page, ie. between the 2
762 * planes, the first byte must be read apart. If a word (16bit) read was used,
763 * the read would return the byte of plane 2 as low *and* high endian, which
764 * will mess the read.
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200765 *
766 */
767static int doc_read_page_getbytes(struct docg3 *docg3, int len, u_char *buf,
Robert Jarzmik52c2d9a2012-04-09 13:19:08 +0200768 int first, int last_odd)
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200769{
Robert Jarzmik52c2d9a2012-04-09 13:19:08 +0200770 if (last_odd && len > 0) {
771 doc_read_data_area(docg3, buf, 1, first);
772 doc_read_data_area(docg3, buf ? buf + 1 : buf, len - 1, 0);
773 } else {
774 doc_read_data_area(docg3, buf, len, first);
775 }
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200776 doc_delay(docg3, 2);
777 return len;
778}
779
780/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100781 * doc_write_page_putbytes - Writes bytes into a prepared page
782 * @docg3: the device
783 * @len: the number of bytes to be written
784 * @buf: the buffer of input bytes
785 *
786 */
787static void doc_write_page_putbytes(struct docg3 *docg3, int len,
788 const u_char *buf)
789{
790 doc_write_data_area(docg3, buf, len);
791 doc_delay(docg3, 2);
792}
793
794/**
Robert Jarzmikb6044362011-12-02 20:00:12 +0100795 * doc_get_bch_hw_ecc - Get hardware calculated BCH ECC
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200796 * @docg3: the device
Robert Jarzmikb6044362011-12-02 20:00:12 +0100797 * @hwecc: the array of 7 integers where the hardware ecc will be stored
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200798 */
Robert Jarzmikb6044362011-12-02 20:00:12 +0100799static void doc_get_bch_hw_ecc(struct docg3 *docg3, u8 *hwecc)
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200800{
801 int i;
802
803 for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
Robert Jarzmikb6044362011-12-02 20:00:12 +0100804 hwecc[i] = doc_register_readb(docg3, DOC_BCH_HW_ECC(i));
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200805}
806
807/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100808 * doc_page_finish - Ends reading/writing of a flash page
809 * @docg3: the device
810 */
811static void doc_page_finish(struct docg3 *docg3)
812{
813 doc_writeb(docg3, 0, DOC_DATAEND);
814 doc_delay(docg3, 2);
815}
816
817/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200818 * doc_read_page_finish - Ends reading of a flash page
819 * @docg3: the device
820 *
821 * As a side effect, resets the chip selector to 0. This ensures that after each
822 * read operation, the floor 0 is selected. Therefore, if the systems halts, the
823 * reboot will boot on floor 0, where the IPL is.
824 */
825static void doc_read_page_finish(struct docg3 *docg3)
826{
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100827 doc_page_finish(docg3);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200828 doc_set_device_id(docg3, 0);
829}
830
831/**
832 * calc_block_sector - Calculate blocks, pages and ofs.
833
834 * @from: offset in flash
835 * @block0: first plane block index calculated
836 * @block1: second plane block index calculated
837 * @page: page calculated
838 * @ofs: offset in page
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100839 * @reliable: 0 if docg3 in normal mode, 1 if docg3 in fast mode, 2 if docg3 in
840 * reliable mode.
841 *
842 * The calculation is based on the reliable/normal mode. In normal mode, the 64
843 * pages of a block are available. In reliable mode, as pages 2*n and 2*n+1 are
844 * clones, only 32 pages per block are available.
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200845 */
846static void calc_block_sector(loff_t from, int *block0, int *block1, int *page,
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100847 int *ofs, int reliable)
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200848{
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100849 uint sector, pages_biblock;
850
851 pages_biblock = DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_NBPLANES;
852 if (reliable == 1 || reliable == 2)
853 pages_biblock /= 2;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200854
855 sector = from / DOC_LAYOUT_PAGE_SIZE;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100856 *block0 = sector / pages_biblock * DOC_LAYOUT_NBPLANES;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200857 *block1 = *block0 + 1;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100858 *page = sector % pages_biblock;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200859 *page /= DOC_LAYOUT_NBPLANES;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100860 if (reliable == 1 || reliable == 2)
861 *page *= 2;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200862 if (sector % 2)
863 *ofs = DOC_LAYOUT_PAGE_OOB_SIZE;
864 else
865 *ofs = 0;
866}
867
868/**
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100869 * doc_read_oob - Read out of band bytes from flash
870 * @mtd: the device
871 * @from: the offset from first block and first page, in bytes, aligned on page
872 * size
873 * @ops: the mtd oob structure
874 *
875 * Reads flash memory OOB area of pages.
876 *
Octavian Moraru86d2f6f2012-03-03 16:19:39 +0200877 * Returns 0 if read successful, of -EIO, -EINVAL if an error occurred
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100878 */
879static int doc_read_oob(struct mtd_info *mtd, loff_t from,
880 struct mtd_oob_ops *ops)
881{
882 struct docg3 *docg3 = mtd->priv;
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100883 int block0, block1, page, ret, skip, ofs = 0;
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100884 u8 *oobbuf = ops->oobbuf;
885 u8 *buf = ops->datbuf;
886 size_t len, ooblen, nbdata, nboob;
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100887 u8 hwecc[DOC_ECC_BCH_SIZE], eccconf1;
Mike Dunnedbc45402012-04-25 12:06:11 -0700888 int max_bitflips = 0;
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100889
890 if (buf)
891 len = ops->len;
892 else
893 len = 0;
894 if (oobbuf)
895 ooblen = ops->ooblen;
896 else
897 ooblen = 0;
898
899 if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
900 oobbuf += ops->ooboffs;
901
902 doc_dbg("doc_read_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
903 from, ops->mode, buf, len, oobbuf, ooblen);
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100904 if (ooblen % DOC_LAYOUT_OOB_SIZE)
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100905 return -EINVAL;
906
Robert Jarzmika7baef12012-03-22 21:00:50 +0100907 if (from + len > mtd->size)
908 return -EINVAL;
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100909
910 ops->oobretlen = 0;
911 ops->retlen = 0;
912 ret = 0;
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100913 skip = from % DOC_LAYOUT_PAGE_SIZE;
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +0100914 mutex_lock(&docg3->cascade->lock);
Mike Dunnedbc45402012-04-25 12:06:11 -0700915 while (ret >= 0 && (len > 0 || ooblen > 0)) {
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100916 calc_block_sector(from - skip, &block0, &block1, &page, &ofs,
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100917 docg3->reliable);
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100918 nbdata = min_t(size_t, len, DOC_LAYOUT_PAGE_SIZE - skip);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100919 nboob = min_t(size_t, ooblen, (size_t)DOC_LAYOUT_OOB_SIZE);
920 ret = doc_read_page_prepare(docg3, block0, block1, page, ofs);
921 if (ret < 0)
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +0100922 goto out;
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100923 ret = doc_read_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100924 if (ret < 0)
925 goto err_in_read;
Robert Jarzmik52c2d9a2012-04-09 13:19:08 +0200926 ret = doc_read_page_getbytes(docg3, skip, NULL, 1, 0);
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100927 if (ret < skip)
928 goto err_in_read;
Robert Jarzmik52c2d9a2012-04-09 13:19:08 +0200929 ret = doc_read_page_getbytes(docg3, nbdata, buf, 0, skip % 2);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100930 if (ret < nbdata)
931 goto err_in_read;
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100932 doc_read_page_getbytes(docg3,
933 DOC_LAYOUT_PAGE_SIZE - nbdata - skip,
Robert Jarzmik52c2d9a2012-04-09 13:19:08 +0200934 NULL, 0, (skip + nbdata) % 2);
935 ret = doc_read_page_getbytes(docg3, nboob, oobbuf, 0, 0);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100936 if (ret < nboob)
937 goto err_in_read;
938 doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE - nboob,
Robert Jarzmik52c2d9a2012-04-09 13:19:08 +0200939 NULL, 0, nboob % 2);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100940
Robert Jarzmikb6044362011-12-02 20:00:12 +0100941 doc_get_bch_hw_ecc(docg3, hwecc);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100942 eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1);
943
944 if (nboob >= DOC_LAYOUT_OOB_SIZE) {
Andy Shevchenko13e85972012-08-02 16:06:47 +0300945 doc_dbg("OOB - INFO: %*phC\n", 7, oobbuf);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100946 doc_dbg("OOB - HAMMING: %02x\n", oobbuf[7]);
Andy Shevchenko13e85972012-08-02 16:06:47 +0300947 doc_dbg("OOB - BCH_ECC: %*phC\n", 7, oobbuf + 8);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100948 doc_dbg("OOB - UNUSED: %02x\n", oobbuf[15]);
949 }
950 doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1);
Andy Shevchenko13e85972012-08-02 16:06:47 +0300951 doc_dbg("ECC HW_ECC: %*phC\n", 7, hwecc);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100952
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100953 ret = -EIO;
954 if (is_prot_seq_error(docg3))
955 goto err_in_read;
956 ret = 0;
957 if ((block0 >= DOC_LAYOUT_BLOCK_FIRST_DATA) &&
958 (eccconf1 & DOC_ECCCONF1_BCH_SYNDROM_ERR) &&
959 (eccconf1 & DOC_ECCCONF1_PAGE_IS_WRITTEN) &&
960 (ops->mode != MTD_OPS_RAW) &&
961 (nbdata == DOC_LAYOUT_PAGE_SIZE)) {
962 ret = doc_ecc_bch_fix_data(docg3, buf, hwecc);
963 if (ret < 0) {
964 mtd->ecc_stats.failed++;
965 ret = -EBADMSG;
966 }
967 if (ret > 0) {
968 mtd->ecc_stats.corrected += ret;
Mike Dunnedbc45402012-04-25 12:06:11 -0700969 max_bitflips = max(max_bitflips, ret);
970 ret = max_bitflips;
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100971 }
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100972 }
973
974 doc_read_page_finish(docg3);
975 ops->retlen += nbdata;
976 ops->oobretlen += nboob;
977 buf += nbdata;
978 oobbuf += nboob;
979 len -= nbdata;
980 ooblen -= nboob;
981 from += DOC_LAYOUT_PAGE_SIZE;
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100982 skip = 0;
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100983 }
984
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +0100985out:
986 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100987 return ret;
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100988err_in_read:
989 doc_read_page_finish(docg3);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +0100990 goto out;
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100991}
992
993/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200994 * doc_read - Read bytes from flash
995 * @mtd: the device
996 * @from: the offset from first block and first page, in bytes, aligned on page
997 * size
998 * @len: the number of bytes to read (must be a multiple of 4)
999 * @retlen: the number of bytes actually read
1000 * @buf: the filled in buffer
1001 *
1002 * Reads flash memory pages. This function does not read the OOB chunk, but only
1003 * the page data.
1004 *
Octavian Moraru86d2f6f2012-03-03 16:19:39 +02001005 * Returns 0 if read successful, of -EIO, -EINVAL if an error occurred
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001006 */
1007static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
1008 size_t *retlen, u_char *buf)
1009{
Robert Jarzmik32a50b32011-11-19 16:02:47 +01001010 struct mtd_oob_ops ops;
1011 size_t ret;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001012
Robert Jarzmik32a50b32011-11-19 16:02:47 +01001013 memset(&ops, 0, sizeof(ops));
1014 ops.datbuf = buf;
1015 ops.len = len;
1016 ops.mode = MTD_OPS_AUTO_OOB;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001017
Robert Jarzmik32a50b32011-11-19 16:02:47 +01001018 ret = doc_read_oob(mtd, from, &ops);
1019 *retlen = ops.retlen;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001020 return ret;
1021}
1022
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001023static int doc_reload_bbt(struct docg3 *docg3)
1024{
1025 int block = DOC_LAYOUT_BLOCK_BBT;
1026 int ret = 0, nbpages, page;
1027 u_char *buf = docg3->bbt;
1028
1029 nbpages = DIV_ROUND_UP(docg3->max_block + 1, 8 * DOC_LAYOUT_PAGE_SIZE);
1030 for (page = 0; !ret && (page < nbpages); page++) {
1031 ret = doc_read_page_prepare(docg3, block, block + 1,
1032 page + DOC_LAYOUT_PAGE_BBT, 0);
1033 if (!ret)
1034 ret = doc_read_page_ecc_init(docg3,
1035 DOC_LAYOUT_PAGE_SIZE);
1036 if (!ret)
1037 doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE,
Robert Jarzmik52c2d9a2012-04-09 13:19:08 +02001038 buf, 1, 0);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001039 buf += DOC_LAYOUT_PAGE_SIZE;
1040 }
1041 doc_read_page_finish(docg3);
1042 return ret;
1043}
1044
1045/**
1046 * doc_block_isbad - Checks whether a block is good or not
1047 * @mtd: the device
1048 * @from: the offset to find the correct block
1049 *
1050 * Returns 1 if block is bad, 0 if block is good
1051 */
1052static int doc_block_isbad(struct mtd_info *mtd, loff_t from)
1053{
1054 struct docg3 *docg3 = mtd->priv;
1055 int block0, block1, page, ofs, is_good;
1056
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001057 calc_block_sector(from, &block0, &block1, &page, &ofs,
1058 docg3->reliable);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001059 doc_dbg("doc_block_isbad(from=%lld) => block=(%d,%d), page=%d, ofs=%d\n",
1060 from, block0, block1, page, ofs);
1061
1062 if (block0 < DOC_LAYOUT_BLOCK_FIRST_DATA)
1063 return 0;
1064 if (block1 > docg3->max_block)
1065 return -EINVAL;
1066
1067 is_good = docg3->bbt[block0 >> 3] & (1 << (block0 & 0x7));
1068 return !is_good;
1069}
1070
Robert Jarzmike10019b2011-12-16 23:25:23 +01001071#if 0
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001072/**
1073 * doc_get_erase_count - Get block erase count
1074 * @docg3: the device
1075 * @from: the offset in which the block is.
1076 *
1077 * Get the number of times a block was erased. The number is the maximum of
1078 * erase times between first and second plane (which should be equal normally).
1079 *
1080 * Returns The number of erases, or -EINVAL or -EIO on error.
1081 */
1082static int doc_get_erase_count(struct docg3 *docg3, loff_t from)
1083{
1084 u8 buf[DOC_LAYOUT_WEAR_SIZE];
1085 int ret, plane1_erase_count, plane2_erase_count;
1086 int block0, block1, page, ofs;
1087
1088 doc_dbg("doc_get_erase_count(from=%lld, buf=%p)\n", from, buf);
1089 if (from % DOC_LAYOUT_PAGE_SIZE)
1090 return -EINVAL;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001091 calc_block_sector(from, &block0, &block1, &page, &ofs, docg3->reliable);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001092 if (block1 > docg3->max_block)
1093 return -EINVAL;
1094
1095 ret = doc_reset_seq(docg3);
1096 if (!ret)
1097 ret = doc_read_page_prepare(docg3, block0, block1, page,
Robert Jarzmik52c2d9a2012-04-09 13:19:08 +02001098 ofs + DOC_LAYOUT_WEAR_OFFSET, 0);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001099 if (!ret)
1100 ret = doc_read_page_getbytes(docg3, DOC_LAYOUT_WEAR_SIZE,
Robert Jarzmik52c2d9a2012-04-09 13:19:08 +02001101 buf, 1, 0);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001102 doc_read_page_finish(docg3);
1103
1104 if (ret || (buf[0] != DOC_ERASE_MARK) || (buf[2] != DOC_ERASE_MARK))
1105 return -EIO;
1106 plane1_erase_count = (u8)(~buf[1]) | ((u8)(~buf[4]) << 8)
1107 | ((u8)(~buf[5]) << 16);
1108 plane2_erase_count = (u8)(~buf[3]) | ((u8)(~buf[6]) << 8)
1109 | ((u8)(~buf[7]) << 16);
1110
1111 return max(plane1_erase_count, plane2_erase_count);
1112}
Robert Jarzmike10019b2011-12-16 23:25:23 +01001113#endif
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001114
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001115/**
1116 * doc_get_op_status - get erase/write operation status
1117 * @docg3: the device
1118 *
1119 * Queries the status from the chip, and returns it
1120 *
1121 * Returns the status (bits DOC_PLANES_STATUS_*)
1122 */
1123static int doc_get_op_status(struct docg3 *docg3)
1124{
1125 u8 status;
1126
1127 doc_flash_sequence(docg3, DOC_SEQ_PLANES_STATUS);
1128 doc_flash_command(docg3, DOC_CMD_PLANES_STATUS);
1129 doc_delay(docg3, 5);
1130
1131 doc_ecc_disable(docg3);
1132 doc_read_data_area(docg3, &status, 1, 1);
1133 return status;
1134}
1135
1136/**
1137 * doc_write_erase_wait_status - wait for write or erase completion
1138 * @docg3: the device
1139 *
1140 * Wait for the chip to be ready again after erase or write operation, and check
1141 * erase/write status.
1142 *
Octavian Moraru86d2f6f2012-03-03 16:19:39 +02001143 * Returns 0 if erase successful, -EIO if erase/write issue, -ETIMEOUT if
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001144 * timeout
1145 */
1146static int doc_write_erase_wait_status(struct docg3 *docg3)
1147{
Robert Jarzmika2b3d282012-03-22 21:00:51 +01001148 int i, status, ret = 0;
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001149
Robert Jarzmika2b3d282012-03-22 21:00:51 +01001150 for (i = 0; !doc_is_ready(docg3) && i < 5; i++)
1151 msleep(20);
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001152 if (!doc_is_ready(docg3)) {
1153 doc_dbg("Timeout reached and the chip is still not ready\n");
1154 ret = -EAGAIN;
1155 goto out;
1156 }
1157
1158 status = doc_get_op_status(docg3);
1159 if (status & DOC_PLANES_STATUS_FAIL) {
1160 doc_dbg("Erase/Write failed on (a) plane(s), status = %x\n",
1161 status);
1162 ret = -EIO;
1163 }
1164
1165out:
1166 doc_page_finish(docg3);
1167 return ret;
1168}
1169
1170/**
Robert Jarzmikde03cd72011-11-19 16:02:53 +01001171 * doc_erase_block - Erase a couple of blocks
1172 * @docg3: the device
1173 * @block0: the first block to erase (leftmost plane)
1174 * @block1: the second block to erase (rightmost plane)
1175 *
1176 * Erase both blocks, and return operation status
1177 *
1178 * Returns 0 if erase successful, -EIO if erase issue, -ETIMEOUT if chip not
1179 * ready for too long
1180 */
1181static int doc_erase_block(struct docg3 *docg3, int block0, int block1)
1182{
1183 int ret, sector;
1184
1185 doc_dbg("doc_erase_block(blocks=(%d,%d))\n", block0, block1);
1186 ret = doc_reset_seq(docg3);
1187 if (ret)
1188 return -EIO;
1189
1190 doc_set_reliable_mode(docg3);
1191 doc_flash_sequence(docg3, DOC_SEQ_ERASE);
1192
1193 sector = block0 << DOC_ADDR_BLOCK_SHIFT;
1194 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
1195 doc_setup_addr_sector(docg3, sector);
1196 sector = block1 << DOC_ADDR_BLOCK_SHIFT;
1197 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
1198 doc_setup_addr_sector(docg3, sector);
1199 doc_delay(docg3, 1);
1200
1201 doc_flash_command(docg3, DOC_CMD_ERASECYCLE2);
1202 doc_delay(docg3, 2);
1203
1204 if (is_prot_seq_error(docg3)) {
1205 doc_err("Erase blocks %d,%d error\n", block0, block1);
1206 return -EIO;
1207 }
1208
1209 return doc_write_erase_wait_status(docg3);
1210}
1211
1212/**
1213 * doc_erase - Erase a portion of the chip
1214 * @mtd: the device
1215 * @info: the erase info
1216 *
1217 * Erase a bunch of contiguous blocks, by pairs, as a "mtd" page of 1024 is
1218 * split into 2 pages of 512 bytes on 2 contiguous blocks.
1219 *
Octavian Moraru86d2f6f2012-03-03 16:19:39 +02001220 * Returns 0 if erase successful, -EINVAL if addressing error, -EIO if erase
Robert Jarzmikde03cd72011-11-19 16:02:53 +01001221 * issue
1222 */
1223static int doc_erase(struct mtd_info *mtd, struct erase_info *info)
1224{
1225 struct docg3 *docg3 = mtd->priv;
1226 uint64_t len;
1227 int block0, block1, page, ret, ofs = 0;
1228
1229 doc_dbg("doc_erase(from=%lld, len=%lld\n", info->addr, info->len);
Robert Jarzmikde03cd72011-11-19 16:02:53 +01001230
1231 info->state = MTD_ERASE_PENDING;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001232 calc_block_sector(info->addr + info->len, &block0, &block1, &page,
1233 &ofs, docg3->reliable);
Robert Jarzmikde03cd72011-11-19 16:02:53 +01001234 ret = -EINVAL;
Robert Jarzmika7baef12012-03-22 21:00:50 +01001235 if (info->addr + info->len > mtd->size || page || ofs)
Robert Jarzmikde03cd72011-11-19 16:02:53 +01001236 goto reset_err;
1237
1238 ret = 0;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001239 calc_block_sector(info->addr, &block0, &block1, &page, &ofs,
1240 docg3->reliable);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001241 mutex_lock(&docg3->cascade->lock);
1242 doc_set_device_id(docg3, docg3->device_id);
Robert Jarzmikde03cd72011-11-19 16:02:53 +01001243 doc_set_reliable_mode(docg3);
1244 for (len = info->len; !ret && len > 0; len -= mtd->erasesize) {
1245 info->state = MTD_ERASING;
1246 ret = doc_erase_block(docg3, block0, block1);
1247 block0 += 2;
1248 block1 += 2;
1249 }
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001250 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmikde03cd72011-11-19 16:02:53 +01001251
1252 if (ret)
1253 goto reset_err;
1254
1255 info->state = MTD_ERASE_DONE;
1256 return 0;
1257
1258reset_err:
1259 info->state = MTD_ERASE_FAILED;
1260 return ret;
1261}
1262
1263/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001264 * doc_write_page - Write a single page to the chip
1265 * @docg3: the device
1266 * @to: the offset from first block and first page, in bytes, aligned on page
1267 * size
1268 * @buf: buffer to get bytes from
1269 * @oob: buffer to get out of band bytes from (can be NULL if no OOB should be
1270 * written)
1271 * @autoecc: if 0, all 16 bytes from OOB are taken, regardless of HW Hamming or
1272 * BCH computations. If 1, only bytes 0-7 and byte 15 are taken,
1273 * remaining ones are filled with hardware Hamming and BCH
1274 * computations. Its value is not meaningfull is oob == NULL.
1275 *
1276 * Write one full page (ie. 1 page split on two planes), of 512 bytes, with the
1277 * OOB data. The OOB ECC is automatically computed by the hardware Hamming and
1278 * BCH generator if autoecc is not null.
1279 *
1280 * Returns 0 if write successful, -EIO if write error, -EAGAIN if timeout
1281 */
1282static int doc_write_page(struct docg3 *docg3, loff_t to, const u_char *buf,
1283 const u_char *oob, int autoecc)
1284{
1285 int block0, block1, page, ret, ofs = 0;
Robert Jarzmikb6044362011-12-02 20:00:12 +01001286 u8 hwecc[DOC_ECC_BCH_SIZE], hamming;
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001287
1288 doc_dbg("doc_write_page(to=%lld)\n", to);
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001289 calc_block_sector(to, &block0, &block1, &page, &ofs, docg3->reliable);
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001290
1291 doc_set_device_id(docg3, docg3->device_id);
1292 ret = doc_reset_seq(docg3);
1293 if (ret)
1294 goto err;
1295
1296 /* Program the flash address block and page */
1297 ret = doc_write_seek(docg3, block0, block1, page, ofs);
1298 if (ret)
1299 goto err;
1300
Robert Jarzmikd13d19e2011-11-19 16:02:55 +01001301 doc_write_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001302 doc_delay(docg3, 2);
1303 doc_write_page_putbytes(docg3, DOC_LAYOUT_PAGE_SIZE, buf);
1304
1305 if (oob && autoecc) {
1306 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ, oob);
1307 doc_delay(docg3, 2);
1308 oob += DOC_LAYOUT_OOB_UNUSED_OFS;
1309
1310 hamming = doc_register_readb(docg3, DOC_HAMMINGPARITY);
1311 doc_delay(docg3, 2);
1312 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_HAMMING_SZ,
1313 &hamming);
1314 doc_delay(docg3, 2);
1315
Robert Jarzmikb6044362011-12-02 20:00:12 +01001316 doc_get_bch_hw_ecc(docg3, hwecc);
1317 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_BCH_SZ, hwecc);
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001318 doc_delay(docg3, 2);
1319
1320 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_UNUSED_SZ, oob);
1321 }
1322 if (oob && !autoecc)
1323 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_SIZE, oob);
1324
1325 doc_delay(docg3, 2);
1326 doc_page_finish(docg3);
1327 doc_delay(docg3, 2);
1328 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE2);
1329 doc_delay(docg3, 2);
1330
1331 /*
1332 * The wait status will perform another doc_page_finish() call, but that
1333 * seems to please the docg3, so leave it.
1334 */
1335 ret = doc_write_erase_wait_status(docg3);
1336 return ret;
1337err:
1338 doc_read_page_finish(docg3);
1339 return ret;
1340}
1341
1342/**
1343 * doc_guess_autoecc - Guess autoecc mode from mbd_oob_ops
1344 * @ops: the oob operations
1345 *
1346 * Returns 0 or 1 if success, -EINVAL if invalid oob mode
1347 */
1348static int doc_guess_autoecc(struct mtd_oob_ops *ops)
1349{
1350 int autoecc;
1351
1352 switch (ops->mode) {
1353 case MTD_OPS_PLACE_OOB:
1354 case MTD_OPS_AUTO_OOB:
1355 autoecc = 1;
1356 break;
1357 case MTD_OPS_RAW:
1358 autoecc = 0;
1359 break;
1360 default:
1361 autoecc = -EINVAL;
1362 }
1363 return autoecc;
1364}
1365
1366/**
1367 * doc_fill_autooob - Fill a 16 bytes OOB from 8 non-ECC bytes
1368 * @dst: the target 16 bytes OOB buffer
1369 * @oobsrc: the source 8 bytes non-ECC OOB buffer
1370 *
1371 */
1372static void doc_fill_autooob(u8 *dst, u8 *oobsrc)
1373{
1374 memcpy(dst, oobsrc, DOC_LAYOUT_OOB_PAGEINFO_SZ);
1375 dst[DOC_LAYOUT_OOB_UNUSED_OFS] = oobsrc[DOC_LAYOUT_OOB_PAGEINFO_SZ];
1376}
1377
1378/**
1379 * doc_backup_oob - Backup OOB into docg3 structure
1380 * @docg3: the device
1381 * @to: the page offset in the chip
1382 * @ops: the OOB size and buffer
1383 *
1384 * As the docg3 should write a page with its OOB in one pass, and some userland
1385 * applications do write_oob() to setup the OOB and then write(), store the OOB
1386 * into a temporary storage. This is very dangerous, as 2 concurrent
1387 * applications could store an OOB, and then write their pages (which will
1388 * result into one having its OOB corrupted).
1389 *
1390 * The only reliable way would be for userland to call doc_write_oob() with both
1391 * the page data _and_ the OOB area.
1392 *
1393 * Returns 0 if success, -EINVAL if ops content invalid
1394 */
1395static int doc_backup_oob(struct docg3 *docg3, loff_t to,
1396 struct mtd_oob_ops *ops)
1397{
1398 int ooblen = ops->ooblen, autoecc;
1399
1400 if (ooblen != DOC_LAYOUT_OOB_SIZE)
1401 return -EINVAL;
1402 autoecc = doc_guess_autoecc(ops);
1403 if (autoecc < 0)
1404 return autoecc;
1405
1406 docg3->oob_write_ofs = to;
1407 docg3->oob_autoecc = autoecc;
1408 if (ops->mode == MTD_OPS_AUTO_OOB) {
1409 doc_fill_autooob(docg3->oob_write_buf, ops->oobbuf);
1410 ops->oobretlen = 8;
1411 } else {
1412 memcpy(docg3->oob_write_buf, ops->oobbuf, DOC_LAYOUT_OOB_SIZE);
1413 ops->oobretlen = DOC_LAYOUT_OOB_SIZE;
1414 }
1415 return 0;
1416}
1417
1418/**
1419 * doc_write_oob - Write out of band bytes to flash
1420 * @mtd: the device
1421 * @ofs: the offset from first block and first page, in bytes, aligned on page
1422 * size
1423 * @ops: the mtd oob structure
1424 *
1425 * Either write OOB data into a temporary buffer, for the subsequent write
1426 * page. The provided OOB should be 16 bytes long. If a data buffer is provided
1427 * as well, issue the page write.
1428 * Or provide data without OOB, and then a all zeroed OOB will be used (ECC will
1429 * still be filled in if asked for).
1430 *
Octavian Moraru86d2f6f2012-03-03 16:19:39 +02001431 * Returns 0 is successful, EINVAL if length is not 14 bytes
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001432 */
1433static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
1434 struct mtd_oob_ops *ops)
1435{
1436 struct docg3 *docg3 = mtd->priv;
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001437 int ret, autoecc, oobdelta;
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001438 u8 *oobbuf = ops->oobbuf;
1439 u8 *buf = ops->datbuf;
1440 size_t len, ooblen;
1441 u8 oob[DOC_LAYOUT_OOB_SIZE];
1442
1443 if (buf)
1444 len = ops->len;
1445 else
1446 len = 0;
1447 if (oobbuf)
1448 ooblen = ops->ooblen;
1449 else
1450 ooblen = 0;
1451
1452 if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
1453 oobbuf += ops->ooboffs;
1454
1455 doc_dbg("doc_write_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
1456 ofs, ops->mode, buf, len, oobbuf, ooblen);
1457 switch (ops->mode) {
1458 case MTD_OPS_PLACE_OOB:
1459 case MTD_OPS_RAW:
1460 oobdelta = mtd->oobsize;
1461 break;
1462 case MTD_OPS_AUTO_OOB:
Boris BREZILLONf5b8aa72016-03-07 10:46:51 +01001463 oobdelta = mtd->oobavail;
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001464 break;
1465 default:
Dan Carpenter6c810f92012-11-29 17:16:51 +03001466 return -EINVAL;
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001467 }
1468 if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % oobdelta) ||
1469 (ofs % DOC_LAYOUT_PAGE_SIZE))
1470 return -EINVAL;
1471 if (len && ooblen &&
1472 (len / DOC_LAYOUT_PAGE_SIZE) != (ooblen / oobdelta))
1473 return -EINVAL;
Robert Jarzmika7baef12012-03-22 21:00:50 +01001474 if (ofs + len > mtd->size)
1475 return -EINVAL;
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001476
1477 ops->oobretlen = 0;
1478 ops->retlen = 0;
1479 ret = 0;
1480 if (len == 0 && ooblen == 0)
1481 return -EINVAL;
1482 if (len == 0 && ooblen > 0)
1483 return doc_backup_oob(docg3, ofs, ops);
1484
1485 autoecc = doc_guess_autoecc(ops);
1486 if (autoecc < 0)
1487 return autoecc;
1488
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001489 mutex_lock(&docg3->cascade->lock);
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001490 while (!ret && len > 0) {
1491 memset(oob, 0, sizeof(oob));
1492 if (ofs == docg3->oob_write_ofs)
1493 memcpy(oob, docg3->oob_write_buf, DOC_LAYOUT_OOB_SIZE);
1494 else if (ooblen > 0 && ops->mode == MTD_OPS_AUTO_OOB)
1495 doc_fill_autooob(oob, oobbuf);
1496 else if (ooblen > 0)
1497 memcpy(oob, oobbuf, DOC_LAYOUT_OOB_SIZE);
1498 ret = doc_write_page(docg3, ofs, buf, oob, autoecc);
1499
1500 ofs += DOC_LAYOUT_PAGE_SIZE;
1501 len -= DOC_LAYOUT_PAGE_SIZE;
1502 buf += DOC_LAYOUT_PAGE_SIZE;
1503 if (ooblen) {
1504 oobbuf += oobdelta;
1505 ooblen -= oobdelta;
1506 ops->oobretlen += oobdelta;
1507 }
1508 ops->retlen += DOC_LAYOUT_PAGE_SIZE;
1509 }
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001510
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001511 doc_set_device_id(docg3, 0);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001512 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001513 return ret;
1514}
1515
1516/**
1517 * doc_write - Write a buffer to the chip
1518 * @mtd: the device
1519 * @to: the offset from first block and first page, in bytes, aligned on page
1520 * size
1521 * @len: the number of bytes to write (must be a full page size, ie. 512)
1522 * @retlen: the number of bytes actually written (0 or 512)
1523 * @buf: the buffer to get bytes from
1524 *
1525 * Writes data to the chip.
1526 *
1527 * Returns 0 if write successful, -EIO if write error
1528 */
1529static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
1530 size_t *retlen, const u_char *buf)
1531{
1532 struct docg3 *docg3 = mtd->priv;
1533 int ret;
1534 struct mtd_oob_ops ops;
1535
1536 doc_dbg("doc_write(to=%lld, len=%zu)\n", to, len);
1537 ops.datbuf = (char *)buf;
1538 ops.len = len;
1539 ops.mode = MTD_OPS_PLACE_OOB;
1540 ops.oobbuf = NULL;
1541 ops.ooblen = 0;
1542 ops.ooboffs = 0;
1543
1544 ret = doc_write_oob(mtd, to, &ops);
1545 *retlen = ops.retlen;
1546 return ret;
1547}
1548
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001549static struct docg3 *sysfs_dev2docg3(struct device *dev,
1550 struct device_attribute *attr)
1551{
1552 int floor;
1553 struct platform_device *pdev = to_platform_device(dev);
1554 struct mtd_info **docg3_floors = platform_get_drvdata(pdev);
1555
1556 floor = attr->attr.name[1] - '0';
1557 if (floor < 0 || floor >= DOC_MAX_NBFLOORS)
1558 return NULL;
1559 else
1560 return docg3_floors[floor]->priv;
1561}
1562
1563static ssize_t dps0_is_key_locked(struct device *dev,
1564 struct device_attribute *attr, char *buf)
1565{
1566 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1567 int dps0;
1568
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001569 mutex_lock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001570 doc_set_device_id(docg3, docg3->device_id);
1571 dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
1572 doc_set_device_id(docg3, 0);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001573 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001574
1575 return sprintf(buf, "%d\n", !(dps0 & DOC_DPS_KEY_OK));
1576}
1577
1578static ssize_t dps1_is_key_locked(struct device *dev,
1579 struct device_attribute *attr, char *buf)
1580{
1581 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1582 int dps1;
1583
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001584 mutex_lock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001585 doc_set_device_id(docg3, docg3->device_id);
1586 dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
1587 doc_set_device_id(docg3, 0);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001588 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001589
1590 return sprintf(buf, "%d\n", !(dps1 & DOC_DPS_KEY_OK));
1591}
1592
1593static ssize_t dps0_insert_key(struct device *dev,
1594 struct device_attribute *attr,
1595 const char *buf, size_t count)
1596{
1597 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1598 int i;
1599
1600 if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
1601 return -EINVAL;
1602
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001603 mutex_lock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001604 doc_set_device_id(docg3, docg3->device_id);
1605 for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
1606 doc_writeb(docg3, buf[i], DOC_DPS0_KEY);
1607 doc_set_device_id(docg3, 0);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001608 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001609 return count;
1610}
1611
1612static ssize_t dps1_insert_key(struct device *dev,
1613 struct device_attribute *attr,
1614 const char *buf, size_t count)
1615{
1616 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1617 int i;
1618
1619 if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
1620 return -EINVAL;
1621
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001622 mutex_lock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001623 doc_set_device_id(docg3, docg3->device_id);
1624 for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
1625 doc_writeb(docg3, buf[i], DOC_DPS1_KEY);
1626 doc_set_device_id(docg3, 0);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001627 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001628 return count;
1629}
1630
1631#define FLOOR_SYSFS(id) { \
1632 __ATTR(f##id##_dps0_is_keylocked, S_IRUGO, dps0_is_key_locked, NULL), \
1633 __ATTR(f##id##_dps1_is_keylocked, S_IRUGO, dps1_is_key_locked, NULL), \
Rusty Russell993bcc62014-05-14 10:33:47 +09301634 __ATTR(f##id##_dps0_protection_key, S_IWUSR|S_IWGRP, NULL, dps0_insert_key), \
1635 __ATTR(f##id##_dps1_protection_key, S_IWUSR|S_IWGRP, NULL, dps1_insert_key), \
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001636}
1637
1638static struct device_attribute doc_sys_attrs[DOC_MAX_NBFLOORS][4] = {
1639 FLOOR_SYSFS(0), FLOOR_SYSFS(1), FLOOR_SYSFS(2), FLOOR_SYSFS(3)
1640};
1641
1642static int doc_register_sysfs(struct platform_device *pdev,
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001643 struct docg3_cascade *cascade)
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001644{
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001645 struct device *dev = &pdev->dev;
Dan Carpenter23829602015-10-19 13:20:05 +03001646 int floor;
1647 int ret;
1648 int i;
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001649
Dan Carpenter23829602015-10-19 13:20:05 +03001650 for (floor = 0;
1651 floor < DOC_MAX_NBFLOORS && cascade->floors[floor];
1652 floor++) {
1653 for (i = 0; i < 4; i++) {
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001654 ret = device_create_file(dev, &doc_sys_attrs[floor][i]);
Dan Carpenter23829602015-10-19 13:20:05 +03001655 if (ret)
1656 goto remove_files;
1657 }
1658 }
1659
1660 return 0;
1661
1662remove_files:
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001663 do {
1664 while (--i >= 0)
1665 device_remove_file(dev, &doc_sys_attrs[floor][i]);
1666 i = 4;
1667 } while (--floor >= 0);
Dan Carpenter23829602015-10-19 13:20:05 +03001668
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001669 return ret;
1670}
1671
1672static void doc_unregister_sysfs(struct platform_device *pdev,
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001673 struct docg3_cascade *cascade)
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001674{
1675 struct device *dev = &pdev->dev;
1676 int floor, i;
1677
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001678 for (floor = 0; floor < DOC_MAX_NBFLOORS && cascade->floors[floor];
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001679 floor++)
1680 for (i = 0; i < 4; i++)
1681 device_remove_file(dev, &doc_sys_attrs[floor][i]);
1682}
1683
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001684/*
1685 * Debug sysfs entries
1686 */
1687static int dbg_flashctrl_show(struct seq_file *s, void *p)
1688{
1689 struct docg3 *docg3 = (struct docg3 *)s->private;
1690
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001691 u8 fctrl;
1692
1693 mutex_lock(&docg3->cascade->lock);
1694 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
1695 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001696
Joe Perches8c98d252014-09-29 16:08:27 -07001697 seq_printf(s, "FlashControl : 0x%02x (%s,CE# %s,%s,%s,flash %s)\n",
1698 fctrl,
1699 fctrl & DOC_CTRL_VIOLATION ? "protocol violation" : "-",
1700 fctrl & DOC_CTRL_CE ? "active" : "inactive",
1701 fctrl & DOC_CTRL_PROTECTION_ERROR ? "protection error" : "-",
1702 fctrl & DOC_CTRL_SEQUENCE_ERROR ? "sequence error" : "-",
1703 fctrl & DOC_CTRL_FLASHREADY ? "ready" : "not ready");
1704
1705 return 0;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001706}
1707DEBUGFS_RO_ATTR(flashcontrol, dbg_flashctrl_show);
1708
1709static int dbg_asicmode_show(struct seq_file *s, void *p)
1710{
1711 struct docg3 *docg3 = (struct docg3 *)s->private;
1712
Joe Perches8c98d252014-09-29 16:08:27 -07001713 int pctrl, mode;
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001714
1715 mutex_lock(&docg3->cascade->lock);
1716 pctrl = doc_register_readb(docg3, DOC_ASICMODE);
1717 mode = pctrl & 0x03;
1718 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001719
Joe Perches8c98d252014-09-29 16:08:27 -07001720 seq_printf(s,
1721 "%04x : RAM_WE=%d,RSTIN_RESET=%d,BDETCT_RESET=%d,WRITE_ENABLE=%d,POWERDOWN=%d,MODE=%d%d (",
1722 pctrl,
1723 pctrl & DOC_ASICMODE_RAM_WE ? 1 : 0,
1724 pctrl & DOC_ASICMODE_RSTIN_RESET ? 1 : 0,
1725 pctrl & DOC_ASICMODE_BDETCT_RESET ? 1 : 0,
1726 pctrl & DOC_ASICMODE_MDWREN ? 1 : 0,
1727 pctrl & DOC_ASICMODE_POWERDOWN ? 1 : 0,
1728 mode >> 1, mode & 0x1);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001729
1730 switch (mode) {
1731 case DOC_ASICMODE_RESET:
Joe Perches8c98d252014-09-29 16:08:27 -07001732 seq_puts(s, "reset");
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001733 break;
1734 case DOC_ASICMODE_NORMAL:
Joe Perches8c98d252014-09-29 16:08:27 -07001735 seq_puts(s, "normal");
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001736 break;
1737 case DOC_ASICMODE_POWERDOWN:
Joe Perches8c98d252014-09-29 16:08:27 -07001738 seq_puts(s, "powerdown");
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001739 break;
1740 }
Joe Perches8c98d252014-09-29 16:08:27 -07001741 seq_puts(s, ")\n");
1742 return 0;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001743}
1744DEBUGFS_RO_ATTR(asic_mode, dbg_asicmode_show);
1745
1746static int dbg_device_id_show(struct seq_file *s, void *p)
1747{
1748 struct docg3 *docg3 = (struct docg3 *)s->private;
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001749 int id;
1750
1751 mutex_lock(&docg3->cascade->lock);
1752 id = doc_register_readb(docg3, DOC_DEVICESELECT);
1753 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001754
Joe Perches8c98d252014-09-29 16:08:27 -07001755 seq_printf(s, "DeviceId = %d\n", id);
1756 return 0;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001757}
1758DEBUGFS_RO_ATTR(device_id, dbg_device_id_show);
1759
1760static int dbg_protection_show(struct seq_file *s, void *p)
1761{
1762 struct docg3 *docg3 = (struct docg3 *)s->private;
Robert Jarzmikdbc26d92011-11-19 16:02:45 +01001763 int protect, dps0, dps0_low, dps0_high, dps1, dps1_low, dps1_high;
1764
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001765 mutex_lock(&docg3->cascade->lock);
Robert Jarzmikdbc26d92011-11-19 16:02:45 +01001766 protect = doc_register_readb(docg3, DOC_PROTECTION);
1767 dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
1768 dps0_low = doc_register_readw(docg3, DOC_DPS0_ADDRLOW);
1769 dps0_high = doc_register_readw(docg3, DOC_DPS0_ADDRHIGH);
1770 dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
1771 dps1_low = doc_register_readw(docg3, DOC_DPS1_ADDRLOW);
1772 dps1_high = doc_register_readw(docg3, DOC_DPS1_ADDRHIGH);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001773 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001774
Joe Perches8c98d252014-09-29 16:08:27 -07001775 seq_printf(s, "Protection = 0x%02x (", protect);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001776 if (protect & DOC_PROTECT_FOUNDRY_OTP_LOCK)
Joe Perches8c98d252014-09-29 16:08:27 -07001777 seq_puts(s, "FOUNDRY_OTP_LOCK,");
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001778 if (protect & DOC_PROTECT_CUSTOMER_OTP_LOCK)
Joe Perches8c98d252014-09-29 16:08:27 -07001779 seq_puts(s, "CUSTOMER_OTP_LOCK,");
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001780 if (protect & DOC_PROTECT_LOCK_INPUT)
Joe Perches8c98d252014-09-29 16:08:27 -07001781 seq_puts(s, "LOCK_INPUT,");
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001782 if (protect & DOC_PROTECT_STICKY_LOCK)
Joe Perches8c98d252014-09-29 16:08:27 -07001783 seq_puts(s, "STICKY_LOCK,");
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001784 if (protect & DOC_PROTECT_PROTECTION_ENABLED)
Joe Perches8c98d252014-09-29 16:08:27 -07001785 seq_puts(s, "PROTECTION ON,");
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001786 if (protect & DOC_PROTECT_IPL_DOWNLOAD_LOCK)
Joe Perches8c98d252014-09-29 16:08:27 -07001787 seq_puts(s, "IPL_DOWNLOAD_LOCK,");
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001788 if (protect & DOC_PROTECT_PROTECTION_ERROR)
Joe Perches8c98d252014-09-29 16:08:27 -07001789 seq_puts(s, "PROTECT_ERR,");
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001790 else
Joe Perches8c98d252014-09-29 16:08:27 -07001791 seq_puts(s, "NO_PROTECT_ERR");
1792 seq_puts(s, ")\n");
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001793
Joe Perches8c98d252014-09-29 16:08:27 -07001794 seq_printf(s, "DPS0 = 0x%02x : Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1795 dps0, dps0_low, dps0_high,
1796 !!(dps0 & DOC_DPS_OTP_PROTECTED),
1797 !!(dps0 & DOC_DPS_READ_PROTECTED),
1798 !!(dps0 & DOC_DPS_WRITE_PROTECTED),
1799 !!(dps0 & DOC_DPS_HW_LOCK_ENABLED),
1800 !!(dps0 & DOC_DPS_KEY_OK));
1801 seq_printf(s, "DPS1 = 0x%02x : Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1802 dps1, dps1_low, dps1_high,
1803 !!(dps1 & DOC_DPS_OTP_PROTECTED),
1804 !!(dps1 & DOC_DPS_READ_PROTECTED),
1805 !!(dps1 & DOC_DPS_WRITE_PROTECTED),
1806 !!(dps1 & DOC_DPS_HW_LOCK_ENABLED),
1807 !!(dps1 & DOC_DPS_KEY_OK));
1808 return 0;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001809}
1810DEBUGFS_RO_ATTR(protection, dbg_protection_show);
1811
1812static int __init doc_dbg_register(struct docg3 *docg3)
1813{
1814 struct dentry *root, *entry;
1815
1816 root = debugfs_create_dir("docg3", NULL);
1817 if (!root)
1818 return -ENOMEM;
1819
1820 entry = debugfs_create_file("flashcontrol", S_IRUSR, root, docg3,
1821 &flashcontrol_fops);
1822 if (entry)
1823 entry = debugfs_create_file("asic_mode", S_IRUSR, root,
1824 docg3, &asic_mode_fops);
1825 if (entry)
1826 entry = debugfs_create_file("device_id", S_IRUSR, root,
1827 docg3, &device_id_fops);
1828 if (entry)
1829 entry = debugfs_create_file("protection", S_IRUSR, root,
1830 docg3, &protection_fops);
1831 if (entry) {
1832 docg3->debugfs_root = root;
1833 return 0;
1834 } else {
1835 debugfs_remove_recursive(root);
1836 return -ENOMEM;
1837 }
1838}
1839
Brian Norris45fd3572015-03-31 09:53:37 -07001840static void doc_dbg_unregister(struct docg3 *docg3)
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001841{
1842 debugfs_remove_recursive(docg3->debugfs_root);
1843}
1844
1845/**
1846 * doc_set_driver_info - Fill the mtd_info structure and docg3 structure
1847 * @chip_id: The chip ID of the supported chip
1848 * @mtd: The structure to fill
1849 */
Richard Weinberger0eb86182015-06-01 23:10:53 +02001850static int __init doc_set_driver_info(int chip_id, struct mtd_info *mtd)
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001851{
1852 struct docg3 *docg3 = mtd->priv;
1853 int cfg;
1854
1855 cfg = doc_register_readb(docg3, DOC_CONFIGURATION);
1856 docg3->if_cfg = (cfg & DOC_CONF_IF_CFG ? 1 : 0);
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001857 docg3->reliable = reliable_mode;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001858
1859 switch (chip_id) {
1860 case DOC_CHIPID_G3:
Robert Jarzmik31716a52012-03-03 21:57:33 +01001861 mtd->name = kasprintf(GFP_KERNEL, "docg3.%d",
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001862 docg3->device_id);
Richard Weinberger0eb86182015-06-01 23:10:53 +02001863 if (!mtd->name)
1864 return -ENOMEM;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001865 docg3->max_block = 2047;
1866 break;
1867 }
1868 mtd->type = MTD_NANDFLASH;
Robert Jarzmik7a7fcf12011-11-19 16:02:54 +01001869 mtd->flags = MTD_CAP_NANDFLASH;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001870 mtd->size = (docg3->max_block + 1) * DOC_LAYOUT_BLOCK_SIZE;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001871 if (docg3->reliable == 2)
1872 mtd->size /= 2;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001873 mtd->erasesize = DOC_LAYOUT_BLOCK_SIZE * DOC_LAYOUT_NBPLANES;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001874 if (docg3->reliable == 2)
1875 mtd->erasesize /= 2;
Artem Bityutskiy82c4c582012-02-03 09:44:32 +02001876 mtd->writebufsize = mtd->writesize = DOC_LAYOUT_PAGE_SIZE;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001877 mtd->oobsize = DOC_LAYOUT_OOB_SIZE;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02001878 mtd->_erase = doc_erase;
1879 mtd->_read = doc_read;
1880 mtd->_write = doc_write;
1881 mtd->_read_oob = doc_read_oob;
1882 mtd->_write_oob = doc_write_oob;
1883 mtd->_block_isbad = doc_block_isbad;
Boris Brezillon1bd0b242016-02-03 19:05:04 +01001884 mtd_set_ooblayout(mtd, &nand_ooblayout_docg3_ops);
Boris BREZILLONf5b8aa72016-03-07 10:46:51 +01001885 mtd->oobavail = 8;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001886 mtd->ecc_strength = DOC_ECC_BCH_T;
Richard Weinberger0eb86182015-06-01 23:10:53 +02001887
1888 return 0;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001889}
1890
1891/**
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001892 * doc_probe_device - Check if a device is available
1893 * @base: the io space where the device is probed
1894 * @floor: the floor of the probed device
1895 * @dev: the device
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001896 * @cascade: the cascade of chips this devices will belong to
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001897 *
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001898 * Checks whether a device at the specified IO range, and floor is available.
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001899 *
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001900 * Returns a mtd_info struct if there is a device, ENODEV if none found, ENOMEM
1901 * if a memory allocation failed. If floor 0 is checked, a reset of the ASIC is
1902 * launched.
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001903 */
Ryosuke Saito30053b82012-03-15 19:19:03 +09001904static struct mtd_info * __init
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001905doc_probe_device(struct docg3_cascade *cascade, int floor, struct device *dev)
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001906{
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001907 int ret, bbt_nbpages;
1908 u16 chip_id, chip_id_inv;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001909 struct docg3 *docg3;
1910 struct mtd_info *mtd;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001911
1912 ret = -ENOMEM;
1913 docg3 = kzalloc(sizeof(struct docg3), GFP_KERNEL);
1914 if (!docg3)
1915 goto nomem1;
1916 mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
1917 if (!mtd)
1918 goto nomem2;
1919 mtd->priv = docg3;
Frans Klaver1560d212015-06-10 22:38:18 +02001920 mtd->dev.parent = dev;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001921 bbt_nbpages = DIV_ROUND_UP(docg3->max_block + 1,
1922 8 * DOC_LAYOUT_PAGE_SIZE);
1923 docg3->bbt = kzalloc(bbt_nbpages * DOC_LAYOUT_PAGE_SIZE, GFP_KERNEL);
1924 if (!docg3->bbt)
1925 goto nomem3;
1926
1927 docg3->dev = dev;
1928 docg3->device_id = floor;
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001929 docg3->cascade = cascade;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001930 doc_set_device_id(docg3, docg3->device_id);
1931 if (!floor)
1932 doc_set_asic_mode(docg3, DOC_ASICMODE_RESET);
1933 doc_set_asic_mode(docg3, DOC_ASICMODE_NORMAL);
1934
1935 chip_id = doc_register_readw(docg3, DOC_CHIPID);
1936 chip_id_inv = doc_register_readw(docg3, DOC_CHIPID_INV);
1937
1938 ret = 0;
1939 if (chip_id != (u16)(~chip_id_inv)) {
Richard Weinberger45c2ebd2015-06-01 23:10:52 +02001940 goto nomem4;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001941 }
1942
1943 switch (chip_id) {
1944 case DOC_CHIPID_G3:
1945 doc_info("Found a G3 DiskOnChip at addr %p, floor %d\n",
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001946 docg3->cascade->base, floor);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001947 break;
1948 default:
1949 doc_err("Chip id %04x is not a DiskOnChip G3 chip\n", chip_id);
Richard Weinberger45c2ebd2015-06-01 23:10:52 +02001950 goto nomem4;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001951 }
1952
Richard Weinberger0eb86182015-06-01 23:10:53 +02001953 ret = doc_set_driver_info(chip_id, mtd);
1954 if (ret)
1955 goto nomem4;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001956
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001957 doc_hamming_ecc_init(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001958 doc_reload_bbt(docg3);
1959 return mtd;
1960
Richard Weinberger45c2ebd2015-06-01 23:10:52 +02001961nomem4:
1962 kfree(docg3->bbt);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001963nomem3:
1964 kfree(mtd);
1965nomem2:
1966 kfree(docg3);
1967nomem1:
1968 return ERR_PTR(ret);
1969}
1970
1971/**
1972 * doc_release_device - Release a docg3 floor
1973 * @mtd: the device
1974 */
1975static void doc_release_device(struct mtd_info *mtd)
1976{
1977 struct docg3 *docg3 = mtd->priv;
1978
1979 mtd_device_unregister(mtd);
1980 kfree(docg3->bbt);
1981 kfree(docg3);
1982 kfree(mtd->name);
1983 kfree(mtd);
1984}
1985
1986/**
Robert Jarzmike4b2a962011-11-19 16:02:56 +01001987 * docg3_resume - Awakens docg3 floor
1988 * @pdev: platfrom device
1989 *
Octavian Moraru86d2f6f2012-03-03 16:19:39 +02001990 * Returns 0 (always successful)
Robert Jarzmike4b2a962011-11-19 16:02:56 +01001991 */
1992static int docg3_resume(struct platform_device *pdev)
1993{
1994 int i;
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001995 struct docg3_cascade *cascade;
Robert Jarzmike4b2a962011-11-19 16:02:56 +01001996 struct mtd_info **docg3_floors, *mtd;
1997 struct docg3 *docg3;
1998
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001999 cascade = platform_get_drvdata(pdev);
2000 docg3_floors = cascade->floors;
Robert Jarzmike4b2a962011-11-19 16:02:56 +01002001 mtd = docg3_floors[0];
2002 docg3 = mtd->priv;
2003
2004 doc_dbg("docg3_resume()\n");
2005 for (i = 0; i < 12; i++)
2006 doc_readb(docg3, DOC_IOSPACE_IPL);
2007 return 0;
2008}
2009
2010/**
2011 * docg3_suspend - Put in low power mode the docg3 floor
2012 * @pdev: platform device
2013 * @state: power state
2014 *
2015 * Shuts off most of docg3 circuitery to lower power consumption.
2016 *
2017 * Returns 0 if suspend succeeded, -EIO if chip refused suspend
2018 */
2019static int docg3_suspend(struct platform_device *pdev, pm_message_t state)
2020{
2021 int floor, i;
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002022 struct docg3_cascade *cascade;
Robert Jarzmike4b2a962011-11-19 16:02:56 +01002023 struct mtd_info **docg3_floors, *mtd;
2024 struct docg3 *docg3;
2025 u8 ctrl, pwr_down;
2026
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002027 cascade = platform_get_drvdata(pdev);
2028 docg3_floors = cascade->floors;
Robert Jarzmike4b2a962011-11-19 16:02:56 +01002029 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
2030 mtd = docg3_floors[floor];
2031 if (!mtd)
2032 continue;
2033 docg3 = mtd->priv;
2034
2035 doc_writeb(docg3, floor, DOC_DEVICESELECT);
2036 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
2037 ctrl &= ~DOC_CTRL_VIOLATION & ~DOC_CTRL_CE;
2038 doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
2039
2040 for (i = 0; i < 10; i++) {
2041 usleep_range(3000, 4000);
2042 pwr_down = doc_register_readb(docg3, DOC_POWERMODE);
2043 if (pwr_down & DOC_POWERDOWN_READY)
2044 break;
2045 }
2046 if (pwr_down & DOC_POWERDOWN_READY) {
2047 doc_dbg("docg3_suspend(): floor %d powerdown ok\n",
2048 floor);
2049 } else {
2050 doc_err("docg3_suspend(): floor %d powerdown failed\n",
2051 floor);
2052 return -EIO;
2053 }
2054 }
2055
2056 mtd = docg3_floors[0];
2057 docg3 = mtd->priv;
2058 doc_set_asic_mode(docg3, DOC_ASICMODE_POWERDOWN);
2059 return 0;
2060}
2061
2062/**
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002063 * doc_probe - Probe the IO space for a DiskOnChip G3 chip
2064 * @pdev: platform device
2065 *
2066 * Probes for a G3 chip at the specified IO space in the platform data
2067 * ressources. The floor 0 must be available.
2068 *
2069 * Returns 0 on success, -ENOMEM, -ENXIO on error
2070 */
2071static int __init docg3_probe(struct platform_device *pdev)
2072{
2073 struct device *dev = &pdev->dev;
2074 struct mtd_info *mtd;
2075 struct resource *ress;
2076 void __iomem *base;
Brian Norrisdc524992015-03-09 10:18:16 -07002077 int ret, floor;
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002078 struct docg3_cascade *cascade;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002079
2080 ret = -ENXIO;
2081 ress = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2082 if (!ress) {
2083 dev_err(dev, "No I/O memory resource defined\n");
Jingoo Han82402ae2013-12-26 10:40:52 +09002084 return ret;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002085 }
Jingoo Han82402ae2013-12-26 10:40:52 +09002086 base = devm_ioremap(dev, ress->start, DOC_IOSPACE_SIZE);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002087
2088 ret = -ENOMEM;
Jingoo Han82402ae2013-12-26 10:40:52 +09002089 cascade = devm_kzalloc(dev, sizeof(*cascade) * DOC_MAX_NBFLOORS,
2090 GFP_KERNEL);
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002091 if (!cascade)
Jingoo Han82402ae2013-12-26 10:40:52 +09002092 return ret;
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002093 cascade->base = base;
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01002094 mutex_init(&cascade->lock);
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002095 cascade->bch = init_bch(DOC_ECC_BCH_M, DOC_ECC_BCH_T,
Robert Jarzmikd13d19e2011-11-19 16:02:55 +01002096 DOC_ECC_BCH_PRIMPOLY);
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002097 if (!cascade->bch)
Jingoo Han82402ae2013-12-26 10:40:52 +09002098 return ret;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002099
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002100 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002101 mtd = doc_probe_device(cascade, floor, dev);
Dan Carpenterb49e3452011-11-28 16:53:13 +03002102 if (IS_ERR(mtd)) {
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002103 ret = PTR_ERR(mtd);
Dan Carpenterb49e3452011-11-28 16:53:13 +03002104 goto err_probe;
2105 }
2106 if (!mtd) {
2107 if (floor == 0)
2108 goto notfound;
2109 else
2110 continue;
2111 }
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002112 cascade->floors[floor] = mtd;
Dan Carpenterb49e3452011-11-28 16:53:13 +03002113 ret = mtd_device_parse_register(mtd, part_probes, NULL, NULL,
2114 0);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002115 if (ret)
2116 goto err_probe;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002117 }
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002118
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002119 ret = doc_register_sysfs(pdev, cascade);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01002120 if (ret)
2121 goto err_probe;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002122
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002123 platform_set_drvdata(pdev, cascade);
2124 doc_dbg_register(cascade->floors[0]->priv);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002125 return 0;
2126
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002127notfound:
2128 ret = -ENODEV;
2129 dev_info(dev, "No supported DiskOnChip found\n");
2130err_probe:
Wei Yongjun39ac9ca2013-10-11 12:40:21 +08002131 free_bch(cascade->bch);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002132 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002133 if (cascade->floors[floor])
2134 doc_release_device(cascade->floors[floor]);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002135 return ret;
2136}
2137
2138/**
2139 * docg3_release - Release the driver
2140 * @pdev: the platform device
2141 *
2142 * Returns 0
2143 */
Brian Norris45fd3572015-03-31 09:53:37 -07002144static int docg3_release(struct platform_device *pdev)
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002145{
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002146 struct docg3_cascade *cascade = platform_get_drvdata(pdev);
2147 struct docg3 *docg3 = cascade->floors[0]->priv;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002148 int floor;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002149
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002150 doc_unregister_sysfs(pdev, cascade);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002151 doc_dbg_unregister(docg3);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002152 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002153 if (cascade->floors[floor])
2154 doc_release_device(cascade->floors[floor]);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002155
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002156 free_bch(docg3->cascade->bch);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002157 return 0;
2158}
2159
Brian Norrisd4efafc2014-10-22 10:17:38 -07002160#ifdef CONFIG_OF
Fabian Frederick66610442015-03-16 20:20:28 +01002161static const struct of_device_id docg3_dt_ids[] = {
Robert Jarzmika59459f2014-09-27 21:34:29 +02002162 { .compatible = "m-systems,diskonchip-g3" },
2163 {}
2164};
2165MODULE_DEVICE_TABLE(of, docg3_dt_ids);
Brian Norrisd4efafc2014-10-22 10:17:38 -07002166#endif
Robert Jarzmika59459f2014-09-27 21:34:29 +02002167
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002168static struct platform_driver g3_driver = {
2169 .driver = {
2170 .name = "docg3",
Robert Jarzmika59459f2014-09-27 21:34:29 +02002171 .of_match_table = of_match_ptr(docg3_dt_ids),
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002172 },
Robert Jarzmike4b2a962011-11-19 16:02:56 +01002173 .suspend = docg3_suspend,
2174 .resume = docg3_resume,
Brian Norris45fd3572015-03-31 09:53:37 -07002175 .remove = docg3_release,
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002176};
2177
Jingoo Han725a2272013-03-05 13:28:33 +09002178module_platform_driver_probe(g3_driver, docg3_probe);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002179
2180MODULE_LICENSE("GPL");
2181MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
2182MODULE_DESCRIPTION("MTD driver for DiskOnChip G3");