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