tmc18c30_pccard.c revision 71260
1/* $FreeBSD: head/sys/dev/stg/tmc18c30_pccard.c 71260 2001-01-19 13:55:08Z peter $ */ 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 "card.h" 83#if NCARD > 0 84#include <sys/kernel.h> 85#include <sys/module.h> 86#if !defined(__FreeBSD__) || __FreeBSD_version < 500014 87#include <sys/select.h> 88#endif 89#include <pccard/cardinfo.h> 90#include <pccard/slot.h> 91 92static int stgprobe(DEVPORT_PDEVICE devi); 93static int stgattach(DEVPORT_PDEVICE devi); 94 95static int stg_card_intr __P((DEVPORT_PDEVICE)); 96static void stg_card_unload __P((DEVPORT_PDEVICE)); 97#if defined(__FreeBSD__) && __FreeBSD_version < 400001 98static int stg_card_init __P((DEVPORT_PDEVICE)); 99#endif 100 101#if defined(__FreeBSD__) && __FreeBSD_version >= 400001 102/* 103 * Additional code for FreeBSD new-bus PCCard frontend 104 */ 105 106static void 107stg_pccard_intr(void * arg) 108{ 109 stgintr(arg); 110} 111 112static void 113stg_release_resource(DEVPORT_PDEVICE dev) 114{ 115 struct stg_softc *sc = device_get_softc(dev); 116 117 if (sc->stg_intrhand) { 118 bus_teardown_intr(dev, sc->irq_res, sc->stg_intrhand); 119 } 120 121 if (sc->port_res) { 122 bus_release_resource(dev, SYS_RES_IOPORT, 123 sc->port_rid, sc->port_res); 124 } 125 126 if (sc->irq_res) { 127 bus_release_resource(dev, SYS_RES_IRQ, 128 sc->irq_rid, sc->irq_res); 129 } 130 131 if (sc->mem_res) { 132 bus_release_resource(dev, SYS_RES_MEMORY, 133 sc->mem_rid, sc->mem_res); 134 } 135} 136 137static int 138stg_alloc_resource(DEVPORT_PDEVICE dev) 139{ 140 struct stg_softc *sc = device_get_softc(dev); 141 u_long maddr, msize; 142 int error; 143 144 sc->port_rid = 0; 145 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid, 146 0, ~0, STGIOSZ, RF_ACTIVE); 147 if (sc->port_res == NULL) { 148 stg_release_resource(dev); 149 return(ENOMEM); 150 } 151 152 sc->irq_rid = 0; 153 sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid, 154 0, ~0, 1, RF_ACTIVE); 155 if (sc->irq_res == NULL) { 156 stg_release_resource(dev); 157 return(ENOMEM); 158 } 159 160 error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize); 161 if (error) { 162 return(0); /* XXX */ 163 } 164 165 /* no need to allocate memory if not configured */ 166 if (maddr == 0 || msize == 0) { 167 return(0); 168 } 169 170 sc->mem_rid = 0; 171 sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid, 172 0, ~0, msize, RF_ACTIVE); 173 if (sc->mem_res == NULL) { 174 stg_release_resource(dev); 175 return(ENOMEM); 176 } 177 178 return(0); 179} 180 181static int 182stg_pccard_probe(DEVPORT_PDEVICE dev) 183{ 184 struct stg_softc *sc = device_get_softc(dev); 185 int error; 186 187 bzero(sc, sizeof(struct stg_softc)); 188 189 error = stg_alloc_resource(dev); 190 if (error) { 191 return(error); 192 } 193 194 if (stgprobe(dev) == 0) { 195 stg_release_resource(dev); 196 return(ENXIO); 197 } 198 199 stg_release_resource(dev); 200 201 return(0); 202} 203 204static int 205stg_pccard_attach(DEVPORT_PDEVICE dev) 206{ 207 struct stg_softc *sc = device_get_softc(dev); 208 int error; 209 210 error = stg_alloc_resource(dev); 211 if (error) { 212 return(error); 213 } 214 215 error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM, 216 stg_pccard_intr, (void *)sc, &sc->stg_intrhand); 217 if (error) { 218 stg_release_resource(dev); 219 return(error); 220 } 221 222 if (stgattach(dev) == 0) { 223 stg_release_resource(dev); 224 return(ENXIO); 225 } 226 227 return(0); 228} 229 230static void 231stg_pccard_detach(DEVPORT_PDEVICE dev) 232{ 233 stg_card_unload(dev); 234 stg_release_resource(dev); 235} 236 237static device_method_t stg_pccard_methods[] = { 238 /* Device interface */ 239 DEVMETHOD(device_probe, stg_pccard_probe), 240 DEVMETHOD(device_attach, stg_pccard_attach), 241 DEVMETHOD(device_detach, stg_pccard_detach), 242 243 { 0, 0 } 244}; 245 246static driver_t stg_pccard_driver = { 247 "stg", 248 stg_pccard_methods, 249 sizeof(struct stg_softc), 250}; 251 252static devclass_t stg_devclass; 253 254DRIVER_MODULE(stg, pccard, stg_pccard_driver, stg_devclass, 0, 0); 255 256#else 257 258PCCARD_MODULE(stg, stg_card_init,stg_card_unload, stg_card_intr, 0, cam_imask); 259 260#endif 261 262#if defined(__FreeBSD__) && __FreeBSD_version < 400001 263static struct stg_softc * 264stg_get_softc(int unit) 265{ 266 struct stg_softc *sc; 267 268 if (unit >= NSTG) { 269 return(NULL); 270 } 271 272 if (stgdata[unit] == NULL) { 273 sc = malloc(sizeof(struct stg_softc), M_TEMP,M_NOWAIT); 274 if (sc == NULL) { 275 printf("stg_get_softc: cannot malloc!\n"); 276 return(NULL); 277 } 278 stgdata[unit] = sc; 279 } else { 280 sc = stgdata[unit]; 281 } 282 283 return(sc); 284} 285 286static int 287stg_card_init(DEVPORT_PDEVICE devi) 288{ 289 int unit = DEVPORT_PDEVUNIT(devi); 290 291 if (NSTG <= unit) 292 return (ENODEV); 293 294 printf("probe stg\n"); 295 if (stgprobe(devi) == 0) 296 return (ENXIO); 297 298 printf("attach stg\n"); 299 if (stgattach(devi) == 0) 300 return (ENXIO); 301 302 return (0); 303} 304#endif 305 306static void 307stg_card_unload(DEVPORT_PDEVICE devi) 308{ 309 struct stg_softc *sc = DEVPORT_PDEVGET_SOFTC(devi); 310 311 printf("%s: unload\n",sc->sc_sclow.sl_xname); 312 scsi_low_deactivate((struct scsi_low_softc *)sc); 313 scsi_low_dettach(&sc->sc_sclow); 314} 315 316static int 317stg_card_intr(DEVPORT_PDEVICE devi) 318{ 319 stgintr(DEVPORT_PDEVGET_SOFTC(devi)); 320 return 1; 321} 322 323static int 324stgprobe(DEVPORT_PDEVICE devi) 325{ 326 int rv; 327#if defined(__FreeBSD__) && __FreeBSD_version >= 400001 328 struct stg_softc *sc = device_get_softc(devi); 329 330 rv = stgprobesubr(rman_get_bustag(sc->port_res), 331 rman_get_bushandle(sc->port_res), 332 DEVPORT_PDEVFLAGS(devi)); 333#else 334 rv = stgprobesubr(I386_BUS_SPACE_IO, 335 DEVPORT_PDEVIOBASE(devi), DEVPORT_PDEVFLAGS(devi)); 336#endif 337 338 return rv; 339} 340 341static int 342stgattach(DEVPORT_PDEVICE devi) 343{ 344 int unit = DEVPORT_PDEVUNIT(devi); 345 struct stg_softc *sc; 346 struct scsi_low_softc *slp; 347 u_int32_t flags = DEVPORT_PDEVFLAGS(devi); 348 u_int iobase = DEVPORT_PDEVIOBASE(devi); 349 350 char dvname[16]; 351 352 strcpy(dvname,"stg"); 353 354#if defined(__FreeBSD__) && __FreeBSD_version < 400001 355 if (unit >= NSTG) 356 { 357 printf("%s: unit number too high\n",dvname); 358 return (0); 359 } 360#endif 361 362 if (iobase == 0) 363 { 364 printf("%s: no ioaddr is given\n", dvname); 365 return (0); 366 } 367 368 sc = DEVPORT_PDEVALLOC_SOFTC(devi); 369 if (sc == NULL) { 370 return(0); 371 } 372 373 slp = &sc->sc_sclow; 374#if defined(__FreeBSD__) && __FreeBSD_version >= 400001 375 slp->sl_dev = devi; 376 sc->sc_iot = rman_get_bustag(sc->port_res); 377 sc->sc_ioh = rman_get_bushandle(sc->port_res); 378#else 379 bzero(sc, sizeof(struct stg_softc)); 380 strcpy(slp->sl_dev.dv_xname, dvname); 381 slp->sl_dev.dv_unit = unit; 382 sc->sc_iot = I386_BUS_SPACE_IO; 383 sc->sc_ioh = iobase; 384#endif 385 386 slp->sl_hostid = STG_HOSTID; 387 slp->sl_cfgflags = flags; 388 389 stgattachsubr(sc); 390 391 sc->sc_ih = stgintr; 392 393 return(STGIOSZ); 394} 395#endif /* NCARD>0 */ 396