| 1 | /* $FreeBSD: src/sys/dev/nsp/nsp_pccard.c,v 1.2.2.6 2001/12/17 13:30:19 non Exp $ */ |
| 2 | /* $DragonFly: src/sys/dev/disk/nsp/nsp_pccard.c,v 1.4 2003/08/27 10:35:17 rob Exp $ */ |
| 3 | /* $NecBSD: nsp_pisa.c,v 1.4 1999/04/15 01:35:54 kmatsuda Exp $ */ |
| 4 | /* $NetBSD$ */ |
| 5 | |
| 6 | /* |
| 7 | * [Ported for FreeBSD] |
| 8 | * Copyright (c) 2000 |
| 9 | * Noriaki Mitsunaga, Mitsuru Iwasaki and Takanori Watanabe. |
| 10 | * All rights reserved. |
| 11 | * [NetBSD for NEC PC-98 series] |
| 12 | * Copyright (c) 1998 |
| 13 | * NetBSD/pc98 porting staff. All rights reserved. |
| 14 | * |
| 15 | * Redistribution and use in source and binary forms, with or without |
| 16 | * modification, are permitted provided that the following conditions |
| 17 | * are met: |
| 18 | * 1. Redistributions of source code must retain the above copyright |
| 19 | * notice, this list of conditions and the following disclaimer. |
| 20 | * 2. Redistributions in binary form must reproduce the above copyright |
| 21 | * notice, this list of conditions and the following disclaimer in the |
| 22 | * documentation and/or other materials provided with the distribution. |
| 23 | * 3. The name of the author may not be used to endorse or promote products |
| 24 | * derived from this software without specific prior written permission. |
| 25 | * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 27 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 28 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 29 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 30 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 31 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 32 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 34 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 35 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 36 | * POSSIBILITY OF SUCH DAMAGE. |
| 37 | */ |
| 38 | |
| 39 | #include <sys/param.h> |
| 40 | #include <sys/systm.h> |
| 41 | #include <sys/disklabel.h> |
| 42 | #if defined(__FreeBSD__) && __FreeBSD_version >= 500001 |
| 43 | #include <sys/bio.h> |
| 44 | #endif |
| 45 | #include <sys/buf.h> |
| 46 | #include <sys/queue.h> |
| 47 | #include <sys/malloc.h> |
| 48 | #include <sys/errno.h> |
| 49 | |
| 50 | #include <vm/vm.h> |
| 51 | |
| 52 | #include <machine/bus.h> |
| 53 | #include <machine/dvcfg.h> |
| 54 | |
| 55 | #include <sys/device_port.h> |
| 56 | |
| 57 | #include <bus/cam/scsi/scsi_low.h> |
| 58 | #include <bus/cam/scsi/scsi_low_pisa.h> |
| 59 | |
| 60 | #include "nspreg.h" |
| 61 | #include "nspvar.h" |
| 62 | |
| 63 | #define NSP_HOSTID 7 |
| 64 | |
| 65 | #include <sys/kernel.h> |
| 66 | #include <sys/module.h> |
| 67 | #if !defined(__FreeBSD__) || __FreeBSD_version < 500014 |
| 68 | #include <sys/select.h> |
| 69 | #endif |
| 70 | #include <bus/pccard/cardinfo.h> |
| 71 | #include <bus/pccard/slot.h> |
| 72 | |
| 73 | #define PIO_MODE 0x100 /* pd_flags */ |
| 74 | |
| 75 | static int nspprobe(DEVPORT_PDEVICE devi); |
| 76 | static int nspattach(DEVPORT_PDEVICE devi); |
| 77 | |
| 78 | static void nsp_card_unload (DEVPORT_PDEVICE); |
| 79 | |
| 80 | /* |
| 81 | * Additional code for FreeBSD new-bus PCCard frontend |
| 82 | */ |
| 83 | |
| 84 | static void |
| 85 | nsp_pccard_intr(void * arg) |
| 86 | { |
| 87 | nspintr(arg); |
| 88 | } |
| 89 | |
| 90 | static void |
| 91 | nsp_release_resource(DEVPORT_PDEVICE dev) |
| 92 | { |
| 93 | struct nsp_softc *sc = device_get_softc(dev); |
| 94 | |
| 95 | if (sc->nsp_intrhand) { |
| 96 | bus_teardown_intr(dev, sc->irq_res, sc->nsp_intrhand); |
| 97 | } |
| 98 | |
| 99 | if (sc->port_res) { |
| 100 | bus_release_resource(dev, SYS_RES_IOPORT, |
| 101 | sc->port_rid, sc->port_res); |
| 102 | } |
| 103 | |
| 104 | if (sc->irq_res) { |
| 105 | bus_release_resource(dev, SYS_RES_IRQ, |
| 106 | sc->irq_rid, sc->irq_res); |
| 107 | } |
| 108 | |
| 109 | if (sc->mem_res) { |
| 110 | bus_release_resource(dev, SYS_RES_MEMORY, |
| 111 | sc->mem_rid, sc->mem_res); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | static int |
| 116 | nsp_alloc_resource(DEVPORT_PDEVICE dev) |
| 117 | { |
| 118 | struct nsp_softc *sc = device_get_softc(dev); |
| 119 | u_long ioaddr, iosize, maddr, msize; |
| 120 | int error; |
| 121 | |
| 122 | error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize); |
| 123 | if (error || iosize < NSP_IOSIZE) |
| 124 | return(ENOMEM); |
| 125 | |
| 126 | sc->port_rid = 0; |
| 127 | sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid, |
| 128 | 0, ~0, NSP_IOSIZE, RF_ACTIVE); |
| 129 | if (sc->port_res == NULL) { |
| 130 | nsp_release_resource(dev); |
| 131 | return(ENOMEM); |
| 132 | } |
| 133 | |
| 134 | sc->irq_rid = 0; |
| 135 | sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid, |
| 136 | 0, ~0, 1, RF_ACTIVE); |
| 137 | if (sc->irq_res == NULL) { |
| 138 | nsp_release_resource(dev); |
| 139 | return(ENOMEM); |
| 140 | } |
| 141 | |
| 142 | error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize); |
| 143 | if (error) { |
| 144 | return(0); /* XXX */ |
| 145 | } |
| 146 | |
| 147 | /* No need to allocate memory if not configured and it's in PIO mode */ |
| 148 | if (maddr == 0 || msize == 0) { |
| 149 | if ((DEVPORT_PDEVFLAGS(dev) & PIO_MODE) == 0) { |
| 150 | printf("Memory window was not configured. Configure or use in PIO mode."); |
| 151 | nsp_release_resource(dev); |
| 152 | return(ENOMEM); |
| 153 | } |
| 154 | /* no need to allocate memory if PIO mode */ |
| 155 | return(0); |
| 156 | } |
| 157 | |
| 158 | sc->mem_rid = 0; |
| 159 | sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid, |
| 160 | 0, ~0, 1, RF_ACTIVE); |
| 161 | if (sc->mem_res == NULL) { |
| 162 | nsp_release_resource(dev); |
| 163 | return(ENOMEM); |
| 164 | } |
| 165 | |
| 166 | return(0); |
| 167 | } |
| 168 | |
| 169 | static int |
| 170 | nsp_pccard_probe(DEVPORT_PDEVICE dev) |
| 171 | { |
| 172 | struct nsp_softc *sc = device_get_softc(dev); |
| 173 | int error; |
| 174 | |
| 175 | bzero(sc, sizeof(struct nsp_softc)); |
| 176 | |
| 177 | error = nsp_alloc_resource(dev); |
| 178 | if (error) { |
| 179 | return(error); |
| 180 | } |
| 181 | |
| 182 | if (nspprobe(dev) == 0) { |
| 183 | nsp_release_resource(dev); |
| 184 | return(ENXIO); |
| 185 | } |
| 186 | |
| 187 | nsp_release_resource(dev); |
| 188 | |
| 189 | return(0); |
| 190 | } |
| 191 | |
| 192 | static int |
| 193 | nsp_pccard_attach(DEVPORT_PDEVICE dev) |
| 194 | { |
| 195 | struct nsp_softc *sc = device_get_softc(dev); |
| 196 | int error; |
| 197 | |
| 198 | error = nsp_alloc_resource(dev); |
| 199 | if (error) { |
| 200 | return(error); |
| 201 | } |
| 202 | |
| 203 | error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM, |
| 204 | nsp_pccard_intr, (void *)sc, &sc->nsp_intrhand); |
| 205 | if (error) { |
| 206 | nsp_release_resource(dev); |
| 207 | return(error); |
| 208 | } |
| 209 | |
| 210 | if (nspattach(dev) == 0) { |
| 211 | nsp_release_resource(dev); |
| 212 | return(ENXIO); |
| 213 | } |
| 214 | |
| 215 | return(0); |
| 216 | } |
| 217 | |
| 218 | static void |
| 219 | nsp_pccard_detach(DEVPORT_PDEVICE dev) |
| 220 | { |
| 221 | nsp_card_unload(dev); |
| 222 | nsp_release_resource(dev); |
| 223 | } |
| 224 | |
| 225 | static device_method_t nsp_pccard_methods[] = { |
| 226 | /* Device interface */ |
| 227 | DEVMETHOD(device_probe, nsp_pccard_probe), |
| 228 | DEVMETHOD(device_attach, nsp_pccard_attach), |
| 229 | DEVMETHOD(device_detach, nsp_pccard_detach), |
| 230 | |
| 231 | { 0, 0 } |
| 232 | }; |
| 233 | |
| 234 | static driver_t nsp_pccard_driver = { |
| 235 | "nsp", |
| 236 | nsp_pccard_methods, |
| 237 | sizeof(struct nsp_softc), |
| 238 | }; |
| 239 | |
| 240 | static devclass_t nsp_devclass; |
| 241 | |
| 242 | MODULE_DEPEND(nsp, scsi_low, 1, 1, 1); |
| 243 | DRIVER_MODULE(nsp, pccard, nsp_pccard_driver, nsp_devclass, 0, 0); |
| 244 | |
| 245 | static void |
| 246 | nsp_card_unload(DEVPORT_PDEVICE devi) |
| 247 | { |
| 248 | struct nsp_softc *sc = DEVPORT_PDEVGET_SOFTC(devi); |
| 249 | intrmask_t s; |
| 250 | |
| 251 | printf("%s: unload\n",sc->sc_sclow.sl_xname); |
| 252 | s = splcam(); |
| 253 | scsi_low_deactivate((struct scsi_low_softc *)sc); |
| 254 | scsi_low_dettach(&sc->sc_sclow); |
| 255 | splx(s); |
| 256 | } |
| 257 | |
| 258 | static int |
| 259 | nspprobe(DEVPORT_PDEVICE devi) |
| 260 | { |
| 261 | int rv; |
| 262 | struct nsp_softc *sc = device_get_softc(devi); |
| 263 | |
| 264 | rv = nspprobesubr(rman_get_bustag(sc->port_res), |
| 265 | rman_get_bushandle(sc->port_res), |
| 266 | DEVPORT_PDEVFLAGS(devi)); |
| 267 | |
| 268 | return rv; |
| 269 | } |
| 270 | |
| 271 | static int |
| 272 | nspattach(DEVPORT_PDEVICE devi) |
| 273 | { |
| 274 | struct nsp_softc *sc; |
| 275 | struct scsi_low_softc *slp; |
| 276 | u_int32_t flags = DEVPORT_PDEVFLAGS(devi); |
| 277 | u_int iobase = DEVPORT_PDEVIOBASE(devi); |
| 278 | intrmask_t s; |
| 279 | char dvname[16]; |
| 280 | |
| 281 | strcpy(dvname,"nsp"); |
| 282 | |
| 283 | if (iobase == 0) |
| 284 | { |
| 285 | printf("%s: no ioaddr is given\n", dvname); |
| 286 | return (0); |
| 287 | } |
| 288 | |
| 289 | sc = DEVPORT_PDEVALLOC_SOFTC(devi); |
| 290 | if (sc == NULL) { |
| 291 | return (0); |
| 292 | } |
| 293 | |
| 294 | slp = &sc->sc_sclow; |
| 295 | slp->sl_dev = devi; |
| 296 | sc->sc_iot = rman_get_bustag(sc->port_res); |
| 297 | sc->sc_ioh = rman_get_bushandle(sc->port_res); |
| 298 | |
| 299 | if((flags & PIO_MODE) == 0) { |
| 300 | sc->sc_memt = rman_get_bustag(sc->mem_res); |
| 301 | sc->sc_memh = rman_get_bushandle(sc->mem_res); |
| 302 | } else { |
| 303 | sc->sc_memh = 0; |
| 304 | } |
| 305 | /* slp->sl_irq = devi->pd_irq; */ |
| 306 | sc->sc_iclkdiv = CLKDIVR_20M; |
| 307 | sc->sc_clkdiv = CLKDIVR_40M; |
| 308 | |
| 309 | slp->sl_hostid = NSP_HOSTID; |
| 310 | slp->sl_cfgflags = flags; |
| 311 | |
| 312 | s = splcam(); |
| 313 | nspattachsubr(sc); |
| 314 | splx(s); |
| 315 | |
| 316 | return(NSP_IOSIZE); |
| 317 | } |