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