mcclock_isa.c revision 1.1.8.3       1  1.1.8.3  yamt /*	$NetBSD: mcclock_isa.c,v 1.1.8.3 2008/01/21 09:39:08 yamt Exp $	*/
      2  1.1.8.2  yamt 
      3  1.1.8.2  yamt /*-
      4  1.1.8.2  yamt  * Copyright (c) 1990 The Regents of the University of California.
      5  1.1.8.2  yamt  * All rights reserved.
      6  1.1.8.2  yamt  *
      7  1.1.8.2  yamt  * This code is derived from software contributed to Berkeley by
      8  1.1.8.2  yamt  * William Jolitz and Don Ahn.
      9  1.1.8.2  yamt  *
     10  1.1.8.2  yamt  * Redistribution and use in source and binary forms, with or without
     11  1.1.8.2  yamt  * modification, are permitted provided that the following conditions
     12  1.1.8.2  yamt  * are met:
     13  1.1.8.2  yamt  * 1. Redistributions of source code must retain the above copyright
     14  1.1.8.2  yamt  *    notice, this list of conditions and the following disclaimer.
     15  1.1.8.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1.8.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     17  1.1.8.2  yamt  *    documentation and/or other materials provided with the distribution.
     18  1.1.8.2  yamt  * 3. Neither the name of the University nor the names of its contributors
     19  1.1.8.2  yamt  *    may be used to endorse or promote products derived from this software
     20  1.1.8.2  yamt  *    without specific prior written permission.
     21  1.1.8.2  yamt  *
     22  1.1.8.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.1.8.2  yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.1.8.2  yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.1.8.2  yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.1.8.2  yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.1.8.2  yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.1.8.2  yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.1.8.2  yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.1.8.2  yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.1.8.2  yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.1.8.2  yamt  * SUCH DAMAGE.
     33  1.1.8.2  yamt  *
     34  1.1.8.2  yamt  *	@(#)clock.c	7.2 (Berkeley) 5/12/91
     35  1.1.8.2  yamt  */
     36  1.1.8.2  yamt /*-
     37  1.1.8.2  yamt  * Copyright (c) 1993, 1994 Charles M. Hannum.
     38  1.1.8.2  yamt  *
     39  1.1.8.2  yamt  * This code is derived from software contributed to Berkeley by
     40  1.1.8.2  yamt  * William Jolitz and Don Ahn.
     41  1.1.8.2  yamt  *
     42  1.1.8.2  yamt  * Redistribution and use in source and binary forms, with or without
     43  1.1.8.2  yamt  * modification, are permitted provided that the following conditions
     44  1.1.8.2  yamt  * are met:
     45  1.1.8.2  yamt  * 1. Redistributions of source code must retain the above copyright
     46  1.1.8.2  yamt  *    notice, this list of conditions and the following disclaimer.
     47  1.1.8.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     48  1.1.8.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     49  1.1.8.2  yamt  *    documentation and/or other materials provided with the distribution.
     50  1.1.8.2  yamt  * 3. All advertising materials mentioning features or use of this software
     51  1.1.8.2  yamt  *    must display the following acknowledgement:
     52  1.1.8.2  yamt  *	This product includes software developed by the University of
     53  1.1.8.2  yamt  *	California, Berkeley and its contributors.
     54  1.1.8.2  yamt  * 4. Neither the name of the University nor the names of its contributors
     55  1.1.8.2  yamt  *    may be used to endorse or promote products derived from this software
     56  1.1.8.2  yamt  *    without specific prior written permission.
     57  1.1.8.2  yamt  *
     58  1.1.8.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  1.1.8.2  yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  1.1.8.2  yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  1.1.8.2  yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  1.1.8.2  yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  1.1.8.2  yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  1.1.8.2  yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  1.1.8.2  yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  1.1.8.2  yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  1.1.8.2  yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  1.1.8.2  yamt  * SUCH DAMAGE.
     69  1.1.8.2  yamt  *
     70  1.1.8.2  yamt  *	@(#)clock.c	7.2 (Berkeley) 5/12/91
     71  1.1.8.2  yamt  */
     72  1.1.8.2  yamt /*
     73  1.1.8.2  yamt  * Mach Operating System
     74  1.1.8.2  yamt  * Copyright (c) 1991,1990,1989 Carnegie Mellon University
     75  1.1.8.2  yamt  * All Rights Reserved.
     76  1.1.8.2  yamt  *
     77  1.1.8.2  yamt  * Permission to use, copy, modify and distribute this software and its
     78  1.1.8.2  yamt  * documentation is hereby granted, provided that both the copyright
     79  1.1.8.2  yamt  * notice and this permission notice appear in all copies of the
     80  1.1.8.2  yamt  * software, derivative works or modified versions, and any portions
     81  1.1.8.2  yamt  * thereof, and that both notices appear in supporting documentation.
     82  1.1.8.2  yamt  *
     83  1.1.8.2  yamt  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     84  1.1.8.2  yamt  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     85  1.1.8.2  yamt  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     86  1.1.8.2  yamt  *
     87  1.1.8.2  yamt  * Carnegie Mellon requests users of this software to return to
     88  1.1.8.2  yamt  *
     89  1.1.8.2  yamt  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     90  1.1.8.2  yamt  *  School of Computer Science
     91  1.1.8.2  yamt  *  Carnegie Mellon University
     92  1.1.8.2  yamt  *  Pittsburgh PA 15213-3890
     93  1.1.8.2  yamt  *
     94  1.1.8.2  yamt  * any improvements or extensions that they make and grant Carnegie Mellon
     95  1.1.8.2  yamt  * the rights to redistribute these changes.
     96  1.1.8.2  yamt  */
     97  1.1.8.2  yamt /*
     98  1.1.8.2  yamt   Copyright 1988, 1989 by Intel Corporation, Santa Clara, California.
     99  1.1.8.2  yamt 
    100  1.1.8.2  yamt 		All Rights Reserved
    101  1.1.8.2  yamt 
    102  1.1.8.2  yamt Permission to use, copy, modify, and distribute this software and
    103  1.1.8.2  yamt its documentation for any purpose and without fee is hereby
    104  1.1.8.2  yamt granted, provided that the above copyright notice appears in all
    105  1.1.8.2  yamt copies and that both the copyright notice and this permission notice
    106  1.1.8.2  yamt appear in supporting documentation, and that the name of Intel
    107  1.1.8.2  yamt not be used in advertising or publicity pertaining to distribution
    108  1.1.8.2  yamt of the software without specific, written prior permission.
    109  1.1.8.2  yamt 
    110  1.1.8.2  yamt INTEL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
    111  1.1.8.2  yamt INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
    112  1.1.8.2  yamt IN NO EVENT SHALL INTEL BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
    113  1.1.8.2  yamt CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
    114  1.1.8.2  yamt LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
    115  1.1.8.2  yamt NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
    116  1.1.8.2  yamt WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    117  1.1.8.2  yamt */
    118  1.1.8.2  yamt 
    119  1.1.8.2  yamt #include <sys/cdefs.h>
    120  1.1.8.3  yamt __KERNEL_RCSID(0, "$NetBSD: mcclock_isa.c,v 1.1.8.3 2008/01/21 09:39:08 yamt Exp $");
    121  1.1.8.2  yamt 
    122  1.1.8.2  yamt #include <sys/param.h>
    123  1.1.8.2  yamt #include <sys/systm.h>
    124  1.1.8.2  yamt #include <sys/kernel.h>
    125  1.1.8.2  yamt #include <sys/device.h>
    126  1.1.8.2  yamt #include <sys/time.h>
    127  1.1.8.2  yamt 
    128  1.1.8.2  yamt #include <machine/bus.h>
    129  1.1.8.2  yamt 
    130  1.1.8.2  yamt #include <dev/clock_subr.h>
    131  1.1.8.2  yamt #include <dev/isa/isareg.h>
    132  1.1.8.2  yamt #include <dev/isa/isavar.h>
    133  1.1.8.2  yamt #include <dev/ic/mc146818reg.h>
    134  1.1.8.2  yamt #include <dev/ic/mc146818var.h>
    135  1.1.8.2  yamt 
    136  1.1.8.2  yamt /*
    137  1.1.8.2  yamt  * We only deal with the RTC portion of the mc146818 right now.  Later
    138  1.1.8.2  yamt  * we might want to add support for the NVRAM portion, since it
    139  1.1.8.2  yamt  * appears that some systems use that.
    140  1.1.8.2  yamt  */
    141  1.1.8.2  yamt 
    142  1.1.8.2  yamt static int mcclock_isa_probe(struct device *, struct cfdata *, void *);
    143  1.1.8.2  yamt static void mcclock_isa_attach(struct device *, struct device *, void *);
    144  1.1.8.2  yamt 
    145  1.1.8.2  yamt CFATTACH_DECL(mcclock_isa, sizeof(struct mc146818_softc),
    146  1.1.8.2  yamt     mcclock_isa_probe, mcclock_isa_attach, NULL, NULL);
    147  1.1.8.2  yamt 
    148  1.1.8.2  yamt static unsigned
    149  1.1.8.2  yamt mcclock_isa_read(struct mc146818_softc *sc, unsigned reg)
    150  1.1.8.2  yamt {
    151  1.1.8.2  yamt 
    152  1.1.8.2  yamt 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, 0, reg);
    153  1.1.8.2  yamt 	return bus_space_read_1(sc->sc_bst, sc->sc_bsh, 1);
    154  1.1.8.2  yamt }
    155  1.1.8.2  yamt 
    156  1.1.8.2  yamt static void
    157  1.1.8.2  yamt mcclock_isa_write(struct mc146818_softc *sc, unsigned reg, unsigned datum)
    158  1.1.8.2  yamt {
    159  1.1.8.2  yamt 
    160  1.1.8.2  yamt 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, 0, reg);
    161  1.1.8.2  yamt 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, 1, datum);
    162  1.1.8.2  yamt }
    163  1.1.8.2  yamt 
    164  1.1.8.2  yamt int
    165  1.1.8.2  yamt mcclock_isa_probe(struct device *parent, struct cfdata *match, void *aux)
    166  1.1.8.2  yamt {
    167  1.1.8.2  yamt 	struct isa_attach_args *ia = aux;
    168  1.1.8.2  yamt 	bus_space_handle_t ioh;
    169  1.1.8.2  yamt 
    170  1.1.8.2  yamt 	if (ia->ia_nio < 1)
    171  1.1.8.2  yamt 		return 0;
    172  1.1.8.2  yamt 
    173  1.1.8.2  yamt 	if (ISA_DIRECT_CONFIG(ia))
    174  1.1.8.2  yamt 		return 0;
    175  1.1.8.2  yamt 
    176  1.1.8.2  yamt 	if ((ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT) &&
    177  1.1.8.2  yamt 	    (ia->ia_io[0].ir_addr != IO_RTC))
    178  1.1.8.2  yamt 		return 0;
    179  1.1.8.2  yamt 
    180  1.1.8.2  yamt 	if (ia->ia_niomem > 0 &&
    181  1.1.8.2  yamt 	    (ia->ia_iomem[0].ir_addr != ISA_UNKNOWN_IOMEM))
    182  1.1.8.2  yamt 		return 0;
    183  1.1.8.2  yamt 
    184  1.1.8.2  yamt 	if (ia->ia_nirq > 0 &&
    185  1.1.8.2  yamt 	    (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ))
    186  1.1.8.2  yamt 		return 0;
    187  1.1.8.2  yamt 
    188  1.1.8.2  yamt 	if (ia->ia_ndrq > 0 &&
    189  1.1.8.2  yamt 	    (ia->ia_drq[0].ir_drq != ISA_UNKNOWN_DRQ))
    190  1.1.8.2  yamt 		return 0;
    191  1.1.8.2  yamt 
    192  1.1.8.2  yamt 	if (bus_space_map(ia->ia_iot, IO_RTC, 2, 0, &ioh))
    193  1.1.8.2  yamt 		return 0;
    194  1.1.8.2  yamt 	bus_space_unmap(ia->ia_iot, ioh, 2);
    195  1.1.8.2  yamt 
    196  1.1.8.2  yamt 	ia->ia_io[0].ir_addr = IO_RTC;
    197  1.1.8.2  yamt 	ia->ia_io[0].ir_size = 2;
    198  1.1.8.2  yamt 	ia->ia_nio = 1;
    199  1.1.8.2  yamt 
    200  1.1.8.2  yamt 	ia->ia_niomem = 0;
    201  1.1.8.2  yamt 	ia->ia_nirq = 0;
    202  1.1.8.2  yamt 	ia->ia_ndrq = 0;
    203  1.1.8.2  yamt 
    204  1.1.8.2  yamt 	return 1;
    205  1.1.8.2  yamt }
    206  1.1.8.2  yamt 
    207  1.1.8.2  yamt void
    208  1.1.8.2  yamt mcclock_isa_attach(struct device *parent, struct device *self, void *aux)
    209  1.1.8.2  yamt {
    210  1.1.8.2  yamt 	struct mc146818_softc *sc = (struct mc146818_softc *)self;
    211  1.1.8.2  yamt 	struct isa_attach_args *ia = aux;
    212  1.1.8.2  yamt 
    213  1.1.8.2  yamt 	sc->sc_bst = ia->ia_iot;
    214  1.1.8.2  yamt 	if (bus_space_map(sc->sc_bst, ia->ia_io[0].ir_addr,
    215  1.1.8.2  yamt 		ia->ia_io[0].ir_size, 0, &sc->sc_bsh)) {
    216  1.1.8.2  yamt 		printf(": can't map registers!\n");
    217  1.1.8.2  yamt 		return;
    218  1.1.8.2  yamt 	}
    219  1.1.8.2  yamt 
    220  1.1.8.2  yamt 	/*
    221  1.1.8.2  yamt 	 * Select a 32KHz crystal, periodic interrupt every 1024 Hz.
    222  1.1.8.2  yamt 	 * XXX: We disable periodic interrupts, so why set a rate?
    223  1.1.8.2  yamt 	 */
    224  1.1.8.2  yamt 	mcclock_isa_write(sc, MC_REGA, MC_BASE_32_KHz | MC_RATE_1024_Hz);
    225  1.1.8.2  yamt 
    226  1.1.8.2  yamt 	/*
    227  1.1.8.2  yamt 	 * 24 Hour clock, no interrupts please.
    228  1.1.8.2  yamt 	 */
    229  1.1.8.2  yamt 	mcclock_isa_write(sc, MC_REGB, MC_REGB_24HR);
    230  1.1.8.2  yamt 
    231  1.1.8.2  yamt 	sc->sc_year0 = 1900;
    232  1.1.8.2  yamt 	sc->sc_flag = 0;
    233  1.1.8.2  yamt 	sc->sc_mcread = mcclock_isa_read;
    234  1.1.8.2  yamt 	sc->sc_mcwrite = mcclock_isa_write;
    235  1.1.8.2  yamt #if 0
    236  1.1.8.2  yamt 	/*
    237  1.1.8.2  yamt 	 * XXX: perhaps on some systems we should manage the century byte?
    238  1.1.8.2  yamt 	 * For now we don't worry about it.  (Ugly MD code.)
    239  1.1.8.2  yamt 	 */
    240  1.1.8.2  yamt 	sc->sc_getcent = mcclock_isa_getcent;
    241  1.1.8.2  yamt 	sc->sc_setcent = mcclock_isa_setcent;
    242  1.1.8.2  yamt #endif
    243  1.1.8.2  yamt 	mc146818_attach(sc);
    244  1.1.8.2  yamt 	printf("\n");
    245  1.1.8.2  yamt }
    246