crmfb.c revision 1.31 1 /* $NetBSD: crmfb.c,v 1.31 2011/04/04 22:50:36 macallan Exp $ */
2
3 /*-
4 * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * 2008 Michael Lorenz <macallan (at) netbsd.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*
31 * SGI-CRM (O2) Framebuffer driver
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: crmfb.c,v 1.31 2011/04/04 22:50:36 macallan Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/malloc.h>
41
42 #define _SGIMIPS_BUS_DMA_PRIVATE
43 #include <machine/autoconf.h>
44 #include <machine/bus.h>
45 #include <machine/machtype.h>
46 #include <machine/vmparam.h>
47
48 #include <dev/arcbios/arcbios.h>
49 #include <dev/arcbios/arcbiosvar.h>
50
51 #include <dev/wscons/wsdisplayvar.h>
52 #include <dev/wscons/wsconsio.h>
53 #include <dev/wsfont/wsfont.h>
54 #include <dev/rasops/rasops.h>
55 #include <dev/wscons/wsdisplay_vconsvar.h>
56
57 #include <dev/i2c/i2cvar.h>
58 #include <dev/i2c/i2c_bitbang.h>
59 #include <dev/i2c/ddcvar.h>
60 #include <dev/videomode/videomode.h>
61 #include <dev/videomode/edidvar.h>
62
63 #include <arch/sgimips/dev/crmfbreg.h>
64
65 /*#define CRMFB_DEBUG*/
66
67 struct wsscreen_descr crmfb_defaultscreen = {
68 "default",
69 0, 0,
70 NULL,
71 8, 16,
72 WSSCREEN_WSCOLORS,
73 NULL,
74 };
75
76 const struct wsscreen_descr *_crmfb_scrlist[] = {
77 &crmfb_defaultscreen,
78 };
79
80 struct wsscreen_list crmfb_screenlist = {
81 sizeof(_crmfb_scrlist) / sizeof(struct wsscreen_descr *),
82 _crmfb_scrlist
83 };
84
85 static struct vcons_screen crmfb_console_screen;
86
87 static int crmfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
88 static paddr_t crmfb_mmap(void *, void *, off_t, int);
89 static void crmfb_init_screen(void *, struct vcons_screen *, int, long *);
90
91 struct wsdisplay_accessops crmfb_accessops = {
92 crmfb_ioctl,
93 crmfb_mmap,
94 NULL, /* alloc_screen */
95 NULL, /* free_screen */
96 NULL, /* show_screen */
97 NULL, /* load_font */
98 NULL, /* pollc */
99 NULL, /* scroll */
100 };
101
102 /* Memory to allocate to SGI-CRM -- remember, this is stolen from
103 * host memory!
104 */
105 #define CRMFB_TILESIZE (512*128)
106
107 static int crmfb_match(device_t, struct cfdata *, void *);
108 static void crmfb_attach(device_t, device_t, void *);
109 int crmfb_probe(void);
110
111 #define KERNADDR(p) ((void *)((p).addr))
112 #define DMAADDR(p) ((p).map->dm_segs[0].ds_addr)
113
114 #define CRMFB_REG_MASK(msb, lsb) \
115 ( (((uint32_t) 1 << ((msb)-(lsb)+1)) - 1) << (lsb) )
116
117
118 struct crmfb_dma {
119 bus_dmamap_t map;
120 void *addr;
121 bus_dma_segment_t segs[1];
122 int nsegs;
123 size_t size;
124 };
125
126 struct crmfb_softc {
127 device_t sc_dev;
128 struct vcons_data sc_vd;
129 struct i2c_controller sc_i2c;
130 int sc_dir;
131
132 bus_space_tag_t sc_iot;
133 bus_space_handle_t sc_ioh;
134 bus_space_handle_t sc_reh;
135
136 bus_dma_tag_t sc_dmat;
137
138 struct crmfb_dma sc_dma;
139 struct crmfb_dma sc_dmai;
140
141 int sc_width;
142 int sc_height;
143 int sc_depth;
144 int sc_tiles_x, sc_tiles_y;
145 uint32_t sc_fbsize;
146 int sc_mte_direction;
147 uint8_t *sc_scratch;
148 paddr_t sc_linear;
149 int sc_wsmode;
150
151 /* cursor stuff */
152 int sc_cur_x;
153 int sc_cur_y;
154 int sc_hot_x;
155 int sc_hot_y;
156
157 u_char sc_cmap_red[256];
158 u_char sc_cmap_green[256];
159 u_char sc_cmap_blue[256];
160 };
161
162 static int crmfb_putcmap(struct crmfb_softc *, struct wsdisplay_cmap *);
163 static int crmfb_getcmap(struct crmfb_softc *, struct wsdisplay_cmap *);
164 static void crmfb_set_palette(struct crmfb_softc *,
165 int, uint8_t, uint8_t, uint8_t);
166 static int crmfb_set_curpos(struct crmfb_softc *, int, int);
167 static int crmfb_gcursor(struct crmfb_softc *, struct wsdisplay_cursor *);
168 static int crmfb_scursor(struct crmfb_softc *, struct wsdisplay_cursor *);
169 static inline void crmfb_write_reg(struct crmfb_softc *, int, uint32_t);
170 static inline uint32_t crmfb_read_reg(struct crmfb_softc *, int);
171 static int crmfb_wait_dma_idle(struct crmfb_softc *);
172
173 /* setup video hw in given colour depth */
174 static int crmfb_setup_video(struct crmfb_softc *, int);
175 static void crmfb_setup_palette(struct crmfb_softc *);
176
177 static void crmfb_fill_rect(struct crmfb_softc *, int, int, int, int, uint32_t);
178 static void crmfb_bitblt(struct crmfb_softc *, int, int, int, int, int, int,
179 uint32_t);
180 static void crmfb_scroll(struct crmfb_softc *, int, int, int, int, int, int);
181
182 static void crmfb_copycols(void *, int, int, int, int);
183 static void crmfb_erasecols(void *, int, int, int, long);
184 static void crmfb_copyrows(void *, int, int, int);
185 static void crmfb_eraserows(void *, int, int, long);
186 static void crmfb_cursor(void *, int, int, int);
187 static void crmfb_putchar(void *, int, int, u_int, long);
188
189 /* I2C glue */
190 static int crmfb_i2c_acquire_bus(void *, int);
191 static void crmfb_i2c_release_bus(void *, int);
192 static int crmfb_i2c_send_start(void *, int);
193 static int crmfb_i2c_send_stop(void *, int);
194 static int crmfb_i2c_initiate_xfer(void *, i2c_addr_t, int);
195 static int crmfb_i2c_read_byte(void *, uint8_t *, int);
196 static int crmfb_i2c_write_byte(void *, uint8_t, int);
197
198 /* I2C bitbang glue */
199 static void crmfb_i2cbb_set_bits(void *, uint32_t);
200 static void crmfb_i2cbb_set_dir(void *, uint32_t);
201 static uint32_t crmfb_i2cbb_read(void *);
202
203 static const struct i2c_bitbang_ops crmfb_i2cbb_ops = {
204 crmfb_i2cbb_set_bits,
205 crmfb_i2cbb_set_dir,
206 crmfb_i2cbb_read,
207 {
208 CRMFB_I2C_SDA,
209 CRMFB_I2C_SCL,
210 0,
211 1
212 }
213 };
214 static void crmfb_setup_ddc(struct crmfb_softc *);
215
216 CFATTACH_DECL_NEW(crmfb, sizeof(struct crmfb_softc),
217 crmfb_match, crmfb_attach, NULL, NULL);
218
219 static int
220 crmfb_match(device_t parent, struct cfdata *cf, void *opaque)
221 {
222 return crmfb_probe();
223 }
224
225 static void
226 crmfb_attach(device_t parent, device_t self, void *opaque)
227 {
228 struct mainbus_attach_args *ma;
229 struct crmfb_softc *sc;
230 struct rasops_info *ri;
231 struct wsemuldisplaydev_attach_args aa;
232 uint32_t d, h;
233 uint16_t *p;
234 unsigned long v;
235 long defattr;
236 const char *consdev;
237 int rv, i;
238
239 sc = device_private(self);
240 sc->sc_dev = self;
241
242 ma = (struct mainbus_attach_args *)opaque;
243
244 sc->sc_iot = SGIMIPS_BUS_SPACE_CRIME;
245 sc->sc_dmat = &sgimips_default_bus_dma_tag;
246 sc->sc_wsmode = WSDISPLAYIO_MODE_EMUL;
247
248 aprint_normal(": SGI CRIME Graphics Display Engine\n");
249 rv = bus_space_map(sc->sc_iot, ma->ma_addr, 0 /* XXX */,
250 BUS_SPACE_MAP_LINEAR, &sc->sc_ioh);
251 if (rv)
252 panic("crmfb_attach: can't map I/O space");
253 rv = bus_space_map(sc->sc_iot, 0x15000000, 0x6000, 0, &sc->sc_reh);
254 if (rv)
255 panic("crmfb_attach: can't map rendering engine");
256
257 //crmfb_setup_ddc(sc);
258
259 /* determine mode configured by firmware */
260 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_HCMAP);
261 sc->sc_width = (d >> CRMFB_VT_HCMAP_ON_SHIFT) & 0xfff;
262 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_VCMAP);
263 sc->sc_height = (d >> CRMFB_VT_VCMAP_ON_SHIFT) & 0xfff;
264 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE);
265 h = (d >> CRMFB_FRM_TILESIZE_DEPTH_SHIFT) & 0x3;
266 if (h == 0)
267 sc->sc_depth = 8;
268 else if (h == 1)
269 sc->sc_depth = 16;
270 else
271 sc->sc_depth = 32;
272
273 if (sc->sc_width == 0 || sc->sc_height == 0) {
274 aprint_error_dev(sc->sc_dev,
275 "device unusable if not setup by firmware\n");
276 bus_space_unmap(sc->sc_iot, sc->sc_ioh, 0 /* XXX */);
277 return;
278 }
279
280 aprint_normal_dev(sc->sc_dev, "initial resolution %dx%d\n",
281 sc->sc_width, sc->sc_height);
282
283 /*
284 * first determine how many tiles we need
285 * in 32bit each tile is 128x128 pixels
286 */
287 sc->sc_tiles_x = (sc->sc_width + 127) >> 7;
288 sc->sc_tiles_y = (sc->sc_height + 127) >> 7;
289 sc->sc_fbsize = 0x10000 * sc->sc_tiles_x * sc->sc_tiles_y;
290
291 sc->sc_dmai.size = 256 * sizeof(uint16_t);
292 rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dmai.size, 65536, 0,
293 sc->sc_dmai.segs,
294 sizeof(sc->sc_dmai.segs) / sizeof(sc->sc_dmai.segs[0]),
295 &sc->sc_dmai.nsegs, BUS_DMA_NOWAIT);
296 if (rv)
297 panic("crmfb_attach: can't allocate DMA memory");
298 rv = bus_dmamem_map(sc->sc_dmat, sc->sc_dmai.segs, sc->sc_dmai.nsegs,
299 sc->sc_dmai.size, &sc->sc_dmai.addr,
300 BUS_DMA_NOWAIT);
301 if (rv)
302 panic("crmfb_attach: can't map DMA memory");
303 rv = bus_dmamap_create(sc->sc_dmat, sc->sc_dmai.size, 1,
304 sc->sc_dmai.size, 0, BUS_DMA_NOWAIT, &sc->sc_dmai.map);
305 if (rv)
306 panic("crmfb_attach: can't create DMA map");
307 rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dmai.map, sc->sc_dmai.addr,
308 sc->sc_dmai.size, NULL, BUS_DMA_NOWAIT);
309 if (rv)
310 panic("crmfb_attach: can't load DMA map");
311
312 /* allocate an extra 64Kb for a linear buffer */
313 sc->sc_dma.size = 0x10000 * (16 * sc->sc_tiles_x + 1);
314 rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dma.size, 65536, 0,
315 sc->sc_dma.segs,
316 sizeof(sc->sc_dma.segs) / sizeof(sc->sc_dma.segs[0]),
317 &sc->sc_dma.nsegs, BUS_DMA_NOWAIT);
318 if (rv)
319 panic("crmfb_attach: can't allocate DMA memory");
320 rv = bus_dmamem_map(sc->sc_dmat, sc->sc_dma.segs, sc->sc_dma.nsegs,
321 sc->sc_dma.size, &sc->sc_dma.addr,
322 BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
323 if (rv)
324 panic("crmfb_attach: can't map DMA memory");
325 rv = bus_dmamap_create(sc->sc_dmat, sc->sc_dma.size, 1,
326 sc->sc_dma.size, 0, BUS_DMA_NOWAIT, &sc->sc_dma.map);
327 if (rv)
328 panic("crmfb_attach: can't create DMA map");
329 rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dma.map, sc->sc_dma.addr,
330 sc->sc_dma.size, NULL, BUS_DMA_NOWAIT);
331 if (rv)
332 panic("crmfb_attach: can't load DMA map");
333
334 p = KERNADDR(sc->sc_dmai);
335 v = (unsigned long)DMAADDR(sc->sc_dma);
336 for (i = 0; i < (sc->sc_tiles_x * sc->sc_tiles_y); i++) {
337 p[i] = ((uint32_t)v >> 16) + i;
338 }
339 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmai.map, 0, sc->sc_dmai.size,
340 BUS_DMASYNC_PREWRITE);
341 sc->sc_scratch = (char *)KERNADDR(sc->sc_dma) + (0xf0000 * sc->sc_tiles_x);
342 sc->sc_linear = (paddr_t)DMAADDR(sc->sc_dma) + 0x100000 * sc->sc_tiles_x;
343
344 aprint_normal_dev(sc->sc_dev, "allocated %d byte fb @ %p (%p)\n",
345 sc->sc_fbsize, KERNADDR(sc->sc_dmai), KERNADDR(sc->sc_dma));
346
347 crmfb_setup_video(sc, 8);
348 ri = &crmfb_console_screen.scr_ri;
349 memset(ri, 0, sizeof(struct rasops_info));
350
351 vcons_init(&sc->sc_vd, sc, &crmfb_defaultscreen, &crmfb_accessops);
352 sc->sc_vd.init_screen = crmfb_init_screen;
353 crmfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
354 vcons_init_screen(&sc->sc_vd, &crmfb_console_screen, 1, &defattr);
355
356 crmfb_defaultscreen.ncols = ri->ri_cols;
357 crmfb_defaultscreen.nrows = ri->ri_rows;
358 crmfb_defaultscreen.textops = &ri->ri_ops;
359 crmfb_defaultscreen.capabilities = ri->ri_caps;
360 crmfb_defaultscreen.modecookie = NULL;
361
362 crmfb_setup_palette(sc);
363 crmfb_fill_rect(sc, 0, 0, sc->sc_width, sc->sc_height,
364 ri->ri_devcmap[(defattr >> 16) & 0xff]);
365
366 consdev = arcbios_GetEnvironmentVariable("ConsoleOut");
367 if (consdev != NULL && strcmp(consdev, "video()") == 0) {
368 wsdisplay_cnattach(&crmfb_defaultscreen, ri, 0, 0, defattr);
369 vcons_replay_msgbuf(&crmfb_console_screen);
370 aa.console = 1;
371 } else
372 aa.console = 0;
373 aa.scrdata = &crmfb_screenlist;
374 aa.accessops = &crmfb_accessops;
375 aa.accesscookie = &sc->sc_vd;
376
377 config_found(self, &aa, wsemuldisplaydevprint);
378
379 sc->sc_cur_x = 0;
380 sc->sc_cur_y = 0;
381 sc->sc_hot_x = 0;
382 sc->sc_hot_y = 0;
383
384 crmfb_setup_ddc(sc);
385 return;
386 }
387
388 int
389 crmfb_probe(void)
390 {
391
392 if (mach_type != MACH_SGI_IP32)
393 return 0;
394
395 return 1;
396 }
397
398 static int
399 crmfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
400 {
401 struct vcons_data *vd;
402 struct crmfb_softc *sc;
403 struct vcons_screen *ms;
404 struct wsdisplay_fbinfo *wdf;
405 int nmode;
406
407 vd = (struct vcons_data *)v;
408 sc = (struct crmfb_softc *)vd->cookie;
409 ms = (struct vcons_screen *)vd->active;
410
411 switch (cmd) {
412 case WSDISPLAYIO_GTYPE:
413 /* not really, but who cares? */
414 /* wsfb does */
415 *(u_int *)data = WSDISPLAY_TYPE_CRIME;
416 return 0;
417 case WSDISPLAYIO_GINFO:
418 if (vd->active != NULL) {
419 wdf = (void *)data;
420 wdf->height = sc->sc_height;
421 wdf->width = sc->sc_width;
422 wdf->depth = 32;
423 wdf->cmsize = 256;
424 return 0;
425 } else
426 return ENODEV;
427 case WSDISPLAYIO_GETCMAP:
428 if (sc->sc_depth == 8)
429 return crmfb_getcmap(sc, (struct wsdisplay_cmap *)data);
430 else
431 return EINVAL;
432 case WSDISPLAYIO_PUTCMAP:
433 if (sc->sc_depth == 8)
434 return crmfb_putcmap(sc, (struct wsdisplay_cmap *)data);
435 else
436 return EINVAL;
437 case WSDISPLAYIO_LINEBYTES:
438 *(u_int *)data = sc->sc_width * sc->sc_depth / 8;
439 return 0;
440 case WSDISPLAYIO_SMODE:
441 nmode = *(int *)data;
442 if (nmode != sc->sc_wsmode) {
443 sc->sc_wsmode = nmode;
444 if (nmode == WSDISPLAYIO_MODE_EMUL) {
445 crmfb_setup_video(sc, 8);
446 crmfb_setup_palette(sc);
447 vcons_redraw_screen(vd->active);
448 } else {
449 crmfb_setup_video(sc, 32);
450 }
451 }
452 return 0;
453 case WSDISPLAYIO_SVIDEO:
454 case WSDISPLAYIO_GVIDEO:
455 return ENODEV; /* not supported yet */
456
457 case WSDISPLAYIO_GCURPOS:
458 {
459 struct wsdisplay_curpos *pos;
460
461 pos = (struct wsdisplay_curpos *)data;
462 pos->x = sc->sc_cur_x;
463 pos->y = sc->sc_cur_y;
464 }
465 return 0;
466 case WSDISPLAYIO_SCURPOS:
467 {
468 struct wsdisplay_curpos *pos;
469
470 pos = (struct wsdisplay_curpos *)data;
471 crmfb_set_curpos(sc, pos->x, pos->y);
472 }
473 return 0;
474 case WSDISPLAYIO_GCURMAX:
475 {
476 struct wsdisplay_curpos *pos;
477
478 pos = (struct wsdisplay_curpos *)data;
479 pos->x = 32;
480 pos->y = 32;
481 }
482 return 0;
483 case WSDISPLAYIO_GCURSOR:
484 {
485 struct wsdisplay_cursor *cu;
486
487 cu = (struct wsdisplay_cursor *)data;
488 return crmfb_gcursor(sc, cu);
489 }
490 case WSDISPLAYIO_SCURSOR:
491 {
492 struct wsdisplay_cursor *cu;
493
494 cu = (struct wsdisplay_cursor *)data;
495 return crmfb_scursor(sc, cu);
496 }
497 }
498 return EPASSTHROUGH;
499 }
500
501 static paddr_t
502 crmfb_mmap(void *v, void *vs, off_t offset, int prot)
503 {
504 struct vcons_data *vd;
505 struct crmfb_softc *sc;
506 paddr_t pa;
507
508 vd = (struct vcons_data *)v;
509 sc = (struct crmfb_softc *)vd->cookie;
510
511 /* we probably shouldn't let anyone mmap the framebuffer */
512 #if 1
513 if (offset >= 0 && offset < (0x100000 * sc->sc_tiles_x)) {
514 pa = bus_dmamem_mmap(sc->sc_dmat, sc->sc_dma.segs,
515 sc->sc_dma.nsegs, offset, prot,
516 BUS_DMA_WAITOK | BUS_DMA_COHERENT);
517 return pa;
518 }
519 #endif
520 /*
521 * here would the TLBs be but we don't want to show them to userland
522 * so we return the page containing the status register
523 */
524 if ((offset >= 0x15000000) && (offset < 0x15002000))
525 return bus_space_mmap(sc->sc_iot, 0x15004000, 0, prot, 0);
526 /* now the actual engine registers */
527 if ((offset >= 0x15002000) && (offset < 0x15005000))
528 return bus_space_mmap(sc->sc_iot, offset, 0, prot, 0);
529 /* and now the scratch area */
530 if ((offset >= 0x15010000) && (offset < 0x15020000))
531 return bus_dmamem_mmap(sc->sc_dmat, sc->sc_dma.segs,
532 sc->sc_dma.nsegs,
533 offset + (0x100000 * sc->sc_tiles_x) - 0x15010000,
534 prot, BUS_DMA_WAITOK | BUS_DMA_COHERENT);
535 return -1;
536 }
537
538 static void
539 crmfb_init_screen(void *c, struct vcons_screen *scr, int existing,
540 long *defattr)
541 {
542 struct crmfb_softc *sc;
543 struct rasops_info *ri;
544
545 sc = (struct crmfb_softc *)c;
546 ri = &scr->scr_ri;
547
548 ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
549 ri->ri_depth = sc->sc_depth;
550 ri->ri_width = sc->sc_width;
551 ri->ri_height = sc->sc_height;
552 ri->ri_stride = ri->ri_width * (ri->ri_depth / 8);
553
554 switch (ri->ri_depth) {
555 case 16:
556 ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 5;
557 ri->ri_rpos = 10;
558 ri->ri_gpos = 5;
559 ri->ri_bpos = 0;
560 break;
561 case 32:
562 ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
563 ri->ri_rpos = 8;
564 ri->ri_gpos = 16;
565 ri->ri_bpos = 24;
566 break;
567 }
568
569 ri->ri_bits = KERNADDR(sc->sc_dma);
570
571 if (existing)
572 ri->ri_flg |= RI_CLEAR;
573
574 rasops_init(ri, ri->ri_height / 16, ri->ri_width / 8);
575 ri->ri_caps = WSSCREEN_WSCOLORS;
576 rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
577 ri->ri_width / ri->ri_font->fontwidth);
578 ri->ri_hw = scr;
579
580 ri->ri_ops.cursor = crmfb_cursor;
581 ri->ri_ops.copyrows = crmfb_copyrows;
582 ri->ri_ops.eraserows = crmfb_eraserows;
583 ri->ri_ops.copycols = crmfb_copycols;
584 ri->ri_ops.erasecols = crmfb_erasecols;
585 ri->ri_ops.putchar = crmfb_putchar;
586
587 return;
588 }
589
590 static int
591 crmfb_putcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm)
592 {
593 u_int idx, cnt;
594 u_char r[256], g[256], b[256];
595 u_char *rp, *gp, *bp;
596 int rv, i;
597
598 idx = cm->index;
599 cnt = cm->count;
600
601 if (idx >= 255 || cnt > 256 || idx + cnt > 256)
602 return EINVAL;
603
604 rv = copyin(cm->red, &r[idx], cnt);
605 if (rv)
606 return rv;
607 rv = copyin(cm->green, &g[idx], cnt);
608 if (rv)
609 return rv;
610 rv = copyin(cm->blue, &b[idx], cnt);
611 if (rv)
612 return rv;
613
614 memcpy(&sc->sc_cmap_red[idx], &r[idx], cnt);
615 memcpy(&sc->sc_cmap_green[idx], &g[idx], cnt);
616 memcpy(&sc->sc_cmap_blue[idx], &b[idx], cnt);
617
618 rp = &sc->sc_cmap_red[idx];
619 gp = &sc->sc_cmap_green[idx];
620 bp = &sc->sc_cmap_blue[idx];
621
622 for (i = 0; i < cnt; i++) {
623 crmfb_set_palette(sc, idx, *rp, *gp, *bp);
624 idx++;
625 rp++, gp++, bp++;
626 }
627
628 return 0;
629 }
630
631 static int
632 crmfb_getcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm)
633 {
634 u_int idx, cnt;
635 int rv;
636
637 idx = cm->index;
638 cnt = cm->count;
639
640 if (idx >= 255 || cnt > 256 || idx + cnt > 256)
641 return EINVAL;
642
643 rv = copyout(&sc->sc_cmap_red[idx], cm->red, cnt);
644 if (rv)
645 return rv;
646 rv = copyout(&sc->sc_cmap_green[idx], cm->green, cnt);
647 if (rv)
648 return rv;
649 rv = copyout(&sc->sc_cmap_blue[idx], cm->blue, cnt);
650 if (rv)
651 return rv;
652
653 return 0;
654 }
655
656 static void
657 crmfb_set_palette(struct crmfb_softc *sc, int reg, uint8_t r, uint8_t g,
658 uint8_t b)
659 {
660 uint32_t val;
661
662 if (reg > 255 || sc->sc_depth != 8)
663 return;
664
665 while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_CMAP_FIFO) >= 63)
666 DELAY(10);
667
668 val = (r << 8) | (g << 16) | (b << 24);
669 crmfb_write_reg(sc, CRMFB_CMAP + (reg * 4), val);
670
671 return;
672 }
673
674 static int
675 crmfb_set_curpos(struct crmfb_softc *sc, int x, int y)
676 {
677 uint32_t val;
678
679 sc->sc_cur_x = x;
680 sc->sc_cur_y = y;
681
682 val = ((x - sc->sc_hot_x) & 0xffff) | ((y - sc->sc_hot_y) << 16);
683 crmfb_write_reg(sc, CRMFB_CURSOR_POS, val);
684
685 return 0;
686 }
687
688 static int
689 crmfb_gcursor(struct crmfb_softc *sc, struct wsdisplay_cursor *cur)
690 {
691 /* do nothing for now */
692 return 0;
693 }
694
695 static int
696 crmfb_scursor(struct crmfb_softc *sc, struct wsdisplay_cursor *cur)
697 {
698 if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
699
700 crmfb_write_reg(sc, CRMFB_CURSOR_CONTROL, cur->enable ? 1 : 0);
701 }
702 if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
703
704 sc->sc_hot_x = cur->hot.x;
705 sc->sc_hot_y = cur->hot.y;
706 }
707 if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
708
709 crmfb_set_curpos(sc, cur->pos.x, cur->pos.y);
710 }
711 if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
712 int i;
713 uint32_t val;
714
715 for (i = 0; i < cur->cmap.count; i++) {
716 val = (cur->cmap.red[i] << 24) |
717 (cur->cmap.green[i] << 16) |
718 (cur->cmap.blue[i] << 8);
719 crmfb_write_reg(sc, CRMFB_CURSOR_CMAP0 +
720 ((i + cur->cmap.index) << 2), val);
721 }
722 }
723 if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
724
725 int i, j, cnt = 0;
726 uint32_t latch = 0, omask;
727 uint8_t imask;
728 for (i = 0; i < 64; i++) {
729 omask = 0x80000000;
730 imask = 0x01;
731 cur->image[cnt] &= cur->mask[cnt];
732 for (j = 0; j < 8; j++) {
733 if (cur->image[cnt] & imask)
734 latch |= omask;
735 omask >>= 1;
736 if (cur->mask[cnt] & imask)
737 latch |= omask;
738 omask >>= 1;
739 imask <<= 1;
740 }
741 cnt++;
742 imask = 0x01;
743 cur->image[cnt] &= cur->mask[cnt];
744 for (j = 0; j < 8; j++) {
745 if (cur->image[cnt] & imask)
746 latch |= omask;
747 omask >>= 1;
748 if (cur->mask[cnt] & imask)
749 latch |= omask;
750 omask >>= 1;
751 imask <<= 1;
752 }
753 cnt++;
754 crmfb_write_reg(sc, CRMFB_CURSOR_BITMAP + (i << 2),
755 latch);
756 latch = 0;
757 }
758 }
759 return 0;
760 }
761
762 static inline void
763 crmfb_write_reg(struct crmfb_softc *sc, int offset, uint32_t val)
764 {
765
766 bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, val);
767 wbflush();
768 }
769
770 static inline uint32_t
771 crmfb_read_reg(struct crmfb_softc *sc, int offset)
772 {
773
774 return bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset);
775 }
776
777 static int
778 crmfb_wait_dma_idle(struct crmfb_softc *sc)
779 {
780 int bail = 100000, idle;
781
782 do {
783 idle = ((bus_space_read_4(sc->sc_iot, sc->sc_ioh,
784 CRMFB_OVR_CONTROL) & 1) == 0) &&
785 ((bus_space_read_4(sc->sc_iot, sc->sc_ioh,
786 CRMFB_FRM_CONTROL) & 1) == 0) &&
787 ((bus_space_read_4(sc->sc_iot, sc->sc_ioh,
788 CRMFB_DID_CONTROL) & 1) == 0);
789 if (!idle)
790 delay(10);
791 bail--;
792 } while ((!idle) && (bail > 0));
793 return idle;
794 }
795
796 static int
797 crmfb_setup_video(struct crmfb_softc *sc, int depth)
798 {
799 uint64_t reg;
800 uint32_t d, h, mode, page;
801 int i, bail, tile_width, tlbptr, lptr, j, tx, shift;
802 const char *wantsync;
803 uint16_t v;
804
805 /* disable DMA */
806 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_CONTROL);
807 d &= ~(1 << CRMFB_OVR_CONTROL_DMAEN_SHIFT);
808 crmfb_write_reg(sc, CRMFB_OVR_CONTROL, d);
809 DELAY(50000);
810 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
811 d &= ~(1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
812 crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d);
813 DELAY(50000);
814 crmfb_write_reg(sc, CRMFB_DID_CONTROL, 0);
815 DELAY(50000);
816
817 if (!crmfb_wait_dma_idle(sc))
818 aprint_error("crmfb: crmfb_wait_dma_idle timed out\n");
819
820 /* ensure that CRM starts drawing at the top left of the screen
821 * when we re-enable DMA later
822 */
823 d = (1 << CRMFB_VT_XY_FREEZE_SHIFT);
824 crmfb_write_reg(sc, CRMFB_VT_XY, d);
825 delay(1000);
826 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK);
827 d &= ~(1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT);
828 crmfb_write_reg(sc, CRMFB_DOTCLOCK, d);
829
830 /* wait for dotclock to turn off */
831 bail = 10000;
832 while ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK) &
833 (1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT)) && (bail > 0)) {
834 delay(10);
835 bail--;
836 }
837
838 /* reset FIFO */
839 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE);
840 d |= (1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT);
841 crmfb_write_reg(sc, CRMFB_FRM_TILESIZE, d);
842 d &= ~(1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT);
843 crmfb_write_reg(sc, CRMFB_FRM_TILESIZE, d);
844
845 /* setup colour mode */
846 switch (depth) {
847 case 8:
848 h = CRMFB_MODE_TYP_I8;
849 tile_width = 512;
850 break;
851 case 16:
852 h = CRMFB_MODE_TYP_ARGB5;
853 tile_width = 256;
854 break;
855 case 32:
856 h = CRMFB_MODE_TYP_RGB8;
857 tile_width = 128;
858 break;
859 default:
860 panic("Unsupported depth");
861 }
862 d = h << CRMFB_MODE_TYP_SHIFT;
863 d |= CRMFB_MODE_BUF_BOTH << CRMFB_MODE_BUF_SHIFT;
864 for (i = 0; i < (32 * 4); i += 4)
865 bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_MODE + i, d);
866 wbflush();
867
868 /* setup tile pointer, but don't turn on DMA yet! */
869 h = DMAADDR(sc->sc_dmai);
870 d = (h >> 9) << CRMFB_FRM_CONTROL_TILEPTR_SHIFT;
871 crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d);
872
873 /* init framebuffer width and pixel size */
874 /*d = (1 << CRMFB_FRM_TILESIZE_WIDTH_SHIFT);*/
875
876 d = ((int)(sc->sc_width / tile_width)) <<
877 CRMFB_FRM_TILESIZE_WIDTH_SHIFT;
878 if ((sc->sc_width & (tile_width - 1)) != 0)
879 d |= sc->sc_tiles_y;
880
881 switch (depth) {
882 case 8:
883 h = CRMFB_FRM_TILESIZE_DEPTH_8;
884 break;
885 case 16:
886 h = CRMFB_FRM_TILESIZE_DEPTH_16;
887 break;
888 case 32:
889 h = CRMFB_FRM_TILESIZE_DEPTH_32;
890 break;
891 default:
892 panic("Unsupported depth");
893 }
894 d |= (h << CRMFB_FRM_TILESIZE_DEPTH_SHIFT);
895 crmfb_write_reg(sc, CRMFB_FRM_TILESIZE, d);
896
897 /*h = sc->sc_width * sc->sc_height / (512 / (depth >> 3));*/
898 h = sc->sc_height;
899 d = h << CRMFB_FRM_PIXSIZE_HEIGHT_SHIFT;
900 crmfb_write_reg(sc, CRMFB_FRM_PIXSIZE, d);
901
902 /* turn off firmware overlay and hardware cursor */
903 crmfb_write_reg(sc, CRMFB_OVR_WIDTH_TILE, 0);
904 crmfb_write_reg(sc, CRMFB_CURSOR_CONTROL, 0);
905
906 /* enable drawing again */
907 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK);
908 d |= (1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT);
909 crmfb_write_reg(sc, CRMFB_DOTCLOCK, d);
910 crmfb_write_reg(sc, CRMFB_VT_XY, 0);
911
912 /* turn on DMA for the framebuffer */
913 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
914 d |= (1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
915 crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d);
916
917 /* turn off sync-on-green */
918
919 wantsync = arcbios_GetEnvironmentVariable("SyncOnGreen");
920 if ( (wantsync != NULL) && (wantsync[0] == 'n') ) {
921 d = ( 1 << CRMFB_VT_FLAGS_SYNC_LOW_LSB) &
922 CRMFB_REG_MASK(CRMFB_VT_FLAGS_SYNC_LOW_MSB,
923 CRMFB_VT_FLAGS_SYNC_LOW_LSB);
924 crmfb_write_reg(sc, CRMFB_VT_FLAGS, d);
925 }
926
927 sc->sc_depth = depth;
928
929 /* finally set up the drawing engine's TLB A */
930 v = (DMAADDR(sc->sc_dma) >> 16) & 0xffff;
931 tlbptr = 0;
932 tx = ((sc->sc_width + (tile_width - 1)) & ~(tile_width - 1)) /
933 tile_width;
934
935 #ifdef CRMFB_DEBUG
936 printf("tx: %d\n", tx);
937 #endif
938
939 for (i = 0; i < 16; i++) {
940 reg = 0;
941 shift = 64;
942 lptr = 0;
943 for (j = 0; j < tx; j++) {
944 shift -= 16;
945 reg |= (((uint64_t)(v | 0x8000)) << shift);
946 if (shift == 0) {
947 shift = 64;
948 bus_space_write_8(sc->sc_iot, sc->sc_reh,
949 CRIME_RE_TLB_A + tlbptr + lptr,
950 reg);
951 #ifdef CRMFB_DEBUG
952 printf("%04x: %016llx\n", tlbptr + lptr, reg);
953 #endif
954 reg = 0;
955 lptr += 8;
956 }
957 v++;
958 }
959 if (shift != 64) {
960 bus_space_write_8(sc->sc_iot, sc->sc_reh,
961 CRIME_RE_TLB_A + tlbptr + lptr, reg);
962 #ifdef CRMFB_DEBUG
963 printf("%04x: %016llx\n", tlbptr + lptr, reg);
964 #endif
965 }
966 tlbptr += 32;
967 }
968 sc->sc_scratch = (char *)KERNADDR(sc->sc_dma) + (0xf0000 * tx);
969
970 /* now put the last 64kB into the 1st linear TLB */
971 page = (sc->sc_linear >> 12) | 0x80000000;
972 tlbptr = 0;
973 for (i = 0; i < 8; i++) {
974 reg = ((uint64_t)page << 32) | (page + 1);
975 bus_space_write_8(sc->sc_iot, sc->sc_reh,
976 CRIME_RE_LINEAR_A + tlbptr, reg);
977 page += 2;
978 tlbptr += 8;
979 }
980 wbflush();
981
982 /* do some very basic engine setup */
983 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_CLIPMODE, 0);
984 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_WINOFFSET_SRC, 0);
985 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_WINOFFSET_DST, 0);
986 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_PLANEMASK,
987 0xffffffff);
988
989 bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x20, 0);
990 bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x28, 0);
991 bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x30, 0);
992 bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x38, 0);
993 bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x40, 0);
994
995 switch (depth) {
996 case 8:
997 mode = DE_MODE_TLB_A | DE_MODE_BUFDEPTH_8 |
998 DE_MODE_TYPE_CI | DE_MODE_PIXDEPTH_8;
999 break;
1000 case 16:
1001 mode = DE_MODE_TLB_A | DE_MODE_BUFDEPTH_16 |
1002 DE_MODE_TYPE_RGB | DE_MODE_PIXDEPTH_16;
1003 break;
1004 case 32:
1005 mode = DE_MODE_TLB_A | DE_MODE_BUFDEPTH_32 |
1006 DE_MODE_TYPE_RGBA | DE_MODE_PIXDEPTH_32;
1007 break;
1008 default:
1009 panic("%s: unsuported colour depth %d\n", __func__,
1010 depth);
1011 }
1012 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_MODE_DST, mode);
1013 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_MODE_SRC, mode);
1014 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_STEP_X, 1);
1015 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_STEP_Y, 1);
1016
1017 /* initialize memory transfer engine */
1018 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_MODE,
1019 MTE_MODE_DST_ECC |
1020 (MTE_TLB_A << MTE_DST_TLB_SHIFT) |
1021 (MTE_TLB_A << MTE_SRC_TLB_SHIFT) |
1022 (MTE_DEPTH_8 << MTE_DEPTH_SHIFT) |
1023 MTE_MODE_COPY);
1024 sc->sc_mte_direction = 1;
1025 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST_Y_STEP, 1);
1026 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC_Y_STEP, 1);
1027
1028 return 0;
1029 }
1030
1031 static void
1032 crmfb_set_mte_direction(struct crmfb_softc *sc, int dir)
1033 {
1034 if (dir == sc->sc_mte_direction)
1035 return;
1036
1037 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST_Y_STEP, dir);
1038 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC_Y_STEP, dir);
1039 sc->sc_mte_direction = dir;
1040 }
1041
1042 static void
1043 crmfb_setup_palette(struct crmfb_softc *sc)
1044 {
1045 int i;
1046
1047 for (i = 0; i < 256; i++) {
1048 crmfb_set_palette(sc, i, rasops_cmap[(i * 3) + 2],
1049 rasops_cmap[(i * 3) + 1], rasops_cmap[(i * 3) + 0]);
1050 sc->sc_cmap_red[i] = rasops_cmap[(i * 3) + 2];
1051 sc->sc_cmap_green[i] = rasops_cmap[(i * 3) + 1];
1052 sc->sc_cmap_blue[i] = rasops_cmap[(i * 3) + 0];
1053 }
1054 }
1055
1056 static inline void
1057 crmfb_wait_idle(struct crmfb_softc *sc)
1058 {
1059 int i = 0;
1060
1061 do {
1062 i++;
1063 } while (((bus_space_read_4(sc->sc_iot, sc->sc_reh, CRIME_DE_STATUS) &
1064 CRIME_DE_IDLE) == 0) && (i < 100000000));
1065 if (i >= 100000000)
1066 aprint_error("crmfb_wait_idle() timed out\n");
1067 }
1068
1069 static void
1070 crmfb_fill_rect(struct crmfb_softc *sc, int x, int y, int width, int height,
1071 uint32_t colour)
1072 {
1073 crmfb_wait_idle(sc);
1074 crmfb_set_mte_direction(sc, 1);
1075 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_MODE,
1076 MTE_MODE_DST_ECC |
1077 (MTE_TLB_A << MTE_DST_TLB_SHIFT) |
1078 (MTE_TLB_A << MTE_SRC_TLB_SHIFT) |
1079 (MTE_DEPTH_8 << MTE_DEPTH_SHIFT) |
1080 0);
1081 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_BG, colour);
1082 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST0,
1083 (x << 16) | (y & 0xffff));
1084 bus_space_write_4(sc->sc_iot, sc->sc_reh,
1085 CRIME_MTE_DST1 | CRIME_DE_START,
1086 ((x + width - 1) << 16) | ((y + height - 1) & 0xffff));
1087 }
1088
1089 static void
1090 crmfb_bitblt(struct crmfb_softc *sc, int xs, int ys, int xd, int yd,
1091 int wi, int he, uint32_t rop)
1092 {
1093 uint32_t prim = DE_PRIM_RECTANGLE;
1094 int rxa, rya, rxe, rye, rxs, rys;
1095 crmfb_wait_idle(sc);
1096 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_DRAWMODE,
1097 DE_DRAWMODE_PLANEMASK | DE_DRAWMODE_BYTEMASK | DE_DRAWMODE_ROP |
1098 DE_DRAWMODE_XFER_EN);
1099 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_ROP, rop);
1100 if (xs < xd) {
1101 prim |= DE_PRIM_RL;
1102 rxe = xd;
1103 rxa = xd + wi - 1;
1104 rxs = xs + wi - 1;
1105 } else {
1106 prim |= DE_PRIM_LR;
1107 rxe = xd + wi - 1;
1108 rxa = xd;
1109 rxs = xs;
1110 }
1111 if (ys < yd) {
1112 prim |= DE_PRIM_BT;
1113 rye = yd;
1114 rya = yd + he - 1;
1115 rys = ys + he - 1;
1116 } else {
1117 prim |= DE_PRIM_TB;
1118 rye = yd + he - 1;
1119 rya = yd;
1120 rys = ys;
1121 }
1122 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_PRIMITIVE, prim);
1123 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_ADDR_SRC,
1124 (rxs << 16) | (rys & 0xffff));
1125 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_X_VERTEX_0,
1126 (rxa << 16) | (rya & 0xffff));
1127 bus_space_write_4(sc->sc_iot, sc->sc_reh,
1128 CRIME_DE_X_VERTEX_1 | CRIME_DE_START,
1129 (rxe << 16) | (rye & 0xffff));
1130 }
1131
1132 static void
1133 crmfb_scroll(struct crmfb_softc *sc, int xs, int ys, int xd, int yd,
1134 int wi, int he)
1135 {
1136 int rxa, rya, rxe, rye, rxd, ryd, rxde, ryde;
1137
1138 rxa = xs;
1139 rxe = xs + wi - 1;
1140 rxd = xd;
1141 rxde = xd + wi - 1;
1142
1143 crmfb_wait_idle(sc);
1144
1145 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_MODE,
1146 MTE_MODE_DST_ECC |
1147 (MTE_TLB_A << MTE_DST_TLB_SHIFT) |
1148 (MTE_TLB_A << MTE_SRC_TLB_SHIFT) |
1149 (MTE_DEPTH_8 << MTE_DEPTH_SHIFT) |
1150 MTE_MODE_COPY);
1151
1152 if (ys < yd) {
1153 /* bottom to top */
1154 rye = ys;
1155 rya = ys + he - 1;
1156 ryd = yd + he - 1;
1157 ryde = yd;
1158 crmfb_set_mte_direction(sc, -1);
1159 } else {
1160 /* top to bottom */
1161 rye = ys + he - 1;
1162 rya = ys;
1163 ryd = yd;
1164 ryde = yd + he - 1;
1165 crmfb_set_mte_direction(sc, 1);
1166 }
1167 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC0,
1168 (rxa << 16) | rya);
1169 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC1,
1170 (rxe << 16) | rye);
1171 bus_space_write_4(sc->sc_iot, sc->sc_reh,
1172 CRIME_MTE_DST0,
1173 (rxd << 16) | ryd);
1174 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST1 |
1175 CRIME_DE_START,
1176 (rxde << 16) | ryde);
1177 }
1178
1179 static void
1180 crmfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
1181 {
1182 struct rasops_info *ri = cookie;
1183 struct vcons_screen *scr = ri->ri_hw;
1184 int32_t xs, xd, y, width, height;
1185
1186 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
1187 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
1188 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1189 width = ri->ri_font->fontwidth * ncols;
1190 height = ri->ri_font->fontheight;
1191 crmfb_bitblt(scr->scr_cookie, xs, y, xd, y, width, height, 3);
1192 }
1193
1194 static void
1195 crmfb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
1196 {
1197 struct rasops_info *ri = cookie;
1198 struct vcons_screen *scr = ri->ri_hw;
1199 int32_t x, y, width, height, bg;
1200
1201 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
1202 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1203 width = ri->ri_font->fontwidth * ncols;
1204 height = ri->ri_font->fontheight;
1205 bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff];
1206 crmfb_fill_rect(scr->scr_cookie, x, y, width, height, bg);
1207 }
1208
1209 static void
1210 crmfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
1211 {
1212 struct rasops_info *ri = cookie;
1213 struct vcons_screen *scr = ri->ri_hw;
1214 int32_t x, ys, yd, width, height;
1215
1216 x = ri->ri_xorigin;
1217 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
1218 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
1219 width = ri->ri_emuwidth;
1220 height = ri->ri_font->fontheight * nrows;
1221
1222 crmfb_scroll(scr->scr_cookie, x, ys, x, yd, width, height);
1223 }
1224
1225 static void
1226 crmfb_eraserows(void *cookie, int row, int nrows, long fillattr)
1227 {
1228 struct rasops_info *ri = cookie;
1229 struct vcons_screen *scr = ri->ri_hw;
1230 int32_t x, y, width, height, bg;
1231
1232 if ((row == 0) && (nrows == ri->ri_rows)) {
1233 x = y = 0;
1234 width = ri->ri_width;
1235 height = ri->ri_height;
1236 } else {
1237 x = ri->ri_xorigin;
1238 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1239 width = ri->ri_emuwidth;
1240 height = ri->ri_font->fontheight * nrows;
1241 }
1242 bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff];
1243 crmfb_fill_rect(scr->scr_cookie, x, y, width, height, bg);
1244 }
1245
1246 static void
1247 crmfb_cursor(void *cookie, int on, int row, int col)
1248 {
1249 struct rasops_info *ri = cookie;
1250 struct vcons_screen *scr = ri->ri_hw;
1251 struct crmfb_softc *sc = scr->scr_cookie;
1252 int x, y, wi,he;
1253
1254 wi = ri->ri_font->fontwidth;
1255 he = ri->ri_font->fontheight;
1256
1257 if (ri->ri_flg & RI_CURSOR) {
1258 x = ri->ri_ccol * wi + ri->ri_xorigin;
1259 y = ri->ri_crow * he + ri->ri_yorigin;
1260 crmfb_bitblt(sc, x, y, x, y, wi, he, 12);
1261 ri->ri_flg &= ~RI_CURSOR;
1262 }
1263
1264 ri->ri_crow = row;
1265 ri->ri_ccol = col;
1266
1267 if (on)
1268 {
1269 x = ri->ri_ccol * wi + ri->ri_xorigin;
1270 y = ri->ri_crow * he + ri->ri_yorigin;
1271 crmfb_bitblt(sc, x, y, x, y, wi, he, 12);
1272 ri->ri_flg |= RI_CURSOR;
1273 }
1274 }
1275
1276 static void
1277 crmfb_putchar(void *cookie, int row, int col, u_int c, long attr)
1278 {
1279 struct rasops_info *ri = cookie;
1280 struct vcons_screen *scr = ri->ri_hw;
1281 struct crmfb_softc *sc = scr->scr_cookie;
1282 struct wsdisplay_font *font = PICK_FONT(ri, c);
1283 uint32_t bg, fg;
1284 int x, y, wi, he, i, uc;
1285 uint8_t *fd8;
1286 uint16_t *fd16;
1287 void *fd;
1288
1289 wi = font->fontwidth;
1290 he = font->fontheight;
1291
1292 x = ri->ri_xorigin + col * wi;
1293 y = ri->ri_yorigin + row * he;
1294
1295 bg = ri->ri_devcmap[(attr >> 16) & 0xff];
1296 fg = ri->ri_devcmap[(attr >> 24) & 0xff];
1297 uc = c - font->firstchar;
1298 fd = (uint8_t *)font->data + uc * ri->ri_fontscale;
1299 if (c == 0x20) {
1300 crmfb_fill_rect(sc, x, y, wi, he, bg);
1301 } else {
1302 crmfb_wait_idle(sc);
1303 /* setup */
1304 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_ROP, 3);
1305 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_FG, fg);
1306 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_BG, bg);
1307 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_DRAWMODE,
1308 DE_DRAWMODE_PLANEMASK | DE_DRAWMODE_BYTEMASK |
1309 DE_DRAWMODE_ROP |
1310 DE_DRAWMODE_OPAQUE_STIP | DE_DRAWMODE_POLY_STIP);
1311 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_PRIMITIVE,
1312 DE_PRIM_RECTANGLE | DE_PRIM_LR | DE_PRIM_TB);
1313 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_STIPPLE_MODE,
1314 0x001f0000);
1315 /* now let's feed the engine */
1316 if (font->stride == 1) {
1317 /* shovel in 8 bit quantities */
1318 fd8 = fd;
1319 for (i = 0; i < he; i++) {
1320 /*
1321 * the pipeline should be long enough to
1322 * draw any character without having to wait
1323 */
1324 bus_space_write_4(sc->sc_iot, sc->sc_reh,
1325 CRIME_DE_STIPPLE_PAT, *fd8 << 24);
1326 bus_space_write_4(sc->sc_iot, sc->sc_reh,
1327 CRIME_DE_X_VERTEX_0, (x << 16) | y);
1328 bus_space_write_4(sc->sc_iot, sc->sc_reh,
1329 CRIME_DE_X_VERTEX_1 | CRIME_DE_START,
1330 ((x + wi) << 16) | y);
1331 y++;
1332 fd8++;
1333 }
1334 } else if (font->stride == 2) {
1335 /* shovel in 16 bit quantities */
1336 fd16 = fd;
1337 for (i = 0; i < he; i++) {
1338 /*
1339 * the pipeline should be long enough to
1340 * draw any character without having to wait
1341 */
1342 bus_space_write_4(sc->sc_iot, sc->sc_reh,
1343 CRIME_DE_STIPPLE_PAT, *fd16 << 16);
1344 bus_space_write_4(sc->sc_iot, sc->sc_reh,
1345 CRIME_DE_X_VERTEX_0, (x << 16) | y);
1346 bus_space_write_4(sc->sc_iot, sc->sc_reh,
1347 CRIME_DE_X_VERTEX_1 | CRIME_DE_START,
1348 ((x + wi) << 16) | y);
1349 y++;
1350 fd16++;
1351 }
1352 }
1353 }
1354 }
1355
1356 static void
1357 crmfb_setup_ddc(struct crmfb_softc *sc)
1358 {
1359 int i;
1360 char edid_data[128];
1361 struct edid_info ei;
1362
1363 memset(edid_data, 0, 128);
1364 sc->sc_i2c.ic_cookie = sc;
1365 sc->sc_i2c.ic_acquire_bus = crmfb_i2c_acquire_bus;
1366 sc->sc_i2c.ic_release_bus = crmfb_i2c_release_bus;
1367 sc->sc_i2c.ic_send_start = crmfb_i2c_send_start;
1368 sc->sc_i2c.ic_send_stop = crmfb_i2c_send_stop;
1369 sc->sc_i2c.ic_initiate_xfer = crmfb_i2c_initiate_xfer;
1370 sc->sc_i2c.ic_read_byte = crmfb_i2c_read_byte;
1371 sc->sc_i2c.ic_write_byte = crmfb_i2c_write_byte;
1372 sc->sc_i2c.ic_exec = NULL;
1373 i = 0;
1374 while (edid_data[1] == 0 && i++ < 10)
1375 ddc_read_edid(&sc->sc_i2c, edid_data, 128);
1376 if (i > 1)
1377 aprint_debug_dev(sc->sc_dev,
1378 "had to try %d times to get EDID data\n", i);
1379 if (i < 11) {
1380 edid_parse(edid_data, &ei);
1381 edid_print(&ei);
1382 }
1383 }
1384
1385 /* I2C bitbanging */
1386 static void
1387 crmfb_i2cbb_set_bits(void *cookie, uint32_t bits)
1388 {
1389 struct crmfb_softc *sc = cookie;
1390
1391 bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_I2C_VGA, bits ^ 3);
1392 }
1393
1394 static void
1395 crmfb_i2cbb_set_dir(void *cookie, uint32_t dir)
1396 {
1397
1398 /* Nothing to do */
1399 }
1400
1401 static uint32_t
1402 crmfb_i2cbb_read(void *cookie)
1403 {
1404 struct crmfb_softc *sc = cookie;
1405
1406 return bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_I2C_VGA) ^ 3;
1407 }
1408
1409 /* higher level I2C stuff */
1410 static int
1411 crmfb_i2c_acquire_bus(void *cookie, int flags)
1412 {
1413
1414 /* private bus */
1415 return 0;
1416 }
1417
1418 static void
1419 crmfb_i2c_release_bus(void *cookie, int flags)
1420 {
1421
1422 /* private bus */
1423 }
1424
1425 static int
1426 crmfb_i2c_send_start(void *cookie, int flags)
1427 {
1428
1429 return i2c_bitbang_send_start(cookie, flags, &crmfb_i2cbb_ops);
1430 }
1431
1432 static int
1433 crmfb_i2c_send_stop(void *cookie, int flags)
1434 {
1435
1436 return i2c_bitbang_send_stop(cookie, flags, &crmfb_i2cbb_ops);
1437 }
1438
1439 static int
1440 crmfb_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
1441 {
1442
1443 return i2c_bitbang_initiate_xfer(cookie, addr, flags,
1444 &crmfb_i2cbb_ops);
1445 }
1446
1447 static int
1448 crmfb_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
1449 {
1450
1451 return i2c_bitbang_read_byte(cookie, valp, flags, &crmfb_i2cbb_ops);
1452 }
1453
1454 static int
1455 crmfb_i2c_write_byte(void *cookie, uint8_t val, int flags)
1456 {
1457
1458 return i2c_bitbang_write_byte(cookie, val, flags, &crmfb_i2cbb_ops);
1459 }
1460