summaryrefslogtreecommitdiffstats
path: root/tmk_core/common/chibios/eeprom_teensy.c
blob: 97da6f9e1483cf1ae82852835e0c9c3855113a64 (plain)
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
#include <ch.h>
#include <hal.h>

#include "eeconfig.h"

/*************************************/
/*          Hardware backend         */
/*                                   */
/*    Code from PJRC/Teensyduino     */
/*************************************/

/* Teensyduino Core Library
 * http://www.pjrc.com/teensy/
 * Copyright (c) 2013 PJRC.COM, LLC.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * 1. The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * 2. If the Software is incorporated into a build system that allows
 * selection among a list of target devices, then similar target
 * devices manufactured by PJRC.COM must be included in the list of
 * target devices and selectable in the same manner.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#define SMC_PMSTAT_RUN ((uint8_t)0x01)
#define SMC_PMSTAT_HSRUN ((uint8_t)0x80)

#define F_CPU KINETIS_SYSCLK_FREQUENCY

static inline int kinetis_hsrun_disable(void) {
#if defined(MK66F18)
    if (SMC->PMSTAT == SMC_PMSTAT_HSRUN) {
// First, reduce the CPU clock speed, but do not change
// the peripheral speed (F_BUS).  Serial1 & Serial2 baud
// rates will be impacted, but most other peripherals
// will continue functioning at the same speed.
#    if F_CPU == 256000000 && F_BUS == 64000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 3, 1, 7);  // TODO: TEST
#    elif F_CPU == 256000000 && F_BUS == 128000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 7);  // TODO: TEST
#    elif F_CPU == 240000000 && F_BUS == 60000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 3, 1, 7);  // ok
#    elif F_CPU == 240000000 && F_BUS == 80000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 8);  // ok
#    elif F_CPU == 240000000 && F_BUS == 120000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 7);  // ok
#    elif F_CPU == 216000000 && F_BUS == 54000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 3, 1, 7);  // ok
#    elif F_CPU == 216000000 && F_BUS == 72000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 8);  // ok
#    elif F_CPU == 216000000 && F_BUS == 108000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 7);  // ok
#    elif F_CPU == 192000000 && F_BUS == 48000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 3, 1, 7);  // ok
#    elif F_CPU == 192000000 && F_BUS == 64000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 8);  // ok
#    elif F_CPU == 192000000 && F_BUS == 96000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 7);  // ok
#    elif F_CPU == 180000000 && F_BUS == 60000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 8);  // ok
#    elif F_CPU == 180000000 && F_BUS == 90000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 7);  // ok
#    elif F_CPU == 168000000 && F_BUS == 56000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 5);  // ok
#    elif F_CPU == 144000000 && F_BUS == 48000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 5);  // ok
#    elif F_CPU == 144000000 && F_BUS == 72000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 5);  // ok
#    elif F_CPU == 120000000 && F_BUS == 60000000
        SIM->CLKDIV1 = SIM_CLKDIV1_OUTDIV1(KINETIS_CLKDIV1_OUTDIV1 - 1) | SIM_CLKDIV1_OUTDIV2(KINETIS_CLKDIV1_OUTDIV2 - 1) |
#        if defined(MK66F18)
                       SIM_CLKDIV1_OUTDIV3(KINETIS_CLKDIV1_OUTDIV3 - 1) |
#        endif
                       SIM_CLKDIV1_OUTDIV4(KINETIS_CLKDIV1_OUTDIV4 - 1);
#    else
        return 0;
#    endif
        // Then turn off HSRUN mode
        SMC->PMCTRL = SMC_PMCTRL_RUNM_SET(0);
        while (SMC->PMSTAT == SMC_PMSTAT_HSRUN)
            ;  // wait
        return 1;
    }
#endif
    return 0;
}

static inline int kinetis_hsrun_enable(void) {
#if defined(MK66F18)
    if (SMC->PMSTAT == SMC_PMSTAT_RUN) {
        // Turn HSRUN mode on
        SMC->PMCTRL = SMC_PMCTRL_RUNM_SET(3);
        while (SMC->PMSTAT != SMC_PMSTAT_HSRUN) {
            ;
        }  // wait
// Then configure clock for full speed
#    if F_CPU == 256000000 && F_BUS == 64000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 3, 0, 7);
#    elif F_CPU == 256000000 && F_BUS == 128000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 7);
#    elif F_CPU == 240000000 && F_BUS == 60000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 3, 0, 7);
#    elif F_CPU == 240000000 && F_BUS == 80000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 7);
#    elif F_CPU == 240000000 && F_BUS == 120000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 7);
#    elif F_CPU == 216000000 && F_BUS == 54000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 3, 0, 7);
#    elif F_CPU == 216000000 && F_BUS == 72000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 7);
#    elif F_CPU == 216000000 && F_BUS == 108000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 7);
#    elif F_CPU == 192000000 && F_BUS == 48000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 3, 0, 6);
#    elif F_CPU == 192000000 && F_BUS == 64000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 6);
#    elif F_CPU == 192000000 && F_BUS == 96000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 6);
#    elif F_CPU == 180000000 && F_BUS == 60000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 6);
#    elif F_CPU == 180000000 && F_BUS == 90000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 6);
#    elif F_CPU == 168000000 && F_BUS == 56000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 5);
#    elif F_CPU == 144000000 && F_BUS == 48000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 4);
#    elif F_CPU == 144000000 && F_BUS == 72000000
        SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 4);
#    elif F_CPU == 120000000 && F_BUS == 60000000
        SIM->CLKDIV1 = SIM_CLKDIV1_OUTDIV1(KINETIS_CLKDIV1_OUTDIV1 - 1) | SIM_CLKDIV1_OUTDIV2(KINETIS_CLKDIV1_OUTDIV2 - 1) |
#        if defined(MK66F18)
                       SIM_CLKDIV1_OUTDIV3(KINETIS_CLKDIV1_OUTDIV3 - 1) |
#        endif
                       SIM_CLKDIV1_OUTDIV4(KINETIS_CLKDIV1_OUTDIV4 - 1);
#    else
        return 0;
#    endif
        return 1;
    }
#endif
    return 0;
}

#if defined(K20x) || defined(MK66F18) /* chip selection */
/* Teensy 3.0, 3.1, 3.2; mchck; infinity keyboard */

// The EEPROM is really RAM with a hardware-based backup system to
// flash memory.  Selecting a smaller size EEPROM allows more wear
// leveling, for higher write endurance.  If you edit this file,
// set this to the smallest size your application can use.  Also,
// due to Freescale's implementation, writing 16 or 32 bit words
// (aligned to 2 or 4 byte boundaries) has twice the endurance
// compared to writing 8 bit bytes.
//
#    ifndef EEPROM_SIZE
#        define EEPROM_SIZE 32
#    endif

/*
    ^^^ Here be dragons:
        NXP AppNote AN4282 section 3.1 states that partitioning must only be done once.
        Once EEPROM partitioning is done, the size is locked to this initial configuration.
        Attempts to modify the EEPROM_SIZE setting may brick your board.
*/

// Writing unaligned 16 or 32 bit data is handled automatically when
// this is defined, but at a cost of extra code size.  Without this,
// any unaligned write will cause a hard fault exception!  If you're
// absolutely sure all 16 and 32 bit writes will be aligned, you can
// remove the extra unnecessary code.
//
#    define HANDLE_UNALIGNED_WRITES

#    if defined(K20x)
#        define EEPROM_MAX 2048
#        define EEPARTITION 0x03  // all 32K dataflash for EEPROM, none for Data
#        define EEESPLIT 0x30     // must be 0x30 on these chips
#    elif defined(MK66F18)
#        define EEPROM_MAX 4096
#        define EEPARTITION 0x05  // 128K dataflash for EEPROM, 128K for Data
#        define EEESPLIT 0x10     // best endurance: 0x00 = first 12%, 0x10 = first 25%, 0x30 = all equal
#    endif

// Minimum EEPROM Endurance
// ------------------------
#    if (EEPROM_SIZE == 4096)
#        define EEESIZE 0x02
#    elif (EEPROM_SIZE == 2048)  // 35000 writes/byte or 70000 writes/word
#        define EEESIZE 0x03
#    elif (EEPROM_SIZE == 1024)  // 75000 writes/byte or 150000 writes/word
#        define EEESIZE 0x04
#    elif (EEPROM_SIZE == 512)  // 155000 writes/byte or 310000 writes/word
#        define EEESIZE 0x05
#    elif (EEPROM_SIZE == 256)  // 315000 writes/byte or 630000 writes/word
#        define EEESIZE 0x06
#    elif (EEPROM_SIZE == 128)  // 635000 writes/byte or 1270000 writes/word
#        define EEESIZE 0x07
#    elif (EEPROM_SIZE == 64)  // 1275000 writes/byte or 2550000 writes/word
#        define EEESIZE 0x08
#    elif (EEPROM_SIZE == 32)  // 2555000 writes/byte or 5110000 writes/word
#        define EEESIZE 0x09
#    endif

/** \brief eeprom initialization
 *
 * FIXME: needs doc
 */
void eeprom_initialize(void) {
    uint32_t count          = 0;
    uint16_t do_flash_cmd[] = {0xf06f, 0x037f, 0x7003, 0x7803, 0xf013, 0x0f80, 0xd0fb, 0x4770};
    uint8_t  status;

    if (FTFL->FCNFG & FTFL_FCNFG_RAMRDY) {
        uint8_t stat = FTFL->FSTAT & 0x70;
        if (stat) FTFL->FSTAT = stat;

        // FlexRAM is configured as traditional RAM
        // We need to reconfigure for EEPROM usage
        kinetis_hsrun_disable();
        FTFL->FCCOB0 = 0x80;  // PGMPART = Program Partition Command
        FTFL->FCCOB3 = 0;
        FTFL->FCCOB4 = EEESPLIT | EEESIZE;
        FTFL->FCCOB5 = EEPARTITION;
        __disable_irq();
        // do_flash_cmd() must execute from RAM.  Luckily the C syntax is simple...
        (*((void (*)(volatile uint8_t *))((uint32_t)do_flash_cmd | 1)))(&(FTFL->FSTAT));
        __enable_irq();
        kinetis_hsrun_enable();
        status = FTFL->FSTAT;
        if (status & (FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL)) {
            FTFL->FSTAT = (status & (FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL));
            return;  // error
        }
    }
    // wait for eeprom to become ready (is this really necessary?)
    while (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) {
        if (++count > 200000) break;
    }
}

#    define FlexRAM ((volatile uint8_t *)0x14000000)

/** \brief eeprom read byte
 *
 * FIXME: needs doc
 */
uint8_t eeprom_read_byte(const uint8_t *addr) {
    uint32_t offset = (uint32_t)addr;
    if (offset >= EEPROM_SIZE) return 0;
    if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
    return FlexRAM[offset];
}

/** \brief eeprom read word
 *
 * FIXME: needs doc
 */
uint16_t eeprom_read_word(const uint16_t *addr) {
    uint32_t offset = (uint32_t)addr;
    if (offset >= EEPROM_SIZE - 1) return 0;
    if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
    return *(uint16_t *)(&FlexRAM[offset]);
}

/** \brief eeprom read dword
 *
 * FIXME: needs doc
 */
uint32_t eeprom_read_dword(const uint32_t *addr) {
    uint32_t offset = (uint32_t)addr;
    if (offset >= EEPROM_SIZE - 3) return 0;
    if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
    return *(uint32_t *)(&FlexRAM[offset]);
}

/** \brief eeprom read block
 *
 * FIXME: needs doc
 */
void eeprom_read_block(void *buf, const void *addr, uint32_t len) {
    uint32_t offset = (uint32_t)addr;
    uint8_t *dest   = (uint8_t *)buf;
    uint32_t end    = offset + len;

    if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
    if (end > EEPROM_SIZE) end = EEPROM_SIZE;
    while (offset < end) {
        *dest++ = FlexRAM[offset++];
    }
}

/** \brief eeprom is ready
 *
 * FIXME: needs doc
 */
int eeprom_is_ready(void) { return (FTFL->FCNFG & FTFL_FCNFG_EEERDY) ? 1 : 0; }

/** \brief flexram wait
 *
 * FIXME: needs doc
 */
static void flexram_wait(void) {
    while (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) {
        // TODO: timeout
    }
}

/** \brief eeprom_write_byte
 *
 * FIXME: needs doc
 */
void eeprom_write_byte(uint8_t *addr, uint8_t value) {
    uint32_t offset = (uint32_t)addr;

    if (offset >= EEPROM_SIZE) return;
    if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
    if (FlexRAM[offset] != value) {
        kinetis_hsrun_disable();
        uint8_t stat = FTFL->FSTAT & 0x70;
        if (stat) FTFL->FSTAT = stat;
        FlexRAM[offset] = value;
        flexram_wait();
        kinetis_hsrun_enable();
    }
}

/** \brief eeprom write word
 *
 * FIXME: needs doc
 */
void eeprom_write_word(uint16_t *addr, uint16_t value) {
    uint32_t offset = (uint32_t)addr;

    if (offset >= EEPROM_SIZE - 1) return;
    if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
#    ifdef HANDLE_UNALIGNED_WRITES
    if ((offset & 1) == 0) {
#    endif
        if (*(uint16_t *)(&FlexRAM[offset]) != value) {
            kinetis_hsrun_disable();
            uint8_t stat = FTFL->FSTAT & 0x70;
            if (stat) FTFL->FSTAT = stat;
            *(uint16_t *)(&FlexRAM[offset]) = value;
            flexram_wait();
            kinetis_hsrun_enable();
        }
#    ifdef HANDLE_UNALIGNED_WRITES
    } else {
        if (FlexRAM[offset] != value) {
            kinetis_hsrun_disable();
            uint8_t stat = FTFL->FSTAT & 0x70;
            if (stat) FTFL->FSTAT = stat;
            FlexRAM[offset] = value;
            flexram_wait();
            kinetis_hsrun_enable();
        }
        if (FlexRAM[offset + 1] != (value >> 8)) {
            kinetis_hsrun_disable();
            uint8_t stat = FTFL->FSTAT & 0x70;
            if (stat) FTFL->FSTAT = stat;
            FlexRAM[offset + 1] = value >> 8;
            flexram_wait();
            kinetis_hsrun_enable();
        }
    }
#    endif
}

/** \brief eeprom write dword
 *
 * FIXME: needs doc
 */
void eeprom_write_dword(uint32_t *addr, uint32_t value) {
    uint32_t offset = (uint32_t)addr;

    if (offset >= EEPROM_SIZE - 3) return;
    if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
#    ifdef HANDLE_UNALIGNED_WRITES
    switch (offset & 3) {
        case 0:
#    endif
            if (*(uint32_t *)(&FlexRAM[offset]) != value) {
                kinetis_hsrun_disable();
                uint8_t stat = FTFL->FSTAT & 0x70;
                if (stat) FTFL->FSTAT = stat;
                *(uint32_t *)(&FlexRAM[offset]) = value;
                flexram_wait();
                kinetis_hsrun_enable();
            }
            return;
#    ifdef HANDLE_UNALIGNED_WRITES
        case 2:
            if (*(uint16_t *)(&FlexRAM[offset]) != value) {
                kinetis_hsrun_disable();
                uint8_t stat = FTFL->FSTAT & 0x70;
                if (stat) FTFL->FSTAT = stat;
                *(uint16_t *)(&FlexRAM[offset]) = value;
                flexram_wait();
                kinetis_hsrun_enable();
            }
            if (*(uint16_t *)(&FlexRAM[offset + 2]) != (value >> 16)) {
                kinetis_hsrun_disable();
                uint8_t stat = FTFL->FSTAT & 0x70;
                if (stat) FTFL->FSTAT = stat;
                *(uint16_t *)(&FlexRAM[offset + 2]) = value >> 16;
                flexram_wait();
                kinetis_hsrun_enable();
            }
            return;
        default:
            if (FlexRAM[offset] != value) {
                kinetis_hsrun_disable();
                uint8_t stat = FTFL->FSTAT & 0x70;
                if (stat) FTFL->FSTAT = stat;
                FlexRAM[offset] = value;
                flexram_wait();
                kinetis_hsrun_enable();
            }
            if (*(uint16_t *)(&FlexRAM[offset + 1]) != (value >> 8)) {
                kinetis_hsrun_disable();
                uint8_t stat = FTFL->FSTAT & 0x70;
                if (stat) FTFL->FSTAT = stat;
                *(uint16_t *)(&FlexRAM[offset + 1]) = value >> 8;
                flexram_wait();
                kinetis_hsrun_enable();
            }
            if (FlexRAM[offset + 3] != (value >> 24)) {
                kinetis_hsrun_disable();
                uint8_t stat = FTFL->FSTAT & 0x70;
                if (stat) FTFL->FSTAT = stat;
                FlexRAM[offset + 3] = value >> 24;
                flexram_wait();
                kinetis_hsrun_enable();
            }
    }
#    endif
}

/** \brief eeprom write block
 *
 * FIXME: needs doc
 */
void eeprom_write_block(const void *buf, void *addr, uint32_t len) {
    uint32_t       offset = (uint32_t)addr;
    const uint8_t *src    = (const uint8_t *)buf;

    if (offset >= EEPROM_SIZE) return;
    if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
    if (len >= EEPROM_SIZE) len = EEPROM_SIZE;
    if (offset + len >= EEPROM_SIZE) len = EEPROM_SIZE - offset;
    kinetis_hsrun_disable();
    while (len > 0) {
        uint32_t lsb = offset & 3;
        if (lsb == 0 && len >= 4) {
            // write aligned 32 bits
            uint32_t val32;
            val32 = *src++;
            val32 |= (*src++ << 8);
            val32 |= (*src++ << 16);
            val32 |= (*src++ << 24);
            if (*(uint32_t *)(&FlexRAM[offset]) != val32) {
                uint8_t stat = FTFL->FSTAT & 0x70;
                if (stat) FTFL->FSTAT = stat;
                *(uint32_t *)(&FlexRAM[offset]) = val32;
                flexram_wait();
            }
            offset += 4;
            len -= 4;
        } else if ((lsb == 0 || lsb == 2) && len >= 2) {
            // write aligned 16 bits
            uint16_t val16;
            val16 = *src++;
            val16 |= (*src++ << 8);
            if (*(uint16_t *)(&FlexRAM[offset]) != val16) {
                uint8_t stat = FTFL->FSTAT & 0x70;
                if (stat) FTFL->FSTAT = stat;
                *(uint16_t *)(&FlexRAM[offset]) = val16;
                flexram_wait();
            }
            offset += 2;
            len -= 2;
        } else {
            // write 8 bits
            uint8_t val8 = *src++;
            if (FlexRAM[offset] != val8) {
                uint8_t stat = FTFL->FSTAT & 0x70;
                if (stat) FTFL->FSTAT = stat;
                FlexRAM[offset] = val8;
                flexram_wait();
            }
            offset++;
            len--;
        }
    }
    kinetis_hsrun_enable();
}

/*
void do_flash_cmd(volatile uint8_t *fstat)
{
    *fstat = 0x80;
    while ((*fstat & 0x80) == 0) ; // wait
}
00000000 <do_flash_cmd>:
   0:	f06f 037f 	mvn.w	r3, #127	; 0x7f
   4:	7003      	strb	r3, [r0, #0]
   6:	7803      	ldrb	r3, [r0, #0]
   8:	f013 0f80 	tst.w	r3, #128	; 0x80
   c:	d0fb      	beq.n	6 <do_flash_cmd+0x6>
   e:	4770      	bx	lr
*/

#elif defined(KL2x) /* chip selection */
/* Teensy LC (emulated) */

#    define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0))

extern uint32_t __eeprom_workarea_start__;
extern uint32_t __eeprom_workarea_end__;

#    define EEPROM_SIZE 128

static uint32_t flashend = 0;

void eeprom_initialize(void) {
    const uint16_t *p = (uint16_t *)SYMVAL(__eeprom_workarea_start__);

    do {
        if (*p++ == 0xFFFF) {
            flashend = (uint32_t)(p - 2);
            return;
        }
    } while (p < (uint16_t *)SYMVAL(__eeprom_workarea_end__));
    flashend = (uint32_t)(p - 1);
}

uint8_t eeprom_read_byte(const uint8_t *addr) {
    uint32_t        offset = (uint32_t)addr;
    const uint16_t *p      = (uint16_t *)SYMVAL(__eeprom_workarea_start__);
    const uint16_t *end    = (const uint16_t *)((uint32_t)flashend);
    uint16_t        val;
    uint8_t         data = 0xFF;

    if (!end) {
        eeprom_initialize();
        end = (const uint16_t *)((uint32_t)flashend);
    }
    if (offset < EEPROM_SIZE) {
        while (p <= end) {
            val = *p++;
            if ((val & 255) == offset) data = val >> 8;
        }
    }
    return data;
}

static void flash_write(const uint16_t *code, uint32_t addr, uint32_t data) {
    // with great power comes great responsibility....
    uint32_t stat;
    *(uint32_t *)&(FTFA->FCCOB3) = 0x06000000 | (addr & 0x00FFFFFC);
    *(uint32_t *)&(FTFA->FCCOB7) = data;
    __disable_irq();
    (*((void (*)(volatile uint8_t *))((uint32_t)code | 1)))(&(FTFA->FSTAT));
    __enable_irq();
    stat = FTFA->FSTAT & (FTFA_FSTAT_RDCOLERR | FTFA_FSTAT_ACCERR | FTFA_FSTAT_FPVIOL);
    if (stat) {
        FTFA->FSTAT = stat;
    }
    MCM->PLACR |= MCM_PLACR_CFCC;
}

void eeprom_write_byte(uint8_t *addr, uint8_t data) {
    uint32_t        offset = (uint32_t)addr;
    const uint16_t *p, *end = (const uint16_t *)((uint32_t)flashend);
    uint32_t        i, val, flashaddr;
    uint16_t        do_flash_cmd[] = {0x2380, 0x7003, 0x7803, 0xb25b, 0x2b00, 0xdafb, 0x4770};
    uint8_t         buf[EEPROM_SIZE];

    if (offset >= EEPROM_SIZE) return;
    if (!end) {
        eeprom_initialize();
        end = (const uint16_t *)((uint32_t)flashend);
    }
    if (++end < (uint16_t *)SYMVAL(__eeprom_workarea_end__)) {
        val       = (data << 8) | offset;
        flashaddr = (uint32_t)end;
        flashend  = flashaddr;
        if ((flashaddr & 2) == 0) {
            val |= 0xFFFF0000;
        } else {
            val <<= 16;
            val |= 0x0000FFFF;
        }
        flash_write(do_flash_cmd, flashaddr, val);
    } else {
        for (i = 0; i < EEPROM_SIZE; i++) {
            buf[i] = 0xFF;
        }
        val = 0;
        for (p = (uint16_t *)SYMVAL(__eeprom_workarea_start__); p < (uint16_t *)SYMVAL(__eeprom_workarea_end__); p++) {
            val = *p;
            if ((val & 255) < EEPROM_SIZE) {
                buf[val & 255] = val >> 8;
            }
        }
        buf[offset] = data;
        for (flashaddr = (uint32_t)(uint16_t *)SYMVAL(__eeprom_workarea_start__); flashaddr < (uint32_t)(uint16_t *)SYMVAL(__eeprom_workarea_end__); flashaddr += 1024) {
            *(uint32_t *)&(FTFA->FCCOB3) = 0x09000000 | flashaddr;
            __disable_irq();
            (*((void (*)(volatile uint8_t *))((uint32_t)do_flash_cmd | 1)))(&(FTFA->FSTAT));
            __enable_irq();
            val = FTFA->FSTAT & (FTFA_FSTAT_RDCOLERR | FTFA_FSTAT_ACCERR | FTFA_FSTAT_FPVIOL);
            ;
            if (val) FTFA->FSTAT = val;
            MCM->PLACR |= MCM_PLACR_CFCC;
        }
        flashaddr = (uint32_t)(uint16_t *)SYMVAL(__eeprom_workarea_start__);
        for (i = 0; i < EEPROM_SIZE; i++) {
            if (buf[i] == 0xFF) continue;
            if ((flashaddr & 2) == 0) {
                val = (buf[i] << 8) | i;
            } else {
                val = val | (buf[i] << 24) | (i << 16);
                flash_write(do_flash_cmd, flashaddr, val);
            }
            flashaddr += 2;
        }
        flashend = flashaddr;
        if ((flashaddr & 2)) {
            val |= 0xFFFF0000;
            flash_write(do_flash_cmd, flashaddr, val);
        }
    }
}

/*
void do_flash_cmd(volatile uint8_t *fstat)
{
        *fstat = 0x80;
        while ((*fstat & 0x80) == 0) ; // wait
}
00000000 <do_flash_cmd>:
   0:	2380      	movs	r3, #128	; 0x80
   2:	7003      	strb	r3, [r0, #0]
   4:	7803      	ldrb	r3, [r0, #0]
   6:	b25b      	sxtb	r3, r3
   8:	2b00      	cmp	r3, #0
   a:	dafb      	bge.n	4 <do_flash_cmd+0x4>
   c:	4770      	bx	lr
*/

uint16_t eeprom_read_word(const uint16_t *addr) {
    const uint8_t *p = (const uint8_t *)addr;
    return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8);
}

uint32_t eeprom_read_dword(const uint32_t *addr) {
    const uint8_t *p = (const uint8_t *)addr;
    return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8) | (eeprom_read_byte(p + 2) << 16) | (eeprom_read_byte(p + 3) << 24);
}

void eeprom_read_block(void *buf, const void *addr, uint32_t len) {
    const uint8_t *p    = (const uint8_t *)addr;
    uint8_t *      dest = (uint8_t *)buf;
    while (len--) {
        *dest++ = eeprom_read_byte(p++);
    }
}

int eeprom_is_ready(void) { return 1; }

void eeprom_write_word(uint16_t *addr, uint16_t value) {
    uint8_t *p = (uint8_t *)addr;
    eeprom_write_byte(p++, value);
    eeprom_write_byte(p, value >> 8);
}

void eeprom_write_dword(uint32_t *addr, uint32_t value) {
    uint8_t *p = (uint8_t *)addr;
    eeprom_write_byte(p++, value);
    eeprom_write_byte(p++, value >> 8);
    eeprom_write_byte(p++, value >> 16);
    eeprom_write_byte(p, value >> 24);
}

void eeprom_write_block(const void *buf, void *addr, uint32_t len) {
    uint8_t *      p   = (uint8_t *)addr;
    const uint8_t *src = (const uint8_t *)buf;
    while (len--) {
        eeprom_write_byte(p++, *src++);
    }
}

#else
// No EEPROM supported, so emulate it

#    ifndef EEPROM_SIZE
#        include "eeconfig.h"
#        define EEPROM_SIZE (((EECONFIG_SIZE + 3) / 4) * 4)  // based off eeconfig's current usage, aligned to 4-byte sizes, to deal with LTO
#    endif
__attribute__((aligned(4))) static uint8_t buffer[EEPROM_SIZE];

uint8_t eeprom_read_byte(const uint8_t *addr) {
    uint32_t offset = (uint32_t)addr;
    return buffer[offset];
}

void eeprom_write_byte(uint8_t *addr, uint8_t value) {
    uint32_t offset = (uint32_t)addr;
    buffer[offset]  = value;
}

uint16_t eeprom_read_word(const uint16_t *addr) {
    const uint8_t *p = (const uint8_t *)addr;
    return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8);
}

uint32_t eeprom_read_dword(const uint32_t *addr) {
    const uint8_t *p = (const uint8_t *)addr;
    return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8) | (eeprom_read_byte(p + 2) << 16) | (eeprom_read_byte(p + 3) << 24);
}

void eeprom_read_block(void *buf, const void *addr, size_t len) {
    const uint8_t *p    = (const uint8_t *)addr;
    uint8_t *      dest = (uint8_t *)buf;
    while (len--) {
        *dest++ = eeprom_read_byte(p++);
    }
}

void eeprom_write_word(uint16_t *addr, uint16_t value) {
    uint8_t *p = (uint8_t *)addr;
    eeprom_write_byte(p++, value);
    eeprom_write_byte(p, value >> 8);
}

void eeprom_write_dword(uint32_t *addr, uint32_t value) {
    uint8_t *p = (uint8_t *)addr;
    eeprom_write_byte(p++, value);
    eeprom_write_byte(p++, value >> 8);
    eeprom_write_byte(p++, value >> 16);
    eeprom_write_byte(p, value >> 24);
}

void eeprom_write_block(const void *buf, void *addr, size_t len) {
    uint8_t *      p   = (uint8_t *)addr;
    const uint8_t *src = (const uint8_t *)buf;
    while (len--) {
        eeprom_write_byte(p++, *src++);
    }
}

#endif /* chip selection */
// The update functions just calls write for now, but could probably be optimized

void eeprom_update_byte(uint8_t *addr, uint8_t value) { eeprom_write_byte(addr, value); }

void eeprom_update_word(uint16_t *addr, uint16_t value) {
    uint8_t *p = (uint8_t *)addr;
    eeprom_write_byte(p++, value);
    eeprom_write_byte(p, value >> 8);
}

void eeprom_update_dword(uint32_t *addr, uint32_t value) {
    uint8_t *p = (uint8_t *)addr;
    eeprom_write_byte(p++, value);
    eeprom_write_byte(p++, value >> 8);
    eeprom_write_byte(p++, value >> 16);
    eeprom_write_byte(p, value >> 24);
}

void eeprom_update_block(const void *buf, void *addr, size_t len) {
    uint8_t *      p   = (uint8_t *)addr;
    const uint8_t *src = (const uint8_t *)buf;
    while (len--) {
        eeprom_write_byte(p++, *src++);
    }
}