summaryrefslogtreecommitdiffstats
path: root/src/usr/pnor/sfc_ast2X00.C
blob: b976a7cc6eb4d9e98f2fa655f9af49ba75cc6188 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/pnor/sfc_ast2X00.C $                                  */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2016,2017                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
/*****************************************************************************/
// I n c l u d e s
/*****************************************************************************/
#include <sio/sio.H>
#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 <errl/errlmanager.H>
#include <errl/errludlogregister.H>
#include <targeting/common/targetservice.H>
#include <pnor/pnor_reasoncodes.H>
#include <sys/time.h>
#include <errl/hberrltypes.H>
#include <lpc/lpcif.H>
#include "sfc_ast2X00.H"
#include "norflash.H"
#include <util/align.H>
#include "pnor_common.H"

/*****************************************************************************/
// C o n s t a n t s
/*****************************************************************************/


/*****************************************************************************/
// G l o b a l s
/*****************************************************************************/

// Initialized in pnorrp.C
extern trace_desc_t* g_trac_pnor;
/*****************************************************************************/
// M e t h o d s
/*****************************************************************************/
namespace PNOR {
/**
 * @brief Wrapper for device driver constructor
 */
/*errlHndl_t create_SfcDD( SfcDD*& o_sfc,
                         TARGETING::Target* i_proc )
{
    errlHndl_t l_err = NULL;
    TRACFCOMP( g_trac_pnor, "Creating SfcAST2X00 object" );
    o_sfc = new SfcAST2X00( l_err, i_proc );
    return l_err;
}
**/
};

/**
 * @brief Constructor
 */
SfcAST2X00::SfcAST2X00( errlHndl_t& o_err,
                        TARGETING::Target* i_proc )
: SfcDD(o_err,i_proc)
{
}

/**
 * @brief Read data from the flash
 */
errlHndl_t SfcAST2X00::readFlash( uint32_t i_addr,
                                  size_t i_size,
                                  void* o_data )
{
    errlHndl_t l_err = NULL;
    TRACDCOMP( g_trac_pnor, ENTER_MRK"SfcAST2X00::readFlash> i_addr=0x%.8x, i_size=0x%.8x",  i_addr, i_size );
    do
    {
        uint32_t* word_ptr = static_cast<uint32_t*>(o_data);
        uint32_t word_size = (ALIGN_4(i_size))/4;
        for( uint32_t words_read = 0;
             words_read < word_size;
             words_read ++ )
        {
            //Read directly from MMIO space
            uint32_t lpc_addr = PNOR::LPC_SFC_MMIO_OFFSET |
                                (i_addr + words_read*4);
            size_t reg_size = sizeof(uint32_t);

            l_err = deviceOp( DeviceFW::READ,
                              iv_proc,
                              &(word_ptr[words_read]),
                              reg_size,
                              DEVICE_LPC_ADDRESS(LPC::TRANS_FW,
                                                 lpc_addr) );
            if( l_err ) {  break; }
        }
        if( l_err ) { break; }
    } while(0);

    TRACDCOMP( g_trac_pnor, EXIT_MRK"SfcAST2X00::readFlash> err=%.8X", ERRL_GETEID_SAFE(l_err) );
    return l_err;
}


/**
 * @brief Write data into flash
 */
errlHndl_t SfcAST2X00::writeFlash( uint32_t i_addr,
                                   size_t i_size,
                                   void* i_data )
{
    TRACDCOMP( g_trac_pnor, ENTER_MRK"SfcAST2X00::writeFlash> i_addr=0x%.8x, i_size=0x%.8x", i_addr, i_size );
    errlHndl_t l_err = NULL;
    size_t l_bytes_left = i_size;
    size_t l_bytes_to_write = 0;
    uint32_t l_addr_to_write = i_addr;
    uint8_t* l_dataptr = reinterpret_cast<uint8_t*>(i_data);

    do {
        // Enable write mode
        l_err = enableWriteMode();
        if( l_err ) { break; }

        // Page Program (PP) command only supports 256 bytes at a time
        if( l_bytes_left <= PNOR::PAGE_PROGRAM_BYTES )
        {
            l_bytes_to_write = l_bytes_left;
            l_bytes_left = 0;
        }
        else
        {
            l_bytes_to_write = PNOR::PAGE_PROGRAM_BYTES;
            l_bytes_left -= PNOR::PAGE_PROGRAM_BYTES;
        }

        // Send in the Page Program command with the data to write
        uint8_t opcode = PNOR::SPI_JEDEC_PAGE_PROGRAM;
        l_err = sendSpiCmd( opcode, l_addr_to_write,
                            l_bytes_to_write,
                            l_dataptr,
                            0, NULL );
        if( l_err ) { break; }

        // Move to the next chunk
        l_addr_to_write += l_bytes_to_write;
        l_dataptr += l_bytes_to_write;

        // Wait for idle
        l_err = pollOpComplete();
        if( l_err ) { break; }

#ifdef CONFIG_ALLOW_MICRON_PNOR
        //check for special Micron Flag Status reg
        if(iv_flashWorkarounds & PNOR::HWWK_MICRON_WRT_ERASE)
        {
            l_err = PNOR::micronFlagStatus(this);
            if(l_err) { break; }
        }
#endif

    } while(l_bytes_left);

    TRACDCOMP( g_trac_pnor, EXIT_MRK"SfcAST2X00::writeFlash> err=%.8X", ERRL_GETEID_SAFE(l_err) );
    return l_err;
}


/**
 * @brief Erase a block of flash
 */
errlHndl_t SfcAST2X00::eraseFlash( uint32_t i_addr )
{
    TRACDCOMP(g_trac_pnor, ">>SfcAST2X00::eraseFlash> Block 0x%.8X", i_addr );
    errlHndl_t l_err = NULL;

    do {
        // Enable write mode
        l_err = enableWriteMode();
        if( l_err ) { break; }

        // Send erase command
        uint8_t opcode = PNOR::SPI_JEDEC_SECTOR_ERASE;
        l_err = sendSpiCmd( opcode, i_addr, 0, 0, 0, NULL );
        if( l_err ) { break; }

        // Wait for idle
        l_err = pollOpComplete();
        if( l_err ) { break; }

#ifdef CONFIG_ALLOW_MICRON_PNOR
        //check for special Micron Flag Status reg
        if(iv_flashWorkarounds & PNOR::HWWK_MICRON_WRT_ERASE)
        {
            l_err = PNOR::micronFlagStatus(this);
            if(l_err) { break; }
        }
#endif

    } while(0);

    TRACDCOMP( g_trac_pnor, EXIT_MRK"SfcAST2X00::eraseFlash> err=%.8X", ERRL_GETEID_SAFE(l_err) );
    return l_err;
}

//function to call SIO dd for ahb sio read
errlHndl_t SfcAST2X00::ahbSioReadWrapper(uint32_t i_ahb_sio_data,
                                         uint32_t i_lpc_addr)
{
    size_t l_ahb_sio_len = sizeof(i_ahb_sio_data);
    return deviceOp( DeviceFW::READ,
                     TARGETING::MASTER_PROCESSOR_CHIP_TARGET_SENTINEL,
                     &(i_ahb_sio_data),
                     l_ahb_sio_len,
                     DEVICE_AHB_SIO_ADDRESS(i_lpc_addr));
}

//function to call SIO dd for ahb sio write
errlHndl_t SfcAST2X00::ahbSioWriteWrapper(uint32_t i_ahb_sio_data,
                                          uint32_t i_lpc_addr)
{
    size_t l_ahb_sio_len = sizeof(i_ahb_sio_data);
    return deviceOp( DeviceFW::WRITE,
                     TARGETING::MASTER_PROCESSOR_CHIP_TARGET_SENTINEL,
                     &(i_ahb_sio_data),
                     l_ahb_sio_len,
                     DEVICE_AHB_SIO_ADDRESS(i_lpc_addr));
}

/**
 * @brief Send a SPI command
 */
errlHndl_t SfcAST2X00::sendSpiCmd( uint8_t i_opCode,
                                   uint32_t i_address,
                                   size_t i_writeCnt,
                                   const uint8_t* i_writeData,
                                   size_t i_readCnt,
                                   uint8_t* o_readData )
{
    errlHndl_t l_err = NULL;
    size_t opsize = 0;
    TRACDCOMP( g_trac_pnor, ENTER_MRK"SfcAST2X00::sendSpiCmd> i_opCode=%.2X, i_address=%.8X, i_writeCnt=0x%X, i_writeData=%p, i_readCnt=0x%X, o_readData=%p", i_opCode, i_address, i_writeCnt, i_writeData, i_readCnt, o_readData );

    do {
#ifdef CONFIG_ALLOW_MICRON_PNOR
        //Do a read of flash address zero to workaround
        // a micron bug with extended reads
        if( (PNOR::HWWK_MICRON_EXT_READ & iv_flashWorkarounds)
            && (i_readCnt > 4) )
        {
            uint32_t ignored = 0;
            l_err = readFlash( 0, 1, &ignored );
            if(l_err) { break; }
        }
#endif

        // Put controller into command mode (instead of read mode)
        l_err = commandMode( true );
        if( l_err ) { break; }

        // Write command to the beginning of the flash space
        opsize = sizeof(i_opCode);
        l_err = deviceOp( DeviceFW::WRITE,
                          iv_proc,
                          &i_opCode,
                          opsize, //just send opcode
                          DEVICE_LPC_ADDRESS(LPC::TRANS_FW,
                                             PNOR::LPC_SFC_MMIO_OFFSET) );
        if( l_err ) { break; }

        // Send address if there is one
        if( i_address != NO_ADDRESS )
        {
            // Write address to the beginning of the flash space
            opsize = sizeof(i_address);
            l_err = deviceOp( DeviceFW::WRITE,
                              iv_proc,
                              &i_address,
                              opsize, //only supporting 4-byte addresses
                              DEVICE_LPC_ADDRESS(LPC::TRANS_FW,
                                                 PNOR::LPC_SFC_MMIO_OFFSET) );
            if( l_err ) { break; }
        }

        // Send in the rest of the write data
        if( i_writeCnt && i_writeData )
        {
            size_t bytes_left = i_writeCnt;
            uint8_t* curptr = const_cast<uint8_t*>(i_writeData);
            while( bytes_left )
            {
                // Write the last partial word if there is one
                if( bytes_left < 4 )
                {
                    opsize = bytes_left;
                    l_err = deviceOp( DeviceFW::WRITE,
                                      iv_proc,
                                      curptr,
                                      opsize,
                                      DEVICE_LPC_ADDRESS(LPC::TRANS_FW,
                                                   PNOR::LPC_SFC_MMIO_OFFSET) );
                    break;
                }

                // Write data into the beginning of the flash space,
                //  in command mode this doesn't write the flash
                //  but instead is a pass-through to the area we
                //  really want to write
                opsize = sizeof(uint32_t);
                l_err = deviceOp( DeviceFW::WRITE,
                                  iv_proc,
                                  curptr,
                                  opsize,
                                  DEVICE_LPC_ADDRESS(LPC::TRANS_FW,
                                                   PNOR::LPC_SFC_MMIO_OFFSET));
                if( l_err ) { break; }

                curptr += 4;
                bytes_left -= 4;
            }
            if( l_err ) { break; }
        }

        // Read back the results
        if( i_readCnt && o_readData )
        {
            size_t bytes_left = i_readCnt;
            uint8_t* curptr = o_readData;
            while( bytes_left )
            {
                // Grab the last partial word if there is one
                if( bytes_left < 4 )
                {
                    opsize = bytes_left;
                    l_err = deviceOp( DeviceFW::READ,
                                      iv_proc,
                                      curptr,
                                      opsize,
                                      DEVICE_LPC_ADDRESS(LPC::TRANS_FW,
                                                   PNOR::LPC_SFC_MMIO_OFFSET) );
                    break;
                }

                // Read data from the beginning of the flash space,
                //  in command mode this doesn't read the flash
                //  but instead is a pass-through to the data we
                //  really want
                opsize = sizeof(uint32_t);
                l_err = deviceOp( DeviceFW::READ,
                                  iv_proc,
                                  curptr,
                                  opsize,
                                  DEVICE_LPC_ADDRESS(LPC::TRANS_FW,
                                                    PNOR::LPC_SFC_MMIO_OFFSET));
                if( l_err ) { break; }

                curptr += 4;
                bytes_left -= 4;
            }
            if( l_err ) { break; }
        }
    } while(0);

    // No matter what, put the logic back into read mode
    errlHndl_t tmp_err = commandMode( false );
    if( tmp_err )
    {
        if( l_err )
        {
            // Keep the original error, commit this one as info
            tmp_err->plid(l_err->plid());
            tmp_err->setSev(ERRORLOG::ERRL_SEV_INFORMATIONAL);
            ERRORLOG::errlCommit(tmp_err,PNOR_COMP_ID);
        }
        else
        {
            l_err = tmp_err;
        }
    }

    TRACDCOMP( g_trac_pnor, EXIT_MRK"SfcAST2X00::sendSpiCmd> o_readData=%.2X, err=%.8X", o_readData == NULL ? 0 : o_readData[0], ERRL_GETEID_SAFE(l_err) );
    return l_err;
}

/**
 * @brief Enable write mode
 */
errlHndl_t SfcAST2X00::enableWriteMode( void )
{
    errlHndl_t l_err = NULL;
    TRACDCOMP( g_trac_pnor, ENTER_MRK"SfcAST2X00::enableWriteMode>" );

    /* Some flashes need it to be hammered */
    PNOR::NorStatusReg_t status;
    size_t i = 0;
    for( i = 0; i < 10; i++ )
    {
        // Send the command to enable writes
        uint8_t opcode = PNOR::SPI_JEDEC_WRITE_ENABLE;
        l_err = sendSpiCmd( opcode, NO_ADDRESS, 0, NULL, 0, NULL );
        if( l_err ) { break; }

        // Check to see if it worked
        opcode = PNOR::SPI_JEDEC_READ_STATUS;
        l_err = sendSpiCmd( opcode, NO_ADDRESS, 0, NULL, 1, &(status.data8) );
        if( l_err ) { break; }

        if( status.writeEnable )
        {
            break;
        }
    }

    if( !l_err && !status.writeEnable )
    {
        /*@
         * @errortype
         * @moduleid     PNOR::MOD_SFCAST2X00_ENABLEWRITEMODE
         * @reasoncode   PNOR::RC_CANNOT_ENABLE_WRITES
         * @userdata1[24:31]   Output from RDSR
         * @userdata1[32:63]   NOR chip id
         * @userdata2    <unused>
         * @devdesc      SfcAST2X00::enableWriteMode> Unable to enable
         *               write mode on the PNOR flash
         * @custdesc     Firmware error accessing flash during IPL
         */
        l_err = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                              PNOR::MOD_SFCAST2X00_ENABLEWRITEMODE,
                              PNOR::RC_CANNOT_ENABLE_WRITES,
                              TWO_UINT32_TO_UINT64( TO_UINT32(status.data8),
                                                    iv_norChipId),
                              0);
        // Limited in callout: no flash sub-target, so calling out processor
        l_err->addHwCallout( iv_proc,
                             HWAS::SRCI_PRIORITY_HIGH,
                             HWAS::NO_DECONFIG,
                             HWAS::GARD_NULL );
        addFFDC(l_err);
        l_err->collectTrace(PNOR_COMP_NAME);
    }

    TRACDCOMP( g_trac_pnor, EXIT_MRK"SfcAST2X00::enableWriteMode> err=%.8X", ERRL_GETEID_SAFE(l_err) );
    return l_err;
}

/**
 * @brief Poll for completion of SPI operation
 */
errlHndl_t SfcAST2X00::pollOpComplete( void )
{
    errlHndl_t l_err = NULL;
    TRACDCOMP( g_trac_pnor, ENTER_MRK"SfcAST2X00::pollOpComplete>" );

    do {
        // Send RDSR command until write-in-progress clears
        PNOR::NorStatusReg_t status;
        uint64_t poll_time = 0;
        uint64_t loop = 0;
        while( poll_time < MAX_WRITE_TIME_NS )
        {
            uint8_t opcode = PNOR::SPI_JEDEC_READ_STATUS;
            l_err = sendSpiCmd( opcode,
                                NO_ADDRESS,
                                0, NULL,
                                1, &(status.data8) );
            if( l_err ) { break; }

            // check if any op is still going
            if( !status.writeInProgress )
            {
                break;
            }

            // want to start out incrementing by small numbers then get bigger
            //  to avoid a really tight loop in an error case so we'll increase
            //  the wait each time through
            ++loop;
            nanosleep( 0, 100*loop );
            poll_time += 100*loop;
        }
        if( l_err ) { break; }

        TRACDCOMP(g_trac_pnor,"SfcAST2X00::pollOpComplete> command took %d ns", poll_time);

        // No status regs to check so just look for timeout
        if( status.writeInProgress )
        {
            TRACFCOMP( g_trac_pnor, "SfcAST2X00::pollOpComplete> Timeout during write or erase" );

            /*@
             * @errortype
             * @moduleid     PNOR::MOD_SFCAST2X00_POLLOPCOMPLETE
             * @reasoncode   PNOR::RC_SFC_TIMEOUT
             * @userdata1[0:31]   NOR Flash Chip ID
             * @userdata1[32:63]  Total poll time (ns)
             * @userdata2[56:63]  Output of RDSR command
             * @devdesc      SfcAST2X00::pollOpComplete> Timeout during
             *               write or erase operation
             * @custdesc     Hardware error accessing flash during IPL
             */
            l_err = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                            PNOR::MOD_SFCAST2X00_POLLOPCOMPLETE,
                                            PNOR::RC_SFC_TIMEOUT,
                                            TWO_UINT32_TO_UINT64(iv_norChipId,
                                                                 poll_time),
                                            status.data8);

            // Limited in callout: no PNOR target, so calling out processor
            l_err->addHwCallout(
                        iv_proc,
                        HWAS::SRCI_PRIORITY_HIGH,
                        HWAS::NO_DECONFIG,
                        HWAS::GARD_NULL );

            addFFDC(l_err);
            l_err->collectTrace(PNOR_COMP_NAME);
            break;
        }
    } while(0);

    TRACDCOMP( g_trac_pnor, EXIT_MRK"SfcAST2X00::pollOpComplete> err=%.8X", ERRL_GETEID_SAFE(l_err) );
    return l_err;
}

/**
 * @brief Add error registers to an existing Error Log
 */
void SfcAST2X00::addFFDC( errlHndl_t& io_errhdl )
{
    TRACDCOMP( g_trac_pnor, ENTER_MRK"SfcAST2X00::addFFDC>" );
    //@fixme-RTC:115212 - Create userdetails that includes chipid and SFDP data

    errlHndl_t l_err = NULL;

    //Read SFDP for FFDC
    uint32_t outdata[4];
    l_err = sendSpiCmd( PNOR::SPI_JEDEC_READ_SFDP,
                        0,
                        0, NULL,
                        16, reinterpret_cast<uint8_t*>(outdata) );
    if( l_err )
    {
        delete l_err;
        l_err = nullptr;
    }
    else
    {
        //Loop around and grab all 16 bytes
        for( size_t x=0; x<4; x++ )
        {
            TRACFCOMP( g_trac_pnor, "SFDP[%d]=%.8X", x, outdata[x] );
        }
    }

    TRACDCOMP( g_trac_pnor, EXIT_MRK"SfcAST2X00::addFFDC>" );
}
OpenPOWER on IntegriCloud