3 * Linux driver for Disk-On-Chip 2000 and Millennium
4 * (c) 1999 Machine Vision Holdings, Inc.
5 * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
7 * $Id: doc2000.c,v 1.67 2005/11/07 11:14:24 gleixner Exp $
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <asm/errno.h>
14 #include <asm/uaccess.h>
15 #include <linux/miscdevice.h>
16 #include <linux/pci.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/sched.h>
20 #include <linux/init.h>
21 #include <linux/types.h>
22 #include <linux/bitops.h>
24 #include <linux/mtd/mtd.h>
25 #include <linux/mtd/nand.h>
26 #include <linux/mtd/doc2000.h>
28 #define DOC_SUPPORT_2000
29 #define DOC_SUPPORT_2000TSOP
30 #define DOC_SUPPORT_MILLENNIUM
32 #ifdef DOC_SUPPORT_2000
33 #define DoC_is_2000(doc) (doc->ChipID == DOC_ChipID_Doc2k)
35 #define DoC_is_2000(doc) (0)
38 #if defined(DOC_SUPPORT_2000TSOP) || defined(DOC_SUPPORT_MILLENNIUM)
39 #define DoC_is_Millennium(doc) (doc->ChipID == DOC_ChipID_DocMil)
41 #define DoC_is_Millennium(doc) (0)
44 /* #define ECC_DEBUG */
46 /* I have no idea why some DoC chips can not use memcpy_from|to_io().
47 * This may be due to the different revisions of the ASIC controller built-in or
48 * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
53 static int doc_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
54 size_t *retlen
, u_char
*buf
);
55 static int doc_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
56 size_t *retlen
, const u_char
*buf
);
57 static int doc_read_ecc(struct mtd_info
*mtd
, loff_t from
, size_t len
,
58 size_t *retlen
, u_char
*buf
, u_char
*eccbuf
, struct nand_oobinfo
*oobsel
);
59 static int doc_write_ecc(struct mtd_info
*mtd
, loff_t to
, size_t len
,
60 size_t *retlen
, const u_char
*buf
, u_char
*eccbuf
, struct nand_oobinfo
*oobsel
);
61 static int doc_writev_ecc(struct mtd_info
*mtd
, const struct kvec
*vecs
,
62 unsigned long count
, loff_t to
, size_t *retlen
,
63 u_char
*eccbuf
, struct nand_oobinfo
*oobsel
);
64 static int doc_read_oob(struct mtd_info
*mtd
, loff_t ofs
, size_t len
,
65 size_t *retlen
, u_char
*buf
);
66 static int doc_write_oob(struct mtd_info
*mtd
, loff_t ofs
, size_t len
,
67 size_t *retlen
, const u_char
*buf
);
68 static int doc_write_oob_nolock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
,
69 size_t *retlen
, const u_char
*buf
);
70 static int doc_erase (struct mtd_info
*mtd
, struct erase_info
*instr
);
72 static struct mtd_info
*doc2klist
= NULL
;
74 /* Perform the required delay cycles by reading from the appropriate register */
75 static void DoC_Delay(struct DiskOnChip
*doc
, unsigned short cycles
)
80 for (i
= 0; i
< cycles
; i
++) {
81 if (DoC_is_Millennium(doc
))
82 dummy
= ReadDOC(doc
->virtadr
, NOP
);
84 dummy
= ReadDOC(doc
->virtadr
, DOCStatus
);
89 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
90 static int _DoC_WaitReady(struct DiskOnChip
*doc
)
92 void __iomem
*docptr
= doc
->virtadr
;
93 unsigned long timeo
= jiffies
+ (HZ
* 10);
95 DEBUG(MTD_DEBUG_LEVEL3
,
96 "_DoC_WaitReady called for out-of-line wait\n");
98 /* Out-of-line routine to wait for chip response */
99 while (!(ReadDOC(docptr
, CDSNControl
) & CDSN_CTRL_FR_B
)) {
100 /* issue 2 read from NOP register after reading from CDSNControl register
101 see Software Requirement 11.4 item 2. */
104 if (time_after(jiffies
, timeo
)) {
105 DEBUG(MTD_DEBUG_LEVEL2
, "_DoC_WaitReady timed out.\n");
115 static inline int DoC_WaitReady(struct DiskOnChip
*doc
)
117 void __iomem
*docptr
= doc
->virtadr
;
119 /* This is inline, to optimise the common case, where it's ready instantly */
122 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
123 see Software Requirement 11.4 item 2. */
126 if (!(ReadDOC(docptr
, CDSNControl
) & CDSN_CTRL_FR_B
))
127 /* Call the out-of-line routine to wait */
128 ret
= _DoC_WaitReady(doc
);
130 /* issue 2 read from NOP register after reading from CDSNControl register
131 see Software Requirement 11.4 item 2. */
137 /* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to
138 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
139 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
141 static int DoC_Command(struct DiskOnChip
*doc
, unsigned char command
,
142 unsigned char xtraflags
)
144 void __iomem
*docptr
= doc
->virtadr
;
146 if (DoC_is_2000(doc
))
147 xtraflags
|= CDSN_CTRL_FLASH_IO
;
149 /* Assert the CLE (Command Latch Enable) line to the flash chip */
150 WriteDOC(xtraflags
| CDSN_CTRL_CLE
| CDSN_CTRL_CE
, docptr
, CDSNControl
);
151 DoC_Delay(doc
, 4); /* Software requirement 11.4.3 for Millennium */
153 if (DoC_is_Millennium(doc
))
154 WriteDOC(command
, docptr
, CDSNSlowIO
);
156 /* Send the command */
157 WriteDOC_(command
, docptr
, doc
->ioreg
);
158 if (DoC_is_Millennium(doc
))
159 WriteDOC(command
, docptr
, WritePipeTerm
);
161 /* Lower the CLE line */
162 WriteDOC(xtraflags
| CDSN_CTRL_CE
, docptr
, CDSNControl
);
163 DoC_Delay(doc
, 4); /* Software requirement 11.4.3 for Millennium */
165 /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */
166 return DoC_WaitReady(doc
);
169 /* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
170 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
171 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
173 static int DoC_Address(struct DiskOnChip
*doc
, int numbytes
, unsigned long ofs
,
174 unsigned char xtraflags1
, unsigned char xtraflags2
)
177 void __iomem
*docptr
= doc
->virtadr
;
179 if (DoC_is_2000(doc
))
180 xtraflags1
|= CDSN_CTRL_FLASH_IO
;
182 /* Assert the ALE (Address Latch Enable) line to the flash chip */
183 WriteDOC(xtraflags1
| CDSN_CTRL_ALE
| CDSN_CTRL_CE
, docptr
, CDSNControl
);
185 DoC_Delay(doc
, 4); /* Software requirement 11.4.3 for Millennium */
187 /* Send the address */
188 /* Devices with 256-byte page are addressed as:
189 Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
190 * there is no device on the market with page256
191 and more than 24 bits.
192 Devices with 512-byte page are addressed as:
193 Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
194 * 25-31 is sent only if the chip support it.
195 * bit 8 changes the read command to be sent
196 (NAND_CMD_READ0 or NAND_CMD_READ1).
199 if (numbytes
== ADDR_COLUMN
|| numbytes
== ADDR_COLUMN_PAGE
) {
200 if (DoC_is_Millennium(doc
))
201 WriteDOC(ofs
& 0xff, docptr
, CDSNSlowIO
);
202 WriteDOC_(ofs
& 0xff, docptr
, doc
->ioreg
);
211 if (numbytes
== ADDR_PAGE
|| numbytes
== ADDR_COLUMN_PAGE
) {
212 for (i
= 0; i
< doc
->pageadrlen
; i
++, ofs
= ofs
>> 8) {
213 if (DoC_is_Millennium(doc
))
214 WriteDOC(ofs
& 0xff, docptr
, CDSNSlowIO
);
215 WriteDOC_(ofs
& 0xff, docptr
, doc
->ioreg
);
219 if (DoC_is_Millennium(doc
))
220 WriteDOC(ofs
& 0xff, docptr
, WritePipeTerm
);
222 DoC_Delay(doc
, 2); /* Needed for some slow flash chips. mf. */
224 /* FIXME: The SlowIO's for millennium could be replaced by
225 a single WritePipeTerm here. mf. */
227 /* Lower the ALE line */
228 WriteDOC(xtraflags1
| xtraflags2
| CDSN_CTRL_CE
, docptr
,
231 DoC_Delay(doc
, 4); /* Software requirement 11.4.3 for Millennium */
233 /* Wait for the chip to respond - Software requirement 11.4.1 */
234 return DoC_WaitReady(doc
);
237 /* Read a buffer from DoC, taking care of Millennium odditys */
238 static void DoC_ReadBuf(struct DiskOnChip
*doc
, u_char
* buf
, int len
)
241 int modulus
= 0xffff;
242 void __iomem
*docptr
= doc
->virtadr
;
248 if (DoC_is_Millennium(doc
)) {
249 /* Read the data via the internal pipeline through CDSN IO register,
250 see Pipelined Read Operations 11.3 */
251 dummy
= ReadDOC(docptr
, ReadPipeInit
);
253 /* Millennium should use the LastDataRead register - Pipeline Reads */
256 /* This is needed for correctly ECC calculation */
260 for (i
= 0; i
< len
; i
++)
261 buf
[i
] = ReadDOC_(docptr
, doc
->ioreg
+ (i
& modulus
));
263 if (DoC_is_Millennium(doc
)) {
264 buf
[i
] = ReadDOC(docptr
, LastDataRead
);
268 /* Write a buffer to DoC, taking care of Millennium odditys */
269 static void DoC_WriteBuf(struct DiskOnChip
*doc
, const u_char
* buf
, int len
)
271 void __iomem
*docptr
= doc
->virtadr
;
277 for (i
= 0; i
< len
; i
++)
278 WriteDOC_(buf
[i
], docptr
, doc
->ioreg
+ i
);
280 if (DoC_is_Millennium(doc
)) {
281 WriteDOC(0x00, docptr
, WritePipeTerm
);
286 /* DoC_SelectChip: Select a given flash chip within the current floor */
288 static inline int DoC_SelectChip(struct DiskOnChip
*doc
, int chip
)
290 void __iomem
*docptr
= doc
->virtadr
;
292 /* Software requirement 11.4.4 before writing DeviceSelect */
293 /* Deassert the CE line to eliminate glitches on the FCE# outputs */
294 WriteDOC(CDSN_CTRL_WP
, docptr
, CDSNControl
);
295 DoC_Delay(doc
, 4); /* Software requirement 11.4.3 for Millennium */
297 /* Select the individual flash chip requested */
298 WriteDOC(chip
, docptr
, CDSNDeviceSelect
);
301 /* Reassert the CE line */
302 WriteDOC(CDSN_CTRL_CE
| CDSN_CTRL_FLASH_IO
| CDSN_CTRL_WP
, docptr
,
304 DoC_Delay(doc
, 4); /* Software requirement 11.4.3 for Millennium */
306 /* Wait for it to be ready */
307 return DoC_WaitReady(doc
);
310 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
312 static inline int DoC_SelectFloor(struct DiskOnChip
*doc
, int floor
)
314 void __iomem
*docptr
= doc
->virtadr
;
316 /* Select the floor (bank) of chips required */
317 WriteDOC(floor
, docptr
, FloorSelect
);
319 /* Wait for the chip to be ready */
320 return DoC_WaitReady(doc
);
323 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
325 static int DoC_IdentChip(struct DiskOnChip
*doc
, int floor
, int chip
)
330 /* Page in the required floor/chip */
331 DoC_SelectFloor(doc
, floor
);
332 DoC_SelectChip(doc
, chip
);
335 if (DoC_Command(doc
, NAND_CMD_RESET
, CDSN_CTRL_WP
)) {
336 DEBUG(MTD_DEBUG_LEVEL2
,
337 "DoC_Command (reset) for %d,%d returned true\n",
343 /* Read the NAND chip ID: 1. Send ReadID command */
344 if (DoC_Command(doc
, NAND_CMD_READID
, CDSN_CTRL_WP
)) {
345 DEBUG(MTD_DEBUG_LEVEL2
,
346 "DoC_Command (ReadID) for %d,%d returned true\n",
351 /* Read the NAND chip ID: 2. Send address byte zero */
352 DoC_Address(doc
, ADDR_COLUMN
, 0, CDSN_CTRL_WP
, 0);
354 /* Read the manufacturer and device id codes from the device */
356 if (DoC_is_Millennium(doc
)) {
358 dummy
= ReadDOC(doc
->virtadr
, ReadPipeInit
);
359 mfr
= ReadDOC(doc
->virtadr
, LastDataRead
);
362 dummy
= ReadDOC(doc
->virtadr
, ReadPipeInit
);
363 id
= ReadDOC(doc
->virtadr
, LastDataRead
);
365 /* CDSN Slow IO register see Software Req 11.4 item 5. */
366 dummy
= ReadDOC(doc
->virtadr
, CDSNSlowIO
);
368 mfr
= ReadDOC_(doc
->virtadr
, doc
->ioreg
);
370 /* CDSN Slow IO register see Software Req 11.4 item 5. */
371 dummy
= ReadDOC(doc
->virtadr
, CDSNSlowIO
);
373 id
= ReadDOC_(doc
->virtadr
, doc
->ioreg
);
376 /* No response - return failure */
377 if (mfr
== 0xff || mfr
== 0)
380 /* Check it's the same as the first chip we identified.
381 * M-Systems say that any given DiskOnChip device should only
382 * contain _one_ type of flash part, although that's not a
383 * hardware restriction. */
385 if (doc
->mfr
== mfr
&& doc
->id
== id
)
386 return 1; /* This is another the same the first */
389 "Flash chip at floor %d, chip %d is different:\n",
393 /* Print and store the manufacturer and ID codes. */
394 for (i
= 0; nand_flash_ids
[i
].name
!= NULL
; i
++) {
395 if (id
== nand_flash_ids
[i
].id
) {
396 /* Try to identify manufacturer */
397 for (j
= 0; nand_manuf_ids
[j
].id
!= 0x0; j
++) {
398 if (nand_manuf_ids
[j
].id
== mfr
)
402 "Flash chip found: Manufacturer ID: %2.2X, "
403 "Chip ID: %2.2X (%s:%s)\n", mfr
, id
,
404 nand_manuf_ids
[j
].name
, nand_flash_ids
[i
].name
);
409 ffs((nand_flash_ids
[i
].chipsize
<< 20)) - 1;
410 doc
->page256
= (nand_flash_ids
[i
].pagesize
== 256) ? 1 : 0;
411 doc
->pageadrlen
= doc
->chipshift
> 25 ? 3 : 2;
413 nand_flash_ids
[i
].erasesize
;
421 /* We haven't fully identified the chip. Print as much as we know. */
422 printk(KERN_WARNING
"Unknown flash chip found: %2.2X %2.2X\n",
425 printk(KERN_WARNING
"Please report to dwmw2@infradead.org\n");
429 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
431 static void DoC_ScanChips(struct DiskOnChip
*this, int maxchips
)
434 int numchips
[MAX_FLOORS
];
441 /* For each floor, find the number of valid chips it contains */
442 for (floor
= 0; floor
< MAX_FLOORS
; floor
++) {
445 for (chip
= 0; chip
< maxchips
&& ret
!= 0; chip
++) {
447 ret
= DoC_IdentChip(this, floor
, chip
);
455 /* If there are none at all that we recognise, bail */
456 if (!this->numchips
) {
457 printk(KERN_NOTICE
"No flash chips recognised.\n");
461 /* Allocate an array to hold the information for each chip */
462 this->chips
= kmalloc(sizeof(struct Nand
) * this->numchips
, GFP_KERNEL
);
464 printk(KERN_NOTICE
"No memory for allocating chip info structures\n");
470 /* Fill out the chip array with {floor, chipno} for each
471 * detected chip in the device. */
472 for (floor
= 0; floor
< MAX_FLOORS
; floor
++) {
473 for (chip
= 0; chip
< numchips
[floor
]; chip
++) {
474 this->chips
[ret
].floor
= floor
;
475 this->chips
[ret
].chip
= chip
;
476 this->chips
[ret
].curadr
= 0;
477 this->chips
[ret
].curmode
= 0x50;
482 /* Calculate and print the total size of the device */
483 this->totlen
= this->numchips
* (1 << this->chipshift
);
485 printk(KERN_INFO
"%d flash chips found. Total DiskOnChip size: %ld MiB\n",
486 this->numchips
, this->totlen
>> 20);
489 static int DoC2k_is_alias(struct DiskOnChip
*doc1
, struct DiskOnChip
*doc2
)
491 int tmp1
, tmp2
, retval
;
492 if (doc1
->physadr
== doc2
->physadr
)
495 /* Use the alias resolution register which was set aside for this
496 * purpose. If it's value is the same on both chips, they might
497 * be the same chip, and we write to one and check for a change in
498 * the other. It's unclear if this register is usuable in the
499 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
500 tmp1
= ReadDOC(doc1
->virtadr
, AliasResolution
);
501 tmp2
= ReadDOC(doc2
->virtadr
, AliasResolution
);
505 WriteDOC((tmp1
+ 1) % 0xff, doc1
->virtadr
, AliasResolution
);
506 tmp2
= ReadDOC(doc2
->virtadr
, AliasResolution
);
507 if (tmp2
== (tmp1
+ 1) % 0xff)
512 /* Restore register contents. May not be necessary, but do it just to
514 WriteDOC(tmp1
, doc1
->virtadr
, AliasResolution
);
519 static const char im_name
[] = "DoC2k_init";
521 /* This routine is made available to other mtd code via
522 * inter_module_register. It must only be accessed through
523 * inter_module_get which will bump the use count of this module. The
524 * addresses passed back in mtd are valid as long as the use count of
525 * this module is non-zero, i.e. between inter_module_get and
526 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
528 static void DoC2k_init(struct mtd_info
*mtd
)
530 struct DiskOnChip
*this = mtd
->priv
;
531 struct DiskOnChip
*old
= NULL
;
534 /* We must avoid being called twice for the same device. */
537 old
= doc2klist
->priv
;
540 if (DoC2k_is_alias(old
, this)) {
542 "Ignoring DiskOnChip 2000 at 0x%lX - already configured\n",
544 iounmap(this->virtadr
);
549 old
= old
->nextdoc
->priv
;
555 switch (this->ChipID
) {
556 case DOC_ChipID_Doc2kTSOP
:
557 mtd
->name
= "DiskOnChip 2000 TSOP";
558 this->ioreg
= DoC_Mil_CDSN_IO
;
559 /* Pretend it's a Millennium */
560 this->ChipID
= DOC_ChipID_DocMil
;
561 maxchips
= MAX_CHIPS
;
563 case DOC_ChipID_Doc2k
:
564 mtd
->name
= "DiskOnChip 2000";
565 this->ioreg
= DoC_2k_CDSN_IO
;
566 maxchips
= MAX_CHIPS
;
568 case DOC_ChipID_DocMil
:
569 mtd
->name
= "DiskOnChip Millennium";
570 this->ioreg
= DoC_Mil_CDSN_IO
;
571 maxchips
= MAX_CHIPS_MIL
;
574 printk("Unknown ChipID 0x%02x\n", this->ChipID
);
576 iounmap(this->virtadr
);
580 printk(KERN_NOTICE
"%s found at address 0x%lX\n", mtd
->name
,
583 mtd
->type
= MTD_NANDFLASH
;
584 mtd
->flags
= MTD_CAP_NANDFLASH
;
585 mtd
->ecctype
= MTD_ECC_RS_DiskOnChip
;
590 mtd
->owner
= THIS_MODULE
;
591 mtd
->erase
= doc_erase
;
594 mtd
->read
= doc_read
;
595 mtd
->write
= doc_write
;
596 mtd
->read_ecc
= doc_read_ecc
;
597 mtd
->write_ecc
= doc_write_ecc
;
598 mtd
->writev_ecc
= doc_writev_ecc
;
599 mtd
->read_oob
= doc_read_oob
;
600 mtd
->write_oob
= doc_write_oob
;
608 init_MUTEX(&this->lock
);
610 /* Ident all the chips present. */
611 DoC_ScanChips(this, maxchips
);
615 iounmap(this->virtadr
);
617 this->nextdoc
= doc2klist
;
619 mtd
->size
= this->totlen
;
620 mtd
->erasesize
= this->erasesize
;
626 static int doc_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
627 size_t * retlen
, u_char
* buf
)
629 /* Just a special case of doc_read_ecc */
630 return doc_read_ecc(mtd
, from
, len
, retlen
, buf
, NULL
, NULL
);
633 static int doc_read_ecc(struct mtd_info
*mtd
, loff_t from
, size_t len
,
634 size_t * retlen
, u_char
* buf
, u_char
* eccbuf
, struct nand_oobinfo
*oobsel
)
636 struct DiskOnChip
*this = mtd
->priv
;
637 void __iomem
*docptr
= this->virtadr
;
639 unsigned char syndrome
[6];
641 int i
, len256
= 0, ret
=0;
644 /* Don't allow read past end of device */
645 if (from
>= this->totlen
)
654 /* Don't allow a single read to cross a 512-byte block boundary */
655 if (from
+ len
> ((from
| 0x1ff) + 1))
656 len
= ((from
| 0x1ff) + 1) - from
;
658 /* The ECC will not be calculated correctly if less than 512 is read */
659 if (len
!= 0x200 && eccbuf
)
661 "ECC needs a full sector read (adr: %lx size %lx)\n",
662 (long) from
, (long) len
);
664 /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */
667 /* Find the chip which is to be used and select it */
668 mychip
= &this->chips
[from
>> (this->chipshift
)];
670 if (this->curfloor
!= mychip
->floor
) {
671 DoC_SelectFloor(this, mychip
->floor
);
672 DoC_SelectChip(this, mychip
->chip
);
673 } else if (this->curchip
!= mychip
->chip
) {
674 DoC_SelectChip(this, mychip
->chip
);
677 this->curfloor
= mychip
->floor
;
678 this->curchip
= mychip
->chip
;
682 && (from
& 0x100)) ? NAND_CMD_READ1
: NAND_CMD_READ0
,
684 DoC_Address(this, ADDR_COLUMN_PAGE
, from
, CDSN_CTRL_WP
,
688 /* Prime the ECC engine */
689 WriteDOC(DOC_ECC_RESET
, docptr
, ECCConf
);
690 WriteDOC(DOC_ECC_EN
, docptr
, ECCConf
);
692 /* disable the ECC engine */
693 WriteDOC(DOC_ECC_RESET
, docptr
, ECCConf
);
694 WriteDOC(DOC_ECC_DIS
, docptr
, ECCConf
);
697 /* treat crossing 256-byte sector for 2M x 8bits devices */
698 if (this->page256
&& from
+ len
> (from
| 0xff) + 1) {
699 len256
= (from
| 0xff) + 1 - from
;
700 DoC_ReadBuf(this, buf
, len256
);
702 DoC_Command(this, NAND_CMD_READ0
, CDSN_CTRL_WP
);
703 DoC_Address(this, ADDR_COLUMN_PAGE
, from
+ len256
,
704 CDSN_CTRL_WP
, CDSN_CTRL_ECC_IO
);
707 DoC_ReadBuf(this, &buf
[len256
], len
- len256
);
709 /* Let the caller know we completed it */
713 /* Read the ECC data through the DiskOnChip ECC logic */
714 /* Note: this will work even with 2M x 8bit devices as */
715 /* they have 8 bytes of OOB per 256 page. mf. */
716 DoC_ReadBuf(this, eccbuf
, 6);
718 /* Flush the pipeline */
719 if (DoC_is_Millennium(this)) {
720 dummy
= ReadDOC(docptr
, ECCConf
);
721 dummy
= ReadDOC(docptr
, ECCConf
);
722 i
= ReadDOC(docptr
, ECCConf
);
724 dummy
= ReadDOC(docptr
, 2k_ECCStatus
);
725 dummy
= ReadDOC(docptr
, 2k_ECCStatus
);
726 i
= ReadDOC(docptr
, 2k_ECCStatus
);
729 /* Check the ECC Status */
732 /* There was an ECC error */
734 printk(KERN_ERR
"DiskOnChip ECC Error: Read at %lx\n", (long)from
);
736 /* Read the ECC syndrom through the DiskOnChip ECC logic.
737 These syndrome will be all ZERO when there is no error */
738 for (i
= 0; i
< 6; i
++) {
740 ReadDOC(docptr
, ECCSyndrome0
+ i
);
742 nb_errors
= doc_decode_ecc(buf
, syndrome
);
745 printk(KERN_ERR
"Errors corrected: %x\n", nb_errors
);
748 /* We return error, but have actually done the read. Not that
749 this can be told to user-space, via sys_read(), but at least
750 MTD-aware stuff can know about it by checking *retlen */
756 printk(KERN_DEBUG
"ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
757 (long)from
, eccbuf
[0], eccbuf
[1], eccbuf
[2],
758 eccbuf
[3], eccbuf
[4], eccbuf
[5]);
761 /* disable the ECC engine */
762 WriteDOC(DOC_ECC_DIS
, docptr
, ECCConf
);
765 /* according to 11.4.1, we need to wait for the busy line
766 * drop if we read to the end of the page. */
767 if(0 == ((from
+ len
) & 0x1ff))
782 static int doc_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
783 size_t * retlen
, const u_char
* buf
)
786 return doc_write_ecc(mtd
, to
, len
, retlen
, buf
, eccbuf
, NULL
);
789 static int doc_write_ecc(struct mtd_info
*mtd
, loff_t to
, size_t len
,
790 size_t * retlen
, const u_char
* buf
,
791 u_char
* eccbuf
, struct nand_oobinfo
*oobsel
)
793 struct DiskOnChip
*this = mtd
->priv
;
794 int di
; /* Yes, DI is a hangover from when I was disassembling the binary driver */
795 void __iomem
*docptr
= this->virtadr
;
802 /* Don't allow write past end of device */
803 if (to
>= this->totlen
)
812 /* Don't allow a single write to cross a 512-byte block boundary */
813 if (to
+ len
> ((to
| 0x1ff) + 1))
814 len
= ((to
| 0x1ff) + 1) - to
;
816 /* The ECC will not be calculated correctly if less than 512 is written */
818 if (len != 0x200 && eccbuf)
820 "ECC needs a full sector write (adr: %lx size %lx)\n",
821 (long) to, (long) len);
824 /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
826 /* Find the chip which is to be used and select it */
827 mychip
= &this->chips
[to
>> (this->chipshift
)];
829 if (this->curfloor
!= mychip
->floor
) {
830 DoC_SelectFloor(this, mychip
->floor
);
831 DoC_SelectChip(this, mychip
->chip
);
832 } else if (this->curchip
!= mychip
->chip
) {
833 DoC_SelectChip(this, mychip
->chip
);
836 this->curfloor
= mychip
->floor
;
837 this->curchip
= mychip
->chip
;
839 /* Set device to main plane of flash */
840 DoC_Command(this, NAND_CMD_RESET
, CDSN_CTRL_WP
);
843 && (to
& 0x100)) ? NAND_CMD_READ1
: NAND_CMD_READ0
,
846 DoC_Command(this, NAND_CMD_SEQIN
, 0);
847 DoC_Address(this, ADDR_COLUMN_PAGE
, to
, 0, CDSN_CTRL_ECC_IO
);
850 /* Prime the ECC engine */
851 WriteDOC(DOC_ECC_RESET
, docptr
, ECCConf
);
852 WriteDOC(DOC_ECC_EN
| DOC_ECC_RW
, docptr
, ECCConf
);
854 /* disable the ECC engine */
855 WriteDOC(DOC_ECC_RESET
, docptr
, ECCConf
);
856 WriteDOC(DOC_ECC_DIS
, docptr
, ECCConf
);
859 /* treat crossing 256-byte sector for 2M x 8bits devices */
860 if (this->page256
&& to
+ len
> (to
| 0xff) + 1) {
861 len256
= (to
| 0xff) + 1 - to
;
862 DoC_WriteBuf(this, buf
, len256
);
864 DoC_Command(this, NAND_CMD_PAGEPROG
, 0);
866 DoC_Command(this, NAND_CMD_STATUS
, CDSN_CTRL_WP
);
867 /* There's an implicit DoC_WaitReady() in DoC_Command */
869 dummy
= ReadDOC(docptr
, CDSNSlowIO
);
872 if (ReadDOC_(docptr
, this->ioreg
) & 1) {
873 printk(KERN_ERR
"Error programming flash\n");
874 /* Error in programming */
880 DoC_Command(this, NAND_CMD_SEQIN
, 0);
881 DoC_Address(this, ADDR_COLUMN_PAGE
, to
+ len256
, 0,
885 DoC_WriteBuf(this, &buf
[len256
], len
- len256
);
888 WriteDOC(CDSN_CTRL_ECC_IO
| CDSN_CTRL_CE
, docptr
,
891 if (DoC_is_Millennium(this)) {
892 WriteDOC(0, docptr
, NOP
);
893 WriteDOC(0, docptr
, NOP
);
894 WriteDOC(0, docptr
, NOP
);
896 WriteDOC_(0, docptr
, this->ioreg
);
897 WriteDOC_(0, docptr
, this->ioreg
);
898 WriteDOC_(0, docptr
, this->ioreg
);
901 WriteDOC(CDSN_CTRL_ECC_IO
| CDSN_CTRL_FLASH_IO
| CDSN_CTRL_CE
, docptr
,
904 /* Read the ECC data through the DiskOnChip ECC logic */
905 for (di
= 0; di
< 6; di
++) {
906 eccbuf
[di
] = ReadDOC(docptr
, ECCSyndrome0
+ di
);
909 /* Reset the ECC engine */
910 WriteDOC(DOC_ECC_DIS
, docptr
, ECCConf
);
914 ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
915 (long) to
, eccbuf
[0], eccbuf
[1], eccbuf
[2], eccbuf
[3],
916 eccbuf
[4], eccbuf
[5]);
920 DoC_Command(this, NAND_CMD_PAGEPROG
, 0);
922 DoC_Command(this, NAND_CMD_STATUS
, CDSN_CTRL_WP
);
923 /* There's an implicit DoC_WaitReady() in DoC_Command */
925 if (DoC_is_Millennium(this)) {
926 ReadDOC(docptr
, ReadPipeInit
);
927 status
= ReadDOC(docptr
, LastDataRead
);
929 dummy
= ReadDOC(docptr
, CDSNSlowIO
);
931 status
= ReadDOC_(docptr
, this->ioreg
);
935 printk(KERN_ERR
"Error programming flash\n");
936 /* Error in programming */
942 /* Let the caller know we completed it */
950 /* Write the ECC data to flash */
951 for (di
=0; di
<6; di
++)
957 ret
= doc_write_oob_nolock(mtd
, to
, 8, &dummy
, x
);
973 static int doc_writev_ecc(struct mtd_info
*mtd
, const struct kvec
*vecs
,
974 unsigned long count
, loff_t to
, size_t *retlen
,
975 u_char
*eccbuf
, struct nand_oobinfo
*oobsel
)
977 static char static_buf
[512];
978 static DECLARE_MUTEX(writev_buf_sem
);
980 size_t totretlen
= 0;
981 size_t thisvecofs
= 0;
984 down(&writev_buf_sem
);
987 size_t thislen
, thisretlen
;
990 buf
= vecs
->iov_base
+ thisvecofs
;
991 thislen
= vecs
->iov_len
- thisvecofs
;
994 if (thislen
>= 512) {
995 thislen
= thislen
& ~(512-1);
996 thisvecofs
+= thislen
;
998 /* Not enough to fill a page. Copy into buf */
999 memcpy(static_buf
, buf
, thislen
);
1000 buf
= &static_buf
[thislen
];
1002 while(count
&& thislen
< 512) {
1005 thisvecofs
= min((512-thislen
), vecs
->iov_len
);
1006 memcpy(buf
, vecs
->iov_base
, thisvecofs
);
1007 thislen
+= thisvecofs
;
1012 if (count
&& thisvecofs
== vecs
->iov_len
) {
1017 ret
= doc_write_ecc(mtd
, to
, thislen
, &thisretlen
, buf
, eccbuf
, oobsel
);
1019 totretlen
+= thisretlen
;
1021 if (ret
|| thisretlen
!= thislen
)
1027 up(&writev_buf_sem
);
1028 *retlen
= totretlen
;
1033 static int doc_read_oob(struct mtd_info
*mtd
, loff_t ofs
, size_t len
,
1034 size_t * retlen
, u_char
* buf
)
1036 struct DiskOnChip
*this = mtd
->priv
;
1037 int len256
= 0, ret
;
1038 struct Nand
*mychip
;
1042 mychip
= &this->chips
[ofs
>> this->chipshift
];
1044 if (this->curfloor
!= mychip
->floor
) {
1045 DoC_SelectFloor(this, mychip
->floor
);
1046 DoC_SelectChip(this, mychip
->chip
);
1047 } else if (this->curchip
!= mychip
->chip
) {
1048 DoC_SelectChip(this, mychip
->chip
);
1050 this->curfloor
= mychip
->floor
;
1051 this->curchip
= mychip
->chip
;
1053 /* update address for 2M x 8bit devices. OOB starts on the second */
1054 /* page to maintain compatibility with doc_read_ecc. */
1055 if (this->page256
) {
1062 DoC_Command(this, NAND_CMD_READOOB
, CDSN_CTRL_WP
);
1063 DoC_Address(this, ADDR_COLUMN_PAGE
, ofs
, CDSN_CTRL_WP
, 0);
1065 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1066 /* Note: datasheet says it should automaticaly wrap to the */
1067 /* next OOB block, but it didn't work here. mf. */
1068 if (this->page256
&& ofs
+ len
> (ofs
| 0x7) + 1) {
1069 len256
= (ofs
| 0x7) + 1 - ofs
;
1070 DoC_ReadBuf(this, buf
, len256
);
1072 DoC_Command(this, NAND_CMD_READOOB
, CDSN_CTRL_WP
);
1073 DoC_Address(this, ADDR_COLUMN_PAGE
, ofs
& (~0x1ff),
1077 DoC_ReadBuf(this, &buf
[len256
], len
- len256
);
1080 /* Reading the full OOB data drops us off of the end of the page,
1081 * causing the flash device to go into busy mode, so we need
1082 * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
1084 ret
= DoC_WaitReady(this);
1091 static int doc_write_oob_nolock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
,
1092 size_t * retlen
, const u_char
* buf
)
1094 struct DiskOnChip
*this = mtd
->priv
;
1096 void __iomem
*docptr
= this->virtadr
;
1097 struct Nand
*mychip
= &this->chips
[ofs
>> this->chipshift
];
1101 // printk("doc_write_oob(%lx, %d): %2.2X %2.2X %2.2X %2.2X ... %2.2X %2.2X .. %2.2X %2.2X\n",(long)ofs, len,
1102 // buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]);
1104 /* Find the chip which is to be used and select it */
1105 if (this->curfloor
!= mychip
->floor
) {
1106 DoC_SelectFloor(this, mychip
->floor
);
1107 DoC_SelectChip(this, mychip
->chip
);
1108 } else if (this->curchip
!= mychip
->chip
) {
1109 DoC_SelectChip(this, mychip
->chip
);
1111 this->curfloor
= mychip
->floor
;
1112 this->curchip
= mychip
->chip
;
1114 /* disable the ECC engine */
1115 WriteDOC (DOC_ECC_RESET
, docptr
, ECCConf
);
1116 WriteDOC (DOC_ECC_DIS
, docptr
, ECCConf
);
1118 /* Reset the chip, see Software Requirement 11.4 item 1. */
1119 DoC_Command(this, NAND_CMD_RESET
, CDSN_CTRL_WP
);
1121 /* issue the Read2 command to set the pointer to the Spare Data Area. */
1122 DoC_Command(this, NAND_CMD_READOOB
, CDSN_CTRL_WP
);
1124 /* update address for 2M x 8bit devices. OOB starts on the second */
1125 /* page to maintain compatibility with doc_read_ecc. */
1126 if (this->page256
) {
1133 /* issue the Serial Data In command to initial the Page Program process */
1134 DoC_Command(this, NAND_CMD_SEQIN
, 0);
1135 DoC_Address(this, ADDR_COLUMN_PAGE
, ofs
, 0, 0);
1137 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1138 /* Note: datasheet says it should automaticaly wrap to the */
1139 /* next OOB block, but it didn't work here. mf. */
1140 if (this->page256
&& ofs
+ len
> (ofs
| 0x7) + 1) {
1141 len256
= (ofs
| 0x7) + 1 - ofs
;
1142 DoC_WriteBuf(this, buf
, len256
);
1144 DoC_Command(this, NAND_CMD_PAGEPROG
, 0);
1145 DoC_Command(this, NAND_CMD_STATUS
, 0);
1146 /* DoC_WaitReady() is implicit in DoC_Command */
1148 if (DoC_is_Millennium(this)) {
1149 ReadDOC(docptr
, ReadPipeInit
);
1150 status
= ReadDOC(docptr
, LastDataRead
);
1152 dummy
= ReadDOC(docptr
, CDSNSlowIO
);
1154 status
= ReadDOC_(docptr
, this->ioreg
);
1158 printk(KERN_ERR
"Error programming oob data\n");
1159 /* There was an error */
1163 DoC_Command(this, NAND_CMD_SEQIN
, 0);
1164 DoC_Address(this, ADDR_COLUMN_PAGE
, ofs
& (~0x1ff), 0, 0);
1167 DoC_WriteBuf(this, &buf
[len256
], len
- len256
);
1169 DoC_Command(this, NAND_CMD_PAGEPROG
, 0);
1170 DoC_Command(this, NAND_CMD_STATUS
, 0);
1171 /* DoC_WaitReady() is implicit in DoC_Command */
1173 if (DoC_is_Millennium(this)) {
1174 ReadDOC(docptr
, ReadPipeInit
);
1175 status
= ReadDOC(docptr
, LastDataRead
);
1177 dummy
= ReadDOC(docptr
, CDSNSlowIO
);
1179 status
= ReadDOC_(docptr
, this->ioreg
);
1183 printk(KERN_ERR
"Error programming oob data\n");
1184 /* There was an error */
1194 static int doc_write_oob(struct mtd_info
*mtd
, loff_t ofs
, size_t len
,
1195 size_t * retlen
, const u_char
* buf
)
1197 struct DiskOnChip
*this = mtd
->priv
;
1201 ret
= doc_write_oob_nolock(mtd
, ofs
, len
, retlen
, buf
);
1207 static int doc_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
1209 struct DiskOnChip
*this = mtd
->priv
;
1210 __u32 ofs
= instr
->addr
;
1211 __u32 len
= instr
->len
;
1213 void __iomem
*docptr
= this->virtadr
;
1214 struct Nand
*mychip
;
1219 if (ofs
& (mtd
->erasesize
-1) || len
& (mtd
->erasesize
-1)) {
1224 instr
->state
= MTD_ERASING
;
1226 /* FIXME: Do this in the background. Use timers or schedule_task() */
1228 mychip
= &this->chips
[ofs
>> this->chipshift
];
1230 if (this->curfloor
!= mychip
->floor
) {
1231 DoC_SelectFloor(this, mychip
->floor
);
1232 DoC_SelectChip(this, mychip
->chip
);
1233 } else if (this->curchip
!= mychip
->chip
) {
1234 DoC_SelectChip(this, mychip
->chip
);
1236 this->curfloor
= mychip
->floor
;
1237 this->curchip
= mychip
->chip
;
1239 DoC_Command(this, NAND_CMD_ERASE1
, 0);
1240 DoC_Address(this, ADDR_PAGE
, ofs
, 0, 0);
1241 DoC_Command(this, NAND_CMD_ERASE2
, 0);
1243 DoC_Command(this, NAND_CMD_STATUS
, CDSN_CTRL_WP
);
1245 if (DoC_is_Millennium(this)) {
1246 ReadDOC(docptr
, ReadPipeInit
);
1247 status
= ReadDOC(docptr
, LastDataRead
);
1249 dummy
= ReadDOC(docptr
, CDSNSlowIO
);
1251 status
= ReadDOC_(docptr
, this->ioreg
);
1255 printk(KERN_ERR
"Error erasing at 0x%x\n", ofs
);
1256 /* There was an error */
1257 instr
->state
= MTD_ERASE_FAILED
;
1260 ofs
+= mtd
->erasesize
;
1261 len
-= mtd
->erasesize
;
1263 instr
->state
= MTD_ERASE_DONE
;
1266 mtd_erase_callback(instr
);
1273 /****************************************************************************
1277 ****************************************************************************/
1279 static int __init
init_doc2000(void)
1281 inter_module_register(im_name
, THIS_MODULE
, &DoC2k_init
);
1285 static void __exit
cleanup_doc2000(void)
1287 struct mtd_info
*mtd
;
1288 struct DiskOnChip
*this;
1290 while ((mtd
= doc2klist
)) {
1292 doc2klist
= this->nextdoc
;
1294 del_mtd_device(mtd
);
1296 iounmap(this->virtadr
);
1300 inter_module_unregister(im_name
);
1303 module_exit(cleanup_doc2000
);
1304 module_init(init_doc2000
);
1306 MODULE_LICENSE("GPL");
1307 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
1308 MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium");