if_sbni_isa.c revision 119419
186752Sfjoe/*
286752Sfjoe * Copyright (c) 1997-2001 Granch, Ltd. All rights reserved.
386752Sfjoe * Author: Denis I.Timofeev <timofeev@granch.ru>
486752Sfjoe *
586752Sfjoe * Redistributon and use in source and binary forms, with or without
686752Sfjoe * modification, are permitted provided that the following conditions
786752Sfjoe * are met:
886752Sfjoe * 1. Redistributions of source code must retain the above copyright
986752Sfjoe *    notice unmodified, this list of conditions, and the following
1086752Sfjoe *    disclaimer.
1186752Sfjoe * 2. Redistributions in binary form must reproduce the above copyright
1286752Sfjoe *    notice, this list of conditions and the following disclaimer in the
1386752Sfjoe *    documentation and/or other materials provided with the distribution.
1486752Sfjoe *
1586752Sfjoe * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1686752Sfjoe * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1786752Sfjoe * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1886752Sfjoe * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1986752Sfjoe * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2086752Sfjoe * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2186752Sfjoe * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2286752Sfjoe * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2386752Sfjoe * LIABILITY, OR TORT (INCLUDING NEIGENCE OR OTHERWISE) ARISING IN ANY WAY
2486752Sfjoe * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2586752Sfjoe * SUCH DAMAGE.
2686752Sfjoe *
2786752Sfjoe */
2886752Sfjoe
29119419Sobrien#include <sys/cdefs.h>
30119419Sobrien__FBSDID("$FreeBSD: head/sys/dev/sbni/if_sbni_isa.c 119419 2003-08-24 18:03:45Z obrien $");
3186752Sfjoe
32119419Sobrien
3386752Sfjoe#include <sys/param.h>
3486752Sfjoe#include <sys/systm.h>
3586752Sfjoe#include <sys/socket.h>
3686752Sfjoe
3786752Sfjoe#include <sys/bus.h>
3886752Sfjoe#include <sys/kernel.h>
3986752Sfjoe#include <sys/module.h>
4086752Sfjoe#include <machine/bus.h>
4186752Sfjoe#include <machine/resource.h>
4286752Sfjoe#include <sys/rman.h>
4386752Sfjoe
4486752Sfjoe#include <net/if.h>
4586752Sfjoe#include <net/ethernet.h>
4686752Sfjoe#include <net/if_arp.h>
4786752Sfjoe
4886752Sfjoe#include <isa/isavar.h>
4986752Sfjoe
5086752Sfjoe#include <dev/sbni/if_sbnireg.h>
5186752Sfjoe#include <dev/sbni/if_sbnivar.h>
5286752Sfjoe
5386752Sfjoestatic int	sbni_probe_isa(device_t);
5486752Sfjoestatic int	sbni_attach_isa(device_t);
5586752Sfjoe
5686752Sfjoestatic device_method_t sbni_isa_methods[] = {
5786752Sfjoe	/* Device interface */
5886752Sfjoe	DEVMETHOD(device_probe,	sbni_probe_isa),
5986752Sfjoe	DEVMETHOD(device_attach, sbni_attach_isa),
6086752Sfjoe	{ 0, 0 }
6186752Sfjoe};
6286752Sfjoe
6386752Sfjoestatic driver_t sbni_isa_driver = {
6486752Sfjoe	"sbni",
6586752Sfjoe	sbni_isa_methods,
6686752Sfjoe	sizeof(struct sbni_softc)
6786752Sfjoe};
6886752Sfjoe
6986752Sfjoestatic devclass_t sbni_isa_devclass;
7086752Sfjoestatic struct isa_pnp_id  sbni_ids[] = {
7186752Sfjoe	{ 0, NULL }	/* we have no pnp sbni cards atm.  */
7286752Sfjoe};
7386752Sfjoe
74113506SmdoddDRIVER_MODULE(sbni, isa, sbni_isa_driver, sbni_isa_devclass, 0, 0);
75113506SmdoddMODULE_DEPEND(sbni, isa, 1, 1, 1);
7686752Sfjoe
7786752Sfjoestatic int
7886752Sfjoesbni_probe_isa(device_t dev)
7986752Sfjoe{
8086752Sfjoe	struct sbni_softc *sc;
8186752Sfjoe	int error;
8286752Sfjoe
8386752Sfjoe	error = ISA_PNP_PROBE(device_get_parent(dev), dev, sbni_ids);
8486752Sfjoe	if (error && error != ENOENT)
8586752Sfjoe		return (error);
8686752Sfjoe
8786752Sfjoe	sc = device_get_softc(dev);
8886752Sfjoe	bzero(sc, sizeof(struct sbni_softc));
8986752Sfjoe
9086752Sfjoe 	sc->io_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->io_rid,
9186752Sfjoe					0ul, ~0ul, SBNI_PORTS, RF_ACTIVE);
9286752Sfjoe	if (!sc->io_res) {
9386752Sfjoe		printf("sbni: cannot allocate io ports!\n");
9486752Sfjoe		return (ENOENT);
9586752Sfjoe	}
9686752Sfjoe
9786752Sfjoe	if (sbni_probe(sc) != 0) {
9886752Sfjoe		bus_release_resource(dev, SYS_RES_IOPORT,
9986752Sfjoe				     sc->io_rid, sc->io_res);
10086752Sfjoe		return (ENXIO);
10186752Sfjoe	}
10286752Sfjoe
10386752Sfjoe	device_quiet(dev);
10486752Sfjoe	return (0);
10586752Sfjoe}
10686752Sfjoe
10786752Sfjoe
10886752Sfjoestatic int
10986752Sfjoesbni_attach_isa(device_t dev)
11086752Sfjoe{
11186752Sfjoe	struct sbni_softc *sc;
11286752Sfjoe	struct sbni_flags flags;
11386752Sfjoe	int error;
11486752Sfjoe
11586752Sfjoe	sc = device_get_softc(dev);
11686752Sfjoe
117101432Sfjoe	printf("sbni%d: <Granch SBNI12/ISA adapter> port 0x%lx",
118101400Sfjoe	       next_sbni_unit, rman_get_start(sc->io_res));
119101393Sfjoe	sc->irq_res = bus_alloc_resource(
120101393Sfjoe	    dev, SYS_RES_IRQ, &sc->irq_rid, 0ul, ~0ul, 1, RF_ACTIVE);
12186752Sfjoe
12286752Sfjoe	if (sc->irq_res) {
12386752Sfjoe		printf(" irq %ld\n", rman_get_start(sc->irq_res));
124101393Sfjoe		error = bus_setup_intr(
125101393Sfjoe		    dev, sc->irq_res, INTR_TYPE_NET,
126101393Sfjoe		    sbni_intr, sc, &sc->irq_handle);
12786752Sfjoe		if (error) {
12886752Sfjoe			printf("sbni%d: bus_setup_intr\n", next_sbni_unit);
129101393Sfjoe			bus_release_resource(
130101393Sfjoe			    dev, SYS_RES_IOPORT, sc->io_rid, sc->io_res);
131101400Sfjoe			bus_release_resource(
132101400Sfjoe			    dev, SYS_RES_IRQ, sc->irq_rid, sc->irq_res);
13386752Sfjoe			return (error);
13486752Sfjoe		}
13586752Sfjoe
13686752Sfjoe#ifndef SBNI_DUAL_COMPOUND
13786752Sfjoe
13886752Sfjoe	} else {
13986752Sfjoe		printf("\nsbni%d: irq conflict!\n", next_sbni_unit);
14086752Sfjoe		bus_release_resource(dev, SYS_RES_IOPORT,
14186752Sfjoe				     sc->io_rid, sc->io_res);
14286752Sfjoe		return (ENOENT);
14386752Sfjoe	}
14486752Sfjoe
14586752Sfjoe#else	/* SBNI_DUAL_COMPOUND */
14686752Sfjoe
14789092Smsmith		sc->link = sbni_headlist;
14889092Smsmith		sbni_headlist = sc;
14986752Sfjoe	} else {
15086752Sfjoe		struct sbni_softc  *master;
15186752Sfjoe
15286752Sfjoe		if ((master = connect_to_master(sc)) == 0) {
15386752Sfjoe			printf("\nsbni%d: failed to alloc irq\n",
15486752Sfjoe			       next_sbni_unit);
155101393Sfjoe			bus_release_resource(
156101393Sfjoe			    dev, SYS_RES_IOPORT, sc->io_rid, sc->io_res);
15786752Sfjoe			return (ENXIO);
158101393Sfjoe		} else {
15986752Sfjoe			printf(" shared irq with sbni%d\n",
16086752Sfjoe			       master->arpcom.ac_if.if_unit);
161101393Sfjoe		}
16286752Sfjoe	}
16386752Sfjoe#endif	/* SBNI_DUAL_COMPOUND */
16486752Sfjoe
16586752Sfjoe	*(u_int32_t*)&flags = device_get_flags(dev);
16686752Sfjoe
16786752Sfjoe	sbni_attach(sc, next_sbni_unit++, flags);
16886752Sfjoe	return (0);
16986752Sfjoe}
170