1 /* 2 * Access the LCD peripheral on the Ben NanoNote. 3 * 4 * Copyright (C) 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-jz4740.h> 23 24 #include <l4/devices/lcd-jz4740.h> 25 #include <l4/devices/lcd-jz4740-config.h> 26 #include <l4/devices/lcd-jz4740-panel.h> 27 #include <l4/devices/panel.h> 28 29 #include <l4/devices/memory.h> 30 31 #include <l4/re/c/dataspace.h> 32 #include <l4/re/c/dma_space.h> 33 #include <l4/re/c/mem_alloc.h> 34 #include <l4/re/c/rm.h> 35 #include <l4/re/c/util/cap_alloc.h> 36 #include <l4/re/env.h> 37 #include <l4/re/protocols.h> 38 39 #include <l4/sys/cache.h> 40 #include <l4/sys/err.h> 41 #include <l4/sys/factory.h> 42 #include <l4/sys/icu.h> 43 #include <l4/sys/ipc.h> 44 #include <l4/sys/irq.h> 45 #include <l4/sys/rcv_endpoint.h> 46 47 #include <l4/io/io.h> 48 #include <l4/vbus/vbus.h> 49 50 #include <stdio.h> 51 #include <unistd.h> 52 #include <stdint.h> 53 #include <string.h> 54 #include <stdlib.h> 55 56 57 58 /* Device and resource discovery. */ 59 60 static long item_in_range(long start, long end, long index) 61 { 62 if (start < end) 63 return start + index; 64 else 65 return start - index; 66 } 67 68 int main(void) 69 { 70 long err; 71 int i; 72 73 /* Peripheral memory. */ 74 75 l4_addr_t cpm_base = 0, cpm_base_end = 0; 76 l4_addr_t lcd_base = 0, lcd_base_end = 0; 77 78 /* Peripheral abstractions. */ 79 80 void *cpm; 81 void *lcd; 82 83 /* Allocated memory. */ 84 85 l4_cap_idx_t desc_mem, fb_mem, dma, vbus; 86 l4_size_t desc_size, desc_psize, fb_size, fb_psize; 87 l4_addr_t desc_addr, fb_addr; 88 l4re_dma_space_dma_addr_t desc_paddr, fb_paddr; 89 unsigned char *picture; 90 unsigned char *fb_picture, *fb_picture_row; 91 unsigned int x, y; 92 93 /* Access to IRQs. */ 94 95 l4_uint32_t lcd_irq_start = 0, lcd_irq_end = 0; 96 l4_cap_idx_t icu, lcd_irq; 97 98 /* Capability allocation. */ 99 100 desc_mem = l4re_util_cap_alloc(); 101 fb_mem = l4re_util_cap_alloc(); 102 dma = l4re_util_cap_alloc(); 103 lcd_irq = l4re_util_cap_alloc(); 104 icu = l4re_env_get_cap("icu"); 105 vbus = l4re_env_get_cap("vbus"); 106 107 if (l4_is_invalid_cap(icu)) 108 { 109 printf("No 'icu' capability available in the virtual bus.\n"); 110 return 1; 111 } 112 113 if (l4_is_invalid_cap(desc_mem) || l4_is_invalid_cap(fb_mem) || 114 l4_is_invalid_cap(lcd_irq)) 115 { 116 printf("Capabilities could not be reserved.\n"); 117 return 1; 118 } 119 120 /* Obtain resource details describing the interrupt for HDMI I2C. */ 121 122 printf("Access IRQ...\n"); 123 124 if (get_irq("jz4740-lcd", &lcd_irq_start, &lcd_irq_end) < 0) 125 return 1; 126 127 printf("LCD IRQ range at %d...%d.\n", lcd_irq_start, lcd_irq_end); 128 129 /* Create interrupt objects. */ 130 131 err = l4_error(l4_factory_create_irq(l4re_global_env->factory, lcd_irq)); 132 133 if (err) 134 { 135 printf("Could not create IRQ object: %lx\n", err); 136 return 1; 137 } 138 139 /* Bind interrupt objects to IRQ numbers. */ 140 141 err = l4_error(l4_icu_bind(icu, 142 item_in_range(lcd_irq_start, lcd_irq_end, 0), 143 lcd_irq)); 144 145 if (err) 146 { 147 printf("Could not bind IRQ to the ICU: %ld\n", err); 148 return 1; 149 } 150 151 /* Attach ourselves to the interrupt handler with some arbitrary labels. */ 152 153 err = l4_error(l4_rcv_ep_bind_thread(lcd_irq, l4re_env()->main_thread, 0x10)); 154 155 if (err) 156 { 157 printf("Could not attach to IRQs: %ld\n", err); 158 return 1; 159 } 160 161 /* Obtain resource details describing I/O memory. */ 162 163 printf("Access CPM...\n"); 164 165 if (get_memory("jz4740-cpm", &cpm_base, &cpm_base_end) < 0) 166 return 1; 167 168 printf("CPM at 0x%lx...0x%lx.\n", cpm_base, cpm_base_end); 169 170 printf("Access LCD...\n"); 171 172 if (get_memory("jz4740-lcd", &lcd_base, &lcd_base_end) < 0) 173 return 1; 174 175 printf("LCD at 0x%lx...0x%lx.\n", lcd_base, lcd_base_end); 176 177 /* Obtain CPM object. */ 178 179 cpm = jz4740_cpm_init(cpm_base); 180 181 /* Obtain LCD reference. */ 182 183 printf("Set up LCD...\n"); 184 185 lcd = jz4740_lcd_init(lcd_base, panel_get()); 186 187 /* Test initialisation with a frequency appropriate for the test panel. */ 188 189 jz4740_cpm_stop_clock(cpm, Clock_lcd); 190 jz4740_cpm_set_frequency(cpm, Clock_frequency_lcd, jz4740_lcd_get_pixel_clock(lcd) * 3); 191 jz4740_cpm_set_frequency(cpm, Clock_frequency_lcd_pixel, jz4740_lcd_get_pixel_clock(lcd)); 192 jz4740_cpm_update_output_frequency(cpm); 193 194 /* Create the DMA space. */ 195 196 err = l4_error(l4_factory_create(l4re_env()->mem_alloc, L4RE_PROTO_DMA_SPACE, dma)); 197 198 if (err) 199 { 200 printf("Could not create DMA space: %s\n", l4sys_errtostr(err)); 201 return 1; 202 } 203 204 l4vbus_device_handle_t device = L4VBUS_NULL; 205 l4vbus_resource_t dma_resource; 206 207 if (!find_resource(&device, &dma_resource, L4VBUS_RESOURCE_DMA_DOMAIN)) 208 { 209 printf("Could not find DMA domain.\n"); 210 return 1; 211 } 212 213 err = l4vbus_assign_dma_domain(vbus, dma_resource.start, 214 L4VBUS_DMAD_BIND | L4VBUS_DMAD_L4RE_DMA_SPACE, 215 dma); 216 217 if (err) 218 { 219 printf("Could not assign DMA space: %s\n", l4sys_errtostr(err)); 220 return 1; 221 } 222 223 /* Allocate descriptors and framebuffer at 2**8 == 256 byte == 64 word alignment. */ 224 225 desc_size = jz4740_lcd_get_descriptors_size(lcd); 226 fb_size = jz4740_lcd_get_screen_size(lcd); 227 228 err = l4re_ma_alloc_align(desc_size, desc_mem, L4RE_MA_CONTINUOUS | L4RE_MA_PINNED, 8) || 229 l4re_ma_alloc_align(fb_size, fb_mem, L4RE_MA_CONTINUOUS | L4RE_MA_PINNED, 8); 230 231 if (err) 232 { 233 printf("Could not allocate memory: %s\n", l4sys_errtostr(err)); 234 return 1; 235 } 236 237 err = l4re_rm_attach((void **) &desc_addr, desc_size, 238 L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_EAGER_MAP | L4RE_RM_F_RW, 239 desc_mem, 0, L4_PAGESHIFT) || 240 l4re_rm_attach((void **) &fb_addr, fb_size, 241 L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_EAGER_MAP | L4RE_RM_F_RW, 242 fb_mem, 0, L4_PAGESHIFT); 243 244 if (err) 245 { 246 printf("Could not map memory: %s\n", l4sys_errtostr(err)); 247 return 1; 248 } 249 250 err = l4re_dma_space_map(dma, desc_mem | L4_CAP_FPAGE_RW, 0, &desc_psize, 0, 251 L4RE_DMA_SPACE_TO_DEVICE, &desc_paddr) || 252 l4re_dma_space_map(dma, fb_mem | L4_CAP_FPAGE_RW, 0, &fb_psize, 0, 253 L4RE_DMA_SPACE_TO_DEVICE, &fb_paddr); 254 255 if (err) 256 { 257 printf("Could not get physical addresses for memory: %s\n", l4sys_errtostr(err)); 258 return 1; 259 } 260 261 printf("Descriptors at %lx/%llx, size %d/%d.\n", desc_addr, desc_paddr, desc_size, desc_psize); 262 printf("Framebuffer at %lx/%llx, size %d/%d.\n", fb_addr, fb_paddr, fb_size, fb_psize); 263 264 //memset((void *) fb_addr, 0x7f, fb_size); 265 266 picture = (unsigned char *) malloc(gimp_image.width * gimp_image.height * gimp_image.bytes_per_pixel); 267 268 GIMP_IMAGE_RUN_LENGTH_DECODE(picture, 269 gimp_image.rle_pixel_data, 270 gimp_image.width * gimp_image.height, 271 gimp_image.bytes_per_pixel); 272 273 fb_picture_row = (unsigned char *) fb_addr; 274 275 for (y = 0; y < gimp_image.height; y++) 276 { 277 fb_picture = fb_picture_row; 278 279 for (x = 0; x < gimp_image.width; x++) 280 { 281 *(fb_picture + 2) = *picture++; 282 *(fb_picture + 1) = *picture++; 283 *fb_picture = *picture++; 284 *(fb_picture + 3) = 0; 285 fb_picture += 4; 286 } 287 288 fb_picture_row += jz4740_lcd_get_line_size(lcd); 289 } 290 291 l4_cache_clean_data((unsigned long) fb_addr, (unsigned long) fb_addr + fb_size); 292 293 printf("Start LCD clock and initialise LCD...\n"); 294 295 jz4740_cpm_start_clock(cpm, Clock_lcd); 296 l4_sleep(1); // 1ms == 1000us 297 298 jz4740_lcd_disable(lcd); 299 300 jz4740_lcd_set_irq(lcd, lcd_irq, Lcd_irq_frame_end); 301 302 jz4740_lcd_config(lcd, (struct Jz4740_lcd_descriptor *) desc_addr, 303 (struct Jz4740_lcd_descriptor *) desc_paddr, 304 fb_paddr); 305 306 jz4740_lcd_enable(lcd); 307 308 printf("LCD enabled: %s\n", jz4740_lcd_enabled(lcd) ? "yes" : "no"); 309 310 printf("Wait for interrupt conditions...\n"); 311 312 for (i = 0; i < 30000; i++) 313 { 314 if (jz4740_lcd_wait_for_irq(lcd)) 315 continue; 316 317 if (!(i % 60)) 318 printf("IRQ #%d\n", i); 319 } 320 321 /* Detach from the interrupts. */ 322 323 err = l4_error(l4_irq_detach(lcd_irq)); 324 325 if (err) 326 printf("Error detaching from IRQ: %ld\n", err); 327 328 printf("Done.\n"); 329 330 return 0; 331 }