summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/powerpc/ppcn_60x/nvram/nvram.c
blob: 753cdda86ae724bc07bc324b78e757cd08cce010 (plain) (blame)
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
/*
 *  This file contains the NvRAM driver for the PPCn_60x
 *
 *  COPYRIGHT (c) 1998 by Radstone Technology
 *
 *
 * THIS FILE IS PROVIDED TO YOU, THE USER, "AS IS", WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK
 * AS TO THE QUALITY AND PERFORMANCE OF ALL CODE IN THIS FILE IS WITH YOU.
 *
 * You are hereby granted permission to use, copy, modify, and distribute
 * this file, provided that this notice, plus the above copyright notice
 * and disclaimer, appears in all copies. Radstone Technology will provide
 * no support for this code.
 *
 */

#include <bsp.h>
#include "ds1385.h"
#include "mk48t18.h"
#include "stk11c68.h"

/*
 * Private types
 */
typedef
void
(*PNVRAMWRITE)
(
	uint32_t   ulOffset,
	uint8_t   ucByte
);

typedef
uint8_t  
(*PNVRAMREAD)
(
	uint32_t   ulOffset
);

typedef
void
(*PNVRAMCOMMIT)
(
);

typedef struct _NVRAM_ENTRY_TABLE
{
	PNVRAMWRITE	nvramWrite;
	PNVRAMREAD	nvramRead;
	PNVRAMCOMMIT	nvramCommit;
	uint32_t  	nvramSize;
} NVRAM_ENTRY_TABLE, *PNVRAM_ENTRY_TABLE;

/*
 * Private routines
 */

/*
 * This routine provides a stub for NvRAM devices which
 * do not require a commit operation
 */
static void nvramCommitStub();

/*
 * DS1385 specific routines
 */
static void nvramDsWrite(uint32_t   ulOffset, uint8_t   ucByte);
static uint8_t   nvramDsRead(uint32_t   ulOffset);

/*
 * MK48T18 specific routines
 */
static void nvramMkWrite(uint32_t   ulOffset, uint8_t   ucByte);
static uint8_t   nvramMkRead(uint32_t   ulOffset);

/*
 * STK11C68 specific routines
 */
static void nvramStk11C68Commit();
/*
 * STK11C88 specific routines
 */
static void nvramStk11C88Commit();

/*
 * NvRAM hook tables
 */
NVRAM_ENTRY_TABLE nvramDsTable =
{
	nvramDsWrite,
	nvramDsRead,
	nvramCommitStub,
	DS1385_NVSIZE
};

NVRAM_ENTRY_TABLE nvramMkTable =
{
	nvramMkWrite,
	nvramMkRead,
	nvramCommitStub,
	MK48T18_NVSIZE
};

/*
 * As the STK devicxe is at the same address as the MK device,
 * the MK read/write routines may be used
 */
NVRAM_ENTRY_TABLE nvramStkTable =
{
	nvramMkWrite,
	nvramMkRead,
	nvramStk11C68Commit,
	STK11C68_NVSIZE
};

NVRAM_ENTRY_TABLE nvramStk88Table =
{
	nvramMkWrite,
	nvramMkRead,
	nvramStk11C88Commit,
	STK11C88_NVSIZE
};

/*
 * Private variables
 */
static PNVRAM_ENTRY_TABLE pNvRAMFunc;
static boolean		bNvRAMChanged=FALSE;
static uint32_t  	ulPRePOSAreaLength;
static uint32_t  	ulPRePOSAreaOffset;

/*
 * Mutual-exclusion semaphore
 */
static rtems_id semNvRAM;

/*
 * These routines support the ds1385
 */
static uint8_t   nvramDsRead(uint32_t   ulOffset)
{
	uint8_t   ucTemp;

	ucTemp = ulOffset & 0xff;
	outport_byte(DS1385_PORT_BASE, ucTemp);

	ucTemp = (ulOffset >> 8) & 0xf;
	outport_byte((DS1385_PORT_BASE + 1) , ucTemp);

	inport_byte(DS1385_PORT_BASE+3, ucTemp);
	return(ucTemp);
}

static void nvramDsWrite(uint32_t   ulOffset, uint8_t   ucData)
{
	uint8_t   ucTemp;

	ucTemp = (uint8_t)(ulOffset & 0xff);
	outport_byte(DS1385_PORT_BASE, (uint8_t) ucTemp);

	ucTemp = (uint8_t)((ulOffset >> 8) & 0xf);
	outport_byte((DS1385_PORT_BASE + 1) , (uint8_t)ucTemp);

	outport_byte((DS1385_PORT_BASE+3), ucData);
}

/*
 * These routines support the MK48T18 and STK11C68
 */
static uint8_t   nvramMkRead(uint32_t   ulOffset)
{
	uint8_t   *pNvRAM = (uint8_t*)MK48T18_BASE;

	return(pNvRAM[ulOffset]);
}

static void nvramMkWrite(uint32_t   ulOffset, uint8_t   ucData)
{
	uint8_t   *pNvRAM = (uint8_t*)MK48T18_BASE;

	pNvRAM[ulOffset]=ucData;
}

/*
 * This routine provides a stub for NvRAM devices which
 * do not require a commit operation
 */
static void nvramCommitStub()
{
}

/*
 * This routine triggers a transfer from the NvRAM to the
 * EE array in the STK11C68 device
 */
static void nvramStk11C68Commit()
{
#if 0
	rtems_interval		ticks_per_second;
	rtems_status_code	status;
#endif

	rtems_semaphore_obtain(semNvRAM, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
	/*
	 * Issue Store command
	 */
	EIEIO;
	(void)pNvRAMFunc->nvramRead(0x0000);
	EIEIO;
	(void)pNvRAMFunc->nvramRead(0x1555);
	EIEIO;
	(void)pNvRAMFunc->nvramRead(0x0aaa);
	EIEIO;
	(void)pNvRAMFunc->nvramRead(0x1fff);
	EIEIO;
	(void)pNvRAMFunc->nvramRead(0x10f0);
	EIEIO;
	(void)pNvRAMFunc->nvramRead(0x0f0f);
	EIEIO;
	/*
	 * Delay for 10mS to allow store to
	 * complete
	 */
#if 0
	status = rtems_clock_get(
		RTEMS_CLOCK_GET_TICKS_PER_SECOND,
		&ticks_per_second
	);

	status = rtems_task_wake_after(ticks_per_second/100);
#endif
	bNvRAMChanged=FALSE;

	rtems_semaphore_release(semNvRAM);
}

/*
 * This routine triggers a transfer from the NvRAM to the
 * EE array in the STK11C88 device
 */
static void nvramStk11C88Commit()
{
#if 0
	rtems_interval		ticks_per_second;
	rtems_status_code	status;
#endif

	rtems_semaphore_obtain(semNvRAM, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
	/*
	 * Issue Store command
	 */
	EIEIO;
	(void)pNvRAMFunc->nvramRead(0x0e38);
	EIEIO;
	(void)pNvRAMFunc->nvramRead(0x31c7);
	EIEIO;
	(void)pNvRAMFunc->nvramRead(0x03e0);
	EIEIO;
	(void)pNvRAMFunc->nvramRead(0x3c1f);
	EIEIO;
	(void)pNvRAMFunc->nvramRead(0x303f);
	EIEIO;
	(void)pNvRAMFunc->nvramRead(0x0fc0);
	EIEIO;
	/*
	 * Delay for 10mS to allow store to
	 * complete
	 */
#if 0
	status = rtems_clock_get(
		RTEMS_CLOCK_GET_TICKS_PER_SECOND,
		&ticks_per_second
	);

	status = rtems_task_wake_after(ticks_per_second/100);
#endif
	bNvRAMChanged=FALSE;

	rtems_semaphore_release(semNvRAM);
}

/*
 * These are the publically accessable routines
 */
/*
 * This routine returns the size of the NvRAM
 */
uint32_t   SizeNvRAM()
{
	return(ulPRePOSAreaLength);
}

/*
 * This routine commits changes to the NvRAM
 */
void CommitNvRAM()
{
	if(bNvRAMChanged)
	{
		(pNvRAMFunc->nvramCommit)();
	}
}

/*
 * This routine reads a byte from the NvRAM
 */
rtems_status_code ReadNvRAM8(uint32_t   ulOffset, uint8_t   *pucData)
{
	if(ulOffset>ulPRePOSAreaLength)
	{
		return RTEMS_INVALID_ADDRESS;
	}
	rtems_semaphore_obtain(semNvRAM, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
	*pucData=pNvRAMFunc->nvramRead(ulPRePOSAreaOffset+ulOffset);
	rtems_semaphore_release(semNvRAM);
	return(RTEMS_SUCCESSFUL);
}

/*
 * This routine writes a byte to the NvRAM
 */
rtems_status_code WriteNvRAM8(uint32_t   ulOffset, uint8_t   ucValue)
{
	if(ulOffset>ulPRePOSAreaLength)
	{
		return RTEMS_INVALID_ADDRESS;
	}
	rtems_semaphore_obtain(semNvRAM, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
	pNvRAMFunc->nvramWrite(ulPRePOSAreaOffset+ulOffset, ucValue);
	bNvRAMChanged=TRUE;
	rtems_semaphore_release(semNvRAM);
	return(RTEMS_SUCCESSFUL);
}

/*
 * This routine reads a block of bytes from the NvRAM
 */
rtems_status_code ReadNvRAMBlock(
  uint32_t   ulOffset, uint8_t   *pucData, uint32_t   length)
{
	uint32_t   i;

	if((ulOffset + length) > ulPRePOSAreaLength)
	{
		return RTEMS_INVALID_ADDRESS;
	}
	rtems_semaphore_obtain(semNvRAM, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
	for ( i=0 ; i<length ; i++ )
		pucData[i] =
			pNvRAMFunc->nvramRead(ulPRePOSAreaOffset+ulOffset+i);
	rtems_semaphore_release(semNvRAM);
	return(RTEMS_SUCCESSFUL);
}

/*
 * This routine writes a block of bytes to the NvRAM
 */
rtems_status_code WriteNvRAMBlock(
  uint32_t   ulOffset, uint8_t   *ucValue, uint32_t   length)
{
	uint32_t   i;

	if((ulOffset + length) > ulPRePOSAreaLength)
	{
		return RTEMS_INVALID_ADDRESS;
	}
	rtems_semaphore_obtain(semNvRAM, RTEMS_WAIT, RTEMS_NO_TIMEOUT);

	for ( i=0 ; i<length ; i++ )
		pNvRAMFunc->nvramWrite(
			ulPRePOSAreaOffset+ulOffset+i, ucValue[i]);
	bNvRAMChanged=TRUE;
	rtems_semaphore_release(semNvRAM);
	return(RTEMS_SUCCESSFUL);
}

/*
 * The NVRAM holds data in Big-Endian format
 */
rtems_status_code ReadNvRAM16 (uint32_t   ulOffset, uint16_t   *pusData)
{
	uint32_t   ulTrueOffset=ulPRePOSAreaOffset+ulOffset;

	if(ulOffset>ulPRePOSAreaLength)
	{
		return RTEMS_INVALID_ADDRESS;
	}
	rtems_semaphore_obtain(semNvRAM, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
        *pusData=(pNvRAMFunc->nvramRead(ulTrueOffset) << 8) +
		 (pNvRAMFunc->nvramRead(ulTrueOffset + 1));
	rtems_semaphore_release(semNvRAM);
	return(RTEMS_SUCCESSFUL);
}

rtems_status_code WriteNvRAM16 (uint32_t   ulOffset, uint16_t   usValue)
{
	uint32_t   ulTrueOffset=ulPRePOSAreaOffset+ulOffset;

	if(ulOffset>ulPRePOSAreaLength)
	{
		return RTEMS_INVALID_ADDRESS;
	}
	rtems_semaphore_obtain(semNvRAM, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
        pNvRAMFunc->nvramWrite(ulTrueOffset, (uint8_t) (usValue >> 8));
        pNvRAMFunc->nvramWrite(ulTrueOffset + 1, (uint8_t) usValue);
	bNvRAMChanged=TRUE;
	rtems_semaphore_release(semNvRAM);
	return(RTEMS_SUCCESSFUL);
}

rtems_status_code ReadNvRAM32 (uint32_t   ulOffset, uint32_t   *pulData)
{
	uint32_t   ulTrueOffset=ulPRePOSAreaOffset+ulOffset;

	if(ulOffset>ulPRePOSAreaLength)
	{
		return RTEMS_INVALID_ADDRESS;
	}
	rtems_semaphore_obtain(semNvRAM, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
        *pulData=(pNvRAMFunc->nvramRead(ulTrueOffset) << 24) +
		 (pNvRAMFunc->nvramRead(ulTrueOffset + 1) << 16) +
		 (pNvRAMFunc->nvramRead(ulTrueOffset + 2) << 8) +
		 (pNvRAMFunc->nvramRead(ulTrueOffset + 3));
	rtems_semaphore_release(semNvRAM);
	return(RTEMS_SUCCESSFUL);
}

rtems_status_code WriteNvRAM32 (uint32_t   ulOffset, uint32_t   ulValue)
{
	uint32_t   ulTrueOffset=ulPRePOSAreaOffset+ulOffset;

	if(ulOffset>ulPRePOSAreaLength)
	{
		return RTEMS_INVALID_ADDRESS;
	}
	rtems_semaphore_obtain(semNvRAM, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
        pNvRAMFunc->nvramWrite(ulTrueOffset, (uint8_t) (ulValue >> 24));
        pNvRAMFunc->nvramWrite(ulTrueOffset + 1, (uint8_t) (ulValue >> 16));
        pNvRAMFunc->nvramWrite(ulTrueOffset + 2, (uint8_t) (ulValue >> 8));
        pNvRAMFunc->nvramWrite(ulTrueOffset + 3, (uint8_t) ulValue);
	bNvRAMChanged=TRUE;
	rtems_semaphore_release(semNvRAM);
	return(RTEMS_SUCCESSFUL);
}

void
InitializeNvRAM(void)
{
	PHEADER pNvHeader = (PHEADER)0;
	rtems_status_code sc;
	uint32_t   ulLength, ulOffset;

	if(ucSystemType==SYS_TYPE_PPC1)
	{
		if(ucBoardRevMaj<5)
		{
			pNvRAMFunc=&nvramDsTable;
		}
		else
		{
			pNvRAMFunc=&nvramMkTable;
		}
	}
	else if(ucSystemType==SYS_TYPE_PPC1a)
	{
		pNvRAMFunc=&nvramMkTable;
	}
	else if(ucSystemType==SYS_TYPE_PPC4)
	{
		pNvRAMFunc=&nvramStk88Table;
	}
	else
	{
		pNvRAMFunc=&nvramStkTable;
	}

        /*
         * Set up mutex semaphore
         */
        sc = rtems_semaphore_create (
                rtems_build_name ('N', 'V', 'R', 's'),
                1,
                RTEMS_BINARY_SEMAPHORE |
                RTEMS_INHERIT_PRIORITY |
                RTEMS_PRIORITY,
                RTEMS_NO_PRIORITY,
                &semNvRAM);
        if (sc != RTEMS_SUCCESSFUL)
        {
                rtems_fatal_error_occurred (sc);
        }

	/*
	 * Initially access the whole of NvRAM until we determine where the
	 * OS Area is located.
	 */
	ulPRePOSAreaLength=0xffffffff;
	ulPRePOSAreaOffset=0;

	/*
	 * Access the header at the start of NvRAM
	 */
	ReadNvRAM32((uint32_t)(&pNvHeader->OSAreaLength), &ulLength);
	ReadNvRAM32((uint32_t)(&pNvHeader->OSAreaAddress), &ulOffset);

	/*
	 * Now set limits for future accesses
	 */
	ulPRePOSAreaLength=ulLength;
	ulPRePOSAreaOffset=ulOffset;
}