summaryrefslogtreecommitdiff
path: root/usr/src/uts/i86pc/io/todpc_subr.c
blob: 19042d49b789eaea377b8bbf18961864b10ee2b4 (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
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2018 Gary Mills
 * Copyright 2012 Nexenta Systems, Inc.  All rights reserved.
 */
/*
 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
/*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
/*	  All Rights Reserved  	*/

/*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
/*	  All Rights Reserved	*/

#include <sys/param.h>
#include <sys/time.h>
#include <sys/systm.h>

#include <sys/cpuvar.h>
#include <sys/clock.h>
#include <sys/debug.h>
#include <sys/rtc.h>
#include <sys/archsystm.h>
#include <sys/sysmacros.h>
#include <sys/lockstat.h>
#include <sys/stat.h>
#include <sys/sunddi.h>
#include <sys/ddi.h>

#include <sys/acpi/acpi.h>
#include <sys/acpica.h>

static int todpc_rtcget(unsigned char *buf);
static void todpc_rtcput(unsigned char *buf);

#define	CLOCK_RES	1000		/* 1 microsec in nanosecs */

int clock_res = CLOCK_RES;

/*
 * The minimum sleep time till an alarm can be fired.
 * This can be tuned in /etc/system, but if the value is too small,
 * there is a danger that it will be missed if it takes too long to
 * get from the set point to sleep.  Or that it can fire quickly, and
 * generate a power spike on the hardware.  And small values are
 * probably only usefull for test setups.
 */
int clock_min_alarm = 4;

/*
 * Machine-dependent clock routines.
 */

extern long gmt_lag;

struct rtc_offset {
	int8_t	loaded;
	uint8_t	day_alrm;
	uint8_t mon_alrm;
	uint8_t	century;
};

static struct rtc_offset pc_rtc_offset = {0, 0, 0, 0};


/*
 * Entry point for ACPI to pass RTC or other clock values that
 * are useful to TOD.
 */
void
pc_tod_set_rtc_offsets(ACPI_TABLE_FADT *fadt)
{
	int		ok = 0;

	/*
	 * ASSERT is for debugging, but we don't want the machine
	 * falling over because for some reason we didn't get a valid
	 * pointer.
	 */
	ASSERT(fadt);
	if (fadt == NULL) {
		return;
	}

	if (fadt->DayAlarm) {
		pc_rtc_offset.day_alrm = fadt->DayAlarm;
		ok = 1;
	}

	if (fadt->MonthAlarm) {
		pc_rtc_offset.mon_alrm = fadt->MonthAlarm;
		ok = 1;
	}

	if (fadt->Century) {
		pc_rtc_offset.century = fadt->Century;
		ok = 1;
	}

	pc_rtc_offset.loaded = ok;
}


/*
 * Write the specified time into the clock chip.
 * Must be called with tod_lock held.
 */
/*ARGSUSED*/
static void
todpc_set(tod_ops_t *top, timestruc_t ts)
{
	todinfo_t tod = utc_to_tod(ts.tv_sec - ggmtl());
	struct rtc_t rtc;

	ASSERT(MUTEX_HELD(&tod_lock));

	if (todpc_rtcget((unsigned char *)&rtc))
		return;

	/*
	 * rtc bytes are in binary-coded decimal, so we have to convert.
	 * We assume that we wrap the rtc year back to zero at 2000.
	 */
	/* LINTED: YRBASE = 0 for x86 */
	tod.tod_year -= YRBASE;
	if (tod.tod_year >= 100) {
		tod.tod_year -= 100;
		rtc.rtc_century = BYTE_TO_BCD(20); /* 20xx year */
	} else
		rtc.rtc_century = BYTE_TO_BCD(19); /* 19xx year */
	rtc.rtc_yr	= BYTE_TO_BCD(tod.tod_year);
	rtc.rtc_mon	= BYTE_TO_BCD(tod.tod_month);
	rtc.rtc_dom	= BYTE_TO_BCD(tod.tod_day);
	/* dow < 10, so no conversion */
	rtc.rtc_dow	= (unsigned char)tod.tod_dow;
	rtc.rtc_hr	= BYTE_TO_BCD(tod.tod_hour);
	rtc.rtc_min	= BYTE_TO_BCD(tod.tod_min);
	rtc.rtc_sec	= BYTE_TO_BCD(tod.tod_sec);

	todpc_rtcput((unsigned char *)&rtc);
}

/*
 * Read the current time from the clock chip and convert to UNIX form.
 * Assumes that the year in the clock chip is valid.
 * Must be called with tod_lock held.
 */
/*ARGSUSED*/
static timestruc_t
todpc_get(tod_ops_t *top)
{
	timestruc_t ts;
	todinfo_t tod;
	struct rtc_t rtc;
	int compute_century;
	static int century_warn = 1; /* only warn once, not each time called */
	static int range_warn = 1;

	ASSERT(MUTEX_HELD(&tod_lock));

	if (todpc_rtcget((unsigned char *)&rtc)) {
		tod_status_set(TOD_GET_FAILED);
		return (hrestime);
	}

	/* assume that we wrap the rtc year back to zero at 2000 */
	tod.tod_year	= BCD_TO_BYTE(rtc.rtc_yr);
	if (tod.tod_year < 69) {
		if (range_warn && tod.tod_year > 38) {
			cmn_err(CE_WARN, "hardware real-time clock is out "
			    "of range -- time needs to be reset");
			range_warn = 0;
		}
		tod.tod_year += 100 + YRBASE; /* 20xx year */
		compute_century = 20;
	} else {
		/* LINTED: YRBASE = 0 for x86 */
		tod.tod_year += YRBASE; /* 19xx year */
		compute_century = 19;
	}
	if (century_warn && BCD_TO_BYTE(rtc.rtc_century) != compute_century) {
		cmn_err(CE_NOTE,
		    "The hardware real-time clock appears to have the "
		    "wrong century: %d.\nSolaris will still operate "
		    "correctly, but other OS's/firmware agents may "
		    "not.\nUse date(1) to set the date to the current "
		    "time to correct the RTC.",
		    BCD_TO_BYTE(rtc.rtc_century));
		century_warn = 0;
	}
	tod.tod_month	= BCD_TO_BYTE(rtc.rtc_mon);
	tod.tod_day	= BCD_TO_BYTE(rtc.rtc_dom);
	tod.tod_dow	= rtc.rtc_dow;	/* dow < 10, so no conversion needed */
	tod.tod_hour	= BCD_TO_BYTE(rtc.rtc_hr);
	tod.tod_min	= BCD_TO_BYTE(rtc.rtc_min);
	tod.tod_sec	= BCD_TO_BYTE(rtc.rtc_sec);

	/* read was successful so ensure failure flag is clear */
	tod_status_clear(TOD_GET_FAILED);

	ts.tv_sec = tod_to_utc(tod) + ggmtl();
	ts.tv_nsec = 0;

	return (ts);
}

#include <sys/promif.h>
/*
 * Write the specified wakeup alarm into the clock chip.
 * Must be called with tod_lock held.
 */
void
/*ARGSUSED*/
todpc_setalarm(tod_ops_t *top, int nsecs)
{
	struct rtc_t rtc;
	int delta, asec, amin, ahr, adom, amon;
	int day_alrm = pc_rtc_offset.day_alrm;
	int mon_alrm = pc_rtc_offset.mon_alrm;

	ASSERT(MUTEX_HELD(&tod_lock));

	/* A delay of zero is not allowed */
	if (nsecs == 0)
		return;

	/* Make sure that we delay no less than the minimum time */
	if (nsecs < clock_min_alarm)
		nsecs = clock_min_alarm;

	if (todpc_rtcget((unsigned char *)&rtc))
		return;

	/*
	 * Compute alarm secs, mins and hrs, and where appropriate, dom
	 * and mon.  rtc bytes are in binary-coded decimal, so we have
	 * to convert.
	 */
	delta = nsecs + BCD_TO_BYTE(rtc.rtc_sec);
	asec = delta % 60;

	delta = (delta / 60) + BCD_TO_BYTE(rtc.rtc_min);
	amin = delta % 60;

	delta = (delta / 60) + BCD_TO_BYTE(rtc.rtc_hr);
	ahr  = delta % 24;

	if (day_alrm == 0 && delta >= 24) {
		prom_printf("No day alarm - set to end of today!\n");
		asec = 59;
		amin = 59;
		ahr  = 23;
	} else {
		int mon = BCD_TO_BYTE(rtc.rtc_mon);
		static int dpm[] =
		    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

		adom = (delta / 24) + BCD_TO_BYTE(rtc.rtc_dom);

		if (mon_alrm == 0) {
			if (adom > dpm[mon]) {
				prom_printf("No mon alarm - "
				    "set to end of current month!\n");
				asec = 59;
				amin = 59;
				ahr  = 23;
				adom = dpm[mon];
			}
		} else {
			for (amon = mon;
			    amon <= 12 && adom > dpm[amon]; amon++) {
				adom -= dpm[amon];
			}
			if (amon > 12) {
				prom_printf("Alarm too far in future - "
				    "set to end of current year!\n");
				asec = 59;
				amin = 59;
				ahr  = 23;
				adom = dpm[12];
				amon = 12;
			}
			rtc.rtc_amon = BYTE_TO_BCD(amon);
		}

		rtc.rtc_adom = BYTE_TO_BCD(adom);
	}

	rtc.rtc_asec = BYTE_TO_BCD(asec);
	rtc.rtc_amin = BYTE_TO_BCD(amin);
	rtc.rtc_ahr  = BYTE_TO_BCD(ahr);

	rtc.rtc_statusb |= RTC_AIE;	/* Enable alarm interrupt */

	todpc_rtcput((unsigned char *)&rtc);
}

/*
 * Clear an alarm.  This is effectively setting an alarm of 0.
 */
void
/*ARGSUSED*/
todpc_clralarm(tod_ops_t *top)
{
	mutex_enter(&tod_lock);
	todpc_setalarm(top, 0);
	mutex_exit(&tod_lock);
}

/*
 * Routine to read contents of real time clock to the specified buffer.
 * Returns ENXIO if clock not valid, or EAGAIN if clock data cannot be read
 * else 0.
 * Some RTC hardware is very slow at asserting the validity flag on
 * startup.  The routine will busy wait for the RTC to become valid.
 * The routine will also busy wait for the Update-In-Progress flag to clear.
 * On completion of the reads the Seconds register is re-read and the
 * UIP flag is rechecked to confirm that an clock update did not occur
 * during the accesses.  Routine will error exit after 256 attempts.
 * (See bugid 1158298.)
 * Routine returns RTC_NREG (which is 15) bytes of data, as given in the
 * technical reference.  This data includes both time and status registers.
 */

static int
todpc_rtcget(unsigned char *buf)
{
	unsigned char	reg;
	int		i;
	int		uip_try = 256;
	int		vrt_try = 512;
	unsigned char	*rawp;
	unsigned char	century = RTC_CENTURY;
	unsigned char	day_alrm;
	unsigned char	mon_alrm;

	ASSERT(MUTEX_HELD(&tod_lock));

	day_alrm = pc_rtc_offset.day_alrm;
	mon_alrm = pc_rtc_offset.mon_alrm;
	if (pc_rtc_offset.century != 0) {
		century = pc_rtc_offset.century;
	}

	for (;;) {
		if (vrt_try-- < 0)
			return (ENXIO);
		outb(RTC_ADDR, RTC_D);		/* check if clock valid */
		reg = inb(RTC_DATA);
		if ((reg & RTC_VRT) != 0)
			break;
		drv_usecwait(5000);		/* Delay for 5000 us */
	}


checkuip:
	if (uip_try-- < 0)
		return (EAGAIN);
	outb(RTC_ADDR, RTC_A);		/* check if update in progress */
	reg = inb(RTC_DATA);
	if (reg & RTC_UIP) {
		tenmicrosec();
		goto checkuip;
	}

	for (i = 0, rawp = buf; i < RTC_NREG; i++) {
		outb(RTC_ADDR, i);
		*rawp++ = inb(RTC_DATA);
	}
	outb(RTC_ADDR, century); /* do century */
	((struct rtc_t *)buf)->rtc_century = inb(RTC_DATA);

	if (day_alrm > 0) {
		outb(RTC_ADDR, day_alrm);
		((struct rtc_t *)buf)->rtc_adom = inb(RTC_DATA) & 0x3f;
	}
	if (mon_alrm > 0) {
		outb(RTC_ADDR, mon_alrm);
		((struct rtc_t *)buf)->rtc_amon = inb(RTC_DATA);
	}

	outb(RTC_ADDR, 0);		/* re-read Seconds register */
	reg = inb(RTC_DATA);
	if (reg != ((struct rtc_t *)buf)->rtc_sec ||
	    (((struct rtc_t *)buf)->rtc_statusa & RTC_UIP))
		/* update occured during reads */
		goto checkuip;

	return (0);
}

/*
 * This routine writes the contents of the given buffer to the real time
 * clock.  It is given RTC_NREGP bytes of data, which are the 10 bytes used
 * to write the time and set the alarm.  It should be called with the priority
 * raised to 5.
 */
static void
todpc_rtcput(unsigned char *buf)
{
	unsigned char	reg;
	int		i;
	unsigned char	century = RTC_CENTURY;
	unsigned char	day_alrm = pc_rtc_offset.day_alrm;
	unsigned char	mon_alrm = pc_rtc_offset.mon_alrm;
	unsigned char	tmp;

	if (pc_rtc_offset.century != 0) {
		century = pc_rtc_offset.century;
	}

	outb(RTC_ADDR, RTC_B);
	reg = inb(RTC_DATA);
	outb(RTC_ADDR, RTC_B);
	outb(RTC_DATA, reg | RTC_SET);	/* allow time set now */
	for (i = 0; i < RTC_NREGP; i++) { /* set the time */
		outb(RTC_ADDR, i);
		outb(RTC_DATA, buf[i]);
	}
	outb(RTC_ADDR, century); /* do century */
	outb(RTC_DATA, ((struct rtc_t *)buf)->rtc_century);

	if (day_alrm > 0) {
		outb(RTC_ADDR, day_alrm);
		/* preserve RTC_VRT bit; some virt envs accept writes there */
		tmp = inb(RTC_DATA) & RTC_VRT;
		tmp |= ((struct rtc_t *)buf)->rtc_adom & ~RTC_VRT;
		outb(RTC_DATA, tmp);
	}
	if (mon_alrm > 0) {
		outb(RTC_ADDR, mon_alrm);
		outb(RTC_DATA, ((struct rtc_t *)buf)->rtc_amon);
	}

	outb(RTC_ADDR, RTC_B);
	reg = inb(RTC_DATA);
	outb(RTC_ADDR, RTC_B);
	outb(RTC_DATA, reg & ~RTC_SET);	/* allow time update */
}

static tod_ops_t todpc_ops = {
	TOD_OPS_VERSION,
	todpc_get,
	todpc_set,
	NULL,
	NULL,
	todpc_setalarm,
	todpc_clralarm,
	NULL
};

/*
 * Initialize for the default TOD ops vector for use on hardware.
 */

tod_ops_t *tod_ops = &todpc_ops;