14249382dSDoug Rabson /* 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3fe267a55SPedro F. Giffuni * 44249382dSDoug Rabson * Copyright (c) 1996, Sujal M. Patel 54249382dSDoug Rabson * All rights reserved. 64249382dSDoug Rabson * 74249382dSDoug Rabson * Redistribution and use in source and binary forms, with or without 84249382dSDoug Rabson * modification, are permitted provided that the following conditions 94249382dSDoug Rabson * are met: 104249382dSDoug Rabson * 1. Redistributions of source code must retain the above copyright 114249382dSDoug Rabson * notice, this list of conditions and the following disclaimer. 124249382dSDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright 134249382dSDoug Rabson * notice, this list of conditions and the following disclaimer in the 144249382dSDoug Rabson * documentation and/or other materials provided with the distribution. 154249382dSDoug Rabson * 164249382dSDoug Rabson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 174249382dSDoug Rabson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 184249382dSDoug Rabson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 194249382dSDoug Rabson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 204249382dSDoug Rabson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 214249382dSDoug Rabson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 224249382dSDoug Rabson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 234249382dSDoug Rabson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 244249382dSDoug Rabson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 254249382dSDoug Rabson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 264249382dSDoug Rabson * SUCH DAMAGE. 274249382dSDoug Rabson * 284249382dSDoug Rabson * from: pnp.c,v 1.11 1999/05/06 22:11:19 peter Exp 294249382dSDoug Rabson */ 304249382dSDoug Rabson 314249382dSDoug Rabson #include <sys/param.h> 324249382dSDoug Rabson #include <sys/systm.h> 334249382dSDoug Rabson #include <sys/kernel.h> 344249382dSDoug Rabson #include <sys/module.h> 354249382dSDoug Rabson #include <sys/bus.h> 36c7974ff1SMarius Strobl #include <sys/endian.h> 374249382dSDoug Rabson #include <sys/malloc.h> 384249382dSDoug Rabson #include <isa/isavar.h> 394249382dSDoug Rabson #include <isa/pnpreg.h> 404249382dSDoug Rabson #include <isa/pnpvar.h> 4121c3015aSDoug Rabson #include <machine/bus.h> 424249382dSDoug Rabson 434249382dSDoug Rabson typedef struct _pnp_id { 447a83aa63SWarner Losh uint32_t vendor_id; 457a83aa63SWarner Losh uint32_t serial; 464249382dSDoug Rabson u_char checksum; 474249382dSDoug Rabson } pnp_id; 484249382dSDoug Rabson 494249382dSDoug Rabson struct pnp_set_config_arg { 504249382dSDoug Rabson int csn; /* Card number to configure */ 514249382dSDoug Rabson int ldn; /* Logical device on card */ 524249382dSDoug Rabson }; 534249382dSDoug Rabson 544249382dSDoug Rabson struct pnp_quirk { 557a83aa63SWarner Losh uint32_t vendor_id; /* Vendor of the card */ 567a83aa63SWarner Losh uint32_t logical_id; /* ID of the device with quirk */ 574249382dSDoug Rabson int type; 584249382dSDoug Rabson #define PNP_QUIRK_WRITE_REG 1 /* Need to write a pnp register */ 59fb0ef528SSeigo Tanimura #define PNP_QUIRK_EXTRA_IO 2 /* Has extra io ports */ 604249382dSDoug Rabson int arg1; 614249382dSDoug Rabson int arg2; 624249382dSDoug Rabson }; 634249382dSDoug Rabson 644249382dSDoug Rabson struct pnp_quirk pnp_quirks[] = { 654249382dSDoug Rabson /* 664249382dSDoug Rabson * The Gravis UltraSound needs register 0xf2 to be set to 0xff 674249382dSDoug Rabson * to enable power. 684249382dSDoug Rabson * XXX need to know the logical device id. 694249382dSDoug Rabson */ 704249382dSDoug Rabson { 0x0100561e /* GRV0001 */, 0, 714249382dSDoug Rabson PNP_QUIRK_WRITE_REG, 0xf2, 0xff }, 72fb0ef528SSeigo Tanimura /* 73fb0ef528SSeigo Tanimura * An emu8000 does not give us other than the first 74fb0ef528SSeigo Tanimura * port. 75fb0ef528SSeigo Tanimura */ 76fb0ef528SSeigo Tanimura { 0x26008c0e /* SB16 */, 0x21008c0e, 77fb0ef528SSeigo Tanimura PNP_QUIRK_EXTRA_IO, 0x400, 0x800 }, 78fb0ef528SSeigo Tanimura { 0x42008c0e /* SB32(CTL0042) */, 0x21008c0e, 79fb0ef528SSeigo Tanimura PNP_QUIRK_EXTRA_IO, 0x400, 0x800 }, 80fb0ef528SSeigo Tanimura { 0x44008c0e /* SB32(CTL0044) */, 0x21008c0e, 81fb0ef528SSeigo Tanimura PNP_QUIRK_EXTRA_IO, 0x400, 0x800 }, 82fb0ef528SSeigo Tanimura { 0x49008c0e /* SB32(CTL0049) */, 0x21008c0e, 83fb0ef528SSeigo Tanimura PNP_QUIRK_EXTRA_IO, 0x400, 0x800 }, 84fb0ef528SSeigo Tanimura { 0xf1008c0e /* SB32(CTL00f1) */, 0x21008c0e, 85fb0ef528SSeigo Tanimura PNP_QUIRK_EXTRA_IO, 0x400, 0x800 }, 86fb0ef528SSeigo Tanimura { 0xc1008c0e /* SB64(CTL00c1) */, 0x22008c0e, 87fb0ef528SSeigo Tanimura PNP_QUIRK_EXTRA_IO, 0x400, 0x800 }, 8838b968c3SSeigo Tanimura { 0xc5008c0e /* SB64(CTL00c5) */, 0x22008c0e, 8938b968c3SSeigo Tanimura PNP_QUIRK_EXTRA_IO, 0x400, 0x800 }, 90fb0ef528SSeigo Tanimura { 0xe4008c0e /* SB64(CTL00e4) */, 0x22008c0e, 91fb0ef528SSeigo Tanimura PNP_QUIRK_EXTRA_IO, 0x400, 0x800 }, 924249382dSDoug Rabson 934249382dSDoug Rabson { 0 } 944249382dSDoug Rabson }; 954249382dSDoug Rabson 964249382dSDoug Rabson /* The READ_DATA port that we are using currently */ 974249382dSDoug Rabson static int pnp_rd_port; 984249382dSDoug Rabson 994249382dSDoug Rabson static void pnp_send_initiation_key(void); 1004249382dSDoug Rabson static int pnp_get_serial(pnp_id *p); 1014249382dSDoug Rabson static int pnp_isolation_protocol(device_t parent); 1024249382dSDoug Rabson 1034249382dSDoug Rabson static void 1044249382dSDoug Rabson pnp_write(int d, u_char r) 1054249382dSDoug Rabson { 1064249382dSDoug Rabson outb (_PNP_ADDRESS, d); 1074249382dSDoug Rabson outb (_PNP_WRITE_DATA, r); 1084249382dSDoug Rabson } 1094249382dSDoug Rabson 1104249382dSDoug Rabson /* 1114249382dSDoug Rabson * Send Initiation LFSR as described in "Plug and Play ISA Specification", 1124249382dSDoug Rabson * Intel May 94. 1134249382dSDoug Rabson */ 1144249382dSDoug Rabson static void 11524e8823eSDimitry Andric pnp_send_initiation_key(void) 1164249382dSDoug Rabson { 1174249382dSDoug Rabson int cur, i; 1184249382dSDoug Rabson 1194249382dSDoug Rabson /* Reset the LSFR */ 1204249382dSDoug Rabson outb(_PNP_ADDRESS, 0); 1214249382dSDoug Rabson outb(_PNP_ADDRESS, 0); /* yes, we do need it twice! */ 1224249382dSDoug Rabson 1234249382dSDoug Rabson cur = 0x6a; 1244249382dSDoug Rabson outb(_PNP_ADDRESS, cur); 1254249382dSDoug Rabson 1264249382dSDoug Rabson for (i = 1; i < 32; i++) { 1274249382dSDoug Rabson cur = (cur >> 1) | (((cur ^ (cur >> 1)) << 7) & 0xff); 1284249382dSDoug Rabson outb(_PNP_ADDRESS, cur); 1294249382dSDoug Rabson } 1304249382dSDoug Rabson } 1314249382dSDoug Rabson 1324249382dSDoug Rabson 1334249382dSDoug Rabson /* 1344249382dSDoug Rabson * Get the device's serial number. Returns 1 if the serial is valid. 1354249382dSDoug Rabson */ 1364249382dSDoug Rabson static int 1374249382dSDoug Rabson pnp_get_serial(pnp_id *p) 1384249382dSDoug Rabson { 1394249382dSDoug Rabson int i, bit, valid = 0, sum = 0x6a; 1404249382dSDoug Rabson u_char *data = (u_char *)p; 1414249382dSDoug Rabson 1424249382dSDoug Rabson bzero(data, sizeof(char) * 9); 1434249382dSDoug Rabson outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION); 1444249382dSDoug Rabson for (i = 0; i < 72; i++) { 1454249382dSDoug Rabson bit = inb((pnp_rd_port << 2) | 0x3) == 0x55; 1464249382dSDoug Rabson DELAY(250); /* Delay 250 usec */ 1474249382dSDoug Rabson 1484249382dSDoug Rabson /* Can't Short Circuit the next evaluation, so 'and' is last */ 1494249382dSDoug Rabson bit = (inb((pnp_rd_port << 2) | 0x3) == 0xaa) && bit; 1504249382dSDoug Rabson DELAY(250); /* Delay 250 usec */ 1514249382dSDoug Rabson 1524249382dSDoug Rabson valid = valid || bit; 1534249382dSDoug Rabson if (i < 64) 1544249382dSDoug Rabson sum = (sum >> 1) | 1554249382dSDoug Rabson (((sum ^ (sum >> 1) ^ bit) << 7) & 0xff); 1564249382dSDoug Rabson data[i / 8] = (data[i / 8] >> 1) | (bit ? 0x80 : 0); 1574249382dSDoug Rabson } 1584249382dSDoug Rabson 1594249382dSDoug Rabson valid = valid && (data[8] == sum); 1604249382dSDoug Rabson 1617a83aa63SWarner Losh return (valid); 1624249382dSDoug Rabson } 1634249382dSDoug Rabson 1644249382dSDoug Rabson /* 1654249382dSDoug Rabson * Fill's the buffer with resource info from the device. 1665b45337dSDoug Rabson * Returns the number of characters read. 1674249382dSDoug Rabson */ 1684249382dSDoug Rabson static int 1694249382dSDoug Rabson pnp_get_resource_info(u_char *buffer, int len) 1704249382dSDoug Rabson { 1715b45337dSDoug Rabson int i, j, count; 1724249382dSDoug Rabson u_char temp; 1734249382dSDoug Rabson 1745b45337dSDoug Rabson count = 0; 1754249382dSDoug Rabson for (i = 0; i < len; i++) { 1764249382dSDoug Rabson outb(_PNP_ADDRESS, PNP_STATUS); 1774249382dSDoug Rabson for (j = 0; j < 100; j++) { 1784249382dSDoug Rabson if ((inb((pnp_rd_port << 2) | 0x3)) & 0x1) 1794249382dSDoug Rabson break; 18094203d0aSJohn Baldwin DELAY(10); 1814249382dSDoug Rabson } 1824249382dSDoug Rabson if (j == 100) { 1834249382dSDoug Rabson printf("PnP device failed to report resource data\n"); 1847a83aa63SWarner Losh return (count); 1854249382dSDoug Rabson } 1864249382dSDoug Rabson outb(_PNP_ADDRESS, PNP_RESOURCE_DATA); 1874249382dSDoug Rabson temp = inb((pnp_rd_port << 2) | 0x3); 1884249382dSDoug Rabson if (buffer != NULL) 1894249382dSDoug Rabson buffer[i] = temp; 1905b45337dSDoug Rabson count++; 1914249382dSDoug Rabson } 1927a83aa63SWarner Losh return (count); 1934249382dSDoug Rabson } 1944249382dSDoug Rabson 1954249382dSDoug Rabson /* 1964249382dSDoug Rabson * This function is called after the bus has assigned resource 1974249382dSDoug Rabson * locations for a logical device. 1984249382dSDoug Rabson */ 1994249382dSDoug Rabson static void 2004249382dSDoug Rabson pnp_set_config(void *arg, struct isa_config *config, int enable) 2014249382dSDoug Rabson { 2024249382dSDoug Rabson int csn = ((struct pnp_set_config_arg *) arg)->csn; 2034249382dSDoug Rabson int ldn = ((struct pnp_set_config_arg *) arg)->ldn; 2044249382dSDoug Rabson int i; 2054249382dSDoug Rabson 2064249382dSDoug Rabson /* 2074249382dSDoug Rabson * First put all cards into Sleep state with the initiation 2084249382dSDoug Rabson * key, then put our card into Config state. 2094249382dSDoug Rabson */ 2104249382dSDoug Rabson pnp_send_initiation_key(); 2114249382dSDoug Rabson pnp_write(PNP_WAKE, csn); 2124249382dSDoug Rabson 2134249382dSDoug Rabson /* 2144249382dSDoug Rabson * Select our logical device so that we can program it. 2154249382dSDoug Rabson */ 2164249382dSDoug Rabson pnp_write(PNP_SET_LDN, ldn); 2174249382dSDoug Rabson 2184249382dSDoug Rabson /* 21908764b84SMike Smith * Constrain the number of resources we will try to program 22008764b84SMike Smith */ 22108764b84SMike Smith if (config->ic_nmem > ISA_PNP_NMEM) { 2227a83aa63SWarner Losh printf("too many ISA memory ranges (%d > %d)\n", 2237a83aa63SWarner Losh config->ic_nmem, ISA_PNP_NMEM); 22408764b84SMike Smith config->ic_nmem = ISA_PNP_NMEM; 22508764b84SMike Smith } 22608764b84SMike Smith if (config->ic_nport > ISA_PNP_NPORT) { 2277a83aa63SWarner Losh printf("too many ISA I/O ranges (%d > %d)\n", config->ic_nport, 2287a83aa63SWarner Losh ISA_PNP_NPORT); 22908764b84SMike Smith config->ic_nport = ISA_PNP_NPORT; 23008764b84SMike Smith } 23108764b84SMike Smith if (config->ic_nirq > ISA_PNP_NIRQ) { 2327a83aa63SWarner Losh printf("too many ISA IRQs (%d > %d)\n", config->ic_nirq, 2337a83aa63SWarner Losh ISA_PNP_NIRQ); 23408764b84SMike Smith config->ic_nirq = ISA_PNP_NIRQ; 23508764b84SMike Smith } 23608764b84SMike Smith if (config->ic_ndrq > ISA_PNP_NDRQ) { 2377a83aa63SWarner Losh printf("too many ISA DRQs (%d > %d)\n", config->ic_ndrq, 2387a83aa63SWarner Losh ISA_PNP_NDRQ); 23908764b84SMike Smith config->ic_ndrq = ISA_PNP_NDRQ; 24008764b84SMike Smith } 24108764b84SMike Smith 24208764b84SMike Smith /* 2434249382dSDoug Rabson * Now program the resources. 2444249382dSDoug Rabson */ 2454249382dSDoug Rabson for (i = 0; i < config->ic_nmem; i++) { 2467a83aa63SWarner Losh uint32_t start; 2477a83aa63SWarner Losh uint32_t size; 248c3959391SKazutaka YOKOTA 249c3959391SKazutaka YOKOTA /* XXX: should handle memory control register, 32 bit memory */ 250c3959391SKazutaka YOKOTA if (config->ic_mem[i].ir_size == 0) { 251c3959391SKazutaka YOKOTA pnp_write(PNP_MEM_BASE_HIGH(i), 0); 252c3959391SKazutaka YOKOTA pnp_write(PNP_MEM_BASE_LOW(i), 0); 253c3959391SKazutaka YOKOTA pnp_write(PNP_MEM_RANGE_HIGH(i), 0); 254c3959391SKazutaka YOKOTA pnp_write(PNP_MEM_RANGE_LOW(i), 0); 255c3959391SKazutaka YOKOTA } else { 256c3959391SKazutaka YOKOTA start = config->ic_mem[i].ir_start; 257c3959391SKazutaka YOKOTA size = config->ic_mem[i].ir_size; 2584249382dSDoug Rabson if (start & 0xff) 2594249382dSDoug Rabson panic("pnp_set_config: bogus memory assignment"); 2604249382dSDoug Rabson pnp_write(PNP_MEM_BASE_HIGH(i), (start >> 16) & 0xff); 2614249382dSDoug Rabson pnp_write(PNP_MEM_BASE_LOW(i), (start >> 8) & 0xff); 2624249382dSDoug Rabson pnp_write(PNP_MEM_RANGE_HIGH(i), (size >> 16) & 0xff); 2634249382dSDoug Rabson pnp_write(PNP_MEM_RANGE_LOW(i), (size >> 8) & 0xff); 2644249382dSDoug Rabson } 265c3959391SKazutaka YOKOTA } 26608764b84SMike Smith for (; i < ISA_PNP_NMEM; i++) { 2674249382dSDoug Rabson pnp_write(PNP_MEM_BASE_HIGH(i), 0); 2684249382dSDoug Rabson pnp_write(PNP_MEM_BASE_LOW(i), 0); 2694249382dSDoug Rabson pnp_write(PNP_MEM_RANGE_HIGH(i), 0); 2704249382dSDoug Rabson pnp_write(PNP_MEM_RANGE_LOW(i), 0); 2714249382dSDoug Rabson } 2724249382dSDoug Rabson 2734249382dSDoug Rabson for (i = 0; i < config->ic_nport; i++) { 2747a83aa63SWarner Losh uint32_t start; 275c3959391SKazutaka YOKOTA 276c3959391SKazutaka YOKOTA if (config->ic_port[i].ir_size == 0) { 277c3959391SKazutaka YOKOTA pnp_write(PNP_IO_BASE_HIGH(i), 0); 278c3959391SKazutaka YOKOTA pnp_write(PNP_IO_BASE_LOW(i), 0); 279c3959391SKazutaka YOKOTA } else { 280c3959391SKazutaka YOKOTA start = config->ic_port[i].ir_start; 2814249382dSDoug Rabson pnp_write(PNP_IO_BASE_HIGH(i), (start >> 8) & 0xff); 2824249382dSDoug Rabson pnp_write(PNP_IO_BASE_LOW(i), (start >> 0) & 0xff); 2834249382dSDoug Rabson } 284c3959391SKazutaka YOKOTA } 28508764b84SMike Smith for (; i < ISA_PNP_NPORT; i++) { 2864249382dSDoug Rabson pnp_write(PNP_IO_BASE_HIGH(i), 0); 2874249382dSDoug Rabson pnp_write(PNP_IO_BASE_LOW(i), 0); 2884249382dSDoug Rabson } 2894249382dSDoug Rabson 2904249382dSDoug Rabson for (i = 0; i < config->ic_nirq; i++) { 291c3959391SKazutaka YOKOTA int irq; 292c3959391SKazutaka YOKOTA 293c3959391SKazutaka YOKOTA /* XXX: interrupt type */ 294c3959391SKazutaka YOKOTA if (config->ic_irqmask[i] == 0) { 295c3959391SKazutaka YOKOTA pnp_write(PNP_IRQ_LEVEL(i), 0); 296c3959391SKazutaka YOKOTA pnp_write(PNP_IRQ_TYPE(i), 2); 297c3959391SKazutaka YOKOTA } else { 298c3959391SKazutaka YOKOTA irq = ffs(config->ic_irqmask[i]) - 1; 2994249382dSDoug Rabson pnp_write(PNP_IRQ_LEVEL(i), irq); 3004249382dSDoug Rabson pnp_write(PNP_IRQ_TYPE(i), 2); /* XXX */ 3014249382dSDoug Rabson } 302c3959391SKazutaka YOKOTA } 30308764b84SMike Smith for (; i < ISA_PNP_NIRQ; i++) { 3044249382dSDoug Rabson /* 3054249382dSDoug Rabson * IRQ 0 is not a valid interrupt selection and 3064249382dSDoug Rabson * represents no interrupt selection. 3074249382dSDoug Rabson */ 3084249382dSDoug Rabson pnp_write(PNP_IRQ_LEVEL(i), 0); 309c3959391SKazutaka YOKOTA pnp_write(PNP_IRQ_TYPE(i), 2); 3104249382dSDoug Rabson } 3114249382dSDoug Rabson 3124249382dSDoug Rabson for (i = 0; i < config->ic_ndrq; i++) { 313c3959391SKazutaka YOKOTA int drq; 314c3959391SKazutaka YOKOTA 315c3959391SKazutaka YOKOTA if (config->ic_drqmask[i] == 0) { 316c3959391SKazutaka YOKOTA pnp_write(PNP_DMA_CHANNEL(i), 4); 317c3959391SKazutaka YOKOTA } else { 318c3959391SKazutaka YOKOTA drq = ffs(config->ic_drqmask[i]) - 1; 3194249382dSDoug Rabson pnp_write(PNP_DMA_CHANNEL(i), drq); 3204249382dSDoug Rabson } 321c3959391SKazutaka YOKOTA } 32208764b84SMike Smith for (; i < ISA_PNP_NDRQ; i++) { 3234249382dSDoug Rabson /* 3244249382dSDoug Rabson * DMA channel 4, the cascade channel is used to 3254249382dSDoug Rabson * indicate no DMA channel is active. 3264249382dSDoug Rabson */ 3274249382dSDoug Rabson pnp_write(PNP_DMA_CHANNEL(i), 4); 3284249382dSDoug Rabson } 3294249382dSDoug Rabson 3304249382dSDoug Rabson pnp_write(PNP_ACTIVATE, enable ? 1 : 0); 3314249382dSDoug Rabson 3324249382dSDoug Rabson /* 3334249382dSDoug Rabson * Wake everyone up again, we are finished. 3344249382dSDoug Rabson */ 3354249382dSDoug Rabson pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY); 3364249382dSDoug Rabson } 3374249382dSDoug Rabson 3384249382dSDoug Rabson /* 3394249382dSDoug Rabson * Process quirks for a logical device.. The card must be in Config state. 3404249382dSDoug Rabson */ 341fb0ef528SSeigo Tanimura void 3427a83aa63SWarner Losh pnp_check_quirks(uint32_t vendor_id, uint32_t logical_id, int ldn, 3437a83aa63SWarner Losh struct isa_config *config) 3444249382dSDoug Rabson { 3454249382dSDoug Rabson struct pnp_quirk *qp; 3464249382dSDoug Rabson 3474249382dSDoug Rabson for (qp = &pnp_quirks[0]; qp->vendor_id; qp++) { 3484249382dSDoug Rabson if (qp->vendor_id == vendor_id 3497a83aa63SWarner Losh && (qp->logical_id == 0 || qp->logical_id == logical_id)) { 3504249382dSDoug Rabson switch (qp->type) { 3514249382dSDoug Rabson case PNP_QUIRK_WRITE_REG: 3524249382dSDoug Rabson pnp_write(PNP_SET_LDN, ldn); 3534249382dSDoug Rabson pnp_write(qp->arg1, qp->arg2); 3544249382dSDoug Rabson break; 355fb0ef528SSeigo Tanimura case PNP_QUIRK_EXTRA_IO: 356fb0ef528SSeigo Tanimura if (config == NULL) 357fb0ef528SSeigo Tanimura break; 358fb0ef528SSeigo Tanimura if (qp->arg1 != 0) { 359fb0ef528SSeigo Tanimura config->ic_nport++; 360fb0ef528SSeigo Tanimura config->ic_port[config->ic_nport - 1] = config->ic_port[0]; 361fb0ef528SSeigo Tanimura config->ic_port[config->ic_nport - 1].ir_start += qp->arg1; 362fb0ef528SSeigo Tanimura config->ic_port[config->ic_nport - 1].ir_end += qp->arg1; 363fb0ef528SSeigo Tanimura } 364fb0ef528SSeigo Tanimura if (qp->arg2 != 0) { 365fb0ef528SSeigo Tanimura config->ic_nport++; 366fb0ef528SSeigo Tanimura config->ic_port[config->ic_nport - 1] = config->ic_port[0]; 367fb0ef528SSeigo Tanimura config->ic_port[config->ic_nport - 1].ir_start += qp->arg2; 368fb0ef528SSeigo Tanimura config->ic_port[config->ic_nport - 1].ir_end += qp->arg2; 369fb0ef528SSeigo Tanimura } 370fb0ef528SSeigo Tanimura break; 3714249382dSDoug Rabson } 3724249382dSDoug Rabson } 3734249382dSDoug Rabson } 3744249382dSDoug Rabson } 3754249382dSDoug Rabson 3764249382dSDoug Rabson /* 3774249382dSDoug Rabson * Scan Resource Data for Logical Devices. 3784249382dSDoug Rabson * 3794249382dSDoug Rabson * This function exits as soon as it gets an error reading *ANY* 3805b45337dSDoug Rabson * Resource Data or it reaches the end of Resource Data. In the first 3814249382dSDoug Rabson * case the return value will be TRUE, FALSE otherwise. 3824249382dSDoug Rabson */ 3834249382dSDoug Rabson static int 3845b45337dSDoug Rabson pnp_create_devices(device_t parent, pnp_id *p, int csn, 3855b45337dSDoug Rabson u_char *resources, int len) 3864249382dSDoug Rabson { 387d9ca4bc0SPedro F. Giffuni u_char tag, *resp, *resinfo, *startres = NULL; 3885b45337dSDoug Rabson int large_len, scanning = len, retval = FALSE; 3897a83aa63SWarner Losh uint32_t logical_id; 3904249382dSDoug Rabson device_t dev = 0; 3914249382dSDoug Rabson int ldn = 0; 3924249382dSDoug Rabson struct pnp_set_config_arg *csnldn; 3935b45337dSDoug Rabson char buf[100]; 394d9ca4bc0SPedro F. Giffuni char *desc = NULL; 3954249382dSDoug Rabson 3965b45337dSDoug Rabson resp = resources; 3975b45337dSDoug Rabson while (scanning > 0) { 3985b45337dSDoug Rabson tag = *resp++; 3995b45337dSDoug Rabson scanning--; 4005b45337dSDoug Rabson if (PNP_RES_TYPE(tag) != 0) { 4015b45337dSDoug Rabson /* Large resource */ 4025b45337dSDoug Rabson if (scanning < 2) { 4034249382dSDoug Rabson scanning = 0; 4044249382dSDoug Rabson continue; 4054249382dSDoug Rabson } 4065b45337dSDoug Rabson large_len = resp[0] + (resp[1] << 8); 4075b45337dSDoug Rabson resp += 2; 4085b45337dSDoug Rabson 4095b45337dSDoug Rabson if (scanning < large_len) { 4105b45337dSDoug Rabson scanning = 0; 4115b45337dSDoug Rabson continue; 4125b45337dSDoug Rabson } 4135b45337dSDoug Rabson resinfo = resp; 4145b45337dSDoug Rabson resp += large_len; 4155b45337dSDoug Rabson scanning -= large_len; 4165b45337dSDoug Rabson 4175b45337dSDoug Rabson if (PNP_LRES_NUM(tag) == PNP_TAG_ID_ANSI) { 418d117cb4eSKazutaka YOKOTA if (dev) { 419d117cb4eSKazutaka YOKOTA /* 420d117cb4eSKazutaka YOKOTA * This is an optional device 42155b05458SPedro F. Giffuni * identifier string. Skip it 422d117cb4eSKazutaka YOKOTA * for now. 423d117cb4eSKazutaka YOKOTA */ 424d117cb4eSKazutaka YOKOTA continue; 425d117cb4eSKazutaka YOKOTA } 426d117cb4eSKazutaka YOKOTA /* else mandately card identifier string */ 4275b45337dSDoug Rabson if (large_len > sizeof(buf) - 1) 4285b45337dSDoug Rabson large_len = sizeof(buf) - 1; 4295b45337dSDoug Rabson bcopy(resinfo, buf, large_len); 4305b45337dSDoug Rabson 4315b45337dSDoug Rabson /* 4325b45337dSDoug Rabson * Trim trailing spaces. 4335b45337dSDoug Rabson */ 4345b45337dSDoug Rabson while (buf[large_len-1] == ' ') 4355b45337dSDoug Rabson large_len--; 4365b45337dSDoug Rabson buf[large_len] = '\0'; 4375b45337dSDoug Rabson desc = buf; 4385b45337dSDoug Rabson continue; 4395b45337dSDoug Rabson } 4405b45337dSDoug Rabson 4415b45337dSDoug Rabson continue; 4425b45337dSDoug Rabson } 4435b45337dSDoug Rabson 4445b45337dSDoug Rabson /* Small resource */ 4455b45337dSDoug Rabson if (scanning < PNP_SRES_LEN(tag)) { 4465b45337dSDoug Rabson scanning = 0; 4475b45337dSDoug Rabson continue; 4485b45337dSDoug Rabson } 4495b45337dSDoug Rabson resinfo = resp; 4505b45337dSDoug Rabson resp += PNP_SRES_LEN(tag); 451c2ede4b3SMartin Blapp scanning -= PNP_SRES_LEN(tag); 4524249382dSDoug Rabson 4534249382dSDoug Rabson switch (PNP_SRES_NUM(tag)) { 45448ab255eSPeter Wemm case PNP_TAG_LOGICAL_DEVICE: 4554249382dSDoug Rabson /* 4565b45337dSDoug Rabson * Parse the resources for the previous 4575b45337dSDoug Rabson * logical device (if any). 4585b45337dSDoug Rabson */ 4595b45337dSDoug Rabson if (startres) { 4605b45337dSDoug Rabson pnp_parse_resources(dev, startres, 4617a83aa63SWarner Losh resinfo - startres - 1, ldn); 4625b45337dSDoug Rabson dev = 0; 463d9ca4bc0SPedro F. Giffuni startres = NULL; 4645b45337dSDoug Rabson } 4655b45337dSDoug Rabson 4665b45337dSDoug Rabson /* 4675b45337dSDoug Rabson * A new logical device. Scan for end of 4685b45337dSDoug Rabson * resources. 4694249382dSDoug Rabson */ 4704249382dSDoug Rabson bcopy(resinfo, &logical_id, 4); 471fb0ef528SSeigo Tanimura pnp_check_quirks(p->vendor_id, logical_id, ldn, NULL); 472*a05a6804SWarner Losh dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNP, NULL, DEVICE_UNIT_ANY); 4734249382dSDoug Rabson if (desc) 4744249382dSDoug Rabson device_set_desc_copy(dev, desc); 475d117cb4eSKazutaka YOKOTA else 476d117cb4eSKazutaka YOKOTA device_set_desc_copy(dev, 477d117cb4eSKazutaka YOKOTA pnp_eisaformat(logical_id)); 4784249382dSDoug Rabson isa_set_vendorid(dev, p->vendor_id); 4794249382dSDoug Rabson isa_set_serial(dev, p->serial); 4804249382dSDoug Rabson isa_set_logicalid(dev, logical_id); 481c3959391SKazutaka YOKOTA isa_set_configattr(dev, 4827a83aa63SWarner Losh ISACFGATTR_CANDISABLE | ISACFGATTR_DYNAMIC); 4835b45337dSDoug Rabson csnldn = malloc(sizeof *csnldn, M_DEVBUF, M_NOWAIT); 4844249382dSDoug Rabson if (!csnldn) { 4857a83aa63SWarner Losh device_printf(parent, "out of memory\n"); 4864249382dSDoug Rabson scanning = 0; 4874249382dSDoug Rabson break; 4884249382dSDoug Rabson } 4894249382dSDoug Rabson csnldn->csn = csn; 4904249382dSDoug Rabson csnldn->ldn = ldn; 4917a83aa63SWarner Losh ISA_SET_CONFIG_CALLBACK(parent, dev, pnp_set_config, 4927a83aa63SWarner Losh csnldn); 493132580b5SWarner Losh isa_set_pnp_csn(dev, csn); 494132580b5SWarner Losh isa_set_pnp_ldn(dev, ldn); 4954249382dSDoug Rabson ldn++; 4965b45337dSDoug Rabson startres = resp; 4974249382dSDoug Rabson break; 4984249382dSDoug Rabson 4994249382dSDoug Rabson case PNP_TAG_END: 5005b45337dSDoug Rabson if (!startres) { 5017a83aa63SWarner Losh device_printf(parent, "malformed resources\n"); 5025b45337dSDoug Rabson scanning = 0; 5035b45337dSDoug Rabson break; 5045b45337dSDoug Rabson } 5055b45337dSDoug Rabson pnp_parse_resources(dev, startres, 506c3959391SKazutaka YOKOTA resinfo - startres - 1, ldn); 5075b45337dSDoug Rabson dev = 0; 508d9ca4bc0SPedro F. Giffuni startres = NULL; 5094249382dSDoug Rabson scanning = 0; 5104249382dSDoug Rabson break; 5114249382dSDoug Rabson 5124249382dSDoug Rabson default: 5134249382dSDoug Rabson /* Skip this resource */ 5144249382dSDoug Rabson break; 5154249382dSDoug Rabson } 5164249382dSDoug Rabson } 5174249382dSDoug Rabson 5187a83aa63SWarner Losh return (retval); 5194249382dSDoug Rabson } 5204249382dSDoug Rabson 5214249382dSDoug Rabson /* 5225b45337dSDoug Rabson * Read 'amount' bytes of resources from the card, allocating memory 5235b45337dSDoug Rabson * as needed. If a buffer is already available, it should be passed in 5245b45337dSDoug Rabson * '*resourcesp' and its length in '*spacep'. The number of resource 5255b45337dSDoug Rabson * bytes already in the buffer should be passed in '*lenp'. The memory 5265b45337dSDoug Rabson * allocated will be returned in '*resourcesp' with its size and the 5275b45337dSDoug Rabson * number of bytes of resources in '*spacep' and '*lenp' respectively. 5288b7f9bdcSPoul-Henning Kamp * 5298b7f9bdcSPoul-Henning Kamp * XXX: Multiple problems here, we forget to free() stuff in one 5308b7f9bdcSPoul-Henning Kamp * XXX: error return, and in another case we free (*resourcesp) but 5318b7f9bdcSPoul-Henning Kamp * XXX: don't tell the caller. 5325b45337dSDoug Rabson */ 5335b45337dSDoug Rabson static int 5345b45337dSDoug Rabson pnp_read_bytes(int amount, u_char **resourcesp, int *spacep, int *lenp) 5355b45337dSDoug Rabson { 5365b45337dSDoug Rabson u_char *resources = *resourcesp; 5375b45337dSDoug Rabson u_char *newres; 5385b45337dSDoug Rabson int space = *spacep; 5395b45337dSDoug Rabson int len = *lenp; 5405b45337dSDoug Rabson 5415b45337dSDoug Rabson if (space == 0) { 5425b45337dSDoug Rabson space = 1024; 5435b45337dSDoug Rabson resources = malloc(space, M_TEMP, M_NOWAIT); 5445b45337dSDoug Rabson if (!resources) 5457a83aa63SWarner Losh return (ENOMEM); 5465b45337dSDoug Rabson } 5475b45337dSDoug Rabson 5485b45337dSDoug Rabson if (len + amount > space) { 5495b45337dSDoug Rabson int extra = 1024; 5505b45337dSDoug Rabson while (len + amount > space + extra) 5515b45337dSDoug Rabson extra += 1024; 5525b45337dSDoug Rabson newres = malloc(space + extra, M_TEMP, M_NOWAIT); 55330f5ad0fSPoul-Henning Kamp if (!newres) { 55430f5ad0fSPoul-Henning Kamp /* XXX: free resources */ 5557a83aa63SWarner Losh return (ENOMEM); 55630f5ad0fSPoul-Henning Kamp } 5575b45337dSDoug Rabson bcopy(resources, newres, len); 5585b45337dSDoug Rabson free(resources, M_TEMP); 5595b45337dSDoug Rabson resources = newres; 5605b45337dSDoug Rabson space += extra; 5615b45337dSDoug Rabson } 5625b45337dSDoug Rabson 5635b45337dSDoug Rabson if (pnp_get_resource_info(resources + len, amount) != amount) 5647a83aa63SWarner Losh return (EINVAL); 5655b45337dSDoug Rabson len += amount; 5665b45337dSDoug Rabson 5675b45337dSDoug Rabson *resourcesp = resources; 5685b45337dSDoug Rabson *spacep = space; 5695b45337dSDoug Rabson *lenp = len; 5705b45337dSDoug Rabson 5717a83aa63SWarner Losh return (0); 5725b45337dSDoug Rabson } 5735b45337dSDoug Rabson 5745b45337dSDoug Rabson /* 5755b45337dSDoug Rabson * Read all resources from the card, allocating memory as needed. If a 5765b45337dSDoug Rabson * buffer is already available, it should be passed in '*resourcesp' 5775b45337dSDoug Rabson * and its length in '*spacep'. The memory allocated will be returned 5785b45337dSDoug Rabson * in '*resourcesp' with its size and the number of bytes of resources 5795b45337dSDoug Rabson * in '*spacep' and '*lenp' respectively. 5805b45337dSDoug Rabson */ 5815b45337dSDoug Rabson static int 5825b45337dSDoug Rabson pnp_read_resources(u_char **resourcesp, int *spacep, int *lenp) 5835b45337dSDoug Rabson { 5845b45337dSDoug Rabson u_char *resources = *resourcesp; 5855b45337dSDoug Rabson int space = *spacep; 5865b45337dSDoug Rabson int len = 0; 5875b45337dSDoug Rabson int error, done; 5885b45337dSDoug Rabson u_char tag; 5895b45337dSDoug Rabson 5905b45337dSDoug Rabson error = 0; 5915b45337dSDoug Rabson done = 0; 5925b45337dSDoug Rabson while (!done) { 5935b45337dSDoug Rabson error = pnp_read_bytes(1, &resources, &space, &len); 5945b45337dSDoug Rabson if (error) 5955b45337dSDoug Rabson goto out; 5965b45337dSDoug Rabson tag = resources[len-1]; 5975b45337dSDoug Rabson if (PNP_RES_TYPE(tag) == 0) { 5985b45337dSDoug Rabson /* 5995b45337dSDoug Rabson * Small resource, read contents. 6005b45337dSDoug Rabson */ 6015b45337dSDoug Rabson error = pnp_read_bytes(PNP_SRES_LEN(tag), 6025b45337dSDoug Rabson &resources, &space, &len); 6035b45337dSDoug Rabson if (error) 6045b45337dSDoug Rabson goto out; 6055b45337dSDoug Rabson if (PNP_SRES_NUM(tag) == PNP_TAG_END) 6065b45337dSDoug Rabson done = 1; 6075b45337dSDoug Rabson } else { 6085b45337dSDoug Rabson /* 6095b45337dSDoug Rabson * Large resource, read length and contents. 6105b45337dSDoug Rabson */ 6115b45337dSDoug Rabson error = pnp_read_bytes(2, &resources, &space, &len); 6125b45337dSDoug Rabson if (error) 6135b45337dSDoug Rabson goto out; 6145b45337dSDoug Rabson error = pnp_read_bytes(resources[len-2] 6157a83aa63SWarner Losh + (resources[len-1] << 8), &resources, &space, 6167a83aa63SWarner Losh &len); 6175b45337dSDoug Rabson if (error) 6185b45337dSDoug Rabson goto out; 6195b45337dSDoug Rabson } 6205b45337dSDoug Rabson } 6215b45337dSDoug Rabson 6225b45337dSDoug Rabson out: 6235b45337dSDoug Rabson *resourcesp = resources; 6245b45337dSDoug Rabson *spacep = space; 6255b45337dSDoug Rabson *lenp = len; 6267a83aa63SWarner Losh return (error); 6275b45337dSDoug Rabson } 6285b45337dSDoug Rabson 6295b45337dSDoug Rabson /* 6304249382dSDoug Rabson * Run the isolation protocol. Use pnp_rd_port as the READ_DATA port 6314249382dSDoug Rabson * value (caller should try multiple READ_DATA locations before giving 6324249382dSDoug Rabson * up). Upon exiting, all cards are aware that they should use 6334249382dSDoug Rabson * pnp_rd_port as the READ_DATA port. 6344249382dSDoug Rabson * 6354249382dSDoug Rabson * In the first pass, a csn is assigned to each board and pnp_id's 6364249382dSDoug Rabson * are saved to an array, pnp_devices. In the second pass, each 6374249382dSDoug Rabson * card is woken up and the device configuration is called. 6384249382dSDoug Rabson */ 6394249382dSDoug Rabson static int 6404249382dSDoug Rabson pnp_isolation_protocol(device_t parent) 6414249382dSDoug Rabson { 6424249382dSDoug Rabson int csn; 6434249382dSDoug Rabson pnp_id id; 6445b45337dSDoug Rabson int found = 0, len; 645d9ca4bc0SPedro F. Giffuni u_char *resources = NULL; 6465b45337dSDoug Rabson int space = 0; 6475b45337dSDoug Rabson int error; 6484249382dSDoug Rabson 6494249382dSDoug Rabson /* 6504249382dSDoug Rabson * Put all cards into the Sleep state so that we can clear 6514249382dSDoug Rabson * their CSNs. 6524249382dSDoug Rabson */ 6534249382dSDoug Rabson pnp_send_initiation_key(); 6544249382dSDoug Rabson 6554249382dSDoug Rabson /* 6564249382dSDoug Rabson * Clear the CSN for all cards. 6574249382dSDoug Rabson */ 6584249382dSDoug Rabson pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_RESET_CSN); 6594249382dSDoug Rabson 6604249382dSDoug Rabson /* 6614249382dSDoug Rabson * Move all cards to the Isolation state. 6624249382dSDoug Rabson */ 6634249382dSDoug Rabson pnp_write(PNP_WAKE, 0); 6644249382dSDoug Rabson 6654249382dSDoug Rabson /* 6664249382dSDoug Rabson * Tell them where the read point is going to be this time. 6674249382dSDoug Rabson */ 6684249382dSDoug Rabson pnp_write(PNP_SET_RD_DATA, pnp_rd_port); 6694249382dSDoug Rabson 6704249382dSDoug Rabson for (csn = 1; csn < PNP_MAX_CARDS; csn++) { 6714249382dSDoug Rabson /* 6724249382dSDoug Rabson * Start the serial isolation protocol. 6734249382dSDoug Rabson */ 6744249382dSDoug Rabson outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION); 6754249382dSDoug Rabson DELAY(1000); /* Delay 1 msec */ 6764249382dSDoug Rabson 6774249382dSDoug Rabson if (pnp_get_serial(&id)) { 6784249382dSDoug Rabson /* 6794249382dSDoug Rabson * We have read the id from a card 6804249382dSDoug Rabson * successfully. The card which won the 6814249382dSDoug Rabson * isolation protocol will be in Isolation 6825b45337dSDoug Rabson * mode and all others will be in Sleep. 6834249382dSDoug Rabson * Program the CSN of the isolated card 6844249382dSDoug Rabson * (taking it to Config state) and read its 6854249382dSDoug Rabson * resources, creating devices as we find 6864249382dSDoug Rabson * logical devices on the card. 6874249382dSDoug Rabson */ 6884249382dSDoug Rabson pnp_write(PNP_SET_CSN, csn); 6897698537bSYoshihiro Takahashi if (bootverbose) 6907698537bSYoshihiro Takahashi printf("Reading PnP configuration for %s.\n", 6917698537bSYoshihiro Takahashi pnp_eisaformat(id.vendor_id)); 6927a83aa63SWarner Losh error = pnp_read_resources(&resources, &space, &len); 6935b45337dSDoug Rabson if (error) 6945b45337dSDoug Rabson break; 6957a83aa63SWarner Losh pnp_create_devices(parent, &id, csn, resources, len); 6964249382dSDoug Rabson found++; 6974249382dSDoug Rabson } else 6984249382dSDoug Rabson break; 6994249382dSDoug Rabson 7004249382dSDoug Rabson /* 7014249382dSDoug Rabson * Put this card back to the Sleep state and 7024249382dSDoug Rabson * simultaneously move all cards which don't have a 7034249382dSDoug Rabson * CSN yet to Isolation state. 7044249382dSDoug Rabson */ 7054249382dSDoug Rabson pnp_write(PNP_WAKE, 0); 7064249382dSDoug Rabson } 7074249382dSDoug Rabson 7084249382dSDoug Rabson /* 7094249382dSDoug Rabson * Unless we have chosen the wrong read port, all cards will 7104249382dSDoug Rabson * be in Sleep state. Put them back into WaitForKey for 7114249382dSDoug Rabson * now. Their resources will be programmed later. 7124249382dSDoug Rabson */ 7134249382dSDoug Rabson pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY); 7144249382dSDoug Rabson 7155b45337dSDoug Rabson /* 7165b45337dSDoug Rabson * Cleanup. 7175b45337dSDoug Rabson */ 7185b45337dSDoug Rabson if (resources) 7195b45337dSDoug Rabson free(resources, M_TEMP); 7205b45337dSDoug Rabson 7217a83aa63SWarner Losh return (found); 7224249382dSDoug Rabson } 7234249382dSDoug Rabson 7244249382dSDoug Rabson 7254249382dSDoug Rabson /* 7264249382dSDoug Rabson * pnp_identify() 7274249382dSDoug Rabson * 7284249382dSDoug Rabson * autoconfiguration of pnp devices. This routine just runs the 7294249382dSDoug Rabson * isolation protocol over several ports, until one is successful. 7304249382dSDoug Rabson * 7314249382dSDoug Rabson * may be called more than once ? 7324249382dSDoug Rabson * 7334249382dSDoug Rabson */ 7344249382dSDoug Rabson 7354249382dSDoug Rabson static void 7364249382dSDoug Rabson pnp_identify(driver_t *driver, device_t parent) 7374249382dSDoug Rabson { 7384249382dSDoug Rabson int num_pnp_devs; 7394249382dSDoug Rabson 7404249382dSDoug Rabson /* Try various READ_DATA ports from 0x203-0x3ff */ 7414249382dSDoug Rabson for (pnp_rd_port = 0x80; (pnp_rd_port < 0xff); pnp_rd_port += 0x10) { 7424249382dSDoug Rabson if (bootverbose) 743934bd5edSWarner Losh printf("pnp_identify: Trying Read_Port at %x\n", 7447a83aa63SWarner Losh (pnp_rd_port << 2) | 0x3); 7454249382dSDoug Rabson 7464249382dSDoug Rabson num_pnp_devs = pnp_isolation_protocol(parent); 7474249382dSDoug Rabson if (num_pnp_devs) 7484249382dSDoug Rabson break; 7494249382dSDoug Rabson } 7502e63992fSWarner Losh if (bootverbose) 7512e63992fSWarner Losh printf("PNP Identify complete\n"); 7524249382dSDoug Rabson } 7534249382dSDoug Rabson 7544249382dSDoug Rabson static device_method_t pnp_methods[] = { 7554249382dSDoug Rabson /* Device interface */ 7564249382dSDoug Rabson DEVMETHOD(device_identify, pnp_identify), 7574249382dSDoug Rabson 7584249382dSDoug Rabson { 0, 0 } 7594249382dSDoug Rabson }; 7604249382dSDoug Rabson 7614249382dSDoug Rabson static driver_t pnp_driver = { 7624249382dSDoug Rabson "pnp", 7634249382dSDoug Rabson pnp_methods, 7644249382dSDoug Rabson 1, /* no softc */ 7654249382dSDoug Rabson }; 7664249382dSDoug Rabson 767be275086SJohn Baldwin DRIVER_MODULE(pnp, isa, pnp_driver, 0, 0); 768