Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Handles the M-Systems DiskOnChip G3 chip
0004  *
0005  * Copyright (C) 2011 Robert Jarzmik
0006  */
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/errno.h>
0011 #include <linux/of.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/string.h>
0014 #include <linux/slab.h>
0015 #include <linux/io.h>
0016 #include <linux/delay.h>
0017 #include <linux/mtd/mtd.h>
0018 #include <linux/mtd/partitions.h>
0019 #include <linux/bitmap.h>
0020 #include <linux/bitrev.h>
0021 #include <linux/bch.h>
0022 
0023 #include <linux/debugfs.h>
0024 #include <linux/seq_file.h>
0025 
0026 #define CREATE_TRACE_POINTS
0027 #include "docg3.h"
0028 
0029 /*
0030  * This driver handles the DiskOnChip G3 flash memory.
0031  *
0032  * As no specification is available from M-Systems/Sandisk, this drivers lacks
0033  * several functions available on the chip, as :
0034  *  - IPL write
0035  *
0036  * The bus data width (8bits versus 16bits) is not handled (if_cfg flag), and
0037  * the driver assumes a 16bits data bus.
0038  *
0039  * DocG3 relies on 2 ECC algorithms, which are handled in hardware :
0040  *  - a 1 byte Hamming code stored in the OOB for each page
0041  *  - a 7 bytes BCH code stored in the OOB for each page
0042  * The BCH ECC is :
0043  *  - BCH is in GF(2^14)
0044  *  - BCH is over data of 520 bytes (512 page + 7 page_info bytes
0045  *                                   + 1 hamming byte)
0046  *  - BCH can correct up to 4 bits (t = 4)
0047  *  - BCH syndroms are calculated in hardware, and checked in hardware as well
0048  *
0049  */
0050 
0051 static unsigned int reliable_mode;
0052 module_param(reliable_mode, uint, 0);
0053 MODULE_PARM_DESC(reliable_mode, "Set the docg3 mode (0=normal MLC, 1=fast, "
0054          "2=reliable) : MLC normal operations are in normal mode");
0055 
0056 static int docg3_ooblayout_ecc(struct mtd_info *mtd, int section,
0057                    struct mtd_oob_region *oobregion)
0058 {
0059     if (section)
0060         return -ERANGE;
0061 
0062     /* byte 7 is Hamming ECC, byte 8-14 are BCH ECC */
0063     oobregion->offset = 7;
0064     oobregion->length = 8;
0065 
0066     return 0;
0067 }
0068 
0069 static int docg3_ooblayout_free(struct mtd_info *mtd, int section,
0070                 struct mtd_oob_region *oobregion)
0071 {
0072     if (section > 1)
0073         return -ERANGE;
0074 
0075     /* free bytes: byte 0 until byte 6, byte 15 */
0076     if (!section) {
0077         oobregion->offset = 0;
0078         oobregion->length = 7;
0079     } else {
0080         oobregion->offset = 15;
0081         oobregion->length = 1;
0082     }
0083 
0084     return 0;
0085 }
0086 
0087 static const struct mtd_ooblayout_ops nand_ooblayout_docg3_ops = {
0088     .ecc = docg3_ooblayout_ecc,
0089     .free = docg3_ooblayout_free,
0090 };
0091 
0092 static inline u8 doc_readb(struct docg3 *docg3, u16 reg)
0093 {
0094     u8 val = readb(docg3->cascade->base + reg);
0095 
0096     trace_docg3_io(0, 8, reg, (int)val);
0097     return val;
0098 }
0099 
0100 static inline u16 doc_readw(struct docg3 *docg3, u16 reg)
0101 {
0102     u16 val = readw(docg3->cascade->base + reg);
0103 
0104     trace_docg3_io(0, 16, reg, (int)val);
0105     return val;
0106 }
0107 
0108 static inline void doc_writeb(struct docg3 *docg3, u8 val, u16 reg)
0109 {
0110     writeb(val, docg3->cascade->base + reg);
0111     trace_docg3_io(1, 8, reg, val);
0112 }
0113 
0114 static inline void doc_writew(struct docg3 *docg3, u16 val, u16 reg)
0115 {
0116     writew(val, docg3->cascade->base + reg);
0117     trace_docg3_io(1, 16, reg, val);
0118 }
0119 
0120 static inline void doc_flash_command(struct docg3 *docg3, u8 cmd)
0121 {
0122     doc_writeb(docg3, cmd, DOC_FLASHCOMMAND);
0123 }
0124 
0125 static inline void doc_flash_sequence(struct docg3 *docg3, u8 seq)
0126 {
0127     doc_writeb(docg3, seq, DOC_FLASHSEQUENCE);
0128 }
0129 
0130 static inline void doc_flash_address(struct docg3 *docg3, u8 addr)
0131 {
0132     doc_writeb(docg3, addr, DOC_FLASHADDRESS);
0133 }
0134 
0135 static char const * const part_probes[] = { "cmdlinepart", "saftlpart", NULL };
0136 
0137 static int doc_register_readb(struct docg3 *docg3, int reg)
0138 {
0139     u8 val;
0140 
0141     doc_writew(docg3, reg, DOC_READADDRESS);
0142     val = doc_readb(docg3, reg);
0143     doc_vdbg("Read register %04x : %02x\n", reg, val);
0144     return val;
0145 }
0146 
0147 static int doc_register_readw(struct docg3 *docg3, int reg)
0148 {
0149     u16 val;
0150 
0151     doc_writew(docg3, reg, DOC_READADDRESS);
0152     val = doc_readw(docg3, reg);
0153     doc_vdbg("Read register %04x : %04x\n", reg, val);
0154     return val;
0155 }
0156 
0157 /**
0158  * doc_delay - delay docg3 operations
0159  * @docg3: the device
0160  * @nbNOPs: the number of NOPs to issue
0161  *
0162  * As no specification is available, the right timings between chip commands are
0163  * unknown. The only available piece of information are the observed nops on a
0164  * working docg3 chip.
0165  * Therefore, doc_delay relies on a busy loop of NOPs, instead of scheduler
0166  * friendlier msleep() functions or blocking mdelay().
0167  */
0168 static void doc_delay(struct docg3 *docg3, int nbNOPs)
0169 {
0170     int i;
0171 
0172     doc_vdbg("NOP x %d\n", nbNOPs);
0173     for (i = 0; i < nbNOPs; i++)
0174         doc_writeb(docg3, 0, DOC_NOP);
0175 }
0176 
0177 static int is_prot_seq_error(struct docg3 *docg3)
0178 {
0179     int ctrl;
0180 
0181     ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
0182     return ctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR);
0183 }
0184 
0185 static int doc_is_ready(struct docg3 *docg3)
0186 {
0187     int ctrl;
0188 
0189     ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
0190     return ctrl & DOC_CTRL_FLASHREADY;
0191 }
0192 
0193 static int doc_wait_ready(struct docg3 *docg3)
0194 {
0195     int maxWaitCycles = 100;
0196 
0197     do {
0198         doc_delay(docg3, 4);
0199         cpu_relax();
0200     } while (!doc_is_ready(docg3) && maxWaitCycles--);
0201     doc_delay(docg3, 2);
0202     if (maxWaitCycles > 0)
0203         return 0;
0204     else
0205         return -EIO;
0206 }
0207 
0208 static int doc_reset_seq(struct docg3 *docg3)
0209 {
0210     int ret;
0211 
0212     doc_writeb(docg3, 0x10, DOC_FLASHCONTROL);
0213     doc_flash_sequence(docg3, DOC_SEQ_RESET);
0214     doc_flash_command(docg3, DOC_CMD_RESET);
0215     doc_delay(docg3, 2);
0216     ret = doc_wait_ready(docg3);
0217 
0218     doc_dbg("doc_reset_seq() -> isReady=%s\n", ret ? "false" : "true");
0219     return ret;
0220 }
0221 
0222 /**
0223  * doc_read_data_area - Read data from data area
0224  * @docg3: the device
0225  * @buf: the buffer to fill in (might be NULL is dummy reads)
0226  * @len: the length to read
0227  * @first: first time read, DOC_READADDRESS should be set
0228  *
0229  * Reads bytes from flash data. Handles the single byte / even bytes reads.
0230  */
0231 static void doc_read_data_area(struct docg3 *docg3, void *buf, int len,
0232                    int first)
0233 {
0234     int i, cdr, len4;
0235     u16 data16, *dst16;
0236     u8 data8, *dst8;
0237 
0238     doc_dbg("doc_read_data_area(buf=%p, len=%d)\n", buf, len);
0239     cdr = len & 0x1;
0240     len4 = len - cdr;
0241 
0242     if (first)
0243         doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
0244     dst16 = buf;
0245     for (i = 0; i < len4; i += 2) {
0246         data16 = doc_readw(docg3, DOC_IOSPACE_DATA);
0247         if (dst16) {
0248             *dst16 = data16;
0249             dst16++;
0250         }
0251     }
0252 
0253     if (cdr) {
0254         doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
0255                DOC_READADDRESS);
0256         doc_delay(docg3, 1);
0257         dst8 = (u8 *)dst16;
0258         for (i = 0; i < cdr; i++) {
0259             data8 = doc_readb(docg3, DOC_IOSPACE_DATA);
0260             if (dst8) {
0261                 *dst8 = data8;
0262                 dst8++;
0263             }
0264         }
0265     }
0266 }
0267 
0268 /**
0269  * doc_write_data_area - Write data into data area
0270  * @docg3: the device
0271  * @buf: the buffer to get input bytes from
0272  * @len: the length to write
0273  *
0274  * Writes bytes into flash data. Handles the single byte / even bytes writes.
0275  */
0276 static void doc_write_data_area(struct docg3 *docg3, const void *buf, int len)
0277 {
0278     int i, cdr, len4;
0279     u16 *src16;
0280     u8 *src8;
0281 
0282     doc_dbg("doc_write_data_area(buf=%p, len=%d)\n", buf, len);
0283     cdr = len & 0x3;
0284     len4 = len - cdr;
0285 
0286     doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
0287     src16 = (u16 *)buf;
0288     for (i = 0; i < len4; i += 2) {
0289         doc_writew(docg3, *src16, DOC_IOSPACE_DATA);
0290         src16++;
0291     }
0292 
0293     src8 = (u8 *)src16;
0294     for (i = 0; i < cdr; i++) {
0295         doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
0296                DOC_READADDRESS);
0297         doc_writeb(docg3, *src8, DOC_IOSPACE_DATA);
0298         src8++;
0299     }
0300 }
0301 
0302 /**
0303  * doc_set_data_mode - Sets the flash to normal or reliable data mode
0304  * @docg3: the device
0305  *
0306  * The reliable data mode is a bit slower than the fast mode, but less errors
0307  * occur.  Entering the reliable mode cannot be done without entering the fast
0308  * mode first.
0309  *
0310  * In reliable mode, pages 2*n and 2*n+1 are clones. Writing to page 0 of blocks
0311  * (4,5) make the hardware write also to page 1 of blocks blocks(4,5). Reading
0312  * from page 0 of blocks (4,5) or from page 1 of blocks (4,5) gives the same
0313  * result, which is a logical and between bytes from page 0 and page 1 (which is
0314  * consistent with the fact that writing to a page is _clearing_ bits of that
0315  * page).
0316  */
0317 static void doc_set_reliable_mode(struct docg3 *docg3)
0318 {
0319     static char *strmode[] = { "normal", "fast", "reliable", "invalid" };
0320 
0321     doc_dbg("doc_set_reliable_mode(%s)\n", strmode[docg3->reliable]);
0322     switch (docg3->reliable) {
0323     case 0:
0324         break;
0325     case 1:
0326         doc_flash_sequence(docg3, DOC_SEQ_SET_FASTMODE);
0327         doc_flash_command(docg3, DOC_CMD_FAST_MODE);
0328         break;
0329     case 2:
0330         doc_flash_sequence(docg3, DOC_SEQ_SET_RELIABLEMODE);
0331         doc_flash_command(docg3, DOC_CMD_FAST_MODE);
0332         doc_flash_command(docg3, DOC_CMD_RELIABLE_MODE);
0333         break;
0334     default:
0335         doc_err("doc_set_reliable_mode(): invalid mode\n");
0336         break;
0337     }
0338     doc_delay(docg3, 2);
0339 }
0340 
0341 /**
0342  * doc_set_asic_mode - Set the ASIC mode
0343  * @docg3: the device
0344  * @mode: the mode
0345  *
0346  * The ASIC can work in 3 modes :
0347  *  - RESET: all registers are zeroed
0348  *  - NORMAL: receives and handles commands
0349  *  - POWERDOWN: minimal poweruse, flash parts shut off
0350  */
0351 static void doc_set_asic_mode(struct docg3 *docg3, u8 mode)
0352 {
0353     int i;
0354 
0355     for (i = 0; i < 12; i++)
0356         doc_readb(docg3, DOC_IOSPACE_IPL);
0357 
0358     mode |= DOC_ASICMODE_MDWREN;
0359     doc_dbg("doc_set_asic_mode(%02x)\n", mode);
0360     doc_writeb(docg3, mode, DOC_ASICMODE);
0361     doc_writeb(docg3, ~mode, DOC_ASICMODECONFIRM);
0362     doc_delay(docg3, 1);
0363 }
0364 
0365 /**
0366  * doc_set_device_id - Sets the devices id for cascaded G3 chips
0367  * @docg3: the device
0368  * @id: the chip to select (amongst 0, 1, 2, 3)
0369  *
0370  * There can be 4 cascaded G3 chips. This function selects the one which will
0371  * should be the active one.
0372  */
0373 static void doc_set_device_id(struct docg3 *docg3, int id)
0374 {
0375     u8 ctrl;
0376 
0377     doc_dbg("doc_set_device_id(%d)\n", id);
0378     doc_writeb(docg3, id, DOC_DEVICESELECT);
0379     ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
0380 
0381     ctrl &= ~DOC_CTRL_VIOLATION;
0382     ctrl |= DOC_CTRL_CE;
0383     doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
0384 }
0385 
0386 /**
0387  * doc_set_extra_page_mode - Change flash page layout
0388  * @docg3: the device
0389  *
0390  * Normally, the flash page is split into the data (512 bytes) and the out of
0391  * band data (16 bytes). For each, 4 more bytes can be accessed, where the wear
0392  * leveling counters are stored.  To access this last area of 4 bytes, a special
0393  * mode must be input to the flash ASIC.
0394  *
0395  * Returns 0 if no error occurred, -EIO else.
0396  */
0397 static int doc_set_extra_page_mode(struct docg3 *docg3)
0398 {
0399     int fctrl;
0400 
0401     doc_dbg("doc_set_extra_page_mode()\n");
0402     doc_flash_sequence(docg3, DOC_SEQ_PAGE_SIZE_532);
0403     doc_flash_command(docg3, DOC_CMD_PAGE_SIZE_532);
0404     doc_delay(docg3, 2);
0405 
0406     fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
0407     if (fctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR))
0408         return -EIO;
0409     else
0410         return 0;
0411 }
0412 
0413 /**
0414  * doc_setup_addr_sector - Setup blocks/page/ofs address for one plane
0415  * @docg3: the device
0416  * @sector: the sector
0417  */
0418 static void doc_setup_addr_sector(struct docg3 *docg3, int sector)
0419 {
0420     doc_delay(docg3, 1);
0421     doc_flash_address(docg3, sector & 0xff);
0422     doc_flash_address(docg3, (sector >> 8) & 0xff);
0423     doc_flash_address(docg3, (sector >> 16) & 0xff);
0424     doc_delay(docg3, 1);
0425 }
0426 
0427 /**
0428  * doc_setup_writeaddr_sector - Setup blocks/page/ofs address for one plane
0429  * @docg3: the device
0430  * @sector: the sector
0431  * @ofs: the offset in the page, between 0 and (512 + 16 + 512)
0432  */
0433 static void doc_setup_writeaddr_sector(struct docg3 *docg3, int sector, int ofs)
0434 {
0435     ofs = ofs >> 2;
0436     doc_delay(docg3, 1);
0437     doc_flash_address(docg3, ofs & 0xff);
0438     doc_flash_address(docg3, sector & 0xff);
0439     doc_flash_address(docg3, (sector >> 8) & 0xff);
0440     doc_flash_address(docg3, (sector >> 16) & 0xff);
0441     doc_delay(docg3, 1);
0442 }
0443 
0444 /**
0445  * doc_seek - Set both flash planes to the specified block, page for reading
0446  * @docg3: the device
0447  * @block0: the first plane block index
0448  * @block1: the second plane block index
0449  * @page: the page index within the block
0450  * @wear: if true, read will occur on the 4 extra bytes of the wear area
0451  * @ofs: offset in page to read
0452  *
0453  * Programs the flash even and odd planes to the specific block and page.
0454  * Alternatively, programs the flash to the wear area of the specified page.
0455  */
0456 static int doc_read_seek(struct docg3 *docg3, int block0, int block1, int page,
0457              int wear, int ofs)
0458 {
0459     int sector, ret = 0;
0460 
0461     doc_dbg("doc_seek(blocks=(%d,%d), page=%d, ofs=%d, wear=%d)\n",
0462         block0, block1, page, ofs, wear);
0463 
0464     if (!wear && (ofs < 2 * DOC_LAYOUT_PAGE_SIZE)) {
0465         doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
0466         doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
0467         doc_delay(docg3, 2);
0468     } else {
0469         doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
0470         doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
0471         doc_delay(docg3, 2);
0472     }
0473 
0474     doc_set_reliable_mode(docg3);
0475     if (wear)
0476         ret = doc_set_extra_page_mode(docg3);
0477     if (ret)
0478         goto out;
0479 
0480     doc_flash_sequence(docg3, DOC_SEQ_READ);
0481     sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
0482     doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
0483     doc_setup_addr_sector(docg3, sector);
0484 
0485     sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
0486     doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
0487     doc_setup_addr_sector(docg3, sector);
0488     doc_delay(docg3, 1);
0489 
0490 out:
0491     return ret;
0492 }
0493 
0494 /**
0495  * doc_write_seek - Set both flash planes to the specified block, page for writing
0496  * @docg3: the device
0497  * @block0: the first plane block index
0498  * @block1: the second plane block index
0499  * @page: the page index within the block
0500  * @ofs: offset in page to write
0501  *
0502  * Programs the flash even and odd planes to the specific block and page.
0503  * Alternatively, programs the flash to the wear area of the specified page.
0504  */
0505 static int doc_write_seek(struct docg3 *docg3, int block0, int block1, int page,
0506              int ofs)
0507 {
0508     int ret = 0, sector;
0509 
0510     doc_dbg("doc_write_seek(blocks=(%d,%d), page=%d, ofs=%d)\n",
0511         block0, block1, page, ofs);
0512 
0513     doc_set_reliable_mode(docg3);
0514 
0515     if (ofs < 2 * DOC_LAYOUT_PAGE_SIZE) {
0516         doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
0517         doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
0518         doc_delay(docg3, 2);
0519     } else {
0520         doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
0521         doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
0522         doc_delay(docg3, 2);
0523     }
0524 
0525     doc_flash_sequence(docg3, DOC_SEQ_PAGE_SETUP);
0526     doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1);
0527 
0528     sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
0529     doc_setup_writeaddr_sector(docg3, sector, ofs);
0530 
0531     doc_flash_command(docg3, DOC_CMD_PROG_CYCLE3);
0532     doc_delay(docg3, 2);
0533     ret = doc_wait_ready(docg3);
0534     if (ret)
0535         goto out;
0536 
0537     doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1);
0538     sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
0539     doc_setup_writeaddr_sector(docg3, sector, ofs);
0540     doc_delay(docg3, 1);
0541 
0542 out:
0543     return ret;
0544 }
0545 
0546 
0547 /**
0548  * doc_read_page_ecc_init - Initialize hardware ECC engine
0549  * @docg3: the device
0550  * @len: the number of bytes covered by the ECC (BCH covered)
0551  *
0552  * The function does initialize the hardware ECC engine to compute the Hamming
0553  * ECC (on 1 byte) and the BCH hardware ECC (on 7 bytes).
0554  *
0555  * Return 0 if succeeded, -EIO on error
0556  */
0557 static int doc_read_page_ecc_init(struct docg3 *docg3, int len)
0558 {
0559     doc_writew(docg3, DOC_ECCCONF0_READ_MODE
0560            | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
0561            | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
0562            DOC_ECCCONF0);
0563     doc_delay(docg3, 4);
0564     doc_register_readb(docg3, DOC_FLASHCONTROL);
0565     return doc_wait_ready(docg3);
0566 }
0567 
0568 /**
0569  * doc_write_page_ecc_init - Initialize hardware BCH ECC engine
0570  * @docg3: the device
0571  * @len: the number of bytes covered by the ECC (BCH covered)
0572  *
0573  * The function does initialize the hardware ECC engine to compute the Hamming
0574  * ECC (on 1 byte) and the BCH hardware ECC (on 7 bytes).
0575  *
0576  * Return 0 if succeeded, -EIO on error
0577  */
0578 static int doc_write_page_ecc_init(struct docg3 *docg3, int len)
0579 {
0580     doc_writew(docg3, DOC_ECCCONF0_WRITE_MODE
0581            | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
0582            | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
0583            DOC_ECCCONF0);
0584     doc_delay(docg3, 4);
0585     doc_register_readb(docg3, DOC_FLASHCONTROL);
0586     return doc_wait_ready(docg3);
0587 }
0588 
0589 /**
0590  * doc_ecc_disable - Disable Hamming and BCH ECC hardware calculator
0591  * @docg3: the device
0592  *
0593  * Disables the hardware ECC generator and checker, for unchecked reads (as when
0594  * reading OOB only or write status byte).
0595  */
0596 static void doc_ecc_disable(struct docg3 *docg3)
0597 {
0598     doc_writew(docg3, DOC_ECCCONF0_READ_MODE, DOC_ECCCONF0);
0599     doc_delay(docg3, 4);
0600 }
0601 
0602 /**
0603  * doc_hamming_ecc_init - Initialize hardware Hamming ECC engine
0604  * @docg3: the device
0605  * @nb_bytes: the number of bytes covered by the ECC (Hamming covered)
0606  *
0607  * This function programs the ECC hardware to compute the hamming code on the
0608  * last provided N bytes to the hardware generator.
0609  */
0610 static void doc_hamming_ecc_init(struct docg3 *docg3, int nb_bytes)
0611 {
0612     u8 ecc_conf1;
0613 
0614     ecc_conf1 = doc_register_readb(docg3, DOC_ECCCONF1);
0615     ecc_conf1 &= ~DOC_ECCCONF1_HAMMING_BITS_MASK;
0616     ecc_conf1 |= (nb_bytes & DOC_ECCCONF1_HAMMING_BITS_MASK);
0617     doc_writeb(docg3, ecc_conf1, DOC_ECCCONF1);
0618 }
0619 
0620 /**
0621  * doc_ecc_bch_fix_data - Fix if need be read data from flash
0622  * @docg3: the device
0623  * @buf: the buffer of read data (512 + 7 + 1 bytes)
0624  * @hwecc: the hardware calculated ECC.
0625  *         It's in fact recv_ecc ^ calc_ecc, where recv_ecc was read from OOB
0626  *         area data, and calc_ecc the ECC calculated by the hardware generator.
0627  *
0628  * Checks if the received data matches the ECC, and if an error is detected,
0629  * tries to fix the bit flips (at most 4) in the buffer buf.  As the docg3
0630  * understands the (data, ecc, syndroms) in an inverted order in comparison to
0631  * the BCH library, the function reverses the order of bits (ie. bit7 and bit0,
0632  * bit6 and bit 1, ...) for all ECC data.
0633  *
0634  * The hardware ecc unit produces oob_ecc ^ calc_ecc.  The kernel's bch
0635  * algorithm is used to decode this.  However the hw operates on page
0636  * data in a bit order that is the reverse of that of the bch alg,
0637  * requiring that the bits be reversed on the result.  Thanks to Ivan
0638  * Djelic for his analysis.
0639  *
0640  * Returns number of fixed bits (0, 1, 2, 3, 4) or -EBADMSG if too many bit
0641  * errors were detected and cannot be fixed.
0642  */
0643 static int doc_ecc_bch_fix_data(struct docg3 *docg3, void *buf, u8 *hwecc)
0644 {
0645     u8 ecc[DOC_ECC_BCH_SIZE];
0646     int errorpos[DOC_ECC_BCH_T], i, numerrs;
0647 
0648     for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
0649         ecc[i] = bitrev8(hwecc[i]);
0650     numerrs = bch_decode(docg3->cascade->bch, NULL,
0651                  DOC_ECC_BCH_COVERED_BYTES,
0652                  NULL, ecc, NULL, errorpos);
0653     BUG_ON(numerrs == -EINVAL);
0654     if (numerrs < 0)
0655         goto out;
0656 
0657     for (i = 0; i < numerrs; i++)
0658         errorpos[i] = (errorpos[i] & ~7) | (7 - (errorpos[i] & 7));
0659     for (i = 0; i < numerrs; i++)
0660         if (errorpos[i] < DOC_ECC_BCH_COVERED_BYTES*8)
0661             /* error is located in data, correct it */
0662             change_bit(errorpos[i], buf);
0663 out:
0664     doc_dbg("doc_ecc_bch_fix_data: flipped %d bits\n", numerrs);
0665     return numerrs;
0666 }
0667 
0668 
0669 /**
0670  * doc_read_page_prepare - Prepares reading data from a flash page
0671  * @docg3: the device
0672  * @block0: the first plane block index on flash memory
0673  * @block1: the second plane block index on flash memory
0674  * @page: the page index in the block
0675  * @offset: the offset in the page (must be a multiple of 4)
0676  *
0677  * Prepares the page to be read in the flash memory :
0678  *   - tell ASIC to map the flash pages
0679  *   - tell ASIC to be in read mode
0680  *
0681  * After a call to this method, a call to doc_read_page_finish is mandatory,
0682  * to end the read cycle of the flash.
0683  *
0684  * Read data from a flash page. The length to be read must be between 0 and
0685  * (page_size + oob_size + wear_size), ie. 532, and a multiple of 4 (because
0686  * the extra bytes reading is not implemented).
0687  *
0688  * As pages are grouped by 2 (in 2 planes), reading from a page must be done
0689  * in two steps:
0690  *  - one read of 512 bytes at offset 0
0691  *  - one read of 512 bytes at offset 512 + 16
0692  *
0693  * Returns 0 if successful, -EIO if a read error occurred.
0694  */
0695 static int doc_read_page_prepare(struct docg3 *docg3, int block0, int block1,
0696                  int page, int offset)
0697 {
0698     int wear_area = 0, ret = 0;
0699 
0700     doc_dbg("doc_read_page_prepare(blocks=(%d,%d), page=%d, ofsInPage=%d)\n",
0701         block0, block1, page, offset);
0702     if (offset >= DOC_LAYOUT_WEAR_OFFSET)
0703         wear_area = 1;
0704     if (!wear_area && offset > (DOC_LAYOUT_PAGE_OOB_SIZE * 2))
0705         return -EINVAL;
0706 
0707     doc_set_device_id(docg3, docg3->device_id);
0708     ret = doc_reset_seq(docg3);
0709     if (ret)
0710         goto err;
0711 
0712     /* Program the flash address block and page */
0713     ret = doc_read_seek(docg3, block0, block1, page, wear_area, offset);
0714     if (ret)
0715         goto err;
0716 
0717     doc_flash_command(docg3, DOC_CMD_READ_ALL_PLANES);
0718     doc_delay(docg3, 2);
0719     doc_wait_ready(docg3);
0720 
0721     doc_flash_command(docg3, DOC_CMD_SET_ADDR_READ);
0722     doc_delay(docg3, 1);
0723     if (offset >= DOC_LAYOUT_PAGE_SIZE * 2)
0724         offset -= 2 * DOC_LAYOUT_PAGE_SIZE;
0725     doc_flash_address(docg3, offset >> 2);
0726     doc_delay(docg3, 1);
0727     doc_wait_ready(docg3);
0728 
0729     doc_flash_command(docg3, DOC_CMD_READ_FLASH);
0730 
0731     return 0;
0732 err:
0733     doc_writeb(docg3, 0, DOC_DATAEND);
0734     doc_delay(docg3, 2);
0735     return -EIO;
0736 }
0737 
0738 /**
0739  * doc_read_page_getbytes - Reads bytes from a prepared page
0740  * @docg3: the device
0741  * @len: the number of bytes to be read (must be a multiple of 4)
0742  * @buf: the buffer to be filled in (or NULL is forget bytes)
0743  * @first: 1 if first time read, DOC_READADDRESS should be set
0744  * @last_odd: 1 if last read ended up on an odd byte
0745  *
0746  * Reads bytes from a prepared page. There is a trickery here : if the last read
0747  * ended up on an odd offset in the 1024 bytes double page, ie. between the 2
0748  * planes, the first byte must be read apart. If a word (16bit) read was used,
0749  * the read would return the byte of plane 2 as low *and* high endian, which
0750  * will mess the read.
0751  *
0752  */
0753 static int doc_read_page_getbytes(struct docg3 *docg3, int len, u_char *buf,
0754                   int first, int last_odd)
0755 {
0756     if (last_odd && len > 0) {
0757         doc_read_data_area(docg3, buf, 1, first);
0758         doc_read_data_area(docg3, buf ? buf + 1 : buf, len - 1, 0);
0759     } else {
0760         doc_read_data_area(docg3, buf, len, first);
0761     }
0762     doc_delay(docg3, 2);
0763     return len;
0764 }
0765 
0766 /**
0767  * doc_write_page_putbytes - Writes bytes into a prepared page
0768  * @docg3: the device
0769  * @len: the number of bytes to be written
0770  * @buf: the buffer of input bytes
0771  *
0772  */
0773 static void doc_write_page_putbytes(struct docg3 *docg3, int len,
0774                     const u_char *buf)
0775 {
0776     doc_write_data_area(docg3, buf, len);
0777     doc_delay(docg3, 2);
0778 }
0779 
0780 /**
0781  * doc_get_bch_hw_ecc - Get hardware calculated BCH ECC
0782  * @docg3: the device
0783  * @hwecc:  the array of 7 integers where the hardware ecc will be stored
0784  */
0785 static void doc_get_bch_hw_ecc(struct docg3 *docg3, u8 *hwecc)
0786 {
0787     int i;
0788 
0789     for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
0790         hwecc[i] = doc_register_readb(docg3, DOC_BCH_HW_ECC(i));
0791 }
0792 
0793 /**
0794  * doc_page_finish - Ends reading/writing of a flash page
0795  * @docg3: the device
0796  */
0797 static void doc_page_finish(struct docg3 *docg3)
0798 {
0799     doc_writeb(docg3, 0, DOC_DATAEND);
0800     doc_delay(docg3, 2);
0801 }
0802 
0803 /**
0804  * doc_read_page_finish - Ends reading of a flash page
0805  * @docg3: the device
0806  *
0807  * As a side effect, resets the chip selector to 0. This ensures that after each
0808  * read operation, the floor 0 is selected. Therefore, if the systems halts, the
0809  * reboot will boot on floor 0, where the IPL is.
0810  */
0811 static void doc_read_page_finish(struct docg3 *docg3)
0812 {
0813     doc_page_finish(docg3);
0814     doc_set_device_id(docg3, 0);
0815 }
0816 
0817 /**
0818  * calc_block_sector - Calculate blocks, pages and ofs.
0819  *
0820  * @from: offset in flash
0821  * @block0: first plane block index calculated
0822  * @block1: second plane block index calculated
0823  * @page: page calculated
0824  * @ofs: offset in page
0825  * @reliable: 0 if docg3 in normal mode, 1 if docg3 in fast mode, 2 if docg3 in
0826  * reliable mode.
0827  *
0828  * The calculation is based on the reliable/normal mode. In normal mode, the 64
0829  * pages of a block are available. In reliable mode, as pages 2*n and 2*n+1 are
0830  * clones, only 32 pages per block are available.
0831  */
0832 static void calc_block_sector(loff_t from, int *block0, int *block1, int *page,
0833                   int *ofs, int reliable)
0834 {
0835     uint sector, pages_biblock;
0836 
0837     pages_biblock = DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_NBPLANES;
0838     if (reliable == 1 || reliable == 2)
0839         pages_biblock /= 2;
0840 
0841     sector = from / DOC_LAYOUT_PAGE_SIZE;
0842     *block0 = sector / pages_biblock * DOC_LAYOUT_NBPLANES;
0843     *block1 = *block0 + 1;
0844     *page = sector % pages_biblock;
0845     *page /= DOC_LAYOUT_NBPLANES;
0846     if (reliable == 1 || reliable == 2)
0847         *page *= 2;
0848     if (sector % 2)
0849         *ofs = DOC_LAYOUT_PAGE_OOB_SIZE;
0850     else
0851         *ofs = 0;
0852 }
0853 
0854 /**
0855  * doc_read_oob - Read out of band bytes from flash
0856  * @mtd: the device
0857  * @from: the offset from first block and first page, in bytes, aligned on page
0858  *        size
0859  * @ops: the mtd oob structure
0860  *
0861  * Reads flash memory OOB area of pages.
0862  *
0863  * Returns 0 if read successful, of -EIO, -EINVAL if an error occurred
0864  */
0865 static int doc_read_oob(struct mtd_info *mtd, loff_t from,
0866             struct mtd_oob_ops *ops)
0867 {
0868     struct docg3 *docg3 = mtd->priv;
0869     int block0, block1, page, ret, skip, ofs = 0;
0870     u8 *oobbuf = ops->oobbuf;
0871     u8 *buf = ops->datbuf;
0872     size_t len, ooblen, nbdata, nboob;
0873     u8 hwecc[DOC_ECC_BCH_SIZE], eccconf1;
0874     int max_bitflips = 0;
0875 
0876     if (buf)
0877         len = ops->len;
0878     else
0879         len = 0;
0880     if (oobbuf)
0881         ooblen = ops->ooblen;
0882     else
0883         ooblen = 0;
0884 
0885     if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
0886         oobbuf += ops->ooboffs;
0887 
0888     doc_dbg("doc_read_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
0889         from, ops->mode, buf, len, oobbuf, ooblen);
0890     if (ooblen % DOC_LAYOUT_OOB_SIZE)
0891         return -EINVAL;
0892 
0893     ops->oobretlen = 0;
0894     ops->retlen = 0;
0895     ret = 0;
0896     skip = from % DOC_LAYOUT_PAGE_SIZE;
0897     mutex_lock(&docg3->cascade->lock);
0898     while (ret >= 0 && (len > 0 || ooblen > 0)) {
0899         calc_block_sector(from - skip, &block0, &block1, &page, &ofs,
0900             docg3->reliable);
0901         nbdata = min_t(size_t, len, DOC_LAYOUT_PAGE_SIZE - skip);
0902         nboob = min_t(size_t, ooblen, (size_t)DOC_LAYOUT_OOB_SIZE);
0903         ret = doc_read_page_prepare(docg3, block0, block1, page, ofs);
0904         if (ret < 0)
0905             goto out;
0906         ret = doc_read_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
0907         if (ret < 0)
0908             goto err_in_read;
0909         ret = doc_read_page_getbytes(docg3, skip, NULL, 1, 0);
0910         if (ret < skip)
0911             goto err_in_read;
0912         ret = doc_read_page_getbytes(docg3, nbdata, buf, 0, skip % 2);
0913         if (ret < nbdata)
0914             goto err_in_read;
0915         doc_read_page_getbytes(docg3,
0916                        DOC_LAYOUT_PAGE_SIZE - nbdata - skip,
0917                        NULL, 0, (skip + nbdata) % 2);
0918         ret = doc_read_page_getbytes(docg3, nboob, oobbuf, 0, 0);
0919         if (ret < nboob)
0920             goto err_in_read;
0921         doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE - nboob,
0922                        NULL, 0, nboob % 2);
0923 
0924         doc_get_bch_hw_ecc(docg3, hwecc);
0925         eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1);
0926 
0927         if (nboob >= DOC_LAYOUT_OOB_SIZE) {
0928             doc_dbg("OOB - INFO: %*phC\n", 7, oobbuf);
0929             doc_dbg("OOB - HAMMING: %02x\n", oobbuf[7]);
0930             doc_dbg("OOB - BCH_ECC: %*phC\n", 7, oobbuf + 8);
0931             doc_dbg("OOB - UNUSED: %02x\n", oobbuf[15]);
0932         }
0933         doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1);
0934         doc_dbg("ECC HW_ECC: %*phC\n", 7, hwecc);
0935 
0936         ret = -EIO;
0937         if (is_prot_seq_error(docg3))
0938             goto err_in_read;
0939         ret = 0;
0940         if ((block0 >= DOC_LAYOUT_BLOCK_FIRST_DATA) &&
0941             (eccconf1 & DOC_ECCCONF1_BCH_SYNDROM_ERR) &&
0942             (eccconf1 & DOC_ECCCONF1_PAGE_IS_WRITTEN) &&
0943             (ops->mode != MTD_OPS_RAW) &&
0944             (nbdata == DOC_LAYOUT_PAGE_SIZE)) {
0945             ret = doc_ecc_bch_fix_data(docg3, buf, hwecc);
0946             if (ret < 0) {
0947                 mtd->ecc_stats.failed++;
0948                 ret = -EBADMSG;
0949             }
0950             if (ret > 0) {
0951                 mtd->ecc_stats.corrected += ret;
0952                 max_bitflips = max(max_bitflips, ret);
0953                 ret = max_bitflips;
0954             }
0955         }
0956 
0957         doc_read_page_finish(docg3);
0958         ops->retlen += nbdata;
0959         ops->oobretlen += nboob;
0960         buf += nbdata;
0961         oobbuf += nboob;
0962         len -= nbdata;
0963         ooblen -= nboob;
0964         from += DOC_LAYOUT_PAGE_SIZE;
0965         skip = 0;
0966     }
0967 
0968 out:
0969     mutex_unlock(&docg3->cascade->lock);
0970     return ret;
0971 err_in_read:
0972     doc_read_page_finish(docg3);
0973     goto out;
0974 }
0975 
0976 static int doc_reload_bbt(struct docg3 *docg3)
0977 {
0978     int block = DOC_LAYOUT_BLOCK_BBT;
0979     int ret = 0, nbpages, page;
0980     u_char *buf = docg3->bbt;
0981 
0982     nbpages = DIV_ROUND_UP(docg3->max_block + 1, 8 * DOC_LAYOUT_PAGE_SIZE);
0983     for (page = 0; !ret && (page < nbpages); page++) {
0984         ret = doc_read_page_prepare(docg3, block, block + 1,
0985                         page + DOC_LAYOUT_PAGE_BBT, 0);
0986         if (!ret)
0987             ret = doc_read_page_ecc_init(docg3,
0988                              DOC_LAYOUT_PAGE_SIZE);
0989         if (!ret)
0990             doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE,
0991                            buf, 1, 0);
0992         buf += DOC_LAYOUT_PAGE_SIZE;
0993     }
0994     doc_read_page_finish(docg3);
0995     return ret;
0996 }
0997 
0998 /**
0999  * doc_block_isbad - Checks whether a block is good or not
1000  * @mtd: the device
1001  * @from: the offset to find the correct block
1002  *
1003  * Returns 1 if block is bad, 0 if block is good
1004  */
1005 static int doc_block_isbad(struct mtd_info *mtd, loff_t from)
1006 {
1007     struct docg3 *docg3 = mtd->priv;
1008     int block0, block1, page, ofs, is_good;
1009 
1010     calc_block_sector(from, &block0, &block1, &page, &ofs,
1011         docg3->reliable);
1012     doc_dbg("doc_block_isbad(from=%lld) => block=(%d,%d), page=%d, ofs=%d\n",
1013         from, block0, block1, page, ofs);
1014 
1015     if (block0 < DOC_LAYOUT_BLOCK_FIRST_DATA)
1016         return 0;
1017     if (block1 > docg3->max_block)
1018         return -EINVAL;
1019 
1020     is_good = docg3->bbt[block0 >> 3] & (1 << (block0 & 0x7));
1021     return !is_good;
1022 }
1023 
1024 #if 0
1025 /**
1026  * doc_get_erase_count - Get block erase count
1027  * @docg3: the device
1028  * @from: the offset in which the block is.
1029  *
1030  * Get the number of times a block was erased. The number is the maximum of
1031  * erase times between first and second plane (which should be equal normally).
1032  *
1033  * Returns The number of erases, or -EINVAL or -EIO on error.
1034  */
1035 static int doc_get_erase_count(struct docg3 *docg3, loff_t from)
1036 {
1037     u8 buf[DOC_LAYOUT_WEAR_SIZE];
1038     int ret, plane1_erase_count, plane2_erase_count;
1039     int block0, block1, page, ofs;
1040 
1041     doc_dbg("doc_get_erase_count(from=%lld, buf=%p)\n", from, buf);
1042     if (from % DOC_LAYOUT_PAGE_SIZE)
1043         return -EINVAL;
1044     calc_block_sector(from, &block0, &block1, &page, &ofs, docg3->reliable);
1045     if (block1 > docg3->max_block)
1046         return -EINVAL;
1047 
1048     ret = doc_reset_seq(docg3);
1049     if (!ret)
1050         ret = doc_read_page_prepare(docg3, block0, block1, page,
1051                         ofs + DOC_LAYOUT_WEAR_OFFSET, 0);
1052     if (!ret)
1053         ret = doc_read_page_getbytes(docg3, DOC_LAYOUT_WEAR_SIZE,
1054                          buf, 1, 0);
1055     doc_read_page_finish(docg3);
1056 
1057     if (ret || (buf[0] != DOC_ERASE_MARK) || (buf[2] != DOC_ERASE_MARK))
1058         return -EIO;
1059     plane1_erase_count = (u8)(~buf[1]) | ((u8)(~buf[4]) << 8)
1060         | ((u8)(~buf[5]) << 16);
1061     plane2_erase_count = (u8)(~buf[3]) | ((u8)(~buf[6]) << 8)
1062         | ((u8)(~buf[7]) << 16);
1063 
1064     return max(plane1_erase_count, plane2_erase_count);
1065 }
1066 #endif
1067 
1068 /**
1069  * doc_get_op_status - get erase/write operation status
1070  * @docg3: the device
1071  *
1072  * Queries the status from the chip, and returns it
1073  *
1074  * Returns the status (bits DOC_PLANES_STATUS_*)
1075  */
1076 static int doc_get_op_status(struct docg3 *docg3)
1077 {
1078     u8 status;
1079 
1080     doc_flash_sequence(docg3, DOC_SEQ_PLANES_STATUS);
1081     doc_flash_command(docg3, DOC_CMD_PLANES_STATUS);
1082     doc_delay(docg3, 5);
1083 
1084     doc_ecc_disable(docg3);
1085     doc_read_data_area(docg3, &status, 1, 1);
1086     return status;
1087 }
1088 
1089 /**
1090  * doc_write_erase_wait_status - wait for write or erase completion
1091  * @docg3: the device
1092  *
1093  * Wait for the chip to be ready again after erase or write operation, and check
1094  * erase/write status.
1095  *
1096  * Returns 0 if erase successful, -EIO if erase/write issue, -ETIMEOUT if
1097  * timeout
1098  */
1099 static int doc_write_erase_wait_status(struct docg3 *docg3)
1100 {
1101     int i, status, ret = 0;
1102 
1103     for (i = 0; !doc_is_ready(docg3) && i < 5; i++)
1104         msleep(20);
1105     if (!doc_is_ready(docg3)) {
1106         doc_dbg("Timeout reached and the chip is still not ready\n");
1107         ret = -EAGAIN;
1108         goto out;
1109     }
1110 
1111     status = doc_get_op_status(docg3);
1112     if (status & DOC_PLANES_STATUS_FAIL) {
1113         doc_dbg("Erase/Write failed on (a) plane(s), status = %x\n",
1114             status);
1115         ret = -EIO;
1116     }
1117 
1118 out:
1119     doc_page_finish(docg3);
1120     return ret;
1121 }
1122 
1123 /**
1124  * doc_erase_block - Erase a couple of blocks
1125  * @docg3: the device
1126  * @block0: the first block to erase (leftmost plane)
1127  * @block1: the second block to erase (rightmost plane)
1128  *
1129  * Erase both blocks, and return operation status
1130  *
1131  * Returns 0 if erase successful, -EIO if erase issue, -ETIMEOUT if chip not
1132  * ready for too long
1133  */
1134 static int doc_erase_block(struct docg3 *docg3, int block0, int block1)
1135 {
1136     int ret, sector;
1137 
1138     doc_dbg("doc_erase_block(blocks=(%d,%d))\n", block0, block1);
1139     ret = doc_reset_seq(docg3);
1140     if (ret)
1141         return -EIO;
1142 
1143     doc_set_reliable_mode(docg3);
1144     doc_flash_sequence(docg3, DOC_SEQ_ERASE);
1145 
1146     sector = block0 << DOC_ADDR_BLOCK_SHIFT;
1147     doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
1148     doc_setup_addr_sector(docg3, sector);
1149     sector = block1 << DOC_ADDR_BLOCK_SHIFT;
1150     doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
1151     doc_setup_addr_sector(docg3, sector);
1152     doc_delay(docg3, 1);
1153 
1154     doc_flash_command(docg3, DOC_CMD_ERASECYCLE2);
1155     doc_delay(docg3, 2);
1156 
1157     if (is_prot_seq_error(docg3)) {
1158         doc_err("Erase blocks %d,%d error\n", block0, block1);
1159         return -EIO;
1160     }
1161 
1162     return doc_write_erase_wait_status(docg3);
1163 }
1164 
1165 /**
1166  * doc_erase - Erase a portion of the chip
1167  * @mtd: the device
1168  * @info: the erase info
1169  *
1170  * Erase a bunch of contiguous blocks, by pairs, as a "mtd" page of 1024 is
1171  * split into 2 pages of 512 bytes on 2 contiguous blocks.
1172  *
1173  * Returns 0 if erase successful, -EINVAL if addressing error, -EIO if erase
1174  * issue
1175  */
1176 static int doc_erase(struct mtd_info *mtd, struct erase_info *info)
1177 {
1178     struct docg3 *docg3 = mtd->priv;
1179     uint64_t len;
1180     int block0, block1, page, ret = 0, ofs = 0;
1181 
1182     doc_dbg("doc_erase(from=%lld, len=%lld\n", info->addr, info->len);
1183 
1184     calc_block_sector(info->addr + info->len, &block0, &block1, &page,
1185               &ofs, docg3->reliable);
1186     if (info->addr + info->len > mtd->size || page || ofs)
1187         return -EINVAL;
1188 
1189     calc_block_sector(info->addr, &block0, &block1, &page, &ofs,
1190               docg3->reliable);
1191     mutex_lock(&docg3->cascade->lock);
1192     doc_set_device_id(docg3, docg3->device_id);
1193     doc_set_reliable_mode(docg3);
1194     for (len = info->len; !ret && len > 0; len -= mtd->erasesize) {
1195         ret = doc_erase_block(docg3, block0, block1);
1196         block0 += 2;
1197         block1 += 2;
1198     }
1199     mutex_unlock(&docg3->cascade->lock);
1200 
1201     return ret;
1202 }
1203 
1204 /**
1205  * doc_write_page - Write a single page to the chip
1206  * @docg3: the device
1207  * @to: the offset from first block and first page, in bytes, aligned on page
1208  *      size
1209  * @buf: buffer to get bytes from
1210  * @oob: buffer to get out of band bytes from (can be NULL if no OOB should be
1211  *       written)
1212  * @autoecc: if 0, all 16 bytes from OOB are taken, regardless of HW Hamming or
1213  *           BCH computations. If 1, only bytes 0-7 and byte 15 are taken,
1214  *           remaining ones are filled with hardware Hamming and BCH
1215  *           computations. Its value is not meaningfull is oob == NULL.
1216  *
1217  * Write one full page (ie. 1 page split on two planes), of 512 bytes, with the
1218  * OOB data. The OOB ECC is automatically computed by the hardware Hamming and
1219  * BCH generator if autoecc is not null.
1220  *
1221  * Returns 0 if write successful, -EIO if write error, -EAGAIN if timeout
1222  */
1223 static int doc_write_page(struct docg3 *docg3, loff_t to, const u_char *buf,
1224               const u_char *oob, int autoecc)
1225 {
1226     int block0, block1, page, ret, ofs = 0;
1227     u8 hwecc[DOC_ECC_BCH_SIZE], hamming;
1228 
1229     doc_dbg("doc_write_page(to=%lld)\n", to);
1230     calc_block_sector(to, &block0, &block1, &page, &ofs, docg3->reliable);
1231 
1232     doc_set_device_id(docg3, docg3->device_id);
1233     ret = doc_reset_seq(docg3);
1234     if (ret)
1235         goto err;
1236 
1237     /* Program the flash address block and page */
1238     ret = doc_write_seek(docg3, block0, block1, page, ofs);
1239     if (ret)
1240         goto err;
1241 
1242     doc_write_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
1243     doc_delay(docg3, 2);
1244     doc_write_page_putbytes(docg3, DOC_LAYOUT_PAGE_SIZE, buf);
1245 
1246     if (oob && autoecc) {
1247         doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ, oob);
1248         doc_delay(docg3, 2);
1249         oob += DOC_LAYOUT_OOB_UNUSED_OFS;
1250 
1251         hamming = doc_register_readb(docg3, DOC_HAMMINGPARITY);
1252         doc_delay(docg3, 2);
1253         doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_HAMMING_SZ,
1254                     &hamming);
1255         doc_delay(docg3, 2);
1256 
1257         doc_get_bch_hw_ecc(docg3, hwecc);
1258         doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_BCH_SZ, hwecc);
1259         doc_delay(docg3, 2);
1260 
1261         doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_UNUSED_SZ, oob);
1262     }
1263     if (oob && !autoecc)
1264         doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_SIZE, oob);
1265 
1266     doc_delay(docg3, 2);
1267     doc_page_finish(docg3);
1268     doc_delay(docg3, 2);
1269     doc_flash_command(docg3, DOC_CMD_PROG_CYCLE2);
1270     doc_delay(docg3, 2);
1271 
1272     /*
1273      * The wait status will perform another doc_page_finish() call, but that
1274      * seems to please the docg3, so leave it.
1275      */
1276     ret = doc_write_erase_wait_status(docg3);
1277     return ret;
1278 err:
1279     doc_read_page_finish(docg3);
1280     return ret;
1281 }
1282 
1283 /**
1284  * doc_guess_autoecc - Guess autoecc mode from mbd_oob_ops
1285  * @ops: the oob operations
1286  *
1287  * Returns 0 or 1 if success, -EINVAL if invalid oob mode
1288  */
1289 static int doc_guess_autoecc(struct mtd_oob_ops *ops)
1290 {
1291     int autoecc;
1292 
1293     switch (ops->mode) {
1294     case MTD_OPS_PLACE_OOB:
1295     case MTD_OPS_AUTO_OOB:
1296         autoecc = 1;
1297         break;
1298     case MTD_OPS_RAW:
1299         autoecc = 0;
1300         break;
1301     default:
1302         autoecc = -EINVAL;
1303     }
1304     return autoecc;
1305 }
1306 
1307 /**
1308  * doc_fill_autooob - Fill a 16 bytes OOB from 8 non-ECC bytes
1309  * @dst: the target 16 bytes OOB buffer
1310  * @oobsrc: the source 8 bytes non-ECC OOB buffer
1311  *
1312  */
1313 static void doc_fill_autooob(u8 *dst, u8 *oobsrc)
1314 {
1315     memcpy(dst, oobsrc, DOC_LAYOUT_OOB_PAGEINFO_SZ);
1316     dst[DOC_LAYOUT_OOB_UNUSED_OFS] = oobsrc[DOC_LAYOUT_OOB_PAGEINFO_SZ];
1317 }
1318 
1319 /**
1320  * doc_backup_oob - Backup OOB into docg3 structure
1321  * @docg3: the device
1322  * @to: the page offset in the chip
1323  * @ops: the OOB size and buffer
1324  *
1325  * As the docg3 should write a page with its OOB in one pass, and some userland
1326  * applications do write_oob() to setup the OOB and then write(), store the OOB
1327  * into a temporary storage. This is very dangerous, as 2 concurrent
1328  * applications could store an OOB, and then write their pages (which will
1329  * result into one having its OOB corrupted).
1330  *
1331  * The only reliable way would be for userland to call doc_write_oob() with both
1332  * the page data _and_ the OOB area.
1333  *
1334  * Returns 0 if success, -EINVAL if ops content invalid
1335  */
1336 static int doc_backup_oob(struct docg3 *docg3, loff_t to,
1337               struct mtd_oob_ops *ops)
1338 {
1339     int ooblen = ops->ooblen, autoecc;
1340 
1341     if (ooblen != DOC_LAYOUT_OOB_SIZE)
1342         return -EINVAL;
1343     autoecc = doc_guess_autoecc(ops);
1344     if (autoecc < 0)
1345         return autoecc;
1346 
1347     docg3->oob_write_ofs = to;
1348     docg3->oob_autoecc = autoecc;
1349     if (ops->mode == MTD_OPS_AUTO_OOB) {
1350         doc_fill_autooob(docg3->oob_write_buf, ops->oobbuf);
1351         ops->oobretlen = 8;
1352     } else {
1353         memcpy(docg3->oob_write_buf, ops->oobbuf, DOC_LAYOUT_OOB_SIZE);
1354         ops->oobretlen = DOC_LAYOUT_OOB_SIZE;
1355     }
1356     return 0;
1357 }
1358 
1359 /**
1360  * doc_write_oob - Write out of band bytes to flash
1361  * @mtd: the device
1362  * @ofs: the offset from first block and first page, in bytes, aligned on page
1363  *       size
1364  * @ops: the mtd oob structure
1365  *
1366  * Either write OOB data into a temporary buffer, for the subsequent write
1367  * page. The provided OOB should be 16 bytes long. If a data buffer is provided
1368  * as well, issue the page write.
1369  * Or provide data without OOB, and then a all zeroed OOB will be used (ECC will
1370  * still be filled in if asked for).
1371  *
1372  * Returns 0 is successful, EINVAL if length is not 14 bytes
1373  */
1374 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
1375              struct mtd_oob_ops *ops)
1376 {
1377     struct docg3 *docg3 = mtd->priv;
1378     int ret, autoecc, oobdelta;
1379     u8 *oobbuf = ops->oobbuf;
1380     u8 *buf = ops->datbuf;
1381     size_t len, ooblen;
1382     u8 oob[DOC_LAYOUT_OOB_SIZE];
1383 
1384     if (buf)
1385         len = ops->len;
1386     else
1387         len = 0;
1388     if (oobbuf)
1389         ooblen = ops->ooblen;
1390     else
1391         ooblen = 0;
1392 
1393     if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
1394         oobbuf += ops->ooboffs;
1395 
1396     doc_dbg("doc_write_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
1397         ofs, ops->mode, buf, len, oobbuf, ooblen);
1398     switch (ops->mode) {
1399     case MTD_OPS_PLACE_OOB:
1400     case MTD_OPS_RAW:
1401         oobdelta = mtd->oobsize;
1402         break;
1403     case MTD_OPS_AUTO_OOB:
1404         oobdelta = mtd->oobavail;
1405         break;
1406     default:
1407         return -EINVAL;
1408     }
1409     if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % oobdelta) ||
1410         (ofs % DOC_LAYOUT_PAGE_SIZE))
1411         return -EINVAL;
1412     if (len && ooblen &&
1413         (len / DOC_LAYOUT_PAGE_SIZE) != (ooblen / oobdelta))
1414         return -EINVAL;
1415 
1416     ops->oobretlen = 0;
1417     ops->retlen = 0;
1418     ret = 0;
1419     if (len == 0 && ooblen == 0)
1420         return -EINVAL;
1421     if (len == 0 && ooblen > 0)
1422         return doc_backup_oob(docg3, ofs, ops);
1423 
1424     autoecc = doc_guess_autoecc(ops);
1425     if (autoecc < 0)
1426         return autoecc;
1427 
1428     mutex_lock(&docg3->cascade->lock);
1429     while (!ret && len > 0) {
1430         memset(oob, 0, sizeof(oob));
1431         if (ofs == docg3->oob_write_ofs)
1432             memcpy(oob, docg3->oob_write_buf, DOC_LAYOUT_OOB_SIZE);
1433         else if (ooblen > 0 && ops->mode == MTD_OPS_AUTO_OOB)
1434             doc_fill_autooob(oob, oobbuf);
1435         else if (ooblen > 0)
1436             memcpy(oob, oobbuf, DOC_LAYOUT_OOB_SIZE);
1437         ret = doc_write_page(docg3, ofs, buf, oob, autoecc);
1438 
1439         ofs += DOC_LAYOUT_PAGE_SIZE;
1440         len -= DOC_LAYOUT_PAGE_SIZE;
1441         buf += DOC_LAYOUT_PAGE_SIZE;
1442         if (ooblen) {
1443             oobbuf += oobdelta;
1444             ooblen -= oobdelta;
1445             ops->oobretlen += oobdelta;
1446         }
1447         ops->retlen += DOC_LAYOUT_PAGE_SIZE;
1448     }
1449 
1450     doc_set_device_id(docg3, 0);
1451     mutex_unlock(&docg3->cascade->lock);
1452     return ret;
1453 }
1454 
1455 static struct docg3 *sysfs_dev2docg3(struct device *dev,
1456                      struct device_attribute *attr)
1457 {
1458     int floor;
1459     struct mtd_info **docg3_floors = dev_get_drvdata(dev);
1460 
1461     floor = attr->attr.name[1] - '0';
1462     if (floor < 0 || floor >= DOC_MAX_NBFLOORS)
1463         return NULL;
1464     else
1465         return docg3_floors[floor]->priv;
1466 }
1467 
1468 static ssize_t dps0_is_key_locked(struct device *dev,
1469                   struct device_attribute *attr, char *buf)
1470 {
1471     struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1472     int dps0;
1473 
1474     mutex_lock(&docg3->cascade->lock);
1475     doc_set_device_id(docg3, docg3->device_id);
1476     dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
1477     doc_set_device_id(docg3, 0);
1478     mutex_unlock(&docg3->cascade->lock);
1479 
1480     return sprintf(buf, "%d\n", !(dps0 & DOC_DPS_KEY_OK));
1481 }
1482 
1483 static ssize_t dps1_is_key_locked(struct device *dev,
1484                   struct device_attribute *attr, char *buf)
1485 {
1486     struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1487     int dps1;
1488 
1489     mutex_lock(&docg3->cascade->lock);
1490     doc_set_device_id(docg3, docg3->device_id);
1491     dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
1492     doc_set_device_id(docg3, 0);
1493     mutex_unlock(&docg3->cascade->lock);
1494 
1495     return sprintf(buf, "%d\n", !(dps1 & DOC_DPS_KEY_OK));
1496 }
1497 
1498 static ssize_t dps0_insert_key(struct device *dev,
1499                    struct device_attribute *attr,
1500                    const char *buf, size_t count)
1501 {
1502     struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1503     int i;
1504 
1505     if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
1506         return -EINVAL;
1507 
1508     mutex_lock(&docg3->cascade->lock);
1509     doc_set_device_id(docg3, docg3->device_id);
1510     for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
1511         doc_writeb(docg3, buf[i], DOC_DPS0_KEY);
1512     doc_set_device_id(docg3, 0);
1513     mutex_unlock(&docg3->cascade->lock);
1514     return count;
1515 }
1516 
1517 static ssize_t dps1_insert_key(struct device *dev,
1518                    struct device_attribute *attr,
1519                    const char *buf, size_t count)
1520 {
1521     struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1522     int i;
1523 
1524     if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
1525         return -EINVAL;
1526 
1527     mutex_lock(&docg3->cascade->lock);
1528     doc_set_device_id(docg3, docg3->device_id);
1529     for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
1530         doc_writeb(docg3, buf[i], DOC_DPS1_KEY);
1531     doc_set_device_id(docg3, 0);
1532     mutex_unlock(&docg3->cascade->lock);
1533     return count;
1534 }
1535 
1536 #define FLOOR_SYSFS(id) { \
1537     __ATTR(f##id##_dps0_is_keylocked, S_IRUGO, dps0_is_key_locked, NULL), \
1538     __ATTR(f##id##_dps1_is_keylocked, S_IRUGO, dps1_is_key_locked, NULL), \
1539     __ATTR(f##id##_dps0_protection_key, S_IWUSR|S_IWGRP, NULL, dps0_insert_key), \
1540     __ATTR(f##id##_dps1_protection_key, S_IWUSR|S_IWGRP, NULL, dps1_insert_key), \
1541 }
1542 
1543 static struct device_attribute doc_sys_attrs[DOC_MAX_NBFLOORS][4] = {
1544     FLOOR_SYSFS(0), FLOOR_SYSFS(1), FLOOR_SYSFS(2), FLOOR_SYSFS(3)
1545 };
1546 
1547 static int doc_register_sysfs(struct platform_device *pdev,
1548                   struct docg3_cascade *cascade)
1549 {
1550     struct device *dev = &pdev->dev;
1551     int floor;
1552     int ret;
1553     int i;
1554 
1555     for (floor = 0;
1556          floor < DOC_MAX_NBFLOORS && cascade->floors[floor];
1557          floor++) {
1558         for (i = 0; i < 4; i++) {
1559             ret = device_create_file(dev, &doc_sys_attrs[floor][i]);
1560             if (ret)
1561                 goto remove_files;
1562         }
1563     }
1564 
1565     return 0;
1566 
1567 remove_files:
1568     do {
1569         while (--i >= 0)
1570             device_remove_file(dev, &doc_sys_attrs[floor][i]);
1571         i = 4;
1572     } while (--floor >= 0);
1573 
1574     return ret;
1575 }
1576 
1577 static void doc_unregister_sysfs(struct platform_device *pdev,
1578                  struct docg3_cascade *cascade)
1579 {
1580     struct device *dev = &pdev->dev;
1581     int floor, i;
1582 
1583     for (floor = 0; floor < DOC_MAX_NBFLOORS && cascade->floors[floor];
1584          floor++)
1585         for (i = 0; i < 4; i++)
1586             device_remove_file(dev, &doc_sys_attrs[floor][i]);
1587 }
1588 
1589 /*
1590  * Debug sysfs entries
1591  */
1592 static int flashcontrol_show(struct seq_file *s, void *p)
1593 {
1594     struct docg3 *docg3 = (struct docg3 *)s->private;
1595 
1596     u8 fctrl;
1597 
1598     mutex_lock(&docg3->cascade->lock);
1599     fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
1600     mutex_unlock(&docg3->cascade->lock);
1601 
1602     seq_printf(s, "FlashControl : 0x%02x (%s,CE# %s,%s,%s,flash %s)\n",
1603            fctrl,
1604            fctrl & DOC_CTRL_VIOLATION ? "protocol violation" : "-",
1605            fctrl & DOC_CTRL_CE ? "active" : "inactive",
1606            fctrl & DOC_CTRL_PROTECTION_ERROR ? "protection error" : "-",
1607            fctrl & DOC_CTRL_SEQUENCE_ERROR ? "sequence error" : "-",
1608            fctrl & DOC_CTRL_FLASHREADY ? "ready" : "not ready");
1609 
1610     return 0;
1611 }
1612 DEFINE_SHOW_ATTRIBUTE(flashcontrol);
1613 
1614 static int asic_mode_show(struct seq_file *s, void *p)
1615 {
1616     struct docg3 *docg3 = (struct docg3 *)s->private;
1617 
1618     int pctrl, mode;
1619 
1620     mutex_lock(&docg3->cascade->lock);
1621     pctrl = doc_register_readb(docg3, DOC_ASICMODE);
1622     mode = pctrl & 0x03;
1623     mutex_unlock(&docg3->cascade->lock);
1624 
1625     seq_printf(s,
1626            "%04x : RAM_WE=%d,RSTIN_RESET=%d,BDETCT_RESET=%d,WRITE_ENABLE=%d,POWERDOWN=%d,MODE=%d%d (",
1627            pctrl,
1628            pctrl & DOC_ASICMODE_RAM_WE ? 1 : 0,
1629            pctrl & DOC_ASICMODE_RSTIN_RESET ? 1 : 0,
1630            pctrl & DOC_ASICMODE_BDETCT_RESET ? 1 : 0,
1631            pctrl & DOC_ASICMODE_MDWREN ? 1 : 0,
1632            pctrl & DOC_ASICMODE_POWERDOWN ? 1 : 0,
1633            mode >> 1, mode & 0x1);
1634 
1635     switch (mode) {
1636     case DOC_ASICMODE_RESET:
1637         seq_puts(s, "reset");
1638         break;
1639     case DOC_ASICMODE_NORMAL:
1640         seq_puts(s, "normal");
1641         break;
1642     case DOC_ASICMODE_POWERDOWN:
1643         seq_puts(s, "powerdown");
1644         break;
1645     }
1646     seq_puts(s, ")\n");
1647     return 0;
1648 }
1649 DEFINE_SHOW_ATTRIBUTE(asic_mode);
1650 
1651 static int device_id_show(struct seq_file *s, void *p)
1652 {
1653     struct docg3 *docg3 = (struct docg3 *)s->private;
1654     int id;
1655 
1656     mutex_lock(&docg3->cascade->lock);
1657     id = doc_register_readb(docg3, DOC_DEVICESELECT);
1658     mutex_unlock(&docg3->cascade->lock);
1659 
1660     seq_printf(s, "DeviceId = %d\n", id);
1661     return 0;
1662 }
1663 DEFINE_SHOW_ATTRIBUTE(device_id);
1664 
1665 static int protection_show(struct seq_file *s, void *p)
1666 {
1667     struct docg3 *docg3 = (struct docg3 *)s->private;
1668     int protect, dps0, dps0_low, dps0_high, dps1, dps1_low, dps1_high;
1669 
1670     mutex_lock(&docg3->cascade->lock);
1671     protect = doc_register_readb(docg3, DOC_PROTECTION);
1672     dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
1673     dps0_low = doc_register_readw(docg3, DOC_DPS0_ADDRLOW);
1674     dps0_high = doc_register_readw(docg3, DOC_DPS0_ADDRHIGH);
1675     dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
1676     dps1_low = doc_register_readw(docg3, DOC_DPS1_ADDRLOW);
1677     dps1_high = doc_register_readw(docg3, DOC_DPS1_ADDRHIGH);
1678     mutex_unlock(&docg3->cascade->lock);
1679 
1680     seq_printf(s, "Protection = 0x%02x (", protect);
1681     if (protect & DOC_PROTECT_FOUNDRY_OTP_LOCK)
1682         seq_puts(s, "FOUNDRY_OTP_LOCK,");
1683     if (protect & DOC_PROTECT_CUSTOMER_OTP_LOCK)
1684         seq_puts(s, "CUSTOMER_OTP_LOCK,");
1685     if (protect & DOC_PROTECT_LOCK_INPUT)
1686         seq_puts(s, "LOCK_INPUT,");
1687     if (protect & DOC_PROTECT_STICKY_LOCK)
1688         seq_puts(s, "STICKY_LOCK,");
1689     if (protect & DOC_PROTECT_PROTECTION_ENABLED)
1690         seq_puts(s, "PROTECTION ON,");
1691     if (protect & DOC_PROTECT_IPL_DOWNLOAD_LOCK)
1692         seq_puts(s, "IPL_DOWNLOAD_LOCK,");
1693     if (protect & DOC_PROTECT_PROTECTION_ERROR)
1694         seq_puts(s, "PROTECT_ERR,");
1695     else
1696         seq_puts(s, "NO_PROTECT_ERR");
1697     seq_puts(s, ")\n");
1698 
1699     seq_printf(s, "DPS0 = 0x%02x : Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1700            dps0, dps0_low, dps0_high,
1701            !!(dps0 & DOC_DPS_OTP_PROTECTED),
1702            !!(dps0 & DOC_DPS_READ_PROTECTED),
1703            !!(dps0 & DOC_DPS_WRITE_PROTECTED),
1704            !!(dps0 & DOC_DPS_HW_LOCK_ENABLED),
1705            !!(dps0 & DOC_DPS_KEY_OK));
1706     seq_printf(s, "DPS1 = 0x%02x : Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1707            dps1, dps1_low, dps1_high,
1708            !!(dps1 & DOC_DPS_OTP_PROTECTED),
1709            !!(dps1 & DOC_DPS_READ_PROTECTED),
1710            !!(dps1 & DOC_DPS_WRITE_PROTECTED),
1711            !!(dps1 & DOC_DPS_HW_LOCK_ENABLED),
1712            !!(dps1 & DOC_DPS_KEY_OK));
1713     return 0;
1714 }
1715 DEFINE_SHOW_ATTRIBUTE(protection);
1716 
1717 static void __init doc_dbg_register(struct mtd_info *floor)
1718 {
1719     struct dentry *root = floor->dbg.dfs_dir;
1720     struct docg3 *docg3 = floor->priv;
1721 
1722     if (IS_ERR_OR_NULL(root)) {
1723         if (IS_ENABLED(CONFIG_DEBUG_FS) &&
1724             !IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER))
1725             dev_warn(floor->dev.parent,
1726                  "CONFIG_MTD_PARTITIONED_MASTER must be enabled to expose debugfs stuff\n");
1727         return;
1728     }
1729 
1730     debugfs_create_file("docg3_flashcontrol", S_IRUSR, root, docg3,
1731                 &flashcontrol_fops);
1732     debugfs_create_file("docg3_asic_mode", S_IRUSR, root, docg3,
1733                 &asic_mode_fops);
1734     debugfs_create_file("docg3_device_id", S_IRUSR, root, docg3,
1735                 &device_id_fops);
1736     debugfs_create_file("docg3_protection", S_IRUSR, root, docg3,
1737                 &protection_fops);
1738 }
1739 
1740 /**
1741  * doc_set_driver_info - Fill the mtd_info structure and docg3 structure
1742  * @chip_id: The chip ID of the supported chip
1743  * @mtd: The structure to fill
1744  */
1745 static int __init doc_set_driver_info(int chip_id, struct mtd_info *mtd)
1746 {
1747     struct docg3 *docg3 = mtd->priv;
1748     int cfg;
1749 
1750     cfg = doc_register_readb(docg3, DOC_CONFIGURATION);
1751     docg3->if_cfg = (cfg & DOC_CONF_IF_CFG ? 1 : 0);
1752     docg3->reliable = reliable_mode;
1753 
1754     switch (chip_id) {
1755     case DOC_CHIPID_G3:
1756         mtd->name = devm_kasprintf(docg3->dev, GFP_KERNEL, "docg3.%d",
1757                        docg3->device_id);
1758         if (!mtd->name)
1759             return -ENOMEM;
1760         docg3->max_block = 2047;
1761         break;
1762     }
1763     mtd->type = MTD_NANDFLASH;
1764     mtd->flags = MTD_CAP_NANDFLASH;
1765     mtd->size = (docg3->max_block + 1) * DOC_LAYOUT_BLOCK_SIZE;
1766     if (docg3->reliable == 2)
1767         mtd->size /= 2;
1768     mtd->erasesize = DOC_LAYOUT_BLOCK_SIZE * DOC_LAYOUT_NBPLANES;
1769     if (docg3->reliable == 2)
1770         mtd->erasesize /= 2;
1771     mtd->writebufsize = mtd->writesize = DOC_LAYOUT_PAGE_SIZE;
1772     mtd->oobsize = DOC_LAYOUT_OOB_SIZE;
1773     mtd->_erase = doc_erase;
1774     mtd->_read_oob = doc_read_oob;
1775     mtd->_write_oob = doc_write_oob;
1776     mtd->_block_isbad = doc_block_isbad;
1777     mtd_set_ooblayout(mtd, &nand_ooblayout_docg3_ops);
1778     mtd->oobavail = 8;
1779     mtd->ecc_strength = DOC_ECC_BCH_T;
1780 
1781     return 0;
1782 }
1783 
1784 /**
1785  * doc_probe_device - Check if a device is available
1786  * @cascade: the cascade of chips this devices will belong to
1787  * @floor: the floor of the probed device
1788  * @dev: the device
1789  *
1790  * Checks whether a device at the specified IO range, and floor is available.
1791  *
1792  * Returns a mtd_info struct if there is a device, ENODEV if none found, ENOMEM
1793  * if a memory allocation failed. If floor 0 is checked, a reset of the ASIC is
1794  * launched.
1795  */
1796 static struct mtd_info * __init
1797 doc_probe_device(struct docg3_cascade *cascade, int floor, struct device *dev)
1798 {
1799     int ret, bbt_nbpages;
1800     u16 chip_id, chip_id_inv;
1801     struct docg3 *docg3;
1802     struct mtd_info *mtd;
1803 
1804     ret = -ENOMEM;
1805     docg3 = kzalloc(sizeof(struct docg3), GFP_KERNEL);
1806     if (!docg3)
1807         goto nomem1;
1808     mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
1809     if (!mtd)
1810         goto nomem2;
1811     mtd->priv = docg3;
1812     mtd->dev.parent = dev;
1813     bbt_nbpages = DIV_ROUND_UP(docg3->max_block + 1,
1814                    8 * DOC_LAYOUT_PAGE_SIZE);
1815     docg3->bbt = kcalloc(DOC_LAYOUT_PAGE_SIZE, bbt_nbpages, GFP_KERNEL);
1816     if (!docg3->bbt)
1817         goto nomem3;
1818 
1819     docg3->dev = dev;
1820     docg3->device_id = floor;
1821     docg3->cascade = cascade;
1822     doc_set_device_id(docg3, docg3->device_id);
1823     if (!floor)
1824         doc_set_asic_mode(docg3, DOC_ASICMODE_RESET);
1825     doc_set_asic_mode(docg3, DOC_ASICMODE_NORMAL);
1826 
1827     chip_id = doc_register_readw(docg3, DOC_CHIPID);
1828     chip_id_inv = doc_register_readw(docg3, DOC_CHIPID_INV);
1829 
1830     ret = 0;
1831     if (chip_id != (u16)(~chip_id_inv)) {
1832         goto nomem4;
1833     }
1834 
1835     switch (chip_id) {
1836     case DOC_CHIPID_G3:
1837         doc_info("Found a G3 DiskOnChip at addr %p, floor %d\n",
1838              docg3->cascade->base, floor);
1839         break;
1840     default:
1841         doc_err("Chip id %04x is not a DiskOnChip G3 chip\n", chip_id);
1842         goto nomem4;
1843     }
1844 
1845     ret = doc_set_driver_info(chip_id, mtd);
1846     if (ret)
1847         goto nomem4;
1848 
1849     doc_hamming_ecc_init(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ);
1850     doc_reload_bbt(docg3);
1851     return mtd;
1852 
1853 nomem4:
1854     kfree(docg3->bbt);
1855 nomem3:
1856     kfree(mtd);
1857 nomem2:
1858     kfree(docg3);
1859 nomem1:
1860     return ret ? ERR_PTR(ret) : NULL;
1861 }
1862 
1863 /**
1864  * doc_release_device - Release a docg3 floor
1865  * @mtd: the device
1866  */
1867 static void doc_release_device(struct mtd_info *mtd)
1868 {
1869     struct docg3 *docg3 = mtd->priv;
1870 
1871     mtd_device_unregister(mtd);
1872     kfree(docg3->bbt);
1873     kfree(docg3);
1874     kfree(mtd);
1875 }
1876 
1877 /**
1878  * docg3_resume - Awakens docg3 floor
1879  * @pdev: platfrom device
1880  *
1881  * Returns 0 (always successful)
1882  */
1883 static int docg3_resume(struct platform_device *pdev)
1884 {
1885     int i;
1886     struct docg3_cascade *cascade;
1887     struct mtd_info **docg3_floors, *mtd;
1888     struct docg3 *docg3;
1889 
1890     cascade = platform_get_drvdata(pdev);
1891     docg3_floors = cascade->floors;
1892     mtd = docg3_floors[0];
1893     docg3 = mtd->priv;
1894 
1895     doc_dbg("docg3_resume()\n");
1896     for (i = 0; i < 12; i++)
1897         doc_readb(docg3, DOC_IOSPACE_IPL);
1898     return 0;
1899 }
1900 
1901 /**
1902  * docg3_suspend - Put in low power mode the docg3 floor
1903  * @pdev: platform device
1904  * @state: power state
1905  *
1906  * Shuts off most of docg3 circuitery to lower power consumption.
1907  *
1908  * Returns 0 if suspend succeeded, -EIO if chip refused suspend
1909  */
1910 static int docg3_suspend(struct platform_device *pdev, pm_message_t state)
1911 {
1912     int floor, i;
1913     struct docg3_cascade *cascade;
1914     struct mtd_info **docg3_floors, *mtd;
1915     struct docg3 *docg3;
1916     u8 ctrl, pwr_down;
1917 
1918     cascade = platform_get_drvdata(pdev);
1919     docg3_floors = cascade->floors;
1920     for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
1921         mtd = docg3_floors[floor];
1922         if (!mtd)
1923             continue;
1924         docg3 = mtd->priv;
1925 
1926         doc_writeb(docg3, floor, DOC_DEVICESELECT);
1927         ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
1928         ctrl &= ~DOC_CTRL_VIOLATION & ~DOC_CTRL_CE;
1929         doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
1930 
1931         for (i = 0; i < 10; i++) {
1932             usleep_range(3000, 4000);
1933             pwr_down = doc_register_readb(docg3, DOC_POWERMODE);
1934             if (pwr_down & DOC_POWERDOWN_READY)
1935                 break;
1936         }
1937         if (pwr_down & DOC_POWERDOWN_READY) {
1938             doc_dbg("docg3_suspend(): floor %d powerdown ok\n",
1939                 floor);
1940         } else {
1941             doc_err("docg3_suspend(): floor %d powerdown failed\n",
1942                 floor);
1943             return -EIO;
1944         }
1945     }
1946 
1947     mtd = docg3_floors[0];
1948     docg3 = mtd->priv;
1949     doc_set_asic_mode(docg3, DOC_ASICMODE_POWERDOWN);
1950     return 0;
1951 }
1952 
1953 /**
1954  * doc_probe - Probe the IO space for a DiskOnChip G3 chip
1955  * @pdev: platform device
1956  *
1957  * Probes for a G3 chip at the specified IO space in the platform data
1958  * ressources. The floor 0 must be available.
1959  *
1960  * Returns 0 on success, -ENOMEM, -ENXIO on error
1961  */
1962 static int __init docg3_probe(struct platform_device *pdev)
1963 {
1964     struct device *dev = &pdev->dev;
1965     struct mtd_info *mtd;
1966     struct resource *ress;
1967     void __iomem *base;
1968     int ret, floor;
1969     struct docg3_cascade *cascade;
1970 
1971     ret = -ENXIO;
1972     ress = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1973     if (!ress) {
1974         dev_err(dev, "No I/O memory resource defined\n");
1975         return ret;
1976     }
1977     base = devm_ioremap(dev, ress->start, DOC_IOSPACE_SIZE);
1978 
1979     ret = -ENOMEM;
1980     cascade = devm_kcalloc(dev, DOC_MAX_NBFLOORS, sizeof(*cascade),
1981                    GFP_KERNEL);
1982     if (!cascade)
1983         return ret;
1984     cascade->base = base;
1985     mutex_init(&cascade->lock);
1986     cascade->bch = bch_init(DOC_ECC_BCH_M, DOC_ECC_BCH_T,
1987                 DOC_ECC_BCH_PRIMPOLY, false);
1988     if (!cascade->bch)
1989         return ret;
1990 
1991     for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
1992         mtd = doc_probe_device(cascade, floor, dev);
1993         if (IS_ERR(mtd)) {
1994             ret = PTR_ERR(mtd);
1995             goto err_probe;
1996         }
1997         if (!mtd) {
1998             if (floor == 0)
1999                 goto notfound;
2000             else
2001                 continue;
2002         }
2003         cascade->floors[floor] = mtd;
2004         ret = mtd_device_parse_register(mtd, part_probes, NULL, NULL,
2005                         0);
2006         if (ret)
2007             goto err_probe;
2008 
2009         doc_dbg_register(cascade->floors[floor]);
2010     }
2011 
2012     ret = doc_register_sysfs(pdev, cascade);
2013     if (ret)
2014         goto err_probe;
2015 
2016     platform_set_drvdata(pdev, cascade);
2017     return 0;
2018 
2019 notfound:
2020     ret = -ENODEV;
2021     dev_info(dev, "No supported DiskOnChip found\n");
2022 err_probe:
2023     bch_free(cascade->bch);
2024     for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
2025         if (cascade->floors[floor])
2026             doc_release_device(cascade->floors[floor]);
2027     return ret;
2028 }
2029 
2030 /**
2031  * docg3_release - Release the driver
2032  * @pdev: the platform device
2033  *
2034  * Returns 0
2035  */
2036 static int docg3_release(struct platform_device *pdev)
2037 {
2038     struct docg3_cascade *cascade = platform_get_drvdata(pdev);
2039     struct docg3 *docg3 = cascade->floors[0]->priv;
2040     int floor;
2041 
2042     doc_unregister_sysfs(pdev, cascade);
2043     for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
2044         if (cascade->floors[floor])
2045             doc_release_device(cascade->floors[floor]);
2046 
2047     bch_free(docg3->cascade->bch);
2048     return 0;
2049 }
2050 
2051 #ifdef CONFIG_OF
2052 static const struct of_device_id docg3_dt_ids[] = {
2053     { .compatible = "m-systems,diskonchip-g3" },
2054     {}
2055 };
2056 MODULE_DEVICE_TABLE(of, docg3_dt_ids);
2057 #endif
2058 
2059 static struct platform_driver g3_driver = {
2060     .driver     = {
2061         .name   = "docg3",
2062         .of_match_table = of_match_ptr(docg3_dt_ids),
2063     },
2064     .suspend    = docg3_suspend,
2065     .resume     = docg3_resume,
2066     .remove     = docg3_release,
2067 };
2068 
2069 module_platform_driver_probe(g3_driver, docg3_probe);
2070 
2071 MODULE_LICENSE("GPL");
2072 MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
2073 MODULE_DESCRIPTION("MTD driver for DiskOnChip G3");