1 1.1 thorpej /* $NetBSD: mcclock_ofisa.c,v 1.1 2021/04/27 21:39:39 thorpej Exp $ */ 2 1.1 thorpej 3 1.1 thorpej /*- 4 1.1 thorpej * Copyright (c) 2021 The NetBSD Foundation, Inc. 5 1.1 thorpej * All rights reserved. 6 1.1 thorpej * 7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation 8 1.1 thorpej * by Jason R. Thorpe. 9 1.1 thorpej * 10 1.1 thorpej * Redistribution and use in source and binary forms, with or without 11 1.1 thorpej * modification, are permitted provided that the following conditions 12 1.1 thorpej * are met: 13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright 14 1.1 thorpej * notice, this list of conditions and the following disclaimer. 15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the 17 1.1 thorpej * documentation and/or other materials provided with the distribution. 18 1.1 thorpej * 19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE. 30 1.1 thorpej */ 31 1.1 thorpej 32 1.1 thorpej /* 33 1.1 thorpej * OFW ISA attachment for the mc146818 real time clock. 34 1.1 thorpej */ 35 1.1 thorpej 36 1.1 thorpej #include <sys/cdefs.h> 37 1.1 thorpej __KERNEL_RCSID(0, "$NetBSD: mcclock_ofisa.c,v 1.1 2021/04/27 21:39:39 thorpej Exp $"); 38 1.1 thorpej 39 1.1 thorpej #include <sys/param.h> 40 1.1 thorpej #include <sys/systm.h> 41 1.1 thorpej #include <sys/kernel.h> 42 1.1 thorpej #include <sys/device.h> 43 1.1 thorpej 44 1.1 thorpej #include <sys/bus.h> 45 1.1 thorpej 46 1.1 thorpej #include <dev/clock_subr.h> 47 1.1 thorpej #include <dev/ic/mc146818var.h> 48 1.1 thorpej 49 1.1 thorpej #include <dev/ofw/openfirm.h> 50 1.1 thorpej #include <dev/isa/isavar.h> 51 1.1 thorpej #include <dev/ofisa/ofisavar.h> 52 1.1 thorpej 53 1.1 thorpej struct mcclock_ofisa_platform_data { 54 1.1 thorpej int year0; 55 1.1 thorpej int flags; 56 1.1 thorpej }; 57 1.1 thorpej 58 1.1 thorpej static const struct device_compatible_entry compat_data[] = { 59 1.1 thorpej { .compat = "pnpPNP,b00" }, 60 1.1 thorpej DEVICE_COMPAT_EOL 61 1.1 thorpej }; 62 1.1 thorpej 63 1.1 thorpej static const struct mcclock_ofisa_platform_data dec_dnard_data = { 64 1.1 thorpej .year0 = 1900, 65 1.1 thorpej .flags = MC146818_BCD, 66 1.1 thorpej }; 67 1.1 thorpej 68 1.1 thorpej static const struct device_compatible_entry platform_data[] = { 69 1.1 thorpej { .compat = "DEC,DNARD", .data = &dec_dnard_data, }, 70 1.1 thorpej DEVICE_COMPAT_EOL 71 1.1 thorpej }; 72 1.1 thorpej 73 1.1 thorpej static const struct mcclock_ofisa_platform_data * 74 1.1 thorpej lookup_platform_data(char *model, size_t const size) 75 1.1 thorpej { 76 1.1 thorpej const struct device_compatible_entry *dce; 77 1.1 thorpej 78 1.1 thorpej if (OF_getprop(OF_finddevice("/"), "model", model, size) <= 0) { 79 1.1 thorpej return NULL; 80 1.1 thorpej } 81 1.1 thorpej 82 1.1 thorpej /* should be plenty big enough, but never hurts... */ 83 1.1 thorpej model[size - 1] = '\0'; 84 1.1 thorpej const char *cmodel = model; 85 1.1 thorpej 86 1.1 thorpej dce = device_compatible_lookup(&cmodel, 1, platform_data); 87 1.1 thorpej if (dce != NULL) { 88 1.1 thorpej return dce->data; 89 1.1 thorpej } 90 1.1 thorpej return NULL; 91 1.1 thorpej } 92 1.1 thorpej 93 1.1 thorpej static u_int 94 1.1 thorpej mcclock_ofisa_read(struct mc146818_softc *sc, u_int reg) 95 1.1 thorpej { 96 1.1 thorpej bus_space_write_1(sc->sc_bst, sc->sc_bsh, 0, reg); 97 1.1 thorpej return bus_space_read_1(sc->sc_bst, sc->sc_bsh, 1); 98 1.1 thorpej } 99 1.1 thorpej 100 1.1 thorpej static void 101 1.1 thorpej mcclock_ofisa_write(struct mc146818_softc *sc, u_int reg, u_int datum) 102 1.1 thorpej { 103 1.1 thorpej bus_space_write_1(sc->sc_bst, sc->sc_bsh, 0, reg); 104 1.1 thorpej bus_space_write_1(sc->sc_bst, sc->sc_bsh, 1, (uint8_t)datum); 105 1.1 thorpej } 106 1.1 thorpej 107 1.1 thorpej static int 108 1.1 thorpej mcclock_ofisa_match(device_t parent, cfdata_t cf, void *aux) 109 1.1 thorpej { 110 1.1 thorpej struct ofisa_attach_args *aa = aux; 111 1.1 thorpej int rv; 112 1.1 thorpej 113 1.1 thorpej rv = of_compatible_match(aa->oba.oba_phandle, compat_data) ? 5 : 0; 114 1.1 thorpej #ifdef _MCCLOCK_OFISA_MD_MATCH 115 1.1 thorpej if (!rv) 116 1.1 thorpej rv = mcclock_ofisa_md_match(parent, cf, aux); 117 1.1 thorpej #endif 118 1.1 thorpej return rv; 119 1.1 thorpej } 120 1.1 thorpej 121 1.1 thorpej static void 122 1.1 thorpej mcclock_ofisa_attach(device_t parent, device_t self, void *aux) 123 1.1 thorpej { 124 1.1 thorpej struct mc146818_softc *sc = device_private(self); 125 1.1 thorpej struct ofisa_attach_args *aa = aux; 126 1.1 thorpej const struct mcclock_ofisa_platform_data *pd; 127 1.1 thorpej struct ofisa_reg_desc reg; 128 1.1 thorpej char model[64]; 129 1.1 thorpej int n; 130 1.1 thorpej 131 1.1 thorpej sc->sc_dev = self; 132 1.1 thorpej 133 1.1 thorpej n = ofisa_reg_get(aa->oba.oba_phandle, ®, 1); 134 1.1 thorpej #ifdef _MCCLOCK_OFISA_MD_REG_FIXUP 135 1.1 thorpej n = mcclock_ofisa_md_reg_fixup(parent, self, aux, ®, 1, n); 136 1.1 thorpej #endif 137 1.1 thorpej if (n != 1) { 138 1.1 thorpej aprint_error(": error getting register data\n"); 139 1.1 thorpej return; 140 1.1 thorpej } 141 1.1 thorpej if (reg.len != 2) { 142 1.1 thorpej aprint_error(": weird register size (%lu, expected 2)\n", 143 1.1 thorpej (unsigned long)reg.len); 144 1.1 thorpej return; 145 1.1 thorpej } 146 1.1 thorpej 147 1.1 thorpej /* 148 1.1 thorpej * We don't configure interrupts here; we're only using this 149 1.1 thorpej * for the TODR. 150 1.1 thorpej */ 151 1.1 thorpej 152 1.1 thorpej pd = lookup_platform_data(model, sizeof(model)); 153 1.1 thorpej 154 1.1 thorpej sc->sc_bst = (reg.type == OFISA_REG_TYPE_IO) ? aa->iot : aa->memt; 155 1.1 thorpej if (bus_space_map(sc->sc_bst, reg.addr, reg.len, 0, &sc->sc_bsh) != 0) { 156 1.1 thorpej aprint_error(": can't map register space\n"); 157 1.1 thorpej return; 158 1.1 thorpej } 159 1.1 thorpej 160 1.1 thorpej /* 161 1.1 thorpej * These default to 0. We'll warn after attaching if we didn't 162 1.1 thorpej * find any platform data. 163 1.1 thorpej */ 164 1.1 thorpej if (pd != NULL) { 165 1.1 thorpej sc->sc_year0 = pd->year0; 166 1.1 thorpej sc->sc_flag = pd->flags; 167 1.1 thorpej } 168 1.1 thorpej sc->sc_mcread = mcclock_ofisa_read; 169 1.1 thorpej sc->sc_mcwrite = mcclock_ofisa_write; 170 1.1 thorpej 171 1.1 thorpej mc146818_attach(sc); 172 1.1 thorpej 173 1.1 thorpej aprint_normal("\n"); 174 1.1 thorpej 175 1.1 thorpej if (pd == NULL) { 176 1.1 thorpej /* 177 1.1 thorpej * We could dynamically compute at least BCD-ness and 178 1.1 thorpej * base year by comparing against the OFW get-time 179 1.1 thorpej * call, but it hardly seems worth it right now. 180 1.1 thorpej */ 181 1.1 thorpej aprint_error_dev(self, 182 1.1 thorpej "WARNING: no platform data for '%s', using defaults.\n", 183 1.1 thorpej model); 184 1.1 thorpej } 185 1.1 thorpej } 186 1.1 thorpej 187 1.1 thorpej CFATTACH_DECL_NEW(mcclock_ofisa, sizeof(struct mc146818_softc), 188 1.1 thorpej mcclock_ofisa_match, mcclock_ofisa_attach, NULL, NULL); 189