cia_pci.c revision 1.5
1/*	$OpenBSD: cia_pci.c,v 1.5 1997/01/24 19:57:39 niklas Exp $	*/
2/*	$NetBSD: cia_pci.c,v 1.7 1996/11/23 06:46:50 cgd Exp $	*/
3
4/*
5 * Copyright (c) 1995, 1996 Carnegie-Mellon University.
6 * All rights reserved.
7 *
8 * Author: Chris G. Demetriou
9 *
10 * Permission to use, copy, modify and distribute this software and
11 * its documentation is hereby granted, provided that both the copyright
12 * notice and this permission notice appear in all copies of the
13 * software, derivative works or modified versions, and any portions
14 * thereof, and that both notices appear in supporting documentation.
15 *
16 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
17 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
18 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
19 *
20 * Carnegie Mellon requests users of this software to return to
21 *
22 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
23 *  School of Computer Science
24 *  Carnegie Mellon University
25 *  Pittsburgh PA 15213-3890
26 *
27 * any improvements or extensions that they make and grant Carnegie the
28 * rights to redistribute these changes.
29 */
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/kernel.h>
34#include <sys/device.h>
35#include <vm/vm.h>
36
37#include <machine/autoconf.h>	/* badaddr proto */
38
39#include <dev/pci/pcireg.h>
40#include <dev/pci/pcivar.h>
41#include <alpha/pci/ciareg.h>
42#include <alpha/pci/ciavar.h>
43
44#include <machine/rpb.h>	/* XXX for eb164 CIA firmware workarounds. */
45#include "dec_eb164.h"		/* XXX for eb164 CIA firmware workarounds. */
46
47void		cia_attach_hook __P((struct device *, struct device *,
48		    struct pcibus_attach_args *));
49int		cia_bus_maxdevs __P((void *, int));
50pcitag_t	cia_make_tag __P((void *, int, int, int));
51void		cia_decompose_tag __P((void *, pcitag_t, int *, int *,
52		    int *));
53pcireg_t	cia_conf_read __P((void *, pcitag_t, int));
54void		cia_conf_write __P((void *, pcitag_t, int, pcireg_t));
55
56void
57cia_pci_init(pc, v)
58	pci_chipset_tag_t pc;
59	void *v;
60{
61
62	pc->pc_conf_v = v;
63	pc->pc_attach_hook = cia_attach_hook;
64	pc->pc_bus_maxdevs = cia_bus_maxdevs;
65	pc->pc_make_tag = cia_make_tag;
66	pc->pc_decompose_tag = cia_decompose_tag;
67	pc->pc_conf_read = cia_conf_read;
68	pc->pc_conf_write = cia_conf_write;
69}
70
71void
72cia_attach_hook(parent, self, pba)
73	struct device *parent, *self;
74	struct pcibus_attach_args *pba;
75{
76}
77
78int
79cia_bus_maxdevs(cpv, busno)
80	void *cpv;
81	int busno;
82{
83
84	return 32;
85}
86
87pcitag_t
88cia_make_tag(cpv, b, d, f)
89	void *cpv;
90	int b, d, f;
91{
92
93	return (b << 16) | (d << 11) | (f << 8);
94}
95
96void
97cia_decompose_tag(cpv, tag, bp, dp, fp)
98	void *cpv;
99	pcitag_t tag;
100	int *bp, *dp, *fp;
101{
102
103	if (bp != NULL)
104		*bp = (tag >> 16) & 0xff;
105	if (dp != NULL)
106		*dp = (tag >> 11) & 0x1f;
107	if (fp != NULL)
108		*fp = (tag >> 8) & 0x7;
109}
110
111pcireg_t
112cia_conf_read(cpv, tag, offset)
113	void *cpv;
114	pcitag_t tag;
115	int offset;
116{
117	struct cia_config *ccp = cpv;
118	pcireg_t *datap, data;
119	int s, secondary, ba;
120	int32_t old_haxr2;					/* XXX */
121#if NDEC_EB164
122	extern int cputype;					/* XXX */
123#endif
124
125#ifdef DIAGNOSTIC
126	s = 0;					/* XXX gcc -Wuninitialized */
127	old_haxr2 = 0;				/* XXX gcc -Wuninitialized */
128#endif
129
130#if NDEC_EB164
131	/*
132	 * Some (apparently-common) revisions of EB164 firmware do the
133	 * Wrong thing with PCI master aborts, which are caused by
134	 * accesing the configuration space of devices that don't
135	 * exist (for example).
136	 *
137	 * On EB164's we clear the CIA error register's PCI master
138	 * abort bit before touching PCI configuration space and
139	 * check it afterwards.  If it indicates a master abort,
140	 * the device wasn't there so we return 0xffffffff.
141	 */
142	if (cputype == ST_EB164) {
143		/* clear the PCI master abort bit in CIA error register */
144		REGVAL(CIA_CSR_CIA_ERR) = 0x00000080;		/* XXX */
145		alpha_mb();
146		alpha_pal_draina();
147	}
148#endif
149
150	/* secondary if bus # != 0 */
151	pci_decompose_tag(&ccp->cc_pc, tag, &secondary, 0, 0);
152	if (secondary) {
153		s = splhigh();
154		old_haxr2 = REGVAL(CIA_CSRS + 0x480);		/* XXX */
155		alpha_mb();
156		REGVAL(CIA_CSRS + 0x480) = old_haxr2 | 0x1;	/* XXX */
157		alpha_mb();
158	}
159
160	datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_PCI_CONF |
161	    tag << 5UL |					/* XXX */
162	    (offset & ~0x03) << 5 |				/* XXX */
163	    0 << 5 |						/* XXX */
164	    0x3 << 3);						/* XXX */
165	data = (pcireg_t)-1;
166	if (!(ba = badaddr(datap, sizeof *datap)))
167		data = *datap;
168
169	if (secondary) {
170		alpha_mb();
171		REGVAL(CIA_CSRS + 0x480) = old_haxr2;		/* XXX */
172		alpha_mb();
173		splx(s);
174	}
175
176#if NDEC_EB164
177	if (cputype == ST_EB164) {
178		alpha_pal_draina();
179		/* check CIA error register for PCI master abort */
180		if (REGVAL(CIA_CSR_CIA_ERR) & 0x00000080) {	/* XXX */
181			ba = 1;
182			data = 0xffffffff;
183		}
184	}
185#endif
186
187#if 0
188	printf("cia_conf_read: tag 0x%lx, reg 0x%lx -> %x @ %p%s\n", tag, reg,
189	    data, datap, ba ? " (badaddr)" : "");
190#endif
191
192	return data;
193}
194
195void
196cia_conf_write(cpv, tag, offset, data)
197	void *cpv;
198	pcitag_t tag;
199	int offset;
200	pcireg_t data;
201{
202	struct cia_config *ccp = cpv;
203	pcireg_t *datap;
204	int s, secondary;
205	int32_t old_haxr2;					/* XXX */
206
207#ifdef DIAGNOSTIC
208	s = 0;					/* XXX gcc -Wuninitialized */
209	old_haxr2 = 0;				/* XXX gcc -Wuninitialized */
210#endif
211
212	/* secondary if bus # != 0 */
213	pci_decompose_tag(&ccp->cc_pc, tag, &secondary, 0, 0);
214	if (secondary) {
215		s = splhigh();
216		old_haxr2 = REGVAL(CIA_CSRS + 0x480);		/* XXX */
217		alpha_mb();
218		REGVAL(CIA_CSRS + 0x480) = old_haxr2 | 0x1;	/* XXX */
219		alpha_mb();
220	}
221
222	datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_PCI_CONF |
223	    tag << 5UL |					/* XXX */
224	    (offset & ~0x03) << 5 |				/* XXX */
225	    0 << 5 |						/* XXX */
226	    0x3 << 3);						/* XXX */
227	*datap = data;
228
229	if (secondary) {
230		alpha_mb();
231		REGVAL(CIA_CSRS + 0x480) = old_haxr2;		/* XXX */
232		alpha_mb();
233		splx(s);
234	}
235
236#if 0
237	printf("cia_conf_write: tag 0x%lx, reg 0x%lx -> 0x%x @ %p\n", tag,
238	    reg, data, datap);
239#endif
240}
241