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