xref: /linux/drivers/mmc/core/card.h (revision 91516a2a4734614d62ee3ed921f8f88acc67c000)
14facdde1SUlf Hansson /*
24facdde1SUlf Hansson  * Private header for the mmc subsystem
34facdde1SUlf Hansson  *
44facdde1SUlf Hansson  * Copyright (C) 2016 Linaro Ltd
54facdde1SUlf Hansson  *
64facdde1SUlf Hansson  * Author: Ulf Hansson <ulf.hansson@linaro.org>
74facdde1SUlf Hansson  *
84facdde1SUlf Hansson  * License terms: GNU General Public License (GPL) version 2
94facdde1SUlf Hansson  */
104facdde1SUlf Hansson 
114facdde1SUlf Hansson #ifndef _MMC_CORE_CARD_H
124facdde1SUlf Hansson #define _MMC_CORE_CARD_H
134facdde1SUlf Hansson 
144facdde1SUlf Hansson #include <linux/mmc/card.h>
154facdde1SUlf Hansson 
164facdde1SUlf Hansson #define mmc_card_name(c)	((c)->cid.prod_name)
174facdde1SUlf Hansson #define mmc_card_id(c)		(dev_name(&(c)->dev))
184facdde1SUlf Hansson #define mmc_dev_to_card(d)	container_of(d, struct mmc_card, dev)
194facdde1SUlf Hansson 
204facdde1SUlf Hansson /* Card states */
214facdde1SUlf Hansson #define MMC_STATE_PRESENT	(1<<0)		/* present in sysfs */
224facdde1SUlf Hansson #define MMC_STATE_READONLY	(1<<1)		/* card is read-only */
234facdde1SUlf Hansson #define MMC_STATE_BLOCKADDR	(1<<2)		/* card uses block-addressing */
244facdde1SUlf Hansson #define MMC_CARD_SDXC		(1<<3)		/* card is SDXC */
254facdde1SUlf Hansson #define MMC_CARD_REMOVED	(1<<4)		/* card has been removed */
264facdde1SUlf Hansson #define MMC_STATE_DOING_BKOPS	(1<<5)		/* card is doing BKOPS */
274facdde1SUlf Hansson #define MMC_STATE_SUSPENDED	(1<<6)		/* card is suspended */
284facdde1SUlf Hansson 
294facdde1SUlf Hansson #define mmc_card_present(c)	((c)->state & MMC_STATE_PRESENT)
304facdde1SUlf Hansson #define mmc_card_readonly(c)	((c)->state & MMC_STATE_READONLY)
314facdde1SUlf Hansson #define mmc_card_blockaddr(c)	((c)->state & MMC_STATE_BLOCKADDR)
324facdde1SUlf Hansson #define mmc_card_ext_capacity(c) ((c)->state & MMC_CARD_SDXC)
334facdde1SUlf Hansson #define mmc_card_removed(c)	((c) && ((c)->state & MMC_CARD_REMOVED))
344facdde1SUlf Hansson #define mmc_card_doing_bkops(c)	((c)->state & MMC_STATE_DOING_BKOPS)
354facdde1SUlf Hansson #define mmc_card_suspended(c)	((c)->state & MMC_STATE_SUSPENDED)
364facdde1SUlf Hansson 
374facdde1SUlf Hansson #define mmc_card_set_present(c)	((c)->state |= MMC_STATE_PRESENT)
384facdde1SUlf Hansson #define mmc_card_set_readonly(c) ((c)->state |= MMC_STATE_READONLY)
394facdde1SUlf Hansson #define mmc_card_set_blockaddr(c) ((c)->state |= MMC_STATE_BLOCKADDR)
404facdde1SUlf Hansson #define mmc_card_set_ext_capacity(c) ((c)->state |= MMC_CARD_SDXC)
414facdde1SUlf Hansson #define mmc_card_set_removed(c) ((c)->state |= MMC_CARD_REMOVED)
424facdde1SUlf Hansson #define mmc_card_set_doing_bkops(c)	((c)->state |= MMC_STATE_DOING_BKOPS)
434facdde1SUlf Hansson #define mmc_card_clr_doing_bkops(c)	((c)->state &= ~MMC_STATE_DOING_BKOPS)
444facdde1SUlf Hansson #define mmc_card_set_suspended(c) ((c)->state |= MMC_STATE_SUSPENDED)
454facdde1SUlf Hansson #define mmc_card_clr_suspended(c) ((c)->state &= ~MMC_STATE_SUSPENDED)
464facdde1SUlf Hansson 
474facdde1SUlf Hansson /*
484facdde1SUlf Hansson  * The world is not perfect and supplies us with broken mmc/sdio devices.
494facdde1SUlf Hansson  * For at least some of these bugs we need a work-around.
504facdde1SUlf Hansson  */
514facdde1SUlf Hansson struct mmc_fixup {
524facdde1SUlf Hansson 	/* CID-specific fields. */
534facdde1SUlf Hansson 	const char *name;
544facdde1SUlf Hansson 
554facdde1SUlf Hansson 	/* Valid revision range */
564facdde1SUlf Hansson 	u64 rev_start, rev_end;
574facdde1SUlf Hansson 
584facdde1SUlf Hansson 	unsigned int manfid;
594facdde1SUlf Hansson 	unsigned short oemid;
604facdde1SUlf Hansson 
614facdde1SUlf Hansson 	/* SDIO-specific fields. You can use SDIO_ANY_ID here of course */
624facdde1SUlf Hansson 	u16 cis_vendor, cis_device;
634facdde1SUlf Hansson 
644facdde1SUlf Hansson 	/* for MMC cards */
654facdde1SUlf Hansson 	unsigned int ext_csd_rev;
664facdde1SUlf Hansson 
674facdde1SUlf Hansson 	void (*vendor_fixup)(struct mmc_card *card, int data);
684facdde1SUlf Hansson 	int data;
694facdde1SUlf Hansson };
704facdde1SUlf Hansson 
714facdde1SUlf Hansson #define CID_MANFID_ANY (-1u)
724facdde1SUlf Hansson #define CID_OEMID_ANY ((unsigned short) -1)
734facdde1SUlf Hansson #define CID_NAME_ANY (NULL)
744facdde1SUlf Hansson 
754facdde1SUlf Hansson #define EXT_CSD_REV_ANY (-1u)
764facdde1SUlf Hansson 
774facdde1SUlf Hansson #define CID_MANFID_SANDISK      0x2
78*91516a2aSChristoph Fritz #define CID_MANFID_ATP          0x9
794facdde1SUlf Hansson #define CID_MANFID_TOSHIBA      0x11
804facdde1SUlf Hansson #define CID_MANFID_MICRON       0x13
814facdde1SUlf Hansson #define CID_MANFID_SAMSUNG      0x15
82*91516a2aSChristoph Fritz #define CID_MANFID_APACER       0x27
834facdde1SUlf Hansson #define CID_MANFID_KINGSTON     0x70
844facdde1SUlf Hansson #define CID_MANFID_HYNIX	0x90
854facdde1SUlf Hansson 
864facdde1SUlf Hansson #define END_FIXUP { NULL }
874facdde1SUlf Hansson 
884facdde1SUlf Hansson #define _FIXUP_EXT(_name, _manfid, _oemid, _rev_start, _rev_end,	\
894facdde1SUlf Hansson 		   _cis_vendor, _cis_device,				\
904facdde1SUlf Hansson 		   _fixup, _data, _ext_csd_rev)				\
914facdde1SUlf Hansson 	{						\
924facdde1SUlf Hansson 		.name = (_name),			\
934facdde1SUlf Hansson 		.manfid = (_manfid),			\
944facdde1SUlf Hansson 		.oemid = (_oemid),			\
954facdde1SUlf Hansson 		.rev_start = (_rev_start),		\
964facdde1SUlf Hansson 		.rev_end = (_rev_end),			\
974facdde1SUlf Hansson 		.cis_vendor = (_cis_vendor),		\
984facdde1SUlf Hansson 		.cis_device = (_cis_device),		\
994facdde1SUlf Hansson 		.vendor_fixup = (_fixup),		\
1004facdde1SUlf Hansson 		.data = (_data),			\
1014facdde1SUlf Hansson 		.ext_csd_rev = (_ext_csd_rev),		\
1024facdde1SUlf Hansson 	}
1034facdde1SUlf Hansson 
1044facdde1SUlf Hansson #define MMC_FIXUP_REV(_name, _manfid, _oemid, _rev_start, _rev_end,	\
1054facdde1SUlf Hansson 		      _fixup, _data, _ext_csd_rev)			\
1064facdde1SUlf Hansson 	_FIXUP_EXT(_name, _manfid,					\
1074facdde1SUlf Hansson 		   _oemid, _rev_start, _rev_end,			\
1084facdde1SUlf Hansson 		   SDIO_ANY_ID, SDIO_ANY_ID,				\
1094facdde1SUlf Hansson 		   _fixup, _data, _ext_csd_rev)				\
1104facdde1SUlf Hansson 
1114facdde1SUlf Hansson #define MMC_FIXUP(_name, _manfid, _oemid, _fixup, _data) \
1124facdde1SUlf Hansson 	MMC_FIXUP_REV(_name, _manfid, _oemid, 0, -1ull, _fixup, _data,	\
1134facdde1SUlf Hansson 		      EXT_CSD_REV_ANY)
1144facdde1SUlf Hansson 
1154facdde1SUlf Hansson #define MMC_FIXUP_EXT_CSD_REV(_name, _manfid, _oemid, _fixup, _data,	\
1164facdde1SUlf Hansson 			      _ext_csd_rev)				\
1174facdde1SUlf Hansson 	MMC_FIXUP_REV(_name, _manfid, _oemid, 0, -1ull, _fixup, _data,	\
1184facdde1SUlf Hansson 		      _ext_csd_rev)
1194facdde1SUlf Hansson 
1204facdde1SUlf Hansson #define SDIO_FIXUP(_vendor, _device, _fixup, _data)			\
1214facdde1SUlf Hansson 	_FIXUP_EXT(CID_NAME_ANY, CID_MANFID_ANY,			\
1224facdde1SUlf Hansson 		    CID_OEMID_ANY, 0, -1ull,				\
1234facdde1SUlf Hansson 		   _vendor, _device,					\
1244facdde1SUlf Hansson 		   _fixup, _data, EXT_CSD_REV_ANY)			\
1254facdde1SUlf Hansson 
1264facdde1SUlf Hansson #define cid_rev(hwrev, fwrev, year, month)	\
1274facdde1SUlf Hansson 	(((u64) hwrev) << 40 |			\
1284facdde1SUlf Hansson 	 ((u64) fwrev) << 32 |			\
1294facdde1SUlf Hansson 	 ((u64) year) << 16 |			\
1304facdde1SUlf Hansson 	 ((u64) month))
1314facdde1SUlf Hansson 
1324facdde1SUlf Hansson #define cid_rev_card(card)			\
1334facdde1SUlf Hansson 	cid_rev(card->cid.hwrev,		\
1344facdde1SUlf Hansson 		    card->cid.fwrev,		\
1354facdde1SUlf Hansson 		    card->cid.year,		\
1364facdde1SUlf Hansson 		    card->cid.month)
1374facdde1SUlf Hansson 
1384facdde1SUlf Hansson /*
1394facdde1SUlf Hansson  * Unconditionally quirk add/remove.
1404facdde1SUlf Hansson  */
1414facdde1SUlf Hansson static inline void __maybe_unused add_quirk(struct mmc_card *card, int data)
1424facdde1SUlf Hansson {
1434facdde1SUlf Hansson 	card->quirks |= data;
1444facdde1SUlf Hansson }
1454facdde1SUlf Hansson 
1464facdde1SUlf Hansson static inline void __maybe_unused remove_quirk(struct mmc_card *card, int data)
1474facdde1SUlf Hansson {
1484facdde1SUlf Hansson 	card->quirks &= ~data;
1494facdde1SUlf Hansson }
1504facdde1SUlf Hansson 
1514facdde1SUlf Hansson /*
1524facdde1SUlf Hansson  * Quirk add/remove for MMC products.
1534facdde1SUlf Hansson  */
1544facdde1SUlf Hansson static inline void __maybe_unused add_quirk_mmc(struct mmc_card *card, int data)
1554facdde1SUlf Hansson {
1564facdde1SUlf Hansson 	if (mmc_card_mmc(card))
1574facdde1SUlf Hansson 		card->quirks |= data;
1584facdde1SUlf Hansson }
1594facdde1SUlf Hansson 
1604facdde1SUlf Hansson static inline void __maybe_unused remove_quirk_mmc(struct mmc_card *card,
1614facdde1SUlf Hansson 						   int data)
1624facdde1SUlf Hansson {
1634facdde1SUlf Hansson 	if (mmc_card_mmc(card))
1644facdde1SUlf Hansson 		card->quirks &= ~data;
1654facdde1SUlf Hansson }
1664facdde1SUlf Hansson 
1674facdde1SUlf Hansson /*
1684facdde1SUlf Hansson  * Quirk add/remove for SD products.
1694facdde1SUlf Hansson  */
1704facdde1SUlf Hansson static inline void __maybe_unused add_quirk_sd(struct mmc_card *card, int data)
1714facdde1SUlf Hansson {
1724facdde1SUlf Hansson 	if (mmc_card_sd(card))
1734facdde1SUlf Hansson 		card->quirks |= data;
1744facdde1SUlf Hansson }
1754facdde1SUlf Hansson 
1764facdde1SUlf Hansson static inline void __maybe_unused remove_quirk_sd(struct mmc_card *card,
1774facdde1SUlf Hansson 						   int data)
1784facdde1SUlf Hansson {
1794facdde1SUlf Hansson 	if (mmc_card_sd(card))
1804facdde1SUlf Hansson 		card->quirks &= ~data;
1814facdde1SUlf Hansson }
1824facdde1SUlf Hansson 
1834facdde1SUlf Hansson static inline int mmc_card_lenient_fn0(const struct mmc_card *c)
1844facdde1SUlf Hansson {
1854facdde1SUlf Hansson 	return c->quirks & MMC_QUIRK_LENIENT_FN0;
1864facdde1SUlf Hansson }
1874facdde1SUlf Hansson 
1884facdde1SUlf Hansson static inline int mmc_blksz_for_byte_mode(const struct mmc_card *c)
1894facdde1SUlf Hansson {
1904facdde1SUlf Hansson 	return c->quirks & MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
1914facdde1SUlf Hansson }
1924facdde1SUlf Hansson 
1934facdde1SUlf Hansson static inline int mmc_card_disable_cd(const struct mmc_card *c)
1944facdde1SUlf Hansson {
1954facdde1SUlf Hansson 	return c->quirks & MMC_QUIRK_DISABLE_CD;
1964facdde1SUlf Hansson }
1974facdde1SUlf Hansson 
1984facdde1SUlf Hansson static inline int mmc_card_nonstd_func_interface(const struct mmc_card *c)
1994facdde1SUlf Hansson {
2004facdde1SUlf Hansson 	return c->quirks & MMC_QUIRK_NONSTD_FUNC_IF;
2014facdde1SUlf Hansson }
2024facdde1SUlf Hansson 
2034facdde1SUlf Hansson static inline int mmc_card_broken_byte_mode_512(const struct mmc_card *c)
2044facdde1SUlf Hansson {
2054facdde1SUlf Hansson 	return c->quirks & MMC_QUIRK_BROKEN_BYTE_MODE_512;
2064facdde1SUlf Hansson }
2074facdde1SUlf Hansson 
2084facdde1SUlf Hansson static inline int mmc_card_long_read_time(const struct mmc_card *c)
2094facdde1SUlf Hansson {
2104facdde1SUlf Hansson 	return c->quirks & MMC_QUIRK_LONG_READ_TIME;
2114facdde1SUlf Hansson }
2124facdde1SUlf Hansson 
2134facdde1SUlf Hansson static inline int mmc_card_broken_irq_polling(const struct mmc_card *c)
2144facdde1SUlf Hansson {
2154facdde1SUlf Hansson 	return c->quirks & MMC_QUIRK_BROKEN_IRQ_POLLING;
2164facdde1SUlf Hansson }
2174facdde1SUlf Hansson 
2184facdde1SUlf Hansson static inline int mmc_card_broken_hpi(const struct mmc_card *c)
2194facdde1SUlf Hansson {
2204facdde1SUlf Hansson 	return c->quirks & MMC_QUIRK_BROKEN_HPI;
2214facdde1SUlf Hansson }
2224facdde1SUlf Hansson 
2234facdde1SUlf Hansson #endif
224