blob: 22896403754beb6c1b1d0481d02ceecf9e3b4f13 [file] [log] [blame]
Paul Burton8d30cc92013-09-09 15:30:26 +01001/*
2 * Copyright 2008, Freescale Semiconductor, Inc
3 * Andy Fleming
4 *
5 * Based vaguely on the Linux code
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10#include <config.h>
11#include <common.h>
Simon Glass59bc6f22016-05-01 13:52:41 -060012#include <dm.h>
Paul Burton8d30cc92013-09-09 15:30:26 +010013#include <part.h>
Tom Rini58e31942015-06-11 20:53:31 -040014#include <div64.h>
15#include <linux/math64.h>
Paul Burton8d30cc92013-09-09 15:30:26 +010016#include "mmc_private.h"
17
18static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
19{
20 struct mmc_cmd cmd;
21 ulong end;
22 int err, start_cmd, end_cmd;
23
24 if (mmc->high_capacity) {
25 end = start + blkcnt - 1;
26 } else {
27 end = (start + blkcnt - 1) * mmc->write_bl_len;
28 start *= mmc->write_bl_len;
29 }
30
31 if (IS_SD(mmc)) {
32 start_cmd = SD_CMD_ERASE_WR_BLK_START;
33 end_cmd = SD_CMD_ERASE_WR_BLK_END;
34 } else {
35 start_cmd = MMC_CMD_ERASE_GROUP_START;
36 end_cmd = MMC_CMD_ERASE_GROUP_END;
37 }
38
39 cmd.cmdidx = start_cmd;
40 cmd.cmdarg = start;
41 cmd.resp_type = MMC_RSP_R1;
42
43 err = mmc_send_cmd(mmc, &cmd, NULL);
44 if (err)
45 goto err_out;
46
47 cmd.cmdidx = end_cmd;
48 cmd.cmdarg = end;
49
50 err = mmc_send_cmd(mmc, &cmd, NULL);
51 if (err)
52 goto err_out;
53
54 cmd.cmdidx = MMC_CMD_ERASE;
Eric Nelson957e0662015-12-07 07:50:01 -070055 cmd.cmdarg = MMC_ERASE_ARG;
Paul Burton8d30cc92013-09-09 15:30:26 +010056 cmd.resp_type = MMC_RSP_R1b;
57
58 err = mmc_send_cmd(mmc, &cmd, NULL);
59 if (err)
60 goto err_out;
61
62 return 0;
63
64err_out:
65 puts("mmc erase failed\n");
66 return err;
67}
68
Simon Glasse3394752016-02-29 15:25:34 -070069unsigned long mmc_berase(struct blk_desc *block_dev, lbaint_t start,
Stephen Warrene73f2962015-12-07 11:38:48 -070070 lbaint_t blkcnt)
Paul Burton8d30cc92013-09-09 15:30:26 +010071{
Simon Glass2f26fff2016-02-29 15:25:51 -070072 int dev_num = block_dev->devnum;
Paul Burton8d30cc92013-09-09 15:30:26 +010073 int err = 0;
Tom Rini58e31942015-06-11 20:53:31 -040074 u32 start_rem, blkcnt_rem;
Paul Burton8d30cc92013-09-09 15:30:26 +010075 struct mmc *mmc = find_mmc_device(dev_num);
76 lbaint_t blk = 0, blk_r = 0;
77 int timeout = 1000;
78
79 if (!mmc)
80 return -1;
81
Simon Glass11f2bb62016-05-01 13:52:29 -060082 err = blk_select_hwpart_devnum(IF_TYPE_MMC, dev_num,
83 block_dev->hwpart);
Stephen Warren1e0f92a2015-12-07 11:38:49 -070084 if (err < 0)
85 return -1;
86
Tom Rini58e31942015-06-11 20:53:31 -040087 /*
88 * We want to see if the requested start or total block count are
89 * unaligned. We discard the whole numbers and only care about the
90 * remainder.
91 */
92 err = div_u64_rem(start, mmc->erase_grp_size, &start_rem);
93 err = div_u64_rem(blkcnt, mmc->erase_grp_size, &blkcnt_rem);
94 if (start_rem || blkcnt_rem)
Paul Burton8d30cc92013-09-09 15:30:26 +010095 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
96 "The erase range would be change to "
97 "0x" LBAF "~0x" LBAF "\n\n",
98 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
99 ((start + blkcnt + mmc->erase_grp_size)
100 & ~(mmc->erase_grp_size - 1)) - 1);
101
102 while (blk < blkcnt) {
Peng Fan32260452016-09-01 11:13:39 +0800103 if (IS_SD(mmc) && mmc->ssr.au) {
104 blk_r = ((blkcnt - blk) > mmc->ssr.au) ?
105 mmc->ssr.au : (blkcnt - blk);
106 } else {
107 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
108 mmc->erase_grp_size : (blkcnt - blk);
109 }
Paul Burton8d30cc92013-09-09 15:30:26 +0100110 err = mmc_erase_t(mmc, start + blk, blk_r);
111 if (err)
112 break;
113
114 blk += blk_r;
115
116 /* Waiting for the ready status */
117 if (mmc_send_status(mmc, timeout))
118 return 0;
119 }
120
121 return blk;
122}
123
124static ulong mmc_write_blocks(struct mmc *mmc, lbaint_t start,
125 lbaint_t blkcnt, const void *src)
126{
127 struct mmc_cmd cmd;
128 struct mmc_data data;
129 int timeout = 1000;
130
Simon Glasse5db1152016-05-01 13:52:35 -0600131 if ((start + blkcnt) > mmc_get_blk_desc(mmc)->lba) {
Paul Burton8d30cc92013-09-09 15:30:26 +0100132 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
Simon Glasse5db1152016-05-01 13:52:35 -0600133 start + blkcnt, mmc_get_blk_desc(mmc)->lba);
Paul Burton8d30cc92013-09-09 15:30:26 +0100134 return 0;
135 }
136
137 if (blkcnt == 0)
138 return 0;
139 else if (blkcnt == 1)
140 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
141 else
142 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
143
144 if (mmc->high_capacity)
145 cmd.cmdarg = start;
146 else
147 cmd.cmdarg = start * mmc->write_bl_len;
148
149 cmd.resp_type = MMC_RSP_R1;
150
151 data.src = src;
152 data.blocks = blkcnt;
153 data.blocksize = mmc->write_bl_len;
154 data.flags = MMC_DATA_WRITE;
155
156 if (mmc_send_cmd(mmc, &cmd, &data)) {
157 printf("mmc write failed\n");
158 return 0;
159 }
160
161 /* SPI multiblock writes terminate using a special
162 * token, not a STOP_TRANSMISSION request.
163 */
164 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
165 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
166 cmd.cmdarg = 0;
167 cmd.resp_type = MMC_RSP_R1b;
168 if (mmc_send_cmd(mmc, &cmd, NULL)) {
169 printf("mmc fail to send stop cmd\n");
170 return 0;
171 }
172 }
173
174 /* Waiting for the ready status */
175 if (mmc_send_status(mmc, timeout))
176 return 0;
177
178 return blkcnt;
179}
180
Simon Glass59bc6f22016-05-01 13:52:41 -0600181#ifdef CONFIG_BLK
182ulong mmc_bwrite(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
183 const void *src)
184#else
Simon Glasse3394752016-02-29 15:25:34 -0700185ulong mmc_bwrite(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
Stephen Warrene73f2962015-12-07 11:38:48 -0700186 const void *src)
Simon Glass59bc6f22016-05-01 13:52:41 -0600187#endif
Paul Burton8d30cc92013-09-09 15:30:26 +0100188{
Simon Glass59bc6f22016-05-01 13:52:41 -0600189#ifdef CONFIG_BLK
190 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
191#endif
Simon Glass2f26fff2016-02-29 15:25:51 -0700192 int dev_num = block_dev->devnum;
Paul Burton8d30cc92013-09-09 15:30:26 +0100193 lbaint_t cur, blocks_todo = blkcnt;
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700194 int err;
Paul Burton8d30cc92013-09-09 15:30:26 +0100195
196 struct mmc *mmc = find_mmc_device(dev_num);
197 if (!mmc)
198 return 0;
199
Simon Glass11f2bb62016-05-01 13:52:29 -0600200 err = blk_select_hwpart_devnum(IF_TYPE_MMC, dev_num, block_dev->hwpart);
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700201 if (err < 0)
202 return 0;
203
Paul Burton8d30cc92013-09-09 15:30:26 +0100204 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
205 return 0;
206
207 do {
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200208 cur = (blocks_todo > mmc->cfg->b_max) ?
209 mmc->cfg->b_max : blocks_todo;
Paul Burton8d30cc92013-09-09 15:30:26 +0100210 if (mmc_write_blocks(mmc, start, cur, src) != cur)
211 return 0;
212 blocks_todo -= cur;
213 start += cur;
214 src += cur * mmc->write_bl_len;
215 } while (blocks_todo > 0);
216
217 return blkcnt;
218}