Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions drivers/mmc/core/mmc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1910,8 +1910,8 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr,
host->cqe_enabled = true;

if (card->ext_csd.cmdq_en) {
pr_info("%s: Command Queue Engine enabled\n",
mmc_hostname(host));
pr_info("%s: Command Queue Engine enabled, %u tags\n",
mmc_hostname(host), card->ext_csd.cmdq_depth);
} else {
host->hsq_enabled = true;
pr_info("%s: Host Software Queue enabled\n",
Expand Down
133 changes: 36 additions & 97 deletions drivers/mmc/core/sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,8 @@ MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
MMC_DEV_ATTR(ocr, "0x%08x\n", card->ocr);
MMC_DEV_ATTR(rca, "0x%04x\n", card->rca);

MMC_DEV_ATTR(ext_perf, "%02x\n", card->ext_perf.feature_support);
MMC_DEV_ATTR(ext_power, "%02x\n", card->ext_power.feature_support);

static ssize_t mmc_dsr_show(struct device *dev, struct device_attribute *attr,
char *buf)
Expand Down Expand Up @@ -776,6 +777,8 @@ static struct attribute *sd_std_attrs[] = {
&dev_attr_ocr.attr,
&dev_attr_rca.attr,
&dev_attr_dsr.attr,
&dev_attr_ext_perf.attr,
&dev_attr_ext_power.attr,
NULL,
};

Expand Down Expand Up @@ -1015,83 +1018,6 @@ static bool mmc_sd_card_using_v18(struct mmc_card *card)
(SD_MODE_UHS_SDR50 | SD_MODE_UHS_SDR104 | SD_MODE_UHS_DDR50);
}

int sd_write_ext_reg(struct mmc_card *card, u8 fno, u8 page, u16 offset,
u8 reg_data)
{
struct mmc_host *host = card->host;
struct mmc_request mrq = {};
struct mmc_command cmd = {};
struct mmc_data data = {};
struct scatterlist sg;
u8 *reg_buf;

reg_buf = card->ext_reg_buf;
memset(reg_buf, 0, 512);

mrq.cmd = &cmd;
mrq.data = &data;

/*
* Arguments of CMD49:
* [31:31] MIO (0 = memory).
* [30:27] FNO (function number).
* [26:26] MW - mask write mode (0 = disable).
* [25:18] page number.
* [17:9] offset address.
* [8:0] length (0 = 1 byte).
*/
cmd.arg = fno << 27 | page << 18 | offset << 9;

/* The first byte in the buffer is the data to be written. */
reg_buf[0] = reg_data;

data.flags = MMC_DATA_WRITE;
data.blksz = 512;
data.blocks = 1;
data.sg = &sg;
data.sg_len = 1;
sg_init_one(&sg, reg_buf, 512);

cmd.opcode = SD_WRITE_EXTR_SINGLE;
cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;

mmc_set_data_timeout(&data, card);
mmc_wait_for_req(host, &mrq);

/*
* Note that, the SD card is allowed to signal busy on DAT0 up to 1s
* after the CMD49. Although, let's leave this to be managed by the
* caller.
*/

if (cmd.error)
return cmd.error;
if (data.error)
return data.error;

return 0;
}

static int sd_read_ext_reg(struct mmc_card *card, u8 fno, u8 page,
u16 offset, u16 len, u8 *reg_buf)
{
u32 cmd_args;

/*
* Command arguments of CMD48:
* [31:31] MIO (0 = memory).
* [30:27] FNO (function number).
* [26:26] reserved (0).
* [25:18] page number.
* [17:9] offset address.
* [8:0] length (0 = 1 byte, 1ff = 512 bytes).
*/
cmd_args = fno << 27 | page << 18 | offset << 9 | (len -1);

return mmc_send_adtc_data(card, card->host, SD_READ_EXTR_SINGLE,
cmd_args, reg_buf, 512);
}

static int sd_parse_ext_reg_power(struct mmc_card *card, u8 fno, u8 page,
u16 offset)
{
Expand All @@ -1101,7 +1027,7 @@ static int sd_parse_ext_reg_power(struct mmc_card *card, u8 fno, u8 page,
reg_buf = card->ext_reg_buf;

/* Read the extension register for power management function. */
err = sd_read_ext_reg(card, fno, page, offset, 512, reg_buf);
err = mmc_sd_read_ext_reg(card, fno, page, offset, 512, reg_buf);
if (err) {
pr_warn("%s: error %d reading PM func of ext reg\n",
mmc_hostname(card->host), err);
Expand Down Expand Up @@ -1139,7 +1065,7 @@ static int sd_parse_ext_reg_perf(struct mmc_card *card, u8 fno, u8 page,

reg_buf = card->ext_reg_buf;

err = sd_read_ext_reg(card, fno, page, offset, 512, reg_buf);
err = mmc_sd_read_ext_reg(card, fno, page, offset, 512, reg_buf);
if (err) {
pr_warn("%s: error %d reading PERF func of ext reg\n",
mmc_hostname(card->host), err);
Expand All @@ -1165,8 +1091,12 @@ static int sd_parse_ext_reg_perf(struct mmc_card *card, u8 fno, u8 page,
if ((reg_buf[4] & BIT(0)) && !mmc_card_broken_sd_cache(card))
card->ext_perf.feature_support |= SD_EXT_PERF_CACHE;

/* Command queue support indicated via queue depth bits (0 to 4). */
if (reg_buf[6] & 0x1f) {
/*
* Command queue support indicated via queue depth bits (0 to 4).
* Qualify this with the other mandatory required features.
*/
if (reg_buf[6] & 0x1f && card->ext_power.feature_support & SD_EXT_POWER_OFF_NOTIFY &&
card->ext_perf.feature_support & SD_EXT_PERF_CACHE) {
card->ext_perf.feature_support |= SD_EXT_PERF_CMD_QUEUE;
card->ext_csd.cmdq_depth = reg_buf[6] & 0x1f;
card->ext_csd.cmdq_support = true;
Expand Down Expand Up @@ -1234,7 +1164,7 @@ static int sd_parse_ext_reg(struct mmc_card *card, u8 *gen_info_buf,
return 0;
}

static int sd_read_ext_regs(struct mmc_card *card)
static int mmc_sd_read_ext_regs(struct mmc_card *card)
{
int err, i;
u8 num_ext, *gen_info_buf;
Expand All @@ -1246,7 +1176,7 @@ static int sd_read_ext_regs(struct mmc_card *card)
if (!(card->scr.cmds & SD_SCR_CMD48_SUPPORT))
return 0;

gen_info_buf = kzalloc(512, GFP_KERNEL);
gen_info_buf = kzalloc(1024, GFP_KERNEL);
if (!gen_info_buf)
return -ENOMEM;

Expand All @@ -1260,7 +1190,7 @@ static int sd_read_ext_regs(struct mmc_card *card)
* Read 512 bytes of general info, which is found at function number 0,
* at page 0 and with no offset.
*/
err = sd_read_ext_reg(card, 0, 0, 0, 512, gen_info_buf);
err = mmc_sd_read_ext_reg(card, 0, 0, 0, 512, gen_info_buf);
if (err) {
pr_err("%s: error %d reading general info of SD ext reg\n",
mmc_hostname(card->host), err);
Expand All @@ -1277,14 +1207,23 @@ static int sd_read_ext_regs(struct mmc_card *card)
num_ext = gen_info_buf[4];

/*
* We only support revision 0 and limit it to 512 bytes for simplicity.
* We only support revision 0 and up to the spec-defined maximum of 1K.
* No matter what, let's return zero to allow us to continue using the
* card, even if we can't support the features from the SD function
* extensions registers.
*/
if (rev != 0 || len > 512) {
pr_warn("%s: non-supported SD ext reg layout\n",
mmc_hostname(card->host));
if (rev != 0 || len > 1024) {
pr_warn("%s: non-supported SD ext reg layout rev %u length %u\n",
mmc_hostname(card->host), rev, len);
goto out;
}

/* If the General Information block spills into the next page, read the rest */
if (len > 512)
err = mmc_sd_read_ext_reg(card, 0, 1, 0, 512, &gen_info_buf[512]);
if (err) {
pr_err("%s: error %d reading page 1 of general info of SD ext reg\n",
mmc_hostname(card->host), err);
goto out;
}

Expand Down Expand Up @@ -1332,7 +1271,7 @@ static int sd_flush_cache(struct mmc_host *host)
page = card->ext_perf.page;
offset = card->ext_perf.offset + 261;

err = sd_write_ext_reg(card, fno, page, offset, BIT(0));
err = mmc_sd_write_ext_reg(card, fno, page, offset, BIT(0));
if (err) {
pr_warn("%s: error %d writing Cache Flush bit\n",
mmc_hostname(host), err);
Expand All @@ -1348,7 +1287,7 @@ static int sd_flush_cache(struct mmc_host *host)
* Read the Flush Cache bit. The card shall reset it, to confirm that
* it's has completed the flushing of the cache.
*/
err = sd_read_ext_reg(card, fno, page, offset, 1, reg_buf);
err = mmc_sd_read_ext_reg(card, fno, page, offset, 1, reg_buf);
if (err) {
pr_warn("%s: error %d reading Cache Flush bit\n",
mmc_hostname(host), err);
Expand All @@ -1371,7 +1310,7 @@ static int sd_enable_cache(struct mmc_card *card)
* Set Cache Enable at bit 0 in the performance enhancement register at
* 260 bytes offset.
*/
err = sd_write_ext_reg(card, card->ext_perf.fno, card->ext_perf.page,
err = mmc_sd_write_ext_reg(card, card->ext_perf.fno, card->ext_perf.page,
card->ext_perf.offset + 260, BIT(0));
if (err) {
pr_warn("%s: error %d writing Cache Enable bit\n",
Expand Down Expand Up @@ -1541,7 +1480,7 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
cont:
if (!oldcard) {
/* Read/parse the extension registers. */
err = sd_read_ext_regs(card);
err = mmc_sd_read_ext_regs(card);
if (err)
goto free_card;
}
Expand Down Expand Up @@ -1581,8 +1520,8 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
host->cqe_enabled = true;

if (card->ext_csd.cmdq_en) {
pr_info("%s: Command Queue Engine enabled\n",
mmc_hostname(host));
pr_info("%s: Command Queue Engine enabled, %u tags\n",
mmc_hostname(host), card->ext_csd.cmdq_depth);
} else {
host->hsq_enabled = true;
pr_info("%s: Host Software Queue enabled\n",
Expand Down Expand Up @@ -1668,7 +1607,7 @@ static int sd_busy_poweroff_notify_cb(void *cb_data, bool *busy)
* one byte offset and is one byte long. The Power Off Notification
* Ready is bit 0.
*/
err = sd_read_ext_reg(card, card->ext_power.fno, card->ext_power.page,
err = mmc_sd_read_ext_reg(card, card->ext_power.fno, card->ext_power.page,
card->ext_power.offset + 1, 1, data->reg_buf);
if (err) {
pr_warn("%s: error %d reading status reg of PM func\n",
Expand All @@ -1694,7 +1633,7 @@ static int sd_poweroff_notify(struct mmc_card *card)
* Set the Power Off Notification bit in the power management settings
* register at 2 bytes offset.
*/
err = sd_write_ext_reg(card, card->ext_power.fno, card->ext_power.page,
err = mmc_sd_write_ext_reg(card, card->ext_power.fno, card->ext_power.page,
card->ext_power.offset + 2, BIT(0));
if (err) {
pr_warn("%s: error %d writing Power Off Notify bit\n",
Expand Down
105 changes: 101 additions & 4 deletions drivers/mmc/core/sd_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/export.h>
#include <linux/ktime.h>
#include <linux/scatterlist.h>

#include <linux/mmc/host.h>
Expand Down Expand Up @@ -366,13 +367,90 @@ int mmc_app_sd_status(struct mmc_card *card, void *ssr)
return 0;
}

int sd_write_ext_reg(struct mmc_card *card, u8 fno, u8 page, u16 offset,
u8 reg_data);

int mmc_sd_write_ext_reg(struct mmc_card *card, u8 fno, u8 page, u16 offset,
u8 reg_data)
{
struct mmc_host *host = card->host;
struct mmc_request mrq = {};
struct mmc_command cmd = {};
struct mmc_data data = {};
struct scatterlist sg;
u8 *reg_buf;

reg_buf = card->ext_reg_buf;
memset(reg_buf, 0, 512);

mrq.cmd = &cmd;
mrq.data = &data;

/*
* Arguments of CMD49:
* [31:31] MIO (0 = memory).
* [30:27] FNO (function number).
* [26:26] MW - mask write mode (0 = disable).
* [25:18] page number.
* [17:9] offset address.
* [8:0] length (0 = 1 byte).
*/
cmd.arg = fno << 27 | page << 18 | offset << 9;

/* The first byte in the buffer is the data to be written. */
reg_buf[0] = reg_data;

data.flags = MMC_DATA_WRITE;
data.blksz = 512;
data.blocks = 1;
data.sg = &sg;
data.sg_len = 1;
sg_init_one(&sg, reg_buf, 512);

cmd.opcode = SD_WRITE_EXTR_SINGLE;
cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;

mmc_set_data_timeout(&data, card);
mmc_wait_for_req(host, &mrq);

/*
* Note that, the SD card is allowed to signal busy on DAT0 up to 1s
* after the CMD49. Although, let's leave this to be managed by the
* caller.
*/

if (cmd.error)
return cmd.error;
if (data.error)
return data.error;

return 0;
}

int mmc_sd_read_ext_reg(struct mmc_card *card, u8 fno, u8 page,
u16 offset, u16 len, u8 *reg_buf)
{
u32 cmd_args;

/*
* Command arguments of CMD48:
* [31:31] MIO (0 = memory).
* [30:27] FNO (function number).
* [26:26] reserved (0).
* [25:18] page number.
* [17:9] offset address.
* [8:0] length (0 = 1 byte, 1ff = 512 bytes).
*/
cmd_args = fno << 27 | page << 18 | offset << 9 | (len - 1);

return mmc_send_adtc_data(card, card->host, SD_READ_EXTR_SINGLE,
cmd_args, reg_buf, 512);
}

static int mmc_sd_cmdq_switch(struct mmc_card *card, bool enable)
{
int err;
u8 reg = 0;
u8 *reg_buf = card->ext_reg_buf;
ktime_t timeout;
/*
* SD offers two command queueing modes - sequential (in-order) and
* voluntary (out-of-order). Apps Class A2 performance is only
Expand All @@ -383,8 +461,27 @@ static int mmc_sd_cmdq_switch(struct mmc_card *card, bool enable)
reg = BIT(0);

/* Performance enhancement register byte 262 controls command queueing */
err = sd_write_ext_reg(card, card->ext_perf.fno, card->ext_perf.page,
card->ext_perf.offset + 262, reg);
err = mmc_sd_write_ext_reg(card, card->ext_perf.fno, card->ext_perf.page,
card->ext_perf.offset + 262, reg);
if (err)
goto out;

/* Poll the register - cards may have a lazy init/deinit sequence. */
timeout = ktime_add_ms(ktime_get(), 10);
while (1) {
err = mmc_sd_read_ext_reg(card, card->ext_perf.fno, card->ext_perf.page,
card->ext_perf.offset + 262, 1, reg_buf);
if (err)
break;
if ((reg_buf[0] & BIT(0)) == reg)
break;
if (ktime_after(ktime_get(), timeout)) {
err = -EBADMSG;
break;
}
usleep_range(100, 200);
}
out:
if (!err)
card->ext_csd.cmdq_en = enable;

Expand Down
Loading