dm: mmc: Move the device list into a separate file
[platform/kernel/u-boot.git] / drivers / mmc / mmc_write.c
1 /*
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>
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 unsigned long mmc_berase(struct blk_desc *block_dev, lbaint_t start,
69                          lbaint_t blkcnt)
70 {
71         int dev_num = block_dev->devnum;
72         int err = 0;
73         u32 start_rem, blkcnt_rem;
74         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
81         err = blk_select_hwpart_devnum(IF_TYPE_MMC, dev_num,
82                                        block_dev->hwpart);
83         if (err < 0)
84                 return -1;
85
86         /*
87          * We want to see if the requested start or total block count are
88          * unaligned.  We discard the whole numbers and only care about the
89          * remainder.
90          */
91         err = div_u64_rem(start, mmc->erase_grp_size, &start_rem);
92         err = div_u64_rem(blkcnt, mmc->erase_grp_size, &blkcnt_rem);
93         if (start_rem || blkcnt_rem)
94                 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
95                        "The erase range would be change to "
96                        "0x" LBAF "~0x" LBAF "\n\n",
97                        mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
98                        ((start + blkcnt + mmc->erase_grp_size)
99                        & ~(mmc->erase_grp_size - 1)) - 1);
100
101         while (blk < blkcnt) {
102                 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
103                         mmc->erase_grp_size : (blkcnt - blk);
104                 err = mmc_erase_t(mmc, start + blk, blk_r);
105                 if (err)
106                         break;
107
108                 blk += blk_r;
109
110                 /* Waiting for the ready status */
111                 if (mmc_send_status(mmc, timeout))
112                         return 0;
113         }
114
115         return blk;
116 }
117
118 static ulong mmc_write_blocks(struct mmc *mmc, lbaint_t start,
119                 lbaint_t blkcnt, const void *src)
120 {
121         struct mmc_cmd cmd;
122         struct mmc_data data;
123         int timeout = 1000;
124
125         if ((start + blkcnt) > mmc_get_blk_desc(mmc)->lba) {
126                 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
127                        start + blkcnt, mmc_get_blk_desc(mmc)->lba);
128                 return 0;
129         }
130
131         if (blkcnt == 0)
132                 return 0;
133         else if (blkcnt == 1)
134                 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
135         else
136                 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
137
138         if (mmc->high_capacity)
139                 cmd.cmdarg = start;
140         else
141                 cmd.cmdarg = start * mmc->write_bl_len;
142
143         cmd.resp_type = MMC_RSP_R1;
144
145         data.src = src;
146         data.blocks = blkcnt;
147         data.blocksize = mmc->write_bl_len;
148         data.flags = MMC_DATA_WRITE;
149
150         if (mmc_send_cmd(mmc, &cmd, &data)) {
151                 printf("mmc write failed\n");
152                 return 0;
153         }
154
155         /* SPI multiblock writes terminate using a special
156          * token, not a STOP_TRANSMISSION request.
157          */
158         if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
159                 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
160                 cmd.cmdarg = 0;
161                 cmd.resp_type = MMC_RSP_R1b;
162                 if (mmc_send_cmd(mmc, &cmd, NULL)) {
163                         printf("mmc fail to send stop cmd\n");
164                         return 0;
165                 }
166         }
167
168         /* Waiting for the ready status */
169         if (mmc_send_status(mmc, timeout))
170                 return 0;
171
172         return blkcnt;
173 }
174
175 ulong mmc_bwrite(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
176                  const void *src)
177 {
178         int dev_num = block_dev->devnum;
179         lbaint_t cur, blocks_todo = blkcnt;
180         int err;
181
182         struct mmc *mmc = find_mmc_device(dev_num);
183         if (!mmc)
184                 return 0;
185
186         err = blk_select_hwpart_devnum(IF_TYPE_MMC, dev_num, block_dev->hwpart);
187         if (err < 0)
188                 return 0;
189
190         if (mmc_set_blocklen(mmc, mmc->write_bl_len))
191                 return 0;
192
193         do {
194                 cur = (blocks_todo > mmc->cfg->b_max) ?
195                         mmc->cfg->b_max : blocks_todo;
196                 if (mmc_write_blocks(mmc, start, cur, src) != cur)
197                         return 0;
198                 blocks_todo -= cur;
199                 start += cur;
200                 src += cur * mmc->write_bl_len;
201         } while (blocks_todo > 0);
202
203         return blkcnt;
204 }