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_lcd_frequencies(pclk, 3); 68 cpm_device->update_output_frequency(); 69 cpm_device->start_clock(Clock_lcd); 70 71 l4_sleep(1); // 1ms == 1000us 72 } 73 74 75 76 // Disable the display. 77 78 void 79 Lcd_jz4740_device::disable() 80 { 81 Lcd_jz4740_chip *chip = static_cast<Lcd_jz4740_chip *>(_chip); 82 83 disable_display(); 84 chip->disable(); 85 } 86 87 // Configure the peripheral and enable the display. 88 89 void 90 Lcd_jz4740_device::enable() 91 { 92 Lcd_jz4740_chip *chip = static_cast<Lcd_jz4740_chip *>(_chip); 93 94 // Initialise the clocks for the LCD controller. Without this, the controller 95 // cannot be operated. 96 97 set_timing(); 98 99 // Configure the controller. 100 101 chip->disable(); 102 chip->config((struct Jz4740_lcd_descriptor *) desc_vaddr, 103 (struct Jz4740_lcd_descriptor *) desc_paddr, 104 fb_paddr); 105 106 // Activate the display channel. 107 108 enable_display(); 109 110 // Activate the controller output. 111 112 chip->enable(); 113 } 114 115 // Set up memory addresses for the peripheral, initialising the framebuffer and 116 // descriptor members. 117 118 int 119 Lcd_jz4740_device::_setup_memory() 120 { 121 // Framebuffer and descriptor sizes. 122 123 unsigned long fb_size, desc_size; 124 125 // Size of physically contiguous regions to be allocated. 126 127 l4_size_t fb_size_out, desc_size_out; 128 129 // Memory allocation capability. 130 131 L4::Cap<L4Re::Dataspace> descmem; 132 133 // Test for existing setup. 134 135 if (fb_vaddr) 136 return 0; 137 138 // Obtain capabilities for allocating memory. 139 140 _fbmem = L4Re::Util::cap_alloc.alloc<L4Re::Dataspace>(); 141 if (!_fbmem.is_valid()) return 1; 142 143 descmem = L4Re::Util::cap_alloc.alloc<L4Re::Dataspace>(); 144 if (!descmem.is_valid()) return 1; 145 146 // Obtain the memory requirements. 147 148 Lcd_jz4740_chip *chip = static_cast<Lcd_jz4740_chip *>(_chip); 149 150 fb_size = chip->get_screen_size(); 151 desc_size = chip->get_descriptors_size(); 152 153 // Allocate memory for the framebuffer at 2**6 == 64 byte == 16 word alignment, 154 // also for the descriptors. 155 // NOTE: A 64 word burst mode is available on the JZ4780 that would 156 // NOTE: necessitate 64 word alignment for the framebuffer. 157 158 const l4_size_t alloc_flags = L4Re::Mem_alloc::Continuous | L4Re::Mem_alloc::Pinned; 159 160 if (L4Re::Env::env()->mem_alloc()->alloc(fb_size, _fbmem, alloc_flags, 8) || 161 L4Re::Env::env()->mem_alloc()->alloc(desc_size, descmem, alloc_flags, 6)) 162 return 1; 163 164 // Map the allocated memory, obtaining virtual addresses. 165 166 const l4_size_t attach_flags = L4Re::Rm::Search_addr | L4Re::Rm::Eager_map; 167 168 fb_vaddr = 0; 169 desc_vaddr = 0; 170 171 if (L4Re::Env::env()->rm()->attach(&fb_vaddr, fb_size, attach_flags, _fbmem, 0) || 172 L4Re::Env::env()->rm()->attach(&desc_vaddr, desc_size, attach_flags, descmem, 0)) 173 return 1; 174 175 // Obtain physical addresses for the framebuffer and descriptors. 176 177 fb_paddr = 0; 178 desc_paddr = 0; 179 180 fb_size_out = fb_size; 181 desc_size_out = desc_size; 182 183 if (_fbmem->phys(0, fb_paddr, fb_size_out) || 184 descmem->phys(0, desc_paddr, desc_size_out)) 185 return 1; 186 187 // Test the mapped region sizes. 188 189 if ((fb_size_out != fb_size) || (desc_size_out != desc_size)) 190 return 1; 191 192 return 0; 193 } 194 195 // Populate a view information structure. 196 197 void 198 Lcd_jz4740_device::get_view_info(l4re_video_view_info_t *view_info) 199 { 200 // Populate the L4Re framebuffer description with details from the 201 // somewhat similar panel description. 202 203 Lcd_jz4740_chip *chip = static_cast<Lcd_jz4740_chip *>(_chip); 204 struct Jz4740_lcd_panel *panel = chip->get_panel(); 205 206 view_info->width = panel->width; 207 view_info->height = panel->height; 208 view_info->pixel_info.bytes_per_pixel = (panel->bpp + 7) / 8; 209 view_info->bytes_per_line = chip->get_line_size(); 210 211 switch (panel->bpp) 212 { 213 // NOTE: 24bpp here is actually RGBA or RGB padded. 214 215 case 24: 216 view_info->pixel_info.bytes_per_pixel = 4; 217 view_info->pixel_info.r.shift = 16; view_info->pixel_info.r.size = 8; 218 view_info->pixel_info.g.shift = 8; view_info->pixel_info.g.size = 8; 219 view_info->pixel_info.b.shift = 0; view_info->pixel_info.b.size = 8; 220 break; 221 222 case 32: 223 view_info->pixel_info.r.shift = 16; view_info->pixel_info.r.size = 8; 224 view_info->pixel_info.g.shift = 8; view_info->pixel_info.g.size = 8; 225 view_info->pixel_info.b.shift = 0; view_info->pixel_info.b.size = 8; 226 break; 227 228 case 8: 229 view_info->pixel_info.r.shift = 5; view_info->pixel_info.r.size = 3; 230 view_info->pixel_info.g.shift = 2; view_info->pixel_info.g.size = 3; 231 view_info->pixel_info.b.shift = 0; view_info->pixel_info.b.size = 2; 232 break; 233 234 case 4: 235 view_info->pixel_info.r.shift = 3; view_info->pixel_info.r.size = 1; 236 view_info->pixel_info.g.shift = 1; view_info->pixel_info.g.size = 2; 237 view_info->pixel_info.b.shift = 0; view_info->pixel_info.b.size = 1; 238 break; 239 240 case 16: 241 default: 242 view_info->pixel_info.r.shift = 11; view_info->pixel_info.r.size = 5; 243 view_info->pixel_info.g.shift = 5; view_info->pixel_info.g.size = 6; 244 view_info->pixel_info.b.shift = 0; view_info->pixel_info.b.size = 5; 245 break; 246 } 247 248 view_info->pixel_info.a.shift = 0; 249 view_info->pixel_info.a.size = 0; 250 } 251 252 // Access to peripheral memory. 253 254 static int setup_memory() 255 { 256 bool is_jz4780 = false; 257 258 if (get_memory("jz4740-lcd", &lcd_virt_base, &lcd_virt_base_end)) 259 { 260 if (get_memory("jz4780-lcd", &lcd_virt_base, &lcd_virt_base_end)) 261 return 1; 262 else 263 is_jz4780 = true; 264 } 265 266 // Obtain access to the CPM and display devices. 267 268 l4_cap_idx_t cpm = l4re_env_get_cap("cpm"); 269 if (!l4_is_valid_cap(cpm)) return 1; 270 271 l4_cap_idx_t display = l4re_env_get_cap("display"); 272 if (!l4_is_valid_cap(display)) return 1; 273 274 static client_Activation display_obj(display); 275 display_device = &display_obj; 276 277 static client_CPM cpm_obj(cpm); 278 cpm_device = &cpm_obj; 279 280 // Load the panel data from the configured library. 281 282 struct Jz4740_lcd_panel *panel = (struct Jz4740_lcd_panel *) load_panel(); 283 284 // Initialise the LCD abstraction. 285 286 if (is_jz4780) 287 lcd_chip = new Lcd_jz4780_chip(lcd_virt_base, panel); 288 else 289 lcd_chip = new Lcd_jz4740_chip(lcd_virt_base, panel); 290 291 return 0; 292 } 293 294 // Device initialisation. 295 296 Lcd_jz4740_device *Lcd_jz4740_device::device = 0; 297 298 Lcd_device *Lcd_device::get_device() 299 { 300 if (Lcd_jz4740_device::device) return Lcd_jz4740_device::device; 301 302 if (setup_memory()) return 0; 303 304 // Initialise the common device. 305 306 Lcd_jz4740_device::device = new Lcd_jz4740_device(lcd_chip, display_device); 307 308 return Lcd_jz4740_device::device; 309 }