blob: 90323ac6132311da79c7d61fffa7c146f1a2d273 [file] [log] [blame]
Yann Gautier73680c22019-06-04 18:06:34 +02001/*
Nicolas Le Bayona24d5942019-09-19 11:27:24 +02002 * Copyright (c) 2016-2021, STMicroelectronics - All Rights Reserved
Yann Gautier73680c22019-06-04 18:06:34 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Nicolas Le Bayona24d5942019-09-19 11:27:24 +02007#include <assert.h>
Yann Gautier73680c22019-06-04 18:06:34 +02008#include <errno.h>
9
10#include <platform_def.h>
11
12#include <common/debug.h>
13#include <drivers/st/bsec.h>
14#include <drivers/st/stm32mp1_rcc.h>
15#include <lib/mmio.h>
16#include <lib/utils_def.h>
17
18#include <stm32mp1_dbgmcu.h>
19
Yann Gautierdec286d2019-06-04 18:02:37 +020020#define DBGMCU_IDC U(0x00)
Yann Gautier73680c22019-06-04 18:06:34 +020021#define DBGMCU_APB4FZ1 U(0x2C)
Yann Gautierdec286d2019-06-04 18:02:37 +020022
23#define DBGMCU_IDC_DEV_ID_MASK GENMASK(11, 0)
24#define DBGMCU_IDC_REV_ID_MASK GENMASK(31, 16)
25#define DBGMCU_IDC_REV_ID_SHIFT 16
26
Yann Gautier73680c22019-06-04 18:06:34 +020027#define DBGMCU_APB4FZ1_IWDG2 BIT(2)
28
Yann Gautier73680c22019-06-04 18:06:34 +020029static int stm32mp1_dbgmcu_init(void)
30{
31 uint32_t dbg_conf;
Yann Gautier73680c22019-06-04 18:06:34 +020032
33 dbg_conf = bsec_read_debug_conf();
34
35 if ((dbg_conf & BSEC_DBGSWGEN) == 0U) {
36 uint32_t result = bsec_write_debug_conf(dbg_conf |
37 BSEC_DBGSWGEN);
38
39 if (result != BSEC_OK) {
40 ERROR("Error enabling DBGSWGEN\n");
41 return -1;
42 }
43 }
44
Nicolas Le Bayona24d5942019-09-19 11:27:24 +020045 mmio_setbits_32(RCC_BASE + RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN);
Yann Gautier73680c22019-06-04 18:06:34 +020046
47 return 0;
48}
49
Nicolas Le Bayona24d5942019-09-19 11:27:24 +020050/*
51 * @brief Get silicon revision from DBGMCU registers.
52 * @param chip_version: pointer to the read value.
53 * @retval 0 on success, negative value on failure.
54 */
Yann Gautierdec286d2019-06-04 18:02:37 +020055int stm32mp1_dbgmcu_get_chip_version(uint32_t *chip_version)
56{
Nicolas Le Bayona24d5942019-09-19 11:27:24 +020057 assert(chip_version != NULL);
58
Yann Gautierdec286d2019-06-04 18:02:37 +020059 if (stm32mp1_dbgmcu_init() != 0) {
60 return -EPERM;
61 }
62
63 *chip_version = (mmio_read_32(DBGMCU_BASE + DBGMCU_IDC) &
64 DBGMCU_IDC_REV_ID_MASK) >> DBGMCU_IDC_REV_ID_SHIFT;
65
66 return 0;
67}
68
Nicolas Le Bayona24d5942019-09-19 11:27:24 +020069/*
70 * @brief Get device ID from DBGMCU registers.
71 * @param chip_dev_id: pointer to the read value.
72 * @retval 0 on success, negative value on failure.
73 */
Yann Gautierdec286d2019-06-04 18:02:37 +020074int stm32mp1_dbgmcu_get_chip_dev_id(uint32_t *chip_dev_id)
75{
Nicolas Le Bayona24d5942019-09-19 11:27:24 +020076 assert(chip_dev_id != NULL);
77
Yann Gautierdec286d2019-06-04 18:02:37 +020078 if (stm32mp1_dbgmcu_init() != 0) {
79 return -EPERM;
80 }
81
82 *chip_dev_id = mmio_read_32(DBGMCU_BASE + DBGMCU_IDC) &
Nicolas Le Bayona24d5942019-09-19 11:27:24 +020083 DBGMCU_IDC_DEV_ID_MASK;
Yann Gautierdec286d2019-06-04 18:02:37 +020084
85 return 0;
86}
87
Nicolas Le Bayona24d5942019-09-19 11:27:24 +020088/*
89 * @brief Freeze IWDG2 in debug mode.
90 * @retval None.
91 */
Yann Gautier73680c22019-06-04 18:06:34 +020092int stm32mp1_dbgmcu_freeze_iwdg2(void)
93{
94 uint32_t dbg_conf;
95
96 if (stm32mp1_dbgmcu_init() != 0) {
97 return -EPERM;
98 }
99
100 dbg_conf = bsec_read_debug_conf();
101
102 if ((dbg_conf & (BSEC_SPIDEN | BSEC_SPINDEN)) != 0U) {
103 mmio_setbits_32(DBGMCU_BASE + DBGMCU_APB4FZ1,
104 DBGMCU_APB4FZ1_IWDG2);
105 }
106
107 return 0;
108}