tmc18c30_pccard.c revision 87983
12786Ssos/* $FreeBSD: head/sys/dev/stg/tmc18c30_pccard.c 87983 2001-12-15 12:32:23Z non $ */ 22786Ssos/* $NecBSD: tmc18c30_pisa.c,v 1.22 1998/11/26 01:59:21 honda Exp $ */ 32786Ssos/* $NetBSD$ */ 42786Ssos 52786Ssos/* 62786Ssos * [Ported for FreeBSD] 72786Ssos * Copyright (c) 2000 86851Ssos * Noriaki Mitsunaga, Mitsuru Iwasaki and Takanori Watanabe. 92786Ssos * All rights reserved. 102786Ssos * [NetBSD for NEC PC-98 series] 112786Ssos * Copyright (c) 1996, 1997, 1998 122786Ssos * NetBSD/pc98 porting staff. All rights reserved. 132786Ssos * Copyright (c) 1996, 1997, 1998 142786Ssos * Naofumi HONDA. All rights reserved. 152786Ssos * Copyright (c) 1996, 1997, 1998 162786Ssos * Kouichi Matsuda. All rights reserved. 172786Ssos * 182786Ssos * Redistribution and use in source and binary forms, with or without 197420Ssos * modification, are permitted provided that the following conditions 204686Sache * are met: 212786Ssos * 1. Redistributions of source code must retain the above copyright 222786Ssos * notice, this list of conditions and the following disclaimer. 232786Ssos * 2. Redistributions in binary form must reproduce the above copyright 242786Ssos * notice, this list of conditions and the following disclaimer in the 252786Ssos * documentation and/or other materials provided with the distribution. 262786Ssos * 3. The name of the author may not be used to endorse or promote products 272786Ssos * derived from this software without specific prior written permission. 282786Ssos * 292786Ssos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 302786Ssos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 312786Ssos * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 322786Ssos * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 332786Ssos * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 342786Ssos * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 352786Ssos * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 362786Ssos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 372786Ssos * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 382786Ssos * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 392786Ssos * POSSIBILITY OF SUCH DAMAGE. 402786Ssos */ 412786Ssos 422786Ssos#include <sys/param.h> 432786Ssos#include <sys/systm.h> 442786Ssos#include <sys/disklabel.h> 452786Ssos#if defined(__FreeBSD__) && __FreeBSD_version >= 500001 462786Ssos#include <sys/bio.h> 472786Ssos#endif 482786Ssos#include <sys/buf.h> 492786Ssos#include <sys/queue.h> 502786Ssos#include <sys/malloc.h> 512786Ssos#include <sys/errno.h> 522786Ssos 532786Ssos#include <vm/vm.h> 542786Ssos 552786Ssos#include <machine/bus.h> 562786Ssos#include <machine/bus_pio.h> 572786Ssos#include <machine/dvcfg.h> 582786Ssos 592786Ssos#include <sys/device_port.h> 602786Ssos 612786Ssos#include <cam/scsi/scsi_low.h> 626851Ssos#include <cam/scsi/scsi_low_pisa.h> 632786Ssos 642786Ssos#include <dev/stg/tmc18c30reg.h> 652786Ssos#include <dev/stg/tmc18c30var.h> 662786Ssos 672786Ssos#define STG_HOSTID 7 682786Ssos 692786Ssos#include <sys/kernel.h> 702786Ssos#include <sys/module.h> 712786Ssos#if !defined(__FreeBSD__) || __FreeBSD_version < 500014 722786Ssos#include <sys/select.h> 732786Ssos#endif 742786Ssos#include <pccard/cardinfo.h> 752786Ssos#include <pccard/slot.h> 762786Ssos 772786Ssosstatic int stgprobe(DEVPORT_PDEVICE devi); 782786Ssosstatic int stgattach(DEVPORT_PDEVICE devi); 792786Ssos 802786Ssosstatic void stg_card_unload __P((DEVPORT_PDEVICE)); 815994Ssos 822786Ssos/* 832786Ssos * Additional code for FreeBSD new-bus PCCard frontend 842786Ssos */ 852786Ssos 862786Ssosstatic void 872786Ssosstg_pccard_intr(void * arg) 886045Ssos{ 892786Ssos stgintr(arg); 902786Ssos} 912786Ssos 922786Ssosstatic void 932786Ssosstg_release_resource(DEVPORT_PDEVICE dev) 9418194Ssos{ 952786Ssos struct stg_softc *sc = device_get_softc(dev); 962786Ssos 972786Ssos if (sc->stg_intrhand) { 982786Ssos bus_teardown_intr(dev, sc->irq_res, sc->stg_intrhand); 992786Ssos } 1002786Ssos 1012786Ssos if (sc->port_res) { 1022786Ssos bus_release_resource(dev, SYS_RES_IOPORT, 1032786Ssos sc->port_rid, sc->port_res); 1042786Ssos } 1052786Ssos 1062786Ssos if (sc->irq_res) { 1072786Ssos bus_release_resource(dev, SYS_RES_IRQ, 1086851Ssos sc->irq_rid, sc->irq_res); 1092786Ssos } 1106851Ssos 1116851Ssos if (sc->mem_res) { 1126851Ssos bus_release_resource(dev, SYS_RES_MEMORY, 113 sc->mem_rid, sc->mem_res); 114 } 115} 116 117static int 118stg_alloc_resource(DEVPORT_PDEVICE dev) 119{ 120 struct stg_softc *sc = device_get_softc(dev); 121 u_long ioaddr, iosize, maddr, msize; 122 int error; 123 124 error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize); 125 if (error || iosize < STGIOSZ) { 126 return(ENOMEM); 127 } 128 129 sc->port_rid = 0; 130 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid, 131 0, ~0, STGIOSZ, RF_ACTIVE); 132 if (sc->port_res == NULL) { 133 stg_release_resource(dev); 134 return(ENOMEM); 135 } 136 137 sc->irq_rid = 0; 138 sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid, 139 0, ~0, 1, RF_ACTIVE); 140 if (sc->irq_res == NULL) { 141 stg_release_resource(dev); 142 return(ENOMEM); 143 } 144 145 error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize); 146 if (error) { 147 return(0); /* XXX */ 148 } 149 150 /* no need to allocate memory if not configured */ 151 if (maddr == 0 || msize == 0) { 152 return(0); 153 } 154 155 sc->mem_rid = 0; 156 sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid, 157 0, ~0, 1, RF_ACTIVE); 158 if (sc->mem_res == NULL) { 159 stg_release_resource(dev); 160 return(ENOMEM); 161 } 162 163 return(0); 164} 165 166static int 167stg_pccard_probe(DEVPORT_PDEVICE dev) 168{ 169 struct stg_softc *sc = device_get_softc(dev); 170 int error; 171 172 bzero(sc, sizeof(struct stg_softc)); 173 174 error = stg_alloc_resource(dev); 175 if (error) { 176 return(error); 177 } 178 179 if (stgprobe(dev) == 0) { 180 stg_release_resource(dev); 181 return(ENXIO); 182 } 183 184 stg_release_resource(dev); 185 186 return(0); 187} 188 189static int 190stg_pccard_attach(DEVPORT_PDEVICE dev) 191{ 192 struct stg_softc *sc = device_get_softc(dev); 193 int error; 194 195 error = stg_alloc_resource(dev); 196 if (error) { 197 return(error); 198 } 199 200 error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM | INTR_ENTROPY, 201 stg_pccard_intr, (void *)sc, &sc->stg_intrhand); 202 if (error) { 203 stg_release_resource(dev); 204 return(error); 205 } 206 207 if (stgattach(dev) == 0) { 208 stg_release_resource(dev); 209 return(ENXIO); 210 } 211 212 return(0); 213} 214 215static void 216stg_pccard_detach(DEVPORT_PDEVICE dev) 217{ 218 stg_card_unload(dev); 219 stg_release_resource(dev); 220} 221 222static device_method_t stg_pccard_methods[] = { 223 /* Device interface */ 224 DEVMETHOD(device_probe, stg_pccard_probe), 225 DEVMETHOD(device_attach, stg_pccard_attach), 226 DEVMETHOD(device_detach, stg_pccard_detach), 227 228 { 0, 0 } 229}; 230 231static driver_t stg_pccard_driver = { 232 "stg", 233 stg_pccard_methods, 234 sizeof(struct stg_softc), 235}; 236 237static devclass_t stg_devclass; 238 239MODULE_DEPEND(stg, scsi_low, 1, 1, 1); 240DRIVER_MODULE(stg, pccard, stg_pccard_driver, stg_devclass, 0, 0); 241 242static void 243stg_card_unload(DEVPORT_PDEVICE devi) 244{ 245 struct stg_softc *sc = DEVPORT_PDEVGET_SOFTC(devi); 246 intrmask_t s; 247 248 printf("%s: unload\n",sc->sc_sclow.sl_xname); 249 s = splcam(); 250 scsi_low_deactivate((struct scsi_low_softc *)sc); 251 scsi_low_dettach(&sc->sc_sclow); 252 splx(s); 253} 254 255static int 256stgprobe(DEVPORT_PDEVICE devi) 257{ 258 int rv; 259 struct stg_softc *sc = device_get_softc(devi); 260 261 rv = stgprobesubr(rman_get_bustag(sc->port_res), 262 rman_get_bushandle(sc->port_res), 263 DEVPORT_PDEVFLAGS(devi)); 264 265 return rv; 266} 267 268static int 269stgattach(DEVPORT_PDEVICE devi) 270{ 271 struct stg_softc *sc; 272 struct scsi_low_softc *slp; 273 u_int32_t flags = DEVPORT_PDEVFLAGS(devi); 274 u_int iobase = DEVPORT_PDEVIOBASE(devi); 275 intrmask_t s; 276 char dvname[16]; 277 278 strcpy(dvname,"stg"); 279 280 if (iobase == 0) 281 { 282 printf("%s: no ioaddr is given\n", dvname); 283 return (0); 284 } 285 286 sc = DEVPORT_PDEVALLOC_SOFTC(devi); 287 if (sc == NULL) { 288 return(0); 289 } 290 291 slp = &sc->sc_sclow; 292 slp->sl_dev = devi; 293 sc->sc_iot = rman_get_bustag(sc->port_res); 294 sc->sc_ioh = rman_get_bushandle(sc->port_res); 295 296 slp->sl_hostid = STG_HOSTID; 297 slp->sl_cfgflags = flags; 298 299 s = splcam(); 300 stgattachsubr(sc); 301 splx(s); 302 303 return(STGIOSZ); 304} 305