crmfb.c revision 1.42 1 /* $NetBSD: crmfb.c,v 1.42 2016/02/08 16:44:45 christos 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.42 2016/02/08 16:44:45 christos Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/malloc.h>
41
42 #include <machine/autoconf.h>
43 #include <sys/bus.h>
44 #include <machine/machtype.h>
45 #include <machine/vmparam.h>
46
47 #include <dev/arcbios/arcbios.h>
48 #include <dev/arcbios/arcbiosvar.h>
49
50 #include <dev/wscons/wsdisplayvar.h>
51 #include <dev/wscons/wsconsio.h>
52 #include <dev/wsfont/wsfont.h>
53 #include <dev/rasops/rasops.h>
54 #include <dev/wscons/wsdisplay_vconsvar.h>
55
56 #include <dev/i2c/i2cvar.h>
57 #include <dev/i2c/i2c_bitbang.h>
58 #include <dev/i2c/ddcvar.h>
59 #include <dev/videomode/videomode.h>
60 #include <dev/videomode/edidvar.h>
61
62 #include <arch/sgimips/dev/crmfbreg.h>
63
64 #include "opt_crmfb.h"
65
66 #ifdef CRMFB_DEBUG
67 #define DPRINTF printf
68 #else
69 #define DPRINTF while (0) printf
70 #endif
71
72 struct wsscreen_descr crmfb_defaultscreen = {
73 "default",
74 0, 0,
75 NULL,
76 8, 16,
77 WSSCREEN_WSCOLORS,
78 NULL,
79 };
80
81 const struct wsscreen_descr *_crmfb_scrlist[] = {
82 &crmfb_defaultscreen,
83 };
84
85 struct wsscreen_list crmfb_screenlist = {
86 sizeof(_crmfb_scrlist) / sizeof(struct wsscreen_descr *),
87 _crmfb_scrlist
88 };
89
90 static struct vcons_screen crmfb_console_screen;
91
92 static int crmfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
93 static paddr_t crmfb_mmap(void *, void *, off_t, int);
94 static void crmfb_init_screen(void *, struct vcons_screen *, int, long *);
95
96 struct wsdisplay_accessops crmfb_accessops = {
97 crmfb_ioctl,
98 crmfb_mmap,
99 NULL, /* alloc_screen */
100 NULL, /* free_screen */
101 NULL, /* show_screen */
102 NULL, /* load_font */
103 NULL, /* pollc */
104 NULL, /* scroll */
105 };
106
107 /* Memory to allocate to SGI-CRM -- remember, this is stolen from
108 * host memory!
109 */
110 #define CRMFB_TILESIZE (512*128)
111
112 static int crmfb_match(device_t, struct cfdata *, void *);
113 static void crmfb_attach(device_t, device_t, void *);
114 int crmfb_probe(void);
115
116 #define KERNADDR(p) ((void *)((p).addr))
117 #define DMAADDR(p) ((p).map->dm_segs[0].ds_addr)
118
119 #define CRMFB_REG_MASK(msb, lsb) \
120 ( (((uint32_t) 1 << ((msb)-(lsb)+1)) - 1) << (lsb) )
121
122
123 struct crmfb_dma {
124 bus_dmamap_t map;
125 void *addr;
126 bus_dma_segment_t segs[1];
127 int nsegs;
128 size_t size;
129 };
130
131 struct crmfb_softc {
132 device_t sc_dev;
133 struct vcons_data sc_vd;
134 struct i2c_controller sc_i2c;
135 int sc_dir;
136
137 bus_space_tag_t sc_iot;
138 bus_space_handle_t sc_ioh;
139 bus_space_handle_t sc_reh;
140
141 bus_dma_tag_t sc_dmat;
142
143 struct crmfb_dma sc_dma;
144 struct crmfb_dma sc_dmai;
145
146 int sc_width;
147 int sc_height;
148 int sc_depth;
149 int sc_console_depth;
150 int sc_tiles_x, sc_tiles_y;
151 uint32_t sc_fbsize;
152 int sc_mte_direction;
153 int sc_mte_x_shift;
154 uint32_t sc_mte_mode;
155 uint32_t sc_de_mode;
156 uint32_t sc_src_mode;
157 uint32_t sc_dst_mode;
158 int sc_needs_sync;
159 uint8_t *sc_lptr;
160 paddr_t sc_linear;
161 uint32_t sc_vtflags;
162 int sc_wsmode, sc_video_on;
163 uint8_t sc_edid_data[128];
164 struct edid_info sc_edid_info;
165
166 /* cursor stuff */
167 int sc_cur_x;
168 int sc_cur_y;
169 int sc_hot_x;
170 int sc_hot_y;
171
172 u_char sc_cmap_red[256];
173 u_char sc_cmap_green[256];
174 u_char sc_cmap_blue[256];
175 };
176
177 static int crmfb_putcmap(struct crmfb_softc *, struct wsdisplay_cmap *);
178 static int crmfb_getcmap(struct crmfb_softc *, struct wsdisplay_cmap *);
179 static void crmfb_set_palette(struct crmfb_softc *,
180 int, uint8_t, uint8_t, uint8_t);
181 static int crmfb_set_curpos(struct crmfb_softc *, int, int);
182 static int crmfb_gcursor(struct crmfb_softc *, struct wsdisplay_cursor *);
183 static int crmfb_scursor(struct crmfb_softc *, struct wsdisplay_cursor *);
184 static inline void crmfb_write_reg(struct crmfb_softc *, int, uint32_t);
185 static inline uint32_t crmfb_read_reg(struct crmfb_softc *, int);
186 static int crmfb_wait_dma_idle(struct crmfb_softc *);
187
188 /* setup video hw in given colour depth */
189 static int crmfb_setup_video(struct crmfb_softc *, int);
190 static void crmfb_setup_palette(struct crmfb_softc *);
191
192 static void crmfb_fill_rect(struct crmfb_softc *, int, int, int, int, uint32_t);
193 static void crmfb_bitblt(struct crmfb_softc *, int, int, int, int, int, int,
194 uint32_t);
195 static void crmfb_scroll(struct crmfb_softc *, int, int, int, int, int, int);
196
197 static void crmfb_copycols(void *, int, int, int, int);
198 static void crmfb_erasecols(void *, int, int, int, long);
199 static void crmfb_copyrows(void *, int, int, int);
200 static void crmfb_eraserows(void *, int, int, long);
201 static void crmfb_cursor(void *, int, int, int);
202 static void crmfb_putchar(void *, int, int, u_int, long);
203 static void crmfb_putchar_aa(void *, int, int, u_int, long);
204
205 /* I2C glue */
206 static int crmfb_i2c_acquire_bus(void *, int);
207 static void crmfb_i2c_release_bus(void *, int);
208 static int crmfb_i2c_send_start(void *, int);
209 static int crmfb_i2c_send_stop(void *, int);
210 static int crmfb_i2c_initiate_xfer(void *, i2c_addr_t, int);
211 static int crmfb_i2c_read_byte(void *, uint8_t *, int);
212 static int crmfb_i2c_write_byte(void *, uint8_t, int);
213
214 /* I2C bitbang glue */
215 static void crmfb_i2cbb_set_bits(void *, uint32_t);
216 static void crmfb_i2cbb_set_dir(void *, uint32_t);
217 static uint32_t crmfb_i2cbb_read(void *);
218
219 static const struct i2c_bitbang_ops crmfb_i2cbb_ops = {
220 crmfb_i2cbb_set_bits,
221 crmfb_i2cbb_set_dir,
222 crmfb_i2cbb_read,
223 {
224 CRMFB_I2C_SDA,
225 CRMFB_I2C_SCL,
226 0,
227 1
228 }
229 };
230 static void crmfb_setup_ddc(struct crmfb_softc *);
231
232 /* mode setting stuff */
233 static uint32_t calc_pll(int); /* frequency in kHz */
234 static int crmfb_set_mode(struct crmfb_softc *, const struct videomode *);
235
236 CFATTACH_DECL_NEW(crmfb, sizeof(struct crmfb_softc),
237 crmfb_match, crmfb_attach, NULL, NULL);
238
239 static int
240 crmfb_match(device_t parent, struct cfdata *cf, void *opaque)
241 {
242 return crmfb_probe();
243 }
244
245 static void
246 crmfb_attach(device_t parent, device_t self, void *opaque)
247 {
248 struct mainbus_attach_args *ma;
249 struct crmfb_softc *sc;
250 struct rasops_info *ri;
251 struct wsemuldisplaydev_attach_args aa;
252 uint32_t d, h;
253 uint16_t *p;
254 unsigned long v;
255 long defattr;
256 const char *consdev;
257 int rv, i;
258
259 sc = device_private(self);
260 sc->sc_dev = self;
261
262 ma = (struct mainbus_attach_args *)opaque;
263
264 sc->sc_iot = normal_memt;
265 sc->sc_dmat = &sgimips_default_bus_dma_tag;
266 sc->sc_wsmode = WSDISPLAYIO_MODE_EMUL;
267
268 aprint_normal(": SGI CRIME Graphics Display Engine\n");
269 rv = bus_space_map(sc->sc_iot, ma->ma_addr, 0 /* XXX */,
270 BUS_SPACE_MAP_LINEAR, &sc->sc_ioh);
271 if (rv)
272 panic("crmfb_attach: can't map I/O space");
273 rv = bus_space_map(sc->sc_iot, 0x15000000, 0x6000, 0, &sc->sc_reh);
274 if (rv)
275 panic("crmfb_attach: can't map rendering engine");
276
277 /* determine mode configured by firmware */
278 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_HCMAP);
279 sc->sc_width = (d >> CRMFB_VT_HCMAP_ON_SHIFT) & 0xfff;
280 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_VCMAP);
281 sc->sc_height = (d >> CRMFB_VT_VCMAP_ON_SHIFT) & 0xfff;
282 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE);
283 h = (d >> CRMFB_FRM_TILESIZE_DEPTH_SHIFT) & 0x3;
284 if (h == 0)
285 sc->sc_depth = 8;
286 else if (h == 1)
287 sc->sc_depth = 16;
288 else
289 sc->sc_depth = 32;
290
291 if (sc->sc_width == 0 || sc->sc_height == 0) {
292 /*
293 * XXX
294 * actually, these days we probably could
295 */
296 aprint_error_dev(sc->sc_dev,
297 "device unusable if not setup by firmware\n");
298 bus_space_unmap(sc->sc_iot, sc->sc_ioh, 0 /* XXX */);
299 return;
300 }
301
302 aprint_normal_dev(sc->sc_dev, "initial resolution %dx%d\n",
303 sc->sc_width, sc->sc_height);
304
305 sc->sc_console_depth = 8;
306
307 crmfb_setup_ddc(sc);
308 if ((sc->sc_edid_info.edid_preferred_mode != NULL)) {
309 if (crmfb_set_mode(sc, sc->sc_edid_info.edid_preferred_mode))
310 aprint_normal_dev(sc->sc_dev, "using %dx%d\n",
311 sc->sc_width, sc->sc_height);
312 }
313 /*
314 * first determine how many tiles we need
315 * in 32bit each tile is 128x128 pixels
316 */
317 sc->sc_tiles_x = (sc->sc_width + 127) >> 7;
318 sc->sc_tiles_y = (sc->sc_height + 127) >> 7;
319 sc->sc_fbsize = 0x10000 * sc->sc_tiles_x * sc->sc_tiles_y;
320
321 sc->sc_dmai.size = 256 * sizeof(uint16_t);
322 rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dmai.size, 65536, 0,
323 sc->sc_dmai.segs,
324 sizeof(sc->sc_dmai.segs) / sizeof(sc->sc_dmai.segs[0]),
325 &sc->sc_dmai.nsegs, BUS_DMA_NOWAIT);
326 if (rv)
327 panic("crmfb_attach: can't allocate DMA memory");
328 rv = bus_dmamem_map(sc->sc_dmat, sc->sc_dmai.segs, sc->sc_dmai.nsegs,
329 sc->sc_dmai.size, &sc->sc_dmai.addr,
330 BUS_DMA_NOWAIT);
331 if (rv)
332 panic("crmfb_attach: can't map DMA memory");
333 rv = bus_dmamap_create(sc->sc_dmat, sc->sc_dmai.size, 1,
334 sc->sc_dmai.size, 0, BUS_DMA_NOWAIT, &sc->sc_dmai.map);
335 if (rv)
336 panic("crmfb_attach: can't create DMA map");
337 rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dmai.map, sc->sc_dmai.addr,
338 sc->sc_dmai.size, NULL, BUS_DMA_NOWAIT);
339 if (rv)
340 panic("crmfb_attach: can't load DMA map");
341
342 /* allocate an extra 128Kb for a linear buffer */
343 sc->sc_dma.size = 0x10000 * (16 * sc->sc_tiles_x + 2);
344 rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dma.size, 65536, 0,
345 sc->sc_dma.segs,
346 sizeof(sc->sc_dma.segs) / sizeof(sc->sc_dma.segs[0]),
347 &sc->sc_dma.nsegs, BUS_DMA_NOWAIT);
348 if (rv)
349 panic("crmfb_attach: can't allocate DMA memory");
350 rv = bus_dmamem_map(sc->sc_dmat, sc->sc_dma.segs, sc->sc_dma.nsegs,
351 sc->sc_dma.size, &sc->sc_dma.addr,
352 BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
353 if (rv)
354 panic("crmfb_attach: can't map DMA memory");
355 rv = bus_dmamap_create(sc->sc_dmat, sc->sc_dma.size, 1,
356 sc->sc_dma.size, 0, BUS_DMA_NOWAIT, &sc->sc_dma.map);
357 if (rv)
358 panic("crmfb_attach: can't create DMA map");
359 rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dma.map, sc->sc_dma.addr,
360 sc->sc_dma.size, NULL, BUS_DMA_NOWAIT);
361 if (rv)
362 panic("crmfb_attach: can't load DMA map");
363
364 p = KERNADDR(sc->sc_dmai);
365 v = (unsigned long)DMAADDR(sc->sc_dma);
366 for (i = 0; i < (sc->sc_tiles_x * sc->sc_tiles_y); i++) {
367 p[i] = ((uint32_t)v >> 16) + i;
368 }
369 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmai.map, 0, sc->sc_dmai.size,
370 BUS_DMASYNC_PREWRITE);
371 sc->sc_linear = (paddr_t)DMAADDR(sc->sc_dma) + 0x100000 * sc->sc_tiles_x;
372 sc->sc_lptr = (char *)KERNADDR(sc->sc_dma) + (0x100000 * sc->sc_tiles_x);
373
374 aprint_normal_dev(sc->sc_dev, "allocated %d byte fb @ %p (%p)\n",
375 sc->sc_fbsize, KERNADDR(sc->sc_dmai), KERNADDR(sc->sc_dma));
376
377 crmfb_setup_video(sc, sc->sc_console_depth);
378 ri = &crmfb_console_screen.scr_ri;
379 memset(ri, 0, sizeof(struct rasops_info));
380 sc->sc_video_on = 1;
381
382 vcons_init(&sc->sc_vd, sc, &crmfb_defaultscreen, &crmfb_accessops);
383 sc->sc_vd.init_screen = crmfb_init_screen;
384 crmfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
385 vcons_init_screen(&sc->sc_vd, &crmfb_console_screen, 1, &defattr);
386
387 crmfb_defaultscreen.ncols = ri->ri_cols;
388 crmfb_defaultscreen.nrows = ri->ri_rows;
389 crmfb_defaultscreen.textops = &ri->ri_ops;
390 crmfb_defaultscreen.capabilities = ri->ri_caps;
391 crmfb_defaultscreen.modecookie = NULL;
392
393 crmfb_setup_palette(sc);
394 crmfb_fill_rect(sc, 0, 0, sc->sc_width, sc->sc_height,
395 ri->ri_devcmap[(defattr >> 16) & 0xff]);
396
397 consdev = arcbios_GetEnvironmentVariable("ConsoleOut");
398 if (consdev != NULL && strcmp(consdev, "video()") == 0) {
399 wsdisplay_cnattach(&crmfb_defaultscreen, ri, 0, 0, defattr);
400 vcons_replay_msgbuf(&crmfb_console_screen);
401 aa.console = 1;
402 } else
403 aa.console = 0;
404 aa.scrdata = &crmfb_screenlist;
405 aa.accessops = &crmfb_accessops;
406 aa.accesscookie = &sc->sc_vd;
407
408 config_found(self, &aa, wsemuldisplaydevprint);
409
410 sc->sc_cur_x = 0;
411 sc->sc_cur_y = 0;
412 sc->sc_hot_x = 0;
413 sc->sc_hot_y = 0;
414
415 return;
416 }
417
418 int
419 crmfb_probe(void)
420 {
421
422 if (mach_type != MACH_SGI_IP32)
423 return 0;
424
425 return 1;
426 }
427
428 static int
429 crmfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
430 {
431 struct vcons_data *vd;
432 struct crmfb_softc *sc;
433 struct wsdisplay_fbinfo *wdf;
434 int nmode;
435
436 vd = (struct vcons_data *)v;
437 sc = (struct crmfb_softc *)vd->cookie;
438
439 switch (cmd) {
440 case WSDISPLAYIO_GTYPE:
441 /* not really, but who cares? */
442 /* xf86-video-crime does */
443 *(u_int *)data = WSDISPLAY_TYPE_CRIME;
444 return 0;
445 case WSDISPLAYIO_GINFO:
446 if (vd->active != NULL) {
447 wdf = (void *)data;
448 wdf->height = sc->sc_height;
449 wdf->width = sc->sc_width;
450 wdf->depth = 32;
451 wdf->cmsize = 256;
452 return 0;
453 } else
454 return ENODEV;
455 case WSDISPLAYIO_GETCMAP:
456 if (sc->sc_depth == 8)
457 return crmfb_getcmap(sc, (struct wsdisplay_cmap *)data);
458 else
459 return EINVAL;
460 case WSDISPLAYIO_PUTCMAP:
461 if (sc->sc_depth == 8)
462 return crmfb_putcmap(sc, (struct wsdisplay_cmap *)data);
463 else
464 return EINVAL;
465 case WSDISPLAYIO_LINEBYTES:
466 *(u_int *)data = sc->sc_width * sc->sc_depth / 8;
467 return 0;
468 case WSDISPLAYIO_SMODE:
469 nmode = *(int *)data;
470 if (nmode != sc->sc_wsmode) {
471 sc->sc_wsmode = nmode;
472 if (nmode == WSDISPLAYIO_MODE_EMUL) {
473 crmfb_setup_video(sc, sc->sc_console_depth);
474 crmfb_setup_palette(sc);
475 vcons_redraw_screen(vd->active);
476 } else {
477 crmfb_setup_video(sc, 32);
478 }
479 }
480 return 0;
481 case WSDISPLAYIO_SVIDEO:
482 {
483 int d = *(int *)data;
484 if (d == sc->sc_video_on)
485 return 0;
486 sc->sc_video_on = d;
487 if (d == WSDISPLAYIO_VIDEO_ON) {
488 crmfb_write_reg(sc,
489 CRMFB_VT_FLAGS, sc->sc_vtflags);
490 } else {
491 /* turn all SYNCs off */
492 crmfb_write_reg(sc, CRMFB_VT_FLAGS,
493 sc->sc_vtflags | CRMFB_VT_FLAGS_VDRV_LOW |
494 CRMFB_VT_FLAGS_HDRV_LOW |
495 CRMFB_VT_FLAGS_SYNC_LOW);
496 }
497 }
498 return 0;
499
500 case WSDISPLAYIO_GVIDEO:
501 *(int *)data = sc->sc_video_on;
502 return 0;
503
504 case WSDISPLAYIO_GCURPOS:
505 {
506 struct wsdisplay_curpos *pos;
507
508 pos = (struct wsdisplay_curpos *)data;
509 pos->x = sc->sc_cur_x;
510 pos->y = sc->sc_cur_y;
511 }
512 return 0;
513 case WSDISPLAYIO_SCURPOS:
514 {
515 struct wsdisplay_curpos *pos;
516
517 pos = (struct wsdisplay_curpos *)data;
518 crmfb_set_curpos(sc, pos->x, pos->y);
519 }
520 return 0;
521 case WSDISPLAYIO_GCURMAX:
522 {
523 struct wsdisplay_curpos *pos;
524
525 pos = (struct wsdisplay_curpos *)data;
526 pos->x = 32;
527 pos->y = 32;
528 }
529 return 0;
530 case WSDISPLAYIO_GCURSOR:
531 {
532 struct wsdisplay_cursor *cu;
533
534 cu = (struct wsdisplay_cursor *)data;
535 return crmfb_gcursor(sc, cu);
536 }
537 case WSDISPLAYIO_SCURSOR:
538 {
539 struct wsdisplay_cursor *cu;
540
541 cu = (struct wsdisplay_cursor *)data;
542 return crmfb_scursor(sc, cu);
543 }
544 case WSDISPLAYIO_GET_EDID: {
545 struct wsdisplayio_edid_info *d = data;
546
547 d->data_size = 128;
548 if (d->buffer_size < 128)
549 return EAGAIN;
550 if (sc->sc_edid_data[1] == 0)
551 return ENODATA;
552 return copyout(sc->sc_edid_data, d->edid_data, 128);
553 }
554 }
555 return EPASSTHROUGH;
556 }
557
558 static paddr_t
559 crmfb_mmap(void *v, void *vs, off_t offset, int prot)
560 {
561 struct vcons_data *vd;
562 struct crmfb_softc *sc;
563 paddr_t pa;
564
565 vd = (struct vcons_data *)v;
566 sc = (struct crmfb_softc *)vd->cookie;
567
568 /* we probably shouldn't let anyone mmap the framebuffer */
569 #if 1
570 if (offset >= 0 && offset < (0x100000 * sc->sc_tiles_x)) {
571 pa = bus_dmamem_mmap(sc->sc_dmat, sc->sc_dma.segs,
572 sc->sc_dma.nsegs, offset, prot,
573 BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_PREFETCHABLE);
574 return pa;
575 }
576 #endif
577 /*
578 * here would the TLBs be but we don't want to show them to userland
579 * so we return the page containing the status register
580 */
581 if ((offset >= 0x15000000) && (offset < 0x15002000))
582 return bus_space_mmap(sc->sc_iot, 0x15004000, 0, prot, 0);
583 /* now the actual engine registers */
584 if ((offset >= 0x15002000) && (offset < 0x15005000))
585 return bus_space_mmap(sc->sc_iot, offset, 0, prot, 0);
586 /* and now the linear area */
587 if ((offset >= 0x15010000) && (offset < 0x15030000))
588 return bus_dmamem_mmap(sc->sc_dmat, sc->sc_dma.segs,
589 sc->sc_dma.nsegs,
590 offset + (0x100000 * sc->sc_tiles_x) - 0x15010000, prot,
591 BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_PREFETCHABLE);
592 return -1;
593 }
594
595 static void
596 crmfb_init_screen(void *c, struct vcons_screen *scr, int existing,
597 long *defattr)
598 {
599 struct crmfb_softc *sc;
600 struct rasops_info *ri;
601
602 sc = (struct crmfb_softc *)c;
603 ri = &scr->scr_ri;
604
605 ri->ri_flg = RI_CENTER | RI_FULLCLEAR | RI_ENABLE_ALPHA;
606 ri->ri_depth = sc->sc_console_depth;
607 ri->ri_width = sc->sc_width;
608 ri->ri_height = sc->sc_height;
609 ri->ri_stride = ri->ri_width * (ri->ri_depth / 8);
610
611 switch (ri->ri_depth) {
612 case 8:
613 ri->ri_flg |= RI_8BIT_IS_RGB;
614 break;
615 case 16:
616 ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 5;
617 ri->ri_rpos = 11;
618 ri->ri_gpos = 6;
619 ri->ri_bpos = 1;
620 break;
621 case 32:
622 ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
623 ri->ri_rpos = 8;
624 ri->ri_gpos = 16;
625 ri->ri_bpos = 24;
626 break;
627 }
628
629 ri->ri_bits = NULL;
630
631 rasops_init(ri, 0, 0);
632 ri->ri_caps = WSSCREEN_WSCOLORS;
633 rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
634 ri->ri_width / ri->ri_font->fontwidth);
635 ri->ri_hw = scr;
636
637 ri->ri_ops.cursor = crmfb_cursor;
638 ri->ri_ops.copyrows = crmfb_copyrows;
639 ri->ri_ops.eraserows = crmfb_eraserows;
640 ri->ri_ops.copycols = crmfb_copycols;
641 ri->ri_ops.erasecols = crmfb_erasecols;
642 if (FONT_IS_ALPHA(ri->ri_font)) {
643 ri->ri_ops.putchar = crmfb_putchar_aa;
644 } else {
645 ri->ri_ops.putchar = crmfb_putchar;
646 }
647 return;
648 }
649
650 static int
651 crmfb_putcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm)
652 {
653 u_int idx, cnt;
654 u_char r[256], g[256], b[256];
655 u_char *rp, *gp, *bp;
656 int rv, i;
657
658 idx = cm->index;
659 cnt = cm->count;
660
661 if (idx >= 255 || cnt > 256 || idx + cnt > 256)
662 return EINVAL;
663
664 rv = copyin(cm->red, &r[idx], cnt);
665 if (rv)
666 return rv;
667 rv = copyin(cm->green, &g[idx], cnt);
668 if (rv)
669 return rv;
670 rv = copyin(cm->blue, &b[idx], cnt);
671 if (rv)
672 return rv;
673
674 memcpy(&sc->sc_cmap_red[idx], &r[idx], cnt);
675 memcpy(&sc->sc_cmap_green[idx], &g[idx], cnt);
676 memcpy(&sc->sc_cmap_blue[idx], &b[idx], cnt);
677
678 rp = &sc->sc_cmap_red[idx];
679 gp = &sc->sc_cmap_green[idx];
680 bp = &sc->sc_cmap_blue[idx];
681
682 for (i = 0; i < cnt; i++) {
683 crmfb_set_palette(sc, idx, *rp, *gp, *bp);
684 idx++;
685 rp++, gp++, bp++;
686 }
687
688 return 0;
689 }
690
691 static int
692 crmfb_getcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm)
693 {
694 u_int idx, cnt;
695 int rv;
696
697 idx = cm->index;
698 cnt = cm->count;
699
700 if (idx >= 255 || cnt > 256 || idx + cnt > 256)
701 return EINVAL;
702
703 rv = copyout(&sc->sc_cmap_red[idx], cm->red, cnt);
704 if (rv)
705 return rv;
706 rv = copyout(&sc->sc_cmap_green[idx], cm->green, cnt);
707 if (rv)
708 return rv;
709 rv = copyout(&sc->sc_cmap_blue[idx], cm->blue, cnt);
710 if (rv)
711 return rv;
712
713 return 0;
714 }
715
716 static void
717 crmfb_set_palette(struct crmfb_softc *sc, int reg, uint8_t r, uint8_t g,
718 uint8_t b)
719 {
720 uint32_t val;
721
722 if (reg > 255 || sc->sc_depth != 8)
723 return;
724
725 while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_CMAP_FIFO) >= 63)
726 DELAY(10);
727
728 val = (r << 8) | (g << 16) | (b << 24);
729 crmfb_write_reg(sc, CRMFB_CMAP + (reg * 4), val);
730
731 return;
732 }
733
734 static int
735 crmfb_set_curpos(struct crmfb_softc *sc, int x, int y)
736 {
737 uint32_t val;
738
739 sc->sc_cur_x = x;
740 sc->sc_cur_y = y;
741
742 val = ((x - sc->sc_hot_x) & 0xffff) | ((y - sc->sc_hot_y) << 16);
743 crmfb_write_reg(sc, CRMFB_CURSOR_POS, val);
744
745 return 0;
746 }
747
748 static int
749 crmfb_gcursor(struct crmfb_softc *sc, struct wsdisplay_cursor *cur)
750 {
751 /* do nothing for now */
752 return 0;
753 }
754
755 static int
756 crmfb_scursor(struct crmfb_softc *sc, struct wsdisplay_cursor *cur)
757 {
758 if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
759
760 crmfb_write_reg(sc, CRMFB_CURSOR_CONTROL, cur->enable ? 1 : 0);
761 }
762 if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
763
764 sc->sc_hot_x = cur->hot.x;
765 sc->sc_hot_y = cur->hot.y;
766 }
767 if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
768
769 crmfb_set_curpos(sc, cur->pos.x, cur->pos.y);
770 }
771 if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
772 int i;
773 uint32_t val;
774
775 for (i = 0; i < cur->cmap.count; i++) {
776 val = (cur->cmap.red[i] << 24) |
777 (cur->cmap.green[i] << 16) |
778 (cur->cmap.blue[i] << 8);
779 crmfb_write_reg(sc, CRMFB_CURSOR_CMAP0 +
780 ((i + cur->cmap.index) << 2), val);
781 }
782 }
783 if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
784
785 int i, j, cnt = 0;
786 uint32_t latch = 0, omask;
787 uint8_t imask;
788 for (i = 0; i < 64; i++) {
789 omask = 0x80000000;
790 imask = 0x01;
791 cur->image[cnt] &= cur->mask[cnt];
792 for (j = 0; j < 8; j++) {
793 if (cur->image[cnt] & imask)
794 latch |= omask;
795 omask >>= 1;
796 if (cur->mask[cnt] & imask)
797 latch |= omask;
798 omask >>= 1;
799 imask <<= 1;
800 }
801 cnt++;
802 imask = 0x01;
803 cur->image[cnt] &= cur->mask[cnt];
804 for (j = 0; j < 8; j++) {
805 if (cur->image[cnt] & imask)
806 latch |= omask;
807 omask >>= 1;
808 if (cur->mask[cnt] & imask)
809 latch |= omask;
810 omask >>= 1;
811 imask <<= 1;
812 }
813 cnt++;
814 crmfb_write_reg(sc, CRMFB_CURSOR_BITMAP + (i << 2),
815 latch);
816 latch = 0;
817 }
818 }
819 return 0;
820 }
821
822 static inline void
823 crmfb_write_reg(struct crmfb_softc *sc, int offset, uint32_t val)
824 {
825
826 bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, val);
827 wbflush();
828 }
829
830 static inline uint32_t
831 crmfb_read_reg(struct crmfb_softc *sc, int offset)
832 {
833
834 return bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset);
835 }
836
837 static inline void
838 crmfb_wait_idle(struct crmfb_softc *sc)
839 {
840 int i = 0;
841
842 do {
843 i++;
844 } while (((bus_space_read_4(sc->sc_iot, sc->sc_reh, CRIME_DE_STATUS) &
845 CRIME_DE_IDLE) == 0) && (i < 100000000));
846 if (i >= 100000000)
847 aprint_error("crmfb_wait_idle() timed out\n");
848 sc->sc_needs_sync = 0;
849 }
850
851 /* writes to CRIME_DE_MODE_* only take effect when the engine is idle */
852
853 static inline void
854 crmfb_src_mode(struct crmfb_softc *sc, uint32_t mode)
855 {
856 if (mode == sc->sc_src_mode)
857 return;
858 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_MODE_SRC, mode);
859 sc->sc_needs_sync = 1;
860 sc->sc_src_mode = mode;
861 }
862
863 static inline void
864 crmfb_dst_mode(struct crmfb_softc *sc, uint32_t mode)
865 {
866 if (mode == sc->sc_dst_mode)
867 return;
868 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_MODE_DST, mode);
869 sc->sc_needs_sync = 1;
870 sc->sc_dst_mode = mode;
871 }
872
873 static inline void
874 crmfb_make_room(struct crmfb_softc *sc, int num)
875 {
876 int i = 0, slots;
877 uint32_t status;
878
879 if (sc->sc_needs_sync != 0) {
880 crmfb_wait_idle(sc);
881 return;
882 }
883
884 do {
885 i++;
886 status = bus_space_read_4(sc->sc_iot, sc->sc_reh,
887 CRIME_DE_STATUS);
888 slots = 60 - CRIME_PIPE_LEVEL(status);
889 } while (slots <= num);
890 }
891
892 static int
893 crmfb_wait_dma_idle(struct crmfb_softc *sc)
894 {
895 int bail = 100000, idle;
896
897 do {
898 idle = ((bus_space_read_4(sc->sc_iot, sc->sc_ioh,
899 CRMFB_OVR_CONTROL) & 1) == 0) &&
900 ((bus_space_read_4(sc->sc_iot, sc->sc_ioh,
901 CRMFB_FRM_CONTROL) & 1) == 0) &&
902 ((bus_space_read_4(sc->sc_iot, sc->sc_ioh,
903 CRMFB_DID_CONTROL) & 1) == 0);
904 if (!idle)
905 delay(10);
906 bail--;
907 } while ((!idle) && (bail > 0));
908 return idle;
909 }
910
911 static int
912 crmfb_setup_video(struct crmfb_softc *sc, int depth)
913 {
914 uint64_t reg;
915 uint32_t d, h, page;
916 int i, bail, tile_width, tlbptr, lptr, j, tx, shift, overhang;
917 const char *wantsync;
918 uint16_t v;
919
920 /* disable DMA */
921 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_CONTROL);
922 d &= ~(1 << CRMFB_OVR_CONTROL_DMAEN_SHIFT);
923 crmfb_write_reg(sc, CRMFB_OVR_CONTROL, d);
924 DELAY(50000);
925 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
926 d &= ~(1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
927 crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d);
928 DELAY(50000);
929 crmfb_write_reg(sc, CRMFB_DID_CONTROL, d);
930 DELAY(50000);
931
932 if (!crmfb_wait_dma_idle(sc))
933 aprint_error("crmfb: crmfb_wait_dma_idle timed out\n");
934
935 /* ensure that CRM starts drawing at the top left of the screen
936 * when we re-enable DMA later
937 */
938 d = (1 << CRMFB_VT_XY_FREEZE_SHIFT);
939 crmfb_write_reg(sc, CRMFB_VT_XY, d);
940 delay(1000);
941 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK);
942 d &= ~(1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT);
943 crmfb_write_reg(sc, CRMFB_DOTCLOCK, d);
944
945 /* wait for dotclock to turn off */
946 bail = 10000;
947 while ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK) &
948 (1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT)) && (bail > 0)) {
949 delay(10);
950 bail--;
951 }
952
953 /* reset FIFO */
954 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE);
955 d |= (1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT);
956 crmfb_write_reg(sc, CRMFB_FRM_TILESIZE, d);
957 d &= ~(1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT);
958 crmfb_write_reg(sc, CRMFB_FRM_TILESIZE, d);
959
960 /* setup colour mode */
961 switch (depth) {
962 case 8:
963 h = CRMFB_MODE_TYP_RG3B2;
964 tile_width = 512;
965 break;
966 case 16:
967 h = CRMFB_MODE_TYP_ARGB5;
968 tile_width = 256;
969 break;
970 case 32:
971 h = CRMFB_MODE_TYP_RGB8;
972 tile_width = 128;
973 break;
974 default:
975 panic("Unsupported depth");
976 }
977 d = h << CRMFB_MODE_TYP_SHIFT;
978 d |= CRMFB_MODE_BUF_BOTH << CRMFB_MODE_BUF_SHIFT;
979 for (i = 0; i < (32 * 4); i += 4)
980 bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_MODE + i, d);
981 wbflush();
982
983 /* setup tile pointer, but don't turn on DMA yet! */
984 h = DMAADDR(sc->sc_dmai);
985 d = (h >> 9) << CRMFB_FRM_CONTROL_TILEPTR_SHIFT;
986 crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d);
987
988 /* init framebuffer width and pixel size */
989 /*d = (1 << CRMFB_FRM_TILESIZE_WIDTH_SHIFT);*/
990
991 d = ((int)(sc->sc_width / tile_width)) <<
992 CRMFB_FRM_TILESIZE_WIDTH_SHIFT;
993 overhang = sc->sc_width % tile_width;
994 if (overhang != 0) {
995 uint32_t val;
996 DPRINTF("tile width: %d\n", tile_width);
997 DPRINTF("overhang: %d\n", overhang);
998 val = (overhang * (depth >> 3)) >> 5;
999 DPRINTF("reg: %08x\n", val);
1000 d |= (val & 0x1f);
1001 DPRINTF("d: %08x\n", d);
1002 }
1003
1004 switch (depth) {
1005 case 8:
1006 h = CRMFB_FRM_TILESIZE_DEPTH_8;
1007 break;
1008 case 16:
1009 h = CRMFB_FRM_TILESIZE_DEPTH_16;
1010 break;
1011 case 32:
1012 h = CRMFB_FRM_TILESIZE_DEPTH_32;
1013 break;
1014 default:
1015 panic("Unsupported depth");
1016 }
1017 d |= (h << CRMFB_FRM_TILESIZE_DEPTH_SHIFT);
1018 crmfb_write_reg(sc, CRMFB_FRM_TILESIZE, d);
1019
1020 /*h = sc->sc_width * sc->sc_height / (512 / (depth >> 3));*/
1021 h = sc->sc_height;
1022 d = h << CRMFB_FRM_PIXSIZE_HEIGHT_SHIFT;
1023 crmfb_write_reg(sc, CRMFB_FRM_PIXSIZE, d);
1024
1025 /* turn off firmware overlay and hardware cursor */
1026 crmfb_write_reg(sc, CRMFB_OVR_WIDTH_TILE, 0);
1027 crmfb_write_reg(sc, CRMFB_CURSOR_CONTROL, 0);
1028
1029 /* turn on DMA for the framebuffer */
1030 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
1031 d |= (1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
1032 crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d);
1033
1034 /* enable drawing again */
1035 crmfb_write_reg(sc, CRMFB_VT_XY, 0);
1036 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK);
1037 d |= (1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT);
1038 crmfb_write_reg(sc, CRMFB_DOTCLOCK, d);
1039
1040 /* turn off sync-on-green */
1041
1042 wantsync = arcbios_GetEnvironmentVariable("SyncOnGreen");
1043 if ( (wantsync != NULL) && (wantsync[0] == 'n') ) {
1044 sc->sc_vtflags |= CRMFB_VT_FLAGS_SYNC_LOW;
1045 crmfb_write_reg(sc, CRMFB_VT_FLAGS, d);
1046 }
1047
1048 sc->sc_depth = depth;
1049
1050 /* finally set up the drawing engine's TLB A */
1051 v = (DMAADDR(sc->sc_dma) >> 16) & 0xffff;
1052 tlbptr = 0;
1053 tx = ((sc->sc_width + (tile_width - 1)) & ~(tile_width - 1)) /
1054 tile_width;
1055
1056 DPRINTF("tx: %d\n", tx);
1057
1058 for (i = 0; i < 16; i++) {
1059 reg = 0;
1060 shift = 64;
1061 lptr = 0;
1062 for (j = 0; j < tx; j++) {
1063 shift -= 16;
1064 reg |= (((uint64_t)(v | 0x8000)) << shift);
1065 if (shift == 0) {
1066 shift = 64;
1067 bus_space_write_8(sc->sc_iot, sc->sc_reh,
1068 CRIME_RE_TLB_A + tlbptr + lptr,
1069 reg);
1070 DPRINTF("%04x: %016"PRIx64"\n", tlbptr + lptr, reg);
1071 reg = 0;
1072 lptr += 8;
1073 }
1074 v++;
1075 }
1076 if (shift != 64) {
1077 bus_space_write_8(sc->sc_iot, sc->sc_reh,
1078 CRIME_RE_TLB_A + tlbptr + lptr, reg);
1079 DPRINTF("%04x: %016"PRIx64"\n", tlbptr + lptr, reg);
1080 }
1081 tlbptr += 32;
1082 }
1083
1084 /* now put the last 128kB into the 1st linear TLB */
1085 page = (sc->sc_linear >> 12) | 0x80000000;
1086 tlbptr = 0;
1087 for (i = 0; i < 16; i++) {
1088 reg = ((uint64_t)page << 32) | (page + 1);
1089 bus_space_write_8(sc->sc_iot, sc->sc_reh,
1090 CRIME_RE_LINEAR_A + tlbptr, reg);
1091 page += 2;
1092 tlbptr += 8;
1093 }
1094 wbflush();
1095
1096 /* do some very basic engine setup */
1097 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_CLIPMODE, 0);
1098 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_WINOFFSET_SRC, 0);
1099 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_WINOFFSET_DST, 0);
1100 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_PLANEMASK,
1101 0xffffffff);
1102
1103 bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x20, 0);
1104 bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x28, 0);
1105 bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x30, 0);
1106 bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x38, 0);
1107 bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x40, 0);
1108
1109 switch (depth) {
1110 case 8:
1111 sc->sc_de_mode = DE_MODE_TLB_A | DE_MODE_BUFDEPTH_8 |
1112 DE_MODE_TYPE_CI | DE_MODE_PIXDEPTH_8;
1113 sc->sc_mte_mode = MTE_MODE_DST_ECC |
1114 (MTE_TLB_A << MTE_DST_TLB_SHIFT) |
1115 (MTE_TLB_A << MTE_SRC_TLB_SHIFT) |
1116 (MTE_DEPTH_8 << MTE_DEPTH_SHIFT);
1117 sc->sc_mte_x_shift = 0;
1118 break;
1119 case 16:
1120 sc->sc_de_mode = DE_MODE_TLB_A | DE_MODE_BUFDEPTH_16 |
1121 DE_MODE_TYPE_RGBA | DE_MODE_PIXDEPTH_16;
1122 sc->sc_mte_mode = MTE_MODE_DST_ECC |
1123 (MTE_TLB_A << MTE_DST_TLB_SHIFT) |
1124 (MTE_TLB_A << MTE_SRC_TLB_SHIFT) |
1125 (MTE_DEPTH_16 << MTE_DEPTH_SHIFT);
1126 sc->sc_mte_x_shift = 1;
1127 break;
1128 case 32:
1129 sc->sc_de_mode = DE_MODE_TLB_A | DE_MODE_BUFDEPTH_32 |
1130 DE_MODE_TYPE_RGBA | DE_MODE_PIXDEPTH_32;
1131 break;
1132 sc->sc_mte_mode = MTE_MODE_DST_ECC |
1133 (MTE_TLB_A << MTE_DST_TLB_SHIFT) |
1134 (MTE_TLB_A << MTE_SRC_TLB_SHIFT) |
1135 (MTE_DEPTH_32 << MTE_DEPTH_SHIFT);
1136 sc->sc_mte_x_shift = 2;
1137 break;
1138 default:
1139 panic("%s: unsuported colour depth %d\n", __func__,
1140 depth);
1141 }
1142 sc->sc_needs_sync = 0;
1143 sc->sc_src_mode = 0xffffffff;
1144 sc->sc_dst_mode = 0xffffffff;
1145
1146 crmfb_src_mode(sc, sc->sc_de_mode);
1147 crmfb_dst_mode(sc, sc->sc_de_mode);
1148
1149 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_STEP_X, 1);
1150 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_STEP_Y, 1);
1151
1152 /* initialize memory transfer engine */
1153 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_MODE,
1154 sc->sc_mte_mode | MTE_MODE_COPY);
1155 sc->sc_mte_direction = 1;
1156 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST_Y_STEP, 1);
1157 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC_Y_STEP, 1);
1158
1159 return 0;
1160 }
1161
1162 static void
1163 crmfb_set_mte_direction(struct crmfb_softc *sc, int dir)
1164 {
1165 if (dir == sc->sc_mte_direction)
1166 return;
1167
1168 crmfb_make_room(sc, 2);
1169 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST_Y_STEP, dir);
1170 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC_Y_STEP, dir);
1171 sc->sc_mte_direction = dir;
1172 }
1173
1174 static void
1175 crmfb_setup_palette(struct crmfb_softc *sc)
1176 {
1177 int i, j, x;
1178 uint32_t col;
1179 struct rasops_info *ri = &crmfb_console_screen.scr_ri;
1180
1181 for (i = 0; i < 256; i++) {
1182 crmfb_set_palette(sc, i, rasops_cmap[(i * 3) + 2],
1183 rasops_cmap[(i * 3) + 1], rasops_cmap[(i * 3) + 0]);
1184 sc->sc_cmap_red[i] = rasops_cmap[(i * 3) + 2];
1185 sc->sc_cmap_green[i] = rasops_cmap[(i * 3) + 1];
1186 sc->sc_cmap_blue[i] = rasops_cmap[(i * 3) + 0];
1187 }
1188
1189 if (FONT_IS_ALPHA(ri->ri_font)) {
1190 sc->sc_de_mode =
1191 (sc->sc_de_mode & ~DE_MODE_TYPE_MASK) | DE_MODE_TYPE_RGB;
1192 }
1193
1194 /* draw 16 character cells in 32bit RGBA for alpha blending */
1195 crmfb_make_room(sc, 3);
1196 crmfb_dst_mode(sc,
1197 DE_MODE_TLB_A |
1198 DE_MODE_BUFDEPTH_32 |
1199 DE_MODE_TYPE_RGBA |
1200 DE_MODE_PIXDEPTH_32);
1201 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_DRAWMODE,
1202 DE_DRAWMODE_PLANEMASK | DE_DRAWMODE_BYTEMASK);
1203 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_PRIMITIVE,
1204 DE_PRIM_RECTANGLE | DE_PRIM_TB);
1205 j = 0;
1206 x = 0;
1207 for (i = 0; i < 16; i++) {
1208 crmfb_make_room(sc, 2);
1209 col = (rasops_cmap[j] << 24) |
1210 (rasops_cmap[j + 1] << 16) |
1211 (rasops_cmap[j + 2] << 8);
1212 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_FG, col);
1213 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_X_VERTEX_0,
1214 (x << 16) | ((sc->sc_height - 500) & 0xffff));
1215 bus_space_write_4(sc->sc_iot, sc->sc_reh,
1216 CRIME_DE_X_VERTEX_1 | CRIME_DE_START,
1217 ((x + ri->ri_font->fontwidth - 1) << 16) |
1218 ((sc->sc_height + ri->ri_font->fontheight - 1) & 0xffff));
1219 j += 3;
1220 x += ri->ri_font->fontwidth;
1221 }
1222 crmfb_dst_mode(sc, sc->sc_de_mode);
1223 }
1224
1225 static void
1226 crmfb_fill_rect(struct crmfb_softc *sc, int x, int y, int width, int height,
1227 uint32_t colour)
1228 {
1229 int rxa, rxe;
1230
1231 rxa = x << sc->sc_mte_x_shift;
1232 rxe = ((x + width) << sc->sc_mte_x_shift) - 1;
1233 crmfb_set_mte_direction(sc, 1);
1234 crmfb_make_room(sc, 4);
1235 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_MODE,
1236 sc->sc_mte_mode | 0);
1237 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_BG, colour);
1238 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST0,
1239 (rxa << 16) | (y & 0xffff));
1240 bus_space_write_4(sc->sc_iot, sc->sc_reh,
1241 CRIME_MTE_DST1 | CRIME_DE_START,
1242 (rxe << 16) | ((y + height - 1) & 0xffff));
1243 }
1244
1245 static void
1246 crmfb_bitblt(struct crmfb_softc *sc, int xs, int ys, int xd, int yd,
1247 int wi, int he, uint32_t rop)
1248 {
1249 uint32_t prim = DE_PRIM_RECTANGLE;
1250 int rxa, rya, rxe, rye, rxs, rys;
1251 crmfb_make_room(sc, 2);
1252 crmfb_src_mode(sc, sc->sc_de_mode);
1253 crmfb_make_room(sc, 6);
1254 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_DRAWMODE,
1255 DE_DRAWMODE_PLANEMASK | DE_DRAWMODE_BYTEMASK | DE_DRAWMODE_ROP |
1256 DE_DRAWMODE_XFER_EN);
1257 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_ROP, rop);
1258 if (xs < xd) {
1259 prim |= DE_PRIM_RL;
1260 rxe = xd;
1261 rxa = xd + wi - 1;
1262 rxs = xs + wi - 1;
1263 } else {
1264 prim |= DE_PRIM_LR;
1265 rxe = xd + wi - 1;
1266 rxa = xd;
1267 rxs = xs;
1268 }
1269 if (ys < yd) {
1270 prim |= DE_PRIM_BT;
1271 rye = yd;
1272 rya = yd + he - 1;
1273 rys = ys + he - 1;
1274 } else {
1275 prim |= DE_PRIM_TB;
1276 rye = yd + he - 1;
1277 rya = yd;
1278 rys = ys;
1279 }
1280 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_PRIMITIVE, prim);
1281 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_ADDR_SRC,
1282 (rxs << 16) | (rys & 0xffff));
1283 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_X_VERTEX_0,
1284 (rxa << 16) | (rya & 0xffff));
1285 bus_space_write_4(sc->sc_iot, sc->sc_reh,
1286 CRIME_DE_X_VERTEX_1 | CRIME_DE_START,
1287 (rxe << 16) | (rye & 0xffff));
1288 }
1289
1290 static void
1291 crmfb_scroll(struct crmfb_softc *sc, int xs, int ys, int xd, int yd,
1292 int wi, int he)
1293 {
1294 int rxa, rya, rxe, rye, rxd, ryd, rxde, ryde;
1295
1296 rxa = xs << sc->sc_mte_x_shift;
1297 rxd = xd << sc->sc_mte_x_shift;
1298 rxe = ((xs + wi) << sc->sc_mte_x_shift) - 1;
1299 rxde = ((xd + wi) << sc->sc_mte_x_shift) - 1;
1300
1301 crmfb_make_room(sc, 1);
1302
1303 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_MODE,
1304 sc->sc_mte_mode | MTE_MODE_COPY);
1305
1306 if (ys < yd) {
1307 /* bottom to top */
1308 rye = ys;
1309 rya = ys + he - 1;
1310 ryd = yd + he - 1;
1311 ryde = yd;
1312 crmfb_set_mte_direction(sc, -1);
1313 } else {
1314 /* top to bottom */
1315 rye = ys + he - 1;
1316 rya = ys;
1317 ryd = yd;
1318 ryde = yd + he - 1;
1319 crmfb_set_mte_direction(sc, 1);
1320 }
1321 crmfb_make_room(sc, 4);
1322 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC0,
1323 (rxa << 16) | rya);
1324 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC1,
1325 (rxe << 16) | rye);
1326 bus_space_write_4(sc->sc_iot, sc->sc_reh,
1327 CRIME_MTE_DST0,
1328 (rxd << 16) | ryd);
1329 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST1 |
1330 CRIME_DE_START,
1331 (rxde << 16) | ryde);
1332 }
1333
1334 static void
1335 crmfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
1336 {
1337 struct rasops_info *ri = cookie;
1338 struct vcons_screen *scr = ri->ri_hw;
1339 int32_t xs, xd, y, width, height;
1340
1341 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
1342 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
1343 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1344 width = ri->ri_font->fontwidth * ncols;
1345 height = ri->ri_font->fontheight;
1346 crmfb_bitblt(scr->scr_cookie, xs, y, xd, y, width, height, 3);
1347 }
1348
1349 static void
1350 crmfb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
1351 {
1352 struct rasops_info *ri = cookie;
1353 struct vcons_screen *scr = ri->ri_hw;
1354 int32_t x, y, width, height, bg;
1355
1356 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
1357 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1358 width = ri->ri_font->fontwidth * ncols;
1359 height = ri->ri_font->fontheight;
1360 bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff];
1361 crmfb_fill_rect(scr->scr_cookie, x, y, width, height, bg);
1362 }
1363
1364 static void
1365 crmfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
1366 {
1367 struct rasops_info *ri = cookie;
1368 struct vcons_screen *scr = ri->ri_hw;
1369 int32_t x, ys, yd, width, height;
1370
1371 x = ri->ri_xorigin;
1372 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
1373 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
1374 width = ri->ri_emuwidth;
1375 height = ri->ri_font->fontheight * nrows;
1376
1377 crmfb_scroll(scr->scr_cookie, x, ys, x, yd, width, height);
1378 }
1379
1380 static void
1381 crmfb_eraserows(void *cookie, int row, int nrows, long fillattr)
1382 {
1383 struct rasops_info *ri = cookie;
1384 struct vcons_screen *scr = ri->ri_hw;
1385 int32_t x, y, width, height, bg;
1386
1387 if ((row == 0) && (nrows == ri->ri_rows)) {
1388 x = y = 0;
1389 width = ri->ri_width;
1390 height = ri->ri_height;
1391 } else {
1392 x = ri->ri_xorigin;
1393 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1394 width = ri->ri_emuwidth;
1395 height = ri->ri_font->fontheight * nrows;
1396 }
1397 bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff];
1398 crmfb_fill_rect(scr->scr_cookie, x, y, width, height, bg);
1399 }
1400
1401 static void
1402 crmfb_cursor(void *cookie, int on, int row, int col)
1403 {
1404 struct rasops_info *ri = cookie;
1405 struct vcons_screen *scr = ri->ri_hw;
1406 struct crmfb_softc *sc = scr->scr_cookie;
1407 int x, y, wi,he;
1408
1409 wi = ri->ri_font->fontwidth;
1410 he = ri->ri_font->fontheight;
1411
1412 if (ri->ri_flg & RI_CURSOR) {
1413 x = ri->ri_ccol * wi + ri->ri_xorigin;
1414 y = ri->ri_crow * he + ri->ri_yorigin;
1415 crmfb_bitblt(sc, x, y, x, y, wi, he, 12);
1416 ri->ri_flg &= ~RI_CURSOR;
1417 }
1418
1419 ri->ri_crow = row;
1420 ri->ri_ccol = col;
1421
1422 if (on)
1423 {
1424 x = ri->ri_ccol * wi + ri->ri_xorigin;
1425 y = ri->ri_crow * he + ri->ri_yorigin;
1426 crmfb_bitblt(sc, x, y, x, y, wi, he, 12);
1427 ri->ri_flg |= RI_CURSOR;
1428 }
1429 }
1430
1431 static void
1432 crmfb_putchar(void *cookie, int row, int col, u_int c, long attr)
1433 {
1434 struct rasops_info *ri = cookie;
1435 struct vcons_screen *scr = ri->ri_hw;
1436 struct crmfb_softc *sc = scr->scr_cookie;
1437 struct wsdisplay_font *font = PICK_FONT(ri, c);
1438 uint32_t bg, fg;
1439 int x, y, wi, he, i, uc;
1440 uint8_t *fd8;
1441 uint16_t *fd16;
1442 void *fd;
1443
1444 wi = font->fontwidth;
1445 he = font->fontheight;
1446
1447 x = ri->ri_xorigin + col * wi;
1448 y = ri->ri_yorigin + row * he;
1449
1450 bg = ri->ri_devcmap[(attr >> 16) & 0xff];
1451 fg = ri->ri_devcmap[(attr >> 24) & 0xff];
1452 uc = c - font->firstchar;
1453 fd = (uint8_t *)font->data + uc * ri->ri_fontscale;
1454 if (c == 0x20) {
1455 crmfb_fill_rect(sc, x, y, wi, he, bg);
1456 } else {
1457 crmfb_make_room(sc, 6);
1458 /* setup */
1459 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_DRAWMODE,
1460 DE_DRAWMODE_PLANEMASK | DE_DRAWMODE_BYTEMASK |
1461 DE_DRAWMODE_ROP |
1462 DE_DRAWMODE_OPAQUE_STIP | DE_DRAWMODE_POLY_STIP);
1463 wbflush();
1464 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_ROP, 3);
1465 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_FG, fg);
1466 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_BG, bg);
1467 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_PRIMITIVE,
1468 DE_PRIM_RECTANGLE | DE_PRIM_LR | DE_PRIM_TB);
1469 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_STIPPLE_MODE,
1470 0x001f0000);
1471 /* now let's feed the engine */
1472 crmfb_make_room(sc, 30);
1473 if (font->stride == 1) {
1474 /* shovel in 8 bit quantities */
1475 fd8 = fd;
1476 for (i = 0; i < he; i++) {
1477 if (i & 8)
1478 crmfb_make_room(sc, 30);
1479 bus_space_write_4(sc->sc_iot, sc->sc_reh,
1480 CRIME_DE_STIPPLE_PAT, *fd8 << 24);
1481 bus_space_write_4(sc->sc_iot, sc->sc_reh,
1482 CRIME_DE_X_VERTEX_0, (x << 16) | y);
1483 bus_space_write_4(sc->sc_iot, sc->sc_reh,
1484 CRIME_DE_X_VERTEX_1 | CRIME_DE_START,
1485 ((x + wi) << 16) | y);
1486 y++;
1487 fd8++;
1488 }
1489 } else if (font->stride == 2) {
1490 /* shovel in 16 bit quantities */
1491 fd16 = fd;
1492 for (i = 0; i < he; i++) {
1493 if (i & 8)
1494 crmfb_make_room(sc, 30);
1495 bus_space_write_4(sc->sc_iot, sc->sc_reh,
1496 CRIME_DE_STIPPLE_PAT, *fd16 << 16);
1497 bus_space_write_4(sc->sc_iot, sc->sc_reh,
1498 CRIME_DE_X_VERTEX_0, (x << 16) | y);
1499 bus_space_write_4(sc->sc_iot, sc->sc_reh,
1500 CRIME_DE_X_VERTEX_1 | CRIME_DE_START,
1501 ((x + wi) << 16) | y);
1502 y++;
1503 fd16++;
1504 }
1505 }
1506 }
1507 }
1508
1509 static void
1510 crmfb_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
1511 {
1512 struct rasops_info *ri = cookie;
1513 struct vcons_screen *scr = ri->ri_hw;
1514 struct crmfb_softc *sc = scr->scr_cookie;
1515 struct wsdisplay_font *font = PICK_FONT(ri, c);
1516 uint32_t bg, fg;
1517 int x, y, wi, he, uc, xx;
1518 void *fd;
1519
1520 wi = font->fontwidth;
1521 he = font->fontheight;
1522
1523 x = ri->ri_xorigin + col * wi;
1524 y = ri->ri_yorigin + row * he;
1525
1526 bg = ri->ri_devcmap[(attr >> 16) & 0xff];
1527 fg = (attr >> 24);
1528 uc = c - font->firstchar;
1529 fd = (uint8_t *)font->data + uc * ri->ri_fontscale;
1530
1531 /* fill the cell with the background colour */
1532 crmfb_fill_rect(sc, x, y, wi, he, bg);
1533
1534 /* if all we draw is a space we're done */
1535 if (c == 0x20)
1536 return;
1537
1538 /* copy the glyph into the linear buffer */
1539 memcpy(sc->sc_lptr, fd, ri->ri_fontscale);
1540 wbflush();
1541
1542 /* now blit it on top of the requested fg colour cell */
1543 xx = fg * wi;
1544 crmfb_make_room(sc, 2);
1545 crmfb_src_mode(sc,
1546 DE_MODE_LIN_A |
1547 DE_MODE_BUFDEPTH_8 |
1548 DE_MODE_TYPE_CI |
1549 DE_MODE_PIXDEPTH_8);
1550 crmfb_dst_mode(sc,
1551 DE_MODE_TLB_A |
1552 DE_MODE_BUFDEPTH_32 |
1553 DE_MODE_TYPE_CI |
1554 DE_MODE_PIXDEPTH_8);
1555
1556 crmfb_make_room(sc, 6);
1557 /* only write into the alpha channel */
1558 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_DRAWMODE,
1559 DE_DRAWMODE_PLANEMASK | 0x08 |
1560 DE_DRAWMODE_XFER_EN);
1561 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_PRIMITIVE,
1562 DE_PRIM_RECTANGLE | DE_PRIM_TB);
1563 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_STRD_SRC, 1);
1564 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_ADDR_SRC, 0);
1565 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_X_VERTEX_0,
1566 (xx << 16) | (sc->sc_height & 0xffff));
1567 bus_space_write_4(sc->sc_iot, sc->sc_reh,
1568 CRIME_DE_X_VERTEX_1 | CRIME_DE_START,
1569 ((xx + wi - 1) << 16) | ((sc->sc_height + he - 1) & 0xffff));
1570
1571 /* now draw the actual character */
1572 crmfb_make_room(sc, 2);
1573 crmfb_src_mode(sc,
1574 DE_MODE_TLB_A |
1575 DE_MODE_BUFDEPTH_32 |
1576 DE_MODE_TYPE_RGBA |
1577 DE_MODE_PIXDEPTH_32);
1578 crmfb_dst_mode(sc, sc->sc_de_mode);
1579
1580 crmfb_make_room(sc, 6);
1581 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_DRAWMODE,
1582 DE_DRAWMODE_PLANEMASK | DE_DRAWMODE_BYTEMASK |
1583 DE_DRAWMODE_ALPHA_BLEND |
1584 DE_DRAWMODE_XFER_EN);
1585 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_ALPHA_FUNC,
1586 DE_ALPHA_ADD |
1587 (DE_ALPHA_OP_SRC_ALPHA << DE_ALPHA_OP_SRC_SHIFT) |
1588 (DE_ALPHA_OP_1_MINUS_SRC_ALPHA << DE_ALPHA_OP_DST_SHIFT));
1589 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_PRIMITIVE,
1590 DE_PRIM_RECTANGLE | DE_PRIM_TB);
1591 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_ADDR_SRC,
1592 (xx << 16) | (sc->sc_height & 0xffff));
1593 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_X_VERTEX_0,
1594 (x << 16) | (y & 0xffff));
1595 bus_space_write_4(sc->sc_iot, sc->sc_reh,
1596 CRIME_DE_X_VERTEX_1 | CRIME_DE_START,
1597 ((x + wi - 1) << 16) | ((y + he - 1) & 0xffff));
1598 }
1599
1600 static void
1601 crmfb_setup_ddc(struct crmfb_softc *sc)
1602 {
1603 int i;
1604
1605 memset(sc->sc_edid_data, 0, 128);
1606 sc->sc_i2c.ic_cookie = sc;
1607 sc->sc_i2c.ic_acquire_bus = crmfb_i2c_acquire_bus;
1608 sc->sc_i2c.ic_release_bus = crmfb_i2c_release_bus;
1609 sc->sc_i2c.ic_send_start = crmfb_i2c_send_start;
1610 sc->sc_i2c.ic_send_stop = crmfb_i2c_send_stop;
1611 sc->sc_i2c.ic_initiate_xfer = crmfb_i2c_initiate_xfer;
1612 sc->sc_i2c.ic_read_byte = crmfb_i2c_read_byte;
1613 sc->sc_i2c.ic_write_byte = crmfb_i2c_write_byte;
1614 sc->sc_i2c.ic_exec = NULL;
1615 i = 0;
1616 while (sc->sc_edid_data[1] == 0 && i++ < 10)
1617 ddc_read_edid(&sc->sc_i2c, sc->sc_edid_data, 128);
1618 if (i > 1)
1619 aprint_debug_dev(sc->sc_dev,
1620 "had to try %d times to get EDID data\n", i);
1621 if (i < 11) {
1622 edid_parse(sc->sc_edid_data, &sc->sc_edid_info);
1623 edid_print(&sc->sc_edid_info);
1624 }
1625 }
1626
1627 /* I2C bitbanging */
1628 static void
1629 crmfb_i2cbb_set_bits(void *cookie, uint32_t bits)
1630 {
1631 struct crmfb_softc *sc = cookie;
1632
1633 bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_I2C_VGA, bits ^ 3);
1634 }
1635
1636 static void
1637 crmfb_i2cbb_set_dir(void *cookie, uint32_t dir)
1638 {
1639
1640 /* Nothing to do */
1641 }
1642
1643 static uint32_t
1644 crmfb_i2cbb_read(void *cookie)
1645 {
1646 struct crmfb_softc *sc = cookie;
1647
1648 return bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_I2C_VGA) ^ 3;
1649 }
1650
1651 /* higher level I2C stuff */
1652 static int
1653 crmfb_i2c_acquire_bus(void *cookie, int flags)
1654 {
1655
1656 /* private bus */
1657 return 0;
1658 }
1659
1660 static void
1661 crmfb_i2c_release_bus(void *cookie, int flags)
1662 {
1663
1664 /* private bus */
1665 }
1666
1667 static int
1668 crmfb_i2c_send_start(void *cookie, int flags)
1669 {
1670
1671 return i2c_bitbang_send_start(cookie, flags, &crmfb_i2cbb_ops);
1672 }
1673
1674 static int
1675 crmfb_i2c_send_stop(void *cookie, int flags)
1676 {
1677
1678 return i2c_bitbang_send_stop(cookie, flags, &crmfb_i2cbb_ops);
1679 }
1680
1681 static int
1682 crmfb_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
1683 {
1684
1685 return i2c_bitbang_initiate_xfer(cookie, addr, flags,
1686 &crmfb_i2cbb_ops);
1687 }
1688
1689 static int
1690 crmfb_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
1691 {
1692
1693 return i2c_bitbang_read_byte(cookie, valp, flags, &crmfb_i2cbb_ops);
1694 }
1695
1696 static int
1697 crmfb_i2c_write_byte(void *cookie, uint8_t val, int flags)
1698 {
1699
1700 return i2c_bitbang_write_byte(cookie, val, flags, &crmfb_i2cbb_ops);
1701 }
1702
1703 /* mode setting stuff */
1704 static uint32_t
1705 calc_pll(int f_out)
1706 {
1707 uint32_t ret;
1708 int f_in = 20000; /* 20MHz in */
1709 int M, N, P;
1710 int error, div, best = 9999999;
1711 int ff1, ff2;
1712 int MM = 0, NN = 0, PP = 0, ff = 0;
1713
1714 /* f_out = M * f_in / (N * (1 << P) */
1715
1716 for (P = 0; P < 4; P++) {
1717 for (N = 64; N > 0; N--) {
1718 div = N * (1 << P);
1719 M = f_out * div / f_in;
1720 if ((M < 257) && (M > 100)) {
1721 ff1 = M * f_in / div;
1722 ff2 = (M + 1) * f_in / div;
1723 error = abs(ff1 - f_out);
1724 if (error < best) {
1725 MM = M;
1726 NN = N;
1727 PP = P;
1728 ff = ff1;
1729 best = error;
1730 }
1731 error = abs(ff2 - f_out);
1732 if ((error < best) && ( M < 256)){
1733 MM = M + 1;
1734 NN = N;
1735 PP = P;
1736 ff = ff2;
1737 best = error;
1738 }
1739 }
1740 }
1741 }
1742 DPRINTF("%d: M %d N %d P %d -> %d\n", f_out, MM, NN, PP, ff);
1743 /* now shove the parameters into the register's format */
1744 ret = (MM - 1) | ((NN - 1) << 8) | (P << 14);
1745 return ret;
1746 }
1747
1748 static int
1749 crmfb_set_mode(struct crmfb_softc *sc, const struct videomode *mode)
1750 {
1751 uint32_t d, dc;
1752 int tmp, diff;
1753
1754 switch (mode->hdisplay % 32) {
1755 case 0:
1756 sc->sc_console_depth = 8;
1757 break;
1758 case 16:
1759 sc->sc_console_depth = 16;
1760 break;
1761 case 8:
1762 case 24:
1763 sc->sc_console_depth = 32;
1764 break;
1765 default:
1766 aprint_error_dev(sc->sc_dev,
1767 "hdisplay (%d) is not a multiple of 32\n",
1768 mode->hdisplay);
1769 return FALSE;
1770 }
1771 if (mode->dot_clock > 150000) {
1772 aprint_error_dev(sc->sc_dev,
1773 "requested dot clock is too high ( %d MHz )\n",
1774 mode->dot_clock / 1000);
1775 return FALSE;
1776 }
1777
1778 /* disable DMA */
1779 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_CONTROL);
1780 d &= ~(1 << CRMFB_OVR_CONTROL_DMAEN_SHIFT);
1781 crmfb_write_reg(sc, CRMFB_OVR_CONTROL, d);
1782 DELAY(50000);
1783 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
1784 d &= ~(1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
1785 crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d);
1786 DELAY(50000);
1787 crmfb_write_reg(sc, CRMFB_DID_CONTROL, d);
1788 DELAY(50000);
1789
1790 if (!crmfb_wait_dma_idle(sc))
1791 aprint_error("crmfb: crmfb_wait_dma_idle timed out\n");
1792
1793 /* ok, now we're good to go */
1794 dc = calc_pll(mode->dot_clock);
1795
1796 crmfb_write_reg(sc, CRMFB_VT_XY, 1 << CRMFB_VT_XY_FREEZE_SHIFT);
1797 delay(1000);
1798
1799 /* set the dot clock pll but don't start it yet */
1800 crmfb_write_reg(sc, CRMFB_DOTCLOCK, dc);
1801 delay(10000);
1802
1803 /* pixel counter */
1804 d = mode->htotal | (mode->vtotal << 12);
1805 crmfb_write_reg(sc, CRMFB_VT_XYMAX, d);
1806
1807 /* video timings */
1808 d = mode->vsync_end | (mode->vsync_start << 12);
1809 crmfb_write_reg(sc, CRMFB_VT_VSYNC, d);
1810
1811 d = mode->hsync_end | (mode->hsync_start << 12);
1812 crmfb_write_reg(sc, CRMFB_VT_HSYNC, d);
1813
1814 d = mode->vtotal | (mode->vdisplay << 12);
1815 crmfb_write_reg(sc, CRMFB_VT_VBLANK, d);
1816
1817 d = (mode->htotal - 5) | ((mode->hdisplay - 5) << 12);
1818 crmfb_write_reg(sc, CRMFB_VT_HBLANK, d);
1819
1820 d = mode->vtotal | (mode->vdisplay << 12);
1821 crmfb_write_reg(sc, CRMFB_VT_VCMAP, d);
1822 d = mode->htotal | (mode->hdisplay << 12);
1823 crmfb_write_reg(sc, CRMFB_VT_HCMAP, d);
1824
1825 d = 0;
1826 if (mode->flags & VID_NHSYNC) d |= CRMFB_VT_FLAGS_HDRV_INVERT;
1827 if (mode->flags & VID_NVSYNC) d |= CRMFB_VT_FLAGS_VDRV_INVERT;
1828 crmfb_write_reg(sc, CRMFB_VT_FLAGS, d);
1829 sc->sc_vtflags = d;
1830
1831 diff = -abs(mode->vtotal - mode->vdisplay - 1);
1832 d = ((uint32_t)diff << 12) & 0x00fff000;
1833 d |= (mode->htotal - 20);
1834 crmfb_write_reg(sc, CRMFB_VT_DID_STARTXY, d);
1835
1836 d = ((uint32_t)(diff + 1) << 12) & 0x00fff000;
1837 d |= (mode->htotal - 54);
1838 crmfb_write_reg(sc, CRMFB_VT_CRS_STARTXY, d);
1839
1840 d = ((uint32_t)diff << 12) & 0x00fff000;
1841 d |= (mode->htotal - 4);
1842 crmfb_write_reg(sc, CRMFB_VT_VC_STARTXY, d);
1843
1844 tmp = mode->htotal - 19;
1845 d = tmp << 12;
1846 d |= ((tmp + mode->hdisplay - 2) % mode->htotal);
1847 crmfb_write_reg(sc, CRMFB_VT_HPIX_EN, d);
1848
1849 d = mode->vdisplay | (mode->vtotal << 12);
1850 crmfb_write_reg(sc, CRMFB_VT_VPIX_EN, d);
1851
1852 sc->sc_width = mode->hdisplay;
1853 sc->sc_height = mode->vdisplay;
1854
1855 return TRUE;
1856 }
1857