1
2
3
4
5
6
7
8
9
10#include <config.h>
11#include <common.h>
12#include <dm.h>
13#include <part.h>
14#include <div64.h>
15#include <linux/math64.h>
16#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;
55 cmd.cmdarg = MMC_ERASE_ARG;
56 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
69unsigned long mmc_berase(struct blk_desc *block_dev, lbaint_t start,
70 lbaint_t blkcnt)
71{
72 int dev_num = block_dev->devnum;
73 int err = 0;
74 u32 start_rem, blkcnt_rem;
75 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
82 err = blk_select_hwpart_devnum(IF_TYPE_MMC, dev_num,
83 block_dev->hwpart);
84 if (err < 0)
85 return -1;
86
87
88
89
90
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)
95 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) {
103 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
104 mmc->erase_grp_size : (blkcnt - blk);
105 err = mmc_erase_t(mmc, start + blk, blk_r);
106 if (err)
107 break;
108
109 blk += blk_r;
110
111
112 if (mmc_send_status(mmc, timeout))
113 return 0;
114 }
115
116 return blk;
117}
118
119static ulong mmc_write_blocks(struct mmc *mmc, lbaint_t start,
120 lbaint_t blkcnt, const void *src)
121{
122 struct mmc_cmd cmd;
123 struct mmc_data data;
124 int timeout = 1000;
125
126 if ((start + blkcnt) > mmc_get_blk_desc(mmc)->lba) {
127 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
128 start + blkcnt, mmc_get_blk_desc(mmc)->lba);
129 return 0;
130 }
131
132 if (blkcnt == 0)
133 return 0;
134 else if (blkcnt == 1)
135 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
136 else
137 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
138
139 if (mmc->high_capacity)
140 cmd.cmdarg = start;
141 else
142 cmd.cmdarg = start * mmc->write_bl_len;
143
144 cmd.resp_type = MMC_RSP_R1;
145
146 data.src = src;
147 data.blocks = blkcnt;
148 data.blocksize = mmc->write_bl_len;
149 data.flags = MMC_DATA_WRITE;
150
151 if (mmc_send_cmd(mmc, &cmd, &data)) {
152 printf("mmc write failed\n");
153 return 0;
154 }
155
156
157
158
159 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
160 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
161 cmd.cmdarg = 0;
162 cmd.resp_type = MMC_RSP_R1b;
163 if (mmc_send_cmd(mmc, &cmd, NULL)) {
164 printf("mmc fail to send stop cmd\n");
165 return 0;
166 }
167 }
168
169
170 if (mmc_send_status(mmc, timeout))
171 return 0;
172
173 return blkcnt;
174}
175
176#ifdef CONFIG_BLK
177ulong mmc_bwrite(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
178 const void *src)
179#else
180ulong mmc_bwrite(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
181 const void *src)
182#endif
183{
184#ifdef CONFIG_BLK
185 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
186#endif
187 int dev_num = block_dev->devnum;
188 lbaint_t cur, blocks_todo = blkcnt;
189 int err;
190
191 struct mmc *mmc = find_mmc_device(dev_num);
192 if (!mmc)
193 return 0;
194
195 err = blk_select_hwpart_devnum(IF_TYPE_MMC, dev_num, block_dev->hwpart);
196 if (err < 0)
197 return 0;
198
199 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
200 return 0;
201
202 do {
203 cur = (blocks_todo > mmc->cfg->b_max) ?
204 mmc->cfg->b_max : blocks_todo;
205 if (mmc_write_blocks(mmc, start, cur, src) != cur)
206 return 0;
207 blocks_todo -= cur;
208 start += cur;
209 src += cur * mmc->write_bl_len;
210 } while (blocks_todo > 0);
211
212 return blkcnt;
213}
214