1 // Copyright 2020 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 #include "esp_efuse.h"
16 #include "esp_efuse_utility.h"
17 #include "soc/efuse_periph.h"
18 #include "assert.h"
19 #include "sdkconfig.h"
20 #include "esp_efuse_table.h"
21 
22 const static char *TAG = "efuse";
23 
24 // Sets a write protection for the whole block.
esp_efuse_set_write_protect(esp_efuse_block_t blk)25 esp_err_t esp_efuse_set_write_protect(esp_efuse_block_t blk)
26 {
27     if (blk == EFUSE_BLK1) {
28        return esp_efuse_write_field_cnt(ESP_EFUSE_WR_DIS_BLK1, 1);
29     } else if (blk == EFUSE_BLK2) {
30        return esp_efuse_write_field_cnt(ESP_EFUSE_WR_DIS_SYS_DATA_PART1, 1);
31     } else if (blk == EFUSE_BLK3) {
32        return esp_efuse_write_field_cnt(ESP_EFUSE_WR_DIS_USER_DATA, 1);
33     } else if (blk == EFUSE_BLK4) {
34         return esp_efuse_write_field_cnt(ESP_EFUSE_WR_DIS_KEY0, 1);
35     } else if (blk == EFUSE_BLK5) {
36         return esp_efuse_write_field_cnt(ESP_EFUSE_WR_DIS_KEY1, 1);
37     } else if (blk == EFUSE_BLK6) {
38         return esp_efuse_write_field_cnt(ESP_EFUSE_WR_DIS_KEY2, 1);
39     } else if (blk == EFUSE_BLK7) {
40         return esp_efuse_write_field_cnt(ESP_EFUSE_WR_DIS_KEY3, 1);
41     } else if (blk == EFUSE_BLK8) {
42         return esp_efuse_write_field_cnt(ESP_EFUSE_WR_DIS_KEY4, 1);
43     } else if (blk == EFUSE_BLK9) {
44         return esp_efuse_write_field_cnt(ESP_EFUSE_WR_DIS_KEY5, 1);
45     } else if (blk == EFUSE_BLK10) {
46         return esp_efuse_write_field_cnt(ESP_EFUSE_WR_DIS_SYS_DATA_PART2, 1);
47     }
48     return ESP_ERR_NOT_SUPPORTED;
49 }
50 
51 // read protect for blk.
esp_efuse_set_read_protect(esp_efuse_block_t blk)52 esp_err_t esp_efuse_set_read_protect(esp_efuse_block_t blk)
53 {
54     if (blk == EFUSE_BLK4) {
55         return esp_efuse_write_field_cnt(ESP_EFUSE_RD_DIS_KEY0, 1);
56     } else if (blk == EFUSE_BLK5) {
57         return esp_efuse_write_field_cnt(ESP_EFUSE_RD_DIS_KEY1, 1);
58     } else if (blk == EFUSE_BLK6) {
59         return esp_efuse_write_field_cnt(ESP_EFUSE_RD_DIS_KEY2, 1);
60     } else if (blk == EFUSE_BLK7) {
61         return esp_efuse_write_field_cnt(ESP_EFUSE_RD_DIS_KEY3, 1);
62     } else if (blk == EFUSE_BLK8) {
63         return esp_efuse_write_field_cnt(ESP_EFUSE_RD_DIS_KEY4, 1);
64     } else if (blk == EFUSE_BLK9) {
65         return esp_efuse_write_field_cnt(ESP_EFUSE_RD_DIS_KEY5, 1);
66     } else if (blk == EFUSE_BLK10) {
67         return esp_efuse_write_field_cnt(ESP_EFUSE_RD_DIS_SYS_DATA_PART2, 1);
68     }
69     return ESP_ERR_NOT_SUPPORTED;
70 }
71 
72 // get efuse coding_scheme.
esp_efuse_get_coding_scheme(esp_efuse_block_t blk)73 esp_efuse_coding_scheme_t esp_efuse_get_coding_scheme(esp_efuse_block_t blk)
74 {
75     esp_efuse_coding_scheme_t scheme;
76     if (blk == EFUSE_BLK0) {
77         scheme = EFUSE_CODING_SCHEME_NONE;
78     } else {
79         scheme = EFUSE_CODING_SCHEME_RS;
80     }
81     ESP_EARLY_LOGD(TAG, "coding scheme %d", scheme);
82     return scheme;
83 }
84