plfb_fdt.c revision 1.3 1 1.3 jmcneill /* $NetBSD: plfb_fdt.c,v 1.3 2019/07/06 15:53:38 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill /*
30 1.1 jmcneill * ARM PrimeCell PL111 framebuffer console driver
31 1.1 jmcneill */
32 1.1 jmcneill
33 1.1 jmcneill #include <sys/cdefs.h>
34 1.3 jmcneill __KERNEL_RCSID(0, "$NetBSD: plfb_fdt.c,v 1.3 2019/07/06 15:53:38 jmcneill Exp $");
35 1.1 jmcneill
36 1.1 jmcneill #include <sys/param.h>
37 1.1 jmcneill #include <sys/types.h>
38 1.1 jmcneill #include <sys/systm.h>
39 1.1 jmcneill #include <sys/device.h>
40 1.1 jmcneill #include <sys/conf.h>
41 1.1 jmcneill #include <sys/bus.h>
42 1.1 jmcneill #include <sys/kmem.h>
43 1.1 jmcneill #include <sys/sysctl.h>
44 1.1 jmcneill
45 1.1 jmcneill #include <dev/wsfb/genfbvar.h>
46 1.1 jmcneill
47 1.1 jmcneill #include <dev/fdt/fdtvar.h>
48 1.1 jmcneill #include <dev/fdt/display_timing.h>
49 1.1 jmcneill
50 1.1 jmcneill #define LCDTIMING0 0x000
51 1.1 jmcneill #define LCDTIMING0_HBP __BITS(31,24)
52 1.1 jmcneill #define LCDTIMING0_HFP __BITS(23,16)
53 1.1 jmcneill #define LCDTIMING0_HSW __BITS(15,8)
54 1.1 jmcneill #define LCDTIMING0_PPL __BITS(7,2)
55 1.1 jmcneill #define LCDTIMING1 0x004
56 1.1 jmcneill #define LCDTIMING1_VBP __BITS(31,24)
57 1.1 jmcneill #define LCDTIMING1_VFP __BITS(23,16)
58 1.1 jmcneill #define LCDTIMING1_VSW __BITS(15,10)
59 1.1 jmcneill #define LCDTIMING1_LPP __BITS(9,0)
60 1.1 jmcneill #define LCDUPBASE 0x010
61 1.1 jmcneill #define LCDLPBASE 0x014
62 1.1 jmcneill #define LCDCONTROL 0x018
63 1.1 jmcneill #define LCDCONTROL_PWR __BIT(11)
64 1.1 jmcneill #define LCDCONTROL_BGR __BIT(8)
65 1.1 jmcneill #define LCDCONTROL_BPP __BITS(3,1)
66 1.1 jmcneill #define LCDCONTROL_BPP_24 __SHIFTIN(5, LCDCONTROL_BPP)
67 1.1 jmcneill #define LCDCONTROL_EN __BIT(0)
68 1.1 jmcneill
69 1.1 jmcneill #define PLFB_BPP 32
70 1.1 jmcneill
71 1.2 jmcneill static int plfb_console_phandle = -1;
72 1.2 jmcneill
73 1.1 jmcneill struct plfb_softc {
74 1.1 jmcneill struct genfb_softc sc_gen;
75 1.1 jmcneill bus_space_tag_t sc_bst;
76 1.1 jmcneill bus_space_handle_t sc_bsh;
77 1.1 jmcneill int sc_phandle;
78 1.1 jmcneill
79 1.1 jmcneill bus_space_handle_t sc_vram_bsh;
80 1.1 jmcneill bus_addr_t sc_vram_addr;
81 1.1 jmcneill bus_size_t sc_vram_size;
82 1.1 jmcneill uintptr_t sc_vram;
83 1.1 jmcneill
84 1.1 jmcneill uint32_t sc_wstype;
85 1.1 jmcneill };
86 1.1 jmcneill
87 1.1 jmcneill static int plfb_match(device_t, cfdata_t, void *);
88 1.1 jmcneill static void plfb_attach(device_t, device_t, void *);
89 1.1 jmcneill
90 1.1 jmcneill static int plfb_ioctl(void *, void *, u_long, void *, int, lwp_t *);
91 1.1 jmcneill static paddr_t plfb_mmap(void *, void *, off_t, int);
92 1.1 jmcneill static bool plfb_shutdown(device_t, int);
93 1.1 jmcneill
94 1.1 jmcneill static void plfb_init(struct plfb_softc *);
95 1.1 jmcneill
96 1.1 jmcneill static const char * const compatible[] = {
97 1.1 jmcneill "arm,pl111",
98 1.1 jmcneill NULL
99 1.1 jmcneill };
100 1.1 jmcneill
101 1.1 jmcneill CFATTACH_DECL_NEW(plfb_fdt, sizeof(struct plfb_softc),
102 1.1 jmcneill plfb_match, plfb_attach, NULL, NULL);
103 1.1 jmcneill
104 1.1 jmcneill #define FB_READ(sc, reg) \
105 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
106 1.1 jmcneill #define FB_WRITE(sc, reg, val) \
107 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
108 1.1 jmcneill
109 1.1 jmcneill static int
110 1.1 jmcneill plfb_match(device_t parent, cfdata_t match, void *aux)
111 1.1 jmcneill {
112 1.1 jmcneill struct fdt_attach_args * const faa = aux;
113 1.1 jmcneill
114 1.1 jmcneill return of_match_compatible(faa->faa_phandle, compatible);
115 1.1 jmcneill }
116 1.1 jmcneill
117 1.1 jmcneill static void
118 1.1 jmcneill plfb_attach(device_t parent, device_t self, void *aux)
119 1.1 jmcneill {
120 1.1 jmcneill struct plfb_softc *sc = device_private(self);
121 1.1 jmcneill prop_dictionary_t dict = device_properties(self);
122 1.1 jmcneill struct fdt_attach_args * const faa = aux;
123 1.1 jmcneill const int phandle = faa->faa_phandle;
124 1.1 jmcneill struct genfb_ops ops;
125 1.1 jmcneill struct clk *clk;
126 1.1 jmcneill bus_addr_t addr;
127 1.1 jmcneill bus_size_t size;
128 1.1 jmcneill
129 1.1 jmcneill sc->sc_gen.sc_dev = self;
130 1.1 jmcneill sc->sc_phandle = phandle;
131 1.1 jmcneill sc->sc_bst = faa->faa_bst;
132 1.1 jmcneill
133 1.1 jmcneill /* Enable clocks */
134 1.1 jmcneill for (int i = 0; (clk = fdtbus_clock_get_index(phandle, i)); i++)
135 1.1 jmcneill if (clk_enable(clk) != 0) {
136 1.1 jmcneill aprint_error(": couldn't enable clock #%d\n", i);
137 1.1 jmcneill return;
138 1.1 jmcneill }
139 1.1 jmcneill
140 1.1 jmcneill /* Map CLCD registers */
141 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
142 1.1 jmcneill aprint_error(": missing 'reg' property\n");
143 1.1 jmcneill return;
144 1.1 jmcneill }
145 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh)) {
146 1.1 jmcneill aprint_error(": couldn't map device\n");
147 1.1 jmcneill return;
148 1.1 jmcneill }
149 1.1 jmcneill
150 1.1 jmcneill /* Map VRAM */
151 1.1 jmcneill const int vram_phandle = fdtbus_get_phandle(phandle, "memory-region");
152 1.1 jmcneill if (vram_phandle == -1) {
153 1.1 jmcneill /*
154 1.1 jmcneill * The 'memory-region' property is optional. If
155 1.1 jmcneill * absent, we can allocate FB from main RAM. (TODO)
156 1.1 jmcneill */
157 1.1 jmcneill aprint_error(": missing 'memory-region' property\n");
158 1.1 jmcneill return;
159 1.1 jmcneill }
160 1.1 jmcneill if (fdtbus_get_reg(vram_phandle, 0, &sc->sc_vram_addr,
161 1.1 jmcneill &sc->sc_vram_size) != 0) {
162 1.1 jmcneill aprint_error(": missing 'reg' property on memory-region\n");
163 1.1 jmcneill return;
164 1.1 jmcneill }
165 1.1 jmcneill if (bus_space_map(sc->sc_bst, sc->sc_vram_addr, sc->sc_vram_size,
166 1.1 jmcneill BUS_SPACE_MAP_LINEAR, &sc->sc_vram_bsh)) {
167 1.1 jmcneill aprint_error(": couldn't map vram\n");
168 1.1 jmcneill return;
169 1.1 jmcneill }
170 1.1 jmcneill sc->sc_vram = (uintptr_t)bus_space_vaddr(sc->sc_bst, sc->sc_vram_bsh);
171 1.1 jmcneill
172 1.1 jmcneill plfb_init(sc);
173 1.1 jmcneill
174 1.1 jmcneill sc->sc_wstype = WSDISPLAY_TYPE_PLFB;
175 1.2 jmcneill prop_dictionary_set_bool(dict, "is_console",
176 1.2 jmcneill phandle == plfb_console_phandle);
177 1.1 jmcneill
178 1.1 jmcneill genfb_init(&sc->sc_gen);
179 1.1 jmcneill
180 1.1 jmcneill if (sc->sc_gen.sc_width == 0 ||
181 1.1 jmcneill sc->sc_gen.sc_fbsize == 0) {
182 1.1 jmcneill aprint_normal(": disabled\n");
183 1.1 jmcneill return;
184 1.1 jmcneill }
185 1.1 jmcneill
186 1.1 jmcneill pmf_device_register1(self, NULL, NULL, plfb_shutdown);
187 1.1 jmcneill
188 1.1 jmcneill memset(&ops, 0, sizeof(ops));
189 1.1 jmcneill ops.genfb_ioctl = plfb_ioctl;
190 1.1 jmcneill ops.genfb_mmap = plfb_mmap;
191 1.1 jmcneill
192 1.1 jmcneill aprint_naive("\n");
193 1.1 jmcneill aprint_normal("\n");
194 1.1 jmcneill
195 1.1 jmcneill genfb_attach(&sc->sc_gen, &ops);
196 1.1 jmcneill }
197 1.1 jmcneill
198 1.1 jmcneill static int
199 1.1 jmcneill plfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
200 1.1 jmcneill {
201 1.1 jmcneill struct plfb_softc *sc = v;
202 1.1 jmcneill struct wsdisplayio_bus_id *busid;
203 1.1 jmcneill
204 1.1 jmcneill switch (cmd) {
205 1.1 jmcneill case WSDISPLAYIO_GTYPE:
206 1.1 jmcneill *(u_int *)data = sc->sc_wstype;
207 1.1 jmcneill return 0;
208 1.1 jmcneill case WSDISPLAYIO_GET_BUSID:
209 1.1 jmcneill busid = data;
210 1.1 jmcneill busid->bus_type = WSDISPLAYIO_BUS_SOC;
211 1.1 jmcneill return 0;
212 1.1 jmcneill case WSDISPLAYIO_GET_FBINFO:
213 1.1 jmcneill {
214 1.1 jmcneill struct wsdisplayio_fbinfo *fbi = data;
215 1.1 jmcneill struct rasops_info *ri = &sc->sc_gen.vd.active->scr_ri;
216 1.1 jmcneill
217 1.1 jmcneill return wsdisplayio_get_fbinfo(ri, fbi);
218 1.1 jmcneill }
219 1.1 jmcneill default:
220 1.1 jmcneill return EPASSTHROUGH;
221 1.1 jmcneill }
222 1.1 jmcneill }
223 1.1 jmcneill
224 1.1 jmcneill static paddr_t
225 1.1 jmcneill plfb_mmap(void *v, void *vs, off_t offset, int prot)
226 1.1 jmcneill {
227 1.1 jmcneill struct plfb_softc *sc = v;
228 1.1 jmcneill
229 1.1 jmcneill if (offset < 0 || offset >= sc->sc_vram_size)
230 1.1 jmcneill return -1;
231 1.1 jmcneill
232 1.1 jmcneill return bus_space_mmap(sc->sc_bst, sc->sc_vram_addr, offset, prot,
233 1.1 jmcneill BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
234 1.1 jmcneill }
235 1.1 jmcneill
236 1.1 jmcneill static bool
237 1.1 jmcneill plfb_shutdown(device_t self, int flags)
238 1.1 jmcneill {
239 1.1 jmcneill genfb_enable_polling(self);
240 1.1 jmcneill return true;
241 1.1 jmcneill }
242 1.1 jmcneill
243 1.1 jmcneill static int
244 1.1 jmcneill plfb_get_panel_timing(struct plfb_softc *sc, struct display_timing *timing)
245 1.1 jmcneill {
246 1.1 jmcneill int panel, panel_timing;
247 1.1 jmcneill
248 1.1 jmcneill panel = of_find_firstchild_byname(sc->sc_phandle, "panel");
249 1.1 jmcneill if (panel <= 0)
250 1.1 jmcneill return ENOENT;
251 1.1 jmcneill panel_timing = of_find_firstchild_byname(panel, "panel-timing");
252 1.1 jmcneill if (panel_timing <= 0)
253 1.1 jmcneill return ENOENT;
254 1.1 jmcneill
255 1.1 jmcneill return display_timing_parse(panel_timing, timing);
256 1.1 jmcneill }
257 1.1 jmcneill
258 1.1 jmcneill static void
259 1.1 jmcneill plfb_init(struct plfb_softc *sc)
260 1.1 jmcneill {
261 1.1 jmcneill prop_dictionary_t dict = device_properties(sc->sc_gen.sc_dev);
262 1.1 jmcneill struct display_timing timing;
263 1.1 jmcneill
264 1.1 jmcneill if (plfb_get_panel_timing(sc, &timing) != 0) {
265 1.3 jmcneill /* No timings specified in DT, assume 800x600 */
266 1.3 jmcneill timing.hactive = 800;
267 1.3 jmcneill timing.hback_porch = 128;
268 1.3 jmcneill timing.hfront_porch = 24;
269 1.3 jmcneill timing.hsync_len = 72;
270 1.3 jmcneill timing.vactive = 600;
271 1.3 jmcneill timing.vback_porch = 22;
272 1.3 jmcneill timing.vfront_porch = 1;
273 1.3 jmcneill timing.vsync_len = 2;
274 1.1 jmcneill }
275 1.1 jmcneill
276 1.1 jmcneill prop_dictionary_set_uint32(dict, "width", timing.hactive);
277 1.1 jmcneill prop_dictionary_set_uint32(dict, "height", timing.vactive);
278 1.1 jmcneill prop_dictionary_set_uint8(dict, "depth", PLFB_BPP);
279 1.1 jmcneill prop_dictionary_set_bool(dict, "dblscan", 0);
280 1.1 jmcneill prop_dictionary_set_bool(dict, "interlace", 0);
281 1.1 jmcneill prop_dictionary_set_uint16(dict, "linebytes", timing.hactive * (PLFB_BPP / 8));
282 1.1 jmcneill prop_dictionary_set_uint32(dict, "address", sc->sc_vram_addr);
283 1.1 jmcneill prop_dictionary_set_uint32(dict, "virtual_address", sc->sc_vram);
284 1.1 jmcneill
285 1.1 jmcneill /* FB base address */
286 1.1 jmcneill FB_WRITE(sc, LCDUPBASE, sc->sc_vram_addr);
287 1.1 jmcneill FB_WRITE(sc, LCDLPBASE, 0);
288 1.1 jmcneill
289 1.1 jmcneill /* CRTC timings */
290 1.1 jmcneill FB_WRITE(sc, LCDTIMING0,
291 1.1 jmcneill __SHIFTIN(timing.hback_porch - 1, LCDTIMING0_HBP) |
292 1.1 jmcneill __SHIFTIN(timing.hfront_porch - 1, LCDTIMING0_HFP) |
293 1.1 jmcneill __SHIFTIN(timing.hsync_len - 1, LCDTIMING0_HSW) |
294 1.1 jmcneill __SHIFTIN((timing.hactive / 16) - 1, LCDTIMING0_PPL));
295 1.1 jmcneill FB_WRITE(sc, LCDTIMING1,
296 1.1 jmcneill __SHIFTIN(timing.vback_porch - 1, LCDTIMING1_VBP) |
297 1.1 jmcneill __SHIFTIN(timing.vfront_porch - 1, LCDTIMING1_VFP) |
298 1.1 jmcneill __SHIFTIN(timing.vsync_len - 1, LCDTIMING1_VSW) |
299 1.1 jmcneill __SHIFTIN(timing.vactive - 1, LCDTIMING1_LPP));
300 1.1 jmcneill
301 1.1 jmcneill /* Configure and enable CLCD */
302 1.1 jmcneill FB_WRITE(sc, LCDCONTROL,
303 1.1 jmcneill LCDCONTROL_PWR | LCDCONTROL_EN | LCDCONTROL_BPP_24 |
304 1.1 jmcneill LCDCONTROL_BGR);
305 1.1 jmcneill }
306 1.2 jmcneill
307 1.2 jmcneill static int
308 1.2 jmcneill plfb_console_match(int phandle)
309 1.2 jmcneill {
310 1.2 jmcneill return of_match_compatible(phandle, compatible);
311 1.2 jmcneill }
312 1.2 jmcneill
313 1.2 jmcneill static void
314 1.2 jmcneill plfb_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
315 1.2 jmcneill {
316 1.2 jmcneill plfb_console_phandle = faa->faa_phandle;
317 1.2 jmcneill genfb_cnattach();
318 1.2 jmcneill }
319 1.2 jmcneill
320 1.2 jmcneill static const struct fdt_console plfb_fdt_console = {
321 1.2 jmcneill .match = plfb_console_match,
322 1.2 jmcneill .consinit = plfb_console_consinit
323 1.2 jmcneill };
324 1.2 jmcneill
325 1.2 jmcneill FDT_CONSOLE(plfb, &plfb_fdt_console);
326