xref: /linux/drivers/mmc/core/card.h (revision 818cd40529d9f19aa7ca3794f73477b5ac3ef350)
1af873fceSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
24facdde1SUlf Hansson /*
34facdde1SUlf Hansson  * Private header for the mmc subsystem
44facdde1SUlf Hansson  *
54facdde1SUlf Hansson  * Copyright (C) 2016 Linaro Ltd
64facdde1SUlf Hansson  *
74facdde1SUlf Hansson  * Author: Ulf Hansson <ulf.hansson@linaro.org>
84facdde1SUlf Hansson  */
94facdde1SUlf Hansson 
104facdde1SUlf Hansson #ifndef _MMC_CORE_CARD_H
114facdde1SUlf Hansson #define _MMC_CORE_CARD_H
124facdde1SUlf Hansson 
134facdde1SUlf Hansson #include <linux/mmc/card.h>
144facdde1SUlf Hansson 
154facdde1SUlf Hansson #define mmc_card_name(c)	((c)->cid.prod_name)
164facdde1SUlf Hansson #define mmc_card_id(c)		(dev_name(&(c)->dev))
174facdde1SUlf Hansson #define mmc_dev_to_card(d)	container_of(d, struct mmc_card, dev)
184facdde1SUlf Hansson 
194facdde1SUlf Hansson /* Card states */
204facdde1SUlf Hansson #define MMC_STATE_PRESENT	(1<<0)		/* present in sysfs */
214facdde1SUlf Hansson #define MMC_STATE_READONLY	(1<<1)		/* card is read-only */
224facdde1SUlf Hansson #define MMC_STATE_BLOCKADDR	(1<<2)		/* card uses block-addressing */
234facdde1SUlf Hansson #define MMC_CARD_SDXC		(1<<3)		/* card is SDXC */
244facdde1SUlf Hansson #define MMC_CARD_REMOVED	(1<<4)		/* card has been removed */
250c204979SUlf Hansson #define MMC_STATE_SUSPENDED	(1<<5)		/* card is suspended */
264facdde1SUlf Hansson 
274facdde1SUlf Hansson #define mmc_card_present(c)	((c)->state & MMC_STATE_PRESENT)
284facdde1SUlf Hansson #define mmc_card_readonly(c)	((c)->state & MMC_STATE_READONLY)
294facdde1SUlf Hansson #define mmc_card_blockaddr(c)	((c)->state & MMC_STATE_BLOCKADDR)
304facdde1SUlf Hansson #define mmc_card_ext_capacity(c) ((c)->state & MMC_CARD_SDXC)
314facdde1SUlf Hansson #define mmc_card_removed(c)	((c) && ((c)->state & MMC_CARD_REMOVED))
324facdde1SUlf Hansson #define mmc_card_suspended(c)	((c)->state & MMC_STATE_SUSPENDED)
334facdde1SUlf Hansson 
344facdde1SUlf Hansson #define mmc_card_set_present(c)	((c)->state |= MMC_STATE_PRESENT)
354facdde1SUlf Hansson #define mmc_card_set_readonly(c) ((c)->state |= MMC_STATE_READONLY)
364facdde1SUlf Hansson #define mmc_card_set_blockaddr(c) ((c)->state |= MMC_STATE_BLOCKADDR)
374facdde1SUlf Hansson #define mmc_card_set_ext_capacity(c) ((c)->state |= MMC_CARD_SDXC)
384facdde1SUlf Hansson #define mmc_card_set_removed(c) ((c)->state |= MMC_CARD_REMOVED)
394facdde1SUlf Hansson #define mmc_card_set_suspended(c) ((c)->state |= MMC_STATE_SUSPENDED)
404facdde1SUlf Hansson #define mmc_card_clr_suspended(c) ((c)->state &= ~MMC_STATE_SUSPENDED)
414facdde1SUlf Hansson 
424facdde1SUlf Hansson /*
434facdde1SUlf Hansson  * The world is not perfect and supplies us with broken mmc/sdio devices.
444facdde1SUlf Hansson  * For at least some of these bugs we need a work-around.
454facdde1SUlf Hansson  */
464facdde1SUlf Hansson struct mmc_fixup {
474facdde1SUlf Hansson 	/* CID-specific fields. */
484facdde1SUlf Hansson 	const char *name;
494facdde1SUlf Hansson 
504facdde1SUlf Hansson 	/* Valid revision range */
514facdde1SUlf Hansson 	u64 rev_start, rev_end;
524facdde1SUlf Hansson 
534facdde1SUlf Hansson 	unsigned int manfid;
544facdde1SUlf Hansson 	unsigned short oemid;
554facdde1SUlf Hansson 
564facdde1SUlf Hansson 	/* SDIO-specific fields. You can use SDIO_ANY_ID here of course */
574facdde1SUlf Hansson 	u16 cis_vendor, cis_device;
584facdde1SUlf Hansson 
594facdde1SUlf Hansson 	/* for MMC cards */
604facdde1SUlf Hansson 	unsigned int ext_csd_rev;
614facdde1SUlf Hansson 
62b360b110SJérôme Pouiller 	/* Match against functions declared in device tree */
63b360b110SJérôme Pouiller 	const char *of_compatible;
64b360b110SJérôme Pouiller 
654facdde1SUlf Hansson 	void (*vendor_fixup)(struct mmc_card *card, int data);
664facdde1SUlf Hansson 	int data;
674facdde1SUlf Hansson };
684facdde1SUlf Hansson 
694facdde1SUlf Hansson #define CID_MANFID_ANY (-1u)
704facdde1SUlf Hansson #define CID_OEMID_ANY ((unsigned short) -1)
714facdde1SUlf Hansson #define CID_NAME_ANY (NULL)
724facdde1SUlf Hansson 
734facdde1SUlf Hansson #define EXT_CSD_REV_ANY (-1u)
744facdde1SUlf Hansson 
754facdde1SUlf Hansson #define CID_MANFID_SANDISK      0x2
7691516a2aSChristoph Fritz #define CID_MANFID_ATP          0x9
774facdde1SUlf Hansson #define CID_MANFID_TOSHIBA      0x11
784facdde1SUlf Hansson #define CID_MANFID_MICRON       0x13
794facdde1SUlf Hansson #define CID_MANFID_SAMSUNG      0x15
8091516a2aSChristoph Fritz #define CID_MANFID_APACER       0x27
814facdde1SUlf Hansson #define CID_MANFID_KINGSTON     0x70
824facdde1SUlf Hansson #define CID_MANFID_HYNIX	0x90
83dbe7dc6bSDirk Behme #define CID_MANFID_NUMONYX	0xFE
844facdde1SUlf Hansson 
854facdde1SUlf Hansson #define END_FIXUP { NULL }
864facdde1SUlf Hansson 
874facdde1SUlf Hansson #define _FIXUP_EXT(_name, _manfid, _oemid, _rev_start, _rev_end,	\
884facdde1SUlf Hansson 		   _cis_vendor, _cis_device,				\
894facdde1SUlf Hansson 		   _fixup, _data, _ext_csd_rev)				\
904facdde1SUlf Hansson 	{						\
914facdde1SUlf Hansson 		.name = (_name),			\
924facdde1SUlf Hansson 		.manfid = (_manfid),			\
934facdde1SUlf Hansson 		.oemid = (_oemid),			\
944facdde1SUlf Hansson 		.rev_start = (_rev_start),		\
954facdde1SUlf Hansson 		.rev_end = (_rev_end),			\
964facdde1SUlf Hansson 		.cis_vendor = (_cis_vendor),		\
974facdde1SUlf Hansson 		.cis_device = (_cis_device),		\
984facdde1SUlf Hansson 		.vendor_fixup = (_fixup),		\
994facdde1SUlf Hansson 		.data = (_data),			\
1004facdde1SUlf Hansson 		.ext_csd_rev = (_ext_csd_rev),		\
1014facdde1SUlf Hansson 	}
1024facdde1SUlf Hansson 
1034facdde1SUlf Hansson #define MMC_FIXUP_REV(_name, _manfid, _oemid, _rev_start, _rev_end,	\
1044facdde1SUlf Hansson 		      _fixup, _data, _ext_csd_rev)			\
1054facdde1SUlf Hansson 	_FIXUP_EXT(_name, _manfid,					\
1064facdde1SUlf Hansson 		   _oemid, _rev_start, _rev_end,			\
1074facdde1SUlf Hansson 		   SDIO_ANY_ID, SDIO_ANY_ID,				\
1084facdde1SUlf Hansson 		   _fixup, _data, _ext_csd_rev)				\
1094facdde1SUlf Hansson 
1104facdde1SUlf Hansson #define MMC_FIXUP(_name, _manfid, _oemid, _fixup, _data) \
1114facdde1SUlf Hansson 	MMC_FIXUP_REV(_name, _manfid, _oemid, 0, -1ull, _fixup, _data,	\
1124facdde1SUlf Hansson 		      EXT_CSD_REV_ANY)
1134facdde1SUlf Hansson 
1144facdde1SUlf Hansson #define MMC_FIXUP_EXT_CSD_REV(_name, _manfid, _oemid, _fixup, _data,	\
1154facdde1SUlf Hansson 			      _ext_csd_rev)				\
1164facdde1SUlf Hansson 	MMC_FIXUP_REV(_name, _manfid, _oemid, 0, -1ull, _fixup, _data,	\
1174facdde1SUlf Hansson 		      _ext_csd_rev)
1184facdde1SUlf Hansson 
1194facdde1SUlf Hansson #define SDIO_FIXUP(_vendor, _device, _fixup, _data)			\
1204facdde1SUlf Hansson 	_FIXUP_EXT(CID_NAME_ANY, CID_MANFID_ANY,			\
1214facdde1SUlf Hansson 		    CID_OEMID_ANY, 0, -1ull,				\
1224facdde1SUlf Hansson 		   _vendor, _device,					\
1234facdde1SUlf Hansson 		   _fixup, _data, EXT_CSD_REV_ANY)			\
1244facdde1SUlf Hansson 
125*818cd405SH. Nikolaus Schaller #define SDIO_FIXUP_COMPATIBLE(_compatible, _fixup, _data)		\
126*818cd405SH. Nikolaus Schaller 	{						\
127*818cd405SH. Nikolaus Schaller 		.name = CID_NAME_ANY,			\
128*818cd405SH. Nikolaus Schaller 		.manfid = CID_MANFID_ANY,		\
129*818cd405SH. Nikolaus Schaller 		.oemid = CID_OEMID_ANY,			\
130*818cd405SH. Nikolaus Schaller 		.rev_start = 0,				\
131*818cd405SH. Nikolaus Schaller 		.rev_end = -1ull,			\
132*818cd405SH. Nikolaus Schaller 		.cis_vendor = SDIO_ANY_ID,		\
133*818cd405SH. Nikolaus Schaller 		.cis_device = SDIO_ANY_ID,		\
134*818cd405SH. Nikolaus Schaller 		.vendor_fixup = (_fixup),		\
135*818cd405SH. Nikolaus Schaller 		.data = (_data),			\
136*818cd405SH. Nikolaus Schaller 		.ext_csd_rev = EXT_CSD_REV_ANY,		\
137*818cd405SH. Nikolaus Schaller 		.of_compatible = _compatible,	\
138*818cd405SH. Nikolaus Schaller 	}
139*818cd405SH. Nikolaus Schaller 
1404facdde1SUlf Hansson #define cid_rev(hwrev, fwrev, year, month)	\
1414facdde1SUlf Hansson 	(((u64) hwrev) << 40 |			\
1424facdde1SUlf Hansson 	 ((u64) fwrev) << 32 |			\
1434facdde1SUlf Hansson 	 ((u64) year) << 16 |			\
1444facdde1SUlf Hansson 	 ((u64) month))
1454facdde1SUlf Hansson 
1464facdde1SUlf Hansson #define cid_rev_card(card)			\
1474facdde1SUlf Hansson 	cid_rev(card->cid.hwrev,		\
1484facdde1SUlf Hansson 		    card->cid.fwrev,		\
1494facdde1SUlf Hansson 		    card->cid.year,		\
1504facdde1SUlf Hansson 		    card->cid.month)
1514facdde1SUlf Hansson 
1524facdde1SUlf Hansson /*
1534facdde1SUlf Hansson  * Unconditionally quirk add/remove.
1544facdde1SUlf Hansson  */
1554facdde1SUlf Hansson static inline void __maybe_unused add_quirk(struct mmc_card *card, int data)
1564facdde1SUlf Hansson {
1574facdde1SUlf Hansson 	card->quirks |= data;
1584facdde1SUlf Hansson }
1594facdde1SUlf Hansson 
1604facdde1SUlf Hansson static inline void __maybe_unused remove_quirk(struct mmc_card *card, int data)
1614facdde1SUlf Hansson {
1624facdde1SUlf Hansson 	card->quirks &= ~data;
1634facdde1SUlf Hansson }
1644facdde1SUlf Hansson 
165ebc5a1bfSharish_kandiga@mentor.com static inline void __maybe_unused add_limit_rate_quirk(struct mmc_card *card,
166ebc5a1bfSharish_kandiga@mentor.com 						       int data)
167ebc5a1bfSharish_kandiga@mentor.com {
168ebc5a1bfSharish_kandiga@mentor.com 	card->quirk_max_rate = data;
169ebc5a1bfSharish_kandiga@mentor.com }
170ebc5a1bfSharish_kandiga@mentor.com 
1714facdde1SUlf Hansson /*
1724facdde1SUlf Hansson  * Quirk add/remove for MMC products.
1734facdde1SUlf Hansson  */
1744facdde1SUlf Hansson static inline void __maybe_unused add_quirk_mmc(struct mmc_card *card, int data)
1754facdde1SUlf Hansson {
1764facdde1SUlf Hansson 	if (mmc_card_mmc(card))
1774facdde1SUlf Hansson 		card->quirks |= data;
1784facdde1SUlf Hansson }
1794facdde1SUlf Hansson 
1804facdde1SUlf Hansson static inline void __maybe_unused remove_quirk_mmc(struct mmc_card *card,
1814facdde1SUlf Hansson 						   int data)
1824facdde1SUlf Hansson {
1834facdde1SUlf Hansson 	if (mmc_card_mmc(card))
1844facdde1SUlf Hansson 		card->quirks &= ~data;
1854facdde1SUlf Hansson }
1864facdde1SUlf Hansson 
1874facdde1SUlf Hansson /*
1884facdde1SUlf Hansson  * Quirk add/remove for SD products.
1894facdde1SUlf Hansson  */
1904facdde1SUlf Hansson static inline void __maybe_unused add_quirk_sd(struct mmc_card *card, int data)
1914facdde1SUlf Hansson {
1924facdde1SUlf Hansson 	if (mmc_card_sd(card))
1934facdde1SUlf Hansson 		card->quirks |= data;
1944facdde1SUlf Hansson }
1954facdde1SUlf Hansson 
1964facdde1SUlf Hansson static inline void __maybe_unused remove_quirk_sd(struct mmc_card *card,
1974facdde1SUlf Hansson 						   int data)
1984facdde1SUlf Hansson {
1994facdde1SUlf Hansson 	if (mmc_card_sd(card))
2004facdde1SUlf Hansson 		card->quirks &= ~data;
2014facdde1SUlf Hansson }
2024facdde1SUlf Hansson 
2034facdde1SUlf Hansson static inline int mmc_card_lenient_fn0(const struct mmc_card *c)
2044facdde1SUlf Hansson {
2054facdde1SUlf Hansson 	return c->quirks & MMC_QUIRK_LENIENT_FN0;
2064facdde1SUlf Hansson }
2074facdde1SUlf Hansson 
2084facdde1SUlf Hansson static inline int mmc_blksz_for_byte_mode(const struct mmc_card *c)
2094facdde1SUlf Hansson {
2104facdde1SUlf Hansson 	return c->quirks & MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
2114facdde1SUlf Hansson }
2124facdde1SUlf Hansson 
2134facdde1SUlf Hansson static inline int mmc_card_disable_cd(const struct mmc_card *c)
2144facdde1SUlf Hansson {
2154facdde1SUlf Hansson 	return c->quirks & MMC_QUIRK_DISABLE_CD;
2164facdde1SUlf Hansson }
2174facdde1SUlf Hansson 
2184facdde1SUlf Hansson static inline int mmc_card_nonstd_func_interface(const struct mmc_card *c)
2194facdde1SUlf Hansson {
2204facdde1SUlf Hansson 	return c->quirks & MMC_QUIRK_NONSTD_FUNC_IF;
2214facdde1SUlf Hansson }
2224facdde1SUlf Hansson 
2234facdde1SUlf Hansson static inline int mmc_card_broken_byte_mode_512(const struct mmc_card *c)
2244facdde1SUlf Hansson {
2254facdde1SUlf Hansson 	return c->quirks & MMC_QUIRK_BROKEN_BYTE_MODE_512;
2264facdde1SUlf Hansson }
2274facdde1SUlf Hansson 
2284facdde1SUlf Hansson static inline int mmc_card_long_read_time(const struct mmc_card *c)
2294facdde1SUlf Hansson {
2304facdde1SUlf Hansson 	return c->quirks & MMC_QUIRK_LONG_READ_TIME;
2314facdde1SUlf Hansson }
2324facdde1SUlf Hansson 
2334facdde1SUlf Hansson static inline int mmc_card_broken_irq_polling(const struct mmc_card *c)
2344facdde1SUlf Hansson {
2354facdde1SUlf Hansson 	return c->quirks & MMC_QUIRK_BROKEN_IRQ_POLLING;
2364facdde1SUlf Hansson }
2374facdde1SUlf Hansson 
2384facdde1SUlf Hansson static inline int mmc_card_broken_hpi(const struct mmc_card *c)
2394facdde1SUlf Hansson {
2404facdde1SUlf Hansson 	return c->quirks & MMC_QUIRK_BROKEN_HPI;
2414facdde1SUlf Hansson }
2424facdde1SUlf Hansson 
2434facdde1SUlf Hansson #endif
244