Yann Gautier | 73680c2 | 2019-06-04 18:06:34 +0200 | [diff] [blame] | 1 | /* |
Nicolas Le Bayon | a24d594 | 2019-09-19 11:27:24 +0200 | [diff] [blame^] | 2 | * Copyright (c) 2016-2021, STMicroelectronics - All Rights Reserved |
Yann Gautier | 73680c2 | 2019-06-04 18:06:34 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Nicolas Le Bayon | a24d594 | 2019-09-19 11:27:24 +0200 | [diff] [blame^] | 7 | #include <assert.h> |
Yann Gautier | 73680c2 | 2019-06-04 18:06:34 +0200 | [diff] [blame] | 8 | #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 Gautier | dec286d | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 20 | #define DBGMCU_IDC U(0x00) |
Yann Gautier | 73680c2 | 2019-06-04 18:06:34 +0200 | [diff] [blame] | 21 | #define DBGMCU_APB4FZ1 U(0x2C) |
Yann Gautier | dec286d | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 22 | |
| 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 Gautier | 73680c2 | 2019-06-04 18:06:34 +0200 | [diff] [blame] | 27 | #define DBGMCU_APB4FZ1_IWDG2 BIT(2) |
| 28 | |
Yann Gautier | 73680c2 | 2019-06-04 18:06:34 +0200 | [diff] [blame] | 29 | static int stm32mp1_dbgmcu_init(void) |
| 30 | { |
| 31 | uint32_t dbg_conf; |
Yann Gautier | 73680c2 | 2019-06-04 18:06:34 +0200 | [diff] [blame] | 32 | |
| 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 Bayon | a24d594 | 2019-09-19 11:27:24 +0200 | [diff] [blame^] | 45 | mmio_setbits_32(RCC_BASE + RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN); |
Yann Gautier | 73680c2 | 2019-06-04 18:06:34 +0200 | [diff] [blame] | 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
Nicolas Le Bayon | a24d594 | 2019-09-19 11:27:24 +0200 | [diff] [blame^] | 50 | /* |
| 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 Gautier | dec286d | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 55 | int stm32mp1_dbgmcu_get_chip_version(uint32_t *chip_version) |
| 56 | { |
Nicolas Le Bayon | a24d594 | 2019-09-19 11:27:24 +0200 | [diff] [blame^] | 57 | assert(chip_version != NULL); |
| 58 | |
Yann Gautier | dec286d | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 59 | 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 Bayon | a24d594 | 2019-09-19 11:27:24 +0200 | [diff] [blame^] | 69 | /* |
| 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 Gautier | dec286d | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 74 | int stm32mp1_dbgmcu_get_chip_dev_id(uint32_t *chip_dev_id) |
| 75 | { |
Nicolas Le Bayon | a24d594 | 2019-09-19 11:27:24 +0200 | [diff] [blame^] | 76 | assert(chip_dev_id != NULL); |
| 77 | |
Yann Gautier | dec286d | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 78 | if (stm32mp1_dbgmcu_init() != 0) { |
| 79 | return -EPERM; |
| 80 | } |
| 81 | |
| 82 | *chip_dev_id = mmio_read_32(DBGMCU_BASE + DBGMCU_IDC) & |
Nicolas Le Bayon | a24d594 | 2019-09-19 11:27:24 +0200 | [diff] [blame^] | 83 | DBGMCU_IDC_DEV_ID_MASK; |
Yann Gautier | dec286d | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
Nicolas Le Bayon | a24d594 | 2019-09-19 11:27:24 +0200 | [diff] [blame^] | 88 | /* |
| 89 | * @brief Freeze IWDG2 in debug mode. |
| 90 | * @retval None. |
| 91 | */ |
Yann Gautier | 73680c2 | 2019-06-04 18:06:34 +0200 | [diff] [blame] | 92 | int 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 | } |