xref: /dragonfly/sys/dev/disk/nata/chipsets/ata-ite.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_ite_chipinit(device_t dev);
29*560012aaSzrj static void ata_ite_821x_setmode(device_t dev, int mode);
30*560012aaSzrj static void ata_ite_8213_setmode(device_t dev, int mode);
31a1917f14Szrj 
32a1917f14Szrj /*
33a1917f14Szrj  * Integrated Technology Express Inc. (ITE) chipset support functions
34a1917f14Szrj  */
35a1917f14Szrj int
ata_ite_ident(device_t dev)36a1917f14Szrj ata_ite_ident(device_t dev)
37a1917f14Szrj {
38a1917f14Szrj     struct ata_pci_controller *ctlr = device_get_softc(dev);
3959503772Szrj     static const struct ata_chip_id ids[] =
40*560012aaSzrj     {{ ATA_IT8213F, 0x00, 0x00, 0x00, ATA_UDMA6, "IT8213F" },
41*560012aaSzrj      { ATA_IT8212F, 0x00, 0x00, 0x00, ATA_UDMA6, "IT8212F" },
42a1917f14Szrj      { ATA_IT8211F, 0x00, 0x00, 0x00, ATA_UDMA6, "IT8211F" },
43a1917f14Szrj      { 0, 0, 0, 0, 0, 0}};
44a1917f14Szrj 
4559503772Szrj     if (pci_get_vendor(dev) != ATA_ITE_ID)
46a1917f14Szrj 	return ENXIO;
47a1917f14Szrj 
4859503772Szrj     if (!(ctlr->chip = ata_match_chip(dev, ids)))
4959503772Szrj 	return ENXIO;
5059503772Szrj 
5159503772Szrj     ata_set_desc(dev);
52a1917f14Szrj     ctlr->chipinit = ata_ite_chipinit;
53a1917f14Szrj     return 0;
54a1917f14Szrj }
55a1917f14Szrj 
56a1917f14Szrj static int
ata_ite_chipinit(device_t dev)57a1917f14Szrj ata_ite_chipinit(device_t dev)
58a1917f14Szrj {
59a1917f14Szrj     struct ata_pci_controller *ctlr = device_get_softc(dev);
60a1917f14Szrj 
6143156ad7Szrj     if (ata_setup_interrupt(dev, ata_generic_intr))
62a1917f14Szrj 	return ENXIO;
63a1917f14Szrj 
64*560012aaSzrj     if (ctlr->chip->chipid == ATA_IT8213F) {
65*560012aaSzrj 	/* the ITE 8213F only has one channel */
66*560012aaSzrj 	ctlr->channels = 1;
67a1917f14Szrj 
68*560012aaSzrj 	ctlr->setmode = ata_ite_8213_setmode;
69*560012aaSzrj     }
70*560012aaSzrj     else {
71a1917f14Szrj 	/* set PCI mode and 66Mhz reference clock */
72a1917f14Szrj 	pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) & ~0x83, 1);
73a1917f14Szrj 
74a1917f14Szrj 	/* set default active & recover timings */
75a1917f14Szrj 	pci_write_config(dev, 0x54, 0x31, 1);
76a1917f14Szrj 	pci_write_config(dev, 0x56, 0x31, 1);
77*560012aaSzrj 
78*560012aaSzrj 	ctlr->setmode = ata_ite_821x_setmode;
79*560012aaSzrj     }
80*560012aaSzrj 
81a1917f14Szrj     return 0;
82a1917f14Szrj }
83a1917f14Szrj 
84a1917f14Szrj static void
ata_ite_821x_setmode(device_t dev,int mode)85*560012aaSzrj ata_ite_821x_setmode(device_t dev, int mode)
86a1917f14Szrj {
87a1917f14Szrj     device_t gparent = GRANDPARENT(dev);
88a1917f14Szrj     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
89a1917f14Szrj     struct ata_device *atadev = device_get_softc(dev);
902458a87aSzrj     int devno = (ch->unit << 1) + atadev->unit;
91a1917f14Szrj     int error;
9259503772Szrj 	static const uint8_t udmatiming[] =
9359503772Szrj 		{ 0x44, 0x42, 0x31, 0x21, 0x11, 0xa2, 0x91 };
9459503772Szrj 	static const uint8_t chtiming[] =
9559503772Szrj 		{ 0xaa, 0xa3, 0xa1, 0x33, 0x31, 0x88, 0x32, 0x31 };
96a1917f14Szrj 
97a1917f14Szrj     /* correct the mode for what the HW supports */
98a1917f14Szrj     mode = ata_limit_mode(dev, mode, ATA_UDMA6);
99a1917f14Szrj 
100a1917f14Szrj     /* check the CBLID bits for 80 conductor cable detection */
101a1917f14Szrj     if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x40, 2) &
102a1917f14Szrj 			     (ch->unit ? (1<<3) : (1<<2)))) {
103a1917f14Szrj 	ata_print_cable(dev, "controller");
104a1917f14Szrj 	mode = ATA_UDMA2;
105a1917f14Szrj     }
106a1917f14Szrj 
107a1917f14Szrj     /* set the wanted mode on the device */
108a1917f14Szrj     error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
109a1917f14Szrj 
110a1917f14Szrj     if (bootverbose)
111a1917f14Szrj 	device_printf(dev, "%s setting %s on ITE8212F chip\n",
112a1917f14Szrj 		      (error) ? "failed" : "success", ata_mode2str(mode));
113a1917f14Szrj 
114a1917f14Szrj     /* if the device accepted the mode change, setup the HW accordingly */
115a1917f14Szrj     if (!error) {
116a1917f14Szrj 	if (mode >= ATA_UDMA0) {
117a1917f14Szrj 	    /* enable UDMA mode */
118a1917f14Szrj 	    pci_write_config(gparent, 0x50,
119a1917f14Szrj 			     pci_read_config(gparent, 0x50, 1) &
120a1917f14Szrj 			     ~(1 << (devno + 3)), 1);
121a1917f14Szrj 
122a1917f14Szrj 	    /* set UDMA timing */
123a1917f14Szrj 	    pci_write_config(gparent,
1242458a87aSzrj 			     0x56 + (ch->unit << 2) + atadev->unit,
125a1917f14Szrj 			     udmatiming[mode & ATA_MODE_MASK], 1);
126a1917f14Szrj 	}
127a1917f14Szrj 	else {
128a1917f14Szrj 	    /* disable UDMA mode */
129a1917f14Szrj 	    pci_write_config(gparent, 0x50,
130a1917f14Szrj 			     pci_read_config(gparent, 0x50, 1) |
131a1917f14Szrj 			     (1 << (devno + 3)), 1);
132a1917f14Szrj 
133a1917f14Szrj 	    /* set active and recover timing (shared between master & slave) */
134a1917f14Szrj 	    if (pci_read_config(gparent, 0x54 + (ch->unit << 2), 1) <
135a1917f14Szrj 		chtiming[ata_mode2idx(mode)])
136a1917f14Szrj 		pci_write_config(gparent, 0x54 + (ch->unit << 2),
137a1917f14Szrj 				 chtiming[ata_mode2idx(mode)], 1);
138a1917f14Szrj 	}
139a1917f14Szrj 	atadev->mode = mode;
140a1917f14Szrj     }
141a1917f14Szrj }
142*560012aaSzrj 
143*560012aaSzrj static void
ata_ite_8213_setmode(device_t dev,int mode)144*560012aaSzrj ata_ite_8213_setmode(device_t dev, int mode)
145*560012aaSzrj {
146*560012aaSzrj     device_t gparent = GRANDPARENT(dev);
147*560012aaSzrj     struct ata_pci_controller *ctlr = device_get_softc(gparent);
148*560012aaSzrj     struct ata_device *atadev = device_get_softc(dev);
149*560012aaSzrj     u_int16_t reg40 = pci_read_config(gparent, 0x40, 2);
150*560012aaSzrj     u_int8_t reg44 = pci_read_config(gparent, 0x44, 1);
151*560012aaSzrj     u_int8_t reg48 = pci_read_config(gparent, 0x48, 1);
152*560012aaSzrj     u_int16_t reg4a = pci_read_config(gparent, 0x4a, 2);
153*560012aaSzrj     u_int16_t reg54 = pci_read_config(gparent, 0x54, 2);
154*560012aaSzrj     u_int16_t mask40 = 0, new40 = 0;
155*560012aaSzrj     u_int8_t mask44 = 0, new44 = 0;
156*560012aaSzrj     int devno = atadev->unit;
157*560012aaSzrj     int error;
158*560012aaSzrj 	static const uint8_t timings[] =
159*560012aaSzrj 			 { 0x00, 0x00, 0x10, 0x21, 0x23, 0x10, 0x21, 0x23,
160*560012aaSzrj 			   0x23, 0x23, 0x23, 0x23, 0x23, 0x23 };
161*560012aaSzrj 	static const uint8_t utimings[] =
162*560012aaSzrj 			 { 0x00, 0x01, 0x10, 0x01, 0x10, 0x01, 0x10 };
163*560012aaSzrj 
164*560012aaSzrj     mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
165*560012aaSzrj 
166*560012aaSzrj     if (mode > ATA_UDMA2 && !(reg54 & (0x10 << devno))) {
167*560012aaSzrj 	ata_print_cable(dev, "controller");
168*560012aaSzrj 	mode = ATA_UDMA2;
169*560012aaSzrj     }
170*560012aaSzrj 
171*560012aaSzrj     error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
172*560012aaSzrj 
173*560012aaSzrj     if (bootverbose)
174*560012aaSzrj 	device_printf(dev, "%s setting %s on %s chip\n",
175*560012aaSzrj 		      (error) ? "FAILURE" : "",
176*560012aaSzrj 		      ata_mode2str(mode), ctlr->chip->text);
177*560012aaSzrj     if (!error) {
178*560012aaSzrj 	if (mode >= ATA_UDMA0) {
179*560012aaSzrj 	    pci_write_config(gparent, 0x48, reg48 | (0x0001 << devno), 2);
180*560012aaSzrj 	    pci_write_config(gparent, 0x4a,
181*560012aaSzrj 			     (reg4a & ~(0x3 << (devno << 2))) |
182*560012aaSzrj 			     (utimings[mode & ATA_MODE_MASK] << (devno<<2)), 2);
183*560012aaSzrj 	}
184*560012aaSzrj 	else {
185*560012aaSzrj 	    pci_write_config(gparent, 0x48, reg48 & ~(0x0001 << devno), 2);
186*560012aaSzrj 	    pci_write_config(gparent, 0x4a, (reg4a & ~(0x3 << (devno << 2))),2);
187*560012aaSzrj 	}
188*560012aaSzrj 	if (mode >= ATA_UDMA2)
189*560012aaSzrj 	    reg54 |= (0x1 << devno);
190*560012aaSzrj 	else
191*560012aaSzrj 	    reg54 &= ~(0x1 << devno);
192*560012aaSzrj 	if (mode >= ATA_UDMA5)
193*560012aaSzrj 	    reg54 |= (0x1000 << devno);
194*560012aaSzrj 	else
195*560012aaSzrj 	    reg54 &= ~(0x1000 << devno);
196*560012aaSzrj 	pci_write_config(gparent, 0x54, reg54, 2);
197*560012aaSzrj 
198*560012aaSzrj 	reg40 &= 0xff00;
199*560012aaSzrj 	reg40 |= 0x4033;
200*560012aaSzrj 	if (atadev->unit == ATA_MASTER) {
201*560012aaSzrj 	    reg40 |= (ata_atapi(dev) ? 0x04 : 0x00);
202*560012aaSzrj 	    mask40 = 0x3300;
203*560012aaSzrj 	    new40 = timings[ata_mode2idx(mode)] << 8;
204*560012aaSzrj 	}
205*560012aaSzrj 	else {
206*560012aaSzrj 	    reg40 |= (ata_atapi(dev) ? 0x40 : 0x00);
207*560012aaSzrj 	    mask44 = 0x0f;
208*560012aaSzrj 	    new44 = ((timings[ata_mode2idx(mode)] & 0x30) >> 2) |
209*560012aaSzrj 		    (timings[ata_mode2idx(mode)] & 0x03);
210*560012aaSzrj 	}
211*560012aaSzrj 	pci_write_config(gparent, 0x40, (reg40 & ~mask40) | new40, 4);
212*560012aaSzrj 	pci_write_config(gparent, 0x44, (reg44 & ~mask44) | new44, 1);
213*560012aaSzrj 
214*560012aaSzrj 	atadev->mode = mode;
215*560012aaSzrj     }
216*560012aaSzrj }
217