xref: /linux/drivers/mmc/core/quirks.h (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  *  This file contains work-arounds for many known SD/MMC
4  *  and SDIO hardware bugs.
5  *
6  *  Copyright (c) 2011 Andrei Warkentin <andreiw@motorola.com>
7  *  Copyright (c) 2011 Pierre Tardy <tardyp@gmail.com>
8  *  Inspired from pci fixup code:
9  *  Copyright (c) 1999 Martin Mares <mj@ucw.cz>
10  *
11  */
12 
13 #include <linux/of.h>
14 #include <linux/mmc/sdio_ids.h>
15 
16 #include "card.h"
17 
18 static const struct mmc_fixup __maybe_unused mmc_sd_fixups[] = {
19 	/*
20 	 * Kingston Canvas Go! Plus microSD cards never finish SD cache flush.
21 	 * This has so far only been observed on cards from 11/2019, while new
22 	 * cards from 2023/05 do not exhibit this behavior.
23 	 */
24 	_FIXUP_EXT("SD64G", CID_MANFID_KINGSTON_SD, 0x5449, 2019, 11,
25 		   0, -1ull, SDIO_ANY_ID, SDIO_ANY_ID, add_quirk_sd,
26 		   MMC_QUIRK_BROKEN_SD_CACHE, EXT_CSD_REV_ANY),
27 
28 	/*
29 	 * GIGASTONE Gaming Plus microSD cards manufactured on 02/2022 never
30 	 * clear Flush Cache bit and set Poweroff Notification Ready bit.
31 	 */
32 	_FIXUP_EXT("ASTC", CID_MANFID_GIGASTONE, 0x3456, 2022, 2,
33 		   0, -1ull, SDIO_ANY_ID, SDIO_ANY_ID, add_quirk_sd,
34 		   MMC_QUIRK_BROKEN_SD_CACHE | MMC_QUIRK_BROKEN_SD_POWEROFF_NOTIFY,
35 		   EXT_CSD_REV_ANY),
36 
37 	END_FIXUP
38 };
39 
40 static const struct mmc_fixup __maybe_unused mmc_blk_fixups[] = {
41 #define INAND_CMD38_ARG_EXT_CSD  113
42 #define INAND_CMD38_ARG_ERASE    0x00
43 #define INAND_CMD38_ARG_TRIM     0x01
44 #define INAND_CMD38_ARG_SECERASE 0x80
45 #define INAND_CMD38_ARG_SECTRIM1 0x81
46 #define INAND_CMD38_ARG_SECTRIM2 0x88
47 	/* CMD38 argument is passed through EXT_CSD[113] */
48 	MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
49 		  MMC_QUIRK_INAND_CMD38),
50 	MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
51 		  MMC_QUIRK_INAND_CMD38),
52 	MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
53 		  MMC_QUIRK_INAND_CMD38),
54 	MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
55 		  MMC_QUIRK_INAND_CMD38),
56 	MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
57 		  MMC_QUIRK_INAND_CMD38),
58 
59 	/*
60 	 * Some MMC cards experience performance degradation with CMD23
61 	 * instead of CMD12-bounded multiblock transfers. For now we'll
62 	 * black list what's bad...
63 	 * - Certain Toshiba cards.
64 	 *
65 	 * N.B. This doesn't affect SD cards.
66 	 */
67 	MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
68 		  MMC_QUIRK_BLK_NO_CMD23),
69 	MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
70 		  MMC_QUIRK_BLK_NO_CMD23),
71 	MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
72 		  MMC_QUIRK_BLK_NO_CMD23),
73 	MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
74 		  MMC_QUIRK_BLK_NO_CMD23),
75 	MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
76 		  MMC_QUIRK_BLK_NO_CMD23),
77 
78 	/*
79 	 * Some SD cards lockup while using CMD23 multiblock transfers.
80 	 */
81 	MMC_FIXUP("AF SD", CID_MANFID_ATP, CID_OEMID_ANY, add_quirk_sd,
82 		  MMC_QUIRK_BLK_NO_CMD23),
83 	MMC_FIXUP("APUSD", CID_MANFID_APACER, 0x5048, add_quirk_sd,
84 		  MMC_QUIRK_BLK_NO_CMD23),
85 
86 	/*
87 	 * Some MMC cards need longer data read timeout than indicated in CSD.
88 	 */
89 	MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
90 		  MMC_QUIRK_LONG_READ_TIME),
91 	MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
92 		  MMC_QUIRK_LONG_READ_TIME),
93 
94 	/*
95 	 * On these Samsung MoviNAND parts, performing secure erase or
96 	 * secure trim can result in unrecoverable corruption due to a
97 	 * firmware bug.
98 	 */
99 	MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
100 		  MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
101 	MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
102 		  MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
103 	MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
104 		  MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
105 	MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
106 		  MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
107 	MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
108 		  MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
109 	MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
110 		  MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
111 	MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
112 		  MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
113 	MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
114 		  MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
115 
116 	/*
117 	 *  On Some Kingston eMMCs, performing trim can result in
118 	 *  unrecoverable data conrruption occasionally due to a firmware bug.
119 	 */
120 	MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
121 		  MMC_QUIRK_TRIM_BROKEN),
122 	MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
123 		  MMC_QUIRK_TRIM_BROKEN),
124 
125 	/*
126 	 * Micron MTFC4GACAJCN-1M supports TRIM but does not appear to support
127 	 * WRITE_ZEROES offloading. It also supports caching, but the cache can
128 	 * only be flushed after a write has occurred.
129 	 */
130 	MMC_FIXUP("Q2J54A", CID_MANFID_MICRON, 0x014e, add_quirk_mmc,
131 		  MMC_QUIRK_TRIM_BROKEN | MMC_QUIRK_BROKEN_CACHE_FLUSH),
132 
133 	/*
134 	 * Kingston EMMC04G-M627 advertises TRIM but it does not seems to
135 	 * support being used to offload WRITE_ZEROES.
136 	 */
137 	MMC_FIXUP("M62704", CID_MANFID_KINGSTON, 0x0100, add_quirk_mmc,
138 		  MMC_QUIRK_TRIM_BROKEN),
139 
140 	/*
141 	 * Some SD cards reports discard support while they don't
142 	 */
143 	MMC_FIXUP(CID_NAME_ANY, CID_MANFID_SANDISK_SD, 0x5344, add_quirk_sd,
144 		  MMC_QUIRK_BROKEN_SD_DISCARD),
145 
146 	END_FIXUP
147 };
148 
149 static const struct mmc_fixup __maybe_unused mmc_ext_csd_fixups[] = {
150 	/*
151 	 * Certain Hynix eMMC 4.41 cards might get broken when HPI feature
152 	 * is used so disable the HPI feature for such buggy cards.
153 	 */
154 	MMC_FIXUP_EXT_CSD_REV(CID_NAME_ANY, CID_MANFID_HYNIX,
155 			      0x014a, add_quirk, MMC_QUIRK_BROKEN_HPI, 5),
156 	/*
157 	 * Certain Micron (Numonyx) eMMC 4.5 cards might get broken when HPI
158 	 * feature is used so disable the HPI feature for such buggy cards.
159 	 */
160 	MMC_FIXUP_EXT_CSD_REV(CID_NAME_ANY, CID_MANFID_NUMONYX,
161 			      0x014e, add_quirk, MMC_QUIRK_BROKEN_HPI, 6),
162 
163 	END_FIXUP
164 };
165 
166 
167 static const struct mmc_fixup __maybe_unused sdio_fixup_methods[] = {
168 	SDIO_FIXUP(SDIO_VENDOR_ID_TI_WL1251, SDIO_DEVICE_ID_TI_WL1251,
169 		   add_quirk, MMC_QUIRK_NONSTD_FUNC_IF),
170 
171 	SDIO_FIXUP(SDIO_VENDOR_ID_TI_WL1251, SDIO_DEVICE_ID_TI_WL1251,
172 		   add_quirk, MMC_QUIRK_DISABLE_CD),
173 
174 	SDIO_FIXUP(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1271,
175 		   add_quirk, MMC_QUIRK_NONSTD_FUNC_IF),
176 
177 	SDIO_FIXUP(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1271,
178 		   add_quirk, MMC_QUIRK_DISABLE_CD),
179 
180 	SDIO_FIXUP(SDIO_VENDOR_ID_STE, SDIO_DEVICE_ID_STE_CW1200,
181 		   add_quirk, MMC_QUIRK_BROKEN_BYTE_MODE_512),
182 
183 	SDIO_FIXUP(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797_F0,
184 		   add_quirk, MMC_QUIRK_BROKEN_IRQ_POLLING),
185 
186 	SDIO_FIXUP(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887_F0,
187 		   add_limit_rate_quirk, 150000000),
188 
189 	END_FIXUP
190 };
191 
192 static const struct mmc_fixup __maybe_unused sdio_card_init_methods[] = {
193 	SDIO_FIXUP_COMPATIBLE("ti,wl1251", wl1251_quirk, 0),
194 
195 	SDIO_FIXUP_COMPATIBLE("silabs,wf200", add_quirk,
196 			      MMC_QUIRK_BROKEN_BYTE_MODE_512 |
197 			      MMC_QUIRK_LENIENT_FN0 |
198 			      MMC_QUIRK_BLKSZ_FOR_BYTE_MODE),
199 
200 	END_FIXUP
201 };
202 
203 static inline bool mmc_fixup_of_compatible_match(struct mmc_card *card,
204 						 const char *compatible)
205 {
206 	struct device_node *np;
207 
208 	for_each_child_of_node(mmc_dev(card->host)->of_node, np) {
209 		if (of_device_is_compatible(np, compatible)) {
210 			of_node_put(np);
211 			return true;
212 		}
213 	}
214 
215 	return false;
216 }
217 
218 static inline void mmc_fixup_device(struct mmc_card *card,
219 				    const struct mmc_fixup *table)
220 {
221 	const struct mmc_fixup *f;
222 	u64 rev = cid_rev_card(card);
223 
224 	for (f = table; f->vendor_fixup; f++) {
225 		if (f->manfid != CID_MANFID_ANY &&
226 		    f->manfid != card->cid.manfid)
227 			continue;
228 		if (f->oemid != CID_OEMID_ANY &&
229 		    f->oemid != card->cid.oemid)
230 			continue;
231 		if (f->name != CID_NAME_ANY &&
232 		    strncmp(f->name, card->cid.prod_name,
233 			    sizeof(card->cid.prod_name)))
234 			continue;
235 		if (f->cis_vendor != (u16)SDIO_ANY_ID &&
236 		    f->cis_vendor != card->cis.vendor)
237 			continue;
238 		if (f->cis_device != (u16)SDIO_ANY_ID &&
239 		    f->cis_device != card->cis.device)
240 			continue;
241 		if (f->ext_csd_rev != EXT_CSD_REV_ANY &&
242 		    f->ext_csd_rev != card->ext_csd.rev)
243 			continue;
244 		if (rev < f->rev_start || rev > f->rev_end)
245 			continue;
246 		if (f->of_compatible &&
247 		    !mmc_fixup_of_compatible_match(card, f->of_compatible))
248 			continue;
249 		if (f->year != CID_YEAR_ANY && f->year != card->cid.year)
250 			continue;
251 		if (f->month != CID_MONTH_ANY && f->month != card->cid.month)
252 			continue;
253 
254 		dev_dbg(&card->dev, "calling %ps\n", f->vendor_fixup);
255 		f->vendor_fixup(card, f->data);
256 	}
257 }
258