cia_pci.c revision 1.28 1 /* $NetBSD: cia_pci.c,v 1.28 2009/03/14 15:35:59 dsl 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.28 2009/03/14 15:35:59 dsl Exp $");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/device.h>
38
39 #include <uvm/uvm_extern.h>
40
41 #include <dev/pci/pcireg.h>
42 #include <dev/pci/pcivar.h>
43 #include <alpha/pci/ciareg.h>
44 #include <alpha/pci/ciavar.h>
45
46 void cia_attach_hook(struct device *, struct device *,
47 struct pcibus_attach_args *);
48 int cia_bus_maxdevs(void *, int);
49 pcitag_t cia_make_tag(void *, int, int, int);
50 void cia_decompose_tag(void *, pcitag_t, int *, int *,
51 int *);
52 pcireg_t cia_conf_read(void *, pcitag_t, int);
53 void cia_conf_write(void *, pcitag_t, int, pcireg_t);
54
55 void
56 cia_pci_init(pci_chipset_tag_t pc, void *v)
57 {
58
59 pc->pc_conf_v = v;
60 pc->pc_attach_hook = cia_attach_hook;
61 pc->pc_bus_maxdevs = cia_bus_maxdevs;
62 pc->pc_make_tag = cia_make_tag;
63 pc->pc_decompose_tag = cia_decompose_tag;
64 pc->pc_conf_read = cia_conf_read;
65 pc->pc_conf_write = cia_conf_write;
66 }
67
68 void
69 cia_attach_hook(parent, self, pba)
70 struct device *parent, *self;
71 struct pcibus_attach_args *pba;
72 {
73 }
74
75 int
76 cia_bus_maxdevs(void *cpv, int busno)
77 {
78
79 return 32;
80 }
81
82 pcitag_t
83 cia_make_tag(cpv, b, d, f)
84 void *cpv;
85 int b, d, f;
86 {
87
88 return (b << 16) | (d << 11) | (f << 8);
89 }
90
91 void
92 cia_decompose_tag(cpv, tag, bp, dp, fp)
93 void *cpv;
94 pcitag_t tag;
95 int *bp, *dp, *fp;
96 {
97
98 if (bp != NULL)
99 *bp = (tag >> 16) & 0xff;
100 if (dp != NULL)
101 *dp = (tag >> 11) & 0x1f;
102 if (fp != NULL)
103 *fp = (tag >> 8) & 0x7;
104 }
105
106 pcireg_t
107 cia_conf_read(void *cpv, pcitag_t tag, int offset)
108 {
109 struct cia_config *ccp = cpv;
110 pcireg_t *datap, data;
111 int s, secondary, ba;
112 u_int32_t old_cfg, errbits;
113
114 #ifdef __GNUC__
115 s = 0; /* XXX gcc -Wuninitialized */
116 old_cfg = 0; /* XXX gcc -Wuninitialized */
117 #endif
118
119 /*
120 * Some (apparently-common) revisions of EB164 and AlphaStation
121 * firmware do the Wrong thing with PCI master and target aborts,
122 * which are caused by accesing the configuration space of devices
123 * that don't exist (for example).
124 *
125 * To work around this, we clear the CIA error register's PCI
126 * master and target abort bits before touching PCI configuration
127 * space and check it afterwards. If it indicates a master or target
128 * abort, the device wasn't there so we return 0xffffffff.
129 */
130 REGVAL(CIA_CSR_CIA_ERR) = CIA_ERR_RCVD_MAS_ABT|CIA_ERR_RCVD_TAR_ABT;
131 alpha_mb();
132 alpha_pal_draina();
133
134 /* secondary if bus # != 0 */
135 pci_decompose_tag(&ccp->cc_pc, tag, &secondary, 0, 0);
136 if (secondary) {
137 s = splhigh();
138 old_cfg = REGVAL(CIA_CSR_CFG);
139 alpha_mb();
140 REGVAL(CIA_CSR_CFG) = old_cfg | 0x1;
141 alpha_mb();
142 }
143
144 /*
145 * We just inline the BWX support, since this is the only
146 * difference between BWX and swiz for config space.
147 */
148 if (ccp->cc_flags & CCF_PCI_USE_BWX) {
149 if (secondary) {
150 datap =
151 (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_EV56_BWCONF1 |
152 tag | (offset & ~0x03));
153 } else {
154 datap =
155 (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_EV56_BWCONF0 |
156 tag | (offset & ~0x03));
157 }
158 } else {
159 datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_PCI_CONF |
160 tag << 5UL | /* XXX */
161 (offset & ~0x03) << 5 | /* XXX */
162 0 << 5 | /* XXX */
163 0x3 << 3); /* XXX */
164 }
165 data = (pcireg_t)-1;
166 alpha_mb();
167 if (!(ba = badaddr(datap, sizeof *datap)))
168 data = *datap;
169 alpha_mb();
170 alpha_mb();
171
172 if (secondary) {
173 alpha_mb();
174 REGVAL(CIA_CSR_CFG) = old_cfg;
175 alpha_mb();
176 splx(s);
177 }
178
179 alpha_pal_draina();
180 alpha_mb();
181 errbits = REGVAL(CIA_CSR_CIA_ERR);
182 if (errbits & (CIA_ERR_RCVD_MAS_ABT|CIA_ERR_RCVD_TAR_ABT)) {
183 ba = 1;
184 data = 0xffffffff;
185 }
186
187 if (errbits) {
188 REGVAL(CIA_CSR_CIA_ERR) = errbits;
189 alpha_mb();
190 alpha_pal_draina();
191 }
192
193 #if 0
194 printf("cia_conf_read: tag 0x%lx, reg 0x%lx -> %x @ %p%s\n", tag, reg,
195 data, datap, ba ? " (badaddr)" : "");
196 #endif
197
198 return data;
199 }
200
201 void
202 cia_conf_write(void *cpv, pcitag_t tag, int offset, pcireg_t data)
203 {
204 struct cia_config *ccp = cpv;
205 pcireg_t *datap;
206 int s, secondary;
207 u_int32_t old_cfg;
208
209 #ifdef __GNUC__
210 s = 0; /* XXX gcc -Wuninitialized */
211 old_cfg = 0; /* XXX gcc -Wuninitialized */
212 #endif
213
214 /* secondary if bus # != 0 */
215 pci_decompose_tag(&ccp->cc_pc, tag, &secondary, 0, 0);
216 if (secondary) {
217 s = splhigh();
218 old_cfg = REGVAL(CIA_CSR_CFG);
219 alpha_mb();
220 REGVAL(CIA_CSR_CFG) = old_cfg | 0x1;
221 alpha_mb();
222 }
223
224 /*
225 * We just inline the BWX support, since this is the only
226 * difference between BWX and swiz for config space.
227 */
228 if (ccp->cc_flags & CCF_PCI_USE_BWX) {
229 if (secondary) {
230 datap =
231 (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_EV56_BWCONF1 |
232 tag | (offset & ~0x03));
233 } else {
234 datap =
235 (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_EV56_BWCONF0 |
236 tag | (offset & ~0x03));
237 }
238 } else {
239 datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_PCI_CONF |
240 tag << 5UL | /* XXX */
241 (offset & ~0x03) << 5 | /* XXX */
242 0 << 5 | /* XXX */
243 0x3 << 3); /* XXX */
244 }
245 alpha_mb();
246 *datap = data;
247 alpha_mb();
248 alpha_mb();
249
250 if (secondary) {
251 alpha_mb();
252 REGVAL(CIA_CSR_CFG) = old_cfg;
253 alpha_mb();
254 splx(s);
255 }
256
257 #if 0
258 printf("cia_conf_write: tag 0x%lx, reg 0x%lx -> 0x%x @ %p\n", tag,
259 reg, data, datap);
260 #endif
261 }
262