1 /*
2  * Copyright (c) 2023, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include "stdint.h"
9 
10 #include "Driver_Flash.h"
11 #include "flash_layout.h"
12 
13 #include "io_driver.h"
14 #include "io_flash.h"
15 #include "io_storage.h"
16 
17 #include "platform.h"
18 
19 #define PLAT_LOG_MODULE_NAME    "platform"
20 #include "platform_log.h"
21 
22 typedef struct {
23     uintptr_t dev_handle;
24     uintptr_t image_spec;
25 } platform_image_source_t;
26 
27 extern ARM_DRIVER_FLASH FLASH_DEV_NAME;
28 
29 static io_dev_connector_t *flash_dev_con;
30 static uint8_t local_block_flash[FLASH_SECTOR_SIZE];
31 static io_flash_dev_spec_t flash_dev_spec = {
32     .buffer = local_block_flash,
33     .bufferlen = FLASH_SECTOR_SIZE,
34     .base_addr = FLASH_BASE_ADDRESS,
35     .flash_driver = &FLASH_DEV_NAME,
36 };
37 static io_block_spec_t flash_spec = {
38     .offset = FLASH_BASE_ADDRESS,
39     .length = FLASH_TOTAL_SIZE
40 };
41 
42 static platform_image_source_t platform_image_source[] = {
43     [PLATFORM_GPT_IMAGE] = {
44         .dev_handle = NULL,
45         .image_spec = &flash_spec,
46     }
47 };
48 
49 /* Initialize io storage of the platform */
plat_io_storage_init(void)50 int32_t plat_io_storage_init(void)
51 {
52     int rc = -1;
53     uintptr_t flash_dev_handle = NULL;
54     uintptr_t flash_handle = NULL;
55 
56     rc = register_io_dev_flash((const io_dev_connector_t **) &flash_dev_con);
57     if (rc != 0) {
58         ERROR("Failed to register io flash rc: %d", rc);
59         return rc;
60     }
61 
62     rc = io_dev_open(flash_dev_con, (const uintptr_t)&flash_dev_spec, &flash_dev_handle);
63     if (rc != 0) {
64         ERROR("Failed to open io flash dev rc: %d", rc);
65         return rc;
66     }
67 
68     VERBOSE("Flash_dev_handle = %p",flash_dev_handle);
69 
70     rc = io_open(flash_dev_handle, (const uintptr_t)&flash_spec, &flash_handle);
71     if (rc != 0) {
72         ERROR("Failed to open io flash rc: %d", rc);
73         return rc;
74     }
75 
76     VERBOSE("Flash_handle = %p",flash_handle);
77 
78     rc = io_close(flash_handle);
79     if (rc != 0) {
80         ERROR("Failed to close io flash rc: %d", rc);
81         return rc;
82     }
83     /* Update the platform image source that uses the flash with dev handles */
84     platform_image_source[PLATFORM_GPT_IMAGE].dev_handle = flash_dev_handle;
85 
86     return rc;
87 }
88 
89 /* Return an IO device handle and specification which can be used to access
90  * an image. This has to be implemented for the GPT parser. */
plat_get_image_source(unsigned int image_id,uintptr_t * dev_handle,uintptr_t * image_spec)91 int32_t plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
92                               uintptr_t *image_spec) {
93     if (image_id >= PLATFORM_IMAGE_COUNT) {
94         return -1;
95     }
96     *dev_handle = platform_image_source[image_id].dev_handle;
97     *image_spec = platform_image_source[image_id].image_spec;
98     return 0;
99 }
100