chipsfb.c revision 1.27 1 /* $NetBSD: chipsfb.c,v 1.27 2011/03/23 04:02:43 macallan Exp $ */
2
3 /*
4 * Copyright (c) 2006 Michael Lorenz
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /*
29 * A console driver for Chips & Technologies 65550 graphics controllers
30 * tested on macppc only so far
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: chipsfb.c,v 1.27 2011/03/23 04:02:43 macallan Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/device.h>
40 #include <sys/kauth.h>
41
42 #include <dev/pci/pcivar.h>
43 #include <dev/pci/pcireg.h>
44 #include <dev/pci/pcidevs.h>
45 #include <dev/pci/pciio.h>
46 #include <dev/pci/wsdisplay_pci.h>
47
48
49 #include <dev/ic/ct65550reg.h>
50 #include <dev/ic/ct65550var.h>
51
52 #include "opt_wsemul.h"
53 #include "opt_chipsfb.h"
54
55 struct chipsfb_pci_softc {
56 struct chipsfb_softc sc_chips;
57 pci_chipset_tag_t sc_pc;
58 pcitag_t sc_pcitag;
59 };
60
61 static int chipsfb_pci_match(device_t, cfdata_t, void *);
62 static void chipsfb_pci_attach(device_t, device_t, void *);
63 static int chipsfb_pci_ioctl(void *, void *, u_long, void *, int,
64 struct lwp *);
65
66 CFATTACH_DECL_NEW(chipsfb_pci, sizeof(struct chipsfb_pci_softc),
67 chipsfb_pci_match, chipsfb_pci_attach, NULL, NULL);
68
69 static int
70 chipsfb_pci_match(device_t parent, cfdata_t match, void *aux)
71 {
72 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
73
74 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
75 PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
76 return 0;
77 if ((PCI_VENDOR(pa->pa_id) == PCI_VENDOR_CHIPS) &&
78 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CHIPS_65550))
79 return 100;
80 if ((PCI_VENDOR(pa->pa_id) == PCI_VENDOR_CHIPS) &&
81 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CHIPS_65554))
82 return 100;
83 return 0;
84 }
85
86 static void
87 chipsfb_pci_attach(device_t parent, device_t self, void *aux)
88 {
89 struct chipsfb_pci_softc *scp = device_private(self);
90 struct chipsfb_softc *sc = &scp->sc_chips;
91 struct pci_attach_args *pa = aux;
92 char devinfo[256];
93 pcireg_t screg;
94
95 scp->sc_pc = pa->pa_pc;
96 scp->sc_pcitag = pa->pa_tag;
97 sc->sc_dev = self;
98
99 screg = pci_conf_read(scp->sc_pc, scp->sc_pcitag,
100 PCI_COMMAND_STATUS_REG);
101 screg |= PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
102 pci_conf_write(scp->sc_pc, scp->sc_pcitag, PCI_COMMAND_STATUS_REG,
103 screg);
104
105 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
106 aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
107 PCI_REVISION(pa->pa_class));
108 #ifdef CHIPSFB_DEBUG
109 printf(prop_dictionary_externalize(dict));
110 #endif
111
112 sc->sc_memt = pa->pa_memt;
113 sc->sc_iot = pa->pa_iot;
114 sc->sc_ioctl = chipsfb_pci_ioctl;
115 sc->sc_mmap = NULL;
116
117 /* the framebuffer */
118 sc->sc_fb = (pci_conf_read(scp->sc_pc, scp->sc_pcitag, PCI_BAR0) &
119 ~PCI_MAPREG_MEM_TYPE_MASK);
120 sc->sc_fbsize = 0x01000000; /* 16MB aperture */
121
122 if (bus_space_map(sc->sc_memt, sc->sc_fb, 0x400000,
123 BUS_SPACE_MAP_LINEAR, &sc->sc_fbh)) {
124 aprint_error_dev(sc->sc_dev,
125 "failed to map the frame buffer.\n");
126 }
127
128 if (bus_space_map(sc->sc_memt, sc->sc_fb + CT_OFF_BITBLT, 0x20000,
129 BUS_SPACE_MAP_LINEAR, &sc->sc_mmregh)) {
130 aprint_error_dev(sc->sc_dev,
131 "failed to map MMIO registers.\n");
132 }
133
134 /* IO-mapped registers */
135 if (bus_space_map(sc->sc_iot, 0x0, 0x400, 0, &sc->sc_ioregh) != 0) {
136 aprint_error_dev(sc->sc_dev, "failed to map IO registers.\n");
137 }
138
139 sc->memsize = chipsfb_probe_vram(sc);
140
141 chipsfb_do_attach(sc);
142 }
143
144 static int
145 chipsfb_pci_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
146 struct lwp *l)
147 {
148 struct vcons_data *vd = v;
149 struct chipsfb_softc *sc = vd->cookie;
150 struct chipsfb_pci_softc *scp = vd->cookie;
151
152 switch (cmd) {
153 /* PCI config read/write passthrough. */
154 case PCI_IOC_CFGREAD:
155 case PCI_IOC_CFGWRITE:
156 return pci_devioctl(scp->sc_pc, scp->sc_pcitag,
157 cmd, data, flag, l);
158
159 case WSDISPLAYIO_GET_BUSID:
160 return wsdisplayio_busid_pci(sc->sc_dev, scp->sc_pc,
161 scp->sc_pcitag, data);
162 }
163 return EPASSTHROUGH;
164 }
165