1 /*
2 * linux/include/linux/mmc/card.h
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Card driver specific definitions.
9 */
10 #ifndef LINUX_MMC_CARD_H
11 #define LINUX_MMC_CARD_H
12
13 #include <linux/device.h>
14 #include <linux/mmc/core.h>
15 #include <linux/mod_devicetable.h>
16
17 struct mmc_cid {
18 unsigned int manfid;
19 char prod_name[8];
20 unsigned int serial;
21 unsigned short oemid;
22 unsigned short year;
23 unsigned char hwrev;
24 unsigned char fwrev;
25 unsigned char month;
26 };
27
28 struct mmc_csd {
29 unsigned char structure;
30 unsigned char mmca_vsn;
31 unsigned short cmdclass;
32 unsigned short tacc_clks;
33 unsigned int tacc_ns;
34 unsigned int c_size;
35 unsigned int r2w_factor;
36 unsigned int max_dtr;
37 unsigned int erase_size; /* In sectors */
38 unsigned int read_blkbits;
39 unsigned int write_blkbits;
40 unsigned int capacity;
41 unsigned int read_partial:1,
42 read_misalign:1,
43 write_partial:1,
44 write_misalign:1;
45 };
46
47 struct mmc_ext_csd {
48 u8 rev;
49 u8 erase_group_def;
50 u8 sec_feature_support;
51 u8 rel_sectors;
52 u8 rel_param;
53 u8 part_config;
54 u8 cache_ctrl;
55 u8 rst_n_function;
56 unsigned int part_time; /* Units: ms */
57 unsigned int sa_timeout; /* Units: 100ns */
58 unsigned int generic_cmd6_time; /* Units: 10ms */
59 unsigned int power_off_longtime; /* Units: ms */
60 unsigned int hs_max_dtr;
61 unsigned int sectors;
62 unsigned int card_type;
63 unsigned int hc_erase_size; /* In sectors */
64 unsigned int hc_erase_timeout; /* In milliseconds */
65 unsigned int sec_trim_mult; /* Secure trim multiplier */
66 unsigned int sec_erase_mult; /* Secure erase multiplier */
67 unsigned int trim_timeout; /* In milliseconds */
68 bool enhanced_area_en; /* enable bit */
69 unsigned long long enhanced_area_offset; /* Units: Byte */
70 unsigned int enhanced_area_size; /* Units: KB */
71 unsigned int cache_size; /* Units: KB */
72 bool hpi_en; /* HPI enablebit */
73 bool hpi; /* HPI support bit */
74 unsigned int hpi_cmd; /* cmd used as HPI */
75 unsigned int data_sector_size; /* 512 bytes or 4KB */
76 unsigned int data_tag_unit_size; /* DATA TAG UNIT size */
77 unsigned int boot_ro_lock; /* ro lock support */
78 bool boot_ro_lockable;
79 u8 raw_partition_support; /* 160 */
80 u8 raw_erased_mem_count; /* 181 */
81 u8 raw_ext_csd_structure; /* 194 */
82 u8 raw_card_type; /* 196 */
83 u8 out_of_int_time; /* 198 */
84 u8 raw_s_a_timeout; /* 217 */
85 u8 raw_hc_erase_gap_size; /* 221 */
86 u8 raw_erase_timeout_mult; /* 223 */
87 u8 raw_hc_erase_grp_size; /* 224 */
88 u8 raw_sec_trim_mult; /* 229 */
89 u8 raw_sec_erase_mult; /* 230 */
90 u8 raw_sec_feature_support;/* 231 */
91 u8 raw_trim_mult; /* 232 */
92 u8 raw_sectors[4]; /* 212 - 4 bytes */
93
94 unsigned int feature_support;
95 #define MMC_DISCARD_FEATURE BIT(0) /* CMD38 feature */
96 };
97
98 struct sd_scr {
99 unsigned char sda_vsn;
100 unsigned char sda_spec3;
101 unsigned char bus_widths;
102 #define SD_SCR_BUS_WIDTH_1 (1<<0)
103 #define SD_SCR_BUS_WIDTH_4 (1<<2)
104 unsigned char cmds;
105 #define SD_SCR_CMD20_SUPPORT (1<<0)
106 #define SD_SCR_CMD23_SUPPORT (1<<1)
107 };
108
109 struct sd_ssr {
110 unsigned int au; /* In sectors */
111 unsigned int erase_timeout; /* In milliseconds */
112 unsigned int erase_offset; /* In milliseconds */
113 };
114
115 struct sd_switch_caps {
116 unsigned int hs_max_dtr;
117 unsigned int uhs_max_dtr;
118 #define HIGH_SPEED_MAX_DTR 50000000
119 #define UHS_SDR104_MAX_DTR 208000000
120 #define UHS_SDR50_MAX_DTR 100000000
121 #define UHS_DDR50_MAX_DTR 50000000
122 #define UHS_SDR25_MAX_DTR UHS_DDR50_MAX_DTR
123 #define UHS_SDR12_MAX_DTR 25000000
124 unsigned int sd3_bus_mode;
125 #define UHS_SDR12_BUS_SPEED 0
126 #define HIGH_SPEED_BUS_SPEED 1
127 #define UHS_SDR25_BUS_SPEED 1
128 #define UHS_SDR50_BUS_SPEED 2
129 #define UHS_SDR104_BUS_SPEED 3
130 #define UHS_DDR50_BUS_SPEED 4
131
132 #define SD_MODE_HIGH_SPEED (1 << HIGH_SPEED_BUS_SPEED)
133 #define SD_MODE_UHS_SDR12 (1 << UHS_SDR12_BUS_SPEED)
134 #define SD_MODE_UHS_SDR25 (1 << UHS_SDR25_BUS_SPEED)
135 #define SD_MODE_UHS_SDR50 (1 << UHS_SDR50_BUS_SPEED)
136 #define SD_MODE_UHS_SDR104 (1 << UHS_SDR104_BUS_SPEED)
137 #define SD_MODE_UHS_DDR50 (1 << UHS_DDR50_BUS_SPEED)
138 unsigned int sd3_drv_type;
139 #define SD_DRIVER_TYPE_B 0x01
140 #define SD_DRIVER_TYPE_A 0x02
141 #define SD_DRIVER_TYPE_C 0x04
142 #define SD_DRIVER_TYPE_D 0x08
143 unsigned int sd3_curr_limit;
144 #define SD_SET_CURRENT_LIMIT_200 0
145 #define SD_SET_CURRENT_LIMIT_400 1
146 #define SD_SET_CURRENT_LIMIT_600 2
147 #define SD_SET_CURRENT_LIMIT_800 3
148
149 #define SD_MAX_CURRENT_200 (1 << SD_SET_CURRENT_LIMIT_200)
150 #define SD_MAX_CURRENT_400 (1 << SD_SET_CURRENT_LIMIT_400)
151 #define SD_MAX_CURRENT_600 (1 << SD_SET_CURRENT_LIMIT_600)
152 #define SD_MAX_CURRENT_800 (1 << SD_SET_CURRENT_LIMIT_800)
153 };
154
155 struct sdio_cccr {
156 unsigned int sdio_vsn;
157 unsigned int sd_vsn;
158 unsigned int multi_block:1,
159 low_speed:1,
160 wide_bus:1,
161 high_power:1,
162 high_speed:1,
163 disable_cd:1;
164 };
165
166 struct sdio_cis {
167 unsigned short vendor;
168 unsigned short device;
169 unsigned short blksize;
170 unsigned int max_dtr;
171 };
172
173 struct mmc_host;
174 struct sdio_func;
175 struct sdio_func_tuple;
176
177 #define SDIO_MAX_FUNCS 7
178
179 /* The number of MMC physical partitions. These consist of:
180 * boot partitions (2), general purpose partitions (4) in MMC v4.4.
181 */
182 #define MMC_NUM_BOOT_PARTITION 2
183 #define MMC_NUM_GP_PARTITION 4
184 #define MMC_NUM_PHY_PARTITION 6
185 #define MAX_MMC_PART_NAME_LEN 20
186
187 /*
188 * MMC Physical partitions
189 */
190 struct mmc_part {
191 unsigned int size; /* partition size (in bytes) */
192 unsigned int part_cfg; /* partition type */
193 char name[MAX_MMC_PART_NAME_LEN];
194 bool force_ro; /* to make boot parts RO by default */
195 unsigned int area_type;
196 #define MMC_BLK_DATA_AREA_MAIN (1<<0)
197 #define MMC_BLK_DATA_AREA_BOOT (1<<1)
198 #define MMC_BLK_DATA_AREA_GP (1<<2)
199 };
200
201 /*
202 * MMC device
203 */
204 struct mmc_card {
205 struct mmc_host *host; /* the host this device belongs to */
206 struct device dev; /* the device */
207 unsigned int rca; /* relative card address of device */
208 unsigned int type; /* card type */
209 #define MMC_TYPE_MMC 0 /* MMC card */
210 #define MMC_TYPE_SD 1 /* SD card */
211 #define MMC_TYPE_SDIO 2 /* SDIO card */
212 #define MMC_TYPE_SD_COMBO 3 /* SD combo (IO+mem) card */
213 unsigned int state; /* (our) card state */
214 #define MMC_STATE_PRESENT (1<<0) /* present in sysfs */
215 #define MMC_STATE_READONLY (1<<1) /* card is read-only */
216 #define MMC_STATE_HIGHSPEED (1<<2) /* card is in high speed mode */
217 #define MMC_STATE_BLOCKADDR (1<<3) /* card uses block-addressing */
218 #define MMC_STATE_HIGHSPEED_DDR (1<<4) /* card is in high speed mode */
219 #define MMC_STATE_ULTRAHIGHSPEED (1<<5) /* card is in ultra high speed mode */
220 #define MMC_CARD_SDXC (1<<6) /* card is SDXC */
221 #define MMC_CARD_REMOVED (1<<7) /* card has been removed */
222 #define MMC_STATE_HIGHSPEED_200 (1<<8) /* card is in HS200 mode */
223 #define MMC_STATE_SLEEP (1<<9) /* card is in sleep state */
224 unsigned int quirks; /* card quirks */
225 #define MMC_QUIRK_LENIENT_FN0 (1<<0) /* allow SDIO FN0 writes outside of the VS CCCR range */
226 #define MMC_QUIRK_BLKSZ_FOR_BYTE_MODE (1<<1) /* use func->cur_blksize */
227 /* for byte mode */
228 #define MMC_QUIRK_NONSTD_SDIO (1<<2) /* non-standard SDIO card attached */
229 /* (missing CIA registers) */
230 #define MMC_QUIRK_BROKEN_CLK_GATING (1<<3) /* clock gating the sdio bus will make card fail */
231 #define MMC_QUIRK_NONSTD_FUNC_IF (1<<4) /* SDIO card has nonstd function interfaces */
232 #define MMC_QUIRK_DISABLE_CD (1<<5) /* disconnect CD/DAT[3] resistor */
233 #define MMC_QUIRK_INAND_CMD38 (1<<6) /* iNAND devices have broken CMD38 */
234 #define MMC_QUIRK_BLK_NO_CMD23 (1<<7) /* Avoid CMD23 for regular multiblock */
235 #define MMC_QUIRK_BROKEN_BYTE_MODE_512 (1<<8) /* Avoid sending 512 bytes in */
236 #define MMC_QUIRK_LONG_READ_TIME (1<<9) /* Data read time > CSD says */
237 #define MMC_QUIRK_SEC_ERASE_TRIM_BROKEN (1<<10) /* Skip secure for erase/trim */
238 /* byte mode */
239 unsigned int poweroff_notify_state; /* eMMC4.5 notify feature */
240 #define MMC_NO_POWER_NOTIFICATION 0
241 #define MMC_POWERED_ON 1
242 #define MMC_POWEROFF_SHORT 2
243 #define MMC_POWEROFF_LONG 3
244
245 unsigned int erase_size; /* erase size in sectors */
246 unsigned int erase_shift; /* if erase unit is power 2 */
247 unsigned int pref_erase; /* in sectors */
248 u8 erased_byte; /* value of erased bytes */
249
250 u32 raw_cid[4]; /* raw card CID */
251 u32 raw_csd[4]; /* raw card CSD */
252 u32 raw_scr[2]; /* raw card SCR */
253 struct mmc_cid cid; /* card identification */
254 struct mmc_csd csd; /* card specific */
255 struct mmc_ext_csd ext_csd; /* mmc v4 extended card specific */
256 struct sd_scr scr; /* extra SD information */
257 struct sd_ssr ssr; /* yet more SD information */
258 struct sd_switch_caps sw_caps; /* switch (CMD6) caps */
259
260 unsigned int sdio_funcs; /* number of SDIO functions */
261 struct sdio_cccr cccr; /* common card info */
262 struct sdio_cis cis; /* common tuple info */
263 struct sdio_func *sdio_func[SDIO_MAX_FUNCS]; /* SDIO functions (devices) */
264 struct sdio_func *sdio_single_irq; /* SDIO function when only one IRQ active */
265 unsigned num_info; /* number of info strings */
266 const char **info; /* info strings */
267 struct sdio_func_tuple *tuples; /* unknown common tuples */
268
269 unsigned int sd_bus_speed; /* Bus Speed Mode set for the card */
270
271 struct dentry *debugfs_root;
272 struct mmc_part part[MMC_NUM_PHY_PARTITION]; /* physical partitions */
273 unsigned int nr_parts;
274 };
275
276 /*
277 * This function fill contents in mmc_part.
278 */
mmc_part_add(struct mmc_card * card,unsigned int size,unsigned int part_cfg,char * name,int idx,bool ro,int area_type)279 static inline void mmc_part_add(struct mmc_card *card, unsigned int size,
280 unsigned int part_cfg, char *name, int idx, bool ro,
281 int area_type)
282 {
283 card->part[card->nr_parts].size = size;
284 card->part[card->nr_parts].part_cfg = part_cfg;
285 sprintf(card->part[card->nr_parts].name, name, idx);
286 card->part[card->nr_parts].force_ro = ro;
287 card->part[card->nr_parts].area_type = area_type;
288 card->nr_parts++;
289 }
290
291 /*
292 * The world is not perfect and supplies us with broken mmc/sdio devices.
293 * For at least some of these bugs we need a work-around.
294 */
295
296 struct mmc_fixup {
297 /* CID-specific fields. */
298 const char *name;
299
300 /* Valid revision range */
301 u64 rev_start, rev_end;
302
303 unsigned int manfid;
304 unsigned short oemid;
305
306 /* SDIO-specfic fields. You can use SDIO_ANY_ID here of course */
307 u16 cis_vendor, cis_device;
308
309 void (*vendor_fixup)(struct mmc_card *card, int data);
310 int data;
311 };
312
313 #define CID_MANFID_ANY (-1u)
314 #define CID_OEMID_ANY ((unsigned short) -1)
315 #define CID_NAME_ANY (NULL)
316
317 #define END_FIXUP { 0 }
318
319 #define _FIXUP_EXT(_name, _manfid, _oemid, _rev_start, _rev_end, \
320 _cis_vendor, _cis_device, \
321 _fixup, _data) \
322 { \
323 .name = (_name), \
324 .manfid = (_manfid), \
325 .oemid = (_oemid), \
326 .rev_start = (_rev_start), \
327 .rev_end = (_rev_end), \
328 .cis_vendor = (_cis_vendor), \
329 .cis_device = (_cis_device), \
330 .vendor_fixup = (_fixup), \
331 .data = (_data), \
332 }
333
334 #define MMC_FIXUP_REV(_name, _manfid, _oemid, _rev_start, _rev_end, \
335 _fixup, _data) \
336 _FIXUP_EXT(_name, _manfid, \
337 _oemid, _rev_start, _rev_end, \
338 SDIO_ANY_ID, SDIO_ANY_ID, \
339 _fixup, _data) \
340
341 #define MMC_FIXUP(_name, _manfid, _oemid, _fixup, _data) \
342 MMC_FIXUP_REV(_name, _manfid, _oemid, 0, -1ull, _fixup, _data)
343
344 #define SDIO_FIXUP(_vendor, _device, _fixup, _data) \
345 _FIXUP_EXT(CID_NAME_ANY, CID_MANFID_ANY, \
346 CID_OEMID_ANY, 0, -1ull, \
347 _vendor, _device, \
348 _fixup, _data) \
349
350 #define cid_rev(hwrev, fwrev, year, month) \
351 (((u64) hwrev) << 40 | \
352 ((u64) fwrev) << 32 | \
353 ((u64) year) << 16 | \
354 ((u64) month))
355
356 #define cid_rev_card(card) \
357 cid_rev(card->cid.hwrev, \
358 card->cid.fwrev, \
359 card->cid.year, \
360 card->cid.month)
361
362 /*
363 * Unconditionally quirk add/remove.
364 */
365
add_quirk(struct mmc_card * card,int data)366 static inline void __maybe_unused add_quirk(struct mmc_card *card, int data)
367 {
368 card->quirks |= data;
369 }
370
remove_quirk(struct mmc_card * card,int data)371 static inline void __maybe_unused remove_quirk(struct mmc_card *card, int data)
372 {
373 card->quirks &= ~data;
374 }
375
376 #define mmc_card_mmc(c) ((c)->type == MMC_TYPE_MMC)
377 #define mmc_card_sd(c) ((c)->type == MMC_TYPE_SD)
378 #define mmc_card_sdio(c) ((c)->type == MMC_TYPE_SDIO)
379
380 #define mmc_card_present(c) ((c)->state & MMC_STATE_PRESENT)
381 #define mmc_card_readonly(c) ((c)->state & MMC_STATE_READONLY)
382 #define mmc_card_highspeed(c) ((c)->state & MMC_STATE_HIGHSPEED)
383 #define mmc_card_hs200(c) ((c)->state & MMC_STATE_HIGHSPEED_200)
384 #define mmc_card_blockaddr(c) ((c)->state & MMC_STATE_BLOCKADDR)
385 #define mmc_card_ddr_mode(c) ((c)->state & MMC_STATE_HIGHSPEED_DDR)
386 #define mmc_card_uhs(c) ((c)->state & MMC_STATE_ULTRAHIGHSPEED)
387 #define mmc_sd_card_uhs(c) ((c)->state & MMC_STATE_ULTRAHIGHSPEED)
388 #define mmc_card_ext_capacity(c) ((c)->state & MMC_CARD_SDXC)
389 #define mmc_card_removed(c) ((c) && ((c)->state & MMC_CARD_REMOVED))
390 #define mmc_card_is_sleep(c) ((c)->state & MMC_STATE_SLEEP)
391
392 #define mmc_card_set_present(c) ((c)->state |= MMC_STATE_PRESENT)
393 #define mmc_card_set_readonly(c) ((c)->state |= MMC_STATE_READONLY)
394 #define mmc_card_set_highspeed(c) ((c)->state |= MMC_STATE_HIGHSPEED)
395 #define mmc_card_set_hs200(c) ((c)->state |= MMC_STATE_HIGHSPEED_200)
396 #define mmc_card_set_blockaddr(c) ((c)->state |= MMC_STATE_BLOCKADDR)
397 #define mmc_card_set_ddr_mode(c) ((c)->state |= MMC_STATE_HIGHSPEED_DDR)
398 #define mmc_card_set_uhs(c) ((c)->state |= MMC_STATE_ULTRAHIGHSPEED)
399 #define mmc_sd_card_set_uhs(c) ((c)->state |= MMC_STATE_ULTRAHIGHSPEED)
400 #define mmc_card_set_ext_capacity(c) ((c)->state |= MMC_CARD_SDXC)
401 #define mmc_card_set_removed(c) ((c)->state |= MMC_CARD_REMOVED)
402 #define mmc_card_set_sleep(c) ((c)->state |= MMC_STATE_SLEEP)
403
404 #define mmc_card_clr_sleep(c) ((c)->state &= ~MMC_STATE_SLEEP)
405 /*
406 * Quirk add/remove for MMC products.
407 */
408
add_quirk_mmc(struct mmc_card * card,int data)409 static inline void __maybe_unused add_quirk_mmc(struct mmc_card *card, int data)
410 {
411 if (mmc_card_mmc(card))
412 card->quirks |= data;
413 }
414
remove_quirk_mmc(struct mmc_card * card,int data)415 static inline void __maybe_unused remove_quirk_mmc(struct mmc_card *card,
416 int data)
417 {
418 if (mmc_card_mmc(card))
419 card->quirks &= ~data;
420 }
421
422 /*
423 * Quirk add/remove for SD products.
424 */
425
add_quirk_sd(struct mmc_card * card,int data)426 static inline void __maybe_unused add_quirk_sd(struct mmc_card *card, int data)
427 {
428 if (mmc_card_sd(card))
429 card->quirks |= data;
430 }
431
remove_quirk_sd(struct mmc_card * card,int data)432 static inline void __maybe_unused remove_quirk_sd(struct mmc_card *card,
433 int data)
434 {
435 if (mmc_card_sd(card))
436 card->quirks &= ~data;
437 }
438
mmc_card_lenient_fn0(const struct mmc_card * c)439 static inline int mmc_card_lenient_fn0(const struct mmc_card *c)
440 {
441 return c->quirks & MMC_QUIRK_LENIENT_FN0;
442 }
443
mmc_blksz_for_byte_mode(const struct mmc_card * c)444 static inline int mmc_blksz_for_byte_mode(const struct mmc_card *c)
445 {
446 return c->quirks & MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
447 }
448
mmc_card_disable_cd(const struct mmc_card * c)449 static inline int mmc_card_disable_cd(const struct mmc_card *c)
450 {
451 return c->quirks & MMC_QUIRK_DISABLE_CD;
452 }
453
mmc_card_nonstd_func_interface(const struct mmc_card * c)454 static inline int mmc_card_nonstd_func_interface(const struct mmc_card *c)
455 {
456 return c->quirks & MMC_QUIRK_NONSTD_FUNC_IF;
457 }
458
mmc_card_broken_byte_mode_512(const struct mmc_card * c)459 static inline int mmc_card_broken_byte_mode_512(const struct mmc_card *c)
460 {
461 return c->quirks & MMC_QUIRK_BROKEN_BYTE_MODE_512;
462 }
463
mmc_card_long_read_time(const struct mmc_card * c)464 static inline int mmc_card_long_read_time(const struct mmc_card *c)
465 {
466 return c->quirks & MMC_QUIRK_LONG_READ_TIME;
467 }
468
469 #define mmc_card_name(c) ((c)->cid.prod_name)
470 #define mmc_card_id(c) (dev_name(&(c)->dev))
471
472 #define mmc_dev_to_card(d) container_of(d, struct mmc_card, dev)
473
474 #define mmc_list_to_card(l) container_of(l, struct mmc_card, node)
475 #define mmc_get_drvdata(c) dev_get_drvdata(&(c)->dev)
476 #define mmc_set_drvdata(c,d) dev_set_drvdata(&(c)->dev, d)
477
478 /*
479 * MMC device driver (e.g., Flash card, I/O card...)
480 */
481 struct mmc_driver {
482 struct device_driver drv;
483 int (*probe)(struct mmc_card *);
484 void (*remove)(struct mmc_card *);
485 int (*suspend)(struct mmc_card *);
486 int (*resume)(struct mmc_card *);
487 };
488
489 extern int mmc_register_driver(struct mmc_driver *);
490 extern void mmc_unregister_driver(struct mmc_driver *);
491
492 extern void mmc_fixup_device(struct mmc_card *card,
493 const struct mmc_fixup *table);
494
495 #endif /* LINUX_MMC_CARD_H */
496