summaryrefslogtreecommitdiffstats
path: root/libs/NVRam/nvm.c
blob: bf494f69789f1bce6c8f78979d46774d98111112 (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
////////////////////////////////////////////////////////////////////////////////
///
/// @file       NVRam.c
///
/// @project
///
/// @brief      NVRam Support Routines
///
////////////////////////////////////////////////////////////////////////////////
///
////////////////////////////////////////////////////////////////////////////////
///
/// @copyright Copyright (c) 2018, Evan Lojewski
/// @cond
///
/// All rights reserved.
///
/// Redistribution and use in source and binary forms, with or without
/// modification, are permitted provided that the following conditions are met:
/// 1. Redistributions of source code must retain the above copyright notice,
/// this list of conditions and the following disclaimer.
/// 2. Redistributions in binary form must reproduce the above copyright notice,
/// this list of conditions and the following disclaimer in the documentation
/// and/or other materials provided with the distribution.
/// 3. Neither the name of the copyright holder nor the
/// names of its contributors may be used to endorse or promote products
/// derived from this software without specific prior written permission.
///
////////////////////////////////////////////////////////////////////////////////
///
/// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
/// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
/// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
/// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
/// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
/// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
/// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
/// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
/// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
/// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
/// POSSIBILITY OF SUCH DAMAGE.
/// @endcond
////////////////////////////////////////////////////////////////////////////////
#include "bcm5719_NVM.h"

#include <NVRam.h>

#define ATMEL_AT45DB0X1B_PAGE_POS (9u)
#define ATMEL_AT45DB0X1B_PAGE_SIZE (264u)
#define ATMEL_AT45DB0X1B_ERASE (false)

#define PAGE_POS ATMEL_AT45DB0X1B_PAGE_POS
#define PAGE_SIZE ATMEL_AT45DB0X1B_PAGE_SIZE
#define NEEDS_ERASE ATMEL_AT45DB0X1B_ERASE

#ifdef CXX_SIMULATOR
#include <arpa/inet.h>
#define REQ ReqSet2
#define CLR ReqClr2
#define WON ArbWon2
#else /* Firmware */
#define ntohl(__x__) (__x__)
#define htonl(__x__) (__x__)
#define REQ ReqSet0
#define CLR ReqClr0
#define WON ArbWon0
#endif

/**
 * @fn  uint32_t NVRam_translate(uint32_t address)
 *
 * @brief Translates a logical address to the NVM physical address.
 */
static inline uint32_t NVRam_translate(uint32_t address)
{
#if 0
    // Equation from NetXtremeII PG203
    if(NVM_NVM_CFG_1_PAGE_SIZE_264_BYTES == NVM.NvmCfg1.bits.PageSize)
    {
        return ((address / PAGE_SIZE) << PAGE_POS) + (address % PAGE_SIZE);
    }
    else
    {
        return address;        
    }
#else
    return address;
#endif
}

void NVRam_enable(void)
{
    NVM.Access.bits.Enable = 1;
}

void NVRam_enableWrites(void)
{
    NVM.Access.bits.WriteEnable = 1;
}

void NVRam_disable(void)
{
    NVM.Access.bits.Enable = 0;
}

void NVRam_disableWrites(void)
{
    NVM.Access.bits.WriteEnable = 0;
}

static inline void NVRam_waitDone(void)
{
    while (!NVM.Command.bits.Done)
    {
    }
}

bool NVRam_acquireLock(void)
{
    // Grab lock
    RegNVMSoftwareArbitration_t req;
    req.r32 = 0;
    req.bits.REQ = 1;
    NVM.SoftwareArbitration = req;

    while (!NVM.SoftwareArbitration.bits.WON)
    {
        // Spin
    }

    return true;
}

bool NVRam_releaseLock(void)
{
    // Release locks
    RegNVMSoftwareArbitration_t req;
    req.r32 = 0;
    req.bits.CLR = 1;
    NVM.SoftwareArbitration = req;

    return true;
}

static uint32_t NVRam_readWordInternal(uint32_t address, RegNVMCommand_t cmd)
{
    address = NVRam_translate(address);

    // Clear the done bit
    RegNVMCommand_t done;
    done.r32 = 0;
    done.bits.Done = 1;

    NVM.Command = done;
    NVM.Addr.r32 = address;
    NVM.Command = cmd;

    NVRam_waitDone();

    return ntohl(NVM.Read.r32);
}
static void NVRam_writeWordInternal(uint32_t address, uint32_t data,
                                    RegNVMCommand_t cmd)
{
    address = NVRam_translate(address);

    // Clear the done bit
    RegNVMCommand_t done;
    done.r32 = 0;
    done.bits.Done = 1;

    NVM.Command = done;
    NVM.Write.r32 = htonl(data);
    NVM.Addr.r32 = address;
    NVM.Command = cmd;

    NVRam_waitDone();
}

uint32_t NVRam_readWord(uint32_t address)
{
    RegNVMCommand_t cmd;
    cmd.r32 = 0;
    cmd.bits.First = 1;
    cmd.bits.Last = 1;
    cmd.bits.Doit = 1;

    return NVRam_readWordInternal(address, cmd);
}

void NVRam_read(uint32_t address, uint32_t *buffer, size_t words)
{
    if (!words)
    {
        // No data to read.
        return;
    }

    // First word.
    RegNVMCommand_t cmd;
    cmd.r32 = 0;
    cmd.bits.Doit = 1;
    cmd.bits.First = 1;

    while (words)
    {
        if (1 == words)
        {
            // Last word.
            cmd.bits.Last = 1;
        }

        *buffer = NVRam_readWordInternal(address, cmd);
        buffer++;
        words--;
        address += 4;

        // If we have more than one word, clear the first bit.
        cmd.bits.First = 0;
    }
}

void NVRam_writeWord(uint32_t address, uint32_t data)
{
    if (data != NVRam_readWord(address))
    {
        // Only write if different.

        RegNVMCommand_t cmd;
        cmd.r32 = 0;
        cmd.bits.First = 1;
        cmd.bits.Last = 1;
        cmd.bits.Doit = 1;
        cmd.bits.Wr = 1;

        NVRam_writeWordInternal(address, data, cmd);
    }
}

void NVRam_write(uint32_t address, uint32_t *buffer, size_t words)
{
#if 0
    if (!words)
    {
        // No bytes to read.
        return;
    }

    // Reduce beginning of buffer until we find the first different work.
    while(buffer[0] == NVRam_readWord(address))
    {
        address += 4;
        buffer++;
        words--;
    }

    // Trim words at end of buffer if they don't need to change.s
    while(words && (buffer[words-1] == NVRam_readWord(address + (words-1)*4)))
    {
        words--;
    }

    // First word.
    RegNVMCommand_t cmd;
    cmd.r32 = 0;
    cmd.bits.Doit = 1;
    cmd.bits.First = 1;
    cmd.bits.Wr = 1;

    while (words)
    {
        if (1 == words)
        {
            // Last word.
            cmd.bits.Last = 1;
        }

        NVRam_writeWordInternal(address, *buffer, cmd);
        buffer++;
        words--;
        address += 4;

        // If we have more than one word, clear the first bit.
        cmd.bits.First = 0;
    }
#else
    while (words)
    {
        NVRam_writeWord(address, *buffer);
        buffer++;
        words--;
        address += 4;
    }
#endif
}
OpenPOWER on IntegriCloud