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 	 * Some SD cards lockup while using CMD23 multiblock transfers.
58 	 */
59 	MMC_FIXUP("AF SD", CID_MANFID_ATP, CID_OEMID_ANY, add_quirk_sd,
60 		  MMC_QUIRK_BLK_NO_CMD23),
61 	MMC_FIXUP("APUSD", CID_MANFID_APACER, 0x5048, add_quirk_sd,
62 		  MMC_QUIRK_BLK_NO_CMD23),
63 
64 	/*
65 	 * Some MMC cards need longer data read timeout than indicated in CSD.
66 	 */
67 	MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
68 		  MMC_QUIRK_LONG_READ_TIME),
69 	MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
70 		  MMC_QUIRK_LONG_READ_TIME),
71 
72 	/*
73 	 * On these Samsung MoviNAND parts, performing secure erase or
74 	 * secure trim can result in unrecoverable corruption due to a
75 	 * firmware bug.
76 	 */
77 	MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
78 		  MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
79 	MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
80 		  MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
81 	MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
82 		  MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
83 	MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
84 		  MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
85 	MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
86 		  MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
87 	MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
88 		  MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
89 	MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
90 		  MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
91 	MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
92 		  MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
93 
94 	/*
95 	 *  On Some Kingston eMMCs, performing trim can result in
96 	 *  unrecoverable data conrruption occasionally due to a firmware bug.
97 	 */
98 	MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
99 		  MMC_QUIRK_TRIM_BROKEN),
100 	MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
101 		  MMC_QUIRK_TRIM_BROKEN),
102 
103 	END_FIXUP
104 };
105 
106 static const struct mmc_fixup __maybe_unused mmc_ext_csd_fixups[] = {
107 	/*
108 	 * Certain Hynix eMMC 4.41 cards might get broken when HPI feature
109 	 * is used so disable the HPI feature for such buggy cards.
110 	 */
111 	MMC_FIXUP_EXT_CSD_REV(CID_NAME_ANY, CID_MANFID_HYNIX,
112 			      0x014a, add_quirk, MMC_QUIRK_BROKEN_HPI, 5),
113 	/*
114 	 * Certain Micron (Numonyx) eMMC 4.5 cards might get broken when HPI
115 	 * feature is used so disable the HPI feature for such buggy cards.
116 	 */
117 	MMC_FIXUP_EXT_CSD_REV(CID_NAME_ANY, CID_MANFID_NUMONYX,
118 			      0x014e, add_quirk, MMC_QUIRK_BROKEN_HPI, 6),
119 
120 	END_FIXUP
121 };
122 
123 
124 static const struct mmc_fixup __maybe_unused sdio_fixup_methods[] = {
125 	SDIO_FIXUP(SDIO_VENDOR_ID_TI_WL1251, SDIO_DEVICE_ID_TI_WL1251,
126 		   add_quirk, MMC_QUIRK_NONSTD_FUNC_IF),
127 
128 	SDIO_FIXUP(SDIO_VENDOR_ID_TI_WL1251, SDIO_DEVICE_ID_TI_WL1251,
129 		   add_quirk, MMC_QUIRK_DISABLE_CD),
130 
131 	SDIO_FIXUP(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1271,
132 		   add_quirk, MMC_QUIRK_NONSTD_FUNC_IF),
133 
134 	SDIO_FIXUP(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1271,
135 		   add_quirk, MMC_QUIRK_DISABLE_CD),
136 
137 	SDIO_FIXUP(SDIO_VENDOR_ID_STE, SDIO_DEVICE_ID_STE_CW1200,
138 		   add_quirk, MMC_QUIRK_BROKEN_BYTE_MODE_512),
139 
140 	SDIO_FIXUP(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797_F0,
141 		   add_quirk, MMC_QUIRK_BROKEN_IRQ_POLLING),
142 
143 	SDIO_FIXUP(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887_F0,
144 		   add_limit_rate_quirk, 150000000),
145 
146 	END_FIXUP
147 };
148 
149 static const struct mmc_fixup __maybe_unused sdio_card_init_methods[] = {
150 	SDIO_FIXUP_COMPATIBLE("ti,wl1251", wl1251_quirk, 0),
151 
152 	SDIO_FIXUP_COMPATIBLE("silabs,wf200", add_quirk,
153 			      MMC_QUIRK_BROKEN_BYTE_MODE_512 |
154 			      MMC_QUIRK_LENIENT_FN0 |
155 			      MMC_QUIRK_BLKSZ_FOR_BYTE_MODE),
156 
157 	END_FIXUP
158 };
159 
mmc_fixup_of_compatible_match(struct mmc_card * card,const char * compatible)160 static inline bool mmc_fixup_of_compatible_match(struct mmc_card *card,
161 						 const char *compatible)
162 {
163 	struct device_node *np;
164 
165 	for_each_child_of_node(mmc_dev(card->host)->of_node, np) {
166 		if (of_device_is_compatible(np, compatible)) {
167 			of_node_put(np);
168 			return true;
169 		}
170 	}
171 
172 	return false;
173 }
174 
mmc_fixup_device(struct mmc_card * card,const struct mmc_fixup * table)175 static inline void mmc_fixup_device(struct mmc_card *card,
176 				    const struct mmc_fixup *table)
177 {
178 	const struct mmc_fixup *f;
179 	u64 rev = cid_rev_card(card);
180 
181 	for (f = table; f->vendor_fixup; f++) {
182 		if (f->manfid != CID_MANFID_ANY &&
183 		    f->manfid != card->cid.manfid)
184 			continue;
185 		if (f->oemid != CID_OEMID_ANY &&
186 		    f->oemid != card->cid.oemid)
187 			continue;
188 		if (f->name != CID_NAME_ANY &&
189 		    strncmp(f->name, card->cid.prod_name,
190 			    sizeof(card->cid.prod_name)))
191 			continue;
192 		if (f->cis_vendor != (u16)SDIO_ANY_ID &&
193 		    f->cis_vendor != card->cis.vendor)
194 			continue;
195 		if (f->cis_device != (u16)SDIO_ANY_ID &&
196 		    f->cis_device != card->cis.device)
197 			continue;
198 		if (f->ext_csd_rev != EXT_CSD_REV_ANY &&
199 		    f->ext_csd_rev != card->ext_csd.rev)
200 			continue;
201 		if (rev < f->rev_start || rev > f->rev_end)
202 			continue;
203 		if (f->of_compatible &&
204 		    !mmc_fixup_of_compatible_match(card, f->of_compatible))
205 			continue;
206 
207 		dev_dbg(&card->dev, "calling %ps\n", f->vendor_fixup);
208 		f->vendor_fixup(card, f->data);
209 	}
210 }
211