vme_pcc.c revision 1.7 1 /* $NetBSD: vme_pcc.c,v 1.7 2000/03/18 22:33:04 scw Exp $ */
2
3 /*-
4 * Copyright (c) 1996-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 and Steve C. Woodford.
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 * VME support specific to the Type 1 VMEchip found on the
41 * MVME-147.
42 *
43 * For a manual on the MVME-147, call: 408.991.8634. (Yes, this
44 * is the Sunnyvale sales office.)
45 */
46
47 #include <sys/param.h>
48 #include <sys/kernel.h>
49 #include <sys/systm.h>
50 #include <sys/device.h>
51 #include <sys/malloc.h>
52 #include <sys/kcore.h>
53
54 #include <machine/cpu.h>
55 #include <machine/bus.h>
56
57 #include <dev/vme/vmereg.h>
58 #include <dev/vme/vmevar.h>
59
60 #include <mvme68k/mvme68k/isr.h>
61
62 #include <mvme68k/dev/pccreg.h>
63 #include <mvme68k/dev/pccvar.h>
64 #include <mvme68k/dev/vme_pccreg.h>
65 #include <mvme68k/dev/vme_pccvar.h>
66
67
68 int vme_pcc_match __P((struct device *, struct cfdata *, void *));
69 void vme_pcc_attach __P((struct device *, struct device *, void *));
70
71 struct cfattach vmepcc_ca = {
72 sizeof(struct vme_pcc_softc), vme_pcc_match, vme_pcc_attach
73 };
74
75 extern struct cfdriver vmepcc_cd;
76
77 extern phys_ram_seg_t mem_clusters[];
78 static int vme_pcc_attached;
79
80 #ifdef DIAGNOSTIC
81 const char *_vme1_mod_string __P((vme_addr_t, vme_size_t,
82 vme_am_t, vme_datasize_t));
83 #endif
84
85 /*
86 * Describe the VMEbus ranges available from the MVME147
87 */
88 struct vme_pcc_range {
89 vme_am_t pr_am; /* Address Modifier for this range */
90 vme_datasize_t pr_datasize; /* Usable Data Sizes (D8, D16, * D32) */
91 vme_addr_t pr_locstart; /* Local-bus start address of range */
92 vme_addr_t pr_start; /* VMEbus start address of range */
93 vme_addr_t pr_end; /* VMEbus end address of range */
94 };
95
96 static struct vme_pcc_range vme_pcc_ranges[] = {
97 {VME_AM_MBO | VME_AM_A24 | VME_AM_DATA | VME_AM_SUPER,
98 VME_D32 | VME_D16 | VME_D8,
99 VME1_A24D32_LOC_START,
100 VME1_A24D32_START,
101 VME1_A24D32_END},
102
103 {VME_AM_MBO | VME_AM_A32 | VME_AM_DATA | VME_AM_SUPER,
104 VME_D32 | VME_D16 | VME_D8,
105 VME1_A32D32_LOC_START,
106 VME1_A32D32_START,
107 VME1_A32D32_END},
108
109 {VME_AM_MBO | VME_AM_A24 | VME_AM_DATA | VME_AM_SUPER,
110 VME_D16 | VME_D8,
111 VME1_A24D16_LOC_START,
112 VME1_A24D16_START,
113 VME1_A24D16_END},
114
115 {VME_AM_MBO | VME_AM_A32 | VME_AM_DATA | VME_AM_SUPER,
116 VME_D16 | VME_D8,
117 VME1_A32D16_LOC_START,
118 VME1_A32D16_START,
119 VME1_A32D16_END},
120
121 {VME_AM_MBO | VME_AM_A16 | VME_AM_DATA | VME_AM_SUPER,
122 VME_D16 | VME_D8,
123 VME1_A16D16_LOC_START,
124 VME1_A16D16_START,
125 VME1_A16D16_END}
126 };
127 #define VME1_NRANGES (sizeof(vme_pcc_ranges)/sizeof(struct vme_pcc_range))
128
129
130 /* ARGSUSED */
131 int
132 vme_pcc_match(parent, cf, aux)
133 struct device *parent;
134 struct cfdata *cf;
135 void *aux;
136 {
137 struct pcc_attach_args *pa;
138
139 pa = aux;
140
141 /* Only one VME chip, please. */
142 if (vme_pcc_attached)
143 return (0);
144
145 if (strcmp(pa->pa_name, vmepcc_cd.cd_name))
146 return (0);
147
148 return (1);
149 }
150
151 void
152 vme_pcc_attach(parent, self, aux)
153 struct device *parent;
154 struct device *self;
155 void *aux;
156 {
157 struct pcc_attach_args *pa;
158 struct vme_pcc_softc *sc;
159 struct vmebus_attach_args vaa;
160 u_int8_t reg;
161 int i;
162
163 sc = (struct vme_pcc_softc *) self;
164 pa = aux;
165
166 sc->sc_dmat = pa->pa_dmat;
167 sc->sc_bust = pa->pa_bust;
168 sc->sc_vmet = MVME68K_VME_BUS_SPACE;
169
170 bus_space_map(sc->sc_bust, pa->pa_offset, VME1REG_SIZE, 0,
171 &sc->sc_bush);
172
173 /* Initialize the chip. */
174 reg = vme1_reg_read(sc, VME1REG_SCON) & ~VME1_SCON_SYSFAIL;
175 vme1_reg_write(sc, VME1REG_SCON, reg);
176
177 printf(": Type 1 VMEchip, scon jumper %s\n",
178 (reg & VME1_SCON_SWITCH) ? "enabled" : "disabled");
179
180 sc->sc_vct.cookie = self;
181 sc->sc_vct.vct_probe = _vme_pcc_probe;
182 sc->sc_vct.vct_map = _vme_pcc_map;
183 sc->sc_vct.vct_unmap = _vme_pcc_unmap;
184 sc->sc_vct.vct_int_map = _vme_pcc_intmap;
185 sc->sc_vct.vct_int_establish = _vme_pcc_intr_establish;
186 sc->sc_vct.vct_int_disestablish = _vme_pcc_intr_disestablish;
187 sc->sc_vct.vct_dmamap_create = _vme_pcc_dmamap_create;
188 sc->sc_vct.vct_dmamap_destroy = _vme_pcc_dmamap_destroy;
189 sc->sc_vct.vct_dmamem_alloc = _vme_pcc_dmamem_alloc;
190 sc->sc_vct.vct_dmamem_free = _vme_pcc_dmamem_free;
191
192 /*
193 * Adjust the start address of the first range in vme_pcc_ranges[]
194 * according to how much onboard memory exists. Disable the first
195 * range if onboard memory >= 16Mb, and adjust the start of the
196 * second range (A32D32).
197 */
198 vme_pcc_ranges[0].pr_start = (vme_addr_t) mem_clusters[0].size;
199 if (mem_clusters[0].size >= 0x01000000) {
200 vme_pcc_ranges[0].pr_am = (vme_am_t) - 1;
201 vme_pcc_ranges[1].pr_start +=
202 (vme_addr_t) (mem_clusters[0].size - 0x01000000);
203 }
204
205 #ifdef DIAGNOSTIC
206 for (i = 0; i < VME1_NRANGES; i++) {
207 vme_addr_t mask;
208
209 switch (vme_pcc_ranges[i].pr_am & VME_AM_ADRSIZEMASK) {
210 case VME_AM_A32:
211 mask = 0xffffffffu;
212 break;
213
214 case VME_AM_A24:
215 mask = 0x00ffffffu;
216 break;
217
218 case VME_AM_A16:
219 mask = 0x0000ffffu;
220 break;
221
222 default:
223 printf("%s: Map#%d: disabled\n",
224 sc->sc_dev.dv_xname, i);
225 continue;
226 }
227
228 printf("%s: Map#%d: 0x%08x -> %s\n", sc->sc_dev.dv_xname, i,
229 vme_pcc_ranges[i].pr_locstart + vme_pcc_ranges[i].pr_start,
230 _vme1_mod_string(vme_pcc_ranges[i].pr_start & mask,
231 (vme_pcc_ranges[i].pr_end -
232 vme_pcc_ranges[i].pr_start) + 1,
233 vme_pcc_ranges[i].pr_am,
234 vme_pcc_ranges[i].pr_datasize));
235 }
236 #endif
237
238 vaa.va_vct = &(sc->sc_vct);
239 vaa.va_bdt = sc->sc_dmat;
240 vaa.va_slaveconfig = NULL;
241
242 vme_pcc_attached = 1;
243
244 /* Attach the MI VMEbus glue. */
245 config_found(self, &vaa, 0);
246 }
247
248 /* ARGSUSED */
249 int
250 _vme_pcc_map(vsc, vmeaddr, len, am, datasize, swap, tag, handle, resc)
251 void *vsc;
252 vme_addr_t vmeaddr;
253 vme_size_t len;
254 vme_am_t am;
255 vme_datasize_t datasize;
256 vme_swap_t swap;
257 bus_space_tag_t *tag;
258 bus_space_handle_t *handle;
259 vme_mapresc_t *resc;
260 {
261 struct vme_pcc_softc *sc;
262 struct vme_pcc_mapresc_t *pm;
263 struct vme_pcc_range *pr;
264 vme_addr_t end;
265 paddr_t paddr;
266 int rv;
267 int i;
268
269 sc = vsc;
270 end = (vmeaddr + len) - 1;
271 paddr = 0;
272
273 for (i = 0, pr = &vme_pcc_ranges[0]; i < VME1_NRANGES; i++, pr++) {
274 /* Ignore if range is disabled */
275 if (pr->pr_am == (vme_am_t) - 1)
276 continue;
277
278 /*
279 * Accept the range if it matches the constraints
280 */
281 if (am == pr->pr_am &&
282 datasize <= pr->pr_datasize &&
283 vmeaddr >= pr->pr_start && end <= pr->pr_end) {
284 /*
285 * We have a match.
286 */
287 paddr = pr->pr_locstart + vmeaddr;
288 break;
289 }
290 }
291
292 if (paddr == 0) {
293 #ifdef DIAGNOSTIC
294 printf("%s: Unable to map %s\n", sc->sc_dev.dv_xname,
295 _vme1_mod_string(vmeaddr, len, am, datasize));
296 #endif
297 return (ENOMEM);
298 }
299 if ((rv = bus_space_map(sc->sc_vmet, paddr, len, 0, handle)) != 0)
300 return (rv);
301
302 if ((pm = malloc(sizeof(*pm), M_DEVBUF, M_NOWAIT)) == NULL) {
303 bus_space_unmap(sc->sc_vmet, *handle, len);
304 return (ENOMEM);
305 }
306 *tag = sc->sc_vmet;
307 pm->pm_am = am;
308 pm->pm_datasize = datasize;
309 pm->pm_addr = vmeaddr;
310 pm->pm_size = len;
311 pm->pm_handle = *handle;
312 *resc = (vme_mapresc_t *) pm;
313
314 return (0);
315 }
316
317 void
318 _vme_pcc_unmap(vsc, resc)
319 void *vsc;
320 vme_mapresc_t resc;
321 {
322 struct vme_pcc_softc *sc;
323 struct vme_pcc_mapresc_t *pm;
324
325 sc = (struct vme_pcc_softc *) vsc;
326 pm = (struct vme_pcc_mapresc_t *) resc;
327
328 bus_space_unmap(sc->sc_vmet, pm->pm_handle, pm->pm_size);
329
330 free(pm, M_DEVBUF);
331 }
332
333 int
334 _vme_pcc_probe(vsc, vmeaddr, len, am, datasize, callback, arg)
335 void *vsc;
336 vme_addr_t vmeaddr;
337 vme_size_t len;
338 vme_am_t am;
339 vme_datasize_t datasize;
340 int (*callback) __P((void *, bus_space_tag_t, bus_space_handle_t));
341 void *arg;
342 {
343 bus_space_tag_t tag;
344 bus_space_handle_t handle;
345 vme_mapresc_t resc;
346 int rv;
347
348 rv = _vme_pcc_map(vsc, vmeaddr, len, am, datasize,
349 0, &tag, &handle, &resc);
350 if (rv)
351 return (rv);
352
353 if (callback)
354 rv = (*callback) (arg, tag, handle);
355 else {
356 /*
357 * FIXME: datasize is fixed by hardware, so using badaddr() in
358 * this way may cause several accesses to each VMEbus address.
359 * Also, using 'handle' in this way is a bit presumptuous...
360 */
361 rv = badaddr((caddr_t) handle, (int) len) ? EIO : 0;
362 }
363
364 _vme_pcc_unmap(vsc, resc);
365
366 return (rv);
367 }
368
369 /* ARGSUSED */
370 int
371 _vme_pcc_intmap(vsc, level, vector, handlep)
372 void *vsc;
373 int level, vector;
374 vme_intr_handle_t *handlep;
375 {
376
377 if (level < 1 || level > 7 || vector < 0x80 || vector > 0xff)
378 return (EINVAL);
379
380 /* This is rather gross */
381 *handlep = (void *) (int) ((level << 8) | vector);
382
383 return (0);
384 }
385
386 void *
387 _vme_pcc_intr_establish(vsc, handle, prior, func, arg)
388 void *vsc;
389 vme_intr_handle_t handle;
390 int prior;
391 int (*func) __P((void *));
392 void *arg;
393 {
394 struct vme_pcc_softc *sc;
395 int level, vector;
396
397 sc = vsc;
398 level = ((int) handle) >> 8;
399 vector = ((int) handle) & 0xff;
400
401 isrlink_vectored(func, arg, level, vector);
402 sc->sc_irqref[level]++;
403
404 /*
405 * There had better not be another VMEbus master responding
406 * to this interrupt level...
407 */
408 vme1_reg_write(sc, VME1REG_IRQEN,
409 vme1_reg_read(sc, VME1REG_IRQEN) | VME1_IRQ_VME(level));
410
411 return ((void *) handle);
412 }
413
414 void
415 _vme_pcc_intr_disestablish(vsc, handle)
416 void *vsc;
417 vme_intr_handle_t handle;
418 {
419 struct vme_pcc_softc *sc;
420 int level, vector;
421
422 sc = (struct vme_pcc_softc *) vsc;
423 level = ((int) handle) >> 8;
424 vector = ((int) handle) & 0xff;
425
426 isrunlink_vectored(vector);
427
428 /* Disable VME IRQ if possible. */
429 switch (sc->sc_irqref[level]) {
430 case 0:
431 printf("vme_pcc_intr_disestablish: nothing using IRQ %d\n",
432 level);
433 panic("vme_pcc_intr_disestablish");
434 /* NOTREACHED */
435
436 case 1:
437 vme1_reg_write(sc, VME1REG_IRQEN,
438 vme1_reg_read(sc, VME1REG_IRQEN) & ~VME1_IRQ_VME(level));
439 /* FALLTHROUGH */
440
441 default:
442 sc->sc_irqref[level]--;
443 }
444 }
445
446 /* ARGSUSED */
447 int
448 _vme_pcc_dmamap_create(vsc, len, am, datasize, swap, nsegs,
449 segsz, bound, flags, mapp)
450 void *vsc;
451 vme_size_t len;
452 vme_am_t am;
453 vme_datasize_t datasize;
454 vme_swap_t swap;
455 int nsegs;
456 vme_size_t segsz;
457 vme_addr_t bound;
458 int flags;
459 bus_dmamap_t *mapp;
460 {
461
462 return (EINVAL);
463 }
464
465 /* ARGSUSED */
466 void
467 _vme_pcc_dmamap_destroy(vsc, map)
468 void *vsc;
469 bus_dmamap_t map;
470 {
471 }
472
473 /* ARGSUSED */
474 int
475 _vme_pcc_dmamem_alloc(vsc, len, am, datasizes, swap,
476 segs, nsegs, rsegs, flags)
477 void *vsc;
478 vme_size_t len;
479 vme_am_t am;
480 vme_datasize_t datasizes;
481 vme_swap_t swap;
482 bus_dma_segment_t *segs;
483 int nsegs;
484 int *rsegs;
485 int flags;
486 {
487
488 return (EINVAL);
489 }
490
491 /* ARGSUSED */
492 void
493 _vme_pcc_dmamem_free(vsc, segs, nsegs)
494 void *vsc;
495 bus_dma_segment_t *segs;
496 int nsegs;
497 {
498 }
499
500 #ifdef DIAGNOSTIC
501 const char *
502 _vme1_mod_string(addr, len, am, ds)
503 vme_addr_t addr;
504 vme_size_t len;
505 vme_am_t am;
506 vme_datasize_t ds;
507 {
508 static const char *mode[] = {"BLT64)", "DATA)", "PROG)", "BLT32)"};
509 static const char *dsiz[] = {"(", "(D8,", "(D16,", "(D16-D8,",
510 "(D32,", "(D32,D8,", "(D32-D16,", "(D32-D8,"};
511 static char mstring[40];
512 char *fmt;
513
514 switch (am & VME_AM_ADRSIZEMASK) {
515 case VME_AM_A32:
516 fmt = "A32:%08x-%08x ";
517 break;
518
519 case VME_AM_A24:
520 fmt = "A24:%06x-%06x ";
521 break;
522
523 case VME_AM_A16:
524 fmt = "A16:%04x-%04x ";
525 break;
526
527 case VME_AM_USERDEF:
528 fmt = "USR:%08x-%08x ";
529 break;
530 }
531
532 sprintf(mstring, fmt, addr, addr + len - 1);
533 strcat(mstring, dsiz[ds & 0x7]);
534 strcat(mstring, ((am & VME_AM_PRIVMASK) == VME_AM_USER) ?
535 "USER," : "SUPER,");
536 strcat(mstring, mode[am & VME_AM_MODEMASK]);
537
538 return (mstring);
539 }
540 #endif
541