blob: 0d9d3ee68b61e1c2add20d07b449b63ee0da0e14 [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
Robert Jarzmik732b63b2011-11-19 16:02:49 +010070/**
71 * struct docg3_oobinfo - DiskOnChip G3 OOB layout
72 * @eccbytes: 8 bytes are used (1 for Hamming ECC, 7 for BCH ECC)
73 * @eccpos: ecc positions (byte 7 is Hamming ECC, byte 8-14 are BCH ECC)
74 * @oobfree: free pageinfo bytes (byte 0 until byte 6, byte 15
75 * @oobavail: 8 available bytes remaining after ECC toll
76 */
77static struct nand_ecclayout docg3_oobinfo = {
78 .eccbytes = 8,
79 .eccpos = {7, 8, 9, 10, 11, 12, 13, 14},
80 .oobfree = {{0, 7}, {15, 1} },
81 .oobavail = 8,
82};
83
Robert Jarzmikefa2ca72011-10-05 15:22:34 +020084static inline u8 doc_readb(struct docg3 *docg3, u16 reg)
85{
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +010086 u8 val = readb(docg3->cascade->base + reg);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +020087
88 trace_docg3_io(0, 8, reg, (int)val);
89 return val;
90}
91
92static inline u16 doc_readw(struct docg3 *docg3, u16 reg)
93{
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +010094 u16 val = readw(docg3->cascade->base + reg);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +020095
96 trace_docg3_io(0, 16, reg, (int)val);
97 return val;
98}
99
100static inline void doc_writeb(struct docg3 *docg3, u8 val, u16 reg)
101{
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +0100102 writeb(val, docg3->cascade->base + reg);
Robert Jarzmik84a93052011-11-19 16:02:44 +0100103 trace_docg3_io(1, 8, reg, val);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200104}
105
106static inline void doc_writew(struct docg3 *docg3, u16 val, u16 reg)
107{
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +0100108 writew(val, docg3->cascade->base + reg);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200109 trace_docg3_io(1, 16, reg, val);
110}
111
112static inline void doc_flash_command(struct docg3 *docg3, u8 cmd)
113{
114 doc_writeb(docg3, cmd, DOC_FLASHCOMMAND);
115}
116
117static inline void doc_flash_sequence(struct docg3 *docg3, u8 seq)
118{
119 doc_writeb(docg3, seq, DOC_FLASHSEQUENCE);
120}
121
122static inline void doc_flash_address(struct docg3 *docg3, u8 addr)
123{
124 doc_writeb(docg3, addr, DOC_FLASHADDRESS);
125}
126
Artem Bityutskiyafffeec2013-03-12 10:50:07 +0200127static char const * const part_probes[] = { "cmdlinepart", "saftlpart", NULL };
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200128
129static int doc_register_readb(struct docg3 *docg3, int reg)
130{
131 u8 val;
132
133 doc_writew(docg3, reg, DOC_READADDRESS);
134 val = doc_readb(docg3, reg);
135 doc_vdbg("Read register %04x : %02x\n", reg, val);
136 return val;
137}
138
139static int doc_register_readw(struct docg3 *docg3, int reg)
140{
141 u16 val;
142
143 doc_writew(docg3, reg, DOC_READADDRESS);
144 val = doc_readw(docg3, reg);
145 doc_vdbg("Read register %04x : %04x\n", reg, val);
146 return val;
147}
148
149/**
150 * doc_delay - delay docg3 operations
151 * @docg3: the device
152 * @nbNOPs: the number of NOPs to issue
153 *
154 * As no specification is available, the right timings between chip commands are
155 * unknown. The only available piece of information are the observed nops on a
156 * working docg3 chip.
157 * Therefore, doc_delay relies on a busy loop of NOPs, instead of scheduler
158 * friendlier msleep() functions or blocking mdelay().
159 */
160static void doc_delay(struct docg3 *docg3, int nbNOPs)
161{
162 int i;
163
Robert Jarzmikac48e802011-11-19 16:02:43 +0100164 doc_vdbg("NOP x %d\n", nbNOPs);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200165 for (i = 0; i < nbNOPs; i++)
166 doc_writeb(docg3, 0, DOC_NOP);
167}
168
169static int is_prot_seq_error(struct docg3 *docg3)
170{
171 int ctrl;
172
173 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
174 return ctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR);
175}
176
177static int doc_is_ready(struct docg3 *docg3)
178{
179 int ctrl;
180
181 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
182 return ctrl & DOC_CTRL_FLASHREADY;
183}
184
185static int doc_wait_ready(struct docg3 *docg3)
186{
187 int maxWaitCycles = 100;
188
189 do {
190 doc_delay(docg3, 4);
191 cpu_relax();
192 } while (!doc_is_ready(docg3) && maxWaitCycles--);
193 doc_delay(docg3, 2);
194 if (maxWaitCycles > 0)
195 return 0;
196 else
197 return -EIO;
198}
199
200static int doc_reset_seq(struct docg3 *docg3)
201{
202 int ret;
203
204 doc_writeb(docg3, 0x10, DOC_FLASHCONTROL);
205 doc_flash_sequence(docg3, DOC_SEQ_RESET);
206 doc_flash_command(docg3, DOC_CMD_RESET);
207 doc_delay(docg3, 2);
208 ret = doc_wait_ready(docg3);
209
210 doc_dbg("doc_reset_seq() -> isReady=%s\n", ret ? "false" : "true");
211 return ret;
212}
213
214/**
215 * doc_read_data_area - Read data from data area
216 * @docg3: the device
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100217 * @buf: the buffer to fill in (might be NULL is dummy reads)
218 * @len: the length to read
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200219 * @first: first time read, DOC_READADDRESS should be set
220 *
221 * Reads bytes from flash data. Handles the single byte / even bytes reads.
222 */
223static void doc_read_data_area(struct docg3 *docg3, void *buf, int len,
224 int first)
225{
226 int i, cdr, len4;
227 u16 data16, *dst16;
228 u8 data8, *dst8;
229
230 doc_dbg("doc_read_data_area(buf=%p, len=%d)\n", buf, len);
Robert Jarzmik52c2d9a2012-04-09 13:19:08 +0200231 cdr = len & 0x1;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200232 len4 = len - cdr;
233
234 if (first)
235 doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
236 dst16 = buf;
237 for (i = 0; i < len4; i += 2) {
238 data16 = doc_readw(docg3, DOC_IOSPACE_DATA);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100239 if (dst16) {
240 *dst16 = data16;
241 dst16++;
242 }
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200243 }
244
245 if (cdr) {
246 doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
247 DOC_READADDRESS);
248 doc_delay(docg3, 1);
249 dst8 = (u8 *)dst16;
250 for (i = 0; i < cdr; i++) {
251 data8 = doc_readb(docg3, DOC_IOSPACE_DATA);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100252 if (dst8) {
253 *dst8 = data8;
254 dst8++;
255 }
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200256 }
257 }
258}
259
260/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100261 * doc_write_data_area - Write data into data area
262 * @docg3: the device
263 * @buf: the buffer to get input bytes from
264 * @len: the length to write
265 *
266 * Writes bytes into flash data. Handles the single byte / even bytes writes.
267 */
268static void doc_write_data_area(struct docg3 *docg3, const void *buf, int len)
269{
270 int i, cdr, len4;
271 u16 *src16;
272 u8 *src8;
273
274 doc_dbg("doc_write_data_area(buf=%p, len=%d)\n", buf, len);
275 cdr = len & 0x3;
276 len4 = len - cdr;
277
278 doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
279 src16 = (u16 *)buf;
280 for (i = 0; i < len4; i += 2) {
281 doc_writew(docg3, *src16, DOC_IOSPACE_DATA);
282 src16++;
283 }
284
285 src8 = (u8 *)src16;
286 for (i = 0; i < cdr; i++) {
287 doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
288 DOC_READADDRESS);
289 doc_writeb(docg3, *src8, DOC_IOSPACE_DATA);
290 src8++;
291 }
292}
293
294/**
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100295 * doc_set_data_mode - Sets the flash to normal or reliable data mode
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200296 * @docg3: the device
297 *
298 * The reliable data mode is a bit slower than the fast mode, but less errors
299 * occur. Entering the reliable mode cannot be done without entering the fast
300 * mode first.
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100301 *
302 * In reliable mode, pages 2*n and 2*n+1 are clones. Writing to page 0 of blocks
303 * (4,5) make the hardware write also to page 1 of blocks blocks(4,5). Reading
304 * from page 0 of blocks (4,5) or from page 1 of blocks (4,5) gives the same
305 * result, which is a logical and between bytes from page 0 and page 1 (which is
306 * consistent with the fact that writing to a page is _clearing_ bits of that
307 * page).
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200308 */
309static void doc_set_reliable_mode(struct docg3 *docg3)
310{
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100311 static char *strmode[] = { "normal", "fast", "reliable", "invalid" };
312
313 doc_dbg("doc_set_reliable_mode(%s)\n", strmode[docg3->reliable]);
314 switch (docg3->reliable) {
315 case 0:
316 break;
317 case 1:
318 doc_flash_sequence(docg3, DOC_SEQ_SET_FASTMODE);
319 doc_flash_command(docg3, DOC_CMD_FAST_MODE);
320 break;
321 case 2:
322 doc_flash_sequence(docg3, DOC_SEQ_SET_RELIABLEMODE);
323 doc_flash_command(docg3, DOC_CMD_FAST_MODE);
324 doc_flash_command(docg3, DOC_CMD_RELIABLE_MODE);
325 break;
326 default:
327 doc_err("doc_set_reliable_mode(): invalid mode\n");
328 break;
329 }
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200330 doc_delay(docg3, 2);
331}
332
333/**
334 * doc_set_asic_mode - Set the ASIC mode
335 * @docg3: the device
336 * @mode: the mode
337 *
338 * The ASIC can work in 3 modes :
339 * - RESET: all registers are zeroed
340 * - NORMAL: receives and handles commands
341 * - POWERDOWN: minimal poweruse, flash parts shut off
342 */
343static void doc_set_asic_mode(struct docg3 *docg3, u8 mode)
344{
345 int i;
346
347 for (i = 0; i < 12; i++)
348 doc_readb(docg3, DOC_IOSPACE_IPL);
349
350 mode |= DOC_ASICMODE_MDWREN;
351 doc_dbg("doc_set_asic_mode(%02x)\n", mode);
352 doc_writeb(docg3, mode, DOC_ASICMODE);
353 doc_writeb(docg3, ~mode, DOC_ASICMODECONFIRM);
354 doc_delay(docg3, 1);
355}
356
357/**
358 * doc_set_device_id - Sets the devices id for cascaded G3 chips
359 * @docg3: the device
360 * @id: the chip to select (amongst 0, 1, 2, 3)
361 *
362 * There can be 4 cascaded G3 chips. This function selects the one which will
363 * should be the active one.
364 */
365static void doc_set_device_id(struct docg3 *docg3, int id)
366{
367 u8 ctrl;
368
369 doc_dbg("doc_set_device_id(%d)\n", id);
370 doc_writeb(docg3, id, DOC_DEVICESELECT);
371 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
372
373 ctrl &= ~DOC_CTRL_VIOLATION;
374 ctrl |= DOC_CTRL_CE;
375 doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
376}
377
378/**
379 * doc_set_extra_page_mode - Change flash page layout
380 * @docg3: the device
381 *
382 * Normally, the flash page is split into the data (512 bytes) and the out of
383 * band data (16 bytes). For each, 4 more bytes can be accessed, where the wear
384 * leveling counters are stored. To access this last area of 4 bytes, a special
385 * mode must be input to the flash ASIC.
386 *
Octavian Moraru86d2f6f2012-03-03 16:19:39 +0200387 * Returns 0 if no error occurred, -EIO else.
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200388 */
389static int doc_set_extra_page_mode(struct docg3 *docg3)
390{
391 int fctrl;
392
393 doc_dbg("doc_set_extra_page_mode()\n");
394 doc_flash_sequence(docg3, DOC_SEQ_PAGE_SIZE_532);
395 doc_flash_command(docg3, DOC_CMD_PAGE_SIZE_532);
396 doc_delay(docg3, 2);
397
398 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
399 if (fctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR))
400 return -EIO;
401 else
402 return 0;
403}
404
405/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100406 * doc_setup_addr_sector - Setup blocks/page/ofs address for one plane
407 * @docg3: the device
408 * @sector: the sector
409 */
410static void doc_setup_addr_sector(struct docg3 *docg3, int sector)
411{
412 doc_delay(docg3, 1);
413 doc_flash_address(docg3, sector & 0xff);
414 doc_flash_address(docg3, (sector >> 8) & 0xff);
415 doc_flash_address(docg3, (sector >> 16) & 0xff);
416 doc_delay(docg3, 1);
417}
418
419/**
420 * doc_setup_writeaddr_sector - Setup blocks/page/ofs address for one plane
421 * @docg3: the device
422 * @sector: the sector
423 * @ofs: the offset in the page, between 0 and (512 + 16 + 512)
424 */
425static void doc_setup_writeaddr_sector(struct docg3 *docg3, int sector, int ofs)
426{
427 ofs = ofs >> 2;
428 doc_delay(docg3, 1);
429 doc_flash_address(docg3, ofs & 0xff);
430 doc_flash_address(docg3, sector & 0xff);
431 doc_flash_address(docg3, (sector >> 8) & 0xff);
432 doc_flash_address(docg3, (sector >> 16) & 0xff);
433 doc_delay(docg3, 1);
434}
435
436/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200437 * doc_seek - Set both flash planes to the specified block, page for reading
438 * @docg3: the device
439 * @block0: the first plane block index
440 * @block1: the second plane block index
441 * @page: the page index within the block
442 * @wear: if true, read will occur on the 4 extra bytes of the wear area
443 * @ofs: offset in page to read
444 *
445 * Programs the flash even and odd planes to the specific block and page.
446 * Alternatively, programs the flash to the wear area of the specified page.
447 */
448static int doc_read_seek(struct docg3 *docg3, int block0, int block1, int page,
449 int wear, int ofs)
450{
451 int sector, ret = 0;
452
453 doc_dbg("doc_seek(blocks=(%d,%d), page=%d, ofs=%d, wear=%d)\n",
454 block0, block1, page, ofs, wear);
455
456 if (!wear && (ofs < 2 * DOC_LAYOUT_PAGE_SIZE)) {
457 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
458 doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
459 doc_delay(docg3, 2);
460 } else {
461 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
462 doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
463 doc_delay(docg3, 2);
464 }
465
466 doc_set_reliable_mode(docg3);
467 if (wear)
468 ret = doc_set_extra_page_mode(docg3);
469 if (ret)
470 goto out;
471
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200472 doc_flash_sequence(docg3, DOC_SEQ_READ);
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100473 sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200474 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100475 doc_setup_addr_sector(docg3, sector);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200476
477 sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
478 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100479 doc_setup_addr_sector(docg3, sector);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200480 doc_delay(docg3, 1);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200481
482out:
483 return ret;
484}
485
486/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100487 * doc_write_seek - Set both flash planes to the specified block, page for writing
488 * @docg3: the device
489 * @block0: the first plane block index
490 * @block1: the second plane block index
491 * @page: the page index within the block
492 * @ofs: offset in page to write
493 *
494 * Programs the flash even and odd planes to the specific block and page.
495 * Alternatively, programs the flash to the wear area of the specified page.
496 */
497static int doc_write_seek(struct docg3 *docg3, int block0, int block1, int page,
498 int ofs)
499{
500 int ret = 0, sector;
501
502 doc_dbg("doc_write_seek(blocks=(%d,%d), page=%d, ofs=%d)\n",
503 block0, block1, page, ofs);
504
505 doc_set_reliable_mode(docg3);
506
507 if (ofs < 2 * DOC_LAYOUT_PAGE_SIZE) {
508 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
509 doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
510 doc_delay(docg3, 2);
511 } else {
512 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
513 doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
514 doc_delay(docg3, 2);
515 }
516
517 doc_flash_sequence(docg3, DOC_SEQ_PAGE_SETUP);
518 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1);
519
520 sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
521 doc_setup_writeaddr_sector(docg3, sector, ofs);
522
523 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE3);
524 doc_delay(docg3, 2);
525 ret = doc_wait_ready(docg3);
526 if (ret)
527 goto out;
528
529 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1);
530 sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
531 doc_setup_writeaddr_sector(docg3, sector, ofs);
532 doc_delay(docg3, 1);
533
534out:
535 return ret;
536}
537
538
539/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200540 * doc_read_page_ecc_init - Initialize hardware ECC engine
541 * @docg3: the device
542 * @len: the number of bytes covered by the ECC (BCH covered)
543 *
544 * The function does initialize the hardware ECC engine to compute the Hamming
Robert Jarzmikb6044362011-12-02 20:00:12 +0100545 * ECC (on 1 byte) and the BCH hardware ECC (on 7 bytes).
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200546 *
547 * Return 0 if succeeded, -EIO on error
548 */
549static int doc_read_page_ecc_init(struct docg3 *docg3, int len)
550{
551 doc_writew(docg3, DOC_ECCCONF0_READ_MODE
552 | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
553 | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
554 DOC_ECCCONF0);
555 doc_delay(docg3, 4);
556 doc_register_readb(docg3, DOC_FLASHCONTROL);
557 return doc_wait_ready(docg3);
558}
559
560/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100561 * doc_write_page_ecc_init - Initialize hardware BCH ECC engine
562 * @docg3: the device
563 * @len: the number of bytes covered by the ECC (BCH covered)
564 *
565 * The function does initialize the hardware ECC engine to compute the Hamming
Robert Jarzmikb6044362011-12-02 20:00:12 +0100566 * ECC (on 1 byte) and the BCH hardware ECC (on 7 bytes).
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100567 *
568 * Return 0 if succeeded, -EIO on error
569 */
570static int doc_write_page_ecc_init(struct docg3 *docg3, int len)
571{
Robert Jarzmikb6044362011-12-02 20:00:12 +0100572 doc_writew(docg3, DOC_ECCCONF0_WRITE_MODE
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100573 | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
574 | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
575 DOC_ECCCONF0);
576 doc_delay(docg3, 4);
577 doc_register_readb(docg3, DOC_FLASHCONTROL);
578 return doc_wait_ready(docg3);
579}
580
581/**
582 * doc_ecc_disable - Disable Hamming and BCH ECC hardware calculator
583 * @docg3: the device
584 *
585 * Disables the hardware ECC generator and checker, for unchecked reads (as when
586 * reading OOB only or write status byte).
587 */
588static void doc_ecc_disable(struct docg3 *docg3)
589{
590 doc_writew(docg3, DOC_ECCCONF0_READ_MODE, DOC_ECCCONF0);
591 doc_delay(docg3, 4);
592}
593
594/**
595 * doc_hamming_ecc_init - Initialize hardware Hamming ECC engine
596 * @docg3: the device
597 * @nb_bytes: the number of bytes covered by the ECC (Hamming covered)
598 *
599 * This function programs the ECC hardware to compute the hamming code on the
600 * last provided N bytes to the hardware generator.
601 */
602static void doc_hamming_ecc_init(struct docg3 *docg3, int nb_bytes)
603{
604 u8 ecc_conf1;
605
606 ecc_conf1 = doc_register_readb(docg3, DOC_ECCCONF1);
607 ecc_conf1 &= ~DOC_ECCCONF1_HAMMING_BITS_MASK;
608 ecc_conf1 |= (nb_bytes & DOC_ECCCONF1_HAMMING_BITS_MASK);
609 doc_writeb(docg3, ecc_conf1, DOC_ECCCONF1);
610}
611
612/**
Robert Jarzmikb6044362011-12-02 20:00:12 +0100613 * doc_ecc_bch_fix_data - Fix if need be read data from flash
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100614 * @docg3: the device
615 * @buf: the buffer of read data (512 + 7 + 1 bytes)
616 * @hwecc: the hardware calculated ECC.
617 * It's in fact recv_ecc ^ calc_ecc, where recv_ecc was read from OOB
618 * area data, and calc_ecc the ECC calculated by the hardware generator.
619 *
620 * Checks if the received data matches the ECC, and if an error is detected,
621 * tries to fix the bit flips (at most 4) in the buffer buf. As the docg3
622 * understands the (data, ecc, syndroms) in an inverted order in comparison to
623 * the BCH library, the function reverses the order of bits (ie. bit7 and bit0,
624 * bit6 and bit 1, ...) for all ECC data.
625 *
626 * The hardware ecc unit produces oob_ecc ^ calc_ecc. The kernel's bch
627 * algorithm is used to decode this. However the hw operates on page
628 * data in a bit order that is the reverse of that of the bch alg,
629 * requiring that the bits be reversed on the result. Thanks to Ivan
630 * Djelic for his analysis.
631 *
632 * Returns number of fixed bits (0, 1, 2, 3, 4) or -EBADMSG if too many bit
633 * errors were detected and cannot be fixed.
634 */
635static int doc_ecc_bch_fix_data(struct docg3 *docg3, void *buf, u8 *hwecc)
636{
637 u8 ecc[DOC_ECC_BCH_SIZE];
638 int errorpos[DOC_ECC_BCH_T], i, numerrs;
639
640 for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
641 ecc[i] = bitrev8(hwecc[i]);
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +0100642 numerrs = decode_bch(docg3->cascade->bch, NULL,
643 DOC_ECC_BCH_COVERED_BYTES,
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100644 NULL, ecc, NULL, errorpos);
645 BUG_ON(numerrs == -EINVAL);
646 if (numerrs < 0)
647 goto out;
648
649 for (i = 0; i < numerrs; i++)
650 errorpos[i] = (errorpos[i] & ~7) | (7 - (errorpos[i] & 7));
651 for (i = 0; i < numerrs; i++)
652 if (errorpos[i] < DOC_ECC_BCH_COVERED_BYTES*8)
653 /* error is located in data, correct it */
654 change_bit(errorpos[i], buf);
655out:
656 doc_dbg("doc_ecc_bch_fix_data: flipped %d bits\n", numerrs);
657 return numerrs;
658}
659
660
661/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200662 * doc_read_page_prepare - Prepares reading data from a flash page
663 * @docg3: the device
664 * @block0: the first plane block index on flash memory
665 * @block1: the second plane block index on flash memory
666 * @page: the page index in the block
667 * @offset: the offset in the page (must be a multiple of 4)
668 *
669 * Prepares the page to be read in the flash memory :
670 * - tell ASIC to map the flash pages
671 * - tell ASIC to be in read mode
672 *
673 * After a call to this method, a call to doc_read_page_finish is mandatory,
674 * to end the read cycle of the flash.
675 *
676 * Read data from a flash page. The length to be read must be between 0 and
677 * (page_size + oob_size + wear_size), ie. 532, and a multiple of 4 (because
678 * the extra bytes reading is not implemented).
679 *
680 * As pages are grouped by 2 (in 2 planes), reading from a page must be done
681 * in two steps:
682 * - one read of 512 bytes at offset 0
683 * - one read of 512 bytes at offset 512 + 16
684 *
Octavian Moraru86d2f6f2012-03-03 16:19:39 +0200685 * Returns 0 if successful, -EIO if a read error occurred.
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200686 */
687static int doc_read_page_prepare(struct docg3 *docg3, int block0, int block1,
688 int page, int offset)
689{
690 int wear_area = 0, ret = 0;
691
692 doc_dbg("doc_read_page_prepare(blocks=(%d,%d), page=%d, ofsInPage=%d)\n",
693 block0, block1, page, offset);
694 if (offset >= DOC_LAYOUT_WEAR_OFFSET)
695 wear_area = 1;
696 if (!wear_area && offset > (DOC_LAYOUT_PAGE_OOB_SIZE * 2))
697 return -EINVAL;
698
699 doc_set_device_id(docg3, docg3->device_id);
700 ret = doc_reset_seq(docg3);
701 if (ret)
702 goto err;
703
704 /* Program the flash address block and page */
705 ret = doc_read_seek(docg3, block0, block1, page, wear_area, offset);
706 if (ret)
707 goto err;
708
709 doc_flash_command(docg3, DOC_CMD_READ_ALL_PLANES);
710 doc_delay(docg3, 2);
711 doc_wait_ready(docg3);
712
713 doc_flash_command(docg3, DOC_CMD_SET_ADDR_READ);
714 doc_delay(docg3, 1);
715 if (offset >= DOC_LAYOUT_PAGE_SIZE * 2)
716 offset -= 2 * DOC_LAYOUT_PAGE_SIZE;
717 doc_flash_address(docg3, offset >> 2);
718 doc_delay(docg3, 1);
719 doc_wait_ready(docg3);
720
721 doc_flash_command(docg3, DOC_CMD_READ_FLASH);
722
723 return 0;
724err:
725 doc_writeb(docg3, 0, DOC_DATAEND);
726 doc_delay(docg3, 2);
727 return -EIO;
728}
729
730/**
731 * doc_read_page_getbytes - Reads bytes from a prepared page
732 * @docg3: the device
733 * @len: the number of bytes to be read (must be a multiple of 4)
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100734 * @buf: the buffer to be filled in (or NULL is forget bytes)
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200735 * @first: 1 if first time read, DOC_READADDRESS should be set
Robert Jarzmik52c2d9a2012-04-09 13:19:08 +0200736 * @last_odd: 1 if last read ended up on an odd byte
737 *
738 * Reads bytes from a prepared page. There is a trickery here : if the last read
739 * ended up on an odd offset in the 1024 bytes double page, ie. between the 2
740 * planes, the first byte must be read apart. If a word (16bit) read was used,
741 * the read would return the byte of plane 2 as low *and* high endian, which
742 * will mess the read.
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200743 *
744 */
745static int doc_read_page_getbytes(struct docg3 *docg3, int len, u_char *buf,
Robert Jarzmik52c2d9a2012-04-09 13:19:08 +0200746 int first, int last_odd)
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200747{
Robert Jarzmik52c2d9a2012-04-09 13:19:08 +0200748 if (last_odd && len > 0) {
749 doc_read_data_area(docg3, buf, 1, first);
750 doc_read_data_area(docg3, buf ? buf + 1 : buf, len - 1, 0);
751 } else {
752 doc_read_data_area(docg3, buf, len, first);
753 }
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200754 doc_delay(docg3, 2);
755 return len;
756}
757
758/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100759 * doc_write_page_putbytes - Writes bytes into a prepared page
760 * @docg3: the device
761 * @len: the number of bytes to be written
762 * @buf: the buffer of input bytes
763 *
764 */
765static void doc_write_page_putbytes(struct docg3 *docg3, int len,
766 const u_char *buf)
767{
768 doc_write_data_area(docg3, buf, len);
769 doc_delay(docg3, 2);
770}
771
772/**
Robert Jarzmikb6044362011-12-02 20:00:12 +0100773 * doc_get_bch_hw_ecc - Get hardware calculated BCH ECC
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200774 * @docg3: the device
Robert Jarzmikb6044362011-12-02 20:00:12 +0100775 * @hwecc: the array of 7 integers where the hardware ecc will be stored
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200776 */
Robert Jarzmikb6044362011-12-02 20:00:12 +0100777static void doc_get_bch_hw_ecc(struct docg3 *docg3, u8 *hwecc)
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200778{
779 int i;
780
781 for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
Robert Jarzmikb6044362011-12-02 20:00:12 +0100782 hwecc[i] = doc_register_readb(docg3, DOC_BCH_HW_ECC(i));
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200783}
784
785/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100786 * doc_page_finish - Ends reading/writing of a flash page
787 * @docg3: the device
788 */
789static void doc_page_finish(struct docg3 *docg3)
790{
791 doc_writeb(docg3, 0, DOC_DATAEND);
792 doc_delay(docg3, 2);
793}
794
795/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200796 * doc_read_page_finish - Ends reading of a flash page
797 * @docg3: the device
798 *
799 * As a side effect, resets the chip selector to 0. This ensures that after each
800 * read operation, the floor 0 is selected. Therefore, if the systems halts, the
801 * reboot will boot on floor 0, where the IPL is.
802 */
803static void doc_read_page_finish(struct docg3 *docg3)
804{
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100805 doc_page_finish(docg3);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200806 doc_set_device_id(docg3, 0);
807}
808
809/**
810 * calc_block_sector - Calculate blocks, pages and ofs.
811
812 * @from: offset in flash
813 * @block0: first plane block index calculated
814 * @block1: second plane block index calculated
815 * @page: page calculated
816 * @ofs: offset in page
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100817 * @reliable: 0 if docg3 in normal mode, 1 if docg3 in fast mode, 2 if docg3 in
818 * reliable mode.
819 *
820 * The calculation is based on the reliable/normal mode. In normal mode, the 64
821 * pages of a block are available. In reliable mode, as pages 2*n and 2*n+1 are
822 * clones, only 32 pages per block are available.
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200823 */
824static void calc_block_sector(loff_t from, int *block0, int *block1, int *page,
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100825 int *ofs, int reliable)
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200826{
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100827 uint sector, pages_biblock;
828
829 pages_biblock = DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_NBPLANES;
830 if (reliable == 1 || reliable == 2)
831 pages_biblock /= 2;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200832
833 sector = from / DOC_LAYOUT_PAGE_SIZE;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100834 *block0 = sector / pages_biblock * DOC_LAYOUT_NBPLANES;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200835 *block1 = *block0 + 1;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100836 *page = sector % pages_biblock;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200837 *page /= DOC_LAYOUT_NBPLANES;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100838 if (reliable == 1 || reliable == 2)
839 *page *= 2;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200840 if (sector % 2)
841 *ofs = DOC_LAYOUT_PAGE_OOB_SIZE;
842 else
843 *ofs = 0;
844}
845
846/**
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100847 * doc_read_oob - Read out of band bytes from flash
848 * @mtd: the device
849 * @from: the offset from first block and first page, in bytes, aligned on page
850 * size
851 * @ops: the mtd oob structure
852 *
853 * Reads flash memory OOB area of pages.
854 *
Octavian Moraru86d2f6f2012-03-03 16:19:39 +0200855 * Returns 0 if read successful, of -EIO, -EINVAL if an error occurred
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100856 */
857static int doc_read_oob(struct mtd_info *mtd, loff_t from,
858 struct mtd_oob_ops *ops)
859{
860 struct docg3 *docg3 = mtd->priv;
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100861 int block0, block1, page, ret, skip, ofs = 0;
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100862 u8 *oobbuf = ops->oobbuf;
863 u8 *buf = ops->datbuf;
864 size_t len, ooblen, nbdata, nboob;
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100865 u8 hwecc[DOC_ECC_BCH_SIZE], eccconf1;
Mike Dunnedbc45402012-04-25 12:06:11 -0700866 int max_bitflips = 0;
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100867
868 if (buf)
869 len = ops->len;
870 else
871 len = 0;
872 if (oobbuf)
873 ooblen = ops->ooblen;
874 else
875 ooblen = 0;
876
877 if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
878 oobbuf += ops->ooboffs;
879
880 doc_dbg("doc_read_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
881 from, ops->mode, buf, len, oobbuf, ooblen);
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100882 if (ooblen % DOC_LAYOUT_OOB_SIZE)
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100883 return -EINVAL;
884
Robert Jarzmika7baef12012-03-22 21:00:50 +0100885 if (from + len > mtd->size)
886 return -EINVAL;
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100887
888 ops->oobretlen = 0;
889 ops->retlen = 0;
890 ret = 0;
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100891 skip = from % DOC_LAYOUT_PAGE_SIZE;
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +0100892 mutex_lock(&docg3->cascade->lock);
Mike Dunnedbc45402012-04-25 12:06:11 -0700893 while (ret >= 0 && (len > 0 || ooblen > 0)) {
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100894 calc_block_sector(from - skip, &block0, &block1, &page, &ofs,
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100895 docg3->reliable);
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100896 nbdata = min_t(size_t, len, DOC_LAYOUT_PAGE_SIZE - skip);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100897 nboob = min_t(size_t, ooblen, (size_t)DOC_LAYOUT_OOB_SIZE);
898 ret = doc_read_page_prepare(docg3, block0, block1, page, ofs);
899 if (ret < 0)
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +0100900 goto out;
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100901 ret = doc_read_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100902 if (ret < 0)
903 goto err_in_read;
Robert Jarzmik52c2d9a2012-04-09 13:19:08 +0200904 ret = doc_read_page_getbytes(docg3, skip, NULL, 1, 0);
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100905 if (ret < skip)
906 goto err_in_read;
Robert Jarzmik52c2d9a2012-04-09 13:19:08 +0200907 ret = doc_read_page_getbytes(docg3, nbdata, buf, 0, skip % 2);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100908 if (ret < nbdata)
909 goto err_in_read;
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100910 doc_read_page_getbytes(docg3,
911 DOC_LAYOUT_PAGE_SIZE - nbdata - skip,
Robert Jarzmik52c2d9a2012-04-09 13:19:08 +0200912 NULL, 0, (skip + nbdata) % 2);
913 ret = doc_read_page_getbytes(docg3, nboob, oobbuf, 0, 0);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100914 if (ret < nboob)
915 goto err_in_read;
916 doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE - nboob,
Robert Jarzmik52c2d9a2012-04-09 13:19:08 +0200917 NULL, 0, nboob % 2);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100918
Robert Jarzmikb6044362011-12-02 20:00:12 +0100919 doc_get_bch_hw_ecc(docg3, hwecc);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100920 eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1);
921
922 if (nboob >= DOC_LAYOUT_OOB_SIZE) {
Andy Shevchenko13e85972012-08-02 16:06:47 +0300923 doc_dbg("OOB - INFO: %*phC\n", 7, oobbuf);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100924 doc_dbg("OOB - HAMMING: %02x\n", oobbuf[7]);
Andy Shevchenko13e85972012-08-02 16:06:47 +0300925 doc_dbg("OOB - BCH_ECC: %*phC\n", 7, oobbuf + 8);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100926 doc_dbg("OOB - UNUSED: %02x\n", oobbuf[15]);
927 }
928 doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1);
Andy Shevchenko13e85972012-08-02 16:06:47 +0300929 doc_dbg("ECC HW_ECC: %*phC\n", 7, hwecc);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100930
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100931 ret = -EIO;
932 if (is_prot_seq_error(docg3))
933 goto err_in_read;
934 ret = 0;
935 if ((block0 >= DOC_LAYOUT_BLOCK_FIRST_DATA) &&
936 (eccconf1 & DOC_ECCCONF1_BCH_SYNDROM_ERR) &&
937 (eccconf1 & DOC_ECCCONF1_PAGE_IS_WRITTEN) &&
938 (ops->mode != MTD_OPS_RAW) &&
939 (nbdata == DOC_LAYOUT_PAGE_SIZE)) {
940 ret = doc_ecc_bch_fix_data(docg3, buf, hwecc);
941 if (ret < 0) {
942 mtd->ecc_stats.failed++;
943 ret = -EBADMSG;
944 }
945 if (ret > 0) {
946 mtd->ecc_stats.corrected += ret;
Mike Dunnedbc45402012-04-25 12:06:11 -0700947 max_bitflips = max(max_bitflips, ret);
948 ret = max_bitflips;
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100949 }
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100950 }
951
952 doc_read_page_finish(docg3);
953 ops->retlen += nbdata;
954 ops->oobretlen += nboob;
955 buf += nbdata;
956 oobbuf += nboob;
957 len -= nbdata;
958 ooblen -= nboob;
959 from += DOC_LAYOUT_PAGE_SIZE;
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100960 skip = 0;
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100961 }
962
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +0100963out:
964 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100965 return ret;
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100966err_in_read:
967 doc_read_page_finish(docg3);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +0100968 goto out;
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100969}
970
971/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200972 * doc_read - Read bytes from flash
973 * @mtd: the device
974 * @from: the offset from first block and first page, in bytes, aligned on page
975 * size
976 * @len: the number of bytes to read (must be a multiple of 4)
977 * @retlen: the number of bytes actually read
978 * @buf: the filled in buffer
979 *
980 * Reads flash memory pages. This function does not read the OOB chunk, but only
981 * the page data.
982 *
Octavian Moraru86d2f6f2012-03-03 16:19:39 +0200983 * Returns 0 if read successful, of -EIO, -EINVAL if an error occurred
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200984 */
985static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
986 size_t *retlen, u_char *buf)
987{
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100988 struct mtd_oob_ops ops;
989 size_t ret;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200990
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100991 memset(&ops, 0, sizeof(ops));
992 ops.datbuf = buf;
993 ops.len = len;
994 ops.mode = MTD_OPS_AUTO_OOB;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200995
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100996 ret = doc_read_oob(mtd, from, &ops);
997 *retlen = ops.retlen;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200998 return ret;
999}
1000
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001001static int doc_reload_bbt(struct docg3 *docg3)
1002{
1003 int block = DOC_LAYOUT_BLOCK_BBT;
1004 int ret = 0, nbpages, page;
1005 u_char *buf = docg3->bbt;
1006
1007 nbpages = DIV_ROUND_UP(docg3->max_block + 1, 8 * DOC_LAYOUT_PAGE_SIZE);
1008 for (page = 0; !ret && (page < nbpages); page++) {
1009 ret = doc_read_page_prepare(docg3, block, block + 1,
1010 page + DOC_LAYOUT_PAGE_BBT, 0);
1011 if (!ret)
1012 ret = doc_read_page_ecc_init(docg3,
1013 DOC_LAYOUT_PAGE_SIZE);
1014 if (!ret)
1015 doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE,
Robert Jarzmik52c2d9a2012-04-09 13:19:08 +02001016 buf, 1, 0);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001017 buf += DOC_LAYOUT_PAGE_SIZE;
1018 }
1019 doc_read_page_finish(docg3);
1020 return ret;
1021}
1022
1023/**
1024 * doc_block_isbad - Checks whether a block is good or not
1025 * @mtd: the device
1026 * @from: the offset to find the correct block
1027 *
1028 * Returns 1 if block is bad, 0 if block is good
1029 */
1030static int doc_block_isbad(struct mtd_info *mtd, loff_t from)
1031{
1032 struct docg3 *docg3 = mtd->priv;
1033 int block0, block1, page, ofs, is_good;
1034
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001035 calc_block_sector(from, &block0, &block1, &page, &ofs,
1036 docg3->reliable);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001037 doc_dbg("doc_block_isbad(from=%lld) => block=(%d,%d), page=%d, ofs=%d\n",
1038 from, block0, block1, page, ofs);
1039
1040 if (block0 < DOC_LAYOUT_BLOCK_FIRST_DATA)
1041 return 0;
1042 if (block1 > docg3->max_block)
1043 return -EINVAL;
1044
1045 is_good = docg3->bbt[block0 >> 3] & (1 << (block0 & 0x7));
1046 return !is_good;
1047}
1048
Robert Jarzmike10019b2011-12-16 23:25:23 +01001049#if 0
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001050/**
1051 * doc_get_erase_count - Get block erase count
1052 * @docg3: the device
1053 * @from: the offset in which the block is.
1054 *
1055 * Get the number of times a block was erased. The number is the maximum of
1056 * erase times between first and second plane (which should be equal normally).
1057 *
1058 * Returns The number of erases, or -EINVAL or -EIO on error.
1059 */
1060static int doc_get_erase_count(struct docg3 *docg3, loff_t from)
1061{
1062 u8 buf[DOC_LAYOUT_WEAR_SIZE];
1063 int ret, plane1_erase_count, plane2_erase_count;
1064 int block0, block1, page, ofs;
1065
1066 doc_dbg("doc_get_erase_count(from=%lld, buf=%p)\n", from, buf);
1067 if (from % DOC_LAYOUT_PAGE_SIZE)
1068 return -EINVAL;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001069 calc_block_sector(from, &block0, &block1, &page, &ofs, docg3->reliable);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001070 if (block1 > docg3->max_block)
1071 return -EINVAL;
1072
1073 ret = doc_reset_seq(docg3);
1074 if (!ret)
1075 ret = doc_read_page_prepare(docg3, block0, block1, page,
Robert Jarzmik52c2d9a2012-04-09 13:19:08 +02001076 ofs + DOC_LAYOUT_WEAR_OFFSET, 0);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001077 if (!ret)
1078 ret = doc_read_page_getbytes(docg3, DOC_LAYOUT_WEAR_SIZE,
Robert Jarzmik52c2d9a2012-04-09 13:19:08 +02001079 buf, 1, 0);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001080 doc_read_page_finish(docg3);
1081
1082 if (ret || (buf[0] != DOC_ERASE_MARK) || (buf[2] != DOC_ERASE_MARK))
1083 return -EIO;
1084 plane1_erase_count = (u8)(~buf[1]) | ((u8)(~buf[4]) << 8)
1085 | ((u8)(~buf[5]) << 16);
1086 plane2_erase_count = (u8)(~buf[3]) | ((u8)(~buf[6]) << 8)
1087 | ((u8)(~buf[7]) << 16);
1088
1089 return max(plane1_erase_count, plane2_erase_count);
1090}
Robert Jarzmike10019b2011-12-16 23:25:23 +01001091#endif
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001092
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001093/**
1094 * doc_get_op_status - get erase/write operation status
1095 * @docg3: the device
1096 *
1097 * Queries the status from the chip, and returns it
1098 *
1099 * Returns the status (bits DOC_PLANES_STATUS_*)
1100 */
1101static int doc_get_op_status(struct docg3 *docg3)
1102{
1103 u8 status;
1104
1105 doc_flash_sequence(docg3, DOC_SEQ_PLANES_STATUS);
1106 doc_flash_command(docg3, DOC_CMD_PLANES_STATUS);
1107 doc_delay(docg3, 5);
1108
1109 doc_ecc_disable(docg3);
1110 doc_read_data_area(docg3, &status, 1, 1);
1111 return status;
1112}
1113
1114/**
1115 * doc_write_erase_wait_status - wait for write or erase completion
1116 * @docg3: the device
1117 *
1118 * Wait for the chip to be ready again after erase or write operation, and check
1119 * erase/write status.
1120 *
Octavian Moraru86d2f6f2012-03-03 16:19:39 +02001121 * Returns 0 if erase successful, -EIO if erase/write issue, -ETIMEOUT if
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001122 * timeout
1123 */
1124static int doc_write_erase_wait_status(struct docg3 *docg3)
1125{
Robert Jarzmika2b3d282012-03-22 21:00:51 +01001126 int i, status, ret = 0;
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001127
Robert Jarzmika2b3d282012-03-22 21:00:51 +01001128 for (i = 0; !doc_is_ready(docg3) && i < 5; i++)
1129 msleep(20);
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001130 if (!doc_is_ready(docg3)) {
1131 doc_dbg("Timeout reached and the chip is still not ready\n");
1132 ret = -EAGAIN;
1133 goto out;
1134 }
1135
1136 status = doc_get_op_status(docg3);
1137 if (status & DOC_PLANES_STATUS_FAIL) {
1138 doc_dbg("Erase/Write failed on (a) plane(s), status = %x\n",
1139 status);
1140 ret = -EIO;
1141 }
1142
1143out:
1144 doc_page_finish(docg3);
1145 return ret;
1146}
1147
1148/**
Robert Jarzmikde03cd72011-11-19 16:02:53 +01001149 * doc_erase_block - Erase a couple of blocks
1150 * @docg3: the device
1151 * @block0: the first block to erase (leftmost plane)
1152 * @block1: the second block to erase (rightmost plane)
1153 *
1154 * Erase both blocks, and return operation status
1155 *
1156 * Returns 0 if erase successful, -EIO if erase issue, -ETIMEOUT if chip not
1157 * ready for too long
1158 */
1159static int doc_erase_block(struct docg3 *docg3, int block0, int block1)
1160{
1161 int ret, sector;
1162
1163 doc_dbg("doc_erase_block(blocks=(%d,%d))\n", block0, block1);
1164 ret = doc_reset_seq(docg3);
1165 if (ret)
1166 return -EIO;
1167
1168 doc_set_reliable_mode(docg3);
1169 doc_flash_sequence(docg3, DOC_SEQ_ERASE);
1170
1171 sector = block0 << DOC_ADDR_BLOCK_SHIFT;
1172 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
1173 doc_setup_addr_sector(docg3, sector);
1174 sector = block1 << DOC_ADDR_BLOCK_SHIFT;
1175 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
1176 doc_setup_addr_sector(docg3, sector);
1177 doc_delay(docg3, 1);
1178
1179 doc_flash_command(docg3, DOC_CMD_ERASECYCLE2);
1180 doc_delay(docg3, 2);
1181
1182 if (is_prot_seq_error(docg3)) {
1183 doc_err("Erase blocks %d,%d error\n", block0, block1);
1184 return -EIO;
1185 }
1186
1187 return doc_write_erase_wait_status(docg3);
1188}
1189
1190/**
1191 * doc_erase - Erase a portion of the chip
1192 * @mtd: the device
1193 * @info: the erase info
1194 *
1195 * Erase a bunch of contiguous blocks, by pairs, as a "mtd" page of 1024 is
1196 * split into 2 pages of 512 bytes on 2 contiguous blocks.
1197 *
Octavian Moraru86d2f6f2012-03-03 16:19:39 +02001198 * Returns 0 if erase successful, -EINVAL if addressing error, -EIO if erase
Robert Jarzmikde03cd72011-11-19 16:02:53 +01001199 * issue
1200 */
1201static int doc_erase(struct mtd_info *mtd, struct erase_info *info)
1202{
1203 struct docg3 *docg3 = mtd->priv;
1204 uint64_t len;
1205 int block0, block1, page, ret, ofs = 0;
1206
1207 doc_dbg("doc_erase(from=%lld, len=%lld\n", info->addr, info->len);
Robert Jarzmikde03cd72011-11-19 16:02:53 +01001208
1209 info->state = MTD_ERASE_PENDING;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001210 calc_block_sector(info->addr + info->len, &block0, &block1, &page,
1211 &ofs, docg3->reliable);
Robert Jarzmikde03cd72011-11-19 16:02:53 +01001212 ret = -EINVAL;
Robert Jarzmika7baef12012-03-22 21:00:50 +01001213 if (info->addr + info->len > mtd->size || page || ofs)
Robert Jarzmikde03cd72011-11-19 16:02:53 +01001214 goto reset_err;
1215
1216 ret = 0;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001217 calc_block_sector(info->addr, &block0, &block1, &page, &ofs,
1218 docg3->reliable);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001219 mutex_lock(&docg3->cascade->lock);
1220 doc_set_device_id(docg3, docg3->device_id);
Robert Jarzmikde03cd72011-11-19 16:02:53 +01001221 doc_set_reliable_mode(docg3);
1222 for (len = info->len; !ret && len > 0; len -= mtd->erasesize) {
1223 info->state = MTD_ERASING;
1224 ret = doc_erase_block(docg3, block0, block1);
1225 block0 += 2;
1226 block1 += 2;
1227 }
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001228 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmikde03cd72011-11-19 16:02:53 +01001229
1230 if (ret)
1231 goto reset_err;
1232
1233 info->state = MTD_ERASE_DONE;
1234 return 0;
1235
1236reset_err:
1237 info->state = MTD_ERASE_FAILED;
1238 return ret;
1239}
1240
1241/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001242 * doc_write_page - Write a single page to the chip
1243 * @docg3: the device
1244 * @to: the offset from first block and first page, in bytes, aligned on page
1245 * size
1246 * @buf: buffer to get bytes from
1247 * @oob: buffer to get out of band bytes from (can be NULL if no OOB should be
1248 * written)
1249 * @autoecc: if 0, all 16 bytes from OOB are taken, regardless of HW Hamming or
1250 * BCH computations. If 1, only bytes 0-7 and byte 15 are taken,
1251 * remaining ones are filled with hardware Hamming and BCH
1252 * computations. Its value is not meaningfull is oob == NULL.
1253 *
1254 * Write one full page (ie. 1 page split on two planes), of 512 bytes, with the
1255 * OOB data. The OOB ECC is automatically computed by the hardware Hamming and
1256 * BCH generator if autoecc is not null.
1257 *
1258 * Returns 0 if write successful, -EIO if write error, -EAGAIN if timeout
1259 */
1260static int doc_write_page(struct docg3 *docg3, loff_t to, const u_char *buf,
1261 const u_char *oob, int autoecc)
1262{
1263 int block0, block1, page, ret, ofs = 0;
Robert Jarzmikb6044362011-12-02 20:00:12 +01001264 u8 hwecc[DOC_ECC_BCH_SIZE], hamming;
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001265
1266 doc_dbg("doc_write_page(to=%lld)\n", to);
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001267 calc_block_sector(to, &block0, &block1, &page, &ofs, docg3->reliable);
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001268
1269 doc_set_device_id(docg3, docg3->device_id);
1270 ret = doc_reset_seq(docg3);
1271 if (ret)
1272 goto err;
1273
1274 /* Program the flash address block and page */
1275 ret = doc_write_seek(docg3, block0, block1, page, ofs);
1276 if (ret)
1277 goto err;
1278
Robert Jarzmikd13d19e2011-11-19 16:02:55 +01001279 doc_write_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001280 doc_delay(docg3, 2);
1281 doc_write_page_putbytes(docg3, DOC_LAYOUT_PAGE_SIZE, buf);
1282
1283 if (oob && autoecc) {
1284 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ, oob);
1285 doc_delay(docg3, 2);
1286 oob += DOC_LAYOUT_OOB_UNUSED_OFS;
1287
1288 hamming = doc_register_readb(docg3, DOC_HAMMINGPARITY);
1289 doc_delay(docg3, 2);
1290 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_HAMMING_SZ,
1291 &hamming);
1292 doc_delay(docg3, 2);
1293
Robert Jarzmikb6044362011-12-02 20:00:12 +01001294 doc_get_bch_hw_ecc(docg3, hwecc);
1295 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_BCH_SZ, hwecc);
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001296 doc_delay(docg3, 2);
1297
1298 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_UNUSED_SZ, oob);
1299 }
1300 if (oob && !autoecc)
1301 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_SIZE, oob);
1302
1303 doc_delay(docg3, 2);
1304 doc_page_finish(docg3);
1305 doc_delay(docg3, 2);
1306 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE2);
1307 doc_delay(docg3, 2);
1308
1309 /*
1310 * The wait status will perform another doc_page_finish() call, but that
1311 * seems to please the docg3, so leave it.
1312 */
1313 ret = doc_write_erase_wait_status(docg3);
1314 return ret;
1315err:
1316 doc_read_page_finish(docg3);
1317 return ret;
1318}
1319
1320/**
1321 * doc_guess_autoecc - Guess autoecc mode from mbd_oob_ops
1322 * @ops: the oob operations
1323 *
1324 * Returns 0 or 1 if success, -EINVAL if invalid oob mode
1325 */
1326static int doc_guess_autoecc(struct mtd_oob_ops *ops)
1327{
1328 int autoecc;
1329
1330 switch (ops->mode) {
1331 case MTD_OPS_PLACE_OOB:
1332 case MTD_OPS_AUTO_OOB:
1333 autoecc = 1;
1334 break;
1335 case MTD_OPS_RAW:
1336 autoecc = 0;
1337 break;
1338 default:
1339 autoecc = -EINVAL;
1340 }
1341 return autoecc;
1342}
1343
1344/**
1345 * doc_fill_autooob - Fill a 16 bytes OOB from 8 non-ECC bytes
1346 * @dst: the target 16 bytes OOB buffer
1347 * @oobsrc: the source 8 bytes non-ECC OOB buffer
1348 *
1349 */
1350static void doc_fill_autooob(u8 *dst, u8 *oobsrc)
1351{
1352 memcpy(dst, oobsrc, DOC_LAYOUT_OOB_PAGEINFO_SZ);
1353 dst[DOC_LAYOUT_OOB_UNUSED_OFS] = oobsrc[DOC_LAYOUT_OOB_PAGEINFO_SZ];
1354}
1355
1356/**
1357 * doc_backup_oob - Backup OOB into docg3 structure
1358 * @docg3: the device
1359 * @to: the page offset in the chip
1360 * @ops: the OOB size and buffer
1361 *
1362 * As the docg3 should write a page with its OOB in one pass, and some userland
1363 * applications do write_oob() to setup the OOB and then write(), store the OOB
1364 * into a temporary storage. This is very dangerous, as 2 concurrent
1365 * applications could store an OOB, and then write their pages (which will
1366 * result into one having its OOB corrupted).
1367 *
1368 * The only reliable way would be for userland to call doc_write_oob() with both
1369 * the page data _and_ the OOB area.
1370 *
1371 * Returns 0 if success, -EINVAL if ops content invalid
1372 */
1373static int doc_backup_oob(struct docg3 *docg3, loff_t to,
1374 struct mtd_oob_ops *ops)
1375{
1376 int ooblen = ops->ooblen, autoecc;
1377
1378 if (ooblen != DOC_LAYOUT_OOB_SIZE)
1379 return -EINVAL;
1380 autoecc = doc_guess_autoecc(ops);
1381 if (autoecc < 0)
1382 return autoecc;
1383
1384 docg3->oob_write_ofs = to;
1385 docg3->oob_autoecc = autoecc;
1386 if (ops->mode == MTD_OPS_AUTO_OOB) {
1387 doc_fill_autooob(docg3->oob_write_buf, ops->oobbuf);
1388 ops->oobretlen = 8;
1389 } else {
1390 memcpy(docg3->oob_write_buf, ops->oobbuf, DOC_LAYOUT_OOB_SIZE);
1391 ops->oobretlen = DOC_LAYOUT_OOB_SIZE;
1392 }
1393 return 0;
1394}
1395
1396/**
1397 * doc_write_oob - Write out of band bytes to flash
1398 * @mtd: the device
1399 * @ofs: the offset from first block and first page, in bytes, aligned on page
1400 * size
1401 * @ops: the mtd oob structure
1402 *
1403 * Either write OOB data into a temporary buffer, for the subsequent write
1404 * page. The provided OOB should be 16 bytes long. If a data buffer is provided
1405 * as well, issue the page write.
1406 * Or provide data without OOB, and then a all zeroed OOB will be used (ECC will
1407 * still be filled in if asked for).
1408 *
Octavian Moraru86d2f6f2012-03-03 16:19:39 +02001409 * Returns 0 is successful, EINVAL if length is not 14 bytes
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001410 */
1411static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
1412 struct mtd_oob_ops *ops)
1413{
1414 struct docg3 *docg3 = mtd->priv;
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001415 int ret, autoecc, oobdelta;
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001416 u8 *oobbuf = ops->oobbuf;
1417 u8 *buf = ops->datbuf;
1418 size_t len, ooblen;
1419 u8 oob[DOC_LAYOUT_OOB_SIZE];
1420
1421 if (buf)
1422 len = ops->len;
1423 else
1424 len = 0;
1425 if (oobbuf)
1426 ooblen = ops->ooblen;
1427 else
1428 ooblen = 0;
1429
1430 if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
1431 oobbuf += ops->ooboffs;
1432
1433 doc_dbg("doc_write_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
1434 ofs, ops->mode, buf, len, oobbuf, ooblen);
1435 switch (ops->mode) {
1436 case MTD_OPS_PLACE_OOB:
1437 case MTD_OPS_RAW:
1438 oobdelta = mtd->oobsize;
1439 break;
1440 case MTD_OPS_AUTO_OOB:
1441 oobdelta = mtd->ecclayout->oobavail;
1442 break;
1443 default:
Dan Carpenter6c810f92012-11-29 17:16:51 +03001444 return -EINVAL;
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001445 }
1446 if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % oobdelta) ||
1447 (ofs % DOC_LAYOUT_PAGE_SIZE))
1448 return -EINVAL;
1449 if (len && ooblen &&
1450 (len / DOC_LAYOUT_PAGE_SIZE) != (ooblen / oobdelta))
1451 return -EINVAL;
Robert Jarzmika7baef12012-03-22 21:00:50 +01001452 if (ofs + len > mtd->size)
1453 return -EINVAL;
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001454
1455 ops->oobretlen = 0;
1456 ops->retlen = 0;
1457 ret = 0;
1458 if (len == 0 && ooblen == 0)
1459 return -EINVAL;
1460 if (len == 0 && ooblen > 0)
1461 return doc_backup_oob(docg3, ofs, ops);
1462
1463 autoecc = doc_guess_autoecc(ops);
1464 if (autoecc < 0)
1465 return autoecc;
1466
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001467 mutex_lock(&docg3->cascade->lock);
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001468 while (!ret && len > 0) {
1469 memset(oob, 0, sizeof(oob));
1470 if (ofs == docg3->oob_write_ofs)
1471 memcpy(oob, docg3->oob_write_buf, DOC_LAYOUT_OOB_SIZE);
1472 else if (ooblen > 0 && ops->mode == MTD_OPS_AUTO_OOB)
1473 doc_fill_autooob(oob, oobbuf);
1474 else if (ooblen > 0)
1475 memcpy(oob, oobbuf, DOC_LAYOUT_OOB_SIZE);
1476 ret = doc_write_page(docg3, ofs, buf, oob, autoecc);
1477
1478 ofs += DOC_LAYOUT_PAGE_SIZE;
1479 len -= DOC_LAYOUT_PAGE_SIZE;
1480 buf += DOC_LAYOUT_PAGE_SIZE;
1481 if (ooblen) {
1482 oobbuf += oobdelta;
1483 ooblen -= oobdelta;
1484 ops->oobretlen += oobdelta;
1485 }
1486 ops->retlen += DOC_LAYOUT_PAGE_SIZE;
1487 }
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001488
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001489 doc_set_device_id(docg3, 0);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001490 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001491 return ret;
1492}
1493
1494/**
1495 * doc_write - Write a buffer to the chip
1496 * @mtd: the device
1497 * @to: the offset from first block and first page, in bytes, aligned on page
1498 * size
1499 * @len: the number of bytes to write (must be a full page size, ie. 512)
1500 * @retlen: the number of bytes actually written (0 or 512)
1501 * @buf: the buffer to get bytes from
1502 *
1503 * Writes data to the chip.
1504 *
1505 * Returns 0 if write successful, -EIO if write error
1506 */
1507static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
1508 size_t *retlen, const u_char *buf)
1509{
1510 struct docg3 *docg3 = mtd->priv;
1511 int ret;
1512 struct mtd_oob_ops ops;
1513
1514 doc_dbg("doc_write(to=%lld, len=%zu)\n", to, len);
1515 ops.datbuf = (char *)buf;
1516 ops.len = len;
1517 ops.mode = MTD_OPS_PLACE_OOB;
1518 ops.oobbuf = NULL;
1519 ops.ooblen = 0;
1520 ops.ooboffs = 0;
1521
1522 ret = doc_write_oob(mtd, to, &ops);
1523 *retlen = ops.retlen;
1524 return ret;
1525}
1526
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001527static struct docg3 *sysfs_dev2docg3(struct device *dev,
1528 struct device_attribute *attr)
1529{
1530 int floor;
1531 struct platform_device *pdev = to_platform_device(dev);
1532 struct mtd_info **docg3_floors = platform_get_drvdata(pdev);
1533
1534 floor = attr->attr.name[1] - '0';
1535 if (floor < 0 || floor >= DOC_MAX_NBFLOORS)
1536 return NULL;
1537 else
1538 return docg3_floors[floor]->priv;
1539}
1540
1541static ssize_t dps0_is_key_locked(struct device *dev,
1542 struct device_attribute *attr, char *buf)
1543{
1544 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1545 int dps0;
1546
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001547 mutex_lock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001548 doc_set_device_id(docg3, docg3->device_id);
1549 dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
1550 doc_set_device_id(docg3, 0);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001551 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001552
1553 return sprintf(buf, "%d\n", !(dps0 & DOC_DPS_KEY_OK));
1554}
1555
1556static ssize_t dps1_is_key_locked(struct device *dev,
1557 struct device_attribute *attr, char *buf)
1558{
1559 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1560 int dps1;
1561
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001562 mutex_lock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001563 doc_set_device_id(docg3, docg3->device_id);
1564 dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
1565 doc_set_device_id(docg3, 0);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001566 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001567
1568 return sprintf(buf, "%d\n", !(dps1 & DOC_DPS_KEY_OK));
1569}
1570
1571static ssize_t dps0_insert_key(struct device *dev,
1572 struct device_attribute *attr,
1573 const char *buf, size_t count)
1574{
1575 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1576 int i;
1577
1578 if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
1579 return -EINVAL;
1580
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001581 mutex_lock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001582 doc_set_device_id(docg3, docg3->device_id);
1583 for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
1584 doc_writeb(docg3, buf[i], DOC_DPS0_KEY);
1585 doc_set_device_id(docg3, 0);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001586 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001587 return count;
1588}
1589
1590static ssize_t dps1_insert_key(struct device *dev,
1591 struct device_attribute *attr,
1592 const char *buf, size_t count)
1593{
1594 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1595 int i;
1596
1597 if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
1598 return -EINVAL;
1599
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001600 mutex_lock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001601 doc_set_device_id(docg3, docg3->device_id);
1602 for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
1603 doc_writeb(docg3, buf[i], DOC_DPS1_KEY);
1604 doc_set_device_id(docg3, 0);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001605 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001606 return count;
1607}
1608
1609#define FLOOR_SYSFS(id) { \
1610 __ATTR(f##id##_dps0_is_keylocked, S_IRUGO, dps0_is_key_locked, NULL), \
1611 __ATTR(f##id##_dps1_is_keylocked, S_IRUGO, dps1_is_key_locked, NULL), \
Rusty Russell993bcc62014-05-14 10:33:47 +09301612 __ATTR(f##id##_dps0_protection_key, S_IWUSR|S_IWGRP, NULL, dps0_insert_key), \
1613 __ATTR(f##id##_dps1_protection_key, S_IWUSR|S_IWGRP, NULL, dps1_insert_key), \
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001614}
1615
1616static struct device_attribute doc_sys_attrs[DOC_MAX_NBFLOORS][4] = {
1617 FLOOR_SYSFS(0), FLOOR_SYSFS(1), FLOOR_SYSFS(2), FLOOR_SYSFS(3)
1618};
1619
1620static int doc_register_sysfs(struct platform_device *pdev,
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001621 struct docg3_cascade *cascade)
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001622{
1623 int ret = 0, floor, i = 0;
1624 struct device *dev = &pdev->dev;
1625
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001626 for (floor = 0; !ret && floor < DOC_MAX_NBFLOORS &&
1627 cascade->floors[floor]; floor++)
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001628 for (i = 0; !ret && i < 4; i++)
1629 ret = device_create_file(dev, &doc_sys_attrs[floor][i]);
1630 if (!ret)
1631 return 0;
1632 do {
1633 while (--i >= 0)
1634 device_remove_file(dev, &doc_sys_attrs[floor][i]);
1635 i = 4;
1636 } while (--floor >= 0);
1637 return ret;
1638}
1639
1640static void doc_unregister_sysfs(struct platform_device *pdev,
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001641 struct docg3_cascade *cascade)
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001642{
1643 struct device *dev = &pdev->dev;
1644 int floor, i;
1645
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001646 for (floor = 0; floor < DOC_MAX_NBFLOORS && cascade->floors[floor];
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001647 floor++)
1648 for (i = 0; i < 4; i++)
1649 device_remove_file(dev, &doc_sys_attrs[floor][i]);
1650}
1651
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001652/*
1653 * Debug sysfs entries
1654 */
1655static int dbg_flashctrl_show(struct seq_file *s, void *p)
1656{
1657 struct docg3 *docg3 = (struct docg3 *)s->private;
1658
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001659 u8 fctrl;
1660
1661 mutex_lock(&docg3->cascade->lock);
1662 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
1663 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001664
Joe Perches8c98d252014-09-29 16:08:27 -07001665 seq_printf(s, "FlashControl : 0x%02x (%s,CE# %s,%s,%s,flash %s)\n",
1666 fctrl,
1667 fctrl & DOC_CTRL_VIOLATION ? "protocol violation" : "-",
1668 fctrl & DOC_CTRL_CE ? "active" : "inactive",
1669 fctrl & DOC_CTRL_PROTECTION_ERROR ? "protection error" : "-",
1670 fctrl & DOC_CTRL_SEQUENCE_ERROR ? "sequence error" : "-",
1671 fctrl & DOC_CTRL_FLASHREADY ? "ready" : "not ready");
1672
1673 return 0;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001674}
1675DEBUGFS_RO_ATTR(flashcontrol, dbg_flashctrl_show);
1676
1677static int dbg_asicmode_show(struct seq_file *s, void *p)
1678{
1679 struct docg3 *docg3 = (struct docg3 *)s->private;
1680
Joe Perches8c98d252014-09-29 16:08:27 -07001681 int pctrl, mode;
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001682
1683 mutex_lock(&docg3->cascade->lock);
1684 pctrl = doc_register_readb(docg3, DOC_ASICMODE);
1685 mode = pctrl & 0x03;
1686 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001687
Joe Perches8c98d252014-09-29 16:08:27 -07001688 seq_printf(s,
1689 "%04x : RAM_WE=%d,RSTIN_RESET=%d,BDETCT_RESET=%d,WRITE_ENABLE=%d,POWERDOWN=%d,MODE=%d%d (",
1690 pctrl,
1691 pctrl & DOC_ASICMODE_RAM_WE ? 1 : 0,
1692 pctrl & DOC_ASICMODE_RSTIN_RESET ? 1 : 0,
1693 pctrl & DOC_ASICMODE_BDETCT_RESET ? 1 : 0,
1694 pctrl & DOC_ASICMODE_MDWREN ? 1 : 0,
1695 pctrl & DOC_ASICMODE_POWERDOWN ? 1 : 0,
1696 mode >> 1, mode & 0x1);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001697
1698 switch (mode) {
1699 case DOC_ASICMODE_RESET:
Joe Perches8c98d252014-09-29 16:08:27 -07001700 seq_puts(s, "reset");
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001701 break;
1702 case DOC_ASICMODE_NORMAL:
Joe Perches8c98d252014-09-29 16:08:27 -07001703 seq_puts(s, "normal");
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001704 break;
1705 case DOC_ASICMODE_POWERDOWN:
Joe Perches8c98d252014-09-29 16:08:27 -07001706 seq_puts(s, "powerdown");
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001707 break;
1708 }
Joe Perches8c98d252014-09-29 16:08:27 -07001709 seq_puts(s, ")\n");
1710 return 0;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001711}
1712DEBUGFS_RO_ATTR(asic_mode, dbg_asicmode_show);
1713
1714static int dbg_device_id_show(struct seq_file *s, void *p)
1715{
1716 struct docg3 *docg3 = (struct docg3 *)s->private;
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001717 int id;
1718
1719 mutex_lock(&docg3->cascade->lock);
1720 id = doc_register_readb(docg3, DOC_DEVICESELECT);
1721 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001722
Joe Perches8c98d252014-09-29 16:08:27 -07001723 seq_printf(s, "DeviceId = %d\n", id);
1724 return 0;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001725}
1726DEBUGFS_RO_ATTR(device_id, dbg_device_id_show);
1727
1728static int dbg_protection_show(struct seq_file *s, void *p)
1729{
1730 struct docg3 *docg3 = (struct docg3 *)s->private;
Robert Jarzmikdbc26d92011-11-19 16:02:45 +01001731 int protect, dps0, dps0_low, dps0_high, dps1, dps1_low, dps1_high;
1732
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001733 mutex_lock(&docg3->cascade->lock);
Robert Jarzmikdbc26d92011-11-19 16:02:45 +01001734 protect = doc_register_readb(docg3, DOC_PROTECTION);
1735 dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
1736 dps0_low = doc_register_readw(docg3, DOC_DPS0_ADDRLOW);
1737 dps0_high = doc_register_readw(docg3, DOC_DPS0_ADDRHIGH);
1738 dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
1739 dps1_low = doc_register_readw(docg3, DOC_DPS1_ADDRLOW);
1740 dps1_high = doc_register_readw(docg3, DOC_DPS1_ADDRHIGH);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001741 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001742
Joe Perches8c98d252014-09-29 16:08:27 -07001743 seq_printf(s, "Protection = 0x%02x (", protect);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001744 if (protect & DOC_PROTECT_FOUNDRY_OTP_LOCK)
Joe Perches8c98d252014-09-29 16:08:27 -07001745 seq_puts(s, "FOUNDRY_OTP_LOCK,");
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001746 if (protect & DOC_PROTECT_CUSTOMER_OTP_LOCK)
Joe Perches8c98d252014-09-29 16:08:27 -07001747 seq_puts(s, "CUSTOMER_OTP_LOCK,");
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001748 if (protect & DOC_PROTECT_LOCK_INPUT)
Joe Perches8c98d252014-09-29 16:08:27 -07001749 seq_puts(s, "LOCK_INPUT,");
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001750 if (protect & DOC_PROTECT_STICKY_LOCK)
Joe Perches8c98d252014-09-29 16:08:27 -07001751 seq_puts(s, "STICKY_LOCK,");
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001752 if (protect & DOC_PROTECT_PROTECTION_ENABLED)
Joe Perches8c98d252014-09-29 16:08:27 -07001753 seq_puts(s, "PROTECTION ON,");
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001754 if (protect & DOC_PROTECT_IPL_DOWNLOAD_LOCK)
Joe Perches8c98d252014-09-29 16:08:27 -07001755 seq_puts(s, "IPL_DOWNLOAD_LOCK,");
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001756 if (protect & DOC_PROTECT_PROTECTION_ERROR)
Joe Perches8c98d252014-09-29 16:08:27 -07001757 seq_puts(s, "PROTECT_ERR,");
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001758 else
Joe Perches8c98d252014-09-29 16:08:27 -07001759 seq_puts(s, "NO_PROTECT_ERR");
1760 seq_puts(s, ")\n");
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001761
Joe Perches8c98d252014-09-29 16:08:27 -07001762 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",
1763 dps0, dps0_low, dps0_high,
1764 !!(dps0 & DOC_DPS_OTP_PROTECTED),
1765 !!(dps0 & DOC_DPS_READ_PROTECTED),
1766 !!(dps0 & DOC_DPS_WRITE_PROTECTED),
1767 !!(dps0 & DOC_DPS_HW_LOCK_ENABLED),
1768 !!(dps0 & DOC_DPS_KEY_OK));
1769 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",
1770 dps1, dps1_low, dps1_high,
1771 !!(dps1 & DOC_DPS_OTP_PROTECTED),
1772 !!(dps1 & DOC_DPS_READ_PROTECTED),
1773 !!(dps1 & DOC_DPS_WRITE_PROTECTED),
1774 !!(dps1 & DOC_DPS_HW_LOCK_ENABLED),
1775 !!(dps1 & DOC_DPS_KEY_OK));
1776 return 0;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001777}
1778DEBUGFS_RO_ATTR(protection, dbg_protection_show);
1779
1780static int __init doc_dbg_register(struct docg3 *docg3)
1781{
1782 struct dentry *root, *entry;
1783
1784 root = debugfs_create_dir("docg3", NULL);
1785 if (!root)
1786 return -ENOMEM;
1787
1788 entry = debugfs_create_file("flashcontrol", S_IRUSR, root, docg3,
1789 &flashcontrol_fops);
1790 if (entry)
1791 entry = debugfs_create_file("asic_mode", S_IRUSR, root,
1792 docg3, &asic_mode_fops);
1793 if (entry)
1794 entry = debugfs_create_file("device_id", S_IRUSR, root,
1795 docg3, &device_id_fops);
1796 if (entry)
1797 entry = debugfs_create_file("protection", S_IRUSR, root,
1798 docg3, &protection_fops);
1799 if (entry) {
1800 docg3->debugfs_root = root;
1801 return 0;
1802 } else {
1803 debugfs_remove_recursive(root);
1804 return -ENOMEM;
1805 }
1806}
1807
1808static void __exit doc_dbg_unregister(struct docg3 *docg3)
1809{
1810 debugfs_remove_recursive(docg3->debugfs_root);
1811}
1812
1813/**
1814 * doc_set_driver_info - Fill the mtd_info structure and docg3 structure
1815 * @chip_id: The chip ID of the supported chip
1816 * @mtd: The structure to fill
1817 */
1818static void __init doc_set_driver_info(int chip_id, struct mtd_info *mtd)
1819{
1820 struct docg3 *docg3 = mtd->priv;
1821 int cfg;
1822
1823 cfg = doc_register_readb(docg3, DOC_CONFIGURATION);
1824 docg3->if_cfg = (cfg & DOC_CONF_IF_CFG ? 1 : 0);
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001825 docg3->reliable = reliable_mode;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001826
1827 switch (chip_id) {
1828 case DOC_CHIPID_G3:
Robert Jarzmik31716a52012-03-03 21:57:33 +01001829 mtd->name = kasprintf(GFP_KERNEL, "docg3.%d",
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001830 docg3->device_id);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001831 docg3->max_block = 2047;
1832 break;
1833 }
1834 mtd->type = MTD_NANDFLASH;
Robert Jarzmik7a7fcf12011-11-19 16:02:54 +01001835 mtd->flags = MTD_CAP_NANDFLASH;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001836 mtd->size = (docg3->max_block + 1) * DOC_LAYOUT_BLOCK_SIZE;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001837 if (docg3->reliable == 2)
1838 mtd->size /= 2;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001839 mtd->erasesize = DOC_LAYOUT_BLOCK_SIZE * DOC_LAYOUT_NBPLANES;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001840 if (docg3->reliable == 2)
1841 mtd->erasesize /= 2;
Artem Bityutskiy82c4c582012-02-03 09:44:32 +02001842 mtd->writebufsize = mtd->writesize = DOC_LAYOUT_PAGE_SIZE;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001843 mtd->oobsize = DOC_LAYOUT_OOB_SIZE;
1844 mtd->owner = THIS_MODULE;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02001845 mtd->_erase = doc_erase;
1846 mtd->_read = doc_read;
1847 mtd->_write = doc_write;
1848 mtd->_read_oob = doc_read_oob;
1849 mtd->_write_oob = doc_write_oob;
1850 mtd->_block_isbad = doc_block_isbad;
Robert Jarzmik732b63b2011-11-19 16:02:49 +01001851 mtd->ecclayout = &docg3_oobinfo;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001852 mtd->ecc_strength = DOC_ECC_BCH_T;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001853}
1854
1855/**
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001856 * doc_probe_device - Check if a device is available
1857 * @base: the io space where the device is probed
1858 * @floor: the floor of the probed device
1859 * @dev: the device
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001860 * @cascade: the cascade of chips this devices will belong to
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001861 *
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001862 * Checks whether a device at the specified IO range, and floor is available.
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001863 *
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001864 * Returns a mtd_info struct if there is a device, ENODEV if none found, ENOMEM
1865 * if a memory allocation failed. If floor 0 is checked, a reset of the ASIC is
1866 * launched.
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001867 */
Ryosuke Saito30053b82012-03-15 19:19:03 +09001868static struct mtd_info * __init
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001869doc_probe_device(struct docg3_cascade *cascade, int floor, struct device *dev)
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001870{
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001871 int ret, bbt_nbpages;
1872 u16 chip_id, chip_id_inv;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001873 struct docg3 *docg3;
1874 struct mtd_info *mtd;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001875
1876 ret = -ENOMEM;
1877 docg3 = kzalloc(sizeof(struct docg3), GFP_KERNEL);
1878 if (!docg3)
1879 goto nomem1;
1880 mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
1881 if (!mtd)
1882 goto nomem2;
1883 mtd->priv = docg3;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001884 bbt_nbpages = DIV_ROUND_UP(docg3->max_block + 1,
1885 8 * DOC_LAYOUT_PAGE_SIZE);
1886 docg3->bbt = kzalloc(bbt_nbpages * DOC_LAYOUT_PAGE_SIZE, GFP_KERNEL);
1887 if (!docg3->bbt)
1888 goto nomem3;
1889
1890 docg3->dev = dev;
1891 docg3->device_id = floor;
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001892 docg3->cascade = cascade;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001893 doc_set_device_id(docg3, docg3->device_id);
1894 if (!floor)
1895 doc_set_asic_mode(docg3, DOC_ASICMODE_RESET);
1896 doc_set_asic_mode(docg3, DOC_ASICMODE_NORMAL);
1897
1898 chip_id = doc_register_readw(docg3, DOC_CHIPID);
1899 chip_id_inv = doc_register_readw(docg3, DOC_CHIPID_INV);
1900
1901 ret = 0;
1902 if (chip_id != (u16)(~chip_id_inv)) {
1903 goto nomem3;
1904 }
1905
1906 switch (chip_id) {
1907 case DOC_CHIPID_G3:
1908 doc_info("Found a G3 DiskOnChip at addr %p, floor %d\n",
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001909 docg3->cascade->base, floor);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001910 break;
1911 default:
1912 doc_err("Chip id %04x is not a DiskOnChip G3 chip\n", chip_id);
1913 goto nomem3;
1914 }
1915
1916 doc_set_driver_info(chip_id, mtd);
1917
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001918 doc_hamming_ecc_init(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001919 doc_reload_bbt(docg3);
1920 return mtd;
1921
1922nomem3:
1923 kfree(mtd);
1924nomem2:
1925 kfree(docg3);
1926nomem1:
1927 return ERR_PTR(ret);
1928}
1929
1930/**
1931 * doc_release_device - Release a docg3 floor
1932 * @mtd: the device
1933 */
1934static void doc_release_device(struct mtd_info *mtd)
1935{
1936 struct docg3 *docg3 = mtd->priv;
1937
1938 mtd_device_unregister(mtd);
1939 kfree(docg3->bbt);
1940 kfree(docg3);
1941 kfree(mtd->name);
1942 kfree(mtd);
1943}
1944
1945/**
Robert Jarzmike4b2a962011-11-19 16:02:56 +01001946 * docg3_resume - Awakens docg3 floor
1947 * @pdev: platfrom device
1948 *
Octavian Moraru86d2f6f2012-03-03 16:19:39 +02001949 * Returns 0 (always successful)
Robert Jarzmike4b2a962011-11-19 16:02:56 +01001950 */
1951static int docg3_resume(struct platform_device *pdev)
1952{
1953 int i;
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001954 struct docg3_cascade *cascade;
Robert Jarzmike4b2a962011-11-19 16:02:56 +01001955 struct mtd_info **docg3_floors, *mtd;
1956 struct docg3 *docg3;
1957
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001958 cascade = platform_get_drvdata(pdev);
1959 docg3_floors = cascade->floors;
Robert Jarzmike4b2a962011-11-19 16:02:56 +01001960 mtd = docg3_floors[0];
1961 docg3 = mtd->priv;
1962
1963 doc_dbg("docg3_resume()\n");
1964 for (i = 0; i < 12; i++)
1965 doc_readb(docg3, DOC_IOSPACE_IPL);
1966 return 0;
1967}
1968
1969/**
1970 * docg3_suspend - Put in low power mode the docg3 floor
1971 * @pdev: platform device
1972 * @state: power state
1973 *
1974 * Shuts off most of docg3 circuitery to lower power consumption.
1975 *
1976 * Returns 0 if suspend succeeded, -EIO if chip refused suspend
1977 */
1978static int docg3_suspend(struct platform_device *pdev, pm_message_t state)
1979{
1980 int floor, i;
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001981 struct docg3_cascade *cascade;
Robert Jarzmike4b2a962011-11-19 16:02:56 +01001982 struct mtd_info **docg3_floors, *mtd;
1983 struct docg3 *docg3;
1984 u8 ctrl, pwr_down;
1985
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001986 cascade = platform_get_drvdata(pdev);
1987 docg3_floors = cascade->floors;
Robert Jarzmike4b2a962011-11-19 16:02:56 +01001988 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
1989 mtd = docg3_floors[floor];
1990 if (!mtd)
1991 continue;
1992 docg3 = mtd->priv;
1993
1994 doc_writeb(docg3, floor, DOC_DEVICESELECT);
1995 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
1996 ctrl &= ~DOC_CTRL_VIOLATION & ~DOC_CTRL_CE;
1997 doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
1998
1999 for (i = 0; i < 10; i++) {
2000 usleep_range(3000, 4000);
2001 pwr_down = doc_register_readb(docg3, DOC_POWERMODE);
2002 if (pwr_down & DOC_POWERDOWN_READY)
2003 break;
2004 }
2005 if (pwr_down & DOC_POWERDOWN_READY) {
2006 doc_dbg("docg3_suspend(): floor %d powerdown ok\n",
2007 floor);
2008 } else {
2009 doc_err("docg3_suspend(): floor %d powerdown failed\n",
2010 floor);
2011 return -EIO;
2012 }
2013 }
2014
2015 mtd = docg3_floors[0];
2016 docg3 = mtd->priv;
2017 doc_set_asic_mode(docg3, DOC_ASICMODE_POWERDOWN);
2018 return 0;
2019}
2020
2021/**
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002022 * doc_probe - Probe the IO space for a DiskOnChip G3 chip
2023 * @pdev: platform device
2024 *
2025 * Probes for a G3 chip at the specified IO space in the platform data
2026 * ressources. The floor 0 must be available.
2027 *
2028 * Returns 0 on success, -ENOMEM, -ENXIO on error
2029 */
2030static int __init docg3_probe(struct platform_device *pdev)
2031{
2032 struct device *dev = &pdev->dev;
2033 struct mtd_info *mtd;
2034 struct resource *ress;
2035 void __iomem *base;
2036 int ret, floor, found = 0;
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002037 struct docg3_cascade *cascade;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002038
2039 ret = -ENXIO;
2040 ress = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2041 if (!ress) {
2042 dev_err(dev, "No I/O memory resource defined\n");
Jingoo Han82402ae2013-12-26 10:40:52 +09002043 return ret;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002044 }
Jingoo Han82402ae2013-12-26 10:40:52 +09002045 base = devm_ioremap(dev, ress->start, DOC_IOSPACE_SIZE);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002046
2047 ret = -ENOMEM;
Jingoo Han82402ae2013-12-26 10:40:52 +09002048 cascade = devm_kzalloc(dev, sizeof(*cascade) * DOC_MAX_NBFLOORS,
2049 GFP_KERNEL);
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002050 if (!cascade)
Jingoo Han82402ae2013-12-26 10:40:52 +09002051 return ret;
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002052 cascade->base = base;
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01002053 mutex_init(&cascade->lock);
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002054 cascade->bch = init_bch(DOC_ECC_BCH_M, DOC_ECC_BCH_T,
Robert Jarzmikd13d19e2011-11-19 16:02:55 +01002055 DOC_ECC_BCH_PRIMPOLY);
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002056 if (!cascade->bch)
Jingoo Han82402ae2013-12-26 10:40:52 +09002057 return ret;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002058
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002059 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002060 mtd = doc_probe_device(cascade, floor, dev);
Dan Carpenterb49e3452011-11-28 16:53:13 +03002061 if (IS_ERR(mtd)) {
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002062 ret = PTR_ERR(mtd);
Dan Carpenterb49e3452011-11-28 16:53:13 +03002063 goto err_probe;
2064 }
2065 if (!mtd) {
2066 if (floor == 0)
2067 goto notfound;
2068 else
2069 continue;
2070 }
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002071 cascade->floors[floor] = mtd;
Dan Carpenterb49e3452011-11-28 16:53:13 +03002072 ret = mtd_device_parse_register(mtd, part_probes, NULL, NULL,
2073 0);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002074 if (ret)
2075 goto err_probe;
Dan Carpenterb49e3452011-11-28 16:53:13 +03002076 found++;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002077 }
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002078
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002079 ret = doc_register_sysfs(pdev, cascade);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01002080 if (ret)
2081 goto err_probe;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002082 if (!found)
2083 goto notfound;
2084
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002085 platform_set_drvdata(pdev, cascade);
2086 doc_dbg_register(cascade->floors[0]->priv);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002087 return 0;
2088
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002089notfound:
2090 ret = -ENODEV;
2091 dev_info(dev, "No supported DiskOnChip found\n");
2092err_probe:
Wei Yongjun39ac9ca2013-10-11 12:40:21 +08002093 free_bch(cascade->bch);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002094 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002095 if (cascade->floors[floor])
2096 doc_release_device(cascade->floors[floor]);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002097 return ret;
2098}
2099
2100/**
2101 * docg3_release - Release the driver
2102 * @pdev: the platform device
2103 *
2104 * Returns 0
2105 */
2106static int __exit docg3_release(struct platform_device *pdev)
2107{
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002108 struct docg3_cascade *cascade = platform_get_drvdata(pdev);
2109 struct docg3 *docg3 = cascade->floors[0]->priv;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002110 int floor;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002111
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002112 doc_unregister_sysfs(pdev, cascade);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002113 doc_dbg_unregister(docg3);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002114 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002115 if (cascade->floors[floor])
2116 doc_release_device(cascade->floors[floor]);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002117
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002118 free_bch(docg3->cascade->bch);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002119 return 0;
2120}
2121
Brian Norrisd4efafc2014-10-22 10:17:38 -07002122#ifdef CONFIG_OF
Robert Jarzmika59459f2014-09-27 21:34:29 +02002123static struct of_device_id docg3_dt_ids[] = {
2124 { .compatible = "m-systems,diskonchip-g3" },
2125 {}
2126};
2127MODULE_DEVICE_TABLE(of, docg3_dt_ids);
Brian Norrisd4efafc2014-10-22 10:17:38 -07002128#endif
Robert Jarzmika59459f2014-09-27 21:34:29 +02002129
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002130static struct platform_driver g3_driver = {
2131 .driver = {
2132 .name = "docg3",
2133 .owner = THIS_MODULE,
Robert Jarzmika59459f2014-09-27 21:34:29 +02002134 .of_match_table = of_match_ptr(docg3_dt_ids),
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002135 },
Robert Jarzmike4b2a962011-11-19 16:02:56 +01002136 .suspend = docg3_suspend,
2137 .resume = docg3_resume,
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002138 .remove = __exit_p(docg3_release),
2139};
2140
Jingoo Han725a2272013-03-05 13:28:33 +09002141module_platform_driver_probe(g3_driver, docg3_probe);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002142
2143MODULE_LICENSE("GPL");
2144MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
2145MODULE_DESCRIPTION("MTD driver for DiskOnChip G3");