2019-08-07 23:44:24 -04:00
|
|
|
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
|
2019-07-05 08:21:36 -04:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include "string.h"
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_log.h"
|
2020-01-16 22:47:08 -05:00
|
|
|
#include "esp32s2/rom/spi_flash.h"
|
2019-07-05 08:21:36 -04:00
|
|
|
#include "soc/efuse_reg.h"
|
|
|
|
#include "soc/spi_reg.h"
|
2019-08-08 00:00:45 -04:00
|
|
|
#include "soc/spi_mem_reg.h"
|
2019-07-05 08:21:36 -04:00
|
|
|
#include "soc/spi_caps.h"
|
|
|
|
#include "flash_qio_mode.h"
|
|
|
|
#include "bootloader_flash_config.h"
|
2019-08-08 02:21:34 -04:00
|
|
|
#include "bootloader_common.h"
|
|
|
|
|
|
|
|
#define FLASH_IO_MATRIX_DUMMY_40M 0
|
|
|
|
#define FLASH_IO_MATRIX_DUMMY_80M 0
|
|
|
|
|
|
|
|
#define FLASH_IO_DRIVE_GD_WITH_1V8PSRAM 3
|
2019-07-05 08:21:36 -04:00
|
|
|
|
|
|
|
void bootloader_flash_update_id()
|
|
|
|
{
|
|
|
|
g_rom_flashchip.device_id = bootloader_read_flash_id();
|
|
|
|
}
|
|
|
|
|
2020-08-23 23:09:33 -04:00
|
|
|
void bootloader_flash_update_size(uint32_t size)
|
|
|
|
{
|
|
|
|
g_rom_flashchip.chip_size = size;
|
|
|
|
}
|
|
|
|
|
2019-07-05 08:21:36 -04:00
|
|
|
void IRAM_ATTR bootloader_flash_cs_timing_config()
|
|
|
|
{
|
2020-01-10 06:31:30 -05:00
|
|
|
SET_PERI_REG_MASK(SPI_MEM_USER_REG(0), SPI_MEM_CS_HOLD_M | SPI_MEM_CS_SETUP_M);
|
|
|
|
SET_PERI_REG_BITS(SPI_MEM_CTRL2_REG(0), SPI_MEM_CS_HOLD_TIME_V, 0, SPI_MEM_CS_HOLD_TIME_S);
|
|
|
|
SET_PERI_REG_BITS(SPI_MEM_CTRL2_REG(0), SPI_MEM_CS_SETUP_TIME_V, 0, SPI_MEM_CS_SETUP_TIME_S);
|
|
|
|
SET_PERI_REG_MASK(SPI_MEM_USER_REG(1), SPI_MEM_CS_HOLD_M | SPI_MEM_CS_SETUP_M);
|
|
|
|
SET_PERI_REG_BITS(SPI_MEM_CTRL2_REG(1), SPI_MEM_CS_HOLD_TIME_V, 1, SPI_MEM_CS_HOLD_TIME_S);
|
|
|
|
SET_PERI_REG_BITS(SPI_MEM_CTRL2_REG(1), SPI_MEM_CS_SETUP_TIME_V, 0, SPI_MEM_CS_SETUP_TIME_S);
|
2019-07-05 08:21:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void IRAM_ATTR bootloader_flash_clock_config(const esp_image_header_t* pfhdr)
|
|
|
|
{
|
|
|
|
uint32_t spi_clk_div = 0;
|
|
|
|
switch (pfhdr->spi_speed) {
|
|
|
|
case ESP_IMAGE_SPI_SPEED_80M:
|
|
|
|
spi_clk_div = 1;
|
|
|
|
break;
|
|
|
|
case ESP_IMAGE_SPI_SPEED_40M:
|
|
|
|
spi_clk_div = 2;
|
|
|
|
break;
|
|
|
|
case ESP_IMAGE_SPI_SPEED_26M:
|
|
|
|
spi_clk_div = 3;
|
|
|
|
break;
|
|
|
|
case ESP_IMAGE_SPI_SPEED_20M:
|
|
|
|
spi_clk_div = 4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
esp_rom_spiflash_config_clk(spi_clk_div, 0);
|
2020-09-25 22:24:26 -04:00
|
|
|
esp_rom_spiflash_config_clk(spi_clk_div, 1);
|
2019-07-05 08:21:36 -04:00
|
|
|
}
|
|
|
|
|
2020-01-10 06:31:30 -05:00
|
|
|
void IRAM_ATTR bootloader_flash_set_dummy_out(void)
|
2019-07-05 08:21:36 -04:00
|
|
|
{
|
2020-01-10 06:31:30 -05:00
|
|
|
REG_SET_BIT(SPI_MEM_CTRL_REG(0), SPI_MEM_FDUMMY_OUT | SPI_MEM_D_POL | SPI_MEM_Q_POL);
|
|
|
|
REG_SET_BIT(SPI_MEM_CTRL_REG(1), SPI_MEM_FDUMMY_OUT | SPI_MEM_D_POL | SPI_MEM_Q_POL);
|
|
|
|
}
|
2019-07-05 08:21:36 -04:00
|
|
|
|
2020-01-10 06:31:30 -05:00
|
|
|
void IRAM_ATTR bootloader_flash_dummy_config(const esp_image_header_t* pfhdr)
|
|
|
|
{
|
|
|
|
bootloader_configure_spi_pins(1);
|
|
|
|
bootloader_flash_set_dummy_out();
|
2019-08-07 23:44:24 -04:00
|
|
|
}
|