dwlpx_pci.c revision 1.5 1 /* $NetBSD: dwlpx_pci.c,v 1.5 1997/04/07 02:01:18 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1997 by Matthew Jacob
5 * NASA AMES Research Center.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice immediately at the beginning of the file, without modification,
13 * this list of conditions, and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <machine/options.h> /* Pull in config options headers */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <vm/vm.h>
40
41 #include <dev/pci/pcireg.h>
42 #include <dev/pci/pcivar.h>
43 #include <alpha/tlsb/tlsbreg.h>
44 #include <alpha/pci/dwlpxreg.h>
45 #include <alpha/pci/dwlpxvar.h>
46
47 #define DO_SECONDARIES 1
48
49 #define KV(_addr) ((caddr_t)ALPHA_PHYS_TO_K0SEG((_addr)))
50
51 void dwlpx_attach_hook __P((struct device *, struct device *,
52 struct pcibus_attach_args *));
53 int dwlpx_bus_maxdevs __P((void *, int));
54 pcitag_t dwlpx_make_tag __P((void *, int, int, int));
55 void dwlpx_decompose_tag __P((void *, pcitag_t, int *, int *,
56 int *));
57 pcireg_t dwlpx_conf_read __P((void *, pcitag_t, int));
58 void dwlpx_conf_write __P((void *, pcitag_t, int, pcireg_t));
59
60 void
61 dwlpx_pci_init(pc, v)
62 pci_chipset_tag_t pc;
63 void *v;
64 {
65 pc->pc_conf_v = v;
66 pc->pc_attach_hook = dwlpx_attach_hook;
67 pc->pc_bus_maxdevs = dwlpx_bus_maxdevs;
68 pc->pc_make_tag = dwlpx_make_tag;
69 pc->pc_decompose_tag = dwlpx_decompose_tag;
70 pc->pc_conf_read = dwlpx_conf_read;
71 pc->pc_conf_write = dwlpx_conf_write;
72 }
73
74 void
75 dwlpx_attach_hook(parent, self, pba)
76 struct device *parent, *self;
77 struct pcibus_attach_args *pba;
78 {
79 #if 0
80 struct dwlpx_config *ccp = pba->pba_pc->pc_conf_v;
81 printf("dwlpx_attach_hook for %s\n", ccp->cc_sc->dwlpx_dev.dv_xname);
82 #endif
83 }
84
85 int
86 dwlpx_bus_maxdevs(cpv, busno)
87 void *cpv;
88 int busno;
89 {
90 return DWLPX_MAXDEV;
91 }
92
93 pcitag_t
94 dwlpx_make_tag(cpv, b, d, f)
95 void *cpv;
96 int b, d, f;
97 {
98 pcitag_t tag;
99 int hpcdev, pci_idsel;
100
101 pci_idsel = (1 << ((d & 0x3) + 2));
102 hpcdev = d >> 2;
103 tag = (b << 24) | (hpcdev << 22) | (pci_idsel << 16) | (f << 13);
104 return (tag);
105 }
106
107 void
108 dwlpx_decompose_tag(cpv, tag, bp, dp, fp)
109 void *cpv;
110 pcitag_t tag;
111 int *bp, *dp, *fp;
112 {
113
114 if (bp != NULL)
115 *bp = (tag >> 24) & 0xff;
116 if (dp != NULL) {
117 int j, i = (tag >> 18) & 0xf;
118 j = -1;
119 while (i != 0) {
120 j++;
121 i >>= 1;
122 }
123 j += (((tag >> 22) & 3) << 2);
124 *dp = j;
125 }
126 if (fp != NULL)
127 *fp = (tag >> 13) & 0x7;
128 }
129
130 pcireg_t
131 dwlpx_conf_read(cpv, tag, offset)
132 void *cpv;
133 pcitag_t tag;
134 int offset;
135 {
136 struct dwlpx_config *ccp = cpv;
137 struct dwlpx_softc *sc;
138 pcireg_t *dp, data = (pcireg_t) -1;
139 unsigned long paddr;
140 int secondary, i, s = 0;
141 u_int32_t rvp;
142
143 if (ccp == NULL) {
144 panic("NULL ccp in dwlpx_conf_read\n");
145 }
146 sc = ccp->cc_sc;
147 secondary = tag >> 24;
148 if (secondary) {
149 #ifdef DO_SECONDARIES
150 tag &= 0x1fffff;
151 tag |= (secondary << 21);
152
153 #if 0
154 printf("read secondary %d reg %x (tag %x)",
155 secondary, offset, tag);
156 #endif
157
158 alpha_pal_draina();
159 s = splhigh();
160 /*
161 * Set up HPCs for type 1 cycles.
162 */
163 for (i = 0; i < sc->dwlpx_nhpc; i++) {
164 rvp = REGVAL(PCIA_CTL(i) + ccp->cc_sysbase) | 1;
165 alpha_mb();
166 REGVAL(PCIA_CTL(i) + ccp->cc_sysbase) = rvp;
167 alpha_mb();
168 }
169 #else
170 return (data);
171 #endif
172 }
173 paddr = (unsigned long) tag;
174 paddr |= DWLPX_PCI_CONF;
175 paddr |= ((unsigned long) ((offset >> 2) << 7));
176 paddr |= (((unsigned long) sc->dwlpx_hosenum) << 34);
177 paddr |= (((u_long) sc->dwlpx_node - 4) << 36);
178 paddr |= (1LL << 39);
179 paddr |= (1LL << 3); /* 32 Bit PCI byte enables */
180
181 dp = (pcireg_t *)KV(paddr);
182 if (badaddr(dp, sizeof (*dp)) == 0) {
183 data = *dp;
184 }
185 if (secondary) {
186 alpha_pal_draina();
187 for (i = 0; i < sc->dwlpx_nhpc; i++) {
188 rvp = REGVAL(PCIA_CTL(i) + ccp->cc_sysbase) & ~1;
189 alpha_mb();
190 REGVAL(PCIA_CTL(i) + ccp->cc_sysbase) = rvp;
191 alpha_mb();
192 }
193 (void) splx(s);
194 #ifdef DO_SECONDARIES
195 #if 0
196 printf("=%x\n", data);
197 #endif
198 #endif
199 }
200 return (data);
201 }
202
203 void
204 dwlpx_conf_write(cpv, tag, offset, data)
205 void *cpv;
206 pcitag_t tag;
207 int offset;
208 pcireg_t data;
209 {
210 struct dwlpx_config *ccp = cpv;
211 struct dwlpx_softc *sc;
212 pcireg_t *dp;
213 unsigned long paddr;
214 int secondary, i, s = 0;
215 u_int32_t rvp;
216
217 if (ccp == NULL) {
218 panic("NULL ccp in dwlpx_conf_write\n");
219 }
220 sc = ccp->cc_sc;
221 secondary = tag >> 24;
222 if (secondary) {
223 tag &= 0x1fffff;
224 tag |= (secondary << 21);
225 #if 0
226 printf("write secondary %d reg %x (tag %x) with %x\n",
227 secondary, offset, tag, data);
228 #endif
229
230 alpha_pal_draina();
231 s = splhigh();
232 /*
233 * Set up HPCs for type 1 cycles.
234 */
235 for (i = 0; i < sc->dwlpx_nhpc; i++) {
236 rvp = REGVAL(PCIA_CTL(i) + ccp->cc_sysbase) | 1;
237 alpha_mb();
238 REGVAL(PCIA_CTL(i) + ccp->cc_sysbase) = rvp;
239 alpha_mb();
240 }
241 }
242 paddr = (unsigned long) tag;
243 paddr |= DWLPX_PCI_CONF;
244 paddr |= ((unsigned long) ((offset >> 2) << 7));
245 paddr |= (((unsigned long) sc->dwlpx_hosenum) << 34);
246 paddr |= (((u_long) sc->dwlpx_node - 4) << 36);
247 paddr |= (1LL << 39);
248 paddr |= (1LL << 3); /* 32 bit PCI byte enables */
249
250 dp = (pcireg_t *)KV(paddr);
251 *dp = data;
252 alpha_mb();
253 if (secondary) {
254 alpha_pal_draina();
255 for (i = 0; i < sc->dwlpx_nhpc; i++) {
256 rvp = REGVAL(PCIA_CTL(i) + ccp->cc_sysbase) & ~1;
257 alpha_mb();
258 REGVAL(PCIA_CTL(i) + ccp->cc_sysbase) = rvp;
259 alpha_mb();
260 }
261 (void) splx(s);
262 }
263 }
264