xref: /dragonfly/sys/dev/disk/nata/chipsets/ata-sis.c (revision 2458a87a)
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
ata_sis_ident(device_t dev)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 
82954c7881Szrj     if (pci_get_class(dev) != PCIC_STORAGE)
83954c7881Szrj 	return (ENXIO);
84954c7881Szrj 
8559503772Szrj     if (pci_get_vendor(dev) != ATA_SIS_ID)
8659503772Szrj 	return ENXIO;
8759503772Szrj 
88a1917f14Szrj     if (!(idx = ata_find_chip(dev, ids, -pci_get_slot(dev))))
89a1917f14Szrj 	return ENXIO;
90a1917f14Szrj 
91a1917f14Szrj     if (idx->cfg2 && !found) {
92a1917f14Szrj 	u_int8_t reg57 = pci_read_config(dev, 0x57, 1);
93a1917f14Szrj 
94a1917f14Szrj 	pci_write_config(dev, 0x57, (reg57 & 0x7f), 1);
95a1917f14Szrj 	if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5518) {
96a1917f14Szrj 	    found = 1;
9759503772Szrj 	    memcpy(&id[0], idx, sizeof(id[0]));
9859503772Szrj 	    id[0].cfg1 = SIS_133NEW;
9959503772Szrj 	    id[0].max_dma = ATA_UDMA6;
100a1917f14Szrj 	    ksprintf(buffer, "SiS 962/963 %s controller",
101a1917f14Szrj 		    ata_mode2str(idx->max_dma));
102a1917f14Szrj 	}
103a1917f14Szrj 	pci_write_config(dev, 0x57, reg57, 1);
104a1917f14Szrj     }
105a1917f14Szrj     if (idx->cfg2 && !found) {
106a1917f14Szrj 	u_int8_t reg4a = pci_read_config(dev, 0x4a, 1);
107a1917f14Szrj 
108a1917f14Szrj 	pci_write_config(dev, 0x4a, (reg4a | 0x10), 1);
109a1917f14Szrj 	if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5517) {
110a1917f14Szrj 	    found = 1;
111a1917f14Szrj 	    if (ata_find_chip(dev, id, pci_get_slot(dev))) {
11259503772Szrj 		id[0].cfg1 = SIS_133OLD;
11359503772Szrj 		id[0].max_dma = ATA_UDMA6;
11459503772Szrj 	    } else {
11559503772Szrj 		id[0].cfg1 = SIS_100NEW;
11659503772Szrj 		id[0].max_dma = ATA_UDMA5;
117a1917f14Szrj 	    }
118a1917f14Szrj 	    ksprintf(buffer, "SiS 961 %s controller",ata_mode2str(idx->max_dma));
119a1917f14Szrj 	}
120a1917f14Szrj 	pci_write_config(dev, 0x4a, reg4a, 1);
121a1917f14Szrj     }
122a1917f14Szrj     if (!found)
123a1917f14Szrj 	ksprintf(buffer,"SiS %s %s controller",
124a1917f14Szrj 		idx->text, ata_mode2str(idx->max_dma));
12559503772Szrj     else
12659503772Szrj 	idx = &id[0];
127a1917f14Szrj 
128a1917f14Szrj     device_set_desc_copy(dev, buffer);
129a1917f14Szrj     ctlr->chip = idx;
130a1917f14Szrj     ctlr->chipinit = ata_sis_chipinit;
131a1917f14Szrj     return 0;
132a1917f14Szrj }
133a1917f14Szrj 
134a1917f14Szrj static int
ata_sis_chipinit(device_t dev)135a1917f14Szrj ata_sis_chipinit(device_t dev)
136a1917f14Szrj {
137a1917f14Szrj     struct ata_pci_controller *ctlr = device_get_softc(dev);
138a1917f14Szrj 
13943156ad7Szrj     if (ata_setup_interrupt(dev, ata_generic_intr))
140a1917f14Szrj 	return ENXIO;
141a1917f14Szrj 
142a1917f14Szrj     switch (ctlr->chip->cfg1) {
143853eb30dSzrj     case SIS_33:
144a1917f14Szrj 	break;
145853eb30dSzrj     case SIS_66:
146853eb30dSzrj     case SIS_100OLD:
147a1917f14Szrj 	pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 1) & ~0x04, 1);
148a1917f14Szrj 	break;
149853eb30dSzrj     case SIS_100NEW:
150853eb30dSzrj     case SIS_133OLD:
151a1917f14Szrj 	pci_write_config(dev, 0x49, pci_read_config(dev, 0x49, 1) & ~0x01, 1);
152a1917f14Szrj 	break;
153853eb30dSzrj     case SIS_133NEW:
154a1917f14Szrj 	pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 2) | 0x0008, 2);
155a1917f14Szrj 	pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 2) | 0x0008, 2);
156a1917f14Szrj 	break;
157853eb30dSzrj     case SIS_SATA:
158a1917f14Szrj 	ctlr->r_type2 = SYS_RES_IOPORT;
159a1917f14Szrj 	ctlr->r_rid2 = PCIR_BAR(5);
160a1917f14Szrj 	if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
161a1917f14Szrj 						   &ctlr->r_rid2, RF_ACTIVE))) {
162a1917f14Szrj 	    ctlr->allocate = ata_sis_allocate;
163a1917f14Szrj 	    ctlr->reset = ata_sis_reset;
164a1917f14Szrj 
165a1917f14Szrj 	    /* enable PCI interrupt */
166a1917f14Szrj 	    pci_write_config(dev, PCIR_COMMAND,
167a1917f14Szrj 			     pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
168a1917f14Szrj 	}
169a1917f14Szrj 	ctlr->setmode = ata_sata_setmode;
170a1917f14Szrj 	return 0;
171a1917f14Szrj     default:
172a1917f14Szrj 	ata_teardown_interrupt(dev);
173a1917f14Szrj 	return ENXIO;
174a1917f14Szrj     }
175a1917f14Szrj     ctlr->setmode = ata_sis_setmode;
176a1917f14Szrj     return 0;
177a1917f14Szrj }
178a1917f14Szrj 
179a1917f14Szrj static int
ata_sis_allocate(device_t dev)180a1917f14Szrj ata_sis_allocate(device_t dev)
181a1917f14Szrj {
182a1917f14Szrj     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
183a1917f14Szrj     struct ata_channel *ch = device_get_softc(dev);
184a1917f14Szrj     int offset = ch->unit << ((ctlr->chip->chipid == ATA_SIS182) ? 5 : 6);
185a1917f14Szrj 
186a1917f14Szrj     /* setup the usual register normal pci style */
187a1917f14Szrj     if (ata_pci_allocate(dev))
188a1917f14Szrj 	return ENXIO;
189a1917f14Szrj 
190a1917f14Szrj     ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
191a1917f14Szrj     ch->r_io[ATA_SSTATUS].offset = 0x00 + offset;
192a1917f14Szrj     ch->r_io[ATA_SERROR].res = ctlr->r_res2;
193a1917f14Szrj     ch->r_io[ATA_SERROR].offset = 0x04 + offset;
194a1917f14Szrj     ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
195a1917f14Szrj     ch->r_io[ATA_SCONTROL].offset = 0x08 + offset;
196a1917f14Szrj     ch->flags |= ATA_NO_SLAVE;
197a1917f14Szrj 
198a1917f14Szrj     /* XXX SOS PHY hotplug handling missing in SiS chip ?? */
199a1917f14Szrj     /* XXX SOS unknown how to enable PHY state change interrupt */
200a1917f14Szrj     return 0;
201a1917f14Szrj }
202a1917f14Szrj 
203a1917f14Szrj static void
ata_sis_reset(device_t dev)204a1917f14Szrj ata_sis_reset(device_t dev)
205a1917f14Szrj {
206a1917f14Szrj     if (ata_sata_phy_reset(dev))
207a1917f14Szrj 	ata_generic_reset(dev);
208a1917f14Szrj }
209a1917f14Szrj 
210a1917f14Szrj static void
ata_sis_setmode(device_t dev,int mode)211a1917f14Szrj ata_sis_setmode(device_t dev, int mode)
212a1917f14Szrj {
213a1917f14Szrj     device_t gparent = GRANDPARENT(dev);
214a1917f14Szrj     struct ata_pci_controller *ctlr = device_get_softc(gparent);
215a1917f14Szrj     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
216a1917f14Szrj     struct ata_device *atadev = device_get_softc(dev);
217*2458a87aSzrj     int devno = (ch->unit << 1) + atadev->unit;
218a1917f14Szrj     int error;
219a1917f14Szrj 
220a1917f14Szrj     mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
221a1917f14Szrj 
222853eb30dSzrj     if (ctlr->chip->cfg1 == SIS_133NEW) {
223a1917f14Szrj 	if (mode > ATA_UDMA2 &&
224a1917f14Szrj 	    pci_read_config(gparent, ch->unit ? 0x52 : 0x50,2) & 0x8000) {
225a1917f14Szrj 	    ata_print_cable(dev, "controller");
226a1917f14Szrj 	    mode = ATA_UDMA2;
227a1917f14Szrj 	}
228a1917f14Szrj     }
229a1917f14Szrj     else {
230a1917f14Szrj 	if (mode > ATA_UDMA2 &&
231a1917f14Szrj 	    pci_read_config(gparent, 0x48, 1)&(ch->unit ? 0x20 : 0x10)) {
232a1917f14Szrj 	    ata_print_cable(dev, "controller");
233a1917f14Szrj 	    mode = ATA_UDMA2;
234a1917f14Szrj 	}
235a1917f14Szrj     }
236a1917f14Szrj 
237a1917f14Szrj     error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
238a1917f14Szrj 
239a1917f14Szrj     if (bootverbose)
240a1917f14Szrj 	device_printf(dev, "%ssetting %s on %s chip\n",
241a1917f14Szrj 		      (error) ? "FAILURE " : "",
242a1917f14Szrj 		      ata_mode2str(mode), ctlr->chip->text);
243a1917f14Szrj     if (!error) {
244a1917f14Szrj 	switch (ctlr->chip->cfg1) {
245853eb30dSzrj 	case SIS_133NEW: {
24659503772Szrj 	    static const uint32_t timings[] =
247a1917f14Szrj 		{ 0x28269008, 0x0c266008, 0x04263008, 0x0c0a3008, 0x05093008,
248a1917f14Szrj 		  0x22196008, 0x0c0a3008, 0x05093008, 0x050939fc, 0x050936ac,
249a1917f14Szrj 		  0x0509347c, 0x0509325c, 0x0509323c, 0x0509322c, 0x0509321c};
250a1917f14Szrj 	    u_int32_t reg;
251a1917f14Szrj 
252a1917f14Szrj 	    reg = (pci_read_config(gparent, 0x57, 1)&0x40?0x70:0x40)+(devno<<2);
253a1917f14Szrj 	    pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 4);
254a1917f14Szrj 	    break;
255a1917f14Szrj 	    }
256853eb30dSzrj 	case SIS_133OLD: {
25759503772Szrj 	    static const uint16_t timings[] =
258a1917f14Szrj 	     { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033, 0x0031,
259a1917f14Szrj 	       0x8f31, 0x8a31, 0x8731, 0x8531, 0x8331, 0x8231, 0x8131 };
260a1917f14Szrj 
261a1917f14Szrj 	    u_int16_t reg = 0x40 + (devno << 1);
262a1917f14Szrj 
263a1917f14Szrj 	    pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
264a1917f14Szrj 	    break;
265a1917f14Szrj 	    }
266853eb30dSzrj 	case SIS_100NEW: {
26759503772Szrj 	    static const uint16_t timings[] =
268a1917f14Szrj 		{ 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033,
269a1917f14Szrj 		  0x0031, 0x8b31, 0x8731, 0x8531, 0x8431, 0x8231, 0x8131 };
270a1917f14Szrj 	    u_int16_t reg = 0x40 + (devno << 1);
271a1917f14Szrj 
272a1917f14Szrj 	    pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
273a1917f14Szrj 	    break;
274a1917f14Szrj 	    }
275853eb30dSzrj 	case SIS_100OLD:
276853eb30dSzrj 	case SIS_66:
277853eb30dSzrj 	case SIS_33: {
27859503772Szrj 	    static const uint16_t timings[] =
279a1917f14Szrj 		{ 0x0c0b, 0x0607, 0x0404, 0x0303, 0x0301, 0x0404, 0x0303,
280a1917f14Szrj 		  0x0301, 0xf301, 0xd301, 0xb301, 0xa301, 0x9301, 0x8301 };
281a1917f14Szrj 	    u_int16_t reg = 0x40 + (devno << 1);
282a1917f14Szrj 
283a1917f14Szrj 	    pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
284a1917f14Szrj 	    break;
285a1917f14Szrj 	    }
286a1917f14Szrj 	}
287a1917f14Szrj 	atadev->mode = mode;
288a1917f14Szrj     }
289a1917f14Szrj }
290