2 * Copyright © 2009 - Maxim Levitsky
3 * Common routines & support for xD format
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 #include <linux/kernel.h>
10 #include <linux/mtd/rawnand.h>
11 #include <linux/module.h>
12 #include <linux/sizes.h>
13 #include "sm_common.h"
15 static int oob_sm_ooblayout_ecc(struct mtd_info *mtd, int section,
16 struct mtd_oob_region *oobregion)
21 oobregion->length = 3;
22 oobregion->offset = ((section + 1) * 8) - 3;
27 static int oob_sm_ooblayout_free(struct mtd_info *mtd, int section,
28 struct mtd_oob_region *oobregion)
33 oobregion->offset = 0;
34 oobregion->length = 4;
38 oobregion->offset = 6;
39 oobregion->length = 2;
43 oobregion->offset = 11;
44 oobregion->length = 2;
53 static const struct mtd_ooblayout_ops oob_sm_ops = {
54 .ecc = oob_sm_ooblayout_ecc,
55 .free = oob_sm_ooblayout_free,
58 /* NOTE: This layout is is not compatabable with SmartMedia, */
59 /* because the 256 byte devices have page depenent oob layout */
60 /* However it does preserve the bad block markers */
61 /* If you use smftl, it will bypass this and work correctly */
62 /* If you not, then you break SmartMedia compliance anyway */
64 static int oob_sm_small_ooblayout_ecc(struct mtd_info *mtd, int section,
65 struct mtd_oob_region *oobregion)
70 oobregion->length = 3;
71 oobregion->offset = 0;
76 static int oob_sm_small_ooblayout_free(struct mtd_info *mtd, int section,
77 struct mtd_oob_region *oobregion)
82 oobregion->offset = 3;
83 oobregion->length = 2;
87 oobregion->offset = 6;
88 oobregion->length = 2;
97 static const struct mtd_ooblayout_ops oob_sm_small_ops = {
98 .ecc = oob_sm_small_ooblayout_ecc,
99 .free = oob_sm_small_ooblayout_free,
102 static int sm_block_markbad(struct nand_chip *chip, loff_t ofs)
104 struct mtd_info *mtd = nand_to_mtd(chip);
105 struct mtd_oob_ops ops;
109 memset(&oob, -1, SM_OOB_SIZE);
110 oob.block_status = 0x0F;
112 /* As long as this function is called on erase block boundaries
113 it will work correctly for 256 byte nand */
114 ops.mode = MTD_OPS_PLACE_OOB;
116 ops.ooblen = mtd->oobsize;
117 ops.oobbuf = (void *)&oob;
121 ret = mtd_write_oob(mtd, ofs, &ops);
122 if (ret < 0 || ops.oobretlen != SM_OOB_SIZE) {
123 pr_notice("sm_common: can't mark sector at %i as bad\n",
131 static struct nand_flash_dev nand_smartmedia_flash_ids[] = {
132 LEGACY_ID_NAND("SmartMedia 2MiB 3,3V ROM", 0x5d, 2, SZ_8K, NAND_ROM),
133 LEGACY_ID_NAND("SmartMedia 4MiB 3,3V", 0xe3, 4, SZ_8K, 0),
134 LEGACY_ID_NAND("SmartMedia 4MiB 3,3/5V", 0xe5, 4, SZ_8K, 0),
135 LEGACY_ID_NAND("SmartMedia 4MiB 5V", 0x6b, 4, SZ_8K, 0),
136 LEGACY_ID_NAND("SmartMedia 4MiB 3,3V ROM", 0xd5, 4, SZ_8K, NAND_ROM),
137 LEGACY_ID_NAND("SmartMedia 8MiB 3,3V", 0xe6, 8, SZ_8K, 0),
138 LEGACY_ID_NAND("SmartMedia 8MiB 3,3V ROM", 0xd6, 8, SZ_8K, NAND_ROM),
139 LEGACY_ID_NAND("SmartMedia 16MiB 3,3V", 0x73, 16, SZ_16K, 0),
140 LEGACY_ID_NAND("SmartMedia 16MiB 3,3V ROM", 0x57, 16, SZ_16K, NAND_ROM),
141 LEGACY_ID_NAND("SmartMedia 32MiB 3,3V", 0x75, 32, SZ_16K, 0),
142 LEGACY_ID_NAND("SmartMedia 32MiB 3,3V ROM", 0x58, 32, SZ_16K, NAND_ROM),
143 LEGACY_ID_NAND("SmartMedia 64MiB 3,3V", 0x76, 64, SZ_16K, 0),
144 LEGACY_ID_NAND("SmartMedia 64MiB 3,3V ROM", 0xd9, 64, SZ_16K, NAND_ROM),
145 LEGACY_ID_NAND("SmartMedia 128MiB 3,3V", 0x79, 128, SZ_16K, 0),
146 LEGACY_ID_NAND("SmartMedia 128MiB 3,3V ROM", 0xda, 128, SZ_16K, NAND_ROM),
147 LEGACY_ID_NAND("SmartMedia 256MiB 3, 3V", 0x71, 256, SZ_16K, 0),
148 LEGACY_ID_NAND("SmartMedia 256MiB 3,3V ROM", 0x5b, 256, SZ_16K, NAND_ROM),
152 static struct nand_flash_dev nand_xd_flash_ids[] = {
153 LEGACY_ID_NAND("xD 16MiB 3,3V", 0x73, 16, SZ_16K, 0),
154 LEGACY_ID_NAND("xD 32MiB 3,3V", 0x75, 32, SZ_16K, 0),
155 LEGACY_ID_NAND("xD 64MiB 3,3V", 0x76, 64, SZ_16K, 0),
156 LEGACY_ID_NAND("xD 128MiB 3,3V", 0x79, 128, SZ_16K, 0),
157 LEGACY_ID_NAND("xD 256MiB 3,3V", 0x71, 256, SZ_16K, NAND_BROKEN_XD),
158 LEGACY_ID_NAND("xD 512MiB 3,3V", 0xdc, 512, SZ_16K, NAND_BROKEN_XD),
159 LEGACY_ID_NAND("xD 1GiB 3,3V", 0xd3, 1024, SZ_16K, NAND_BROKEN_XD),
160 LEGACY_ID_NAND("xD 2GiB 3,3V", 0xd5, 2048, SZ_16K, NAND_BROKEN_XD),
164 static int sm_attach_chip(struct nand_chip *chip)
166 struct mtd_info *mtd = nand_to_mtd(chip);
168 /* Bad block marker position */
169 chip->badblockpos = 0x05;
170 chip->badblockbits = 7;
171 chip->legacy.block_markbad = sm_block_markbad;
174 if (mtd->writesize == SM_SECTOR_SIZE)
175 mtd_set_ooblayout(mtd, &oob_sm_ops);
176 else if (mtd->writesize == SM_SMALL_PAGE)
177 mtd_set_ooblayout(mtd, &oob_sm_small_ops);
184 static const struct nand_controller_ops sm_controller_ops = {
185 .attach_chip = sm_attach_chip,
188 int sm_register_device(struct mtd_info *mtd, int smartmedia)
190 struct nand_chip *chip = mtd_to_nand(mtd);
191 struct nand_flash_dev *flash_ids;
194 chip->options |= NAND_SKIP_BBTSCAN;
196 /* Scan for card properties */
197 chip->legacy.dummy_controller.ops = &sm_controller_ops;
198 flash_ids = smartmedia ? nand_smartmedia_flash_ids : nand_xd_flash_ids;
199 ret = nand_scan_with_ids(chip, 1, flash_ids);
203 ret = mtd_device_register(mtd, NULL, 0);
209 EXPORT_SYMBOL_GPL(sm_register_device);
211 MODULE_LICENSE("GPL");
213 MODULE_DESCRIPTION("Common SmartMedia/xD functions");