1 /* $OpenBSD: gdt_pci.c,v 1.19 2003/08/06 21:08:06 millert Exp $ */ 2 3 /* 4 * Copyright (c) 1999, 2000 Niklas Hallqvist. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 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 /* 28 * This driver would not have written if it was not for the hardware donations 29 * from both ICP-Vortex and �ko.neT. I want to thank them for their support. 30 */ 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/device.h> 35 #include <sys/kernel.h> 36 #include <sys/malloc.h> 37 #include <sys/queue.h> 38 39 #include <machine/bus.h> 40 #include <machine/endian.h> 41 #include <machine/intr.h> 42 43 #include <scsi/scsi_all.h> 44 #include <scsi/scsiconf.h> 45 46 #include <dev/pci/pcidevs.h> 47 #include <dev/pci/pcireg.h> 48 #include <dev/pci/pcivar.h> 49 50 #include <dev/ic/gdtreg.h> 51 #include <dev/ic/gdtvar.h> 52 53 /* Product numbers for Fibre-Channel are greater than or equal to 0x200 */ 54 #define GDT_PCI_PRODUCT_FC 0x200 55 56 /* Mapping registers for various areas */ 57 #define GDT_PCI_DPMEM 0x10 58 #define GDT_PCINEW_IOMEM 0x10 59 #define GDT_PCINEW_IO 0x14 60 #define GDT_PCINEW_DPMEM 0x18 61 62 /* PCI SRAM structure */ 63 #define GDT_MAGIC 0x00 /* u_int32_t, controller ID from BIOS */ 64 #define GDT_NEED_DEINIT 0x04 /* u_int16_t, switch between BIOS/driver */ 65 #define GDT_SWITCH_SUPPORT 0x06 /* u_int8_t, see GDT_NEED_DEINIT */ 66 #define GDT_OS_USED 0x10 /* u_int8_t [16], OS code per service */ 67 #define GDT_FW_MAGIC 0x3c /* u_int8_t, controller ID from firmware */ 68 #define GDT_SRAM_SZ 0x40 69 70 /* DPRAM PCI controllers */ 71 #define GDT_DPR_IF 0x00 /* interface area */ 72 #define GDT_6SR (0xff0 - GDT_SRAM_SZ) 73 #define GDT_SEMA1 0xff1 /* volatile u_int8_t, command semaphore */ 74 #define GDT_IRQEN 0xff5 /* u_int8_t, board interrupts enable */ 75 #define GDT_EVENT 0xff8 /* u_int8_t, release event */ 76 #define GDT_IRQDEL 0xffc /* u_int8_t, acknowledge board interrupt */ 77 #define GDT_DPRAM_SZ 0x1000 78 79 /* PLX register structure (new PCI controllers) */ 80 #define GDT_CFG_REG 0x00 /* u_int8_t, DPRAM cfg. (2: < 1MB, 0: any) */ 81 #define GDT_SEMA0_REG 0x40 /* volatile u_int8_t, command semaphore */ 82 #define GDT_SEMA1_REG 0x41 /* volatile u_int8_t, status semaphore */ 83 #define GDT_PLX_STATUS 0x44 /* volatile u_int16_t, command status */ 84 #define GDT_PLX_SERVICE 0x46 /* u_int16_t, service */ 85 #define GDT_PLX_INFO 0x48 /* u_int32_t [2], additional info */ 86 #define GDT_LDOOR_REG 0x60 /* u_int8_t, PCI to local doorbell */ 87 #define GDT_EDOOR_REG 0x64 /* volatile u_int8_t, local to PCI doorbell */ 88 #define GDT_CONTROL0 0x68 /* u_int8_t, control0 register (unused) */ 89 #define GDT_CONTROL1 0x69 /* u_int8_t, board interrupts enable */ 90 #define GDT_PLX_SZ 0x80 91 92 /* DPRAM new PCI controllers */ 93 #define GDT_IC 0x00 /* interface */ 94 #define GDT_PCINEW_6SR (0x4000 - GDT_SRAM_SZ) 95 /* SRAM structure */ 96 #define GDT_PCINEW_SZ 0x4000 97 98 /* i960 register structure (PCI MPR controllers) */ 99 #define GDT_MPR_SEMA0 0x10 /* volatile u_int8_t, command semaphore */ 100 #define GDT_MPR_SEMA1 0x12 /* volatile u_int8_t, status semaphore */ 101 #define GDT_MPR_STATUS 0x14 /* volatile u_int16_t, command status */ 102 #define GDT_MPR_SERVICE 0x16 /* u_int16_t, service */ 103 #define GDT_MPR_INFO 0x18 /* u_int32_t [2], additional info */ 104 #define GDT_MPR_LDOOR 0x20 /* u_int8_t, PCI to local doorbell */ 105 #define GDT_MPR_EDOOR 0x2c /* volatile u_int8_t, locl to PCI doorbell */ 106 #define GDT_EDOOR_EN 0x34 /* u_int8_t, board interrupts enable */ 107 #define GDT_I960_SZ 0x1000 108 109 /* DPRAM PCI MPR controllers */ 110 #define GDT_I960R 0x00 /* 4KB i960 registers */ 111 #define GDT_MPR_IC GDT_I960_SZ 112 /* interface area */ 113 #define GDT_MPR_6SR (GDT_I960_SZ + 0x3000 - GDT_SRAM_SZ) 114 /* SRAM structure */ 115 #define GDT_MPR_SZ 0x4000 116 117 int gdt_pci_probe(struct device *, void *, void *); 118 void gdt_pci_attach(struct device *, struct device *, void *); 119 void gdt_pci_enable_intr(struct gdt_softc *); 120 121 void gdt_pci_copy_cmd(struct gdt_softc *, struct gdt_ccb *); 122 u_int8_t gdt_pci_get_status(struct gdt_softc *); 123 void gdt_pci_intr(struct gdt_softc *, struct gdt_intr_ctx *); 124 void gdt_pci_release_event(struct gdt_softc *, struct gdt_ccb *); 125 void gdt_pci_set_sema0(struct gdt_softc *); 126 int gdt_pci_test_busy(struct gdt_softc *); 127 128 void gdt_pcinew_copy_cmd(struct gdt_softc *, struct gdt_ccb *); 129 u_int8_t gdt_pcinew_get_status(struct gdt_softc *); 130 void gdt_pcinew_intr(struct gdt_softc *, struct gdt_intr_ctx *); 131 void gdt_pcinew_release_event(struct gdt_softc *, struct gdt_ccb *); 132 void gdt_pcinew_set_sema0(struct gdt_softc *); 133 int gdt_pcinew_test_busy(struct gdt_softc *); 134 135 void gdt_mpr_copy_cmd(struct gdt_softc *, struct gdt_ccb *); 136 u_int8_t gdt_mpr_get_status(struct gdt_softc *); 137 void gdt_mpr_intr(struct gdt_softc *, struct gdt_intr_ctx *); 138 void gdt_mpr_release_event(struct gdt_softc *, struct gdt_ccb *); 139 void gdt_mpr_set_sema0(struct gdt_softc *); 140 int gdt_mpr_test_busy(struct gdt_softc *); 141 142 struct cfattach gdt_pci_ca = { 143 sizeof (struct gdt_softc), gdt_pci_probe, gdt_pci_attach 144 }; 145 146 int 147 gdt_pci_probe(parent, match, aux) 148 struct device *parent; 149 void *match, *aux; 150 { 151 struct pci_attach_args *pa = aux; 152 153 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_VORTEX && 154 PCI_PRODUCT(pa->pa_id) >= 0x100 && PCI_PRODUCT(pa->pa_id) <= 0x300) 155 return (1); 156 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_INTEL && 157 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_GDT_RAID1 || 158 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_GDT_RAID2)) 159 return (1); 160 return (0); 161 } 162 163 void 164 gdt_pci_attach(parent, self, aux) 165 struct device *parent, *self; 166 void *aux; 167 { 168 struct pci_attach_args *pa = aux; 169 struct gdt_softc *gdt = (void *)self; 170 bus_space_tag_t dpmemt, iomemt, iot; 171 bus_space_handle_t dpmemh, iomemh, ioh; 172 bus_addr_t dpmembase, iomembase, iobase; 173 bus_size_t dpmemsize, iomemsize, iosize; 174 u_int16_t prod; 175 u_int32_t status = 0; 176 #define DPMEM_MAPPED 1 177 #define IOMEM_MAPPED 2 178 #define IO_MAPPED 4 179 #define INTR_ESTABLISHED 8 180 int retries; 181 u_int8_t protocol; 182 pci_intr_handle_t ih; 183 const char *intrstr; 184 185 printf(": "); 186 187 gdt->sc_class = 0; 188 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_VORTEX) { 189 prod = PCI_PRODUCT(pa->pa_id); 190 switch (prod) { 191 case PCI_PRODUCT_VORTEX_GDT_60x0: 192 case PCI_PRODUCT_VORTEX_GDT_6000B: 193 gdt->sc_class = GDT_PCI; 194 break; 195 196 case PCI_PRODUCT_VORTEX_GDT_6x10: 197 case PCI_PRODUCT_VORTEX_GDT_6x20: 198 case PCI_PRODUCT_VORTEX_GDT_6530: 199 case PCI_PRODUCT_VORTEX_GDT_6550: 200 case PCI_PRODUCT_VORTEX_GDT_6x17: 201 case PCI_PRODUCT_VORTEX_GDT_6x27: 202 case PCI_PRODUCT_VORTEX_GDT_6537: 203 case PCI_PRODUCT_VORTEX_GDT_6557: 204 case PCI_PRODUCT_VORTEX_GDT_6x15: 205 case PCI_PRODUCT_VORTEX_GDT_6x25: 206 case PCI_PRODUCT_VORTEX_GDT_6535: 207 case PCI_PRODUCT_VORTEX_GDT_6555: 208 gdt->sc_class = GDT_PCINEW; 209 break; 210 211 case PCI_PRODUCT_VORTEX_GDT_6x17RP: 212 case PCI_PRODUCT_VORTEX_GDT_6x27RP: 213 case PCI_PRODUCT_VORTEX_GDT_6537RP: 214 case PCI_PRODUCT_VORTEX_GDT_6557RP: 215 case PCI_PRODUCT_VORTEX_GDT_6x11RP: 216 case PCI_PRODUCT_VORTEX_GDT_6x21RP: 217 case PCI_PRODUCT_VORTEX_GDT_6x17RD: 218 case PCI_PRODUCT_VORTEX_GDT_6x27RD: 219 case PCI_PRODUCT_VORTEX_GDT_6537RD: 220 case PCI_PRODUCT_VORTEX_GDT_6557RD: 221 case PCI_PRODUCT_VORTEX_GDT_6x11RD: 222 case PCI_PRODUCT_VORTEX_GDT_6x21RD: 223 case PCI_PRODUCT_VORTEX_GDT_6x18RD: 224 case PCI_PRODUCT_VORTEX_GDT_6x28RD: 225 case PCI_PRODUCT_VORTEX_GDT_6x38RD: 226 case PCI_PRODUCT_VORTEX_GDT_6x58RD: 227 case PCI_PRODUCT_VORTEX_GDT_6518RS: 228 case PCI_PRODUCT_VORTEX_GDT_7x18RN: 229 case PCI_PRODUCT_VORTEX_GDT_7x28RN: 230 case PCI_PRODUCT_VORTEX_GDT_7x38RN: 231 case PCI_PRODUCT_VORTEX_GDT_7x58RN: 232 case PCI_PRODUCT_VORTEX_GDT_6x19RD: 233 case PCI_PRODUCT_VORTEX_GDT_6x29RD: 234 case PCI_PRODUCT_VORTEX_GDT_7x19RN: 235 case PCI_PRODUCT_VORTEX_GDT_7x29RN: 236 case PCI_PRODUCT_VORTEX_GDT_7x43RN: 237 gdt->sc_class = GDT_MPR; 238 } 239 240 /* If we don't recognize it, determine class heuristically. */ 241 if (gdt->sc_class == 0) 242 gdt->sc_class = prod < 0x100 ? GDT_PCINEW : GDT_MPR; 243 244 if (prod >= GDT_PCI_PRODUCT_FC) 245 gdt->sc_class |= GDT_FC; 246 247 } else if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_INTEL) { 248 gdt->sc_class = GDT_MPR; 249 } 250 251 if (pci_mapreg_map(pa, 252 GDT_CLASS(gdt) == GDT_PCINEW ? GDT_PCINEW_DPMEM : GDT_PCI_DPMEM, 253 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0, &dpmemt, 254 &dpmemh, &dpmembase, &dpmemsize, 0)) { 255 if (pci_mapreg_map(pa, 256 GDT_CLASS(gdt) == GDT_PCINEW ? GDT_PCINEW_DPMEM : 257 GDT_PCI_DPMEM, 258 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT_1M, 0, 259 &dpmemt,&dpmemh, &dpmembase, &dpmemsize, 0)) { 260 printf("cannot map DPMEM\n"); 261 goto bail_out; 262 } 263 } 264 status |= DPMEM_MAPPED; 265 gdt->sc_dpmemt = dpmemt; 266 gdt->sc_dpmemh = dpmemh; 267 gdt->sc_dpmembase = dpmembase; 268 gdt->sc_dmat = pa->pa_dmat; 269 270 /* 271 * The GDT_PCINEW series also has two other regions to map. 272 */ 273 if (GDT_CLASS(gdt) == GDT_PCINEW) { 274 if (pci_mapreg_map(pa, GDT_PCINEW_IOMEM, PCI_MAPREG_TYPE_MEM, 275 0, &iomemt, &iomemh, &iomembase, &iomemsize, 0)) { 276 printf("cannot map memory mapped I/O ports\n"); 277 goto bail_out; 278 } 279 status |= IOMEM_MAPPED; 280 281 if (pci_mapreg_map(pa, GDT_PCINEW_IO, PCI_MAPREG_TYPE_IO, 0, 282 &iot, &ioh, &iobase, &iosize, 0)) { 283 printf("cannot map I/O ports\n"); 284 goto bail_out; 285 } 286 status |= IO_MAPPED; 287 gdt->sc_iot = iot; 288 gdt->sc_ioh = ioh; 289 gdt->sc_iobase = iobase; 290 } 291 292 switch (GDT_CLASS(gdt)) { 293 case GDT_PCI: 294 bus_space_set_region_4(dpmemt, dpmemh, 0, 0, 295 GDT_DPR_IF_SZ >> 2); 296 if (bus_space_read_1(dpmemt, dpmemh, 0) != 0) { 297 printf("cannot write to DPMEM\n"); 298 goto bail_out; 299 } 300 301 #if 0 302 /* disable board interrupts, deinit services */ 303 gdth_writeb(0xff, &dp6_ptr->io.irqdel); 304 gdth_writeb(0x00, &dp6_ptr->io.irqen); 305 gdth_writeb(0x00, &dp6_ptr->u.ic.S_Status); 306 gdth_writeb(0x00, &dp6_ptr->u.ic.Cmd_Index); 307 308 gdth_writel(pcistr->dpmem, &dp6_ptr->u.ic.S_Info[0]); 309 gdth_writeb(0xff, &dp6_ptr->u.ic.S_Cmd_Indx); 310 gdth_writeb(0, &dp6_ptr->io.event); 311 retries = INIT_RETRIES; 312 gdth_delay(20); 313 while (gdth_readb(&dp6_ptr->u.ic.S_Status) != 0xff) { 314 if (--retries == 0) { 315 printk("initialization error (DEINIT failed)\n"); 316 gdth_munmap(ha->brd); 317 return 0; 318 } 319 gdth_delay(1); 320 } 321 prot_ver = (unchar)gdth_readl(&dp6_ptr->u.ic.S_Info[0]); 322 gdth_writeb(0, &dp6_ptr->u.ic.S_Status); 323 gdth_writeb(0xff, &dp6_ptr->io.irqdel); 324 if (prot_ver != PROTOCOL_VERSION) { 325 printk("illegal protocol version\n"); 326 gdth_munmap(ha->brd); 327 return 0; 328 } 329 330 ha->type = GDT_PCI; 331 ha->ic_all_size = sizeof(dp6_ptr->u); 332 333 /* special command to controller BIOS */ 334 gdth_writel(0x00, &dp6_ptr->u.ic.S_Info[0]); 335 gdth_writel(0x00, &dp6_ptr->u.ic.S_Info[1]); 336 gdth_writel(0x01, &dp6_ptr->u.ic.S_Info[2]); 337 gdth_writel(0x00, &dp6_ptr->u.ic.S_Info[3]); 338 gdth_writeb(0xfe, &dp6_ptr->u.ic.S_Cmd_Indx); 339 gdth_writeb(0, &dp6_ptr->io.event); 340 retries = INIT_RETRIES; 341 gdth_delay(20); 342 while (gdth_readb(&dp6_ptr->u.ic.S_Status) != 0xfe) { 343 if (--retries == 0) { 344 printk("initialization error\n"); 345 gdth_munmap(ha->brd); 346 return 0; 347 } 348 gdth_delay(1); 349 } 350 gdth_writeb(0, &dp6_ptr->u.ic.S_Status); 351 gdth_writeb(0xff, &dp6_ptr->io.irqdel); 352 #endif 353 354 gdt->sc_ic_all_size = GDT_DPRAM_SZ; 355 356 gdt->sc_copy_cmd = gdt_pci_copy_cmd; 357 gdt->sc_get_status = gdt_pci_get_status; 358 gdt->sc_intr = gdt_pci_intr; 359 gdt->sc_release_event = gdt_pci_release_event; 360 gdt->sc_set_sema0 = gdt_pci_set_sema0; 361 gdt->sc_test_busy = gdt_pci_test_busy; 362 363 break; 364 365 case GDT_PCINEW: 366 bus_space_set_region_4(dpmemt, dpmemh, 0, 0, 367 GDT_DPR_IF_SZ >> 2); 368 if (bus_space_read_1(dpmemt, dpmemh, 0) != 0) { 369 printf("cannot write to DPMEM\n"); 370 goto bail_out; 371 } 372 373 #if 0 374 /* disable board interrupts, deinit services */ 375 outb(0x00,PTR2USHORT(&ha->plx->control1)); 376 outb(0xff,PTR2USHORT(&ha->plx->edoor_reg)); 377 378 gdth_writeb(0x00, &dp6c_ptr->u.ic.S_Status); 379 gdth_writeb(0x00, &dp6c_ptr->u.ic.Cmd_Index); 380 381 gdth_writel(pcistr->dpmem, &dp6c_ptr->u.ic.S_Info[0]); 382 gdth_writeb(0xff, &dp6c_ptr->u.ic.S_Cmd_Indx); 383 384 outb(1,PTR2USHORT(&ha->plx->ldoor_reg)); 385 386 retries = INIT_RETRIES; 387 gdth_delay(20); 388 while (gdth_readb(&dp6c_ptr->u.ic.S_Status) != 0xff) { 389 if (--retries == 0) { 390 printk("initialization error (DEINIT failed)\n"); 391 gdth_munmap(ha->brd); 392 return 0; 393 } 394 gdth_delay(1); 395 } 396 prot_ver = (unchar)gdth_readl(&dp6c_ptr->u.ic.S_Info[0]); 397 gdth_writeb(0, &dp6c_ptr->u.ic.Status); 398 if (prot_ver != PROTOCOL_VERSION) { 399 printk("illegal protocol version\n"); 400 gdth_munmap(ha->brd); 401 return 0; 402 } 403 404 ha->type = GDT_PCINEW; 405 ha->ic_all_size = sizeof(dp6c_ptr->u); 406 407 /* special command to controller BIOS */ 408 gdth_writel(0x00, &dp6c_ptr->u.ic.S_Info[0]); 409 gdth_writel(0x00, &dp6c_ptr->u.ic.S_Info[1]); 410 gdth_writel(0x01, &dp6c_ptr->u.ic.S_Info[2]); 411 gdth_writel(0x00, &dp6c_ptr->u.ic.S_Info[3]); 412 gdth_writeb(0xfe, &dp6c_ptr->u.ic.S_Cmd_Indx); 413 414 outb(1,PTR2USHORT(&ha->plx->ldoor_reg)); 415 416 retries = INIT_RETRIES; 417 gdth_delay(20); 418 while (gdth_readb(&dp6c_ptr->u.ic.S_Status) != 0xfe) { 419 if (--retries == 0) { 420 printk("initialization error\n"); 421 gdth_munmap(ha->brd); 422 return 0; 423 } 424 gdth_delay(1); 425 } 426 gdth_writeb(0, &dp6c_ptr->u.ic.S_Status); 427 #endif 428 429 gdt->sc_ic_all_size = GDT_PCINEW_SZ; 430 431 gdt->sc_copy_cmd = gdt_pcinew_copy_cmd; 432 gdt->sc_get_status = gdt_pcinew_get_status; 433 gdt->sc_intr = gdt_pcinew_intr; 434 gdt->sc_release_event = gdt_pcinew_release_event; 435 gdt->sc_set_sema0 = gdt_pcinew_set_sema0; 436 gdt->sc_test_busy = gdt_pcinew_test_busy; 437 438 break; 439 440 case GDT_MPR: 441 bus_space_write_4(dpmemt, dpmemh, GDT_MPR_IC, GDT_MPR_MAGIC); 442 if (bus_space_read_4(dpmemt, dpmemh, GDT_MPR_IC) != 443 GDT_MPR_MAGIC) { 444 printf("cannot access DPMEM at 0x%x (shadowed?)\n", 445 dpmembase); 446 goto bail_out; 447 } 448 449 /* 450 * XXX Here the Linux driver has a weird remapping logic I 451 * don't understand. My controller does not need it, and I 452 * cannot see what purpose it serves, therefore I did not 453 * do anything similar. 454 */ 455 456 bus_space_set_region_4(dpmemt, dpmemh, GDT_I960_SZ, 0, 457 GDT_DPR_IF_SZ >> 2); 458 459 /* Disable everything */ 460 bus_space_write_1(dpmemt, dpmemh, GDT_EDOOR_EN, 461 bus_space_read_1(dpmemt, dpmemh, GDT_EDOOR_EN) | 4); 462 bus_space_write_1(dpmemt, dpmemh, GDT_MPR_EDOOR, 0xff); 463 bus_space_write_1(dpmemt, dpmemh, GDT_MPR_IC + GDT_S_STATUS, 464 0); 465 bus_space_write_1(dpmemt, dpmemh, GDT_MPR_IC + GDT_CMD_INDEX, 466 0); 467 468 bus_space_write_4(dpmemt, dpmemh, GDT_MPR_IC + GDT_S_INFO, 469 dpmembase); 470 bus_space_write_1(dpmemt, dpmemh, GDT_MPR_IC + GDT_S_CMD_INDX, 471 0xff); 472 bus_space_write_1(dpmemt, dpmemh, GDT_MPR_LDOOR, 1); 473 474 DELAY(20); 475 retries = GDT_RETRIES; 476 while (bus_space_read_1(dpmemt, dpmemh, 477 GDT_MPR_IC + GDT_S_STATUS) != 0xff) { 478 if (--retries == 0) { 479 printf("DEINIT failed (status 0x%x)\n", 480 bus_space_read_1(dpmemt, dpmemh, 481 GDT_MPR_IC + GDT_S_STATUS)); 482 goto bail_out; 483 } 484 DELAY(1); 485 } 486 487 protocol = (u_int8_t)bus_space_read_4(dpmemt, dpmemh, 488 GDT_MPR_IC + GDT_S_INFO); 489 bus_space_write_1(dpmemt, dpmemh, GDT_MPR_IC + GDT_S_STATUS, 490 0); 491 if (protocol != GDT_PROTOCOL_VERSION) { 492 printf("unsupported protocol %d\n", protocol); 493 goto bail_out; 494 } 495 496 /* special commnd to controller BIOS */ 497 bus_space_write_4(dpmemt, dpmemh, GDT_MPR_IC + GDT_S_INFO, 0); 498 bus_space_write_4(dpmemt, dpmemh, 499 GDT_MPR_IC + GDT_S_INFO + sizeof (u_int32_t), 0); 500 bus_space_write_4(dpmemt, dpmemh, 501 GDT_MPR_IC + GDT_S_INFO + 2 * sizeof (u_int32_t), 1); 502 bus_space_write_4(dpmemt, dpmemh, 503 GDT_MPR_IC + GDT_S_INFO + 3 * sizeof (u_int32_t), 0); 504 bus_space_write_1(dpmemt, dpmemh, GDT_MPR_IC + GDT_S_CMD_INDX, 505 0xfe); 506 bus_space_write_1(dpmemt, dpmemh, GDT_MPR_LDOOR, 1); 507 508 DELAY(20); 509 retries = GDT_RETRIES; 510 while (bus_space_read_1(dpmemt, dpmemh, 511 GDT_MPR_IC + GDT_S_STATUS) != 0xfe) { 512 if (--retries == 0) { 513 printf("initialization error\n"); 514 goto bail_out; 515 } 516 DELAY(1); 517 } 518 519 bus_space_write_1(dpmemt, dpmemh, GDT_MPR_IC + GDT_S_STATUS, 520 0); 521 522 gdt->sc_ic_all_size = GDT_MPR_SZ; 523 524 gdt->sc_copy_cmd = gdt_mpr_copy_cmd; 525 gdt->sc_get_status = gdt_mpr_get_status; 526 gdt->sc_intr = gdt_mpr_intr; 527 gdt->sc_release_event = gdt_mpr_release_event; 528 gdt->sc_set_sema0 = gdt_mpr_set_sema0; 529 gdt->sc_test_busy = gdt_mpr_test_busy; 530 } 531 532 if (pci_intr_map(pa, &ih)) { 533 printf("couldn't map interrupt\n"); 534 goto bail_out; 535 } 536 intrstr = pci_intr_string(pa->pa_pc, ih); 537 gdt->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO, gdt_intr, gdt, 538 gdt->sc_dev.dv_xname); 539 if (gdt->sc_ih == NULL) { 540 printf("couldn't establish interrupt"); 541 if (intrstr != NULL) 542 printf(" at %s", intrstr); 543 printf("\n"); 544 goto bail_out; 545 } 546 status |= INTR_ESTABLISHED; 547 if (intrstr != NULL) 548 printf("%s ", intrstr); 549 550 if (gdt_attach(gdt)) 551 goto bail_out; 552 553 gdt_pci_enable_intr(gdt); 554 555 return; 556 557 bail_out: 558 if (status & DPMEM_MAPPED) 559 bus_space_unmap(dpmemt, dpmemh, dpmemsize); 560 if (status & IOMEM_MAPPED) 561 bus_space_unmap(iomemt, iomemh, iomembase); 562 if (status & IO_MAPPED) 563 bus_space_unmap(iot, ioh, iosize); 564 if (status & INTR_ESTABLISHED) 565 pci_intr_disestablish(pa->pa_pc, gdt->sc_ih); 566 return; 567 } 568 569 /* Enable interrupts */ 570 void 571 gdt_pci_enable_intr(gdt) 572 struct gdt_softc *gdt; 573 { 574 GDT_DPRINTF(GDT_D_INTR, ("gdt_pci_enable_intr(%p) ", gdt)); 575 576 switch(GDT_CLASS(gdt)) { 577 case GDT_PCI: 578 bus_space_write_1(gdt->sc_dpmemt, gdt->sc_dpmemh, GDT_IRQDEL, 579 1); 580 bus_space_write_1(gdt->sc_dpmemt, gdt->sc_dpmemh, 581 GDT_CMD_INDEX, 0); 582 bus_space_write_1(gdt->sc_dpmemt, gdt->sc_dpmemh, GDT_IRQEN, 583 1); 584 break; 585 586 case GDT_PCINEW: 587 bus_space_write_1(gdt->sc_iot, gdt->sc_ioh, GDT_EDOOR_REG, 588 0xff); 589 bus_space_write_1(gdt->sc_iot, gdt->sc_ioh, GDT_CONTROL1, 3); 590 break; 591 592 case GDT_MPR: 593 bus_space_write_1(gdt->sc_dpmemt, gdt->sc_dpmemh, 594 GDT_MPR_EDOOR, 0xff); 595 bus_space_write_1(gdt->sc_dpmemt, gdt->sc_dpmemh, GDT_EDOOR_EN, 596 bus_space_read_1(gdt->sc_dpmemt, gdt->sc_dpmemh, 597 GDT_EDOOR_EN) & ~4); 598 break; 599 } 600 } 601 602 /* 603 * "old" PCI controller-specific functions 604 */ 605 606 void 607 gdt_pci_copy_cmd(gdt, ccb) 608 struct gdt_softc *gdt; 609 struct gdt_ccb *ccb; 610 { 611 /* XXX Not yet implemented */ 612 } 613 614 u_int8_t 615 gdt_pci_get_status(gdt) 616 struct gdt_softc *gdt; 617 { 618 /* XXX Not yet implemented */ 619 return (0); 620 } 621 622 void 623 gdt_pci_intr(gdt, ctx) 624 struct gdt_softc *gdt; 625 struct gdt_intr_ctx *ctx; 626 { 627 /* XXX Not yet implemented */ 628 } 629 630 void 631 gdt_pci_release_event(gdt, ccb) 632 struct gdt_softc *gdt; 633 struct gdt_ccb *ccb; 634 { 635 /* XXX Not yet implemented */ 636 } 637 638 void 639 gdt_pci_set_sema0(gdt) 640 struct gdt_softc *gdt; 641 { 642 bus_space_write_1(gdt->sc_dpmemt, gdt->sc_dpmemh, GDT_SEMA0, 1); 643 } 644 645 int 646 gdt_pci_test_busy(gdt) 647 struct gdt_softc *gdt; 648 { 649 /* XXX Not yet implemented */ 650 return (0); 651 } 652 653 /* 654 * "new" PCI controller-specific functions 655 */ 656 657 void 658 gdt_pcinew_copy_cmd(gdt, ccb) 659 struct gdt_softc *gdt; 660 struct gdt_ccb *ccb; 661 { 662 /* XXX Not yet implemented */ 663 } 664 665 u_int8_t 666 gdt_pcinew_get_status(gdt) 667 struct gdt_softc *gdt; 668 { 669 /* XXX Not yet implemented */ 670 return (0); 671 } 672 673 void 674 gdt_pcinew_intr(gdt, ctx) 675 struct gdt_softc *gdt; 676 struct gdt_intr_ctx *ctx; 677 { 678 /* XXX Not yet implemented */ 679 } 680 681 void 682 gdt_pcinew_release_event(gdt, ccb) 683 struct gdt_softc *gdt; 684 struct gdt_ccb *ccb; 685 { 686 /* XXX Not yet implemented */ 687 } 688 689 void 690 gdt_pcinew_set_sema0(gdt) 691 struct gdt_softc *gdt; 692 { 693 bus_space_write_1(gdt->sc_iot, gdt->sc_ioh, GDT_SEMA0_REG, 1); 694 } 695 696 int 697 gdt_pcinew_test_busy(gdt) 698 struct gdt_softc *gdt; 699 { 700 /* XXX Not yet implemented */ 701 return (0); 702 } 703 704 /* 705 * MPR PCI controller-specific functions 706 */ 707 708 void 709 gdt_mpr_copy_cmd(gdt, ccb) 710 struct gdt_softc *gdt; 711 struct gdt_ccb *ccb; 712 { 713 u_int16_t cp_count = roundup(gdt->sc_cmd_len, sizeof (u_int32_t)); 714 u_int16_t dp_offset = gdt->sc_cmd_off; 715 u_int16_t cmd_no = gdt->sc_cmd_cnt++; 716 717 GDT_DPRINTF(GDT_D_CMD, ("gdt_mpr_copy_cmd(%p) ", gdt)); 718 719 gdt->sc_cmd_off += cp_count; 720 721 bus_space_write_2(gdt->sc_dpmemt, gdt->sc_dpmemh, 722 GDT_MPR_IC + GDT_COMM_QUEUE + cmd_no * GDT_COMM_Q_SZ + GDT_OFFSET, 723 GDT_DPMEM_COMMAND_OFFSET + dp_offset); 724 bus_space_write_2(gdt->sc_dpmemt, gdt->sc_dpmemh, 725 GDT_MPR_IC + GDT_COMM_QUEUE + cmd_no * GDT_COMM_Q_SZ + GDT_SERV_ID, 726 ccb->gc_service); 727 bus_space_write_raw_region_4(gdt->sc_dpmemt, gdt->sc_dpmemh, 728 GDT_MPR_IC + GDT_DPR_CMD + dp_offset, gdt->sc_cmd, cp_count); 729 } 730 731 u_int8_t 732 gdt_mpr_get_status(gdt) 733 struct gdt_softc *gdt; 734 { 735 GDT_DPRINTF(GDT_D_MISC, ("gdt_mpr_get_status(%p) ", gdt)); 736 737 return bus_space_read_1(gdt->sc_dpmemt, gdt->sc_dpmemh, GDT_MPR_EDOOR); 738 } 739 740 void 741 gdt_mpr_intr(gdt, ctx) 742 struct gdt_softc *gdt; 743 struct gdt_intr_ctx *ctx; 744 { 745 GDT_DPRINTF(GDT_D_INTR, ("gdt_mpr_intr(%p) ", gdt)); 746 747 if (ctx->istatus & 0x80) { /* error flag */ 748 ctx->istatus &= ~0x80; 749 ctx->cmd_status = bus_space_read_2(gdt->sc_dpmemt, 750 gdt->sc_dpmemh, GDT_MPR_STATUS); 751 if (ctx->istatus == GDT_ASYNCINDEX) { 752 ctx->service = bus_space_read_2(gdt->sc_dpmemt, 753 gdt->sc_dpmemh, GDT_MPR_SERVICE); 754 ctx->info2 = bus_space_read_4(gdt->sc_dpmemt, 755 gdt->sc_dpmemh, GDT_MPR_INFO + sizeof (u_int32_t)); 756 } 757 } else /* no error */ 758 ctx->cmd_status = GDT_S_OK; 759 760 ctx->info = 761 bus_space_read_4(gdt->sc_dpmemt, gdt->sc_dpmemh, GDT_MPR_INFO); 762 763 if (gdt_polling) /* init. -> more info */ 764 ctx->info2 = bus_space_read_4(gdt->sc_dpmemt, gdt->sc_dpmemh, 765 GDT_MPR_INFO + sizeof (u_int32_t)); 766 bus_space_write_1(gdt->sc_dpmemt, gdt->sc_dpmemh, GDT_MPR_EDOOR, 0xff); 767 bus_space_write_1(gdt->sc_dpmemt, gdt->sc_dpmemh, GDT_MPR_SEMA1, 0); 768 } 769 770 void 771 gdt_mpr_release_event(gdt, ccb) 772 struct gdt_softc *gdt; 773 struct gdt_ccb *ccb; 774 { 775 GDT_DPRINTF(GDT_D_MISC, ("gdt_mpr_release_event(%p) ", gdt)); 776 777 if (gdt_dec16(gdt->sc_cmd + GDT_CMD_OPCODE) == GDT_INIT) 778 ccb->gc_service |= 0x80; 779 bus_space_write_1(gdt->sc_dpmemt, gdt->sc_dpmemh, GDT_MPR_LDOOR, 1); 780 } 781 782 void 783 gdt_mpr_set_sema0(gdt) 784 struct gdt_softc *gdt; 785 { 786 GDT_DPRINTF(GDT_D_MISC, ("gdt_mpr_set_sema0(%p) ", gdt)); 787 788 bus_space_write_1(gdt->sc_dpmemt, gdt->sc_dpmemh, GDT_MPR_SEMA0, 1); 789 } 790 791 int 792 gdt_mpr_test_busy(gdt) 793 struct gdt_softc *gdt; 794 { 795 GDT_DPRINTF(GDT_D_MISC, ("gdt_mpr_test_busy(%p) ", gdt)); 796 797 return (bus_space_read_1(gdt->sc_dpmemt, gdt->sc_dpmemh, 798 GDT_MPR_SEMA0) & 1); 799 } 800