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