Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * (C) 2003 Red Hat, Inc.
0004  * (C) 2004 Dan Brown <dan_brown@ieee.org>
0005  * (C) 2004 Kalev Lember <kalev@smartlink.ee>
0006  *
0007  * Author: David Woodhouse <dwmw2@infradead.org>
0008  * Additional Diskonchip 2000 and Millennium support by Dan Brown <dan_brown@ieee.org>
0009  * Diskonchip Millennium Plus support by Kalev Lember <kalev@smartlink.ee>
0010  *
0011  * Error correction code lifted from the old docecc code
0012  * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
0013  * Copyright (C) 2000 Netgem S.A.
0014  * converted to the generic Reed-Solomon library by Thomas Gleixner <tglx@linutronix.de>
0015  *
0016  * Interface to generic NAND code for M-Systems DiskOnChip devices
0017  */
0018 
0019 #include <linux/kernel.h>
0020 #include <linux/init.h>
0021 #include <linux/sched.h>
0022 #include <linux/delay.h>
0023 #include <linux/rslib.h>
0024 #include <linux/moduleparam.h>
0025 #include <linux/slab.h>
0026 #include <linux/io.h>
0027 
0028 #include <linux/mtd/mtd.h>
0029 #include <linux/mtd/rawnand.h>
0030 #include <linux/mtd/doc2000.h>
0031 #include <linux/mtd/partitions.h>
0032 #include <linux/mtd/inftl.h>
0033 #include <linux/module.h>
0034 
0035 /* Where to look for the devices? */
0036 #ifndef CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS
0037 #define CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS 0
0038 #endif
0039 
0040 static unsigned long doc_locations[] __initdata = {
0041 #if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
0042 #ifdef CONFIG_MTD_NAND_DISKONCHIP_PROBE_HIGH
0043     0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
0044     0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
0045     0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
0046     0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
0047     0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
0048 #else
0049     0xc8000, 0xca000, 0xcc000, 0xce000,
0050     0xd0000, 0xd2000, 0xd4000, 0xd6000,
0051     0xd8000, 0xda000, 0xdc000, 0xde000,
0052     0xe0000, 0xe2000, 0xe4000, 0xe6000,
0053     0xe8000, 0xea000, 0xec000, 0xee000,
0054 #endif
0055 #endif
0056     0xffffffff };
0057 
0058 static struct mtd_info *doclist = NULL;
0059 
0060 struct doc_priv {
0061     struct nand_controller base;
0062     void __iomem *virtadr;
0063     unsigned long physadr;
0064     u_char ChipID;
0065     u_char CDSNControl;
0066     int chips_per_floor;    /* The number of chips detected on each floor */
0067     int curfloor;
0068     int curchip;
0069     int mh0_page;
0070     int mh1_page;
0071     struct rs_control *rs_decoder;
0072     struct mtd_info *nextdoc;
0073     bool supports_32b_reads;
0074 
0075     /* Handle the last stage of initialization (BBT scan, partitioning) */
0076     int (*late_init)(struct mtd_info *mtd);
0077 };
0078 
0079 /* This is the ecc value computed by the HW ecc generator upon writing an empty
0080    page, one with all 0xff for data. */
0081 static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
0082 
0083 #define INFTL_BBT_RESERVED_BLOCKS 4
0084 
0085 #define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
0086 #define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
0087 #define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
0088 
0089 static int debug = 0;
0090 module_param(debug, int, 0);
0091 
0092 static int try_dword = 1;
0093 module_param(try_dword, int, 0);
0094 
0095 static int no_ecc_failures = 0;
0096 module_param(no_ecc_failures, int, 0);
0097 
0098 static int no_autopart = 0;
0099 module_param(no_autopart, int, 0);
0100 
0101 static int show_firmware_partition = 0;
0102 module_param(show_firmware_partition, int, 0);
0103 
0104 #ifdef CONFIG_MTD_NAND_DISKONCHIP_BBTWRITE
0105 static int inftl_bbt_write = 1;
0106 #else
0107 static int inftl_bbt_write = 0;
0108 #endif
0109 module_param(inftl_bbt_write, int, 0);
0110 
0111 static unsigned long doc_config_location = CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS;
0112 module_param(doc_config_location, ulong, 0);
0113 MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
0114 
0115 /* Sector size for HW ECC */
0116 #define SECTOR_SIZE 512
0117 /* The sector bytes are packed into NB_DATA 10 bit words */
0118 #define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
0119 /* Number of roots */
0120 #define NROOTS 4
0121 /* First consective root */
0122 #define FCR 510
0123 /* Number of symbols */
0124 #define NN 1023
0125 
0126 /*
0127  * The HW decoder in the DoC ASIC's provides us a error syndrome,
0128  * which we must convert to a standard syndrome usable by the generic
0129  * Reed-Solomon library code.
0130  *
0131  * Fabrice Bellard figured this out in the old docecc code. I added
0132  * some comments, improved a minor bit and converted it to make use
0133  * of the generic Reed-Solomon library. tglx
0134  */
0135 static int doc_ecc_decode(struct rs_control *rs, uint8_t *data, uint8_t *ecc)
0136 {
0137     int i, j, nerr, errpos[8];
0138     uint8_t parity;
0139     uint16_t ds[4], s[5], tmp, errval[8], syn[4];
0140     struct rs_codec *cd = rs->codec;
0141 
0142     memset(syn, 0, sizeof(syn));
0143     /* Convert the ecc bytes into words */
0144     ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
0145     ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
0146     ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
0147     ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
0148     parity = ecc[1];
0149 
0150     /* Initialize the syndrome buffer */
0151     for (i = 0; i < NROOTS; i++)
0152         s[i] = ds[0];
0153     /*
0154      *  Evaluate
0155      *  s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
0156      *  where x = alpha^(FCR + i)
0157      */
0158     for (j = 1; j < NROOTS; j++) {
0159         if (ds[j] == 0)
0160             continue;
0161         tmp = cd->index_of[ds[j]];
0162         for (i = 0; i < NROOTS; i++)
0163             s[i] ^= cd->alpha_to[rs_modnn(cd, tmp + (FCR + i) * j)];
0164     }
0165 
0166     /* Calc syn[i] = s[i] / alpha^(v + i) */
0167     for (i = 0; i < NROOTS; i++) {
0168         if (s[i])
0169             syn[i] = rs_modnn(cd, cd->index_of[s[i]] + (NN - FCR - i));
0170     }
0171     /* Call the decoder library */
0172     nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);
0173 
0174     /* Incorrectable errors ? */
0175     if (nerr < 0)
0176         return nerr;
0177 
0178     /*
0179      * Correct the errors. The bitpositions are a bit of magic,
0180      * but they are given by the design of the de/encoder circuit
0181      * in the DoC ASIC's.
0182      */
0183     for (i = 0; i < nerr; i++) {
0184         int index, bitpos, pos = 1015 - errpos[i];
0185         uint8_t val;
0186         if (pos >= NB_DATA && pos < 1019)
0187             continue;
0188         if (pos < NB_DATA) {
0189             /* extract bit position (MSB first) */
0190             pos = 10 * (NB_DATA - 1 - pos) - 6;
0191             /* now correct the following 10 bits. At most two bytes
0192                can be modified since pos is even */
0193             index = (pos >> 3) ^ 1;
0194             bitpos = pos & 7;
0195             if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
0196                 val = (uint8_t) (errval[i] >> (2 + bitpos));
0197                 parity ^= val;
0198                 if (index < SECTOR_SIZE)
0199                     data[index] ^= val;
0200             }
0201             index = ((pos >> 3) + 1) ^ 1;
0202             bitpos = (bitpos + 10) & 7;
0203             if (bitpos == 0)
0204                 bitpos = 8;
0205             if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
0206                 val = (uint8_t) (errval[i] << (8 - bitpos));
0207                 parity ^= val;
0208                 if (index < SECTOR_SIZE)
0209                     data[index] ^= val;
0210             }
0211         }
0212     }
0213     /* If the parity is wrong, no rescue possible */
0214     return parity ? -EBADMSG : nerr;
0215 }
0216 
0217 static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
0218 {
0219     volatile char __always_unused dummy;
0220     int i;
0221 
0222     for (i = 0; i < cycles; i++) {
0223         if (DoC_is_Millennium(doc))
0224             dummy = ReadDOC(doc->virtadr, NOP);
0225         else if (DoC_is_MillenniumPlus(doc))
0226             dummy = ReadDOC(doc->virtadr, Mplus_NOP);
0227         else
0228             dummy = ReadDOC(doc->virtadr, DOCStatus);
0229     }
0230 
0231 }
0232 
0233 #define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
0234 
0235 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
0236 static int _DoC_WaitReady(struct doc_priv *doc)
0237 {
0238     void __iomem *docptr = doc->virtadr;
0239     unsigned long timeo = jiffies + (HZ * 10);
0240 
0241     if (debug)
0242         printk("_DoC_WaitReady...\n");
0243     /* Out-of-line routine to wait for chip response */
0244     if (DoC_is_MillenniumPlus(doc)) {
0245         while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
0246             if (time_after(jiffies, timeo)) {
0247                 printk("_DoC_WaitReady timed out.\n");
0248                 return -EIO;
0249             }
0250             udelay(1);
0251             cond_resched();
0252         }
0253     } else {
0254         while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
0255             if (time_after(jiffies, timeo)) {
0256                 printk("_DoC_WaitReady timed out.\n");
0257                 return -EIO;
0258             }
0259             udelay(1);
0260             cond_resched();
0261         }
0262     }
0263 
0264     return 0;
0265 }
0266 
0267 static inline int DoC_WaitReady(struct doc_priv *doc)
0268 {
0269     void __iomem *docptr = doc->virtadr;
0270     int ret = 0;
0271 
0272     if (DoC_is_MillenniumPlus(doc)) {
0273         DoC_Delay(doc, 4);
0274 
0275         if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
0276             /* Call the out-of-line routine to wait */
0277             ret = _DoC_WaitReady(doc);
0278     } else {
0279         DoC_Delay(doc, 4);
0280 
0281         if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
0282             /* Call the out-of-line routine to wait */
0283             ret = _DoC_WaitReady(doc);
0284         DoC_Delay(doc, 2);
0285     }
0286 
0287     if (debug)
0288         printk("DoC_WaitReady OK\n");
0289     return ret;
0290 }
0291 
0292 static void doc2000_write_byte(struct nand_chip *this, u_char datum)
0293 {
0294     struct doc_priv *doc = nand_get_controller_data(this);
0295     void __iomem *docptr = doc->virtadr;
0296 
0297     if (debug)
0298         printk("write_byte %02x\n", datum);
0299     WriteDOC(datum, docptr, CDSNSlowIO);
0300     WriteDOC(datum, docptr, 2k_CDSN_IO);
0301 }
0302 
0303 static void doc2000_writebuf(struct nand_chip *this, const u_char *buf,
0304                  int len)
0305 {
0306     struct doc_priv *doc = nand_get_controller_data(this);
0307     void __iomem *docptr = doc->virtadr;
0308     int i;
0309     if (debug)
0310         printk("writebuf of %d bytes: ", len);
0311     for (i = 0; i < len; i++) {
0312         WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
0313         if (debug && i < 16)
0314             printk("%02x ", buf[i]);
0315     }
0316     if (debug)
0317         printk("\n");
0318 }
0319 
0320 static void doc2000_readbuf(struct nand_chip *this, u_char *buf, int len)
0321 {
0322     struct doc_priv *doc = nand_get_controller_data(this);
0323     void __iomem *docptr = doc->virtadr;
0324     u32 *buf32 = (u32 *)buf;
0325     int i;
0326 
0327     if (debug)
0328         printk("readbuf of %d bytes: ", len);
0329 
0330     if (!doc->supports_32b_reads ||
0331         ((((unsigned long)buf) | len) & 3)) {
0332         for (i = 0; i < len; i++)
0333             buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
0334     } else {
0335         for (i = 0; i < len / 4; i++)
0336             buf32[i] = readl(docptr + DoC_2k_CDSN_IO + i);
0337     }
0338 }
0339 
0340 /*
0341  * We need our own readid() here because it's called before the NAND chip
0342  * has been initialized, and calling nand_op_readid() would lead to a NULL
0343  * pointer exception when dereferencing the NAND timings.
0344  */
0345 static void doc200x_readid(struct nand_chip *this, unsigned int cs, u8 *id)
0346 {
0347     u8 addr = 0;
0348     struct nand_op_instr instrs[] = {
0349         NAND_OP_CMD(NAND_CMD_READID, 0),
0350         NAND_OP_ADDR(1, &addr, 50),
0351         NAND_OP_8BIT_DATA_IN(2, id, 0),
0352     };
0353 
0354     struct nand_operation op = NAND_OPERATION(cs, instrs);
0355 
0356     if (!id)
0357         op.ninstrs--;
0358 
0359     this->controller->ops->exec_op(this, &op, false);
0360 }
0361 
0362 static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
0363 {
0364     struct nand_chip *this = mtd_to_nand(mtd);
0365     struct doc_priv *doc = nand_get_controller_data(this);
0366     uint16_t ret;
0367     u8 id[2];
0368 
0369     doc200x_readid(this, nr, id);
0370 
0371     ret = ((u16)id[0] << 8) | id[1];
0372 
0373     if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
0374         /* First chip probe. See if we get same results by 32-bit access */
0375         union {
0376             uint32_t dword;
0377             uint8_t byte[4];
0378         } ident;
0379         void __iomem *docptr = doc->virtadr;
0380 
0381         doc200x_readid(this, nr, NULL);
0382 
0383         ident.dword = readl(docptr + DoC_2k_CDSN_IO);
0384         if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
0385             pr_info("DiskOnChip 2000 responds to DWORD access\n");
0386             doc->supports_32b_reads = true;
0387         }
0388     }
0389 
0390     return ret;
0391 }
0392 
0393 static void __init doc2000_count_chips(struct mtd_info *mtd)
0394 {
0395     struct nand_chip *this = mtd_to_nand(mtd);
0396     struct doc_priv *doc = nand_get_controller_data(this);
0397     uint16_t mfrid;
0398     int i;
0399 
0400     /* Max 4 chips per floor on DiskOnChip 2000 */
0401     doc->chips_per_floor = 4;
0402 
0403     /* Find out what the first chip is */
0404     mfrid = doc200x_ident_chip(mtd, 0);
0405 
0406     /* Find how many chips in each floor. */
0407     for (i = 1; i < 4; i++) {
0408         if (doc200x_ident_chip(mtd, i) != mfrid)
0409             break;
0410     }
0411     doc->chips_per_floor = i;
0412     pr_debug("Detected %d chips per floor.\n", i);
0413 }
0414 
0415 static void doc2001_write_byte(struct nand_chip *this, u_char datum)
0416 {
0417     struct doc_priv *doc = nand_get_controller_data(this);
0418     void __iomem *docptr = doc->virtadr;
0419 
0420     WriteDOC(datum, docptr, CDSNSlowIO);
0421     WriteDOC(datum, docptr, Mil_CDSN_IO);
0422     WriteDOC(datum, docptr, WritePipeTerm);
0423 }
0424 
0425 static void doc2001_writebuf(struct nand_chip *this, const u_char *buf, int len)
0426 {
0427     struct doc_priv *doc = nand_get_controller_data(this);
0428     void __iomem *docptr = doc->virtadr;
0429     int i;
0430 
0431     for (i = 0; i < len; i++)
0432         WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
0433     /* Terminate write pipeline */
0434     WriteDOC(0x00, docptr, WritePipeTerm);
0435 }
0436 
0437 static void doc2001_readbuf(struct nand_chip *this, u_char *buf, int len)
0438 {
0439     struct doc_priv *doc = nand_get_controller_data(this);
0440     void __iomem *docptr = doc->virtadr;
0441     int i;
0442 
0443     /* Start read pipeline */
0444     ReadDOC(docptr, ReadPipeInit);
0445 
0446     for (i = 0; i < len - 1; i++)
0447         buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
0448 
0449     /* Terminate read pipeline */
0450     buf[i] = ReadDOC(docptr, LastDataRead);
0451 }
0452 
0453 static void doc2001plus_writebuf(struct nand_chip *this, const u_char *buf, int len)
0454 {
0455     struct doc_priv *doc = nand_get_controller_data(this);
0456     void __iomem *docptr = doc->virtadr;
0457     int i;
0458 
0459     if (debug)
0460         printk("writebuf of %d bytes: ", len);
0461     for (i = 0; i < len; i++) {
0462         WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
0463         if (debug && i < 16)
0464             printk("%02x ", buf[i]);
0465     }
0466     if (debug)
0467         printk("\n");
0468 }
0469 
0470 static void doc2001plus_readbuf(struct nand_chip *this, u_char *buf, int len)
0471 {
0472     struct doc_priv *doc = nand_get_controller_data(this);
0473     void __iomem *docptr = doc->virtadr;
0474     int i;
0475 
0476     if (debug)
0477         printk("readbuf of %d bytes: ", len);
0478 
0479     /* Start read pipeline */
0480     ReadDOC(docptr, Mplus_ReadPipeInit);
0481     ReadDOC(docptr, Mplus_ReadPipeInit);
0482 
0483     for (i = 0; i < len - 2; i++) {
0484         buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
0485         if (debug && i < 16)
0486             printk("%02x ", buf[i]);
0487     }
0488 
0489     /* Terminate read pipeline */
0490     if (len >= 2) {
0491         buf[len - 2] = ReadDOC(docptr, Mplus_LastDataRead);
0492         if (debug && i < 16)
0493             printk("%02x ", buf[len - 2]);
0494     }
0495 
0496     buf[len - 1] = ReadDOC(docptr, Mplus_LastDataRead);
0497     if (debug && i < 16)
0498         printk("%02x ", buf[len - 1]);
0499     if (debug)
0500         printk("\n");
0501 }
0502 
0503 static void doc200x_write_control(struct doc_priv *doc, u8 value)
0504 {
0505     WriteDOC(value, doc->virtadr, CDSNControl);
0506     /* 11.4.3 -- 4 NOPs after CSDNControl write */
0507     DoC_Delay(doc, 4);
0508 }
0509 
0510 static void doc200x_exec_instr(struct nand_chip *this,
0511                    const struct nand_op_instr *instr)
0512 {
0513     struct doc_priv *doc = nand_get_controller_data(this);
0514     unsigned int i;
0515 
0516     switch (instr->type) {
0517     case NAND_OP_CMD_INSTR:
0518         doc200x_write_control(doc, CDSN_CTRL_CE | CDSN_CTRL_CLE);
0519         doc2000_write_byte(this, instr->ctx.cmd.opcode);
0520         break;
0521 
0522     case NAND_OP_ADDR_INSTR:
0523         doc200x_write_control(doc, CDSN_CTRL_CE | CDSN_CTRL_ALE);
0524         for (i = 0; i < instr->ctx.addr.naddrs; i++) {
0525             u8 addr = instr->ctx.addr.addrs[i];
0526 
0527             if (DoC_is_2000(doc))
0528                 doc2000_write_byte(this, addr);
0529             else
0530                 doc2001_write_byte(this, addr);
0531         }
0532         break;
0533 
0534     case NAND_OP_DATA_IN_INSTR:
0535         doc200x_write_control(doc, CDSN_CTRL_CE);
0536         if (DoC_is_2000(doc))
0537             doc2000_readbuf(this, instr->ctx.data.buf.in,
0538                     instr->ctx.data.len);
0539         else
0540             doc2001_readbuf(this, instr->ctx.data.buf.in,
0541                     instr->ctx.data.len);
0542         break;
0543 
0544     case NAND_OP_DATA_OUT_INSTR:
0545         doc200x_write_control(doc, CDSN_CTRL_CE);
0546         if (DoC_is_2000(doc))
0547             doc2000_writebuf(this, instr->ctx.data.buf.out,
0548                      instr->ctx.data.len);
0549         else
0550             doc2001_writebuf(this, instr->ctx.data.buf.out,
0551                      instr->ctx.data.len);
0552         break;
0553 
0554     case NAND_OP_WAITRDY_INSTR:
0555         DoC_WaitReady(doc);
0556         break;
0557     }
0558 
0559     if (instr->delay_ns)
0560         ndelay(instr->delay_ns);
0561 }
0562 
0563 static int doc200x_exec_op(struct nand_chip *this,
0564                const struct nand_operation *op,
0565                bool check_only)
0566 {
0567     struct doc_priv *doc = nand_get_controller_data(this);
0568     unsigned int i;
0569 
0570     if (check_only)
0571         return true;
0572 
0573     doc->curchip = op->cs % doc->chips_per_floor;
0574     doc->curfloor = op->cs / doc->chips_per_floor;
0575 
0576     WriteDOC(doc->curfloor, doc->virtadr, FloorSelect);
0577     WriteDOC(doc->curchip, doc->virtadr, CDSNDeviceSelect);
0578 
0579     /* Assert CE pin */
0580     doc200x_write_control(doc, CDSN_CTRL_CE);
0581 
0582     for (i = 0; i < op->ninstrs; i++)
0583         doc200x_exec_instr(this, &op->instrs[i]);
0584 
0585     /* De-assert CE pin */
0586     doc200x_write_control(doc, 0);
0587 
0588     return 0;
0589 }
0590 
0591 static void doc2001plus_write_pipe_term(struct doc_priv *doc)
0592 {
0593     WriteDOC(0x00, doc->virtadr, Mplus_WritePipeTerm);
0594     WriteDOC(0x00, doc->virtadr, Mplus_WritePipeTerm);
0595 }
0596 
0597 static void doc2001plus_exec_instr(struct nand_chip *this,
0598                    const struct nand_op_instr *instr)
0599 {
0600     struct doc_priv *doc = nand_get_controller_data(this);
0601     unsigned int i;
0602 
0603     switch (instr->type) {
0604     case NAND_OP_CMD_INSTR:
0605         WriteDOC(instr->ctx.cmd.opcode, doc->virtadr, Mplus_FlashCmd);
0606         doc2001plus_write_pipe_term(doc);
0607         break;
0608 
0609     case NAND_OP_ADDR_INSTR:
0610         for (i = 0; i < instr->ctx.addr.naddrs; i++) {
0611             u8 addr = instr->ctx.addr.addrs[i];
0612 
0613             WriteDOC(addr, doc->virtadr, Mplus_FlashAddress);
0614         }
0615         doc2001plus_write_pipe_term(doc);
0616         /* deassert ALE */
0617         WriteDOC(0, doc->virtadr, Mplus_FlashControl);
0618         break;
0619 
0620     case NAND_OP_DATA_IN_INSTR:
0621         doc2001plus_readbuf(this, instr->ctx.data.buf.in,
0622                     instr->ctx.data.len);
0623         break;
0624     case NAND_OP_DATA_OUT_INSTR:
0625         doc2001plus_writebuf(this, instr->ctx.data.buf.out,
0626                      instr->ctx.data.len);
0627         doc2001plus_write_pipe_term(doc);
0628         break;
0629     case NAND_OP_WAITRDY_INSTR:
0630         DoC_WaitReady(doc);
0631         break;
0632     }
0633 
0634     if (instr->delay_ns)
0635         ndelay(instr->delay_ns);
0636 }
0637 
0638 static int doc2001plus_exec_op(struct nand_chip *this,
0639                    const struct nand_operation *op,
0640                    bool check_only)
0641 {
0642     struct doc_priv *doc = nand_get_controller_data(this);
0643     unsigned int i;
0644 
0645     if (check_only)
0646         return true;
0647 
0648     doc->curchip = op->cs % doc->chips_per_floor;
0649     doc->curfloor = op->cs / doc->chips_per_floor;
0650 
0651     /* Assert ChipEnable and deassert WriteProtect */
0652     WriteDOC(DOC_FLASH_CE, doc->virtadr, Mplus_FlashSelect);
0653 
0654     for (i = 0; i < op->ninstrs; i++)
0655         doc2001plus_exec_instr(this, &op->instrs[i]);
0656 
0657     /* De-assert ChipEnable */
0658     WriteDOC(0, doc->virtadr, Mplus_FlashSelect);
0659 
0660     return 0;
0661 }
0662 
0663 static void doc200x_enable_hwecc(struct nand_chip *this, int mode)
0664 {
0665     struct doc_priv *doc = nand_get_controller_data(this);
0666     void __iomem *docptr = doc->virtadr;
0667 
0668     /* Prime the ECC engine */
0669     switch (mode) {
0670     case NAND_ECC_READ:
0671         WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
0672         WriteDOC(DOC_ECC_EN, docptr, ECCConf);
0673         break;
0674     case NAND_ECC_WRITE:
0675         WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
0676         WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
0677         break;
0678     }
0679 }
0680 
0681 static void doc2001plus_enable_hwecc(struct nand_chip *this, int mode)
0682 {
0683     struct doc_priv *doc = nand_get_controller_data(this);
0684     void __iomem *docptr = doc->virtadr;
0685 
0686     /* Prime the ECC engine */
0687     switch (mode) {
0688     case NAND_ECC_READ:
0689         WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
0690         WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
0691         break;
0692     case NAND_ECC_WRITE:
0693         WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
0694         WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
0695         break;
0696     }
0697 }
0698 
0699 /* This code is only called on write */
0700 static int doc200x_calculate_ecc(struct nand_chip *this, const u_char *dat,
0701                  unsigned char *ecc_code)
0702 {
0703     struct doc_priv *doc = nand_get_controller_data(this);
0704     void __iomem *docptr = doc->virtadr;
0705     int i;
0706     int __always_unused emptymatch = 1;
0707 
0708     /* flush the pipeline */
0709     if (DoC_is_2000(doc)) {
0710         WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
0711         WriteDOC(0, docptr, 2k_CDSN_IO);
0712         WriteDOC(0, docptr, 2k_CDSN_IO);
0713         WriteDOC(0, docptr, 2k_CDSN_IO);
0714         WriteDOC(doc->CDSNControl, docptr, CDSNControl);
0715     } else if (DoC_is_MillenniumPlus(doc)) {
0716         WriteDOC(0, docptr, Mplus_NOP);
0717         WriteDOC(0, docptr, Mplus_NOP);
0718         WriteDOC(0, docptr, Mplus_NOP);
0719     } else {
0720         WriteDOC(0, docptr, NOP);
0721         WriteDOC(0, docptr, NOP);
0722         WriteDOC(0, docptr, NOP);
0723     }
0724 
0725     for (i = 0; i < 6; i++) {
0726         if (DoC_is_MillenniumPlus(doc))
0727             ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
0728         else
0729             ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
0730         if (ecc_code[i] != empty_write_ecc[i])
0731             emptymatch = 0;
0732     }
0733     if (DoC_is_MillenniumPlus(doc))
0734         WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
0735     else
0736         WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
0737 #if 0
0738     /* If emptymatch=1, we might have an all-0xff data buffer.  Check. */
0739     if (emptymatch) {
0740         /* Note: this somewhat expensive test should not be triggered
0741            often.  It could be optimized away by examining the data in
0742            the writebuf routine, and remembering the result. */
0743         for (i = 0; i < 512; i++) {
0744             if (dat[i] == 0xff)
0745                 continue;
0746             emptymatch = 0;
0747             break;
0748         }
0749     }
0750     /* If emptymatch still =1, we do have an all-0xff data buffer.
0751        Return all-0xff ecc value instead of the computed one, so
0752        it'll look just like a freshly-erased page. */
0753     if (emptymatch)
0754         memset(ecc_code, 0xff, 6);
0755 #endif
0756     return 0;
0757 }
0758 
0759 static int doc200x_correct_data(struct nand_chip *this, u_char *dat,
0760                 u_char *read_ecc, u_char *isnull)
0761 {
0762     int i, ret = 0;
0763     struct doc_priv *doc = nand_get_controller_data(this);
0764     void __iomem *docptr = doc->virtadr;
0765     uint8_t calc_ecc[6];
0766     volatile u_char dummy;
0767 
0768     /* flush the pipeline */
0769     if (DoC_is_2000(doc)) {
0770         dummy = ReadDOC(docptr, 2k_ECCStatus);
0771         dummy = ReadDOC(docptr, 2k_ECCStatus);
0772         dummy = ReadDOC(docptr, 2k_ECCStatus);
0773     } else if (DoC_is_MillenniumPlus(doc)) {
0774         dummy = ReadDOC(docptr, Mplus_ECCConf);
0775         dummy = ReadDOC(docptr, Mplus_ECCConf);
0776         dummy = ReadDOC(docptr, Mplus_ECCConf);
0777     } else {
0778         dummy = ReadDOC(docptr, ECCConf);
0779         dummy = ReadDOC(docptr, ECCConf);
0780         dummy = ReadDOC(docptr, ECCConf);
0781     }
0782 
0783     /* Error occurred ? */
0784     if (dummy & 0x80) {
0785         for (i = 0; i < 6; i++) {
0786             if (DoC_is_MillenniumPlus(doc))
0787                 calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
0788             else
0789                 calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
0790         }
0791 
0792         ret = doc_ecc_decode(doc->rs_decoder, dat, calc_ecc);
0793         if (ret > 0)
0794             pr_err("doc200x_correct_data corrected %d errors\n",
0795                    ret);
0796     }
0797     if (DoC_is_MillenniumPlus(doc))
0798         WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
0799     else
0800         WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
0801     if (no_ecc_failures && mtd_is_eccerr(ret)) {
0802         pr_err("suppressing ECC failure\n");
0803         ret = 0;
0804     }
0805     return ret;
0806 }
0807 
0808 //u_char mydatabuf[528];
0809 
0810 static int doc200x_ooblayout_ecc(struct mtd_info *mtd, int section,
0811                  struct mtd_oob_region *oobregion)
0812 {
0813     if (section)
0814         return -ERANGE;
0815 
0816     oobregion->offset = 0;
0817     oobregion->length = 6;
0818 
0819     return 0;
0820 }
0821 
0822 static int doc200x_ooblayout_free(struct mtd_info *mtd, int section,
0823                   struct mtd_oob_region *oobregion)
0824 {
0825     if (section > 1)
0826         return -ERANGE;
0827 
0828     /*
0829      * The strange out-of-order free bytes definition is a (possibly
0830      * unneeded) attempt to retain compatibility.  It used to read:
0831      *  .oobfree = { {8, 8} }
0832      * Since that leaves two bytes unusable, it was changed.  But the
0833      * following scheme might affect existing jffs2 installs by moving the
0834      * cleanmarker:
0835      *  .oobfree = { {6, 10} }
0836      * jffs2 seems to handle the above gracefully, but the current scheme
0837      * seems safer. The only problem with it is that any code retrieving
0838      * free bytes position must be able to handle out-of-order segments.
0839      */
0840     if (!section) {
0841         oobregion->offset = 8;
0842         oobregion->length = 8;
0843     } else {
0844         oobregion->offset = 6;
0845         oobregion->length = 2;
0846     }
0847 
0848     return 0;
0849 }
0850 
0851 static const struct mtd_ooblayout_ops doc200x_ooblayout_ops = {
0852     .ecc = doc200x_ooblayout_ecc,
0853     .free = doc200x_ooblayout_free,
0854 };
0855 
0856 /* Find the (I)NFTL Media Header, and optionally also the mirror media header.
0857    On successful return, buf will contain a copy of the media header for
0858    further processing.  id is the string to scan for, and will presumably be
0859    either "ANAND" or "BNAND".  If findmirror=1, also look for the mirror media
0860    header.  The page #s of the found media headers are placed in mh0_page and
0861    mh1_page in the DOC private structure. */
0862 static int __init find_media_headers(struct mtd_info *mtd, u_char *buf, const char *id, int findmirror)
0863 {
0864     struct nand_chip *this = mtd_to_nand(mtd);
0865     struct doc_priv *doc = nand_get_controller_data(this);
0866     unsigned offs;
0867     int ret;
0868     size_t retlen;
0869 
0870     for (offs = 0; offs < mtd->size; offs += mtd->erasesize) {
0871         ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
0872         if (retlen != mtd->writesize)
0873             continue;
0874         if (ret) {
0875             pr_warn("ECC error scanning DOC at 0x%x\n", offs);
0876         }
0877         if (memcmp(buf, id, 6))
0878             continue;
0879         pr_info("Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
0880         if (doc->mh0_page == -1) {
0881             doc->mh0_page = offs >> this->page_shift;
0882             if (!findmirror)
0883                 return 1;
0884             continue;
0885         }
0886         doc->mh1_page = offs >> this->page_shift;
0887         return 2;
0888     }
0889     if (doc->mh0_page == -1) {
0890         pr_warn("DiskOnChip %s Media Header not found.\n", id);
0891         return 0;
0892     }
0893     /* Only one mediaheader was found.  We want buf to contain a
0894        mediaheader on return, so we'll have to re-read the one we found. */
0895     offs = doc->mh0_page << this->page_shift;
0896     ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
0897     if (retlen != mtd->writesize) {
0898         /* Insanity.  Give up. */
0899         pr_err("Read DiskOnChip Media Header once, but can't reread it???\n");
0900         return 0;
0901     }
0902     return 1;
0903 }
0904 
0905 static inline int __init nftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
0906 {
0907     struct nand_chip *this = mtd_to_nand(mtd);
0908     struct doc_priv *doc = nand_get_controller_data(this);
0909     struct nand_memory_organization *memorg;
0910     int ret = 0;
0911     u_char *buf;
0912     struct NFTLMediaHeader *mh;
0913     const unsigned psize = 1 << this->page_shift;
0914     int numparts = 0;
0915     unsigned blocks, maxblocks;
0916     int offs, numheaders;
0917 
0918     memorg = nanddev_get_memorg(&this->base);
0919 
0920     buf = kmalloc(mtd->writesize, GFP_KERNEL);
0921     if (!buf) {
0922         return 0;
0923     }
0924     if (!(numheaders = find_media_headers(mtd, buf, "ANAND", 1)))
0925         goto out;
0926     mh = (struct NFTLMediaHeader *)buf;
0927 
0928     le16_to_cpus(&mh->NumEraseUnits);
0929     le16_to_cpus(&mh->FirstPhysicalEUN);
0930     le32_to_cpus(&mh->FormattedSize);
0931 
0932     pr_info("    DataOrgID        = %s\n"
0933         "    NumEraseUnits    = %d\n"
0934         "    FirstPhysicalEUN = %d\n"
0935         "    FormattedSize    = %d\n"
0936         "    UnitSizeFactor   = %d\n",
0937         mh->DataOrgID, mh->NumEraseUnits,
0938         mh->FirstPhysicalEUN, mh->FormattedSize,
0939         mh->UnitSizeFactor);
0940 
0941     blocks = mtd->size >> this->phys_erase_shift;
0942     maxblocks = min(32768U, mtd->erasesize - psize);
0943 
0944     if (mh->UnitSizeFactor == 0x00) {
0945         /* Auto-determine UnitSizeFactor.  The constraints are:
0946            - There can be at most 32768 virtual blocks.
0947            - There can be at most (virtual block size - page size)
0948            virtual blocks (because MediaHeader+BBT must fit in 1).
0949          */
0950         mh->UnitSizeFactor = 0xff;
0951         while (blocks > maxblocks) {
0952             blocks >>= 1;
0953             maxblocks = min(32768U, (maxblocks << 1) + psize);
0954             mh->UnitSizeFactor--;
0955         }
0956         pr_warn("UnitSizeFactor=0x00 detected.  Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
0957     }
0958 
0959     /* NOTE: The lines below modify internal variables of the NAND and MTD
0960        layers; variables with have already been configured by nand_scan.
0961        Unfortunately, we didn't know before this point what these values
0962        should be.  Thus, this code is somewhat dependent on the exact
0963        implementation of the NAND layer.  */
0964     if (mh->UnitSizeFactor != 0xff) {
0965         this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
0966         memorg->pages_per_eraseblock <<= (0xff - mh->UnitSizeFactor);
0967         mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
0968         pr_info("Setting virtual erase size to %d\n", mtd->erasesize);
0969         blocks = mtd->size >> this->bbt_erase_shift;
0970         maxblocks = min(32768U, mtd->erasesize - psize);
0971     }
0972 
0973     if (blocks > maxblocks) {
0974         pr_err("UnitSizeFactor of 0x%02x is inconsistent with device size.  Aborting.\n", mh->UnitSizeFactor);
0975         goto out;
0976     }
0977 
0978     /* Skip past the media headers. */
0979     offs = max(doc->mh0_page, doc->mh1_page);
0980     offs <<= this->page_shift;
0981     offs += mtd->erasesize;
0982 
0983     if (show_firmware_partition == 1) {
0984         parts[0].name = " DiskOnChip Firmware / Media Header partition";
0985         parts[0].offset = 0;
0986         parts[0].size = offs;
0987         numparts = 1;
0988     }
0989 
0990     parts[numparts].name = " DiskOnChip BDTL partition";
0991     parts[numparts].offset = offs;
0992     parts[numparts].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
0993 
0994     offs += parts[numparts].size;
0995     numparts++;
0996 
0997     if (offs < mtd->size) {
0998         parts[numparts].name = " DiskOnChip Remainder partition";
0999         parts[numparts].offset = offs;
1000         parts[numparts].size = mtd->size - offs;
1001         numparts++;
1002     }
1003 
1004     ret = numparts;
1005  out:
1006     kfree(buf);
1007     return ret;
1008 }
1009 
1010 /* This is a stripped-down copy of the code in inftlmount.c */
1011 static inline int __init inftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
1012 {
1013     struct nand_chip *this = mtd_to_nand(mtd);
1014     struct doc_priv *doc = nand_get_controller_data(this);
1015     int ret = 0;
1016     u_char *buf;
1017     struct INFTLMediaHeader *mh;
1018     struct INFTLPartition *ip;
1019     int numparts = 0;
1020     int blocks;
1021     int vshift, lastvunit = 0;
1022     int i;
1023     int end = mtd->size;
1024 
1025     if (inftl_bbt_write)
1026         end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
1027 
1028     buf = kmalloc(mtd->writesize, GFP_KERNEL);
1029     if (!buf) {
1030         return 0;
1031     }
1032 
1033     if (!find_media_headers(mtd, buf, "BNAND", 0))
1034         goto out;
1035     doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
1036     mh = (struct INFTLMediaHeader *)buf;
1037 
1038     le32_to_cpus(&mh->NoOfBootImageBlocks);
1039     le32_to_cpus(&mh->NoOfBinaryPartitions);
1040     le32_to_cpus(&mh->NoOfBDTLPartitions);
1041     le32_to_cpus(&mh->BlockMultiplierBits);
1042     le32_to_cpus(&mh->FormatFlags);
1043     le32_to_cpus(&mh->PercentUsed);
1044 
1045     pr_info("    bootRecordID          = %s\n"
1046         "    NoOfBootImageBlocks   = %d\n"
1047         "    NoOfBinaryPartitions  = %d\n"
1048         "    NoOfBDTLPartitions    = %d\n"
1049         "    BlockMultiplierBits   = %d\n"
1050         "    FormatFlgs            = %d\n"
1051         "    OsakVersion           = %d.%d.%d.%d\n"
1052         "    PercentUsed           = %d\n",
1053         mh->bootRecordID, mh->NoOfBootImageBlocks,
1054         mh->NoOfBinaryPartitions,
1055         mh->NoOfBDTLPartitions,
1056         mh->BlockMultiplierBits, mh->FormatFlags,
1057         ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
1058         ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
1059         ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
1060         ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
1061         mh->PercentUsed);
1062 
1063     vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
1064 
1065     blocks = mtd->size >> vshift;
1066     if (blocks > 32768) {
1067         pr_err("BlockMultiplierBits=%d is inconsistent with device size.  Aborting.\n", mh->BlockMultiplierBits);
1068         goto out;
1069     }
1070 
1071     blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
1072     if (inftl_bbt_write && (blocks > mtd->erasesize)) {
1073         pr_err("Writeable BBTs spanning more than one erase block are not yet supported.  FIX ME!\n");
1074         goto out;
1075     }
1076 
1077     /* Scan the partitions */
1078     for (i = 0; (i < 4); i++) {
1079         ip = &(mh->Partitions[i]);
1080         le32_to_cpus(&ip->virtualUnits);
1081         le32_to_cpus(&ip->firstUnit);
1082         le32_to_cpus(&ip->lastUnit);
1083         le32_to_cpus(&ip->flags);
1084         le32_to_cpus(&ip->spareUnits);
1085         le32_to_cpus(&ip->Reserved0);
1086 
1087         pr_info("    PARTITION[%d] ->\n"
1088             "        virtualUnits    = %d\n"
1089             "        firstUnit       = %d\n"
1090             "        lastUnit        = %d\n"
1091             "        flags           = 0x%x\n"
1092             "        spareUnits      = %d\n",
1093             i, ip->virtualUnits, ip->firstUnit,
1094             ip->lastUnit, ip->flags,
1095             ip->spareUnits);
1096 
1097         if ((show_firmware_partition == 1) &&
1098             (i == 0) && (ip->firstUnit > 0)) {
1099             parts[0].name = " DiskOnChip IPL / Media Header partition";
1100             parts[0].offset = 0;
1101             parts[0].size = mtd->erasesize * ip->firstUnit;
1102             numparts = 1;
1103         }
1104 
1105         if (ip->flags & INFTL_BINARY)
1106             parts[numparts].name = " DiskOnChip BDK partition";
1107         else
1108             parts[numparts].name = " DiskOnChip BDTL partition";
1109         parts[numparts].offset = ip->firstUnit << vshift;
1110         parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
1111         numparts++;
1112         if (ip->lastUnit > lastvunit)
1113             lastvunit = ip->lastUnit;
1114         if (ip->flags & INFTL_LAST)
1115             break;
1116     }
1117     lastvunit++;
1118     if ((lastvunit << vshift) < end) {
1119         parts[numparts].name = " DiskOnChip Remainder partition";
1120         parts[numparts].offset = lastvunit << vshift;
1121         parts[numparts].size = end - parts[numparts].offset;
1122         numparts++;
1123     }
1124     ret = numparts;
1125  out:
1126     kfree(buf);
1127     return ret;
1128 }
1129 
1130 static int __init nftl_scan_bbt(struct mtd_info *mtd)
1131 {
1132     int ret, numparts;
1133     struct nand_chip *this = mtd_to_nand(mtd);
1134     struct doc_priv *doc = nand_get_controller_data(this);
1135     struct mtd_partition parts[2];
1136 
1137     memset((char *)parts, 0, sizeof(parts));
1138     /* On NFTL, we have to find the media headers before we can read the
1139        BBTs, since they're stored in the media header eraseblocks. */
1140     numparts = nftl_partscan(mtd, parts);
1141     if (!numparts)
1142         return -EIO;
1143     this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1144                 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1145                 NAND_BBT_VERSION;
1146     this->bbt_td->veroffs = 7;
1147     this->bbt_td->pages[0] = doc->mh0_page + 1;
1148     if (doc->mh1_page != -1) {
1149         this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1150                     NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1151                     NAND_BBT_VERSION;
1152         this->bbt_md->veroffs = 7;
1153         this->bbt_md->pages[0] = doc->mh1_page + 1;
1154     } else {
1155         this->bbt_md = NULL;
1156     }
1157 
1158     ret = nand_create_bbt(this);
1159     if (ret)
1160         return ret;
1161 
1162     return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts);
1163 }
1164 
1165 static int __init inftl_scan_bbt(struct mtd_info *mtd)
1166 {
1167     int ret, numparts;
1168     struct nand_chip *this = mtd_to_nand(mtd);
1169     struct doc_priv *doc = nand_get_controller_data(this);
1170     struct mtd_partition parts[5];
1171 
1172     if (nanddev_ntargets(&this->base) > doc->chips_per_floor) {
1173         pr_err("Multi-floor INFTL devices not yet supported.\n");
1174         return -EIO;
1175     }
1176 
1177     if (DoC_is_MillenniumPlus(doc)) {
1178         this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
1179         if (inftl_bbt_write)
1180             this->bbt_td->options |= NAND_BBT_WRITE;
1181         this->bbt_td->pages[0] = 2;
1182         this->bbt_md = NULL;
1183     } else {
1184         this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
1185         if (inftl_bbt_write)
1186             this->bbt_td->options |= NAND_BBT_WRITE;
1187         this->bbt_td->offs = 8;
1188         this->bbt_td->len = 8;
1189         this->bbt_td->veroffs = 7;
1190         this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1191         this->bbt_td->reserved_block_code = 0x01;
1192         this->bbt_td->pattern = "MSYS_BBT";
1193 
1194         this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
1195         if (inftl_bbt_write)
1196             this->bbt_md->options |= NAND_BBT_WRITE;
1197         this->bbt_md->offs = 8;
1198         this->bbt_md->len = 8;
1199         this->bbt_md->veroffs = 7;
1200         this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1201         this->bbt_md->reserved_block_code = 0x01;
1202         this->bbt_md->pattern = "TBB_SYSM";
1203     }
1204 
1205     ret = nand_create_bbt(this);
1206     if (ret)
1207         return ret;
1208 
1209     memset((char *)parts, 0, sizeof(parts));
1210     numparts = inftl_partscan(mtd, parts);
1211     /* At least for now, require the INFTL Media Header.  We could probably
1212        do without it for non-INFTL use, since all it gives us is
1213        autopartitioning, but I want to give it more thought. */
1214     if (!numparts)
1215         return -EIO;
1216     return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts);
1217 }
1218 
1219 static inline int __init doc2000_init(struct mtd_info *mtd)
1220 {
1221     struct nand_chip *this = mtd_to_nand(mtd);
1222     struct doc_priv *doc = nand_get_controller_data(this);
1223 
1224     doc->late_init = nftl_scan_bbt;
1225 
1226     doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
1227     doc2000_count_chips(mtd);
1228     mtd->name = "DiskOnChip 2000 (NFTL Model)";
1229     return (4 * doc->chips_per_floor);
1230 }
1231 
1232 static inline int __init doc2001_init(struct mtd_info *mtd)
1233 {
1234     struct nand_chip *this = mtd_to_nand(mtd);
1235     struct doc_priv *doc = nand_get_controller_data(this);
1236 
1237     ReadDOC(doc->virtadr, ChipID);
1238     ReadDOC(doc->virtadr, ChipID);
1239     ReadDOC(doc->virtadr, ChipID);
1240     if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1241         /* It's not a Millennium; it's one of the newer
1242            DiskOnChip 2000 units with a similar ASIC.
1243            Treat it like a Millennium, except that it
1244            can have multiple chips. */
1245         doc2000_count_chips(mtd);
1246         mtd->name = "DiskOnChip 2000 (INFTL Model)";
1247         doc->late_init = inftl_scan_bbt;
1248         return (4 * doc->chips_per_floor);
1249     } else {
1250         /* Bog-standard Millennium */
1251         doc->chips_per_floor = 1;
1252         mtd->name = "DiskOnChip Millennium";
1253         doc->late_init = nftl_scan_bbt;
1254         return 1;
1255     }
1256 }
1257 
1258 static inline int __init doc2001plus_init(struct mtd_info *mtd)
1259 {
1260     struct nand_chip *this = mtd_to_nand(mtd);
1261     struct doc_priv *doc = nand_get_controller_data(this);
1262 
1263     doc->late_init = inftl_scan_bbt;
1264     this->ecc.hwctl = doc2001plus_enable_hwecc;
1265 
1266     doc->chips_per_floor = 1;
1267     mtd->name = "DiskOnChip Millennium Plus";
1268 
1269     return 1;
1270 }
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 };
1298 
1299 static int __init doc_probe(unsigned long physadr)
1300 {
1301     struct nand_chip *nand = NULL;
1302     struct doc_priv *doc = NULL;
1303     unsigned char ChipID;
1304     struct mtd_info *mtd;
1305     void __iomem *virtadr;
1306     unsigned char save_control;
1307     unsigned char tmp, tmpb, tmpc;
1308     int reg, len, numchips;
1309     int ret = 0;
1310 
1311     if (!request_mem_region(physadr, DOC_IOREMAP_LEN, "DiskOnChip"))
1312         return -EBUSY;
1313     virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
1314     if (!virtadr) {
1315         pr_err("Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n",
1316                DOC_IOREMAP_LEN, physadr);
1317         ret = -EIO;
1318         goto error_ioremap;
1319     }
1320 
1321     /* It's not possible to cleanly detect the DiskOnChip - the
1322      * bootup procedure will put the device into reset mode, and
1323      * it's not possible to talk to it without actually writing
1324      * to the DOCControl register. So we store the current contents
1325      * of the DOCControl register's location, in case we later decide
1326      * that it's not a DiskOnChip, and want to put it back how we
1327      * found it.
1328      */
1329     save_control = ReadDOC(virtadr, DOCControl);
1330 
1331     /* Reset the DiskOnChip ASIC */
1332     WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1333     WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1334 
1335     /* Enable the DiskOnChip ASIC */
1336     WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1337     WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1338 
1339     ChipID = ReadDOC(virtadr, ChipID);
1340 
1341     switch (ChipID) {
1342     case DOC_ChipID_Doc2k:
1343         reg = DoC_2k_ECCStatus;
1344         break;
1345     case DOC_ChipID_DocMil:
1346         reg = DoC_ECCConf;
1347         break;
1348     case DOC_ChipID_DocMilPlus16:
1349     case DOC_ChipID_DocMilPlus32:
1350     case 0:
1351         /* Possible Millennium Plus, need to do more checks */
1352         /* Possibly release from power down mode */
1353         for (tmp = 0; (tmp < 4); tmp++)
1354             ReadDOC(virtadr, Mplus_Power);
1355 
1356         /* Reset the Millennium Plus ASIC */
1357         tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
1358         WriteDOC(tmp, virtadr, Mplus_DOCControl);
1359         WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1360 
1361         usleep_range(1000, 2000);
1362         /* Enable the Millennium Plus ASIC */
1363         tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
1364         WriteDOC(tmp, virtadr, Mplus_DOCControl);
1365         WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1366         usleep_range(1000, 2000);
1367 
1368         ChipID = ReadDOC(virtadr, ChipID);
1369 
1370         switch (ChipID) {
1371         case DOC_ChipID_DocMilPlus16:
1372             reg = DoC_Mplus_Toggle;
1373             break;
1374         case DOC_ChipID_DocMilPlus32:
1375             pr_err("DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
1376             fallthrough;
1377         default:
1378             ret = -ENODEV;
1379             goto notfound;
1380         }
1381         break;
1382 
1383     default:
1384         ret = -ENODEV;
1385         goto notfound;
1386     }
1387     /* Check the TOGGLE bit in the ECC register */
1388     tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1389     tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1390     tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1391     if ((tmp == tmpb) || (tmp != tmpc)) {
1392         pr_warn("Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
1393         ret = -ENODEV;
1394         goto notfound;
1395     }
1396 
1397     for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1398         unsigned char oldval;
1399         unsigned char newval;
1400         nand = mtd_to_nand(mtd);
1401         doc = nand_get_controller_data(nand);
1402         /* Use the alias resolution register to determine if this is
1403            in fact the same DOC aliased to a new address.  If writes
1404            to one chip's alias resolution register change the value on
1405            the other chip, they're the same chip. */
1406         if (ChipID == DOC_ChipID_DocMilPlus16) {
1407             oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1408             newval = ReadDOC(virtadr, Mplus_AliasResolution);
1409         } else {
1410             oldval = ReadDOC(doc->virtadr, AliasResolution);
1411             newval = ReadDOC(virtadr, AliasResolution);
1412         }
1413         if (oldval != newval)
1414             continue;
1415         if (ChipID == DOC_ChipID_DocMilPlus16) {
1416             WriteDOC(~newval, virtadr, Mplus_AliasResolution);
1417             oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1418             WriteDOC(newval, virtadr, Mplus_AliasResolution);   // restore it
1419         } else {
1420             WriteDOC(~newval, virtadr, AliasResolution);
1421             oldval = ReadDOC(doc->virtadr, AliasResolution);
1422             WriteDOC(newval, virtadr, AliasResolution); // restore it
1423         }
1424         newval = ~newval;
1425         if (oldval == newval) {
1426             pr_debug("Found alias of DOC at 0x%lx to 0x%lx\n",
1427                  doc->physadr, physadr);
1428             goto notfound;
1429         }
1430     }
1431 
1432     pr_notice("DiskOnChip found at 0x%lx\n", physadr);
1433 
1434     len = sizeof(struct nand_chip) + sizeof(struct doc_priv) +
1435           (2 * sizeof(struct nand_bbt_descr));
1436     nand = kzalloc(len, GFP_KERNEL);
1437     if (!nand) {
1438         ret = -ENOMEM;
1439         goto fail;
1440     }
1441 
1442     /*
1443      * Allocate a RS codec instance
1444      *
1445      * Symbolsize is 10 (bits)
1446      * Primitve polynomial is x^10+x^3+1
1447      * First consecutive root is 510
1448      * Primitve element to generate roots = 1
1449      * Generator polinomial degree = 4
1450      */
1451     doc = (struct doc_priv *) (nand + 1);
1452     doc->rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
1453     if (!doc->rs_decoder) {
1454         pr_err("DiskOnChip: Could not create a RS codec\n");
1455         ret = -ENOMEM;
1456         goto fail;
1457     }
1458 
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 
1465     mtd         = nand_to_mtd(nand);
1466     nand->bbt_td        = (struct nand_bbt_descr *) (doc + 1);
1467     nand->bbt_md        = nand->bbt_td + 1;
1468 
1469     mtd->owner      = THIS_MODULE;
1470     mtd_set_ooblayout(mtd, &doc200x_ooblayout_ops);
1471 
1472     nand->controller    = &doc->base;
1473     nand_set_controller_data(nand, doc);
1474     nand->bbt_options   = NAND_BBT_USE_FLASH;
1475     /* Skip the automatic BBT scan so we can run it manually */
1476     nand->options       |= NAND_SKIP_BBTSCAN | NAND_NO_BBM_QUIRK;
1477 
1478     doc->physadr        = physadr;
1479     doc->virtadr        = virtadr;
1480     doc->ChipID     = ChipID;
1481     doc->curfloor       = -1;
1482     doc->curchip        = -1;
1483     doc->mh0_page       = -1;
1484     doc->mh1_page       = -1;
1485     doc->nextdoc        = doclist;
1486 
1487     if (ChipID == DOC_ChipID_Doc2k)
1488         numchips = doc2000_init(mtd);
1489     else if (ChipID == DOC_ChipID_DocMilPlus16)
1490         numchips = doc2001plus_init(mtd);
1491     else
1492         numchips = doc2001_init(mtd);
1493 
1494     if ((ret = nand_scan(nand, numchips)) || (ret = doc->late_init(mtd))) {
1495         /* DBB note: i believe nand_cleanup is necessary here, as
1496            buffers may have been allocated in nand_base.  Check with
1497            Thomas. FIX ME! */
1498         nand_cleanup(nand);
1499         goto fail;
1500     }
1501 
1502     /* Success! */
1503     doclist = mtd;
1504     return 0;
1505 
1506  notfound:
1507     /* Put back the contents of the DOCControl register, in case it's not
1508        actually a DiskOnChip.  */
1509     WriteDOC(save_control, virtadr, DOCControl);
1510  fail:
1511     if (doc)
1512         free_rs(doc->rs_decoder);
1513     kfree(nand);
1514     iounmap(virtadr);
1515 
1516 error_ioremap:
1517     release_mem_region(physadr, DOC_IOREMAP_LEN);
1518 
1519     return ret;
1520 }
1521 
1522 static void release_nanddoc(void)
1523 {
1524     struct mtd_info *mtd, *nextmtd;
1525     struct nand_chip *nand;
1526     struct doc_priv *doc;
1527     int ret;
1528 
1529     for (mtd = doclist; mtd; mtd = nextmtd) {
1530         nand = mtd_to_nand(mtd);
1531         doc = nand_get_controller_data(nand);
1532 
1533         nextmtd = doc->nextdoc;
1534         ret = mtd_device_unregister(mtd);
1535         WARN_ON(ret);
1536         nand_cleanup(nand);
1537         iounmap(doc->virtadr);
1538         release_mem_region(doc->physadr, DOC_IOREMAP_LEN);
1539         free_rs(doc->rs_decoder);
1540         kfree(nand);
1541     }
1542 }
1543 
1544 static int __init init_nanddoc(void)
1545 {
1546     int i, ret = 0;
1547 
1548     if (doc_config_location) {
1549         pr_info("Using configured DiskOnChip probe address 0x%lx\n",
1550             doc_config_location);
1551         ret = doc_probe(doc_config_location);
1552         if (ret < 0)
1553             return ret;
1554     } else {
1555         for (i = 0; (doc_locations[i] != 0xffffffff); i++) {
1556             doc_probe(doc_locations[i]);
1557         }
1558     }
1559     /* No banner message any more. Print a message if no DiskOnChip
1560        found, so the user knows we at least tried. */
1561     if (!doclist) {
1562         pr_info("No valid DiskOnChip devices found\n");
1563         ret = -ENODEV;
1564     }
1565     return ret;
1566 }
1567 
1568 static void __exit cleanup_nanddoc(void)
1569 {
1570     /* Cleanup the nand/DoC resources */
1571     release_nanddoc();
1572 }
1573 
1574 module_init(init_nanddoc);
1575 module_exit(cleanup_nanddoc);
1576 
1577 MODULE_LICENSE("GPL");
1578 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1579 MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver");