bootloader: Reimplement flash::run_command() in C.
[gps-watch.git] / src / bootloader / flash.rs
1 /*
2  * Copyright (c) 2019 Tilman Sauerbeck (tilman at code-monkey de)
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23
24 use common::register;
25
26 type Reg8 = register::Register<u8>;
27
28 const FTFA_BASE: u32 = 0x40020000;
29
30 const FTFA_FSTAT : u32 = FTFA_BASE + 0x00;
31 const FTFA_FCCOB3: u32 = FTFA_BASE + 0x04;
32 const FTFA_FCCOB2: u32 = FTFA_BASE + 0x05;
33 const FTFA_FCCOB1: u32 = FTFA_BASE + 0x06;
34 const FTFA_FCCOB0: u32 = FTFA_BASE + 0x07;
35 const FTFA_FCCOB7: u32 = FTFA_BASE + 0x08;
36 const FTFA_FCCOB6: u32 = FTFA_BASE + 0x09;
37 const FTFA_FCCOB5: u32 = FTFA_BASE + 0x0a;
38 const FTFA_FCCOB4: u32 = FTFA_BASE + 0x0b;
39
40 const FTFA_FSTAT_MGSTAT0 : u8 = 1 << 0;
41 const FTFA_FSTAT_FPVIOL  : u8 = 1 << 4;
42 const FTFA_FSTAT_ACCERR  : u8 = 1 << 5;
43 const FTFA_FSTAT_RDCOLERR: u8 = 1 << 6;
44
45 const FCMD_RD1SEC : u8 = 0x01;
46 const FCMD_PGM4   : u8 = 0x06;
47 const FCMD_ERSSCR : u8 = 0x09;
48
49 pub const SECTOR_SIZE : usize = 1024;
50
51 extern {
52     fn flash_run_command() -> u8;
53 }
54
55 // The hardware refuses to run another command (or even take the new command's
56 // parameters) until the errors reported by a previous command have been
57 // acknowledged (see KL26P121M48SF4RM.pdf p. 445).
58 fn prepare_command() {
59     let mut fstat = Reg8::new(FTFA_FSTAT);
60
61     fstat.write(FTFA_FSTAT_FPVIOL | FTFA_FSTAT_ACCERR | FTFA_FSTAT_RDCOLERR);
62 }
63
64 fn run_command() -> u8 {
65     unsafe { flash_run_command() }
66 }
67
68 fn erase_check(sector: u32) -> bool {
69     let address = sector as usize * SECTOR_SIZE;
70     let num_words = SECTOR_SIZE / 4;
71
72     prepare_command();
73
74     Reg8::new(FTFA_FCCOB0).write(FCMD_RD1SEC);
75     Reg8::new(FTFA_FCCOB1).write((address >> 16) as u8);
76     Reg8::new(FTFA_FCCOB2).write((address >>  8) as u8);
77     Reg8::new(FTFA_FCCOB3).write((address >>  0) as u8);
78     Reg8::new(FTFA_FCCOB4).write((num_words >> 8) as u8);
79     Reg8::new(FTFA_FCCOB5).write((num_words >> 0) as u8);
80     Reg8::new(FTFA_FCCOB6).write(0);
81
82     (run_command() & FTFA_FSTAT_MGSTAT0) == 0
83 }
84
85 pub fn erase(sector: u32) {
86     // Already erased?
87     if erase_check(sector) {
88         return;
89     }
90
91     let address = sector as usize * SECTOR_SIZE;
92
93     prepare_command();
94
95     Reg8::new(FTFA_FCCOB0).write(FCMD_ERSSCR);
96     Reg8::new(FTFA_FCCOB1).write((address >> 16) as u8);
97     Reg8::new(FTFA_FCCOB2).write((address >>  8) as u8);
98     Reg8::new(FTFA_FCCOB3).write((address >>  0) as u8);
99
100     run_command();
101 }
102
103 pub fn program(sector: u32, data: &[u8]) {
104     prepare_command();
105
106     for (i, bytes) in data.chunks(4).enumerate() {
107         if bytes[0] == 0xff &&
108            bytes[1] == 0xff &&
109            bytes[2] == 0xff &&
110            bytes[3] == 0xff {
111             continue;
112         }
113
114         let address = (sector as usize * SECTOR_SIZE) + (i * 4);
115
116         Reg8::new(FTFA_FCCOB0).write(FCMD_PGM4);
117         Reg8::new(FTFA_FCCOB1).write((address >> 16) as u8);
118         Reg8::new(FTFA_FCCOB2).write((address >>  8) as u8);
119         Reg8::new(FTFA_FCCOB3).write((address >>  0) as u8);
120         Reg8::new(FTFA_FCCOB4).write(bytes[3]);
121         Reg8::new(FTFA_FCCOB5).write(bytes[2]);
122         Reg8::new(FTFA_FCCOB6).write(bytes[1]);
123         Reg8::new(FTFA_FCCOB7).write(bytes[0]);
124
125         run_command();
126     }
127 }
128
129 pub fn verify(sector: u32, data: &[u8]) -> (u32, u32) {
130     let mut num_mismatches = 0;
131     let mut first_mismatch_offset = 0;
132
133     for i in (0..data.len()).rev() {
134         let address = (sector as usize * SECTOR_SIZE) + i;
135         let ptr = address as *const u8;
136         let actual_byte = unsafe { core::ptr::read(ptr) };
137         let expected_byte = data[i];
138
139         if actual_byte != expected_byte {
140             num_mismatches += 1;
141             first_mismatch_offset = i;
142         }
143     }
144
145     (num_mismatches, first_mismatch_offset as u32)
146 }