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