xref: /openbmc/u-boot/drivers/mmc/mmc_write.c (revision de8106c1)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2008, Freescale Semiconductor, Inc
4  * Andy Fleming
5  *
6  * Based vaguely on the Linux code
7  */
8 
9 #include <config.h>
10 #include <common.h>
11 #include <dm.h>
12 #include <part.h>
13 #include <div64.h>
14 #include <linux/math64.h>
15 #include "mmc_private.h"
16 
17 static 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;
54 	cmd.cmdarg = MMC_ERASE_ARG;
55 	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 
63 err_out:
64 	puts("mmc erase failed\n");
65 	return err;
66 }
67 
68 #ifdef CONFIG_BLK
69 ulong mmc_berase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt)
70 #else
71 ulong mmc_berase(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt)
72 #endif
73 {
74 #ifdef CONFIG_BLK
75 	struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
76 #endif
77 	int dev_num = block_dev->devnum;
78 	int err = 0;
79 	u32 start_rem, blkcnt_rem;
80 	struct mmc *mmc = find_mmc_device(dev_num);
81 	lbaint_t blk = 0, blk_r = 0;
82 	int timeout = 1000;
83 
84 	if (!mmc)
85 		return -1;
86 
87 	err = blk_select_hwpart_devnum(IF_TYPE_MMC, dev_num,
88 				       block_dev->hwpart);
89 	if (err < 0)
90 		return -1;
91 
92 	/*
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)
100 		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) {
108 		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 		}
115 		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 */
122 		if (mmc_send_status(mmc, timeout))
123 			return 0;
124 	}
125 
126 	return blk;
127 }
128 
129 static 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;
134 	int timeout = 1000;
135 
136 	if ((start + blkcnt) > mmc_get_blk_desc(mmc)->lba) {
137 		printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
138 		       start + blkcnt, mmc_get_blk_desc(mmc)->lba);
139 		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 */
180 	if (mmc_send_status(mmc, timeout))
181 		return 0;
182 
183 	return blkcnt;
184 }
185 
186 #ifdef CONFIG_BLK
187 ulong mmc_bwrite(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
188 		 const void *src)
189 #else
190 ulong mmc_bwrite(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
191 		 const void *src)
192 #endif
193 {
194 #ifdef CONFIG_BLK
195 	struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
196 #endif
197 	int dev_num = block_dev->devnum;
198 	lbaint_t cur, blocks_todo = blkcnt;
199 	int err;
200 
201 	struct mmc *mmc = find_mmc_device(dev_num);
202 	if (!mmc)
203 		return 0;
204 
205 	err = blk_select_hwpart_devnum(IF_TYPE_MMC, dev_num, block_dev->hwpart);
206 	if (err < 0)
207 		return 0;
208 
209 	if (mmc_set_blocklen(mmc, mmc->write_bl_len))
210 		return 0;
211 
212 	do {
213 		cur = (blocks_todo > mmc->cfg->b_max) ?
214 			mmc->cfg->b_max : blocks_todo;
215 		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 }
224