1 /* $OpenBSD: simplefb.c,v 1.3 2017/12/18 10:13:45 kettenis Exp $ */ 2 /* 3 * Copyright (c) 2016 Mark Kettenis 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/param.h> 19 #include <sys/systm.h> 20 #include <sys/device.h> 21 22 #include <uvm/uvm_extern.h> 23 24 #include <machine/bus.h> 25 #include <machine/fdt.h> 26 27 #include <dev/ofw/openfirm.h> 28 #include <dev/ofw/fdt.h> 29 30 #include <dev/wscons/wsconsio.h> 31 #include <dev/wscons/wsdisplayvar.h> 32 #include <dev/rasops/rasops.h> 33 34 #define SIMPLEFB_WIDTH 160 35 #define SIMPLEFB_HEIGHT 50 36 37 struct simplefb_format { 38 const char *format; 39 int depth; 40 int rpos, rnum; 41 int gpos, gnum; 42 int bpos, bnum; 43 }; 44 45 /* 46 * Supported pixel formats. Layout ommitted when it matches the 47 * rasops defaults. 48 */ 49 struct simplefb_format simplefb_formats[] = { 50 { "r5g6b5", 16 }, 51 { "x1r5g5b5", 15 }, 52 { "a1r5g5b5", 15 }, 53 { "r8g8b8", 24 }, 54 { "x8r8g8b8", 32, 16, 8, 8, 8, 0, 8 }, 55 { "a8r8g8b8", 32, 16, 8, 8, 8, 0, 8 }, 56 { "x8b8g8r8", 32 }, 57 { "a8b8g8r8", 32 } 58 }; 59 60 struct simplefb_softc { 61 struct device sc_dev; 62 bus_space_tag_t sc_iot; 63 bus_space_handle_t sc_ioh; 64 65 struct rasops_info sc_ri; 66 struct wsscreen_descr sc_wsd; 67 struct wsscreen_list sc_wsl; 68 struct wsscreen_descr *sc_scrlist[1]; 69 70 struct simplefb_format *sc_format; 71 paddr_t sc_paddr; 72 psize_t sc_psize; 73 }; 74 75 struct rasops_info simplefb_ri; 76 struct wsscreen_descr simplefb_wsd = { "std" }; 77 struct wsdisplay_charcell simplefb_bs[SIMPLEFB_WIDTH * SIMPLEFB_HEIGHT]; 78 79 int simplefb_match(struct device *, void *, void *); 80 void simplefb_attach(struct device *, struct device *, void *); 81 82 struct cfattach simplefb_ca = { 83 sizeof(struct simplefb_softc), simplefb_match, simplefb_attach 84 }; 85 86 struct cfdriver simplefb_cd = { 87 NULL, "simplefb", DV_DULL 88 }; 89 90 const char *simplefb_init(int, struct rasops_info *); 91 92 int simplefb_wsioctl(void *, u_long, caddr_t, int, struct proc *); 93 paddr_t simplefb_wsmmap(void *, off_t, int); 94 int simplefb_alloc_screen(void *, const struct wsscreen_descr *, 95 void **, int *, int *, long *); 96 97 struct wsdisplay_accessops simplefb_accessops = { 98 .ioctl = simplefb_wsioctl, 99 .mmap = simplefb_wsmmap, 100 .alloc_screen = simplefb_alloc_screen, 101 .free_screen = rasops_free_screen, 102 .show_screen = rasops_show_screen, 103 .getchar = rasops_getchar, 104 .load_font = rasops_load_font, 105 .list_font = rasops_list_font, 106 }; 107 108 int 109 simplefb_match(struct device *parent, void *match, void *aux) 110 { 111 struct fdt_attach_args *faa = aux; 112 113 return OF_is_compatible(faa->fa_node, "simple-framebuffer"); 114 } 115 116 void 117 simplefb_attach(struct device *parent, struct device *self, void *aux) 118 { 119 struct simplefb_softc *sc = (struct simplefb_softc *)self; 120 struct fdt_attach_args *faa = aux; 121 struct rasops_info *ri = &sc->sc_ri; 122 struct wsemuldisplaydev_attach_args waa; 123 const char *format; 124 int console = 0; 125 long defattr; 126 127 if (faa->fa_nreg < 1) 128 return; 129 130 format = simplefb_init(faa->fa_node, ri); 131 if (format) { 132 printf(": unsupported format \"%s\"\n", format); 133 return; 134 } 135 136 if (faa->fa_node == stdout_node) 137 console = 1; 138 139 sc->sc_iot = faa->fa_iot; 140 sc->sc_paddr = faa->fa_reg[0].addr; 141 sc->sc_psize = faa->fa_reg[0].size; 142 if (bus_space_map(sc->sc_iot, sc->sc_paddr, sc->sc_psize, 143 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE, &sc->sc_ioh)) { 144 printf(": can't map framebuffer\n"); 145 return; 146 } 147 148 ri->ri_bits = bus_space_vaddr(sc->sc_iot, sc->sc_ioh); 149 ri->ri_hw = sc; 150 151 if (console) { 152 /* Preserve contents. */ 153 ri->ri_bs = simplefb_bs; 154 ri->ri_flg &= ~RI_CLEAR; 155 } 156 157 printf(": %dx%d\n", ri->ri_width, ri->ri_height); 158 159 ri->ri_flg |= RI_VCONS; 160 rasops_init(ri, SIMPLEFB_HEIGHT, SIMPLEFB_WIDTH); 161 162 strlcpy(sc->sc_wsd.name, "std", sizeof(sc->sc_wsd.name)); 163 sc->sc_wsd.capabilities = ri->ri_caps; 164 sc->sc_wsd.nrows = ri->ri_rows; 165 sc->sc_wsd.ncols = ri->ri_cols; 166 sc->sc_wsd.textops = &ri->ri_ops; 167 sc->sc_wsd.fontwidth = ri->ri_font->fontwidth; 168 sc->sc_wsd.fontheight = ri->ri_font->fontheight; 169 170 sc->sc_scrlist[0] = &sc->sc_wsd; 171 sc->sc_wsl.nscreens = 1; 172 sc->sc_wsl.screens = (const struct wsscreen_descr **)sc->sc_scrlist; 173 174 if (console) { 175 ri->ri_ops.alloc_attr(ri->ri_active, 0, 0, 0, &defattr); 176 wsdisplay_cnattach(&sc->sc_wsd, ri->ri_active, 177 simplefb_ri.ri_ccol, simplefb_ri.ri_crow, defattr); 178 } 179 180 memset(&waa, 0, sizeof(waa)); 181 waa.scrdata = &sc->sc_wsl; 182 waa.accessops = &simplefb_accessops; 183 waa.accesscookie = ri; 184 waa.console = console; 185 186 config_found_sm(self, &waa, wsemuldisplaydevprint, 187 wsemuldisplaydevsubmatch); 188 } 189 190 const char * 191 simplefb_init(int node, struct rasops_info *ri) 192 { 193 struct simplefb_format *fmt = NULL; 194 static char format[16]; 195 int i; 196 197 format[0] = 0; 198 OF_getprop(node, "format", format, sizeof(format)); 199 format[sizeof(format) - 1] = 0; 200 201 for (i = 0; i < nitems(simplefb_formats); i++) { 202 if (strcmp(format, simplefb_formats[i].format) == 0) { 203 fmt = &simplefb_formats[i]; 204 break; 205 } 206 } 207 if (fmt == NULL) 208 return format; 209 210 ri->ri_width = OF_getpropint(node, "width", 0); 211 ri->ri_height = OF_getpropint(node, "height", 0); 212 ri->ri_stride = OF_getpropint(node, "stride", 0); 213 ri->ri_depth = fmt->depth; 214 ri->ri_rpos = fmt->rpos; 215 ri->ri_rnum = fmt->rnum; 216 ri->ri_gpos = fmt->gpos; 217 ri->ri_gnum = fmt->gnum; 218 ri->ri_bpos = fmt->bpos; 219 ri->ri_bnum = fmt->bnum; 220 ri->ri_flg = RI_CENTER | RI_CLEAR | RI_FULLCLEAR | RI_WRONLY; 221 222 return NULL; 223 } 224 225 int 226 simplefb_wsioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p) 227 { 228 struct rasops_info *ri = v; 229 struct wsdisplay_fbinfo *wdf; 230 231 switch (cmd) { 232 case WSDISPLAYIO_GTYPE: 233 *(int *)data = WSDISPLAY_TYPE_EFIFB; 234 return 0; 235 case WSDISPLAYIO_GINFO: 236 wdf = (struct wsdisplay_fbinfo *)data; 237 wdf->width = ri->ri_width; 238 wdf->height = ri->ri_height; 239 wdf->depth = ri->ri_depth; 240 wdf->cmsize = 0; /* color map is unavailable */ 241 break; 242 case WSDISPLAYIO_LINEBYTES: 243 *(u_int *)data = ri->ri_stride; 244 break; 245 case WSDISPLAYIO_SMODE: 246 break; 247 case WSDISPLAYIO_GETSUPPORTEDDEPTH: 248 switch (ri->ri_depth) { 249 case 32: 250 *(u_int *)data = WSDISPLAYIO_DEPTH_24_32; 251 break; 252 case 24: 253 *(u_int *)data = WSDISPLAYIO_DEPTH_24_24; 254 break; 255 case 16: 256 *(u_int *)data = WSDISPLAYIO_DEPTH_16; 257 break; 258 case 15: 259 *(u_int *)data = WSDISPLAYIO_DEPTH_15; 260 break; 261 default: 262 return -1; 263 } 264 break; 265 default: 266 return -1; 267 } 268 269 return 0; 270 } 271 272 paddr_t 273 simplefb_wsmmap(void *v, off_t off, int prot) 274 { 275 struct rasops_info *ri = v; 276 struct simplefb_softc *sc = ri->ri_hw; 277 278 if (off < 0 || off >= sc->sc_psize) 279 return -1; 280 281 return ((sc->sc_paddr + off) | PMAP_NOCACHE); 282 } 283 284 int 285 simplefb_alloc_screen(void *v, const struct wsscreen_descr *type, 286 void **cookiep, int *curxp, int *curyp, long *attrp) 287 { 288 return rasops_alloc_screen(v, cookiep, curxp, curyp, attrp); 289 } 290 291 #include "ukbd.h" 292 293 #if NUKBD > 0 294 #include <dev/usb/ukbdvar.h> 295 #endif 296 297 void 298 simplefb_init_cons(bus_space_tag_t iot) 299 { 300 struct rasops_info *ri = &simplefb_ri; 301 bus_space_handle_t ioh; 302 struct fdt_reg reg; 303 void *node; 304 long defattr = 0; 305 306 node = fdt_find_cons("simple-framebuffer"); 307 if (node == NULL) 308 return; 309 310 if (fdt_get_reg(node, 0, ®)) 311 return; 312 313 if (bus_space_map(iot, reg.addr, reg.size, 314 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE, &ioh)) 315 return; 316 317 ri->ri_bits = bus_space_vaddr(iot, ioh); 318 319 if (simplefb_init(stdout_node, ri)) 320 return; 321 322 ri->ri_bs = simplefb_bs; 323 rasops_init(ri, SIMPLEFB_HEIGHT, SIMPLEFB_WIDTH); 324 325 simplefb_wsd.capabilities = ri->ri_caps; 326 simplefb_wsd.ncols = ri->ri_cols; 327 simplefb_wsd.nrows = ri->ri_rows; 328 simplefb_wsd.textops = &ri->ri_ops; 329 simplefb_wsd.fontwidth = ri->ri_font->fontwidth; 330 simplefb_wsd.fontheight = ri->ri_font->fontheight; 331 332 ri->ri_ops.alloc_attr(ri, 0, 0, 0, &defattr); 333 wsdisplay_cnattach(&simplefb_wsd, ri, 0, 0, defattr); 334 335 #if NUKBD > 0 336 /* Allow USB keyboards to become the console input device. */ 337 ukbd_cnattach(); 338 #endif 339 } 340