1139749Simp/*-
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$");
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
8986752Sfjoe 	sc->io_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->io_rid,
9086752Sfjoe					0ul, ~0ul, SBNI_PORTS, RF_ACTIVE);
9186752Sfjoe	if (!sc->io_res) {
9286752Sfjoe		printf("sbni: cannot allocate io ports!\n");
9386752Sfjoe		return (ENOENT);
9486752Sfjoe	}
9586752Sfjoe
9686752Sfjoe	if (sbni_probe(sc) != 0) {
97180263Sjhb		sbni_release_resources(sc);
9886752Sfjoe		return (ENXIO);
9986752Sfjoe	}
10086752Sfjoe
101180263Sjhb	device_set_desc(dev, "Granch SBNI12/ISA adapter");
10286752Sfjoe	return (0);
10386752Sfjoe}
10486752Sfjoe
10586752Sfjoe
10686752Sfjoestatic int
10786752Sfjoesbni_attach_isa(device_t dev)
10886752Sfjoe{
10986752Sfjoe	struct sbni_softc *sc;
11086752Sfjoe	struct sbni_flags flags;
11186752Sfjoe	int error;
11286752Sfjoe
11386752Sfjoe	sc = device_get_softc(dev);
114180263Sjhb	sc->dev = dev;
11586752Sfjoe
116127135Snjl	sc->irq_res = bus_alloc_resource_any(
117127135Snjl	    dev, SYS_RES_IRQ, &sc->irq_rid, RF_ACTIVE);
11886752Sfjoe
11986752Sfjoe#ifndef SBNI_DUAL_COMPOUND
12086752Sfjoe
121180263Sjhb	if (sc->irq_res == NULL) {
122180263Sjhb		device_printf(dev, "irq conflict!\n");
123180263Sjhb		sbni_release_resources(sc);
12486752Sfjoe		return (ENOENT);
12586752Sfjoe	}
12686752Sfjoe
12786752Sfjoe#else	/* SBNI_DUAL_COMPOUND */
12886752Sfjoe
129180263Sjhb	if (sc->irq_res) {
130180263Sjhb		sbni_add(sc);
13186752Sfjoe	} else {
13286752Sfjoe		struct sbni_softc  *master;
13386752Sfjoe
13486752Sfjoe		if ((master = connect_to_master(sc)) == 0) {
135180263Sjhb			device_printf(dev, "failed to alloc irq\n");
136180263Sjhb			sbni_release_resources(sc);
13786752Sfjoe			return (ENXIO);
138101393Sfjoe		} else {
139180263Sjhb			device_printf(dev, "shared irq with %s\n",
140147256Sbrooks			       master->ifp->if_xname);
141101393Sfjoe		}
14286752Sfjoe	}
14386752Sfjoe#endif	/* SBNI_DUAL_COMPOUND */
14486752Sfjoe
14586752Sfjoe	*(u_int32_t*)&flags = device_get_flags(dev);
14686752Sfjoe
147180263Sjhb	error = sbni_attach(sc, device_get_unit(dev) * 2, flags);
148180263Sjhb	if (error) {
149180263Sjhb		device_printf(dev, "cannot initialize driver\n");
150180263Sjhb		sbni_release_resources(sc);
151180263Sjhb		return (error);
152180263Sjhb	}
153180263Sjhb
154180263Sjhb	if (sc->irq_res) {
155180263Sjhb		error = bus_setup_intr(
156180263Sjhb		    dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
157180263Sjhb		    NULL, sbni_intr, sc, &sc->irq_handle);
158180263Sjhb		if (error) {
159180263Sjhb			device_printf(dev, "bus_setup_intr\n");
160180263Sjhb			sbni_detach(sc);
161180263Sjhb			sbni_release_resources(sc);
162180263Sjhb			return (error);
163180263Sjhb		}
164180263Sjhb	}
165180263Sjhb
16686752Sfjoe	return (0);
16786752Sfjoe}
168