Home | History | Annotate | Line # | Download | only in isa
      1 /*	$NetBSD: if_fmv_isa.c,v 1.14 2026/02/03 21:34:56 andvar Exp $	*/
      2 
      3 /*
      4  * All Rights Reserved, Copyright (C) Fujitsu Limited 1995
      5  *
      6  * This software may be used, modified, copied, distributed, and sold, in
      7  * both source and binary form provided that the above copyright, these
      8  * terms and the following disclaimer are retained.  The name of the author
      9  * and/or the contributor may not be used to endorse or promote products
     10  * derived from this software without specific prior written permission.
     11  *
     12  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND THE CONTRIBUTOR ``AS IS'' AND
     13  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     14  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     15  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR THE CONTRIBUTOR BE LIABLE
     16  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     17  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     18  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION.
     19  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     20  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     21  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     22  * SUCH DAMAGE.
     23  */
     24 
     25 /*
     26  * Portions copyright (C) 1993, David Greenman.  This software may be used,
     27  * modified, copied, distributed, and sold, in both source and binary form
     28  * provided that the above copyright and these terms are retained.  Under no
     29  * circumstances is the author responsible for the proper functioning of this
     30  * software, nor does the author assume any responsibility for damages
     31  * incurred with its use.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: if_fmv_isa.c,v 1.14 2026/02/03 21:34:56 andvar Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/device.h>
     40 
     41 #include <net/if.h>
     42 #include <net/if_ether.h>
     43 #include <net/if_media.h>
     44 
     45 #include <sys/bus.h>
     46 #include <sys/intr.h>
     47 
     48 #include <dev/ic/mb86960reg.h>
     49 #include <dev/ic/mb86960var.h>
     50 #include <dev/ic/fmvreg.h>
     51 #include <dev/ic/fmvvar.h>
     52 
     53 #include <dev/isa/isavar.h>
     54 
     55 int	fmv_isa_match(device_t, cfdata_t, void *);
     56 void	fmv_isa_attach(device_t, device_t, void *);
     57 
     58 struct fmv_isa_softc {
     59 	struct	mb86960_softc sc_mb86960;	/* real "mb86960" softc */
     60 
     61 	/* ISA-specific goo. */
     62 	void	*sc_ih;				/* interrupt cookie */
     63 };
     64 
     65 CFATTACH_DECL_NEW(fmv_isa, sizeof(struct fmv_isa_softc),
     66     fmv_isa_match, fmv_isa_attach, NULL, NULL);
     67 
     68 struct fe_simple_probe_struct {
     69 	uint8_t port;	/* Offset from the base I/O address. */
     70 	uint8_t mask;	/* Bits to be checked. */
     71 	uint8_t bits;	/* Values to be compared against. */
     72 };
     73 
     74 static inline int fe_simple_probe(bus_space_tag_t, bus_space_handle_t,
     75     struct fe_simple_probe_struct const *);
     76 static int fmv_find(bus_space_tag_t, bus_space_handle_t, int *, int *);
     77 
     78 static const int fmv_iomap[8] = {
     79 	0x220, 0x240, 0x260, 0x280, 0x2A0, 0x2C0, 0x300, 0x340
     80 };
     81 #define NFMV_IOMAP __arraycount(fmv_iomap)
     82 #define FMV_NPORTS 0x20
     83 
     84 #ifdef FMV_DEBUG
     85 #define DPRINTF	printf
     86 #else
     87 #define DPRINTF	while (/* CONSTCOND */0) printf
     88 #endif
     89 
     90 /*
     91  * Hardware probe routines.
     92  */
     93 
     94 /*
     95  * Determine if the device is present.
     96  */
     97 int
     98 fmv_isa_match(device_t parent, cfdata_t cf, void *aux)
     99 {
    100 	struct isa_attach_args *ia = aux;
    101 	bus_space_tag_t iot = ia->ia_iot;
    102 	bus_space_handle_t ioh;
    103 	int i, iobase, irq, rv = 0;
    104 	uint8_t myea[ETHER_ADDR_LEN];
    105 
    106 	if (ia->ia_nio < 1)
    107 		return 0;
    108 	if (ia->ia_nirq < 1)
    109 		return 0;
    110 
    111 	if (ISA_DIRECT_CONFIG(ia))
    112 		return 0;
    113 
    114 	/* Disallow wildcarded values. */
    115 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
    116 		return 0;
    117 
    118 	/*
    119 	 * See if the specified address is valid for FMV-180 series.
    120 	 */
    121 	for (i = 0; i < NFMV_IOMAP; i++)
    122 		if (fmv_iomap[i] == ia->ia_io[0].ir_addr)
    123 			break;
    124 	if (i == NFMV_IOMAP) {
    125 		DPRINTF("%s: unknown iobase 0x%x\n",
    126 		    __func__, ia->ia_io[0].ir_addr);
    127 		return 0;
    128 	}
    129 
    130 	/* Map i/o space. */
    131 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, FMV_NPORTS, 0, &ioh)) {
    132 		DPRINTF("%s: couldn't map iospace 0x%x\n",
    133 		    __func__, ia->ia_io[0].ir_addr);
    134 		return 0;
    135 	}
    136 
    137 	if (fmv_find(iot, ioh, &iobase, &irq) == 0) {
    138 		DPRINTF("%s: fmv_find failed\n", __func__);
    139 		goto out;
    140 	}
    141 
    142 	if (iobase != ia->ia_io[0].ir_addr) {
    143 		DPRINTF("%s: unexpected iobase in board: 0x%x\n",
    144 		    __func__, iobase);
    145 		goto out;
    146 	}
    147 
    148 	if (fmv_detect(iot, ioh, myea) == 0) { /* XXX necessary? */
    149 		DPRINTF("%s: fmv_detect failed\n", __func__);
    150 		goto out;
    151 	}
    152 
    153 	if (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ) {
    154 		if (ia->ia_irq[0].ir_irq != irq) {
    155 			aprint_error("%s: irq mismatch; "
    156 			    "kernel configured %d != board configured %d\n",
    157 			    __func__, ia->ia_irq[0].ir_irq, irq);
    158 			goto out;
    159 		}
    160 	} else
    161 		ia->ia_irq[0].ir_irq = irq;
    162 
    163 	ia->ia_nio = 1;
    164 	ia->ia_io[0].ir_size = FMV_NPORTS;
    165 
    166 	ia->ia_nirq = 1;
    167 
    168 	ia->ia_niomem = 0;
    169 	ia->ia_ndrq = 0;
    170 
    171 	rv = 1;
    172 
    173  out:
    174 	bus_space_unmap(iot, ioh, FMV_NPORTS);
    175 	return rv;
    176 }
    177 
    178 /*
    179  * Check for specific bits in specific registers have specific values.
    180  */
    181 static inline int
    182 fe_simple_probe(bus_space_tag_t iot, bus_space_handle_t ioh,
    183     const struct fe_simple_probe_struct *sp)
    184 {
    185 	uint8_t val;
    186 	const struct fe_simple_probe_struct *p;
    187 
    188 	for (p = sp; p->mask != 0; p++) {
    189 		val = bus_space_read_1(iot, ioh, p->port);
    190 		if ((val & p->mask) != p->bits) {
    191 			DPRINTF("%s: %x & %x != %x\n",
    192 			    __func__, val, p->mask, p->bits);
    193 			return 0;
    194 		}
    195 	}
    196 
    197 	return 1;
    198 }
    199 
    200 /*
    201  * Hardware (vendor) specific probe routines.
    202  */
    203 
    204 /*
    205  * Probe Fujitsu FMV-180 series boards and get iobase and irq from
    206  * board.
    207  */
    208 static int
    209 fmv_find(bus_space_tag_t iot, bus_space_handle_t ioh, int *iobase, int *irq)
    210 {
    211 	uint8_t config;
    212 	static const int fmv_irqmap[4] = { 3, 7, 10, 15 };
    213 	static const struct fe_simple_probe_struct probe_table[] = {
    214 		{ FE_DLCR2, 0x70, 0x00 },
    215 		{ FE_DLCR4, 0x08, 0x00 },
    216 	    /*	{ FE_DLCR5, 0x80, 0x00 },	Doesn't work. */
    217 
    218 		{ FE_FMV0, FE_FMV0_MAGIC_MASK, FE_FMV0_MAGIC_VALUE },
    219 		{ FE_FMV1, FE_FMV1_MAGIC_MASK, FE_FMV1_MAGIC_VALUE },
    220 		{ FE_FMV3, FE_FMV3_EXTRA_MASK, FE_FMV3_EXTRA_VALUE },
    221 #if 1
    222 	/*
    223 	 * Test *vendor* part of the station address for Fujitsu.
    224 	 * The test will gain reliability of probe process, but
    225 	 * it rejects FMV-180 clone boards manufactured by other vendors.
    226 	 * We have to turn the test off when such cards are made available.
    227 	 */
    228 		{ FE_FMV4, 0xFF, 0x00 },
    229 		{ FE_FMV5, 0xFF, 0x00 },
    230 		{ FE_FMV6, 0xFF, 0x0E },
    231 #else
    232 	/*
    233 	 * We can always verify the *first* 2 bits (in Ehternet
    234 	 * bit order) are "no multicast" and "no local" even for
    235 	 * unknown vendors.
    236 	 */
    237 		{ FE_FMV4, 0x03, 0x00 },
    238 #endif
    239 		{ 0,	   0x00, 0x00 },
    240 	};
    241 
    242 	/* Simple probe. */
    243 	if (fe_simple_probe(iot, ioh, probe_table) == 0)
    244 		return 0;
    245 
    246 	/* Check if our I/O address matches config info on EEPROM. */
    247 	config = bus_space_read_1(iot, ioh, FE_FMV2);
    248 	*iobase = fmv_iomap[(config & FE_FMV2_ADDR) >> FE_FMV2_ADDR_SHIFT];
    249 
    250 	/*
    251 	 * Determine which IRQ to be used.
    252 	 *
    253 	 * In this version, we always get an IRQ assignment from the
    254 	 * FMV-180's configuration EEPROM, ignoring that specified in
    255 	 * config file.
    256 	 */
    257 	*irq = fmv_irqmap[(config & FE_FMV2_IRQ) >> FE_FMV2_IRQ_SHIFT];
    258 
    259 	return 1;
    260 }
    261 
    262 void
    263 fmv_isa_attach(device_t parent, device_t self, void *aux)
    264 {
    265 	struct fmv_isa_softc *isc = device_private(self);
    266 	struct mb86960_softc *sc = &isc->sc_mb86960;
    267 	struct isa_attach_args *ia = aux;
    268 	bus_space_tag_t iot = ia->ia_iot;
    269 	bus_space_handle_t ioh;
    270 
    271 	sc->sc_dev = self;
    272 
    273 	/* Map i/o space. */
    274 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, FMV_NPORTS, 0, &ioh)) {
    275 		aprint_error(": can't map i/o space\n");
    276 		return;
    277 	}
    278 
    279 	sc->sc_bst = iot;
    280 	sc->sc_bsh = ioh;
    281 
    282 	fmv_attach(sc);
    283 
    284 	/* Establish the interrupt handler. */
    285 	isc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
    286 	    IST_EDGE, IPL_NET, mb86960_intr, sc);
    287 	if (isc->sc_ih == NULL)
    288 		aprint_error_dev(sc->sc_dev,
    289 		    "couldn't establish interrupt handler\n");
    290 }
    291