1 /* 2 * Ben NanoNote LCD initialisation, based on uboot-xburst and xburst-tools. 3 * 4 * Copyright (C) 2001-2002 Wolfgang Denk, DENX Software Engineering, <wd@denx.de> 5 * Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk> 6 * 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 #include "board-display.h" 22 23 #include "jzlcd.h" 24 #include "sdram.h" 25 #include "board.h" 26 27 extern vidinfo_t panel_info; 28 extern struct jzfb_info jzfb; 29 static uint32_t lcd_base; 30 31 static uint16_t get_line_length() 32 { 33 return ALIGN((panel_info.jz_fb->w * panel_info.jz_fb->bpp) / 8, sizeof(uint32_t)); 34 } 35 36 static uint32_t *get_pixel32(uint16_t h, uint16_t v) 37 { 38 return (uint32_t *) (lcd_base + v * get_line_length()) + h; 39 } 40 41 static uint16_t *get_pixel16(uint16_t h, uint16_t v) 42 { 43 return (uint16_t *) (lcd_base + v * get_line_length()) + h; 44 } 45 46 static uint8_t *get_pixel8(uint16_t h, uint16_t v) 47 { 48 return (uint8_t *) (lcd_base + v * get_line_length()) + h; 49 } 50 51 static uint8_t *get_pixel4(uint16_t h, uint16_t v) 52 { 53 return (uint8_t *) (lcd_base + v * get_line_length()) + h / 2; 54 } 55 56 static inline uint16_t div(uint16_t num, uint16_t denom, uint16_t scale) 57 { 58 return (scale * num) / denom; 59 } 60 61 static uint32_t pixel(uint8_t r, uint8_t g, uint8_t b, uint8_t rmax, uint8_t gmax, uint8_t bmax, uint8_t rshift, uint8_t gshift, uint8_t bshift) 62 { 63 return (div(r, 255, rmax) << rshift) | (div(g, 255, gmax) << gshift) | (div(b, 255, bmax) << bshift); 64 } 65 66 uint32_t get_bitmap_value(uint16_t x, uint32_t value) 67 { 68 return (value >> (((panel_info.jz_fb->w - 1 - x) * 32) / panel_info.jz_fb->w)) % 2 ? 0xffffffff : 0; 69 } 70 71 static void get_colour(uint16_t h, uint16_t v, uint8_t rgb[], uint16_t pixel_type) 72 { 73 uint16_t v_max = panel_info.jz_fb->h - 1; 74 uint16_t h_max = panel_info.jz_fb->w - 1; 75 76 rgb[(pixel_type - 1) % 3] = div(h, h_max, 255); 77 rgb[pixel_type % 3] = div(v, v_max, 255); 78 rgb[(pixel_type + 1) % 3] = (rgb[0] + rgb[1]) / 2; 79 } 80 81 static void set_pixel32(uint16_t h, uint16_t v, uint32_t value) 82 { 83 uint32_t *pix = get_pixel32(h, v); 84 *pix = value; 85 } 86 87 static void set_pixel16_565(uint16_t h, uint16_t v, uint32_t value) 88 { 89 uint16_t *pix = get_pixel16(h, v); 90 *pix = (uint16_t) value; 91 } 92 93 static void set_pixel8(uint16_t h, uint16_t v, uint32_t value) 94 { 95 uint8_t *pix = get_pixel8(h, v); 96 *pix = (uint8_t) value; 97 } 98 99 static void set_pixel4(uint16_t h, uint16_t v, uint32_t value) 100 { 101 uint8_t *pix = get_pixel4(h, v); 102 uint8_t mask = h & 1 ? 0xf0 : 0x0f; 103 *pix = (*pix & mask) | ((uint8_t) value); 104 } 105 106 void set_pixel(uint16_t h, uint16_t v, uint32_t value) 107 { 108 switch (panel_info.jz_fb->bpp) 109 { 110 case 32: 111 set_pixel32(h, v, value); 112 break; 113 114 case 8: 115 set_pixel8(h, v, value); 116 break; 117 118 case 4: 119 set_pixel4(h, v, value); 120 break; 121 122 case 16: 123 default: 124 set_pixel16_565(h, v, value); 125 break; 126 } 127 } 128 static void test_pixel32(uint16_t h, uint16_t v, uint16_t pixel_type) 129 { 130 uint8_t rgb[3]; 131 132 get_colour(h, v, rgb, pixel_type); 133 set_pixel32(h, v, pixel(rgb[0], rgb[1], rgb[2], 255, 255, 255, 16, 8, 0) | get_bitmap_value(h, pixel_type)); 134 } 135 136 static void test_pixel16_565(uint16_t h, uint16_t v, uint16_t pixel_type) 137 { 138 uint8_t rgb[3]; 139 140 get_colour(h, v, rgb, pixel_type); 141 set_pixel16_565(h, v, pixel(rgb[0], rgb[1], rgb[2], 31, 63, 31, 11, 5, 0) | get_bitmap_value(h, pixel_type)); 142 } 143 144 static void test_pixel8(uint16_t h, uint16_t v, uint16_t pixel_type) 145 { 146 uint8_t rgb[3]; 147 148 get_colour(h, v, rgb, pixel_type); 149 set_pixel8(h, v, pixel(rgb[0], rgb[1], rgb[2], 7, 7, 3, 5, 2, 0) | get_bitmap_value(h, pixel_type)); 150 } 151 152 static void test_pixel4(uint16_t h, uint16_t v, uint16_t pixel_type) 153 { 154 uint8_t rgb[3]; 155 156 get_colour(h, v, rgb, pixel_type); 157 set_pixel4(h, v, pixel(rgb[0], rgb[1], rgb[2], 1, 2, 1, 3, 1, 0) | get_bitmap_value(h, pixel_type)); 158 } 159 160 void test_pixel(uint16_t h, uint16_t v, uint16_t pixel_type) 161 { 162 switch (panel_info.jz_fb->bpp) 163 { 164 case 32: 165 test_pixel32(h, v, pixel_type); 166 break; 167 168 case 8: 169 test_pixel8(h, v, pixel_type); 170 break; 171 172 case 4: 173 test_pixel4(h, v, pixel_type); 174 break; 175 176 case 16: 177 default: 178 test_pixel16_565(h, v, pixel_type); 179 break; 180 } 181 } 182 183 void clear_pixel32(uint16_t h, uint16_t v) 184 { 185 uint32_t *pix = get_pixel32(h, v); 186 *pix = 0; 187 } 188 189 void clear_pixel16(uint16_t h, uint16_t v) 190 { 191 uint16_t *pix = get_pixel16(h, v); 192 *pix = 0; 193 } 194 195 void clear_pixel8(uint16_t h, uint16_t v) 196 { 197 uint8_t *pix = get_pixel8(h, v); 198 *pix = 0; 199 } 200 201 void clear_pixel4(uint16_t h, uint16_t v) 202 { 203 uint8_t *pix = get_pixel4(h, v); 204 uint8_t mask = h & 1 ? 0xf0 : 0x0f; 205 *pix = *pix & mask; 206 } 207 208 void clear_pixel(uint16_t h, uint16_t v) 209 { 210 switch (panel_info.jz_fb->bpp) 211 { 212 case 32: 213 clear_pixel32(h, v); 214 break; 215 216 case 8: 217 clear_pixel8(h, v); 218 break; 219 220 case 4: 221 clear_pixel4(h, v); 222 break; 223 224 case 16: 225 default: 226 clear_pixel16(h, v); 227 break; 228 } 229 } 230 231 void test_pattern() 232 { 233 uint16_t v_max = panel_info.jz_fb->h; 234 uint16_t h_max = panel_info.jz_fb->w; 235 uint16_t v, h; 236 237 for (v = 0; v < v_max; v += 1) { 238 for (h = 0; h < h_max; h += 1) { 239 test_pixel(h, v, 1); 240 } 241 } 242 } 243 244 void lcd_clear(uint32_t lcd_base) 245 { 246 uint16_t v_max = panel_info.jz_fb->h; 247 uint16_t h_max = panel_info.jz_fb->w; 248 uint16_t v, h; 249 uint32_t *pix = (uint32_t *)lcd_base; 250 251 for (v = 0; v < v_max; v += 1) { 252 for (h = 0; h < h_max; h += 1) { 253 *pix++ = 0; 254 } 255 } 256 } 257 258 /* LCD initialisation. */ 259 260 void lcd_init() 261 { 262 __lcd_display_pin_init(); 263 __lcd_display_on(); 264 265 /* Initialise the member here since the address is otherwise invalid. */ 266 267 panel_info.jz_fb = (struct jzfb_info *) ((uint32_t) &jzfb & ~0x80000000); 268 269 lcd_base = lcd_ctrl_init(); 270 lcd_clear(lcd_base); 271 lcd_enable(); 272 }