adv_cardbus.c revision 1.3
11.3Syamt/* $NetBSD: adv_cardbus.c,v 1.3 2001/09/29 01:53:23 yamt Exp $ */ 21.1Sthorpej 31.1Sthorpej/*- 41.1Sthorpej * Copyright (c) 2000 The NetBSD Foundation, Inc. 51.1Sthorpej * All rights reserved. 61.1Sthorpej * 71.1Sthorpej * This code is derived from software contributed to The NetBSD Foundation 81.1Sthorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 91.1Sthorpej * NASA Ames Research Center. 101.1Sthorpej * 111.1Sthorpej * Redistribution and use in source and binary forms, with or without 121.1Sthorpej * modification, are permitted provided that the following conditions 131.1Sthorpej * are met: 141.1Sthorpej * 1. Redistributions of source code must retain the above copyright 151.1Sthorpej * notice, this list of conditions and the following disclaimer. 161.1Sthorpej * 2. Redistributions in binary form must reproduce the above copyright 171.1Sthorpej * notice, this list of conditions and the following disclaimer in the 181.1Sthorpej * documentation and/or other materials provided with the distribution. 191.1Sthorpej * 3. All advertising materials mentioning features or use of this software 201.1Sthorpej * must display the following acknowledgement: 211.1Sthorpej * This product includes software developed by the NetBSD 221.1Sthorpej * Foundation, Inc. and its contributors. 231.1Sthorpej * 4. Neither the name of The NetBSD Foundation nor the names of its 241.1Sthorpej * contributors may be used to endorse or promote products derived 251.1Sthorpej * from this software without specific prior written permission. 261.1Sthorpej * 271.1Sthorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 281.1Sthorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 291.1Sthorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 301.1Sthorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 311.1Sthorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 321.1Sthorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 331.1Sthorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 341.1Sthorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 351.1Sthorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 361.1Sthorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 371.1Sthorpej * POSSIBILITY OF SUCH DAMAGE. 381.1Sthorpej */ 391.1Sthorpej 401.1Sthorpej/* 411.1Sthorpej * this file was brought from ahc_cardbus.c and adv_pci.c 421.1Sthorpej * and modified by YAMAMOTO Takashi. 431.1Sthorpej */ 441.1Sthorpej 451.1Sthorpej#include <sys/param.h> 461.1Sthorpej#include <sys/systm.h> 471.1Sthorpej#include <sys/malloc.h> 481.1Sthorpej#include <sys/kernel.h> 491.1Sthorpej#include <sys/queue.h> 501.1Sthorpej#include <sys/device.h> 511.1Sthorpej 521.1Sthorpej#include <machine/bus.h> 531.1Sthorpej#include <machine/intr.h> 541.1Sthorpej 551.1Sthorpej#include <dev/scsipi/scsi_all.h> 561.1Sthorpej#include <dev/scsipi/scsipi_all.h> 571.1Sthorpej#include <dev/scsipi/scsiconf.h> 581.1Sthorpej 591.1Sthorpej#include <dev/pci/pcireg.h> 601.1Sthorpej#include <dev/pci/pcidevs.h> 611.1Sthorpej 621.1Sthorpej#include <dev/cardbus/cardbusvar.h> 631.1Sthorpej#include <dev/cardbus/cardbusdevs.h> 641.1Sthorpej 651.1Sthorpej#include <dev/ic/advlib.h> 661.1Sthorpej#include <dev/ic/adv.h> 671.1Sthorpej 681.1Sthorpej#define ADV_CARDBUS_IOBA CARDBUS_BASE0_REG 691.1Sthorpej#define ADV_CARDBUS_MMBA CARDBUS_BASE1_REG 701.1Sthorpej 711.3Syamt#define ADV_CARDBUS_DEBUG 721.3Syamt#define ADV_CARDBUS_ALLOW_MEMIO 731.3Syamt 741.1Sthorpej#define DEVNAME(sc) sc->sc_dev.dv_xname 751.1Sthorpej 761.1Sthorpejstruct adv_cardbus_softc { 771.1Sthorpej struct asc_softc sc_adv; /* real ADV */ 781.1Sthorpej 791.1Sthorpej /* CardBus-specific goo. */ 801.1Sthorpej cardbus_devfunc_t sc_ct; /* our CardBus devfuncs */ 811.1Sthorpej int sc_intrline; /* our interrupt line */ 821.1Sthorpej cardbustag_t sc_tag; 831.1Sthorpej 841.1Sthorpej int sc_cbenable; /* what CardBus access type to enable */ 851.1Sthorpej int sc_csr; /* CSR bits */ 861.1Sthorpej bus_size_t sc_size; 871.1Sthorpej}; 881.1Sthorpej 891.1Sthorpejint adv_cardbus_match __P((struct device *, struct cfdata *, void *)); 901.1Sthorpejvoid adv_cardbus_attach __P((struct device *, struct device *, void *)); 911.1Sthorpejint adv_cardbus_detach __P((struct device *, int)); 921.1Sthorpej 931.1Sthorpejstruct cfattach adv_cardbus_ca = { 941.3Syamt sizeof(struct adv_cardbus_softc), adv_cardbus_match, adv_cardbus_attach, 951.1Sthorpej adv_cardbus_detach 961.1Sthorpej}; 971.1Sthorpej 981.1Sthorpejint 991.1Sthorpejadv_cardbus_match(parent, match, aux) 1001.1Sthorpej struct device *parent; 1011.1Sthorpej struct cfdata *match; 1021.1Sthorpej void *aux; 1031.1Sthorpej{ 1041.1Sthorpej struct cardbus_attach_args *ca = aux; 1051.1Sthorpej 1061.1Sthorpej if (CARDBUS_VENDOR(ca->ca_id) == CARDBUS_VENDOR_ADVSYS && 1071.1Sthorpej CARDBUS_PRODUCT(ca->ca_id) == CARDBUS_PRODUCT_ADVSYS_ULTRA) 1081.1Sthorpej return (1); 1091.1Sthorpej 1101.1Sthorpej return (0); 1111.1Sthorpej} 1121.1Sthorpej 1131.1Sthorpejvoid 1141.1Sthorpejadv_cardbus_attach(parent, self, aux) 1151.1Sthorpej struct device *parent, *self; 1161.1Sthorpej void *aux; 1171.1Sthorpej{ 1181.1Sthorpej struct cardbus_attach_args *ca = aux; 1191.1Sthorpej struct adv_cardbus_softc *csc = (void *) self; 1201.1Sthorpej struct asc_softc *sc = &csc->sc_adv; 1211.1Sthorpej cardbus_devfunc_t ct = ca->ca_ct; 1221.1Sthorpej cardbus_chipset_tag_t cc = ct->ct_cc; 1231.1Sthorpej cardbus_function_tag_t cf = ct->ct_cf; 1241.1Sthorpej bus_space_tag_t iot; 1251.1Sthorpej bus_space_handle_t ioh; 1261.1Sthorpej pcireg_t reg; 1271.1Sthorpej u_int8_t latency = 0x20; 1281.1Sthorpej 1291.1Sthorpej sc->sc_flags = 0; 1301.1Sthorpej 1311.1Sthorpej if (PCI_VENDOR(ca->ca_id) == PCI_VENDOR_ADVSYS) { 1321.1Sthorpej switch (PCI_PRODUCT(ca->ca_id)) { 1331.1Sthorpej case PCI_PRODUCT_ADVSYS_1200A: 1341.1Sthorpej printf(": AdvanSys ASC1200A SCSI adapter\n"); 1351.1Sthorpej latency = 0; 1361.1Sthorpej break; 1371.1Sthorpej 1381.1Sthorpej case PCI_PRODUCT_ADVSYS_1200B: 1391.1Sthorpej printf(": AdvanSys ASC1200B SCSI adapter\n"); 1401.1Sthorpej latency = 0; 1411.1Sthorpej break; 1421.1Sthorpej 1431.1Sthorpej case PCI_PRODUCT_ADVSYS_ULTRA: 1441.1Sthorpej switch (PCI_REVISION(ca->ca_class)) { 1451.1Sthorpej case ASC_PCI_REVISION_3050: 1461.1Sthorpej printf(": AdvanSys ABP-9xxUA SCSI adapter\n"); 1471.1Sthorpej break; 1481.1Sthorpej 1491.1Sthorpej case ASC_PCI_REVISION_3150: 1501.1Sthorpej printf(": AdvanSys ABP-9xxU SCSI adapter\n"); 1511.1Sthorpej break; 1521.1Sthorpej } 1531.1Sthorpej break; 1541.1Sthorpej 1551.1Sthorpej default: 1561.1Sthorpej printf(": unknown model!\n"); 1571.1Sthorpej return; 1581.1Sthorpej } 1591.1Sthorpej } 1601.1Sthorpej 1611.1Sthorpej csc->sc_ct = ct; 1621.1Sthorpej csc->sc_tag = ca->ca_tag; 1631.1Sthorpej csc->sc_intrline = ca->ca_intrline; 1641.1Sthorpej csc->sc_cbenable = 0; 1651.1Sthorpej 1661.1Sthorpej /* 1671.1Sthorpej * Map the device. 1681.1Sthorpej */ 1691.1Sthorpej csc->sc_csr = PCI_COMMAND_MASTER_ENABLE; 1701.1Sthorpej 1711.3Syamt#ifdef ADV_CARDBUS_ALLOW_MEMIO 1721.1Sthorpej if (Cardbus_mapreg_map(csc->sc_ct, ADV_CARDBUS_MMBA, 1731.1Sthorpej PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0, 1741.1Sthorpej &iot, &ioh, NULL, &csc->sc_size) == 0) { 1751.3Syamt#ifdef ADV_CARDBUS_DEBUG 1761.3Syamt printf("%s: memio enabled\n", DEVNAME(sc)); 1771.3Syamt#endif 1781.1Sthorpej csc->sc_cbenable = CARDBUS_MEM_ENABLE; 1791.1Sthorpej csc->sc_csr |= PCI_COMMAND_MEM_ENABLE; 1801.1Sthorpej } else 1811.1Sthorpej#endif 1821.1Sthorpej if (Cardbus_mapreg_map(csc->sc_ct, ADV_CARDBUS_IOBA, 1831.1Sthorpej PCI_MAPREG_TYPE_IO, 0, &iot, &ioh, NULL, &csc->sc_size) == 0) { 1841.3Syamt#ifdef ADV_CARDBUS_DEBUG 1851.1Sthorpej printf("%s: io enabled\n", DEVNAME(sc)); 1861.3Syamt#endif 1871.1Sthorpej csc->sc_cbenable = CARDBUS_IO_ENABLE; 1881.1Sthorpej csc->sc_csr |= PCI_COMMAND_IO_ENABLE; 1891.1Sthorpej } else { 1901.1Sthorpej printf("%s: unable to map device registers\n", 1911.1Sthorpej DEVNAME(sc)); 1921.1Sthorpej return; 1931.1Sthorpej } 1941.1Sthorpej 1951.3Syamt printf("tag = %u\n", iot); 1961.3Syamt 1971.1Sthorpej /* Make sure the right access type is on the CardBus bridge. */ 1981.1Sthorpej (*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cbenable); 1991.1Sthorpej (*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE); 2001.1Sthorpej 2011.1Sthorpej /* Enable the appropriate bits in the PCI CSR. */ 2021.1Sthorpej reg = cardbus_conf_read(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG); 2031.1Sthorpej reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE); 2041.1Sthorpej reg |= csc->sc_csr; 2051.1Sthorpej cardbus_conf_write(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG, reg); 2061.1Sthorpej 2071.1Sthorpej /* 2081.1Sthorpej * Make sure the latency timer is set to some reasonable 2091.1Sthorpej * value. 2101.1Sthorpej */ 2111.1Sthorpej reg = cardbus_conf_read(cc, cf, ca->ca_tag, PCI_BHLC_REG); 2121.1Sthorpej if (PCI_LATTIMER(reg) < latency) { 2131.1Sthorpej reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT); 2141.1Sthorpej reg |= (latency << PCI_LATTIMER_SHIFT); 2151.1Sthorpej cardbus_conf_write(cc, cf, ca->ca_tag, PCI_BHLC_REG, reg); 2161.1Sthorpej } 2171.1Sthorpej 2181.1Sthorpej ASC_SET_CHIP_CONTROL(iot, ioh, ASC_CC_HALT); 2191.1Sthorpej ASC_SET_CHIP_STATUS(iot, ioh, 0); 2201.1Sthorpej 2211.1Sthorpej sc->sc_iot = iot; 2221.1Sthorpej sc->sc_ioh = ioh; 2231.1Sthorpej sc->sc_dmat = ca->ca_dmat; 2241.1Sthorpej sc->pci_device_id = ca->ca_id; 2251.1Sthorpej sc->bus_type = ASC_IS_PCI; 2261.1Sthorpej sc->chip_version = ASC_GET_CHIP_VER_NO(iot, ioh); 2271.3Syamt 2281.1Sthorpej /* 2291.2Swiz * Initialize the board 2301.1Sthorpej */ 2311.1Sthorpej if (adv_init(sc)) { 2321.1Sthorpej printf("adv_init failed\n"); 2331.1Sthorpej return; 2341.1Sthorpej } 2351.1Sthorpej 2361.1Sthorpej /* 2371.1Sthorpej * Establish the interrupt. 2381.1Sthorpej */ 2391.1Sthorpej sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline, IPL_BIO, 2401.1Sthorpej adv_intr, sc); 2411.1Sthorpej if (sc->sc_ih == NULL) { 2421.1Sthorpej printf("%s: unable to establish interrupt at %d\n", 2431.1Sthorpej DEVNAME(sc), ca->ca_intrline); 2441.1Sthorpej return; 2451.1Sthorpej } 2461.1Sthorpej printf("%s: interrupting at %d\n", DEVNAME(sc), ca->ca_intrline); 2471.1Sthorpej 2481.1Sthorpej /* 2491.1Sthorpej * Attach. 2501.1Sthorpej */ 2511.1Sthorpej adv_attach(sc); 2521.1Sthorpej} 2531.1Sthorpej 2541.1Sthorpejint 2551.1Sthorpejadv_cardbus_detach(self, flags) 2561.1Sthorpej struct device *self; 2571.1Sthorpej int flags; 2581.1Sthorpej{ 2591.1Sthorpej struct adv_cardbus_softc *csc = (void*)self; 2601.1Sthorpej struct asc_softc *sc = &csc->sc_adv; 2611.1Sthorpej 2621.1Sthorpej int rv; 2631.1Sthorpej 2641.1Sthorpej rv = adv_detach(sc, flags); 2651.1Sthorpej if (rv) 2661.1Sthorpej return rv; 2671.1Sthorpej 2681.1Sthorpej if (sc->sc_ih) { 2691.1Sthorpej cardbus_intr_disestablish(csc->sc_ct->ct_cc, 2701.1Sthorpej csc->sc_ct->ct_cf, sc->sc_ih); 2711.1Sthorpej sc->sc_ih = 0; 2721.1Sthorpej } 2731.1Sthorpej 2741.1Sthorpej if (csc->sc_cbenable) { 2751.3Syamt#ifdef ADV_CARDBUS_ALLOW_MEMIO 2761.1Sthorpej if (csc->sc_cbenable == CARDBUS_MEM_ENABLE) { 2771.1Sthorpej Cardbus_mapreg_unmap(csc->sc_ct, ADV_CARDBUS_MMBA, 2781.1Sthorpej sc->sc_iot, sc->sc_ioh, csc->sc_size); 2791.1Sthorpej } else { 2801.1Sthorpej#endif 2811.1Sthorpej Cardbus_mapreg_unmap(csc->sc_ct, ADV_CARDBUS_IOBA, 2821.1Sthorpej sc->sc_iot, sc->sc_ioh, csc->sc_size); 2831.3Syamt#ifdef ADV_CARDBUS_ALLOW_MEMIO 2841.1Sthorpej } 2851.1Sthorpej#endif 2861.1Sthorpej csc->sc_cbenable = 0; 2871.1Sthorpej } 2881.1Sthorpej 2891.1Sthorpej return 0; 2901.1Sthorpej} 291