xref: /dflybsd-src/sys/dev/disk/nata/chipsets/ata-highpoint.c (revision 2458a87a718a37261418254ad47f5f9adc3366d4)
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_highpoint_chipinit(device_t dev);
29a1917f14Szrj static int ata_highpoint_allocate(device_t dev);
30a1917f14Szrj static void ata_highpoint_setmode(device_t dev, int mode);
31a1917f14Szrj static int ata_highpoint_check_80pin(device_t dev, int mode);
32a1917f14Szrj 
33853eb30dSzrj /* misc defines */
34853eb30dSzrj #define HPT_366		0
35853eb30dSzrj #define HPT_370		1
36853eb30dSzrj #define HPT_372		2
37853eb30dSzrj #define HPT_374		3
38853eb30dSzrj #define HPT_OLD		0x01
39853eb30dSzrj 
40a1917f14Szrj /*
41a1917f14Szrj  * HighPoint chipset support functions
42a1917f14Szrj  */
43a1917f14Szrj int
ata_highpoint_ident(device_t dev)44a1917f14Szrj ata_highpoint_ident(device_t dev)
45a1917f14Szrj {
46a1917f14Szrj     struct ata_pci_controller *ctlr = device_get_softc(dev);
4759503772Szrj     const struct ata_chip_id *idx;
4859503772Szrj     static const struct ata_chip_id ids[] =
49853eb30dSzrj     {{ ATA_HPT374, 0x07, HPT_374, 0x00,    ATA_UDMA6, "HPT374" },
50853eb30dSzrj      { ATA_HPT372, 0x02, HPT_372, 0x00,    ATA_UDMA6, "HPT372N" },
51853eb30dSzrj      { ATA_HPT372, 0x01, HPT_372, 0x00,    ATA_UDMA6, "HPT372" },
52853eb30dSzrj      { ATA_HPT371, 0x01, HPT_372, 0x00,    ATA_UDMA6, "HPT371" },
53853eb30dSzrj      { ATA_HPT366, 0x05, HPT_372, 0x00,    ATA_UDMA6, "HPT372" },
54853eb30dSzrj      { ATA_HPT366, 0x03, HPT_370, 0x00,    ATA_UDMA5, "HPT370" },
55853eb30dSzrj      { ATA_HPT366, 0x02, HPT_366, 0x00,    ATA_UDMA4, "HPT368" },
56853eb30dSzrj      { ATA_HPT366, 0x00, HPT_366, HPT_OLD, ATA_UDMA4, "HPT366" },
57853eb30dSzrj      { ATA_HPT302, 0x01, HPT_372, 0x00,    ATA_UDMA6, "HPT302" },
58a1917f14Szrj      { 0, 0, 0, 0, 0, 0}};
59a1917f14Szrj     char buffer[64];
60a1917f14Szrj 
6159503772Szrj     if (pci_get_vendor(dev) != ATA_HIGHPOINT_ID)
6259503772Szrj         return ENXIO;
6359503772Szrj 
64a1917f14Szrj     if (!(idx = ata_match_chip(dev, ids)))
65a1917f14Szrj 	return ENXIO;
66a1917f14Szrj 
67a1917f14Szrj     strcpy(buffer, "HighPoint ");
68a1917f14Szrj     strcat(buffer, idx->text);
69853eb30dSzrj     if (idx->cfg1 == HPT_374) {
70a1917f14Szrj 	if (pci_get_function(dev) == 0)
71a1917f14Szrj 	    strcat(buffer, " (channel 0+1)");
72a1917f14Szrj 	if (pci_get_function(dev) == 1)
73a1917f14Szrj 	    strcat(buffer, " (channel 2+3)");
74a1917f14Szrj     }
75a1917f14Szrj     ksprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
76a1917f14Szrj     device_set_desc_copy(dev, buffer);
77a1917f14Szrj     ctlr->chip = idx;
78a1917f14Szrj     ctlr->chipinit = ata_highpoint_chipinit;
79a1917f14Szrj     return 0;
80a1917f14Szrj }
81a1917f14Szrj 
82a1917f14Szrj static int
ata_highpoint_chipinit(device_t dev)83a1917f14Szrj ata_highpoint_chipinit(device_t dev)
84a1917f14Szrj {
85a1917f14Szrj     struct ata_pci_controller *ctlr = device_get_softc(dev);
86a1917f14Szrj 
8743156ad7Szrj     if (ata_setup_interrupt(dev, ata_generic_intr))
88a1917f14Szrj 	return ENXIO;
89a1917f14Szrj 
90853eb30dSzrj     if (ctlr->chip->cfg2 == HPT_OLD) {
91a1917f14Szrj 	/* disable interrupt prediction */
92a1917f14Szrj 	pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
93a1917f14Szrj     }
94a1917f14Szrj     else {
95a1917f14Szrj 	/* disable interrupt prediction */
96a1917f14Szrj 	pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
97a1917f14Szrj 	pci_write_config(dev, 0x55, (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
98a1917f14Szrj 
99a1917f14Szrj 	/* enable interrupts */
100a1917f14Szrj 	pci_write_config(dev, 0x5a, (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
101a1917f14Szrj 
102a1917f14Szrj 	/* set clocks etc */
103853eb30dSzrj 	if (ctlr->chip->cfg1 < HPT_372)
104a1917f14Szrj 	    pci_write_config(dev, 0x5b, 0x22, 1);
105a1917f14Szrj 	else
106a1917f14Szrj 	    pci_write_config(dev, 0x5b,
107a1917f14Szrj 			     (pci_read_config(dev, 0x5b, 1) & 0x01) | 0x20, 1);
108a1917f14Szrj     }
109a1917f14Szrj     ctlr->allocate = ata_highpoint_allocate;
110a1917f14Szrj     ctlr->setmode = ata_highpoint_setmode;
111a1917f14Szrj     return 0;
112a1917f14Szrj }
113a1917f14Szrj 
114a1917f14Szrj static int
ata_highpoint_allocate(device_t dev)115a1917f14Szrj ata_highpoint_allocate(device_t dev)
116a1917f14Szrj {
117a1917f14Szrj     struct ata_channel *ch = device_get_softc(dev);
118a1917f14Szrj 
119a1917f14Szrj     /* setup the usual register normal pci style */
120a1917f14Szrj     if (ata_pci_allocate(dev))
121a1917f14Szrj 	return ENXIO;
122a1917f14Szrj 
123a1917f14Szrj     ch->flags |= ATA_ALWAYS_DMASTAT;
124a1917f14Szrj     return 0;
125a1917f14Szrj }
126a1917f14Szrj 
127a1917f14Szrj static void
ata_highpoint_setmode(device_t dev,int mode)128a1917f14Szrj ata_highpoint_setmode(device_t dev, int mode)
129a1917f14Szrj {
130a1917f14Szrj 	device_t gparent = GRANDPARENT(dev);
131a1917f14Szrj 	struct ata_pci_controller *ctlr = device_get_softc(gparent);
132a1917f14Szrj 	struct ata_channel *ch = device_get_softc(device_get_parent(dev));
133a1917f14Szrj 	struct ata_device *atadev = device_get_softc(dev);
134*2458a87aSzrj 	int devno = (ch->unit << 1) + atadev->unit;
135a1917f14Szrj 	int error;
13659503772Szrj 	static const uint32_t timings33[][4] = {
137a1917f14Szrj 	/*    HPT366      HPT370      HPT372      HPT374           mode */
138a1917f14Szrj 	{ 0x40d0a7aa, 0x06914e57, 0x0d029d5e, 0x0ac1f48a },     /* PIO 0 */
139a1917f14Szrj 	{ 0x40d0a7a3, 0x06914e43, 0x0d029d26, 0x0ac1f465 },     /* PIO 1 */
140a1917f14Szrj 	{ 0x40d0a753, 0x06514e33, 0x0c829ca6, 0x0a81f454 },     /* PIO 2 */
141a1917f14Szrj 	{ 0x40c8a742, 0x06514e22, 0x0c829c84, 0x0a81f443 },     /* PIO 3 */
142a1917f14Szrj 	{ 0x40c8a731, 0x06514e21, 0x0c829c62, 0x0a81f442 },     /* PIO 4 */
143a1917f14Szrj 	{ 0x20c8a797, 0x26514e97, 0x2c82922e, 0x228082ea },     /* MWDMA 0 */
144a1917f14Szrj 	{ 0x20c8a732, 0x26514e33, 0x2c829266, 0x22808254 },     /* MWDMA 1 */
145a1917f14Szrj 	{ 0x20c8a731, 0x26514e21, 0x2c829262, 0x22808242 },     /* MWDMA 2 */
146a1917f14Szrj 	{ 0x10c8a731, 0x16514e31, 0x1c829c62, 0x121882ea },     /* UDMA 0 */
147a1917f14Szrj 	{ 0x10cba731, 0x164d4e31, 0x1c9a9c62, 0x12148254 },     /* UDMA 1 */
148a1917f14Szrj 	{ 0x10caa731, 0x16494e31, 0x1c929c62, 0x120c8242 },     /* UDMA 2 */
149a1917f14Szrj 	{ 0x10cfa731, 0x166d4e31, 0x1c8e9c62, 0x128c8242 },     /* UDMA 3 */
150a1917f14Szrj 	{ 0x10c9a731, 0x16454e31, 0x1c8a9c62, 0x12ac8242 },     /* UDMA 4 */
151a1917f14Szrj 	{ 0,          0x16454e31, 0x1c8a9c62, 0x12848242 },     /* UDMA 5 */
152a1917f14Szrj 	{ 0,          0,          0x1c869c62, 0x12808242 }      /* UDMA 6 */
153a1917f14Szrj 	};
154a1917f14Szrj 
155a1917f14Szrj     mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
156a1917f14Szrj 
157853eb30dSzrj     if (ctlr->chip->cfg1 == HPT_366 && ata_atapi(dev))
158a1917f14Szrj 	mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
159a1917f14Szrj 
160a1917f14Szrj     mode = ata_highpoint_check_80pin(dev, mode);
161a1917f14Szrj 
162a1917f14Szrj     /*
163a1917f14Szrj      * most if not all HPT chips cant really handle that the device is
164a1917f14Szrj      * running at ATA_UDMA6/ATA133 speed, so we cheat at set the device to
165a1917f14Szrj      * a max of ATA_UDMA5/ATA100 to guard against suboptimal performance
166a1917f14Szrj      */
167a1917f14Szrj     error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
168a1917f14Szrj 			   ata_limit_mode(dev, mode, ATA_UDMA5));
169a1917f14Szrj     if (bootverbose)
170a1917f14Szrj 	device_printf(dev, "%ssetting %s on HighPoint chip\n",
171a1917f14Szrj 		      (error) ? "FAILURE " : "", ata_mode2str(mode));
172a1917f14Szrj     if (!error)
173a1917f14Szrj 	pci_write_config(gparent, 0x40 + (devno << 2),
174a1917f14Szrj 			 timings33[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
175a1917f14Szrj     atadev->mode = mode;
176a1917f14Szrj }
177a1917f14Szrj 
178a1917f14Szrj static int
ata_highpoint_check_80pin(device_t dev,int mode)179a1917f14Szrj ata_highpoint_check_80pin(device_t dev, int mode)
180a1917f14Szrj {
181a1917f14Szrj     device_t gparent = GRANDPARENT(dev);
182a1917f14Szrj     struct ata_pci_controller *ctlr = device_get_softc(gparent);
183a1917f14Szrj     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
184a1917f14Szrj     u_int8_t reg, val, res;
185a1917f14Szrj 
186853eb30dSzrj     if (ctlr->chip->cfg1 == HPT_374 && pci_get_function(gparent) == 1) {
187a1917f14Szrj 	reg = ch->unit ? 0x57 : 0x53;
188a1917f14Szrj 	val = pci_read_config(gparent, reg, 1);
189a1917f14Szrj 	pci_write_config(gparent, reg, val | 0x80, 1);
190a1917f14Szrj     }
191a1917f14Szrj     else {
192a1917f14Szrj 	reg = 0x5b;
193a1917f14Szrj 	val = pci_read_config(gparent, reg, 1);
194a1917f14Szrj 	pci_write_config(gparent, reg, val & 0xfe, 1);
195a1917f14Szrj     }
196a1917f14Szrj     res = pci_read_config(gparent, 0x5a, 1) & (ch->unit ? 0x1:0x2);
197a1917f14Szrj     pci_write_config(gparent, reg, val, 1);
198a1917f14Szrj 
199a1917f14Szrj     if (mode > ATA_UDMA2 && res) {
200a1917f14Szrj 	ata_print_cable(dev, "controller");
201a1917f14Szrj 	mode = ATA_UDMA2;
202a1917f14Szrj     }
203a1917f14Szrj     return mode;
204a1917f14Szrj }
205