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