crmfb.c revision 1.5 1 1.5 jmcneill /* $NetBSD: crmfb.c,v 1.5 2007/04/15 20:37:24 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill * 3. All advertising materials mentioning features or use of this software
16 1.1 jmcneill * must display the following acknowledgement:
17 1.1 jmcneill * This product includes software developed by Jared D. McNeill.
18 1.1 jmcneill * 4. Neither the name of The NetBSD Foundation nor the names of its
19 1.1 jmcneill * contributors may be used to endorse or promote products derived
20 1.1 jmcneill * from this software without specific prior written permission.
21 1.1 jmcneill *
22 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE.
33 1.1 jmcneill */
34 1.1 jmcneill
35 1.1 jmcneill /*
36 1.1 jmcneill * SGI-CRM (O2) Framebuffer driver
37 1.1 jmcneill */
38 1.1 jmcneill
39 1.1 jmcneill #include <sys/cdefs.h>
40 1.5 jmcneill __KERNEL_RCSID(0, "$NetBSD: crmfb.c,v 1.5 2007/04/15 20:37:24 jmcneill Exp $");
41 1.1 jmcneill
42 1.1 jmcneill #include <sys/param.h>
43 1.1 jmcneill #include <sys/systm.h>
44 1.1 jmcneill #include <sys/device.h>
45 1.1 jmcneill #include <sys/malloc.h>
46 1.1 jmcneill
47 1.1 jmcneill #define _SGIMIPS_BUS_DMA_PRIVATE
48 1.1 jmcneill #include <machine/autoconf.h>
49 1.1 jmcneill #include <machine/bus.h>
50 1.1 jmcneill #include <machine/machtype.h>
51 1.1 jmcneill #include <machine/vmparam.h>
52 1.1 jmcneill
53 1.2 jmcneill #include <dev/arcbios/arcbios.h>
54 1.2 jmcneill #include <dev/arcbios/arcbiosvar.h>
55 1.2 jmcneill
56 1.1 jmcneill #include <dev/wscons/wsdisplayvar.h>
57 1.1 jmcneill #include <dev/wscons/wsconsio.h>
58 1.1 jmcneill #include <dev/wsfont/wsfont.h>
59 1.1 jmcneill #include <dev/rasops/rasops.h>
60 1.1 jmcneill #include <dev/wscons/wsdisplay_vconsvar.h>
61 1.1 jmcneill
62 1.3 jmcneill #define CRMFB_SHADOWFB
63 1.3 jmcneill
64 1.1 jmcneill struct wsscreen_descr crmfb_defaultscreen = {
65 1.1 jmcneill "default",
66 1.1 jmcneill 0, 0,
67 1.1 jmcneill NULL,
68 1.1 jmcneill 8, 16,
69 1.1 jmcneill WSSCREEN_WSCOLORS,
70 1.1 jmcneill NULL,
71 1.1 jmcneill };
72 1.1 jmcneill
73 1.1 jmcneill const struct wsscreen_descr *_crmfb_scrlist[] = {
74 1.1 jmcneill &crmfb_defaultscreen,
75 1.1 jmcneill };
76 1.1 jmcneill
77 1.1 jmcneill struct wsscreen_list crmfb_screenlist = {
78 1.1 jmcneill sizeof(_crmfb_scrlist) / sizeof(struct wsscreen_descr *),
79 1.1 jmcneill _crmfb_scrlist
80 1.1 jmcneill };
81 1.1 jmcneill
82 1.1 jmcneill static struct vcons_screen crmfb_console_screen;
83 1.1 jmcneill
84 1.1 jmcneill static int crmfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
85 1.1 jmcneill static paddr_t crmfb_mmap(void *, void *, off_t, int);
86 1.1 jmcneill static void crmfb_init_screen(void *, struct vcons_screen *, int, long *);
87 1.1 jmcneill
88 1.1 jmcneill struct wsdisplay_accessops crmfb_accessops = {
89 1.1 jmcneill crmfb_ioctl,
90 1.1 jmcneill crmfb_mmap,
91 1.1 jmcneill NULL, /* alloc_screen */
92 1.1 jmcneill NULL, /* free_screen */
93 1.1 jmcneill NULL, /* show_screen */
94 1.1 jmcneill NULL, /* load_font */
95 1.1 jmcneill NULL, /* pollc */
96 1.1 jmcneill NULL, /* scroll */
97 1.1 jmcneill };
98 1.1 jmcneill
99 1.1 jmcneill /* Memory to allocate to SGI-CRM -- remember, this is stolen from
100 1.1 jmcneill * host memory!
101 1.1 jmcneill */
102 1.1 jmcneill #define CRMFB_TILESIZE (512*128)
103 1.1 jmcneill
104 1.1 jmcneill #define CRMFB_DOTCLOCK 0x00000004
105 1.1 jmcneill #define CRMFB_DOTCLOCK_CLKRUN_SHIFT 20
106 1.1 jmcneill #define CRMFB_VT_XY 0x00010000
107 1.1 jmcneill #define CRMFB_VT_XY_FREEZE_SHIFT 31
108 1.1 jmcneill #define CRMFB_VT_INTR01 0x00010020
109 1.1 jmcneill #define CRMFB_VT_INTR01_EN 0xffffffff
110 1.1 jmcneill #define CRMFB_VT_INTR23 0x00010024
111 1.1 jmcneill #define CRMFB_VT_INTR23_EN 0xffffffff
112 1.1 jmcneill #define CRMFB_VT_VPIX_EN 0x00010038
113 1.1 jmcneill #define CRMFB_VT_VPIX_EN_OFF_SHIFT 0
114 1.1 jmcneill #define CRMFB_VT_VCMAP 0x0001003c
115 1.1 jmcneill #define CRMFB_VT_VCMAP_ON_SHIFT 12
116 1.1 jmcneill #define CRMFB_VT_HCMAP 0x00010040
117 1.1 jmcneill #define CRMFB_VT_HCMAP_ON_SHIFT 12
118 1.1 jmcneill #define CRMFB_OVR_WIDTH_TILE 0x00020000
119 1.1 jmcneill #define CRMFB_OVR_CONTROL 0x00020008
120 1.1 jmcneill #define CRMFB_OVR_CONTROL_DMAEN_SHIFT 0
121 1.1 jmcneill #define CRMFB_FRM_TILESIZE 0x00030000
122 1.1 jmcneill #define CRMFB_FRM_TILESIZE_RHS_SHIFT 0
123 1.1 jmcneill #define CRMFB_FRM_TILESIZE_WIDTH_SHIFT 5
124 1.1 jmcneill #define CRMFB_FRM_TILESIZE_DEPTH_SHIFT 13
125 1.1 jmcneill #define CRMFB_FRM_TILESIZE_DEPTH_8 0
126 1.1 jmcneill #define CRMFB_FRM_TILESIZE_DEPTH_16 1
127 1.1 jmcneill #define CRMFB_FRM_TILESIZE_DEPTH_32 2
128 1.1 jmcneill #define CRMFB_FRM_TILESIZE_FIFOR_SHIFT 15
129 1.1 jmcneill #define CRMFB_FRM_PIXSIZE 0x00030004
130 1.1 jmcneill #define CRMFB_FRM_PIXSIZE_HEIGHT_SHIFT 16
131 1.1 jmcneill #define CRMFB_FRM_CONTROL 0x0003000c
132 1.1 jmcneill #define CRMFB_FRM_CONTROL_DMAEN_SHIFT 0
133 1.1 jmcneill #define CRMFB_FRM_CONTROL_LINEAR_SHIFT 1
134 1.1 jmcneill #define CRMFB_FRM_CONTROL_TILEPTR_SHIFT 9
135 1.1 jmcneill #define CRMFB_DID_CONTROL 0x00040004
136 1.1 jmcneill #define CRMFB_DID_CONTROL_DMAEN_SHIFT 0
137 1.1 jmcneill #define CRMFB_MODE 0x00048000
138 1.1 jmcneill #define CRMFB_MODE_TYP_SHIFT 2
139 1.1 jmcneill #define CRMFB_MODE_TYP_I8 0
140 1.1 jmcneill #define CRMFB_MODE_TYP_ARGB5 4
141 1.1 jmcneill #define CRMFB_MODE_TYP_RGB8 5
142 1.1 jmcneill #define CRMFB_MODE_BUF_SHIFT 0
143 1.1 jmcneill #define CRMFB_MODE_BUF_BOTH 3
144 1.1 jmcneill #define CRMFB_CMAP 0x00050000
145 1.1 jmcneill #define CRMFB_CMAP_FIFO 0x00058000
146 1.1 jmcneill #define CRMFB_GMAP 0x00060000
147 1.1 jmcneill #define CRMFB_CRS_CONTROL 0x00070004
148 1.1 jmcneill
149 1.1 jmcneill static int crmfb_match(struct device *, struct cfdata *, void *);
150 1.1 jmcneill static void crmfb_attach(struct device *, struct device *, void *);
151 1.1 jmcneill int crmfb_probe(void);
152 1.1 jmcneill
153 1.1 jmcneill #define KERNADDR(p) ((void *)((p).addr))
154 1.1 jmcneill #define DMAADDR(p) ((p).map->dm_segs[0].ds_addr)
155 1.1 jmcneill
156 1.1 jmcneill struct crmfb_dma {
157 1.1 jmcneill bus_dmamap_t map;
158 1.1 jmcneill void *addr;
159 1.1 jmcneill bus_dma_segment_t segs[1];
160 1.1 jmcneill int nsegs;
161 1.1 jmcneill size_t size;
162 1.1 jmcneill };
163 1.1 jmcneill
164 1.1 jmcneill struct crmfb_softc {
165 1.1 jmcneill struct device sc_dev;
166 1.1 jmcneill struct vcons_data sc_vd;
167 1.1 jmcneill
168 1.1 jmcneill bus_space_tag_t sc_iot;
169 1.1 jmcneill bus_space_handle_t sc_ioh;
170 1.1 jmcneill bus_dma_tag_t sc_dmat;
171 1.1 jmcneill
172 1.1 jmcneill struct crmfb_dma sc_dma;
173 1.1 jmcneill struct crmfb_dma sc_dmai;
174 1.1 jmcneill
175 1.1 jmcneill int sc_width;
176 1.1 jmcneill int sc_height;
177 1.1 jmcneill int sc_depth;
178 1.1 jmcneill uint32_t sc_fbsize;
179 1.3 jmcneill uint8_t *sc_shadowfb;
180 1.1 jmcneill
181 1.1 jmcneill int sc_wsmode;
182 1.1 jmcneill u_char sc_cmap_red[256];
183 1.1 jmcneill u_char sc_cmap_green[256];
184 1.1 jmcneill u_char sc_cmap_blue[256];
185 1.1 jmcneill };
186 1.1 jmcneill
187 1.1 jmcneill static int crmfb_putcmap(struct crmfb_softc *, struct wsdisplay_cmap *);
188 1.1 jmcneill static int crmfb_getcmap(struct crmfb_softc *, struct wsdisplay_cmap *);
189 1.1 jmcneill static void crmfb_set_palette(struct crmfb_softc *,
190 1.1 jmcneill int, uint8_t, uint8_t, uint8_t);
191 1.1 jmcneill
192 1.1 jmcneill CFATTACH_DECL(crmfb, sizeof(struct crmfb_softc),
193 1.1 jmcneill crmfb_match, crmfb_attach, NULL, NULL);
194 1.1 jmcneill
195 1.1 jmcneill static int
196 1.1 jmcneill crmfb_match(struct device *parent, struct cfdata *cf, void *opaque)
197 1.1 jmcneill {
198 1.1 jmcneill return crmfb_probe();
199 1.1 jmcneill }
200 1.1 jmcneill
201 1.1 jmcneill static void
202 1.1 jmcneill crmfb_attach(struct device *parent, struct device *self, void *opaque)
203 1.1 jmcneill {
204 1.1 jmcneill struct mainbus_attach_args *ma;
205 1.1 jmcneill struct crmfb_softc *sc;
206 1.1 jmcneill struct rasops_info *ri;
207 1.1 jmcneill struct wsemuldisplaydev_attach_args aa;
208 1.1 jmcneill uint32_t d, h;
209 1.1 jmcneill uint16_t *p;
210 1.1 jmcneill unsigned long v;
211 1.1 jmcneill long defattr;
212 1.2 jmcneill const char *consdev;
213 1.1 jmcneill int rv, i;
214 1.1 jmcneill
215 1.1 jmcneill sc = (struct crmfb_softc *)self;
216 1.1 jmcneill ma = (struct mainbus_attach_args *)opaque;
217 1.1 jmcneill
218 1.1 jmcneill sc->sc_iot = SGIMIPS_BUS_SPACE_CRIME;
219 1.1 jmcneill sc->sc_dmat = &sgimips_default_bus_dma_tag;
220 1.1 jmcneill sc->sc_wsmode = WSDISPLAYIO_MODE_EMUL;
221 1.1 jmcneill
222 1.1 jmcneill aprint_normal(": SGI CRIME Graphics Display Engine\n");
223 1.1 jmcneill rv = bus_space_map(sc->sc_iot, ma->ma_addr, 0 /* XXX */,
224 1.1 jmcneill BUS_SPACE_MAP_LINEAR, &sc->sc_ioh);
225 1.1 jmcneill if (rv)
226 1.1 jmcneill panic("crmfb_attach: can't map I/O space");
227 1.1 jmcneill
228 1.1 jmcneill /* determine mode configured by firmware */
229 1.1 jmcneill d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_VCMAP);
230 1.1 jmcneill sc->sc_width = (d >> CRMFB_VT_VCMAP_ON_SHIFT) & 0xfff;
231 1.1 jmcneill d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_HCMAP);
232 1.1 jmcneill sc->sc_height = (d >> CRMFB_VT_HCMAP_ON_SHIFT) & 0xfff;
233 1.1 jmcneill d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE);
234 1.1 jmcneill h = (d >> CRMFB_FRM_TILESIZE_DEPTH_SHIFT) & 0x3;
235 1.1 jmcneill if (h == 0)
236 1.1 jmcneill sc->sc_depth = 8;
237 1.1 jmcneill else if (h == 1)
238 1.1 jmcneill sc->sc_depth = 16;
239 1.1 jmcneill else
240 1.1 jmcneill sc->sc_depth = 32;
241 1.1 jmcneill
242 1.4 martin if (sc->sc_width == 0 || sc->sc_height == 0) {
243 1.4 martin printf("%s: device unusable if not setup by firmware\n",
244 1.4 martin sc->sc_dev.dv_xname);
245 1.4 martin bus_space_unmap(sc->sc_iot, sc->sc_ioh, 0 /* XXX */);
246 1.4 martin return;
247 1.4 martin }
248 1.4 martin
249 1.1 jmcneill printf("%s: initial resolution %dx%d %d bpp\n",
250 1.1 jmcneill sc->sc_dev.dv_xname, sc->sc_width, sc->sc_height, sc->sc_depth);
251 1.1 jmcneill
252 1.1 jmcneill #if 0
253 1.1 jmcneill /* Changing the colour depth is easy... */
254 1.1 jmcneill sc->sc_depth = 32;
255 1.1 jmcneill printf("%s: using resolution %dx%d %d bpp\n",
256 1.1 jmcneill sc->sc_dev.dv_xname, sc->sc_width, sc->sc_height, sc->sc_depth);
257 1.1 jmcneill #endif
258 1.1 jmcneill
259 1.1 jmcneill sc->sc_fbsize = sc->sc_width * sc->sc_height * sc->sc_depth / 8;
260 1.2 jmcneill sc->sc_fbsize = (sc->sc_fbsize / CRMFB_TILESIZE) * CRMFB_TILESIZE;
261 1.1 jmcneill
262 1.1 jmcneill sc->sc_dmai.size = 128 * sizeof(uint16_t);
263 1.1 jmcneill rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dmai.size, 65536, 0,
264 1.1 jmcneill sc->sc_dmai.segs,
265 1.1 jmcneill sizeof(sc->sc_dmai.segs) / sizeof(sc->sc_dmai.segs[0]),
266 1.1 jmcneill &sc->sc_dmai.nsegs, BUS_DMA_NOWAIT);
267 1.1 jmcneill if (rv)
268 1.1 jmcneill panic("crmfb_attach: can't allocate DMA memory");
269 1.1 jmcneill rv = bus_dmamem_map(sc->sc_dmat, sc->sc_dmai.segs, sc->sc_dmai.nsegs,
270 1.1 jmcneill sc->sc_dmai.size, &sc->sc_dmai.addr,
271 1.1 jmcneill BUS_DMA_NOWAIT);
272 1.1 jmcneill if (rv)
273 1.1 jmcneill panic("crmfb_attach: can't map DMA memory");
274 1.1 jmcneill rv = bus_dmamap_create(sc->sc_dmat, sc->sc_dmai.size, 1,
275 1.1 jmcneill sc->sc_dmai.size, 0, BUS_DMA_NOWAIT, &sc->sc_dmai.map);
276 1.1 jmcneill if (rv)
277 1.1 jmcneill panic("crmfb_attach: can't create DMA map");
278 1.1 jmcneill rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dmai.map, sc->sc_dmai.addr,
279 1.1 jmcneill sc->sc_dmai.size, NULL, BUS_DMA_NOWAIT);
280 1.1 jmcneill if (rv)
281 1.1 jmcneill panic("crmfb_attach: can't load DMA map");
282 1.1 jmcneill
283 1.1 jmcneill sc->sc_dma.size = sc->sc_fbsize;
284 1.1 jmcneill rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dma.size, 65536, 0,
285 1.1 jmcneill sc->sc_dma.segs,
286 1.1 jmcneill sizeof(sc->sc_dma.segs) / sizeof(sc->sc_dma.segs[0]),
287 1.1 jmcneill &sc->sc_dma.nsegs, BUS_DMA_NOWAIT);
288 1.1 jmcneill if (rv)
289 1.1 jmcneill panic("crmfb_attach: can't allocate DMA memory");
290 1.1 jmcneill rv = bus_dmamem_map(sc->sc_dmat, sc->sc_dma.segs, sc->sc_dma.nsegs,
291 1.1 jmcneill sc->sc_dma.size, &sc->sc_dma.addr,
292 1.1 jmcneill BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
293 1.1 jmcneill if (rv)
294 1.1 jmcneill panic("crmfb_attach: can't map DMA memory");
295 1.1 jmcneill rv = bus_dmamap_create(sc->sc_dmat, sc->sc_dma.size, 1,
296 1.1 jmcneill sc->sc_dma.size, 0, BUS_DMA_NOWAIT, &sc->sc_dma.map);
297 1.1 jmcneill if (rv)
298 1.1 jmcneill panic("crmfb_attach: can't create DMA map");
299 1.1 jmcneill rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dma.map, sc->sc_dma.addr,
300 1.1 jmcneill sc->sc_dma.size, NULL, BUS_DMA_NOWAIT);
301 1.1 jmcneill if (rv)
302 1.1 jmcneill panic("crmfb_attach: can't load DMA map");
303 1.1 jmcneill
304 1.1 jmcneill p = KERNADDR(sc->sc_dmai);
305 1.1 jmcneill v = (unsigned long)DMAADDR(sc->sc_dma);
306 1.1 jmcneill for (i = 0; i < (sc->sc_fbsize >> 16); i++) {
307 1.1 jmcneill p[i] = ((uint32_t)v >> 16) + i;
308 1.1 jmcneill }
309 1.1 jmcneill
310 1.1 jmcneill memset(KERNADDR(sc->sc_dma), 0x00, sc->sc_fbsize);
311 1.1 jmcneill
312 1.1 jmcneill printf("%s: allocated %d byte fb @ %p (%p)\n", sc->sc_dev.dv_xname,
313 1.1 jmcneill sc->sc_fbsize, KERNADDR(sc->sc_dmai), KERNADDR(sc->sc_dma));
314 1.1 jmcneill
315 1.3 jmcneill #ifdef CRMFB_SHADOWFB
316 1.3 jmcneill /* setup shadow framebuffer */
317 1.3 jmcneill sc->sc_shadowfb = malloc(sc->sc_fbsize, M_DEVBUF, M_NOWAIT | M_ZERO);
318 1.3 jmcneill if (sc->sc_shadowfb == NULL)
319 1.3 jmcneill aprint_error("%s: WARNING: couldn't allocate shadow fb\n",
320 1.3 jmcneill sc->sc_dev.dv_xname);
321 1.3 jmcneill #endif
322 1.3 jmcneill
323 1.1 jmcneill ri = &crmfb_console_screen.scr_ri;
324 1.1 jmcneill memset(ri, 0, sizeof(struct rasops_info));
325 1.1 jmcneill
326 1.1 jmcneill vcons_init(&sc->sc_vd, sc, &crmfb_defaultscreen, &crmfb_accessops);
327 1.1 jmcneill sc->sc_vd.init_screen = crmfb_init_screen;
328 1.1 jmcneill crmfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
329 1.1 jmcneill vcons_init_screen(&sc->sc_vd, &crmfb_console_screen, 1, &defattr);
330 1.1 jmcneill
331 1.1 jmcneill crmfb_defaultscreen.ncols = ri->ri_cols;
332 1.1 jmcneill crmfb_defaultscreen.nrows = ri->ri_rows;
333 1.1 jmcneill crmfb_defaultscreen.textops = &ri->ri_ops;
334 1.1 jmcneill crmfb_defaultscreen.capabilities = ri->ri_caps;
335 1.1 jmcneill crmfb_defaultscreen.modecookie = NULL;
336 1.1 jmcneill
337 1.1 jmcneill /* disable DMA */
338 1.1 jmcneill d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_CONTROL);
339 1.1 jmcneill d &= ~(1 << CRMFB_OVR_CONTROL_DMAEN_SHIFT);
340 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_CONTROL, d);
341 1.1 jmcneill DELAY(10000);
342 1.1 jmcneill d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
343 1.1 jmcneill d &= ~(1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
344 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL, d);
345 1.1 jmcneill DELAY(10000);
346 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_DID_CONTROL, 0);
347 1.1 jmcneill DELAY(10000);
348 1.1 jmcneill
349 1.1 jmcneill /* ensure that CRM starts drawing at the top left of the screen
350 1.1 jmcneill * when we re-enable DMA later
351 1.1 jmcneill */
352 1.1 jmcneill d = (1 << CRMFB_VT_XY_FREEZE_SHIFT);
353 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_XY, d);
354 1.1 jmcneill d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK);
355 1.1 jmcneill d &= ~(1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT);
356 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK, d);
357 1.1 jmcneill
358 1.1 jmcneill /* reset FIFO */
359 1.1 jmcneill d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE);
360 1.1 jmcneill d |= (1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT);
361 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE, d);
362 1.1 jmcneill d &= ~(1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT);
363 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE, d);
364 1.1 jmcneill
365 1.1 jmcneill /* setup colour mode */
366 1.1 jmcneill switch (sc->sc_depth) {
367 1.1 jmcneill case 8:
368 1.1 jmcneill h = CRMFB_MODE_TYP_I8;
369 1.1 jmcneill break;
370 1.1 jmcneill case 16:
371 1.1 jmcneill h = CRMFB_MODE_TYP_ARGB5;
372 1.1 jmcneill break;
373 1.1 jmcneill case 32:
374 1.1 jmcneill h = CRMFB_MODE_TYP_RGB8;
375 1.1 jmcneill break;
376 1.1 jmcneill default:
377 1.1 jmcneill panic("Unsupported depth");
378 1.1 jmcneill }
379 1.1 jmcneill d = h << CRMFB_MODE_TYP_SHIFT;
380 1.1 jmcneill d |= CRMFB_MODE_BUF_BOTH << CRMFB_MODE_BUF_SHIFT;
381 1.1 jmcneill for (i = 0; i < (32 * 4); i += 4)
382 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_MODE + i, d);
383 1.1 jmcneill
384 1.1 jmcneill /* setup tile pointer, but don't turn on DMA yet! */
385 1.1 jmcneill h = DMAADDR(sc->sc_dmai);
386 1.1 jmcneill d = (h >> 9) << CRMFB_FRM_CONTROL_TILEPTR_SHIFT;
387 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL, d);
388 1.1 jmcneill
389 1.1 jmcneill /* init framebuffer width and pixel size */
390 1.1 jmcneill d = (1 << CRMFB_FRM_TILESIZE_WIDTH_SHIFT);
391 1.1 jmcneill switch (sc->sc_depth) {
392 1.1 jmcneill case 8:
393 1.1 jmcneill h = CRMFB_FRM_TILESIZE_DEPTH_8;
394 1.1 jmcneill break;
395 1.1 jmcneill case 16:
396 1.1 jmcneill h = CRMFB_FRM_TILESIZE_DEPTH_16;
397 1.1 jmcneill break;
398 1.1 jmcneill case 32:
399 1.1 jmcneill h = CRMFB_FRM_TILESIZE_DEPTH_32;
400 1.1 jmcneill break;
401 1.1 jmcneill default:
402 1.1 jmcneill panic("Unsupported depth");
403 1.1 jmcneill }
404 1.1 jmcneill d |= (h << CRMFB_FRM_TILESIZE_DEPTH_SHIFT);
405 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE, d);
406 1.1 jmcneill
407 1.1 jmcneill /* init framebuffer height, we use the trick that the Linux
408 1.1 jmcneill * driver uses to fool the CRM out of tiled mode and into
409 1.1 jmcneill * linear mode
410 1.1 jmcneill */
411 1.1 jmcneill h = sc->sc_width * sc->sc_height / (512 / (sc->sc_depth / 8));
412 1.1 jmcneill d = h << CRMFB_FRM_PIXSIZE_HEIGHT_SHIFT;
413 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_PIXSIZE, d);
414 1.1 jmcneill
415 1.1 jmcneill /* turn off firmware overlay and hardware cursor */
416 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_WIDTH_TILE, 0);
417 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_CRS_CONTROL, 0);
418 1.1 jmcneill
419 1.1 jmcneill /* enable drawing again */
420 1.1 jmcneill d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK);
421 1.1 jmcneill d |= (1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT);
422 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK, d);
423 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_XY, 0);
424 1.1 jmcneill
425 1.1 jmcneill /* turn on DMA for the framebuffer */
426 1.1 jmcneill d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
427 1.1 jmcneill d |= (1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
428 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL, d);
429 1.1 jmcneill
430 1.1 jmcneill for (i = 0; i < 256; i++) {
431 1.1 jmcneill crmfb_set_palette(sc, i, rasops_cmap[(i * 3) + 2],
432 1.1 jmcneill rasops_cmap[(i * 3) + 1], rasops_cmap[(i * 3) + 0]);
433 1.1 jmcneill sc->sc_cmap_red[i] = rasops_cmap[(i * 3) + 2];
434 1.1 jmcneill sc->sc_cmap_green[i] = rasops_cmap[(i * 3) + 1];
435 1.1 jmcneill sc->sc_cmap_blue[i] = rasops_cmap[(i * 3) + 0];
436 1.1 jmcneill }
437 1.1 jmcneill
438 1.2 jmcneill consdev = ARCBIOS->GetEnvironmentVariable("ConsoleOut");
439 1.2 jmcneill if (consdev != NULL && strcmp(consdev, "video()") == 0) {
440 1.2 jmcneill wsdisplay_cnattach(&crmfb_defaultscreen, ri, 0, 0, defattr);
441 1.2 jmcneill aa.console = 1;
442 1.2 jmcneill } else
443 1.2 jmcneill aa.console = 0;
444 1.1 jmcneill aa.scrdata = &crmfb_screenlist;
445 1.1 jmcneill aa.accessops = &crmfb_accessops;
446 1.1 jmcneill aa.accesscookie = &sc->sc_vd;
447 1.1 jmcneill
448 1.1 jmcneill config_found(self, &aa, wsemuldisplaydevprint);
449 1.1 jmcneill
450 1.1 jmcneill return;
451 1.1 jmcneill }
452 1.1 jmcneill
453 1.1 jmcneill int
454 1.1 jmcneill crmfb_probe(void)
455 1.1 jmcneill {
456 1.1 jmcneill
457 1.1 jmcneill if (mach_type != MACH_SGI_IP32)
458 1.1 jmcneill return 0;
459 1.1 jmcneill
460 1.1 jmcneill return 1;
461 1.1 jmcneill }
462 1.1 jmcneill
463 1.1 jmcneill static int
464 1.1 jmcneill crmfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
465 1.1 jmcneill {
466 1.1 jmcneill struct vcons_data *vd;
467 1.1 jmcneill struct crmfb_softc *sc;
468 1.1 jmcneill struct vcons_screen *ms;
469 1.1 jmcneill struct wsdisplay_fbinfo *wdf;
470 1.1 jmcneill int nmode;
471 1.1 jmcneill
472 1.1 jmcneill vd = (struct vcons_data *)v;
473 1.1 jmcneill sc = (struct crmfb_softc *)vd->cookie;
474 1.1 jmcneill ms = (struct vcons_screen *)vd->active;
475 1.1 jmcneill
476 1.1 jmcneill switch (cmd) {
477 1.1 jmcneill case WSDISPLAYIO_GTYPE:
478 1.1 jmcneill /* not really, but who cares? */
479 1.1 jmcneill *(u_int *)data = WSDISPLAY_TYPE_NEWPORT;
480 1.1 jmcneill return 0;
481 1.1 jmcneill case WSDISPLAYIO_GINFO:
482 1.1 jmcneill if (vd->active != NULL) {
483 1.1 jmcneill wdf = (void *)data;
484 1.1 jmcneill wdf->height = sc->sc_height;
485 1.1 jmcneill wdf->width = sc->sc_width;
486 1.1 jmcneill wdf->depth = sc->sc_depth;
487 1.1 jmcneill wdf->cmsize = 256;
488 1.1 jmcneill return 0;
489 1.1 jmcneill } else
490 1.1 jmcneill return ENODEV;
491 1.1 jmcneill case WSDISPLAYIO_GETCMAP:
492 1.1 jmcneill if (sc->sc_depth == 8)
493 1.1 jmcneill return crmfb_getcmap(sc, (struct wsdisplay_cmap *)data);
494 1.1 jmcneill else
495 1.1 jmcneill return EINVAL;
496 1.1 jmcneill case WSDISPLAYIO_PUTCMAP:
497 1.1 jmcneill if (sc->sc_depth == 8)
498 1.1 jmcneill return crmfb_putcmap(sc, (struct wsdisplay_cmap *)data);
499 1.1 jmcneill else
500 1.1 jmcneill return EINVAL;
501 1.1 jmcneill case WSDISPLAYIO_LINEBYTES:
502 1.1 jmcneill *(u_int *)data = sc->sc_width * sc->sc_depth / 8;
503 1.1 jmcneill return 0;
504 1.1 jmcneill case WSDISPLAYIO_SMODE:
505 1.1 jmcneill nmode = *(int *)data;
506 1.1 jmcneill if (nmode != sc->sc_wsmode) {
507 1.1 jmcneill sc->sc_wsmode = nmode;
508 1.1 jmcneill if (nmode == WSDISPLAYIO_MODE_EMUL)
509 1.1 jmcneill vcons_redraw_screen(vd->active);
510 1.1 jmcneill }
511 1.1 jmcneill return 0;
512 1.5 jmcneill case WSDISPLAYIO_SVIDEO:
513 1.5 jmcneill case WSDISPLAYIO_GVIDEO:
514 1.5 jmcneill return ENODEV; /* not supported yet */
515 1.1 jmcneill }
516 1.1 jmcneill
517 1.1 jmcneill return EPASSTHROUGH;
518 1.1 jmcneill }
519 1.1 jmcneill
520 1.1 jmcneill static paddr_t
521 1.1 jmcneill crmfb_mmap(void *v, void *vs, off_t offset, int prot)
522 1.1 jmcneill {
523 1.1 jmcneill struct vcons_data *vd;
524 1.1 jmcneill struct crmfb_softc *sc;
525 1.1 jmcneill paddr_t pa;
526 1.1 jmcneill
527 1.1 jmcneill vd = (struct vcons_data *)v;
528 1.1 jmcneill sc = (struct crmfb_softc *)vd->cookie;
529 1.1 jmcneill
530 1.1 jmcneill if (offset >= 0 && offset < sc->sc_fbsize) {
531 1.1 jmcneill pa = bus_dmamem_mmap(sc->sc_dmat, sc->sc_dma.segs,
532 1.1 jmcneill sc->sc_dma.nsegs, offset, prot,
533 1.3 jmcneill BUS_DMA_WAITOK | BUS_DMA_COHERENT);
534 1.1 jmcneill return pa;
535 1.1 jmcneill }
536 1.1 jmcneill
537 1.1 jmcneill return -1;
538 1.1 jmcneill }
539 1.1 jmcneill
540 1.1 jmcneill static void
541 1.1 jmcneill crmfb_init_screen(void *c, struct vcons_screen *scr, int existing,
542 1.1 jmcneill long *defattr)
543 1.1 jmcneill {
544 1.1 jmcneill struct crmfb_softc *sc;
545 1.1 jmcneill struct rasops_info *ri;
546 1.1 jmcneill
547 1.1 jmcneill sc = (struct crmfb_softc *)c;
548 1.1 jmcneill ri = &scr->scr_ri;
549 1.1 jmcneill
550 1.1 jmcneill ri->ri_flg = RI_CENTER;
551 1.1 jmcneill ri->ri_depth = sc->sc_depth;
552 1.1 jmcneill ri->ri_width = sc->sc_width;
553 1.1 jmcneill ri->ri_height = sc->sc_height;
554 1.1 jmcneill ri->ri_stride = ri->ri_width * (ri->ri_depth / 8);
555 1.1 jmcneill
556 1.1 jmcneill switch (ri->ri_depth) {
557 1.1 jmcneill case 16:
558 1.1 jmcneill ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 5;
559 1.1 jmcneill ri->ri_rpos = 10;
560 1.1 jmcneill ri->ri_gpos = 5;
561 1.1 jmcneill ri->ri_bpos = 0;
562 1.1 jmcneill break;
563 1.1 jmcneill case 32:
564 1.1 jmcneill ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
565 1.1 jmcneill ri->ri_rpos = 8;
566 1.1 jmcneill ri->ri_gpos = 16;
567 1.1 jmcneill ri->ri_bpos = 24;
568 1.1 jmcneill break;
569 1.1 jmcneill }
570 1.1 jmcneill
571 1.3 jmcneill if (sc->sc_shadowfb == NULL)
572 1.3 jmcneill ri->ri_bits = KERNADDR(sc->sc_dma);
573 1.3 jmcneill else {
574 1.3 jmcneill ri->ri_bits = sc->sc_shadowfb;
575 1.3 jmcneill ri->ri_hwbits = KERNADDR(sc->sc_dma);
576 1.3 jmcneill }
577 1.1 jmcneill
578 1.1 jmcneill if (existing)
579 1.1 jmcneill ri->ri_flg |= RI_CLEAR;
580 1.1 jmcneill
581 1.1 jmcneill rasops_init(ri, ri->ri_height / 16, ri->ri_width / 8);
582 1.1 jmcneill ri->ri_caps = WSSCREEN_WSCOLORS;
583 1.1 jmcneill rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
584 1.1 jmcneill ri->ri_width / ri->ri_font->fontwidth);
585 1.1 jmcneill ri->ri_hw = scr;
586 1.1 jmcneill
587 1.1 jmcneill return;
588 1.1 jmcneill }
589 1.1 jmcneill
590 1.1 jmcneill static int
591 1.1 jmcneill crmfb_putcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm)
592 1.1 jmcneill {
593 1.1 jmcneill u_int idx, cnt;
594 1.1 jmcneill u_char r[256], g[256], b[256];
595 1.1 jmcneill u_char *rp, *gp, *bp;
596 1.1 jmcneill int rv, i;
597 1.1 jmcneill
598 1.1 jmcneill idx = cm->index;
599 1.1 jmcneill cnt = cm->count;
600 1.1 jmcneill
601 1.1 jmcneill if (idx >= 255 || cnt > 256 || idx + cnt > 256)
602 1.1 jmcneill return EINVAL;
603 1.1 jmcneill
604 1.1 jmcneill rv = copyin(cm->red, &r[idx], cnt);
605 1.1 jmcneill if (rv)
606 1.1 jmcneill return rv;
607 1.1 jmcneill rv = copyin(cm->green, &g[idx], cnt);
608 1.1 jmcneill if (rv)
609 1.1 jmcneill return rv;
610 1.1 jmcneill rv = copyin(cm->blue, &b[idx], cnt);
611 1.1 jmcneill if (rv)
612 1.1 jmcneill return rv;
613 1.1 jmcneill
614 1.1 jmcneill memcpy(&sc->sc_cmap_red[idx], &r[idx], cnt);
615 1.1 jmcneill memcpy(&sc->sc_cmap_green[idx], &g[idx], cnt);
616 1.1 jmcneill memcpy(&sc->sc_cmap_blue[idx], &b[idx], cnt);
617 1.1 jmcneill
618 1.1 jmcneill rp = &sc->sc_cmap_red[idx];
619 1.1 jmcneill gp = &sc->sc_cmap_green[idx];
620 1.1 jmcneill bp = &sc->sc_cmap_blue[idx];
621 1.1 jmcneill
622 1.1 jmcneill for (i = 0; i < cnt; i++) {
623 1.1 jmcneill crmfb_set_palette(sc, idx, *rp, *gp, *bp);
624 1.1 jmcneill idx++;
625 1.1 jmcneill rp++, gp++, bp++;
626 1.1 jmcneill }
627 1.1 jmcneill
628 1.1 jmcneill return 0;
629 1.1 jmcneill }
630 1.1 jmcneill
631 1.1 jmcneill static int
632 1.1 jmcneill crmfb_getcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm)
633 1.1 jmcneill {
634 1.1 jmcneill u_int idx, cnt;
635 1.1 jmcneill int rv;
636 1.1 jmcneill
637 1.1 jmcneill idx = cm->index;
638 1.1 jmcneill cnt = cm->count;
639 1.1 jmcneill
640 1.1 jmcneill if (idx >= 255 || cnt > 256 || idx + cnt > 256)
641 1.1 jmcneill return EINVAL;
642 1.1 jmcneill
643 1.1 jmcneill rv = copyout(&sc->sc_cmap_red[idx], cm->red, cnt);
644 1.1 jmcneill if (rv)
645 1.1 jmcneill return rv;
646 1.1 jmcneill rv = copyout(&sc->sc_cmap_green[idx], cm->green, cnt);
647 1.1 jmcneill if (rv)
648 1.1 jmcneill return rv;
649 1.1 jmcneill rv = copyout(&sc->sc_cmap_blue[idx], cm->blue, cnt);
650 1.1 jmcneill if (rv)
651 1.1 jmcneill return rv;
652 1.1 jmcneill
653 1.1 jmcneill return 0;
654 1.1 jmcneill }
655 1.1 jmcneill
656 1.1 jmcneill static void
657 1.1 jmcneill crmfb_set_palette(struct crmfb_softc *sc, int reg, uint8_t r, uint8_t g,
658 1.1 jmcneill uint8_t b)
659 1.1 jmcneill {
660 1.1 jmcneill uint32_t val;
661 1.1 jmcneill
662 1.1 jmcneill if (reg > 255 || sc->sc_depth != 8)
663 1.1 jmcneill return;
664 1.1 jmcneill
665 1.1 jmcneill while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_CMAP_FIFO) >= 63)
666 1.1 jmcneill DELAY(10);
667 1.1 jmcneill
668 1.1 jmcneill val = (r << 24) | (g << 16) | (b << 8);
669 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_CMAP + (reg * 4), val);
670 1.1 jmcneill
671 1.1 jmcneill return;
672 1.1 jmcneill }
673