1139749Simp/*-
297883Sgibbs * FreeBSD, PCI product support functions
397883Sgibbs *
497883Sgibbs * Copyright (c) 1995-2001 Justin T. Gibbs
597883Sgibbs * All rights reserved.
697883Sgibbs *
797883Sgibbs * Redistribution and use in source and binary forms, with or without
897883Sgibbs * modification, are permitted provided that the following conditions
997883Sgibbs * are met:
1097883Sgibbs * 1. Redistributions of source code must retain the above copyright
1197883Sgibbs *    notice, this list of conditions, and the following disclaimer,
1297883Sgibbs *    without modification, immediately at the beginning of the file.
1397883Sgibbs * 2. The name of the author may not be used to endorse or promote products
1497883Sgibbs *    derived from this software without specific prior written permission.
1597883Sgibbs *
1697883Sgibbs * Alternatively, this software may be distributed under the terms of the
1797883Sgibbs * GNU Public License ("GPL").
1897883Sgibbs *
1997883Sgibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2097883Sgibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2197883Sgibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2297883Sgibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
2397883Sgibbs * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2497883Sgibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2597883Sgibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2697883Sgibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2797883Sgibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2897883Sgibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2997883Sgibbs * SUCH DAMAGE.
3097883Sgibbs *
31123579Sgibbs * $Id: //depot/aic7xxx/freebsd/dev/aic7xxx/ahd_pci.c#17 $
3297883Sgibbs */
3397883Sgibbs
34119418Sobrien#include <sys/cdefs.h>
35119418Sobrien__FBSDID("$FreeBSD$");
36119418Sobrien
3797883Sgibbs#include <dev/aic7xxx/aic79xx_osm.h>
3897883Sgibbs
3997883Sgibbsstatic int ahd_pci_probe(device_t dev);
4097883Sgibbsstatic int ahd_pci_attach(device_t dev);
4197883Sgibbs
4297883Sgibbsstatic device_method_t ahd_pci_device_methods[] = {
4397883Sgibbs	/* Device interface */
4497883Sgibbs	DEVMETHOD(device_probe,		ahd_pci_probe),
4597883Sgibbs	DEVMETHOD(device_attach,	ahd_pci_attach),
4697883Sgibbs	DEVMETHOD(device_detach,	ahd_detach),
4797883Sgibbs	{ 0, 0 }
4897883Sgibbs};
4997883Sgibbs
5097883Sgibbsstatic driver_t ahd_pci_driver = {
5197883Sgibbs	"ahd",
5297883Sgibbs	ahd_pci_device_methods,
5397883Sgibbs	sizeof(struct ahd_softc)
5497883Sgibbs};
5597883Sgibbs
5697883Sgibbsstatic devclass_t ahd_devclass;
5797883Sgibbs
5897883SgibbsDRIVER_MODULE(ahd, pci, ahd_pci_driver, ahd_devclass, 0, 0);
5997883SgibbsMODULE_DEPEND(ahd_pci, ahd, 1, 1, 1);
6097883SgibbsMODULE_VERSION(ahd_pci, 1);
6197883Sgibbs
6297883Sgibbsstatic int
6397883Sgibbsahd_pci_probe(device_t dev)
6497883Sgibbs{
6597883Sgibbs	struct	ahd_pci_identity *entry;
6697883Sgibbs
6797883Sgibbs	entry = ahd_find_pci_device(dev);
6897883Sgibbs	if (entry != NULL) {
6997883Sgibbs		device_set_desc(dev, entry->name);
70143164Simp		return (BUS_PROBE_DEFAULT);
7197883Sgibbs	}
7297883Sgibbs	return (ENXIO);
7397883Sgibbs}
7497883Sgibbs
7597883Sgibbsstatic int
7697883Sgibbsahd_pci_attach(device_t dev)
7797883Sgibbs{
7897883Sgibbs	struct	 ahd_pci_identity *entry;
7997883Sgibbs	struct	 ahd_softc *ahd;
8097883Sgibbs	char	*name;
8197883Sgibbs	int	 error;
8297883Sgibbs
8397883Sgibbs	entry = ahd_find_pci_device(dev);
8497883Sgibbs	if (entry == NULL)
8597883Sgibbs		return (ENXIO);
8697883Sgibbs
8797883Sgibbs	/*
8897883Sgibbs	 * Allocate a softc for this card and
8997883Sgibbs	 * set it up for attachment by our
9097883Sgibbs	 * common detect routine.
9197883Sgibbs	 */
9297883Sgibbs	name = malloc(strlen(device_get_nameunit(dev)) + 1, M_DEVBUF, M_NOWAIT);
9397883Sgibbs	if (name == NULL)
9497883Sgibbs		return (ENOMEM);
9597883Sgibbs	strcpy(name, device_get_nameunit(dev));
9697883Sgibbs	ahd = ahd_alloc(dev, name);
9797883Sgibbs	if (ahd == NULL)
9897883Sgibbs		return (ENOMEM);
9997883Sgibbs
10097883Sgibbs	ahd_set_unit(ahd, device_get_unit(dev));
10197883Sgibbs
10297883Sgibbs	/*
10397883Sgibbs	 * Should we bother disabling 39Bit addressing
10497883Sgibbs	 * based on installed memory?
10597883Sgibbs	 */
10697883Sgibbs	if (sizeof(bus_addr_t) > 4)
10797883Sgibbs                ahd->flags |= AHD_39BIT_ADDRESSING;
10897883Sgibbs
10997883Sgibbs	/* Allocate a dmatag for our SCB DMA maps */
110161928Sjmg	error = aic_dma_tag_create(ahd, /*parent*/bus_get_dma_tag(dev),
111161928Sjmg				   /*alignment*/1, /*boundary*/0,
11297883Sgibbs				   (ahd->flags & AHD_39BIT_ADDRESSING)
113123579Sgibbs				   ? 0x7FFFFFFFFF
11497883Sgibbs				   : BUS_SPACE_MAXADDR_32BIT,
11597883Sgibbs				   /*highaddr*/BUS_SPACE_MAXADDR,
11697883Sgibbs				   /*filter*/NULL, /*filterarg*/NULL,
117108479Sscottl				   /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
118108479Sscottl				   /*nsegments*/AHD_NSEG,
11997883Sgibbs				   /*maxsegsz*/AHD_MAXTRANSFER_SIZE,
120114618Sgibbs				   /*flags*/0,
12197883Sgibbs				   &ahd->parent_dmat);
12297883Sgibbs
12397883Sgibbs	if (error != 0) {
12497883Sgibbs		printf("ahd_pci_attach: Could not allocate DMA tag "
12597883Sgibbs		       "- error %d\n", error);
12697883Sgibbs		ahd_free(ahd);
12797883Sgibbs		return (ENOMEM);
12897883Sgibbs	}
12997883Sgibbs	ahd->dev_softc = dev;
13097883Sgibbs	error = ahd_pci_config(ahd, entry);
13197883Sgibbs	if (error != 0) {
13297883Sgibbs		ahd_free(ahd);
13397883Sgibbs		return (error);
13497883Sgibbs	}
13597883Sgibbs
136199260Sattilio	ahd_sysctl(ahd);
13797883Sgibbs	ahd_attach(ahd);
13897883Sgibbs	return (0);
13997883Sgibbs}
14097883Sgibbs
14197883Sgibbsint
14297883Sgibbsahd_pci_map_registers(struct ahd_softc *ahd)
14397883Sgibbs{
14497883Sgibbs	struct	resource *regs;
14597883Sgibbs	struct	resource *regs2;
14697883Sgibbs	int	regs_type;
14797883Sgibbs	int	regs_id;
14897883Sgibbs	int	regs_id2;
149107623Sscottl	int	allow_memio;
15097883Sgibbs
15197883Sgibbs	regs = NULL;
15297883Sgibbs	regs2 = NULL;
15397883Sgibbs	regs_type = 0;
15497883Sgibbs	regs_id = 0;
155107623Sscottl
156107623Sscottl	/* Retrieve the per-device 'allow_memio' hint */
157107623Sscottl	if (resource_int_value(device_get_name(ahd->dev_softc),
158107623Sscottl			       device_get_unit(ahd->dev_softc),
159107623Sscottl			       "allow_memio", &allow_memio) != 0) {
160107623Sscottl		if (bootverbose)
161107623Sscottl			device_printf(ahd->dev_softc,
162107623Sscottl				      "Defaulting to MEMIO on\n");
163150449Sgibbs		allow_memio = 1;
164107623Sscottl	}
165107623Sscottl
166254306Sscottl	if ((ahd->bugs & AHD_PCIX_MMAPIO_BUG) == 0
167107623Sscottl	 && allow_memio != 0) {
16897883Sgibbs
16997883Sgibbs		regs_type = SYS_RES_MEMORY;
17097883Sgibbs		regs_id = AHD_PCI_MEMADDR;
171127135Snjl		regs = bus_alloc_resource_any(ahd->dev_softc, regs_type,
172127135Snjl					      &regs_id, RF_ACTIVE);
17397883Sgibbs		if (regs != NULL) {
17497883Sgibbs			int error;
17597883Sgibbs
17697883Sgibbs			ahd->tags[0] = rman_get_bustag(regs);
17797883Sgibbs			ahd->bshs[0] = rman_get_bushandle(regs);
17897883Sgibbs			ahd->tags[1] = ahd->tags[0];
17997883Sgibbs			error = bus_space_subregion(ahd->tags[0], ahd->bshs[0],
18097883Sgibbs						    /*offset*/0x100,
18197883Sgibbs						    /*size*/0x100,
18297883Sgibbs						    &ahd->bshs[1]);
18397883Sgibbs			/*
18497883Sgibbs			 * Do a quick test to see if memory mapped
18597883Sgibbs			 * I/O is functioning correctly.
18697883Sgibbs			 */
187107438Sscottl			if (error != 0
188107438Sscottl			 || ahd_pci_test_register_access(ahd) != 0) {
18997883Sgibbs				device_printf(ahd->dev_softc,
19097883Sgibbs				       "PCI Device %d:%d:%d failed memory "
19197883Sgibbs				       "mapped test.  Using PIO.\n",
192123579Sgibbs				       aic_get_pci_bus(ahd->dev_softc),
193123579Sgibbs				       aic_get_pci_slot(ahd->dev_softc),
194123579Sgibbs				       aic_get_pci_function(ahd->dev_softc));
19597883Sgibbs				bus_release_resource(ahd->dev_softc, regs_type,
19697883Sgibbs						     regs_id, regs);
19797883Sgibbs				regs = NULL;
198199260Sattilio				AHD_CORRECTABLE_ERROR(ahd);
19997883Sgibbs			}
20097883Sgibbs		}
20197883Sgibbs	}
202254306Sscottl	if (regs == NULL) {
20397883Sgibbs		regs_type = SYS_RES_IOPORT;
20497883Sgibbs		regs_id = AHD_PCI_IOADDR0;
205127135Snjl		regs = bus_alloc_resource_any(ahd->dev_softc, regs_type,
206127135Snjl					      &regs_id, RF_ACTIVE);
20797883Sgibbs		if (regs == NULL) {
20897883Sgibbs			device_printf(ahd->dev_softc,
20997883Sgibbs				      "can't allocate register resources\n");
210199260Sattilio			AHD_UNCORRECTABLE_ERROR(ahd);
21197883Sgibbs			return (ENOMEM);
21297883Sgibbs		}
21397883Sgibbs		ahd->tags[0] = rman_get_bustag(regs);
21497883Sgibbs		ahd->bshs[0] = rman_get_bushandle(regs);
21597883Sgibbs
21697883Sgibbs		/* And now the second BAR */
21797883Sgibbs		regs_id2 = AHD_PCI_IOADDR1;
218127135Snjl		regs2 = bus_alloc_resource_any(ahd->dev_softc, regs_type,
219127135Snjl					       &regs_id2, RF_ACTIVE);
22097883Sgibbs		if (regs2 == NULL) {
22197883Sgibbs			device_printf(ahd->dev_softc,
22297883Sgibbs				      "can't allocate register resources\n");
223199260Sattilio			AHD_UNCORRECTABLE_ERROR(ahd);
22497883Sgibbs			return (ENOMEM);
22597883Sgibbs		}
22697883Sgibbs		ahd->tags[1] = rman_get_bustag(regs2);
22797883Sgibbs		ahd->bshs[1] = rman_get_bushandle(regs2);
22897883Sgibbs		ahd->platform_data->regs_res_type[1] = regs_type;
22997883Sgibbs		ahd->platform_data->regs_res_id[1] = regs_id2;
23097883Sgibbs		ahd->platform_data->regs[1] = regs2;
23197883Sgibbs	}
23297883Sgibbs	ahd->platform_data->regs_res_type[0] = regs_type;
23397883Sgibbs	ahd->platform_data->regs_res_id[0] = regs_id;
23497883Sgibbs	ahd->platform_data->regs[0] = regs;
23597883Sgibbs	return (0);
23697883Sgibbs}
23797883Sgibbs
23897883Sgibbsint
23997883Sgibbsahd_pci_map_int(struct ahd_softc *ahd)
24097883Sgibbs{
24197883Sgibbs	int zero;
24297883Sgibbs
24397883Sgibbs	zero = 0;
24497883Sgibbs	ahd->platform_data->irq =
245127135Snjl	    bus_alloc_resource_any(ahd->dev_softc, SYS_RES_IRQ, &zero,
246127135Snjl				   RF_ACTIVE | RF_SHAREABLE);
24797883Sgibbs	if (ahd->platform_data->irq == NULL)
24897883Sgibbs		return (ENOMEM);
24997883Sgibbs	ahd->platform_data->irq_res_type = SYS_RES_IRQ;
25097883Sgibbs	return (ahd_map_int(ahd));
25197883Sgibbs}
252