Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008, Freescale Semiconductor, Inc |
| 3 | * Andy Fleming |
| 4 | * |
| 5 | * Based vaguely on the Linux code |
| 6 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <config.h> |
| 11 | #include <common.h> |
| 12 | #include <command.h> |
Stephen Warren | bf0c785 | 2014-05-23 12:47:06 -0600 | [diff] [blame] | 13 | #include <errno.h> |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 14 | #include <mmc.h> |
| 15 | #include <part.h> |
| 16 | #include <malloc.h> |
| 17 | #include <linux/list.h> |
Rabin Vincent | 69d4e2c | 2009-04-05 13:30:54 +0530 | [diff] [blame] | 18 | #include <div64.h> |
Paul Burton | 8d30cc9 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 19 | #include "mmc_private.h" |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 20 | |
| 21 | static struct list_head mmc_devices; |
| 22 | static int cur_dev_num = -1; |
| 23 | |
Jeroen Hofstee | aedeeaa | 2014-07-12 21:24:08 +0200 | [diff] [blame] | 24 | __weak int board_mmc_getwp(struct mmc *mmc) |
Nikita Kiryanov | 020f261 | 2012-12-03 02:19:46 +0000 | [diff] [blame] | 25 | { |
| 26 | return -1; |
| 27 | } |
| 28 | |
| 29 | int mmc_getwp(struct mmc *mmc) |
| 30 | { |
| 31 | int wp; |
| 32 | |
| 33 | wp = board_mmc_getwp(mmc); |
| 34 | |
Peter Korsgaard | f7b1510 | 2013-03-21 04:00:03 +0000 | [diff] [blame] | 35 | if (wp < 0) { |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 36 | if (mmc->cfg->ops->getwp) |
| 37 | wp = mmc->cfg->ops->getwp(mmc); |
Peter Korsgaard | f7b1510 | 2013-03-21 04:00:03 +0000 | [diff] [blame] | 38 | else |
| 39 | wp = 0; |
| 40 | } |
Nikita Kiryanov | 020f261 | 2012-12-03 02:19:46 +0000 | [diff] [blame] | 41 | |
| 42 | return wp; |
| 43 | } |
| 44 | |
Jeroen Hofstee | 4772630 | 2014-07-10 22:46:28 +0200 | [diff] [blame] | 45 | __weak int board_mmc_getcd(struct mmc *mmc) |
| 46 | { |
Stefano Babic | 6e00edf | 2010-02-05 15:04:43 +0100 | [diff] [blame] | 47 | return -1; |
| 48 | } |
| 49 | |
Paul Burton | 8d30cc9 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 50 | int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 51 | { |
Raffaele Recalcati | 894b1e2 | 2011-03-11 02:01:14 +0000 | [diff] [blame] | 52 | int ret; |
Marek Vasut | dccb608 | 2012-03-15 18:41:35 +0000 | [diff] [blame] | 53 | |
Marek Vasut | dccb608 | 2012-03-15 18:41:35 +0000 | [diff] [blame] | 54 | #ifdef CONFIG_MMC_TRACE |
Raffaele Recalcati | 894b1e2 | 2011-03-11 02:01:14 +0000 | [diff] [blame] | 55 | int i; |
| 56 | u8 *ptr; |
| 57 | |
| 58 | printf("CMD_SEND:%d\n", cmd->cmdidx); |
| 59 | printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg); |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 60 | ret = mmc->cfg->ops->send_cmd(mmc, cmd, data); |
Raffaele Recalcati | 894b1e2 | 2011-03-11 02:01:14 +0000 | [diff] [blame] | 61 | switch (cmd->resp_type) { |
| 62 | case MMC_RSP_NONE: |
| 63 | printf("\t\tMMC_RSP_NONE\n"); |
| 64 | break; |
| 65 | case MMC_RSP_R1: |
| 66 | printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n", |
| 67 | cmd->response[0]); |
| 68 | break; |
| 69 | case MMC_RSP_R1b: |
| 70 | printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n", |
| 71 | cmd->response[0]); |
| 72 | break; |
| 73 | case MMC_RSP_R2: |
| 74 | printf("\t\tMMC_RSP_R2\t\t 0x%08X \n", |
| 75 | cmd->response[0]); |
| 76 | printf("\t\t \t\t 0x%08X \n", |
| 77 | cmd->response[1]); |
| 78 | printf("\t\t \t\t 0x%08X \n", |
| 79 | cmd->response[2]); |
| 80 | printf("\t\t \t\t 0x%08X \n", |
| 81 | cmd->response[3]); |
| 82 | printf("\n"); |
| 83 | printf("\t\t\t\t\tDUMPING DATA\n"); |
| 84 | for (i = 0; i < 4; i++) { |
| 85 | int j; |
| 86 | printf("\t\t\t\t\t%03d - ", i*4); |
Dirk Behme | c9cb4a9 | 2012-03-08 02:35:34 +0000 | [diff] [blame] | 87 | ptr = (u8 *)&cmd->response[i]; |
Raffaele Recalcati | 894b1e2 | 2011-03-11 02:01:14 +0000 | [diff] [blame] | 88 | ptr += 3; |
| 89 | for (j = 0; j < 4; j++) |
| 90 | printf("%02X ", *ptr--); |
| 91 | printf("\n"); |
| 92 | } |
| 93 | break; |
| 94 | case MMC_RSP_R3: |
| 95 | printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n", |
| 96 | cmd->response[0]); |
| 97 | break; |
| 98 | default: |
| 99 | printf("\t\tERROR MMC rsp not supported\n"); |
| 100 | break; |
| 101 | } |
Raffaele Recalcati | 894b1e2 | 2011-03-11 02:01:14 +0000 | [diff] [blame] | 102 | #else |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 103 | ret = mmc->cfg->ops->send_cmd(mmc, cmd, data); |
Raffaele Recalcati | 894b1e2 | 2011-03-11 02:01:14 +0000 | [diff] [blame] | 104 | #endif |
Marek Vasut | dccb608 | 2012-03-15 18:41:35 +0000 | [diff] [blame] | 105 | return ret; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 106 | } |
| 107 | |
Paul Burton | 8d30cc9 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 108 | int mmc_send_status(struct mmc *mmc, int timeout) |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 109 | { |
| 110 | struct mmc_cmd cmd; |
Jan Kloetzke | 3178932 | 2012-02-05 22:29:12 +0000 | [diff] [blame] | 111 | int err, retries = 5; |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 112 | #ifdef CONFIG_MMC_TRACE |
| 113 | int status; |
| 114 | #endif |
| 115 | |
| 116 | cmd.cmdidx = MMC_CMD_SEND_STATUS; |
| 117 | cmd.resp_type = MMC_RSP_R1; |
Marek Vasut | c442739 | 2011-08-10 09:24:48 +0200 | [diff] [blame] | 118 | if (!mmc_host_is_spi(mmc)) |
| 119 | cmd.cmdarg = mmc->rca << 16; |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 120 | |
Andrew Gabbasov | 034857c | 2015-03-19 07:44:06 -0500 | [diff] [blame] | 121 | while (1) { |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 122 | err = mmc_send_cmd(mmc, &cmd, NULL); |
Jan Kloetzke | 3178932 | 2012-02-05 22:29:12 +0000 | [diff] [blame] | 123 | if (!err) { |
| 124 | if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) && |
| 125 | (cmd.response[0] & MMC_STATUS_CURR_STATE) != |
| 126 | MMC_STATE_PRG) |
| 127 | break; |
| 128 | else if (cmd.response[0] & MMC_STATUS_MASK) { |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 129 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
Jan Kloetzke | 3178932 | 2012-02-05 22:29:12 +0000 | [diff] [blame] | 130 | printf("Status Error: 0x%08X\n", |
| 131 | cmd.response[0]); |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 132 | #endif |
Jan Kloetzke | 3178932 | 2012-02-05 22:29:12 +0000 | [diff] [blame] | 133 | return COMM_ERR; |
| 134 | } |
| 135 | } else if (--retries < 0) |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 136 | return err; |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 137 | |
Andrew Gabbasov | 034857c | 2015-03-19 07:44:06 -0500 | [diff] [blame] | 138 | if (timeout-- <= 0) |
| 139 | break; |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 140 | |
Andrew Gabbasov | 034857c | 2015-03-19 07:44:06 -0500 | [diff] [blame] | 141 | udelay(1000); |
| 142 | } |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 143 | |
Raffaele Recalcati | 894b1e2 | 2011-03-11 02:01:14 +0000 | [diff] [blame] | 144 | #ifdef CONFIG_MMC_TRACE |
| 145 | status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9; |
| 146 | printf("CURR STATE:%d\n", status); |
| 147 | #endif |
Jongman Heo | 1be00d9 | 2012-06-03 21:32:13 +0000 | [diff] [blame] | 148 | if (timeout <= 0) { |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 149 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 150 | printf("Timeout waiting card ready\n"); |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 151 | #endif |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 152 | return TIMEOUT; |
| 153 | } |
Andrew Gabbasov | e80682f | 2014-04-03 04:34:32 -0500 | [diff] [blame] | 154 | if (cmd.response[0] & MMC_STATUS_SWITCH_ERROR) |
| 155 | return SWITCH_ERR; |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
Paul Burton | 8d30cc9 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 160 | int mmc_set_blocklen(struct mmc *mmc, int len) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 161 | { |
| 162 | struct mmc_cmd cmd; |
| 163 | |
Andrew Gabbasov | 9fc2a41 | 2014-12-01 06:59:09 -0600 | [diff] [blame] | 164 | if (mmc->ddr_mode) |
Jaehoon Chung | 38ce30b | 2014-05-16 13:59:54 +0900 | [diff] [blame] | 165 | return 0; |
| 166 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 167 | cmd.cmdidx = MMC_CMD_SET_BLOCKLEN; |
| 168 | cmd.resp_type = MMC_RSP_R1; |
| 169 | cmd.cmdarg = len; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 170 | |
| 171 | return mmc_send_cmd(mmc, &cmd, NULL); |
| 172 | } |
| 173 | |
| 174 | struct mmc *find_mmc_device(int dev_num) |
| 175 | { |
| 176 | struct mmc *m; |
| 177 | struct list_head *entry; |
| 178 | |
| 179 | list_for_each(entry, &mmc_devices) { |
| 180 | m = list_entry(entry, struct mmc, link); |
| 181 | |
| 182 | if (m->block_dev.dev == dev_num) |
| 183 | return m; |
| 184 | } |
| 185 | |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 186 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 187 | printf("MMC Device %d not found\n", dev_num); |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 188 | #endif |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 189 | |
| 190 | return NULL; |
| 191 | } |
| 192 | |
Sascha Silbe | 4bdf6fd | 2013-06-14 13:07:25 +0200 | [diff] [blame] | 193 | static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start, |
Kim Phillips | 87ea389 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 194 | lbaint_t blkcnt) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 195 | { |
| 196 | struct mmc_cmd cmd; |
| 197 | struct mmc_data data; |
| 198 | |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 199 | if (blkcnt > 1) |
| 200 | cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK; |
| 201 | else |
| 202 | cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 203 | |
| 204 | if (mmc->high_capacity) |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 205 | cmd.cmdarg = start; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 206 | else |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 207 | cmd.cmdarg = start * mmc->read_bl_len; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 208 | |
| 209 | cmd.resp_type = MMC_RSP_R1; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 210 | |
| 211 | data.dest = dst; |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 212 | data.blocks = blkcnt; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 213 | data.blocksize = mmc->read_bl_len; |
| 214 | data.flags = MMC_DATA_READ; |
| 215 | |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 216 | if (mmc_send_cmd(mmc, &cmd, &data)) |
| 217 | return 0; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 218 | |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 219 | if (blkcnt > 1) { |
| 220 | cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; |
| 221 | cmd.cmdarg = 0; |
| 222 | cmd.resp_type = MMC_RSP_R1b; |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 223 | if (mmc_send_cmd(mmc, &cmd, NULL)) { |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 224 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 225 | printf("mmc fail to send stop cmd\n"); |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 226 | #endif |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 227 | return 0; |
| 228 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 229 | } |
| 230 | |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 231 | return blkcnt; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 232 | } |
| 233 | |
Sascha Silbe | 4bdf6fd | 2013-06-14 13:07:25 +0200 | [diff] [blame] | 234 | static ulong mmc_bread(int dev_num, lbaint_t start, lbaint_t blkcnt, void *dst) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 235 | { |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 236 | lbaint_t cur, blocks_todo = blkcnt; |
| 237 | |
| 238 | if (blkcnt == 0) |
| 239 | return 0; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 240 | |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 241 | struct mmc *mmc = find_mmc_device(dev_num); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 242 | if (!mmc) |
| 243 | return 0; |
| 244 | |
Lei Wen | e1cc9c8 | 2010-09-13 22:07:27 +0800 | [diff] [blame] | 245 | if ((start + blkcnt) > mmc->block_dev.lba) { |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 246 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
Sascha Silbe | 4bdf6fd | 2013-06-14 13:07:25 +0200 | [diff] [blame] | 247 | printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n", |
Lei Wen | e1cc9c8 | 2010-09-13 22:07:27 +0800 | [diff] [blame] | 248 | start + blkcnt, mmc->block_dev.lba); |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 249 | #endif |
Lei Wen | e1cc9c8 | 2010-09-13 22:07:27 +0800 | [diff] [blame] | 250 | return 0; |
| 251 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 252 | |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 253 | if (mmc_set_blocklen(mmc, mmc->read_bl_len)) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 254 | return 0; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 255 | |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 256 | do { |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 257 | cur = (blocks_todo > mmc->cfg->b_max) ? |
| 258 | mmc->cfg->b_max : blocks_todo; |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 259 | if(mmc_read_blocks(mmc, dst, start, cur) != cur) |
| 260 | return 0; |
| 261 | blocks_todo -= cur; |
| 262 | start += cur; |
| 263 | dst += cur * mmc->read_bl_len; |
| 264 | } while (blocks_todo > 0); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 265 | |
| 266 | return blkcnt; |
| 267 | } |
| 268 | |
Kim Phillips | 87ea389 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 269 | static int mmc_go_idle(struct mmc *mmc) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 270 | { |
| 271 | struct mmc_cmd cmd; |
| 272 | int err; |
| 273 | |
| 274 | udelay(1000); |
| 275 | |
| 276 | cmd.cmdidx = MMC_CMD_GO_IDLE_STATE; |
| 277 | cmd.cmdarg = 0; |
| 278 | cmd.resp_type = MMC_RSP_NONE; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 279 | |
| 280 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 281 | |
| 282 | if (err) |
| 283 | return err; |
| 284 | |
| 285 | udelay(2000); |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
Kim Phillips | 87ea389 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 290 | static int sd_send_op_cond(struct mmc *mmc) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 291 | { |
| 292 | int timeout = 1000; |
| 293 | int err; |
| 294 | struct mmc_cmd cmd; |
| 295 | |
Andrew Gabbasov | 034857c | 2015-03-19 07:44:06 -0500 | [diff] [blame] | 296 | while (1) { |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 297 | cmd.cmdidx = MMC_CMD_APP_CMD; |
| 298 | cmd.resp_type = MMC_RSP_R1; |
| 299 | cmd.cmdarg = 0; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 300 | |
| 301 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 302 | |
| 303 | if (err) |
| 304 | return err; |
| 305 | |
| 306 | cmd.cmdidx = SD_CMD_APP_SEND_OP_COND; |
| 307 | cmd.resp_type = MMC_RSP_R3; |
Stefano Babic | f8e9a21 | 2010-01-20 18:20:39 +0100 | [diff] [blame] | 308 | |
| 309 | /* |
| 310 | * Most cards do not answer if some reserved bits |
| 311 | * in the ocr are set. However, Some controller |
| 312 | * can set bit 7 (reserved for low voltages), but |
| 313 | * how to manage low voltages SD card is not yet |
| 314 | * specified. |
| 315 | */ |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 316 | cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 : |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 317 | (mmc->cfg->voltages & 0xff8000); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 318 | |
| 319 | if (mmc->version == SD_VERSION_2) |
| 320 | cmd.cmdarg |= OCR_HCS; |
| 321 | |
| 322 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 323 | |
| 324 | if (err) |
| 325 | return err; |
| 326 | |
Andrew Gabbasov | 034857c | 2015-03-19 07:44:06 -0500 | [diff] [blame] | 327 | if (cmd.response[0] & OCR_BUSY) |
| 328 | break; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 329 | |
Andrew Gabbasov | 034857c | 2015-03-19 07:44:06 -0500 | [diff] [blame] | 330 | if (timeout-- <= 0) |
| 331 | return UNUSABLE_ERR; |
| 332 | |
| 333 | udelay(1000); |
| 334 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 335 | |
| 336 | if (mmc->version != SD_VERSION_2) |
| 337 | mmc->version = SD_VERSION_1_0; |
| 338 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 339 | if (mmc_host_is_spi(mmc)) { /* read OCR for spi */ |
| 340 | cmd.cmdidx = MMC_CMD_SPI_READ_OCR; |
| 341 | cmd.resp_type = MMC_RSP_R3; |
| 342 | cmd.cmdarg = 0; |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 343 | |
| 344 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 345 | |
| 346 | if (err) |
| 347 | return err; |
| 348 | } |
| 349 | |
Rabin Vincent | b6eed94 | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 350 | mmc->ocr = cmd.response[0]; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 351 | |
| 352 | mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); |
| 353 | mmc->rca = 0; |
| 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
Andrew Gabbasov | fafa6a0 | 2015-03-19 07:44:04 -0500 | [diff] [blame] | 358 | static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 359 | { |
Andrew Gabbasov | fafa6a0 | 2015-03-19 07:44:04 -0500 | [diff] [blame] | 360 | struct mmc_cmd cmd; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 361 | int err; |
| 362 | |
Andrew Gabbasov | fafa6a0 | 2015-03-19 07:44:04 -0500 | [diff] [blame] | 363 | cmd.cmdidx = MMC_CMD_SEND_OP_COND; |
| 364 | cmd.resp_type = MMC_RSP_R3; |
| 365 | cmd.cmdarg = 0; |
Rob Herring | 5fd3edd | 2015-03-23 17:56:59 -0500 | [diff] [blame] | 366 | if (use_arg && !mmc_host_is_spi(mmc)) |
| 367 | cmd.cmdarg = OCR_HCS | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 368 | (mmc->cfg->voltages & |
Andrew Gabbasov | ec600d1 | 2015-03-19 07:44:03 -0500 | [diff] [blame] | 369 | (mmc->ocr & OCR_VOLTAGE_MASK)) | |
| 370 | (mmc->ocr & OCR_ACCESS_MODE); |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 371 | |
Andrew Gabbasov | fafa6a0 | 2015-03-19 07:44:04 -0500 | [diff] [blame] | 372 | err = mmc_send_cmd(mmc, &cmd, NULL); |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 373 | if (err) |
| 374 | return err; |
Andrew Gabbasov | fafa6a0 | 2015-03-19 07:44:04 -0500 | [diff] [blame] | 375 | mmc->ocr = cmd.response[0]; |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 376 | return 0; |
| 377 | } |
| 378 | |
Jeroen Hofstee | aedeeaa | 2014-07-12 21:24:08 +0200 | [diff] [blame] | 379 | static int mmc_send_op_cond(struct mmc *mmc) |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 380 | { |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 381 | int err, i; |
| 382 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 383 | /* Some cards seem to need this */ |
| 384 | mmc_go_idle(mmc); |
| 385 | |
Raffaele Recalcati | 1df837e | 2011-03-11 02:01:13 +0000 | [diff] [blame] | 386 | /* Asking to the card its capabilities */ |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 387 | for (i = 0; i < 2; i++) { |
Andrew Gabbasov | fafa6a0 | 2015-03-19 07:44:04 -0500 | [diff] [blame] | 388 | err = mmc_send_op_cond_iter(mmc, i != 0); |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 389 | if (err) |
| 390 | return err; |
Wolfgang Denk | 80f7021 | 2011-05-19 22:21:41 +0200 | [diff] [blame] | 391 | |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 392 | /* exit if not busy (flag seems to be inverted) */ |
Andrew Gabbasov | ec600d1 | 2015-03-19 07:44:03 -0500 | [diff] [blame] | 393 | if (mmc->ocr & OCR_BUSY) |
Andrew Gabbasov | 3a669bc | 2015-03-19 07:44:07 -0500 | [diff] [blame] | 394 | break; |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 395 | } |
Andrew Gabbasov | 3a669bc | 2015-03-19 07:44:07 -0500 | [diff] [blame] | 396 | mmc->op_cond_pending = 1; |
| 397 | return 0; |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 398 | } |
Wolfgang Denk | 80f7021 | 2011-05-19 22:21:41 +0200 | [diff] [blame] | 399 | |
Jeroen Hofstee | aedeeaa | 2014-07-12 21:24:08 +0200 | [diff] [blame] | 400 | static int mmc_complete_op_cond(struct mmc *mmc) |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 401 | { |
| 402 | struct mmc_cmd cmd; |
| 403 | int timeout = 1000; |
| 404 | uint start; |
| 405 | int err; |
Wolfgang Denk | 80f7021 | 2011-05-19 22:21:41 +0200 | [diff] [blame] | 406 | |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 407 | mmc->op_cond_pending = 0; |
Andrew Gabbasov | 5a513ca | 2015-03-19 07:44:05 -0500 | [diff] [blame] | 408 | if (!(mmc->ocr & OCR_BUSY)) { |
| 409 | start = get_timer(0); |
Andrew Gabbasov | 034857c | 2015-03-19 07:44:06 -0500 | [diff] [blame] | 410 | while (1) { |
Andrew Gabbasov | 5a513ca | 2015-03-19 07:44:05 -0500 | [diff] [blame] | 411 | err = mmc_send_op_cond_iter(mmc, 1); |
| 412 | if (err) |
| 413 | return err; |
Andrew Gabbasov | 034857c | 2015-03-19 07:44:06 -0500 | [diff] [blame] | 414 | if (mmc->ocr & OCR_BUSY) |
| 415 | break; |
Andrew Gabbasov | 5a513ca | 2015-03-19 07:44:05 -0500 | [diff] [blame] | 416 | if (get_timer(start) > timeout) |
| 417 | return UNUSABLE_ERR; |
| 418 | udelay(100); |
Andrew Gabbasov | 034857c | 2015-03-19 07:44:06 -0500 | [diff] [blame] | 419 | } |
Andrew Gabbasov | 5a513ca | 2015-03-19 07:44:05 -0500 | [diff] [blame] | 420 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 421 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 422 | if (mmc_host_is_spi(mmc)) { /* read OCR for spi */ |
| 423 | cmd.cmdidx = MMC_CMD_SPI_READ_OCR; |
| 424 | cmd.resp_type = MMC_RSP_R3; |
| 425 | cmd.cmdarg = 0; |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 426 | |
| 427 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 428 | |
| 429 | if (err) |
| 430 | return err; |
Andrew Gabbasov | ec600d1 | 2015-03-19 07:44:03 -0500 | [diff] [blame] | 431 | |
| 432 | mmc->ocr = cmd.response[0]; |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 435 | mmc->version = MMC_VERSION_UNKNOWN; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 436 | |
| 437 | mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); |
Stephen Warren | f6545f1 | 2014-01-30 16:11:12 -0700 | [diff] [blame] | 438 | mmc->rca = 1; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 439 | |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | |
Kim Phillips | 87ea389 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 444 | static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 445 | { |
| 446 | struct mmc_cmd cmd; |
| 447 | struct mmc_data data; |
| 448 | int err; |
| 449 | |
| 450 | /* Get the Card Status Register */ |
| 451 | cmd.cmdidx = MMC_CMD_SEND_EXT_CSD; |
| 452 | cmd.resp_type = MMC_RSP_R1; |
| 453 | cmd.cmdarg = 0; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 454 | |
Yoshihiro Shimoda | f6bec73 | 2012-06-07 19:09:11 +0000 | [diff] [blame] | 455 | data.dest = (char *)ext_csd; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 456 | data.blocks = 1; |
Simon Glass | a09c2b7 | 2013-04-03 08:54:30 +0000 | [diff] [blame] | 457 | data.blocksize = MMC_MAX_BLOCK_LEN; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 458 | data.flags = MMC_DATA_READ; |
| 459 | |
| 460 | err = mmc_send_cmd(mmc, &cmd, &data); |
| 461 | |
| 462 | return err; |
| 463 | } |
| 464 | |
| 465 | |
Kim Phillips | 87ea389 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 466 | static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 467 | { |
| 468 | struct mmc_cmd cmd; |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 469 | int timeout = 1000; |
| 470 | int ret; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 471 | |
| 472 | cmd.cmdidx = MMC_CMD_SWITCH; |
| 473 | cmd.resp_type = MMC_RSP_R1b; |
| 474 | cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 475 | (index << 16) | |
| 476 | (value << 8); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 477 | |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 478 | ret = mmc_send_cmd(mmc, &cmd, NULL); |
| 479 | |
| 480 | /* Waiting for the ready status */ |
Jan Kloetzke | 4e929dd | 2012-02-05 22:29:11 +0000 | [diff] [blame] | 481 | if (!ret) |
| 482 | ret = mmc_send_status(mmc, timeout); |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 483 | |
| 484 | return ret; |
| 485 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 486 | } |
| 487 | |
Kim Phillips | 87ea389 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 488 | static int mmc_change_freq(struct mmc *mmc) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 489 | { |
Simon Glass | a09c2b7 | 2013-04-03 08:54:30 +0000 | [diff] [blame] | 490 | ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 491 | char cardtype; |
| 492 | int err; |
| 493 | |
Andrew Gabbasov | ccb7b04 | 2014-12-25 10:22:25 -0600 | [diff] [blame] | 494 | mmc->card_caps = 0; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 495 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 496 | if (mmc_host_is_spi(mmc)) |
| 497 | return 0; |
| 498 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 499 | /* Only version 4 supports high-speed */ |
| 500 | if (mmc->version < MMC_VERSION_4) |
| 501 | return 0; |
| 502 | |
Andrew Gabbasov | ccb7b04 | 2014-12-25 10:22:25 -0600 | [diff] [blame] | 503 | mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT; |
| 504 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 505 | err = mmc_send_ext_csd(mmc, ext_csd); |
| 506 | |
| 507 | if (err) |
| 508 | return err; |
| 509 | |
Lei Wen | 217467f | 2011-10-03 20:35:10 +0000 | [diff] [blame] | 510 | cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 511 | |
| 512 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1); |
| 513 | |
| 514 | if (err) |
Andrew Gabbasov | e80682f | 2014-04-03 04:34:32 -0500 | [diff] [blame] | 515 | return err == SWITCH_ERR ? 0 : err; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 516 | |
| 517 | /* Now check to see that it worked */ |
| 518 | err = mmc_send_ext_csd(mmc, ext_csd); |
| 519 | |
| 520 | if (err) |
| 521 | return err; |
| 522 | |
| 523 | /* No high-speed support */ |
Lei Wen | 217467f | 2011-10-03 20:35:10 +0000 | [diff] [blame] | 524 | if (!ext_csd[EXT_CSD_HS_TIMING]) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 525 | return 0; |
| 526 | |
| 527 | /* High Speed is set, there are two types: 52MHz and 26MHz */ |
Jaehoon Chung | 38ce30b | 2014-05-16 13:59:54 +0900 | [diff] [blame] | 528 | if (cardtype & EXT_CSD_CARD_TYPE_52) { |
Andrew Gabbasov | 95a3713 | 2014-12-01 06:59:10 -0600 | [diff] [blame] | 529 | if (cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V) |
Jaehoon Chung | 38ce30b | 2014-05-16 13:59:54 +0900 | [diff] [blame] | 530 | mmc->card_caps |= MMC_MODE_DDR_52MHz; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 531 | mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; |
Jaehoon Chung | 38ce30b | 2014-05-16 13:59:54 +0900 | [diff] [blame] | 532 | } else { |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 533 | mmc->card_caps |= MMC_MODE_HS; |
Jaehoon Chung | 38ce30b | 2014-05-16 13:59:54 +0900 | [diff] [blame] | 534 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 535 | |
| 536 | return 0; |
| 537 | } |
| 538 | |
Stephen Warren | e315ae8 | 2013-06-11 15:14:01 -0600 | [diff] [blame] | 539 | static int mmc_set_capacity(struct mmc *mmc, int part_num) |
| 540 | { |
| 541 | switch (part_num) { |
| 542 | case 0: |
| 543 | mmc->capacity = mmc->capacity_user; |
| 544 | break; |
| 545 | case 1: |
| 546 | case 2: |
| 547 | mmc->capacity = mmc->capacity_boot; |
| 548 | break; |
| 549 | case 3: |
| 550 | mmc->capacity = mmc->capacity_rpmb; |
| 551 | break; |
| 552 | case 4: |
| 553 | case 5: |
| 554 | case 6: |
| 555 | case 7: |
| 556 | mmc->capacity = mmc->capacity_gp[part_num - 4]; |
| 557 | break; |
| 558 | default: |
| 559 | return -1; |
| 560 | } |
| 561 | |
| 562 | mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len); |
| 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
Stephen Warren | 52c44e4 | 2014-05-07 12:19:02 -0600 | [diff] [blame] | 567 | int mmc_select_hwpart(int dev_num, int hwpart) |
| 568 | { |
| 569 | struct mmc *mmc = find_mmc_device(dev_num); |
| 570 | int ret; |
| 571 | |
| 572 | if (!mmc) |
Stephen Warren | bf0c785 | 2014-05-23 12:47:06 -0600 | [diff] [blame] | 573 | return -ENODEV; |
Stephen Warren | 52c44e4 | 2014-05-07 12:19:02 -0600 | [diff] [blame] | 574 | |
| 575 | if (mmc->part_num == hwpart) |
| 576 | return 0; |
| 577 | |
| 578 | if (mmc->part_config == MMCPART_NOAVAILABLE) { |
| 579 | printf("Card doesn't support part_switch\n"); |
Stephen Warren | bf0c785 | 2014-05-23 12:47:06 -0600 | [diff] [blame] | 580 | return -EMEDIUMTYPE; |
Stephen Warren | 52c44e4 | 2014-05-07 12:19:02 -0600 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | ret = mmc_switch_part(dev_num, hwpart); |
| 584 | if (ret) |
Stephen Warren | bf0c785 | 2014-05-23 12:47:06 -0600 | [diff] [blame] | 585 | return ret; |
Stephen Warren | 52c44e4 | 2014-05-07 12:19:02 -0600 | [diff] [blame] | 586 | |
| 587 | mmc->part_num = hwpart; |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | |
Lei Wen | 31b9980 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 593 | int mmc_switch_part(int dev_num, unsigned int part_num) |
| 594 | { |
| 595 | struct mmc *mmc = find_mmc_device(dev_num); |
Stephen Warren | e315ae8 | 2013-06-11 15:14:01 -0600 | [diff] [blame] | 596 | int ret; |
Lei Wen | 31b9980 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 597 | |
| 598 | if (!mmc) |
| 599 | return -1; |
| 600 | |
Stephen Warren | e315ae8 | 2013-06-11 15:14:01 -0600 | [diff] [blame] | 601 | ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF, |
| 602 | (mmc->part_config & ~PART_ACCESS_MASK) |
| 603 | | (part_num & PART_ACCESS_MASK)); |
Peter Bigot | 45fde89 | 2014-09-02 18:31:23 -0500 | [diff] [blame] | 604 | |
| 605 | /* |
| 606 | * Set the capacity if the switch succeeded or was intended |
| 607 | * to return to representing the raw device. |
| 608 | */ |
| 609 | if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) |
| 610 | ret = mmc_set_capacity(mmc, part_num); |
Stephen Warren | e315ae8 | 2013-06-11 15:14:01 -0600 | [diff] [blame] | 611 | |
Peter Bigot | 45fde89 | 2014-09-02 18:31:23 -0500 | [diff] [blame] | 612 | return ret; |
Lei Wen | 31b9980 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Diego Santa Cruz | 69eb71a0 | 2014-12-23 10:50:29 +0100 | [diff] [blame] | 615 | int mmc_hwpart_config(struct mmc *mmc, |
| 616 | const struct mmc_hwpart_conf *conf, |
| 617 | enum mmc_hwpart_conf_mode mode) |
| 618 | { |
| 619 | u8 part_attrs = 0; |
| 620 | u32 enh_size_mult; |
| 621 | u32 enh_start_addr; |
| 622 | u32 gp_size_mult[4]; |
| 623 | u32 max_enh_size_mult; |
| 624 | u32 tot_enh_size_mult = 0; |
Diego Santa Cruz | 8020027 | 2014-12-23 10:50:31 +0100 | [diff] [blame] | 625 | u8 wr_rel_set; |
Diego Santa Cruz | 69eb71a0 | 2014-12-23 10:50:29 +0100 | [diff] [blame] | 626 | int i, pidx, err; |
| 627 | ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN); |
| 628 | |
| 629 | if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE) |
| 630 | return -EINVAL; |
| 631 | |
| 632 | if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) { |
| 633 | printf("eMMC >= 4.4 required for enhanced user data area\n"); |
| 634 | return -EMEDIUMTYPE; |
| 635 | } |
| 636 | |
| 637 | if (!(mmc->part_support & PART_SUPPORT)) { |
| 638 | printf("Card does not support partitioning\n"); |
| 639 | return -EMEDIUMTYPE; |
| 640 | } |
| 641 | |
| 642 | if (!mmc->hc_wp_grp_size) { |
| 643 | printf("Card does not define HC WP group size\n"); |
| 644 | return -EMEDIUMTYPE; |
| 645 | } |
| 646 | |
| 647 | /* check partition alignment and total enhanced size */ |
| 648 | if (conf->user.enh_size) { |
| 649 | if (conf->user.enh_size % mmc->hc_wp_grp_size || |
| 650 | conf->user.enh_start % mmc->hc_wp_grp_size) { |
| 651 | printf("User data enhanced area not HC WP group " |
| 652 | "size aligned\n"); |
| 653 | return -EINVAL; |
| 654 | } |
| 655 | part_attrs |= EXT_CSD_ENH_USR; |
| 656 | enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size; |
| 657 | if (mmc->high_capacity) { |
| 658 | enh_start_addr = conf->user.enh_start; |
| 659 | } else { |
| 660 | enh_start_addr = (conf->user.enh_start << 9); |
| 661 | } |
| 662 | } else { |
| 663 | enh_size_mult = 0; |
| 664 | enh_start_addr = 0; |
| 665 | } |
| 666 | tot_enh_size_mult += enh_size_mult; |
| 667 | |
| 668 | for (pidx = 0; pidx < 4; pidx++) { |
| 669 | if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) { |
| 670 | printf("GP%i partition not HC WP group size " |
| 671 | "aligned\n", pidx+1); |
| 672 | return -EINVAL; |
| 673 | } |
| 674 | gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size; |
| 675 | if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) { |
| 676 | part_attrs |= EXT_CSD_ENH_GP(pidx); |
| 677 | tot_enh_size_mult += gp_size_mult[pidx]; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) { |
| 682 | printf("Card does not support enhanced attribute\n"); |
| 683 | return -EMEDIUMTYPE; |
| 684 | } |
| 685 | |
| 686 | err = mmc_send_ext_csd(mmc, ext_csd); |
| 687 | if (err) |
| 688 | return err; |
| 689 | |
| 690 | max_enh_size_mult = |
| 691 | (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) + |
| 692 | (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) + |
| 693 | ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT]; |
| 694 | if (tot_enh_size_mult > max_enh_size_mult) { |
| 695 | printf("Total enhanced size exceeds maximum (%u > %u)\n", |
| 696 | tot_enh_size_mult, max_enh_size_mult); |
| 697 | return -EMEDIUMTYPE; |
| 698 | } |
| 699 | |
Diego Santa Cruz | 8020027 | 2014-12-23 10:50:31 +0100 | [diff] [blame] | 700 | /* The default value of EXT_CSD_WR_REL_SET is device |
| 701 | * dependent, the values can only be changed if the |
| 702 | * EXT_CSD_HS_CTRL_REL bit is set. The values can be |
| 703 | * changed only once and before partitioning is completed. */ |
| 704 | wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET]; |
| 705 | if (conf->user.wr_rel_change) { |
| 706 | if (conf->user.wr_rel_set) |
| 707 | wr_rel_set |= EXT_CSD_WR_DATA_REL_USR; |
| 708 | else |
| 709 | wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR; |
| 710 | } |
| 711 | for (pidx = 0; pidx < 4; pidx++) { |
| 712 | if (conf->gp_part[pidx].wr_rel_change) { |
| 713 | if (conf->gp_part[pidx].wr_rel_set) |
| 714 | wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx); |
| 715 | else |
| 716 | wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx); |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] && |
| 721 | !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) { |
| 722 | puts("Card does not support host controlled partition write " |
| 723 | "reliability settings\n"); |
| 724 | return -EMEDIUMTYPE; |
| 725 | } |
| 726 | |
Diego Santa Cruz | 69eb71a0 | 2014-12-23 10:50:29 +0100 | [diff] [blame] | 727 | if (ext_csd[EXT_CSD_PARTITION_SETTING] & |
| 728 | EXT_CSD_PARTITION_SETTING_COMPLETED) { |
| 729 | printf("Card already partitioned\n"); |
| 730 | return -EPERM; |
| 731 | } |
| 732 | |
| 733 | if (mode == MMC_HWPART_CONF_CHECK) |
| 734 | return 0; |
| 735 | |
| 736 | /* Partitioning requires high-capacity size definitions */ |
| 737 | if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) { |
| 738 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
| 739 | EXT_CSD_ERASE_GROUP_DEF, 1); |
| 740 | |
| 741 | if (err) |
| 742 | return err; |
| 743 | |
| 744 | ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1; |
| 745 | |
| 746 | /* update erase group size to be high-capacity */ |
| 747 | mmc->erase_grp_size = |
| 748 | ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024; |
| 749 | |
| 750 | } |
| 751 | |
| 752 | /* all OK, write the configuration */ |
| 753 | for (i = 0; i < 4; i++) { |
| 754 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
| 755 | EXT_CSD_ENH_START_ADDR+i, |
| 756 | (enh_start_addr >> (i*8)) & 0xFF); |
| 757 | if (err) |
| 758 | return err; |
| 759 | } |
| 760 | for (i = 0; i < 3; i++) { |
| 761 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
| 762 | EXT_CSD_ENH_SIZE_MULT+i, |
| 763 | (enh_size_mult >> (i*8)) & 0xFF); |
| 764 | if (err) |
| 765 | return err; |
| 766 | } |
| 767 | for (pidx = 0; pidx < 4; pidx++) { |
| 768 | for (i = 0; i < 3; i++) { |
| 769 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
| 770 | EXT_CSD_GP_SIZE_MULT+pidx*3+i, |
| 771 | (gp_size_mult[pidx] >> (i*8)) & 0xFF); |
| 772 | if (err) |
| 773 | return err; |
| 774 | } |
| 775 | } |
| 776 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
| 777 | EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs); |
| 778 | if (err) |
| 779 | return err; |
| 780 | |
| 781 | if (mode == MMC_HWPART_CONF_SET) |
| 782 | return 0; |
| 783 | |
Diego Santa Cruz | 8020027 | 2014-12-23 10:50:31 +0100 | [diff] [blame] | 784 | /* The WR_REL_SET is a write-once register but shall be |
| 785 | * written before setting PART_SETTING_COMPLETED. As it is |
| 786 | * write-once we can only write it when completing the |
| 787 | * partitioning. */ |
| 788 | if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) { |
| 789 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
| 790 | EXT_CSD_WR_REL_SET, wr_rel_set); |
| 791 | if (err) |
| 792 | return err; |
| 793 | } |
| 794 | |
Diego Santa Cruz | 69eb71a0 | 2014-12-23 10:50:29 +0100 | [diff] [blame] | 795 | /* Setting PART_SETTING_COMPLETED confirms the partition |
| 796 | * configuration but it only becomes effective after power |
| 797 | * cycle, so we do not adjust the partition related settings |
| 798 | * in the mmc struct. */ |
| 799 | |
| 800 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
| 801 | EXT_CSD_PARTITION_SETTING, |
| 802 | EXT_CSD_PARTITION_SETTING_COMPLETED); |
| 803 | if (err) |
| 804 | return err; |
| 805 | |
| 806 | return 0; |
| 807 | } |
| 808 | |
Thierry Reding | b9c8b77 | 2012-01-02 01:15:37 +0000 | [diff] [blame] | 809 | int mmc_getcd(struct mmc *mmc) |
| 810 | { |
| 811 | int cd; |
| 812 | |
| 813 | cd = board_mmc_getcd(mmc); |
| 814 | |
Peter Korsgaard | f7b1510 | 2013-03-21 04:00:03 +0000 | [diff] [blame] | 815 | if (cd < 0) { |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 816 | if (mmc->cfg->ops->getcd) |
| 817 | cd = mmc->cfg->ops->getcd(mmc); |
Peter Korsgaard | f7b1510 | 2013-03-21 04:00:03 +0000 | [diff] [blame] | 818 | else |
| 819 | cd = 1; |
| 820 | } |
Thierry Reding | b9c8b77 | 2012-01-02 01:15:37 +0000 | [diff] [blame] | 821 | |
| 822 | return cd; |
| 823 | } |
| 824 | |
Kim Phillips | 87ea389 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 825 | static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 826 | { |
| 827 | struct mmc_cmd cmd; |
| 828 | struct mmc_data data; |
| 829 | |
| 830 | /* Switch the frequency */ |
| 831 | cmd.cmdidx = SD_CMD_SWITCH_FUNC; |
| 832 | cmd.resp_type = MMC_RSP_R1; |
| 833 | cmd.cmdarg = (mode << 31) | 0xffffff; |
| 834 | cmd.cmdarg &= ~(0xf << (group * 4)); |
| 835 | cmd.cmdarg |= value << (group * 4); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 836 | |
| 837 | data.dest = (char *)resp; |
| 838 | data.blocksize = 64; |
| 839 | data.blocks = 1; |
| 840 | data.flags = MMC_DATA_READ; |
| 841 | |
| 842 | return mmc_send_cmd(mmc, &cmd, &data); |
| 843 | } |
| 844 | |
| 845 | |
Kim Phillips | 87ea389 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 846 | static int sd_change_freq(struct mmc *mmc) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 847 | { |
| 848 | int err; |
| 849 | struct mmc_cmd cmd; |
Anton staaf | 9b00f0d | 2011-10-03 13:54:59 +0000 | [diff] [blame] | 850 | ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2); |
| 851 | ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 852 | struct mmc_data data; |
| 853 | int timeout; |
| 854 | |
| 855 | mmc->card_caps = 0; |
| 856 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 857 | if (mmc_host_is_spi(mmc)) |
| 858 | return 0; |
| 859 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 860 | /* Read the SCR to find out if this card supports higher speeds */ |
| 861 | cmd.cmdidx = MMC_CMD_APP_CMD; |
| 862 | cmd.resp_type = MMC_RSP_R1; |
| 863 | cmd.cmdarg = mmc->rca << 16; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 864 | |
| 865 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 866 | |
| 867 | if (err) |
| 868 | return err; |
| 869 | |
| 870 | cmd.cmdidx = SD_CMD_APP_SEND_SCR; |
| 871 | cmd.resp_type = MMC_RSP_R1; |
| 872 | cmd.cmdarg = 0; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 873 | |
| 874 | timeout = 3; |
| 875 | |
| 876 | retry_scr: |
Anton staaf | 9b00f0d | 2011-10-03 13:54:59 +0000 | [diff] [blame] | 877 | data.dest = (char *)scr; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 878 | data.blocksize = 8; |
| 879 | data.blocks = 1; |
| 880 | data.flags = MMC_DATA_READ; |
| 881 | |
| 882 | err = mmc_send_cmd(mmc, &cmd, &data); |
| 883 | |
| 884 | if (err) { |
| 885 | if (timeout--) |
| 886 | goto retry_scr; |
| 887 | |
| 888 | return err; |
| 889 | } |
| 890 | |
Yauhen Kharuzhy | 6e8edf4 | 2009-05-07 00:43:30 +0300 | [diff] [blame] | 891 | mmc->scr[0] = __be32_to_cpu(scr[0]); |
| 892 | mmc->scr[1] = __be32_to_cpu(scr[1]); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 893 | |
| 894 | switch ((mmc->scr[0] >> 24) & 0xf) { |
| 895 | case 0: |
| 896 | mmc->version = SD_VERSION_1_0; |
| 897 | break; |
| 898 | case 1: |
| 899 | mmc->version = SD_VERSION_1_10; |
| 900 | break; |
| 901 | case 2: |
| 902 | mmc->version = SD_VERSION_2; |
Jaehoon Chung | d552bd1 | 2013-01-29 22:58:16 +0000 | [diff] [blame] | 903 | if ((mmc->scr[0] >> 15) & 0x1) |
| 904 | mmc->version = SD_VERSION_3; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 905 | break; |
| 906 | default: |
| 907 | mmc->version = SD_VERSION_1_0; |
| 908 | break; |
| 909 | } |
| 910 | |
Alagu Sankar | 24bb5ab | 2010-05-12 15:08:24 +0530 | [diff] [blame] | 911 | if (mmc->scr[0] & SD_DATA_4BIT) |
| 912 | mmc->card_caps |= MMC_MODE_4BIT; |
| 913 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 914 | /* Version 1.0 doesn't support switching */ |
| 915 | if (mmc->version == SD_VERSION_1_0) |
| 916 | return 0; |
| 917 | |
| 918 | timeout = 4; |
| 919 | while (timeout--) { |
| 920 | err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1, |
Anton staaf | 9b00f0d | 2011-10-03 13:54:59 +0000 | [diff] [blame] | 921 | (u8 *)switch_status); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 922 | |
| 923 | if (err) |
| 924 | return err; |
| 925 | |
| 926 | /* The high-speed function is busy. Try again */ |
Yauhen Kharuzhy | 6e8edf4 | 2009-05-07 00:43:30 +0300 | [diff] [blame] | 927 | if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY)) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 928 | break; |
| 929 | } |
| 930 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 931 | /* If high-speed isn't supported, we return */ |
Yauhen Kharuzhy | 6e8edf4 | 2009-05-07 00:43:30 +0300 | [diff] [blame] | 932 | if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 933 | return 0; |
| 934 | |
Macpaul Lin | 24e92ec | 2011-11-28 16:31:09 +0000 | [diff] [blame] | 935 | /* |
| 936 | * If the host doesn't support SD_HIGHSPEED, do not switch card to |
| 937 | * HIGHSPEED mode even if the card support SD_HIGHSPPED. |
| 938 | * This can avoid furthur problem when the card runs in different |
| 939 | * mode between the host. |
| 940 | */ |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 941 | if (!((mmc->cfg->host_caps & MMC_MODE_HS_52MHz) && |
| 942 | (mmc->cfg->host_caps & MMC_MODE_HS))) |
Macpaul Lin | 24e92ec | 2011-11-28 16:31:09 +0000 | [diff] [blame] | 943 | return 0; |
| 944 | |
Anton staaf | 9b00f0d | 2011-10-03 13:54:59 +0000 | [diff] [blame] | 945 | err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 946 | |
| 947 | if (err) |
| 948 | return err; |
| 949 | |
Yauhen Kharuzhy | 6e8edf4 | 2009-05-07 00:43:30 +0300 | [diff] [blame] | 950 | if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 951 | mmc->card_caps |= MMC_MODE_HS; |
| 952 | |
| 953 | return 0; |
| 954 | } |
| 955 | |
| 956 | /* frequency bases */ |
| 957 | /* divided by 10 to be nice to platforms without floating point */ |
Mike Frysinger | b588caf | 2010-10-20 01:15:53 +0000 | [diff] [blame] | 958 | static const int fbase[] = { |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 959 | 10000, |
| 960 | 100000, |
| 961 | 1000000, |
| 962 | 10000000, |
| 963 | }; |
| 964 | |
| 965 | /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice |
| 966 | * to platforms without floating point. |
| 967 | */ |
Mike Frysinger | b588caf | 2010-10-20 01:15:53 +0000 | [diff] [blame] | 968 | static const int multipliers[] = { |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 969 | 0, /* reserved */ |
| 970 | 10, |
| 971 | 12, |
| 972 | 13, |
| 973 | 15, |
| 974 | 20, |
| 975 | 25, |
| 976 | 30, |
| 977 | 35, |
| 978 | 40, |
| 979 | 45, |
| 980 | 50, |
| 981 | 55, |
| 982 | 60, |
| 983 | 70, |
| 984 | 80, |
| 985 | }; |
| 986 | |
Kim Phillips | 87ea389 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 987 | static void mmc_set_ios(struct mmc *mmc) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 988 | { |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 989 | if (mmc->cfg->ops->set_ios) |
| 990 | mmc->cfg->ops->set_ios(mmc); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | void mmc_set_clock(struct mmc *mmc, uint clock) |
| 994 | { |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 995 | if (clock > mmc->cfg->f_max) |
| 996 | clock = mmc->cfg->f_max; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 997 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 998 | if (clock < mmc->cfg->f_min) |
| 999 | clock = mmc->cfg->f_min; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1000 | |
| 1001 | mmc->clock = clock; |
| 1002 | |
| 1003 | mmc_set_ios(mmc); |
| 1004 | } |
| 1005 | |
Kim Phillips | 87ea389 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 1006 | static void mmc_set_bus_width(struct mmc *mmc, uint width) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1007 | { |
| 1008 | mmc->bus_width = width; |
| 1009 | |
| 1010 | mmc_set_ios(mmc); |
| 1011 | } |
| 1012 | |
Kim Phillips | 87ea389 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 1013 | static int mmc_startup(struct mmc *mmc) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1014 | { |
Stephen Warren | e315ae8 | 2013-06-11 15:14:01 -0600 | [diff] [blame] | 1015 | int err, i; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1016 | uint mult, freq; |
Yoshihiro Shimoda | 7d88de5 | 2011-07-04 22:13:26 +0000 | [diff] [blame] | 1017 | u64 cmult, csize, capacity; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1018 | struct mmc_cmd cmd; |
Simon Glass | a09c2b7 | 2013-04-03 08:54:30 +0000 | [diff] [blame] | 1019 | ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN); |
| 1020 | ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN); |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 1021 | int timeout = 1000; |
Diego Santa Cruz | cea8c5c | 2014-12-23 10:50:20 +0100 | [diff] [blame] | 1022 | bool has_parts = false; |
Diego Santa Cruz | a7a7599 | 2014-12-23 10:50:27 +0100 | [diff] [blame] | 1023 | bool part_completed; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1024 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 1025 | #ifdef CONFIG_MMC_SPI_CRC_ON |
| 1026 | if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */ |
| 1027 | cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF; |
| 1028 | cmd.resp_type = MMC_RSP_R1; |
| 1029 | cmd.cmdarg = 1; |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 1030 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 1031 | |
| 1032 | if (err) |
| 1033 | return err; |
| 1034 | } |
| 1035 | #endif |
| 1036 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1037 | /* Put the Card in Identify Mode */ |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 1038 | cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID : |
| 1039 | MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */ |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1040 | cmd.resp_type = MMC_RSP_R2; |
| 1041 | cmd.cmdarg = 0; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1042 | |
| 1043 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 1044 | |
| 1045 | if (err) |
| 1046 | return err; |
| 1047 | |
| 1048 | memcpy(mmc->cid, cmd.response, 16); |
| 1049 | |
| 1050 | /* |
| 1051 | * For MMC cards, set the Relative Address. |
| 1052 | * For SD cards, get the Relatvie Address. |
| 1053 | * This also puts the cards into Standby State |
| 1054 | */ |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 1055 | if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */ |
| 1056 | cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR; |
| 1057 | cmd.cmdarg = mmc->rca << 16; |
| 1058 | cmd.resp_type = MMC_RSP_R6; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1059 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 1060 | err = mmc_send_cmd(mmc, &cmd, NULL); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1061 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 1062 | if (err) |
| 1063 | return err; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1064 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 1065 | if (IS_SD(mmc)) |
| 1066 | mmc->rca = (cmd.response[0] >> 16) & 0xffff; |
| 1067 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1068 | |
| 1069 | /* Get the Card-Specific Data */ |
| 1070 | cmd.cmdidx = MMC_CMD_SEND_CSD; |
| 1071 | cmd.resp_type = MMC_RSP_R2; |
| 1072 | cmd.cmdarg = mmc->rca << 16; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1073 | |
| 1074 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 1075 | |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 1076 | /* Waiting for the ready status */ |
| 1077 | mmc_send_status(mmc, timeout); |
| 1078 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1079 | if (err) |
| 1080 | return err; |
| 1081 | |
Rabin Vincent | b6eed94 | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 1082 | mmc->csd[0] = cmd.response[0]; |
| 1083 | mmc->csd[1] = cmd.response[1]; |
| 1084 | mmc->csd[2] = cmd.response[2]; |
| 1085 | mmc->csd[3] = cmd.response[3]; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1086 | |
| 1087 | if (mmc->version == MMC_VERSION_UNKNOWN) { |
Rabin Vincent | bdf7a68 | 2009-04-05 13:30:55 +0530 | [diff] [blame] | 1088 | int version = (cmd.response[0] >> 26) & 0xf; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1089 | |
| 1090 | switch (version) { |
| 1091 | case 0: |
| 1092 | mmc->version = MMC_VERSION_1_2; |
| 1093 | break; |
| 1094 | case 1: |
| 1095 | mmc->version = MMC_VERSION_1_4; |
| 1096 | break; |
| 1097 | case 2: |
| 1098 | mmc->version = MMC_VERSION_2_2; |
| 1099 | break; |
| 1100 | case 3: |
| 1101 | mmc->version = MMC_VERSION_3; |
| 1102 | break; |
| 1103 | case 4: |
| 1104 | mmc->version = MMC_VERSION_4; |
| 1105 | break; |
| 1106 | default: |
| 1107 | mmc->version = MMC_VERSION_1_2; |
| 1108 | break; |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | /* divide frequency by 10, since the mults are 10x bigger */ |
Rabin Vincent | bdf7a68 | 2009-04-05 13:30:55 +0530 | [diff] [blame] | 1113 | freq = fbase[(cmd.response[0] & 0x7)]; |
| 1114 | mult = multipliers[((cmd.response[0] >> 3) & 0xf)]; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1115 | |
| 1116 | mmc->tran_speed = freq * mult; |
| 1117 | |
Markus Niebel | 0395141 | 2013-12-16 13:40:46 +0100 | [diff] [blame] | 1118 | mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1); |
Rabin Vincent | b6eed94 | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 1119 | mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1120 | |
| 1121 | if (IS_SD(mmc)) |
| 1122 | mmc->write_bl_len = mmc->read_bl_len; |
| 1123 | else |
Rabin Vincent | b6eed94 | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 1124 | mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1125 | |
| 1126 | if (mmc->high_capacity) { |
| 1127 | csize = (mmc->csd[1] & 0x3f) << 16 |
| 1128 | | (mmc->csd[2] & 0xffff0000) >> 16; |
| 1129 | cmult = 8; |
| 1130 | } else { |
| 1131 | csize = (mmc->csd[1] & 0x3ff) << 2 |
| 1132 | | (mmc->csd[2] & 0xc0000000) >> 30; |
| 1133 | cmult = (mmc->csd[2] & 0x00038000) >> 15; |
| 1134 | } |
| 1135 | |
Stephen Warren | e315ae8 | 2013-06-11 15:14:01 -0600 | [diff] [blame] | 1136 | mmc->capacity_user = (csize + 1) << (cmult + 2); |
| 1137 | mmc->capacity_user *= mmc->read_bl_len; |
| 1138 | mmc->capacity_boot = 0; |
| 1139 | mmc->capacity_rpmb = 0; |
| 1140 | for (i = 0; i < 4; i++) |
| 1141 | mmc->capacity_gp[i] = 0; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1142 | |
Simon Glass | a09c2b7 | 2013-04-03 08:54:30 +0000 | [diff] [blame] | 1143 | if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN) |
| 1144 | mmc->read_bl_len = MMC_MAX_BLOCK_LEN; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1145 | |
Simon Glass | a09c2b7 | 2013-04-03 08:54:30 +0000 | [diff] [blame] | 1146 | if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN) |
| 1147 | mmc->write_bl_len = MMC_MAX_BLOCK_LEN; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1148 | |
Markus Niebel | 0395141 | 2013-12-16 13:40:46 +0100 | [diff] [blame] | 1149 | if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) { |
| 1150 | cmd.cmdidx = MMC_CMD_SET_DSR; |
| 1151 | cmd.cmdarg = (mmc->dsr & 0xffff) << 16; |
| 1152 | cmd.resp_type = MMC_RSP_NONE; |
| 1153 | if (mmc_send_cmd(mmc, &cmd, NULL)) |
| 1154 | printf("MMC: SET_DSR failed\n"); |
| 1155 | } |
| 1156 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1157 | /* Select the card, and put it into Transfer Mode */ |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 1158 | if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */ |
| 1159 | cmd.cmdidx = MMC_CMD_SELECT_CARD; |
Ajay Bhargav | 4a32fba | 2011-10-05 03:13:23 +0000 | [diff] [blame] | 1160 | cmd.resp_type = MMC_RSP_R1; |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 1161 | cmd.cmdarg = mmc->rca << 16; |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 1162 | err = mmc_send_cmd(mmc, &cmd, NULL); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1163 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 1164 | if (err) |
| 1165 | return err; |
| 1166 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1167 | |
Lei Wen | ea52676 | 2011-06-22 17:03:31 +0000 | [diff] [blame] | 1168 | /* |
| 1169 | * For SD, its erase group is always one sector |
| 1170 | */ |
| 1171 | mmc->erase_grp_size = 1; |
Lei Wen | 31b9980 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 1172 | mmc->part_config = MMCPART_NOAVAILABLE; |
Sukumar Ghorai | 232293c | 2010-09-20 18:29:29 +0530 | [diff] [blame] | 1173 | if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) { |
| 1174 | /* check ext_csd version and capacity */ |
| 1175 | err = mmc_send_ext_csd(mmc, ext_csd); |
Diego Santa Cruz | ca25e06 | 2014-12-23 10:50:28 +0100 | [diff] [blame] | 1176 | if (err) |
| 1177 | return err; |
| 1178 | if (ext_csd[EXT_CSD_REV] >= 2) { |
Yoshihiro Shimoda | 7d88de5 | 2011-07-04 22:13:26 +0000 | [diff] [blame] | 1179 | /* |
| 1180 | * According to the JEDEC Standard, the value of |
| 1181 | * ext_csd's capacity is valid if the value is more |
| 1182 | * than 2GB |
| 1183 | */ |
Lei Wen | 217467f | 2011-10-03 20:35:10 +0000 | [diff] [blame] | 1184 | capacity = ext_csd[EXT_CSD_SEC_CNT] << 0 |
| 1185 | | ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
| 1186 | | ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
| 1187 | | ext_csd[EXT_CSD_SEC_CNT + 3] << 24; |
Simon Glass | a09c2b7 | 2013-04-03 08:54:30 +0000 | [diff] [blame] | 1188 | capacity *= MMC_MAX_BLOCK_LEN; |
Łukasz Majewski | 237823e | 2011-07-05 02:19:44 +0000 | [diff] [blame] | 1189 | if ((capacity >> 20) > 2 * 1024) |
Stephen Warren | e315ae8 | 2013-06-11 15:14:01 -0600 | [diff] [blame] | 1190 | mmc->capacity_user = capacity; |
Sukumar Ghorai | 232293c | 2010-09-20 18:29:29 +0530 | [diff] [blame] | 1191 | } |
Lei Wen | 31b9980 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 1192 | |
Jaehoon Chung | 6108ef6 | 2013-01-29 19:31:16 +0000 | [diff] [blame] | 1193 | switch (ext_csd[EXT_CSD_REV]) { |
| 1194 | case 1: |
| 1195 | mmc->version = MMC_VERSION_4_1; |
| 1196 | break; |
| 1197 | case 2: |
| 1198 | mmc->version = MMC_VERSION_4_2; |
| 1199 | break; |
| 1200 | case 3: |
| 1201 | mmc->version = MMC_VERSION_4_3; |
| 1202 | break; |
| 1203 | case 5: |
| 1204 | mmc->version = MMC_VERSION_4_41; |
| 1205 | break; |
| 1206 | case 6: |
| 1207 | mmc->version = MMC_VERSION_4_5; |
| 1208 | break; |
Markus Niebel | 32f53b6 | 2014-11-18 15:13:53 +0100 | [diff] [blame] | 1209 | case 7: |
| 1210 | mmc->version = MMC_VERSION_5_0; |
| 1211 | break; |
Jaehoon Chung | 6108ef6 | 2013-01-29 19:31:16 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
Diego Santa Cruz | a7a7599 | 2014-12-23 10:50:27 +0100 | [diff] [blame] | 1214 | /* The partition data may be non-zero but it is only |
| 1215 | * effective if PARTITION_SETTING_COMPLETED is set in |
| 1216 | * EXT_CSD, so ignore any data if this bit is not set, |
| 1217 | * except for enabling the high-capacity group size |
| 1218 | * definition (see below). */ |
| 1219 | part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] & |
| 1220 | EXT_CSD_PARTITION_SETTING_COMPLETED); |
| 1221 | |
Diego Santa Cruz | cea8c5c | 2014-12-23 10:50:20 +0100 | [diff] [blame] | 1222 | /* store the partition info of emmc */ |
| 1223 | mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT]; |
| 1224 | if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) || |
| 1225 | ext_csd[EXT_CSD_BOOT_MULT]) |
| 1226 | mmc->part_config = ext_csd[EXT_CSD_PART_CONF]; |
Diego Santa Cruz | a7a7599 | 2014-12-23 10:50:27 +0100 | [diff] [blame] | 1227 | if (part_completed && |
| 1228 | (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT)) |
Diego Santa Cruz | cea8c5c | 2014-12-23 10:50:20 +0100 | [diff] [blame] | 1229 | mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE]; |
| 1230 | |
| 1231 | mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17; |
| 1232 | |
| 1233 | mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17; |
| 1234 | |
| 1235 | for (i = 0; i < 4; i++) { |
| 1236 | int idx = EXT_CSD_GP_SIZE_MULT + i * 3; |
Diego Santa Cruz | a7a7599 | 2014-12-23 10:50:27 +0100 | [diff] [blame] | 1237 | uint mult = (ext_csd[idx + 2] << 16) + |
Diego Santa Cruz | cea8c5c | 2014-12-23 10:50:20 +0100 | [diff] [blame] | 1238 | (ext_csd[idx + 1] << 8) + ext_csd[idx]; |
Diego Santa Cruz | a7a7599 | 2014-12-23 10:50:27 +0100 | [diff] [blame] | 1239 | if (mult) |
| 1240 | has_parts = true; |
| 1241 | if (!part_completed) |
| 1242 | continue; |
| 1243 | mmc->capacity_gp[i] = mult; |
Diego Santa Cruz | cea8c5c | 2014-12-23 10:50:20 +0100 | [diff] [blame] | 1244 | mmc->capacity_gp[i] *= |
| 1245 | ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]; |
| 1246 | mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; |
Diego Santa Cruz | e5a2a3a | 2014-12-23 10:50:21 +0100 | [diff] [blame] | 1247 | mmc->capacity_gp[i] <<= 19; |
Diego Santa Cruz | cea8c5c | 2014-12-23 10:50:20 +0100 | [diff] [blame] | 1248 | } |
| 1249 | |
Diego Santa Cruz | a7a7599 | 2014-12-23 10:50:27 +0100 | [diff] [blame] | 1250 | if (part_completed) { |
| 1251 | mmc->enh_user_size = |
| 1252 | (ext_csd[EXT_CSD_ENH_SIZE_MULT+2] << 16) + |
| 1253 | (ext_csd[EXT_CSD_ENH_SIZE_MULT+1] << 8) + |
| 1254 | ext_csd[EXT_CSD_ENH_SIZE_MULT]; |
| 1255 | mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]; |
| 1256 | mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; |
| 1257 | mmc->enh_user_size <<= 19; |
| 1258 | mmc->enh_user_start = |
| 1259 | (ext_csd[EXT_CSD_ENH_START_ADDR+3] << 24) + |
| 1260 | (ext_csd[EXT_CSD_ENH_START_ADDR+2] << 16) + |
| 1261 | (ext_csd[EXT_CSD_ENH_START_ADDR+1] << 8) + |
| 1262 | ext_csd[EXT_CSD_ENH_START_ADDR]; |
| 1263 | if (mmc->high_capacity) |
| 1264 | mmc->enh_user_start <<= 9; |
| 1265 | } |
Diego Santa Cruz | 3b62d84 | 2014-12-23 10:50:22 +0100 | [diff] [blame] | 1266 | |
Lei Wen | ea52676 | 2011-06-22 17:03:31 +0000 | [diff] [blame] | 1267 | /* |
Oliver Metz | b3f1409 | 2013-10-01 20:32:07 +0200 | [diff] [blame] | 1268 | * Host needs to enable ERASE_GRP_DEF bit if device is |
| 1269 | * partitioned. This bit will be lost every time after a reset |
| 1270 | * or power off. This will affect erase size. |
Lei Wen | ea52676 | 2011-06-22 17:03:31 +0000 | [diff] [blame] | 1271 | */ |
Diego Santa Cruz | a7a7599 | 2014-12-23 10:50:27 +0100 | [diff] [blame] | 1272 | if (part_completed) |
Diego Santa Cruz | cea8c5c | 2014-12-23 10:50:20 +0100 | [diff] [blame] | 1273 | has_parts = true; |
Oliver Metz | b3f1409 | 2013-10-01 20:32:07 +0200 | [diff] [blame] | 1274 | if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) && |
Diego Santa Cruz | cea8c5c | 2014-12-23 10:50:20 +0100 | [diff] [blame] | 1275 | (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB)) |
| 1276 | has_parts = true; |
| 1277 | if (has_parts) { |
Oliver Metz | b3f1409 | 2013-10-01 20:32:07 +0200 | [diff] [blame] | 1278 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
| 1279 | EXT_CSD_ERASE_GROUP_DEF, 1); |
| 1280 | |
| 1281 | if (err) |
| 1282 | return err; |
Hannes Petermaier | 15e874d | 2014-08-08 09:47:22 +0200 | [diff] [blame] | 1283 | else |
| 1284 | ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1; |
Diego Santa Cruz | 61b78fe | 2014-12-23 10:50:25 +0100 | [diff] [blame] | 1285 | } |
Oliver Metz | b3f1409 | 2013-10-01 20:32:07 +0200 | [diff] [blame] | 1286 | |
Diego Santa Cruz | 61b78fe | 2014-12-23 10:50:25 +0100 | [diff] [blame] | 1287 | if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) { |
Oliver Metz | b3f1409 | 2013-10-01 20:32:07 +0200 | [diff] [blame] | 1288 | /* Read out group size from ext_csd */ |
Lei Wen | 217467f | 2011-10-03 20:35:10 +0000 | [diff] [blame] | 1289 | mmc->erase_grp_size = |
Diego Santa Cruz | 747f6fa | 2014-12-23 10:50:24 +0100 | [diff] [blame] | 1290 | ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024; |
Markus Niebel | 6d39892 | 2014-11-18 15:11:42 +0100 | [diff] [blame] | 1291 | /* |
| 1292 | * if high capacity and partition setting completed |
| 1293 | * SEC_COUNT is valid even if it is smaller than 2 GiB |
| 1294 | * JEDEC Standard JESD84-B45, 6.2.4 |
| 1295 | */ |
Diego Santa Cruz | a7a7599 | 2014-12-23 10:50:27 +0100 | [diff] [blame] | 1296 | if (mmc->high_capacity && part_completed) { |
Markus Niebel | 6d39892 | 2014-11-18 15:11:42 +0100 | [diff] [blame] | 1297 | capacity = (ext_csd[EXT_CSD_SEC_CNT]) | |
| 1298 | (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) | |
| 1299 | (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) | |
| 1300 | (ext_csd[EXT_CSD_SEC_CNT + 3] << 24); |
| 1301 | capacity *= MMC_MAX_BLOCK_LEN; |
| 1302 | mmc->capacity_user = capacity; |
| 1303 | } |
Simon Glass | a09c2b7 | 2013-04-03 08:54:30 +0000 | [diff] [blame] | 1304 | } else { |
Oliver Metz | b3f1409 | 2013-10-01 20:32:07 +0200 | [diff] [blame] | 1305 | /* Calculate the group size from the csd value. */ |
Lei Wen | ea52676 | 2011-06-22 17:03:31 +0000 | [diff] [blame] | 1306 | int erase_gsz, erase_gmul; |
| 1307 | erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10; |
| 1308 | erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5; |
| 1309 | mmc->erase_grp_size = (erase_gsz + 1) |
| 1310 | * (erase_gmul + 1); |
| 1311 | } |
Diego Santa Cruz | 61b78fe | 2014-12-23 10:50:25 +0100 | [diff] [blame] | 1312 | |
| 1313 | mmc->hc_wp_grp_size = 1024 |
| 1314 | * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] |
| 1315 | * ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; |
Diego Santa Cruz | 37a50b9 | 2014-12-23 10:50:33 +0100 | [diff] [blame] | 1316 | |
| 1317 | mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET]; |
Sukumar Ghorai | 232293c | 2010-09-20 18:29:29 +0530 | [diff] [blame] | 1318 | } |
| 1319 | |
Stephen Warren | e315ae8 | 2013-06-11 15:14:01 -0600 | [diff] [blame] | 1320 | err = mmc_set_capacity(mmc, mmc->part_num); |
| 1321 | if (err) |
| 1322 | return err; |
| 1323 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1324 | if (IS_SD(mmc)) |
| 1325 | err = sd_change_freq(mmc); |
| 1326 | else |
| 1327 | err = mmc_change_freq(mmc); |
| 1328 | |
| 1329 | if (err) |
| 1330 | return err; |
| 1331 | |
| 1332 | /* Restrict card's capabilities by what the host can do */ |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 1333 | mmc->card_caps &= mmc->cfg->host_caps; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1334 | |
| 1335 | if (IS_SD(mmc)) { |
| 1336 | if (mmc->card_caps & MMC_MODE_4BIT) { |
| 1337 | cmd.cmdidx = MMC_CMD_APP_CMD; |
| 1338 | cmd.resp_type = MMC_RSP_R1; |
| 1339 | cmd.cmdarg = mmc->rca << 16; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1340 | |
| 1341 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 1342 | if (err) |
| 1343 | return err; |
| 1344 | |
| 1345 | cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH; |
| 1346 | cmd.resp_type = MMC_RSP_R1; |
| 1347 | cmd.cmdarg = 2; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1348 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 1349 | if (err) |
| 1350 | return err; |
| 1351 | |
| 1352 | mmc_set_bus_width(mmc, 4); |
| 1353 | } |
| 1354 | |
| 1355 | if (mmc->card_caps & MMC_MODE_HS) |
Jaehoon Chung | e1d4c7b | 2012-03-26 21:16:03 +0000 | [diff] [blame] | 1356 | mmc->tran_speed = 50000000; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1357 | else |
Jaehoon Chung | e1d4c7b | 2012-03-26 21:16:03 +0000 | [diff] [blame] | 1358 | mmc->tran_speed = 25000000; |
Andrew Gabbasov | ccb7b04 | 2014-12-25 10:22:25 -0600 | [diff] [blame] | 1359 | } else if (mmc->version >= MMC_VERSION_4) { |
| 1360 | /* Only version 4 of MMC supports wider bus widths */ |
Andy Fleming | eb766ad | 2012-10-31 19:02:38 +0000 | [diff] [blame] | 1361 | int idx; |
| 1362 | |
| 1363 | /* An array of possible bus widths in order of preference */ |
| 1364 | static unsigned ext_csd_bits[] = { |
Jaehoon Chung | 38ce30b | 2014-05-16 13:59:54 +0900 | [diff] [blame] | 1365 | EXT_CSD_DDR_BUS_WIDTH_8, |
| 1366 | EXT_CSD_DDR_BUS_WIDTH_4, |
Andy Fleming | eb766ad | 2012-10-31 19:02:38 +0000 | [diff] [blame] | 1367 | EXT_CSD_BUS_WIDTH_8, |
| 1368 | EXT_CSD_BUS_WIDTH_4, |
| 1369 | EXT_CSD_BUS_WIDTH_1, |
| 1370 | }; |
| 1371 | |
| 1372 | /* An array to map CSD bus widths to host cap bits */ |
| 1373 | static unsigned ext_to_hostcaps[] = { |
Andrew Gabbasov | 9fc2a41 | 2014-12-01 06:59:09 -0600 | [diff] [blame] | 1374 | [EXT_CSD_DDR_BUS_WIDTH_4] = |
| 1375 | MMC_MODE_DDR_52MHz | MMC_MODE_4BIT, |
| 1376 | [EXT_CSD_DDR_BUS_WIDTH_8] = |
| 1377 | MMC_MODE_DDR_52MHz | MMC_MODE_8BIT, |
Andy Fleming | eb766ad | 2012-10-31 19:02:38 +0000 | [diff] [blame] | 1378 | [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT, |
| 1379 | [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT, |
| 1380 | }; |
| 1381 | |
| 1382 | /* An array to map chosen bus width to an integer */ |
| 1383 | static unsigned widths[] = { |
Jaehoon Chung | 38ce30b | 2014-05-16 13:59:54 +0900 | [diff] [blame] | 1384 | 8, 4, 8, 4, 1, |
Andy Fleming | eb766ad | 2012-10-31 19:02:38 +0000 | [diff] [blame] | 1385 | }; |
| 1386 | |
| 1387 | for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) { |
| 1388 | unsigned int extw = ext_csd_bits[idx]; |
Andrew Gabbasov | 9fc2a41 | 2014-12-01 06:59:09 -0600 | [diff] [blame] | 1389 | unsigned int caps = ext_to_hostcaps[extw]; |
Andy Fleming | eb766ad | 2012-10-31 19:02:38 +0000 | [diff] [blame] | 1390 | |
| 1391 | /* |
Andrew Gabbasov | c1b2cf0 | 2014-12-25 10:22:24 -0600 | [diff] [blame] | 1392 | * If the bus width is still not changed, |
| 1393 | * don't try to set the default again. |
| 1394 | * Otherwise, recover from switch attempts |
| 1395 | * by switching to 1-bit bus width. |
| 1396 | */ |
| 1397 | if (extw == EXT_CSD_BUS_WIDTH_1 && |
| 1398 | mmc->bus_width == 1) { |
| 1399 | err = 0; |
| 1400 | break; |
| 1401 | } |
| 1402 | |
| 1403 | /* |
Andrew Gabbasov | 9fc2a41 | 2014-12-01 06:59:09 -0600 | [diff] [blame] | 1404 | * Check to make sure the card and controller support |
| 1405 | * these capabilities |
Andy Fleming | eb766ad | 2012-10-31 19:02:38 +0000 | [diff] [blame] | 1406 | */ |
Andrew Gabbasov | 9fc2a41 | 2014-12-01 06:59:09 -0600 | [diff] [blame] | 1407 | if ((mmc->card_caps & caps) != caps) |
Andy Fleming | eb766ad | 2012-10-31 19:02:38 +0000 | [diff] [blame] | 1408 | continue; |
| 1409 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1410 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
Andy Fleming | eb766ad | 2012-10-31 19:02:38 +0000 | [diff] [blame] | 1411 | EXT_CSD_BUS_WIDTH, extw); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1412 | |
| 1413 | if (err) |
Lei Wen | 4f5a6a5 | 2011-10-03 20:35:11 +0000 | [diff] [blame] | 1414 | continue; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1415 | |
Andrew Gabbasov | 9fc2a41 | 2014-12-01 06:59:09 -0600 | [diff] [blame] | 1416 | mmc->ddr_mode = (caps & MMC_MODE_DDR_52MHz) ? 1 : 0; |
Andy Fleming | eb766ad | 2012-10-31 19:02:38 +0000 | [diff] [blame] | 1417 | mmc_set_bus_width(mmc, widths[idx]); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1418 | |
Lei Wen | 4f5a6a5 | 2011-10-03 20:35:11 +0000 | [diff] [blame] | 1419 | err = mmc_send_ext_csd(mmc, test_csd); |
Andrew Gabbasov | 9fc2a41 | 2014-12-01 06:59:09 -0600 | [diff] [blame] | 1420 | |
| 1421 | if (err) |
| 1422 | continue; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1423 | |
Andrew Gabbasov | 9fc2a41 | 2014-12-01 06:59:09 -0600 | [diff] [blame] | 1424 | /* Only compare read only fields */ |
| 1425 | if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] |
| 1426 | == test_csd[EXT_CSD_PARTITIONING_SUPPORT] && |
| 1427 | ext_csd[EXT_CSD_HC_WP_GRP_SIZE] |
| 1428 | == test_csd[EXT_CSD_HC_WP_GRP_SIZE] && |
| 1429 | ext_csd[EXT_CSD_REV] |
| 1430 | == test_csd[EXT_CSD_REV] && |
| 1431 | ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] |
| 1432 | == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] && |
| 1433 | memcmp(&ext_csd[EXT_CSD_SEC_CNT], |
| 1434 | &test_csd[EXT_CSD_SEC_CNT], 4) == 0) |
Lei Wen | 4f5a6a5 | 2011-10-03 20:35:11 +0000 | [diff] [blame] | 1435 | break; |
Andrew Gabbasov | 9fc2a41 | 2014-12-01 06:59:09 -0600 | [diff] [blame] | 1436 | else |
| 1437 | err = SWITCH_ERR; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1438 | } |
| 1439 | |
Andrew Gabbasov | 9fc2a41 | 2014-12-01 06:59:09 -0600 | [diff] [blame] | 1440 | if (err) |
| 1441 | return err; |
| 1442 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1443 | if (mmc->card_caps & MMC_MODE_HS) { |
| 1444 | if (mmc->card_caps & MMC_MODE_HS_52MHz) |
Jaehoon Chung | e1d4c7b | 2012-03-26 21:16:03 +0000 | [diff] [blame] | 1445 | mmc->tran_speed = 52000000; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1446 | else |
Jaehoon Chung | e1d4c7b | 2012-03-26 21:16:03 +0000 | [diff] [blame] | 1447 | mmc->tran_speed = 26000000; |
| 1448 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1449 | } |
| 1450 | |
Jaehoon Chung | e1d4c7b | 2012-03-26 21:16:03 +0000 | [diff] [blame] | 1451 | mmc_set_clock(mmc, mmc->tran_speed); |
| 1452 | |
Andrew Gabbasov | 532663b | 2014-12-01 06:59:11 -0600 | [diff] [blame] | 1453 | /* Fix the block length for DDR mode */ |
| 1454 | if (mmc->ddr_mode) { |
| 1455 | mmc->read_bl_len = MMC_MAX_BLOCK_LEN; |
| 1456 | mmc->write_bl_len = MMC_MAX_BLOCK_LEN; |
| 1457 | } |
| 1458 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1459 | /* fill in device description */ |
| 1460 | mmc->block_dev.lun = 0; |
| 1461 | mmc->block_dev.type = 0; |
| 1462 | mmc->block_dev.blksz = mmc->read_bl_len; |
Egbert Eich | 2eec2ab | 2013-04-09 21:11:56 +0000 | [diff] [blame] | 1463 | mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz); |
Rabin Vincent | 69d4e2c | 2009-04-05 13:30:54 +0530 | [diff] [blame] | 1464 | mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len); |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 1465 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
Taylor Hutt | 7367ec2 | 2012-10-20 17:15:59 +0000 | [diff] [blame] | 1466 | sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x", |
| 1467 | mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff), |
| 1468 | (mmc->cid[3] >> 16) & 0xffff); |
| 1469 | sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff, |
| 1470 | (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff, |
| 1471 | (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff, |
| 1472 | (mmc->cid[2] >> 24) & 0xff); |
| 1473 | sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf, |
| 1474 | (mmc->cid[2] >> 16) & 0xf); |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 1475 | #else |
| 1476 | mmc->block_dev.vendor[0] = 0; |
| 1477 | mmc->block_dev.product[0] = 0; |
| 1478 | mmc->block_dev.revision[0] = 0; |
| 1479 | #endif |
Mikhail Kshevetskiy | 5cbfa8e7a | 2012-07-09 08:53:38 +0000 | [diff] [blame] | 1480 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1481 | init_part(&mmc->block_dev); |
Mikhail Kshevetskiy | 5cbfa8e7a | 2012-07-09 08:53:38 +0000 | [diff] [blame] | 1482 | #endif |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1483 | |
| 1484 | return 0; |
| 1485 | } |
| 1486 | |
Kim Phillips | 87ea389 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 1487 | static int mmc_send_if_cond(struct mmc *mmc) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1488 | { |
| 1489 | struct mmc_cmd cmd; |
| 1490 | int err; |
| 1491 | |
| 1492 | cmd.cmdidx = SD_CMD_SEND_IF_COND; |
| 1493 | /* We set the bit if the host supports voltages between 2.7 and 3.6 V */ |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 1494 | cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1495 | cmd.resp_type = MMC_RSP_R7; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1496 | |
| 1497 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 1498 | |
| 1499 | if (err) |
| 1500 | return err; |
| 1501 | |
Rabin Vincent | b6eed94 | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 1502 | if ((cmd.response[0] & 0xff) != 0xaa) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1503 | return UNUSABLE_ERR; |
| 1504 | else |
| 1505 | mmc->version = SD_VERSION_2; |
| 1506 | |
| 1507 | return 0; |
| 1508 | } |
| 1509 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 1510 | /* not used any more */ |
| 1511 | int __deprecated mmc_register(struct mmc *mmc) |
| 1512 | { |
| 1513 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
| 1514 | printf("%s is deprecated! use mmc_create() instead.\n", __func__); |
| 1515 | #endif |
| 1516 | return -1; |
| 1517 | } |
| 1518 | |
| 1519 | struct mmc *mmc_create(const struct mmc_config *cfg, void *priv) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1520 | { |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 1521 | struct mmc *mmc; |
| 1522 | |
| 1523 | /* quick validation */ |
| 1524 | if (cfg == NULL || cfg->ops == NULL || cfg->ops->send_cmd == NULL || |
| 1525 | cfg->f_min == 0 || cfg->f_max == 0 || cfg->b_max == 0) |
| 1526 | return NULL; |
| 1527 | |
| 1528 | mmc = calloc(1, sizeof(*mmc)); |
| 1529 | if (mmc == NULL) |
| 1530 | return NULL; |
| 1531 | |
| 1532 | mmc->cfg = cfg; |
| 1533 | mmc->priv = priv; |
| 1534 | |
| 1535 | /* the following chunk was mmc_register() */ |
| 1536 | |
Markus Niebel | 0395141 | 2013-12-16 13:40:46 +0100 | [diff] [blame] | 1537 | /* Setup dsr related values */ |
| 1538 | mmc->dsr_imp = 0; |
| 1539 | mmc->dsr = 0xffffffff; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1540 | /* Setup the universal parts of the block interface just once */ |
| 1541 | mmc->block_dev.if_type = IF_TYPE_MMC; |
| 1542 | mmc->block_dev.dev = cur_dev_num++; |
| 1543 | mmc->block_dev.removable = 1; |
| 1544 | mmc->block_dev.block_read = mmc_bread; |
| 1545 | mmc->block_dev.block_write = mmc_bwrite; |
Lei Wen | ea52676 | 2011-06-22 17:03:31 +0000 | [diff] [blame] | 1546 | mmc->block_dev.block_erase = mmc_berase; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1547 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 1548 | /* setup initial part type */ |
| 1549 | mmc->block_dev.part_type = mmc->cfg->part_type; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1550 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 1551 | INIT_LIST_HEAD(&mmc->link); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1552 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 1553 | list_add_tail(&mmc->link, &mmc_devices); |
| 1554 | |
| 1555 | return mmc; |
| 1556 | } |
| 1557 | |
| 1558 | void mmc_destroy(struct mmc *mmc) |
| 1559 | { |
| 1560 | /* only freeing memory for now */ |
| 1561 | free(mmc); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1562 | } |
| 1563 | |
Matthew McClintock | 6252b4f | 2011-05-24 05:31:19 +0000 | [diff] [blame] | 1564 | #ifdef CONFIG_PARTITIONS |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1565 | block_dev_desc_t *mmc_get_dev(int dev) |
| 1566 | { |
| 1567 | struct mmc *mmc = find_mmc_device(dev); |
Benoît Thébaudeau | 6ce8aed | 2012-08-10 08:59:12 +0000 | [diff] [blame] | 1568 | if (!mmc || mmc_init(mmc)) |
Łukasz Majewski | 65b04cf | 2012-04-19 02:39:18 +0000 | [diff] [blame] | 1569 | return NULL; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1570 | |
Łukasz Majewski | 65b04cf | 2012-04-19 02:39:18 +0000 | [diff] [blame] | 1571 | return &mmc->block_dev; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1572 | } |
Matthew McClintock | 6252b4f | 2011-05-24 05:31:19 +0000 | [diff] [blame] | 1573 | #endif |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1574 | |
Paul Kocialkowski | 2439fe9 | 2014-11-08 20:55:45 +0100 | [diff] [blame] | 1575 | /* board-specific MMC power initializations. */ |
| 1576 | __weak void board_mmc_power_init(void) |
| 1577 | { |
| 1578 | } |
| 1579 | |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 1580 | int mmc_start_init(struct mmc *mmc) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1581 | { |
Macpaul Lin | 028bde1 | 2011-11-14 23:35:39 +0000 | [diff] [blame] | 1582 | int err; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1583 | |
Pantelis Antoniou | c9e7591 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 1584 | /* we pretend there's no card when init is NULL */ |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 1585 | if (mmc_getcd(mmc) == 0 || mmc->cfg->ops->init == NULL) { |
Thierry Reding | b9c8b77 | 2012-01-02 01:15:37 +0000 | [diff] [blame] | 1586 | mmc->has_init = 0; |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 1587 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
Thierry Reding | b9c8b77 | 2012-01-02 01:15:37 +0000 | [diff] [blame] | 1588 | printf("MMC: no card present\n"); |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 1589 | #endif |
Thierry Reding | b9c8b77 | 2012-01-02 01:15:37 +0000 | [diff] [blame] | 1590 | return NO_CARD_ERR; |
| 1591 | } |
| 1592 | |
Lei Wen | 31b9980 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 1593 | if (mmc->has_init) |
| 1594 | return 0; |
| 1595 | |
Paul Kocialkowski | 2439fe9 | 2014-11-08 20:55:45 +0100 | [diff] [blame] | 1596 | board_mmc_power_init(); |
| 1597 | |
Pantelis Antoniou | c9e7591 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 1598 | /* made sure it's not NULL earlier */ |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 1599 | err = mmc->cfg->ops->init(mmc); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1600 | |
| 1601 | if (err) |
| 1602 | return err; |
| 1603 | |
Andrew Gabbasov | 9fc2a41 | 2014-12-01 06:59:09 -0600 | [diff] [blame] | 1604 | mmc->ddr_mode = 0; |
Ilya Yanok | 8459aab | 2009-06-29 17:53:16 +0400 | [diff] [blame] | 1605 | mmc_set_bus_width(mmc, 1); |
| 1606 | mmc_set_clock(mmc, 1); |
| 1607 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1608 | /* Reset the Card */ |
| 1609 | err = mmc_go_idle(mmc); |
| 1610 | |
| 1611 | if (err) |
| 1612 | return err; |
| 1613 | |
Lei Wen | 31b9980 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 1614 | /* The internal partition reset to user partition(0) at every CMD0*/ |
| 1615 | mmc->part_num = 0; |
| 1616 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1617 | /* Test for SD version 2 */ |
Macpaul Lin | 028bde1 | 2011-11-14 23:35:39 +0000 | [diff] [blame] | 1618 | err = mmc_send_if_cond(mmc); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1619 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1620 | /* Now try to get the SD card's operating condition */ |
| 1621 | err = sd_send_op_cond(mmc); |
| 1622 | |
| 1623 | /* If the command timed out, we check for an MMC card */ |
| 1624 | if (err == TIMEOUT) { |
| 1625 | err = mmc_send_op_cond(mmc); |
| 1626 | |
Andrew Gabbasov | 3a669bc | 2015-03-19 07:44:07 -0500 | [diff] [blame] | 1627 | if (err) { |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 1628 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1629 | printf("Card did not respond to voltage select!\n"); |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 1630 | #endif |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1631 | return UNUSABLE_ERR; |
| 1632 | } |
| 1633 | } |
| 1634 | |
Andrew Gabbasov | 3a669bc | 2015-03-19 07:44:07 -0500 | [diff] [blame] | 1635 | if (!err) |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 1636 | mmc->init_in_progress = 1; |
| 1637 | |
| 1638 | return err; |
| 1639 | } |
| 1640 | |
| 1641 | static int mmc_complete_init(struct mmc *mmc) |
| 1642 | { |
| 1643 | int err = 0; |
| 1644 | |
Andrew Gabbasov | 3a669bc | 2015-03-19 07:44:07 -0500 | [diff] [blame] | 1645 | mmc->init_in_progress = 0; |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 1646 | if (mmc->op_cond_pending) |
| 1647 | err = mmc_complete_op_cond(mmc); |
| 1648 | |
| 1649 | if (!err) |
| 1650 | err = mmc_startup(mmc); |
Lei Wen | 31b9980 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 1651 | if (err) |
| 1652 | mmc->has_init = 0; |
| 1653 | else |
| 1654 | mmc->has_init = 1; |
| 1655 | return err; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1656 | } |
| 1657 | |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 1658 | int mmc_init(struct mmc *mmc) |
| 1659 | { |
Andrew Gabbasov | 3a669bc | 2015-03-19 07:44:07 -0500 | [diff] [blame] | 1660 | int err = 0; |
Mateusz Zalega | da35178 | 2014-04-29 20:15:30 +0200 | [diff] [blame] | 1661 | unsigned start; |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 1662 | |
| 1663 | if (mmc->has_init) |
| 1664 | return 0; |
Mateusz Zalega | da35178 | 2014-04-29 20:15:30 +0200 | [diff] [blame] | 1665 | |
| 1666 | start = get_timer(0); |
| 1667 | |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 1668 | if (!mmc->init_in_progress) |
| 1669 | err = mmc_start_init(mmc); |
| 1670 | |
Andrew Gabbasov | 3a669bc | 2015-03-19 07:44:07 -0500 | [diff] [blame] | 1671 | if (!err) |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 1672 | err = mmc_complete_init(mmc); |
| 1673 | debug("%s: %d, time %lu\n", __func__, err, get_timer(start)); |
| 1674 | return err; |
| 1675 | } |
| 1676 | |
Markus Niebel | 0395141 | 2013-12-16 13:40:46 +0100 | [diff] [blame] | 1677 | int mmc_set_dsr(struct mmc *mmc, u16 val) |
| 1678 | { |
| 1679 | mmc->dsr = val; |
| 1680 | return 0; |
| 1681 | } |
| 1682 | |
Jeroen Hofstee | 4772630 | 2014-07-10 22:46:28 +0200 | [diff] [blame] | 1683 | /* CPU-specific MMC initializations */ |
| 1684 | __weak int cpu_mmc_init(bd_t *bis) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1685 | { |
| 1686 | return -1; |
| 1687 | } |
| 1688 | |
Jeroen Hofstee | 4772630 | 2014-07-10 22:46:28 +0200 | [diff] [blame] | 1689 | /* board-specific MMC initializations. */ |
| 1690 | __weak int board_mmc_init(bd_t *bis) |
| 1691 | { |
| 1692 | return -1; |
| 1693 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1694 | |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 1695 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
| 1696 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1697 | void print_mmc_devices(char separator) |
| 1698 | { |
| 1699 | struct mmc *m; |
| 1700 | struct list_head *entry; |
Przemyslaw Marczak | 7bc4e30 | 2015-02-20 12:29:27 +0100 | [diff] [blame] | 1701 | char *mmc_type; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1702 | |
| 1703 | list_for_each(entry, &mmc_devices) { |
| 1704 | m = list_entry(entry, struct mmc, link); |
| 1705 | |
Przemyslaw Marczak | 7bc4e30 | 2015-02-20 12:29:27 +0100 | [diff] [blame] | 1706 | if (m->has_init) |
| 1707 | mmc_type = IS_SD(m) ? "SD" : "eMMC"; |
| 1708 | else |
| 1709 | mmc_type = NULL; |
| 1710 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 1711 | printf("%s: %d", m->cfg->name, m->block_dev.dev); |
Przemyslaw Marczak | 7bc4e30 | 2015-02-20 12:29:27 +0100 | [diff] [blame] | 1712 | if (mmc_type) |
| 1713 | printf(" (%s)", mmc_type); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1714 | |
Lubomir Popov | 456104e | 2014-11-11 12:25:42 +0200 | [diff] [blame] | 1715 | if (entry->next != &mmc_devices) { |
| 1716 | printf("%c", separator); |
| 1717 | if (separator != '\n') |
| 1718 | puts (" "); |
| 1719 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | printf("\n"); |
| 1723 | } |
| 1724 | |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 1725 | #else |
| 1726 | void print_mmc_devices(char separator) { } |
| 1727 | #endif |
| 1728 | |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 1729 | int get_mmc_num(void) |
| 1730 | { |
| 1731 | return cur_dev_num; |
| 1732 | } |
| 1733 | |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 1734 | void mmc_set_preinit(struct mmc *mmc, int preinit) |
| 1735 | { |
| 1736 | mmc->preinit = preinit; |
| 1737 | } |
| 1738 | |
| 1739 | static void do_preinit(void) |
| 1740 | { |
| 1741 | struct mmc *m; |
| 1742 | struct list_head *entry; |
| 1743 | |
| 1744 | list_for_each(entry, &mmc_devices) { |
| 1745 | m = list_entry(entry, struct mmc, link); |
| 1746 | |
| 1747 | if (m->preinit) |
| 1748 | mmc_start_init(m); |
| 1749 | } |
| 1750 | } |
| 1751 | |
| 1752 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1753 | int mmc_initialize(bd_t *bis) |
| 1754 | { |
| 1755 | INIT_LIST_HEAD (&mmc_devices); |
| 1756 | cur_dev_num = 0; |
| 1757 | |
| 1758 | if (board_mmc_init(bis) < 0) |
| 1759 | cpu_mmc_init(bis); |
| 1760 | |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 1761 | #ifndef CONFIG_SPL_BUILD |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1762 | print_mmc_devices(','); |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 1763 | #endif |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1764 | |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 1765 | do_preinit(); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1766 | return 0; |
| 1767 | } |
Amar | 1104e9b | 2013-04-27 11:42:58 +0530 | [diff] [blame] | 1768 | |
| 1769 | #ifdef CONFIG_SUPPORT_EMMC_BOOT |
| 1770 | /* |
| 1771 | * This function changes the size of boot partition and the size of rpmb |
| 1772 | * partition present on EMMC devices. |
| 1773 | * |
| 1774 | * Input Parameters: |
| 1775 | * struct *mmc: pointer for the mmc device strcuture |
| 1776 | * bootsize: size of boot partition |
| 1777 | * rpmbsize: size of rpmb partition |
| 1778 | * |
| 1779 | * Returns 0 on success. |
| 1780 | */ |
| 1781 | |
| 1782 | int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize, |
| 1783 | unsigned long rpmbsize) |
| 1784 | { |
| 1785 | int err; |
| 1786 | struct mmc_cmd cmd; |
| 1787 | |
| 1788 | /* Only use this command for raw EMMC moviNAND. Enter backdoor mode */ |
| 1789 | cmd.cmdidx = MMC_CMD_RES_MAN; |
| 1790 | cmd.resp_type = MMC_RSP_R1b; |
| 1791 | cmd.cmdarg = MMC_CMD62_ARG1; |
| 1792 | |
| 1793 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 1794 | if (err) { |
| 1795 | debug("mmc_boot_partition_size_change: Error1 = %d\n", err); |
| 1796 | return err; |
| 1797 | } |
| 1798 | |
| 1799 | /* Boot partition changing mode */ |
| 1800 | cmd.cmdidx = MMC_CMD_RES_MAN; |
| 1801 | cmd.resp_type = MMC_RSP_R1b; |
| 1802 | cmd.cmdarg = MMC_CMD62_ARG2; |
| 1803 | |
| 1804 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 1805 | if (err) { |
| 1806 | debug("mmc_boot_partition_size_change: Error2 = %d\n", err); |
| 1807 | return err; |
| 1808 | } |
| 1809 | /* boot partition size is multiple of 128KB */ |
| 1810 | bootsize = (bootsize * 1024) / 128; |
| 1811 | |
| 1812 | /* Arg: boot partition size */ |
| 1813 | cmd.cmdidx = MMC_CMD_RES_MAN; |
| 1814 | cmd.resp_type = MMC_RSP_R1b; |
| 1815 | cmd.cmdarg = bootsize; |
| 1816 | |
| 1817 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 1818 | if (err) { |
| 1819 | debug("mmc_boot_partition_size_change: Error3 = %d\n", err); |
| 1820 | return err; |
| 1821 | } |
| 1822 | /* RPMB partition size is multiple of 128KB */ |
| 1823 | rpmbsize = (rpmbsize * 1024) / 128; |
| 1824 | /* Arg: RPMB partition size */ |
| 1825 | cmd.cmdidx = MMC_CMD_RES_MAN; |
| 1826 | cmd.resp_type = MMC_RSP_R1b; |
| 1827 | cmd.cmdarg = rpmbsize; |
| 1828 | |
| 1829 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 1830 | if (err) { |
| 1831 | debug("mmc_boot_partition_size_change: Error4 = %d\n", err); |
| 1832 | return err; |
| 1833 | } |
| 1834 | return 0; |
| 1835 | } |
| 1836 | |
| 1837 | /* |
Tom Rini | 4cf854c | 2014-02-05 10:24:22 -0500 | [diff] [blame] | 1838 | * Modify EXT_CSD[177] which is BOOT_BUS_WIDTH |
| 1839 | * based on the passed in values for BOOT_BUS_WIDTH, RESET_BOOT_BUS_WIDTH |
| 1840 | * and BOOT_MODE. |
| 1841 | * |
| 1842 | * Returns 0 on success. |
| 1843 | */ |
| 1844 | int mmc_set_boot_bus_width(struct mmc *mmc, u8 width, u8 reset, u8 mode) |
| 1845 | { |
| 1846 | int err; |
| 1847 | |
| 1848 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_BUS_WIDTH, |
| 1849 | EXT_CSD_BOOT_BUS_WIDTH_MODE(mode) | |
| 1850 | EXT_CSD_BOOT_BUS_WIDTH_RESET(reset) | |
| 1851 | EXT_CSD_BOOT_BUS_WIDTH_WIDTH(width)); |
| 1852 | |
| 1853 | if (err) |
| 1854 | return err; |
| 1855 | return 0; |
| 1856 | } |
| 1857 | |
| 1858 | /* |
Tom Rini | f8c6f79 | 2014-02-05 10:24:21 -0500 | [diff] [blame] | 1859 | * Modify EXT_CSD[179] which is PARTITION_CONFIG (formerly BOOT_CONFIG) |
| 1860 | * based on the passed in values for BOOT_ACK, BOOT_PARTITION_ENABLE and |
| 1861 | * PARTITION_ACCESS. |
| 1862 | * |
| 1863 | * Returns 0 on success. |
| 1864 | */ |
| 1865 | int mmc_set_part_conf(struct mmc *mmc, u8 ack, u8 part_num, u8 access) |
| 1866 | { |
| 1867 | int err; |
| 1868 | |
| 1869 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF, |
| 1870 | EXT_CSD_BOOT_ACK(ack) | |
| 1871 | EXT_CSD_BOOT_PART_NUM(part_num) | |
| 1872 | EXT_CSD_PARTITION_ACCESS(access)); |
| 1873 | |
| 1874 | if (err) |
| 1875 | return err; |
| 1876 | return 0; |
| 1877 | } |
Tom Rini | 35a3ea1 | 2014-02-07 14:15:20 -0500 | [diff] [blame] | 1878 | |
| 1879 | /* |
| 1880 | * Modify EXT_CSD[162] which is RST_n_FUNCTION based on the given value |
| 1881 | * for enable. Note that this is a write-once field for non-zero values. |
| 1882 | * |
| 1883 | * Returns 0 on success. |
| 1884 | */ |
| 1885 | int mmc_set_rst_n_function(struct mmc *mmc, u8 enable) |
| 1886 | { |
| 1887 | return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_RST_N_FUNCTION, |
| 1888 | enable); |
| 1889 | } |
Amar | 1104e9b | 2013-04-27 11:42:58 +0530 | [diff] [blame] | 1890 | #endif |