1 /* 2 * Common LCD device support for the JZ4740 and related SoCs. 3 * 4 * Copyright (C) 2018, 2020, 2023 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/dma_space> 32 #include <l4/re/env> 33 #include <l4/re/mem_alloc> 34 #include <l4/re/rm> 35 #include <l4/re/util/cap_alloc> 36 #include <l4/sys/factory> 37 #include <l4/sys/types.h> 38 #include <l4/vbus/vbus> 39 40 #include <stdint.h> 41 42 // Virtual addresses for the LCD register block. 43 44 static l4_addr_t lcd_virt_base = 0, lcd_virt_base_end = 0; 45 46 // LCD, CPM and display device abstractions. 47 48 static Lcd_jz4740_chip *lcd_chip = 0; 49 50 static L4::Cap<Cpm_device_interface> cpm_device; 51 static L4::Cap<Display_device_interface> display_device; 52 53 54 55 // CPM operations. 56 57 static void set_timing() 58 { 59 uint32_t pclk = lcd_chip->get_pixel_clock(); 60 61 cpm_device->stop_lcd(); 62 63 // Original comment: LCDClock > 2.5*Pixclock 64 // However, the documentation indicates that a TFT panel needs a device clock 65 // 1.5 times that of the pixel clock, and a STN panel needs a device clock 3 66 // times that of the pixel clock. 67 68 cpm_device->set_lcd_frequencies(pclk, 3); 69 cpm_device->update_output_frequency(); 70 cpm_device->start_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 capabilities. 131 132 L4::Cap<L4Re::Dataspace> descmem; 133 L4::Cap<L4Re::Dma_space> dma; 134 135 // Test for existing setup. 136 137 if (fb_vaddr) 138 return 0; 139 140 // Obtain capabilities for allocating memory. 141 142 _fbmem = L4Re::Util::cap_alloc.alloc<L4Re::Dataspace>(); 143 if (!_fbmem.is_valid()) return 1; 144 145 descmem = L4Re::Util::cap_alloc.alloc<L4Re::Dataspace>(); 146 if (!descmem.is_valid()) return 1; 147 148 dma = L4Re::Util::cap_alloc.alloc<L4Re::Dma_space>(); 149 if (!dma.is_valid()) return 1; 150 151 // Create the DMA space. 152 153 L4::Cap<L4vbus::Vbus> vbus = L4Re::Env::env()->get_cap<L4vbus::Vbus>("vbus"); 154 if (!vbus.is_valid()) return 1; 155 156 if (l4_error(L4Re::Env::env()->user_factory()->create(dma))) 157 return 1; 158 159 l4vbus_device_handle_t device = L4VBUS_NULL; 160 l4vbus_resource_t dma_resource; 161 162 if (!find_resource(&device, &dma_resource, L4VBUS_RESOURCE_DMA_DOMAIN)) 163 return 1; 164 165 if (vbus->assign_dma_domain(dma_resource.start, 166 L4VBUS_DMAD_BIND | L4VBUS_DMAD_L4RE_DMA_SPACE, 167 dma)) 168 return 1; 169 170 // Obtain the memory requirements. 171 172 Lcd_jz4740_chip *chip = static_cast<Lcd_jz4740_chip *>(_chip); 173 174 fb_size = chip->get_screen_size(); 175 desc_size = chip->get_descriptors_size(); 176 177 // Allocate memory for the framebuffer at 2**6 == 64 byte == 16 word alignment, 178 // also for the descriptors. 179 // NOTE: A 64 word burst mode is available on the JZ4780 that would 180 // NOTE: necessitate 64 word alignment for the framebuffer. 181 182 const l4_size_t alloc_flags = L4Re::Mem_alloc::Continuous | L4Re::Mem_alloc::Pinned; 183 184 if (L4Re::Env::env()->mem_alloc()->alloc(fb_size, _fbmem, alloc_flags, 8) || 185 L4Re::Env::env()->mem_alloc()->alloc(desc_size, descmem, alloc_flags, 6)) 186 return 1; 187 188 // Map the allocated memory, obtaining virtual addresses. 189 190 const L4Re::Rm::F::Flags flags = L4Re::Rm::F::Search_addr | 191 L4Re::Rm::F::Eager_map | 192 L4Re::Rm::F::RW; 193 194 fb_vaddr = 0; 195 desc_vaddr = 0; 196 197 if (L4Re::Env::env()->rm()->attach(&fb_vaddr, fb_size, flags, _fbmem, 0) || 198 L4Re::Env::env()->rm()->attach(&desc_vaddr, desc_size, flags, descmem, 0)) 199 return 1; 200 201 // Obtain physical addresses for the framebuffer and descriptors. 202 203 fb_paddr = 0; 204 desc_paddr = 0; 205 206 fb_size_out = fb_size; 207 desc_size_out = desc_size; 208 209 if (dma->map(L4::Ipc::make_cap_rw(_fbmem), 0, &fb_size_out, 210 L4Re::Dma_space::Attributes::None, 211 L4Re::Dma_space::Direction::To_device, 212 &fb_paddr)) 213 return 1; 214 215 if (dma->map(L4::Ipc::make_cap_rw(descmem), 0, &desc_size_out, 216 L4Re::Dma_space::Attributes::None, 217 L4Re::Dma_space::Direction::To_device, 218 &desc_paddr)) 219 return 1; 220 221 // Test the mapped region sizes. 222 223 if ((fb_size_out != fb_size) || (desc_size_out != desc_size)) 224 return 1; 225 226 return 0; 227 } 228 229 // Populate a view information structure. 230 231 void 232 Lcd_jz4740_device::get_view_info(l4re_video_view_info_t *view_info) 233 { 234 // Populate the L4Re framebuffer description with details from the 235 // somewhat similar panel description. 236 237 Lcd_jz4740_chip *chip = static_cast<Lcd_jz4740_chip *>(_chip); 238 struct Jz4740_lcd_panel *panel = chip->get_panel(); 239 240 view_info->width = panel->width; 241 view_info->height = panel->height; 242 view_info->pixel_info.bytes_per_pixel = (panel->bpp + 7) / 8; 243 view_info->bytes_per_line = chip->get_line_size(); 244 245 switch (panel->bpp) 246 { 247 // NOTE: 24bpp here is actually RGBA or RGB padded. 248 249 case 24: 250 view_info->pixel_info.bytes_per_pixel = 4; 251 view_info->pixel_info.r.shift = 16; view_info->pixel_info.r.size = 8; 252 view_info->pixel_info.g.shift = 8; view_info->pixel_info.g.size = 8; 253 view_info->pixel_info.b.shift = 0; view_info->pixel_info.b.size = 8; 254 break; 255 256 case 32: 257 view_info->pixel_info.r.shift = 16; view_info->pixel_info.r.size = 8; 258 view_info->pixel_info.g.shift = 8; view_info->pixel_info.g.size = 8; 259 view_info->pixel_info.b.shift = 0; view_info->pixel_info.b.size = 8; 260 break; 261 262 case 8: 263 view_info->pixel_info.r.shift = 5; view_info->pixel_info.r.size = 3; 264 view_info->pixel_info.g.shift = 2; view_info->pixel_info.g.size = 3; 265 view_info->pixel_info.b.shift = 0; view_info->pixel_info.b.size = 2; 266 break; 267 268 case 4: 269 view_info->pixel_info.r.shift = 3; view_info->pixel_info.r.size = 1; 270 view_info->pixel_info.g.shift = 1; view_info->pixel_info.g.size = 2; 271 view_info->pixel_info.b.shift = 0; view_info->pixel_info.b.size = 1; 272 break; 273 274 case 16: 275 default: 276 view_info->pixel_info.r.shift = 11; view_info->pixel_info.r.size = 5; 277 view_info->pixel_info.g.shift = 5; view_info->pixel_info.g.size = 6; 278 view_info->pixel_info.b.shift = 0; view_info->pixel_info.b.size = 5; 279 break; 280 } 281 282 view_info->pixel_info.a.shift = 0; 283 view_info->pixel_info.a.size = 0; 284 } 285 286 // Access to peripheral memory. 287 288 static int setup_memory() 289 { 290 bool is_jz4780 = false; 291 292 if (get_memory("jz4740-lcd", &lcd_virt_base, &lcd_virt_base_end)) 293 { 294 if (get_memory("jz4780-lcd", &lcd_virt_base, &lcd_virt_base_end)) 295 return 1; 296 else 297 is_jz4780 = true; 298 } 299 300 // Obtain access to the CPM and display devices. 301 302 cpm_device = L4Re::Env::env()->get_cap<Cpm_device_interface>("cpm"); 303 if (!cpm_device.is_valid()) return 1; 304 305 display_device = L4Re::Env::env()->get_cap<Display_device_interface>("display"); 306 if (!display_device.is_valid()) return 1; 307 308 // Load the panel data from the configured library. 309 310 struct Jz4740_lcd_panel *panel = (struct Jz4740_lcd_panel *) load_panel(); 311 312 // Initialise the LCD abstraction. 313 314 if (is_jz4780) 315 lcd_chip = new Lcd_jz4780_chip(lcd_virt_base, panel); 316 else 317 lcd_chip = new Lcd_jz4740_chip(lcd_virt_base, panel); 318 319 return 0; 320 } 321 322 // Device initialisation. 323 324 Lcd_jz4740_device *Lcd_jz4740_device::device = 0; 325 326 Lcd_device *Lcd_device::get_device() 327 { 328 if (Lcd_jz4740_device::device) return Lcd_jz4740_device::device; 329 330 if (setup_memory()) return 0; 331 332 // Initialise the common device. 333 334 Lcd_jz4740_device::device = new Lcd_jz4740_device(lcd_chip, display_device); 335 336 return Lcd_jz4740_device::device; 337 }