Skip to main content

nrf52/
approtect.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2023.
4
5//! Access port protection
6//!
7//! <https://infocenter.nordicsemi.com/index.jsp?topic=%2Fps_nrf52840%2Fdif.html&cp=5_0_0_3_7_1&anchor=register.DISABLE>
8//!
9//! The logic around APPROTECT was changed in newer revisions of the nRF52
10//! series chips (Oct 2021) and later which requires more careful disabling of
11//! the access port (JTAG), both in the UICR register and in a software written
12//! register. This module enables the kernel to disable the protection on boot.
13//!
14//! Example code to disable the APPROTECT protection in software:
15//!
16//! ```rust,ignore
17//! let approtect = nrf52::approtect::Approtect::new();
18//! approtect.sw_disable_approtect();
19//! ```
20
21use crate::ficr;
22use kernel::utilities::StaticRef;
23use kernel::utilities::registers::interfaces::Writeable;
24use kernel::utilities::registers::{ReadWrite, register_bitfields, register_structs};
25
26register_structs! {
27    pub ApprotectRegisters {
28        (0x000 => _reserved0),
29        (0x550 => forceprotect: ReadWrite<u32, Forceprotect::Register>),
30        (0x554 => _reserved1),
31        (0x558 => disable: ReadWrite<u32, Disable::Register>),
32        (0x55c => @END),
33    }
34}
35
36register_bitfields! [u32,
37    Forceprotect [
38        FORCEPROTECT OFFSET(0) NUMBITS(8) [
39            FORCE = 0
40        ]
41    ],
42    /// Access port protection
43    Disable [
44        DISABLE OFFSET(0) NUMBITS(8) [
45            SWDISABLE = 0x5a
46        ]
47    ]
48];
49
50pub struct Approtect<'a> {
51    registers: StaticRef<ApprotectRegisters>,
52    ficr: &'a ficr::Ficr,
53}
54
55impl<'a> Approtect<'a> {
56    pub const fn new(
57        registers: StaticRef<ApprotectRegisters>,
58        ficr: &'a ficr::Ficr,
59    ) -> Approtect<'a> {
60        Approtect { registers, ficr }
61    }
62
63    /// Software disable the Access Port Protection mechanism.
64    ///
65    /// On newer variants of the nRF52, to enable JTAG, APPROTECT must be
66    /// disabled both in the UICR register (hardware) and in this register
67    /// (software). For older variants this is just a no-op.
68    ///
69    /// - <https://devzone.nordicsemi.com/f/nordic-q-a/96590/how-to-disable-approtect-permanently-dfu-is-needed>
70    /// - <https://devzone.nordicsemi.com/nordic/nordic-blog/b/blog/posts/working-with-the-nrf52-series-improved-approtect>
71    pub fn sw_disable_approtect(&self) {
72        match self.ficr.variant() {
73            ficr::Variant::AAF0 | ficr::Variant::Unspecified => {
74                // Newer revisions of the chip require setting the APPROTECT
75                // software register to `SwDisable`. We assume that an unspecified
76                // version means that it is new and the FICR module hasn't been
77                // updated to recognize it.
78                self.registers.disable.write(Disable::DISABLE::SWDISABLE);
79            }
80
81            // Exhaustively list variants here to produce compiler error on
82            // adding a new variant, which would otherwise not match the above
83            // condition.
84            ficr::Variant::AAA0
85            | ficr::Variant::AAAA
86            | ficr::Variant::AAAB
87            | ficr::Variant::AAB0
88            | ficr::Variant::AABA
89            | ficr::Variant::AABB
90            | ficr::Variant::AAC0
91            | ficr::Variant::AACA
92            | ficr::Variant::AACB
93            | ficr::Variant::AAD0
94            | ficr::Variant::AAD1
95            | ficr::Variant::AADA
96            | ficr::Variant::AAE0
97            | ficr::Variant::AAEA
98            | ficr::Variant::ABBA
99            | ficr::Variant::BAAA
100            | ficr::Variant::CAAA => {
101                // All other revisions don't need this.
102            }
103        }
104    }
105}