1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 29fc51a37SMaxim Levitsky /* 39fc51a37SMaxim Levitsky * Copyright © 2009 - Maxim Levitsky 49fc51a37SMaxim Levitsky * Common routines & support for xD format 59fc51a37SMaxim Levitsky */ 69fc51a37SMaxim Levitsky #include <linux/kernel.h> 7d4092d76SBoris Brezillon #include <linux/mtd/rawnand.h> 8a0e5cc58SPaul Gortmaker #include <linux/module.h> 90c4a235cSArtem Bityutskiy #include <linux/sizes.h> 109fc51a37SMaxim Levitsky #include "sm_common.h" 119fc51a37SMaxim Levitsky 12987b913cSBoris Brezillon static int oob_sm_ooblayout_ecc(struct mtd_info *mtd, int section, 13987b913cSBoris Brezillon struct mtd_oob_region *oobregion) 14987b913cSBoris Brezillon { 15987b913cSBoris Brezillon if (section > 1) 16987b913cSBoris Brezillon return -ERANGE; 17987b913cSBoris Brezillon 18987b913cSBoris Brezillon oobregion->length = 3; 19987b913cSBoris Brezillon oobregion->offset = ((section + 1) * 8) - 3; 20987b913cSBoris Brezillon 21987b913cSBoris Brezillon return 0; 229fc51a37SMaxim Levitsky } 23987b913cSBoris Brezillon 24987b913cSBoris Brezillon static int oob_sm_ooblayout_free(struct mtd_info *mtd, int section, 25987b913cSBoris Brezillon struct mtd_oob_region *oobregion) 26987b913cSBoris Brezillon { 27987b913cSBoris Brezillon switch (section) { 28987b913cSBoris Brezillon case 0: 29987b913cSBoris Brezillon /* reserved */ 30987b913cSBoris Brezillon oobregion->offset = 0; 31987b913cSBoris Brezillon oobregion->length = 4; 32987b913cSBoris Brezillon break; 33987b913cSBoris Brezillon case 1: 34987b913cSBoris Brezillon /* LBA1 */ 35987b913cSBoris Brezillon oobregion->offset = 6; 36987b913cSBoris Brezillon oobregion->length = 2; 37987b913cSBoris Brezillon break; 38987b913cSBoris Brezillon case 2: 39987b913cSBoris Brezillon /* LBA2 */ 40987b913cSBoris Brezillon oobregion->offset = 11; 41987b913cSBoris Brezillon oobregion->length = 2; 42987b913cSBoris Brezillon break; 43987b913cSBoris Brezillon default: 44987b913cSBoris Brezillon return -ERANGE; 45987b913cSBoris Brezillon } 46987b913cSBoris Brezillon 47987b913cSBoris Brezillon return 0; 48987b913cSBoris Brezillon } 49987b913cSBoris Brezillon 50987b913cSBoris Brezillon static const struct mtd_ooblayout_ops oob_sm_ops = { 51987b913cSBoris Brezillon .ecc = oob_sm_ooblayout_ecc, 52987b913cSBoris Brezillon .free = oob_sm_ooblayout_free, 539fc51a37SMaxim Levitsky }; 549fc51a37SMaxim Levitsky 559fc51a37SMaxim Levitsky /* NOTE: This layout is is not compatabable with SmartMedia, */ 569fc51a37SMaxim Levitsky /* because the 256 byte devices have page depenent oob layout */ 579fc51a37SMaxim Levitsky /* However it does preserve the bad block markers */ 589fc51a37SMaxim Levitsky /* If you use smftl, it will bypass this and work correctly */ 599fc51a37SMaxim Levitsky /* If you not, then you break SmartMedia compliance anyway */ 609fc51a37SMaxim Levitsky 61987b913cSBoris Brezillon static int oob_sm_small_ooblayout_ecc(struct mtd_info *mtd, int section, 62987b913cSBoris Brezillon struct mtd_oob_region *oobregion) 63987b913cSBoris Brezillon { 64987b913cSBoris Brezillon if (section) 65987b913cSBoris Brezillon return -ERANGE; 669fc51a37SMaxim Levitsky 67987b913cSBoris Brezillon oobregion->length = 3; 68987b913cSBoris Brezillon oobregion->offset = 0; 69987b913cSBoris Brezillon 70987b913cSBoris Brezillon return 0; 71987b913cSBoris Brezillon } 72987b913cSBoris Brezillon 73987b913cSBoris Brezillon static int oob_sm_small_ooblayout_free(struct mtd_info *mtd, int section, 74987b913cSBoris Brezillon struct mtd_oob_region *oobregion) 75987b913cSBoris Brezillon { 76987b913cSBoris Brezillon switch (section) { 77987b913cSBoris Brezillon case 0: 78987b913cSBoris Brezillon /* reserved */ 79987b913cSBoris Brezillon oobregion->offset = 3; 80987b913cSBoris Brezillon oobregion->length = 2; 81987b913cSBoris Brezillon break; 82987b913cSBoris Brezillon case 1: 83987b913cSBoris Brezillon /* LBA1 */ 84987b913cSBoris Brezillon oobregion->offset = 6; 85987b913cSBoris Brezillon oobregion->length = 2; 86987b913cSBoris Brezillon break; 87987b913cSBoris Brezillon default: 88987b913cSBoris Brezillon return -ERANGE; 89987b913cSBoris Brezillon } 90987b913cSBoris Brezillon 91987b913cSBoris Brezillon return 0; 92987b913cSBoris Brezillon } 93987b913cSBoris Brezillon 94987b913cSBoris Brezillon static const struct mtd_ooblayout_ops oob_sm_small_ops = { 95987b913cSBoris Brezillon .ecc = oob_sm_small_ooblayout_ecc, 96987b913cSBoris Brezillon .free = oob_sm_small_ooblayout_free, 97987b913cSBoris Brezillon }; 989fc51a37SMaxim Levitsky 99c17556f5SBoris Brezillon static int sm_block_markbad(struct nand_chip *chip, loff_t ofs) 1009fc51a37SMaxim Levitsky { 101c17556f5SBoris Brezillon struct mtd_info *mtd = nand_to_mtd(chip); 1029fc51a37SMaxim Levitsky struct mtd_oob_ops ops; 1039fc51a37SMaxim Levitsky struct sm_oob oob; 1045a0edb25SBrian Norris int ret; 1059fc51a37SMaxim Levitsky 1069fc51a37SMaxim Levitsky memset(&oob, -1, SM_OOB_SIZE); 1079fc51a37SMaxim Levitsky oob.block_status = 0x0F; 1089fc51a37SMaxim Levitsky 1099fc51a37SMaxim Levitsky /* As long as this function is called on erase block boundaries 1109fc51a37SMaxim Levitsky it will work correctly for 256 byte nand */ 1110612b9ddSBrian Norris ops.mode = MTD_OPS_PLACE_OOB; 1129fc51a37SMaxim Levitsky ops.ooboffs = 0; 1139fc51a37SMaxim Levitsky ops.ooblen = mtd->oobsize; 1149fc51a37SMaxim Levitsky ops.oobbuf = (void *)&oob; 1159fc51a37SMaxim Levitsky ops.datbuf = NULL; 1169fc51a37SMaxim Levitsky 1179fc51a37SMaxim Levitsky 118a2cc5ba0SArtem Bityutskiy ret = mtd_write_oob(mtd, ofs, &ops); 1199fc51a37SMaxim Levitsky if (ret < 0 || ops.oobretlen != SM_OOB_SIZE) { 12063fa37f0SShreeya Patel pr_notice("sm_common: can't mark sector at %i as bad\n", 1219fc51a37SMaxim Levitsky (int)ofs); 1225a0edb25SBrian Norris return -EIO; 1235a0edb25SBrian Norris } 1249fc51a37SMaxim Levitsky 1255a0edb25SBrian Norris return 0; 1269fc51a37SMaxim Levitsky } 1279fc51a37SMaxim Levitsky 1282764fb42SDavid Woodhouse static struct nand_flash_dev nand_smartmedia_flash_ids[] = { 1290c4a235cSArtem Bityutskiy LEGACY_ID_NAND("SmartMedia 2MiB 3,3V ROM", 0x5d, 2, SZ_8K, NAND_ROM), 1300c4a235cSArtem Bityutskiy LEGACY_ID_NAND("SmartMedia 4MiB 3,3V", 0xe3, 4, SZ_8K, 0), 1310c4a235cSArtem Bityutskiy LEGACY_ID_NAND("SmartMedia 4MiB 3,3/5V", 0xe5, 4, SZ_8K, 0), 1320c4a235cSArtem Bityutskiy LEGACY_ID_NAND("SmartMedia 4MiB 5V", 0x6b, 4, SZ_8K, 0), 1330c4a235cSArtem Bityutskiy LEGACY_ID_NAND("SmartMedia 4MiB 3,3V ROM", 0xd5, 4, SZ_8K, NAND_ROM), 1340c4a235cSArtem Bityutskiy LEGACY_ID_NAND("SmartMedia 8MiB 3,3V", 0xe6, 8, SZ_8K, 0), 1350c4a235cSArtem Bityutskiy LEGACY_ID_NAND("SmartMedia 8MiB 3,3V ROM", 0xd6, 8, SZ_8K, NAND_ROM), 1360c4a235cSArtem Bityutskiy LEGACY_ID_NAND("SmartMedia 16MiB 3,3V", 0x73, 16, SZ_16K, 0), 1370c4a235cSArtem Bityutskiy LEGACY_ID_NAND("SmartMedia 16MiB 3,3V ROM", 0x57, 16, SZ_16K, NAND_ROM), 1380c4a235cSArtem Bityutskiy LEGACY_ID_NAND("SmartMedia 32MiB 3,3V", 0x75, 32, SZ_16K, 0), 1390c4a235cSArtem Bityutskiy LEGACY_ID_NAND("SmartMedia 32MiB 3,3V ROM", 0x58, 32, SZ_16K, NAND_ROM), 1400c4a235cSArtem Bityutskiy LEGACY_ID_NAND("SmartMedia 64MiB 3,3V", 0x76, 64, SZ_16K, 0), 1410c4a235cSArtem Bityutskiy LEGACY_ID_NAND("SmartMedia 64MiB 3,3V ROM", 0xd9, 64, SZ_16K, NAND_ROM), 1420c4a235cSArtem Bityutskiy LEGACY_ID_NAND("SmartMedia 128MiB 3,3V", 0x79, 128, SZ_16K, 0), 1430c4a235cSArtem Bityutskiy LEGACY_ID_NAND("SmartMedia 128MiB 3,3V ROM", 0xda, 128, SZ_16K, NAND_ROM), 1440c4a235cSArtem Bityutskiy LEGACY_ID_NAND("SmartMedia 256MiB 3, 3V", 0x71, 256, SZ_16K, 0), 1450c4a235cSArtem Bityutskiy LEGACY_ID_NAND("SmartMedia 256MiB 3,3V ROM", 0x5b, 256, SZ_16K, NAND_ROM), 1468dbfae1eSArtem Bityutskiy {NULL} 1472764fb42SDavid Woodhouse }; 1482764fb42SDavid Woodhouse 149c3611570SMaxim Levitsky static struct nand_flash_dev nand_xd_flash_ids[] = { 1500c4a235cSArtem Bityutskiy LEGACY_ID_NAND("xD 16MiB 3,3V", 0x73, 16, SZ_16K, 0), 1510c4a235cSArtem Bityutskiy LEGACY_ID_NAND("xD 32MiB 3,3V", 0x75, 32, SZ_16K, 0), 1520c4a235cSArtem Bityutskiy LEGACY_ID_NAND("xD 64MiB 3,3V", 0x76, 64, SZ_16K, 0), 1530c4a235cSArtem Bityutskiy LEGACY_ID_NAND("xD 128MiB 3,3V", 0x79, 128, SZ_16K, 0), 1540c4a235cSArtem Bityutskiy LEGACY_ID_NAND("xD 256MiB 3,3V", 0x71, 256, SZ_16K, NAND_BROKEN_XD), 1550c4a235cSArtem Bityutskiy LEGACY_ID_NAND("xD 512MiB 3,3V", 0xdc, 512, SZ_16K, NAND_BROKEN_XD), 1560c4a235cSArtem Bityutskiy LEGACY_ID_NAND("xD 1GiB 3,3V", 0xd3, 1024, SZ_16K, NAND_BROKEN_XD), 1570c4a235cSArtem Bityutskiy LEGACY_ID_NAND("xD 2GiB 3,3V", 0xd5, 2048, SZ_16K, NAND_BROKEN_XD), 1588dbfae1eSArtem Bityutskiy {NULL} 159c3611570SMaxim Levitsky }; 160c3611570SMaxim Levitsky 161fe13ae02SMiquel Raynal static int sm_attach_chip(struct nand_chip *chip) 1629fc51a37SMaxim Levitsky { 163fe13ae02SMiquel Raynal struct mtd_info *mtd = nand_to_mtd(chip); 1649fc51a37SMaxim Levitsky 16525985edcSLucas De Marchi /* Bad block marker position */ 1669fc51a37SMaxim Levitsky chip->badblockpos = 0x05; 1679fc51a37SMaxim Levitsky chip->badblockbits = 7; 168cdc784c7SBoris Brezillon chip->legacy.block_markbad = sm_block_markbad; 1699fc51a37SMaxim Levitsky 1709fc51a37SMaxim Levitsky /* ECC layout */ 1719fc51a37SMaxim Levitsky if (mtd->writesize == SM_SECTOR_SIZE) 172987b913cSBoris Brezillon mtd_set_ooblayout(mtd, &oob_sm_ops); 1739fc51a37SMaxim Levitsky else if (mtd->writesize == SM_SMALL_PAGE) 174987b913cSBoris Brezillon mtd_set_ooblayout(mtd, &oob_sm_small_ops); 1759fc51a37SMaxim Levitsky else 1769fc51a37SMaxim Levitsky return -ENODEV; 1779fc51a37SMaxim Levitsky 178fe13ae02SMiquel Raynal return 0; 179fe13ae02SMiquel Raynal } 1809fc51a37SMaxim Levitsky 181fe13ae02SMiquel Raynal static const struct nand_controller_ops sm_controller_ops = { 182fe13ae02SMiquel Raynal .attach_chip = sm_attach_chip, 183fe13ae02SMiquel Raynal }; 184fe13ae02SMiquel Raynal 185fe13ae02SMiquel Raynal int sm_register_device(struct mtd_info *mtd, int smartmedia) 186fe13ae02SMiquel Raynal { 187fe13ae02SMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 188fe13ae02SMiquel Raynal struct nand_flash_dev *flash_ids; 189fe13ae02SMiquel Raynal int ret; 190fe13ae02SMiquel Raynal 191fe13ae02SMiquel Raynal chip->options |= NAND_SKIP_BBTSCAN; 192fe13ae02SMiquel Raynal 193fe13ae02SMiquel Raynal /* Scan for card properties */ 1947b6a9b28SBoris Brezillon chip->legacy.dummy_controller.ops = &sm_controller_ops; 195fe13ae02SMiquel Raynal flash_ids = smartmedia ? nand_smartmedia_flash_ids : nand_xd_flash_ids; 19600ad378fSBoris Brezillon ret = nand_scan_with_ids(chip, 1, flash_ids); 1979fc51a37SMaxim Levitsky if (ret) 1989fc51a37SMaxim Levitsky return ret; 1999fc51a37SMaxim Levitsky 20092aa292dSMiquel Raynal ret = mtd_device_register(mtd, NULL, 0); 20192aa292dSMiquel Raynal if (ret) 20292aa292dSMiquel Raynal nand_cleanup(chip); 20392aa292dSMiquel Raynal 20492aa292dSMiquel Raynal return ret; 2059fc51a37SMaxim Levitsky } 2069fc51a37SMaxim Levitsky EXPORT_SYMBOL_GPL(sm_register_device); 2079fc51a37SMaxim Levitsky 2089fc51a37SMaxim Levitsky MODULE_LICENSE("GPL"); 2099fc51a37SMaxim Levitsky MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>"); 2109fc51a37SMaxim Levitsky MODULE_DESCRIPTION("Common SmartMedia/xD functions"); 211