Path: blob/main/sys/arm/ti/am335x/am335x_lcd_syscons.c
39536 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2013 Oleksandr Tymoshenko <[email protected]>4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#include <sys/param.h>29#include <sys/systm.h>30#include <sys/bus.h>31#include <sys/conf.h>32#include <sys/endian.h>33#include <sys/kernel.h>34#include <sys/lock.h>35#include <sys/malloc.h>36#include <sys/module.h>37#include <sys/mutex.h>38#include <sys/resource.h>39#include <sys/rman.h>40#include <sys/fbio.h>41#include <sys/consio.h>4243#include <sys/kdb.h>4445#include <machine/bus.h>46#include <machine/resource.h>47#include <machine/intr.h>4849#include <dev/ofw/ofw_bus.h>50#include <dev/ofw/ofw_bus_subr.h>5152#include <dev/fb/fbreg.h>53#include <dev/syscons/syscons.h>5455#include "am335x_lcd.h"5657struct video_adapter_softc {58/* Videoadpater part */59video_adapter_t va;60int console;6162intptr_t fb_addr;63intptr_t fb_paddr;64unsigned int fb_size;6566unsigned int height;67unsigned int width;68unsigned int depth;69unsigned int stride;7071unsigned int xmargin;72unsigned int ymargin;7374unsigned char *font;75int initialized;76};7778struct argb {79uint8_t a;80uint8_t r;81uint8_t g;82uint8_t b;83};8485static struct argb am335x_syscons_palette[16] = {86{0x00, 0x00, 0x00, 0x00},87{0x00, 0x00, 0x00, 0xaa},88{0x00, 0x00, 0xaa, 0x00},89{0x00, 0x00, 0xaa, 0xaa},90{0x00, 0xaa, 0x00, 0x00},91{0x00, 0xaa, 0x00, 0xaa},92{0x00, 0xaa, 0x55, 0x00},93{0x00, 0xaa, 0xaa, 0xaa},94{0x00, 0x55, 0x55, 0x55},95{0x00, 0x55, 0x55, 0xff},96{0x00, 0x55, 0xff, 0x55},97{0x00, 0x55, 0xff, 0xff},98{0x00, 0xff, 0x55, 0x55},99{0x00, 0xff, 0x55, 0xff},100{0x00, 0xff, 0xff, 0x55},101{0x00, 0xff, 0xff, 0xff}102};103104/* mouse pointer from dev/syscons/scgfbrndr.c */105static u_char mouse_pointer[16] = {1060x00, 0x40, 0x60, 0x70, 0x78, 0x7c, 0x7e, 0x68,1070x0c, 0x0c, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00108};109110#define AM335X_FONT_HEIGHT 16111112#define FB_WIDTH 640113#define FB_HEIGHT 480114#define FB_DEPTH 24115116static struct video_adapter_softc va_softc;117118static int am335x_syscons_configure(int flags);119120/*121* Video driver routines and glue.122*/123static vi_probe_t am335x_syscons_probe;124static vi_init_t am335x_syscons_init;125static vi_get_info_t am335x_syscons_get_info;126static vi_query_mode_t am335x_syscons_query_mode;127static vi_set_mode_t am335x_syscons_set_mode;128static vi_save_font_t am335x_syscons_save_font;129static vi_load_font_t am335x_syscons_load_font;130static vi_show_font_t am335x_syscons_show_font;131static vi_save_palette_t am335x_syscons_save_palette;132static vi_load_palette_t am335x_syscons_load_palette;133static vi_set_border_t am335x_syscons_set_border;134static vi_save_state_t am335x_syscons_save_state;135static vi_load_state_t am335x_syscons_load_state;136static vi_set_win_org_t am335x_syscons_set_win_org;137static vi_read_hw_cursor_t am335x_syscons_read_hw_cursor;138static vi_set_hw_cursor_t am335x_syscons_set_hw_cursor;139static vi_set_hw_cursor_shape_t am335x_syscons_set_hw_cursor_shape;140static vi_blank_display_t am335x_syscons_blank_display;141static vi_mmap_t am335x_syscons_mmap;142static vi_ioctl_t am335x_syscons_ioctl;143static vi_clear_t am335x_syscons_clear;144static vi_fill_rect_t am335x_syscons_fill_rect;145static vi_bitblt_t am335x_syscons_bitblt;146static vi_diag_t am335x_syscons_diag;147static vi_save_cursor_palette_t am335x_syscons_save_cursor_palette;148static vi_load_cursor_palette_t am335x_syscons_load_cursor_palette;149static vi_copy_t am335x_syscons_copy;150static vi_putp_t am335x_syscons_putp;151static vi_putc_t am335x_syscons_putc;152static vi_puts_t am335x_syscons_puts;153static vi_putm_t am335x_syscons_putm;154155static video_switch_t am335x_sysconsvidsw = {156.probe = am335x_syscons_probe,157.init = am335x_syscons_init,158.get_info = am335x_syscons_get_info,159.query_mode = am335x_syscons_query_mode,160.set_mode = am335x_syscons_set_mode,161.save_font = am335x_syscons_save_font,162.load_font = am335x_syscons_load_font,163.show_font = am335x_syscons_show_font,164.save_palette = am335x_syscons_save_palette,165.load_palette = am335x_syscons_load_palette,166.set_border = am335x_syscons_set_border,167.save_state = am335x_syscons_save_state,168.load_state = am335x_syscons_load_state,169.set_win_org = am335x_syscons_set_win_org,170.read_hw_cursor = am335x_syscons_read_hw_cursor,171.set_hw_cursor = am335x_syscons_set_hw_cursor,172.set_hw_cursor_shape = am335x_syscons_set_hw_cursor_shape,173.blank_display = am335x_syscons_blank_display,174.mmap = am335x_syscons_mmap,175.ioctl = am335x_syscons_ioctl,176.clear = am335x_syscons_clear,177.fill_rect = am335x_syscons_fill_rect,178.bitblt = am335x_syscons_bitblt,179.diag = am335x_syscons_diag,180.save_cursor_palette = am335x_syscons_save_cursor_palette,181.load_cursor_palette = am335x_syscons_load_cursor_palette,182.copy = am335x_syscons_copy,183.putp = am335x_syscons_putp,184.putc = am335x_syscons_putc,185.puts = am335x_syscons_puts,186.putm = am335x_syscons_putm,187};188189VIDEO_DRIVER(am335x_syscons, am335x_sysconsvidsw, am335x_syscons_configure);190191static vr_init_t am335x_rend_init;192static vr_clear_t am335x_rend_clear;193static vr_draw_border_t am335x_rend_draw_border;194static vr_draw_t am335x_rend_draw;195static vr_set_cursor_t am335x_rend_set_cursor;196static vr_draw_cursor_t am335x_rend_draw_cursor;197static vr_blink_cursor_t am335x_rend_blink_cursor;198static vr_set_mouse_t am335x_rend_set_mouse;199static vr_draw_mouse_t am335x_rend_draw_mouse;200201/*202* We use our own renderer; this is because we must emulate a hardware203* cursor.204*/205static sc_rndr_sw_t am335x_rend = {206am335x_rend_init,207am335x_rend_clear,208am335x_rend_draw_border,209am335x_rend_draw,210am335x_rend_set_cursor,211am335x_rend_draw_cursor,212am335x_rend_blink_cursor,213am335x_rend_set_mouse,214am335x_rend_draw_mouse215};216217RENDERER(am335x_syscons, 0, am335x_rend, gfb_set);218RENDERER_MODULE(am335x_syscons, gfb_set);219220static void221am335x_rend_init(scr_stat* scp)222{223}224225static void226am335x_rend_clear(scr_stat* scp, int c, int attr)227{228}229230static void231am335x_rend_draw_border(scr_stat* scp, int color)232{233}234235static void236am335x_rend_draw(scr_stat* scp, int from, int count, int flip)237{238video_adapter_t* adp = scp->sc->adp;239int i, c, a;240241if (!flip) {242/* Normal printing */243vidd_puts(adp, from, (uint16_t*)sc_vtb_pointer(&scp->vtb, from), count);244} else {245/* This is for selections and such: invert the color attribute */246for (i = count; i-- > 0; ++from) {247c = sc_vtb_getc(&scp->vtb, from);248a = sc_vtb_geta(&scp->vtb, from) >> 8;249vidd_putc(adp, from, c, (a >> 4) | ((a & 0xf) << 4));250}251}252}253254static void255am335x_rend_set_cursor(scr_stat* scp, int base, int height, int blink)256{257}258259static void260am335x_rend_draw_cursor(scr_stat* scp, int off, int blink, int on, int flip)261{262video_adapter_t* adp = scp->sc->adp;263struct video_adapter_softc *sc;264int row, col;265uint8_t *addr;266int i, j, bytes;267268sc = (struct video_adapter_softc *)adp;269270if (scp->curs_attr.height <= 0)271return;272273if (sc->fb_addr == 0)274return;275276if (off >= adp->va_info.vi_width * adp->va_info.vi_height)277return;278279/* calculate the coordinates in the video buffer */280row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;281col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;282283addr = (uint8_t *)sc->fb_addr284+ (row + sc->ymargin)*(sc->stride)285+ (sc->depth/8) * (col + sc->xmargin);286287bytes = sc->depth/8;288289/* our cursor consists of simply inverting the char under it */290for (i = 0; i < adp->va_info.vi_cheight; i++) {291for (j = 0; j < adp->va_info.vi_cwidth; j++) {292switch (sc->depth) {293case 32:294case 24:295addr[bytes*j + 2] ^= 0xff;296/* FALLTHROUGH */297case 16:298addr[bytes*j + 1] ^= 0xff;299addr[bytes*j] ^= 0xff;300break;301default:302break;303}304}305306addr += sc->stride;307}308}309310static void311am335x_rend_blink_cursor(scr_stat* scp, int at, int flip)312{313}314315static void316am335x_rend_set_mouse(scr_stat* scp)317{318}319320static void321am335x_rend_draw_mouse(scr_stat* scp, int x, int y, int on)322{323vidd_putm(scp->sc->adp, x, y, mouse_pointer, 0xffffffff, 16, 8);324}325326static uint16_t am335x_syscons_static_window[ROW*COL];327extern u_char dflt_font_16[];328329/*330* Update videoadapter settings after changing resolution331*/332static void333am335x_syscons_update_margins(video_adapter_t *adp)334{335struct video_adapter_softc *sc;336video_info_t *vi;337338sc = (struct video_adapter_softc *)adp;339vi = &adp->va_info;340341sc->xmargin = (sc->width - (vi->vi_width * vi->vi_cwidth)) / 2;342sc->ymargin = (sc->height - (vi->vi_height * vi->vi_cheight))/2;343}344345static phandle_t346am335x_syscons_find_panel_node(phandle_t start)347{348phandle_t child;349phandle_t result;350351for (child = OF_child(start); child != 0; child = OF_peer(child)) {352if (ofw_bus_node_is_compatible(child, "ti,am335x-lcd"))353return (child);354if ((result = am335x_syscons_find_panel_node(child)))355return (result);356}357358return (0);359}360361static int362am335x_syscons_configure(int flags)363{364struct video_adapter_softc *va_sc;365366va_sc = &va_softc;367phandle_t display, root;368pcell_t cell;369370if (va_sc->initialized)371return (0);372373va_sc->width = 0;374va_sc->height = 0;375376/*377* It seems there is no way to let syscons framework know378* that framebuffer resolution has changed. So just try379* to fetch data from FDT and go with defaults if failed380*/381root = OF_finddevice("/");382if ((root != -1) &&383(display = am335x_syscons_find_panel_node(root))) {384if ((OF_getencprop(display, "panel_width", &cell,385sizeof(cell))) > 0)386va_sc->width = cell;387388if ((OF_getencprop(display, "panel_height", &cell,389sizeof(cell))) > 0)390va_sc->height = cell;391}392393if (va_sc->width == 0)394va_sc->width = FB_WIDTH;395if (va_sc->height == 0)396va_sc->height = FB_HEIGHT;397398am335x_syscons_init(0, &va_sc->va, 0);399400va_sc->initialized = 1;401402return (0);403}404405static int406am335x_syscons_probe(int unit, video_adapter_t **adp, void *arg, int flags)407{408409return (0);410}411412static int413am335x_syscons_init(int unit, video_adapter_t *adp, int flags)414{415struct video_adapter_softc *sc;416video_info_t *vi;417418sc = (struct video_adapter_softc *)adp;419vi = &adp->va_info;420421vid_init_struct(adp, "am335x_syscons", -1, unit);422423sc->font = dflt_font_16;424vi->vi_cheight = AM335X_FONT_HEIGHT;425vi->vi_cwidth = 8;426427vi->vi_width = sc->width/8;428vi->vi_height = sc->height/vi->vi_cheight;429430/*431* Clamp width/height to syscons maximums432*/433if (vi->vi_width > COL)434vi->vi_width = COL;435if (vi->vi_height > ROW)436vi->vi_height = ROW;437438sc->xmargin = (sc->width - (vi->vi_width * vi->vi_cwidth)) / 2;439sc->ymargin = (sc->height - (vi->vi_height * vi->vi_cheight))/2;440441adp->va_window = (vm_offset_t) am335x_syscons_static_window;442adp->va_flags |= V_ADP_FONT /* | V_ADP_COLOR | V_ADP_MODECHANGE */;443444vid_register(&sc->va);445446return (0);447}448449static int450am335x_syscons_get_info(video_adapter_t *adp, int mode, video_info_t *info)451{452bcopy(&adp->va_info, info, sizeof(*info));453return (0);454}455456static int457am335x_syscons_query_mode(video_adapter_t *adp, video_info_t *info)458{459return (0);460}461462static int463am335x_syscons_set_mode(video_adapter_t *adp, int mode)464{465return (0);466}467468static int469am335x_syscons_save_font(video_adapter_t *adp, int page, int size, int width,470u_char *data, int c, int count)471{472return (0);473}474475static int476am335x_syscons_load_font(video_adapter_t *adp, int page, int size, int width,477u_char *data, int c, int count)478{479struct video_adapter_softc *sc = (struct video_adapter_softc *)adp;480481sc->font = data;482483return (0);484}485486static int487am335x_syscons_show_font(video_adapter_t *adp, int page)488{489return (0);490}491492static int493am335x_syscons_save_palette(video_adapter_t *adp, u_char *palette)494{495return (0);496}497498static int499am335x_syscons_load_palette(video_adapter_t *adp, u_char *palette)500{501return (0);502}503504static int505am335x_syscons_set_border(video_adapter_t *adp, int border)506{507return (am335x_syscons_blank_display(adp, border));508}509510static int511am335x_syscons_save_state(video_adapter_t *adp, void *p, size_t size)512{513return (0);514}515516static int517am335x_syscons_load_state(video_adapter_t *adp, void *p)518{519return (0);520}521522static int523am335x_syscons_set_win_org(video_adapter_t *adp, off_t offset)524{525return (0);526}527528static int529am335x_syscons_read_hw_cursor(video_adapter_t *adp, int *col, int *row)530{531*col = *row = 0;532533return (0);534}535536static int537am335x_syscons_set_hw_cursor(video_adapter_t *adp, int col, int row)538{539return (0);540}541542static int543am335x_syscons_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,544int celsize, int blink)545{546return (0);547}548549static int550am335x_syscons_blank_display(video_adapter_t *adp, int mode)551{552553struct video_adapter_softc *sc;554555sc = (struct video_adapter_softc *)adp;556if (sc && sc->fb_addr)557memset((void*)sc->fb_addr, 0, sc->fb_size);558559return (0);560}561562static int563am335x_syscons_mmap(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr,564int prot, vm_memattr_t *memattr)565{566struct video_adapter_softc *sc;567568sc = (struct video_adapter_softc *)adp;569570/*571* This might be a legacy VGA mem request: if so, just point it at the572* framebuffer, since it shouldn't be touched573*/574if (offset < sc->stride*sc->height) {575*paddr = sc->fb_paddr + offset;576return (0);577}578579return (EINVAL);580}581582static int583am335x_syscons_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)584{585struct video_adapter_softc *sc;586struct fbtype *fb;587588sc = (struct video_adapter_softc *)adp;589590switch (cmd) {591case FBIOGTYPE:592fb = (struct fbtype *)data;593fb->fb_type = FBTYPE_PCIMISC;594fb->fb_height = sc->height;595fb->fb_width = sc->width;596fb->fb_depth = sc->depth;597if (sc->depth <= 1 || sc->depth > 8)598fb->fb_cmsize = 0;599else600fb->fb_cmsize = 1 << sc->depth;601fb->fb_size = sc->fb_size;602break;603default:604return (fb_commonioctl(adp, cmd, data));605}606607return (0);608}609610static int611am335x_syscons_clear(video_adapter_t *adp)612{613614return (am335x_syscons_blank_display(adp, 0));615}616617static int618am335x_syscons_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)619{620621return (0);622}623624static int625am335x_syscons_bitblt(video_adapter_t *adp, ...)626{627628return (0);629}630631static int632am335x_syscons_diag(video_adapter_t *adp, int level)633{634635return (0);636}637638static int639am335x_syscons_save_cursor_palette(video_adapter_t *adp, u_char *palette)640{641642return (0);643}644645static int646am335x_syscons_load_cursor_palette(video_adapter_t *adp, u_char *palette)647{648649return (0);650}651652static int653am335x_syscons_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)654{655656return (0);657}658659static int660am335x_syscons_putp(video_adapter_t *adp, vm_offset_t off, uint32_t p, uint32_t a,661int size, int bpp, int bit_ltor, int byte_ltor)662{663664return (0);665}666667static int668am335x_syscons_putc(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)669{670struct video_adapter_softc *sc;671int row;672int col;673int i, j, k;674uint8_t *addr;675u_char *p;676uint8_t fg, bg, color;677uint16_t rgb;678679sc = (struct video_adapter_softc *)adp;680681if (sc->fb_addr == 0)682return (0);683684row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;685col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;686p = sc->font + c*AM335X_FONT_HEIGHT;687addr = (uint8_t *)sc->fb_addr688+ (row + sc->ymargin)*(sc->stride)689+ (sc->depth/8) * (col + sc->xmargin);690691fg = a & 0xf ;692bg = (a >> 4) & 0xf;693694for (i = 0; i < AM335X_FONT_HEIGHT; i++) {695for (j = 0, k = 7; j < 8; j++, k--) {696if ((p[i] & (1 << k)) == 0)697color = bg;698else699color = fg;700701switch (sc->depth) {702case 32:703addr[4*j+0] = am335x_syscons_palette[color].r;704addr[4*j+1] = am335x_syscons_palette[color].g;705addr[4*j+2] = am335x_syscons_palette[color].b;706addr[4*j+3] = am335x_syscons_palette[color].a;707break;708case 24:709addr[3*j] = am335x_syscons_palette[color].r;710addr[3*j+1] = am335x_syscons_palette[color].g;711addr[3*j+2] = am335x_syscons_palette[color].b;712break;713case 16:714rgb = (am335x_syscons_palette[color].r >> 3) << 11;715rgb |= (am335x_syscons_palette[color].g >> 2) << 5;716rgb |= (am335x_syscons_palette[color].b >> 3);717addr[2*j] = rgb & 0xff;718addr[2*j + 1] = (rgb >> 8) & 0xff;719default:720/* Not supported yet */721break;722}723}724725addr += (sc->stride);726}727728return (0);729}730731static int732am335x_syscons_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s, int len)733{734int i;735736for (i = 0; i < len; i++)737am335x_syscons_putc(adp, off + i, s[i] & 0xff, (s[i] & 0xff00) >> 8);738739return (0);740}741742static int743am335x_syscons_putm(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,744uint32_t pixel_mask, int size, int width)745{746747return (0);748}749750/* Initialization function */751int am335x_lcd_syscons_setup(vm_offset_t vaddr, vm_paddr_t paddr,752struct panel_info *panel)753{754struct video_adapter_softc *va_sc = &va_softc;755756va_sc->fb_addr = vaddr;757va_sc->fb_paddr = paddr;758va_sc->depth = panel->bpp;759va_sc->stride = panel->bpp*panel->panel_width/8;760761va_sc->width = panel->panel_width;762va_sc->height = panel->panel_height;763va_sc->fb_size = va_sc->width * va_sc->height764* va_sc->depth/8;765am335x_syscons_update_margins(&va_sc->va);766767return (0);768}769770771