167468Snon/* $NecBSD: nsp_pisa.c,v 1.4 1999/04/15 01:35:54 kmatsuda Exp $ */ 267468Snon/* $NetBSD$ */ 367468Snon 4139749Simp/*- 567468Snon * [Ported for FreeBSD] 667468Snon * Copyright (c) 2000 767468Snon * Noriaki Mitsunaga, Mitsuru Iwasaki and Takanori Watanabe. 867468Snon * All rights reserved. 967468Snon * [NetBSD for NEC PC-98 series] 1067468Snon * Copyright (c) 1998 1167468Snon * NetBSD/pc98 porting staff. All rights reserved. 1267468Snon * 1367468Snon * Redistribution and use in source and binary forms, with or without 1467468Snon * modification, are permitted provided that the following conditions 1567468Snon * are met: 1667468Snon * 1. Redistributions of source code must retain the above copyright 1767468Snon * notice, this list of conditions and the following disclaimer. 1867468Snon * 2. Redistributions in binary form must reproduce the above copyright 1967468Snon * notice, this list of conditions and the following disclaimer in the 2067468Snon * documentation and/or other materials provided with the distribution. 2167468Snon * 3. The name of the author may not be used to endorse or promote products 2267468Snon * derived from this software without specific prior written permission. 2367468Snon * 2467468Snon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2567468Snon * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 2667468Snon * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 2767468Snon * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 2867468Snon * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 2967468Snon * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 3067468Snon * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3167468Snon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 3267468Snon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 3367468Snon * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3467468Snon * POSSIBILITY OF SUCH DAMAGE. 3567468Snon */ 3667468Snon 37119418Sobrien#include <sys/cdefs.h> 38119418Sobrien__FBSDID("$FreeBSD$"); 39119418Sobrien 4067468Snon#include <sys/param.h> 41129764Simp#include <sys/bus.h> 42129764Simp#include <sys/errno.h> 43129764Simp#include <sys/kernel.h> 44129764Simp#include <sys/malloc.h> 45240172Sjhb#include <sys/module.h> 4667468Snon#include <sys/systm.h> 4767468Snon 4867468Snon#include <machine/bus.h> 49129764Simp#include <machine/resource.h> 50129764Simp#include <sys/rman.h> 5167468Snon 52240172Sjhb#include <sys/bus.h> 5367468Snon 54107361Snon#include <dev/pccard/pccardvar.h> 55107361Snon 5667468Snon#include <cam/scsi/scsi_low.h> 5767468Snon 5867468Snon#include <dev/nsp/nspreg.h> 5967468Snon#include <dev/nsp/nspvar.h> 6067468Snon 6167468Snon#define NSP_HOSTID 7 6267468Snon 63129764Simp#include "pccarddevs.h" 6467468Snon 6567848Snon#define PIO_MODE 0x100 /* pd_flags */ 6667468Snon 67240172Sjhbstatic int nspprobe(device_t devi); 68240172Sjhbstatic int nspattach(device_t devi); 6967468Snon 70240172Sjhbstatic void nsp_card_unload (device_t); 7167468Snon 72107361Snonconst struct pccard_product nsp_products[] = { 73147580Simp PCMCIA_CARD(IODATA3, CBSC16), 74147580Simp PCMCIA_CARD(PANASONIC, KME), 75147580Simp PCMCIA_CARD(WORKBIT2, NINJA_SCSI3), 76147580Simp PCMCIA_CARD(WORKBIT, ULTRA_NINJA_16), 77107361Snon { NULL } 78107361Snon}; 79107361Snon 8067468Snon/* 81150396Simp * Additional code for FreeBSD new-bus PC Card frontend 8267468Snon */ 8367468Snon 8467468Snonstatic void 8567468Snonnsp_pccard_intr(void * arg) 8667468Snon{ 8767468Snon nspintr(arg); 8867468Snon} 8967468Snon 9067468Snonstatic void 91150396Simpnsp_release_resource(device_t dev) 9267468Snon{ 9367468Snon struct nsp_softc *sc = device_get_softc(dev); 9467468Snon 95141231Simp if (sc->nsp_intrhand) 9667468Snon bus_teardown_intr(dev, sc->irq_res, sc->nsp_intrhand); 97141231Simp if (sc->port_res) 9867468Snon bus_release_resource(dev, SYS_RES_IOPORT, 9967468Snon sc->port_rid, sc->port_res); 100141231Simp if (sc->irq_res) 10167468Snon bus_release_resource(dev, SYS_RES_IRQ, 10267468Snon sc->irq_rid, sc->irq_res); 103141231Simp if (sc->mem_res) 10467468Snon bus_release_resource(dev, SYS_RES_MEMORY, 10567468Snon sc->mem_rid, sc->mem_res); 10667468Snon} 10767468Snon 10867468Snonstatic int 109150396Simpnsp_alloc_resource(device_t dev) 11067468Snon{ 11167468Snon struct nsp_softc *sc = device_get_softc(dev); 11273029Snon u_long ioaddr, iosize, maddr, msize; 11367468Snon int error; 11467468Snon 11573029Snon error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize); 11673029Snon if (error || iosize < NSP_IOSIZE) 11773029Snon return(ENOMEM); 11873029Snon 11967468Snon sc->port_rid = 0; 12067468Snon sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid, 12182782Snon 0, ~0, NSP_IOSIZE, RF_ACTIVE); 12267468Snon if (sc->port_res == NULL) { 12367468Snon nsp_release_resource(dev); 12467468Snon return(ENOMEM); 12567468Snon } 12667468Snon 12767468Snon sc->irq_rid = 0; 128127135Snjl sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid, 129127135Snjl RF_ACTIVE); 13067468Snon if (sc->irq_res == NULL) { 13167468Snon nsp_release_resource(dev); 13267468Snon return(ENOMEM); 13367468Snon } 13467468Snon 13567468Snon error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize); 136141231Simp if (error) 13767468Snon return(0); /* XXX */ 13867468Snon 13973029Snon /* No need to allocate memory if not configured and it's in PIO mode */ 14067468Snon if (maddr == 0 || msize == 0) { 141240172Sjhb if ((device_get_flags(dev) & PIO_MODE) == 0) { 14273029Snon printf("Memory window was not configured. Configure or use in PIO mode."); 14373029Snon nsp_release_resource(dev); 14473029Snon return(ENOMEM); 14573029Snon } 14673029Snon /* no need to allocate memory if PIO mode */ 14767468Snon return(0); 14867468Snon } 14967468Snon 15067468Snon sc->mem_rid = 0; 151127135Snjl sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid, 152127135Snjl RF_ACTIVE); 15367468Snon if (sc->mem_res == NULL) { 15467468Snon nsp_release_resource(dev); 15567468Snon return(ENOMEM); 15667468Snon } 15767468Snon 15867468Snon return(0); 15967468Snon} 16067468Snon 161150396Simpstatic int 162150396Simpnsp_pccard_probe(device_t dev) 163107361Snon{ 164107361Snon const struct pccard_product *pp; 165107361Snon 166107361Snon if ((pp = pccard_product_lookup(dev, nsp_products, 167107361Snon sizeof(nsp_products[0]), NULL)) != NULL) { 168141231Simp if (pp->pp_name) 169141231Simp device_set_desc(dev, pp->pp_name); 170107361Snon return(0); 171107361Snon } 172107361Snon return(EIO); 173107361Snon} 174107361Snon 17567468Snonstatic int 176150396Simpnsp_pccard_attach(device_t dev) 17767468Snon{ 178150396Simp struct nsp_softc *sc = device_get_softc(dev); 17967468Snon int error; 18067468Snon 18167468Snon error = nsp_alloc_resource(dev); 182141231Simp if (error) 18367468Snon return(error); 18467468Snon if (nspprobe(dev) == 0) { 18567468Snon nsp_release_resource(dev); 18667468Snon return(ENXIO); 18767468Snon } 18873280Smarkm error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM | INTR_ENTROPY, 189166901Spiso NULL, nsp_pccard_intr, (void *)sc, &sc->nsp_intrhand); 19067468Snon if (error) { 19167468Snon nsp_release_resource(dev); 19267468Snon return(error); 19367468Snon } 19467468Snon if (nspattach(dev) == 0) { 19567468Snon nsp_release_resource(dev); 19667468Snon return(ENXIO); 19767468Snon } 19867468Snon 19967468Snon return(0); 20067468Snon} 20167468Snon 202194023Savgstatic int 203150396Simpnsp_pccard_detach(device_t dev) 20467468Snon{ 20567468Snon nsp_card_unload(dev); 20667468Snon nsp_release_resource(dev); 207194023Savg 208194023Savg return (0); 20967468Snon} 21067468Snon 21167468Snonstatic device_method_t nsp_pccard_methods[] = { 21267468Snon /* Device interface */ 213150396Simp DEVMETHOD(device_probe, nsp_pccard_probe), 214150396Simp DEVMETHOD(device_attach, nsp_pccard_attach), 21567468Snon DEVMETHOD(device_detach, nsp_pccard_detach), 21667468Snon { 0, 0 } 21767468Snon}; 21867468Snon 21967468Snonstatic driver_t nsp_pccard_driver = { 22067468Snon "nsp", 22167468Snon nsp_pccard_methods, 22267468Snon sizeof(struct nsp_softc), 22367468Snon}; 22467468Snon 22567468Snonstatic devclass_t nsp_devclass; 22667468Snon 22787983SnonMODULE_DEPEND(nsp, scsi_low, 1, 1, 1); 22867468SnonDRIVER_MODULE(nsp, pccard, nsp_pccard_driver, nsp_devclass, 0, 0); 22967468Snon 230141231Simpstatic void 231240172Sjhbnsp_card_unload(device_t devi) 23267468Snon{ 233240172Sjhb struct nsp_softc *sc = device_get_softc(devi); 23479697Snon intrmask_t s; 23567468Snon 23679697Snon s = splcam(); 23767468Snon scsi_low_deactivate((struct scsi_low_softc *)sc); 23867468Snon scsi_low_dettach(&sc->sc_sclow); 23979697Snon splx(s); 24067468Snon} 24167468Snon 24267468Snonstatic int 243240172Sjhbnspprobe(device_t devi) 24467468Snon{ 24567468Snon int rv; 24667468Snon struct nsp_softc *sc = device_get_softc(devi); 24767468Snon 24867468Snon rv = nspprobesubr(rman_get_bustag(sc->port_res), 24967468Snon rman_get_bushandle(sc->port_res), 250240172Sjhb device_get_flags(devi)); 25167468Snon 25267468Snon return rv; 25367468Snon} 25467468Snon 25567468Snonstatic int 256240172Sjhbnspattach(device_t devi) 25767468Snon{ 25867468Snon struct nsp_softc *sc; 25967468Snon struct scsi_low_softc *slp; 260240172Sjhb u_int32_t flags = device_get_flags(devi); 261240172Sjhb u_int iobase = bus_get_resource_start(devi, SYS_RES_IOPORT, 0); 26279697Snon intrmask_t s; 26367468Snon char dvname[16]; 26467468Snon 26567468Snon strcpy(dvname,"nsp"); 26667468Snon 267141231Simp if (iobase == 0) { 26867468Snon printf("%s: no ioaddr is given\n", dvname); 26967468Snon return (0); 27067468Snon } 27167468Snon 272240172Sjhb sc = device_get_softc(devi); 273141231Simp if (sc == NULL) 27467468Snon return (0); 27567468Snon 27667468Snon slp = &sc->sc_sclow; 27767468Snon slp->sl_dev = devi; 27867468Snon sc->sc_iot = rman_get_bustag(sc->port_res); 27967468Snon sc->sc_ioh = rman_get_bushandle(sc->port_res); 28067468Snon 281141231Simp if (sc->mem_res == NULL) { 282141231Simp printf("WARNING: CANNOT GET Memory RESOURCE going PIO mode"); 283141231Simp flags |= PIO_MODE; 284107361Snon } 285107361Snon 286141231Simp if ((flags & PIO_MODE) == 0) { 28767468Snon sc->sc_memt = rman_get_bustag(sc->mem_res); 28867468Snon sc->sc_memh = rman_get_bushandle(sc->mem_res); 28967468Snon } else { 29067468Snon sc->sc_memh = 0; 29167468Snon } 29267468Snon /* slp->sl_irq = devi->pd_irq; */ 29367468Snon sc->sc_iclkdiv = CLKDIVR_20M; 29467468Snon sc->sc_clkdiv = CLKDIVR_40M; 29567468Snon 29667468Snon slp->sl_hostid = NSP_HOSTID; 29767468Snon slp->sl_cfgflags = flags; 29867468Snon 29979697Snon s = splcam(); 30067468Snon nspattachsubr(sc); 30179697Snon splx(s); 30267468Snon 30367468Snon return(NSP_IOSIZE); 30467468Snon} 305