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: releng/11.0/sys/dev/sbni/if_sbni_isa.c 296137 2016-02-27 03:38:01Z jhibbits $");
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>
45257324Sglebius#include <net/if_var.h>
4686752Sfjoe#include <net/ethernet.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
89296137Sjhibbits 	sc->io_res = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT,
90296137Sjhibbits 						 &sc->io_rid, SBNI_PORTS,
91296137Sjhibbits 						 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) {
98180263Sjhb		sbni_release_resources(sc);
9986752Sfjoe		return (ENXIO);
10086752Sfjoe	}
10186752Sfjoe
102180263Sjhb	device_set_desc(dev, "Granch SBNI12/ISA adapter");
10386752Sfjoe	return (0);
10486752Sfjoe}
10586752Sfjoe
10686752Sfjoe
10786752Sfjoestatic int
10886752Sfjoesbni_attach_isa(device_t dev)
10986752Sfjoe{
11086752Sfjoe	struct sbni_softc *sc;
11186752Sfjoe	struct sbni_flags flags;
11286752Sfjoe	int error;
11386752Sfjoe
11486752Sfjoe	sc = device_get_softc(dev);
115180263Sjhb	sc->dev = dev;
11686752Sfjoe
117127135Snjl	sc->irq_res = bus_alloc_resource_any(
118127135Snjl	    dev, SYS_RES_IRQ, &sc->irq_rid, RF_ACTIVE);
11986752Sfjoe
12086752Sfjoe#ifndef SBNI_DUAL_COMPOUND
12186752Sfjoe
122180263Sjhb	if (sc->irq_res == NULL) {
123180263Sjhb		device_printf(dev, "irq conflict!\n");
124180263Sjhb		sbni_release_resources(sc);
12586752Sfjoe		return (ENOENT);
12686752Sfjoe	}
12786752Sfjoe
12886752Sfjoe#else	/* SBNI_DUAL_COMPOUND */
12986752Sfjoe
130180263Sjhb	if (sc->irq_res) {
131180263Sjhb		sbni_add(sc);
13286752Sfjoe	} else {
13386752Sfjoe		struct sbni_softc  *master;
13486752Sfjoe
13586752Sfjoe		if ((master = connect_to_master(sc)) == 0) {
136180263Sjhb			device_printf(dev, "failed to alloc irq\n");
137180263Sjhb			sbni_release_resources(sc);
13886752Sfjoe			return (ENXIO);
139101393Sfjoe		} else {
140180263Sjhb			device_printf(dev, "shared irq with %s\n",
141147256Sbrooks			       master->ifp->if_xname);
142101393Sfjoe		}
14386752Sfjoe	}
14486752Sfjoe#endif	/* SBNI_DUAL_COMPOUND */
14586752Sfjoe
14686752Sfjoe	*(u_int32_t*)&flags = device_get_flags(dev);
14786752Sfjoe
148180263Sjhb	error = sbni_attach(sc, device_get_unit(dev) * 2, flags);
149180263Sjhb	if (error) {
150180263Sjhb		device_printf(dev, "cannot initialize driver\n");
151180263Sjhb		sbni_release_resources(sc);
152180263Sjhb		return (error);
153180263Sjhb	}
154180263Sjhb
155180263Sjhb	if (sc->irq_res) {
156180263Sjhb		error = bus_setup_intr(
157180263Sjhb		    dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
158180263Sjhb		    NULL, sbni_intr, sc, &sc->irq_handle);
159180263Sjhb		if (error) {
160180263Sjhb			device_printf(dev, "bus_setup_intr\n");
161180263Sjhb			sbni_detach(sc);
162180263Sjhb			sbni_release_resources(sc);
163180263Sjhb			return (error);
164180263Sjhb		}
165180263Sjhb	}
166180263Sjhb
16786752Sfjoe	return (0);
16886752Sfjoe}
169