ncr53c500_pccard.c revision 67848
167468Snon/* $FreeBSD: head/sys/dev/ncv/ncr53c500_pccard.c 67848 2000-10-29 06:54:32Z 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/disklabel.h> 4367468Snon#if defined(__FreeBSD__) && __FreeBSD_version >= 500001 4467468Snon#include <sys/bio.h> 4567468Snon#endif 4667468Snon#include <sys/buf.h> 4767468Snon#include <sys/queue.h> 4867468Snon#include <sys/malloc.h> 4967468Snon#include <sys/errno.h> 5067468Snon 5167468Snon#include <vm/vm.h> 5267468Snon 5367468Snon#include <machine/bus.h> 5467468Snon#include <machine/bus_pio.h> 5567468Snon#include <i386/isa/isa_device.h> 5667468Snon 5767468Snon#include <machine/dvcfg.h> 5867468Snon 5967468Snon#if defined(__FreeBSD__) && __FreeBSD_version < 400001 6067468Snonstatic struct ncv_softc *ncv_get_softc(int); 6167468Snonextern struct ncv_softc *ncvdata[]; 6267468Snon#define DEVPORT_ALLOCSOFTCFUNC ncv_get_softc 6367468Snon#define DEVPORT_SOFTCARRAY ncvdata 6467468Snon#endif 6567468Snon#include <sys/device_port.h> 6667468Snon 6767468Snon#include <cam/scsi/scsi_low.h> 6867468Snon#include <cam/scsi/scsi_low_pisa.h> 6967468Snon 7067468Snon#include <dev/ncv/ncr53c500reg.h> 7167468Snon#include <dev/ncv/ncr53c500hw.h> 7267468Snon#include <dev/ncv/ncr53c500var.h> 7367468Snon#if defined(__NetBSD__) || (defined(__FreeBSD__) && __FreeBSD_version < 400001) 7467468Snon#include "ncv.h" 7567468Snon#endif 7667468Snon 7767848Snon#define KME_KXLC004_01 0x100 7867468Snon#define OFFSET_KME_KXLC004_01 0x10 7967468Snon 8067468Snon/* pccard support */ 8167468Snon#include "apm.h" 8267468Snon#if NAPM > 0 8367468Snon#include <machine/apm_bios.h> 8467468Snon#endif /* NAPM > 0 */ 8567468Snon 8667468Snon#include "card.h" 8767468Snon#if NCARD > 0 8867468Snon#include <sys/kernel.h> 8967468Snon#include <sys/module.h> 9067468Snon#include <sys/select.h> 9167468Snon#include <pccard/cardinfo.h> 9267468Snon#include <pccard/slot.h> 9367468Snon 9467468Snonstatic int ncvprobe(DEVPORT_PDEVICE devi); 9567468Snonstatic int ncvattach(DEVPORT_PDEVICE devi); 9667468Snon 9767468Snonstatic int ncv_card_intr __P((DEVPORT_PDEVICE)); 9867468Snonstatic void ncv_card_unload __P((DEVPORT_PDEVICE)); 9967468Snon#if defined(__FreeBSD__) && __FreeBSD_version < 400001 10067468Snonstatic int ncv_card_init __P((DEVPORT_PDEVICE)); 10167468Snon#endif 10267468Snon 10367468Snon#if defined(__FreeBSD__) && __FreeBSD_version >= 400001 10467468Snon/* 10567468Snon * Additional code for FreeBSD new-bus PCCard frontend 10667468Snon */ 10767468Snon 10867468Snonstatic void 10967468Snonncv_pccard_intr(void * arg) 11067468Snon{ 11167468Snon ncvintr(arg); 11267468Snon} 11367468Snon 11467468Snonstatic void 11567468Snonncv_release_resource(DEVPORT_PDEVICE dev) 11667468Snon{ 11767468Snon struct ncv_softc *sc = device_get_softc(dev); 11867468Snon 11967468Snon if (sc->ncv_intrhand) { 12067468Snon bus_teardown_intr(dev, sc->irq_res, sc->ncv_intrhand); 12167468Snon } 12267468Snon 12367468Snon if (sc->port_res) { 12467468Snon bus_release_resource(dev, SYS_RES_IOPORT, 12567468Snon sc->port_rid, sc->port_res); 12667468Snon } 12767468Snon 12867468Snon if (sc->irq_res) { 12967468Snon bus_release_resource(dev, SYS_RES_IRQ, 13067468Snon sc->irq_rid, sc->irq_res); 13167468Snon } 13267468Snon 13367468Snon if (sc->mem_res) { 13467468Snon bus_release_resource(dev, SYS_RES_MEMORY, 13567468Snon sc->mem_rid, sc->mem_res); 13667468Snon } 13767468Snon} 13867468Snon 13967468Snonstatic int 14067468Snonncv_alloc_resource(DEVPORT_PDEVICE dev) 14167468Snon{ 14267468Snon struct ncv_softc *sc = device_get_softc(dev); 14367468Snon u_int32_t flags = DEVPORT_PDEVFLAGS(dev); 14467468Snon u_int iobase = DEVPORT_PDEVIOBASE(dev); 14567468Snon u_long maddr, msize; 14667468Snon int error; 14767468Snon bus_addr_t offset = 0; 14867468Snon 14967468Snon if(flags & KME_KXLC004_01) 15067468Snon offset = OFFSET_KME_KXLC004_01; 15167468Snon 15267468Snon sc->port_rid = 0; 15367468Snon sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid, 15467468Snon iobase+offset, ~0, NCVIOSZ, RF_ACTIVE); 15567468Snon if (sc->port_res == NULL) { 15667468Snon ncv_release_resource(dev); 15767468Snon return(ENOMEM); 15867468Snon } 15967468Snon 16067468Snon sc->irq_rid = 0; 16167468Snon sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid, 16267468Snon 0, ~0, 1, RF_ACTIVE); 16367468Snon if (sc->irq_res == NULL) { 16467468Snon ncv_release_resource(dev); 16567468Snon return(ENOMEM); 16667468Snon } 16767468Snon 16867468Snon error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize); 16967468Snon if (error) { 17067468Snon return(0); /* XXX */ 17167468Snon } 17267468Snon 17367468Snon /* no need to allocate memory if not configured */ 17467468Snon if (maddr == 0 || msize == 0) { 17567468Snon return(0); 17667468Snon } 17767468Snon 17867468Snon sc->mem_rid = 0; 17967468Snon sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid, 18067468Snon 0, ~0, msize, RF_ACTIVE); 18167468Snon if (sc->mem_res == NULL) { 18267468Snon ncv_release_resource(dev); 18367468Snon return(ENOMEM); 18467468Snon } 18567468Snon 18667468Snon return(0); 18767468Snon} 18867468Snon 18967468Snonstatic int 19067468Snonncv_pccard_probe(DEVPORT_PDEVICE dev) 19167468Snon{ 19267468Snon struct ncv_softc *sc = device_get_softc(dev); 19367468Snon int error; 19467468Snon 19567468Snon bzero(sc, sizeof(struct ncv_softc)); 19667468Snon 19767468Snon error = ncv_alloc_resource(dev); 19867468Snon if (error) { 19967468Snon return(error); 20067468Snon } 20167468Snon 20267468Snon if (ncvprobe(dev) == 0) { 20367468Snon ncv_release_resource(dev); 20467468Snon return(ENXIO); 20567468Snon } 20667468Snon 20767468Snon ncv_release_resource(dev); 20867468Snon 20967468Snon return(0); 21067468Snon} 21167468Snon 21267468Snonstatic int 21367468Snonncv_pccard_attach(DEVPORT_PDEVICE dev) 21467468Snon{ 21567468Snon struct ncv_softc *sc = device_get_softc(dev); 21667468Snon int error; 21767468Snon 21867468Snon error = ncv_alloc_resource(dev); 21967468Snon if (error) { 22067468Snon return(error); 22167468Snon } 22267468Snon 22367468Snon error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM, 22467468Snon ncv_pccard_intr, (void *)sc, &sc->ncv_intrhand); 22567468Snon if (error) { 22667468Snon ncv_release_resource(dev); 22767468Snon return(error); 22867468Snon } 22967468Snon 23067468Snon if (ncvattach(dev) == 0) { 23167468Snon ncv_release_resource(dev); 23267468Snon return(ENXIO); 23367468Snon } 23467468Snon 23567468Snon return(0); 23667468Snon} 23767468Snon 23867468Snonstatic void 23967468Snonncv_pccard_detach(DEVPORT_PDEVICE dev) 24067468Snon{ 24167468Snon ncv_card_unload(dev); 24267468Snon ncv_release_resource(dev); 24367468Snon} 24467468Snon 24567468Snonstatic device_method_t ncv_pccard_methods[] = { 24667468Snon /* Device interface */ 24767468Snon DEVMETHOD(device_probe, ncv_pccard_probe), 24867468Snon DEVMETHOD(device_attach, ncv_pccard_attach), 24967468Snon DEVMETHOD(device_detach, ncv_pccard_detach), 25067468Snon 25167468Snon { 0, 0 } 25267468Snon}; 25367468Snon 25467468Snonstatic driver_t ncv_pccard_driver = { 25567468Snon "ncv", 25667468Snon ncv_pccard_methods, 25767468Snon sizeof(struct ncv_softc), 25867468Snon}; 25967468Snon 26067468Snonstatic devclass_t ncv_devclass; 26167468Snon 26267468SnonDRIVER_MODULE(ncv, pccard, ncv_pccard_driver, ncv_devclass, 0, 0); 26367468Snon 26467468Snon#else 26567468Snon 26667468SnonPCCARD_MODULE(ncv, ncv_card_init, ncv_card_unload, ncv_card_intr, 0, cam_imask); 26767468Snon 26867468Snon#endif 26967468Snon 27067468Snon#if defined(__FreeBSD__) && __FreeBSD_version < 400001 27167468Snonstatic struct ncv_softc * 27267468Snonncv_get_softc(int unit) 27367468Snon{ 27467468Snon struct ncv_softc *sc; 27567468Snon 27667468Snon if (unit >= NNCV) { 27767468Snon return(NULL); 27867468Snon } 27967468Snon 28067468Snon if (ncvdata[unit] == NULL) { 28167468Snon sc = malloc(sizeof(struct ncv_softc), M_TEMP,M_NOWAIT); 28267468Snon if (sc == NULL) { 28367468Snon printf("ncv_get_softc: cannot malloc!\n"); 28467468Snon return(NULL); 28567468Snon } 28667468Snon ncvdata[unit] = sc; 28767468Snon } else { 28867468Snon sc = ncvdata[unit]; 28967468Snon } 29067468Snon 29167468Snon return(sc); 29267468Snon} 29367468Snon 29467468Snonstatic int 29567468Snonncv_card_init(DEVPORT_PDEVICE devi) 29667468Snon{ 29767468Snon int unit = DEVPORT_PDEVUNIT(devi); 29867468Snon 29967468Snon if (NNCV <= unit) 30067468Snon return (ENODEV); 30167468Snon 30267468Snon if (ncvprobe(devi) == 0) 30367468Snon return (ENXIO); 30467468Snon 30567468Snon if (ncvattach(devi) == 0) 30667468Snon return (ENXIO); 30767468Snon return (0); 30867468Snon} 30967468Snon#endif 31067468Snon 31167468Snonstatic void 31267468Snonncv_card_unload(DEVPORT_PDEVICE devi) 31367468Snon{ 31467468Snon struct ncv_softc *sc = DEVPORT_PDEVGET_SOFTC(devi); 31567468Snon 31667468Snon printf("%s: unload\n", sc->sc_sclow.sl_xname); 31767468Snon scsi_low_deactivate((struct scsi_low_softc *)sc); 31867468Snon scsi_low_dettach(&sc->sc_sclow); 31967468Snon} 32067468Snon 32167468Snonstatic int 32267468Snonncv_card_intr(DEVPORT_PDEVICE devi) 32367468Snon{ 32467468Snon 32567468Snon ncvintr(DEVPORT_PDEVGET_SOFTC(devi)); 32667468Snon return 1; 32767468Snon} 32867468Snon 32967468Snonstatic int 33067468Snonncvprobe(DEVPORT_PDEVICE devi) 33167468Snon{ 33267468Snon int rv; 33367468Snon struct ncv_softc *sc = device_get_softc(devi); 33467468Snon u_int32_t flags = DEVPORT_PDEVFLAGS(devi); 33567468Snon 33667468Snon#if defined(__FreeBSD__) && __FreeBSD_version >= 400001 33767468Snon rv = ncvprobesubr(rman_get_bustag(sc->port_res), 33867468Snon rman_get_bushandle(sc->port_res), 33967468Snon flags, NCV_HOSTID); 34067468Snon#else 34167468Snon bus_addr_t offset = 0; 34267468Snon u_int iobase = DEVPORT_PDEVIOBASE(devi); 34367468Snon 34467468Snon if(flags & KME_KXLC004_01) 34567468Snon offset = OFFSET_KME_KXLC004_01; 34667468Snon 34767468Snon rv = ncvprobesubr(I386_BUS_SPACE_IO, 34867468Snon iobase + offset, 34967468Snon flags, NCV_HOSTID); 35067468Snon#endif 35167468Snon 35267468Snon return rv; 35367468Snon} 35467468Snon 35567468Snonstatic int 35667468Snonncvattach(DEVPORT_PDEVICE devi) 35767468Snon{ 35867468Snon struct ncv_softc *sc; 35967468Snon struct scsi_low_softc *slp; 36067468Snon u_int32_t flags = DEVPORT_PDEVFLAGS(devi); 36167468Snon#if defined(__FreeBSD__) && __FreeBSD_version < 400001 36267468Snon int unit = DEVPORT_PDEVUNIT(devi); 36367468Snon bus_addr_t offset = 0; 36467468Snon u_int iobase = DEVPORT_PDEVIOBASE(devi); 36567468Snon#endif 36667468Snon char dvname[16]; /* SCSI_LOW_DVNAME_LEN */ 36767468Snon 36867468Snon strcpy(dvname, "ncv"); 36967468Snon 37067468Snon#if defined(__FreeBSD__) && __FreeBSD_version < 400001 37167468Snon if (unit >= NNCV) 37267468Snon { 37367468Snon printf("%s: unit number too high\n", dvname); 37467468Snon return (0); 37567468Snon } 37667468Snon 37767468Snon if (iobase == 0) 37867468Snon { 37967468Snon printf("%s: no ioaddr is given\n", dvname); 38067468Snon return (0); 38167468Snon } 38267468Snon 38367468Snon if(flags & KME_KXLC004_01) 38467468Snon offset = OFFSET_KME_KXLC004_01; 38567468Snon#endif 38667468Snon 38767468Snon sc = DEVPORT_PDEVALLOC_SOFTC(devi); 38867468Snon if (sc == NULL) { 38967468Snon return(0); 39067468Snon } 39167468Snon 39267468Snon slp = &sc->sc_sclow; 39367468Snon#if defined(__FreeBSD__) && __FreeBSD_version >= 400001 39467468Snon slp->sl_dev = devi; 39567468Snon sc->sc_iot = rman_get_bustag(sc->port_res); 39667468Snon sc->sc_ioh = rman_get_bushandle(sc->port_res); 39767468Snon#else 39867468Snon bzero(sc, sizeof(struct ncv_softc)); 39967468Snon strcpy(slp->sl_dev.dv_xname, dvname); 40067468Snon slp->sl_dev.dv_unit = unit; 40167468Snon sc->sc_iot = I386_BUS_SPACE_IO; 40267468Snon sc->sc_ioh = iobase + offset; 40367468Snon#endif 40467468Snon 40567468Snon slp->sl_hostid = NCV_HOSTID; 40667468Snon slp->sl_cfgflags = flags; 40767468Snon 40867468Snon ncvattachsubr(sc); 40967468Snon 41067468Snon sc->sc_ih = ncvintr; 41167468Snon 41267468Snon return(NCVIOSZ); 41367468Snon} 41467468Snon#endif /* NCARD */ 415