if_sbni_isa.c revision 101400
1/*
2 * Copyright (c) 1997-2001 Granch, Ltd. All rights reserved.
3 * Author: Denis I.Timofeev <timofeev@granch.ru>
4 *
5 * Redistributon and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice unmodified, this list of conditions, and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/sys/dev/sbni/if_sbni_isa.c 101400 2002-08-05 18:14:16Z fjoe $
28 */
29
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/socket.h>
34
35#include <sys/bus.h>
36#include <sys/bus_private.h>
37#include <sys/kernel.h>
38#include <sys/module.h>
39#include <machine/bus.h>
40#include <machine/resource.h>
41#include <sys/rman.h>
42
43#include <net/if.h>
44#include <net/ethernet.h>
45#include <net/if_arp.h>
46
47#include <isa/isavar.h>
48
49#include <dev/sbni/if_sbnireg.h>
50#include <dev/sbni/if_sbnivar.h>
51
52static int	sbni_probe_isa(device_t);
53static int	sbni_attach_isa(device_t);
54
55static device_method_t sbni_isa_methods[] = {
56	/* Device interface */
57	DEVMETHOD(device_probe,	sbni_probe_isa),
58	DEVMETHOD(device_attach, sbni_attach_isa),
59	{ 0, 0 }
60};
61
62static driver_t sbni_isa_driver = {
63	"sbni",
64	sbni_isa_methods,
65	sizeof(struct sbni_softc)
66};
67
68static devclass_t sbni_isa_devclass;
69static struct isa_pnp_id  sbni_ids[] = {
70	{ 0, NULL }	/* we have no pnp sbni cards atm.  */
71};
72
73DRIVER_MODULE(if_sbni, isa, sbni_isa_driver, sbni_isa_devclass, 0, 0);
74
75
76static int
77sbni_probe_isa(device_t dev)
78{
79	struct sbni_softc *sc;
80	int error;
81
82	error = ISA_PNP_PROBE(device_get_parent(dev), dev, sbni_ids);
83	if (error && error != ENOENT)
84		return (error);
85
86	sc = device_get_softc(dev);
87	bzero(sc, sizeof(struct sbni_softc));
88
89 	sc->io_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->io_rid,
90					0ul, ~0ul, SBNI_PORTS, RF_ACTIVE);
91	if (!sc->io_res) {
92		printf("sbni: cannot allocate io ports!\n");
93		return (ENOENT);
94	}
95
96	if (sbni_probe(sc) != 0) {
97		bus_release_resource(dev, SYS_RES_IOPORT,
98				     sc->io_rid, sc->io_res);
99		return (ENXIO);
100	}
101
102	device_quiet(dev);
103	return (0);
104}
105
106
107static int
108sbni_attach_isa(device_t dev)
109{
110	struct sbni_softc *sc;
111	struct sbni_flags flags;
112	int error;
113
114	sc = device_get_softc(dev);
115
116	printf("sbni%d: <Granch SBNI12/ISA adapter> port 0x%x",
117	       next_sbni_unit, rman_get_start(sc->io_res));
118	sc->irq_res = bus_alloc_resource(
119	    dev, SYS_RES_IRQ, &sc->irq_rid, 0ul, ~0ul, 1, RF_ACTIVE);
120
121	if (sc->irq_res) {
122		printf(" irq %ld\n", rman_get_start(sc->irq_res));
123		error = bus_setup_intr(
124		    dev, sc->irq_res, INTR_TYPE_NET,
125		    sbni_intr, sc, &sc->irq_handle);
126		if (error) {
127			printf("sbni%d: bus_setup_intr\n", next_sbni_unit);
128			bus_release_resource(
129			    dev, SYS_RES_IOPORT, sc->io_rid, sc->io_res);
130			bus_release_resource(
131			    dev, SYS_RES_IRQ, sc->irq_rid, sc->irq_res);
132			return (error);
133		}
134
135#ifndef SBNI_DUAL_COMPOUND
136
137	} else {
138		printf("\nsbni%d: irq conflict!\n", next_sbni_unit);
139		bus_release_resource(dev, SYS_RES_IOPORT,
140				     sc->io_rid, sc->io_res);
141		return (ENOENT);
142	}
143
144#else	/* SBNI_DUAL_COMPOUND */
145
146		sc->link = sbni_headlist;
147		sbni_headlist = sc;
148	} else {
149		struct sbni_softc  *master;
150
151		if ((master = connect_to_master(sc)) == 0) {
152			printf("\nsbni%d: failed to alloc irq\n",
153			       next_sbni_unit);
154			bus_release_resource(
155			    dev, SYS_RES_IOPORT, sc->io_rid, sc->io_res);
156			return (ENXIO);
157		} else {
158			printf(" shared irq with sbni%d\n",
159			       master->arpcom.ac_if.if_unit);
160		}
161	}
162#endif	/* SBNI_DUAL_COMPOUND */
163
164	*(u_int32_t*)&flags = device_get_flags(dev);
165
166	sbni_attach(sc, next_sbni_unit++, flags);
167	return (0);
168}
169