puc_pci.c revision 140040
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
11140040Simp *    notice, this list of conditions and the following disclaimer.
12102714Sphk * 2. Redistributions in binary form must reproduce the above copyright
13102714Sphk *    notice, this list of conditions and the following disclaimer in the
14102714Sphk *    documentation and/or other materials provided with the distribution.
15102714Sphk *
16140040Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17140040Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18140040Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19140040Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20140040Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21140040Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22140040Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23140040Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24140040Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25140040Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26140040Simp * SUCH DAMAGE.
27102714Sphk */
28102714Sphk
29119418Sobrien#include <sys/cdefs.h>
30119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/puc/puc_pci.c 140040 2005-01-11 06:24:40Z imp $");
31119418Sobrien
32139749Simp/*-
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 140040 2005-01-11 06:24:40Z imp $");
65102714Sphk
66102751Sjmallett#include "opt_puc.h"
67102751Sjmallett
68102714Sphk#include <sys/param.h>
69102714Sphk#include <sys/systm.h>
70102714Sphk#include <sys/kernel.h>
71129879Sphk#include <sys/module.h>
72102714Sphk#include <sys/bus.h>
73102714Sphk#include <sys/conf.h>
74102714Sphk#include <sys/malloc.h>
75102714Sphk
76102714Sphk#include <machine/bus.h>
77102714Sphk#include <machine/resource.h>
78102714Sphk#include <sys/rman.h>
79102714Sphk
80102714Sphk#include <dev/pci/pcireg.h>
81102714Sphk#include <dev/pci/pcivar.h>
82102714Sphk
83102714Sphk#define PUC_ENTRAILS	1
84102714Sphk#include <dev/puc/pucvar.h>
85102714Sphk
86119814Smarcelextern const struct puc_device_description puc_devices[];
87119814Smarcel
88119814Smarcelint puc_config_win877(struct puc_softc *);
89119814Smarcel
90119814Smarcelstatic const struct puc_device_description *
91119814Smarcelpuc_find_description(uint32_t vend, uint32_t prod, uint32_t svend,
92119814Smarcel    uint32_t sprod)
93119814Smarcel{
94119814Smarcel	int i;
95119814Smarcel
96119814Smarcel#define checkreg(val, index) \
97119814Smarcel    (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
98119814Smarcel
99119814Smarcel	for (i = 0; puc_devices[i].name != NULL; i++) {
100119814Smarcel		if (checkreg(vend, PUC_REG_VEND) &&
101119814Smarcel		    checkreg(prod, PUC_REG_PROD) &&
102119814Smarcel		    checkreg(svend, PUC_REG_SVEND) &&
103119814Smarcel		    checkreg(sprod, PUC_REG_SPROD))
104119814Smarcel			return (&puc_devices[i]);
105119814Smarcel	}
106119814Smarcel
107119814Smarcel#undef checkreg
108119814Smarcel
109119814Smarcel	return (NULL);
110119814Smarcel}
111119814Smarcel
112102714Sphkstatic int
113102714Sphkpuc_pci_probe(device_t dev)
114102714Sphk{
115102714Sphk	uint32_t v1, v2, d1, d2;
116102714Sphk	const struct puc_device_description *desc;
117102714Sphk
118119539Sjhb	if ((pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) != 0)
119102714Sphk		return (ENXIO);
120102714Sphk
121102714Sphk	v1 = pci_read_config(dev, PCIR_VENDOR, 2);
122102714Sphk	d1 = pci_read_config(dev, PCIR_DEVICE, 2);
123102714Sphk	v2 = pci_read_config(dev, PCIR_SUBVEND_0, 2);
124102714Sphk	d2 = pci_read_config(dev, PCIR_SUBDEV_0, 2);
125102714Sphk
126102714Sphk	desc = puc_find_description(v1, d1, v2, d2);
127102714Sphk	if (desc == NULL)
128102714Sphk		return (ENXIO);
129102714Sphk	device_set_desc(dev, desc->name);
130102714Sphk	return (0);
131102714Sphk}
132102714Sphk
133102714Sphkstatic int
134102714Sphkpuc_pci_attach(device_t dev)
135102714Sphk{
136102714Sphk	uint32_t v1, v2, d1, d2;
137102714Sphk
138102714Sphk	v1 = pci_read_config(dev, PCIR_VENDOR, 2);
139102714Sphk	d1 = pci_read_config(dev, PCIR_DEVICE, 2);
140102714Sphk	v2 = pci_read_config(dev, PCIR_SUBVEND_0, 2);
141102714Sphk	d2 = pci_read_config(dev, PCIR_SUBDEV_0, 2);
142102714Sphk	return (puc_attach(dev, puc_find_description(v1, d1, v2, d2)));
143102714Sphk}
144102714Sphk
145102714Sphkstatic device_method_t puc_pci_methods[] = {
146102714Sphk    /* Device interface */
147102714Sphk    DEVMETHOD(device_probe,		puc_pci_probe),
148102714Sphk    DEVMETHOD(device_attach,		puc_pci_attach),
149102714Sphk
150102714Sphk    DEVMETHOD(bus_alloc_resource,	puc_alloc_resource),
151102714Sphk    DEVMETHOD(bus_release_resource,	puc_release_resource),
152102714Sphk    DEVMETHOD(bus_get_resource,		puc_get_resource),
153102714Sphk    DEVMETHOD(bus_read_ivar,		puc_read_ivar),
154102714Sphk    DEVMETHOD(bus_setup_intr,		puc_setup_intr),
155102714Sphk    DEVMETHOD(bus_teardown_intr,	puc_teardown_intr),
156102714Sphk    DEVMETHOD(bus_print_child,		bus_generic_print_child),
157102714Sphk    DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
158102714Sphk    { 0, 0 }
159102714Sphk};
160102714Sphk
161102714Sphkstatic driver_t puc_pci_driver = {
162102714Sphk	"puc",
163102714Sphk	puc_pci_methods,
164102714Sphk	sizeof(struct puc_softc),
165102714Sphk};
166102714Sphk
167102714SphkDRIVER_MODULE(puc, pci, puc_pci_driver, puc_devclass, 0, 0);
168123019SimpDRIVER_MODULE(puc, cardbus, puc_pci_driver, puc_devclass, 0, 0);
169102734Sphk
170102734Sphk
171102734Sphk#define rdspio(indx)		(bus_space_write_1(bst, bsh, efir, indx), \
172102734Sphk				bus_space_read_1(bst, bsh, efdr))
173102734Sphk#define wrspio(indx,data)	(bus_space_write_1(bst, bsh, efir, indx), \
174102734Sphk				bus_space_write_1(bst, bsh, efdr, data))
175102734Sphk
176102734Sphk#ifdef PUC_DEBUG
177102734Sphkstatic void
178102734Sphkpuc_print_win877(bus_space_tag_t bst, bus_space_handle_t bsh, u_int efir,
179102734Sphk	u_int efdr)
180102734Sphk{
181102734Sphk	u_char cr00, cr01, cr04, cr09, cr0d, cr14, cr15, cr16, cr17;
182102734Sphk	u_char cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32;
183102734Sphk
184102734Sphk	cr00 = rdspio(0x00);
185102734Sphk	cr01 = rdspio(0x01);
186102734Sphk	cr04 = rdspio(0x04);
187102734Sphk	cr09 = rdspio(0x09);
188102734Sphk	cr0d = rdspio(0x0d);
189102734Sphk	cr14 = rdspio(0x14);
190102734Sphk	cr15 = rdspio(0x15);
191102734Sphk	cr16 = rdspio(0x16);
192102734Sphk	cr17 = rdspio(0x17);
193102734Sphk	cr18 = rdspio(0x18);
194102734Sphk	cr19 = rdspio(0x19);
195102734Sphk	cr24 = rdspio(0x24);
196102734Sphk	cr25 = rdspio(0x25);
197102734Sphk	cr28 = rdspio(0x28);
198102734Sphk	cr2c = rdspio(0x2c);
199102734Sphk	cr31 = rdspio(0x31);
200102734Sphk	cr32 = rdspio(0x32);
201102734Sphk	printf("877T: cr00 %x, cr01 %x, cr04 %x, cr09 %x, cr0d %x, cr14 %x, "
202102734Sphk	    "cr15 %x, cr16 %x, cr17 %x, cr18 %x, cr19 %x, cr24 %x, cr25 %x, "
203102734Sphk	    "cr28 %x, cr2c %x, cr31 %x, cr32 %x\n", cr00, cr01, cr04, cr09,
204102734Sphk	    cr0d, cr14, cr15, cr16, cr17,
205102734Sphk	    cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32);
206102734Sphk}
207102734Sphk#endif
208102734Sphk
209102734Sphkint
210102734Sphkpuc_config_win877(struct puc_softc *sc)
211102734Sphk{
212102734Sphk	u_char val;
213102734Sphk	u_int efir, efdr;
214102734Sphk	bus_space_tag_t bst;
215102734Sphk	bus_space_handle_t bsh;
216102734Sphk        struct resource *res;
217102734Sphk
218102734Sphk	res = sc->sc_bar_mappings[0].res;
219102734Sphk
220102734Sphk	bst = rman_get_bustag(res);
221102734Sphk	bsh = rman_get_bushandle(res);
222102734Sphk
223102734Sphk	/* configure the first W83877TF */
224102734Sphk	bus_space_write_1(bst, bsh, 0x250, 0x89);
225102734Sphk	efir = 0x251;
226102734Sphk	efdr = 0x252;
227102734Sphk	val = rdspio(0x09) & 0x0f;
228102734Sphk	if (val != 0x0c) {
229102734Sphk		printf("conf_win877: Oops not a W83877TF\n");
230102734Sphk		return (ENXIO);
231102734Sphk	}
232102734Sphk
233102734Sphk#ifdef PUC_DEBUG
234102734Sphk	printf("before: ");
235102734Sphk	puc_print_win877(bst, bsh, efir, efdr);
236102734Sphk#endif
237102734Sphk
238102734Sphk	val = rdspio(0x16);
239102734Sphk	val |= 0x04;
240102734Sphk	wrspio(0x16, val);
241102734Sphk	val &= ~0x04;
242102734Sphk	wrspio(0x16, val);
243102734Sphk
244102734Sphk	wrspio(0x24, 0x2e8 >> 2);
245102734Sphk	wrspio(0x25, 0x2f8 >> 2);
246102734Sphk	wrspio(0x17, 0x03);
247102734Sphk	wrspio(0x28, 0x43);
248102734Sphk
249102734Sphk#ifdef PUC_DEBUG
250102734Sphk	printf("after: ");
251102734Sphk	puc_print_win877(bst, bsh, efir, efdr);
252102734Sphk#endif
253102734Sphk
254102734Sphk	bus_space_write_1(bst, bsh, 0x250, 0xaa);
255102734Sphk
256102734Sphk	/* configure the second W83877TF */
257102734Sphk	bus_space_write_1(bst, bsh, 0x3f0, 0x87);
258102734Sphk	bus_space_write_1(bst, bsh, 0x3f0, 0x87);
259102734Sphk	efir = 0x3f0;
260102734Sphk	efdr = 0x3f1;
261102734Sphk	val = rdspio(0x09) & 0x0f;
262102734Sphk	if (val != 0x0c) {
263102734Sphk		printf("conf_win877: Oops not a W83877TF\n");
264102734Sphk		return(ENXIO);
265102734Sphk	}
266102734Sphk
267102734Sphk#ifdef PUC_DEBUG
268102734Sphk	printf("before: ");
269102734Sphk	puc_print_win877(bst, bsh, efir, efdr);
270102734Sphk#endif
271102734Sphk
272102734Sphk	val = rdspio(0x16);
273102734Sphk	val |= 0x04;
274102734Sphk	wrspio(0x16, val);
275102734Sphk	val &= ~0x04;
276102734Sphk	wrspio(0x16, val);
277102734Sphk
278102734Sphk	wrspio(0x24, 0x3e8 >> 2);
279102734Sphk	wrspio(0x25, 0x3f8 >> 2);
280102734Sphk	wrspio(0x17, 0x03);
281102734Sphk	wrspio(0x28, 0x43);
282102734Sphk
283102734Sphk#ifdef PUC_DEBUG
284102734Sphk	printf("after: ");
285102734Sphk	puc_print_win877(bst, bsh, efir, efdr);
286102734Sphk#endif
287102734Sphk
288102734Sphk	bus_space_write_1(bst, bsh, 0x3f0, 0xaa);
289102734Sphk	return (0);
290102734Sphk}
291102734Sphk
292102734Sphk#undef rdspio
293102734Sphk#undef wrspio
294102734Sphk
295