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