189099Sfjoe/*	$NetBSD: if_bah_zbus.c,v 1.6 2000/01/23 21:06:12 aymeric Exp $ */
289099Sfjoe
3119418Sobrien#include <sys/cdefs.h>
4119418Sobrien__FBSDID("$FreeBSD: stable/11/sys/dev/cm/if_cm_isa.c 331882 2018-04-02 16:11:49Z brooks $");
5119418Sobrien
689099Sfjoe/*-
789099Sfjoe * Copyright (c) 1994, 1995, 1998 The NetBSD Foundation, Inc.
889099Sfjoe * All rights reserved.
989099Sfjoe *
1089099Sfjoe * This code is derived from software contributed to The NetBSD Foundation
1189099Sfjoe * by Ignatios Souvatzis.
1289099Sfjoe *
1389099Sfjoe * Redistribution and use in source and binary forms, with or without
1489099Sfjoe * modification, are permitted provided that the following conditions
1589099Sfjoe * are met:
1689099Sfjoe * 1. Redistributions of source code must retain the above copyright
1789099Sfjoe *    notice, this list of conditions and the following disclaimer.
1889099Sfjoe * 2. Redistributions in binary form must reproduce the above copyright
1989099Sfjoe *    notice, this list of conditions and the following disclaimer in the
2089099Sfjoe *    documentation and/or other materials provided with the distribution.
2189099Sfjoe *
2289099Sfjoe * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2389099Sfjoe * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2489099Sfjoe * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2589099Sfjoe * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2689099Sfjoe * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2789099Sfjoe * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2889099Sfjoe * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2989099Sfjoe * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3089099Sfjoe * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3189099Sfjoe * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3289099Sfjoe * POSSIBILITY OF SUCH DAMAGE.
3389099Sfjoe */
3489099Sfjoe
3589099Sfjoe#include <sys/param.h>
3689099Sfjoe#include <sys/systm.h>
3789099Sfjoe#include <sys/socket.h>
3889099Sfjoe#include <sys/kernel.h>
39257289Sglebius#include <sys/malloc.h>
4089099Sfjoe
4189099Sfjoe#include <sys/module.h>
4289099Sfjoe#include <sys/bus.h>
4389099Sfjoe
4489099Sfjoe#include <machine/bus.h>
45159529Sfjoe#include <sys/rman.h>
46159529Sfjoe#include <machine/resource.h>
4789099Sfjoe
4889099Sfjoe#include <net/if.h>
49257289Sglebius#include <net/if_var.h>
5089099Sfjoe#include <net/if_arc.h>
5189099Sfjoe
52159529Sfjoe#include <dev/cm/smc90cx6reg.h>
5389099Sfjoe#include <dev/cm/smc90cx6var.h>
5489099Sfjoe
5592739Salfredstatic int cm_isa_probe		(device_t);
5692739Salfredstatic int cm_isa_attach	(device_t);
5789099Sfjoe
5889099Sfjoestatic int
5989099Sfjoecm_isa_probe(dev)
6089099Sfjoe	device_t dev;
6189099Sfjoe{
6289099Sfjoe	struct cm_softc *sc = device_get_softc(dev);
63159529Sfjoe	int rid;
6489099Sfjoe
65159529Sfjoe	rid = 0;
66296137Sjhibbits	sc->port_res = bus_alloc_resource_anywhere(
67296137Sjhibbits	    dev, SYS_RES_IOPORT, &rid, CM_IO_PORTS, RF_ACTIVE);
68159529Sfjoe	if (sc->port_res == NULL)
69159529Sfjoe		return (ENOENT);
7089099Sfjoe
71159529Sfjoe	if (GETREG(CMSTAT) == 0xff) {
72159529Sfjoe		cm_release_resources(dev);
73159529Sfjoe		return (ENXIO);
74159529Sfjoe	}
7589099Sfjoe
76159529Sfjoe	rid = 0;
77296137Sjhibbits	sc->mem_res = bus_alloc_resource_anywhere(
78296137Sjhibbits	    dev, SYS_RES_MEMORY, &rid, CM_MEM_SIZE, RF_ACTIVE);
79159529Sfjoe	if (sc->mem_res == NULL) {
80159529Sfjoe		cm_release_resources(dev);
81159529Sfjoe		return (ENOENT);
82159529Sfjoe	}
8389099Sfjoe
84159529Sfjoe	rid = 0;
85159529Sfjoe	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
86159529Sfjoe	if (sc->irq_res == NULL) {
87159529Sfjoe		cm_release_resources(dev);
88159529Sfjoe		return (ENOENT);
89159529Sfjoe	}
90159529Sfjoe
91159529Sfjoe	return (0);
9289099Sfjoe}
9389099Sfjoe
9489099Sfjoestatic int
9589099Sfjoecm_isa_attach(dev)
9689099Sfjoe	device_t dev;
9789099Sfjoe{
9889099Sfjoe	struct cm_softc *sc = device_get_softc(dev);
9989099Sfjoe	int error;
10089099Sfjoe
101159529Sfjoe	/* init mtx and setup interrupt */
102159529Sfjoe	mtx_init(&sc->sc_mtx, device_get_nameunit(dev),
103159529Sfjoe	    MTX_NETWORK_LOCK, MTX_DEF);
104159529Sfjoe	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
105166901Spiso	    NULL, cmintr, sc, &sc->irq_handle);
106159529Sfjoe	if (error)
107159529Sfjoe		goto err;
10889099Sfjoe
109159529Sfjoe	/* attach */
110159529Sfjoe	error = cm_attach(dev);
111159529Sfjoe	if (error)
112159529Sfjoe		goto err;
11389099Sfjoe
114331882Sbrooks	gone_in_dev(dev, 12, "cm(4) driver");
115159529Sfjoe	return 0;
116159529Sfjoe
117159529Sfjoeerr:
118159529Sfjoe	mtx_destroy(&sc->sc_mtx);
119159529Sfjoe	cm_release_resources(dev);
120159529Sfjoe	return (error);
12189099Sfjoe}
12289099Sfjoe
12389099Sfjoestatic int
12489099Sfjoecm_isa_detach(device_t dev)
12589099Sfjoe{
12689099Sfjoe	struct cm_softc *sc = device_get_softc(dev);
127147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
12889099Sfjoe
129159529Sfjoe	/* stop and detach */
130159529Sfjoe	CM_LOCK(sc);
131159529Sfjoe	cm_stop_locked(sc);
132148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
133159529Sfjoe	CM_UNLOCK(sc);
13489099Sfjoe
135159529Sfjoe	callout_drain(&sc->sc_recon_ch);
136147256Sbrooks	arc_ifdetach(ifp);
13789099Sfjoe
138159529Sfjoe	/* teardown interrupt, destroy mtx and release resources */
13989099Sfjoe	bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
140159529Sfjoe	mtx_destroy(&sc->sc_mtx);
141150306Simp	if_free(ifp);
14289099Sfjoe	cm_release_resources(dev);
14389099Sfjoe
144159529Sfjoe	bus_generic_detach(dev);
14589099Sfjoe	return (0);
14689099Sfjoe}
14789099Sfjoe
14889099Sfjoestatic device_method_t cm_isa_methods[] = {
14989099Sfjoe	/* Device interface */
15089099Sfjoe	DEVMETHOD(device_probe,		cm_isa_probe),
15189099Sfjoe	DEVMETHOD(device_attach,	cm_isa_attach),
15289099Sfjoe	DEVMETHOD(device_detach,	cm_isa_detach),
15389099Sfjoe
15489099Sfjoe	{ 0, 0 }
15589099Sfjoe};
15689099Sfjoe
15789099Sfjoestatic driver_t cm_isa_driver = {
15889099Sfjoe	"cm",
15989099Sfjoe	cm_isa_methods,
16089099Sfjoe	sizeof(struct cm_softc)
16189099Sfjoe};
16289099Sfjoe
163113506SmdoddDRIVER_MODULE(cm, isa, cm_isa_driver, cm_devclass, 0, 0);
164113506SmdoddMODULE_DEPEND(cm, isa, 1, 1, 1);
165