mcclock_mainbus.c revision 1.7
11.7Stsutsui/*	$NetBSD: mcclock_mainbus.c,v 1.7 2008/01/09 14:16:35 tsutsui Exp $	*/
21.1Sthorpej
31.1Sthorpej/*
41.1Sthorpej * Copyright (c) 1995, 1996 Carnegie-Mellon University.
51.1Sthorpej * All rights reserved.
61.1Sthorpej *
71.1Sthorpej * Author: Chris G. Demetriou
81.1Sthorpej *
91.1Sthorpej * Permission to use, copy, modify and distribute this software and
101.1Sthorpej * its documentation is hereby granted, provided that both the copyright
111.1Sthorpej * notice and this permission notice appear in all copies of the
121.1Sthorpej * software, derivative works or modified versions, and any portions
131.1Sthorpej * thereof, and that both notices appear in supporting documentation.
141.1Sthorpej *
151.1Sthorpej * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
161.1Sthorpej * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
171.1Sthorpej * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
181.1Sthorpej *
191.1Sthorpej * Carnegie Mellon requests users of this software to return to
201.1Sthorpej *
211.1Sthorpej *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
221.1Sthorpej *  School of Computer Science
231.1Sthorpej *  Carnegie Mellon University
241.1Sthorpej *  Pittsburgh PA 15213-3890
251.1Sthorpej *
261.1Sthorpej * any improvements or extensions that they make and grant Carnegie the
271.1Sthorpej * rights to redistribute these changes.
281.1Sthorpej */
291.1Sthorpej
301.1Sthorpej#include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
311.1Sthorpej
321.7Stsutsui__KERNEL_RCSID(0, "$NetBSD: mcclock_mainbus.c,v 1.7 2008/01/09 14:16:35 tsutsui Exp $");
331.1Sthorpej
341.1Sthorpej#include <sys/param.h>
351.1Sthorpej#include <sys/kernel.h>
361.1Sthorpej#include <sys/systm.h>
371.1Sthorpej#include <sys/device.h>
381.1Sthorpej
391.1Sthorpej#include <machine/autoconf.h>
401.1Sthorpej#include <machine/bus.h>
411.1Sthorpej
421.6Sgdamore#include <dev/clock_subr.h>
431.1Sthorpej#include <dev/ic/mc146818reg.h>
441.6Sgdamore#include <dev/ic/mc146818var.h>
451.1Sthorpej
461.1Sthorpejint	mcclock_mainbus_match(struct device *, struct cfdata *, void *);
471.1Sthorpejvoid	mcclock_mainbus_attach(struct device *, struct device *, void *);
481.1Sthorpej
491.6SgdamoreCFATTACH_DECL(mcclock_mainbus, sizeof(struct mc146818_softc),
501.5Sthorpej    mcclock_mainbus_match, mcclock_mainbus_attach, NULL, NULL);
511.1Sthorpej
521.6Sgdamorevoid	mcclock_mainbus_write(struct mc146818_softc *, u_int, u_int);
531.6Sgdamoreu_int	mcclock_mainbus_read(struct mc146818_softc *, u_int);
541.1Sthorpej
551.1Sthorpejint
561.1Sthorpejmcclock_mainbus_match(struct device *parent, struct cfdata *match, void *aux)
571.1Sthorpej{
581.1Sthorpej	struct mainbus_attach_args *ma = aux;
591.1Sthorpej
601.2Sthorpej	if (strcmp(ma->ma_name, match->cf_name) == 0)
611.1Sthorpej		return (1);
621.1Sthorpej
631.1Sthorpej	return (0);
641.1Sthorpej}
651.1Sthorpej
661.1Sthorpejvoid
671.1Sthorpejmcclock_mainbus_attach(struct device *parent, struct device *self, void *aux)
681.1Sthorpej{
691.1Sthorpej	struct mainbus_attach_args *ma = aux;
701.6Sgdamore	struct mc146818_softc *sc = (struct mc146818_softc *)self;
711.1Sthorpej
721.6Sgdamore	sc->sc_bst = ma->ma_st;
731.6Sgdamore	if (bus_space_map(sc->sc_bst, ma->ma_addr, 2, 0, &sc->sc_bsh))
741.1Sthorpej		panic("mcclock_mainbus_attach: couldn't map clock I/O space");
751.1Sthorpej
761.6Sgdamore	/*
771.6Sgdamore	 * Turn interrupts off, just in case.  Need to leave the SQWE
781.6Sgdamore	 * set, because that's the DRAM refresh signal on Rev. B boards.
791.6Sgdamore	 */
801.6Sgdamore	mcclock_mainbus_write(sc, MC_REGB, MC_REGB_SQWE | MC_REGB_BINARY |
811.6Sgdamore	    MC_REGB_24HR);
821.6Sgdamore
831.6Sgdamore	sc->sc_mcread = mcclock_mainbus_read;
841.6Sgdamore	sc->sc_mcwrite = mcclock_mainbus_write;
851.6Sgdamore	sc->sc_getcent = NULL;
861.6Sgdamore	sc->sc_setcent = NULL;
871.6Sgdamore	sc->sc_flag = 0;
881.6Sgdamore
891.6Sgdamore	/* Algor uses year 1980 as offset */
901.7Stsutsui	sc->sc_year0 = 1980;
911.6Sgdamore
921.6Sgdamore	mc146818_attach(sc);
931.7Stsutsui
941.7Stsutsui	aprint_normal("\n");
951.7Stsutsui
961.7Stsutsui	todr_attach(&sc->sc_handle);
971.1Sthorpej}
981.1Sthorpej
991.1Sthorpejvoid
1001.6Sgdamoremcclock_mainbus_write(struct mc146818_softc *sc, u_int reg, u_int datum)
1011.1Sthorpej{
1021.6Sgdamore	bus_space_tag_t iot = sc->sc_bst;
1031.6Sgdamore	bus_space_handle_t ioh = sc->sc_bsh;
1041.1Sthorpej
1051.1Sthorpej	bus_space_write_1(iot, ioh, 0, reg);
1061.1Sthorpej	bus_space_write_1(iot, ioh, 1, datum);
1071.1Sthorpej}
1081.1Sthorpej
1091.1Sthorpeju_int
1101.6Sgdamoremcclock_mainbus_read(struct mc146818_softc *sc, u_int reg)
1111.1Sthorpej{
1121.6Sgdamore	bus_space_tag_t iot = sc->sc_bst;
1131.6Sgdamore	bus_space_handle_t ioh = sc->sc_bsh;
1141.1Sthorpej
1151.1Sthorpej	bus_space_write_1(iot, ioh, 0, reg);
1161.1Sthorpej	return bus_space_read_1(iot, ioh, 1);
1171.1Sthorpej}
118