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