vtpbc.c revision 1.1 1 /* $NetBSD: vtpbc.c,v 1.1 2001/05/28 16:22:21 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2001 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 /*
40 * Support for the V3 Semiconductor i960 PCI bus controller. This appears
41 * on some MIPS boards (notably Algorithmics P-4032 and P-5064).
42 *
43 * Some help was provided by the Algorithmics PMON sources.
44 */
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/device.h>
49
50 #include <machine/bus.h>
51 #include <machine/intr.h>
52 #include <machine/locore.h>
53
54 #include <dev/pci/pcireg.h>
55 #include <dev/pci/pcivar.h>
56
57 #include <algor/pci/vtpbcreg.h>
58 #include <algor/pci/vtpbcvar.h>
59
60 struct vtpbc_config vtpbc_configuration;
61
62 #define PCI_CONF_LOCK(s) (s) = splhigh()
63 #define PCI_CONF_UNLOCK(s) splx((s))
64
65 const char *vtpbc_revs[] = {
66 "A",
67 "B0",
68 "B1",
69 "B2",
70 "C0",
71 };
72 const int vtpbc_nrevs = sizeof(vtpbc_revs) / sizeof(vtpbc_revs[0]);
73
74 void vtpbc_attach_hook(struct device *, struct device *,
75 struct pcibus_attach_args *);
76 int vtpbc_bus_maxdevs(void *, int);
77 pcitag_t vtpbc_make_tag(void *, int, int, int);
78 void vtpbc_decompose_tag(void *, pcitag_t, int *, int *, int *);
79 pcireg_t vtpbc_conf_read(void *, pcitag_t, int);
80 void vtpbc_conf_write(void *, pcitag_t, int, pcireg_t);
81
82 /*
83 * vtpbc_init:
84 *
85 * Initialize the V3 PCI controller's software state. We
86 * simply use the existing windows that the firmware has
87 * set up for us.
88 */
89 void
90 vtpbc_init(pci_chipset_tag_t pc, struct vtpbc_config *vt)
91 {
92
93 pc->pc_conf_v = vt;
94 pc->pc_attach_hook = vtpbc_attach_hook;
95 pc->pc_bus_maxdevs = vtpbc_bus_maxdevs;
96 pc->pc_make_tag = vtpbc_make_tag;
97 pc->pc_decompose_tag = vtpbc_decompose_tag;
98 pc->pc_conf_read = vtpbc_conf_read;
99 pc->pc_conf_write = vtpbc_conf_write;
100
101 vt->vt_rev = V96X_PCI_CC_REV(vt) & V96X_PCI_CC_REV_VREV;
102 }
103
104 void
105 vtpbc_attach_hook(struct device *parent, struct device *self,
106 struct pcibus_attach_args *pba)
107 {
108 }
109
110 int
111 vtpbc_bus_maxdevs(void *v, int busno)
112 {
113
114 return (32);
115 }
116
117 pcitag_t
118 vtpbc_make_tag(void *v, int b, int d, int f)
119 {
120
121 return ((b << 16) | (d << 11) | (f << 8));
122 }
123
124 void
125 vtpbc_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
126 {
127
128 if (bp != NULL)
129 *bp = (tag >> 16) & 0xff;
130 if (dp != NULL)
131 *dp = (tag >> 11) & 0x1f;
132 if (fp != NULL)
133 *fp = (tag >> 8) & 0x7;
134 }
135
136 static int
137 vtpbc_conf_addr(struct vtpbc_config *vt, pcitag_t tag, int offset,
138 u_int32_t *cfgoff, u_int32_t *ad_low)
139 {
140 int b, d, f;
141
142 vtpbc_decompose_tag(vt, tag, &b, &d, &f);
143
144 if (b == 0) {
145 if (d > (31 - vt->vt_adbase))
146 return (1);
147 *cfgoff = (1UL << (d + vt->vt_adbase)) | (f << 8) |
148 offset;
149 *ad_low = 0;
150 } else if (vt->vt_rev >= V96X_VREV_C0) {
151 *cfgoff = tag | offset;
152 *ad_low = V96X_LB_MAPx_AD_LOW_EN;
153 } else
154 return (1);
155
156 return (0);
157 }
158
159 pcireg_t
160 vtpbc_conf_read(void *v, pcitag_t tag, int offset)
161 {
162 struct vtpbc_config *vt = v;
163 pcireg_t data;
164 u_int32_t cfgoff, ad_low;
165 int s;
166 u_int16_t errbits;
167
168 if (vtpbc_conf_addr(vt, tag, offset, &cfgoff, &ad_low))
169 return ((pcireg_t) -1);
170
171 PCI_CONF_LOCK(s);
172
173 /* high 12 bits of address go into map register */
174 V96X_LB_MAP0(vt) = ((cfgoff >> 16) & V96X_LB_MAPx_MAP_ADR) |
175 ad_low | V96X_LB_TYPE_CONF;
176
177 /* clear aborts */
178 V96X_PCI_STAT(vt) |= V96X_PCI_STAT_M_ABORT | V96X_PCI_STAT_T_ABORT;
179
180 wbflush();
181
182 /* low 20 bits of address are offset into config space */
183 data = *(__volatile u_int32_t *) (vt->vt_cfgbase + (cfgoff & 0xfffff));
184
185 errbits = V96X_PCI_STAT(vt) &
186 (V96X_PCI_STAT_M_ABORT|V96X_PCI_STAT_T_ABORT);
187 if (errbits) {
188 V96X_PCI_STAT(vt) |= errbits;
189 data = (pcireg_t) -1;
190 }
191
192 PCI_CONF_UNLOCK(s);
193
194 return (data);
195 }
196
197 void
198 vtpbc_conf_write(void *v, pcitag_t tag, int offset, pcireg_t data)
199 {
200 struct vtpbc_config *vt = v;
201 u_int32_t cfgoff, ad_low;
202 int s;
203
204 if (vtpbc_conf_addr(vt, tag, offset, &cfgoff, &ad_low))
205 panic("vtpbc_conf_write");
206
207 PCI_CONF_LOCK(s);
208
209 /* high 12 bits of address go into map register */
210 V96X_LB_MAP0(vt) = ((cfgoff >> 16) & V96X_LB_MAPx_MAP_ADR) |
211 ad_low | V96X_LB_TYPE_CONF;
212
213 /* clear aborts */
214 V96X_PCI_STAT(vt) |= V96X_PCI_STAT_M_ABORT | V96X_PCI_STAT_T_ABORT;
215
216 wbflush();
217
218 /* low 20 bits of address are offset into config space */
219 *(__volatile u_int32_t *) (vt->vt_cfgbase + (cfgoff & 0xfffff)) = data;
220
221 /* wait for FIFO to drain */
222 while (V96X_FIFO_STAT(vt) & V96X_FIFO_STAT_L2P_WR)
223 /* spin */ ;
224
225 PCI_CONF_UNLOCK(s);
226 }
227