s3_bios.c revision 5788ca14
1/*
2 *      Copyright 2001  Ani Joshi <ajoshi@unixbox.com>
3 *
4 *      XFree86 4.x driver for S3 chipsets
5 *
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that copyright
10 * notice and this permission notice appear in supporting documentation and
11 * that the name of Ani Joshi not be used in advertising or
12 * publicity pertaining to distribution of the software without specific,
13 * written prior permission.  Ani Joshi makes no representations
14 * about the suitability of this software for any purpose.  It is provided
15 * "as-is" without express or implied warranty.
16 *
17 * ANI JOSHI DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19 * EVENT SHALL ANI JOSHI BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
23 * PERFORMANCE OF THIS SOFTWARE.
24 *
25 *
26 */
27
28#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
31
32#include "xf86.h"
33#include "xf86_OSproc.h"
34#include "compiler.h"
35
36#include "s3.h"
37
38
39static unsigned char *find_bios_string(ScrnInfoPtr pScrn, int BIOSbase,
40				       char *match1, char *match2)
41{
42	static unsigned char *bios;
43	static int init=0;
44	int i, j, l1, l2, ret;
45
46	S3Ptr pS3 = S3PTR(pScrn);
47
48	bios = malloc(BIOS_BSIZE);
49	if (bios = NULL)
50		return NULL;
51
52	if (!init) {
53		init = 1;
54#ifndef XSERVER_LIBPCIACCESS
55		if (xf86ReadDomainMemory(pS3->PciTag, BIOSbase, BIOS_BSIZE, bios) != BIOS_BSIZE)
56			goto error;
57#else
58		ret = pci_device_read_rom(pS3->PciInfo, bios);
59		if (ret) {
60			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
61				"libpciaccess failed to read video BIOS: %s\n",
62				strerror(-ret));
63		        goto error;
64		}
65#endif
66		if ((bios[0] != 0x55) || (bios[1] != 0xaa))
67			goto error;
68	}
69	if (match1 == NULL)
70		goto error;
71
72	l1 = strlen(match1);
73	if (match2 != NULL)
74		l2 = strlen(match2);
75	else
76		l2 = 0;
77
78	for (i=0; i<BIOS_BSIZE; i++)
79		if (bios[i] == match1[0] && !memcmp(&bios[i], match1, l1)) {
80			if (match2 == NULL)
81				return &bios[i+l1];
82			else
83				for(j=i+l1; (j<BIOS_BSIZE-l2) && bios[j]; j++)
84					if (bios[j] == match2[0] &&
85					    !memcmp(&bios[j], match2, l2))
86						return &bios[j+l2];
87		}
88error:
89	free(bios);
90	return NULL;
91}
92
93
94int S3GetRefClock(ScrnInfoPtr pScrn)
95{
96	int RefClock = 16000;	/* default */
97
98	if (find_bios_string(pScrn, BIOS_BASE, "Number Nine Visual Technology",
99					"Motion 771") != NULL)
100		RefClock = 16000;
101
102	return RefClock;
103}
104