Commit 8c4444ff5e1d8e9fd1a847a2fb096aa9aff66f85
1 parent
6258b04e9a
Exists in
master
and in
54 other branches
Fix memory leak in mmc_read()
There is be a path through mmc_read in drivers/mmc/mmc.c where malloc'd memory is not freed before exiting mmc_read: it occurs if mmc_set_blocklen() returns a non-zero value. Reported-by: Quentin Armitage <Quentin@Armitage.org.uk> Signed-off-by: Wolfgang Denk <wd@denx.de>
Showing 1 changed file with 1 additions and 1 deletions Inline Diff
drivers/mmc/mmc.c
1 | /* | 1 | /* |
2 | * Copyright 2008, Freescale Semiconductor, Inc | 2 | * Copyright 2008, Freescale Semiconductor, Inc |
3 | * Andy Fleming | 3 | * Andy Fleming |
4 | * | 4 | * |
5 | * Based vaguely on the Linux code | 5 | * Based vaguely on the Linux code |
6 | * | 6 | * |
7 | * See file CREDITS for list of people who contributed to this | 7 | * See file CREDITS for list of people who contributed to this |
8 | * project. | 8 | * project. |
9 | * | 9 | * |
10 | * This program is free software; you can redistribute it and/or | 10 | * This program is free software; you can redistribute it and/or |
11 | * modify it under the terms of the GNU General Public License as | 11 | * modify it under the terms of the GNU General Public License as |
12 | * published by the Free Software Foundation; either version 2 of | 12 | * published by the Free Software Foundation; either version 2 of |
13 | * the License, or (at your option) any later version. | 13 | * the License, or (at your option) any later version. |
14 | * | 14 | * |
15 | * This program is distributed in the hope that it will be useful, | 15 | * This program is distributed in the hope that it will be useful, |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | * GNU General Public License for more details. | 18 | * GNU General Public License for more details. |
19 | * | 19 | * |
20 | * You should have received a copy of the GNU General Public License | 20 | * You should have received a copy of the GNU General Public License |
21 | * along with this program; if not, write to the Free Software | 21 | * along with this program; if not, write to the Free Software |
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
23 | * MA 02111-1307 USA | 23 | * MA 02111-1307 USA |
24 | */ | 24 | */ |
25 | 25 | ||
26 | #include <config.h> | 26 | #include <config.h> |
27 | #include <common.h> | 27 | #include <common.h> |
28 | #include <command.h> | 28 | #include <command.h> |
29 | #include <mmc.h> | 29 | #include <mmc.h> |
30 | #include <part.h> | 30 | #include <part.h> |
31 | #include <malloc.h> | 31 | #include <malloc.h> |
32 | #include <linux/list.h> | 32 | #include <linux/list.h> |
33 | #include <mmc.h> | 33 | #include <mmc.h> |
34 | #include <div64.h> | 34 | #include <div64.h> |
35 | 35 | ||
36 | static struct list_head mmc_devices; | 36 | static struct list_head mmc_devices; |
37 | static int cur_dev_num = -1; | 37 | static int cur_dev_num = -1; |
38 | 38 | ||
39 | int __board_mmc_getcd(u8 *cd, struct mmc *mmc) { | 39 | int __board_mmc_getcd(u8 *cd, struct mmc *mmc) { |
40 | return -1; | 40 | return -1; |
41 | } | 41 | } |
42 | 42 | ||
43 | int board_mmc_getcd(u8 *cd, struct mmc *mmc)__attribute__((weak, | 43 | int board_mmc_getcd(u8 *cd, struct mmc *mmc)__attribute__((weak, |
44 | alias("__board_mmc_getcd"))); | 44 | alias("__board_mmc_getcd"))); |
45 | 45 | ||
46 | int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) | 46 | int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) |
47 | { | 47 | { |
48 | return mmc->send_cmd(mmc, cmd, data); | 48 | return mmc->send_cmd(mmc, cmd, data); |
49 | } | 49 | } |
50 | 50 | ||
51 | int mmc_set_blocklen(struct mmc *mmc, int len) | 51 | int mmc_set_blocklen(struct mmc *mmc, int len) |
52 | { | 52 | { |
53 | struct mmc_cmd cmd; | 53 | struct mmc_cmd cmd; |
54 | 54 | ||
55 | cmd.cmdidx = MMC_CMD_SET_BLOCKLEN; | 55 | cmd.cmdidx = MMC_CMD_SET_BLOCKLEN; |
56 | cmd.resp_type = MMC_RSP_R1; | 56 | cmd.resp_type = MMC_RSP_R1; |
57 | cmd.cmdarg = len; | 57 | cmd.cmdarg = len; |
58 | cmd.flags = 0; | 58 | cmd.flags = 0; |
59 | 59 | ||
60 | return mmc_send_cmd(mmc, &cmd, NULL); | 60 | return mmc_send_cmd(mmc, &cmd, NULL); |
61 | } | 61 | } |
62 | 62 | ||
63 | struct mmc *find_mmc_device(int dev_num) | 63 | struct mmc *find_mmc_device(int dev_num) |
64 | { | 64 | { |
65 | struct mmc *m; | 65 | struct mmc *m; |
66 | struct list_head *entry; | 66 | struct list_head *entry; |
67 | 67 | ||
68 | list_for_each(entry, &mmc_devices) { | 68 | list_for_each(entry, &mmc_devices) { |
69 | m = list_entry(entry, struct mmc, link); | 69 | m = list_entry(entry, struct mmc, link); |
70 | 70 | ||
71 | if (m->block_dev.dev == dev_num) | 71 | if (m->block_dev.dev == dev_num) |
72 | return m; | 72 | return m; |
73 | } | 73 | } |
74 | 74 | ||
75 | printf("MMC Device %d not found\n", dev_num); | 75 | printf("MMC Device %d not found\n", dev_num); |
76 | 76 | ||
77 | return NULL; | 77 | return NULL; |
78 | } | 78 | } |
79 | 79 | ||
80 | static ulong | 80 | static ulong |
81 | mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src) | 81 | mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src) |
82 | { | 82 | { |
83 | struct mmc_cmd cmd; | 83 | struct mmc_cmd cmd; |
84 | struct mmc_data data; | 84 | struct mmc_data data; |
85 | int err; | 85 | int err; |
86 | int stoperr = 0; | 86 | int stoperr = 0; |
87 | struct mmc *mmc = find_mmc_device(dev_num); | 87 | struct mmc *mmc = find_mmc_device(dev_num); |
88 | int blklen; | 88 | int blklen; |
89 | 89 | ||
90 | if (!mmc) | 90 | if (!mmc) |
91 | return -1; | 91 | return -1; |
92 | 92 | ||
93 | blklen = mmc->write_bl_len; | 93 | blklen = mmc->write_bl_len; |
94 | 94 | ||
95 | err = mmc_set_blocklen(mmc, mmc->write_bl_len); | 95 | err = mmc_set_blocklen(mmc, mmc->write_bl_len); |
96 | 96 | ||
97 | if (err) { | 97 | if (err) { |
98 | printf("set write bl len failed\n\r"); | 98 | printf("set write bl len failed\n\r"); |
99 | return err; | 99 | return err; |
100 | } | 100 | } |
101 | 101 | ||
102 | if (blkcnt > 1) | 102 | if (blkcnt > 1) |
103 | cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK; | 103 | cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK; |
104 | else | 104 | else |
105 | cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK; | 105 | cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK; |
106 | 106 | ||
107 | if (mmc->high_capacity) | 107 | if (mmc->high_capacity) |
108 | cmd.cmdarg = start; | 108 | cmd.cmdarg = start; |
109 | else | 109 | else |
110 | cmd.cmdarg = start * blklen; | 110 | cmd.cmdarg = start * blklen; |
111 | 111 | ||
112 | cmd.resp_type = MMC_RSP_R1; | 112 | cmd.resp_type = MMC_RSP_R1; |
113 | cmd.flags = 0; | 113 | cmd.flags = 0; |
114 | 114 | ||
115 | data.src = src; | 115 | data.src = src; |
116 | data.blocks = blkcnt; | 116 | data.blocks = blkcnt; |
117 | data.blocksize = blklen; | 117 | data.blocksize = blklen; |
118 | data.flags = MMC_DATA_WRITE; | 118 | data.flags = MMC_DATA_WRITE; |
119 | 119 | ||
120 | err = mmc_send_cmd(mmc, &cmd, &data); | 120 | err = mmc_send_cmd(mmc, &cmd, &data); |
121 | 121 | ||
122 | if (err) { | 122 | if (err) { |
123 | printf("mmc write failed\n\r"); | 123 | printf("mmc write failed\n\r"); |
124 | return err; | 124 | return err; |
125 | } | 125 | } |
126 | 126 | ||
127 | if (blkcnt > 1) { | 127 | if (blkcnt > 1) { |
128 | cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; | 128 | cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; |
129 | cmd.cmdarg = 0; | 129 | cmd.cmdarg = 0; |
130 | cmd.resp_type = MMC_RSP_R1b; | 130 | cmd.resp_type = MMC_RSP_R1b; |
131 | cmd.flags = 0; | 131 | cmd.flags = 0; |
132 | stoperr = mmc_send_cmd(mmc, &cmd, NULL); | 132 | stoperr = mmc_send_cmd(mmc, &cmd, NULL); |
133 | } | 133 | } |
134 | 134 | ||
135 | return blkcnt; | 135 | return blkcnt; |
136 | } | 136 | } |
137 | 137 | ||
138 | int mmc_read_block(struct mmc *mmc, void *dst, uint blocknum) | 138 | int mmc_read_block(struct mmc *mmc, void *dst, uint blocknum) |
139 | { | 139 | { |
140 | struct mmc_cmd cmd; | 140 | struct mmc_cmd cmd; |
141 | struct mmc_data data; | 141 | struct mmc_data data; |
142 | 142 | ||
143 | cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK; | 143 | cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK; |
144 | 144 | ||
145 | if (mmc->high_capacity) | 145 | if (mmc->high_capacity) |
146 | cmd.cmdarg = blocknum; | 146 | cmd.cmdarg = blocknum; |
147 | else | 147 | else |
148 | cmd.cmdarg = blocknum * mmc->read_bl_len; | 148 | cmd.cmdarg = blocknum * mmc->read_bl_len; |
149 | 149 | ||
150 | cmd.resp_type = MMC_RSP_R1; | 150 | cmd.resp_type = MMC_RSP_R1; |
151 | cmd.flags = 0; | 151 | cmd.flags = 0; |
152 | 152 | ||
153 | data.dest = dst; | 153 | data.dest = dst; |
154 | data.blocks = 1; | 154 | data.blocks = 1; |
155 | data.blocksize = mmc->read_bl_len; | 155 | data.blocksize = mmc->read_bl_len; |
156 | data.flags = MMC_DATA_READ; | 156 | data.flags = MMC_DATA_READ; |
157 | 157 | ||
158 | return mmc_send_cmd(mmc, &cmd, &data); | 158 | return mmc_send_cmd(mmc, &cmd, &data); |
159 | } | 159 | } |
160 | 160 | ||
161 | int mmc_read(struct mmc *mmc, u64 src, uchar *dst, int size) | 161 | int mmc_read(struct mmc *mmc, u64 src, uchar *dst, int size) |
162 | { | 162 | { |
163 | char *buffer; | 163 | char *buffer; |
164 | int i; | 164 | int i; |
165 | int blklen = mmc->read_bl_len; | 165 | int blklen = mmc->read_bl_len; |
166 | int startblock = lldiv(src, mmc->read_bl_len); | 166 | int startblock = lldiv(src, mmc->read_bl_len); |
167 | int endblock = lldiv(src + size - 1, mmc->read_bl_len); | 167 | int endblock = lldiv(src + size - 1, mmc->read_bl_len); |
168 | int err = 0; | 168 | int err = 0; |
169 | 169 | ||
170 | /* Make a buffer big enough to hold all the blocks we might read */ | 170 | /* Make a buffer big enough to hold all the blocks we might read */ |
171 | buffer = malloc(blklen); | 171 | buffer = malloc(blklen); |
172 | 172 | ||
173 | if (!buffer) { | 173 | if (!buffer) { |
174 | printf("Could not allocate buffer for MMC read!\n"); | 174 | printf("Could not allocate buffer for MMC read!\n"); |
175 | return -1; | 175 | return -1; |
176 | } | 176 | } |
177 | 177 | ||
178 | /* We always do full block reads from the card */ | 178 | /* We always do full block reads from the card */ |
179 | err = mmc_set_blocklen(mmc, mmc->read_bl_len); | 179 | err = mmc_set_blocklen(mmc, mmc->read_bl_len); |
180 | 180 | ||
181 | if (err) | 181 | if (err) |
182 | return err; | 182 | goto free_buffer; |
183 | 183 | ||
184 | for (i = startblock; i <= endblock; i++) { | 184 | for (i = startblock; i <= endblock; i++) { |
185 | int segment_size; | 185 | int segment_size; |
186 | int offset; | 186 | int offset; |
187 | 187 | ||
188 | err = mmc_read_block(mmc, buffer, i); | 188 | err = mmc_read_block(mmc, buffer, i); |
189 | 189 | ||
190 | if (err) | 190 | if (err) |
191 | goto free_buffer; | 191 | goto free_buffer; |
192 | 192 | ||
193 | /* | 193 | /* |
194 | * The first block may not be aligned, so we | 194 | * The first block may not be aligned, so we |
195 | * copy from the desired point in the block | 195 | * copy from the desired point in the block |
196 | */ | 196 | */ |
197 | offset = (src & (blklen - 1)); | 197 | offset = (src & (blklen - 1)); |
198 | segment_size = MIN(blklen - offset, size); | 198 | segment_size = MIN(blklen - offset, size); |
199 | 199 | ||
200 | memcpy(dst, buffer + offset, segment_size); | 200 | memcpy(dst, buffer + offset, segment_size); |
201 | 201 | ||
202 | dst += segment_size; | 202 | dst += segment_size; |
203 | src += segment_size; | 203 | src += segment_size; |
204 | size -= segment_size; | 204 | size -= segment_size; |
205 | } | 205 | } |
206 | 206 | ||
207 | free_buffer: | 207 | free_buffer: |
208 | free(buffer); | 208 | free(buffer); |
209 | 209 | ||
210 | return err; | 210 | return err; |
211 | } | 211 | } |
212 | 212 | ||
213 | static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst) | 213 | static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst) |
214 | { | 214 | { |
215 | int err; | 215 | int err; |
216 | int i; | 216 | int i; |
217 | struct mmc *mmc = find_mmc_device(dev_num); | 217 | struct mmc *mmc = find_mmc_device(dev_num); |
218 | 218 | ||
219 | if (!mmc) | 219 | if (!mmc) |
220 | return 0; | 220 | return 0; |
221 | 221 | ||
222 | /* We always do full block reads from the card */ | 222 | /* We always do full block reads from the card */ |
223 | err = mmc_set_blocklen(mmc, mmc->read_bl_len); | 223 | err = mmc_set_blocklen(mmc, mmc->read_bl_len); |
224 | 224 | ||
225 | if (err) { | 225 | if (err) { |
226 | return 0; | 226 | return 0; |
227 | } | 227 | } |
228 | 228 | ||
229 | for (i = start; i < start + blkcnt; i++, dst += mmc->read_bl_len) { | 229 | for (i = start; i < start + blkcnt; i++, dst += mmc->read_bl_len) { |
230 | err = mmc_read_block(mmc, dst, i); | 230 | err = mmc_read_block(mmc, dst, i); |
231 | 231 | ||
232 | if (err) { | 232 | if (err) { |
233 | printf("block read failed: %d\n", err); | 233 | printf("block read failed: %d\n", err); |
234 | return i - start; | 234 | return i - start; |
235 | } | 235 | } |
236 | } | 236 | } |
237 | 237 | ||
238 | return blkcnt; | 238 | return blkcnt; |
239 | } | 239 | } |
240 | 240 | ||
241 | int mmc_go_idle(struct mmc* mmc) | 241 | int mmc_go_idle(struct mmc* mmc) |
242 | { | 242 | { |
243 | struct mmc_cmd cmd; | 243 | struct mmc_cmd cmd; |
244 | int err; | 244 | int err; |
245 | 245 | ||
246 | udelay(1000); | 246 | udelay(1000); |
247 | 247 | ||
248 | cmd.cmdidx = MMC_CMD_GO_IDLE_STATE; | 248 | cmd.cmdidx = MMC_CMD_GO_IDLE_STATE; |
249 | cmd.cmdarg = 0; | 249 | cmd.cmdarg = 0; |
250 | cmd.resp_type = MMC_RSP_NONE; | 250 | cmd.resp_type = MMC_RSP_NONE; |
251 | cmd.flags = 0; | 251 | cmd.flags = 0; |
252 | 252 | ||
253 | err = mmc_send_cmd(mmc, &cmd, NULL); | 253 | err = mmc_send_cmd(mmc, &cmd, NULL); |
254 | 254 | ||
255 | if (err) | 255 | if (err) |
256 | return err; | 256 | return err; |
257 | 257 | ||
258 | udelay(2000); | 258 | udelay(2000); |
259 | 259 | ||
260 | return 0; | 260 | return 0; |
261 | } | 261 | } |
262 | 262 | ||
263 | int | 263 | int |
264 | sd_send_op_cond(struct mmc *mmc) | 264 | sd_send_op_cond(struct mmc *mmc) |
265 | { | 265 | { |
266 | int timeout = 1000; | 266 | int timeout = 1000; |
267 | int err; | 267 | int err; |
268 | struct mmc_cmd cmd; | 268 | struct mmc_cmd cmd; |
269 | 269 | ||
270 | do { | 270 | do { |
271 | cmd.cmdidx = MMC_CMD_APP_CMD; | 271 | cmd.cmdidx = MMC_CMD_APP_CMD; |
272 | cmd.resp_type = MMC_RSP_R1; | 272 | cmd.resp_type = MMC_RSP_R1; |
273 | cmd.cmdarg = 0; | 273 | cmd.cmdarg = 0; |
274 | cmd.flags = 0; | 274 | cmd.flags = 0; |
275 | 275 | ||
276 | err = mmc_send_cmd(mmc, &cmd, NULL); | 276 | err = mmc_send_cmd(mmc, &cmd, NULL); |
277 | 277 | ||
278 | if (err) | 278 | if (err) |
279 | return err; | 279 | return err; |
280 | 280 | ||
281 | cmd.cmdidx = SD_CMD_APP_SEND_OP_COND; | 281 | cmd.cmdidx = SD_CMD_APP_SEND_OP_COND; |
282 | cmd.resp_type = MMC_RSP_R3; | 282 | cmd.resp_type = MMC_RSP_R3; |
283 | 283 | ||
284 | /* | 284 | /* |
285 | * Most cards do not answer if some reserved bits | 285 | * Most cards do not answer if some reserved bits |
286 | * in the ocr are set. However, Some controller | 286 | * in the ocr are set. However, Some controller |
287 | * can set bit 7 (reserved for low voltages), but | 287 | * can set bit 7 (reserved for low voltages), but |
288 | * how to manage low voltages SD card is not yet | 288 | * how to manage low voltages SD card is not yet |
289 | * specified. | 289 | * specified. |
290 | */ | 290 | */ |
291 | cmd.cmdarg = mmc->voltages & 0xff8000; | 291 | cmd.cmdarg = mmc->voltages & 0xff8000; |
292 | 292 | ||
293 | if (mmc->version == SD_VERSION_2) | 293 | if (mmc->version == SD_VERSION_2) |
294 | cmd.cmdarg |= OCR_HCS; | 294 | cmd.cmdarg |= OCR_HCS; |
295 | 295 | ||
296 | err = mmc_send_cmd(mmc, &cmd, NULL); | 296 | err = mmc_send_cmd(mmc, &cmd, NULL); |
297 | 297 | ||
298 | if (err) | 298 | if (err) |
299 | return err; | 299 | return err; |
300 | 300 | ||
301 | udelay(1000); | 301 | udelay(1000); |
302 | } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--); | 302 | } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--); |
303 | 303 | ||
304 | if (timeout <= 0) | 304 | if (timeout <= 0) |
305 | return UNUSABLE_ERR; | 305 | return UNUSABLE_ERR; |
306 | 306 | ||
307 | if (mmc->version != SD_VERSION_2) | 307 | if (mmc->version != SD_VERSION_2) |
308 | mmc->version = SD_VERSION_1_0; | 308 | mmc->version = SD_VERSION_1_0; |
309 | 309 | ||
310 | mmc->ocr = cmd.response[0]; | 310 | mmc->ocr = cmd.response[0]; |
311 | 311 | ||
312 | mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); | 312 | mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); |
313 | mmc->rca = 0; | 313 | mmc->rca = 0; |
314 | 314 | ||
315 | return 0; | 315 | return 0; |
316 | } | 316 | } |
317 | 317 | ||
318 | int mmc_send_op_cond(struct mmc *mmc) | 318 | int mmc_send_op_cond(struct mmc *mmc) |
319 | { | 319 | { |
320 | int timeout = 1000; | 320 | int timeout = 1000; |
321 | struct mmc_cmd cmd; | 321 | struct mmc_cmd cmd; |
322 | int err; | 322 | int err; |
323 | 323 | ||
324 | /* Some cards seem to need this */ | 324 | /* Some cards seem to need this */ |
325 | mmc_go_idle(mmc); | 325 | mmc_go_idle(mmc); |
326 | 326 | ||
327 | do { | 327 | do { |
328 | cmd.cmdidx = MMC_CMD_SEND_OP_COND; | 328 | cmd.cmdidx = MMC_CMD_SEND_OP_COND; |
329 | cmd.resp_type = MMC_RSP_R3; | 329 | cmd.resp_type = MMC_RSP_R3; |
330 | cmd.cmdarg = OCR_HCS | mmc->voltages; | 330 | cmd.cmdarg = OCR_HCS | mmc->voltages; |
331 | cmd.flags = 0; | 331 | cmd.flags = 0; |
332 | 332 | ||
333 | err = mmc_send_cmd(mmc, &cmd, NULL); | 333 | err = mmc_send_cmd(mmc, &cmd, NULL); |
334 | 334 | ||
335 | if (err) | 335 | if (err) |
336 | return err; | 336 | return err; |
337 | 337 | ||
338 | udelay(1000); | 338 | udelay(1000); |
339 | } while (!(cmd.response[0] & OCR_BUSY) && timeout--); | 339 | } while (!(cmd.response[0] & OCR_BUSY) && timeout--); |
340 | 340 | ||
341 | if (timeout <= 0) | 341 | if (timeout <= 0) |
342 | return UNUSABLE_ERR; | 342 | return UNUSABLE_ERR; |
343 | 343 | ||
344 | mmc->version = MMC_VERSION_UNKNOWN; | 344 | mmc->version = MMC_VERSION_UNKNOWN; |
345 | mmc->ocr = cmd.response[0]; | 345 | mmc->ocr = cmd.response[0]; |
346 | 346 | ||
347 | mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); | 347 | mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); |
348 | mmc->rca = 0; | 348 | mmc->rca = 0; |
349 | 349 | ||
350 | return 0; | 350 | return 0; |
351 | } | 351 | } |
352 | 352 | ||
353 | 353 | ||
354 | int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd) | 354 | int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd) |
355 | { | 355 | { |
356 | struct mmc_cmd cmd; | 356 | struct mmc_cmd cmd; |
357 | struct mmc_data data; | 357 | struct mmc_data data; |
358 | int err; | 358 | int err; |
359 | 359 | ||
360 | /* Get the Card Status Register */ | 360 | /* Get the Card Status Register */ |
361 | cmd.cmdidx = MMC_CMD_SEND_EXT_CSD; | 361 | cmd.cmdidx = MMC_CMD_SEND_EXT_CSD; |
362 | cmd.resp_type = MMC_RSP_R1; | 362 | cmd.resp_type = MMC_RSP_R1; |
363 | cmd.cmdarg = 0; | 363 | cmd.cmdarg = 0; |
364 | cmd.flags = 0; | 364 | cmd.flags = 0; |
365 | 365 | ||
366 | data.dest = ext_csd; | 366 | data.dest = ext_csd; |
367 | data.blocks = 1; | 367 | data.blocks = 1; |
368 | data.blocksize = 512; | 368 | data.blocksize = 512; |
369 | data.flags = MMC_DATA_READ; | 369 | data.flags = MMC_DATA_READ; |
370 | 370 | ||
371 | err = mmc_send_cmd(mmc, &cmd, &data); | 371 | err = mmc_send_cmd(mmc, &cmd, &data); |
372 | 372 | ||
373 | return err; | 373 | return err; |
374 | } | 374 | } |
375 | 375 | ||
376 | 376 | ||
377 | int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value) | 377 | int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value) |
378 | { | 378 | { |
379 | struct mmc_cmd cmd; | 379 | struct mmc_cmd cmd; |
380 | 380 | ||
381 | cmd.cmdidx = MMC_CMD_SWITCH; | 381 | cmd.cmdidx = MMC_CMD_SWITCH; |
382 | cmd.resp_type = MMC_RSP_R1b; | 382 | cmd.resp_type = MMC_RSP_R1b; |
383 | cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | | 383 | cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | |
384 | (index << 16) | | 384 | (index << 16) | |
385 | (value << 8); | 385 | (value << 8); |
386 | cmd.flags = 0; | 386 | cmd.flags = 0; |
387 | 387 | ||
388 | return mmc_send_cmd(mmc, &cmd, NULL); | 388 | return mmc_send_cmd(mmc, &cmd, NULL); |
389 | } | 389 | } |
390 | 390 | ||
391 | int mmc_change_freq(struct mmc *mmc) | 391 | int mmc_change_freq(struct mmc *mmc) |
392 | { | 392 | { |
393 | char ext_csd[512]; | 393 | char ext_csd[512]; |
394 | char cardtype; | 394 | char cardtype; |
395 | int err; | 395 | int err; |
396 | 396 | ||
397 | mmc->card_caps = 0; | 397 | mmc->card_caps = 0; |
398 | 398 | ||
399 | /* Only version 4 supports high-speed */ | 399 | /* Only version 4 supports high-speed */ |
400 | if (mmc->version < MMC_VERSION_4) | 400 | if (mmc->version < MMC_VERSION_4) |
401 | return 0; | 401 | return 0; |
402 | 402 | ||
403 | mmc->card_caps |= MMC_MODE_4BIT; | 403 | mmc->card_caps |= MMC_MODE_4BIT; |
404 | 404 | ||
405 | err = mmc_send_ext_csd(mmc, ext_csd); | 405 | err = mmc_send_ext_csd(mmc, ext_csd); |
406 | 406 | ||
407 | if (err) | 407 | if (err) |
408 | return err; | 408 | return err; |
409 | 409 | ||
410 | if (ext_csd[212] || ext_csd[213] || ext_csd[214] || ext_csd[215]) | 410 | if (ext_csd[212] || ext_csd[213] || ext_csd[214] || ext_csd[215]) |
411 | mmc->high_capacity = 1; | 411 | mmc->high_capacity = 1; |
412 | 412 | ||
413 | cardtype = ext_csd[196] & 0xf; | 413 | cardtype = ext_csd[196] & 0xf; |
414 | 414 | ||
415 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1); | 415 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1); |
416 | 416 | ||
417 | if (err) | 417 | if (err) |
418 | return err; | 418 | return err; |
419 | 419 | ||
420 | /* Now check to see that it worked */ | 420 | /* Now check to see that it worked */ |
421 | err = mmc_send_ext_csd(mmc, ext_csd); | 421 | err = mmc_send_ext_csd(mmc, ext_csd); |
422 | 422 | ||
423 | if (err) | 423 | if (err) |
424 | return err; | 424 | return err; |
425 | 425 | ||
426 | /* No high-speed support */ | 426 | /* No high-speed support */ |
427 | if (!ext_csd[185]) | 427 | if (!ext_csd[185]) |
428 | return 0; | 428 | return 0; |
429 | 429 | ||
430 | /* High Speed is set, there are two types: 52MHz and 26MHz */ | 430 | /* High Speed is set, there are two types: 52MHz and 26MHz */ |
431 | if (cardtype & MMC_HS_52MHZ) | 431 | if (cardtype & MMC_HS_52MHZ) |
432 | mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; | 432 | mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; |
433 | else | 433 | else |
434 | mmc->card_caps |= MMC_MODE_HS; | 434 | mmc->card_caps |= MMC_MODE_HS; |
435 | 435 | ||
436 | return 0; | 436 | return 0; |
437 | } | 437 | } |
438 | 438 | ||
439 | int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp) | 439 | int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp) |
440 | { | 440 | { |
441 | struct mmc_cmd cmd; | 441 | struct mmc_cmd cmd; |
442 | struct mmc_data data; | 442 | struct mmc_data data; |
443 | 443 | ||
444 | /* Switch the frequency */ | 444 | /* Switch the frequency */ |
445 | cmd.cmdidx = SD_CMD_SWITCH_FUNC; | 445 | cmd.cmdidx = SD_CMD_SWITCH_FUNC; |
446 | cmd.resp_type = MMC_RSP_R1; | 446 | cmd.resp_type = MMC_RSP_R1; |
447 | cmd.cmdarg = (mode << 31) | 0xffffff; | 447 | cmd.cmdarg = (mode << 31) | 0xffffff; |
448 | cmd.cmdarg &= ~(0xf << (group * 4)); | 448 | cmd.cmdarg &= ~(0xf << (group * 4)); |
449 | cmd.cmdarg |= value << (group * 4); | 449 | cmd.cmdarg |= value << (group * 4); |
450 | cmd.flags = 0; | 450 | cmd.flags = 0; |
451 | 451 | ||
452 | data.dest = (char *)resp; | 452 | data.dest = (char *)resp; |
453 | data.blocksize = 64; | 453 | data.blocksize = 64; |
454 | data.blocks = 1; | 454 | data.blocks = 1; |
455 | data.flags = MMC_DATA_READ; | 455 | data.flags = MMC_DATA_READ; |
456 | 456 | ||
457 | return mmc_send_cmd(mmc, &cmd, &data); | 457 | return mmc_send_cmd(mmc, &cmd, &data); |
458 | } | 458 | } |
459 | 459 | ||
460 | 460 | ||
461 | int sd_change_freq(struct mmc *mmc) | 461 | int sd_change_freq(struct mmc *mmc) |
462 | { | 462 | { |
463 | int err; | 463 | int err; |
464 | struct mmc_cmd cmd; | 464 | struct mmc_cmd cmd; |
465 | uint scr[2]; | 465 | uint scr[2]; |
466 | uint switch_status[16]; | 466 | uint switch_status[16]; |
467 | struct mmc_data data; | 467 | struct mmc_data data; |
468 | int timeout; | 468 | int timeout; |
469 | 469 | ||
470 | mmc->card_caps = 0; | 470 | mmc->card_caps = 0; |
471 | 471 | ||
472 | /* Read the SCR to find out if this card supports higher speeds */ | 472 | /* Read the SCR to find out if this card supports higher speeds */ |
473 | cmd.cmdidx = MMC_CMD_APP_CMD; | 473 | cmd.cmdidx = MMC_CMD_APP_CMD; |
474 | cmd.resp_type = MMC_RSP_R1; | 474 | cmd.resp_type = MMC_RSP_R1; |
475 | cmd.cmdarg = mmc->rca << 16; | 475 | cmd.cmdarg = mmc->rca << 16; |
476 | cmd.flags = 0; | 476 | cmd.flags = 0; |
477 | 477 | ||
478 | err = mmc_send_cmd(mmc, &cmd, NULL); | 478 | err = mmc_send_cmd(mmc, &cmd, NULL); |
479 | 479 | ||
480 | if (err) | 480 | if (err) |
481 | return err; | 481 | return err; |
482 | 482 | ||
483 | cmd.cmdidx = SD_CMD_APP_SEND_SCR; | 483 | cmd.cmdidx = SD_CMD_APP_SEND_SCR; |
484 | cmd.resp_type = MMC_RSP_R1; | 484 | cmd.resp_type = MMC_RSP_R1; |
485 | cmd.cmdarg = 0; | 485 | cmd.cmdarg = 0; |
486 | cmd.flags = 0; | 486 | cmd.flags = 0; |
487 | 487 | ||
488 | timeout = 3; | 488 | timeout = 3; |
489 | 489 | ||
490 | retry_scr: | 490 | retry_scr: |
491 | data.dest = (char *)&scr; | 491 | data.dest = (char *)&scr; |
492 | data.blocksize = 8; | 492 | data.blocksize = 8; |
493 | data.blocks = 1; | 493 | data.blocks = 1; |
494 | data.flags = MMC_DATA_READ; | 494 | data.flags = MMC_DATA_READ; |
495 | 495 | ||
496 | err = mmc_send_cmd(mmc, &cmd, &data); | 496 | err = mmc_send_cmd(mmc, &cmd, &data); |
497 | 497 | ||
498 | if (err) { | 498 | if (err) { |
499 | if (timeout--) | 499 | if (timeout--) |
500 | goto retry_scr; | 500 | goto retry_scr; |
501 | 501 | ||
502 | return err; | 502 | return err; |
503 | } | 503 | } |
504 | 504 | ||
505 | mmc->scr[0] = __be32_to_cpu(scr[0]); | 505 | mmc->scr[0] = __be32_to_cpu(scr[0]); |
506 | mmc->scr[1] = __be32_to_cpu(scr[1]); | 506 | mmc->scr[1] = __be32_to_cpu(scr[1]); |
507 | 507 | ||
508 | switch ((mmc->scr[0] >> 24) & 0xf) { | 508 | switch ((mmc->scr[0] >> 24) & 0xf) { |
509 | case 0: | 509 | case 0: |
510 | mmc->version = SD_VERSION_1_0; | 510 | mmc->version = SD_VERSION_1_0; |
511 | break; | 511 | break; |
512 | case 1: | 512 | case 1: |
513 | mmc->version = SD_VERSION_1_10; | 513 | mmc->version = SD_VERSION_1_10; |
514 | break; | 514 | break; |
515 | case 2: | 515 | case 2: |
516 | mmc->version = SD_VERSION_2; | 516 | mmc->version = SD_VERSION_2; |
517 | break; | 517 | break; |
518 | default: | 518 | default: |
519 | mmc->version = SD_VERSION_1_0; | 519 | mmc->version = SD_VERSION_1_0; |
520 | break; | 520 | break; |
521 | } | 521 | } |
522 | 522 | ||
523 | /* Version 1.0 doesn't support switching */ | 523 | /* Version 1.0 doesn't support switching */ |
524 | if (mmc->version == SD_VERSION_1_0) | 524 | if (mmc->version == SD_VERSION_1_0) |
525 | return 0; | 525 | return 0; |
526 | 526 | ||
527 | timeout = 4; | 527 | timeout = 4; |
528 | while (timeout--) { | 528 | while (timeout--) { |
529 | err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1, | 529 | err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1, |
530 | (u8 *)&switch_status); | 530 | (u8 *)&switch_status); |
531 | 531 | ||
532 | if (err) | 532 | if (err) |
533 | return err; | 533 | return err; |
534 | 534 | ||
535 | /* The high-speed function is busy. Try again */ | 535 | /* The high-speed function is busy. Try again */ |
536 | if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY)) | 536 | if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY)) |
537 | break; | 537 | break; |
538 | } | 538 | } |
539 | 539 | ||
540 | if (mmc->scr[0] & SD_DATA_4BIT) | 540 | if (mmc->scr[0] & SD_DATA_4BIT) |
541 | mmc->card_caps |= MMC_MODE_4BIT; | 541 | mmc->card_caps |= MMC_MODE_4BIT; |
542 | 542 | ||
543 | /* If high-speed isn't supported, we return */ | 543 | /* If high-speed isn't supported, we return */ |
544 | if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)) | 544 | if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)) |
545 | return 0; | 545 | return 0; |
546 | 546 | ||
547 | err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status); | 547 | err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status); |
548 | 548 | ||
549 | if (err) | 549 | if (err) |
550 | return err; | 550 | return err; |
551 | 551 | ||
552 | if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000) | 552 | if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000) |
553 | mmc->card_caps |= MMC_MODE_HS; | 553 | mmc->card_caps |= MMC_MODE_HS; |
554 | 554 | ||
555 | return 0; | 555 | return 0; |
556 | } | 556 | } |
557 | 557 | ||
558 | /* frequency bases */ | 558 | /* frequency bases */ |
559 | /* divided by 10 to be nice to platforms without floating point */ | 559 | /* divided by 10 to be nice to platforms without floating point */ |
560 | int fbase[] = { | 560 | int fbase[] = { |
561 | 10000, | 561 | 10000, |
562 | 100000, | 562 | 100000, |
563 | 1000000, | 563 | 1000000, |
564 | 10000000, | 564 | 10000000, |
565 | }; | 565 | }; |
566 | 566 | ||
567 | /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice | 567 | /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice |
568 | * to platforms without floating point. | 568 | * to platforms without floating point. |
569 | */ | 569 | */ |
570 | int multipliers[] = { | 570 | int multipliers[] = { |
571 | 0, /* reserved */ | 571 | 0, /* reserved */ |
572 | 10, | 572 | 10, |
573 | 12, | 573 | 12, |
574 | 13, | 574 | 13, |
575 | 15, | 575 | 15, |
576 | 20, | 576 | 20, |
577 | 25, | 577 | 25, |
578 | 30, | 578 | 30, |
579 | 35, | 579 | 35, |
580 | 40, | 580 | 40, |
581 | 45, | 581 | 45, |
582 | 50, | 582 | 50, |
583 | 55, | 583 | 55, |
584 | 60, | 584 | 60, |
585 | 70, | 585 | 70, |
586 | 80, | 586 | 80, |
587 | }; | 587 | }; |
588 | 588 | ||
589 | void mmc_set_ios(struct mmc *mmc) | 589 | void mmc_set_ios(struct mmc *mmc) |
590 | { | 590 | { |
591 | mmc->set_ios(mmc); | 591 | mmc->set_ios(mmc); |
592 | } | 592 | } |
593 | 593 | ||
594 | void mmc_set_clock(struct mmc *mmc, uint clock) | 594 | void mmc_set_clock(struct mmc *mmc, uint clock) |
595 | { | 595 | { |
596 | if (clock > mmc->f_max) | 596 | if (clock > mmc->f_max) |
597 | clock = mmc->f_max; | 597 | clock = mmc->f_max; |
598 | 598 | ||
599 | if (clock < mmc->f_min) | 599 | if (clock < mmc->f_min) |
600 | clock = mmc->f_min; | 600 | clock = mmc->f_min; |
601 | 601 | ||
602 | mmc->clock = clock; | 602 | mmc->clock = clock; |
603 | 603 | ||
604 | mmc_set_ios(mmc); | 604 | mmc_set_ios(mmc); |
605 | } | 605 | } |
606 | 606 | ||
607 | void mmc_set_bus_width(struct mmc *mmc, uint width) | 607 | void mmc_set_bus_width(struct mmc *mmc, uint width) |
608 | { | 608 | { |
609 | mmc->bus_width = width; | 609 | mmc->bus_width = width; |
610 | 610 | ||
611 | mmc_set_ios(mmc); | 611 | mmc_set_ios(mmc); |
612 | } | 612 | } |
613 | 613 | ||
614 | int mmc_startup(struct mmc *mmc) | 614 | int mmc_startup(struct mmc *mmc) |
615 | { | 615 | { |
616 | int err; | 616 | int err; |
617 | uint mult, freq; | 617 | uint mult, freq; |
618 | u64 cmult, csize; | 618 | u64 cmult, csize; |
619 | struct mmc_cmd cmd; | 619 | struct mmc_cmd cmd; |
620 | 620 | ||
621 | /* Put the Card in Identify Mode */ | 621 | /* Put the Card in Identify Mode */ |
622 | cmd.cmdidx = MMC_CMD_ALL_SEND_CID; | 622 | cmd.cmdidx = MMC_CMD_ALL_SEND_CID; |
623 | cmd.resp_type = MMC_RSP_R2; | 623 | cmd.resp_type = MMC_RSP_R2; |
624 | cmd.cmdarg = 0; | 624 | cmd.cmdarg = 0; |
625 | cmd.flags = 0; | 625 | cmd.flags = 0; |
626 | 626 | ||
627 | err = mmc_send_cmd(mmc, &cmd, NULL); | 627 | err = mmc_send_cmd(mmc, &cmd, NULL); |
628 | 628 | ||
629 | if (err) | 629 | if (err) |
630 | return err; | 630 | return err; |
631 | 631 | ||
632 | memcpy(mmc->cid, cmd.response, 16); | 632 | memcpy(mmc->cid, cmd.response, 16); |
633 | 633 | ||
634 | /* | 634 | /* |
635 | * For MMC cards, set the Relative Address. | 635 | * For MMC cards, set the Relative Address. |
636 | * For SD cards, get the Relatvie Address. | 636 | * For SD cards, get the Relatvie Address. |
637 | * This also puts the cards into Standby State | 637 | * This also puts the cards into Standby State |
638 | */ | 638 | */ |
639 | cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR; | 639 | cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR; |
640 | cmd.cmdarg = mmc->rca << 16; | 640 | cmd.cmdarg = mmc->rca << 16; |
641 | cmd.resp_type = MMC_RSP_R6; | 641 | cmd.resp_type = MMC_RSP_R6; |
642 | cmd.flags = 0; | 642 | cmd.flags = 0; |
643 | 643 | ||
644 | err = mmc_send_cmd(mmc, &cmd, NULL); | 644 | err = mmc_send_cmd(mmc, &cmd, NULL); |
645 | 645 | ||
646 | if (err) | 646 | if (err) |
647 | return err; | 647 | return err; |
648 | 648 | ||
649 | if (IS_SD(mmc)) | 649 | if (IS_SD(mmc)) |
650 | mmc->rca = (cmd.response[0] >> 16) & 0xffff; | 650 | mmc->rca = (cmd.response[0] >> 16) & 0xffff; |
651 | 651 | ||
652 | /* Get the Card-Specific Data */ | 652 | /* Get the Card-Specific Data */ |
653 | cmd.cmdidx = MMC_CMD_SEND_CSD; | 653 | cmd.cmdidx = MMC_CMD_SEND_CSD; |
654 | cmd.resp_type = MMC_RSP_R2; | 654 | cmd.resp_type = MMC_RSP_R2; |
655 | cmd.cmdarg = mmc->rca << 16; | 655 | cmd.cmdarg = mmc->rca << 16; |
656 | cmd.flags = 0; | 656 | cmd.flags = 0; |
657 | 657 | ||
658 | err = mmc_send_cmd(mmc, &cmd, NULL); | 658 | err = mmc_send_cmd(mmc, &cmd, NULL); |
659 | 659 | ||
660 | if (err) | 660 | if (err) |
661 | return err; | 661 | return err; |
662 | 662 | ||
663 | mmc->csd[0] = cmd.response[0]; | 663 | mmc->csd[0] = cmd.response[0]; |
664 | mmc->csd[1] = cmd.response[1]; | 664 | mmc->csd[1] = cmd.response[1]; |
665 | mmc->csd[2] = cmd.response[2]; | 665 | mmc->csd[2] = cmd.response[2]; |
666 | mmc->csd[3] = cmd.response[3]; | 666 | mmc->csd[3] = cmd.response[3]; |
667 | 667 | ||
668 | if (mmc->version == MMC_VERSION_UNKNOWN) { | 668 | if (mmc->version == MMC_VERSION_UNKNOWN) { |
669 | int version = (cmd.response[0] >> 26) & 0xf; | 669 | int version = (cmd.response[0] >> 26) & 0xf; |
670 | 670 | ||
671 | switch (version) { | 671 | switch (version) { |
672 | case 0: | 672 | case 0: |
673 | mmc->version = MMC_VERSION_1_2; | 673 | mmc->version = MMC_VERSION_1_2; |
674 | break; | 674 | break; |
675 | case 1: | 675 | case 1: |
676 | mmc->version = MMC_VERSION_1_4; | 676 | mmc->version = MMC_VERSION_1_4; |
677 | break; | 677 | break; |
678 | case 2: | 678 | case 2: |
679 | mmc->version = MMC_VERSION_2_2; | 679 | mmc->version = MMC_VERSION_2_2; |
680 | break; | 680 | break; |
681 | case 3: | 681 | case 3: |
682 | mmc->version = MMC_VERSION_3; | 682 | mmc->version = MMC_VERSION_3; |
683 | break; | 683 | break; |
684 | case 4: | 684 | case 4: |
685 | mmc->version = MMC_VERSION_4; | 685 | mmc->version = MMC_VERSION_4; |
686 | break; | 686 | break; |
687 | default: | 687 | default: |
688 | mmc->version = MMC_VERSION_1_2; | 688 | mmc->version = MMC_VERSION_1_2; |
689 | break; | 689 | break; |
690 | } | 690 | } |
691 | } | 691 | } |
692 | 692 | ||
693 | /* divide frequency by 10, since the mults are 10x bigger */ | 693 | /* divide frequency by 10, since the mults are 10x bigger */ |
694 | freq = fbase[(cmd.response[0] & 0x7)]; | 694 | freq = fbase[(cmd.response[0] & 0x7)]; |
695 | mult = multipliers[((cmd.response[0] >> 3) & 0xf)]; | 695 | mult = multipliers[((cmd.response[0] >> 3) & 0xf)]; |
696 | 696 | ||
697 | mmc->tran_speed = freq * mult; | 697 | mmc->tran_speed = freq * mult; |
698 | 698 | ||
699 | mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf); | 699 | mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf); |
700 | 700 | ||
701 | if (IS_SD(mmc)) | 701 | if (IS_SD(mmc)) |
702 | mmc->write_bl_len = mmc->read_bl_len; | 702 | mmc->write_bl_len = mmc->read_bl_len; |
703 | else | 703 | else |
704 | mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf); | 704 | mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf); |
705 | 705 | ||
706 | if (mmc->high_capacity) { | 706 | if (mmc->high_capacity) { |
707 | csize = (mmc->csd[1] & 0x3f) << 16 | 707 | csize = (mmc->csd[1] & 0x3f) << 16 |
708 | | (mmc->csd[2] & 0xffff0000) >> 16; | 708 | | (mmc->csd[2] & 0xffff0000) >> 16; |
709 | cmult = 8; | 709 | cmult = 8; |
710 | } else { | 710 | } else { |
711 | csize = (mmc->csd[1] & 0x3ff) << 2 | 711 | csize = (mmc->csd[1] & 0x3ff) << 2 |
712 | | (mmc->csd[2] & 0xc0000000) >> 30; | 712 | | (mmc->csd[2] & 0xc0000000) >> 30; |
713 | cmult = (mmc->csd[2] & 0x00038000) >> 15; | 713 | cmult = (mmc->csd[2] & 0x00038000) >> 15; |
714 | } | 714 | } |
715 | 715 | ||
716 | mmc->capacity = (csize + 1) << (cmult + 2); | 716 | mmc->capacity = (csize + 1) << (cmult + 2); |
717 | mmc->capacity *= mmc->read_bl_len; | 717 | mmc->capacity *= mmc->read_bl_len; |
718 | 718 | ||
719 | if (mmc->read_bl_len > 512) | 719 | if (mmc->read_bl_len > 512) |
720 | mmc->read_bl_len = 512; | 720 | mmc->read_bl_len = 512; |
721 | 721 | ||
722 | if (mmc->write_bl_len > 512) | 722 | if (mmc->write_bl_len > 512) |
723 | mmc->write_bl_len = 512; | 723 | mmc->write_bl_len = 512; |
724 | 724 | ||
725 | /* Select the card, and put it into Transfer Mode */ | 725 | /* Select the card, and put it into Transfer Mode */ |
726 | cmd.cmdidx = MMC_CMD_SELECT_CARD; | 726 | cmd.cmdidx = MMC_CMD_SELECT_CARD; |
727 | cmd.resp_type = MMC_RSP_R1b; | 727 | cmd.resp_type = MMC_RSP_R1b; |
728 | cmd.cmdarg = mmc->rca << 16; | 728 | cmd.cmdarg = mmc->rca << 16; |
729 | cmd.flags = 0; | 729 | cmd.flags = 0; |
730 | err = mmc_send_cmd(mmc, &cmd, NULL); | 730 | err = mmc_send_cmd(mmc, &cmd, NULL); |
731 | 731 | ||
732 | if (err) | 732 | if (err) |
733 | return err; | 733 | return err; |
734 | 734 | ||
735 | if (IS_SD(mmc)) | 735 | if (IS_SD(mmc)) |
736 | err = sd_change_freq(mmc); | 736 | err = sd_change_freq(mmc); |
737 | else | 737 | else |
738 | err = mmc_change_freq(mmc); | 738 | err = mmc_change_freq(mmc); |
739 | 739 | ||
740 | if (err) | 740 | if (err) |
741 | return err; | 741 | return err; |
742 | 742 | ||
743 | /* Restrict card's capabilities by what the host can do */ | 743 | /* Restrict card's capabilities by what the host can do */ |
744 | mmc->card_caps &= mmc->host_caps; | 744 | mmc->card_caps &= mmc->host_caps; |
745 | 745 | ||
746 | if (IS_SD(mmc)) { | 746 | if (IS_SD(mmc)) { |
747 | if (mmc->card_caps & MMC_MODE_4BIT) { | 747 | if (mmc->card_caps & MMC_MODE_4BIT) { |
748 | cmd.cmdidx = MMC_CMD_APP_CMD; | 748 | cmd.cmdidx = MMC_CMD_APP_CMD; |
749 | cmd.resp_type = MMC_RSP_R1; | 749 | cmd.resp_type = MMC_RSP_R1; |
750 | cmd.cmdarg = mmc->rca << 16; | 750 | cmd.cmdarg = mmc->rca << 16; |
751 | cmd.flags = 0; | 751 | cmd.flags = 0; |
752 | 752 | ||
753 | err = mmc_send_cmd(mmc, &cmd, NULL); | 753 | err = mmc_send_cmd(mmc, &cmd, NULL); |
754 | if (err) | 754 | if (err) |
755 | return err; | 755 | return err; |
756 | 756 | ||
757 | cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH; | 757 | cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH; |
758 | cmd.resp_type = MMC_RSP_R1; | 758 | cmd.resp_type = MMC_RSP_R1; |
759 | cmd.cmdarg = 2; | 759 | cmd.cmdarg = 2; |
760 | cmd.flags = 0; | 760 | cmd.flags = 0; |
761 | err = mmc_send_cmd(mmc, &cmd, NULL); | 761 | err = mmc_send_cmd(mmc, &cmd, NULL); |
762 | if (err) | 762 | if (err) |
763 | return err; | 763 | return err; |
764 | 764 | ||
765 | mmc_set_bus_width(mmc, 4); | 765 | mmc_set_bus_width(mmc, 4); |
766 | } | 766 | } |
767 | 767 | ||
768 | if (mmc->card_caps & MMC_MODE_HS) | 768 | if (mmc->card_caps & MMC_MODE_HS) |
769 | mmc_set_clock(mmc, 50000000); | 769 | mmc_set_clock(mmc, 50000000); |
770 | else | 770 | else |
771 | mmc_set_clock(mmc, 25000000); | 771 | mmc_set_clock(mmc, 25000000); |
772 | } else { | 772 | } else { |
773 | if (mmc->card_caps & MMC_MODE_4BIT) { | 773 | if (mmc->card_caps & MMC_MODE_4BIT) { |
774 | /* Set the card to use 4 bit*/ | 774 | /* Set the card to use 4 bit*/ |
775 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, | 775 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
776 | EXT_CSD_BUS_WIDTH, | 776 | EXT_CSD_BUS_WIDTH, |
777 | EXT_CSD_BUS_WIDTH_4); | 777 | EXT_CSD_BUS_WIDTH_4); |
778 | 778 | ||
779 | if (err) | 779 | if (err) |
780 | return err; | 780 | return err; |
781 | 781 | ||
782 | mmc_set_bus_width(mmc, 4); | 782 | mmc_set_bus_width(mmc, 4); |
783 | } else if (mmc->card_caps & MMC_MODE_8BIT) { | 783 | } else if (mmc->card_caps & MMC_MODE_8BIT) { |
784 | /* Set the card to use 8 bit*/ | 784 | /* Set the card to use 8 bit*/ |
785 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, | 785 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
786 | EXT_CSD_BUS_WIDTH, | 786 | EXT_CSD_BUS_WIDTH, |
787 | EXT_CSD_BUS_WIDTH_8); | 787 | EXT_CSD_BUS_WIDTH_8); |
788 | 788 | ||
789 | if (err) | 789 | if (err) |
790 | return err; | 790 | return err; |
791 | 791 | ||
792 | mmc_set_bus_width(mmc, 8); | 792 | mmc_set_bus_width(mmc, 8); |
793 | } | 793 | } |
794 | 794 | ||
795 | if (mmc->card_caps & MMC_MODE_HS) { | 795 | if (mmc->card_caps & MMC_MODE_HS) { |
796 | if (mmc->card_caps & MMC_MODE_HS_52MHz) | 796 | if (mmc->card_caps & MMC_MODE_HS_52MHz) |
797 | mmc_set_clock(mmc, 52000000); | 797 | mmc_set_clock(mmc, 52000000); |
798 | else | 798 | else |
799 | mmc_set_clock(mmc, 26000000); | 799 | mmc_set_clock(mmc, 26000000); |
800 | } else | 800 | } else |
801 | mmc_set_clock(mmc, 20000000); | 801 | mmc_set_clock(mmc, 20000000); |
802 | } | 802 | } |
803 | 803 | ||
804 | /* fill in device description */ | 804 | /* fill in device description */ |
805 | mmc->block_dev.lun = 0; | 805 | mmc->block_dev.lun = 0; |
806 | mmc->block_dev.type = 0; | 806 | mmc->block_dev.type = 0; |
807 | mmc->block_dev.blksz = mmc->read_bl_len; | 807 | mmc->block_dev.blksz = mmc->read_bl_len; |
808 | mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len); | 808 | mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len); |
809 | sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8, | 809 | sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8, |
810 | (mmc->cid[2] << 8) | (mmc->cid[3] >> 24)); | 810 | (mmc->cid[2] << 8) | (mmc->cid[3] >> 24)); |
811 | sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff, | 811 | sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff, |
812 | (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff, | 812 | (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff, |
813 | (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff); | 813 | (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff); |
814 | sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28, | 814 | sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28, |
815 | (mmc->cid[2] >> 24) & 0xf); | 815 | (mmc->cid[2] >> 24) & 0xf); |
816 | init_part(&mmc->block_dev); | 816 | init_part(&mmc->block_dev); |
817 | 817 | ||
818 | return 0; | 818 | return 0; |
819 | } | 819 | } |
820 | 820 | ||
821 | int mmc_send_if_cond(struct mmc *mmc) | 821 | int mmc_send_if_cond(struct mmc *mmc) |
822 | { | 822 | { |
823 | struct mmc_cmd cmd; | 823 | struct mmc_cmd cmd; |
824 | int err; | 824 | int err; |
825 | 825 | ||
826 | cmd.cmdidx = SD_CMD_SEND_IF_COND; | 826 | cmd.cmdidx = SD_CMD_SEND_IF_COND; |
827 | /* We set the bit if the host supports voltages between 2.7 and 3.6 V */ | 827 | /* We set the bit if the host supports voltages between 2.7 and 3.6 V */ |
828 | cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa; | 828 | cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa; |
829 | cmd.resp_type = MMC_RSP_R7; | 829 | cmd.resp_type = MMC_RSP_R7; |
830 | cmd.flags = 0; | 830 | cmd.flags = 0; |
831 | 831 | ||
832 | err = mmc_send_cmd(mmc, &cmd, NULL); | 832 | err = mmc_send_cmd(mmc, &cmd, NULL); |
833 | 833 | ||
834 | if (err) | 834 | if (err) |
835 | return err; | 835 | return err; |
836 | 836 | ||
837 | if ((cmd.response[0] & 0xff) != 0xaa) | 837 | if ((cmd.response[0] & 0xff) != 0xaa) |
838 | return UNUSABLE_ERR; | 838 | return UNUSABLE_ERR; |
839 | else | 839 | else |
840 | mmc->version = SD_VERSION_2; | 840 | mmc->version = SD_VERSION_2; |
841 | 841 | ||
842 | return 0; | 842 | return 0; |
843 | } | 843 | } |
844 | 844 | ||
845 | int mmc_register(struct mmc *mmc) | 845 | int mmc_register(struct mmc *mmc) |
846 | { | 846 | { |
847 | /* Setup the universal parts of the block interface just once */ | 847 | /* Setup the universal parts of the block interface just once */ |
848 | mmc->block_dev.if_type = IF_TYPE_MMC; | 848 | mmc->block_dev.if_type = IF_TYPE_MMC; |
849 | mmc->block_dev.dev = cur_dev_num++; | 849 | mmc->block_dev.dev = cur_dev_num++; |
850 | mmc->block_dev.removable = 1; | 850 | mmc->block_dev.removable = 1; |
851 | mmc->block_dev.block_read = mmc_bread; | 851 | mmc->block_dev.block_read = mmc_bread; |
852 | mmc->block_dev.block_write = mmc_bwrite; | 852 | mmc->block_dev.block_write = mmc_bwrite; |
853 | 853 | ||
854 | INIT_LIST_HEAD (&mmc->link); | 854 | INIT_LIST_HEAD (&mmc->link); |
855 | 855 | ||
856 | list_add_tail (&mmc->link, &mmc_devices); | 856 | list_add_tail (&mmc->link, &mmc_devices); |
857 | 857 | ||
858 | return 0; | 858 | return 0; |
859 | } | 859 | } |
860 | 860 | ||
861 | block_dev_desc_t *mmc_get_dev(int dev) | 861 | block_dev_desc_t *mmc_get_dev(int dev) |
862 | { | 862 | { |
863 | struct mmc *mmc = find_mmc_device(dev); | 863 | struct mmc *mmc = find_mmc_device(dev); |
864 | 864 | ||
865 | return mmc ? &mmc->block_dev : NULL; | 865 | return mmc ? &mmc->block_dev : NULL; |
866 | } | 866 | } |
867 | 867 | ||
868 | int mmc_init(struct mmc *mmc) | 868 | int mmc_init(struct mmc *mmc) |
869 | { | 869 | { |
870 | int err; | 870 | int err; |
871 | 871 | ||
872 | err = mmc->init(mmc); | 872 | err = mmc->init(mmc); |
873 | 873 | ||
874 | if (err) | 874 | if (err) |
875 | return err; | 875 | return err; |
876 | 876 | ||
877 | mmc_set_bus_width(mmc, 1); | 877 | mmc_set_bus_width(mmc, 1); |
878 | mmc_set_clock(mmc, 1); | 878 | mmc_set_clock(mmc, 1); |
879 | 879 | ||
880 | /* Reset the Card */ | 880 | /* Reset the Card */ |
881 | err = mmc_go_idle(mmc); | 881 | err = mmc_go_idle(mmc); |
882 | 882 | ||
883 | if (err) | 883 | if (err) |
884 | return err; | 884 | return err; |
885 | 885 | ||
886 | /* Test for SD version 2 */ | 886 | /* Test for SD version 2 */ |
887 | err = mmc_send_if_cond(mmc); | 887 | err = mmc_send_if_cond(mmc); |
888 | 888 | ||
889 | /* Now try to get the SD card's operating condition */ | 889 | /* Now try to get the SD card's operating condition */ |
890 | err = sd_send_op_cond(mmc); | 890 | err = sd_send_op_cond(mmc); |
891 | 891 | ||
892 | /* If the command timed out, we check for an MMC card */ | 892 | /* If the command timed out, we check for an MMC card */ |
893 | if (err == TIMEOUT) { | 893 | if (err == TIMEOUT) { |
894 | err = mmc_send_op_cond(mmc); | 894 | err = mmc_send_op_cond(mmc); |
895 | 895 | ||
896 | if (err) { | 896 | if (err) { |
897 | printf("Card did not respond to voltage select!\n"); | 897 | printf("Card did not respond to voltage select!\n"); |
898 | return UNUSABLE_ERR; | 898 | return UNUSABLE_ERR; |
899 | } | 899 | } |
900 | } | 900 | } |
901 | 901 | ||
902 | return mmc_startup(mmc); | 902 | return mmc_startup(mmc); |
903 | } | 903 | } |
904 | 904 | ||
905 | /* | 905 | /* |
906 | * CPU and board-specific MMC initializations. Aliased function | 906 | * CPU and board-specific MMC initializations. Aliased function |
907 | * signals caller to move on | 907 | * signals caller to move on |
908 | */ | 908 | */ |
909 | static int __def_mmc_init(bd_t *bis) | 909 | static int __def_mmc_init(bd_t *bis) |
910 | { | 910 | { |
911 | return -1; | 911 | return -1; |
912 | } | 912 | } |
913 | 913 | ||
914 | int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init"))); | 914 | int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init"))); |
915 | int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init"))); | 915 | int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init"))); |
916 | 916 | ||
917 | void print_mmc_devices(char separator) | 917 | void print_mmc_devices(char separator) |
918 | { | 918 | { |
919 | struct mmc *m; | 919 | struct mmc *m; |
920 | struct list_head *entry; | 920 | struct list_head *entry; |
921 | 921 | ||
922 | list_for_each(entry, &mmc_devices) { | 922 | list_for_each(entry, &mmc_devices) { |
923 | m = list_entry(entry, struct mmc, link); | 923 | m = list_entry(entry, struct mmc, link); |
924 | 924 | ||
925 | printf("%s: %d", m->name, m->block_dev.dev); | 925 | printf("%s: %d", m->name, m->block_dev.dev); |
926 | 926 | ||
927 | if (entry->next != &mmc_devices) | 927 | if (entry->next != &mmc_devices) |
928 | printf("%c ", separator); | 928 | printf("%c ", separator); |
929 | } | 929 | } |
930 | 930 | ||
931 | printf("\n"); | 931 | printf("\n"); |
932 | } | 932 | } |
933 | 933 | ||
934 | int mmc_initialize(bd_t *bis) | 934 | int mmc_initialize(bd_t *bis) |
935 | { | 935 | { |
936 | INIT_LIST_HEAD (&mmc_devices); | 936 | INIT_LIST_HEAD (&mmc_devices); |
937 | cur_dev_num = 0; | 937 | cur_dev_num = 0; |
938 | 938 | ||
939 | if (board_mmc_init(bis) < 0) | 939 | if (board_mmc_init(bis) < 0) |
940 | cpu_mmc_init(bis); | 940 | cpu_mmc_init(bis); |
941 | 941 | ||
942 | print_mmc_devices(','); | 942 | print_mmc_devices(','); |
943 | 943 | ||
944 | return 0; | 944 | return 0; |
945 | } | 945 | } |
946 | 946 |