linux/drivers/mtd/nand/diskonchip.c
<<
>>
Prefs
   1/*
   2 * drivers/mtd/nand/diskonchip.c
   3 *
   4 * (C) 2003 Red Hat, Inc.
   5 * (C) 2004 Dan Brown <dan_brown@ieee.org>
   6 * (C) 2004 Kalev Lember <kalev@smartlink.ee>
   7 *
   8 * Author: David Woodhouse <dwmw2@infradead.org>
   9 * Additional Diskonchip 2000 and Millennium support by Dan Brown <dan_brown@ieee.org>
  10 * Diskonchip Millennium Plus support by Kalev Lember <kalev@smartlink.ee>
  11 *
  12 * Error correction code lifted from the old docecc code
  13 * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
  14 * Copyright (C) 2000 Netgem S.A.
  15 * converted to the generic Reed-Solomon library by Thomas Gleixner <tglx@linutronix.de>
  16 *
  17 * Interface to generic NAND code for M-Systems DiskOnChip devices
  18 */
  19
  20#include <linux/kernel.h>
  21#include <linux/init.h>
  22#include <linux/sched.h>
  23#include <linux/delay.h>
  24#include <linux/rslib.h>
  25#include <linux/moduleparam.h>
  26#include <linux/slab.h>
  27#include <linux/io.h>
  28
  29#include <linux/mtd/mtd.h>
  30#include <linux/mtd/nand.h>
  31#include <linux/mtd/doc2000.h>
  32#include <linux/mtd/partitions.h>
  33#include <linux/mtd/inftl.h>
  34#include <linux/module.h>
  35
  36/* Where to look for the devices? */
  37#ifndef CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS
  38#define CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS 0
  39#endif
  40
  41static unsigned long doc_locations[] __initdata = {
  42#if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
  43#ifdef CONFIG_MTD_NAND_DISKONCHIP_PROBE_HIGH
  44        0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
  45        0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
  46        0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
  47        0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
  48        0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
  49#else
  50        0xc8000, 0xca000, 0xcc000, 0xce000,
  51        0xd0000, 0xd2000, 0xd4000, 0xd6000,
  52        0xd8000, 0xda000, 0xdc000, 0xde000,
  53        0xe0000, 0xe2000, 0xe4000, 0xe6000,
  54        0xe8000, 0xea000, 0xec000, 0xee000,
  55#endif
  56#endif
  57        0xffffffff };
  58
  59static struct mtd_info *doclist = NULL;
  60
  61struct doc_priv {
  62        void __iomem *virtadr;
  63        unsigned long physadr;
  64        u_char ChipID;
  65        u_char CDSNControl;
  66        int chips_per_floor;    /* The number of chips detected on each floor */
  67        int curfloor;
  68        int curchip;
  69        int mh0_page;
  70        int mh1_page;
  71        struct mtd_info *nextdoc;
  72
  73        /* Handle the last stage of initialization (BBT scan, partitioning) */
  74        int (*late_init)(struct mtd_info *mtd);
  75};
  76
  77/* This is the ecc value computed by the HW ecc generator upon writing an empty
  78   page, one with all 0xff for data. */
  79static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
  80
  81#define INFTL_BBT_RESERVED_BLOCKS 4
  82
  83#define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
  84#define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
  85#define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
  86
  87static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
  88                              unsigned int bitmask);
  89static void doc200x_select_chip(struct mtd_info *mtd, int chip);
  90
  91static int debug = 0;
  92module_param(debug, int, 0);
  93
  94static int try_dword = 1;
  95module_param(try_dword, int, 0);
  96
  97static int no_ecc_failures = 0;
  98module_param(no_ecc_failures, int, 0);
  99
 100static int no_autopart = 0;
 101module_param(no_autopart, int, 0);
 102
 103static int show_firmware_partition = 0;
 104module_param(show_firmware_partition, int, 0);
 105
 106#ifdef CONFIG_MTD_NAND_DISKONCHIP_BBTWRITE
 107static int inftl_bbt_write = 1;
 108#else
 109static int inftl_bbt_write = 0;
 110#endif
 111module_param(inftl_bbt_write, int, 0);
 112
 113static unsigned long doc_config_location = CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS;
 114module_param(doc_config_location, ulong, 0);
 115MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
 116
 117/* Sector size for HW ECC */
 118#define SECTOR_SIZE 512
 119/* The sector bytes are packed into NB_DATA 10 bit words */
 120#define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
 121/* Number of roots */
 122#define NROOTS 4
 123/* First consective root */
 124#define FCR 510
 125/* Number of symbols */
 126#define NN 1023
 127
 128/* the Reed Solomon control structure */
 129static struct rs_control *rs_decoder;
 130
 131/*
 132 * The HW decoder in the DoC ASIC's provides us a error syndrome,
 133 * which we must convert to a standard syndrome usable by the generic
 134 * Reed-Solomon library code.
 135 *
 136 * Fabrice Bellard figured this out in the old docecc code. I added
 137 * some comments, improved a minor bit and converted it to make use
 138 * of the generic Reed-Solomon library. tglx
 139 */
 140static int doc_ecc_decode(struct rs_control *rs, uint8_t *data, uint8_t *ecc)
 141{
 142        int i, j, nerr, errpos[8];
 143        uint8_t parity;
 144        uint16_t ds[4], s[5], tmp, errval[8], syn[4];
 145
 146        memset(syn, 0, sizeof(syn));
 147        /* Convert the ecc bytes into words */
 148        ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
 149        ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
 150        ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
 151        ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
 152        parity = ecc[1];
 153
 154        /* Initialize the syndrome buffer */
 155        for (i = 0; i < NROOTS; i++)
 156                s[i] = ds[0];
 157        /*
 158         *  Evaluate
 159         *  s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
 160         *  where x = alpha^(FCR + i)
 161         */
 162        for (j = 1; j < NROOTS; j++) {
 163                if (ds[j] == 0)
 164                        continue;
 165                tmp = rs->index_of[ds[j]];
 166                for (i = 0; i < NROOTS; i++)
 167                        s[i] ^= rs->alpha_to[rs_modnn(rs, tmp + (FCR + i) * j)];
 168        }
 169
 170        /* Calc syn[i] = s[i] / alpha^(v + i) */
 171        for (i = 0; i < NROOTS; i++) {
 172                if (s[i])
 173                        syn[i] = rs_modnn(rs, rs->index_of[s[i]] + (NN - FCR - i));
 174        }
 175        /* Call the decoder library */
 176        nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);
 177
 178        /* Incorrectable errors ? */
 179        if (nerr < 0)
 180                return nerr;
 181
 182        /*
 183         * Correct the errors. The bitpositions are a bit of magic,
 184         * but they are given by the design of the de/encoder circuit
 185         * in the DoC ASIC's.
 186         */
 187        for (i = 0; i < nerr; i++) {
 188                int index, bitpos, pos = 1015 - errpos[i];
 189                uint8_t val;
 190                if (pos >= NB_DATA && pos < 1019)
 191                        continue;
 192                if (pos < NB_DATA) {
 193                        /* extract bit position (MSB first) */
 194                        pos = 10 * (NB_DATA - 1 - pos) - 6;
 195                        /* now correct the following 10 bits. At most two bytes
 196                           can be modified since pos is even */
 197                        index = (pos >> 3) ^ 1;
 198                        bitpos = pos & 7;
 199                        if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
 200                                val = (uint8_t) (errval[i] >> (2 + bitpos));
 201                                parity ^= val;
 202                                if (index < SECTOR_SIZE)
 203                                        data[index] ^= val;
 204                        }
 205                        index = ((pos >> 3) + 1) ^ 1;
 206                        bitpos = (bitpos + 10) & 7;
 207                        if (bitpos == 0)
 208                                bitpos = 8;
 209                        if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
 210                                val = (uint8_t) (errval[i] << (8 - bitpos));
 211                                parity ^= val;
 212                                if (index < SECTOR_SIZE)
 213                                        data[index] ^= val;
 214                        }
 215                }
 216        }
 217        /* If the parity is wrong, no rescue possible */
 218        return parity ? -EBADMSG : nerr;
 219}
 220
 221static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
 222{
 223        volatile char dummy;
 224        int i;
 225
 226        for (i = 0; i < cycles; i++) {
 227                if (DoC_is_Millennium(doc))
 228                        dummy = ReadDOC(doc->virtadr, NOP);
 229                else if (DoC_is_MillenniumPlus(doc))
 230                        dummy = ReadDOC(doc->virtadr, Mplus_NOP);
 231                else
 232                        dummy = ReadDOC(doc->virtadr, DOCStatus);
 233        }
 234
 235}
 236
 237#define CDSN_CTRL_FR_B_MASK     (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
 238
 239/* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
 240static int _DoC_WaitReady(struct doc_priv *doc)
 241{
 242        void __iomem *docptr = doc->virtadr;
 243        unsigned long timeo = jiffies + (HZ * 10);
 244
 245        if (debug)
 246                printk("_DoC_WaitReady...\n");
 247        /* Out-of-line routine to wait for chip response */
 248        if (DoC_is_MillenniumPlus(doc)) {
 249                while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
 250                        if (time_after(jiffies, timeo)) {
 251                                printk("_DoC_WaitReady timed out.\n");
 252                                return -EIO;
 253                        }
 254                        udelay(1);
 255                        cond_resched();
 256                }
 257        } else {
 258                while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
 259                        if (time_after(jiffies, timeo)) {
 260                                printk("_DoC_WaitReady timed out.\n");
 261                                return -EIO;
 262                        }
 263                        udelay(1);
 264                        cond_resched();
 265                }
 266        }
 267
 268        return 0;
 269}
 270
 271static inline int DoC_WaitReady(struct doc_priv *doc)
 272{
 273        void __iomem *docptr = doc->virtadr;
 274        int ret = 0;
 275
 276        if (DoC_is_MillenniumPlus(doc)) {
 277                DoC_Delay(doc, 4);
 278
 279                if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
 280                        /* Call the out-of-line routine to wait */
 281                        ret = _DoC_WaitReady(doc);
 282        } else {
 283                DoC_Delay(doc, 4);
 284
 285                if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
 286                        /* Call the out-of-line routine to wait */
 287                        ret = _DoC_WaitReady(doc);
 288                DoC_Delay(doc, 2);
 289        }
 290
 291        if (debug)
 292                printk("DoC_WaitReady OK\n");
 293        return ret;
 294}
 295
 296static void doc2000_write_byte(struct mtd_info *mtd, u_char datum)
 297{
 298        struct nand_chip *this = mtd_to_nand(mtd);
 299        struct doc_priv *doc = nand_get_controller_data(this);
 300        void __iomem *docptr = doc->virtadr;
 301
 302        if (debug)
 303                printk("write_byte %02x\n", datum);
 304        WriteDOC(datum, docptr, CDSNSlowIO);
 305        WriteDOC(datum, docptr, 2k_CDSN_IO);
 306}
 307
 308static u_char doc2000_read_byte(struct mtd_info *mtd)
 309{
 310        struct nand_chip *this = mtd_to_nand(mtd);
 311        struct doc_priv *doc = nand_get_controller_data(this);
 312        void __iomem *docptr = doc->virtadr;
 313        u_char ret;
 314
 315        ReadDOC(docptr, CDSNSlowIO);
 316        DoC_Delay(doc, 2);
 317        ret = ReadDOC(docptr, 2k_CDSN_IO);
 318        if (debug)
 319                printk("read_byte returns %02x\n", ret);
 320        return ret;
 321}
 322
 323static void doc2000_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
 324{
 325        struct nand_chip *this = mtd_to_nand(mtd);
 326        struct doc_priv *doc = nand_get_controller_data(this);
 327        void __iomem *docptr = doc->virtadr;
 328        int i;
 329        if (debug)
 330                printk("writebuf of %d bytes: ", len);
 331        for (i = 0; i < len; i++) {
 332                WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
 333                if (debug && i < 16)
 334                        printk("%02x ", buf[i]);
 335        }
 336        if (debug)
 337                printk("\n");
 338}
 339
 340static void doc2000_readbuf(struct mtd_info *mtd, u_char *buf, int len)
 341{
 342        struct nand_chip *this = mtd_to_nand(mtd);
 343        struct doc_priv *doc = nand_get_controller_data(this);
 344        void __iomem *docptr = doc->virtadr;
 345        int i;
 346
 347        if (debug)
 348                printk("readbuf of %d bytes: ", len);
 349
 350        for (i = 0; i < len; i++) {
 351                buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
 352        }
 353}
 354
 355static void doc2000_readbuf_dword(struct mtd_info *mtd, u_char *buf, int len)
 356{
 357        struct nand_chip *this = mtd_to_nand(mtd);
 358        struct doc_priv *doc = nand_get_controller_data(this);
 359        void __iomem *docptr = doc->virtadr;
 360        int i;
 361
 362        if (debug)
 363                printk("readbuf_dword of %d bytes: ", len);
 364
 365        if (unlikely((((unsigned long)buf) | len) & 3)) {
 366                for (i = 0; i < len; i++) {
 367                        *(uint8_t *) (&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
 368                }
 369        } else {
 370                for (i = 0; i < len; i += 4) {
 371                        *(uint32_t *) (&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
 372                }
 373        }
 374}
 375
 376static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
 377{
 378        struct nand_chip *this = mtd_to_nand(mtd);
 379        struct doc_priv *doc = nand_get_controller_data(this);
 380        uint16_t ret;
 381
 382        doc200x_select_chip(mtd, nr);
 383        doc200x_hwcontrol(mtd, NAND_CMD_READID,
 384                          NAND_CTRL_CLE | NAND_CTRL_CHANGE);
 385        doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
 386        doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
 387
 388        /* We can't use dev_ready here, but at least we wait for the
 389         * command to complete
 390         */
 391        udelay(50);
 392
 393        ret = this->read_byte(mtd) << 8;
 394        ret |= this->read_byte(mtd);
 395
 396        if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
 397                /* First chip probe. See if we get same results by 32-bit access */
 398                union {
 399                        uint32_t dword;
 400                        uint8_t byte[4];
 401                } ident;
 402                void __iomem *docptr = doc->virtadr;
 403
 404                doc200x_hwcontrol(mtd, NAND_CMD_READID,
 405                                  NAND_CTRL_CLE | NAND_CTRL_CHANGE);
 406                doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
 407                doc200x_hwcontrol(mtd, NAND_CMD_NONE,
 408                                  NAND_NCE | NAND_CTRL_CHANGE);
 409
 410                udelay(50);
 411
 412                ident.dword = readl(docptr + DoC_2k_CDSN_IO);
 413                if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
 414                        printk(KERN_INFO "DiskOnChip 2000 responds to DWORD access\n");
 415                        this->read_buf = &doc2000_readbuf_dword;
 416                }
 417        }
 418
 419        return ret;
 420}
 421
 422static void __init doc2000_count_chips(struct mtd_info *mtd)
 423{
 424        struct nand_chip *this = mtd_to_nand(mtd);
 425        struct doc_priv *doc = nand_get_controller_data(this);
 426        uint16_t mfrid;
 427        int i;
 428
 429        /* Max 4 chips per floor on DiskOnChip 2000 */
 430        doc->chips_per_floor = 4;
 431
 432        /* Find out what the first chip is */
 433        mfrid = doc200x_ident_chip(mtd, 0);
 434
 435        /* Find how many chips in each floor. */
 436        for (i = 1; i < 4; i++) {
 437                if (doc200x_ident_chip(mtd, i) != mfrid)
 438                        break;
 439        }
 440        doc->chips_per_floor = i;
 441        printk(KERN_DEBUG "Detected %d chips per floor.\n", i);
 442}
 443
 444static int doc200x_wait(struct mtd_info *mtd, struct nand_chip *this)
 445{
 446        struct doc_priv *doc = nand_get_controller_data(this);
 447
 448        int status;
 449
 450        DoC_WaitReady(doc);
 451        this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
 452        DoC_WaitReady(doc);
 453        status = (int)this->read_byte(mtd);
 454
 455        return status;
 456}
 457
 458static void doc2001_write_byte(struct mtd_info *mtd, u_char datum)
 459{
 460        struct nand_chip *this = mtd_to_nand(mtd);
 461        struct doc_priv *doc = nand_get_controller_data(this);
 462        void __iomem *docptr = doc->virtadr;
 463
 464        WriteDOC(datum, docptr, CDSNSlowIO);
 465        WriteDOC(datum, docptr, Mil_CDSN_IO);
 466        WriteDOC(datum, docptr, WritePipeTerm);
 467}
 468
 469static u_char doc2001_read_byte(struct mtd_info *mtd)
 470{
 471        struct nand_chip *this = mtd_to_nand(mtd);
 472        struct doc_priv *doc = nand_get_controller_data(this);
 473        void __iomem *docptr = doc->virtadr;
 474
 475        //ReadDOC(docptr, CDSNSlowIO);
 476        /* 11.4.5 -- delay twice to allow extended length cycle */
 477        DoC_Delay(doc, 2);
 478        ReadDOC(docptr, ReadPipeInit);
 479        //return ReadDOC(docptr, Mil_CDSN_IO);
 480        return ReadDOC(docptr, LastDataRead);
 481}
 482
 483static void doc2001_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
 484{
 485        struct nand_chip *this = mtd_to_nand(mtd);
 486        struct doc_priv *doc = nand_get_controller_data(this);
 487        void __iomem *docptr = doc->virtadr;
 488        int i;
 489
 490        for (i = 0; i < len; i++)
 491                WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
 492        /* Terminate write pipeline */
 493        WriteDOC(0x00, docptr, WritePipeTerm);
 494}
 495
 496static void doc2001_readbuf(struct mtd_info *mtd, u_char *buf, int len)
 497{
 498        struct nand_chip *this = mtd_to_nand(mtd);
 499        struct doc_priv *doc = nand_get_controller_data(this);
 500        void __iomem *docptr = doc->virtadr;
 501        int i;
 502
 503        /* Start read pipeline */
 504        ReadDOC(docptr, ReadPipeInit);
 505
 506        for (i = 0; i < len - 1; i++)
 507                buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
 508
 509        /* Terminate read pipeline */
 510        buf[i] = ReadDOC(docptr, LastDataRead);
 511}
 512
 513static u_char doc2001plus_read_byte(struct mtd_info *mtd)
 514{
 515        struct nand_chip *this = mtd_to_nand(mtd);
 516        struct doc_priv *doc = nand_get_controller_data(this);
 517        void __iomem *docptr = doc->virtadr;
 518        u_char ret;
 519
 520        ReadDOC(docptr, Mplus_ReadPipeInit);
 521        ReadDOC(docptr, Mplus_ReadPipeInit);
 522        ret = ReadDOC(docptr, Mplus_LastDataRead);
 523        if (debug)
 524                printk("read_byte returns %02x\n", ret);
 525        return ret;
 526}
 527
 528static void doc2001plus_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
 529{
 530        struct nand_chip *this = mtd_to_nand(mtd);
 531        struct doc_priv *doc = nand_get_controller_data(this);
 532        void __iomem *docptr = doc->virtadr;
 533        int i;
 534
 535        if (debug)
 536                printk("writebuf of %d bytes: ", len);
 537        for (i = 0; i < len; i++) {
 538                WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
 539                if (debug && i < 16)
 540                        printk("%02x ", buf[i]);
 541        }
 542        if (debug)
 543                printk("\n");
 544}
 545
 546static void doc2001plus_readbuf(struct mtd_info *mtd, u_char *buf, int len)
 547{
 548        struct nand_chip *this = mtd_to_nand(mtd);
 549        struct doc_priv *doc = nand_get_controller_data(this);
 550        void __iomem *docptr = doc->virtadr;
 551        int i;
 552
 553        if (debug)
 554                printk("readbuf of %d bytes: ", len);
 555
 556        /* Start read pipeline */
 557        ReadDOC(docptr, Mplus_ReadPipeInit);
 558        ReadDOC(docptr, Mplus_ReadPipeInit);
 559
 560        for (i = 0; i < len - 2; i++) {
 561                buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
 562                if (debug && i < 16)
 563                        printk("%02x ", buf[i]);
 564        }
 565
 566        /* Terminate read pipeline */
 567        buf[len - 2] = ReadDOC(docptr, Mplus_LastDataRead);
 568        if (debug && i < 16)
 569                printk("%02x ", buf[len - 2]);
 570        buf[len - 1] = ReadDOC(docptr, Mplus_LastDataRead);
 571        if (debug && i < 16)
 572                printk("%02x ", buf[len - 1]);
 573        if (debug)
 574                printk("\n");
 575}
 576
 577static void doc2001plus_select_chip(struct mtd_info *mtd, int chip)
 578{
 579        struct nand_chip *this = mtd_to_nand(mtd);
 580        struct doc_priv *doc = nand_get_controller_data(this);
 581        void __iomem *docptr = doc->virtadr;
 582        int floor = 0;
 583
 584        if (debug)
 585                printk("select chip (%d)\n", chip);
 586
 587        if (chip == -1) {
 588                /* Disable flash internally */
 589                WriteDOC(0, docptr, Mplus_FlashSelect);
 590                return;
 591        }
 592
 593        floor = chip / doc->chips_per_floor;
 594        chip -= (floor * doc->chips_per_floor);
 595
 596        /* Assert ChipEnable and deassert WriteProtect */
 597        WriteDOC((DOC_FLASH_CE), docptr, Mplus_FlashSelect);
 598        this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
 599
 600        doc->curchip = chip;
 601        doc->curfloor = floor;
 602}
 603
 604static void doc200x_select_chip(struct mtd_info *mtd, int chip)
 605{
 606        struct nand_chip *this = mtd_to_nand(mtd);
 607        struct doc_priv *doc = nand_get_controller_data(this);
 608        void __iomem *docptr = doc->virtadr;
 609        int floor = 0;
 610
 611        if (debug)
 612                printk("select chip (%d)\n", chip);
 613
 614        if (chip == -1)
 615                return;
 616
 617        floor = chip / doc->chips_per_floor;
 618        chip -= (floor * doc->chips_per_floor);
 619
 620        /* 11.4.4 -- deassert CE before changing chip */
 621        doc200x_hwcontrol(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
 622
 623        WriteDOC(floor, docptr, FloorSelect);
 624        WriteDOC(chip, docptr, CDSNDeviceSelect);
 625
 626        doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
 627
 628        doc->curchip = chip;
 629        doc->curfloor = floor;
 630}
 631
 632#define CDSN_CTRL_MSK (CDSN_CTRL_CE | CDSN_CTRL_CLE | CDSN_CTRL_ALE)
 633
 634static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
 635                              unsigned int ctrl)
 636{
 637        struct nand_chip *this = mtd_to_nand(mtd);
 638        struct doc_priv *doc = nand_get_controller_data(this);
 639        void __iomem *docptr = doc->virtadr;
 640
 641        if (ctrl & NAND_CTRL_CHANGE) {
 642                doc->CDSNControl &= ~CDSN_CTRL_MSK;
 643                doc->CDSNControl |= ctrl & CDSN_CTRL_MSK;
 644                if (debug)
 645                        printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
 646                WriteDOC(doc->CDSNControl, docptr, CDSNControl);
 647                /* 11.4.3 -- 4 NOPs after CSDNControl write */
 648                DoC_Delay(doc, 4);
 649        }
 650        if (cmd != NAND_CMD_NONE) {
 651                if (DoC_is_2000(doc))
 652                        doc2000_write_byte(mtd, cmd);
 653                else
 654                        doc2001_write_byte(mtd, cmd);
 655        }
 656}
 657
 658static void doc2001plus_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
 659{
 660        struct nand_chip *this = mtd_to_nand(mtd);
 661        struct doc_priv *doc = nand_get_controller_data(this);
 662        void __iomem *docptr = doc->virtadr;
 663
 664        /*
 665         * Must terminate write pipeline before sending any commands
 666         * to the device.
 667         */
 668        if (command == NAND_CMD_PAGEPROG) {
 669                WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
 670                WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
 671        }
 672
 673        /*
 674         * Write out the command to the device.
 675         */
 676        if (command == NAND_CMD_SEQIN) {
 677                int readcmd;
 678
 679                if (column >= mtd->writesize) {
 680                        /* OOB area */
 681                        column -= mtd->writesize;
 682                        readcmd = NAND_CMD_READOOB;
 683                } else if (column < 256) {
 684                        /* First 256 bytes --> READ0 */
 685                        readcmd = NAND_CMD_READ0;
 686                } else {
 687                        column -= 256;
 688                        readcmd = NAND_CMD_READ1;
 689                }
 690                WriteDOC(readcmd, docptr, Mplus_FlashCmd);
 691        }
 692        WriteDOC(command, docptr, Mplus_FlashCmd);
 693        WriteDOC(0, docptr, Mplus_WritePipeTerm);
 694        WriteDOC(0, docptr, Mplus_WritePipeTerm);
 695
 696        if (column != -1 || page_addr != -1) {
 697                /* Serially input address */
 698                if (column != -1) {
 699                        /* Adjust columns for 16 bit buswidth */
 700                        if (this->options & NAND_BUSWIDTH_16 &&
 701                                        !nand_opcode_8bits(command))
 702                                column >>= 1;
 703                        WriteDOC(column, docptr, Mplus_FlashAddress);
 704                }
 705                if (page_addr != -1) {
 706                        WriteDOC((unsigned char)(page_addr & 0xff), docptr, Mplus_FlashAddress);
 707                        WriteDOC((unsigned char)((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
 708                        /* One more address cycle for higher density devices */
 709                        if (this->chipsize & 0x0c000000) {
 710                                WriteDOC((unsigned char)((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
 711                                printk("high density\n");
 712                        }
 713                }
 714                WriteDOC(0, docptr, Mplus_WritePipeTerm);
 715                WriteDOC(0, docptr, Mplus_WritePipeTerm);
 716                /* deassert ALE */
 717                if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
 718                    command == NAND_CMD_READOOB || command == NAND_CMD_READID)
 719                        WriteDOC(0, docptr, Mplus_FlashControl);
 720        }
 721
 722        /*
 723         * program and erase have their own busy handlers
 724         * status and sequential in needs no delay
 725         */
 726        switch (command) {
 727
 728        case NAND_CMD_PAGEPROG:
 729        case NAND_CMD_ERASE1:
 730        case NAND_CMD_ERASE2:
 731        case NAND_CMD_SEQIN:
 732        case NAND_CMD_STATUS:
 733                return;
 734
 735        case NAND_CMD_RESET:
 736                if (this->dev_ready)
 737                        break;
 738                udelay(this->chip_delay);
 739                WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd);
 740                WriteDOC(0, docptr, Mplus_WritePipeTerm);
 741                WriteDOC(0, docptr, Mplus_WritePipeTerm);
 742                while (!(this->read_byte(mtd) & 0x40)) ;
 743                return;
 744
 745                /* This applies to read commands */
 746        default:
 747                /*
 748                 * If we don't have access to the busy pin, we apply the given
 749                 * command delay
 750                 */
 751                if (!this->dev_ready) {
 752                        udelay(this->chip_delay);
 753                        return;
 754                }
 755        }
 756
 757        /* Apply this short delay always to ensure that we do wait tWB in
 758         * any case on any machine. */
 759        ndelay(100);
 760        /* wait until command is processed */
 761        while (!this->dev_ready(mtd)) ;
 762}
 763
 764static int doc200x_dev_ready(struct mtd_info *mtd)
 765{
 766        struct nand_chip *this = mtd_to_nand(mtd);
 767        struct doc_priv *doc = nand_get_controller_data(this);
 768        void __iomem *docptr = doc->virtadr;
 769
 770        if (DoC_is_MillenniumPlus(doc)) {
 771                /* 11.4.2 -- must NOP four times before checking FR/B# */
 772                DoC_Delay(doc, 4);
 773                if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
 774                        if (debug)
 775                                printk("not ready\n");
 776                        return 0;
 777                }
 778                if (debug)
 779                        printk("was ready\n");
 780                return 1;
 781        } else {
 782                /* 11.4.2 -- must NOP four times before checking FR/B# */
 783                DoC_Delay(doc, 4);
 784                if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
 785                        if (debug)
 786                                printk("not ready\n");
 787                        return 0;
 788                }
 789                /* 11.4.2 -- Must NOP twice if it's ready */
 790                DoC_Delay(doc, 2);
 791                if (debug)
 792                        printk("was ready\n");
 793                return 1;
 794        }
 795}
 796
 797static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs)
 798{
 799        /* This is our last resort if we couldn't find or create a BBT.  Just
 800           pretend all blocks are good. */
 801        return 0;
 802}
 803
 804static void doc200x_enable_hwecc(struct mtd_info *mtd, int mode)
 805{
 806        struct nand_chip *this = mtd_to_nand(mtd);
 807        struct doc_priv *doc = nand_get_controller_data(this);
 808        void __iomem *docptr = doc->virtadr;
 809
 810        /* Prime the ECC engine */
 811        switch (mode) {
 812        case NAND_ECC_READ:
 813                WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
 814                WriteDOC(DOC_ECC_EN, docptr, ECCConf);
 815                break;
 816        case NAND_ECC_WRITE:
 817                WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
 818                WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
 819                break;
 820        }
 821}
 822
 823static void doc2001plus_enable_hwecc(struct mtd_info *mtd, int mode)
 824{
 825        struct nand_chip *this = mtd_to_nand(mtd);
 826        struct doc_priv *doc = nand_get_controller_data(this);
 827        void __iomem *docptr = doc->virtadr;
 828
 829        /* Prime the ECC engine */
 830        switch (mode) {
 831        case NAND_ECC_READ:
 832                WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
 833                WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
 834                break;
 835        case NAND_ECC_WRITE:
 836                WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
 837                WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
 838                break;
 839        }
 840}
 841
 842/* This code is only called on write */
 843static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat, unsigned char *ecc_code)
 844{
 845        struct nand_chip *this = mtd_to_nand(mtd);
 846        struct doc_priv *doc = nand_get_controller_data(this);
 847        void __iomem *docptr = doc->virtadr;
 848        int i;
 849        int emptymatch = 1;
 850
 851        /* flush the pipeline */
 852        if (DoC_is_2000(doc)) {
 853                WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
 854                WriteDOC(0, docptr, 2k_CDSN_IO);
 855                WriteDOC(0, docptr, 2k_CDSN_IO);
 856                WriteDOC(0, docptr, 2k_CDSN_IO);
 857                WriteDOC(doc->CDSNControl, docptr, CDSNControl);
 858        } else if (DoC_is_MillenniumPlus(doc)) {
 859                WriteDOC(0, docptr, Mplus_NOP);
 860                WriteDOC(0, docptr, Mplus_NOP);
 861                WriteDOC(0, docptr, Mplus_NOP);
 862        } else {
 863                WriteDOC(0, docptr, NOP);
 864                WriteDOC(0, docptr, NOP);
 865                WriteDOC(0, docptr, NOP);
 866        }
 867
 868        for (i = 0; i < 6; i++) {
 869                if (DoC_is_MillenniumPlus(doc))
 870                        ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
 871                else
 872                        ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
 873                if (ecc_code[i] != empty_write_ecc[i])
 874                        emptymatch = 0;
 875        }
 876        if (DoC_is_MillenniumPlus(doc))
 877                WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
 878        else
 879                WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
 880#if 0
 881        /* If emptymatch=1, we might have an all-0xff data buffer.  Check. */
 882        if (emptymatch) {
 883                /* Note: this somewhat expensive test should not be triggered
 884                   often.  It could be optimized away by examining the data in
 885                   the writebuf routine, and remembering the result. */
 886                for (i = 0; i < 512; i++) {
 887                        if (dat[i] == 0xff)
 888                                continue;
 889                        emptymatch = 0;
 890                        break;
 891                }
 892        }
 893        /* If emptymatch still =1, we do have an all-0xff data buffer.
 894           Return all-0xff ecc value instead of the computed one, so
 895           it'll look just like a freshly-erased page. */
 896        if (emptymatch)
 897                memset(ecc_code, 0xff, 6);
 898#endif
 899        return 0;
 900}
 901
 902static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat,
 903                                u_char *read_ecc, u_char *isnull)
 904{
 905        int i, ret = 0;
 906        struct nand_chip *this = mtd_to_nand(mtd);
 907        struct doc_priv *doc = nand_get_controller_data(this);
 908        void __iomem *docptr = doc->virtadr;
 909        uint8_t calc_ecc[6];
 910        volatile u_char dummy;
 911
 912        /* flush the pipeline */
 913        if (DoC_is_2000(doc)) {
 914                dummy = ReadDOC(docptr, 2k_ECCStatus);
 915                dummy = ReadDOC(docptr, 2k_ECCStatus);
 916                dummy = ReadDOC(docptr, 2k_ECCStatus);
 917        } else if (DoC_is_MillenniumPlus(doc)) {
 918                dummy = ReadDOC(docptr, Mplus_ECCConf);
 919                dummy = ReadDOC(docptr, Mplus_ECCConf);
 920                dummy = ReadDOC(docptr, Mplus_ECCConf);
 921        } else {
 922                dummy = ReadDOC(docptr, ECCConf);
 923                dummy = ReadDOC(docptr, ECCConf);
 924                dummy = ReadDOC(docptr, ECCConf);
 925        }
 926
 927        /* Error occurred ? */
 928        if (dummy & 0x80) {
 929                for (i = 0; i < 6; i++) {
 930                        if (DoC_is_MillenniumPlus(doc))
 931                                calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
 932                        else
 933                                calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
 934                }
 935
 936                ret = doc_ecc_decode(rs_decoder, dat, calc_ecc);
 937                if (ret > 0)
 938                        printk(KERN_ERR "doc200x_correct_data corrected %d errors\n", ret);
 939        }
 940        if (DoC_is_MillenniumPlus(doc))
 941                WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
 942        else
 943                WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
 944        if (no_ecc_failures && mtd_is_eccerr(ret)) {
 945                printk(KERN_ERR "suppressing ECC failure\n");
 946                ret = 0;
 947        }
 948        return ret;
 949}
 950
 951//u_char mydatabuf[528];
 952
 953/* The strange out-of-order .oobfree list below is a (possibly unneeded)
 954 * attempt to retain compatibility.  It used to read:
 955 *      .oobfree = { {8, 8} }
 956 * Since that leaves two bytes unusable, it was changed.  But the following
 957 * scheme might affect existing jffs2 installs by moving the cleanmarker:
 958 *      .oobfree = { {6, 10} }
 959 * jffs2 seems to handle the above gracefully, but the current scheme seems
 960 * safer.  The only problem with it is that any code that parses oobfree must
 961 * be able to handle out-of-order segments.
 962 */
 963static struct nand_ecclayout doc200x_oobinfo = {
 964        .eccbytes = 6,
 965        .eccpos = {0, 1, 2, 3, 4, 5},
 966        .oobfree = {{8, 8}, {6, 2}}
 967};
 968
 969/* Find the (I)NFTL Media Header, and optionally also the mirror media header.
 970   On successful return, buf will contain a copy of the media header for
 971   further processing.  id is the string to scan for, and will presumably be
 972   either "ANAND" or "BNAND".  If findmirror=1, also look for the mirror media
 973   header.  The page #s of the found media headers are placed in mh0_page and
 974   mh1_page in the DOC private structure. */
 975static int __init find_media_headers(struct mtd_info *mtd, u_char *buf, const char *id, int findmirror)
 976{
 977        struct nand_chip *this = mtd_to_nand(mtd);
 978        struct doc_priv *doc = nand_get_controller_data(this);
 979        unsigned offs;
 980        int ret;
 981        size_t retlen;
 982
 983        for (offs = 0; offs < mtd->size; offs += mtd->erasesize) {
 984                ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
 985                if (retlen != mtd->writesize)
 986                        continue;
 987                if (ret) {
 988                        printk(KERN_WARNING "ECC error scanning DOC at 0x%x\n", offs);
 989                }
 990                if (memcmp(buf, id, 6))
 991                        continue;
 992                printk(KERN_INFO "Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
 993                if (doc->mh0_page == -1) {
 994                        doc->mh0_page = offs >> this->page_shift;
 995                        if (!findmirror)
 996                                return 1;
 997                        continue;
 998                }
 999                doc->mh1_page = offs >> this->page_shift;
1000                return 2;
1001        }
1002        if (doc->mh0_page == -1) {
1003                printk(KERN_WARNING "DiskOnChip %s Media Header not found.\n", id);
1004                return 0;
1005        }
1006        /* Only one mediaheader was found.  We want buf to contain a
1007           mediaheader on return, so we'll have to re-read the one we found. */
1008        offs = doc->mh0_page << this->page_shift;
1009        ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
1010        if (retlen != mtd->writesize) {
1011                /* Insanity.  Give up. */
1012                printk(KERN_ERR "Read DiskOnChip Media Header once, but can't reread it???\n");
1013                return 0;
1014        }
1015        return 1;
1016}
1017
1018static inline int __init nftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
1019{
1020        struct nand_chip *this = mtd_to_nand(mtd);
1021        struct doc_priv *doc = nand_get_controller_data(this);
1022        int ret = 0;
1023        u_char *buf;
1024        struct NFTLMediaHeader *mh;
1025        const unsigned psize = 1 << this->page_shift;
1026        int numparts = 0;
1027        unsigned blocks, maxblocks;
1028        int offs, numheaders;
1029
1030        buf = kmalloc(mtd->writesize, GFP_KERNEL);
1031        if (!buf) {
1032                return 0;
1033        }
1034        if (!(numheaders = find_media_headers(mtd, buf, "ANAND", 1)))
1035                goto out;
1036        mh = (struct NFTLMediaHeader *)buf;
1037
1038        le16_to_cpus(&mh->NumEraseUnits);
1039        le16_to_cpus(&mh->FirstPhysicalEUN);
1040        le32_to_cpus(&mh->FormattedSize);
1041
1042        printk(KERN_INFO "    DataOrgID        = %s\n"
1043                         "    NumEraseUnits    = %d\n"
1044                         "    FirstPhysicalEUN = %d\n"
1045                         "    FormattedSize    = %d\n"
1046                         "    UnitSizeFactor   = %d\n",
1047                mh->DataOrgID, mh->NumEraseUnits,
1048                mh->FirstPhysicalEUN, mh->FormattedSize,
1049                mh->UnitSizeFactor);
1050
1051        blocks = mtd->size >> this->phys_erase_shift;
1052        maxblocks = min(32768U, mtd->erasesize - psize);
1053
1054        if (mh->UnitSizeFactor == 0x00) {
1055                /* Auto-determine UnitSizeFactor.  The constraints are:
1056                   - There can be at most 32768 virtual blocks.
1057                   - There can be at most (virtual block size - page size)
1058                   virtual blocks (because MediaHeader+BBT must fit in 1).
1059                 */
1060                mh->UnitSizeFactor = 0xff;
1061                while (blocks > maxblocks) {
1062                        blocks >>= 1;
1063                        maxblocks = min(32768U, (maxblocks << 1) + psize);
1064                        mh->UnitSizeFactor--;
1065                }
1066                printk(KERN_WARNING "UnitSizeFactor=0x00 detected.  Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
1067        }
1068
1069        /* NOTE: The lines below modify internal variables of the NAND and MTD
1070           layers; variables with have already been configured by nand_scan.
1071           Unfortunately, we didn't know before this point what these values
1072           should be.  Thus, this code is somewhat dependent on the exact
1073           implementation of the NAND layer.  */
1074        if (mh->UnitSizeFactor != 0xff) {
1075                this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
1076                mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
1077                printk(KERN_INFO "Setting virtual erase size to %d\n", mtd->erasesize);
1078                blocks = mtd->size >> this->bbt_erase_shift;
1079                maxblocks = min(32768U, mtd->erasesize - psize);
1080        }
1081
1082        if (blocks > maxblocks) {
1083                printk(KERN_ERR "UnitSizeFactor of 0x%02x is inconsistent with device size.  Aborting.\n", mh->UnitSizeFactor);
1084                goto out;
1085        }
1086
1087        /* Skip past the media headers. */
1088        offs = max(doc->mh0_page, doc->mh1_page);
1089        offs <<= this->page_shift;
1090        offs += mtd->erasesize;
1091
1092        if (show_firmware_partition == 1) {
1093                parts[0].name = " DiskOnChip Firmware / Media Header partition";
1094                parts[0].offset = 0;
1095                parts[0].size = offs;
1096                numparts = 1;
1097        }
1098
1099        parts[numparts].name = " DiskOnChip BDTL partition";
1100        parts[numparts].offset = offs;
1101        parts[numparts].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
1102
1103        offs += parts[numparts].size;
1104        numparts++;
1105
1106        if (offs < mtd->size) {
1107                parts[numparts].name = " DiskOnChip Remainder partition";
1108                parts[numparts].offset = offs;
1109                parts[numparts].size = mtd->size - offs;
1110                numparts++;
1111        }
1112
1113        ret = numparts;
1114 out:
1115        kfree(buf);
1116        return ret;
1117}
1118
1119/* This is a stripped-down copy of the code in inftlmount.c */
1120static inline int __init inftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
1121{
1122        struct nand_chip *this = mtd_to_nand(mtd);
1123        struct doc_priv *doc = nand_get_controller_data(this);
1124        int ret = 0;
1125        u_char *buf;
1126        struct INFTLMediaHeader *mh;
1127        struct INFTLPartition *ip;
1128        int numparts = 0;
1129        int blocks;
1130        int vshift, lastvunit = 0;
1131        int i;
1132        int end = mtd->size;
1133
1134        if (inftl_bbt_write)
1135                end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
1136
1137        buf = kmalloc(mtd->writesize, GFP_KERNEL);
1138        if (!buf) {
1139                return 0;
1140        }
1141
1142        if (!find_media_headers(mtd, buf, "BNAND", 0))
1143                goto out;
1144        doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
1145        mh = (struct INFTLMediaHeader *)buf;
1146
1147        le32_to_cpus(&mh->NoOfBootImageBlocks);
1148        le32_to_cpus(&mh->NoOfBinaryPartitions);
1149        le32_to_cpus(&mh->NoOfBDTLPartitions);
1150        le32_to_cpus(&mh->BlockMultiplierBits);
1151        le32_to_cpus(&mh->FormatFlags);
1152        le32_to_cpus(&mh->PercentUsed);
1153
1154        printk(KERN_INFO "    bootRecordID          = %s\n"
1155                         "    NoOfBootImageBlocks   = %d\n"
1156                         "    NoOfBinaryPartitions  = %d\n"
1157                         "    NoOfBDTLPartitions    = %d\n"
1158                         "    BlockMultiplerBits    = %d\n"
1159                         "    FormatFlgs            = %d\n"
1160                         "    OsakVersion           = %d.%d.%d.%d\n"
1161                         "    PercentUsed           = %d\n",
1162                mh->bootRecordID, mh->NoOfBootImageBlocks,
1163                mh->NoOfBinaryPartitions,
1164                mh->NoOfBDTLPartitions,
1165                mh->BlockMultiplierBits, mh->FormatFlags,
1166                ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
1167                ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
1168                ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
1169                ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
1170                mh->PercentUsed);
1171
1172        vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
1173
1174        blocks = mtd->size >> vshift;
1175        if (blocks > 32768) {
1176                printk(KERN_ERR "BlockMultiplierBits=%d is inconsistent with device size.  Aborting.\n", mh->BlockMultiplierBits);
1177                goto out;
1178        }
1179
1180        blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
1181        if (inftl_bbt_write && (blocks > mtd->erasesize)) {
1182                printk(KERN_ERR "Writeable BBTs spanning more than one erase block are not yet supported.  FIX ME!\n");
1183                goto out;
1184        }
1185
1186        /* Scan the partitions */
1187        for (i = 0; (i < 4); i++) {
1188                ip = &(mh->Partitions[i]);
1189                le32_to_cpus(&ip->virtualUnits);
1190                le32_to_cpus(&ip->firstUnit);
1191                le32_to_cpus(&ip->lastUnit);
1192                le32_to_cpus(&ip->flags);
1193                le32_to_cpus(&ip->spareUnits);
1194                le32_to_cpus(&ip->Reserved0);
1195
1196                printk(KERN_INFO        "    PARTITION[%d] ->\n"
1197                        "        virtualUnits    = %d\n"
1198                        "        firstUnit       = %d\n"
1199                        "        lastUnit        = %d\n"
1200                        "        flags           = 0x%x\n"
1201                        "        spareUnits      = %d\n",
1202                        i, ip->virtualUnits, ip->firstUnit,
1203                        ip->lastUnit, ip->flags,
1204                        ip->spareUnits);
1205
1206                if ((show_firmware_partition == 1) &&
1207                    (i == 0) && (ip->firstUnit > 0)) {
1208                        parts[0].name = " DiskOnChip IPL / Media Header partition";
1209                        parts[0].offset = 0;
1210                        parts[0].size = mtd->erasesize * ip->firstUnit;
1211                        numparts = 1;
1212                }
1213
1214                if (ip->flags & INFTL_BINARY)
1215                        parts[numparts].name = " DiskOnChip BDK partition";
1216                else
1217                        parts[numparts].name = " DiskOnChip BDTL partition";
1218                parts[numparts].offset = ip->firstUnit << vshift;
1219                parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
1220                numparts++;
1221                if (ip->lastUnit > lastvunit)
1222                        lastvunit = ip->lastUnit;
1223                if (ip->flags & INFTL_LAST)
1224                        break;
1225        }
1226        lastvunit++;
1227        if ((lastvunit << vshift) < end) {
1228                parts[numparts].name = " DiskOnChip Remainder partition";
1229                parts[numparts].offset = lastvunit << vshift;
1230                parts[numparts].size = end - parts[numparts].offset;
1231                numparts++;
1232        }
1233        ret = numparts;
1234 out:
1235        kfree(buf);
1236        return ret;
1237}
1238
1239static int __init nftl_scan_bbt(struct mtd_info *mtd)
1240{
1241        int ret, numparts;
1242        struct nand_chip *this = mtd_to_nand(mtd);
1243        struct doc_priv *doc = nand_get_controller_data(this);
1244        struct mtd_partition parts[2];
1245
1246        memset((char *)parts, 0, sizeof(parts));
1247        /* On NFTL, we have to find the media headers before we can read the
1248           BBTs, since they're stored in the media header eraseblocks. */
1249        numparts = nftl_partscan(mtd, parts);
1250        if (!numparts)
1251                return -EIO;
1252        this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1253                                NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1254                                NAND_BBT_VERSION;
1255        this->bbt_td->veroffs = 7;
1256        this->bbt_td->pages[0] = doc->mh0_page + 1;
1257        if (doc->mh1_page != -1) {
1258                this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1259                                        NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1260                                        NAND_BBT_VERSION;
1261                this->bbt_md->veroffs = 7;
1262                this->bbt_md->pages[0] = doc->mh1_page + 1;
1263        } else {
1264                this->bbt_md = NULL;
1265        }
1266
1267        ret = this->scan_bbt(mtd);
1268        if (ret)
1269                return ret;
1270
1271        return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts);
1272}
1273
1274static int __init inftl_scan_bbt(struct mtd_info *mtd)
1275{
1276        int ret, numparts;
1277        struct nand_chip *this = mtd_to_nand(mtd);
1278        struct doc_priv *doc = nand_get_controller_data(this);
1279        struct mtd_partition parts[5];
1280
1281        if (this->numchips > doc->chips_per_floor) {
1282                printk(KERN_ERR "Multi-floor INFTL devices not yet supported.\n");
1283                return -EIO;
1284        }
1285
1286        if (DoC_is_MillenniumPlus(doc)) {
1287                this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
1288                if (inftl_bbt_write)
1289                        this->bbt_td->options |= NAND_BBT_WRITE;
1290                this->bbt_td->pages[0] = 2;
1291                this->bbt_md = NULL;
1292        } else {
1293                this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
1294                if (inftl_bbt_write)
1295                        this->bbt_td->options |= NAND_BBT_WRITE;
1296                this->bbt_td->offs = 8;
1297                this->bbt_td->len = 8;
1298                this->bbt_td->veroffs = 7;
1299                this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1300                this->bbt_td->reserved_block_code = 0x01;
1301                this->bbt_td->pattern = "MSYS_BBT";
1302
1303                this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
1304                if (inftl_bbt_write)
1305                        this->bbt_md->options |= NAND_BBT_WRITE;
1306                this->bbt_md->offs = 8;
1307                this->bbt_md->len = 8;
1308                this->bbt_md->veroffs = 7;
1309                this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1310                this->bbt_md->reserved_block_code = 0x01;
1311                this->bbt_md->pattern = "TBB_SYSM";
1312        }
1313
1314        ret = this->scan_bbt(mtd);
1315        if (ret)
1316                return ret;
1317
1318        memset((char *)parts, 0, sizeof(parts));
1319        numparts = inftl_partscan(mtd, parts);
1320        /* At least for now, require the INFTL Media Header.  We could probably
1321           do without it for non-INFTL use, since all it gives us is
1322           autopartitioning, but I want to give it more thought. */
1323        if (!numparts)
1324                return -EIO;
1325        return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts);
1326}
1327
1328static inline int __init doc2000_init(struct mtd_info *mtd)
1329{
1330        struct nand_chip *this = mtd_to_nand(mtd);
1331        struct doc_priv *doc = nand_get_controller_data(this);
1332
1333        this->read_byte = doc2000_read_byte;
1334        this->write_buf = doc2000_writebuf;
1335        this->read_buf = doc2000_readbuf;
1336        doc->late_init = nftl_scan_bbt;
1337
1338        doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
1339        doc2000_count_chips(mtd);
1340        mtd->name = "DiskOnChip 2000 (NFTL Model)";
1341        return (4 * doc->chips_per_floor);
1342}
1343
1344static inline int __init doc2001_init(struct mtd_info *mtd)
1345{
1346        struct nand_chip *this = mtd_to_nand(mtd);
1347        struct doc_priv *doc = nand_get_controller_data(this);
1348
1349        this->read_byte = doc2001_read_byte;
1350        this->write_buf = doc2001_writebuf;
1351        this->read_buf = doc2001_readbuf;
1352
1353        ReadDOC(doc->virtadr, ChipID);
1354        ReadDOC(doc->virtadr, ChipID);
1355        ReadDOC(doc->virtadr, ChipID);
1356        if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1357                /* It's not a Millennium; it's one of the newer
1358                   DiskOnChip 2000 units with a similar ASIC.
1359                   Treat it like a Millennium, except that it
1360                   can have multiple chips. */
1361                doc2000_count_chips(mtd);
1362                mtd->name = "DiskOnChip 2000 (INFTL Model)";
1363                doc->late_init = inftl_scan_bbt;
1364                return (4 * doc->chips_per_floor);
1365        } else {
1366                /* Bog-standard Millennium */
1367                doc->chips_per_floor = 1;
1368                mtd->name = "DiskOnChip Millennium";
1369                doc->late_init = nftl_scan_bbt;
1370                return 1;
1371        }
1372}
1373
1374static inline int __init doc2001plus_init(struct mtd_info *mtd)
1375{
1376        struct nand_chip *this = mtd_to_nand(mtd);
1377        struct doc_priv *doc = nand_get_controller_data(this);
1378
1379        this->read_byte = doc2001plus_read_byte;
1380        this->write_buf = doc2001plus_writebuf;
1381        this->read_buf = doc2001plus_readbuf;
1382        doc->late_init = inftl_scan_bbt;
1383        this->cmd_ctrl = NULL;
1384        this->select_chip = doc2001plus_select_chip;
1385        this->cmdfunc = doc2001plus_command;
1386        this->ecc.hwctl = doc2001plus_enable_hwecc;
1387
1388        doc->chips_per_floor = 1;
1389        mtd->name = "DiskOnChip Millennium Plus";
1390
1391        return 1;
1392}
1393
1394static int __init doc_probe(unsigned long physadr)
1395{
1396        unsigned char ChipID;
1397        struct mtd_info *mtd;
1398        struct nand_chip *nand;
1399        struct doc_priv *doc;
1400        void __iomem *virtadr;
1401        unsigned char save_control;
1402        unsigned char tmp, tmpb, tmpc;
1403        int reg, len, numchips;
1404        int ret = 0;
1405
1406        if (!request_mem_region(physadr, DOC_IOREMAP_LEN, "DiskOnChip"))
1407                return -EBUSY;
1408        virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
1409        if (!virtadr) {
1410                printk(KERN_ERR "Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n", DOC_IOREMAP_LEN, physadr);
1411                ret = -EIO;
1412                goto error_ioremap;
1413        }
1414
1415        /* It's not possible to cleanly detect the DiskOnChip - the
1416         * bootup procedure will put the device into reset mode, and
1417         * it's not possible to talk to it without actually writing
1418         * to the DOCControl register. So we store the current contents
1419         * of the DOCControl register's location, in case we later decide
1420         * that it's not a DiskOnChip, and want to put it back how we
1421         * found it.
1422         */
1423        save_control = ReadDOC(virtadr, DOCControl);
1424
1425        /* Reset the DiskOnChip ASIC */
1426        WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1427        WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1428
1429        /* Enable the DiskOnChip ASIC */
1430        WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1431        WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1432
1433        ChipID = ReadDOC(virtadr, ChipID);
1434
1435        switch (ChipID) {
1436        case DOC_ChipID_Doc2k:
1437                reg = DoC_2k_ECCStatus;
1438                break;
1439        case DOC_ChipID_DocMil:
1440                reg = DoC_ECCConf;
1441                break;
1442        case DOC_ChipID_DocMilPlus16:
1443        case DOC_ChipID_DocMilPlus32:
1444        case 0:
1445                /* Possible Millennium Plus, need to do more checks */
1446                /* Possibly release from power down mode */
1447                for (tmp = 0; (tmp < 4); tmp++)
1448                        ReadDOC(virtadr, Mplus_Power);
1449
1450                /* Reset the Millennium Plus ASIC */
1451                tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
1452                WriteDOC(tmp, virtadr, Mplus_DOCControl);
1453                WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1454
1455                mdelay(1);
1456                /* Enable the Millennium Plus ASIC */
1457                tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
1458                WriteDOC(tmp, virtadr, Mplus_DOCControl);
1459                WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1460                mdelay(1);
1461
1462                ChipID = ReadDOC(virtadr, ChipID);
1463
1464                switch (ChipID) {
1465                case DOC_ChipID_DocMilPlus16:
1466                        reg = DoC_Mplus_Toggle;
1467                        break;
1468                case DOC_ChipID_DocMilPlus32:
1469                        printk(KERN_ERR "DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
1470                default:
1471                        ret = -ENODEV;
1472                        goto notfound;
1473                }
1474                break;
1475
1476        default:
1477                ret = -ENODEV;
1478                goto notfound;
1479        }
1480        /* Check the TOGGLE bit in the ECC register */
1481        tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1482        tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1483        tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1484        if ((tmp == tmpb) || (tmp != tmpc)) {
1485                printk(KERN_WARNING "Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
1486                ret = -ENODEV;
1487                goto notfound;
1488        }
1489
1490        for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1491                unsigned char oldval;
1492                unsigned char newval;
1493                nand = mtd_to_nand(mtd);
1494                doc = nand_get_controller_data(nand);
1495                /* Use the alias resolution register to determine if this is
1496                   in fact the same DOC aliased to a new address.  If writes
1497                   to one chip's alias resolution register change the value on
1498                   the other chip, they're the same chip. */
1499                if (ChipID == DOC_ChipID_DocMilPlus16) {
1500                        oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1501                        newval = ReadDOC(virtadr, Mplus_AliasResolution);
1502                } else {
1503                        oldval = ReadDOC(doc->virtadr, AliasResolution);
1504                        newval = ReadDOC(virtadr, AliasResolution);
1505                }
1506                if (oldval != newval)
1507                        continue;
1508                if (ChipID == DOC_ChipID_DocMilPlus16) {
1509                        WriteDOC(~newval, virtadr, Mplus_AliasResolution);
1510                        oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1511                        WriteDOC(newval, virtadr, Mplus_AliasResolution);       // restore it
1512                } else {
1513                        WriteDOC(~newval, virtadr, AliasResolution);
1514                        oldval = ReadDOC(doc->virtadr, AliasResolution);
1515                        WriteDOC(newval, virtadr, AliasResolution);     // restore it
1516                }
1517                newval = ~newval;
1518                if (oldval == newval) {
1519                        printk(KERN_DEBUG "Found alias of DOC at 0x%lx to 0x%lx\n", doc->physadr, physadr);
1520                        goto notfound;
1521                }
1522        }
1523
1524        printk(KERN_NOTICE "DiskOnChip found at 0x%lx\n", physadr);
1525
1526        len = sizeof(struct nand_chip) + sizeof(struct doc_priv) +
1527              (2 * sizeof(struct nand_bbt_descr));
1528        nand = kzalloc(len, GFP_KERNEL);
1529        if (!nand) {
1530                ret = -ENOMEM;
1531                goto fail;
1532        }
1533
1534        mtd                     = nand_to_mtd(nand);
1535        doc                     = (struct doc_priv *) (nand + 1);
1536        nand->bbt_td            = (struct nand_bbt_descr *) (doc + 1);
1537        nand->bbt_md            = nand->bbt_td + 1;
1538
1539        mtd->owner              = THIS_MODULE;
1540
1541        nand_set_controller_data(nand, doc);
1542        nand->select_chip       = doc200x_select_chip;
1543        nand->cmd_ctrl          = doc200x_hwcontrol;
1544        nand->dev_ready         = doc200x_dev_ready;
1545        nand->waitfunc          = doc200x_wait;
1546        nand->block_bad         = doc200x_block_bad;
1547        nand->ecc.hwctl         = doc200x_enable_hwecc;
1548        nand->ecc.calculate     = doc200x_calculate_ecc;
1549        nand->ecc.correct       = doc200x_correct_data;
1550
1551        nand->ecc.layout        = &doc200x_oobinfo;
1552        nand->ecc.mode          = NAND_ECC_HW_SYNDROME;
1553        nand->ecc.size          = 512;
1554        nand->ecc.bytes         = 6;
1555        nand->ecc.strength      = 2;
1556        nand->ecc.options       = NAND_ECC_GENERIC_ERASED_CHECK;
1557        nand->bbt_options       = NAND_BBT_USE_FLASH;
1558        /* Skip the automatic BBT scan so we can run it manually */
1559        nand->options           |= NAND_SKIP_BBTSCAN;
1560
1561        doc->physadr            = physadr;
1562        doc->virtadr            = virtadr;
1563        doc->ChipID             = ChipID;
1564        doc->curfloor           = -1;
1565        doc->curchip            = -1;
1566        doc->mh0_page           = -1;
1567        doc->mh1_page           = -1;
1568        doc->nextdoc            = doclist;
1569
1570        if (ChipID == DOC_ChipID_Doc2k)
1571                numchips = doc2000_init(mtd);
1572        else if (ChipID == DOC_ChipID_DocMilPlus16)
1573                numchips = doc2001plus_init(mtd);
1574        else
1575                numchips = doc2001_init(mtd);
1576
1577        if ((ret = nand_scan(mtd, numchips)) || (ret = doc->late_init(mtd))) {
1578                /* DBB note: i believe nand_release is necessary here, as
1579                   buffers may have been allocated in nand_base.  Check with
1580                   Thomas. FIX ME! */
1581                /* nand_release will call mtd_device_unregister, but we
1582                   haven't yet added it.  This is handled without incident by
1583                   mtd_device_unregister, as far as I can tell. */
1584                nand_release(mtd);
1585                kfree(nand);
1586                goto fail;
1587        }
1588
1589        /* Success! */
1590        doclist = mtd;
1591        return 0;
1592
1593 notfound:
1594        /* Put back the contents of the DOCControl register, in case it's not
1595           actually a DiskOnChip.  */
1596        WriteDOC(save_control, virtadr, DOCControl);
1597 fail:
1598        iounmap(virtadr);
1599
1600error_ioremap:
1601        release_mem_region(physadr, DOC_IOREMAP_LEN);
1602
1603        return ret;
1604}
1605
1606static void release_nanddoc(void)
1607{
1608        struct mtd_info *mtd, *nextmtd;
1609        struct nand_chip *nand;
1610        struct doc_priv *doc;
1611
1612        for (mtd = doclist; mtd; mtd = nextmtd) {
1613                nand = mtd_to_nand(mtd);
1614                doc = nand_get_controller_data(nand);
1615
1616                nextmtd = doc->nextdoc;
1617                nand_release(mtd);
1618                iounmap(doc->virtadr);
1619                release_mem_region(doc->physadr, DOC_IOREMAP_LEN);
1620                kfree(nand);
1621        }
1622}
1623
1624static int __init init_nanddoc(void)
1625{
1626        int i, ret = 0;
1627
1628        /* We could create the decoder on demand, if memory is a concern.
1629         * This way we have it handy, if an error happens
1630         *
1631         * Symbolsize is 10 (bits)
1632         * Primitve polynomial is x^10+x^3+1
1633         * first consecutive root is 510
1634         * primitve element to generate roots = 1
1635         * generator polinomial degree = 4
1636         */
1637        rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
1638        if (!rs_decoder) {
1639                printk(KERN_ERR "DiskOnChip: Could not create a RS decoder\n");
1640                return -ENOMEM;
1641        }
1642
1643        if (doc_config_location) {
1644                printk(KERN_INFO "Using configured DiskOnChip probe address 0x%lx\n", doc_config_location);
1645                ret = doc_probe(doc_config_location);
1646                if (ret < 0)
1647                        goto outerr;
1648        } else {
1649                for (i = 0; (doc_locations[i] != 0xffffffff); i++) {
1650                        doc_probe(doc_locations[i]);
1651                }
1652        }
1653        /* No banner message any more. Print a message if no DiskOnChip
1654           found, so the user knows we at least tried. */
1655        if (!doclist) {
1656                printk(KERN_INFO "No valid DiskOnChip devices found\n");
1657                ret = -ENODEV;
1658                goto outerr;
1659        }
1660        return 0;
1661 outerr:
1662        free_rs(rs_decoder);
1663        return ret;
1664}
1665
1666static void __exit cleanup_nanddoc(void)
1667{
1668        /* Cleanup the nand/DoC resources */
1669        release_nanddoc();
1670
1671        /* Free the reed solomon resources */
1672        if (rs_decoder) {
1673                free_rs(rs_decoder);
1674        }
1675}
1676
1677module_init(init_nanddoc);
1678module_exit(cleanup_nanddoc);
1679
1680MODULE_LICENSE("GPL");
1681MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1682MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver");
1683