summaryrefslogtreecommitdiff
path: root/sys/dev/pci/radeonfb_bios.c
blob: 62eb6371b5fd0d8092b177f56b11736cdf1a0fd2 (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
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/* $NetBSD: radeonfb_bios.c,v 1.7 2022/09/25 17:52:25 thorpej Exp $ */

/*-
 * Copyright (c) 2006 Itronix Inc.
 * All rights reserved.
 *
 * Written by Garrett D'Amore for Itronix Inc.
 *
 * 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. The name of Itronix Inc. may not be used to endorse
 *    or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``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 ITRONIX INC. 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.
 */ 

/*
 * ATI Technologies Inc. ("ATI") has not assisted in the creation of, and
 * does not endorse, this software.  ATI will not be responsible or liable
 * for any actual or alleged damage or loss caused by or in connection with
 * the use of or reliance on this software.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: radeonfb_bios.c,v 1.7 2022/09/25 17:52:25 thorpej Exp $");

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

#include <dev/pci/pcidevs.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/radeonfbreg.h>
#include <dev/pci/radeonfbvar.h>

#include "opt_radeonfb.h"

#ifdef RADEONFB_BIOS_INIT

/*
 * Globals for the entire BIOS.
 */
#define	ROM_HEADER_OFFSET		0x48
#define	MAX_REVISION			0x10
#define	SINGLE_TABLE_REVISION		0x09
#define	MIN_OFFSET			0x60

/*
 * Offsets of specific tables.
 */
#define	RAGE_REGS1_OFFSET		0x0c
#define	RAGE_REGS2_OFFSET		0x4e
#define	DYN_CLOCK_OFFSET		0x52
#define	PLL_INIT_OFFSET			0x46
#define	MEM_CONFIG_OFFSET		0x48

/*
 * Values related to generic initialization tables.
 */
#define	TABLE_ENTRY_FLAG_MASK		0xe000
#define	TABLE_ENTRY_INDEX_MASK		0x1fff
#define	TABLE_ENTRY_COMMAND_MASK	0x00ff

#define	TABLE_FLAG_WRITE_INDEXED	0x0000
#define	TABLE_FLAG_WRITE_DIRECT		0x2000
#define	TABLE_FLAG_MASK_INDEXED		0x4000
#define	TABLE_FLAG_MASK_DIRECT		0x6000
#define	TABLE_FLAG_DELAY		0x8000
#define	TABLE_FLAG_SCOMMAND		0xa000

#define	TABLE_SCOMMAND_WAIT_MC_BUSY_MASK	0x03
#define	TABLE_SCOMMAND_WAIT_MEM_PWRUP_COMPLETE	0x08

/*
 * PLL initialization block values.
 */
#define	PLL_FLAG_MASK			0xc0
#define	PLL_INDEX_MASK			0x3f

#define	PLL_FLAG_WRITE			0x00
#define	PLL_FLAG_MASK_BYTE		0x40
#define	PLL_FLAG_WAIT			0x80

#define	PLL_WAIT_150MKS				1
#define	PLL_WAIT_5MS				2
#define	PLL_WAIT_MC_BUSY_MASK			3
#define	PLL_WAIT_DLL_READY_MASK			4
#define	PLL_WAIT_CHK_SET_CLK_PWRMGT_CNTL24	5


#ifdef	RADEONFB_BIOS_DEBUG
#define	DPRINTF(x)	printf x
#else
#define	DPRINTF(x)
#endif

struct rb_table;

static void rb_validate(struct radeonfb_softc *, struct rb_table *);
static uint16_t rb_find_asic_table(struct radeonfb_softc *, struct rb_table *);
static uint16_t rb_find_mem_reset_table(struct radeonfb_softc *,
    struct rb_table *);
static uint16_t rb_find_short_mem_reset_table(struct radeonfb_softc *,
    struct rb_table *);
static int rb_load_init_block(struct radeonfb_softc *, struct rb_table *);
static int rb_load_pll_block(struct radeonfb_softc *, struct rb_table *);
static int rb_reset_sdram(struct radeonfb_softc *, struct rb_table *);

static void rb_wait_mc_busy_mask(struct radeonfb_softc *, uint16_t);
static void rb_wait_mem_pwrup_complete(struct radeonfb_softc *, uint16_t);
static void rb_wait_dll_ready_mask(struct radeonfb_softc *, uint16_t);
static void rb_wait_chk_set_clk_pwrmgt_cntl24(struct radeonfb_softc *);

/*
 * Generic structure describing the tables.
 */
struct rb_table {
	const unsigned char	*name;
	uint16_t		offset;
	struct rb_table 	*parent;

	/* validate that the table looks sane */
	void	(*validate)(struct radeonfb_softc *, struct rb_table *);

	/* find looks for the table relative to its "parent" */
	uint16_t	(*find)(struct radeonfb_softc *, struct rb_table *);
};

/*
 * Instances of specific tables.
 */
static struct rb_table rb_rage_regs1_table = {
	"rage_regs_1",			/* name */
	RAGE_REGS1_OFFSET,		/* offset */
	NULL,				/* parent */
	rb_validate,			/* validate */
	NULL,				/* find */
};

static struct rb_table rb_rage_regs2_table = {
	"rage_regs_2",			/* name */
	RAGE_REGS2_OFFSET,		/* offset */
	NULL,				/* parent */
	rb_validate,			/* validate */
	NULL,				/* find */
};

static struct rb_table rb_dyn_clock_table = {
	"dyn_clock",			/* name */
	DYN_CLOCK_OFFSET,		/* offset */
	NULL,				/* parent */
	rb_validate,			/* validate */
	NULL,				/* find */
};

static struct rb_table rb_pll_init_table = {
	"pll_init",			/* name */
	PLL_INIT_OFFSET,		/* offset */
	NULL,				/* parent */
	rb_validate,			/* validate */
	NULL,				/* find */
};

static struct rb_table rb_mem_config_table = {
	"mem_config",			/* name */
	MEM_CONFIG_OFFSET,		/* offset */
	NULL,				/* parent */
	rb_validate,			/* validate */
	NULL,				/* find */
};

static struct rb_table rb_mem_reset_table = {
	"mem_reset",			/* name */
	0,				/* offset */
	&rb_mem_config_table,		/* parent */
	NULL,				/* validate */
	rb_find_mem_reset_table,	/* find */
};

static struct rb_table rb_short_mem_reset_table = {
	"short_mem_reset",		/* name */
	0,				/* offset */
	&rb_mem_config_table,		/* parent */
	NULL,				/* validate */
	rb_find_short_mem_reset_table,	/* find */
};

static struct rb_table rb_rage_regs3_table = {
	"rage_regs_3",			/* name */
	0,				/* offset */
	&rb_rage_regs2_table,		/* parent */
	NULL,				/* validate */
	rb_find_asic_table,		/* find */
};

static struct rb_table rb_rage_regs4_table = {
	"rage_regs_4",			/* name */
	0,				/* offset */
	&rb_rage_regs3_table,		/* parent */
	NULL,				/* validate */
	rb_find_asic_table,		/* find */
};

static struct rb_table *rb_tables[] = {
	&rb_rage_regs1_table,
	&rb_rage_regs2_table,
	&rb_dyn_clock_table,
	&rb_pll_init_table,
	&rb_mem_config_table,
	&rb_mem_reset_table,
	&rb_short_mem_reset_table,
	&rb_rage_regs3_table,
	&rb_rage_regs4_table,
	NULL
};

void
rb_validate(struct radeonfb_softc *sc, struct rb_table *tp)
{
	uint8_t	rev;

	rev = GETBIOS8(sc, tp->offset - 1);

	if (rev > MAX_REVISION) {
		DPRINTF(("%s: bad rev %x of %s\n", XNAME(sc), rev, tp->name));
		tp->offset = 0;
		return;
	}

	if (tp->offset < MIN_OFFSET) {
		DPRINTF(("%s: wrong pointer to %s!\n", XNAME(sc), tp->name));
		tp->offset = 0;
		return;
	}
}

uint16_t
rb_find_asic_table(struct radeonfb_softc *sc, struct rb_table *tp)
{
	uint16_t		offset;
	uint8_t			c;

	if ((offset = tp->offset) != 0) {
		while ((c = GETBIOS8(sc, offset + 1)) != 0) {
			if (c & 0x40)
				offset += 10;
			else if (c & 0x80)
				offset += 4;
			else
				offset += 6;
		}
		return offset + 2;
	}
	return 0;
}

uint16_t
rb_find_mem_reset_table(struct radeonfb_softc *sc, struct rb_table *tp)
{
	uint16_t		offset;

	if ((offset = tp->offset) != 0) {
		while (GETBIOS8(sc, offset))
			offset++;
		offset++;
		return offset + 2;	/* skip table revision and mask */
	}
	return 0;
}

uint16_t
rb_find_short_mem_reset_table(struct radeonfb_softc *sc, struct rb_table *tp)
{

	if ((tp->offset != 0) && (GETBIOS8(sc, tp->offset - 2) <= 64))
		return (tp->offset + GETBIOS8(sc, tp->offset - 3));

	return 0;
}

/* helper commands */
void
rb_wait_mc_busy_mask(struct radeonfb_softc *sc, uint16_t count)
{
	DPRINTF(("WAIT_MC_BUSY_MASK: %d ", count));
	while (count--) {
		if (!(radeonfb_getpll(sc, RADEON_CLK_PWRMGT_CNTL) &
			RADEON_MC_BUSY))
			break;
	}
	DPRINTF(("%d\n", count));
}

void
rb_wait_mem_pwrup_complete(struct radeonfb_softc *sc, uint16_t count)
{
	DPRINTF(("WAIT_MEM_PWRUP_COMPLETE: %d ", count));
	while (count--) {
		if ((radeonfb_getindex(sc, RADEON_MEM_STR_CNTL) &
			RADEON_MEM_PWRUP_COMPLETE) ==
		    RADEON_MEM_PWRUP_COMPLETE)
			break;
	}
	DPRINTF(("%d\n", count));
}

void
rb_wait_dll_ready_mask(struct radeonfb_softc *sc, uint16_t count)
{
	DPRINTF(("WAIT_DLL_READY_MASK: %d ", count));
	while (count--) {
		if (radeonfb_getpll(sc, RADEON_CLK_PWRMGT_CNTL) &
		    RADEON_DLL_READY)
			break;
	}
	DPRINTF(("%d\n", count));
}

void
rb_wait_chk_set_clk_pwrmgt_cntl24(struct radeonfb_softc *sc)
{
	uint32_t	pmc;
	DPRINTF(("WAIT CHK_SET_CLK_PWRMGT_CNTL24\n"));
	pmc = radeonfb_getpll(sc, RADEON_CLK_PWRMGT_CNTL);

	if (pmc & RADEON_CLK_PWRMGT_CNTL24) {
		radeonfb_maskpll(sc, RADEON_MCLK_CNTL, 0xFFFF0000,
		    RADEON_SET_ALL_SRCS_TO_PCI);
		delay(10000);
		radeonfb_putpll(sc, RADEON_CLK_PWRMGT_CNTL,
		    pmc & ~RADEON_CLK_PWRMGT_CNTL24);
		delay(10000);
	}
}

/*
 * Block initialization routines.  These take action based on data in
 * the tables.
 */
int
rb_load_init_block(struct radeonfb_softc *sc, struct rb_table *tp)
{
	uint16_t	offset;
	uint16_t	value;

	if ((tp == NULL) || ((offset = tp->offset) == 0))
		return 1;

	DPRINTF(("%s: load_init_block processing %s\n", XNAME(sc), tp->name));
	while ((value = GETBIOS16(sc, offset)) != 0) {
		uint16_t	flag = value & TABLE_ENTRY_FLAG_MASK;
		uint16_t	index = value & TABLE_ENTRY_INDEX_MASK;
		uint8_t		command = value & TABLE_ENTRY_COMMAND_MASK;
		uint32_t	ormask;
		uint32_t	andmask;
		uint16_t	count;

		offset += 2;

		switch (flag) {
		case TABLE_FLAG_WRITE_INDEXED:
			DPRINTF(("WRITE INDEXED: %x %x\n",
				    index, (uint32_t)GETBIOS32(sc, offset)));
			radeonfb_putindex(sc, index, GETBIOS32(sc, offset));
			offset += 4;
			break;

		case TABLE_FLAG_WRITE_DIRECT:
			DPRINTF(("WRITE DIRECT: %x %x\n",
				    index, (uint32_t)GETBIOS32(sc, offset)));
			radeonfb_put32(sc, index, GETBIOS32(sc, offset));
			offset += 4;
			break;

		case TABLE_FLAG_MASK_INDEXED:
			andmask = GETBIOS32(sc, offset);
			offset += 4;
			ormask = GETBIOS32(sc, offset);
			offset += 4;
			DPRINTF(("MASK INDEXED: %x %x %x\n",
				    index, andmask, ormask));
			radeonfb_maskindex(sc, index, andmask, ormask);
			break;

		case TABLE_FLAG_MASK_DIRECT:
			andmask = GETBIOS32(sc, offset);
			offset += 4;
			ormask = GETBIOS32(sc, offset);
			offset += 4;
			DPRINTF(("MASK DIRECT: %x %x %x\n",
				    index, andmask, ormask));
			radeonfb_mask32(sc, index,  andmask, ormask);
			break;

		case TABLE_FLAG_DELAY:
			/* in the worst case, this would be 16msec */
			count = GETBIOS16(sc, offset);
			DPRINTF(("DELAY: %d\n", count));
			delay(count);
			offset += 2;
			break;

		case TABLE_FLAG_SCOMMAND:
			DPRINTF(("SCOMMAND %x\n", command)); 
			switch (command) {

			case TABLE_SCOMMAND_WAIT_MC_BUSY_MASK:
				count = GETBIOS16(sc, offset);
				rb_wait_mc_busy_mask(sc, count);
				break;

			case TABLE_SCOMMAND_WAIT_MEM_PWRUP_COMPLETE:
				count = GETBIOS16(sc, offset);
				rb_wait_mem_pwrup_complete(sc, count);
				break;

			}
			offset += 2;
			break;
		}
	}
	return 0;
}

int
rb_load_pll_block(struct radeonfb_softc *sc, struct rb_table *tp)
{
	uint16_t	offset;
	uint8_t		index;
	uint8_t		shift;
	uint32_t	andmask;
	uint32_t	ormask;

	if ((tp == NULL) || ((offset = tp->offset) == 0))
		return 1;

	DPRINTF(("%s: load_pll_block processing %s\n", XNAME(sc), tp->name));
	while ((index = GETBIOS8(sc, offset)) != 0) {
		offset++;

		switch (index & PLL_FLAG_MASK) {
		case PLL_FLAG_WAIT:
			switch (index & PLL_INDEX_MASK) {
			case PLL_WAIT_150MKS:
				delay(150);
				break;
			case PLL_WAIT_5MS:
				/* perhaps this should be tsleep? */
				delay(5000);
				break;

			case PLL_WAIT_MC_BUSY_MASK:
				rb_wait_mc_busy_mask(sc, 1000);
				break;

			case PLL_WAIT_DLL_READY_MASK:
				rb_wait_dll_ready_mask(sc, 1000);
				break;

			case PLL_WAIT_CHK_SET_CLK_PWRMGT_CNTL24:
				rb_wait_chk_set_clk_pwrmgt_cntl24(sc);
				break;
			}
			break;
			
		case PLL_FLAG_MASK_BYTE:
			shift = GETBIOS8(sc, offset) * 8;
			offset++;

			andmask =
			    (((uint32_t)GETBIOS8(sc, offset)) << shift) |
			    ~((uint32_t)0xff << shift);
			offset++;

			ormask = ((uint32_t)GETBIOS8(sc, offset)) << shift;
			offset++;

			DPRINTF(("PLL_MASK_BYTE %u %u %x %x\n", index, 
				    shift, andmask, ormask));
			radeonfb_maskpll(sc, index, andmask, ormask);
			break;

		case PLL_FLAG_WRITE:
			DPRINTF(("PLL_WRITE %u %x\n", index,
				    GETBIOS32(sc, offset)));
			radeonfb_putpll(sc, index, GETBIOS32(sc, offset));
			offset += 4;
			break;
		}
	}

	return 0;
}

int
rb_reset_sdram(struct radeonfb_softc *sc, struct rb_table *tp)
{
	uint16_t offset;
	uint8_t	index;

	if ((tp == NULL) || ((offset = tp->offset) == 0))
		return 1;

	DPRINTF(("%s: reset_sdram processing %s\n", XNAME(sc), tp->name));

	while ((index = GETBIOS8(sc, offset)) != 0xff) {
		offset++;
		if (index == 0x0f) {
			rb_wait_mem_pwrup_complete(sc, 20000);
		} else {
			uint32_t	ormask;

			ormask = GETBIOS16(sc, offset);
			offset += 2;

			DPRINTF(("INDEX reg RADEON_MEM_SDRAM_MODE_REG %x %x\n",
				    RADEON_SDRAM_MODE_MASK, ormask));
			radeonfb_maskindex(sc, RADEON_MEM_SDRAM_MODE_REG,
			    RADEON_SDRAM_MODE_MASK, ormask);

			ormask = (uint32_t)index << 24;
			DPRINTF(("INDEX reg RADEON_MEM_SDRAM_MODE_REG %x %x\n",
				    RADEON_B3MEM_RESET_MASK, ormask));
			radeonfb_maskindex(sc, RADEON_MEM_SDRAM_MODE_REG,
			    RADEON_B3MEM_RESET_MASK, ormask);
		}
	}
	return 0;
}

/*
 * Master entry point to parse and act on table data.
 */
int
radeonfb_bios_init(struct radeonfb_softc *sc)
{
	uint16_t		revision;
	uint16_t		scratch;
	int			i;
	struct rb_table		*tp;

	if (!sc->sc_biossz)
		return 1;

	scratch = GETBIOS16(sc, ROM_HEADER_OFFSET);
	revision = GETBIOS8(sc, scratch);
	DPRINTF(("%s: Bios Rev: %d\n", XNAME(sc), revision));


	/* First parse pass -- locate tables  */
	for (i = 0; (tp = rb_tables[i]) != NULL; i++) {

		DPRINTF(("%s: parsing table %s\n", XNAME(sc), tp->name));

		if (tp->offset != 0) {
			uint16_t	temp, offset;

			temp = GETBIOS16(sc, ROM_HEADER_OFFSET);
			offset = GETBIOS16(sc, temp + tp->offset);
			if (offset)
				tp->offset = offset;
			    
		} else {
			tp->offset = tp->find(sc, tp->parent);
		}

		if (tp->validate)
			tp->validate(sc, tp);

		if (revision > SINGLE_TABLE_REVISION)
			break;
	}

	if (rb_rage_regs3_table.offset + 1 == rb_pll_init_table.offset) {
		rb_rage_regs3_table.offset = 0;
		rb_rage_regs4_table.offset = 0;
	}

	if (rb_rage_regs1_table.offset)
		rb_load_init_block(sc, &rb_rage_regs1_table);

	if (revision < SINGLE_TABLE_REVISION) {
		if (rb_pll_init_table.offset)
			rb_load_pll_block(sc, &rb_pll_init_table);
		if (rb_rage_regs2_table.offset)
			rb_load_init_block(sc, &rb_rage_regs2_table);
		if (rb_rage_regs4_table.offset)
			rb_load_init_block(sc, &rb_rage_regs4_table);
		if (rb_mem_reset_table.offset)
			rb_reset_sdram(sc, &rb_mem_reset_table);
		if (rb_rage_regs3_table.offset)
			rb_load_init_block(sc, &rb_rage_regs3_table);
		if (rb_dyn_clock_table.offset)
			rb_load_pll_block(sc, &rb_dyn_clock_table);
	}

	DPRINTF(("%s: BIOS parse done\n", XNAME(sc)));
	return 0;
}

#endif