sunxi_debe.c revision 1.12 1 /* $NetBSD: sunxi_debe.c,v 1.12 2021/01/27 03:10:20 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2018 Manuel Bouyer <bouyer (at) antioche.eu.org>
5 * All rights reserved.
6 *
7 * Copyright (c) 2014 Jared D. McNeill <jmcneill (at) invisible.ca>
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include "genfb.h"
33
34 #ifndef SUNXI_DEBE_VIDEOMEM
35 #define SUNXI_DEBE_VIDEOMEM (16 * 1024 * 1024)
36 #endif
37
38 #define SUNXI_DEBE_CURMAX 64
39
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: sunxi_debe.c,v 1.12 2021/01/27 03:10:20 thorpej Exp $");
42
43 #include <sys/param.h>
44 #include <sys/bus.h>
45 #include <sys/device.h>
46 #include <sys/intr.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/mutex.h>
50 #include <sys/condvar.h>
51
52 #include <dev/fdt/fdtvar.h>
53 #include <dev/fdt/fdt_port.h>
54
55 #include <dev/videomode/videomode.h>
56 #include <dev/wscons/wsconsio.h>
57 #include <dev/wsfb/genfbvar.h>
58
59 #include <arm/sunxi/sunxi_debereg.h>
60 #include <arm/sunxi/sunxi_display.h>
61 #include <arm/sunxi/sunxi_platform.h>
62 #include <machine/bootconfig.h>
63
64 enum sunxi_debe_type {
65 DEBE_A10 = 1,
66 };
67
68 struct sunxi_debe_softc {
69 device_t sc_dev;
70 device_t sc_fbdev;
71 enum sunxi_debe_type sc_type;
72 bus_space_tag_t sc_bst;
73 bus_space_handle_t sc_bsh;
74 bus_dma_tag_t sc_dmat;
75
76 struct clk *sc_clk_ahb;
77 struct clk *sc_clk_mod;
78 struct clk *sc_clk_ram;
79
80 struct fdtbus_reset *sc_rst;
81
82 bus_dma_segment_t sc_dmasegs[1];
83 bus_size_t sc_dmasize;
84 bus_dmamap_t sc_dmamap;
85 void *sc_dmap;
86
87 bool sc_cursor_enable;
88 int sc_cursor_x, sc_cursor_y;
89 int sc_hot_x, sc_hot_y;
90 uint8_t sc_cursor_bitmap[8 * SUNXI_DEBE_CURMAX];
91 uint8_t sc_cursor_mask[8 * SUNXI_DEBE_CURMAX];
92
93 int sc_phandle;
94 struct fdt_device_ports sc_ports;
95 struct fdt_endpoint *sc_out_ep;
96 int sc_unit; /* debe0 or debe1 */
97 };
98
99 #define DEBE_READ(sc, reg) \
100 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
101 #define DEBE_WRITE(sc, reg, val) \
102 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
103
104 static const struct device_compatible_entry compat_data[] = {
105 { .compat = "allwinner,sun4i-a10-display-backend", .value = DEBE_A10 },
106 { .compat = "allwinner,sun7i-a20-display-backend", .value = DEBE_A10 },
107 DEVICE_COMPAT_EOL
108 };
109
110 struct sunxifb_attach_args {
111 void *afb_fb;
112 uint32_t afb_width;
113 uint32_t afb_height;
114 bus_dma_tag_t afb_dmat;
115 bus_dma_segment_t *afb_dmasegs;
116 int afb_ndmasegs;
117 };
118
119 static void sunxi_debe_ep_connect(device_t, struct fdt_endpoint *, bool);
120 static int sunxi_debe_ep_enable(device_t, struct fdt_endpoint *, bool);
121 static int sunxi_debe_match(device_t, cfdata_t, void *);
122 static void sunxi_debe_attach(device_t, device_t, void *);
123
124 static int sunxi_debe_alloc_videomem(struct sunxi_debe_softc *);
125 static void sunxi_debe_setup_fbdev(struct sunxi_debe_softc *,
126 const struct videomode *);
127
128 static int sunxi_debe_set_curpos(struct sunxi_debe_softc *, int, int);
129 static int sunxi_debe_set_cursor(struct sunxi_debe_softc *,
130 struct wsdisplay_cursor *);
131 static int sunxi_debe_ioctl(device_t, u_long, void *);
132 static void sunxi_befb_set_videomode(device_t, u_int, u_int);
133 void sunxi_debe_dump_regs(int);
134
135 static struct sunxi_debe_softc *debe_console_sc;
136 static int sunxi_simplefb_phandle = -1;
137
138 CFATTACH_DECL_NEW(sunxi_debe, sizeof(struct sunxi_debe_softc),
139 sunxi_debe_match, sunxi_debe_attach, NULL, NULL);
140
141 static int
142 sunxi_debe_match(device_t parent, cfdata_t cf, void *aux)
143 {
144 struct fdt_attach_args * const faa = aux;
145
146 return of_compatible_match(faa->faa_phandle, compat_data);
147 }
148
149 static void
150 sunxi_debe_attach(device_t parent, device_t self, void *aux)
151 {
152 struct sunxi_debe_softc *sc = device_private(self);
153 struct fdt_attach_args * const faa = aux;
154 const int phandle = faa->faa_phandle;
155 bus_addr_t addr;
156 bus_size_t size;
157 int error;
158
159 sc->sc_dev = self;
160 sc->sc_phandle = phandle;
161 sc->sc_bst = faa->faa_bst;
162 sc->sc_dmat = faa->faa_dmat;
163 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
164 aprint_error(": couldn't get registers\n");
165 }
166 if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
167 aprint_error(": couldn't map registers\n");
168 return;
169 }
170
171 sc->sc_clk_ahb = fdtbus_clock_get(phandle, "ahb");
172 sc->sc_clk_mod = fdtbus_clock_get(phandle, "mod");
173 sc->sc_clk_ram = fdtbus_clock_get(phandle, "ram");
174
175 if (sc->sc_clk_ahb == NULL || sc->sc_clk_mod == NULL
176 || sc->sc_clk_ram == NULL) {
177 aprint_error(": couldn't get clocks\n");
178 aprint_debug_dev(self, "clk ahb %s mod %s ram %s\n",
179 sc->sc_clk_ahb == NULL ? "missing" : "present",
180 sc->sc_clk_mod == NULL ? "missing" : "present",
181 sc->sc_clk_ram == NULL ? "missing" : "present");
182 return;
183 }
184
185 sc->sc_rst = fdtbus_reset_get_index(phandle, 0);
186 if (sc->sc_rst == NULL) {
187 aprint_error(": couldn't get reset\n");
188 return;
189 }
190
191 sc->sc_type =
192 of_compatible_lookup(faa->faa_phandle, compat_data)->value;
193
194 aprint_naive("\n");
195 aprint_normal(": Display Engine Backend (%s)\n",
196 fdtbus_get_string(phandle, "name"));
197
198
199 sc->sc_dmasize = SUNXI_DEBE_VIDEOMEM;
200
201 error = sunxi_debe_alloc_videomem(sc);
202 if (error) {
203 aprint_error_dev(sc->sc_dev,
204 "couldn't allocate video memory, error = %d\n", error);
205 return;
206 }
207
208 sc->sc_unit = -1;
209 sc->sc_ports.dp_ep_connect = sunxi_debe_ep_connect;
210 sc->sc_ports.dp_ep_enable = sunxi_debe_ep_enable;
211 fdt_ports_register(&sc->sc_ports, self, phandle, EP_OTHER);
212 }
213
214 static void
215 sunxi_debe_doreset(void)
216 {
217 device_t dev;
218 struct sunxi_debe_softc *sc;
219 int error;
220
221 for (int i = 0;;i++) {
222 dev = device_find_by_driver_unit("sunxidebe", i);
223 if (dev == NULL)
224 return;
225 sc = device_private(dev);
226
227 if (fdtbus_reset_assert(sc->sc_rst) != 0) {
228 aprint_error_dev(dev, ": couldn't assert reset\n");
229 return;
230 }
231 delay(1);
232 if (fdtbus_reset_deassert(sc->sc_rst) != 0) {
233 aprint_error_dev(dev, ": couldn't de-assert reset\n");
234 return;
235 }
236
237
238 error = clk_set_rate(sc->sc_clk_mod, 300000000);
239 if (error) {
240 aprint_error_dev(dev,
241 "couln't set mod clock rate (%d)\n", error);
242 return;
243 }
244
245 if (clk_enable(sc->sc_clk_ahb) != 0 ||
246 clk_enable(sc->sc_clk_mod) != 0) {
247 aprint_error_dev(dev, ": couldn't enable clocks\n");
248 return;
249 }
250 if (clk_disable(sc->sc_clk_ram) != 0) {
251 aprint_error_dev(dev, ": couldn't disable ram clock\n");
252 }
253
254 for (unsigned int reg = 0x800; reg < 0x1000; reg += 4) {
255 DEBE_WRITE(sc, reg, 0);
256 }
257
258 DEBE_WRITE(sc, SUNXI_DEBE_MODCTL_REG, SUNXI_DEBE_MODCTL_EN);
259
260 DEBE_WRITE(sc, SUNXI_DEBE_HWC_PALETTE_TABLE, 0);
261
262 if (clk_disable(sc->sc_clk_ahb) != 0 ||
263 clk_disable(sc->sc_clk_mod) != 0) {
264 aprint_error_dev(sc->sc_dev,
265 ": couldn't disable clocks\n");
266 }
267 }
268 }
269
270 static void
271 sunxi_debe_ep_connect(device_t self, struct fdt_endpoint *ep, bool connect)
272 {
273 struct sunxi_debe_softc *sc = device_private(self);
274 struct fdt_endpoint *rep = fdt_endpoint_remote(ep);
275 int rep_idx = fdt_endpoint_index(rep);
276
277 KASSERT(device_is_a(self, "sunxidebe"));
278 if (!connect) {
279 aprint_error_dev(self, "endpoint disconnect not supported\n");
280 return;
281 }
282
283 if (fdt_endpoint_port_index(ep) == 1) {
284 bool do_print = (sc->sc_unit == -1);
285 /*
286 * one of our output endpoints has been connected.
287 * the remote id is our unit number
288 */
289 if (sc->sc_unit != -1 && rep_idx != -1 &&
290 sc->sc_unit != rep_idx) {
291 aprint_error_dev(self, ": remote id %d doens't match"
292 " discovered unit number %d\n",
293 rep_idx, sc->sc_unit);
294 return;
295 }
296 if (!device_is_a(fdt_endpoint_device(rep), "sunxitcon")) {
297 aprint_error_dev(self,
298 ": output %d connected to unknown device\n",
299 fdt_endpoint_index(ep));
300 return;
301 }
302 if (rep_idx != -1)
303 sc->sc_unit = rep_idx;
304 else {
305 /* assume only one debe */
306 sc->sc_unit = 0;
307 }
308 if (do_print)
309 aprint_verbose_dev(self, "debe unit %d\n", sc->sc_unit);
310 }
311 }
312
313 static int
314 sunxi_debe_alloc_videomem(struct sunxi_debe_softc *sc)
315 {
316 int error, nsegs;
317
318 error = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dmasize, 0x1000, 0,
319 sc->sc_dmasegs, 1, &nsegs, BUS_DMA_WAITOK);
320 if (error)
321 return error;
322 error = bus_dmamem_map(sc->sc_dmat, sc->sc_dmasegs, nsegs,
323 sc->sc_dmasize, &sc->sc_dmap, BUS_DMA_WAITOK | BUS_DMA_COHERENT);
324 if (error)
325 goto free;
326 error = bus_dmamap_create(sc->sc_dmat, sc->sc_dmasize, 1,
327 sc->sc_dmasize, 0, BUS_DMA_WAITOK, &sc->sc_dmamap);
328 if (error)
329 goto unmap;
330 error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap, sc->sc_dmap,
331 sc->sc_dmasize, NULL, BUS_DMA_WAITOK);
332 if (error)
333 goto destroy;
334
335 memset(sc->sc_dmap, 0, sc->sc_dmasize);
336
337 return 0;
338
339 destroy:
340 bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap);
341 unmap:
342 bus_dmamem_unmap(sc->sc_dmat, sc->sc_dmap, sc->sc_dmasize);
343 free:
344 bus_dmamem_free(sc->sc_dmat, sc->sc_dmasegs, nsegs);
345
346 sc->sc_dmasize = 0;
347 sc->sc_dmap = NULL;
348
349 return error;
350 }
351
352 static void
353 sunxi_debe_setup_fbdev(struct sunxi_debe_softc *sc, const struct videomode *mode)
354 {
355 if (mode == NULL)
356 return;
357
358 const u_int interlace_p = !!(mode->flags & VID_INTERLACE);
359 const u_int fb_width = mode->hdisplay;
360 const u_int fb_height = (mode->vdisplay << interlace_p);
361
362 if (mode && sc->sc_fbdev == NULL) {
363 /* see if we are the console */
364 if (sunxi_simplefb_phandle >= 0) {
365 const char *cons_pipeline =
366 fdtbus_get_string(sunxi_simplefb_phandle,
367 "allwinner,pipeline");
368 struct fdt_endpoint *ep = fdt_endpoint_get_from_index(
369 &sc->sc_ports, SUNXI_PORT_OUTPUT, sc->sc_unit);
370 struct fdt_endpoint *rep = fdt_endpoint_remote(ep);
371 if (sunxi_tcon_is_console(
372 fdt_endpoint_device(rep), cons_pipeline))
373 debe_console_sc = sc;
374 } else if (debe_console_sc == NULL) {
375 if (match_bootconf_option(boot_args,
376 "console", "fb0")) {
377 if (sc->sc_unit == 0)
378 debe_console_sc = sc;
379 } else if (match_bootconf_option(boot_args,
380 "console", "fb1")) {
381 if (sc->sc_unit == 1)
382 debe_console_sc = sc;
383 } else if (match_bootconf_option(boot_args,
384 "console", "fb")) {
385 /* match first activated */
386 debe_console_sc = sc;
387 }
388 }
389 struct sunxifb_attach_args afb = {
390 .afb_fb = sc->sc_dmap,
391 .afb_width = fb_width,
392 .afb_height = fb_height,
393 .afb_dmat = sc->sc_dmat,
394 .afb_dmasegs = sc->sc_dmasegs,
395 .afb_ndmasegs = 1
396 };
397 sc->sc_fbdev = config_found_ia(sc->sc_dev, "sunxidebe",
398 &afb, NULL);
399 } else if (sc->sc_fbdev != NULL) {
400 sunxi_befb_set_videomode(sc->sc_fbdev, fb_width, fb_height);
401 }
402 }
403
404 static int
405 sunxi_debe_set_curpos(struct sunxi_debe_softc *sc, int x, int y)
406 {
407 int xx, yy;
408 u_int yoff, xoff;
409
410 xoff = yoff = 0;
411 xx = x - sc->sc_hot_x;
412 yy = y - sc->sc_hot_y;
413 if (xx < 0) {
414 xoff -= xx;
415 xx = 0;
416 }
417 if (yy < 0) {
418 yoff -= yy;
419 yy = 0;
420 }
421
422 DEBE_WRITE(sc, SUNXI_DEBE_HWCCTL_REG,
423 __SHIFTIN(yy, SUNXI_DEBE_HWCCTL_YCOOR) |
424 __SHIFTIN(xx, SUNXI_DEBE_HWCCTL_XCOOR));
425 DEBE_WRITE(sc, SUNXI_DEBE_HWCFBCTL_REG,
426 #if SUNXI_DEBE_CURMAX == 32
427 __SHIFTIN(SUNXI_DEBE_HWCFBCTL_YSIZE_32, SUNXI_DEBE_HWCFBCTL_YSIZE) |
428 __SHIFTIN(SUNXI_DEBE_HWCFBCTL_XSIZE_32, SUNXI_DEBE_HWCFBCTL_XSIZE) |
429 #else
430 __SHIFTIN(SUNXI_DEBE_HWCFBCTL_YSIZE_64, SUNXI_DEBE_HWCFBCTL_YSIZE) |
431 __SHIFTIN(SUNXI_DEBE_HWCFBCTL_XSIZE_64, SUNXI_DEBE_HWCFBCTL_XSIZE) |
432 #endif
433 __SHIFTIN(SUNXI_DEBE_HWCFBCTL_FBFMT_2BPP, SUNXI_DEBE_HWCFBCTL_FBFMT) |
434 __SHIFTIN(yoff, SUNXI_DEBE_HWCFBCTL_YCOOROFF) |
435 __SHIFTIN(xoff, SUNXI_DEBE_HWCFBCTL_XCOOROFF));
436
437 return 0;
438 }
439
440 static int
441 sunxi_debe_set_cursor(struct sunxi_debe_softc *sc, struct wsdisplay_cursor *cur)
442 {
443 uint32_t val;
444 uint8_t r[4], g[4], b[4];
445 u_int index, count, shift, off, pcnt;
446 int i, j, idx, error;
447 uint8_t mask;
448
449 if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
450 val = DEBE_READ(sc, SUNXI_DEBE_MODCTL_REG);
451 if (cur->enable)
452 val |= SUNXI_DEBE_MODCTL_HWC_EN;
453 else
454 val &= ~SUNXI_DEBE_MODCTL_HWC_EN;
455 DEBE_WRITE(sc, SUNXI_DEBE_MODCTL_REG, val);
456
457 sc->sc_cursor_enable = cur->enable;
458 }
459
460 if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
461 sc->sc_hot_x = cur->hot.x;
462 sc->sc_hot_y = cur->hot.y;
463 cur->which |= WSDISPLAY_CURSOR_DOPOS;
464 }
465
466 if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
467 sunxi_debe_set_curpos(sc, cur->pos.x, cur->pos.y);
468 }
469
470 if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
471 index = cur->cmap.index;
472 count = cur->cmap.count;
473 if (index >= 2 || count > 2 - index)
474 return EINVAL;
475 error = copyin(cur->cmap.red, &r[index], count);
476 if (error)
477 return error;
478 error = copyin(cur->cmap.green, &g[index], count);
479 if (error)
480 return error;
481 error = copyin(cur->cmap.blue, &b[index], count);
482 if (error)
483 return error;
484
485 for (i = index; i < (index + count); i++) {
486 DEBE_WRITE(sc,
487 SUNXI_DEBE_HWC_PALETTE_TABLE + (4 * (i + 2)),
488 (r[i] << 16) | (g[i] << 8) | b[i] | 0xff000000);
489 }
490 }
491
492 if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
493 error = copyin(cur->mask, sc->sc_cursor_mask,
494 SUNXI_DEBE_CURMAX * 8);
495 if (error)
496 return error;
497 error = copyin(cur->image, sc->sc_cursor_bitmap,
498 SUNXI_DEBE_CURMAX * 8);
499 if (error)
500 return error;
501 }
502
503 if (cur->which & (WSDISPLAY_CURSOR_DOCMAP|WSDISPLAY_CURSOR_DOSHAPE)) {
504 for (i = 0, pcnt = 0; i < SUNXI_DEBE_CURMAX * 8; i++) {
505 for (j = 0, mask = 1; j < 8; j++, mask <<= 1, pcnt++) {
506 idx = ((sc->sc_cursor_mask[i] & mask) ? 2 : 0) |
507 ((sc->sc_cursor_bitmap[i] & mask) ? 1 : 0);
508 off = (pcnt >> 4) * 4;
509 shift = (pcnt & 0xf) * 2;
510 val = DEBE_READ(sc,
511 SUNXI_DEBE_HWC_PATTERN_BLOCK + off);
512 val &= ~(3 << shift);
513 val |= (idx << shift);
514 DEBE_WRITE(sc,
515 SUNXI_DEBE_HWC_PATTERN_BLOCK + off, val);
516 }
517 }
518 }
519
520 return 0;
521 }
522
523 static int
524 sunxi_debe_ep_enable(device_t dev, struct fdt_endpoint *ep, bool enable)
525 {
526 struct sunxi_debe_softc *sc;
527 uint32_t val;
528
529 KASSERT(device_is_a(dev, "sunxidebe"));
530 sc = device_private(dev);
531
532 if (enable) {
533 if (clk_enable(sc->sc_clk_ram) != 0) {
534 device_printf(dev,
535 ": warning: failed to enable ram clock\n");
536 }
537 val = DEBE_READ(sc, SUNXI_DEBE_REGBUFFCTL_REG);
538 val |= SUNXI_DEBE_REGBUFFCTL_REGLOADCTL;
539 DEBE_WRITE(sc, SUNXI_DEBE_REGBUFFCTL_REG, val);
540
541 val = DEBE_READ(sc, SUNXI_DEBE_MODCTL_REG);
542 val |= SUNXI_DEBE_MODCTL_START_CTL;
543 DEBE_WRITE(sc, SUNXI_DEBE_MODCTL_REG, val);
544 #ifdef SUNXI_DEBE_DEBUG
545 sunxi_debe_dump_regs(sc->sc_unit);
546 #endif
547 } else {
548 val = DEBE_READ(sc, SUNXI_DEBE_MODCTL_REG);
549 val &= ~SUNXI_DEBE_MODCTL_START_CTL;
550 DEBE_WRITE(sc, SUNXI_DEBE_MODCTL_REG, val);
551 if (clk_disable(sc->sc_clk_ram) != 0) {
552 device_printf(dev,
553 ": warning: failed to disable ram clock\n");
554 }
555 }
556 #if 0
557 for (int i = 0; i < 0x1000; i += 4) {
558 printf("DEBE 0x%04x: 0x%08x\n", i, DEBE_READ(sc, i));
559 }
560 #endif
561 return 0;
562 }
563
564 /*
565 * FIXME 2020/10/19
566 * This function is not called actually at the moment.
567 */
568 void
569 sunxi_debe_set_videomode(device_t dev, const struct videomode *mode)
570 {
571 struct sunxi_debe_softc *sc;
572 uint32_t val;
573
574 KASSERT(device_is_a(dev, "sunxidebe"));
575 sc = device_private(dev);
576
577 if (mode) {
578 const u_int interlace_p = !!(mode->flags & VID_INTERLACE);
579 const u_int width = mode->hdisplay;
580 const u_int height = (mode->vdisplay << interlace_p);
581 const u_int fb_width = width;
582 const u_int fb_height = height;
583 uint32_t vmem = width * height * 4;
584
585 if (vmem > sc->sc_dmasize) {
586 device_printf(sc->sc_dev,
587 "not enough memory for %ux%u fb (req %u have %u)\n",
588 width, height, vmem, (unsigned int)sc->sc_dmasize);
589 return;
590 }
591
592 paddr_t pa = sc->sc_dmamap->dm_segs[0].ds_addr;
593 #if !defined(ALLWINNER_A80) && 0
594 #define SUNXI_SDRAM_PBASE-0 0x40000000
595 /*
596 * On 2GB systems, we need to subtract AWIN_SDRAM_PBASE from
597 * the phys addr.
598 */
599 if (pa >= SUNXI_SDRAM_PBASE)
600 pa -= SUNXI_SDRAM_PBASE;
601 #endif
602
603 /* notify fb */
604 sunxi_debe_setup_fbdev(sc, mode);
605
606 DEBE_WRITE(sc, SUNXI_DEBE_DISSIZE_REG,
607 ((height - 1) << 16) | (width - 1));
608 DEBE_WRITE(sc, SUNXI_DEBE_LAYSIZE_REG,
609 ((fb_height - 1) << 16) | (fb_width - 1));
610 DEBE_WRITE(sc, SUNXI_DEBE_LAYCOOR_REG, 0);
611 DEBE_WRITE(sc, SUNXI_DEBE_LAYLINEWIDTH_REG, (fb_width << 5));
612 DEBE_WRITE(sc, SUNXI_DEBE_LAYFB_L32ADD_REG, pa << 3);
613 DEBE_WRITE(sc, SUNXI_DEBE_LAYFB_H4ADD_REG, pa >> 29);
614
615 val = DEBE_READ(sc, SUNXI_DEBE_ATTCTL1_REG);
616 val &= ~SUNXI_DEBE_ATTCTL1_LAY_FBFMT;
617 val |= __SHIFTIN(SUNXI_DEBE_ATTCTL1_LAY_FBFMT_XRGB8888,
618 SUNXI_DEBE_ATTCTL1_LAY_FBFMT);
619 val &= ~SUNXI_DEBE_ATTCTL1_LAY_BRSWAPEN;
620 val &= ~SUNXI_DEBE_ATTCTL1_LAY_FBPS;
621 #if 0 /* __ARMEB__ */
622 /*
623 * For big endian, we dynamically override FDT to let
624 * genfb(4) know that framebuffer is byte-swapped.
625 * See fdt_update_fb_format() in fdt_machdep.c.
626 */
627 val |= __SHIFTIN(SUNXI_DEBE_ATTCTL1_LAY_FBPS_32BPP_BGRA,
628 SUNXI_DEBE_ATTCTL1_LAY_FBPS);
629 #else
630 val |= __SHIFTIN(SUNXI_DEBE_ATTCTL1_LAY_FBPS_32BPP_ARGB,
631 SUNXI_DEBE_ATTCTL1_LAY_FBPS);
632 #endif
633 DEBE_WRITE(sc, SUNXI_DEBE_ATTCTL1_REG, val);
634
635 val = DEBE_READ(sc, SUNXI_DEBE_MODCTL_REG);
636 val |= SUNXI_DEBE_MODCTL_LAY0_EN;
637 if (interlace_p) {
638 val |= SUNXI_DEBE_MODCTL_ITLMOD_EN;
639 } else {
640 val &= ~SUNXI_DEBE_MODCTL_ITLMOD_EN;
641 }
642 val &= ~SUNXI_DEBE_MODCTL_OUT_SEL;
643 if (sc->sc_unit == 1) {
644 val |= __SHIFTIN(SUNXI_DEBE_MODCTL_OUT_SEL_LCD1,
645 SUNXI_DEBE_MODCTL_OUT_SEL);
646 }
647 DEBE_WRITE(sc, SUNXI_DEBE_MODCTL_REG, val);
648 } else {
649 /* disable */
650 val = DEBE_READ(sc, SUNXI_DEBE_MODCTL_REG);
651 val &= ~SUNXI_DEBE_MODCTL_LAY0_EN;
652 val &= ~SUNXI_DEBE_MODCTL_START_CTL;
653 DEBE_WRITE(sc, SUNXI_DEBE_MODCTL_REG, val);
654
655 /* notify fb */
656 sunxi_debe_setup_fbdev(sc, mode);
657 }
658 }
659
660 static int
661 sunxi_debe_ioctl(device_t self, u_long cmd, void *data)
662 {
663 struct sunxi_debe_softc *sc = device_private(self);
664 struct wsdisplay_curpos *cp;
665 uint32_t val;
666 int enable;
667
668 switch (cmd) {
669 case WSDISPLAYIO_SVIDEO:
670 enable = *(int *)data;
671 val = DEBE_READ(sc, SUNXI_DEBE_MODCTL_REG);
672 if (enable) {
673 if (val & SUNXI_DEBE_MODCTL_START_CTL) {
674 /* already enabled */
675 return 0;
676 }
677 } else {
678 if ((val & SUNXI_DEBE_MODCTL_START_CTL) == 0) {
679 /* already disabled */
680 return 0;
681 }
682 }
683 return fdt_endpoint_enable(sc->sc_out_ep, enable);
684 case WSDISPLAYIO_GVIDEO:
685 val = DEBE_READ(sc, SUNXI_DEBE_MODCTL_REG);
686 *(int *)data = !!(val & SUNXI_DEBE_MODCTL_LAY0_EN);
687 return 0;
688 case WSDISPLAYIO_GCURPOS:
689 cp = data;
690 cp->x = sc->sc_cursor_x;
691 cp->y = sc->sc_cursor_y;
692 return 0;
693 case WSDISPLAYIO_SCURPOS:
694 cp = data;
695 return sunxi_debe_set_curpos(sc, cp->x, cp->y);
696 case WSDISPLAYIO_GCURMAX:
697 cp = data;
698 cp->x = SUNXI_DEBE_CURMAX;
699 cp->y = SUNXI_DEBE_CURMAX;
700 return 0;
701 case WSDISPLAYIO_SCURSOR:
702 return sunxi_debe_set_cursor(sc, data);
703 }
704
705 return EPASSTHROUGH;
706 }
707
708 /* genfb attachement */
709
710 struct sunxi_befb_softc {
711 struct genfb_softc sc_gen;
712 device_t sc_debedev;
713
714 bus_dma_tag_t sc_dmat;
715 bus_dma_segment_t *sc_dmasegs;
716 int sc_ndmasegs;
717 };
718
719 static device_t sunxi_befb_consoledev = NULL;
720
721 static int sunxi_befb_match(device_t, cfdata_t, void *);
722 static void sunxi_befb_attach(device_t, device_t, void *);
723
724 static int sunxi_befb_ioctl(void *, void *, u_long, void *, int, lwp_t *);
725 static paddr_t sunxi_befb_mmap(void *, void *, off_t, int);
726 static bool sunxi_befb_shutdown(device_t, int);
727
728 CFATTACH_DECL_NEW(sunxi_befb, sizeof(struct sunxi_befb_softc),
729 sunxi_befb_match, sunxi_befb_attach, NULL, NULL);
730
731 static int
732 sunxi_befb_match(device_t parent, cfdata_t cf, void *aux)
733 {
734 return 1;
735 }
736
737 static void
738 sunxi_befb_attach(device_t parent, device_t self, void *aux)
739 {
740 struct sunxi_befb_softc *sc = device_private(self);
741 struct sunxifb_attach_args * const afb = aux;
742 prop_dictionary_t cfg = device_properties(self);
743 struct genfb_ops ops;
744
745 sc->sc_gen.sc_dev = self;
746 sc->sc_debedev = parent;
747 sc->sc_dmat = afb->afb_dmat;
748 sc->sc_dmasegs = afb->afb_dmasegs;
749 sc->sc_ndmasegs = afb->afb_ndmasegs;
750
751 prop_dictionary_set_uint32(cfg, "width", afb->afb_width);
752 prop_dictionary_set_uint32(cfg, "height", afb->afb_height);
753 prop_dictionary_set_uint8(cfg, "depth", 32);
754 prop_dictionary_set_uint16(cfg, "linebytes", afb->afb_width * 4);
755 prop_dictionary_set_uint32(cfg, "address", 0);
756 prop_dictionary_set_uint32(cfg, "virtual_address",
757 (uintptr_t)afb->afb_fb);
758
759 genfb_init(&sc->sc_gen);
760
761 if (sc->sc_gen.sc_width == 0 || sc->sc_gen.sc_fbsize == 0) {
762 aprint_normal(": disabled\n");
763 return;
764 }
765
766 pmf_device_register1(self, NULL, NULL, sunxi_befb_shutdown);
767
768 memset(&ops, 0, sizeof(ops));
769 ops.genfb_ioctl = sunxi_befb_ioctl;
770 ops.genfb_mmap = sunxi_befb_mmap;
771
772 aprint_naive("\n");
773
774 bool is_console = (debe_console_sc == device_private(parent));
775 if (is_console)
776 aprint_normal(": switching to framebuffer console\n");
777 else
778 aprint_normal("\n");
779
780 #ifdef WSDISPLAY_MULTICONS
781 /*
782 * if we support multicons, only the first framebuffer is console,
783 * unless we already know which framebuffer will be the console
784 */
785 if (!is_console && debe_console_sc == NULL &&
786 sunxi_befb_consoledev == NULL)
787 is_console = true;
788 #endif
789 prop_dictionary_set_bool(cfg, "is_console", is_console);
790 if (is_console) {
791 KASSERT(sunxi_befb_consoledev == NULL);
792 sunxi_befb_consoledev = self;
793 }
794
795 genfb_attach(&sc->sc_gen, &ops);
796 }
797
798 static int
799 sunxi_befb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
800 {
801 struct sunxi_befb_softc *sc = v;
802 struct wsdisplayio_bus_id *busid;
803 struct wsdisplayio_fbinfo *fbi;
804 struct rasops_info *ri;
805 int error;
806
807 switch (cmd) {
808 case WSDISPLAYIO_GTYPE:
809 *(u_int *)data = WSDISPLAY_TYPE_ALLWINNER;
810 return 0;
811 case WSDISPLAYIO_GET_BUSID:
812 busid = data;
813 busid->bus_type = WSDISPLAYIO_BUS_SOC;
814 return 0;
815 case WSDISPLAYIO_GET_FBINFO:
816 fbi = data;
817 ri = &sc->sc_gen.vd.active->scr_ri;
818 error = wsdisplayio_get_fbinfo(ri, fbi);
819 if (error == 0) {
820 fbi->fbi_flags |= WSFB_VRAM_IS_RAM;
821 fbi->fbi_fbsize = sc->sc_dmasegs[0].ds_len;
822 }
823 return error;
824 case WSDISPLAYIO_SVIDEO:
825 case WSDISPLAYIO_GVIDEO:
826 case WSDISPLAYIO_GCURPOS:
827 case WSDISPLAYIO_SCURPOS:
828 case WSDISPLAYIO_GCURMAX:
829 case WSDISPLAYIO_SCURSOR:
830 return sunxi_debe_ioctl(sc->sc_debedev, cmd, data);
831 default:
832 return EPASSTHROUGH;
833 }
834 }
835
836 static paddr_t
837 sunxi_befb_mmap(void *v, void *vs, off_t off, int prot)
838 {
839 struct sunxi_befb_softc *sc = v;
840
841 if (off < 0 || off >= sc->sc_dmasegs[0].ds_len)
842 return -1;
843
844 return bus_dmamem_mmap(sc->sc_dmat, sc->sc_dmasegs, sc->sc_ndmasegs,
845 off, prot, BUS_DMA_PREFETCHABLE);
846 }
847
848 static bool
849 sunxi_befb_shutdown(device_t self, int flags)
850 {
851 genfb_enable_polling(self);
852 return true;
853 }
854
855 static void
856 sunxi_befb_set_videomode(device_t dev, u_int width, u_int height)
857 {
858 struct sunxi_befb_softc *sc = device_private(dev);
859
860 if (sc->sc_gen.sc_width != width || sc->sc_gen.sc_height != height) {
861 device_printf(sc->sc_gen.sc_dev,
862 "mode switching not yet supported\n");
863 }
864 }
865
866 int
867 sunxi_debe_pipeline(int phandle, bool active)
868 {
869 device_t dev;
870 struct sunxi_debe_softc *sc;
871 struct fdt_endpoint *ep;
872 int i, error;
873 static bool reset_done = false;
874
875 if (!active)
876 return EOPNOTSUPP;
877
878 for (i = 0;;i++) {
879 dev = device_find_by_driver_unit("sunxidebe", i);
880 if (dev == NULL)
881 return ENODEV;
882 sc = device_private(dev);
883 if (sc->sc_phandle == phandle)
884 break;
885 }
886 if (!reset_done) {
887 sunxi_debe_doreset();
888 sunxi_tcon_doreset();
889 sunxi_hdmi_doreset();
890 reset_done = true;
891 }
892
893 aprint_normal("activate %s\n", device_xname(dev));
894 if (clk_enable(sc->sc_clk_ahb) != 0 ||
895 clk_enable(sc->sc_clk_mod) != 0) {
896 aprint_error_dev(dev, "couldn't enable clocks\n");
897 return EIO;
898 }
899 /* connect debd0 to tcon0, debe1 to tcon1 */
900 ep = fdt_endpoint_get_from_index(&sc->sc_ports, SUNXI_PORT_OUTPUT,
901 sc->sc_unit);
902 if (ep == NULL) {
903 aprint_error_dev(dev, "no output endpoint for %d\n",
904 sc->sc_unit);
905 return ENODEV;
906 }
907 error = fdt_endpoint_activate(ep, true);
908 if (error)
909 return error;
910
911 sc->sc_out_ep = ep;
912 error = fdt_endpoint_enable(ep, true);
913 return error;
914 }
915
916 /*
917 * we don't want to take over console at this time - simplefb will
918 * do a better job than us. We will take over later.
919 * But we want to record the /chose/framebuffer phandle if there is one
920 */
921
922 static const struct device_compatible_entry simplefb_compat_data[] = {
923 { .compat = "allwinner,simple-framebuffer" },
924 DEVICE_COMPAT_EOL
925 };
926
927 static int
928 sunxidebe_console_match(int phandle)
929 {
930 if (of_compatible_match(phandle, simplefb_compat_data)) {
931 sunxi_simplefb_phandle = phandle;
932 }
933 return 0;
934 }
935
936 static void
937 sunxidebe_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
938 {
939 panic("sunxidebe_console_consinit");
940 }
941
942 static const struct fdt_console sunxidebe_fdt_console = {
943 .match = sunxidebe_console_match,
944 .consinit = sunxidebe_console_consinit
945 };
946
947 FDT_CONSOLE(sunxidebe, &sunxidebe_fdt_console);
948
949 #if defined(SUNXI_DEBE_DEBUG)
950 void
951 sunxi_debe_dump_regs(int u)
952 {
953 static const struct {
954 const char *name;
955 uint16_t reg;
956 } regs[] = {
957 { "SUNXI_DEBE_MODCTL_REG", SUNXI_DEBE_MODCTL_REG},
958 { "SUNXI_DEBE_BACKCOLOR_REG", SUNXI_DEBE_BACKCOLOR_REG},
959 { "SUNXI_DEBE_DISSIZE_REG", SUNXI_DEBE_DISSIZE_REG},
960 { "SUNXI_DEBE_LAYSIZE_REG", SUNXI_DEBE_LAYSIZE_REG},
961 { "SUNXI_DEBE_LAYCOOR_REG", SUNXI_DEBE_LAYCOOR_REG},
962 { "SUNXI_DEBE_LAYLINEWIDTH_REG", SUNXI_DEBE_LAYLINEWIDTH_REG},
963 { "SUNXI_DEBE_LAYFB_L32ADD_REG", SUNXI_DEBE_LAYFB_L32ADD_REG},
964 { "SUNXI_DEBE_LAYFB_H4ADD_REG", SUNXI_DEBE_LAYFB_H4ADD_REG},
965 { "SUNXI_DEBE_REGBUFFCTL_REG", SUNXI_DEBE_REGBUFFCTL_REG},
966 { "SUNXI_DEBE_CKMAX_REG", SUNXI_DEBE_CKMAX_REG},
967 { "SUNXI_DEBE_CKMIN_REG", SUNXI_DEBE_CKMIN_REG},
968 { "SUNXI_DEBE_CKCFG_REG", SUNXI_DEBE_CKCFG_REG},
969 { "SUNXI_DEBE_ATTCTL0_REG", SUNXI_DEBE_ATTCTL0_REG},
970 { "SUNXI_DEBE_ATTCTL1_REG", SUNXI_DEBE_ATTCTL1_REG},
971 { "SUNXI_DEBE_HWCCTL_REG", SUNXI_DEBE_HWCCTL_REG},
972 { "SUNXI_DEBE_HWCFBCTL_REG", SUNXI_DEBE_HWCFBCTL_REG},
973 { "SUNXI_DEBE_WBCTL_REG", SUNXI_DEBE_WBCTL_REG},
974 { "SUNXI_DEBE_WBADD_REG", SUNXI_DEBE_WBADD_REG},
975 { "SUNXI_DEBE_WBLINEWIDTH_REG", SUNXI_DEBE_WBLINEWIDTH_REG},
976 { "SUNXI_DEBE_IYUVCTL_REG", SUNXI_DEBE_IYUVCTL_REG},
977 { "SUNXI_DEBE_IYUVADD_REG", SUNXI_DEBE_IYUVADD_REG},
978 { "SUNXI_DEBE_IYUVLINEWIDTH_REG", SUNXI_DEBE_IYUVLINEWIDTH_REG},
979 { "SUNXI_DEBE_YGCOEF_REG", SUNXI_DEBE_YGCOEF_REG},
980 { "SUNXI_DEBE_YGCONS_REG", SUNXI_DEBE_YGCONS_REG},
981 { "SUNXI_DEBE_URCOEF_REG", SUNXI_DEBE_URCOEF_REG},
982 { "SUNXI_DEBE_URCONS_REG", SUNXI_DEBE_URCONS_REG},
983 { "SUNXI_DEBE_VBCOEF_REG", SUNXI_DEBE_VBCOEF_REG},
984 { "SUNXI_DEBE_VBCONS_REG", SUNXI_DEBE_VBCONS_REG},
985 { "SUNXI_DEBE_OCCTL_REG", SUNXI_DEBE_OCCTL_REG},
986 { "SUNXI_DEBE_OCRCOEF_REG", SUNXI_DEBE_OCRCOEF_REG},
987 { "SUNXI_DEBE_OCRCONS_REG", SUNXI_DEBE_OCRCONS_REG},
988 { "SUNXI_DEBE_OCGCOEF_REG", SUNXI_DEBE_OCGCOEF_REG},
989 { "SUNXI_DEBE_OCGCONS_REG", SUNXI_DEBE_OCGCONS_REG},
990 { "SUNXI_DEBE_OCBCOEF_REG", SUNXI_DEBE_OCBCOEF_REG},
991 { "SUNXI_DEBE_OCBCONS_REG", SUNXI_DEBE_OCBCONS_REG},
992 };
993 struct sunxi_debe_softc *sc;
994 device_t dev;
995
996 dev = device_find_by_driver_unit("sunxidebe", u);
997 if (dev == NULL)
998 return;
999 sc = device_private(dev);
1000
1001 for (int i = 0; i < __arraycount(regs); i++) {
1002 printf("%s: 0x%08x\n", regs[i].name,
1003 DEBE_READ(sc, regs[i].reg));
1004 }
1005 }
1006 #endif
1007