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_acard_chipinit(device_t dev); 56 static int ata_acard_ch_attach(device_t dev); 57 static int ata_acard_status(device_t dev); 58 static void ata_acard_850_setmode(device_t dev, int mode); 59 static void ata_acard_86X_setmode(device_t dev, int mode); 60 static int ata_serialize(device_t dev, int flags); 61 62 /* misc defines */ 63 #define ATP_OLD 1 64 65 66 /* 67 * Acard chipset support functions 68 */ 69 static int 70 ata_acard_probe(device_t dev) 71 { 72 struct ata_pci_controller *ctlr = device_get_softc(dev); 73 static struct ata_chip_id ids[] = 74 {{ ATA_ATP850R, 0, ATP_OLD, 0x00, ATA_UDMA2, "ATP850" }, 75 { ATA_ATP860A, 0, 0, 0x00, ATA_UDMA4, "ATP860A" }, 76 { ATA_ATP860R, 0, 0, 0x00, ATA_UDMA4, "ATP860R" }, 77 { ATA_ATP865A, 0, 0, 0x00, ATA_UDMA6, "ATP865A" }, 78 { ATA_ATP865R, 0, 0, 0x00, ATA_UDMA6, "ATP865R" }, 79 { 0, 0, 0, 0, 0, 0}}; 80 81 if (pci_get_vendor(dev) != ATA_ACARD_ID) 82 return ENXIO; 83 84 if (!(ctlr->chip = ata_match_chip(dev, ids))) 85 return ENXIO; 86 87 ata_set_desc(dev); 88 ctlr->chipinit = ata_acard_chipinit; 89 return 0; 90 } 91 92 static int 93 ata_acard_chipinit(device_t dev) 94 { 95 struct ata_pci_controller *ctlr = device_get_softc(dev); 96 97 if (ata_setup_interrupt(dev, ata_generic_intr)) 98 return ENXIO; 99 100 ctlr->ch_attach = ata_acard_ch_attach; 101 ctlr->ch_detach = ata_pci_ch_detach; 102 if (ctlr->chip->cfg1 == ATP_OLD) { 103 ctlr->setmode = ata_acard_850_setmode; 104 ctlr->locking = ata_serialize; 105 } 106 else 107 ctlr->setmode = ata_acard_86X_setmode; 108 return 0; 109 } 110 111 static int 112 ata_acard_ch_attach(device_t dev) 113 { 114 struct ata_channel *ch = device_get_softc(dev); 115 116 /* setup the usual register normal pci style */ 117 if (ata_pci_ch_attach(dev)) 118 return ENXIO; 119 120 ch->hw.status = ata_acard_status; 121 return 0; 122 } 123 124 static int 125 ata_acard_status(device_t dev) 126 { 127 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 128 struct ata_channel *ch = device_get_softc(dev); 129 130 if (ctlr->chip->cfg1 == ATP_OLD && 131 ATA_LOCKING(dev, ATA_LF_WHICH) != ch->unit) 132 return 0; 133 if (ch->dma.flags & ATA_DMA_ACTIVE) { 134 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK; 135 136 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) != 137 ATA_BMSTAT_INTERRUPT) 138 return 0; 139 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR); 140 DELAY(1); 141 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT, 142 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP); 143 DELAY(1); 144 } 145 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) { 146 DELAY(100); 147 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) 148 return 0; 149 } 150 return 1; 151 } 152 153 static void 154 ata_acard_850_setmode(device_t dev, int mode) 155 { 156 device_t gparent = GRANDPARENT(dev); 157 struct ata_pci_controller *ctlr = device_get_softc(gparent); 158 struct ata_channel *ch = device_get_softc(device_get_parent(dev)); 159 struct ata_device *atadev = device_get_softc(dev); 160 int devno = (ch->unit << 1) + atadev->unit; 161 int error; 162 163 mode = ata_limit_mode(dev, mode, 164 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma); 165 166 /* XXX SOS missing WDMA0+1 + PIO modes */ 167 if (mode >= ATA_WDMA2) { 168 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode); 169 if (bootverbose) 170 device_printf(dev, "%ssetting %s on %s chip\n", 171 (error) ? "FAILURE " : "", 172 ata_mode2str(mode), ctlr->chip->text); 173 if (!error) { 174 u_int8_t reg54 = pci_read_config(gparent, 0x54, 1); 175 176 reg54 &= ~(0x03 << (devno << 1)); 177 if (mode >= ATA_UDMA0) 178 reg54 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 1)); 179 pci_write_config(gparent, 0x54, reg54, 1); 180 pci_write_config(gparent, 0x4a, 0xa6, 1); 181 pci_write_config(gparent, 0x40 + (devno << 1), 0x0301, 2); 182 atadev->mode = mode; 183 return; 184 } 185 } 186 /* we could set PIO mode timings, but we assume the BIOS did that */ 187 } 188 189 static void 190 ata_acard_86X_setmode(device_t dev, int mode) 191 { 192 device_t gparent = GRANDPARENT(dev); 193 struct ata_pci_controller *ctlr = device_get_softc(gparent); 194 struct ata_channel *ch = device_get_softc(device_get_parent(dev)); 195 struct ata_device *atadev = device_get_softc(dev); 196 int devno = (ch->unit << 1) + atadev->unit; 197 int error; 198 199 200 mode = ata_limit_mode(dev, mode, 201 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma); 202 203 mode = ata_check_80pin(dev, mode); 204 205 /* XXX SOS missing WDMA0+1 + PIO modes */ 206 if (mode >= ATA_WDMA2) { 207 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode); 208 if (bootverbose) 209 device_printf(dev, "%ssetting %s on %s chip\n", 210 (error) ? "FAILURE " : "", 211 ata_mode2str(mode), ctlr->chip->text); 212 if (!error) { 213 u_int16_t reg44 = pci_read_config(gparent, 0x44, 2); 214 215 reg44 &= ~(0x000f << (devno << 2)); 216 if (mode >= ATA_UDMA0) 217 reg44 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 2)); 218 pci_write_config(gparent, 0x44, reg44, 2); 219 pci_write_config(gparent, 0x4a, 0xa6, 1); 220 pci_write_config(gparent, 0x40 + devno, 0x31, 1); 221 atadev->mode = mode; 222 return; 223 } 224 } 225 /* we could set PIO mode timings, but we assume the BIOS did that */ 226 } 227 228 struct ata_serialize { 229 struct mtx locked_mtx; 230 int locked_ch; 231 int restart_ch; 232 }; 233 234 static int 235 ata_serialize(device_t dev, int flags) 236 { 237 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 238 struct ata_channel *ch = device_get_softc(dev); 239 struct ata_serialize *serial; 240 static int inited = 0; 241 int res; 242 243 if (!inited) { 244 serial = malloc(sizeof(struct ata_serialize), 245 M_TEMP, M_NOWAIT | M_ZERO); 246 mtx_init(&serial->locked_mtx, "ATA serialize lock", NULL, MTX_DEF); 247 serial->locked_ch = -1; 248 serial->restart_ch = -1; 249 device_set_ivars(ctlr->dev, serial); 250 inited = 1; 251 } 252 else 253 serial = device_get_ivars(ctlr->dev); 254 255 mtx_lock(&serial->locked_mtx); 256 switch (flags) { 257 case ATA_LF_LOCK: 258 if (serial->locked_ch == -1) 259 serial->locked_ch = ch->unit; 260 if (serial->locked_ch != ch->unit) 261 serial->restart_ch = ch->unit; 262 break; 263 264 case ATA_LF_UNLOCK: 265 if (serial->locked_ch == ch->unit) { 266 serial->locked_ch = -1; 267 if (serial->restart_ch != -1) { 268 if ((ch = ctlr->interrupt[serial->restart_ch].argument)) { 269 serial->restart_ch = -1; 270 mtx_unlock(&serial->locked_mtx); 271 ata_start(dev); 272 return -1; 273 } 274 } 275 } 276 break; 277 278 case ATA_LF_WHICH: 279 break; 280 } 281 res = serial->locked_ch; 282 mtx_unlock(&serial->locked_mtx); 283 return res; 284 } 285 286 ATA_DECLARE_DRIVER(ata_acard); 287