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