xref: /dragonfly/sys/dev/disk/nata/chipsets/ata-sis.c (revision 43156ad7)
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_sis_chipinit(device_t dev);
29a1917f14Szrj static int ata_sis_allocate(device_t dev);
30a1917f14Szrj static void ata_sis_reset(device_t dev);
31a1917f14Szrj static void ata_sis_setmode(device_t dev, int mode);
32a1917f14Szrj 
33853eb30dSzrj /* misc defines */
34853eb30dSzrj #define SIS_33		1
35853eb30dSzrj #define SIS_66		2
36853eb30dSzrj #define SIS_100NEW	3
37853eb30dSzrj #define SIS_100OLD	4
38853eb30dSzrj #define SIS_133NEW	5
39853eb30dSzrj #define SIS_133OLD	6
40853eb30dSzrj #define SIS_SATA	7
41853eb30dSzrj 
42a1917f14Szrj /*
43a1917f14Szrj  * Silicon Integrated Systems Corp. (SiS) chipset support functions
44a1917f14Szrj  */
45a1917f14Szrj int
46a1917f14Szrj ata_sis_ident(device_t dev)
47a1917f14Szrj {
48a1917f14Szrj     struct ata_pci_controller *ctlr = device_get_softc(dev);
4959503772Szrj     const struct ata_chip_id *idx;
5059503772Szrj     static const struct ata_chip_id ids[] =
51853eb30dSzrj     {{ ATA_SIS182,  0x00, SIS_SATA,   0, ATA_SA150, "182" }, /* south */
52853eb30dSzrj      { ATA_SIS181,  0x00, SIS_SATA,   0, ATA_SA150, "181" }, /* south */
53853eb30dSzrj      { ATA_SIS180,  0x00, SIS_SATA,   0, ATA_SA150, "180" }, /* south */
54853eb30dSzrj      { ATA_SIS965,  0x00, SIS_133NEW, 0, ATA_UDMA6, "965" }, /* south */
55853eb30dSzrj      { ATA_SIS964,  0x00, SIS_133NEW, 0, ATA_UDMA6, "964" }, /* south */
56853eb30dSzrj      { ATA_SIS963,  0x00, SIS_133NEW, 0, ATA_UDMA6, "963" }, /* south */
57853eb30dSzrj      { ATA_SIS962,  0x00, SIS_133NEW, 0, ATA_UDMA6, "962" }, /* south */
58a1917f14Szrj 
59853eb30dSzrj      { ATA_SIS745,  0x00, SIS_100NEW, 0, ATA_UDMA5, "745" }, /* 1chip */
60853eb30dSzrj      { ATA_SIS735,  0x00, SIS_100NEW, 0, ATA_UDMA5, "735" }, /* 1chip */
61853eb30dSzrj      { ATA_SIS733,  0x00, SIS_100NEW, 0, ATA_UDMA5, "733" }, /* 1chip */
62853eb30dSzrj      { ATA_SIS730,  0x00, SIS_100OLD, 0, ATA_UDMA5, "730" }, /* 1chip */
63a1917f14Szrj 
64853eb30dSzrj      { ATA_SIS635,  0x00, SIS_100NEW, 0, ATA_UDMA5, "635" }, /* 1chip */
65853eb30dSzrj      { ATA_SIS633,  0x00, SIS_100NEW, 0, ATA_UDMA5, "633" }, /* unknown */
66853eb30dSzrj      { ATA_SIS630,  0x30, SIS_100OLD, 0, ATA_UDMA5, "630S"}, /* 1chip */
67853eb30dSzrj      { ATA_SIS630,  0x00, SIS_66,     0, ATA_UDMA4, "630" }, /* 1chip */
68853eb30dSzrj      { ATA_SIS620,  0x00, SIS_66,     0, ATA_UDMA4, "620" }, /* 1chip */
69a1917f14Szrj 
70853eb30dSzrj      { ATA_SIS550,  0x00, SIS_66,     0, ATA_UDMA5, "550" },
71853eb30dSzrj      { ATA_SIS540,  0x00, SIS_66,     0, ATA_UDMA4, "540" },
72853eb30dSzrj      { ATA_SIS530,  0x00, SIS_66,     0, ATA_UDMA4, "530" },
73a1917f14Szrj 
74853eb30dSzrj      { ATA_SIS5513, 0xc2, SIS_33,     1, ATA_UDMA2, "5513" },
75853eb30dSzrj      { ATA_SIS5513, 0x00, SIS_33,     1, ATA_WDMA2, "5513" },
76a1917f14Szrj      { 0, 0, 0, 0, 0, 0 }};
7759503772Szrj     static struct ata_chip_id id[] =
7859503772Szrj     {{ ATA_SISSOUTH, 0x10, 0, 0, 0, "" }, { 0, 0, 0, 0, 0, 0 }};
79a1917f14Szrj     char buffer[64];
80a1917f14Szrj     int found = 0;
81a1917f14Szrj 
8259503772Szrj     if (pci_get_vendor(dev) != ATA_SIS_ID)
8359503772Szrj 	return ENXIO;
8459503772Szrj 
85a1917f14Szrj     if (!(idx = ata_find_chip(dev, ids, -pci_get_slot(dev))))
86a1917f14Szrj 	return ENXIO;
87a1917f14Szrj 
88a1917f14Szrj     if (idx->cfg2 && !found) {
89a1917f14Szrj 	u_int8_t reg57 = pci_read_config(dev, 0x57, 1);
90a1917f14Szrj 
91a1917f14Szrj 	pci_write_config(dev, 0x57, (reg57 & 0x7f), 1);
92a1917f14Szrj 	if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5518) {
93a1917f14Szrj 	    found = 1;
9459503772Szrj 	    memcpy(&id[0], idx, sizeof(id[0]));
9559503772Szrj 	    id[0].cfg1 = SIS_133NEW;
9659503772Szrj 	    id[0].max_dma = ATA_UDMA6;
97a1917f14Szrj 	    ksprintf(buffer, "SiS 962/963 %s controller",
98a1917f14Szrj 		    ata_mode2str(idx->max_dma));
99a1917f14Szrj 	}
100a1917f14Szrj 	pci_write_config(dev, 0x57, reg57, 1);
101a1917f14Szrj     }
102a1917f14Szrj     if (idx->cfg2 && !found) {
103a1917f14Szrj 	u_int8_t reg4a = pci_read_config(dev, 0x4a, 1);
104a1917f14Szrj 
105a1917f14Szrj 	pci_write_config(dev, 0x4a, (reg4a | 0x10), 1);
106a1917f14Szrj 	if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5517) {
107a1917f14Szrj 	    found = 1;
108a1917f14Szrj 	    if (ata_find_chip(dev, id, pci_get_slot(dev))) {
10959503772Szrj 		id[0].cfg1 = SIS_133OLD;
11059503772Szrj 		id[0].max_dma = ATA_UDMA6;
11159503772Szrj 	    } else {
11259503772Szrj 		id[0].cfg1 = SIS_100NEW;
11359503772Szrj 		id[0].max_dma = ATA_UDMA5;
114a1917f14Szrj 	    }
115a1917f14Szrj 	    ksprintf(buffer, "SiS 961 %s controller",ata_mode2str(idx->max_dma));
116a1917f14Szrj 	}
117a1917f14Szrj 	pci_write_config(dev, 0x4a, reg4a, 1);
118a1917f14Szrj     }
119a1917f14Szrj     if (!found)
120a1917f14Szrj 	ksprintf(buffer,"SiS %s %s controller",
121a1917f14Szrj 		idx->text, ata_mode2str(idx->max_dma));
12259503772Szrj     else
12359503772Szrj 	idx = &id[0];
124a1917f14Szrj 
125a1917f14Szrj     device_set_desc_copy(dev, buffer);
126a1917f14Szrj     ctlr->chip = idx;
127a1917f14Szrj     ctlr->chipinit = ata_sis_chipinit;
128a1917f14Szrj     return 0;
129a1917f14Szrj }
130a1917f14Szrj 
131a1917f14Szrj static int
132a1917f14Szrj ata_sis_chipinit(device_t dev)
133a1917f14Szrj {
134a1917f14Szrj     struct ata_pci_controller *ctlr = device_get_softc(dev);
135a1917f14Szrj 
136*43156ad7Szrj     if (ata_setup_interrupt(dev, ata_generic_intr))
137a1917f14Szrj 	return ENXIO;
138a1917f14Szrj 
139a1917f14Szrj     switch (ctlr->chip->cfg1) {
140853eb30dSzrj     case SIS_33:
141a1917f14Szrj 	break;
142853eb30dSzrj     case SIS_66:
143853eb30dSzrj     case SIS_100OLD:
144a1917f14Szrj 	pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 1) & ~0x04, 1);
145a1917f14Szrj 	break;
146853eb30dSzrj     case SIS_100NEW:
147853eb30dSzrj     case SIS_133OLD:
148a1917f14Szrj 	pci_write_config(dev, 0x49, pci_read_config(dev, 0x49, 1) & ~0x01, 1);
149a1917f14Szrj 	break;
150853eb30dSzrj     case SIS_133NEW:
151a1917f14Szrj 	pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 2) | 0x0008, 2);
152a1917f14Szrj 	pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 2) | 0x0008, 2);
153a1917f14Szrj 	break;
154853eb30dSzrj     case SIS_SATA:
155a1917f14Szrj 	ctlr->r_type2 = SYS_RES_IOPORT;
156a1917f14Szrj 	ctlr->r_rid2 = PCIR_BAR(5);
157a1917f14Szrj 	if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
158a1917f14Szrj 						   &ctlr->r_rid2, RF_ACTIVE))) {
159a1917f14Szrj 	    ctlr->allocate = ata_sis_allocate;
160a1917f14Szrj 	    ctlr->reset = ata_sis_reset;
161a1917f14Szrj 
162a1917f14Szrj 	    /* enable PCI interrupt */
163a1917f14Szrj 	    pci_write_config(dev, PCIR_COMMAND,
164a1917f14Szrj 			     pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
165a1917f14Szrj 	}
166a1917f14Szrj 	ctlr->setmode = ata_sata_setmode;
167a1917f14Szrj 	return 0;
168a1917f14Szrj     default:
169a1917f14Szrj 	ata_teardown_interrupt(dev);
170a1917f14Szrj 	return ENXIO;
171a1917f14Szrj     }
172a1917f14Szrj     ctlr->setmode = ata_sis_setmode;
173a1917f14Szrj     return 0;
174a1917f14Szrj }
175a1917f14Szrj 
176a1917f14Szrj static int
177a1917f14Szrj ata_sis_allocate(device_t dev)
178a1917f14Szrj {
179a1917f14Szrj     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
180a1917f14Szrj     struct ata_channel *ch = device_get_softc(dev);
181a1917f14Szrj     int offset = ch->unit << ((ctlr->chip->chipid == ATA_SIS182) ? 5 : 6);
182a1917f14Szrj 
183a1917f14Szrj     /* setup the usual register normal pci style */
184a1917f14Szrj     if (ata_pci_allocate(dev))
185a1917f14Szrj 	return ENXIO;
186a1917f14Szrj 
187a1917f14Szrj     ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
188a1917f14Szrj     ch->r_io[ATA_SSTATUS].offset = 0x00 + offset;
189a1917f14Szrj     ch->r_io[ATA_SERROR].res = ctlr->r_res2;
190a1917f14Szrj     ch->r_io[ATA_SERROR].offset = 0x04 + offset;
191a1917f14Szrj     ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
192a1917f14Szrj     ch->r_io[ATA_SCONTROL].offset = 0x08 + offset;
193a1917f14Szrj     ch->flags |= ATA_NO_SLAVE;
194a1917f14Szrj 
195a1917f14Szrj     /* XXX SOS PHY hotplug handling missing in SiS chip ?? */
196a1917f14Szrj     /* XXX SOS unknown how to enable PHY state change interrupt */
197a1917f14Szrj     return 0;
198a1917f14Szrj }
199a1917f14Szrj 
200a1917f14Szrj static void
201a1917f14Szrj ata_sis_reset(device_t dev)
202a1917f14Szrj {
203a1917f14Szrj     if (ata_sata_phy_reset(dev))
204a1917f14Szrj 	ata_generic_reset(dev);
205a1917f14Szrj }
206a1917f14Szrj 
207a1917f14Szrj static void
208a1917f14Szrj ata_sis_setmode(device_t dev, int mode)
209a1917f14Szrj {
210a1917f14Szrj     device_t gparent = GRANDPARENT(dev);
211a1917f14Szrj     struct ata_pci_controller *ctlr = device_get_softc(gparent);
212a1917f14Szrj     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
213a1917f14Szrj     struct ata_device *atadev = device_get_softc(dev);
214a1917f14Szrj     int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
215a1917f14Szrj     int error;
216a1917f14Szrj 
217a1917f14Szrj     mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
218a1917f14Szrj 
219853eb30dSzrj     if (ctlr->chip->cfg1 == SIS_133NEW) {
220a1917f14Szrj 	if (mode > ATA_UDMA2 &&
221a1917f14Szrj 	    pci_read_config(gparent, ch->unit ? 0x52 : 0x50,2) & 0x8000) {
222a1917f14Szrj 	    ata_print_cable(dev, "controller");
223a1917f14Szrj 	    mode = ATA_UDMA2;
224a1917f14Szrj 	}
225a1917f14Szrj     }
226a1917f14Szrj     else {
227a1917f14Szrj 	if (mode > ATA_UDMA2 &&
228a1917f14Szrj 	    pci_read_config(gparent, 0x48, 1)&(ch->unit ? 0x20 : 0x10)) {
229a1917f14Szrj 	    ata_print_cable(dev, "controller");
230a1917f14Szrj 	    mode = ATA_UDMA2;
231a1917f14Szrj 	}
232a1917f14Szrj     }
233a1917f14Szrj 
234a1917f14Szrj     error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
235a1917f14Szrj 
236a1917f14Szrj     if (bootverbose)
237a1917f14Szrj 	device_printf(dev, "%ssetting %s on %s chip\n",
238a1917f14Szrj 		      (error) ? "FAILURE " : "",
239a1917f14Szrj 		      ata_mode2str(mode), ctlr->chip->text);
240a1917f14Szrj     if (!error) {
241a1917f14Szrj 	switch (ctlr->chip->cfg1) {
242853eb30dSzrj 	case SIS_133NEW: {
24359503772Szrj 	    static const uint32_t timings[] =
244a1917f14Szrj 		{ 0x28269008, 0x0c266008, 0x04263008, 0x0c0a3008, 0x05093008,
245a1917f14Szrj 		  0x22196008, 0x0c0a3008, 0x05093008, 0x050939fc, 0x050936ac,
246a1917f14Szrj 		  0x0509347c, 0x0509325c, 0x0509323c, 0x0509322c, 0x0509321c};
247a1917f14Szrj 	    u_int32_t reg;
248a1917f14Szrj 
249a1917f14Szrj 	    reg = (pci_read_config(gparent, 0x57, 1)&0x40?0x70:0x40)+(devno<<2);
250a1917f14Szrj 	    pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 4);
251a1917f14Szrj 	    break;
252a1917f14Szrj 	    }
253853eb30dSzrj 	case SIS_133OLD: {
25459503772Szrj 	    static const uint16_t timings[] =
255a1917f14Szrj 	     { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033, 0x0031,
256a1917f14Szrj 	       0x8f31, 0x8a31, 0x8731, 0x8531, 0x8331, 0x8231, 0x8131 };
257a1917f14Szrj 
258a1917f14Szrj 	    u_int16_t reg = 0x40 + (devno << 1);
259a1917f14Szrj 
260a1917f14Szrj 	    pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
261a1917f14Szrj 	    break;
262a1917f14Szrj 	    }
263853eb30dSzrj 	case SIS_100NEW: {
26459503772Szrj 	    static const uint16_t timings[] =
265a1917f14Szrj 		{ 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033,
266a1917f14Szrj 		  0x0031, 0x8b31, 0x8731, 0x8531, 0x8431, 0x8231, 0x8131 };
267a1917f14Szrj 	    u_int16_t reg = 0x40 + (devno << 1);
268a1917f14Szrj 
269a1917f14Szrj 	    pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
270a1917f14Szrj 	    break;
271a1917f14Szrj 	    }
272853eb30dSzrj 	case SIS_100OLD:
273853eb30dSzrj 	case SIS_66:
274853eb30dSzrj 	case SIS_33: {
27559503772Szrj 	    static const uint16_t timings[] =
276a1917f14Szrj 		{ 0x0c0b, 0x0607, 0x0404, 0x0303, 0x0301, 0x0404, 0x0303,
277a1917f14Szrj 		  0x0301, 0xf301, 0xd301, 0xb301, 0xa301, 0x9301, 0x8301 };
278a1917f14Szrj 	    u_int16_t reg = 0x40 + (devno << 1);
279a1917f14Szrj 
280a1917f14Szrj 	    pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
281a1917f14Szrj 	    break;
282a1917f14Szrj 	    }
283a1917f14Szrj 	}
284a1917f14Szrj 	atadev->mode = mode;
285a1917f14Szrj     }
286a1917f14Szrj }
287