sunxi_debe.c revision 1.1 1 1.1 jmcneill /* $NetBSD: sunxi_debe.c,v 1.1 2017/08/26 22:24:50 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_debe.c,v 1.1 2017/08/26 22:24:50 jmcneill Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/bus.h>
34 1.1 jmcneill #include <sys/device.h>
35 1.1 jmcneill #include <sys/systm.h>
36 1.1 jmcneill
37 1.1 jmcneill #include <dev/fdt/fdtvar.h>
38 1.1 jmcneill
39 1.1 jmcneill #include <dev/wsfb/genfbvar.h>
40 1.1 jmcneill
41 1.1 jmcneill #define SUNXI_SDRAM_BASE 0x40000000
42 1.1 jmcneill
43 1.1 jmcneill #define DEBE_MAX_LAYER 4
44 1.1 jmcneill
45 1.1 jmcneill #define DEBE_MODCTL_REG 0x800
46 1.1 jmcneill #define DEBE_MODCTL_LAYn_EN(n) __BIT(8 + (n))
47 1.1 jmcneill #define DEBE_MODCTL_EN __BIT(0)
48 1.1 jmcneill #define DEBE_LAYSIZE_REG(n) (0x810 + (n) * 0x10)
49 1.1 jmcneill #define DEBE_LAYSIZE_HEIGHT __BITS(28,16)
50 1.1 jmcneill #define DEBE_LAYSIZE_WIDTH __BITS(12,0)
51 1.1 jmcneill #define DEBE_LAYCOOR_REG(n) (0x820 + (n) * 0x4)
52 1.1 jmcneill #define DEBE_LAYLINEWIDTH_REG(n) (0x840 + (n) * 0x4)
53 1.1 jmcneill #define DEBE_L32ADD_REG(n) (0x850 + (n) * 0x4)
54 1.1 jmcneill #define DEBE_H4ADD_REG 0x860
55 1.1 jmcneill #define DEBE_H4ADD(n) __BITS((n) * 8 + 3, (n) * 8)
56 1.1 jmcneill
57 1.1 jmcneill static const char * const compatible[] = {
58 1.1 jmcneill "allwinner,sun5i-a13-display-backend",
59 1.1 jmcneill NULL
60 1.1 jmcneill };
61 1.1 jmcneill
62 1.1 jmcneill struct sunxi_debe_softc {
63 1.1 jmcneill struct genfb_softc sc_gen;
64 1.1 jmcneill bus_space_tag_t sc_bst;
65 1.1 jmcneill bus_space_handle_t sc_bsh;
66 1.1 jmcneill int sc_phandle;
67 1.1 jmcneill
68 1.1 jmcneill bus_space_handle_t sc_fb_bsh;
69 1.1 jmcneill bus_addr_t sc_fb_paddr;
70 1.1 jmcneill };
71 1.1 jmcneill
72 1.1 jmcneill #define BE_READ(sc, reg) \
73 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
74 1.1 jmcneill #define BE_WRITE(sc, reg, val) \
75 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
76 1.1 jmcneill
77 1.1 jmcneill static bool
78 1.1 jmcneill sunxi_debe_shutdown(device_t self, int flags)
79 1.1 jmcneill {
80 1.1 jmcneill genfb_enable_polling(self);
81 1.1 jmcneill return true;
82 1.1 jmcneill }
83 1.1 jmcneill
84 1.1 jmcneill static int
85 1.1 jmcneill sunxi_debe_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
86 1.1 jmcneill {
87 1.1 jmcneill struct sunxi_debe_softc * const sc = v;
88 1.1 jmcneill struct wsdisplayio_bus_id *busid;
89 1.1 jmcneill struct wsdisplayio_fbinfo *fbi;
90 1.1 jmcneill struct rasops_info *ri;
91 1.1 jmcneill int error;
92 1.1 jmcneill
93 1.1 jmcneill switch (cmd) {
94 1.1 jmcneill case WSDISPLAYIO_GTYPE:
95 1.1 jmcneill *(u_int *)data = WSDISPLAY_TYPE_ALLWINNER;
96 1.1 jmcneill return 0;
97 1.1 jmcneill case WSDISPLAYIO_GET_BUSID:
98 1.1 jmcneill busid = data;
99 1.1 jmcneill busid->bus_type = WSDISPLAYIO_BUS_SOC;
100 1.1 jmcneill return 0;
101 1.1 jmcneill case WSDISPLAYIO_GET_FBINFO:
102 1.1 jmcneill fbi = data;
103 1.1 jmcneill ri = &sc->sc_gen.vd.active->scr_ri;
104 1.1 jmcneill error = wsdisplayio_get_fbinfo(ri, fbi);
105 1.1 jmcneill if (error == 0)
106 1.1 jmcneill fbi->fbi_flags |= WSFB_VRAM_IS_RAM;
107 1.1 jmcneill return error;
108 1.1 jmcneill default:
109 1.1 jmcneill return EPASSTHROUGH;
110 1.1 jmcneill }
111 1.1 jmcneill }
112 1.1 jmcneill
113 1.1 jmcneill static paddr_t
114 1.1 jmcneill sunxi_debe_mmap(void *v, void *vs, off_t off, int prot)
115 1.1 jmcneill {
116 1.1 jmcneill struct sunxi_debe_softc * const sc = v;
117 1.1 jmcneill
118 1.1 jmcneill if (off < 0 || off >= sc->sc_gen.sc_fbsize)
119 1.1 jmcneill return -1;
120 1.1 jmcneill
121 1.1 jmcneill return bus_space_mmap(sc->sc_bst, sc->sc_fb_paddr, off, prot,
122 1.1 jmcneill BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
123 1.1 jmcneill }
124 1.1 jmcneill
125 1.1 jmcneill static void
126 1.1 jmcneill sunxi_debe_attach_layer(struct sunxi_debe_softc *sc, u_int lay)
127 1.1 jmcneill {
128 1.1 jmcneill prop_dictionary_t dict = device_properties(sc->sc_gen.sc_dev);
129 1.1 jmcneill struct genfb_ops ops;
130 1.1 jmcneill
131 1.1 jmcneill const uint32_t size = BE_READ(sc, DEBE_LAYSIZE_REG(lay));
132 1.1 jmcneill const uint32_t linewidth = BE_READ(sc, DEBE_LAYLINEWIDTH_REG(lay));
133 1.1 jmcneill const uint32_t l32add = BE_READ(sc, DEBE_L32ADD_REG(lay));
134 1.1 jmcneill const uint32_t h4add = BE_READ(sc, DEBE_H4ADD_REG);
135 1.1 jmcneill
136 1.1 jmcneill const int width = __SHIFTOUT(size, DEBE_LAYSIZE_WIDTH) + 1;
137 1.1 jmcneill const int height = __SHIFTOUT(size, DEBE_LAYSIZE_HEIGHT) + 1;
138 1.1 jmcneill const int stride = linewidth / NBBY;
139 1.1 jmcneill const uint64_t fbaddr_bits =
140 1.1 jmcneill l32add | (__SHIFTOUT(h4add, DEBE_H4ADD(lay)) << 32);
141 1.1 jmcneill const uint32_t fbaddr = SUNXI_SDRAM_BASE +
142 1.1 jmcneill (uint32_t)(fbaddr_bits / NBBY);
143 1.1 jmcneill const int fbsize = height * stride;
144 1.1 jmcneill
145 1.1 jmcneill aprint_normal_dev(sc->sc_gen.sc_dev,
146 1.1 jmcneill "attaching %dx%d fb to layer %d @ 0x%08x size 0x%x\n",
147 1.1 jmcneill width, height, lay, fbaddr, fbsize);
148 1.1 jmcneill
149 1.1 jmcneill if (bus_space_map(sc->sc_bst, fbaddr, fbsize,
150 1.1 jmcneill BUS_SPACE_MAP_LINEAR, &sc->sc_fb_bsh) != 0) {
151 1.1 jmcneill aprint_error_dev(sc->sc_gen.sc_dev, "failed to map fb\n");
152 1.1 jmcneill return;
153 1.1 jmcneill }
154 1.1 jmcneill
155 1.1 jmcneill sc->sc_fb_paddr = fbaddr;
156 1.1 jmcneill
157 1.1 jmcneill prop_dictionary_set_uint32(dict, "width", width);
158 1.1 jmcneill prop_dictionary_set_uint32(dict, "height", height);
159 1.1 jmcneill prop_dictionary_set_uint8(dict, "depth", 32);
160 1.1 jmcneill prop_dictionary_set_uint16(dict, "linebytes", stride);
161 1.1 jmcneill prop_dictionary_set_uint32(dict, "address", fbaddr);
162 1.1 jmcneill prop_dictionary_set_uint32(dict, "virtual_address",
163 1.1 jmcneill (uintptr_t)bus_space_vaddr(sc->sc_bst, sc->sc_fb_bsh));
164 1.1 jmcneill
165 1.1 jmcneill genfb_init(&sc->sc_gen);
166 1.1 jmcneill
167 1.1 jmcneill if (sc->sc_gen.sc_width == 0 || sc->sc_gen.sc_fbsize == 0) {
168 1.1 jmcneill aprint_normal(": disabled\n");
169 1.1 jmcneill return;
170 1.1 jmcneill }
171 1.1 jmcneill
172 1.1 jmcneill pmf_device_register1(sc->sc_gen.sc_dev, NULL, NULL,
173 1.1 jmcneill sunxi_debe_shutdown);
174 1.1 jmcneill
175 1.1 jmcneill memset(&ops, 0, sizeof(ops));
176 1.1 jmcneill ops.genfb_ioctl = sunxi_debe_ioctl;
177 1.1 jmcneill ops.genfb_mmap = sunxi_debe_mmap;
178 1.1 jmcneill
179 1.1 jmcneill bool is_console = false;
180 1.1 jmcneill prop_dictionary_get_bool(dict, "is_console", &is_console);
181 1.1 jmcneill prop_dictionary_set_bool(dict, "is_console", is_console);
182 1.1 jmcneill
183 1.1 jmcneill if (is_console)
184 1.1 jmcneill aprint_normal_dev(sc->sc_gen.sc_dev,
185 1.1 jmcneill "switching to framebuffer console\n");
186 1.1 jmcneill
187 1.1 jmcneill genfb_attach(&sc->sc_gen, &ops);
188 1.1 jmcneill }
189 1.1 jmcneill
190 1.1 jmcneill static int
191 1.1 jmcneill sunxi_debe_match(device_t parent, cfdata_t cf, void *aux)
192 1.1 jmcneill {
193 1.1 jmcneill struct fdt_attach_args * const faa = aux;
194 1.1 jmcneill
195 1.1 jmcneill return of_match_compatible(faa->faa_phandle, compatible);
196 1.1 jmcneill }
197 1.1 jmcneill
198 1.1 jmcneill static void
199 1.1 jmcneill sunxi_debe_attach(device_t parent, device_t self, void *aux)
200 1.1 jmcneill {
201 1.1 jmcneill struct sunxi_debe_softc * const sc = device_private(self);
202 1.1 jmcneill struct fdt_attach_args * const faa = aux;
203 1.1 jmcneill const int phandle = faa->faa_phandle;
204 1.1 jmcneill struct fdtbus_reset *rst;
205 1.1 jmcneill struct clk *clk;
206 1.1 jmcneill bus_addr_t addr;
207 1.1 jmcneill bus_size_t size;
208 1.1 jmcneill u_int lay;
209 1.1 jmcneill
210 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
211 1.1 jmcneill aprint_error(": couldn't get registers\n");
212 1.1 jmcneill return;
213 1.1 jmcneill }
214 1.1 jmcneill
215 1.1 jmcneill if ((clk = fdtbus_clock_get(phandle, "ahb")) == NULL ||
216 1.1 jmcneill clk_enable(clk) != 0) {
217 1.1 jmcneill aprint_error(": couldn't enable ahb clock\n");
218 1.1 jmcneill return;
219 1.1 jmcneill }
220 1.1 jmcneill if ((rst = fdtbus_reset_get_index(phandle, 0)) == NULL ||
221 1.1 jmcneill fdtbus_reset_deassert(rst) != 0) {
222 1.1 jmcneill aprint_error(": couldn't de-assert reset\n");
223 1.1 jmcneill return;
224 1.1 jmcneill }
225 1.1 jmcneill
226 1.1 jmcneill sc->sc_gen.sc_dev = self;
227 1.1 jmcneill sc->sc_phandle = phandle;
228 1.1 jmcneill sc->sc_bst = faa->faa_bst;
229 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
230 1.1 jmcneill aprint_error(": couldn't map registers\n");
231 1.1 jmcneill return;
232 1.1 jmcneill }
233 1.1 jmcneill
234 1.1 jmcneill aprint_naive("\n");
235 1.1 jmcneill aprint_normal(": Display Backend\n");
236 1.1 jmcneill
237 1.1 jmcneill aprint_normal_dev(self, "boot config:");
238 1.1 jmcneill const uint32_t modctl = BE_READ(sc, DEBE_MODCTL_REG);
239 1.1 jmcneill aprint_normal(" %08x\n", modctl);
240 1.1 jmcneill
241 1.1 jmcneill if ((modctl & DEBE_MODCTL_EN) == 0) {
242 1.1 jmcneill aprint_normal_dev(self, "not enabled by firmware\n");
243 1.1 jmcneill return;
244 1.1 jmcneill }
245 1.1 jmcneill
246 1.1 jmcneill for (lay = 0; lay < DEBE_MAX_LAYER; lay++) {
247 1.1 jmcneill if ((modctl & DEBE_MODCTL_LAYn_EN(lay)) == 0)
248 1.1 jmcneill continue;
249 1.1 jmcneill sunxi_debe_attach_layer(sc, lay);
250 1.1 jmcneill break;
251 1.1 jmcneill }
252 1.1 jmcneill if (lay == DEBE_MAX_LAYER) {
253 1.1 jmcneill aprint_error_dev(self, "no layers enabled\n");
254 1.1 jmcneill return;
255 1.1 jmcneill }
256 1.1 jmcneill }
257 1.1 jmcneill
258 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_debe, sizeof(struct sunxi_debe_softc),
259 1.1 jmcneill sunxi_debe_match, sunxi_debe_attach, NULL, NULL);
260