tmc18c30_isa.c revision 73280
167468Snon/*	$FreeBSD: head/sys/dev/stg/tmc18c30_isa.c 73280 2001-03-01 17:09:09Z markm $	*/
267468Snon/*	$NecBSD: tmc18c30_pisa.c,v 1.22 1998/11/26 01:59:21 honda Exp $	*/
367468Snon/*	$NetBSD$	*/
467468Snon
567468Snon/*
667468Snon * [Ported for FreeBSD]
767468Snon *  Copyright (c) 2000
867468Snon *      Noriaki Mitsunaga, Mitsuru Iwasaki and Takanori Watanabe.
967468Snon *      All rights reserved.
1067468Snon * [NetBSD for NEC PC-98 series]
1167468Snon *  Copyright (c) 1996, 1997, 1998
1267468Snon *	NetBSD/pc98 porting staff. All rights reserved.
1367468Snon *  Copyright (c) 1996, 1997, 1998
1467468Snon *	Naofumi HONDA. All rights reserved.
1567468Snon *  Copyright (c) 1996, 1997, 1998
1667468Snon *	Kouichi Matsuda. All rights reserved.
1767468Snon *
1867468Snon *  Redistribution and use in source and binary forms, with or without
1967468Snon *  modification, are permitted provided that the following conditions
2067468Snon *  are met:
2167468Snon *  1. Redistributions of source code must retain the above copyright
2267468Snon *     notice, this list of conditions and the following disclaimer.
2367468Snon *  2. Redistributions in binary form must reproduce the above copyright
2467468Snon *     notice, this list of conditions and the following disclaimer in the
2567468Snon *     documentation and/or other materials provided with the distribution.
2667468Snon *  3. The name of the author may not be used to endorse or promote products
2767468Snon *     derived from this software without specific prior written permission.
2867468Snon *
2967468Snon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
3067468Snon * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
3167468Snon * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
3267468Snon * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
3367468Snon * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
3467468Snon * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
3567468Snon * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3667468Snon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3767468Snon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
3867468Snon * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3967468Snon * POSSIBILITY OF SUCH DAMAGE.
4067468Snon */
4167468Snon
4267468Snon#include <sys/param.h>
4367468Snon#include <sys/systm.h>
4467468Snon#include <sys/kernel.h>
4567468Snon#include <sys/module.h>
4667468Snon#include <sys/bus.h>
4767468Snon#include <sys/disklabel.h>
4867468Snon#if defined(__FreeBSD__) && __FreeBSD_version >= 500001
4967468Snon#include <sys/bio.h>
5067468Snon#endif
5167468Snon#include <sys/buf.h>
5267468Snon#include <sys/queue.h>
5367468Snon#include <sys/malloc.h>
5467468Snon#include <sys/errno.h>
5567468Snon
5667468Snon#include <vm/vm.h>
5767468Snon
5867468Snon#include <machine/bus_pio.h>
5967468Snon#include <machine/bus.h>
6067468Snon#include <machine/resource.h>
6167468Snon#include <sys/rman.h>
6267468Snon
6367468Snon#include <isa/isavar.h>
6467468Snon
6567468Snon#include <machine/dvcfg.h>
6667468Snon
6767468Snon#include <sys/device_port.h>
6867468Snon
6967468Snon#include <cam/scsi/scsi_low.h>
7067468Snon#include <isa/isa_common.h>
7167468Snon#include <cam/scsi/scsi_low_pisa.h>
7267468Snon
7367468Snon#include <dev/stg/tmc18c30reg.h>
7467468Snon#include <dev/stg/tmc18c30var.h>
7567468Snon
7667468Snon#define	STG_HOSTID	7
7767468Snon
7867468Snon#include	<sys/kernel.h>
7967468Snon#include	<sys/module.h>
8067468Snon
8167468Snonstatic	int	stgprobe(device_t devi);
8267468Snonstatic	int	stgattach(device_t devi);
8367468Snon
8467468Snonstatic	void	stg_isa_unload	__P((device_t));
8567468Snon
8667468Snonstatic void
8767468Snonstg_isa_intr(void * arg)
8867468Snon{
8967468Snon	stgintr(arg);
9067468Snon}
9167468Snon
9267468Snonstatic void
9367468Snonstg_release_resource(device_t dev)
9467468Snon{
9567468Snon	struct stg_softc	*sc = device_get_softc(dev);
9667468Snon
9767468Snon	if (sc->stg_intrhand) {
9867468Snon		bus_teardown_intr(dev, sc->irq_res, sc->stg_intrhand);
9967468Snon	}
10067468Snon
10167468Snon	if (sc->port_res) {
10267468Snon		bus_release_resource(dev, SYS_RES_IOPORT,
10367468Snon				     sc->port_rid, sc->port_res);
10467468Snon	}
10567468Snon
10667468Snon	if (sc->irq_res) {
10767468Snon		bus_release_resource(dev, SYS_RES_IRQ,
10867468Snon				     sc->irq_rid, sc->irq_res);
10967468Snon	}
11067468Snon
11167468Snon	if (sc->mem_res) {
11267468Snon		bus_release_resource(dev, SYS_RES_MEMORY,
11367468Snon				     sc->mem_rid, sc->mem_res);
11467468Snon	}
11567468Snon}
11667468Snon
11767468Snonstatic int
11867468Snonstg_alloc_resource(device_t dev)
11967468Snon{
12067468Snon	struct stg_softc	*sc = device_get_softc(dev);
12167468Snon	u_long			maddr, msize;
12267468Snon	int			error;
12367468Snon
12467468Snon	sc->port_rid = 0;
12567468Snon	sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
12667468Snon					  0, ~0, STGIOSZ, RF_ACTIVE);
12767468Snon	if (sc->port_res == NULL) {
12867468Snon		stg_release_resource(dev);
12967468Snon		return(ENOMEM);
13067468Snon	}
13167468Snon
13267468Snon	sc->irq_rid = 0;
13367468Snon	sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
13467468Snon					 0, ~0, 1, RF_ACTIVE);
13567468Snon	if (sc->irq_res == NULL) {
13667468Snon		stg_release_resource(dev);
13767468Snon		return(ENOMEM);
13867468Snon	}
13967468Snon
14067468Snon	error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
14167468Snon	if (error) {
14267468Snon		return(0);      /* XXX */
14367468Snon	}
14467468Snon
14567468Snon	/* no need to allocate memory if not configured */
14667468Snon	if (maddr == 0 || msize == 0) {
14767468Snon		return(0);
14867468Snon	}
14967468Snon
15067468Snon	sc->mem_rid = 0;
15167468Snon	sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid,
15267468Snon					 0, ~0, msize, RF_ACTIVE);
15367468Snon	if (sc->mem_res == NULL) {
15467468Snon		stg_release_resource(dev);
15567468Snon		return(ENOMEM);
15667468Snon	}
15767468Snon
15867468Snon	return(0);
15967468Snon}
16067468Snon
16167468Snonstatic int
16267468Snonstg_isa_probe(device_t dev)
16367468Snon{
16467468Snon	struct stg_softc	*sc = device_get_softc(dev);
16567468Snon	int			error;
16667468Snon
16767468Snon	bzero(sc, sizeof(struct stg_softc));
16867468Snon
16967468Snon	error = stg_alloc_resource(dev);
17067468Snon	if (error) {
17167468Snon		return(error);
17267468Snon	}
17367468Snon
17467468Snon	if (stgprobe(dev) == 0) {
17567468Snon		stg_release_resource(dev);
17667468Snon		return(ENXIO);
17767468Snon	}
17867468Snon
17967468Snon	stg_release_resource(dev);
18067468Snon
18167468Snon	return(0);
18267468Snon}
18367468Snon
18467468Snonstatic int
18567468Snonstg_isa_attach(device_t dev)
18667468Snon{
18767468Snon	struct stg_softc	*sc = device_get_softc(dev);
18867468Snon	int			error;
18967468Snon
19067468Snon	error = stg_alloc_resource(dev);
19167468Snon	if (error) {
19267468Snon		return(error);
19367468Snon	}
19467468Snon
19573280Smarkm	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM | INTR_ENTROPY,
19667468Snon			       stg_isa_intr, (void *)sc, &sc->stg_intrhand);
19767468Snon	if (error) {
19867468Snon		stg_release_resource(dev);
19967468Snon		return(error);
20067468Snon	}
20167468Snon
20267468Snon	if (stgattach(dev) == 0) {
20367468Snon		stg_release_resource(dev);
20467468Snon		return(ENXIO);
20567468Snon	}
20667468Snon
20767468Snon	return(0);
20867468Snon}
20967468Snon
21067468Snonstatic	void
21167468Snonstg_isa_detach(device_t dev)
21267468Snon{
21367468Snon	stg_isa_unload(dev);
21467468Snon	stg_release_resource(dev);
21567468Snon}
21667468Snon
21767468Snonstatic device_method_t stg_isa_methods[] = {
21867468Snon	/* Device interface */
21967468Snon	DEVMETHOD(device_probe,		stg_isa_probe),
22067468Snon	DEVMETHOD(device_attach,	stg_isa_attach),
22167468Snon	DEVMETHOD(device_detach,	stg_isa_detach),
22267468Snon
22367468Snon	{ 0, 0 }
22467468Snon};
22567468Snon
22667468Snonstatic driver_t stg_isa_driver = {
22767468Snon	"stg",
22867468Snon	stg_isa_methods,
22967468Snon	sizeof(struct stg_softc),
23067468Snon};
23167468Snon
23267468Snonstatic devclass_t stg_devclass;
23367468Snon
23467468SnonDRIVER_MODULE(stg, isa, stg_isa_driver, stg_devclass, 0, 0);
23567468Snon
23667468Snonstatic	void
23767468Snonstg_isa_unload(device_t devi)
23867468Snon{
23967468Snon	struct stg_softc *sc = device_get_softc(devi);
24067468Snon
24167468Snon	printf("%s: unload\n",sc->sc_sclow.sl_xname);
24267468Snon	scsi_low_deactivate((struct scsi_low_softc *)sc);
24367468Snon	scsi_low_dettach(&sc->sc_sclow);
24467468Snon}
24567468Snon
24667468Snonstatic	int
24767468Snonstgprobe(device_t devi)
24867468Snon{
24967468Snon	int rv;
25067468Snon	struct stg_softc *sc = device_get_softc(devi);
25167468Snon
25267468Snon	rv = stgprobesubr(rman_get_bustag(sc->port_res),
25367468Snon			  rman_get_bushandle(sc->port_res),
25467468Snon			  device_get_flags(devi));
25567468Snon
25667468Snon	return rv;
25767468Snon}
25867468Snon
25967468Snonstatic	int
26067468Snonstgattach(device_t devi)
26167468Snon{
26267468Snon	struct stg_softc *sc;
26367468Snon	struct scsi_low_softc *slp;
26467468Snon	u_int32_t flags = device_get_flags(devi);
26567468Snon	u_int iobase = bus_get_resource_start(devi, SYS_RES_IOPORT, 0);
26667468Snon
26767468Snon	char	dvname[16];
26867468Snon
26967468Snon	strcpy(dvname,"stg");
27067468Snon
27167468Snon
27267468Snon	if (iobase == 0)
27367468Snon	{
27467468Snon		printf("%s: no ioaddr is given\n", dvname);
27567468Snon		return (0);
27667468Snon	}
27767468Snon
27867468Snon	sc = device_get_softc(devi);
27967468Snon	if (sc == NULL) {
28067468Snon		return(0);
28167468Snon	}
28267468Snon
28367468Snon	slp = &sc->sc_sclow;
28467468Snon	slp->sl_dev = devi;
28567468Snon	sc->sc_iot = rman_get_bustag(sc->port_res);
28667468Snon	sc->sc_ioh = rman_get_bushandle(sc->port_res);
28767468Snon
28867468Snon	slp->sl_hostid = STG_HOSTID;
28967468Snon	slp->sl_cfgflags = flags;
29067468Snon
29167468Snon	stgattachsubr(sc);
29267468Snon
29367468Snon	sc->sc_ih = stgintr;
29467468Snon
29567468Snon	return(STGIOSZ);
29667468Snon}
297