summaryrefslogtreecommitdiff
path: root/sys/arch/vax/mbus/mbus.c
blob: 92fdd1799f77980d3077702d0911306a076016bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*	$OpenBSD: mbus.c,v 1.2 2008/09/22 19:45:35 miod Exp $	*/

/*
 * Copyright (c) 2008 Miodrag Vallat.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/malloc.h>

#include <machine/bus.h>
#include <machine/cpu.h>
#include <machine/intr.h>
#include <machine/nexus.h>
#include <machine/scb.h>

#include <vax/mbus/mbusreg.h>
#include <vax/mbus/mbusvar.h>

#define	PR_CPUID 14

/*
 * FBIC interrupt handlers
 */
struct fbic_irq {
	int	(*fi_fn)(void *);
	void	*fi_arg;
	struct evcount fi_cnt;
	int	fi_ipl;
};

/*
 * Generic information for each slot.
 *
 * This information is maintained at the mbus level, rather than
 * enforcing each child driver to provide it.  This allows proper
 * M-bus configuration on slots where no driver attaches.
 */

struct	fbic {
	paddr_t		base;
	vaddr_t		regs;
	int		vecbase;
	struct fbic_irq	*firq[FBIC_DEVIRQMAX];
};

struct mbus_slot {
	uint8_t		ms_interface;	/* MODTYPE interface */
	uint8_t		ms_class;	/* MODTYPE class */

	unsigned int	ms_nfbic;
	struct fbic	ms_fbic[2];
};

struct mbus_softc {
	struct device	sc_dev;
	struct mbus_slot *sc_slots[MBUS_SLOT_MAX];
};

void	mbus_attach(struct device *, struct device *, void *);
int	mbus_match(struct device *, void *, void *);

struct cfdriver mbus_cd = {
	NULL, "mbus", DV_DULL
};

const struct cfattach mbus_ca = {
	sizeof(struct mbus_softc), mbus_match, mbus_attach
};

void	mbus_initialize_cpu(struct mbus_slot *, unsigned int, int);
void	mbus_initialize_device(struct mbus_slot *, unsigned int, uint8_t);
void	mbus_intr_dispatch(void *);
int	mbus_print(void *, const char *);
int	mbus_submatch(struct device *, void *, void *);

unsigned int mbus_ioslot = (unsigned int)-1;

int
mbus_match(struct device *parent, void *vcf, void *aux)
{
	struct mainbus_attach_args *maa = (struct mainbus_attach_args *)aux;

	return maa->maa_bustype == VAX_MBUS ? 1 : 0;
}

void
mbus_attach(struct device *parent, struct device *self, void *aux)
{
	struct mbus_softc *sc = (struct mbus_softc *)self;
	struct mbus_slot *ms;
	unsigned int mid;
	struct mbus_attach_args maa;
	paddr_t pa;
	vaddr_t fbic;
	uint32_t modtype;
	uint8_t class, interface;

	printf("\n");

	/*
	 * Walk the bus and probe slots.
	 * We will also record information about all occupied slots,
	 * and keep a permanent mapping of their FBIC, as we will end
	 * up needing to play with them often...
	 */

	for (mid = 0; mid < MBUS_SLOT_MAX; mid++) {

		/*
		 * Map main (and often, only) FBIC.
		 */

		pa = MBUS_SLOT_BASE(mid);
		fbic = vax_map_physmem(pa + FBIC_BASE, 1);
		if (fbic == NULL)
			panic("unable to map slot %d registers", mid);

		if (badaddr((caddr_t)(fbic + FBIC_MODTYPE), 4) != 0)
			modtype = 0;
		else
			modtype = *(uint32_t *)(fbic + FBIC_MODTYPE);

		if (modtype == 0 || modtype == 0xffffffff) {
			vax_unmap_physmem(fbic, 1);
			continue;
		}

		/*
		 * The slot is populated.  Write things down.
		 */

		ms = (struct mbus_slot *)malloc(sizeof(*ms),
		    M_DEVBUF, M_NOWAIT | M_ZERO);
		if (ms == NULL)
			panic("not enough memory to probe M-bus");

		sc->sc_slots[mid] = ms;
		ms->ms_nfbic = 1;	/* only one so far! */
		ms->ms_fbic[0].base = pa + FBIC_BASE;
		ms->ms_fbic[0].regs = fbic;

		class = (modtype & MODTYPE_CLASS_MASK) >> MODTYPE_CLASS_SHIFT;
		interface = (modtype & MODTYPE_INTERFACE_MASK) >>
		    MODTYPE_INTERFACE_SHIFT;

		ms->ms_interface = interface;
		ms->ms_class = class;

		/*
		 * If there are two FBICs on this board, map the second one.
		 */

		if (class == CLASS_CPU) {
			/* the FBIC we mapped is in fact the second one... */
			ms->ms_fbic[1].base = ms->ms_fbic[0].base;
			ms->ms_fbic[1].regs = ms->ms_fbic[0].regs;
			ms->ms_nfbic = 2;
			fbic = vax_map_physmem(pa + FBIC_CPUA_BASE, 1);
			if (fbic == NULL)
				panic("unable to map slot %d registers", mid);
			ms->ms_fbic[0].base = pa + FBIC_CPUA_BASE;
			ms->ms_fbic[0].regs = fbic;
		}

		/*
		 * Perform a minimal sane initialization.
		 */

		if (class == CLASS_CPU) {
			mbus_initialize_cpu(ms, mid, 0);
			mbus_initialize_cpu(ms, mid, 1);
		} else
			mbus_initialize_device(ms, mid, interface);

		/*
		 * Attach subdevices if possible.
		 */

		maa.maa_mid = mid;
		maa.maa_class = class;
		maa.maa_subclass = (modtype & MODTYPE_SUBCLASS_MASK) >>
		    MODTYPE_SUBCLASS_SHIFT;
		maa.maa_interface = interface;
		maa.maa_revision = (modtype & MODTYPE_REVISION_MASK) >>
		    MODTYPE_REVISION_SHIFT;
		maa.maa_addr = pa;
		maa.maa_vecbase = ms->ms_fbic[0].vecbase;

		(void)config_found_sm(self, &maa, mbus_print, mbus_submatch);
	}
}

int
mbus_print(void *aux, const char *pnp)
{
	struct mbus_attach_args *maa = (struct mbus_attach_args *)aux;
	int rc = UNCONF;
	const char *descr;

	switch (maa->maa_class) {
	case CLASS_BA:
		descr = "Bus Adaptor";
		break;
	case CLASS_GRAPHICS:
		descr = "Graphics";
		break;
	case CLASS_IO:
		descr = "I/O Module";
		break;
	case CLASS_CPU:
		descr = "cpu";
		break;
	case CLASS_MEMORY:
		descr = maa->maa_interface == INTERFACE_FMDC ?
		    "ECC memory" : "Memory";
		break;
	default:
		descr = NULL;
		rc = UNSUPP;
		break;
	}

	if (maa->maa_interface != INTERFACE_FBIC) {
		if (maa->maa_class != CLASS_MEMORY ||
		    (maa->maa_interface != INTERFACE_FMCM &&
		     maa->maa_interface != INTERFACE_FMDC))
			rc = UNSUPP;
	}

	if (pnp != NULL) {
		if (rc == UNSUPP) {
			printf("logic board class %02x:%02x interface %u.%u ",
			    maa->maa_class, maa->maa_subclass,
			    maa->maa_interface, maa->maa_revision);
			if (descr != NULL)
				printf("(%s)", descr);
		} else
			printf("%s", descr);
		printf(" at %s", pnp);
	}
	printf(" mid %u", maa->maa_mid);

	return (rc);
}

int
mbus_submatch(struct device *parent, void *vcf, void *aux)
{
	struct mbus_attach_args *maa = (struct mbus_attach_args *)aux;
	struct cfdata *cf = (struct cfdata *)vcf;

	/*
	 * If attachment specifies the mid, it has to match.
	 */
	if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != maa->maa_mid)
		return 0;

	return (*cf->cf_attach->ca_match)(parent, vcf, aux);
}

/*
 * CPU board initialization.
 */

void
mbus_initialize_cpu(struct mbus_slot *ms, unsigned int mid, int cpu)
{
	struct fbic *fbic = &ms->ms_fbic[cpu];
	uint32_t fbicsr;
	int cpuid;

	cpuid = (mid << CPUID_MID_SHIFT) |
	    (cpu != 0 ? CPUID_PROC_1 : CPUID_PROC_0);

	/*
	 * Clear error log
	 */
	*(uint32_t *)(fbic->regs + FBIC_BUSCSR) = BUSCSR_RESET;

	/*
	 * Set (IPI) interrupt vectors base, but do not enable them yet.
	 */
	fbic->vecbase = MBUS_VECTOR_BASE(mid, cpu);
	*(uint32_t *)(fbic->regs + FBIC_IPDVINT) = 0 /* IPDVINT_IPUNIT */ |
	    (fbic->vecbase & IPDVINT_VECTOR_MASK);

	/*
	 * Enable all interrupt sources if on the boot processor,
	 * disable them otherwise (this does not affect IPIs).
	 */
	fbicsr = *(uint32_t *)(fbic->regs + FBIC_BUSCSR);
	if (cpuid == mfpr(PR_CPUID))
		fbicsr |= FBICSR_IRQEN_MASK;
	else
		fbicsr &= ~FBICSR_IRQEN_MASK;

	/*
	 * Route interrupts from the M-bus to the CVAX.
	 */
	fbicsr &= ~FBICSR_IRQC2M_MASK;

	/*
	 * Allow the CPU to be halted.
	 */
	fbicsr |= FBICSR_HALTEN;

	*(uint32_t *)(fbic->regs + FBIC_BUSCSR) = fbicsr;
}

/*
 * Device board initialization.
 */

void
mbus_initialize_device(struct mbus_slot *ms, unsigned int mid,
    uint8_t interface)
{
	struct fbic *fbic = ms->ms_fbic;
	uint32_t fbicsr;

	/*
	 * Clear error log if applicable
	 */
	if (interface == INTERFACE_FBIC || interface == INTERFACE_FMDC)
		*(uint32_t *)(fbic->regs + FBIC_BUSCSR) = BUSCSR_RESET;

	if (interface == INTERFACE_FBIC) {
		/*
		 * Set interrupt vector base.
		 */
		fbic->vecbase = MBUS_VECTOR_BASE(mid, 0);
		*(uint32_t *)(fbic->regs + FBIC_IPDVINT) = IPDVINT_DEVICE |
		    (fbic->vecbase & IPDVINT_VECTOR_MASK);

		/*
		 * Disable all interrupt sources, and route them
		 * from the devices to the M-bus.
		 */
		fbicsr = *(uint32_t *)(fbic->regs + FBIC_BUSCSR);
		fbicsr &= ~FBICSR_IRQEN_MASK;
		fbicsr |= FBICSR_IRQC2M_MASK;
		*(uint32_t *)(fbic->regs + FBIC_BUSCSR) = fbicsr;
	}
}

/*
 * Interrupt handling.
 */

int
mbus_intr_establish(unsigned int vec, int ipl, int (*fn)(void *), void *arg,
    const char *name)
{
	struct mbus_softc *sc;
	struct mbus_slot *ms;
	struct fbic *fbic;
	struct fbic_irq *fi;
	uint32_t fbicsr;
	unsigned int mid, fbicirq;

	mid = MBUS_VECTOR_TO_MID(vec);

#ifdef DIAGNOSTIC
	if (mid >= MBUS_SLOT_MAX)
		return EINVAL;
	if (mbus_cd.cd_ndevs == 0)
		return ENXIO;
#endif
	sc = (struct mbus_softc *)mbus_cd.cd_devs[0];
#ifdef DIAGNOSTIC
	if (sc == NULL)
		return ENXIO;
#endif
	ms = sc->sc_slots[mid];
#ifdef DIAGNOSTIC
	if (ms == NULL)
		return ENXIO;
#endif
	fi = (struct fbic_irq *)malloc(sizeof *fi, M_DEVBUF, M_NOWAIT);
	if (fi == NULL)
		return ENOMEM;

	/*
	 * This interface is intended to be used for device interrupts
	 * only, so there is no need to handle dual-FBIC slots.
	 */
	fbic = &ms->ms_fbic[0 /* MBUS_VECTOR_TO_FBIC(vec) */];

	fi->fi_fn = fn;
	fi->fi_arg = arg;
	fi->fi_ipl = ipl;
	evcount_attach(&fi->fi_cnt, name, &fi->fi_ipl, &evcount_intr);

	fbicirq = MBUS_VECTOR_TO_IRQ(vec);
	fbic->firq[fbicirq] = fi;
	scb_vecalloc(vec, mbus_intr_dispatch, fi, SCB_ISTACK, &fi->fi_cnt);

	/*
	 * Enable device interrupt in the module FBIC.  Proper direction
	 * has been setup in mbus_slot_initialize().
	 */

	fbicsr = *(uint32_t *)(fbic->regs + FBIC_BUSCSR);
	fbicsr |= fbicirq << FBICSR_IRQEN_SHIFT;
	*(uint32_t *)(fbic + FBIC_BUSCSR) = fbicsr;

	return 0;
}

/*
 * Interrupt dispatcher.
 */

void
mbus_intr_dispatch(void *v)
{
	struct fbic_irq *fi = (struct fbic_irq *)v;
	int s;

	/*
	 * FBIC interrupts are at fixed levels.  In case the level is
	 * below the level the driver expects the interrupt at, we need
	 * to raise spl to be safe (e.g. for sii).
	 */
	s = _splraise(fi->fi_ipl);
	(void)(*fi->fi_fn)(fi->fi_arg);
	splx(s);
}