ttwoga_pci.c revision 1.1.2.2 1 1.1.2.2 bouyer /* $NetBSD: ttwoga_pci.c,v 1.1.2.2 2001/01/05 17:33:49 bouyer Exp $ */
2 1.1.2.2 bouyer
3 1.1.2.2 bouyer /*-
4 1.1.2.2 bouyer * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
5 1.1.2.2 bouyer * All rights reserved.
6 1.1.2.2 bouyer *
7 1.1.2.2 bouyer * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.2 bouyer * by Jason R. Thorpe.
9 1.1.2.2 bouyer *
10 1.1.2.2 bouyer * Redistribution and use in source and binary forms, with or without
11 1.1.2.2 bouyer * modification, are permitted provided that the following conditions
12 1.1.2.2 bouyer * are met:
13 1.1.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
14 1.1.2.2 bouyer * notice, this list of conditions and the following disclaimer.
15 1.1.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
17 1.1.2.2 bouyer * documentation and/or other materials provided with the distribution.
18 1.1.2.2 bouyer * 3. All advertising materials mentioning features or use of this software
19 1.1.2.2 bouyer * must display the following acknowledgement:
20 1.1.2.2 bouyer * This product includes software developed by the NetBSD
21 1.1.2.2 bouyer * Foundation, Inc. and its contributors.
22 1.1.2.2 bouyer * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.2.2 bouyer * contributors may be used to endorse or promote products derived
24 1.1.2.2 bouyer * from this software without specific prior written permission.
25 1.1.2.2 bouyer *
26 1.1.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.2.2 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.2.2 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.2.2 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.2.2 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.2.2 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.2.2 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.2.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.2.2 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.2.2 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.2.2 bouyer * POSSIBILITY OF SUCH DAMAGE.
37 1.1.2.2 bouyer */
38 1.1.2.2 bouyer
39 1.1.2.2 bouyer #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
40 1.1.2.2 bouyer
41 1.1.2.2 bouyer __KERNEL_RCSID(0, "$NetBSD: ttwoga_pci.c,v 1.1.2.2 2001/01/05 17:33:49 bouyer Exp $");
42 1.1.2.2 bouyer
43 1.1.2.2 bouyer #include <sys/param.h>
44 1.1.2.2 bouyer #include <sys/systm.h>
45 1.1.2.2 bouyer #include <sys/kernel.h>
46 1.1.2.2 bouyer #include <sys/device.h>
47 1.1.2.2 bouyer #include <sys/lock.h>
48 1.1.2.2 bouyer
49 1.1.2.2 bouyer #include <dev/pci/pcireg.h>
50 1.1.2.2 bouyer #include <dev/pci/pcivar.h>
51 1.1.2.2 bouyer
52 1.1.2.2 bouyer #include <alpha/pci/ttwogareg.h>
53 1.1.2.2 bouyer #include <alpha/pci/ttwogavar.h>
54 1.1.2.2 bouyer
55 1.1.2.2 bouyer void ttwoga_attach_hook(struct device *, struct device *,
56 1.1.2.2 bouyer struct pcibus_attach_args *);
57 1.1.2.2 bouyer int ttwoga_bus_maxdevs(void *, int);
58 1.1.2.2 bouyer pcitag_t ttwoga_make_tag(void *, int, int, int);
59 1.1.2.2 bouyer void ttwoga_decompose_tag(void *, pcitag_t, int *, int *,
60 1.1.2.2 bouyer int *);
61 1.1.2.2 bouyer pcireg_t ttwoga_conf_read(void *, pcitag_t, int);
62 1.1.2.2 bouyer void ttwoga_conf_write(void *, pcitag_t, int, pcireg_t);
63 1.1.2.2 bouyer
64 1.1.2.2 bouyer paddr_t ttwoga_make_type0addr(int, int);
65 1.1.2.2 bouyer
66 1.1.2.2 bouyer /*
67 1.1.2.2 bouyer * The T2 has an annoying bug that can manifest itself while
68 1.1.2.2 bouyer * probing for devices. If a non-existent CBUS address is
69 1.1.2.2 bouyer * accessed with a read, "a few" subsequent write cycles
70 1.1.2.2 bouyer * will generate spurious memory errors. We work around this
71 1.1.2.2 bouyer * by tracking which CPU has the PCI configuration mutex and
72 1.1.2.2 bouyer * "expect" machine checks on that CPU for the duraction of
73 1.1.2.2 bouyer * the PCI configuration access routine.
74 1.1.2.2 bouyer */
75 1.1.2.2 bouyer
76 1.1.2.2 bouyer struct simplelock ttwoga_conf_slock;
77 1.1.2.2 bouyer cpuid_t ttwoga_conf_cpu; /* XXX core logic bug */
78 1.1.2.2 bouyer
79 1.1.2.2 bouyer #define TTWOGA_CONF_LOCK(s) \
80 1.1.2.2 bouyer do { \
81 1.1.2.2 bouyer (s) = splhigh(); \
82 1.1.2.2 bouyer simple_lock(&ttwoga_conf_slock); \
83 1.1.2.2 bouyer ttwoga_conf_cpu = cpu_number(); \
84 1.1.2.2 bouyer } while (0)
85 1.1.2.2 bouyer
86 1.1.2.2 bouyer #define TTWOGA_CONF_UNLOCK(s) \
87 1.1.2.2 bouyer do { \
88 1.1.2.2 bouyer ttwoga_conf_cpu = (cpuid_t)-1; \
89 1.1.2.2 bouyer simple_unlock(&ttwoga_conf_slock); \
90 1.1.2.2 bouyer splx((s)); \
91 1.1.2.2 bouyer } while (0)
92 1.1.2.2 bouyer
93 1.1.2.2 bouyer void
94 1.1.2.2 bouyer ttwoga_pci_init(pci_chipset_tag_t pc, void *v)
95 1.1.2.2 bouyer {
96 1.1.2.2 bouyer
97 1.1.2.2 bouyer simple_lock_init(&ttwoga_conf_slock);
98 1.1.2.2 bouyer
99 1.1.2.2 bouyer pc->pc_conf_v = v;
100 1.1.2.2 bouyer pc->pc_attach_hook = ttwoga_attach_hook;
101 1.1.2.2 bouyer pc->pc_bus_maxdevs = ttwoga_bus_maxdevs;
102 1.1.2.2 bouyer pc->pc_make_tag = ttwoga_make_tag;
103 1.1.2.2 bouyer pc->pc_decompose_tag = ttwoga_decompose_tag;
104 1.1.2.2 bouyer pc->pc_conf_read = ttwoga_conf_read;
105 1.1.2.2 bouyer pc->pc_conf_write = ttwoga_conf_write;
106 1.1.2.2 bouyer }
107 1.1.2.2 bouyer
108 1.1.2.2 bouyer void
109 1.1.2.2 bouyer ttwoga_attach_hook(struct device *parent, struct device *self,
110 1.1.2.2 bouyer struct pcibus_attach_args *pba)
111 1.1.2.2 bouyer {
112 1.1.2.2 bouyer }
113 1.1.2.2 bouyer
114 1.1.2.2 bouyer int
115 1.1.2.2 bouyer ttwoga_bus_maxdevs(void *cpv, int busno)
116 1.1.2.2 bouyer {
117 1.1.2.2 bouyer
118 1.1.2.2 bouyer return 32;
119 1.1.2.2 bouyer }
120 1.1.2.2 bouyer
121 1.1.2.2 bouyer pcitag_t
122 1.1.2.2 bouyer ttwoga_make_tag(void *cpv, int b, int d, int f)
123 1.1.2.2 bouyer {
124 1.1.2.2 bouyer
125 1.1.2.2 bouyer /* This is the format used for Type 1 configuration cycles. */
126 1.1.2.2 bouyer return (b << 16) | (d << 11) | (f << 8);
127 1.1.2.2 bouyer }
128 1.1.2.2 bouyer
129 1.1.2.2 bouyer void
130 1.1.2.2 bouyer ttwoga_decompose_tag(void *cpv, pcitag_t tag, int *bp, int *dp, int *fp)
131 1.1.2.2 bouyer {
132 1.1.2.2 bouyer
133 1.1.2.2 bouyer if (bp != NULL)
134 1.1.2.2 bouyer *bp = (tag >> 16) & 0xff;
135 1.1.2.2 bouyer if (dp != NULL)
136 1.1.2.2 bouyer *dp = (tag >> 11) & 0x1f;
137 1.1.2.2 bouyer if (fp != NULL)
138 1.1.2.2 bouyer *fp = (tag >> 8) & 0x7;
139 1.1.2.2 bouyer }
140 1.1.2.2 bouyer
141 1.1.2.2 bouyer paddr_t
142 1.1.2.2 bouyer ttwoga_make_type0addr(int d, int f)
143 1.1.2.2 bouyer {
144 1.1.2.2 bouyer
145 1.1.2.2 bouyer if (d > 8) /* XXX ??? */
146 1.1.2.2 bouyer return ((paddr_t) -1);
147 1.1.2.2 bouyer return ((0x0800UL << d) | (f << 8));
148 1.1.2.2 bouyer }
149 1.1.2.2 bouyer
150 1.1.2.2 bouyer pcireg_t
151 1.1.2.2 bouyer ttwoga_conf_read(void *cpv, pcitag_t tag, int offset)
152 1.1.2.2 bouyer {
153 1.1.2.2 bouyer struct ttwoga_config *tcp = cpv;
154 1.1.2.2 bouyer pcireg_t *datap, data;
155 1.1.2.2 bouyer int s, b, d, f, ba;
156 1.1.2.2 bouyer paddr_t addr;
157 1.1.2.2 bouyer u_int64_t old_hae3;
158 1.1.2.2 bouyer
159 1.1.2.2 bouyer alpha_pci_decompose_tag(&tcp->tc_pc, tag, &b, &d, &f);
160 1.1.2.2 bouyer
161 1.1.2.2 bouyer addr = b ? tag : ttwoga_make_type0addr(d, f);
162 1.1.2.2 bouyer if (addr == (paddr_t)-1)
163 1.1.2.2 bouyer return ((pcireg_t) -1);
164 1.1.2.2 bouyer
165 1.1.2.2 bouyer TTWOGA_CONF_LOCK(s);
166 1.1.2.2 bouyer
167 1.1.2.2 bouyer alpha_mb();
168 1.1.2.2 bouyer old_hae3 = T2GA(tcp, T2_HAE0_3) & ~HAE0_3_PCA;
169 1.1.2.2 bouyer T2GA(tcp, T2_HAE0_3) =
170 1.1.2.2 bouyer old_hae3 | ((b ? 1UL : 0UL) << HAE0_3_PCA_SHIFT);
171 1.1.2.2 bouyer alpha_mb();
172 1.1.2.2 bouyer alpha_mb();
173 1.1.2.2 bouyer
174 1.1.2.2 bouyer datap =
175 1.1.2.2 bouyer (pcireg_t *)ALPHA_PHYS_TO_K0SEG(tcp->tc_sysmap->tsmap_conf_base |
176 1.1.2.2 bouyer addr << 5UL | /* address shift */
177 1.1.2.2 bouyer (offset & ~0x03) << 5UL | /* address shift */
178 1.1.2.2 bouyer 0x3 << 3UL); /* 4-byte, size shift */
179 1.1.2.2 bouyer data = (pcireg_t)-1;
180 1.1.2.2 bouyer if (!(ba = badaddr(datap, sizeof *datap)))
181 1.1.2.2 bouyer data = *datap;
182 1.1.2.2 bouyer
183 1.1.2.2 bouyer alpha_mb();
184 1.1.2.2 bouyer T2GA(tcp, T2_HAE0_3) = old_hae3;
185 1.1.2.2 bouyer alpha_mb();
186 1.1.2.2 bouyer alpha_mb();
187 1.1.2.2 bouyer
188 1.1.2.2 bouyer alpha_pal_draina();
189 1.1.2.2 bouyer alpha_mb();
190 1.1.2.2 bouyer alpha_mb();
191 1.1.2.2 bouyer
192 1.1.2.2 bouyer TTWOGA_CONF_UNLOCK(s);
193 1.1.2.2 bouyer
194 1.1.2.2 bouyer #if 0
195 1.1.2.2 bouyer printf("ttwoga_conf_read: tag 0x%lx, reg 0x%x -> 0x%x @ %p%s\n",
196 1.1.2.2 bouyer tag, offset, data, datap, ba ? " (badaddr)" : "");
197 1.1.2.2 bouyer #endif
198 1.1.2.2 bouyer
199 1.1.2.2 bouyer return (data);
200 1.1.2.2 bouyer }
201 1.1.2.2 bouyer
202 1.1.2.2 bouyer void
203 1.1.2.2 bouyer ttwoga_conf_write(void *cpv, pcitag_t tag, int offset, pcireg_t data)
204 1.1.2.2 bouyer {
205 1.1.2.2 bouyer struct ttwoga_config *tcp = cpv;
206 1.1.2.2 bouyer pcireg_t *datap;
207 1.1.2.2 bouyer int s, b, d, f;
208 1.1.2.2 bouyer paddr_t addr;
209 1.1.2.2 bouyer u_int64_t old_hae3;
210 1.1.2.2 bouyer
211 1.1.2.2 bouyer alpha_pci_decompose_tag(&tcp->tc_pc, tag, &b, &d, &f);
212 1.1.2.2 bouyer
213 1.1.2.2 bouyer addr = b ? tag : ttwoga_make_type0addr(d, f);
214 1.1.2.2 bouyer if (addr == (paddr_t)-1)
215 1.1.2.2 bouyer return;
216 1.1.2.2 bouyer
217 1.1.2.2 bouyer TTWOGA_CONF_LOCK(s);
218 1.1.2.2 bouyer
219 1.1.2.2 bouyer alpha_mb();
220 1.1.2.2 bouyer old_hae3 = T2GA(tcp, T2_HAE0_3) & ~HAE0_3_PCA;
221 1.1.2.2 bouyer T2GA(tcp, T2_HAE0_3) =
222 1.1.2.2 bouyer old_hae3 | ((b ? 1UL : 0UL) << HAE0_3_PCA_SHIFT);
223 1.1.2.2 bouyer alpha_mb();
224 1.1.2.2 bouyer alpha_mb();
225 1.1.2.2 bouyer
226 1.1.2.2 bouyer datap =
227 1.1.2.2 bouyer (pcireg_t *)ALPHA_PHYS_TO_K0SEG(tcp->tc_sysmap->tsmap_conf_base |
228 1.1.2.2 bouyer addr << 5UL | /* address shift */
229 1.1.2.2 bouyer (offset & ~0x03) << 5UL | /* address shift */
230 1.1.2.2 bouyer 0x3 << 3UL); /* 4-byte, size shift */
231 1.1.2.2 bouyer
232 1.1.2.2 bouyer alpha_mb();
233 1.1.2.2 bouyer *datap = data;
234 1.1.2.2 bouyer alpha_mb();
235 1.1.2.2 bouyer alpha_mb();
236 1.1.2.2 bouyer
237 1.1.2.2 bouyer alpha_mb();
238 1.1.2.2 bouyer T2GA(tcp, T2_HAE0_3) = old_hae3;
239 1.1.2.2 bouyer alpha_mb();
240 1.1.2.2 bouyer alpha_mb();
241 1.1.2.2 bouyer
242 1.1.2.2 bouyer TTWOGA_CONF_UNLOCK(s);
243 1.1.2.2 bouyer
244 1.1.2.2 bouyer #if 0
245 1.1.2.2 bouyer printf("ttwoga_conf_write: tag 0x%lx, reg 0x%x -> 0x%x @ %p\n",
246 1.1.2.2 bouyer tag, offset, data, datap);
247 1.1.2.2 bouyer #endif
248 1.1.2.2 bouyer }
249