1 1.32 tsutsui /* $NetBSD: wdc_obio.c,v 1.32 2022/08/20 20:02:22 tsutsui Exp $ */ 2 1.1 shiba 3 1.1 shiba /* 4 1.1 shiba * Copyright (c) 2002 Takeshi Shibagaki All rights reserved. 5 1.1 shiba * 6 1.1 shiba * mac68k OBIO-IDE attachment created by Takeshi Shibagaki 7 1.1 shiba * 8 1.1 shiba * Redistribution and use in source and binary forms, with or without 9 1.1 shiba * modification, are permitted provided that the following conditions 10 1.1 shiba * are met: 11 1.1 shiba * 1. Redistributions of source code must retain the above copyright 12 1.1 shiba * notice, this list of conditions and the following disclaimer. 13 1.1 shiba * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 shiba * notice, this list of conditions and the following disclaimer in the 15 1.1 shiba * documentation and/or other materials provided with the distribution. 16 1.1 shiba * 3. All advertising materials mentioning features or use of this software 17 1.1 shiba * must display the following acknowledgement: 18 1.1 shiba * This product includes software developed by Charles M. Hannum. 19 1.1 shiba * 4. The name of the author may not be used to endorse or promote products 20 1.1 shiba * derived from this software without specific prior written permission. 21 1.1 shiba * 22 1.1 shiba * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 1.1 shiba * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 1.1 shiba * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 1.1 shiba * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 1.1 shiba * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 1.1 shiba * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 1.1 shiba * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 1.1 shiba * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 1.1 shiba * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 1.1 shiba * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 1.1 shiba */ 33 1.4 lukem 34 1.4 lukem #include <sys/cdefs.h> 35 1.32 tsutsui __KERNEL_RCSID(0, "$NetBSD: wdc_obio.c,v 1.32 2022/08/20 20:02:22 tsutsui Exp $"); 36 1.1 shiba 37 1.1 shiba #include <sys/types.h> 38 1.1 shiba #include <sys/param.h> 39 1.1 shiba #include <sys/systm.h> 40 1.1 shiba #include <sys/device.h> 41 1.1 shiba #include <sys/kernel.h> 42 1.30 rin #include <sys/kmem.h> 43 1.1 shiba #include <sys/callout.h> 44 1.1 shiba 45 1.1 shiba #include <machine/bus.h> 46 1.1 shiba #include <machine/intr.h> 47 1.1 shiba #include <machine/cpu.h> 48 1.1 shiba #include <machine/viareg.h> 49 1.1 shiba 50 1.1 shiba #include <mac68k/obio/obiovar.h> 51 1.1 shiba 52 1.1 shiba #include <dev/ata/atavar.h> 53 1.1 shiba #include <dev/ic/wdcvar.h> 54 1.1 shiba 55 1.8 fredb #define WDC_OBIO_REG_NPORTS 0x40 56 1.1 shiba #define WDC_OBIO_AUXREG_OFFSET 0x38 57 1.1 shiba #define WDC_OBIO_AUXREG_NPORTS 1 58 1.1 shiba #define WDC_OBIO_ISR_OFFSET 0x101 59 1.1 shiba #define WDC_OBIO_ISR_NPORTS 1 60 1.1 shiba 61 1.1 shiba static u_long IDEBase = 0x50f1a000; 62 1.1 shiba 63 1.1 shiba /* 64 1.1 shiba * XXX This code currently doesn't even try to allow 32-bit data port use. 65 1.1 shiba */ 66 1.1 shiba 67 1.1 shiba struct wdc_obio_softc { 68 1.1 shiba struct wdc_softc sc_wdcdev; 69 1.16 thorpej struct ata_channel *sc_chanlist[1]; 70 1.16 thorpej struct ata_channel sc_channel; 71 1.16 thorpej struct wdc_regs sc_wdc_regs; 72 1.1 shiba void *sc_ih; 73 1.1 shiba }; 74 1.1 shiba 75 1.23 cube int wdc_obio_match(device_t, cfdata_t, void *); 76 1.23 cube void wdc_obio_attach(device_t, device_t, void *); 77 1.18 chs void wdc_obio_intr(void *); 78 1.1 shiba 79 1.23 cube CFATTACH_DECL_NEW(wdc_obio, sizeof(struct wdc_obio_softc), 80 1.3 thorpej wdc_obio_match, wdc_obio_attach, NULL, NULL); 81 1.1 shiba 82 1.18 chs static bus_space_tag_t wdc_obio_isr_tag; 83 1.18 chs static bus_space_handle_t wdc_obio_isr_hdl; 84 1.18 chs static struct ata_channel *ch_sc; 85 1.18 chs 86 1.1 shiba int 87 1.23 cube wdc_obio_match(device_t parent, cfdata_t match, void *aux) 88 1.1 shiba { 89 1.1 shiba struct obio_attach_args *oa = (struct obio_attach_args *) aux; 90 1.30 rin struct wdc_regs *wdr; 91 1.8 fredb int i, result = 0; 92 1.1 shiba 93 1.31 tsutsui wdr = kmem_alloc(sizeof(*wdr), KM_SLEEP); 94 1.30 rin 95 1.1 shiba switch (current_mac_model->machineid) { 96 1.1 shiba case MACH_MACPB150: 97 1.1 shiba case MACH_MACPB190: 98 1.1 shiba case MACH_MACPB190CS: 99 1.1 shiba case MACH_MACP580: 100 1.1 shiba case MACH_MACQ630: 101 1.30 rin wdr->cmd_iot = wdr->ctl_iot = oa->oa_tag; 102 1.1 shiba 103 1.30 rin if (bus_space_map(wdr->cmd_iot, IDEBase, WDC_OBIO_REG_NPORTS, 104 1.31 tsutsui 0, &wdr->cmd_baseioh)) 105 1.30 rin goto out; 106 1.1 shiba 107 1.30 rin mac68k_bus_space_handle_swapped(wdr->cmd_iot, 108 1.30 rin &wdr->cmd_baseioh); 109 1.1 shiba 110 1.8 fredb for (i = 0; i < WDC_NREG; i++) { 111 1.30 rin if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh, 112 1.31 tsutsui i * 4, 4, &wdr->cmd_iohs[i]) != 0) 113 1.32 tsutsui goto unmap; 114 1.8 fredb } 115 1.30 rin wdc_init_shadow_regs(wdr); 116 1.8 fredb 117 1.30 rin if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh, 118 1.31 tsutsui WDC_OBIO_AUXREG_OFFSET, WDC_OBIO_AUXREG_NPORTS, 119 1.31 tsutsui &wdr->ctl_ioh)) 120 1.32 tsutsui goto unmap; 121 1.1 shiba 122 1.30 rin result = wdcprobe(wdr); 123 1.1 shiba 124 1.32 tsutsui unmap: 125 1.30 rin bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, 126 1.30 rin WDC_OBIO_REG_NPORTS); 127 1.1 shiba } 128 1.31 tsutsui out: 129 1.31 tsutsui kmem_free(wdr, sizeof(*wdr)); 130 1.30 rin return result; 131 1.1 shiba } 132 1.1 shiba 133 1.1 shiba void 134 1.23 cube wdc_obio_attach(device_t parent, device_t self, void *aux) 135 1.1 shiba { 136 1.23 cube struct wdc_obio_softc *sc = device_private(self); 137 1.16 thorpej struct wdc_regs *wdr; 138 1.1 shiba struct obio_attach_args *oa = aux; 139 1.16 thorpej struct ata_channel *chp = &sc->sc_channel; 140 1.8 fredb int i; 141 1.1 shiba 142 1.23 cube sc->sc_wdcdev.sc_atac.atac_dev = self; 143 1.16 thorpej sc->sc_wdcdev.regs = wdr = &sc->sc_wdc_regs; 144 1.16 thorpej 145 1.1 shiba oa->oa_addr = IDEBase; 146 1.16 thorpej wdr->cmd_iot = wdr->ctl_iot = oa->oa_tag; 147 1.1 shiba 148 1.31 tsutsui if (bus_space_map(wdr->cmd_iot, oa->oa_addr, WDC_OBIO_REG_NPORTS, 0, 149 1.31 tsutsui &wdr->cmd_baseioh)) { 150 1.32 tsutsui aprint_error(": couldn't map registers\n"); 151 1.1 shiba return; 152 1.1 shiba } 153 1.1 shiba 154 1.31 tsutsui mac68k_bus_space_handle_swapped(wdr->cmd_iot, &wdr->cmd_baseioh); 155 1.8 fredb 156 1.8 fredb for (i = 0; i < WDC_NREG; i++) { 157 1.31 tsutsui if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh, 158 1.31 tsutsui i * 4, 4, &wdr->cmd_iohs[i]) != 0) { 159 1.32 tsutsui aprint_error( 160 1.32 tsutsui ": unable to subregion control register\n"); 161 1.32 tsutsui goto err; 162 1.8 fredb } 163 1.8 fredb } 164 1.1 shiba 165 1.31 tsutsui if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh, 166 1.31 tsutsui WDC_OBIO_AUXREG_OFFSET, WDC_OBIO_AUXREG_NPORTS, &wdr->ctl_ioh)) { 167 1.32 tsutsui aprint_error(": unable to subregion aux register\n"); 168 1.32 tsutsui goto err; 169 1.8 fredb } 170 1.1 shiba 171 1.31 tsutsui wdc_obio_isr_tag = oa->oa_tag; 172 1.1 shiba 173 1.31 tsutsui if (bus_space_map(wdc_obio_isr_tag, oa->oa_addr + WDC_OBIO_ISR_OFFSET, 174 1.31 tsutsui WDC_OBIO_ISR_NPORTS, 0, &wdc_obio_isr_hdl)) { 175 1.32 tsutsui aprint_error(": couldn't map intr status register\n"); 176 1.32 tsutsui goto err; 177 1.1 shiba } 178 1.1 shiba 179 1.1 shiba switch (current_mac_model->machineid) { 180 1.1 shiba case MACH_MACP580: 181 1.1 shiba case MACH_MACQ630: 182 1.1 shiba /* 183 1.1 shiba * Quadra/Performa IDE generates pseudo Nubus intr at slot F 184 1.1 shiba */ 185 1.23 cube aprint_normal(" (Quadra/Performa series IDE interface)"); 186 1.1 shiba 187 1.31 tsutsui add_nubus_intr(0xf, wdc_obio_intr, sc); 188 1.1 shiba 189 1.1 shiba break; 190 1.1 shiba case MACH_MACPB150: 191 1.1 shiba case MACH_MACPB190: 192 1.1 shiba case MACH_MACPB190CS: 193 1.1 shiba /* 194 1.1 shiba * PowerBook IDE generates pseudo NuBus intr at slot C 195 1.1 shiba */ 196 1.23 cube aprint_normal(" (PowerBook series IDE interface)"); 197 1.1 shiba 198 1.31 tsutsui add_nubus_intr(0xc, wdc_obio_intr, sc); 199 1.1 shiba 200 1.1 shiba break; 201 1.1 shiba } 202 1.1 shiba 203 1.1 shiba ch_sc = chp; 204 1.31 tsutsui if ((device_cfdata(self)->cf_flags & ATAC_CAP_NOIRQ) != 0) 205 1.17 thorpej sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_NOIRQ; 206 1.17 thorpej sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16; 207 1.17 thorpej sc->sc_wdcdev.sc_atac.atac_pio_cap = 0; 208 1.16 thorpej sc->sc_chanlist[0] = chp; 209 1.17 thorpej sc->sc_wdcdev.sc_atac.atac_channels = sc->sc_chanlist; 210 1.17 thorpej sc->sc_wdcdev.sc_atac.atac_nchannels = 1; 211 1.26 bouyer sc->sc_wdcdev.wdc_maxdrives = 2; 212 1.12 thorpej chp->ch_channel = 0; 213 1.17 thorpej chp->ch_atac = &sc->sc_wdcdev.sc_atac; 214 1.29 jdolecek 215 1.28 jdolecek wdc_init_shadow_regs(wdr); 216 1.1 shiba 217 1.23 cube aprint_normal("\n"); 218 1.1 shiba 219 1.7 bouyer wdcattach(chp); 220 1.32 tsutsui return; 221 1.32 tsutsui 222 1.32 tsutsui err: 223 1.32 tsutsui bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, WDC_OBIO_REG_NPORTS); 224 1.1 shiba } 225 1.18 chs 226 1.18 chs void 227 1.18 chs wdc_obio_intr(void *arg) 228 1.18 chs { 229 1.31 tsutsui uint8_t status; 230 1.18 chs 231 1.31 tsutsui status = bus_space_read_1(wdc_obio_isr_tag, wdc_obio_isr_hdl, 0); 232 1.31 tsutsui if ((status & 0x20) != 0) { 233 1.18 chs wdcintr(ch_sc); 234 1.31 tsutsui bus_space_write_1(wdc_obio_isr_tag, wdc_obio_isr_hdl, 0, 235 1.31 tsutsui status & ~0x20); 236 1.18 chs } 237 1.18 chs } 238