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