ata-dma.c revision 51548
1/*- 2 * Copyright (c) 1998,1999 S�ren Schmidt 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 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $FreeBSD: head/sys/dev/ata/ata-dma.c 51548 1999-09-22 12:04:51Z sos $ 29 */ 30 31#include "pci.h" 32#include "apm.h" 33#include <sys/param.h> 34#include <sys/systm.h> 35#include <sys/kernel.h> 36#include <sys/buf.h> 37#include <sys/malloc.h> 38#include <sys/bus.h> 39#include <vm/vm.h> 40#include <vm/pmap.h> 41#if NPCI > 0 42#include <pci/pcivar.h> 43#include <pci/pcireg.h> 44#endif 45#if NAPM > 0 46#include <machine/apm_bios.h> 47#endif 48#include <dev/ata/ata-all.h> 49 50#ifdef __alpha__ 51#undef vtophys 52#define vtophys(va) alpha_XXX_dmamap((vm_offset_t)va) 53#endif 54 55/* misc defines */ 56#define MIN(a,b) ((a)>(b)?(b):(a)) 57 58#if NPCI > 0 59 60int32_t 61ata_dmainit(struct ata_softc *scp, int32_t device, 62 int32_t apiomode, int32_t wdmamode, int32_t udmamode) 63{ 64 int32_t type, devno, error; 65 void *dmatab; 66 67 if (!scp->bmaddr) 68 return -1; 69#ifdef ATA_DMADEBUG 70 printf("ata%d: dmainit: ioaddr=0x%x altioaddr=0x%x, bmaddr=0x%x\n", 71 scp->lun, scp->ioaddr, scp->altioaddr, scp->bmaddr); 72#endif 73 74 if (!(dmatab = malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT))) 75 return -1; 76 77 if (((uintptr_t)dmatab >> PAGE_SHIFT) ^ 78 (((uintptr_t)dmatab + PAGE_SIZE - 1) >> PAGE_SHIFT)) { 79 printf("ata_dmainit: dmatab crosses page boundary, no DMA\n"); 80 free(dmatab, M_DEVBUF); 81 return -1; 82 } 83 scp->dmatab[(device == ATA_MASTER) ? 0 : 1] = dmatab; 84 85 switch (type = pci_get_devid(scp->dev)) { 86 87 case 0x71118086: /* Intel PIIX4 */ 88 if (udmamode >= 2) { 89 int32_t mask48, new48; 90 91 error = ata_command(scp, device, ATA_C_SETFEATURES, 0, 0, 0, 92 ATA_UDMA2, ATA_C_FEA_SETXFER, ATA_IGNORE_INTR); 93 if (bootverbose) 94 printf("ata%d: %s: %s setting up UDMA2 mode on PIIX4 chip\n", 95 scp->lun, (device == ATA_MASTER) ? "master" : "slave", 96 (error) ? "failed" : "success"); 97 if (error) 98 break; 99 devno = (scp->unit << 1) + ((device == ATA_MASTER) ? 0 : 1); 100 mask48 = (1 << devno) + (3 << (16 + (devno << 2))); 101 new48 = (1 << devno) + (2 << (16 + (devno << 2))); 102 pci_write_config(scp->dev, 0x48, 103 (pci_read_config(scp->dev, 0x48, 4) & 104 ~mask48) | new48, 4); 105 scp->mode[(device == ATA_MASTER) ? 0 : 1] = ATA_MODE_UDMA33; 106 return 0; 107 } 108 /* FALLTHROUGH */ 109 110 case 0x70108086: /* Intel PIIX3 */ 111 if (wdmamode >= 2 && apiomode >= 4) { 112 int32_t mask40, new40, mask44, new44; 113 114 /* if SITRE not set doit for both channels */ 115 if (!((pci_read_config(scp->dev, 0x40, 4)>>(scp->unit<<8))&0x4000)){ 116 new40 = pci_read_config(scp->dev, 0x40, 4); 117 new44 = pci_read_config(scp->dev, 0x44, 4); 118 if (!(new40 & 0x00004000)) { 119 new44 &= ~0x0000000f; 120 new44 |= ((new40&0x00003000)>>10)|((new40&0x00000300)>>8); 121 } 122 if (!(new40 & 0x40000000)) { 123 new44 &= ~0x000000f0; 124 new44 |= ((new40&0x30000000)>>22)|((new40&0x03000000)>>20); 125 } 126 new40 |= 0x40004000; 127 pci_write_config(scp->dev, 0x40, new40, 4); 128 pci_write_config(scp->dev, 0x44, new44, 4); 129 } 130 error = ata_command(scp, device, ATA_C_SETFEATURES, 0, 0, 0, 131 ATA_WDMA2, ATA_C_FEA_SETXFER, ATA_IGNORE_INTR); 132 if (bootverbose) 133 printf("ata%d: %s: %s setting up WDMA2 mode on PIIX4 chip\n", 134 scp->lun, (device == ATA_MASTER) ? "master" : "slave", 135 (error) ? "failed" : "success"); 136 if (error) 137 break; 138 if (device == ATA_MASTER) { 139 mask40 = 0x0000330f; 140 new40 = 0x00002307; 141 mask44 = 0; 142 new44 = 0; 143 } else { 144 mask40 = 0x000000f0; 145 new40 = 0x00000070; 146 mask44 = 0x0000000f; 147 new44 = 0x0000000b; 148 } 149 if (scp->unit) { 150 mask40 <<= 16; 151 new40 <<= 16; 152 mask44 <<= 4; 153 new44 <<= 4; 154 } 155 pci_write_config(scp->dev, 0x40, 156 (pci_read_config(scp->dev, 0x40, 4) & ~mask40) | 157 new40, 4); 158 pci_write_config(scp->dev, 0x44, 159 (pci_read_config(scp->dev, 0x44, 4) & ~mask44) | 160 new44, 4); 161 scp->mode[(device == ATA_MASTER) ? 0 : 1] = ATA_MODE_DMA; 162 return 0; 163 } 164 break; 165 166 case 0x12308086: /* Intel PIIX */ 167 /* probably not worth the trouble */ 168 break; 169 170 case 0x4d33105a: /* Promise Ultra/33 / FastTrack controllers */ 171 case 0x4d38105a: /* Promise Ultra/66 controllers */ 172 /* the Promise can only do DMA on ATA disks not on ATAPI devices */ 173 if ((device == ATA_MASTER && scp->devices & ATA_ATAPI_MASTER) || 174 (device == ATA_SLAVE && scp->devices & ATA_ATAPI_SLAVE)) 175 break; 176 177 devno = (scp->unit << 1) + ((device == ATA_MASTER) ? 0 : 1); 178 if (udmamode >=4 && type == 0x4d38105a) { 179 error = ata_command(scp, device, ATA_C_SETFEATURES, 0, 0, 0, 180 ATA_UDMA4, ATA_C_FEA_SETXFER, ATA_IGNORE_INTR); 181 if (bootverbose) 182 printf("ata%d: %s: %s setting up UDMA4 mode on Promise chip\n", 183 scp->lun, (device == ATA_MASTER) ? "master" : "slave", 184 (error) ? "failed" : "success"); 185 if (error) 186 break; 187 pci_write_config(scp->dev, 0x60 + (devno << 2), 0x004117f3, 4); 188 scp->mode[(device == ATA_MASTER) ? 0 : 1] = ATA_MODE_UDMA66; 189 return 0; 190 } 191 192 if (udmamode >=2) { 193 error = ata_command(scp, device, ATA_C_SETFEATURES, 0, 0, 0, 194 ATA_UDMA2, ATA_C_FEA_SETXFER, ATA_IGNORE_INTR); 195 if (bootverbose) 196 printf("ata%d: %s: %s setting up UDMA2 mode on Promise chip\n", 197 scp->lun, (device == ATA_MASTER) ? "master" : "slave", 198 (error) ? "failed" : "success"); 199 if (error) 200 break; 201 pci_write_config(scp->dev, 0x60 + (devno << 2), 0x004127f3, 4); 202 scp->mode[(device == ATA_MASTER) ? 0 : 1] = ATA_MODE_UDMA33; 203 return 0; 204 } 205 else if (wdmamode >= 2 && apiomode >= 4) { 206 error = ata_command(scp, device, ATA_C_SETFEATURES, 0, 0, 0, 207 ATA_WDMA2, ATA_C_FEA_SETXFER, ATA_IGNORE_INTR); 208 if (bootverbose) 209 printf("ata%d: %s: %s setting up WDMA2 mode on Promise chip\n", 210 scp->lun, (device == ATA_MASTER) ? "master" : "slave", 211 (error) ? "failed" : "success"); 212 if (error) 213 break; 214 pci_write_config(scp->dev, 0x60 + (devno << 2), 0x004367f3, 4); 215 scp->mode[(device == ATA_MASTER) ? 0 : 1] = ATA_MODE_DMA; 216 return 0; 217 } 218 else { 219 if (bootverbose) 220 printf("ata%d: %s: setting PIO mode on Promise chip\n", 221 scp->lun, (device == ATA_MASTER) ? "master" : "slave"); 222 pci_write_config(scp->dev, 0x60 + (devno << 2), 0x004fe924, 4); 223 } 224 break; 225 226 case 0x522910b9: /* AcerLabs Aladdin IV/V */ 227 /* the Aladdin has to be setup specially for ATAPI devices */ 228 if ((device == ATA_MASTER && scp->devices & ATA_ATAPI_MASTER) || 229 (device == ATA_SLAVE && scp->devices & ATA_ATAPI_SLAVE)) { 230 int8_t word53 = pci_read_config(scp->dev, 0x53, 1); 231 232 /* if needed set atapi fifo & dma */ 233 if ((udmamode >=2) || (wdmamode >= 2 && apiomode >= 4)) { 234 pci_write_config(scp->dev, 0x53, word53 | 0x03, 1); 235 scp->flags |= ATA_ATAPI_DMA_RO; 236 if (device == ATA_MASTER) 237 outb(scp->bmaddr + ATA_BMSTAT_PORT, 238 inb(scp->bmaddr + ATA_BMSTAT_PORT) | 239 ATA_BMSTAT_DMA_MASTER); 240 else 241 outb(scp->bmaddr + ATA_BMSTAT_PORT, 242 inb(scp->bmaddr + ATA_BMSTAT_PORT) | 243 ATA_BMSTAT_DMA_SLAVE); 244 } 245 else { 246 pci_write_config(scp->dev, 0x53, (word53 & ~0x01) | 0x02, 1); 247 } 248 } 249 if (udmamode >=2) { 250 int32_t word54 = pci_read_config(scp->dev, 0x54, 4); 251 252 error = ata_command(scp, device, ATA_C_SETFEATURES, 0, 0, 0, 253 ATA_UDMA2, ATA_C_FEA_SETXFER, ATA_IGNORE_INTR); 254 if (bootverbose) 255 printf("ata%d: %s: %s setting up UDMA2 mode on Aladdin chip\n", 256 scp->lun, (device == ATA_MASTER) ? "master" : "slave", 257 (error) ? "failed" : "success"); 258 if (error) 259 break; 260 word54 |= 0x5555; 261 word54 |= (0x0a << (16 + (scp->unit << 3) + (device << 2))); 262 pci_write_config(scp->dev, 0x54, word54, 4); 263 scp->mode[(device == ATA_MASTER) ? 0 : 1] = ATA_MODE_UDMA33; 264 return 0; 265 266 } 267 else if (wdmamode >= 2 && apiomode >= 4) { 268 error = ata_command(scp, device, ATA_C_SETFEATURES, 0, 0, 0, 269 ATA_WDMA2, ATA_C_FEA_SETXFER, ATA_IGNORE_INTR); 270 if (bootverbose) 271 printf("ata%d: %s: %s setting up WDMA2 mode on Aladdin chip\n", 272 scp->lun, (device == ATA_MASTER) ? "master" : "slave", 273 (error) ? "failed" : "success"); 274 if (error) 275 break; 276 scp->mode[(device == ATA_MASTER) ? 0 : 1] = ATA_MODE_DMA; 277 return 0; 278 } 279 break; 280 281 default: /* unknown controller chip */ 282 /* better not try generic DMA on ATAPI devices it almost never works */ 283 if ((device == ATA_MASTER && scp->devices & ATA_ATAPI_MASTER) || 284 (device == ATA_SLAVE && scp->devices & ATA_ATAPI_SLAVE)) 285 break; 286 287 /* well, we have no support for this, but try anyways */ 288 if (((wdmamode >= 2 && apiomode >= 4) || udmamode >= 2) && 289 (inb(scp->bmaddr + ATA_BMSTAT_PORT) & 290 ((device == ATA_MASTER) ? 291 ATA_BMSTAT_DMA_SLAVE : ATA_BMSTAT_DMA_MASTER))) { 292 error = ata_command(scp, device, ATA_C_SETFEATURES, 0, 0, 0, 293 ATA_WDMA2, ATA_C_FEA_SETXFER, ATA_IGNORE_INTR); 294 if (bootverbose) 295 printf("ata%d: %s: %s setting up WDMA2 mode on generic chip\n", 296 scp->lun, (device == ATA_MASTER) ? "master" : "slave", 297 (error) ? "failed" : "success"); 298 if (error) 299 break; 300 scp->mode[(device == ATA_MASTER) ? 0 : 1] = ATA_MODE_DMA; 301 return 0; 302 } 303 } 304 free(dmatab, M_DEVBUF); 305 return -1; 306} 307 308int32_t 309ata_dmasetup(struct ata_softc *scp, int32_t device, 310 int8_t *data, int32_t count, int32_t flags) 311{ 312 struct ata_dmaentry *dmatab; 313 u_int32_t dma_count, dma_base; 314 int32_t i = 0; 315 316#ifdef ATA_DMADEBUG 317 printf("ata%d: dmasetup\n", scp->lun); 318#endif 319 if (((uintptr_t)data & 1) || (count & 1)) 320 return -1; 321 322 if (!count) { 323#ifdef ATA_DMADEBUG 324 printf("ata%d: zero length DMA transfer attempt on %s\n", 325 scp->lun, ((device == ATA_MASTER) ? "master" : "slave")); 326#endif 327 return -1; 328 } 329 330 dmatab = scp->dmatab[(device == ATA_MASTER) ? 0 : 1]; 331 dma_base = vtophys(data); 332 dma_count = MIN(count, (PAGE_SIZE - ((uintptr_t)data & PAGE_MASK))); 333 data += dma_count; 334 count -= dma_count; 335 336 while (count) { 337 dmatab[i].base = dma_base; 338 dmatab[i].count = (dma_count & 0xffff); 339 i++; 340 if (i >= ATA_DMA_ENTRIES) { 341 printf("ata%d: too many segments in DMA table for %s\n", 342 scp->lun, (device ? "slave" : "master")); 343 return -1; 344 } 345 dma_base = vtophys(data); 346 dma_count = MIN(count, PAGE_SIZE); 347 data += MIN(count, PAGE_SIZE); 348 count -= MIN(count, PAGE_SIZE); 349 } 350#ifdef ATA_DMADEBUG 351 printf("ata_dmasetup: base=%08x count%08x\n", dma_base, dma_count); 352#endif 353 dmatab[i].base = dma_base; 354 dmatab[i].count = (dma_count & 0xffff) | ATA_DMA_EOT; 355 356 outl(scp->bmaddr + ATA_BMDTP_PORT, vtophys(dmatab)); 357#ifdef ATA_DMADEBUG 358 printf("dmatab=%08x %08x\n", 359 vtophys(dmatab), inl(scp->bmaddr+ATA_BMDTP_PORT)); 360#endif 361 outb(scp->bmaddr + ATA_BMCMD_PORT, flags ? ATA_BMCMD_WRITE_READ:0); 362 outb(scp->bmaddr + ATA_BMSTAT_PORT, (inb(scp->bmaddr + ATA_BMSTAT_PORT) | 363 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR))); 364 return 0; 365} 366 367void 368ata_dmastart(struct ata_softc *scp, int32_t device) 369{ 370#ifdef ATA_DMADEBUG 371 printf("ata%d: dmastart\n", scp->lun); 372#endif 373 outb(scp->bmaddr + ATA_BMCMD_PORT, 374 inb(scp->bmaddr + ATA_BMCMD_PORT) | ATA_BMCMD_START_STOP); 375} 376 377int32_t 378ata_dmadone(struct ata_softc *scp, int32_t device) 379{ 380#ifdef ATA_DMADEBUG 381 printf("ata%d: dmadone\n", scp->lun); 382#endif 383 outb(scp->bmaddr + ATA_BMCMD_PORT, 384 inb(scp->bmaddr + ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP); 385 return inb(scp->bmaddr + ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK; 386} 387 388int32_t 389ata_dmastatus(struct ata_softc *scp, int32_t device) 390{ 391#ifdef ATA_DMADEBUG 392 printf("ata%d: dmastatus\n", scp->lun); 393#endif 394 return inb(scp->bmaddr + ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK; 395} 396 397#else /* NPCI > 0 */ 398 399int32_t 400ata_dmainit(struct ata_softc *scp, int32_t device, 401 int32_t piomode, int32_t wdmamode, int32_t udmamode) 402{ 403 return -1; 404} 405 406int32_t 407ata_dmasetup(struct ata_softc *scp, int32_t device, 408 int8_t *data, int32_t count, int32_t flags) 409{ 410 return -1; 411} 412 413void 414ata_dmastart(struct ata_softc *scp, int32_t device) 415{ 416} 417 418int32_t 419ata_dmadone(struct ata_softc *scp, int32_t device) 420{ 421 return -1; 422} 423 424int32_t 425ata_dmastatus(struct ata_softc *scp, int32_t device) 426{ 427 return -1; 428} 429 430#endif /* NPCI > 0 */ 431