sam4l/
flashcalw.rs

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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Implementation of the SAM4L flash controller.
//!
//! This implementation of the flash controller for the SAM4L uses interrupts to
//! handle main tasks of a flash -- write, reads, and erases. If modifying this
//! file, you should check whether the flash commands (issued via issue_command)
//! generates an interrupt and design a higher level function based off of that.
//!
//! Although the datasheet says that when the FRDY interrupt is on, an interrupt
//! will be generated after a command is complete, it doesn't appear to occur
//! for some commands.
//!
//! A clean interface for reading from flash, writing pages and erasing pages is
//! defined below and should be used to handle the complexity of these tasks.
//!
//! A Client should be set to enable a callback after a command is completed.
//!
//! Almost all of the flash controller functionality is implemented (except for
//! general purpose fuse bits, and more granular control of the cache).
//!
//! - Author:  Kevin Baichoo <kbaichoo@cs.stanford.edu>
//! - Date: July 27, 2016

use crate::pm;
use core::cell::Cell;
use core::ops::{Index, IndexMut};
use kernel::deferred_call::{DeferredCall, DeferredCallClient};
use kernel::hil;
use kernel::utilities::cells::{OptionalCell, TakeCell};
use kernel::utilities::registers::interfaces::{ReadWriteable, Readable, Writeable};
use kernel::utilities::registers::{register_bitfields, ReadOnly, ReadWrite, WriteOnly};
use kernel::utilities::StaticRef;
use kernel::ErrorCode;

/// Struct of the FLASHCALW registers. Section 14.10 of the datasheet.
#[repr(C)]
struct FlashcalwRegisters {
    fcr: ReadWrite<u32, FlashControl::Register>,
    fcmd: ReadWrite<u32, FlashCommand::Register>,
    fsr: ReadOnly<u32, FlashStatus::Register>,
    fpr: ReadOnly<u32, FlashParameter::Register>,
    fvr: ReadOnly<u32, FlashVersion::Register>,
    fgpfrhi: ReadOnly<u32, FlashGeneralPurposeFuseHigh::Register>,
    fgpfrlo: ReadOnly<u32, FlashGeneralPurposeFuseLow::Register>,
    _reserved1: [u32; 251],
    ctrl: WriteOnly<u32, PicoCacheControl::Register>,
    sr: ReadWrite<u32, PicoCacheStatus::Register>,
    _reserved2: [u32; 4],
    maint0: WriteOnly<u32, PicoCacheMaintenance0::Register>,
    maint1: WriteOnly<u32, PicoCacheMaintenance1::Register>,
    mcfg: ReadWrite<u32, PicoCacheMonitorConfiguration::Register>,
    men: ReadWrite<u32, PicoCacheMonitorEnable::Register>,
    mctrl: WriteOnly<u32, PicoCacheMonitorStatus::Register>,
    msr: ReadOnly<u32, PicoCacheMonitorStatus::Register>,
}

register_bitfields![u32,
    FlashControl [
        /// Wait State 1 Optimization
        WS1OPT OFFSET(7) NUMBITS(1) [
            NoOptimize = 0,
            Optimize = 1
        ],
        /// Flash Wait State
        FWS OFFSET(6) NUMBITS(1) [
            ZeroWaitStates = 0,
            OneWaitState = 1
        ],
        /// ECC Error Interrupt Enable
        ECCE OFFSET(4) NUMBITS(1) [],
        /// Programming Error Interrupt Enable
        PROGE OFFSET(3) NUMBITS(1) [],
        /// Lock Error Interrupt Enable
        LOCKE OFFSET(2) NUMBITS(1) [],
        /// Flash Ready Interrupt Enable
        FRDY OFFSET(0) NUMBITS(1) []
    ],

    FlashCommand [
        /// Write protection key
        KEY OFFSET(24) NUMBITS(8) [],
        /// Page number
        PAGEN OFFSET(8) NUMBITS(16) [],
        /// Command
        CMD OFFSET(0) NUMBITS(6) [
            NOP = 0,
            WP = 1,
            EP = 2,
            CPB = 3,
            LP = 4,
            UP = 5,
            EA = 6,
            WGPB = 7,
            EGPB = 8,
            SSB = 9,
            PGPFB = 10,
            EAGPF = 11,
            QPR = 12,
            WUP = 13,
            EUP = 14,
            QPRUP = 15,
            HSEN = 16,
            HSDIS = 17
        ]
    ],

    FlashStatus [
        /// Lock region x Lock Status
        LOCK15 31,
        LOCK14 30,
        LOCK13 29,
        LOCK12 28,
        LOCK11 27,
        LOCK10 26,
        LOCK9 25,
        LOCK8 24,
        LOCK7 23,
        LOCK6 22,
        LOCK5 21,
        LOCK4 20,
        LOCK3 19,
        LOCK2 18,
        LOCK1 17,
        LOCK0 16,
        ///ECC Error Status
        ///
        /// WARNING! Datasheet has this bit listed in two places...
        ECCERR 9,
        /// High Speed Mode
        HSMODE 6,
        /// Quick Page Read Result
        QPRR 5,
        /// Security Fuses Status
        SECURITY 4,
        /// Programming Error Status
        PROGE 3,
        /// Lock Error Status
        LOCKE 2,
        /// Flash Ready Status
        FRDY 0
    ],

    FlashParameter [
        /// Page Size
        PSZ OFFSET(8) NUMBITS(3) [
            Bytes32 = 0,
            Bytes64 = 1,
            Bytes128 = 2,
            Bytes256 = 3,
            Bytes512 = 4,
            Bytes1024 = 5,
            Bytes2048 = 6,
            Bytes4096 = 7
        ],
        /// Flash Size
        FSZ OFFSET(0) NUMBITS(4) [
            KBytes4 = 0,
            KBytes8 = 1,
            KBytes16 = 2,
            KBytes32 = 3,
            KBytes48 = 4,
            KBytes64 = 5,
            KBytes96 = 6,
            KBytes128 = 7,
            KBytes192 = 8,
            KBytes256 = 9,
            KBytes384 = 10,
            KBytes512 = 11,
            KBytes768 = 12,
            KBytes1024 = 13,
            KBytes2048 = 14
        ]
    ],

    FlashVersion [
        /// Variant Number
        VARIANT OFFSET(16) NUMBITS(4) [],
        /// Version Number
        VERSION OFFSET(0) NUMBITS(12) []
    ],

    FlashGeneralPurposeFuseHigh [
        /// General Purpose Fuse
        GPF63 31,
        GPF62 30,
        GPF61 29,
        GPF60 28,
        GPF59 27,
        GPF58 26,
        GPF57 25,
        GPF56 24,
        GPF55 23,
        GPF54 22,
        GPF53 21,
        GPF52 20,
        GPF51 19,
        GPF50 18,
        GPF49 17,
        GPF48 16,
        GPF47 15,
        GPF46 14,
        GPF45 13,
        GPF44 12,
        GPF43 11,
        GPF42 10,
        GPF41 9,
        GPF40 8,
        GPF39 7,
        GPF38 6,
        GPF37 5,
        GPF36 4,
        GPF35 3,
        GPF34 2,
        GPF33 1,
        GPF32 0
    ],

    FlashGeneralPurposeFuseLow [
        GPF31 31,
        GPF30 30,
        GPF29 29,
        GPF28 28,
        GPF27 27,
        GPF26 26,
        GPF25 25,
        GPF24 24,
        GPF23 23,
        GPF22 22,
        GPF21 21,
        GPF20 20,
        GPF19 19,
        GPF18 18,
        GPF17 17,
        GPF16 16,
        GPF15 15,
        GPF14 14,
        GPF13 13,
        GPF12 12,
        GPF11 11,
        GPF10 10,
        GPF9 9,
        GPF8 8,
        GPF7 7,
        GPF6 6,
        GPF5 5,
        GPF4 4,
        GPF3 3,
        GPF2 2,
        GPF1 1,
        GPF0 0
    ],

    PicoCacheControl [
        /// Cache Enable
        CEN OFFSET(0) NUMBITS(1) [
            Disable = 0,
            Enable = 1
        ]
    ],

    PicoCacheStatus [
        /// Cache Controller Status
        CSTS OFFSET(0) NUMBITS(1) [
            Disabled = 0,
            Enabled = 1
        ]
    ],

    PicoCacheMaintenance0 [
        /// Cache Controller Invalidate All
        INVALL 0
    ],

    PicoCacheMaintenance1 [
        /// Invalidate Index
        INDEX OFFSET(4) NUMBITS(4) []
    ],

    PicoCacheMonitorConfiguration [
        /// Cache Controller Monitor Counter Mode
        MODE OFFSET(0) NUMBITS(1) [
            CycleCount = 0,
            IhitCount = 1,
            DhitCount = 2
        ]
    ],

    PicoCacheMonitorEnable [
        /// Monitor Enable
        MENABLE OFFSET(0) NUMBITS(1) [
            Disable = 0,
            Enable = 1
        ]
    ],

    PicoCacheMonitorControl [
        /// Monitor Software Reset
        SWRST 0
    ],

    PicoCacheMonitorStatus [
        /// Monitor Event Counter
        EVENTCNT OFFSET(0) NUMBITS(32) []
    ]
];

const FLASHCALW_ADDRESS: StaticRef<FlashcalwRegisters> =
    unsafe { StaticRef::new(0x400A0000 as *const FlashcalwRegisters) };

#[allow(dead_code)]
enum RegKey {
    CONTROL,
    COMMAND,
    STATUS,
    PARAMETER,
    VERSION,
    GPFRHI,
    GPFRLO,
}

/// There are 18 recognized commands for the flash. These are "bare-bones"
/// commands and values that are written to the Flash's command register to
/// inform the flash what to do. Table 14-5.
#[derive(Clone, Copy, PartialEq)]
#[allow(dead_code)]
enum FlashCMD {
    NOP,
    WP,
    EP,
    CPB,
    LP,
    UP,
    EA,
    WGPB,
    EGPB,
    SSB,
    PGPFB,
    EAGPF,
    QPR,
    WUP,
    EUP,
    QPRUP,
    HSEN,
    HSDIS,
}

/// FlashState is used to track the current state and command of the flash.
#[derive(Clone, Copy, PartialEq)]
enum FlashState {
    Unconfigured,                 // Flash is unconfigured, call configure().
    Ready,                        // Flash is ready to complete a command.
    Read,                         // Performing a read operation.
    WriteUnlocking { page: i32 }, // Started a write operation.
    WriteErasing { page: i32 },   // Waiting on the page to erase.
    WriteWriting,                 // Waiting on the page to actually be written.
    EraseUnlocking { page: i32 }, // Started an erase operation.
    EraseErasing,                 // Waiting on the erase to finish.
}

/// This is a wrapper around a u8 array that is sized to a single page for the
/// SAM4L. Users of this module must pass an object of this type to use the
/// `hil::flash::Flash` interface.
///
/// An example looks like:
///
/// ```rust
/// # extern crate sam4l;
/// # use sam4l::flashcalw::Sam4lPage;
/// # use kernel::static_init;
///
/// let pagebuffer = unsafe { static_init!(Sam4lPage, Sam4lPage::default()) };
/// ```
pub struct Sam4lPage(pub [u8; PAGE_SIZE as usize]);

impl Default for Sam4lPage {
    fn default() -> Self {
        Self([0; PAGE_SIZE as usize])
    }
}

impl Sam4lPage {
    fn len(&self) -> usize {
        self.0.len()
    }
}

impl Index<usize> for Sam4lPage {
    type Output = u8;

    fn index(&self, idx: usize) -> &u8 {
        &self.0[idx]
    }
}

impl IndexMut<usize> for Sam4lPage {
    fn index_mut(&mut self, idx: usize) -> &mut u8 {
        &mut self.0[idx]
    }
}

impl AsMut<[u8]> for Sam4lPage {
    fn as_mut(&mut self) -> &mut [u8] {
        &mut self.0
    }
}

// The FLASHCALW controller
pub struct FLASHCALW {
    registers: StaticRef<FlashcalwRegisters>,
    ahb_clock: pm::Clock,
    hramc1_clock: pm::Clock,
    pb_clock: pm::Clock,
    client: OptionalCell<&'static dyn hil::flash::Client<FLASHCALW>>,
    current_state: Cell<FlashState>,
    buffer: TakeCell<'static, Sam4lPage>,
    deferred_call: DeferredCall,
}

// Few constants relating to module configuration.
const PAGE_SIZE: u32 = 512;

const FREQ_PS2_FWS_0_MAX_FREQ: u32 = 24000000;

impl FLASHCALW {
    pub fn new(ahb_clk: pm::HSBClock, hramc1_clk: pm::HSBClock, pb_clk: pm::PBBClock) -> FLASHCALW {
        FLASHCALW {
            registers: FLASHCALW_ADDRESS,
            ahb_clock: pm::Clock::HSB(ahb_clk),
            hramc1_clock: pm::Clock::HSB(hramc1_clk),
            pb_clock: pm::Clock::PBB(pb_clk),
            client: OptionalCell::empty(),
            current_state: Cell::new(FlashState::Unconfigured),
            buffer: TakeCell::empty(),
            deferred_call: DeferredCall::new(),
        }
    }

    /// Cache controlling functionality.

    //  Flush the cache. Should be called after every write!
    fn invalidate_cache(&self) {
        self.registers
            .maint0
            .write(PicoCacheMaintenance0::INVALL::SET);
    }

    fn enable_picocache(&self, enable: bool) {
        if enable {
            self.registers.ctrl.write(PicoCacheControl::CEN::Enable);
        } else {
            self.registers.ctrl.write(PicoCacheControl::CEN::Disable);
        }
    }

    /// Enable HCACHE
    pub fn enable_cache(&self) {
        // enable appropriate clocks
        pm::enable_clock(pm::Clock::HSB(pm::HSBClock::FLASHCALWP));
        pm::enable_clock(pm::Clock::PBB(pm::PBBClock::HRAMC1));

        // enable and wait for it to be ready
        self.enable_picocache(true);
        while !self.pico_enabled() {}
    }

    fn pico_enabled(&self) -> bool {
        self.registers.sr.is_set(PicoCacheStatus::CSTS)
    }

    pub fn handle_interrupt(&self) {
        // Disable the interrupt line for flash
        self.registers.fcr.modify(FlashControl::FRDY::CLEAR);

        // Since the only interrupt on is FRDY, a command should have
        // either completed or failed at this point.

        // Check for errors and report to Client if there are any
        if self.is_error() {
            let attempted_operation = self.current_state.get();

            // Reset state now that we are ready to do a new operation.
            self.current_state.set(FlashState::Ready);

            self.client.map(|client| match attempted_operation {
                FlashState::Read => {
                    self.buffer.take().map(|buffer| {
                        client.read_complete(buffer, Err(hil::flash::Error::FlashError));
                    });
                }
                FlashState::WriteUnlocking { .. }
                | FlashState::WriteErasing { .. }
                | FlashState::WriteWriting => {
                    self.buffer.take().map(|buffer| {
                        client.write_complete(buffer, Err(hil::flash::Error::FlashError));
                    });
                }
                FlashState::EraseUnlocking { .. } | FlashState::EraseErasing => {
                    client.erase_complete(Err(hil::flash::Error::FlashError));
                }
                _ => {}
            });
        }

        // Part of a command succeeded -- continue onto next steps.
        match self.current_state.get() {
            FlashState::Read => {
                self.current_state.set(FlashState::Ready);

                self.client.map(|client| {
                    self.buffer.take().map(|buffer| {
                        client.read_complete(buffer, Ok(()));
                    });
                });
            }
            FlashState::WriteUnlocking { page } => {
                self.current_state.set(FlashState::WriteErasing { page });
                self.flashcalw_erase_page(page);
            }
            FlashState::WriteErasing { page } => {
                //  Write page buffer isn't really a command, and
                //  clear page buffer doesn't trigger an interrupt thus
                //  I'm combining these with an actual command, write_page,
                //  which generates and interrupt and saves the page.
                self.clear_page_buffer();
                self.write_to_page_buffer(page as usize * PAGE_SIZE as usize);

                self.current_state.set(FlashState::WriteWriting);
                self.flashcalw_write_page(page);
            }
            FlashState::WriteWriting => {
                // Flush the cache
                self.invalidate_cache();

                self.current_state.set(FlashState::Ready);

                self.client.map(|client| {
                    self.buffer.take().map(|buffer| {
                        client.write_complete(buffer, Ok(()));
                    });
                });
            }
            FlashState::EraseUnlocking { page } => {
                self.current_state.set(FlashState::EraseErasing);
                self.flashcalw_erase_page(page);
            }
            FlashState::EraseErasing => {
                // Flush the cache
                self.invalidate_cache();

                self.current_state.set(FlashState::Ready);

                self.client.map(|client| {
                    client.erase_complete(Ok(()));
                });
            }
            _ => {
                self.current_state.set(FlashState::Ready);
            }
        }
    }

    /// FLASH properties.
    fn get_flash_size(&self) -> u32 {
        let flash_sizes = [
            4, 8, 16, 32, 48, 64, 96, 128, 192, 256, 384, 512, 768, 1024, 2048,
        ];
        // get the FSZ number and lookup in the table for the size.
        flash_sizes[self.registers.fpr.read(FlashParameter::FSZ) as usize] << 10
    }

    /// FLASHC Control
    pub fn set_wait_state(&self, wait_state: u32) {
        self.registers.fcr.modify(FlashControl::FWS.val(wait_state));
    }

    fn enable_ws1_read_opt(&self, enable: bool) {
        if enable {
            self.registers.fcr.modify(FlashControl::WS1OPT::Optimize);
        } else {
            self.registers.fcr.modify(FlashControl::WS1OPT::NoOptimize);
        }
    }

    //  By default, we are going with High Speed Enable (based on our device running
    //  in PS2).
    fn set_flash_waitstate_and_readmode(&self, cpu_freq: u32, _ps_val: u32, _is_fwu_enabled: bool) {
        // ps_val and is_fwu_enabled not used in this implementation.
        if cpu_freq > FREQ_PS2_FWS_0_MAX_FREQ {
            self.set_wait_state(1);
        } else {
            self.set_wait_state(0);
        }

        self.issue_command(FlashCMD::HSEN, -1);
    }

    /// Configure high-speed flash mode. This is taken from the ASF code
    pub fn enable_high_speed_flash(&self) {
        // Since we are running at a fast speed we have to set a clock delay
        // for flash, as well as enable fast flash mode.
        self.registers.fcr.modify(FlashControl::FWS::OneWaitState);

        // Enable high speed mode for flash
        self.registers
            .fcmd
            .modify(FlashCommand::KEY.val(0xA5) + FlashCommand::CMD::HSEN);

        // And wait for the flash to be ready
        while !self.registers.fsr.is_set(FlashStatus::FRDY) {}
    }

    /// Flashcalw status
    fn is_error(&self) -> bool {
        pm::enable_clock(self.pb_clock);
        self.registers.fsr.is_set(FlashStatus::LOCKE)
            | self.registers.fsr.is_set(FlashStatus::PROGE)
    }

    /// Flashcalw command control
    fn issue_command(&self, command: FlashCMD, page_number: i32) {
        pm::enable_clock(self.pb_clock);
        // For most commands we wait for the interrupt, for some certain
        // fast/rarely used commands or commands that don't generate interrupts
        // it is better to wait (or at least that is how this driver was
        // originally implemented).
        if command != FlashCMD::QPRUP
            && command != FlashCMD::QPR
            && command != FlashCMD::CPB
            && command != FlashCMD::HSEN
        {
            // Enable ready interrupt.
            self.registers.fcr.modify(FlashControl::FRDY::SET);
        }

        // Setup the command register to run this command.
        let mut cmd = FlashCommand::KEY.val(0xA5) + FlashCommand::CMD.val(command as u32);

        // If this command relies on using a certain page, we need to add that
        // in as well.
        if page_number >= 0 {
            cmd += FlashCommand::PAGEN.val(page_number as u32);
        }

        self.registers.fcmd.write(cmd);

        // Since we don't enable interrupts for these commands, spin wait
        // until they are finished. In particular, QPR and QPRUP will not issue
        // interrupts (see datasheet 14.6 paragraph 2).
        if command == FlashCMD::QPRUP
            || command == FlashCMD::QPR
            || command == FlashCMD::CPB
            || command == FlashCMD::HSEN
        {
            while !self.registers.fsr.is_set(FlashStatus::FRDY) {}
        }
    }

    /// Flashcalw global commands
    fn lock_page_region(&self, page_number: i32, lock: bool) {
        if lock {
            self.issue_command(FlashCMD::LP, page_number);
        } else {
            self.issue_command(FlashCMD::UP, page_number);
        }
    }

    /// Flashcalw Access to Flash Pages
    fn clear_page_buffer(&self) {
        self.issue_command(FlashCMD::CPB, -1);
    }

    fn is_page_erased(&self) -> bool {
        self.registers.fsr.is_set(FlashStatus::QPRR)
    }

    fn flashcalw_erase_page(&self, page_number: i32) {
        self.issue_command(FlashCMD::EP, page_number);
    }

    fn flashcalw_write_page(&self, page_number: i32) {
        self.issue_command(FlashCMD::WP, page_number);
    }

    /// There's a user_page that isn't contiguous with the rest of the flash.
    /// Currently it's not being used.
    #[allow(dead_code)]
    fn quick_user_page_read(&self) -> bool {
        self.issue_command(FlashCMD::QPRUP, -1);
        self.is_page_erased()
    }

    #[allow(dead_code)]
    fn erase_user_page(&self, check: bool) -> bool {
        self.issue_command(FlashCMD::EUP, -1);
        if check {
            self.quick_user_page_read()
        } else {
            true
        }
    }

    #[allow(dead_code)]
    fn write_user_page(&self) {
        self.issue_command(FlashCMD::WUP, -1);
    }

    // Instead of having several memset/memcpy functions as Atmel's ASF
    // implementation will only have one to write to the page buffer.
    fn write_to_page_buffer(&self, pg_buff_addr: usize) {
        let mut page_buffer: *mut u8 = pg_buff_addr as *mut u8;

        // Errata 45.1.7 - Need to write a 64-bit all one word for every write
        // to the page buffer.
        let cleared_double_word: [u8; 8] = [255; 8];
        let clr_ptr: *const u8 = &cleared_double_word[0] as *const u8;

        self.buffer.map(|buffer| {
            unsafe {
                use core::ptr;

                let mut start_buffer: *const u8 = &buffer[0] as *const u8;
                let mut data_transfered: u32 = 0;
                while data_transfered < PAGE_SIZE {
                    // errata copy..
                    ptr::copy(clr_ptr, page_buffer, 8);

                    // real copy
                    ptr::copy(start_buffer, page_buffer, 8);
                    page_buffer = page_buffer.offset(8);
                    start_buffer = start_buffer.offset(8);
                    data_transfered += 8;
                }
            }
        });
    }
}

// Implementation of high level calls using the low-lv functions.
impl FLASHCALW {
    pub fn configure(&self) {
        // Enable all clocks (if they aren't on already...).
        pm::enable_clock(self.ahb_clock);
        pm::enable_clock(self.hramc1_clock);
        pm::enable_clock(self.pb_clock);

        // Configure all other interrupts explicitly. Note the issue_command
        // function turns this on when need be.
        self.registers.fcr.modify(
            FlashControl::FRDY::CLEAR
                + FlashControl::LOCKE::CLEAR
                + FlashControl::PROGE::CLEAR
                + FlashControl::ECCE::CLEAR,
        );

        // Enable wait state 1 optimization.
        self.enable_ws1_read_opt(true);
        // Change speed mode.
        self.set_flash_waitstate_and_readmode(48_000_000, 0, false);

        // By default the picocache ( a cache only for the flash) is turned off.
        // However the bootloader turns it on. I will explicitly turn it on
        // here. So if the bootloader changes, nothing breaks.
        self.enable_picocache(true);

        self.current_state.set(FlashState::Ready);
    }

    // Address is some raw address in flash that you want to read.
    fn read_range(
        &self,
        address: usize,
        size: usize,
        buffer: &'static mut Sam4lPage,
    ) -> Result<(), (ErrorCode, &'static mut Sam4lPage)> {
        if self.current_state.get() == FlashState::Unconfigured {
            self.configure();
        }

        // Enable clock in case it's off.
        pm::enable_clock(self.ahb_clock);

        // Check that address makes sense and buffer has room.
        let Some(end_address) = address.checked_add(size) else {
            return Err((ErrorCode::INVAL, buffer));
        };
        if end_address > (self.get_flash_size() as usize) || buffer.len() < size {
            return Err((ErrorCode::INVAL, buffer));
        }

        // Actually do a copy from flash into the buffer.
        let mut byte: *const u8 = address as *const u8;
        unsafe {
            for i in 0..size {
                buffer[i] = *byte;
                byte = byte.offset(1);
            }
        }

        self.current_state.set(FlashState::Read);
        // Hold on to the buffer for the callback.
        self.buffer.replace(buffer);

        // This is kind of strange, but because read() in this case is
        // synchronous, we still need to schedule as if we had an interrupt so
        // we can allow this function to return and then call the callback.
        self.deferred_call.set();

        Ok(())
    }

    fn write_page(
        &self,
        page_num: i32,
        data: &'static mut Sam4lPage,
    ) -> Result<(), (ErrorCode, &'static mut Sam4lPage)> {
        // Enable clock in case it's off.
        pm::enable_clock(self.ahb_clock);

        match self.current_state.get() {
            FlashState::Unconfigured => self.configure(),
            FlashState::Ready => {}
            // If we're not ready don't take the command
            _ => return Err((ErrorCode::BUSY, data)),
        }

        // Save the buffer for the future write.
        self.buffer.replace(data);

        self.current_state
            .set(FlashState::WriteUnlocking { page: page_num });
        self.lock_page_region(page_num, false);
        Ok(())
    }

    fn erase_page(&self, page_num: i32) -> Result<(), ErrorCode> {
        // Enable AHB clock (in case it was off).
        pm::enable_clock(self.ahb_clock);

        match self.current_state.get() {
            FlashState::Unconfigured => self.configure(),
            FlashState::Ready => {}
            // If we're not ready don't take the command
            _ => return Err(ErrorCode::BUSY),
        }

        self.current_state
            .set(FlashState::EraseUnlocking { page: page_num });
        self.lock_page_region(page_num, false);
        Ok(())
    }
}

impl DeferredCallClient for FLASHCALW {
    fn handle_deferred_call(&self) {
        self.handle_interrupt();
    }

    fn register(&'static self) {
        self.deferred_call.register(self);
    }
}

impl<C: hil::flash::Client<Self>> hil::flash::HasClient<'static, C> for FLASHCALW {
    fn set_client(&self, client: &'static C) {
        self.client.set(client);
    }
}

impl hil::flash::Flash for FLASHCALW {
    type Page = Sam4lPage;

    fn read_page(
        &self,
        page_number: usize,
        buf: &'static mut Self::Page,
    ) -> Result<(), (ErrorCode, &'static mut Self::Page)> {
        self.read_range(page_number * (PAGE_SIZE as usize), buf.len(), buf)
    }

    fn write_page(
        &self,
        page_number: usize,
        buf: &'static mut Self::Page,
    ) -> Result<(), (ErrorCode, &'static mut Self::Page)> {
        self.write_page(page_number as i32, buf)
    }

    fn erase_page(&self, page_number: usize) -> Result<(), ErrorCode> {
        self.erase_page(page_number as i32)
    }
}