summaryrefslogtreecommitdiff
path: root/hw/kdrive/neomagic/neomagic.c
blob: b4c08f0c715a49d4cd3fa03c31ece502d9a1b33d (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
/*
 *
 * Copyright © 2004 Franco Catrin
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Franco Catrin not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Franco Catrin makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * FRANCO CATRIN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL FRANCO CATRIN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "neomagic.h"
#include <sys/io.h>

struct NeoChipInfo neoChips[] = {
    {NEO_VENDOR, 0x0001, CAP_NM2070, "MagicGraph 128(NM2070)",
     896, 65000, 2048, 0x100, 1024, 1024, 1024},
    {NEO_VENDOR, 0x0002, CAP_NM2090, "MagicGraph 128V(NM2090)",
     1152, 80000, 2048, 0x100, 2048, 1024, 1024},
    {NEO_VENDOR, 0x0003, CAP_NM2090, "MagicGraph 128ZV(NM2093)",
     1152, 80000, 2048, 0x100, 2048, 1024, 1024},
    {NEO_VENDOR, 0x0083, CAP_NM2097, "MagicGraph 128ZV+(NM2097)",
     1152, 80000, 1024, 0x100, 2048, 1024, 1024},
    {NEO_VENDOR, 0x0004, CAP_NM2097, "MagicGraph 128XD(NM2160)",
     2048, 90000, 1024, 0x100, 2048, 1024, 1024},
    {NEO_VENDOR, 0x0005, CAP_NM2200, "MagicGraph 256AV(NM2200)",
     2560, 110000, 1024, 0x1000, 4096, 1280, 1024},
    {NEO_VENDOR, 0x0025, CAP_NM2200, "MagicGraph 256AV+(NM2230)",
     3008, 110000, 1024, 0x1000, 4096, 1280, 1024},
    {NEO_VENDOR, 0x0006, CAP_NM2200, "MagicGraph 256ZX(NM2360)",
     4096, 110000, 1024, 0x1000, 4096, 1280, 1024},
    {NEO_VENDOR, 0x0016, CAP_NM2200, "MagicGraph 256XL+(NM2380)",
     6144, 110000, 1024, 0x1000, 8192, 1280, 1024},
    {0, 0, 0, NULL},
};

static Bool
neoCardInit(KdCardInfo *card)
{
    ENTER();
    NeoCardInfo    *neoc;
    struct NeoChipInfo *chip;

    neoc =(NeoCardInfo *) xalloc(sizeof(NeoCardInfo));
    if(!neoc) {
        return FALSE;
    }

    if(!backendInitialize(card, &neoc->backendCard)) {
        xfree(neoc);
        return FALSE;
    }

    for(chip = neoChips; chip->name != NULL; ++chip) {
        if(chip->device == card->attr.deviceID) {
            neoc->chip = chip;
            break;
        }
    }

    ErrorF("Using Neomagic card: %s\n", neoc->chip->name);

    iopl(3);

    if (neoc->chip->caps != CAP_NONE) {
        neoMapReg(card, neoc);
    }

    card->driver = neoc;

    LEAVE();
    return TRUE;
}

static Bool
neoScreenInit(KdScreenInfo *screen)
{
    ENTER();
    NeoScreenInfo *neos;
    neoCardInfo(screen);
    int screen_size, memory;

    neos = xcalloc(sizeof(NeoScreenInfo), 1);
    if(neos == NULL) {
        return FALSE;
    }

    if(!backendScreenInitialize(screen, &neos->backendScreen, &neoc->backendCard)) {
        xfree(neos);
        return FALSE;
    }

    screen->softCursor = TRUE;    // no hardware color cursor available

    switch(neoc->backendCard.type) {
#ifdef KDRIVEFBDEV
        case FBDEV:
            neos->screen = neoc->backendCard.priv.fbdev.fb;
            break;
#endif
#ifdef KDRIVEVESA
        case VESA:
            neos->screen = neos->backendScreen.vesa.fb;
            break;
#endif
        default:
            ErrorF("Unhandled backend, we should never get here.\n");
            xfree(neos);
            return FALSE;
    }

    memory = neoc->chip->linearSize * 1024;
    screen_size = screen->fb[0].byteStride * screen->height;
    memory -= screen_size;

    if(memory > screen->fb[0].byteStride) {
        neos->off_screen = neos->screen + screen_size;
        neos->off_screen_size = memory;
    } else {
        neos->off_screen = 0;
        neos->off_screen_size = 0;
    }

    screen->driver = neos;

    LEAVE();
    return TRUE;
}

static Bool
neoInitScreen(ScreenPtr pScreen)
{
    ENTER();
    KdScreenPriv(pScreen);
    neoCardInfo(pScreenPriv);
    
    return neoc->backendCard.initScreen(pScreen);
    LEAVE();
}

static Bool
neoFinishInitScreen(ScreenPtr pScreen)
{
    KdScreenPriv(pScreen);
    neoCardInfo(pScreenPriv);
    
    return neoc->backendCard.finishInitScreen(pScreen);
}

static Bool
neoCreateResources(ScreenPtr pScreen)
{
    KdScreenPriv(pScreen);
    neoCardInfo(pScreenPriv);

    return neoc->backendCard.createRes(pScreen);
}

void
neoPreserve(KdCardInfo *card)
{
    NeoCardInfo *neoc = card->driver;
    neoc->backendCard.preserve(card);
}

CARD8
neoGetIndex(NeoCardInfo *nvidiac, CARD16 addr,  CARD8 index)
{
    outb(index, addr);
    
    return inb(addr+1);
}

void
neoSetIndex(NeoCardInfo *nvidiac, CARD16 addr,  CARD8 index, CARD8 val)
{
    outb(index, addr);
    outb(val, addr+1);
}

static void neoLock(NeoCardInfo *neoc){
    CARD8 cr11;
    neoSetIndex(neoc, 0x3ce,  0x09, 0x00);
    cr11 = neoGetIndex(neoc, 0x3d4, 0x11);
    neoSetIndex(neoc, 0x3d4, 0x11, cr11 | 0x80);
}

static void neoUnlock(NeoCardInfo *neoc){
    CARD8 cr11;
    cr11 = neoGetIndex(neoc, 0x3d4, 0x11);
    neoSetIndex(neoc, 0x3d4, 0x11, cr11 & 0x7F);
    neoSetIndex(neoc, 0x3ce,  0x09, 0x26);
}


Bool
neoMapReg(KdCardInfo *card, NeoCardInfo *neoc)
{
    ENTER();
    neoc->reg_base = card->attr.address[1] & 0xFFF80000;
    if(!neoc->reg_base) {
        return FALSE;
    }

    neoc->mmio = KdMapDevice(neoc->reg_base, NEO_REG_SIZE(card));
    if(!neoc->mmio) {
        return FALSE;
    }

    KdSetMappedMode(neoc->reg_base, NEO_REG_SIZE(card), KD_MAPPED_MODE_REGISTERS);

    // if you see the cursor sprite them MMIO is working

    *(((CARD32 *)neoc->mmio)+0x400) =(CARD32)8;
    //neoSetIndex(neoc, 0x3ce, 0x82,8);
    LEAVE();
    return TRUE;
}

void
neoUnmapReg(KdCardInfo *card, NeoCardInfo *neoc)
{
    ENTER();
    if(neoc->reg_base)
    {
        neoSetIndex(neoc, 0x3ce, 0x82,0);
        KdResetMappedMode(neoc->reg_base, NEO_REG_SIZE(card), KD_MAPPED_MODE_REGISTERS);
        KdUnmapDevice((void *)neoc->mmio, NEO_REG_SIZE(card));
        neoc->reg_base = 0;
    }
    LEAVE();
}

static void
neoSetMMIO(KdCardInfo *card, NeoCardInfo *neoc)
{
    if(!neoc->reg_base)
        neoMapReg(card, neoc);
        neoUnlock(neoc);
}

static void
neoResetMMIO(KdCardInfo *card, NeoCardInfo *neoc)
{
    neoUnmapReg(card, neoc);
    neoLock(neoc);
}


Bool
neoEnable(ScreenPtr pScreen)
{
    KdScreenPriv(pScreen);
    neoCardInfo(pScreenPriv);

    if(!neoc->backendCard.enable(pScreen)) {
        return FALSE;
    }

    if (neoc->chip->caps != CAP_NONE) {
        neoSetMMIO(pScreenPriv->card, neoc);
    }

    return TRUE;
}

void
neoDisable(ScreenPtr pScreen)
{
    KdScreenPriv(pScreen);
    neoCardInfo(pScreenPriv);

    if (neoc->chip->caps != CAP_NONE) {
        neoResetMMIO(pScreenPriv->card, neoc);
    }

    neoc->backendCard.disable(pScreen);
}

static void
neoGetColors(ScreenPtr pScreen, int fb, int n, xColorItem *pdefs)
{
    KdScreenPriv(pScreen);
    neoCardInfo(pScreenPriv);

    neoc->backendCard.getColors(pScreen, fb, n, pdefs);
}

static void
neoPutColors(ScreenPtr pScreen, int fb, int n, xColorItem *pdefs)
{
    KdScreenPriv(pScreen);
    neoCardInfo(pScreenPriv);

    neoc->backendCard.putColors(pScreen, fb, n, pdefs);
}

static Bool
neoDPMS(ScreenPtr pScreen, int mode)
{
    KdScreenPriv(pScreen);
    neoCardInfo(pScreenPriv);

    return neoc->backendCard.dpms(pScreen, mode);
}

static void
neoRestore(KdCardInfo *card)
{
    NeoCardInfo *neoc = card->driver;

    if (neoc->chip->caps != CAP_NONE) {
        neoResetMMIO(card, neoc);
    }
    neoc->backendCard.restore(card);
}

static void
neoScreenFini(KdScreenInfo *screen)
{
    NeoScreenInfo *neos =(NeoScreenInfo *) screen->driver;
    NeoCardInfo *neoc = screen->card->driver;

    neoc->backendCard.scrfini(screen);
    xfree(neos);
    screen->driver = 0;
}

static void
neoCardFini(KdCardInfo *card)
{
    NeoCardInfo *neoc = card->driver;

    if (neoc->chip->caps != CAP_NONE) {
        neoUnmapReg(card, neoc);
    }
    neoc->backendCard.cardfini(card);
}

#define neoCursorInit 0       // initCursor
#define neoCursorEnable 0     // enableCursor
#define neoCursorDisable 0    // disableCursor
#define neoCursorFini 0       // finiCursor */
#define neoRecolorCursor 0    // recolorCursor */

KdCardFuncs neoFuncs = {
    neoCardInit,              // cardinit
    neoScreenInit,            // scrinit
    neoInitScreen,            // initScreen
    neoFinishInitScreen,      // finishInitScreen
    neoCreateResources,       // createRes
    neoPreserve,              // preserve
    neoEnable,                // enable
    neoDPMS,                  // dpms
    neoDisable,               // disable
    neoRestore,               // restore
    neoScreenFini,            // scrfini
    neoCardFini,              // cardfini

    neoCursorInit,            // initCursor
    neoCursorEnable,          // enableCursor
    neoCursorDisable,         // disableCursor
    neoCursorFini,            // finiCursor
    neoRecolorCursor,         // recolorCursor

    neoDrawInit,              // initAccel
    neoDrawEnable,            // enableAccel
    neoDrawSync,              // syncAccel
    neoDrawDisable,           // disableAccel
    neoDrawFini,              // finiAccel

    neoGetColors,             // getColors
    neoPutColors,             // putColors
};