forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/mtd/nand/raw/diskonchip.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * (C) 2003 Red Hat, Inc.
34 * (C) 2004 Dan Brown <dan_brown@ieee.org>
....@@ -57,6 +58,7 @@
5758 static struct mtd_info *doclist = NULL;
5859
5960 struct doc_priv {
61
+ struct nand_controller base;
6062 void __iomem *virtadr;
6163 unsigned long physadr;
6264 u_char ChipID;
....@@ -68,6 +70,7 @@
6870 int mh1_page;
6971 struct rs_control *rs_decoder;
7072 struct mtd_info *nextdoc;
73
+ bool supports_32b_reads;
7174
7275 /* Handle the last stage of initialization (BBT scan, partitioning) */
7376 int (*late_init)(struct mtd_info *mtd);
....@@ -82,10 +85,6 @@
8285 #define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
8386 #define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
8487 #define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
85
-
86
-static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
87
- unsigned int bitmask);
88
-static void doc200x_select_chip(struct mtd_info *mtd, int chip);
8988
9089 static int debug = 0;
9190 module_param(debug, int, 0);
....@@ -290,9 +289,8 @@
290289 return ret;
291290 }
292291
293
-static void doc2000_write_byte(struct mtd_info *mtd, u_char datum)
292
+static void doc2000_write_byte(struct nand_chip *this, u_char datum)
294293 {
295
- struct nand_chip *this = mtd_to_nand(mtd);
296294 struct doc_priv *doc = nand_get_controller_data(this);
297295 void __iomem *docptr = doc->virtadr;
298296
....@@ -302,24 +300,9 @@
302300 WriteDOC(datum, docptr, 2k_CDSN_IO);
303301 }
304302
305
-static u_char doc2000_read_byte(struct mtd_info *mtd)
303
+static void doc2000_writebuf(struct nand_chip *this, const u_char *buf,
304
+ int len)
306305 {
307
- struct nand_chip *this = mtd_to_nand(mtd);
308
- struct doc_priv *doc = nand_get_controller_data(this);
309
- void __iomem *docptr = doc->virtadr;
310
- u_char ret;
311
-
312
- ReadDOC(docptr, CDSNSlowIO);
313
- DoC_Delay(doc, 2);
314
- ret = ReadDOC(docptr, 2k_CDSN_IO);
315
- if (debug)
316
- printk("read_byte returns %02x\n", ret);
317
- return ret;
318
-}
319
-
320
-static void doc2000_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
321
-{
322
- struct nand_chip *this = mtd_to_nand(mtd);
323306 struct doc_priv *doc = nand_get_controller_data(this);
324307 void __iomem *docptr = doc->virtadr;
325308 int i;
....@@ -334,40 +317,46 @@
334317 printk("\n");
335318 }
336319
337
-static void doc2000_readbuf(struct mtd_info *mtd, u_char *buf, int len)
320
+static void doc2000_readbuf(struct nand_chip *this, u_char *buf, int len)
338321 {
339
- struct nand_chip *this = mtd_to_nand(mtd);
340322 struct doc_priv *doc = nand_get_controller_data(this);
341323 void __iomem *docptr = doc->virtadr;
324
+ u32 *buf32 = (u32 *)buf;
342325 int i;
343326
344327 if (debug)
345328 printk("readbuf of %d bytes: ", len);
346329
347
- for (i = 0; i < len; i++) {
348
- buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
330
+ if (!doc->supports_32b_reads ||
331
+ ((((unsigned long)buf) | len) & 3)) {
332
+ for (i = 0; i < len; i++)
333
+ buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
334
+ } else {
335
+ for (i = 0; i < len / 4; i++)
336
+ buf32[i] = readl(docptr + DoC_2k_CDSN_IO + i);
349337 }
350338 }
351339
352
-static void doc2000_readbuf_dword(struct mtd_info *mtd, u_char *buf, int len)
340
+/*
341
+ * We need our own readid() here because it's called before the NAND chip
342
+ * has been initialized, and calling nand_op_readid() would lead to a NULL
343
+ * pointer exception when dereferencing the NAND timings.
344
+ */
345
+static void doc200x_readid(struct nand_chip *this, unsigned int cs, u8 *id)
353346 {
354
- struct nand_chip *this = mtd_to_nand(mtd);
355
- struct doc_priv *doc = nand_get_controller_data(this);
356
- void __iomem *docptr = doc->virtadr;
357
- int i;
347
+ u8 addr = 0;
348
+ struct nand_op_instr instrs[] = {
349
+ NAND_OP_CMD(NAND_CMD_READID, 0),
350
+ NAND_OP_ADDR(1, &addr, 50),
351
+ NAND_OP_8BIT_DATA_IN(2, id, 0),
352
+ };
358353
359
- if (debug)
360
- printk("readbuf_dword of %d bytes: ", len);
354
+ struct nand_operation op = NAND_OPERATION(cs, instrs);
361355
362
- if (unlikely((((unsigned long)buf) | len) & 3)) {
363
- for (i = 0; i < len; i++) {
364
- *(uint8_t *) (&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
365
- }
366
- } else {
367
- for (i = 0; i < len; i += 4) {
368
- *(uint32_t *) (&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
369
- }
370
- }
356
+ if (!id)
357
+ op.ninstrs--;
358
+
359
+ this->controller->ops->exec_op(this, &op, false);
371360 }
372361
373362 static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
....@@ -375,20 +364,11 @@
375364 struct nand_chip *this = mtd_to_nand(mtd);
376365 struct doc_priv *doc = nand_get_controller_data(this);
377366 uint16_t ret;
367
+ u8 id[2];
378368
379
- doc200x_select_chip(mtd, nr);
380
- doc200x_hwcontrol(mtd, NAND_CMD_READID,
381
- NAND_CTRL_CLE | NAND_CTRL_CHANGE);
382
- doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
383
- doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
369
+ doc200x_readid(this, nr, id);
384370
385
- /* We can't use dev_ready here, but at least we wait for the
386
- * command to complete
387
- */
388
- udelay(50);
389
-
390
- ret = this->read_byte(mtd) << 8;
391
- ret |= this->read_byte(mtd);
371
+ ret = ((u16)id[0] << 8) | id[1];
392372
393373 if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
394374 /* First chip probe. See if we get same results by 32-bit access */
....@@ -398,18 +378,12 @@
398378 } ident;
399379 void __iomem *docptr = doc->virtadr;
400380
401
- doc200x_hwcontrol(mtd, NAND_CMD_READID,
402
- NAND_CTRL_CLE | NAND_CTRL_CHANGE);
403
- doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
404
- doc200x_hwcontrol(mtd, NAND_CMD_NONE,
405
- NAND_NCE | NAND_CTRL_CHANGE);
406
-
407
- udelay(50);
381
+ doc200x_readid(this, nr, NULL);
408382
409383 ident.dword = readl(docptr + DoC_2k_CDSN_IO);
410384 if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
411385 pr_info("DiskOnChip 2000 responds to DWORD access\n");
412
- this->read_buf = &doc2000_readbuf_dword;
386
+ doc->supports_32b_reads = true;
413387 }
414388 }
415389
....@@ -438,23 +412,8 @@
438412 pr_debug("Detected %d chips per floor.\n", i);
439413 }
440414
441
-static int doc200x_wait(struct mtd_info *mtd, struct nand_chip *this)
415
+static void doc2001_write_byte(struct nand_chip *this, u_char datum)
442416 {
443
- struct doc_priv *doc = nand_get_controller_data(this);
444
-
445
- int status;
446
-
447
- DoC_WaitReady(doc);
448
- nand_status_op(this, NULL);
449
- DoC_WaitReady(doc);
450
- status = (int)this->read_byte(mtd);
451
-
452
- return status;
453
-}
454
-
455
-static void doc2001_write_byte(struct mtd_info *mtd, u_char datum)
456
-{
457
- struct nand_chip *this = mtd_to_nand(mtd);
458417 struct doc_priv *doc = nand_get_controller_data(this);
459418 void __iomem *docptr = doc->virtadr;
460419
....@@ -463,23 +422,8 @@
463422 WriteDOC(datum, docptr, WritePipeTerm);
464423 }
465424
466
-static u_char doc2001_read_byte(struct mtd_info *mtd)
425
+static void doc2001_writebuf(struct nand_chip *this, const u_char *buf, int len)
467426 {
468
- struct nand_chip *this = mtd_to_nand(mtd);
469
- struct doc_priv *doc = nand_get_controller_data(this);
470
- void __iomem *docptr = doc->virtadr;
471
-
472
- //ReadDOC(docptr, CDSNSlowIO);
473
- /* 11.4.5 -- delay twice to allow extended length cycle */
474
- DoC_Delay(doc, 2);
475
- ReadDOC(docptr, ReadPipeInit);
476
- //return ReadDOC(docptr, Mil_CDSN_IO);
477
- return ReadDOC(docptr, LastDataRead);
478
-}
479
-
480
-static void doc2001_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
481
-{
482
- struct nand_chip *this = mtd_to_nand(mtd);
483427 struct doc_priv *doc = nand_get_controller_data(this);
484428 void __iomem *docptr = doc->virtadr;
485429 int i;
....@@ -490,9 +434,8 @@
490434 WriteDOC(0x00, docptr, WritePipeTerm);
491435 }
492436
493
-static void doc2001_readbuf(struct mtd_info *mtd, u_char *buf, int len)
437
+static void doc2001_readbuf(struct nand_chip *this, u_char *buf, int len)
494438 {
495
- struct nand_chip *this = mtd_to_nand(mtd);
496439 struct doc_priv *doc = nand_get_controller_data(this);
497440 void __iomem *docptr = doc->virtadr;
498441 int i;
....@@ -507,24 +450,8 @@
507450 buf[i] = ReadDOC(docptr, LastDataRead);
508451 }
509452
510
-static u_char doc2001plus_read_byte(struct mtd_info *mtd)
453
+static void doc2001plus_writebuf(struct nand_chip *this, const u_char *buf, int len)
511454 {
512
- struct nand_chip *this = mtd_to_nand(mtd);
513
- struct doc_priv *doc = nand_get_controller_data(this);
514
- void __iomem *docptr = doc->virtadr;
515
- u_char ret;
516
-
517
- ReadDOC(docptr, Mplus_ReadPipeInit);
518
- ReadDOC(docptr, Mplus_ReadPipeInit);
519
- ret = ReadDOC(docptr, Mplus_LastDataRead);
520
- if (debug)
521
- printk("read_byte returns %02x\n", ret);
522
- return ret;
523
-}
524
-
525
-static void doc2001plus_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
526
-{
527
- struct nand_chip *this = mtd_to_nand(mtd);
528455 struct doc_priv *doc = nand_get_controller_data(this);
529456 void __iomem *docptr = doc->virtadr;
530457 int i;
....@@ -540,9 +467,8 @@
540467 printk("\n");
541468 }
542469
543
-static void doc2001plus_readbuf(struct mtd_info *mtd, u_char *buf, int len)
470
+static void doc2001plus_readbuf(struct nand_chip *this, u_char *buf, int len)
544471 {
545
- struct nand_chip *this = mtd_to_nand(mtd);
546472 struct doc_priv *doc = nand_get_controller_data(this);
547473 void __iomem *docptr = doc->virtadr;
548474 int i;
....@@ -561,9 +487,12 @@
561487 }
562488
563489 /* Terminate read pipeline */
564
- buf[len - 2] = ReadDOC(docptr, Mplus_LastDataRead);
565
- if (debug && i < 16)
566
- printk("%02x ", buf[len - 2]);
490
+ if (len >= 2) {
491
+ buf[len - 2] = ReadDOC(docptr, Mplus_LastDataRead);
492
+ if (debug && i < 16)
493
+ printk("%02x ", buf[len - 2]);
494
+ }
495
+
567496 buf[len - 1] = ReadDOC(docptr, Mplus_LastDataRead);
568497 if (debug && i < 16)
569498 printk("%02x ", buf[len - 1]);
....@@ -571,235 +500,168 @@
571500 printk("\n");
572501 }
573502
574
-static void doc2001plus_select_chip(struct mtd_info *mtd, int chip)
503
+static void doc200x_write_control(struct doc_priv *doc, u8 value)
575504 {
576
- struct nand_chip *this = mtd_to_nand(mtd);
577
- struct doc_priv *doc = nand_get_controller_data(this);
578
- void __iomem *docptr = doc->virtadr;
579
- int floor = 0;
580
-
581
- if (debug)
582
- printk("select chip (%d)\n", chip);
583
-
584
- if (chip == -1) {
585
- /* Disable flash internally */
586
- WriteDOC(0, docptr, Mplus_FlashSelect);
587
- return;
588
- }
589
-
590
- floor = chip / doc->chips_per_floor;
591
- chip -= (floor * doc->chips_per_floor);
592
-
593
- /* Assert ChipEnable and deassert WriteProtect */
594
- WriteDOC((DOC_FLASH_CE), docptr, Mplus_FlashSelect);
595
- nand_reset_op(this);
596
-
597
- doc->curchip = chip;
598
- doc->curfloor = floor;
505
+ WriteDOC(value, doc->virtadr, CDSNControl);
506
+ /* 11.4.3 -- 4 NOPs after CSDNControl write */
507
+ DoC_Delay(doc, 4);
599508 }
600509
601
-static void doc200x_select_chip(struct mtd_info *mtd, int chip)
510
+static void doc200x_exec_instr(struct nand_chip *this,
511
+ const struct nand_op_instr *instr)
602512 {
603
- struct nand_chip *this = mtd_to_nand(mtd);
604513 struct doc_priv *doc = nand_get_controller_data(this);
605
- void __iomem *docptr = doc->virtadr;
606
- int floor = 0;
514
+ unsigned int i;
607515
608
- if (debug)
609
- printk("select chip (%d)\n", chip);
516
+ switch (instr->type) {
517
+ case NAND_OP_CMD_INSTR:
518
+ doc200x_write_control(doc, CDSN_CTRL_CE | CDSN_CTRL_CLE);
519
+ doc2000_write_byte(this, instr->ctx.cmd.opcode);
520
+ break;
610521
611
- if (chip == -1)
612
- return;
522
+ case NAND_OP_ADDR_INSTR:
523
+ doc200x_write_control(doc, CDSN_CTRL_CE | CDSN_CTRL_ALE);
524
+ for (i = 0; i < instr->ctx.addr.naddrs; i++) {
525
+ u8 addr = instr->ctx.addr.addrs[i];
613526
614
- floor = chip / doc->chips_per_floor;
615
- chip -= (floor * doc->chips_per_floor);
527
+ if (DoC_is_2000(doc))
528
+ doc2000_write_byte(this, addr);
529
+ else
530
+ doc2001_write_byte(this, addr);
531
+ }
532
+ break;
616533
617
- /* 11.4.4 -- deassert CE before changing chip */
618
- doc200x_hwcontrol(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
619
-
620
- WriteDOC(floor, docptr, FloorSelect);
621
- WriteDOC(chip, docptr, CDSNDeviceSelect);
622
-
623
- doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
624
-
625
- doc->curchip = chip;
626
- doc->curfloor = floor;
627
-}
628
-
629
-#define CDSN_CTRL_MSK (CDSN_CTRL_CE | CDSN_CTRL_CLE | CDSN_CTRL_ALE)
630
-
631
-static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
632
- unsigned int ctrl)
633
-{
634
- struct nand_chip *this = mtd_to_nand(mtd);
635
- struct doc_priv *doc = nand_get_controller_data(this);
636
- void __iomem *docptr = doc->virtadr;
637
-
638
- if (ctrl & NAND_CTRL_CHANGE) {
639
- doc->CDSNControl &= ~CDSN_CTRL_MSK;
640
- doc->CDSNControl |= ctrl & CDSN_CTRL_MSK;
641
- if (debug)
642
- printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
643
- WriteDOC(doc->CDSNControl, docptr, CDSNControl);
644
- /* 11.4.3 -- 4 NOPs after CSDNControl write */
645
- DoC_Delay(doc, 4);
646
- }
647
- if (cmd != NAND_CMD_NONE) {
534
+ case NAND_OP_DATA_IN_INSTR:
535
+ doc200x_write_control(doc, CDSN_CTRL_CE);
648536 if (DoC_is_2000(doc))
649
- doc2000_write_byte(mtd, cmd);
537
+ doc2000_readbuf(this, instr->ctx.data.buf.in,
538
+ instr->ctx.data.len);
650539 else
651
- doc2001_write_byte(mtd, cmd);
540
+ doc2001_readbuf(this, instr->ctx.data.buf.in,
541
+ instr->ctx.data.len);
542
+ break;
543
+
544
+ case NAND_OP_DATA_OUT_INSTR:
545
+ doc200x_write_control(doc, CDSN_CTRL_CE);
546
+ if (DoC_is_2000(doc))
547
+ doc2000_writebuf(this, instr->ctx.data.buf.out,
548
+ instr->ctx.data.len);
549
+ else
550
+ doc2001_writebuf(this, instr->ctx.data.buf.out,
551
+ instr->ctx.data.len);
552
+ break;
553
+
554
+ case NAND_OP_WAITRDY_INSTR:
555
+ DoC_WaitReady(doc);
556
+ break;
652557 }
558
+
559
+ if (instr->delay_ns)
560
+ ndelay(instr->delay_ns);
653561 }
654562
655
-static void doc2001plus_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
563
+static int doc200x_exec_op(struct nand_chip *this,
564
+ const struct nand_operation *op,
565
+ bool check_only)
656566 {
657
- struct nand_chip *this = mtd_to_nand(mtd);
658567 struct doc_priv *doc = nand_get_controller_data(this);
659
- void __iomem *docptr = doc->virtadr;
568
+ unsigned int i;
660569
661
- /*
662
- * Must terminate write pipeline before sending any commands
663
- * to the device.
664
- */
665
- if (command == NAND_CMD_PAGEPROG) {
666
- WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
667
- WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
668
- }
570
+ if (check_only)
571
+ return true;
669572
670
- /*
671
- * Write out the command to the device.
672
- */
673
- if (command == NAND_CMD_SEQIN) {
674
- int readcmd;
573
+ doc->curchip = op->cs % doc->chips_per_floor;
574
+ doc->curfloor = op->cs / doc->chips_per_floor;
675575
676
- if (column >= mtd->writesize) {
677
- /* OOB area */
678
- column -= mtd->writesize;
679
- readcmd = NAND_CMD_READOOB;
680
- } else if (column < 256) {
681
- /* First 256 bytes --> READ0 */
682
- readcmd = NAND_CMD_READ0;
683
- } else {
684
- column -= 256;
685
- readcmd = NAND_CMD_READ1;
686
- }
687
- WriteDOC(readcmd, docptr, Mplus_FlashCmd);
688
- }
689
- WriteDOC(command, docptr, Mplus_FlashCmd);
690
- WriteDOC(0, docptr, Mplus_WritePipeTerm);
691
- WriteDOC(0, docptr, Mplus_WritePipeTerm);
576
+ WriteDOC(doc->curfloor, doc->virtadr, FloorSelect);
577
+ WriteDOC(doc->curchip, doc->virtadr, CDSNDeviceSelect);
692578
693
- if (column != -1 || page_addr != -1) {
694
- /* Serially input address */
695
- if (column != -1) {
696
- /* Adjust columns for 16 bit buswidth */
697
- if (this->options & NAND_BUSWIDTH_16 &&
698
- !nand_opcode_8bits(command))
699
- column >>= 1;
700
- WriteDOC(column, docptr, Mplus_FlashAddress);
701
- }
702
- if (page_addr != -1) {
703
- WriteDOC((unsigned char)(page_addr & 0xff), docptr, Mplus_FlashAddress);
704
- WriteDOC((unsigned char)((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
705
- if (this->options & NAND_ROW_ADDR_3) {
706
- WriteDOC((unsigned char)((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
707
- printk("high density\n");
708
- }
709
- }
710
- WriteDOC(0, docptr, Mplus_WritePipeTerm);
711
- WriteDOC(0, docptr, Mplus_WritePipeTerm);
712
- /* deassert ALE */
713
- if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
714
- command == NAND_CMD_READOOB || command == NAND_CMD_READID)
715
- WriteDOC(0, docptr, Mplus_FlashControl);
716
- }
579
+ /* Assert CE pin */
580
+ doc200x_write_control(doc, CDSN_CTRL_CE);
717581
718
- /*
719
- * program and erase have their own busy handlers
720
- * status and sequential in needs no delay
721
- */
722
- switch (command) {
582
+ for (i = 0; i < op->ninstrs; i++)
583
+ doc200x_exec_instr(this, &op->instrs[i]);
723584
724
- case NAND_CMD_PAGEPROG:
725
- case NAND_CMD_ERASE1:
726
- case NAND_CMD_ERASE2:
727
- case NAND_CMD_SEQIN:
728
- case NAND_CMD_STATUS:
729
- return;
585
+ /* De-assert CE pin */
586
+ doc200x_write_control(doc, 0);
730587
731
- case NAND_CMD_RESET:
732
- if (this->dev_ready)
733
- break;
734
- udelay(this->chip_delay);
735
- WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd);
736
- WriteDOC(0, docptr, Mplus_WritePipeTerm);
737
- WriteDOC(0, docptr, Mplus_WritePipeTerm);
738
- while (!(this->read_byte(mtd) & 0x40)) ;
739
- return;
740
-
741
- /* This applies to read commands */
742
- default:
743
- /*
744
- * If we don't have access to the busy pin, we apply the given
745
- * command delay
746
- */
747
- if (!this->dev_ready) {
748
- udelay(this->chip_delay);
749
- return;
750
- }
751
- }
752
-
753
- /* Apply this short delay always to ensure that we do wait tWB in
754
- * any case on any machine. */
755
- ndelay(100);
756
- /* wait until command is processed */
757
- while (!this->dev_ready(mtd)) ;
758
-}
759
-
760
-static int doc200x_dev_ready(struct mtd_info *mtd)
761
-{
762
- struct nand_chip *this = mtd_to_nand(mtd);
763
- struct doc_priv *doc = nand_get_controller_data(this);
764
- void __iomem *docptr = doc->virtadr;
765
-
766
- if (DoC_is_MillenniumPlus(doc)) {
767
- /* 11.4.2 -- must NOP four times before checking FR/B# */
768
- DoC_Delay(doc, 4);
769
- if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
770
- if (debug)
771
- printk("not ready\n");
772
- return 0;
773
- }
774
- if (debug)
775
- printk("was ready\n");
776
- return 1;
777
- } else {
778
- /* 11.4.2 -- must NOP four times before checking FR/B# */
779
- DoC_Delay(doc, 4);
780
- if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
781
- if (debug)
782
- printk("not ready\n");
783
- return 0;
784
- }
785
- /* 11.4.2 -- Must NOP twice if it's ready */
786
- DoC_Delay(doc, 2);
787
- if (debug)
788
- printk("was ready\n");
789
- return 1;
790
- }
791
-}
792
-
793
-static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs)
794
-{
795
- /* This is our last resort if we couldn't find or create a BBT. Just
796
- pretend all blocks are good. */
797588 return 0;
798589 }
799590
800
-static void doc200x_enable_hwecc(struct mtd_info *mtd, int mode)
591
+static void doc2001plus_write_pipe_term(struct doc_priv *doc)
801592 {
802
- struct nand_chip *this = mtd_to_nand(mtd);
593
+ WriteDOC(0x00, doc->virtadr, Mplus_WritePipeTerm);
594
+ WriteDOC(0x00, doc->virtadr, Mplus_WritePipeTerm);
595
+}
596
+
597
+static void doc2001plus_exec_instr(struct nand_chip *this,
598
+ const struct nand_op_instr *instr)
599
+{
600
+ struct doc_priv *doc = nand_get_controller_data(this);
601
+ unsigned int i;
602
+
603
+ switch (instr->type) {
604
+ case NAND_OP_CMD_INSTR:
605
+ WriteDOC(instr->ctx.cmd.opcode, doc->virtadr, Mplus_FlashCmd);
606
+ doc2001plus_write_pipe_term(doc);
607
+ break;
608
+
609
+ case NAND_OP_ADDR_INSTR:
610
+ for (i = 0; i < instr->ctx.addr.naddrs; i++) {
611
+ u8 addr = instr->ctx.addr.addrs[i];
612
+
613
+ WriteDOC(addr, doc->virtadr, Mplus_FlashAddress);
614
+ }
615
+ doc2001plus_write_pipe_term(doc);
616
+ /* deassert ALE */
617
+ WriteDOC(0, doc->virtadr, Mplus_FlashControl);
618
+ break;
619
+
620
+ case NAND_OP_DATA_IN_INSTR:
621
+ doc2001plus_readbuf(this, instr->ctx.data.buf.in,
622
+ instr->ctx.data.len);
623
+ break;
624
+ case NAND_OP_DATA_OUT_INSTR:
625
+ doc2001plus_writebuf(this, instr->ctx.data.buf.out,
626
+ instr->ctx.data.len);
627
+ doc2001plus_write_pipe_term(doc);
628
+ break;
629
+ case NAND_OP_WAITRDY_INSTR:
630
+ DoC_WaitReady(doc);
631
+ break;
632
+ }
633
+
634
+ if (instr->delay_ns)
635
+ ndelay(instr->delay_ns);
636
+}
637
+
638
+static int doc2001plus_exec_op(struct nand_chip *this,
639
+ const struct nand_operation *op,
640
+ bool check_only)
641
+{
642
+ struct doc_priv *doc = nand_get_controller_data(this);
643
+ unsigned int i;
644
+
645
+ if (check_only)
646
+ return true;
647
+
648
+ doc->curchip = op->cs % doc->chips_per_floor;
649
+ doc->curfloor = op->cs / doc->chips_per_floor;
650
+
651
+ /* Assert ChipEnable and deassert WriteProtect */
652
+ WriteDOC(DOC_FLASH_CE, doc->virtadr, Mplus_FlashSelect);
653
+
654
+ for (i = 0; i < op->ninstrs; i++)
655
+ doc2001plus_exec_instr(this, &op->instrs[i]);
656
+
657
+ /* De-assert ChipEnable */
658
+ WriteDOC(0, doc->virtadr, Mplus_FlashSelect);
659
+
660
+ return 0;
661
+}
662
+
663
+static void doc200x_enable_hwecc(struct nand_chip *this, int mode)
664
+{
803665 struct doc_priv *doc = nand_get_controller_data(this);
804666 void __iomem *docptr = doc->virtadr;
805667
....@@ -816,9 +678,8 @@
816678 }
817679 }
818680
819
-static void doc2001plus_enable_hwecc(struct mtd_info *mtd, int mode)
681
+static void doc2001plus_enable_hwecc(struct nand_chip *this, int mode)
820682 {
821
- struct nand_chip *this = mtd_to_nand(mtd);
822683 struct doc_priv *doc = nand_get_controller_data(this);
823684 void __iomem *docptr = doc->virtadr;
824685
....@@ -836,9 +697,9 @@
836697 }
837698
838699 /* This code is only called on write */
839
-static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat, unsigned char *ecc_code)
700
+static int doc200x_calculate_ecc(struct nand_chip *this, const u_char *dat,
701
+ unsigned char *ecc_code)
840702 {
841
- struct nand_chip *this = mtd_to_nand(mtd);
842703 struct doc_priv *doc = nand_get_controller_data(this);
843704 void __iomem *docptr = doc->virtadr;
844705 int i;
....@@ -895,11 +756,10 @@
895756 return 0;
896757 }
897758
898
-static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat,
759
+static int doc200x_correct_data(struct nand_chip *this, u_char *dat,
899760 u_char *read_ecc, u_char *isnull)
900761 {
901762 int i, ret = 0;
902
- struct nand_chip *this = mtd_to_nand(mtd);
903763 struct doc_priv *doc = nand_get_controller_data(this);
904764 void __iomem *docptr = doc->virtadr;
905765 uint8_t calc_ecc[6];
....@@ -1046,6 +906,7 @@
1046906 {
1047907 struct nand_chip *this = mtd_to_nand(mtd);
1048908 struct doc_priv *doc = nand_get_controller_data(this);
909
+ struct nand_memory_organization *memorg;
1049910 int ret = 0;
1050911 u_char *buf;
1051912 struct NFTLMediaHeader *mh;
....@@ -1053,6 +914,8 @@
1053914 int numparts = 0;
1054915 unsigned blocks, maxblocks;
1055916 int offs, numheaders;
917
+
918
+ memorg = nanddev_get_memorg(&this->base);
1056919
1057920 buf = kmalloc(mtd->writesize, GFP_KERNEL);
1058921 if (!buf) {
....@@ -1100,6 +963,7 @@
1100963 implementation of the NAND layer. */
1101964 if (mh->UnitSizeFactor != 0xff) {
1102965 this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
966
+ memorg->pages_per_eraseblock <<= (0xff - mh->UnitSizeFactor);
1103967 mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
1104968 pr_info("Setting virtual erase size to %d\n", mtd->erasesize);
1105969 blocks = mtd->size >> this->bbt_erase_shift;
....@@ -1182,7 +1046,7 @@
11821046 " NoOfBootImageBlocks = %d\n"
11831047 " NoOfBinaryPartitions = %d\n"
11841048 " NoOfBDTLPartitions = %d\n"
1185
- " BlockMultiplerBits = %d\n"
1049
+ " BlockMultiplierBits = %d\n"
11861050 " FormatFlgs = %d\n"
11871051 " OsakVersion = %d.%d.%d.%d\n"
11881052 " PercentUsed = %d\n",
....@@ -1305,7 +1169,7 @@
13051169 struct doc_priv *doc = nand_get_controller_data(this);
13061170 struct mtd_partition parts[5];
13071171
1308
- if (this->numchips > doc->chips_per_floor) {
1172
+ if (nanddev_ntargets(&this->base) > doc->chips_per_floor) {
13091173 pr_err("Multi-floor INFTL devices not yet supported.\n");
13101174 return -EIO;
13111175 }
....@@ -1357,9 +1221,6 @@
13571221 struct nand_chip *this = mtd_to_nand(mtd);
13581222 struct doc_priv *doc = nand_get_controller_data(this);
13591223
1360
- this->read_byte = doc2000_read_byte;
1361
- this->write_buf = doc2000_writebuf;
1362
- this->read_buf = doc2000_readbuf;
13631224 doc->late_init = nftl_scan_bbt;
13641225
13651226 doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
....@@ -1372,10 +1233,6 @@
13721233 {
13731234 struct nand_chip *this = mtd_to_nand(mtd);
13741235 struct doc_priv *doc = nand_get_controller_data(this);
1375
-
1376
- this->read_byte = doc2001_read_byte;
1377
- this->write_buf = doc2001_writebuf;
1378
- this->read_buf = doc2001_readbuf;
13791236
13801237 ReadDOC(doc->virtadr, ChipID);
13811238 ReadDOC(doc->virtadr, ChipID);
....@@ -1403,13 +1260,7 @@
14031260 struct nand_chip *this = mtd_to_nand(mtd);
14041261 struct doc_priv *doc = nand_get_controller_data(this);
14051262
1406
- this->read_byte = doc2001plus_read_byte;
1407
- this->write_buf = doc2001plus_writebuf;
1408
- this->read_buf = doc2001plus_readbuf;
14091263 doc->late_init = inftl_scan_bbt;
1410
- this->cmd_ctrl = NULL;
1411
- this->select_chip = doc2001plus_select_chip;
1412
- this->cmdfunc = doc2001plus_command;
14131264 this->ecc.hwctl = doc2001plus_enable_hwecc;
14141265
14151266 doc->chips_per_floor = 1;
....@@ -1417,6 +1268,33 @@
14171268
14181269 return 1;
14191270 }
1271
+
1272
+static int doc200x_attach_chip(struct nand_chip *chip)
1273
+{
1274
+ if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST)
1275
+ return 0;
1276
+
1277
+ chip->ecc.placement = NAND_ECC_PLACEMENT_INTERLEAVED;
1278
+ chip->ecc.size = 512;
1279
+ chip->ecc.bytes = 6;
1280
+ chip->ecc.strength = 2;
1281
+ chip->ecc.options = NAND_ECC_GENERIC_ERASED_CHECK;
1282
+ chip->ecc.hwctl = doc200x_enable_hwecc;
1283
+ chip->ecc.calculate = doc200x_calculate_ecc;
1284
+ chip->ecc.correct = doc200x_correct_data;
1285
+
1286
+ return 0;
1287
+}
1288
+
1289
+static const struct nand_controller_ops doc200x_ops = {
1290
+ .exec_op = doc200x_exec_op,
1291
+ .attach_chip = doc200x_attach_chip,
1292
+};
1293
+
1294
+static const struct nand_controller_ops doc2001plus_ops = {
1295
+ .exec_op = doc2001plus_exec_op,
1296
+ .attach_chip = doc200x_attach_chip,
1297
+};
14201298
14211299 static int __init doc_probe(unsigned long physadr)
14221300 {
....@@ -1495,6 +1373,7 @@
14951373 break;
14961374 case DOC_ChipID_DocMilPlus32:
14971375 pr_err("DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
1376
+ fallthrough;
14981377 default:
14991378 ret = -ENODEV;
15001379 goto notfound;
....@@ -1560,7 +1439,6 @@
15601439 goto fail;
15611440 }
15621441
1563
-
15641442 /*
15651443 * Allocate a RS codec instance
15661444 *
....@@ -1578,6 +1456,12 @@
15781456 goto fail;
15791457 }
15801458
1459
+ nand_controller_init(&doc->base);
1460
+ if (ChipID == DOC_ChipID_DocMilPlus16)
1461
+ doc->base.ops = &doc2001plus_ops;
1462
+ else
1463
+ doc->base.ops = &doc200x_ops;
1464
+
15811465 mtd = nand_to_mtd(nand);
15821466 nand->bbt_td = (struct nand_bbt_descr *) (doc + 1);
15831467 nand->bbt_md = nand->bbt_td + 1;
....@@ -1585,24 +1469,11 @@
15851469 mtd->owner = THIS_MODULE;
15861470 mtd_set_ooblayout(mtd, &doc200x_ooblayout_ops);
15871471
1472
+ nand->controller = &doc->base;
15881473 nand_set_controller_data(nand, doc);
1589
- nand->select_chip = doc200x_select_chip;
1590
- nand->cmd_ctrl = doc200x_hwcontrol;
1591
- nand->dev_ready = doc200x_dev_ready;
1592
- nand->waitfunc = doc200x_wait;
1593
- nand->block_bad = doc200x_block_bad;
1594
- nand->ecc.hwctl = doc200x_enable_hwecc;
1595
- nand->ecc.calculate = doc200x_calculate_ecc;
1596
- nand->ecc.correct = doc200x_correct_data;
1597
-
1598
- nand->ecc.mode = NAND_ECC_HW_SYNDROME;
1599
- nand->ecc.size = 512;
1600
- nand->ecc.bytes = 6;
1601
- nand->ecc.strength = 2;
1602
- nand->ecc.options = NAND_ECC_GENERIC_ERASED_CHECK;
16031474 nand->bbt_options = NAND_BBT_USE_FLASH;
16041475 /* Skip the automatic BBT scan so we can run it manually */
1605
- nand->options |= NAND_SKIP_BBTSCAN;
1476
+ nand->options |= NAND_SKIP_BBTSCAN | NAND_NO_BBM_QUIRK;
16061477
16071478 doc->physadr = physadr;
16081479 doc->virtadr = virtadr;
....@@ -1653,13 +1524,16 @@
16531524 struct mtd_info *mtd, *nextmtd;
16541525 struct nand_chip *nand;
16551526 struct doc_priv *doc;
1527
+ int ret;
16561528
16571529 for (mtd = doclist; mtd; mtd = nextmtd) {
16581530 nand = mtd_to_nand(mtd);
16591531 doc = nand_get_controller_data(nand);
16601532
16611533 nextmtd = doc->nextdoc;
1662
- nand_release(nand);
1534
+ ret = mtd_device_unregister(mtd);
1535
+ WARN_ON(ret);
1536
+ nand_cleanup(nand);
16631537 iounmap(doc->virtadr);
16641538 release_mem_region(doc->physadr, DOC_IOREMAP_LEN);
16651539 free_rs(doc->rs_decoder);