Home | History | Annotate | Line # | Download | only in pci
satalink.c revision 1.42
      1  1.42  jakllsch /*	$NetBSD: satalink.c,v 1.42 2010/11/05 18:07:24 jakllsch Exp $	*/
      2   1.2   thorpej 
      3   1.2   thorpej /*-
      4   1.2   thorpej  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5   1.2   thorpej  * All rights reserved.
      6   1.2   thorpej  *
      7   1.2   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.2   thorpej  * by Jason R. Thorpe of Wasabi Systems, Inc.
      9   1.2   thorpej  *
     10   1.2   thorpej  * Redistribution and use in source and binary forms, with or without
     11   1.2   thorpej  * modification, are permitted provided that the following conditions
     12   1.2   thorpej  * are met:
     13   1.2   thorpej  * 1. Redistributions of source code must retain the above copyright
     14   1.2   thorpej  *    notice, this list of conditions and the following disclaimer.
     15   1.2   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.2   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17   1.2   thorpej  *    documentation and/or other materials provided with the distribution.
     18   1.2   thorpej  *
     19   1.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.2   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.2   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.2   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.2   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.2   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.2   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.2   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.2   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.2   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.2   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1   thorpej  */
     31   1.1   thorpej 
     32  1.30   xtraeme #include <sys/cdefs.h>
     33  1.42  jakllsch __KERNEL_RCSID(0, "$NetBSD: satalink.c,v 1.42 2010/11/05 18:07:24 jakllsch Exp $");
     34  1.30   xtraeme 
     35   1.1   thorpej #include <sys/param.h>
     36   1.1   thorpej #include <sys/systm.h>
     37   1.1   thorpej #include <sys/malloc.h>
     38   1.1   thorpej 
     39   1.1   thorpej #include <dev/pci/pcivar.h>
     40   1.1   thorpej #include <dev/pci/pcidevs.h>
     41   1.1   thorpej #include <dev/pci/pciidereg.h>
     42   1.1   thorpej #include <dev/pci/pciidevar.h>
     43   1.1   thorpej #include <dev/pci/pciide_sii3112_reg.h>
     44   1.1   thorpej 
     45   1.2   thorpej #include <dev/ata/satareg.h>
     46  1.15   thorpej #include <dev/ata/satavar.h>
     47   1.8    martin #include <dev/ata/atareg.h>
     48   1.1   thorpej 
     49   1.4   thorpej /*
     50   1.4   thorpej  * Register map for BA5 register space, indexed by channel.
     51   1.4   thorpej  */
     52   1.4   thorpej static const struct {
     53   1.4   thorpej 	bus_addr_t	ba5_IDEDMA_CMD;
     54   1.4   thorpej 	bus_addr_t	ba5_IDEDMA_CTL;
     55   1.4   thorpej 	bus_addr_t	ba5_IDEDMA_TBL;
     56   1.4   thorpej 	bus_addr_t	ba5_IDEDMA_CMD2;
     57   1.4   thorpej 	bus_addr_t	ba5_IDEDMA_CTL2;
     58   1.4   thorpej 	bus_addr_t	ba5_IDE_TF0;
     59   1.4   thorpej 	bus_addr_t	ba5_IDE_TF1;
     60   1.4   thorpej 	bus_addr_t	ba5_IDE_TF2;
     61   1.4   thorpej 	bus_addr_t	ba5_IDE_TF3;
     62   1.4   thorpej 	bus_addr_t	ba5_IDE_TF4;
     63   1.4   thorpej 	bus_addr_t	ba5_IDE_TF5;
     64   1.4   thorpej 	bus_addr_t	ba5_IDE_TF6;
     65   1.4   thorpej 	bus_addr_t	ba5_IDE_TF7;
     66   1.4   thorpej 	bus_addr_t	ba5_IDE_TF8;
     67   1.4   thorpej 	bus_addr_t	ba5_IDE_RAD;
     68   1.4   thorpej 	bus_addr_t	ba5_IDE_TF9;
     69   1.4   thorpej 	bus_addr_t	ba5_IDE_TF10;
     70   1.4   thorpej 	bus_addr_t	ba5_IDE_TF11;
     71   1.4   thorpej 	bus_addr_t	ba5_IDE_TF12;
     72   1.4   thorpej 	bus_addr_t	ba5_IDE_TF13;
     73   1.4   thorpej 	bus_addr_t	ba5_IDE_TF14;
     74   1.4   thorpej 	bus_addr_t	ba5_IDE_TF15;
     75   1.4   thorpej 	bus_addr_t	ba5_IDE_TF16;
     76   1.4   thorpej 	bus_addr_t	ba5_IDE_TF17;
     77   1.4   thorpej 	bus_addr_t	ba5_IDE_TF18;
     78   1.4   thorpej 	bus_addr_t	ba5_IDE_TF19;
     79   1.4   thorpej 	bus_addr_t	ba5_IDE_RABC;
     80   1.4   thorpej 	bus_addr_t	ba5_IDE_CMD_STS;
     81   1.4   thorpej 	bus_addr_t	ba5_IDE_CFG_STS;
     82   1.4   thorpej 	bus_addr_t	ba5_IDE_DTM;
     83   1.4   thorpej 	bus_addr_t	ba5_SControl;
     84   1.4   thorpej 	bus_addr_t	ba5_SStatus;
     85   1.4   thorpej 	bus_addr_t	ba5_SError;
     86   1.5   thorpej 	bus_addr_t	ba5_SActive;		/* 3114 */
     87   1.5   thorpej 	bus_addr_t	ba5_SMisc;
     88   1.5   thorpej 	bus_addr_t	ba5_PHY_CONFIG;
     89   1.5   thorpej 	bus_addr_t	ba5_SIEN;
     90   1.5   thorpej 	bus_addr_t	ba5_SFISCfg;
     91   1.4   thorpej } satalink_ba5_regmap[] = {
     92   1.5   thorpej 	{	/* Channel 0 */
     93   1.4   thorpej 		.ba5_IDEDMA_CMD		=	0x000,
     94   1.4   thorpej 		.ba5_IDEDMA_CTL		=	0x002,
     95   1.4   thorpej 		.ba5_IDEDMA_TBL		=	0x004,
     96   1.4   thorpej 		.ba5_IDEDMA_CMD2	=	0x010,
     97   1.4   thorpej 		.ba5_IDEDMA_CTL2	=	0x012,
     98   1.4   thorpej 		.ba5_IDE_TF0		=	0x080,	/* wd_data */
     99   1.4   thorpej 		.ba5_IDE_TF1		=	0x081,	/* wd_error */
    100   1.4   thorpej 		.ba5_IDE_TF2		=	0x082,	/* wd_seccnt */
    101   1.4   thorpej 		.ba5_IDE_TF3		=	0x083,	/* wd_sector */
    102   1.4   thorpej 		.ba5_IDE_TF4		=	0x084,	/* wd_cyl_lo */
    103   1.4   thorpej 		.ba5_IDE_TF5		=	0x085,	/* wd_cyl_hi */
    104   1.4   thorpej 		.ba5_IDE_TF6		=	0x086,	/* wd_sdh */
    105   1.4   thorpej 		.ba5_IDE_TF7		=	0x087,	/* wd_command */
    106   1.4   thorpej 		.ba5_IDE_TF8		=	0x08a,	/* wd_altsts */
    107   1.4   thorpej 		.ba5_IDE_RAD		=	0x08c,
    108   1.4   thorpej 		.ba5_IDE_TF9		=	0x091,	/* Features 2 */
    109   1.4   thorpej 		.ba5_IDE_TF10		=	0x092,	/* Sector Count 2 */
    110   1.4   thorpej 		.ba5_IDE_TF11		=	0x093,	/* Start Sector 2 */
    111   1.4   thorpej 		.ba5_IDE_TF12		=	0x094,	/* Cylinder Low 2 */
    112   1.4   thorpej 		.ba5_IDE_TF13		=	0x095,	/* Cylinder High 2 */
    113   1.4   thorpej 		.ba5_IDE_TF14		=	0x096,	/* Device/Head 2 */
    114   1.4   thorpej 		.ba5_IDE_TF15		=	0x097,	/* Cmd Sts 2 */
    115   1.4   thorpej 		.ba5_IDE_TF16		=	0x098,	/* Sector Count 2 ext */
    116   1.4   thorpej 		.ba5_IDE_TF17		=	0x099,	/* Start Sector 2 ext */
    117   1.4   thorpej 		.ba5_IDE_TF18		=	0x09a,	/* Cyl Low 2 ext */
    118   1.4   thorpej 		.ba5_IDE_TF19		=	0x09b,	/* Cyl High 2 ext */
    119   1.4   thorpej 		.ba5_IDE_RABC		=	0x09c,
    120   1.4   thorpej 		.ba5_IDE_CMD_STS	=	0x0a0,
    121   1.4   thorpej 		.ba5_IDE_CFG_STS	=	0x0a1,
    122   1.4   thorpej 		.ba5_IDE_DTM		=	0x0b4,
    123   1.4   thorpej 		.ba5_SControl		=	0x100,
    124   1.4   thorpej 		.ba5_SStatus		=	0x104,
    125   1.4   thorpej 		.ba5_SError		=	0x108,
    126   1.5   thorpej 		.ba5_SActive		=	0x10c,
    127   1.5   thorpej 		.ba5_SMisc		=	0x140,
    128   1.5   thorpej 		.ba5_PHY_CONFIG		=	0x144,
    129   1.5   thorpej 		.ba5_SIEN		=	0x148,
    130   1.5   thorpej 		.ba5_SFISCfg		=	0x14c,
    131   1.4   thorpej 	},
    132   1.5   thorpej 	{	/* Channel 1 */
    133   1.4   thorpej 		.ba5_IDEDMA_CMD		=	0x008,
    134   1.4   thorpej 		.ba5_IDEDMA_CTL		=	0x00a,
    135   1.4   thorpej 		.ba5_IDEDMA_TBL		=	0x00c,
    136   1.4   thorpej 		.ba5_IDEDMA_CMD2	=	0x018,
    137   1.4   thorpej 		.ba5_IDEDMA_CTL2	=	0x01a,
    138   1.4   thorpej 		.ba5_IDE_TF0		=	0x0c0,	/* wd_data */
    139   1.4   thorpej 		.ba5_IDE_TF1		=	0x0c1,	/* wd_error */
    140   1.4   thorpej 		.ba5_IDE_TF2		=	0x0c2,	/* wd_seccnt */
    141   1.4   thorpej 		.ba5_IDE_TF3		=	0x0c3,	/* wd_sector */
    142   1.4   thorpej 		.ba5_IDE_TF4		=	0x0c4,	/* wd_cyl_lo */
    143   1.4   thorpej 		.ba5_IDE_TF5		=	0x0c5,	/* wd_cyl_hi */
    144   1.4   thorpej 		.ba5_IDE_TF6		=	0x0c6,	/* wd_sdh */
    145   1.4   thorpej 		.ba5_IDE_TF7		=	0x0c7,	/* wd_command */
    146   1.4   thorpej 		.ba5_IDE_TF8		=	0x0ca,	/* wd_altsts */
    147   1.4   thorpej 		.ba5_IDE_RAD		=	0x0cc,
    148   1.4   thorpej 		.ba5_IDE_TF9		=	0x0d1,	/* Features 2 */
    149   1.4   thorpej 		.ba5_IDE_TF10		=	0x0d2,	/* Sector Count 2 */
    150   1.4   thorpej 		.ba5_IDE_TF11		=	0x0d3,	/* Start Sector 2 */
    151   1.4   thorpej 		.ba5_IDE_TF12		=	0x0d4,	/* Cylinder Low 2 */
    152   1.4   thorpej 		.ba5_IDE_TF13		=	0x0d5,	/* Cylinder High 2 */
    153   1.4   thorpej 		.ba5_IDE_TF14		=	0x0d6,	/* Device/Head 2 */
    154   1.4   thorpej 		.ba5_IDE_TF15		=	0x0d7,	/* Cmd Sts 2 */
    155   1.4   thorpej 		.ba5_IDE_TF16		=	0x0d8,	/* Sector Count 2 ext */
    156   1.4   thorpej 		.ba5_IDE_TF17		=	0x0d9,	/* Start Sector 2 ext */
    157   1.4   thorpej 		.ba5_IDE_TF18		=	0x0da,	/* Cyl Low 2 ext */
    158   1.4   thorpej 		.ba5_IDE_TF19		=	0x0db,	/* Cyl High 2 ext */
    159   1.4   thorpej 		.ba5_IDE_RABC		=	0x0dc,
    160   1.4   thorpej 		.ba5_IDE_CMD_STS	=	0x0e0,
    161   1.4   thorpej 		.ba5_IDE_CFG_STS	=	0x0e1,
    162   1.4   thorpej 		.ba5_IDE_DTM		=	0x0f4,
    163   1.4   thorpej 		.ba5_SControl		=	0x180,
    164   1.4   thorpej 		.ba5_SStatus		=	0x184,
    165   1.4   thorpej 		.ba5_SError		=	0x188,
    166   1.5   thorpej 		.ba5_SActive		=	0x18c,
    167   1.5   thorpej 		.ba5_SMisc		=	0x1c0,
    168   1.5   thorpej 		.ba5_PHY_CONFIG		=	0x1c4,
    169   1.5   thorpej 		.ba5_SIEN		=	0x1c8,
    170   1.5   thorpej 		.ba5_SFISCfg		=	0x1cc,
    171   1.5   thorpej 	},
    172   1.5   thorpej 	{	/* Channel 2 (3114) */
    173   1.5   thorpej 		.ba5_IDEDMA_CMD		=	0x200,
    174   1.5   thorpej 		.ba5_IDEDMA_CTL		=	0x202,
    175   1.5   thorpej 		.ba5_IDEDMA_TBL		=	0x204,
    176   1.5   thorpej 		.ba5_IDEDMA_CMD2	=	0x210,
    177   1.5   thorpej 		.ba5_IDEDMA_CTL2	=	0x212,
    178   1.5   thorpej 		.ba5_IDE_TF0		=	0x280,	/* wd_data */
    179   1.5   thorpej 		.ba5_IDE_TF1		=	0x281,	/* wd_error */
    180   1.5   thorpej 		.ba5_IDE_TF2		=	0x282,	/* wd_seccnt */
    181   1.5   thorpej 		.ba5_IDE_TF3		=	0x283,	/* wd_sector */
    182   1.5   thorpej 		.ba5_IDE_TF4		=	0x284,	/* wd_cyl_lo */
    183   1.5   thorpej 		.ba5_IDE_TF5		=	0x285,	/* wd_cyl_hi */
    184   1.5   thorpej 		.ba5_IDE_TF6		=	0x286,	/* wd_sdh */
    185   1.5   thorpej 		.ba5_IDE_TF7		=	0x287,	/* wd_command */
    186   1.5   thorpej 		.ba5_IDE_TF8		=	0x28a,	/* wd_altsts */
    187   1.5   thorpej 		.ba5_IDE_RAD		=	0x28c,
    188   1.5   thorpej 		.ba5_IDE_TF9		=	0x291,	/* Features 2 */
    189   1.5   thorpej 		.ba5_IDE_TF10		=	0x292,	/* Sector Count 2 */
    190   1.5   thorpej 		.ba5_IDE_TF11		=	0x293,	/* Start Sector 2 */
    191   1.5   thorpej 		.ba5_IDE_TF12		=	0x294,	/* Cylinder Low 2 */
    192   1.5   thorpej 		.ba5_IDE_TF13		=	0x295,	/* Cylinder High 2 */
    193   1.5   thorpej 		.ba5_IDE_TF14		=	0x296,	/* Device/Head 2 */
    194   1.5   thorpej 		.ba5_IDE_TF15		=	0x297,	/* Cmd Sts 2 */
    195   1.5   thorpej 		.ba5_IDE_TF16		=	0x298,	/* Sector Count 2 ext */
    196   1.5   thorpej 		.ba5_IDE_TF17		=	0x299,	/* Start Sector 2 ext */
    197   1.5   thorpej 		.ba5_IDE_TF18		=	0x29a,	/* Cyl Low 2 ext */
    198   1.5   thorpej 		.ba5_IDE_TF19		=	0x29b,	/* Cyl High 2 ext */
    199   1.5   thorpej 		.ba5_IDE_RABC		=	0x29c,
    200   1.5   thorpej 		.ba5_IDE_CMD_STS	=	0x2a0,
    201   1.5   thorpej 		.ba5_IDE_CFG_STS	=	0x2a1,
    202   1.5   thorpej 		.ba5_IDE_DTM		=	0x2b4,
    203   1.5   thorpej 		.ba5_SControl		=	0x300,
    204   1.5   thorpej 		.ba5_SStatus		=	0x304,
    205   1.5   thorpej 		.ba5_SError		=	0x308,
    206   1.5   thorpej 		.ba5_SActive		=	0x30c,
    207   1.5   thorpej 		.ba5_SMisc		=	0x340,
    208   1.5   thorpej 		.ba5_PHY_CONFIG		=	0x344,
    209   1.5   thorpej 		.ba5_SIEN		=	0x348,
    210   1.5   thorpej 		.ba5_SFISCfg		=	0x34c,
    211   1.5   thorpej 	},
    212   1.5   thorpej 	{	/* Channel 3 (3114) */
    213   1.5   thorpej 		.ba5_IDEDMA_CMD		=	0x208,
    214   1.5   thorpej 		.ba5_IDEDMA_CTL		=	0x20a,
    215   1.5   thorpej 		.ba5_IDEDMA_TBL		=	0x20c,
    216   1.5   thorpej 		.ba5_IDEDMA_CMD2	=	0x218,
    217   1.5   thorpej 		.ba5_IDEDMA_CTL2	=	0x21a,
    218   1.5   thorpej 		.ba5_IDE_TF0		=	0x2c0,	/* wd_data */
    219   1.5   thorpej 		.ba5_IDE_TF1		=	0x2c1,	/* wd_error */
    220   1.5   thorpej 		.ba5_IDE_TF2		=	0x2c2,	/* wd_seccnt */
    221   1.5   thorpej 		.ba5_IDE_TF3		=	0x2c3,	/* wd_sector */
    222   1.5   thorpej 		.ba5_IDE_TF4		=	0x2c4,	/* wd_cyl_lo */
    223   1.5   thorpej 		.ba5_IDE_TF5		=	0x2c5,	/* wd_cyl_hi */
    224   1.5   thorpej 		.ba5_IDE_TF6		=	0x2c6,	/* wd_sdh */
    225   1.5   thorpej 		.ba5_IDE_TF7		=	0x2c7,	/* wd_command */
    226   1.5   thorpej 		.ba5_IDE_TF8		=	0x2ca,	/* wd_altsts */
    227   1.5   thorpej 		.ba5_IDE_RAD		=	0x2cc,
    228   1.5   thorpej 		.ba5_IDE_TF9		=	0x2d1,	/* Features 2 */
    229   1.5   thorpej 		.ba5_IDE_TF10		=	0x2d2,	/* Sector Count 2 */
    230   1.5   thorpej 		.ba5_IDE_TF11		=	0x2d3,	/* Start Sector 2 */
    231   1.5   thorpej 		.ba5_IDE_TF12		=	0x2d4,	/* Cylinder Low 2 */
    232   1.5   thorpej 		.ba5_IDE_TF13		=	0x2d5,	/* Cylinder High 2 */
    233   1.5   thorpej 		.ba5_IDE_TF14		=	0x2d6,	/* Device/Head 2 */
    234   1.5   thorpej 		.ba5_IDE_TF15		=	0x2d7,	/* Cmd Sts 2 */
    235   1.5   thorpej 		.ba5_IDE_TF16		=	0x2d8,	/* Sector Count 2 ext */
    236   1.5   thorpej 		.ba5_IDE_TF17		=	0x2d9,	/* Start Sector 2 ext */
    237   1.5   thorpej 		.ba5_IDE_TF18		=	0x2da,	/* Cyl Low 2 ext */
    238   1.5   thorpej 		.ba5_IDE_TF19		=	0x2db,	/* Cyl High 2 ext */
    239   1.5   thorpej 		.ba5_IDE_RABC		=	0x2dc,
    240   1.5   thorpej 		.ba5_IDE_CMD_STS	=	0x2e0,
    241   1.5   thorpej 		.ba5_IDE_CFG_STS	=	0x2e1,
    242   1.5   thorpej 		.ba5_IDE_DTM		=	0x2f4,
    243   1.5   thorpej 		.ba5_SControl		=	0x380,
    244   1.5   thorpej 		.ba5_SStatus		=	0x384,
    245   1.5   thorpej 		.ba5_SError		=	0x388,
    246   1.5   thorpej 		.ba5_SActive		=	0x38c,
    247   1.5   thorpej 		.ba5_SMisc		=	0x3c0,
    248   1.5   thorpej 		.ba5_PHY_CONFIG		=	0x3c4,
    249   1.5   thorpej 		.ba5_SIEN		=	0x3c8,
    250   1.5   thorpej 		.ba5_SFISCfg		=	0x3cc,
    251   1.5   thorpej 	},
    252   1.4   thorpej };
    253   1.4   thorpej 
    254   1.5   thorpej #define	ba5_SIS		0x214		/* summary interrupt status */
    255   1.5   thorpej 
    256   1.5   thorpej /* Interrupt steering bit in BA5[0x200]. */
    257   1.5   thorpej #define	IDEDMA_CMD_INT_STEER	(1U << 1)
    258   1.5   thorpej 
    259  1.36      cube static int  satalink_match(device_t, cfdata_t, void *);
    260  1.36      cube static void satalink_attach(device_t, device_t, void *);
    261   1.1   thorpej 
    262  1.36      cube CFATTACH_DECL_NEW(satalink, sizeof(struct pciide_softc),
    263   1.1   thorpej     satalink_match, satalink_attach, NULL, NULL);
    264   1.1   thorpej 
    265   1.1   thorpej static void sii3112_chip_map(struct pciide_softc*, struct pci_attach_args*);
    266   1.5   thorpej static void sii3114_chip_map(struct pciide_softc*, struct pci_attach_args*);
    267  1.19   thorpej static void sii3112_drv_probe(struct ata_channel*);
    268  1.19   thorpej static void sii3112_setup_channel(struct ata_channel*);
    269   1.1   thorpej 
    270   1.1   thorpej static const struct pciide_product_desc pciide_satalink_products[] =  {
    271   1.1   thorpej 	{ PCI_PRODUCT_CMDTECH_3112,
    272   1.1   thorpej 	  0,
    273   1.1   thorpej 	  "Silicon Image SATALink 3112",
    274   1.1   thorpej 	  sii3112_chip_map,
    275   1.1   thorpej 	},
    276  1.41       mrg 	{ PCI_PRODUCT_CMDTECH_240,
    277  1.41       mrg 	  0,
    278  1.41       mrg 	  "Silicon Image SATALink Sil240",
    279  1.41       mrg 	  sii3112_chip_map,
    280  1.41       mrg 	},
    281  1.13    sekiya 	{ PCI_PRODUCT_CMDTECH_3512,
    282  1.13    sekiya 	  0,
    283  1.13    sekiya 	  "Silicon Image SATALink 3512",
    284  1.13    sekiya 	  sii3112_chip_map,
    285  1.13    sekiya 	},
    286  1.17   msaitoh 	{ PCI_PRODUCT_CMDTECH_AAR_1210SA,
    287  1.17   msaitoh 	  0,
    288  1.17   msaitoh 	  "Adaptec AAR-1210SA serial ATA RAID controller",
    289  1.17   msaitoh 	  sii3112_chip_map,
    290  1.17   msaitoh 	},
    291   1.5   thorpej 	{ PCI_PRODUCT_CMDTECH_3114,
    292   1.5   thorpej 	  0,
    293   1.5   thorpej 	  "Silicon Image SATALink 3114",
    294   1.5   thorpej 	  sii3114_chip_map,
    295   1.5   thorpej 	},
    296  1.41       mrg 	{ PCI_PRODUCT_ATI_IXP_SATA_300,
    297  1.41       mrg 	  0,
    298  1.41       mrg 	  "ATI IXP 300 SATA",
    299  1.41       mrg 	  sii3112_chip_map,
    300  1.41       mrg 	},
    301   1.1   thorpej 	{ 0,
    302   1.1   thorpej 	  0,
    303   1.1   thorpej 	  NULL,
    304   1.1   thorpej 	  NULL
    305   1.1   thorpej 	}
    306   1.1   thorpej };
    307   1.1   thorpej 
    308   1.1   thorpej static int
    309  1.36      cube satalink_match(device_t parent, cfdata_t match, void *aux)
    310   1.1   thorpej {
    311   1.1   thorpej 	struct pci_attach_args *pa = aux;
    312   1.1   thorpej 
    313   1.1   thorpej 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_CMDTECH) {
    314   1.1   thorpej 		if (pciide_lookup_product(pa->pa_id, pciide_satalink_products))
    315   1.1   thorpej 			return (2);
    316   1.1   thorpej 	}
    317   1.1   thorpej 	return (0);
    318   1.1   thorpej }
    319   1.1   thorpej 
    320   1.1   thorpej static void
    321  1.36      cube satalink_attach(device_t parent, device_t self, void *aux)
    322   1.1   thorpej {
    323   1.1   thorpej 	struct pci_attach_args *pa = aux;
    324  1.36      cube 	struct pciide_softc *sc = device_private(self);
    325  1.36      cube 
    326  1.36      cube 	sc->sc_wdcdev.sc_atac.atac_dev = self;
    327   1.1   thorpej 
    328   1.1   thorpej 	pciide_common_attach(sc, pa,
    329   1.1   thorpej 	    pciide_lookup_product(pa->pa_id, pciide_satalink_products));
    330   1.1   thorpej 
    331   1.1   thorpej }
    332   1.1   thorpej 
    333  1.27     perry static inline uint32_t
    334   1.5   thorpej ba5_read_4_ind(struct pciide_softc *sc, bus_addr_t reg)
    335   1.5   thorpej {
    336   1.5   thorpej 	uint32_t rv;
    337   1.5   thorpej 	int s;
    338   1.5   thorpej 
    339   1.5   thorpej 	s = splbio();
    340   1.5   thorpej 	pci_conf_write(sc->sc_pc, sc->sc_tag, SII3112_BA5_IND_ADDR, reg);
    341   1.5   thorpej 	rv = pci_conf_read(sc->sc_pc, sc->sc_tag, SII3112_BA5_IND_DATA);
    342   1.5   thorpej 	splx(s);
    343   1.5   thorpej 
    344   1.5   thorpej 	return (rv);
    345   1.5   thorpej }
    346   1.5   thorpej 
    347  1.27     perry static inline uint32_t
    348   1.2   thorpej ba5_read_4(struct pciide_softc *sc, bus_addr_t reg)
    349   1.2   thorpej {
    350   1.2   thorpej 
    351   1.2   thorpej 	if (__predict_true(sc->sc_ba5_en != 0))
    352   1.2   thorpej 		return (bus_space_read_4(sc->sc_ba5_st, sc->sc_ba5_sh, reg));
    353   1.2   thorpej 
    354   1.5   thorpej 	return (ba5_read_4_ind(sc, reg));
    355   1.2   thorpej }
    356   1.2   thorpej 
    357   1.4   thorpej #define	BA5_READ_4(sc, chan, reg)					\
    358   1.4   thorpej 	ba5_read_4((sc), satalink_ba5_regmap[(chan)].reg)
    359   1.4   thorpej 
    360  1.27     perry static inline void
    361   1.5   thorpej ba5_write_4_ind(struct pciide_softc *sc, bus_addr_t reg, uint32_t val)
    362   1.5   thorpej {
    363   1.5   thorpej 	int s;
    364   1.5   thorpej 
    365   1.5   thorpej 	s = splbio();
    366   1.5   thorpej 	pci_conf_write(sc->sc_pc, sc->sc_tag, SII3112_BA5_IND_ADDR, reg);
    367   1.5   thorpej 	pci_conf_write(sc->sc_pc, sc->sc_tag, SII3112_BA5_IND_DATA, val);
    368   1.5   thorpej 	splx(s);
    369   1.5   thorpej }
    370   1.5   thorpej 
    371  1.27     perry static inline void
    372   1.2   thorpej ba5_write_4(struct pciide_softc *sc, bus_addr_t reg, uint32_t val)
    373   1.2   thorpej {
    374   1.2   thorpej 
    375   1.2   thorpej 	if (__predict_true(sc->sc_ba5_en != 0))
    376   1.2   thorpej 		bus_space_write_4(sc->sc_ba5_st, sc->sc_ba5_sh, reg, val);
    377   1.5   thorpej 	else
    378   1.5   thorpej 		ba5_write_4_ind(sc, reg, val);
    379   1.2   thorpej }
    380   1.2   thorpej 
    381   1.4   thorpej #define	BA5_WRITE_4(sc, chan, reg, val)					\
    382   1.4   thorpej 	ba5_write_4((sc), satalink_ba5_regmap[(chan)].reg, (val))
    383   1.4   thorpej 
    384  1.25     ragge /*
    385  1.25     ragge  * When the Silicon Image 3112 retries a PCI memory read command,
    386  1.25     ragge  * it may retry it as a memory read multiple command under some
    387  1.25     ragge  * circumstances.  This can totally confuse some PCI controllers,
    388  1.25     ragge  * so ensure that it will never do this by making sure that the
    389  1.25     ragge  * Read Threshold (FIFO Read Request Control) field of the FIFO
    390  1.25     ragge  * Valid Byte Count and Control registers for both channels (BA5
    391  1.25     ragge  * offset 0x40 and 0x44) are set to be at least as large as the
    392  1.25     ragge  * cacheline size register.
    393  1.25     ragge  * This may also happen on the 3114 (ragge 050527)
    394  1.25     ragge  */
    395  1.25     ragge static void
    396  1.35        ws sii_fixup_cacheline(struct pciide_softc *sc, struct pci_attach_args *pa, int n)
    397  1.25     ragge {
    398  1.35        ws 	pcireg_t cls, reg;
    399  1.35        ws 	int i;
    400  1.35        ws 	static bus_addr_t addr[] = { 0x40, 0x44, 0x240, 0x244 };
    401  1.25     ragge 
    402  1.25     ragge 	cls = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG);
    403  1.25     ragge 	cls = (cls >> PCI_CACHELINE_SHIFT) & PCI_CACHELINE_MASK;
    404  1.25     ragge 	cls *= 4;
    405  1.25     ragge 	if (cls > 224) {
    406  1.25     ragge 		cls = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG);
    407  1.25     ragge 		cls &= ~(PCI_CACHELINE_MASK << PCI_CACHELINE_SHIFT);
    408  1.25     ragge 		cls |= ((224/4) << PCI_CACHELINE_SHIFT);
    409  1.25     ragge 		pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG, cls);
    410  1.25     ragge 		cls = 224;
    411  1.25     ragge 	}
    412  1.25     ragge 	if (cls < 32)
    413  1.25     ragge 		cls = 32;
    414  1.25     ragge 	cls = (cls + 31) / 32;
    415  1.35        ws 	for (i = 0; i < n; i++) {
    416  1.35        ws 		reg = ba5_read_4(sc, addr[i]);
    417  1.35        ws 		if ((reg & 0x7) < cls)
    418  1.35        ws 			ba5_write_4(sc, addr[i], (reg & 0x07) | cls);
    419  1.35        ws 	}
    420  1.25     ragge }
    421  1.25     ragge 
    422   1.1   thorpej static void
    423   1.1   thorpej sii3112_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
    424   1.1   thorpej {
    425   1.1   thorpej 	struct pciide_channel *cp;
    426   1.2   thorpej 	pcireg_t interface, scs_cmd, cfgctl;
    427   1.1   thorpej 	int channel;
    428   1.1   thorpej 
    429   1.1   thorpej 	if (pciide_chipen(sc, pa) == 0)
    430   1.1   thorpej 		return;
    431   1.1   thorpej 
    432   1.5   thorpej #define	SII3112_RESET_BITS						\
    433   1.5   thorpej 	(SCS_CMD_PBM_RESET | SCS_CMD_ARB_RESET |			\
    434   1.5   thorpej 	 SCS_CMD_FF1_RESET | SCS_CMD_FF0_RESET |			\
    435   1.5   thorpej 	 SCS_CMD_IDE1_RESET | SCS_CMD_IDE0_RESET)
    436   1.5   thorpej 
    437   1.5   thorpej 	/*
    438   1.5   thorpej 	 * Reset everything and then unblock all of the interrupts.
    439   1.5   thorpej 	 */
    440   1.2   thorpej 	scs_cmd = pci_conf_read(pa->pa_pc, pa->pa_tag, SII3112_SCS_CMD);
    441   1.2   thorpej 	pci_conf_write(pa->pa_pc, pa->pa_tag, SII3112_SCS_CMD,
    442   1.5   thorpej 		       scs_cmd | SII3112_RESET_BITS);
    443   1.5   thorpej 	delay(50 * 1000);
    444   1.5   thorpej 	pci_conf_write(pa->pa_pc, pa->pa_tag, SII3112_SCS_CMD,
    445   1.2   thorpej 		       scs_cmd & SCS_CMD_BA5_EN);
    446   1.5   thorpej 	delay(50 * 1000);
    447   1.2   thorpej 
    448   1.2   thorpej 	if (scs_cmd & SCS_CMD_BA5_EN) {
    449  1.36      cube 		aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    450  1.36      cube 		    "SATALink BA5 register space enabled\n");
    451   1.2   thorpej 		if (pci_mapreg_map(pa, PCI_MAPREG_START + 0x14,
    452   1.2   thorpej 				   PCI_MAPREG_TYPE_MEM|
    453   1.2   thorpej 				   PCI_MAPREG_MEM_TYPE_32BIT, 0,
    454   1.2   thorpej 				   &sc->sc_ba5_st, &sc->sc_ba5_sh,
    455  1.42  jakllsch 				   NULL, &sc->sc_ba5_ss) != 0)
    456  1.36      cube 			aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    457  1.36      cube 			    "unable to map SATALink BA5 register space\n");
    458   1.2   thorpej 		else
    459   1.2   thorpej 			sc->sc_ba5_en = 1;
    460   1.2   thorpej 	} else {
    461  1.36      cube 		aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    462  1.36      cube 		    "SATALink BA5 register space disabled\n");
    463   1.2   thorpej 
    464   1.2   thorpej 		cfgctl = pci_conf_read(pa->pa_pc, pa->pa_tag,
    465   1.2   thorpej 				       SII3112_PCI_CFGCTL);
    466   1.2   thorpej 		pci_conf_write(pa->pa_pc, pa->pa_tag, SII3112_PCI_CFGCTL,
    467   1.2   thorpej 			       cfgctl | CFGCTL_BA5INDEN);
    468   1.2   thorpej 	}
    469   1.2   thorpej 
    470  1.36      cube 	aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    471  1.36      cube 	    "bus-master DMA support present");
    472   1.1   thorpej 	pciide_mapreg_dma(sc, pa);
    473  1.34        ad 	aprint_verbose("\n");
    474   1.1   thorpej 
    475   1.1   thorpej 	/*
    476   1.1   thorpej 	 * Rev. <= 0x01 of the 3112 have a bug that can cause data
    477   1.1   thorpej 	 * corruption if DMA transfers cross an 8K boundary.  This is
    478   1.1   thorpej 	 * apparently hard to tickle, but we'll go ahead and play it
    479   1.1   thorpej 	 * safe.
    480   1.1   thorpej 	 */
    481  1.40       mrg 	if ((PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CMDTECH_3112 ||
    482  1.40       mrg 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CMDTECH_AAR_1210SA) &&
    483  1.40       mrg 	    PCI_REVISION(pa->pa_class) <= 0x01) {
    484   1.1   thorpej 		sc->sc_dma_maxsegsz = 8192;
    485   1.1   thorpej 		sc->sc_dma_boundary = 8192;
    486   1.1   thorpej 	}
    487   1.1   thorpej 
    488  1.35        ws 	sii_fixup_cacheline(sc, pa, 2);
    489  1.16    briggs 
    490  1.23       skd 	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
    491  1.21   thorpej 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
    492   1.1   thorpej 	if (sc->sc_dma_ok) {
    493  1.23       skd 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
    494   1.1   thorpej 		sc->sc_wdcdev.irqack = pciide_irqack;
    495  1.21   thorpej 		sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
    496  1.21   thorpej 		sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    497   1.1   thorpej 	}
    498  1.21   thorpej 	sc->sc_wdcdev.sc_atac.atac_set_modes = sii3112_setup_channel;
    499   1.1   thorpej 
    500   1.2   thorpej 	/* We can use SControl and SStatus to probe for drives. */
    501  1.21   thorpej 	sc->sc_wdcdev.sc_atac.atac_probe = sii3112_drv_probe;
    502   1.2   thorpej 
    503  1.21   thorpej 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
    504  1.21   thorpej 	sc->sc_wdcdev.sc_atac.atac_nchannels = PCIIDE_NUM_CHANNELS;
    505   1.1   thorpej 
    506  1.19   thorpej 	wdc_allocate_regs(&sc->sc_wdcdev);
    507  1.19   thorpej 
    508  1.24     perry 	/*
    509   1.2   thorpej 	 * The 3112 either identifies itself as a RAID storage device
    510   1.2   thorpej 	 * or a Misc storage device.  Fake up the interface bits for
    511   1.2   thorpej 	 * what our driver expects.
    512   1.1   thorpej 	 */
    513   1.1   thorpej 	if (PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
    514   1.1   thorpej 		interface = PCI_INTERFACE(pa->pa_class);
    515   1.1   thorpej 	} else {
    516   1.1   thorpej 		interface = PCIIDE_INTERFACE_BUS_MASTER_DMA |
    517   1.1   thorpej 		    PCIIDE_INTERFACE_PCI(0) | PCIIDE_INTERFACE_PCI(1);
    518   1.1   thorpej 	}
    519   1.1   thorpej 
    520  1.21   thorpej 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
    521  1.21   thorpej 	     channel++) {
    522   1.1   thorpej 		cp = &sc->pciide_channels[channel];
    523   1.1   thorpej 		if (pciide_chansetup(sc, channel, interface) == 0)
    524   1.1   thorpej 			continue;
    525  1.42  jakllsch 		pciide_mapchan(pa, cp, interface, pciide_pci_intr);
    526   1.1   thorpej 	}
    527   1.2   thorpej }
    528   1.2   thorpej 
    529   1.5   thorpej static void
    530   1.5   thorpej sii3114_mapreg_dma(struct pciide_softc *sc, struct pci_attach_args *pa)
    531   1.5   thorpej {
    532   1.5   thorpej 	struct pciide_channel *pc;
    533   1.5   thorpej 	int chan, reg;
    534   1.5   thorpej 	bus_size_t size;
    535   1.5   thorpej 
    536   1.5   thorpej 	sc->sc_wdcdev.dma_arg = sc;
    537   1.5   thorpej 	sc->sc_wdcdev.dma_init = pciide_dma_init;
    538   1.5   thorpej 	sc->sc_wdcdev.dma_start = pciide_dma_start;
    539   1.5   thorpej 	sc->sc_wdcdev.dma_finish = pciide_dma_finish;
    540   1.5   thorpej 
    541  1.36      cube 	if (device_cfdata(sc->sc_wdcdev.sc_atac.atac_dev)->cf_flags &
    542   1.5   thorpej 	    PCIIDE_OPTIONS_NODMA) {
    543  1.34        ad 		aprint_verbose(
    544   1.5   thorpej 		    ", but unused (forced off by config file)");
    545   1.5   thorpej 		sc->sc_dma_ok = 0;
    546   1.5   thorpej 		return;
    547   1.5   thorpej 	}
    548   1.5   thorpej 
    549   1.5   thorpej 	/*
    550   1.5   thorpej 	 * Slice off a subregion of BA5 for each of the channel's DMA
    551   1.5   thorpej 	 * registers.
    552   1.5   thorpej 	 */
    553   1.5   thorpej 
    554   1.5   thorpej 	sc->sc_dma_iot = sc->sc_ba5_st;
    555   1.5   thorpej 	for (chan = 0; chan < 4; chan++) {
    556   1.5   thorpej 		pc = &sc->pciide_channels[chan];
    557   1.5   thorpej 		for (reg = 0; reg < IDEDMA_NREGS; reg++) {
    558   1.5   thorpej 			size = 4;
    559   1.5   thorpej 			if (size > (IDEDMA_SCH_OFFSET - reg))
    560   1.5   thorpej 				size = IDEDMA_SCH_OFFSET - reg;
    561   1.5   thorpej 			if (bus_space_subregion(sc->sc_ba5_st,
    562   1.5   thorpej 			    sc->sc_ba5_sh,
    563   1.5   thorpej 			    satalink_ba5_regmap[chan].ba5_IDEDMA_CMD + reg,
    564   1.5   thorpej 			    size, &pc->dma_iohs[reg]) != 0) {
    565   1.5   thorpej 				sc->sc_dma_ok = 0;
    566  1.34        ad 				aprint_verbose(", but can't subregion offset "
    567   1.5   thorpej 				    "%lu size %lu",
    568   1.5   thorpej 				    (u_long) satalink_ba5_regmap[
    569   1.5   thorpej 						chan].ba5_IDEDMA_CMD + reg,
    570   1.5   thorpej 				    (u_long) size);
    571   1.5   thorpej 				return;
    572   1.5   thorpej 			}
    573   1.5   thorpej 		}
    574   1.5   thorpej 	}
    575   1.5   thorpej 
    576   1.5   thorpej 	/* DMA registers all set up! */
    577   1.5   thorpej 	sc->sc_dmat = pa->pa_dmat;
    578   1.5   thorpej 	sc->sc_dma_ok = 1;
    579   1.5   thorpej }
    580   1.5   thorpej 
    581   1.5   thorpej static int
    582   1.5   thorpej sii3114_chansetup(struct pciide_softc *sc, int channel)
    583   1.5   thorpej {
    584   1.5   thorpej 	static const char *channel_names[] = {
    585   1.5   thorpej 		"port 0",
    586   1.5   thorpej 		"port 1",
    587   1.5   thorpej 		"port 2",
    588   1.5   thorpej 		"port 3",
    589   1.5   thorpej 	};
    590   1.5   thorpej 	struct pciide_channel *cp = &sc->pciide_channels[channel];
    591   1.5   thorpej 
    592  1.19   thorpej 	sc->wdc_chanarray[channel] = &cp->ata_channel;
    593   1.5   thorpej 
    594   1.5   thorpej 	/*
    595   1.5   thorpej 	 * We must always keep the Interrupt Steering bit set in channel 2's
    596   1.5   thorpej 	 * IDEDMA_CMD register.
    597   1.5   thorpej 	 */
    598   1.5   thorpej 	if (channel == 2)
    599   1.5   thorpej 		cp->idedma_cmd = IDEDMA_CMD_INT_STEER;
    600   1.5   thorpej 
    601   1.5   thorpej 	cp->name = channel_names[channel];
    602  1.19   thorpej 	cp->ata_channel.ch_channel = channel;
    603  1.21   thorpej 	cp->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
    604  1.19   thorpej 	cp->ata_channel.ch_queue =
    605   1.9   thorpej 	    malloc(sizeof(struct ata_queue), M_DEVBUF, M_NOWAIT);
    606  1.28    bouyer 	cp->ata_channel.ch_ndrive = 2;
    607  1.19   thorpej 	if (cp->ata_channel.ch_queue == NULL) {
    608   1.5   thorpej 		aprint_error("%s %s channel: "
    609   1.5   thorpej 		    "can't allocate memory for command queue",
    610  1.36      cube 		    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), cp->name);
    611   1.5   thorpej 		return (0);
    612   1.5   thorpej 	}
    613   1.5   thorpej 	return (1);
    614   1.5   thorpej }
    615   1.5   thorpej 
    616   1.5   thorpej static void
    617   1.5   thorpej sii3114_mapchan(struct pciide_channel *cp)
    618   1.5   thorpej {
    619  1.19   thorpej 	struct ata_channel *wdc_cp = &cp->ata_channel;
    620  1.20   thorpej 	struct pciide_softc *sc = CHAN_TO_PCIIDE(wdc_cp);
    621  1.20   thorpej 	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(wdc_cp);
    622   1.5   thorpej 	int i;
    623   1.5   thorpej 
    624   1.5   thorpej 	cp->compat = 0;
    625   1.5   thorpej 	cp->ih = sc->sc_pci_ih;
    626   1.5   thorpej 
    627  1.19   thorpej 	wdr->cmd_iot = sc->sc_ba5_st;
    628   1.5   thorpej 	if (bus_space_subregion(sc->sc_ba5_st, sc->sc_ba5_sh,
    629  1.11   thorpej 			satalink_ba5_regmap[wdc_cp->ch_channel].ba5_IDE_TF0,
    630  1.19   thorpej 			9, &wdr->cmd_baseioh) != 0) {
    631  1.36      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    632  1.36      cube 		    "couldn't subregion %s cmd base\n", cp->name);
    633   1.5   thorpej 		goto bad;
    634   1.5   thorpej 	}
    635   1.5   thorpej 
    636  1.19   thorpej 	wdr->ctl_iot = sc->sc_ba5_st;
    637   1.5   thorpej 	if (bus_space_subregion(sc->sc_ba5_st, sc->sc_ba5_sh,
    638  1.11   thorpej 			satalink_ba5_regmap[wdc_cp->ch_channel].ba5_IDE_TF8,
    639   1.5   thorpej 			1, &cp->ctl_baseioh) != 0) {
    640  1.36      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    641  1.36      cube 		    "couldn't subregion %s ctl base\n", cp->name);
    642   1.5   thorpej 		goto bad;
    643   1.5   thorpej 	}
    644  1.19   thorpej 	wdr->ctl_ioh = cp->ctl_baseioh;
    645   1.5   thorpej 
    646   1.5   thorpej 	for (i = 0; i < WDC_NREG; i++) {
    647  1.19   thorpej 		if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
    648   1.5   thorpej 					i, i == 0 ? 4 : 1,
    649  1.19   thorpej 					&wdr->cmd_iohs[i]) != 0) {
    650  1.36      cube 			aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    651  1.36      cube 			    "couldn't subregion %s channel cmd regs\n",
    652  1.36      cube 			    cp->name);
    653   1.5   thorpej 			goto bad;
    654   1.5   thorpej 		}
    655   1.5   thorpej 	}
    656  1.14   thorpej 	wdc_init_shadow_regs(wdc_cp);
    657  1.19   thorpej 	wdr->data32iot = wdr->cmd_iot;
    658  1.19   thorpej 	wdr->data32ioh = wdr->cmd_iohs[0];
    659   1.5   thorpej 	wdcattach(wdc_cp);
    660   1.5   thorpej 	return;
    661   1.5   thorpej 
    662   1.5   thorpej  bad:
    663  1.19   thorpej 	cp->ata_channel.ch_flags |= ATACH_DISABLED;
    664   1.5   thorpej }
    665   1.5   thorpej 
    666   1.5   thorpej static void
    667   1.5   thorpej sii3114_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
    668   1.5   thorpej {
    669   1.5   thorpej 	struct pciide_channel *cp;
    670   1.5   thorpej 	pcireg_t scs_cmd;
    671   1.5   thorpej 	pci_intr_handle_t intrhandle;
    672   1.5   thorpej 	const char *intrstr;
    673   1.5   thorpej 	int channel;
    674   1.5   thorpej 
    675   1.5   thorpej 	if (pciide_chipen(sc, pa) == 0)
    676   1.5   thorpej 		return;
    677   1.5   thorpej 
    678   1.5   thorpej #define	SII3114_RESET_BITS						\
    679   1.5   thorpej 	(SCS_CMD_PBM_RESET | SCS_CMD_ARB_RESET |			\
    680   1.5   thorpej 	 SCS_CMD_FF1_RESET | SCS_CMD_FF0_RESET |			\
    681   1.5   thorpej 	 SCS_CMD_FF3_RESET | SCS_CMD_FF2_RESET |			\
    682   1.5   thorpej 	 SCS_CMD_IDE1_RESET | SCS_CMD_IDE0_RESET |			\
    683   1.5   thorpej 	 SCS_CMD_IDE3_RESET | SCS_CMD_IDE2_RESET)
    684   1.5   thorpej 
    685   1.5   thorpej 	/*
    686   1.5   thorpej 	 * Reset everything and then unblock all of the interrupts.
    687   1.5   thorpej 	 */
    688   1.5   thorpej 	scs_cmd = pci_conf_read(pa->pa_pc, pa->pa_tag, SII3112_SCS_CMD);
    689   1.5   thorpej 	pci_conf_write(pa->pa_pc, pa->pa_tag, SII3112_SCS_CMD,
    690   1.5   thorpej 		       scs_cmd | SII3114_RESET_BITS);
    691   1.5   thorpej 	delay(50 * 1000);
    692   1.5   thorpej 	pci_conf_write(pa->pa_pc, pa->pa_tag, SII3112_SCS_CMD,
    693   1.5   thorpej 		       scs_cmd & SCS_CMD_M66EN);
    694   1.5   thorpej 	delay(50 * 1000);
    695   1.5   thorpej 
    696   1.5   thorpej 	/*
    697   1.5   thorpej 	 * On the 3114, the BA5 register space is always enabled.  In
    698   1.5   thorpej 	 * order to use the 3114 in any sane way, we must use this BA5
    699   1.5   thorpej 	 * register space, and so we consider it an error if we cannot
    700   1.5   thorpej 	 * map it.
    701   1.5   thorpej 	 *
    702   1.5   thorpej 	 * As a consequence of using BA5, our register mapping is different
    703   1.5   thorpej 	 * from a normal PCI IDE controller's, and so we are unable to use
    704   1.5   thorpej 	 * most of the common PCI IDE register mapping functions.
    705   1.5   thorpej 	 */
    706   1.5   thorpej 	if (pci_mapreg_map(pa, PCI_MAPREG_START + 0x14,
    707   1.5   thorpej 			   PCI_MAPREG_TYPE_MEM|
    708   1.5   thorpej 			   PCI_MAPREG_MEM_TYPE_32BIT, 0,
    709   1.5   thorpej 			   &sc->sc_ba5_st, &sc->sc_ba5_sh,
    710  1.42  jakllsch 			   NULL, &sc->sc_ba5_ss) != 0) {
    711  1.36      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    712  1.36      cube 		    "unable to map SATALink BA5 register space\n");
    713   1.5   thorpej 		return;
    714   1.5   thorpej 	}
    715   1.5   thorpej 	sc->sc_ba5_en = 1;
    716   1.5   thorpej 
    717  1.36      cube 	aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    718  1.36      cube 	    "%dMHz PCI bus\n", (scs_cmd & SCS_CMD_M66EN) ? 66 : 33);
    719   1.5   thorpej 
    720   1.5   thorpej 	/*
    721   1.5   thorpej 	 * Set the Interrupt Steering bit in the IDEDMA_CMD register of
    722   1.5   thorpej 	 * channel 2.  This is required at all times for proper operation
    723   1.5   thorpej 	 * when using the BA5 register space (otherwise interrupts from
    724   1.5   thorpej 	 * all 4 channels won't work).
    725   1.5   thorpej 	 */
    726   1.5   thorpej 	BA5_WRITE_4(sc, 2, ba5_IDEDMA_CMD, IDEDMA_CMD_INT_STEER);
    727   1.5   thorpej 
    728  1.36      cube 	aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    729  1.36      cube 	    "bus-master DMA support present");
    730   1.5   thorpej 	sii3114_mapreg_dma(sc, pa);
    731  1.34        ad 	aprint_verbose("\n");
    732   1.5   thorpej 
    733  1.35        ws 	sii_fixup_cacheline(sc, pa, 4);
    734  1.25     ragge 
    735  1.23       skd 	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
    736  1.21   thorpej 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
    737   1.5   thorpej 	if (sc->sc_dma_ok) {
    738  1.23       skd 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
    739   1.5   thorpej 		sc->sc_wdcdev.irqack = pciide_irqack;
    740  1.21   thorpej 		sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
    741  1.21   thorpej 		sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
    742   1.5   thorpej 	}
    743  1.21   thorpej 	sc->sc_wdcdev.sc_atac.atac_set_modes = sii3112_setup_channel;
    744   1.5   thorpej 
    745   1.5   thorpej 	/* We can use SControl and SStatus to probe for drives. */
    746  1.21   thorpej 	sc->sc_wdcdev.sc_atac.atac_probe = sii3112_drv_probe;
    747   1.5   thorpej 
    748  1.21   thorpej 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
    749  1.21   thorpej 	sc->sc_wdcdev.sc_atac.atac_nchannels = 4;
    750   1.5   thorpej 
    751  1.19   thorpej 	wdc_allocate_regs(&sc->sc_wdcdev);
    752  1.19   thorpej 
    753   1.5   thorpej 	/* Map and establish the interrupt handler. */
    754   1.5   thorpej 	if (pci_intr_map(pa, &intrhandle) != 0) {
    755  1.36      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    756  1.36      cube 		    "couldn't map native-PCI interrupt\n");
    757   1.5   thorpej 		return;
    758   1.5   thorpej 	}
    759   1.5   thorpej 	intrstr = pci_intr_string(pa->pa_pc, intrhandle);
    760   1.5   thorpej 	sc->sc_pci_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_BIO,
    761   1.5   thorpej 					   /* XXX */
    762   1.5   thorpej 					   pciide_pci_intr, sc);
    763   1.5   thorpej 	if (sc->sc_pci_ih != NULL) {
    764  1.36      cube 		aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    765  1.36      cube 		    "using %s for native-PCI interrupt\n",
    766  1.36      cube 		    intrstr ? intrstr : "unknown interrupt");
    767   1.5   thorpej 	} else {
    768  1.36      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    769  1.36      cube 		    "couldn't establish native-PCI interrupt");
    770   1.5   thorpej 		if (intrstr != NULL)
    771  1.39     njoly 			aprint_error(" at %s", intrstr);
    772  1.39     njoly 		aprint_error("\n");
    773   1.5   thorpej 		return;
    774   1.5   thorpej 	}
    775   1.5   thorpej 
    776  1.21   thorpej 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
    777  1.21   thorpej 	     channel++) {
    778   1.5   thorpej 		cp = &sc->pciide_channels[channel];
    779   1.5   thorpej 		if (sii3114_chansetup(sc, channel) == 0)
    780   1.5   thorpej 			continue;
    781   1.5   thorpej 		sii3114_mapchan(cp);
    782   1.5   thorpej 	}
    783   1.5   thorpej }
    784   1.5   thorpej 
    785  1.32    bouyer /* Probe the drives using SATA registers.
    786  1.32    bouyer  * Note we can't use wdc_sataprobe as we may not be able to map ba5
    787  1.32    bouyer  */
    788   1.7   thorpej static void
    789  1.19   thorpej sii3112_drv_probe(struct ata_channel *chp)
    790   1.2   thorpej {
    791  1.20   thorpej 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    792  1.20   thorpej 	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
    793   1.2   thorpej 	uint32_t scontrol, sstatus;
    794   1.2   thorpej 	uint8_t scnt, sn, cl, ch;
    795  1.22   thorpej 	int i, s;
    796  1.12   thorpej 
    797  1.12   thorpej 	/* XXX This should be done by other code. */
    798  1.12   thorpej 	for (i = 0; i < 2; i++) {
    799  1.12   thorpej 		chp->ch_drive[i].chnl_softc = chp;
    800  1.12   thorpej 		chp->ch_drive[i].drive = i;
    801  1.12   thorpej 	}
    802   1.2   thorpej 
    803   1.2   thorpej 	/*
    804   1.2   thorpej 	 * The 3112 is a 2-port part, and only has one drive per channel
    805   1.2   thorpej 	 * (each port emulates a master drive).
    806   1.5   thorpej 	 *
    807   1.5   thorpej 	 * The 3114 is similar, but has 4 channels.
    808   1.2   thorpej 	 */
    809   1.2   thorpej 
    810   1.2   thorpej 	/*
    811   1.2   thorpej 	 * Request communication initialization sequence, any speed.
    812   1.2   thorpej 	 * Performing this is the equivalent of an ATA Reset.
    813   1.2   thorpej 	 */
    814   1.2   thorpej 	scontrol = SControl_DET_INIT | SControl_SPD_ANY;
    815   1.2   thorpej 
    816   1.2   thorpej 	/*
    817   1.2   thorpej 	 * XXX We don't yet support SATA power management; disable all
    818   1.2   thorpej 	 * power management state transitions.
    819   1.2   thorpej 	 */
    820   1.2   thorpej 	scontrol |= SControl_IPM_NONE;
    821   1.2   thorpej 
    822  1.11   thorpej 	BA5_WRITE_4(sc, chp->ch_channel, ba5_SControl, scontrol);
    823   1.5   thorpej 	delay(50 * 1000);
    824   1.2   thorpej 	scontrol &= ~SControl_DET_INIT;
    825  1.11   thorpej 	BA5_WRITE_4(sc, chp->ch_channel, ba5_SControl, scontrol);
    826   1.5   thorpej 	delay(50 * 1000);
    827   1.2   thorpej 
    828  1.11   thorpej 	sstatus = BA5_READ_4(sc, chp->ch_channel, ba5_SStatus);
    829   1.5   thorpej #if 0
    830  1.37    cegger 	aprint_normal_dev(&sc->sc_wdcdev.sc_atac.atac_dev,
    831  1.37    cegger 	    "port %d: SStatus=0x%08x, SControl=0x%08x\n",
    832  1.37    cegger 	    chp->ch_channel, sstatus,
    833  1.11   thorpej 	    BA5_READ_4(sc, chp->ch_channel, ba5_SControl));
    834   1.5   thorpej #endif
    835   1.2   thorpej 	switch (sstatus & SStatus_DET_mask) {
    836   1.2   thorpej 	case SStatus_DET_NODEV:
    837   1.2   thorpej 		/* No device; be silent. */
    838   1.2   thorpej 		break;
    839   1.2   thorpej 
    840   1.2   thorpej 	case SStatus_DET_DEV_NE:
    841  1.36      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    842  1.36      cube 		    "port %d: device connected, but "
    843  1.36      cube 		    "communication not established\n", chp->ch_channel);
    844   1.2   thorpej 		break;
    845   1.2   thorpej 
    846   1.2   thorpej 	case SStatus_DET_OFFLINE:
    847  1.36      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    848  1.36      cube 		    "port %d: PHY offline\n", chp->ch_channel);
    849   1.2   thorpej 		break;
    850   1.2   thorpej 
    851   1.2   thorpej 	case SStatus_DET_DEV:
    852   1.2   thorpej 		/*
    853   1.2   thorpej 		 * XXX ATAPI detection doesn't currently work.  Don't
    854   1.2   thorpej 		 * XXX know why.  But, it's not like the standard method
    855   1.2   thorpej 		 * XXX can detect an ATAPI device connected via a SATA/PATA
    856   1.2   thorpej 		 * XXX bridge, so at least this is no worse.  --thorpej
    857   1.2   thorpej 		 */
    858  1.19   thorpej 		bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
    859   1.2   thorpej 		    WDSD_IBM | (0 << 4));
    860   1.2   thorpej 		delay(10);	/* 400ns delay */
    861   1.2   thorpej 		/* Save register contents. */
    862  1.19   thorpej 		scnt = bus_space_read_1(wdr->cmd_iot,
    863  1.19   thorpej 				        wdr->cmd_iohs[wd_seccnt], 0);
    864  1.19   thorpej 		sn = bus_space_read_1(wdr->cmd_iot,
    865  1.19   thorpej 				      wdr->cmd_iohs[wd_sector], 0);
    866  1.19   thorpej 		cl = bus_space_read_1(wdr->cmd_iot,
    867  1.19   thorpej 				      wdr->cmd_iohs[wd_cyl_lo], 0);
    868  1.19   thorpej 		ch = bus_space_read_1(wdr->cmd_iot,
    869  1.19   thorpej 				      wdr->cmd_iohs[wd_cyl_hi], 0);
    870   1.2   thorpej #if 0
    871   1.2   thorpej 		printf("%s: port %d: scnt=0x%x sn=0x%x cl=0x%x ch=0x%x\n",
    872  1.37    cegger 		    device_xname(&sc->sc_wdcdev.sc_atac.atac_dev), chp->ch_channel,
    873   1.2   thorpej 		    scnt, sn, cl, ch);
    874   1.2   thorpej #endif
    875   1.2   thorpej 		/*
    876   1.2   thorpej 		 * scnt and sn are supposed to be 0x1 for ATAPI, but in some
    877   1.2   thorpej 		 * cases we get wrong values here, so ignore it.
    878   1.2   thorpej 		 */
    879  1.22   thorpej 		s = splbio();
    880   1.2   thorpej 		if (cl == 0x14 && ch == 0xeb)
    881   1.2   thorpej 			chp->ch_drive[0].drive_flags |= DRIVE_ATAPI;
    882   1.2   thorpej 		else
    883   1.2   thorpej 			chp->ch_drive[0].drive_flags |= DRIVE_ATA;
    884  1.22   thorpej 		splx(s);
    885   1.2   thorpej 
    886  1.36      cube 		aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    887  1.36      cube 		    "port %d: device present, speed: %s\n",
    888  1.36      cube 		    chp->ch_channel,
    889  1.15   thorpej 		    sata_speed(sstatus));
    890   1.2   thorpej 		break;
    891   1.2   thorpej 
    892   1.2   thorpej 	default:
    893  1.36      cube 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    894  1.36      cube 		    "port %d: unknown SStatus: 0x%08x\n",
    895  1.36      cube 		    chp->ch_channel, sstatus);
    896   1.2   thorpej 	}
    897   1.1   thorpej }
    898   1.1   thorpej 
    899   1.1   thorpej static void
    900  1.19   thorpej sii3112_setup_channel(struct ata_channel *chp)
    901   1.1   thorpej {
    902   1.1   thorpej 	struct ata_drive_datas *drvp;
    903  1.22   thorpej 	int drive, s;
    904   1.1   thorpej 	u_int32_t idedma_ctl, dtm;
    905  1.20   thorpej 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
    906  1.20   thorpej 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    907   1.1   thorpej 
    908   1.1   thorpej 	/* setup DMA if needed */
    909   1.1   thorpej 	pciide_channel_dma_setup(cp);
    910   1.1   thorpej 
    911   1.1   thorpej 	idedma_ctl = 0;
    912   1.1   thorpej 	dtm = 0;
    913   1.1   thorpej 
    914   1.1   thorpej 	for (drive = 0; drive < 2; drive++) {
    915   1.1   thorpej 		drvp = &chp->ch_drive[drive];
    916   1.1   thorpej 		/* If no drive, skip */
    917   1.1   thorpej 		if ((drvp->drive_flags & DRIVE) == 0)
    918   1.1   thorpej 			continue;
    919   1.1   thorpej 		if (drvp->drive_flags & DRIVE_UDMA) {
    920   1.1   thorpej 			/* use Ultra/DMA */
    921  1.22   thorpej 			s = splbio();
    922   1.1   thorpej 			drvp->drive_flags &= ~DRIVE_DMA;
    923  1.22   thorpej 			splx(s);
    924   1.1   thorpej 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    925   1.1   thorpej 			dtm |= DTM_IDEx_DMA;
    926   1.1   thorpej 		} else if (drvp->drive_flags & DRIVE_DMA) {
    927   1.1   thorpej 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    928   1.1   thorpej 			dtm |= DTM_IDEx_DMA;
    929   1.1   thorpej 		} else {
    930   1.1   thorpej 			dtm |= DTM_IDEx_PIO;
    931   1.1   thorpej 		}
    932   1.1   thorpej 	}
    933   1.1   thorpej 
    934   1.1   thorpej 	/*
    935   1.1   thorpej 	 * Nothing to do to setup modes; it is meaningless in S-ATA
    936   1.1   thorpej 	 * (but many S-ATA drives still want to get the SET_FEATURE
    937   1.1   thorpej 	 * command).
    938   1.1   thorpej 	 */
    939   1.1   thorpej 	if (idedma_ctl != 0) {
    940   1.1   thorpej 		/* Add software bits in status register */
    941   1.1   thorpej 		bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
    942   1.1   thorpej 		    idedma_ctl);
    943   1.1   thorpej 	}
    944  1.11   thorpej 	BA5_WRITE_4(sc, chp->ch_channel, ba5_IDE_DTM, dtm);
    945   1.1   thorpej }
    946