blob: 6733314942ea4fd4ad67d2b5643878dece2ba343 [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>
12#include <part.h>
Tom Rini58e31942015-06-11 20:53:31 -040013#include <div64.h>
14#include <linux/math64.h>
Paul Burton8d30cc92013-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 Nelson957e0662015-12-07 07:50:01 -070054 cmd.cmdarg = MMC_ERASE_ARG;
Paul Burton8d30cc92013-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 Warrene73f2962015-12-07 11:38:48 -070068unsigned long mmc_berase(block_dev_desc_t *block_dev, lbaint_t start,
69 lbaint_t blkcnt)
Paul Burton8d30cc92013-09-09 15:30:26 +010070{
Stephen Warrene73f2962015-12-07 11:38:48 -070071 int dev_num = block_dev->dev;
Paul Burton8d30cc92013-09-09 15:30:26 +010072 int err = 0;
Tom Rini58e31942015-06-11 20:53:31 -040073 u32 start_rem, blkcnt_rem;
Paul Burton8d30cc92013-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
Tom Rini58e31942015-06-11 20:53:31 -040081 /*
82 * We want to see if the requested start or total block count are
83 * unaligned. We discard the whole numbers and only care about the
84 * remainder.
85 */
86 err = div_u64_rem(start, mmc->erase_grp_size, &start_rem);
87 err = div_u64_rem(blkcnt, mmc->erase_grp_size, &blkcnt_rem);
88 if (start_rem || blkcnt_rem)
Paul Burton8d30cc92013-09-09 15:30:26 +010089 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
90 "The erase range would be change to "
91 "0x" LBAF "~0x" LBAF "\n\n",
92 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
93 ((start + blkcnt + mmc->erase_grp_size)
94 & ~(mmc->erase_grp_size - 1)) - 1);
95
96 while (blk < blkcnt) {
97 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
98 mmc->erase_grp_size : (blkcnt - blk);
99 err = mmc_erase_t(mmc, start + blk, blk_r);
100 if (err)
101 break;
102
103 blk += blk_r;
104
105 /* Waiting for the ready status */
106 if (mmc_send_status(mmc, timeout))
107 return 0;
108 }
109
110 return blk;
111}
112
113static ulong mmc_write_blocks(struct mmc *mmc, lbaint_t start,
114 lbaint_t blkcnt, const void *src)
115{
116 struct mmc_cmd cmd;
117 struct mmc_data data;
118 int timeout = 1000;
119
120 if ((start + blkcnt) > mmc->block_dev.lba) {
121 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
122 start + blkcnt, mmc->block_dev.lba);
123 return 0;
124 }
125
126 if (blkcnt == 0)
127 return 0;
128 else if (blkcnt == 1)
129 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
130 else
131 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
132
133 if (mmc->high_capacity)
134 cmd.cmdarg = start;
135 else
136 cmd.cmdarg = start * mmc->write_bl_len;
137
138 cmd.resp_type = MMC_RSP_R1;
139
140 data.src = src;
141 data.blocks = blkcnt;
142 data.blocksize = mmc->write_bl_len;
143 data.flags = MMC_DATA_WRITE;
144
145 if (mmc_send_cmd(mmc, &cmd, &data)) {
146 printf("mmc write failed\n");
147 return 0;
148 }
149
150 /* SPI multiblock writes terminate using a special
151 * token, not a STOP_TRANSMISSION request.
152 */
153 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
154 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
155 cmd.cmdarg = 0;
156 cmd.resp_type = MMC_RSP_R1b;
157 if (mmc_send_cmd(mmc, &cmd, NULL)) {
158 printf("mmc fail to send stop cmd\n");
159 return 0;
160 }
161 }
162
163 /* Waiting for the ready status */
164 if (mmc_send_status(mmc, timeout))
165 return 0;
166
167 return blkcnt;
168}
169
Stephen Warrene73f2962015-12-07 11:38:48 -0700170ulong mmc_bwrite(block_dev_desc_t *block_dev, lbaint_t start, lbaint_t blkcnt,
171 const void *src)
Paul Burton8d30cc92013-09-09 15:30:26 +0100172{
Stephen Warrene73f2962015-12-07 11:38:48 -0700173 int dev_num = block_dev->dev;
Paul Burton8d30cc92013-09-09 15:30:26 +0100174 lbaint_t cur, blocks_todo = blkcnt;
175
176 struct mmc *mmc = find_mmc_device(dev_num);
177 if (!mmc)
178 return 0;
179
180 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
181 return 0;
182
183 do {
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200184 cur = (blocks_todo > mmc->cfg->b_max) ?
185 mmc->cfg->b_max : blocks_todo;
Paul Burton8d30cc92013-09-09 15:30:26 +0100186 if (mmc_write_blocks(mmc, start, cur, src) != cur)
187 return 0;
188 blocks_todo -= cur;
189 start += cur;
190 src += cur * mmc->write_bl_len;
191 } while (blocks_todo > 0);
192
193 return blkcnt;
194}