1 /*- 2 * Copyright (c) 1998 - 2008 S�ren Schmidt <sos@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification, immediately at the beginning of the file. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include "opt_ata.h" 31 #include <sys/param.h> 32 #include <sys/module.h> 33 #include <sys/systm.h> 34 #include <sys/kernel.h> 35 #include <sys/ata.h> 36 #include <sys/bus.h> 37 #include <sys/endian.h> 38 #include <sys/malloc.h> 39 #include <sys/lock.h> 40 #include <sys/mutex.h> 41 #include <sys/sema.h> 42 #include <sys/taskqueue.h> 43 #include <vm/uma.h> 44 #include <machine/stdarg.h> 45 #include <machine/resource.h> 46 #include <machine/bus.h> 47 #include <sys/rman.h> 48 #include <dev/pci/pcivar.h> 49 #include <dev/pci/pcireg.h> 50 #include <dev/ata/ata-all.h> 51 #include <dev/ata/ata-pci.h> 52 #include <ata_if.h> 53 54 /* local prototypes */ 55 static int ata_nvidia_chipinit(device_t dev); 56 static int ata_nvidia_ch_attach(device_t dev); 57 static int ata_nvidia_status(device_t dev); 58 static void ata_nvidia_reset(device_t dev); 59 static void ata_nvidia_setmode(device_t dev, int mode); 60 61 /* misc defines */ 62 #define NV4 0x01 63 #define NVQ 0x02 64 #define NVAHCI 0x04 65 66 67 /* 68 * nVidia chipset support functions 69 */ 70 static int 71 ata_nvidia_probe(device_t dev) 72 { 73 struct ata_pci_controller *ctlr = device_get_softc(dev); 74 static struct ata_chip_id ids[] = 75 {{ ATA_NFORCE1, 0, 0, 0, ATA_UDMA5, "nForce" }, 76 { ATA_NFORCE2, 0, 0, 0, ATA_UDMA6, "nForce2" }, 77 { ATA_NFORCE2_PRO, 0, 0, 0, ATA_UDMA6, "nForce2 Pro" }, 78 { ATA_NFORCE2_PRO_S1, 0, 0, 0, ATA_SA150, "nForce2 Pro" }, 79 { ATA_NFORCE3, 0, 0, 0, ATA_UDMA6, "nForce3" }, 80 { ATA_NFORCE3_PRO, 0, 0, 0, ATA_UDMA6, "nForce3 Pro" }, 81 { ATA_NFORCE3_PRO_S1, 0, 0, 0, ATA_SA150, "nForce3 Pro" }, 82 { ATA_NFORCE3_PRO_S2, 0, 0, 0, ATA_SA150, "nForce3 Pro" }, 83 { ATA_NFORCE_MCP04, 0, 0, 0, ATA_UDMA6, "nForce MCP" }, 84 { ATA_NFORCE_MCP04_S1, 0, NV4, 0, ATA_SA150, "nForce MCP" }, 85 { ATA_NFORCE_MCP04_S2, 0, NV4, 0, ATA_SA150, "nForce MCP" }, 86 { ATA_NFORCE_CK804, 0, 0, 0, ATA_UDMA6, "nForce CK804" }, 87 { ATA_NFORCE_CK804_S1, 0, NV4, 0, ATA_SA300, "nForce CK804" }, 88 { ATA_NFORCE_CK804_S2, 0, NV4, 0, ATA_SA300, "nForce CK804" }, 89 { ATA_NFORCE_MCP51, 0, 0, 0, ATA_UDMA6, "nForce MCP51" }, 90 { ATA_NFORCE_MCP51_S1, 0, NV4|NVQ, 0, ATA_SA300, "nForce MCP51" }, 91 { ATA_NFORCE_MCP51_S2, 0, NV4|NVQ, 0, ATA_SA300, "nForce MCP51" }, 92 { ATA_NFORCE_MCP55, 0, 0, 0, ATA_UDMA6, "nForce MCP55" }, 93 { ATA_NFORCE_MCP55_S1, 0, NV4|NVQ, 0, ATA_SA300, "nForce MCP55" }, 94 { ATA_NFORCE_MCP55_S2, 0, NV4|NVQ, 0, ATA_SA300, "nForce MCP55" }, 95 { ATA_NFORCE_MCP61, 0, 0, 0, ATA_UDMA6, "nForce MCP61" }, 96 { ATA_NFORCE_MCP61_S1, 0, NV4|NVQ, 0, ATA_SA300, "nForce MCP61" }, 97 { ATA_NFORCE_MCP61_S2, 0, NV4|NVQ, 0, ATA_SA300, "nForce MCP61" }, 98 { ATA_NFORCE_MCP61_S3, 0, NV4|NVQ, 0, ATA_SA300, "nForce MCP61" }, 99 { ATA_NFORCE_MCP65, 0, 0, 0, ATA_UDMA6, "nForce MCP65" }, 100 { ATA_NFORCE_MCP67, 0, 0, 0, ATA_UDMA6, "nForce MCP67" }, 101 { ATA_NFORCE_MCP67_A0, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" }, 102 { ATA_NFORCE_MCP67_A1, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" }, 103 { ATA_NFORCE_MCP67_A2, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" }, 104 { ATA_NFORCE_MCP67_A3, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" }, 105 { ATA_NFORCE_MCP67_A4, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" }, 106 { ATA_NFORCE_MCP67_A5, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" }, 107 { ATA_NFORCE_MCP67_A6, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" }, 108 { ATA_NFORCE_MCP67_A7, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" }, 109 { ATA_NFORCE_MCP67_A8, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" }, 110 { ATA_NFORCE_MCP67_A9, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" }, 111 { ATA_NFORCE_MCP67_AA, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" }, 112 { ATA_NFORCE_MCP67_AB, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" }, 113 { ATA_NFORCE_MCP73, 0, 0, 0, ATA_UDMA6, "nForce MCP73" }, 114 { ATA_NFORCE_MCP73_A0, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" }, 115 { ATA_NFORCE_MCP73_A1, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" }, 116 { ATA_NFORCE_MCP73_A2, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" }, 117 { ATA_NFORCE_MCP73_A3, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" }, 118 { ATA_NFORCE_MCP73_A4, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" }, 119 { ATA_NFORCE_MCP73_A5, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" }, 120 { ATA_NFORCE_MCP73_A6, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" }, 121 { ATA_NFORCE_MCP73_A7, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" }, 122 { ATA_NFORCE_MCP73_A8, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" }, 123 { ATA_NFORCE_MCP73_A9, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" }, 124 { ATA_NFORCE_MCP73_AA, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" }, 125 { ATA_NFORCE_MCP73_AB, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" }, 126 { ATA_NFORCE_MCP77, 0, 0, 0, ATA_UDMA6, "nForce MCP77" }, 127 { 0, 0, 0, 0, 0, 0}} ; 128 129 if (pci_get_vendor(dev) != ATA_NVIDIA_ID) 130 return ENXIO; 131 132 if (!(ctlr->chip = ata_match_chip(dev, ids))) 133 return ENXIO; 134 135 ata_set_desc(dev); 136 if (ctlr->chip->cfg1 & NVAHCI) 137 ctlr->chipinit = ata_ahci_chipinit; 138 else 139 ctlr->chipinit = ata_nvidia_chipinit; 140 return 0; 141 } 142 143 static int 144 ata_nvidia_chipinit(device_t dev) 145 { 146 struct ata_pci_controller *ctlr = device_get_softc(dev); 147 148 if (ata_setup_interrupt(dev, ata_generic_intr)) 149 return ENXIO; 150 151 if (ctlr->chip->max_dma >= ATA_SA150) { 152 if (pci_read_config(dev, PCIR_BAR(5), 1) & 1) 153 ctlr->r_type2 = SYS_RES_IOPORT; 154 else 155 ctlr->r_type2 = SYS_RES_MEMORY; 156 ctlr->r_rid2 = PCIR_BAR(5); 157 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2, 158 &ctlr->r_rid2, RF_ACTIVE))) { 159 int offset = ctlr->chip->cfg1 & NV4 ? 0x0440 : 0x0010; 160 161 ctlr->ch_attach = ata_nvidia_ch_attach; 162 ctlr->ch_detach = ata_pci_ch_detach; 163 ctlr->reset = ata_nvidia_reset; 164 165 /* enable control access */ 166 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) | 0x04,1); 167 168 if (ctlr->chip->cfg1 & NVQ) { 169 /* clear interrupt status */ 170 ATA_OUTL(ctlr->r_res2, offset, 0x00ff00ff); 171 172 /* enable device and PHY state change interrupts */ 173 ATA_OUTL(ctlr->r_res2, offset + 4, 0x000d000d); 174 175 /* disable NCQ support */ 176 ATA_OUTL(ctlr->r_res2, 0x0400, 177 ATA_INL(ctlr->r_res2, 0x0400) & 0xfffffff9); 178 } 179 else { 180 /* clear interrupt status */ 181 ATA_OUTB(ctlr->r_res2, offset, 0xff); 182 183 /* enable device and PHY state change interrupts */ 184 ATA_OUTB(ctlr->r_res2, offset + 1, 0xdd); 185 } 186 187 /* enable PCI interrupt */ 188 pci_write_config(dev, PCIR_COMMAND, 189 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2); 190 191 } 192 ctlr->setmode = ata_sata_setmode; 193 } 194 else { 195 /* disable prefetch, postwrite */ 196 pci_write_config(dev, 0x51, pci_read_config(dev, 0x51, 1) & 0x0f, 1); 197 ctlr->setmode = ata_nvidia_setmode; 198 } 199 return 0; 200 } 201 202 static int 203 ata_nvidia_ch_attach(device_t dev) 204 { 205 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 206 struct ata_channel *ch = device_get_softc(dev); 207 208 /* setup the usual register normal pci style */ 209 if (ata_pci_ch_attach(dev)) 210 return ENXIO; 211 212 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2; 213 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 6); 214 ch->r_io[ATA_SERROR].res = ctlr->r_res2; 215 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 6); 216 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2; 217 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 6); 218 219 ch->hw.status = ata_nvidia_status; 220 ch->flags |= ATA_NO_SLAVE; 221 222 return 0; 223 } 224 225 static int 226 ata_nvidia_status(device_t dev) 227 { 228 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 229 struct ata_channel *ch = device_get_softc(dev); 230 int offset = ctlr->chip->cfg1 & NV4 ? 0x0440 : 0x0010; 231 int shift = ch->unit << (ctlr->chip->cfg1 & NVQ ? 4 : 2); 232 u_int32_t istatus; 233 234 /* get interrupt status */ 235 if (ctlr->chip->cfg1 & NVQ) 236 istatus = ATA_INL(ctlr->r_res2, offset); 237 else 238 istatus = ATA_INB(ctlr->r_res2, offset); 239 240 /* do we have any PHY events ? */ 241 if (istatus & (0x0c << shift)) 242 ata_sata_phy_check_events(dev); 243 244 /* clear interrupt(s) */ 245 if (ctlr->chip->cfg1 & NVQ) 246 ATA_OUTL(ctlr->r_res2, offset, (0x0f << shift) | 0x00f000f0); 247 else 248 ATA_OUTB(ctlr->r_res2, offset, (0x0f << shift)); 249 250 /* do we have any device action ? */ 251 return (istatus & (0x01 << shift)); 252 } 253 254 static void 255 ata_nvidia_reset(device_t dev) 256 { 257 if (ata_sata_phy_reset(dev)) 258 ata_generic_reset(dev); 259 } 260 261 static void 262 ata_nvidia_setmode(device_t dev, int mode) 263 { 264 device_t gparent = GRANDPARENT(dev); 265 struct ata_pci_controller *ctlr = device_get_softc(gparent); 266 struct ata_channel *ch = device_get_softc(device_get_parent(dev)); 267 struct ata_device *atadev = device_get_softc(dev); 268 u_int8_t timings[] = { 0xa8, 0x65, 0x42, 0x22, 0x20, 0x42, 0x22, 0x20, 269 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }; 270 int modes[7] = { 0xc2, 0xc1, 0xc0, 0xc4, 0xc5, 0xc6, 0xc7 }; 271 int devno = (ch->unit << 1) + atadev->unit; 272 int reg = 0x63 - devno; 273 int error; 274 275 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma); 276 mode = ata_check_80pin(dev, mode); 277 278 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode); 279 if (bootverbose) 280 device_printf(dev, "%ssetting %s on %s chip\n", 281 (error) ? "FAILURE " : "", ata_mode2str(mode), 282 ctlr->chip->text); 283 if (!error) { 284 pci_write_config(gparent, reg - 0x08, timings[ata_mode2idx(mode)], 1); 285 if (mode >= ATA_UDMA0) 286 pci_write_config(gparent, reg, modes[mode & ATA_MODE_MASK], 1); 287 else 288 pci_write_config(gparent, reg, 0x8b, 1); 289 atadev->mode = mode; 290 } 291 } 292 293 ATA_DECLARE_DRIVER(ata_nvidia); 294