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