crmfb.c revision 1.3 1 1.3 jmcneill /* $NetBSD: crmfb.c,v 1.3 2007/04/13 12:21:00 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.3 jmcneill __KERNEL_RCSID(0, "$NetBSD: crmfb.c,v 1.3 2007/04/13 12:21:00 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.1 jmcneill printf("%s: initial resolution %dx%d %d bpp\n",
243 1.1 jmcneill sc->sc_dev.dv_xname, sc->sc_width, sc->sc_height, sc->sc_depth);
244 1.1 jmcneill
245 1.1 jmcneill #if 0
246 1.1 jmcneill /* Changing the colour depth is easy... */
247 1.1 jmcneill sc->sc_depth = 32;
248 1.1 jmcneill printf("%s: using resolution %dx%d %d bpp\n",
249 1.1 jmcneill sc->sc_dev.dv_xname, sc->sc_width, sc->sc_height, sc->sc_depth);
250 1.1 jmcneill #endif
251 1.1 jmcneill
252 1.1 jmcneill sc->sc_fbsize = sc->sc_width * sc->sc_height * sc->sc_depth / 8;
253 1.2 jmcneill sc->sc_fbsize = (sc->sc_fbsize / CRMFB_TILESIZE) * CRMFB_TILESIZE;
254 1.1 jmcneill
255 1.1 jmcneill sc->sc_dmai.size = 128 * sizeof(uint16_t);
256 1.1 jmcneill rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dmai.size, 65536, 0,
257 1.1 jmcneill sc->sc_dmai.segs,
258 1.1 jmcneill sizeof(sc->sc_dmai.segs) / sizeof(sc->sc_dmai.segs[0]),
259 1.1 jmcneill &sc->sc_dmai.nsegs, BUS_DMA_NOWAIT);
260 1.1 jmcneill if (rv)
261 1.1 jmcneill panic("crmfb_attach: can't allocate DMA memory");
262 1.1 jmcneill rv = bus_dmamem_map(sc->sc_dmat, sc->sc_dmai.segs, sc->sc_dmai.nsegs,
263 1.1 jmcneill sc->sc_dmai.size, &sc->sc_dmai.addr,
264 1.1 jmcneill BUS_DMA_NOWAIT);
265 1.1 jmcneill if (rv)
266 1.1 jmcneill panic("crmfb_attach: can't map DMA memory");
267 1.1 jmcneill rv = bus_dmamap_create(sc->sc_dmat, sc->sc_dmai.size, 1,
268 1.1 jmcneill sc->sc_dmai.size, 0, BUS_DMA_NOWAIT, &sc->sc_dmai.map);
269 1.1 jmcneill if (rv)
270 1.1 jmcneill panic("crmfb_attach: can't create DMA map");
271 1.1 jmcneill rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dmai.map, sc->sc_dmai.addr,
272 1.1 jmcneill sc->sc_dmai.size, NULL, BUS_DMA_NOWAIT);
273 1.1 jmcneill if (rv)
274 1.1 jmcneill panic("crmfb_attach: can't load DMA map");
275 1.1 jmcneill
276 1.1 jmcneill sc->sc_dma.size = sc->sc_fbsize;
277 1.1 jmcneill rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dma.size, 65536, 0,
278 1.1 jmcneill sc->sc_dma.segs,
279 1.1 jmcneill sizeof(sc->sc_dma.segs) / sizeof(sc->sc_dma.segs[0]),
280 1.1 jmcneill &sc->sc_dma.nsegs, BUS_DMA_NOWAIT);
281 1.1 jmcneill if (rv)
282 1.1 jmcneill panic("crmfb_attach: can't allocate DMA memory");
283 1.1 jmcneill rv = bus_dmamem_map(sc->sc_dmat, sc->sc_dma.segs, sc->sc_dma.nsegs,
284 1.1 jmcneill sc->sc_dma.size, &sc->sc_dma.addr,
285 1.1 jmcneill BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
286 1.1 jmcneill if (rv)
287 1.1 jmcneill panic("crmfb_attach: can't map DMA memory");
288 1.1 jmcneill rv = bus_dmamap_create(sc->sc_dmat, sc->sc_dma.size, 1,
289 1.1 jmcneill sc->sc_dma.size, 0, BUS_DMA_NOWAIT, &sc->sc_dma.map);
290 1.1 jmcneill if (rv)
291 1.1 jmcneill panic("crmfb_attach: can't create DMA map");
292 1.1 jmcneill rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dma.map, sc->sc_dma.addr,
293 1.1 jmcneill sc->sc_dma.size, NULL, BUS_DMA_NOWAIT);
294 1.1 jmcneill if (rv)
295 1.1 jmcneill panic("crmfb_attach: can't load DMA map");
296 1.1 jmcneill
297 1.1 jmcneill p = KERNADDR(sc->sc_dmai);
298 1.1 jmcneill v = (unsigned long)DMAADDR(sc->sc_dma);
299 1.1 jmcneill for (i = 0; i < (sc->sc_fbsize >> 16); i++) {
300 1.1 jmcneill p[i] = ((uint32_t)v >> 16) + i;
301 1.1 jmcneill }
302 1.1 jmcneill
303 1.1 jmcneill memset(KERNADDR(sc->sc_dma), 0x00, sc->sc_fbsize);
304 1.1 jmcneill
305 1.1 jmcneill printf("%s: allocated %d byte fb @ %p (%p)\n", sc->sc_dev.dv_xname,
306 1.1 jmcneill sc->sc_fbsize, KERNADDR(sc->sc_dmai), KERNADDR(sc->sc_dma));
307 1.1 jmcneill
308 1.3 jmcneill #ifdef CRMFB_SHADOWFB
309 1.3 jmcneill /* setup shadow framebuffer */
310 1.3 jmcneill sc->sc_shadowfb = malloc(sc->sc_fbsize, M_DEVBUF, M_NOWAIT | M_ZERO);
311 1.3 jmcneill if (sc->sc_shadowfb == NULL)
312 1.3 jmcneill aprint_error("%s: WARNING: couldn't allocate shadow fb\n",
313 1.3 jmcneill sc->sc_dev.dv_xname);
314 1.3 jmcneill #endif
315 1.3 jmcneill
316 1.1 jmcneill ri = &crmfb_console_screen.scr_ri;
317 1.1 jmcneill memset(ri, 0, sizeof(struct rasops_info));
318 1.1 jmcneill
319 1.1 jmcneill vcons_init(&sc->sc_vd, sc, &crmfb_defaultscreen, &crmfb_accessops);
320 1.1 jmcneill sc->sc_vd.init_screen = crmfb_init_screen;
321 1.1 jmcneill crmfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
322 1.1 jmcneill vcons_init_screen(&sc->sc_vd, &crmfb_console_screen, 1, &defattr);
323 1.1 jmcneill
324 1.1 jmcneill crmfb_defaultscreen.ncols = ri->ri_cols;
325 1.1 jmcneill crmfb_defaultscreen.nrows = ri->ri_rows;
326 1.1 jmcneill crmfb_defaultscreen.textops = &ri->ri_ops;
327 1.1 jmcneill crmfb_defaultscreen.capabilities = ri->ri_caps;
328 1.1 jmcneill crmfb_defaultscreen.modecookie = NULL;
329 1.1 jmcneill
330 1.1 jmcneill /* disable DMA */
331 1.1 jmcneill d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_CONTROL);
332 1.1 jmcneill d &= ~(1 << CRMFB_OVR_CONTROL_DMAEN_SHIFT);
333 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_CONTROL, d);
334 1.1 jmcneill DELAY(10000);
335 1.1 jmcneill d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
336 1.1 jmcneill d &= ~(1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
337 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL, d);
338 1.1 jmcneill DELAY(10000);
339 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_DID_CONTROL, 0);
340 1.1 jmcneill DELAY(10000);
341 1.1 jmcneill
342 1.1 jmcneill /* ensure that CRM starts drawing at the top left of the screen
343 1.1 jmcneill * when we re-enable DMA later
344 1.1 jmcneill */
345 1.1 jmcneill d = (1 << CRMFB_VT_XY_FREEZE_SHIFT);
346 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_XY, d);
347 1.1 jmcneill d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK);
348 1.1 jmcneill d &= ~(1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT);
349 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK, d);
350 1.1 jmcneill
351 1.1 jmcneill /* reset FIFO */
352 1.1 jmcneill d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE);
353 1.1 jmcneill d |= (1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT);
354 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE, d);
355 1.1 jmcneill d &= ~(1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT);
356 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE, d);
357 1.1 jmcneill
358 1.1 jmcneill /* setup colour mode */
359 1.1 jmcneill switch (sc->sc_depth) {
360 1.1 jmcneill case 8:
361 1.1 jmcneill h = CRMFB_MODE_TYP_I8;
362 1.1 jmcneill break;
363 1.1 jmcneill case 16:
364 1.1 jmcneill h = CRMFB_MODE_TYP_ARGB5;
365 1.1 jmcneill break;
366 1.1 jmcneill case 32:
367 1.1 jmcneill h = CRMFB_MODE_TYP_RGB8;
368 1.1 jmcneill break;
369 1.1 jmcneill default:
370 1.1 jmcneill panic("Unsupported depth");
371 1.1 jmcneill }
372 1.1 jmcneill d = h << CRMFB_MODE_TYP_SHIFT;
373 1.1 jmcneill d |= CRMFB_MODE_BUF_BOTH << CRMFB_MODE_BUF_SHIFT;
374 1.1 jmcneill for (i = 0; i < (32 * 4); i += 4)
375 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_MODE + i, d);
376 1.1 jmcneill
377 1.1 jmcneill /* setup tile pointer, but don't turn on DMA yet! */
378 1.1 jmcneill h = DMAADDR(sc->sc_dmai);
379 1.1 jmcneill d = (h >> 9) << CRMFB_FRM_CONTROL_TILEPTR_SHIFT;
380 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL, d);
381 1.1 jmcneill
382 1.1 jmcneill /* init framebuffer width and pixel size */
383 1.1 jmcneill d = (1 << CRMFB_FRM_TILESIZE_WIDTH_SHIFT);
384 1.1 jmcneill switch (sc->sc_depth) {
385 1.1 jmcneill case 8:
386 1.1 jmcneill h = CRMFB_FRM_TILESIZE_DEPTH_8;
387 1.1 jmcneill break;
388 1.1 jmcneill case 16:
389 1.1 jmcneill h = CRMFB_FRM_TILESIZE_DEPTH_16;
390 1.1 jmcneill break;
391 1.1 jmcneill case 32:
392 1.1 jmcneill h = CRMFB_FRM_TILESIZE_DEPTH_32;
393 1.1 jmcneill break;
394 1.1 jmcneill default:
395 1.1 jmcneill panic("Unsupported depth");
396 1.1 jmcneill }
397 1.1 jmcneill d |= (h << CRMFB_FRM_TILESIZE_DEPTH_SHIFT);
398 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE, d);
399 1.1 jmcneill
400 1.1 jmcneill /* init framebuffer height, we use the trick that the Linux
401 1.1 jmcneill * driver uses to fool the CRM out of tiled mode and into
402 1.1 jmcneill * linear mode
403 1.1 jmcneill */
404 1.1 jmcneill h = sc->sc_width * sc->sc_height / (512 / (sc->sc_depth / 8));
405 1.1 jmcneill d = h << CRMFB_FRM_PIXSIZE_HEIGHT_SHIFT;
406 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_PIXSIZE, d);
407 1.1 jmcneill
408 1.1 jmcneill /* turn off firmware overlay and hardware cursor */
409 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_WIDTH_TILE, 0);
410 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_CRS_CONTROL, 0);
411 1.1 jmcneill
412 1.1 jmcneill /* enable drawing again */
413 1.1 jmcneill d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK);
414 1.1 jmcneill d |= (1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT);
415 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK, d);
416 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_XY, 0);
417 1.1 jmcneill
418 1.1 jmcneill /* turn on DMA for the framebuffer */
419 1.1 jmcneill d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
420 1.1 jmcneill d |= (1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
421 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL, d);
422 1.1 jmcneill
423 1.1 jmcneill for (i = 0; i < 256; i++) {
424 1.1 jmcneill crmfb_set_palette(sc, i, rasops_cmap[(i * 3) + 2],
425 1.1 jmcneill rasops_cmap[(i * 3) + 1], rasops_cmap[(i * 3) + 0]);
426 1.1 jmcneill sc->sc_cmap_red[i] = rasops_cmap[(i * 3) + 2];
427 1.1 jmcneill sc->sc_cmap_green[i] = rasops_cmap[(i * 3) + 1];
428 1.1 jmcneill sc->sc_cmap_blue[i] = rasops_cmap[(i * 3) + 0];
429 1.1 jmcneill }
430 1.1 jmcneill
431 1.2 jmcneill consdev = ARCBIOS->GetEnvironmentVariable("ConsoleOut");
432 1.2 jmcneill if (consdev != NULL && strcmp(consdev, "video()") == 0) {
433 1.2 jmcneill wsdisplay_cnattach(&crmfb_defaultscreen, ri, 0, 0, defattr);
434 1.2 jmcneill aa.console = 1;
435 1.2 jmcneill } else
436 1.2 jmcneill aa.console = 0;
437 1.1 jmcneill aa.scrdata = &crmfb_screenlist;
438 1.1 jmcneill aa.accessops = &crmfb_accessops;
439 1.1 jmcneill aa.accesscookie = &sc->sc_vd;
440 1.1 jmcneill
441 1.1 jmcneill config_found(self, &aa, wsemuldisplaydevprint);
442 1.1 jmcneill
443 1.1 jmcneill return;
444 1.1 jmcneill }
445 1.1 jmcneill
446 1.1 jmcneill int
447 1.1 jmcneill crmfb_probe(void)
448 1.1 jmcneill {
449 1.1 jmcneill
450 1.1 jmcneill if (mach_type != MACH_SGI_IP32)
451 1.1 jmcneill return 0;
452 1.1 jmcneill
453 1.1 jmcneill return 1;
454 1.1 jmcneill }
455 1.1 jmcneill
456 1.1 jmcneill static int
457 1.1 jmcneill crmfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
458 1.1 jmcneill {
459 1.1 jmcneill struct vcons_data *vd;
460 1.1 jmcneill struct crmfb_softc *sc;
461 1.1 jmcneill struct vcons_screen *ms;
462 1.1 jmcneill struct wsdisplay_fbinfo *wdf;
463 1.1 jmcneill int nmode;
464 1.1 jmcneill
465 1.1 jmcneill vd = (struct vcons_data *)v;
466 1.1 jmcneill sc = (struct crmfb_softc *)vd->cookie;
467 1.1 jmcneill ms = (struct vcons_screen *)vd->active;
468 1.1 jmcneill
469 1.1 jmcneill switch (cmd) {
470 1.1 jmcneill case WSDISPLAYIO_GTYPE:
471 1.1 jmcneill /* not really, but who cares? */
472 1.1 jmcneill *(u_int *)data = WSDISPLAY_TYPE_NEWPORT;
473 1.1 jmcneill return 0;
474 1.1 jmcneill case WSDISPLAYIO_GINFO:
475 1.1 jmcneill if (vd->active != NULL) {
476 1.1 jmcneill wdf = (void *)data;
477 1.1 jmcneill wdf->height = sc->sc_height;
478 1.1 jmcneill wdf->width = sc->sc_width;
479 1.1 jmcneill wdf->depth = sc->sc_depth;
480 1.1 jmcneill wdf->cmsize = 256;
481 1.1 jmcneill return 0;
482 1.1 jmcneill } else
483 1.1 jmcneill return ENODEV;
484 1.1 jmcneill case WSDISPLAYIO_GETCMAP:
485 1.1 jmcneill if (sc->sc_depth == 8)
486 1.1 jmcneill return crmfb_getcmap(sc, (struct wsdisplay_cmap *)data);
487 1.1 jmcneill else
488 1.1 jmcneill return EINVAL;
489 1.1 jmcneill case WSDISPLAYIO_PUTCMAP:
490 1.1 jmcneill if (sc->sc_depth == 8)
491 1.1 jmcneill return crmfb_putcmap(sc, (struct wsdisplay_cmap *)data);
492 1.1 jmcneill else
493 1.1 jmcneill return EINVAL;
494 1.1 jmcneill case WSDISPLAYIO_LINEBYTES:
495 1.1 jmcneill *(u_int *)data = sc->sc_width * sc->sc_depth / 8;
496 1.1 jmcneill return 0;
497 1.1 jmcneill case WSDISPLAYIO_SMODE:
498 1.1 jmcneill nmode = *(int *)data;
499 1.1 jmcneill if (nmode != sc->sc_wsmode) {
500 1.1 jmcneill sc->sc_wsmode = nmode;
501 1.1 jmcneill if (nmode == WSDISPLAYIO_MODE_EMUL)
502 1.1 jmcneill vcons_redraw_screen(vd->active);
503 1.1 jmcneill }
504 1.1 jmcneill return 0;
505 1.1 jmcneill }
506 1.1 jmcneill
507 1.1 jmcneill return EPASSTHROUGH;
508 1.1 jmcneill }
509 1.1 jmcneill
510 1.1 jmcneill static paddr_t
511 1.1 jmcneill crmfb_mmap(void *v, void *vs, off_t offset, int prot)
512 1.1 jmcneill {
513 1.1 jmcneill struct vcons_data *vd;
514 1.1 jmcneill struct crmfb_softc *sc;
515 1.1 jmcneill paddr_t pa;
516 1.1 jmcneill
517 1.1 jmcneill vd = (struct vcons_data *)v;
518 1.1 jmcneill sc = (struct crmfb_softc *)vd->cookie;
519 1.1 jmcneill
520 1.1 jmcneill if (offset >= 0 && offset < sc->sc_fbsize) {
521 1.1 jmcneill pa = bus_dmamem_mmap(sc->sc_dmat, sc->sc_dma.segs,
522 1.1 jmcneill sc->sc_dma.nsegs, offset, prot,
523 1.3 jmcneill BUS_DMA_WAITOK | BUS_DMA_COHERENT);
524 1.1 jmcneill return pa;
525 1.1 jmcneill }
526 1.1 jmcneill
527 1.1 jmcneill return -1;
528 1.1 jmcneill }
529 1.1 jmcneill
530 1.1 jmcneill static void
531 1.1 jmcneill crmfb_init_screen(void *c, struct vcons_screen *scr, int existing,
532 1.1 jmcneill long *defattr)
533 1.1 jmcneill {
534 1.1 jmcneill struct crmfb_softc *sc;
535 1.1 jmcneill struct rasops_info *ri;
536 1.1 jmcneill
537 1.1 jmcneill sc = (struct crmfb_softc *)c;
538 1.1 jmcneill ri = &scr->scr_ri;
539 1.1 jmcneill
540 1.1 jmcneill ri->ri_flg = RI_CENTER;
541 1.1 jmcneill ri->ri_depth = sc->sc_depth;
542 1.1 jmcneill ri->ri_width = sc->sc_width;
543 1.1 jmcneill ri->ri_height = sc->sc_height;
544 1.1 jmcneill ri->ri_stride = ri->ri_width * (ri->ri_depth / 8);
545 1.1 jmcneill
546 1.1 jmcneill switch (ri->ri_depth) {
547 1.1 jmcneill case 16:
548 1.1 jmcneill ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 5;
549 1.1 jmcneill ri->ri_rpos = 10;
550 1.1 jmcneill ri->ri_gpos = 5;
551 1.1 jmcneill ri->ri_bpos = 0;
552 1.1 jmcneill break;
553 1.1 jmcneill case 32:
554 1.1 jmcneill ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
555 1.1 jmcneill ri->ri_rpos = 8;
556 1.1 jmcneill ri->ri_gpos = 16;
557 1.1 jmcneill ri->ri_bpos = 24;
558 1.1 jmcneill break;
559 1.1 jmcneill }
560 1.1 jmcneill
561 1.3 jmcneill if (sc->sc_shadowfb == NULL)
562 1.3 jmcneill ri->ri_bits = KERNADDR(sc->sc_dma);
563 1.3 jmcneill else {
564 1.3 jmcneill ri->ri_bits = sc->sc_shadowfb;
565 1.3 jmcneill ri->ri_hwbits = KERNADDR(sc->sc_dma);
566 1.3 jmcneill }
567 1.1 jmcneill
568 1.1 jmcneill if (existing)
569 1.1 jmcneill ri->ri_flg |= RI_CLEAR;
570 1.1 jmcneill
571 1.1 jmcneill rasops_init(ri, ri->ri_height / 16, ri->ri_width / 8);
572 1.1 jmcneill ri->ri_caps = WSSCREEN_WSCOLORS;
573 1.1 jmcneill rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
574 1.1 jmcneill ri->ri_width / ri->ri_font->fontwidth);
575 1.1 jmcneill ri->ri_hw = scr;
576 1.1 jmcneill
577 1.1 jmcneill return;
578 1.1 jmcneill }
579 1.1 jmcneill
580 1.1 jmcneill static int
581 1.1 jmcneill crmfb_putcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm)
582 1.1 jmcneill {
583 1.1 jmcneill u_int idx, cnt;
584 1.1 jmcneill u_char r[256], g[256], b[256];
585 1.1 jmcneill u_char *rp, *gp, *bp;
586 1.1 jmcneill int rv, i;
587 1.1 jmcneill
588 1.1 jmcneill idx = cm->index;
589 1.1 jmcneill cnt = cm->count;
590 1.1 jmcneill
591 1.1 jmcneill if (idx >= 255 || cnt > 256 || idx + cnt > 256)
592 1.1 jmcneill return EINVAL;
593 1.1 jmcneill
594 1.1 jmcneill rv = copyin(cm->red, &r[idx], cnt);
595 1.1 jmcneill if (rv)
596 1.1 jmcneill return rv;
597 1.1 jmcneill rv = copyin(cm->green, &g[idx], cnt);
598 1.1 jmcneill if (rv)
599 1.1 jmcneill return rv;
600 1.1 jmcneill rv = copyin(cm->blue, &b[idx], cnt);
601 1.1 jmcneill if (rv)
602 1.1 jmcneill return rv;
603 1.1 jmcneill
604 1.1 jmcneill memcpy(&sc->sc_cmap_red[idx], &r[idx], cnt);
605 1.1 jmcneill memcpy(&sc->sc_cmap_green[idx], &g[idx], cnt);
606 1.1 jmcneill memcpy(&sc->sc_cmap_blue[idx], &b[idx], cnt);
607 1.1 jmcneill
608 1.1 jmcneill rp = &sc->sc_cmap_red[idx];
609 1.1 jmcneill gp = &sc->sc_cmap_green[idx];
610 1.1 jmcneill bp = &sc->sc_cmap_blue[idx];
611 1.1 jmcneill
612 1.1 jmcneill for (i = 0; i < cnt; i++) {
613 1.1 jmcneill crmfb_set_palette(sc, idx, *rp, *gp, *bp);
614 1.1 jmcneill idx++;
615 1.1 jmcneill rp++, gp++, bp++;
616 1.1 jmcneill }
617 1.1 jmcneill
618 1.1 jmcneill return 0;
619 1.1 jmcneill }
620 1.1 jmcneill
621 1.1 jmcneill static int
622 1.1 jmcneill crmfb_getcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm)
623 1.1 jmcneill {
624 1.1 jmcneill u_int idx, cnt;
625 1.1 jmcneill int rv;
626 1.1 jmcneill
627 1.1 jmcneill idx = cm->index;
628 1.1 jmcneill cnt = cm->count;
629 1.1 jmcneill
630 1.1 jmcneill if (idx >= 255 || cnt > 256 || idx + cnt > 256)
631 1.1 jmcneill return EINVAL;
632 1.1 jmcneill
633 1.1 jmcneill rv = copyout(&sc->sc_cmap_red[idx], cm->red, cnt);
634 1.1 jmcneill if (rv)
635 1.1 jmcneill return rv;
636 1.1 jmcneill rv = copyout(&sc->sc_cmap_green[idx], cm->green, cnt);
637 1.1 jmcneill if (rv)
638 1.1 jmcneill return rv;
639 1.1 jmcneill rv = copyout(&sc->sc_cmap_blue[idx], cm->blue, cnt);
640 1.1 jmcneill if (rv)
641 1.1 jmcneill return rv;
642 1.1 jmcneill
643 1.1 jmcneill return 0;
644 1.1 jmcneill }
645 1.1 jmcneill
646 1.1 jmcneill static void
647 1.1 jmcneill crmfb_set_palette(struct crmfb_softc *sc, int reg, uint8_t r, uint8_t g,
648 1.1 jmcneill uint8_t b)
649 1.1 jmcneill {
650 1.1 jmcneill uint32_t val;
651 1.1 jmcneill
652 1.1 jmcneill if (reg > 255 || sc->sc_depth != 8)
653 1.1 jmcneill return;
654 1.1 jmcneill
655 1.1 jmcneill while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_CMAP_FIFO) >= 63)
656 1.1 jmcneill DELAY(10);
657 1.1 jmcneill
658 1.1 jmcneill val = (r << 24) | (g << 16) | (b << 8);
659 1.1 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_CMAP + (reg * 4), val);
660 1.1 jmcneill
661 1.1 jmcneill return;
662 1.1 jmcneill }
663