xref: /freebsd/sys/dev/aic7xxx/ahc_pci.c (revision 069ac184)
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/ahc_pci.c#19 $
32  */
33 
34 #include <sys/cdefs.h>
35 #include <dev/aic7xxx/aic7xxx_osm.h>
36 
37 static int ahc_pci_probe(device_t dev);
38 static int ahc_pci_attach(device_t dev);
39 
40 static device_method_t ahc_pci_device_methods[] = {
41 	/* Device interface */
42 	DEVMETHOD(device_probe,		ahc_pci_probe),
43 	DEVMETHOD(device_attach,	ahc_pci_attach),
44 	DEVMETHOD(device_detach,	ahc_detach),
45 	{ 0, 0 }
46 };
47 
48 static driver_t ahc_pci_driver = {
49 	"ahc",
50 	ahc_pci_device_methods,
51 	sizeof(struct ahc_softc)
52 };
53 
54 DRIVER_MODULE(ahc_pci, pci, ahc_pci_driver, 0, 0);
55 MODULE_DEPEND(ahc_pci, ahc, 1, 1, 1);
56 MODULE_VERSION(ahc_pci, 1);
57 
58 static int
59 ahc_pci_probe(device_t dev)
60 {
61 	struct	ahc_pci_identity *entry;
62 
63 	entry = ahc_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 ahc_pci_attach(device_t dev)
73 {
74 	struct	 ahc_pci_identity *entry;
75 	struct	 ahc_softc *ahc;
76 	char	*name;
77 	int	 error;
78 
79 	entry = ahc_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 	ahc = ahc_alloc(dev, name);
93 	if (ahc == NULL)
94 		return (ENOMEM);
95 
96 	ahc_set_unit(ahc, 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                 ahc->flags |= AHC_39BIT_ADDRESSING;
104 
105 	/* Allocate a dmatag for our SCB DMA maps */
106 	error = aic_dma_tag_create(ahc, /*parent*/bus_get_dma_tag(dev),
107 				   /*alignment*/1, /*boundary*/0,
108 				   (ahc->flags & AHC_39BIT_ADDRESSING)
109 				   ? 0x7FFFFFFFFFLL
110 				   : BUS_SPACE_MAXADDR_32BIT,
111 				   /*highaddr*/BUS_SPACE_MAXADDR,
112 				   /*filter*/NULL, /*filterarg*/NULL,
113 				   /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
114 				   /*nsegments*/AHC_NSEG,
115 				   /*maxsegsz*/AHC_MAXTRANSFER_SIZE,
116 				   /*flags*/0,
117 				   &ahc->parent_dmat);
118 
119 	if (error != 0) {
120 		printf("ahc_pci_attach: Could not allocate DMA tag "
121 		       "- error %d\n", error);
122 		ahc_free(ahc);
123 		return (ENOMEM);
124 	}
125 	ahc->dev_softc = dev;
126 	error = ahc_pci_config(ahc, entry);
127 	if (error != 0) {
128 		ahc_free(ahc);
129 		return (error);
130 	}
131 
132 	ahc_attach(ahc);
133 	return (0);
134 }
135 
136 int
137 ahc_pci_map_registers(struct ahc_softc *ahc)
138 {
139 	struct	resource *regs;
140 	int	regs_type;
141 	int	regs_id;
142 	int	allow_memio;
143 
144 	regs = NULL;
145 	regs_type = 0;
146 	regs_id = 0;
147 
148 	/* Retrieve the per-device 'allow_memio' hint */
149 	if (resource_int_value(device_get_name(ahc->dev_softc),
150 			       device_get_unit(ahc->dev_softc),
151 			       "allow_memio", &allow_memio) != 0) {
152 		if (bootverbose)
153 			device_printf(ahc->dev_softc, "Defaulting to MEMIO ");
154 #ifdef AHC_ALLOW_MEMIO
155 		if (bootverbose)
156 			printf("on\n");
157 		allow_memio = 1;
158 #else
159 		if (bootverbose)
160 			printf("off\n");
161 		allow_memio = 0;
162 #endif
163 	}
164 
165 	if (allow_memio != 0) {
166 		regs_type = SYS_RES_MEMORY;
167 		regs_id = AHC_PCI_MEMADDR;
168 		regs = bus_alloc_resource_any(ahc->dev_softc, regs_type,
169 					      &regs_id, RF_ACTIVE);
170 		if (regs != NULL) {
171 			ahc->tag = rman_get_bustag(regs);
172 			ahc->bsh = rman_get_bushandle(regs);
173 
174 			/*
175 			 * Do a quick test to see if memory mapped
176 			 * I/O is functioning correctly.
177 			 */
178 			if (ahc_pci_test_register_access(ahc) != 0) {
179 				device_printf(ahc->dev_softc,
180 				       "PCI Device %d:%d:%d failed memory "
181 				       "mapped test.  Using PIO.\n",
182 				       aic_get_pci_bus(ahc->dev_softc),
183 				       aic_get_pci_slot(ahc->dev_softc),
184 				       aic_get_pci_function(ahc->dev_softc));
185 				bus_release_resource(ahc->dev_softc, regs_type,
186 						     regs_id, regs);
187 				regs = NULL;
188 			}
189 		}
190 	}
191 
192 	if (regs == NULL) {
193 		regs_type = SYS_RES_IOPORT;
194 		regs_id = AHC_PCI_IOADDR;
195 		regs = bus_alloc_resource_any(ahc->dev_softc, regs_type,
196 					      &regs_id, RF_ACTIVE);
197 		if (regs != NULL) {
198 			ahc->tag = rman_get_bustag(regs);
199 			ahc->bsh = rman_get_bushandle(regs);
200 			if (ahc_pci_test_register_access(ahc) != 0) {
201 				device_printf(ahc->dev_softc,
202 				       "PCI Device %d:%d:%d failed I/O "
203 				       "mapped test.\n",
204 				       aic_get_pci_bus(ahc->dev_softc),
205 				       aic_get_pci_slot(ahc->dev_softc),
206 				       aic_get_pci_function(ahc->dev_softc));
207 				bus_release_resource(ahc->dev_softc, regs_type,
208 						     regs_id, regs);
209 				regs = NULL;
210 			}
211 		}
212 	}
213 	if (regs == NULL) {
214 		device_printf(ahc->dev_softc,
215 			      "can't allocate register resources\n");
216 		return (ENOMEM);
217 	}
218 	ahc->platform_data->regs_res_type = regs_type;
219 	ahc->platform_data->regs_res_id = regs_id;
220 	ahc->platform_data->regs = regs;
221 	return (0);
222 }
223