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