1139749Simp/*-
2113205Smdodd * [Ported for FreeBSD]
3113205Smdodd *  Copyright (c) 2000
4113205Smdodd *      Noriaki Mitsunaga, Mitsuru Iwasaki and Takanori Watanabe.
5113205Smdodd *      All rights reserved.
6113205Smdodd * [NetBSD for NEC PC-98 series]
7113205Smdodd *  Copyright (c) 1996, 1997, 1998
8113205Smdodd *	NetBSD/pc98 porting staff. All rights reserved.
9113205Smdodd *  Copyright (c) 1996, 1997, 1998
10113205Smdodd *	Naofumi HONDA. All rights reserved.
11113205Smdodd *  Copyright (c) 1996, 1997, 1998
12113205Smdodd *	Kouichi Matsuda. All rights reserved.
13113205Smdodd *
14113205Smdodd *  Redistribution and use in source and binary forms, with or without
15113205Smdodd *  modification, are permitted provided that the following conditions
16113205Smdodd *  are met:
17113205Smdodd *  1. Redistributions of source code must retain the above copyright
18113205Smdodd *     notice, this list of conditions and the following disclaimer.
19113205Smdodd *  2. Redistributions in binary form must reproduce the above copyright
20113205Smdodd *     notice, this list of conditions and the following disclaimer in the
21113205Smdodd *     documentation and/or other materials provided with the distribution.
22113205Smdodd *  3. The name of the author may not be used to endorse or promote products
23113205Smdodd *     derived from this software without specific prior written permission.
24113205Smdodd *
25113205Smdodd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26113205Smdodd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27113205Smdodd * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28113205Smdodd * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
29113205Smdodd * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30113205Smdodd * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31113205Smdodd * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32113205Smdodd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33113205Smdodd * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34113205Smdodd * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35113205Smdodd * POSSIBILITY OF SUCH DAMAGE.
36113205Smdodd *
37113205Smdodd */
38113205Smdodd
39119420Sobrien#include <sys/cdefs.h>
40119420Sobrien__FBSDID("$FreeBSD: releng/11.0/sys/dev/stg/tmc18c30_subr.c 294883 2016-01-27 02:23:54Z jhibbits $");
41119420Sobrien
42113205Smdodd#include <sys/param.h>
43113205Smdodd#include <sys/systm.h>
44113205Smdodd#include <sys/kernel.h>
45113205Smdodd#include <sys/socket.h>
46113205Smdodd
47113205Smdodd#include <sys/module.h>
48113205Smdodd#include <sys/bus.h>
49113205Smdodd
50113205Smdodd#include <machine/bus.h>
51113205Smdodd#include <machine/resource.h>
52113205Smdodd#include <sys/rman.h>
53113205Smdodd
54113205Smdodd#include <cam/scsi/scsi_low.h>
55113205Smdodd
56113205Smdodd#include <dev/stg/tmc18c30reg.h>
57113205Smdodd#include <dev/stg/tmc18c30var.h>
58113205Smdodd#include <dev/stg/tmc18c30.h>
59113205Smdodd
60113205Smdodd#define	STG_HOSTID	7
61113205Smdodd
62113205Smdodddevclass_t stg_devclass;
63113205Smdodd
64113205Smdoddint
65113205Smdoddstg_alloc_resource(device_t dev)
66113205Smdodd{
67113205Smdodd	struct stg_softc *	sc = device_get_softc(dev);
68294883Sjhibbits	rman_res_t		maddr, msize;
69113205Smdodd	int			error;
70113205Smdodd
71274760Sjhb	mtx_init(&sc->sc_sclow.sl_lock, "stg", NULL, MTX_DEF);
72127135Snjl	sc->port_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
73127135Snjl					      &sc->port_rid, RF_ACTIVE);
74113205Smdodd	if (sc->port_res == NULL) {
75113205Smdodd		stg_release_resource(dev);
76113205Smdodd		return(ENOMEM);
77113205Smdodd	}
78113205Smdodd
79127135Snjl	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
80127135Snjl					     RF_ACTIVE);
81113205Smdodd	if (sc->irq_res == NULL) {
82113205Smdodd		stg_release_resource(dev);
83113205Smdodd		return(ENOMEM);
84113205Smdodd	}
85113205Smdodd	error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
86113205Smdodd	if (error) {
87113205Smdodd		return(0);      /* XXX */
88113205Smdodd	}
89113205Smdodd
90113205Smdodd	/* no need to allocate memory if not configured */
91113205Smdodd	if (maddr == 0 || msize == 0) {
92113205Smdodd		return(0);
93113205Smdodd	}
94113205Smdodd
95127135Snjl	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
96127135Snjl					     RF_ACTIVE);
97113205Smdodd	if (sc->mem_res == NULL) {
98113205Smdodd		stg_release_resource(dev);
99113205Smdodd		return(ENOMEM);
100113205Smdodd	}
101113205Smdodd
102113205Smdodd	return(0);
103113205Smdodd}
104113205Smdodd
105113205Smdoddvoid
106113205Smdoddstg_release_resource(device_t dev)
107113205Smdodd{
108113205Smdodd	struct stg_softc	*sc = device_get_softc(dev);
109113205Smdodd
110113205Smdodd	if (sc->stg_intrhand)
111113205Smdodd		bus_teardown_intr(dev, sc->irq_res, sc->stg_intrhand);
112113205Smdodd	if (sc->port_res)
113113205Smdodd		bus_release_resource(dev, SYS_RES_IOPORT,
114113205Smdodd				     sc->port_rid, sc->port_res);
115113205Smdodd	if (sc->irq_res)
116113205Smdodd		bus_release_resource(dev, SYS_RES_IRQ,
117113205Smdodd				     sc->irq_rid, sc->irq_res);
118113205Smdodd	if (sc->mem_res)
119113205Smdodd		bus_release_resource(dev, SYS_RES_MEMORY,
120113205Smdodd				     sc->mem_rid, sc->mem_res);
121274760Sjhb	mtx_destroy(&sc->sc_sclow.sl_lock);
122113205Smdodd}
123113205Smdodd
124113205Smdoddint
125113205Smdoddstg_probe(device_t dev)
126113205Smdodd{
127113205Smdodd	int rv;
128113205Smdodd	struct stg_softc *sc = device_get_softc(dev);
129113205Smdodd
130274760Sjhb	rv = stgprobesubr(sc->port_res,
131113205Smdodd			  device_get_flags(dev));
132113205Smdodd
133113205Smdodd	return rv;
134113205Smdodd}
135113205Smdodd
136113205Smdoddint
137113205Smdoddstg_attach(device_t dev)
138113205Smdodd{
139113205Smdodd	struct stg_softc *sc;
140113205Smdodd	struct scsi_low_softc *slp;
141113205Smdodd	u_int32_t flags = device_get_flags(dev);
142113205Smdodd
143113205Smdodd	sc = device_get_softc(dev);
144113205Smdodd
145113205Smdodd	slp = &sc->sc_sclow;
146113205Smdodd	slp->sl_dev = dev;
147113205Smdodd
148113205Smdodd	slp->sl_hostid = STG_HOSTID;
149113205Smdodd	slp->sl_cfgflags = flags;
150113205Smdodd
151113205Smdodd	stgattachsubr(sc);
152113205Smdodd
153113205Smdodd	return(STGIOSZ);
154113205Smdodd}
155113205Smdodd
156194023Savgint
157274760Sjhbstg_detach(device_t dev)
158113205Smdodd{
159113205Smdodd	struct stg_softc *sc = device_get_softc(dev);
160113205Smdodd
161274760Sjhb	scsi_low_deactivate(&sc->sc_sclow);
162274760Sjhb	scsi_low_detach(&sc->sc_sclow);
163113205Smdodd	stg_release_resource(dev);
164194023Savg	return (0);
165113205Smdodd}
166113205Smdodd
167113205Smdoddvoid
168274760Sjhbstg_intr(void *arg)
169113205Smdodd{
170274760Sjhb	struct stg_softc *sc;
171274760Sjhb
172274760Sjhb	sc = arg;
173274760Sjhb	SCSI_LOW_LOCK(&sc->sc_sclow);
174274760Sjhb	stgintr(sc);
175274760Sjhb	SCSI_LOW_UNLOCK(&sc->sc_sclow);
176113205Smdodd}
177