1ba0eab60Smacallan/*
2ba0eab60Smacallan * Id: newport_cmap.c,v 1.1 2000/11/29 20:58:10 agx Exp $
3ba0eab60Smacallan */
4ba0eab60Smacallan/* $XFree86: xc/programs/Xserver/hw/xfree86/drivers/newport/newport_cmap.c,v 1.2 2001/11/23 19:50:45 dawes Exp $ */
5ba0eab60Smacallan
6ba0eab60Smacallan#ifdef HAVE_CONFIG_H
7ba0eab60Smacallan#include "config.h"
8ba0eab60Smacallan#endif
9ba0eab60Smacallan
10ba0eab60Smacallan#include "newport.h"
11ba0eab60Smacallan
12ba0eab60Smacallan#ifndef USEFIFOWAIT
13ba0eab60Smacallan#define USEFIFOWAIT 0
14ba0eab60Smacallan#endif
15ba0eab60Smacallan
16ba0eab60Smacallanstatic void NewportCmapGetRGB( NewportRegsPtr pNewportRegs, unsigned short addr, LOCO *color);
17ba0eab60Smacallan#if USEFIFOWAIT
18ba0eab60Smacallanstatic void NewportCmapFifoWait( NewportRegsPtr pNewportRegs);
19ba0eab60Smacallan#endif
20ba0eab60Smacallan
21ba0eab60Smacallan/* Load a colormap into the hardware */
22ba0eab60Smacallanvoid NewportLoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices, \
23ba0eab60Smacallan			LOCO* colors, VisualPtr pVisual)
24ba0eab60Smacallan{
25ba0eab60Smacallan	int i,index;
26ba0eab60Smacallan	NewportRegsPtr pNewportRegs = NEWPORTPTR(pScrn)->pNewportRegs;
27ba0eab60Smacallan
28ba0eab60Smacallan	for(i = 0; i < numColors; i++) {
29ba0eab60Smacallan		index=indices[i];
30ba0eab60Smacallan 		NewportBfwait(pNewportRegs);
31ba0eab60Smacallan		NewportCmapSetRGB(pNewportRegs, index, colors[index]);
32ba0eab60Smacallan        }
33ba0eab60Smacallan}
34ba0eab60Smacallan
35ba0eab60Smacallanvoid NewportBackupPalette(ScrnInfoPtr pScrn)
36ba0eab60Smacallan{
37ba0eab60Smacallan	int i;
38ba0eab60Smacallan	NewportPtr pNewport = NEWPORTPTR(pScrn);
39ba0eab60Smacallan
40ba0eab60Smacallan	NewportWait(pNewport->pNewportRegs);
41ba0eab60Smacallan	for(i = 0; i < 256; i++) {
42ba0eab60Smacallan		NewportCmapGetRGB(pNewport->pNewportRegs, i, &(pNewport->txt_colormap[i]));
43ba0eab60Smacallan	}
44ba0eab60Smacallan}
45ba0eab60Smacallan
46ba0eab60Smacallan#ifdef linux
47ba0eab60Smacallan/* stolen from kernel :) */
48ba0eab60Smacallanstatic unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
49ba0eab60Smacallan				       8,12,10,14, 9,13,11,15 };
50ba0eab60Smacallan
51ba0eab60Smacallan/* the default colour table, for VGA+ colour systems */
52ba0eab60Smacallanstatic int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
53ba0eab60Smacallan    0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
54ba0eab60Smacallanstatic int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
55ba0eab60Smacallan    0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
56ba0eab60Smacallanstatic int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
57ba0eab60Smacallan    0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
58ba0eab60Smacallan#endif
59ba0eab60Smacallan
60ba0eab60Smacallan/* restore the default colormap */
61ba0eab60Smacallanvoid NewportRestorePalette(ScrnInfoPtr pScrn)
62ba0eab60Smacallan{
63ba0eab60Smacallan	int i;
64ba0eab60Smacallan	NewportPtr pNewport = NEWPORTPTR(pScrn);
65ba0eab60Smacallan
66ba0eab60Smacallan#ifdef linux
67ba0eab60Smacallan        for (i = 0; i < 16; i++) {
68ba0eab60Smacallan		pNewport->txt_colormap[color_table[i]].red = default_red[i];
69ba0eab60Smacallan		pNewport->txt_colormap[color_table[i]].green = default_grn[i];
70ba0eab60Smacallan		pNewport->txt_colormap[color_table[i]].blue = default_blu[i];
71ba0eab60Smacallan	}
72ba0eab60Smacallan#endif
73ba0eab60Smacallan	for(i = 0; i < 256; i++) {
7409048934Smacallan		NewportCmapSetRGB(pNewport->pNewportRegs, i,
7509048934Smacallan		    pNewport->txt_colormap[i]);
76ba0eab60Smacallan	}
77ba0eab60Smacallan}
78ba0eab60Smacallan
79ba0eab60Smacallan#if USEFIFOWAIT
80ba0eab60Smacallan/* wait 'til cmap fifo is completely empty */
81ba0eab60Smacallanstatic void NewportCmapFifoWait(NewportRegsPtr pNewportRegs)
82ba0eab60Smacallan{
83ba0eab60Smacallan        while(1) {
8409048934Smacallan		pNewportRegs->set.dcbmode = (NPORT_DMODE_ACM0 | NCMAP_PROTOCOL |
8509048934Smacallan		    NCMAP_REGADDR_SREG | NPORT_DMODE_W1);
86ba0eab60Smacallan		if(!(pNewportRegs->set.dcbdata0.bytes.b3 & 4))
87ba0eab60Smacallan			break;
88ba0eab60Smacallan        }
89ba0eab60Smacallan}
90ba0eab60Smacallan#endif
91ba0eab60Smacallan
92ba0eab60Smacallan/* set the colormap entry at addr to color */
93ba0eab60Smacallanvoid NewportCmapSetRGB( NewportRegsPtr pNewportRegs, unsigned short addr, LOCO color)
94ba0eab60Smacallan{
9509048934Smacallan	uint32_t colour;
96ba0eab60Smacallan	NewportWait(pNewportRegs);	/* this one should not be necessary */
97ba0eab60Smacallan	NewportBfwait(pNewportRegs);
98ba0eab60Smacallan	pNewportRegs->set.dcbmode = (NPORT_DMODE_ACMALL | NCMAP_PROTOCOL |
99ba0eab60Smacallan				NPORT_DMODE_SENDIAN | NPORT_DMODE_ECINC |
100ba0eab60Smacallan				NCMAP_REGADDR_AREG | NPORT_DMODE_W2);
101ba0eab60Smacallan	pNewportRegs->set.dcbdata0.hwords.s1 = addr;
102ba0eab60Smacallan	pNewportRegs->set.dcbmode = (NPORT_DMODE_ACMALL | NCMAP_PROTOCOL |
103ba0eab60Smacallan				 NCMAP_REGADDR_PBUF | NPORT_DMODE_W3);
10409048934Smacallan	colour = (color.red << 24) | (color.green << 16) |
105ba0eab60Smacallan						(color.blue << 8);
10609048934Smacallan	pNewportRegs->set.dcbdata0.all = colour;
107ba0eab60Smacallan}
108ba0eab60Smacallan
109ba0eab60Smacallan/* get the colormap entry at addr */
110ba0eab60Smacallanstatic void NewportCmapGetRGB( NewportRegsPtr pNewportRegs, unsigned short addr, LOCO* color)
111ba0eab60Smacallan{
112ba0eab60Smacallan	npireg_t tmp;
113ba0eab60Smacallan
114ba0eab60Smacallan	NewportBfwait(pNewportRegs);
115ba0eab60Smacallan	pNewportRegs->set.dcbmode = (NPORT_DMODE_ACM0 | NCMAP_PROTOCOL |
116ba0eab60Smacallan				NPORT_DMODE_SENDIAN | NPORT_DMODE_ECINC |
117ba0eab60Smacallan				NCMAP_REGADDR_AREG | NPORT_DMODE_W2);
118ba0eab60Smacallan	pNewportRegs->set.dcbdata0.hwords.s1 = addr;
119ba0eab60Smacallan	pNewportRegs->set.dcbmode = (NPORT_DMODE_ACM0 | NCMAP_PROTOCOL |
120ba0eab60Smacallan				 NCMAP_REGADDR_PBUF | NPORT_DMODE_W3);
121ba0eab60Smacallan	tmp = pNewportRegs->set.dcbdata0.all;
122ba0eab60Smacallan	color->red = (tmp >> 24) & 0xff;
123ba0eab60Smacallan	color->green = (tmp >> 16) & 0xff;
124ba0eab60Smacallan	color->blue = (tmp >> 8) & 0xff;
125ba0eab60Smacallan}
126ba0eab60Smacallan
127