xref: /netbsd-src/sys/dev/pci/dpt_pci.c (revision d3cda6139a6a0678a420b8a6e1bef0e6269c13a3)
1*d3cda613Sjdolecek /*	$NetBSD: dpt_pci.c,v 1.28 2018/12/09 11:14:02 jdolecek Exp $	*/
248a0b623Sad 
348a0b623Sad /*
4b8702f53Skeihan  * Copyright (c) 1999, 2000, 2001 Andrew Doran <ad@NetBSD.org>
548a0b623Sad  * All rights reserved.
648a0b623Sad  *
748a0b623Sad  * Redistribution and use in source and binary forms, with or without
848a0b623Sad  * modification, are permitted provided that the following conditions
948a0b623Sad  * are met:
1048a0b623Sad  * 1. Redistributions of source code must retain the above copyright
1148a0b623Sad  *    notice, this list of conditions and the following disclaimer.
1248a0b623Sad  * 2. Redistributions in binary form must reproduce the above copyright
1348a0b623Sad  *    notice, this list of conditions and the following disclaimer in the
1448a0b623Sad  *    documentation and/or other materials provided with the distribution.
1548a0b623Sad  *
1648a0b623Sad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1748a0b623Sad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1848a0b623Sad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1948a0b623Sad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2048a0b623Sad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2148a0b623Sad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2248a0b623Sad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2348a0b623Sad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2448a0b623Sad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2548a0b623Sad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2648a0b623Sad  * SUCH DAMAGE.
2748a0b623Sad  *
2848a0b623Sad  */
2948a0b623Sad 
3048a0b623Sad /*
31f6e42db7Sad  * PCI front-end for DPT EATA SCSI driver.
3248a0b623Sad  */
3348a0b623Sad 
349048aaaeSlukem #include <sys/cdefs.h>
35*d3cda613Sjdolecek __KERNEL_RCSID(0, "$NetBSD: dpt_pci.c,v 1.28 2018/12/09 11:14:02 jdolecek Exp $");
369048aaaeSlukem 
3748a0b623Sad #include <sys/param.h>
3848a0b623Sad #include <sys/systm.h>
3948a0b623Sad #include <sys/device.h>
4048a0b623Sad #include <sys/queue.h>
4148a0b623Sad 
42a2a38285Sad #include <sys/bus.h>
43a2a38285Sad #include <sys/intr.h>
4448a0b623Sad 
4548a0b623Sad #include <dev/scsipi/scsipi_all.h>
4648a0b623Sad #include <dev/scsipi/scsiconf.h>
4748a0b623Sad 
4848a0b623Sad #include <dev/pci/pcidevs.h>
4948a0b623Sad #include <dev/pci/pcivar.h>
5048a0b623Sad 
5148a0b623Sad #include <dev/ic/dptreg.h>
5248a0b623Sad #include <dev/ic/dptvar.h>
5348a0b623Sad 
5459773deaSad #include <dev/i2o/dptivar.h>
5559773deaSad 
5648a0b623Sad #define	PCI_CBMA	0x14	/* Configuration base memory address */
5748a0b623Sad #define	PCI_CBIO	0x10	/* Configuration base I/O address */
5848a0b623Sad 
59a591bc88Scegger static int	dpt_pci_match(device_t, cfdata_t, void *);
60a591bc88Scegger static void	dpt_pci_attach(device_t, device_t, void *);
6148a0b623Sad 
62cbab9cadSchs CFATTACH_DECL_NEW(dpt_pci, sizeof(struct dpt_softc),
639b2c2ec2Sthorpej     dpt_pci_match, dpt_pci_attach, NULL, NULL);
6448a0b623Sad 
65937a7a3eSbouyer static int
dpt_pci_match(device_t parent,cfdata_t match,void * aux)66a591bc88Scegger dpt_pci_match(device_t parent, cfdata_t match, void *aux)
6748a0b623Sad {
68c86b7572Sad 	struct pci_attach_args *pa;
69c86b7572Sad 
70c86b7572Sad 	pa = (struct pci_attach_args *)aux;
7148a0b623Sad 
7248a0b623Sad 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_DPT &&
7348a0b623Sad 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_DPT_SC_RAID)
7448a0b623Sad 		return (1);
7548a0b623Sad 
7648a0b623Sad 	return (0);
7748a0b623Sad }
7848a0b623Sad 
79937a7a3eSbouyer static void
dpt_pci_attach(device_t parent,device_t self,void * aux)80a591bc88Scegger dpt_pci_attach(device_t parent, device_t self, void *aux)
8148a0b623Sad {
8248a0b623Sad 	struct pci_attach_args *pa;
8348a0b623Sad 	struct dpt_softc *sc;
8448a0b623Sad 	pci_chipset_tag_t pc;
8548a0b623Sad 	pci_intr_handle_t ih;
86e716b4baSad 	bus_space_handle_t ioh;
8748a0b623Sad 	const char *intrstr;
8848a0b623Sad 	pcireg_t csr;
89e58a356cSchristos 	char intrbuf[PCI_INTRSTR_LEN];
9048a0b623Sad 
917ec10e2dSthorpej 	aprint_naive(": Storage controller\n");
927ec10e2dSthorpej 
93b8169823Scegger 	sc = device_private(self);
94cbab9cadSchs 	sc->sc_dev = self;
9548a0b623Sad 	pa = (struct pci_attach_args *)aux;
9648a0b623Sad 	pc = pa->pa_pc;
977ec10e2dSthorpej 	aprint_normal(": ");
9848a0b623Sad 
9948a0b623Sad 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot,
100e716b4baSad 	    &ioh, NULL, NULL)) {
1017ec10e2dSthorpej 		aprint_error("can't map i/o space\n");
10248a0b623Sad 		return;
10348a0b623Sad 	}
10448a0b623Sad 
105937a7a3eSbouyer 	/* Need to map in by 16 registers. */
106e716b4baSad 	if (bus_space_subregion(sc->sc_iot, ioh, 16, 16, &sc->sc_ioh)) {
1077ec10e2dSthorpej 		aprint_error("can't map i/o subregion\n");
108e716b4baSad 		return;
109e716b4baSad 	}
110e716b4baSad 
11148a0b623Sad 	sc->sc_dmat = pa->pa_dmat;
11248a0b623Sad 
11348a0b623Sad 	/* Enable the device. */
11448a0b623Sad 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
11548a0b623Sad 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
11648a0b623Sad 		       csr | PCI_COMMAND_MASTER_ENABLE);
11748a0b623Sad 
11848a0b623Sad 	/* Map and establish the interrupt. */
119851de295Ssommerfeld 	if (pci_intr_map(pa, &ih)) {
1207ec10e2dSthorpej 		aprint_error("can't map interrupt\n");
12148a0b623Sad 		return;
12248a0b623Sad 	}
123e58a356cSchristos 	intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
124*d3cda613Sjdolecek 	sc->sc_ih = pci_intr_establish_xname(pc, ih, IPL_BIO, dpt_intr, sc,
125*d3cda613Sjdolecek 	    device_xname(self));
12648a0b623Sad 	if (sc->sc_ih == NULL) {
1277ec10e2dSthorpej 		aprint_error("can't establish interrupt");
12848a0b623Sad 		if (intrstr != NULL)
12985cadc23Snjoly 			aprint_error(" at %s", intrstr);
13085cadc23Snjoly 		aprint_error("\n");
13148a0b623Sad 		return;
13248a0b623Sad 	}
13348a0b623Sad 
134937a7a3eSbouyer 	/* Read the EATA configuration. */
135f6e42db7Sad 	if (dpt_readcfg(sc)) {
136cbab9cadSchs 		aprint_error_dev(sc->sc_dev, "readcfg failed - see dpt(4)\n");
137f6e42db7Sad 		return;
138f6e42db7Sad 	}
139f6e42db7Sad 
14059773deaSad 	sc->sc_bustype = SI_PCI_BUS;
14159773deaSad 
142937a7a3eSbouyer 	/* Now attach to the bus-independent code. */
14348a0b623Sad 	dpt_init(sc, intrstr);
14448a0b623Sad }
145