1 1.10 tsutsui /* $NetBSD: sti_sgc.c,v 1.10 2025/05/28 17:33:38 tsutsui Exp $ */ 2 1.1 tsutsui /* $OpenBSD: sti_sgc.c,v 1.14 2007/05/26 00:36:03 krw Exp $ */ 3 1.1 tsutsui 4 1.1 tsutsui /* 5 1.1 tsutsui * Copyright (c) 2005, Miodrag Vallat 6 1.1 tsutsui * 7 1.1 tsutsui * Redistribution and use in source and binary forms, with or without 8 1.1 tsutsui * modification, are permitted provided that the following conditions 9 1.1 tsutsui * are met: 10 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright 11 1.1 tsutsui * notice, this list of conditions and the following disclaimer. 12 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the 14 1.1 tsutsui * documentation and/or other materials provided with the distribution. 15 1.1 tsutsui * 16 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 tsutsui * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 1.1 tsutsui * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 1.1 tsutsui * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 20 1.1 tsutsui * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 1.1 tsutsui * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 1.1 tsutsui * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 1.1 tsutsui * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 1.1 tsutsui * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 25 1.1 tsutsui * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 tsutsui * POSSIBILITY OF SUCH DAMAGE. 27 1.1 tsutsui * 28 1.1 tsutsui */ 29 1.1 tsutsui #include <sys/cdefs.h> 30 1.10 tsutsui __KERNEL_RCSID(0, "$NetBSD: sti_sgc.c,v 1.10 2025/05/28 17:33:38 tsutsui Exp $"); 31 1.1 tsutsui 32 1.1 tsutsui #include <sys/param.h> 33 1.1 tsutsui #include <sys/device.h> 34 1.1 tsutsui #include <sys/bus.h> 35 1.1 tsutsui 36 1.1 tsutsui #include <uvm/uvm_extern.h> 37 1.1 tsutsui 38 1.1 tsutsui #include <hp300/dev/sgcvar.h> 39 1.2 tsutsui #include <hp300/dev/sti_sgcvar.h> 40 1.9 tsutsui #include <hp300/dev/sti_machdep.h> 41 1.3 tsutsui 42 1.2 tsutsui static int sticonslot = -1; 43 1.1 tsutsui 44 1.1 tsutsui static int sti_sgc_match(device_t, struct cfdata *, void *); 45 1.1 tsutsui static void sti_sgc_attach(device_t, device_t, void *); 46 1.1 tsutsui 47 1.1 tsutsui static int sti_sgc_probe(bus_space_tag_t, int); 48 1.1 tsutsui 49 1.9 tsutsui CFATTACH_DECL_NEW(sti_sgc, sizeof(struct sti_machdep_softc), 50 1.1 tsutsui sti_sgc_match, sti_sgc_attach, NULL, NULL); 51 1.1 tsutsui 52 1.1 tsutsui static int 53 1.1 tsutsui sti_sgc_match(device_t parent, struct cfdata *cf, void *aux) 54 1.1 tsutsui { 55 1.1 tsutsui struct sgc_attach_args *saa = aux; 56 1.1 tsutsui 57 1.1 tsutsui /* 58 1.1 tsutsui * If we already probed it successfully as a console device, go ahead, 59 1.1 tsutsui * since we will not be able to bus_space_map() again. 60 1.1 tsutsui */ 61 1.1 tsutsui if (saa->saa_slot == sticonslot) 62 1.1 tsutsui return 1; 63 1.1 tsutsui 64 1.1 tsutsui return sti_sgc_probe(saa->saa_iot, saa->saa_slot); 65 1.1 tsutsui } 66 1.1 tsutsui 67 1.1 tsutsui static void 68 1.1 tsutsui sti_sgc_attach(device_t parent, device_t self, void *aux) 69 1.1 tsutsui { 70 1.9 tsutsui struct sti_machdep_softc *sc = device_private(self); 71 1.3 tsutsui struct sti_softc *ssc = &sc->sc_sti; 72 1.1 tsutsui struct sgc_attach_args *saa = aux; 73 1.4 tsutsui bus_space_tag_t bst; 74 1.2 tsutsui bus_space_handle_t romh; 75 1.2 tsutsui bus_addr_t base; 76 1.1 tsutsui u_int romend; 77 1.1 tsutsui int i; 78 1.1 tsutsui 79 1.3 tsutsui ssc->sc_dev = self; 80 1.4 tsutsui bst = saa->saa_iot; 81 1.3 tsutsui base = (bus_addr_t)sgc_slottopa(saa->saa_slot); 82 1.9 tsutsui sc->sc_base = base; 83 1.2 tsutsui 84 1.2 tsutsui if (saa->saa_slot == sticonslot) { 85 1.9 tsutsui sti_machdep_attach_console(sc); 86 1.2 tsutsui } else { 87 1.4 tsutsui if (bus_space_map(bst, base, PAGE_SIZE, 0, &romh)) { 88 1.2 tsutsui aprint_error(": can't map ROM"); 89 1.2 tsutsui return; 90 1.2 tsutsui } 91 1.2 tsutsui /* 92 1.2 tsutsui * Compute real PROM size 93 1.2 tsutsui */ 94 1.4 tsutsui romend = sti_rom_size(bst, romh); 95 1.2 tsutsui 96 1.4 tsutsui bus_space_unmap(bst, romh, PAGE_SIZE); 97 1.2 tsutsui 98 1.4 tsutsui if (bus_space_map(bst, base, romend, 0, &romh)) { 99 1.2 tsutsui aprint_error(": can't map frame buffer"); 100 1.2 tsutsui return; 101 1.2 tsutsui } 102 1.2 tsutsui 103 1.3 tsutsui ssc->bases[0] = romh; 104 1.2 tsutsui for (i = 0; i < STI_REGION_MAX; i++) 105 1.3 tsutsui ssc->bases[i] = base; 106 1.2 tsutsui 107 1.4 tsutsui if (sti_attach_common(ssc, bst, bst, romh, 108 1.10 tsutsui STI_CODEBASE_M68K) != 0) 109 1.2 tsutsui return; 110 1.1 tsutsui } 111 1.1 tsutsui 112 1.9 tsutsui sti_machdep_attach(sc); 113 1.1 tsutsui } 114 1.1 tsutsui 115 1.1 tsutsui static int 116 1.1 tsutsui sti_sgc_probe(bus_space_tag_t iot, int slot) 117 1.1 tsutsui { 118 1.1 tsutsui bus_space_handle_t ioh; 119 1.1 tsutsui int devtype; 120 1.1 tsutsui 121 1.2 tsutsui if (bus_space_map(iot, (bus_addr_t)sgc_slottopa(slot), 122 1.2 tsutsui PAGE_SIZE, 0, &ioh)) 123 1.1 tsutsui return 0; 124 1.2 tsutsui 125 1.1 tsutsui devtype = bus_space_read_1(iot, ioh, 3); 126 1.2 tsutsui 127 1.1 tsutsui bus_space_unmap(iot, ioh, PAGE_SIZE); 128 1.1 tsutsui 129 1.1 tsutsui /* 130 1.1 tsutsui * This might not be reliable enough. On the other hand, non-STI 131 1.7 andvar * SGC cards will apparently not initialize in the hp300, to the 132 1.1 tsutsui * point of not even answering bus probes (checked with an 133 1.1 tsutsui * Harmony/FDDI SGC card). 134 1.1 tsutsui */ 135 1.2 tsutsui if (devtype != STI_DEVTYPE1 && devtype != STI_DEVTYPE4) 136 1.1 tsutsui return 0; 137 1.2 tsutsui 138 1.1 tsutsui return 1; 139 1.1 tsutsui } 140 1.1 tsutsui 141 1.2 tsutsui int 142 1.9 tsutsui sti_sgc_cnprobe(bus_space_tag_t bst, int slot) 143 1.2 tsutsui { 144 1.2 tsutsui void *va; 145 1.2 tsutsui bus_space_handle_t romh; 146 1.9 tsutsui bus_addr_t base; 147 1.2 tsutsui int devtype, rv = 0; 148 1.2 tsutsui 149 1.9 tsutsui base = sgc_slottopa(slot); 150 1.9 tsutsui if (bus_space_map(bst, base, PAGE_SIZE, 0, &romh)) 151 1.2 tsutsui return 0; 152 1.2 tsutsui 153 1.2 tsutsui va = bus_space_vaddr(bst, romh); 154 1.2 tsutsui if (badaddr(va)) 155 1.2 tsutsui goto out; 156 1.2 tsutsui 157 1.2 tsutsui devtype = bus_space_read_1(bst, romh, 3); 158 1.2 tsutsui if (devtype == STI_DEVTYPE1 || devtype == STI_DEVTYPE4) 159 1.2 tsutsui rv = 1; 160 1.2 tsutsui 161 1.2 tsutsui out: 162 1.2 tsutsui bus_space_unmap(bst, romh, PAGE_SIZE); 163 1.2 tsutsui return rv; 164 1.2 tsutsui } 165 1.2 tsutsui 166 1.2 tsutsui void 167 1.9 tsutsui sti_sgc_cnattach(bus_space_tag_t bst, int slot) 168 1.1 tsutsui { 169 1.9 tsutsui paddr_t base; 170 1.4 tsutsui 171 1.9 tsutsui base = sgc_slottopa(slot); 172 1.2 tsutsui 173 1.9 tsutsui sti_machdep_cnattach(bst, base); 174 1.1 tsutsui 175 1.2 tsutsui sticonslot = slot; 176 1.1 tsutsui } 177