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_lcd, jz4740_lcd_get_pixel_clock(lcd) * 3); 191 jz4740_cpm_set_frequency(cpm, Clock_lcd_pixel, jz4740_lcd_get_pixel_clock(lcd)); 192 193 /* Create the DMA space. */ 194 195 err = l4_error(l4_factory_create(l4re_env()->mem_alloc, L4RE_PROTO_DMA_SPACE, dma)); 196 197 if (err) 198 { 199 printf("Could not create DMA space: %s\n", l4sys_errtostr(err)); 200 return 1; 201 } 202 203 l4vbus_device_handle_t device = L4VBUS_NULL; 204 l4vbus_resource_t dma_resource; 205 206 if (!find_resource(&device, &dma_resource, L4VBUS_RESOURCE_DMA_DOMAIN)) 207 { 208 printf("Could not find DMA domain.\n"); 209 return 1; 210 } 211 212 err = l4vbus_assign_dma_domain(vbus, dma_resource.start, 213 L4VBUS_DMAD_BIND | L4VBUS_DMAD_L4RE_DMA_SPACE, 214 dma); 215 216 if (err) 217 { 218 printf("Could not assign DMA space: %s\n", l4sys_errtostr(err)); 219 return 1; 220 } 221 222 /* Allocate descriptors and framebuffer at 2**8 == 256 byte == 64 word alignment. */ 223 224 desc_size = jz4740_lcd_get_descriptors_size(lcd); 225 fb_size = jz4740_lcd_get_screen_size(lcd); 226 227 err = l4re_ma_alloc_align(desc_size, desc_mem, L4RE_MA_CONTINUOUS | L4RE_MA_PINNED, 8) || 228 l4re_ma_alloc_align(fb_size, fb_mem, L4RE_MA_CONTINUOUS | L4RE_MA_PINNED, 8); 229 230 if (err) 231 { 232 printf("Could not allocate memory: %s\n", l4sys_errtostr(err)); 233 return 1; 234 } 235 236 err = l4re_rm_attach((void **) &desc_addr, desc_size, 237 L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_EAGER_MAP | L4RE_RM_F_RW, 238 desc_mem, 0, L4_PAGESHIFT) || 239 l4re_rm_attach((void **) &fb_addr, fb_size, 240 L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_EAGER_MAP | L4RE_RM_F_RW, 241 fb_mem, 0, L4_PAGESHIFT); 242 243 if (err) 244 { 245 printf("Could not map memory: %s\n", l4sys_errtostr(err)); 246 return 1; 247 } 248 249 err = l4re_dma_space_map(dma, desc_mem | L4_CAP_FPAGE_RW, 0, &desc_psize, 0, 250 L4RE_DMA_SPACE_TO_DEVICE, &desc_paddr) || 251 l4re_dma_space_map(dma, fb_mem | L4_CAP_FPAGE_RW, 0, &fb_psize, 0, 252 L4RE_DMA_SPACE_TO_DEVICE, &fb_paddr); 253 254 if (err) 255 { 256 printf("Could not get physical addresses for memory: %s\n", l4sys_errtostr(err)); 257 return 1; 258 } 259 260 printf("Descriptors at %lx/%llx, size %d/%d.\n", desc_addr, desc_paddr, desc_size, desc_psize); 261 printf("Framebuffer at %lx/%llx, size %d/%d.\n", fb_addr, fb_paddr, fb_size, fb_psize); 262 263 //memset((void *) fb_addr, 0x7f, fb_size); 264 265 picture = (unsigned char *) malloc(gimp_image.width * gimp_image.height * gimp_image.bytes_per_pixel); 266 267 GIMP_IMAGE_RUN_LENGTH_DECODE(picture, 268 gimp_image.rle_pixel_data, 269 gimp_image.width * gimp_image.height, 270 gimp_image.bytes_per_pixel); 271 272 fb_picture_row = (unsigned char *) fb_addr; 273 274 for (y = 0; y < gimp_image.height; y++) 275 { 276 fb_picture = fb_picture_row; 277 278 for (x = 0; x < gimp_image.width; x++) 279 { 280 *(fb_picture + 2) = *picture++; 281 *(fb_picture + 1) = *picture++; 282 *fb_picture = *picture++; 283 *(fb_picture + 3) = 0; 284 fb_picture += 4; 285 } 286 287 fb_picture_row += jz4740_lcd_get_line_size(lcd); 288 } 289 290 l4_cache_clean_data((unsigned long) fb_addr, (unsigned long) fb_addr + fb_size); 291 292 printf("Start LCD clock and initialise LCD...\n"); 293 294 jz4740_cpm_start_clock(cpm, Clock_lcd); 295 l4_sleep(1); // 1ms == 1000us 296 297 jz4740_lcd_disable(lcd); 298 299 jz4740_lcd_set_irq(lcd, lcd_irq, Lcd_irq_frame_end); 300 301 jz4740_lcd_config(lcd, (struct Jz4740_lcd_descriptor *) desc_addr, 302 (struct Jz4740_lcd_descriptor *) (l4_addr_t) desc_paddr, 303 fb_paddr); 304 305 jz4740_lcd_enable(lcd); 306 307 printf("LCD enabled: %s\n", jz4740_lcd_enabled(lcd) ? "yes" : "no"); 308 309 printf("Wait for interrupt conditions...\n"); 310 311 for (i = 0; i < 30000; i++) 312 { 313 if (jz4740_lcd_wait_for_irq(lcd)) 314 continue; 315 316 if (!(i % 60)) 317 printf("IRQ #%d\n", i); 318 } 319 320 /* Detach from the interrupts. */ 321 322 err = l4_error(l4_irq_detach(lcd_irq)); 323 324 if (err) 325 printf("Error detaching from IRQ: %ld\n", err); 326 327 printf("Done.\n"); 328 329 return 0; 330 }