Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright © 2009 - Maxim Levitsky
0004  * Common routines & support for xD format
0005  */
0006 #include <linux/kernel.h>
0007 #include <linux/mtd/rawnand.h>
0008 #include <linux/module.h>
0009 #include <linux/sizes.h>
0010 #include "sm_common.h"
0011 
0012 static int oob_sm_ooblayout_ecc(struct mtd_info *mtd, int section,
0013                 struct mtd_oob_region *oobregion)
0014 {
0015     if (section > 1)
0016         return -ERANGE;
0017 
0018     oobregion->length = 3;
0019     oobregion->offset = ((section + 1) * 8) - 3;
0020 
0021     return 0;
0022 }
0023 
0024 static int oob_sm_ooblayout_free(struct mtd_info *mtd, int section,
0025                  struct mtd_oob_region *oobregion)
0026 {
0027     switch (section) {
0028     case 0:
0029         /* reserved */
0030         oobregion->offset = 0;
0031         oobregion->length = 4;
0032         break;
0033     case 1:
0034         /* LBA1 */
0035         oobregion->offset = 6;
0036         oobregion->length = 2;
0037         break;
0038     case 2:
0039         /* LBA2 */
0040         oobregion->offset = 11;
0041         oobregion->length = 2;
0042         break;
0043     default:
0044         return -ERANGE;
0045     }
0046 
0047     return 0;
0048 }
0049 
0050 static const struct mtd_ooblayout_ops oob_sm_ops = {
0051     .ecc = oob_sm_ooblayout_ecc,
0052     .free = oob_sm_ooblayout_free,
0053 };
0054 
0055 /* NOTE: This layout is not compatabable with SmartMedia, */
0056 /* because the 256 byte devices have page depenent oob layout */
0057 /* However it does preserve the bad block markers */
0058 /* If you use smftl, it will bypass this and work correctly */
0059 /* If you not, then you break SmartMedia compliance anyway */
0060 
0061 static int oob_sm_small_ooblayout_ecc(struct mtd_info *mtd, int section,
0062                       struct mtd_oob_region *oobregion)
0063 {
0064     if (section)
0065         return -ERANGE;
0066 
0067     oobregion->length = 3;
0068     oobregion->offset = 0;
0069 
0070     return 0;
0071 }
0072 
0073 static int oob_sm_small_ooblayout_free(struct mtd_info *mtd, int section,
0074                        struct mtd_oob_region *oobregion)
0075 {
0076     switch (section) {
0077     case 0:
0078         /* reserved */
0079         oobregion->offset = 3;
0080         oobregion->length = 2;
0081         break;
0082     case 1:
0083         /* LBA1 */
0084         oobregion->offset = 6;
0085         oobregion->length = 2;
0086         break;
0087     default:
0088         return -ERANGE;
0089     }
0090 
0091     return 0;
0092 }
0093 
0094 static const struct mtd_ooblayout_ops oob_sm_small_ops = {
0095     .ecc = oob_sm_small_ooblayout_ecc,
0096     .free = oob_sm_small_ooblayout_free,
0097 };
0098 
0099 static int sm_block_markbad(struct nand_chip *chip, loff_t ofs)
0100 {
0101     struct mtd_info *mtd = nand_to_mtd(chip);
0102     struct mtd_oob_ops ops;
0103     struct sm_oob oob;
0104     int ret;
0105 
0106     memset(&oob, -1, SM_OOB_SIZE);
0107     oob.block_status = 0x0F;
0108 
0109     /* As long as this function is called on erase block boundaries
0110         it will work correctly for 256 byte nand */
0111     ops.mode = MTD_OPS_PLACE_OOB;
0112     ops.ooboffs = 0;
0113     ops.ooblen = mtd->oobsize;
0114     ops.oobbuf = (void *)&oob;
0115     ops.datbuf = NULL;
0116 
0117 
0118     ret = mtd_write_oob(mtd, ofs, &ops);
0119     if (ret < 0 || ops.oobretlen != SM_OOB_SIZE) {
0120         pr_notice("sm_common: can't mark sector at %i as bad\n",
0121               (int)ofs);
0122         return -EIO;
0123     }
0124 
0125     return 0;
0126 }
0127 
0128 static struct nand_flash_dev nand_smartmedia_flash_ids[] = {
0129     LEGACY_ID_NAND("SmartMedia 2MiB 3,3V ROM",   0x5d, 2,   SZ_8K, NAND_ROM),
0130     LEGACY_ID_NAND("SmartMedia 4MiB 3,3V",       0xe3, 4,   SZ_8K, 0),
0131     LEGACY_ID_NAND("SmartMedia 4MiB 3,3/5V",     0xe5, 4,   SZ_8K, 0),
0132     LEGACY_ID_NAND("SmartMedia 4MiB 5V",         0x6b, 4,   SZ_8K, 0),
0133     LEGACY_ID_NAND("SmartMedia 4MiB 3,3V ROM",   0xd5, 4,   SZ_8K, NAND_ROM),
0134     LEGACY_ID_NAND("SmartMedia 8MiB 3,3V",       0xe6, 8,   SZ_8K, 0),
0135     LEGACY_ID_NAND("SmartMedia 8MiB 3,3V ROM",   0xd6, 8,   SZ_8K, NAND_ROM),
0136     LEGACY_ID_NAND("SmartMedia 16MiB 3,3V",      0x73, 16,  SZ_16K, 0),
0137     LEGACY_ID_NAND("SmartMedia 16MiB 3,3V ROM",  0x57, 16,  SZ_16K, NAND_ROM),
0138     LEGACY_ID_NAND("SmartMedia 32MiB 3,3V",      0x75, 32,  SZ_16K, 0),
0139     LEGACY_ID_NAND("SmartMedia 32MiB 3,3V ROM",  0x58, 32,  SZ_16K, NAND_ROM),
0140     LEGACY_ID_NAND("SmartMedia 64MiB 3,3V",      0x76, 64,  SZ_16K, 0),
0141     LEGACY_ID_NAND("SmartMedia 64MiB 3,3V ROM",  0xd9, 64,  SZ_16K, NAND_ROM),
0142     LEGACY_ID_NAND("SmartMedia 128MiB 3,3V",     0x79, 128, SZ_16K, 0),
0143     LEGACY_ID_NAND("SmartMedia 128MiB 3,3V ROM", 0xda, 128, SZ_16K, NAND_ROM),
0144     LEGACY_ID_NAND("SmartMedia 256MiB 3, 3V",    0x71, 256, SZ_16K, 0),
0145     LEGACY_ID_NAND("SmartMedia 256MiB 3,3V ROM", 0x5b, 256, SZ_16K, NAND_ROM),
0146     {NULL}
0147 };
0148 
0149 static struct nand_flash_dev nand_xd_flash_ids[] = {
0150     LEGACY_ID_NAND("xD 16MiB 3,3V",  0x73, 16,   SZ_16K, 0),
0151     LEGACY_ID_NAND("xD 32MiB 3,3V",  0x75, 32,   SZ_16K, 0),
0152     LEGACY_ID_NAND("xD 64MiB 3,3V",  0x76, 64,   SZ_16K, 0),
0153     LEGACY_ID_NAND("xD 128MiB 3,3V", 0x79, 128,  SZ_16K, 0),
0154     LEGACY_ID_NAND("xD 256MiB 3,3V", 0x71, 256,  SZ_16K, NAND_BROKEN_XD),
0155     LEGACY_ID_NAND("xD 512MiB 3,3V", 0xdc, 512,  SZ_16K, NAND_BROKEN_XD),
0156     LEGACY_ID_NAND("xD 1GiB 3,3V",   0xd3, 1024, SZ_16K, NAND_BROKEN_XD),
0157     LEGACY_ID_NAND("xD 2GiB 3,3V",   0xd5, 2048, SZ_16K, NAND_BROKEN_XD),
0158     {NULL}
0159 };
0160 
0161 static int sm_attach_chip(struct nand_chip *chip)
0162 {
0163     struct mtd_info *mtd = nand_to_mtd(chip);
0164 
0165     /* Bad block marker position */
0166     chip->badblockpos = 0x05;
0167     chip->badblockbits = 7;
0168     chip->legacy.block_markbad = sm_block_markbad;
0169 
0170     /* ECC layout */
0171     if (mtd->writesize == SM_SECTOR_SIZE)
0172         mtd_set_ooblayout(mtd, &oob_sm_ops);
0173     else if (mtd->writesize == SM_SMALL_PAGE)
0174         mtd_set_ooblayout(mtd, &oob_sm_small_ops);
0175     else
0176         return -ENODEV;
0177 
0178     return 0;
0179 }
0180 
0181 static const struct nand_controller_ops sm_controller_ops = {
0182     .attach_chip = sm_attach_chip,
0183 };
0184 
0185 int sm_register_device(struct mtd_info *mtd, int smartmedia)
0186 {
0187     struct nand_chip *chip = mtd_to_nand(mtd);
0188     struct nand_flash_dev *flash_ids;
0189     int ret;
0190 
0191     chip->options |= NAND_SKIP_BBTSCAN;
0192 
0193     /* Scan for card properties */
0194     chip->legacy.dummy_controller.ops = &sm_controller_ops;
0195     flash_ids = smartmedia ? nand_smartmedia_flash_ids : nand_xd_flash_ids;
0196     ret = nand_scan_with_ids(chip, 1, flash_ids);
0197     if (ret)
0198         return ret;
0199 
0200     ret = mtd_device_register(mtd, NULL, 0);
0201     if (ret)
0202         nand_cleanup(chip);
0203 
0204     return ret;
0205 }
0206 EXPORT_SYMBOL_GPL(sm_register_device);
0207 
0208 MODULE_LICENSE("GPL");
0209 MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>");
0210 MODULE_DESCRIPTION("Common SmartMedia/xD functions");