xref: /freebsd/sys/dev/aic7xxx/ahc_pci.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
1 /*
2  * FreeBSD, PCI product support functions
3  *
4  * Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000 Justin T. Gibbs
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification, immediately at the beginning of the file.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * Alternatively, this software may be distributed under the terms of the
17  * GNU Public License ("GPL").
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $Id$
32  *
33  * $FreeBSD$
34  */
35 
36 #include <dev/aic7xxx/aic7xxx_freebsd.h>
37 
38 #define	AHC_PCI_IOADDR  PCIR_MAPS	/* I/O Address */
39 #define	AHC_PCI_MEMADDR (PCIR_MAPS + 4) /* Mem I/O Address */
40 
41 static int ahc_pci_probe(device_t dev);
42 static int ahc_pci_attach(device_t dev);
43 
44 static device_method_t ahc_pci_methods[] = {
45 	/* Device interface */
46 	DEVMETHOD(device_probe,		ahc_pci_probe),
47 	DEVMETHOD(device_attach,	ahc_pci_attach),
48 	{ 0, 0 }
49 };
50 
51 static driver_t ahc_pci_driver = {
52 	"ahc",
53 	ahc_pci_methods,
54 	sizeof(struct ahc_softc)
55 };
56 
57 static devclass_t ahc_devclass;
58 
59 DRIVER_MODULE(ahc, pci, ahc_pci_driver, ahc_devclass, 0, 0);
60 
61 static int
62 ahc_pci_probe(device_t dev)
63 {
64 	struct	ahc_pci_identity *entry;
65 
66 	entry = ahc_find_pci_device(dev);
67 	if (entry != NULL) {
68 		device_set_desc(dev, entry->name);
69 		return (0);
70 	}
71 	return (ENXIO);
72 }
73 
74 static int
75 ahc_pci_attach(device_t dev)
76 {
77 	struct	 ahc_pci_identity *entry;
78 	struct	 ahc_softc *ahc;
79 	char	*name;
80 	int	 error;
81 
82 	entry = ahc_find_pci_device(dev);
83 	if (entry == NULL)
84 		return (ENXIO);
85 
86 	/*
87 	 * Allocate a softc for this card and
88 	 * set it up for attachment by our
89 	 * common detect routine.
90 	 */
91 	name = malloc(strlen(device_get_nameunit(dev)) + 1, M_DEVBUF, M_NOWAIT);
92 	if (name == NULL)
93 		return (ENOMEM);
94 	strcpy(name, device_get_nameunit(dev));
95 	ahc = ahc_alloc(NULL, name);
96 	if (ahc == NULL)
97 		return (ENOMEM);
98 
99 	/* Allocate a dmatag for our SCB DMA maps */
100 	/* XXX Should be a child of the PCI bus dma tag */
101 	error = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1,
102 				   /*boundary*/0,
103 				   /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
104 				   /*highaddr*/BUS_SPACE_MAXADDR,
105 				   /*filter*/NULL, /*filterarg*/NULL,
106 				   /*maxsize*/MAXBSIZE, /*nsegments*/AHC_NSEG,
107 				   /*maxsegsz*/AHC_MAXTRANSFER_SIZE,
108 				   /*flags*/BUS_DMA_ALLOCNOW,
109 				   &ahc->parent_dmat);
110 
111 	if (error != 0) {
112 		printf("ahc_pci_attach: Could not allocate DMA tag "
113 		       "- error %d\n", error);
114 		ahc_free(ahc);
115 		return (ENOMEM);
116 	}
117 	ahc->dev_softc = dev;
118 	error = ahc_pci_config(ahc, entry);
119 	if (error != 0) {
120 		ahc_free(ahc);
121 		return (error);
122 	}
123 
124 	ahc_attach(ahc);
125 	return (0);
126 }
127 
128 int
129 ahc_pci_map_registers(struct ahc_softc *ahc)
130 {
131 	struct	resource *regs;
132 	u_int	command;
133 	int	regs_type;
134 	int	regs_id;
135 
136 	command = ahc_pci_read_config(ahc->dev_softc, PCIR_COMMAND, /*bytes*/1);
137 	regs = NULL;
138 	regs_type = 0;
139 	regs_id = 0;
140 #ifdef AHC_ALLOW_MEMIO
141 	if ((command & PCIM_CMD_MEMEN) != 0) {
142 		regs_type = SYS_RES_MEMORY;
143 		regs_id = AHC_PCI_MEMADDR;
144 		regs = bus_alloc_resource(ahc->dev_softc, regs_type,
145 					  &regs_id, 0, ~0, 1, RF_ACTIVE);
146 		if (regs != NULL) {
147 			ahc->tag = rman_get_bustag(regs);
148 			ahc->bsh = rman_get_bushandle(regs);
149 
150 			/*
151 			 * Do a quick test to see if memory mapped
152 			 * I/O is functioning correctly.
153 			 */
154 			if (ahc_inb(ahc, HCNTRL) == 0xFF) {
155 				device_printf(ahc->dev_softc,
156 				       "PCI Device %d:%d:%d failed memory "
157 				       "mapped test.  Using PIO.\n",
158 				       ahc_get_pci_bus(ahc->dev_softc),
159 				       ahc_get_pci_slot(ahc->dev_softc),
160 				       ahc_get_pci_function(ahc->dev_softc));
161 				bus_release_resource(ahc->dev_softc, regs_type,
162 						     regs_id, regs);
163 				regs = NULL;
164 			}
165 		}
166 	}
167 #endif
168 	if (regs == NULL && (command & PCIM_CMD_PORTEN) != 0) {
169 		regs_type = SYS_RES_IOPORT;
170 		regs_id = AHC_PCI_IOADDR;
171 		regs = bus_alloc_resource(ahc->dev_softc, regs_type,
172 					  &regs_id, 0, ~0, 1, RF_ACTIVE);
173 		ahc->tag = rman_get_bustag(regs);
174 		ahc->bsh = rman_get_bushandle(regs);
175 	}
176 	ahc->platform_data->regs_res_type = regs_type;
177 	ahc->platform_data->regs_res_id = regs_id;
178 	ahc->platform_data->regs = regs;
179 
180 	if (regs == NULL) {
181 		device_printf(ahc->dev_softc,
182 			      "can't allocate register resources\n");
183 		return (ENOMEM);
184 	}
185 	return (0);
186 }
187 
188 int
189 ahc_pci_map_int(struct ahc_softc *ahc)
190 {
191 	int zero;
192 
193 	zero = 0;
194 	ahc->platform_data->irq =
195 	    bus_alloc_resource(ahc->dev_softc, SYS_RES_IRQ, &zero,
196 			       0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
197 	if (ahc->platform_data->irq == NULL)
198 		return (ENOMEM);
199 	ahc->platform_data->irq_res_type = SYS_RES_IRQ;
200 	return (0);
201 }
202