1 /*
2  * Copyright (c) 2017-2019, STMicroelectronics - All Rights Reserved
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef BSEC_H
8 #define BSEC_H
9 
10 #include <stdbool.h>
11 #include <stdint.h>
12 
13 #include <lib/utils_def.h>
14 
15 /*
16  * IP configuration
17  */
18 #define BSEC_OTP_MASK			GENMASK(4, 0)
19 #define BSEC_OTP_BANK_SHIFT		5
20 #define BSEC_TIMEOUT_VALUE		0xFFFF
21 
22 #define ADDR_LOWER_OTP_PERLOCK_SHIFT	0x03
23 #define DATA_LOWER_OTP_PERLOCK_BIT	0x03U /* 2 significants bits are used */
24 #define DATA_LOWER_OTP_PERLOCK_MASK	GENMASK(2, 0)
25 #define ADDR_UPPER_OTP_PERLOCK_SHIFT	0x04
26 #define DATA_UPPER_OTP_PERLOCK_BIT	0x01U /* 1 significants bits are used */
27 #define DATA_UPPER_OTP_PERLOCK_MASK	GENMASK(3, 0)
28 
29 /*
30  * Return status
31  */
32 #define BSEC_OK				0U
33 #define BSEC_ERROR			0xFFFFFFFFU
34 #define BSEC_DISTURBED			0xFFFFFFFEU
35 #define BSEC_INVALID_PARAM		0xFFFFFFFCU
36 #define BSEC_PROG_FAIL			0xFFFFFFFBU
37 #define BSEC_LOCK_FAIL			0xFFFFFFFAU
38 #define BSEC_WRITE_FAIL			0xFFFFFFF9U
39 #define BSEC_SHADOW_FAIL		0xFFFFFFF8U
40 #define BSEC_TIMEOUT			0xFFFFFFF7U
41 
42 /*
43  * BSEC REGISTER OFFSET (base relative)
44  */
45 #define BSEC_OTP_CONF_OFF		0x000U
46 #define BSEC_OTP_CTRL_OFF		0x004U
47 #define BSEC_OTP_WRDATA_OFF		0x008U
48 #define BSEC_OTP_STATUS_OFF		0x00CU
49 #define BSEC_OTP_LOCK_OFF		0x010U
50 #define BSEC_DEN_OFF			0x014U
51 #define BSEC_DISTURBED_OFF		0x01CU
52 #define BSEC_DISTURBED1_OFF		0x020U
53 #define BSEC_DISTURBED2_OFF		0x024U
54 #define BSEC_ERROR_OFF			0x034U
55 #define BSEC_ERROR1_OFF			0x038U
56 #define BSEC_ERROR2_OFF			0x03CU
57 #define BSEC_WRLOCK_OFF			0x04CU /* Safmem permanent lock */
58 #define BSEC_WRLOCK1_OFF		0x050U
59 #define BSEC_WRLOCK2_OFF		0x054U
60 #define BSEC_SPLOCK_OFF			0x064U /* Program safmem sticky lock */
61 #define BSEC_SPLOCK1_OFF		0x068U
62 #define BSEC_SPLOCK2_OFF		0x06CU
63 #define BSEC_SWLOCK_OFF			0x07CU /* Write in OTP sticky lock */
64 #define BSEC_SWLOCK1_OFF		0x080U
65 #define BSEC_SWLOCK2_OFF		0x084U
66 #define BSEC_SRLOCK_OFF			0x094U /* Shadowing sticky lock */
67 #define BSEC_SRLOCK1_OFF		0x098U
68 #define BSEC_SRLOCK2_OFF		0x09CU
69 #define BSEC_JTAG_IN_OFF		0x0ACU
70 #define BSEC_JTAG_OUT_OFF		0x0B0U
71 #define BSEC_SCRATCH_OFF		0x0B4U
72 #define BSEC_OTP_DATA_OFF		0x200U
73 #define BSEC_IPHW_CFG_OFF		0xFF0U
74 #define BSEC_IPVR_OFF			0xFF4U
75 #define BSEC_IP_ID_OFF			0xFF8U
76 #define BSEC_IP_MAGIC_ID_OFF		0xFFCU
77 
78 /*
79  * BSEC_CONFIGURATION Register
80  */
81 #define BSEC_CONF_POWER_UP_MASK		BIT(0)
82 #define BSEC_CONF_POWER_UP_SHIFT	0
83 #define BSEC_CONF_FRQ_MASK		GENMASK(2, 1)
84 #define BSEC_CONF_FRQ_SHIFT		1
85 #define BSEC_CONF_PRG_WIDTH_MASK	GENMASK(6, 3)
86 #define BSEC_CONF_PRG_WIDTH_SHIFT	3
87 #define BSEC_CONF_TREAD_MASK		GENMASK(8, 7)
88 #define BSEC_CONF_TREAD_SHIFT		7
89 
90 /*
91  * BSEC_CONTROL Register
92  */
93 #define BSEC_READ			0x000U
94 #define BSEC_WRITE			0x100U
95 #define BSEC_LOCK			0x200U
96 
97 /*
98  * BSEC_OTP_LOCK register
99  */
100 #define UPPER_OTP_LOCK_MASK		BIT(0)
101 #define UPPER_OTP_LOCK_SHIFT		0
102 #define DENREG_LOCK_MASK		BIT(2)
103 #define DENREG_LOCK_SHIFT		2
104 #define GPLOCK_LOCK_MASK		BIT(4)
105 #define GPLOCK_LOCK_SHIFT		4
106 
107 /*
108  * BSEC_OTP_STATUS Register
109  */
110 #define BSEC_MODE_STATUS_MASK		GENMASK(2, 0)
111 #define BSEC_MODE_BUSY_MASK		BIT(3)
112 #define BSEC_MODE_PROGFAIL_MASK		BIT(4)
113 #define BSEC_MODE_PWR_MASK		BIT(5)
114 #define BSEC_MODE_BIST1_LOCK_MASK	BIT(6)
115 #define BSEC_MODE_BIST2_LOCK_MASK	BIT(7)
116 
117 /* OTP MODE*/
118 #define BSEC_MODE_OPEN1			0x00
119 #define BSEC_MODE_SECURED		0x01
120 #define BSEC_MODE_OPEN2			0x02
121 #define BSEC_MODE_INVALID		0x04
122 
123 /* BSEC_DENABLE Register */
124 #define BSEC_HDPEN			BIT(4)
125 #define BSEC_SPIDEN			BIT(5)
126 #define BSEC_SPINDEN			BIT(6)
127 #define BSEC_DBGSWGEN			BIT(10)
128 #define BSEC_DEN_ALL_MSK		GENMASK(10, 0)
129 
130 /* BSEC_FENABLE Register */
131 #define BSEC_FEN_ALL_MSK		GENMASK(14, 0)
132 
133 /*
134  * OTP Lock services definition
135  * Value must corresponding to the bit number in the register
136  */
137 #define BSEC_LOCK_UPPER_OTP		0x00
138 #define BSEC_LOCK_DEBUG			0x02
139 #define BSEC_LOCK_PROGRAM		0x03
140 
141 /* Values for struct bsec_config::freq */
142 #define FREQ_10_20_MHZ			0x0
143 #define FREQ_20_30_MHZ			0x1
144 #define FREQ_30_45_MHZ			0x2
145 #define FREQ_45_67_MHZ			0x3
146 
147 /*
148  * Device info structure, providing device-specific functions and a means of
149  * adding driver-specific state
150  */
151 struct bsec_config {
152 	uint8_t tread;		/* SAFMEM Reading current level default 0 */
153 	uint8_t pulse_width;	/* SAFMEM Programming pulse width default 1 */
154 	uint8_t freq;		/* SAFMEM CLOCK see freq value define
155 				 * default FREQ_45_67_MHZ
156 				 */
157 	uint8_t power;		/* Power up SAFMEM. 1 power up, 0 power off */
158 	uint8_t prog_lock;	/* Programming Sticky lock
159 				 * 1 programming is locked until next reset
160 				 */
161 	uint8_t den_lock;	/* Debug enable sticky lock
162 				 * 1 debug enable is locked until next reset
163 				 */
164 	uint8_t upper_otp_lock;	/* Shadowing of upper OTP sticky lock
165 				 * 1 shadowing of upper OTP is locked
166 				 * until next reset
167 				 */
168 };
169 
170 uint32_t bsec_probe(void);
171 uint32_t bsec_get_base(void);
172 
173 uint32_t bsec_set_config(struct bsec_config *cfg);
174 uint32_t bsec_get_config(struct bsec_config *cfg);
175 
176 uint32_t bsec_shadow_register(uint32_t otp);
177 uint32_t bsec_read_otp(uint32_t *val, uint32_t otp);
178 uint32_t bsec_write_otp(uint32_t val, uint32_t otp);
179 uint32_t bsec_program_otp(uint32_t val, uint32_t otp);
180 uint32_t bsec_permanent_lock_otp(uint32_t otp);
181 
182 uint32_t bsec_write_debug_conf(uint32_t val);
183 uint32_t bsec_read_debug_conf(void);
184 uint32_t bsec_write_feature_conf(uint32_t val);
185 uint32_t bsec_read_feature_conf(uint32_t *val);
186 
187 uint32_t bsec_get_status(void);
188 uint32_t bsec_get_hw_conf(void);
189 uint32_t bsec_get_version(void);
190 uint32_t bsec_get_id(void);
191 uint32_t bsec_get_magic_id(void);
192 
193 bool bsec_write_sr_lock(uint32_t otp, uint32_t value);
194 bool bsec_read_sr_lock(uint32_t otp);
195 bool bsec_write_sw_lock(uint32_t otp, uint32_t value);
196 bool bsec_read_sw_lock(uint32_t otp);
197 bool bsec_write_sp_lock(uint32_t otp, uint32_t value);
198 bool bsec_read_sp_lock(uint32_t otp);
199 bool bsec_wr_lock(uint32_t otp);
200 uint32_t bsec_otp_lock(uint32_t service, uint32_t value);
201 
202 uint32_t bsec_shadow_read_otp(uint32_t *otp_value, uint32_t word);
203 uint32_t bsec_check_nsec_access_rights(uint32_t otp);
204 
205 #endif /* BSEC_H */
206