Skip to main content

nrf52/
ficr.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 2022.
4
5//! Factory Information Configuration Registers (FICR)
6//!
7//! Factory information configuration registers (FICR) are pre-programmed in
8//! factory and cannot be erased by the user. These registers contain
9//! chip-specific information and configuration.
10//!
11//! - Author: Pat Pannuto <ppannuto@berkeley.edu>
12//! - Date: November 27, 2017
13
14use core::fmt;
15use kernel::utilities::StaticRef;
16use tock_registers_v0::interfaces::Readable;
17use tock_registers_v0::registers::ReadOnly;
18use tock_registers_v0::{register_bitfields, register_structs};
19
20register_structs! {
21    /// Struct of the FICR registers
22    ///
23    /// Section 13.1 of <https://infocenter.nordicsemi.com/pdf/nRF52832_PS_v1.0.pdf>
24    /// Section  4.4 of <https://infocenter.nordicsemi.com/pdf/nRF52840_PS_v1.1.pdf>
25    /// The structure is identical for the data mapped here, differences start
26    /// at address 0x350.
27    pub FicrRegisters {
28        (0x000 => _reserved0),
29        /// Code memory page size
30        (0x010 => codepagesize: ReadOnly<u32, CodePageSize::Register>),
31        /// Code memory size
32        (0x014 => codesize: ReadOnly<u32, CodeSize::Register>),
33        (0x018 => _reserved1),
34        /// Device identifier
35        (0x060 => deviceid0: ReadOnly<u32, DeviceId0::Register>),
36        /// Device identifier
37        (0x064 => deviceid1: ReadOnly<u32, DeviceId1::Register>),
38        (0x068 => _reserved2),
39        /// Encryption Root
40        (0x080 => er: [ReadOnly<u32, EncryptionRoot::Register>; 4]),
41        /// Identity Root
42        (0x090 => ir: [ReadOnly<u32, IdentityRoot::Register>; 4]),
43        /// Device address type
44        (0x0A0 => deviceaddrtype: ReadOnly<u32, DeviceAddressType::Register>),
45        /// Device address
46        (0x0A4 => deviceaddr0: ReadOnly<u32, DeviceAddress0::Register>),
47        /// Device address
48        (0x0A8 => deviceaddr1: ReadOnly<u32, DeviceAddress1::Register>),
49        (0x0AC => _reserved3),
50        /// Part code
51        (0x100 => info_part: ReadOnly<u32, InfoPart::Register>),
52        /// Part Variant, Hardware version and Production configuration
53        (0x104 => info_variant: ReadOnly<u32, InfoVariant::Register>),
54        /// Package option
55        (0x108 => info_package: ReadOnly<u32, InfoPackage::Register>),
56        /// RAM variant
57        (0x10C => info_ram: ReadOnly<u32, InfoRam::Register>),
58        /// Flash variant
59        (0x110 => info_flash: ReadOnly<u32, InfoFlash::Register>),
60        (0x114 => @END),
61    }
62}
63
64register_bitfields! [u32,
65    /// Code memory page size
66    CodePageSize [
67        /// Code memory page size
68        CODEPAGESIZE OFFSET(0) NUMBITS(32)
69    ],
70    /// Code memory size
71    CodeSize [
72        /// Code memory size in number of pages
73        CODESIZE OFFSET(0) NUMBITS(32)
74    ],
75    /// Device Identifier
76    DeviceId0 [
77        /// 32 LSB of 64 bit unique device identifier
78        DEVICEID OFFSET(0) NUMBITS(32)
79    ],
80    /// Device Identifier
81    DeviceId1 [
82        /// 32 MSB of 64 bit unique device identifier
83        DEVICEID OFFSET(0) NUMBITS(32)
84    ],
85    /// Encryption Root
86    EncryptionRoot [
87        /// Encryption Root, word n
88        ER OFFSET(0) NUMBITS(32)
89    ],
90    /// Identity Root
91    IdentityRoot [
92        /// Identity Root, word n
93        IR OFFSET(0) NUMBITS(32)
94    ],
95    /// Device address type
96    DeviceAddressType [
97        /// Device address type
98        DEVICEADDRESSTYPE OFFSET(0) NUMBITS(1) [
99            /// Public
100            PUBLIC = 0,
101            /// Random
102            RANDOM = 1
103        ]
104    ],
105    /// Device address 1
106    DeviceAddress0 [
107        /// 32 LSB of 48 bit device address
108        DEVICEADDRESS OFFSET(0) NUMBITS(32)
109    ],
110    /// Device address 2
111    DeviceAddress1 [
112        /// 16 MSB of 48 bit device address
113        DEVICEADDRESS OFFSET(0) NUMBITS(16)
114    ],
115    /// Part code
116    InfoPart [
117        PART OFFSET(0) NUMBITS(32) [
118            /// nRF52832
119            N52832 = 0x52832,
120            /// nRF52833
121            N52833 = 0x52833,
122            /// nRF52840
123            N52840 = 0x52840,
124            /// Unspecified
125            Unspecified = 0xffffffff
126        ]
127    ],
128    /// Part Variant, Hardware version and Production configuration
129    InfoVariant [
130        /// Part Variant, Hardware version and Production configuration, encoded as ASCII
131        // Note, some of these are not present in datasheets
132        // but are in nrf52.svd or are observed in the wild
133        VARIANT OFFSET(0) NUMBITS(32) [
134            /// AAA0
135            AAA0 = 0x41414130,
136            /// AAAA
137            AAAA = 0x41414141,
138            /// AAAB
139            AAAB = 0x41414142,
140            /// AAB0
141            AAB0 = 0x41414230,
142            /// AABA
143            AABA = 0x41414241,
144            /// AABB
145            AABB = 0x41414242,
146            /// AAC0
147            AAC0 = 0x41414330,
148            /// AACA
149            AACA = 0x41414341,
150            /// AACB
151            AACB = 0x41414342,
152            /// ABBA
153            ABBA = 0x41424241,
154            /// AAD0
155            AAD0 = 0x41414430,
156            /// AAD1
157            AAD1 = 0x41414431,
158            /// AADA
159            AADA = 0x41414441,
160            /// AAE0
161            AAE0 = 0x41414530,
162            /// AAEA
163            AAEA = 0x41414541,
164            /// AAF0
165            AAF0 = 0x41414630,
166            /// BAAA
167            BAAA = 0x42414141,
168            /// CAAA
169            CAAA = 0x43414141,
170            /// Unspecified
171            Unspecified = 0xffffffff
172        ]
173    ],
174    /// Package option
175    // Note, some of these are not present in datasheet but is in nrf52.svd
176    InfoPackage [
177        PACKAGE OFFSET(0) NUMBITS(32) [
178            /// QFxx - 48-pin QFN
179            QF = 0x2000,
180            /// CHxx - 7x8 WLCSP 56 balls
181            CH = 0x2001,
182            /// CIxx - 7x8 WLCSP 56 balls<
183            CI = 0x2002,
184            /// QIxx - 73-pin aQFN
185            QI = 0x2004,
186            /// CKxx - 7x8 WLCSP 56 balls with backside coating for light protection
187            CK = 0x2005,
188            /// Unspecified
189            Unspecified = 0xffffffff
190        ]
191    ],
192    /// RAM variant
193    InfoRam [
194        RAM OFFSET(0) NUMBITS(32) [
195            /// 16 kByte RAM
196            K16 = 0x10,
197            /// 32 kByte RAM
198            K32 = 0x20,
199            /// 64 kByte RAM
200            K64 = 0x40,
201            /// 128 kByte RAM
202            K128 = 0x80,
203            /// 256 kByte RAM
204            K256 = 0x100,
205            Unspecified = 0xffffffff
206
207        ]
208    ],
209    /// Flash
210    InfoFlash [
211        FLASH OFFSET(0) NUMBITS(32) [
212            /// 128 kByte FLASH
213            K128 = 0x80,
214            /// 256 kByte FLASH
215            K256 = 0x100,
216            /// 512 kByte FLASH
217            K512 = 0x200,
218            /// 1024 kByte FLASH
219            K1024 = 0x400,
220            /// 2048 kByte FLASH
221            K2048 = 0x800,
222            /// Unspecified
223            Unspecified = 0xffffffff
224        ]
225    ]
226];
227
228/// Variant describes part variant, hardware version, and production configuration.
229#[derive(PartialEq, Debug)]
230#[repr(u32)]
231pub(crate) enum Variant {
232    AAA0 = 0x41414130,
233    AAAA = 0x41414141,
234    AAAB = 0x41414142,
235    AAB0 = 0x41414230,
236    AABA = 0x41414241,
237    AABB = 0x41414242,
238    AAC0 = 0x41414330,
239    AACA = 0x41414341,
240    AACB = 0x41414342,
241    AAD0 = 0x41414430,
242    AAD1 = 0x41414431,
243    AADA = 0x41414441,
244    AAE0 = 0x41414530,
245    AAEA = 0x41414541,
246    AAF0 = 0x41414630,
247    ABBA = 0x41424241,
248    BAAA = 0x42414141,
249    CAAA = 0x43414141,
250    Unspecified = 0xffffffff,
251}
252
253#[derive(PartialEq, Debug)]
254#[repr(u32)]
255enum Part {
256    N52832 = 0x52832,
257    N52833 = 0x52833,
258    N52840 = 0x52840,
259    Unspecified = 0xffffffff,
260}
261
262#[derive(PartialEq, Debug)]
263#[repr(u32)]
264enum Package {
265    QF = 0x2000,
266    CH = 0x2001,
267    CI = 0x2002,
268    QI = 0x2004,
269    CK = 0x2005,
270    Unspecified = 0xffffffff,
271}
272
273#[derive(PartialEq, Debug)]
274#[repr(u32)]
275enum Ram {
276    K16 = 0x10,
277    K32 = 0x20,
278    K64 = 0x40,
279    K128 = 0x80,
280    K256 = 0x100,
281    Unspecified = 0xffffffff,
282}
283
284#[derive(Debug)]
285#[repr(u32)]
286enum Flash {
287    K128 = 0x80,
288    K256 = 0x100,
289    K512 = 0x200,
290    K1024 = 0x400,
291    K2048 = 0x800,
292    Unspecified = 0xffffffff,
293}
294
295#[derive(Debug)]
296#[repr(u32)]
297pub enum AddressType {
298    Public = 0x0,
299    Random = 0x1,
300}
301
302pub struct Ficr {
303    registers: StaticRef<FicrRegisters>,
304}
305
306impl Ficr {
307    pub const fn new(registers: StaticRef<FicrRegisters>) -> Ficr {
308        Ficr { registers }
309    }
310
311    fn part(&self) -> Part {
312        match self.registers.info_part.get() {
313            0x52832 => Part::N52832,
314            0x52833 => Part::N52833,
315            0x52840 => Part::N52840,
316            _ => Part::Unspecified,
317        }
318    }
319
320    pub(crate) fn variant(&self) -> Variant {
321        // If you update this, make sure to update
322        // `has_updated_approtect_logic()` as well.
323        match self.registers.info_variant.get() {
324            0x41414130 => Variant::AAA0,
325            0x41414141 => Variant::AAAA,
326            0x41414142 => Variant::AAAB,
327            0x41414230 => Variant::AAB0,
328            0x41414241 => Variant::AABA,
329            0x41414242 => Variant::AABB,
330            0x41414330 => Variant::AAC0,
331            0x41414341 => Variant::AACA,
332            0x41414342 => Variant::AACB,
333            0x41424241 => Variant::ABBA,
334            0x41414430 => Variant::AAD0,
335            0x41414431 => Variant::AAD1,
336            0x41414441 => Variant::AADA,
337            0x41414530 => Variant::AAE0,
338            0x41414541 => Variant::AAEA,
339            0x41414630 => Variant::AAF0,
340            0x42414141 => Variant::BAAA,
341            0x43414141 => Variant::CAAA,
342            _ => Variant::Unspecified,
343        }
344    }
345
346    /// Returns if this variant of the nRF52 has the updated APPROTECT logic.
347    /// This changed occurred towards the end of 2021 with chips becoming widely
348    /// available/used in 2023.
349    ///
350    /// See <https://devzone.nordicsemi.com/nordic/nordic-blog/b/blog/posts/working-with-the-nrf52-series-improved-approtect>.
351    /// for more information.
352    pub(crate) fn has_updated_approtect_logic(&self) -> bool {
353        // We assume that an unspecified version means that it is new and this
354        // module hasn't been updated to recognize it.
355        match self.variant() {
356            Variant::AAF0 | Variant::Unspecified => true,
357            _ => false,
358        }
359    }
360
361    fn package(&self) -> Package {
362        match self.registers.info_package.get() {
363            0x2000 => Package::QF,
364            0x2001 => Package::CH,
365            0x2002 => Package::CI,
366            0x2004 => Package::QI,
367            0x2005 => Package::CK,
368            _ => Package::Unspecified,
369        }
370    }
371
372    fn ram(&self) -> Ram {
373        match self.registers.info_ram.get() {
374            0x10 => Ram::K16,
375            0x20 => Ram::K32,
376            0x40 => Ram::K64,
377            0x80 => Ram::K128,
378            0x100 => Ram::K256,
379            _ => Ram::Unspecified,
380        }
381    }
382
383    fn flash(&self) -> Flash {
384        match self.registers.info_flash.get() {
385            0x80 => Flash::K128,
386            0x100 => Flash::K256,
387            0x200 => Flash::K512,
388            0x400 => Flash::K1024,
389            0x800 => Flash::K2048,
390            _ => Flash::Unspecified,
391        }
392    }
393
394    pub fn id(&self) -> [u8; 8] {
395        let lo = self.registers.deviceid0.read(DeviceId0::DEVICEID);
396        let hi = self.registers.deviceid1.read(DeviceId1::DEVICEID);
397        let mut addr = [0; 8];
398        addr[..4].copy_from_slice(&lo.to_le_bytes());
399        addr[4..].copy_from_slice(&hi.to_le_bytes());
400        addr
401    }
402
403    pub fn address(&self) -> [u8; 6] {
404        let lo = self
405            .registers
406            .deviceaddr0
407            .read(DeviceAddress0::DEVICEADDRESS);
408        let hi = self
409            .registers
410            .deviceaddr1
411            .read(DeviceAddress1::DEVICEADDRESS) as u16;
412        let mut addr = [0; 6];
413        addr[..4].copy_from_slice(&lo.to_le_bytes());
414        addr[4..].copy_from_slice(&hi.to_le_bytes());
415        addr
416    }
417
418    pub fn address_type(&self) -> AddressType {
419        match self
420            .registers
421            .deviceaddrtype
422            .read(DeviceAddressType::DEVICEADDRESSTYPE)
423        {
424            0x0 => AddressType::Public,
425            _ => AddressType::Random,
426        }
427    }
428
429    /// Return a MAC address string that has been hardcoded on this chip in the
430    /// format `46:db:52:cd:93:9e`.
431    pub fn address_str(&self, buf: &'static mut [u8; 17]) -> &'static str {
432        let lo = self
433            .registers
434            .deviceaddr0
435            .read(DeviceAddress0::DEVICEADDRESS);
436        let hi = self
437            .registers
438            .deviceaddr1
439            .read(DeviceAddress1::DEVICEADDRESS);
440
441        let h: [u8; 16] = *b"0123456789abcdef";
442
443        buf[0] = h[((hi >> 12) & 0xf) as usize];
444        buf[1] = h[((hi >> 8) & 0xf) as usize];
445        buf[2] = b':';
446        buf[3] = h[((hi >> 4) & 0xf) as usize];
447        buf[4] = h[((hi >> 0) & 0xf) as usize];
448        buf[5] = b':';
449        buf[6] = h[((lo >> 28) & 0xf) as usize];
450        buf[7] = h[((lo >> 24) & 0xf) as usize];
451        buf[8] = b':';
452        buf[9] = h[((lo >> 20) & 0xf) as usize];
453        buf[10] = h[((lo >> 16) & 0xf) as usize];
454        buf[11] = b':';
455        buf[12] = h[((lo >> 12) & 0xf) as usize];
456        buf[13] = h[((lo >> 8) & 0xf) as usize];
457        buf[14] = b':';
458        buf[15] = h[((lo >> 4) & 0xf) as usize];
459        buf[16] = h[((lo >> 0) & 0xf) as usize];
460
461        // Safe because we use only ascii characters in this buffer.
462        unsafe { &*(core::ptr::from_ref::<[u8]>(buf) as *const str) }
463    }
464}
465
466impl fmt::Display for Ficr {
467    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
468        write!(
469            f,
470            "NRF52 HW INFO: Variant: {:?}, Part: {:?}, Package: {:?}, Ram: {:?}, Flash: {:?}",
471            self.variant(),
472            self.part(),
473            self.package(),
474            self.ram(),
475            self.flash()
476        )
477    }
478}