chipsfb_ofbus.c revision 1.1.2.2 1 1.1.2.2 rmind /* $NetBSD: chipsfb_ofbus.c,v 1.1.2.2 2011/04/21 01:41:24 rmind Exp $ */
2 1.1.2.2 rmind
3 1.1.2.2 rmind /*
4 1.1.2.2 rmind * Copyright (c) 2006 Michael Lorenz
5 1.1.2.2 rmind * All rights reserved.
6 1.1.2.2 rmind *
7 1.1.2.2 rmind * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 rmind * modification, are permitted provided that the following conditions
9 1.1.2.2 rmind * are met:
10 1.1.2.2 rmind * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 rmind * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 rmind * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 rmind * documentation and/or other materials provided with the distribution.
15 1.1.2.2 rmind *
16 1.1.2.2 rmind * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1.2.2 rmind * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1.2.2 rmind * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1.2.2 rmind * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1.2.2 rmind * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1.2.2 rmind * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1.2.2 rmind * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1.2.2 rmind * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1.2.2 rmind * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1.2.2 rmind * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1.2.2 rmind */
27 1.1.2.2 rmind
28 1.1.2.2 rmind /*
29 1.1.2.2 rmind * C&T 6555x series.
30 1.1.2.2 rmind * ofbus attachment for chipsfb
31 1.1.2.2 rmind */
32 1.1.2.2 rmind
33 1.1.2.2 rmind #include <sys/cdefs.h>
34 1.1.2.2 rmind __KERNEL_RCSID(0, "$NetBSD: chipsfb_ofbus.c,v 1.1.2.2 2011/04/21 01:41:24 rmind Exp $");
35 1.1.2.2 rmind
36 1.1.2.2 rmind #include <sys/param.h>
37 1.1.2.2 rmind #include <sys/systm.h>
38 1.1.2.2 rmind #include <sys/kernel.h>
39 1.1.2.2 rmind #include <sys/device.h>
40 1.1.2.2 rmind #include <sys/malloc.h>
41 1.1.2.2 rmind #include <sys/buf.h>
42 1.1.2.2 rmind #include <uvm/uvm.h>
43 1.1.2.2 rmind
44 1.1.2.2 rmind #include <machine/bus.h>
45 1.1.2.2 rmind #include <machine/intr.h>
46 1.1.2.2 rmind #include <machine/ofw.h>
47 1.1.2.2 rmind #include <machine/pmap.h>
48 1.1.2.2 rmind
49 1.1.2.2 rmind #include <dev/isa/isavar.h>
50 1.1.2.2 rmind
51 1.1.2.2 rmind #include <dev/wscons/wsdisplayvar.h>
52 1.1.2.2 rmind #include <dev/wscons/wsconsio.h>
53 1.1.2.2 rmind #include <dev/rasops/rasops.h>
54 1.1.2.2 rmind #include <dev/wscons/wsdisplay_vconsvar.h>
55 1.1.2.2 rmind #include <dev/ofw/openfirm.h>
56 1.1.2.2 rmind
57 1.1.2.2 rmind #include <dev/ic/ct65550reg.h>
58 1.1.2.2 rmind #include <dev/ic/ct65550var.h>
59 1.1.2.2 rmind #include <shark/ofw/igsfb_ofbusvar.h>
60 1.1.2.2 rmind
61 1.1.2.2 rmind static int chipsfb_ofbus_is_console(int);
62 1.1.2.2 rmind
63 1.1.2.2 rmind static int chipsfb_ofbus_console = 0;
64 1.1.2.2 rmind static int chipsfb_ofbus_phandle = 0;
65 1.1.2.2 rmind
66 1.1.2.2 rmind
67 1.1.2.2 rmind
68 1.1.2.2 rmind static int chipsfb_ofbus_match(device_t, struct cfdata *, void *);
69 1.1.2.2 rmind static void chipsfb_ofbus_attach(device_t, device_t, void *);
70 1.1.2.2 rmind static paddr_t chipsfb_ofbus_mmap(void *, void *, off_t, int);
71 1.1.2.2 rmind int chipsfb_ofbus_cnattach(bus_space_tag_t, bus_space_tag_t);
72 1.1.2.2 rmind
73 1.1.2.2 rmind CFATTACH_DECL_NEW(chipsfb_ofbus, sizeof(struct chipsfb_softc),
74 1.1.2.2 rmind chipsfb_ofbus_match, chipsfb_ofbus_attach, NULL, NULL);
75 1.1.2.2 rmind
76 1.1.2.2 rmind static const char const *compat_strings[] = { "CHPS,ct65550", NULL };
77 1.1.2.2 rmind
78 1.1.2.2 rmind vaddr_t chipsfb_mem_vaddr = 0, chipsfb_mmio_vaddr = 0;
79 1.1.2.2 rmind paddr_t chipsfb_mem_paddr;
80 1.1.2.2 rmind extern paddr_t isa_io_physaddr;
81 1.1.2.2 rmind struct bus_space chipsfb_memt, chipsfb_iot;
82 1.1.2.2 rmind
83 1.1.2.2 rmind #if (NCHIPSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
84 1.1.2.2 rmind extern int console_ihandle;
85 1.1.2.2 rmind #endif
86 1.1.2.2 rmind
87 1.1.2.2 rmind int
88 1.1.2.2 rmind chipsfb_ofbus_cnattach(bus_space_tag_t iot, bus_space_tag_t memt)
89 1.1.2.2 rmind {
90 1.1.2.2 rmind int chosen_phandle, ct_node;
91 1.1.2.2 rmind int stdout_ihandle, stdout_phandle;
92 1.1.2.2 rmind uint32_t regs[16];
93 1.1.2.2 rmind //char mode_buffer[64];
94 1.1.2.2 rmind
95 1.1.2.2 rmind stdout_phandle = 0;
96 1.1.2.2 rmind
97 1.1.2.2 rmind /* first find out if there's a ct65550 at all in this machine */
98 1.1.2.2 rmind ct_node = OF_finddevice("/vlbus/display");
99 1.1.2.2 rmind if (ct_node == -1)
100 1.1.2.2 rmind return ENXIO;
101 1.1.2.2 rmind if (of_compatible(ct_node, compat_strings) < 0)
102 1.1.2.2 rmind return ENXIO;
103 1.1.2.2 rmind
104 1.1.2.2 rmind /*
105 1.1.2.2 rmind * now we know there's a CyberPro in this machine so map it into
106 1.1.2.2 rmind * kernel space, even if it's not the console
107 1.1.2.2 rmind */
108 1.1.2.2 rmind if (OF_getprop(ct_node, "reg", regs, sizeof(regs)) <= 0)
109 1.1.2.2 rmind return ENXIO;
110 1.1.2.2 rmind
111 1.1.2.2 rmind chipsfb_mem_paddr = be32toh(regs[10]);
112 1.1.2.2 rmind /* 2MB RAM aperture, bufferable and cacheable */
113 1.1.2.2 rmind chipsfb_mem_vaddr = ofw_map(chipsfb_mem_paddr, 0x00200000, L2_B | L2_C);
114 1.1.2.2 rmind /* 128kB MMIO registers */
115 1.1.2.2 rmind chipsfb_mmio_vaddr = ofw_map(chipsfb_mem_paddr + CT_OFF_BITBLT,
116 1.1.2.2 rmind 0x00020000, 0);
117 1.1.2.2 rmind
118 1.1.2.2 rmind memcpy(&chipsfb_memt, memt, sizeof(struct bus_space));
119 1.1.2.2 rmind chipsfb_memt.bs_cookie = (void *)chipsfb_mem_vaddr;
120 1.1.2.2 rmind memcpy(&chipsfb_iot, memt, sizeof(struct bus_space));
121 1.1.2.2 rmind chipsfb_iot.bs_cookie = (void *)chipsfb_mmio_vaddr;
122 1.1.2.2 rmind
123 1.1.2.2 rmind /*
124 1.1.2.2 rmind * check if the firmware output device is indeed the ct65550
125 1.1.2.2 rmind */
126 1.1.2.2 rmind if ((chosen_phandle = OF_finddevice("/chosen")) == -1 ||
127 1.1.2.2 rmind OF_getprop(chosen_phandle, "stdout", &stdout_ihandle,
128 1.1.2.2 rmind sizeof(stdout_ihandle)) != sizeof(stdout_ihandle)) {
129 1.1.2.2 rmind return ENXIO;
130 1.1.2.2 rmind }
131 1.1.2.2 rmind stdout_ihandle = of_decode_int((void *)&stdout_ihandle);
132 1.1.2.2 rmind stdout_phandle = OF_instance_to_package(stdout_ihandle);
133 1.1.2.2 rmind
134 1.1.2.2 rmind if (stdout_phandle != ct_node)
135 1.1.2.2 rmind return ENXIO;
136 1.1.2.2 rmind
137 1.1.2.2 rmind
138 1.1.2.2 rmind chipsfb_ofbus_console = 1;
139 1.1.2.2 rmind chipsfb_ofbus_phandle = stdout_phandle;
140 1.1.2.2 rmind #if (NCHIPSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
141 1.1.2.2 rmind console_ihandle = stdout_ihandle;
142 1.1.2.2 rmind #endif
143 1.1.2.2 rmind /* we're all set, now let's wait for chipsfb to attach */
144 1.1.2.2 rmind
145 1.1.2.2 rmind return 0;
146 1.1.2.2 rmind }
147 1.1.2.2 rmind
148 1.1.2.2 rmind static int
149 1.1.2.2 rmind chipsfb_ofbus_is_console(int phandle)
150 1.1.2.2 rmind {
151 1.1.2.2 rmind
152 1.1.2.2 rmind return chipsfb_ofbus_console && (phandle == chipsfb_ofbus_phandle);
153 1.1.2.2 rmind }
154 1.1.2.2 rmind
155 1.1.2.2 rmind
156 1.1.2.2 rmind static int
157 1.1.2.2 rmind chipsfb_ofbus_match(device_t parent, struct cfdata *match, void *aux)
158 1.1.2.2 rmind {
159 1.1.2.2 rmind struct ofbus_attach_args *oba = aux;
160 1.1.2.2 rmind
161 1.1.2.2 rmind if (of_compatible(oba->oba_phandle, compat_strings) < 0)
162 1.1.2.2 rmind return 0;
163 1.1.2.2 rmind
164 1.1.2.2 rmind return 10; /* beat vga etc. */
165 1.1.2.2 rmind }
166 1.1.2.2 rmind
167 1.1.2.2 rmind static void
168 1.1.2.2 rmind chipsfb_ofbus_attach(device_t parent, device_t self, void *aux)
169 1.1.2.2 rmind {
170 1.1.2.2 rmind struct chipsfb_softc *sc = device_private(self);
171 1.1.2.2 rmind struct ofbus_attach_args *oba = aux;
172 1.1.2.2 rmind prop_dictionary_t dict;
173 1.1.2.2 rmind int isconsole, width, height, linebytes, depth;
174 1.1.2.2 rmind
175 1.1.2.2 rmind printf(": Chips & Technologies 65550 at 0x%08x\n",
176 1.1.2.2 rmind (uint32_t)chipsfb_mem_paddr);
177 1.1.2.2 rmind
178 1.1.2.2 rmind sc->sc_dev = self;
179 1.1.2.2 rmind sc->sc_memt = &chipsfb_memt;
180 1.1.2.2 rmind sc->sc_iot = &chipsfb_iot;
181 1.1.2.2 rmind sc->sc_fb = chipsfb_mem_paddr;
182 1.1.2.2 rmind sc->sc_fbsize = 0x00800000; /* 8MB aperture */
183 1.1.2.2 rmind sc->sc_fbh = chipsfb_mem_vaddr;
184 1.1.2.2 rmind sc->sc_mmregh = chipsfb_mmio_vaddr;
185 1.1.2.2 rmind sc->sc_mmap = chipsfb_ofbus_mmap;
186 1.1.2.2 rmind sc->sc_ioctl = NULL;
187 1.1.2.2 rmind sc->memsize = 0x00200000;
188 1.1.2.2 rmind
189 1.1.2.2 rmind dict = device_properties(sc->sc_dev);
190 1.1.2.2 rmind if (OF_getprop(oba->oba_phandle, "width", &width, sizeof(width)) == 4) {
191 1.1.2.2 rmind width = be32toh(width);
192 1.1.2.2 rmind } else
193 1.1.2.2 rmind width = 640;
194 1.1.2.2 rmind if (OF_getprop(oba->oba_phandle, "height", &height, sizeof(height))
195 1.1.2.2 rmind == 4) {
196 1.1.2.2 rmind height = be32toh(height);
197 1.1.2.2 rmind } else
198 1.1.2.2 rmind height = 480;
199 1.1.2.2 rmind if (OF_getprop(oba->oba_phandle, "depth", &depth, sizeof(depth)) == 4) {
200 1.1.2.2 rmind depth = be32toh(depth);
201 1.1.2.2 rmind } else
202 1.1.2.2 rmind depth = 8;
203 1.1.2.2 rmind if (OF_getprop(oba->oba_phandle, "linebytes", &linebytes,
204 1.1.2.2 rmind sizeof(linebytes)) == 4) {
205 1.1.2.2 rmind linebytes = be32toh(linebytes);
206 1.1.2.2 rmind } else
207 1.1.2.2 rmind linebytes = width * (depth >> 3);
208 1.1.2.2 rmind isconsole = chipsfb_ofbus_is_console(oba->oba_phandle);
209 1.1.2.2 rmind
210 1.1.2.2 rmind prop_dictionary_set_uint32(dict, "width", width);
211 1.1.2.2 rmind prop_dictionary_set_uint32(dict, "height", height);
212 1.1.2.2 rmind prop_dictionary_set_uint32(dict, "linebytes", linebytes);
213 1.1.2.2 rmind prop_dictionary_set_uint32(dict, "depth", depth);
214 1.1.2.2 rmind prop_dictionary_set_bool(dict, "is_console", isconsole);
215 1.1.2.2 rmind
216 1.1.2.2 rmind if (bus_space_map(sc->sc_iot, 0, 0x400, 0, &sc->sc_ioregh) != 0) {
217 1.1.2.2 rmind aprint_error("unable to map IO registers\n");
218 1.1.2.2 rmind return;
219 1.1.2.2 rmind }
220 1.1.2.2 rmind chipsfb_do_attach(sc);
221 1.1.2.2 rmind }
222 1.1.2.2 rmind
223 1.1.2.2 rmind static paddr_t
224 1.1.2.2 rmind chipsfb_ofbus_mmap(void *v, void *vs, off_t offset, int prot)
225 1.1.2.2 rmind {
226 1.1.2.2 rmind
227 1.1.2.2 rmind #ifdef PCI_MAGIC_IO_RANGE
228 1.1.2.2 rmind /* access to IO ports */
229 1.1.2.2 rmind if ((offset >= PCI_MAGIC_IO_RANGE) &&
230 1.1.2.2 rmind (offset < (PCI_MAGIC_IO_RANGE + 0x10000))) {
231 1.1.2.2 rmind paddr_t pa;
232 1.1.2.2 rmind
233 1.1.2.2 rmind pa = isa_io_physaddr + offset - PCI_MAGIC_IO_RANGE;
234 1.1.2.2 rmind return arm_btop(pa);
235 1.1.2.2 rmind }
236 1.1.2.2 rmind #endif
237 1.1.2.2 rmind
238 1.1.2.2 rmind if ((offset >= chipsfb_mem_paddr) &&
239 1.1.2.2 rmind (offset < (chipsfb_mem_paddr + CT_OFF_BITBLT))) {
240 1.1.2.2 rmind return (arm_btop(offset) | ARM32_MMAP_WRITECOMBINE);
241 1.1.2.2 rmind } else if ((offset >= (chipsfb_mem_paddr + CT_OFF_BITBLT)) &&
242 1.1.2.2 rmind (offset < (chipsfb_mem_paddr + 0x00800000))) {
243 1.1.2.2 rmind return arm_btop(offset);
244 1.1.2.2 rmind } else if ((offset >= 0xa0000) &&
245 1.1.2.2 rmind (offset < 0xbffff)) {
246 1.1.2.2 rmind return (arm_btop(offset) | ARM32_MMAP_WRITECOMBINE);
247 1.1.2.2 rmind }
248 1.1.2.2 rmind
249 1.1.2.2 rmind return -1;
250 1.1.2.2 rmind }
251