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 tock_registers_v0::interfaces::Writeable;
24use tock_registers_v0::registers::ReadWrite;
25use tock_registers_v0::{register_bitfields, register_structs};
26
27register_structs! {
28    pub ApprotectRegisters {
29        (0x000 => _reserved0),
30        (0x550 => forceprotect: ReadWrite<u32, Forceprotect::Register>),
31        (0x554 => _reserved1),
32        (0x558 => disable: ReadWrite<u32, Disable::Register>),
33        (0x55c => @END),
34    }
35}
36
37register_bitfields! [u32,
38    Forceprotect [
39        FORCEPROTECT OFFSET(0) NUMBITS(8) [
40            FORCE = 0
41        ]
42    ],
43    /// Access port protection
44    Disable [
45        DISABLE OFFSET(0) NUMBITS(8) [
46            SWDISABLE = 0x5a
47        ]
48    ]
49];
50
51pub struct Approtect<'a> {
52    registers: StaticRef<ApprotectRegisters>,
53    ficr: &'a ficr::Ficr,
54}
55
56impl<'a> Approtect<'a> {
57    pub const fn new(
58        registers: StaticRef<ApprotectRegisters>,
59        ficr: &'a ficr::Ficr,
60    ) -> Approtect<'a> {
61        Approtect { registers, ficr }
62    }
63
64    /// Software disable the Access Port Protection mechanism.
65    ///
66    /// On newer variants of the nRF52, to enable JTAG, APPROTECT must be
67    /// disabled both in the UICR register (hardware) and in this register
68    /// (software). For older variants this is just a no-op.
69    ///
70    /// - <https://devzone.nordicsemi.com/f/nordic-q-a/96590/how-to-disable-approtect-permanently-dfu-is-needed>
71    /// - <https://devzone.nordicsemi.com/nordic/nordic-blog/b/blog/posts/working-with-the-nrf52-series-improved-approtect>
72    pub fn sw_disable_approtect(&self) {
73        match self.ficr.variant() {
74            ficr::Variant::AAF0 | ficr::Variant::Unspecified => {
75                // Newer revisions of the chip require setting the APPROTECT
76                // software register to `SwDisable`. We assume that an unspecified
77                // version means that it is new and the FICR module hasn't been
78                // updated to recognize it.
79                self.registers.disable.write(Disable::DISABLE::SWDISABLE);
80            }
81
82            // Exhaustively list variants here to produce compiler error on
83            // adding a new variant, which would otherwise not match the above
84            // condition.
85            ficr::Variant::AAA0
86            | ficr::Variant::AAAA
87            | ficr::Variant::AAAB
88            | ficr::Variant::AAB0
89            | ficr::Variant::AABA
90            | ficr::Variant::AABB
91            | ficr::Variant::AAC0
92            | ficr::Variant::AACA
93            | ficr::Variant::AACB
94            | ficr::Variant::AAD0
95            | ficr::Variant::AAD1
96            | ficr::Variant::AADA
97            | ficr::Variant::AAE0
98            | ficr::Variant::AAEA
99            | ficr::Variant::ABBA
100            | ficr::Variant::BAAA
101            | ficr::Variant::CAAA => {
102                // All other revisions don't need this.
103            }
104        }
105    }
106}