1 1.11 jakllsch /* $NetBSD: toshide.c,v 1.11 2013/10/07 19:53:05 jakllsch Exp $ */ 2 1.1 christos 3 1.1 christos /*- 4 1.1 christos * Copyright (c) 2009 The NetBSD Foundation, Inc. 5 1.1 christos * All rights reserved. 6 1.1 christos * 7 1.1 christos * Redistribution and use in source and binary forms, with or without 8 1.1 christos * modification, are permitted provided that the following conditions 9 1.1 christos * are met: 10 1.1 christos * 1. Redistributions of source code must retain the above copyright 11 1.1 christos * notice, this list of conditions and the following disclaimer. 12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 christos * notice, this list of conditions and the following disclaimer in the 14 1.1 christos * documentation and/or other materials provided with the distribution. 15 1.1 christos * 16 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 christos * POSSIBILITY OF SUCH DAMAGE. 27 1.1 christos */ 28 1.1 christos 29 1.1 christos #include <sys/cdefs.h> 30 1.11 jakllsch __KERNEL_RCSID(0, "$NetBSD: toshide.c,v 1.11 2013/10/07 19:53:05 jakllsch Exp $"); 31 1.1 christos 32 1.1 christos #include <sys/param.h> 33 1.1 christos #include <sys/systm.h> 34 1.1 christos 35 1.1 christos #include <dev/pci/pcivar.h> 36 1.1 christos #include <dev/pci/pcidevs.h> 37 1.1 christos #include <dev/pci/pciidereg.h> 38 1.1 christos #include <dev/pci/pciidevar.h> 39 1.1 christos #include <dev/pci/pciide_piccolo_reg.h> 40 1.1 christos 41 1.4 dyoung static void piccolo_chip_map(struct pciide_softc *, 42 1.4 dyoung const struct pci_attach_args *); 43 1.1 christos static void piccolo_setup_channel(struct ata_channel *); 44 1.1 christos 45 1.1 christos static int piccolo_match(device_t, cfdata_t, void *); 46 1.1 christos static void piccolo_attach(device_t, device_t, void *); 47 1.1 christos 48 1.1 christos CFATTACH_DECL_NEW(toshide, sizeof(struct pciide_softc), 49 1.10 jakllsch piccolo_match, piccolo_attach, pciide_detach, NULL); 50 1.1 christos 51 1.1 christos static const struct pciide_product_desc pciide_toshiba2_products[] = { 52 1.11 jakllsch { 53 1.1 christos PCI_PRODUCT_TOSHIBA2_PICCOLO, 54 1.1 christos 0, 55 1.1 christos "Toshiba Piccolo IDE controller", 56 1.1 christos piccolo_chip_map, 57 1.1 christos }, 58 1.1 christos { 59 1.1 christos PCI_PRODUCT_TOSHIBA2_PICCOLO2, 60 1.1 christos 0, 61 1.1 christos "Toshiba Piccolo 2 IDE controller", 62 1.1 christos piccolo_chip_map, 63 1.1 christos }, 64 1.1 christos { 65 1.1 christos PCI_PRODUCT_TOSHIBA2_PICCOLO3, 66 1.1 christos 0, 67 1.1 christos "Toshiba Piccolo 3 IDE controller", 68 1.1 christos piccolo_chip_map, 69 1.1 christos }, 70 1.1 christos { 71 1.1 christos PCI_PRODUCT_TOSHIBA2_PICCOLO5, 72 1.1 christos 0, 73 1.1 christos "Toshiba Piccolo 5 IDE controller", 74 1.1 christos piccolo_chip_map, 75 1.1 christos }, 76 1.1 christos { 77 1.1 christos 0, 78 1.1 christos 0, 79 1.1 christos NULL, 80 1.1 christos NULL, 81 1.1 christos } 82 1.1 christos }; 83 1.1 christos 84 1.1 christos static int 85 1.1 christos piccolo_match(device_t parent, cfdata_t match, void *aux) 86 1.1 christos { 87 1.1 christos struct pci_attach_args *pa = aux; 88 1.1 christos 89 1.1 christos if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TOSHIBA2) { 90 1.1 christos if (pciide_lookup_product(pa->pa_id, pciide_toshiba2_products)) 91 1.1 christos return 2; 92 1.1 christos } 93 1.1 christos return 0; 94 1.1 christos } 95 1.1 christos 96 1.1 christos static void 97 1.1 christos piccolo_attach(device_t parent, device_t self, void *aux) 98 1.1 christos { 99 1.1 christos struct pci_attach_args *pa = aux; 100 1.1 christos struct pciide_softc *sc = device_private(self); 101 1.1 christos const struct pciide_product_desc *pp; 102 1.1 christos 103 1.1 christos sc->sc_wdcdev.sc_atac.atac_dev = self; 104 1.1 christos 105 1.1 christos if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TOSHIBA2) 106 1.1 christos pp = pciide_lookup_product(pa->pa_id, pciide_toshiba2_products); 107 1.1 christos else 108 1.1 christos pp = NULL; 109 1.1 christos if (pp == NULL) 110 1.1 christos panic("toshide_attach"); 111 1.1 christos pciide_common_attach(sc, pa, pp); 112 1.1 christos } 113 1.1 christos 114 1.1 christos static void 115 1.4 dyoung piccolo_chip_map(struct pciide_softc *sc, const struct pci_attach_args *pa) 116 1.1 christos { 117 1.1 christos struct pciide_channel *cp; 118 1.1 christos pcireg_t interface; 119 1.1 christos int channel; 120 1.1 christos 121 1.1 christos if (pciide_chipen(sc, pa) == 0) 122 1.1 christos return; 123 1.1 christos 124 1.1 christos aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev, 125 1.1 christos "bus-master DMA support present"); 126 1.1 christos 127 1.1 christos pciide_mapreg_dma(sc, pa); 128 1.1 christos aprint_verbose("\n"); 129 1.1 christos 130 1.1 christos sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA32 | ATAC_CAP_DATA16; 131 1.1 christos sc->sc_wdcdev.sc_atac.atac_pio_cap = 5; 132 1.1 christos 133 1.1 christos if (sc->sc_dma_ok) { 134 1.1 christos sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA; 135 1.1 christos sc->sc_wdcdev.irqack = pciide_irqack; 136 1.1 christos sc->sc_wdcdev.sc_atac.atac_dma_cap = 3; 137 1.1 christos sc->sc_wdcdev.sc_atac.atac_udma_cap = 2; 138 1.1 christos } 139 1.1 christos 140 1.1 christos sc->sc_wdcdev.sc_atac.atac_set_modes = piccolo_setup_channel; 141 1.1 christos 142 1.1 christos sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray; 143 1.1 christos sc->sc_wdcdev.sc_atac.atac_nchannels = 1; 144 1.9 bouyer sc->sc_wdcdev.wdc_maxdrives = 2; 145 1.11 jakllsch /* 146 1.1 christos * XXX one for now. We'll figure out how to talk to the second channel 147 1.1 christos * later, hopefully! Second interface config is via the 148 1.1 christos * "alternate PCI Configuration Space" whatever that is! 149 1.1 christos */ 150 1.1 christos 151 1.1 christos interface = PCI_INTERFACE(pa->pa_class); 152 1.1 christos 153 1.1 christos wdc_allocate_regs(&sc->sc_wdcdev); 154 1.1 christos 155 1.1 christos for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels; 156 1.1 christos channel++) { 157 1.1 christos cp = &sc->pciide_channels[channel]; 158 1.1 christos if (pciide_chansetup(sc, channel, interface) == 0) 159 1.1 christos continue; 160 1.1 christos 161 1.3 jakllsch pciide_mapchan(pa, cp, interface, pciide_pci_intr); 162 1.1 christos } 163 1.1 christos } 164 1.1 christos 165 1.1 christos static void 166 1.1 christos piccolo_setup_channel(struct ata_channel *chp) 167 1.1 christos { 168 1.1 christos struct ata_drive_datas *drvp; 169 1.1 christos struct pciide_channel *cp = CHAN_TO_PCHAN(chp); 170 1.1 christos struct pciide_softc *sc = CHAN_TO_PCIIDE(chp); 171 1.1 christos u_int32_t idedma_ctl; 172 1.1 christos int drive, s; 173 1.2 christos pcireg_t pxdx; 174 1.1 christos #ifdef TOSHIDE_DEBUG 175 1.1 christos pcireg_t pxdx_prime; 176 1.1 christos #endif 177 1.1 christos 178 1.1 christos idedma_ctl = 0; 179 1.1 christos 180 1.1 christos /* Set up DMA if needed. */ 181 1.1 christos pciide_channel_dma_setup(cp); 182 1.1 christos 183 1.1 christos for (drive = 0; drive < 2; drive++) { 184 1.1 christos 185 1.1 christos drvp = &chp->ch_drive[drive]; 186 1.1 christos /* If no drive, skip */ 187 1.9 bouyer if (drvp->drive_type == ATA_DRIVET_NONE) 188 1.1 christos continue; 189 1.1 christos 190 1.9 bouyer if (drvp->drive_flags & ATA_DRIVE_UDMA) { 191 1.1 christos /* use Ultra/DMA */ 192 1.1 christos s = splbio(); 193 1.9 bouyer drvp->drive_flags &= ~ATA_DRIVE_DMA; 194 1.1 christos splx(s); 195 1.1 christos 196 1.1 christos /* 197 1.1 christos * Use UDMA - we can go up to mode 2 so no need to 198 1.1 christos * check anything since nearly all drives with UDMA 199 1.11 jakllsch * are mode 2 or faster 200 1.1 christos */ 201 1.1 christos pxdx = pci_conf_read(sc->sc_pc, sc->sc_tag, 202 1.1 christos PICCOLO_DMA_TIMING); 203 1.1 christos pxdx &= PICCOLO_UDMA_MASK; 204 1.1 christos pxdx |= piccolo_udma_times[2]; 205 1.1 christos pci_conf_write(sc->sc_pc, sc->sc_tag, 206 1.1 christos PICCOLO_DMA_TIMING, pxdx); 207 1.1 christos #ifdef TOSHIDE_DEBUG 208 1.1 christos /* XXX sanity check */ 209 1.1 christos pxdx_prime = pci_conf_read(sc->sc_pc, sc->sc_tag, 210 1.1 christos PICCOLO_DMA_TIMING); 211 1.1 christos aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev, 212 1.1 christos "UDMA want %x, set %x, got %x\n", 213 1.1 christos piccolo_udma_times[2], pxdx, pxdx_prime); 214 1.1 christos #endif 215 1.1 christos idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive); 216 1.1 christos 217 1.1 christos } 218 1.9 bouyer else if (drvp->drive_flags & ATA_DRIVE_DMA) { 219 1.1 christos /* 220 1.1 christos * Use Multiword DMA 221 1.1 christos */ 222 1.1 christos if (drvp->PIO_mode > (drvp->DMA_mode + 2)) 223 1.1 christos drvp->PIO_mode = drvp->DMA_mode + 2; 224 1.1 christos if (drvp->DMA_mode + 2 > (drvp->PIO_mode)) 225 1.1 christos drvp->DMA_mode = (drvp->PIO_mode > 2) ? 226 1.1 christos drvp->PIO_mode - 2 : 0; 227 1.1 christos 228 1.1 christos pxdx = pci_conf_read(sc->sc_pc, sc->sc_tag, 229 1.1 christos PICCOLO_DMA_TIMING); 230 1.1 christos pxdx &= PICCOLO_DMA_MASK; 231 1.1 christos pxdx |= piccolo_mw_dma_times[drvp->DMA_mode]; 232 1.1 christos pci_conf_write(sc->sc_pc, sc->sc_tag, 233 1.1 christos PICCOLO_DMA_TIMING, pxdx); 234 1.1 christos #ifdef TOSHIDE_DEBUG 235 1.1 christos /* XXX sanity check */ 236 1.1 christos pxdx_prime = pci_conf_read(sc->sc_pc, sc->sc_tag, 237 1.1 christos PICCOLO_DMA_TIMING); 238 1.1 christos aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev, 239 1.1 christos "DMA %d want %x, set %x, got %x\n", 240 1.1 christos drvp->DMA_mode, 241 1.1 christos piccolo_mw_dma_times[drvp->DMA_mode], pxdx, 242 1.1 christos pxdx_prime); 243 1.1 christos #endif 244 1.1 christos idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive); 245 1.1 christos 246 1.1 christos } 247 1.1 christos else { 248 1.1 christos pxdx = pci_conf_read(sc->sc_pc, sc->sc_tag, 249 1.1 christos PICCOLO_PIO_TIMING); 250 1.1 christos pxdx &= PICCOLO_PIO_MASK; 251 1.1 christos pxdx |= piccolo_pio_times[drvp->PIO_mode]; 252 1.1 christos pci_conf_write(sc->sc_pc, sc->sc_tag, 253 1.1 christos PICCOLO_PIO_TIMING, pxdx); 254 1.1 christos #ifdef TOSHIDE_DEBUG 255 1.1 christos /* XXX sanity check */ 256 1.1 christos pxdx_prime = pci_conf_read(sc->sc_pc, sc->sc_tag, 257 1.1 christos PICCOLO_PIO_TIMING); 258 1.1 christos aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev, 259 1.1 christos "PIO %d want %x, set %x, got %x\n", drvp->PIO_mode, 260 1.1 christos piccolo_pio_times[drvp->PIO_mode], pxdx, 261 1.1 christos pxdx_prime); 262 1.1 christos #endif 263 1.1 christos } 264 1.1 christos 265 1.1 christos } 266 1.1 christos if (idedma_ctl != 0) { 267 1.1 christos /* Add software bits in status register */ 268 1.1 christos bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0, 269 1.1 christos idedma_ctl); 270 1.1 christos } 271 1.1 christos } 272