xref: /netbsd-src/sys/arch/shark/ofw/chipsfb_ofbus.c (revision 4c3eb207d36f67d31994830c0a694161fc1ca39b)
1 /*	$NetBSD: chipsfb_ofbus.c,v 1.5 2021/01/27 03:10:21 thorpej Exp $ */
2 
3 /*
4  * Copyright (c) 2011 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  * C&T 6555x series.
30  * ofbus attachment for chipsfb
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: chipsfb_ofbus.c,v 1.5 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/malloc.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/ct65550reg.h>
58 #include <dev/ic/ct65550var.h>
59 #include <shark/ofw/igsfb_ofbusvar.h>
60 
61 static int	chipsfb_ofbus_is_console(int);
62 
63 static int chipsfb_ofbus_console = 0;
64 static int chipsfb_ofbus_phandle = 0;
65 
66 
67 
68 static int	chipsfb_ofbus_match(device_t, struct cfdata *, void *);
69 static void	chipsfb_ofbus_attach(device_t, device_t, void *);
70 static paddr_t	chipsfb_ofbus_mmap(void *, void *, off_t, int);
71 int chipsfb_ofbus_cnattach(bus_space_tag_t, bus_space_tag_t);
72 
73 CFATTACH_DECL_NEW(chipsfb_ofbus, sizeof(struct chipsfb_softc),
74     chipsfb_ofbus_match, chipsfb_ofbus_attach, NULL, NULL);
75 
76 static const struct device_compatible_entry compat_data[] = {
77 	{ .compat = "CHPS,ct65550" },
78 	DEVICE_COMPAT_EOL
79 };
80 
81 vaddr_t chipsfb_mem_vaddr = 0, chipsfb_mmio_vaddr = 0;
82 paddr_t chipsfb_mem_paddr;
83 extern paddr_t isa_io_physaddr;
84 struct bus_space chipsfb_memt, chipsfb_iot;
85 
86 #if (NCHIPSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
87 extern int console_ihandle;
88 #endif
89 
90 int
91 chipsfb_ofbus_cnattach(bus_space_tag_t iot, bus_space_tag_t memt)
92 {
93 	int chosen_phandle, ct_node;
94 	int stdout_ihandle, stdout_phandle;
95 	uint32_t regs[16];
96 
97 	stdout_phandle = 0;
98 
99 	/* first find out if there's a ct65550 at all in this machine */
100 	ct_node = OF_finddevice("/vlbus/display");
101 	if (ct_node == -1)
102 		return ENXIO;
103 	if (!of_compatible_match(ct_node, compat_data))
104 		return ENXIO;
105 
106 	/*
107 	 * now we know there's a CyberPro in this machine so map it into
108 	 * kernel space, even if it's not the console
109 	 */
110 	if (OF_getprop(ct_node, "reg", regs, sizeof(regs)) <= 0)
111 		return ENXIO;
112 
113 	chipsfb_mem_paddr = be32toh(regs[10]);
114 	/* 2MB RAM aperture, bufferable and not cacheable */
115 	chipsfb_mem_vaddr = ofw_map(chipsfb_mem_paddr, 0x00200000, L2_B);
116 	/* 128kB MMIO registers */
117 	chipsfb_mmio_vaddr = ofw_map(chipsfb_mem_paddr + CT_OFF_BITBLT,
118 	    0x00020000, 0);
119 
120 	memcpy(&chipsfb_memt, memt, sizeof(struct bus_space));
121 	chipsfb_memt.bs_cookie = (void *)chipsfb_mem_vaddr;
122 	memcpy(&chipsfb_iot, iot, sizeof(struct bus_space));
123 
124 	/*
125 	 * check if the firmware output device is indeed the ct65550
126 	 */
127 	if ((chosen_phandle = OF_finddevice("/chosen")) == -1 ||
128 	    OF_getprop(chosen_phandle, "stdout", &stdout_ihandle,
129 	    sizeof(stdout_ihandle)) != sizeof(stdout_ihandle)) {
130 		return ENXIO;
131 	}
132 	stdout_ihandle = of_decode_int((void *)&stdout_ihandle);
133 	stdout_phandle = OF_instance_to_package(stdout_ihandle);
134 
135 	if (stdout_phandle != ct_node)
136 		return ENXIO;
137 
138 
139 	chipsfb_ofbus_console = 1;
140 	chipsfb_ofbus_phandle = stdout_phandle;
141 #if (NCHIPSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
142 	console_ihandle = stdout_ihandle;
143 #endif
144 	/* we're all set, now let's wait for chipsfb to attach */
145 
146 	return 0;
147 }
148 
149 static int
150 chipsfb_ofbus_is_console(int phandle)
151 {
152 
153 	return chipsfb_ofbus_console && (phandle == chipsfb_ofbus_phandle);
154 }
155 
156 
157 static int
158 chipsfb_ofbus_match(device_t parent, struct cfdata *match, void *aux)
159 {
160 	struct ofbus_attach_args *oba = aux;
161 
162 							/* beat vga etc. */
163 	return of_compatible_match(oba->oba_phandle, compat_data) * 10;
164 }
165 
166 static void
167 chipsfb_ofbus_attach(device_t parent, device_t self, void *aux)
168 {
169 	struct chipsfb_softc *sc = device_private(self);
170 	struct ofbus_attach_args *oba = aux;
171 	prop_dictionary_t dict;
172 	int isconsole, width, height, linebytes, depth;
173 
174 	printf(": Chips & Technologies 65550 at 0x%08x\n",
175 	    (uint32_t)chipsfb_mem_paddr);
176 
177 	sc->sc_dev = self;
178 	sc->sc_memt = &chipsfb_memt;
179 	sc->sc_iot = &chipsfb_iot;
180 	sc->sc_fb = chipsfb_mem_paddr;
181 	sc->sc_fbsize = 0x00800000;	/* 8MB aperture */
182 	sc->sc_fbh = chipsfb_mem_vaddr;
183 	sc->sc_mmregh = chipsfb_mmio_vaddr;
184 	sc->sc_ioregh = isa_io_data_vaddr();
185 	sc->sc_mmap = chipsfb_ofbus_mmap;
186 	sc->sc_ioctl = NULL;
187 	sc->memsize = 0x00200000;
188 
189 	dict = device_properties(sc->sc_dev);
190 	if (OF_getprop(oba->oba_phandle, "width", &width, sizeof(width)) == 4) {
191 		width = be32toh(width);
192 	} else
193 		width = 640;
194 	if (OF_getprop(oba->oba_phandle, "height", &height, sizeof(height))
195 	    == 4) {
196 		height = be32toh(height);
197 	} else
198 		height = 480;
199 	if (OF_getprop(oba->oba_phandle, "depth", &depth, sizeof(depth)) == 4) {
200 		depth = be32toh(depth);
201 	} else
202 		depth = 8;
203 	if (OF_getprop(oba->oba_phandle, "linebytes", &linebytes,
204 	    sizeof(linebytes)) == 4) {
205 		linebytes = be32toh(linebytes);
206 	} else
207 		linebytes = width * (depth >> 3);
208 	isconsole = chipsfb_ofbus_is_console(oba->oba_phandle);
209 
210 	prop_dictionary_set_uint32(dict, "width", width);
211 	prop_dictionary_set_uint32(dict, "height", height);
212 	prop_dictionary_set_uint32(dict, "linebytes", linebytes);
213 	prop_dictionary_set_uint32(dict, "depth", depth);
214 	prop_dictionary_set_bool(dict, "is_console", isconsole);
215 
216 	chipsfb_do_attach(sc);
217 }
218 
219 static paddr_t
220 chipsfb_ofbus_mmap(void *v, void *vs, off_t offset, int prot)
221 {
222 
223 #ifdef PCI_MAGIC_IO_RANGE
224 	/* access to IO ports */
225 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
226 	    (offset < (PCI_MAGIC_IO_RANGE + 0x10000))) {
227 		paddr_t pa;
228 
229 		pa = isa_io_physaddr + offset - PCI_MAGIC_IO_RANGE;
230 		return arm_btop(pa);
231 	}
232 #endif
233 
234 	if ((offset >= chipsfb_mem_paddr) &&
235 	    (offset < (chipsfb_mem_paddr + CT_OFF_BITBLT))) {
236 		return (arm_btop(offset) | ARM32_MMAP_WRITECOMBINE);
237 	} else if ((offset >= (chipsfb_mem_paddr + CT_OFF_BITBLT)) &&
238 	    (offset < (chipsfb_mem_paddr + 0x00800000))) {
239 		return arm_btop(offset);
240 	} else if ((offset >= 0xa0000) &&
241 	    (offset < 0xbffff)) {
242 		return (arm_btop(offset) | ARM32_MMAP_WRITECOMBINE);
243 	}
244 
245 	return -1;
246 }
247