summaryrefslogtreecommitdiffstats
path: root/libs/NVRam/nvm.c
blob: af34a6fb16d9e7990aca0bc808337fb022108b31 (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
////////////////////////////////////////////////////////////////////////////////
///
/// @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 <NVRam.h>
#include "bcm5719_NVM.h"
#include <stdio.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
#define REQ     ReqSet2
#define CLR     ReqClr2
#define WON     ArbWon2
#else /* Firmware */
#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)
{
    // 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;        
    }
}

void NVRam_enable(void)
{
    // TODO: enable both bits on read.
    NVM.Access.bits.Enable = 1;
}

void NVRam_disable(void)
{
    // TODO: enable both bits on read.
    NVM.Access.bits.Enable = 0;
}

static inline void NVRam_waitDone(void)
{
    printf("Waiting for  NVM.Command.bits.Done = %x\n", (uint32_t)NVM.Command.bits.Done);
    printf("Waiting for  NVM.Command.r32 = %x\n", (uint32_t)NVM.Command.r32);
    while(!NVM.Command.bits.Done)
    {

    }
    printf("NVM.Command.r32 = %x\n", (uint32_t)NVM.Command.r32);
}

static inline void NVRam_erase(uint32_t offset)
{
    if(NEEDS_ERASE)
    {
        // TODO: No need to erase when buffered.        
    }
}

bool NVRam_acquireLock(void)
{
    // Grab lock
    printf("NVM.SoftwareArbitration.r32 = %x\n", (uint32_t)NVM.SoftwareArbitration.r32);
    printf("NVM.SoftwareArbitration.bits.WON = %x\n", (uint32_t)NVM.SoftwareArbitration.bits.WON);
    NVM.SoftwareArbitration.bits.REQ = 1;
    printf("NVM.SoftwareArbitration.bits.WON w/req = %x\n", (uint32_t)NVM.SoftwareArbitration.bits.WON);
    printf("NVM.SoftwareArbitration.r32 w/req = %x\n", (uint32_t)NVM.SoftwareArbitration.r32);

    while(!NVM.SoftwareArbitration.bits.WON);


    printf("DEVICE->SoftwareArbitration.r32 = %x\n", (uint32_t)NVM.SoftwareArbitration.r32);

    return true;
}

bool NVRam_releaseLock(void)
{
    // Release locks
    // printf("DEVICE->SoftwareArbitration.r32 = %x\n", (uint32_t)NVM.SoftwareArbitration.r32);

    NVM.SoftwareArbitration.bits.CLR = 1;
    // printf("DEVICE->SoftwareArbitration.r32 = %x\n", (uint32_t)NVM.SoftwareArbitration.r32);
    return true;
}

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

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

    // printf("done.r32 = %x\n", (uint32_t)done.r32);
    // printf("Pre done write: NVM.Command.r32 = %x\n", (uint32_t)NVM.Command.r32);
    NVM.Command = done;
    // printf("Post done write: NVM.Command.r32 = %x\n", (uint32_t)NVM.Command.r32);
    NVM.Addr.r32 = address;
    NVM.Command = cmd;
    // printf("Post cmd write: NVM.Command.r32 = %x\n", (uint32_t)NVM.Command.r32);
    // printf("cmd = %x\n", (uint32_t)cmd.r32);

    NVRam_waitDone();

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

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

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

    NVRam_waitDone();
}

uint32_t NVRam_readByte(uint32_t address)
{
    RegNVMCommand_t cmd;
    cmd.bits.First = 1;
    cmd.bits.Last = 1;
    cmd.bits.Doit = 1;
    printf("cmd = %x\n", (uint32_t)cmd.r32);

    return NVRam_readByteInternal(address, cmd);
}

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

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

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

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

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

void NVRam_writeByte(uint32_t address, uint32_t data)
{
    RegNVMCommand_t cmd;
    cmd.bits.First = 1;
    cmd.bits.Last = 1;
    cmd.bits.Doit = 1;
    cmd.bits.Wr = 1;

    printf("cmd = %x\n", (uint32_t)cmd.r32);

    NVRam_writeByteInternal(address, data, cmd);
}

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

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

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

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

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