blob: 3785733650cbf473008c676eef5df74b78031728 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2/*
3 * Linux driver for Disk-On-Chip Millennium
4 * (c) 1999 Machine Vision Holdings, Inc.
5 * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <asm/errno.h>
11#include <asm/io.h>
12#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/delay.h>
14#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
16#include <linux/types.h>
17#include <linux/bitops.h>
18
19#include <linux/mtd/mtd.h>
20#include <linux/mtd/nand.h>
21#include <linux/mtd/doc2000.h>
22
23/* #define ECC_DEBUG */
24
25/* I have no idea why some DoC chips can not use memcop_form|to_io().
26 * This may be due to the different revisions of the ASIC controller built-in or
27 * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
28 * this:*/
29#undef USE_MEMCPY
30
31static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
32 size_t *retlen, u_char *buf);
33static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
34 size_t *retlen, const u_char *buf);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020035static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
36 struct mtd_oob_ops *ops);
37static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
38 struct mtd_oob_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
40
41static struct mtd_info *docmillist = NULL;
42
43/* Perform the required delay cycles by reading from the NOP register */
44static void DoC_Delay(void __iomem * docptr, unsigned short cycles)
45{
46 volatile char dummy;
47 int i;
48
49 for (i = 0; i < cycles; i++)
50 dummy = ReadDOC(docptr, NOP);
51}
52
53/* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
54static int _DoC_WaitReady(void __iomem * docptr)
55{
56 unsigned short c = 0xffff;
57
Brian Norris289c0522011-07-19 10:06:09 -070058 pr_debug("_DoC_WaitReady called for out-of-line wait\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 /* Out-of-line routine to wait for chip response */
61 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B) && --c)
62 ;
63
64 if (c == 0)
Brian Norris289c0522011-07-19 10:06:09 -070065 pr_debug("_DoC_WaitReady timed out.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 return (c == 0);
68}
69
70static inline int DoC_WaitReady(void __iomem * docptr)
71{
72 /* This is inline, to optimise the common case, where it's ready instantly */
73 int ret = 0;
74
75 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
76 see Software Requirement 11.4 item 2. */
77 DoC_Delay(docptr, 4);
78
79 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
80 /* Call the out-of-line routine to wait */
81 ret = _DoC_WaitReady(docptr);
82
83 /* issue 2 read from NOP register after reading from CDSNControl register
84 see Software Requirement 11.4 item 2. */
85 DoC_Delay(docptr, 2);
86
87 return ret;
88}
89
90/* DoC_Command: Send a flash command to the flash chip through the CDSN IO register
91 with the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
92 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
93
Arjan van de Ven858119e2006-01-14 13:20:43 -080094static void DoC_Command(void __iomem * docptr, unsigned char command,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 unsigned char xtraflags)
96{
97 /* Assert the CLE (Command Latch Enable) line to the flash chip */
98 WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
99 DoC_Delay(docptr, 4);
100
101 /* Send the command */
102 WriteDOC(command, docptr, Mil_CDSN_IO);
103 WriteDOC(0x00, docptr, WritePipeTerm);
104
105 /* Lower the CLE line */
106 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
107 DoC_Delay(docptr, 4);
108}
109
110/* DoC_Address: Set the current address for the flash chip through the CDSN IO register
111 with the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
112 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
113
114static inline void DoC_Address(void __iomem * docptr, int numbytes, unsigned long ofs,
115 unsigned char xtraflags1, unsigned char xtraflags2)
116{
117 /* Assert the ALE (Address Latch Enable) line to the flash chip */
118 WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
119 DoC_Delay(docptr, 4);
120
121 /* Send the address */
122 switch (numbytes)
123 {
124 case 1:
125 /* Send single byte, bits 0-7. */
126 WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
127 WriteDOC(0x00, docptr, WritePipeTerm);
128 break;
129 case 2:
130 /* Send bits 9-16 followed by 17-23 */
131 WriteDOC((ofs >> 9) & 0xff, docptr, Mil_CDSN_IO);
132 WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
133 WriteDOC(0x00, docptr, WritePipeTerm);
134 break;
135 case 3:
136 /* Send 0-7, 9-16, then 17-23 */
137 WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
138 WriteDOC((ofs >> 9) & 0xff, docptr, Mil_CDSN_IO);
139 WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
140 WriteDOC(0x00, docptr, WritePipeTerm);
141 break;
142 default:
143 return;
144 }
145
146 /* Lower the ALE line */
147 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr, CDSNControl);
148 DoC_Delay(docptr, 4);
149}
150
151/* DoC_SelectChip: Select a given flash chip within the current floor */
152static int DoC_SelectChip(void __iomem * docptr, int chip)
153{
154 /* Select the individual flash chip requested */
155 WriteDOC(chip, docptr, CDSNDeviceSelect);
156 DoC_Delay(docptr, 4);
157
158 /* Wait for it to be ready */
159 return DoC_WaitReady(docptr);
160}
161
162/* DoC_SelectFloor: Select a given floor (bank of flash chips) */
163static int DoC_SelectFloor(void __iomem * docptr, int floor)
164{
165 /* Select the floor (bank) of chips required */
166 WriteDOC(floor, docptr, FloorSelect);
167
168 /* Wait for the chip to be ready */
169 return DoC_WaitReady(docptr);
170}
171
172/* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
173static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
174{
175 int mfr, id, i, j;
176 volatile char dummy;
177
178 /* Page in the required floor/chip
179 FIXME: is this supported by Millennium ?? */
180 DoC_SelectFloor(doc->virtadr, floor);
181 DoC_SelectChip(doc->virtadr, chip);
182
183 /* Reset the chip, see Software Requirement 11.4 item 1. */
184 DoC_Command(doc->virtadr, NAND_CMD_RESET, CDSN_CTRL_WP);
185 DoC_WaitReady(doc->virtadr);
186
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000187 /* Read the NAND chip ID: 1. Send ReadID command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 DoC_Command(doc->virtadr, NAND_CMD_READID, CDSN_CTRL_WP);
189
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000190 /* Read the NAND chip ID: 2. Send address byte zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 DoC_Address(doc->virtadr, 1, 0x00, CDSN_CTRL_WP, 0x00);
192
193 /* Read the manufacturer and device id codes of the flash device through
194 CDSN IO register see Software Requirement 11.4 item 5.*/
195 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
196 DoC_Delay(doc->virtadr, 2);
197 mfr = ReadDOC(doc->virtadr, Mil_CDSN_IO);
198
199 DoC_Delay(doc->virtadr, 2);
200 id = ReadDOC(doc->virtadr, Mil_CDSN_IO);
201 dummy = ReadDOC(doc->virtadr, LastDataRead);
202
203 /* No response - return failure */
204 if (mfr == 0xff || mfr == 0)
205 return 0;
206
207 /* FIXME: to deal with multi-flash on multi-Millennium case more carefully */
208 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
209 if ( id == nand_flash_ids[i].id) {
210 /* Try to identify manufacturer */
211 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
212 if (nand_manuf_ids[j].id == mfr)
213 break;
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 printk(KERN_INFO "Flash chip found: Manufacturer ID: %2.2X, "
216 "Chip ID: %2.2X (%s:%s)\n",
217 mfr, id, nand_manuf_ids[j].name, nand_flash_ids[i].name);
218 doc->mfr = mfr;
219 doc->id = id;
220 doc->chipshift = ffs((nand_flash_ids[i].chipsize << 20)) - 1;
221 break;
222 }
223 }
224
225 if (nand_flash_ids[i].name == NULL)
226 return 0;
227 else
228 return 1;
229}
230
231/* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
232static void DoC_ScanChips(struct DiskOnChip *this)
233{
234 int floor, chip;
235 int numchips[MAX_FLOORS_MIL];
236 int ret;
237
238 this->numchips = 0;
239 this->mfr = 0;
240 this->id = 0;
241
242 /* For each floor, find the number of valid chips it contains */
243 for (floor = 0,ret = 1; floor < MAX_FLOORS_MIL; floor++) {
244 numchips[floor] = 0;
245 for (chip = 0; chip < MAX_CHIPS_MIL && ret != 0; chip++) {
246 ret = DoC_IdentChip(this, floor, chip);
247 if (ret) {
248 numchips[floor]++;
249 this->numchips++;
250 }
251 }
252 }
253 /* If there are none at all that we recognise, bail */
254 if (!this->numchips) {
255 printk("No flash chips recognised.\n");
256 return;
257 }
258
259 /* Allocate an array to hold the information for each chip */
260 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
261 if (!this->chips){
262 printk("No memory for allocating chip info structures\n");
263 return;
264 }
265
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000266 /* Fill out the chip array with {floor, chipno} for each
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 * detected chip in the device. */
268 for (floor = 0, ret = 0; floor < MAX_FLOORS_MIL; floor++) {
269 for (chip = 0 ; chip < numchips[floor] ; chip++) {
270 this->chips[ret].floor = floor;
271 this->chips[ret].chip = chip;
272 this->chips[ret].curadr = 0;
273 this->chips[ret].curmode = 0x50;
274 ret++;
275 }
276 }
277
278 /* Calculate and print the total size of the device */
279 this->totlen = this->numchips * (1 << this->chipshift);
280 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
281 this->numchips ,this->totlen >> 20);
282}
283
284static int DoCMil_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
285{
286 int tmp1, tmp2, retval;
287
288 if (doc1->physadr == doc2->physadr)
289 return 1;
290
291 /* Use the alias resolution register which was set aside for this
292 * purpose. If it's value is the same on both chips, they might
293 * be the same chip, and we write to one and check for a change in
294 * the other. It's unclear if this register is usuable in the
295 * DoC 2000 (it's in the Millenium docs), but it seems to work. */
296 tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
297 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
298 if (tmp1 != tmp2)
299 return 0;
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 WriteDOC((tmp1+1) % 0xff, doc1->virtadr, AliasResolution);
302 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
303 if (tmp2 == (tmp1+1) % 0xff)
304 retval = 1;
305 else
306 retval = 0;
307
308 /* Restore register contents. May not be necessary, but do it just to
309 * be safe. */
310 WriteDOC(tmp1, doc1->virtadr, AliasResolution);
311
312 return retval;
313}
314
David Woodhouse5e535422006-05-08 14:05:05 +0100315/* This routine is found from the docprobe code by symbol_get(),
316 * which will bump the use count of this module. */
317void DoCMil_init(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 struct DiskOnChip *this = mtd->priv;
320 struct DiskOnChip *old = NULL;
321
322 /* We must avoid being called twice for the same device. */
323 if (docmillist)
324 old = docmillist->priv;
325
326 while (old) {
327 if (DoCMil_is_alias(this, old)) {
328 printk(KERN_NOTICE "Ignoring DiskOnChip Millennium at "
329 "0x%lX - already configured\n", this->physadr);
330 iounmap(this->virtadr);
331 kfree(mtd);
332 return;
333 }
334 if (old->nextdoc)
335 old = old->nextdoc->priv;
336 else
337 old = NULL;
338 }
339
340 mtd->name = "DiskOnChip Millennium";
341 printk(KERN_NOTICE "DiskOnChip Millennium found at address 0x%lX\n",
342 this->physadr);
343
344 mtd->type = MTD_NANDFLASH;
345 mtd->flags = MTD_CAP_NANDFLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 /* FIXME: erase size is not always 8KiB */
348 mtd->erasesize = 0x2000;
Joern Engel28318772006-05-22 23:18:05 +0200349 mtd->writesize = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 mtd->oobsize = 16;
351 mtd->owner = THIS_MODULE;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200352 mtd->_erase = doc_erase;
353 mtd->_read = doc_read;
354 mtd->_write = doc_write;
355 mtd->_read_oob = doc_read_oob;
356 mtd->_write_oob = doc_write_oob;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 this->curfloor = -1;
358 this->curchip = -1;
359
360 /* Ident all the chips present. */
361 DoC_ScanChips(this);
362
363 if (!this->totlen) {
364 kfree(mtd);
365 iounmap(this->virtadr);
366 } else {
367 this->nextdoc = docmillist;
368 docmillist = mtd;
369 mtd->size = this->totlen;
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100370 mtd_device_register(mtd, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return;
372 }
373}
David Woodhousef0ad11d2006-05-12 11:40:13 +0100374EXPORT_SYMBOL_GPL(DoCMil_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376static int doc_read (struct mtd_info *mtd, loff_t from, size_t len,
377 size_t *retlen, u_char *buf)
378{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 int i, ret;
380 volatile char dummy;
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200381 unsigned char syndrome[6], eccbuf[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 struct DiskOnChip *this = mtd->priv;
383 void __iomem *docptr = this->virtadr;
384 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
385
386 /* Don't allow read past end of device */
387 if (from >= this->totlen)
388 return -EINVAL;
389
390 /* Don't allow a single read to cross a 512-byte block boundary */
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000391 if (from + len > ((from | 0x1ff) + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 len = ((from | 0x1ff) + 1) - from;
393
394 /* Find the chip which is to be used and select it */
395 if (this->curfloor != mychip->floor) {
396 DoC_SelectFloor(docptr, mychip->floor);
397 DoC_SelectChip(docptr, mychip->chip);
398 } else if (this->curchip != mychip->chip) {
399 DoC_SelectChip(docptr, mychip->chip);
400 }
401 this->curfloor = mychip->floor;
402 this->curchip = mychip->chip;
403
404 /* issue the Read0 or Read1 command depend on which half of the page
405 we are accessing. Polling the Flash Ready bit after issue 3 bytes
406 address in Sequence Read Mode, see Software Requirement 11.4 item 1.*/
407 DoC_Command(docptr, (from >> 8) & 1, CDSN_CTRL_WP);
408 DoC_Address(docptr, 3, from, CDSN_CTRL_WP, 0x00);
409 DoC_WaitReady(docptr);
410
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200411 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
412 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
413 WriteDOC (DOC_ECC_EN, docptr, ECCConf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 /* Read the data via the internal pipeline through CDSN IO register,
416 see Pipelined Read Operations 11.3 */
417 dummy = ReadDOC(docptr, ReadPipeInit);
418#ifndef USE_MEMCPY
419 for (i = 0; i < len-1; i++) {
420 /* N.B. you have to increase the source address in this way or the
421 ECC logic will not work properly */
422 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
423 }
424#else
425 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
426#endif
427 buf[len - 1] = ReadDOC(docptr, LastDataRead);
428
429 /* Let the caller know we completed it */
430 *retlen = len;
431 ret = 0;
432
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200433 /* Read the ECC data from Spare Data Area,
434 see Reed-Solomon EDC/ECC 11.1 */
435 dummy = ReadDOC(docptr, ReadPipeInit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436#ifndef USE_MEMCPY
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200437 for (i = 0; i < 5; i++) {
438 /* N.B. you have to increase the source address in this way or the
439 ECC logic will not work properly */
440 eccbuf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442#else
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200443 memcpy_fromio(eccbuf, docptr + DoC_Mil_CDSN_IO, 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444#endif
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200445 eccbuf[5] = ReadDOC(docptr, LastDataRead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200447 /* Flush the pipeline */
448 dummy = ReadDOC(docptr, ECCConf);
449 dummy = ReadDOC(docptr, ECCConf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200451 /* Check the ECC Status */
452 if (ReadDOC(docptr, ECCConf) & 0x80) {
453 int nb_errors;
454 /* There was an ECC error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455#ifdef ECC_DEBUG
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200456 printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457#endif
Brian Norris7854d3f2011-06-23 14:12:08 -0700458 /* Read the ECC syndrome through the DiskOnChip ECC logic.
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200459 These syndrome will be all ZERO when there is no error */
460 for (i = 0; i < 6; i++) {
461 syndrome[i] = ReadDOC(docptr, ECCSyndrome0 + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200463 nb_errors = doc_decode_ecc(buf, syndrome);
464#ifdef ECC_DEBUG
465 printk("ECC Errors corrected: %x\n", nb_errors);
466#endif
467 if (nb_errors < 0) {
468 /* We return error, but have actually done the read. Not that
469 this can be told to user-space, via sys_read(), but at least
470 MTD-aware stuff can know about it by checking *retlen */
471 ret = -EIO;
472 }
473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475#ifdef PSYCHO_DEBUG
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200476 printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
477 (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
478 eccbuf[4], eccbuf[5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479#endif
480
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200481 /* disable the ECC engine */
482 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 return ret;
485}
486
487static int doc_write (struct mtd_info *mtd, loff_t to, size_t len,
488 size_t *retlen, const u_char *buf)
489{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 int i,ret = 0;
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200491 char eccbuf[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 volatile char dummy;
493 struct DiskOnChip *this = mtd->priv;
494 void __iomem *docptr = this->virtadr;
495 struct Nand *mychip = &this->chips[to >> (this->chipshift)];
496
497 /* Don't allow write past end of device */
498 if (to >= this->totlen)
499 return -EINVAL;
500
501#if 0
502 /* Don't allow a single write to cross a 512-byte block boundary */
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000503 if (to + len > ( (to | 0x1ff) + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 len = ((to | 0x1ff) + 1) - to;
505#else
506 /* Don't allow writes which aren't exactly one block */
507 if (to & 0x1ff || len != 0x200)
508 return -EINVAL;
509#endif
510
511 /* Find the chip which is to be used and select it */
512 if (this->curfloor != mychip->floor) {
513 DoC_SelectFloor(docptr, mychip->floor);
514 DoC_SelectChip(docptr, mychip->chip);
515 } else if (this->curchip != mychip->chip) {
516 DoC_SelectChip(docptr, mychip->chip);
517 }
518 this->curfloor = mychip->floor;
519 this->curchip = mychip->chip;
520
521 /* Reset the chip, see Software Requirement 11.4 item 1. */
522 DoC_Command(docptr, NAND_CMD_RESET, 0x00);
523 DoC_WaitReady(docptr);
524 /* Set device to main plane of flash */
525 DoC_Command(docptr, NAND_CMD_READ0, 0x00);
526
527 /* issue the Serial Data In command to initial the Page Program process */
528 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
529 DoC_Address(docptr, 3, to, 0x00, 0x00);
530 DoC_WaitReady(docptr);
531
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200532 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
533 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
534 WriteDOC (DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 /* Write the data via the internal pipeline through CDSN IO register,
537 see Pipelined Write Operations 11.2 */
538#ifndef USE_MEMCPY
539 for (i = 0; i < len; i++) {
540 /* N.B. you have to increase the source address in this way or the
541 ECC logic will not work properly */
542 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
543 }
544#else
545 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
546#endif
547 WriteDOC(0x00, docptr, WritePipeTerm);
548
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200549 /* Write ECC data to flash, the ECC info is generated by the DiskOnChip ECC logic
550 see Reed-Solomon EDC/ECC 11.1 */
551 WriteDOC(0, docptr, NOP);
552 WriteDOC(0, docptr, NOP);
553 WriteDOC(0, docptr, NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200555 /* Read the ECC data through the DiskOnChip ECC logic */
556 for (i = 0; i < 6; i++) {
557 eccbuf[i] = ReadDOC(docptr, ECCSyndrome0 + i);
558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200560 /* ignore the ECC engine */
561 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563#ifndef USE_MEMCPY
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200564 /* Write the ECC data to flash */
565 for (i = 0; i < 6; i++) {
566 /* N.B. you have to increase the source address in this way or the
567 ECC logic will not work properly */
568 WriteDOC(eccbuf[i], docptr, Mil_CDSN_IO + i);
569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570#else
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200571 memcpy_toio(docptr + DoC_Mil_CDSN_IO, eccbuf, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572#endif
573
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200574 /* write the block status BLOCK_USED (0x5555) at the end of ECC data
575 FIXME: this is only a hack for programming the IPL area for LinuxBIOS
576 and should be replace with proper codes in user space utilities */
577 WriteDOC(0x55, docptr, Mil_CDSN_IO);
578 WriteDOC(0x55, docptr, Mil_CDSN_IO + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200580 WriteDOC(0x00, docptr, WritePipeTerm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582#ifdef PSYCHO_DEBUG
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200583 printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
584 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
585 eccbuf[4], eccbuf[5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 /* Commit the Page Program command and wait for ready
589 see Software Requirement 11.4 item 1.*/
590 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
591 DoC_WaitReady(docptr);
592
593 /* Read the status of the flash device through CDSN IO register
594 see Software Requirement 11.4 item 5.*/
595 DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
596 dummy = ReadDOC(docptr, ReadPipeInit);
597 DoC_Delay(docptr, 2);
598 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
599 printk("Error programming flash\n");
600 /* Error in programming
601 FIXME: implement Bad Block Replacement (in nftl.c ??) */
602 *retlen = 0;
603 ret = -EIO;
604 }
605 dummy = ReadDOC(docptr, LastDataRead);
606
607 /* Let the caller know we completed it */
608 *retlen = len;
609
610 return ret;
611}
612
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200613static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
614 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616#ifndef USE_MEMCPY
617 int i;
618#endif
619 volatile char dummy;
620 struct DiskOnChip *this = mtd->priv;
621 void __iomem *docptr = this->virtadr;
622 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200623 uint8_t *buf = ops->oobbuf;
624 size_t len = ops->len;
625
Brian Norris0612b9d2011-08-30 18:45:40 -0700626 BUG_ON(ops->mode != MTD_OPS_PLACE_OOB);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200627
628 ofs += ops->ooboffs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 /* Find the chip which is to be used and select it */
631 if (this->curfloor != mychip->floor) {
632 DoC_SelectFloor(docptr, mychip->floor);
633 DoC_SelectChip(docptr, mychip->chip);
634 } else if (this->curchip != mychip->chip) {
635 DoC_SelectChip(docptr, mychip->chip);
636 }
637 this->curfloor = mychip->floor;
638 this->curchip = mychip->chip;
639
640 /* disable the ECC engine */
641 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
642 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
643
644 /* issue the Read2 command to set the pointer to the Spare Data Area.
645 Polling the Flash Ready bit after issue 3 bytes address in
646 Sequence Read Mode, see Software Requirement 11.4 item 1.*/
647 DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
648 DoC_Address(docptr, 3, ofs, CDSN_CTRL_WP, 0x00);
649 DoC_WaitReady(docptr);
650
651 /* Read the data out via the internal pipeline through CDSN IO register,
652 see Pipelined Read Operations 11.3 */
653 dummy = ReadDOC(docptr, ReadPipeInit);
654#ifndef USE_MEMCPY
655 for (i = 0; i < len-1; i++) {
656 /* N.B. you have to increase the source address in this way or the
657 ECC logic will not work properly */
658 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
659 }
660#else
661 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
662#endif
663 buf[len - 1] = ReadDOC(docptr, LastDataRead);
664
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200665 ops->retlen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 return 0;
668}
669
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200670static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
671 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673#ifndef USE_MEMCPY
674 int i;
675#endif
676 volatile char dummy;
677 int ret = 0;
678 struct DiskOnChip *this = mtd->priv;
679 void __iomem *docptr = this->virtadr;
680 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200681 uint8_t *buf = ops->oobbuf;
682 size_t len = ops->len;
683
Brian Norris0612b9d2011-08-30 18:45:40 -0700684 BUG_ON(ops->mode != MTD_OPS_PLACE_OOB);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200685
686 ofs += ops->ooboffs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 /* Find the chip which is to be used and select it */
689 if (this->curfloor != mychip->floor) {
690 DoC_SelectFloor(docptr, mychip->floor);
691 DoC_SelectChip(docptr, mychip->chip);
692 } else if (this->curchip != mychip->chip) {
693 DoC_SelectChip(docptr, mychip->chip);
694 }
695 this->curfloor = mychip->floor;
696 this->curchip = mychip->chip;
697
698 /* disable the ECC engine */
699 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
700 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
701
702 /* Reset the chip, see Software Requirement 11.4 item 1. */
703 DoC_Command(docptr, NAND_CMD_RESET, CDSN_CTRL_WP);
704 DoC_WaitReady(docptr);
705 /* issue the Read2 command to set the pointer to the Spare Data Area. */
706 DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
707
708 /* issue the Serial Data In command to initial the Page Program process */
709 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
710 DoC_Address(docptr, 3, ofs, 0x00, 0x00);
711
712 /* Write the data via the internal pipeline through CDSN IO register,
713 see Pipelined Write Operations 11.2 */
714#ifndef USE_MEMCPY
715 for (i = 0; i < len; i++) {
716 /* N.B. you have to increase the source address in this way or the
717 ECC logic will not work properly */
718 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
719 }
720#else
721 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
722#endif
723 WriteDOC(0x00, docptr, WritePipeTerm);
724
725 /* Commit the Page Program command and wait for ready
726 see Software Requirement 11.4 item 1.*/
727 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
728 DoC_WaitReady(docptr);
729
730 /* Read the status of the flash device through CDSN IO register
731 see Software Requirement 11.4 item 5.*/
732 DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
733 dummy = ReadDOC(docptr, ReadPipeInit);
734 DoC_Delay(docptr, 2);
735 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
736 printk("Error programming oob data\n");
737 /* FIXME: implement Bad Block Replacement (in nftl.c ??) */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200738 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 ret = -EIO;
740 }
741 dummy = ReadDOC(docptr, LastDataRead);
742
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200743 ops->retlen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 return ret;
746}
747
748int doc_erase (struct mtd_info *mtd, struct erase_info *instr)
749{
750 volatile char dummy;
751 struct DiskOnChip *this = mtd->priv;
752 __u32 ofs = instr->addr;
753 __u32 len = instr->len;
754 void __iomem *docptr = this->virtadr;
755 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
756
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000757 if (len != mtd->erasesize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 printk(KERN_WARNING "Erase not right size (%x != %x)n",
759 len, mtd->erasesize);
760
761 /* Find the chip which is to be used and select it */
762 if (this->curfloor != mychip->floor) {
763 DoC_SelectFloor(docptr, mychip->floor);
764 DoC_SelectChip(docptr, mychip->chip);
765 } else if (this->curchip != mychip->chip) {
766 DoC_SelectChip(docptr, mychip->chip);
767 }
768 this->curfloor = mychip->floor;
769 this->curchip = mychip->chip;
770
771 instr->state = MTD_ERASE_PENDING;
772
773 /* issue the Erase Setup command */
774 DoC_Command(docptr, NAND_CMD_ERASE1, 0x00);
775 DoC_Address(docptr, 2, ofs, 0x00, 0x00);
776
777 /* Commit the Erase Start command and wait for ready
778 see Software Requirement 11.4 item 1.*/
779 DoC_Command(docptr, NAND_CMD_ERASE2, 0x00);
780 DoC_WaitReady(docptr);
781
782 instr->state = MTD_ERASING;
783
784 /* Read the status of the flash device through CDSN IO register
785 see Software Requirement 11.4 item 5.
786 FIXME: it seems that we are not wait long enough, some blocks are not
787 erased fully */
788 DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
789 dummy = ReadDOC(docptr, ReadPipeInit);
790 DoC_Delay(docptr, 2);
791 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
792 printk("Error Erasing at 0x%x\n", ofs);
793 /* There was an error
794 FIXME: implement Bad Block Replacement (in nftl.c ??) */
795 instr->state = MTD_ERASE_FAILED;
796 } else
797 instr->state = MTD_ERASE_DONE;
798 dummy = ReadDOC(docptr, LastDataRead);
799
800 mtd_erase_callback(instr);
801
802 return 0;
803}
804
805/****************************************************************************
806 *
807 * Module stuff
808 *
809 ****************************************************************************/
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811static void __exit cleanup_doc2001(void)
812{
813 struct mtd_info *mtd;
814 struct DiskOnChip *this;
815
816 while ((mtd=docmillist)) {
817 this = mtd->priv;
818 docmillist = this->nextdoc;
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000819
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100820 mtd_device_unregister(mtd);
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 iounmap(this->virtadr);
823 kfree(this->chips);
824 kfree(mtd);
825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
828module_exit(cleanup_doc2001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830MODULE_LICENSE("GPL");
831MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
832MODULE_DESCRIPTION("Alternative driver for DiskOnChip Millennium");