1/*
2 * Copyright © 2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 *    Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <stdarg.h>
32#include <pciaccess.h>
33#include <err.h>
34#include <unistd.h>
35
36#include "reg_dumper.h"
37#include "../i810_reg.h"
38
39struct idle_flags {
40    uint32_t instdone_flag;
41    char *name;
42    unsigned int count;
43};
44
45int main(int argc, char **argv)
46{
47    struct pci_device *dev;
48    I830Rec i830;
49    I830Ptr pI830 = &i830;
50    ScrnInfoRec scrn;
51    int err, mmio_bar;
52    void *mmio;
53    int i;
54
55    err = pci_system_init();
56    if (err != 0) {
57	fprintf(stderr, "Couldn't initialize PCI system: %s\n", strerror(err));
58	exit(1);
59    }
60
61    /* Grab the graphics card */
62    dev = pci_device_find_by_slot(0, 0, 2, 0);
63    if (dev == NULL)
64	errx(1, "Couldn't find graphics card");
65
66    err = pci_device_probe(dev);
67    if (err != 0) {
68	fprintf(stderr, "Couldn't probe graphics card: %s\n", strerror(err));
69	exit(1);
70    }
71
72    if (dev->vendor_id != 0x8086)
73	errx(1, "Graphics card is non-intel");
74
75    i830.PciInfo = dev;
76
77    mmio_bar = IS_I9XX((&i830)) ? 0 : 1;
78
79    err = pci_device_map_range (dev,
80				dev->regions[mmio_bar].base_addr,
81				dev->regions[mmio_bar].size,
82				PCI_DEV_MAP_FLAG_WRITABLE,
83				&mmio);
84
85    if (err != 0) {
86	fprintf(stderr, "Couldn't map MMIO region: %s\n", strerror(err));
87	exit(1);
88    }
89    i830.mmio = mmio;
90
91    scrn.scrnIndex = 0;
92    scrn.pI830 = &i830;
93
94    OUTREG(SDVOB, (0x0 << 10));
95    OUTREG(SDVOC, (0x0 << 10));
96
97    OUTREG(PORT_HOTPLUG_EN,
98	   (1 << 29) |
99	   (1 << 28) |
100	   (1 << 27) |
101	   SDVOB_HOTPLUG_INT_EN |
102	   SDVOC_HOTPLUG_INT_EN |
103	   (1 << 24) |
104	   CRT_HOTPLUG_INT_EN |
105	   TV_HOTPLUG_INT_EN |
106	   CRT_HOTPLUG_INT_EN);
107
108    for (i = 0;; i++) {
109	OUTREG(PORT_HOTPLUG_STAT,
110	       (1 << 20) |
111	       (1 << 19) |
112	       (1 << 18) |
113	       (1 << 17) |
114	       CRT_HOTPLUG_INT_STATUS |
115	       TV_HOTPLUG_INT_STATUS |
116	       SDVOC_HOTPLUG_INT_STATUS |
117	       SDVOB_HOTPLUG_INT_STATUS);
118	INREG(PORT_HOTPLUG_STAT);
119
120	usleep(500 * 1000);
121
122	printf("%5d: 0x%08x\n", i, INREG(PORT_HOTPLUG_STAT));
123	sleep(1);
124    }
125
126    return 0;
127}
128
129void xf86DrvMsg(int scrnIndex, int severity, const char *format, ...)
130{
131    va_list va;
132
133    switch (severity) {
134    case X_INFO:
135	printf("(II): ");
136	break;
137    case X_WARNING:
138	printf("(WW): ");
139	break;
140    case X_ERROR:
141	printf("(EE): ");
142	break;
143    }
144
145    va_start(va, format);
146    vprintf(format, va);
147    va_end(va);
148}
149