Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 1 | /* |
Pierre Ossman | 70f1048 | 2007-07-11 20:04:50 +0200 | [diff] [blame] | 2 | * linux/drivers/mmc/core/mmc_ops.h |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2006-2007 Pierre Ossman |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or (at |
| 9 | * your option) any later version. |
| 10 | */ |
| 11 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Paul Gortmaker | 3ef77af | 2011-07-10 12:42:00 -0400 | [diff] [blame] | 13 | #include <linux/export.h> |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 14 | #include <linux/types.h> |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 15 | #include <linux/scatterlist.h> |
| 16 | |
| 17 | #include <linux/mmc/host.h> |
| 18 | #include <linux/mmc/card.h> |
| 19 | #include <linux/mmc/mmc.h> |
| 20 | |
| 21 | #include "core.h" |
| 22 | #include "mmc_ops.h" |
| 23 | |
Trey Ramsay | 8fee476 | 2012-11-16 09:31:41 -0600 | [diff] [blame] | 24 | #define MMC_OPS_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */ |
| 25 | |
Ulf Hansson | 04cdbbf | 2014-12-01 16:53:34 +0100 | [diff] [blame] | 26 | static const u8 tuning_blk_pattern_4bit[] = { |
| 27 | 0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc, |
| 28 | 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef, |
| 29 | 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb, |
| 30 | 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef, |
| 31 | 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c, |
| 32 | 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee, |
| 33 | 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff, |
| 34 | 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde, |
| 35 | }; |
| 36 | |
| 37 | static const u8 tuning_blk_pattern_8bit[] = { |
| 38 | 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, |
| 39 | 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc, |
| 40 | 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff, |
| 41 | 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff, |
| 42 | 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd, |
| 43 | 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb, |
| 44 | 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff, |
| 45 | 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff, |
| 46 | 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, |
| 47 | 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, |
| 48 | 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, |
| 49 | 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, |
| 50 | 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, |
| 51 | 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, |
| 52 | 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, |
| 53 | 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, |
| 54 | }; |
| 55 | |
Seungwon Jeon | a27fbf2 | 2013-09-04 21:21:05 +0900 | [diff] [blame] | 56 | static inline int __mmc_send_status(struct mmc_card *card, u32 *status, |
| 57 | bool ignore_crc) |
| 58 | { |
| 59 | int err; |
| 60 | struct mmc_command cmd = {0}; |
| 61 | |
| 62 | BUG_ON(!card); |
| 63 | BUG_ON(!card->host); |
| 64 | |
| 65 | cmd.opcode = MMC_SEND_STATUS; |
| 66 | if (!mmc_host_is_spi(card->host)) |
| 67 | cmd.arg = card->rca << 16; |
| 68 | cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC; |
| 69 | if (ignore_crc) |
| 70 | cmd.flags &= ~MMC_RSP_CRC; |
| 71 | |
| 72 | err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES); |
| 73 | if (err) |
| 74 | return err; |
| 75 | |
| 76 | /* NOTE: callers are required to understand the difference |
| 77 | * between "native" and SPI format status words! |
| 78 | */ |
| 79 | if (status) |
| 80 | *status = cmd.resp[0]; |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | int mmc_send_status(struct mmc_card *card, u32 *status) |
| 86 | { |
| 87 | return __mmc_send_status(card, status, false); |
| 88 | } |
| 89 | |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 90 | static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card) |
| 91 | { |
| 92 | int err; |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 93 | struct mmc_command cmd = {0}; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 94 | |
| 95 | BUG_ON(!host); |
| 96 | |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 97 | cmd.opcode = MMC_SELECT_CARD; |
| 98 | |
| 99 | if (card) { |
| 100 | cmd.arg = card->rca << 16; |
| 101 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 102 | } else { |
| 103 | cmd.arg = 0; |
| 104 | cmd.flags = MMC_RSP_NONE | MMC_CMD_AC; |
| 105 | } |
| 106 | |
| 107 | err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 108 | if (err) |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 109 | return err; |
| 110 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 111 | return 0; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | int mmc_select_card(struct mmc_card *card) |
| 115 | { |
| 116 | BUG_ON(!card); |
| 117 | |
| 118 | return _mmc_select_card(card->host, card); |
| 119 | } |
| 120 | |
| 121 | int mmc_deselect_cards(struct mmc_host *host) |
| 122 | { |
| 123 | return _mmc_select_card(host, NULL); |
| 124 | } |
| 125 | |
Sascha Hauer | 3d705d1 | 2014-08-19 10:45:51 +0200 | [diff] [blame] | 126 | /* |
| 127 | * Write the value specified in the device tree or board code into the optional |
| 128 | * 16 bit Driver Stage Register. This can be used to tune raise/fall times and |
| 129 | * drive strength of the DAT and CMD outputs. The actual meaning of a given |
| 130 | * value is hardware dependant. |
| 131 | * The presence of the DSR register can be determined from the CSD register, |
| 132 | * bit 76. |
| 133 | */ |
| 134 | int mmc_set_dsr(struct mmc_host *host) |
| 135 | { |
| 136 | struct mmc_command cmd = {0}; |
| 137 | |
| 138 | cmd.opcode = MMC_SET_DSR; |
| 139 | |
| 140 | cmd.arg = (host->dsr << 16) | 0xffff; |
| 141 | cmd.flags = MMC_RSP_NONE | MMC_CMD_AC; |
| 142 | |
| 143 | return mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES); |
| 144 | } |
| 145 | |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 146 | int mmc_go_idle(struct mmc_host *host) |
| 147 | { |
| 148 | int err; |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 149 | struct mmc_command cmd = {0}; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 150 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 151 | /* |
| 152 | * Non-SPI hosts need to prevent chipselect going active during |
| 153 | * GO_IDLE; that would put chips into SPI mode. Remind them of |
| 154 | * that in case of hardware that won't pull up DAT3/nCS otherwise. |
| 155 | * |
| 156 | * SPI hosts ignore ios.chip_select; it's managed according to |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 157 | * rules that must accommodate non-MMC slaves which this layer |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 158 | * won't even know about. |
| 159 | */ |
| 160 | if (!mmc_host_is_spi(host)) { |
| 161 | mmc_set_chip_select(host, MMC_CS_HIGH); |
| 162 | mmc_delay(1); |
| 163 | } |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 164 | |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 165 | cmd.opcode = MMC_GO_IDLE_STATE; |
| 166 | cmd.arg = 0; |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 167 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 168 | |
| 169 | err = mmc_wait_for_cmd(host, &cmd, 0); |
| 170 | |
| 171 | mmc_delay(1); |
| 172 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 173 | if (!mmc_host_is_spi(host)) { |
| 174 | mmc_set_chip_select(host, MMC_CS_DONTCARE); |
| 175 | mmc_delay(1); |
| 176 | } |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 177 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 178 | host->use_spi_crc = 0; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 179 | |
| 180 | return err; |
| 181 | } |
| 182 | |
| 183 | int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr) |
| 184 | { |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 185 | struct mmc_command cmd = {0}; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 186 | int i, err = 0; |
| 187 | |
| 188 | BUG_ON(!host); |
| 189 | |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 190 | cmd.opcode = MMC_SEND_OP_COND; |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 191 | cmd.arg = mmc_host_is_spi(host) ? 0 : ocr; |
| 192 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 193 | |
| 194 | for (i = 100; i; i--) { |
| 195 | err = mmc_wait_for_cmd(host, &cmd, 0); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 196 | if (err) |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 197 | break; |
| 198 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 199 | /* if we're just probing, do a single pass */ |
| 200 | if (ocr == 0) |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 201 | break; |
| 202 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 203 | /* otherwise wait until reset completes */ |
| 204 | if (mmc_host_is_spi(host)) { |
| 205 | if (!(cmd.resp[0] & R1_SPI_IDLE)) |
| 206 | break; |
| 207 | } else { |
| 208 | if (cmd.resp[0] & MMC_CARD_BUSY) |
| 209 | break; |
| 210 | } |
| 211 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 212 | err = -ETIMEDOUT; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 213 | |
| 214 | mmc_delay(10); |
| 215 | } |
| 216 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 217 | if (rocr && !mmc_host_is_spi(host)) |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 218 | *rocr = cmd.resp[0]; |
| 219 | |
| 220 | return err; |
| 221 | } |
| 222 | |
| 223 | int mmc_all_send_cid(struct mmc_host *host, u32 *cid) |
| 224 | { |
| 225 | int err; |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 226 | struct mmc_command cmd = {0}; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 227 | |
| 228 | BUG_ON(!host); |
| 229 | BUG_ON(!cid); |
| 230 | |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 231 | cmd.opcode = MMC_ALL_SEND_CID; |
| 232 | cmd.arg = 0; |
| 233 | cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; |
| 234 | |
| 235 | err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 236 | if (err) |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 237 | return err; |
| 238 | |
| 239 | memcpy(cid, cmd.resp, sizeof(u32) * 4); |
| 240 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 241 | return 0; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | int mmc_set_relative_addr(struct mmc_card *card) |
| 245 | { |
| 246 | int err; |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 247 | struct mmc_command cmd = {0}; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 248 | |
| 249 | BUG_ON(!card); |
| 250 | BUG_ON(!card->host); |
| 251 | |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 252 | cmd.opcode = MMC_SET_RELATIVE_ADDR; |
| 253 | cmd.arg = card->rca << 16; |
| 254 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 255 | |
| 256 | err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 257 | if (err) |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 258 | return err; |
| 259 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 260 | return 0; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 261 | } |
| 262 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 263 | static int |
| 264 | mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode) |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 265 | { |
| 266 | int err; |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 267 | struct mmc_command cmd = {0}; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 268 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 269 | BUG_ON(!host); |
| 270 | BUG_ON(!cxd); |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 271 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 272 | cmd.opcode = opcode; |
| 273 | cmd.arg = arg; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 274 | cmd.flags = MMC_RSP_R2 | MMC_CMD_AC; |
| 275 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 276 | err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 277 | if (err) |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 278 | return err; |
| 279 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 280 | memcpy(cxd, cmd.resp, sizeof(u32) * 4); |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 281 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 282 | return 0; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 283 | } |
| 284 | |
Kyungsik Lee | 1a41313 | 2012-08-03 00:58:03 +0000 | [diff] [blame] | 285 | /* |
| 286 | * NOTE: void *buf, caller for the buf is required to use DMA-capable |
| 287 | * buffer or on-stack buffer (with some overhead in callee). |
| 288 | */ |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 289 | static int |
| 290 | mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host, |
| 291 | u32 opcode, void *buf, unsigned len) |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 292 | { |
Venkatraman S | ad5fd97 | 2011-08-25 00:30:50 +0530 | [diff] [blame] | 293 | struct mmc_request mrq = {NULL}; |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 294 | struct mmc_command cmd = {0}; |
Chris Ball | a61ad2b | 2011-04-13 23:46:05 -0400 | [diff] [blame] | 295 | struct mmc_data data = {0}; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 296 | struct scatterlist sg; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 297 | |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 298 | mrq.cmd = &cmd; |
| 299 | mrq.data = &data; |
| 300 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 301 | cmd.opcode = opcode; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 302 | cmd.arg = 0; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 303 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 304 | /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we |
| 305 | * rely on callers to never use this with "native" calls for reading |
| 306 | * CSD or CID. Native versions of those commands use the R2 type, |
| 307 | * not R1 plus a data block. |
| 308 | */ |
| 309 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
| 310 | |
| 311 | data.blksz = len; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 312 | data.blocks = 1; |
| 313 | data.flags = MMC_DATA_READ; |
| 314 | data.sg = &sg; |
| 315 | data.sg_len = 1; |
| 316 | |
Ulf Hansson | 601ed60 | 2014-10-17 11:58:24 +0200 | [diff] [blame] | 317 | sg_init_one(&sg, buf, len); |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 318 | |
Adrian Hunter | cda56ac2 | 2009-02-10 16:32:33 +0200 | [diff] [blame] | 319 | if (opcode == MMC_SEND_CSD || opcode == MMC_SEND_CID) { |
| 320 | /* |
| 321 | * The spec states that CSR and CID accesses have a timeout |
| 322 | * of 64 clock cycles. |
| 323 | */ |
| 324 | data.timeout_ns = 0; |
| 325 | data.timeout_clks = 64; |
| 326 | } else |
| 327 | mmc_set_data_timeout(&data, card); |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 328 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 329 | mmc_wait_for_req(host, &mrq); |
| 330 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 331 | if (cmd.error) |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 332 | return cmd.error; |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 333 | if (data.error) |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 334 | return data.error; |
| 335 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 336 | return 0; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 337 | } |
| 338 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 339 | int mmc_send_csd(struct mmc_card *card, u32 *csd) |
| 340 | { |
Pierre Ossman | 78e4807 | 2007-10-27 14:14:23 +0200 | [diff] [blame] | 341 | int ret, i; |
Kyungsik Lee | 1a41313 | 2012-08-03 00:58:03 +0000 | [diff] [blame] | 342 | u32 *csd_tmp; |
Pierre Ossman | 78e4807 | 2007-10-27 14:14:23 +0200 | [diff] [blame] | 343 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 344 | if (!mmc_host_is_spi(card->host)) |
| 345 | return mmc_send_cxd_native(card->host, card->rca << 16, |
| 346 | csd, MMC_SEND_CSD); |
| 347 | |
Ulf Hansson | 22b7870 | 2014-10-17 12:00:56 +0200 | [diff] [blame] | 348 | csd_tmp = kzalloc(16, GFP_KERNEL); |
Kyungsik Lee | 1a41313 | 2012-08-03 00:58:03 +0000 | [diff] [blame] | 349 | if (!csd_tmp) |
| 350 | return -ENOMEM; |
| 351 | |
| 352 | ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd_tmp, 16); |
Pierre Ossman | 78e4807 | 2007-10-27 14:14:23 +0200 | [diff] [blame] | 353 | if (ret) |
Kyungsik Lee | 1a41313 | 2012-08-03 00:58:03 +0000 | [diff] [blame] | 354 | goto err; |
Pierre Ossman | 78e4807 | 2007-10-27 14:14:23 +0200 | [diff] [blame] | 355 | |
| 356 | for (i = 0;i < 4;i++) |
Kyungsik Lee | 1a41313 | 2012-08-03 00:58:03 +0000 | [diff] [blame] | 357 | csd[i] = be32_to_cpu(csd_tmp[i]); |
Pierre Ossman | 78e4807 | 2007-10-27 14:14:23 +0200 | [diff] [blame] | 358 | |
Kyungsik Lee | 1a41313 | 2012-08-03 00:58:03 +0000 | [diff] [blame] | 359 | err: |
| 360 | kfree(csd_tmp); |
| 361 | return ret; |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | int mmc_send_cid(struct mmc_host *host, u32 *cid) |
| 365 | { |
Pierre Ossman | 78e4807 | 2007-10-27 14:14:23 +0200 | [diff] [blame] | 366 | int ret, i; |
Kyungsik Lee | 1a41313 | 2012-08-03 00:58:03 +0000 | [diff] [blame] | 367 | u32 *cid_tmp; |
Pierre Ossman | 78e4807 | 2007-10-27 14:14:23 +0200 | [diff] [blame] | 368 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 369 | if (!mmc_host_is_spi(host)) { |
| 370 | if (!host->card) |
| 371 | return -EINVAL; |
| 372 | return mmc_send_cxd_native(host, host->card->rca << 16, |
| 373 | cid, MMC_SEND_CID); |
| 374 | } |
| 375 | |
Ulf Hansson | 22b7870 | 2014-10-17 12:00:56 +0200 | [diff] [blame] | 376 | cid_tmp = kzalloc(16, GFP_KERNEL); |
Kyungsik Lee | 1a41313 | 2012-08-03 00:58:03 +0000 | [diff] [blame] | 377 | if (!cid_tmp) |
| 378 | return -ENOMEM; |
| 379 | |
| 380 | ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid_tmp, 16); |
Pierre Ossman | 78e4807 | 2007-10-27 14:14:23 +0200 | [diff] [blame] | 381 | if (ret) |
Kyungsik Lee | 1a41313 | 2012-08-03 00:58:03 +0000 | [diff] [blame] | 382 | goto err; |
Pierre Ossman | 78e4807 | 2007-10-27 14:14:23 +0200 | [diff] [blame] | 383 | |
| 384 | for (i = 0;i < 4;i++) |
Kyungsik Lee | 1a41313 | 2012-08-03 00:58:03 +0000 | [diff] [blame] | 385 | cid[i] = be32_to_cpu(cid_tmp[i]); |
Pierre Ossman | 78e4807 | 2007-10-27 14:14:23 +0200 | [diff] [blame] | 386 | |
Kyungsik Lee | 1a41313 | 2012-08-03 00:58:03 +0000 | [diff] [blame] | 387 | err: |
| 388 | kfree(cid_tmp); |
| 389 | return ret; |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Ulf Hansson | e21aa51 | 2014-10-17 11:32:32 +0200 | [diff] [blame] | 392 | int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd) |
| 393 | { |
| 394 | int err; |
| 395 | u8 *ext_csd; |
| 396 | |
| 397 | if (!card || !new_ext_csd) |
| 398 | return -EINVAL; |
| 399 | |
| 400 | if (!mmc_can_ext_csd(card)) |
| 401 | return -EOPNOTSUPP; |
| 402 | |
| 403 | /* |
| 404 | * As the ext_csd is so large and mostly unused, we don't store the |
| 405 | * raw block in mmc_card. |
| 406 | */ |
Ulf Hansson | 22b7870 | 2014-10-17 12:00:56 +0200 | [diff] [blame] | 407 | ext_csd = kzalloc(512, GFP_KERNEL); |
Ulf Hansson | e21aa51 | 2014-10-17 11:32:32 +0200 | [diff] [blame] | 408 | if (!ext_csd) |
| 409 | return -ENOMEM; |
| 410 | |
Ulf Hansson | 2fc91e8 | 2014-10-17 11:54:22 +0200 | [diff] [blame] | 411 | err = mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD, ext_csd, |
| 412 | 512); |
Ulf Hansson | e21aa51 | 2014-10-17 11:32:32 +0200 | [diff] [blame] | 413 | if (err) |
| 414 | kfree(ext_csd); |
| 415 | else |
| 416 | *new_ext_csd = ext_csd; |
| 417 | |
| 418 | return err; |
| 419 | } |
| 420 | EXPORT_SYMBOL_GPL(mmc_get_ext_csd); |
| 421 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 422 | int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp) |
| 423 | { |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 424 | struct mmc_command cmd = {0}; |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 425 | int err; |
| 426 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 427 | cmd.opcode = MMC_SPI_READ_OCR; |
| 428 | cmd.arg = highcap ? (1 << 30) : 0; |
| 429 | cmd.flags = MMC_RSP_SPI_R3; |
| 430 | |
| 431 | err = mmc_wait_for_cmd(host, &cmd, 0); |
| 432 | |
| 433 | *ocrp = cmd.resp[1]; |
| 434 | return err; |
| 435 | } |
| 436 | |
| 437 | int mmc_spi_set_crc(struct mmc_host *host, int use_crc) |
| 438 | { |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 439 | struct mmc_command cmd = {0}; |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 440 | int err; |
| 441 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 442 | cmd.opcode = MMC_SPI_CRC_ON_OFF; |
| 443 | cmd.flags = MMC_RSP_SPI_R1; |
| 444 | cmd.arg = use_crc; |
| 445 | |
| 446 | err = mmc_wait_for_cmd(host, &cmd, 0); |
| 447 | if (!err) |
| 448 | host->use_spi_crc = use_crc; |
| 449 | return err; |
| 450 | } |
| 451 | |
Andrei Warkentin | d3a8d95 | 2011-04-11 16:13:43 -0500 | [diff] [blame] | 452 | /** |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 453 | * __mmc_switch - modify EXT_CSD register |
Andrei Warkentin | d3a8d95 | 2011-04-11 16:13:43 -0500 | [diff] [blame] | 454 | * @card: the MMC card associated with the data transfer |
| 455 | * @set: cmd set values |
| 456 | * @index: EXT_CSD register index |
| 457 | * @value: value to program into EXT_CSD register |
| 458 | * @timeout_ms: timeout (ms) for operation performed by register write, |
| 459 | * timeout of zero implies maximum possible timeout |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 460 | * @use_busy_signal: use the busy signal as response type |
Ulf Hansson | 878e200 | 2013-09-09 11:57:57 +0200 | [diff] [blame] | 461 | * @send_status: send status cmd to poll for busy |
Ulf Hansson | 4509f8477 | 2014-01-08 16:09:33 +0100 | [diff] [blame] | 462 | * @ignore_crc: ignore CRC errors when sending status cmd to poll for busy |
Andrei Warkentin | d3a8d95 | 2011-04-11 16:13:43 -0500 | [diff] [blame] | 463 | * |
| 464 | * Modifies the EXT_CSD register for selected card. |
| 465 | */ |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 466 | int __mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value, |
Ulf Hansson | 4509f8477 | 2014-01-08 16:09:33 +0100 | [diff] [blame] | 467 | unsigned int timeout_ms, bool use_busy_signal, bool send_status, |
| 468 | bool ignore_crc) |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 469 | { |
Ulf Hansson | 636bd13 | 2014-01-28 14:05:39 +0100 | [diff] [blame] | 470 | struct mmc_host *host = card->host; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 471 | int err; |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 472 | struct mmc_command cmd = {0}; |
Trey Ramsay | 8fee476 | 2012-11-16 09:31:41 -0600 | [diff] [blame] | 473 | unsigned long timeout; |
Ulf Hansson | ecd3a7d | 2013-10-28 02:51:41 +0100 | [diff] [blame] | 474 | u32 status = 0; |
Ulf Hansson | b9ec261 | 2014-01-28 14:15:34 +0100 | [diff] [blame] | 475 | bool use_r1b_resp = use_busy_signal; |
| 476 | |
| 477 | /* |
| 478 | * If the cmd timeout and the max_busy_timeout of the host are both |
| 479 | * specified, let's validate them. A failure means we need to prevent |
| 480 | * the host from doing hw busy detection, which is done by converting |
| 481 | * to a R1 response instead of a R1B. |
| 482 | */ |
| 483 | if (timeout_ms && host->max_busy_timeout && |
| 484 | (timeout_ms > host->max_busy_timeout)) |
| 485 | use_r1b_resp = false; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 486 | |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 487 | cmd.opcode = MMC_SWITCH; |
| 488 | cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | |
| 489 | (index << 16) | |
| 490 | (value << 8) | |
| 491 | set; |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 492 | cmd.flags = MMC_CMD_AC; |
Ulf Hansson | b9ec261 | 2014-01-28 14:15:34 +0100 | [diff] [blame] | 493 | if (use_r1b_resp) { |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 494 | cmd.flags |= MMC_RSP_SPI_R1B | MMC_RSP_R1B; |
Ulf Hansson | b9ec261 | 2014-01-28 14:15:34 +0100 | [diff] [blame] | 495 | /* |
| 496 | * A busy_timeout of zero means the host can decide to use |
| 497 | * whatever value it finds suitable. |
| 498 | */ |
| 499 | cmd.busy_timeout = timeout_ms; |
| 500 | } else { |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 501 | cmd.flags |= MMC_RSP_SPI_R1 | MMC_RSP_R1; |
Ulf Hansson | b9ec261 | 2014-01-28 14:15:34 +0100 | [diff] [blame] | 502 | } |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 503 | |
Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 504 | if (index == EXT_CSD_SANITIZE_START) |
| 505 | cmd.sanitize_busy = true; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 506 | |
Ulf Hansson | 636bd13 | 2014-01-28 14:05:39 +0100 | [diff] [blame] | 507 | err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 508 | if (err) |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 509 | return err; |
| 510 | |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 511 | /* No need to check card status in case of unblocking command */ |
| 512 | if (!use_busy_signal) |
| 513 | return 0; |
| 514 | |
Seungwon Jeon | a27fbf2 | 2013-09-04 21:21:05 +0900 | [diff] [blame] | 515 | /* |
Ulf Hansson | 4509f8477 | 2014-01-08 16:09:33 +0100 | [diff] [blame] | 516 | * CRC errors shall only be ignored in cases were CMD13 is used to poll |
| 517 | * to detect busy completion. |
Seungwon Jeon | a27fbf2 | 2013-09-04 21:21:05 +0900 | [diff] [blame] | 518 | */ |
Ulf Hansson | b9ec261 | 2014-01-28 14:15:34 +0100 | [diff] [blame] | 519 | if ((host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp) |
Ulf Hansson | 4509f8477 | 2014-01-08 16:09:33 +0100 | [diff] [blame] | 520 | ignore_crc = false; |
Seungwon Jeon | a27fbf2 | 2013-09-04 21:21:05 +0900 | [diff] [blame] | 521 | |
Ulf Hansson | b9ec261 | 2014-01-28 14:15:34 +0100 | [diff] [blame] | 522 | /* We have an unspecified cmd timeout, use the fallback value. */ |
| 523 | if (!timeout_ms) |
| 524 | timeout_ms = MMC_OPS_TIMEOUT_MS; |
| 525 | |
Ulf Hansson | 4509f8477 | 2014-01-08 16:09:33 +0100 | [diff] [blame] | 526 | /* Must check status to be sure of no errors. */ |
Ulf Hansson | b9ec261 | 2014-01-28 14:15:34 +0100 | [diff] [blame] | 527 | timeout = jiffies + msecs_to_jiffies(timeout_ms); |
Adrian Hunter | ef0b27d | 2009-09-22 16:44:37 -0700 | [diff] [blame] | 528 | do { |
Ulf Hansson | 878e200 | 2013-09-09 11:57:57 +0200 | [diff] [blame] | 529 | if (send_status) { |
| 530 | err = __mmc_send_status(card, &status, ignore_crc); |
| 531 | if (err) |
| 532 | return err; |
| 533 | } |
Ulf Hansson | b9ec261 | 2014-01-28 14:15:34 +0100 | [diff] [blame] | 534 | if ((host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp) |
Adrian Hunter | ef0b27d | 2009-09-22 16:44:37 -0700 | [diff] [blame] | 535 | break; |
Ulf Hansson | 636bd13 | 2014-01-28 14:05:39 +0100 | [diff] [blame] | 536 | if (mmc_host_is_spi(host)) |
Adrian Hunter | ef0b27d | 2009-09-22 16:44:37 -0700 | [diff] [blame] | 537 | break; |
Trey Ramsay | 8fee476 | 2012-11-16 09:31:41 -0600 | [diff] [blame] | 538 | |
Ulf Hansson | 878e200 | 2013-09-09 11:57:57 +0200 | [diff] [blame] | 539 | /* |
| 540 | * We are not allowed to issue a status command and the host |
| 541 | * does'nt support MMC_CAP_WAIT_WHILE_BUSY, then we can only |
| 542 | * rely on waiting for the stated timeout to be sufficient. |
| 543 | */ |
| 544 | if (!send_status) { |
| 545 | mmc_delay(timeout_ms); |
| 546 | return 0; |
| 547 | } |
| 548 | |
Trey Ramsay | 8fee476 | 2012-11-16 09:31:41 -0600 | [diff] [blame] | 549 | /* Timeout if the device never leaves the program state. */ |
| 550 | if (time_after(jiffies, timeout)) { |
| 551 | pr_err("%s: Card stuck in programming state! %s\n", |
Ulf Hansson | 636bd13 | 2014-01-28 14:05:39 +0100 | [diff] [blame] | 552 | mmc_hostname(host), __func__); |
Trey Ramsay | 8fee476 | 2012-11-16 09:31:41 -0600 | [diff] [blame] | 553 | return -ETIMEDOUT; |
| 554 | } |
Jaehoon Chung | 7435bb7 | 2011-08-10 18:46:28 +0900 | [diff] [blame] | 555 | } while (R1_CURRENT_STATE(status) == R1_STATE_PRG); |
Adrian Hunter | ef0b27d | 2009-09-22 16:44:37 -0700 | [diff] [blame] | 556 | |
Ulf Hansson | 636bd13 | 2014-01-28 14:05:39 +0100 | [diff] [blame] | 557 | if (mmc_host_is_spi(host)) { |
Adrian Hunter | ef0b27d | 2009-09-22 16:44:37 -0700 | [diff] [blame] | 558 | if (status & R1_SPI_ILLEGAL_COMMAND) |
| 559 | return -EBADMSG; |
| 560 | } else { |
| 561 | if (status & 0xFDFFA000) |
Ulf Hansson | 636bd13 | 2014-01-28 14:05:39 +0100 | [diff] [blame] | 562 | pr_warn("%s: unexpected status %#x after switch\n", |
| 563 | mmc_hostname(host), status); |
Adrian Hunter | ef0b27d | 2009-09-22 16:44:37 -0700 | [diff] [blame] | 564 | if (status & R1_SWITCH_ERROR) |
| 565 | return -EBADMSG; |
| 566 | } |
| 567 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 568 | return 0; |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 569 | } |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 570 | EXPORT_SYMBOL_GPL(__mmc_switch); |
| 571 | |
| 572 | int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value, |
| 573 | unsigned int timeout_ms) |
| 574 | { |
Ulf Hansson | 4509f8477 | 2014-01-08 16:09:33 +0100 | [diff] [blame] | 575 | return __mmc_switch(card, set, index, value, timeout_ms, true, true, |
| 576 | false); |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 577 | } |
Andrei Warkentin | d3a8d95 | 2011-04-11 16:13:43 -0500 | [diff] [blame] | 578 | EXPORT_SYMBOL_GPL(mmc_switch); |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 579 | |
Ulf Hansson | fe5afb13d | 2014-12-05 11:31:22 +0100 | [diff] [blame] | 580 | int mmc_send_tuning(struct mmc_host *host) |
Minda Chen | 996903d | 2014-11-26 13:05:33 +0800 | [diff] [blame] | 581 | { |
| 582 | struct mmc_request mrq = {NULL}; |
| 583 | struct mmc_command cmd = {0}; |
| 584 | struct mmc_data data = {0}; |
| 585 | struct scatterlist sg; |
Ulf Hansson | fe5afb13d | 2014-12-05 11:31:22 +0100 | [diff] [blame] | 586 | struct mmc_ios *ios = &host->ios; |
Minda Chen | 996903d | 2014-11-26 13:05:33 +0800 | [diff] [blame] | 587 | const u8 *tuning_block_pattern; |
| 588 | int size, err = 0; |
| 589 | u8 *data_buf; |
| 590 | u32 opcode; |
| 591 | |
| 592 | if (ios->bus_width == MMC_BUS_WIDTH_8) { |
| 593 | tuning_block_pattern = tuning_blk_pattern_8bit; |
| 594 | size = sizeof(tuning_blk_pattern_8bit); |
| 595 | opcode = MMC_SEND_TUNING_BLOCK_HS200; |
| 596 | } else if (ios->bus_width == MMC_BUS_WIDTH_4) { |
| 597 | tuning_block_pattern = tuning_blk_pattern_4bit; |
| 598 | size = sizeof(tuning_blk_pattern_4bit); |
| 599 | opcode = MMC_SEND_TUNING_BLOCK; |
| 600 | } else |
| 601 | return -EINVAL; |
| 602 | |
| 603 | data_buf = kzalloc(size, GFP_KERNEL); |
| 604 | if (!data_buf) |
| 605 | return -ENOMEM; |
| 606 | |
| 607 | mrq.cmd = &cmd; |
| 608 | mrq.data = &data; |
| 609 | |
| 610 | cmd.opcode = opcode; |
| 611 | cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; |
| 612 | |
| 613 | data.blksz = size; |
| 614 | data.blocks = 1; |
| 615 | data.flags = MMC_DATA_READ; |
| 616 | |
| 617 | /* |
| 618 | * According to the tuning specs, Tuning process |
| 619 | * is normally shorter 40 executions of CMD19, |
| 620 | * and timeout value should be shorter than 150 ms |
| 621 | */ |
| 622 | data.timeout_ns = 150 * NSEC_PER_MSEC; |
| 623 | |
| 624 | data.sg = &sg; |
| 625 | data.sg_len = 1; |
| 626 | sg_init_one(&sg, data_buf, size); |
| 627 | |
Ulf Hansson | fe5afb13d | 2014-12-05 11:31:22 +0100 | [diff] [blame] | 628 | mmc_wait_for_req(host, &mrq); |
Minda Chen | 996903d | 2014-11-26 13:05:33 +0800 | [diff] [blame] | 629 | |
| 630 | if (cmd.error) { |
| 631 | err = cmd.error; |
| 632 | goto out; |
| 633 | } |
| 634 | |
| 635 | if (data.error) { |
| 636 | err = data.error; |
| 637 | goto out; |
| 638 | } |
| 639 | |
| 640 | if (memcmp(data_buf, tuning_block_pattern, size)) |
| 641 | err = -EIO; |
| 642 | |
| 643 | out: |
| 644 | kfree(data_buf); |
| 645 | return err; |
| 646 | } |
| 647 | EXPORT_SYMBOL_GPL(mmc_send_tuning); |
| 648 | |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 649 | static int |
| 650 | mmc_send_bus_test(struct mmc_card *card, struct mmc_host *host, u8 opcode, |
| 651 | u8 len) |
| 652 | { |
Venkatraman S | ad5fd97 | 2011-08-25 00:30:50 +0530 | [diff] [blame] | 653 | struct mmc_request mrq = {NULL}; |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 654 | struct mmc_command cmd = {0}; |
Chris Ball | a61ad2b | 2011-04-13 23:46:05 -0400 | [diff] [blame] | 655 | struct mmc_data data = {0}; |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 656 | struct scatterlist sg; |
| 657 | u8 *data_buf; |
| 658 | u8 *test_buf; |
| 659 | int i, err; |
| 660 | static u8 testdata_8bit[8] = { 0x55, 0xaa, 0, 0, 0, 0, 0, 0 }; |
| 661 | static u8 testdata_4bit[4] = { 0x5a, 0, 0, 0 }; |
| 662 | |
| 663 | /* dma onto stack is unsafe/nonportable, but callers to this |
| 664 | * routine normally provide temporary on-stack buffers ... |
| 665 | */ |
| 666 | data_buf = kmalloc(len, GFP_KERNEL); |
| 667 | if (!data_buf) |
| 668 | return -ENOMEM; |
| 669 | |
| 670 | if (len == 8) |
| 671 | test_buf = testdata_8bit; |
| 672 | else if (len == 4) |
| 673 | test_buf = testdata_4bit; |
| 674 | else { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 675 | pr_err("%s: Invalid bus_width %d\n", |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 676 | mmc_hostname(host), len); |
| 677 | kfree(data_buf); |
| 678 | return -EINVAL; |
| 679 | } |
| 680 | |
| 681 | if (opcode == MMC_BUS_TEST_W) |
| 682 | memcpy(data_buf, test_buf, len); |
| 683 | |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 684 | mrq.cmd = &cmd; |
| 685 | mrq.data = &data; |
| 686 | cmd.opcode = opcode; |
| 687 | cmd.arg = 0; |
| 688 | |
| 689 | /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we |
| 690 | * rely on callers to never use this with "native" calls for reading |
| 691 | * CSD or CID. Native versions of those commands use the R2 type, |
| 692 | * not R1 plus a data block. |
| 693 | */ |
| 694 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
| 695 | |
| 696 | data.blksz = len; |
| 697 | data.blocks = 1; |
| 698 | if (opcode == MMC_BUS_TEST_R) |
| 699 | data.flags = MMC_DATA_READ; |
| 700 | else |
| 701 | data.flags = MMC_DATA_WRITE; |
| 702 | |
| 703 | data.sg = &sg; |
| 704 | data.sg_len = 1; |
Minjian Wu | 84532e3 | 2013-08-24 23:25:12 -0400 | [diff] [blame] | 705 | mmc_set_data_timeout(&data, card); |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 706 | sg_init_one(&sg, data_buf, len); |
| 707 | mmc_wait_for_req(host, &mrq); |
| 708 | err = 0; |
| 709 | if (opcode == MMC_BUS_TEST_R) { |
| 710 | for (i = 0; i < len / 4; i++) |
| 711 | if ((test_buf[i] ^ data_buf[i]) != 0xff) { |
| 712 | err = -EIO; |
| 713 | break; |
| 714 | } |
| 715 | } |
| 716 | kfree(data_buf); |
| 717 | |
| 718 | if (cmd.error) |
| 719 | return cmd.error; |
| 720 | if (data.error) |
| 721 | return data.error; |
| 722 | |
| 723 | return err; |
| 724 | } |
| 725 | |
| 726 | int mmc_bus_test(struct mmc_card *card, u8 bus_width) |
| 727 | { |
| 728 | int err, width; |
| 729 | |
| 730 | if (bus_width == MMC_BUS_WIDTH_8) |
| 731 | width = 8; |
| 732 | else if (bus_width == MMC_BUS_WIDTH_4) |
| 733 | width = 4; |
| 734 | else if (bus_width == MMC_BUS_WIDTH_1) |
| 735 | return 0; /* no need for test */ |
| 736 | else |
| 737 | return -EINVAL; |
| 738 | |
| 739 | /* |
| 740 | * Ignore errors from BUS_TEST_W. BUS_TEST_R will fail if there |
| 741 | * is a problem. This improves chances that the test will work. |
| 742 | */ |
| 743 | mmc_send_bus_test(card, card->host, MMC_BUS_TEST_W, width); |
| 744 | err = mmc_send_bus_test(card, card->host, MMC_BUS_TEST_R, width); |
| 745 | return err; |
| 746 | } |
Jaehoon Chung | eb0d8f1 | 2011-10-18 01:26:42 -0400 | [diff] [blame] | 747 | |
| 748 | int mmc_send_hpi_cmd(struct mmc_card *card, u32 *status) |
| 749 | { |
| 750 | struct mmc_command cmd = {0}; |
| 751 | unsigned int opcode; |
Jaehoon Chung | eb0d8f1 | 2011-10-18 01:26:42 -0400 | [diff] [blame] | 752 | int err; |
| 753 | |
Jaehoon Chung | 2378975 | 2012-03-12 08:57:27 +0900 | [diff] [blame] | 754 | if (!card->ext_csd.hpi) { |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 755 | pr_warn("%s: Card didn't support HPI command\n", |
| 756 | mmc_hostname(card->host)); |
Jaehoon Chung | 2378975 | 2012-03-12 08:57:27 +0900 | [diff] [blame] | 757 | return -EINVAL; |
| 758 | } |
| 759 | |
Jaehoon Chung | eb0d8f1 | 2011-10-18 01:26:42 -0400 | [diff] [blame] | 760 | opcode = card->ext_csd.hpi_cmd; |
| 761 | if (opcode == MMC_STOP_TRANSMISSION) |
Jaehoon Chung | 2378975 | 2012-03-12 08:57:27 +0900 | [diff] [blame] | 762 | cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; |
Jaehoon Chung | eb0d8f1 | 2011-10-18 01:26:42 -0400 | [diff] [blame] | 763 | else if (opcode == MMC_SEND_STATUS) |
Jaehoon Chung | 2378975 | 2012-03-12 08:57:27 +0900 | [diff] [blame] | 764 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
Jaehoon Chung | eb0d8f1 | 2011-10-18 01:26:42 -0400 | [diff] [blame] | 765 | |
| 766 | cmd.opcode = opcode; |
| 767 | cmd.arg = card->rca << 16 | 1; |
Jaehoon Chung | eb0d8f1 | 2011-10-18 01:26:42 -0400 | [diff] [blame] | 768 | |
| 769 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 770 | if (err) { |
| 771 | pr_warn("%s: error %d interrupting operation. " |
| 772 | "HPI command response %#x\n", mmc_hostname(card->host), |
| 773 | err, cmd.resp[0]); |
| 774 | return err; |
| 775 | } |
| 776 | if (status) |
| 777 | *status = cmd.resp[0]; |
| 778 | |
| 779 | return 0; |
| 780 | } |
Ulf Hansson | 148bcab | 2014-10-20 11:33:53 +0200 | [diff] [blame] | 781 | |
| 782 | int mmc_can_ext_csd(struct mmc_card *card) |
| 783 | { |
| 784 | return (card && card->csd.mmca_vsn > CSD_SPEC_VER_3); |
| 785 | } |