1 1.8 jakllsch /* $NetBSD: schide.c,v 1.8 2013/10/07 19:51:55 jakllsch Exp $ */ 2 1.1 jakllsch /* $OpenBSD: pciide.c,v 1.305 2009/11/01 01:50:15 dlg Exp $ */ 3 1.1 jakllsch 4 1.1 jakllsch /* 5 1.1 jakllsch * Copyright (c) 1999, 2000, 2001 Manuel Bouyer. 6 1.1 jakllsch * 7 1.1 jakllsch * Redistribution and use in source and binary forms, with or without 8 1.1 jakllsch * modification, are permitted provided that the following conditions 9 1.1 jakllsch * are met: 10 1.1 jakllsch * 1. Redistributions of source code must retain the above copyright 11 1.1 jakllsch * notice, this list of conditions and the following disclaimer. 12 1.1 jakllsch * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 jakllsch * notice, this list of conditions and the following disclaimer in the 14 1.1 jakllsch * documentation and/or other materials provided with the distribution. 15 1.1 jakllsch * 16 1.1 jakllsch * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 jakllsch * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 jakllsch * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 jakllsch * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.1 jakllsch * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 1.1 jakllsch * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 1.1 jakllsch * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 1.1 jakllsch * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 1.1 jakllsch * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 1.1 jakllsch * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 1.1 jakllsch * 27 1.1 jakllsch */ 28 1.1 jakllsch 29 1.1 jakllsch /* 30 1.1 jakllsch * Copyright (c) 1996, 1998 Christopher G. Demetriou. All rights reserved. 31 1.1 jakllsch * 32 1.1 jakllsch * Redistribution and use in source and binary forms, with or without 33 1.1 jakllsch * modification, are permitted provided that the following conditions 34 1.1 jakllsch * are met: 35 1.1 jakllsch * 1. Redistributions of source code must retain the above copyright 36 1.1 jakllsch * notice, this list of conditions and the following disclaimer. 37 1.1 jakllsch * 2. Redistributions in binary form must reproduce the above copyright 38 1.1 jakllsch * notice, this list of conditions and the following disclaimer in the 39 1.1 jakllsch * documentation and/or other materials provided with the distribution. 40 1.1 jakllsch * 3. All advertising materials mentioning features or use of this software 41 1.1 jakllsch * must display the following acknowledgement: 42 1.1 jakllsch * This product includes software developed by Christopher G. Demetriou 43 1.1 jakllsch * for the NetBSD Project. 44 1.1 jakllsch * 4. The name of the author may not be used to endorse or promote products 45 1.1 jakllsch * derived from this software without specific prior written permission 46 1.1 jakllsch * 47 1.1 jakllsch * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 48 1.1 jakllsch * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 49 1.1 jakllsch * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 50 1.1 jakllsch * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 51 1.1 jakllsch * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 52 1.1 jakllsch * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 53 1.1 jakllsch * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 54 1.1 jakllsch * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 55 1.1 jakllsch * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 56 1.1 jakllsch * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 57 1.1 jakllsch */ 58 1.1 jakllsch 59 1.1 jakllsch #include <sys/cdefs.h> 60 1.8 jakllsch __KERNEL_RCSID(0, "$NetBSD: schide.c,v 1.8 2013/10/07 19:51:55 jakllsch Exp $"); 61 1.1 jakllsch 62 1.1 jakllsch #include <sys/param.h> 63 1.1 jakllsch #include <sys/systm.h> 64 1.1 jakllsch 65 1.1 jakllsch #include <dev/pci/pcivar.h> 66 1.1 jakllsch #include <dev/pci/pcidevs.h> 67 1.1 jakllsch #include <dev/pci/pciidereg.h> 68 1.1 jakllsch #include <dev/pci/pciidevar.h> 69 1.1 jakllsch #include <dev/pci/pciide_sch_reg.h> 70 1.1 jakllsch 71 1.2 dyoung static void sch_chip_map(struct pciide_softc*, const struct pci_attach_args*); 72 1.1 jakllsch static void sch_setup_channel(struct ata_channel*); 73 1.1 jakllsch static int schide_match(device_t, cfdata_t, void *); 74 1.1 jakllsch static void schide_attach(device_t, device_t, void *); 75 1.1 jakllsch 76 1.1 jakllsch CFATTACH_DECL_NEW(schide, sizeof(struct pciide_softc), 77 1.8 jakllsch schide_match, schide_attach, pciide_detach, NULL); 78 1.1 jakllsch 79 1.1 jakllsch static const struct pciide_product_desc pciide_sch_products[] = { 80 1.1 jakllsch { PCI_PRODUCT_INTEL_SCH_IDE, 81 1.1 jakllsch 0, 82 1.1 jakllsch "Intel SCH IDE Controller", 83 1.1 jakllsch sch_chip_map, 84 1.1 jakllsch }, 85 1.1 jakllsch { 0, 86 1.1 jakllsch 0, 87 1.1 jakllsch NULL, 88 1.1 jakllsch NULL 89 1.1 jakllsch } 90 1.1 jakllsch }; 91 1.1 jakllsch 92 1.1 jakllsch static int 93 1.1 jakllsch schide_match(device_t parent, cfdata_t match, void *aux) 94 1.1 jakllsch { 95 1.1 jakllsch struct pci_attach_args *pa = aux; 96 1.1 jakllsch 97 1.1 jakllsch if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_INTEL) { 98 1.1 jakllsch if (pciide_lookup_product(pa->pa_id, pciide_sch_products)) 99 1.1 jakllsch return (2); 100 1.1 jakllsch } 101 1.1 jakllsch return (0); 102 1.1 jakllsch } 103 1.1 jakllsch 104 1.1 jakllsch static void 105 1.1 jakllsch schide_attach(device_t parent, device_t self, void *aux) 106 1.1 jakllsch { 107 1.1 jakllsch struct pci_attach_args *pa = aux; 108 1.1 jakllsch struct pciide_softc *sc = device_private(self); 109 1.1 jakllsch 110 1.1 jakllsch sc->sc_wdcdev.sc_atac.atac_dev = self; 111 1.1 jakllsch 112 1.1 jakllsch pciide_common_attach(sc, pa, 113 1.1 jakllsch pciide_lookup_product(pa->pa_id, pciide_sch_products)); 114 1.1 jakllsch 115 1.1 jakllsch } 116 1.1 jakllsch 117 1.1 jakllsch static void 118 1.2 dyoung sch_chip_map(struct pciide_softc *sc, const struct pci_attach_args *pa) 119 1.1 jakllsch { 120 1.1 jakllsch struct pciide_channel *cp; 121 1.1 jakllsch pcireg_t interface; 122 1.1 jakllsch int channel; 123 1.1 jakllsch 124 1.1 jakllsch if (pciide_chipen(sc, pa) == 0) 125 1.1 jakllsch return; 126 1.1 jakllsch 127 1.1 jakllsch aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev, 128 1.1 jakllsch "bus-master DMA support present"); 129 1.1 jakllsch pciide_mapreg_dma(sc, pa); 130 1.1 jakllsch aprint_verbose("\n"); 131 1.1 jakllsch sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16 | ATAC_CAP_DATA32; 132 1.1 jakllsch 133 1.1 jakllsch if (sc->sc_dma_ok) { 134 1.1 jakllsch sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA; 135 1.1 jakllsch sc->sc_wdcdev.irqack = pciide_irqack; 136 1.1 jakllsch } 137 1.1 jakllsch sc->sc_wdcdev.sc_atac.atac_pio_cap = 4; 138 1.1 jakllsch sc->sc_wdcdev.sc_atac.atac_dma_cap = 2; 139 1.1 jakllsch sc->sc_wdcdev.sc_atac.atac_udma_cap = 5; 140 1.1 jakllsch sc->sc_wdcdev.sc_atac.atac_set_modes = sch_setup_channel; 141 1.1 jakllsch sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray; 142 1.1 jakllsch sc->sc_wdcdev.sc_atac.atac_nchannels = 1; 143 1.7 bouyer sc->sc_wdcdev.wdc_maxdrives = 2; 144 1.1 jakllsch 145 1.1 jakllsch 146 1.1 jakllsch ATADEBUG_PRINT(("sch_setup_chip: old d0tim=0x%x, d1tim=0x%x\n", 147 1.1 jakllsch pci_conf_read(sc->sc_pc, sc->sc_tag, SCH_D0TIM), 148 1.1 jakllsch pci_conf_read(sc->sc_pc, sc->sc_tag, SCH_D1TIM)), 149 1.1 jakllsch DEBUG_PROBE); 150 1.1 jakllsch 151 1.1 jakllsch interface = PCI_INTERFACE(pa->pa_class); 152 1.1 jakllsch 153 1.1 jakllsch wdc_allocate_regs(&sc->sc_wdcdev); 154 1.1 jakllsch 155 1.1 jakllsch for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels; 156 1.1 jakllsch channel++) { 157 1.1 jakllsch cp = &sc->pciide_channels[channel]; 158 1.1 jakllsch if (pciide_chansetup(sc, channel, interface) == 0) 159 1.1 jakllsch continue; 160 1.1 jakllsch pciide_mapchan(pa, cp, interface, pciide_pci_intr); 161 1.1 jakllsch } 162 1.1 jakllsch 163 1.1 jakllsch ATADEBUG_PRINT(("sch_setup_chip: d0tim=0x%x, d1tim=0x%x\n", 164 1.1 jakllsch pci_conf_read(sc->sc_pc, sc->sc_tag, SCH_D0TIM), 165 1.1 jakllsch pci_conf_read(sc->sc_pc, sc->sc_tag, SCH_D1TIM)), 166 1.1 jakllsch DEBUG_PROBE); 167 1.1 jakllsch } 168 1.1 jakllsch 169 1.1 jakllsch static void 170 1.1 jakllsch sch_setup_channel(struct ata_channel *chp) 171 1.1 jakllsch { 172 1.1 jakllsch struct ata_drive_datas *drvp; 173 1.1 jakllsch u_int32_t tim, timaddr, idedma_ctl; 174 1.1 jakllsch struct atac_softc *atac = chp->ch_atac; 175 1.1 jakllsch struct pciide_channel *cp = CHAN_TO_PCHAN(chp); 176 1.1 jakllsch struct pciide_softc *sc = CHAN_TO_PCIIDE(chp); 177 1.1 jakllsch int drive, s; 178 1.1 jakllsch 179 1.1 jakllsch idedma_ctl = 0; 180 1.1 jakllsch 181 1.1 jakllsch /* setup DMA if needed */ 182 1.1 jakllsch pciide_channel_dma_setup(cp); 183 1.1 jakllsch 184 1.1 jakllsch /* Per drive settings */ 185 1.1 jakllsch for (drive = 0; drive < 2; drive++) { 186 1.1 jakllsch drvp = &chp->ch_drive[drive]; 187 1.1 jakllsch /* If no drive, skip */ 188 1.7 bouyer if (drvp->drive_type == ATA_DRIVET_NONE) 189 1.1 jakllsch continue; 190 1.1 jakllsch 191 1.1 jakllsch timaddr = (drive == 0) ? SCH_D0TIM : SCH_D1TIM; 192 1.1 jakllsch tim = pci_conf_read(sc->sc_pc, sc->sc_tag, timaddr); 193 1.1 jakllsch tim &= ~SCH_TIM_MASK; 194 1.1 jakllsch 195 1.7 bouyer if (((drvp->drive_flags & ATA_DRIVE_DMA) == 0 && 196 1.7 bouyer (drvp->drive_flags & ATA_DRIVE_UDMA) == 0)) 197 1.1 jakllsch goto pio; 198 1.1 jakllsch 199 1.1 jakllsch /* add timing values, setup DMA if needed */ 200 1.1 jakllsch if ((atac->atac_cap & ATAC_CAP_UDMA) && 201 1.7 bouyer (drvp->drive_flags & ATA_DRIVE_UDMA)) { 202 1.1 jakllsch /* use Ultra/DMA */ 203 1.1 jakllsch tim |= (drvp->UDMA_mode << 16) | SCH_TIM_SYNCDMA; 204 1.1 jakllsch } else { 205 1.1 jakllsch /* use Multiword DMA */ 206 1.1 jakllsch s = splbio(); 207 1.7 bouyer drvp->drive_flags &= ~ATA_DRIVE_UDMA; 208 1.1 jakllsch splx(s); 209 1.1 jakllsch tim &= ~SCH_TIM_SYNCDMA; 210 1.1 jakllsch } 211 1.1 jakllsch idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive); 212 1.1 jakllsch 213 1.1 jakllsch pio: /* use PIO mode */ 214 1.1 jakllsch 215 1.1 jakllsch tim |= (drvp->DMA_mode << 8) | (drvp->PIO_mode); 216 1.1 jakllsch pci_conf_write(sc->sc_pc, sc->sc_tag, timaddr, tim); 217 1.1 jakllsch } 218 1.1 jakllsch if (idedma_ctl != 0) { 219 1.1 jakllsch /* Add software bits in status register */ 220 1.1 jakllsch bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0, 221 1.1 jakllsch idedma_ctl); 222 1.1 jakllsch } 223 1.1 jakllsch } 224