tmc18c30_isa.c revision 67468
167468Snon/*	$FreeBSD: head/sys/dev/stg/tmc18c30_isa.c 67468 2000-10-23 12:55:51Z non $	*/
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#include	<sys/select.h>
8167468Snon
8267468Snonstatic	int	stgprobe(device_t devi);
8367468Snonstatic	int	stgattach(device_t devi);
8467468Snon
8567468Snonstatic	void	stg_isa_unload	__P((device_t));
8667468Snon
8767468Snonstatic void
8867468Snonstg_isa_intr(void * arg)
8967468Snon{
9067468Snon	stgintr(arg);
9167468Snon}
9267468Snon
9367468Snonstatic void
9467468Snonstg_release_resource(device_t dev)
9567468Snon{
9667468Snon	struct stg_softc	*sc = device_get_softc(dev);
9767468Snon
9867468Snon	if (sc->stg_intrhand) {
9967468Snon		bus_teardown_intr(dev, sc->irq_res, sc->stg_intrhand);
10067468Snon	}
10167468Snon
10267468Snon	if (sc->port_res) {
10367468Snon		bus_release_resource(dev, SYS_RES_IOPORT,
10467468Snon				     sc->port_rid, sc->port_res);
10567468Snon	}
10667468Snon
10767468Snon	if (sc->irq_res) {
10867468Snon		bus_release_resource(dev, SYS_RES_IRQ,
10967468Snon				     sc->irq_rid, sc->irq_res);
11067468Snon	}
11167468Snon
11267468Snon	if (sc->mem_res) {
11367468Snon		bus_release_resource(dev, SYS_RES_MEMORY,
11467468Snon				     sc->mem_rid, sc->mem_res);
11567468Snon	}
11667468Snon}
11767468Snon
11867468Snonstatic int
11967468Snonstg_alloc_resource(device_t dev)
12067468Snon{
12167468Snon	struct stg_softc	*sc = device_get_softc(dev);
12267468Snon	u_long			maddr, msize;
12367468Snon	int			error;
12467468Snon
12567468Snon	sc->port_rid = 0;
12667468Snon	sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
12767468Snon					  0, ~0, STGIOSZ, RF_ACTIVE);
12867468Snon	if (sc->port_res == NULL) {
12967468Snon		stg_release_resource(dev);
13067468Snon		return(ENOMEM);
13167468Snon	}
13267468Snon
13367468Snon	sc->irq_rid = 0;
13467468Snon	sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
13567468Snon					 0, ~0, 1, RF_ACTIVE);
13667468Snon	if (sc->irq_res == NULL) {
13767468Snon		stg_release_resource(dev);
13867468Snon		return(ENOMEM);
13967468Snon	}
14067468Snon
14167468Snon	error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
14267468Snon	if (error) {
14367468Snon		return(0);      /* XXX */
14467468Snon	}
14567468Snon
14667468Snon	/* no need to allocate memory if not configured */
14767468Snon	if (maddr == 0 || msize == 0) {
14867468Snon		return(0);
14967468Snon	}
15067468Snon
15167468Snon	sc->mem_rid = 0;
15267468Snon	sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid,
15367468Snon					 0, ~0, msize, RF_ACTIVE);
15467468Snon	if (sc->mem_res == NULL) {
15567468Snon		stg_release_resource(dev);
15667468Snon		return(ENOMEM);
15767468Snon	}
15867468Snon
15967468Snon	return(0);
16067468Snon}
16167468Snon
16267468Snonstatic int
16367468Snonstg_isa_probe(device_t dev)
16467468Snon{
16567468Snon	struct stg_softc	*sc = device_get_softc(dev);
16667468Snon	int			error;
16767468Snon
16867468Snon	bzero(sc, sizeof(struct stg_softc));
16967468Snon
17067468Snon	error = stg_alloc_resource(dev);
17167468Snon	if (error) {
17267468Snon		return(error);
17367468Snon	}
17467468Snon
17567468Snon	if (stgprobe(dev) == 0) {
17667468Snon		stg_release_resource(dev);
17767468Snon		return(ENXIO);
17867468Snon	}
17967468Snon
18067468Snon	stg_release_resource(dev);
18167468Snon
18267468Snon	return(0);
18367468Snon}
18467468Snon
18567468Snonstatic int
18667468Snonstg_isa_attach(device_t dev)
18767468Snon{
18867468Snon	struct stg_softc	*sc = device_get_softc(dev);
18967468Snon	int			error;
19067468Snon
19167468Snon	error = stg_alloc_resource(dev);
19267468Snon	if (error) {
19367468Snon		return(error);
19467468Snon	}
19567468Snon
19667468Snon	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM,
19767468Snon			       stg_isa_intr, (void *)sc, &sc->stg_intrhand);
19867468Snon	if (error) {
19967468Snon		stg_release_resource(dev);
20067468Snon		return(error);
20167468Snon	}
20267468Snon
20367468Snon	if (stgattach(dev) == 0) {
20467468Snon		stg_release_resource(dev);
20567468Snon		return(ENXIO);
20667468Snon	}
20767468Snon
20867468Snon	return(0);
20967468Snon}
21067468Snon
21167468Snonstatic	void
21267468Snonstg_isa_detach(device_t dev)
21367468Snon{
21467468Snon	stg_isa_unload(dev);
21567468Snon	stg_release_resource(dev);
21667468Snon}
21767468Snon
21867468Snonstatic device_method_t stg_isa_methods[] = {
21967468Snon	/* Device interface */
22067468Snon	DEVMETHOD(device_probe,		stg_isa_probe),
22167468Snon	DEVMETHOD(device_attach,	stg_isa_attach),
22267468Snon	DEVMETHOD(device_detach,	stg_isa_detach),
22367468Snon
22467468Snon	{ 0, 0 }
22567468Snon};
22667468Snon
22767468Snonstatic driver_t stg_isa_driver = {
22867468Snon	"stg",
22967468Snon	stg_isa_methods,
23067468Snon	sizeof(struct stg_softc),
23167468Snon};
23267468Snon
23367468Snonstatic devclass_t stg_devclass;
23467468Snon
23567468SnonDRIVER_MODULE(stg, isa, stg_isa_driver, stg_devclass, 0, 0);
23667468Snon
23767468Snonstatic	void
23867468Snonstg_isa_unload(device_t devi)
23967468Snon{
24067468Snon	struct stg_softc *sc = device_get_softc(devi);
24167468Snon
24267468Snon	printf("%s: unload\n",sc->sc_sclow.sl_xname);
24367468Snon	scsi_low_deactivate((struct scsi_low_softc *)sc);
24467468Snon	scsi_low_dettach(&sc->sc_sclow);
24567468Snon}
24667468Snon
24767468Snonstatic	int
24867468Snonstgprobe(device_t devi)
24967468Snon{
25067468Snon	int rv;
25167468Snon	struct stg_softc *sc = device_get_softc(devi);
25267468Snon
25367468Snon	rv = stgprobesubr(rman_get_bustag(sc->port_res),
25467468Snon			  rman_get_bushandle(sc->port_res),
25567468Snon			  device_get_flags(devi));
25667468Snon
25767468Snon	return rv;
25867468Snon}
25967468Snon
26067468Snonstatic	int
26167468Snonstgattach(device_t devi)
26267468Snon{
26367468Snon	struct stg_softc *sc;
26467468Snon	struct scsi_low_softc *slp;
26567468Snon	u_int32_t flags = device_get_flags(devi);
26667468Snon	u_int iobase = bus_get_resource_start(devi, SYS_RES_IOPORT, 0);
26767468Snon
26867468Snon	char	dvname[16];
26967468Snon
27067468Snon	strcpy(dvname,"stg");
27167468Snon
27267468Snon
27367468Snon	if (iobase == 0)
27467468Snon	{
27567468Snon		printf("%s: no ioaddr is given\n", dvname);
27667468Snon		return (0);
27767468Snon	}
27867468Snon
27967468Snon	sc = device_get_softc(devi);
28067468Snon	if (sc == NULL) {
28167468Snon		return(0);
28267468Snon	}
28367468Snon
28467468Snon	slp = &sc->sc_sclow;
28567468Snon	slp->sl_dev = devi;
28667468Snon	sc->sc_iot = rman_get_bustag(sc->port_res);
28767468Snon	sc->sc_ioh = rman_get_bushandle(sc->port_res);
28867468Snon
28967468Snon	slp->sl_hostid = STG_HOSTID;
29067468Snon	slp->sl_cfgflags = flags;
29167468Snon
29267468Snon	stgattachsubr(sc);
29367468Snon
29467468Snon	sc->sc_ih = stgintr;
29567468Snon
29667468Snon	printf("stg%d",device_get_unit(devi));
29767468Snon	return(STGIOSZ);
29867468Snon}
299