igsfb_ofbus.c revision 1.1 1 /* $NetBSD: igsfb_ofbus.c,v 1.1 2006/12/07 03:10:14 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 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*
31 * Integraphics Systems IGA 168x and CyberPro series.
32 * ofbus attachment for Valeriy E. Ushakov's igsfb driver
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: igsfb_ofbus.c,v 1.1 2006/12/07 03:10:14 macallan Exp $");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/device.h>
42 #include <sys/malloc.h>
43 #include <sys/buf.h>
44
45 #include <machine/bus.h>
46 #include <machine/intr.h>
47 #include <machine/ofw.h>
48
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/pcidevs.h>
51 #include <dev/isa/isavar.h>
52
53 #include <dev/wscons/wsdisplayvar.h>
54 #include <dev/wscons/wsconsio.h>
55 #include <dev/rasops/rasops.h>
56 #include <dev/wscons/wsdisplay_vconsvar.h>
57 #include <dev/ofw/openfirm.h>
58
59 #include <dev/ic/igsfbreg.h>
60 #include <dev/ic/igsfbvar.h>
61 #include "igsfb_ofbusvar.h"
62
63 static int igsfb_ofbus_is_console(int);
64
65 static int igsfb_ofbus_console = 0;
66 static int igsfb_ofbus_phandle = 0;
67
68
69
70 static int igsfb_ofbus_match(struct device *, struct cfdata *, void *);
71 static void igsfb_ofbus_attach(struct device *, struct device *, void *);
72 static int igsfb_setup_dc(struct igsfb_devconfig *);
73
74 CFATTACH_DECL(igsfb_ofbus, sizeof(struct igsfb_softc),
75 igsfb_ofbus_match, igsfb_ofbus_attach, NULL, NULL);
76
77 static const char *compat_strings[] = { "igs,cyperpro2010", 0 };
78
79 vaddr_t igsfb_mem_vaddr = 0, igsfb_mmio_vaddr = 0;
80 paddr_t igsfb_mem_paddr;
81 struct bus_space igsfb_memt, igsfb_iot;
82
83 int
84 igsfb_ofbus_cnattach(bus_space_tag_t iot, bus_space_tag_t memt)
85 {
86 struct igsfb_devconfig *dc;
87 int ret;
88 int chosen_phandle, igs_node;
89 int stdout_ihandle, stdout_phandle;
90 uint32_t regs[16];
91
92 stdout_phandle = 0;
93
94 /* first find out if there's a CyberPro at all in this machine */
95 if ((igs_node = OF_finddevice("/vlbus/display")) == -1)
96 return ENXIO;
97 if (of_compatible(igs_node, compat_strings) < 0)
98 return ENXIO;
99 /*
100 * now we know there's a CyberPro in this machine so map it into
101 * kernel space, even if it's not the console
102 */
103 if (OF_getprop(igs_node, "reg", regs, sizeof(regs)) <= 0)
104 return ENXIO;
105
106 igsfb_mem_paddr = be32toh(regs[13]);
107 /* 4MB VRAM */
108 igsfb_mem_vaddr = ofw_map(igsfb_mem_paddr, 0x00400000, 0);
109 /* MMIO registers */
110 igsfb_mmio_vaddr = ofw_map(igsfb_mem_paddr + IGS_MEM_MMIO_SELECT,
111 0x00100000, 0);
112
113 memcpy(&igsfb_memt, memt, sizeof(struct bus_space));
114 igsfb_memt.bs_cookie = (void *)igsfb_mem_vaddr;
115 memcpy(&igsfb_iot, memt, sizeof(struct bus_space));
116 igsfb_iot.bs_cookie = (void *)igsfb_mmio_vaddr;
117
118 /*
119 * check if the firmware output device is indeed the CyberPro
120 */
121 if ((chosen_phandle = OF_finddevice("/chosen")) == -1 ||
122 OF_getprop(chosen_phandle, "stdout", &stdout_ihandle,
123 sizeof(stdout_ihandle)) != sizeof(stdout_ihandle)) {
124 return ENXIO;
125 }
126 stdout_ihandle = of_decode_int((unsigned char *)&stdout_ihandle);
127 stdout_phandle = OF_instance_to_package(stdout_ihandle);
128
129 if (stdout_phandle != igs_node)
130 return ENXIO;
131
132 /* ok, now setup and attach the console */
133 dc = &igsfb_console_dc;
134 ret = igsfb_setup_dc(dc);
135 if (ret)
136 return ret;
137
138 ret = igsfb_cnattach_subr(dc);
139 if (ret)
140 return ret;
141
142 igsfb_ofbus_console = 1;
143 igsfb_ofbus_phandle = stdout_phandle;
144 return 0;
145 }
146
147 static int
148 igsfb_setup_dc(struct igsfb_devconfig *dc)
149 {
150 int ret;
151
152 dc->dc_id = PCI_PRODUCT_INTEGRAPHICS_CYBERPRO2010;
153 dc->dc_memt = &igsfb_memt;
154 dc->dc_memaddr = 0;
155 dc->dc_memsz= 0x00400000;
156 dc->dc_memflags = PCI_MAPREG_TYPE_MEM;
157
158 dc->dc_iot = &igsfb_iot;
159 dc->dc_iobase = 0;
160 dc->dc_ioflags = 0;
161
162 if (bus_space_map(dc->dc_iot,
163 dc->dc_iobase + IGS_REG_BASE, IGS_REG_SIZE,
164 dc->dc_ioflags,
165 &dc->dc_ioh) != 0)
166 {
167 printf("unable to map I/O registers\n");
168 return 1;
169 }
170 ret = igsfb_enable(dc->dc_iot, dc->dc_iobase, dc->dc_ioflags);
171 if (ret)
172 return ret;
173 return 0;
174 }
175
176 static int
177 igsfb_ofbus_is_console(int phandle)
178 {
179
180 return igsfb_ofbus_console && (phandle == igsfb_ofbus_phandle);
181 }
182
183
184 static int
185 igsfb_ofbus_match(struct device *parent, struct cfdata *match, void *aux)
186 {
187 struct ofbus_attach_args *oba = aux;
188
189 if (of_compatible(oba->oba_phandle, compat_strings) == -1)
190 return (0);
191 return 10; /* beat vga etc. */
192 }
193
194
195 static void
196 igsfb_ofbus_attach(struct device *parent, struct device *self, void *aux)
197 {
198 struct igsfb_softc *sc = (struct igsfb_softc *)self;
199 struct ofbus_attach_args *oba = aux;
200 uint32_t regs[16];
201 int isconsole, ret;
202
203 OF_getprop(oba->oba_phandle, "reg", regs, sizeof(regs));
204 isconsole = 0;
205 if (igsfb_ofbus_is_console(oba->oba_phandle)) {
206 sc->sc_dc = &igsfb_console_dc;
207 isconsole = 1;
208 } else {
209 sc->sc_dc = malloc(sizeof(struct igsfb_devconfig),
210 M_DEVBUF, M_NOWAIT | M_ZERO);
211 if (sc->sc_dc == NULL)
212 panic("unable to allocate igsfb_devconfig");
213 if (OF_getprop(oba->oba_phandle, "reg", regs, sizeof(regs)) <= 0) {
214 printf("Can't read 'reg' property\n");
215 return;
216 }
217 ret = igsfb_setup_dc(sc->sc_dc);
218 if (ret)
219 return;
220 }
221 printf(": IGS CyberPro 2010 at 0x%08x\n", (uint32_t)igsfb_mem_paddr);
222
223 igsfb_attach_subr(sc, isconsole);
224 }
225