summaryrefslogtreecommitdiff
path: root/usr/src/uts/sun4u/io/todbq4802.c
blob: ed22ae7cdb84d7de375e2efdbbc2f23fcc31e5ad (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
/*
 * 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 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * tod driver module for TI BQ4802 part
 *
 * Note: The way to access the bq4802's RTC registers is different than
 * the previous RTC devices (m5823, m5819p, ds1287, etc) that we used.
 * The address returns from OBP is mapped directly to the bq4802's RTC
 * registers. To read/write the data from/to the bq4802 registers, one
 * just add the register offset to the base address.
 * To access the previous RTC devices, we write the register index to
 * the address port (v_rtc_addr_reg) then read/write the data from/to
 * the data port (v_rtc_data_reg).
 */

#include <sys/types.h>
#include <sys/conf.h>
#include <sys/kmem.h>
#include <sys/open.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/sysmacros.h>

#include <sys/todbq4802.h>
#include <sys/modctl.h>
#include <sys/stat.h>
#include <sys/clock.h>
#include <sys/reboot.h>
#include <sys/machsystm.h>

/*
 * tod_ops entry routines
 */
static timestruc_t	todbq4802_get(void);
static void		todbq4802_set(timestruc_t);
static uint_t		todbq4802_set_watchdog_timer(uint_t);
static uint_t		todbq4802_clear_watchdog_timer(void);
static void		todbq4802_set_power_alarm(timestruc_t);
static void		todbq4802_clear_power_alarm(void);
static uint64_t		todbq4802_get_cpufrequency(void);

extern uint64_t		find_cpufrequency(volatile uint8_t *);

/*
 * External variables
 */
extern int watchdog_enable;
extern int watchdog_available;
extern int boothowto;

/*
 * Global variables
 */
int bq4802_debug_flags;
uint_t bq4802_hrestime_count = 0;
uint_t bq4802_uip_count = 0;

/*
 * Module linkage information for the kernel.
 */
static struct modlmisc modlmisc = {
	&mod_miscops, "tod module for TI BQ4802"
};

static struct modlinkage modlinkage = {
	MODREV_1, (void *)&modlmisc, NULL
};

static void read_rtc(struct rtc_t *);
static void write_rtc_time(struct rtc_t *);
static void write_rtc_alarm(struct rtc_t *);

int
_init(void)
{
	if (strcmp(tod_module_name, "todbq4802") == 0) {
		if (v_rtc_addr_reg == NULL)
			cmn_err(CE_PANIC, "addr not set, cannot read RTC\n");

		BQ4802_DATA_REG(RTC_CNTRL) = (RTC_HM | RTC_STOP_N);

		/* Clear AF flag by reading reg Flags (D) */
		(void) BQ4802_DATA_REG(RTC_FLAGS);

		tod_ops.tod_get = todbq4802_get;
		tod_ops.tod_set = todbq4802_set;
		tod_ops.tod_set_watchdog_timer =
		    todbq4802_set_watchdog_timer;
		tod_ops.tod_clear_watchdog_timer =
		    todbq4802_clear_watchdog_timer;
		tod_ops.tod_set_power_alarm = todbq4802_set_power_alarm;
		tod_ops.tod_clear_power_alarm = todbq4802_clear_power_alarm;
		tod_ops.tod_get_cpufrequency = todbq4802_get_cpufrequency;

		/*
		 * check if hardware watchdog timer is available and user
		 * enabled it.
		 */
		if (watchdog_enable) {
			if (!watchdog_available) {
				cmn_err(CE_WARN, "bq4802: Hardware watchdog "
				    "unavailable");
			} else if (boothowto & RB_DEBUG) {
				cmn_err(CE_WARN, "bq4802: Hardware watchdog"
				    " disabled [debugger]");
			}
		}
	}

	return (mod_install(&modlinkage));
}

int
_fini(void)
{
	if (strcmp(tod_module_name, "todbq4802") == 0)
		return (EBUSY);

	return (mod_remove(&modlinkage));
}

/*
 * The loadable-module _info(9E) entry point
 */
int
_info(struct modinfo *modinfop)
{
	return (mod_info(&modlinkage, modinfop));
}

/*
 * 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.
 */
static timestruc_t
todbq4802_get(void)
{
	timestruc_t ts;
	todinfo_t tod;
	struct rtc_t rtc;

	ASSERT(MUTEX_HELD(&tod_lock));

	read_rtc(&rtc);
	DPRINTF("todbq4802_get: century=%d year=%d dom=%d hrs=%d min=%d"
	    " sec=%d\n", rtc.rtc_century, rtc.rtc_year, rtc.rtc_dom,
	    rtc.rtc_hrs, rtc.rtc_min, rtc.rtc_sec);

	/*
	 * tod_year is base 1900 so this code needs to adjust the true
	 * year retrieved from the rtc's century and year fields.
	 */
	tod.tod_year	= rtc.rtc_year + (rtc.rtc_century * 100) - 1900;
	tod.tod_month	= rtc.rtc_mon;
	tod.tod_day	= rtc.rtc_dom;
	tod.tod_dow	= rtc.rtc_dow;
	tod.tod_hour	= rtc.rtc_hrs;
	tod.tod_min	= rtc.rtc_min;
	tod.tod_sec	= rtc.rtc_sec;

	ts.tv_sec = tod_to_utc(tod);
	ts.tv_nsec = 0;
	return (ts);
}

/*
 * Once every second, the user-accessible clock/calendar
 * locations are updated simultaneously from the internal
 * real-time counters. To prevent reading data in transition,
 * updates to the bq4802 clock registers should be halted.
 * Updating is halted by setting the Update Transfer Inhibit
 * (UTI) bit D3 of the control register E. As long as the
 * UTI bit is 1, updates to user-accessible clock locations are
 * inhibited. Once the frozen clock information is retrieved by
 * reading the appropriate clock memory locations, the UTI
 * bit should be reset to 0 in order to allow updates to occur
 * from the internal counters. Because the internal counters
 * are not halted by setting the UTI bit, reading the clock
 * locations has no effect on clock accuracy. Once the UTI bit
 * is reset to 0, the internal registers update within one
 * second the user-accessible registers with the correct time.
 * A halt command issued during a clock update allows the
 * update to occur before freezing the data.
 */
static void
read_rtc(struct rtc_t *rtc)
{
	uint8_t	reg_cntrl;

	/*
	 * Freeze
	 */
	reg_cntrl = BQ4802_DATA_REG(RTC_CNTRL);
	BQ4802_DATA_REG(RTC_CNTRL) = (reg_cntrl | RTC_UTI);

	rtc->rtc_sec = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_SEC));
	rtc->rtc_asec = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_ASEC));
	rtc->rtc_min = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_MIN));
	rtc->rtc_amin = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_AMIN));
	rtc->rtc_hrs = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_HRS));
	rtc->rtc_ahrs = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_AHRS));
	rtc->rtc_dom = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_DOM));
	rtc->rtc_adom = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_ADOM));
	rtc->rtc_dow = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_DOW));
	rtc->rtc_mon = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_MON));
	rtc->rtc_year = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_YEAR));
	rtc->rtc_century = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_CENTURY));

	/*
	 * Unfreeze
	 */
	BQ4802_DATA_REG(RTC_CNTRL) = reg_cntrl;
}

/*
 * Write the specified time into the clock chip.
 * Must be called with tod_lock held.
 */
static void
todbq4802_set(timestruc_t ts)
{
	struct rtc_t	rtc;
	todinfo_t tod = utc_to_tod(ts.tv_sec);
	int year;

	ASSERT(MUTEX_HELD(&tod_lock));

	/* tod_year is base 1900 so this code needs to adjust */
	year = 1900 + tod.tod_year;
	rtc.rtc_year	= year % 100;
	rtc.rtc_century = year / 100;
	rtc.rtc_mon	= (uint8_t)tod.tod_month;
	rtc.rtc_dom	= (uint8_t)tod.tod_day;
	rtc.rtc_dow	= (uint8_t)tod.tod_dow;
	rtc.rtc_hrs	= (uint8_t)tod.tod_hour;
	rtc.rtc_min	= (uint8_t)tod.tod_min;
	rtc.rtc_sec	= (uint8_t)tod.tod_sec;
	DPRINTF("todbq4802_set: year=%d dom=%d hrs=%d min=%d sec=%d\n",
	    rtc.rtc_year, rtc.rtc_dom, rtc.rtc_hrs, rtc.rtc_min, rtc.rtc_sec);

	write_rtc_time(&rtc);
}

/*
 * The UTI bit must be used to set the bq4802 clock.
 * Once set, the locations can be written with the desired
 * information in BCD format. Resetting the UTI bit to 0 causes
 * the written values to be transferred to the internal clock
 * counters and allows updates to the user-accessible registers
 * to resume within one second.
 */
void
write_rtc_time(struct rtc_t *rtc)
{
	uint8_t	reg_cntrl;

	/*
	 * Freeze
	 */
	reg_cntrl = BQ4802_DATA_REG(RTC_CNTRL);
	BQ4802_DATA_REG(RTC_CNTRL) = (reg_cntrl | RTC_UTI);

	BQ4802_DATA_REG(RTC_SEC) = BYTE_TO_BCD(rtc->rtc_sec);
	BQ4802_DATA_REG(RTC_MIN) = BYTE_TO_BCD(rtc->rtc_min);
	BQ4802_DATA_REG(RTC_HRS) = BYTE_TO_BCD(rtc->rtc_hrs);
	BQ4802_DATA_REG(RTC_DOM) = BYTE_TO_BCD(rtc->rtc_dom);
	BQ4802_DATA_REG(RTC_DOW) = BYTE_TO_BCD(rtc->rtc_dow);
	BQ4802_DATA_REG(RTC_MON) = BYTE_TO_BCD(rtc->rtc_mon);
	BQ4802_DATA_REG(RTC_YEAR) = BYTE_TO_BCD(rtc->rtc_year);
	BQ4802_DATA_REG(RTC_CENTURY) = BYTE_TO_BCD(rtc->rtc_century);

	/*
	 * Unfreeze
	 */
	BQ4802_DATA_REG(RTC_CNTRL) = reg_cntrl;
}

void
write_rtc_alarm(struct rtc_t *rtc)
{
	BQ4802_DATA_REG(RTC_ASEC) = BYTE_TO_BCD(rtc->rtc_asec);
	BQ4802_DATA_REG(RTC_AMIN) = BYTE_TO_BCD(rtc->rtc_amin);
	BQ4802_DATA_REG(RTC_AHRS) = BYTE_TO_BCD(rtc->rtc_ahrs);
	BQ4802_DATA_REG(RTC_ADOM) = BYTE_TO_BCD(rtc->rtc_adom);
}

/*
 * program the rtc registers for alarm to go off at the specified time
 */
static void
todbq4802_set_power_alarm(timestruc_t ts)
{
	todinfo_t	tod;
	uint8_t		regc;
	struct rtc_t	rtc;

	ASSERT(MUTEX_HELD(&tod_lock));
	tod = utc_to_tod(ts.tv_sec);

	/*
	 * disable alarms and clear AF flag by reading reg Flags (D)
	 */
	regc = BQ4802_DATA_REG(RTC_ENABLES);
	BQ4802_DATA_REG(RTC_ENABLES) = regc & ~(RTC_AIE | RTC_ABE);
	(void) BQ4802_DATA_REG(RTC_FLAGS);

	rtc.rtc_asec = (uint8_t)tod.tod_sec;
	rtc.rtc_amin = (uint8_t)tod.tod_min;
	rtc.rtc_ahrs = (uint8_t)tod.tod_hour;
	rtc.rtc_adom = (uint8_t)tod.tod_day;
	DPRINTF("todbq4802_set_alarm: dom=%d hrs=%d min=%d sec=%d\n",
	    rtc.rtc_adom, rtc.rtc_ahrs, rtc.rtc_amin, rtc.rtc_asec);

	/*
	 * Write alarm values and enable alarm
	 */
	write_rtc_alarm(&rtc);

	BQ4802_DATA_REG(RTC_ENABLES) = regc | RTC_AIE | RTC_ABE;
}

/*
 * clear alarm interrupt
 */
static void
todbq4802_clear_power_alarm(void)
{
	uint8_t regc;

	ASSERT(MUTEX_HELD(&tod_lock));

	regc = BQ4802_DATA_REG(RTC_ENABLES);
	BQ4802_DATA_REG(RTC_ENABLES) = regc & ~(RTC_AIE | RTC_ABE);
}

/*
 * Determine the cpu frequency by watching the TOD chip rollover twice.
 * Cpu clock rate is determined by computing the ticks added (in tick register)
 * during one second interval on TOD.
 */
uint64_t
todbq4802_get_cpufrequency(void)
{
	ASSERT(MUTEX_HELD(&tod_lock));
	return (find_cpufrequency((volatile uint8_t *)v_rtc_addr_reg));
}

/*ARGSUSED*/
static uint_t
todbq4802_set_watchdog_timer(uint_t timeoutval)
{
	ASSERT(MUTEX_HELD(&tod_lock));
	return (0);
}

static uint_t
todbq4802_clear_watchdog_timer(void)
{
	ASSERT(MUTEX_HELD(&tod_lock));
	return (0);
}