summaryrefslogtreecommitdiff
path: root/src/rdc_tool.c
blob: 4ba363a21eb4cb91b089c67ef66c845e02d52ea8 (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
/* 
 * Copyright (C) 2009 RDC Semiconductor Co.,Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * For technical support : 
 *     <jason.lin@rdc.com.tw>
 */
 
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>

#include "xf86.h"
#include "xf86_OSproc.h"
#include "xf86Resources.h"
#include "xf86RAC.h"
#include "xf86cmap.h"
#include "compiler.h"
#include "mibstore.h"
#include "vgaHW.h"
#include "mipointer.h"
#include "micmap.h"

#include "fb.h"
#include "regionstr.h"
#include "xf86xv.h"
#include <X11/extensions/Xv.h>
#include "vbe.h"

#include "xf86PciInfo.h"
#include "xf86Pci.h"

/* framebuffer offscreen manager */
#include "xf86fbman.h"

/* include xaa includes */
#include "xaa.h"
#include "xaarop.h"

/* H/W cursor support */
#include "xf86Cursor.h"

/* Driver specific headers */
#include "rdc.h"

/* Prototype type declaration*/
Bool RDCMapMem(ScrnInfoPtr pScrn);
Bool RDCUnmapMem(ScrnInfoPtr pScrn);
Bool RDCMapMMIO(ScrnInfoPtr pScrn);
void RDCUnmapMMIO(ScrnInfoPtr pScrn);
Bool RDCMapVBIOS(ScrnInfoPtr pScrn);
Bool RDCUnmapVBIOS(ScrnInfoPtr pScrn);


// map, unmap frame buffer
Bool
RDCMapMem(ScrnInfoPtr pScrn)
{
    RDCRecPtr pRDC = RDCPTR(pScrn);
    
#if XSERVER_LIBPCIACCESS
    struct pci_device *const device = pRDC->PciInfo;
    int err;
    
    err = pci_device_map_range(device, pRDC->FBPhysAddr, pRDC->FbMapSize, 
                                   PCI_DEV_MAP_FLAG_WRITABLE | PCI_DEV_MAP_FLAG_WRITE_COMBINE,
                                   (void **) &pRDC->FBVirtualAddr);
    
    if (err)
    {
        xf86DrvMsg (pScrn->scrnIndex, X_ERROR,
        "Unable to map frame buffer BAR. %s (%d)\n",
        strerror (err), err);
        return FALSE;
    }
#else
    pRDC->FBVirtualAddr = xf86MapPciMem(pScrn->scrnIndex, VIDMEM_FRAMEBUFFER,
                                        pRDC->PciTag,
                                        pRDC->FBPhysAddr, pRDC->FbMapSize);
#endif

    if (!pRDC->FBVirtualAddr)
        return FALSE;

    return TRUE;
}

Bool
RDCUnmapMem(ScrnInfoPtr pScrn)
{
    RDCRecPtr pRDC = RDCPTR(pScrn);

#if XSERVER_LIBPCIACCESS
       pci_device_unmap_range (pRDC->PciInfo, pRDC->FBVirtualAddr, pRDC->FbMapSize);
#else 
    xf86UnMapVidMem(pScrn->scrnIndex, (pointer) pRDC->FBVirtualAddr,
                    pRDC->FbMapSize);
#endif

    pRDC->FBVirtualAddr = 0;
   
    return TRUE;
}

// map, ummap MMIO
Bool
RDCMapMMIO(ScrnInfoPtr pScrn)
{
   int mmioFlags;
    RDCRecPtr pRDC = RDCPTR(pScrn);

#if !defined(__alpha__)
    mmioFlags = VIDMEM_MMIO | VIDMEM_READSIDEEFFECT;
#else
    mmioFlags = VIDMEM_MMIO | VIDMEM_READSIDEEFFECT | VIDMEM_SPARSE;
#endif

#if XSERVER_LIBPCIACCESS
    struct pci_device *const device = pRDC->PciInfo;
        
    pci_device_map_range(device, pRDC->MMIOPhysAddr, pRDC->MMIOMapSize,
                         PCI_DEV_MAP_FLAG_WRITABLE | PCI_DEV_MAP_FLAG_WRITE_COMBINE,
                         (void **) &pRDC->MMIOVirtualAddr);
#else
    pRDC->MMIOVirtualAddr = xf86MapPciMem(pScrn->scrnIndex, mmioFlags,
                                          pRDC->PciTag,
                                          pRDC->MMIOPhysAddr, pRDC->MMIOMapSize);
#endif

    if (!pRDC->MMIOVirtualAddr)
        return FALSE;

    return TRUE;
}

void
RDCUnmapMMIO(ScrnInfoPtr pScrn)
{
    RDCRecPtr pRDC = RDCPTR(pScrn);

#if XSERVER_LIBPCIACCESS
        pci_device_unmap_range (pRDC->PciInfo, pRDC->MMIOVirtualAddr, pRDC->MMIOMapSize);
#else
    xf86UnMapVidMem(pScrn->scrnIndex, (pointer) pRDC->MMIOVirtualAddr,
                    pRDC->MMIOMapSize);
#endif

    pRDC->MMIOVirtualAddr = 0;
   
}

// map, unmap VBIOS
Bool RDCMapVBIOS(ScrnInfoPtr pScrn)
{
    RDCRecPtr   pRDC = RDCPTR(pScrn);
    FILE        *fpVBIOS;
    int         i;

    xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, ErrorLevel, "==Enter RDCMapVBIOS()==\n");
    pRDC->ulROMType = 0;

    // Map C000
    if (pRDC->ulROMType == 0)
    {
        pRDC->BIOSVirtualAddr = xf86MapVidMem(pScrn->scrnIndex, VIDMEM_READONLY, BIOS_ROM_PHY_BASE, BIOS_ROM_SIZE);
        if (pRDC->BIOSVirtualAddr)
        {
            xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, 0, "pRDC->BIOSVirtualAddr = 0x%08x\n", pRDC->BIOSVirtualAddr);
            pRDC->ulROMType = 1;
        }
        else
        {
            xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, InfoLevel, "==BIOS ROM not found()==\n");
        }
    }

    // read ROM file
    if (pRDC->ulROMType == 0)
    {
        // open BIOS ROM file
        fpVBIOS = fopen(BIOS_ROM_PATH_FILE, "r");
        if (!fpVBIOS)
        {
            xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, InfoLevel, "BIOS ROM file \"/root/RDCVBIOS.ROM\" not found()==\n");
        }
    
        // memory allocate
        pRDC->BIOSVirtualAddr = xnfalloc(BIOS_ROM_SIZE);
        if (!pRDC->BIOSVirtualAddr)
        {
            xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, InfoLevel, "Read BIOS ROM file: Mem Allocate Fail!!==\n");
            fclose (fpVBIOS);
        }

        // set ROM type
        if (fpVBIOS && pRDC->BIOSVirtualAddr)
        {
            pRDC->ulROMType = 2;
            for (i = 0; i < BIOS_ROM_SIZE; i++)
            {
                fscanf(fpVBIOS, "%c", pRDC->BIOSVirtualAddr+i);
            }
            fclose (fpVBIOS);
        }
    }

    if (pRDC->ulROMType == 0)
    {
        xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, ErrorLevel, "==Exit1 RDCMapVBIOS()== No VBIOS\n");
        return false; // no VBIOS and ROM file
    }

    xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, InfoLevel, "pRDC->ulROMType = %d\n", pRDC->ulROMType);

    if ((*(USHORT*)pRDC->BIOSVirtualAddr == 0xAA55) && (*(USHORT*)(pRDC->BIOSVirtualAddr+0x40) == PCI_VENDOR_RDC))
    {
        xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, ErrorLevel, "==Exit RDCMapVBIOS()== return TRUE\n");
        return TRUE;
    }
    else
    {
        RDCUnmapVBIOS(pScrn);

        xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, ErrorLevel, "==Exit2 RDCMapVBIOS()== Not RDC VBIOS\n");
        return false; // not RDC VGA BIOS
    }
}

Bool RDCUnmapVBIOS(ScrnInfoPtr pScrn)
{
    RDCRecPtr pRDC = RDCPTR(pScrn);

    if (pRDC->ulROMType == 1)
    {
        xf86UnMapVidMem(pScrn->scrnIndex, (pointer) pRDC->BIOSVirtualAddr, BIOS_ROM_SIZE);
    }
    else if (pRDC->ulROMType == 2)
    {
        xfree(pRDC->BIOSVirtualAddr);
    }
    pRDC->BIOSVirtualAddr = 0;
   
    return TRUE;
}