1 /*
2  * Copyright (c) 2016-2022, STMicroelectronics - All Rights Reserved
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <errno.h>
9 
10 #include <common/debug.h>
11 #include <drivers/st/bsec.h>
12 #include <drivers/st/bsec2_reg.h>
13 #include <drivers/st/stm32mp1_rcc.h>
14 #include <lib/mmio.h>
15 #include <lib/utils_def.h>
16 
17 #include <platform_def.h>
18 #include <stm32mp1_dbgmcu.h>
19 
20 #define DBGMCU_IDC		U(0x00)
21 
22 #define DBGMCU_IDC_DEV_ID_MASK	GENMASK(11, 0)
23 #define DBGMCU_IDC_REV_ID_MASK	GENMASK(31, 16)
24 #define DBGMCU_IDC_REV_ID_SHIFT	16
25 
stm32mp1_dbgmcu_init(void)26 static int stm32mp1_dbgmcu_init(void)
27 {
28 	if ((bsec_read_debug_conf() & BSEC_DBGSWGEN) == 0U) {
29 		INFO("Software access to all debug components is disabled\n");
30 		return -1;
31 	}
32 
33 	mmio_setbits_32(RCC_BASE + RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN);
34 
35 	return 0;
36 }
37 
38 /*
39  * @brief  Get silicon revision from DBGMCU registers.
40  * @param  chip_version: pointer to the read value.
41  * @retval 0 on success, negative value on failure.
42  */
stm32mp1_dbgmcu_get_chip_version(uint32_t * chip_version)43 int stm32mp1_dbgmcu_get_chip_version(uint32_t *chip_version)
44 {
45 	assert(chip_version != NULL);
46 
47 	if (stm32mp1_dbgmcu_init() != 0) {
48 		return -EPERM;
49 	}
50 
51 	*chip_version = (mmio_read_32(DBGMCU_BASE + DBGMCU_IDC) &
52 			 DBGMCU_IDC_REV_ID_MASK) >> DBGMCU_IDC_REV_ID_SHIFT;
53 
54 	return 0;
55 }
56 
57 /*
58  * @brief  Get device ID from DBGMCU registers.
59  * @param  chip_dev_id: pointer to the read value.
60  * @retval 0 on success, negative value on failure.
61  */
stm32mp1_dbgmcu_get_chip_dev_id(uint32_t * chip_dev_id)62 int stm32mp1_dbgmcu_get_chip_dev_id(uint32_t *chip_dev_id)
63 {
64 	assert(chip_dev_id != NULL);
65 
66 	if (stm32mp1_dbgmcu_init() != 0) {
67 		return -EPERM;
68 	}
69 
70 	*chip_dev_id = mmio_read_32(DBGMCU_BASE + DBGMCU_IDC) &
71 		       DBGMCU_IDC_DEV_ID_MASK;
72 
73 	return 0;
74 }
75