1183724Ssos/*- 2230132Suqs * Copyright (c) 1998 - 2008 S��ren Schmidt <sos@FreeBSD.org> 3183724Ssos * All rights reserved. 4183724Ssos * 5183724Ssos * Redistribution and use in source and binary forms, with or without 6183724Ssos * modification, are permitted provided that the following conditions 7183724Ssos * are met: 8183724Ssos * 1. Redistributions of source code must retain the above copyright 9183724Ssos * notice, this list of conditions and the following disclaimer, 10183724Ssos * without modification, immediately at the beginning of the file. 11183724Ssos * 2. Redistributions in binary form must reproduce the above copyright 12183724Ssos * notice, this list of conditions and the following disclaimer in the 13183724Ssos * documentation and/or other materials provided with the distribution. 14183724Ssos * 15183724Ssos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16183724Ssos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17183724Ssos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18183724Ssos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19183724Ssos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20183724Ssos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21183724Ssos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22183724Ssos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23183724Ssos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24183724Ssos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25183724Ssos */ 26183724Ssos 27183724Ssos#include <sys/cdefs.h> 28183724Ssos__FBSDID("$FreeBSD$"); 29183724Ssos 30183724Ssos#include <sys/param.h> 31183724Ssos#include <sys/module.h> 32183724Ssos#include <sys/systm.h> 33183724Ssos#include <sys/kernel.h> 34183724Ssos#include <sys/ata.h> 35183724Ssos#include <sys/bus.h> 36183724Ssos#include <sys/endian.h> 37183724Ssos#include <sys/malloc.h> 38183724Ssos#include <sys/lock.h> 39183724Ssos#include <sys/mutex.h> 40183724Ssos#include <sys/sema.h> 41183724Ssos#include <sys/taskqueue.h> 42183724Ssos#include <vm/uma.h> 43183724Ssos#include <machine/stdarg.h> 44183724Ssos#include <machine/resource.h> 45183724Ssos#include <machine/bus.h> 46183724Ssos#include <sys/rman.h> 47183724Ssos#include <dev/pci/pcivar.h> 48183724Ssos#include <dev/pci/pcireg.h> 49183724Ssos#include <dev/ata/ata-all.h> 50183724Ssos#include <dev/ata/ata-pci.h> 51183724Ssos#include <ata_if.h> 52183724Ssos 53183724Ssos/* local prototypes */ 54183724Ssosstatic int ata_sis_chipinit(device_t dev); 55188765Smavstatic int ata_sis_ch_attach(device_t dev); 56183724Ssosstatic void ata_sis_reset(device_t dev); 57200171Smavstatic int ata_sis_setmode(device_t dev, int target, int mode); 58183724Ssos 59183724Ssos/* misc defines */ 60183724Ssos#define SIS_33 1 61183724Ssos#define SIS_66 2 62183724Ssos#define SIS_100NEW 3 63183724Ssos#define SIS_100OLD 4 64183724Ssos#define SIS_133NEW 5 65183724Ssos#define SIS_133OLD 6 66183724Ssos#define SIS_SATA 7 67183724Ssos 68183724Ssos/* 69183724Ssos * Silicon Integrated Systems Corp. (SiS) chipset support functions 70183724Ssos */ 71183724Ssosstatic int 72183724Ssosata_sis_probe(device_t dev) 73183724Ssos{ 74183724Ssos struct ata_pci_controller *ctlr = device_get_softc(dev); 75233282Smarius const struct ata_chip_id *idx; 76242625Sdim static const struct ata_chip_id ids[] = 77183724Ssos {{ ATA_SIS182, 0x00, SIS_SATA, 0, ATA_SA150, "182" }, /* south */ 78183724Ssos { ATA_SIS181, 0x00, SIS_SATA, 0, ATA_SA150, "181" }, /* south */ 79183724Ssos { ATA_SIS180, 0x00, SIS_SATA, 0, ATA_SA150, "180" }, /* south */ 80183724Ssos { ATA_SIS965, 0x00, SIS_133NEW, 0, ATA_UDMA6, "965" }, /* south */ 81183724Ssos { ATA_SIS964, 0x00, SIS_133NEW, 0, ATA_UDMA6, "964" }, /* south */ 82183724Ssos { ATA_SIS963, 0x00, SIS_133NEW, 0, ATA_UDMA6, "963" }, /* south */ 83183724Ssos { ATA_SIS962, 0x00, SIS_133NEW, 0, ATA_UDMA6, "962" }, /* south */ 84183724Ssos 85183724Ssos { ATA_SIS745, 0x00, SIS_100NEW, 0, ATA_UDMA5, "745" }, /* 1chip */ 86183724Ssos { ATA_SIS735, 0x00, SIS_100NEW, 0, ATA_UDMA5, "735" }, /* 1chip */ 87183724Ssos { ATA_SIS733, 0x00, SIS_100NEW, 0, ATA_UDMA5, "733" }, /* 1chip */ 88183724Ssos { ATA_SIS730, 0x00, SIS_100OLD, 0, ATA_UDMA5, "730" }, /* 1chip */ 89183724Ssos 90183724Ssos { ATA_SIS635, 0x00, SIS_100NEW, 0, ATA_UDMA5, "635" }, /* 1chip */ 91183724Ssos { ATA_SIS633, 0x00, SIS_100NEW, 0, ATA_UDMA5, "633" }, /* unknown */ 92183724Ssos { ATA_SIS630, 0x30, SIS_100OLD, 0, ATA_UDMA5, "630S"}, /* 1chip */ 93183724Ssos { ATA_SIS630, 0x00, SIS_66, 0, ATA_UDMA4, "630" }, /* 1chip */ 94183724Ssos { ATA_SIS620, 0x00, SIS_66, 0, ATA_UDMA4, "620" }, /* 1chip */ 95183724Ssos 96183724Ssos { ATA_SIS550, 0x00, SIS_66, 0, ATA_UDMA5, "550" }, 97183724Ssos { ATA_SIS540, 0x00, SIS_66, 0, ATA_UDMA4, "540" }, 98183724Ssos { ATA_SIS530, 0x00, SIS_66, 0, ATA_UDMA4, "530" }, 99183724Ssos 100183724Ssos { ATA_SIS5513, 0xc2, SIS_33, 1, ATA_UDMA2, "5513" }, 101183724Ssos { ATA_SIS5513, 0x00, SIS_33, 1, ATA_WDMA2, "5513" }, 102183724Ssos { 0, 0, 0, 0, 0, 0 }}; 103233282Smarius static struct ata_chip_id id[] = 104233282Smarius {{ ATA_SISSOUTH, 0x10, 0, 0, 0, "" }, { 0, 0, 0, 0, 0, 0 }}; 105183724Ssos char buffer[64]; 106183724Ssos int found = 0; 107183724Ssos 108186296Smav if (pci_get_class(dev) != PCIC_STORAGE) 109186296Smav return (ENXIO); 110186296Smav 111183724Ssos if (pci_get_vendor(dev) != ATA_SIS_ID) 112183724Ssos return ENXIO; 113183724Ssos 114183724Ssos if (!(idx = ata_find_chip(dev, ids, -pci_get_slot(dev)))) 115183724Ssos return ENXIO; 116183724Ssos 117233282Smarius if (idx->cfg2) { 118183724Ssos u_int8_t reg57 = pci_read_config(dev, 0x57, 1); 119183724Ssos 120183724Ssos pci_write_config(dev, 0x57, (reg57 & 0x7f), 1); 121183724Ssos if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5518) { 122183724Ssos found = 1; 123233282Smarius memcpy(&id[0], idx, sizeof(id[0])); 124233282Smarius id[0].cfg1 = SIS_133NEW; 125233282Smarius id[0].max_dma = ATA_UDMA6; 126183724Ssos sprintf(buffer, "SiS 962/963 %s controller", 127183724Ssos ata_mode2str(idx->max_dma)); 128183724Ssos } 129183724Ssos pci_write_config(dev, 0x57, reg57, 1); 130183724Ssos } 131183724Ssos if (idx->cfg2 && !found) { 132183724Ssos u_int8_t reg4a = pci_read_config(dev, 0x4a, 1); 133183724Ssos 134183724Ssos pci_write_config(dev, 0x4a, (reg4a | 0x10), 1); 135183724Ssos if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5517) { 136183724Ssos found = 1; 137183724Ssos if (ata_find_chip(dev, id, pci_get_slot(dev))) { 138233282Smarius id[0].cfg1 = SIS_133OLD; 139233282Smarius id[0].max_dma = ATA_UDMA6; 140233282Smarius } else { 141233282Smarius id[0].cfg1 = SIS_100NEW; 142233282Smarius id[0].max_dma = ATA_UDMA5; 143183724Ssos } 144183724Ssos sprintf(buffer, "SiS 961 %s controller",ata_mode2str(idx->max_dma)); 145183724Ssos } 146183724Ssos pci_write_config(dev, 0x4a, reg4a, 1); 147183724Ssos } 148183724Ssos if (!found) 149183724Ssos sprintf(buffer,"SiS %s %s controller", 150183724Ssos idx->text, ata_mode2str(idx->max_dma)); 151233282Smarius else 152233282Smarius idx = &id[0]; 153183724Ssos 154183724Ssos device_set_desc_copy(dev, buffer); 155183724Ssos ctlr->chip = idx; 156183724Ssos ctlr->chipinit = ata_sis_chipinit; 157194893Smav return (BUS_PROBE_DEFAULT); 158183724Ssos} 159183724Ssos 160183724Ssosstatic int 161183724Ssosata_sis_chipinit(device_t dev) 162183724Ssos{ 163183724Ssos struct ata_pci_controller *ctlr = device_get_softc(dev); 164183724Ssos 165183724Ssos if (ata_setup_interrupt(dev, ata_generic_intr)) 166183724Ssos return ENXIO; 167183724Ssos 168183724Ssos switch (ctlr->chip->cfg1) { 169183724Ssos case SIS_33: 170183724Ssos break; 171183724Ssos case SIS_66: 172183724Ssos case SIS_100OLD: 173183724Ssos pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 1) & ~0x04, 1); 174183724Ssos break; 175183724Ssos case SIS_100NEW: 176183724Ssos case SIS_133OLD: 177183724Ssos pci_write_config(dev, 0x49, pci_read_config(dev, 0x49, 1) & ~0x01, 1); 178183724Ssos break; 179183724Ssos case SIS_133NEW: 180183724Ssos pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 2) | 0x0008, 2); 181183724Ssos pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 2) | 0x0008, 2); 182183724Ssos break; 183183724Ssos case SIS_SATA: 184183724Ssos ctlr->r_type2 = SYS_RES_IOPORT; 185183724Ssos ctlr->r_rid2 = PCIR_BAR(5); 186183724Ssos if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2, 187183724Ssos &ctlr->r_rid2, RF_ACTIVE))) { 188188765Smav ctlr->ch_attach = ata_sis_ch_attach; 189188769Smav ctlr->ch_detach = ata_pci_ch_detach; 190183724Ssos ctlr->reset = ata_sis_reset; 191183724Ssos } 192183724Ssos ctlr->setmode = ata_sata_setmode; 193200171Smav ctlr->getrev = ata_sata_getrev; 194183724Ssos return 0; 195183724Ssos default: 196183724Ssos return ENXIO; 197183724Ssos } 198183724Ssos ctlr->setmode = ata_sis_setmode; 199183724Ssos return 0; 200183724Ssos} 201183724Ssos 202183724Ssosstatic int 203188765Smavata_sis_ch_attach(device_t dev) 204183724Ssos{ 205183724Ssos struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 206183724Ssos struct ata_channel *ch = device_get_softc(dev); 207183724Ssos int offset = ch->unit << ((ctlr->chip->chipid == ATA_SIS182) ? 5 : 6); 208183724Ssos 209183724Ssos /* setup the usual register normal pci style */ 210188765Smav if (ata_pci_ch_attach(dev)) 211183724Ssos return ENXIO; 212183724Ssos 213183724Ssos ch->r_io[ATA_SSTATUS].res = ctlr->r_res2; 214183724Ssos ch->r_io[ATA_SSTATUS].offset = 0x00 + offset; 215183724Ssos ch->r_io[ATA_SERROR].res = ctlr->r_res2; 216183724Ssos ch->r_io[ATA_SERROR].offset = 0x04 + offset; 217183724Ssos ch->r_io[ATA_SCONTROL].res = ctlr->r_res2; 218183724Ssos ch->r_io[ATA_SCONTROL].offset = 0x08 + offset; 219183724Ssos ch->flags |= ATA_NO_SLAVE; 220200171Smav ch->flags |= ATA_SATA; 221183724Ssos 222183724Ssos /* XXX SOS PHY hotplug handling missing in SiS chip ?? */ 223183724Ssos /* XXX SOS unknown how to enable PHY state change interrupt */ 224183724Ssos return 0; 225183724Ssos} 226183724Ssos 227183724Ssosstatic void 228183724Ssosata_sis_reset(device_t dev) 229183724Ssos{ 230209884Smav struct ata_channel *ch = device_get_softc(dev); 231209884Smav 232190581Smav if (ata_sata_phy_reset(dev, -1, 1)) 233183724Ssos ata_generic_reset(dev); 234209884Smav else 235209884Smav ch->devices = 0; 236183724Ssos} 237183724Ssos 238200171Smavstatic int 239200171Smavata_sis_setmode(device_t dev, int target, int mode) 240183724Ssos{ 241200171Smav device_t parent = device_get_parent(dev); 242200171Smav struct ata_pci_controller *ctlr = device_get_softc(parent); 243200171Smav struct ata_channel *ch = device_get_softc(dev); 244200171Smav int devno = (ch->unit << 1) + target; 245183724Ssos 246200171Smav mode = min(mode, ctlr->chip->max_dma); 247183724Ssos 248200171Smav if (ctlr->chip->cfg1 == SIS_133NEW) { 249209872Smav if (ata_dma_check_80pin && mode > ATA_UDMA2 && 250200171Smav pci_read_config(parent, ch->unit ? 0x52 : 0x50,2) & 0x8000) { 251200171Smav ata_print_cable(dev, "controller"); 252200171Smav mode = ATA_UDMA2; 253200171Smav } 254200171Smav } else { 255209872Smav if (ata_dma_check_80pin && mode > ATA_UDMA2 && 256200171Smav pci_read_config(parent, 0x48, 1)&(ch->unit ? 0x20 : 0x10)) { 257200171Smav ata_print_cable(dev, "controller"); 258200171Smav mode = ATA_UDMA2; 259200171Smav } 260200171Smav } 261183724Ssos 262183724Ssos switch (ctlr->chip->cfg1) { 263183724Ssos case SIS_133NEW: { 264233282Smarius static const uint32_t timings[] = 265183724Ssos { 0x28269008, 0x0c266008, 0x04263008, 0x0c0a3008, 0x05093008, 266183724Ssos 0x22196008, 0x0c0a3008, 0x05093008, 0x050939fc, 0x050936ac, 267183724Ssos 0x0509347c, 0x0509325c, 0x0509323c, 0x0509322c, 0x0509321c}; 268183724Ssos u_int32_t reg; 269183724Ssos 270200171Smav reg = (pci_read_config(parent, 0x57, 1)&0x40?0x70:0x40)+(devno<<2); 271200171Smav pci_write_config(parent, reg, timings[ata_mode2idx(mode)], 4); 272183724Ssos break; 273183724Ssos } 274183724Ssos case SIS_133OLD: { 275233282Smarius static const uint16_t timings[] = 276183724Ssos { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033, 0x0031, 277183724Ssos 0x8f31, 0x8a31, 0x8731, 0x8531, 0x8331, 0x8231, 0x8131 }; 278183724Ssos 279183724Ssos u_int16_t reg = 0x40 + (devno << 1); 280183724Ssos 281200171Smav pci_write_config(parent, reg, timings[ata_mode2idx(mode)], 2); 282183724Ssos break; 283183724Ssos } 284183724Ssos case SIS_100NEW: { 285233282Smarius static const uint16_t timings[] = 286183724Ssos { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033, 287183724Ssos 0x0031, 0x8b31, 0x8731, 0x8531, 0x8431, 0x8231, 0x8131 }; 288183724Ssos u_int16_t reg = 0x40 + (devno << 1); 289183724Ssos 290200171Smav pci_write_config(parent, reg, timings[ata_mode2idx(mode)], 2); 291183724Ssos break; 292183724Ssos } 293183724Ssos case SIS_100OLD: 294183724Ssos case SIS_66: 295183724Ssos case SIS_33: { 296233282Smarius static const uint16_t timings[] = 297183724Ssos { 0x0c0b, 0x0607, 0x0404, 0x0303, 0x0301, 0x0404, 0x0303, 298183724Ssos 0x0301, 0xf301, 0xd301, 0xb301, 0xa301, 0x9301, 0x8301 }; 299183724Ssos u_int16_t reg = 0x40 + (devno << 1); 300183724Ssos 301200171Smav pci_write_config(parent, reg, timings[ata_mode2idx(mode)], 2); 302183724Ssos break; 303183724Ssos } 304183724Ssos } 305200171Smav return (mode); 306183724Ssos} 307183724Ssos 308183724SsosATA_DECLARE_DRIVER(ata_sis); 309