xref: /netbsd/sys/arch/shark/ofw/igsfb_ofbus.c (revision 6550d01e)
1 /*	$NetBSD: igsfb_ofbus.c,v 1.10 2009/11/11 17:05:11 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  * Integraphics Systems IGA 168x and CyberPro series.
30  * ofbus attachment for Valeriy E. Ushakov's igsfb driver
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: igsfb_ofbus.c,v 1.10 2009/11/11 17:05:11 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/malloc.h>
41 #include <sys/buf.h>
42 
43 #include <machine/bus.h>
44 #include <machine/intr.h>
45 #include <machine/ofw.h>
46 
47 #include <dev/isa/isavar.h>
48 
49 #include <dev/wscons/wsdisplayvar.h>
50 #include <dev/wscons/wsconsio.h>
51 #include <dev/rasops/rasops.h>
52 #include <dev/wscons/wsdisplay_vconsvar.h>
53 #include <dev/ofw/openfirm.h>
54 
55 #include <dev/ic/igsfbreg.h>
56 #include <dev/ic/igsfbvar.h>
57 #include <shark/ofw/igsfb_ofbusvar.h>
58 
59 static int	igsfb_ofbus_is_console(int);
60 
61 static int igsfb_ofbus_console = 0;
62 static int igsfb_ofbus_phandle = 0;
63 
64 
65 
66 static int	igsfb_ofbus_match(struct device *, struct cfdata *, void *);
67 static void	igsfb_ofbus_attach(struct device *, struct device *, void *);
68 static int	igsfb_setup_dc(struct igsfb_devconfig *);
69 static paddr_t	igsfb_ofbus_mmap(void *, void *, off_t, int);
70 
71 CFATTACH_DECL(igsfb_ofbus, sizeof(struct igsfb_softc),
72     igsfb_ofbus_match, igsfb_ofbus_attach, NULL, NULL);
73 
74 static const char const *compat_strings[] = { "igs,cyperpro2010", NULL };
75 
76 vaddr_t igsfb_mem_vaddr = 0, igsfb_mmio_vaddr = 0;
77 paddr_t igsfb_mem_paddr;
78 extern paddr_t isa_io_physaddr;
79 struct bus_space igsfb_memt, igsfb_iot;
80 
81 #if (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
82 extern int console_ihandle;
83 #endif
84 
85 int
86 igsfb_ofbus_cnattach(bus_space_tag_t iot, bus_space_tag_t memt)
87 {
88 	struct igsfb_devconfig *dc;
89 	int ret;
90 	int chosen_phandle, igs_node;
91 	int stdout_ihandle, stdout_phandle;
92 	uint32_t regs[16];
93 	char mode_buffer[64];
94 
95 	stdout_phandle = 0;
96 
97 	/* first find out if there's a CyberPro at all in this machine */
98 	igs_node = OF_finddevice("/vlbus/display");
99 	if (igs_node == -1)
100 		return ENXIO;
101 	if (of_compatible(igs_node, compat_strings) < 0)
102 		return ENXIO;
103 
104 	/*
105 	 * now we know there's a CyberPro in this machine so map it into
106 	 * kernel space, even if it's not the console
107 	 */
108 	if (OF_getprop(igs_node, "reg", regs, sizeof(regs)) <= 0)
109 		return ENXIO;
110 
111 	igsfb_mem_paddr = be32toh(regs[13]);
112 	/* 4MB VRAM aperture, bufferable and cacheable */
113 	igsfb_mem_vaddr = ofw_map(igsfb_mem_paddr, 0x00400000, L2_B | L2_C);
114 	/* MMIO registers */
115 	igsfb_mmio_vaddr = ofw_map(igsfb_mem_paddr + IGS_MEM_MMIO_SELECT,
116 	    0x00100000, 0);
117 
118 	memcpy(&igsfb_memt, memt, sizeof(struct bus_space));
119 	igsfb_memt.bs_cookie = (void *)igsfb_mem_vaddr;
120 	memcpy(&igsfb_iot, memt, sizeof(struct bus_space));
121 	igsfb_iot.bs_cookie = (void *)igsfb_mmio_vaddr;
122 
123 	/*
124 	 * check if the firmware output device is indeed the CyberPro
125 	 */
126 	if ((chosen_phandle = OF_finddevice("/chosen")) == -1 ||
127 	    OF_getprop(chosen_phandle, "stdout", &stdout_ihandle,
128 	    sizeof(stdout_ihandle)) != sizeof(stdout_ihandle)) {
129 		return ENXIO;
130 	}
131 	stdout_ihandle = of_decode_int((void *)&stdout_ihandle);
132 	stdout_phandle = OF_instance_to_package(stdout_ihandle);
133 
134 	if (stdout_phandle != igs_node)
135 		return ENXIO;
136 
137 	/* ok, now setup and attach the console */
138 	dc = &igsfb_console_dc;
139 	ret = igsfb_setup_dc(dc);
140 	if (ret)
141 		return ret;
142 
143 	if (of_get_mode_string(mode_buffer, sizeof(mode_buffer))) {
144 		strcpy(dc->dc_modestring, mode_buffer);
145 	}
146 
147 	ret = igsfb_cnattach_subr(dc);
148 	if (ret)
149 		return ret;
150 
151 	igsfb_ofbus_console = 1;
152 	igsfb_ofbus_phandle = stdout_phandle;
153 #if (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
154 	console_ihandle = stdout_ihandle;
155 #endif
156 	return 0;
157 }
158 
159 static int
160 igsfb_setup_dc(struct igsfb_devconfig *dc)
161 {
162 	int ret;
163 
164 	dc->dc_id = 0x2010;
165 	dc->dc_memt = &igsfb_memt;
166 	dc->dc_memaddr = 0;
167 	dc->dc_memsz= 0x00400000;
168 	dc->dc_memflags = 0;
169 
170 	dc->dc_iot = &igsfb_iot;
171 	dc->dc_iobase = 0;
172 	dc->dc_ioflags = 0;
173 	dc->dc_mmap = igsfb_ofbus_mmap;
174 	if (bus_space_map(dc->dc_iot,
175 			  dc->dc_iobase + IGS_REG_BASE, IGS_REG_SIZE,
176 			  dc->dc_ioflags,
177 			  &dc->dc_ioh) != 0)
178 	{
179 		printf("unable to map I/O registers\n");
180 		return 1;
181 	}
182 	ret = igsfb_enable(dc->dc_iot, dc->dc_iobase, dc->dc_ioflags);
183 	if (ret)
184 		return ret;
185 	return 0;
186 }
187 
188 static int
189 igsfb_ofbus_is_console(int phandle)
190 {
191 
192 	return igsfb_ofbus_console && (phandle == igsfb_ofbus_phandle);
193 }
194 
195 
196 static int
197 igsfb_ofbus_match(struct device *parent, struct cfdata *match, void *aux)
198 {
199 	struct ofbus_attach_args *oba = aux;
200 
201 	if (of_compatible(oba->oba_phandle, compat_strings) < 0)
202 		return 0;
203 
204 	return 10;	/* beat vga etc. */
205 }
206 
207 static void
208 igsfb_ofbus_attach(struct device *parent, struct device *self, void *aux)
209 {
210 	struct igsfb_softc *sc = (struct igsfb_softc *)self;
211 	struct ofbus_attach_args *oba = aux;
212 	uint32_t regs[16];
213 	int isconsole, ret;
214 
215 	if (igsfb_ofbus_is_console(oba->oba_phandle)) {
216 		isconsole = 1;
217 		sc->sc_dc = &igsfb_console_dc;
218 	} else {
219 		isconsole = 0;
220 		sc->sc_dc = malloc(sizeof(struct igsfb_devconfig),
221 				   M_DEVBUF, M_NOWAIT | M_ZERO);
222 		if (sc->sc_dc == NULL)
223 			panic("unable to allocate igsfb_devconfig");
224 		if (OF_getprop(oba->oba_phandle, "reg",
225 			       regs, sizeof(regs)) <= 0)
226 		{
227 			printf(": unable to read 'reg' property\n");
228 			return;
229 		}
230 		ret = igsfb_setup_dc(sc->sc_dc);
231 		if (ret)
232 			return;
233 	}
234 	printf(": IGS CyberPro 2010 at 0x%08x\n", (uint32_t)igsfb_mem_paddr);
235 
236 	igsfb_attach_subr(sc, isconsole);
237 }
238 
239 static paddr_t
240 igsfb_ofbus_mmap(void *v, void *vs, off_t offset, int prot)
241 {
242 
243 #ifdef PCI_MAGIC_IO_RANGE
244 	/* access to IO ports */
245 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
246 	    (offset < (PCI_MAGIC_IO_RANGE + 0x10000))) {
247 		paddr_t pa;
248 
249 		pa = isa_io_physaddr + offset - PCI_MAGIC_IO_RANGE;
250 		return arm_btop(pa);
251 	}
252 #endif
253 	/*
254 	 * we also need to allow mapping of the whole aperture, including MMIO
255 	 * registers on CyberPro at its physical address
256 	 */
257 	if ((offset >= igsfb_mem_paddr) &&
258 	    (offset < (igsfb_mem_paddr + 0x01000000)))
259 		return arm_btop(offset);
260 
261 	return -1;
262 }
263