1 // Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // HAL for SPI Flash (non-IRAM part)
16 // The IRAM part is in spi_flash_hal_iram.c, spi_flash_hal_gpspi.c, spi_flash_hal_common.inc.
17
18 #include <stdlib.h>
19 #include "hal/spi_flash_hal.h"
20 #include "string.h"
21 #include "soc/soc_caps.h"
22 #include "hal/hal_defs.h"
23
24 #define APB_CYCLE_NS (1000*1000*1000LL/APB_CLK_FREQ)
25
26 static const char TAG[] = "FLASH_HAL";
27
28 typedef struct {
29 int div;
30 spi_flash_ll_clock_reg_t clock_reg_val;
31 } spi_flash_hal_clock_config_t;
32
33
34
35
36 static const spi_flash_hal_clock_config_t spi_flash_clk_cfg_reg[ESP_FLASH_SPEED_MAX] = {
37 {16, SPI_FLASH_LL_CLKREG_VAL_5MHZ},
38 {8, SPI_FLASH_LL_CLKREG_VAL_10MHZ},
39 {4, SPI_FLASH_LL_CLKREG_VAL_20MHZ},
40 {3, SPI_FLASH_LL_CLKREG_VAL_26MHZ},
41 {2, SPI_FLASH_LL_CLKREG_VAL_40MHZ},
42 {1, SPI_FLASH_LL_CLKREG_VAL_80MHZ},
43 };
44
45 #if !CONFIG_IDF_TARGET_ESP32
46 static const spi_flash_hal_clock_config_t spi_flash_gpspi_clk_cfg_reg[ESP_FLASH_SPEED_MAX] = {
47 {16, {.gpspi=GPSPI_FLASH_LL_CLKREG_VAL_5MHZ}},
48 {8, {.gpspi=GPSPI_FLASH_LL_CLKREG_VAL_10MHZ}},
49 {4, {.gpspi=GPSPI_FLASH_LL_CLKREG_VAL_20MHZ}},
50 {3, {.gpspi=GPSPI_FLASH_LL_CLKREG_VAL_26MHZ}},
51 {2, {.gpspi=GPSPI_FLASH_LL_CLKREG_VAL_40MHZ}},
52 {1, {.gpspi=GPSPI_FLASH_LL_CLKREG_VAL_80MHZ}},
53 };
54 #else
55 #define spi_flash_gpspi_clk_cfg_reg spi_flash_clk_cfg_reg
56 #endif
57
get_dummy_n(bool gpio_is_used,int input_delay_ns,int eff_clk)58 static inline int get_dummy_n(bool gpio_is_used, int input_delay_ns, int eff_clk)
59 {
60 const int apbclk_kHz = APB_CLK_FREQ / 1000;
61 //calculate how many apb clocks a period has
62 const int apbclk_n = APB_CLK_FREQ / eff_clk;
63 const int gpio_delay_ns = gpio_is_used ? GPIO_MATRIX_DELAY_NS : 0;
64
65 //calculate how many apb clocks the delay is, the 1 is to compensate in case ``input_delay_ns`` is rounded off.
66 int apb_period_n = (1 + input_delay_ns + gpio_delay_ns) * apbclk_kHz / 1000 / 1000;
67 if (apb_period_n < 0) {
68 apb_period_n = 0;
69 }
70
71 return apb_period_n / apbclk_n;
72 }
73
spi_flash_hal_init(spi_flash_hal_context_t * data_out,const spi_flash_hal_config_t * cfg)74 esp_err_t spi_flash_hal_init(spi_flash_hal_context_t *data_out, const spi_flash_hal_config_t *cfg)
75 {
76 if (!esp_ptr_internal(data_out) && cfg->host_id == SPI1_HOST) {
77 return ESP_ERR_INVALID_ARG;
78 }
79 if (cfg->cs_num >= SOC_SPI_PERIPH_CS_NUM(cfg->host_id)) {
80 return ESP_ERR_INVALID_ARG;
81 }
82
83 bool gpspi = (cfg->host_id > SPI1_HOST);
84 const spi_flash_hal_clock_config_t *clock_cfg = gpspi? &spi_flash_gpspi_clk_cfg_reg[cfg->speed]: &spi_flash_clk_cfg_reg[cfg->speed];
85
86 *data_out = (spi_flash_hal_context_t) {
87 .inst = data_out->inst, // Keeps the function pointer table
88 .spi = spi_flash_ll_get_hw(cfg->host_id),
89 .cs_num = cfg->cs_num,
90 .extra_dummy = get_dummy_n(!cfg->iomux, cfg->input_delay_ns, APB_CLK_FREQ/clock_cfg->div),
91 .clock_conf = clock_cfg->clock_reg_val,
92 .cs_hold = cfg->cs_hold,
93 .cs_setup = cfg->cs_setup,
94 };
95 if (cfg->auto_sus_en) {
96 data_out->flags |= SPI_FLASH_HOST_CONTEXT_FLAG_AUTO_SUSPEND;
97 data_out->flags |= SPI_FLASH_HOST_CONTEXT_FLAG_AUTO_RESUME;
98 }
99
100 ESP_EARLY_LOGD(TAG, "extra_dummy: %d", data_out->extra_dummy);
101 return ESP_OK;
102 }
103
spi_flash_hal_supports_direct_write(spi_flash_host_inst_t * host,const void * p)104 bool spi_flash_hal_supports_direct_write(spi_flash_host_inst_t *host, const void *p)
105 {
106 bool direct_write = ( ((spi_flash_hal_context_t *)host)->spi != spi_flash_ll_get_hw(SPI1_HOST)
107 || esp_ptr_in_dram(p) );
108 return direct_write;
109 }
110
111
spi_flash_hal_supports_direct_read(spi_flash_host_inst_t * host,const void * p)112 bool spi_flash_hal_supports_direct_read(spi_flash_host_inst_t *host, const void *p)
113 {
114 //currently the host doesn't support to read through dma, no word-aligned requirements
115 bool direct_read = ( ((spi_flash_hal_context_t *)host)->spi != spi_flash_ll_get_hw(SPI1_HOST)
116 || esp_ptr_in_dram(p) );
117 return direct_read;
118 }
119