xref: /dragonfly/sys/dev/disk/nata/chipsets/ata-ati.c (revision 560012aa)
1a1917f14Szrj /*-
2a1917f14Szrj  * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
3a1917f14Szrj  * All rights reserved.
4a1917f14Szrj  *
5a1917f14Szrj  * Redistribution and use in source and binary forms, with or without
6a1917f14Szrj  * modification, are permitted provided that the following conditions
7a1917f14Szrj  * are met:
8a1917f14Szrj  * 1. Redistributions of source code must retain the above copyright
9a1917f14Szrj  *    notice, this list of conditions and the following disclaimer,
10a1917f14Szrj  *    without modification, immediately at the beginning of the file.
11a1917f14Szrj  * 2. Redistributions in binary form must reproduce the above copyright
12a1917f14Szrj  *    notice, this list of conditions and the following disclaimer in the
13a1917f14Szrj  *    documentation and/or other materials provided with the distribution.
14a1917f14Szrj  *
15a1917f14Szrj  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16a1917f14Szrj  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17a1917f14Szrj  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18a1917f14Szrj  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19a1917f14Szrj  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20a1917f14Szrj  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21a1917f14Szrj  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22a1917f14Szrj  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23a1917f14Szrj  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24a1917f14Szrj  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25a1917f14Szrj  */
26a1917f14Szrj 
27a1917f14Szrj /* local prototypes */
28a1917f14Szrj static int ata_ati_chipinit(device_t dev);
29a1917f14Szrj static void ata_ati_setmode(device_t dev, int mode);
30a1917f14Szrj 
31853eb30dSzrj /* misc defines */
32*560012aaSzrj #undef SII_MEMIO
33*560012aaSzrj #define SII_MEMIO       1	/* must match ata_siliconimage.c's definition */
34*560012aaSzrj #define SII_BUG         0x04	/* must match ata_siliconimage.c's definition */
35*560012aaSzrj 
36*560012aaSzrj #define ATI_SATA	SII_MEMIO
37*560012aaSzrj #define ATI_PATA	0x02
38853eb30dSzrj #define ATI_AHCI	0x04
39853eb30dSzrj 
40a1917f14Szrj /*
41a1917f14Szrj  * ATI chipset support functions
42a1917f14Szrj  */
43a1917f14Szrj int
ata_ati_ident(device_t dev)44a1917f14Szrj ata_ati_ident(device_t dev)
45a1917f14Szrj {
46a1917f14Szrj     struct ata_pci_controller *ctlr = device_get_softc(dev);
4759503772Szrj     static const struct ata_chip_id ids[] =
48*560012aaSzrj     {{ ATA_ATI_IXP200,    0x00, ATI_PATA, 0, ATA_UDMA5, "IXP200" },
49*560012aaSzrj      { ATA_ATI_IXP300,    0x00, ATI_PATA, 0, ATA_UDMA6, "IXP300" },
50*560012aaSzrj      { ATA_ATI_IXP300_S1, 0x00, ATI_SATA, SII_BUG, ATA_SA150, "IXP300" },
51*560012aaSzrj      { ATA_ATI_IXP400,    0x00, ATI_PATA, 0, ATA_UDMA6, "IXP400" },
52*560012aaSzrj      { ATA_ATI_IXP400_S1, 0x00, ATI_SATA, SII_BUG, ATA_SA150, "IXP400" },
53*560012aaSzrj      { ATA_ATI_IXP400_S2, 0x00, ATI_SATA, SII_BUG, ATA_SA150, "IXP400" },
54*560012aaSzrj      { ATA_ATI_IXP600,    0x00, ATI_PATA, 0, ATA_UDMA6, "IXP600" },
5513b0cf9eSzrj      { ATA_ATI_IXP600_S1, 0x00, ATI_AHCI, 0, ATA_SA300, "IXP600" },
5613b0cf9eSzrj      { ATA_ATI_IXP600_S2, 0x00, ATI_AHCI, 0, ATA_SA300, "IXP600" },
57*560012aaSzrj      { ATA_ATI_IXP700,    0x00, ATI_PATA, 0, ATA_UDMA6, "IXP700/800" },
58*560012aaSzrj      { ATA_ATI_IXP700_S1, 0x00, ATI_AHCI, 0, ATA_SA300, "IXP700/800" },
59*560012aaSzrj      { ATA_ATI_IXP700_S2, 0x00, ATI_AHCI, 0, ATA_SA300, "IXP700/800" },
60*560012aaSzrj      { ATA_ATI_IXP700_S3, 0x00, ATI_AHCI, 0, ATA_SA300, "IXP700/800" },
61*560012aaSzrj      { ATA_ATI_IXP700_S4, 0x00, ATI_AHCI, 0, ATA_SA300, "IXP700/800" },
62*560012aaSzrj      { ATA_ATI_IXP800_S1, 0x00, ATI_AHCI, 0, ATA_SA300, "IXP800" },
63*560012aaSzrj      { ATA_ATI_IXP800_S2, 0x00, ATI_AHCI, 0, ATA_SA300, "IXP800" },
64a1917f14Szrj      { 0, 0, 0, 0, 0, 0}};
65a1917f14Szrj 
6659503772Szrj     if (pci_get_vendor(dev) != ATA_ATI_ID)
67a1917f14Szrj 	return ENXIO;
68a1917f14Szrj 
6959503772Szrj     if (!(ctlr->chip = ata_match_chip(dev, ids)))
7059503772Szrj 	return ENXIO;
7159503772Szrj 
7259503772Szrj     ata_set_desc(dev);
73a1917f14Szrj 
74*560012aaSzrj     switch (ctlr->chip->cfg1) {
75*560012aaSzrj     case ATI_PATA:
76*560012aaSzrj 	ctlr->chipinit = ata_ati_chipinit;
77*560012aaSzrj 	break;
78*560012aaSzrj     case ATI_SATA:
79a1917f14Szrj 	/*
80*560012aaSzrj 	 * the ATI SATA controller is actually a SiI 3112 controller
81a1917f14Szrj 	 */
82a1917f14Szrj 	ctlr->chipinit = ata_sii_chipinit;
83*560012aaSzrj 	break;
84*560012aaSzrj     case ATI_AHCI:
85*560012aaSzrj 	if (pci_get_subclass(dev) != PCIS_STORAGE_IDE)
86*560012aaSzrj 		ctlr->chipinit = ata_ahci_chipinit;
87a1917f14Szrj 	else
88a1917f14Szrj 		ctlr->chipinit = ata_ati_chipinit;
89*560012aaSzrj 	break;
90*560012aaSzrj     }
91a1917f14Szrj     return 0;
92a1917f14Szrj }
93a1917f14Szrj 
94a1917f14Szrj static int
ata_ati_chipinit(device_t dev)95a1917f14Szrj ata_ati_chipinit(device_t dev)
96a1917f14Szrj {
97a1917f14Szrj     struct ata_pci_controller *ctlr = device_get_softc(dev);
98a1917f14Szrj 
9943156ad7Szrj     if (ata_setup_interrupt(dev, ata_generic_intr))
100a1917f14Szrj 	return ENXIO;
101a1917f14Szrj 
102*560012aaSzrj     /* IXP600 & IXP700 only have 1 PATA channel */
103*560012aaSzrj     if ((ctlr->chip->chipid == ATA_ATI_IXP600) ||
104*560012aaSzrj 	(ctlr->chip->chipid == ATA_ATI_IXP700))
105*560012aaSzrj 	ctlr->channels = 1;
106*560012aaSzrj 
107a1917f14Szrj     /* The SB600 needs special treatment. */
108853eb30dSzrj     if (ctlr->chip->cfg1 & ATI_AHCI) {
109a1917f14Szrj 	/* Check if the chip is configured as an AHCI part. */
110a1917f14Szrj 	if ((pci_get_subclass(dev) == PCIS_STORAGE_SATA) &&
111a1917f14Szrj 	    (pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_STORAGE_SATA_AHCI_1_0)) {
112*560012aaSzrj 	    ctlr->setmode = ata_sata_setmode;
113a1917f14Szrj 	    if (ata_ahci_chipinit(dev) != ENXIO)
114a1917f14Szrj 		return 0;
115a1917f14Szrj 	}
116a1917f14Szrj     }
117a1917f14Szrj 
118a1917f14Szrj     ctlr->setmode = ata_ati_setmode;
119a1917f14Szrj     return 0;
120a1917f14Szrj }
121a1917f14Szrj 
122a1917f14Szrj static void
ata_ati_setmode(device_t dev,int mode)123a1917f14Szrj ata_ati_setmode(device_t dev, int mode)
124a1917f14Szrj {
125a1917f14Szrj 	device_t gparent = GRANDPARENT(dev);
126a1917f14Szrj 	struct ata_pci_controller *ctlr = device_get_softc(gparent);
127a1917f14Szrj 	struct ata_channel *ch = device_get_softc(device_get_parent(dev));
128a1917f14Szrj 	struct ata_device *atadev = device_get_softc(dev);
1292458a87aSzrj 	int devno = (ch->unit << 1) + atadev->unit;
130a1917f14Szrj 	int offset = (devno ^ 0x01) << 3;
131a1917f14Szrj 	int error;
132*560012aaSzrj 	int piomode;
13359503772Szrj 	static const uint8_t piotimings[] =
13459503772Szrj 			    { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
135a1917f14Szrj 			      0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
13659503772Szrj 	static const uint8_t dmatimings[] = { 0x77, 0x21, 0x20 };
137a1917f14Szrj 
138a1917f14Szrj     mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
139a1917f14Szrj 
140a1917f14Szrj     mode = ata_check_80pin(dev, mode);
141a1917f14Szrj 
142a1917f14Szrj     error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
143a1917f14Szrj 
144a1917f14Szrj     if (bootverbose)
145a1917f14Szrj 	device_printf(dev, "%ssetting %s on %s chip\n",
146a1917f14Szrj 		      (error) ? "FAILURE " : "",
147a1917f14Szrj 		      ata_mode2str(mode), ctlr->chip->text);
148a1917f14Szrj     if (!error) {
149a1917f14Szrj 	if (mode >= ATA_UDMA0) {
150*560012aaSzrj 	    /* Set UDMA mode, enable UDMA, set WDMA2/PIO4 */
151a1917f14Szrj 	    pci_write_config(gparent, 0x56,
152a1917f14Szrj 			     (pci_read_config(gparent, 0x56, 2) &
153a1917f14Szrj 			      ~(0xf << (devno << 2))) |
154a1917f14Szrj 			     ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
155a1917f14Szrj 	    pci_write_config(gparent, 0x54,
156a1917f14Szrj 			     pci_read_config(gparent, 0x54, 1) |
157a1917f14Szrj 			     (0x01 << devno), 1);
158a1917f14Szrj 	    pci_write_config(gparent, 0x44,
159a1917f14Szrj 			     (pci_read_config(gparent, 0x44, 4) &
160a1917f14Szrj 			      ~(0xff << offset)) |
161a1917f14Szrj 			     (dmatimings[2] << offset), 4);
162*560012aaSzrj 	    piomode = ATA_PIO4;
163*560012aaSzrj 	} else if (mode >= ATA_WDMA0) {
164*560012aaSzrj 	    /* Disable UDMA, set WDMA mode and timings, calculate PIO. */
165a1917f14Szrj 	    pci_write_config(gparent, 0x54,
166a1917f14Szrj 			     pci_read_config(gparent, 0x54, 1) &
167a1917f14Szrj 			      ~(0x01 << devno), 1);
168a1917f14Szrj 	    pci_write_config(gparent, 0x44,
169a1917f14Szrj 			     (pci_read_config(gparent, 0x44, 4) &
170a1917f14Szrj 			      ~(0xff << offset)) |
171a1917f14Szrj 			     (dmatimings[mode & ATA_MODE_MASK] << offset), 4);
172*560012aaSzrj 	    piomode = (mode == ATA_WDMA0) ? ATA_PIO0 :
173*560012aaSzrj 		(mode == ATA_WDMA1) ? ATA_PIO3 : ATA_PIO4;
174*560012aaSzrj 	} else {
175*560012aaSzrj 	    /* Disable UDMA, set requested PIO. */
176a1917f14Szrj 	    pci_write_config(gparent, 0x54,
177a1917f14Szrj 			     pci_read_config(gparent, 0x54, 1) &
178a1917f14Szrj 			     ~(0x01 << devno), 1);
179*560012aaSzrj 	    piomode = mode;
180*560012aaSzrj 	}
181*560012aaSzrj 	/* Set PIO mode and timings, calculated above. */
182a1917f14Szrj 	pci_write_config(gparent, 0x4a,
183a1917f14Szrj 			 (pci_read_config(gparent, 0x4a, 2) &
184a1917f14Szrj 			  ~(0xf << (devno << 2))) |
185*560012aaSzrj 			 ((piomode - ATA_PIO0) << (devno<<2)),2);
186a1917f14Szrj 	pci_write_config(gparent, 0x40,
187a1917f14Szrj 			 (pci_read_config(gparent, 0x40, 4) &
188a1917f14Szrj 			  ~(0xff << offset)) |
189*560012aaSzrj 			 (piotimings[ata_mode2idx(piomode)] << offset), 4);
190a1917f14Szrj 	atadev->mode = mode;
191a1917f14Szrj     }
192a1917f14Szrj }
193