0001
0002
0003
0004
0005
0006
0007 #ifndef LINUX_MMC_HOST_H
0008 #define LINUX_MMC_HOST_H
0009
0010 #include <linux/sched.h>
0011 #include <linux/device.h>
0012 #include <linux/fault-inject.h>
0013
0014 #include <linux/mmc/core.h>
0015 #include <linux/mmc/card.h>
0016 #include <linux/mmc/pm.h>
0017 #include <linux/dma-direction.h>
0018 #include <linux/blk-crypto-profile.h>
0019
0020 struct mmc_ios {
0021 unsigned int clock;
0022 unsigned short vdd;
0023 unsigned int power_delay_ms;
0024
0025
0026
0027 unsigned char bus_mode;
0028
0029 #define MMC_BUSMODE_OPENDRAIN 1
0030 #define MMC_BUSMODE_PUSHPULL 2
0031
0032 unsigned char chip_select;
0033
0034 #define MMC_CS_DONTCARE 0
0035 #define MMC_CS_HIGH 1
0036 #define MMC_CS_LOW 2
0037
0038 unsigned char power_mode;
0039
0040 #define MMC_POWER_OFF 0
0041 #define MMC_POWER_UP 1
0042 #define MMC_POWER_ON 2
0043 #define MMC_POWER_UNDEFINED 3
0044
0045 unsigned char bus_width;
0046
0047 #define MMC_BUS_WIDTH_1 0
0048 #define MMC_BUS_WIDTH_4 2
0049 #define MMC_BUS_WIDTH_8 3
0050
0051 unsigned char timing;
0052
0053 #define MMC_TIMING_LEGACY 0
0054 #define MMC_TIMING_MMC_HS 1
0055 #define MMC_TIMING_SD_HS 2
0056 #define MMC_TIMING_UHS_SDR12 3
0057 #define MMC_TIMING_UHS_SDR25 4
0058 #define MMC_TIMING_UHS_SDR50 5
0059 #define MMC_TIMING_UHS_SDR104 6
0060 #define MMC_TIMING_UHS_DDR50 7
0061 #define MMC_TIMING_MMC_DDR52 8
0062 #define MMC_TIMING_MMC_HS200 9
0063 #define MMC_TIMING_MMC_HS400 10
0064 #define MMC_TIMING_SD_EXP 11
0065 #define MMC_TIMING_SD_EXP_1_2V 12
0066
0067 unsigned char signal_voltage;
0068
0069 #define MMC_SIGNAL_VOLTAGE_330 0
0070 #define MMC_SIGNAL_VOLTAGE_180 1
0071 #define MMC_SIGNAL_VOLTAGE_120 2
0072
0073 unsigned char drv_type;
0074
0075 #define MMC_SET_DRIVER_TYPE_B 0
0076 #define MMC_SET_DRIVER_TYPE_A 1
0077 #define MMC_SET_DRIVER_TYPE_C 2
0078 #define MMC_SET_DRIVER_TYPE_D 3
0079
0080 bool enhanced_strobe;
0081 };
0082
0083 struct mmc_clk_phase {
0084 bool valid;
0085 u16 in_deg;
0086 u16 out_deg;
0087 };
0088
0089 #define MMC_NUM_CLK_PHASES (MMC_TIMING_MMC_HS400 + 1)
0090 struct mmc_clk_phase_map {
0091 struct mmc_clk_phase phase[MMC_NUM_CLK_PHASES];
0092 };
0093
0094 struct mmc_host;
0095
0096 enum mmc_err_stat {
0097 MMC_ERR_CMD_TIMEOUT,
0098 MMC_ERR_CMD_CRC,
0099 MMC_ERR_DAT_TIMEOUT,
0100 MMC_ERR_DAT_CRC,
0101 MMC_ERR_AUTO_CMD,
0102 MMC_ERR_ADMA,
0103 MMC_ERR_TUNING,
0104 MMC_ERR_CMDQ_RED,
0105 MMC_ERR_CMDQ_GCE,
0106 MMC_ERR_CMDQ_ICCE,
0107 MMC_ERR_REQ_TIMEOUT,
0108 MMC_ERR_CMDQ_REQ_TIMEOUT,
0109 MMC_ERR_ICE_CFG,
0110 MMC_ERR_CTRL_TIMEOUT,
0111 MMC_ERR_UNEXPECTED_IRQ,
0112 MMC_ERR_MAX,
0113 };
0114
0115 struct mmc_host_ops {
0116
0117
0118
0119
0120
0121
0122
0123
0124 void (*post_req)(struct mmc_host *host, struct mmc_request *req,
0125 int err);
0126 void (*pre_req)(struct mmc_host *host, struct mmc_request *req);
0127 void (*request)(struct mmc_host *host, struct mmc_request *req);
0128
0129 int (*request_atomic)(struct mmc_host *host,
0130 struct mmc_request *req);
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146 void (*set_ios)(struct mmc_host *host, struct mmc_ios *ios);
0147
0148
0149
0150
0151
0152
0153
0154
0155 int (*get_ro)(struct mmc_host *host);
0156
0157
0158
0159
0160
0161
0162
0163
0164 int (*get_cd)(struct mmc_host *host);
0165
0166 void (*enable_sdio_irq)(struct mmc_host *host, int enable);
0167
0168 void (*ack_sdio_irq)(struct mmc_host *host);
0169
0170
0171 void (*init_card)(struct mmc_host *host, struct mmc_card *card);
0172
0173 int (*start_signal_voltage_switch)(struct mmc_host *host, struct mmc_ios *ios);
0174
0175
0176 int (*card_busy)(struct mmc_host *host);
0177
0178
0179 int (*execute_tuning)(struct mmc_host *host, u32 opcode);
0180
0181
0182 int (*prepare_hs400_tuning)(struct mmc_host *host, struct mmc_ios *ios);
0183
0184
0185 int (*execute_hs400_tuning)(struct mmc_host *host, struct mmc_card *card);
0186
0187
0188 int (*hs400_prepare_ddr)(struct mmc_host *host);
0189
0190
0191 void (*hs400_downgrade)(struct mmc_host *host);
0192
0193
0194 void (*hs400_complete)(struct mmc_host *host);
0195
0196
0197 void (*hs400_enhanced_strobe)(struct mmc_host *host,
0198 struct mmc_ios *ios);
0199 int (*select_drive_strength)(struct mmc_card *card,
0200 unsigned int max_dtr, int host_drv,
0201 int card_drv, int *drv_type);
0202
0203 void (*card_hw_reset)(struct mmc_host *host);
0204 void (*card_event)(struct mmc_host *host);
0205
0206
0207
0208
0209
0210 int (*multi_io_quirk)(struct mmc_card *card,
0211 unsigned int direction, int blk_size);
0212
0213
0214 int (*init_sd_express)(struct mmc_host *host, struct mmc_ios *ios);
0215 };
0216
0217 struct mmc_cqe_ops {
0218
0219 int (*cqe_enable)(struct mmc_host *host, struct mmc_card *card);
0220
0221 void (*cqe_disable)(struct mmc_host *host);
0222
0223
0224
0225
0226 int (*cqe_request)(struct mmc_host *host, struct mmc_request *mrq);
0227
0228 void (*cqe_post_req)(struct mmc_host *host, struct mmc_request *mrq);
0229
0230
0231
0232
0233
0234 void (*cqe_off)(struct mmc_host *host);
0235
0236
0237
0238
0239 int (*cqe_wait_for_idle)(struct mmc_host *host);
0240
0241
0242
0243
0244
0245 bool (*cqe_timeout)(struct mmc_host *host, struct mmc_request *mrq,
0246 bool *recovery_needed);
0247
0248
0249
0250
0251 void (*cqe_recovery_start)(struct mmc_host *host);
0252
0253
0254
0255
0256
0257
0258 void (*cqe_recovery_finish)(struct mmc_host *host);
0259 };
0260
0261 struct mmc_async_req {
0262
0263 struct mmc_request *mrq;
0264
0265
0266
0267
0268 enum mmc_blk_status (*err_check)(struct mmc_card *, struct mmc_async_req *);
0269 };
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282 struct mmc_slot {
0283 int cd_irq;
0284 bool cd_wake_enabled;
0285 void *handler_priv;
0286 };
0287
0288
0289
0290
0291
0292
0293
0294
0295 struct mmc_context_info {
0296 bool is_done_rcv;
0297 bool is_new_req;
0298 bool is_waiting_last_req;
0299 wait_queue_head_t wait;
0300 };
0301
0302 struct regulator;
0303 struct mmc_pwrseq;
0304
0305 struct mmc_supply {
0306 struct regulator *vmmc;
0307 struct regulator *vqmmc;
0308 };
0309
0310 struct mmc_ctx {
0311 struct task_struct *task;
0312 };
0313
0314 struct mmc_host {
0315 struct device *parent;
0316 struct device class_dev;
0317 int index;
0318 const struct mmc_host_ops *ops;
0319 struct mmc_pwrseq *pwrseq;
0320 unsigned int f_min;
0321 unsigned int f_max;
0322 unsigned int f_init;
0323 u32 ocr_avail;
0324 u32 ocr_avail_sdio;
0325 u32 ocr_avail_sd;
0326 u32 ocr_avail_mmc;
0327 struct wakeup_source *ws;
0328 u32 max_current_330;
0329 u32 max_current_300;
0330 u32 max_current_180;
0331
0332 #define MMC_VDD_165_195 0x00000080
0333 #define MMC_VDD_20_21 0x00000100
0334 #define MMC_VDD_21_22 0x00000200
0335 #define MMC_VDD_22_23 0x00000400
0336 #define MMC_VDD_23_24 0x00000800
0337 #define MMC_VDD_24_25 0x00001000
0338 #define MMC_VDD_25_26 0x00002000
0339 #define MMC_VDD_26_27 0x00004000
0340 #define MMC_VDD_27_28 0x00008000
0341 #define MMC_VDD_28_29 0x00010000
0342 #define MMC_VDD_29_30 0x00020000
0343 #define MMC_VDD_30_31 0x00040000
0344 #define MMC_VDD_31_32 0x00080000
0345 #define MMC_VDD_32_33 0x00100000
0346 #define MMC_VDD_33_34 0x00200000
0347 #define MMC_VDD_34_35 0x00400000
0348 #define MMC_VDD_35_36 0x00800000
0349
0350 u32 caps;
0351
0352 #define MMC_CAP_4_BIT_DATA (1 << 0)
0353 #define MMC_CAP_MMC_HIGHSPEED (1 << 1)
0354 #define MMC_CAP_SD_HIGHSPEED (1 << 2)
0355 #define MMC_CAP_SDIO_IRQ (1 << 3)
0356 #define MMC_CAP_SPI (1 << 4)
0357 #define MMC_CAP_NEEDS_POLL (1 << 5)
0358 #define MMC_CAP_8_BIT_DATA (1 << 6)
0359 #define MMC_CAP_AGGRESSIVE_PM (1 << 7)
0360 #define MMC_CAP_NONREMOVABLE (1 << 8)
0361 #define MMC_CAP_WAIT_WHILE_BUSY (1 << 9)
0362 #define MMC_CAP_3_3V_DDR (1 << 11)
0363 #define MMC_CAP_1_8V_DDR (1 << 12)
0364 #define MMC_CAP_1_2V_DDR (1 << 13)
0365 #define MMC_CAP_DDR (MMC_CAP_3_3V_DDR | MMC_CAP_1_8V_DDR | \
0366 MMC_CAP_1_2V_DDR)
0367 #define MMC_CAP_POWER_OFF_CARD (1 << 14)
0368 #define MMC_CAP_BUS_WIDTH_TEST (1 << 15)
0369 #define MMC_CAP_UHS_SDR12 (1 << 16)
0370 #define MMC_CAP_UHS_SDR25 (1 << 17)
0371 #define MMC_CAP_UHS_SDR50 (1 << 18)
0372 #define MMC_CAP_UHS_SDR104 (1 << 19)
0373 #define MMC_CAP_UHS_DDR50 (1 << 20)
0374 #define MMC_CAP_UHS (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | \
0375 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | \
0376 MMC_CAP_UHS_DDR50)
0377 #define MMC_CAP_SYNC_RUNTIME_PM (1 << 21)
0378 #define MMC_CAP_NEED_RSP_BUSY (1 << 22)
0379 #define MMC_CAP_DRIVER_TYPE_A (1 << 23)
0380 #define MMC_CAP_DRIVER_TYPE_C (1 << 24)
0381 #define MMC_CAP_DRIVER_TYPE_D (1 << 25)
0382 #define MMC_CAP_DONE_COMPLETE (1 << 27)
0383 #define MMC_CAP_CD_WAKE (1 << 28)
0384 #define MMC_CAP_CMD_DURING_TFR (1 << 29)
0385 #define MMC_CAP_CMD23 (1 << 30)
0386 #define MMC_CAP_HW_RESET (1 << 31)
0387
0388 u32 caps2;
0389
0390 #define MMC_CAP2_BOOTPART_NOACC (1 << 0)
0391 #define MMC_CAP2_FULL_PWR_CYCLE (1 << 2)
0392 #define MMC_CAP2_FULL_PWR_CYCLE_IN_SUSPEND (1 << 3)
0393 #define MMC_CAP2_HS200_1_8V_SDR (1 << 5)
0394 #define MMC_CAP2_HS200_1_2V_SDR (1 << 6)
0395 #define MMC_CAP2_HS200 (MMC_CAP2_HS200_1_8V_SDR | \
0396 MMC_CAP2_HS200_1_2V_SDR)
0397 #define MMC_CAP2_SD_EXP (1 << 7)
0398 #define MMC_CAP2_SD_EXP_1_2V (1 << 8)
0399 #define MMC_CAP2_CD_ACTIVE_HIGH (1 << 10)
0400 #define MMC_CAP2_RO_ACTIVE_HIGH (1 << 11)
0401 #define MMC_CAP2_NO_PRESCAN_POWERUP (1 << 14)
0402 #define MMC_CAP2_HS400_1_8V (1 << 15)
0403 #define MMC_CAP2_HS400_1_2V (1 << 16)
0404 #define MMC_CAP2_HS400 (MMC_CAP2_HS400_1_8V | \
0405 MMC_CAP2_HS400_1_2V)
0406 #define MMC_CAP2_HSX00_1_8V (MMC_CAP2_HS200_1_8V_SDR | MMC_CAP2_HS400_1_8V)
0407 #define MMC_CAP2_HSX00_1_2V (MMC_CAP2_HS200_1_2V_SDR | MMC_CAP2_HS400_1_2V)
0408 #define MMC_CAP2_SDIO_IRQ_NOTHREAD (1 << 17)
0409 #define MMC_CAP2_NO_WRITE_PROTECT (1 << 18)
0410 #define MMC_CAP2_NO_SDIO (1 << 19)
0411 #define MMC_CAP2_HS400_ES (1 << 20)
0412 #define MMC_CAP2_NO_SD (1 << 21)
0413 #define MMC_CAP2_NO_MMC (1 << 22)
0414 #define MMC_CAP2_CQE (1 << 23)
0415 #define MMC_CAP2_CQE_DCMD (1 << 24)
0416 #define MMC_CAP2_AVOID_3_3V (1 << 25)
0417 #define MMC_CAP2_MERGE_CAPABLE (1 << 26)
0418 #ifdef CONFIG_MMC_CRYPTO
0419 #define MMC_CAP2_CRYPTO (1 << 27)
0420 #else
0421 #define MMC_CAP2_CRYPTO 0
0422 #endif
0423 #define MMC_CAP2_ALT_GPT_TEGRA (1 << 28)
0424
0425 int fixed_drv_type;
0426
0427 mmc_pm_flag_t pm_caps;
0428
0429
0430 unsigned int max_seg_size;
0431 unsigned short max_segs;
0432 unsigned short unused;
0433 unsigned int max_req_size;
0434 unsigned int max_blk_size;
0435 unsigned int max_blk_count;
0436 unsigned int max_busy_timeout;
0437
0438
0439 spinlock_t lock;
0440
0441 struct mmc_ios ios;
0442
0443
0444 unsigned int use_spi_crc:1;
0445 unsigned int claimed:1;
0446 unsigned int doing_init_tune:1;
0447 unsigned int can_retune:1;
0448 unsigned int doing_retune:1;
0449 unsigned int retune_now:1;
0450 unsigned int retune_paused:1;
0451 unsigned int retune_crc_disable:1;
0452 unsigned int can_dma_map_merge:1;
0453
0454 int rescan_disable;
0455 int rescan_entered;
0456
0457 int need_retune;
0458 int hold_retune;
0459 unsigned int retune_period;
0460 struct timer_list retune_timer;
0461
0462 bool trigger_card_event;
0463
0464 struct mmc_card *card;
0465
0466 wait_queue_head_t wq;
0467 struct mmc_ctx *claimer;
0468 int claim_cnt;
0469 struct mmc_ctx default_ctx;
0470
0471 struct delayed_work detect;
0472 int detect_change;
0473 struct mmc_slot slot;
0474
0475 const struct mmc_bus_ops *bus_ops;
0476
0477 unsigned int sdio_irqs;
0478 struct task_struct *sdio_irq_thread;
0479 struct delayed_work sdio_irq_work;
0480 bool sdio_irq_pending;
0481 atomic_t sdio_irq_thread_abort;
0482
0483 mmc_pm_flag_t pm_flags;
0484
0485 struct led_trigger *led;
0486
0487 #ifdef CONFIG_REGULATOR
0488 bool regulator_enabled;
0489 #endif
0490 struct mmc_supply supply;
0491
0492 struct dentry *debugfs_root;
0493
0494
0495 struct mmc_request *ongoing_mrq;
0496
0497 #ifdef CONFIG_FAIL_MMC_REQUEST
0498 struct fault_attr fail_mmc_request;
0499 #endif
0500
0501 unsigned int actual_clock;
0502
0503 unsigned int slotno;
0504
0505 int dsr_req;
0506 u32 dsr;
0507
0508
0509 const struct mmc_cqe_ops *cqe_ops;
0510 void *cqe_private;
0511 int cqe_qdepth;
0512 bool cqe_enabled;
0513 bool cqe_on;
0514
0515
0516 #ifdef CONFIG_MMC_CRYPTO
0517 struct blk_crypto_profile crypto_profile;
0518 #endif
0519
0520
0521 bool hsq_enabled;
0522
0523 u32 err_stats[MMC_ERR_MAX];
0524 unsigned long private[] ____cacheline_aligned;
0525 };
0526
0527 struct device_node;
0528
0529 struct mmc_host *mmc_alloc_host(int extra, struct device *);
0530 int mmc_add_host(struct mmc_host *);
0531 void mmc_remove_host(struct mmc_host *);
0532 void mmc_free_host(struct mmc_host *);
0533 void mmc_of_parse_clk_phase(struct mmc_host *host,
0534 struct mmc_clk_phase_map *map);
0535 int mmc_of_parse(struct mmc_host *host);
0536 int mmc_of_parse_voltage(struct mmc_host *host, u32 *mask);
0537
0538 static inline void *mmc_priv(struct mmc_host *host)
0539 {
0540 return (void *)host->private;
0541 }
0542
0543 static inline struct mmc_host *mmc_from_priv(void *priv)
0544 {
0545 return container_of(priv, struct mmc_host, private);
0546 }
0547
0548 #define mmc_host_is_spi(host) ((host)->caps & MMC_CAP_SPI)
0549
0550 #define mmc_dev(x) ((x)->parent)
0551 #define mmc_classdev(x) (&(x)->class_dev)
0552 #define mmc_hostname(x) (dev_name(&(x)->class_dev))
0553
0554 void mmc_detect_change(struct mmc_host *, unsigned long delay);
0555 void mmc_request_done(struct mmc_host *, struct mmc_request *);
0556 void mmc_command_done(struct mmc_host *host, struct mmc_request *mrq);
0557
0558 void mmc_cqe_request_done(struct mmc_host *host, struct mmc_request *mrq);
0559
0560
0561
0562
0563
0564 static inline bool sdio_irq_claimed(struct mmc_host *host)
0565 {
0566 return host->sdio_irqs > 0;
0567 }
0568
0569 static inline void mmc_signal_sdio_irq(struct mmc_host *host)
0570 {
0571 host->ops->enable_sdio_irq(host, 0);
0572 host->sdio_irq_pending = true;
0573 if (host->sdio_irq_thread)
0574 wake_up_process(host->sdio_irq_thread);
0575 }
0576
0577 void sdio_signal_irq(struct mmc_host *host);
0578
0579 #ifdef CONFIG_REGULATOR
0580 int mmc_regulator_set_ocr(struct mmc_host *mmc,
0581 struct regulator *supply,
0582 unsigned short vdd_bit);
0583 int mmc_regulator_set_vqmmc(struct mmc_host *mmc, struct mmc_ios *ios);
0584 #else
0585 static inline int mmc_regulator_set_ocr(struct mmc_host *mmc,
0586 struct regulator *supply,
0587 unsigned short vdd_bit)
0588 {
0589 return 0;
0590 }
0591
0592 static inline int mmc_regulator_set_vqmmc(struct mmc_host *mmc,
0593 struct mmc_ios *ios)
0594 {
0595 return -EINVAL;
0596 }
0597 #endif
0598
0599 int mmc_regulator_get_supply(struct mmc_host *mmc);
0600
0601 static inline int mmc_card_is_removable(struct mmc_host *host)
0602 {
0603 return !(host->caps & MMC_CAP_NONREMOVABLE);
0604 }
0605
0606 static inline int mmc_card_keep_power(struct mmc_host *host)
0607 {
0608 return host->pm_flags & MMC_PM_KEEP_POWER;
0609 }
0610
0611 static inline int mmc_card_wake_sdio_irq(struct mmc_host *host)
0612 {
0613 return host->pm_flags & MMC_PM_WAKE_SDIO_IRQ;
0614 }
0615
0616
0617 static inline int mmc_card_hs(struct mmc_card *card)
0618 {
0619 return card->host->ios.timing == MMC_TIMING_SD_HS ||
0620 card->host->ios.timing == MMC_TIMING_MMC_HS;
0621 }
0622
0623
0624 static inline int mmc_card_uhs(struct mmc_card *card)
0625 {
0626 return card->host->ios.timing >= MMC_TIMING_UHS_SDR12 &&
0627 card->host->ios.timing <= MMC_TIMING_UHS_DDR50;
0628 }
0629
0630 void mmc_retune_timer_stop(struct mmc_host *host);
0631
0632 static inline void mmc_retune_needed(struct mmc_host *host)
0633 {
0634 if (host->can_retune)
0635 host->need_retune = 1;
0636 }
0637
0638 static inline bool mmc_can_retune(struct mmc_host *host)
0639 {
0640 return host->can_retune == 1;
0641 }
0642
0643 static inline bool mmc_doing_retune(struct mmc_host *host)
0644 {
0645 return host->doing_retune == 1;
0646 }
0647
0648 static inline bool mmc_doing_tune(struct mmc_host *host)
0649 {
0650 return host->doing_retune == 1 || host->doing_init_tune == 1;
0651 }
0652
0653 static inline enum dma_data_direction mmc_get_dma_dir(struct mmc_data *data)
0654 {
0655 return data->flags & MMC_DATA_WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
0656 }
0657
0658 static inline void mmc_debugfs_err_stats_inc(struct mmc_host *host,
0659 enum mmc_err_stat stat)
0660 {
0661 host->err_stats[stat] += 1;
0662 }
0663
0664 int mmc_send_tuning(struct mmc_host *host, u32 opcode, int *cmd_error);
0665 int mmc_send_abort_tuning(struct mmc_host *host, u32 opcode);
0666 int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd);
0667
0668 #endif