1 /* 2 * Common LCD device support for the JZ4740 and related SoCs. 3 * 4 * Copyright (C) 2018, 2020, 2021 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <l4/devices/lcd-jz4740.h> 23 #include <l4/devices/lcd-jz4780.h> 24 #include <l4/devices/panel-loader.h> 25 #include <l4/devices/memory.h> 26 #include "lcd-jz4740-device.h" 27 28 #include <l4/re/dataspace> 29 #include <l4/re/env> 30 #include <l4/re/env.h> 31 #include <l4/re/mem_alloc> 32 #include <l4/re/rm> 33 #include <l4/re/util/cap_alloc> 34 #include <l4/sys/types.h> 35 36 #include <stdint.h> 37 38 #include "activation_client.h" 39 #include "cpm_client.h" 40 41 // Virtual addresses for the LCD register block. 42 43 static l4_addr_t lcd_virt_base = 0, lcd_virt_base_end = 0; 44 45 // LCD, CPM and display device abstractions. 46 47 static Lcd_jz4740_chip *lcd_chip = 0; 48 49 static Activation *display_device; 50 static CPM *cpm_device; 51 52 53 54 // CPM operations. 55 56 static void set_timing() 57 { 58 uint32_t pclk = lcd_chip->get_pixel_clock(); 59 60 cpm_device->stop_clock(Clock_lcd); 61 62 // Original comment: LCDClock > 2.5*Pixclock 63 // However, the documentation indicates that a TFT panel needs a device clock 64 // 1.5 times that of the pixel clock, and a STN panel needs a device clock 3 65 // times that of the pixel clock. 66 67 cpm_device->set_frequency(Clock_frequency_lcd, pclk * 3); 68 cpm_device->set_frequency(Clock_frequency_lcd_pixel, pclk); 69 cpm_device->update_output_frequency(); 70 cpm_device->start_clock(Clock_lcd); 71 72 l4_sleep(1); // 1ms == 1000us 73 } 74 75 76 77 // Disable the display. 78 79 void 80 Lcd_jz4740_device::disable() 81 { 82 Lcd_jz4740_chip *chip = static_cast<Lcd_jz4740_chip *>(_chip); 83 84 disable_display(); 85 chip->disable(); 86 } 87 88 // Configure the peripheral and enable the display. 89 90 void 91 Lcd_jz4740_device::enable() 92 { 93 Lcd_jz4740_chip *chip = static_cast<Lcd_jz4740_chip *>(_chip); 94 95 // Initialise the clocks for the LCD controller. Without this, the controller 96 // cannot be operated. 97 98 set_timing(); 99 100 // Configure the controller. 101 102 chip->disable(); 103 chip->config((struct Jz4740_lcd_descriptor *) desc_vaddr, 104 (struct Jz4740_lcd_descriptor *) desc_paddr, 105 fb_paddr); 106 107 // Activate the display channel. 108 109 enable_display(); 110 111 // Activate the controller output. 112 113 chip->enable(); 114 } 115 116 // Set up memory addresses for the peripheral, initialising the framebuffer and 117 // descriptor members. 118 119 int 120 Lcd_jz4740_device::_setup_memory() 121 { 122 // Framebuffer and descriptor sizes. 123 124 unsigned long fb_size, desc_size; 125 126 // Size of physically contiguous regions to be allocated. 127 128 l4_size_t fb_size_out, desc_size_out; 129 130 // Memory allocation capability. 131 132 L4::Cap<L4Re::Dataspace> descmem; 133 134 // Test for existing setup. 135 136 if (fb_vaddr) 137 return 0; 138 139 // Obtain capabilities for allocating memory. 140 141 _fbmem = L4Re::Util::cap_alloc.alloc<L4Re::Dataspace>(); 142 if (!_fbmem.is_valid()) return 1; 143 144 descmem = L4Re::Util::cap_alloc.alloc<L4Re::Dataspace>(); 145 if (!descmem.is_valid()) return 1; 146 147 // Obtain the memory requirements. 148 149 Lcd_jz4740_chip *chip = static_cast<Lcd_jz4740_chip *>(_chip); 150 151 fb_size = chip->get_screen_size(); 152 desc_size = chip->get_descriptors_size(); 153 154 // Allocate memory for the framebuffer at 2**6 == 64 byte == 16 word alignment, 155 // also for the descriptors. 156 // NOTE: A 64 word burst mode is available on the JZ4780 that would 157 // NOTE: necessitate 64 word alignment for the framebuffer. 158 159 const l4_size_t alloc_flags = L4Re::Mem_alloc::Continuous | L4Re::Mem_alloc::Pinned; 160 161 if (L4Re::Env::env()->mem_alloc()->alloc(fb_size, _fbmem, alloc_flags, 8) || 162 L4Re::Env::env()->mem_alloc()->alloc(desc_size, descmem, alloc_flags, 6)) 163 return 1; 164 165 // Map the allocated memory, obtaining virtual addresses. 166 167 const l4_size_t attach_flags = L4Re::Rm::Search_addr | L4Re::Rm::Eager_map; 168 169 fb_vaddr = 0; 170 desc_vaddr = 0; 171 172 if (L4Re::Env::env()->rm()->attach(&fb_vaddr, fb_size, attach_flags, _fbmem, 0) || 173 L4Re::Env::env()->rm()->attach(&desc_vaddr, desc_size, attach_flags, descmem, 0)) 174 return 1; 175 176 // Obtain physical addresses for the framebuffer and descriptors. 177 178 fb_paddr = 0; 179 desc_paddr = 0; 180 181 fb_size_out = fb_size; 182 desc_size_out = desc_size; 183 184 if (_fbmem->phys(0, fb_paddr, fb_size_out) || 185 descmem->phys(0, desc_paddr, desc_size_out)) 186 return 1; 187 188 // Test the mapped region sizes. 189 190 if ((fb_size_out != fb_size) || (desc_size_out != desc_size)) 191 return 1; 192 193 return 0; 194 } 195 196 // Populate a view information structure. 197 198 void 199 Lcd_jz4740_device::get_view_info(l4re_video_view_info_t *view_info) 200 { 201 // Populate the L4Re framebuffer description with details from the 202 // somewhat similar panel description. 203 204 Lcd_jz4740_chip *chip = static_cast<Lcd_jz4740_chip *>(_chip); 205 struct Jz4740_lcd_panel *panel = chip->get_panel(); 206 207 view_info->width = panel->width; 208 view_info->height = panel->height; 209 view_info->pixel_info.bytes_per_pixel = (panel->bpp + 7) / 8; 210 view_info->bytes_per_line = chip->get_line_size(); 211 212 switch (panel->bpp) 213 { 214 // NOTE: 24bpp here is actually RGBA or RGB padded. 215 216 case 24: 217 view_info->pixel_info.bytes_per_pixel = 4; 218 view_info->pixel_info.r.shift = 16; view_info->pixel_info.r.size = 8; 219 view_info->pixel_info.g.shift = 8; view_info->pixel_info.g.size = 8; 220 view_info->pixel_info.b.shift = 0; view_info->pixel_info.b.size = 8; 221 break; 222 223 case 32: 224 view_info->pixel_info.r.shift = 16; view_info->pixel_info.r.size = 8; 225 view_info->pixel_info.g.shift = 8; view_info->pixel_info.g.size = 8; 226 view_info->pixel_info.b.shift = 0; view_info->pixel_info.b.size = 8; 227 break; 228 229 case 8: 230 view_info->pixel_info.r.shift = 5; view_info->pixel_info.r.size = 3; 231 view_info->pixel_info.g.shift = 2; view_info->pixel_info.g.size = 3; 232 view_info->pixel_info.b.shift = 0; view_info->pixel_info.b.size = 2; 233 break; 234 235 case 4: 236 view_info->pixel_info.r.shift = 3; view_info->pixel_info.r.size = 1; 237 view_info->pixel_info.g.shift = 1; view_info->pixel_info.g.size = 2; 238 view_info->pixel_info.b.shift = 0; view_info->pixel_info.b.size = 1; 239 break; 240 241 case 16: 242 default: 243 view_info->pixel_info.r.shift = 11; view_info->pixel_info.r.size = 5; 244 view_info->pixel_info.g.shift = 5; view_info->pixel_info.g.size = 6; 245 view_info->pixel_info.b.shift = 0; view_info->pixel_info.b.size = 5; 246 break; 247 } 248 249 view_info->pixel_info.a.shift = 0; 250 view_info->pixel_info.a.size = 0; 251 } 252 253 // Access to peripheral memory. 254 255 static int setup_memory() 256 { 257 bool is_jz4780 = false; 258 259 if (get_memory("jz4740-lcd", &lcd_virt_base, &lcd_virt_base_end)) 260 { 261 if (get_memory("jz4780-lcd", &lcd_virt_base, &lcd_virt_base_end)) 262 return 1; 263 else 264 is_jz4780 = true; 265 } 266 267 // Obtain access to the CPM and display devices. 268 269 l4_cap_idx_t cpm = l4re_env_get_cap("cpm"); 270 if (!l4_is_valid_cap(cpm)) return 1; 271 272 l4_cap_idx_t display = l4re_env_get_cap("display"); 273 if (!l4_is_valid_cap(display)) return 1; 274 275 static client_Activation display_obj(display); 276 display_device = &display_obj; 277 278 static client_CPM cpm_obj(cpm); 279 cpm_device = &cpm_obj; 280 281 // Load the panel data from the configured library. 282 283 struct Jz4740_lcd_panel *panel = (struct Jz4740_lcd_panel *) load_panel(); 284 285 // Initialise the LCD abstraction. 286 287 if (is_jz4780) 288 lcd_chip = new Lcd_jz4780_chip(lcd_virt_base, panel); 289 else 290 lcd_chip = new Lcd_jz4740_chip(lcd_virt_base, panel); 291 292 return 0; 293 } 294 295 // Device initialisation. 296 297 Lcd_jz4740_device *Lcd_jz4740_device::device = 0; 298 299 Lcd_device *Lcd_device::get_device() 300 { 301 if (Lcd_jz4740_device::device) return Lcd_jz4740_device::device; 302 303 if (setup_memory()) return 0; 304 305 // Initialise the common device. 306 307 Lcd_jz4740_device::device = new Lcd_jz4740_device(lcd_chip, display_device); 308 309 return Lcd_jz4740_device::device; 310 }