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