1 /* $NetBSD: acafh.c,v 1.3 2013/12/26 20:38:11 rkujawa Exp $ */ 2 3 /*- 4 * Copyright (c) 2013 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Radoslaw Kujawa. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: acafh.c,v 1.3 2013/12/26 20:38:11 rkujawa Exp $"); 34 35 /* 36 * Individual Computers ACA500 driver. 37 */ 38 39 #include <sys/param.h> 40 #include <sys/device.h> 41 #include <sys/malloc.h> 42 #include <sys/socket.h> 43 #include <sys/systm.h> 44 #include <sys/bus.h> 45 #include <sys/types.h> 46 47 #include <uvm/uvm.h> 48 49 #include <machine/cpu.h> 50 51 #include <amiga/amiga/device.h> 52 #include <amiga/amiga/isr.h> 53 54 #include <amiga/dev/zbusvar.h> 55 #include <amiga/dev/acafhvar.h> 56 #include <amiga/dev/acafhreg.h> 57 58 int acafh_match(device_t, cfdata_t , void *); 59 void acafh_attach(device_t, device_t, void *); 60 static int acafh_print(void *, const char *); 61 static uint8_t acafh_reg_read(struct acafh_softc *, uint8_t); 62 static uint8_t acafh_revision(struct acafh_softc *); 63 64 CFATTACH_DECL_NEW(acafh, sizeof(struct acafh_softc), 65 acafh_match, acafh_attach, NULL, NULL); 66 67 /* 68 * Since ACA500 is not an AutoConfig board and current amiga port infrastructure 69 * does not have typical obio attachment, we need to hack in the probe procedure 70 * into mbattach(). This is supposed to be a temporary solution. 71 */ 72 bool 73 acafh_mbattach_probe(void) 74 { 75 vaddr_t aca_rom_vbase; 76 struct bus_space_tag aca_rom_bst; 77 bus_space_tag_t aca_rom_t; 78 bus_space_handle_t aca_rom_h; 79 uint32_t aca_id; 80 bool rv; 81 82 rv = false; 83 84 #ifdef ACAFH_DEBUG 85 aprint_normal("acafh: probing for ACA500\n"); 86 #endif /* ACAFH_DEBUG */ 87 88 /* 89 * Allocate VA to hold one mapped page, which we will use 90 * to access the beginning of ACA500 flash. 91 */ 92 aca_rom_vbase = uvm_km_alloc(kernel_map, 93 PAGE_SIZE, 0, UVM_KMF_VAONLY | UVM_KMF_NOWAIT); 94 95 /* Create the physical to virtual mapping. */ 96 pmap_enter(vm_map_pmap(kernel_map), aca_rom_vbase, ACAFH_ROM_BASE, 97 VM_PROT_READ, PMAP_NOCACHE); 98 pmap_update(vm_map_pmap(kernel_map)); 99 100 aca_rom_bst.base = (bus_addr_t) aca_rom_vbase; 101 aca_rom_bst.absm = &amiga_bus_stride_1; 102 aca_rom_t = &aca_rom_bst; 103 bus_space_map(aca_rom_t, 0, PAGE_SIZE, 0, &aca_rom_h); 104 105 /* Read out the ID. */ 106 aca_id = bus_space_read_4(aca_rom_t, aca_rom_h, ACAFH_ROM_ID_OFFSET); 107 #ifdef ACAFH_DEBUG 108 aprint_normal("acafh: probe read %x from ACA ROM offset %x\n", aca_id, 109 ACAFH_ROM_ID_OFFSET); 110 #endif /* ACAFH_DEBUG */ 111 112 if (aca_id == ACAFH_ROM_ID_VALUE) 113 rv = true; 114 else 115 rv = false; 116 117 #ifdef ACAFH_DEBUG 118 aprint_normal("acafh: clean up after probe\n"); 119 #endif /* ACAFH_DEBUG */ 120 121 pmap_remove(vm_map_pmap(kernel_map), aca_rom_vbase, aca_rom_vbase + PAGE_SIZE); 122 pmap_update(vm_map_pmap(kernel_map)); 123 124 uvm_km_free(kernel_map, aca_rom_vbase, PAGE_SIZE, 125 UVM_KMF_VAONLY | UVM_KMF_NOWAIT); 126 127 return rv; 128 } 129 130 int 131 acafh_match(device_t parent, cfdata_t cf, void *aux) 132 { 133 if (matchname(aux, "acafh") == 0) 134 return(0); 135 return(1); 136 } 137 138 void 139 acafh_attach(device_t parent, device_t self, void *aux) 140 { 141 struct acafh_softc *sc; 142 vaddr_t aca_vbase; 143 int i; 144 struct acafhbus_attach_args aaa_wdc; 145 struct acafhbus_attach_args aaa_cp; 146 147 sc = device_private(self); 148 sc->sc_dev = self; 149 150 /* 151 * Allocate enough kernel memory. 152 * XXX: we should be sure to prepare enough kva during early init... 153 */ 154 aca_vbase = uvm_km_alloc(kernel_map, 155 ACAFH_END - ACAFH_BASE, 0, UVM_KMF_VAONLY | UVM_KMF_NOWAIT); 156 157 if (aca_vbase == 0) { 158 aprint_error_dev(sc->sc_dev, 159 "failed allocating virtual memory\n"); 160 return; 161 } 162 163 /* 164 * Map the ACA500 registers into kernel virutal space. 165 */ 166 for (i = ACAFH_BASE; i < ACAFH_END; i += PAGE_SIZE) 167 pmap_enter(vm_map_pmap(kernel_map), 168 i - ACAFH_BASE + aca_vbase, i, 169 VM_PROT_READ | VM_PROT_WRITE, true); 170 pmap_update(vm_map_pmap(kernel_map)); 171 172 aca_vbase += ACAFH_FIRST_REG_OFF; 173 174 sc->sc_aca_bst.base = (bus_addr_t) aca_vbase; 175 sc->sc_aca_bst.absm = &amiga_bus_stride_1; 176 /* sc->sc_aca_bst.absm = &amiga_bus_stride_0x4000; */ 177 sc->sc_aca_iot = &sc->sc_aca_bst; 178 179 bus_space_map(sc->sc_aca_iot, 0, 0xF, 0, &sc->sc_aca_ioh); 180 181 #ifdef ACAFH_DEBUG 182 aprint_normal_dev(sc->sc_dev, 183 "ACA500 registers mapped to pa %x (va %x)\n", 184 kvtop((void*)sc->sc_aca_ioh), sc->sc_aca_bst.base); 185 aprint_normal_dev(sc->sc_dev, "AUX intr enable %x\n", 186 acafh_reg_read(sc, ACAFH_MEMPROBE_AUXIRQ)); 187 #endif /* ACAFH_DEBUG */ 188 189 aprint_normal(": Individual Computers ACA500 (rev %x)\n", 190 acafh_revision(sc)); 191 192 aprint_normal_dev(sc->sc_dev, "CF cards present: "); 193 if (acafh_reg_read(sc, ACAFH_CF_DETECT_BOOT)) { 194 aprint_normal("BOOT "); 195 } 196 if (acafh_reg_read(sc, ACAFH_CF_DETECT_AUX)) { 197 aprint_normal("AUX "); 198 } 199 aprint_normal("\n"); 200 201 aaa_wdc.aaa_pbase = (bus_addr_t) GAYLE_IDE_BASE + 2; 202 strcpy(aaa_wdc.aaa_name, "wdc_acafh"); 203 config_found_ia(sc->sc_dev, "acafhbus", &aaa_wdc, acafh_print); 204 205 aaa_cp.aaa_pbase = (bus_addr_t) ACAFH_CLOCKPORT_BASE; 206 strcpy(aaa_cp.aaa_name, "gencp_acafh"); 207 config_found_ia(sc->sc_dev, "acafhbus", &aaa_cp, acafh_print); 208 } 209 210 uint8_t 211 acafh_cf_intr_status(struct acafh_softc *sc, uint8_t slot) 212 { 213 uint8_t status; 214 215 if (slot == 0) { 216 status = acafh_reg_read(sc, ACAFH_CF_IRQ_BOOT); 217 } else { 218 status = acafh_reg_read(sc, ACAFH_CF_IRQ_AUX); 219 } 220 221 return status; 222 } 223 224 static uint8_t 225 acafh_revision(struct acafh_softc *sc) 226 { 227 uint8_t revbit0, revbit1, revbit2, revbit3; 228 229 revbit3 = acafh_reg_read(sc, ACAFH_VERSION_BIT3); 230 revbit2 = acafh_reg_read(sc, ACAFH_VERSION_BIT2); 231 revbit1 = acafh_reg_read(sc, ACAFH_VERSION_BIT1); 232 revbit0 = acafh_reg_read(sc, ACAFH_VERSION_BIT0); 233 234 return revbit0 | (revbit1 << 1) | (revbit2 << 2) | (revbit3 << 3); 235 } 236 237 static uint8_t 238 acafh_reg_read(struct acafh_softc *sc, uint8_t reg) 239 { 240 uint16_t val; 241 242 val = bus_space_read_2(sc->sc_aca_iot, sc->sc_aca_ioh, reg * 0x4000); 243 #ifdef ACAFH_DEBUG 244 aprint_normal_dev(sc->sc_dev, "read out reg %x from %lx (%x), val %x\n", 245 reg, sc->sc_aca_ioh + reg, (bus_addr_t) kvtop((void*) 246 ((unsigned) sc->sc_aca_ioh + reg * 0x4000)), val); 247 #endif /* ACAFH_DEBUG */ 248 249 return (val & ACAFH_MSB_MASK) >> ACAFH_MSB_SHIFT; 250 } 251 252 static int 253 acafh_print(void *aux, const char *w) 254 { 255 if (w == NULL) 256 return 0; 257 258 return 0; 259 } 260 261