blob: cedcf1ee1c1bd3c04166ec9775eaa2970d5d81e4 [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"
24
25#define STRATIX10_FIP_BASE (0)
26#define STRATIX10_FIP_MAX_SIZE (0x1000000)
27#define STRATIX10_MMC_DATA_BASE (0xffe3c000)
28#define STRATIX10_MMC_DATA_SIZE (0x2000)
29
30static const io_dev_connector_t *mmc_dev_con;
31static const io_dev_connector_t *fip_dev_con;
32
33static uintptr_t fip_dev_handle;
34static uintptr_t mmc_dev_handle;
35
36static const io_uuid_spec_t bl2_uuid_spec = {
37 .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
38};
39
40static const io_uuid_spec_t bl31_uuid_spec = {
41 .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
42};
43
44static const io_uuid_spec_t bl33_uuid_spec = {
45 .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
46};
47
48uintptr_t a2_lba_offset;
49
50static const io_block_spec_t gpt_block_spec = {
51 .offset = 0,
52 .length = MMC_BLOCK_SIZE
53};
54
55static int check_mmc(const uintptr_t spec);
56static int check_fip(const uintptr_t spec);
57
58static io_block_spec_t mmc_fip_spec = {
59 .offset = STRATIX10_FIP_BASE,
60 .length = STRATIX10_FIP_MAX_SIZE,
61};
62
63const char a2[] = {0xa2, 0x0};
64
65static const io_block_dev_spec_t mmc_dev_spec = {
66 .buffer = {
67 .offset = STRATIX10_MMC_DATA_BASE,
68 .length = MMC_BLOCK_SIZE,
69 },
70
71 .ops = {
72 .read = mmc_read_blocks,
73 .write = mmc_write_blocks,
74 },
75
76 .block_size = MMC_BLOCK_SIZE,
77};
78
79struct plat_io_policy {
80 uintptr_t *dev_handle;
81 uintptr_t image_spec;
82 int (*check)(const uintptr_t spec);
83};
84
85static const struct plat_io_policy policies[] = {
86 [FIP_IMAGE_ID] = {
87 &mmc_dev_handle,
88 (uintptr_t)&mmc_fip_spec,
89 check_mmc
90 },
91 [BL2_IMAGE_ID] = {
92 &fip_dev_handle,
93 (uintptr_t)&bl2_uuid_spec,
94 check_fip
95 },
96 [BL31_IMAGE_ID] = {
97 &fip_dev_handle,
98 (uintptr_t)&bl31_uuid_spec,
99 check_fip
100 },
101 [BL33_IMAGE_ID] = {
102 &fip_dev_handle,
103 (uintptr_t) &bl33_uuid_spec,
104 check_fip
105 },
106 [GPT_IMAGE_ID] = {
107 &mmc_dev_handle,
108 (uintptr_t) &gpt_block_spec,
109 check_mmc
110 },
111};
112
113static int check_mmc(const uintptr_t spec)
114{
115 int result;
116 uintptr_t local_handle;
117
118 result = io_dev_init(mmc_dev_handle, (uintptr_t)NULL);
119 if (result == 0) {
120 result = io_open(mmc_dev_handle, spec, &local_handle);
121 if (result == 0)
122 io_close(local_handle);
123 }
124 return result;
125}
126
127static int check_fip(const uintptr_t spec)
128{
129 int result;
130 uintptr_t local_image_handle;
131
132 result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
133 if (result == 0) {
134 result = io_open(fip_dev_handle, spec, &local_image_handle);
135 if (result == 0)
136 io_close(local_image_handle);
137 }
138 return result;
139}
140
141void stratix10_io_setup(void)
142{
143 int result;
144
145 result = register_io_dev_block(&mmc_dev_con);
146 assert(result == 0);
147
148 result = register_io_dev_fip(&fip_dev_con);
149 assert(result == 0);
150
151 result = io_dev_open(mmc_dev_con, (uintptr_t)&mmc_dev_spec,
152 &mmc_dev_handle);
153 assert(result == 0);
154
155 result = io_dev_open(fip_dev_con, (uintptr_t)NULL, &fip_dev_handle);
156 assert(result == 0);
157
158 partition_init(GPT_IMAGE_ID);
159
160 mmc_fip_spec.offset = get_partition_entry(a2)->start;
161
162 (void)result;
163}
164
165int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
166 uintptr_t *image_spec)
167{
168 int result;
169 const struct plat_io_policy *policy;
170
171 assert(image_id < ARRAY_SIZE(policies));
172
173 policy = &policies[image_id];
174 result = policy->check(policy->image_spec);
175 assert(result == 0);
176
177 *image_spec = policy->image_spec;
178 *dev_handle = *(policy->dev_handle);
179
180 return result;
181}