mtd: docg3: add multiple floor support
[deliverable/linux.git] / drivers / mtd / devices / docg3.c
CommitLineData
efa2ca73
RJ
1/*
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>
32
33#include <linux/debugfs.h>
34#include <linux/seq_file.h>
35
36#define CREATE_TRACE_POINTS
37#include "docg3.h"
38
39/*
40 * This driver handles the DiskOnChip G3 flash memory.
41 *
42 * As no specification is available from M-Systems/Sandisk, this drivers lacks
43 * several functions available on the chip, as :
44 * - block erase
45 * - page write
46 * - IPL write
47 * - ECC fixing (lack of BCH algorith understanding)
48 * - powerdown / powerup
49 *
50 * The bus data width (8bits versus 16bits) is not handled (if_cfg flag), and
51 * the driver assumes a 16bits data bus.
52 *
53 * DocG3 relies on 2 ECC algorithms, which are handled in hardware :
54 * - a 1 byte Hamming code stored in the OOB for each page
55 * - a 7 bytes BCH code stored in the OOB for each page
56 * The BCH part is only used for check purpose, no correction is available as
57 * some information is missing. What is known is that :
58 * - BCH is in GF(2^14)
59 * - BCH is over data of 520 bytes (512 page + 7 page_info bytes
60 * + 1 hamming byte)
61 * - BCH can correct up to 4 bits (t = 4)
62 * - BCH syndroms are calculated in hardware, and checked in hardware as well
63 *
64 */
65
66static inline u8 doc_readb(struct docg3 *docg3, u16 reg)
67{
68 u8 val = readb(docg3->base + reg);
69
70 trace_docg3_io(0, 8, reg, (int)val);
71 return val;
72}
73
74static inline u16 doc_readw(struct docg3 *docg3, u16 reg)
75{
76 u16 val = readw(docg3->base + reg);
77
78 trace_docg3_io(0, 16, reg, (int)val);
79 return val;
80}
81
82static inline void doc_writeb(struct docg3 *docg3, u8 val, u16 reg)
83{
84 writeb(val, docg3->base + reg);
84a93058 85 trace_docg3_io(1, 8, reg, val);
efa2ca73
RJ
86}
87
88static inline void doc_writew(struct docg3 *docg3, u16 val, u16 reg)
89{
90 writew(val, docg3->base + reg);
91 trace_docg3_io(1, 16, reg, val);
92}
93
94static inline void doc_flash_command(struct docg3 *docg3, u8 cmd)
95{
96 doc_writeb(docg3, cmd, DOC_FLASHCOMMAND);
97}
98
99static inline void doc_flash_sequence(struct docg3 *docg3, u8 seq)
100{
101 doc_writeb(docg3, seq, DOC_FLASHSEQUENCE);
102}
103
104static inline void doc_flash_address(struct docg3 *docg3, u8 addr)
105{
106 doc_writeb(docg3, addr, DOC_FLASHADDRESS);
107}
108
109static char const *part_probes[] = { "cmdlinepart", "saftlpart", NULL };
110
111static int doc_register_readb(struct docg3 *docg3, int reg)
112{
113 u8 val;
114
115 doc_writew(docg3, reg, DOC_READADDRESS);
116 val = doc_readb(docg3, reg);
117 doc_vdbg("Read register %04x : %02x\n", reg, val);
118 return val;
119}
120
121static int doc_register_readw(struct docg3 *docg3, int reg)
122{
123 u16 val;
124
125 doc_writew(docg3, reg, DOC_READADDRESS);
126 val = doc_readw(docg3, reg);
127 doc_vdbg("Read register %04x : %04x\n", reg, val);
128 return val;
129}
130
131/**
132 * doc_delay - delay docg3 operations
133 * @docg3: the device
134 * @nbNOPs: the number of NOPs to issue
135 *
136 * As no specification is available, the right timings between chip commands are
137 * unknown. The only available piece of information are the observed nops on a
138 * working docg3 chip.
139 * Therefore, doc_delay relies on a busy loop of NOPs, instead of scheduler
140 * friendlier msleep() functions or blocking mdelay().
141 */
142static void doc_delay(struct docg3 *docg3, int nbNOPs)
143{
144 int i;
145
ac48e800 146 doc_vdbg("NOP x %d\n", nbNOPs);
efa2ca73
RJ
147 for (i = 0; i < nbNOPs; i++)
148 doc_writeb(docg3, 0, DOC_NOP);
149}
150
151static int is_prot_seq_error(struct docg3 *docg3)
152{
153 int ctrl;
154
155 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
156 return ctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR);
157}
158
159static int doc_is_ready(struct docg3 *docg3)
160{
161 int ctrl;
162
163 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
164 return ctrl & DOC_CTRL_FLASHREADY;
165}
166
167static int doc_wait_ready(struct docg3 *docg3)
168{
169 int maxWaitCycles = 100;
170
171 do {
172 doc_delay(docg3, 4);
173 cpu_relax();
174 } while (!doc_is_ready(docg3) && maxWaitCycles--);
175 doc_delay(docg3, 2);
176 if (maxWaitCycles > 0)
177 return 0;
178 else
179 return -EIO;
180}
181
182static int doc_reset_seq(struct docg3 *docg3)
183{
184 int ret;
185
186 doc_writeb(docg3, 0x10, DOC_FLASHCONTROL);
187 doc_flash_sequence(docg3, DOC_SEQ_RESET);
188 doc_flash_command(docg3, DOC_CMD_RESET);
189 doc_delay(docg3, 2);
190 ret = doc_wait_ready(docg3);
191
192 doc_dbg("doc_reset_seq() -> isReady=%s\n", ret ? "false" : "true");
193 return ret;
194}
195
196/**
197 * doc_read_data_area - Read data from data area
198 * @docg3: the device
32a50b3a
RJ
199 * @buf: the buffer to fill in (might be NULL is dummy reads)
200 * @len: the length to read
efa2ca73
RJ
201 * @first: first time read, DOC_READADDRESS should be set
202 *
203 * Reads bytes from flash data. Handles the single byte / even bytes reads.
204 */
205static void doc_read_data_area(struct docg3 *docg3, void *buf, int len,
206 int first)
207{
208 int i, cdr, len4;
209 u16 data16, *dst16;
210 u8 data8, *dst8;
211
212 doc_dbg("doc_read_data_area(buf=%p, len=%d)\n", buf, len);
213 cdr = len & 0x3;
214 len4 = len - cdr;
215
216 if (first)
217 doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
218 dst16 = buf;
219 for (i = 0; i < len4; i += 2) {
220 data16 = doc_readw(docg3, DOC_IOSPACE_DATA);
32a50b3a
RJ
221 if (dst16) {
222 *dst16 = data16;
223 dst16++;
224 }
efa2ca73
RJ
225 }
226
227 if (cdr) {
228 doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
229 DOC_READADDRESS);
230 doc_delay(docg3, 1);
231 dst8 = (u8 *)dst16;
232 for (i = 0; i < cdr; i++) {
233 data8 = doc_readb(docg3, DOC_IOSPACE_DATA);
32a50b3a
RJ
234 if (dst8) {
235 *dst8 = data8;
236 dst8++;
237 }
efa2ca73
RJ
238 }
239 }
240}
241
242/**
243 * doc_set_data_mode - Sets the flash to reliable data mode
244 * @docg3: the device
245 *
246 * The reliable data mode is a bit slower than the fast mode, but less errors
247 * occur. Entering the reliable mode cannot be done without entering the fast
248 * mode first.
249 */
250static void doc_set_reliable_mode(struct docg3 *docg3)
251{
252 doc_dbg("doc_set_reliable_mode()\n");
253 doc_flash_sequence(docg3, DOC_SEQ_SET_MODE);
254 doc_flash_command(docg3, DOC_CMD_FAST_MODE);
255 doc_flash_command(docg3, DOC_CMD_RELIABLE_MODE);
256 doc_delay(docg3, 2);
257}
258
259/**
260 * doc_set_asic_mode - Set the ASIC mode
261 * @docg3: the device
262 * @mode: the mode
263 *
264 * The ASIC can work in 3 modes :
265 * - RESET: all registers are zeroed
266 * - NORMAL: receives and handles commands
267 * - POWERDOWN: minimal poweruse, flash parts shut off
268 */
269static void doc_set_asic_mode(struct docg3 *docg3, u8 mode)
270{
271 int i;
272
273 for (i = 0; i < 12; i++)
274 doc_readb(docg3, DOC_IOSPACE_IPL);
275
276 mode |= DOC_ASICMODE_MDWREN;
277 doc_dbg("doc_set_asic_mode(%02x)\n", mode);
278 doc_writeb(docg3, mode, DOC_ASICMODE);
279 doc_writeb(docg3, ~mode, DOC_ASICMODECONFIRM);
280 doc_delay(docg3, 1);
281}
282
283/**
284 * doc_set_device_id - Sets the devices id for cascaded G3 chips
285 * @docg3: the device
286 * @id: the chip to select (amongst 0, 1, 2, 3)
287 *
288 * There can be 4 cascaded G3 chips. This function selects the one which will
289 * should be the active one.
290 */
291static void doc_set_device_id(struct docg3 *docg3, int id)
292{
293 u8 ctrl;
294
295 doc_dbg("doc_set_device_id(%d)\n", id);
296 doc_writeb(docg3, id, DOC_DEVICESELECT);
297 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
298
299 ctrl &= ~DOC_CTRL_VIOLATION;
300 ctrl |= DOC_CTRL_CE;
301 doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
302}
303
304/**
305 * doc_set_extra_page_mode - Change flash page layout
306 * @docg3: the device
307 *
308 * Normally, the flash page is split into the data (512 bytes) and the out of
309 * band data (16 bytes). For each, 4 more bytes can be accessed, where the wear
310 * leveling counters are stored. To access this last area of 4 bytes, a special
311 * mode must be input to the flash ASIC.
312 *
313 * Returns 0 if no error occured, -EIO else.
314 */
315static int doc_set_extra_page_mode(struct docg3 *docg3)
316{
317 int fctrl;
318
319 doc_dbg("doc_set_extra_page_mode()\n");
320 doc_flash_sequence(docg3, DOC_SEQ_PAGE_SIZE_532);
321 doc_flash_command(docg3, DOC_CMD_PAGE_SIZE_532);
322 doc_delay(docg3, 2);
323
324 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
325 if (fctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR))
326 return -EIO;
327 else
328 return 0;
329}
330
331/**
332 * doc_seek - Set both flash planes to the specified block, page for reading
333 * @docg3: the device
334 * @block0: the first plane block index
335 * @block1: the second plane block index
336 * @page: the page index within the block
337 * @wear: if true, read will occur on the 4 extra bytes of the wear area
338 * @ofs: offset in page to read
339 *
340 * Programs the flash even and odd planes to the specific block and page.
341 * Alternatively, programs the flash to the wear area of the specified page.
342 */
343static int doc_read_seek(struct docg3 *docg3, int block0, int block1, int page,
344 int wear, int ofs)
345{
346 int sector, ret = 0;
347
348 doc_dbg("doc_seek(blocks=(%d,%d), page=%d, ofs=%d, wear=%d)\n",
349 block0, block1, page, ofs, wear);
350
351 if (!wear && (ofs < 2 * DOC_LAYOUT_PAGE_SIZE)) {
352 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
353 doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
354 doc_delay(docg3, 2);
355 } else {
356 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
357 doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
358 doc_delay(docg3, 2);
359 }
360
361 doc_set_reliable_mode(docg3);
362 if (wear)
363 ret = doc_set_extra_page_mode(docg3);
364 if (ret)
365 goto out;
366
367 sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
368 doc_flash_sequence(docg3, DOC_SEQ_READ);
369 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
370 doc_delay(docg3, 1);
371 doc_flash_address(docg3, sector & 0xff);
372 doc_flash_address(docg3, (sector >> 8) & 0xff);
373 doc_flash_address(docg3, (sector >> 16) & 0xff);
374 doc_delay(docg3, 1);
375
376 sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
377 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
378 doc_delay(docg3, 1);
379 doc_flash_address(docg3, sector & 0xff);
380 doc_flash_address(docg3, (sector >> 8) & 0xff);
381 doc_flash_address(docg3, (sector >> 16) & 0xff);
382 doc_delay(docg3, 2);
383
384out:
385 return ret;
386}
387
388/**
389 * doc_read_page_ecc_init - Initialize hardware ECC engine
390 * @docg3: the device
391 * @len: the number of bytes covered by the ECC (BCH covered)
392 *
393 * The function does initialize the hardware ECC engine to compute the Hamming
394 * ECC (on 1 byte) and the BCH Syndroms (on 7 bytes).
395 *
396 * Return 0 if succeeded, -EIO on error
397 */
398static int doc_read_page_ecc_init(struct docg3 *docg3, int len)
399{
400 doc_writew(docg3, DOC_ECCCONF0_READ_MODE
401 | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
402 | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
403 DOC_ECCCONF0);
404 doc_delay(docg3, 4);
405 doc_register_readb(docg3, DOC_FLASHCONTROL);
406 return doc_wait_ready(docg3);
407}
408
409/**
410 * doc_read_page_prepare - Prepares reading data from a flash page
411 * @docg3: the device
412 * @block0: the first plane block index on flash memory
413 * @block1: the second plane block index on flash memory
414 * @page: the page index in the block
415 * @offset: the offset in the page (must be a multiple of 4)
416 *
417 * Prepares the page to be read in the flash memory :
418 * - tell ASIC to map the flash pages
419 * - tell ASIC to be in read mode
420 *
421 * After a call to this method, a call to doc_read_page_finish is mandatory,
422 * to end the read cycle of the flash.
423 *
424 * Read data from a flash page. The length to be read must be between 0 and
425 * (page_size + oob_size + wear_size), ie. 532, and a multiple of 4 (because
426 * the extra bytes reading is not implemented).
427 *
428 * As pages are grouped by 2 (in 2 planes), reading from a page must be done
429 * in two steps:
430 * - one read of 512 bytes at offset 0
431 * - one read of 512 bytes at offset 512 + 16
432 *
433 * Returns 0 if successful, -EIO if a read error occured.
434 */
435static int doc_read_page_prepare(struct docg3 *docg3, int block0, int block1,
436 int page, int offset)
437{
438 int wear_area = 0, ret = 0;
439
440 doc_dbg("doc_read_page_prepare(blocks=(%d,%d), page=%d, ofsInPage=%d)\n",
441 block0, block1, page, offset);
442 if (offset >= DOC_LAYOUT_WEAR_OFFSET)
443 wear_area = 1;
444 if (!wear_area && offset > (DOC_LAYOUT_PAGE_OOB_SIZE * 2))
445 return -EINVAL;
446
447 doc_set_device_id(docg3, docg3->device_id);
448 ret = doc_reset_seq(docg3);
449 if (ret)
450 goto err;
451
452 /* Program the flash address block and page */
453 ret = doc_read_seek(docg3, block0, block1, page, wear_area, offset);
454 if (ret)
455 goto err;
456
457 doc_flash_command(docg3, DOC_CMD_READ_ALL_PLANES);
458 doc_delay(docg3, 2);
459 doc_wait_ready(docg3);
460
461 doc_flash_command(docg3, DOC_CMD_SET_ADDR_READ);
462 doc_delay(docg3, 1);
463 if (offset >= DOC_LAYOUT_PAGE_SIZE * 2)
464 offset -= 2 * DOC_LAYOUT_PAGE_SIZE;
465 doc_flash_address(docg3, offset >> 2);
466 doc_delay(docg3, 1);
467 doc_wait_ready(docg3);
468
469 doc_flash_command(docg3, DOC_CMD_READ_FLASH);
470
471 return 0;
472err:
473 doc_writeb(docg3, 0, DOC_DATAEND);
474 doc_delay(docg3, 2);
475 return -EIO;
476}
477
478/**
479 * doc_read_page_getbytes - Reads bytes from a prepared page
480 * @docg3: the device
481 * @len: the number of bytes to be read (must be a multiple of 4)
482 * @buf: the buffer to be filled in
483 * @first: 1 if first time read, DOC_READADDRESS should be set
484 *
485 */
486static int doc_read_page_getbytes(struct docg3 *docg3, int len, u_char *buf,
487 int first)
488{
489 doc_read_data_area(docg3, buf, len, first);
490 doc_delay(docg3, 2);
491 return len;
492}
493
494/**
495 * doc_get_hw_bch_syndroms - Get hardware calculated BCH syndroms
496 * @docg3: the device
497 * @syns: the array of 7 integers where the syndroms will be stored
498 */
499static void doc_get_hw_bch_syndroms(struct docg3 *docg3, int *syns)
500{
501 int i;
502
503 for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
504 syns[i] = doc_register_readb(docg3, DOC_BCH_SYNDROM(i));
505}
506
507/**
508 * doc_read_page_finish - Ends reading of a flash page
509 * @docg3: the device
510 *
511 * As a side effect, resets the chip selector to 0. This ensures that after each
512 * read operation, the floor 0 is selected. Therefore, if the systems halts, the
513 * reboot will boot on floor 0, where the IPL is.
514 */
515static void doc_read_page_finish(struct docg3 *docg3)
516{
517 doc_writeb(docg3, 0, DOC_DATAEND);
518 doc_delay(docg3, 2);
519 doc_set_device_id(docg3, 0);
520}
521
522/**
523 * calc_block_sector - Calculate blocks, pages and ofs.
524
525 * @from: offset in flash
526 * @block0: first plane block index calculated
527 * @block1: second plane block index calculated
528 * @page: page calculated
529 * @ofs: offset in page
530 */
531static void calc_block_sector(loff_t from, int *block0, int *block1, int *page,
532 int *ofs)
533{
534 uint sector;
535
536 sector = from / DOC_LAYOUT_PAGE_SIZE;
537 *block0 = sector / (DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_NBPLANES)
538 * DOC_LAYOUT_NBPLANES;
539 *block1 = *block0 + 1;
540 *page = sector % (DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_NBPLANES);
541 *page /= DOC_LAYOUT_NBPLANES;
542 if (sector % 2)
543 *ofs = DOC_LAYOUT_PAGE_OOB_SIZE;
544 else
545 *ofs = 0;
546}
547
548/**
32a50b3a 549 * doc_read_oob - Read out of band bytes from flash
efa2ca73
RJ
550 * @mtd: the device
551 * @from: the offset from first block and first page, in bytes, aligned on page
552 * size
32a50b3a 553 * @ops: the mtd oob structure
efa2ca73 554 *
32a50b3a 555 * Reads flash memory OOB area of pages.
efa2ca73
RJ
556 *
557 * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
558 */
32a50b3a
RJ
559static int doc_read_oob(struct mtd_info *mtd, loff_t from,
560 struct mtd_oob_ops *ops)
efa2ca73
RJ
561{
562 struct docg3 *docg3 = mtd->priv;
32a50b3a
RJ
563 int block0, block1, page, ret, ofs = 0;
564 u8 *oobbuf = ops->oobbuf;
565 u8 *buf = ops->datbuf;
566 size_t len, ooblen, nbdata, nboob;
567 u8 calc_ecc[DOC_ECC_BCH_SIZE], eccconf1;
568
569 if (buf)
570 len = ops->len;
571 else
572 len = 0;
573 if (oobbuf)
574 ooblen = ops->ooblen;
575 else
576 ooblen = 0;
577
578 if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
579 oobbuf += ops->ooboffs;
580
581 doc_dbg("doc_read_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
582 from, ops->mode, buf, len, oobbuf, ooblen);
583 if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % DOC_LAYOUT_OOB_SIZE) ||
584 (from % DOC_LAYOUT_PAGE_SIZE))
585 return -EINVAL;
efa2ca73
RJ
586
587 ret = -EINVAL;
32a50b3a 588 calc_block_sector(from + len, &block0, &block1, &page, &ofs);
efa2ca73
RJ
589 if (block1 > docg3->max_block)
590 goto err;
591
32a50b3a
RJ
592 ops->oobretlen = 0;
593 ops->retlen = 0;
efa2ca73 594 ret = 0;
32a50b3a
RJ
595 while (!ret && (len > 0 || ooblen > 0)) {
596 calc_block_sector(from, &block0, &block1, &page, &ofs);
597 nbdata = min_t(size_t, len, (size_t)DOC_LAYOUT_PAGE_SIZE);
598 nboob = min_t(size_t, ooblen, (size_t)DOC_LAYOUT_OOB_SIZE);
efa2ca73
RJ
599 ret = doc_read_page_prepare(docg3, block0, block1, page, ofs);
600 if (ret < 0)
601 goto err;
602 ret = doc_read_page_ecc_init(docg3, DOC_ECC_BCH_COVERED_BYTES);
603 if (ret < 0)
604 goto err_in_read;
32a50b3a
RJ
605 ret = doc_read_page_getbytes(docg3, nbdata, buf, 1);
606 if (ret < nbdata)
efa2ca73 607 goto err_in_read;
32a50b3a
RJ
608 doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE - nbdata,
609 NULL, 0);
610 ret = doc_read_page_getbytes(docg3, nboob, oobbuf, 0);
611 if (ret < nboob)
efa2ca73 612 goto err_in_read;
32a50b3a
RJ
613 doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE - nboob,
614 NULL, 0);
efa2ca73 615
32a50b3a 616 doc_get_hw_bch_syndroms(docg3, calc_ecc);
efa2ca73
RJ
617 eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1);
618
32a50b3a
RJ
619 if (nboob >= DOC_LAYOUT_OOB_SIZE) {
620 doc_dbg("OOB - INFO: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
621 oobbuf[0], oobbuf[1], oobbuf[2], oobbuf[3],
622 oobbuf[4], oobbuf[5], oobbuf[6]);
623 doc_dbg("OOB - HAMMING: %02x\n", oobbuf[7]);
624 doc_dbg("OOB - BCH_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
625 oobbuf[8], oobbuf[9], oobbuf[10], oobbuf[11],
626 oobbuf[12], oobbuf[13], oobbuf[14]);
627 doc_dbg("OOB - UNUSED: %02x\n", oobbuf[15]);
628 }
efa2ca73 629 doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1);
32a50b3a
RJ
630 doc_dbg("ECC CALC_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
631 calc_ecc[0], calc_ecc[1], calc_ecc[2],
632 calc_ecc[3], calc_ecc[4], calc_ecc[5],
633 calc_ecc[6]);
efa2ca73
RJ
634
635 ret = -EBADMSG;
636 if (block0 >= DOC_LAYOUT_BLOCK_FIRST_DATA) {
32a50b3a
RJ
637 if ((eccconf1 & DOC_ECCCONF1_BCH_SYNDROM_ERR) &&
638 (eccconf1 & DOC_ECCCONF1_PAGE_IS_WRITTEN))
efa2ca73
RJ
639 goto err_in_read;
640 if (is_prot_seq_error(docg3))
641 goto err_in_read;
642 }
32a50b3a 643
efa2ca73 644 doc_read_page_finish(docg3);
32a50b3a
RJ
645 ops->retlen += nbdata;
646 ops->oobretlen += nboob;
647 buf += nbdata;
648 oobbuf += nboob;
649 len -= nbdata;
650 ooblen -= nboob;
651 from += DOC_LAYOUT_PAGE_SIZE;
efa2ca73
RJ
652 }
653
654 return 0;
655err_in_read:
656 doc_read_page_finish(docg3);
657err:
658 return ret;
659}
660
661/**
32a50b3a 662 * doc_read - Read bytes from flash
efa2ca73
RJ
663 * @mtd: the device
664 * @from: the offset from first block and first page, in bytes, aligned on page
665 * size
32a50b3a
RJ
666 * @len: the number of bytes to read (must be a multiple of 4)
667 * @retlen: the number of bytes actually read
668 * @buf: the filled in buffer
efa2ca73 669 *
32a50b3a
RJ
670 * Reads flash memory pages. This function does not read the OOB chunk, but only
671 * the page data.
efa2ca73
RJ
672 *
673 * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
674 */
32a50b3a
RJ
675static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
676 size_t *retlen, u_char *buf)
efa2ca73 677{
32a50b3a
RJ
678 struct mtd_oob_ops ops;
679 size_t ret;
efa2ca73 680
32a50b3a
RJ
681 memset(&ops, 0, sizeof(ops));
682 ops.datbuf = buf;
683 ops.len = len;
684 ops.mode = MTD_OPS_AUTO_OOB;
efa2ca73 685
32a50b3a
RJ
686 ret = doc_read_oob(mtd, from, &ops);
687 *retlen = ops.retlen;
688 return ret;
efa2ca73
RJ
689}
690
691static int doc_reload_bbt(struct docg3 *docg3)
692{
693 int block = DOC_LAYOUT_BLOCK_BBT;
694 int ret = 0, nbpages, page;
695 u_char *buf = docg3->bbt;
696
697 nbpages = DIV_ROUND_UP(docg3->max_block + 1, 8 * DOC_LAYOUT_PAGE_SIZE);
698 for (page = 0; !ret && (page < nbpages); page++) {
699 ret = doc_read_page_prepare(docg3, block, block + 1,
700 page + DOC_LAYOUT_PAGE_BBT, 0);
701 if (!ret)
702 ret = doc_read_page_ecc_init(docg3,
703 DOC_LAYOUT_PAGE_SIZE);
704 if (!ret)
705 doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE,
706 buf, 1);
707 buf += DOC_LAYOUT_PAGE_SIZE;
708 }
709 doc_read_page_finish(docg3);
710 return ret;
711}
712
713/**
714 * doc_block_isbad - Checks whether a block is good or not
715 * @mtd: the device
716 * @from: the offset to find the correct block
717 *
718 * Returns 1 if block is bad, 0 if block is good
719 */
720static int doc_block_isbad(struct mtd_info *mtd, loff_t from)
721{
722 struct docg3 *docg3 = mtd->priv;
723 int block0, block1, page, ofs, is_good;
724
725 calc_block_sector(from, &block0, &block1, &page, &ofs);
726 doc_dbg("doc_block_isbad(from=%lld) => block=(%d,%d), page=%d, ofs=%d\n",
727 from, block0, block1, page, ofs);
728
729 if (block0 < DOC_LAYOUT_BLOCK_FIRST_DATA)
730 return 0;
731 if (block1 > docg3->max_block)
732 return -EINVAL;
733
734 is_good = docg3->bbt[block0 >> 3] & (1 << (block0 & 0x7));
735 return !is_good;
736}
737
738/**
739 * doc_get_erase_count - Get block erase count
740 * @docg3: the device
741 * @from: the offset in which the block is.
742 *
743 * Get the number of times a block was erased. The number is the maximum of
744 * erase times between first and second plane (which should be equal normally).
745 *
746 * Returns The number of erases, or -EINVAL or -EIO on error.
747 */
748static int doc_get_erase_count(struct docg3 *docg3, loff_t from)
749{
750 u8 buf[DOC_LAYOUT_WEAR_SIZE];
751 int ret, plane1_erase_count, plane2_erase_count;
752 int block0, block1, page, ofs;
753
754 doc_dbg("doc_get_erase_count(from=%lld, buf=%p)\n", from, buf);
755 if (from % DOC_LAYOUT_PAGE_SIZE)
756 return -EINVAL;
757 calc_block_sector(from, &block0, &block1, &page, &ofs);
758 if (block1 > docg3->max_block)
759 return -EINVAL;
760
761 ret = doc_reset_seq(docg3);
762 if (!ret)
763 ret = doc_read_page_prepare(docg3, block0, block1, page,
764 ofs + DOC_LAYOUT_WEAR_OFFSET);
765 if (!ret)
766 ret = doc_read_page_getbytes(docg3, DOC_LAYOUT_WEAR_SIZE,
767 buf, 1);
768 doc_read_page_finish(docg3);
769
770 if (ret || (buf[0] != DOC_ERASE_MARK) || (buf[2] != DOC_ERASE_MARK))
771 return -EIO;
772 plane1_erase_count = (u8)(~buf[1]) | ((u8)(~buf[4]) << 8)
773 | ((u8)(~buf[5]) << 16);
774 plane2_erase_count = (u8)(~buf[3]) | ((u8)(~buf[6]) << 8)
775 | ((u8)(~buf[7]) << 16);
776
777 return max(plane1_erase_count, plane2_erase_count);
778}
779
780/*
781 * Debug sysfs entries
782 */
783static int dbg_flashctrl_show(struct seq_file *s, void *p)
784{
785 struct docg3 *docg3 = (struct docg3 *)s->private;
786
787 int pos = 0;
788 u8 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
789
790 pos += seq_printf(s,
791 "FlashControl : 0x%02x (%s,CE# %s,%s,%s,flash %s)\n",
792 fctrl,
793 fctrl & DOC_CTRL_VIOLATION ? "protocol violation" : "-",
794 fctrl & DOC_CTRL_CE ? "active" : "inactive",
795 fctrl & DOC_CTRL_PROTECTION_ERROR ? "protection error" : "-",
796 fctrl & DOC_CTRL_SEQUENCE_ERROR ? "sequence error" : "-",
797 fctrl & DOC_CTRL_FLASHREADY ? "ready" : "not ready");
798 return pos;
799}
800DEBUGFS_RO_ATTR(flashcontrol, dbg_flashctrl_show);
801
802static int dbg_asicmode_show(struct seq_file *s, void *p)
803{
804 struct docg3 *docg3 = (struct docg3 *)s->private;
805
806 int pos = 0;
807 int pctrl = doc_register_readb(docg3, DOC_ASICMODE);
808 int mode = pctrl & 0x03;
809
810 pos += seq_printf(s,
811 "%04x : RAM_WE=%d,RSTIN_RESET=%d,BDETCT_RESET=%d,WRITE_ENABLE=%d,POWERDOWN=%d,MODE=%d%d (",
812 pctrl,
813 pctrl & DOC_ASICMODE_RAM_WE ? 1 : 0,
814 pctrl & DOC_ASICMODE_RSTIN_RESET ? 1 : 0,
815 pctrl & DOC_ASICMODE_BDETCT_RESET ? 1 : 0,
816 pctrl & DOC_ASICMODE_MDWREN ? 1 : 0,
817 pctrl & DOC_ASICMODE_POWERDOWN ? 1 : 0,
818 mode >> 1, mode & 0x1);
819
820 switch (mode) {
821 case DOC_ASICMODE_RESET:
822 pos += seq_printf(s, "reset");
823 break;
824 case DOC_ASICMODE_NORMAL:
825 pos += seq_printf(s, "normal");
826 break;
827 case DOC_ASICMODE_POWERDOWN:
828 pos += seq_printf(s, "powerdown");
829 break;
830 }
831 pos += seq_printf(s, ")\n");
832 return pos;
833}
834DEBUGFS_RO_ATTR(asic_mode, dbg_asicmode_show);
835
836static int dbg_device_id_show(struct seq_file *s, void *p)
837{
838 struct docg3 *docg3 = (struct docg3 *)s->private;
839 int pos = 0;
840 int id = doc_register_readb(docg3, DOC_DEVICESELECT);
841
842 pos += seq_printf(s, "DeviceId = %d\n", id);
843 return pos;
844}
845DEBUGFS_RO_ATTR(device_id, dbg_device_id_show);
846
847static int dbg_protection_show(struct seq_file *s, void *p)
848{
849 struct docg3 *docg3 = (struct docg3 *)s->private;
850 int pos = 0;
dbc26d98
RJ
851 int protect, dps0, dps0_low, dps0_high, dps1, dps1_low, dps1_high;
852
853 protect = doc_register_readb(docg3, DOC_PROTECTION);
854 dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
855 dps0_low = doc_register_readw(docg3, DOC_DPS0_ADDRLOW);
856 dps0_high = doc_register_readw(docg3, DOC_DPS0_ADDRHIGH);
857 dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
858 dps1_low = doc_register_readw(docg3, DOC_DPS1_ADDRLOW);
859 dps1_high = doc_register_readw(docg3, DOC_DPS1_ADDRHIGH);
efa2ca73
RJ
860
861 pos += seq_printf(s, "Protection = 0x%02x (",
862 protect);
863 if (protect & DOC_PROTECT_FOUNDRY_OTP_LOCK)
864 pos += seq_printf(s, "FOUNDRY_OTP_LOCK,");
865 if (protect & DOC_PROTECT_CUSTOMER_OTP_LOCK)
866 pos += seq_printf(s, "CUSTOMER_OTP_LOCK,");
867 if (protect & DOC_PROTECT_LOCK_INPUT)
868 pos += seq_printf(s, "LOCK_INPUT,");
869 if (protect & DOC_PROTECT_STICKY_LOCK)
870 pos += seq_printf(s, "STICKY_LOCK,");
871 if (protect & DOC_PROTECT_PROTECTION_ENABLED)
872 pos += seq_printf(s, "PROTECTION ON,");
873 if (protect & DOC_PROTECT_IPL_DOWNLOAD_LOCK)
874 pos += seq_printf(s, "IPL_DOWNLOAD_LOCK,");
875 if (protect & DOC_PROTECT_PROTECTION_ERROR)
876 pos += seq_printf(s, "PROTECT_ERR,");
877 else
878 pos += seq_printf(s, "NO_PROTECT_ERR");
879 pos += seq_printf(s, ")\n");
880
881 pos += seq_printf(s, "DPS0 = 0x%02x : "
882 "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
883 "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
884 dps0, dps0_low, dps0_high,
885 !!(dps0 & DOC_DPS_OTP_PROTECTED),
886 !!(dps0 & DOC_DPS_READ_PROTECTED),
887 !!(dps0 & DOC_DPS_WRITE_PROTECTED),
888 !!(dps0 & DOC_DPS_HW_LOCK_ENABLED),
889 !!(dps0 & DOC_DPS_KEY_OK));
890 pos += seq_printf(s, "DPS1 = 0x%02x : "
891 "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
892 "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
893 dps1, dps1_low, dps1_high,
894 !!(dps1 & DOC_DPS_OTP_PROTECTED),
895 !!(dps1 & DOC_DPS_READ_PROTECTED),
896 !!(dps1 & DOC_DPS_WRITE_PROTECTED),
897 !!(dps1 & DOC_DPS_HW_LOCK_ENABLED),
898 !!(dps1 & DOC_DPS_KEY_OK));
899 return pos;
900}
901DEBUGFS_RO_ATTR(protection, dbg_protection_show);
902
903static int __init doc_dbg_register(struct docg3 *docg3)
904{
905 struct dentry *root, *entry;
906
907 root = debugfs_create_dir("docg3", NULL);
908 if (!root)
909 return -ENOMEM;
910
911 entry = debugfs_create_file("flashcontrol", S_IRUSR, root, docg3,
912 &flashcontrol_fops);
913 if (entry)
914 entry = debugfs_create_file("asic_mode", S_IRUSR, root,
915 docg3, &asic_mode_fops);
916 if (entry)
917 entry = debugfs_create_file("device_id", S_IRUSR, root,
918 docg3, &device_id_fops);
919 if (entry)
920 entry = debugfs_create_file("protection", S_IRUSR, root,
921 docg3, &protection_fops);
922 if (entry) {
923 docg3->debugfs_root = root;
924 return 0;
925 } else {
926 debugfs_remove_recursive(root);
927 return -ENOMEM;
928 }
929}
930
931static void __exit doc_dbg_unregister(struct docg3 *docg3)
932{
933 debugfs_remove_recursive(docg3->debugfs_root);
934}
935
936/**
937 * doc_set_driver_info - Fill the mtd_info structure and docg3 structure
938 * @chip_id: The chip ID of the supported chip
939 * @mtd: The structure to fill
940 */
941static void __init doc_set_driver_info(int chip_id, struct mtd_info *mtd)
942{
943 struct docg3 *docg3 = mtd->priv;
944 int cfg;
945
946 cfg = doc_register_readb(docg3, DOC_CONFIGURATION);
947 docg3->if_cfg = (cfg & DOC_CONF_IF_CFG ? 1 : 0);
948
949 switch (chip_id) {
950 case DOC_CHIPID_G3:
ae9d4934
RJ
951 mtd->name = kasprintf(GFP_KERNEL, "DiskOnChip G3 floor %d",
952 docg3->device_id);
efa2ca73
RJ
953 docg3->max_block = 2047;
954 break;
955 }
956 mtd->type = MTD_NANDFLASH;
957 /*
958 * Once write methods are added, the correct flags will be set.
959 * mtd->flags = MTD_CAP_NANDFLASH;
960 */
961 mtd->flags = MTD_CAP_ROM;
962 mtd->size = (docg3->max_block + 1) * DOC_LAYOUT_BLOCK_SIZE;
963 mtd->erasesize = DOC_LAYOUT_BLOCK_SIZE * DOC_LAYOUT_NBPLANES;
964 mtd->writesize = DOC_LAYOUT_PAGE_SIZE;
965 mtd->oobsize = DOC_LAYOUT_OOB_SIZE;
966 mtd->owner = THIS_MODULE;
967 mtd->erase = NULL;
968 mtd->point = NULL;
969 mtd->unpoint = NULL;
970 mtd->read = doc_read;
971 mtd->write = NULL;
972 mtd->read_oob = doc_read_oob;
973 mtd->write_oob = NULL;
974 mtd->sync = NULL;
975 mtd->block_isbad = doc_block_isbad;
976}
977
978/**
ae9d4934
RJ
979 * doc_probe_device - Check if a device is available
980 * @base: the io space where the device is probed
981 * @floor: the floor of the probed device
982 * @dev: the device
efa2ca73 983 *
ae9d4934 984 * Checks whether a device at the specified IO range, and floor is available.
efa2ca73 985 *
ae9d4934
RJ
986 * Returns a mtd_info struct if there is a device, ENODEV if none found, ENOMEM
987 * if a memory allocation failed. If floor 0 is checked, a reset of the ASIC is
988 * launched.
efa2ca73 989 */
ae9d4934
RJ
990static struct mtd_info *doc_probe_device(void __iomem *base, int floor,
991 struct device *dev)
efa2ca73 992{
efa2ca73
RJ
993 int ret, bbt_nbpages;
994 u16 chip_id, chip_id_inv;
ae9d4934
RJ
995 struct docg3 *docg3;
996 struct mtd_info *mtd;
efa2ca73
RJ
997
998 ret = -ENOMEM;
999 docg3 = kzalloc(sizeof(struct docg3), GFP_KERNEL);
1000 if (!docg3)
1001 goto nomem1;
1002 mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
1003 if (!mtd)
1004 goto nomem2;
1005 mtd->priv = docg3;
ae9d4934
RJ
1006 bbt_nbpages = DIV_ROUND_UP(docg3->max_block + 1,
1007 8 * DOC_LAYOUT_PAGE_SIZE);
1008 docg3->bbt = kzalloc(bbt_nbpages * DOC_LAYOUT_PAGE_SIZE, GFP_KERNEL);
1009 if (!docg3->bbt)
1010 goto nomem3;
efa2ca73 1011
ae9d4934
RJ
1012 docg3->dev = dev;
1013 docg3->device_id = floor;
1014 docg3->base = base;
efa2ca73 1015 doc_set_device_id(docg3, docg3->device_id);
ae9d4934
RJ
1016 if (!floor)
1017 doc_set_asic_mode(docg3, DOC_ASICMODE_RESET);
efa2ca73
RJ
1018 doc_set_asic_mode(docg3, DOC_ASICMODE_NORMAL);
1019
1020 chip_id = doc_register_readw(docg3, DOC_CHIPID);
1021 chip_id_inv = doc_register_readw(docg3, DOC_CHIPID_INV);
1022
ae9d4934 1023 ret = 0;
efa2ca73 1024 if (chip_id != (u16)(~chip_id_inv)) {
ae9d4934 1025 goto nomem3;
efa2ca73
RJ
1026 }
1027
1028 switch (chip_id) {
1029 case DOC_CHIPID_G3:
ae9d4934
RJ
1030 doc_info("Found a G3 DiskOnChip at addr %p, floor %d\n",
1031 base, floor);
efa2ca73
RJ
1032 break;
1033 default:
1034 doc_err("Chip id %04x is not a DiskOnChip G3 chip\n", chip_id);
ae9d4934 1035 goto nomem3;
efa2ca73
RJ
1036 }
1037
1038 doc_set_driver_info(chip_id, mtd);
efa2ca73 1039
efa2ca73 1040 doc_reload_bbt(docg3);
ae9d4934 1041 return mtd;
efa2ca73 1042
ae9d4934 1043nomem3:
efa2ca73
RJ
1044 kfree(mtd);
1045nomem2:
1046 kfree(docg3);
1047nomem1:
ae9d4934
RJ
1048 return ERR_PTR(ret);
1049}
1050
1051/**
1052 * doc_release_device - Release a docg3 floor
1053 * @mtd: the device
1054 */
1055static void doc_release_device(struct mtd_info *mtd)
1056{
1057 struct docg3 *docg3 = mtd->priv;
1058
1059 mtd_device_unregister(mtd);
1060 kfree(docg3->bbt);
1061 kfree(docg3);
1062 kfree(mtd->name);
1063 kfree(mtd);
1064}
1065
1066/**
1067 * doc_probe - Probe the IO space for a DiskOnChip G3 chip
1068 * @pdev: platform device
1069 *
1070 * Probes for a G3 chip at the specified IO space in the platform data
1071 * ressources. The floor 0 must be available.
1072 *
1073 * Returns 0 on success, -ENOMEM, -ENXIO on error
1074 */
1075static int __init docg3_probe(struct platform_device *pdev)
1076{
1077 struct device *dev = &pdev->dev;
1078 struct mtd_info *mtd;
1079 struct resource *ress;
1080 void __iomem *base;
1081 int ret, floor, found = 0;
1082 struct mtd_info **docg3_floors;
1083
1084 ret = -ENXIO;
1085 ress = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1086 if (!ress) {
1087 dev_err(dev, "No I/O memory resource defined\n");
1088 goto noress;
1089 }
1090 base = ioremap(ress->start, DOC_IOSPACE_SIZE);
1091
1092 ret = -ENOMEM;
1093 docg3_floors = kzalloc(sizeof(*docg3_floors) * DOC_MAX_NBFLOORS,
1094 GFP_KERNEL);
1095 if (!docg3_floors)
1096 goto nomem;
1097
1098 ret = 0;
1099 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
1100 mtd = doc_probe_device(base, floor, dev);
1101 if (floor == 0 && !mtd)
1102 goto notfound;
1103 if (!IS_ERR_OR_NULL(mtd))
1104 ret = mtd_device_parse_register(mtd, part_probes,
1105 NULL, NULL, 0);
1106 else
1107 ret = PTR_ERR(mtd);
1108 docg3_floors[floor] = mtd;
1109 if (ret)
1110 goto err_probe;
1111 if (mtd)
1112 found++;
1113 }
1114
1115 if (!found)
1116 goto notfound;
1117
1118 platform_set_drvdata(pdev, docg3_floors);
1119 doc_dbg_register(docg3_floors[0]->priv);
1120 return 0;
1121
1122notfound:
1123 ret = -ENODEV;
1124 dev_info(dev, "No supported DiskOnChip found\n");
1125err_probe:
1126 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
1127 if (docg3_floors[floor])
1128 doc_release_device(docg3_floors[floor]);
1129nomem:
1130 iounmap(base);
1131noress:
efa2ca73
RJ
1132 return ret;
1133}
1134
1135/**
1136 * docg3_release - Release the driver
1137 * @pdev: the platform device
1138 *
1139 * Returns 0
1140 */
1141static int __exit docg3_release(struct platform_device *pdev)
1142{
ae9d4934
RJ
1143 struct mtd_info **docg3_floors = platform_get_drvdata(pdev);
1144 struct docg3 *docg3 = docg3_floors[0]->priv;
1145 void __iomem *base = docg3->base;
1146 int floor;
efa2ca73
RJ
1147
1148 doc_dbg_unregister(docg3);
ae9d4934
RJ
1149 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
1150 if (docg3_floors[floor])
1151 doc_release_device(docg3_floors[floor]);
1152
1153 kfree(docg3_floors);
1154 iounmap(base);
efa2ca73
RJ
1155 return 0;
1156}
1157
1158static struct platform_driver g3_driver = {
1159 .driver = {
1160 .name = "docg3",
1161 .owner = THIS_MODULE,
1162 },
1163 .remove = __exit_p(docg3_release),
1164};
1165
1166static int __init docg3_init(void)
1167{
1168 return platform_driver_probe(&g3_driver, docg3_probe);
1169}
1170module_init(docg3_init);
1171
1172
1173static void __exit docg3_exit(void)
1174{
1175 platform_driver_unregister(&g3_driver);
1176}
1177module_exit(docg3_exit);
1178
1179MODULE_LICENSE("GPL");
1180MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
1181MODULE_DESCRIPTION("MTD driver for DiskOnChip G3");
This page took 0.121135 seconds and 5 git commands to generate.