bt_pci.c revision 45791
1492SN/A/*
22518Sihse * Product specific probe and attach routines for:
3492SN/A *      Buslogic BT946, BT948, BT956, BT958 SCSI controllers
4492SN/A *
5492SN/A * Copyright (c) 1995, 1997, 1998 Justin T. Gibbs
6492SN/A * All rights reserved.
7492SN/A *
8492SN/A * Redistribution and use in source and binary forms, with or without
9492SN/A * modification, are permitted provided that the following conditions
10492SN/A * are met:
11492SN/A * 1. Redistributions of source code must retain the above copyright
12492SN/A *    notice, this list of conditions, and the following disclaimer,
13492SN/A *    without modification, immediately at the beginning of the file.
14492SN/A * 2. The name of the author may not be used to endorse or promote products
15492SN/A *    derived from this software without specific prior written permission.
16492SN/A *
17492SN/A * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18492SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19492SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20492SN/A * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21492SN/A * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22492SN/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23492SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24492SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25492SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261120Schegar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271120Schegar * SUCH DAMAGE.
281120Schegar *
291410Sihse *	$Id: bt_pci.c,v 1.4 1998/12/14 06:32:54 dillon Exp $
301120Schegar */
311120Schegar
321120Schegar#include "pci.h"
33492SN/A#if NPCI > 0
341410Sihse#include <sys/param.h>
351410Sihse#include <sys/systm.h>
361410Sihse#include <sys/kernel.h>
371410Sihse#include <sys/bus.h>
38492SN/A
39492SN/A#include <pci/pcireg.h>
40492SN/A#include <pci/pcivar.h>
411410Sihse
421120Schegar#include <machine/bus_memio.h>
43837SN/A#include <machine/bus_pio.h>
44910Sihse#include <machine/bus.h>
451131Serikj#include <machine/resource.h>
462518Sihse#include <sys/rman.h>
47492SN/A
481120Schegar#include <dev/buslogic/btreg.h>
491236Sihse
501120Schegar#define BT_PCI_IOADDR	PCIR_MAPS
511120Schegar#define BT_PCI_MEMADDR	PCIR_MAPS + 4
521120Schegar
53968Sihse#define PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER	0x1040104Bul
54968Sihse#define PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER_NC	0x0140104Bul
55492SN/A#define PCI_DEVICE_ID_BUSLOGIC_FLASHPOINT	0x8130104Bul
561120Schegar
571804Serikjstatic int
581120Schegarbt_pci_alloc_resources(device_t dev)
591120Schegar{
601120Schegar	int		command, type = 0, rid, zero;
611120Schegar	struct resource *regs = 0;
621120Schegar	struct resource *irq = 0;
631120Schegar
641120Schegar	command = pci_read_config(dev, PCIR_COMMAND, /*bytes*/1);
651120Schegar#if 0
661120Schegar	/* XXX Memory Mapped I/O seems to cause problems */
671120Schegar	if (command & PCIM_CMD_MEMEN) {
681120Schegar		type = SYS_RES_MEMORY;
692108Serikj		rid = BT_PCI_MEMADDR;
702108Serikj		regs = bus_alloc_resource(dev, type, &rid,
712108Serikj					  0, ~0, 1, RF_ACTIVE);
722108Serikj	}
731223Schegar#else
741223Schegar	if (!regs && (command & PCIM_CMD_PORTEN)) {
752108Serikj		type = SYS_RES_IOPORT;
761120Schegar		rid = BT_PCI_IOADDR;
771120Schegar		regs = bus_alloc_resource(dev, type, &rid,
782108Serikj					  0, ~0, 1, RF_ACTIVE);
791120Schegar	}
801120Schegar#endif
812108Serikj	if (!regs)
821600Snaoto		return (ENOMEM);
831600Snaoto
842108Serikj	zero = 0;
851862Serikj	irq = bus_alloc_resource(dev, SYS_RES_IRQ, &zero,
862261Serikj				 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
872261Serikj	if (!irq) {
882261Serikj		bus_release_resource(dev, type, rid, regs);
892399Skvn		return (ENOMEM);
902399Skvn	}
912399Skvn
922108Serikj	bt_init_softc(dev, regs, irq, 0);
931120Schegar
941659Serikj	return (0);
952399Skvn}
962399Skvn
971120Schegarstatic void
981120Schegarbt_pci_release_resources(device_t dev)
991120Schegar{
1001120Schegar	struct bt_softc *bt = device_get_softc(dev);
1011120Schegar
1021120Schegar	if (bt->port)
1031120Schegar		/* XXX can't cope with memory registers anyway */
1041735Sbobv		bus_release_resource(dev, SYS_RES_IOPORT,
1051735Sbobv				     BT_PCI_IOADDR, bt->port);
1061735Sbobv	if (bt->irq)
1072264Serikj		bus_release_resource(dev, SYS_RES_IOPORT, 0, bt->irq);
108492SN/A	bt_free_softc(dev);
1091120Schegar}
1101120Schegar
1112108Serikjstatic int
1122108Serikjbt_pci_probe(device_t dev)
1132108Serikj{
1142108Serikj	switch (pci_get_devid(dev)) {
1152108Serikj		case PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER:
1162108Serikj		case PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER_NC:
1172108Serikj		{
1182108Serikj			struct bt_softc   *bt = device_get_softc(dev);
1192108Serikj			pci_info_data_t pci_info;
1202108Serikj			int error;
121492SN/A
1222108Serikj			error = bt_pci_alloc_resources(dev);
1232108Serikj			if (error)
1242108Serikj				return (error);
1252108Serikj
126557SN/A			/*
1272108Serikj			 * Determine if an ISA compatible I/O port has been
1282108Serikj			 * enabled.  If so, record the port so it will not
1292108Serikj			 * be probed by our ISA probe.  If the PCI I/O port
1301961Salanb			 * was not set to the compatibility port, disable it.
1312108Serikj			 */
1322108Serikj			error = bt_cmd(bt, BOP_INQUIRE_PCI_INFO,
1332108Serikj				       /*param*/NULL, /*paramlen*/0,
1341961Salanb				       (u_int8_t*)&pci_info, sizeof(pci_info),
1352108Serikj				       DEFAULT_CMD_TIMEOUT);
1362108Serikj			if (error == 0
1371961Salanb			 && pci_info.io_port < BIO_DISABLED) {
1381961Salanb				bt_mark_probed_bio(pci_info.io_port);
1391961Salanb				if (bt->bsh != bt_iop_from_bio(pci_info.io_port)) {
1402108Serikj					u_int8_t new_addr;
1412108Serikj
1421961Salanb					new_addr = BIO_DISABLED;
1432108Serikj					bt_cmd(bt, BOP_MODIFY_IO_ADDR,
1442108Serikj					       /*param*/&new_addr,
1451961Salanb					       /*paramlen*/1, /*reply_buf*/NULL,
1461120Schegar					       /*reply_len*/0,
1471120Schegar					       DEFAULT_CMD_TIMEOUT);
1481120Schegar				}
1491120Schegar			}
1501223Schegar			bt_pci_release_resources(dev);
1511223Schegar			device_set_desc(dev, "Buslogic Multi-Master SCSI Host Adapter");
1521223Schegar			return (0);
1531223Schegar		}
1541223Schegar		default:
1551223Schegar			break;
1561120Schegar	}
1571120Schegar
1581120Schegar	return (ENXIO);
1591120Schegar}
1601120Schegar
1611223Schegarstatic int
1621223Schegarbt_pci_attach(device_t dev)
1631223Schegar{
1641223Schegar	struct bt_softc   *bt = device_get_softc(dev);
1651223Schegar	int		   opri;
1661695Stwisti	int		   error;
1671695Stwisti
1681120Schegar	/* Initialise softc */
1691961Salanb	error = bt_pci_alloc_resources(dev);
1701961Salanb	if (error) {
1711961Salanb		device_printf(dev, "can't allocate resources in bt_pci_attach\n");
1721961Salanb		return error;
1731961Salanb	}
1741961Salanb
1751961Salanb	/* Allocate a dmatag for our CCB DMA maps */
1761961Salanb	/* XXX Should be a child of the PCI bus dma tag */
1771961Salanb	if (bus_dma_tag_create(/*parent*/NULL, /*alignemnt*/0, /*boundary*/0,
1781961Salanb			       /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1791961Salanb			       /*highaddr*/BUS_SPACE_MAXADDR,
1801961Salanb			       /*filter*/NULL, /*filterarg*/NULL,
1811961Salanb			       /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
1821961Salanb			       /*nsegments*/BUS_SPACE_UNRESTRICTED,
1831961Salanb			       /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
1841961Salanb			       /*flags*/0, &bt->parent_dmat) != 0) {
1851961Salanb		bt_pci_release_resources(dev);
186607SN/A		return (ENOMEM);
1871120Schegar	}
1881120Schegar
1891961Salanb	/*
1901120Schegar	 * Protect ourself from spurrious interrupts during
191492SN/A	 * intialization and attach.  We should really rely
1921120Schegar	 * on interrupts during attach, but we don't have
1931120Schegar	 * access to our interrupts during ISA probes, so until
1941790Serikj	 * that changes, we mask our interrupts during attach
1951790Serikj	 * too.
1961120Schegar	 */
1971120Schegar	opri = splcam();
1981120Schegar
1991120Schegar	if (bt_probe(dev) || bt_fetch_adapter_info(dev) || bt_init(dev)) {
2001120Schegar		bt_pci_release_resources(dev);
2011120Schegar		splx(opri);
2021120Schegar		return (ENXIO);
2031120Schegar	}
2041790Serikj
2051790Serikj	error = bt_attach(dev);
206492SN/A	splx(opri);
2071120Schegar
2081120Schegar	if (error) {
2091120Schegar		bt_pci_release_resources(dev);
2101120Schegar		return (error);
2111223Schegar	}
2121223Schegar
2131223Schegar	return (0);
2141223Schegar}
2151223Schegar
2161120Schegarstatic device_method_t bt_pci_methods[] = {
2171120Schegar	/* Device interface */
2181120Schegar	DEVMETHOD(device_probe,		bt_pci_probe),
2191120Schegar	DEVMETHOD(device_attach,	bt_pci_attach),
2201120Schegar
2211223Schegar	{ 0, 0 }
2221223Schegar};
2231223Schegar
2241223Schegarstatic driver_t bt_pci_driver = {
2251223Schegar	"bt",
2261223Schegar	bt_pci_methods,
227492SN/A	DRIVER_TYPE_CAM,
2281223Schegar	sizeof(struct bt_softc),
229492SN/A};
2301120Schegar
2311120Schegarstatic devclass_t bt_devclass;
2321223Schegar
2331223SchegarDRIVER_MODULE(bt, pci, bt_pci_driver, bt_devclass, 0, 0);
2341223Schegar
2351223Schegar#endif /* NPCI > 0 */
2361223Schegar