1/* $NetBSD: mcclock_pnpbus.c,v 1.9 2008/04/28 20:23:33 martin Exp $ */
2/*-
3 * Copyright (c) 2006 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Tim Rightnour
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/*
32 * PNPBus driver for the mc146818 compatible time-of-day clock found on all
33 * IBM Prep machines.  This one is only discernable from the motorola clock
34 * part by the interface portion of the residual data. (or by the small vendor
35 * item chip id)
36 * NOTE: The IBM machines actually have either a dallas 1385 or a dallas 1585
37 * chip.  This driver should probably be replaced by something that does that
38 * one day.
39 */
40
41#include <sys/cdefs.h>
42__KERNEL_RCSID(0, "$NetBSD: mcclock_pnpbus.c,v 1.9 2008/04/28 20:23:33 martin Exp $");
43
44#include <sys/types.h>
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/device.h>
48#include <sys/malloc.h>
49
50#include <sys/bus.h>
51#include <machine/intr.h>
52#include <machine/isa_machdep.h>
53#include <machine/residual.h>
54#include <machine/chpidpnp.h>
55/* XXX */
56#include <machine/pio.h>
57
58#include <dev/clock_subr.h>
59#include <dev/ic/mc146818reg.h>
60#include <dev/ic/mc146818var.h>
61#include <dev/ic/ds1687reg.h>
62
63#include <dev/isa/isavar.h>
64
65#include <prep/pnpbus/pnpbusvar.h>
66
67static int	mcclock_pnpbus_probe(device_t, cfdata_t, void *);
68static void	mcclock_pnpbus_attach(device_t, device_t, void *);
69
70extern struct cfdriver mcclock_cd;
71
72CFATTACH_DECL_NEW(mcclock_pnpbus, sizeof(struct mc146818_softc),
73    mcclock_pnpbus_probe, mcclock_pnpbus_attach, NULL, NULL);
74
75void	mcclock_pnpbus_write(struct mc146818_softc *, u_int, u_int);
76u_int	mcclock_pnpbus_read(struct mc146818_softc *, u_int);
77void	ds1585_reboot(void);
78
79int have_ds1585 = 0;
80#define MCCLOCK_STD_DEV		0
81
82static int
83mcclock_pnpbus_probe(device_t parent, cfdata_t cf, void *aux)
84{
85	struct pnpbus_dev_attach_args *pna = aux;
86	int ret = 0;
87
88	if (strcmp(pna->pna_devid, "PNP0B00") == 0 &&
89	    pna->subtype == RealTimeClock && pna->chipid == DallasRTC)
90		ret = 1;
91	if (strcmp(pna->pna_devid, "PNP0B00") == 0 &&
92	    pna->subtype == RealTimeClock && pna->chipid == Dallas1585)
93		ret = 1;
94
95	if (ret)
96		pnpbus_scan(pna, pna->pna_ppc_dev);
97	return ret;
98}
99
100static void
101mcclock_pnpbus_attach(device_t parent, device_t self, void *aux)
102{
103	struct mc146818_softc *sc = device_private(self);
104	struct pnpbus_dev_attach_args *pna = aux;
105
106	sc->sc_dev = self;
107	sc->sc_bst = pna->pna_iot;
108	if (pnpbus_io_map(&pna->pna_res, 0, &sc->sc_bst, &sc->sc_bsh)) {
109		/* XXX should we panic instead? */
110		aprint_error(": couldn't map clock I/O space\n");
111		return;
112	}
113
114	if (pna->chipid == Dallas1585)
115		have_ds1585 = 1;
116	sc->sc_year0 = 1900;
117	sc->sc_mcread = mcclock_pnpbus_read;
118	sc->sc_mcwrite = mcclock_pnpbus_write;
119	sc->sc_flag = MC146818_BCD;
120	mc146818_attach(sc);
121
122	aprint_normal("\n");
123
124	(*sc->sc_mcwrite)(sc, MC_REGB, MC_REGB_24HR);
125}
126
127void
128ds1585_reboot(void)
129{
130	struct mc146818_softc *sc;
131	int i, j;
132
133	if (!have_ds1585)
134		return;
135
136	sc = device_lookup_private(&mcclock_cd, MCCLOCK_STD_DEV);
137
138	/* monitors b0: 05,03,01  b1: 49, WIE=1 */
139
140	(*sc->sc_mcwrite)(sc, DS1687_ASEC, 0);
141
142	/* If we are nearing 59 minutes on the hour, the math is too complex
143	 * to compute out when to reboot, so we just wait for the clock to
144	 * flip back over to zero, then set the alarm time.
145	 */
146	i = (*sc->sc_mcread)(sc, DS1687_MIN);
147	if (i == 0x58) {
148		(*sc->sc_mcwrite)(sc, DS1687_AMIN, 0x59);
149		(*sc->sc_mcwrite)(sc, DS1687_ASEC, 0x59);
150		i = 0x59;
151	} else {
152		while (i == 0x59) {
153			delay(100);
154			i = (*sc->sc_mcread)(sc, DS1687_MIN);
155		}
156		i += 2;
157		if ((i & 0x0f) > 8)
158			i = (i & 0xf0) + 0x10;
159		(*sc->sc_mcwrite)(sc, DS1687_AMIN, i);
160	}
161	i = (*sc->sc_mcread)(sc, DS1687_HOUR);
162	(*sc->sc_mcwrite)(sc, DS1687_AHOUR, i);
163	j = (*sc->sc_mcread)(sc, DS1687_DOM); /* get date */
164	i = (*sc->sc_mcread)(sc, DS1687_CONTROLA);
165	i |= DS1687_BANK1; /* set DV0 on */
166	(*sc->sc_mcwrite)(sc, DS1687_CONTROLA, i);
167	/* write today into wakeup */
168	(*sc->sc_mcwrite)(sc, DS1687_BANK1_ADATE, j);
169	i = (*sc->sc_mcread)(sc, 0x4b); /* bank1 reg B */
170	i |= 0x82; /* WIE + ABE */
171	(*sc->sc_mcwrite)(sc, 0x4b, i);
172	i = (*sc->sc_mcread)(sc, 0x4b);
173	/* XXX power control bit on 7024/7025 */
174	outb(PREP_BUS_SPACE_IO + 0x856, 1);
175	for (;;);
176}
177
178void
179mcclock_pnpbus_write(struct mc146818_softc *sc, u_int reg, u_int datum)
180{
181	bus_space_tag_t iot;
182	bus_space_handle_t ioh;
183
184	iot = sc->sc_bst;
185	ioh = sc->sc_bsh;
186
187	bus_space_write_1(iot, ioh, 0, reg);
188	bus_space_write_1(iot, ioh, 1, datum);
189}
190
191u_int
192mcclock_pnpbus_read(struct mc146818_softc *sc, u_int reg)
193{
194	bus_space_tag_t iot;
195	bus_space_handle_t ioh;
196	u_int datum;
197
198	iot = sc->sc_bst;
199	ioh = sc->sc_bsh;
200
201	bus_space_write_1(iot, ioh, 0, reg);
202	datum = bus_space_read_1(iot, ioh, 1);
203
204	return (datum);
205}
206