1 1.2 tsutsui /* $NetBSD: mcclock_frodo.c,v 1.2 2023/01/15 06:19:45 tsutsui Exp $ */ 2 1.1 tsutsui /*- 3 1.1 tsutsui * Copyright (c) 2014 Izumi Tsutsui. All rights reserved. 4 1.1 tsutsui * 5 1.1 tsutsui * Redistribution and use in source and binary forms, with or without 6 1.1 tsutsui * modification, are permitted provided that the following conditions 7 1.1 tsutsui * are met: 8 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright 9 1.1 tsutsui * notice, this list of conditions and the following disclaimer. 10 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright 11 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the 12 1.1 tsutsui * documentation and/or other materials provided with the distribution. 13 1.1 tsutsui * 14 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 1.1 tsutsui * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 1.1 tsutsui * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 1.1 tsutsui * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 1.1 tsutsui * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 1.1 tsutsui * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 1.1 tsutsui * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 1.1 tsutsui * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 1.1 tsutsui * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 1.1 tsutsui * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 1.1 tsutsui */ 25 1.1 tsutsui 26 1.1 tsutsui #include <sys/cdefs.h> 27 1.2 tsutsui __KERNEL_RCSID(0, "$NetBSD: mcclock_frodo.c,v 1.2 2023/01/15 06:19:45 tsutsui Exp $"); 28 1.1 tsutsui 29 1.1 tsutsui #include <sys/param.h> 30 1.1 tsutsui #include <sys/device.h> 31 1.1 tsutsui #include <sys/bus.h> 32 1.1 tsutsui 33 1.1 tsutsui #include <machine/cpu.h> 34 1.1 tsutsui 35 1.1 tsutsui #include <dev/clock_subr.h> 36 1.1 tsutsui #include <dev/ic/mc146818reg.h> 37 1.1 tsutsui #include <dev/ic/mc146818var.h> 38 1.1 tsutsui 39 1.1 tsutsui #include <hp300/dev/frodoreg.h> 40 1.1 tsutsui #include <hp300/dev/frodovar.h> 41 1.1 tsutsui 42 1.1 tsutsui #include "ioconf.h" 43 1.1 tsutsui 44 1.1 tsutsui static int mcclock_frodo_probe(device_t, cfdata_t, void *); 45 1.1 tsutsui static void mcclock_frodo_attach(device_t, device_t, void *); 46 1.1 tsutsui 47 1.1 tsutsui static u_int mcclock_frodo_read(struct mc146818_softc *, u_int); 48 1.1 tsutsui static void mcclock_frodo_write(struct mc146818_softc *, u_int, u_int); 49 1.1 tsutsui 50 1.1 tsutsui CFATTACH_DECL_NEW(mcclock_frodo, sizeof(struct mc146818_softc), 51 1.1 tsutsui mcclock_frodo_probe, mcclock_frodo_attach, NULL, NULL); 52 1.1 tsutsui 53 1.1 tsutsui #define MCCLOCK_FRODO_NPORTS 2 54 1.1 tsutsui 55 1.1 tsutsui static int 56 1.1 tsutsui mcclock_frodo_probe(device_t parent, cfdata_t cf, void *aux) 57 1.1 tsutsui { 58 1.1 tsutsui struct frodo_attach_args *fa = aux; 59 1.1 tsutsui 60 1.1 tsutsui /* only 425e has a valid mcclock */ 61 1.1 tsutsui if (machineid != HP_425 || mmuid != MMUID_425_E) 62 1.1 tsutsui return 0; 63 1.1 tsutsui 64 1.1 tsutsui if (strcmp(fa->fa_name, mcclock_cd.cd_name) != 0) 65 1.1 tsutsui return 0; 66 1.1 tsutsui 67 1.1 tsutsui if (fa->fa_offset != FRODO_CALENDAR) 68 1.1 tsutsui return 0; 69 1.1 tsutsui 70 1.1 tsutsui return 1; 71 1.1 tsutsui } 72 1.1 tsutsui 73 1.1 tsutsui static void 74 1.1 tsutsui mcclock_frodo_attach(device_t parent, device_t self, void *aux) 75 1.1 tsutsui { 76 1.1 tsutsui struct mc146818_softc *sc = device_private(self); 77 1.1 tsutsui struct frodo_attach_args *fa = aux; 78 1.1 tsutsui 79 1.1 tsutsui sc->sc_dev = self; 80 1.1 tsutsui sc->sc_bst = fa->fa_bst; 81 1.1 tsutsui 82 1.1 tsutsui if (bus_space_map(sc->sc_bst, fa->fa_base + fa->fa_offset, 83 1.1 tsutsui MCCLOCK_FRODO_NPORTS << 2, 0, &sc->sc_bsh) != 0) { 84 1.1 tsutsui aprint_error(": can't map register\n"); 85 1.1 tsutsui return; 86 1.1 tsutsui } 87 1.1 tsutsui 88 1.1 tsutsui sc->sc_year0 = 1900; 89 1.1 tsutsui sc->sc_mcread = mcclock_frodo_read; 90 1.1 tsutsui sc->sc_mcwrite = mcclock_frodo_write; 91 1.1 tsutsui sc->sc_flag = 0; 92 1.1 tsutsui mc146818_attach(sc); 93 1.1 tsutsui 94 1.1 tsutsui aprint_normal("\n"); 95 1.1 tsutsui 96 1.1 tsutsui /* XXX: not sure which mode (BINARY or BCD) is used on HP-UX */ 97 1.1 tsutsui mcclock_frodo_write(sc, MC_REGB, MC_REGB_BINARY | MC_REGB_24HR); 98 1.1 tsutsui 99 1.1 tsutsui /* make sure to start the 32.768kHz OSC */ 100 1.2 tsutsui mcclock_frodo_write(sc, MC_REGA, 101 1.1 tsutsui (mcclock_frodo_read(sc, MC_REGA) & ~MC_REGA_DVMASK) | 102 1.1 tsutsui MC_BASE_32_KHz); 103 1.1 tsutsui } 104 1.1 tsutsui 105 1.1 tsutsui /* 106 1.1 tsutsui * mc146818 calendar chip in the frodo utility chip is can be accessed 107 1.1 tsutsui * via register port at offset 0x80 and data port at offset 0x84. 108 1.1 tsutsui * 109 1.1 tsutsui * Note 4 byte offset stride is handled by frodo bus_space(9) layer, 110 1.1 tsutsui * so bus_space_read(9) and bus_space_write(9) takes 0 or 1 as offset 111 1.1 tsutsui * to access these registers. 112 1.1 tsutsui */ 113 1.1 tsutsui 114 1.1 tsutsui static void 115 1.1 tsutsui mcclock_frodo_write(struct mc146818_softc *sc, u_int reg, u_int datum) 116 1.1 tsutsui { 117 1.1 tsutsui bus_space_tag_t iot; 118 1.1 tsutsui bus_space_handle_t ioh; 119 1.1 tsutsui 120 1.1 tsutsui iot = sc->sc_bst; 121 1.1 tsutsui ioh = sc->sc_bsh; 122 1.1 tsutsui 123 1.1 tsutsui bus_space_write_1(iot, ioh, 0, reg); 124 1.1 tsutsui bus_space_write_1(iot, ioh, 1, datum); 125 1.1 tsutsui } 126 1.1 tsutsui 127 1.1 tsutsui static u_int 128 1.1 tsutsui mcclock_frodo_read(struct mc146818_softc *sc, u_int reg) 129 1.1 tsutsui { 130 1.1 tsutsui bus_space_tag_t iot; 131 1.1 tsutsui bus_space_handle_t ioh; 132 1.1 tsutsui u_int datum; 133 1.1 tsutsui 134 1.1 tsutsui iot = sc->sc_bst; 135 1.1 tsutsui ioh = sc->sc_bsh; 136 1.1 tsutsui 137 1.1 tsutsui bus_space_write_1(iot, ioh, 0, reg); 138 1.1 tsutsui datum = bus_space_read_1(iot, ioh, 1); 139 1.1 tsutsui 140 1.1 tsutsui return datum; 141 1.1 tsutsui } 142