xref: /freebsd/sys/dev/aic7xxx/ahd_pci.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*-
2  * FreeBSD, PCI product support functions
3  *
4  * Copyright (c) 1995-2001 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: //depot/aic7xxx/freebsd/dev/aic7xxx/ahd_pci.c#17 $
32  */
33 
34 #include <sys/cdefs.h>
35 #include <dev/aic7xxx/aic79xx_osm.h>
36 
37 static int ahd_pci_probe(device_t dev);
38 static int ahd_pci_attach(device_t dev);
39 
40 static device_method_t ahd_pci_device_methods[] = {
41 	/* Device interface */
42 	DEVMETHOD(device_probe,		ahd_pci_probe),
43 	DEVMETHOD(device_attach,	ahd_pci_attach),
44 	DEVMETHOD(device_detach,	ahd_detach),
45 	{ 0, 0 }
46 };
47 
48 static driver_t ahd_pci_driver = {
49 	"ahd",
50 	ahd_pci_device_methods,
51 	sizeof(struct ahd_softc)
52 };
53 
54 DRIVER_MODULE(ahd, pci, ahd_pci_driver, 0, 0);
55 MODULE_DEPEND(ahd_pci, ahd, 1, 1, 1);
56 MODULE_VERSION(ahd_pci, 1);
57 
58 static int
59 ahd_pci_probe(device_t dev)
60 {
61 	struct	ahd_pci_identity *entry;
62 
63 	entry = ahd_find_pci_device(dev);
64 	if (entry != NULL) {
65 		device_set_desc(dev, entry->name);
66 		return (BUS_PROBE_DEFAULT);
67 	}
68 	return (ENXIO);
69 }
70 
71 static int
72 ahd_pci_attach(device_t dev)
73 {
74 	struct	 ahd_pci_identity *entry;
75 	struct	 ahd_softc *ahd;
76 	char	*name;
77 	int	 error;
78 
79 	entry = ahd_find_pci_device(dev);
80 	if (entry == NULL)
81 		return (ENXIO);
82 
83 	/*
84 	 * Allocate a softc for this card and
85 	 * set it up for attachment by our
86 	 * common detect routine.
87 	 */
88 	name = malloc(strlen(device_get_nameunit(dev)) + 1, M_DEVBUF, M_NOWAIT);
89 	if (name == NULL)
90 		return (ENOMEM);
91 	strcpy(name, device_get_nameunit(dev));
92 	ahd = ahd_alloc(dev, name);
93 	if (ahd == NULL)
94 		return (ENOMEM);
95 
96 	ahd_set_unit(ahd, device_get_unit(dev));
97 
98 	/*
99 	 * Should we bother disabling 39Bit addressing
100 	 * based on installed memory?
101 	 */
102 	if (sizeof(bus_addr_t) > 4)
103                 ahd->flags |= AHD_39BIT_ADDRESSING;
104 
105 	/* Allocate a dmatag for our SCB DMA maps */
106 	error = aic_dma_tag_create(ahd, /*parent*/bus_get_dma_tag(dev),
107 				   /*alignment*/1, /*boundary*/0,
108 				   (ahd->flags & AHD_39BIT_ADDRESSING)
109 				   ? 0x7FFFFFFFFF
110 				   : BUS_SPACE_MAXADDR_32BIT,
111 				   /*highaddr*/BUS_SPACE_MAXADDR,
112 				   /*filter*/NULL, /*filterarg*/NULL,
113 				   /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
114 				   /*nsegments*/AHD_NSEG,
115 				   /*maxsegsz*/AHD_MAXTRANSFER_SIZE,
116 				   /*flags*/0,
117 				   &ahd->parent_dmat);
118 
119 	if (error != 0) {
120 		printf("ahd_pci_attach: Could not allocate DMA tag "
121 		       "- error %d\n", error);
122 		ahd_free(ahd);
123 		return (ENOMEM);
124 	}
125 	ahd->dev_softc = dev;
126 	error = ahd_pci_config(ahd, entry);
127 	if (error != 0) {
128 		ahd_free(ahd);
129 		return (error);
130 	}
131 
132 	ahd_sysctl(ahd);
133 	ahd_attach(ahd);
134 	return (0);
135 }
136 
137 int
138 ahd_pci_map_registers(struct ahd_softc *ahd)
139 {
140 	struct	resource *regs;
141 	struct	resource *regs2;
142 	int	regs_type;
143 	int	regs_id;
144 	int	regs_id2;
145 	int	allow_memio;
146 
147 	regs = NULL;
148 	regs2 = NULL;
149 	regs_type = 0;
150 	regs_id = 0;
151 
152 	/* Retrieve the per-device 'allow_memio' hint */
153 	if (resource_int_value(device_get_name(ahd->dev_softc),
154 			       device_get_unit(ahd->dev_softc),
155 			       "allow_memio", &allow_memio) != 0) {
156 		if (bootverbose)
157 			device_printf(ahd->dev_softc,
158 				      "Defaulting to MEMIO on\n");
159 		allow_memio = 1;
160 	}
161 
162 	if ((ahd->bugs & AHD_PCIX_MMAPIO_BUG) == 0
163 	 && allow_memio != 0) {
164 		regs_type = SYS_RES_MEMORY;
165 		regs_id = AHD_PCI_MEMADDR;
166 		regs = bus_alloc_resource_any(ahd->dev_softc, regs_type,
167 					      &regs_id, RF_ACTIVE);
168 		if (regs != NULL) {
169 			int error;
170 
171 			ahd->tags[0] = rman_get_bustag(regs);
172 			ahd->bshs[0] = rman_get_bushandle(regs);
173 			ahd->tags[1] = ahd->tags[0];
174 			error = bus_space_subregion(ahd->tags[0], ahd->bshs[0],
175 						    /*offset*/0x100,
176 						    /*size*/0x100,
177 						    &ahd->bshs[1]);
178 			/*
179 			 * Do a quick test to see if memory mapped
180 			 * I/O is functioning correctly.
181 			 */
182 			if (error != 0
183 			 || ahd_pci_test_register_access(ahd) != 0) {
184 				device_printf(ahd->dev_softc,
185 				       "PCI Device %d:%d:%d failed memory "
186 				       "mapped test.  Using PIO.\n",
187 				       aic_get_pci_bus(ahd->dev_softc),
188 				       aic_get_pci_slot(ahd->dev_softc),
189 				       aic_get_pci_function(ahd->dev_softc));
190 				bus_release_resource(ahd->dev_softc, regs_type,
191 						     regs_id, regs);
192 				regs = NULL;
193 				AHD_CORRECTABLE_ERROR(ahd);
194 			}
195 		}
196 	}
197 	if (regs == NULL) {
198 		regs_type = SYS_RES_IOPORT;
199 		regs_id = AHD_PCI_IOADDR0;
200 		regs = bus_alloc_resource_any(ahd->dev_softc, regs_type,
201 					      &regs_id, RF_ACTIVE);
202 		if (regs == NULL) {
203 			device_printf(ahd->dev_softc,
204 				      "can't allocate register resources\n");
205 			AHD_UNCORRECTABLE_ERROR(ahd);
206 			return (ENOMEM);
207 		}
208 		ahd->tags[0] = rman_get_bustag(regs);
209 		ahd->bshs[0] = rman_get_bushandle(regs);
210 
211 		/* And now the second BAR */
212 		regs_id2 = AHD_PCI_IOADDR1;
213 		regs2 = bus_alloc_resource_any(ahd->dev_softc, regs_type,
214 					       &regs_id2, RF_ACTIVE);
215 		if (regs2 == NULL) {
216 			device_printf(ahd->dev_softc,
217 				      "can't allocate register resources\n");
218 			AHD_UNCORRECTABLE_ERROR(ahd);
219 			return (ENOMEM);
220 		}
221 		ahd->tags[1] = rman_get_bustag(regs2);
222 		ahd->bshs[1] = rman_get_bushandle(regs2);
223 		ahd->platform_data->regs_res_type[1] = regs_type;
224 		ahd->platform_data->regs_res_id[1] = regs_id2;
225 		ahd->platform_data->regs[1] = regs2;
226 	}
227 	ahd->platform_data->regs_res_type[0] = regs_type;
228 	ahd->platform_data->regs_res_id[0] = regs_id;
229 	ahd->platform_data->regs[0] = regs;
230 	return (0);
231 }
232 
233 int
234 ahd_pci_map_int(struct ahd_softc *ahd)
235 {
236 	int zero;
237 
238 	zero = 0;
239 	ahd->platform_data->irq =
240 	    bus_alloc_resource_any(ahd->dev_softc, SYS_RES_IRQ, &zero,
241 				   RF_ACTIVE | RF_SHAREABLE);
242 	if (ahd->platform_data->irq == NULL)
243 		return (ENOMEM);
244 	ahd->platform_data->irq_res_type = SYS_RES_IRQ;
245 	return (ahd_map_int(ahd));
246 }
247