blob: f5fd8715cf086673f130e93f5a4e39e44182995f [file] [log] [blame]
Loh Tien Hock59400a42019-02-04 16:17:24 +08001/*
2 * Copyright (c) 2019, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <arch_helpers.h>
8#include <assert.h>
9#include <common/debug.h>
10#include <drivers/mmc.h>
11#include <tools_share/firmware_image_package.h>
12#include <drivers/io/io_block.h>
13#include <drivers/io/io_driver.h>
14#include <drivers/io/io_fip.h>
15#include <drivers/io/io_memmap.h>
16#include <drivers/io/io_storage.h>
17#include <lib/mmio.h>
18#include <drivers/partition/partition.h>
19#include <lib/semihosting.h>
20#include <string.h>
21#include <lib/utils.h>
22#include <common/tbbr/tbbr_img_def.h>
23#include "platform_def.h"
Muhammad Hadi Asyrafi Abdul Halim2444bfa2019-03-08 19:02:33 +080024#include "aarch64/stratix10_private.h"
Loh Tien Hock59400a42019-02-04 16:17:24 +080025
Muhammad Hadi Asyrafi Abdul Halim2444bfa2019-03-08 19:02:33 +080026#define STRATIX10_FIP_BASE (0)
27#define STRATIX10_FIP_MAX_SIZE (0x1000000)
28#define STRATIX10_MMC_DATA_BASE (0xffe3c000)
29#define STRATIX10_MMC_DATA_SIZE (0x2000)
30#define STRATIX10_QSPI_DATA_BASE (0x3C00000)
31#define STRATIX10_QSPI_DATA_SIZE (0x1000000)
Loh Tien Hock59400a42019-02-04 16:17:24 +080032
Muhammad Hadi Asyrafi Abdul Halim2444bfa2019-03-08 19:02:33 +080033
Loh Tien Hock59400a42019-02-04 16:17:24 +080034static const io_dev_connector_t *fip_dev_con;
Muhammad Hadi Asyrafi Abdul Halim2444bfa2019-03-08 19:02:33 +080035static const io_dev_connector_t *boot_dev_con;
Loh Tien Hock59400a42019-02-04 16:17:24 +080036
37static uintptr_t fip_dev_handle;
Muhammad Hadi Asyrafi Abdul Halim2444bfa2019-03-08 19:02:33 +080038static uintptr_t boot_dev_handle;
Loh Tien Hock59400a42019-02-04 16:17:24 +080039
40static const io_uuid_spec_t bl2_uuid_spec = {
41 .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
42};
43
44static const io_uuid_spec_t bl31_uuid_spec = {
45 .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
46};
47
48static const io_uuid_spec_t bl33_uuid_spec = {
49 .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
50};
51
52uintptr_t a2_lba_offset;
Muhammad Hadi Asyrafi Abdul Halim2444bfa2019-03-08 19:02:33 +080053const char a2[] = {0xa2, 0x0};
Loh Tien Hock59400a42019-02-04 16:17:24 +080054
55static const io_block_spec_t gpt_block_spec = {
56 .offset = 0,
57 .length = MMC_BLOCK_SIZE
58};
59
Loh Tien Hock59400a42019-02-04 16:17:24 +080060static int check_fip(const uintptr_t spec);
Muhammad Hadi Asyrafi Abdul Halim2444bfa2019-03-08 19:02:33 +080061static int check_dev(const uintptr_t spec);
Loh Tien Hock59400a42019-02-04 16:17:24 +080062
Muhammad Hadi Asyrafi Abdul Halim2444bfa2019-03-08 19:02:33 +080063static io_block_dev_spec_t boot_dev_spec;
64static int (*register_io_dev)(const io_dev_connector_t **);
65
66static io_block_spec_t fip_spec = {
Loh Tien Hock59400a42019-02-04 16:17:24 +080067 .offset = STRATIX10_FIP_BASE,
68 .length = STRATIX10_FIP_MAX_SIZE,
69};
70
Loh Tien Hock59400a42019-02-04 16:17:24 +080071struct plat_io_policy {
Muhammad Hadi Asyrafi Abdul Halim2444bfa2019-03-08 19:02:33 +080072 uintptr_t *dev_handle;
73 uintptr_t image_spec;
74 int (*check)(const uintptr_t spec);
Loh Tien Hock59400a42019-02-04 16:17:24 +080075};
76
77static const struct plat_io_policy policies[] = {
78 [FIP_IMAGE_ID] = {
Muhammad Hadi Asyrafi Abdul Halim2444bfa2019-03-08 19:02:33 +080079 &boot_dev_handle,
80 (uintptr_t)&fip_spec,
81 check_dev
Loh Tien Hock59400a42019-02-04 16:17:24 +080082 },
83 [BL2_IMAGE_ID] = {
84 &fip_dev_handle,
85 (uintptr_t)&bl2_uuid_spec,
86 check_fip
87 },
88 [BL31_IMAGE_ID] = {
89 &fip_dev_handle,
90 (uintptr_t)&bl31_uuid_spec,
91 check_fip
92 },
93 [BL33_IMAGE_ID] = {
94 &fip_dev_handle,
95 (uintptr_t) &bl33_uuid_spec,
96 check_fip
97 },
98 [GPT_IMAGE_ID] = {
Muhammad Hadi Asyrafi Abdul Halim2444bfa2019-03-08 19:02:33 +080099 &boot_dev_handle,
Loh Tien Hock59400a42019-02-04 16:17:24 +0800100 (uintptr_t) &gpt_block_spec,
Muhammad Hadi Asyrafi Abdul Halim2444bfa2019-03-08 19:02:33 +0800101 check_dev
Loh Tien Hock59400a42019-02-04 16:17:24 +0800102 },
103};
104
Muhammad Hadi Asyrafi Abdul Halim2444bfa2019-03-08 19:02:33 +0800105static int check_dev(const uintptr_t spec)
Loh Tien Hock59400a42019-02-04 16:17:24 +0800106{
107 int result;
108 uintptr_t local_handle;
109
Muhammad Hadi Asyrafi Abdul Halim2444bfa2019-03-08 19:02:33 +0800110 result = io_dev_init(boot_dev_handle, (uintptr_t)NULL);
Loh Tien Hock59400a42019-02-04 16:17:24 +0800111 if (result == 0) {
Muhammad Hadi Asyrafi Abdul Halim2444bfa2019-03-08 19:02:33 +0800112 result = io_open(boot_dev_handle, spec, &local_handle);
Loh Tien Hock59400a42019-02-04 16:17:24 +0800113 if (result == 0)
114 io_close(local_handle);
115 }
116 return result;
117}
118
119static int check_fip(const uintptr_t spec)
120{
121 int result;
122 uintptr_t local_image_handle;
123
124 result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
125 if (result == 0) {
126 result = io_open(fip_dev_handle, spec, &local_image_handle);
127 if (result == 0)
128 io_close(local_image_handle);
129 }
130 return result;
131}
132
Muhammad Hadi Asyrafi Abdul Halim2444bfa2019-03-08 19:02:33 +0800133void stratix10_io_setup(int boot_source)
Loh Tien Hock59400a42019-02-04 16:17:24 +0800134{
135 int result;
136
Muhammad Hadi Asyrafi Abdul Halim2444bfa2019-03-08 19:02:33 +0800137 switch (boot_source) {
138 case BOOT_SOURCE_SDMMC:
139 register_io_dev = &register_io_dev_block;
140 boot_dev_spec.buffer.offset = STRATIX10_MMC_DATA_BASE;
141 boot_dev_spec.buffer.length = MMC_BLOCK_SIZE;
142 boot_dev_spec.ops.read = mmc_read_blocks;
143 boot_dev_spec.ops.write = mmc_write_blocks;
144 boot_dev_spec.block_size = MMC_BLOCK_SIZE;
145 break;
146
147 case BOOT_SOURCE_QSPI:
148 register_io_dev = &register_io_dev_memmap;
149 fip_spec.offset = fip_spec.offset + STRATIX10_QSPI_DATA_BASE;
150 break;
151
152 default:
153 ERROR("Unsupported boot source\n");
154 panic();
155 break;
156 }
157
158 result = (*register_io_dev)(&boot_dev_con);
Loh Tien Hock59400a42019-02-04 16:17:24 +0800159 assert(result == 0);
160
161 result = register_io_dev_fip(&fip_dev_con);
162 assert(result == 0);
163
Muhammad Hadi Asyrafi Abdul Halim2444bfa2019-03-08 19:02:33 +0800164 result = io_dev_open(boot_dev_con, (uintptr_t)&boot_dev_spec,
165 &boot_dev_handle);
Loh Tien Hock59400a42019-02-04 16:17:24 +0800166 assert(result == 0);
167
168 result = io_dev_open(fip_dev_con, (uintptr_t)NULL, &fip_dev_handle);
169 assert(result == 0);
170
Muhammad Hadi Asyrafi Abdul Halim2444bfa2019-03-08 19:02:33 +0800171 if (boot_source == BOOT_SOURCE_SDMMC) {
172 partition_init(GPT_IMAGE_ID);
173 fip_spec.offset = get_partition_entry(a2)->start;
174 }
Loh Tien Hock59400a42019-02-04 16:17:24 +0800175
176 (void)result;
177}
178
179int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
180 uintptr_t *image_spec)
181{
182 int result;
183 const struct plat_io_policy *policy;
184
185 assert(image_id < ARRAY_SIZE(policies));
186
187 policy = &policies[image_id];
188 result = policy->check(policy->image_spec);
189 assert(result == 0);
190
191 *image_spec = policy->image_spec;
192 *dev_handle = *(policy->dev_handle);
193
194 return result;
195}