blob: 65d22a0439c63b455d9baaac3136ba57e57ad7e7 [file] [log] [blame]
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001/*
2 * Handles the M-Systems DiskOnChip G3 chip
3 *
4 * Copyright (C) 2011 Robert Jarzmik
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/errno.h>
25#include <linux/platform_device.h>
26#include <linux/string.h>
27#include <linux/slab.h>
28#include <linux/io.h>
29#include <linux/delay.h>
30#include <linux/mtd/mtd.h>
31#include <linux/mtd/partitions.h>
Robert Jarzmikd13d19e2011-11-19 16:02:55 +010032#include <linux/bitmap.h>
33#include <linux/bitrev.h>
34#include <linux/bch.h>
Robert Jarzmikefa2ca72011-10-05 15:22:34 +020035
36#include <linux/debugfs.h>
37#include <linux/seq_file.h>
38
39#define CREATE_TRACE_POINTS
40#include "docg3.h"
41
42/*
43 * This driver handles the DiskOnChip G3 flash memory.
44 *
45 * As no specification is available from M-Systems/Sandisk, this drivers lacks
46 * several functions available on the chip, as :
Robert Jarzmikefa2ca72011-10-05 15:22:34 +020047 * - IPL write
Robert Jarzmikefa2ca72011-10-05 15:22:34 +020048 *
49 * The bus data width (8bits versus 16bits) is not handled (if_cfg flag), and
50 * the driver assumes a 16bits data bus.
51 *
52 * DocG3 relies on 2 ECC algorithms, which are handled in hardware :
53 * - a 1 byte Hamming code stored in the OOB for each page
54 * - a 7 bytes BCH code stored in the OOB for each page
Robert Jarzmikd13d19e2011-11-19 16:02:55 +010055 * The BCH ECC is :
Robert Jarzmikefa2ca72011-10-05 15:22:34 +020056 * - BCH is in GF(2^14)
57 * - BCH is over data of 520 bytes (512 page + 7 page_info bytes
58 * + 1 hamming byte)
59 * - BCH can correct up to 4 bits (t = 4)
60 * - BCH syndroms are calculated in hardware, and checked in hardware as well
61 *
62 */
63
Robert Jarzmikb6044362011-12-02 20:00:12 +010064static unsigned int reliable_mode;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +010065module_param(reliable_mode, uint, 0);
66MODULE_PARM_DESC(reliable_mode, "Set the docg3 mode (0=normal MLC, 1=fast, "
67 "2=reliable) : MLC normal operations are in normal mode");
68
Robert Jarzmik732b63b2011-11-19 16:02:49 +010069/**
70 * struct docg3_oobinfo - DiskOnChip G3 OOB layout
71 * @eccbytes: 8 bytes are used (1 for Hamming ECC, 7 for BCH ECC)
72 * @eccpos: ecc positions (byte 7 is Hamming ECC, byte 8-14 are BCH ECC)
73 * @oobfree: free pageinfo bytes (byte 0 until byte 6, byte 15
74 * @oobavail: 8 available bytes remaining after ECC toll
75 */
76static struct nand_ecclayout docg3_oobinfo = {
77 .eccbytes = 8,
78 .eccpos = {7, 8, 9, 10, 11, 12, 13, 14},
79 .oobfree = {{0, 7}, {15, 1} },
80 .oobavail = 8,
81};
82
Robert Jarzmikefa2ca72011-10-05 15:22:34 +020083static inline u8 doc_readb(struct docg3 *docg3, u16 reg)
84{
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +010085 u8 val = readb(docg3->cascade->base + reg);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +020086
87 trace_docg3_io(0, 8, reg, (int)val);
88 return val;
89}
90
91static inline u16 doc_readw(struct docg3 *docg3, u16 reg)
92{
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +010093 u16 val = readw(docg3->cascade->base + reg);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +020094
95 trace_docg3_io(0, 16, reg, (int)val);
96 return val;
97}
98
99static inline void doc_writeb(struct docg3 *docg3, u8 val, u16 reg)
100{
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +0100101 writeb(val, docg3->cascade->base + reg);
Robert Jarzmik84a93052011-11-19 16:02:44 +0100102 trace_docg3_io(1, 8, reg, val);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200103}
104
105static inline void doc_writew(struct docg3 *docg3, u16 val, u16 reg)
106{
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +0100107 writew(val, docg3->cascade->base + reg);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200108 trace_docg3_io(1, 16, reg, val);
109}
110
111static inline void doc_flash_command(struct docg3 *docg3, u8 cmd)
112{
113 doc_writeb(docg3, cmd, DOC_FLASHCOMMAND);
114}
115
116static inline void doc_flash_sequence(struct docg3 *docg3, u8 seq)
117{
118 doc_writeb(docg3, seq, DOC_FLASHSEQUENCE);
119}
120
121static inline void doc_flash_address(struct docg3 *docg3, u8 addr)
122{
123 doc_writeb(docg3, addr, DOC_FLASHADDRESS);
124}
125
126static char const *part_probes[] = { "cmdlinepart", "saftlpart", NULL };
127
128static int doc_register_readb(struct docg3 *docg3, int reg)
129{
130 u8 val;
131
132 doc_writew(docg3, reg, DOC_READADDRESS);
133 val = doc_readb(docg3, reg);
134 doc_vdbg("Read register %04x : %02x\n", reg, val);
135 return val;
136}
137
138static int doc_register_readw(struct docg3 *docg3, int reg)
139{
140 u16 val;
141
142 doc_writew(docg3, reg, DOC_READADDRESS);
143 val = doc_readw(docg3, reg);
144 doc_vdbg("Read register %04x : %04x\n", reg, val);
145 return val;
146}
147
148/**
149 * doc_delay - delay docg3 operations
150 * @docg3: the device
151 * @nbNOPs: the number of NOPs to issue
152 *
153 * As no specification is available, the right timings between chip commands are
154 * unknown. The only available piece of information are the observed nops on a
155 * working docg3 chip.
156 * Therefore, doc_delay relies on a busy loop of NOPs, instead of scheduler
157 * friendlier msleep() functions or blocking mdelay().
158 */
159static void doc_delay(struct docg3 *docg3, int nbNOPs)
160{
161 int i;
162
Robert Jarzmikac48e802011-11-19 16:02:43 +0100163 doc_vdbg("NOP x %d\n", nbNOPs);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200164 for (i = 0; i < nbNOPs; i++)
165 doc_writeb(docg3, 0, DOC_NOP);
166}
167
168static int is_prot_seq_error(struct docg3 *docg3)
169{
170 int ctrl;
171
172 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
173 return ctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR);
174}
175
176static int doc_is_ready(struct docg3 *docg3)
177{
178 int ctrl;
179
180 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
181 return ctrl & DOC_CTRL_FLASHREADY;
182}
183
184static int doc_wait_ready(struct docg3 *docg3)
185{
186 int maxWaitCycles = 100;
187
188 do {
189 doc_delay(docg3, 4);
190 cpu_relax();
191 } while (!doc_is_ready(docg3) && maxWaitCycles--);
192 doc_delay(docg3, 2);
193 if (maxWaitCycles > 0)
194 return 0;
195 else
196 return -EIO;
197}
198
199static int doc_reset_seq(struct docg3 *docg3)
200{
201 int ret;
202
203 doc_writeb(docg3, 0x10, DOC_FLASHCONTROL);
204 doc_flash_sequence(docg3, DOC_SEQ_RESET);
205 doc_flash_command(docg3, DOC_CMD_RESET);
206 doc_delay(docg3, 2);
207 ret = doc_wait_ready(docg3);
208
209 doc_dbg("doc_reset_seq() -> isReady=%s\n", ret ? "false" : "true");
210 return ret;
211}
212
213/**
214 * doc_read_data_area - Read data from data area
215 * @docg3: the device
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100216 * @buf: the buffer to fill in (might be NULL is dummy reads)
217 * @len: the length to read
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200218 * @first: first time read, DOC_READADDRESS should be set
219 *
220 * Reads bytes from flash data. Handles the single byte / even bytes reads.
221 */
222static void doc_read_data_area(struct docg3 *docg3, void *buf, int len,
223 int first)
224{
225 int i, cdr, len4;
226 u16 data16, *dst16;
227 u8 data8, *dst8;
228
229 doc_dbg("doc_read_data_area(buf=%p, len=%d)\n", buf, len);
230 cdr = len & 0x3;
231 len4 = len - cdr;
232
233 if (first)
234 doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
235 dst16 = buf;
236 for (i = 0; i < len4; i += 2) {
237 data16 = doc_readw(docg3, DOC_IOSPACE_DATA);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100238 if (dst16) {
239 *dst16 = data16;
240 dst16++;
241 }
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200242 }
243
244 if (cdr) {
245 doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
246 DOC_READADDRESS);
247 doc_delay(docg3, 1);
248 dst8 = (u8 *)dst16;
249 for (i = 0; i < cdr; i++) {
250 data8 = doc_readb(docg3, DOC_IOSPACE_DATA);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100251 if (dst8) {
252 *dst8 = data8;
253 dst8++;
254 }
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200255 }
256 }
257}
258
259/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100260 * doc_write_data_area - Write data into data area
261 * @docg3: the device
262 * @buf: the buffer to get input bytes from
263 * @len: the length to write
264 *
265 * Writes bytes into flash data. Handles the single byte / even bytes writes.
266 */
267static void doc_write_data_area(struct docg3 *docg3, const void *buf, int len)
268{
269 int i, cdr, len4;
270 u16 *src16;
271 u8 *src8;
272
273 doc_dbg("doc_write_data_area(buf=%p, len=%d)\n", buf, len);
274 cdr = len & 0x3;
275 len4 = len - cdr;
276
277 doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
278 src16 = (u16 *)buf;
279 for (i = 0; i < len4; i += 2) {
280 doc_writew(docg3, *src16, DOC_IOSPACE_DATA);
281 src16++;
282 }
283
284 src8 = (u8 *)src16;
285 for (i = 0; i < cdr; i++) {
286 doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
287 DOC_READADDRESS);
288 doc_writeb(docg3, *src8, DOC_IOSPACE_DATA);
289 src8++;
290 }
291}
292
293/**
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100294 * doc_set_data_mode - Sets the flash to normal or reliable data mode
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200295 * @docg3: the device
296 *
297 * The reliable data mode is a bit slower than the fast mode, but less errors
298 * occur. Entering the reliable mode cannot be done without entering the fast
299 * mode first.
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100300 *
301 * In reliable mode, pages 2*n and 2*n+1 are clones. Writing to page 0 of blocks
302 * (4,5) make the hardware write also to page 1 of blocks blocks(4,5). Reading
303 * from page 0 of blocks (4,5) or from page 1 of blocks (4,5) gives the same
304 * result, which is a logical and between bytes from page 0 and page 1 (which is
305 * consistent with the fact that writing to a page is _clearing_ bits of that
306 * page).
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200307 */
308static void doc_set_reliable_mode(struct docg3 *docg3)
309{
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100310 static char *strmode[] = { "normal", "fast", "reliable", "invalid" };
311
312 doc_dbg("doc_set_reliable_mode(%s)\n", strmode[docg3->reliable]);
313 switch (docg3->reliable) {
314 case 0:
315 break;
316 case 1:
317 doc_flash_sequence(docg3, DOC_SEQ_SET_FASTMODE);
318 doc_flash_command(docg3, DOC_CMD_FAST_MODE);
319 break;
320 case 2:
321 doc_flash_sequence(docg3, DOC_SEQ_SET_RELIABLEMODE);
322 doc_flash_command(docg3, DOC_CMD_FAST_MODE);
323 doc_flash_command(docg3, DOC_CMD_RELIABLE_MODE);
324 break;
325 default:
326 doc_err("doc_set_reliable_mode(): invalid mode\n");
327 break;
328 }
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200329 doc_delay(docg3, 2);
330}
331
332/**
333 * doc_set_asic_mode - Set the ASIC mode
334 * @docg3: the device
335 * @mode: the mode
336 *
337 * The ASIC can work in 3 modes :
338 * - RESET: all registers are zeroed
339 * - NORMAL: receives and handles commands
340 * - POWERDOWN: minimal poweruse, flash parts shut off
341 */
342static void doc_set_asic_mode(struct docg3 *docg3, u8 mode)
343{
344 int i;
345
346 for (i = 0; i < 12; i++)
347 doc_readb(docg3, DOC_IOSPACE_IPL);
348
349 mode |= DOC_ASICMODE_MDWREN;
350 doc_dbg("doc_set_asic_mode(%02x)\n", mode);
351 doc_writeb(docg3, mode, DOC_ASICMODE);
352 doc_writeb(docg3, ~mode, DOC_ASICMODECONFIRM);
353 doc_delay(docg3, 1);
354}
355
356/**
357 * doc_set_device_id - Sets the devices id for cascaded G3 chips
358 * @docg3: the device
359 * @id: the chip to select (amongst 0, 1, 2, 3)
360 *
361 * There can be 4 cascaded G3 chips. This function selects the one which will
362 * should be the active one.
363 */
364static void doc_set_device_id(struct docg3 *docg3, int id)
365{
366 u8 ctrl;
367
368 doc_dbg("doc_set_device_id(%d)\n", id);
369 doc_writeb(docg3, id, DOC_DEVICESELECT);
370 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
371
372 ctrl &= ~DOC_CTRL_VIOLATION;
373 ctrl |= DOC_CTRL_CE;
374 doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
375}
376
377/**
378 * doc_set_extra_page_mode - Change flash page layout
379 * @docg3: the device
380 *
381 * Normally, the flash page is split into the data (512 bytes) and the out of
382 * band data (16 bytes). For each, 4 more bytes can be accessed, where the wear
383 * leveling counters are stored. To access this last area of 4 bytes, a special
384 * mode must be input to the flash ASIC.
385 *
386 * Returns 0 if no error occured, -EIO else.
387 */
388static int doc_set_extra_page_mode(struct docg3 *docg3)
389{
390 int fctrl;
391
392 doc_dbg("doc_set_extra_page_mode()\n");
393 doc_flash_sequence(docg3, DOC_SEQ_PAGE_SIZE_532);
394 doc_flash_command(docg3, DOC_CMD_PAGE_SIZE_532);
395 doc_delay(docg3, 2);
396
397 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
398 if (fctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR))
399 return -EIO;
400 else
401 return 0;
402}
403
404/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100405 * doc_setup_addr_sector - Setup blocks/page/ofs address for one plane
406 * @docg3: the device
407 * @sector: the sector
408 */
409static void doc_setup_addr_sector(struct docg3 *docg3, int sector)
410{
411 doc_delay(docg3, 1);
412 doc_flash_address(docg3, sector & 0xff);
413 doc_flash_address(docg3, (sector >> 8) & 0xff);
414 doc_flash_address(docg3, (sector >> 16) & 0xff);
415 doc_delay(docg3, 1);
416}
417
418/**
419 * doc_setup_writeaddr_sector - Setup blocks/page/ofs address for one plane
420 * @docg3: the device
421 * @sector: the sector
422 * @ofs: the offset in the page, between 0 and (512 + 16 + 512)
423 */
424static void doc_setup_writeaddr_sector(struct docg3 *docg3, int sector, int ofs)
425{
426 ofs = ofs >> 2;
427 doc_delay(docg3, 1);
428 doc_flash_address(docg3, ofs & 0xff);
429 doc_flash_address(docg3, sector & 0xff);
430 doc_flash_address(docg3, (sector >> 8) & 0xff);
431 doc_flash_address(docg3, (sector >> 16) & 0xff);
432 doc_delay(docg3, 1);
433}
434
435/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200436 * doc_seek - Set both flash planes to the specified block, page for reading
437 * @docg3: the device
438 * @block0: the first plane block index
439 * @block1: the second plane block index
440 * @page: the page index within the block
441 * @wear: if true, read will occur on the 4 extra bytes of the wear area
442 * @ofs: offset in page to read
443 *
444 * Programs the flash even and odd planes to the specific block and page.
445 * Alternatively, programs the flash to the wear area of the specified page.
446 */
447static int doc_read_seek(struct docg3 *docg3, int block0, int block1, int page,
448 int wear, int ofs)
449{
450 int sector, ret = 0;
451
452 doc_dbg("doc_seek(blocks=(%d,%d), page=%d, ofs=%d, wear=%d)\n",
453 block0, block1, page, ofs, wear);
454
455 if (!wear && (ofs < 2 * DOC_LAYOUT_PAGE_SIZE)) {
456 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
457 doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
458 doc_delay(docg3, 2);
459 } else {
460 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
461 doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
462 doc_delay(docg3, 2);
463 }
464
465 doc_set_reliable_mode(docg3);
466 if (wear)
467 ret = doc_set_extra_page_mode(docg3);
468 if (ret)
469 goto out;
470
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200471 doc_flash_sequence(docg3, DOC_SEQ_READ);
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100472 sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200473 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100474 doc_setup_addr_sector(docg3, sector);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200475
476 sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
477 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100478 doc_setup_addr_sector(docg3, sector);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200479 doc_delay(docg3, 1);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200480
481out:
482 return ret;
483}
484
485/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100486 * doc_write_seek - Set both flash planes to the specified block, page for writing
487 * @docg3: the device
488 * @block0: the first plane block index
489 * @block1: the second plane block index
490 * @page: the page index within the block
491 * @ofs: offset in page to write
492 *
493 * Programs the flash even and odd planes to the specific block and page.
494 * Alternatively, programs the flash to the wear area of the specified page.
495 */
496static int doc_write_seek(struct docg3 *docg3, int block0, int block1, int page,
497 int ofs)
498{
499 int ret = 0, sector;
500
501 doc_dbg("doc_write_seek(blocks=(%d,%d), page=%d, ofs=%d)\n",
502 block0, block1, page, ofs);
503
504 doc_set_reliable_mode(docg3);
505
506 if (ofs < 2 * DOC_LAYOUT_PAGE_SIZE) {
507 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
508 doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
509 doc_delay(docg3, 2);
510 } else {
511 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
512 doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
513 doc_delay(docg3, 2);
514 }
515
516 doc_flash_sequence(docg3, DOC_SEQ_PAGE_SETUP);
517 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1);
518
519 sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
520 doc_setup_writeaddr_sector(docg3, sector, ofs);
521
522 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE3);
523 doc_delay(docg3, 2);
524 ret = doc_wait_ready(docg3);
525 if (ret)
526 goto out;
527
528 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1);
529 sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
530 doc_setup_writeaddr_sector(docg3, sector, ofs);
531 doc_delay(docg3, 1);
532
533out:
534 return ret;
535}
536
537
538/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200539 * doc_read_page_ecc_init - Initialize hardware ECC engine
540 * @docg3: the device
541 * @len: the number of bytes covered by the ECC (BCH covered)
542 *
543 * The function does initialize the hardware ECC engine to compute the Hamming
Robert Jarzmikb6044362011-12-02 20:00:12 +0100544 * ECC (on 1 byte) and the BCH hardware ECC (on 7 bytes).
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200545 *
546 * Return 0 if succeeded, -EIO on error
547 */
548static int doc_read_page_ecc_init(struct docg3 *docg3, int len)
549{
550 doc_writew(docg3, DOC_ECCCONF0_READ_MODE
551 | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
552 | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
553 DOC_ECCCONF0);
554 doc_delay(docg3, 4);
555 doc_register_readb(docg3, DOC_FLASHCONTROL);
556 return doc_wait_ready(docg3);
557}
558
559/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100560 * doc_write_page_ecc_init - Initialize hardware BCH ECC engine
561 * @docg3: the device
562 * @len: the number of bytes covered by the ECC (BCH covered)
563 *
564 * The function does initialize the hardware ECC engine to compute the Hamming
Robert Jarzmikb6044362011-12-02 20:00:12 +0100565 * ECC (on 1 byte) and the BCH hardware ECC (on 7 bytes).
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100566 *
567 * Return 0 if succeeded, -EIO on error
568 */
569static int doc_write_page_ecc_init(struct docg3 *docg3, int len)
570{
Robert Jarzmikb6044362011-12-02 20:00:12 +0100571 doc_writew(docg3, DOC_ECCCONF0_WRITE_MODE
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100572 | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
573 | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
574 DOC_ECCCONF0);
575 doc_delay(docg3, 4);
576 doc_register_readb(docg3, DOC_FLASHCONTROL);
577 return doc_wait_ready(docg3);
578}
579
580/**
581 * doc_ecc_disable - Disable Hamming and BCH ECC hardware calculator
582 * @docg3: the device
583 *
584 * Disables the hardware ECC generator and checker, for unchecked reads (as when
585 * reading OOB only or write status byte).
586 */
587static void doc_ecc_disable(struct docg3 *docg3)
588{
589 doc_writew(docg3, DOC_ECCCONF0_READ_MODE, DOC_ECCCONF0);
590 doc_delay(docg3, 4);
591}
592
593/**
594 * doc_hamming_ecc_init - Initialize hardware Hamming ECC engine
595 * @docg3: the device
596 * @nb_bytes: the number of bytes covered by the ECC (Hamming covered)
597 *
598 * This function programs the ECC hardware to compute the hamming code on the
599 * last provided N bytes to the hardware generator.
600 */
601static void doc_hamming_ecc_init(struct docg3 *docg3, int nb_bytes)
602{
603 u8 ecc_conf1;
604
605 ecc_conf1 = doc_register_readb(docg3, DOC_ECCCONF1);
606 ecc_conf1 &= ~DOC_ECCCONF1_HAMMING_BITS_MASK;
607 ecc_conf1 |= (nb_bytes & DOC_ECCCONF1_HAMMING_BITS_MASK);
608 doc_writeb(docg3, ecc_conf1, DOC_ECCCONF1);
609}
610
611/**
Robert Jarzmikb6044362011-12-02 20:00:12 +0100612 * doc_ecc_bch_fix_data - Fix if need be read data from flash
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100613 * @docg3: the device
614 * @buf: the buffer of read data (512 + 7 + 1 bytes)
615 * @hwecc: the hardware calculated ECC.
616 * It's in fact recv_ecc ^ calc_ecc, where recv_ecc was read from OOB
617 * area data, and calc_ecc the ECC calculated by the hardware generator.
618 *
619 * Checks if the received data matches the ECC, and if an error is detected,
620 * tries to fix the bit flips (at most 4) in the buffer buf. As the docg3
621 * understands the (data, ecc, syndroms) in an inverted order in comparison to
622 * the BCH library, the function reverses the order of bits (ie. bit7 and bit0,
623 * bit6 and bit 1, ...) for all ECC data.
624 *
625 * The hardware ecc unit produces oob_ecc ^ calc_ecc. The kernel's bch
626 * algorithm is used to decode this. However the hw operates on page
627 * data in a bit order that is the reverse of that of the bch alg,
628 * requiring that the bits be reversed on the result. Thanks to Ivan
629 * Djelic for his analysis.
630 *
631 * Returns number of fixed bits (0, 1, 2, 3, 4) or -EBADMSG if too many bit
632 * errors were detected and cannot be fixed.
633 */
634static int doc_ecc_bch_fix_data(struct docg3 *docg3, void *buf, u8 *hwecc)
635{
636 u8 ecc[DOC_ECC_BCH_SIZE];
637 int errorpos[DOC_ECC_BCH_T], i, numerrs;
638
639 for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
640 ecc[i] = bitrev8(hwecc[i]);
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +0100641 numerrs = decode_bch(docg3->cascade->bch, NULL,
642 DOC_ECC_BCH_COVERED_BYTES,
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100643 NULL, ecc, NULL, errorpos);
644 BUG_ON(numerrs == -EINVAL);
645 if (numerrs < 0)
646 goto out;
647
648 for (i = 0; i < numerrs; i++)
649 errorpos[i] = (errorpos[i] & ~7) | (7 - (errorpos[i] & 7));
650 for (i = 0; i < numerrs; i++)
651 if (errorpos[i] < DOC_ECC_BCH_COVERED_BYTES*8)
652 /* error is located in data, correct it */
653 change_bit(errorpos[i], buf);
654out:
655 doc_dbg("doc_ecc_bch_fix_data: flipped %d bits\n", numerrs);
656 return numerrs;
657}
658
659
660/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200661 * doc_read_page_prepare - Prepares reading data from a flash page
662 * @docg3: the device
663 * @block0: the first plane block index on flash memory
664 * @block1: the second plane block index on flash memory
665 * @page: the page index in the block
666 * @offset: the offset in the page (must be a multiple of 4)
667 *
668 * Prepares the page to be read in the flash memory :
669 * - tell ASIC to map the flash pages
670 * - tell ASIC to be in read mode
671 *
672 * After a call to this method, a call to doc_read_page_finish is mandatory,
673 * to end the read cycle of the flash.
674 *
675 * Read data from a flash page. The length to be read must be between 0 and
676 * (page_size + oob_size + wear_size), ie. 532, and a multiple of 4 (because
677 * the extra bytes reading is not implemented).
678 *
679 * As pages are grouped by 2 (in 2 planes), reading from a page must be done
680 * in two steps:
681 * - one read of 512 bytes at offset 0
682 * - one read of 512 bytes at offset 512 + 16
683 *
684 * Returns 0 if successful, -EIO if a read error occured.
685 */
686static int doc_read_page_prepare(struct docg3 *docg3, int block0, int block1,
687 int page, int offset)
688{
689 int wear_area = 0, ret = 0;
690
691 doc_dbg("doc_read_page_prepare(blocks=(%d,%d), page=%d, ofsInPage=%d)\n",
692 block0, block1, page, offset);
693 if (offset >= DOC_LAYOUT_WEAR_OFFSET)
694 wear_area = 1;
695 if (!wear_area && offset > (DOC_LAYOUT_PAGE_OOB_SIZE * 2))
696 return -EINVAL;
697
698 doc_set_device_id(docg3, docg3->device_id);
699 ret = doc_reset_seq(docg3);
700 if (ret)
701 goto err;
702
703 /* Program the flash address block and page */
704 ret = doc_read_seek(docg3, block0, block1, page, wear_area, offset);
705 if (ret)
706 goto err;
707
708 doc_flash_command(docg3, DOC_CMD_READ_ALL_PLANES);
709 doc_delay(docg3, 2);
710 doc_wait_ready(docg3);
711
712 doc_flash_command(docg3, DOC_CMD_SET_ADDR_READ);
713 doc_delay(docg3, 1);
714 if (offset >= DOC_LAYOUT_PAGE_SIZE * 2)
715 offset -= 2 * DOC_LAYOUT_PAGE_SIZE;
716 doc_flash_address(docg3, offset >> 2);
717 doc_delay(docg3, 1);
718 doc_wait_ready(docg3);
719
720 doc_flash_command(docg3, DOC_CMD_READ_FLASH);
721
722 return 0;
723err:
724 doc_writeb(docg3, 0, DOC_DATAEND);
725 doc_delay(docg3, 2);
726 return -EIO;
727}
728
729/**
730 * doc_read_page_getbytes - Reads bytes from a prepared page
731 * @docg3: the device
732 * @len: the number of bytes to be read (must be a multiple of 4)
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100733 * @buf: the buffer to be filled in (or NULL is forget bytes)
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200734 * @first: 1 if first time read, DOC_READADDRESS should be set
735 *
736 */
737static int doc_read_page_getbytes(struct docg3 *docg3, int len, u_char *buf,
738 int first)
739{
740 doc_read_data_area(docg3, buf, len, first);
741 doc_delay(docg3, 2);
742 return len;
743}
744
745/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100746 * doc_write_page_putbytes - Writes bytes into a prepared page
747 * @docg3: the device
748 * @len: the number of bytes to be written
749 * @buf: the buffer of input bytes
750 *
751 */
752static void doc_write_page_putbytes(struct docg3 *docg3, int len,
753 const u_char *buf)
754{
755 doc_write_data_area(docg3, buf, len);
756 doc_delay(docg3, 2);
757}
758
759/**
Robert Jarzmikb6044362011-12-02 20:00:12 +0100760 * doc_get_bch_hw_ecc - Get hardware calculated BCH ECC
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200761 * @docg3: the device
Robert Jarzmikb6044362011-12-02 20:00:12 +0100762 * @hwecc: the array of 7 integers where the hardware ecc will be stored
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200763 */
Robert Jarzmikb6044362011-12-02 20:00:12 +0100764static void doc_get_bch_hw_ecc(struct docg3 *docg3, u8 *hwecc)
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200765{
766 int i;
767
768 for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
Robert Jarzmikb6044362011-12-02 20:00:12 +0100769 hwecc[i] = doc_register_readb(docg3, DOC_BCH_HW_ECC(i));
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200770}
771
772/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100773 * doc_page_finish - Ends reading/writing of a flash page
774 * @docg3: the device
775 */
776static void doc_page_finish(struct docg3 *docg3)
777{
778 doc_writeb(docg3, 0, DOC_DATAEND);
779 doc_delay(docg3, 2);
780}
781
782/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200783 * doc_read_page_finish - Ends reading of a flash page
784 * @docg3: the device
785 *
786 * As a side effect, resets the chip selector to 0. This ensures that after each
787 * read operation, the floor 0 is selected. Therefore, if the systems halts, the
788 * reboot will boot on floor 0, where the IPL is.
789 */
790static void doc_read_page_finish(struct docg3 *docg3)
791{
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100792 doc_page_finish(docg3);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200793 doc_set_device_id(docg3, 0);
794}
795
796/**
797 * calc_block_sector - Calculate blocks, pages and ofs.
798
799 * @from: offset in flash
800 * @block0: first plane block index calculated
801 * @block1: second plane block index calculated
802 * @page: page calculated
803 * @ofs: offset in page
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100804 * @reliable: 0 if docg3 in normal mode, 1 if docg3 in fast mode, 2 if docg3 in
805 * reliable mode.
806 *
807 * The calculation is based on the reliable/normal mode. In normal mode, the 64
808 * pages of a block are available. In reliable mode, as pages 2*n and 2*n+1 are
809 * clones, only 32 pages per block are available.
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200810 */
811static void calc_block_sector(loff_t from, int *block0, int *block1, int *page,
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100812 int *ofs, int reliable)
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200813{
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100814 uint sector, pages_biblock;
815
816 pages_biblock = DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_NBPLANES;
817 if (reliable == 1 || reliable == 2)
818 pages_biblock /= 2;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200819
820 sector = from / DOC_LAYOUT_PAGE_SIZE;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100821 *block0 = sector / pages_biblock * DOC_LAYOUT_NBPLANES;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200822 *block1 = *block0 + 1;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100823 *page = sector % pages_biblock;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200824 *page /= DOC_LAYOUT_NBPLANES;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100825 if (reliable == 1 || reliable == 2)
826 *page *= 2;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200827 if (sector % 2)
828 *ofs = DOC_LAYOUT_PAGE_OOB_SIZE;
829 else
830 *ofs = 0;
831}
832
833/**
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100834 * doc_read_oob - Read out of band bytes from flash
835 * @mtd: the device
836 * @from: the offset from first block and first page, in bytes, aligned on page
837 * size
838 * @ops: the mtd oob structure
839 *
840 * Reads flash memory OOB area of pages.
841 *
842 * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
843 */
844static int doc_read_oob(struct mtd_info *mtd, loff_t from,
845 struct mtd_oob_ops *ops)
846{
847 struct docg3 *docg3 = mtd->priv;
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100848 int block0, block1, page, ret, skip, ofs = 0;
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100849 u8 *oobbuf = ops->oobbuf;
850 u8 *buf = ops->datbuf;
851 size_t len, ooblen, nbdata, nboob;
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100852 u8 hwecc[DOC_ECC_BCH_SIZE], eccconf1;
Mike Dunnedbc45402012-04-25 12:06:11 -0700853 int max_bitflips = 0;
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100854
855 if (buf)
856 len = ops->len;
857 else
858 len = 0;
859 if (oobbuf)
860 ooblen = ops->ooblen;
861 else
862 ooblen = 0;
863
864 if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
865 oobbuf += ops->ooboffs;
866
867 doc_dbg("doc_read_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
868 from, ops->mode, buf, len, oobbuf, ooblen);
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100869 if (ooblen % DOC_LAYOUT_OOB_SIZE)
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100870 return -EINVAL;
871
Robert Jarzmika7baef12012-03-22 21:00:50 +0100872 if (from + len > mtd->size)
873 return -EINVAL;
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100874
875 ops->oobretlen = 0;
876 ops->retlen = 0;
877 ret = 0;
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100878 skip = from % DOC_LAYOUT_PAGE_SIZE;
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +0100879 mutex_lock(&docg3->cascade->lock);
Mike Dunnedbc45402012-04-25 12:06:11 -0700880 while (ret >= 0 && (len > 0 || ooblen > 0)) {
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100881 calc_block_sector(from - skip, &block0, &block1, &page, &ofs,
Robert Jarzmikc3de8a82011-11-19 16:02:57 +0100882 docg3->reliable);
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100883 nbdata = min_t(size_t, len, DOC_LAYOUT_PAGE_SIZE - skip);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100884 nboob = min_t(size_t, ooblen, (size_t)DOC_LAYOUT_OOB_SIZE);
885 ret = doc_read_page_prepare(docg3, block0, block1, page, ofs);
886 if (ret < 0)
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +0100887 goto out;
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100888 ret = doc_read_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100889 if (ret < 0)
890 goto err_in_read;
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100891 ret = doc_read_page_getbytes(docg3, skip, NULL, 1);
892 if (ret < skip)
893 goto err_in_read;
894 ret = doc_read_page_getbytes(docg3, nbdata, buf, 0);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100895 if (ret < nbdata)
896 goto err_in_read;
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100897 doc_read_page_getbytes(docg3,
898 DOC_LAYOUT_PAGE_SIZE - nbdata - skip,
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100899 NULL, 0);
900 ret = doc_read_page_getbytes(docg3, nboob, oobbuf, 0);
901 if (ret < nboob)
902 goto err_in_read;
903 doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE - nboob,
904 NULL, 0);
905
Robert Jarzmikb6044362011-12-02 20:00:12 +0100906 doc_get_bch_hw_ecc(docg3, hwecc);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100907 eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1);
908
909 if (nboob >= DOC_LAYOUT_OOB_SIZE) {
910 doc_dbg("OOB - INFO: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
911 oobbuf[0], oobbuf[1], oobbuf[2], oobbuf[3],
912 oobbuf[4], oobbuf[5], oobbuf[6]);
913 doc_dbg("OOB - HAMMING: %02x\n", oobbuf[7]);
914 doc_dbg("OOB - BCH_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
915 oobbuf[8], oobbuf[9], oobbuf[10], oobbuf[11],
916 oobbuf[12], oobbuf[13], oobbuf[14]);
917 doc_dbg("OOB - UNUSED: %02x\n", oobbuf[15]);
918 }
919 doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1);
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100920 doc_dbg("ECC HW_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
921 hwecc[0], hwecc[1], hwecc[2], hwecc[3], hwecc[4],
922 hwecc[5], hwecc[6]);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100923
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100924 ret = -EIO;
925 if (is_prot_seq_error(docg3))
926 goto err_in_read;
927 ret = 0;
928 if ((block0 >= DOC_LAYOUT_BLOCK_FIRST_DATA) &&
929 (eccconf1 & DOC_ECCCONF1_BCH_SYNDROM_ERR) &&
930 (eccconf1 & DOC_ECCCONF1_PAGE_IS_WRITTEN) &&
931 (ops->mode != MTD_OPS_RAW) &&
932 (nbdata == DOC_LAYOUT_PAGE_SIZE)) {
933 ret = doc_ecc_bch_fix_data(docg3, buf, hwecc);
934 if (ret < 0) {
935 mtd->ecc_stats.failed++;
936 ret = -EBADMSG;
937 }
938 if (ret > 0) {
939 mtd->ecc_stats.corrected += ret;
Mike Dunnedbc45402012-04-25 12:06:11 -0700940 max_bitflips = max(max_bitflips, ret);
941 ret = max_bitflips;
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100942 }
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100943 }
944
945 doc_read_page_finish(docg3);
946 ops->retlen += nbdata;
947 ops->oobretlen += nboob;
948 buf += nbdata;
949 oobbuf += nboob;
950 len -= nbdata;
951 ooblen -= nboob;
952 from += DOC_LAYOUT_PAGE_SIZE;
Robert Jarzmikd107bc32012-03-11 11:28:45 +0100953 skip = 0;
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100954 }
955
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +0100956out:
957 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmikd13d19e2011-11-19 16:02:55 +0100958 return ret;
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100959err_in_read:
960 doc_read_page_finish(docg3);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +0100961 goto out;
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100962}
963
964/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200965 * doc_read - Read bytes from flash
966 * @mtd: the device
967 * @from: the offset from first block and first page, in bytes, aligned on page
968 * size
969 * @len: the number of bytes to read (must be a multiple of 4)
970 * @retlen: the number of bytes actually read
971 * @buf: the filled in buffer
972 *
973 * Reads flash memory pages. This function does not read the OOB chunk, but only
974 * the page data.
975 *
976 * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
977 */
978static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
979 size_t *retlen, u_char *buf)
980{
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100981 struct mtd_oob_ops ops;
982 size_t ret;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200983
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100984 memset(&ops, 0, sizeof(ops));
985 ops.datbuf = buf;
986 ops.len = len;
987 ops.mode = MTD_OPS_AUTO_OOB;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200988
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100989 ret = doc_read_oob(mtd, from, &ops);
990 *retlen = ops.retlen;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200991 return ret;
992}
993
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200994static int doc_reload_bbt(struct docg3 *docg3)
995{
996 int block = DOC_LAYOUT_BLOCK_BBT;
997 int ret = 0, nbpages, page;
998 u_char *buf = docg3->bbt;
999
1000 nbpages = DIV_ROUND_UP(docg3->max_block + 1, 8 * DOC_LAYOUT_PAGE_SIZE);
1001 for (page = 0; !ret && (page < nbpages); page++) {
1002 ret = doc_read_page_prepare(docg3, block, block + 1,
1003 page + DOC_LAYOUT_PAGE_BBT, 0);
1004 if (!ret)
1005 ret = doc_read_page_ecc_init(docg3,
1006 DOC_LAYOUT_PAGE_SIZE);
1007 if (!ret)
1008 doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE,
1009 buf, 1);
1010 buf += DOC_LAYOUT_PAGE_SIZE;
1011 }
1012 doc_read_page_finish(docg3);
1013 return ret;
1014}
1015
1016/**
1017 * doc_block_isbad - Checks whether a block is good or not
1018 * @mtd: the device
1019 * @from: the offset to find the correct block
1020 *
1021 * Returns 1 if block is bad, 0 if block is good
1022 */
1023static int doc_block_isbad(struct mtd_info *mtd, loff_t from)
1024{
1025 struct docg3 *docg3 = mtd->priv;
1026 int block0, block1, page, ofs, is_good;
1027
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001028 calc_block_sector(from, &block0, &block1, &page, &ofs,
1029 docg3->reliable);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001030 doc_dbg("doc_block_isbad(from=%lld) => block=(%d,%d), page=%d, ofs=%d\n",
1031 from, block0, block1, page, ofs);
1032
1033 if (block0 < DOC_LAYOUT_BLOCK_FIRST_DATA)
1034 return 0;
1035 if (block1 > docg3->max_block)
1036 return -EINVAL;
1037
1038 is_good = docg3->bbt[block0 >> 3] & (1 << (block0 & 0x7));
1039 return !is_good;
1040}
1041
Robert Jarzmike10019b2011-12-16 23:25:23 +01001042#if 0
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001043/**
1044 * doc_get_erase_count - Get block erase count
1045 * @docg3: the device
1046 * @from: the offset in which the block is.
1047 *
1048 * Get the number of times a block was erased. The number is the maximum of
1049 * erase times between first and second plane (which should be equal normally).
1050 *
1051 * Returns The number of erases, or -EINVAL or -EIO on error.
1052 */
1053static int doc_get_erase_count(struct docg3 *docg3, loff_t from)
1054{
1055 u8 buf[DOC_LAYOUT_WEAR_SIZE];
1056 int ret, plane1_erase_count, plane2_erase_count;
1057 int block0, block1, page, ofs;
1058
1059 doc_dbg("doc_get_erase_count(from=%lld, buf=%p)\n", from, buf);
1060 if (from % DOC_LAYOUT_PAGE_SIZE)
1061 return -EINVAL;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001062 calc_block_sector(from, &block0, &block1, &page, &ofs, docg3->reliable);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001063 if (block1 > docg3->max_block)
1064 return -EINVAL;
1065
1066 ret = doc_reset_seq(docg3);
1067 if (!ret)
1068 ret = doc_read_page_prepare(docg3, block0, block1, page,
1069 ofs + DOC_LAYOUT_WEAR_OFFSET);
1070 if (!ret)
1071 ret = doc_read_page_getbytes(docg3, DOC_LAYOUT_WEAR_SIZE,
1072 buf, 1);
1073 doc_read_page_finish(docg3);
1074
1075 if (ret || (buf[0] != DOC_ERASE_MARK) || (buf[2] != DOC_ERASE_MARK))
1076 return -EIO;
1077 plane1_erase_count = (u8)(~buf[1]) | ((u8)(~buf[4]) << 8)
1078 | ((u8)(~buf[5]) << 16);
1079 plane2_erase_count = (u8)(~buf[3]) | ((u8)(~buf[6]) << 8)
1080 | ((u8)(~buf[7]) << 16);
1081
1082 return max(plane1_erase_count, plane2_erase_count);
1083}
Robert Jarzmike10019b2011-12-16 23:25:23 +01001084#endif
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001085
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001086/**
1087 * doc_get_op_status - get erase/write operation status
1088 * @docg3: the device
1089 *
1090 * Queries the status from the chip, and returns it
1091 *
1092 * Returns the status (bits DOC_PLANES_STATUS_*)
1093 */
1094static int doc_get_op_status(struct docg3 *docg3)
1095{
1096 u8 status;
1097
1098 doc_flash_sequence(docg3, DOC_SEQ_PLANES_STATUS);
1099 doc_flash_command(docg3, DOC_CMD_PLANES_STATUS);
1100 doc_delay(docg3, 5);
1101
1102 doc_ecc_disable(docg3);
1103 doc_read_data_area(docg3, &status, 1, 1);
1104 return status;
1105}
1106
1107/**
1108 * doc_write_erase_wait_status - wait for write or erase completion
1109 * @docg3: the device
1110 *
1111 * Wait for the chip to be ready again after erase or write operation, and check
1112 * erase/write status.
1113 *
1114 * Returns 0 if erase successfull, -EIO if erase/write issue, -ETIMEOUT if
1115 * timeout
1116 */
1117static int doc_write_erase_wait_status(struct docg3 *docg3)
1118{
Robert Jarzmika2b3d282012-03-22 21:00:51 +01001119 int i, status, ret = 0;
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001120
Robert Jarzmika2b3d282012-03-22 21:00:51 +01001121 for (i = 0; !doc_is_ready(docg3) && i < 5; i++)
1122 msleep(20);
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001123 if (!doc_is_ready(docg3)) {
1124 doc_dbg("Timeout reached and the chip is still not ready\n");
1125 ret = -EAGAIN;
1126 goto out;
1127 }
1128
1129 status = doc_get_op_status(docg3);
1130 if (status & DOC_PLANES_STATUS_FAIL) {
1131 doc_dbg("Erase/Write failed on (a) plane(s), status = %x\n",
1132 status);
1133 ret = -EIO;
1134 }
1135
1136out:
1137 doc_page_finish(docg3);
1138 return ret;
1139}
1140
1141/**
Robert Jarzmikde03cd72011-11-19 16:02:53 +01001142 * doc_erase_block - Erase a couple of blocks
1143 * @docg3: the device
1144 * @block0: the first block to erase (leftmost plane)
1145 * @block1: the second block to erase (rightmost plane)
1146 *
1147 * Erase both blocks, and return operation status
1148 *
1149 * Returns 0 if erase successful, -EIO if erase issue, -ETIMEOUT if chip not
1150 * ready for too long
1151 */
1152static int doc_erase_block(struct docg3 *docg3, int block0, int block1)
1153{
1154 int ret, sector;
1155
1156 doc_dbg("doc_erase_block(blocks=(%d,%d))\n", block0, block1);
1157 ret = doc_reset_seq(docg3);
1158 if (ret)
1159 return -EIO;
1160
1161 doc_set_reliable_mode(docg3);
1162 doc_flash_sequence(docg3, DOC_SEQ_ERASE);
1163
1164 sector = block0 << DOC_ADDR_BLOCK_SHIFT;
1165 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
1166 doc_setup_addr_sector(docg3, sector);
1167 sector = block1 << DOC_ADDR_BLOCK_SHIFT;
1168 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
1169 doc_setup_addr_sector(docg3, sector);
1170 doc_delay(docg3, 1);
1171
1172 doc_flash_command(docg3, DOC_CMD_ERASECYCLE2);
1173 doc_delay(docg3, 2);
1174
1175 if (is_prot_seq_error(docg3)) {
1176 doc_err("Erase blocks %d,%d error\n", block0, block1);
1177 return -EIO;
1178 }
1179
1180 return doc_write_erase_wait_status(docg3);
1181}
1182
1183/**
1184 * doc_erase - Erase a portion of the chip
1185 * @mtd: the device
1186 * @info: the erase info
1187 *
1188 * Erase a bunch of contiguous blocks, by pairs, as a "mtd" page of 1024 is
1189 * split into 2 pages of 512 bytes on 2 contiguous blocks.
1190 *
1191 * Returns 0 if erase successful, -EINVAL if adressing error, -EIO if erase
1192 * issue
1193 */
1194static int doc_erase(struct mtd_info *mtd, struct erase_info *info)
1195{
1196 struct docg3 *docg3 = mtd->priv;
1197 uint64_t len;
1198 int block0, block1, page, ret, ofs = 0;
1199
1200 doc_dbg("doc_erase(from=%lld, len=%lld\n", info->addr, info->len);
Robert Jarzmikde03cd72011-11-19 16:02:53 +01001201
1202 info->state = MTD_ERASE_PENDING;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001203 calc_block_sector(info->addr + info->len, &block0, &block1, &page,
1204 &ofs, docg3->reliable);
Robert Jarzmikde03cd72011-11-19 16:02:53 +01001205 ret = -EINVAL;
Robert Jarzmika7baef12012-03-22 21:00:50 +01001206 if (info->addr + info->len > mtd->size || page || ofs)
Robert Jarzmikde03cd72011-11-19 16:02:53 +01001207 goto reset_err;
1208
1209 ret = 0;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001210 calc_block_sector(info->addr, &block0, &block1, &page, &ofs,
1211 docg3->reliable);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001212 mutex_lock(&docg3->cascade->lock);
1213 doc_set_device_id(docg3, docg3->device_id);
Robert Jarzmikde03cd72011-11-19 16:02:53 +01001214 doc_set_reliable_mode(docg3);
1215 for (len = info->len; !ret && len > 0; len -= mtd->erasesize) {
1216 info->state = MTD_ERASING;
1217 ret = doc_erase_block(docg3, block0, block1);
1218 block0 += 2;
1219 block1 += 2;
1220 }
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001221 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmikde03cd72011-11-19 16:02:53 +01001222
1223 if (ret)
1224 goto reset_err;
1225
1226 info->state = MTD_ERASE_DONE;
1227 return 0;
1228
1229reset_err:
1230 info->state = MTD_ERASE_FAILED;
1231 return ret;
1232}
1233
1234/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001235 * doc_write_page - Write a single page to the chip
1236 * @docg3: the device
1237 * @to: the offset from first block and first page, in bytes, aligned on page
1238 * size
1239 * @buf: buffer to get bytes from
1240 * @oob: buffer to get out of band bytes from (can be NULL if no OOB should be
1241 * written)
1242 * @autoecc: if 0, all 16 bytes from OOB are taken, regardless of HW Hamming or
1243 * BCH computations. If 1, only bytes 0-7 and byte 15 are taken,
1244 * remaining ones are filled with hardware Hamming and BCH
1245 * computations. Its value is not meaningfull is oob == NULL.
1246 *
1247 * Write one full page (ie. 1 page split on two planes), of 512 bytes, with the
1248 * OOB data. The OOB ECC is automatically computed by the hardware Hamming and
1249 * BCH generator if autoecc is not null.
1250 *
1251 * Returns 0 if write successful, -EIO if write error, -EAGAIN if timeout
1252 */
1253static int doc_write_page(struct docg3 *docg3, loff_t to, const u_char *buf,
1254 const u_char *oob, int autoecc)
1255{
1256 int block0, block1, page, ret, ofs = 0;
Robert Jarzmikb6044362011-12-02 20:00:12 +01001257 u8 hwecc[DOC_ECC_BCH_SIZE], hamming;
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001258
1259 doc_dbg("doc_write_page(to=%lld)\n", to);
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001260 calc_block_sector(to, &block0, &block1, &page, &ofs, docg3->reliable);
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001261
1262 doc_set_device_id(docg3, docg3->device_id);
1263 ret = doc_reset_seq(docg3);
1264 if (ret)
1265 goto err;
1266
1267 /* Program the flash address block and page */
1268 ret = doc_write_seek(docg3, block0, block1, page, ofs);
1269 if (ret)
1270 goto err;
1271
Robert Jarzmikd13d19e2011-11-19 16:02:55 +01001272 doc_write_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001273 doc_delay(docg3, 2);
1274 doc_write_page_putbytes(docg3, DOC_LAYOUT_PAGE_SIZE, buf);
1275
1276 if (oob && autoecc) {
1277 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ, oob);
1278 doc_delay(docg3, 2);
1279 oob += DOC_LAYOUT_OOB_UNUSED_OFS;
1280
1281 hamming = doc_register_readb(docg3, DOC_HAMMINGPARITY);
1282 doc_delay(docg3, 2);
1283 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_HAMMING_SZ,
1284 &hamming);
1285 doc_delay(docg3, 2);
1286
Robert Jarzmikb6044362011-12-02 20:00:12 +01001287 doc_get_bch_hw_ecc(docg3, hwecc);
1288 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_BCH_SZ, hwecc);
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001289 doc_delay(docg3, 2);
1290
1291 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_UNUSED_SZ, oob);
1292 }
1293 if (oob && !autoecc)
1294 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_SIZE, oob);
1295
1296 doc_delay(docg3, 2);
1297 doc_page_finish(docg3);
1298 doc_delay(docg3, 2);
1299 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE2);
1300 doc_delay(docg3, 2);
1301
1302 /*
1303 * The wait status will perform another doc_page_finish() call, but that
1304 * seems to please the docg3, so leave it.
1305 */
1306 ret = doc_write_erase_wait_status(docg3);
1307 return ret;
1308err:
1309 doc_read_page_finish(docg3);
1310 return ret;
1311}
1312
1313/**
1314 * doc_guess_autoecc - Guess autoecc mode from mbd_oob_ops
1315 * @ops: the oob operations
1316 *
1317 * Returns 0 or 1 if success, -EINVAL if invalid oob mode
1318 */
1319static int doc_guess_autoecc(struct mtd_oob_ops *ops)
1320{
1321 int autoecc;
1322
1323 switch (ops->mode) {
1324 case MTD_OPS_PLACE_OOB:
1325 case MTD_OPS_AUTO_OOB:
1326 autoecc = 1;
1327 break;
1328 case MTD_OPS_RAW:
1329 autoecc = 0;
1330 break;
1331 default:
1332 autoecc = -EINVAL;
1333 }
1334 return autoecc;
1335}
1336
1337/**
1338 * doc_fill_autooob - Fill a 16 bytes OOB from 8 non-ECC bytes
1339 * @dst: the target 16 bytes OOB buffer
1340 * @oobsrc: the source 8 bytes non-ECC OOB buffer
1341 *
1342 */
1343static void doc_fill_autooob(u8 *dst, u8 *oobsrc)
1344{
1345 memcpy(dst, oobsrc, DOC_LAYOUT_OOB_PAGEINFO_SZ);
1346 dst[DOC_LAYOUT_OOB_UNUSED_OFS] = oobsrc[DOC_LAYOUT_OOB_PAGEINFO_SZ];
1347}
1348
1349/**
1350 * doc_backup_oob - Backup OOB into docg3 structure
1351 * @docg3: the device
1352 * @to: the page offset in the chip
1353 * @ops: the OOB size and buffer
1354 *
1355 * As the docg3 should write a page with its OOB in one pass, and some userland
1356 * applications do write_oob() to setup the OOB and then write(), store the OOB
1357 * into a temporary storage. This is very dangerous, as 2 concurrent
1358 * applications could store an OOB, and then write their pages (which will
1359 * result into one having its OOB corrupted).
1360 *
1361 * The only reliable way would be for userland to call doc_write_oob() with both
1362 * the page data _and_ the OOB area.
1363 *
1364 * Returns 0 if success, -EINVAL if ops content invalid
1365 */
1366static int doc_backup_oob(struct docg3 *docg3, loff_t to,
1367 struct mtd_oob_ops *ops)
1368{
1369 int ooblen = ops->ooblen, autoecc;
1370
1371 if (ooblen != DOC_LAYOUT_OOB_SIZE)
1372 return -EINVAL;
1373 autoecc = doc_guess_autoecc(ops);
1374 if (autoecc < 0)
1375 return autoecc;
1376
1377 docg3->oob_write_ofs = to;
1378 docg3->oob_autoecc = autoecc;
1379 if (ops->mode == MTD_OPS_AUTO_OOB) {
1380 doc_fill_autooob(docg3->oob_write_buf, ops->oobbuf);
1381 ops->oobretlen = 8;
1382 } else {
1383 memcpy(docg3->oob_write_buf, ops->oobbuf, DOC_LAYOUT_OOB_SIZE);
1384 ops->oobretlen = DOC_LAYOUT_OOB_SIZE;
1385 }
1386 return 0;
1387}
1388
1389/**
1390 * doc_write_oob - Write out of band bytes to flash
1391 * @mtd: the device
1392 * @ofs: the offset from first block and first page, in bytes, aligned on page
1393 * size
1394 * @ops: the mtd oob structure
1395 *
1396 * Either write OOB data into a temporary buffer, for the subsequent write
1397 * page. The provided OOB should be 16 bytes long. If a data buffer is provided
1398 * as well, issue the page write.
1399 * Or provide data without OOB, and then a all zeroed OOB will be used (ECC will
1400 * still be filled in if asked for).
1401 *
1402 * Returns 0 is successfull, EINVAL if length is not 14 bytes
1403 */
1404static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
1405 struct mtd_oob_ops *ops)
1406{
1407 struct docg3 *docg3 = mtd->priv;
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001408 int ret, autoecc, oobdelta;
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001409 u8 *oobbuf = ops->oobbuf;
1410 u8 *buf = ops->datbuf;
1411 size_t len, ooblen;
1412 u8 oob[DOC_LAYOUT_OOB_SIZE];
1413
1414 if (buf)
1415 len = ops->len;
1416 else
1417 len = 0;
1418 if (oobbuf)
1419 ooblen = ops->ooblen;
1420 else
1421 ooblen = 0;
1422
1423 if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
1424 oobbuf += ops->ooboffs;
1425
1426 doc_dbg("doc_write_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
1427 ofs, ops->mode, buf, len, oobbuf, ooblen);
1428 switch (ops->mode) {
1429 case MTD_OPS_PLACE_OOB:
1430 case MTD_OPS_RAW:
1431 oobdelta = mtd->oobsize;
1432 break;
1433 case MTD_OPS_AUTO_OOB:
1434 oobdelta = mtd->ecclayout->oobavail;
1435 break;
1436 default:
1437 oobdelta = 0;
1438 }
1439 if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % oobdelta) ||
1440 (ofs % DOC_LAYOUT_PAGE_SIZE))
1441 return -EINVAL;
1442 if (len && ooblen &&
1443 (len / DOC_LAYOUT_PAGE_SIZE) != (ooblen / oobdelta))
1444 return -EINVAL;
Robert Jarzmika7baef12012-03-22 21:00:50 +01001445 if (ofs + len > mtd->size)
1446 return -EINVAL;
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001447
1448 ops->oobretlen = 0;
1449 ops->retlen = 0;
1450 ret = 0;
1451 if (len == 0 && ooblen == 0)
1452 return -EINVAL;
1453 if (len == 0 && ooblen > 0)
1454 return doc_backup_oob(docg3, ofs, ops);
1455
1456 autoecc = doc_guess_autoecc(ops);
1457 if (autoecc < 0)
1458 return autoecc;
1459
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001460 mutex_lock(&docg3->cascade->lock);
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001461 while (!ret && len > 0) {
1462 memset(oob, 0, sizeof(oob));
1463 if (ofs == docg3->oob_write_ofs)
1464 memcpy(oob, docg3->oob_write_buf, DOC_LAYOUT_OOB_SIZE);
1465 else if (ooblen > 0 && ops->mode == MTD_OPS_AUTO_OOB)
1466 doc_fill_autooob(oob, oobbuf);
1467 else if (ooblen > 0)
1468 memcpy(oob, oobbuf, DOC_LAYOUT_OOB_SIZE);
1469 ret = doc_write_page(docg3, ofs, buf, oob, autoecc);
1470
1471 ofs += DOC_LAYOUT_PAGE_SIZE;
1472 len -= DOC_LAYOUT_PAGE_SIZE;
1473 buf += DOC_LAYOUT_PAGE_SIZE;
1474 if (ooblen) {
1475 oobbuf += oobdelta;
1476 ooblen -= oobdelta;
1477 ops->oobretlen += oobdelta;
1478 }
1479 ops->retlen += DOC_LAYOUT_PAGE_SIZE;
1480 }
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001481
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001482 doc_set_device_id(docg3, 0);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001483 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001484 return ret;
1485}
1486
1487/**
1488 * doc_write - Write a buffer to the chip
1489 * @mtd: the device
1490 * @to: the offset from first block and first page, in bytes, aligned on page
1491 * size
1492 * @len: the number of bytes to write (must be a full page size, ie. 512)
1493 * @retlen: the number of bytes actually written (0 or 512)
1494 * @buf: the buffer to get bytes from
1495 *
1496 * Writes data to the chip.
1497 *
1498 * Returns 0 if write successful, -EIO if write error
1499 */
1500static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
1501 size_t *retlen, const u_char *buf)
1502{
1503 struct docg3 *docg3 = mtd->priv;
1504 int ret;
1505 struct mtd_oob_ops ops;
1506
1507 doc_dbg("doc_write(to=%lld, len=%zu)\n", to, len);
1508 ops.datbuf = (char *)buf;
1509 ops.len = len;
1510 ops.mode = MTD_OPS_PLACE_OOB;
1511 ops.oobbuf = NULL;
1512 ops.ooblen = 0;
1513 ops.ooboffs = 0;
1514
1515 ret = doc_write_oob(mtd, to, &ops);
1516 *retlen = ops.retlen;
1517 return ret;
1518}
1519
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001520static struct docg3 *sysfs_dev2docg3(struct device *dev,
1521 struct device_attribute *attr)
1522{
1523 int floor;
1524 struct platform_device *pdev = to_platform_device(dev);
1525 struct mtd_info **docg3_floors = platform_get_drvdata(pdev);
1526
1527 floor = attr->attr.name[1] - '0';
1528 if (floor < 0 || floor >= DOC_MAX_NBFLOORS)
1529 return NULL;
1530 else
1531 return docg3_floors[floor]->priv;
1532}
1533
1534static ssize_t dps0_is_key_locked(struct device *dev,
1535 struct device_attribute *attr, char *buf)
1536{
1537 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1538 int dps0;
1539
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001540 mutex_lock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001541 doc_set_device_id(docg3, docg3->device_id);
1542 dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
1543 doc_set_device_id(docg3, 0);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001544 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001545
1546 return sprintf(buf, "%d\n", !(dps0 & DOC_DPS_KEY_OK));
1547}
1548
1549static ssize_t dps1_is_key_locked(struct device *dev,
1550 struct device_attribute *attr, char *buf)
1551{
1552 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1553 int dps1;
1554
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001555 mutex_lock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001556 doc_set_device_id(docg3, docg3->device_id);
1557 dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
1558 doc_set_device_id(docg3, 0);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001559 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001560
1561 return sprintf(buf, "%d\n", !(dps1 & DOC_DPS_KEY_OK));
1562}
1563
1564static ssize_t dps0_insert_key(struct device *dev,
1565 struct device_attribute *attr,
1566 const char *buf, size_t count)
1567{
1568 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1569 int i;
1570
1571 if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
1572 return -EINVAL;
1573
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001574 mutex_lock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001575 doc_set_device_id(docg3, docg3->device_id);
1576 for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
1577 doc_writeb(docg3, buf[i], DOC_DPS0_KEY);
1578 doc_set_device_id(docg3, 0);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001579 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001580 return count;
1581}
1582
1583static ssize_t dps1_insert_key(struct device *dev,
1584 struct device_attribute *attr,
1585 const char *buf, size_t count)
1586{
1587 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1588 int i;
1589
1590 if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
1591 return -EINVAL;
1592
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001593 mutex_lock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001594 doc_set_device_id(docg3, docg3->device_id);
1595 for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
1596 doc_writeb(docg3, buf[i], DOC_DPS1_KEY);
1597 doc_set_device_id(docg3, 0);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001598 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001599 return count;
1600}
1601
1602#define FLOOR_SYSFS(id) { \
1603 __ATTR(f##id##_dps0_is_keylocked, S_IRUGO, dps0_is_key_locked, NULL), \
1604 __ATTR(f##id##_dps1_is_keylocked, S_IRUGO, dps1_is_key_locked, NULL), \
1605 __ATTR(f##id##_dps0_protection_key, S_IWUGO, NULL, dps0_insert_key), \
1606 __ATTR(f##id##_dps1_protection_key, S_IWUGO, NULL, dps1_insert_key), \
1607}
1608
1609static struct device_attribute doc_sys_attrs[DOC_MAX_NBFLOORS][4] = {
1610 FLOOR_SYSFS(0), FLOOR_SYSFS(1), FLOOR_SYSFS(2), FLOOR_SYSFS(3)
1611};
1612
1613static int doc_register_sysfs(struct platform_device *pdev,
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001614 struct docg3_cascade *cascade)
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001615{
1616 int ret = 0, floor, i = 0;
1617 struct device *dev = &pdev->dev;
1618
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001619 for (floor = 0; !ret && floor < DOC_MAX_NBFLOORS &&
1620 cascade->floors[floor]; floor++)
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001621 for (i = 0; !ret && i < 4; i++)
1622 ret = device_create_file(dev, &doc_sys_attrs[floor][i]);
1623 if (!ret)
1624 return 0;
1625 do {
1626 while (--i >= 0)
1627 device_remove_file(dev, &doc_sys_attrs[floor][i]);
1628 i = 4;
1629 } while (--floor >= 0);
1630 return ret;
1631}
1632
1633static void doc_unregister_sysfs(struct platform_device *pdev,
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001634 struct docg3_cascade *cascade)
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001635{
1636 struct device *dev = &pdev->dev;
1637 int floor, i;
1638
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001639 for (floor = 0; floor < DOC_MAX_NBFLOORS && cascade->floors[floor];
Robert Jarzmik0f769d32011-11-19 16:02:58 +01001640 floor++)
1641 for (i = 0; i < 4; i++)
1642 device_remove_file(dev, &doc_sys_attrs[floor][i]);
1643}
1644
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001645/*
1646 * Debug sysfs entries
1647 */
1648static int dbg_flashctrl_show(struct seq_file *s, void *p)
1649{
1650 struct docg3 *docg3 = (struct docg3 *)s->private;
1651
1652 int pos = 0;
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001653 u8 fctrl;
1654
1655 mutex_lock(&docg3->cascade->lock);
1656 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
1657 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001658
1659 pos += seq_printf(s,
1660 "FlashControl : 0x%02x (%s,CE# %s,%s,%s,flash %s)\n",
1661 fctrl,
1662 fctrl & DOC_CTRL_VIOLATION ? "protocol violation" : "-",
1663 fctrl & DOC_CTRL_CE ? "active" : "inactive",
1664 fctrl & DOC_CTRL_PROTECTION_ERROR ? "protection error" : "-",
1665 fctrl & DOC_CTRL_SEQUENCE_ERROR ? "sequence error" : "-",
1666 fctrl & DOC_CTRL_FLASHREADY ? "ready" : "not ready");
1667 return pos;
1668}
1669DEBUGFS_RO_ATTR(flashcontrol, dbg_flashctrl_show);
1670
1671static int dbg_asicmode_show(struct seq_file *s, void *p)
1672{
1673 struct docg3 *docg3 = (struct docg3 *)s->private;
1674
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001675 int pos = 0, pctrl, mode;
1676
1677 mutex_lock(&docg3->cascade->lock);
1678 pctrl = doc_register_readb(docg3, DOC_ASICMODE);
1679 mode = pctrl & 0x03;
1680 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001681
1682 pos += seq_printf(s,
1683 "%04x : RAM_WE=%d,RSTIN_RESET=%d,BDETCT_RESET=%d,WRITE_ENABLE=%d,POWERDOWN=%d,MODE=%d%d (",
1684 pctrl,
1685 pctrl & DOC_ASICMODE_RAM_WE ? 1 : 0,
1686 pctrl & DOC_ASICMODE_RSTIN_RESET ? 1 : 0,
1687 pctrl & DOC_ASICMODE_BDETCT_RESET ? 1 : 0,
1688 pctrl & DOC_ASICMODE_MDWREN ? 1 : 0,
1689 pctrl & DOC_ASICMODE_POWERDOWN ? 1 : 0,
1690 mode >> 1, mode & 0x1);
1691
1692 switch (mode) {
1693 case DOC_ASICMODE_RESET:
1694 pos += seq_printf(s, "reset");
1695 break;
1696 case DOC_ASICMODE_NORMAL:
1697 pos += seq_printf(s, "normal");
1698 break;
1699 case DOC_ASICMODE_POWERDOWN:
1700 pos += seq_printf(s, "powerdown");
1701 break;
1702 }
1703 pos += seq_printf(s, ")\n");
1704 return pos;
1705}
1706DEBUGFS_RO_ATTR(asic_mode, dbg_asicmode_show);
1707
1708static int dbg_device_id_show(struct seq_file *s, void *p)
1709{
1710 struct docg3 *docg3 = (struct docg3 *)s->private;
1711 int pos = 0;
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001712 int id;
1713
1714 mutex_lock(&docg3->cascade->lock);
1715 id = doc_register_readb(docg3, DOC_DEVICESELECT);
1716 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001717
1718 pos += seq_printf(s, "DeviceId = %d\n", id);
1719 return pos;
1720}
1721DEBUGFS_RO_ATTR(device_id, dbg_device_id_show);
1722
1723static int dbg_protection_show(struct seq_file *s, void *p)
1724{
1725 struct docg3 *docg3 = (struct docg3 *)s->private;
1726 int pos = 0;
Robert Jarzmikdbc26d92011-11-19 16:02:45 +01001727 int protect, dps0, dps0_low, dps0_high, dps1, dps1_low, dps1_high;
1728
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001729 mutex_lock(&docg3->cascade->lock);
Robert Jarzmikdbc26d92011-11-19 16:02:45 +01001730 protect = doc_register_readb(docg3, DOC_PROTECTION);
1731 dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
1732 dps0_low = doc_register_readw(docg3, DOC_DPS0_ADDRLOW);
1733 dps0_high = doc_register_readw(docg3, DOC_DPS0_ADDRHIGH);
1734 dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
1735 dps1_low = doc_register_readw(docg3, DOC_DPS1_ADDRLOW);
1736 dps1_high = doc_register_readw(docg3, DOC_DPS1_ADDRHIGH);
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01001737 mutex_unlock(&docg3->cascade->lock);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001738
1739 pos += seq_printf(s, "Protection = 0x%02x (",
1740 protect);
1741 if (protect & DOC_PROTECT_FOUNDRY_OTP_LOCK)
1742 pos += seq_printf(s, "FOUNDRY_OTP_LOCK,");
1743 if (protect & DOC_PROTECT_CUSTOMER_OTP_LOCK)
1744 pos += seq_printf(s, "CUSTOMER_OTP_LOCK,");
1745 if (protect & DOC_PROTECT_LOCK_INPUT)
1746 pos += seq_printf(s, "LOCK_INPUT,");
1747 if (protect & DOC_PROTECT_STICKY_LOCK)
1748 pos += seq_printf(s, "STICKY_LOCK,");
1749 if (protect & DOC_PROTECT_PROTECTION_ENABLED)
1750 pos += seq_printf(s, "PROTECTION ON,");
1751 if (protect & DOC_PROTECT_IPL_DOWNLOAD_LOCK)
1752 pos += seq_printf(s, "IPL_DOWNLOAD_LOCK,");
1753 if (protect & DOC_PROTECT_PROTECTION_ERROR)
1754 pos += seq_printf(s, "PROTECT_ERR,");
1755 else
1756 pos += seq_printf(s, "NO_PROTECT_ERR");
1757 pos += seq_printf(s, ")\n");
1758
1759 pos += seq_printf(s, "DPS0 = 0x%02x : "
1760 "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
1761 "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1762 dps0, dps0_low, dps0_high,
1763 !!(dps0 & DOC_DPS_OTP_PROTECTED),
1764 !!(dps0 & DOC_DPS_READ_PROTECTED),
1765 !!(dps0 & DOC_DPS_WRITE_PROTECTED),
1766 !!(dps0 & DOC_DPS_HW_LOCK_ENABLED),
1767 !!(dps0 & DOC_DPS_KEY_OK));
1768 pos += seq_printf(s, "DPS1 = 0x%02x : "
1769 "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
1770 "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1771 dps1, dps1_low, dps1_high,
1772 !!(dps1 & DOC_DPS_OTP_PROTECTED),
1773 !!(dps1 & DOC_DPS_READ_PROTECTED),
1774 !!(dps1 & DOC_DPS_WRITE_PROTECTED),
1775 !!(dps1 & DOC_DPS_HW_LOCK_ENABLED),
1776 !!(dps1 & DOC_DPS_KEY_OK));
1777 return pos;
1778}
1779DEBUGFS_RO_ATTR(protection, dbg_protection_show);
1780
1781static int __init doc_dbg_register(struct docg3 *docg3)
1782{
1783 struct dentry *root, *entry;
1784
1785 root = debugfs_create_dir("docg3", NULL);
1786 if (!root)
1787 return -ENOMEM;
1788
1789 entry = debugfs_create_file("flashcontrol", S_IRUSR, root, docg3,
1790 &flashcontrol_fops);
1791 if (entry)
1792 entry = debugfs_create_file("asic_mode", S_IRUSR, root,
1793 docg3, &asic_mode_fops);
1794 if (entry)
1795 entry = debugfs_create_file("device_id", S_IRUSR, root,
1796 docg3, &device_id_fops);
1797 if (entry)
1798 entry = debugfs_create_file("protection", S_IRUSR, root,
1799 docg3, &protection_fops);
1800 if (entry) {
1801 docg3->debugfs_root = root;
1802 return 0;
1803 } else {
1804 debugfs_remove_recursive(root);
1805 return -ENOMEM;
1806 }
1807}
1808
1809static void __exit doc_dbg_unregister(struct docg3 *docg3)
1810{
1811 debugfs_remove_recursive(docg3->debugfs_root);
1812}
1813
1814/**
1815 * doc_set_driver_info - Fill the mtd_info structure and docg3 structure
1816 * @chip_id: The chip ID of the supported chip
1817 * @mtd: The structure to fill
1818 */
1819static void __init doc_set_driver_info(int chip_id, struct mtd_info *mtd)
1820{
1821 struct docg3 *docg3 = mtd->priv;
1822 int cfg;
1823
1824 cfg = doc_register_readb(docg3, DOC_CONFIGURATION);
1825 docg3->if_cfg = (cfg & DOC_CONF_IF_CFG ? 1 : 0);
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001826 docg3->reliable = reliable_mode;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001827
1828 switch (chip_id) {
1829 case DOC_CHIPID_G3:
Robert Jarzmik31716a52012-03-03 21:57:33 +01001830 mtd->name = kasprintf(GFP_KERNEL, "docg3.%d",
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001831 docg3->device_id);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001832 docg3->max_block = 2047;
1833 break;
1834 }
1835 mtd->type = MTD_NANDFLASH;
Robert Jarzmik7a7fcf12011-11-19 16:02:54 +01001836 mtd->flags = MTD_CAP_NANDFLASH;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001837 mtd->size = (docg3->max_block + 1) * DOC_LAYOUT_BLOCK_SIZE;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001838 if (docg3->reliable == 2)
1839 mtd->size /= 2;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001840 mtd->erasesize = DOC_LAYOUT_BLOCK_SIZE * DOC_LAYOUT_NBPLANES;
Robert Jarzmikc3de8a82011-11-19 16:02:57 +01001841 if (docg3->reliable == 2)
1842 mtd->erasesize /= 2;
Artem Bityutskiy82c4c582012-02-03 09:44:32 +02001843 mtd->writebufsize = mtd->writesize = DOC_LAYOUT_PAGE_SIZE;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001844 mtd->oobsize = DOC_LAYOUT_OOB_SIZE;
1845 mtd->owner = THIS_MODULE;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02001846 mtd->_erase = doc_erase;
1847 mtd->_read = doc_read;
1848 mtd->_write = doc_write;
1849 mtd->_read_oob = doc_read_oob;
1850 mtd->_write_oob = doc_write_oob;
1851 mtd->_block_isbad = doc_block_isbad;
Robert Jarzmik732b63b2011-11-19 16:02:49 +01001852 mtd->ecclayout = &docg3_oobinfo;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001853 mtd->ecc_strength = DOC_ECC_BCH_T;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001854}
1855
1856/**
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001857 * doc_probe_device - Check if a device is available
1858 * @base: the io space where the device is probed
1859 * @floor: the floor of the probed device
1860 * @dev: the device
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001861 * @cascade: the cascade of chips this devices will belong to
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001862 *
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001863 * Checks whether a device at the specified IO range, and floor is available.
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001864 *
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001865 * Returns a mtd_info struct if there is a device, ENODEV if none found, ENOMEM
1866 * if a memory allocation failed. If floor 0 is checked, a reset of the ASIC is
1867 * launched.
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001868 */
Ryosuke Saito30053b82012-03-15 19:19:03 +09001869static struct mtd_info * __init
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001870doc_probe_device(struct docg3_cascade *cascade, int floor, struct device *dev)
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001871{
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001872 int ret, bbt_nbpages;
1873 u16 chip_id, chip_id_inv;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001874 struct docg3 *docg3;
1875 struct mtd_info *mtd;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001876
1877 ret = -ENOMEM;
1878 docg3 = kzalloc(sizeof(struct docg3), GFP_KERNEL);
1879 if (!docg3)
1880 goto nomem1;
1881 mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
1882 if (!mtd)
1883 goto nomem2;
1884 mtd->priv = docg3;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001885 bbt_nbpages = DIV_ROUND_UP(docg3->max_block + 1,
1886 8 * DOC_LAYOUT_PAGE_SIZE);
1887 docg3->bbt = kzalloc(bbt_nbpages * DOC_LAYOUT_PAGE_SIZE, GFP_KERNEL);
1888 if (!docg3->bbt)
1889 goto nomem3;
1890
1891 docg3->dev = dev;
1892 docg3->device_id = floor;
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001893 docg3->cascade = cascade;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001894 doc_set_device_id(docg3, docg3->device_id);
1895 if (!floor)
1896 doc_set_asic_mode(docg3, DOC_ASICMODE_RESET);
1897 doc_set_asic_mode(docg3, DOC_ASICMODE_NORMAL);
1898
1899 chip_id = doc_register_readw(docg3, DOC_CHIPID);
1900 chip_id_inv = doc_register_readw(docg3, DOC_CHIPID_INV);
1901
1902 ret = 0;
1903 if (chip_id != (u16)(~chip_id_inv)) {
1904 goto nomem3;
1905 }
1906
1907 switch (chip_id) {
1908 case DOC_CHIPID_G3:
1909 doc_info("Found a G3 DiskOnChip at addr %p, floor %d\n",
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001910 docg3->cascade->base, floor);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001911 break;
1912 default:
1913 doc_err("Chip id %04x is not a DiskOnChip G3 chip\n", chip_id);
1914 goto nomem3;
1915 }
1916
1917 doc_set_driver_info(chip_id, mtd);
1918
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001919 doc_hamming_ecc_init(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001920 doc_reload_bbt(docg3);
1921 return mtd;
1922
1923nomem3:
1924 kfree(mtd);
1925nomem2:
1926 kfree(docg3);
1927nomem1:
1928 return ERR_PTR(ret);
1929}
1930
1931/**
1932 * doc_release_device - Release a docg3 floor
1933 * @mtd: the device
1934 */
1935static void doc_release_device(struct mtd_info *mtd)
1936{
1937 struct docg3 *docg3 = mtd->priv;
1938
1939 mtd_device_unregister(mtd);
1940 kfree(docg3->bbt);
1941 kfree(docg3);
1942 kfree(mtd->name);
1943 kfree(mtd);
1944}
1945
1946/**
Robert Jarzmike4b2a962011-11-19 16:02:56 +01001947 * docg3_resume - Awakens docg3 floor
1948 * @pdev: platfrom device
1949 *
1950 * Returns 0 (always successfull)
1951 */
1952static int docg3_resume(struct platform_device *pdev)
1953{
1954 int i;
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001955 struct docg3_cascade *cascade;
Robert Jarzmike4b2a962011-11-19 16:02:56 +01001956 struct mtd_info **docg3_floors, *mtd;
1957 struct docg3 *docg3;
1958
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001959 cascade = platform_get_drvdata(pdev);
1960 docg3_floors = cascade->floors;
Robert Jarzmike4b2a962011-11-19 16:02:56 +01001961 mtd = docg3_floors[0];
1962 docg3 = mtd->priv;
1963
1964 doc_dbg("docg3_resume()\n");
1965 for (i = 0; i < 12; i++)
1966 doc_readb(docg3, DOC_IOSPACE_IPL);
1967 return 0;
1968}
1969
1970/**
1971 * docg3_suspend - Put in low power mode the docg3 floor
1972 * @pdev: platform device
1973 * @state: power state
1974 *
1975 * Shuts off most of docg3 circuitery to lower power consumption.
1976 *
1977 * Returns 0 if suspend succeeded, -EIO if chip refused suspend
1978 */
1979static int docg3_suspend(struct platform_device *pdev, pm_message_t state)
1980{
1981 int floor, i;
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001982 struct docg3_cascade *cascade;
Robert Jarzmike4b2a962011-11-19 16:02:56 +01001983 struct mtd_info **docg3_floors, *mtd;
1984 struct docg3 *docg3;
1985 u8 ctrl, pwr_down;
1986
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01001987 cascade = platform_get_drvdata(pdev);
1988 docg3_floors = cascade->floors;
Robert Jarzmike4b2a962011-11-19 16:02:56 +01001989 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
1990 mtd = docg3_floors[floor];
1991 if (!mtd)
1992 continue;
1993 docg3 = mtd->priv;
1994
1995 doc_writeb(docg3, floor, DOC_DEVICESELECT);
1996 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
1997 ctrl &= ~DOC_CTRL_VIOLATION & ~DOC_CTRL_CE;
1998 doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
1999
2000 for (i = 0; i < 10; i++) {
2001 usleep_range(3000, 4000);
2002 pwr_down = doc_register_readb(docg3, DOC_POWERMODE);
2003 if (pwr_down & DOC_POWERDOWN_READY)
2004 break;
2005 }
2006 if (pwr_down & DOC_POWERDOWN_READY) {
2007 doc_dbg("docg3_suspend(): floor %d powerdown ok\n",
2008 floor);
2009 } else {
2010 doc_err("docg3_suspend(): floor %d powerdown failed\n",
2011 floor);
2012 return -EIO;
2013 }
2014 }
2015
2016 mtd = docg3_floors[0];
2017 docg3 = mtd->priv;
2018 doc_set_asic_mode(docg3, DOC_ASICMODE_POWERDOWN);
2019 return 0;
2020}
2021
2022/**
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002023 * doc_probe - Probe the IO space for a DiskOnChip G3 chip
2024 * @pdev: platform device
2025 *
2026 * Probes for a G3 chip at the specified IO space in the platform data
2027 * ressources. The floor 0 must be available.
2028 *
2029 * Returns 0 on success, -ENOMEM, -ENXIO on error
2030 */
2031static int __init docg3_probe(struct platform_device *pdev)
2032{
2033 struct device *dev = &pdev->dev;
2034 struct mtd_info *mtd;
2035 struct resource *ress;
2036 void __iomem *base;
2037 int ret, floor, found = 0;
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002038 struct docg3_cascade *cascade;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002039
2040 ret = -ENXIO;
2041 ress = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2042 if (!ress) {
2043 dev_err(dev, "No I/O memory resource defined\n");
2044 goto noress;
2045 }
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002046 base = ioremap(ress->start, DOC_IOSPACE_SIZE);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002047
2048 ret = -ENOMEM;
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002049 cascade = kzalloc(sizeof(*cascade) * DOC_MAX_NBFLOORS,
2050 GFP_KERNEL);
2051 if (!cascade)
Robert Jarzmikd13d19e2011-11-19 16:02:55 +01002052 goto nomem1;
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002053 cascade->base = base;
Robert Jarzmik7b0e67f2012-03-22 21:00:53 +01002054 mutex_init(&cascade->lock);
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002055 cascade->bch = init_bch(DOC_ECC_BCH_M, DOC_ECC_BCH_T,
Robert Jarzmikd13d19e2011-11-19 16:02:55 +01002056 DOC_ECC_BCH_PRIMPOLY);
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002057 if (!cascade->bch)
Robert Jarzmikd13d19e2011-11-19 16:02:55 +01002058 goto nomem2;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002059
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002060 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002061 mtd = doc_probe_device(cascade, floor, dev);
Dan Carpenterb49e3452011-11-28 16:53:13 +03002062 if (IS_ERR(mtd)) {
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002063 ret = PTR_ERR(mtd);
Dan Carpenterb49e3452011-11-28 16:53:13 +03002064 goto err_probe;
2065 }
2066 if (!mtd) {
2067 if (floor == 0)
2068 goto notfound;
2069 else
2070 continue;
2071 }
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002072 cascade->floors[floor] = mtd;
Dan Carpenterb49e3452011-11-28 16:53:13 +03002073 ret = mtd_device_parse_register(mtd, part_probes, NULL, NULL,
2074 0);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002075 if (ret)
2076 goto err_probe;
Dan Carpenterb49e3452011-11-28 16:53:13 +03002077 found++;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002078 }
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002079
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002080 ret = doc_register_sysfs(pdev, cascade);
Robert Jarzmik0f769d32011-11-19 16:02:58 +01002081 if (ret)
2082 goto err_probe;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002083 if (!found)
2084 goto notfound;
2085
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002086 platform_set_drvdata(pdev, cascade);
2087 doc_dbg_register(cascade->floors[0]->priv);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002088 return 0;
2089
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002090notfound:
2091 ret = -ENODEV;
2092 dev_info(dev, "No supported DiskOnChip found\n");
2093err_probe:
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002094 kfree(cascade->bch);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002095 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002096 if (cascade->floors[floor])
2097 doc_release_device(cascade->floors[floor]);
Robert Jarzmikd13d19e2011-11-19 16:02:55 +01002098nomem2:
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002099 kfree(cascade);
Robert Jarzmikd13d19e2011-11-19 16:02:55 +01002100nomem1:
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002101 iounmap(base);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002102noress:
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002103 return ret;
2104}
2105
2106/**
2107 * docg3_release - Release the driver
2108 * @pdev: the platform device
2109 *
2110 * Returns 0
2111 */
2112static int __exit docg3_release(struct platform_device *pdev)
2113{
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002114 struct docg3_cascade *cascade = platform_get_drvdata(pdev);
2115 struct docg3 *docg3 = cascade->floors[0]->priv;
2116 void __iomem *base = cascade->base;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002117 int floor;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002118
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002119 doc_unregister_sysfs(pdev, cascade);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002120 doc_dbg_unregister(docg3);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002121 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002122 if (cascade->floors[floor])
2123 doc_release_device(cascade->floors[floor]);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002124
Robert Jarzmik1b15a5f2012-03-22 21:00:52 +01002125 free_bch(docg3->cascade->bch);
2126 kfree(cascade);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01002127 iounmap(base);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02002128 return 0;
2129}
2130
2131static struct platform_driver g3_driver = {
2132 .driver = {
2133 .name = "docg3",
2134 .owner = THIS_MODULE,
2135 },
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
2141static int __init docg3_init(void)
2142{
2143 return platform_driver_probe(&g3_driver, docg3_probe);
2144}
2145module_init(docg3_init);
2146
2147
2148static void __exit docg3_exit(void)
2149{
2150 platform_driver_unregister(&g3_driver);
2151}
2152module_exit(docg3_exit);
2153
2154MODULE_LICENSE("GPL");
2155MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
2156MODULE_DESCRIPTION("MTD driver for DiskOnChip G3");