summaryrefslogtreecommitdiffstats
path: root/src/occ_405/firdata/pnor_mboxdd.c
blob: 463e950b1af263bd1d4fc01a7f26b3bd7f67d6ee (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/occ_405/firdata/pnor_mboxdd.c $                           */
/*                                                                        */
/* OpenPOWER OnChipController Project                                     */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 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                                                     */

/**
 *  @file ast_mboxdd.c
 *
 *  @brief Implementation of the PNOR access code on top of AST MBOX protocol
 */

/*****************************************************************************/
// I n c l u d e s
/*****************************************************************************/
#include <native.h>
#include <norflash.h>
#include <pnor_mboxdd.h>
#include <lpc.h>

errorHndl_t hwInit(pnorMbox_t* i_pnorMbox)
{
    errorHndl_t l_err = NO_ERROR;
    uint8_t* l_data;
    int i;
    do
    {
        //Current window starts closed
        i_pnorMbox->iv_curWindowOpen = false;
        i_pnorMbox->iv_mbox.iv_mboxMsgSeq = 1;
        l_err = initializeMbox();
        if (l_err)
        {
            TRAC_ERR("initializeMbox failed rc:0x%x", l_err);
            break;
        }

        //Send message to BMC Mbox to get MBOX info
        // This message gets the MBOX protocol version
        mboxMessage_t l_getInfoMsg;
        l_data = (uint8_t*)(&l_getInfoMsg);
        for (i = 0; i < BMC_MBOX_DATA_REGS; i++)
        {
            l_data[i] = 0;
        }

        l_getInfoMsg.iv_cmd = MBOX_C_GET_MBOX_INFO;
        put8(&l_getInfoMsg, 0, 2);
        l_err = doMessage(&i_pnorMbox->iv_mbox, &l_getInfoMsg);
        if (l_err)
        {
            TRAC_ERR("doMessage to ping BMC failed with rc=0x%x", l_err);
            break;
        }

        i_pnorMbox->iv_protocolVersion = get8(&l_getInfoMsg, 0);
        if (i_pnorMbox->iv_protocolVersion == 1)
        {
            i_pnorMbox->iv_blockShift = 12;
            i_pnorMbox->iv_readWindowSize = get16(&l_getInfoMsg, 1)
                                                << i_pnorMbox->iv_blockShift;
            i_pnorMbox->iv_writeWindowSize = get16(&l_getInfoMsg, 3)
                                                << i_pnorMbox->iv_blockShift;
        }
        else
        {
            i_pnorMbox->iv_blockShift = get8(&l_getInfoMsg, 5);
        }

        //Now get the size of the flash
        mboxMessage_t l_getSizeMsg;
        l_getSizeMsg.iv_cmd = MBOX_C_GET_FLASH_INFO;
        l_err = doMessage(&i_pnorMbox->iv_mbox, &l_getSizeMsg);
        if (l_err)
        {
            TRAC_ERR("doMessage failed to get flash size rc=0x%x", l_err);
            break;
        }

        if (i_pnorMbox->iv_protocolVersion == 1)
        {
            i_pnorMbox->iv_flashSize = get32(&l_getSizeMsg, 0);
            i_pnorMbox->iv_flashEraseSize = get32(&l_getSizeMsg, 4);
        }
        else
        {
            i_pnorMbox->iv_flashSize = get16(&l_getSizeMsg, 0)
                                            << i_pnorMbox->iv_blockShift;
            i_pnorMbox->iv_flashEraseSize = get16(&l_getSizeMsg, 2)
                                            << i_pnorMbox->iv_blockShift;
        }
    } while (0);
    return l_err;
}


errorHndl_t readFlash(pnorMbox_t* i_pnorMbox,
                      uint32_t i_addr,
                      size_t i_size,
                      void* o_data)
{
    errorHndl_t l_err = NO_ERROR;

    do
    {
        // Ensure we are operating on a 4-byte boundary
        if ((i_size % 4 != 0) && (i_addr % 4 != 0))
        {
            TRAC_ERR("readFlash: not on 4-byte boundary");
            return FAIL;
        }

        while (i_size)
        {
            uint32_t l_lpcAddr;
            size_t l_chunkLen;

            l_err = adjustMboxWindow(i_pnorMbox,
                                     false,
                                     i_addr,
                                     i_size,
                                     &l_lpcAddr,
                                     &l_chunkLen);

            if (l_err)
            {
                break;
            }

            //Directly access LPC to do read/write as BMC is setup now
            l_err = lpc_read(LPC_TRANS_FW, l_lpcAddr, o_data, l_chunkLen);

            if (l_err)
            {
                break;
            }

            i_addr += l_chunkLen;
            i_size -= l_chunkLen;
            o_data = (char*)o_data + l_chunkLen;
        }

        if(l_err)
        {
            break;
        }

    }
    while(0);

    return l_err;
}

errorHndl_t writeFlash(pnorMbox_t* i_pnorMbox,
                       uint32_t i_addr,
                       size_t i_size,
                       void* i_data)
{
    errorHndl_t l_err = NO_ERROR;

    do
    {
        // Ensure we are operating on a 4-byte boundary
        if (i_size % 4 != 0)
        {
            TRAC_ERR("writeFlash: not on 4-byte boundary");
            return FAIL;
        }


        errorHndl_t l_flushErr = NO_ERROR;

        while (i_size)
        {
            uint32_t l_lpcAddr;
            size_t l_chunkLen;

            l_err = adjustMboxWindow(i_pnorMbox,
                                     true,
                                     i_addr,
                                     i_size,
                                     &l_lpcAddr,
                                     &l_chunkLen);

            if (l_err)
            {
                break;
            }

            //Directly do LPC access to space pointed to by BMC
            l_err = lpc_write(LPC_TRANS_FW, l_lpcAddr, i_data, l_chunkLen);

            if (l_err)
            {
                break;
            }

            //Tell BMC to push data from LPC space into PNOR
            l_err = writeDirty(i_pnorMbox, i_addr, l_chunkLen);

            if (l_err)
            {
                break;
            }

            i_addr += l_chunkLen;
            i_size -= l_chunkLen;
            i_data = (char*)i_data + l_chunkLen;
        }

        /* We flush whether we had an error or not.
         *
         * NOTE: It would help the daemon a lot if we moved that out of here
         * and instead had a single flush call over a series of writes.
         */
        l_flushErr = writeFlush(i_pnorMbox);

        if ( l_err == NO_ERROR && l_flushErr )
        {
            l_err = l_flushErr;
        }

        if( l_err )
        {
            i_size = 0;
        }

        if(l_err)
        {
            break;
        }

    }
    while(0);

    return l_err;
}

errorHndl_t adjustMboxWindow(pnorMbox_t* i_pnorMbox,
                                    bool i_isWrite, uint32_t i_reqAddr,
                                    size_t i_reqSize, uint32_t *o_lpcAddr,
                                    size_t *o_chunkLen)
{
    errorHndl_t l_err = NO_ERROR;
    uint32_t l_pos, l_wSize, l_reqSize;

    do
    {
        /*
         * Handle the case where the window is already opened, is of
         * the right type and contains the requested address.
         */
        uint32_t l_wEnd = i_pnorMbox->iv_curWindowOffset +
                              i_pnorMbox->iv_curWindowSize;

        /* A read request can be serviced by a write window */
        if (i_pnorMbox->iv_curWindowOpen &&
            (i_pnorMbox->iv_curWindowWrite || !i_isWrite) &&
            i_reqAddr >= i_pnorMbox->iv_curWindowOffset && i_reqAddr < l_wEnd)
        {
            size_t l_gap = (l_wEnd - i_reqAddr);

            *o_lpcAddr = i_pnorMbox->iv_curWindowLpcOffset +
                          (i_reqAddr - i_pnorMbox->iv_curWindowOffset);
            if (i_reqSize <= l_gap)
            {
                *o_chunkLen = i_reqSize;
            }
            else
            {
                *o_chunkLen = l_gap;
            }
            return NO_ERROR;
        }

        /*
         * We need a window change, mark it closed first
         */
        i_pnorMbox->iv_curWindowOpen = false;

        /*
         * Then open the new one at the right position. The required
         * alignment differs between protocol versions
         */
        if (i_pnorMbox->iv_protocolVersion == 1)
        {
            l_wSize = i_isWrite ? i_pnorMbox->iv_writeWindowSize
                                : i_pnorMbox->iv_readWindowSize;
            l_pos = i_reqAddr & ~(l_wSize - 1);
            l_reqSize = 0;
        }
        else
        {
            uint32_t l_blockMask = (1u << i_pnorMbox->iv_blockShift) - 1;
            l_wSize = 0;
            l_pos = i_reqAddr & ~l_blockMask;
            l_reqSize = (((i_reqAddr + i_reqSize) + l_blockMask) & ~l_blockMask)
                          - l_pos;
        }

        mboxMessage_t winMsg;
        if (i_isWrite)
        {
            winMsg.iv_cmd = MBOX_C_CREATE_WRITE_WINDOW;
        }
        else
        {
            winMsg.iv_cmd = MBOX_C_CREATE_READ_WINDOW;
        }

        put16(&winMsg, 0, l_pos >> i_pnorMbox->iv_blockShift);
        put16(&winMsg, 2, l_reqSize >> i_pnorMbox->iv_blockShift);
        l_err = doMessage(&i_pnorMbox->iv_mbox, &winMsg);

        if (l_err)
        {
            break;
        }

        i_pnorMbox->iv_curWindowLpcOffset =
                        (get16(&winMsg,0)) << i_pnorMbox->iv_blockShift;

        if (i_pnorMbox->iv_protocolVersion == 1)
        {
            i_pnorMbox->iv_curWindowOffset = l_pos;
            i_pnorMbox->iv_curWindowLpcOffset =
                        (get16(&winMsg,0)) << i_pnorMbox->iv_blockShift;
            i_pnorMbox->iv_curWindowSize = l_wSize;
        }
        else
        {
            i_pnorMbox->iv_curWindowLpcOffset = (get16(&winMsg,0))
                             << i_pnorMbox->iv_blockShift;
            i_pnorMbox->iv_curWindowSize = (get16(&winMsg,2))
                             << i_pnorMbox->iv_blockShift;
            i_pnorMbox->iv_curWindowOffset = (get16(&winMsg,4))
                             << i_pnorMbox->iv_blockShift;
        }

        i_pnorMbox->iv_curWindowOpen = true;
        i_pnorMbox->iv_curWindowWrite = i_isWrite;

    }
    while (true);

    return l_err;
}

errorHndl_t writeDirty(pnorMbox_t* i_pnorMbox, uint32_t i_addr, size_t i_size)
{
    /* To pass a correct "size" for both protocol versions, we
     * calculate the block-aligned start and end.
     */
    uint32_t l_blockMask = (1u << i_pnorMbox->iv_blockShift) - 1;
    uint32_t l_start     = i_addr & ~l_blockMask;
    uint32_t l_end       = ((i_addr + i_size) + l_blockMask) & ~l_blockMask;

    mboxMessage_t dirtyMsg;
    dirtyMsg.iv_cmd = MBOX_C_MARK_WRITE_DIRTY;

    if (i_pnorMbox->iv_protocolVersion == 1)
    {
        put16(&dirtyMsg, 0, i_addr >> i_pnorMbox->iv_blockShift);
        put32(&dirtyMsg, 2, l_end - l_start);
    }
    else
    {
        put16(&dirtyMsg, 0, (i_addr - i_pnorMbox->iv_curWindowOffset)
                                >> i_pnorMbox->iv_blockShift);
        put16(&dirtyMsg, 2, (l_end - l_start) >> i_pnorMbox->iv_blockShift);
    }

    return doMessage(&i_pnorMbox->iv_mbox, &dirtyMsg);
}

errorHndl_t writeFlush(pnorMbox_t* i_pnorMbox)
{
    mboxMessage_t flushMsg;
    flushMsg.iv_cmd = MBOX_C_WRITE_FLUSH;

    put16(&flushMsg, 0, 0);
    put32(&flushMsg, 2, 0);

    return doMessage(&i_pnorMbox->iv_mbox, &flushMsg);
}
OpenPOWER on IntegriCloud