]> git.sur5r.net Git - u-boot/blob - common/spl/spl_mmc.c
spl: spl_mmc: Minor cosmetics
[u-boot] / common / spl / spl_mmc.c
1 /*
2  * (C) Copyright 2010
3  * Texas Instruments, <www.ti.com>
4  *
5  * Aneesh V <aneesh@ti.com>
6  *
7  * SPDX-License-Identifier:     GPL-2.0+
8  */
9 #include <common.h>
10 #include <spl.h>
11 #include <linux/compiler.h>
12 #include <asm/u-boot.h>
13 #include <mmc.h>
14 #include <image.h>
15
16 DECLARE_GLOBAL_DATA_PTR;
17
18 static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
19 {
20         unsigned long count;
21         u32 image_size_sectors;
22         struct image_header *header;
23
24         header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
25                                          sizeof(struct image_header));
26
27         /* read image header to find the image size & load address */
28         count = mmc->block_dev.block_read(0, sector, 1, header);
29         if (count == 0)
30                 goto end;
31
32         if (image_get_magic(header) != IH_MAGIC)
33                 return -1;
34
35         spl_parse_image_header(header);
36
37         /* convert size to sectors - round up */
38         image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
39                              mmc->read_bl_len;
40
41         /* Read the header too to avoid extra memcpy */
42         count = mmc->block_dev.block_read(0, sector, image_size_sectors,
43                                           (void *) spl_image.load_addr);
44
45 end:
46         if (count == 0) {
47 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
48                 puts("spl: mmc block read error\n");
49 #endif
50                 return -1;
51         }
52
53         return 0;
54 }
55
56 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
57 static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
58 {
59         disk_partition_t info;
60         int err;
61
62         err = get_partition_info(&mmc->block_dev, partition, &info);
63         if (err) {
64 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
65                 puts("spl: partition error\n");
66 #endif
67                 return -1;
68         }
69
70         return mmc_load_image_raw_sector(mmc, info.start);
71 }
72 #endif
73
74 #ifdef CONFIG_SPL_OS_BOOT
75 static int mmc_load_image_raw_os(struct mmc *mmc)
76 {
77         unsigned long count;
78
79         count = mmc->block_dev.block_read(0,
80                 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
81                 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
82                 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
83         if (count == 0) {
84 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
85                 puts("spl: mmc block read error\n");
86 #endif
87                 return -1;
88         }
89
90         return mmc_load_image_raw_sector(mmc,
91                 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
92 }
93 #endif
94
95 void spl_mmc_load_image(void)
96 {
97         struct mmc *mmc;
98         u32 boot_mode;
99         int err;
100         __maybe_unused int part;
101
102         mmc_initialize(gd->bd);
103
104         /* We register only one device. So, the dev id is always 0 */
105         mmc = find_mmc_device(0);
106         if (!mmc) {
107 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
108                 puts("spl: mmc device not found\n");
109 #endif
110                 hang();
111         }
112
113         err = mmc_init(mmc);
114         if (err) {
115 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
116                 printf("spl: mmc init failed with error: %d\n", err);
117 #endif
118                 hang();
119         }
120
121         boot_mode = spl_boot_mode();
122         switch (boot_mode) {
123         case MMCSD_MODE_RAW:
124                 debug("spl: mmc boot mode: raw\n");
125
126 #ifdef CONFIG_SPL_OS_BOOT
127                 if (!spl_start_uboot()) {
128                         err = mmc_load_image_raw_os(mmc);
129                         if (!err)
130                                 return;
131                 }
132 #endif
133 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
134                 err = mmc_load_image_raw_partition(mmc,
135                         CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
136 #else
137                 err = mmc_load_image_raw_sector(mmc,
138                         CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
139 #endif
140                 if (!err)
141                         return;
142 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
143         case MMCSD_MODE_FS:
144                 debug("spl: mmc boot mode: fs\n");
145
146 #ifdef CONFIG_SPL_FAT_SUPPORT
147 #ifdef CONFIG_SPL_OS_BOOT
148                 if (!spl_start_uboot()) {
149                         err = spl_load_image_fat_os(&mmc->block_dev,
150                                 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
151                         if (!err)
152                                 return;
153                 }
154 #endif
155                 err = spl_load_image_fat(&mmc->block_dev,
156                                          CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
157                                          CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
158                 if (!err)
159                         return;
160 #endif
161 #ifdef CONFIG_SPL_EXT_SUPPORT
162 #ifdef CONFIG_SPL_OS_BOOT
163                 if (!spl_start_uboot()) {
164                         err = spl_load_image_ext_os(&mmc->block_dev,
165                                 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
166                         if (!err)
167                                 return;
168                 }
169 #endif
170                 err = spl_load_image_ext(&mmc->block_dev,
171                                          CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
172                                          CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
173                 if (!err)
174                         return;
175 #endif
176 #endif
177 #ifdef CONFIG_SUPPORT_EMMC_BOOT
178         case MMCSD_MODE_EMMCBOOT:
179                 /*
180                  * We need to check what the partition is configured to.
181                  * 1 and 2 match up to boot0 / boot1 and 7 is user data
182                  * which is the first physical partition (0).
183                  */
184                 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
185
186                 if (part == 7)
187                         part = 0;
188
189                 if (mmc_switch_part(0, part)) {
190 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
191                         puts("spl: mmc partition switch failed\n");
192 #endif
193                         hang();
194                 }
195
196 #ifdef CONFIG_SPL_OS_BOOT
197                 if (!spl_start_uboot()) {
198                         err = mmc_load_image_raw_os(mmc);
199                         if (!err)
200                                 return;
201                 }
202 #endif
203 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
204                 err = mmc_load_image_raw_partition(mmc,
205                         CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
206 #else
207                 err = mmc_load_image_raw_sector(mmc,
208                         CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
209 #endif
210                 if (!err)
211                         return;
212 #endif
213         case MMCSD_MODE_UNDEFINED:
214         default:
215 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
216                 if (err)
217                         puts("spl: mmc: no boot mode left to try\n");
218                 else
219                         puts("spl: mmc: wrong boot mode\n");
220 #endif
221                 hang();
222         }
223 }