tmc18c30_subr.c revision 139749
1139749Simp/*- 2113205Smdodd * [Ported for FreeBSD] 3113205Smdodd * Copyright (c) 2000 4113205Smdodd * Noriaki Mitsunaga, Mitsuru Iwasaki and Takanori Watanabe. 5113205Smdodd * All rights reserved. 6113205Smdodd * [NetBSD for NEC PC-98 series] 7113205Smdodd * Copyright (c) 1996, 1997, 1998 8113205Smdodd * NetBSD/pc98 porting staff. All rights reserved. 9113205Smdodd * Copyright (c) 1996, 1997, 1998 10113205Smdodd * Naofumi HONDA. All rights reserved. 11113205Smdodd * Copyright (c) 1996, 1997, 1998 12113205Smdodd * Kouichi Matsuda. All rights reserved. 13113205Smdodd * 14113205Smdodd * Redistribution and use in source and binary forms, with or without 15113205Smdodd * modification, are permitted provided that the following conditions 16113205Smdodd * are met: 17113205Smdodd * 1. Redistributions of source code must retain the above copyright 18113205Smdodd * notice, this list of conditions and the following disclaimer. 19113205Smdodd * 2. Redistributions in binary form must reproduce the above copyright 20113205Smdodd * notice, this list of conditions and the following disclaimer in the 21113205Smdodd * documentation and/or other materials provided with the distribution. 22113205Smdodd * 3. The name of the author may not be used to endorse or promote products 23113205Smdodd * derived from this software without specific prior written permission. 24113205Smdodd * 25113205Smdodd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 26113205Smdodd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 27113205Smdodd * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 28113205Smdodd * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 29113205Smdodd * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 30113205Smdodd * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 31113205Smdodd * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32113205Smdodd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 33113205Smdodd * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 34113205Smdodd * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35113205Smdodd * POSSIBILITY OF SUCH DAMAGE. 36113205Smdodd * 37113205Smdodd */ 38113205Smdodd 39119420Sobrien#include <sys/cdefs.h> 40119420Sobrien__FBSDID("$FreeBSD: head/sys/dev/stg/tmc18c30_subr.c 139749 2005-01-06 01:43:34Z imp $"); 41119420Sobrien 42113205Smdodd#include <sys/param.h> 43113205Smdodd#include <sys/systm.h> 44113205Smdodd#include <sys/kernel.h> 45113205Smdodd#include <sys/socket.h> 46113205Smdodd 47113205Smdodd#include <sys/module.h> 48113205Smdodd#include <sys/bus.h> 49113205Smdodd 50113205Smdodd#include <machine/bus_memio.h> 51113205Smdodd#include <machine/bus_pio.h> 52113205Smdodd#include <machine/bus.h> 53113205Smdodd#include <machine/resource.h> 54113205Smdodd#include <sys/rman.h> 55113205Smdodd 56113205Smdodd#include <cam/scsi/scsi_low.h> 57113205Smdodd#include <cam/scsi/scsi_low_pisa.h> 58113205Smdodd 59113205Smdodd#include <dev/stg/tmc18c30reg.h> 60113205Smdodd#include <dev/stg/tmc18c30var.h> 61113205Smdodd#include <dev/stg/tmc18c30.h> 62113205Smdodd 63113205Smdodd#define STG_HOSTID 7 64113205Smdodd 65113205Smdodddevclass_t stg_devclass; 66113205Smdodd 67113205Smdoddint 68113205Smdoddstg_alloc_resource(device_t dev) 69113205Smdodd{ 70113205Smdodd struct stg_softc * sc = device_get_softc(dev); 71113205Smdodd u_long maddr, msize; 72113205Smdodd int error; 73113205Smdodd 74127135Snjl sc->port_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, 75127135Snjl &sc->port_rid, RF_ACTIVE); 76113205Smdodd if (sc->port_res == NULL) { 77113205Smdodd stg_release_resource(dev); 78113205Smdodd return(ENOMEM); 79113205Smdodd } 80113205Smdodd 81127135Snjl sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid, 82127135Snjl RF_ACTIVE); 83113205Smdodd if (sc->irq_res == NULL) { 84113205Smdodd stg_release_resource(dev); 85113205Smdodd return(ENOMEM); 86113205Smdodd } 87113205Smdodd error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize); 88113205Smdodd if (error) { 89113205Smdodd return(0); /* XXX */ 90113205Smdodd } 91113205Smdodd 92113205Smdodd /* no need to allocate memory if not configured */ 93113205Smdodd if (maddr == 0 || msize == 0) { 94113205Smdodd return(0); 95113205Smdodd } 96113205Smdodd 97127135Snjl sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid, 98127135Snjl RF_ACTIVE); 99113205Smdodd if (sc->mem_res == NULL) { 100113205Smdodd stg_release_resource(dev); 101113205Smdodd return(ENOMEM); 102113205Smdodd } 103113205Smdodd 104113205Smdodd return(0); 105113205Smdodd} 106113205Smdodd 107113205Smdoddvoid 108113205Smdoddstg_release_resource(device_t dev) 109113205Smdodd{ 110113205Smdodd struct stg_softc *sc = device_get_softc(dev); 111113205Smdodd 112113205Smdodd if (sc->stg_intrhand) 113113205Smdodd bus_teardown_intr(dev, sc->irq_res, sc->stg_intrhand); 114113205Smdodd if (sc->port_res) 115113205Smdodd bus_release_resource(dev, SYS_RES_IOPORT, 116113205Smdodd sc->port_rid, sc->port_res); 117113205Smdodd if (sc->irq_res) 118113205Smdodd bus_release_resource(dev, SYS_RES_IRQ, 119113205Smdodd sc->irq_rid, sc->irq_res); 120113205Smdodd if (sc->mem_res) 121113205Smdodd bus_release_resource(dev, SYS_RES_MEMORY, 122113205Smdodd sc->mem_rid, sc->mem_res); 123113205Smdodd return; 124113205Smdodd} 125113205Smdodd 126113205Smdoddint 127113205Smdoddstg_probe(device_t dev) 128113205Smdodd{ 129113205Smdodd int rv; 130113205Smdodd struct stg_softc *sc = device_get_softc(dev); 131113205Smdodd 132113205Smdodd rv = stgprobesubr(rman_get_bustag(sc->port_res), 133113205Smdodd rman_get_bushandle(sc->port_res), 134113205Smdodd device_get_flags(dev)); 135113205Smdodd 136113205Smdodd return rv; 137113205Smdodd} 138113205Smdodd 139113205Smdoddint 140113205Smdoddstg_attach(device_t dev) 141113205Smdodd{ 142113205Smdodd struct stg_softc *sc; 143113205Smdodd struct scsi_low_softc *slp; 144113205Smdodd u_int32_t flags = device_get_flags(dev); 145113205Smdodd intrmask_t s; 146113205Smdodd char dvname[16]; 147113205Smdodd 148113205Smdodd sc = device_get_softc(dev); 149113205Smdodd 150113205Smdodd strcpy(dvname,"stg"); 151113205Smdodd 152113205Smdodd slp = &sc->sc_sclow; 153113205Smdodd slp->sl_dev = dev; 154113205Smdodd sc->sc_iot = rman_get_bustag(sc->port_res); 155113205Smdodd sc->sc_ioh = rman_get_bushandle(sc->port_res); 156113205Smdodd 157113205Smdodd slp->sl_hostid = STG_HOSTID; 158113205Smdodd slp->sl_cfgflags = flags; 159113205Smdodd 160113205Smdodd s = splcam(); 161113205Smdodd stgattachsubr(sc); 162113205Smdodd splx(s); 163113205Smdodd 164113205Smdodd return(STGIOSZ); 165113205Smdodd} 166113205Smdodd 167113205Smdoddvoid 168113205Smdoddstg_detach (device_t dev) 169113205Smdodd{ 170113205Smdodd struct stg_softc *sc = device_get_softc(dev); 171113205Smdodd intrmask_t s; 172113205Smdodd 173113205Smdodd printf("%s: unload\n",sc->sc_sclow.sl_xname); 174113205Smdodd s = splcam(); 175113205Smdodd scsi_low_deactivate((struct scsi_low_softc *)sc); 176113205Smdodd scsi_low_dettach(&sc->sc_sclow); 177113205Smdodd splx(s); 178113205Smdodd stg_release_resource(dev); 179113205Smdodd return; 180113205Smdodd} 181113205Smdodd 182113205Smdoddvoid 183113205Smdoddstg_intr (void *arg) 184113205Smdodd{ 185113205Smdodd stgintr(arg); 186113205Smdodd return; 187113205Smdodd} 188