1 /* $NetBSD: sunxi_dep.c,v 1.7 2021/01/27 03:10:20 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Manuel Bouyer.
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32
33 #include <sys/cdefs.h>
34
35 __KERNEL_RCSID(1, "$NetBSD: sunxi_dep.c,v 1.7 2021/01/27 03:10:20 thorpej Exp $");
36
37 #include <sys/param.h>
38 #include <sys/bus.h>
39 #include <sys/device.h>
40 #include <sys/systm.h>
41
42 #include <libfdt.h>
43
44 #include <dev/fdt/fdtvar.h>
45 #include <arm/sunxi/sunxi_display.h>
46
47 #include "sunxi_debe.h"
48
49 struct sunxi_dep_softc {
50 device_t sc_dev;
51 int sc_phandle;
52 };
53
54 static const struct device_compatible_entry compat_data[] = {
55 { .compat = "allwinner,sun4i-a10-display-engine" },
56 { .compat = "allwinner,sun7i-a20-display-engine" },
57 DEVICE_COMPAT_EOL
58 };
59
60 static const char *fb_compat[] = {
61 "allwinner,simple-framebuffer",
62 NULL
63 };
64
65 static int sunxi_dep_match(device_t, cfdata_t, void *);
66 static void sunxi_dep_attach(device_t, device_t, void *);
67
68 CFATTACH_DECL_NEW(sunxi_dep, sizeof(struct sunxi_dep_softc),
69 sunxi_dep_match, sunxi_dep_attach, NULL, NULL);
70
71 static int
sunxi_dep_match(device_t parent,cfdata_t cf,void * aux)72 sunxi_dep_match(device_t parent, cfdata_t cf, void *aux)
73 {
74 #if NSUNXI_DEBE > 0
75 struct fdt_attach_args * const faa = aux;
76
77 return of_compatible_match(faa->faa_phandle, compat_data);
78 #else
79 return 0;
80 #endif
81 }
82
83 static void
sunxi_dep_attach(device_t parent,device_t self,void * aux)84 sunxi_dep_attach(device_t parent, device_t self, void *aux)
85 {
86 struct sunxi_dep_softc * const sc = device_private(self);
87 struct fdt_attach_args * const faa = aux;
88 const int phandle = faa->faa_phandle;
89 int sunxi_dep_npipelines = 0;
90 int len;
91 const u_int *buf;
92 u_int ref;
93 int error;
94
95 sc->sc_dev = self;
96
97 buf = fdt_getprop(fdtbus_get_data(),
98 fdtbus_phandle2offset(phandle), "allwinner,pipelines", &len);
99 if (buf == NULL || len < sizeof(ref) || (len % sizeof(ref)) != 0) {
100 aprint_error("bad/missing allwinner,pipelines property\n");
101 return;
102 }
103 aprint_normal(": ");
104 #if NSUNXI_DEBE > 0
105 for (int i = 0; i < (len / sizeof(ref)); i++) {
106 if (i > 0)
107 aprint_normal_dev(self, "");
108 ref = be32dec(&buf[i]);
109 error = sunxi_debe_pipeline(
110 fdtbus_get_phandle_from_native(ref), true);
111 if (error)
112 aprint_error("can't activate pipeline %d\n", i);
113 else
114 sunxi_dep_npipelines++;
115 }
116 aprint_naive("\n");
117 if (sunxi_dep_npipelines > 0)
118 fdt_remove_bycompat(fb_compat);
119 #else
120 aprint_error("debe not configured\n");
121 return;
122 #endif
123 }
124