Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*	$NetBSD: bcu_vrip.c,v 1.30 2012/10/27 17:17:54 chs Exp $	*/

/*-
 * Copyright (c) 1999-2001 SATO Kazumi. All rights reserved.
 * Copyright (c) 1999, 2002 PocketBSD Project. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the PocketBSD project
 *	and its contributors.
 * 4. Neither the name of the project nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: bcu_vrip.c,v 1.30 2012/10/27 17:17:54 chs Exp $");

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

#include <machine/bus.h>
#include <machine/debug.h>
#include <machine/platid.h>
#include <machine/platid_mask.h>

#include <mips/cpuregs.h>

#include "opt_vr41xx.h"
#include <hpcmips/vr/vr.h>
#include <hpcmips/vr/vrcpudef.h>
#include <hpcmips/vr/vripif.h>
#include <hpcmips/vr/vripvar.h>
#include <hpcmips/vr/vripreg.h>
#include <hpcmips/vr/bcureg.h>
#include <hpcmips/vr/bcuvar.h>

static int vrbcu_match(device_t, cfdata_t, void *);
static void vrbcu_attach(device_t, device_t, void *);

static void vrbcu_write(struct vrbcu_softc *, int, unsigned short);
static unsigned short vrbcu_read(struct vrbcu_softc *, int);

static void vrbcu_dump_regs(void);

const char *vr_cpuname=NULL;
int	vr_major=-1;
int	vr_minor=-1;
int	vr_cpuid=-1;

CFATTACH_DECL_NEW(vrbcu, sizeof(struct vrbcu_softc),
    vrbcu_match, vrbcu_attach, NULL, NULL);

struct vrbcu_softc *the_bcu_sc = NULL;

#ifdef SINGLE_VRIP_BASE
#define vrbcu_addr()	VRIP_BCU_ADDR
#else
static bus_addr_t vrbcu_addr(void);
static bus_addr_t
vrbcu_addr(void)
{
	static bus_addr_t addr = 0;
	static struct platid_data addrs[] = {
		{ &platid_mask_CPU_MIPS_VR_4102, (void *)VR4102_BCU_ADDR },
		{ &platid_mask_CPU_MIPS_VR_4111, (void *)VR4102_BCU_ADDR },
		{ &platid_mask_CPU_MIPS_VR_4121, (void *)VR4102_BCU_ADDR },
		{ &platid_mask_CPU_MIPS_VR_4122, (void *)VR4122_BCU_ADDR },
		{ &platid_mask_CPU_MIPS_VR_4131, (void *)VR4122_BCU_ADDR },
		{ &platid_mask_CPU_MIPS_VR_4181, (void *)VR4181_BCU_ADDR },
		{ NULL, NULL }	/* terminator, don't delete */
	};
	struct platid_data *p;

	if (addr == 0) {
		if ((p = platid_search_data(&platid, addrs)) == NULL)
			panic("%s: can't find VR BCU address", __func__);
		addr = (bus_addr_t)p->data;
	}

	return (addr);
}
#endif /* SINGLE_VRIP_BASE */

static inline void
vrbcu_write(struct vrbcu_softc *sc, int port, unsigned short val)
{

	bus_space_write_2(sc->sc_iot, sc->sc_ioh, port, val);
}

static inline unsigned short
vrbcu_read(struct vrbcu_softc *sc, int port)
{

	return (bus_space_read_2(sc->sc_iot, sc->sc_ioh, port));
}

static int
vrbcu_match(device_t parent, cfdata_t cf, void *aux)
{

	return (2);
}

static void
vrbcu_attach(device_t parent, device_t self, void *aux)
{
	struct vrip_attach_args *va = aux;
	struct vrbcu_softc *sc = device_private(self);

	sc->sc_dev = self;
	sc->sc_iot = va->va_iot;
	bus_space_map(sc->sc_iot, va->va_addr, va->va_size,
	    0, /* no flags */
	    &sc->sc_ioh);

	printf("\n");
	the_bcu_sc = sc;
	vrbcu_dump_regs();
}

static void
vrbcu_dump_regs(void)
{
	struct vrbcu_softc *sc = the_bcu_sc;
	int cpuclock = 0, tclock = 0, vtclock = 0, cpuid;
#if !defined(ONLY_VR4102)
	int spdreg = 0;	/* XXX gcc doesn't stand a chance of tracking this! */
#endif
#ifdef VRBCUDEBUG
	int reg;
#endif /* VRBCUDEBUG */

	cpuid = vrbcu_vrip_getcpuid();
#if !defined(ONLY_VR4181) && !defined(ONLY_VR4102)
	if (cpuid != BCUREVID_FIXRID_4181 
	    && cpuid <= BCUREVID_RID_4131
	    && cpuid >= BCUREVID_RID_4111) {
		spdreg = vrbcu_read(sc, BCUCLKSPEED_REG_W);
#ifdef VRBCUDEBUG
		printf("vrbcu: CLKSPEED %x: \n",  spdreg);
#endif /* VRBCUDEBUG */
	}
#endif
#if defined VR4181
	if (cpuid == BCUREVID_FIXRID_4181){
		spdreg = vrbcu_read(sc, BCU81CLKSPEED_REG_W);
#ifdef VRBCUDEBUG
		printf("vrbcu: CLKSPEED %x: \n",  spdreg);
#endif /* VRBCUDEBUG */
	}
#endif

	cpuclock = vrbcu_vrip_getcpuclock();

	switch (cpuid) {
#if defined VR4181
	case BCUREVID_FIXRID_4181:
		switch ((spdreg & BCU81CLKSPEED_DIVTMASK) >>
		    BCU81CLKSPEED_DIVTSHFT){
		case BCU81CLKSPEED_DIVT1:
			vtclock = tclock = cpuclock;
			break;
		case BCU81CLKSPEED_DIVT2:
			vtclock = tclock = cpuclock/2;
			break;
		case BCU81CLKSPEED_DIVT3:
			vtclock = tclock = cpuclock/3;
			break;
		case BCU81CLKSPEED_DIVT4:
			vtclock = tclock = cpuclock/4;
			break;
		default:
			vtclock = tclock = 0;
		}
		break;
#endif /* VR4181 */
	case BCUREVID_RID_4101:
	case BCUREVID_RID_4102:
		vtclock = tclock = cpuclock/2;
		break;
#if defined VR4111
	case BCUREVID_RID_4111:
		if ((spdreg&BCUCLKSPEED_DIVT2B) == 0) 
			vtclock = tclock = cpuclock/2;
		else if ((spdreg&BCUCLKSPEED_DIVT3B) == 0) 
			vtclock = tclock = cpuclock/3;
		else if ((spdreg&BCUCLKSPEED_DIVT4B) == 0) 
			vtclock = tclock = cpuclock/4;
		else
			vtclock = tclock = 0; /* XXX */
		break;
#endif /* VR4111 */
#if defined VR4121
	case BCUREVID_RID_4121:
	{
		int vt;
		tclock = cpuclock / ((spdreg & BCUCLKSPEED_DIVTMASK) >>
		    BCUCLKSPEED_DIVTSHFT);
		vt = ((spdreg & BCUCLKSPEED_DIVVTMASK) >>
		    BCUCLKSPEED_DIVVTSHFT);
		if (vt == 0)
			vtclock = 0; /* XXX */
		else if (vt < 0x9)
			vtclock = cpuclock / vt;
		else
			vtclock = cpuclock / ((vt - 8)*2+1) * 2;
	}
	break;
#endif /* VR4121 */
#if defined VR4122 || defined VR4131
	case BCUREVID_RID_4122:
	case BCUREVID_RID_4131:
	{
		int vtdiv;

		vtdiv = ((spdreg & BCUCLKSPEED_VTDIVMODE) >>
		    BCUCLKSPEED_VTDIVSHFT);
		if (vtdiv == 0 || vtdiv > BCUCLKSPEED_VTDIV6)
			vtclock = 0; /* XXX */
		else
			vtclock = cpuclock / vtdiv;
		tclock = vtclock /
		    (((spdreg & BCUCLKSPEED_TDIVMODE) >>
			BCUCLKSPEED_TDIVSHFT) ? 4 : 2);
	}
	break;
#endif /* VR4122 || VR4131 */
	default:
		break;
	}
	if (tclock)
		printf("%s: CPU %d.%03dMHz, bus %d.%03dMHz, ram %d.%03dMHz\n",
		    device_xname(sc->sc_dev),
		    cpuclock/1000000, (cpuclock%1000000)/1000,
		    tclock/1000000, (tclock%1000000)/1000,
		    vtclock/1000000, (vtclock%1000000)/1000);
	else {
		printf("%s: CPU %d.%03dMHz\n",
		    device_xname(sc->sc_dev),
		    cpuclock/1000000, (cpuclock%1000000)/1000);
		printf("%s: UNKNOWN BUS CLOCK SPEED:"
		    " CPU is UNKNOWN or NOT CONFIGURED\n",
		    device_xname(sc->sc_dev));
	}
#ifdef VRBCUDEBUG
	reg = vrbcu_read(sc, BCUCNT1_REG_W);
	printf("vrbcu: CNT1 %x: ",  reg);
	dbg_bit_print(reg);
#if !defined(ONLY_VR4181)
	if (cpuid != BCUREVID_FIXRID_4181 
	    && cpuid <= BCUREVID_RID_4121
	    && cpuid >= BCUREVID_RID_4102) {
		reg = vrbcu_read(sc, BCUCNT2_REG_W);
		printf("vrbcu: CNT2 %x: ",  reg);
		dbg_bit_print(reg);
	}
#endif /* !defined ONLY_VR4181 */
#if !defined(ONLY_VR4181) || !defined(ONLY_VR4122_4131)
	if (cpuid != BCUREVID_FIXRID_4181
	    && cpuid <= BCUREVID_RID_4121
	    && cpuid >= BCUREVID_RID_4102) {
		reg = vrbcu_read(sc, BCUSPEED_REG_W);
		printf("vrbcu: SPEED %x: ",  reg);
		dbg_bit_print(reg);
		reg = vrbcu_read(sc, BCUERRST_REG_W);
		printf("vrbcu: ERRST %x: ",  reg);
		dbg_bit_print(reg);
		reg = vrbcu_read(sc, BCURFCNT_REG_W);
		printf("vrbcu: RFCNT %x\n",  reg);
		reg = vrbcu_read(sc, BCUREFCOUNT_REG_W);
		printf("vrbcu: RFCOUNT %x\n",  reg);
	}
#endif /* !defined(ONLY_VR4181) || !defined(ONLY_VR4122_4131) */
#if !defined(ONLY_VR4181)
	if (cpuid != BCUREVID_FIXRID_4181 
	    && cpuid <= BCUREVID_RID_4131
	    && cpuid >= BCUREVID_RID_4111)
	{
		reg = vrbcu_read(sc, BCUCNT3_REG_W);
		printf("vrbcu: CNT3 %x: ",  reg);
		dbg_bit_print(reg);
	}
#endif /* !defined ONLY_VR4181 */
#endif /* VRBCUDEBUG */

}

static const char *cpuname[] = {
	"VR4101",	/* 0 */
	"VR4102",	/* 1 */
	"VR4111",	/* 2 */
	"VR4121",	/* 3 */
	"VR4122",	/* 4 */
	"VR4131",	/* 5 */
	"UNKNOWN",
	"UNKNOWN",
	"UNKNOWN",
	"UNKNOWN",
	"UNKNOWN",
	"UNKNOWN",
	"UNKNOWN",
	"UNKNOWN",
	"UNKNOWN",
	"UNKNOWN",
	"VR4181",	/* 0x10 + 0 */
};

int
vrbcu_vrip_getcpuid(void)
{
	volatile u_int16_t *revreg;

	if (vr_cpuid != -1)
		return (vr_cpuid); 

	if (vr_cpuid == -1) {
		if (vrbcu_addr() == VR4181_BCU_ADDR)
			revreg = (u_int16_t *)MIPS_PHYS_TO_KSEG1
			    ((vrbcu_addr() + BCU81REVID_REG_W));
		else
			revreg = (u_int16_t *)MIPS_PHYS_TO_KSEG1
			    ((vrbcu_addr() + BCUREVID_REG_W));

		vr_cpuid = *revreg;
		vr_cpuid = (vr_cpuid&BCUREVID_RIDMASK)>>BCUREVID_RIDSHFT;
		if (vrbcu_addr() == VR4181_BCU_ADDR 
		    && vr_cpuid == BCUREVID_RID_4181) /* conflict vr4101 */
			vr_cpuid = BCUREVID_FIXRID_4181;
	}
	return (vr_cpuid);
}	

const char *
vrbcu_vrip_getcpuname(void)
{
	int cpuid;	

	if (vr_cpuname != NULL)
		return (vr_cpuname);

	cpuid = vrbcu_vrip_getcpuid();
	vr_cpuname = cpuname[cpuid];

	return (vr_cpuname);
}	


int
vrbcu_vrip_getcpumajor(void)
{
	volatile u_int16_t *revreg;

	if (vr_major != -1)
		return (vr_major);

	revreg = (u_int16_t *)MIPS_PHYS_TO_KSEG1
	    ((vrbcu_addr() + BCUREVID_REG_W));

	vr_major = *revreg;
	vr_major = (vr_major&BCUREVID_MJREVMASK)>>BCUREVID_MJREVSHFT;

	return (vr_major);
}	

int
vrbcu_vrip_getcpuminor(void)
{
	volatile u_int16_t *revreg;

	if (vr_minor != -1)
		return (vr_minor);

	revreg = (u_int16_t *)MIPS_PHYS_TO_KSEG1
	    ((vrbcu_addr() + BCUREVID_REG_W));

	vr_minor = *revreg;
	vr_minor = (vr_minor&BCUREVID_MNREVMASK)>>BCUREVID_MNREVSHFT;

	return (vr_minor);
}	

#define CLKX	18432000	/* CLKX1,CLKX2: 18.432MHz */
#define MHZ	1000000

int
vrbcu_vrip_getcpuclock(void)
{
	u_int16_t clksp = 0;
	int cpuid, cpuclock;

	cpuid = vrbcu_vrip_getcpuid();
	if (cpuid != BCUREVID_FIXRID_4181 && cpuid >= BCUREVID_RID_4111) {
		clksp = *(u_int16_t *)MIPS_PHYS_TO_KSEG1
		    ((vrbcu_addr() + BCUCLKSPEED_REG_W)) &
		    BCUCLKSPEED_CLKSPMASK;
	} else if (cpuid == BCUREVID_FIXRID_4181) {
		clksp = *(u_int16_t *)MIPS_PHYS_TO_KSEG1
		    ((vrbcu_addr() + BCU81CLKSPEED_REG_W)) &
		    BCUCLKSPEED_CLKSPMASK;
	}

	switch (cpuid) {
	case BCUREVID_FIXRID_4181:
		cpuclock = CLKX / clksp * 64;
		/* branch delay is 1 clock; 2 clock/loop */
		cpuspeed = (cpuclock / 2 + MHZ / 2) / MHZ;
		break;
	case BCUREVID_RID_4101:
		/* assume 33MHz */
		cpuclock = 33000000;
		/* branch delay is 1 clock; 2 clock/loop */
		cpuspeed = (cpuclock / 2 + MHZ / 2) / MHZ;
		break;
	case BCUREVID_RID_4102:
		cpuclock = CLKX / clksp * 32;
		/* branch delay is 1 clock; 2 clock/loop */
		cpuspeed = (cpuclock / 2 + MHZ / 2) / MHZ;
		break;
	case BCUREVID_RID_4111:
		cpuclock = CLKX / clksp * 64;
		/* branch delay is 1 clock; 2 clock/loop */
		cpuspeed = (cpuclock / 2 + MHZ / 2) / MHZ;
		break;
	case BCUREVID_RID_4121:
		cpuclock = CLKX / clksp * 64;
		/* branch delay is 2 clock; 3 clock/loop */
		cpuspeed = (cpuclock / 3 + MHZ / 2) / MHZ;
		break;
	case BCUREVID_RID_4122:
		cpuclock = CLKX / clksp * 98;
		/* branch delay is 2 clock; 3 clock/loop */
		cpuspeed = (cpuclock / 3 + MHZ / 2) / MHZ;
		break;
	case BCUREVID_RID_4131:
		cpuclock = CLKX / clksp * 98;
		/* branch delay is 2 clock; 3 clock/loop */
		cpuspeed = (cpuclock / 3 + MHZ / 2) / MHZ;
		break;
	default:
		panic("unknown CPU type %d", cpuid);
		break;
	}

	return (cpuclock);
}