genfb_sbus.c revision 1.10 1 1.10 macallan /* $NetBSD: genfb_sbus.c,v 1.10 2010/10/07 07:53:54 macallan Exp $ */
2 1.1 macallan
3 1.1 macallan /*-
4 1.1 macallan * Copyright (c) 2007 Michael Lorenz
5 1.1 macallan * All rights reserved.
6 1.1 macallan *
7 1.1 macallan * Redistribution and use in source and binary forms, with or without
8 1.1 macallan * modification, are permitted provided that the following conditions
9 1.1 macallan * are met:
10 1.1 macallan * 1. Redistributions of source code must retain the above copyright
11 1.1 macallan * notice, this list of conditions and the following disclaimer.
12 1.1 macallan * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 macallan * notice, this list of conditions and the following disclaimer in the
14 1.1 macallan * documentation and/or other materials provided with the distribution.
15 1.1 macallan *
16 1.1 macallan * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 macallan * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 macallan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 macallan * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 macallan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 macallan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 macallan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 macallan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 macallan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 macallan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 macallan * POSSIBILITY OF SUCH DAMAGE.
27 1.1 macallan */
28 1.1 macallan
29 1.1 macallan /* an SBus frontend for the generic fb console driver */
30 1.1 macallan
31 1.1 macallan #include <sys/cdefs.h>
32 1.10 macallan __KERNEL_RCSID(0, "$NetBSD: genfb_sbus.c,v 1.10 2010/10/07 07:53:54 macallan Exp $");
33 1.1 macallan
34 1.1 macallan #include <sys/param.h>
35 1.1 macallan #include <sys/systm.h>
36 1.1 macallan #include <sys/buf.h>
37 1.1 macallan #include <sys/device.h>
38 1.1 macallan #include <sys/ioctl.h>
39 1.1 macallan #include <sys/conf.h>
40 1.1 macallan
41 1.1 macallan #include <uvm/uvm.h>
42 1.1 macallan
43 1.3 ad #include <sys/bus.h>
44 1.1 macallan #include <machine/autoconf.h>
45 1.1 macallan #include <machine/pmap.h>
46 1.1 macallan
47 1.1 macallan #include <dev/sbus/sbusvar.h>
48 1.1 macallan #include <dev/sun/fbio.h>
49 1.1 macallan #include <dev/sun/fbvar.h>
50 1.1 macallan
51 1.1 macallan #include <dev/wsfb/genfbvar.h>
52 1.1 macallan
53 1.1 macallan struct genfb_sbus_softc {
54 1.1 macallan struct genfb_softc sc_gen;
55 1.1 macallan bus_space_tag_t sc_tag;
56 1.1 macallan paddr_t sc_paddr;
57 1.1 macallan };
58 1.1 macallan
59 1.7 cegger static int genfb_match_sbus(device_t, cfdata_t, void *);
60 1.7 cegger static void genfb_attach_sbus(device_t, device_t, void *);
61 1.1 macallan static int genfb_ioctl_sbus(void *, void *, u_long, void *, int,
62 1.1 macallan struct lwp*);
63 1.1 macallan static paddr_t genfb_mmap_sbus(void *, void *, off_t, int);
64 1.1 macallan
65 1.10 macallan CFATTACH_DECL_NEW(genfb_sbus, sizeof(struct genfb_sbus_softc),
66 1.1 macallan genfb_match_sbus, genfb_attach_sbus, NULL, NULL);
67 1.1 macallan
68 1.1 macallan /*
69 1.1 macallan * Match a graphics device.
70 1.1 macallan */
71 1.1 macallan static int
72 1.9 tsutsui genfb_match_sbus(device_t parent, cfdata_t cf, void *aux)
73 1.1 macallan {
74 1.1 macallan struct sbus_attach_args *sa = aux;
75 1.1 macallan
76 1.1 macallan /* if there's no address property we can't use the device */
77 1.1 macallan if (prom_getpropint(sa->sa_node, "address", -1) == -1)
78 1.1 macallan return 0;
79 1.1 macallan if (strcmp("display", prom_getpropstring(sa->sa_node, "device_type"))
80 1.1 macallan == 0)
81 1.1 macallan return 1;
82 1.1 macallan
83 1.1 macallan return 0;
84 1.1 macallan }
85 1.1 macallan
86 1.1 macallan /*
87 1.1 macallan * Attach a display. We need to notice if it is the console, too.
88 1.1 macallan */
89 1.1 macallan static void
90 1.7 cegger genfb_attach_sbus(device_t parent, device_t self, void *args)
91 1.1 macallan {
92 1.9 tsutsui struct genfb_sbus_softc *sc = device_private(self);
93 1.1 macallan struct sbus_attach_args *sa = args;
94 1.1 macallan struct genfb_ops ops;
95 1.1 macallan prop_dictionary_t dict;
96 1.1 macallan bus_space_handle_t bh;
97 1.1 macallan paddr_t fbpa;
98 1.1 macallan vaddr_t fbva;
99 1.1 macallan int node = sa->sa_node;
100 1.1 macallan int isconsole;
101 1.1 macallan
102 1.1 macallan aprint_normal("\n");
103 1.10 macallan sc->sc_gen.sc_dev = self;
104 1.1 macallan /* Remember cookies for genfb_mmap_sbus() */
105 1.1 macallan sc->sc_tag = sa->sa_bustag;
106 1.1 macallan sc->sc_paddr = sbus_bus_addr(sa->sa_bustag, sa->sa_slot, sa->sa_offset);
107 1.1 macallan
108 1.1 macallan /* read geometry information from the device tree */
109 1.1 macallan sc->sc_gen.sc_width = prom_getpropint(sa->sa_node, "width", 1152);
110 1.1 macallan sc->sc_gen.sc_height = prom_getpropint(sa->sa_node, "height", 900);
111 1.1 macallan sc->sc_gen.sc_depth = prom_getpropint(sa->sa_node, "depth", 8);
112 1.1 macallan sc->sc_gen.sc_stride = prom_getpropint(sa->sa_node, "linebytes",
113 1.1 macallan (sc->sc_gen.sc_width * sc->sc_gen.sc_depth + 7) >> 3 );
114 1.2 macallan sc->sc_gen.sc_fbsize = sc->sc_gen.sc_height * sc->sc_gen.sc_stride;
115 1.1 macallan fbva = (uint32_t)prom_getpropint(sa->sa_node, "address", 0);
116 1.1 macallan if (fbva == 0)
117 1.1 macallan panic("this fb has no address property\n");
118 1.4 cegger aprint_normal_dev(self, "%d x %d at %d bit\n",
119 1.1 macallan sc->sc_gen.sc_width, sc->sc_gen.sc_height, sc->sc_gen.sc_depth);
120 1.1 macallan
121 1.1 macallan pmap_extract(pmap_kernel(), fbva, &fbpa);
122 1.1 macallan sc->sc_gen.sc_fboffset = (fbpa & 0x01ffffff) -
123 1.1 macallan (sc->sc_paddr & 0x01ffffff);
124 1.4 cegger aprint_normal_dev(self, "framebuffer at offset 0x%x\n",
125 1.1 macallan (uint32_t)sc->sc_gen.sc_fboffset);
126 1.1 macallan
127 1.1 macallan #if notyet
128 1.1 macallan if (sc->sc_gen.sc_depth <= 8) {
129 1.1 macallan /* setup some ANSIish colour map */
130 1.1 macallan char boo[256];
131 1.1 macallan snprintf(boo, 256, "\" pal!\" %x %x %x %x %x call",
132 1.1 macallan sa->sa_node, 0, 0xa0, 0xa0, 0);
133 1.1 macallan prom_interpret(boo);
134 1.1 macallan }
135 1.1 macallan #endif
136 1.1 macallan
137 1.1 macallan isconsole = fb_is_console(node);
138 1.1 macallan dict = device_properties(self);
139 1.1 macallan prop_dictionary_set_bool(dict, "is_console", isconsole);
140 1.1 macallan
141 1.1 macallan if (sbus_bus_map(sa->sa_bustag,
142 1.1 macallan sa->sa_slot,
143 1.1 macallan sa->sa_offset + sc->sc_gen.sc_fboffset,
144 1.1 macallan sc->sc_gen.sc_fbsize,
145 1.1 macallan BUS_SPACE_MAP_LINEAR, &bh) != 0) {
146 1.4 cegger aprint_error_dev(self, "cannot map framebuffer\n");
147 1.1 macallan return;
148 1.1 macallan }
149 1.1 macallan sc->sc_gen.sc_fbaddr = (void *)bus_space_vaddr(sa->sa_bustag, bh);
150 1.1 macallan
151 1.1 macallan ops.genfb_ioctl = genfb_ioctl_sbus;
152 1.1 macallan ops.genfb_mmap = genfb_mmap_sbus;
153 1.1 macallan
154 1.1 macallan genfb_attach(&sc->sc_gen, &ops);
155 1.1 macallan }
156 1.1 macallan
157 1.1 macallan static int
158 1.1 macallan genfb_ioctl_sbus(void *v, void *vs, u_long cmd, void *data, int flag,
159 1.1 macallan struct lwp *l)
160 1.1 macallan {
161 1.1 macallan
162 1.1 macallan switch (cmd) {
163 1.1 macallan case WSDISPLAYIO_GTYPE:
164 1.1 macallan *(u_int *)data = WSDISPLAY_TYPE_GENFB;
165 1.1 macallan return 0;
166 1.1 macallan }
167 1.1 macallan
168 1.1 macallan return EPASSTHROUGH;
169 1.1 macallan }
170 1.1 macallan
171 1.1 macallan static paddr_t
172 1.1 macallan genfb_mmap_sbus(void *v, void *vs, off_t offset, int prot)
173 1.1 macallan {
174 1.1 macallan struct genfb_sbus_softc *sc = v;
175 1.1 macallan
176 1.1 macallan /* regular fb mapping at 0 */
177 1.1 macallan if ((offset >= 0) && (offset < sc->sc_gen.sc_fbsize)) {
178 1.1 macallan
179 1.1 macallan return bus_space_mmap(sc->sc_tag, sc->sc_paddr,
180 1.1 macallan sc->sc_gen.sc_fboffset + offset, prot,
181 1.1 macallan BUS_SPACE_MAP_LINEAR);
182 1.1 macallan }
183 1.1 macallan
184 1.1 macallan return -1;
185 1.1 macallan }
186