blob: b52ff9f3bc0fd53d9cd769b9f80a2d5dd2fa26fb [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Paul Burtonda61fa52013-09-09 15:30:26 +01002/*
3 * Copyright 2008, Freescale Semiconductor, Inc
4 * Andy Fleming
5 *
6 * Based vaguely on the Linux code
Paul Burtonda61fa52013-09-09 15:30:26 +01007 */
8
9#include <config.h>
10#include <common.h>
Simon Glass33fb2112016-05-01 13:52:41 -060011#include <dm.h>
Paul Burtonda61fa52013-09-09 15:30:26 +010012#include <part.h>
Tom Rini180f87f2015-06-11 20:53:31 -040013#include <div64.h>
14#include <linux/math64.h>
Paul Burtonda61fa52013-09-09 15:30:26 +010015#include "mmc_private.h"
16
17static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
18{
19 struct mmc_cmd cmd;
20 ulong end;
21 int err, start_cmd, end_cmd;
22
23 if (mmc->high_capacity) {
24 end = start + blkcnt - 1;
25 } else {
26 end = (start + blkcnt - 1) * mmc->write_bl_len;
27 start *= mmc->write_bl_len;
28 }
29
30 if (IS_SD(mmc)) {
31 start_cmd = SD_CMD_ERASE_WR_BLK_START;
32 end_cmd = SD_CMD_ERASE_WR_BLK_END;
33 } else {
34 start_cmd = MMC_CMD_ERASE_GROUP_START;
35 end_cmd = MMC_CMD_ERASE_GROUP_END;
36 }
37
38 cmd.cmdidx = start_cmd;
39 cmd.cmdarg = start;
40 cmd.resp_type = MMC_RSP_R1;
41
42 err = mmc_send_cmd(mmc, &cmd, NULL);
43 if (err)
44 goto err_out;
45
46 cmd.cmdidx = end_cmd;
47 cmd.cmdarg = end;
48
49 err = mmc_send_cmd(mmc, &cmd, NULL);
50 if (err)
51 goto err_out;
52
53 cmd.cmdidx = MMC_CMD_ERASE;
Eric Nelson1aa2d072015-12-07 07:50:01 -070054 cmd.cmdarg = MMC_ERASE_ARG;
Paul Burtonda61fa52013-09-09 15:30:26 +010055 cmd.resp_type = MMC_RSP_R1b;
56
57 err = mmc_send_cmd(mmc, &cmd, NULL);
58 if (err)
59 goto err_out;
60
61 return 0;
62
63err_out:
64 puts("mmc erase failed\n");
65 return err;
66}
67
Ezequiel Garcia2a4bb3d2019-01-07 18:13:24 -030068#if CONFIG_IS_ENABLED(BLK)
Simon Glass561e6242016-10-01 14:43:17 -060069ulong mmc_berase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt)
70#else
71ulong mmc_berase(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt)
72#endif
Paul Burtonda61fa52013-09-09 15:30:26 +010073{
Ezequiel Garcia2a4bb3d2019-01-07 18:13:24 -030074#if CONFIG_IS_ENABLED(BLK)
Simon Glass561e6242016-10-01 14:43:17 -060075 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
76#endif
Simon Glassbcce53d2016-02-29 15:25:51 -070077 int dev_num = block_dev->devnum;
Paul Burtonda61fa52013-09-09 15:30:26 +010078 int err = 0;
Tom Rini180f87f2015-06-11 20:53:31 -040079 u32 start_rem, blkcnt_rem;
Paul Burtonda61fa52013-09-09 15:30:26 +010080 struct mmc *mmc = find_mmc_device(dev_num);
81 lbaint_t blk = 0, blk_r = 0;
Sam Protsenko6cf8a902019-08-14 22:52:51 +030082 int timeout_ms = 1000;
Paul Burtonda61fa52013-09-09 15:30:26 +010083
84 if (!mmc)
85 return -1;
86
Simon Glass69f45cd2016-05-01 13:52:29 -060087 err = blk_select_hwpart_devnum(IF_TYPE_MMC, dev_num,
88 block_dev->hwpart);
Stephen Warren873cc1d2015-12-07 11:38:49 -070089 if (err < 0)
90 return -1;
91
Tom Rini180f87f2015-06-11 20:53:31 -040092 /*
93 * We want to see if the requested start or total block count are
94 * unaligned. We discard the whole numbers and only care about the
95 * remainder.
96 */
97 err = div_u64_rem(start, mmc->erase_grp_size, &start_rem);
98 err = div_u64_rem(blkcnt, mmc->erase_grp_size, &blkcnt_rem);
99 if (start_rem || blkcnt_rem)
Paul Burtonda61fa52013-09-09 15:30:26 +0100100 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
101 "The erase range would be change to "
102 "0x" LBAF "~0x" LBAF "\n\n",
103 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
104 ((start + blkcnt + mmc->erase_grp_size)
105 & ~(mmc->erase_grp_size - 1)) - 1);
106
107 while (blk < blkcnt) {
Peng Fane492dbb2016-09-01 11:13:39 +0800108 if (IS_SD(mmc) && mmc->ssr.au) {
109 blk_r = ((blkcnt - blk) > mmc->ssr.au) ?
110 mmc->ssr.au : (blkcnt - blk);
111 } else {
112 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
113 mmc->erase_grp_size : (blkcnt - blk);
114 }
Paul Burtonda61fa52013-09-09 15:30:26 +0100115 err = mmc_erase_t(mmc, start + blk, blk_r);
116 if (err)
117 break;
118
119 blk += blk_r;
120
121 /* Waiting for the ready status */
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300122 if (mmc_poll_for_busy(mmc, timeout_ms))
Paul Burtonda61fa52013-09-09 15:30:26 +0100123 return 0;
124 }
125
126 return blk;
127}
128
129static ulong mmc_write_blocks(struct mmc *mmc, lbaint_t start,
130 lbaint_t blkcnt, const void *src)
131{
132 struct mmc_cmd cmd;
133 struct mmc_data data;
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300134 int timeout_ms = 1000;
Paul Burtonda61fa52013-09-09 15:30:26 +0100135
Simon Glassc40fdca2016-05-01 13:52:35 -0600136 if ((start + blkcnt) > mmc_get_blk_desc(mmc)->lba) {
Paul Burtonda61fa52013-09-09 15:30:26 +0100137 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
Simon Glassc40fdca2016-05-01 13:52:35 -0600138 start + blkcnt, mmc_get_blk_desc(mmc)->lba);
Paul Burtonda61fa52013-09-09 15:30:26 +0100139 return 0;
140 }
141
142 if (blkcnt == 0)
143 return 0;
144 else if (blkcnt == 1)
145 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
146 else
147 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
148
149 if (mmc->high_capacity)
150 cmd.cmdarg = start;
151 else
152 cmd.cmdarg = start * mmc->write_bl_len;
153
154 cmd.resp_type = MMC_RSP_R1;
155
156 data.src = src;
157 data.blocks = blkcnt;
158 data.blocksize = mmc->write_bl_len;
159 data.flags = MMC_DATA_WRITE;
160
161 if (mmc_send_cmd(mmc, &cmd, &data)) {
162 printf("mmc write failed\n");
163 return 0;
164 }
165
166 /* SPI multiblock writes terminate using a special
167 * token, not a STOP_TRANSMISSION request.
168 */
169 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
170 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
171 cmd.cmdarg = 0;
172 cmd.resp_type = MMC_RSP_R1b;
173 if (mmc_send_cmd(mmc, &cmd, NULL)) {
174 printf("mmc fail to send stop cmd\n");
175 return 0;
176 }
177 }
178
179 /* Waiting for the ready status */
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300180 if (mmc_poll_for_busy(mmc, timeout_ms))
Paul Burtonda61fa52013-09-09 15:30:26 +0100181 return 0;
182
183 return blkcnt;
184}
185
Ezequiel Garcia2a4bb3d2019-01-07 18:13:24 -0300186#if CONFIG_IS_ENABLED(BLK)
Simon Glass33fb2112016-05-01 13:52:41 -0600187ulong mmc_bwrite(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
188 const void *src)
189#else
Simon Glass4101f682016-02-29 15:25:34 -0700190ulong mmc_bwrite(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
Stephen Warren7c4213f2015-12-07 11:38:48 -0700191 const void *src)
Simon Glass33fb2112016-05-01 13:52:41 -0600192#endif
Paul Burtonda61fa52013-09-09 15:30:26 +0100193{
Ezequiel Garcia2a4bb3d2019-01-07 18:13:24 -0300194#if CONFIG_IS_ENABLED(BLK)
Simon Glass33fb2112016-05-01 13:52:41 -0600195 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
196#endif
Simon Glassbcce53d2016-02-29 15:25:51 -0700197 int dev_num = block_dev->devnum;
Paul Burtonda61fa52013-09-09 15:30:26 +0100198 lbaint_t cur, blocks_todo = blkcnt;
Stephen Warren873cc1d2015-12-07 11:38:49 -0700199 int err;
Paul Burtonda61fa52013-09-09 15:30:26 +0100200
201 struct mmc *mmc = find_mmc_device(dev_num);
202 if (!mmc)
203 return 0;
204
Simon Glass69f45cd2016-05-01 13:52:29 -0600205 err = blk_select_hwpart_devnum(IF_TYPE_MMC, dev_num, block_dev->hwpart);
Stephen Warren873cc1d2015-12-07 11:38:49 -0700206 if (err < 0)
207 return 0;
208
Paul Burtonda61fa52013-09-09 15:30:26 +0100209 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
210 return 0;
211
212 do {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200213 cur = (blocks_todo > mmc->cfg->b_max) ?
214 mmc->cfg->b_max : blocks_todo;
Paul Burtonda61fa52013-09-09 15:30:26 +0100215 if (mmc_write_blocks(mmc, start, cur, src) != cur)
216 return 0;
217 blocks_todo -= cur;
218 start += cur;
219 src += cur * mmc->write_bl_len;
220 } while (blocks_todo > 0);
221
222 return blkcnt;
223}