Skip to content

Commit fc70273

Browse files
Matthew Macovskydustin-crossman
authored andcommitted
Create canonical CyReservedRegionBlockDevice for working with/around Cypress WiFi firmware in external memory
This change allows external memory to be used for other purposes while the WiFi firmware is stored in it by interacting with it via the reserved region block device.
1 parent 95a85a5 commit fc70273

File tree

4 files changed

+525
-0
lines changed

4 files changed

+525
-0
lines changed

connectivity/drivers/emac/TARGET_Cypress/COMPONENT_WHD/interface/whd_emac.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@
2727
#include "events/mbed_shared_queues.h"
2828
#include "whd_wlioctl.h"
2929
#include "whd_buffer_api.h"
30+
#include "wiced_resource.h"
3031
#include "cybsp_wifi.h"
3132
#include "emac_eapol.h"
3233
#include "cy_result.h"
3334

35+
#if defined(CY_EXT_WIFI_FW_STORAGE) && !MBED_CONF_TARGET_XIP_ENABLE
36+
#include "cy_ext_wifi_fw_reserved_region_bd.h"
37+
#endif /* defined(CY_EXT_WIFI_FW_STORAGE) && !MBED_CONF_TARGET_XIP_ENABLE */
38+
3439
#define NULL_MAC(a) ( ( ( ( (unsigned char *)a )[0] ) == 0 ) && \
3540
( ( ( (unsigned char *)a )[1] ) == 0 ) && \
3641
( ( ( (unsigned char *)a )[2] ) == 0 ) && \
@@ -112,7 +117,17 @@ bool WHD_EMAC::power_up()
112117
WHD_EMAC &emac_other = WHD_EMAC::get_instance(interface_type == WHD_STA_ROLE ? WHD_AP_ROLE :
113118
WHD_STA_ROLE);
114119
if (!emac_other.powered_up) {
120+
#if defined(CY_EXT_WIFI_FW_STORAGE) && !MBED_CONF_TARGET_XIP_ENABLE
121+
CyReservedRegionBlockDevice *reserved_region_bd = cy_get_ext_wifi_fw_reserved_region_bd();
122+
reserved_region_bd->init();
123+
124+
extern whd_resource_source_t cy_ext_wifi_fw_resource_ops;
125+
res = cybsp_wifi_init_primary_extended(&ifp /* OUT */, &cy_ext_wifi_fw_resource_ops, NULL, NULL);
126+
127+
reserved_region_bd->deinit();
128+
#else
115129
res = cybsp_wifi_init_primary(&ifp /* OUT */);
130+
#endif /* defined(CY_EXT_WIFI_FW_STORAGE) && !MBED_CONF_TARGET_XIP_ENABLE */
116131
} else {
117132
ifp = emac_other.ifp;
118133
}
@@ -295,4 +310,13 @@ extern "C"
295310
}
296311
}
297312

313+
#if defined(CY_EXT_WIFI_FW_STORAGE) && !MBED_CONF_TARGET_XIP_ENABLE
314+
resource_result_t platform_read_external_resource(const resource_hnd_t *resource, uint32_t offset, uint32_t maxsize, uint32_t *size, void *buffer)
315+
{
316+
CyReservedRegionBlockDevice *bd = cy_get_ext_wifi_fw_reserved_region_bd();
317+
int status = bd->reserved_read(buffer, (mbed::bd_addr_t) resource->val.external_storage_context + offset, *size);
318+
return (status == 0) ? RESOURCE_SUCCESS : RESOURCE_FILE_READ_FAIL;
319+
}
320+
#endif /* defined(CY_EXT_WIFI_FW_STORAGE) && !MBED_CONF_TARGET_XIP_ENABLE */
321+
298322
} // extern "C"
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/***************************************************************************//**
2+
* \file cy_ext_wifi_fw_reserved_region_bd.cpp
3+
*
4+
* \brief
5+
* Block device use to interact with external memory without interfering with
6+
* the region reserved for the WiFi firmware.
7+
*
8+
* Creates a canonical instance that can be accessed via cy_get_ext_wifi_fw_reserved_region_bd().
9+
* Provides overrides for Mbed OS functions so that storage mechanisms such as KVStore
10+
* will default to using this block device.
11+
*
12+
********************************************************************************
13+
* \copyright
14+
* Copyright 2020 Cypress Semiconductor Corporation
15+
* SPDX-License-Identifier: Apache-2.0
16+
*
17+
* Licensed under the Apache License, Version 2.0 (the "License");
18+
* you may not use this file except in compliance with the License.
19+
* You may obtain a copy of the License at
20+
*
21+
* http://www.apache.org/licenses/LICENSE-2.0
22+
*
23+
* Unless required by applicable law or agreed to in writing, software
24+
* distributed under the License is distributed on an "AS IS" BASIS,
25+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26+
* See the License for the specific language governing permissions and
27+
* limitations under the License.
28+
*******************************************************************************/
29+
30+
// Use of xip-enable by user overrides definition of CY_EXT_WIFI_FW_STORAGE in target configuration
31+
#if defined(CY_EXT_WIFI_FW_STORAGE) && !MBED_CONF_TARGET_XIP_ENABLE
32+
33+
#include "cy_ext_wifi_fw_reserved_region_bd.h"
34+
#include "cy_wifi_fw_section.h"
35+
#include "QSPIFBlockDevice.h"
36+
37+
BlockDevice *cy_get_ext_wifi_fw_reserved_region_underlying_bd()
38+
{
39+
#if CY_EXT_WIFI_FW_STORAGE == QSPIF
40+
static QSPIFBlockDevice underlying_bd;
41+
#else
42+
#error "Invalid WiFi storage type"
43+
#endif
44+
45+
return &underlying_bd;
46+
}
47+
48+
CyReservedRegionBlockDevice *cy_get_ext_wifi_fw_reserved_region_bd()
49+
{
50+
BlockDevice *underlying_bd = cy_get_ext_wifi_fw_reserved_region_underlying_bd();
51+
static CyReservedRegionBlockDevice default_instance(underlying_bd, (mbed::bd_addr_t) CY_WIFI_FW_SECTION_END - (mbed::bd_addr_t) CY_WIFI_FW_SECTION_START);
52+
return &default_instance;
53+
}
54+
55+
extern "C" {
56+
extern void cy_ext_wifi_fw_resources_update_handles(void *image_addr, unsigned long image_size, void *clm_blob_addr, unsigned long clm_blob_size);
57+
}
58+
59+
int cy_update_ext_wifi_fw_location_and_size(mbed::bd_addr_t image_addr, mbed::bd_size_t image_size, mbed::bd_addr_t clm_blob_addr, mbed::bd_size_t clm_blob_size)
60+
{
61+
CyReservedRegionBlockDevice *bd = cy_get_ext_wifi_fw_reserved_region_bd();
62+
// If the reserved region end is NULL, the block device hasn't been initialized yet, so we can't perform this check
63+
if ((bd->reserved_region_end() != 0) && (image_addr + image_size > bd->reserved_region_end() || clm_blob_addr + clm_blob_size > bd->reserved_region_end()))
64+
{
65+
// Should not exceed originally computed reserved region size, as this will break anything that was using the rest of external storage
66+
return -1;
67+
}
68+
69+
// These addresses are not valid pointers, but are converted to pointers so as to be used with the WHD resource handles
70+
cy_ext_wifi_fw_resources_update_handles((void *) image_addr, image_size, (void *) clm_blob_addr, clm_blob_size);
71+
72+
return 0;
73+
}
74+
75+
//
76+
// Overrides for other Mbed OS storage mechanisms to that this is the default block device
77+
//
78+
79+
BlockDevice *get_other_blockdevice()
80+
{
81+
return cy_get_ext_wifi_fw_reserved_region_bd();
82+
}
83+
84+
BlockDevice *BlockDevice::get_default_instance()
85+
{
86+
return cy_get_ext_wifi_fw_reserved_region_bd();
87+
}
88+
89+
#endif /* defined(CY_EXT_WIFI_FW_STORAGE) && !MBED_CONF_TARGET_XIP_ENABLE */
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/***************************************************************************//**
2+
* \file cy_ext_wifi_fw_reserved_region_bd.h
3+
*
4+
* \brief
5+
* Block device use to interact with external memory without interfering with
6+
* the region reserved for the WiFi firmware.
7+
*
8+
* Creates a canonical instance that can be accessed via cy_get_ext_wifi_fw_reserved_region_bd().
9+
* Provides overrides for Mbed OS functions so that storage mechanisms such as KVStore
10+
* will default to using this block device.
11+
*
12+
* See cy_get_ext_wifi_fw_reserved_region_bd() for more details.
13+
*
14+
********************************************************************************
15+
* \copyright
16+
* Copyright 2020 Cypress Semiconductor Corporation
17+
* SPDX-License-Identifier: Apache-2.0
18+
*
19+
* Licensed under the Apache License, Version 2.0 (the "License");
20+
* you may not use this file except in compliance with the License.
21+
* You may obtain a copy of the License at
22+
*
23+
* http://www.apache.org/licenses/LICENSE-2.0
24+
*
25+
* Unless required by applicable law or agreed to in writing, software
26+
* distributed under the License is distributed on an "AS IS" BASIS,
27+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28+
* See the License for the specific language governing permissions and
29+
* limitations under the License.
30+
*******************************************************************************/
31+
32+
#ifndef CY_EXT_WIFI_FW_RESERVED_REGION_BD_H
33+
#define CY_EXT_WIFI_FW_RESERVED_REGION_BD_H
34+
35+
// Use of xip-enable by user overrides definition of CY_EXT_WIFI_FW_STORAGE in target configuration
36+
#if defined(CY_EXT_WIFI_FW_STORAGE) && !MBED_CONF_TARGET_XIP_ENABLE
37+
38+
#include "CyReservedRegionBlockDevice.h"
39+
40+
/**
41+
* Get the canonical instance of the underlying block device used by the canonical
42+
* CyReservedRegionBlockDevice instance (retrieved via cy_get_ext_wifi_fw_reserved_region_bd()).
43+
* Uses the macro CY_EXT_WIFI_FW_STORAGE to determine the type of the underlying block device.
44+
* Valid values of CY_EXT_WIFI_FW_STORAGE are: QSPIF.
45+
*
46+
* @return A pointer to the canonical instance of the underlying block device
47+
*/
48+
BlockDevice *cy_get_ext_wifi_fw_reserved_region_underlying_bd();
49+
50+
/**
51+
* Get the canonical instance of the block device used for interacting with external memory
52+
* without interfering with the region reserved for the WiFi firmware.
53+
*
54+
* This block device and the overrides that use it will only be defined if the macro CY_EXT_WIFI_FW_STORAGE is defined.
55+
* If it is, then its value is used by to determine the default block device for accessing
56+
* the external storage where the WiFi firmware is located.
57+
* See cy_get_ext_wifi_fw_reserved_region_underlying_bd() for more details.
58+
*
59+
* In order to store the WiFi firmware in external storage at all, the macro CY_ENABLE_XIP_PROGRAM must be defined.
60+
* Further, the macro CY_STORAGE_WIFI_DATA must be set to the name of the input section where the WiFi firmware is
61+
* to be placed, with surrounding quotation marks. The macro CY_STORAGE_WIFI_DATA_OUTPUT must be set to the name of the
62+
* output section that the linker script places the input section in, without quotation marks. The name of this output
63+
* section must not contain a '.' character.
64+
*
65+
* Using XIP by adding the xip-enable configuration override will prevent any of this from begin defined.
66+
*
67+
* @return A pointer to the canonical instance of the block device
68+
*/
69+
CyReservedRegionBlockDevice *cy_get_ext_wifi_fw_reserved_region_bd();
70+
71+
/**
72+
* Modify the location and size of the WiFi firmware image/CLM blob.
73+
*
74+
* If the WiFi firmware is ever updated, this function must be called afterward as well as
75+
* on every startup as long as the updated firmware continues to be used.
76+
* The defaults are the initial locations and sizes on compilation.
77+
* New firmware end should not exceed the initial end of the WiFi firmware output section
78+
* so that storage mechanisms keep a consistent storage region.
79+
*
80+
* @param image_addr[in] New address of the WiFi firmware image
81+
* @param image_size[in] New size of the WiFi firmware image
82+
* @param clm_blob_addr[in] New address of the CLM blob
83+
* @param clm_blob_size[in] New size of the CLM blob
84+
* @return 0 on success, negative on failure
85+
*/
86+
int cy_update_ext_wifi_fw_location_and_size(mbed::bd_addr_t image_addr, mbed::bd_size_t image_size, mbed::bd_addr_t clm_blob_addr, mbed::bd_size_t clm_blob_size);
87+
88+
#endif /* defined(CY_EXT_WIFI_FW_STORAGE) && !MBED_CONF_TARGET_XIP_ENABLE */
89+
90+
#endif /* CY_EXT_WIFI_FW_RESERVED_REGION_BD_H */

0 commit comments

Comments
 (0)