tmc18c30_pccard.c revision 70834
1/* $FreeBSD: head/sys/dev/stg/tmc18c30_pccard.c 70834 2001-01-09 04:33:49Z wollman $ */ 2/* $NecBSD: tmc18c30_pisa.c,v 1.22 1998/11/26 01:59:21 honda Exp $ */ 3/* $NetBSD$ */ 4 5/* 6 * [Ported for FreeBSD] 7 * Copyright (c) 2000 8 * Noriaki Mitsunaga, Mitsuru Iwasaki and Takanori Watanabe. 9 * All rights reserved. 10 * [NetBSD for NEC PC-98 series] 11 * Copyright (c) 1996, 1997, 1998 12 * NetBSD/pc98 porting staff. All rights reserved. 13 * Copyright (c) 1996, 1997, 1998 14 * Naofumi HONDA. All rights reserved. 15 * Copyright (c) 1996, 1997, 1998 16 * Kouichi Matsuda. All rights reserved. 17 * 18 * Redistribution and use in source and binary forms, with or without 19 * modification, are permitted provided that the following conditions 20 * are met: 21 * 1. Redistributions of source code must retain the above copyright 22 * notice, this list of conditions and the following disclaimer. 23 * 2. Redistributions in binary form must reproduce the above copyright 24 * notice, this list of conditions and the following disclaimer in the 25 * documentation and/or other materials provided with the distribution. 26 * 3. The name of the author may not be used to endorse or promote products 27 * derived from this software without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 31 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 32 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 38 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 39 * POSSIBILITY OF SUCH DAMAGE. 40 */ 41 42#include <sys/param.h> 43#include <sys/systm.h> 44#include <sys/disklabel.h> 45#if defined(__FreeBSD__) && __FreeBSD_version >= 500001 46#include <sys/bio.h> 47#endif 48#include <sys/buf.h> 49#include <sys/queue.h> 50#include <sys/malloc.h> 51#include <sys/errno.h> 52 53#include <vm/vm.h> 54 55#include <machine/bus.h> 56 57#include <machine/bus_pio.h> 58#include <i386/isa/isa_device.h> 59 60#include <machine/dvcfg.h> 61 62#if defined(__FreeBSD__) && __FreeBSD_version < 400001 63static struct stg_softc *stg_get_softc(int); 64extern struct stg_softc *stgdata[]; 65#define DEVPORT_ALLOCSOFTCFUNC stg_get_softc 66#define DEVPORT_SOFTCARRAY stgdata 67#endif 68#include <sys/device_port.h> 69 70#include <cam/scsi/scsi_low.h> 71#include <cam/scsi/scsi_low_pisa.h> 72 73#include <dev/stg/tmc18c30reg.h> 74#include <dev/stg/tmc18c30var.h> 75#if defined(__FreeBSD__) && __FreeBSD_version < 400001 76#include "stg.h" 77#endif 78 79#define STG_HOSTID 7 80 81/* pccard support */ 82#include "apm.h" 83#if NAPM > 0 84#include <machine/apm_bios.h> 85#endif 86 87#include "card.h" 88#if NCARD > 0 89#include <sys/kernel.h> 90#include <sys/module.h> 91#if !defined(__FreeBSD__) || __FreeBSD_version < 500014 92#include <sys/select.h> 93#endif 94#include <pccard/cardinfo.h> 95#include <pccard/slot.h> 96 97static int stgprobe(DEVPORT_PDEVICE devi); 98static int stgattach(DEVPORT_PDEVICE devi); 99 100static int stg_card_intr __P((DEVPORT_PDEVICE)); 101static void stg_card_unload __P((DEVPORT_PDEVICE)); 102#if defined(__FreeBSD__) && __FreeBSD_version < 400001 103static int stg_card_init __P((DEVPORT_PDEVICE)); 104#endif 105 106#if defined(__FreeBSD__) && __FreeBSD_version >= 400001 107/* 108 * Additional code for FreeBSD new-bus PCCard frontend 109 */ 110 111static void 112stg_pccard_intr(void * arg) 113{ 114 stgintr(arg); 115} 116 117static void 118stg_release_resource(DEVPORT_PDEVICE dev) 119{ 120 struct stg_softc *sc = device_get_softc(dev); 121 122 if (sc->stg_intrhand) { 123 bus_teardown_intr(dev, sc->irq_res, sc->stg_intrhand); 124 } 125 126 if (sc->port_res) { 127 bus_release_resource(dev, SYS_RES_IOPORT, 128 sc->port_rid, sc->port_res); 129 } 130 131 if (sc->irq_res) { 132 bus_release_resource(dev, SYS_RES_IRQ, 133 sc->irq_rid, sc->irq_res); 134 } 135 136 if (sc->mem_res) { 137 bus_release_resource(dev, SYS_RES_MEMORY, 138 sc->mem_rid, sc->mem_res); 139 } 140} 141 142static int 143stg_alloc_resource(DEVPORT_PDEVICE dev) 144{ 145 struct stg_softc *sc = device_get_softc(dev); 146 u_long maddr, msize; 147 int error; 148 149 sc->port_rid = 0; 150 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid, 151 0, ~0, STGIOSZ, RF_ACTIVE); 152 if (sc->port_res == NULL) { 153 stg_release_resource(dev); 154 return(ENOMEM); 155 } 156 157 sc->irq_rid = 0; 158 sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid, 159 0, ~0, 1, RF_ACTIVE); 160 if (sc->irq_res == NULL) { 161 stg_release_resource(dev); 162 return(ENOMEM); 163 } 164 165 error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize); 166 if (error) { 167 return(0); /* XXX */ 168 } 169 170 /* no need to allocate memory if not configured */ 171 if (maddr == 0 || msize == 0) { 172 return(0); 173 } 174 175 sc->mem_rid = 0; 176 sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid, 177 0, ~0, msize, RF_ACTIVE); 178 if (sc->mem_res == NULL) { 179 stg_release_resource(dev); 180 return(ENOMEM); 181 } 182 183 return(0); 184} 185 186static int 187stg_pccard_probe(DEVPORT_PDEVICE dev) 188{ 189 struct stg_softc *sc = device_get_softc(dev); 190 int error; 191 192 bzero(sc, sizeof(struct stg_softc)); 193 194 error = stg_alloc_resource(dev); 195 if (error) { 196 return(error); 197 } 198 199 if (stgprobe(dev) == 0) { 200 stg_release_resource(dev); 201 return(ENXIO); 202 } 203 204 stg_release_resource(dev); 205 206 return(0); 207} 208 209static int 210stg_pccard_attach(DEVPORT_PDEVICE dev) 211{ 212 struct stg_softc *sc = device_get_softc(dev); 213 int error; 214 215 error = stg_alloc_resource(dev); 216 if (error) { 217 return(error); 218 } 219 220 error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM, 221 stg_pccard_intr, (void *)sc, &sc->stg_intrhand); 222 if (error) { 223 stg_release_resource(dev); 224 return(error); 225 } 226 227 if (stgattach(dev) == 0) { 228 stg_release_resource(dev); 229 return(ENXIO); 230 } 231 232 return(0); 233} 234 235static void 236stg_pccard_detach(DEVPORT_PDEVICE dev) 237{ 238 stg_card_unload(dev); 239 stg_release_resource(dev); 240} 241 242static device_method_t stg_pccard_methods[] = { 243 /* Device interface */ 244 DEVMETHOD(device_probe, stg_pccard_probe), 245 DEVMETHOD(device_attach, stg_pccard_attach), 246 DEVMETHOD(device_detach, stg_pccard_detach), 247 248 { 0, 0 } 249}; 250 251static driver_t stg_pccard_driver = { 252 "stg", 253 stg_pccard_methods, 254 sizeof(struct stg_softc), 255}; 256 257static devclass_t stg_devclass; 258 259DRIVER_MODULE(stg, pccard, stg_pccard_driver, stg_devclass, 0, 0); 260 261#else 262 263PCCARD_MODULE(stg, stg_card_init,stg_card_unload, stg_card_intr, 0, cam_imask); 264 265#endif 266 267#if defined(__FreeBSD__) && __FreeBSD_version < 400001 268static struct stg_softc * 269stg_get_softc(int unit) 270{ 271 struct stg_softc *sc; 272 273 if (unit >= NSTG) { 274 return(NULL); 275 } 276 277 if (stgdata[unit] == NULL) { 278 sc = malloc(sizeof(struct stg_softc), M_TEMP,M_NOWAIT); 279 if (sc == NULL) { 280 printf("stg_get_softc: cannot malloc!\n"); 281 return(NULL); 282 } 283 stgdata[unit] = sc; 284 } else { 285 sc = stgdata[unit]; 286 } 287 288 return(sc); 289} 290 291static int 292stg_card_init(DEVPORT_PDEVICE devi) 293{ 294 int unit = DEVPORT_PDEVUNIT(devi); 295 296 if (NSTG <= unit) 297 return (ENODEV); 298 299 printf("probe stg\n"); 300 if (stgprobe(devi) == 0) 301 return (ENXIO); 302 303 printf("attach stg\n"); 304 if (stgattach(devi) == 0) 305 return (ENXIO); 306 307 return (0); 308} 309#endif 310 311static void 312stg_card_unload(DEVPORT_PDEVICE devi) 313{ 314 struct stg_softc *sc = DEVPORT_PDEVGET_SOFTC(devi); 315 316 printf("%s: unload\n",sc->sc_sclow.sl_xname); 317 scsi_low_deactivate((struct scsi_low_softc *)sc); 318 scsi_low_dettach(&sc->sc_sclow); 319} 320 321static int 322stg_card_intr(DEVPORT_PDEVICE devi) 323{ 324 stgintr(DEVPORT_PDEVGET_SOFTC(devi)); 325 return 1; 326} 327 328static int 329stgprobe(DEVPORT_PDEVICE devi) 330{ 331 int rv; 332#if defined(__FreeBSD__) && __FreeBSD_version >= 400001 333 struct stg_softc *sc = device_get_softc(devi); 334 335 rv = stgprobesubr(rman_get_bustag(sc->port_res), 336 rman_get_bushandle(sc->port_res), 337 DEVPORT_PDEVFLAGS(devi)); 338#else 339 rv = stgprobesubr(I386_BUS_SPACE_IO, 340 DEVPORT_PDEVIOBASE(devi), DEVPORT_PDEVFLAGS(devi)); 341#endif 342 343 return rv; 344} 345 346static int 347stgattach(DEVPORT_PDEVICE devi) 348{ 349 int unit = DEVPORT_PDEVUNIT(devi); 350 struct stg_softc *sc; 351 struct scsi_low_softc *slp; 352 u_int32_t flags = DEVPORT_PDEVFLAGS(devi); 353 u_int iobase = DEVPORT_PDEVIOBASE(devi); 354 355 char dvname[16]; 356 357 strcpy(dvname,"stg"); 358 359#if defined(__FreeBSD__) && __FreeBSD_version < 400001 360 if (unit >= NSTG) 361 { 362 printf("%s: unit number too high\n",dvname); 363 return (0); 364 } 365#endif 366 367 if (iobase == 0) 368 { 369 printf("%s: no ioaddr is given\n", dvname); 370 return (0); 371 } 372 373 sc = DEVPORT_PDEVALLOC_SOFTC(devi); 374 if (sc == NULL) { 375 return(0); 376 } 377 378 slp = &sc->sc_sclow; 379#if defined(__FreeBSD__) && __FreeBSD_version >= 400001 380 slp->sl_dev = devi; 381 sc->sc_iot = rman_get_bustag(sc->port_res); 382 sc->sc_ioh = rman_get_bushandle(sc->port_res); 383#else 384 bzero(sc, sizeof(struct stg_softc)); 385 strcpy(slp->sl_dev.dv_xname, dvname); 386 slp->sl_dev.dv_unit = unit; 387 sc->sc_iot = I386_BUS_SPACE_IO; 388 sc->sc_ioh = iobase; 389#endif 390 391 slp->sl_hostid = STG_HOSTID; 392 slp->sl_cfgflags = flags; 393 394 stgattachsubr(sc); 395 396 sc->sc_ih = stgintr; 397 398 return(STGIOSZ); 399} 400#endif /* NCARD>0 */ 401