/* $NetBSD: mcclock_pnpbus.c,v 1.11 2023/12/20 15:29:06 thorpej Exp $ */
/*-
* Copyright (c) 2006 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Tim Rightnour
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* PNPBus driver for the mc146818 compatible time-of-day clock found on all
* IBM Prep machines. This one is only discernable from the motorola clock
* part by the interface portion of the residual data. (or by the small vendor
* item chip id)
* NOTE: The IBM machines actually have either a dallas 1385 or a dallas 1585
* chip. This driver should probably be replaced by something that does that
* one day.
*/
/* If we are nearing 59 minutes on the hour, the math is too complex
* to compute out when to reboot, so we just wait for the clock to
* flip back over to zero, then set the alarm time.
*/
i = (*sc->sc_mcread)(sc, DS1687_MIN);
if (i == 0x58) {
(*sc->sc_mcwrite)(sc, DS1687_AMIN, 0x59);
(*sc->sc_mcwrite)(sc, DS1687_ASEC, 0x59);
i = 0x59;
} else {
while (i == 0x59) {
delay(100);
i = (*sc->sc_mcread)(sc, DS1687_MIN);
}
i += 2;
if ((i & 0x0f) > 8)
i = (i & 0xf0) + 0x10;
(*sc->sc_mcwrite)(sc, DS1687_AMIN, i);
}
i = (*sc->sc_mcread)(sc, DS1687_HOUR);
(*sc->sc_mcwrite)(sc, DS1687_AHOUR, i);
j = (*sc->sc_mcread)(sc, DS1687_DOM); /* get date */
i = (*sc->sc_mcread)(sc, DS1687_CONTROLA);
i |= DS1687_BANK1; /* set DV0 on */
(*sc->sc_mcwrite)(sc, DS1687_CONTROLA, i);
/* write today into wakeup */
(*sc->sc_mcwrite)(sc, DS1687_BANK1_ADATE, j);
i = (*sc->sc_mcread)(sc, 0x4b); /* bank1 reg B */
i |= 0x82; /* WIE + ABE */
(*sc->sc_mcwrite)(sc, 0x4b, i);
i = (*sc->sc_mcread)(sc, 0x4b);
/* XXX power control bit on 7024/7025 */
outb(PREP_BUS_SPACE_IO + 0x856, 1);
for (;;);
}