summaryrefslogtreecommitdiffstats
path: root/src/usr/pnor/pnor_sfcdd.H
blob: bfd3140a7be1bb14ab01d3138e6e7357e56d1ccf (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/pnor/pnor_sfcdd.H $                                   */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2011,2018                        */
/* [+] Google Inc.                                                        */
/* [+] 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                                                     */
#ifndef __PNOR_PNORDD_H
#define __PNOR_PNORDD_H

#include <limits.h>
#include <config.h>
#include <pnor/pnor_const.H>

namespace PNOR { class UdPnorDDParms; }
class SfcDD;

/** @file pnor_sfcdd.H
 *  @brief Provides the interfaces to the PNOR Device Driver
 */

/**
 *  @brief  PNOR Device Driver Class
 *     Provides access to the PNOR flash via the ECCB/LPC hardware
 */
class PnorSfcDD
{

  public:
    /**
     * @brief Performs a PNOR Read Operation
     *
     * @parm o_buffer  Buffer to read data into
     * @parm io_buflen  Input: Number of bytes to read,
     *       Output: Number of bytes actually read
     * @parm i_address  Offset into flash to read
     *
     * @return Error from operation
     */
    errlHndl_t readFlash(void* o_buffer,
                         size_t& io_buflen,
                         uint64_t i_address);

    /**
     * @brief Performs a PNOR Write Operation
     *
     * @parm i_buffer  Buffer to write data from
     * @parm io_buflen  Input: Number of bytes to write,
     *       Output: Number of bytes actually written
     * @parm i_address  Offset into flash to write
     *
     * @return Error from operation
     */
    errlHndl_t writeFlash(void* i_buffer,
                          size_t& io_buflen,
                          uint64_t i_address);

    /**
     * @brief Informs caller if PNORDD is using
     *        L3 Cache for fake PNOR or not.
     *
     * @return Indicate state of fake PNOR
     *         true = PNOR DD is using L3 Cache for fake PNOR
     *         false = PNOR DD not using L3 Cache for fake PNOR
     */
    bool usingL3Cache( );


    /**
     * @brief Retrieve bitstring of NOR workarounds
     * @return NOR workarounds (see VendorWorkarounds in norflash.H)
     */
    uint32_t getNorWorkarounds( void );

    /**
     * @brief Retrieve size of NOR flash
     * @return Size of PNOR in bytes
     */
    uint32_t getNorSize( void );

    /**
     * @brief Constructor
     *
     * @parm i_target     Processor Target connected to PNOR
     *       NOTE: i_target can only be used after targeting is loaded
     */
    PnorSfcDD( TARGETING::Target* i_target = NULL );

    /**
     * @brief Destructor
     */
    ~PnorSfcDD();

  protected:

    struct EraseInfo_t
    {
        uint32_t addr;  /**< Address of the erase block */
        uint32_t count;  /**< Num Erases of block */
    };

    /**
     * Enums for different operations that might be re-tried
     */
    enum RetryOp
    {
        RETRY_NOOP              = 0,
        RETRY_writeFlash        = 1,
        RETRY_readFlash         = 2,
        RETRY_eraseFlash        = 3,
    };

    /**
     * @brief Some general constants
     */
    enum {

        LPC_SFC_CMDREG_OFFSET = 0xF0000C00, /** LPC Offest to SFC Cmd Regs */
        LPC_SFC_CMDBUF_OFFSET = 0xF0000D00, /** LPC Off to SFC Cmd Buf space */

        ECCB_STAT_REG = 0x000B0022, /**< ECCB Status Reg (FW) */

        /**< OPB LPCM Sync FIR Reg - used to read the FIR*/
        OPB_LPCM_FIR_REG = 0x01010C00,

        /**< OPB LPCM Sync FIR Reg WOX_AND - used to clear the FIR */
        OPB_LPCM_FIR_WOX_AND_REG = 0x01010C01,

        /**< OPB LPCM Sync FIR Mask Reg WO_OR - used to set the mask */
        OPB_LPCM_FIR_MASK_WO_OR_REG = 0x01010C05,

        OPB_LPCM_FIR_ERROR_MASK = 0xFF00000000000000, /**< Error Bits MASK */

        // LPCHC reset-related registers
        OPB_MASTER_LS_CONTROL_REG = 0xC0010008, /**<OPBM LS Control Reg */
        LPCHC_RESET_REG = 0xC00120FC, /**<LPC HC Reset Register */

        ERASE_COUNT_MAX = 64,  /**<Max number of tracked erase blocks */
        ERASESIZE_BYTES_DEFAULT = 4 * KILOBYTE, /**< Min Erase Block (bytes) */
    };

    /**
     * @brief Read an address from LPC space
     *
     * @parm i_addr  Absolute LPC Address
     * @parm o_data  Buffer to read data into
     *
     * @return Error from operation
     */
    errlHndl_t readLPC(uint32_t i_addr,
                       uint32_t& o_data);

    /**
     * @brief Write an address from LPC space
     *

     * @parm o_data  Data to write
     *
     * @return Error from operation
     */
    errlHndl_t writeLPC(uint32_t i_addr,
                        uint32_t i_data);


    /**
     * @brief Erase a block of flash
     * @pre Mutex should already be locked before calling
     *
     * @parm i_address  Offset into flash to erase, aligned to erase block
     *
     * @return Error from operation
     */
    errlHndl_t eraseFlash(uint32_t i_address);

    /**
     * @brief Compare the existing data in 1 erase block of the flash with
     *   the incoming data and write or erase as needed
     * @pre Mutex should already be locked before calling
     *
     * @parm i_blockStart  Start of Erase Block we're writing to
     * @parm i_writeStart  Starting address where we want to write data.
     * @parm i_bytesToWrite  Number of 32-bit words to write
     * @parm i_data  Buffer of data to write
     *
     * @return Error from operation
     */
    errlHndl_t compareAndWriteBlock(uint32_t i_blockStart,
                                    uint32_t i_writeStart,
                                    size_t i_bytesToWrite,
                                    void* i_data);

    /**
     * @brief Determine the nearest flash address aligned to an erase block
     *
     * @parm i_address  Offset into flash
     *
     * @return Block-aligned flash address
     */
    uint32_t findEraseBlock(uint32_t i_address)
    {
        return (i_address - i_address%iv_eraseSizeBytes);
    };

    /**
     * @brief Determine the number of erase blocks that are included in
     *    the given range
     *
     * @parm i_address  Offset into flash
     * @parm i_byteSize  Number of bytes in range
     *
     * @return Number of full or partial erase blocks
     */
    uint32_t getNumAffectedBlocks(uint32_t i_address,
                                  size_t i_byteSize)
    {
        uint32_t blocks = 0;
        uint32_t addr = i_address;
        while( findEraseBlock(addr) < (i_address+i_byteSize) )
        {
            blocks++;
            addr += iv_eraseSizeBytes;
        }
        return blocks;
    };

    /**
     * @brief Returns if an operation should be retried and handles
     *        the error logs
     *
     * @pre   iv_error_recovery_failed should be 'false' before starting
     *        the retry loop as it gets checked in the function
     *
     * @parm i_op             Operation being retried
     * @parm io_err           Error log from current attempt of the operation
     * @parm io_original_err  Error log representing original error
     * @parm io_retry_count   Count of retries attempted
     *                        NOTE: count incremented when function returns true
     * @return bool           true if retry is necessary; false if not
     *
     * @post  Logic Table:
     *
     * |retries| io_err | io_original_err-     | io_retry_count | function |
     * | left  |        |  related actions     |                |  returns |
     * |-------|--------|----------------------|----------------|----------|
     * | N/A   | NULL   | if NULL then nothing | <nothing>      |  false   |
     * |       |        | if !NULL then        |                |          |
     * |       |        |  a) io_original_err  |                |          |
     * |       |        |     deleted          |                |          |
     * |       |        |  b) io_original_err  |                |          |
     * |       |        |     set to NULL      |                |          |
     * |-------|--------|----------------------|----------------|----------|
     * | No    | !NULL  | if NULL then nothing | <nothing>      |  false   |
     * |       |        | if !NULL then:       |                |          |
     * |       |        |  a) io_err committed |                |          |
     * |       |        |  b) io_err set to    |                |          |
     * |       |        |     io_original_err  |                |          |
     * |       |        |  c) io_original_err  |                |          |
     * |       |        |     set to NULL      |                |          |
     * |-------|--------|----------------------|----------------|----------|
     * | Yes   | !NULL  | if NULL then         | incremented    | true     |
     * |       |        |  a) io_err saved as  |                |          |
     * |       |        |     io_original_err  |                |          |
     * |       |        |  b) io_err set to    |                |          |
     * |       |        |     NULL             |                |          |
     * |       |        | if !NULL then        |                |          |
     * |       |        |  a) io_err info      |                |          |
     * |       |        |     added to         |                |          |
     * |       |        |     io_original_err  |                |          |
     * |       |        |  b) then io_err      |                |          |
     * |       |        |     deleted and set  |                |          |
     * |       |        |     to NULL          |                |          |
     * |-------|--------|----------------------|----------------|----------|
     *
     * NOTES: -- if iv_error_recovery_failed is set then no retries
     *        -- otherwise retries left determined by io_retry_count and
     *           PNORDD_MAX_RETRIES
     */
    bool shouldRetry( RetryOp     i_op,
                      errlHndl_t& io_err,
                      errlHndl_t& io_original_err,
                      uint8_t&    io_retry_count );


    /**
     * @brief Call SFC to write data to the PNOR flash, doing retries
     *        as needed
     * @pre Mutex should already be locked before calling
     *
     * @parm[in] i_addr  PNOR flash Address to write
     * @parm[in] i_size  Amount of data to write, in bytes.
     * @parm[in] i_data  Buffer containing data to write
     *
     * @return Error from operation
     */
    errlHndl_t _writeFlash( uint32_t i_addr,
                            size_t i_size,
                            void* i_data );

    /**
     * @brief Call SFC to read data from the PNOR flash, doing retries
     *        as needed
     * @pre Mutex should already be locked before calling
     *
     * @parm[in] i_addr  PNOR flash Address to read
     * @parm[in] i_size  Amount of data to read, in bytes.
     * @parm[out] o_data  Buffer to read data into
     *
     * @return Error from operation
     */
    errlHndl_t _readFlash( uint32_t i_addr,
                           size_t i_size,
                           void* o_data );

    /**
     * @brief Call SFC to erase the PNOR flash, doing retries
     *        as needed
     * @pre Mutex should already be locked before calling
     *
     * @parm i_address  Offset into flash to erase, aligned to erase block
     *
     * @return Error from operation
     */
    errlHndl_t _eraseFlash( uint32_t i_address );


  private: // Variables

   /**
    * @brief Global Mutex to prevent concurrent PNOR accesses to Master Proc
    *        This needs to be static so we can mutex across multiple
    *        instances of PnorDD
    */
   static mutex_t cv_mutex;

    /**
     * @brief Class Mutex used to prevent concurrent PNOR accesses
     */
    mutex_t iv_mutex;

    /**
     * @brief Mutex pointer to either class-specific or global mutex to prevent
     *        concurrent PNOR accesses.
     *        Each class uses a mutex; some share the static cv_mutex
     */
    mutex_t* iv_mutex_ptr;

    /**
     * @brief Track PNOR erases for wear monitoring
     */
    EraseInfo_t iv_erases[ERASE_COUNT_MAX];

    /**
     * @brief describes the erase block size, set based on NOR chip type
     *
     */
    uint32_t iv_eraseSizeBytes;

    /**
     * @brief CHIP ID or the NOR chip attached to SFC.
     *
     */
    uint32_t iv_norChipId;

    /**
     * @brief Associated Serial Flash Controller
     *
     */
    SfcDD* iv_sfc;

    /**
     * @brief Processor Target used to access PNOR device
     *
     */
    TARGETING::Target* iv_target;

    // Needed for testcases
    friend class PnorDdTest;
    friend class SfcIBMTest;
    friend class SfcAST2400Test;

    // let the UserDetails classes see internal structures
    friend class PNOR::UdPnorDDParms;
};


#endif
OpenPOWER on IntegriCloud