18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * linux/drivers/mmc/core/sd.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2003-2004 Russell King, All Rights Reserved. 68c2ecf20Sopenharmony_ci * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved. 78c2ecf20Sopenharmony_ci * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved. 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/err.h> 118c2ecf20Sopenharmony_ci#include <linux/sizes.h> 128c2ecf20Sopenharmony_ci#include <linux/slab.h> 138c2ecf20Sopenharmony_ci#include <linux/stat.h> 148c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include <linux/mmc/host.h> 178c2ecf20Sopenharmony_ci#include <linux/mmc/card.h> 188c2ecf20Sopenharmony_ci#include <linux/mmc/mmc.h> 198c2ecf20Sopenharmony_ci#include <linux/mmc/sd.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci#include "core.h" 228c2ecf20Sopenharmony_ci#include "card.h" 238c2ecf20Sopenharmony_ci#include "host.h" 248c2ecf20Sopenharmony_ci#include "bus.h" 258c2ecf20Sopenharmony_ci#include "mmc_ops.h" 268c2ecf20Sopenharmony_ci#include "sd.h" 278c2ecf20Sopenharmony_ci#include "sd_ops.h" 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_cistatic const unsigned int tran_exp[] = { 308c2ecf20Sopenharmony_ci 10000, 100000, 1000000, 10000000, 318c2ecf20Sopenharmony_ci 0, 0, 0, 0 328c2ecf20Sopenharmony_ci}; 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_cistatic const unsigned char tran_mant[] = { 358c2ecf20Sopenharmony_ci 0, 10, 12, 13, 15, 20, 25, 30, 368c2ecf20Sopenharmony_ci 35, 40, 45, 50, 55, 60, 70, 80, 378c2ecf20Sopenharmony_ci}; 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_cistatic const unsigned int taac_exp[] = { 408c2ecf20Sopenharmony_ci 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 418c2ecf20Sopenharmony_ci}; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_cistatic const unsigned int taac_mant[] = { 448c2ecf20Sopenharmony_ci 0, 10, 12, 13, 15, 20, 25, 30, 458c2ecf20Sopenharmony_ci 35, 40, 45, 50, 55, 60, 70, 80, 468c2ecf20Sopenharmony_ci}; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_cistatic const unsigned int sd_au_size[] = { 498c2ecf20Sopenharmony_ci 0, SZ_16K / 512, SZ_32K / 512, SZ_64K / 512, 508c2ecf20Sopenharmony_ci SZ_128K / 512, SZ_256K / 512, SZ_512K / 512, SZ_1M / 512, 518c2ecf20Sopenharmony_ci SZ_2M / 512, SZ_4M / 512, SZ_8M / 512, (SZ_8M + SZ_4M) / 512, 528c2ecf20Sopenharmony_ci SZ_16M / 512, (SZ_16M + SZ_8M) / 512, SZ_32M / 512, SZ_64M / 512, 538c2ecf20Sopenharmony_ci}; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci#define UNSTUFF_BITS(resp,start,size) \ 568c2ecf20Sopenharmony_ci ({ \ 578c2ecf20Sopenharmony_ci const int __size = size; \ 588c2ecf20Sopenharmony_ci const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \ 598c2ecf20Sopenharmony_ci const int __off = 3 - ((start) / 32); \ 608c2ecf20Sopenharmony_ci const int __shft = (start) & 31; \ 618c2ecf20Sopenharmony_ci u32 __res; \ 628c2ecf20Sopenharmony_ci \ 638c2ecf20Sopenharmony_ci __res = resp[__off] >> __shft; \ 648c2ecf20Sopenharmony_ci if (__size + __shft > 32) \ 658c2ecf20Sopenharmony_ci __res |= resp[__off-1] << ((32 - __shft) % 32); \ 668c2ecf20Sopenharmony_ci __res & __mask; \ 678c2ecf20Sopenharmony_ci }) 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci/* 708c2ecf20Sopenharmony_ci * Given the decoded CSD structure, decode the raw CID to our CID structure. 718c2ecf20Sopenharmony_ci */ 728c2ecf20Sopenharmony_civoid mmc_decode_cid(struct mmc_card *card) 738c2ecf20Sopenharmony_ci{ 748c2ecf20Sopenharmony_ci u32 *resp = card->raw_cid; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci /* 778c2ecf20Sopenharmony_ci * SD doesn't currently have a version field so we will 788c2ecf20Sopenharmony_ci * have to assume we can parse this. 798c2ecf20Sopenharmony_ci */ 808c2ecf20Sopenharmony_ci card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); 818c2ecf20Sopenharmony_ci card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); 828c2ecf20Sopenharmony_ci card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); 838c2ecf20Sopenharmony_ci card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); 848c2ecf20Sopenharmony_ci card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); 858c2ecf20Sopenharmony_ci card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); 868c2ecf20Sopenharmony_ci card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); 878c2ecf20Sopenharmony_ci card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4); 888c2ecf20Sopenharmony_ci card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4); 898c2ecf20Sopenharmony_ci card->cid.serial = UNSTUFF_BITS(resp, 24, 32); 908c2ecf20Sopenharmony_ci card->cid.year = UNSTUFF_BITS(resp, 12, 8); 918c2ecf20Sopenharmony_ci card->cid.month = UNSTUFF_BITS(resp, 8, 4); 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci card->cid.year += 2000; /* SD cards year offset */ 948c2ecf20Sopenharmony_ci} 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci/* 978c2ecf20Sopenharmony_ci * Given a 128-bit response, decode to our card CSD structure. 988c2ecf20Sopenharmony_ci */ 998c2ecf20Sopenharmony_cistatic int mmc_decode_csd(struct mmc_card *card) 1008c2ecf20Sopenharmony_ci{ 1018c2ecf20Sopenharmony_ci struct mmc_csd *csd = &card->csd; 1028c2ecf20Sopenharmony_ci unsigned int e, m, csd_struct; 1038c2ecf20Sopenharmony_ci u32 *resp = card->raw_csd; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci csd_struct = UNSTUFF_BITS(resp, 126, 2); 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci switch (csd_struct) { 1088c2ecf20Sopenharmony_ci case 0: 1098c2ecf20Sopenharmony_ci m = UNSTUFF_BITS(resp, 115, 4); 1108c2ecf20Sopenharmony_ci e = UNSTUFF_BITS(resp, 112, 3); 1118c2ecf20Sopenharmony_ci csd->taac_ns = (taac_exp[e] * taac_mant[m] + 9) / 10; 1128c2ecf20Sopenharmony_ci csd->taac_clks = UNSTUFF_BITS(resp, 104, 8) * 100; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci m = UNSTUFF_BITS(resp, 99, 4); 1158c2ecf20Sopenharmony_ci e = UNSTUFF_BITS(resp, 96, 3); 1168c2ecf20Sopenharmony_ci csd->max_dtr = tran_exp[e] * tran_mant[m]; 1178c2ecf20Sopenharmony_ci csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci e = UNSTUFF_BITS(resp, 47, 3); 1208c2ecf20Sopenharmony_ci m = UNSTUFF_BITS(resp, 62, 12); 1218c2ecf20Sopenharmony_ci csd->capacity = (1 + m) << (e + 2); 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); 1248c2ecf20Sopenharmony_ci csd->read_partial = UNSTUFF_BITS(resp, 79, 1); 1258c2ecf20Sopenharmony_ci csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); 1268c2ecf20Sopenharmony_ci csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); 1278c2ecf20Sopenharmony_ci csd->dsr_imp = UNSTUFF_BITS(resp, 76, 1); 1288c2ecf20Sopenharmony_ci csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); 1298c2ecf20Sopenharmony_ci csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); 1308c2ecf20Sopenharmony_ci csd->write_partial = UNSTUFF_BITS(resp, 21, 1); 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci if (UNSTUFF_BITS(resp, 46, 1)) { 1338c2ecf20Sopenharmony_ci csd->erase_size = 1; 1348c2ecf20Sopenharmony_ci } else if (csd->write_blkbits >= 9) { 1358c2ecf20Sopenharmony_ci csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1; 1368c2ecf20Sopenharmony_ci csd->erase_size <<= csd->write_blkbits - 9; 1378c2ecf20Sopenharmony_ci } 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci if (UNSTUFF_BITS(resp, 13, 1)) 1408c2ecf20Sopenharmony_ci mmc_card_set_readonly(card); 1418c2ecf20Sopenharmony_ci break; 1428c2ecf20Sopenharmony_ci case 1: 1438c2ecf20Sopenharmony_ci /* 1448c2ecf20Sopenharmony_ci * This is a block-addressed SDHC or SDXC card. Most 1458c2ecf20Sopenharmony_ci * interesting fields are unused and have fixed 1468c2ecf20Sopenharmony_ci * values. To avoid getting tripped by buggy cards, 1478c2ecf20Sopenharmony_ci * we assume those fixed values ourselves. 1488c2ecf20Sopenharmony_ci */ 1498c2ecf20Sopenharmony_ci mmc_card_set_blockaddr(card); 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci csd->taac_ns = 0; /* Unused */ 1528c2ecf20Sopenharmony_ci csd->taac_clks = 0; /* Unused */ 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci m = UNSTUFF_BITS(resp, 99, 4); 1558c2ecf20Sopenharmony_ci e = UNSTUFF_BITS(resp, 96, 3); 1568c2ecf20Sopenharmony_ci csd->max_dtr = tran_exp[e] * tran_mant[m]; 1578c2ecf20Sopenharmony_ci csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); 1588c2ecf20Sopenharmony_ci csd->c_size = UNSTUFF_BITS(resp, 48, 22); 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci /* SDXC cards have a minimum C_SIZE of 0x00FFFF */ 1618c2ecf20Sopenharmony_ci if (csd->c_size >= 0xFFFF) 1628c2ecf20Sopenharmony_ci mmc_card_set_ext_capacity(card); 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci m = UNSTUFF_BITS(resp, 48, 22); 1658c2ecf20Sopenharmony_ci csd->capacity = (1 + m) << 10; 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci csd->read_blkbits = 9; 1688c2ecf20Sopenharmony_ci csd->read_partial = 0; 1698c2ecf20Sopenharmony_ci csd->write_misalign = 0; 1708c2ecf20Sopenharmony_ci csd->read_misalign = 0; 1718c2ecf20Sopenharmony_ci csd->r2w_factor = 4; /* Unused */ 1728c2ecf20Sopenharmony_ci csd->write_blkbits = 9; 1738c2ecf20Sopenharmony_ci csd->write_partial = 0; 1748c2ecf20Sopenharmony_ci csd->erase_size = 1; 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci if (UNSTUFF_BITS(resp, 13, 1)) 1778c2ecf20Sopenharmony_ci mmc_card_set_readonly(card); 1788c2ecf20Sopenharmony_ci break; 1798c2ecf20Sopenharmony_ci default: 1808c2ecf20Sopenharmony_ci pr_err("%s: unrecognised CSD structure version %d\n", 1818c2ecf20Sopenharmony_ci mmc_hostname(card->host), csd_struct); 1828c2ecf20Sopenharmony_ci return -EINVAL; 1838c2ecf20Sopenharmony_ci } 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci card->erase_size = csd->erase_size; 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci return 0; 1888c2ecf20Sopenharmony_ci} 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci/* 1918c2ecf20Sopenharmony_ci * Given a 64-bit response, decode to our card SCR structure. 1928c2ecf20Sopenharmony_ci */ 1938c2ecf20Sopenharmony_cistatic int mmc_decode_scr(struct mmc_card *card) 1948c2ecf20Sopenharmony_ci{ 1958c2ecf20Sopenharmony_ci struct sd_scr *scr = &card->scr; 1968c2ecf20Sopenharmony_ci unsigned int scr_struct; 1978c2ecf20Sopenharmony_ci u32 resp[4]; 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci resp[3] = card->raw_scr[1]; 2008c2ecf20Sopenharmony_ci resp[2] = card->raw_scr[0]; 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci scr_struct = UNSTUFF_BITS(resp, 60, 4); 2038c2ecf20Sopenharmony_ci if (scr_struct != 0) { 2048c2ecf20Sopenharmony_ci pr_err("%s: unrecognised SCR structure version %d\n", 2058c2ecf20Sopenharmony_ci mmc_hostname(card->host), scr_struct); 2068c2ecf20Sopenharmony_ci return -EINVAL; 2078c2ecf20Sopenharmony_ci } 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4); 2108c2ecf20Sopenharmony_ci scr->bus_widths = UNSTUFF_BITS(resp, 48, 4); 2118c2ecf20Sopenharmony_ci if (scr->sda_vsn == SCR_SPEC_VER_2) 2128c2ecf20Sopenharmony_ci /* Check if Physical Layer Spec v3.0 is supported */ 2138c2ecf20Sopenharmony_ci scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1); 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci if (scr->sda_spec3) { 2168c2ecf20Sopenharmony_ci scr->sda_spec4 = UNSTUFF_BITS(resp, 42, 1); 2178c2ecf20Sopenharmony_ci scr->sda_specx = UNSTUFF_BITS(resp, 38, 4); 2188c2ecf20Sopenharmony_ci } 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci if (UNSTUFF_BITS(resp, 55, 1)) 2218c2ecf20Sopenharmony_ci card->erased_byte = 0xFF; 2228c2ecf20Sopenharmony_ci else 2238c2ecf20Sopenharmony_ci card->erased_byte = 0x0; 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci if (scr->sda_spec3) 2268c2ecf20Sopenharmony_ci scr->cmds = UNSTUFF_BITS(resp, 32, 2); 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci /* SD Spec says: any SD Card shall set at least bits 0 and 2 */ 2298c2ecf20Sopenharmony_ci if (!(scr->bus_widths & SD_SCR_BUS_WIDTH_1) || 2308c2ecf20Sopenharmony_ci !(scr->bus_widths & SD_SCR_BUS_WIDTH_4)) { 2318c2ecf20Sopenharmony_ci pr_err("%s: invalid bus width\n", mmc_hostname(card->host)); 2328c2ecf20Sopenharmony_ci return -EINVAL; 2338c2ecf20Sopenharmony_ci } 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci return 0; 2368c2ecf20Sopenharmony_ci} 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci/* 2398c2ecf20Sopenharmony_ci * Fetch and process SD Status register. 2408c2ecf20Sopenharmony_ci */ 2418c2ecf20Sopenharmony_cistatic int mmc_read_ssr(struct mmc_card *card) 2428c2ecf20Sopenharmony_ci{ 2438c2ecf20Sopenharmony_ci unsigned int au, es, et, eo; 2448c2ecf20Sopenharmony_ci __be32 *raw_ssr; 2458c2ecf20Sopenharmony_ci u32 resp[4] = {}; 2468c2ecf20Sopenharmony_ci u8 discard_support; 2478c2ecf20Sopenharmony_ci int i; 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci if (!(card->csd.cmdclass & CCC_APP_SPEC)) { 2508c2ecf20Sopenharmony_ci pr_warn("%s: card lacks mandatory SD Status function\n", 2518c2ecf20Sopenharmony_ci mmc_hostname(card->host)); 2528c2ecf20Sopenharmony_ci return 0; 2538c2ecf20Sopenharmony_ci } 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci raw_ssr = kmalloc(sizeof(card->raw_ssr), GFP_KERNEL); 2568c2ecf20Sopenharmony_ci if (!raw_ssr) 2578c2ecf20Sopenharmony_ci return -ENOMEM; 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci if (mmc_app_sd_status(card, raw_ssr)) { 2608c2ecf20Sopenharmony_ci pr_warn("%s: problem reading SD Status register\n", 2618c2ecf20Sopenharmony_ci mmc_hostname(card->host)); 2628c2ecf20Sopenharmony_ci kfree(raw_ssr); 2638c2ecf20Sopenharmony_ci return 0; 2648c2ecf20Sopenharmony_ci } 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_ci for (i = 0; i < 16; i++) 2678c2ecf20Sopenharmony_ci card->raw_ssr[i] = be32_to_cpu(raw_ssr[i]); 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci kfree(raw_ssr); 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_ci /* 2728c2ecf20Sopenharmony_ci * UNSTUFF_BITS only works with four u32s so we have to offset the 2738c2ecf20Sopenharmony_ci * bitfield positions accordingly. 2748c2ecf20Sopenharmony_ci */ 2758c2ecf20Sopenharmony_ci au = UNSTUFF_BITS(card->raw_ssr, 428 - 384, 4); 2768c2ecf20Sopenharmony_ci if (au) { 2778c2ecf20Sopenharmony_ci if (au <= 9 || card->scr.sda_spec3) { 2788c2ecf20Sopenharmony_ci card->ssr.au = sd_au_size[au]; 2798c2ecf20Sopenharmony_ci es = UNSTUFF_BITS(card->raw_ssr, 408 - 384, 16); 2808c2ecf20Sopenharmony_ci et = UNSTUFF_BITS(card->raw_ssr, 402 - 384, 6); 2818c2ecf20Sopenharmony_ci if (es && et) { 2828c2ecf20Sopenharmony_ci eo = UNSTUFF_BITS(card->raw_ssr, 400 - 384, 2); 2838c2ecf20Sopenharmony_ci card->ssr.erase_timeout = (et * 1000) / es; 2848c2ecf20Sopenharmony_ci card->ssr.erase_offset = eo * 1000; 2858c2ecf20Sopenharmony_ci } 2868c2ecf20Sopenharmony_ci } else { 2878c2ecf20Sopenharmony_ci pr_warn("%s: SD Status: Invalid Allocation Unit size\n", 2888c2ecf20Sopenharmony_ci mmc_hostname(card->host)); 2898c2ecf20Sopenharmony_ci } 2908c2ecf20Sopenharmony_ci } 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci /* 2938c2ecf20Sopenharmony_ci * starting SD5.1 discard is supported if DISCARD_SUPPORT (b313) is set 2948c2ecf20Sopenharmony_ci */ 2958c2ecf20Sopenharmony_ci resp[3] = card->raw_ssr[6]; 2968c2ecf20Sopenharmony_ci discard_support = UNSTUFF_BITS(resp, 313 - 288, 1); 2978c2ecf20Sopenharmony_ci card->erase_arg = (card->scr.sda_specx && discard_support) ? 2988c2ecf20Sopenharmony_ci SD_DISCARD_ARG : SD_ERASE_ARG; 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_ci return 0; 3018c2ecf20Sopenharmony_ci} 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci/* 3048c2ecf20Sopenharmony_ci * Fetches and decodes switch information 3058c2ecf20Sopenharmony_ci */ 3068c2ecf20Sopenharmony_cistatic int mmc_read_switch(struct mmc_card *card) 3078c2ecf20Sopenharmony_ci{ 3088c2ecf20Sopenharmony_ci int err; 3098c2ecf20Sopenharmony_ci u8 *status; 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci if (card->scr.sda_vsn < SCR_SPEC_VER_1) 3128c2ecf20Sopenharmony_ci return 0; 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci if (!(card->csd.cmdclass & CCC_SWITCH)) { 3158c2ecf20Sopenharmony_ci pr_warn("%s: card lacks mandatory switch function, performance might suffer\n", 3168c2ecf20Sopenharmony_ci mmc_hostname(card->host)); 3178c2ecf20Sopenharmony_ci return 0; 3188c2ecf20Sopenharmony_ci } 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci status = kmalloc(64, GFP_KERNEL); 3218c2ecf20Sopenharmony_ci if (!status) 3228c2ecf20Sopenharmony_ci return -ENOMEM; 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci /* 3258c2ecf20Sopenharmony_ci * Find out the card's support bits with a mode 0 operation. 3268c2ecf20Sopenharmony_ci * The argument does not matter, as the support bits do not 3278c2ecf20Sopenharmony_ci * change with the arguments. 3288c2ecf20Sopenharmony_ci */ 3298c2ecf20Sopenharmony_ci err = mmc_sd_switch(card, 0, 0, 0, status); 3308c2ecf20Sopenharmony_ci if (err) { 3318c2ecf20Sopenharmony_ci /* 3328c2ecf20Sopenharmony_ci * If the host or the card can't do the switch, 3338c2ecf20Sopenharmony_ci * fail more gracefully. 3348c2ecf20Sopenharmony_ci */ 3358c2ecf20Sopenharmony_ci if (err != -EINVAL && err != -ENOSYS && err != -EFAULT) 3368c2ecf20Sopenharmony_ci goto out; 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci pr_warn("%s: problem reading Bus Speed modes\n", 3398c2ecf20Sopenharmony_ci mmc_hostname(card->host)); 3408c2ecf20Sopenharmony_ci err = 0; 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_ci goto out; 3438c2ecf20Sopenharmony_ci } 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_ci if (status[13] & SD_MODE_HIGH_SPEED) 3468c2ecf20Sopenharmony_ci card->sw_caps.hs_max_dtr = HIGH_SPEED_MAX_DTR; 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_ci if (card->scr.sda_spec3) { 3498c2ecf20Sopenharmony_ci card->sw_caps.sd3_bus_mode = status[13]; 3508c2ecf20Sopenharmony_ci /* Driver Strengths supported by the card */ 3518c2ecf20Sopenharmony_ci card->sw_caps.sd3_drv_type = status[9]; 3528c2ecf20Sopenharmony_ci card->sw_caps.sd3_curr_limit = status[7] | status[6] << 8; 3538c2ecf20Sopenharmony_ci } 3548c2ecf20Sopenharmony_ci 3558c2ecf20Sopenharmony_ciout: 3568c2ecf20Sopenharmony_ci kfree(status); 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci return err; 3598c2ecf20Sopenharmony_ci} 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_ci/* 3628c2ecf20Sopenharmony_ci * Test if the card supports high-speed mode and, if so, switch to it. 3638c2ecf20Sopenharmony_ci */ 3648c2ecf20Sopenharmony_ciint mmc_sd_switch_hs(struct mmc_card *card) 3658c2ecf20Sopenharmony_ci{ 3668c2ecf20Sopenharmony_ci int err; 3678c2ecf20Sopenharmony_ci u8 *status; 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_ci if (card->scr.sda_vsn < SCR_SPEC_VER_1) 3708c2ecf20Sopenharmony_ci return 0; 3718c2ecf20Sopenharmony_ci 3728c2ecf20Sopenharmony_ci if (!(card->csd.cmdclass & CCC_SWITCH)) 3738c2ecf20Sopenharmony_ci return 0; 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_ci if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED)) 3768c2ecf20Sopenharmony_ci return 0; 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci if (card->sw_caps.hs_max_dtr == 0) 3798c2ecf20Sopenharmony_ci return 0; 3808c2ecf20Sopenharmony_ci 3818c2ecf20Sopenharmony_ci status = kmalloc(64, GFP_KERNEL); 3828c2ecf20Sopenharmony_ci if (!status) 3838c2ecf20Sopenharmony_ci return -ENOMEM; 3848c2ecf20Sopenharmony_ci 3858c2ecf20Sopenharmony_ci err = mmc_sd_switch(card, 1, 0, HIGH_SPEED_BUS_SPEED, status); 3868c2ecf20Sopenharmony_ci if (err) 3878c2ecf20Sopenharmony_ci goto out; 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ci if ((status[16] & 0xF) != HIGH_SPEED_BUS_SPEED) { 3908c2ecf20Sopenharmony_ci pr_warn("%s: Problem switching card into high-speed mode!\n", 3918c2ecf20Sopenharmony_ci mmc_hostname(card->host)); 3928c2ecf20Sopenharmony_ci err = 0; 3938c2ecf20Sopenharmony_ci } else { 3948c2ecf20Sopenharmony_ci err = 1; 3958c2ecf20Sopenharmony_ci } 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ciout: 3988c2ecf20Sopenharmony_ci kfree(status); 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_ci return err; 4018c2ecf20Sopenharmony_ci} 4028c2ecf20Sopenharmony_ci 4038c2ecf20Sopenharmony_cistatic int sd_select_driver_type(struct mmc_card *card, u8 *status) 4048c2ecf20Sopenharmony_ci{ 4058c2ecf20Sopenharmony_ci int card_drv_type, drive_strength, drv_type; 4068c2ecf20Sopenharmony_ci int err; 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci card->drive_strength = 0; 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_ci card_drv_type = card->sw_caps.sd3_drv_type | SD_DRIVER_TYPE_B; 4118c2ecf20Sopenharmony_ci 4128c2ecf20Sopenharmony_ci drive_strength = mmc_select_drive_strength(card, 4138c2ecf20Sopenharmony_ci card->sw_caps.uhs_max_dtr, 4148c2ecf20Sopenharmony_ci card_drv_type, &drv_type); 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci if (drive_strength) { 4178c2ecf20Sopenharmony_ci err = mmc_sd_switch(card, 1, 2, drive_strength, status); 4188c2ecf20Sopenharmony_ci if (err) 4198c2ecf20Sopenharmony_ci return err; 4208c2ecf20Sopenharmony_ci if ((status[15] & 0xF) != drive_strength) { 4218c2ecf20Sopenharmony_ci pr_warn("%s: Problem setting drive strength!\n", 4228c2ecf20Sopenharmony_ci mmc_hostname(card->host)); 4238c2ecf20Sopenharmony_ci return 0; 4248c2ecf20Sopenharmony_ci } 4258c2ecf20Sopenharmony_ci card->drive_strength = drive_strength; 4268c2ecf20Sopenharmony_ci } 4278c2ecf20Sopenharmony_ci 4288c2ecf20Sopenharmony_ci if (drv_type) 4298c2ecf20Sopenharmony_ci mmc_set_driver_type(card->host, drv_type); 4308c2ecf20Sopenharmony_ci 4318c2ecf20Sopenharmony_ci return 0; 4328c2ecf20Sopenharmony_ci} 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_cistatic void sd_update_bus_speed_mode(struct mmc_card *card) 4358c2ecf20Sopenharmony_ci{ 4368c2ecf20Sopenharmony_ci /* 4378c2ecf20Sopenharmony_ci * If the host doesn't support any of the UHS-I modes, fallback on 4388c2ecf20Sopenharmony_ci * default speed. 4398c2ecf20Sopenharmony_ci */ 4408c2ecf20Sopenharmony_ci if (!mmc_host_uhs(card->host)) { 4418c2ecf20Sopenharmony_ci card->sd_bus_speed = 0; 4428c2ecf20Sopenharmony_ci return; 4438c2ecf20Sopenharmony_ci } 4448c2ecf20Sopenharmony_ci 4458c2ecf20Sopenharmony_ci if ((card->host->caps & MMC_CAP_UHS_SDR104) && 4468c2ecf20Sopenharmony_ci (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) { 4478c2ecf20Sopenharmony_ci card->sd_bus_speed = UHS_SDR104_BUS_SPEED; 4488c2ecf20Sopenharmony_ci } else if ((card->host->caps & MMC_CAP_UHS_DDR50) && 4498c2ecf20Sopenharmony_ci (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) { 4508c2ecf20Sopenharmony_ci card->sd_bus_speed = UHS_DDR50_BUS_SPEED; 4518c2ecf20Sopenharmony_ci } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 | 4528c2ecf20Sopenharmony_ci MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode & 4538c2ecf20Sopenharmony_ci SD_MODE_UHS_SDR50)) { 4548c2ecf20Sopenharmony_ci card->sd_bus_speed = UHS_SDR50_BUS_SPEED; 4558c2ecf20Sopenharmony_ci } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 | 4568c2ecf20Sopenharmony_ci MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) && 4578c2ecf20Sopenharmony_ci (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) { 4588c2ecf20Sopenharmony_ci card->sd_bus_speed = UHS_SDR25_BUS_SPEED; 4598c2ecf20Sopenharmony_ci } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 | 4608c2ecf20Sopenharmony_ci MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 | 4618c2ecf20Sopenharmony_ci MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode & 4628c2ecf20Sopenharmony_ci SD_MODE_UHS_SDR12)) { 4638c2ecf20Sopenharmony_ci card->sd_bus_speed = UHS_SDR12_BUS_SPEED; 4648c2ecf20Sopenharmony_ci } 4658c2ecf20Sopenharmony_ci} 4668c2ecf20Sopenharmony_ci 4678c2ecf20Sopenharmony_cistatic int sd_set_bus_speed_mode(struct mmc_card *card, u8 *status) 4688c2ecf20Sopenharmony_ci{ 4698c2ecf20Sopenharmony_ci int err; 4708c2ecf20Sopenharmony_ci unsigned int timing = 0; 4718c2ecf20Sopenharmony_ci 4728c2ecf20Sopenharmony_ci switch (card->sd_bus_speed) { 4738c2ecf20Sopenharmony_ci case UHS_SDR104_BUS_SPEED: 4748c2ecf20Sopenharmony_ci timing = MMC_TIMING_UHS_SDR104; 4758c2ecf20Sopenharmony_ci card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR; 4768c2ecf20Sopenharmony_ci break; 4778c2ecf20Sopenharmony_ci case UHS_DDR50_BUS_SPEED: 4788c2ecf20Sopenharmony_ci timing = MMC_TIMING_UHS_DDR50; 4798c2ecf20Sopenharmony_ci card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR; 4808c2ecf20Sopenharmony_ci break; 4818c2ecf20Sopenharmony_ci case UHS_SDR50_BUS_SPEED: 4828c2ecf20Sopenharmony_ci timing = MMC_TIMING_UHS_SDR50; 4838c2ecf20Sopenharmony_ci card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR; 4848c2ecf20Sopenharmony_ci break; 4858c2ecf20Sopenharmony_ci case UHS_SDR25_BUS_SPEED: 4868c2ecf20Sopenharmony_ci timing = MMC_TIMING_UHS_SDR25; 4878c2ecf20Sopenharmony_ci card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR; 4888c2ecf20Sopenharmony_ci break; 4898c2ecf20Sopenharmony_ci case UHS_SDR12_BUS_SPEED: 4908c2ecf20Sopenharmony_ci timing = MMC_TIMING_UHS_SDR12; 4918c2ecf20Sopenharmony_ci card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR; 4928c2ecf20Sopenharmony_ci break; 4938c2ecf20Sopenharmony_ci default: 4948c2ecf20Sopenharmony_ci return 0; 4958c2ecf20Sopenharmony_ci } 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_ci err = mmc_sd_switch(card, 1, 0, card->sd_bus_speed, status); 4988c2ecf20Sopenharmony_ci if (err) 4998c2ecf20Sopenharmony_ci return err; 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_ci if ((status[16] & 0xF) != card->sd_bus_speed) 5028c2ecf20Sopenharmony_ci pr_warn("%s: Problem setting bus speed mode!\n", 5038c2ecf20Sopenharmony_ci mmc_hostname(card->host)); 5048c2ecf20Sopenharmony_ci else { 5058c2ecf20Sopenharmony_ci mmc_set_timing(card->host, timing); 5068c2ecf20Sopenharmony_ci mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr); 5078c2ecf20Sopenharmony_ci } 5088c2ecf20Sopenharmony_ci 5098c2ecf20Sopenharmony_ci return 0; 5108c2ecf20Sopenharmony_ci} 5118c2ecf20Sopenharmony_ci 5128c2ecf20Sopenharmony_ci/* Get host's max current setting at its current voltage */ 5138c2ecf20Sopenharmony_cistatic u32 sd_get_host_max_current(struct mmc_host *host) 5148c2ecf20Sopenharmony_ci{ 5158c2ecf20Sopenharmony_ci u32 voltage, max_current; 5168c2ecf20Sopenharmony_ci 5178c2ecf20Sopenharmony_ci voltage = 1 << host->ios.vdd; 5188c2ecf20Sopenharmony_ci switch (voltage) { 5198c2ecf20Sopenharmony_ci case MMC_VDD_165_195: 5208c2ecf20Sopenharmony_ci max_current = host->max_current_180; 5218c2ecf20Sopenharmony_ci break; 5228c2ecf20Sopenharmony_ci case MMC_VDD_29_30: 5238c2ecf20Sopenharmony_ci case MMC_VDD_30_31: 5248c2ecf20Sopenharmony_ci max_current = host->max_current_300; 5258c2ecf20Sopenharmony_ci break; 5268c2ecf20Sopenharmony_ci case MMC_VDD_32_33: 5278c2ecf20Sopenharmony_ci case MMC_VDD_33_34: 5288c2ecf20Sopenharmony_ci max_current = host->max_current_330; 5298c2ecf20Sopenharmony_ci break; 5308c2ecf20Sopenharmony_ci default: 5318c2ecf20Sopenharmony_ci max_current = 0; 5328c2ecf20Sopenharmony_ci } 5338c2ecf20Sopenharmony_ci 5348c2ecf20Sopenharmony_ci return max_current; 5358c2ecf20Sopenharmony_ci} 5368c2ecf20Sopenharmony_ci 5378c2ecf20Sopenharmony_cistatic int sd_set_current_limit(struct mmc_card *card, u8 *status) 5388c2ecf20Sopenharmony_ci{ 5398c2ecf20Sopenharmony_ci int current_limit = SD_SET_CURRENT_NO_CHANGE; 5408c2ecf20Sopenharmony_ci int err; 5418c2ecf20Sopenharmony_ci u32 max_current; 5428c2ecf20Sopenharmony_ci 5438c2ecf20Sopenharmony_ci /* 5448c2ecf20Sopenharmony_ci * Current limit switch is only defined for SDR50, SDR104, and DDR50 5458c2ecf20Sopenharmony_ci * bus speed modes. For other bus speed modes, we do not change the 5468c2ecf20Sopenharmony_ci * current limit. 5478c2ecf20Sopenharmony_ci */ 5488c2ecf20Sopenharmony_ci if ((card->sd_bus_speed != UHS_SDR50_BUS_SPEED) && 5498c2ecf20Sopenharmony_ci (card->sd_bus_speed != UHS_SDR104_BUS_SPEED) && 5508c2ecf20Sopenharmony_ci (card->sd_bus_speed != UHS_DDR50_BUS_SPEED)) 5518c2ecf20Sopenharmony_ci return 0; 5528c2ecf20Sopenharmony_ci 5538c2ecf20Sopenharmony_ci /* 5548c2ecf20Sopenharmony_ci * Host has different current capabilities when operating at 5558c2ecf20Sopenharmony_ci * different voltages, so find out its max current first. 5568c2ecf20Sopenharmony_ci */ 5578c2ecf20Sopenharmony_ci max_current = sd_get_host_max_current(card->host); 5588c2ecf20Sopenharmony_ci 5598c2ecf20Sopenharmony_ci /* 5608c2ecf20Sopenharmony_ci * We only check host's capability here, if we set a limit that is 5618c2ecf20Sopenharmony_ci * higher than the card's maximum current, the card will be using its 5628c2ecf20Sopenharmony_ci * maximum current, e.g. if the card's maximum current is 300ma, and 5638c2ecf20Sopenharmony_ci * when we set current limit to 200ma, the card will draw 200ma, and 5648c2ecf20Sopenharmony_ci * when we set current limit to 400/600/800ma, the card will draw its 5658c2ecf20Sopenharmony_ci * maximum 300ma from the host. 5668c2ecf20Sopenharmony_ci * 5678c2ecf20Sopenharmony_ci * The above is incorrect: if we try to set a current limit that is 5688c2ecf20Sopenharmony_ci * not supported by the card, the card can rightfully error out the 5698c2ecf20Sopenharmony_ci * attempt, and remain at the default current limit. This results 5708c2ecf20Sopenharmony_ci * in a 300mA card being limited to 200mA even though the host 5718c2ecf20Sopenharmony_ci * supports 800mA. Failures seen with SanDisk 8GB UHS cards with 5728c2ecf20Sopenharmony_ci * an iMX6 host. --rmk 5738c2ecf20Sopenharmony_ci */ 5748c2ecf20Sopenharmony_ci if (max_current >= 800 && 5758c2ecf20Sopenharmony_ci card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_800) 5768c2ecf20Sopenharmony_ci current_limit = SD_SET_CURRENT_LIMIT_800; 5778c2ecf20Sopenharmony_ci else if (max_current >= 600 && 5788c2ecf20Sopenharmony_ci card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_600) 5798c2ecf20Sopenharmony_ci current_limit = SD_SET_CURRENT_LIMIT_600; 5808c2ecf20Sopenharmony_ci else if (max_current >= 400 && 5818c2ecf20Sopenharmony_ci card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_400) 5828c2ecf20Sopenharmony_ci current_limit = SD_SET_CURRENT_LIMIT_400; 5838c2ecf20Sopenharmony_ci else if (max_current >= 200 && 5848c2ecf20Sopenharmony_ci card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_200) 5858c2ecf20Sopenharmony_ci current_limit = SD_SET_CURRENT_LIMIT_200; 5868c2ecf20Sopenharmony_ci 5878c2ecf20Sopenharmony_ci if (current_limit != SD_SET_CURRENT_NO_CHANGE) { 5888c2ecf20Sopenharmony_ci err = mmc_sd_switch(card, 1, 3, current_limit, status); 5898c2ecf20Sopenharmony_ci if (err) 5908c2ecf20Sopenharmony_ci return err; 5918c2ecf20Sopenharmony_ci 5928c2ecf20Sopenharmony_ci if (((status[15] >> 4) & 0x0F) != current_limit) 5938c2ecf20Sopenharmony_ci pr_warn("%s: Problem setting current limit!\n", 5948c2ecf20Sopenharmony_ci mmc_hostname(card->host)); 5958c2ecf20Sopenharmony_ci 5968c2ecf20Sopenharmony_ci } 5978c2ecf20Sopenharmony_ci 5988c2ecf20Sopenharmony_ci return 0; 5998c2ecf20Sopenharmony_ci} 6008c2ecf20Sopenharmony_ci 6018c2ecf20Sopenharmony_ci/* 6028c2ecf20Sopenharmony_ci * UHS-I specific initialization procedure 6038c2ecf20Sopenharmony_ci */ 6048c2ecf20Sopenharmony_cistatic int mmc_sd_init_uhs_card(struct mmc_card *card) 6058c2ecf20Sopenharmony_ci{ 6068c2ecf20Sopenharmony_ci int err; 6078c2ecf20Sopenharmony_ci u8 *status; 6088c2ecf20Sopenharmony_ci 6098c2ecf20Sopenharmony_ci if (!(card->csd.cmdclass & CCC_SWITCH)) 6108c2ecf20Sopenharmony_ci return 0; 6118c2ecf20Sopenharmony_ci 6128c2ecf20Sopenharmony_ci status = kmalloc(64, GFP_KERNEL); 6138c2ecf20Sopenharmony_ci if (!status) 6148c2ecf20Sopenharmony_ci return -ENOMEM; 6158c2ecf20Sopenharmony_ci 6168c2ecf20Sopenharmony_ci /* Set 4-bit bus width */ 6178c2ecf20Sopenharmony_ci err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4); 6188c2ecf20Sopenharmony_ci if (err) 6198c2ecf20Sopenharmony_ci goto out; 6208c2ecf20Sopenharmony_ci 6218c2ecf20Sopenharmony_ci mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4); 6228c2ecf20Sopenharmony_ci 6238c2ecf20Sopenharmony_ci /* 6248c2ecf20Sopenharmony_ci * Select the bus speed mode depending on host 6258c2ecf20Sopenharmony_ci * and card capability. 6268c2ecf20Sopenharmony_ci */ 6278c2ecf20Sopenharmony_ci sd_update_bus_speed_mode(card); 6288c2ecf20Sopenharmony_ci 6298c2ecf20Sopenharmony_ci /* Set the driver strength for the card */ 6308c2ecf20Sopenharmony_ci err = sd_select_driver_type(card, status); 6318c2ecf20Sopenharmony_ci if (err) 6328c2ecf20Sopenharmony_ci goto out; 6338c2ecf20Sopenharmony_ci 6348c2ecf20Sopenharmony_ci /* Set current limit for the card */ 6358c2ecf20Sopenharmony_ci err = sd_set_current_limit(card, status); 6368c2ecf20Sopenharmony_ci if (err) 6378c2ecf20Sopenharmony_ci goto out; 6388c2ecf20Sopenharmony_ci 6398c2ecf20Sopenharmony_ci /* Set bus speed mode of the card */ 6408c2ecf20Sopenharmony_ci err = sd_set_bus_speed_mode(card, status); 6418c2ecf20Sopenharmony_ci if (err) 6428c2ecf20Sopenharmony_ci goto out; 6438c2ecf20Sopenharmony_ci 6448c2ecf20Sopenharmony_ci /* 6458c2ecf20Sopenharmony_ci * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and 6468c2ecf20Sopenharmony_ci * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104. 6478c2ecf20Sopenharmony_ci */ 6488c2ecf20Sopenharmony_ci if (!mmc_host_is_spi(card->host) && 6498c2ecf20Sopenharmony_ci (card->host->ios.timing == MMC_TIMING_UHS_SDR50 || 6508c2ecf20Sopenharmony_ci card->host->ios.timing == MMC_TIMING_UHS_DDR50 || 6518c2ecf20Sopenharmony_ci card->host->ios.timing == MMC_TIMING_UHS_SDR104)) { 6528c2ecf20Sopenharmony_ci err = mmc_execute_tuning(card); 6538c2ecf20Sopenharmony_ci 6548c2ecf20Sopenharmony_ci /* 6558c2ecf20Sopenharmony_ci * As SD Specifications Part1 Physical Layer Specification 6568c2ecf20Sopenharmony_ci * Version 3.01 says, CMD19 tuning is available for unlocked 6578c2ecf20Sopenharmony_ci * cards in transfer state of 1.8V signaling mode. The small 6588c2ecf20Sopenharmony_ci * difference between v3.00 and 3.01 spec means that CMD19 6598c2ecf20Sopenharmony_ci * tuning is also available for DDR50 mode. 6608c2ecf20Sopenharmony_ci */ 6618c2ecf20Sopenharmony_ci if (err && card->host->ios.timing == MMC_TIMING_UHS_DDR50) { 6628c2ecf20Sopenharmony_ci pr_warn("%s: ddr50 tuning failed\n", 6638c2ecf20Sopenharmony_ci mmc_hostname(card->host)); 6648c2ecf20Sopenharmony_ci err = 0; 6658c2ecf20Sopenharmony_ci } 6668c2ecf20Sopenharmony_ci } 6678c2ecf20Sopenharmony_ci 6688c2ecf20Sopenharmony_ciout: 6698c2ecf20Sopenharmony_ci kfree(status); 6708c2ecf20Sopenharmony_ci 6718c2ecf20Sopenharmony_ci return err; 6728c2ecf20Sopenharmony_ci} 6738c2ecf20Sopenharmony_ci 6748c2ecf20Sopenharmony_ciMMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1], 6758c2ecf20Sopenharmony_ci card->raw_cid[2], card->raw_cid[3]); 6768c2ecf20Sopenharmony_ciMMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1], 6778c2ecf20Sopenharmony_ci card->raw_csd[2], card->raw_csd[3]); 6788c2ecf20Sopenharmony_ciMMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]); 6798c2ecf20Sopenharmony_ciMMC_DEV_ATTR(ssr, 6808c2ecf20Sopenharmony_ci "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x\n", 6818c2ecf20Sopenharmony_ci card->raw_ssr[0], card->raw_ssr[1], card->raw_ssr[2], 6828c2ecf20Sopenharmony_ci card->raw_ssr[3], card->raw_ssr[4], card->raw_ssr[5], 6838c2ecf20Sopenharmony_ci card->raw_ssr[6], card->raw_ssr[7], card->raw_ssr[8], 6848c2ecf20Sopenharmony_ci card->raw_ssr[9], card->raw_ssr[10], card->raw_ssr[11], 6858c2ecf20Sopenharmony_ci card->raw_ssr[12], card->raw_ssr[13], card->raw_ssr[14], 6868c2ecf20Sopenharmony_ci card->raw_ssr[15]); 6878c2ecf20Sopenharmony_ciMMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year); 6888c2ecf20Sopenharmony_ciMMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9); 6898c2ecf20Sopenharmony_ciMMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9); 6908c2ecf20Sopenharmony_ciMMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev); 6918c2ecf20Sopenharmony_ciMMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev); 6928c2ecf20Sopenharmony_ciMMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid); 6938c2ecf20Sopenharmony_ciMMC_DEV_ATTR(name, "%s\n", card->cid.prod_name); 6948c2ecf20Sopenharmony_ciMMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid); 6958c2ecf20Sopenharmony_ciMMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial); 6968c2ecf20Sopenharmony_ciMMC_DEV_ATTR(ocr, "0x%08x\n", card->ocr); 6978c2ecf20Sopenharmony_ciMMC_DEV_ATTR(rca, "0x%04x\n", card->rca); 6988c2ecf20Sopenharmony_ci 6998c2ecf20Sopenharmony_ci 7008c2ecf20Sopenharmony_cistatic ssize_t mmc_dsr_show(struct device *dev, 7018c2ecf20Sopenharmony_ci struct device_attribute *attr, 7028c2ecf20Sopenharmony_ci char *buf) 7038c2ecf20Sopenharmony_ci{ 7048c2ecf20Sopenharmony_ci struct mmc_card *card = mmc_dev_to_card(dev); 7058c2ecf20Sopenharmony_ci struct mmc_host *host = card->host; 7068c2ecf20Sopenharmony_ci 7078c2ecf20Sopenharmony_ci if (card->csd.dsr_imp && host->dsr_req) 7088c2ecf20Sopenharmony_ci return sprintf(buf, "0x%x\n", host->dsr); 7098c2ecf20Sopenharmony_ci else 7108c2ecf20Sopenharmony_ci /* return default DSR value */ 7118c2ecf20Sopenharmony_ci return sprintf(buf, "0x%x\n", 0x404); 7128c2ecf20Sopenharmony_ci} 7138c2ecf20Sopenharmony_ci 7148c2ecf20Sopenharmony_cistatic DEVICE_ATTR(dsr, S_IRUGO, mmc_dsr_show, NULL); 7158c2ecf20Sopenharmony_ci 7168c2ecf20Sopenharmony_ciMMC_DEV_ATTR(vendor, "0x%04x\n", card->cis.vendor); 7178c2ecf20Sopenharmony_ciMMC_DEV_ATTR(device, "0x%04x\n", card->cis.device); 7188c2ecf20Sopenharmony_ciMMC_DEV_ATTR(revision, "%u.%u\n", card->major_rev, card->minor_rev); 7198c2ecf20Sopenharmony_ci 7208c2ecf20Sopenharmony_ci#define sdio_info_attr(num) \ 7218c2ecf20Sopenharmony_cistatic ssize_t info##num##_show(struct device *dev, struct device_attribute *attr, char *buf) \ 7228c2ecf20Sopenharmony_ci{ \ 7238c2ecf20Sopenharmony_ci struct mmc_card *card = mmc_dev_to_card(dev); \ 7248c2ecf20Sopenharmony_ci \ 7258c2ecf20Sopenharmony_ci if (num > card->num_info) \ 7268c2ecf20Sopenharmony_ci return -ENODATA; \ 7278c2ecf20Sopenharmony_ci if (!card->info[num-1][0]) \ 7288c2ecf20Sopenharmony_ci return 0; \ 7298c2ecf20Sopenharmony_ci return sprintf(buf, "%s\n", card->info[num-1]); \ 7308c2ecf20Sopenharmony_ci} \ 7318c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(info##num) 7328c2ecf20Sopenharmony_ci 7338c2ecf20Sopenharmony_cisdio_info_attr(1); 7348c2ecf20Sopenharmony_cisdio_info_attr(2); 7358c2ecf20Sopenharmony_cisdio_info_attr(3); 7368c2ecf20Sopenharmony_cisdio_info_attr(4); 7378c2ecf20Sopenharmony_ci 7388c2ecf20Sopenharmony_cistatic struct attribute *sd_std_attrs[] = { 7398c2ecf20Sopenharmony_ci &dev_attr_vendor.attr, 7408c2ecf20Sopenharmony_ci &dev_attr_device.attr, 7418c2ecf20Sopenharmony_ci &dev_attr_revision.attr, 7428c2ecf20Sopenharmony_ci &dev_attr_info1.attr, 7438c2ecf20Sopenharmony_ci &dev_attr_info2.attr, 7448c2ecf20Sopenharmony_ci &dev_attr_info3.attr, 7458c2ecf20Sopenharmony_ci &dev_attr_info4.attr, 7468c2ecf20Sopenharmony_ci &dev_attr_cid.attr, 7478c2ecf20Sopenharmony_ci &dev_attr_csd.attr, 7488c2ecf20Sopenharmony_ci &dev_attr_scr.attr, 7498c2ecf20Sopenharmony_ci &dev_attr_ssr.attr, 7508c2ecf20Sopenharmony_ci &dev_attr_date.attr, 7518c2ecf20Sopenharmony_ci &dev_attr_erase_size.attr, 7528c2ecf20Sopenharmony_ci &dev_attr_preferred_erase_size.attr, 7538c2ecf20Sopenharmony_ci &dev_attr_fwrev.attr, 7548c2ecf20Sopenharmony_ci &dev_attr_hwrev.attr, 7558c2ecf20Sopenharmony_ci &dev_attr_manfid.attr, 7568c2ecf20Sopenharmony_ci &dev_attr_name.attr, 7578c2ecf20Sopenharmony_ci &dev_attr_oemid.attr, 7588c2ecf20Sopenharmony_ci &dev_attr_serial.attr, 7598c2ecf20Sopenharmony_ci &dev_attr_ocr.attr, 7608c2ecf20Sopenharmony_ci &dev_attr_rca.attr, 7618c2ecf20Sopenharmony_ci &dev_attr_dsr.attr, 7628c2ecf20Sopenharmony_ci NULL, 7638c2ecf20Sopenharmony_ci}; 7648c2ecf20Sopenharmony_ci 7658c2ecf20Sopenharmony_cistatic umode_t sd_std_is_visible(struct kobject *kobj, struct attribute *attr, 7668c2ecf20Sopenharmony_ci int index) 7678c2ecf20Sopenharmony_ci{ 7688c2ecf20Sopenharmony_ci struct device *dev = kobj_to_dev(kobj); 7698c2ecf20Sopenharmony_ci struct mmc_card *card = mmc_dev_to_card(dev); 7708c2ecf20Sopenharmony_ci 7718c2ecf20Sopenharmony_ci /* CIS vendor and device ids, revision and info string are available only for Combo cards */ 7728c2ecf20Sopenharmony_ci if ((attr == &dev_attr_vendor.attr || 7738c2ecf20Sopenharmony_ci attr == &dev_attr_device.attr || 7748c2ecf20Sopenharmony_ci attr == &dev_attr_revision.attr || 7758c2ecf20Sopenharmony_ci attr == &dev_attr_info1.attr || 7768c2ecf20Sopenharmony_ci attr == &dev_attr_info2.attr || 7778c2ecf20Sopenharmony_ci attr == &dev_attr_info3.attr || 7788c2ecf20Sopenharmony_ci attr == &dev_attr_info4.attr 7798c2ecf20Sopenharmony_ci ) && card->type != MMC_TYPE_SD_COMBO) 7808c2ecf20Sopenharmony_ci return 0; 7818c2ecf20Sopenharmony_ci 7828c2ecf20Sopenharmony_ci return attr->mode; 7838c2ecf20Sopenharmony_ci} 7848c2ecf20Sopenharmony_ci 7858c2ecf20Sopenharmony_cistatic const struct attribute_group sd_std_group = { 7868c2ecf20Sopenharmony_ci .attrs = sd_std_attrs, 7878c2ecf20Sopenharmony_ci .is_visible = sd_std_is_visible, 7888c2ecf20Sopenharmony_ci}; 7898c2ecf20Sopenharmony_ci__ATTRIBUTE_GROUPS(sd_std); 7908c2ecf20Sopenharmony_ci 7918c2ecf20Sopenharmony_cistruct device_type sd_type = { 7928c2ecf20Sopenharmony_ci .groups = sd_std_groups, 7938c2ecf20Sopenharmony_ci}; 7948c2ecf20Sopenharmony_ci 7958c2ecf20Sopenharmony_ci/* 7968c2ecf20Sopenharmony_ci * Fetch CID from card. 7978c2ecf20Sopenharmony_ci */ 7988c2ecf20Sopenharmony_ciint mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr) 7998c2ecf20Sopenharmony_ci{ 8008c2ecf20Sopenharmony_ci int err; 8018c2ecf20Sopenharmony_ci u32 max_current; 8028c2ecf20Sopenharmony_ci int retries = 10; 8038c2ecf20Sopenharmony_ci u32 pocr = ocr; 8048c2ecf20Sopenharmony_ci 8058c2ecf20Sopenharmony_citry_again: 8068c2ecf20Sopenharmony_ci if (!retries) { 8078c2ecf20Sopenharmony_ci ocr &= ~SD_OCR_S18R; 8088c2ecf20Sopenharmony_ci pr_warn("%s: Skipping voltage switch\n", mmc_hostname(host)); 8098c2ecf20Sopenharmony_ci } 8108c2ecf20Sopenharmony_ci 8118c2ecf20Sopenharmony_ci /* 8128c2ecf20Sopenharmony_ci * Since we're changing the OCR value, we seem to 8138c2ecf20Sopenharmony_ci * need to tell some cards to go back to the idle 8148c2ecf20Sopenharmony_ci * state. We wait 1ms to give cards time to 8158c2ecf20Sopenharmony_ci * respond. 8168c2ecf20Sopenharmony_ci */ 8178c2ecf20Sopenharmony_ci mmc_go_idle(host); 8188c2ecf20Sopenharmony_ci 8198c2ecf20Sopenharmony_ci /* 8208c2ecf20Sopenharmony_ci * If SD_SEND_IF_COND indicates an SD 2.0 8218c2ecf20Sopenharmony_ci * compliant card and we should set bit 30 8228c2ecf20Sopenharmony_ci * of the ocr to indicate that we can handle 8238c2ecf20Sopenharmony_ci * block-addressed SDHC cards. 8248c2ecf20Sopenharmony_ci */ 8258c2ecf20Sopenharmony_ci err = mmc_send_if_cond(host, ocr); 8268c2ecf20Sopenharmony_ci if (!err) 8278c2ecf20Sopenharmony_ci ocr |= SD_OCR_CCS; 8288c2ecf20Sopenharmony_ci 8298c2ecf20Sopenharmony_ci /* 8308c2ecf20Sopenharmony_ci * If the host supports one of UHS-I modes, request the card 8318c2ecf20Sopenharmony_ci * to switch to 1.8V signaling level. If the card has failed 8328c2ecf20Sopenharmony_ci * repeatedly to switch however, skip this. 8338c2ecf20Sopenharmony_ci */ 8348c2ecf20Sopenharmony_ci if (retries && mmc_host_uhs(host)) 8358c2ecf20Sopenharmony_ci ocr |= SD_OCR_S18R; 8368c2ecf20Sopenharmony_ci 8378c2ecf20Sopenharmony_ci /* 8388c2ecf20Sopenharmony_ci * If the host can supply more than 150mA at current voltage, 8398c2ecf20Sopenharmony_ci * XPC should be set to 1. 8408c2ecf20Sopenharmony_ci */ 8418c2ecf20Sopenharmony_ci max_current = sd_get_host_max_current(host); 8428c2ecf20Sopenharmony_ci if (max_current > 150) 8438c2ecf20Sopenharmony_ci ocr |= SD_OCR_XPC; 8448c2ecf20Sopenharmony_ci 8458c2ecf20Sopenharmony_ci err = mmc_send_app_op_cond(host, ocr, rocr); 8468c2ecf20Sopenharmony_ci if (err) 8478c2ecf20Sopenharmony_ci return err; 8488c2ecf20Sopenharmony_ci 8498c2ecf20Sopenharmony_ci /* 8508c2ecf20Sopenharmony_ci * In case the S18A bit is set in the response, let's start the signal 8518c2ecf20Sopenharmony_ci * voltage switch procedure. SPI mode doesn't support CMD11. 8528c2ecf20Sopenharmony_ci * Note that, according to the spec, the S18A bit is not valid unless 8538c2ecf20Sopenharmony_ci * the CCS bit is set as well. We deliberately deviate from the spec in 8548c2ecf20Sopenharmony_ci * regards to this, which allows UHS-I to be supported for SDSC cards. 8558c2ecf20Sopenharmony_ci */ 8568c2ecf20Sopenharmony_ci if (!mmc_host_is_spi(host) && (ocr & SD_OCR_S18R) && 8578c2ecf20Sopenharmony_ci rocr && (*rocr & SD_ROCR_S18A)) { 8588c2ecf20Sopenharmony_ci err = mmc_set_uhs_voltage(host, pocr); 8598c2ecf20Sopenharmony_ci if (err == -EAGAIN) { 8608c2ecf20Sopenharmony_ci retries--; 8618c2ecf20Sopenharmony_ci goto try_again; 8628c2ecf20Sopenharmony_ci } else if (err) { 8638c2ecf20Sopenharmony_ci retries = 0; 8648c2ecf20Sopenharmony_ci goto try_again; 8658c2ecf20Sopenharmony_ci } 8668c2ecf20Sopenharmony_ci } 8678c2ecf20Sopenharmony_ci 8688c2ecf20Sopenharmony_ci err = mmc_send_cid(host, cid); 8698c2ecf20Sopenharmony_ci return err; 8708c2ecf20Sopenharmony_ci} 8718c2ecf20Sopenharmony_ci 8728c2ecf20Sopenharmony_ciint mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card) 8738c2ecf20Sopenharmony_ci{ 8748c2ecf20Sopenharmony_ci int err; 8758c2ecf20Sopenharmony_ci 8768c2ecf20Sopenharmony_ci /* 8778c2ecf20Sopenharmony_ci * Fetch CSD from card. 8788c2ecf20Sopenharmony_ci */ 8798c2ecf20Sopenharmony_ci err = mmc_send_csd(card, card->raw_csd); 8808c2ecf20Sopenharmony_ci if (err) 8818c2ecf20Sopenharmony_ci return err; 8828c2ecf20Sopenharmony_ci 8838c2ecf20Sopenharmony_ci err = mmc_decode_csd(card); 8848c2ecf20Sopenharmony_ci if (err) 8858c2ecf20Sopenharmony_ci return err; 8868c2ecf20Sopenharmony_ci 8878c2ecf20Sopenharmony_ci return 0; 8888c2ecf20Sopenharmony_ci} 8898c2ecf20Sopenharmony_ci 8908c2ecf20Sopenharmony_cistatic int mmc_sd_get_ro(struct mmc_host *host) 8918c2ecf20Sopenharmony_ci{ 8928c2ecf20Sopenharmony_ci int ro; 8938c2ecf20Sopenharmony_ci 8948c2ecf20Sopenharmony_ci /* 8958c2ecf20Sopenharmony_ci * Some systems don't feature a write-protect pin and don't need one. 8968c2ecf20Sopenharmony_ci * E.g. because they only have micro-SD card slot. For those systems 8978c2ecf20Sopenharmony_ci * assume that the SD card is always read-write. 8988c2ecf20Sopenharmony_ci */ 8998c2ecf20Sopenharmony_ci if (host->caps2 & MMC_CAP2_NO_WRITE_PROTECT) 9008c2ecf20Sopenharmony_ci return 0; 9018c2ecf20Sopenharmony_ci 9028c2ecf20Sopenharmony_ci if (!host->ops->get_ro) 9038c2ecf20Sopenharmony_ci return -1; 9048c2ecf20Sopenharmony_ci 9058c2ecf20Sopenharmony_ci ro = host->ops->get_ro(host); 9068c2ecf20Sopenharmony_ci 9078c2ecf20Sopenharmony_ci return ro; 9088c2ecf20Sopenharmony_ci} 9098c2ecf20Sopenharmony_ci 9108c2ecf20Sopenharmony_ciint mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card, 9118c2ecf20Sopenharmony_ci bool reinit) 9128c2ecf20Sopenharmony_ci{ 9138c2ecf20Sopenharmony_ci int err; 9148c2ecf20Sopenharmony_ci 9158c2ecf20Sopenharmony_ci if (!reinit) { 9168c2ecf20Sopenharmony_ci /* 9178c2ecf20Sopenharmony_ci * Fetch SCR from card. 9188c2ecf20Sopenharmony_ci */ 9198c2ecf20Sopenharmony_ci err = mmc_app_send_scr(card); 9208c2ecf20Sopenharmony_ci if (err) 9218c2ecf20Sopenharmony_ci return err; 9228c2ecf20Sopenharmony_ci 9238c2ecf20Sopenharmony_ci err = mmc_decode_scr(card); 9248c2ecf20Sopenharmony_ci if (err) 9258c2ecf20Sopenharmony_ci return err; 9268c2ecf20Sopenharmony_ci 9278c2ecf20Sopenharmony_ci /* 9288c2ecf20Sopenharmony_ci * Fetch and process SD Status register. 9298c2ecf20Sopenharmony_ci */ 9308c2ecf20Sopenharmony_ci err = mmc_read_ssr(card); 9318c2ecf20Sopenharmony_ci if (err) 9328c2ecf20Sopenharmony_ci return err; 9338c2ecf20Sopenharmony_ci 9348c2ecf20Sopenharmony_ci /* Erase init depends on CSD and SSR */ 9358c2ecf20Sopenharmony_ci mmc_init_erase(card); 9368c2ecf20Sopenharmony_ci } 9378c2ecf20Sopenharmony_ci 9388c2ecf20Sopenharmony_ci /* 9398c2ecf20Sopenharmony_ci * Fetch switch information from card. Note, sd3_bus_mode can change if 9408c2ecf20Sopenharmony_ci * voltage switch outcome changes, so do this always. 9418c2ecf20Sopenharmony_ci */ 9428c2ecf20Sopenharmony_ci err = mmc_read_switch(card); 9438c2ecf20Sopenharmony_ci if (err) 9448c2ecf20Sopenharmony_ci return err; 9458c2ecf20Sopenharmony_ci 9468c2ecf20Sopenharmony_ci /* 9478c2ecf20Sopenharmony_ci * For SPI, enable CRC as appropriate. 9488c2ecf20Sopenharmony_ci * This CRC enable is located AFTER the reading of the 9498c2ecf20Sopenharmony_ci * card registers because some SDHC cards are not able 9508c2ecf20Sopenharmony_ci * to provide valid CRCs for non-512-byte blocks. 9518c2ecf20Sopenharmony_ci */ 9528c2ecf20Sopenharmony_ci if (mmc_host_is_spi(host)) { 9538c2ecf20Sopenharmony_ci err = mmc_spi_set_crc(host, use_spi_crc); 9548c2ecf20Sopenharmony_ci if (err) 9558c2ecf20Sopenharmony_ci return err; 9568c2ecf20Sopenharmony_ci } 9578c2ecf20Sopenharmony_ci 9588c2ecf20Sopenharmony_ci /* 9598c2ecf20Sopenharmony_ci * Check if read-only switch is active. 9608c2ecf20Sopenharmony_ci */ 9618c2ecf20Sopenharmony_ci if (!reinit) { 9628c2ecf20Sopenharmony_ci int ro = mmc_sd_get_ro(host); 9638c2ecf20Sopenharmony_ci 9648c2ecf20Sopenharmony_ci if (ro < 0) { 9658c2ecf20Sopenharmony_ci pr_warn("%s: host does not support reading read-only switch, assuming write-enable\n", 9668c2ecf20Sopenharmony_ci mmc_hostname(host)); 9678c2ecf20Sopenharmony_ci } else if (ro > 0) { 9688c2ecf20Sopenharmony_ci mmc_card_set_readonly(card); 9698c2ecf20Sopenharmony_ci } 9708c2ecf20Sopenharmony_ci } 9718c2ecf20Sopenharmony_ci 9728c2ecf20Sopenharmony_ci return 0; 9738c2ecf20Sopenharmony_ci} 9748c2ecf20Sopenharmony_ci 9758c2ecf20Sopenharmony_ciunsigned mmc_sd_get_max_clock(struct mmc_card *card) 9768c2ecf20Sopenharmony_ci{ 9778c2ecf20Sopenharmony_ci unsigned max_dtr = (unsigned int)-1; 9788c2ecf20Sopenharmony_ci 9798c2ecf20Sopenharmony_ci if (mmc_card_hs(card)) { 9808c2ecf20Sopenharmony_ci if (max_dtr > card->sw_caps.hs_max_dtr) 9818c2ecf20Sopenharmony_ci max_dtr = card->sw_caps.hs_max_dtr; 9828c2ecf20Sopenharmony_ci } else if (max_dtr > card->csd.max_dtr) { 9838c2ecf20Sopenharmony_ci max_dtr = card->csd.max_dtr; 9848c2ecf20Sopenharmony_ci } 9858c2ecf20Sopenharmony_ci 9868c2ecf20Sopenharmony_ci return max_dtr; 9878c2ecf20Sopenharmony_ci} 9888c2ecf20Sopenharmony_ci 9898c2ecf20Sopenharmony_cistatic bool mmc_sd_card_using_v18(struct mmc_card *card) 9908c2ecf20Sopenharmony_ci{ 9918c2ecf20Sopenharmony_ci /* 9928c2ecf20Sopenharmony_ci * According to the SD spec., the Bus Speed Mode (function group 1) bits 9938c2ecf20Sopenharmony_ci * 2 to 4 are zero if the card is initialized at 3.3V signal level. Thus 9948c2ecf20Sopenharmony_ci * they can be used to determine if the card has already switched to 9958c2ecf20Sopenharmony_ci * 1.8V signaling. 9968c2ecf20Sopenharmony_ci */ 9978c2ecf20Sopenharmony_ci return card->sw_caps.sd3_bus_mode & 9988c2ecf20Sopenharmony_ci (SD_MODE_UHS_SDR50 | SD_MODE_UHS_SDR104 | SD_MODE_UHS_DDR50); 9998c2ecf20Sopenharmony_ci} 10008c2ecf20Sopenharmony_ci 10018c2ecf20Sopenharmony_ci/* 10028c2ecf20Sopenharmony_ci * Handle the detection and initialisation of a card. 10038c2ecf20Sopenharmony_ci * 10048c2ecf20Sopenharmony_ci * In the case of a resume, "oldcard" will contain the card 10058c2ecf20Sopenharmony_ci * we're trying to reinitialise. 10068c2ecf20Sopenharmony_ci */ 10078c2ecf20Sopenharmony_cistatic int mmc_sd_init_card(struct mmc_host *host, u32 ocr, 10088c2ecf20Sopenharmony_ci struct mmc_card *oldcard) 10098c2ecf20Sopenharmony_ci{ 10108c2ecf20Sopenharmony_ci struct mmc_card *card; 10118c2ecf20Sopenharmony_ci int err; 10128c2ecf20Sopenharmony_ci u32 cid[4]; 10138c2ecf20Sopenharmony_ci u32 rocr = 0; 10148c2ecf20Sopenharmony_ci bool v18_fixup_failed = false; 10158c2ecf20Sopenharmony_ci 10168c2ecf20Sopenharmony_ci WARN_ON(!host->claimed); 10178c2ecf20Sopenharmony_ciretry: 10188c2ecf20Sopenharmony_ci err = mmc_sd_get_cid(host, ocr, cid, &rocr); 10198c2ecf20Sopenharmony_ci if (err) 10208c2ecf20Sopenharmony_ci return err; 10218c2ecf20Sopenharmony_ci 10228c2ecf20Sopenharmony_ci if (oldcard) { 10238c2ecf20Sopenharmony_ci if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) { 10248c2ecf20Sopenharmony_ci pr_debug("%s: Perhaps the card was replaced\n", 10258c2ecf20Sopenharmony_ci mmc_hostname(host)); 10268c2ecf20Sopenharmony_ci return -ENOENT; 10278c2ecf20Sopenharmony_ci } 10288c2ecf20Sopenharmony_ci 10298c2ecf20Sopenharmony_ci card = oldcard; 10308c2ecf20Sopenharmony_ci } else { 10318c2ecf20Sopenharmony_ci /* 10328c2ecf20Sopenharmony_ci * Allocate card structure. 10338c2ecf20Sopenharmony_ci */ 10348c2ecf20Sopenharmony_ci card = mmc_alloc_card(host, &sd_type); 10358c2ecf20Sopenharmony_ci if (IS_ERR(card)) 10368c2ecf20Sopenharmony_ci return PTR_ERR(card); 10378c2ecf20Sopenharmony_ci 10388c2ecf20Sopenharmony_ci card->ocr = ocr; 10398c2ecf20Sopenharmony_ci card->type = MMC_TYPE_SD; 10408c2ecf20Sopenharmony_ci memcpy(card->raw_cid, cid, sizeof(card->raw_cid)); 10418c2ecf20Sopenharmony_ci } 10428c2ecf20Sopenharmony_ci 10438c2ecf20Sopenharmony_ci /* 10448c2ecf20Sopenharmony_ci * Call the optional HC's init_card function to handle quirks. 10458c2ecf20Sopenharmony_ci */ 10468c2ecf20Sopenharmony_ci if (host->ops->init_card) 10478c2ecf20Sopenharmony_ci host->ops->init_card(host, card); 10488c2ecf20Sopenharmony_ci 10498c2ecf20Sopenharmony_ci /* 10508c2ecf20Sopenharmony_ci * For native busses: get card RCA and quit open drain mode. 10518c2ecf20Sopenharmony_ci */ 10528c2ecf20Sopenharmony_ci if (!mmc_host_is_spi(host)) { 10538c2ecf20Sopenharmony_ci err = mmc_send_relative_addr(host, &card->rca); 10548c2ecf20Sopenharmony_ci if (err) 10558c2ecf20Sopenharmony_ci goto free_card; 10568c2ecf20Sopenharmony_ci } 10578c2ecf20Sopenharmony_ci 10588c2ecf20Sopenharmony_ci if (!oldcard) { 10598c2ecf20Sopenharmony_ci err = mmc_sd_get_csd(host, card); 10608c2ecf20Sopenharmony_ci if (err) 10618c2ecf20Sopenharmony_ci goto free_card; 10628c2ecf20Sopenharmony_ci 10638c2ecf20Sopenharmony_ci mmc_decode_cid(card); 10648c2ecf20Sopenharmony_ci } 10658c2ecf20Sopenharmony_ci 10668c2ecf20Sopenharmony_ci /* 10678c2ecf20Sopenharmony_ci * handling only for cards supporting DSR and hosts requesting 10688c2ecf20Sopenharmony_ci * DSR configuration 10698c2ecf20Sopenharmony_ci */ 10708c2ecf20Sopenharmony_ci if (card->csd.dsr_imp && host->dsr_req) 10718c2ecf20Sopenharmony_ci mmc_set_dsr(host); 10728c2ecf20Sopenharmony_ci 10738c2ecf20Sopenharmony_ci /* 10748c2ecf20Sopenharmony_ci * Select card, as all following commands rely on that. 10758c2ecf20Sopenharmony_ci */ 10768c2ecf20Sopenharmony_ci if (!mmc_host_is_spi(host)) { 10778c2ecf20Sopenharmony_ci err = mmc_select_card(card); 10788c2ecf20Sopenharmony_ci if (err) 10798c2ecf20Sopenharmony_ci goto free_card; 10808c2ecf20Sopenharmony_ci } 10818c2ecf20Sopenharmony_ci 10828c2ecf20Sopenharmony_ci err = mmc_sd_setup_card(host, card, oldcard != NULL); 10838c2ecf20Sopenharmony_ci if (err) 10848c2ecf20Sopenharmony_ci goto free_card; 10858c2ecf20Sopenharmony_ci 10868c2ecf20Sopenharmony_ci /* 10878c2ecf20Sopenharmony_ci * If the card has not been power cycled, it may still be using 1.8V 10888c2ecf20Sopenharmony_ci * signaling. Detect that situation and try to initialize a UHS-I (1.8V) 10898c2ecf20Sopenharmony_ci * transfer mode. 10908c2ecf20Sopenharmony_ci */ 10918c2ecf20Sopenharmony_ci if (!v18_fixup_failed && !mmc_host_is_spi(host) && mmc_host_uhs(host) && 10928c2ecf20Sopenharmony_ci mmc_sd_card_using_v18(card) && 10938c2ecf20Sopenharmony_ci host->ios.signal_voltage != MMC_SIGNAL_VOLTAGE_180) { 10948c2ecf20Sopenharmony_ci if (mmc_host_set_uhs_voltage(host) || 10958c2ecf20Sopenharmony_ci mmc_sd_init_uhs_card(card)) { 10968c2ecf20Sopenharmony_ci v18_fixup_failed = true; 10978c2ecf20Sopenharmony_ci mmc_power_cycle(host, ocr); 10988c2ecf20Sopenharmony_ci if (!oldcard) 10998c2ecf20Sopenharmony_ci mmc_remove_card(card); 11008c2ecf20Sopenharmony_ci goto retry; 11018c2ecf20Sopenharmony_ci } 11028c2ecf20Sopenharmony_ci goto cont; 11038c2ecf20Sopenharmony_ci } 11048c2ecf20Sopenharmony_ci 11058c2ecf20Sopenharmony_ci /* Initialization sequence for UHS-I cards */ 11068c2ecf20Sopenharmony_ci if (rocr & SD_ROCR_S18A && mmc_host_uhs(host)) { 11078c2ecf20Sopenharmony_ci err = mmc_sd_init_uhs_card(card); 11088c2ecf20Sopenharmony_ci if (err) 11098c2ecf20Sopenharmony_ci goto free_card; 11108c2ecf20Sopenharmony_ci } else { 11118c2ecf20Sopenharmony_ci /* 11128c2ecf20Sopenharmony_ci * Attempt to change to high-speed (if supported) 11138c2ecf20Sopenharmony_ci */ 11148c2ecf20Sopenharmony_ci err = mmc_sd_switch_hs(card); 11158c2ecf20Sopenharmony_ci if (err > 0) 11168c2ecf20Sopenharmony_ci mmc_set_timing(card->host, MMC_TIMING_SD_HS); 11178c2ecf20Sopenharmony_ci else if (err) 11188c2ecf20Sopenharmony_ci goto free_card; 11198c2ecf20Sopenharmony_ci 11208c2ecf20Sopenharmony_ci /* 11218c2ecf20Sopenharmony_ci * Set bus speed. 11228c2ecf20Sopenharmony_ci */ 11238c2ecf20Sopenharmony_ci mmc_set_clock(host, mmc_sd_get_max_clock(card)); 11248c2ecf20Sopenharmony_ci 11258c2ecf20Sopenharmony_ci /* 11268c2ecf20Sopenharmony_ci * Switch to wider bus (if supported). 11278c2ecf20Sopenharmony_ci */ 11288c2ecf20Sopenharmony_ci if ((host->caps & MMC_CAP_4_BIT_DATA) && 11298c2ecf20Sopenharmony_ci (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) { 11308c2ecf20Sopenharmony_ci err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4); 11318c2ecf20Sopenharmony_ci if (err) 11328c2ecf20Sopenharmony_ci goto free_card; 11338c2ecf20Sopenharmony_ci 11348c2ecf20Sopenharmony_ci mmc_set_bus_width(host, MMC_BUS_WIDTH_4); 11358c2ecf20Sopenharmony_ci } 11368c2ecf20Sopenharmony_ci } 11378c2ecf20Sopenharmony_cicont: 11388c2ecf20Sopenharmony_ci if (host->cqe_ops && !host->cqe_enabled) { 11398c2ecf20Sopenharmony_ci err = host->cqe_ops->cqe_enable(host, card); 11408c2ecf20Sopenharmony_ci if (!err) { 11418c2ecf20Sopenharmony_ci host->cqe_enabled = true; 11428c2ecf20Sopenharmony_ci host->hsq_enabled = true; 11438c2ecf20Sopenharmony_ci pr_info("%s: Host Software Queue enabled\n", 11448c2ecf20Sopenharmony_ci mmc_hostname(host)); 11458c2ecf20Sopenharmony_ci } 11468c2ecf20Sopenharmony_ci } 11478c2ecf20Sopenharmony_ci 11488c2ecf20Sopenharmony_ci if (host->caps2 & MMC_CAP2_AVOID_3_3V && 11498c2ecf20Sopenharmony_ci host->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_330) { 11508c2ecf20Sopenharmony_ci pr_err("%s: Host failed to negotiate down from 3.3V\n", 11518c2ecf20Sopenharmony_ci mmc_hostname(host)); 11528c2ecf20Sopenharmony_ci err = -EINVAL; 11538c2ecf20Sopenharmony_ci goto free_card; 11548c2ecf20Sopenharmony_ci } 11558c2ecf20Sopenharmony_ci 11568c2ecf20Sopenharmony_ci host->card = card; 11578c2ecf20Sopenharmony_ci return 0; 11588c2ecf20Sopenharmony_ci 11598c2ecf20Sopenharmony_cifree_card: 11608c2ecf20Sopenharmony_ci if (!oldcard) 11618c2ecf20Sopenharmony_ci mmc_remove_card(card); 11628c2ecf20Sopenharmony_ci 11638c2ecf20Sopenharmony_ci return err; 11648c2ecf20Sopenharmony_ci} 11658c2ecf20Sopenharmony_ci 11668c2ecf20Sopenharmony_ci/* 11678c2ecf20Sopenharmony_ci * Host is being removed. Free up the current card. 11688c2ecf20Sopenharmony_ci */ 11698c2ecf20Sopenharmony_cistatic void mmc_sd_remove(struct mmc_host *host) 11708c2ecf20Sopenharmony_ci{ 11718c2ecf20Sopenharmony_ci mmc_remove_card(host->card); 11728c2ecf20Sopenharmony_ci host->card = NULL; 11738c2ecf20Sopenharmony_ci} 11748c2ecf20Sopenharmony_ci 11758c2ecf20Sopenharmony_ci/* 11768c2ecf20Sopenharmony_ci * Card detection - card is alive. 11778c2ecf20Sopenharmony_ci */ 11788c2ecf20Sopenharmony_cistatic int mmc_sd_alive(struct mmc_host *host) 11798c2ecf20Sopenharmony_ci{ 11808c2ecf20Sopenharmony_ci return mmc_send_status(host->card, NULL); 11818c2ecf20Sopenharmony_ci} 11828c2ecf20Sopenharmony_ci 11838c2ecf20Sopenharmony_ci/* 11848c2ecf20Sopenharmony_ci * Card detection callback from host. 11858c2ecf20Sopenharmony_ci */ 11868c2ecf20Sopenharmony_cistatic void mmc_sd_detect(struct mmc_host *host) 11878c2ecf20Sopenharmony_ci{ 11888c2ecf20Sopenharmony_ci int err; 11898c2ecf20Sopenharmony_ci 11908c2ecf20Sopenharmony_ci mmc_get_card(host->card, NULL); 11918c2ecf20Sopenharmony_ci 11928c2ecf20Sopenharmony_ci /* 11938c2ecf20Sopenharmony_ci * Just check if our card has been removed. 11948c2ecf20Sopenharmony_ci */ 11958c2ecf20Sopenharmony_ci err = _mmc_detect_card_removed(host); 11968c2ecf20Sopenharmony_ci 11978c2ecf20Sopenharmony_ci mmc_put_card(host->card, NULL); 11988c2ecf20Sopenharmony_ci 11998c2ecf20Sopenharmony_ci if (err) { 12008c2ecf20Sopenharmony_ci mmc_sd_remove(host); 12018c2ecf20Sopenharmony_ci 12028c2ecf20Sopenharmony_ci mmc_claim_host(host); 12038c2ecf20Sopenharmony_ci mmc_detach_bus(host); 12048c2ecf20Sopenharmony_ci mmc_power_off(host); 12058c2ecf20Sopenharmony_ci mmc_release_host(host); 12068c2ecf20Sopenharmony_ci } 12078c2ecf20Sopenharmony_ci} 12088c2ecf20Sopenharmony_ci 12098c2ecf20Sopenharmony_cistatic int _mmc_sd_suspend(struct mmc_host *host) 12108c2ecf20Sopenharmony_ci{ 12118c2ecf20Sopenharmony_ci int err = 0; 12128c2ecf20Sopenharmony_ci 12138c2ecf20Sopenharmony_ci mmc_claim_host(host); 12148c2ecf20Sopenharmony_ci 12158c2ecf20Sopenharmony_ci if (mmc_card_suspended(host->card)) 12168c2ecf20Sopenharmony_ci goto out; 12178c2ecf20Sopenharmony_ci 12188c2ecf20Sopenharmony_ci if (!mmc_host_is_spi(host)) 12198c2ecf20Sopenharmony_ci err = mmc_deselect_cards(host); 12208c2ecf20Sopenharmony_ci 12218c2ecf20Sopenharmony_ci if (!err) { 12228c2ecf20Sopenharmony_ci mmc_power_off(host); 12238c2ecf20Sopenharmony_ci mmc_card_set_suspended(host->card); 12248c2ecf20Sopenharmony_ci } 12258c2ecf20Sopenharmony_ci 12268c2ecf20Sopenharmony_ciout: 12278c2ecf20Sopenharmony_ci mmc_release_host(host); 12288c2ecf20Sopenharmony_ci return err; 12298c2ecf20Sopenharmony_ci} 12308c2ecf20Sopenharmony_ci 12318c2ecf20Sopenharmony_ci/* 12328c2ecf20Sopenharmony_ci * Callback for suspend 12338c2ecf20Sopenharmony_ci */ 12348c2ecf20Sopenharmony_cistatic int mmc_sd_suspend(struct mmc_host *host) 12358c2ecf20Sopenharmony_ci{ 12368c2ecf20Sopenharmony_ci int err; 12378c2ecf20Sopenharmony_ci 12388c2ecf20Sopenharmony_ci err = _mmc_sd_suspend(host); 12398c2ecf20Sopenharmony_ci if (!err) { 12408c2ecf20Sopenharmony_ci pm_runtime_disable(&host->card->dev); 12418c2ecf20Sopenharmony_ci pm_runtime_set_suspended(&host->card->dev); 12428c2ecf20Sopenharmony_ci } 12438c2ecf20Sopenharmony_ci 12448c2ecf20Sopenharmony_ci return err; 12458c2ecf20Sopenharmony_ci} 12468c2ecf20Sopenharmony_ci 12478c2ecf20Sopenharmony_ci/* 12488c2ecf20Sopenharmony_ci * This function tries to determine if the same card is still present 12498c2ecf20Sopenharmony_ci * and, if so, restore all state to it. 12508c2ecf20Sopenharmony_ci */ 12518c2ecf20Sopenharmony_cistatic int _mmc_sd_resume(struct mmc_host *host) 12528c2ecf20Sopenharmony_ci{ 12538c2ecf20Sopenharmony_ci int err = 0; 12548c2ecf20Sopenharmony_ci 12558c2ecf20Sopenharmony_ci mmc_claim_host(host); 12568c2ecf20Sopenharmony_ci 12578c2ecf20Sopenharmony_ci if (!mmc_card_suspended(host->card)) 12588c2ecf20Sopenharmony_ci goto out; 12598c2ecf20Sopenharmony_ci 12608c2ecf20Sopenharmony_ci mmc_power_up(host, host->card->ocr); 12618c2ecf20Sopenharmony_ci err = mmc_sd_init_card(host, host->card->ocr, host->card); 12628c2ecf20Sopenharmony_ci mmc_card_clr_suspended(host->card); 12638c2ecf20Sopenharmony_ci 12648c2ecf20Sopenharmony_ciout: 12658c2ecf20Sopenharmony_ci mmc_release_host(host); 12668c2ecf20Sopenharmony_ci return err; 12678c2ecf20Sopenharmony_ci} 12688c2ecf20Sopenharmony_ci 12698c2ecf20Sopenharmony_ci/* 12708c2ecf20Sopenharmony_ci * Callback for resume 12718c2ecf20Sopenharmony_ci */ 12728c2ecf20Sopenharmony_cistatic int mmc_sd_resume(struct mmc_host *host) 12738c2ecf20Sopenharmony_ci{ 12748c2ecf20Sopenharmony_ci pm_runtime_enable(&host->card->dev); 12758c2ecf20Sopenharmony_ci return 0; 12768c2ecf20Sopenharmony_ci} 12778c2ecf20Sopenharmony_ci 12788c2ecf20Sopenharmony_ci/* 12798c2ecf20Sopenharmony_ci * Callback for runtime_suspend. 12808c2ecf20Sopenharmony_ci */ 12818c2ecf20Sopenharmony_cistatic int mmc_sd_runtime_suspend(struct mmc_host *host) 12828c2ecf20Sopenharmony_ci{ 12838c2ecf20Sopenharmony_ci int err; 12848c2ecf20Sopenharmony_ci 12858c2ecf20Sopenharmony_ci if (!(host->caps & MMC_CAP_AGGRESSIVE_PM)) 12868c2ecf20Sopenharmony_ci return 0; 12878c2ecf20Sopenharmony_ci 12888c2ecf20Sopenharmony_ci err = _mmc_sd_suspend(host); 12898c2ecf20Sopenharmony_ci if (err) 12908c2ecf20Sopenharmony_ci pr_err("%s: error %d doing aggressive suspend\n", 12918c2ecf20Sopenharmony_ci mmc_hostname(host), err); 12928c2ecf20Sopenharmony_ci 12938c2ecf20Sopenharmony_ci return err; 12948c2ecf20Sopenharmony_ci} 12958c2ecf20Sopenharmony_ci 12968c2ecf20Sopenharmony_ci/* 12978c2ecf20Sopenharmony_ci * Callback for runtime_resume. 12988c2ecf20Sopenharmony_ci */ 12998c2ecf20Sopenharmony_cistatic int mmc_sd_runtime_resume(struct mmc_host *host) 13008c2ecf20Sopenharmony_ci{ 13018c2ecf20Sopenharmony_ci int err; 13028c2ecf20Sopenharmony_ci 13038c2ecf20Sopenharmony_ci err = _mmc_sd_resume(host); 13048c2ecf20Sopenharmony_ci if (err && err != -ENOMEDIUM) 13058c2ecf20Sopenharmony_ci pr_err("%s: error %d doing runtime resume\n", 13068c2ecf20Sopenharmony_ci mmc_hostname(host), err); 13078c2ecf20Sopenharmony_ci 13088c2ecf20Sopenharmony_ci return 0; 13098c2ecf20Sopenharmony_ci} 13108c2ecf20Sopenharmony_ci 13118c2ecf20Sopenharmony_cistatic int mmc_sd_hw_reset(struct mmc_host *host) 13128c2ecf20Sopenharmony_ci{ 13138c2ecf20Sopenharmony_ci mmc_power_cycle(host, host->card->ocr); 13148c2ecf20Sopenharmony_ci return mmc_sd_init_card(host, host->card->ocr, host->card); 13158c2ecf20Sopenharmony_ci} 13168c2ecf20Sopenharmony_ci 13178c2ecf20Sopenharmony_cistatic const struct mmc_bus_ops mmc_sd_ops = { 13188c2ecf20Sopenharmony_ci .remove = mmc_sd_remove, 13198c2ecf20Sopenharmony_ci .detect = mmc_sd_detect, 13208c2ecf20Sopenharmony_ci .runtime_suspend = mmc_sd_runtime_suspend, 13218c2ecf20Sopenharmony_ci .runtime_resume = mmc_sd_runtime_resume, 13228c2ecf20Sopenharmony_ci .suspend = mmc_sd_suspend, 13238c2ecf20Sopenharmony_ci .resume = mmc_sd_resume, 13248c2ecf20Sopenharmony_ci .alive = mmc_sd_alive, 13258c2ecf20Sopenharmony_ci .shutdown = mmc_sd_suspend, 13268c2ecf20Sopenharmony_ci .hw_reset = mmc_sd_hw_reset, 13278c2ecf20Sopenharmony_ci}; 13288c2ecf20Sopenharmony_ci 13298c2ecf20Sopenharmony_ci/* 13308c2ecf20Sopenharmony_ci * Starting point for SD card init. 13318c2ecf20Sopenharmony_ci */ 13328c2ecf20Sopenharmony_ciint mmc_attach_sd(struct mmc_host *host) 13338c2ecf20Sopenharmony_ci{ 13348c2ecf20Sopenharmony_ci int err; 13358c2ecf20Sopenharmony_ci u32 ocr, rocr; 13368c2ecf20Sopenharmony_ci 13378c2ecf20Sopenharmony_ci WARN_ON(!host->claimed); 13388c2ecf20Sopenharmony_ci 13398c2ecf20Sopenharmony_ci err = mmc_send_app_op_cond(host, 0, &ocr); 13408c2ecf20Sopenharmony_ci if (err) 13418c2ecf20Sopenharmony_ci return err; 13428c2ecf20Sopenharmony_ci 13438c2ecf20Sopenharmony_ci mmc_attach_bus(host, &mmc_sd_ops); 13448c2ecf20Sopenharmony_ci if (host->ocr_avail_sd) 13458c2ecf20Sopenharmony_ci host->ocr_avail = host->ocr_avail_sd; 13468c2ecf20Sopenharmony_ci 13478c2ecf20Sopenharmony_ci /* 13488c2ecf20Sopenharmony_ci * We need to get OCR a different way for SPI. 13498c2ecf20Sopenharmony_ci */ 13508c2ecf20Sopenharmony_ci if (mmc_host_is_spi(host)) { 13518c2ecf20Sopenharmony_ci mmc_go_idle(host); 13528c2ecf20Sopenharmony_ci 13538c2ecf20Sopenharmony_ci err = mmc_spi_read_ocr(host, 0, &ocr); 13548c2ecf20Sopenharmony_ci if (err) 13558c2ecf20Sopenharmony_ci goto err; 13568c2ecf20Sopenharmony_ci } 13578c2ecf20Sopenharmony_ci 13588c2ecf20Sopenharmony_ci /* 13598c2ecf20Sopenharmony_ci * Some SD cards claims an out of spec VDD voltage range. Let's treat 13608c2ecf20Sopenharmony_ci * these bits as being in-valid and especially also bit7. 13618c2ecf20Sopenharmony_ci */ 13628c2ecf20Sopenharmony_ci ocr &= ~0x7FFF; 13638c2ecf20Sopenharmony_ci 13648c2ecf20Sopenharmony_ci rocr = mmc_select_voltage(host, ocr); 13658c2ecf20Sopenharmony_ci 13668c2ecf20Sopenharmony_ci /* 13678c2ecf20Sopenharmony_ci * Can we support the voltage(s) of the card(s)? 13688c2ecf20Sopenharmony_ci */ 13698c2ecf20Sopenharmony_ci if (!rocr) { 13708c2ecf20Sopenharmony_ci err = -EINVAL; 13718c2ecf20Sopenharmony_ci goto err; 13728c2ecf20Sopenharmony_ci } 13738c2ecf20Sopenharmony_ci 13748c2ecf20Sopenharmony_ci /* 13758c2ecf20Sopenharmony_ci * Detect and init the card. 13768c2ecf20Sopenharmony_ci */ 13778c2ecf20Sopenharmony_ci err = mmc_sd_init_card(host, rocr, NULL); 13788c2ecf20Sopenharmony_ci if (err) 13798c2ecf20Sopenharmony_ci goto err; 13808c2ecf20Sopenharmony_ci 13818c2ecf20Sopenharmony_ci mmc_release_host(host); 13828c2ecf20Sopenharmony_ci err = mmc_add_card(host->card); 13838c2ecf20Sopenharmony_ci if (err) 13848c2ecf20Sopenharmony_ci goto remove_card; 13858c2ecf20Sopenharmony_ci 13868c2ecf20Sopenharmony_ci mmc_claim_host(host); 13878c2ecf20Sopenharmony_ci return 0; 13888c2ecf20Sopenharmony_ci 13898c2ecf20Sopenharmony_ciremove_card: 13908c2ecf20Sopenharmony_ci mmc_remove_card(host->card); 13918c2ecf20Sopenharmony_ci host->card = NULL; 13928c2ecf20Sopenharmony_ci mmc_claim_host(host); 13938c2ecf20Sopenharmony_cierr: 13948c2ecf20Sopenharmony_ci mmc_detach_bus(host); 13958c2ecf20Sopenharmony_ci 13968c2ecf20Sopenharmony_ci pr_err("%s: error %d whilst initialising SD card\n", 13978c2ecf20Sopenharmony_ci mmc_hostname(host), err); 13988c2ecf20Sopenharmony_ci 13998c2ecf20Sopenharmony_ci return err; 14008c2ecf20Sopenharmony_ci} 1401