184612482Sminht /*
284612482Sminht * CDDL HEADER START
384612482Sminht *
484612482Sminht * The contents of this file are subject to the terms of the
5*4295e777Srameshc * Common Development and Distribution License (the "License").
6*4295e777Srameshc * You may not use this file except in compliance with the License.
784612482Sminht *
884612482Sminht * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
984612482Sminht * or http://www.opensolaris.org/os/licensing.
1084612482Sminht * See the License for the specific language governing permissions
1184612482Sminht * and limitations under the License.
1284612482Sminht *
1384612482Sminht * When distributing Covered Code, include this CDDL HEADER in each
1484612482Sminht * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1584612482Sminht * If applicable, add the following below this CDDL HEADER, with the
1684612482Sminht * fields enclosed by brackets "[]" replaced with your own identifying
1784612482Sminht * information: Portions Copyright [yyyy] [name of copyright owner]
1884612482Sminht *
1984612482Sminht * CDDL HEADER END
2084612482Sminht */
2184612482Sminht /*
22*4295e777Srameshc * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2384612482Sminht * Use is subject to license terms.
2484612482Sminht */
2584612482Sminht
2684612482Sminht #pragma ident "%Z%%M% %I% %E% SMI"
2784612482Sminht
2884612482Sminht /*
2984612482Sminht * tod driver module for TI BQ4802 part
3084612482Sminht *
3184612482Sminht * Note: The way to access the bq4802's RTC registers is different than
3284612482Sminht * the previous RTC devices (m5823, m5819p, ds1287, etc) that we used.
3384612482Sminht * The address returns from OBP is mapped directly to the bq4802's RTC
3484612482Sminht * registers. To read/write the data from/to the bq4802 registers, one
3584612482Sminht * just add the register offset to the base address.
3684612482Sminht * To access the previous RTC devices, we write the register index to
3784612482Sminht * the address port (v_rtc_addr_reg) then read/write the data from/to
3884612482Sminht * the data port (v_rtc_data_reg).
3984612482Sminht */
4084612482Sminht
4184612482Sminht #include <sys/types.h>
4284612482Sminht #include <sys/conf.h>
4384612482Sminht #include <sys/kmem.h>
4484612482Sminht #include <sys/open.h>
4584612482Sminht #include <sys/ddi.h>
4684612482Sminht #include <sys/sunddi.h>
4784612482Sminht #include <sys/sysmacros.h>
4884612482Sminht
4984612482Sminht #include <sys/todbq4802.h>
5084612482Sminht #include <sys/modctl.h>
5184612482Sminht #include <sys/stat.h>
5284612482Sminht #include <sys/clock.h>
5384612482Sminht #include <sys/reboot.h>
5484612482Sminht #include <sys/machsystm.h>
5584612482Sminht
5684612482Sminht /*
5784612482Sminht * tod_ops entry routines
5884612482Sminht */
5984612482Sminht static timestruc_t todbq4802_get(void);
6084612482Sminht static void todbq4802_set(timestruc_t);
6184612482Sminht static uint_t todbq4802_set_watchdog_timer(uint_t);
6284612482Sminht static uint_t todbq4802_clear_watchdog_timer(void);
6384612482Sminht static void todbq4802_set_power_alarm(timestruc_t);
6484612482Sminht static void todbq4802_clear_power_alarm(void);
6584612482Sminht static uint64_t todbq4802_get_cpufrequency(void);
6684612482Sminht
6784612482Sminht extern uint64_t find_cpufrequency(volatile uint8_t *);
6884612482Sminht
6984612482Sminht /*
7084612482Sminht * External variables
7184612482Sminht */
7284612482Sminht extern int watchdog_enable;
7384612482Sminht extern int watchdog_available;
7484612482Sminht extern int boothowto;
7584612482Sminht
7684612482Sminht /*
7784612482Sminht * Global variables
7884612482Sminht */
7984612482Sminht int bq4802_debug_flags;
8084612482Sminht uint_t bq4802_hrestime_count = 0;
8184612482Sminht uint_t bq4802_uip_count = 0;
8284612482Sminht
8384612482Sminht /*
8484612482Sminht * Module linkage information for the kernel.
8584612482Sminht */
8684612482Sminht static struct modlmisc modlmisc = {
8784612482Sminht &mod_miscops, "tod module for TI BQ4802"
8884612482Sminht };
8984612482Sminht
9084612482Sminht static struct modlinkage modlinkage = {
9184612482Sminht MODREV_1, (void *)&modlmisc, NULL
9284612482Sminht };
9384612482Sminht
9484612482Sminht static void read_rtc(struct rtc_t *);
9584612482Sminht static void write_rtc_time(struct rtc_t *);
9684612482Sminht static void write_rtc_alarm(struct rtc_t *);
9784612482Sminht
9884612482Sminht int
_init(void)9984612482Sminht _init(void)
10084612482Sminht {
10184612482Sminht if (strcmp(tod_module_name, "todbq4802") == 0) {
10284612482Sminht if (v_rtc_addr_reg == NULL)
10384612482Sminht cmn_err(CE_PANIC, "addr not set, cannot read RTC\n");
10484612482Sminht
105*4295e777Srameshc BQ4802_DATA_REG(RTC_CNTRL) = (RTC_HM | RTC_STOP_N);
10684612482Sminht
10784612482Sminht /* Clear AF flag by reading reg Flags (D) */
10884612482Sminht (void) BQ4802_DATA_REG(RTC_FLAGS);
10984612482Sminht
11084612482Sminht tod_ops.tod_get = todbq4802_get;
11184612482Sminht tod_ops.tod_set = todbq4802_set;
11284612482Sminht tod_ops.tod_set_watchdog_timer =
11384612482Sminht todbq4802_set_watchdog_timer;
11484612482Sminht tod_ops.tod_clear_watchdog_timer =
11584612482Sminht todbq4802_clear_watchdog_timer;
11684612482Sminht tod_ops.tod_set_power_alarm = todbq4802_set_power_alarm;
11784612482Sminht tod_ops.tod_clear_power_alarm = todbq4802_clear_power_alarm;
11884612482Sminht tod_ops.tod_get_cpufrequency = todbq4802_get_cpufrequency;
11984612482Sminht
12084612482Sminht /*
12184612482Sminht * check if hardware watchdog timer is available and user
12284612482Sminht * enabled it.
12384612482Sminht */
12484612482Sminht if (watchdog_enable) {
12584612482Sminht if (!watchdog_available) {
12684612482Sminht cmn_err(CE_WARN, "bq4802: Hardware watchdog "
12784612482Sminht "unavailable");
12884612482Sminht } else if (boothowto & RB_DEBUG) {
12984612482Sminht cmn_err(CE_WARN, "bq4802: Hardware watchdog"
13084612482Sminht " disabled [debugger]");
13184612482Sminht }
13284612482Sminht }
13384612482Sminht }
13484612482Sminht
13584612482Sminht return (mod_install(&modlinkage));
13684612482Sminht }
13784612482Sminht
13884612482Sminht int
_fini(void)13984612482Sminht _fini(void)
14084612482Sminht {
14184612482Sminht if (strcmp(tod_module_name, "todbq4802") == 0)
14284612482Sminht return (EBUSY);
14384612482Sminht
14484612482Sminht return (mod_remove(&modlinkage));
14584612482Sminht }
14684612482Sminht
14784612482Sminht /*
14884612482Sminht * The loadable-module _info(9E) entry point
14984612482Sminht */
15084612482Sminht int
_info(struct modinfo * modinfop)15184612482Sminht _info(struct modinfo *modinfop)
15284612482Sminht {
15384612482Sminht return (mod_info(&modlinkage, modinfop));
15484612482Sminht }
15584612482Sminht
15684612482Sminht /*
15784612482Sminht * Read the current time from the clock chip and convert to UNIX form.
15884612482Sminht * Assumes that the year in the clock chip is valid.
15984612482Sminht * Must be called with tod_lock held.
16084612482Sminht */
16184612482Sminht static timestruc_t
todbq4802_get(void)16284612482Sminht todbq4802_get(void)
16384612482Sminht {
16484612482Sminht timestruc_t ts;
16584612482Sminht todinfo_t tod;
16684612482Sminht struct rtc_t rtc;
16784612482Sminht
16884612482Sminht ASSERT(MUTEX_HELD(&tod_lock));
16984612482Sminht
17084612482Sminht read_rtc(&rtc);
17184612482Sminht DPRINTF("todbq4802_get: century=%d year=%d dom=%d hrs=%d min=%d"
17284612482Sminht " sec=%d\n", rtc.rtc_century, rtc.rtc_year, rtc.rtc_dom,
17384612482Sminht rtc.rtc_hrs, rtc.rtc_min, rtc.rtc_sec);
17484612482Sminht
17584612482Sminht /*
17684612482Sminht * tod_year is base 1900 so this code needs to adjust the true
17784612482Sminht * year retrieved from the rtc's century and year fields.
17884612482Sminht */
17984612482Sminht tod.tod_year = rtc.rtc_year + (rtc.rtc_century * 100) - 1900;
18084612482Sminht tod.tod_month = rtc.rtc_mon;
18184612482Sminht tod.tod_day = rtc.rtc_dom;
18284612482Sminht tod.tod_dow = rtc.rtc_dow;
18384612482Sminht tod.tod_hour = rtc.rtc_hrs;
18484612482Sminht tod.tod_min = rtc.rtc_min;
18584612482Sminht tod.tod_sec = rtc.rtc_sec;
18684612482Sminht
18784612482Sminht ts.tv_sec = tod_to_utc(tod);
18884612482Sminht ts.tv_nsec = 0;
18984612482Sminht return (ts);
19084612482Sminht }
19184612482Sminht
19284612482Sminht /*
19384612482Sminht * Once every second, the user-accessible clock/calendar
19484612482Sminht * locations are updated simultaneously from the internal
19584612482Sminht * real-time counters. To prevent reading data in transition,
19684612482Sminht * updates to the bq4802 clock registers should be halted.
19784612482Sminht * Updating is halted by setting the Update Transfer Inhibit
19884612482Sminht * (UTI) bit D3 of the control register E. As long as the
19984612482Sminht * UTI bit is 1, updates to user-accessible clock locations are
20084612482Sminht * inhibited. Once the frozen clock information is retrieved by
20184612482Sminht * reading the appropriate clock memory locations, the UTI
20284612482Sminht * bit should be reset to 0 in order to allow updates to occur
20384612482Sminht * from the internal counters. Because the internal counters
20484612482Sminht * are not halted by setting the UTI bit, reading the clock
20584612482Sminht * locations has no effect on clock accuracy. Once the UTI bit
20684612482Sminht * is reset to 0, the internal registers update within one
20784612482Sminht * second the user-accessible registers with the correct time.
20884612482Sminht * A halt command issued during a clock update allows the
20984612482Sminht * update to occur before freezing the data.
21084612482Sminht */
21184612482Sminht static void
read_rtc(struct rtc_t * rtc)21284612482Sminht read_rtc(struct rtc_t *rtc)
21384612482Sminht {
21484612482Sminht uint8_t reg_cntrl;
21584612482Sminht
21684612482Sminht /*
21784612482Sminht * Freeze
21884612482Sminht */
21984612482Sminht reg_cntrl = BQ4802_DATA_REG(RTC_CNTRL);
22084612482Sminht BQ4802_DATA_REG(RTC_CNTRL) = (reg_cntrl | RTC_UTI);
22184612482Sminht
22284612482Sminht rtc->rtc_sec = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_SEC));
22384612482Sminht rtc->rtc_asec = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_ASEC));
22484612482Sminht rtc->rtc_min = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_MIN));
22584612482Sminht rtc->rtc_amin = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_AMIN));
22684612482Sminht rtc->rtc_hrs = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_HRS));
22784612482Sminht rtc->rtc_ahrs = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_AHRS));
22884612482Sminht rtc->rtc_dom = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_DOM));
22984612482Sminht rtc->rtc_adom = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_ADOM));
23084612482Sminht rtc->rtc_dow = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_DOW));
23184612482Sminht rtc->rtc_mon = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_MON));
23284612482Sminht rtc->rtc_year = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_YEAR));
23384612482Sminht rtc->rtc_century = BCD_TO_BYTE(BQ4802_DATA_REG(RTC_CENTURY));
23484612482Sminht
23584612482Sminht /*
23684612482Sminht * Unfreeze
23784612482Sminht */
23884612482Sminht BQ4802_DATA_REG(RTC_CNTRL) = reg_cntrl;
23984612482Sminht }
24084612482Sminht
24184612482Sminht /*
24284612482Sminht * Write the specified time into the clock chip.
24384612482Sminht * Must be called with tod_lock held.
24484612482Sminht */
24584612482Sminht static void
todbq4802_set(timestruc_t ts)24684612482Sminht todbq4802_set(timestruc_t ts)
24784612482Sminht {
24884612482Sminht struct rtc_t rtc;
24984612482Sminht todinfo_t tod = utc_to_tod(ts.tv_sec);
25084612482Sminht int year;
25184612482Sminht
25284612482Sminht ASSERT(MUTEX_HELD(&tod_lock));
25384612482Sminht
25484612482Sminht /* tod_year is base 1900 so this code needs to adjust */
25584612482Sminht year = 1900 + tod.tod_year;
25684612482Sminht rtc.rtc_year = year % 100;
25784612482Sminht rtc.rtc_century = year / 100;
25884612482Sminht rtc.rtc_mon = (uint8_t)tod.tod_month;
25984612482Sminht rtc.rtc_dom = (uint8_t)tod.tod_day;
26084612482Sminht rtc.rtc_dow = (uint8_t)tod.tod_dow;
26184612482Sminht rtc.rtc_hrs = (uint8_t)tod.tod_hour;
26284612482Sminht rtc.rtc_min = (uint8_t)tod.tod_min;
26384612482Sminht rtc.rtc_sec = (uint8_t)tod.tod_sec;
26484612482Sminht DPRINTF("todbq4802_set: year=%d dom=%d hrs=%d min=%d sec=%d\n",
26584612482Sminht rtc.rtc_year, rtc.rtc_dom, rtc.rtc_hrs, rtc.rtc_min, rtc.rtc_sec);
26684612482Sminht
26784612482Sminht write_rtc_time(&rtc);
26884612482Sminht }
26984612482Sminht
27084612482Sminht /*
27184612482Sminht * The UTI bit must be used to set the bq4802 clock.
27284612482Sminht * Once set, the locations can be written with the desired
27384612482Sminht * information in BCD format. Resetting the UTI bit to 0 causes
27484612482Sminht * the written values to be transferred to the internal clock
27584612482Sminht * counters and allows updates to the user-accessible registers
27684612482Sminht * to resume within one second.
27784612482Sminht */
27884612482Sminht void
write_rtc_time(struct rtc_t * rtc)27984612482Sminht write_rtc_time(struct rtc_t *rtc)
28084612482Sminht {
28184612482Sminht uint8_t reg_cntrl;
28284612482Sminht
28384612482Sminht /*
28484612482Sminht * Freeze
28584612482Sminht */
28684612482Sminht reg_cntrl = BQ4802_DATA_REG(RTC_CNTRL);
28784612482Sminht BQ4802_DATA_REG(RTC_CNTRL) = (reg_cntrl | RTC_UTI);
28884612482Sminht
28984612482Sminht BQ4802_DATA_REG(RTC_SEC) = BYTE_TO_BCD(rtc->rtc_sec);
29084612482Sminht BQ4802_DATA_REG(RTC_MIN) = BYTE_TO_BCD(rtc->rtc_min);
29184612482Sminht BQ4802_DATA_REG(RTC_HRS) = BYTE_TO_BCD(rtc->rtc_hrs);
29284612482Sminht BQ4802_DATA_REG(RTC_DOM) = BYTE_TO_BCD(rtc->rtc_dom);
29384612482Sminht BQ4802_DATA_REG(RTC_DOW) = BYTE_TO_BCD(rtc->rtc_dow);
29484612482Sminht BQ4802_DATA_REG(RTC_MON) = BYTE_TO_BCD(rtc->rtc_mon);
29584612482Sminht BQ4802_DATA_REG(RTC_YEAR) = BYTE_TO_BCD(rtc->rtc_year);
29684612482Sminht BQ4802_DATA_REG(RTC_CENTURY) = BYTE_TO_BCD(rtc->rtc_century);
29784612482Sminht
29884612482Sminht /*
29984612482Sminht * Unfreeze
30084612482Sminht */
30184612482Sminht BQ4802_DATA_REG(RTC_CNTRL) = reg_cntrl;
30284612482Sminht }
30384612482Sminht
30484612482Sminht void
write_rtc_alarm(struct rtc_t * rtc)30584612482Sminht write_rtc_alarm(struct rtc_t *rtc)
30684612482Sminht {
30784612482Sminht BQ4802_DATA_REG(RTC_ASEC) = BYTE_TO_BCD(rtc->rtc_asec);
30884612482Sminht BQ4802_DATA_REG(RTC_AMIN) = BYTE_TO_BCD(rtc->rtc_amin);
30984612482Sminht BQ4802_DATA_REG(RTC_AHRS) = BYTE_TO_BCD(rtc->rtc_ahrs);
31084612482Sminht BQ4802_DATA_REG(RTC_ADOM) = BYTE_TO_BCD(rtc->rtc_adom);
31184612482Sminht }
31284612482Sminht
31384612482Sminht /*
31484612482Sminht * program the rtc registers for alarm to go off at the specified time
31584612482Sminht */
31684612482Sminht static void
todbq4802_set_power_alarm(timestruc_t ts)31784612482Sminht todbq4802_set_power_alarm(timestruc_t ts)
31884612482Sminht {
31984612482Sminht todinfo_t tod;
32084612482Sminht uint8_t regc;
32184612482Sminht struct rtc_t rtc;
32284612482Sminht
32384612482Sminht ASSERT(MUTEX_HELD(&tod_lock));
32484612482Sminht tod = utc_to_tod(ts.tv_sec);
32584612482Sminht
32684612482Sminht /*
32784612482Sminht * disable alarms and clear AF flag by reading reg Flags (D)
32884612482Sminht */
32984612482Sminht regc = BQ4802_DATA_REG(RTC_ENABLES);
33084612482Sminht BQ4802_DATA_REG(RTC_ENABLES) = regc & ~(RTC_AIE | RTC_ABE);
33184612482Sminht (void) BQ4802_DATA_REG(RTC_FLAGS);
33284612482Sminht
33384612482Sminht rtc.rtc_asec = (uint8_t)tod.tod_sec;
33484612482Sminht rtc.rtc_amin = (uint8_t)tod.tod_min;
33584612482Sminht rtc.rtc_ahrs = (uint8_t)tod.tod_hour;
33684612482Sminht rtc.rtc_adom = (uint8_t)tod.tod_day;
33784612482Sminht DPRINTF("todbq4802_set_alarm: dom=%d hrs=%d min=%d sec=%d\n",
33884612482Sminht rtc.rtc_adom, rtc.rtc_ahrs, rtc.rtc_amin, rtc.rtc_asec);
33984612482Sminht
34084612482Sminht /*
34184612482Sminht * Write alarm values and enable alarm
34284612482Sminht */
34384612482Sminht write_rtc_alarm(&rtc);
34484612482Sminht
34584612482Sminht BQ4802_DATA_REG(RTC_ENABLES) = regc | RTC_AIE | RTC_ABE;
34684612482Sminht }
34784612482Sminht
34884612482Sminht /*
34984612482Sminht * clear alarm interrupt
35084612482Sminht */
35184612482Sminht static void
todbq4802_clear_power_alarm(void)35284612482Sminht todbq4802_clear_power_alarm(void)
35384612482Sminht {
35484612482Sminht uint8_t regc;
35584612482Sminht
35684612482Sminht ASSERT(MUTEX_HELD(&tod_lock));
35784612482Sminht
35884612482Sminht regc = BQ4802_DATA_REG(RTC_ENABLES);
35984612482Sminht BQ4802_DATA_REG(RTC_ENABLES) = regc & ~(RTC_AIE | RTC_ABE);
36084612482Sminht }
36184612482Sminht
36284612482Sminht /*
36384612482Sminht * Determine the cpu frequency by watching the TOD chip rollover twice.
36484612482Sminht * Cpu clock rate is determined by computing the ticks added (in tick register)
36584612482Sminht * during one second interval on TOD.
36684612482Sminht */
36784612482Sminht uint64_t
todbq4802_get_cpufrequency(void)36884612482Sminht todbq4802_get_cpufrequency(void)
36984612482Sminht {
37084612482Sminht ASSERT(MUTEX_HELD(&tod_lock));
37184612482Sminht return (find_cpufrequency((volatile uint8_t *)v_rtc_addr_reg));
37284612482Sminht }
37384612482Sminht
37484612482Sminht /*ARGSUSED*/
37584612482Sminht static uint_t
todbq4802_set_watchdog_timer(uint_t timeoutval)37684612482Sminht todbq4802_set_watchdog_timer(uint_t timeoutval)
37784612482Sminht {
37884612482Sminht ASSERT(MUTEX_HELD(&tod_lock));
37984612482Sminht return (0);
38084612482Sminht }
38184612482Sminht
38284612482Sminht static uint_t
todbq4802_clear_watchdog_timer(void)38384612482Sminht todbq4802_clear_watchdog_timer(void)
38484612482Sminht {
38584612482Sminht ASSERT(MUTEX_HELD(&tod_lock));
38684612482Sminht return (0);
38784612482Sminht }
388