puc_pci.c revision 119814
1102714Sphk/*	$NetBSD: puc.c,v 1.7 2000/07/29 17:43:38 jlam Exp $	*/
2102714Sphk
3102714Sphk/*-
4102714Sphk * Copyright (c) 2002 JF Hay.  All rights reserved.
5102714Sphk * Copyright (c) 2000 M. Warner Losh.  All rights reserved.
6102714Sphk *
7102714Sphk * Redistribution and use in source and binary forms, with or without
8102714Sphk * modification, are permitted provided that the following conditions
9102714Sphk * are met:
10102714Sphk * 1. Redistributions of source code must retain the above copyright
11102714Sphk *    notice unmodified, this list of conditions, and the following
12102714Sphk *    disclaimer.
13102714Sphk * 2. Redistributions in binary form must reproduce the above copyright
14102714Sphk *    notice, this list of conditions and the following disclaimer in the
15102714Sphk *    documentation and/or other materials provided with the distribution.
16102714Sphk *
17102714Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18102714Sphk * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19102714Sphk * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20102714Sphk * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21102714Sphk * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22102714Sphk * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23102714Sphk * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24102714Sphk * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25102714Sphk * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26102714Sphk * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27102714Sphk */
28102714Sphk
29119418Sobrien#include <sys/cdefs.h>
30119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/puc/puc_pci.c 119814 2003-09-06 21:48:50Z marcel $");
31119418Sobrien
32102714Sphk/*
33102714Sphk * Copyright (c) 1996, 1998, 1999
34102714Sphk *	Christopher G. Demetriou.  All rights reserved.
35102714Sphk *
36102714Sphk * Redistribution and use in source and binary forms, with or without
37102714Sphk * modification, are permitted provided that the following conditions
38102714Sphk * are met:
39102714Sphk * 1. Redistributions of source code must retain the above copyright
40102714Sphk *    notice, this list of conditions and the following disclaimer.
41102714Sphk * 2. Redistributions in binary form must reproduce the above copyright
42102714Sphk *    notice, this list of conditions and the following disclaimer in the
43102714Sphk *    documentation and/or other materials provided with the distribution.
44102714Sphk * 3. All advertising materials mentioning features or use of this software
45102714Sphk *    must display the following acknowledgement:
46102714Sphk *      This product includes software developed by Christopher G. Demetriou
47102714Sphk *	for the NetBSD Project.
48102714Sphk * 4. The name of the author may not be used to endorse or promote products
49102714Sphk *    derived from this software without specific prior written permission
50102714Sphk *
51102714Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
52102714Sphk * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
53102714Sphk * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
54102714Sphk * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
55102714Sphk * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56102714Sphk * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57102714Sphk * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58102714Sphk * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59102714Sphk * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
60102714Sphk * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61102714Sphk */
62102714Sphk
63102714Sphk#include <sys/cdefs.h>
64102714Sphk__FBSDID("$FreeBSD: head/sys/dev/puc/puc_pci.c 119814 2003-09-06 21:48:50Z marcel $");
65102714Sphk
66102751Sjmallett#include "opt_puc.h"
67102751Sjmallett
68102714Sphk#include <sys/param.h>
69102714Sphk#include <sys/systm.h>
70102714Sphk#include <sys/kernel.h>
71102714Sphk#include <sys/bus.h>
72102714Sphk#include <sys/conf.h>
73102714Sphk#include <sys/malloc.h>
74102714Sphk
75102714Sphk#include <machine/bus.h>
76102714Sphk#include <machine/resource.h>
77102714Sphk#include <sys/rman.h>
78102714Sphk
79102714Sphk#include <dev/pci/pcireg.h>
80102714Sphk#include <dev/pci/pcivar.h>
81102714Sphk
82102714Sphk#define PUC_ENTRAILS	1
83102714Sphk#include <dev/puc/pucvar.h>
84102714Sphk
85119814Smarcelextern const struct puc_device_description puc_devices[];
86119814Smarcel
87119814Smarcelint puc_config_win877(struct puc_softc *);
88119814Smarcel
89119814Smarcelstatic const struct puc_device_description *
90119814Smarcelpuc_find_description(uint32_t vend, uint32_t prod, uint32_t svend,
91119814Smarcel    uint32_t sprod)
92119814Smarcel{
93119814Smarcel	int i;
94119814Smarcel
95119814Smarcel#define checkreg(val, index) \
96119814Smarcel    (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
97119814Smarcel
98119814Smarcel	for (i = 0; puc_devices[i].name != NULL; i++) {
99119814Smarcel		if (checkreg(vend, PUC_REG_VEND) &&
100119814Smarcel		    checkreg(prod, PUC_REG_PROD) &&
101119814Smarcel		    checkreg(svend, PUC_REG_SVEND) &&
102119814Smarcel		    checkreg(sprod, PUC_REG_SPROD))
103119814Smarcel			return (&puc_devices[i]);
104119814Smarcel	}
105119814Smarcel
106119814Smarcel#undef checkreg
107119814Smarcel
108119814Smarcel	return (NULL);
109119814Smarcel}
110119814Smarcel
111102714Sphkstatic int
112102714Sphkpuc_pci_probe(device_t dev)
113102714Sphk{
114102714Sphk	uint32_t v1, v2, d1, d2;
115102714Sphk	const struct puc_device_description *desc;
116102714Sphk
117119539Sjhb	if ((pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) != 0)
118102714Sphk		return (ENXIO);
119102714Sphk
120102714Sphk	v1 = pci_read_config(dev, PCIR_VENDOR, 2);
121102714Sphk	d1 = pci_read_config(dev, PCIR_DEVICE, 2);
122102714Sphk	v2 = pci_read_config(dev, PCIR_SUBVEND_0, 2);
123102714Sphk	d2 = pci_read_config(dev, PCIR_SUBDEV_0, 2);
124102714Sphk
125102714Sphk	desc = puc_find_description(v1, d1, v2, d2);
126102714Sphk	if (desc == NULL)
127102714Sphk		return (ENXIO);
128102714Sphk	device_set_desc(dev, desc->name);
129102714Sphk	return (0);
130102714Sphk}
131102714Sphk
132102714Sphkstatic int
133102714Sphkpuc_pci_attach(device_t dev)
134102714Sphk{
135102714Sphk	uint32_t v1, v2, d1, d2;
136102714Sphk
137102714Sphk	v1 = pci_read_config(dev, PCIR_VENDOR, 2);
138102714Sphk	d1 = pci_read_config(dev, PCIR_DEVICE, 2);
139102714Sphk	v2 = pci_read_config(dev, PCIR_SUBVEND_0, 2);
140102714Sphk	d2 = pci_read_config(dev, PCIR_SUBDEV_0, 2);
141102714Sphk	return (puc_attach(dev, puc_find_description(v1, d1, v2, d2)));
142102714Sphk}
143102714Sphk
144102714Sphkstatic device_method_t puc_pci_methods[] = {
145102714Sphk    /* Device interface */
146102714Sphk    DEVMETHOD(device_probe,		puc_pci_probe),
147102714Sphk    DEVMETHOD(device_attach,		puc_pci_attach),
148102714Sphk
149102714Sphk    DEVMETHOD(bus_alloc_resource,	puc_alloc_resource),
150102714Sphk    DEVMETHOD(bus_release_resource,	puc_release_resource),
151102714Sphk    DEVMETHOD(bus_get_resource,		puc_get_resource),
152102714Sphk    DEVMETHOD(bus_read_ivar,		puc_read_ivar),
153102714Sphk    DEVMETHOD(bus_setup_intr,		puc_setup_intr),
154102714Sphk    DEVMETHOD(bus_teardown_intr,	puc_teardown_intr),
155102714Sphk    DEVMETHOD(bus_print_child,		bus_generic_print_child),
156102714Sphk    DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
157102714Sphk    { 0, 0 }
158102714Sphk};
159102714Sphk
160102714Sphkstatic driver_t puc_pci_driver = {
161102714Sphk	"puc",
162102714Sphk	puc_pci_methods,
163102714Sphk	sizeof(struct puc_softc),
164102714Sphk};
165102714Sphk
166102714SphkDRIVER_MODULE(puc, pci, puc_pci_driver, puc_devclass, 0, 0);
167102714SphkDRIVER_MODULE(puc, cardbus, puc_pci_driver, puc_devclass, 0, 0);
168102734Sphk
169102734Sphk
170102734Sphk#define rdspio(indx)		(bus_space_write_1(bst, bsh, efir, indx), \
171102734Sphk				bus_space_read_1(bst, bsh, efdr))
172102734Sphk#define wrspio(indx,data)	(bus_space_write_1(bst, bsh, efir, indx), \
173102734Sphk				bus_space_write_1(bst, bsh, efdr, data))
174102734Sphk
175102734Sphk#ifdef PUC_DEBUG
176102734Sphkstatic void
177102734Sphkpuc_print_win877(bus_space_tag_t bst, bus_space_handle_t bsh, u_int efir,
178102734Sphk	u_int efdr)
179102734Sphk{
180102734Sphk	u_char cr00, cr01, cr04, cr09, cr0d, cr14, cr15, cr16, cr17;
181102734Sphk	u_char cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32;
182102734Sphk
183102734Sphk	cr00 = rdspio(0x00);
184102734Sphk	cr01 = rdspio(0x01);
185102734Sphk	cr04 = rdspio(0x04);
186102734Sphk	cr09 = rdspio(0x09);
187102734Sphk	cr0d = rdspio(0x0d);
188102734Sphk	cr14 = rdspio(0x14);
189102734Sphk	cr15 = rdspio(0x15);
190102734Sphk	cr16 = rdspio(0x16);
191102734Sphk	cr17 = rdspio(0x17);
192102734Sphk	cr18 = rdspio(0x18);
193102734Sphk	cr19 = rdspio(0x19);
194102734Sphk	cr24 = rdspio(0x24);
195102734Sphk	cr25 = rdspio(0x25);
196102734Sphk	cr28 = rdspio(0x28);
197102734Sphk	cr2c = rdspio(0x2c);
198102734Sphk	cr31 = rdspio(0x31);
199102734Sphk	cr32 = rdspio(0x32);
200102734Sphk	printf("877T: cr00 %x, cr01 %x, cr04 %x, cr09 %x, cr0d %x, cr14 %x, "
201102734Sphk	    "cr15 %x, cr16 %x, cr17 %x, cr18 %x, cr19 %x, cr24 %x, cr25 %x, "
202102734Sphk	    "cr28 %x, cr2c %x, cr31 %x, cr32 %x\n", cr00, cr01, cr04, cr09,
203102734Sphk	    cr0d, cr14, cr15, cr16, cr17,
204102734Sphk	    cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32);
205102734Sphk}
206102734Sphk#endif
207102734Sphk
208102734Sphkint
209102734Sphkpuc_config_win877(struct puc_softc *sc)
210102734Sphk{
211102734Sphk	u_char val;
212102734Sphk	u_int efir, efdr;
213102734Sphk	bus_space_tag_t bst;
214102734Sphk	bus_space_handle_t bsh;
215102734Sphk        struct resource *res;
216102734Sphk
217102734Sphk	res = sc->sc_bar_mappings[0].res;
218102734Sphk
219102734Sphk	bst = rman_get_bustag(res);
220102734Sphk	bsh = rman_get_bushandle(res);
221102734Sphk
222102734Sphk	/* configure the first W83877TF */
223102734Sphk	bus_space_write_1(bst, bsh, 0x250, 0x89);
224102734Sphk	efir = 0x251;
225102734Sphk	efdr = 0x252;
226102734Sphk	val = rdspio(0x09) & 0x0f;
227102734Sphk	if (val != 0x0c) {
228102734Sphk		printf("conf_win877: Oops not a W83877TF\n");
229102734Sphk		return (ENXIO);
230102734Sphk	}
231102734Sphk
232102734Sphk#ifdef PUC_DEBUG
233102734Sphk	printf("before: ");
234102734Sphk	puc_print_win877(bst, bsh, efir, efdr);
235102734Sphk#endif
236102734Sphk
237102734Sphk	val = rdspio(0x16);
238102734Sphk	val |= 0x04;
239102734Sphk	wrspio(0x16, val);
240102734Sphk	val &= ~0x04;
241102734Sphk	wrspio(0x16, val);
242102734Sphk
243102734Sphk	wrspio(0x24, 0x2e8 >> 2);
244102734Sphk	wrspio(0x25, 0x2f8 >> 2);
245102734Sphk	wrspio(0x17, 0x03);
246102734Sphk	wrspio(0x28, 0x43);
247102734Sphk
248102734Sphk#ifdef PUC_DEBUG
249102734Sphk	printf("after: ");
250102734Sphk	puc_print_win877(bst, bsh, efir, efdr);
251102734Sphk#endif
252102734Sphk
253102734Sphk	bus_space_write_1(bst, bsh, 0x250, 0xaa);
254102734Sphk
255102734Sphk	/* configure the second W83877TF */
256102734Sphk	bus_space_write_1(bst, bsh, 0x3f0, 0x87);
257102734Sphk	bus_space_write_1(bst, bsh, 0x3f0, 0x87);
258102734Sphk	efir = 0x3f0;
259102734Sphk	efdr = 0x3f1;
260102734Sphk	val = rdspio(0x09) & 0x0f;
261102734Sphk	if (val != 0x0c) {
262102734Sphk		printf("conf_win877: Oops not a W83877TF\n");
263102734Sphk		return(ENXIO);
264102734Sphk	}
265102734Sphk
266102734Sphk#ifdef PUC_DEBUG
267102734Sphk	printf("before: ");
268102734Sphk	puc_print_win877(bst, bsh, efir, efdr);
269102734Sphk#endif
270102734Sphk
271102734Sphk	val = rdspio(0x16);
272102734Sphk	val |= 0x04;
273102734Sphk	wrspio(0x16, val);
274102734Sphk	val &= ~0x04;
275102734Sphk	wrspio(0x16, val);
276102734Sphk
277102734Sphk	wrspio(0x24, 0x3e8 >> 2);
278102734Sphk	wrspio(0x25, 0x3f8 >> 2);
279102734Sphk	wrspio(0x17, 0x03);
280102734Sphk	wrspio(0x28, 0x43);
281102734Sphk
282102734Sphk#ifdef PUC_DEBUG
283102734Sphk	printf("after: ");
284102734Sphk	puc_print_win877(bst, bsh, efir, efdr);
285102734Sphk#endif
286102734Sphk
287102734Sphk	bus_space_write_1(bst, bsh, 0x3f0, 0xaa);
288102734Sphk	return (0);
289102734Sphk}
290102734Sphk
291102734Sphk#undef rdspio
292102734Sphk#undef wrspio
293102734Sphk
294