1
2
3
4
5
6
7
8
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
17struct mmc_cid {
18 unsigned int manfid;
19 char prod_name[8];
20 unsigned char prv;
21 unsigned int serial;
22 unsigned short oemid;
23 unsigned short year;
24 unsigned char hwrev;
25 unsigned char fwrev;
26 unsigned char month;
27};
28
29struct mmc_csd {
30 unsigned char structure;
31 unsigned char mmca_vsn;
32 unsigned short cmdclass;
33 unsigned short tacc_clks;
34 unsigned int tacc_ns;
35 unsigned int c_size;
36 unsigned int r2w_factor;
37 unsigned int max_dtr;
38 unsigned int erase_size;
39 unsigned int read_blkbits;
40 unsigned int write_blkbits;
41 unsigned int capacity;
42 unsigned int read_partial:1,
43 read_misalign:1,
44 write_partial:1,
45 write_misalign:1,
46 dsr_imp:1;
47};
48
49struct mmc_ext_csd {
50 u8 rev;
51 u8 erase_group_def;
52 u8 sec_feature_support;
53 u8 rel_sectors;
54 u8 rel_param;
55 u8 part_config;
56 u8 cache_ctrl;
57 u8 rst_n_function;
58 u8 max_packed_writes;
59 u8 max_packed_reads;
60 u8 packed_event_en;
61 unsigned int part_time;
62 unsigned int sa_timeout;
63 unsigned int generic_cmd6_time;
64 unsigned int power_off_longtime;
65 u8 power_off_notification;
66 unsigned int hs_max_dtr;
67 unsigned int hs200_max_dtr;
68#define MMC_HIGH_26_MAX_DTR 26000000
69#define MMC_HIGH_52_MAX_DTR 52000000
70#define MMC_HIGH_DDR_MAX_DTR 52000000
71#define MMC_HS200_MAX_DTR 200000000
72 unsigned int sectors;
73 unsigned int hc_erase_size;
74 unsigned int hc_erase_timeout;
75 unsigned int sec_trim_mult;
76 unsigned int sec_erase_mult;
77 unsigned int trim_timeout;
78 bool partition_setting_completed;
79 unsigned long long enhanced_area_offset;
80 unsigned int enhanced_area_size;
81 unsigned int cache_size;
82 bool hpi_en;
83 bool hpi;
84 unsigned int hpi_cmd;
85 bool bkops;
86 bool man_bkops_en;
87 unsigned int data_sector_size;
88 unsigned int data_tag_unit_size;
89 unsigned int boot_ro_lock;
90 bool boot_ro_lockable;
91 bool ffu_capable;
92 bool cmdq_support;
93 unsigned int cmdq_depth;
94#define MMC_FIRMWARE_LEN 8
95 u8 fwrev[MMC_FIRMWARE_LEN];
96 u8 raw_exception_status;
97 u8 raw_partition_support;
98 u8 raw_rpmb_size_mult;
99 u8 raw_erased_mem_count;
100 u8 strobe_support;
101 u8 raw_ext_csd_structure;
102 u8 raw_card_type;
103 u8 raw_driver_strength;
104 u8 out_of_int_time;
105 u8 raw_pwr_cl_52_195;
106 u8 raw_pwr_cl_26_195;
107 u8 raw_pwr_cl_52_360;
108 u8 raw_pwr_cl_26_360;
109 u8 raw_s_a_timeout;
110 u8 raw_hc_erase_gap_size;
111 u8 raw_erase_timeout_mult;
112 u8 raw_hc_erase_grp_size;
113 u8 raw_sec_trim_mult;
114 u8 raw_sec_erase_mult;
115 u8 raw_sec_feature_support;
116 u8 raw_trim_mult;
117 u8 raw_pwr_cl_200_195;
118 u8 raw_pwr_cl_200_360;
119 u8 raw_pwr_cl_ddr_52_195;
120 u8 raw_pwr_cl_ddr_52_360;
121 u8 raw_pwr_cl_ddr_200_360;
122 u8 raw_bkops_status;
123 u8 raw_sectors[4];
124
125 unsigned int feature_support;
126#define MMC_DISCARD_FEATURE BIT(0)
127};
128
129struct sd_scr {
130 unsigned char sda_vsn;
131 unsigned char sda_spec3;
132 unsigned char bus_widths;
133#define SD_SCR_BUS_WIDTH_1 (1<<0)
134#define SD_SCR_BUS_WIDTH_4 (1<<2)
135 unsigned char cmds;
136#define SD_SCR_CMD20_SUPPORT (1<<0)
137#define SD_SCR_CMD23_SUPPORT (1<<1)
138};
139
140struct sd_ssr {
141 unsigned int au;
142 unsigned int erase_timeout;
143 unsigned int erase_offset;
144};
145
146struct sd_switch_caps {
147 unsigned int hs_max_dtr;
148 unsigned int uhs_max_dtr;
149#define HIGH_SPEED_MAX_DTR 50000000
150#define UHS_SDR104_MAX_DTR 208000000
151#define UHS_SDR50_MAX_DTR 100000000
152#define UHS_DDR50_MAX_DTR 50000000
153#define UHS_SDR25_MAX_DTR UHS_DDR50_MAX_DTR
154#define UHS_SDR12_MAX_DTR 25000000
155 unsigned int sd3_bus_mode;
156#define UHS_SDR12_BUS_SPEED 0
157#define HIGH_SPEED_BUS_SPEED 1
158#define UHS_SDR25_BUS_SPEED 1
159#define UHS_SDR50_BUS_SPEED 2
160#define UHS_SDR104_BUS_SPEED 3
161#define UHS_DDR50_BUS_SPEED 4
162
163#define SD_MODE_HIGH_SPEED (1 << HIGH_SPEED_BUS_SPEED)
164#define SD_MODE_UHS_SDR12 (1 << UHS_SDR12_BUS_SPEED)
165#define SD_MODE_UHS_SDR25 (1 << UHS_SDR25_BUS_SPEED)
166#define SD_MODE_UHS_SDR50 (1 << UHS_SDR50_BUS_SPEED)
167#define SD_MODE_UHS_SDR104 (1 << UHS_SDR104_BUS_SPEED)
168#define SD_MODE_UHS_DDR50 (1 << UHS_DDR50_BUS_SPEED)
169 unsigned int sd3_drv_type;
170#define SD_DRIVER_TYPE_B 0x01
171#define SD_DRIVER_TYPE_A 0x02
172#define SD_DRIVER_TYPE_C 0x04
173#define SD_DRIVER_TYPE_D 0x08
174 unsigned int sd3_curr_limit;
175#define SD_SET_CURRENT_LIMIT_200 0
176#define SD_SET_CURRENT_LIMIT_400 1
177#define SD_SET_CURRENT_LIMIT_600 2
178#define SD_SET_CURRENT_LIMIT_800 3
179#define SD_SET_CURRENT_NO_CHANGE (-1)
180
181#define SD_MAX_CURRENT_200 (1 << SD_SET_CURRENT_LIMIT_200)
182#define SD_MAX_CURRENT_400 (1 << SD_SET_CURRENT_LIMIT_400)
183#define SD_MAX_CURRENT_600 (1 << SD_SET_CURRENT_LIMIT_600)
184#define SD_MAX_CURRENT_800 (1 << SD_SET_CURRENT_LIMIT_800)
185};
186
187struct sdio_cccr {
188 unsigned int sdio_vsn;
189 unsigned int sd_vsn;
190 unsigned int multi_block:1,
191 low_speed:1,
192 wide_bus:1,
193 high_power:1,
194 high_speed:1,
195 disable_cd:1;
196};
197
198struct sdio_cis {
199 unsigned short vendor;
200 unsigned short device;
201 unsigned short blksize;
202 unsigned int max_dtr;
203};
204
205struct mmc_host;
206struct mmc_ios;
207struct sdio_func;
208struct sdio_func_tuple;
209
210#define SDIO_MAX_FUNCS 7
211
212
213
214
215
216#define MMC_NUM_BOOT_PARTITION 2
217#define MMC_NUM_GP_PARTITION 4
218#define MMC_NUM_PHY_PARTITION 7
219#define MAX_MMC_PART_NAME_LEN 20
220
221
222
223
224struct mmc_part {
225 unsigned int size;
226 unsigned int part_cfg;
227 char name[MAX_MMC_PART_NAME_LEN];
228 bool force_ro;
229 unsigned int area_type;
230#define MMC_BLK_DATA_AREA_MAIN (1<<0)
231#define MMC_BLK_DATA_AREA_BOOT (1<<1)
232#define MMC_BLK_DATA_AREA_GP (1<<2)
233#define MMC_BLK_DATA_AREA_RPMB (1<<3)
234};
235
236
237
238
239struct mmc_card {
240 struct mmc_host *host;
241 struct device dev;
242 u32 ocr;
243 unsigned int rca;
244 unsigned int type;
245#define MMC_TYPE_MMC 0
246#define MMC_TYPE_SD 1
247#define MMC_TYPE_SDIO 2
248#define MMC_TYPE_SD_COMBO 3
249 unsigned int state;
250#define MMC_STATE_PRESENT (1<<0)
251#define MMC_STATE_READONLY (1<<1)
252#define MMC_STATE_BLOCKADDR (1<<2)
253#define MMC_CARD_SDXC (1<<3)
254#define MMC_CARD_REMOVED (1<<4)
255#define MMC_STATE_DOING_BKOPS (1<<5)
256#define MMC_STATE_SUSPENDED (1<<6)
257 unsigned int quirks;
258#define MMC_QUIRK_LENIENT_FN0 (1<<0)
259#define MMC_QUIRK_BLKSZ_FOR_BYTE_MODE (1<<1)
260
261#define MMC_QUIRK_NONSTD_SDIO (1<<2)
262
263#define MMC_QUIRK_NONSTD_FUNC_IF (1<<4)
264#define MMC_QUIRK_DISABLE_CD (1<<5)
265#define MMC_QUIRK_INAND_CMD38 (1<<6)
266#define MMC_QUIRK_BLK_NO_CMD23 (1<<7)
267#define MMC_QUIRK_BROKEN_BYTE_MODE_512 (1<<8)
268
269#define MMC_QUIRK_LONG_READ_TIME (1<<9)
270#define MMC_QUIRK_SEC_ERASE_TRIM_BROKEN (1<<10)
271#define MMC_QUIRK_BROKEN_IRQ_POLLING (1<<11)
272#define MMC_QUIRK_TRIM_BROKEN (1<<12)
273#define MMC_QUIRK_BROKEN_HPI (1<<13)
274
275
276 unsigned int erase_size;
277 unsigned int erase_shift;
278 unsigned int pref_erase;
279 unsigned int eg_boundary;
280 u8 erased_byte;
281
282 u32 raw_cid[4];
283 u32 raw_csd[4];
284 u32 raw_scr[2];
285 u32 raw_ssr[16];
286 struct mmc_cid cid;
287 struct mmc_csd csd;
288 struct mmc_ext_csd ext_csd;
289 struct sd_scr scr;
290 struct sd_ssr ssr;
291 struct sd_switch_caps sw_caps;
292
293 unsigned int sdio_funcs;
294 struct sdio_cccr cccr;
295 struct sdio_cis cis;
296 struct sdio_func *sdio_func[SDIO_MAX_FUNCS];
297 struct sdio_func *sdio_single_irq;
298 unsigned num_info;
299 const char **info;
300 struct sdio_func_tuple *tuples;
301
302 unsigned int sd_bus_speed;
303 unsigned int mmc_avail_type;
304 unsigned int drive_strength;
305
306 struct dentry *debugfs_root;
307 struct mmc_part part[MMC_NUM_PHY_PARTITION];
308 unsigned int nr_parts;
309};
310
311
312
313
314static inline void mmc_part_add(struct mmc_card *card, unsigned int size,
315 unsigned int part_cfg, char *name, int idx, bool ro,
316 int area_type)
317{
318 card->part[card->nr_parts].size = size;
319 card->part[card->nr_parts].part_cfg = part_cfg;
320 sprintf(card->part[card->nr_parts].name, name, idx);
321 card->part[card->nr_parts].force_ro = ro;
322 card->part[card->nr_parts].area_type = area_type;
323 card->nr_parts++;
324}
325
326static inline bool mmc_large_sector(struct mmc_card *card)
327{
328 return card->ext_csd.data_sector_size == 4096;
329}
330
331
332
333
334
335
336struct mmc_fixup {
337
338 const char *name;
339
340
341 u64 rev_start, rev_end;
342
343 unsigned int manfid;
344 unsigned short oemid;
345
346
347 u16 cis_vendor, cis_device;
348
349
350 unsigned int ext_csd_rev;
351
352 void (*vendor_fixup)(struct mmc_card *card, int data);
353 int data;
354};
355
356#define CID_MANFID_ANY (-1u)
357#define CID_OEMID_ANY ((unsigned short) -1)
358#define CID_NAME_ANY (NULL)
359
360#define EXT_CSD_REV_ANY (-1u)
361
362#define CID_MANFID_SANDISK 0x2
363#define CID_MANFID_TOSHIBA 0x11
364#define CID_MANFID_MICRON 0x13
365#define CID_MANFID_SAMSUNG 0x15
366#define CID_MANFID_KINGSTON 0x70
367#define CID_MANFID_HYNIX 0x90
368
369#define END_FIXUP { NULL }
370
371#define _FIXUP_EXT(_name, _manfid, _oemid, _rev_start, _rev_end, \
372 _cis_vendor, _cis_device, \
373 _fixup, _data, _ext_csd_rev) \
374 { \
375 .name = (_name), \
376 .manfid = (_manfid), \
377 .oemid = (_oemid), \
378 .rev_start = (_rev_start), \
379 .rev_end = (_rev_end), \
380 .cis_vendor = (_cis_vendor), \
381 .cis_device = (_cis_device), \
382 .vendor_fixup = (_fixup), \
383 .data = (_data), \
384 .ext_csd_rev = (_ext_csd_rev), \
385 }
386
387#define MMC_FIXUP_REV(_name, _manfid, _oemid, _rev_start, _rev_end, \
388 _fixup, _data, _ext_csd_rev) \
389 _FIXUP_EXT(_name, _manfid, \
390 _oemid, _rev_start, _rev_end, \
391 SDIO_ANY_ID, SDIO_ANY_ID, \
392 _fixup, _data, _ext_csd_rev) \
393
394#define MMC_FIXUP(_name, _manfid, _oemid, _fixup, _data) \
395 MMC_FIXUP_REV(_name, _manfid, _oemid, 0, -1ull, _fixup, _data, \
396 EXT_CSD_REV_ANY)
397
398#define MMC_FIXUP_EXT_CSD_REV(_name, _manfid, _oemid, _fixup, _data, \
399 _ext_csd_rev) \
400 MMC_FIXUP_REV(_name, _manfid, _oemid, 0, -1ull, _fixup, _data, \
401 _ext_csd_rev)
402
403#define SDIO_FIXUP(_vendor, _device, _fixup, _data) \
404 _FIXUP_EXT(CID_NAME_ANY, CID_MANFID_ANY, \
405 CID_OEMID_ANY, 0, -1ull, \
406 _vendor, _device, \
407 _fixup, _data, EXT_CSD_REV_ANY) \
408
409#define cid_rev(hwrev, fwrev, year, month) \
410 (((u64) hwrev) << 40 | \
411 ((u64) fwrev) << 32 | \
412 ((u64) year) << 16 | \
413 ((u64) month))
414
415#define cid_rev_card(card) \
416 cid_rev(card->cid.hwrev, \
417 card->cid.fwrev, \
418 card->cid.year, \
419 card->cid.month)
420
421
422
423
424
425static inline void __maybe_unused add_quirk(struct mmc_card *card, int data)
426{
427 card->quirks |= data;
428}
429
430static inline void __maybe_unused remove_quirk(struct mmc_card *card, int data)
431{
432 card->quirks &= ~data;
433}
434
435#define mmc_card_mmc(c) ((c)->type == MMC_TYPE_MMC)
436#define mmc_card_sd(c) ((c)->type == MMC_TYPE_SD)
437#define mmc_card_sdio(c) ((c)->type == MMC_TYPE_SDIO)
438
439#define mmc_card_present(c) ((c)->state & MMC_STATE_PRESENT)
440#define mmc_card_readonly(c) ((c)->state & MMC_STATE_READONLY)
441#define mmc_card_blockaddr(c) ((c)->state & MMC_STATE_BLOCKADDR)
442#define mmc_card_ext_capacity(c) ((c)->state & MMC_CARD_SDXC)
443#define mmc_card_removed(c) ((c) && ((c)->state & MMC_CARD_REMOVED))
444#define mmc_card_doing_bkops(c) ((c)->state & MMC_STATE_DOING_BKOPS)
445#define mmc_card_suspended(c) ((c)->state & MMC_STATE_SUSPENDED)
446
447#define mmc_card_set_present(c) ((c)->state |= MMC_STATE_PRESENT)
448#define mmc_card_set_readonly(c) ((c)->state |= MMC_STATE_READONLY)
449#define mmc_card_set_blockaddr(c) ((c)->state |= MMC_STATE_BLOCKADDR)
450#define mmc_card_set_ext_capacity(c) ((c)->state |= MMC_CARD_SDXC)
451#define mmc_card_set_removed(c) ((c)->state |= MMC_CARD_REMOVED)
452#define mmc_card_set_doing_bkops(c) ((c)->state |= MMC_STATE_DOING_BKOPS)
453#define mmc_card_clr_doing_bkops(c) ((c)->state &= ~MMC_STATE_DOING_BKOPS)
454#define mmc_card_set_suspended(c) ((c)->state |= MMC_STATE_SUSPENDED)
455#define mmc_card_clr_suspended(c) ((c)->state &= ~MMC_STATE_SUSPENDED)
456
457
458
459
460
461static inline void __maybe_unused add_quirk_mmc(struct mmc_card *card, int data)
462{
463 if (mmc_card_mmc(card))
464 card->quirks |= data;
465}
466
467static inline void __maybe_unused remove_quirk_mmc(struct mmc_card *card,
468 int data)
469{
470 if (mmc_card_mmc(card))
471 card->quirks &= ~data;
472}
473
474
475
476
477
478static inline void __maybe_unused add_quirk_sd(struct mmc_card *card, int data)
479{
480 if (mmc_card_sd(card))
481 card->quirks |= data;
482}
483
484static inline void __maybe_unused remove_quirk_sd(struct mmc_card *card,
485 int data)
486{
487 if (mmc_card_sd(card))
488 card->quirks &= ~data;
489}
490
491static inline int mmc_card_lenient_fn0(const struct mmc_card *c)
492{
493 return c->quirks & MMC_QUIRK_LENIENT_FN0;
494}
495
496static inline int mmc_blksz_for_byte_mode(const struct mmc_card *c)
497{
498 return c->quirks & MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
499}
500
501static inline int mmc_card_disable_cd(const struct mmc_card *c)
502{
503 return c->quirks & MMC_QUIRK_DISABLE_CD;
504}
505
506static inline int mmc_card_nonstd_func_interface(const struct mmc_card *c)
507{
508 return c->quirks & MMC_QUIRK_NONSTD_FUNC_IF;
509}
510
511static inline int mmc_card_broken_byte_mode_512(const struct mmc_card *c)
512{
513 return c->quirks & MMC_QUIRK_BROKEN_BYTE_MODE_512;
514}
515
516static inline int mmc_card_long_read_time(const struct mmc_card *c)
517{
518 return c->quirks & MMC_QUIRK_LONG_READ_TIME;
519}
520
521static inline int mmc_card_broken_irq_polling(const struct mmc_card *c)
522{
523 return c->quirks & MMC_QUIRK_BROKEN_IRQ_POLLING;
524}
525
526static inline int mmc_card_broken_hpi(const struct mmc_card *c)
527{
528 return c->quirks & MMC_QUIRK_BROKEN_HPI;
529}
530
531#define mmc_card_name(c) ((c)->cid.prod_name)
532#define mmc_card_id(c) (dev_name(&(c)->dev))
533
534#define mmc_dev_to_card(d) container_of(d, struct mmc_card, dev)
535
536
537
538
539struct mmc_driver {
540 struct device_driver drv;
541 int (*probe)(struct mmc_card *);
542 void (*remove)(struct mmc_card *);
543 void (*shutdown)(struct mmc_card *);
544};
545
546extern int mmc_register_driver(struct mmc_driver *);
547extern void mmc_unregister_driver(struct mmc_driver *);
548
549extern void mmc_fixup_device(struct mmc_card *card,
550 const struct mmc_fixup *table);
551
552#endif
553