2 * Copyright (c) 2010 Nathan Whitehorn
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
37 #include <sys/mutex.h>
40 #include <machine/resource.h>
42 #include <dev/agp/agppriv.h>
43 #include <dev/agp/agpreg.h>
44 #include <dev/pci/pcivar.h>
45 #include <dev/pci/pcireg.h>
48 #include <vm/vm_object.h>
51 #define UNIN_AGP_GART_BASE 0x8c
52 #define UNIN_AGP_BASE_ADDR 0x90
53 #define UNIN_AGP_GART_CONTROL 0x94
55 #define UNIN_AGP_GART_INVAL 0x00000001
56 #define UNIN_AGP_GART_ENABLE 0x00000100
57 #define UNIN_AGP_GART_2XRESET 0x00010000
58 #define UNIN_AGP_U3_GART_PERFRD 0x00080000
60 struct agp_apple_softc {
63 struct agp_gatt *gatt;
69 agp_apple_probe(device_t dev)
72 if (resource_disabled("agp", device_get_unit(dev)))
75 if (pci_get_class(dev) != PCIC_BRIDGE
76 || pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
79 if (agp_find_caps(dev) == 0)
82 if (pci_get_class(dev) != PCIC_BRIDGE
83 || pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
86 switch (pci_get_devid(dev)) {
89 device_set_desc(dev, "Apple UniNorth AGP Bridge");
90 return (BUS_PROBE_DEFAULT);
92 device_set_desc(dev, "Apple UniNorth 1.5 AGP Bridge");
93 return (BUS_PROBE_DEFAULT);
95 device_set_desc(dev, "Apple UniNorth 2 AGP Bridge");
96 return (BUS_PROBE_DEFAULT);
100 device_set_desc(dev, "Apple U3 AGP Bridge");
101 return (BUS_PROBE_DEFAULT);
103 device_set_desc(dev, "Apple Intrepid AGP Bridge");
104 return (BUS_PROBE_DEFAULT);
111 agp_apple_attach(device_t dev)
113 struct agp_apple_softc *sc = device_get_softc(dev);
117 sc->needs_2x_reset = 0;
119 switch (pci_get_devid(dev)) {
122 sc->needs_2x_reset = 1;
131 /* Set the aperture bus address base (must be 0) */
132 pci_write_config(dev, UNIN_AGP_BASE_ADDR, 0, 4);
133 agp_set_aperture_resource(dev, -1);
135 error = agp_generic_attach(dev);
139 sc->aperture = 256*1024*1024;
141 for (sc->aperture = 256*1024*1024; sc->aperture >= 4*1024*1024;
143 sc->gatt = agp_alloc_gatt(dev);
147 if (sc->aperture < 4*1024*1024) {
148 agp_generic_detach(dev);
152 /* Install the gatt. */
153 AGP_SET_APERTURE(dev, sc->aperture);
155 /* XXX: U3 scratch page? */
157 /* Enable the aperture and TLB. */
164 agp_apple_detach(device_t dev)
166 struct agp_apple_softc *sc = device_get_softc(dev);
170 /* Disable the aperture and TLB */
171 pci_write_config(dev, UNIN_AGP_GART_CONTROL, UNIN_AGP_GART_INVAL, 4);
172 pci_write_config(dev, UNIN_AGP_GART_CONTROL, 0, 4);
174 if (sc->needs_2x_reset) {
175 pci_write_config(dev, UNIN_AGP_GART_CONTROL,
176 UNIN_AGP_GART_2XRESET, 4);
177 pci_write_config(dev, UNIN_AGP_GART_CONTROL, 0, 4);
180 AGP_SET_APERTURE(dev, 0);
182 agp_free_gatt(sc->gatt);
188 agp_apple_get_aperture(device_t dev)
190 struct agp_apple_softc *sc = device_get_softc(dev);
192 return (sc->aperture);
196 agp_apple_set_aperture(device_t dev, uint32_t aperture)
198 struct agp_apple_softc *sc = device_get_softc(dev);
201 * Check for a multiple of 4 MB and make sure it is within the
202 * programmable range.
204 if (aperture % (4*1024*1024)
205 || aperture < 4*1024*1024
206 || aperture > ((sc->u3) ? 512 : 256)*1024*1024)
209 /* The aperture value is a multiple of 4 MB */
210 aperture /= (4*1024*1024);
212 pci_write_config(dev, UNIN_AGP_GART_BASE,
213 (sc->gatt->ag_physical & 0xfffff000) | aperture, 4);
219 agp_apple_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical)
221 struct agp_apple_softc *sc = device_get_softc(dev);
223 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
226 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical;
231 agp_apple_unbind_page(device_t dev, vm_offset_t offset)
233 struct agp_apple_softc *sc = device_get_softc(dev);
235 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
238 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
243 agp_apple_flush_tlb(device_t dev)
245 struct agp_apple_softc *sc = device_get_softc(dev);
246 uint32_t cntrl = UNIN_AGP_GART_ENABLE;
249 cntrl |= UNIN_AGP_U3_GART_PERFRD;
251 pci_write_config(dev, UNIN_AGP_GART_CONTROL,
252 cntrl | UNIN_AGP_GART_INVAL, 4);
253 pci_write_config(dev, UNIN_AGP_GART_CONTROL, cntrl, 4);
255 if (sc->needs_2x_reset) {
256 pci_write_config(dev, UNIN_AGP_GART_CONTROL,
257 cntrl | UNIN_AGP_GART_2XRESET, 4);
258 pci_write_config(dev, UNIN_AGP_GART_CONTROL, cntrl, 4);
262 static device_method_t agp_apple_methods[] = {
263 /* Device interface */
264 DEVMETHOD(device_probe, agp_apple_probe),
265 DEVMETHOD(device_attach, agp_apple_attach),
266 DEVMETHOD(device_detach, agp_apple_detach),
267 DEVMETHOD(device_shutdown, bus_generic_shutdown),
268 DEVMETHOD(device_suspend, bus_generic_suspend),
269 DEVMETHOD(device_resume, bus_generic_resume),
272 DEVMETHOD(agp_get_aperture, agp_apple_get_aperture),
273 DEVMETHOD(agp_set_aperture, agp_apple_set_aperture),
274 DEVMETHOD(agp_bind_page, agp_apple_bind_page),
275 DEVMETHOD(agp_unbind_page, agp_apple_unbind_page),
276 DEVMETHOD(agp_flush_tlb, agp_apple_flush_tlb),
277 DEVMETHOD(agp_enable, agp_generic_enable),
278 DEVMETHOD(agp_alloc_memory, agp_generic_alloc_memory),
279 DEVMETHOD(agp_free_memory, agp_generic_free_memory),
280 DEVMETHOD(agp_bind_memory, agp_generic_bind_memory),
281 DEVMETHOD(agp_unbind_memory, agp_generic_unbind_memory),
286 static driver_t agp_apple_driver = {
289 sizeof(struct agp_apple_softc),
292 static devclass_t agp_devclass;
294 DRIVER_MODULE(agp_apple, hostb, agp_apple_driver, agp_devclass, 0, 0);
295 MODULE_DEPEND(agp_apple, agp, 1, 1, 1);
296 MODULE_DEPEND(agp_apple, pci, 1, 1, 1);