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 "lcd.h" 24 #include "jzlcd.h" 25 #include "sdram.h" 26 #include "cpu.h" 27 #include "cpm.h" 28 #include "board.h" 29 30 extern vidinfo_t panel_info; 31 static uint32_t lcd_base; /* the screen memory, not the LCD registers */ 32 33 34 35 static uint16_t get_line_length() 36 { 37 return ALIGN((panel_info.jz_fb->w * panel_info.jz_fb->bpp) / 8, sizeof(uint32_t)); 38 } 39 40 static uint32_t *get_pixel32(uint16_t h, uint16_t v) 41 { 42 return (uint32_t *) (lcd_base + v * get_line_length()) + h; 43 } 44 45 static uint16_t *get_pixel16(uint16_t h, uint16_t v) 46 { 47 return (uint16_t *) (lcd_base + v * get_line_length()) + h; 48 } 49 50 static uint8_t *get_pixel8(uint16_t h, uint16_t v) 51 { 52 return (uint8_t *) (lcd_base + v * get_line_length()) + h; 53 } 54 55 static uint8_t *get_pixel4(uint16_t h, uint16_t v) 56 { 57 return (uint8_t *) (lcd_base + v * get_line_length()) + h / 2; 58 } 59 60 static inline uint16_t div(uint16_t num, uint16_t denom, uint16_t scale) 61 { 62 return (scale * num) / denom; 63 } 64 65 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) 66 { 67 return (div(r, 255, rmax) << rshift) | (div(g, 255, gmax) << gshift) | (div(b, 255, bmax) << bshift); 68 } 69 70 uint32_t get_bitmap_value(uint16_t x, uint32_t value) 71 { 72 return (value >> (((panel_info.jz_fb->w - 1 - x) * 32) / panel_info.jz_fb->w)) % 2 ? 0xffffffff : 0; 73 } 74 75 static void get_colour(uint16_t h, uint16_t v, uint8_t rgb[], uint16_t pixel_type) 76 { 77 uint16_t v_max = panel_info.jz_fb->h - 1; 78 uint16_t h_max = panel_info.jz_fb->w - 1; 79 80 rgb[(pixel_type - 1) % 3] = div(h, h_max, 255); 81 rgb[pixel_type % 3] = div(v, v_max, 255); 82 rgb[(pixel_type + 1) % 3] = (rgb[0] + rgb[1]) / 2; 83 } 84 85 static void set_pixel32(uint16_t h, uint16_t v, uint32_t value) 86 { 87 uint32_t *pix = get_pixel32(h, v); 88 *pix = value; 89 } 90 91 static void set_pixel16_565(uint16_t h, uint16_t v, uint32_t value) 92 { 93 uint16_t *pix = get_pixel16(h, v); 94 *pix = (uint16_t) value; 95 } 96 97 static void set_pixel8(uint16_t h, uint16_t v, uint32_t value) 98 { 99 uint8_t *pix = get_pixel8(h, v); 100 *pix = (uint8_t) value; 101 } 102 103 static void set_pixel4(uint16_t h, uint16_t v, uint32_t value) 104 { 105 uint8_t *pix = get_pixel4(h, v); 106 uint8_t mask = h & 1 ? 0xf0 : 0x0f; 107 *pix = (*pix & mask) | ((uint8_t) value); 108 } 109 110 void set_pixel(uint16_t h, uint16_t v, uint32_t value) 111 { 112 switch (panel_info.jz_fb->bpp) 113 { 114 case 32: 115 set_pixel32(h, v, value); 116 break; 117 118 case 8: 119 set_pixel8(h, v, value); 120 break; 121 122 case 4: 123 set_pixel4(h, v, value); 124 break; 125 126 case 16: 127 default: 128 set_pixel16_565(h, v, value); 129 break; 130 } 131 } 132 static void test_pixel32(uint16_t h, uint16_t v, uint16_t pixel_type) 133 { 134 uint8_t rgb[3]; 135 136 get_colour(h, v, rgb, pixel_type); 137 set_pixel32(h, v, pixel(rgb[0], rgb[1], rgb[2], 255, 255, 255, 16, 8, 0) | get_bitmap_value(h, pixel_type)); 138 } 139 140 static void test_pixel16_565(uint16_t h, uint16_t v, uint16_t pixel_type) 141 { 142 uint8_t rgb[3]; 143 144 get_colour(h, v, rgb, pixel_type); 145 set_pixel16_565(h, v, pixel(rgb[0], rgb[1], rgb[2], 31, 63, 31, 11, 5, 0) | get_bitmap_value(h, pixel_type)); 146 } 147 148 static void test_pixel8(uint16_t h, uint16_t v, uint16_t pixel_type) 149 { 150 uint8_t rgb[3]; 151 152 get_colour(h, v, rgb, pixel_type); 153 set_pixel8(h, v, pixel(rgb[0], rgb[1], rgb[2], 7, 7, 3, 5, 2, 0) | get_bitmap_value(h, pixel_type)); 154 } 155 156 static void test_pixel4(uint16_t h, uint16_t v, uint16_t pixel_type) 157 { 158 uint8_t rgb[3]; 159 160 get_colour(h, v, rgb, pixel_type); 161 set_pixel4(h, v, pixel(rgb[0], rgb[1], rgb[2], 1, 2, 1, 3, 1, 0) | get_bitmap_value(h, pixel_type)); 162 } 163 164 void test_pixel(uint16_t h, uint16_t v, uint16_t pixel_type) 165 { 166 switch (panel_info.jz_fb->bpp) 167 { 168 case 32: 169 test_pixel32(h, v, pixel_type); 170 break; 171 172 case 8: 173 test_pixel8(h, v, pixel_type); 174 break; 175 176 case 4: 177 test_pixel4(h, v, pixel_type); 178 break; 179 180 case 16: 181 default: 182 test_pixel16_565(h, v, pixel_type); 183 break; 184 } 185 } 186 187 void clear_pixel32(uint16_t h, uint16_t v) 188 { 189 uint32_t *pix = get_pixel32(h, v); 190 *pix = 0; 191 } 192 193 void clear_pixel16(uint16_t h, uint16_t v) 194 { 195 uint16_t *pix = get_pixel16(h, v); 196 *pix = 0; 197 } 198 199 void clear_pixel8(uint16_t h, uint16_t v) 200 { 201 uint8_t *pix = get_pixel8(h, v); 202 *pix = 0; 203 } 204 205 void clear_pixel4(uint16_t h, uint16_t v) 206 { 207 uint8_t *pix = get_pixel4(h, v); 208 uint8_t mask = h & 1 ? 0xf0 : 0x0f; 209 *pix = *pix & mask; 210 } 211 212 void clear_pixel(uint16_t h, uint16_t v) 213 { 214 switch (panel_info.jz_fb->bpp) 215 { 216 case 32: 217 clear_pixel32(h, v); 218 break; 219 220 case 8: 221 clear_pixel8(h, v); 222 break; 223 224 case 4: 225 clear_pixel4(h, v); 226 break; 227 228 case 16: 229 default: 230 clear_pixel16(h, v); 231 break; 232 } 233 } 234 235 void test_pattern() 236 { 237 uint16_t v_max = panel_info.jz_fb->h; 238 uint16_t h_max = panel_info.jz_fb->w; 239 uint16_t v, h; 240 241 for (v = 0; v < v_max; v += 1) { 242 for (h = 0; h < h_max; h += 1) { 243 test_pixel(h, v, 1); 244 } 245 } 246 } 247 248 void lcd_clear(uint32_t lcd_base) 249 { 250 uint16_t v_max = panel_info.jz_fb->h; 251 uint16_t h_max = panel_info.jz_fb->w; 252 uint16_t v, h; 253 uint32_t *pix = (uint32_t *)lcd_base; 254 255 for (v = 0; v < v_max; v += 1) { 256 for (h = 0; h < h_max; h += 1) { 257 *pix++ = 0; 258 } 259 } 260 } 261 262 263 264 /* LCD initialisation. */ 265 266 void lcd_set_timing(vidinfo_t *vid) 267 { 268 uint32_t pclk = jz4740_lcd_get_pixel_clock(vid); 269 270 jz4740_cpm_stop_lcd((void *) CPM_BASE); 271 jz4740_cpm_set_lcd_frequencies((void *) CPM_BASE, pclk, 3); 272 jz4740_cpm_start_lcd((void *) CPM_BASE); 273 udelay(1000); 274 } 275 276 static void lcd_set_bpp(uint8_t bpp) 277 { 278 jz4740_lcd_set_bpp(bpp, &panel_info); 279 } 280 281 static void lcd_enable() 282 { 283 jz4740_lcd_enable(&panel_info); 284 } 285 286 static void lcd_disable() 287 { 288 jz4740_lcd_disable(&panel_info); 289 } 290 291 static void lcd_quick_disable() 292 { 293 jz4740_lcd_quick_disable(&panel_info); 294 } 295 296 uint32_t lcd_ctrl_init() 297 { 298 vidinfo_t *vid = &panel_info; 299 struct jz_mem_info *fbi = &vid->jz_mem; 300 void *fb_vaddr; 301 302 /* Start from the top of memory and obtain the framebuffer region. */ 303 304 fb_vaddr = (void *) (get_memory_size() - jz4740_lcd_get_total_size(vid)); 305 306 jz4740_lcd_ctrl_init((void *) LCD_BASE, fb_vaddr, vid); 307 flush_cache_all(); 308 jz4740_lcd_hw_init(vid); 309 lcd_set_timing(vid); 310 jz4740_lcd_dma_init(vid); 311 312 return fbi->screen; 313 } 314 315 void lcd_init() 316 { 317 lcd_display_pin_init(); 318 lcd_display_on(); 319 320 /* Initialise the member here since the address is otherwise invalid. */ 321 322 panel_info.jz_fb = (struct jzfb_info *) ((uint32_t) panel_info.jz_fb & ~0x80000000); 323 324 lcd_base = lcd_ctrl_init(); 325 lcd_clear(lcd_base); 326 lcd_enable(); 327 }