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