First import
[xorg_rtime.git] / xorg-server-1.4 / hw / xfree86 / os-support / bsd / arm_video.c
blobb556563d3310eeda7b435b1ec526492de407cf3c
1 /*
2 * Copyright 1992 by Rich Murphey <Rich@Rice.edu>
3 * Copyright 1993 by David Wexelblat <dwex@goblin.org>
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the names of Rich Murphey and David Wexelblat
10 * not be used in advertising or publicity pertaining to distribution of
11 * the software without specific, written prior permission. Rich Murphey and
12 * David Wexelblat make no representations about the suitability of this
13 * software for any purpose. It is provided "as is" without express or
14 * implied warranty.
16 * RICH MURPHEY AND DAVID WEXELBLAT DISCLAIM ALL WARRANTIES WITH REGARD TO
17 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL RICH MURPHEY OR DAVID WEXELBLAT BE LIABLE FOR
19 * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 * The ARM32 code here carries the following copyright:
29 * Copyright 1997
30 * Digital Equipment Corporation. All rights reserved.
31 * This software is furnished under license and may be used and copied only in
32 * accordance with the following terms and conditions. Subject to these
33 * conditions, you may download, copy, install, use, modify and distribute
34 * this software in source and/or binary form. No title or ownership is
35 * transferred hereby.
37 * 1) Any source code used, modified or distributed must reproduce and retain
38 * this copyright notice and list of conditions as they appear in the
39 * source file.
41 * 2) No right is granted to use any trade name, trademark, or logo of Digital
42 * Equipment Corporation. Neither the "Digital Equipment Corporation"
43 * name nor any trademark or logo of Digital Equipment Corporation may be
44 * used to endorse or promote products derived from this software without
45 * the prior written permission of Digital Equipment Corporation.
47 * 3) This software is provided "AS-IS" and any express or implied warranties,
48 * including but not limited to, any implied warranties of merchantability,
49 * fitness for a particular purpose, or non-infringement are disclaimed.
50 * In no event shall DIGITAL be liable for any damages whatsoever, and in
51 * particular, DIGITAL shall not be liable for special, indirect,
52 * consequential, or incidental damages or damages for lost profits, loss
53 * of revenue or loss of use, whether such damages arise in contract,
54 * negligence, tort, under statute, in equity, at law or otherwise, even
55 * if advised of the possibility of such damage.
59 #ifdef HAVE_XORG_CONFIG_H
60 #include <xorg-config.h>
61 #endif
63 #include <X11/X.h>
64 #include "xf86.h"
65 #include "xf86Priv.h"
66 #include "xf86_OSlib.h"
67 #include "xf86OSpriv.h"
69 #ifdef __arm32__
70 #include "machine/devmap.h"
71 struct memAccess
73 int ioctl;
74 struct map_info memInfo;
75 pointer regionVirtBase;
76 Bool Checked;
77 Bool OK;
80 static pointer xf86MapInfoMap();
81 static void xf86MapInfoUnmap();
82 static struct memAccess *checkMapInfo();
83 extern int vgaPhysLinearBase;
85 /* A memAccess structure is needed for each possible region */
86 struct memAccess vgaMemInfo = { CONSOLE_GET_MEM_INFO, NULL, NULL,
87 FALSE, FALSE };
88 struct memAccess linearMemInfo = { CONSOLE_GET_LINEAR_INFO, NULL, NULL,
89 FALSE, FALSE };
90 struct memAccess ioMemInfo = { CONSOLE_GET_IO_INFO, NULL, NULL,
91 FALSE, FALSE };
92 #endif /* __arm32__ */
94 #if defined(__NetBSD__) && !defined(MAP_FILE)
95 #define MAP_FLAGS MAP_SHARED
96 #else
97 #define MAP_FLAGS (MAP_FILE | MAP_SHARED)
98 #endif
100 #ifndef MAP_FAILED
101 #define MAP_FAILED ((caddr_t)-1)
102 #endif
105 #define BUS_BASE 0L
106 #define BUS_BASE_BWX 0L
109 /***************************************************************************/
110 /* Video Memory Mapping section */
111 /***************************************************************************/
113 static Bool useDevMem = FALSE;
114 static int devMemFd = -1;
116 #define DEV_MEM "/dev/mem"
118 static pointer mapVidMem(int, unsigned long, unsigned long, int);
119 static void unmapVidMem(int, pointer, unsigned long);
122 * Check if /dev/mem can be mmap'd. If it can't print a warning when
123 * "warn" is TRUE.
125 static void
126 checkDevMem(Bool warn)
128 static Bool devMemChecked = FALSE;
129 int fd;
130 pointer base;
132 if (devMemChecked)
133 return;
134 devMemChecked = TRUE;
136 if ((fd = open(DEV_MEM, O_RDWR)) >= 0)
138 /* Try to map a page at the VGA address */
139 base = mmap((caddr_t)0, 4096, PROT_READ | PROT_WRITE,
140 MAP_FLAGS, fd, (off_t)0xA0000 + BUS_BASE);
142 if (base != MAP_FAILED)
144 munmap((caddr_t)base, 4096);
145 devMemFd = fd;
146 useDevMem = TRUE;
147 return;
148 } else {
149 /* This should not happen */
150 if (warn)
152 xf86Msg(X_WARNING, "checkDevMem: failed to mmap %s (%s)\n",
153 DEV_MEM, strerror(errno));
155 useDevMem = FALSE;
156 return;
159 if (warn)
161 xf86Msg(X_WARNING, "checkDevMem: failed to open %s (%s)\n",
162 DEV_MEM, strerror(errno));
164 useDevMem = FALSE;
165 return;
168 void
169 xf86OSInitVidMem(VidMemInfoPtr pVidMem)
172 checkDevMem(TRUE);
173 pVidMem->linearSupported = useDevMem;
174 pVidMem->mapMem = armMapVidMem;
175 pVidMem->unmapVidMem = armUnmapVidMem;
177 pVidMem->initialised = TRUE;
180 static pointer
181 mapVidMem(int ScreenNum, unsigned long Base, unsigned long Size, int flags)
183 pointer base;
185 checkDevMem(FALSE);
187 if (useDevMem)
189 if (devMemFd < 0)
191 FatalError("xf86MapVidMem: failed to open %s (%s)\n",
192 DEV_MEM, strerror(errno));
194 base = mmap((caddr_t)0, Size,
195 (flags & VIDMEM_READONLY) ?
196 PROT_READ : (PROT_READ | PROT_WRITE),
197 MAP_FLAGS, devMemFd, (off_t)Base + BUS_BASE_BWX);
198 if (base == MAP_FAILED)
200 FatalError("%s: could not mmap %s [s=%x,a=%x] (%s)\n",
201 "xf86MapVidMem", DEV_MEM, Size, Base,
202 strerror(errno));
204 return(base);
207 /* else, mmap /dev/vga */
208 if ((unsigned long)Base < 0xA0000 || (unsigned long)Base >= 0xC0000)
210 FatalError("%s: Address 0x%x outside allowable range\n",
211 "xf86MapVidMem", Base);
213 base = mmap(0, Size,
214 (flags & VIDMEM_READONLY) ?
215 PROT_READ : (PROT_READ | PROT_WRITE),
216 MAP_FLAGS, xf86Info.screenFd,
217 (unsigned long)Base - 0xA0000);
218 if (base == MAP_FAILED)
220 FatalError("xf86MapVidMem: Could not mmap /dev/vga (%s)\n",
221 strerror(errno));
223 return(base);
226 static void
227 unmapVidMem(int ScreenNum, pointer Base, unsigned long Size)
229 munmap((caddr_t)Base, Size);
233 * Read BIOS via mmap()ing DEV_MEM
236 _X_EXPORT int
237 xf86ReadBIOS(unsigned long Base, unsigned long Offset, unsigned char *Buf,
238 int Len)
240 unsigned char *ptr;
241 int psize;
242 int mlen;
244 checkDevMem(TRUE);
245 if (devMemFd == -1) {
246 return(-1);
249 psize = xf86getpagesize();
250 Offset += Base & (psize - 1);
251 Base &= ~(psize - 1);
252 mlen = (Offset + Len + psize - 1) & ~(psize - 1);
253 ptr = (unsigned char *)mmap((caddr_t)0, mlen, PROT_READ,
254 MAP_SHARED, devMemFd, (off_t)Base+BUS_BASE);
255 if ((long)ptr == -1)
257 xf86Msg(X_WARNING,
258 "xf86ReadBIOS: %s mmap[s=%x,a=%x,o=%x] failed (%s)\n",
259 DEV_MEM, Len, Base, Offset, strerror(errno));
260 return(-1);
262 #ifdef DEBUG
263 ErrorF("xf86ReadBIOS: BIOS at 0x%08x has signature 0x%04x\n",
264 Base, ptr[0] | (ptr[1] << 8));
265 #endif
266 (void)memcpy(Buf, (void *)(ptr + Offset), Len);
267 (void)munmap((caddr_t)ptr, mlen);
268 #ifdef DEBUG
269 xf86MsgVerb(X_INFO, 3, "xf86ReadBIOS(%x, %x, Buf, %x)"
270 "-> %02x %02x %02x %02x...\n",
271 Base, Offset, Len, Buf[0], Buf[1], Buf[2], Buf[3]);
272 #endif
273 return(Len);
277 /* XXX This needs to be updated for the ND */
280 ** Find out whether the console driver provides memory mapping information
281 ** for the specified region and return the map_info pointer. Print a warning if required.
283 static struct memAccess *
284 checkMapInfo(Bool warn, int Region)
286 struct memAccess *memAccP;
288 switch (Region)
290 case VGA_REGION:
291 memAccP = &vgaMemInfo;
292 break;
294 case LINEAR_REGION:
295 memAccP = &linearMemInfo;
296 break;
298 case MMIO_REGION:
299 memAccP = &ioMemInfo;
300 break;
302 default:
303 return NULL;
304 break;
307 if(!memAccP->Checked)
309 if(ioctl(xf86Info.screenFd, memAccP->ioctl, &(memAccP->memInfo)) == -1)
311 if(warn)
313 xf86Msg(X_WARNING,
314 "checkMapInfo: failed to get map info for region %d\n\t(%s)\n",
315 Region, strerror(errno));
318 else
320 if(memAccP->memInfo.u.map_info_mmap.map_offset
321 != MAP_INFO_UNKNOWN)
322 memAccP->OK = TRUE;
324 memAccP->Checked = TRUE;
326 if (memAccP->OK)
328 return memAccP;
330 else
332 return NULL;
336 static pointer
337 xf86MapInfoMap(struct memAccess *memInfoP, pointer Base, unsigned long Size)
339 struct map_info *mapInfoP = &(memInfoP->memInfo);
341 if (mapInfoP->u.map_info_mmap.map_size == MAP_INFO_UNKNOWN)
343 Size = (unsigned long)Base + Size;
345 else
347 Size = mapInfoP->u.map_info_mmap.map_size;
350 switch(mapInfoP->method)
352 case MAP_MMAP:
353 /* Need to remap if size is unknown because we may not have
354 mapped the whole region initially */
355 if(memInfoP->regionVirtBase == NULL ||
356 mapInfoP->u.map_info_mmap.map_size == MAP_INFO_UNKNOWN)
358 if((memInfoP->regionVirtBase =
359 mmap((caddr_t)0,
360 Size,
361 PROT_READ | PROT_WRITE,
362 MAP_SHARED,
363 xf86Info.screenFd,
364 (unsigned long)mapInfoP->u.map_info_mmap.map_offset))
365 == (pointer)-1)
367 FatalError("xf86MapInfoMap: Failed to map memory at 0x%x\n\t%s\n",
368 mapInfoP->u.map_info_mmap.map_offset, strerror(errno));
370 if(mapInfoP->u.map_info_mmap.internal_offset > 0)
371 memInfoP->regionVirtBase +=
372 mapInfoP->u.map_info_mmap.internal_offset;
374 break;
376 default:
377 FatalError("xf86MapInfoMap: Unsuported mapping method\n");
378 break;
381 return (pointer)((int)memInfoP->regionVirtBase + (int)Base);
384 static void
385 xf86MapInfoUnmap(struct memAccess *memInfoP, unsigned long Size)
387 struct map_info *mapInfoP = &(memInfoP->memInfo);
389 switch(mapInfoP->method)
391 case MAP_MMAP:
392 if(memInfoP->regionVirtBase != NULL)
394 if(mapInfoP->u.map_info_mmap.map_size != MAP_INFO_UNKNOWN)
395 Size = mapInfoP->u.map_info_mmap.map_size;
396 munmap((caddr_t)memInfoP->regionVirtBase, Size);
397 memInfoP->regionVirtBase = NULL;
399 break;
400 default:
401 FatalError("xf86MapInfoMap: Unsuported mapping method\n");
402 break;
406 static pointer
407 armMapVidMem(int ScreenNum, unsigned long Base, unsigned long Size, int flags)
409 struct memAccess *memInfoP;
411 if((memInfoP = checkMapInfo(FALSE, Region)) != NULL)
414 ** xf86 passes in a physical address offset from the start
415 ** of physical memory, but xf86MapInfoMap expects an
416 ** offset from the start of the specified region - it gets
417 ** the physical address of the region from the display driver.
419 switch(Region)
421 case LINEAR_REGION:
422 if (vgaPhysLinearBase)
424 Base -= vgaPhysLinearBase;
426 break;
427 case VGA_REGION:
428 Base -= 0xA0000;
429 break;
432 base = xf86MapInfoMap(memInfoP, Base, Size);
433 return (base);
435 return mapVidMem(ScreenNum, Base, Size, flags);
438 static void
439 armUnmapVidMem(int ScreenNum, pointer Base, unsigned long Size)
441 struct memAccess *memInfoP;
443 if((memInfoP = checkMapInfo(FALSE, Region)) != NULL)
445 xf86MapInfoUnmap(memInfoP, Base, Size);
447 unmapVidMem(ScreenNum, Base, Size);
450 #ifdef USE_DEV_IO
451 static int IoFd = -1;
453 _X_EXPORT Bool
454 xf86EnableIO()
456 if (IoFd >= 0)
457 return TRUE;
459 if ((IoFd = open("/dev/io", O_RDWR)) == -1)
461 xf86Msg(X_WARNING,"xf86EnableIO: "
462 "Failed to open /dev/io for extended I/O\n");
463 return FALSE;
465 return TRUE;
468 _X_EXPORT void
469 xf86DisableIO()
471 if (IoFd < 0)
472 return;
474 close(IoFd);
475 IoFd = -1;
476 return;
479 #endif
481 #if defined(USE_ARC_MMAP) || defined(__arm32__)
483 Bool
484 xf86EnableIO()
486 int fd;
487 pointer base;
489 if (ExtendedEnabled)
490 return TRUE;
492 if ((fd = open("/dev/ttyC0", O_RDWR)) >= 0) {
493 /* Try to map a page at the pccons I/O space */
494 base = (pointer)mmap((caddr_t)0, 65536, PROT_READ | PROT_WRITE,
495 MAP_FLAGS, fd, (off_t)0x0000);
497 if (base != (pointer)-1) {
498 IOPortBase = base;
500 else {
501 xf86Msg(X_WARNING,"EnableIO: failed to mmap %s (%s)\n",
502 "/dev/ttyC0", strerror(errno));
503 return FALSE;
506 else {
507 xf86Msg("EnableIO: failed to open %s (%s)\n",
508 "/dev/ttyC0", strerror(errno));
509 return FALSE;
512 ExtendedEnabled = TRUE;
514 return TRUE;
517 void
518 xf86DisableIO()
520 return;
523 #endif /* USE_ARC_MMAP */
526 /***************************************************************************/
527 /* Interrupt Handling section */
528 /***************************************************************************/
530 _X_EXPORT Bool
531 xf86DisableInterrupts()
534 return(TRUE);
537 _X_EXPORT void
538 xf86EnableInterrupts()
541 return;
546 #if 0
548 * XXX This is here for reference. It needs to be handled differently for the
549 * ND.
551 #if defined(USE_ARC_MMAP) || defined(__arm32__)
553 #ifdef USE_ARM32_MMAP
554 #define DEV_MEM_IOBASE 0x43000000
555 #endif
557 static Bool ScreenEnabled[MAXSCREENS];
558 static Bool ExtendedEnabled = FALSE;
559 static Bool InitDone = FALSE;
561 Bool
562 xf86EnableIOPorts(ScreenNum)
563 int ScreenNum;
565 int i;
566 int fd;
567 pointer base;
569 #ifdef __arm32__
570 struct memAccess *memInfoP;
571 int *Size;
572 #endif
574 ScreenEnabled[ScreenNum] = TRUE;
576 if (ExtendedEnabled)
577 return TRUE;
579 #ifdef USE_ARC_MMAP
580 if ((fd = open("/dev/ttyC0", O_RDWR)) >= 0) {
581 /* Try to map a page at the pccons I/O space */
582 base = (pointer)mmap((caddr_t)0, 65536, PROT_READ | PROT_WRITE,
583 MAP_FLAGS, fd, (off_t)0x0000);
585 if (base != (pointer)-1) {
586 IOPortBase = base;
588 else {
589 xf86Msg(X_ERROR,
590 "EnableIOPorts: failed to mmap %s (%s)\n",
591 "/dev/ttyC0", strerror(errno));
594 else {
595 xf86Msg(X_ERROR, "EnableIOPorts: failed to open %s (%s)\n",
596 "/dev/ttyC0", strerror(errno));
598 #endif
600 #ifdef __arm32__
601 IOPortBase = (unsigned int)-1;
603 if((memInfoP = checkMapInfo(TRUE, MMIO_REGION)) != NULL)
606 * xf86MapInfoMap maps an offset from the start of video IO
607 * space (e.g. 0x3B0), but IOPortBase is expected to map to
608 * physical address 0x000, so subtract the start of video I/O
609 * space from the result. This is safe for now becase we
610 * actually mmap the start of the page, then the start of video
611 * I/O space is added as an internal offset.
613 IOPortBase = (unsigned int)xf86MapInfoMap(memInfoP,
614 (caddr_t)0x0, 0L)
615 - memInfoP->memInfo.u.map_info_mmap.internal_offset;
616 ExtendedEnabled = TRUE;
617 return TRUE;
619 #ifdef USE_ARM32_MMAP
620 checkDevMem(TRUE);
622 if (devMemFd >= 0 && useDevMem)
624 base = (pointer)mmap((caddr_t)0, 0x400, PROT_READ | PROT_WRITE,
625 MAP_FLAGS, devMemFd, (off_t)DEV_MEM_IOBASE);
627 if (base != (pointer)-1)
628 IOPortBase = (unsigned int)base;
631 if (IOPortBase == (unsigned int)-1)
633 xf86Msg(X_WARNING,"xf86EnableIOPorts: failed to open mem device or map IO base. \n\
634 Make sure you have the Aperture Driver installed, or a kernel built with the INSECURE option\n");
635 return FALSE;
637 #else
638 /* We don't have the IOBASE, so we can't map the address */
639 xf86Msg(X_WARNING,"xf86EnableIOPorts: failed to open mem device or map IO base. \n\
640 Try building the server with USE_ARM32_MMAP defined\n");
641 return FALSE;
642 #endif
643 #endif
645 ExtendedEnabled = TRUE;
647 return TRUE;
650 void
651 xf86DisableIOPorts(ScreenNum)
652 int ScreenNum;
654 int i;
655 #ifdef __arm32__
656 struct memAccess *memInfoP;
657 #endif
659 ScreenEnabled[ScreenNum] = FALSE;
661 #ifdef __arm32__
662 if((memInfoP = checkMapInfo(FALSE, MMIO_REGION)) != NULL)
664 xf86MapInfoUnmap(memInfoP, 0);
666 #endif
668 #ifdef USE_ARM32_MMAP
669 if (!ExtendedEnabled)
670 return;
672 for (i = 0; i < MAXSCREENS; i++)
673 if (ScreenEnabled[i])
674 return;
676 munmap((caddr_t)IOPortBase, 0x400);
677 IOPortBase = (unsigned int)-1;
678 ExtendedEnabled = FALSE;
679 #endif
681 return;
684 #endif /* USE_ARC_MMAP || USE_ARM32_MMAP */
685 #endif