blob: 79b8c4d808fb32c79605dc046417135aed1def9c [file] [log] [blame]
Paul Burtonda61fa52013-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>
12#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
Stephen Warren7c4213f2015-12-07 11:38:48 -070068unsigned long mmc_berase(block_dev_desc_t *block_dev, lbaint_t start,
69 lbaint_t blkcnt)
Paul Burtonda61fa52013-09-09 15:30:26 +010070{
Stephen Warren7c4213f2015-12-07 11:38:48 -070071 int dev_num = block_dev->dev;
Paul Burtonda61fa52013-09-09 15:30:26 +010072 int err = 0;
Tom Rini180f87f2015-06-11 20:53:31 -040073 u32 start_rem, blkcnt_rem;
Paul Burtonda61fa52013-09-09 15:30:26 +010074 struct mmc *mmc = find_mmc_device(dev_num);
75 lbaint_t blk = 0, blk_r = 0;
76 int timeout = 1000;
77
78 if (!mmc)
79 return -1;
80
Stephen Warren873cc1d2015-12-07 11:38:49 -070081 err = mmc_select_hwpart(dev_num, block_dev->hwpart);
82 if (err < 0)
83 return -1;
84
Tom Rini180f87f2015-06-11 20:53:31 -040085 /*
86 * We want to see if the requested start or total block count are
87 * unaligned. We discard the whole numbers and only care about the
88 * remainder.
89 */
90 err = div_u64_rem(start, mmc->erase_grp_size, &start_rem);
91 err = div_u64_rem(blkcnt, mmc->erase_grp_size, &blkcnt_rem);
92 if (start_rem || blkcnt_rem)
Paul Burtonda61fa52013-09-09 15:30:26 +010093 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
94 "The erase range would be change to "
95 "0x" LBAF "~0x" LBAF "\n\n",
96 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
97 ((start + blkcnt + mmc->erase_grp_size)
98 & ~(mmc->erase_grp_size - 1)) - 1);
99
100 while (blk < blkcnt) {
101 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
102 mmc->erase_grp_size : (blkcnt - blk);
103 err = mmc_erase_t(mmc, start + blk, blk_r);
104 if (err)
105 break;
106
107 blk += blk_r;
108
109 /* Waiting for the ready status */
110 if (mmc_send_status(mmc, timeout))
111 return 0;
112 }
113
114 return blk;
115}
116
117static ulong mmc_write_blocks(struct mmc *mmc, lbaint_t start,
118 lbaint_t blkcnt, const void *src)
119{
120 struct mmc_cmd cmd;
121 struct mmc_data data;
122 int timeout = 1000;
123
124 if ((start + blkcnt) > mmc->block_dev.lba) {
125 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
126 start + blkcnt, mmc->block_dev.lba);
127 return 0;
128 }
129
130 if (blkcnt == 0)
131 return 0;
132 else if (blkcnt == 1)
133 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
134 else
135 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
136
137 if (mmc->high_capacity)
138 cmd.cmdarg = start;
139 else
140 cmd.cmdarg = start * mmc->write_bl_len;
141
142 cmd.resp_type = MMC_RSP_R1;
143
144 data.src = src;
145 data.blocks = blkcnt;
146 data.blocksize = mmc->write_bl_len;
147 data.flags = MMC_DATA_WRITE;
148
149 if (mmc_send_cmd(mmc, &cmd, &data)) {
150 printf("mmc write failed\n");
151 return 0;
152 }
153
154 /* SPI multiblock writes terminate using a special
155 * token, not a STOP_TRANSMISSION request.
156 */
157 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
158 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
159 cmd.cmdarg = 0;
160 cmd.resp_type = MMC_RSP_R1b;
161 if (mmc_send_cmd(mmc, &cmd, NULL)) {
162 printf("mmc fail to send stop cmd\n");
163 return 0;
164 }
165 }
166
167 /* Waiting for the ready status */
168 if (mmc_send_status(mmc, timeout))
169 return 0;
170
171 return blkcnt;
172}
173
Stephen Warren7c4213f2015-12-07 11:38:48 -0700174ulong mmc_bwrite(block_dev_desc_t *block_dev, lbaint_t start, lbaint_t blkcnt,
175 const void *src)
Paul Burtonda61fa52013-09-09 15:30:26 +0100176{
Stephen Warren7c4213f2015-12-07 11:38:48 -0700177 int dev_num = block_dev->dev;
Paul Burtonda61fa52013-09-09 15:30:26 +0100178 lbaint_t cur, blocks_todo = blkcnt;
Stephen Warren873cc1d2015-12-07 11:38:49 -0700179 int err;
Paul Burtonda61fa52013-09-09 15:30:26 +0100180
181 struct mmc *mmc = find_mmc_device(dev_num);
182 if (!mmc)
183 return 0;
184
Stephen Warren873cc1d2015-12-07 11:38:49 -0700185 err = mmc_select_hwpart(dev_num, block_dev->hwpart);
186 if (err < 0)
187 return 0;
188
Paul Burtonda61fa52013-09-09 15:30:26 +0100189 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
190 return 0;
191
192 do {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200193 cur = (blocks_todo > mmc->cfg->b_max) ?
194 mmc->cfg->b_max : blocks_todo;
Paul Burtonda61fa52013-09-09 15:30:26 +0100195 if (mmc_write_blocks(mmc, start, cur, src) != cur)
196 return 0;
197 blocks_todo -= cur;
198 start += cur;
199 src += cur * mmc->write_bl_len;
200 } while (blocks_todo > 0);
201
202 return blkcnt;
203}