1 /* $NetBSD: hptide.c,v 1.9 2004/01/03 22:56:53 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1999, 2000, 2001 Manuel Bouyer. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Manuel Bouyer. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 35 #include <dev/pci/pcivar.h> 36 #include <dev/pci/pcidevs.h> 37 #include <dev/pci/pciidereg.h> 38 #include <dev/pci/pciidevar.h> 39 #include <dev/pci/pciide_hpt_reg.h> 40 41 static void hpt_chip_map(struct pciide_softc*, struct pci_attach_args*); 42 static void hpt_setup_channel(struct wdc_channel*); 43 static int hpt_pci_intr(void *); 44 45 static int hptide_match(struct device *, struct cfdata *, void *); 46 static void hptide_attach(struct device *, struct device *, void *); 47 48 CFATTACH_DECL(hptide, sizeof(struct pciide_softc), 49 hptide_match, hptide_attach, NULL, NULL); 50 51 static const struct pciide_product_desc pciide_triones_products[] = { 52 { PCI_PRODUCT_TRIONES_HPT302, 53 0, 54 NULL, 55 hpt_chip_map 56 }, 57 { PCI_PRODUCT_TRIONES_HPT366, 58 0, 59 NULL, 60 hpt_chip_map, 61 }, 62 { PCI_PRODUCT_TRIONES_HPT371, 63 0, 64 NULL, 65 hpt_chip_map, 66 }, 67 { PCI_PRODUCT_TRIONES_HPT372A, 68 0, 69 NULL, 70 hpt_chip_map 71 }, 72 { PCI_PRODUCT_TRIONES_HPT374, 73 0, 74 NULL, 75 hpt_chip_map 76 }, 77 { 0, 78 0, 79 NULL, 80 NULL 81 } 82 }; 83 84 static int 85 hptide_match(struct device *parent, struct cfdata *match, void *aux) 86 { 87 struct pci_attach_args *pa = aux; 88 89 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TRIONES) { 90 if (pciide_lookup_product(pa->pa_id, pciide_triones_products)) 91 return (2); 92 } 93 return (0); 94 } 95 96 static void 97 hptide_attach(struct device *parent, struct device *self, void *aux) 98 { 99 struct pci_attach_args *pa = aux; 100 struct pciide_softc *sc = (struct pciide_softc *)self; 101 102 pciide_common_attach(sc, pa, 103 pciide_lookup_product(pa->pa_id, pciide_triones_products)); 104 105 } 106 107 static void 108 hpt_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa) 109 { 110 struct pciide_channel *cp; 111 int i, compatchan, revision; 112 pcireg_t interface; 113 bus_size_t cmdsize, ctlsize; 114 115 if (pciide_chipen(sc, pa) == 0) 116 return; 117 118 revision = PCI_REVISION(pa->pa_class); 119 aprint_normal("%s: Triones/Highpoint ", 120 sc->sc_wdcdev.sc_dev.dv_xname); 121 switch (sc->sc_pp->ide_product) { 122 case PCI_PRODUCT_TRIONES_HPT302: 123 aprint_normal("HPT302 IDE Controller\n"); 124 break; 125 case PCI_PRODUCT_TRIONES_HPT371: 126 aprint_normal("HPT371 IDE Controller\n"); 127 break; 128 case PCI_PRODUCT_TRIONES_HPT374: 129 aprint_normal("HPT374 IDE Controller\n"); 130 break; 131 case PCI_PRODUCT_TRIONES_HPT372A: 132 aprint_normal("HPT372A IDE Controller\n"); 133 break; 134 case PCI_PRODUCT_TRIONES_HPT366: 135 if (revision == HPT372_REV) 136 aprint_normal("HPT372 IDE Controller\n"); 137 else if (revision == HPT370_REV) 138 aprint_normal("HPT370 IDE Controller\n"); 139 else if (revision == HPT370A_REV) 140 aprint_normal("HPT370A IDE Controller\n"); 141 else if (revision == HPT366_REV) 142 aprint_normal("HPT366 IDE Controller\n"); 143 else 144 aprint_normal("unknown HPT IDE controller rev %d\n", 145 revision); 146 break; 147 default: 148 aprint_normal("unknown HPT IDE controller 0x%x\n", 149 sc->sc_pp->ide_product); 150 } 151 152 /* 153 * when the chip is in native mode it identifies itself as a 154 * 'misc mass storage'. Fake interface in this case. 155 */ 156 if (PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) { 157 interface = PCI_INTERFACE(pa->pa_class); 158 } else { 159 interface = PCIIDE_INTERFACE_BUS_MASTER_DMA | 160 PCIIDE_INTERFACE_PCI(0); 161 if ((sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT366 && 162 (revision == HPT370_REV || revision == HPT370A_REV || 163 revision == HPT372_REV)) || 164 sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT302 || 165 sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT371 || 166 sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT372A || 167 sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT374) 168 interface |= PCIIDE_INTERFACE_PCI(1); 169 } 170 171 aprint_normal("%s: bus-master DMA support present", 172 sc->sc_wdcdev.sc_dev.dv_xname); 173 pciide_mapreg_dma(sc, pa); 174 aprint_normal("\n"); 175 sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 | 176 WDC_CAPABILITY_MODE; 177 if (sc->sc_dma_ok) { 178 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA; 179 sc->sc_wdcdev.cap |= WDC_CAPABILITY_IRQACK; 180 sc->sc_wdcdev.irqack = pciide_irqack; 181 } 182 sc->sc_wdcdev.PIO_cap = 4; 183 sc->sc_wdcdev.DMA_cap = 2; 184 185 sc->sc_wdcdev.set_modes = hpt_setup_channel; 186 sc->sc_wdcdev.channels = sc->wdc_chanarray; 187 if (sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT366 && 188 revision == HPT366_REV) { 189 sc->sc_wdcdev.nchannels = 1; 190 sc->sc_wdcdev.UDMA_cap = 4; 191 } else { 192 sc->sc_wdcdev.nchannels = 2; 193 if (sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT374 || 194 sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT372A || 195 sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT371 || 196 sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT302 || 197 (sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT366 && 198 revision == HPT372_REV)) 199 sc->sc_wdcdev.UDMA_cap = 6; 200 else 201 sc->sc_wdcdev.UDMA_cap = 5; 202 } 203 for (i = 0; i < sc->sc_wdcdev.nchannels; i++) { 204 cp = &sc->pciide_channels[i]; 205 if (sc->sc_wdcdev.nchannels > 1) { 206 compatchan = i; 207 if((pciide_pci_read(sc->sc_pc, sc->sc_tag, 208 HPT370_CTRL1(i)) & HPT370_CTRL1_EN) == 0) { 209 aprint_normal( 210 "%s: %s channel ignored (disabled)\n", 211 sc->sc_wdcdev.sc_dev.dv_xname, cp->name); 212 cp->wdc_channel.ch_flags |= WDCF_DISABLED; 213 continue; 214 } 215 } else { 216 /* 217 * The 366 has 2 PCI IDE functions, one for primary and 218 * one for secondary. So we need to call 219 * pciide_mapregs_compat() with the real channel. 220 */ 221 if (pa->pa_function == 0) 222 compatchan = 0; 223 else if (pa->pa_function == 1) 224 compatchan = 1; 225 else { 226 aprint_error("%s: unexpected PCI function %d\n", 227 sc->sc_wdcdev.sc_dev.dv_xname, pa->pa_function); 228 return; 229 } 230 } 231 if (pciide_chansetup(sc, i, interface) == 0) 232 continue; 233 if (interface & PCIIDE_INTERFACE_PCI(i)) { 234 pciide_mapregs_native(pa, cp, &cmdsize, 235 &ctlsize, hpt_pci_intr); 236 } else { 237 pciide_mapregs_compat(pa, cp, compatchan, 238 &cmdsize, &ctlsize); 239 } 240 wdcattach(&cp->wdc_channel); 241 } 242 if ((sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT366 && 243 (revision == HPT370_REV || revision == HPT370A_REV || 244 revision == HPT372_REV)) || 245 sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT302 || 246 sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT371 || 247 sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT372A || 248 sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT374) { 249 /* 250 * HPT370_REV and highter has a bit to disable interrupts, 251 * make sure to clear it 252 */ 253 pciide_pci_write(sc->sc_pc, sc->sc_tag, HPT_CSEL, 254 pciide_pci_read(sc->sc_pc, sc->sc_tag, HPT_CSEL) & 255 ~HPT_CSEL_IRQDIS); 256 } 257 /* set clocks, etc (mandatory on 372/4, optional otherwise) */ 258 if ((sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT366 && 259 revision == HPT372_REV ) || 260 sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT302 || 261 sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT371 || 262 sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT372A || 263 sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT374) 264 pciide_pci_write(sc->sc_pc, sc->sc_tag, HPT_SC2, 265 (pciide_pci_read(sc->sc_pc, sc->sc_tag, HPT_SC2) & 266 HPT_SC2_MAEN) | HPT_SC2_OSC_EN); 267 return; 268 } 269 270 static void 271 hpt_setup_channel(struct wdc_channel *chp) 272 { 273 struct ata_drive_datas *drvp; 274 int drive; 275 int cable; 276 u_int32_t before, after; 277 u_int32_t idedma_ctl; 278 struct pciide_channel *cp = (struct pciide_channel*)chp; 279 struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.ch_wdc; 280 int revision = 281 PCI_REVISION(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG)); 282 const u_int32_t *tim_pio, *tim_dma, *tim_udma; 283 284 cable = pciide_pci_read(sc->sc_pc, sc->sc_tag, HPT_CSEL); 285 286 /* setup DMA if needed */ 287 pciide_channel_dma_setup(cp); 288 289 idedma_ctl = 0; 290 291 /* select the timing arrays for the chip */ 292 switch (sc->sc_pp->ide_product) { 293 case PCI_PRODUCT_TRIONES_HPT374: 294 tim_udma = hpt374_udma; 295 tim_dma = hpt374_dma; 296 tim_pio = hpt374_pio; 297 break; 298 case PCI_PRODUCT_TRIONES_HPT302: 299 case PCI_PRODUCT_TRIONES_HPT371: 300 case PCI_PRODUCT_TRIONES_HPT372A: 301 tim_udma = hpt372_udma; 302 tim_dma = hpt372_dma; 303 tim_pio = hpt372_pio; 304 break; 305 case PCI_PRODUCT_TRIONES_HPT366: 306 default: 307 switch (revision) { 308 case HPT372_REV: 309 tim_udma = hpt372_udma; 310 tim_dma = hpt372_dma; 311 tim_pio = hpt372_pio; 312 break; 313 case HPT370_REV: 314 case HPT370A_REV: 315 tim_udma = hpt370_udma; 316 tim_dma = hpt370_dma; 317 tim_pio = hpt370_pio; 318 break; 319 case HPT366_REV: 320 default: 321 tim_udma = hpt366_udma; 322 tim_dma = hpt366_dma; 323 tim_pio = hpt366_pio; 324 break; 325 } 326 } 327 328 /* Per drive settings */ 329 for (drive = 0; drive < 2; drive++) { 330 drvp = &chp->ch_drive[drive]; 331 /* If no drive, skip */ 332 if ((drvp->drive_flags & DRIVE) == 0) 333 continue; 334 before = pci_conf_read(sc->sc_pc, sc->sc_tag, 335 HPT_IDETIM(chp->ch_channel, drive)); 336 337 /* add timing values, setup DMA if needed */ 338 if (drvp->drive_flags & DRIVE_UDMA) { 339 /* use Ultra/DMA */ 340 drvp->drive_flags &= ~DRIVE_DMA; 341 if ((cable & HPT_CSEL_CBLID(chp->ch_channel)) != 0 && 342 drvp->UDMA_mode > 2) 343 drvp->UDMA_mode = 2; 344 after = tim_udma[drvp->UDMA_mode]; 345 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive); 346 } else if (drvp->drive_flags & DRIVE_DMA) { 347 /* 348 * use Multiword DMA. 349 * Timings will be used for both PIO and DMA, so adjust 350 * DMA mode if needed 351 */ 352 if (drvp->PIO_mode >= 3 && 353 (drvp->DMA_mode + 2) > drvp->PIO_mode) { 354 drvp->DMA_mode = drvp->PIO_mode - 2; 355 } 356 after = tim_dma[drvp->DMA_mode]; 357 idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive); 358 } else { 359 /* PIO only */ 360 after = tim_pio[drvp->PIO_mode]; 361 } 362 pci_conf_write(sc->sc_pc, sc->sc_tag, 363 HPT_IDETIM(chp->ch_channel, drive), after); 364 WDCDEBUG_PRINT(("%s: bus speed register set to 0x%08x " 365 "(BIOS 0x%08x)\n", drvp->drv_softc->dv_xname, 366 after, before), DEBUG_PROBE); 367 } 368 if (idedma_ctl != 0) { 369 /* Add software bits in status register */ 370 bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0, 371 idedma_ctl); 372 } 373 } 374 375 static int 376 hpt_pci_intr(void *arg) 377 { 378 struct pciide_softc *sc = arg; 379 struct pciide_channel *cp; 380 struct wdc_channel *wdc_cp; 381 int rv = 0; 382 int dmastat, i, crv; 383 384 for (i = 0; i < sc->sc_wdcdev.nchannels; i++) { 385 cp = &sc->pciide_channels[i]; 386 dmastat = bus_space_read_1(sc->sc_dma_iot, 387 cp->dma_iohs[IDEDMA_CTL], 0); 388 if((dmastat & ( IDEDMA_CTL_ACT | IDEDMA_CTL_INTR)) != 389 IDEDMA_CTL_INTR) 390 continue; 391 wdc_cp = &cp->wdc_channel; 392 crv = wdcintr(wdc_cp); 393 if (crv == 0) { 394 printf("%s:%d: bogus intr\n", 395 sc->sc_wdcdev.sc_dev.dv_xname, i); 396 bus_space_write_1(sc->sc_dma_iot, 397 cp->dma_iohs[IDEDMA_CTL], 0, dmastat); 398 } else 399 rv = 1; 400 } 401 return rv; 402 } 403