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