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