Merge branch 'master' of git://git.denx.de/u-boot-fsl-qoriq
[platform/kernel/u-boot.git] / 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 <dm.h>
11 #include <spl.h>
12 #include <linux/compiler.h>
13 #include <errno.h>
14 #include <asm/u-boot.h>
15 #include <errno.h>
16 #include <mmc.h>
17 #include <image.h>
18
19 DECLARE_GLOBAL_DATA_PTR;
20
21 static int mmc_load_legacy(struct mmc *mmc, ulong sector,
22                            struct image_header *header)
23 {
24         u32 image_size_sectors;
25         unsigned long count;
26
27         spl_parse_image_header(header);
28         /* convert size to sectors - round up */
29         image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
30                              mmc->read_bl_len;
31
32         /* Read the header too to avoid extra memcpy */
33         count = mmc->block_dev.block_read(&mmc->block_dev, sector,
34                                           image_size_sectors,
35                                           (void *)(ulong)spl_image.load_addr);
36         debug("read %x sectors to %x\n", image_size_sectors,
37               spl_image.load_addr);
38         if (count != image_size_sectors)
39                 return -EIO;
40
41         return 0;
42 }
43
44 static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
45                              ulong count, void *buf)
46 {
47         struct mmc *mmc = load->dev;
48
49         return mmc->block_dev.block_read(&mmc->block_dev, sector, count, buf);
50 }
51
52 static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
53 {
54         unsigned long count;
55         struct image_header *header;
56         int ret = 0;
57
58         header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
59                                          sizeof(struct image_header));
60
61         /* read image header to find the image size & load address */
62         count = mmc->block_dev.block_read(&mmc->block_dev, sector, 1, header);
63         debug("hdr read sector %lx, count=%lu\n", sector, count);
64         if (count == 0) {
65                 ret = -EIO;
66                 goto end;
67         }
68
69         if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
70             image_get_magic(header) == FDT_MAGIC) {
71                 struct spl_load_info load;
72
73                 debug("Found FIT\n");
74                 load.dev = mmc;
75                 load.priv = NULL;
76                 load.bl_len = mmc->read_bl_len;
77                 load.read = h_spl_load_read;
78                 ret = spl_load_simple_fit(&load, sector, header);
79         } else {
80                 ret = mmc_load_legacy(mmc, sector, header);
81         }
82
83 end:
84         if (ret) {
85 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
86                 puts("spl: mmc block read error\n");
87 #endif
88                 return -1;
89         }
90
91         return 0;
92 }
93
94 int spl_mmc_get_device_index(u32 boot_device)
95 {
96         switch (boot_device) {
97         case BOOT_DEVICE_MMC1:
98                 return 0;
99         case BOOT_DEVICE_MMC2:
100         case BOOT_DEVICE_MMC2_2:
101                 return 1;
102         }
103
104 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
105         printf("spl: unsupported mmc boot device.\n");
106 #endif
107
108         return -ENODEV;
109 }
110
111 static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
112 {
113 #ifdef CONFIG_DM_MMC
114         struct udevice *dev;
115 #endif
116         int err, mmc_dev;
117
118         mmc_dev = spl_mmc_get_device_index(boot_device);
119         if (mmc_dev < 0)
120                 return mmc_dev;
121
122         err = mmc_initialize(NULL);
123         if (err) {
124 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
125                 printf("spl: could not initialize mmc. error: %d\n", err);
126 #endif
127                 return err;
128         }
129
130 #ifdef CONFIG_DM_MMC
131         err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev);
132         if (!err)
133                 *mmcp = mmc_get_mmc_dev(dev);
134 #else
135         *mmcp = find_mmc_device(mmc_dev);
136         err = *mmcp ? 0 : -ENODEV;
137 #endif
138         if (err) {
139 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
140                 printf("spl: could not find mmc device. error: %d\n", err);
141 #endif
142                 return err;
143         }
144
145         return 0;
146 }
147
148 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
149 static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
150 {
151         disk_partition_t info;
152         int err;
153
154         err = part_get_info(&mmc->block_dev, partition, &info);
155         if (err) {
156 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
157                 puts("spl: partition error\n");
158 #endif
159                 return -1;
160         }
161
162 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
163         return mmc_load_image_raw_sector(mmc, info.start +
164                                          CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
165 #else
166         return mmc_load_image_raw_sector(mmc, info.start);
167 #endif
168 }
169 #else
170 #define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1
171 static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
172 {
173         return -ENOSYS;
174 }
175 #endif
176
177 #ifdef CONFIG_SPL_OS_BOOT
178 static int mmc_load_image_raw_os(struct mmc *mmc)
179 {
180         unsigned long count;
181
182         count = mmc->block_dev.block_read(&mmc->block_dev,
183                 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
184                 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
185                 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
186         if (count == 0) {
187 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
188                 puts("spl: mmc block read error\n");
189 #endif
190                 return -1;
191         }
192
193         return mmc_load_image_raw_sector(mmc,
194                 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
195 }
196 #else
197 int spl_start_uboot(void)
198 {
199         return 1;
200 }
201 static int mmc_load_image_raw_os(struct mmc *mmc)
202 {
203         return -ENOSYS;
204 }
205 #endif
206
207 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
208 int spl_mmc_do_fs_boot(struct mmc *mmc)
209 {
210         int err = -ENOSYS;
211
212 #ifdef CONFIG_SPL_FAT_SUPPORT
213         if (!spl_start_uboot()) {
214                 err = spl_load_image_fat_os(&mmc->block_dev,
215                         CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
216                 if (!err)
217                         return err;
218         }
219 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
220         err = spl_load_image_fat(&mmc->block_dev,
221                                  CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
222                                  CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
223         if (!err)
224                 return err;
225 #endif
226 #endif
227 #ifdef CONFIG_SPL_EXT_SUPPORT
228         if (!spl_start_uboot()) {
229                 err = spl_load_image_ext_os(&mmc->block_dev,
230                         CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
231                 if (!err)
232                         return err;
233         }
234 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
235         err = spl_load_image_ext(&mmc->block_dev,
236                                  CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
237                                  CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
238         if (!err)
239                 return err;
240 #endif
241 #endif
242
243 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
244         err = -ENOENT;
245 #endif
246
247         return err;
248 }
249 #else
250 int spl_mmc_do_fs_boot(struct mmc *mmc)
251 {
252         return -ENOSYS;
253 }
254 #endif
255
256 int spl_mmc_load_image(u32 boot_device)
257 {
258         struct mmc *mmc = NULL;
259         u32 boot_mode;
260         int err = 0;
261         __maybe_unused int part;
262
263         err = spl_mmc_find_device(&mmc, boot_device);
264         if (err)
265                 return err;
266
267         err = mmc_init(mmc);
268         if (err) {
269 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
270                 printf("spl: mmc init failed with error: %d\n", err);
271 #endif
272                 return err;
273         }
274
275         boot_mode = spl_boot_mode();
276         err = -EINVAL;
277         switch (boot_mode) {
278         case MMCSD_MODE_EMMCBOOT:
279                         /*
280                          * We need to check what the partition is configured to.
281                          * 1 and 2 match up to boot0 / boot1 and 7 is user data
282                          * which is the first physical partition (0).
283                          */
284                         part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
285
286                         if (part == 7)
287                                 part = 0;
288
289                         err = mmc_switch_part(0, part);
290                         if (err) {
291 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
292                                 puts("spl: mmc partition switch failed\n");
293 #endif
294                                 return err;
295                         }
296                         /* Fall through */
297         case MMCSD_MODE_RAW:
298                 debug("spl: mmc boot mode: raw\n");
299
300                 if (!spl_start_uboot()) {
301                         err = mmc_load_image_raw_os(mmc);
302                         if (!err)
303                                 return err;
304                 }
305
306                 err = mmc_load_image_raw_partition(mmc,
307                         CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
308                 if (!err)
309                         return err;
310 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
311                 err = mmc_load_image_raw_sector(mmc,
312                         CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
313                 if (!err)
314                         return err;
315 #endif
316                 /* If RAW mode fails, try FS mode. */
317         case MMCSD_MODE_FS:
318                 debug("spl: mmc boot mode: fs\n");
319
320                 err = spl_mmc_do_fs_boot(mmc);
321                 if (!err)
322                         return err;
323
324                 break;
325         case MMCSD_MODE_UNDEFINED:
326 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
327         default:
328                 puts("spl: mmc: wrong boot mode\n");
329 #endif
330         }
331
332         return err;
333 }