1/* $NetBSD: tsrtc.c,v 1.6 2008/04/04 09:33:07 tsutsui Exp $ */ 2 3/* 4 * Copyright (c) 1995, 1996 Carnegie-Mellon University. 5 * All rights reserved. 6 * 7 * Author: Chris G. Demetriou 8 * 9 * Permission to use, copy, modify and distribute this software and 10 * its documentation is hereby granted, provided that both the copyright 11 * notice and this permission notice appear in all copies of the 12 * software, derivative works or modified versions, and any portions 13 * thereof, and that both notices appear in supporting documentation. 14 * 15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 18 * 19 * Carnegie Mellon requests users of this software to return to 20 * 21 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 22 * School of Computer Science 23 * Carnegie Mellon University 24 * Pittsburgh PA 15213-3890 25 * 26 * any improvements or extensions that they make and grant Carnegie the 27 * rights to redistribute these changes. 28 */ 29 30#include <sys/cdefs.h> 31__KERNEL_RCSID(0, "$NetBSD: tsrtc.c,v 1.6 2008/04/04 09:33:07 tsutsui Exp $"); 32 33#include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 34 35#include <sys/param.h> 36#include <sys/kernel.h> 37#include <sys/systm.h> 38#include <sys/device.h> 39 40#include <sys/bus.h> 41 42#include <dev/clock_subr.h> 43#include <dev/ic/mc146818reg.h> 44#include <dev/ic/mc146818var.h> 45 46#include <evbarm/tsarm/tspldvar.h> 47#include <evbarm/tsarm/tsarmreg.h> 48 49int tsrtc_match(device_t, cfdata_t, void *); 50void tsrtc_attach(device_t, device_t, void *); 51 52struct tsrtc_softc { 53 struct mc146818_softc sc_mc; 54 bus_space_tag_t sc_iot; 55 bus_space_handle_t sc_dath; 56 bus_space_handle_t sc_idxh; 57}; 58 59CFATTACH_DECL_NEW(tsrtc, sizeof (struct tsrtc_softc), 60 tsrtc_match, tsrtc_attach, NULL, NULL); 61 62void tsrtc_write(struct mc146818_softc *, u_int, u_int); 63u_int tsrtc_read(struct mc146818_softc *, u_int); 64 65 66int 67tsrtc_match(device_t parent, cfdata_t cf, void *aux) 68{ 69 struct tspld_attach_args *aa = aux; 70 struct tsrtc_softc tsrtc, *tsc; 71 struct mc146818_softc *sc; 72 unsigned int t1, t2; 73 static int found = -1; 74 75 if (found != -1) 76 return found; 77 78 tsc = &tsrtc; 79 sc = &tsc->sc_mc; 80 tsc->sc_iot = aa->ta_iot; 81 if (bus_space_map(tsc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCIDX, 1, 0, 82 &tsc->sc_idxh)) 83 return (0); 84 if (bus_space_map(tsc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCDAT, 1, 0, 85 &tsc->sc_dath)) 86 return (0); 87 88 /* Read from the seconds counter. */ 89 t1 = FROMBCD(tsrtc_read(sc, MC_SEC)); 90 if (t1 > 59) 91 goto unmap; 92 93 /* Wait, then look again. */ 94 DELAY(1100000); 95 t2 = FROMBCD(tsrtc_read(sc, MC_SEC)); 96 if (t2 > 59) 97 goto unmap; 98 99 /* If [1,2) seconds have passed since, call it a clock. */ 100 if ((t1 + 1) % 60 == t2 || (t1 + 2) % 60 == t2) 101 found = 1; 102 103 unmap: 104 bus_space_unmap(tsc->sc_iot, tsc->sc_idxh, 1); 105 bus_space_unmap(tsc->sc_iot, tsc->sc_dath, 1); 106 107 return (found); 108} 109 110void 111tsrtc_attach(device_t parent, device_t self, void *aux) 112{ 113 struct tsrtc_softc *tsc = device_private(self); 114 struct mc146818_softc *sc = &tsc->sc_mc; 115 struct tspld_attach_args *aa = aux; 116 117 sc->sc_dev = self; 118 tsc->sc_iot = aa->ta_iot; 119 if (bus_space_map(tsc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCIDX, 120 1, 0, &tsc->sc_idxh)) 121 panic("tsrtc_attach: couldn't map clock I/O space"); 122 if (bus_space_map(tsc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCDAT, 123 1, 0, &tsc->sc_dath)) 124 panic("tsrtc_attach: couldn't map clock I/O space"); 125 126 sc->sc_year0 = 2000; 127 sc->sc_mcread = tsrtc_read; 128 sc->sc_mcwrite = tsrtc_write; 129 sc->sc_flag = MC146818_BCD; 130 mc146818_attach(sc); 131 132 aprint_normal("\n"); 133 134 (*sc->sc_mcwrite)(sc, MC_REGB, MC_REGB_24HR); 135} 136 137void 138tsrtc_write(struct mc146818_softc *mc_sc, u_int reg, u_int datum) 139{ 140 struct tsrtc_softc *sc = (struct tsrtc_softc *)mc_sc; 141 142 bus_space_write_1(sc->sc_iot, sc->sc_idxh, 0, reg); 143 bus_space_write_1(sc->sc_iot, sc->sc_dath, 0, datum); 144} 145 146u_int 147tsrtc_read(struct mc146818_softc *mc_sc, u_int reg) 148{ 149 struct tsrtc_softc *sc = (struct tsrtc_softc *)mc_sc; 150 u_int datum; 151 152 bus_space_write_1(sc->sc_iot, sc->sc_idxh, 0, reg); 153 datum = bus_space_read_1(sc->sc_iot, sc->sc_dath, 0); 154 155 return (datum); 156} 157