ncr53c500_pccard.c revision 107362
167468Snon/* $FreeBSD: head/sys/dev/ncv/ncr53c500_pccard.c 107362 2002-11-28 01:13:58Z non $ */ 267468Snon/* $NecBSD: ncr53c500_pisa.c,v 1.28 1998/11/26 01:59:11 honda Exp $ */ 367468Snon/* $NetBSD$ */ 467468Snon 567468Snon/* 667468Snon * [Ported for FreeBSD] 767468Snon * Copyright (c) 2000 867468Snon * Noriaki Mitsunaga, Mitsuru Iwasaki and Takanori Watanabe. 967468Snon * All rights reserved. 1067468Snon * [NetBSD for NEC PC-98 series] 1167468Snon * Copyright (c) 1995, 1996, 1997, 1998 1267468Snon * NetBSD/pc98 porting staff. All rights reserved. 1367468Snon * Copyright (c) 1995, 1996, 1997, 1998 1467468Snon * Naofumi HONDA. All rights reserved. 1567468Snon * 1667468Snon * Redistribution and use in source and binary forms, with or without 1767468Snon * modification, are permitted provided that the following conditions 1867468Snon * are met: 1967468Snon * 1. Redistributions of source code must retain the above copyright 2067468Snon * notice, this list of conditions and the following disclaimer. 2167468Snon * 2. Redistributions in binary form must reproduce the above copyright 2267468Snon * notice, this list of conditions and the following disclaimer in the 2367468Snon * documentation and/or other materials provided with the distribution. 2467468Snon * 3. The name of the author may not be used to endorse or promote products 2567468Snon * derived from this software without specific prior written permission. 2667468Snon * 2767468Snon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2867468Snon * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 2967468Snon * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 3067468Snon * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 3167468Snon * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 3267468Snon * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 3367468Snon * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3467468Snon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 3567468Snon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 3667468Snon * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3767468Snon * POSSIBILITY OF SUCH DAMAGE. 3867468Snon */ 3967468Snon 4067468Snon#include <sys/param.h> 4167468Snon#include <sys/systm.h> 4267468Snon#include <sys/malloc.h> 4367468Snon#include <sys/errno.h> 4467468Snon 4567468Snon#include <machine/bus.h> 4667468Snon#include <machine/bus_pio.h> 4767468Snon#include <machine/dvcfg.h> 4867468Snon 4967468Snon#include <sys/device_port.h> 5067468Snon 51107362Snon#include <dev/pccard/pccarddevs.h> 52107362Snon#include <dev/pccard/pccardvar.h> 53107362Snon 5467468Snon#include <cam/scsi/scsi_low.h> 5567468Snon#include <cam/scsi/scsi_low_pisa.h> 5667468Snon 5767468Snon#include <dev/ncv/ncr53c500reg.h> 5867468Snon#include <dev/ncv/ncr53c500hw.h> 5967468Snon#include <dev/ncv/ncr53c500var.h> 6067468Snon 6167848Snon#define KME_KXLC004_01 0x100 6267468Snon#define OFFSET_KME_KXLC004_01 0x10 6367468Snon 6467468Snon#include <sys/kernel.h> 6567468Snon#include <sys/module.h> 6670834Swollman#if !defined(__FreeBSD__) || __FreeBSD_version < 500014 6767468Snon#include <sys/select.h> 6870834Swollman#endif 6967468Snon#include <pccard/cardinfo.h> 7067468Snon#include <pccard/slot.h> 7167468Snon 7267468Snonstatic int ncvprobe(DEVPORT_PDEVICE devi); 7367468Snonstatic int ncvattach(DEVPORT_PDEVICE devi); 7467468Snon 7592739Salfredstatic void ncv_card_unload(DEVPORT_PDEVICE); 7667468Snon 77107362Snonstatic const struct ncv_product { 78107362Snon struct pccard_product prod; 79107362Snon int flags; 80107362Snon} ncv_products[] = { 81107362Snon { PCMCIA_CARD(EPSON, SC200, 0), 0}, 82107362Snon { PCMCIA_CARD(PANASONIC, KXLC002, 0), 0xb4d00000 }, 83107362Snon { PCMCIA_CARD(PANASONIC, KXLC004, 0), 0xb4d00100 }, 84107362Snon { PCMCIA_CARD(MACNICA, MPS100, 0), 0xb6250000 }, 85107362Snon { PCMCIA_CARD(MACNICA, MPS110, 0), 0 }, 86107362Snon { PCMCIA_CARD(NEC, PC9801N_J03R, 0), 0 }, 87107362Snon { PCMCIA_CARD(NEWMEDIA, BASICS_SCSI, 0), 0 }, 88107362Snon { PCMCIA_CARD(QLOGIC, PC05, 0), 0x84d00000 }, 89107362Snon#define FLAGS_REX5572 0x84d00000 90107362Snon { PCMCIA_CARD(RATOC, REX5572, 0), FLAGS_REX5572 }, 91107362Snon { PCMCIA_CARD(RATOC, REX_R280, 0), 0x84d00000 }, 92107362Snon { { NULL }, 0 } 93107362Snon}; 94107362Snon 9567468Snon/* 9667468Snon * Additional code for FreeBSD new-bus PCCard frontend 9767468Snon */ 9867468Snon 9967468Snonstatic void 10067468Snonncv_pccard_intr(void * arg) 10167468Snon{ 10267468Snon ncvintr(arg); 10367468Snon} 10467468Snon 10567468Snonstatic void 10667468Snonncv_release_resource(DEVPORT_PDEVICE dev) 10767468Snon{ 10867468Snon struct ncv_softc *sc = device_get_softc(dev); 10967468Snon 11067468Snon if (sc->ncv_intrhand) { 11167468Snon bus_teardown_intr(dev, sc->irq_res, sc->ncv_intrhand); 11267468Snon } 11367468Snon 11467468Snon if (sc->port_res) { 11567468Snon bus_release_resource(dev, SYS_RES_IOPORT, 11667468Snon sc->port_rid, sc->port_res); 11767468Snon } 11867468Snon 11973087Snon if (sc->port_res_dmy) { 12073087Snon bus_release_resource(dev, SYS_RES_IOPORT, 12173087Snon sc->port_rid_dmy, sc->port_res_dmy); 12273087Snon } 12373087Snon 12467468Snon if (sc->irq_res) { 12567468Snon bus_release_resource(dev, SYS_RES_IRQ, 12667468Snon sc->irq_rid, sc->irq_res); 12767468Snon } 12867468Snon 12967468Snon if (sc->mem_res) { 13067468Snon bus_release_resource(dev, SYS_RES_MEMORY, 13167468Snon sc->mem_rid, sc->mem_res); 13267468Snon } 13367468Snon} 13467468Snon 13567468Snonstatic int 13667468Snonncv_alloc_resource(DEVPORT_PDEVICE dev) 13767468Snon{ 13867468Snon struct ncv_softc *sc = device_get_softc(dev); 13967468Snon u_int32_t flags = DEVPORT_PDEVFLAGS(dev); 14073087Snon u_long ioaddr, iosize, maddr, msize; 14167468Snon int error; 14267468Snon bus_addr_t offset = 0; 14367468Snon 14467468Snon if(flags & KME_KXLC004_01) 14567468Snon offset = OFFSET_KME_KXLC004_01; 14667468Snon 14773087Snon error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize); 14873087Snon if (error || (iosize < (offset + NCVIOSZ))) { 14973087Snon return(ENOMEM); 15073087Snon } 15173087Snon 15267468Snon sc->port_rid = 0; 15367468Snon sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid, 15473087Snon ioaddr+offset, ioaddr+iosize-offset, 15573087Snon iosize-offset, RF_ACTIVE); 15667468Snon if (sc->port_res == NULL) { 15767468Snon ncv_release_resource(dev); 15867468Snon return(ENOMEM); 15967468Snon } 16067468Snon 16173087Snon if (offset != 0) { 16273087Snon sc->port_rid_dmy = 0; 16373087Snon sc->port_res_dmy = bus_alloc_resource(dev, SYS_RES_IOPORT, 16473087Snon &sc->port_rid_dmy, 16573087Snon ioaddr, ioaddr+offset, offset, 16673087Snon RF_ACTIVE); 16773087Snon if (sc->port_res_dmy == NULL) { 16873087Snon printf("Warning: cannot allocate IOPORT partially.\n"); 16973087Snon } 17073087Snon } else { 17173087Snon sc->port_rid_dmy = 0; 17273087Snon sc->port_res_dmy = NULL; 17373087Snon } 17473087Snon 17567468Snon sc->irq_rid = 0; 17667468Snon sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid, 17767468Snon 0, ~0, 1, RF_ACTIVE); 17867468Snon if (sc->irq_res == NULL) { 17967468Snon ncv_release_resource(dev); 18067468Snon return(ENOMEM); 18167468Snon } 18267468Snon 18367468Snon error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize); 18467468Snon if (error) { 18567468Snon return(0); /* XXX */ 18667468Snon } 18767468Snon 18867468Snon /* no need to allocate memory if not configured */ 18967468Snon if (maddr == 0 || msize == 0) { 19067468Snon return(0); 19167468Snon } 19267468Snon 19367468Snon sc->mem_rid = 0; 19467468Snon sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid, 19582922Snon 0, ~0, 1, RF_ACTIVE); 19667468Snon if (sc->mem_res == NULL) { 19767468Snon ncv_release_resource(dev); 19867468Snon return(ENOMEM); 19967468Snon } 20067468Snon 20167468Snon return(0); 20267468Snon} 20367468Snon 204107362Snonstatic int ncv_pccard_match(device_t dev) 205107362Snon{ 206107362Snon const struct ncv_product *pp; 207107362Snon char *vendorstr; 208107362Snon char *prodstr; 209107362Snon 210107362Snon if ((pp = (const struct ncv_product *) pccard_product_lookup(dev, 211107362Snon (const struct pccard_product *) ncv_products, 212107362Snon sizeof(ncv_products[0]), NULL)) != NULL) { 213107362Snon device_set_desc(dev, pp->prod.pp_name); 214107362Snon device_set_flags(dev, pp->flags); 215107362Snon return(0); 216107362Snon } 217107362Snon if (pccard_get_vendor_str(dev, &vendorstr)) 218107362Snon return(EIO); 219107362Snon if (pccard_get_product_str(dev, &prodstr)) 220107362Snon return(EIO); 221107362Snon if (strcmp(vendorstr, "RATOC System Inc.") == 0 && 222107362Snon strncmp(prodstr, "SOUND/SCSI2 CARD", 16) == 0) { 223107362Snon device_set_desc(dev, "RATOC REX-5572"); 224107362Snon device_set_flags(dev, FLAGS_REX5572); 225107362Snon return (0); 226107362Snon } 227107362Snon return(EIO); 228107362Snon} 229107362Snon 23067468Snonstatic int 23167468Snonncv_pccard_probe(DEVPORT_PDEVICE dev) 23267468Snon{ 23367468Snon struct ncv_softc *sc = device_get_softc(dev); 23467468Snon int error; 23567468Snon 23667468Snon bzero(sc, sizeof(struct ncv_softc)); 23767468Snon 23867468Snon error = ncv_alloc_resource(dev); 23967468Snon if (error) { 24067468Snon return(error); 24167468Snon } 24267468Snon 24367468Snon if (ncvprobe(dev) == 0) { 24467468Snon ncv_release_resource(dev); 24567468Snon return(ENXIO); 24667468Snon } 24767468Snon 24867468Snon ncv_release_resource(dev); 24967468Snon 25067468Snon return(0); 25167468Snon} 25267468Snon 25367468Snonstatic int 25467468Snonncv_pccard_attach(DEVPORT_PDEVICE dev) 25567468Snon{ 25667468Snon struct ncv_softc *sc = device_get_softc(dev); 25767468Snon int error; 25867468Snon 25967468Snon error = ncv_alloc_resource(dev); 26067468Snon if (error) { 26167468Snon return(error); 26267468Snon } 26367468Snon 26473280Smarkm error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM | INTR_ENTROPY, 26567468Snon ncv_pccard_intr, (void *)sc, &sc->ncv_intrhand); 26667468Snon if (error) { 26767468Snon ncv_release_resource(dev); 26867468Snon return(error); 26967468Snon } 27067468Snon 27167468Snon if (ncvattach(dev) == 0) { 27267468Snon ncv_release_resource(dev); 27367468Snon return(ENXIO); 27467468Snon } 27567468Snon 27667468Snon return(0); 27767468Snon} 27867468Snon 27967468Snonstatic void 28067468Snonncv_pccard_detach(DEVPORT_PDEVICE dev) 28167468Snon{ 28267468Snon ncv_card_unload(dev); 28367468Snon ncv_release_resource(dev); 28467468Snon} 28567468Snon 28667468Snonstatic device_method_t ncv_pccard_methods[] = { 28767468Snon /* Device interface */ 288107362Snon DEVMETHOD(device_probe, pccard_compat_probe), 289107362Snon DEVMETHOD(device_attach, pccard_compat_attach), 29067468Snon DEVMETHOD(device_detach, ncv_pccard_detach), 29167468Snon 292107362Snon /* Card interface */ 293107362Snon DEVMETHOD(card_compat_match, ncv_pccard_match), 294107362Snon DEVMETHOD(card_compat_probe, ncv_pccard_probe), 295107362Snon DEVMETHOD(card_compat_attach, ncv_pccard_attach), 296107362Snon 29767468Snon { 0, 0 } 29867468Snon}; 29967468Snon 30067468Snonstatic driver_t ncv_pccard_driver = { 30167468Snon "ncv", 30267468Snon ncv_pccard_methods, 30367468Snon sizeof(struct ncv_softc), 30467468Snon}; 30567468Snon 30667468Snonstatic devclass_t ncv_devclass; 30767468Snon 30887983SnonMODULE_DEPEND(ncv, scsi_low, 1, 1, 1); 30967468SnonDRIVER_MODULE(ncv, pccard, ncv_pccard_driver, ncv_devclass, 0, 0); 31067468Snon 31167468Snonstatic void 31267468Snonncv_card_unload(DEVPORT_PDEVICE devi) 31367468Snon{ 31467468Snon struct ncv_softc *sc = DEVPORT_PDEVGET_SOFTC(devi); 31579697Snon intrmask_t s; 31667468Snon 31767468Snon printf("%s: unload\n", sc->sc_sclow.sl_xname); 31879697Snon s = splcam(); 31967468Snon scsi_low_deactivate((struct scsi_low_softc *)sc); 32067468Snon scsi_low_dettach(&sc->sc_sclow); 32179697Snon splx(s); 32267468Snon} 32367468Snon 32467468Snonstatic int 32567468Snonncvprobe(DEVPORT_PDEVICE devi) 32667468Snon{ 32767468Snon int rv; 32867468Snon struct ncv_softc *sc = device_get_softc(devi); 32967468Snon u_int32_t flags = DEVPORT_PDEVFLAGS(devi); 33067468Snon 33167468Snon rv = ncvprobesubr(rman_get_bustag(sc->port_res), 33267468Snon rman_get_bushandle(sc->port_res), 33367468Snon flags, NCV_HOSTID); 33467468Snon 33567468Snon return rv; 33667468Snon} 33767468Snon 33867468Snonstatic int 33967468Snonncvattach(DEVPORT_PDEVICE devi) 34067468Snon{ 34167468Snon struct ncv_softc *sc; 34267468Snon struct scsi_low_softc *slp; 34367468Snon u_int32_t flags = DEVPORT_PDEVFLAGS(devi); 34479697Snon intrmask_t s; 34567468Snon char dvname[16]; /* SCSI_LOW_DVNAME_LEN */ 34667468Snon 34767468Snon strcpy(dvname, "ncv"); 34867468Snon 34967468Snon sc = DEVPORT_PDEVALLOC_SOFTC(devi); 35067468Snon if (sc == NULL) { 35167468Snon return(0); 35267468Snon } 35367468Snon 35467468Snon slp = &sc->sc_sclow; 35567468Snon slp->sl_dev = devi; 35667468Snon sc->sc_iot = rman_get_bustag(sc->port_res); 35767468Snon sc->sc_ioh = rman_get_bushandle(sc->port_res); 35867468Snon 35967468Snon slp->sl_hostid = NCV_HOSTID; 36067468Snon slp->sl_cfgflags = flags; 36167468Snon 36279697Snon s = splcam(); 36367468Snon ncvattachsubr(sc); 36479697Snon splx(s); 36567468Snon 36667468Snon return(NCVIOSZ); 36767468Snon} 368