1 // Copyright (C) 2022 Beken Corporation
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 <common/bk_include.h>
16 #include <os/mem.h>
17 #include <driver/efuse.h>
18 #include "efuse_driver.h"
19 #include "efuse_hal.h"
20 #include "sys_driver.h"
21
22 typedef struct {
23 efuse_hal_t hal;
24 } efuse_driver_t;
25
26 #define EFUSE_ADDR_MAX_VALUE EFUSE_F_ADDR_V
27
28 #define EFUSE_RETURN_ON_DRIVER_NOT_INIT() do {\
29 if (!s_efuse_driver_is_init) {\
30 return BK_ERR_EFUSE_DRIVER_NOT_INIT;\
31 }\
32 } while(0)
33
34 #define EFUSE_RETURN_ON_ADDR_OUT_OF_RANGE(addr) do {\
35 if ((addr) > EFUSE_ADDR_MAX_VALUE) {\
36 return BK_ERR_EFUSE_ADDR_OUT_OF_RANGE;\
37 }\
38 } while(0)
39
40 static efuse_driver_t s_efuse = {0};
41 static bool s_efuse_driver_is_init = false;
42
efuse_deinit_common(void)43 static void efuse_deinit_common(void)
44 {
45 efuse_hal_disable(&s_efuse.hal);
46 efuse_hal_reset_config_to_default(&s_efuse.hal);
47 }
48
bk_efuse_driver_init(void)49 bk_err_t bk_efuse_driver_init(void)
50 {
51 if (s_efuse_driver_is_init) {
52 return BK_OK;
53 }
54
55 os_memset(&s_efuse, 0, sizeof(s_efuse));
56 efuse_hal_init(&s_efuse.hal);
57 s_efuse_driver_is_init = true;
58 #if (CONFIG_SYSTEM_CTRL)
59 sys_drv_dev_clk_pwr_up(CLK_PWR_ID_EFUSE, CLK_PWR_CTRL_PWR_UP);
60 #endif
61
62 return BK_OK;
63 }
64
bk_efuse_driver_deinit(void)65 bk_err_t bk_efuse_driver_deinit(void)
66 {
67 if (!s_efuse_driver_is_init) {
68 return BK_OK;
69 }
70
71 #if (CONFIG_SYSTEM_CTRL)
72 sys_drv_dev_clk_pwr_up(CLK_PWR_ID_EFUSE, CLK_PWR_CTRL_PWR_DOWN);
73 #endif
74 efuse_deinit_common();
75 s_efuse_driver_is_init = false;
76
77 return BK_OK;
78 }
79
bk_efuse_write_byte(uint8_t addr,uint8_t data)80 bk_err_t bk_efuse_write_byte(uint8_t addr, uint8_t data)
81 {
82 EFUSE_RETURN_ON_DRIVER_NOT_INIT();
83 EFUSE_RETURN_ON_ADDR_OUT_OF_RANGE(addr);
84
85 return efuse_hal_write(&s_efuse.hal, addr, data);
86 }
87
bk_efuse_read_byte(uint8_t addr,uint8_t * data)88 bk_err_t bk_efuse_read_byte(uint8_t addr, uint8_t *data)
89 {
90 EFUSE_RETURN_ON_DRIVER_NOT_INIT();
91 EFUSE_RETURN_ON_ADDR_OUT_OF_RANGE(addr);
92
93 *data = efuse_hal_read(&s_efuse.hal, addr);
94 return BK_OK;
95 }
96
97