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