Dima Zavin | d5b0b6a | 2009-01-15 18:09:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008, Google Inc. |
| 3 | * All rights reserved. |
| 4 | * |
Shashank Mittal | 4f99a88 | 2010-02-01 13:58:50 -0800 | [diff] [blame] | 5 | * Copyright (c) 2009-2010, Code Aurora Forum. All rights reserved. |
| 6 | * |
Dima Zavin | d5b0b6a | 2009-01-15 18:09:25 -0800 | [diff] [blame] | 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in |
| 14 | * the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 20 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 21 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 23 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 24 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 25 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 27 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 | * SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #include <debug.h> |
| 32 | #include <err.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <dev/fbcon.h> |
| 35 | |
| 36 | #include "font5x12.h" |
| 37 | |
| 38 | struct pos { |
| 39 | int x; |
| 40 | int y; |
| 41 | }; |
| 42 | |
| 43 | static struct fbcon_config *config = NULL; |
| 44 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame^] | 45 | #define RGB565_BLACK 0x0000 |
Dima Zavin | d5b0b6a | 2009-01-15 18:09:25 -0800 | [diff] [blame] | 46 | #define RGB565_WHITE 0xffff |
| 47 | |
| 48 | #define FONT_WIDTH 5 |
| 49 | #define FONT_HEIGHT 12 |
| 50 | |
| 51 | static uint16_t BGCOLOR; |
| 52 | static uint16_t FGCOLOR; |
| 53 | |
| 54 | static struct pos cur_pos; |
| 55 | static struct pos max_pos; |
| 56 | |
| 57 | static void fbcon_drawglyph(uint16_t *pixels, uint16_t paint, unsigned stride, |
| 58 | unsigned *glyph) |
| 59 | { |
| 60 | unsigned x, y, data; |
| 61 | stride -= FONT_WIDTH; |
| 62 | |
| 63 | data = glyph[0]; |
| 64 | for (y = 0; y < (FONT_HEIGHT / 2); ++y) { |
| 65 | for (x = 0; x < FONT_WIDTH; ++x) { |
| 66 | if (data & 1) |
| 67 | *pixels = paint; |
| 68 | data >>= 1; |
| 69 | pixels++; |
| 70 | } |
| 71 | pixels += stride; |
| 72 | } |
| 73 | |
| 74 | data = glyph[1]; |
| 75 | for (y = 0; y < (FONT_HEIGHT / 2); y++) { |
| 76 | for (x = 0; x < FONT_WIDTH; x++) { |
| 77 | if (data & 1) |
| 78 | *pixels = paint; |
| 79 | data >>= 1; |
| 80 | pixels++; |
| 81 | } |
| 82 | pixels += stride; |
| 83 | } |
| 84 | } |
| 85 | |
Dima Zavin | 25ed994 | 2009-01-28 17:04:19 -0800 | [diff] [blame] | 86 | static void fbcon_flush(void) |
| 87 | { |
| 88 | if (config->update_start) |
| 89 | config->update_start(); |
| 90 | if (config->update_done) |
| 91 | while (!config->update_done()); |
| 92 | } |
| 93 | |
Dima Zavin | d5b0b6a | 2009-01-15 18:09:25 -0800 | [diff] [blame] | 94 | /* TODO: Take stride into account */ |
| 95 | static void fbcon_scroll_up(void) |
| 96 | { |
| 97 | unsigned short *dst = config->base; |
| 98 | unsigned short *src = dst + (config->width * FONT_HEIGHT); |
| 99 | unsigned count = config->width * (config->height - FONT_HEIGHT); |
| 100 | |
| 101 | while(count--) { |
| 102 | *dst++ = *src++; |
| 103 | } |
| 104 | |
| 105 | count = config->width * FONT_HEIGHT; |
| 106 | while(count--) { |
| 107 | *dst++ = BGCOLOR; |
| 108 | } |
Dima Zavin | 25ed994 | 2009-01-28 17:04:19 -0800 | [diff] [blame] | 109 | |
| 110 | fbcon_flush(); |
Dima Zavin | d5b0b6a | 2009-01-15 18:09:25 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* TODO: take stride into account */ |
Shashank Mittal | 4f99a88 | 2010-02-01 13:58:50 -0800 | [diff] [blame] | 114 | void fbcon_clear(void) |
Dima Zavin | d5b0b6a | 2009-01-15 18:09:25 -0800 | [diff] [blame] | 115 | { |
| 116 | uint16_t *dst = config->base; |
| 117 | unsigned count = config->width * config->height; |
| 118 | |
| 119 | cur_pos.x = 0; |
| 120 | cur_pos.y = 0; |
| 121 | |
| 122 | while (count--) |
| 123 | *dst++ = BGCOLOR; |
| 124 | } |
| 125 | |
Dima Zavin | d5b0b6a | 2009-01-15 18:09:25 -0800 | [diff] [blame] | 126 | |
| 127 | static void fbcon_set_colors(unsigned bg, unsigned fg) |
| 128 | { |
| 129 | BGCOLOR = bg; |
| 130 | FGCOLOR = fg; |
| 131 | } |
| 132 | |
| 133 | void fbcon_putc(char c) |
| 134 | { |
| 135 | uint16_t *pixels; |
| 136 | |
| 137 | /* ignore anything that happens before fbcon is initialized */ |
| 138 | if (!config) |
| 139 | return; |
| 140 | |
| 141 | if((unsigned char)c > 127) |
| 142 | return; |
| 143 | if((unsigned char)c < 32) { |
| 144 | if(c == '\n') |
| 145 | goto newline; |
| 146 | else if (c == '\r') |
| 147 | cur_pos.x = 0; |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | pixels = config->base; |
| 152 | pixels += cur_pos.y * FONT_HEIGHT * config->width; |
| 153 | pixels += cur_pos.x * (FONT_WIDTH + 1); |
| 154 | fbcon_drawglyph(pixels, FGCOLOR, config->stride, |
| 155 | font5x12 + (c - 32) * 2); |
| 156 | |
| 157 | cur_pos.x++; |
| 158 | if (cur_pos.x < max_pos.x) |
| 159 | return; |
| 160 | |
| 161 | newline: |
| 162 | cur_pos.y++; |
| 163 | cur_pos.x = 0; |
| 164 | if(cur_pos.y >= max_pos.y) { |
| 165 | cur_pos.y = max_pos.y - 1; |
| 166 | fbcon_scroll_up(); |
Dima Zavin | 25ed994 | 2009-01-28 17:04:19 -0800 | [diff] [blame] | 167 | } else |
| 168 | fbcon_flush(); |
Dima Zavin | d5b0b6a | 2009-01-15 18:09:25 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void fbcon_setup(struct fbcon_config *_config) |
| 172 | { |
| 173 | uint32_t bg; |
| 174 | uint32_t fg; |
| 175 | |
| 176 | ASSERT(_config); |
| 177 | |
| 178 | config = _config; |
| 179 | |
| 180 | switch (config->format) { |
| 181 | case FB_FORMAT_RGB565: |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame^] | 182 | bg = RGB565_BLACK; |
Dima Zavin | d5b0b6a | 2009-01-15 18:09:25 -0800 | [diff] [blame] | 183 | fg = RGB565_WHITE; |
| 184 | break; |
| 185 | |
| 186 | default: |
| 187 | dprintf(CRITICAL, "unknown framebuffer pixel format\n"); |
| 188 | ASSERT(0); |
| 189 | break; |
| 190 | } |
| 191 | |
| 192 | fbcon_set_colors(bg, fg); |
| 193 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame^] | 194 | //fbcon_clear(); |
Dima Zavin | d5b0b6a | 2009-01-15 18:09:25 -0800 | [diff] [blame] | 195 | fbcon_flush(); |
| 196 | |
| 197 | cur_pos.x = 0; |
| 198 | cur_pos.y = 0; |
| 199 | max_pos.x = config->width / (FONT_WIDTH+1); |
| 200 | max_pos.y = (config->height - 1) / FONT_HEIGHT; |
| 201 | } |
Shashank Mittal | 4f99a88 | 2010-02-01 13:58:50 -0800 | [diff] [blame] | 202 | |
| 203 | struct fbcon_config* fbcon_display(void) |
| 204 | { |
| 205 | return config; |
| 206 | } |