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.62 2004/08/09 14:04:02 dwmw2 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 unsigned long 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 unsigned long docptr
= doc
->virtadr
;
118 /* This is inline, to optimise the common case, where it's ready instantly */
121 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
122 see Software Requirement 11.4 item 2. */
125 if (!(ReadDOC(docptr
, CDSNControl
) & CDSN_CTRL_FR_B
))
126 /* Call the out-of-line routine to wait */
127 ret
= _DoC_WaitReady(doc
);
129 /* issue 2 read from NOP register after reading from CDSNControl register
130 see Software Requirement 11.4 item 2. */
136 /* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to
137 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
138 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
140 static inline int DoC_Command(struct DiskOnChip
*doc
, unsigned char command
,
141 unsigned char xtraflags
)
143 unsigned long docptr
= doc
->virtadr
;
145 if (DoC_is_2000(doc
))
146 xtraflags
|= CDSN_CTRL_FLASH_IO
;
148 /* Assert the CLE (Command Latch Enable) line to the flash chip */
149 WriteDOC(xtraflags
| CDSN_CTRL_CLE
| CDSN_CTRL_CE
, docptr
, CDSNControl
);
150 DoC_Delay(doc
, 4); /* Software requirement 11.4.3 for Millennium */
152 if (DoC_is_Millennium(doc
))
153 WriteDOC(command
, docptr
, CDSNSlowIO
);
155 /* Send the command */
156 WriteDOC_(command
, docptr
, doc
->ioreg
);
157 if (DoC_is_Millennium(doc
))
158 WriteDOC(command
, docptr
, WritePipeTerm
);
160 /* Lower the CLE line */
161 WriteDOC(xtraflags
| CDSN_CTRL_CE
, docptr
, CDSNControl
);
162 DoC_Delay(doc
, 4); /* Software requirement 11.4.3 for Millennium */
164 /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */
165 return DoC_WaitReady(doc
);
168 /* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
169 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
170 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
172 static int DoC_Address(struct DiskOnChip
*doc
, int numbytes
, unsigned long ofs
,
173 unsigned char xtraflags1
, unsigned char xtraflags2
)
175 unsigned long docptr
;
178 docptr
= doc
->virtadr
;
180 if (DoC_is_2000(doc
))
181 xtraflags1
|= CDSN_CTRL_FLASH_IO
;
183 /* Assert the ALE (Address Latch Enable) line to the flash chip */
184 WriteDOC(xtraflags1
| CDSN_CTRL_ALE
| CDSN_CTRL_CE
, docptr
, CDSNControl
);
186 DoC_Delay(doc
, 4); /* Software requirement 11.4.3 for Millennium */
188 /* Send the address */
189 /* Devices with 256-byte page are addressed as:
190 Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
191 * there is no device on the market with page256
192 and more than 24 bits.
193 Devices with 512-byte page are addressed as:
194 Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
195 * 25-31 is sent only if the chip support it.
196 * bit 8 changes the read command to be sent
197 (NAND_CMD_READ0 or NAND_CMD_READ1).
200 if (numbytes
== ADDR_COLUMN
|| numbytes
== ADDR_COLUMN_PAGE
) {
201 if (DoC_is_Millennium(doc
))
202 WriteDOC(ofs
& 0xff, docptr
, CDSNSlowIO
);
203 WriteDOC_(ofs
& 0xff, docptr
, doc
->ioreg
);
212 if (numbytes
== ADDR_PAGE
|| numbytes
== ADDR_COLUMN_PAGE
) {
213 for (i
= 0; i
< doc
->pageadrlen
; i
++, ofs
= ofs
>> 8) {
214 if (DoC_is_Millennium(doc
))
215 WriteDOC(ofs
& 0xff, docptr
, CDSNSlowIO
);
216 WriteDOC_(ofs
& 0xff, docptr
, doc
->ioreg
);
220 if (DoC_is_Millennium(doc
))
221 WriteDOC(ofs
& 0xff, docptr
, WritePipeTerm
);
223 DoC_Delay(doc
, 2); /* Needed for some slow flash chips. mf. */
225 /* FIXME: The SlowIO's for millennium could be replaced by
226 a single WritePipeTerm here. mf. */
228 /* Lower the ALE line */
229 WriteDOC(xtraflags1
| xtraflags2
| CDSN_CTRL_CE
, docptr
,
232 DoC_Delay(doc
, 4); /* Software requirement 11.4.3 for Millennium */
234 /* Wait for the chip to respond - Software requirement 11.4.1 */
235 return DoC_WaitReady(doc
);
238 /* Read a buffer from DoC, taking care of Millennium odditys */
239 static void DoC_ReadBuf(struct DiskOnChip
*doc
, u_char
* buf
, int len
)
242 int modulus
= 0xffff;
243 unsigned long docptr
;
246 docptr
= doc
->virtadr
;
251 if (DoC_is_Millennium(doc
)) {
252 /* Read the data via the internal pipeline through CDSN IO register,
253 see Pipelined Read Operations 11.3 */
254 dummy
= ReadDOC(docptr
, ReadPipeInit
);
256 /* Millennium should use the LastDataRead register - Pipeline Reads */
259 /* This is needed for correctly ECC calculation */
263 for (i
= 0; i
< len
; i
++)
264 buf
[i
] = ReadDOC_(docptr
, doc
->ioreg
+ (i
& modulus
));
266 if (DoC_is_Millennium(doc
)) {
267 buf
[i
] = ReadDOC(docptr
, LastDataRead
);
271 /* Write a buffer to DoC, taking care of Millennium odditys */
272 static void DoC_WriteBuf(struct DiskOnChip
*doc
, const u_char
* buf
, int len
)
274 unsigned long docptr
;
277 docptr
= doc
->virtadr
;
282 for (i
= 0; i
< len
; i
++)
283 WriteDOC_(buf
[i
], docptr
, doc
->ioreg
+ i
);
285 if (DoC_is_Millennium(doc
)) {
286 WriteDOC(0x00, docptr
, WritePipeTerm
);
291 /* DoC_SelectChip: Select a given flash chip within the current floor */
293 static inline int DoC_SelectChip(struct DiskOnChip
*doc
, int chip
)
295 unsigned long docptr
= doc
->virtadr
;
297 /* Software requirement 11.4.4 before writing DeviceSelect */
298 /* Deassert the CE line to eliminate glitches on the FCE# outputs */
299 WriteDOC(CDSN_CTRL_WP
, docptr
, CDSNControl
);
300 DoC_Delay(doc
, 4); /* Software requirement 11.4.3 for Millennium */
302 /* Select the individual flash chip requested */
303 WriteDOC(chip
, docptr
, CDSNDeviceSelect
);
306 /* Reassert the CE line */
307 WriteDOC(CDSN_CTRL_CE
| CDSN_CTRL_FLASH_IO
| CDSN_CTRL_WP
, docptr
,
309 DoC_Delay(doc
, 4); /* Software requirement 11.4.3 for Millennium */
311 /* Wait for it to be ready */
312 return DoC_WaitReady(doc
);
315 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
317 static inline int DoC_SelectFloor(struct DiskOnChip
*doc
, int floor
)
319 unsigned long docptr
= doc
->virtadr
;
321 /* Select the floor (bank) of chips required */
322 WriteDOC(floor
, docptr
, FloorSelect
);
324 /* Wait for the chip to be ready */
325 return DoC_WaitReady(doc
);
328 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
330 static int DoC_IdentChip(struct DiskOnChip
*doc
, int floor
, int chip
)
335 /* Page in the required floor/chip */
336 DoC_SelectFloor(doc
, floor
);
337 DoC_SelectChip(doc
, chip
);
340 if (DoC_Command(doc
, NAND_CMD_RESET
, CDSN_CTRL_WP
)) {
341 DEBUG(MTD_DEBUG_LEVEL2
,
342 "DoC_Command (reset) for %d,%d returned true\n",
348 /* Read the NAND chip ID: 1. Send ReadID command */
349 if (DoC_Command(doc
, NAND_CMD_READID
, CDSN_CTRL_WP
)) {
350 DEBUG(MTD_DEBUG_LEVEL2
,
351 "DoC_Command (ReadID) for %d,%d returned true\n",
356 /* Read the NAND chip ID: 2. Send address byte zero */
357 DoC_Address(doc
, ADDR_COLUMN
, 0, CDSN_CTRL_WP
, 0);
359 /* Read the manufacturer and device id codes from the device */
361 if (DoC_is_Millennium(doc
)) {
363 dummy
= ReadDOC(doc
->virtadr
, ReadPipeInit
);
364 mfr
= ReadDOC(doc
->virtadr
, LastDataRead
);
367 dummy
= ReadDOC(doc
->virtadr
, ReadPipeInit
);
368 id
= ReadDOC(doc
->virtadr
, LastDataRead
);
370 /* CDSN Slow IO register see Software Req 11.4 item 5. */
371 dummy
= ReadDOC(doc
->virtadr
, CDSNSlowIO
);
373 mfr
= ReadDOC_(doc
->virtadr
, doc
->ioreg
);
375 /* CDSN Slow IO register see Software Req 11.4 item 5. */
376 dummy
= ReadDOC(doc
->virtadr
, CDSNSlowIO
);
378 id
= ReadDOC_(doc
->virtadr
, doc
->ioreg
);
381 /* No response - return failure */
382 if (mfr
== 0xff || mfr
== 0)
385 /* Check it's the same as the first chip we identified.
386 * M-Systems say that any given DiskOnChip device should only
387 * contain _one_ type of flash part, although that's not a
388 * hardware restriction. */
390 if (doc
->mfr
== mfr
&& doc
->id
== id
)
391 return 1; /* This is another the same the first */
394 "Flash chip at floor %d, chip %d is different:\n",
398 /* Print and store the manufacturer and ID codes. */
399 for (i
= 0; nand_flash_ids
[i
].name
!= NULL
; i
++) {
400 if (id
== nand_flash_ids
[i
].id
) {
401 /* Try to identify manufacturer */
402 for (j
= 0; nand_manuf_ids
[j
].id
!= 0x0; j
++) {
403 if (nand_manuf_ids
[j
].id
== mfr
)
407 "Flash chip found: Manufacturer ID: %2.2X, "
408 "Chip ID: %2.2X (%s:%s)\n", mfr
, id
,
409 nand_manuf_ids
[j
].name
, nand_flash_ids
[i
].name
);
414 ffs((nand_flash_ids
[i
].chipsize
<< 20)) - 1;
415 doc
->page256
= (nand_flash_ids
[i
].pagesize
== 256) ? 1 : 0;
416 doc
->pageadrlen
= doc
->chipshift
> 25 ? 3 : 2;
418 nand_flash_ids
[i
].erasesize
;
426 /* We haven't fully identified the chip. Print as much as we know. */
427 printk(KERN_WARNING
"Unknown flash chip found: %2.2X %2.2X\n",
430 printk(KERN_WARNING
"Please report to dwmw2@infradead.org\n");
434 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
436 static void DoC_ScanChips(struct DiskOnChip
*this, int maxchips
)
439 int numchips
[MAX_FLOORS
];
446 /* For each floor, find the number of valid chips it contains */
447 for (floor
= 0; floor
< MAX_FLOORS
; floor
++) {
450 for (chip
= 0; chip
< maxchips
&& ret
!= 0; chip
++) {
452 ret
= DoC_IdentChip(this, floor
, chip
);
460 /* If there are none at all that we recognise, bail */
461 if (!this->numchips
) {
462 printk(KERN_NOTICE
"No flash chips recognised.\n");
466 /* Allocate an array to hold the information for each chip */
467 this->chips
= kmalloc(sizeof(struct Nand
) * this->numchips
, GFP_KERNEL
);
469 printk(KERN_NOTICE
"No memory for allocating chip info structures\n");
475 /* Fill out the chip array with {floor, chipno} for each
476 * detected chip in the device. */
477 for (floor
= 0; floor
< MAX_FLOORS
; floor
++) {
478 for (chip
= 0; chip
< numchips
[floor
]; chip
++) {
479 this->chips
[ret
].floor
= floor
;
480 this->chips
[ret
].chip
= chip
;
481 this->chips
[ret
].curadr
= 0;
482 this->chips
[ret
].curmode
= 0x50;
487 /* Calculate and print the total size of the device */
488 this->totlen
= this->numchips
* (1 << this->chipshift
);
490 printk(KERN_INFO
"%d flash chips found. Total DiskOnChip size: %ld MiB\n",
491 this->numchips
, this->totlen
>> 20);
494 static int DoC2k_is_alias(struct DiskOnChip
*doc1
, struct DiskOnChip
*doc2
)
496 int tmp1
, tmp2
, retval
;
497 if (doc1
->physadr
== doc2
->physadr
)
500 /* Use the alias resolution register which was set aside for this
501 * purpose. If it's value is the same on both chips, they might
502 * be the same chip, and we write to one and check for a change in
503 * the other. It's unclear if this register is usuable in the
504 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
505 tmp1
= ReadDOC(doc1
->virtadr
, AliasResolution
);
506 tmp2
= ReadDOC(doc2
->virtadr
, AliasResolution
);
510 WriteDOC((tmp1
+ 1) % 0xff, doc1
->virtadr
, AliasResolution
);
511 tmp2
= ReadDOC(doc2
->virtadr
, AliasResolution
);
512 if (tmp2
== (tmp1
+ 1) % 0xff)
517 /* Restore register contents. May not be necessary, but do it just to
519 WriteDOC(tmp1
, doc1
->virtadr
, AliasResolution
);
524 static const char im_name
[] = "DoC2k_init";
526 /* This routine is made available to other mtd code via
527 * inter_module_register. It must only be accessed through
528 * inter_module_get which will bump the use count of this module. The
529 * addresses passed back in mtd are valid as long as the use count of
530 * this module is non-zero, i.e. between inter_module_get and
531 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
533 static void DoC2k_init(struct mtd_info
*mtd
)
535 struct DiskOnChip
*this = (struct DiskOnChip
*) mtd
->priv
;
536 struct DiskOnChip
*old
= NULL
;
539 /* We must avoid being called twice for the same device. */
542 old
= (struct DiskOnChip
*) doc2klist
->priv
;
545 if (DoC2k_is_alias(old
, this)) {
547 "Ignoring DiskOnChip 2000 at 0x%lX - already configured\n",
549 iounmap((void *) this->virtadr
);
554 old
= (struct DiskOnChip
*) old
->nextdoc
->priv
;
560 switch (this->ChipID
) {
561 case DOC_ChipID_Doc2kTSOP
:
562 mtd
->name
= "DiskOnChip 2000 TSOP";
563 this->ioreg
= DoC_Mil_CDSN_IO
;
564 /* Pretend it's a Millennium */
565 this->ChipID
= DOC_ChipID_DocMil
;
566 maxchips
= MAX_CHIPS
;
568 case DOC_ChipID_Doc2k
:
569 mtd
->name
= "DiskOnChip 2000";
570 this->ioreg
= DoC_2k_CDSN_IO
;
571 maxchips
= MAX_CHIPS
;
573 case DOC_ChipID_DocMil
:
574 mtd
->name
= "DiskOnChip Millennium";
575 this->ioreg
= DoC_Mil_CDSN_IO
;
576 maxchips
= MAX_CHIPS_MIL
;
579 printk("Unknown ChipID 0x%02x\n", this->ChipID
);
581 iounmap((void *) this->virtadr
);
585 printk(KERN_NOTICE
"%s found at address 0x%lX\n", mtd
->name
,
588 mtd
->type
= MTD_NANDFLASH
;
589 mtd
->flags
= MTD_CAP_NANDFLASH
;
590 mtd
->ecctype
= MTD_ECC_RS_DiskOnChip
;
595 mtd
->owner
= THIS_MODULE
;
596 mtd
->erase
= doc_erase
;
599 mtd
->read
= doc_read
;
600 mtd
->write
= doc_write
;
601 mtd
->read_ecc
= doc_read_ecc
;
602 mtd
->write_ecc
= doc_write_ecc
;
603 mtd
->writev_ecc
= doc_writev_ecc
;
604 mtd
->read_oob
= doc_read_oob
;
605 mtd
->write_oob
= doc_write_oob
;
613 init_MUTEX(&this->lock
);
615 /* Ident all the chips present. */
616 DoC_ScanChips(this, maxchips
);
620 iounmap((void *) this->virtadr
);
622 this->nextdoc
= doc2klist
;
624 mtd
->size
= this->totlen
;
625 mtd
->erasesize
= this->erasesize
;
631 static int doc_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
632 size_t * retlen
, u_char
* buf
)
634 /* Just a special case of doc_read_ecc */
635 return doc_read_ecc(mtd
, from
, len
, retlen
, buf
, NULL
, NULL
);
638 static int doc_read_ecc(struct mtd_info
*mtd
, loff_t from
, size_t len
,
639 size_t * retlen
, u_char
* buf
, u_char
* eccbuf
, struct nand_oobinfo
*oobsel
)
641 struct DiskOnChip
*this = (struct DiskOnChip
*) mtd
->priv
;
642 unsigned long docptr
;
644 unsigned char syndrome
[6];
646 int i
, len256
= 0, ret
=0;
649 docptr
= this->virtadr
;
651 /* Don't allow read past end of device */
652 if (from
>= this->totlen
)
661 /* Don't allow a single read to cross a 512-byte block boundary */
662 if (from
+ len
> ((from
| 0x1ff) + 1))
663 len
= ((from
| 0x1ff) + 1) - from
;
665 /* The ECC will not be calculated correctly if less than 512 is read */
666 if (len
!= 0x200 && eccbuf
)
668 "ECC needs a full sector read (adr: %lx size %lx)\n",
669 (long) from
, (long) len
);
671 /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */
674 /* Find the chip which is to be used and select it */
675 mychip
= &this->chips
[from
>> (this->chipshift
)];
677 if (this->curfloor
!= mychip
->floor
) {
678 DoC_SelectFloor(this, mychip
->floor
);
679 DoC_SelectChip(this, mychip
->chip
);
680 } else if (this->curchip
!= mychip
->chip
) {
681 DoC_SelectChip(this, mychip
->chip
);
684 this->curfloor
= mychip
->floor
;
685 this->curchip
= mychip
->chip
;
689 && (from
& 0x100)) ? NAND_CMD_READ1
: NAND_CMD_READ0
,
691 DoC_Address(this, ADDR_COLUMN_PAGE
, from
, CDSN_CTRL_WP
,
695 /* Prime the ECC engine */
696 WriteDOC(DOC_ECC_RESET
, docptr
, ECCConf
);
697 WriteDOC(DOC_ECC_EN
, docptr
, ECCConf
);
699 /* disable the ECC engine */
700 WriteDOC(DOC_ECC_RESET
, docptr
, ECCConf
);
701 WriteDOC(DOC_ECC_DIS
, docptr
, ECCConf
);
704 /* treat crossing 256-byte sector for 2M x 8bits devices */
705 if (this->page256
&& from
+ len
> (from
| 0xff) + 1) {
706 len256
= (from
| 0xff) + 1 - from
;
707 DoC_ReadBuf(this, buf
, len256
);
709 DoC_Command(this, NAND_CMD_READ0
, CDSN_CTRL_WP
);
710 DoC_Address(this, ADDR_COLUMN_PAGE
, from
+ len256
,
711 CDSN_CTRL_WP
, CDSN_CTRL_ECC_IO
);
714 DoC_ReadBuf(this, &buf
[len256
], len
- len256
);
716 /* Let the caller know we completed it */
720 /* Read the ECC data through the DiskOnChip ECC logic */
721 /* Note: this will work even with 2M x 8bit devices as */
722 /* they have 8 bytes of OOB per 256 page. mf. */
723 DoC_ReadBuf(this, eccbuf
, 6);
725 /* Flush the pipeline */
726 if (DoC_is_Millennium(this)) {
727 dummy
= ReadDOC(docptr
, ECCConf
);
728 dummy
= ReadDOC(docptr
, ECCConf
);
729 i
= ReadDOC(docptr
, ECCConf
);
731 dummy
= ReadDOC(docptr
, 2k_ECCStatus
);
732 dummy
= ReadDOC(docptr
, 2k_ECCStatus
);
733 i
= ReadDOC(docptr
, 2k_ECCStatus
);
736 /* Check the ECC Status */
739 /* There was an ECC error */
741 printk(KERN_ERR
"DiskOnChip ECC Error: Read at %lx\n", (long)from
);
743 /* Read the ECC syndrom through the DiskOnChip ECC logic.
744 These syndrome will be all ZERO when there is no error */
745 for (i
= 0; i
< 6; i
++) {
747 ReadDOC(docptr
, ECCSyndrome0
+ i
);
749 nb_errors
= doc_decode_ecc(buf
, syndrome
);
752 printk(KERN_ERR
"Errors corrected: %x\n", nb_errors
);
755 /* We return error, but have actually done the read. Not that
756 this can be told to user-space, via sys_read(), but at least
757 MTD-aware stuff can know about it by checking *retlen */
763 printk(KERN_DEBUG
"ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
764 (long)from
, eccbuf
[0], eccbuf
[1], eccbuf
[2],
765 eccbuf
[3], eccbuf
[4], eccbuf
[5]);
768 /* disable the ECC engine */
769 WriteDOC(DOC_ECC_DIS
, docptr
, ECCConf
);
772 /* according to 11.4.1, we need to wait for the busy line
773 * drop if we read to the end of the page. */
774 if(0 == ((from
+ len
) & 0x1ff))
789 static int doc_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
790 size_t * retlen
, const u_char
* buf
)
793 return doc_write_ecc(mtd
, to
, len
, retlen
, buf
, eccbuf
, NULL
);
796 static int doc_write_ecc(struct mtd_info
*mtd
, loff_t to
, size_t len
,
797 size_t * retlen
, const u_char
* buf
,
798 u_char
* eccbuf
, struct nand_oobinfo
*oobsel
)
800 struct DiskOnChip
*this = (struct DiskOnChip
*) mtd
->priv
;
801 int di
; /* Yes, DI is a hangover from when I was disassembling the binary driver */
802 unsigned long docptr
;
809 docptr
= this->virtadr
;
811 /* Don't allow write past end of device */
812 if (to
>= this->totlen
)
821 /* Don't allow a single write to cross a 512-byte block boundary */
822 if (to
+ len
> ((to
| 0x1ff) + 1))
823 len
= ((to
| 0x1ff) + 1) - to
;
825 /* The ECC will not be calculated correctly if less than 512 is written */
827 if (len != 0x200 && eccbuf)
829 "ECC needs a full sector write (adr: %lx size %lx)\n",
830 (long) to, (long) len);
833 /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
835 /* Find the chip which is to be used and select it */
836 mychip
= &this->chips
[to
>> (this->chipshift
)];
838 if (this->curfloor
!= mychip
->floor
) {
839 DoC_SelectFloor(this, mychip
->floor
);
840 DoC_SelectChip(this, mychip
->chip
);
841 } else if (this->curchip
!= mychip
->chip
) {
842 DoC_SelectChip(this, mychip
->chip
);
845 this->curfloor
= mychip
->floor
;
846 this->curchip
= mychip
->chip
;
848 /* Set device to main plane of flash */
849 DoC_Command(this, NAND_CMD_RESET
, CDSN_CTRL_WP
);
852 && (to
& 0x100)) ? NAND_CMD_READ1
: NAND_CMD_READ0
,
855 DoC_Command(this, NAND_CMD_SEQIN
, 0);
856 DoC_Address(this, ADDR_COLUMN_PAGE
, to
, 0, CDSN_CTRL_ECC_IO
);
859 /* Prime the ECC engine */
860 WriteDOC(DOC_ECC_RESET
, docptr
, ECCConf
);
861 WriteDOC(DOC_ECC_EN
| DOC_ECC_RW
, docptr
, ECCConf
);
863 /* disable the ECC engine */
864 WriteDOC(DOC_ECC_RESET
, docptr
, ECCConf
);
865 WriteDOC(DOC_ECC_DIS
, docptr
, ECCConf
);
868 /* treat crossing 256-byte sector for 2M x 8bits devices */
869 if (this->page256
&& to
+ len
> (to
| 0xff) + 1) {
870 len256
= (to
| 0xff) + 1 - to
;
871 DoC_WriteBuf(this, buf
, len256
);
873 DoC_Command(this, NAND_CMD_PAGEPROG
, 0);
875 DoC_Command(this, NAND_CMD_STATUS
, CDSN_CTRL_WP
);
876 /* There's an implicit DoC_WaitReady() in DoC_Command */
878 dummy
= ReadDOC(docptr
, CDSNSlowIO
);
881 if (ReadDOC_(docptr
, this->ioreg
) & 1) {
882 printk(KERN_ERR
"Error programming flash\n");
883 /* Error in programming */
889 DoC_Command(this, NAND_CMD_SEQIN
, 0);
890 DoC_Address(this, ADDR_COLUMN_PAGE
, to
+ len256
, 0,
894 DoC_WriteBuf(this, &buf
[len256
], len
- len256
);
897 WriteDOC(CDSN_CTRL_ECC_IO
| CDSN_CTRL_CE
, docptr
,
900 if (DoC_is_Millennium(this)) {
901 WriteDOC(0, docptr
, NOP
);
902 WriteDOC(0, docptr
, NOP
);
903 WriteDOC(0, docptr
, NOP
);
905 WriteDOC_(0, docptr
, this->ioreg
);
906 WriteDOC_(0, docptr
, this->ioreg
);
907 WriteDOC_(0, docptr
, this->ioreg
);
910 WriteDOC(CDSN_CTRL_ECC_IO
| CDSN_CTRL_FLASH_IO
| CDSN_CTRL_CE
, docptr
,
913 /* Read the ECC data through the DiskOnChip ECC logic */
914 for (di
= 0; di
< 6; di
++) {
915 eccbuf
[di
] = ReadDOC(docptr
, ECCSyndrome0
+ di
);
918 /* Reset the ECC engine */
919 WriteDOC(DOC_ECC_DIS
, docptr
, ECCConf
);
923 ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
924 (long) to
, eccbuf
[0], eccbuf
[1], eccbuf
[2], eccbuf
[3],
925 eccbuf
[4], eccbuf
[5]);
929 DoC_Command(this, NAND_CMD_PAGEPROG
, 0);
931 DoC_Command(this, NAND_CMD_STATUS
, CDSN_CTRL_WP
);
932 /* There's an implicit DoC_WaitReady() in DoC_Command */
934 if (DoC_is_Millennium(this)) {
935 ReadDOC(docptr
, ReadPipeInit
);
936 status
= ReadDOC(docptr
, LastDataRead
);
938 dummy
= ReadDOC(docptr
, CDSNSlowIO
);
940 status
= ReadDOC_(docptr
, this->ioreg
);
944 printk(KERN_ERR
"Error programming flash\n");
945 /* Error in programming */
951 /* Let the caller know we completed it */
959 /* Write the ECC data to flash */
960 for (di
=0; di
<6; di
++)
966 ret
= doc_write_oob_nolock(mtd
, to
, 8, &dummy
, x
);
982 static int doc_writev_ecc(struct mtd_info
*mtd
, const struct kvec
*vecs
,
983 unsigned long count
, loff_t to
, size_t *retlen
,
984 u_char
*eccbuf
, struct nand_oobinfo
*oobsel
)
986 static char static_buf
[512];
987 static DECLARE_MUTEX(writev_buf_sem
);
989 size_t totretlen
= 0;
990 size_t thisvecofs
= 0;
993 down(&writev_buf_sem
);
996 size_t thislen
, thisretlen
;
999 buf
= vecs
->iov_base
+ thisvecofs
;
1000 thislen
= vecs
->iov_len
- thisvecofs
;
1003 if (thislen
>= 512) {
1004 thislen
= thislen
& ~(512-1);
1005 thisvecofs
+= thislen
;
1007 /* Not enough to fill a page. Copy into buf */
1008 memcpy(static_buf
, buf
, thislen
);
1009 buf
= &static_buf
[thislen
];
1011 while(count
&& thislen
< 512) {
1014 thisvecofs
= min((512-thislen
), vecs
->iov_len
);
1015 memcpy(buf
, vecs
->iov_base
, thisvecofs
);
1016 thislen
+= thisvecofs
;
1021 if (count
&& thisvecofs
== vecs
->iov_len
) {
1026 ret
= doc_write_ecc(mtd
, to
, thislen
, &thisretlen
, buf
, eccbuf
, oobsel
);
1028 totretlen
+= thisretlen
;
1030 if (ret
|| thisretlen
!= thislen
)
1036 up(&writev_buf_sem
);
1037 *retlen
= totretlen
;
1042 static int doc_read_oob(struct mtd_info
*mtd
, loff_t ofs
, size_t len
,
1043 size_t * retlen
, u_char
* buf
)
1045 struct DiskOnChip
*this = (struct DiskOnChip
*) mtd
->priv
;
1046 int len256
= 0, ret
;
1047 unsigned long docptr
;
1048 struct Nand
*mychip
;
1052 docptr
= this->virtadr
;
1054 mychip
= &this->chips
[ofs
>> this->chipshift
];
1056 if (this->curfloor
!= mychip
->floor
) {
1057 DoC_SelectFloor(this, mychip
->floor
);
1058 DoC_SelectChip(this, mychip
->chip
);
1059 } else if (this->curchip
!= mychip
->chip
) {
1060 DoC_SelectChip(this, mychip
->chip
);
1062 this->curfloor
= mychip
->floor
;
1063 this->curchip
= mychip
->chip
;
1065 /* update address for 2M x 8bit devices. OOB starts on the second */
1066 /* page to maintain compatibility with doc_read_ecc. */
1067 if (this->page256
) {
1074 DoC_Command(this, NAND_CMD_READOOB
, CDSN_CTRL_WP
);
1075 DoC_Address(this, ADDR_COLUMN_PAGE
, ofs
, CDSN_CTRL_WP
, 0);
1077 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1078 /* Note: datasheet says it should automaticaly wrap to the */
1079 /* next OOB block, but it didn't work here. mf. */
1080 if (this->page256
&& ofs
+ len
> (ofs
| 0x7) + 1) {
1081 len256
= (ofs
| 0x7) + 1 - ofs
;
1082 DoC_ReadBuf(this, buf
, len256
);
1084 DoC_Command(this, NAND_CMD_READOOB
, CDSN_CTRL_WP
);
1085 DoC_Address(this, ADDR_COLUMN_PAGE
, ofs
& (~0x1ff),
1089 DoC_ReadBuf(this, &buf
[len256
], len
- len256
);
1092 /* Reading the full OOB data drops us off of the end of the page,
1093 * causing the flash device to go into busy mode, so we need
1094 * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
1096 ret
= DoC_WaitReady(this);
1103 static int doc_write_oob_nolock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
,
1104 size_t * retlen
, const u_char
* buf
)
1106 struct DiskOnChip
*this = (struct DiskOnChip
*) mtd
->priv
;
1108 unsigned long docptr
= this->virtadr
;
1109 struct Nand
*mychip
= &this->chips
[ofs
>> this->chipshift
];
1113 // 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,
1114 // buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]);
1116 /* Find the chip which is to be used and select it */
1117 if (this->curfloor
!= mychip
->floor
) {
1118 DoC_SelectFloor(this, mychip
->floor
);
1119 DoC_SelectChip(this, mychip
->chip
);
1120 } else if (this->curchip
!= mychip
->chip
) {
1121 DoC_SelectChip(this, mychip
->chip
);
1123 this->curfloor
= mychip
->floor
;
1124 this->curchip
= mychip
->chip
;
1126 /* disable the ECC engine */
1127 WriteDOC (DOC_ECC_RESET
, docptr
, ECCConf
);
1128 WriteDOC (DOC_ECC_DIS
, docptr
, ECCConf
);
1130 /* Reset the chip, see Software Requirement 11.4 item 1. */
1131 DoC_Command(this, NAND_CMD_RESET
, CDSN_CTRL_WP
);
1133 /* issue the Read2 command to set the pointer to the Spare Data Area. */
1134 DoC_Command(this, NAND_CMD_READOOB
, CDSN_CTRL_WP
);
1136 /* update address for 2M x 8bit devices. OOB starts on the second */
1137 /* page to maintain compatibility with doc_read_ecc. */
1138 if (this->page256
) {
1145 /* issue the Serial Data In command to initial the Page Program process */
1146 DoC_Command(this, NAND_CMD_SEQIN
, 0);
1147 DoC_Address(this, ADDR_COLUMN_PAGE
, ofs
, 0, 0);
1149 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1150 /* Note: datasheet says it should automaticaly wrap to the */
1151 /* next OOB block, but it didn't work here. mf. */
1152 if (this->page256
&& ofs
+ len
> (ofs
| 0x7) + 1) {
1153 len256
= (ofs
| 0x7) + 1 - ofs
;
1154 DoC_WriteBuf(this, buf
, len256
);
1156 DoC_Command(this, NAND_CMD_PAGEPROG
, 0);
1157 DoC_Command(this, NAND_CMD_STATUS
, 0);
1158 /* DoC_WaitReady() is implicit in DoC_Command */
1160 if (DoC_is_Millennium(this)) {
1161 ReadDOC(docptr
, ReadPipeInit
);
1162 status
= ReadDOC(docptr
, LastDataRead
);
1164 dummy
= ReadDOC(docptr
, CDSNSlowIO
);
1166 status
= ReadDOC_(docptr
, this->ioreg
);
1170 printk(KERN_ERR
"Error programming oob data\n");
1171 /* There was an error */
1175 DoC_Command(this, NAND_CMD_SEQIN
, 0);
1176 DoC_Address(this, ADDR_COLUMN_PAGE
, ofs
& (~0x1ff), 0, 0);
1179 DoC_WriteBuf(this, &buf
[len256
], len
- len256
);
1181 DoC_Command(this, NAND_CMD_PAGEPROG
, 0);
1182 DoC_Command(this, NAND_CMD_STATUS
, 0);
1183 /* DoC_WaitReady() is implicit in DoC_Command */
1185 if (DoC_is_Millennium(this)) {
1186 ReadDOC(docptr
, ReadPipeInit
);
1187 status
= ReadDOC(docptr
, LastDataRead
);
1189 dummy
= ReadDOC(docptr
, CDSNSlowIO
);
1191 status
= ReadDOC_(docptr
, this->ioreg
);
1195 printk(KERN_ERR
"Error programming oob data\n");
1196 /* There was an error */
1206 static int doc_write_oob(struct mtd_info
*mtd
, loff_t ofs
, size_t len
,
1207 size_t * retlen
, const u_char
* buf
)
1209 struct DiskOnChip
*this = (struct DiskOnChip
*) mtd
->priv
;
1213 ret
= doc_write_oob_nolock(mtd
, ofs
, len
, retlen
, buf
);
1219 static int doc_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
1221 struct DiskOnChip
*this = (struct DiskOnChip
*) mtd
->priv
;
1222 __u32 ofs
= instr
->addr
;
1223 __u32 len
= instr
->len
;
1225 unsigned long docptr
;
1226 struct Nand
*mychip
;
1231 if (ofs
& (mtd
->erasesize
-1) || len
& (mtd
->erasesize
-1)) {
1236 instr
->state
= MTD_ERASING
;
1238 docptr
= this->virtadr
;
1240 /* FIXME: Do this in the background. Use timers or schedule_task() */
1242 mychip
= &this->chips
[ofs
>> this->chipshift
];
1244 if (this->curfloor
!= mychip
->floor
) {
1245 DoC_SelectFloor(this, mychip
->floor
);
1246 DoC_SelectChip(this, mychip
->chip
);
1247 } else if (this->curchip
!= mychip
->chip
) {
1248 DoC_SelectChip(this, mychip
->chip
);
1250 this->curfloor
= mychip
->floor
;
1251 this->curchip
= mychip
->chip
;
1253 DoC_Command(this, NAND_CMD_ERASE1
, 0);
1254 DoC_Address(this, ADDR_PAGE
, ofs
, 0, 0);
1255 DoC_Command(this, NAND_CMD_ERASE2
, 0);
1257 DoC_Command(this, NAND_CMD_STATUS
, CDSN_CTRL_WP
);
1259 if (DoC_is_Millennium(this)) {
1260 ReadDOC(docptr
, ReadPipeInit
);
1261 status
= ReadDOC(docptr
, LastDataRead
);
1263 dummy
= ReadDOC(docptr
, CDSNSlowIO
);
1265 status
= ReadDOC_(docptr
, this->ioreg
);
1269 printk(KERN_ERR
"Error erasing at 0x%x\n", ofs
);
1270 /* There was an error */
1271 instr
->state
= MTD_ERASE_FAILED
;
1274 ofs
+= mtd
->erasesize
;
1275 len
-= mtd
->erasesize
;
1277 instr
->state
= MTD_ERASE_DONE
;
1280 mtd_erase_callback(instr
);
1287 /****************************************************************************
1291 ****************************************************************************/
1293 int __init
init_doc2000(void)
1295 inter_module_register(im_name
, THIS_MODULE
, &DoC2k_init
);
1299 static void __exit
cleanup_doc2000(void)
1301 struct mtd_info
*mtd
;
1302 struct DiskOnChip
*this;
1304 while ((mtd
= doc2klist
)) {
1305 this = (struct DiskOnChip
*) mtd
->priv
;
1306 doc2klist
= this->nextdoc
;
1308 del_mtd_device(mtd
);
1310 iounmap((void *) this->virtadr
);
1314 inter_module_unregister(im_name
);
1317 module_exit(cleanup_doc2000
);
1318 module_init(init_doc2000
);
1320 MODULE_LICENSE("GPL");
1321 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
1322 MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium");