paul@0 | 1 | /* |
paul@0 | 2 | * LCD peripheral support for the JZ4740 and related SoCs. |
paul@0 | 3 | * |
paul@0 | 4 | * Copyright (C) Xiangfu Liu <xiangfu@sharism.cc> |
paul@64 | 5 | * Copyright (C) 2015, 2016, 2017, 2018, |
paul@64 | 6 | * 2020 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 7 | * |
paul@0 | 8 | * This program is free software; you can redistribute it and/or |
paul@0 | 9 | * modify it under the terms of the GNU General Public License as |
paul@0 | 10 | * published by the Free Software Foundation; either version 2 of |
paul@0 | 11 | * the License, or (at your option) any later version. |
paul@0 | 12 | * |
paul@0 | 13 | * This program is distributed in the hope that it will be useful, |
paul@0 | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 16 | * GNU General Public License for more details. |
paul@0 | 17 | * |
paul@0 | 18 | * You should have received a copy of the GNU General Public License |
paul@0 | 19 | * along with this program; if not, write to the Free Software |
paul@0 | 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@0 | 21 | * Boston, MA 02110-1301, USA |
paul@0 | 22 | */ |
paul@0 | 23 | |
paul@0 | 24 | #include <l4/devices/hw_mmio_register_block.h> |
paul@0 | 25 | #include <l4/sys/cache.h> |
paul@69 | 26 | #include <l4/sys/irq.h> |
paul@0 | 27 | #include <l4/sys/types.h> |
paul@69 | 28 | #include <l4/util/util.h> |
paul@0 | 29 | |
paul@0 | 30 | #include "lcd-jz4740.h" |
paul@0 | 31 | #include "lcd-jz4740-config.h" |
paul@75 | 32 | #include "lcd-jz4740-regs.h" |
paul@0 | 33 | |
paul@0 | 34 | #include <stdint.h> |
paul@0 | 35 | |
paul@0 | 36 | |
paul@0 | 37 | |
paul@0 | 38 | // Utility functions. |
paul@0 | 39 | |
paul@0 | 40 | // Round values up according to the resolution. |
paul@0 | 41 | |
paul@0 | 42 | static uint32_t align(uint32_t value, uint32_t resolution) |
paul@0 | 43 | { |
paul@0 | 44 | return (value + (resolution - 1)) & ~(resolution - 1); |
paul@0 | 45 | } |
paul@0 | 46 | |
paul@0 | 47 | // Value pair encoding. |
paul@0 | 48 | |
paul@0 | 49 | static uint32_t encode_pair(uint32_t start, uint32_t end) |
paul@0 | 50 | { |
paul@0 | 51 | return (start << Value_first) | (end << Value_second); |
paul@0 | 52 | } |
paul@0 | 53 | |
paul@0 | 54 | // RGB conversions. |
paul@0 | 55 | |
paul@0 | 56 | static uint16_t rgb8_to_rgb16(uint8_t rgb) |
paul@0 | 57 | { |
paul@0 | 58 | return ((((rgb & 0xe0) >> 5) * 4) << 11) | ((((rgb & 0x1c) >> 2) * 9) << 6) | ((rgb & 0x03) * 10); |
paul@0 | 59 | } |
paul@0 | 60 | |
paul@0 | 61 | static uint16_t rgb4_to_rgb16(uint8_t rgb) |
paul@0 | 62 | { |
paul@0 | 63 | return ((((rgb & 8) >> 3) * 0x1f) << 11) | ((((rgb & 6) >> 1) * 0x15) << 5) | ((rgb & 1) * 0x1f); |
paul@0 | 64 | } |
paul@0 | 65 | |
paul@0 | 66 | |
paul@0 | 67 | |
paul@0 | 68 | |
paul@0 | 69 | // If implemented as a Hw::Device, various properties would be |
paul@0 | 70 | // initialised in the constructor and obtained from the device tree |
paul@0 | 71 | // definitions. |
paul@0 | 72 | |
paul@0 | 73 | Lcd_jz4740_chip::Lcd_jz4740_chip(l4_addr_t addr, Jz4740_lcd_panel *panel) |
paul@0 | 74 | : _panel(panel) |
paul@0 | 75 | { |
paul@0 | 76 | _regs = new Hw::Mmio_register_block<32>(addr); |
paul@75 | 77 | _burst_size = 16; // 16-word burst size |
paul@0 | 78 | } |
paul@0 | 79 | |
paul@0 | 80 | struct Jz4740_lcd_panel * |
paul@0 | 81 | Lcd_jz4740_chip::get_panel() |
paul@0 | 82 | { |
paul@0 | 83 | return _panel; |
paul@0 | 84 | } |
paul@0 | 85 | |
paul@0 | 86 | void |
paul@0 | 87 | Lcd_jz4740_chip::disable() |
paul@0 | 88 | { |
paul@0 | 89 | // Set the disable bit for normal shutdown. |
paul@0 | 90 | |
paul@69 | 91 | _regs[Lcd_control] = _regs[Lcd_control] | (1U << Control_disable); |
paul@0 | 92 | } |
paul@0 | 93 | |
paul@0 | 94 | void |
paul@0 | 95 | Lcd_jz4740_chip::disable_quick() |
paul@0 | 96 | { |
paul@0 | 97 | // Clear the enable bit for quick shutdown. |
paul@0 | 98 | |
paul@69 | 99 | _regs[Lcd_control] = _regs[Lcd_control] & ~(1U << Control_enable); |
paul@0 | 100 | } |
paul@0 | 101 | |
paul@0 | 102 | void |
paul@0 | 103 | Lcd_jz4740_chip::enable() |
paul@0 | 104 | { |
paul@0 | 105 | // Clear the disable bit and set the enable bit. |
paul@0 | 106 | |
paul@69 | 107 | _regs[Lcd_status] = 0; |
paul@69 | 108 | _regs[Lcd_control] = (_regs[Lcd_control] & ~(1U << Control_disable)) | (1U << Control_enable); |
paul@69 | 109 | } |
paul@69 | 110 | |
paul@69 | 111 | bool |
paul@69 | 112 | Lcd_jz4740_chip::enabled() |
paul@69 | 113 | { |
paul@69 | 114 | return !(_regs[Lcd_status] & (1U << Status_disabled)); |
paul@0 | 115 | } |
paul@0 | 116 | |
paul@0 | 117 | // Calculate and return the pixel clock frequency. |
paul@0 | 118 | |
paul@0 | 119 | int |
paul@0 | 120 | Lcd_jz4740_chip::get_pixel_clock() |
paul@0 | 121 | { |
paul@0 | 122 | int pclk, multiplier; |
paul@0 | 123 | |
paul@0 | 124 | // Serial mode: 3 pixel clock cycles per pixel (one per channel). |
paul@0 | 125 | // Parallel mode: 1 pixel clock cycle per pixel. |
paul@0 | 126 | |
paul@0 | 127 | multiplier = have_serial_tft() ? 3 : 1; |
paul@0 | 128 | |
paul@0 | 129 | // Derive pixel clock rate from frame rate. |
paul@0 | 130 | // This multiplies the number of pixel periods in a line by the number of |
paul@0 | 131 | // lines in a frame, thus obtaining the number of such periods in a frame. |
paul@0 | 132 | // Multiplying this result with the frame rate yields the pixel frequency. |
paul@0 | 133 | |
paul@0 | 134 | pclk = _panel->frame_rate * |
paul@0 | 135 | (_panel->width * multiplier + |
paul@0 | 136 | _panel->hsync + _panel->line_start + _panel->line_end) * |
paul@0 | 137 | (_panel->height + |
paul@0 | 138 | _panel->vsync + _panel->frame_start + _panel->frame_end); |
paul@0 | 139 | |
paul@0 | 140 | // STN panel adjustments. |
paul@0 | 141 | |
paul@0 | 142 | if (have_stn_panel()) |
paul@0 | 143 | { |
paul@0 | 144 | // Colour STN panels apparently need to be driven at three times the rate. |
paul@0 | 145 | |
paul@0 | 146 | if (have_colour_stn()) pclk = (pclk * 3); |
paul@0 | 147 | |
paul@0 | 148 | // Reduce the rate according to the width of the STN connection. |
paul@0 | 149 | // Since the pins setting employs log2(pins), a shift by this value is |
paul@0 | 150 | // equivalent to a division by the number of pins. |
paul@0 | 151 | |
paul@0 | 152 | pclk = pclk >> ((_panel->config & Config_stn_pins_mask) >> Jz4740_lcd_config_stn_pins); |
paul@0 | 153 | |
paul@0 | 154 | // Divide the rate by the number of panels. |
paul@0 | 155 | |
paul@0 | 156 | pclk /= get_panels(); |
paul@0 | 157 | } |
paul@0 | 158 | |
paul@0 | 159 | return pclk; |
paul@0 | 160 | } |
paul@0 | 161 | |
paul@0 | 162 | |
paul@0 | 163 | |
paul@0 | 164 | // Return the panel mode. |
paul@0 | 165 | |
paul@0 | 166 | uint32_t |
paul@0 | 167 | Lcd_jz4740_chip::_mode() |
paul@0 | 168 | { |
paul@0 | 169 | return _panel->config & Config_mode_mask; |
paul@0 | 170 | } |
paul@0 | 171 | |
paul@0 | 172 | // Return the number of panels available. |
paul@0 | 173 | |
paul@0 | 174 | int |
paul@0 | 175 | Lcd_jz4740_chip::get_panels() |
paul@0 | 176 | { |
paul@0 | 177 | uint32_t mode = _mode(); |
paul@0 | 178 | |
paul@0 | 179 | return (mode == Jz4740_lcd_mode_stn_dual_colour) || |
paul@0 | 180 | (mode == Jz4740_lcd_mode_stn_dual_mono) ? 2 : 1; |
paul@0 | 181 | } |
paul@0 | 182 | |
paul@0 | 183 | // Return whether the panel is STN. |
paul@0 | 184 | |
paul@0 | 185 | int |
paul@0 | 186 | Lcd_jz4740_chip::have_stn_panel() |
paul@0 | 187 | { |
paul@0 | 188 | uint32_t mode = _mode(); |
paul@0 | 189 | |
paul@0 | 190 | return ((mode == Jz4740_lcd_mode_stn_single_colour) || |
paul@0 | 191 | (mode == Jz4740_lcd_mode_stn_dual_colour) || |
paul@0 | 192 | (mode == Jz4740_lcd_mode_stn_single_mono) || |
paul@0 | 193 | (mode == Jz4740_lcd_mode_stn_dual_mono)); |
paul@0 | 194 | } |
paul@0 | 195 | |
paul@0 | 196 | // Return whether the panel is colour STN. |
paul@0 | 197 | |
paul@0 | 198 | int |
paul@0 | 199 | Lcd_jz4740_chip::have_colour_stn() |
paul@0 | 200 | { |
paul@0 | 201 | uint32_t mode = _mode(); |
paul@0 | 202 | |
paul@0 | 203 | return ((mode == Jz4740_lcd_mode_stn_single_colour) || |
paul@0 | 204 | (mode == Jz4740_lcd_mode_stn_dual_colour)); |
paul@0 | 205 | } |
paul@0 | 206 | |
paul@0 | 207 | // Return whether the panel is colour STN. |
paul@0 | 208 | |
paul@0 | 209 | int |
paul@0 | 210 | Lcd_jz4740_chip::have_serial_tft() |
paul@0 | 211 | { |
paul@0 | 212 | return _mode() == Jz4740_lcd_mode_tft_serial; |
paul@0 | 213 | } |
paul@0 | 214 | |
paul@0 | 215 | |
paul@0 | 216 | |
paul@69 | 217 | // Return the pixel memory size in bits. |
paul@69 | 218 | |
paul@69 | 219 | l4_size_t |
paul@69 | 220 | Lcd_jz4740_chip::get_pixel_size() |
paul@69 | 221 | { |
paul@69 | 222 | if (_panel->bpp > 16) |
paul@69 | 223 | return 32; |
paul@69 | 224 | else if (_panel->bpp > 8) |
paul@69 | 225 | return 16; |
paul@69 | 226 | else |
paul@69 | 227 | return _panel->bpp; |
paul@69 | 228 | } |
paul@69 | 229 | |
paul@0 | 230 | // Return the line memory size. |
paul@0 | 231 | |
paul@0 | 232 | l4_size_t |
paul@0 | 233 | Lcd_jz4740_chip::get_line_size() |
paul@0 | 234 | { |
paul@0 | 235 | // Lines must be aligned to a word boundary. |
paul@0 | 236 | |
paul@69 | 237 | return align((_panel->width * get_pixel_size()) / 8, sizeof(uint32_t)); |
paul@0 | 238 | } |
paul@0 | 239 | |
paul@0 | 240 | // Return the screen memory size. |
paul@0 | 241 | |
paul@0 | 242 | l4_size_t |
paul@0 | 243 | Lcd_jz4740_chip::get_screen_size() |
paul@0 | 244 | { |
paul@0 | 245 | return get_line_size() * _panel->height; |
paul@0 | 246 | } |
paul@0 | 247 | |
paul@0 | 248 | // Return the aligned size for the DMA transfer. |
paul@0 | 249 | |
paul@0 | 250 | l4_size_t |
paul@0 | 251 | Lcd_jz4740_chip::get_aligned_size() |
paul@0 | 252 | { |
paul@0 | 253 | return align(get_screen_size(), _burst_size * sizeof(uint32_t)); |
paul@0 | 254 | } |
paul@0 | 255 | |
paul@0 | 256 | // Return the size of the palette. |
paul@0 | 257 | |
paul@0 | 258 | l4_size_t |
paul@0 | 259 | Lcd_jz4740_chip::get_palette_size() |
paul@0 | 260 | { |
paul@0 | 261 | // No palette for modes with more than eight bits per pixel. |
paul@0 | 262 | |
paul@0 | 263 | if (_panel->bpp > 8) return 0; |
paul@0 | 264 | |
paul@0 | 265 | // Get the size of a collection of two-byte entries, one per colour. |
paul@0 | 266 | |
paul@69 | 267 | return (1U << (_panel->bpp)) * sizeof(uint16_t); |
paul@0 | 268 | } |
paul@0 | 269 | |
paul@0 | 270 | // Return the aligned size of the palette for the DMA transfer. |
paul@0 | 271 | |
paul@0 | 272 | l4_size_t |
paul@0 | 273 | Lcd_jz4740_chip::get_aligned_palette_size() |
paul@0 | 274 | { |
paul@0 | 275 | return align(get_palette_size(), _burst_size * sizeof(uint32_t)); |
paul@0 | 276 | } |
paul@0 | 277 | |
paul@0 | 278 | // Return the total memory requirements of the framebuffers and palette. |
paul@0 | 279 | |
paul@0 | 280 | l4_size_t |
paul@0 | 281 | Lcd_jz4740_chip::get_total_size() |
paul@0 | 282 | { |
paul@0 | 283 | return get_aligned_size() * get_panels() + get_aligned_palette_size(); |
paul@0 | 284 | } |
paul@0 | 285 | |
paul@0 | 286 | // Return the total memory requirements of any DMA descriptors. |
paul@0 | 287 | |
paul@0 | 288 | l4_size_t |
paul@0 | 289 | Lcd_jz4740_chip::get_descriptors_size() |
paul@0 | 290 | { |
paul@0 | 291 | return 3 * sizeof(struct Jz4740_lcd_descriptor); |
paul@0 | 292 | } |
paul@0 | 293 | |
paul@0 | 294 | |
paul@0 | 295 | |
paul@0 | 296 | // Functions returning addresses of each data region. |
paul@0 | 297 | // The base parameter permits the retrieval of virtual or physical addresses. |
paul@0 | 298 | |
paul@0 | 299 | l4_addr_t |
paul@0 | 300 | Lcd_jz4740_chip::get_palette(l4_addr_t base) |
paul@0 | 301 | { |
paul@0 | 302 | // Use memory at the end of the allocated region for the palette. |
paul@0 | 303 | |
paul@0 | 304 | return base + (get_panels() * get_aligned_size()) - get_aligned_palette_size(); |
paul@0 | 305 | } |
paul@0 | 306 | |
paul@0 | 307 | l4_addr_t |
paul@0 | 308 | Lcd_jz4740_chip::get_framebuffer(int panel, l4_addr_t base) |
paul@0 | 309 | { |
paul@0 | 310 | // Framebuffers for panels are allocated at the start of the region. |
paul@0 | 311 | |
paul@0 | 312 | return base + (panel * get_aligned_size()); |
paul@0 | 313 | } |
paul@0 | 314 | |
paul@0 | 315 | |
paul@0 | 316 | |
paul@0 | 317 | // Palette initialisation. |
paul@0 | 318 | |
paul@0 | 319 | void |
paul@0 | 320 | Lcd_jz4740_chip::init_palette(l4_addr_t palette) |
paul@0 | 321 | { |
paul@69 | 322 | uint8_t colours = 1U << (_panel->bpp); |
paul@0 | 323 | uint16_t *entry = (uint16_t *) palette; |
paul@0 | 324 | uint16_t *end = entry + colours; |
paul@0 | 325 | uint8_t value = 0; |
paul@0 | 326 | |
paul@0 | 327 | while (entry < end) |
paul@0 | 328 | { |
paul@0 | 329 | switch (_panel->bpp) |
paul@0 | 330 | { |
paul@0 | 331 | case 4: |
paul@0 | 332 | *entry = rgb4_to_rgb16(value); |
paul@0 | 333 | break; |
paul@0 | 334 | |
paul@0 | 335 | case 8: |
paul@0 | 336 | *entry = rgb8_to_rgb16(value); |
paul@0 | 337 | break; |
paul@0 | 338 | |
paul@0 | 339 | default: |
paul@0 | 340 | break; |
paul@0 | 341 | } |
paul@0 | 342 | |
paul@0 | 343 | value++; |
paul@0 | 344 | entry++; |
paul@0 | 345 | } |
paul@0 | 346 | } |
paul@0 | 347 | |
paul@0 | 348 | |
paul@0 | 349 | |
paul@0 | 350 | // Return colour depth control value. |
paul@0 | 351 | // NOTE: Not supporting JZ4780 options. |
paul@0 | 352 | |
paul@0 | 353 | uint32_t |
paul@0 | 354 | Lcd_jz4740_chip::_control_bpp() |
paul@0 | 355 | { |
paul@0 | 356 | switch (_panel->bpp) |
paul@0 | 357 | { |
paul@0 | 358 | case 1: return Control_bpp_1bpp; |
paul@0 | 359 | case 2: return Control_bpp_2bpp; |
paul@0 | 360 | case 3 ... 4: return Control_bpp_4bpp; |
paul@0 | 361 | case 5 ... 8: return Control_bpp_8bpp; |
paul@0 | 362 | case 9 ... 15: return Control_bpp_15bpp | (Rgb_mode_555 << Control_rgb_mode); |
paul@0 | 363 | case 17 ... 18: return Control_bpp_18bpp; |
paul@0 | 364 | case 19 ... 32: return Control_bpp_24bpp; |
paul@0 | 365 | case 16: |
paul@0 | 366 | default: return Control_bpp_16bpp; |
paul@0 | 367 | } |
paul@0 | 368 | } |
paul@0 | 369 | |
paul@0 | 370 | // Return a panel-related control value. |
paul@0 | 371 | |
paul@0 | 372 | uint32_t |
paul@0 | 373 | Lcd_jz4740_chip::_control_panel() |
paul@0 | 374 | { |
paul@0 | 375 | if (have_stn_panel()) |
paul@69 | 376 | return _control_stn_frc() << Control_frc_algorithm; |
paul@0 | 377 | else |
paul@0 | 378 | return 0; |
paul@0 | 379 | } |
paul@0 | 380 | |
paul@0 | 381 | // Return a STN-related control value. |
paul@0 | 382 | |
paul@0 | 383 | uint32_t |
paul@0 | 384 | Lcd_jz4740_chip::_control_stn_frc() |
paul@0 | 385 | { |
paul@0 | 386 | if (_panel->bpp <= 2) |
paul@0 | 387 | return Frc_greyscales_2; |
paul@0 | 388 | if (_panel->bpp <= 4) |
paul@0 | 389 | return Frc_greyscales_4; |
paul@0 | 390 | return Frc_greyscales_16; |
paul@0 | 391 | } |
paul@0 | 392 | |
paul@0 | 393 | // Return a transfer-related control value. |
paul@0 | 394 | |
paul@0 | 395 | uint32_t |
paul@0 | 396 | Lcd_jz4740_chip::_control_transfer() |
paul@0 | 397 | { |
paul@0 | 398 | uint32_t length; |
paul@0 | 399 | |
paul@0 | 400 | switch (_burst_size) |
paul@0 | 401 | { |
paul@0 | 402 | case 4: length = Burst_length_4; break; |
paul@0 | 403 | case 8: length = Burst_length_8; break; |
paul@0 | 404 | case 32: length = Burst_length_32; break; |
paul@0 | 405 | case 64: length = Burst_length_64; break; |
paul@0 | 406 | case 16: |
paul@0 | 407 | default: length = Burst_length_16; break; |
paul@0 | 408 | } |
paul@0 | 409 | |
paul@69 | 410 | return (length << Control_burst_length) | (1U << Control_out_underrun); |
paul@69 | 411 | } |
paul@69 | 412 | |
paul@69 | 413 | // Return an interrupt-related control value. |
paul@69 | 414 | |
paul@69 | 415 | uint32_t |
paul@69 | 416 | Lcd_jz4740_chip::_control_irq() |
paul@69 | 417 | { |
paul@73 | 418 | return ((_irq_conditions & Lcd_irq_frame_start) ? (1U << Control_frame_start_irq_enable) : 0) | |
paul@69 | 419 | ((_irq_conditions & Lcd_irq_frame_end) ? (1U << Control_frame_end_irq_enable) : 0); |
paul@69 | 420 | } |
paul@69 | 421 | |
paul@69 | 422 | // Return an interrupt-related command value. |
paul@69 | 423 | |
paul@69 | 424 | uint32_t |
paul@69 | 425 | Lcd_jz4740_chip::_command_irq() |
paul@69 | 426 | { |
paul@69 | 427 | return ((_irq_conditions & Lcd_irq_frame_start) ? (1U << Command_frame_start_irq) : 0) | |
paul@69 | 428 | ((_irq_conditions & Lcd_irq_frame_end) ? (1U << Command_frame_end_irq) : 0); |
paul@69 | 429 | } |
paul@69 | 430 | |
paul@69 | 431 | // Return an interrupt-related status value. |
paul@69 | 432 | |
paul@69 | 433 | uint32_t |
paul@69 | 434 | Lcd_jz4740_chip::_status_irq() |
paul@69 | 435 | { |
paul@73 | 436 | return ((_irq_conditions & Lcd_irq_frame_start) ? (1U << Status_frame_start_irq) : 0) | |
paul@69 | 437 | ((_irq_conditions & Lcd_irq_frame_end) ? (1U << Status_frame_end_irq) : 0); |
paul@69 | 438 | } |
paul@69 | 439 | |
paul@0 | 440 | // STN panel-specific initialisation. |
paul@0 | 441 | |
paul@0 | 442 | void |
paul@0 | 443 | Lcd_jz4740_chip::_init_stn() |
paul@0 | 444 | { |
paul@0 | 445 | // Divide the height by the number of panels. |
paul@0 | 446 | |
paul@0 | 447 | uint32_t height = _panel->height / get_panels(); |
paul@0 | 448 | |
paul@0 | 449 | // Since the value is log2(pins), 1 << value yields the number of pins. |
paul@0 | 450 | |
paul@0 | 451 | int pins = 1 << ((_panel->config & Config_stn_pins_mask) >> Jz4740_lcd_config_stn_pins); |
paul@0 | 452 | |
paul@0 | 453 | // Round parameters up to a multiple of the number of pins. |
paul@0 | 454 | |
paul@0 | 455 | uint32_t hsync = align(_panel->hsync, pins); |
paul@0 | 456 | uint32_t line_start = align(_panel->line_start, pins); |
paul@0 | 457 | uint32_t line_end = align(_panel->line_end, pins); |
paul@0 | 458 | |
paul@0 | 459 | // Define the start and end positions of visible data on a line and in a frame. |
paul@0 | 460 | // Visible frame data is anchored at line zero, with the start region |
paul@0 | 461 | // preceding this line (and thus appearing at the end of the preceding frame). |
paul@0 | 462 | |
paul@0 | 463 | uint32_t line_start_pos = line_start; |
paul@0 | 464 | uint32_t line_end_pos = line_start_pos + _panel->width; |
paul@0 | 465 | uint32_t frame_start_pos = 0; |
paul@0 | 466 | uint32_t frame_end_pos = frame_start_pos + height; |
paul@0 | 467 | |
paul@0 | 468 | // Define sync pulse locations, with hsync occurring after the visible data. |
paul@0 | 469 | |
paul@0 | 470 | _regs[Lcd_hsync] = encode_pair(line_end_pos, line_end_pos + hsync); |
paul@0 | 471 | _regs[Lcd_vsync] = encode_pair(0, _panel->vsync); |
paul@0 | 472 | |
paul@0 | 473 | // Set the display area and limits. |
paul@0 | 474 | |
paul@69 | 475 | _regs[Virtual_area] = encode_pair(line_end_pos + hsync + line_end, |
paul@69 | 476 | frame_end_pos + _panel->vsync + _panel->frame_end + _panel->frame_start); |
paul@0 | 477 | |
paul@0 | 478 | _regs[Display_hlimits] = encode_pair(line_start_pos, line_end_pos); |
paul@0 | 479 | _regs[Display_vlimits] = encode_pair(frame_start_pos, frame_end_pos); |
paul@0 | 480 | |
paul@0 | 481 | // Set the AC bias signal. |
paul@0 | 482 | |
paul@0 | 483 | _regs[Lcd_ps] = encode_pair(0, _panel->frame_start + height + _panel->vsync + _panel->frame_end); |
paul@0 | 484 | } |
paul@0 | 485 | |
paul@0 | 486 | // TFT panel-specific initialisation. |
paul@0 | 487 | |
paul@0 | 488 | void |
paul@0 | 489 | Lcd_jz4740_chip::_init_tft() |
paul@0 | 490 | { |
paul@0 | 491 | // Define the start and end positions of visible data on a line and in a frame. |
paul@0 | 492 | |
paul@0 | 493 | uint32_t line_start_pos = _panel->line_start + _panel->hsync; |
paul@0 | 494 | uint32_t line_end_pos = line_start_pos + _panel->width; |
paul@0 | 495 | uint32_t frame_start_pos = _panel->frame_start + _panel->vsync; |
paul@0 | 496 | uint32_t frame_end_pos = frame_start_pos + _panel->height; |
paul@0 | 497 | |
paul@0 | 498 | // Define sync pulse locations, with pulses appearing before visible data. |
paul@0 | 499 | |
paul@0 | 500 | _regs[Lcd_hsync] = encode_pair(0, _panel->hsync); |
paul@0 | 501 | _regs[Lcd_vsync] = encode_pair(0, _panel->vsync); |
paul@0 | 502 | |
paul@0 | 503 | // Set the display area and limits. |
paul@0 | 504 | |
paul@69 | 505 | _regs[Virtual_area] = encode_pair(line_end_pos + _panel->line_end, |
paul@69 | 506 | frame_end_pos + _panel->frame_end); |
paul@0 | 507 | |
paul@0 | 508 | _regs[Display_hlimits] = encode_pair(line_start_pos, line_end_pos); |
paul@0 | 509 | _regs[Display_vlimits] = encode_pair(frame_start_pos, frame_end_pos); |
paul@0 | 510 | } |
paul@0 | 511 | |
paul@0 | 512 | // Initialise the panel. |
paul@0 | 513 | // NOTE: Only generic STN and TFT panels are supported. |
paul@0 | 514 | |
paul@0 | 515 | void |
paul@0 | 516 | Lcd_jz4740_chip::_init_panel() |
paul@0 | 517 | { |
paul@0 | 518 | if (have_stn_panel()) |
paul@0 | 519 | _init_stn(); |
paul@0 | 520 | else |
paul@0 | 521 | switch (_mode()) |
paul@0 | 522 | { |
paul@0 | 523 | case Jz4740_lcd_mode_tft_generic: |
paul@0 | 524 | case Jz4740_lcd_mode_tft_casio: |
paul@0 | 525 | case Jz4740_lcd_mode_tft_serial: _init_tft(); |
paul@0 | 526 | |
paul@0 | 527 | default: break; |
paul@0 | 528 | } |
paul@0 | 529 | } |
paul@0 | 530 | |
paul@0 | 531 | // Initialise a DMA descriptor. |
paul@0 | 532 | |
paul@0 | 533 | void |
paul@0 | 534 | Lcd_jz4740_chip::_set_descriptor(struct Jz4740_lcd_descriptor &desc, |
paul@0 | 535 | l4_addr_t source, l4_size_t size, |
paul@0 | 536 | struct Jz4740_lcd_descriptor *next, |
paul@75 | 537 | uint32_t flags) |
paul@0 | 538 | { |
paul@0 | 539 | // In the command, indicate the number of words from the source for transfer. |
paul@0 | 540 | |
paul@0 | 541 | desc.next = next; |
paul@75 | 542 | desc.source = source; |
paul@69 | 543 | desc.identifier = source; |
paul@69 | 544 | desc.command = ((size / sizeof(uint32_t)) & Command_buffer_length_mask) | |
paul@69 | 545 | flags; |
paul@0 | 546 | } |
paul@0 | 547 | |
paul@0 | 548 | // Initialise the LCD controller with the memory, panel and framebuffer details. |
paul@0 | 549 | // Any palette must be initialised separately using get_palette and init_palette. |
paul@0 | 550 | |
paul@0 | 551 | void |
paul@0 | 552 | Lcd_jz4740_chip::config(struct Jz4740_lcd_descriptor *desc_vaddr, |
paul@0 | 553 | struct Jz4740_lcd_descriptor *desc_paddr, |
paul@0 | 554 | l4_addr_t fb_paddr) |
paul@0 | 555 | { |
paul@0 | 556 | int have_palette = (_panel->bpp <= 8); |
paul@0 | 557 | |
paul@0 | 558 | // Provide the first framebuffer descriptor in single and dual modes. |
paul@0 | 559 | // Flip back and forth between any palette and the framebuffer. |
paul@0 | 560 | |
paul@0 | 561 | _set_descriptor(desc_vaddr[0], get_framebuffer(0, fb_paddr), |
paul@69 | 562 | get_aligned_size(), |
paul@69 | 563 | have_palette ? desc_paddr + 2 : desc_paddr, |
paul@69 | 564 | _command_irq()); |
paul@0 | 565 | |
paul@0 | 566 | // Provide the second framebuffer descriptor only in dual-panel mode. |
paul@0 | 567 | // Only employ this descriptor in the second DMA channel. |
paul@0 | 568 | |
paul@75 | 569 | if (get_panels() == 2) |
paul@0 | 570 | _set_descriptor(desc_vaddr[1], get_framebuffer(1, fb_paddr), |
paul@69 | 571 | get_aligned_size(), |
paul@69 | 572 | desc_paddr + 1, |
paul@75 | 573 | _command_irq()); |
paul@0 | 574 | |
paul@0 | 575 | // Initialise palette descriptor details for lower colour depths. |
paul@0 | 576 | |
paul@0 | 577 | if (have_palette) |
paul@0 | 578 | _set_descriptor(desc_vaddr[2], get_palette(fb_paddr), |
paul@69 | 579 | get_aligned_palette_size(), |
paul@0 | 580 | desc_paddr, |
paul@0 | 581 | Command_palette_buffer); |
paul@0 | 582 | |
paul@0 | 583 | // Flush cached structure data. |
paul@0 | 584 | |
paul@0 | 585 | l4_cache_clean_data((unsigned long) desc_vaddr, |
paul@0 | 586 | (unsigned long) desc_vaddr + get_descriptors_size()); |
paul@0 | 587 | |
paul@0 | 588 | // Configure DMA by setting frame descriptor addresses. |
paul@0 | 589 | |
paul@0 | 590 | // Provide the palette descriptor address first, if employed. |
paul@0 | 591 | |
paul@0 | 592 | _regs[Desc_address_0] = (uint32_t) (have_palette ? desc_paddr + 2 : desc_paddr); |
paul@0 | 593 | |
paul@0 | 594 | // Provide a descriptor for the second DMA channel in dual-panel mode. |
paul@0 | 595 | |
paul@75 | 596 | if (get_panels() == 2) |
paul@0 | 597 | _regs[Desc_address_1] = (uint32_t) (desc_paddr + 1); |
paul@0 | 598 | |
paul@0 | 599 | // Initialise panel-related registers. |
paul@0 | 600 | |
paul@0 | 601 | _init_panel(); |
paul@0 | 602 | |
paul@0 | 603 | // Initialise the control and configuration registers. |
paul@0 | 604 | |
paul@69 | 605 | _regs[Lcd_control] = _control_panel() | _control_bpp() | _control_transfer() | _control_irq(); |
paul@0 | 606 | _regs[Lcd_config] = _panel->config; |
paul@69 | 607 | } |
paul@69 | 608 | |
paul@69 | 609 | // Set the interrupt for controller-related events. |
paul@69 | 610 | |
paul@69 | 611 | void |
paul@69 | 612 | Lcd_jz4740_chip::set_irq(l4_cap_idx_t irq, enum Jz4740_lcd_irq_condition conditions) |
paul@69 | 613 | { |
paul@69 | 614 | _irq = irq; |
paul@69 | 615 | _irq_conditions = conditions; |
paul@69 | 616 | } |
paul@69 | 617 | |
paul@69 | 618 | // Wait for an interrupt condition. |
paul@69 | 619 | |
paul@69 | 620 | long |
paul@69 | 621 | Lcd_jz4740_chip::wait_for_irq() |
paul@69 | 622 | { |
paul@69 | 623 | l4_msgtag_t tag; |
paul@69 | 624 | |
paul@69 | 625 | _regs[Lcd_status] = _regs[Lcd_status] & ~(_status_irq()); |
paul@69 | 626 | |
paul@69 | 627 | // Wait for a condition. |
paul@69 | 628 | |
paul@72 | 629 | tag = l4_irq_receive(_irq, l4_timeout(L4_IPC_TIMEOUT_NEVER, l4util_micros2l4to(2000000))); |
paul@72 | 630 | |
paul@69 | 631 | // Acknowledge interrupts. |
paul@69 | 632 | |
paul@72 | 633 | _regs[Lcd_status] = 0; |
paul@69 | 634 | |
paul@72 | 635 | // Return errors. |
paul@72 | 636 | |
paul@72 | 637 | return l4_error(tag); |
paul@0 | 638 | } |
paul@0 | 639 | |
paul@0 | 640 | |
paul@0 | 641 | |
paul@0 | 642 | // C language interface functions. |
paul@0 | 643 | |
paul@0 | 644 | void * |
paul@0 | 645 | jz4740_lcd_init(l4_addr_t lcd_base, struct Jz4740_lcd_panel *panel) |
paul@0 | 646 | { |
paul@0 | 647 | return (void *) new Lcd_jz4740_chip(lcd_base, panel); |
paul@0 | 648 | } |
paul@0 | 649 | |
paul@0 | 650 | void |
paul@0 | 651 | jz4740_lcd_config(void *lcd, struct Jz4740_lcd_descriptor *desc_vaddr, |
paul@0 | 652 | struct Jz4740_lcd_descriptor *desc_paddr, |
paul@0 | 653 | l4_addr_t fb_paddr) |
paul@0 | 654 | { |
paul@0 | 655 | static_cast<Lcd_jz4740_chip *>(lcd)->config(desc_vaddr, desc_paddr, fb_paddr); |
paul@0 | 656 | } |
paul@0 | 657 | |
paul@0 | 658 | void |
paul@69 | 659 | jz4740_lcd_set_irq(void *lcd, l4_cap_idx_t irq, enum Jz4740_lcd_irq_condition conditions) |
paul@69 | 660 | { |
paul@69 | 661 | static_cast<Lcd_jz4740_chip *>(lcd)->set_irq(irq, conditions); |
paul@69 | 662 | } |
paul@69 | 663 | |
paul@69 | 664 | long |
paul@69 | 665 | jz4740_lcd_wait_for_irq(void *lcd) |
paul@69 | 666 | { |
paul@69 | 667 | return static_cast<Lcd_jz4740_chip *>(lcd)->wait_for_irq(); |
paul@69 | 668 | } |
paul@69 | 669 | |
paul@69 | 670 | void |
paul@0 | 671 | jz4740_lcd_disable(void *lcd) |
paul@0 | 672 | { |
paul@0 | 673 | static_cast<Lcd_jz4740_chip *>(lcd)->disable(); |
paul@0 | 674 | } |
paul@0 | 675 | |
paul@0 | 676 | void |
paul@0 | 677 | jz4740_lcd_disable_quick(void *lcd) |
paul@0 | 678 | { |
paul@0 | 679 | static_cast<Lcd_jz4740_chip *>(lcd)->disable_quick(); |
paul@0 | 680 | } |
paul@0 | 681 | |
paul@0 | 682 | void |
paul@0 | 683 | jz4740_lcd_enable(void *lcd) |
paul@0 | 684 | { |
paul@0 | 685 | static_cast<Lcd_jz4740_chip *>(lcd)->enable(); |
paul@0 | 686 | } |
paul@0 | 687 | |
paul@0 | 688 | int |
paul@69 | 689 | jz4740_lcd_enabled(void *lcd) |
paul@69 | 690 | { |
paul@69 | 691 | return (int) static_cast<Lcd_jz4740_chip *>(lcd)->enabled(); |
paul@69 | 692 | } |
paul@69 | 693 | |
paul@69 | 694 | int |
paul@0 | 695 | jz4740_lcd_get_pixel_clock(void *lcd) |
paul@0 | 696 | { |
paul@0 | 697 | return static_cast<Lcd_jz4740_chip *>(lcd)->get_pixel_clock(); |
paul@0 | 698 | } |
paul@0 | 699 | |
paul@69 | 700 | l4_size_t |
paul@69 | 701 | jz4740_lcd_get_descriptors_size(void *lcd) |
paul@69 | 702 | { |
paul@69 | 703 | return static_cast<Lcd_jz4740_chip *>(lcd)->get_descriptors_size(); |
paul@69 | 704 | } |
paul@69 | 705 | |
paul@69 | 706 | l4_size_t |
paul@72 | 707 | jz4740_lcd_get_line_size(void *lcd) |
paul@72 | 708 | { |
paul@72 | 709 | return static_cast<Lcd_jz4740_chip *>(lcd)->get_line_size(); |
paul@72 | 710 | } |
paul@72 | 711 | |
paul@72 | 712 | l4_size_t |
paul@69 | 713 | jz4740_lcd_get_screen_size(void *lcd) |
paul@69 | 714 | { |
paul@69 | 715 | return static_cast<Lcd_jz4740_chip *>(lcd)->get_screen_size(); |
paul@69 | 716 | } |
paul@69 | 717 | |
paul@0 | 718 | l4_addr_t |
paul@0 | 719 | jz4740_lcd_get_palette(void *lcd, l4_addr_t base) |
paul@0 | 720 | { |
paul@0 | 721 | return static_cast<Lcd_jz4740_chip *>(lcd)->get_palette(base); |
paul@0 | 722 | } |
paul@0 | 723 | |
paul@0 | 724 | void |
paul@0 | 725 | jz4740_lcd_init_palette(void *lcd, l4_addr_t palette) |
paul@0 | 726 | { |
paul@0 | 727 | static_cast<Lcd_jz4740_chip *>(lcd)->init_palette(palette); |
paul@0 | 728 | } |