1 1.25 msaitoh /* $NetBSD: ixpide.c,v 1.25 2016/07/14 04:19:27 msaitoh Exp $ */ 2 1.1 cube 3 1.1 cube /* 4 1.1 cube * Copyright (c) 2004 The NetBSD Foundation. 5 1.1 cube * All rights reserved. 6 1.1 cube * 7 1.1 cube * Redistribution and use in source and binary forms, with or without 8 1.1 cube * modification, are permitted provided that the following conditions 9 1.1 cube * are met: 10 1.1 cube * 1. Redistributions of source code must retain the above copyright 11 1.1 cube * notice, this list of conditions and the following disclaimer. 12 1.1 cube * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 cube * notice, this list of conditions and the following disclaimer in the 14 1.1 cube * documentation and/or other materials provided with the distribution. 15 1.2 perry * 16 1.1 cube * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 cube * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 cube * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 cube * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 cube * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 cube * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 cube * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 cube * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 cube * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 cube * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 cube * POSSIBILITY OF SUCH DAMAGE. 27 1.1 cube */ 28 1.1 cube 29 1.1 cube #include <sys/cdefs.h> 30 1.25 msaitoh __KERNEL_RCSID(0, "$NetBSD: ixpide.c,v 1.25 2016/07/14 04:19:27 msaitoh Exp $"); 31 1.1 cube 32 1.1 cube #include <sys/param.h> 33 1.1 cube #include <sys/systm.h> 34 1.1 cube 35 1.1 cube #include <dev/pci/pcivar.h> 36 1.1 cube #include <dev/pci/pcidevs.h> 37 1.1 cube #include <dev/pci/pciidereg.h> 38 1.1 cube #include <dev/pci/pciidevar.h> 39 1.1 cube #include <dev/pci/pciide_ixp_reg.h> 40 1.1 cube 41 1.16 dyoung static bool ixpide_resume(device_t, const pmf_qual_t *); 42 1.16 dyoung static bool ixpide_suspend(device_t, const pmf_qual_t *); 43 1.11 cube static int ixpide_match(device_t, cfdata_t, void *); 44 1.11 cube static void ixpide_attach(device_t, device_t, void *); 45 1.1 cube 46 1.25 msaitoh static void ixp_chip_map(struct pciide_softc *, 47 1.25 msaitoh const struct pci_attach_args *); 48 1.1 cube static void ixp_setup_channel(struct ata_channel *); 49 1.1 cube 50 1.11 cube CFATTACH_DECL_NEW(ixpide, sizeof(struct pciide_softc), 51 1.18 jakllsch ixpide_match, ixpide_attach, pciide_detach, NULL); 52 1.1 cube 53 1.3 christos static const char ixpdesc[] = "ATI Technologies IXP IDE Controller"; 54 1.3 christos 55 1.1 cube static const struct pciide_product_desc pciide_ixpide_products[] = { 56 1.3 christos { PCI_PRODUCT_ATI_IXP_IDE_200, 0, ixpdesc, ixp_chip_map }, 57 1.3 christos { PCI_PRODUCT_ATI_IXP_IDE_300, 0, ixpdesc, ixp_chip_map }, 58 1.3 christos { PCI_PRODUCT_ATI_IXP_IDE_400, 0, ixpdesc, ixp_chip_map }, 59 1.6 xtraeme { PCI_PRODUCT_ATI_IXP_IDE_600, 0, ixpdesc, ixp_chip_map }, 60 1.4 augustss { PCI_PRODUCT_ATI_SB400_SATA_1, 0, ixpdesc, ixp_chip_map }, 61 1.4 augustss { PCI_PRODUCT_ATI_SB400_SATA_2, 0, ixpdesc, ixp_chip_map }, 62 1.6 xtraeme { PCI_PRODUCT_ATI_SB600_SATA_1, 0, ixpdesc, ixp_chip_map }, 63 1.6 xtraeme { PCI_PRODUCT_ATI_SB600_SATA_2, 0, ixpdesc, ixp_chip_map }, 64 1.13 rmind { PCI_PRODUCT_ATI_SB700_SATA_IDE, 0, ixpdesc, ixp_chip_map }, 65 1.13 rmind { PCI_PRODUCT_ATI_SB700_IDE, 0, ixpdesc, ixp_chip_map }, 66 1.3 christos { 0, 0, NULL, NULL } 67 1.1 cube }; 68 1.1 cube 69 1.1 cube static int 70 1.11 cube ixpide_match(device_t parent, cfdata_t cfdata, void *aux) 71 1.1 cube { 72 1.1 cube struct pci_attach_args *pa = (struct pci_attach_args *)aux; 73 1.1 cube 74 1.1 cube if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ATI && 75 1.1 cube PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE && 76 1.1 cube PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE && 77 1.1 cube pciide_lookup_product(pa->pa_id, pciide_ixpide_products)) 78 1.1 cube return (2); 79 1.1 cube return (0); 80 1.1 cube } 81 1.1 cube 82 1.1 cube static void 83 1.11 cube ixpide_attach(device_t parent, device_t self, void *aux) 84 1.1 cube { 85 1.1 cube struct pci_attach_args *pa = aux; 86 1.11 cube struct pciide_softc *sc = device_private(self); 87 1.11 cube 88 1.11 cube sc->sc_wdcdev.sc_atac.atac_dev = self; 89 1.1 cube 90 1.1 cube pciide_common_attach(sc, pa, 91 1.1 cube pciide_lookup_product(pa->pa_id, pciide_ixpide_products)); 92 1.14 reinoud 93 1.14 reinoud if (!pmf_device_register(self, ixpide_suspend, ixpide_resume)) 94 1.14 reinoud aprint_error_dev(self, "couldn't establish power handler\n"); 95 1.1 cube } 96 1.1 cube 97 1.1 cube static void 98 1.19 dyoung ixp_chip_map(struct pciide_softc *sc, const struct pci_attach_args *pa) 99 1.1 cube { 100 1.1 cube struct pciide_channel *cp; 101 1.1 cube int channel; 102 1.1 cube pcireg_t interface; 103 1.1 cube 104 1.1 cube if (pciide_chipen(sc, pa) == 0) 105 1.1 cube return; 106 1.1 cube 107 1.11 cube aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev, 108 1.11 cube "bus-master DMA support present"); 109 1.1 cube pciide_mapreg_dma(sc, pa); 110 1.9 ad aprint_verbose("\n"); 111 1.1 cube 112 1.1 cube sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16 | ATAC_CAP_DATA32; 113 1.1 cube if (sc->sc_dma_ok) { 114 1.1 cube sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA; 115 1.1 cube sc->sc_wdcdev.irqack = pciide_irqack; 116 1.1 cube } 117 1.1 cube 118 1.1 cube sc->sc_wdcdev.sc_atac.atac_pio_cap = 4; 119 1.1 cube sc->sc_wdcdev.sc_atac.atac_dma_cap = 2; 120 1.1 cube sc->sc_wdcdev.sc_atac.atac_udma_cap = 6; 121 1.1 cube sc->sc_wdcdev.sc_atac.atac_set_modes = ixp_setup_channel; 122 1.1 cube sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray; 123 1.1 cube sc->sc_wdcdev.sc_atac.atac_nchannels = PCIIDE_NUM_CHANNELS; 124 1.24 bouyer sc->sc_wdcdev.wdc_maxdrives = 2; 125 1.1 cube 126 1.1 cube interface = PCI_INTERFACE(pa->pa_class); 127 1.1 cube 128 1.1 cube wdc_allocate_regs(&sc->sc_wdcdev); 129 1.1 cube 130 1.1 cube for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels; 131 1.1 cube channel++) { 132 1.1 cube cp = &sc->pciide_channels[channel]; 133 1.1 cube if (pciide_chansetup(sc, channel, interface) == 0) 134 1.1 cube continue; 135 1.17 jakllsch pciide_mapchan(pa, cp, interface, pciide_pci_intr); 136 1.1 cube } 137 1.1 cube } 138 1.1 cube 139 1.1 cube /* Values from linux driver */ 140 1.1 cube static const uint8_t ixp_pio_timings[] = { 141 1.1 cube 0x5d, 0x47, 0x34, 0x22, 0x20 142 1.1 cube }; 143 1.1 cube 144 1.1 cube static const uint8_t ixp_mdma_timings[] = { 145 1.1 cube 0x77, 0x21, 0x20 146 1.1 cube }; 147 1.1 cube 148 1.14 reinoud static bool 149 1.16 dyoung ixpide_resume(device_t dv, const pmf_qual_t *qual) 150 1.14 reinoud { 151 1.14 reinoud struct pciide_softc *sc = device_private(dv); 152 1.14 reinoud 153 1.14 reinoud pci_conf_write(sc->sc_pc, sc->sc_tag, IXP_MDMA_TIMING, 154 1.14 reinoud sc->sc_pm_reg[0]); 155 1.14 reinoud pci_conf_write(sc->sc_pc, sc->sc_tag, IXP_PIO_TIMING, 156 1.14 reinoud sc->sc_pm_reg[1]); 157 1.14 reinoud 158 1.14 reinoud return true; 159 1.14 reinoud } 160 1.14 reinoud 161 1.14 reinoud static bool 162 1.16 dyoung ixpide_suspend(device_t dv, const pmf_qual_t *qual) 163 1.14 reinoud { 164 1.14 reinoud struct pciide_softc *sc = device_private(dv); 165 1.14 reinoud 166 1.14 reinoud sc->sc_pm_reg[0] = pci_conf_read(sc->sc_pc, sc->sc_tag, 167 1.14 reinoud IXP_MDMA_TIMING); 168 1.14 reinoud sc->sc_pm_reg[1] = pci_conf_read(sc->sc_pc, sc->sc_tag, 169 1.14 reinoud IXP_PIO_TIMING); 170 1.14 reinoud 171 1.14 reinoud return true; 172 1.14 reinoud } 173 1.14 reinoud 174 1.1 cube static void 175 1.1 cube ixp_setup_channel(struct ata_channel *chp) 176 1.1 cube { 177 1.1 cube int drive, s; 178 1.1 cube struct pciide_channel *cp = CHAN_TO_PCHAN(chp); 179 1.1 cube pcireg_t udma, mdma_timing, pio, pio_timing; 180 1.1 cube struct ata_drive_datas *drvp; 181 1.1 cube struct pciide_softc *sc = CHAN_TO_PCIIDE(chp); 182 1.1 cube 183 1.1 cube pio_timing = pci_conf_read(sc->sc_pc, sc->sc_tag, IXP_PIO_TIMING); 184 1.1 cube pio = pci_conf_read(sc->sc_pc, sc->sc_tag, IXP_PIO_CTL); 185 1.1 cube mdma_timing = pci_conf_read(sc->sc_pc, sc->sc_tag, IXP_MDMA_TIMING); 186 1.1 cube udma = pci_conf_read(sc->sc_pc, sc->sc_tag, IXP_UDMA_CTL); 187 1.1 cube 188 1.1 cube pciide_channel_dma_setup(cp); 189 1.1 cube 190 1.1 cube for (drive = 0; drive < 2; drive++) { 191 1.1 cube drvp = &chp->ch_drive[drive]; 192 1.24 bouyer if (drvp->drive_type == ATA_DRIVET_NONE) 193 1.1 cube continue; 194 1.24 bouyer if (drvp->drive_flags & ATA_DRIVE_UDMA) { 195 1.1 cube s = splbio(); 196 1.24 bouyer drvp->drive_flags &= ~ATA_DRIVE_DMA; 197 1.1 cube splx(s); 198 1.1 cube IXP_UDMA_ENABLE(udma, chp->ch_channel, drive); 199 1.1 cube IXP_SET_MODE(udma, chp->ch_channel, drive, drvp->UDMA_mode); 200 1.24 bouyer } else if (drvp->drive_flags & ATA_DRIVE_DMA) { 201 1.1 cube IXP_UDMA_DISABLE(udma, chp->ch_channel, drive); 202 1.1 cube IXP_SET_TIMING(mdma_timing, chp->ch_channel, drive, 203 1.1 cube ixp_mdma_timings[drvp->DMA_mode]); 204 1.1 cube } else 205 1.1 cube IXP_UDMA_DISABLE(udma, chp->ch_channel, drive); 206 1.1 cube 207 1.1 cube /* 208 1.1 cube * Set PIO mode and timings 209 1.1 cube * Linux driver avoids PIO mode 1, let's do it too. 210 1.1 cube */ 211 1.1 cube if (drvp->PIO_mode == 1) 212 1.1 cube drvp->PIO_mode = 0; 213 1.1 cube 214 1.1 cube IXP_SET_MODE(pio, chp->ch_channel, drive, drvp->PIO_mode); 215 1.1 cube IXP_SET_TIMING(pio_timing, chp->ch_channel, drive, 216 1.1 cube ixp_pio_timings[drvp->PIO_mode]); 217 1.1 cube } 218 1.1 cube pci_conf_write(sc->sc_pc, sc->sc_tag, IXP_UDMA_CTL, udma); 219 1.1 cube pci_conf_write(sc->sc_pc, sc->sc_tag, IXP_MDMA_TIMING, mdma_timing); 220 1.1 cube pci_conf_write(sc->sc_pc, sc->sc_tag, IXP_PIO_CTL, pio); 221 1.1 cube pci_conf_write(sc->sc_pc, sc->sc_tag, IXP_PIO_TIMING, pio_timing); 222 1.25 msaitoh ATADEBUG_PRINT(("ixp_setup_channel: udma = %08x, mdma_timing = %08x, " 223 1.25 msaitoh "pio_mode = %08x, pio_timing = %08x\n", udma, mdma_timing, pio, 224 1.25 msaitoh pio_timing), DEBUG_PROBE); 225 1.1 cube } 226