summaryrefslogtreecommitdiffstats
path: root/src/usr/pnor/pnordd.C
blob: 86f4efd7bb19305224db1dfbf7b635403dc354c9 (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
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
895
896
897
898
899
900
901
902
903
904
905
906
907
908
//  IBM_PROLOG_BEGIN_TAG
//  This is an automatically generated prolog.
//
//  $Source: src/usr/pnor/pnordd.C $
//
//  IBM CONFIDENTIAL
//
//  COPYRIGHT International Business Machines Corp. 2011
//
//  p1
//
//  Object Code Only (OCO) source materials
//  Licensed Internal Code Source Materials
//  IBM HostBoot Licensed Internal Code
//
//  The source code for this program is not published or other-
//  wise divested of its trade secrets, irrespective of what has
//  been deposited with the U.S. Copyright Office.
//
//  Origin: 30
//
//  IBM_PROLOG_END
/**
 *  @file pnordd.C
 *
 *  @brief Implementation of the PNOR Device Driver
 */

/*****************************************************************************/
// I n c l u d e s
/*****************************************************************************/
#include <sys/mmio.h>
#include <sys/task.h>
#include <sys/sync.h>
#include <string.h>
#include <devicefw/driverif.H>
#include <trace/interface.H>
#include <errl/errlentry.H>
#include <targeting/targetservice.H>
#include <errl/errlmanager.H>
#include "pnordd.H"
#include <pnor/pnorif.H>
#include <pnor/pnor_reasoncodes.H>

// Uncomment this to use the fake PNOR implementation (vs the LPC path)
//   @todo - Switch with RTC 36901
#define USE_FAKE_PNOR

// Uncomment this to skip the LPC code and just do page copies
//#define FAST_FAKE_PNOR

// Uncomment this to enable smart writing
#define SMART_WRITE

#ifdef USE_FAKE_PNOR
#define FAKE_PNOR_START 5*MEGABYTE
#define FAKE_PNOR_END 8*MEGABYTE
#define FAKE_PNOR_SIZE 3*MEGABYTE
void write_fake_pnor( uint64_t i_pnorAddr, void* i_buffer, size_t i_size );
void read_fake_pnor( uint64_t i_pnorAddr, void* o_buffer, size_t i_size );
#endif

extern trace_desc_t* g_trac_pnor;

namespace PNOR
{

/**
 * @brief Performs an PNOR Read Operation
 * This function performs a PNOR Read operation. It follows a pre-defined
 * prototype functions in order to be registered with the device-driver
 * framework.
 *
 * @param[in]   i_opType        Operation type, see DeviceFW::OperationType
 *                              in driverif.H
 * @param[in]   i_target        PNOR target
 * @param[in/out] io_buffer     Read: Pointer to output data storage
 *                              Write: Pointer to input data storage
 * @param[in/out] io_buflen     Input: size of io_buffer (in bytes)
 *                              Output:
 *                                  Read: Size of output data
 *                                  Write: Size of data written
 * @param[in]   i_accessType    DeviceFW::AccessType enum (usrif.H)
 * @param[in]   i_args          This is an argument list for DD framework.
 *                              In this function, there's only one argument,
 *                              containing the PNOR address and chip select
 * @return  errlHndl_t
 */
errlHndl_t ddRead(DeviceFW::OperationType i_opType,
                  TARGETING::Target* i_target,
                  void* io_buffer,
                  size_t& io_buflen,
                  int64_t i_accessType,
                  va_list i_args)
{
    errlHndl_t l_err = NULL;
    uint64_t l_addr = va_arg(i_args,uint64_t);

    do{
        //TODO - Fix with Story 34763
        // Ensure we are operating on a 32-bit (4-byte) boundary
        assert( reinterpret_cast<uint64_t>(io_buffer) % 4 == 0 );

        // Read the flash
        l_err = Singleton<PnorDD>::instance().readFlash(io_buffer,
                                                        io_buflen,
                                                        l_addr);
        if(l_err)
        {
            break;
        }
 
    }while(0);

    return l_err;
}

/**
 * @brief Performs an PNOR Write Operation
 * This function performs a PNOR Write operation. It follows a pre-defined
 * prototype functions in order to be registered with the device-driver
 * framework.
 *
 * @param[in]   i_opType        Operation type, see DeviceFW::OperationType
 *                              in driverif.H
 * @param[in]   i_target        PNOR target
 * @param[in/out] io_buffer     Read: Pointer to output data storage
 *                              Write: Pointer to input data storage
 * @param[in/out] io_buflen     Input: size of io_buffer (in bytes)
 *                              Output:
 *                                  Read: Size of output data
 *                                  Write: Size of data written
 * @param[in]   i_accessType    DeviceFW::AccessType enum (usrif.H)
 * @param[in]   i_args          This is an argument list for DD framework.
 *                              In this function, there's only one argument,
 *                              containing the PNOR address and chip select
 * @return  errlHndl_t
 */
errlHndl_t ddWrite(DeviceFW::OperationType i_opType,
                  TARGETING::Target* i_target,
                  void* io_buffer,
                  size_t& io_buflen,
                  int64_t i_accessType,
                  va_list i_args)
{
    errlHndl_t l_err = NULL;
    uint64_t l_addr = va_arg(i_args,uint64_t);

    do{
        //TODO - Fix with Story 34763
        // Ensure we are operating on a 32-bit (4-byte) boundary
        assert( reinterpret_cast<uint64_t>(io_buffer) % 4 == 0 );

        // Write the flash
        l_err = Singleton<PnorDD>::instance().writeFlash(io_buffer,
                                                         io_buflen,
                                                         l_addr);
        if(l_err)
        {
            break;
        }
 
    }while(0);

    return l_err;
}

// Register PNORDD access functions to DD framework
DEVICE_REGISTER_ROUTE(DeviceFW::READ,
                      DeviceFW::PNOR,
                      TARGETING::TYPE_PROC,
                      ddRead);

DEVICE_REGISTER_ROUTE(DeviceFW::WRITE,
                      DeviceFW::PNOR,
                      TARGETING::TYPE_PROC,
                      ddWrite);


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////


/**
 * @brief Performs a PNOR Read Operation
 */
errlHndl_t PnorDD::readFlash(void* o_buffer,
                             size_t& io_buflen,
                             uint64_t i_address)
{
    //TRACDCOMP(g_trac_pnor, "PnorDD::readFlash(i_address=0x%llx)> ", i_address);
    errlHndl_t l_err = NULL;

    do{
        //mask off chip select for now, will probably break up fake PNOR into
        //multiple fake chips eventually
        uint64_t l_address = i_address & 0x00000000FFFFFFFF;

        l_err = verifyFlashAddressRange(l_address, io_buflen);
        if(l_err)
        {
            io_buflen = 0;
            break;
        }

#ifdef FAST_FAKE_PNOR
        read_fake_pnor( i_address, o_buffer, io_buflen );
        break;
#endif

        // LPC is accessed 32-bits at a time...
        uint32_t* word_ptr = static_cast<uint32_t*>(o_buffer);
        uint64_t words_read = 0;
        for( uint32_t addr = i_address;
             addr < (i_address+io_buflen);
             addr += sizeof(uint32_t) )
        {
            uint32_t read_data = 0;
            l_err = readLPC( addr, read_data );
            if( l_err ) { break; }

            memcpy( word_ptr+words_read, &read_data, sizeof(uint32_t) );

            words_read++;           
        }
        io_buflen = words_read*sizeof(uint32_t);
        if( l_err ) { break; }
    }while(0);

    return l_err;
}

/**
 * @brief Performs a PNOR Write Operation
 */
errlHndl_t PnorDD::writeFlash(void* i_buffer,
                              size_t& io_buflen,
                              uint64_t i_address)
{
    //TRACDCOMP(g_trac_pnor, "PnorDD::writeFlash(i_address=0x%llx)> ", i_address);
    errlHndl_t l_err = NULL;

    do{        
        TRACDCOMP(g_trac_pnor,"PNOR write %.8X", i_address);

        //mask off chip select for now, will probably break up fake PNOR into
        //multiple fake chips eventually
        uint64_t l_address = i_address & 0x00000000FFFFFFFF;

        // make sure this is a valid address
        l_err = verifyFlashAddressRange(l_address, io_buflen);
        if(l_err) { break; }


#ifdef FAST_FAKE_PNOR
        write_fake_pnor( i_address, i_buffer, io_buflen );
        break;
#endif

        // LPC is accessed 32-bits at a time, but we also need to be
        //   smart about handling erases.  In NOR flash we can set bits
        //   without an erase but we cannot clear them.  When we erase
        //   we have to erase and entire block of data at a time.
        uint32_t* word_ptr = static_cast<uint32_t*>(i_buffer);
        uint64_t num_blocks = getNumAffectedBlocks(i_address,io_buflen);
        uint32_t cur_addr = i_address;
        uint64_t bytes_left = io_buflen;

        // loop through erase blocks until we've gotten through all
        //  affected blocks
        for( uint64_t block = 0;
             block < num_blocks;
             ++block )
        {
            TRACDCOMP( g_trac_pnor, "Block %d: bytes_left=%d, cur_addr=0x%.8X", block, bytes_left, cur_addr );

            // write a single block of data out to flash efficiently
            l_err = compareAndWriteBlock( cur_addr,
                                          (bytes_left-1)/sizeof(uint32_t)+1,
                                          word_ptr );
            if( l_err ) { break; }
            //@todo - How should we handle PNOR errors?

            // move on to the next block
            if( bytes_left > ERASESIZE_BYTES )
            {
                bytes_left -= ERASESIZE_BYTES;
                cur_addr += ERASESIZE_BYTES;
            }
            else
            {
                // final block of partial data
                //   align cur_addr to the beginning of the block
                cur_addr = findEraseBlock(cur_addr+bytes_left);
                //   figure out the remaining data in the last block
                bytes_left = (i_address - cur_addr);
            }
            word_ptr += (bytes_left-1)/sizeof(uint32_t);
        }
        if( l_err ) { break; }

    }while(0);

    // keeping track of every actual byte written is complicated and it can
    //  be misleading in the cases where we end up erasing and writing an
    //  entire block, instead just return zero for any failures
    if( l_err )
    {
        io_buflen = 0;
    }

    return l_err;
}


/********************
 Private/Protected Methods
 ********************/


/**
 * @brief  Constructor
 */
PnorDD::PnorDD()
{
    TRACFCOMP(g_trac_pnor, "PnorDD::PnorDD()> ");
    mutex_init(&iv_mutex);

    for( uint64_t x=0; x < (PNORSIZE/ERASESIZE_BYTES); x++ )
    {
        iv_erases[x] = 0;
    }
}

/**
 * @brief  Destructor
 */
PnorDD::~PnorDD()
{

    //Nothing to do for now
}

/**
 * @brief Verify flash request is in appropriate address range
 */
errlHndl_t PnorDD::verifyFlashAddressRange(uint64_t i_address,
                                           size_t& i_length)
{
    errlHndl_t l_err = NULL;

    do{
        //@todo - Do we really need any checking here?

#ifdef USE_FAKE_PNOR
        if((i_address+i_length) > FAKE_PNOR_SIZE)
        {
            TRACFCOMP( g_trac_pnor, "PnorDD::verifyAddressRange> Invalid Address Requested : i_address=%d", i_address );
            /*@
             * @errortype
             * @moduleid     PNOR::MOD_PNORDD_VERIFYADDRESSRANGE
             * @reasoncode   PNOR::RC_INVALID_ADDRESS
             * @userdata1    Requested Address
             * @userdata2    Requested Length
             * @devdesc      PnorDD::verifyAddressRange> Invalid Address requested
             */
            l_err = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                            PNOR::MOD_PNORDD_VERIFYADDRESSRANGE,
                                            PNOR::RC_INVALID_ADDRESS,
                                            TO_UINT64(i_address),
                                            TO_UINT64(i_length));
            break;
        }
#endif


    }while(0);

    return l_err;
}

/**
 * @brief Read a LPC Host Controller Register
 */
errlHndl_t PnorDD::readRegLPC(LpcRegAddr i_addr,
                              uint32_t& o_data)
{
    errlHndl_t l_err = NULL;

    do {
       //@todo - RTC 36901 or 35728        
    } while(0);

    return l_err;
}

/**
 * @brief Write a LPC Host Controller Register
 */
errlHndl_t PnorDD::writeRegLPC(LpcRegAddr i_addr,
                               uint32_t i_data)
{
    return NULL; //@todo - RTC 36901 or 35728
}

/**
 * @brief Read a SPI Register
 */
errlHndl_t PnorDD::readRegSPI(uint32_t i_addr,
                              uint32_t& o_data)
{
    //@todo - Finish with Story 35728
    TRACFCOMP( g_trac_pnor, "PnorDD::readRegSPI> Unsupported Operation : i_addr=%d", i_addr );
    /*@
     * @errortype
     * @moduleid     PNOR::MOD_PNORDD_READREGSPI
     * @reasoncode   PNOR::RC_UNSUPPORTED_OPERATION
     * @userdata1    Requested Address
     * @userdata2    <unused>
     * @devdesc      PnorDD::readRegSPI> Unsupported Operation
     */
    return new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                   PNOR::MOD_PNORDD_READREGSPI,
                                   PNOR::RC_UNSUPPORTED_OPERATION,
                                   TO_UINT64(i_addr),
                                   0);
    
}

/**
 * @brief Write a SPI Register
 */
errlHndl_t PnorDD::writeRegSPI(uint32_t i_addr,
                               uint32_t i_data)
{
    //@todo - Finish with Story 35728
    TRACFCOMP( g_trac_pnor, "PnorDD::writeRegSPI> Unsupported Operation : i_addr=%d", i_addr );
    /*@
     * @errortype
     * @moduleid     PNOR::MOD_PNORDD_WRITEREGSPI
     * @reasoncode   PNOR::RC_UNSUPPORTED_OPERATION
     * @userdata1    Requested Address
     * @userdata2    <unused>
     * @devdesc      PnorDD::writeRegSPI> Unsupported Operation
     */
    return new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                   PNOR::MOD_PNORDD_WRITEREGSPI,
                                   PNOR::RC_UNSUPPORTED_OPERATION,
                                   TO_UINT64(i_addr),
                                   0);
    
}


/**
 * @brief Read an address from LPC space
 */
errlHndl_t PnorDD::readLPC(uint32_t i_addr,
                           uint32_t& o_data)
{
    errlHndl_t l_err = NULL;
    bool need_unlock = false;

    do {
#ifdef USE_FAKE_PNOR
        read_fake_pnor( i_addr, static_cast<void*>(&o_data),
                        sizeof(uint32_t) );
#else
        //@todo - fill in with RTC 36901

        //@fixme - add non-master support  (RTC 36950)
        TARGETING::Target* xscom_target =
          TARGETING::MASTER_PROCESSOR_CHIP_TARGET_SENTINEL;

        // always read/write 64 bits to SCOM
        size_t scom_size = sizeof(uint64_t);

        // atomic section >>
        mutex_lock(&iv_mutex);
        need_unlock = true;

        // write command register with LPC address to read
        ControlReg_t eccb_cmd;
        eccb_cmd.read_op = 1;
        eccb_cmd.address = i_addr;
        l_err = deviceOp( DeviceFW::WRITE,
                          xscom_target,
                          &(eccb_cmd.data64),
                          scom_size,
                          DEVICE_XSCOM_ADDRESS(ECCB_CTL_REG) );
        if( l_err ) { break; }

        // poll for complete and get the data back
        StatusReg_t eccb_stat;
        while(1) //@fixme - need a timeout value
        {
            l_err = deviceOp( DeviceFW::READ,
                              xscom_target,
                              &(eccb_stat.data64),
                              scom_size,
                              DEVICE_XSCOM_ADDRESS(ECCB_STAT_REG) );
            if( l_err ) { break; }

            if( eccb_stat.op_done == 1 )
            {
                break;
            }

            //@fixme - simics doesn't set the done bit yet  (RTC 36901)
            break;
        }
        if( l_err ) { break; }

        // check for errors
        if( eccb_stat.data64 & LPC_STAT_REG_ERROR_MASK )
        {
            TRACFCOMP(g_trac_pnor, "PnorDD::readLPC> Error from LPC Status Register : i_addr=0x%.8X, status=0x%.16X", i_addr, eccb_stat.data64 );

            /*@
             * @errortype
             * @moduleid     PNOR::MOD_PNORDD_READLPC
             * @reasoncode   PNOR::RC_LPC_ERROR
             * @userdata1    LPC Address
             * @userdata2    ECCB Status Register
             * @devdesc      PnorDD::readLPC> Error from LPC Status Register
             */
            l_err = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                            PNOR::MOD_PNORDD_READLPC,
                                            PNOR::RC_LPC_ERROR,
                                            TWO_UINT32_TO_UINT64(0,i_addr),
                                            eccb_stat.data64);
            l_err->collectTrace("PNOR");
            l_err->collectTrace("XSCOM");
            //@todo - Any cleanup or recovery needed?
            break;
        }

        // atomic section <<
        mutex_unlock(&iv_mutex);
        need_unlock = false;

        // copy data out to caller's buffer
        o_data = eccb_stat.read_data;
#endif
    } while(0);

    if( need_unlock )
    {
        mutex_unlock(&iv_mutex);
        need_unlock = false;
    }

    return l_err;
}

/**
 * @brief Write an address from LPC space
 */
errlHndl_t PnorDD::writeLPC(uint32_t i_addr,
                            uint32_t i_data)
{
    errlHndl_t l_err = NULL;
    bool need_unlock = false;

    do {
#ifdef USE_FAKE_PNOR        
        write_fake_pnor( i_addr, static_cast<void*>(&i_data),
                         sizeof(uint32_t) );
#else
        //@todo - fill in with RTC 36901

        //@fixme - add non-master support  (RTC 36950)
        TARGETING::Target* xscom_target =
          TARGETING::MASTER_PROCESSOR_CHIP_TARGET_SENTINEL;

        // always read/write 64 bits to SCOM
        size_t scom_size = sizeof(uint64_t);

        // atomic section >>
        mutex_lock(&iv_mutex);
        need_unlock = true;

        // write data register 
        uint64_t eccb_data = static_cast<uint64_t>(i_data);
        l_err = deviceOp( DeviceFW::WRITE,
                          xscom_target,
                          &eccb_data,
                          scom_size,
                          DEVICE_XSCOM_ADDRESS(ECCB_DATA_REG) );
        if( l_err ) { break; }

        // write command register with LPC address to write
        ControlReg_t eccb_cmd;
        eccb_cmd.read_op = 0;
        eccb_cmd.address = i_addr;
        l_err = deviceOp( DeviceFW::WRITE,
                          xscom_target,
                          &(eccb_cmd.data64),
                          scom_size,
                          DEVICE_XSCOM_ADDRESS(ECCB_CTL_REG) );
        if( l_err ) { break; }

        // poll for complete and get the data back
        StatusReg_t eccb_stat;
        while(1) //@fixme - need a timeout value
        {
            l_err = deviceOp( DeviceFW::READ,
                              xscom_target,
                              &(eccb_stat.data64),
                              scom_size,
                              DEVICE_XSCOM_ADDRESS(ECCB_STAT_REG) );
            if( l_err ) { break; }

            if( eccb_stat.op_done == 1 )
            {
                break;
            }

            //@fixme - simics doesn't set the done bit yet : RTC 36901
            break;
        }
        if( l_err ) { break; }

        // check for errors
        if( eccb_stat.data64 & LPC_STAT_REG_ERROR_MASK )
        {
            TRACFCOMP(g_trac_pnor, "PnorDD::writeLPC> Error from LPC Status Register : i_addr=0x%.8X, status=0x%.16X", i_addr, eccb_stat.data64 );

            /*@
             * @errortype
             * @moduleid     PNOR::MOD_PNORDD_WRITELPC
             * @reasoncode   PNOR::RC_LPC_ERROR
             * @userdata1    LPC Address
             * @userdata2    ECCB Status Register
             * @devdesc      PnorDD::writeLPC> Error from LPC Status Register
             */
            l_err = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                            PNOR::MOD_PNORDD_WRITELPC,
                                            PNOR::RC_LPC_ERROR,
                                            TWO_UINT32_TO_UINT64(0,i_addr),
                                            eccb_stat.data64);
            l_err->collectTrace("PNOR");
            l_err->collectTrace("XSCOM");
            //@todo - Any cleanup or recovery needed?
            break;
        }

        // atomic section <<
        mutex_unlock(&iv_mutex);
        need_unlock = false;
#endif
    } while(0);

    if( need_unlock )
    {
        mutex_unlock(&iv_mutex);
        need_unlock = false;
    }

    return l_err;
}

/**
 * @brief Compare the existing data in 1 erase block of the flash with
 *   the incoming data and write or erase as needed
 */
errlHndl_t PnorDD::compareAndWriteBlock(uint32_t i_targetAddr,
                                        uint32_t i_wordsToWrite,
                                        uint32_t* i_data)
{
    TRACDCOMP(g_trac_pnor,"compareAndWriteBlock(0x%.8X,%d,%p)", i_targetAddr, i_wordsToWrite, i_data);
    errlHndl_t l_err = NULL;

    // remember any data we read so we don't have to reread it later
    typedef struct
    {
        uint32_t data;
        bool wasRead;
        bool diff;
    } readflag_t;
    readflag_t* read_data = NULL;

    do {
#ifndef SMART_WRITE
        // LPC is accessed 32-bits at a time...
        uint64_t words_written = 0;
        for( uint32_t addr = i_targetAddr;
             addr < (i_targetAddr+i_wordsToWrite*sizeof(uint32_t));
             addr += sizeof(uint32_t) )
        {
            l_err = writeLPC( addr, i_data[words_written] );
            if( l_err ) { break; }

            words_written++;
        }
        o_bytesWritten = words_written*sizeof(uint32_t);
        if( l_err ) { break; }


#else 

        // remember any data we read so we don't have to reread it later
        read_data = new readflag_t[ERASESIZE_WORD32];
        for( size_t x = 0; x < ERASESIZE_WORD32; x++ )
        {
            read_data[x].wasRead = false;
            read_data[x].diff = false;
        }

        // remember if we need to erase the block or not
        bool need_erase = false;

        // walk through every word of data to see what changed
        const uint32_t block_addr = findEraseBlock(i_targetAddr);
        for( uint64_t bword = 0; bword < ERASESIZE_WORD32; bword++ )
        {
            // note: bword is the offset into the flash block
            read_data[bword].diff = false;

            // no need to check data before where the write starts
            if( (block_addr + bword*sizeof(uint32_t)) < i_targetAddr )
            {
                continue;
            }
            // no need to check data after where the write ends
            else if( (block_addr + bword*sizeof(uint32_t)) >=
                     (i_targetAddr + i_wordsToWrite*sizeof(uint32_t)) ) 
            {
                // done looking now
                break;
            }
            // otherwise we need to compare our data with what is in flash now
            else
            {                
                l_err = readLPC( block_addr + bword*sizeof(uint32_t),
                                 read_data[bword].data );
                if( l_err ) { break; }

                read_data[bword].wasRead = true;

                // dword is the offset into the input data
                uint64_t dword = (block_addr + bword*sizeof(uint32_t));
                dword -= i_targetAddr; //offset into user data
                dword = dword / sizeof(uint32_t); //convert bytes to words

                // look for any bits being changed (using XOR)
                if( read_data[bword].data ^ i_data[dword] )
                {
                    read_data[bword].diff = true;

                    // look for any bits that go from 1->0
                    if( read_data[bword].data & ~(i_data[dword]) )
                    {
                        need_erase = true;                        
                    }

                    // push the user data into the read buffer
                    //  to get written later
                    read_data[bword].data = i_data[dword];
                }                                 
            }
        }
        if( l_err ) { break; }

        // erase the block if we need to
        if( need_erase )
        {
            // first we need to save off any data we didn't read yet
            //  that is not part of the data we are writing
            for( uint64_t bword = 0; bword < ERASESIZE_WORD32; bword++ )
            {
                // mark the word as different to force a write below
                read_data[bword].diff = true;

                // skip what we already read
                if( read_data[bword].wasRead )
                {
                    continue;
                }

                // dword is the offset into the input data
                uint64_t dword = (block_addr + bword*sizeof(uint32_t));
                dword -= i_targetAddr; //offset into user data
                dword = dword / sizeof(uint32_t); //convert bytes to words

                // restore the data before the write section
                if( (block_addr + bword*sizeof(uint32_t)) < i_targetAddr )
                {
                    l_err = readLPC( block_addr + bword*sizeof(uint32_t),
                                     read_data[bword].data );
                    if( l_err ) { break; }
                }
                // restore the data after the write section
                else if( (block_addr + bword*sizeof(uint32_t)) >=
                         (i_targetAddr + i_wordsToWrite*sizeof(uint32_t)) ) 
                {
                    l_err = readLPC( block_addr + bword*sizeof(uint32_t),
                                     read_data[bword].data );
                    if( l_err ) { break; }
                }
                // otherwise we will use the write data directly
                else
                {
                    read_data[bword].data = i_data[dword];
                }
            }
            if( l_err ) { break; }

            // erase the flash
            l_err = eraseFlash( block_addr );
            if( l_err ) { break; }
        }

        // walk through every word again to write the data back out
        uint64_t bword_written = 0;
        for( bword_written = 0;
             bword_written < ERASESIZE_WORD32;
             bword_written++ )
        {
            // only write what we have to
            if( !(read_data[bword_written].diff) )
            {
                continue;
            }

            // write the word out to the flash
            l_err = writeLPC( block_addr + bword_written*sizeof(uint32_t),
                              read_data[bword_written].data );
            if( l_err ) { break; }
            //@todo - How should we handle PNOR errors?
        }
        if( l_err ) { break; }

#endif

    } while(0);

    if( read_data )
    {
        delete[] read_data;
    }

    return l_err;
}

/**
 * @brief Erase a block of flash
 */
errlHndl_t PnorDD::eraseFlash(uint32_t i_address)
{
    errlHndl_t l_err = NULL;

    do {
        if( findEraseBlock(i_address) != i_address )
        {
            /*@
             * @errortype
             * @moduleid     PNOR::MOD_PNORDD_ERASEFLASH
             * @reasoncode   PNOR::RC_LPC_ERROR
             * @userdata1    LPC Address
             * @userdata2    Nearest Erase Boundary
             * @devdesc      PnorDD::eraseFlash> Address not on erase boundary
             */
            l_err = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                            PNOR::MOD_PNORDD_ERASEFLASH,
                                            PNOR::RC_LPC_ERROR,
                                            TWO_UINT32_TO_UINT64(0,i_address),
                                            findEraseBlock(i_address));
            break;
        }

        // log the erase of this block
        iv_erases[i_address/ERASESIZE_BYTES]++;
        TRACFCOMP(g_trac_pnor, "PnorDD::eraseFlash> Block 0x%.8X has %d erasures", i_address, iv_erases[i_address/ERASESIZE_BYTES] );
        
        //@todo - issue some LPC/SPI commands to erase the block RTC 35728
        char* ptr = (char*)(FAKE_PNOR_START+i_address);
        memset( ptr, 0, ERASESIZE_BYTES );
    } while(0);

    return l_err;
}




}; //end PNOR namespace



#ifdef USE_FAKE_PNOR
void write_fake_pnor( uint64_t i_pnorAddr, void* i_buffer, size_t i_size )
{
    //create a pointer to the offset start.
    char * destPtr = (char *)(FAKE_PNOR_START+i_pnorAddr);

    //copy data from memory into the buffer.
    memcpy(destPtr, i_buffer, i_size);
}
void read_fake_pnor( uint64_t i_pnorAddr, void* o_buffer, size_t i_size )
{
    //create a pointer to the offset start.
    char * srcPtr = (char *)(FAKE_PNOR_START+i_pnorAddr);

    //copy data from memory into the buffer.
    memcpy(o_buffer, srcPtr, i_size);
}
#endif
OpenPOWER on IntegriCloud