if_cm_isa.c revision 330897
1/*	$NetBSD: if_bah_zbus.c,v 1.6 2000/01/23 21:06:12 aymeric Exp $ */
2
3#include <sys/cdefs.h>
4__FBSDID("$FreeBSD: stable/11/sys/dev/cm/if_cm_isa.c 330897 2018-03-14 03:19:51Z eadler $");
5
6/*-
7 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
8 *
9 * Copyright (c) 1994, 1995, 1998 The NetBSD Foundation, Inc.
10 * All rights reserved.
11 *
12 * This code is derived from software contributed to The NetBSD Foundation
13 * by Ignatios Souvatzis.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 *    notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 *    notice, this list of conditions and the following disclaimer in the
22 *    documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/socket.h>
40#include <sys/kernel.h>
41#include <sys/malloc.h>
42
43#include <sys/module.h>
44#include <sys/bus.h>
45
46#include <machine/bus.h>
47#include <sys/rman.h>
48#include <machine/resource.h>
49
50#include <net/if.h>
51#include <net/if_var.h>
52#include <net/if_arc.h>
53
54#include <dev/cm/smc90cx6reg.h>
55#include <dev/cm/smc90cx6var.h>
56
57static int cm_isa_probe		(device_t);
58static int cm_isa_attach	(device_t);
59
60static int
61cm_isa_probe(dev)
62	device_t dev;
63{
64	struct cm_softc *sc = device_get_softc(dev);
65	int rid;
66
67	rid = 0;
68	sc->port_res = bus_alloc_resource_anywhere(
69	    dev, SYS_RES_IOPORT, &rid, CM_IO_PORTS, RF_ACTIVE);
70	if (sc->port_res == NULL)
71		return (ENOENT);
72
73	if (GETREG(CMSTAT) == 0xff) {
74		cm_release_resources(dev);
75		return (ENXIO);
76	}
77
78	rid = 0;
79	sc->mem_res = bus_alloc_resource_anywhere(
80	    dev, SYS_RES_MEMORY, &rid, CM_MEM_SIZE, RF_ACTIVE);
81	if (sc->mem_res == NULL) {
82		cm_release_resources(dev);
83		return (ENOENT);
84	}
85
86	rid = 0;
87	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
88	if (sc->irq_res == NULL) {
89		cm_release_resources(dev);
90		return (ENOENT);
91	}
92
93	return (0);
94}
95
96static int
97cm_isa_attach(dev)
98	device_t dev;
99{
100	struct cm_softc *sc = device_get_softc(dev);
101	int error;
102
103	/* init mtx and setup interrupt */
104	mtx_init(&sc->sc_mtx, device_get_nameunit(dev),
105	    MTX_NETWORK_LOCK, MTX_DEF);
106	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
107	    NULL, cmintr, sc, &sc->irq_handle);
108	if (error)
109		goto err;
110
111	/* attach */
112	error = cm_attach(dev);
113	if (error)
114		goto err;
115
116	return 0;
117
118err:
119	mtx_destroy(&sc->sc_mtx);
120	cm_release_resources(dev);
121	return (error);
122}
123
124static int
125cm_isa_detach(device_t dev)
126{
127	struct cm_softc *sc = device_get_softc(dev);
128	struct ifnet *ifp = sc->sc_ifp;
129
130	/* stop and detach */
131	CM_LOCK(sc);
132	cm_stop_locked(sc);
133	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
134	CM_UNLOCK(sc);
135
136	callout_drain(&sc->sc_recon_ch);
137	arc_ifdetach(ifp);
138
139	/* teardown interrupt, destroy mtx and release resources */
140	bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
141	mtx_destroy(&sc->sc_mtx);
142	if_free(ifp);
143	cm_release_resources(dev);
144
145	bus_generic_detach(dev);
146	return (0);
147}
148
149static device_method_t cm_isa_methods[] = {
150	/* Device interface */
151	DEVMETHOD(device_probe,		cm_isa_probe),
152	DEVMETHOD(device_attach,	cm_isa_attach),
153	DEVMETHOD(device_detach,	cm_isa_detach),
154
155	{ 0, 0 }
156};
157
158static driver_t cm_isa_driver = {
159	"cm",
160	cm_isa_methods,
161	sizeof(struct cm_softc)
162};
163
164DRIVER_MODULE(cm, isa, cm_isa_driver, cm_devclass, 0, 0);
165MODULE_DEPEND(cm, isa, 1, 1, 1);
166