1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#![no_std]
#![feature(core_intrinsics)]
#[macro_use]
extern crate bitflags;
use core::{cmp, mem};
pub use self::io::Io;
mod io;
pub use self::mmio::Mmio;
mod mmio;
#[derive(Debug)]
pub enum SpiError {
Access,
Cycle,
Register,
}
pub trait Spi {
fn len(&mut self) -> Result<usize, SpiError>;
fn read(&mut self, address: usize, buf: &mut [u8]) -> Result<usize, SpiError>;
fn erase(&mut self, address: usize) -> Result<(), SpiError>;
fn write(&mut self, address: usize, buf: &[u8]) -> Result<usize, SpiError>;
}
bitflags! {
pub struct HsfStsCtl: u32 {
const FDONE = 1 << 0;
const FCERR = 1 << 1;
const H_AEL = 1 << 2;
const H_SCIP = 1 << 5;
const WRSDIS = 1 << 11;
const PRR34_LOCKDN = 1 << 12;
const FDOPSS = 1 << 13;
const FDV = 1 << 14;
const FLOCKDN = 1 << 15;
const FGO = 1 << 16;
const FCYCLE = 0b1111 << 17;
const FCYCLE_0 = 1 << 17;
const FCYCLE_1 = 1 << 18;
const FCYCLE_2 = 1 << 19;
const FCYCLE_3 = 1 << 20;
const WET = 1 << 21;
const FDBC = 0b111111 << 24;
const FDBC_0 = 1 << 24;
const FDBC_1 = 1 << 25;
const FDBC_2 = 1 << 26;
const FDBC_3 = 1 << 27;
const FDBC_4 = 1 << 28;
const FDBC_5 = 1 << 29;
const FSMIE = 1 << 31;
}
}
impl HsfStsCtl {
fn cycle(&self) -> HsfStsCtlCycle {
unsafe { mem::transmute((*self & Self::FCYCLE).bits) }
}
fn set_cycle(&mut self, value: HsfStsCtlCycle) {
*self = (*self & !Self::FCYCLE) | (
Self::from_bits_truncate(value as u32)
);
}
fn count(&self) -> u8 {
(((*self & Self::FDBC).bits >> 24) + 1) as u8
}
fn set_count(&mut self, value: u8) {
*self = (*self & !Self::FDBC) | (
Self::from_bits_truncate(
(cmp::max(value, 64).saturating_sub(1) as u32) << 24
)
);
}
}
#[repr(u32)]
pub enum HsfStsCtlCycle {
Read = 0x0 << 17,
Rsvd = 0x1 << 17,
Write = 0x2 << 17,
BlockErase = 0x3 << 17,
SectorErase = 0x4 << 17,
ReadSfdp = 0x5 << 17,
ReadJedec = 0x6 << 17,
WriteStatus = 0x7 << 17,
ReadStatus = 0x8 << 17,
RpmcOp1 = 0x9 << 17,
RpmcOp2 = 0xA << 17,
}
#[repr(u32)]
pub enum FdoSection {
Map = 0b000 << 12,
Component = 0b001 << 12,
Region = 0b010 << 12,
Master = 0b011 << 12
}
pub type SpiKbl = SpiSkl;
pub type SpiCnl = SpiSkl;
#[allow(dead_code)]
#[repr(packed)]
pub struct SpiSkl {
bfpreg: Mmio<u32>,
hsfsts_ctl: Mmio<u32>,
faddr: Mmio<u32>,
dlock: Mmio<u32>,
fdata: [Mmio<u32>; 16],
fracc: Mmio<u32>,
freg: [Mmio<u32>; 6],
_reserved1: [Mmio<u32>; 6],
fpr: [Mmio<u32>; 5],
gpr: Mmio<u32>,
_reserved2: [Mmio<u32>; 5],
sfracc: Mmio<u32>,
fdoc: Mmio<u32>,
fdod: Mmio<u32>,
_reserved3: Mmio<u32>,
afc: Mmio<u32>,
vscc0: Mmio<u32>,
vscc1: Mmio<u32>,
ptinx: Mmio<u32>,
ptdata: Mmio<u32>,
sbrs: Mmio<u32>,
}
impl SpiSkl {
pub fn address() -> usize {
0xfe010000
}
pub fn hsfsts_ctl(&self) -> HsfStsCtl {
HsfStsCtl::from_bits_truncate(self.hsfsts_ctl.read())
}
pub fn set_hsfsts_ctl(&mut self, value: HsfStsCtl) {
self.hsfsts_ctl.write(value.bits);
}
pub fn fdo(&mut self, section: FdoSection, index: u16) -> u32 {
self.fdoc.write(
(section as u32) |
(((index & 0b1111111111) as u32) << 2)
);
self.fdod.read()
}
}
impl Spi for SpiSkl {
fn len(&mut self) -> Result<usize, SpiError> {
let component = self.fdo(FdoSection::Component, 0);
Ok(match component & 0b111 {
0b000 => 512,
0b001 => 1024,
0b010 => 2048,
0b011 => 4096,
0b100 => 8192,
0b101 => 16384,
_ => return Err(SpiError::Register)
} * 1024)
}
fn read(&mut self, address: usize, buf: &mut [u8]) -> Result<usize, SpiError> {
let mut count = 0;
for chunk in buf.chunks_mut(64) {
let mut hsfsts_ctl;
loop {
hsfsts_ctl = self.hsfsts_ctl();
if ! hsfsts_ctl.contains(HsfStsCtl::H_SCIP) {
break;
}
}
hsfsts_ctl.set_cycle(HsfStsCtlCycle::Read);
hsfsts_ctl.set_count(chunk.len() as u8);
hsfsts_ctl.insert(HsfStsCtl::FGO);
self.faddr.write((address + count) as u32);
self.set_hsfsts_ctl(hsfsts_ctl);
loop {
hsfsts_ctl = self.hsfsts_ctl();
if hsfsts_ctl.contains(HsfStsCtl::FCERR) {
return Err(SpiError::Cycle);
}
if hsfsts_ctl.contains(HsfStsCtl::FDONE) {
break;
}
}
for (i, dword) in chunk.chunks_mut(4).enumerate() {
let data = self.fdata[i].read();
for (j, byte) in dword.iter_mut().enumerate() {
*byte = (data >> (j * 8)) as u8;
}
}
count += chunk.len()
}
Ok(count)
}
fn erase(&mut self, address: usize) -> Result<(), SpiError> {
let mut hsfsts_ctl;
loop {
hsfsts_ctl = self.hsfsts_ctl();
if ! hsfsts_ctl.contains(HsfStsCtl::H_SCIP) {
break;
}
}
hsfsts_ctl.set_cycle(HsfStsCtlCycle::BlockErase);
hsfsts_ctl.insert(HsfStsCtl::FGO);
self.faddr.write(address as u32);
self.set_hsfsts_ctl(hsfsts_ctl);
loop {
hsfsts_ctl = self.hsfsts_ctl();
if hsfsts_ctl.contains(HsfStsCtl::FCERR) {
return Err(SpiError::Cycle);
}
if hsfsts_ctl.contains(HsfStsCtl::FDONE) {
break;
}
}
Ok(())
}
fn write(&mut self, address: usize, buf: &[u8]) -> Result<usize, SpiError> {
let mut count = 0;
for chunk in buf.chunks(64) {
let mut hsfsts_ctl;
loop {
hsfsts_ctl = self.hsfsts_ctl();
if ! hsfsts_ctl.contains(HsfStsCtl::H_SCIP) {
break;
}
}
hsfsts_ctl.set_cycle(HsfStsCtlCycle::Write);
hsfsts_ctl.set_count(chunk.len() as u8);
hsfsts_ctl.insert(HsfStsCtl::FGO);
for (i, dword) in chunk.chunks(4).enumerate() {
let mut data = 0;
for (j, byte) in dword.iter().enumerate() {
data |= (*byte as u32) << (j * 8);
}
self.fdata[i].write(data);
}
self.faddr.write((address + count) as u32);
self.set_hsfsts_ctl(hsfsts_ctl);
loop {
hsfsts_ctl = self.hsfsts_ctl();
if hsfsts_ctl.contains(HsfStsCtl::FCERR) {
return Err(SpiError::Cycle);
}
if hsfsts_ctl.contains(HsfStsCtl::FDONE) {
break;
}
}
count += chunk.len()
}
Ok(count)
}
}
#[cfg(test)]
mod tests {
use super::SpiSkl;
#[test]
fn offsets() {
unsafe {
let spi: &SpiSkl = &*(0 as *const SpiSkl);
assert_eq!(&spi.bfpreg as *const _ as usize, 0x00);
assert_eq!(&spi.freg as *const _ as usize, 0x54);
assert_eq!(&spi.fpr as *const _ as usize, 0x84);
assert_eq!(&spi.gpr as *const _ as usize, 0x98);
assert_eq!(&spi.sfracc as *const _ as usize, 0xb0);
assert_eq!(&spi.fdod as *const _ as usize, 0xb8);
assert_eq!(&spi.afc as *const _ as usize, 0xc0);
assert_eq!(&spi.sbrs as *const _ as usize, 0xd4);
}
}
}