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