Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  linux/drivers/mmc/core/sd_ops.h
0004  *
0005  *  Copyright 2006-2007 Pierre Ossman
0006  */
0007 
0008 #include <linux/slab.h>
0009 #include <linux/types.h>
0010 #include <linux/export.h>
0011 #include <linux/scatterlist.h>
0012 
0013 #include <linux/mmc/host.h>
0014 #include <linux/mmc/card.h>
0015 #include <linux/mmc/mmc.h>
0016 #include <linux/mmc/sd.h>
0017 
0018 #include "core.h"
0019 #include "sd_ops.h"
0020 #include "mmc_ops.h"
0021 
0022 int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card)
0023 {
0024     int err;
0025     struct mmc_command cmd = {};
0026 
0027     if (WARN_ON(card && card->host != host))
0028         return -EINVAL;
0029 
0030     cmd.opcode = MMC_APP_CMD;
0031 
0032     if (card) {
0033         cmd.arg = card->rca << 16;
0034         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
0035     } else {
0036         cmd.arg = 0;
0037         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_BCR;
0038     }
0039 
0040     err = mmc_wait_for_cmd(host, &cmd, 0);
0041     if (err)
0042         return err;
0043 
0044     /* Check that card supported application commands */
0045     if (!mmc_host_is_spi(host) && !(cmd.resp[0] & R1_APP_CMD))
0046         return -EOPNOTSUPP;
0047 
0048     return 0;
0049 }
0050 EXPORT_SYMBOL_GPL(mmc_app_cmd);
0051 
0052 static int mmc_wait_for_app_cmd(struct mmc_host *host, struct mmc_card *card,
0053                 struct mmc_command *cmd)
0054 {
0055     struct mmc_request mrq = {};
0056     int i, err = -EIO;
0057 
0058     /*
0059      * We have to resend MMC_APP_CMD for each attempt so
0060      * we cannot use the retries field in mmc_command.
0061      */
0062     for (i = 0; i <= MMC_CMD_RETRIES; i++) {
0063         err = mmc_app_cmd(host, card);
0064         if (err) {
0065             /* no point in retrying; no APP commands allowed */
0066             if (mmc_host_is_spi(host)) {
0067                 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
0068                     break;
0069             }
0070             continue;
0071         }
0072 
0073         memset(&mrq, 0, sizeof(struct mmc_request));
0074 
0075         memset(cmd->resp, 0, sizeof(cmd->resp));
0076         cmd->retries = 0;
0077 
0078         mrq.cmd = cmd;
0079         cmd->data = NULL;
0080 
0081         mmc_wait_for_req(host, &mrq);
0082 
0083         err = cmd->error;
0084         if (!cmd->error)
0085             break;
0086 
0087         /* no point in retrying illegal APP commands */
0088         if (mmc_host_is_spi(host)) {
0089             if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
0090                 break;
0091         }
0092     }
0093 
0094     return err;
0095 }
0096 
0097 int mmc_app_set_bus_width(struct mmc_card *card, int width)
0098 {
0099     struct mmc_command cmd = {};
0100 
0101     cmd.opcode = SD_APP_SET_BUS_WIDTH;
0102     cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
0103 
0104     switch (width) {
0105     case MMC_BUS_WIDTH_1:
0106         cmd.arg = SD_BUS_WIDTH_1;
0107         break;
0108     case MMC_BUS_WIDTH_4:
0109         cmd.arg = SD_BUS_WIDTH_4;
0110         break;
0111     default:
0112         return -EINVAL;
0113     }
0114 
0115     return mmc_wait_for_app_cmd(card->host, card, &cmd);
0116 }
0117 
0118 int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
0119 {
0120     struct mmc_command cmd = {};
0121     int i, err = 0;
0122 
0123     cmd.opcode = SD_APP_OP_COND;
0124     if (mmc_host_is_spi(host))
0125         cmd.arg = ocr & (1 << 30); /* SPI only defines one bit */
0126     else
0127         cmd.arg = ocr;
0128     cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
0129 
0130     for (i = 100; i; i--) {
0131         err = mmc_wait_for_app_cmd(host, NULL, &cmd);
0132         if (err)
0133             break;
0134 
0135         /* if we're just probing, do a single pass */
0136         if (ocr == 0)
0137             break;
0138 
0139         /* otherwise wait until reset completes */
0140         if (mmc_host_is_spi(host)) {
0141             if (!(cmd.resp[0] & R1_SPI_IDLE))
0142                 break;
0143         } else {
0144             if (cmd.resp[0] & MMC_CARD_BUSY)
0145                 break;
0146         }
0147 
0148         err = -ETIMEDOUT;
0149 
0150         mmc_delay(10);
0151     }
0152 
0153     if (!i)
0154         pr_err("%s: card never left busy state\n", mmc_hostname(host));
0155 
0156     if (rocr && !mmc_host_is_spi(host))
0157         *rocr = cmd.resp[0];
0158 
0159     return err;
0160 }
0161 
0162 static int __mmc_send_if_cond(struct mmc_host *host, u32 ocr, u8 pcie_bits,
0163                   u32 *resp)
0164 {
0165     struct mmc_command cmd = {};
0166     int err;
0167     static const u8 test_pattern = 0xAA;
0168     u8 result_pattern;
0169 
0170     /*
0171      * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
0172      * before SD_APP_OP_COND. This command will harmlessly fail for
0173      * SD 1.0 cards.
0174      */
0175     cmd.opcode = SD_SEND_IF_COND;
0176     cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | pcie_bits << 8 | test_pattern;
0177     cmd.flags = MMC_RSP_SPI_R7 | MMC_RSP_R7 | MMC_CMD_BCR;
0178 
0179     err = mmc_wait_for_cmd(host, &cmd, 0);
0180     if (err)
0181         return err;
0182 
0183     if (mmc_host_is_spi(host))
0184         result_pattern = cmd.resp[1] & 0xFF;
0185     else
0186         result_pattern = cmd.resp[0] & 0xFF;
0187 
0188     if (result_pattern != test_pattern)
0189         return -EIO;
0190 
0191     if (resp)
0192         *resp = cmd.resp[0];
0193 
0194     return 0;
0195 }
0196 
0197 int mmc_send_if_cond(struct mmc_host *host, u32 ocr)
0198 {
0199     return __mmc_send_if_cond(host, ocr, 0, NULL);
0200 }
0201 
0202 int mmc_send_if_cond_pcie(struct mmc_host *host, u32 ocr)
0203 {
0204     u32 resp = 0;
0205     u8 pcie_bits = 0;
0206     int ret;
0207 
0208     if (host->caps2 & MMC_CAP2_SD_EXP) {
0209         /* Probe card for SD express support via PCIe. */
0210         pcie_bits = 0x10;
0211         if (host->caps2 & MMC_CAP2_SD_EXP_1_2V)
0212             /* Probe also for 1.2V support. */
0213             pcie_bits = 0x30;
0214     }
0215 
0216     ret = __mmc_send_if_cond(host, ocr, pcie_bits, &resp);
0217     if (ret)
0218         return 0;
0219 
0220     /* Continue with the SD express init, if the card supports it. */
0221     resp &= 0x3000;
0222     if (pcie_bits && resp) {
0223         if (resp == 0x3000)
0224             host->ios.timing = MMC_TIMING_SD_EXP_1_2V;
0225         else
0226             host->ios.timing = MMC_TIMING_SD_EXP;
0227 
0228         /*
0229          * According to the spec the clock shall also be gated, but
0230          * let's leave this to the host driver for more flexibility.
0231          */
0232         return host->ops->init_sd_express(host, &host->ios);
0233     }
0234 
0235     return 0;
0236 }
0237 
0238 int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca)
0239 {
0240     int err;
0241     struct mmc_command cmd = {};
0242 
0243     cmd.opcode = SD_SEND_RELATIVE_ADDR;
0244     cmd.arg = 0;
0245     cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
0246 
0247     err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
0248     if (err)
0249         return err;
0250 
0251     *rca = cmd.resp[0] >> 16;
0252 
0253     return 0;
0254 }
0255 
0256 int mmc_app_send_scr(struct mmc_card *card)
0257 {
0258     int err;
0259     struct mmc_request mrq = {};
0260     struct mmc_command cmd = {};
0261     struct mmc_data data = {};
0262     struct scatterlist sg;
0263     __be32 *scr;
0264 
0265     /* NOTE: caller guarantees scr is heap-allocated */
0266 
0267     err = mmc_app_cmd(card->host, card);
0268     if (err)
0269         return err;
0270 
0271     /* dma onto stack is unsafe/nonportable, but callers to this
0272      * routine normally provide temporary on-stack buffers ...
0273      */
0274     scr = kmalloc(sizeof(card->raw_scr), GFP_KERNEL);
0275     if (!scr)
0276         return -ENOMEM;
0277 
0278     mrq.cmd = &cmd;
0279     mrq.data = &data;
0280 
0281     cmd.opcode = SD_APP_SEND_SCR;
0282     cmd.arg = 0;
0283     cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
0284 
0285     data.blksz = 8;
0286     data.blocks = 1;
0287     data.flags = MMC_DATA_READ;
0288     data.sg = &sg;
0289     data.sg_len = 1;
0290 
0291     sg_init_one(&sg, scr, 8);
0292 
0293     mmc_set_data_timeout(&data, card);
0294 
0295     mmc_wait_for_req(card->host, &mrq);
0296 
0297     card->raw_scr[0] = be32_to_cpu(scr[0]);
0298     card->raw_scr[1] = be32_to_cpu(scr[1]);
0299 
0300     kfree(scr);
0301 
0302     if (cmd.error)
0303         return cmd.error;
0304     if (data.error)
0305         return data.error;
0306 
0307     return 0;
0308 }
0309 
0310 int mmc_sd_switch(struct mmc_card *card, int mode, int group,
0311     u8 value, u8 *resp)
0312 {
0313     u32 cmd_args;
0314 
0315     /* NOTE: caller guarantees resp is heap-allocated */
0316 
0317     mode = !!mode;
0318     value &= 0xF;
0319     cmd_args = mode << 31 | 0x00FFFFFF;
0320     cmd_args &= ~(0xF << (group * 4));
0321     cmd_args |= value << (group * 4);
0322 
0323     return mmc_send_adtc_data(card, card->host, SD_SWITCH, cmd_args, resp,
0324                   64);
0325 }
0326 
0327 int mmc_app_sd_status(struct mmc_card *card, void *ssr)
0328 {
0329     int err;
0330     struct mmc_request mrq = {};
0331     struct mmc_command cmd = {};
0332     struct mmc_data data = {};
0333     struct scatterlist sg;
0334 
0335     /* NOTE: caller guarantees ssr is heap-allocated */
0336 
0337     err = mmc_app_cmd(card->host, card);
0338     if (err)
0339         return err;
0340 
0341     mrq.cmd = &cmd;
0342     mrq.data = &data;
0343 
0344     cmd.opcode = SD_APP_SD_STATUS;
0345     cmd.arg = 0;
0346     cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_ADTC;
0347 
0348     data.blksz = 64;
0349     data.blocks = 1;
0350     data.flags = MMC_DATA_READ;
0351     data.sg = &sg;
0352     data.sg_len = 1;
0353 
0354     sg_init_one(&sg, ssr, 64);
0355 
0356     mmc_set_data_timeout(&data, card);
0357 
0358     mmc_wait_for_req(card->host, &mrq);
0359 
0360     if (cmd.error)
0361         return cmd.error;
0362     if (data.error)
0363         return data.error;
0364 
0365     return 0;
0366 }