arm: vf610: fix double iomux configuration for vf610twr board
[u-boot/qq2440-u-boot.git] / drivers / mmc / mmc_write.c
blobaa2fdefa75edeb39540954ae40cf73a841f5870a
1 /*
2 * Copyright 2008, Freescale Semiconductor, Inc
3 * Andy Fleming
5 * Based vaguely on the Linux code
7 * SPDX-License-Identifier: GPL-2.0+
8 */
10 #include <config.h>
11 #include <common.h>
12 #include <part.h>
13 #include "mmc_private.h"
15 static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
17 struct mmc_cmd cmd;
18 ulong end;
19 int err, start_cmd, end_cmd;
21 if (mmc->high_capacity) {
22 end = start + blkcnt - 1;
23 } else {
24 end = (start + blkcnt - 1) * mmc->write_bl_len;
25 start *= mmc->write_bl_len;
28 if (IS_SD(mmc)) {
29 start_cmd = SD_CMD_ERASE_WR_BLK_START;
30 end_cmd = SD_CMD_ERASE_WR_BLK_END;
31 } else {
32 start_cmd = MMC_CMD_ERASE_GROUP_START;
33 end_cmd = MMC_CMD_ERASE_GROUP_END;
36 cmd.cmdidx = start_cmd;
37 cmd.cmdarg = start;
38 cmd.resp_type = MMC_RSP_R1;
40 err = mmc_send_cmd(mmc, &cmd, NULL);
41 if (err)
42 goto err_out;
44 cmd.cmdidx = end_cmd;
45 cmd.cmdarg = end;
47 err = mmc_send_cmd(mmc, &cmd, NULL);
48 if (err)
49 goto err_out;
51 cmd.cmdidx = MMC_CMD_ERASE;
52 cmd.cmdarg = SECURE_ERASE;
53 cmd.resp_type = MMC_RSP_R1b;
55 err = mmc_send_cmd(mmc, &cmd, NULL);
56 if (err)
57 goto err_out;
59 return 0;
61 err_out:
62 puts("mmc erase failed\n");
63 return err;
66 unsigned long mmc_berase(int dev_num, lbaint_t start, lbaint_t blkcnt)
68 int err = 0;
69 struct mmc *mmc = find_mmc_device(dev_num);
70 lbaint_t blk = 0, blk_r = 0;
71 int timeout = 1000;
73 if (!mmc)
74 return -1;
76 if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size))
77 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
78 "The erase range would be change to "
79 "0x" LBAF "~0x" LBAF "\n\n",
80 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
81 ((start + blkcnt + mmc->erase_grp_size)
82 & ~(mmc->erase_grp_size - 1)) - 1);
84 while (blk < blkcnt) {
85 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
86 mmc->erase_grp_size : (blkcnt - blk);
87 err = mmc_erase_t(mmc, start + blk, blk_r);
88 if (err)
89 break;
91 blk += blk_r;
93 /* Waiting for the ready status */
94 if (mmc_send_status(mmc, timeout))
95 return 0;
98 return blk;
101 static ulong mmc_write_blocks(struct mmc *mmc, lbaint_t start,
102 lbaint_t blkcnt, const void *src)
104 struct mmc_cmd cmd;
105 struct mmc_data data;
106 int timeout = 1000;
108 if ((start + blkcnt) > mmc->block_dev.lba) {
109 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
110 start + blkcnt, mmc->block_dev.lba);
111 return 0;
114 if (blkcnt == 0)
115 return 0;
116 else if (blkcnt == 1)
117 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
118 else
119 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
121 if (mmc->high_capacity)
122 cmd.cmdarg = start;
123 else
124 cmd.cmdarg = start * mmc->write_bl_len;
126 cmd.resp_type = MMC_RSP_R1;
128 data.src = src;
129 data.blocks = blkcnt;
130 data.blocksize = mmc->write_bl_len;
131 data.flags = MMC_DATA_WRITE;
133 if (mmc_send_cmd(mmc, &cmd, &data)) {
134 printf("mmc write failed\n");
135 return 0;
138 /* SPI multiblock writes terminate using a special
139 * token, not a STOP_TRANSMISSION request.
141 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
142 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
143 cmd.cmdarg = 0;
144 cmd.resp_type = MMC_RSP_R1b;
145 if (mmc_send_cmd(mmc, &cmd, NULL)) {
146 printf("mmc fail to send stop cmd\n");
147 return 0;
151 /* Waiting for the ready status */
152 if (mmc_send_status(mmc, timeout))
153 return 0;
155 return blkcnt;
158 ulong mmc_bwrite(int dev_num, lbaint_t start, lbaint_t blkcnt, const void *src)
160 lbaint_t cur, blocks_todo = blkcnt;
162 struct mmc *mmc = find_mmc_device(dev_num);
163 if (!mmc)
164 return 0;
166 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
167 return 0;
169 do {
170 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
171 if (mmc_write_blocks(mmc, start, cur, src) != cur)
172 return 0;
173 blocks_todo -= cur;
174 start += cur;
175 src += cur * mmc->write_bl_len;
176 } while (blocks_todo > 0);
178 return blkcnt;