summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/powerpc/ppcn_60x/tod/tod.c
blob: 22c7090beccd33af4ec459a5facd0a0c18fd9b3d (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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/*
 *  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.
 *
 */

#define MIN_YEAR 1996

#include <stdlib.h>
#include "bsp.h"
#include "cmos.h"
#include "../nvram/mk48t18.h"

#define Bin2BCD(Value) (((Value / 10) << 4) | (Value % 10))
#define BCD2Bin(BcdValue) ((BcdValue >> 4) * 10 + (BcdValue & 0x0f))

/*
 * Private types
 */
typedef
void
(*PTIMESET)
(
	rtems_time_of_day *pTOD
);

typedef
boolean
(*PTIMEGET)
(
	rtems_time_of_day *pTOD
);

typedef struct _TIME_ENTRY_TABLE
{
    PTIMESET	SetTime;
    PTIMEGET	GetTime;
} TIME_ENTRY_TABLE, *PTIME_ENTRY_TABLE;

/*
 * Private routines
 */

/*
 * DS1385 specific routines
 */
static void timeDsSet(rtems_time_of_day *pTOD);
static boolean timeDsGet(rtems_time_of_day *pTOD);

/*
 * MK48T18 specific routines
 */
static void timeMkSet(rtems_time_of_day *pTOD);
static boolean timeMkGet(rtems_time_of_day *pTOD);

TIME_ENTRY_TABLE timeDsTable =
{
	timeDsSet,
	timeDsGet
};

TIME_ENTRY_TABLE timeMkTable =
{
	timeMkSet,
	timeMkGet
};

/*
 * Private variables
 */
static PTIME_ENTRY_TABLE pTimeFunc;

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

/*
 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
 */
uint8_t
GregorianDay(rtems_time_of_day *pTOD)
{
	boolean isLeap;
	unsigned long leapsToDate;
	unsigned long lastYear;
	unsigned long day;
	unsigned long MonthOffset[] = { 0, 31, 59, 90, 120, 151,
					181, 212, 243, 273, 304, 334 };

	lastYear=pTOD->year-1;

	/*
	 * Number of leap corrections to apply up to end of last year
	 */
	leapsToDate = lastYear/4 - lastYear/100 + lastYear/400;

	/*
	 * This year is a leap year if it is divisible by 4 except when it is
	 * divisible by 100 unless it is divisible by 400
	 *
	 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 will be
	 */
	isLeap = (pTOD->year%4==0) &&
			 ((pTOD->year%100!=0) || (pTOD->year%400==0));

	if(isLeap && (pTOD->month>2))
	{
		day=1;
	}
	else
	{
		day=0;
	}

	day += lastYear*365 + leapsToDate + MonthOffset[pTOD->month-1] +
		   pTOD->day;

	return((uint8_t)(day%7));
}

void
DsWriteRawClockRegister (
	uint8_t         Register,
	uint8_t         Value
)

/*++

Routine Description:

    This routine reads the specified realtime clock register.

    This function was added to bridge the BCD format of the IBM roms
    and the binary formate of NT

Arguments:

    Register - Supplies the number of the register whose value is read.

Return Value:

    The value of the register is returned as the function value.

--*/

{
	outport_byte((uint8_t*)RTC_PORT, Register & 0x7f);

	/* Read the realtime clock register value. */

	outport_byte((uint8_t*)(RTC_PORT + 1), Value);
	return;
}

uint8_t
DsReadRawClockRegister (
	uint8_t         Register
)

/*++

Routine Description:

    This routine reads the specified realtime clock register.

    This function was added to bridge the BCD format of the IBM roms
    and the binary formate of NT

Arguments:

    Register - Supplies the number of the register whose value is read.

Return Value:

    The value of the register is returned as the function value.

--*/

{
	uint8_t         ucDataByte;

	outport_byte((uint8_t*)RTC_PORT, Register & 0x7f);

	/* Read the realtime clock register value. */

	inport_byte((uint8_t*)(RTC_PORT + 1), ucDataByte);
	return ucDataByte;
}

void
DsWriteClockRegister (
	uint8_t         Register,
	uint8_t         Value
)

/*++

Routine Description:

    This routine writes the specified value to the specified realtime
    clock register.

Arguments:

    Register - Supplies the number of the register whose value is written.

    Value - Supplies the value that is written to the specified register.

Return Value:

    The value of the register is returned as the function value.

--*/

{
	uint8_t         BcdValue;

	BcdValue = Bin2BCD(Value);
	DsWriteRawClockRegister(Register, BcdValue);
	return;
}

uint8_t
DsReadClockRegister (
	uint8_t         Register
)

/*++

Routine Description:

    This routine reads the specified realtime clock register.

Arguments:

    Register - Supplies the number of the register whose value is read.

Return Value:

    The value of the register is returned as the function value.

--*/

{
	uint8_t         BcdValue;

	BcdValue =  DsReadRawClockRegister(Register);
	return BCD2Bin(BcdValue);
}

void
timeDsSet (
    rtems_time_of_day *pTOD
    )

/*++

Routine Description:

    This routine sets the realtime clock.

    N.B. This routine assumes that the caller has provided any required
        synchronization to set the realtime clock information.

Arguments:

    pTOD - Supplies a pointer to a time structure that specifies the
        realtime clock information.

Return Value:

    If the power to the realtime clock has not failed, then the time
    values are written to the realtime clock and a value of TRUE is
    returned. Otherwise, a value of FALSE is returned.

--*/

{
	uint8_t         ucDataByte;
	PCMOS_MAP pCMOS = (PCMOS_MAP)0;

	/* If the realtime clock battery is still functioning, then write */
	/* the realtime clock values, and return a function value of TRUE. */
	/* Otherwise, return a function value of FALSE. */

	ucDataByte = DsReadRawClockRegister(RTC_CONTROL_REGISTERD);
	if (ucDataByte&DS1385_REGD_VALID)
	{
		/* Set the realtime clock control to set the time. */

		ucDataByte = DS1385_REGB_HOURS_FMT | DS1385_REGB_SET_TIME;
		DsWriteRawClockRegister(RTC_CONTROL_REGISTERB, ucDataByte);

		/* Write the realtime clock values. */

		DsWriteClockRegister(RTC_YEAR,
				     (uint8_t)(pTOD->year%100));
		if(pTOD->year>=100)
		{
			DsWriteClockRegister((uint8_t)
					     ((unsigned long)&pCMOS->Century),
					     pTOD->year/100);
		}
		DsWriteClockRegister(RTC_MONTH,
				     (uint8_t)pTOD->month);
		DsWriteClockRegister(RTC_DAY_OF_MONTH,
				     (uint8_t)pTOD->day);
		DsWriteClockRegister(RTC_DAY_OF_WEEK,
				     (uint8_t)
				     (GregorianDay(pTOD) + 1));
		DsWriteClockRegister(RTC_HOUR,
				     (uint8_t)pTOD->hour);
		DsWriteClockRegister(RTC_MINUTE,
				     (uint8_t)pTOD->minute);
		DsWriteClockRegister(RTC_SECOND,
				     (uint8_t)pTOD->second);

		/* Set the realtime clock control to update the time. */

		ucDataByte &= ~DS1385_REGB_SET_TIME;
		DsWriteRawClockRegister(RTC_CONTROL_REGISTERB, ucDataByte);
		return;

	}
	else
	{
		return;
	}
}

boolean
timeDsGet (
    rtems_time_of_day *pTOD
    )

/*++

Routine Description:

    This routine queries the realtime clock.

Arguments:

    pTOD - Supplies a pointer to a time structure that receives
        the realtime clock information.

Return Value:

    If the power to the realtime clock has not failed, then the time
    values are read from the realtime clock and a value of TRUE is
    returned. Otherwise, a value of FALSE is returned.

--*/

{
	uint8_t         ucDataByte;
	PCMOS_MAP pCMOS = (PCMOS_MAP)0;

	/* If the realtime clock battery is still functioning, then read */
	/* the realtime clock values, and return a function value of TRUE. */
	/* Otherwise, return a function value of FALSE. */

	ucDataByte = DsReadRawClockRegister(RTC_CONTROL_REGISTERD);
	if(ucDataByte&DS1385_REGD_VALID)
	{
		/* Wait until the realtime clock is not being updated. */

		do
		{
			ucDataByte=DsReadRawClockRegister(RTC_CONTROL_REGISTERA);
		} while (ucDataByte&DS1385_REGA_UIP);

		/* Read the realtime clock values. */

		pTOD->year=(uint16_t)
				  (DsReadClockRegister(
				   (uint8_t)
				   (unsigned long)&pCMOS->Century)
				  *100 + DsReadClockRegister(RTC_YEAR));
		pTOD->month=DsReadClockRegister(RTC_MONTH);
		pTOD->day=DsReadClockRegister(RTC_DAY_OF_MONTH);
		pTOD->hour=DsReadClockRegister(RTC_HOUR);
		pTOD->minute=DsReadClockRegister(RTC_MINUTE);
		pTOD->second=DsReadClockRegister(RTC_SECOND);
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

void
timeMkSet (
    rtems_time_of_day *pTOD
    )

/*++

Routine Description:

    This routine sets the realtime clock.

    N.B. This routine assumes that the caller has provided any required
        synchronization to set the realtime clock information.

Arguments:

    pTOD - Supplies a pointer to a time structure that specifies the
        realtime clock information.

Return Value:

    If the power to the realtime clock has not failed, then the time
    values are written to the realtime clock and a value of TRUE is
    returned. Otherwise, a value of FALSE is returned.

--*/

{
	PMK48T18_NVRAM_MAP pNvRAM = MK48T18_BASE;

	/*
	 * Set the RTC into write mode
	 */
	pNvRAM->CMOS.Control|=MK48T18_CTRL_WRITE;
	EIEIO;

	/*
	 * Write the realtime clock values.
	 */

	pNvRAM->CMOS.Year = (uint8_t)Bin2BCD(pTOD->year%100);
	if(pTOD->year>=100)
	{
	    pNvRAM->CMOS.Century=(uint8_t)
	    			 Bin2BCD(pTOD->year/100);
	}
	pNvRAM->CMOS.Month  = (uint8_t)Bin2BCD(pTOD->month);
	pNvRAM->CMOS.Date   = (uint8_t)Bin2BCD(pTOD->day);
	pNvRAM->CMOS.Day    = (uint8_t)(GregorianDay(pTOD) + 1);
	pNvRAM->CMOS.Hour   = (uint8_t)Bin2BCD(pTOD->hour);
	pNvRAM->CMOS.Minute = (uint8_t)Bin2BCD(pTOD->minute);
	pNvRAM->CMOS.Second = (uint8_t)Bin2BCD(pTOD->second);

	/*
	 * Set the realtime clock control to update the time.
	 */

	EIEIO;
	pNvRAM->CMOS.Control&=~MK48T18_CTRL_WRITE;
}

boolean
timeMkGet (
    rtems_time_of_day *pTOD
    )

/*++

Routine Description:

    This routine queries the realtime clock.

    N.B. This routine is required to provide any synchronization necessary
         to query the realtime clock information.

Arguments:

    pTOD - Supplies a pointer to a time structure that receives
        the realtime clock information.

Return Value:

    If the power to the realtime clock has not failed, then the time
    values are read from the realtime clock and a value of TRUE is
    returned. Otherwise, a value of FALSE is returned.

--*/

{
	PMK48T18_NVRAM_MAP pNvRAM = MK48T18_BASE;

	/*
	 * Set the RTC into read mode
	 */
	pNvRAM->CMOS.Control|=MK48T18_CTRL_READ;
	EIEIO;

	/*
	 * Read the realtime clock values.
	 */

	pTOD->year = (uint16_t)(100*BCD2Bin(pNvRAM->CMOS.Century)+
					      BCD2Bin(pNvRAM->CMOS.Year));
	pTOD->month = (uint8_t)BCD2Bin(pNvRAM->CMOS.Month);
	pTOD->day = (uint8_t)BCD2Bin(pNvRAM->CMOS.Date);
	pTOD->hour = (uint8_t)BCD2Bin(pNvRAM->CMOS.Hour);
	pTOD->minute = (uint8_t)BCD2Bin(pNvRAM->CMOS.Minute);
	pTOD->second = (uint8_t)BCD2Bin(pNvRAM->CMOS.Second);

	/*
	 * Set the realtime clock control to normal mode.
	 */
	EIEIO;
	pNvRAM->CMOS.Control&=~MK48T18_CTRL_READ;

	return TRUE;
}

/*
 * Set up entry table
 */
void
InitializeRTC(void)
{
	rtems_status_code sc;

	switch(ucSystemType)
	{
		case SYS_TYPE_PPC1:
		{
			if(ucBoardRevMaj<5)
			{
				pTimeFunc=&timeDsTable;
				break;
			}
			/*
			 * For the 005 and later drop through to the PPC1a support
			 */
		}

		case SYS_TYPE_PPC1a:
		{
			pTimeFunc=&timeMkTable;
			break;
		}

		default:
		{
			pTimeFunc=&timeDsTable;
			break;
		}
	}

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

void setRealTimeToRTEMS()
{
	rtems_time_of_day rtc_tod;

	rtems_semaphore_obtain(semRTC, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
	(pTimeFunc->GetTime)(&rtc_tod);
	rtems_semaphore_release(semRTC);

	/*
	 * Millenium fix...
	 *
	 * If year is earlier than MIN_YEAR then assume the clock has wrapped from
	 * 1999 to 1900 so advance by a century
	 */
	if(rtc_tod.year<MIN_YEAR)
	{
		rtc_tod.year+=100;
		rtems_semaphore_obtain(semRTC, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
		(pTimeFunc->SetTime)(&rtc_tod);
		rtems_semaphore_release(semRTC);
	}

	rtc_tod.ticks=0;

	rtems_clock_set( &rtc_tod );
}

void setRealTimeFromRTEMS()
{
	rtems_time_of_day rtems_tod;

	rtems_clock_get( RTEMS_CLOCK_GET_TOD, &rtems_tod );
	rtems_semaphore_obtain(semRTC, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
	(pTimeFunc->SetTime)(&rtems_tod);
	rtems_semaphore_release(semRTC);
}

int checkRealTime()
{
  return 0;
}