1/*
2 * Copyright 1998 by Alan Hourihane, Wigan, England.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of Alan Hourihane not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission.  Alan Hourihane makes no representations
11 * about the suitability of this software for any purpose.  It is provided
12 * "as is" without express or implied warranty.
13 *
14 * ALAN HOURIHANE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL ALAN HOURIHANE BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
21 *
22 * Authors:  Alan Hourihane, <alanh@fairlite.demon.co.uk>
23 *
24 * BT RAMDAC routines.
25 */
26
27#ifdef HAVE_XORG_CONFIG_H
28#include <xorg-config.h>
29#endif
30
31#include "xf86.h"
32#include "xf86_OSproc.h"
33
34#define INIT_BT_RAMDAC_INFO
35#include "BTPriv.h"
36#include "xf86RamDacPriv.h"
37
38void
39BTramdacRestore(ScrnInfoPtr pScrn, RamDacRecPtr ramdacPtr,
40				    RamDacRegRecPtr ramdacReg)
41{
42	int i;
43
44	/* Here we pass a short, so that we can evaluate a mask too */
45	/* So that the mask is the high byte and the data the low byte */
46	/* Just the command/status registers */
47	for (i=0x06;i<0x0A;i++)
48	    (*ramdacPtr->WriteDAC)
49	        (pScrn, i, (ramdacReg->DacRegs[i] & 0xFF00) >> 8,
50						ramdacReg->DacRegs[i]);
51}
52
53void
54BTramdacSave(ScrnInfoPtr pScrn, RamDacRecPtr ramdacPtr,
55				 RamDacRegRecPtr ramdacReg)
56{
57	int i;
58
59	(*ramdacPtr->ReadAddress)(pScrn, 0); /* Start at index 0 */
60	for (i=0;i<768;i++)
61	    ramdacReg->DAC[i] = (*ramdacPtr->ReadData)(pScrn);
62
63	/* Just the command/status registers */
64	for (i=0x06;i<0x0A;i++)
65	    ramdacReg->DacRegs[i] = (*ramdacPtr->ReadDAC)(pScrn, i);
66}
67
68RamDacHelperRecPtr
69BTramdacProbe(ScrnInfoPtr pScrn, RamDacSupportedInfoRecPtr ramdacs/*, RamDacRecPtr ramdacPtr*/)
70{
71    RamDacRecPtr ramdacPtr = RAMDACSCRPTR(pScrn);
72    Bool RamDacIsSupported = FALSE;
73    RamDacHelperRecPtr ramdacHelperPtr = NULL;
74    int BTramdac_ID = -1;
75    int i, status, cmd0;
76
77    /* Save COMMAND Register 0 */
78    cmd0 = (*ramdacPtr->ReadDAC)(pScrn, BT_COMMAND_REG_0);
79    /* Ensure were going to access the STATUS Register on next read */
80    (*ramdacPtr->WriteDAC)(pScrn, BT_COMMAND_REG_0, 0x7F, 0x00);
81
82    status = (*ramdacPtr->ReadDAC)(pScrn, BT_STATUS_REG);
83    switch (status) {
84	case 0x40:
85		BTramdac_ID = ATT20C504_RAMDAC;
86		break;
87	case 0xD0:
88		BTramdac_ID = ATT20C505_RAMDAC;
89		break;
90	default:
91		xf86DrvMsg(pScrn->scrnIndex, X_PROBED,
92			   "Unknown BT RAMDAC type (0x%x), assuming BT485\n",
93			   status);
94	case 0x80:
95	case 0x90:
96	case 0xA0:
97	case 0xB0:
98	case 0x28: 	/* This is for the DEC TGA - Questionable ? */
99		BTramdac_ID = BT485_RAMDAC;
100		break;
101    }
102
103    /* Restore COMMAND Register 0 */
104    (*ramdacPtr->WriteDAC)(pScrn, BT_COMMAND_REG_0, 0x00, cmd0);
105
106    if (BTramdac_ID == -1) {
107        xf86DrvMsg(pScrn->scrnIndex, X_PROBED,
108		"Cannot determine BT RAMDAC type, aborting\n");
109	return NULL;
110    } else {
111        xf86DrvMsg(pScrn->scrnIndex, X_PROBED,
112		"Attached RAMDAC is %s\n", BTramdacDeviceInfo[BTramdac_ID&0xFFFF].DeviceName);
113    }
114
115    for (i=0;ramdacs[i].token != -1;i++) {
116	if (ramdacs[i].token == BTramdac_ID)
117	    RamDacIsSupported = TRUE;
118    }
119
120    if (!RamDacIsSupported) {
121        xf86DrvMsg(pScrn->scrnIndex, X_PROBED,
122		"This BT RAMDAC is NOT supported by this driver, aborting\n");
123	return NULL;
124    }
125
126    ramdacHelperPtr = RamDacHelperCreateInfoRec();
127    switch(BTramdac_ID) {
128	case BT485_RAMDAC:
129	    ramdacHelperPtr->SetBpp = BTramdacSetBpp;
130	    break;
131    }
132    ramdacPtr->RamDacType = BTramdac_ID;
133    ramdacHelperPtr->RamDacType = BTramdac_ID;
134    ramdacHelperPtr->Save = BTramdacSave;
135    ramdacHelperPtr->Restore = BTramdacRestore;
136
137    return ramdacHelperPtr;
138}
139
140void
141BTramdacSetBpp(ScrnInfoPtr pScrn, RamDacRegRecPtr ramdacReg)
142{
143    /* We need to deal with Direct Colour visuals for 8bpp and other
144     * good stuff for colours */
145    switch (pScrn->bitsPerPixel) {
146	case 32:
147	    ramdacReg->DacRegs[BT_COMMAND_REG_1] = 0x10;
148	    break;
149	case 24:
150	    ramdacReg->DacRegs[BT_COMMAND_REG_1] = 0x10;
151	    break;
152	case 16:
153	    ramdacReg->DacRegs[BT_COMMAND_REG_1] = 0x38;
154	    break;
155	case 15:
156	    ramdacReg->DacRegs[BT_COMMAND_REG_1] = 0x30;
157	    break;
158	case 8:
159	    ramdacReg->DacRegs[BT_COMMAND_REG_1] = 0x40;
160	    break;
161	case 4:
162	    ramdacReg->DacRegs[BT_COMMAND_REG_1] = 0x60;
163	    break;
164    }
165}
166