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/dl.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 // Panel definition. 50 51 static struct Jz4740_lcd_panel *panel; 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 chip->disable(); 96 chip->config((struct Jz4740_lcd_descriptor *) desc_vaddr, 97 (struct Jz4740_lcd_descriptor *) desc_paddr, 98 fb_paddr); 99 100 // Initialise the clocks for the LCD controller. 101 102 set_timing(); 103 enable_display(); 104 105 chip->enable(); 106 } 107 108 // Set up memory addresses for the peripheral, initialising the framebuffer and 109 // descriptor members. 110 111 int 112 Lcd_jz4740_device::_setup_memory() 113 { 114 // Framebuffer and descriptor sizes. 115 116 unsigned long fb_size, desc_size; 117 118 // Size of physically contiguous regions to be allocated. 119 120 l4_size_t fb_size_out, desc_size_out; 121 122 // Memory allocation capability. 123 124 L4::Cap<L4Re::Dataspace> descmem; 125 126 // Test for existing setup. 127 128 if (fb_vaddr) 129 return 0; 130 131 // Obtain capabilities for allocating memory. 132 133 _fbmem = L4Re::Util::cap_alloc.alloc<L4Re::Dataspace>(); 134 if (!_fbmem.is_valid()) return 1; 135 136 descmem = L4Re::Util::cap_alloc.alloc<L4Re::Dataspace>(); 137 if (!descmem.is_valid()) return 1; 138 139 // Obtain the memory requirements. 140 141 Lcd_jz4740_chip *chip = static_cast<Lcd_jz4740_chip *>(_chip); 142 143 fb_size = chip->get_screen_size(); 144 desc_size = chip->get_descriptors_size(); 145 146 // Allocate memory for the framebuffer at 2**6 == 64 byte == 16 word alignment, 147 // also for the descriptors. 148 149 const l4_size_t alloc_flags = L4Re::Mem_alloc::Continuous | L4Re::Mem_alloc::Pinned; 150 151 if (L4Re::Env::env()->mem_alloc()->alloc(fb_size, _fbmem, alloc_flags, 6) || 152 L4Re::Env::env()->mem_alloc()->alloc(desc_size, descmem, alloc_flags, 6)) 153 return 1; 154 155 // Map the allocated memory, obtaining virtual addresses. 156 157 const l4_size_t attach_flags = L4Re::Rm::Search_addr | L4Re::Rm::Eager_map; 158 159 fb_vaddr = 0; 160 desc_vaddr = 0; 161 162 if (L4Re::Env::env()->rm()->attach(&fb_vaddr, fb_size, attach_flags, _fbmem, 0) || 163 L4Re::Env::env()->rm()->attach(&desc_vaddr, desc_size, attach_flags, descmem, 0)) 164 return 1; 165 166 // Obtain physical addresses for the framebuffer and descriptors. 167 168 fb_paddr = 0; 169 desc_paddr = 0; 170 171 fb_size_out = fb_size; 172 desc_size_out = desc_size; 173 174 if (_fbmem->phys(0, fb_paddr, fb_size_out) || 175 descmem->phys(0, desc_paddr, desc_size_out)) 176 return 1; 177 178 // Test the mapped region sizes. 179 180 if ((fb_size_out != fb_size) || (desc_size_out != desc_size)) 181 return 1; 182 183 return 0; 184 } 185 186 // Populate a view information structure. 187 188 void 189 Lcd_jz4740_device::get_view_info(l4re_video_view_info_t *view_info) 190 { 191 // Populate the L4Re framebuffer description with details from the 192 // somewhat similar panel description. 193 194 Lcd_jz4740_chip *chip = static_cast<Lcd_jz4740_chip *>(_chip); 195 struct Jz4740_lcd_panel *panel = chip->get_panel(); 196 197 view_info->width = panel->width; 198 view_info->height = panel->height; 199 view_info->pixel_info.bytes_per_pixel = (panel->bpp + 7) / 8; 200 view_info->bytes_per_line = chip->get_line_size(); 201 202 switch (panel->bpp) 203 { 204 case 32: 205 view_info->pixel_info.r.shift = 16; view_info->pixel_info.r.size = 8; 206 view_info->pixel_info.g.shift = 8; view_info->pixel_info.g.size = 8; 207 view_info->pixel_info.b.shift = 0; view_info->pixel_info.b.size = 8; 208 break; 209 210 case 8: 211 view_info->pixel_info.r.shift = 5; view_info->pixel_info.r.size = 3; 212 view_info->pixel_info.g.shift = 2; view_info->pixel_info.g.size = 3; 213 view_info->pixel_info.b.shift = 0; view_info->pixel_info.b.size = 2; 214 break; 215 216 case 4: 217 view_info->pixel_info.r.shift = 3; view_info->pixel_info.r.size = 1; 218 view_info->pixel_info.g.shift = 1; view_info->pixel_info.g.size = 2; 219 view_info->pixel_info.b.shift = 0; view_info->pixel_info.b.size = 1; 220 break; 221 222 case 16: 223 default: 224 view_info->pixel_info.r.shift = 11; view_info->pixel_info.r.size = 5; 225 view_info->pixel_info.g.shift = 5; view_info->pixel_info.g.size = 6; 226 view_info->pixel_info.b.shift = 0; view_info->pixel_info.b.size = 5; 227 break; 228 } 229 230 view_info->pixel_info.a.shift = 0; 231 view_info->pixel_info.a.size = 0; 232 } 233 234 // Access to peripheral memory. 235 236 static int setup_memory() 237 { 238 if (get_memory("jz4740-lcd", &lcd_virt_base, &lcd_virt_base_end)) 239 return 1; 240 241 // Obtain access to the CPM and display devices. 242 243 cpm_device = L4Re::Env::env()->get_cap<Cpm_device_interface>("cpm"); 244 if (!cpm_device.is_valid()) return 1; 245 246 display_device = L4Re::Env::env()->get_cap<Display_device_interface>("display"); 247 if (!display_device.is_valid()) return 1; 248 249 // Load the panel data. 250 251 void *(*panel_get)(void); 252 panel_get = (void *(*)(void)) load_function("rom/mips-jz4740-panel.txt", "panel_get"); 253 if (!panel_get) return 1; 254 255 panel = (struct Jz4740_lcd_panel *) panel_get(); 256 257 // Initialise the LCD abstraction. 258 259 lcd_chip = new Lcd_jz4740_chip(lcd_virt_base, panel); 260 261 return 0; 262 } 263 264 // Device initialisation. 265 266 Lcd_jz4740_device *Lcd_jz4740_device::device = 0; 267 268 Lcd_device *Lcd_device::get_device() 269 { 270 if (Lcd_jz4740_device::device) return Lcd_jz4740_device::device; 271 272 if (setup_memory()) return 0; 273 274 // Initialise the common device. 275 276 Lcd_jz4740_device::device = new Lcd_jz4740_device(lcd_chip, display_device); 277 278 return Lcd_jz4740_device::device; 279 }