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