1*4facdde1SUlf Hansson /* 2*4facdde1SUlf Hansson * Private header for the mmc subsystem 3*4facdde1SUlf Hansson * 4*4facdde1SUlf Hansson * Copyright (C) 2016 Linaro Ltd 5*4facdde1SUlf Hansson * 6*4facdde1SUlf Hansson * Author: Ulf Hansson <ulf.hansson@linaro.org> 7*4facdde1SUlf Hansson * 8*4facdde1SUlf Hansson * License terms: GNU General Public License (GPL) version 2 9*4facdde1SUlf Hansson */ 10*4facdde1SUlf Hansson 11*4facdde1SUlf Hansson #ifndef _MMC_CORE_CARD_H 12*4facdde1SUlf Hansson #define _MMC_CORE_CARD_H 13*4facdde1SUlf Hansson 14*4facdde1SUlf Hansson #include <linux/mmc/card.h> 15*4facdde1SUlf Hansson 16*4facdde1SUlf Hansson #define mmc_card_name(c) ((c)->cid.prod_name) 17*4facdde1SUlf Hansson #define mmc_card_id(c) (dev_name(&(c)->dev)) 18*4facdde1SUlf Hansson #define mmc_dev_to_card(d) container_of(d, struct mmc_card, dev) 19*4facdde1SUlf Hansson 20*4facdde1SUlf Hansson /* Card states */ 21*4facdde1SUlf Hansson #define MMC_STATE_PRESENT (1<<0) /* present in sysfs */ 22*4facdde1SUlf Hansson #define MMC_STATE_READONLY (1<<1) /* card is read-only */ 23*4facdde1SUlf Hansson #define MMC_STATE_BLOCKADDR (1<<2) /* card uses block-addressing */ 24*4facdde1SUlf Hansson #define MMC_CARD_SDXC (1<<3) /* card is SDXC */ 25*4facdde1SUlf Hansson #define MMC_CARD_REMOVED (1<<4) /* card has been removed */ 26*4facdde1SUlf Hansson #define MMC_STATE_DOING_BKOPS (1<<5) /* card is doing BKOPS */ 27*4facdde1SUlf Hansson #define MMC_STATE_SUSPENDED (1<<6) /* card is suspended */ 28*4facdde1SUlf Hansson 29*4facdde1SUlf Hansson #define mmc_card_present(c) ((c)->state & MMC_STATE_PRESENT) 30*4facdde1SUlf Hansson #define mmc_card_readonly(c) ((c)->state & MMC_STATE_READONLY) 31*4facdde1SUlf Hansson #define mmc_card_blockaddr(c) ((c)->state & MMC_STATE_BLOCKADDR) 32*4facdde1SUlf Hansson #define mmc_card_ext_capacity(c) ((c)->state & MMC_CARD_SDXC) 33*4facdde1SUlf Hansson #define mmc_card_removed(c) ((c) && ((c)->state & MMC_CARD_REMOVED)) 34*4facdde1SUlf Hansson #define mmc_card_doing_bkops(c) ((c)->state & MMC_STATE_DOING_BKOPS) 35*4facdde1SUlf Hansson #define mmc_card_suspended(c) ((c)->state & MMC_STATE_SUSPENDED) 36*4facdde1SUlf Hansson 37*4facdde1SUlf Hansson #define mmc_card_set_present(c) ((c)->state |= MMC_STATE_PRESENT) 38*4facdde1SUlf Hansson #define mmc_card_set_readonly(c) ((c)->state |= MMC_STATE_READONLY) 39*4facdde1SUlf Hansson #define mmc_card_set_blockaddr(c) ((c)->state |= MMC_STATE_BLOCKADDR) 40*4facdde1SUlf Hansson #define mmc_card_set_ext_capacity(c) ((c)->state |= MMC_CARD_SDXC) 41*4facdde1SUlf Hansson #define mmc_card_set_removed(c) ((c)->state |= MMC_CARD_REMOVED) 42*4facdde1SUlf Hansson #define mmc_card_set_doing_bkops(c) ((c)->state |= MMC_STATE_DOING_BKOPS) 43*4facdde1SUlf Hansson #define mmc_card_clr_doing_bkops(c) ((c)->state &= ~MMC_STATE_DOING_BKOPS) 44*4facdde1SUlf Hansson #define mmc_card_set_suspended(c) ((c)->state |= MMC_STATE_SUSPENDED) 45*4facdde1SUlf Hansson #define mmc_card_clr_suspended(c) ((c)->state &= ~MMC_STATE_SUSPENDED) 46*4facdde1SUlf Hansson 47*4facdde1SUlf Hansson /* 48*4facdde1SUlf Hansson * The world is not perfect and supplies us with broken mmc/sdio devices. 49*4facdde1SUlf Hansson * For at least some of these bugs we need a work-around. 50*4facdde1SUlf Hansson */ 51*4facdde1SUlf Hansson struct mmc_fixup { 52*4facdde1SUlf Hansson /* CID-specific fields. */ 53*4facdde1SUlf Hansson const char *name; 54*4facdde1SUlf Hansson 55*4facdde1SUlf Hansson /* Valid revision range */ 56*4facdde1SUlf Hansson u64 rev_start, rev_end; 57*4facdde1SUlf Hansson 58*4facdde1SUlf Hansson unsigned int manfid; 59*4facdde1SUlf Hansson unsigned short oemid; 60*4facdde1SUlf Hansson 61*4facdde1SUlf Hansson /* SDIO-specific fields. You can use SDIO_ANY_ID here of course */ 62*4facdde1SUlf Hansson u16 cis_vendor, cis_device; 63*4facdde1SUlf Hansson 64*4facdde1SUlf Hansson /* for MMC cards */ 65*4facdde1SUlf Hansson unsigned int ext_csd_rev; 66*4facdde1SUlf Hansson 67*4facdde1SUlf Hansson void (*vendor_fixup)(struct mmc_card *card, int data); 68*4facdde1SUlf Hansson int data; 69*4facdde1SUlf Hansson }; 70*4facdde1SUlf Hansson 71*4facdde1SUlf Hansson #define CID_MANFID_ANY (-1u) 72*4facdde1SUlf Hansson #define CID_OEMID_ANY ((unsigned short) -1) 73*4facdde1SUlf Hansson #define CID_NAME_ANY (NULL) 74*4facdde1SUlf Hansson 75*4facdde1SUlf Hansson #define EXT_CSD_REV_ANY (-1u) 76*4facdde1SUlf Hansson 77*4facdde1SUlf Hansson #define CID_MANFID_SANDISK 0x2 78*4facdde1SUlf Hansson #define CID_MANFID_TOSHIBA 0x11 79*4facdde1SUlf Hansson #define CID_MANFID_MICRON 0x13 80*4facdde1SUlf Hansson #define CID_MANFID_SAMSUNG 0x15 81*4facdde1SUlf Hansson #define CID_MANFID_KINGSTON 0x70 82*4facdde1SUlf Hansson #define CID_MANFID_HYNIX 0x90 83*4facdde1SUlf Hansson 84*4facdde1SUlf Hansson #define END_FIXUP { NULL } 85*4facdde1SUlf Hansson 86*4facdde1SUlf Hansson #define _FIXUP_EXT(_name, _manfid, _oemid, _rev_start, _rev_end, \ 87*4facdde1SUlf Hansson _cis_vendor, _cis_device, \ 88*4facdde1SUlf Hansson _fixup, _data, _ext_csd_rev) \ 89*4facdde1SUlf Hansson { \ 90*4facdde1SUlf Hansson .name = (_name), \ 91*4facdde1SUlf Hansson .manfid = (_manfid), \ 92*4facdde1SUlf Hansson .oemid = (_oemid), \ 93*4facdde1SUlf Hansson .rev_start = (_rev_start), \ 94*4facdde1SUlf Hansson .rev_end = (_rev_end), \ 95*4facdde1SUlf Hansson .cis_vendor = (_cis_vendor), \ 96*4facdde1SUlf Hansson .cis_device = (_cis_device), \ 97*4facdde1SUlf Hansson .vendor_fixup = (_fixup), \ 98*4facdde1SUlf Hansson .data = (_data), \ 99*4facdde1SUlf Hansson .ext_csd_rev = (_ext_csd_rev), \ 100*4facdde1SUlf Hansson } 101*4facdde1SUlf Hansson 102*4facdde1SUlf Hansson #define MMC_FIXUP_REV(_name, _manfid, _oemid, _rev_start, _rev_end, \ 103*4facdde1SUlf Hansson _fixup, _data, _ext_csd_rev) \ 104*4facdde1SUlf Hansson _FIXUP_EXT(_name, _manfid, \ 105*4facdde1SUlf Hansson _oemid, _rev_start, _rev_end, \ 106*4facdde1SUlf Hansson SDIO_ANY_ID, SDIO_ANY_ID, \ 107*4facdde1SUlf Hansson _fixup, _data, _ext_csd_rev) \ 108*4facdde1SUlf Hansson 109*4facdde1SUlf Hansson #define MMC_FIXUP(_name, _manfid, _oemid, _fixup, _data) \ 110*4facdde1SUlf Hansson MMC_FIXUP_REV(_name, _manfid, _oemid, 0, -1ull, _fixup, _data, \ 111*4facdde1SUlf Hansson EXT_CSD_REV_ANY) 112*4facdde1SUlf Hansson 113*4facdde1SUlf Hansson #define MMC_FIXUP_EXT_CSD_REV(_name, _manfid, _oemid, _fixup, _data, \ 114*4facdde1SUlf Hansson _ext_csd_rev) \ 115*4facdde1SUlf Hansson MMC_FIXUP_REV(_name, _manfid, _oemid, 0, -1ull, _fixup, _data, \ 116*4facdde1SUlf Hansson _ext_csd_rev) 117*4facdde1SUlf Hansson 118*4facdde1SUlf Hansson #define SDIO_FIXUP(_vendor, _device, _fixup, _data) \ 119*4facdde1SUlf Hansson _FIXUP_EXT(CID_NAME_ANY, CID_MANFID_ANY, \ 120*4facdde1SUlf Hansson CID_OEMID_ANY, 0, -1ull, \ 121*4facdde1SUlf Hansson _vendor, _device, \ 122*4facdde1SUlf Hansson _fixup, _data, EXT_CSD_REV_ANY) \ 123*4facdde1SUlf Hansson 124*4facdde1SUlf Hansson #define cid_rev(hwrev, fwrev, year, month) \ 125*4facdde1SUlf Hansson (((u64) hwrev) << 40 | \ 126*4facdde1SUlf Hansson ((u64) fwrev) << 32 | \ 127*4facdde1SUlf Hansson ((u64) year) << 16 | \ 128*4facdde1SUlf Hansson ((u64) month)) 129*4facdde1SUlf Hansson 130*4facdde1SUlf Hansson #define cid_rev_card(card) \ 131*4facdde1SUlf Hansson cid_rev(card->cid.hwrev, \ 132*4facdde1SUlf Hansson card->cid.fwrev, \ 133*4facdde1SUlf Hansson card->cid.year, \ 134*4facdde1SUlf Hansson card->cid.month) 135*4facdde1SUlf Hansson 136*4facdde1SUlf Hansson /* 137*4facdde1SUlf Hansson * Unconditionally quirk add/remove. 138*4facdde1SUlf Hansson */ 139*4facdde1SUlf Hansson static inline void __maybe_unused add_quirk(struct mmc_card *card, int data) 140*4facdde1SUlf Hansson { 141*4facdde1SUlf Hansson card->quirks |= data; 142*4facdde1SUlf Hansson } 143*4facdde1SUlf Hansson 144*4facdde1SUlf Hansson static inline void __maybe_unused remove_quirk(struct mmc_card *card, int data) 145*4facdde1SUlf Hansson { 146*4facdde1SUlf Hansson card->quirks &= ~data; 147*4facdde1SUlf Hansson } 148*4facdde1SUlf Hansson 149*4facdde1SUlf Hansson /* 150*4facdde1SUlf Hansson * Quirk add/remove for MMC products. 151*4facdde1SUlf Hansson */ 152*4facdde1SUlf Hansson static inline void __maybe_unused add_quirk_mmc(struct mmc_card *card, int data) 153*4facdde1SUlf Hansson { 154*4facdde1SUlf Hansson if (mmc_card_mmc(card)) 155*4facdde1SUlf Hansson card->quirks |= data; 156*4facdde1SUlf Hansson } 157*4facdde1SUlf Hansson 158*4facdde1SUlf Hansson static inline void __maybe_unused remove_quirk_mmc(struct mmc_card *card, 159*4facdde1SUlf Hansson int data) 160*4facdde1SUlf Hansson { 161*4facdde1SUlf Hansson if (mmc_card_mmc(card)) 162*4facdde1SUlf Hansson card->quirks &= ~data; 163*4facdde1SUlf Hansson } 164*4facdde1SUlf Hansson 165*4facdde1SUlf Hansson /* 166*4facdde1SUlf Hansson * Quirk add/remove for SD products. 167*4facdde1SUlf Hansson */ 168*4facdde1SUlf Hansson static inline void __maybe_unused add_quirk_sd(struct mmc_card *card, int data) 169*4facdde1SUlf Hansson { 170*4facdde1SUlf Hansson if (mmc_card_sd(card)) 171*4facdde1SUlf Hansson card->quirks |= data; 172*4facdde1SUlf Hansson } 173*4facdde1SUlf Hansson 174*4facdde1SUlf Hansson static inline void __maybe_unused remove_quirk_sd(struct mmc_card *card, 175*4facdde1SUlf Hansson int data) 176*4facdde1SUlf Hansson { 177*4facdde1SUlf Hansson if (mmc_card_sd(card)) 178*4facdde1SUlf Hansson card->quirks &= ~data; 179*4facdde1SUlf Hansson } 180*4facdde1SUlf Hansson 181*4facdde1SUlf Hansson static inline int mmc_card_lenient_fn0(const struct mmc_card *c) 182*4facdde1SUlf Hansson { 183*4facdde1SUlf Hansson return c->quirks & MMC_QUIRK_LENIENT_FN0; 184*4facdde1SUlf Hansson } 185*4facdde1SUlf Hansson 186*4facdde1SUlf Hansson static inline int mmc_blksz_for_byte_mode(const struct mmc_card *c) 187*4facdde1SUlf Hansson { 188*4facdde1SUlf Hansson return c->quirks & MMC_QUIRK_BLKSZ_FOR_BYTE_MODE; 189*4facdde1SUlf Hansson } 190*4facdde1SUlf Hansson 191*4facdde1SUlf Hansson static inline int mmc_card_disable_cd(const struct mmc_card *c) 192*4facdde1SUlf Hansson { 193*4facdde1SUlf Hansson return c->quirks & MMC_QUIRK_DISABLE_CD; 194*4facdde1SUlf Hansson } 195*4facdde1SUlf Hansson 196*4facdde1SUlf Hansson static inline int mmc_card_nonstd_func_interface(const struct mmc_card *c) 197*4facdde1SUlf Hansson { 198*4facdde1SUlf Hansson return c->quirks & MMC_QUIRK_NONSTD_FUNC_IF; 199*4facdde1SUlf Hansson } 200*4facdde1SUlf Hansson 201*4facdde1SUlf Hansson static inline int mmc_card_broken_byte_mode_512(const struct mmc_card *c) 202*4facdde1SUlf Hansson { 203*4facdde1SUlf Hansson return c->quirks & MMC_QUIRK_BROKEN_BYTE_MODE_512; 204*4facdde1SUlf Hansson } 205*4facdde1SUlf Hansson 206*4facdde1SUlf Hansson static inline int mmc_card_long_read_time(const struct mmc_card *c) 207*4facdde1SUlf Hansson { 208*4facdde1SUlf Hansson return c->quirks & MMC_QUIRK_LONG_READ_TIME; 209*4facdde1SUlf Hansson } 210*4facdde1SUlf Hansson 211*4facdde1SUlf Hansson static inline int mmc_card_broken_irq_polling(const struct mmc_card *c) 212*4facdde1SUlf Hansson { 213*4facdde1SUlf Hansson return c->quirks & MMC_QUIRK_BROKEN_IRQ_POLLING; 214*4facdde1SUlf Hansson } 215*4facdde1SUlf Hansson 216*4facdde1SUlf Hansson static inline int mmc_card_broken_hpi(const struct mmc_card *c) 217*4facdde1SUlf Hansson { 218*4facdde1SUlf Hansson return c->quirks & MMC_QUIRK_BROKEN_HPI; 219*4facdde1SUlf Hansson } 220*4facdde1SUlf Hansson 221*4facdde1SUlf Hansson void mmc_fixup_device(struct mmc_card *card, const struct mmc_fixup *table); 222*4facdde1SUlf Hansson 223*4facdde1SUlf Hansson #endif 224