universe_pci.c revision 1.2.6.2 1 1.2.6.2 bouyer /* $NetBSD: universe_pci.c,v 1.2.6.2 2000/11/20 11:42:38 bouyer Exp $ */
2 1.2.6.2 bouyer
3 1.2.6.2 bouyer /*
4 1.2.6.2 bouyer * Copyright (c) 1999
5 1.2.6.2 bouyer * Matthias Drochner. All rights reserved.
6 1.2.6.2 bouyer *
7 1.2.6.2 bouyer * Redistribution and use in source and binary forms, with or without
8 1.2.6.2 bouyer * modification, are permitted provided that the following conditions
9 1.2.6.2 bouyer * are met:
10 1.2.6.2 bouyer * 1. Redistributions of source code must retain the above copyright
11 1.2.6.2 bouyer * notice, this list of conditions, and the following disclaimer.
12 1.2.6.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.6.2 bouyer * notice, this list of conditions and the following disclaimer in the
14 1.2.6.2 bouyer * documentation and/or other materials provided with the distribution.
15 1.2.6.2 bouyer *
16 1.2.6.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.2.6.2 bouyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.2.6.2 bouyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.2.6.2 bouyer * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.2.6.2 bouyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.2.6.2 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.2.6.2 bouyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.2.6.2 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.2.6.2 bouyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.2.6.2 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.2.6.2 bouyer * SUCH DAMAGE.
27 1.2.6.2 bouyer */
28 1.2.6.2 bouyer
29 1.2.6.2 bouyer /*
30 1.2.6.2 bouyer * Common functions for PCI-VME-interfaces using the
31 1.2.6.2 bouyer * Newbridge/Tundra Universe II chip (CA91C142).
32 1.2.6.2 bouyer */
33 1.2.6.2 bouyer
34 1.2.6.2 bouyer #include <sys/param.h>
35 1.2.6.2 bouyer #include <sys/systm.h>
36 1.2.6.2 bouyer #include <sys/device.h>
37 1.2.6.2 bouyer
38 1.2.6.2 bouyer #include <dev/pci/pcireg.h>
39 1.2.6.2 bouyer #include <dev/pci/pcivar.h>
40 1.2.6.2 bouyer /*#include <dev/pci/pcidevs.h>*/
41 1.2.6.2 bouyer
42 1.2.6.2 bouyer #include <machine/bus.h>
43 1.2.6.2 bouyer
44 1.2.6.2 bouyer #include <dev/vme/vmereg.h>
45 1.2.6.2 bouyer #include <dev/vme/vmevar.h>
46 1.2.6.2 bouyer
47 1.2.6.2 bouyer #include <dev/ic/universereg.h>
48 1.2.6.2 bouyer #include <dev/pci/universe_pci_var.h>
49 1.2.6.2 bouyer
50 1.2.6.2 bouyer int univ_pci_intr __P((void *));
51 1.2.6.2 bouyer
52 1.2.6.2 bouyer #define read_csr_4(d, reg) \
53 1.2.6.2 bouyer bus_space_read_4(d->csrt, d->csrh, offsetof(struct universereg, reg))
54 1.2.6.2 bouyer #define write_csr_4(d, reg, val) \
55 1.2.6.2 bouyer bus_space_write_4(d->csrt, d->csrh, offsetof(struct universereg, reg), val)
56 1.2.6.2 bouyer
57 1.2.6.2 bouyer #define _pso(i) offsetof(struct universereg, __CONCAT(pcislv, i))
58 1.2.6.2 bouyer static int pcislvoffsets[8] = {
59 1.2.6.2 bouyer _pso(0), _pso(1), _pso(2), _pso(3),
60 1.2.6.2 bouyer _pso(4), _pso(5), _pso(6), _pso(7)
61 1.2.6.2 bouyer };
62 1.2.6.2 bouyer #undef _pso
63 1.2.6.2 bouyer
64 1.2.6.2 bouyer #define read_pcislv(d, idx, reg) \
65 1.2.6.2 bouyer bus_space_read_4(d->csrt, d->csrh, \
66 1.2.6.2 bouyer pcislvoffsets[idx] + offsetof(struct universe_pcislvimg, reg))
67 1.2.6.2 bouyer #define write_pcislv(d, idx, reg, val) \
68 1.2.6.2 bouyer bus_space_write_4(d->csrt, d->csrh, \
69 1.2.6.2 bouyer pcislvoffsets[idx] + offsetof(struct universe_pcislvimg, reg), val)
70 1.2.6.2 bouyer
71 1.2.6.2 bouyer
72 1.2.6.2 bouyer #define _vso(i) offsetof(struct universereg, __CONCAT(vmeslv, i))
73 1.2.6.2 bouyer static int vmeslvoffsets[8] = {
74 1.2.6.2 bouyer _vso(0), _vso(1), _vso(2), _vso(3),
75 1.2.6.2 bouyer _vso(4), _vso(5), _vso(6), _vso(7)
76 1.2.6.2 bouyer };
77 1.2.6.2 bouyer #undef _vso
78 1.2.6.2 bouyer
79 1.2.6.2 bouyer #define read_vmeslv(d, idx, reg) \
80 1.2.6.2 bouyer bus_space_read_4(d->csrt, d->csrh, \
81 1.2.6.2 bouyer vmeslvoffsets[idx] + offsetof(struct universe_vmeslvimg, reg))
82 1.2.6.2 bouyer #define write_vmeslv(d, idx, reg, val) \
83 1.2.6.2 bouyer bus_space_write_4(d->csrt, d->csrh, \
84 1.2.6.2 bouyer vmeslvoffsets[idx] + offsetof(struct universe_vmeslvimg, reg), val)
85 1.2.6.2 bouyer
86 1.2.6.2 bouyer int
87 1.2.6.2 bouyer univ_pci_attach(d, pa, name, inthdl, intcookie)
88 1.2.6.2 bouyer struct univ_pci_data *d;
89 1.2.6.2 bouyer struct pci_attach_args *pa;
90 1.2.6.2 bouyer const char *name;
91 1.2.6.2 bouyer void (*inthdl) __P((void *, int, int));
92 1.2.6.2 bouyer void *intcookie;
93 1.2.6.2 bouyer {
94 1.2.6.2 bouyer pci_chipset_tag_t pc = pa->pa_pc;
95 1.2.6.2 bouyer pci_intr_handle_t ih;
96 1.2.6.2 bouyer const char *intrstr = NULL;
97 1.2.6.2 bouyer u_int32_t reg;
98 1.2.6.2 bouyer int i;
99 1.2.6.2 bouyer
100 1.2.6.2 bouyer d->pc = pc;
101 1.2.6.2 bouyer strncpy(d->devname, name, sizeof(d->devname));
102 1.2.6.2 bouyer d->devname[sizeof(d->devname) - 1] = '\0';
103 1.2.6.2 bouyer
104 1.2.6.2 bouyer if (pci_mapreg_map(pa, 0x10,
105 1.2.6.2 bouyer PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
106 1.2.6.2 bouyer 0, &d->csrt, &d->csrh, NULL, NULL) &&
107 1.2.6.2 bouyer pci_mapreg_map(pa, 0x14,
108 1.2.6.2 bouyer PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
109 1.2.6.2 bouyer 0, &d->csrt, &d->csrh, NULL, NULL) &&
110 1.2.6.2 bouyer pci_mapreg_map(pa, 0x10,
111 1.2.6.2 bouyer PCI_MAPREG_TYPE_IO,
112 1.2.6.2 bouyer 0, &d->csrt, &d->csrh, NULL, NULL) &&
113 1.2.6.2 bouyer pci_mapreg_map(pa, 0x14,
114 1.2.6.2 bouyer PCI_MAPREG_TYPE_IO,
115 1.2.6.2 bouyer 0, &d->csrt, &d->csrh, NULL, NULL))
116 1.2.6.2 bouyer return (-1);
117 1.2.6.2 bouyer
118 1.2.6.2 bouyer /* name sure the chip is in a sane state */
119 1.2.6.2 bouyer write_csr_4(d, lint_en, 0); /* mask all PCI interrupts */
120 1.2.6.2 bouyer write_csr_4(d, vint_en, 0); /* mask all VME interrupts */
121 1.2.6.2 bouyer write_csr_4(d, dgcs, 0x40000000); /* stop DMA activity */
122 1.2.6.2 bouyer for (i = 0; i < 8; i++) {
123 1.2.6.2 bouyer univ_pci_unmapvme(d, i);
124 1.2.6.2 bouyer univ_pci_unmappci(d, i);
125 1.2.6.2 bouyer }
126 1.2.6.2 bouyer write_csr_4(d, slsi, 0); /* disable "special PCI slave image" */
127 1.2.6.2 bouyer
128 1.2.6.2 bouyer /* enable DMA */
129 1.2.6.2 bouyer pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
130 1.2.6.2 bouyer pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
131 1.2.6.2 bouyer PCI_COMMAND_MASTER_ENABLE);
132 1.2.6.2 bouyer
133 1.2.6.2 bouyer reg = read_csr_4(d, misc_ctl);
134 1.2.6.2 bouyer printf("%s: ", name);
135 1.2.6.2 bouyer if (reg & 0x00020000) /* SYSCON */
136 1.2.6.2 bouyer printf("VME bus controller, ");
137 1.2.6.2 bouyer reg = read_csr_4(d, mast_ctl);
138 1.2.6.2 bouyer printf("requesting at VME bus level %d\n", (reg >> 22) & 3);
139 1.2.6.2 bouyer
140 1.2.6.2 bouyer /* Map and establish the PCI interrupt. */
141 1.2.6.2 bouyer if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
142 1.2.6.2 bouyer pa->pa_intrline, &ih)) {
143 1.2.6.2 bouyer printf("%s: couldn't map interrupt\n", name);
144 1.2.6.2 bouyer return (-1);
145 1.2.6.2 bouyer }
146 1.2.6.2 bouyer intrstr = pci_intr_string(pc, ih);
147 1.2.6.2 bouyer /*
148 1.2.6.2 bouyer * Use a low interrupt level (the lowest?).
149 1.2.6.2 bouyer * We will raise before calling a subdevice's handler.
150 1.2.6.2 bouyer */
151 1.2.6.2 bouyer d->ih = pci_intr_establish(pc, ih, IPL_BIO, univ_pci_intr, d);
152 1.2.6.2 bouyer if (d->ih == NULL) {
153 1.2.6.2 bouyer printf("%s: couldn't establish interrupt", name);
154 1.2.6.2 bouyer if (intrstr != NULL)
155 1.2.6.2 bouyer printf(" at %s", intrstr);
156 1.2.6.2 bouyer printf("\n");
157 1.2.6.2 bouyer return (-1);
158 1.2.6.2 bouyer }
159 1.2.6.2 bouyer printf("%s: interrupting at %s\n", name, intrstr);
160 1.2.6.2 bouyer
161 1.2.6.2 bouyer /* handle all VME interrupts (XXX should be configurable) */
162 1.2.6.2 bouyer d->vmeinthandler = inthdl;
163 1.2.6.2 bouyer d->vmeintcookie = intcookie;
164 1.2.6.2 bouyer write_csr_4(d, lint_stat, 0x00ff37ff); /* ack all pending IRQs */
165 1.2.6.2 bouyer write_csr_4(d, lint_en, 0x000000fe); /* enable VME IRQ 1..7 */
166 1.2.6.2 bouyer
167 1.2.6.2 bouyer return (0);
168 1.2.6.2 bouyer }
169 1.2.6.2 bouyer
170 1.2.6.2 bouyer int
171 1.2.6.2 bouyer univ_pci_mapvme(d, wnd, vmebase, len, am, datawidth, pcibase)
172 1.2.6.2 bouyer struct univ_pci_data *d;
173 1.2.6.2 bouyer int wnd;
174 1.2.6.2 bouyer vme_addr_t vmebase;
175 1.2.6.2 bouyer u_int32_t len;
176 1.2.6.2 bouyer vme_am_t am;
177 1.2.6.2 bouyer vme_datasize_t datawidth;
178 1.2.6.2 bouyer u_int32_t pcibase;
179 1.2.6.2 bouyer {
180 1.2.6.2 bouyer u_int32_t ctl = 0x80000000;
181 1.2.6.2 bouyer
182 1.2.6.2 bouyer switch (am & VME_AM_ADRSIZEMASK) {
183 1.2.6.2 bouyer case VME_AM_A32:
184 1.2.6.2 bouyer ctl |= 0x00020000;
185 1.2.6.2 bouyer break;
186 1.2.6.2 bouyer case VME_AM_A24:
187 1.2.6.2 bouyer ctl |= 0x00010000;
188 1.2.6.2 bouyer break;
189 1.2.6.2 bouyer case VME_AM_A16:
190 1.2.6.2 bouyer break;
191 1.2.6.2 bouyer default:
192 1.2.6.2 bouyer return (EINVAL);
193 1.2.6.2 bouyer }
194 1.2.6.2 bouyer if (am & VME_AM_SUPER)
195 1.2.6.2 bouyer ctl |= 0x00001000;
196 1.2.6.2 bouyer if ((am & VME_AM_MODEMASK) == VME_AM_PRG)
197 1.2.6.2 bouyer ctl |= 0x00004000;
198 1.2.6.2 bouyer if (datawidth & VME_D32)
199 1.2.6.2 bouyer ctl |= 0x00800000;
200 1.2.6.2 bouyer else if (datawidth & VME_D16)
201 1.2.6.2 bouyer ctl |= 0x00400000;
202 1.2.6.2 bouyer else if (!(datawidth & VME_D8))
203 1.2.6.2 bouyer return (EINVAL);
204 1.2.6.2 bouyer
205 1.2.6.2 bouyer #ifdef UNIV_DEBUG
206 1.2.6.2 bouyer printf("%s: wnd %d, map VME %x-%x to %x, ctl=%x\n",
207 1.2.6.2 bouyer d->devname, wnd, vmebase, vmebase + len, pcibase, ctl);
208 1.2.6.2 bouyer #endif
209 1.2.6.2 bouyer
210 1.2.6.2 bouyer write_pcislv(d, wnd, lsi_bs, pcibase);
211 1.2.6.2 bouyer write_pcislv(d, wnd, lsi_bd, pcibase + len);
212 1.2.6.2 bouyer write_pcislv(d, wnd, lsi_to, vmebase - pcibase);
213 1.2.6.2 bouyer write_pcislv(d, wnd, lsi_ctl, ctl);
214 1.2.6.2 bouyer return (0);
215 1.2.6.2 bouyer }
216 1.2.6.2 bouyer
217 1.2.6.2 bouyer void
218 1.2.6.2 bouyer univ_pci_unmapvme(d, wnd)
219 1.2.6.2 bouyer struct univ_pci_data *d;
220 1.2.6.2 bouyer int wnd;
221 1.2.6.2 bouyer {
222 1.2.6.2 bouyer #ifdef UNIV_DEBUG
223 1.2.6.2 bouyer printf("%s: unmap VME wnd %d\n", d->devname, wnd);
224 1.2.6.2 bouyer #endif
225 1.2.6.2 bouyer write_pcislv(d, wnd, lsi_ctl, 0);
226 1.2.6.2 bouyer }
227 1.2.6.2 bouyer
228 1.2.6.2 bouyer
229 1.2.6.2 bouyer int
230 1.2.6.2 bouyer univ_pci_mappci(d, wnd, pcibase, len, vmebase, am)
231 1.2.6.2 bouyer struct univ_pci_data *d;
232 1.2.6.2 bouyer int wnd;
233 1.2.6.2 bouyer u_int32_t pcibase;
234 1.2.6.2 bouyer u_int32_t len;
235 1.2.6.2 bouyer vme_addr_t vmebase;
236 1.2.6.2 bouyer vme_am_t am;
237 1.2.6.2 bouyer {
238 1.2.6.2 bouyer u_int32_t ctl = 0x80000000;
239 1.2.6.2 bouyer
240 1.2.6.2 bouyer switch (am & VME_AM_ADRSIZEMASK) {
241 1.2.6.2 bouyer case VME_AM_A32:
242 1.2.6.2 bouyer ctl |= 0x00020000;
243 1.2.6.2 bouyer break;
244 1.2.6.2 bouyer case VME_AM_A24:
245 1.2.6.2 bouyer ctl |= 0x00010000;
246 1.2.6.2 bouyer break;
247 1.2.6.2 bouyer case VME_AM_A16:
248 1.2.6.2 bouyer break;
249 1.2.6.2 bouyer default:
250 1.2.6.2 bouyer return (EINVAL);
251 1.2.6.2 bouyer }
252 1.2.6.2 bouyer if (am & VME_AM_SUPER)
253 1.2.6.2 bouyer ctl |= 0x00200000;
254 1.2.6.2 bouyer else
255 1.2.6.2 bouyer ctl |= 0x00300000; /* both */
256 1.2.6.2 bouyer if ((am & VME_AM_MODEMASK) == VME_AM_PRG)
257 1.2.6.2 bouyer ctl |= 0x00800000;
258 1.2.6.2 bouyer else
259 1.2.6.2 bouyer ctl |= 0x00c00000; /* both */
260 1.2.6.2 bouyer
261 1.2.6.2 bouyer #ifdef UNIV_DEBUG
262 1.2.6.2 bouyer printf("%s: wnd %d, map PCI %x-%x to %x, ctl=%x\n",
263 1.2.6.2 bouyer d->devname, wnd, pcibase, pcibase + len, vmebase, ctl);
264 1.2.6.2 bouyer #endif
265 1.2.6.2 bouyer
266 1.2.6.2 bouyer write_vmeslv(d, wnd, vsi_bs, vmebase);
267 1.2.6.2 bouyer write_vmeslv(d, wnd, vsi_bd, vmebase + len);
268 1.2.6.2 bouyer write_vmeslv(d, wnd, vsi_to, pcibase - vmebase);
269 1.2.6.2 bouyer write_vmeslv(d, wnd, vsi_ctl, ctl);
270 1.2.6.2 bouyer return (0);
271 1.2.6.2 bouyer }
272 1.2.6.2 bouyer
273 1.2.6.2 bouyer void
274 1.2.6.2 bouyer univ_pci_unmappci(d, wnd)
275 1.2.6.2 bouyer struct univ_pci_data *d;
276 1.2.6.2 bouyer int wnd;
277 1.2.6.2 bouyer {
278 1.2.6.2 bouyer #ifdef UNIV_DEBUG
279 1.2.6.2 bouyer printf("%s: unmap PCI wnd %d\n", d->devname, wnd);
280 1.2.6.2 bouyer #endif
281 1.2.6.2 bouyer write_vmeslv(d, wnd, vsi_ctl, 0);
282 1.2.6.2 bouyer }
283 1.2.6.2 bouyer
284 1.2.6.2 bouyer int
285 1.2.6.2 bouyer univ_pci_vmebuserr(d, clear)
286 1.2.6.2 bouyer struct univ_pci_data *d;
287 1.2.6.2 bouyer int clear;
288 1.2.6.2 bouyer {
289 1.2.6.2 bouyer u_int32_t pcicsr;
290 1.2.6.2 bouyer
291 1.2.6.2 bouyer pcicsr = read_csr_4(d, pci_csr);
292 1.2.6.2 bouyer if ((pcicsr & 0xf8000000) && clear)
293 1.2.6.2 bouyer write_csr_4(d, pci_csr, pcicsr | 0xf8000000);
294 1.2.6.2 bouyer return (pcicsr & 0x08000000); /* target abort */
295 1.2.6.2 bouyer }
296 1.2.6.2 bouyer
297 1.2.6.2 bouyer int
298 1.2.6.2 bouyer univ_pci_intr(v)
299 1.2.6.2 bouyer void *v;
300 1.2.6.2 bouyer {
301 1.2.6.2 bouyer struct univ_pci_data *d = v;
302 1.2.6.2 bouyer u_int32_t intcsr;
303 1.2.6.2 bouyer int i, vec;
304 1.2.6.2 bouyer
305 1.2.6.2 bouyer intcsr = read_csr_4(d, lint_stat) & 0xffffff;
306 1.2.6.2 bouyer if (!intcsr)
307 1.2.6.2 bouyer return (0);
308 1.2.6.2 bouyer
309 1.2.6.2 bouyer /* ack everything */
310 1.2.6.2 bouyer write_csr_4(d, lint_stat, intcsr);
311 1.2.6.2 bouyer #ifdef UNIV_DEBUG
312 1.2.6.2 bouyer printf("%s: intr, lint_stat=%x\n", d->devname, intcsr);
313 1.2.6.2 bouyer #endif
314 1.2.6.2 bouyer if (intcsr & 0x000000fe) { /* VME interrupt */
315 1.2.6.2 bouyer for (i = 7; i >= 1; i--) {
316 1.2.6.2 bouyer if (!(intcsr & (1 << i)))
317 1.2.6.2 bouyer continue;
318 1.2.6.2 bouyer vec = read_csr_4(d, v_statid[i - 1]);
319 1.2.6.2 bouyer if (vec & 0x100) {
320 1.2.6.2 bouyer printf("%s: err irq %d\n", d->devname, i);
321 1.2.6.2 bouyer continue;
322 1.2.6.2 bouyer }
323 1.2.6.2 bouyer if (d->vmeinthandler)
324 1.2.6.2 bouyer (*d->vmeinthandler)(d->vmeintcookie, i, vec);
325 1.2.6.2 bouyer }
326 1.2.6.2 bouyer }
327 1.2.6.2 bouyer
328 1.2.6.2 bouyer return (1);
329 1.2.6.2 bouyer }
330