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