Home | History | Annotate | Line # | Download | only in pci
viaide.c revision 1.89.34.1
      1  1.89.34.1  perseant /*	$NetBSD: viaide.c,v 1.89.34.1 2025/08/02 05:57:00 perseant Exp $	*/
      2        1.1    bouyer 
      3        1.1    bouyer /*
      4        1.1    bouyer  * Copyright (c) 1999, 2000, 2001 Manuel Bouyer.
      5        1.1    bouyer  *
      6        1.1    bouyer  * Redistribution and use in source and binary forms, with or without
      7        1.1    bouyer  * modification, are permitted provided that the following conditions
      8        1.1    bouyer  * are met:
      9        1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     10        1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     11        1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     12        1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     13        1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     14        1.1    bouyer  *
     15        1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16        1.1    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17        1.1    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18       1.24     perry  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19        1.1    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20        1.1    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21        1.1    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22        1.1    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23        1.1    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24        1.1    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25        1.1    bouyer  *
     26        1.1    bouyer  */
     27        1.1    bouyer 
     28       1.25     lukem #include <sys/cdefs.h>
     29  1.89.34.1  perseant __KERNEL_RCSID(0, "$NetBSD: viaide.c,v 1.89.34.1 2025/08/02 05:57:00 perseant Exp $");
     30       1.25     lukem 
     31        1.1    bouyer #include <sys/param.h>
     32        1.1    bouyer #include <sys/systm.h>
     33        1.1    bouyer 
     34        1.1    bouyer #include <dev/pci/pcivar.h>
     35        1.1    bouyer #include <dev/pci/pcidevs.h>
     36        1.1    bouyer #include <dev/pci/pciidereg.h>
     37        1.1    bouyer #include <dev/pci/pciidevar.h>
     38        1.1    bouyer #include <dev/pci/pciide_apollo_reg.h>
     39        1.1    bouyer 
     40       1.72    dyoung static int	via_pcib_match(const struct pci_attach_args *);
     41       1.72    dyoung static void	via_chip_map(struct pciide_softc *,
     42       1.72    dyoung 		    const struct pci_attach_args *);
     43       1.72    dyoung static void	via_mapchan(const struct pci_attach_args *,
     44       1.72    dyoung 		    struct pciide_channel *,
     45       1.70  jakllsch 		    pcireg_t, int (*)(void *));
     46       1.72    dyoung static void	via_mapregs_compat_native(const struct pci_attach_args *,
     47       1.70  jakllsch 		    struct pciide_channel *);
     48       1.35    bouyer static int	via_sata_chip_map_common(struct pciide_softc *,
     49       1.76  jakllsch 		    const struct pci_attach_args *);
     50        1.4     enami static void	via_sata_chip_map(struct pciide_softc *,
     51       1.72    dyoung 		    const struct pci_attach_args *, int);
     52       1.35    bouyer static void	via_sata_chip_map_6(struct pciide_softc *,
     53       1.72    dyoung 		    const struct pci_attach_args *);
     54       1.35    bouyer static void	via_sata_chip_map_7(struct pciide_softc *,
     55       1.72    dyoung 		    const struct pci_attach_args *);
     56       1.35    bouyer static void	via_sata_chip_map_new(struct pciide_softc *,
     57       1.72    dyoung 		    const struct pci_attach_args *);
     58       1.15   thorpej static void	via_setup_channel(struct ata_channel *);
     59        1.4     enami 
     60       1.53      cube static int	viaide_match(device_t, cfdata_t, void *);
     61       1.53      cube static void	viaide_attach(device_t, device_t, void *);
     62        1.4     enami static const struct pciide_product_desc *
     63        1.4     enami 		viaide_lookup(pcireg_t);
     64       1.68    dyoung static bool	viaide_suspend(device_t, const pmf_qual_t *);
     65       1.68    dyoung static bool	viaide_resume(device_t, const pmf_qual_t *);
     66        1.1    bouyer 
     67       1.53      cube CFATTACH_DECL_NEW(viaide, sizeof(struct pciide_softc),
     68       1.71  jakllsch     viaide_match, viaide_attach, pciide_detach, NULL);
     69        1.1    bouyer 
     70        1.2   thorpej static const struct pciide_product_desc pciide_amd_products[] =  {
     71        1.1    bouyer 	{ PCI_PRODUCT_AMD_PBC756_IDE,
     72        1.1    bouyer 	  0,
     73       1.59  jmcneill 	  "AMD AMD756 IDE Controller",
     74        1.1    bouyer 	  via_chip_map
     75        1.1    bouyer 	},
     76        1.1    bouyer 	{ PCI_PRODUCT_AMD_PBC766_IDE,
     77        1.1    bouyer 	  0,
     78       1.59  jmcneill 	  "AMD AMD766 IDE Controller",
     79        1.1    bouyer 	  via_chip_map
     80        1.1    bouyer 	},
     81        1.1    bouyer 	{ PCI_PRODUCT_AMD_PBC768_IDE,
     82        1.1    bouyer 	  0,
     83       1.59  jmcneill 	  "AMD AMD768 IDE Controller",
     84        1.1    bouyer 	  via_chip_map
     85        1.1    bouyer 	},
     86        1.1    bouyer 	{ PCI_PRODUCT_AMD_PBC8111_IDE,
     87        1.1    bouyer 	  0,
     88       1.59  jmcneill 	  "AMD AMD8111 IDE Controller",
     89        1.1    bouyer 	  via_chip_map
     90        1.1    bouyer 	},
     91       1.38     isaki 	{ PCI_PRODUCT_AMD_CS5536_IDE,
     92       1.38     isaki 	  0,
     93       1.59  jmcneill 	  "AMD CS5536 IDE Controller",
     94       1.38     isaki 	  via_chip_map
     95       1.38     isaki 	},
     96        1.1    bouyer 	{ 0,
     97        1.1    bouyer 	  0,
     98        1.1    bouyer 	  NULL,
     99        1.1    bouyer 	  NULL
    100        1.1    bouyer 	}
    101        1.1    bouyer };
    102        1.1    bouyer 
    103        1.2   thorpej static const struct pciide_product_desc pciide_nvidia_products[] = {
    104        1.1    bouyer 	{ PCI_PRODUCT_NVIDIA_NFORCE_ATA100,
    105        1.1    bouyer 	  0,
    106        1.1    bouyer 	  "NVIDIA nForce IDE Controller",
    107        1.1    bouyer 	  via_chip_map
    108        1.1    bouyer 	},
    109        1.1    bouyer 	{ PCI_PRODUCT_NVIDIA_NFORCE2_ATA133,
    110        1.1    bouyer 	  0,
    111        1.1    bouyer 	  "NVIDIA nForce2 IDE Controller",
    112        1.1    bouyer 	  via_chip_map
    113        1.1    bouyer 	},
    114       1.20  jdolecek 	{ PCI_PRODUCT_NVIDIA_NFORCE2_400_ATA133,
    115       1.20  jdolecek 	  0,
    116       1.20  jdolecek 	  "NVIDIA nForce2 Ultra 400 IDE Controller",
    117       1.20  jdolecek 	  via_chip_map
    118       1.20  jdolecek 	},
    119       1.20  jdolecek 	{ PCI_PRODUCT_NVIDIA_NFORCE2_400_SATA,
    120       1.20  jdolecek 	  0,
    121       1.20  jdolecek 	  "NVIDIA nForce2 Ultra 400 Serial ATA Controller",
    122       1.35    bouyer 	  via_sata_chip_map_6
    123       1.20  jdolecek 	},
    124       1.10      fvdl 	{ PCI_PRODUCT_NVIDIA_NFORCE3_ATA133,
    125       1.10      fvdl 	  0,
    126       1.10      fvdl 	  "NVIDIA nForce3 IDE Controller",
    127       1.10      fvdl 	  via_chip_map
    128       1.10      fvdl 	},
    129       1.19   xtraeme 	{ PCI_PRODUCT_NVIDIA_NFORCE3_250_ATA133,
    130       1.19   xtraeme 	  0,
    131       1.19   xtraeme 	  "NVIDIA nForce3 250 IDE Controller",
    132       1.19   xtraeme 	  via_chip_map
    133       1.19   xtraeme 	},
    134       1.19   xtraeme 	{ PCI_PRODUCT_NVIDIA_NFORCE3_250_SATA,
    135       1.19   xtraeme 	  0,
    136       1.19   xtraeme 	  "NVIDIA nForce3 250 Serial ATA Controller",
    137       1.35    bouyer 	  via_sata_chip_map_6
    138       1.19   xtraeme 	},
    139       1.32   xtraeme 	{ PCI_PRODUCT_NVIDIA_NFORCE3_250_SATA2,
    140       1.32   xtraeme 	  0,
    141       1.32   xtraeme 	  "NVIDIA nForce3 250 Serial ATA Controller",
    142       1.35    bouyer 	  via_sata_chip_map_6
    143       1.32   xtraeme 	},
    144       1.21      kent 	{ PCI_PRODUCT_NVIDIA_NFORCE4_ATA133,
    145       1.21      kent 	  0,
    146       1.21      kent 	  "NVIDIA nForce4 IDE Controller",
    147       1.21      kent 	  via_chip_map
    148       1.21      kent 	},
    149       1.21      kent 	{ PCI_PRODUCT_NVIDIA_NFORCE4_SATA1,
    150       1.21      kent 	  0,
    151       1.21      kent 	  "NVIDIA nForce4 Serial ATA Controller",
    152       1.35    bouyer 	  via_sata_chip_map_6
    153       1.21      kent 	},
    154       1.21      kent 	{ PCI_PRODUCT_NVIDIA_NFORCE4_SATA2,
    155       1.21      kent 	  0,
    156       1.21      kent 	  "NVIDIA nForce4 Serial ATA Controller",
    157       1.35    bouyer 	  via_sata_chip_map_6
    158       1.21      kent 	},
    159       1.27      manu 	{ PCI_PRODUCT_NVIDIA_NFORCE430_ATA133,
    160       1.27      manu 	  0,
    161       1.27      manu 	  "NVIDIA nForce430 IDE Controller",
    162       1.27      manu 	  via_chip_map
    163       1.27      manu 	},
    164       1.27      manu 	{ PCI_PRODUCT_NVIDIA_NFORCE430_SATA1,
    165       1.27      manu 	  0,
    166       1.27      manu 	  "NVIDIA nForce430 Serial ATA Controller",
    167       1.35    bouyer 	  via_sata_chip_map_6
    168       1.27      manu 	},
    169       1.27      manu 	{ PCI_PRODUCT_NVIDIA_NFORCE430_SATA2,
    170       1.27      manu 	  0,
    171       1.27      manu 	  "NVIDIA nForce430 Serial ATA Controller",
    172       1.35    bouyer 	  via_sata_chip_map_6
    173       1.27      manu 	},
    174       1.30   xtraeme 	{ PCI_PRODUCT_NVIDIA_MCP04_IDE,
    175       1.30   xtraeme 	  0,
    176       1.30   xtraeme 	  "NVIDIA MCP04 IDE Controller",
    177       1.30   xtraeme 	  via_chip_map
    178       1.30   xtraeme 	},
    179       1.30   xtraeme 	{ PCI_PRODUCT_NVIDIA_MCP04_SATA,
    180       1.30   xtraeme 	  0,
    181       1.31   xtraeme 	  "NVIDIA MCP04 Serial ATA Controller",
    182       1.35    bouyer 	  via_sata_chip_map_6
    183       1.30   xtraeme 	},
    184       1.30   xtraeme 	{ PCI_PRODUCT_NVIDIA_MCP04_SATA2,
    185       1.30   xtraeme 	  0,
    186       1.31   xtraeme 	  "NVIDIA MCP04 Serial ATA Controller",
    187       1.35    bouyer 	  via_sata_chip_map_6
    188       1.30   xtraeme 	},
    189       1.30   xtraeme 	{ PCI_PRODUCT_NVIDIA_MCP55_IDE,
    190       1.30   xtraeme 	  0,
    191       1.30   xtraeme 	  "NVIDIA MCP55 IDE Controller",
    192       1.30   xtraeme 	  via_chip_map
    193       1.30   xtraeme 	},
    194       1.30   xtraeme 	{ PCI_PRODUCT_NVIDIA_MCP55_SATA,
    195       1.30   xtraeme 	  0,
    196       1.31   xtraeme 	  "NVIDIA MCP55 Serial ATA Controller",
    197       1.35    bouyer 	  via_sata_chip_map_6
    198       1.30   xtraeme 	},
    199       1.30   xtraeme 	{ PCI_PRODUCT_NVIDIA_MCP55_SATA2,
    200       1.30   xtraeme 	  0,
    201       1.31   xtraeme 	  "NVIDIA MCP55 Serial ATA Controller",
    202       1.35    bouyer 	  via_sata_chip_map_6
    203       1.30   xtraeme 	},
    204       1.33   xtraeme 	{ PCI_PRODUCT_NVIDIA_MCP61_IDE,
    205       1.33   xtraeme 	  0,
    206       1.33   xtraeme 	  "NVIDIA MCP61 IDE Controller",
    207       1.33   xtraeme 	  via_chip_map
    208       1.33   xtraeme 	},
    209       1.33   xtraeme 	{ PCI_PRODUCT_NVIDIA_MCP65_IDE,
    210       1.33   xtraeme 	  0,
    211       1.33   xtraeme 	  "NVIDIA MCP65 IDE Controller",
    212       1.33   xtraeme 	  via_chip_map
    213       1.33   xtraeme 	},
    214       1.46   xtraeme 	{ PCI_PRODUCT_NVIDIA_MCP73_IDE,
    215       1.46   xtraeme 	  0,
    216       1.46   xtraeme 	  "NVIDIA MCP73 IDE Controller",
    217       1.46   xtraeme 	  via_chip_map
    218       1.46   xtraeme 	},
    219       1.46   xtraeme 	{ PCI_PRODUCT_NVIDIA_MCP77_IDE,
    220       1.46   xtraeme 	  0,
    221       1.46   xtraeme 	  "NVIDIA MCP77 IDE Controller",
    222       1.46   xtraeme 	  via_chip_map
    223       1.46   xtraeme 	},
    224       1.33   xtraeme 	{ PCI_PRODUCT_NVIDIA_MCP61_SATA,
    225       1.33   xtraeme 	  0,
    226       1.33   xtraeme 	  "NVIDIA MCP61 Serial ATA Controller",
    227       1.35    bouyer 	  via_sata_chip_map_6
    228       1.33   xtraeme 	},
    229       1.33   xtraeme 	{ PCI_PRODUCT_NVIDIA_MCP61_SATA2,
    230       1.33   xtraeme 	  0,
    231       1.33   xtraeme 	  "NVIDIA MCP61 Serial ATA Controller",
    232       1.35    bouyer 	  via_sata_chip_map_6
    233       1.33   xtraeme 	},
    234       1.33   xtraeme 	{ PCI_PRODUCT_NVIDIA_MCP61_SATA3,
    235       1.33   xtraeme 	  0,
    236       1.33   xtraeme 	  "NVIDIA MCP61 Serial ATA Controller",
    237       1.35    bouyer 	  via_sata_chip_map_6
    238       1.33   xtraeme 	},
    239       1.33   xtraeme 	{ PCI_PRODUCT_NVIDIA_MCP65_SATA,
    240       1.33   xtraeme 	  0,
    241       1.33   xtraeme 	  "NVIDIA MCP65 Serial ATA Controller",
    242       1.35    bouyer 	  via_sata_chip_map_6
    243       1.33   xtraeme 	},
    244       1.33   xtraeme 	{ PCI_PRODUCT_NVIDIA_MCP65_SATA2,
    245       1.33   xtraeme 	  0,
    246       1.33   xtraeme 	  "NVIDIA MCP65 Serial ATA Controller",
    247       1.35    bouyer 	  via_sata_chip_map_6
    248       1.33   xtraeme 	},
    249       1.33   xtraeme 	{ PCI_PRODUCT_NVIDIA_MCP65_SATA3,
    250       1.33   xtraeme 	  0,
    251       1.33   xtraeme 	  "NVIDIA MCP65 Serial ATA Controller",
    252       1.35    bouyer 	  via_sata_chip_map_6
    253       1.33   xtraeme 	},
    254       1.33   xtraeme 	{ PCI_PRODUCT_NVIDIA_MCP65_SATA4,
    255       1.33   xtraeme 	  0,
    256       1.33   xtraeme 	  "NVIDIA MCP65 Serial ATA Controller",
    257       1.35    bouyer 	  via_sata_chip_map_6
    258       1.33   xtraeme 	},
    259       1.43   xtraeme 	{ PCI_PRODUCT_NVIDIA_MCP67_IDE,
    260       1.43   xtraeme 	  0,
    261       1.43   xtraeme 	  "NVIDIA MCP67 IDE Controller",
    262       1.43   xtraeme 	  via_chip_map,
    263       1.43   xtraeme 	},
    264       1.43   xtraeme 	{ PCI_PRODUCT_NVIDIA_MCP67_SATA,
    265       1.43   xtraeme 	  0,
    266       1.43   xtraeme 	  "NVIDIA MCP67 Serial ATA Controller",
    267       1.43   xtraeme 	  via_sata_chip_map_6,
    268       1.43   xtraeme 	},
    269       1.43   xtraeme 	{ PCI_PRODUCT_NVIDIA_MCP67_SATA2,
    270       1.43   xtraeme 	  0,
    271       1.43   xtraeme 	  "NVIDIA MCP67 Serial ATA Controller",
    272       1.43   xtraeme 	  via_sata_chip_map_6,
    273       1.43   xtraeme 	},
    274       1.43   xtraeme 	{ PCI_PRODUCT_NVIDIA_MCP67_SATA3,
    275       1.43   xtraeme 	  0,
    276       1.43   xtraeme 	  "NVIDIA MCP67 Serial ATA Controller",
    277       1.43   xtraeme 	  via_sata_chip_map_6,
    278       1.43   xtraeme 	},
    279       1.43   xtraeme 	{ PCI_PRODUCT_NVIDIA_MCP67_SATA4,
    280       1.43   xtraeme 	  0,
    281       1.43   xtraeme 	  "NVIDIA MCP67 Serial ATA Controller",
    282       1.43   xtraeme 	  via_sata_chip_map_6,
    283       1.43   xtraeme 	},
    284        1.1    bouyer 	{ 0,
    285        1.1    bouyer 	  0,
    286        1.1    bouyer 	  NULL,
    287        1.1    bouyer 	  NULL
    288        1.1    bouyer 	}
    289        1.1    bouyer };
    290        1.1    bouyer 
    291        1.2   thorpej static const struct pciide_product_desc pciide_via_products[] =  {
    292        1.1    bouyer 	{ PCI_PRODUCT_VIATECH_VT82C586_IDE,
    293        1.1    bouyer 	  0,
    294        1.1    bouyer 	  NULL,
    295        1.1    bouyer 	  via_chip_map,
    296        1.1    bouyer 	 },
    297        1.1    bouyer 	{ PCI_PRODUCT_VIATECH_VT82C586A_IDE,
    298        1.1    bouyer 	  0,
    299        1.1    bouyer 	  NULL,
    300        1.1    bouyer 	  via_chip_map,
    301        1.1    bouyer 	},
    302       1.42   xtraeme 	{ PCI_PRODUCT_VIATECH_CX700_IDE,
    303       1.42   xtraeme 	  0,
    304  1.89.34.1  perseant 	  "VIA Technologies CX700(M2)/VX700/VX800 SATA/IDE RAID Controller",
    305  1.89.34.1  perseant 	  via_chip_map,
    306       1.42   xtraeme 	},
    307       1.57     rmind 	{ PCI_PRODUCT_VIATECH_CX700M2_IDE,
    308       1.57     rmind 	  0,
    309       1.57     rmind 	  NULL,
    310       1.57     rmind 	  via_chip_map,
    311       1.57     rmind 	},
    312       1.78   tsutsui 	{ PCI_PRODUCT_VIATECH_VX900_IDE,
    313       1.78   tsutsui 	  0,
    314  1.89.34.1  perseant 	  "VIA Technologies VX900/VX11 SATA controller",
    315  1.89.34.1  perseant 	  via_chip_map,
    316  1.89.34.1  perseant 	},
    317  1.89.34.1  perseant 	{ PCI_PRODUCT_VIATECH_VX900_RAID,
    318  1.89.34.1  perseant 	  0,
    319  1.89.34.1  perseant 	  "VIA Technologies VX900/VX11 SATA controller (RAID mode)",
    320       1.78   tsutsui 	  via_chip_map,
    321       1.78   tsutsui 	},
    322       1.77       phx 	{ PCI_PRODUCT_VIATECH_VT6410_RAID,
    323       1.77       phx 	  0,
    324  1.89.34.1  perseant 	  "VIA Technologies VT6410 IDE controller",
    325  1.89.34.1  perseant 	  via_chip_map,
    326  1.89.34.1  perseant 	},
    327  1.89.34.1  perseant 	{ PCI_PRODUCT_VIATECH_VT6415_IDE,
    328  1.89.34.1  perseant 	  0,
    329  1.89.34.1  perseant 	  "VIA Technologies VT6415/VT6330 IDE controller",
    330       1.77       phx 	  via_chip_map,
    331       1.77       phx 	},
    332       1.23       abs 	{ PCI_PRODUCT_VIATECH_VT6421_RAID,
    333       1.22       abs 	  0,
    334       1.76  jakllsch 	  "VIA Technologies VT6421 Serial ATA RAID Controller",
    335       1.35    bouyer 	  via_sata_chip_map_new,
    336       1.22       abs 	},
    337        1.1    bouyer 	{ PCI_PRODUCT_VIATECH_VT8237_SATA,
    338        1.6   mycroft 	  0,
    339        1.1    bouyer 	  "VIA Technologies VT8237 SATA Controller",
    340       1.35    bouyer 	  via_sata_chip_map_7,
    341       1.35    bouyer 	},
    342       1.35    bouyer 	{ PCI_PRODUCT_VIATECH_VT8237A_SATA,
    343       1.35    bouyer 	  0,
    344       1.35    bouyer 	  "VIA Technologies VT8237A SATA Controller",
    345       1.41   garbled 	  via_sata_chip_map_7,
    346        1.1    bouyer 	},
    347       1.60  jmcneill 	{ PCI_PRODUCT_VIATECH_VT8237A_SATA_2,
    348       1.60  jmcneill 	  0,
    349       1.60  jmcneill 	  "VIA Technologies VT8237A (5337) SATA Controller",
    350       1.60  jmcneill 	  via_sata_chip_map_7,
    351       1.60  jmcneill 	},
    352  1.89.34.1  perseant 	/*
    353  1.89.34.1  perseant 	 * The 0x3349 PCI ID may be reused in all modes (IDE, RAID, AHCI).
    354  1.89.34.1  perseant 	 * ahcisata(4) will attach if AHCI mode is selected in the BIOS.
    355  1.89.34.1  perseant 	 * Newer CE revision southbridges use it only in RAID mode.
    356  1.89.34.1  perseant 	 */
    357  1.89.34.1  perseant 	{ PCI_PRODUCT_VIATECH_VT8251_SATA,
    358       1.29   xtraeme 	  0,
    359  1.89.34.1  perseant 	  "VIA Technologies VT8251 SATA Controller",
    360  1.89.34.1  perseant 	  via_chip_map,
    361  1.89.34.1  perseant 	},
    362  1.89.34.1  perseant 	/*
    363  1.89.34.1  perseant 	 * The 0x5287 PCI ID is used only in IDE mode for newer
    364  1.89.34.1  perseant 	 * VT8251 southbridge (CE) revisions.
    365  1.89.34.1  perseant 	 */
    366  1.89.34.1  perseant 	{ PCI_PRODUCT_VIATECH_VT8251_SATA_2,
    367  1.89.34.1  perseant 	  0,
    368  1.89.34.1  perseant 	  "VIA Technologies VT8251 (5287) SATA Controller",
    369  1.89.34.1  perseant 	  via_chip_map,
    370       1.29   xtraeme 	},
    371       1.58    nonaka 	{ PCI_PRODUCT_VIATECH_VT8237S_SATA,
    372       1.58    nonaka 	  0,
    373       1.58    nonaka 	  "VIA Technologies VT8237S SATA Controller",
    374       1.58    nonaka 	  via_sata_chip_map_7,
    375       1.58    nonaka 	},
    376       1.88  jdolecek 	{ PCI_PRODUCT_VIATECH_VT8237S_SATA_RAID,
    377       1.88  jdolecek 	  0,
    378       1.88  jdolecek 	  "VIA Technologies VT8237S SATA Controller (RAID mode)",
    379       1.88  jdolecek 	  via_sata_chip_map_7,
    380       1.88  jdolecek 	},
    381  1.89.34.1  perseant 	{ PCI_PRODUCT_VIATECH_VT8261_SATA,
    382  1.89.34.1  perseant 	  0,
    383  1.89.34.1  perseant 	  "VIA Technologies VT8261 SATA Controller",
    384  1.89.34.1  perseant 	  via_chip_map,
    385  1.89.34.1  perseant 	},
    386  1.89.34.1  perseant 	{ PCI_PRODUCT_VIATECH_VT8261_RAID,
    387  1.89.34.1  perseant 	  0,
    388  1.89.34.1  perseant 	  "VIA Technologies VT8261 SATA Controller (RAID mode)",
    389  1.89.34.1  perseant 	  via_chip_map,
    390  1.89.34.1  perseant 	},
    391        1.1    bouyer 	{ 0,
    392        1.1    bouyer 	  0,
    393        1.1    bouyer 	  NULL,
    394        1.1    bouyer 	  NULL
    395        1.1    bouyer 	}
    396        1.1    bouyer };
    397        1.1    bouyer 
    398        1.4     enami static const struct pciide_product_desc *
    399        1.4     enami viaide_lookup(pcireg_t id)
    400        1.4     enami {
    401        1.4     enami 
    402        1.4     enami 	switch (PCI_VENDOR(id)) {
    403        1.4     enami 	case PCI_VENDOR_VIATECH:
    404        1.4     enami 		return (pciide_lookup_product(id, pciide_via_products));
    405        1.4     enami 
    406        1.4     enami 	case PCI_VENDOR_AMD:
    407        1.4     enami 		return (pciide_lookup_product(id, pciide_amd_products));
    408        1.4     enami 
    409        1.4     enami 	case PCI_VENDOR_NVIDIA:
    410        1.4     enami 		return (pciide_lookup_product(id, pciide_nvidia_products));
    411        1.4     enami 	}
    412        1.4     enami 	return (NULL);
    413        1.4     enami }
    414        1.4     enami 
    415        1.2   thorpej static int
    416       1.53      cube viaide_match(device_t parent, cfdata_t match, void *aux)
    417        1.1    bouyer {
    418       1.76  jakllsch 	const struct pci_attach_args *pa = aux;
    419        1.1    bouyer 
    420        1.4     enami 	if (viaide_lookup(pa->pa_id) != NULL)
    421        1.4     enami 		return (2);
    422        1.1    bouyer 	return (0);
    423        1.1    bouyer }
    424        1.1    bouyer 
    425        1.2   thorpej static void
    426       1.53      cube viaide_attach(device_t parent, device_t self, void *aux)
    427        1.1    bouyer {
    428       1.76  jakllsch 	const struct pci_attach_args *pa = aux;
    429       1.53      cube 	struct pciide_softc *sc = device_private(self);
    430        1.4     enami 	const struct pciide_product_desc *pp;
    431        1.1    bouyer 
    432       1.53      cube 	sc->sc_wdcdev.sc_atac.atac_dev = self;
    433       1.53      cube 
    434        1.4     enami 	pp = viaide_lookup(pa->pa_id);
    435        1.1    bouyer 	if (pp == NULL)
    436        1.1    bouyer 		panic("viaide_attach");
    437        1.1    bouyer 	pciide_common_attach(sc, pa, pp);
    438       1.51     joerg 
    439       1.51     joerg 	if (!pmf_device_register(self, viaide_suspend, viaide_resume))
    440       1.51     joerg 		aprint_error_dev(self, "couldn't establish power handler\n");
    441        1.1    bouyer }
    442        1.1    bouyer 
    443        1.5      fvdl static int
    444       1.72    dyoung via_pcib_match(const struct pci_attach_args *pa)
    445        1.5      fvdl {
    446        1.5      fvdl 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE &&
    447        1.5      fvdl 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_ISA &&
    448        1.5      fvdl 	    PCI_VENDOR(pa->pa_id) == PCI_VENDOR_VIATECH)
    449        1.5      fvdl 		return (1);
    450        1.5      fvdl 	return 0;
    451        1.5      fvdl }
    452        1.5      fvdl 
    453       1.51     joerg static bool
    454       1.68    dyoung viaide_suspend(device_t dv, const pmf_qual_t *qual)
    455       1.51     joerg {
    456       1.51     joerg 	struct pciide_softc *sc = device_private(dv);
    457       1.51     joerg 
    458       1.51     joerg 	sc->sc_pm_reg[0] = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_IDECONF(sc));
    459       1.51     joerg 	/* APO_DATATIM(sc) includes APO_UDMA(sc) */
    460       1.51     joerg 	sc->sc_pm_reg[1] = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM(sc));
    461       1.51     joerg 	/* This two are VIA-only, but should be ignored by other devices. */
    462       1.51     joerg 	sc->sc_pm_reg[2] = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_CTLMISC(sc));
    463       1.51     joerg 	sc->sc_pm_reg[3] = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_MISCTIM(sc));
    464       1.51     joerg 
    465       1.51     joerg 	return true;
    466       1.51     joerg }
    467       1.51     joerg 
    468       1.51     joerg static bool
    469       1.68    dyoung viaide_resume(device_t dv, const pmf_qual_t *qual)
    470       1.51     joerg {
    471       1.51     joerg 	struct pciide_softc *sc = device_private(dv);
    472       1.51     joerg 
    473       1.51     joerg 	pci_conf_write(sc->sc_pc, sc->sc_tag, APO_IDECONF(sc),
    474       1.51     joerg 	    sc->sc_pm_reg[0]);
    475       1.51     joerg 	pci_conf_write(sc->sc_pc, sc->sc_tag, APO_DATATIM(sc),
    476       1.51     joerg 	    sc->sc_pm_reg[1]);
    477       1.51     joerg 	/* This two are VIA-only, but should be ignored by other devices. */
    478       1.51     joerg 	pci_conf_write(sc->sc_pc, sc->sc_tag, APO_CTLMISC(sc),
    479       1.51     joerg 	    sc->sc_pm_reg[2]);
    480       1.51     joerg 	pci_conf_write(sc->sc_pc, sc->sc_tag, APO_MISCTIM(sc),
    481       1.51     joerg 	    sc->sc_pm_reg[3]);
    482       1.51     joerg 
    483       1.51     joerg 	return true;
    484       1.51     joerg }
    485       1.51     joerg 
    486        1.2   thorpej static void
    487       1.72    dyoung via_chip_map(struct pciide_softc *sc, const struct pci_attach_args *pa)
    488        1.1    bouyer {
    489        1.1    bouyer 	struct pciide_channel *cp;
    490        1.1    bouyer 	pcireg_t interface = PCI_INTERFACE(pa->pa_class);
    491        1.1    bouyer 	pcireg_t vendor = PCI_VENDOR(pa->pa_id);
    492        1.1    bouyer 	int channel;
    493        1.1    bouyer 	u_int32_t ideconf;
    494  1.89.34.1  perseant 	int no_ideconf = 0;
    495  1.89.34.1  perseant 	int single_channel = 0;
    496        1.1    bouyer 	pcireg_t pcib_id, pcib_class;
    497        1.5      fvdl 	struct pci_attach_args pcib_pa;
    498        1.1    bouyer 
    499        1.1    bouyer 	if (pciide_chipen(sc, pa) == 0)
    500        1.1    bouyer 		return;
    501        1.1    bouyer 
    502        1.3     enami 	switch (vendor) {
    503        1.1    bouyer 	case PCI_VENDOR_VIATECH:
    504       1.77       phx 		switch (PCI_PRODUCT(pa->pa_id)) {
    505  1.89.34.1  perseant 		case PCI_PRODUCT_VIATECH_VT6415_IDE:
    506  1.89.34.1  perseant 			/* VT6415 is a single channel IDE controller. */
    507  1.89.34.1  perseant 			single_channel = 1;
    508  1.89.34.1  perseant 			/* FALLTHROUGH */
    509       1.77       phx 		case PCI_PRODUCT_VIATECH_VT6410_RAID:
    510  1.89.34.1  perseant 			/*
    511  1.89.34.1  perseant 			 * VT6415 uses an unknown offset of the enable chip
    512  1.89.34.1  perseant 			 * register, enable bits needs to be simulated.
    513  1.89.34.1  perseant 			 *
    514  1.89.34.1  perseant 			 * Some add-in VT6410 controllers have both channels
    515  1.89.34.1  perseant 			 * disabled by default. These require explicit enabling
    516  1.89.34.1  perseant 			 * during initialization.
    517  1.89.34.1  perseant 			 */
    518  1.89.34.1  perseant 			no_ideconf = 1;
    519  1.89.34.1  perseant 			/* FALLTHROUGH */
    520  1.89.34.1  perseant 		case PCI_PRODUCT_VIATECH_CX700_IDE:
    521       1.77       phx 			sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    522        1.1    bouyer 			break;
    523  1.89.34.1  perseant 		case PCI_PRODUCT_VIATECH_VT8251_SATA:
    524  1.89.34.1  perseant 			/* FALLTHROUGH */
    525  1.89.34.1  perseant 		case PCI_PRODUCT_VIATECH_VT8251_SATA_2:
    526  1.89.34.1  perseant 			/* FALLTHROUGH */
    527  1.89.34.1  perseant 		case PCI_PRODUCT_VIATECH_VT8261_SATA:
    528  1.89.34.1  perseant 			/* FALLTHROUGH */
    529  1.89.34.1  perseant 		case PCI_PRODUCT_VIATECH_VT8261_RAID:
    530  1.89.34.1  perseant 			/* FALLTHROUGH */
    531       1.78   tsutsui 		case PCI_PRODUCT_VIATECH_VX900_IDE:
    532  1.89.34.1  perseant 			/* FALLTHROUGH */
    533  1.89.34.1  perseant 		case PCI_PRODUCT_VIATECH_VX900_RAID:
    534  1.89.34.1  perseant 			sc->sc_wdcdev.sc_atac.atac_set_modes = sata_setup_channel;
    535       1.78   tsutsui 			sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    536       1.78   tsutsui 			break;
    537       1.77       phx 		default:
    538       1.77       phx 			/*
    539  1.89.34.1  perseant 			 * VIA integrated IDE controllers share the same PCI ID
    540  1.89.34.1  perseant 			 * between different southbridges and chipsets.
    541  1.89.34.1  perseant 			 * To determine their DMA capabilities, get a PCI tag
    542  1.89.34.1  perseant 			 * for the ISA bridge.
    543       1.77       phx 			 */
    544       1.77       phx 			if (pci_find_device(&pcib_pa, via_pcib_match) == 0)
    545       1.77       phx 				goto unknown;
    546       1.77       phx 			pcib_id = pcib_pa.pa_id;
    547       1.77       phx 			pcib_class = pcib_pa.pa_class;
    548       1.77       phx 			aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    549       1.77       phx 			    "VIA Technologies ");
    550       1.77       phx 			switch (PCI_PRODUCT(pcib_id)) {
    551       1.77       phx 			case PCI_PRODUCT_VIATECH_VT82C586_ISA:
    552       1.77       phx 				aprint_normal("VT82C586 (Apollo VP) ");
    553       1.77       phx 				if(PCI_REVISION(pcib_class) >= 0x02) {
    554       1.77       phx 					aprint_normal("ATA33 controller\n");
    555       1.77       phx 					sc->sc_wdcdev.sc_atac.atac_udma_cap = 2;
    556       1.77       phx 				} else {
    557       1.77       phx 					aprint_normal("controller\n");
    558       1.77       phx 					sc->sc_wdcdev.sc_atac.atac_udma_cap = 0;
    559       1.77       phx 				}
    560       1.77       phx 				break;
    561       1.77       phx 			case PCI_PRODUCT_VIATECH_VT82C596A:
    562       1.77       phx 				aprint_normal("VT82C596A (Apollo Pro) ");
    563       1.77       phx 				if (PCI_REVISION(pcib_class) >= 0x12) {
    564       1.77       phx 					aprint_normal("ATA66 controller\n");
    565       1.77       phx 					sc->sc_wdcdev.sc_atac.atac_udma_cap = 4;
    566       1.77       phx 				} else {
    567       1.77       phx 					aprint_normal("ATA33 controller\n");
    568       1.77       phx 					sc->sc_wdcdev.sc_atac.atac_udma_cap = 2;
    569       1.77       phx 				}
    570       1.77       phx 				break;
    571       1.77       phx 			case PCI_PRODUCT_VIATECH_VT82C686A_ISA:
    572       1.77       phx 				aprint_normal("VT82C686A (Apollo KX133) ");
    573       1.77       phx 				if (PCI_REVISION(pcib_class) >= 0x40) {
    574       1.77       phx 					aprint_normal("ATA100 controller\n");
    575       1.77       phx 					sc->sc_wdcdev.sc_atac.atac_udma_cap = 5;
    576       1.77       phx 				} else {
    577       1.77       phx 					aprint_normal("ATA66 controller\n");
    578       1.77       phx 					sc->sc_wdcdev.sc_atac.atac_udma_cap = 4;
    579       1.77       phx 				}
    580       1.77       phx 				break;
    581       1.77       phx 			case PCI_PRODUCT_VIATECH_VT8231:
    582       1.77       phx 				aprint_normal("VT8231 ATA100 controller\n");
    583       1.77       phx 				sc->sc_wdcdev.sc_atac.atac_udma_cap = 5;
    584       1.77       phx 				break;
    585       1.77       phx 			case PCI_PRODUCT_VIATECH_VT8233:
    586       1.77       phx 				aprint_normal("VT8233 ATA100 controller\n");
    587       1.17   thorpej 				sc->sc_wdcdev.sc_atac.atac_udma_cap = 5;
    588       1.77       phx 				break;
    589       1.77       phx 			case PCI_PRODUCT_VIATECH_VT8233A:
    590       1.77       phx 				aprint_normal("VT8233A ATA133 controller\n");
    591       1.77       phx 				sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    592       1.77       phx 				break;
    593       1.77       phx 			case PCI_PRODUCT_VIATECH_VT8235:
    594       1.77       phx 				aprint_normal("VT8235 ATA133 controller\n");
    595       1.77       phx 				sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    596       1.77       phx 				break;
    597       1.77       phx 			case PCI_PRODUCT_VIATECH_VT8237:
    598       1.77       phx 				aprint_normal("VT8237 ATA133 controller\n");
    599       1.77       phx 				sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    600       1.77       phx 				break;
    601       1.77       phx 			case PCI_PRODUCT_VIATECH_VT8237A_ISA:
    602       1.77       phx 				aprint_normal("VT8237A ATA133 controller\n");
    603       1.77       phx 				sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    604       1.77       phx 				break;
    605  1.89.34.1  perseant 			case PCI_PRODUCT_VIATECH_VT8237S_ISA:
    606  1.89.34.1  perseant 				aprint_normal("VT8237S ATA133 controller\n");
    607  1.89.34.1  perseant 				sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    608  1.89.34.1  perseant 				break;
    609       1.77       phx 			case PCI_PRODUCT_VIATECH_CX700:
    610       1.77       phx 				aprint_normal("CX700 ATA133 controller\n");
    611       1.77       phx 				sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    612       1.77       phx 				break;
    613       1.77       phx 			case PCI_PRODUCT_VIATECH_VT8251:
    614       1.77       phx 				aprint_normal("VT8251 ATA133 controller\n");
    615       1.77       phx 				sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    616       1.77       phx 				break;
    617  1.89.34.1  perseant 			case PCI_PRODUCT_VIATECH_VT8261:
    618  1.89.34.1  perseant 				aprint_normal("VT8261 ATA133 controller\n");
    619  1.89.34.1  perseant 				sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    620  1.89.34.1  perseant 				break;
    621       1.88  jdolecek 			case PCI_PRODUCT_VIATECH_VX800:
    622  1.89.34.1  perseant 				aprint_normal("VX800 ATA133 controller\n");
    623       1.88  jdolecek 				sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    624       1.88  jdolecek 				break;
    625       1.88  jdolecek 			case PCI_PRODUCT_VIATECH_VX855:
    626  1.89.34.1  perseant 				aprint_normal("VX855 ATA133 controller\n");
    627       1.88  jdolecek 				sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    628       1.88  jdolecek 				break;
    629       1.77       phx 			default:
    630       1.77       phx 		unknown:
    631       1.77       phx 				aprint_normal("unknown VIA ATA controller\n");
    632       1.77       phx 				sc->sc_wdcdev.sc_atac.atac_udma_cap = 0;
    633        1.1    bouyer 			}
    634        1.1    bouyer 			break;
    635        1.1    bouyer 		}
    636        1.1    bouyer 		sc->sc_apo_regbase = APO_VIA_REGBASE;
    637        1.1    bouyer 		break;
    638        1.1    bouyer 	case PCI_VENDOR_AMD:
    639        1.1    bouyer 		switch (sc->sc_pp->ide_product) {
    640       1.11    bouyer 		case PCI_PRODUCT_AMD_PBC8111_IDE:
    641       1.17   thorpej 			sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    642       1.11    bouyer 			break;
    643       1.45   xtraeme 		case PCI_PRODUCT_AMD_CS5536_IDE:
    644        1.1    bouyer 		case PCI_PRODUCT_AMD_PBC766_IDE:
    645        1.1    bouyer 		case PCI_PRODUCT_AMD_PBC768_IDE:
    646       1.17   thorpej 			sc->sc_wdcdev.sc_atac.atac_udma_cap = 5;
    647        1.1    bouyer 			break;
    648        1.1    bouyer 		default:
    649       1.17   thorpej 			sc->sc_wdcdev.sc_atac.atac_udma_cap = 4;
    650        1.1    bouyer 		}
    651        1.1    bouyer 		sc->sc_apo_regbase = APO_AMD_REGBASE;
    652        1.1    bouyer 		break;
    653        1.1    bouyer 	case PCI_VENDOR_NVIDIA:
    654        1.1    bouyer 		switch (sc->sc_pp->ide_product) {
    655        1.1    bouyer 		case PCI_PRODUCT_NVIDIA_NFORCE_ATA100:
    656       1.17   thorpej 			sc->sc_wdcdev.sc_atac.atac_udma_cap = 5;
    657        1.1    bouyer 			break;
    658        1.1    bouyer 		case PCI_PRODUCT_NVIDIA_NFORCE2_ATA133:
    659       1.20  jdolecek 		case PCI_PRODUCT_NVIDIA_NFORCE2_400_ATA133:
    660        1.5      fvdl 		case PCI_PRODUCT_NVIDIA_NFORCE3_ATA133:
    661       1.19   xtraeme 		case PCI_PRODUCT_NVIDIA_NFORCE3_250_ATA133:
    662       1.21      kent 		case PCI_PRODUCT_NVIDIA_NFORCE4_ATA133:
    663       1.28   xtraeme 		case PCI_PRODUCT_NVIDIA_NFORCE430_ATA133:
    664       1.30   xtraeme 		case PCI_PRODUCT_NVIDIA_MCP04_IDE:
    665       1.30   xtraeme 		case PCI_PRODUCT_NVIDIA_MCP55_IDE:
    666       1.33   xtraeme 		case PCI_PRODUCT_NVIDIA_MCP61_IDE:
    667       1.33   xtraeme 		case PCI_PRODUCT_NVIDIA_MCP65_IDE:
    668       1.43   xtraeme 		case PCI_PRODUCT_NVIDIA_MCP67_IDE:
    669       1.47   xtraeme 		case PCI_PRODUCT_NVIDIA_MCP73_IDE:
    670       1.47   xtraeme 		case PCI_PRODUCT_NVIDIA_MCP77_IDE:
    671       1.17   thorpej 			sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    672        1.1    bouyer 			break;
    673        1.1    bouyer 		}
    674        1.1    bouyer 		sc->sc_apo_regbase = APO_NVIDIA_REGBASE;
    675        1.1    bouyer 		break;
    676        1.1    bouyer 	default:
    677        1.1    bouyer 		panic("via_chip_map: unknown vendor");
    678        1.1    bouyer 	}
    679        1.3     enami 
    680       1.53      cube 	aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    681       1.53      cube 	    "bus-master DMA support present");
    682        1.1    bouyer 	pciide_mapreg_dma(sc, pa);
    683       1.39        ad 	aprint_verbose("\n");
    684       1.17   thorpej 	sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
    685        1.1    bouyer 	if (sc->sc_dma_ok) {
    686       1.17   thorpej 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA;
    687        1.1    bouyer 		sc->sc_wdcdev.irqack = pciide_irqack;
    688       1.17   thorpej 		if (sc->sc_wdcdev.sc_atac.atac_udma_cap > 0)
    689       1.17   thorpej 			sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_UDMA;
    690        1.1    bouyer 	}
    691       1.17   thorpej 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
    692       1.17   thorpej 	sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
    693  1.89.34.1  perseant 	if (sc->sc_wdcdev.sc_atac.atac_set_modes == NULL)
    694  1.89.34.1  perseant 		sc->sc_wdcdev.sc_atac.atac_set_modes = via_setup_channel;
    695       1.17   thorpej 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
    696  1.89.34.1  perseant 	if (single_channel)
    697  1.89.34.1  perseant 		sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
    698  1.89.34.1  perseant 	else
    699  1.89.34.1  perseant 		sc->sc_wdcdev.sc_atac.atac_nchannels = PCIIDE_NUM_CHANNELS;
    700       1.83    bouyer 	sc->sc_wdcdev.wdc_maxdrives = 2;
    701        1.1    bouyer 
    702       1.41   garbled 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
    703  1.89.34.1  perseant 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_RAID) {
    704       1.41   garbled 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_RAID;
    705  1.89.34.1  perseant 		if (interface == 0) {
    706  1.89.34.1  perseant 			ATADEBUG_PRINT(("via_chip_map interface == 0\n"),
    707  1.89.34.1  perseant 			    DEBUG_PROBE);
    708  1.89.34.1  perseant 			interface = PCIIDE_INTERFACE_BUS_MASTER_DMA |
    709  1.89.34.1  perseant 			    PCIIDE_INTERFACE_PCI(0) | PCIIDE_INTERFACE_PCI(1);
    710  1.89.34.1  perseant 		}
    711  1.89.34.1  perseant 	}
    712       1.41   garbled 
    713       1.15   thorpej 	wdc_allocate_regs(&sc->sc_wdcdev);
    714       1.15   thorpej 
    715       1.14   thorpej 	ATADEBUG_PRINT(("via_chip_map: old APO_IDECONF=0x%x, "
    716        1.1    bouyer 	    "APO_CTLMISC=0x%x, APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
    717        1.1    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_IDECONF(sc)),
    718        1.1    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_CTLMISC(sc)),
    719        1.1    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM(sc)),
    720        1.1    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA(sc))),
    721        1.1    bouyer 	    DEBUG_PROBE);
    722        1.1    bouyer 
    723        1.1    bouyer 	ideconf = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_IDECONF(sc));
    724  1.89.34.1  perseant 	if ((ideconf & APO_IDECONF_ALWAYS_EN) == 0 && no_ideconf) {
    725  1.89.34.1  perseant 		ideconf |= APO_IDECONF_ALWAYS_EN;
    726  1.89.34.1  perseant 		/*
    727  1.89.34.1  perseant 		 * VT6410 requires writing chip enable register bits to enable
    728  1.89.34.1  perseant 		 * the channels.  It can be distinguished from VT6415 because
    729  1.89.34.1  perseant 		 * its ideconf register returns a non-zero value (contains
    730  1.89.34.1  perseant 		 * other IDE configuration bits).
    731  1.89.34.1  perseant 		 *
    732  1.89.34.1  perseant 		 * VT6415 returns zero at this offset and likely uses it for
    733  1.89.34.1  perseant 		 * a different purpose or leaves it unused. Bits are simulated
    734  1.89.34.1  perseant 		 * without writing, until the actual offset will be known.
    735  1.89.34.1  perseant 		 */
    736  1.89.34.1  perseant 		if (ideconf != APO_IDECONF_ALWAYS_EN) {
    737  1.89.34.1  perseant 			pci_conf_write(sc->sc_pc, sc->sc_tag, APO_IDECONF(sc),
    738  1.89.34.1  perseant 			    ideconf);
    739  1.89.34.1  perseant 			ideconf = pci_conf_read(sc->sc_pc, sc->sc_tag,
    740  1.89.34.1  perseant 			    APO_IDECONF(sc));
    741  1.89.34.1  perseant 		}
    742  1.89.34.1  perseant 	}
    743       1.17   thorpej 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
    744       1.17   thorpej 	     channel++) {
    745        1.1    bouyer 		cp = &sc->pciide_channels[channel];
    746        1.1    bouyer 		if (pciide_chansetup(sc, channel, interface) == 0)
    747        1.1    bouyer 			continue;
    748        1.1    bouyer 
    749        1.1    bouyer 		if ((ideconf & APO_IDECONF_EN(channel)) == 0) {
    750       1.53      cube 			aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    751       1.53      cube 			    "%s channel ignored (disabled)\n", cp->name);
    752       1.15   thorpej 			cp->ata_channel.ch_flags |= ATACH_DISABLED;
    753        1.1    bouyer 			continue;
    754        1.1    bouyer 		}
    755       1.70  jakllsch 		via_mapchan(pa, cp, interface, pciide_pci_intr);
    756        1.1    bouyer 	}
    757        1.1    bouyer }
    758        1.1    bouyer 
    759        1.2   thorpej static void
    760       1.72    dyoung via_mapchan(const struct pci_attach_args *pa,	struct pciide_channel *cp,
    761       1.70  jakllsch     pcireg_t interface, int (*pci_intr)(void *))
    762       1.50       phx {
    763       1.50       phx 	struct ata_channel *wdc_cp;
    764       1.50       phx 	struct pciide_softc *sc;
    765       1.50       phx 	prop_bool_t compat_nat_enable;
    766       1.50       phx 
    767       1.50       phx 	wdc_cp = &cp->ata_channel;
    768       1.50       phx 	sc = CHAN_TO_PCIIDE(&cp->ata_channel);
    769       1.50       phx 	compat_nat_enable = prop_dictionary_get(
    770       1.53      cube 	    device_properties(sc->sc_wdcdev.sc_atac.atac_dev),
    771       1.53      cube 	      "use-compat-native-irq");
    772       1.50       phx 
    773       1.50       phx 	if (interface & PCIIDE_INTERFACE_PCI(wdc_cp->ch_channel)) {
    774       1.50       phx 		/* native mode with irq 14/15 requested? */
    775       1.50       phx 		if (compat_nat_enable != NULL &&
    776       1.50       phx 		    prop_bool_true(compat_nat_enable))
    777       1.70  jakllsch 			via_mapregs_compat_native(pa, cp);
    778       1.50       phx 		else
    779       1.70  jakllsch 			pciide_mapregs_native(pa, cp, pci_intr);
    780       1.50       phx 	} else {
    781       1.70  jakllsch 		pciide_mapregs_compat(pa, cp, wdc_cp->ch_channel);
    782       1.50       phx 		if ((cp->ata_channel.ch_flags & ATACH_DISABLED) == 0)
    783       1.50       phx 			pciide_map_compat_intr(pa, cp, wdc_cp->ch_channel);
    784       1.50       phx 	}
    785       1.50       phx 	wdcattach(wdc_cp);
    786       1.50       phx }
    787       1.50       phx 
    788       1.50       phx /*
    789       1.50       phx  * At least under certain (mis)configurations (e.g. on the "Pegasos" board)
    790       1.50       phx  * the VT8231-IDE's native mode only works with irq 14/15, and cannot be
    791       1.50       phx  * programmed to use a single native PCI irq alone. So we install an interrupt
    792       1.50       phx  * handler for each channel, as in compatibility mode.
    793       1.50       phx  */
    794       1.50       phx static void
    795       1.72    dyoung via_mapregs_compat_native(const struct pci_attach_args *pa,
    796       1.70  jakllsch     struct pciide_channel *cp)
    797       1.50       phx {
    798       1.50       phx 	struct ata_channel *wdc_cp;
    799       1.50       phx 	struct pciide_softc *sc;
    800       1.50       phx 
    801       1.50       phx 	wdc_cp = &cp->ata_channel;
    802       1.50       phx 	sc = CHAN_TO_PCIIDE(&cp->ata_channel);
    803       1.50       phx 
    804       1.50       phx 	/* XXX prevent pciide_mapregs_native from installing a handler */
    805       1.50       phx 	if (sc->sc_pci_ih == NULL)
    806       1.50       phx 		sc->sc_pci_ih = (void *)~0;
    807       1.70  jakllsch 	pciide_mapregs_native(pa, cp, NULL);
    808       1.50       phx 
    809       1.50       phx 	/* interrupts are fixed to 14/15, as in compatibility mode */
    810       1.55       phx 	cp->compat = 1;
    811       1.50       phx 	if ((wdc_cp->ch_flags & ATACH_DISABLED) == 0) {
    812       1.50       phx #ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH
    813       1.50       phx 		cp->ih = pciide_machdep_compat_intr_establish(
    814       1.53      cube 		    sc->sc_wdcdev.sc_atac.atac_dev, pa, wdc_cp->ch_channel,
    815       1.55       phx 		    pciide_compat_intr, cp);
    816       1.50       phx 		if (cp->ih == NULL) {
    817       1.50       phx #endif
    818       1.53      cube 			aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    819       1.53      cube 			    "no compatibility interrupt for "
    820       1.53      cube 			    "use by %s channel\n", cp->name);
    821       1.50       phx 			wdc_cp->ch_flags |= ATACH_DISABLED;
    822       1.50       phx #ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH
    823       1.50       phx 		}
    824       1.50       phx 		sc->sc_pci_ih = cp->ih;  /* XXX */
    825       1.50       phx #endif
    826       1.50       phx 	}
    827       1.50       phx }
    828       1.50       phx 
    829       1.50       phx static void
    830       1.15   thorpej via_setup_channel(struct ata_channel *chp)
    831        1.1    bouyer {
    832        1.1    bouyer 	u_int32_t udmatim_reg, datatim_reg;
    833        1.1    bouyer 	u_int8_t idedma_ctl;
    834       1.18   thorpej 	int mode, drive, s;
    835        1.1    bouyer 	struct ata_drive_datas *drvp;
    836       1.17   thorpej 	struct atac_softc *atac = chp->ch_atac;
    837       1.16   thorpej 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
    838       1.16   thorpej 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    839        1.1    bouyer #ifndef PCIIDE_AMD756_ENABLEDMA
    840        1.1    bouyer 	int rev = PCI_REVISION(
    841        1.1    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
    842        1.1    bouyer #endif
    843        1.1    bouyer 
    844        1.1    bouyer 	idedma_ctl = 0;
    845        1.1    bouyer 	datatim_reg = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM(sc));
    846        1.1    bouyer 	udmatim_reg = pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA(sc));
    847        1.9   thorpej 	datatim_reg &= ~APO_DATATIM_MASK(chp->ch_channel);
    848        1.9   thorpej 	udmatim_reg &= ~APO_UDMA_MASK(chp->ch_channel);
    849        1.1    bouyer 
    850        1.1    bouyer 	/* setup DMA if needed */
    851        1.1    bouyer 	pciide_channel_dma_setup(cp);
    852        1.1    bouyer 
    853        1.1    bouyer 	for (drive = 0; drive < 2; drive++) {
    854        1.1    bouyer 		drvp = &chp->ch_drive[drive];
    855        1.1    bouyer 		/* If no drive, skip */
    856       1.83    bouyer 		if (drvp->drive_type == ATA_DRIVET_NONE)
    857        1.1    bouyer 			continue;
    858        1.1    bouyer 		/* add timing values, setup DMA if needed */
    859       1.83    bouyer 		if (((drvp->drive_flags & ATA_DRIVE_DMA) == 0 &&
    860       1.83    bouyer 		    (drvp->drive_flags & ATA_DRIVE_UDMA) == 0)) {
    861        1.1    bouyer 			mode = drvp->PIO_mode;
    862        1.1    bouyer 			goto pio;
    863        1.1    bouyer 		}
    864       1.17   thorpej 		if ((atac->atac_cap & ATAC_CAP_UDMA) &&
    865       1.83    bouyer 		    (drvp->drive_flags & ATA_DRIVE_UDMA)) {
    866        1.1    bouyer 			/* use Ultra/DMA */
    867       1.18   thorpej 			s = splbio();
    868       1.83    bouyer 			drvp->drive_flags &= ~ATA_DRIVE_DMA;
    869       1.18   thorpej 			splx(s);
    870        1.9   thorpej 			udmatim_reg |= APO_UDMA_EN(chp->ch_channel, drive) |
    871        1.9   thorpej 			    APO_UDMA_EN_MTH(chp->ch_channel, drive);
    872        1.3     enami 			switch (PCI_VENDOR(sc->sc_pci_id)) {
    873        1.1    bouyer 			case PCI_VENDOR_VIATECH:
    874       1.17   thorpej 				if (sc->sc_wdcdev.sc_atac.atac_udma_cap == 6) {
    875        1.1    bouyer 					/* 8233a */
    876        1.1    bouyer 					udmatim_reg |= APO_UDMA_TIME(
    877        1.9   thorpej 					    chp->ch_channel,
    878        1.1    bouyer 					    drive,
    879        1.1    bouyer 					    via_udma133_tim[drvp->UDMA_mode]);
    880       1.17   thorpej 				} else if (sc->sc_wdcdev.sc_atac.atac_udma_cap == 5) {
    881        1.1    bouyer 					/* 686b */
    882        1.1    bouyer 					udmatim_reg |= APO_UDMA_TIME(
    883        1.9   thorpej 					    chp->ch_channel,
    884        1.1    bouyer 					    drive,
    885        1.1    bouyer 					    via_udma100_tim[drvp->UDMA_mode]);
    886       1.17   thorpej 				} else if (sc->sc_wdcdev.sc_atac.atac_udma_cap == 4) {
    887        1.1    bouyer 					/* 596b or 686a */
    888        1.1    bouyer 					udmatim_reg |= APO_UDMA_CLK66(
    889        1.9   thorpej 					    chp->ch_channel);
    890        1.1    bouyer 					udmatim_reg |= APO_UDMA_TIME(
    891        1.9   thorpej 					    chp->ch_channel,
    892        1.1    bouyer 					    drive,
    893        1.1    bouyer 					    via_udma66_tim[drvp->UDMA_mode]);
    894        1.1    bouyer 				} else {
    895        1.1    bouyer 					/* 596a or 586b */
    896        1.1    bouyer 					udmatim_reg |= APO_UDMA_TIME(
    897        1.9   thorpej 					    chp->ch_channel,
    898        1.1    bouyer 					    drive,
    899        1.1    bouyer 					    via_udma33_tim[drvp->UDMA_mode]);
    900        1.1    bouyer 				}
    901        1.1    bouyer 				break;
    902        1.1    bouyer 			case PCI_VENDOR_AMD:
    903        1.1    bouyer 			case PCI_VENDOR_NVIDIA:
    904        1.9   thorpej 				udmatim_reg |= APO_UDMA_TIME(chp->ch_channel,
    905        1.1    bouyer 				    drive, amd7x6_udma_tim[drvp->UDMA_mode]);
    906        1.1    bouyer 				 break;
    907        1.1    bouyer 			}
    908        1.1    bouyer 			/* can use PIO timings, MW DMA unused */
    909        1.1    bouyer 			mode = drvp->PIO_mode;
    910        1.1    bouyer 		} else {
    911        1.1    bouyer 			/* use Multiword DMA, but only if revision is OK */
    912       1.18   thorpej 			s = splbio();
    913       1.83    bouyer 			drvp->drive_flags &= ~ATA_DRIVE_UDMA;
    914       1.18   thorpej 			splx(s);
    915        1.1    bouyer #ifndef PCIIDE_AMD756_ENABLEDMA
    916        1.1    bouyer 			/*
    917        1.1    bouyer 			 * The workaround doesn't seem to be necessary
    918        1.1    bouyer 			 * with all drives, so it can be disabled by
    919        1.1    bouyer 			 * PCIIDE_AMD756_ENABLEDMA. It causes a hard hang if
    920        1.1    bouyer 			 * triggered.
    921        1.1    bouyer 			 */
    922        1.1    bouyer 			if (PCI_VENDOR(sc->sc_pci_id) == PCI_VENDOR_AMD &&
    923        1.1    bouyer 			    sc->sc_pp->ide_product ==
    924        1.3     enami 			    PCI_PRODUCT_AMD_PBC756_IDE &&
    925        1.1    bouyer 			    AMD756_CHIPREV_DISABLEDMA(rev)) {
    926        1.1    bouyer 				aprint_normal(
    927        1.1    bouyer 				    "%s:%d:%d: multi-word DMA disabled due "
    928        1.1    bouyer 				    "to chip revision\n",
    929       1.53      cube 				    device_xname(
    930       1.53      cube 				      sc->sc_wdcdev.sc_atac.atac_dev),
    931        1.9   thorpej 				    chp->ch_channel, drive);
    932        1.1    bouyer 				mode = drvp->PIO_mode;
    933       1.18   thorpej 				s = splbio();
    934       1.83    bouyer 				drvp->drive_flags &= ~ATA_DRIVE_DMA;
    935       1.18   thorpej 				splx(s);
    936        1.1    bouyer 				goto pio;
    937        1.1    bouyer 			}
    938        1.1    bouyer #endif
    939        1.1    bouyer 			/* mode = min(pio, dma+2) */
    940        1.3     enami 			if (drvp->PIO_mode <= (drvp->DMA_mode + 2))
    941        1.1    bouyer 				mode = drvp->PIO_mode;
    942        1.1    bouyer 			else
    943        1.1    bouyer 				mode = drvp->DMA_mode + 2;
    944        1.1    bouyer 		}
    945        1.1    bouyer 		idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    946        1.1    bouyer 
    947        1.1    bouyer pio:		/* setup PIO mode */
    948        1.1    bouyer 		if (mode <= 2) {
    949        1.1    bouyer 			drvp->DMA_mode = 0;
    950        1.1    bouyer 			drvp->PIO_mode = 0;
    951        1.1    bouyer 			mode = 0;
    952        1.1    bouyer 		} else {
    953        1.1    bouyer 			drvp->PIO_mode = mode;
    954        1.1    bouyer 			drvp->DMA_mode = mode - 2;
    955        1.1    bouyer 		}
    956        1.1    bouyer 		datatim_reg |=
    957        1.9   thorpej 		    APO_DATATIM_PULSE(chp->ch_channel, drive,
    958        1.1    bouyer 			apollo_pio_set[mode]) |
    959        1.9   thorpej 		    APO_DATATIM_RECOV(chp->ch_channel, drive,
    960        1.1    bouyer 			apollo_pio_rec[mode]);
    961        1.1    bouyer 	}
    962        1.1    bouyer 	if (idedma_ctl != 0) {
    963        1.1    bouyer 		/* Add software bits in status register */
    964        1.7      fvdl 		bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
    965        1.1    bouyer 		    idedma_ctl);
    966        1.1    bouyer 	}
    967        1.1    bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, APO_DATATIM(sc), datatim_reg);
    968        1.1    bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, APO_UDMA(sc), udmatim_reg);
    969  1.89.34.1  perseant 	ATADEBUG_PRINT(("%s: APO_DATATIM=0x%x, APO_UDMA=0x%x\n",
    970  1.89.34.1  perseant 	    __func__,
    971        1.1    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_DATATIM(sc)),
    972        1.1    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, APO_UDMA(sc))), DEBUG_PROBE);
    973        1.1    bouyer }
    974        1.1    bouyer 
    975       1.35    bouyer static int
    976       1.76  jakllsch via_sata_chip_map_common(struct pciide_softc *sc,
    977       1.76  jakllsch     const struct pci_attach_args *cpa)
    978        1.1    bouyer {
    979       1.74    dyoung 	pcireg_t csr;
    980       1.36    bouyer 	int maptype, ret;
    981       1.76  jakllsch 	struct pci_attach_args pac, *pa = &pac;
    982       1.76  jakllsch 
    983       1.76  jakllsch 	pac = *cpa;
    984        1.1    bouyer 
    985        1.1    bouyer 	if (pciide_chipen(sc, pa) == 0)
    986       1.35    bouyer 		return 0;
    987        1.1    bouyer 
    988       1.53      cube 	aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    989       1.53      cube 	    "bus-master DMA support present");
    990        1.1    bouyer 	pciide_mapreg_dma(sc, pa);
    991       1.39        ad 	aprint_verbose("\n");
    992        1.1    bouyer 
    993        1.1    bouyer 	if (sc->sc_dma_ok) {
    994       1.17   thorpej 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_UDMA | ATAC_CAP_DMA;
    995        1.1    bouyer 		sc->sc_wdcdev.irqack = pciide_irqack;
    996        1.1    bouyer 	}
    997       1.17   thorpej 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
    998       1.17   thorpej 	sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
    999       1.17   thorpej 	sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
   1000       1.17   thorpej 
   1001       1.17   thorpej 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
   1002       1.17   thorpej 	sc->sc_wdcdev.sc_atac.atac_nchannels = PCIIDE_NUM_CHANNELS;
   1003       1.17   thorpej 	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
   1004       1.17   thorpej 	sc->sc_wdcdev.sc_atac.atac_set_modes = sata_setup_channel;
   1005       1.83    bouyer 	sc->sc_wdcdev.wdc_maxdrives = 2;
   1006        1.1    bouyer 
   1007       1.41   garbled 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
   1008       1.41   garbled 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_RAID)
   1009       1.41   garbled 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_RAID;
   1010       1.41   garbled 
   1011       1.15   thorpej 	wdc_allocate_regs(&sc->sc_wdcdev);
   1012       1.36    bouyer 	maptype = pci_mapreg_type(pa->pa_pc, pa->pa_tag,
   1013       1.36    bouyer 	    PCI_MAPREG_START + 0x14);
   1014       1.36    bouyer 	switch(maptype) {
   1015       1.36    bouyer 	case PCI_MAPREG_TYPE_IO:
   1016       1.36    bouyer 		ret = pci_mapreg_map(pa, PCI_MAPREG_START + 0x14,
   1017       1.36    bouyer 		    PCI_MAPREG_TYPE_IO, 0, &sc->sc_ba5_st, &sc->sc_ba5_sh,
   1018       1.70  jakllsch 		    NULL, &sc->sc_ba5_ss);
   1019       1.36    bouyer 		break;
   1020       1.36    bouyer 	case PCI_MAPREG_MEM_TYPE_32BIT:
   1021       1.69    dyoung 		/*
   1022       1.69    dyoung 		 * Enable memory-space access if it isn't already there.
   1023       1.69    dyoung 		 */
   1024       1.74    dyoung 		csr = pci_conf_read(pa->pa_pc, pa->pa_tag,
   1025       1.74    dyoung 		    PCI_COMMAND_STATUS_REG);
   1026       1.74    dyoung 		if ((csr & PCI_COMMAND_MEM_ENABLE) == 0 &&
   1027       1.75    dyoung 		    (pa->pa_flags & PCI_FLAGS_MEM_OKAY) != 0) {
   1028       1.69    dyoung 
   1029       1.69    dyoung 			pci_conf_write(pa->pa_pc, pa->pa_tag,
   1030       1.69    dyoung 			    PCI_COMMAND_STATUS_REG,
   1031       1.69    dyoung 			    csr | PCI_COMMAND_MEM_ENABLE);
   1032       1.69    dyoung 		}
   1033       1.69    dyoung 
   1034       1.36    bouyer 		ret = pci_mapreg_map(pa, PCI_MAPREG_START + 0x14,
   1035       1.35    bouyer 		    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
   1036       1.35    bouyer 		    0, &sc->sc_ba5_st, &sc->sc_ba5_sh,
   1037       1.70  jakllsch 		    NULL, &sc->sc_ba5_ss);
   1038       1.36    bouyer 		break;
   1039       1.36    bouyer 	default:
   1040       1.53      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
   1041       1.54  dholland 		    "couldn't map sata regs, unsupported maptype (0x%x)\n",
   1042       1.36    bouyer 		    maptype);
   1043       1.36    bouyer 		return 0;
   1044       1.36    bouyer 	}
   1045       1.36    bouyer 	if (ret != 0) {
   1046       1.53      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
   1047       1.53      cube 		    "couldn't map sata regs\n");
   1048       1.36    bouyer 		return 0;
   1049       1.35    bouyer 	}
   1050       1.35    bouyer 	return 1;
   1051       1.35    bouyer }
   1052       1.35    bouyer 
   1053       1.35    bouyer static void
   1054       1.76  jakllsch via_sata_chip_map(struct pciide_softc *sc, const struct pci_attach_args *pa,
   1055       1.35    bouyer     int satareg_shift)
   1056       1.35    bouyer {
   1057       1.35    bouyer 	struct pciide_channel *cp;
   1058       1.35    bouyer 	struct ata_channel *wdc_cp;
   1059       1.35    bouyer 	struct wdc_regs *wdr;
   1060       1.73  jakllsch 	pcireg_t interface;
   1061       1.35    bouyer 	int channel;
   1062       1.35    bouyer 
   1063       1.73  jakllsch 	interface = PCI_INTERFACE(pa->pa_class);
   1064       1.73  jakllsch 
   1065       1.35    bouyer 	if (via_sata_chip_map_common(sc, pa) == 0)
   1066       1.35    bouyer 		return;
   1067       1.35    bouyer 
   1068       1.35    bouyer 	if (interface == 0) {
   1069       1.35    bouyer 		ATADEBUG_PRINT(("via_sata_chip_map interface == 0\n"),
   1070       1.35    bouyer 		    DEBUG_PROBE);
   1071       1.35    bouyer 		interface = PCIIDE_INTERFACE_BUS_MASTER_DMA |
   1072       1.35    bouyer 		    PCIIDE_INTERFACE_PCI(0) | PCIIDE_INTERFACE_PCI(1);
   1073       1.35    bouyer 	}
   1074       1.15   thorpej 
   1075       1.83    bouyer 	sc->sc_wdcdev.sc_atac.atac_probe = wdc_sataprobe;
   1076       1.83    bouyer 	sc->sc_wdcdev.wdc_maxdrives = 1;
   1077       1.17   thorpej 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
   1078       1.17   thorpej 	     channel++) {
   1079        1.1    bouyer 		cp = &sc->pciide_channels[channel];
   1080        1.1    bouyer 		if (pciide_chansetup(sc, channel, interface) == 0)
   1081        1.1    bouyer 			continue;
   1082       1.35    bouyer 		wdc_cp = &cp->ata_channel;
   1083       1.35    bouyer 		wdr = CHAN_TO_WDC_REGS(wdc_cp);
   1084       1.35    bouyer 		wdr->sata_iot = sc->sc_ba5_st;
   1085       1.35    bouyer 		wdr->sata_baseioh = sc->sc_ba5_sh;
   1086       1.35    bouyer 		if (bus_space_subregion(wdr->sata_iot, wdr->sata_baseioh,
   1087       1.64  jakllsch 		    (wdc_cp->ch_channel << satareg_shift) + 0x0, 4,
   1088       1.35    bouyer 		    &wdr->sata_status) != 0) {
   1089       1.53      cube 			aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
   1090       1.53      cube 			    "couldn't map channel %d sata_status regs\n",
   1091       1.35    bouyer 			    wdc_cp->ch_channel);
   1092       1.35    bouyer 			continue;
   1093       1.35    bouyer 		}
   1094       1.35    bouyer 		if (bus_space_subregion(wdr->sata_iot, wdr->sata_baseioh,
   1095       1.64  jakllsch 		    (wdc_cp->ch_channel << satareg_shift) + 0x4, 4,
   1096       1.35    bouyer 		    &wdr->sata_error) != 0) {
   1097       1.53      cube 			aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
   1098       1.53      cube 			    "couldn't map channel %d sata_error regs\n",
   1099       1.35    bouyer 			    wdc_cp->ch_channel);
   1100       1.35    bouyer 			continue;
   1101       1.35    bouyer 		}
   1102       1.35    bouyer 		if (bus_space_subregion(wdr->sata_iot, wdr->sata_baseioh,
   1103       1.64  jakllsch 		    (wdc_cp->ch_channel << satareg_shift) + 0x8, 4,
   1104       1.35    bouyer 		    &wdr->sata_control) != 0) {
   1105       1.53      cube 			aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
   1106       1.53      cube 			    "couldn't map channel %d sata_control regs\n",
   1107       1.35    bouyer 			    wdc_cp->ch_channel);
   1108       1.35    bouyer 			continue;
   1109       1.35    bouyer 		}
   1110       1.70  jakllsch 		pciide_mapchan(pa, cp, interface, pciide_pci_intr);
   1111        1.1    bouyer 	}
   1112        1.1    bouyer }
   1113       1.35    bouyer 
   1114       1.35    bouyer static void
   1115       1.72    dyoung via_sata_chip_map_6(struct pciide_softc *sc, const struct pci_attach_args *pa)
   1116       1.35    bouyer {
   1117       1.35    bouyer 	via_sata_chip_map(sc, pa, 6);
   1118       1.35    bouyer }
   1119       1.35    bouyer 
   1120       1.35    bouyer static void
   1121       1.72    dyoung via_sata_chip_map_7(struct pciide_softc *sc, const struct pci_attach_args *pa)
   1122       1.35    bouyer {
   1123       1.35    bouyer 	via_sata_chip_map(sc, pa, 7);
   1124       1.35    bouyer }
   1125       1.35    bouyer 
   1126       1.35    bouyer static void
   1127       1.76  jakllsch via_vt6421_mapreg_dma(struct pciide_softc *sc, const struct pci_attach_args *pa)
   1128       1.76  jakllsch {
   1129       1.76  jakllsch 	struct pciide_channel *pc;
   1130       1.76  jakllsch 	int chan, reg;
   1131       1.76  jakllsch 	bus_size_t size;
   1132       1.76  jakllsch 
   1133       1.76  jakllsch 	sc->sc_dma_ok = (pci_mapreg_map(pa, PCIIDE_REG_BUS_MASTER_DMA,
   1134       1.76  jakllsch 	    PCI_MAPREG_TYPE_IO, 0, &sc->sc_dma_iot, &sc->sc_dma_ioh,
   1135       1.76  jakllsch 	    NULL, &sc->sc_dma_ios) == 0);
   1136       1.76  jakllsch 	sc->sc_dmat = pa->pa_dmat;
   1137       1.76  jakllsch 	if (sc->sc_dma_ok == 0) {
   1138       1.76  jakllsch 		aprint_verbose(", but unused (couldn't map registers)");
   1139       1.76  jakllsch 	} else {
   1140       1.76  jakllsch 		sc->sc_wdcdev.dma_arg = sc;
   1141       1.76  jakllsch 		sc->sc_wdcdev.dma_init = pciide_dma_init;
   1142       1.76  jakllsch 		sc->sc_wdcdev.dma_start = pciide_dma_start;
   1143       1.76  jakllsch 		sc->sc_wdcdev.dma_finish = pciide_dma_finish;
   1144       1.76  jakllsch 	}
   1145       1.76  jakllsch 
   1146       1.76  jakllsch 	if (device_cfdata(sc->sc_wdcdev.sc_atac.atac_dev)->cf_flags &
   1147       1.76  jakllsch 	    PCIIDE_OPTIONS_NODMA) {
   1148       1.76  jakllsch 		aprint_verbose(
   1149       1.76  jakllsch 		    ", but unused (forced off by config file)");
   1150       1.76  jakllsch 		sc->sc_dma_ok = 0;
   1151       1.76  jakllsch 	}
   1152       1.76  jakllsch 
   1153       1.76  jakllsch 	if (sc->sc_dma_ok == 0)
   1154       1.76  jakllsch 		return;
   1155       1.76  jakllsch 
   1156       1.76  jakllsch 	for (chan = 0; chan < 4; chan++) {
   1157       1.76  jakllsch 		pc = &sc->pciide_channels[chan];
   1158       1.76  jakllsch 		for (reg = 0; reg < IDEDMA_NREGS; reg++) {
   1159       1.76  jakllsch 			size = 4;
   1160       1.76  jakllsch 			if (size > (IDEDMA_SCH_OFFSET - reg))
   1161       1.76  jakllsch 				size = IDEDMA_SCH_OFFSET - reg;
   1162       1.76  jakllsch 			if (bus_space_subregion(sc->sc_dma_iot, sc->sc_dma_ioh,
   1163       1.76  jakllsch 			    IDEDMA_SCH_OFFSET * chan + reg, size,
   1164       1.76  jakllsch 			    &pc->dma_iohs[reg]) != 0) {
   1165       1.76  jakllsch 				sc->sc_dma_ok = 0;
   1166       1.76  jakllsch 				aprint_verbose(", but can't subregion offset "
   1167       1.76  jakllsch 				               "%d size %lu",
   1168       1.76  jakllsch 					       reg, (u_long)size);
   1169       1.76  jakllsch 				return;
   1170       1.76  jakllsch 			}
   1171       1.76  jakllsch 		}
   1172       1.76  jakllsch 	}
   1173       1.76  jakllsch }
   1174       1.76  jakllsch 
   1175       1.76  jakllsch static int
   1176       1.76  jakllsch via_vt6421_chansetup(struct pciide_softc *sc, int channel)
   1177       1.76  jakllsch {
   1178       1.76  jakllsch 	struct pciide_channel *cp = &sc->pciide_channels[channel];
   1179       1.76  jakllsch 
   1180       1.76  jakllsch 	sc->wdc_chanarray[channel] = &cp->ata_channel;
   1181       1.76  jakllsch 
   1182       1.76  jakllsch 	cp->ata_channel.ch_channel = channel;
   1183       1.76  jakllsch 	cp->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
   1184       1.86  jdolecek 
   1185       1.76  jakllsch 	return 1;
   1186       1.76  jakllsch }
   1187       1.76  jakllsch 
   1188       1.76  jakllsch static void
   1189       1.72    dyoung via_sata_chip_map_new(struct pciide_softc *sc,
   1190       1.76  jakllsch     const struct pci_attach_args *pa)
   1191       1.35    bouyer {
   1192       1.35    bouyer 	struct pciide_channel *cp;
   1193       1.35    bouyer 	struct ata_channel *wdc_cp;
   1194       1.35    bouyer 	struct wdc_regs *wdr;
   1195       1.35    bouyer 	int channel;
   1196       1.35    bouyer 	pci_intr_handle_t intrhandle;
   1197       1.35    bouyer 	const char *intrstr;
   1198       1.35    bouyer 	int i;
   1199       1.84  christos 	char intrbuf[PCI_INTRSTR_LEN];
   1200       1.35    bouyer 
   1201       1.76  jakllsch 	if (pciide_chipen(sc, pa) == 0)
   1202       1.76  jakllsch 		return;
   1203       1.76  jakllsch 
   1204       1.76  jakllsch 	sc->sc_apo_regbase = APO_VIA_VT6421_REGBASE;
   1205       1.76  jakllsch 
   1206       1.76  jakllsch 	if (pci_mapreg_map(pa, PCI_BAR(5), PCI_MAPREG_TYPE_IO, 0,
   1207       1.76  jakllsch 	    &sc->sc_ba5_st, &sc->sc_ba5_sh, NULL, &sc->sc_ba5_ss) != 0) {
   1208       1.76  jakllsch 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
   1209       1.76  jakllsch 		    "couldn't map SATA regs\n");
   1210       1.76  jakllsch 	}
   1211       1.73  jakllsch 
   1212       1.76  jakllsch 	aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
   1213       1.76  jakllsch 	    "bus-master DMA support present");
   1214       1.76  jakllsch 	via_vt6421_mapreg_dma(sc, pa);
   1215       1.76  jakllsch 	aprint_verbose("\n");
   1216       1.35    bouyer 
   1217       1.76  jakllsch 	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
   1218       1.76  jakllsch 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
   1219       1.76  jakllsch 	if (sc->sc_dma_ok) {
   1220       1.76  jakllsch 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
   1221       1.76  jakllsch 		sc->sc_wdcdev.irqack = pciide_irqack;
   1222       1.76  jakllsch 		sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
   1223       1.76  jakllsch 		sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
   1224       1.35    bouyer 	}
   1225       1.76  jakllsch 	sc->sc_wdcdev.sc_atac.atac_set_modes = sata_setup_channel;
   1226  1.89.34.1  perseant 
   1227       1.76  jakllsch 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
   1228       1.76  jakllsch 	sc->sc_wdcdev.sc_atac.atac_nchannels = 3;
   1229       1.83    bouyer 	sc->sc_wdcdev.wdc_maxdrives = 2;
   1230       1.76  jakllsch 
   1231  1.89.34.1  perseant 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
   1232  1.89.34.1  perseant 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_RAID)
   1233  1.89.34.1  perseant 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_RAID;
   1234  1.89.34.1  perseant 
   1235       1.76  jakllsch 	wdc_allocate_regs(&sc->sc_wdcdev);
   1236       1.35    bouyer 
   1237       1.35    bouyer 	if (pci_intr_map(pa, &intrhandle) != 0) {
   1238       1.53      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
   1239       1.53      cube 		    "couldn't map native-PCI interrupt\n");
   1240       1.35    bouyer 		return;
   1241       1.35    bouyer 	}
   1242       1.84  christos 	intrstr = pci_intr_string(pa->pa_pc, intrhandle, intrbuf, sizeof(intrbuf));
   1243       1.87  jdolecek 	sc->sc_pci_ih = pci_intr_establish_xname(pa->pa_pc,
   1244       1.87  jdolecek 	    intrhandle, IPL_BIO, pciide_pci_intr, sc,
   1245       1.87  jdolecek 	    device_xname(sc->sc_wdcdev.sc_atac.atac_dev));
   1246       1.35    bouyer 	if (sc->sc_pci_ih == NULL) {
   1247       1.53      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
   1248       1.53      cube 		    "couldn't establish native-PCI interrupt");
   1249       1.35    bouyer 		if (intrstr != NULL)
   1250       1.35    bouyer 		    aprint_error(" at %s", intrstr);
   1251       1.35    bouyer 		aprint_error("\n");
   1252       1.35    bouyer 		return;
   1253       1.35    bouyer 	}
   1254       1.53      cube 	aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev,
   1255       1.53      cube 	    "using %s for native-PCI interrupt\n",
   1256       1.35    bouyer 	    intrstr ? intrstr : "unknown interrupt");
   1257       1.35    bouyer 
   1258       1.35    bouyer 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
   1259       1.35    bouyer 	     channel++) {
   1260       1.35    bouyer 		cp = &sc->pciide_channels[channel];
   1261       1.76  jakllsch 		if (via_vt6421_chansetup(sc, channel) == 0)
   1262       1.35    bouyer 			continue;
   1263       1.35    bouyer 		wdc_cp = &cp->ata_channel;
   1264       1.35    bouyer 		wdr = CHAN_TO_WDC_REGS(wdc_cp);
   1265       1.35    bouyer 
   1266       1.35    bouyer 		wdr->sata_iot = sc->sc_ba5_st;
   1267       1.35    bouyer 		wdr->sata_baseioh = sc->sc_ba5_sh;
   1268       1.35    bouyer 		if (bus_space_subregion(wdr->sata_iot, wdr->sata_baseioh,
   1269       1.64  jakllsch 		    (wdc_cp->ch_channel << 6) + 0x0, 4,
   1270       1.35    bouyer 		    &wdr->sata_status) != 0) {
   1271       1.53      cube 			aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
   1272       1.53      cube 			    "couldn't map channel %d sata_status regs\n",
   1273       1.35    bouyer 			    wdc_cp->ch_channel);
   1274       1.35    bouyer 			continue;
   1275       1.35    bouyer 		}
   1276       1.35    bouyer 		if (bus_space_subregion(wdr->sata_iot, wdr->sata_baseioh,
   1277       1.64  jakllsch 		    (wdc_cp->ch_channel << 6) + 0x4, 4,
   1278       1.35    bouyer 		    &wdr->sata_error) != 0) {
   1279       1.53      cube 			aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
   1280       1.53      cube 			    "couldn't map channel %d sata_error regs\n",
   1281       1.35    bouyer 			    wdc_cp->ch_channel);
   1282       1.35    bouyer 			continue;
   1283       1.35    bouyer 		}
   1284       1.35    bouyer 		if (bus_space_subregion(wdr->sata_iot, wdr->sata_baseioh,
   1285       1.64  jakllsch 		    (wdc_cp->ch_channel << 6) + 0x8, 4,
   1286       1.35    bouyer 		    &wdr->sata_control) != 0) {
   1287       1.53      cube 			aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
   1288       1.53      cube 			    "couldn't map channel %d sata_control regs\n",
   1289       1.35    bouyer 			    wdc_cp->ch_channel);
   1290       1.35    bouyer 			continue;
   1291       1.35    bouyer 		}
   1292       1.35    bouyer 
   1293       1.76  jakllsch 		if (pci_mapreg_map(pa, PCI_BAR(wdc_cp->ch_channel),
   1294       1.35    bouyer 		    PCI_MAPREG_TYPE_IO, 0, &wdr->cmd_iot, &wdr->cmd_baseioh,
   1295       1.70  jakllsch 		    NULL, &wdr->cmd_ios) != 0) {
   1296       1.53      cube 			aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
   1297       1.53      cube 			    "couldn't map %s channel regs\n", cp->name);
   1298       1.35    bouyer 		}
   1299       1.35    bouyer 		wdr->ctl_iot = wdr->cmd_iot;
   1300       1.35    bouyer 		for (i = 0; i < WDC_NREG; i++) {
   1301       1.35    bouyer 			if (bus_space_subregion(wdr->cmd_iot,
   1302       1.35    bouyer 			    wdr->cmd_baseioh, i, i == 0 ? 4 : 1,
   1303       1.35    bouyer 			    &wdr->cmd_iohs[i]) != 0) {
   1304       1.53      cube 				aprint_error_dev(
   1305       1.53      cube 				    sc->sc_wdcdev.sc_atac.atac_dev,
   1306       1.53      cube 				    "couldn't subregion %s "
   1307       1.53      cube 				    "channel cmd regs\n", cp->name);
   1308       1.35    bouyer 				return;
   1309       1.35    bouyer 			}
   1310       1.35    bouyer 		}
   1311       1.35    bouyer 		if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
   1312       1.35    bouyer 		    WDC_NREG + 2, 1,  &wdr->ctl_ioh) != 0) {
   1313       1.53      cube 			aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
   1314       1.53      cube 			    "couldn't map channel %d ctl regs\n", channel);
   1315       1.35    bouyer 			return;
   1316       1.35    bouyer 		}
   1317       1.85  jdolecek 		wdc_init_shadow_regs(wdr);
   1318       1.65   tsutsui 		wdr->data32iot = wdr->cmd_iot;
   1319       1.65   tsutsui 		wdr->data32ioh = wdr->cmd_iohs[wd_data];
   1320       1.35    bouyer 		wdcattach(wdc_cp);
   1321       1.35    bouyer 	}
   1322       1.35    bouyer }
   1323