| 1 | /* $NetBSD: if_cs_pcmcia.c,v 1.22 2016/07/07 06:55:42 msaitoh Exp $ */ |
| 2 | |
| 3 | /*- |
| 4 | * Copyright (c)2001 YAMAMOTO Takashi, |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 22 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 23 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 24 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 25 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <sys/cdefs.h> |
| 30 | __KERNEL_RCSID(0, "$NetBSD: if_cs_pcmcia.c,v 1.22 2016/07/07 06:55:42 msaitoh Exp $" ); |
| 31 | |
| 32 | #include <sys/param.h> |
| 33 | #include <sys/systm.h> |
| 34 | #include <sys/device.h> |
| 35 | #include <sys/socket.h> |
| 36 | #include <sys/queue.h> |
| 37 | |
| 38 | #include <net/if.h> |
| 39 | #include <net/if_ether.h> |
| 40 | #include <net/if_media.h> |
| 41 | |
| 42 | #include <sys/bus.h> |
| 43 | #include <sys/intr.h> |
| 44 | |
| 45 | #include <dev/pcmcia/pcmciareg.h> |
| 46 | #include <dev/pcmcia/pcmciavar.h> |
| 47 | #include <dev/pcmcia/pcmciadevs.h> |
| 48 | |
| 49 | #include <dev/ic/cs89x0reg.h> |
| 50 | #include <dev/ic/cs89x0var.h> |
| 51 | |
| 52 | struct cs_pcmcia_softc; |
| 53 | |
| 54 | static int cs_pcmcia_match(device_t, cfdata_t, void *); |
| 55 | static int cs_pcmcia_validate_config(struct pcmcia_config_entry *); |
| 56 | static void cs_pcmcia_attach(device_t, device_t, void *); |
| 57 | static int cs_pcmcia_detach(device_t, int); |
| 58 | static int cs_pcmcia_enable(struct cs_softc *); |
| 59 | static void cs_pcmcia_disable(struct cs_softc *); |
| 60 | |
| 61 | struct cs_pcmcia_softc { |
| 62 | struct cs_softc sc_cs; /* real "cs" softc */ |
| 63 | |
| 64 | struct pcmcia_function *sc_pf; |
| 65 | |
| 66 | int sc_state; |
| 67 | #define CS_PCMCIA_ATTACHED 3 |
| 68 | }; |
| 69 | |
| 70 | CFATTACH_DECL_NEW(cs_pcmcia, sizeof(struct cs_pcmcia_softc), |
| 71 | cs_pcmcia_match, cs_pcmcia_attach, cs_pcmcia_detach, cs_activate); |
| 72 | |
| 73 | static int |
| 74 | cs_pcmcia_match(device_t parent, cfdata_t match, |
| 75 | void *aux) |
| 76 | { |
| 77 | struct pcmcia_attach_args *pa = aux; |
| 78 | |
| 79 | if (pa->manufacturer == PCMCIA_VENDOR_IBM && |
| 80 | pa->product == PCMCIA_PRODUCT_IBM_ETHERJET) |
| 81 | return (1); |
| 82 | return (0); |
| 83 | } |
| 84 | |
| 85 | static int |
| 86 | cs_pcmcia_validate_config(struct pcmcia_config_entry *cfe) |
| 87 | { |
| 88 | if (cfe->iftype != PCMCIA_IFTYPE_IO || |
| 89 | cfe->num_memspace != 0 || |
| 90 | cfe->num_iospace != 1 || |
| 91 | cfe->iospace[0].length < CS8900_IOSIZE) |
| 92 | return (EINVAL); |
| 93 | return (0); |
| 94 | } |
| 95 | |
| 96 | static void |
| 97 | cs_pcmcia_attach(device_t parent, device_t self, void *aux) |
| 98 | { |
| 99 | struct cs_pcmcia_softc *psc = device_private(self); |
| 100 | struct cs_softc *sc = &psc->sc_cs; |
| 101 | struct pcmcia_attach_args *pa = aux; |
| 102 | struct pcmcia_config_entry *cfe; |
| 103 | struct pcmcia_function *pf; |
| 104 | int error; |
| 105 | |
| 106 | sc->sc_dev = self; |
| 107 | pf = psc->sc_pf = pa->pf; |
| 108 | |
| 109 | error = pcmcia_function_configure(pa->pf, cs_pcmcia_validate_config); |
| 110 | if (error) { |
| 111 | aprint_error_dev(self, "configure failed, error=%d\n" , error); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | cfe = pf->cfe; |
| 116 | sc->sc_iot = cfe->iospace[0].handle.iot; |
| 117 | sc->sc_ioh = cfe->iospace[0].handle.ioh; |
| 118 | sc->sc_irq = -1; |
| 119 | #define CS_PCMCIA_HACK_FOR_CARDBUS |
| 120 | #ifdef CS_PCMCIA_HACK_FOR_CARDBUS |
| 121 | /* |
| 122 | * XXX is there a generic way to know if it's a cardbus or not? |
| 123 | */ |
| 124 | sc->sc_cfgflags |= CFGFLG_CARDBUS_HACK; |
| 125 | #endif |
| 126 | |
| 127 | error = cs_pcmcia_enable(sc); |
| 128 | if (error) |
| 129 | goto fail; |
| 130 | |
| 131 | sc->sc_enable = cs_pcmcia_enable; |
| 132 | sc->sc_disable = cs_pcmcia_disable; |
| 133 | |
| 134 | /* chip attach */ |
| 135 | error = cs_attach(sc, 0, 0, 0, 0); |
| 136 | if (error) |
| 137 | goto fail2; |
| 138 | |
| 139 | cs_pcmcia_disable(sc); |
| 140 | psc->sc_state = CS_PCMCIA_ATTACHED; |
| 141 | return; |
| 142 | |
| 143 | fail2: |
| 144 | cs_pcmcia_disable(sc); |
| 145 | fail: |
| 146 | pcmcia_function_unconfigure(pf); |
| 147 | } |
| 148 | |
| 149 | static int |
| 150 | cs_pcmcia_detach(device_t self, int flags) |
| 151 | { |
| 152 | struct cs_pcmcia_softc *psc = device_private(self); |
| 153 | struct cs_softc *sc = &psc->sc_cs; |
| 154 | int error; |
| 155 | |
| 156 | if (psc->sc_state != CS_PCMCIA_ATTACHED) |
| 157 | return (0); |
| 158 | |
| 159 | error = cs_detach(sc); |
| 160 | if (error) |
| 161 | return (error); |
| 162 | |
| 163 | pcmcia_function_unconfigure(psc->sc_pf); |
| 164 | |
| 165 | return (0); |
| 166 | } |
| 167 | |
| 168 | static int |
| 169 | cs_pcmcia_enable(struct cs_softc *sc) |
| 170 | { |
| 171 | struct cs_pcmcia_softc *psc = (void *)sc; |
| 172 | int error; |
| 173 | |
| 174 | sc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, cs_intr, sc); |
| 175 | if (!sc->sc_ih) |
| 176 | return (EIO); |
| 177 | |
| 178 | error = pcmcia_function_enable(psc->sc_pf); |
| 179 | if (error) { |
| 180 | pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih); |
| 181 | sc->sc_ih = 0; |
| 182 | } |
| 183 | |
| 184 | return (error); |
| 185 | } |
| 186 | |
| 187 | static void |
| 188 | cs_pcmcia_disable(struct cs_softc *sc) |
| 189 | { |
| 190 | struct cs_pcmcia_softc *psc = (void *)sc; |
| 191 | |
| 192 | pcmcia_function_disable(psc->sc_pf); |
| 193 | pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih); |
| 194 | sc->sc_ih = 0; |
| 195 | } |
| 196 | |