Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2010 |
| 3 | * Texas Instruments, <www.ti.com> |
| 4 | * |
| 5 | * Aneesh V <aneesh@ti.com> |
| 6 | * |
| 7 | * See file CREDITS for list of people who contributed to this |
| 8 | * project. |
| 9 | * |
| 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 |
| 12 | * published by the Free Software Foundation; either version 2 of |
| 13 | * the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 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 |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 23 | * MA 02111-1307 USA |
| 24 | */ |
| 25 | #include <common.h> |
Tom Rini | 28591df | 2012-08-13 12:03:19 -0700 | [diff] [blame] | 26 | #include <spl.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 27 | #include <asm/u-boot.h> |
| 28 | #include <asm/utils.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 29 | #include <mmc.h> |
| 30 | #include <fat.h> |
Simon Glass | 5040241 | 2011-10-10 08:55:19 +0000 | [diff] [blame] | 31 | #include <version.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 32 | |
| 33 | DECLARE_GLOBAL_DATA_PTR; |
| 34 | |
Peter Korsgaard | 5e5483d | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 35 | static int mmc_load_image_raw(struct mmc *mmc) |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 36 | { |
Peter Korsgaard | f247129 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 37 | unsigned long err; |
| 38 | u32 image_size_sectors; |
| 39 | struct image_header *header; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 40 | |
| 41 | header = (struct image_header *)(CONFIG_SYS_TEXT_BASE - |
| 42 | sizeof(struct image_header)); |
| 43 | |
| 44 | /* read image header to find the image size & load address */ |
| 45 | err = mmc->block_dev.block_read(0, |
| 46 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR, 1, |
Peter Korsgaard | f247129 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 47 | header); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 48 | |
Peter Korsgaard | f247129 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 49 | if (err == 0) |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 50 | goto end; |
| 51 | |
| 52 | spl_parse_image_header(header); |
| 53 | |
| 54 | /* convert size to sectors - round up */ |
Tom Rini | a0b9fa5 | 2012-08-14 10:25:15 -0700 | [diff] [blame] | 55 | image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) / |
| 56 | mmc->read_bl_len; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 57 | |
| 58 | /* Read the header too to avoid extra memcpy */ |
| 59 | err = mmc->block_dev.block_read(0, |
| 60 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR, |
| 61 | image_size_sectors, (void *)spl_image.load_addr); |
| 62 | |
| 63 | end: |
Peter Korsgaard | 5e5483d | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 64 | if (err == 0) |
Peter Korsgaard | f247129 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 65 | printf("spl: mmc blk read err - %lu\n", err); |
Peter Korsgaard | 5e5483d | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 66 | |
| 67 | return (err == 0); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 68 | } |
| 69 | |
Tom Rini | a0d8cca | 2012-08-10 09:27:14 -0700 | [diff] [blame] | 70 | #ifdef CONFIG_SPL_FAT_SUPPORT |
Peter Korsgaard | b815ba2 | 2013-05-13 08:36:26 +0000 | [diff] [blame^] | 71 | static int mmc_load_image_fat(struct mmc *mmc, const char *filename) |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 72 | { |
Peter Korsgaard | f247129 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 73 | int err; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 74 | struct image_header *header; |
| 75 | |
| 76 | header = (struct image_header *)(CONFIG_SYS_TEXT_BASE - |
| 77 | sizeof(struct image_header)); |
| 78 | |
Peter Korsgaard | b815ba2 | 2013-05-13 08:36:26 +0000 | [diff] [blame^] | 79 | err = file_fat_read(filename, header, sizeof(struct image_header)); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 80 | if (err <= 0) |
| 81 | goto end; |
| 82 | |
| 83 | spl_parse_image_header(header); |
| 84 | |
Peter Korsgaard | b815ba2 | 2013-05-13 08:36:26 +0000 | [diff] [blame^] | 85 | err = file_fat_read(filename, (u8 *)spl_image.load_addr, 0); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 86 | |
| 87 | end: |
Peter Korsgaard | 5e5483d | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 88 | if (err <= 0) |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 89 | printf("spl: error reading image %s, err - %d\n", |
Peter Korsgaard | b815ba2 | 2013-05-13 08:36:26 +0000 | [diff] [blame^] | 90 | filename, err); |
Peter Korsgaard | 5e5483d | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 91 | |
| 92 | return (err <= 0); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 93 | } |
Tom Rini | a0d8cca | 2012-08-10 09:27:14 -0700 | [diff] [blame] | 94 | #endif |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 95 | |
| 96 | void spl_mmc_load_image(void) |
| 97 | { |
| 98 | struct mmc *mmc; |
| 99 | int err; |
| 100 | u32 boot_mode; |
| 101 | |
| 102 | mmc_initialize(gd->bd); |
| 103 | /* We register only one device. So, the dev id is always 0 */ |
| 104 | mmc = find_mmc_device(0); |
| 105 | if (!mmc) { |
| 106 | puts("spl: mmc device not found!!\n"); |
| 107 | hang(); |
| 108 | } |
| 109 | |
| 110 | err = mmc_init(mmc); |
| 111 | if (err) { |
| 112 | printf("spl: mmc init failed: err - %d\n", err); |
| 113 | hang(); |
| 114 | } |
Peter Korsgaard | 5e5483d | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 115 | |
Tom Rini | a76ff95 | 2012-08-14 09:19:44 -0700 | [diff] [blame] | 116 | boot_mode = spl_boot_mode(); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 117 | if (boot_mode == MMCSD_MODE_RAW) { |
| 118 | debug("boot mode - RAW\n"); |
Peter Korsgaard | 5e5483d | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 119 | err = mmc_load_image_raw(mmc); |
Tom Rini | a0d8cca | 2012-08-10 09:27:14 -0700 | [diff] [blame] | 120 | #ifdef CONFIG_SPL_FAT_SUPPORT |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 121 | } else if (boot_mode == MMCSD_MODE_FAT) { |
| 122 | debug("boot mode - FAT\n"); |
Peter Korsgaard | b815ba2 | 2013-05-13 08:36:26 +0000 | [diff] [blame^] | 123 | |
| 124 | err = fat_register_device(&mmc->block_dev, |
| 125 | CONFIG_SYS_MMC_SD_FAT_BOOT_PARTITION); |
| 126 | if (err) { |
| 127 | printf("spl: fat register err - %d\n", err); |
| 128 | hang(); |
| 129 | } |
| 130 | |
| 131 | err = mmc_load_image_fat(mmc, CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME); |
Tom Rini | a0d8cca | 2012-08-10 09:27:14 -0700 | [diff] [blame] | 132 | #endif |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 133 | } else { |
| 134 | puts("spl: wrong MMC boot mode\n"); |
| 135 | hang(); |
| 136 | } |
Peter Korsgaard | 5e5483d | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 137 | |
| 138 | if (err) |
| 139 | hang(); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 140 | } |