spc_pcmcia.c revision 1.11
1/* $NetBSD: spc_pcmcia.c,v 1.11 2004/08/10 18:43:50 mycroft Exp $ */ 2 3/*- 4 * Copyright (c) 2000, 2004 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Charles M. Hannum. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39#include <sys/cdefs.h> 40__KERNEL_RCSID(0, "$NetBSD: spc_pcmcia.c,v 1.11 2004/08/10 18:43:50 mycroft Exp $"); 41 42#include <sys/param.h> 43#include <sys/systm.h> 44#include <sys/device.h> 45#include <sys/buf.h> 46 47#include <machine/bus.h> 48#include <machine/intr.h> 49 50#include <dev/scsipi/scsi_all.h> 51#include <dev/scsipi/scsipi_all.h> 52#include <dev/scsipi/scsiconf.h> 53 54#include <dev/pcmcia/pcmciareg.h> 55#include <dev/pcmcia/pcmciavar.h> 56#include <dev/pcmcia/pcmciadevs.h> 57 58#include <dev/ic/mb89352var.h> 59 60struct spc_pcmcia_softc { 61 struct spc_softc sc_spc; /* glue to MI code */ 62 63 /* PCMCIA-specific goo. */ 64 struct pcmcia_function *sc_pf; /* our PCMCIA function */ 65 void *sc_ih; /* interrupt handler */ 66 67 int sc_state; 68#define SPC_PCMCIA_ATTACH1 1 69#define SPC_PCMCIA_ATTACH2 2 70#define SPC_PCMCIA_ATTACHED 3 71}; 72 73int spc_pcmcia_match __P((struct device *, struct cfdata *, void *)); 74int spc_pcmcia_validate_config __P((struct pcmcia_config_entry *)); 75void spc_pcmcia_attach __P((struct device *, struct device *, void *)); 76int spc_pcmcia_detach __P((struct device *, int)); 77int spc_pcmcia_enable __P((struct device *, int)); 78 79CFATTACH_DECL(spc_pcmcia, sizeof(struct spc_pcmcia_softc), 80 spc_pcmcia_match, spc_pcmcia_attach, spc_pcmcia_detach, spc_activate); 81 82const struct pcmcia_product spc_pcmcia_products[] = { 83 { PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_SCSI600, 84 PCMCIA_CIS_FUJITSU_SCSI600 }, 85}; 86const size_t spc_pcmcia_nproducts = 87 sizeof(spc_pcmcia_products) / sizeof(spc_pcmcia_products[0]); 88 89int 90spc_pcmcia_match(parent, match, aux) 91 struct device *parent; 92 struct cfdata *match; 93 void *aux; 94{ 95 struct pcmcia_attach_args *pa = aux; 96 97 if (pcmcia_product_lookup(pa, spc_pcmcia_products, spc_pcmcia_nproducts, 98 sizeof(spc_pcmcia_products[0]), NULL)) 99 return (1); 100 return (0); 101} 102 103int 104spc_pcmcia_validate_config(cfe) 105 struct pcmcia_config_entry *cfe; 106{ 107 if (cfe->iftype != PCMCIA_IFTYPE_IO || 108 cfe->num_memspace != 0 || 109 cfe->num_iospace != 1) 110 return (EINVAL); 111 return (0); 112} 113 114void 115spc_pcmcia_attach(parent, self, aux) 116 struct device *parent, *self; 117 void *aux; 118{ 119 struct spc_pcmcia_softc *sc = (void *)self; 120 struct spc_softc *spc = (void *)self; 121 struct pcmcia_attach_args *pa = aux; 122 struct pcmcia_config_entry *cfe; 123 struct pcmcia_function *pf = pa->pf; 124 int error; 125 126 sc->sc_pf = pf; 127 128 error = pcmcia_function_configure(pf, spc_pcmcia_validate_config); 129 if (error) { 130 aprint_error("%s: configure failed, error=%d\n", self->dv_xname, 131 error); 132 return; 133 } 134 135 cfe = pf->cfe; 136 spc->sc_iot = cfe->iospace[0].handle.iot; 137 spc->sc_ioh = cfe->iospace[0].handle.ioh; 138 139 error = spc_pcmcia_enable(self, 1); 140 if (error) 141 goto fail; 142 143 spc->sc_initiator = 7; /* XXX */ 144 spc->sc_adapter.adapt_enable = spc_pcmcia_enable; 145 146 /* 147 * Initialize nca board itself. 148 */ 149 sc->sc_state = SPC_PCMCIA_ATTACH1; 150 spc_attach(spc); 151 if (sc->sc_state == SPC_PCMCIA_ATTACH1) 152 spc_pcmcia_enable(self, 0); 153 sc->sc_state = SPC_PCMCIA_ATTACHED; 154 return; 155 156fail: 157 pcmcia_function_unconfigure(pf); 158} 159 160int 161spc_pcmcia_detach(self, flags) 162 struct device *self; 163 int flags; 164{ 165 struct spc_pcmcia_softc *sc = (void *)self; 166 int error; 167 168 if (sc->sc_state != SPC_PCMCIA_ATTACHED) 169 return (0); 170 171 error = spc_detach(self, flags); 172 if (error) 173 return (error); 174 175 pcmcia_function_unconfigure(sc->sc_pf); 176 177 return (0); 178} 179 180int 181spc_pcmcia_enable(arg, onoff) 182 struct device *arg; 183 int onoff; 184{ 185 struct spc_pcmcia_softc *sc = (void *)arg; 186 int error; 187 188 if (onoff) { 189 /* 190 * If attach is in progress, we already have the device 191 * powered up. 192 */ 193 if (sc->sc_state == SPC_PCMCIA_ATTACH1) { 194 sc->sc_state = SPC_PCMCIA_ATTACH2; 195 } else { 196 /* Establish the interrupt handler. */ 197 sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO, 198 spc_intr, &sc->sc_spc); 199 if (!sc->sc_ih) 200 return (EIO); 201 202 error = pcmcia_function_enable(sc->sc_pf); 203 if (error) { 204 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih); 205 sc->sc_ih = 0; 206 return (error); 207 } 208 209 /* Initialize only chip. */ 210 spc_init(&sc->sc_spc, 0); 211 } 212 } else { 213 pcmcia_function_disable(sc->sc_pf); 214 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih); 215 sc->sc_ih = 0; 216 } 217 218 return (0); 219} 220