blob: 7f5044a79f0132e79e6dba05347b8b9044a8d603 [file] [log] [blame]
Maxim Levitsky9fc51a32010-02-22 20:39:39 +02001/*
2 * Copyright © 2009 - Maxim Levitsky
3 * Common routines & support for xD format
4 *
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.
8 */
9#include <linux/kernel.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020010#include <linux/mtd/rawnand.h>
Paul Gortmakera0e5cc52011-07-03 15:17:31 -040011#include <linux/module.h>
Artem Bityutskiy0c4a2352013-03-19 10:47:56 +020012#include <linux/sizes.h>
Maxim Levitsky9fc51a32010-02-22 20:39:39 +020013#include "sm_common.h"
14
Boris Brezillon987b9132016-02-03 20:04:59 +010015static int oob_sm_ooblayout_ecc(struct mtd_info *mtd, int section,
16 struct mtd_oob_region *oobregion)
17{
18 if (section > 1)
19 return -ERANGE;
20
21 oobregion->length = 3;
22 oobregion->offset = ((section + 1) * 8) - 3;
23
24 return 0;
25}
26
27static int oob_sm_ooblayout_free(struct mtd_info *mtd, int section,
28 struct mtd_oob_region *oobregion)
29{
30 switch (section) {
31 case 0:
32 /* reserved */
33 oobregion->offset = 0;
34 oobregion->length = 4;
35 break;
36 case 1:
37 /* LBA1 */
38 oobregion->offset = 6;
39 oobregion->length = 2;
40 break;
41 case 2:
42 /* LBA2 */
43 oobregion->offset = 11;
44 oobregion->length = 2;
45 break;
46 default:
47 return -ERANGE;
Maxim Levitsky9fc51a32010-02-22 20:39:39 +020048 }
Boris Brezillon987b9132016-02-03 20:04:59 +010049
50 return 0;
51}
52
53static const struct mtd_ooblayout_ops oob_sm_ops = {
54 .ecc = oob_sm_ooblayout_ecc,
55 .free = oob_sm_ooblayout_free,
Maxim Levitsky9fc51a32010-02-22 20:39:39 +020056};
57
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 */
63
Boris Brezillon987b9132016-02-03 20:04:59 +010064static int oob_sm_small_ooblayout_ecc(struct mtd_info *mtd, int section,
65 struct mtd_oob_region *oobregion)
66{
67 if (section)
68 return -ERANGE;
Maxim Levitsky9fc51a32010-02-22 20:39:39 +020069
Boris Brezillon987b9132016-02-03 20:04:59 +010070 oobregion->length = 3;
71 oobregion->offset = 0;
72
73 return 0;
74}
75
76static int oob_sm_small_ooblayout_free(struct mtd_info *mtd, int section,
77 struct mtd_oob_region *oobregion)
78{
79 switch (section) {
80 case 0:
81 /* reserved */
82 oobregion->offset = 3;
83 oobregion->length = 2;
84 break;
85 case 1:
86 /* LBA1 */
87 oobregion->offset = 6;
88 oobregion->length = 2;
89 break;
90 default:
91 return -ERANGE;
92 }
93
94 return 0;
95}
96
97static const struct mtd_ooblayout_ops oob_sm_small_ops = {
98 .ecc = oob_sm_small_ooblayout_ecc,
99 .free = oob_sm_small_ooblayout_free,
100};
Maxim Levitsky9fc51a32010-02-22 20:39:39 +0200101
102static int sm_block_markbad(struct mtd_info *mtd, loff_t ofs)
103{
104 struct mtd_oob_ops ops;
105 struct sm_oob oob;
Brian Norris5a0edb22013-07-30 17:52:58 -0700106 int ret;
Maxim Levitsky9fc51a32010-02-22 20:39:39 +0200107
108 memset(&oob, -1, SM_OOB_SIZE);
109 oob.block_status = 0x0F;
110
111 /* As long as this function is called on erase block boundaries
112 it will work correctly for 256 byte nand */
Brian Norris0612b9d2011-08-30 18:45:40 -0700113 ops.mode = MTD_OPS_PLACE_OOB;
Maxim Levitsky9fc51a32010-02-22 20:39:39 +0200114 ops.ooboffs = 0;
115 ops.ooblen = mtd->oobsize;
116 ops.oobbuf = (void *)&oob;
117 ops.datbuf = NULL;
118
119
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200120 ret = mtd_write_oob(mtd, ofs, &ops);
Maxim Levitsky9fc51a32010-02-22 20:39:39 +0200121 if (ret < 0 || ops.oobretlen != SM_OOB_SIZE) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530122 pr_notice("sm_common: can't mark sector at %i as bad\n",
123 (int)ofs);
Brian Norris5a0edb22013-07-30 17:52:58 -0700124 return -EIO;
125 }
Maxim Levitsky9fc51a32010-02-22 20:39:39 +0200126
Brian Norris5a0edb22013-07-30 17:52:58 -0700127 return 0;
Maxim Levitsky9fc51a32010-02-22 20:39:39 +0200128}
129
David Woodhouse2764fb42010-02-26 18:45:37 +0000130static struct nand_flash_dev nand_smartmedia_flash_ids[] = {
Artem Bityutskiy0c4a2352013-03-19 10:47:56 +0200131 LEGACY_ID_NAND("SmartMedia 2MiB 3,3V ROM", 0x5d, 2, SZ_8K, NAND_ROM),
132 LEGACY_ID_NAND("SmartMedia 4MiB 3,3V", 0xe3, 4, SZ_8K, 0),
133 LEGACY_ID_NAND("SmartMedia 4MiB 3,3/5V", 0xe5, 4, SZ_8K, 0),
134 LEGACY_ID_NAND("SmartMedia 4MiB 5V", 0x6b, 4, SZ_8K, 0),
135 LEGACY_ID_NAND("SmartMedia 4MiB 3,3V ROM", 0xd5, 4, SZ_8K, NAND_ROM),
136 LEGACY_ID_NAND("SmartMedia 8MiB 3,3V", 0xe6, 8, SZ_8K, 0),
137 LEGACY_ID_NAND("SmartMedia 8MiB 3,3V ROM", 0xd6, 8, SZ_8K, NAND_ROM),
138 LEGACY_ID_NAND("SmartMedia 16MiB 3,3V", 0x73, 16, SZ_16K, 0),
139 LEGACY_ID_NAND("SmartMedia 16MiB 3,3V ROM", 0x57, 16, SZ_16K, NAND_ROM),
140 LEGACY_ID_NAND("SmartMedia 32MiB 3,3V", 0x75, 32, SZ_16K, 0),
141 LEGACY_ID_NAND("SmartMedia 32MiB 3,3V ROM", 0x58, 32, SZ_16K, NAND_ROM),
142 LEGACY_ID_NAND("SmartMedia 64MiB 3,3V", 0x76, 64, SZ_16K, 0),
143 LEGACY_ID_NAND("SmartMedia 64MiB 3,3V ROM", 0xd9, 64, SZ_16K, NAND_ROM),
144 LEGACY_ID_NAND("SmartMedia 128MiB 3,3V", 0x79, 128, SZ_16K, 0),
145 LEGACY_ID_NAND("SmartMedia 128MiB 3,3V ROM", 0xda, 128, SZ_16K, NAND_ROM),
146 LEGACY_ID_NAND("SmartMedia 256MiB 3, 3V", 0x71, 256, SZ_16K, 0),
147 LEGACY_ID_NAND("SmartMedia 256MiB 3,3V ROM", 0x5b, 256, SZ_16K, NAND_ROM),
Artem Bityutskiy8dbfae12013-03-04 15:39:18 +0200148 {NULL}
David Woodhouse2764fb42010-02-26 18:45:37 +0000149};
150
Maxim Levitskyc3611572010-04-19 18:20:41 +0300151static struct nand_flash_dev nand_xd_flash_ids[] = {
Artem Bityutskiy0c4a2352013-03-19 10:47:56 +0200152 LEGACY_ID_NAND("xD 16MiB 3,3V", 0x73, 16, SZ_16K, 0),
153 LEGACY_ID_NAND("xD 32MiB 3,3V", 0x75, 32, SZ_16K, 0),
154 LEGACY_ID_NAND("xD 64MiB 3,3V", 0x76, 64, SZ_16K, 0),
155 LEGACY_ID_NAND("xD 128MiB 3,3V", 0x79, 128, SZ_16K, 0),
156 LEGACY_ID_NAND("xD 256MiB 3,3V", 0x71, 256, SZ_16K, NAND_BROKEN_XD),
157 LEGACY_ID_NAND("xD 512MiB 3,3V", 0xdc, 512, SZ_16K, NAND_BROKEN_XD),
158 LEGACY_ID_NAND("xD 1GiB 3,3V", 0xd3, 1024, SZ_16K, NAND_BROKEN_XD),
159 LEGACY_ID_NAND("xD 2GiB 3,3V", 0xd5, 2048, SZ_16K, NAND_BROKEN_XD),
Artem Bityutskiy8dbfae12013-03-04 15:39:18 +0200160 {NULL}
Maxim Levitskyc3611572010-04-19 18:20:41 +0300161};
162
163int sm_register_device(struct mtd_info *mtd, int smartmedia)
Maxim Levitsky9fc51a32010-02-22 20:39:39 +0200164{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100165 struct nand_chip *chip = mtd_to_nand(mtd);
Maxim Levitsky9fc51a32010-02-22 20:39:39 +0200166 int ret;
167
David Woodhouse2764fb42010-02-26 18:45:37 +0000168 chip->options |= NAND_SKIP_BBTSCAN;
Maxim Levitsky9fc51a32010-02-22 20:39:39 +0200169
170 /* Scan for card properties */
Maxim Levitskyc3611572010-04-19 18:20:41 +0300171 ret = nand_scan_ident(mtd, 1, smartmedia ?
172 nand_smartmedia_flash_ids : nand_xd_flash_ids);
Maxim Levitsky9fc51a32010-02-22 20:39:39 +0200173
174 if (ret)
175 return ret;
176
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300177 /* Bad block marker position */
Maxim Levitsky9fc51a32010-02-22 20:39:39 +0200178 chip->badblockpos = 0x05;
179 chip->badblockbits = 7;
180 chip->block_markbad = sm_block_markbad;
181
182 /* ECC layout */
183 if (mtd->writesize == SM_SECTOR_SIZE)
Boris Brezillon987b9132016-02-03 20:04:59 +0100184 mtd_set_ooblayout(mtd, &oob_sm_ops);
Maxim Levitsky9fc51a32010-02-22 20:39:39 +0200185 else if (mtd->writesize == SM_SMALL_PAGE)
Boris Brezillon987b9132016-02-03 20:04:59 +0100186 mtd_set_ooblayout(mtd, &oob_sm_small_ops);
Maxim Levitsky9fc51a32010-02-22 20:39:39 +0200187 else
188 return -ENODEV;
189
190 ret = nand_scan_tail(mtd);
191
192 if (ret)
193 return ret;
194
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100195 return mtd_device_register(mtd, NULL, 0);
Maxim Levitsky9fc51a32010-02-22 20:39:39 +0200196}
197EXPORT_SYMBOL_GPL(sm_register_device);
198
199MODULE_LICENSE("GPL");
200MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>");
201MODULE_DESCRIPTION("Common SmartMedia/xD functions");